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