7c44773cd09a9c4cb276b19903534928f711bf62
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2 ;; Copyright (C) 1996,97,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'custom)
31 (require 'gnus)
32 (require 'gnus-sum)
33 (require 'gnus-spec)
34 (require 'gnus-int)
35 (require 'browse-url)
36
37 (defgroup gnus-article nil
38   "Article display."
39   :link '(custom-manual "(gnus)The Article Buffer")
40   :group 'gnus)
41
42 (defgroup gnus-article-hiding nil
43   "Hiding article parts."
44   :link '(custom-manual "(gnus)Article Hiding")
45   :group 'gnus-article)
46
47 (defgroup gnus-article-highlight nil
48   "Article highlighting."
49   :link '(custom-manual "(gnus)Article Highlighting")
50   :group 'gnus-article
51   :group 'gnus-visual)
52
53 (defgroup gnus-article-signature nil
54   "Article signatures."
55   :link '(custom-manual "(gnus)Article Signature")
56   :group 'gnus-article)
57
58 (defgroup gnus-article-headers nil
59   "Article headers."
60   :link '(custom-manual "(gnus)Hiding Headers")
61   :group 'gnus-article)
62
63 (defgroup gnus-article-washing nil
64   "Special commands on articles."
65   :link '(custom-manual "(gnus)Article Washing")
66   :group 'gnus-article)
67
68 (defgroup gnus-article-emphasis nil
69   "Fontisizing articles."
70   :link '(custom-manual "(gnus)Article Fontisizing")
71   :group 'gnus-article)
72
73 (defgroup gnus-article-saving nil
74   "Saving articles."
75   :link '(custom-manual "(gnus)Saving Articles")
76   :group 'gnus-article)
77
78 (defgroup gnus-article-mime nil
79   "Worshiping the MIME wonder."
80   :link '(custom-manual "(gnus)Using MIME")
81   :group 'gnus-article)
82
83 (defgroup gnus-article-buttons nil
84   "Pushable buttons in the article buffer."
85   :link '(custom-manual "(gnus)Article Buttons")
86   :group 'gnus-article)
87
88 (defgroup gnus-article-various nil
89   "Other article options."
90   :link '(custom-manual "(gnus)Misc Article")
91   :group 'gnus-article)
92
93 (defcustom gnus-ignored-headers
94   '("^Path:" "^Expires:" "^Date-Received:" "^References:" "^Xref:" "^Lines:"
95     "^Relay-Version:" "^Message-ID:" "^Approved:" "^Sender:" "^Received:" 
96     "^X-UIDL:" "^MIME-Version:" "^Return-Path:" "^In-Reply-To:"
97     "^Content-Type:" "^Content-Transfer-Encoding:" "^X-WebTV-Signature:"
98     "^X-MimeOLE:" "^X-MSMail-Priority:" "^X-Priority:" "^X-Loop:"
99     "^X-Authentication-Warning:" "^X-MIME-Autoconverted:" "^X-Face:"
100     "^X-Attribution:" "^X-Originating-IP:" "^Delivered-To:"
101     "^NNTP-[-A-Za-z]+:" "^Distribution:" "^X-no-archive:" "^X-Trace:"
102     "^X-Complaints-To:" "^X-NNTP-Posting-Host:" "^X-Orig.*:"
103     "^Abuse-Reports-To:" "^Cache-Post-Path:" "^X-Article-Creation-Date:"
104     "^X-Poster:" "^X-Mail2News-Path:" "^X-Server-Date:" "^X-Cache:"
105     "^Originator:" "^X-Problems-To:" "^X-Auth-User:" "^X-Post-Time:" 
106     "^X-Admin:" "^X-UID:" "^Resent-[-A-Za-z]+:" "^X-Mailing-List:"
107     "^Precedence:" "^Original-[-A-Za-z]+:" "^X-filename:" "^X-Orcpt:"
108     "^Old-Received:" "^X-Pgp-Fingerprint:" "^X-Pgp-Key-Id:"
109     "^X-Pgp-Public-Key-Url:" "^X-Auth:" "^X-From-Line:"
110     "^X-Gnus-Article-Number:" "^X-Majordomo:" "^X-Url:" "^X-Sender:"
111     "^X-Mailing-List:" "^MBOX-Line" "^Priority:" "^X-Pgp" "^X400-[-A-Za-z]+:"
112     "^Status:")
113   "*All headers that start with this regexp will be hidden.
114 This variable can also be a list of regexps of headers to be ignored.
115 If `gnus-visible-headers' is non-nil, this variable will be ignored."
116   :type '(choice :custom-show nil
117                  regexp
118                  (repeat regexp))
119   :group 'gnus-article-hiding)
120
121 (defcustom gnus-visible-headers
122   "From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|X-Sent:"
123   "*All headers that do not match this regexp will be hidden.
124 This variable can also be a list of regexp of headers to remain visible.
125 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
126   :type '(repeat :value-to-internal (lambda (widget value)
127                                       (custom-split-regexp-maybe value))
128                  :match (lambda (widget value)
129                           (or (stringp value)
130                               (widget-editable-list-match widget value)))
131                  regexp)
132   :group 'gnus-article-hiding)
133
134 (defcustom gnus-sorted-header-list
135   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
136     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
137   "*This variable is a list of regular expressions.
138 If it is non-nil, headers that match the regular expressions will
139 be placed first in the article buffer in the sequence specified by
140 this list."
141   :type '(repeat regexp)
142   :group 'gnus-article-hiding)
143
144 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
145   "Headers that are only to be displayed if they have interesting data.
146 Possible values in this list are `empty', `newsgroups', `followup-to',
147 `reply-to', `date', `long-to', and `many-to'."
148   :type '(set (const :tag "Headers with no content." empty)
149               (const :tag "Newsgroups with only one group." newsgroups)
150               (const :tag "Followup-to identical to newsgroups." followup-to)
151               (const :tag "Reply-to identical to from." reply-to)
152               (const :tag "Date less than four days old." date)
153               (const :tag "Very long To header." long-to)
154               (const :tag "Multiple To headers." many-to))
155   :group 'gnus-article-hiding)
156
157 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
158   "Regexp matching signature separator.
159 This can also be a list of regexps.  In that case, it will be checked
160 from head to tail looking for a separator.  Searches will be done from
161 the end of the buffer."
162   :type '(repeat string)
163   :group 'gnus-article-signature)
164
165 (defcustom gnus-signature-limit nil
166    "Provide a limit to what is considered a signature.
167 If it is a number, no signature may not be longer (in characters) than
168 that number.  If it is a floating point number, no signature may be
169 longer (in lines) than that number.  If it is a function, the function
170 will be called without any parameters, and if it returns nil, there is
171 no signature in the buffer.  If it is a string, it will be used as a
172 regexp.  If it matches, the text in question is not a signature."
173   :type '(choice (integer :value 200)
174                  (number :value 4.0)
175                  (function :value fun)
176                  (regexp :value ".*"))
177   :group 'gnus-article-signature)
178
179 (defcustom gnus-hidden-properties '(invisible t intangible t)
180   "Property list to use for hiding text."
181   :type 'sexp
182   :group 'gnus-article-hiding)
183
184 (defcustom gnus-article-x-face-command
185   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
186   "*String or function to be executed to display an X-Face header.
187 If it is a string, the command will be executed in a sub-shell
188 asynchronously.  The compressed face will be piped to this command."
189   :type 'string                         ;Leave function case to Lisp.
190   :group 'gnus-article-washing)
191
192 (defcustom gnus-article-x-face-too-ugly nil
193   "Regexp matching posters whose face shouldn't be shown automatically."
194   :type '(choice regexp (const nil))
195   :group 'gnus-article-washing)
196
197 (defcustom gnus-emphasis-alist
198   (let ((format
199          "\\(\\s-\\|^\\|[-\"]\\|\\s(\\|\\s)\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\\(\\s-\\|[-?!.,;:\"]\\|\\s(\\|\\s)\\)")
200         (types
201          '(("_" "_" underline)
202            ("/" "/" italic)
203            ("\\*" "\\*" bold)
204            ("_/" "/_" underline-italic)
205            ("_\\*" "\\*_" underline-bold)
206            ("\\*/" "/\\*" bold-italic)
207            ("_\\*/" "/\\*_" underline-bold-italic))))
208     `(("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
209        2 3 gnus-emphasis-underline)
210       ,@(mapcar
211          (lambda (spec)
212            (list
213             (format format (car spec) (cadr spec))
214             2 3 (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
215          types)))
216   "*Alist that says how to fontify certain phrases.
217 Each item looks like this:
218
219   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
220
221 The first element is a regular expression to be matched.  The second
222 is a number that says what regular expression grouping used to find
223 the entire emphasized word.  The third is a number that says what
224 regexp grouping should be displayed and highlighted.  The fourth
225 is the face used for highlighting."
226   :type '(repeat (list :value ("" 0 0 default)
227                        regexp
228                        (integer :tag "Match group")
229                        (integer :tag "Emphasize group")
230                        face))
231   :group 'gnus-article-emphasis)
232
233 (defface gnus-emphasis-bold '((t (:bold t)))
234   "Face used for displaying strong emphasized text (*word*)."
235   :group 'gnus-article-emphasis)
236
237 (defface gnus-emphasis-italic '((t (:italic t)))
238   "Face used for displaying italic emphasized text (/word/)."
239   :group 'gnus-article-emphasis)
240
241 (defface gnus-emphasis-underline '((t (:underline t)))
242   "Face used for displaying underlined emphasized text (_word_)."
243   :group 'gnus-article-emphasis)
244
245 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
246   "Face used for displaying underlined bold emphasized text (_*word*_)."
247   :group 'gnus-article-emphasis)
248
249 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
250   "Face used for displaying underlined italic emphasized text (_*word*_)."
251   :group 'gnus-article-emphasis)
252
253 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
254   "Face used for displaying bold italic emphasized text (/*word*/)."
255   :group 'gnus-article-emphasis)
256
257 (defface gnus-emphasis-underline-bold-italic
258   '((t (:bold t :italic t :underline t)))
259   "Face used for displaying underlined bold italic emphasized text.
260 Esample: (_/*word*/_)."
261   :group 'gnus-article-emphasis)
262
263 (defcustom gnus-article-time-format "%a, %b %d %Y %T %Z"
264   "Format for display of Date headers in article bodies.
265 See `format-time-string' for the possible values.
266
267 The variable can also be function, which should return a complete Date
268 header.  The function is called with one argument, the time, which can
269 be fed to `format-time-string'."
270   :type '(choice string symbol)
271   :link '(custom-manual "(gnus)Article Date")
272   :group 'gnus-article-washing)
273
274 (eval-and-compile
275   (autoload 'timezone-make-date-arpa-standard "timezone")
276   (autoload 'mail-extract-address-components "mail-extr"))
277
278 (defcustom gnus-save-all-headers t
279   "*If non-nil, don't remove any headers before saving."
280   :group 'gnus-article-saving
281   :type 'boolean)
282
283 (defcustom gnus-prompt-before-saving 'always
284   "*This variable says how much prompting is to be done when saving articles.
285 If it is nil, no prompting will be done, and the articles will be
286 saved to the default files.  If this variable is `always', each and
287 every article that is saved will be preceded by a prompt, even when
288 saving large batches of articles.  If this variable is neither nil not
289 `always', there the user will be prompted once for a file name for
290 each invocation of the saving commands."
291   :group 'gnus-article-saving
292   :type '(choice (item always)
293                  (item :tag "never" nil)
294                  (sexp :tag "once" :format "%t\n" :value t)))
295
296 (defcustom gnus-saved-headers gnus-visible-headers
297   "Headers to keep if `gnus-save-all-headers' is nil.
298 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
299 If that variable is nil, however, all headers that match this regexp
300 will be kept while the rest will be deleted before saving."
301   :group 'gnus-article-saving
302   :type 'regexp)
303
304 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
305   "A function to save articles in your favourite format.
306 The function must be interactively callable (in other words, it must
307 be an Emacs command).
308
309 Gnus provides the following functions:
310
311 * gnus-summary-save-in-rmail (Rmail format)
312 * gnus-summary-save-in-mail (Unix mail format)
313 * gnus-summary-save-in-folder (MH folder)
314 * gnus-summary-save-in-file (article format)
315 * gnus-summary-save-in-vm (use VM's folder format)
316 * gnus-summary-write-to-file (article format -- overwrite)."
317   :group 'gnus-article-saving
318   :type '(radio (function-item gnus-summary-save-in-rmail)
319                 (function-item gnus-summary-save-in-mail)
320                 (function-item gnus-summary-save-in-folder)
321                 (function-item gnus-summary-save-in-file)
322                 (function-item gnus-summary-save-in-vm)
323                 (function-item gnus-summary-write-to-file)))
324
325 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
326   "A function generating a file name to save articles in Rmail format.
327 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
328   :group 'gnus-article-saving
329   :type 'function)
330
331 (defcustom gnus-mail-save-name 'gnus-plain-save-name
332   "A function generating a file name to save articles in Unix mail format.
333 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
334   :group 'gnus-article-saving
335   :type 'function)
336
337 (defcustom gnus-folder-save-name 'gnus-folder-save-name
338   "A function generating a file name to save articles in MH folder.
339 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
340   :group 'gnus-article-saving
341   :type 'function)
342
343 (defcustom gnus-file-save-name 'gnus-numeric-save-name
344   "A function generating a file name to save articles in article format.
345 The function is called with NEWSGROUP, HEADERS, and optional
346 LAST-FILE."
347   :group 'gnus-article-saving
348   :type 'function)
349
350 (defcustom gnus-split-methods
351   '((gnus-article-archive-name)
352     (gnus-article-nndoc-name))
353   "*Variable used to suggest where articles are to be saved.
354 For instance, if you would like to save articles related to Gnus in
355 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
356 you could set this variable to something like:
357
358  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
359    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
360
361 This variable is an alist where the where the key is the match and the
362 value is a list of possible files to save in if the match is non-nil.
363
364 If the match is a string, it is used as a regexp match on the
365 article.  If the match is a symbol, that symbol will be funcalled
366 from the buffer of the article to be saved with the newsgroup as the
367 parameter.  If it is a list, it will be evaled in the same buffer.
368
369 If this form or function returns a string, this string will be used as
370 a possible file name; and if it returns a non-nil list, that list will
371 be used as possible file names."
372   :group 'gnus-article-saving
373   :type '(repeat (choice (list :value (fun) function)
374                          (cons :value ("" "") regexp (repeat string))
375                          (sexp :value nil))))
376
377 (defcustom gnus-strict-mime t
378   "*If nil, MIME-decode even if there is no Mime-Version header."
379   :group 'gnus-article-mime
380   :type 'boolean)
381
382 (defcustom gnus-show-mime-method 'metamail-buffer
383   "Function to process a MIME message.
384 The function is called from the article buffer."
385   :group 'gnus-article-mime
386   :type 'function)
387
388 (defcustom gnus-decode-encoded-word-method 'gnus-article-de-quoted-unreadable
389   "*Function to decode MIME encoded words.
390 The function is called from the article buffer."
391   :group 'gnus-article-mime
392   :type 'function)
393
394 (defcustom gnus-page-delimiter "^\^L"
395   "*Regexp describing what to use as article page delimiters.
396 The default value is \"^\^L\", which is a form linefeed at the
397 beginning of a line."
398   :type 'regexp
399   :group 'gnus-article-various)
400
401 (defcustom gnus-article-mode-line-format "Gnus: %%b %S"
402   "*The format specification for the article mode line.
403 See `gnus-summary-mode-line-format' for a closer description."
404   :type 'string
405   :group 'gnus-article-various)
406
407 (defcustom gnus-article-mode-hook nil
408   "*A hook for Gnus article mode."
409   :type 'hook
410   :group 'gnus-article-various)
411
412 (defcustom gnus-article-menu-hook nil
413   "*Hook run after the creation of the article mode menu."
414   :type 'hook
415   :group 'gnus-article-various)
416
417 (defcustom gnus-article-prepare-hook nil
418   "*A hook called after an article has been prepared in the article buffer.
419 If you want to run a special decoding program like nkf, use this hook."
420   :type 'hook
421   :group 'gnus-article-various)
422
423 (defcustom gnus-article-hide-pgp-hook nil
424   "*A hook called after successfully hiding a PGP signature."
425   :type 'hook
426   :group 'gnus-article-various)
427
428 (defcustom gnus-article-button-face 'bold
429   "Face used for highlighting buttons in the article buffer.
430
431 An article button is a piece of text that you can activate by pressing
432 `RET' or `mouse-2' above it."
433   :type 'face
434   :group 'gnus-article-buttons)
435
436 (defcustom gnus-article-mouse-face 'highlight
437   "Face used for mouse highlighting in the article buffer.
438
439 Article buttons will be displayed in this face when the cursor is
440 above them."
441   :type 'face
442   :group 'gnus-article-buttons)
443
444 (defcustom gnus-signature-face 'gnus-signature-face
445   "Face used for highlighting a signature in the article buffer.
446 Obsolete; use the face `gnus-signature-face' for customizations instead."
447   :type 'face
448   :group 'gnus-article-highlight
449   :group 'gnus-article-signature)
450
451 (defface gnus-signature-face
452   '((((type x))
453      (:italic t)))
454   "Face used for highlighting a signature in the article buffer."
455   :group 'gnus-article-highlight
456   :group 'gnus-article-signature)
457
458 (defface gnus-header-from-face
459   '((((class color)
460       (background dark))
461      (:foreground "spring green"))
462     (((class color)
463       (background light))
464      (:foreground "red3"))
465     (t
466      (:italic t)))
467   "Face used for displaying from headers."
468   :group 'gnus-article-headers
469   :group 'gnus-article-highlight)
470
471 (defface gnus-header-subject-face
472   '((((class color)
473       (background dark))
474      (:foreground "SeaGreen3"))
475     (((class color)
476       (background light))
477      (:foreground "red4"))
478     (t
479      (:bold t :italic t)))
480   "Face used for displaying subject headers."
481   :group 'gnus-article-headers
482   :group 'gnus-article-highlight)
483
484 (defface gnus-header-newsgroups-face
485   '((((class color)
486       (background dark))
487      (:foreground "yellow" :italic t))
488     (((class color)
489       (background light))
490      (:foreground "MidnightBlue" :italic t))
491     (t
492      (:italic t)))
493   "Face used for displaying newsgroups headers."
494   :group 'gnus-article-headers
495   :group 'gnus-article-highlight)
496
497 (defface gnus-header-name-face
498   '((((class color)
499       (background dark))
500      (:foreground "SeaGreen"))
501     (((class color)
502       (background light))
503      (:foreground "maroon"))
504     (t
505      (:bold t)))
506   "Face used for displaying header names."
507   :group 'gnus-article-headers
508   :group 'gnus-article-highlight)
509
510 (defface gnus-header-content-face
511   '((((class color)
512       (background dark))
513      (:foreground "forest green" :italic t))
514     (((class color)
515       (background light))
516      (:foreground "indianred4" :italic t))
517     (t
518      (:italic t)))  "Face used for displaying header content."
519   :group 'gnus-article-headers
520   :group 'gnus-article-highlight)
521
522 (defcustom gnus-header-face-alist
523   '(("From" nil gnus-header-from-face)
524     ("Subject" nil gnus-header-subject-face)
525     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
526     ("" gnus-header-name-face gnus-header-content-face))
527   "*Controls highlighting of article header.
528
529 An alist of the form (HEADER NAME CONTENT).
530
531 HEADER is a regular expression which should match the name of an
532 header header and NAME and CONTENT are either face names or nil.
533
534 The name of each header field will be displayed using the face
535 specified by the first element in the list where HEADER match the
536 header name and NAME is non-nil.  Similarly, the content will be
537 displayed by the first non-nil matching CONTENT face."
538   :group 'gnus-article-headers
539   :group 'gnus-article-highlight
540   :type '(repeat (list (regexp :tag "Header")
541                        (choice :tag "Name"
542                                (item :tag "skip" nil)
543                                (face :value default))
544                        (choice :tag "Content"
545                                (item :tag "skip" nil)
546                                (face :value default)))))
547
548 ;;; Internal variables
549
550 (defvar article-lapsed-timer nil)
551 (defvar gnus-article-current-summary nil)
552
553 (defvar gnus-article-mode-syntax-table
554   (let ((table (copy-syntax-table text-mode-syntax-table)))
555     (modify-syntax-entry ?- "w" table)
556     (modify-syntax-entry ?> ")" table)
557     (modify-syntax-entry ?< "(" table)
558     table)
559   "Syntax table used in article mode buffers.
560 Initialized from `text-mode-syntax-table.")
561
562 (defvar gnus-save-article-buffer nil)
563
564 (defvar gnus-article-mode-line-format-alist
565   (nconc '((?w (gnus-article-wash-status) ?s))
566          gnus-summary-mode-line-format-alist))
567
568 (defvar gnus-number-of-articles-to-be-saved nil)
569
570 (defvar gnus-inhibit-hiding nil)
571
572 (defsubst gnus-article-hide-text (b e props)
573   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
574   (add-text-properties b e props)
575   (when (memq 'intangible props)
576     (put-text-property
577      (max (1- b) (point-min))
578      b 'intangible (cddr (memq 'intangible props)))))
579
580 (defsubst gnus-article-unhide-text (b e)
581   "Remove hidden text properties from region between B and E."
582   (remove-text-properties b e gnus-hidden-properties)
583   (when (memq 'intangible gnus-hidden-properties)
584     (put-text-property (max (1- b) (point-min))
585                        b 'intangible nil)))
586
587 (defun gnus-article-hide-text-type (b e type)
588   "Hide text of TYPE between B and E."
589   (gnus-article-hide-text
590    b e (cons 'article-type (cons type gnus-hidden-properties))))
591
592 (defun gnus-article-unhide-text-type (b e type)
593   "Unhide text of TYPE between B and E."
594   (remove-text-properties
595    b e (cons 'article-type (cons type gnus-hidden-properties)))
596   (when (memq 'intangible gnus-hidden-properties)
597     (put-text-property (max (1- b) (point-min))
598                        b 'intangible nil)))
599
600 (defun gnus-article-hide-text-of-type (type)
601   "Hide text of TYPE in the current buffer."
602   (save-excursion
603     (let ((b (point-min))
604           (e (point-max)))
605       (while (setq b (text-property-any b e 'article-type type))
606         (add-text-properties b (incf b) gnus-hidden-properties)))))
607
608 (defun gnus-article-delete-text-of-type (type)
609   "Delete text of TYPE in the current buffer."
610   (save-excursion
611     (let ((b (point-min)))
612       (while (setq b (text-property-any b (point-max) 'article-type type))
613         (delete-region
614          b (or (text-property-not-all b (point-max) 'article-type type)
615                (point-max)))))))
616
617 (defun gnus-article-delete-invisible-text ()
618   "Delete all invisible text in the current buffer."
619   (save-excursion
620     (let ((b (point-min)))
621       (while (setq b (text-property-any b (point-max) 'invisible t))
622         (delete-region
623          b (or (text-property-not-all b (point-max) 'invisible t)
624                (point-max)))))))
625
626 (defun gnus-article-text-type-exists-p (type)
627   "Say whether any text of type TYPE exists in the buffer."
628   (text-property-any (point-min) (point-max) 'article-type type))
629
630 (defsubst gnus-article-header-rank ()
631   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
632   (let ((list gnus-sorted-header-list)
633         (i 0))
634     (while list
635       (when (looking-at (car list))
636         (setq list nil))
637       (setq list (cdr list))
638       (incf i))
639     i))
640
641 (defun article-hide-headers (&optional arg delete)
642   "Toggle whether to hide unwanted headers and possibly sort them as well.
643 If given a negative prefix, always show; if given a positive prefix,
644 always hide."
645   (interactive (gnus-article-hidden-arg))
646   (current-buffer)
647   (if (gnus-article-check-hidden-text 'headers arg)
648       ;; Show boring headers as well.
649       (gnus-article-show-hidden-text 'boring-headers)
650     ;; This function might be inhibited.
651     (unless gnus-inhibit-hiding
652       (save-excursion
653         (save-restriction
654           (let ((buffer-read-only nil)
655                 (case-fold-search t)
656                 (props (nconc (list 'article-type 'headers)
657                               gnus-hidden-properties))
658                 (max (1+ (length gnus-sorted-header-list)))
659                 (ignored (when (not gnus-visible-headers)
660                            (cond ((stringp gnus-ignored-headers)
661                                   gnus-ignored-headers)
662                                  ((listp gnus-ignored-headers)
663                                   (mapconcat 'identity gnus-ignored-headers
664                                              "\\|")))))
665                 (visible
666                  (cond ((stringp gnus-visible-headers)
667                         gnus-visible-headers)
668                        ((and gnus-visible-headers
669                              (listp gnus-visible-headers))
670                         (mapconcat 'identity gnus-visible-headers "\\|"))))
671                 (inhibit-point-motion-hooks t)
672                 beg)
673             ;; First we narrow to just the headers.
674             (widen)
675             (goto-char (point-min))
676             ;; Hide any "From " lines at the beginning of (mail) articles.
677             (while (looking-at "From ")
678               (forward-line 1))
679             (unless (bobp)
680               (if delete
681                   (delete-region (point-min) (point))
682                 (gnus-article-hide-text (point-min) (point) props)))
683             ;; Then treat the rest of the header lines.
684             (narrow-to-region
685              (point)
686              (if (search-forward "\n\n" nil t) ; if there's a body
687                  (progn (forward-line -1) (point))
688                (point-max)))
689             ;; Then we use the two regular expressions
690             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
691             ;; select which header lines is to remain visible in the
692             ;; article buffer.
693             (goto-char (point-min))
694             (while (re-search-forward "^[^ \t]*:" nil t)
695               (beginning-of-line)
696               ;; Mark the rank of the header.
697               (put-text-property
698                (point) (1+ (point)) 'message-rank
699                (if (or (and visible (looking-at visible))
700                        (and ignored
701                             (not (looking-at ignored))))
702                    (gnus-article-header-rank)
703                  (+ 2 max)))
704               (forward-line 1))
705             (message-sort-headers-1)
706             (when (setq beg (text-property-any
707                              (point-min) (point-max) 'message-rank (+ 2 max)))
708               ;; We make the unwanted headers invisible.
709               (if delete
710                   (delete-region beg (point-max))
711                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
712                 (gnus-article-hide-text-type beg (point-max) 'headers))
713               ;; Work around XEmacs lossage.
714               (put-text-property (point-min) beg 'invisible nil))))))))
715
716 (defun article-hide-boring-headers (&optional arg)
717   "Toggle hiding of headers that aren't very interesting.
718 If given a negative prefix, always show; if given a positive prefix,
719 always hide."
720   (interactive (gnus-article-hidden-arg))
721   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
722              (not gnus-show-all-headers))
723     (save-excursion
724       (save-restriction
725         (let ((buffer-read-only nil)
726               (list gnus-boring-article-headers)
727               (inhibit-point-motion-hooks t)
728               elem)
729           (nnheader-narrow-to-headers)
730           (while list
731             (setq elem (pop list))
732             (goto-char (point-min))
733             (cond
734              ;; Hide empty headers.
735              ((eq elem 'empty)
736               (while (re-search-forward "^[^:]+:[ \t]*\n[^ \t]" nil t)
737                 (forward-line -1)
738                 (gnus-article-hide-text-type
739                  (progn (beginning-of-line) (point))
740                  (progn
741                    (end-of-line)
742                    (if (re-search-forward "^[^ \t]" nil t)
743                        (match-beginning 0)
744                      (point-max)))
745                  'boring-headers)))
746              ;; Hide boring Newsgroups header.
747              ((eq elem 'newsgroups)
748               (when (equal (gnus-fetch-field "newsgroups")
749                            (gnus-group-real-name
750                             (if (boundp 'gnus-newsgroup-name)
751                                 gnus-newsgroup-name
752                               "")))
753                 (gnus-article-hide-header "newsgroups")))
754              ((eq elem 'followup-to)
755               (when (equal (message-fetch-field "followup-to")
756                            (message-fetch-field "newsgroups"))
757                 (gnus-article-hide-header "followup-to")))
758              ((eq elem 'reply-to)
759               (let ((from (message-fetch-field "from"))
760                     (reply-to (message-fetch-field "reply-to")))
761                 (when (and
762                        from reply-to
763                        (ignore-errors
764                          (equal
765                           (nth 1 (mail-extract-address-components from))
766                           (nth 1 (mail-extract-address-components reply-to)))))
767                   (gnus-article-hide-header "reply-to"))))
768              ((eq elem 'date)
769               (let ((date (message-fetch-field "date")))
770                 (when (and date
771                            (< (gnus-days-between (current-time-string) date)
772                               4))
773                   (gnus-article-hide-header "date"))))
774              ((eq elem 'long-to)
775               (let ((to (message-fetch-field "to")))
776                 (when (> (length to) 1024)
777                   (gnus-article-hide-header "to"))))
778              ((eq elem 'many-to)
779               (let ((to-count 0))
780                 (goto-char (point-min))
781                 (while (re-search-forward "^to:" nil t)
782                   (setq to-count (1+ to-count)))
783                 (when (> to-count 1)
784                   (while (> to-count 0)
785                     (goto-char (point-min))
786                     (save-restriction
787                       (re-search-forward "^to:" nil nil to-count)
788                       (forward-line -1)
789                       (narrow-to-region (point) (point-max))
790                       (gnus-article-hide-header "to"))
791                     (setq to-count (1- to-count)))))))))))))
792
793 (defun gnus-article-hide-header (header)
794   (save-excursion
795     (goto-char (point-min))
796     (when (re-search-forward (concat "^" header ":") nil t)
797       (gnus-article-hide-text-type
798        (progn (beginning-of-line) (point))
799        (progn
800          (end-of-line)
801          (if (re-search-forward "^[^ \t]" nil t)
802              (match-beginning 0)
803            (point-max)))
804        'boring-headers))))
805
806 (defun article-treat-dumbquotes ()
807   "Translate M******** sm*rtq**t*s into proper text."
808   (interactive)
809   (article-translate-characters "\221\222\223\223" "`'\"\""))
810
811 (defun article-translate-characters (from to)
812   "Translate all characters in the body of the article according to FROM and TO.
813 FROM is a string of characters to translate from; to is a string of
814 characters to translate to."
815   (save-excursion
816     (goto-char (point-min))
817     (when (search-forward "\n\n" nil t)
818       (let ((buffer-read-only nil)
819             (x (make-string 225 ?x))
820             (i -1))
821         (while (< (incf i) (length x))
822           (aset x i i))
823         (setq i 0)
824         (while (< i (length from))
825           (aset x (aref from i) (aref to i))
826           (incf i))
827         (translate-region (point) (point-max) x)))))
828
829 (defun article-treat-overstrike ()
830   "Translate overstrikes into bold text."
831   (interactive)
832   (save-excursion
833     (goto-char (point-min))
834     (when (search-forward "\n\n" nil t)
835       (let ((buffer-read-only nil))
836         (while (search-forward "\b" nil t)
837           (let ((next (following-char))
838                 (previous (char-after (- (point) 2))))
839             ;; We do the boldification/underlining by hiding the
840             ;; overstrikes and putting the proper text property
841             ;; on the letters.
842             (cond
843              ((eq next previous)
844               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
845               (put-text-property (point) (1+ (point)) 'face 'bold))
846              ((eq next ?_)
847               (gnus-article-hide-text-type
848                (1- (point)) (1+ (point)) 'overstrike)
849               (put-text-property
850                (- (point) 2) (1- (point)) 'face 'underline))
851              ((eq previous ?_)
852               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
853               (put-text-property
854                (point) (1+ (point)) 'face 'underline)))))))))
855
856 (defun article-fill ()
857   "Format too long lines."
858   (interactive)
859   (save-excursion
860     (let ((buffer-read-only nil))
861       (widen)
862       (goto-char (point-min))
863       (search-forward "\n\n" nil t)
864       (end-of-line 1)
865       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
866             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
867             (adaptive-fill-mode t))
868         (while (not (eobp))
869           (and (>= (current-column) (min fill-column (window-width)))
870                (/= (preceding-char) ?:)
871                (fill-paragraph nil))
872           (end-of-line 2))))))
873
874 (defun article-remove-cr ()
875   "Remove carriage returns from an article."
876   (interactive)
877   (save-excursion
878     (let ((buffer-read-only nil))
879       (goto-char (point-min))
880       (while (search-forward "\r" nil t)
881         (replace-match "" t t)))))
882
883 (defun article-remove-trailing-blank-lines ()
884   "Remove all trailing blank lines from the article."
885   (interactive)
886   (save-excursion
887     (let ((buffer-read-only nil))
888       (goto-char (point-max))
889       (delete-region
890        (point)
891        (progn
892          (while (and (not (bobp))
893                      (looking-at "^[ \t]*$"))
894            (forward-line -1))
895          (forward-line 1)
896          (point))))))
897
898 (defun article-display-x-face (&optional force)
899   "Look for an X-Face header and display it if present."
900   (interactive (list 'force))
901   (save-excursion
902     ;; Delete the old process, if any.
903     (when (process-status "article-x-face")
904       (delete-process "article-x-face"))
905     (let ((inhibit-point-motion-hooks t)
906           (case-fold-search t)
907           from last)
908       (save-restriction
909         (nnheader-narrow-to-headers)
910         (setq from (message-fetch-field "from"))
911         (goto-char (point-min))
912         (while (and gnus-article-x-face-command
913                     (not last)
914                     (or force
915                         ;; Check whether this face is censored.
916                         (not gnus-article-x-face-too-ugly)
917                         (and gnus-article-x-face-too-ugly from
918                              (not (string-match gnus-article-x-face-too-ugly
919                                                 from))))
920                     ;; Has to be present.
921                     (re-search-forward "^X-Face: " nil t))
922           ;; This used to try to do multiple faces (`while' instead of
923           ;; `when' above), but (a) sending multiple EOFs to xv doesn't
924           ;; work (b) it can crash some versions of Emacs (c) are
925           ;; multiple faces really something to encourage?
926           (when (stringp gnus-article-x-face-command)
927             (setq last t))
928           ;; We now have the area of the buffer where the X-Face is stored.
929           (save-excursion
930             (let ((beg (point))
931                   (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
932               ;; We display the face.
933               (if (symbolp gnus-article-x-face-command)
934                   ;; The command is a lisp function, so we call it.
935                   (if (gnus-functionp gnus-article-x-face-command)
936                       (funcall gnus-article-x-face-command beg end)
937                     (error "%s is not a function" gnus-article-x-face-command))
938                 ;; The command is a string, so we interpret the command
939                 ;; as a, well, command, and fork it off.
940                 (let ((process-connection-type nil))
941                   (process-kill-without-query
942                    (start-process
943                     "article-x-face" nil shell-file-name shell-command-switch
944                     gnus-article-x-face-command))
945                   (process-send-region "article-x-face" beg end)
946                   (process-send-eof "article-x-face"))))))))))
947
948 (defun gnus-hack-decode-rfc1522 ()
949   "Emergency hack function for avoiding problems when decoding."
950   (let ((buffer-read-only nil))
951     (goto-char (point-min))
952     ;; Remove encoded TABs.
953     (while (search-forward "=09" nil t)
954       (replace-match " " t t))
955     ;; Remove encoded newlines.
956     (goto-char (point-min))
957     (while (search-forward "=10" nil t)
958       (replace-match " " t t))))
959
960 (defun gnus-article-decode-mime-words ()
961   "Decode all MIME-encoded words in the article."
962   (interactive)
963   (save-excursion
964     (let (buffer-read-only)
965       (mm-decode-words-region (point-min) (point-max)))))
966
967 (defalias 'gnus-decode-rfc1522 'article-decode-rfc1522)
968 (defalias 'gnus-article-decode-rfc1522 'article-decode-rfc1522)
969 (defun article-decode-rfc1522 ()
970   "Remove QP encoding from headers."
971   (let ((inhibit-point-motion-hooks t)
972         (buffer-read-only nil))
973     (save-restriction
974       (message-narrow-to-head)
975       (mm-decode-words-region (point-min) (point-max)))))
976
977 (defun article-de-quoted-unreadable (&optional force)
978   "Translation a quoted-printable-encoded article.
979 If FORCE, decode the article whether it is marked as quoted-printable
980 or not."
981   (interactive (list 'force))
982   (save-excursion
983     (let ((buffer-read-only nil)
984           (type (gnus-fetch-field "content-transfer-encoding")))
985       (gnus-article-decode-rfc1522)
986       (when (or force
987                 (and type (string-match "quoted-printable" (downcase type))))
988         (goto-char (point-min))
989         (search-forward "\n\n" nil 'move)
990         (quoted-printable-decode-region (point) (point-max))))))
991
992 (defun article-mime-decode-quoted-printable-buffer ()
993   "Decode Quoted-Printable in the current buffer."
994   (quoted-printable-decode-region (point-min) (point-max)))
995
996 (defun article-hide-pgp (&optional arg)
997   "Toggle hiding of any PGP headers and signatures in the current article.
998 If given a negative prefix, always show; if given a positive prefix,
999 always hide."
1000   (interactive (gnus-article-hidden-arg))
1001   (unless (gnus-article-check-hidden-text 'pgp arg)
1002     (save-excursion
1003       (let ((inhibit-point-motion-hooks t)
1004             buffer-read-only beg end)
1005         (widen)
1006         (goto-char (point-min))
1007         ;; Hide the "header".
1008         (when (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
1009           (delete-region (1+ (match-beginning 0)) (match-end 0))
1010           (setq beg (point))
1011           ;; Hide the actual signature.
1012           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
1013                (setq end (1+ (match-beginning 0)))
1014                (delete-region
1015                 end
1016                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
1017                     (match-end 0)
1018                   ;; Perhaps we shouldn't hide to the end of the buffer
1019                   ;; if there is no end to the signature?
1020                   (point-max))))
1021           ;; Hide "- " PGP quotation markers.
1022           (when (and beg end)
1023             (narrow-to-region beg end)
1024             (goto-char (point-min))
1025             (while (re-search-forward "^- " nil t)
1026               (delete-region
1027                (match-beginning 0) (match-end 0)))
1028             (widen))
1029           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
1030
1031 (defun article-hide-pem (&optional arg)
1032   "Toggle hiding of any PEM headers and signatures in the current article.
1033 If given a negative prefix, always show; if given a positive prefix,
1034 always hide."
1035   (interactive (gnus-article-hidden-arg))
1036   (unless (gnus-article-check-hidden-text 'pem arg)
1037     (save-excursion
1038       (let (buffer-read-only end)
1039         (widen)
1040         (goto-char (point-min))
1041         ;; hide the horrendously ugly "header".
1042         (and (search-forward "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
1043                              nil
1044                              t)
1045              (setq end (1+ (match-beginning 0)))
1046              (gnus-article-hide-text-type
1047               end
1048               (if (search-forward "\n\n" nil t)
1049                   (match-end 0)
1050                 (point-max))
1051               'pem))
1052         ;; hide the trailer as well
1053         (and (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
1054                              nil
1055                              t)
1056              (gnus-article-hide-text-type
1057               (match-beginning 0) (match-end 0) 'pem))))))
1058
1059 (defun article-hide-signature (&optional arg)
1060   "Hide the signature in the current article.
1061 If given a negative prefix, always show; if given a positive prefix,
1062 always hide."
1063   (interactive (gnus-article-hidden-arg))
1064   (unless (gnus-article-check-hidden-text 'signature arg)
1065     (save-excursion
1066       (save-restriction
1067         (let ((buffer-read-only nil))
1068           (when (gnus-article-narrow-to-signature)
1069             (gnus-article-hide-text-type
1070              (point-min) (point-max) 'signature)))))))
1071
1072 (defun article-strip-leading-blank-lines ()
1073   "Remove all blank lines from the beginning of the article."
1074   (interactive)
1075   (save-excursion
1076     (let ((inhibit-point-motion-hooks t)
1077           buffer-read-only)
1078       (goto-char (point-min))
1079       (when (search-forward "\n\n" nil t)
1080         (while (and (not (eobp))
1081                     (looking-at "[ \t]*$"))
1082           (gnus-delete-line))))))
1083
1084 (defun article-strip-multiple-blank-lines ()
1085   "Replace consecutive blank lines with one empty line."
1086   (interactive)
1087   (save-excursion
1088     (let ((inhibit-point-motion-hooks t)
1089           buffer-read-only)
1090       ;; First make all blank lines empty.
1091       (goto-char (point-min))
1092       (search-forward "\n\n" nil t)
1093       (while (re-search-forward "^[ \t]+$" nil t)
1094         (replace-match "" nil t))
1095       ;; Then replace multiple empty lines with a single empty line.
1096       (goto-char (point-min))
1097       (search-forward "\n\n" nil t)
1098       (while (re-search-forward "\n\n\n+" nil t)
1099         (replace-match "\n\n" t t)))))
1100
1101 (defun article-strip-leading-space ()
1102   "Remove all white space from the beginning of the lines in the article."
1103   (interactive)
1104   (save-excursion
1105     (let ((inhibit-point-motion-hooks t)
1106           buffer-read-only)
1107       (goto-char (point-min))
1108       (search-forward "\n\n" nil t)
1109       (while (re-search-forward "^[ \t]+" nil t)
1110         (replace-match "" t t)))))
1111
1112 (defun article-strip-blank-lines ()
1113   "Strip leading, trailing and multiple blank lines."
1114   (interactive)
1115   (article-strip-leading-blank-lines)
1116   (article-remove-trailing-blank-lines)
1117   (article-strip-multiple-blank-lines))
1118
1119 (defun article-strip-all-blank-lines ()
1120   "Strip all blank lines."
1121   (interactive)
1122   (save-excursion
1123     (let ((inhibit-point-motion-hooks t)
1124           buffer-read-only)
1125       (goto-char (point-min))
1126       (search-forward "\n\n" nil t)
1127       (while (re-search-forward "^[ \t]*\n" nil t)
1128         (replace-match "" t t)))))
1129
1130 (defvar mime::preview/content-list)
1131 (defvar mime::preview-content-info/point-min)
1132 (defun gnus-article-narrow-to-signature ()
1133   "Narrow to the signature; return t if a signature is found, else nil."
1134   (widen)
1135   (let ((inhibit-point-motion-hooks t))
1136     (when (and (boundp 'mime::preview/content-list)
1137                mime::preview/content-list)
1138       ;; We have a MIMEish article, so we use the MIME data to narrow.
1139       (let ((pcinfo (car (last mime::preview/content-list))))
1140         (ignore-errors
1141           (narrow-to-region
1142            (funcall (intern "mime::preview-content-info/point-min") pcinfo)
1143            (point-max)))))
1144
1145     (when (gnus-article-search-signature)
1146       (forward-line 1)
1147       ;; Check whether we have some limits to what we consider
1148       ;; to be a signature.
1149       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
1150                       (list gnus-signature-limit)))
1151             limit limited)
1152         (while (setq limit (pop limits))
1153           (if (or (and (integerp limit)
1154                        (< (- (point-max) (point)) limit))
1155                   (and (floatp limit)
1156                        (< (count-lines (point) (point-max)) limit))
1157                   (and (gnus-functionp limit)
1158                        (funcall limit))
1159                   (and (stringp limit)
1160                        (not (re-search-forward limit nil t))))
1161               ()                        ; This limit did not succeed.
1162             (setq limited t
1163                   limits nil)))
1164         (unless limited
1165           (narrow-to-region (point) (point-max))
1166           t)))))
1167
1168 (defun gnus-article-search-signature ()
1169   "Search the current buffer for the signature separator.
1170 Put point at the beginning of the signature separator."
1171   (let ((cur (point)))
1172     (goto-char (point-max))
1173     (if (if (stringp gnus-signature-separator)
1174             (re-search-backward gnus-signature-separator nil t)
1175           (let ((seps gnus-signature-separator))
1176             (while (and seps
1177                         (not (re-search-backward (car seps) nil t)))
1178               (pop seps))
1179             seps))
1180         t
1181       (goto-char cur)
1182       nil)))
1183
1184 (eval-and-compile
1185   (autoload 'w3-display "w3-parse")
1186   (autoload 'w3-do-setup "w3" "" t)
1187   (autoload 'w3-region "w3-display" "" t))
1188
1189 (defun gnus-article-treat-html ()
1190   "Render HTML."
1191   (interactive)
1192   (let ((cbuf (current-buffer)))
1193     (set-buffer gnus-article-buffer)
1194     (let (buf buffer-read-only b e)
1195       (w3-do-setup)
1196       (goto-char (point-min))
1197       (narrow-to-region
1198        (if (search-forward "\n\n" nil t)
1199            (setq b (point))
1200          (point-max))
1201        (setq e (point-max)))
1202       (with-temp-buffer
1203         (insert-buffer-substring gnus-article-buffer b e)
1204         (require 'url)
1205         (save-window-excursion
1206           (w3-region (point-min) (point-max))
1207           (setq buf (buffer-substring-no-properties (point-min) (point-max)))))
1208       (when buf
1209         (delete-region (point-min) (point-max))
1210         (insert buf))
1211       (widen)
1212       (goto-char (point-min))
1213       (set-window-start (get-buffer-window (current-buffer)) (point-min))
1214       (set-buffer cbuf))))
1215
1216 (defun gnus-article-hidden-arg ()
1217   "Return the current prefix arg as a number, or 0 if no prefix."
1218   (list (if current-prefix-arg
1219             (prefix-numeric-value current-prefix-arg)
1220           0)))
1221
1222 (defun gnus-article-check-hidden-text (type arg)
1223   "Return nil if hiding is necessary.
1224 Arg can be nil or a number.  Nil and positive means hide, negative
1225 means show, 0 means toggle."
1226   (save-excursion
1227     (save-restriction
1228       (widen)
1229       (let ((hide (gnus-article-hidden-text-p type)))
1230         (cond
1231          ((or (null arg)
1232               (> arg 0))
1233           nil)
1234          ((< arg 0)
1235           (gnus-article-show-hidden-text type))
1236          (t
1237           (if (eq hide 'hidden)
1238               (gnus-article-show-hidden-text type)
1239             nil)))))))
1240
1241 (defun gnus-article-hidden-text-p (type)
1242   "Say whether the current buffer contains hidden text of type TYPE."
1243   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
1244     (while (and pos
1245                 (not (get-text-property pos 'invisible)))
1246       (setq pos
1247             (text-property-any (1+ pos) (point-max) 'article-type type)))
1248     (if pos
1249         'hidden
1250       'shown)))
1251
1252 (defun gnus-article-show-hidden-text (type &optional hide)
1253   "Show all hidden text of type TYPE.
1254 If HIDE, hide the text instead."
1255   (save-excursion
1256     (let ((buffer-read-only nil)
1257           (inhibit-point-motion-hooks t)
1258           (end (point-min))
1259           beg)
1260       (while (setq beg (text-property-any end (point-max) 'article-type type))
1261         (goto-char beg)
1262         (setq end (or
1263                    (text-property-not-all beg (point-max) 'article-type type)
1264                    (point-max)))
1265         (if hide
1266             (gnus-article-hide-text beg end gnus-hidden-properties)
1267           (gnus-article-unhide-text beg end))
1268         (goto-char end))
1269       t)))
1270
1271 (defconst article-time-units
1272   `((year . ,(* 365.25 24 60 60))
1273     (week . ,(* 7 24 60 60))
1274     (day . ,(* 24 60 60))
1275     (hour . ,(* 60 60))
1276     (minute . 60)
1277     (second . 1))
1278   "Mapping from time units to seconds.")
1279
1280 (defun article-date-ut (&optional type highlight header)
1281   "Convert DATE date to universal time in the current article.
1282 If TYPE is `local', convert to local time; if it is `lapsed', output
1283 how much time has lapsed since DATE."
1284   (interactive (list 'ut t))
1285   (let* ((header (or header
1286                      (mail-header-date gnus-current-headers)
1287                      (message-fetch-field "date")
1288                      ""))
1289          (date (if (vectorp header) (mail-header-date header)
1290                  header))
1291          (date-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
1292          (inhibit-point-motion-hooks t)
1293          bface eface newline)
1294     (when (and date (not (string= date "")))
1295       (save-excursion
1296         (save-restriction
1297           (nnheader-narrow-to-headers)
1298           (let ((buffer-read-only nil))
1299             ;; Delete any old Date headers.
1300             (if (re-search-forward date-regexp nil t)
1301                 (progn
1302                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
1303                         eface (get-text-property (1- (gnus-point-at-eol))
1304                                                  'face))
1305                   (delete-region (progn (beginning-of-line) (point))
1306                                  (progn (end-of-line) (point)))
1307                   (beginning-of-line))
1308               (goto-char (point-max))
1309               (setq newline t))
1310             (insert (article-make-date-line date type))
1311             ;; Do highlighting.
1312             (beginning-of-line)
1313             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
1314               (put-text-property (match-beginning 1) (1+ (match-end 1))
1315                                  'face bface)
1316               (put-text-property (match-beginning 2) (match-end 2)
1317                                  'face eface))
1318             (when newline
1319               (end-of-line)
1320               (insert "\n"))))))))
1321
1322 (defun article-make-date-line (date type)
1323   "Return a DATE line of TYPE."
1324   (cond
1325    ;; Convert to the local timezone.  We have to slap a
1326    ;; `condition-case' round the calls to the timezone
1327    ;; functions since they aren't particularly resistant to
1328    ;; buggy dates.
1329    ((eq type 'local)
1330     (concat "Date: " (condition-case ()
1331                          (timezone-make-date-arpa-standard date)
1332                        (error date))))
1333    ;; Convert to Universal Time.
1334    ((eq type 'ut)
1335     (concat "Date: "
1336             (condition-case ()
1337                 (timezone-make-date-arpa-standard date nil "UT")
1338               (error date))))
1339    ;; Get the original date from the article.
1340    ((eq type 'original)
1341     (concat "Date: " date))
1342    ;; Let the user define the format.
1343    ((eq type 'user)
1344     (if (gnus-functionp gnus-article-time-format)
1345         (funcall
1346          gnus-article-time-format
1347          (ignore-errors
1348            (gnus-encode-date
1349             (timezone-make-date-arpa-standard
1350              date nil "UT"))))
1351       (concat
1352        "Date: "
1353        (format-time-string gnus-article-time-format
1354                            (ignore-errors
1355                              (gnus-encode-date
1356                               (timezone-make-date-arpa-standard
1357                                date nil "UT")))))))
1358    ;; ISO 8601.
1359    ((eq type 'iso8601)
1360     (concat
1361      "Date: "
1362      (format-time-string "%Y%M%DT%h%m%s"
1363                          (ignore-errors
1364                            (gnus-encode-date
1365                             (timezone-make-date-arpa-standard
1366                              date nil "UT"))))))
1367    ;; Do an X-Sent lapsed format.
1368    ((eq type 'lapsed)
1369     ;; If the date is seriously mangled, the timezone functions are
1370     ;; liable to bug out, so we ignore all errors.
1371     (let* ((now (current-time))
1372            (real-time
1373             (ignore-errors
1374               (gnus-time-minus
1375                (gnus-encode-date
1376                 (timezone-make-date-arpa-standard
1377                  (current-time-string now)
1378                  (current-time-zone now) "UT"))
1379                (gnus-encode-date
1380                 (timezone-make-date-arpa-standard
1381                  date nil "UT")))))
1382            (real-sec (and real-time
1383                           (+ (* (float (car real-time)) 65536)
1384                              (cadr real-time))))
1385            (sec (and real-time (abs real-sec)))
1386            num prev)
1387       (cond
1388        ((null real-time)
1389         "X-Sent: Unknown")
1390        ((zerop sec)
1391         "X-Sent: Now")
1392        (t
1393         (concat
1394          "X-Sent: "
1395          ;; This is a bit convoluted, but basically we go
1396          ;; through the time units for years, weeks, etc,
1397          ;; and divide things to see whether that results
1398          ;; in positive answers.
1399          (mapconcat
1400           (lambda (unit)
1401             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
1402                 ;; The (remaining) seconds are too few to
1403                 ;; be divided into this time unit.
1404                 ""
1405               ;; It's big enough, so we output it.
1406               (setq sec (- sec (* num (cdr unit))))
1407               (prog1
1408                   (concat (if prev ", " "") (int-to-string
1409                                              (floor num))
1410                           " " (symbol-name (car unit))
1411                           (if (> num 1) "s" ""))
1412                 (setq prev t))))
1413           article-time-units "")
1414          ;; If dates are odd, then it might appear like the
1415          ;; article was sent in the future.
1416          (if (> real-sec 0)
1417              " ago"
1418            " in the future"))))))
1419    (t
1420     (error "Unknown conversion type: %s" type))))
1421
1422 (defun article-date-local (&optional highlight)
1423   "Convert the current article date to the local timezone."
1424   (interactive (list t))
1425   (article-date-ut 'local highlight))
1426
1427 (defun article-date-original (&optional highlight)
1428   "Convert the current article date to what it was originally.
1429 This is only useful if you have used some other date conversion
1430 function and want to see what the date was before converting."
1431   (interactive (list t))
1432   (article-date-ut 'original highlight))
1433
1434 (defun article-date-lapsed (&optional highlight)
1435   "Convert the current article date to time lapsed since it was sent."
1436   (interactive (list t))
1437   (article-date-ut 'lapsed highlight))
1438
1439 (defun article-update-date-lapsed ()
1440   "Function to be run from a timer to update the lapsed time line."
1441   (let (deactivate-mark)
1442     (save-excursion
1443       (ignore-errors
1444         (when (gnus-buffer-live-p gnus-article-buffer)
1445           (set-buffer gnus-article-buffer)
1446           (goto-char (point-min))
1447           (when (re-search-forward "^X-Sent:" nil t)
1448             (article-date-lapsed t)))))))
1449
1450 (defun gnus-start-date-timer (&optional n)
1451   "Start a timer to update the X-Sent header in the article buffers.
1452 The numerical prefix says how frequently (in seconds) the function
1453 is to run."
1454   (interactive "p")
1455   (unless n
1456     (setq n 1))
1457   (gnus-stop-date-timer)
1458   (setq article-lapsed-timer
1459         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
1460
1461 (defun gnus-stop-date-timer ()
1462   "Stop the X-Sent timer."
1463   (interactive)
1464   (when article-lapsed-timer
1465     (nnheader-cancel-timer article-lapsed-timer)
1466     (setq article-lapsed-timer nil)))
1467
1468 (defun article-date-user (&optional highlight)
1469   "Convert the current article date to the user-defined format.
1470 This format is defined by the `gnus-article-time-format' variable."
1471   (interactive (list t))
1472   (article-date-ut 'user highlight))
1473
1474 (defun article-date-iso8601 (&optional highlight)
1475   "Convert the current article date to ISO8601."
1476   (interactive (list t))
1477   (article-date-ut 'iso8601 highlight))
1478
1479 (defun article-show-all ()
1480   "Show all hidden text in the article buffer."
1481   (interactive)
1482   (save-excursion
1483     (let ((buffer-read-only nil))
1484       (gnus-article-unhide-text (point-min) (point-max)))))
1485
1486 (defun article-emphasize (&optional arg)
1487   "Emphasize text according to `gnus-emphasis-alist'."
1488   (interactive (gnus-article-hidden-arg))
1489   (unless (gnus-article-check-hidden-text 'emphasis arg)
1490     (save-excursion
1491       (let ((alist gnus-emphasis-alist)
1492             (buffer-read-only nil)
1493             (props (append '(article-type emphasis)
1494                            gnus-hidden-properties))
1495             regexp elem beg invisible visible face)
1496         (goto-char (point-min))
1497         (search-forward "\n\n" nil t)
1498         (setq beg (point))
1499         (while (setq elem (pop alist))
1500           (goto-char beg)
1501           (setq regexp (car elem)
1502                 invisible (nth 1 elem)
1503                 visible (nth 2 elem)
1504                 face (nth 3 elem))
1505           (while (re-search-forward regexp nil t)
1506             (when (and (match-beginning visible) (match-beginning invisible))
1507               (gnus-article-hide-text
1508                (match-beginning invisible) (match-end invisible) props)
1509               (gnus-article-unhide-text-type
1510                (match-beginning visible) (match-end visible) 'emphasis)
1511               (gnus-put-text-property-excluding-newlines
1512                (match-beginning visible) (match-end visible) 'face face)
1513               (goto-char (match-end invisible)))))))))
1514
1515 (defvar gnus-summary-article-menu)
1516 (defvar gnus-summary-post-menu)
1517
1518 ;;; Saving functions.
1519
1520 (defun gnus-article-save (save-buffer file &optional num)
1521   "Save the currently selected article."
1522   (unless gnus-save-all-headers
1523     ;; Remove headers according to `gnus-saved-headers'.
1524     (let ((gnus-visible-headers
1525            (or gnus-saved-headers gnus-visible-headers))
1526           (gnus-article-buffer save-buffer))
1527       (save-excursion
1528         (set-buffer save-buffer)
1529         (article-hide-headers 1 t))))
1530   (save-window-excursion
1531     (if (not gnus-default-article-saver)
1532         (error "No default saver is defined")
1533       ;; !!! Magic!  The saving functions all save
1534       ;; `gnus-original-article-buffer' (or so they think), but we
1535       ;; bind that variable to our save-buffer.
1536       (set-buffer gnus-article-buffer)
1537       (let* ((gnus-save-article-buffer save-buffer)
1538              (filename
1539               (cond
1540                ((not gnus-prompt-before-saving) 'default)
1541                ((eq gnus-prompt-before-saving 'always) nil)
1542                (t file)))
1543              (gnus-number-of-articles-to-be-saved
1544               (when (eq gnus-prompt-before-saving t)
1545                 num)))                  ; Magic
1546         (set-buffer gnus-article-current-summary)
1547         (funcall gnus-default-article-saver filename)))))
1548
1549 (defun gnus-read-save-file-name (prompt &optional filename
1550                                         function group headers variable)
1551   (let ((default-name
1552           (funcall function group headers (symbol-value variable)))
1553         result)
1554     (setq
1555      result
1556      (cond
1557       ((eq filename 'default)
1558        default-name)
1559       ((eq filename t)
1560        default-name)
1561       (filename filename)
1562       (t
1563        (let* ((split-name (gnus-get-split-value gnus-split-methods))
1564               (prompt
1565                (format prompt
1566                        (if (and gnus-number-of-articles-to-be-saved
1567                                 (> gnus-number-of-articles-to-be-saved 1))
1568                            (format "these %d articles"
1569                                    gnus-number-of-articles-to-be-saved)
1570                          "this article")))
1571               (file
1572                ;; Let the split methods have their say.
1573                (cond
1574                 ;; No split name was found.
1575                 ((null split-name)
1576                  (read-file-name
1577                   (concat prompt " (default "
1578                           (file-name-nondirectory default-name) ") ")
1579                   (file-name-directory default-name)
1580                   default-name))
1581                 ;; A single group name is returned.
1582                 ((stringp split-name)
1583                  (setq default-name
1584                        (funcall function split-name headers
1585                                 (symbol-value variable)))
1586                  (read-file-name
1587                   (concat prompt " (default "
1588                           (file-name-nondirectory default-name) ") ")
1589                   (file-name-directory default-name)
1590                   default-name))
1591                 ;; A single split name was found
1592                 ((= 1 (length split-name))
1593                  (let* ((name (expand-file-name
1594                                (car split-name) gnus-article-save-directory))
1595                         (dir (cond ((file-directory-p name)
1596                                     (file-name-as-directory name))
1597                                    ((file-exists-p name) name)
1598                                    (t gnus-article-save-directory))))
1599                    (read-file-name
1600                     (concat prompt " (default " name ") ")
1601                     dir name)))
1602                 ;; A list of splits was found.
1603                 (t
1604                  (setq split-name (nreverse split-name))
1605                  (let (result)
1606                    (let ((file-name-history
1607                           (nconc split-name file-name-history)))
1608                      (setq result
1609                            (expand-file-name
1610                             (read-file-name
1611                              (concat prompt " (`M-p' for defaults) ")
1612                              gnus-article-save-directory
1613                              (car split-name))
1614                             gnus-article-save-directory)))
1615                    (car (push result file-name-history)))))))
1616          ;; Create the directory.
1617          (gnus-make-directory (file-name-directory file))
1618          ;; If we have read a directory, we append the default file name.
1619          (when (file-directory-p file)
1620            (setq file (concat (file-name-as-directory file)
1621                               (file-name-nondirectory default-name))))
1622          ;; Possibly translate some characters.
1623          (nnheader-translate-file-chars file)))))
1624     (gnus-make-directory (file-name-directory result))
1625     (set variable result)))
1626
1627 (defun gnus-article-archive-name (group)
1628   "Return the first instance of an \"Archive-name\" in the current buffer."
1629   (let ((case-fold-search t))
1630     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
1631       (nnheader-concat gnus-article-save-directory
1632                        (match-string 1)))))
1633
1634 (defun gnus-article-nndoc-name (group)
1635   "If GROUP is an nndoc group, return the name of the parent group."
1636   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
1637     (gnus-group-get-parameter group 'save-article-group)))
1638
1639 (defun gnus-summary-save-in-rmail (&optional filename)
1640   "Append this article to Rmail file.
1641 Optional argument FILENAME specifies file name.
1642 Directory to save to is default to `gnus-article-save-directory'."
1643   (setq filename (gnus-read-save-file-name
1644                   "Save %s in rmail file:" filename
1645                   gnus-rmail-save-name gnus-newsgroup-name
1646                   gnus-current-headers 'gnus-newsgroup-last-rmail))
1647   (gnus-eval-in-buffer-window gnus-save-article-buffer
1648     (save-excursion
1649       (save-restriction
1650         (widen)
1651         (gnus-output-to-rmail filename))))
1652   filename)
1653
1654 (defun gnus-summary-save-in-mail (&optional filename)
1655   "Append this article to Unix mail file.
1656 Optional argument FILENAME specifies file name.
1657 Directory to save to is default to `gnus-article-save-directory'."
1658   (setq filename (gnus-read-save-file-name
1659                   "Save %s in Unix mail file:" filename
1660                   gnus-mail-save-name gnus-newsgroup-name
1661                   gnus-current-headers 'gnus-newsgroup-last-mail))
1662   (gnus-eval-in-buffer-window gnus-save-article-buffer
1663     (save-excursion
1664       (save-restriction
1665         (widen)
1666         (if (and (file-readable-p filename)
1667                  (mail-file-babyl-p filename))
1668             (gnus-output-to-rmail filename t)
1669           (gnus-output-to-mail filename)))))
1670   filename)
1671
1672 (defun gnus-summary-save-in-file (&optional filename overwrite)
1673   "Append this article to file.
1674 Optional argument FILENAME specifies file name.
1675 Directory to save to is default to `gnus-article-save-directory'."
1676   (setq filename (gnus-read-save-file-name
1677                   "Save %s in file:" filename
1678                   gnus-file-save-name gnus-newsgroup-name
1679                   gnus-current-headers 'gnus-newsgroup-last-file))
1680   (gnus-eval-in-buffer-window gnus-save-article-buffer
1681     (save-excursion
1682       (save-restriction
1683         (widen)
1684         (when (and overwrite
1685                    (file-exists-p filename))
1686           (delete-file filename))
1687         (gnus-output-to-file filename))))
1688   filename)
1689
1690 (defun gnus-summary-write-to-file (&optional filename)
1691   "Write this article to a file.
1692 Optional argument FILENAME specifies file name.
1693 The directory to save in defaults to `gnus-article-save-directory'."
1694   (gnus-summary-save-in-file nil t))
1695
1696 (defun gnus-summary-save-body-in-file (&optional filename)
1697   "Append this article body to a file.
1698 Optional argument FILENAME specifies file name.
1699 The directory to save in defaults to `gnus-article-save-directory'."
1700   (setq filename (gnus-read-save-file-name
1701                   "Save %s body in file:" filename
1702                   gnus-file-save-name gnus-newsgroup-name
1703                   gnus-current-headers 'gnus-newsgroup-last-file))
1704   (gnus-eval-in-buffer-window gnus-save-article-buffer
1705     (save-excursion
1706       (save-restriction
1707         (widen)
1708         (goto-char (point-min))
1709         (when (search-forward "\n\n" nil t)
1710           (narrow-to-region (point) (point-max)))
1711         (gnus-output-to-file filename))))
1712   filename)
1713
1714 (defun gnus-summary-save-in-pipe (&optional command)
1715   "Pipe this article to subprocess."
1716   (setq command
1717         (cond ((eq command 'default)
1718                gnus-last-shell-command)
1719               (command command)
1720               (t (read-string
1721                   (format
1722                    "Shell command on %s: "
1723                    (if (and gnus-number-of-articles-to-be-saved
1724                             (> gnus-number-of-articles-to-be-saved 1))
1725                        (format "these %d articles"
1726                                gnus-number-of-articles-to-be-saved)
1727                      "this article"))
1728                   gnus-last-shell-command))))
1729   (when (string-equal command "")
1730     (setq command gnus-last-shell-command))
1731   (gnus-eval-in-buffer-window gnus-article-buffer
1732     (save-restriction
1733       (widen)
1734       (shell-command-on-region (point-min) (point-max) command nil)))
1735   (setq gnus-last-shell-command command))
1736
1737 ;;; Article file names when saving.
1738
1739 (defun gnus-capitalize-newsgroup (newsgroup)
1740   "Capitalize NEWSGROUP name."
1741   (when (not (zerop (length newsgroup)))
1742     (concat (char-to-string (upcase (aref newsgroup 0)))
1743             (substring newsgroup 1))))
1744
1745 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1746   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1747 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
1748 Otherwise, it is like ~/News/news/group/num."
1749   (let ((default
1750           (expand-file-name
1751            (concat (if (gnus-use-long-file-name 'not-save)
1752                        (gnus-capitalize-newsgroup newsgroup)
1753                      (gnus-newsgroup-directory-form newsgroup))
1754                    "/" (int-to-string (mail-header-number headers)))
1755            gnus-article-save-directory)))
1756     (if (and last-file
1757              (string-equal (file-name-directory default)
1758                            (file-name-directory last-file))
1759              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1760         default
1761       (or last-file default))))
1762
1763 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1764   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1765 If variable `gnus-use-long-file-name' is non-nil, it is
1766 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
1767   (let ((default
1768           (expand-file-name
1769            (concat (if (gnus-use-long-file-name 'not-save)
1770                        newsgroup
1771                      (gnus-newsgroup-directory-form newsgroup))
1772                    "/" (int-to-string (mail-header-number headers)))
1773            gnus-article-save-directory)))
1774     (if (and last-file
1775              (string-equal (file-name-directory default)
1776                            (file-name-directory last-file))
1777              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1778         default
1779       (or last-file default))))
1780
1781 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1782   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1783 If variable `gnus-use-long-file-name' is non-nil, it is
1784 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
1785   (or last-file
1786       (expand-file-name
1787        (if (gnus-use-long-file-name 'not-save)
1788            (gnus-capitalize-newsgroup newsgroup)
1789          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1790        gnus-article-save-directory)))
1791
1792 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1793   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1794 If variable `gnus-use-long-file-name' is non-nil, it is
1795 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
1796   (or last-file
1797       (expand-file-name
1798        (if (gnus-use-long-file-name 'not-save)
1799            newsgroup
1800          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1801        gnus-article-save-directory)))
1802
1803 (eval-and-compile
1804   (mapcar
1805    (lambda (func)
1806      (let (afunc gfunc)
1807        (if (consp func)
1808            (setq afunc (car func)
1809                  gfunc (cdr func))
1810          (setq afunc func
1811                gfunc (intern (format "gnus-%s" func))))
1812        (fset gfunc
1813              (if (not (fboundp afunc))
1814                  nil
1815                `(lambda (&optional interactive &rest args)
1816                   ,(documentation afunc t)
1817                   (interactive (list t))
1818                   (save-excursion
1819                     (set-buffer gnus-article-buffer)
1820                     (if interactive
1821                         (call-interactively ',afunc)
1822                       (apply ',afunc args))))))))
1823    '(article-hide-headers
1824      article-hide-boring-headers
1825      article-treat-overstrike
1826      (article-fill . gnus-article-word-wrap)
1827      article-remove-cr
1828      article-display-x-face
1829      article-de-quoted-unreadable
1830      article-mime-decode-quoted-printable
1831      article-hide-pgp
1832      article-hide-pem
1833      article-hide-signature
1834      article-remove-trailing-blank-lines
1835      article-strip-leading-blank-lines
1836      article-strip-multiple-blank-lines
1837      article-strip-leading-space
1838      article-strip-blank-lines
1839      article-strip-all-blank-lines
1840      article-date-local
1841      article-date-iso8601
1842      article-date-original
1843      article-date-ut
1844      article-date-user
1845      article-date-lapsed
1846      article-emphasize
1847      article-treat-dumbquotes
1848      (article-show-all . gnus-article-show-all-headers))))
1849 \f
1850 ;;;
1851 ;;; Gnus article mode
1852 ;;;
1853
1854 (put 'gnus-article-mode 'mode-class 'special)
1855
1856 (gnus-define-keys gnus-article-mode-map
1857   " " gnus-article-goto-next-page
1858   "\177" gnus-article-goto-prev-page
1859   [delete] gnus-article-goto-prev-page
1860   "\C-c^" gnus-article-refer-article
1861   "h" gnus-article-show-summary
1862   "s" gnus-article-show-summary
1863   "\C-c\C-m" gnus-article-mail
1864   "?" gnus-article-describe-briefly
1865   gnus-mouse-2 gnus-article-push-button
1866   "\r" gnus-article-press-button
1867   "\t" gnus-article-next-button
1868   "\M-\t" gnus-article-prev-button
1869   "e" gnus-article-edit
1870   "<" beginning-of-buffer
1871   ">" end-of-buffer
1872   "\C-c\C-i" gnus-info-find-node
1873   "\C-c\C-b" gnus-bug
1874
1875   "\C-d" gnus-article-read-summary-keys
1876   "\M-*" gnus-article-read-summary-keys
1877   "\M-#" gnus-article-read-summary-keys
1878   "\M-^" gnus-article-read-summary-keys
1879   "\M-g" gnus-article-read-summary-keys)
1880
1881 (substitute-key-definition
1882  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
1883
1884 (defun gnus-article-make-menu-bar ()
1885   (gnus-turn-off-edit-menu 'article)
1886   (unless (boundp 'gnus-article-article-menu)
1887     (easy-menu-define
1888      gnus-article-article-menu gnus-article-mode-map ""
1889      '("Article"
1890        ["Scroll forwards" gnus-article-goto-next-page t]
1891        ["Scroll backwards" gnus-article-goto-prev-page t]
1892        ["Show summary" gnus-article-show-summary t]
1893        ["Fetch Message-ID at point" gnus-article-refer-article t]
1894        ["Mail to address at point" gnus-article-mail t]
1895        ["Send a bug report" gnus-bug t]))
1896
1897     (easy-menu-define
1898      gnus-article-treatment-menu gnus-article-mode-map ""
1899      '("Treatment"
1900        ["Hide headers" gnus-article-hide-headers t]
1901        ["Hide signature" gnus-article-hide-signature t]
1902        ["Hide citation" gnus-article-hide-citation t]
1903        ["Treat overstrike" gnus-article-treat-overstrike t]
1904        ["Remove carriage return" gnus-article-remove-cr t]
1905        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]))
1906
1907     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
1908
1909     (when (boundp 'gnus-summary-post-menu)
1910       (define-key gnus-article-mode-map [menu-bar post]
1911         (cons "Post" gnus-summary-post-menu)))
1912
1913     (gnus-run-hooks 'gnus-article-menu-hook)))
1914
1915 (defun gnus-article-mode ()
1916   "Major mode for displaying an article.
1917
1918 All normal editing commands are switched off.
1919
1920 The following commands are available in addition to all summary mode
1921 commands:
1922 \\<gnus-article-mode-map>
1923 \\[gnus-article-next-page]\t Scroll the article one page forwards
1924 \\[gnus-article-prev-page]\t Scroll the article one page backwards
1925 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
1926 \\[gnus-article-show-summary]\t Display the summary buffer
1927 \\[gnus-article-mail]\t Send a reply to the address near point
1928 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
1929 \\[gnus-info-find-node]\t Go to the Gnus info node"
1930   (interactive)
1931   (when (gnus-visual-p 'article-menu 'menu)
1932     (gnus-article-make-menu-bar))
1933   (gnus-simplify-mode-line)
1934   (setq mode-name "Article")
1935   (setq major-mode 'gnus-article-mode)
1936   (make-local-variable 'minor-mode-alist)
1937   (unless (assq 'gnus-show-mime minor-mode-alist)
1938     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
1939   (use-local-map gnus-article-mode-map)
1940   (gnus-update-format-specifications nil 'article-mode)
1941   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
1942   (make-local-variable 'gnus-page-broken)
1943   (make-local-variable 'gnus-button-marker-list)
1944   (make-local-variable 'gnus-article-current-summary)
1945   (gnus-set-default-directory)
1946   (buffer-disable-undo (current-buffer))
1947   (setq buffer-read-only t)
1948   (set-syntax-table gnus-article-mode-syntax-table)
1949   (gnus-run-hooks 'gnus-article-mode-hook))
1950
1951 (defun gnus-article-setup-buffer ()
1952   "Initialize the article buffer."
1953   (let* ((name (if gnus-single-article-buffer "*Article*"
1954                  (concat "*Article " gnus-newsgroup-name "*")))
1955          (original
1956           (progn (string-match "\\*Article" name)
1957                  (concat " *Original Article"
1958                          (substring name (match-end 0))))))
1959     (setq gnus-article-buffer name)
1960     (setq gnus-original-article-buffer original)
1961     ;; This might be a variable local to the summary buffer.
1962     (unless gnus-single-article-buffer
1963       (save-excursion
1964         (set-buffer gnus-summary-buffer)
1965         (setq gnus-article-buffer name)
1966         (setq gnus-original-article-buffer original)
1967         (gnus-set-global-variables)))
1968     ;; Init original article buffer.
1969     (save-excursion
1970       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
1971       (buffer-disable-undo (current-buffer))
1972       (setq major-mode 'gnus-original-article-mode)
1973       (make-local-variable 'gnus-original-article))
1974     (if (get-buffer name)
1975         (save-excursion
1976           (set-buffer name)
1977           (buffer-disable-undo (current-buffer))
1978           (setq buffer-read-only t)
1979           (unless (eq major-mode 'gnus-article-mode)
1980             (gnus-article-mode))
1981           (current-buffer))
1982       (save-excursion
1983         (set-buffer (gnus-get-buffer-create name))
1984         (gnus-article-mode)
1985         (make-local-variable 'gnus-summary-buffer)
1986         (current-buffer)))))
1987
1988 ;; Set article window start at LINE, where LINE is the number of lines
1989 ;; from the head of the article.
1990 (defun gnus-article-set-window-start (&optional line)
1991   (set-window-start
1992    (get-buffer-window gnus-article-buffer t)
1993    (save-excursion
1994      (set-buffer gnus-article-buffer)
1995      (goto-char (point-min))
1996      (if (not line)
1997          (point-min)
1998        (gnus-message 6 "Moved to bookmark")
1999        (search-forward "\n\n" nil t)
2000        (forward-line line)
2001        (point)))))
2002
2003 (defun gnus-article-prepare (article &optional all-headers header)
2004   "Prepare ARTICLE in article mode buffer.
2005 ARTICLE should either be an article number or a Message-ID.
2006 If ARTICLE is an id, HEADER should be the article headers.
2007 If ALL-HEADERS is non-nil, no headers are hidden."
2008   (save-excursion
2009     ;; Make sure we start in a summary buffer.
2010     (unless (eq major-mode 'gnus-summary-mode)
2011       (set-buffer gnus-summary-buffer))
2012     (setq gnus-summary-buffer (current-buffer))
2013     (let* ((gnus-article (if header (mail-header-number header) article))
2014            (summary-buffer (current-buffer))
2015            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
2016            (group gnus-newsgroup-name)
2017            result)
2018       (save-excursion
2019         (gnus-article-setup-buffer)
2020         (set-buffer gnus-article-buffer)
2021         ;; Deactivate active regions.
2022         (when (and (boundp 'transient-mark-mode)
2023                    transient-mark-mode)
2024           (setq mark-active nil))
2025         (if (not (setq result (let ((buffer-read-only nil))
2026                                 (gnus-request-article-this-buffer
2027                                  article group))))
2028             ;; There is no such article.
2029             (save-excursion
2030               (when (and (numberp article)
2031                          (not (memq article gnus-newsgroup-sparse)))
2032                 (setq gnus-article-current
2033                       (cons gnus-newsgroup-name article))
2034                 (set-buffer gnus-summary-buffer)
2035                 (setq gnus-current-article article)
2036                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2037                     (progn
2038                       (gnus-summary-set-agent-mark article)
2039                       (message "Message marked for downloading"))
2040                   (gnus-summary-mark-article article gnus-canceled-mark)
2041                   (unless (memq article gnus-newsgroup-sparse)
2042                     (gnus-error 1
2043                      "No such article (may have expired or been canceled)")))))
2044           (if (or (eq result 'pseudo)
2045                   (eq result 'nneething))
2046               (progn
2047                 (save-excursion
2048                   (set-buffer summary-buffer)
2049                   (push article gnus-newsgroup-history)
2050                   (setq gnus-last-article gnus-current-article
2051                         gnus-current-article 0
2052                         gnus-current-headers nil
2053                         gnus-article-current nil)
2054                   (if (eq result 'nneething)
2055                       (gnus-configure-windows 'summary)
2056                     (gnus-configure-windows 'article))
2057                   (gnus-set-global-variables))
2058                 (gnus-set-mode-line 'article))
2059             ;; The result from the `request' was an actual article -
2060             ;; or at least some text that is now displayed in the
2061             ;; article buffer.
2062             (when (and (numberp article)
2063                        (not (eq article gnus-current-article)))
2064               ;; Seems like a new article has been selected.
2065               ;; `gnus-current-article' must be an article number.
2066               (save-excursion
2067                 (set-buffer summary-buffer)
2068                 (push article gnus-newsgroup-history)
2069                 (setq gnus-last-article gnus-current-article
2070                       gnus-current-article article
2071                       gnus-current-headers
2072                       (gnus-summary-article-header gnus-current-article)
2073                       gnus-article-current
2074                       (cons gnus-newsgroup-name gnus-current-article))
2075                 (unless (vectorp gnus-current-headers)
2076                   (setq gnus-current-headers nil))
2077                 (gnus-summary-goto-subject gnus-current-article)
2078                 (when (gnus-summary-show-thread)
2079                   ;; If the summary buffer really was folded, the
2080                   ;; previous goto may not actually have gone to
2081                   ;; the right article, but the thread root instead.
2082                   ;; So we go again.
2083                   (gnus-summary-goto-subject gnus-current-article))
2084                 (gnus-run-hooks 'gnus-mark-article-hook)
2085                 (gnus-set-mode-line 'summary)
2086                 (when (gnus-visual-p 'article-highlight 'highlight)
2087                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2088                 ;; Set the global newsgroup variables here.
2089                 ;; Suggested by Jim Sisolak
2090                 ;; <sisolak@trans4.neep.wisc.edu>.
2091                 (gnus-set-global-variables)
2092                 (setq gnus-have-all-headers
2093                       (or all-headers gnus-show-all-headers))))
2094             (when (or (numberp article)
2095                       (stringp article))
2096               ;; Hooks for getting information from the article.
2097               ;; This hook must be called before being narrowed.
2098               (let (buffer-read-only)
2099                 (gnus-run-hooks 'gnus-tmp-internal-hook)
2100                 (gnus-run-hooks 'gnus-article-prepare-hook)
2101                 ;; Decode MIME message.
2102                 (when gnus-show-mime
2103                   (if (or (not gnus-strict-mime)
2104                           (gnus-fetch-field "Mime-Version"))
2105                       (let ((coding-system-for-write 'binary)
2106                             (coding-system-for-read 'binary))
2107                         (funcall gnus-show-mime-method))
2108                     (funcall gnus-decode-encoded-word-method)))
2109                 ;; Perform the article display hooks.
2110                 (gnus-run-hooks 'gnus-article-display-hook))
2111               ;; Do page break.
2112               (goto-char (point-min))
2113               (setq gnus-page-broken
2114                     (when gnus-break-pages
2115                       (gnus-narrow-to-page)
2116                       t)))
2117             (gnus-set-mode-line 'article)
2118             (gnus-configure-windows 'article)
2119             (goto-char (point-min))
2120             (search-forward "\n\n" nil t)
2121             (set-window-point (get-buffer-window (current-buffer)) (point))
2122             t))))))
2123
2124 (defun gnus-article-wash-status ()
2125   "Return a string which display status of article washing."
2126   (save-excursion
2127     (set-buffer gnus-article-buffer)
2128     (let ((cite (gnus-article-hidden-text-p 'cite))
2129           (headers (gnus-article-hidden-text-p 'headers))
2130           (boring (gnus-article-hidden-text-p 'boring-headers))
2131           (pgp (gnus-article-hidden-text-p 'pgp))
2132           (pem (gnus-article-hidden-text-p 'pem))
2133           (signature (gnus-article-hidden-text-p 'signature))
2134           (overstrike (gnus-article-hidden-text-p 'overstrike))
2135           (emphasis (gnus-article-hidden-text-p 'emphasis))
2136           (mime gnus-show-mime))
2137       (format "%c%c%c%c%c%c%c"
2138               (if cite ?c ? )
2139               (if (or headers boring) ?h ? )
2140               (if (or pgp pem) ?p ? )
2141               (if signature ?s ? )
2142               (if overstrike ?o ? )
2143               (if mime ?m ? )
2144               (if emphasis ?e ? )))))
2145
2146 (fset 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
2147
2148 (defun gnus-article-maybe-hide-headers ()
2149   "Hide unwanted headers if `gnus-have-all-headers' is nil.
2150 Provided for backwards compatibility."
2151   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
2152       gnus-inhibit-hiding
2153       (gnus-article-hide-headers)))
2154
2155 ;;; Article savers.
2156
2157 (defun gnus-output-to-file (file-name)
2158   "Append the current article to a file named FILE-NAME."
2159   (let ((artbuf (current-buffer)))
2160     (with-temp-buffer
2161       (insert-buffer-substring artbuf)
2162       ;; Append newline at end of the buffer as separator, and then
2163       ;; save it to file.
2164       (goto-char (point-max))
2165       (insert "\n")
2166       (append-to-file (point-min) (point-max) file-name)
2167       t)))
2168
2169 (defun gnus-narrow-to-page (&optional arg)
2170   "Narrow the article buffer to a page.
2171 If given a numerical ARG, move forward ARG pages."
2172   (interactive "P")
2173   (setq arg (if arg (prefix-numeric-value arg) 0))
2174   (save-excursion
2175     (set-buffer gnus-article-buffer)
2176     (goto-char (point-min))
2177     (widen)
2178     ;; Remove any old next/prev buttons.
2179     (when (gnus-visual-p 'page-marker)
2180       (let ((buffer-read-only nil))
2181         (gnus-remove-text-with-property 'gnus-prev)
2182         (gnus-remove-text-with-property 'gnus-next)))
2183     (when
2184         (cond ((< arg 0)
2185                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
2186               ((> arg 0)
2187                (re-search-forward page-delimiter nil 'move arg)))
2188       (goto-char (match-end 0)))
2189     (narrow-to-region
2190      (point)
2191      (if (re-search-forward page-delimiter nil 'move)
2192          (match-beginning 0)
2193        (point)))
2194     (when (and (gnus-visual-p 'page-marker)
2195                (not (= (point-min) 1)))
2196       (save-excursion
2197         (goto-char (point-min))
2198         (gnus-insert-prev-page-button)))
2199     (when (and (gnus-visual-p 'page-marker)
2200                (< (+ (point-max) 2) (buffer-size)))
2201       (save-excursion
2202         (goto-char (point-max))
2203         (gnus-insert-next-page-button)))))
2204
2205 ;; Article mode commands
2206
2207 (defun gnus-article-goto-next-page ()
2208   "Show the next page of the article."
2209   (interactive)
2210   (when (gnus-article-next-page)
2211     (goto-char (point-min))
2212     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
2213
2214 (defun gnus-article-goto-prev-page ()
2215   "Show the next page of the article."
2216   (interactive)
2217   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
2218     (gnus-article-prev-page nil)))
2219
2220 (defun gnus-article-next-page (&optional lines)
2221   "Show the next page of the current article.
2222 If end of article, return non-nil.  Otherwise return nil.
2223 Argument LINES specifies lines to be scrolled up."
2224   (interactive "p")
2225   (move-to-window-line -1)
2226   (if (save-excursion
2227         (end-of-line)
2228         (and (pos-visible-in-window-p)  ;Not continuation line.
2229              (eobp)))
2230       ;; Nothing in this page.
2231       (if (or (not gnus-page-broken)
2232               (save-excursion
2233                 (save-restriction
2234                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
2235           t                             ;Nothing more.
2236         (gnus-narrow-to-page 1)         ;Go to next page.
2237         nil)
2238     ;; More in this page.
2239     (let ((scroll-in-place nil))
2240       (condition-case ()
2241           (scroll-up lines)
2242         (end-of-buffer
2243          ;; Long lines may cause an end-of-buffer error.
2244          (goto-char (point-max)))))
2245     (move-to-window-line 0)
2246     nil))
2247
2248 (defun gnus-article-prev-page (&optional lines)
2249   "Show previous page of current article.
2250 Argument LINES specifies lines to be scrolled down."
2251   (interactive "p")
2252   (move-to-window-line 0)
2253   (if (and gnus-page-broken
2254            (bobp)
2255            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
2256       (progn
2257         (gnus-narrow-to-page -1)        ;Go to previous page.
2258         (goto-char (point-max))
2259         (recenter -1))
2260     (let ((scroll-in-place nil))
2261       (prog1
2262           (condition-case ()
2263               (scroll-down lines)
2264             (beginning-of-buffer
2265              (goto-char (point-min))))
2266         (move-to-window-line 0)))))
2267
2268 (defun gnus-article-refer-article ()
2269   "Read article specified by message-id around point."
2270   (interactive)
2271   (let ((point (point)))
2272     (search-forward ">" nil t)          ;Move point to end of "<....>".
2273     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
2274         (let ((message-id (match-string 1)))
2275           (goto-char point)
2276           (set-buffer gnus-summary-buffer)
2277           (gnus-summary-refer-article message-id))
2278       (goto-char (point))
2279       (error "No references around point"))))
2280
2281 (defun gnus-article-show-summary ()
2282   "Reconfigure windows to show summary buffer."
2283   (interactive)
2284   (if (not (gnus-buffer-live-p gnus-summary-buffer))
2285       (error "There is no summary buffer for this article buffer")
2286     (gnus-article-set-globals)
2287     (gnus-configure-windows 'article)
2288     (gnus-summary-goto-subject gnus-current-article)
2289     (gnus-summary-position-point)))
2290
2291 (defun gnus-article-describe-briefly ()
2292   "Describe article mode commands briefly."
2293   (interactive)
2294   (gnus-message 6
2295                 (substitute-command-keys "\\<gnus-article-mode-map>\\[gnus-article-goto-next-page]:Next page     \\[gnus-article-goto-prev-page]:Prev page  \\[gnus-article-show-summary]:Show summary  \\[gnus-info-find-node]:Run Info  \\[gnus-article-describe-briefly]:This help")))
2296
2297 (defun gnus-article-summary-command ()
2298   "Execute the last keystroke in the summary buffer."
2299   (interactive)
2300   (let ((obuf (current-buffer))
2301         (owin (current-window-configuration))
2302         func)
2303     (switch-to-buffer gnus-article-current-summary 'norecord)
2304     (setq func (lookup-key (current-local-map) (this-command-keys)))
2305     (call-interactively func)
2306     (set-buffer obuf)
2307     (set-window-configuration owin)
2308     (set-window-point (get-buffer-window (current-buffer)) (point))))
2309
2310 (defun gnus-article-summary-command-nosave ()
2311   "Execute the last keystroke in the summary buffer."
2312   (interactive)
2313   (let (func)
2314     (pop-to-buffer gnus-article-current-summary 'norecord)
2315     (setq func (lookup-key (current-local-map) (this-command-keys)))
2316     (call-interactively func)))
2317
2318 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
2319   "Read a summary buffer key sequence and execute it from the article buffer."
2320   (interactive "P")
2321   (let ((nosaves
2322          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
2323            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
2324            "=" "^" "\M-^" "|"))
2325         (nosave-but-article
2326          '("A\r"))
2327         (nosave-in-article
2328          '("\C-d"))
2329         (up-to-top
2330          '("n" "Gn" "p" "Gp"))
2331         keys new-sum-point)
2332     (save-excursion
2333       (set-buffer gnus-article-current-summary)
2334       (let (gnus-pick-mode)
2335         (push (or key last-command-event) unread-command-events)
2336         (setq keys (read-key-sequence nil))))
2337     (message "")
2338
2339     (if (or (member keys nosaves)
2340             (member keys nosave-but-article)
2341             (member keys nosave-in-article))
2342         (let (func)
2343           (save-window-excursion
2344             (pop-to-buffer gnus-article-current-summary 'norecord)
2345             ;; We disable the pick minor mode commands.
2346             (let (gnus-pick-mode)
2347               (setq func (lookup-key (current-local-map) keys))))
2348           (if (not func)
2349               (ding)
2350             (unless (member keys nosave-in-article)
2351               (set-buffer gnus-article-current-summary))
2352             (call-interactively func)
2353             (setq new-sum-point (point)))
2354           (when (member keys nosave-but-article)
2355             (pop-to-buffer gnus-article-buffer 'norecord)))
2356       ;; These commands should restore window configuration.
2357       (let ((obuf (current-buffer))
2358             (owin (current-window-configuration))
2359             (opoint (point))
2360             (summary gnus-article-current-summary)
2361             func in-buffer selected)
2362         (if not-restore-window
2363             (pop-to-buffer summary 'norecord)
2364           (switch-to-buffer summary 'norecord))
2365         (setq in-buffer (current-buffer))
2366         ;; We disable the pick minor mode commands.
2367         (if (setq func (let (gnus-pick-mode)
2368                          (lookup-key (current-local-map) keys)))
2369             (progn
2370               (call-interactively func)
2371               (setq new-sum-point (point)))
2372           (ding))
2373         (when (eq in-buffer (current-buffer))
2374           (setq selected (gnus-summary-select-article))
2375           (set-buffer obuf)
2376           (unless not-restore-window
2377             (set-window-configuration owin))
2378           (unless (or (not (eq selected 'old)) (member keys up-to-top))
2379             (set-window-point (get-buffer-window (current-buffer))
2380                               opoint))
2381           (let ((win (get-buffer-window gnus-article-current-summary)))
2382             (when win
2383               (set-window-point win new-sum-point))))))))
2384
2385 (defun gnus-article-hide (&optional arg force)
2386   "Hide all the gruft in the current article.
2387 This means that PGP stuff, signatures, cited text and (some)
2388 headers will be hidden.
2389 If given a prefix, show the hidden text instead."
2390   (interactive (append (gnus-article-hidden-arg) (list 'force)))
2391   (gnus-article-hide-headers arg)
2392   (gnus-article-hide-pgp arg)
2393   (gnus-article-hide-citation-maybe arg force)
2394   (gnus-article-hide-signature arg))
2395
2396 (defun gnus-article-maybe-highlight ()
2397   "Do some article highlighting if article highlighting is requested."
2398   (when (gnus-visual-p 'article-highlight 'highlight)
2399     (gnus-article-highlight-some)))
2400
2401 (defun gnus-check-group-server ()
2402   ;; Make sure the connection to the server is alive.
2403   (unless (gnus-server-opened
2404            (gnus-find-method-for-group gnus-newsgroup-name))
2405     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
2406     (gnus-request-group gnus-newsgroup-name t)))
2407
2408 (defun gnus-request-article-this-buffer (article group)
2409   "Get an article and insert it into this buffer."
2410   (let (do-update-line sparse-header)
2411     (prog1
2412         (save-excursion
2413           (erase-buffer)
2414           (gnus-kill-all-overlays)
2415           (setq group (or group gnus-newsgroup-name))
2416
2417           ;; Using `gnus-request-article' directly will insert the article into
2418           ;; `nntp-server-buffer' - so we'll save some time by not having to
2419           ;; copy it from the server buffer into the article buffer.
2420
2421           ;; We only request an article by message-id when we do not have the
2422           ;; headers for it, so we'll have to get those.
2423           (when (stringp article)
2424             (let ((gnus-override-method gnus-refer-article-method))
2425               (gnus-read-header article)))
2426
2427           ;; If the article number is negative, that means that this article
2428           ;; doesn't belong in this newsgroup (possibly), so we find its
2429           ;; message-id and request it by id instead of number.
2430           (when (and (numberp article)
2431                      gnus-summary-buffer
2432                      (get-buffer gnus-summary-buffer)
2433                      (gnus-buffer-exists-p gnus-summary-buffer))
2434             (save-excursion
2435               (set-buffer gnus-summary-buffer)
2436               (let ((header (gnus-summary-article-header article)))
2437                 (when (< article 0)
2438                   (cond
2439                    ((memq article gnus-newsgroup-sparse)
2440                     ;; This is a sparse gap article.
2441                     (setq do-update-line article)
2442                     (setq article (mail-header-id header))
2443                     (let ((gnus-override-method gnus-refer-article-method))
2444                       (setq sparse-header (gnus-read-header article)))
2445                     (setq gnus-newsgroup-sparse
2446                           (delq article gnus-newsgroup-sparse)))
2447                    ((vectorp header)
2448                     ;; It's a real article.
2449                     (setq article (mail-header-id header)))
2450                    (t
2451                     ;; It is an extracted pseudo-article.
2452                     (setq article 'pseudo)
2453                     (gnus-request-pseudo-article header))))
2454
2455                 (let ((method (gnus-find-method-for-group
2456                                gnus-newsgroup-name)))
2457                   (when (and (eq (car method) 'nneething)
2458                              (vectorp header))
2459                     (let ((dir (concat
2460                                 (file-name-as-directory
2461                                  (or (cadr (assq 'nneething-address method))
2462                                      (nth 1 method)))
2463                                 (mail-header-subject header))))
2464                       (when (file-directory-p dir)
2465                         (setq article 'nneething)
2466                         (gnus-group-enter-directory dir))))))))
2467
2468           (cond
2469            ;; Refuse to select canceled articles.
2470            ((and (numberp article)
2471                  gnus-summary-buffer
2472                  (get-buffer gnus-summary-buffer)
2473                  (gnus-buffer-exists-p gnus-summary-buffer)
2474                  (eq (cdr (save-excursion
2475                             (set-buffer gnus-summary-buffer)
2476                             (assq article gnus-newsgroup-reads)))
2477                      gnus-canceled-mark))
2478             nil)
2479            ;; We first check `gnus-original-article-buffer'.
2480            ((and (get-buffer gnus-original-article-buffer)
2481                  (numberp article)
2482                  (save-excursion
2483                    (set-buffer gnus-original-article-buffer)
2484                    (and (equal (car gnus-original-article) group)
2485                         (eq (cdr gnus-original-article) article))))
2486             (insert-buffer-substring gnus-original-article-buffer)
2487             'article)
2488            ;; Check the backlog.
2489            ((and gnus-keep-backlog
2490                  (gnus-backlog-request-article group article (current-buffer)))
2491             'article)
2492            ;; Check asynchronous pre-fetch.
2493            ((gnus-async-request-fetched-article group article (current-buffer))
2494             (gnus-async-prefetch-next group article gnus-summary-buffer)
2495             (when (and (numberp article) gnus-keep-backlog)
2496               (gnus-backlog-enter-article group article (current-buffer)))
2497             'article)
2498            ;; Check the cache.
2499            ((and gnus-use-cache
2500                  (numberp article)
2501                  (gnus-cache-request-article article group))
2502             'article)
2503            ;; Get the article and put into the article buffer.
2504            ((or (stringp article) (numberp article))
2505             (let ((gnus-override-method
2506                    (and (stringp article) gnus-refer-article-method))
2507                   (buffer-read-only nil))
2508               (erase-buffer)
2509               (gnus-kill-all-overlays)
2510               (gnus-check-group-server)
2511               (when (gnus-request-article article group (current-buffer))
2512                 (when (numberp article)
2513                   (gnus-async-prefetch-next group article gnus-summary-buffer)
2514                   (when gnus-keep-backlog
2515                     (gnus-backlog-enter-article
2516                      group article (current-buffer))))
2517                 'article)))
2518            ;; It was a pseudo.
2519            (t article)))
2520
2521       ;; Associate this article with the current summary buffer.
2522       (setq gnus-article-current-summary gnus-summary-buffer)
2523
2524       ;; Take the article from the original article buffer
2525       ;; and place it in the buffer it's supposed to be in.
2526       (when (and (get-buffer gnus-article-buffer)
2527                  (equal (buffer-name (current-buffer))
2528                         (buffer-name (get-buffer gnus-article-buffer))))
2529         (save-excursion
2530           (if (get-buffer gnus-original-article-buffer)
2531               (set-buffer gnus-original-article-buffer)
2532             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2533             (buffer-disable-undo (current-buffer))
2534             (setq major-mode 'gnus-original-article-mode)
2535             (setq buffer-read-only t))
2536           (let (buffer-read-only)
2537             (erase-buffer)
2538             (insert-buffer-substring gnus-article-buffer))
2539           (setq gnus-original-article (cons group article))))
2540
2541       ;; Update sparse articles.
2542       (when (and do-update-line
2543                  (or (numberp article)
2544                      (stringp article)))
2545         (let ((buf (current-buffer)))
2546           (set-buffer gnus-summary-buffer)
2547           (gnus-summary-update-article do-update-line sparse-header)
2548           (gnus-summary-goto-subject do-update-line nil t)
2549           (set-window-point (get-buffer-window (current-buffer) t)
2550                             (point))
2551           (set-buffer buf))))))
2552
2553 ;;;
2554 ;;; Article editing
2555 ;;;
2556
2557 (defcustom gnus-article-edit-mode-hook nil
2558   "Hook run in article edit mode buffers."
2559   :group 'gnus-article-various
2560   :type 'hook)
2561
2562 (defvar gnus-article-edit-done-function nil)
2563
2564 (defvar gnus-article-edit-mode-map nil)
2565
2566 (unless gnus-article-edit-mode-map
2567   (setq gnus-article-edit-mode-map (copy-keymap text-mode-map))
2568
2569   (gnus-define-keys gnus-article-edit-mode-map
2570     "\C-c\C-c" gnus-article-edit-done
2571     "\C-c\C-k" gnus-article-edit-exit)
2572
2573   (gnus-define-keys (gnus-article-edit-wash-map
2574                      "\C-c\C-w" gnus-article-edit-mode-map)
2575     "f" gnus-article-edit-full-stops))
2576
2577 (defun gnus-article-edit-mode ()
2578   "Major mode for editing articles.
2579 This is an extended text-mode.
2580
2581 \\{gnus-article-edit-mode-map}"
2582   (interactive)
2583   (setq major-mode 'gnus-article-edit-mode)
2584   (setq mode-name "Article Edit")
2585   (use-local-map gnus-article-edit-mode-map)
2586   (make-local-variable 'gnus-article-edit-done-function)
2587   (make-local-variable 'gnus-prev-winconf)
2588   (setq buffer-read-only nil)
2589   (buffer-enable-undo)
2590   (widen)
2591   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
2592
2593 (defun gnus-article-edit (&optional force)
2594   "Edit the current article.
2595 This will have permanent effect only in mail groups.
2596 If FORCE is non-nil, allow editing of articles even in read-only
2597 groups."
2598   (interactive "P")
2599   (when (and (not force)
2600              (gnus-group-read-only-p))
2601     (error "The current newsgroup does not support article editing"))
2602   (gnus-article-date-original)
2603   (gnus-article-edit-article
2604    `(lambda (no-highlight)
2605       (gnus-summary-edit-article-done
2606        ,(or (mail-header-references gnus-current-headers) "")
2607        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
2608
2609 (defun gnus-article-edit-article (exit-func)
2610   "Start editing the contents of the current article buffer."
2611   (let ((winconf (current-window-configuration)))
2612     (set-buffer gnus-article-buffer)
2613     (gnus-article-edit-mode)
2614     (gnus-article-delete-text-of-type 'annotation)
2615     (gnus-set-text-properties (point-min) (point-max) nil)
2616     (gnus-configure-windows 'edit-article)
2617     (setq gnus-article-edit-done-function exit-func)
2618     (setq gnus-prev-winconf winconf)
2619     (gnus-message 6 "C-c C-c to end edits")))
2620
2621 (defun gnus-article-edit-done (&optional arg)
2622   "Update the article edits and exit."
2623   (interactive "P")
2624   (save-excursion
2625     (save-restriction
2626       (widen)
2627       (goto-char (point-min))
2628       (when (search-forward "\n\n" nil 1)
2629         (let ((lines (count-lines (point) (point-max)))
2630               (length (- (point-max) (point)))
2631               (case-fold-search t)
2632               (body (copy-marker (point))))
2633           (goto-char (point-min))
2634           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
2635             (delete-region (match-beginning 1) (match-end 1))
2636             (insert (number-to-string length)))
2637           (goto-char (point-min))
2638           (when (re-search-forward
2639                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
2640             (delete-region (match-beginning 1) (match-end 1))
2641             (insert (number-to-string length)))
2642           (goto-char (point-min))
2643           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
2644             (delete-region (match-beginning 1) (match-end 1))
2645             (insert (number-to-string lines)))))))
2646   (let ((func gnus-article-edit-done-function)
2647         (buf (current-buffer))
2648         (start (window-start)))
2649     (gnus-article-edit-exit)
2650     (save-excursion
2651       (set-buffer buf)
2652       (let ((buffer-read-only nil))
2653         (funcall func arg)))
2654     (set-buffer buf)
2655     (set-window-start (get-buffer-window buf) start)
2656     (set-window-point (get-buffer-window buf) (point))))
2657
2658 (defun gnus-article-edit-exit ()
2659   "Exit the article editing without updating."
2660   (interactive)
2661   ;; We remove all text props from the article buffer.
2662   (let ((buf (format "%s" (buffer-string)))
2663         (curbuf (current-buffer))
2664         (p (point))
2665         (window-start (window-start)))
2666     (erase-buffer)
2667     (insert buf)
2668     (let ((winconf gnus-prev-winconf))
2669       (gnus-article-mode)
2670       ;; The cache and backlog have to be flushed somewhat.
2671       (when gnus-use-cache
2672         (gnus-cache-update-article
2673          (car gnus-article-current) (cdr gnus-article-current)))
2674       (when gnus-keep-backlog
2675         (gnus-backlog-remove-article
2676          (car gnus-article-current) (cdr gnus-article-current)))
2677       ;; Flush original article as well.
2678       (save-excursion
2679         (when (get-buffer gnus-original-article-buffer)
2680           (set-buffer gnus-original-article-buffer)
2681           (setq gnus-original-article nil)))
2682       (set-window-configuration winconf)
2683       ;; Tippy-toe some to make sure that point remains where it was.
2684       (let ((buf (current-buffer)))
2685         (set-buffer curbuf)
2686         (set-window-start (get-buffer-window (current-buffer)) window-start)
2687         (goto-char p)
2688         (set-buffer buf)))))
2689
2690 (defun gnus-article-edit-full-stops ()
2691   "Interactively repair spacing at end of sentences."
2692   (interactive)
2693   (save-excursion
2694     (goto-char (point-min))
2695     (search-forward-regexp "^$" nil t)
2696     (let ((case-fold-search nil))
2697       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
2698
2699 ;;;
2700 ;;; Article highlights
2701 ;;;
2702
2703 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
2704
2705 ;;; Internal Variables:
2706
2707 (defcustom gnus-button-url-regexp "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?\\([-a-zA-Z0-9_=!?#$@~`%&*+|\\/:;.,]\\|\\w\\)+\\([-a-zA-Z0-9_=#$@~`%&*+|\\/]\\|\\w\\)"
2708   "Regular expression that matches URLs."
2709   :group 'gnus-article-buttons
2710   :type 'regexp)
2711
2712 (defcustom gnus-button-alist
2713   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>" 0 t
2714      gnus-button-message-id 2)
2715     ("\\bnews:\\([^>\n\t ]*@[^>\n\t ]*\\)" 0 t gnus-button-message-id 1)
2716     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
2717      1 t
2718      gnus-button-fetch-group 4)
2719     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
2720     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
2721      t gnus-button-message-id 3)
2722     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
2723     ("mailto:\\([a-zA-Z.-@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
2724     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
2725     ;; This is how URLs _should_ be embedded in text...
2726     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
2727     ;; Raw URLs.
2728     (,gnus-button-url-regexp 0 t gnus-button-url 0))
2729   "*Alist of regexps matching buttons in article bodies.
2730
2731 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
2732 REGEXP: is the string matching text around the button,
2733 BUTTON: is the number of the regexp grouping actually matching the button,
2734 FORM: is a lisp expression which must eval to true for the button to
2735 be added,
2736 CALLBACK: is the function to call when the user push this button, and each
2737 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
2738
2739 CALLBACK can also be a variable, in that case the value of that
2740 variable it the real callback function."
2741   :group 'gnus-article-buttons
2742   :type '(repeat (list regexp
2743                        (integer :tag "Button")
2744                        (sexp :tag "Form")
2745                        (function :tag "Callback")
2746                        (repeat :tag "Par"
2747                                :inline t
2748                                (integer :tag "Regexp group")))))
2749
2750 (defcustom gnus-header-button-alist
2751   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
2752      0 t gnus-button-message-id 0)
2753     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
2754     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
2755      0 t gnus-button-mailto 0)
2756     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2757     ("^Subject:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2758     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2759     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
2760      gnus-button-message-id 3))
2761   "*Alist of headers and regexps to match buttons in article heads.
2762
2763 This alist is very similar to `gnus-button-alist', except that each
2764 alist has an additional HEADER element first in each entry:
2765
2766 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
2767
2768 HEADER is a regexp to match a header.  For a fuller explanation, see
2769 `gnus-button-alist'."
2770   :group 'gnus-article-buttons
2771   :group 'gnus-article-headers
2772   :type '(repeat (list (regexp :tag "Header")
2773                        regexp
2774                        (integer :tag "Button")
2775                        (sexp :tag "Form")
2776                        (function :tag "Callback")
2777                        (repeat :tag "Par"
2778                                :inline t
2779                                (integer :tag "Regexp group")))))
2780
2781 (defvar gnus-button-regexp nil)
2782 (defvar gnus-button-marker-list nil)
2783 ;; Regexp matching any of the regexps from `gnus-button-alist'.
2784
2785 (defvar gnus-button-last nil)
2786 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
2787
2788 ;;; Commands:
2789
2790 (defun gnus-article-push-button (event)
2791   "Check text under the mouse pointer for a callback function.
2792 If the text under the mouse pointer has a `gnus-callback' property,
2793 call it with the value of the `gnus-data' text property."
2794   (interactive "e")
2795   (set-buffer (window-buffer (posn-window (event-start event))))
2796   (let* ((pos (posn-point (event-start event)))
2797          (data (get-text-property pos 'gnus-data))
2798          (fun (get-text-property pos 'gnus-callback)))
2799     (goto-char pos)
2800     (when fun
2801       (funcall fun data))))
2802
2803 (defun gnus-article-press-button ()
2804   "Check text at point for a callback function.
2805 If the text at point has a `gnus-callback' property,
2806 call it with the value of the `gnus-data' text property."
2807   (interactive)
2808   (let* ((data (get-text-property (point) 'gnus-data))
2809          (fun (get-text-property (point) 'gnus-callback)))
2810     (when fun
2811       (funcall fun data))))
2812
2813 (defun gnus-article-prev-button (n)
2814   "Move point to N buttons backward.
2815 If N is negative, move forward instead."
2816   (interactive "p")
2817   (gnus-article-next-button (- n)))
2818
2819 (defun gnus-article-next-button (n)
2820   "Move point to N buttons forward.
2821 If N is negative, move backward instead."
2822   (interactive "p")
2823   (let ((function (if (< n 0) 'previous-single-property-change
2824                     'next-single-property-change))
2825         (inhibit-point-motion-hooks t)
2826         (backward (< n 0))
2827         (limit (if (< n 0) (point-min) (point-max))))
2828     (setq n (abs n))
2829     (while (and (not (= limit (point)))
2830                 (> n 0))
2831       ;; Skip past the current button.
2832       (when (get-text-property (point) 'gnus-callback)
2833         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2834       ;; Go to the next (or previous) button.
2835       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
2836       ;; Put point at the start of the button.
2837       (when (and backward (not (get-text-property (point) 'gnus-callback)))
2838         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2839       ;; Skip past intangible buttons.
2840       (when (get-text-property (point) 'intangible)
2841         (incf n))
2842       (decf n))
2843     (unless (zerop n)
2844       (gnus-message 5 "No more buttons"))
2845     n))
2846
2847 (defun gnus-article-highlight (&optional force)
2848   "Highlight current article.
2849 This function calls `gnus-article-highlight-headers',
2850 `gnus-article-highlight-citation',
2851 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2852 do the highlighting.  See the documentation for those functions."
2853   (interactive (list 'force))
2854   (gnus-article-highlight-headers)
2855   (gnus-article-highlight-citation force)
2856   (gnus-article-highlight-signature)
2857   (gnus-article-add-buttons force)
2858   (gnus-article-add-buttons-to-head))
2859
2860 (defun gnus-article-highlight-some (&optional force)
2861   "Highlight current article.
2862 This function calls `gnus-article-highlight-headers',
2863 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2864 do the highlighting.  See the documentation for those functions."
2865   (interactive (list 'force))
2866   (gnus-article-highlight-headers)
2867   (gnus-article-highlight-signature)
2868   (gnus-article-add-buttons))
2869
2870 (defun gnus-article-highlight-headers ()
2871   "Highlight article headers as specified by `gnus-header-face-alist'."
2872   (interactive)
2873   (save-excursion
2874     (set-buffer gnus-article-buffer)
2875     (save-restriction
2876       (let ((alist gnus-header-face-alist)
2877             (buffer-read-only nil)
2878             (case-fold-search t)
2879             (inhibit-point-motion-hooks t)
2880             entry regexp header-face field-face from hpoints fpoints)
2881         (message-narrow-to-head)
2882         (while (setq entry (pop alist))
2883           (goto-char (point-min))
2884           (setq regexp (concat "^\\("
2885                                (if (string-equal "" (nth 0 entry))
2886                                    "[^\t ]"
2887                                  (nth 0 entry))
2888                                "\\)")
2889                 header-face (nth 1 entry)
2890                 field-face (nth 2 entry))
2891           (while (and (re-search-forward regexp nil t)
2892                       (not (eobp)))
2893             (beginning-of-line)
2894             (setq from (point))
2895             (unless (search-forward ":" nil t)
2896               (forward-char 1))
2897             (when (and header-face
2898                        (not (memq (point) hpoints)))
2899               (push (point) hpoints)
2900               (gnus-put-text-property from (point) 'face header-face))
2901             (when (and field-face
2902                        (not (memq (setq from (point)) fpoints)))
2903               (push from fpoints)
2904               (if (re-search-forward "^[^ \t]" nil t)
2905                   (forward-char -2)
2906                 (goto-char (point-max)))
2907               (gnus-put-text-property from (point) 'face field-face))))))))
2908
2909 (defun gnus-article-highlight-signature ()
2910   "Highlight the signature in an article.
2911 It does this by highlighting everything after
2912 `gnus-signature-separator' using `gnus-signature-face'."
2913   (interactive)
2914   (save-excursion
2915     (set-buffer gnus-article-buffer)
2916     (let ((buffer-read-only nil)
2917           (inhibit-point-motion-hooks t))
2918       (save-restriction
2919         (when (and gnus-signature-face
2920                    (gnus-article-narrow-to-signature))
2921           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
2922                             'face gnus-signature-face)
2923           (widen)
2924           (gnus-article-search-signature)
2925           (let ((start (match-beginning 0))
2926                 (end (set-marker (make-marker) (1+ (match-end 0)))))
2927             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
2928                                      end)))))))
2929
2930 (defun gnus-button-in-region-p (b e prop)
2931   "Say whether PROP exists in the region."
2932   (text-property-not-all b e prop nil))
2933
2934 (defun gnus-article-add-buttons (&optional force)
2935   "Find external references in the article and make buttons of them.
2936 \"External references\" are things like Message-IDs and URLs, as
2937 specified by `gnus-button-alist'."
2938   (interactive (list 'force))
2939   (save-excursion
2940     (set-buffer gnus-article-buffer)
2941     (let ((buffer-read-only nil)
2942           (inhibit-point-motion-hooks t)
2943           (case-fold-search t)
2944           (alist gnus-button-alist)
2945           beg entry regexp)
2946       ;; Remove all old markers.
2947       (let (marker entry)
2948         (while (setq marker (pop gnus-button-marker-list))
2949           (goto-char marker)
2950           (when (setq entry (gnus-button-entry))
2951             (put-text-property (match-beginning (nth 1 entry))
2952                                (match-end (nth 1 entry))
2953                                'gnus-callback nil))
2954           (set-marker marker nil)))
2955       ;; We skip the headers.
2956       (goto-char (point-min))
2957       (unless (search-forward "\n\n" nil t)
2958         (goto-char (point-max)))
2959       (setq beg (point))
2960       (while (setq entry (pop alist))
2961         (setq regexp (car entry))
2962         (goto-char beg)
2963         (while (re-search-forward regexp nil t)
2964           (let* ((start (and entry (match-beginning (nth 1 entry))))
2965                  (end (and entry (match-end (nth 1 entry))))
2966                  (from (match-beginning 0)))
2967             (when (and (or (eq t (nth 2 entry))
2968                            (eval (nth 2 entry)))
2969                        (not (gnus-button-in-region-p
2970                              start end 'gnus-callback)))
2971               ;; That optional form returned non-nil, so we add the
2972               ;; button.
2973               (gnus-article-add-button
2974                start end 'gnus-button-push
2975                (car (push (set-marker (make-marker) from)
2976                           gnus-button-marker-list))))))))))
2977
2978 ;; Add buttons to the head of an article.
2979 (defun gnus-article-add-buttons-to-head ()
2980   "Add buttons to the head of the article."
2981   (interactive)
2982   (save-excursion
2983     (set-buffer gnus-article-buffer)
2984     (let ((buffer-read-only nil)
2985           (inhibit-point-motion-hooks t)
2986           (case-fold-search t)
2987           (alist gnus-header-button-alist)
2988           entry beg end)
2989       (nnheader-narrow-to-headers)
2990       (while alist
2991         ;; Each alist entry.
2992         (setq entry (car alist)
2993               alist (cdr alist))
2994         (goto-char (point-min))
2995         (while (re-search-forward (car entry) nil t)
2996           ;; Each header matching the entry.
2997           (setq beg (match-beginning 0))
2998           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
2999                              (match-beginning 0))
3000                         (point-max)))
3001           (goto-char beg)
3002           (while (re-search-forward (nth 1 entry) end t)
3003             ;; Each match within a header.
3004             (let* ((entry (cdr entry))
3005                    (start (match-beginning (nth 1 entry)))
3006                    (end (match-end (nth 1 entry)))
3007                    (form (nth 2 entry)))
3008               (goto-char (match-end 0))
3009               (when (eval form)
3010                 (gnus-article-add-button
3011                  start end (nth 3 entry)
3012                  (buffer-substring (match-beginning (nth 4 entry))
3013                                    (match-end (nth 4 entry)))))))
3014           (goto-char end))))
3015     (widen)))
3016
3017 ;;; External functions:
3018
3019 (defun gnus-article-add-button (from to fun &optional data)
3020   "Create a button between FROM and TO with callback FUN and data DATA."
3021   (when gnus-article-button-face
3022     (gnus-overlay-put (gnus-make-overlay from to)
3023                       'face gnus-article-button-face))
3024   (gnus-add-text-properties
3025    from to
3026    (nconc (and gnus-article-mouse-face
3027                (list gnus-mouse-face-prop gnus-article-mouse-face))
3028           (list 'gnus-callback fun)
3029           (and data (list 'gnus-data data)))))
3030
3031 ;;; Internal functions:
3032
3033 (defun gnus-article-set-globals ()
3034   (save-excursion
3035     (set-buffer gnus-summary-buffer)
3036     (gnus-set-global-variables)))
3037
3038 (defun gnus-signature-toggle (end)
3039   (save-excursion
3040     (set-buffer gnus-article-buffer)
3041     (let ((buffer-read-only nil)
3042           (inhibit-point-motion-hooks t))
3043       (if (get-text-property end 'invisible)
3044           (gnus-article-unhide-text end (point-max))
3045         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
3046
3047 (defun gnus-button-entry ()
3048   ;; Return the first entry in `gnus-button-alist' matching this place.
3049   (let ((alist gnus-button-alist)
3050         (entry nil))
3051     (while alist
3052       (setq entry (pop alist))
3053       (if (looking-at (car entry))
3054           (setq alist nil)
3055         (setq entry nil)))
3056     entry))
3057
3058 (defun gnus-button-push (marker)
3059   ;; Push button starting at MARKER.
3060   (save-excursion
3061     (set-buffer gnus-article-buffer)
3062     (goto-char marker)
3063     (let* ((entry (gnus-button-entry))
3064            (inhibit-point-motion-hooks t)
3065            (fun (nth 3 entry))
3066            (args (mapcar (lambda (group)
3067                            (let ((string (match-string group)))
3068                              (gnus-set-text-properties
3069                               0 (length string) nil string)
3070                              string))
3071                          (nthcdr 4 entry))))
3072       (cond
3073        ((fboundp fun)
3074         (apply fun args))
3075        ((and (boundp fun)
3076              (fboundp (symbol-value fun)))
3077         (apply (symbol-value fun) args))
3078        (t
3079         (gnus-message 1 "You must define `%S' to use this button"
3080                       (cons fun args)))))))
3081
3082 (defun gnus-button-message-id (message-id)
3083   "Fetch MESSAGE-ID."
3084   (save-excursion
3085     (set-buffer gnus-summary-buffer)
3086     (gnus-summary-refer-article message-id)))
3087
3088 (defun gnus-button-fetch-group (address)
3089   "Fetch GROUP specified by ADDRESS."
3090   (if (not (string-match "[:/]" address))
3091       ;; This is just a simple group url.
3092       (gnus-group-read-ephemeral-group address gnus-select-method)
3093     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
3094                            address))
3095         (error "Can't parse %s" address)
3096       (gnus-group-read-ephemeral-group
3097        (match-string 4 address)
3098        `(nntp ,(match-string 1 address)
3099               (nntp-address ,(match-string 1 address))
3100               (nntp-port-number ,(if (match-end 3)
3101                                      (match-string 3 address)
3102                                    "nntp")))))))
3103
3104 (defun gnus-url-parse-query-string (query &optional downcase)
3105   (let (retval pairs cur key val)
3106     (setq pairs (split-string query "&"))
3107     (while pairs
3108       (setq cur (car pairs)
3109             pairs (cdr pairs))
3110       (if (not (string-match "=" cur))
3111           nil                           ; Grace
3112         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
3113               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
3114         (if downcase
3115             (setq key (downcase key)))
3116         (setq cur (assoc key retval))
3117         (if cur
3118             (setcdr cur (cons val (cdr cur)))
3119           (setq retval (cons (list key val) retval)))))
3120     retval))
3121
3122 (defun gnus-url-unhex (x)
3123   (if (> x ?9)
3124       (if (>= x ?a)
3125           (+ 10 (- x ?a))
3126         (+ 10 (- x ?A)))
3127     (- x ?0)))
3128
3129 (defun gnus-url-unhex-string (str &optional allow-newlines)
3130   "Remove %XXX embedded spaces, etc in a url.
3131 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
3132 decoding of carriage returns and line feeds in the string, which is normally
3133 forbidden in URL encoding."
3134   (setq str (or str ""))
3135   (let ((tmp "")
3136         (case-fold-search t))
3137     (while (string-match "%[0-9a-f][0-9a-f]" str)
3138       (let* ((start (match-beginning 0))
3139              (ch1 (gnus-url-unhex (elt str (+ start 1))))
3140              (code (+ (* 16 ch1)
3141                       (gnus-url-unhex (elt str (+ start 2))))))
3142         (setq tmp (concat
3143                    tmp (substring str 0 start)
3144                    (cond
3145                     (allow-newlines
3146                      (char-to-string code))
3147                     ((or (= code ?\n) (= code ?\r))
3148                      " ")
3149                     (t (char-to-string code))))
3150               str (substring str (match-end 0)))))
3151     (setq tmp (concat tmp str))
3152     tmp))
3153
3154 (defun gnus-url-mailto (url)
3155   ;; Send mail to someone
3156   (when (string-match "mailto:/*\\(.*\\)" url)
3157     (setq url (substring url (match-beginning 1) nil)))
3158   (let (to args subject func)
3159     (if (string-match (regexp-quote "?") url)
3160         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
3161               args (gnus-url-parse-query-string
3162                     (substring url (match-end 0) nil) t))
3163       (setq to (gnus-url-unhex-string url)))
3164     (setq args (cons (list "to" to) args)
3165           subject (cdr-safe (assoc "subject" args)))
3166     (message-mail)
3167     (while args
3168       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
3169       (if (fboundp func)
3170           (funcall func)
3171         (message-position-on-field (caar args)))
3172       (insert (mapconcat 'identity (cdar args) ", "))
3173       (setq args (cdr args)))
3174     (if subject
3175         (message-goto-body)
3176       (message-goto-subject))))
3177
3178 (defun gnus-button-mailto (address)
3179   ;; Mail to ADDRESS.
3180   (set-buffer (gnus-copy-article-buffer))
3181   (message-reply address))
3182
3183 (defun gnus-button-reply (address)
3184   ;; Reply to ADDRESS.
3185   (message-reply address))
3186
3187 (defun gnus-button-url (address)
3188   "Browse ADDRESS."
3189   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
3190   (if (listp browse-url-browser-function)
3191       (browse-url address)
3192     (funcall browse-url-browser-function address)))
3193
3194 (defun gnus-button-embedded-url (address)
3195   "Browse ADDRESS."
3196   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
3197   (if (listp browse-url-browser-function)
3198       (browse-url (gnus-strip-whitespace address))
3199     (funcall browse-url-browser-function (gnus-strip-whitespace address))))
3200
3201 ;;; Next/prev buttons in the article buffer.
3202
3203 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
3204 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
3205
3206 (defvar gnus-prev-page-map nil)
3207 (unless gnus-prev-page-map
3208   (setq gnus-prev-page-map (make-sparse-keymap))
3209   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
3210   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
3211
3212 (defun gnus-insert-prev-page-button ()
3213   (let ((buffer-read-only nil))
3214     (gnus-eval-format
3215      gnus-prev-page-line-format nil
3216      `(gnus-prev t local-map ,gnus-prev-page-map
3217                  gnus-callback gnus-article-button-prev-page
3218                  gnus-type annotation))))
3219
3220 (defvar gnus-next-page-map nil)
3221 (unless gnus-next-page-map
3222   (setq gnus-next-page-map (make-keymap))
3223   (suppress-keymap gnus-prev-page-map)
3224   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
3225   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
3226
3227 (defun gnus-button-next-page ()
3228   "Go to the next page."
3229   (interactive)
3230   (let ((win (selected-window)))
3231     (select-window (get-buffer-window gnus-article-buffer t))
3232     (gnus-article-next-page)
3233     (select-window win)))
3234
3235 (defun gnus-button-prev-page ()
3236   "Go to the prev page."
3237   (interactive)
3238   (let ((win (selected-window)))
3239     (select-window (get-buffer-window gnus-article-buffer t))
3240     (gnus-article-prev-page)
3241     (select-window win)))
3242
3243 (defun gnus-insert-next-page-button ()
3244   (let ((buffer-read-only nil))
3245     (gnus-eval-format gnus-next-page-line-format nil
3246                       `(gnus-next
3247                         t local-map ,gnus-next-page-map
3248                         gnus-callback gnus-article-button-next-page
3249                         gnus-type annotation))))
3250
3251 (defun gnus-article-button-next-page (arg)
3252   "Go to the next page."
3253   (interactive "P")
3254   (let ((win (selected-window)))
3255     (select-window (get-buffer-window gnus-article-buffer t))
3256     (gnus-article-next-page)
3257     (select-window win)))
3258
3259 (defun gnus-article-button-prev-page (arg)
3260   "Go to the prev page."
3261   (interactive "P")
3262   (let ((win (selected-window)))
3263     (select-window (get-buffer-window gnus-article-buffer t))
3264     (gnus-article-prev-page)
3265     (select-window win)))
3266
3267 (gnus-ems-redefine)
3268
3269 (provide 'gnus-art)
3270
3271 (run-hooks 'gnus-art-load-hook)
3272
3273 ;;; gnus-art.el ends here