Importing gnus-5.6.33
[elisp/gnus.git-] / 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 (get-buffer-create gnus-original-article-buffer))
1986       (buffer-disable-undo (current-buffer))
1987       (setq major-mode 'gnus-original-article-mode)
1988       (gnus-add-current-to-buffer-list)
1989       (make-local-variable 'gnus-original-article))
1990     (if (get-buffer name)
1991         (save-excursion
1992           (set-buffer name)
1993           (buffer-disable-undo (current-buffer))
1994           (setq buffer-read-only t)
1995           (gnus-add-current-to-buffer-list)
1996           (unless (eq major-mode 'gnus-article-mode)
1997             (gnus-article-mode))
1998           (current-buffer))
1999       (save-excursion
2000         (set-buffer (get-buffer-create name))
2001         (gnus-add-current-to-buffer-list)
2002         (gnus-article-mode)
2003         (make-local-variable 'gnus-summary-buffer)
2004         (current-buffer)))))
2005
2006 ;; Set article window start at LINE, where LINE is the number of lines
2007 ;; from the head of the article.
2008 (defun gnus-article-set-window-start (&optional line)
2009   (set-window-start
2010    (get-buffer-window gnus-article-buffer t)
2011    (save-excursion
2012      (set-buffer gnus-article-buffer)
2013      (goto-char (point-min))
2014      (if (not line)
2015          (point-min)
2016        (gnus-message 6 "Moved to bookmark")
2017        (search-forward "\n\n" nil t)
2018        (forward-line line)
2019        (point)))))
2020
2021 (defun gnus-article-prepare (article &optional all-headers header)
2022   "Prepare ARTICLE in article mode buffer.
2023 ARTICLE should either be an article number or a Message-ID.
2024 If ARTICLE is an id, HEADER should be the article headers.
2025 If ALL-HEADERS is non-nil, no headers are hidden."
2026   (save-excursion
2027     ;; Make sure we start in a summary buffer.
2028     (unless (eq major-mode 'gnus-summary-mode)
2029       (set-buffer gnus-summary-buffer))
2030     (setq gnus-summary-buffer (current-buffer))
2031     (let* ((gnus-article (if header (mail-header-number header) article))
2032            (summary-buffer (current-buffer))
2033            (internal-hook gnus-article-internal-prepare-hook)
2034            (group gnus-newsgroup-name)
2035            result)
2036       (save-excursion
2037         (gnus-article-setup-buffer)
2038         (set-buffer gnus-article-buffer)
2039         ;; Deactivate active regions.
2040         (when (and (boundp 'transient-mark-mode)
2041                    transient-mark-mode)
2042           (setq mark-active nil))
2043         (if (not (setq result (let ((buffer-read-only nil))
2044                                 (gnus-request-article-this-buffer
2045                                  article group))))
2046             ;; There is no such article.
2047             (save-excursion
2048               (when (and (numberp article)
2049                          (not (memq article gnus-newsgroup-sparse)))
2050                 (setq gnus-article-current
2051                       (cons gnus-newsgroup-name article))
2052                 (set-buffer gnus-summary-buffer)
2053                 (setq gnus-current-article article)
2054                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2055                     (progn
2056                       (gnus-summary-set-agent-mark article)
2057                       (message "Message marked for downloading"))
2058                   (gnus-summary-mark-article article gnus-canceled-mark)
2059                   (unless (memq article gnus-newsgroup-sparse)
2060                     (gnus-error 1
2061                      "No such article (may have expired or been canceled)")))))
2062           (if (or (eq result 'pseudo) (eq result 'nneething))
2063               (progn
2064                 (save-excursion
2065                   (set-buffer summary-buffer)
2066                   (push article gnus-newsgroup-history)
2067                   (setq gnus-last-article gnus-current-article
2068                         gnus-current-article 0
2069                         gnus-current-headers nil
2070                         gnus-article-current nil)
2071                   (if (eq result 'nneething)
2072                       (gnus-configure-windows 'summary)
2073                     (gnus-configure-windows 'article))
2074                   (gnus-set-global-variables))
2075                 (gnus-set-mode-line 'article))
2076             ;; The result from the `request' was an actual article -
2077             ;; or at least some text that is now displayed in the
2078             ;; article buffer.
2079             (when (and (numberp article)
2080                        (not (eq article gnus-current-article)))
2081               ;; Seems like a new article has been selected.
2082               ;; `gnus-current-article' must be an article number.
2083               (save-excursion
2084                 (set-buffer summary-buffer)
2085                 (push article gnus-newsgroup-history)
2086                 (setq gnus-last-article gnus-current-article
2087                       gnus-current-article article
2088                       gnus-current-headers
2089                       (gnus-summary-article-header gnus-current-article)
2090                       gnus-article-current
2091                       (cons gnus-newsgroup-name gnus-current-article))
2092                 (unless (vectorp gnus-current-headers)
2093                   (setq gnus-current-headers nil))
2094                 (gnus-summary-goto-subject gnus-current-article)
2095                 (when (gnus-summary-show-thread)
2096                   ;; If the summary buffer really was folded, the
2097                   ;; previous goto may not actually have gone to
2098                   ;; the right article, but the thread root instead.
2099                   ;; So we go again.
2100                   (gnus-summary-goto-subject gnus-current-article))
2101                 (gnus-run-hooks 'gnus-mark-article-hook)
2102                 (gnus-set-mode-line 'summary)
2103                 (when (gnus-visual-p 'article-highlight 'highlight)
2104                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2105                 ;; Set the global newsgroup variables here.
2106                 ;; Suggested by Jim Sisolak
2107                 ;; <sisolak@trans4.neep.wisc.edu>.
2108                 (gnus-set-global-variables)
2109                 (setq gnus-have-all-headers
2110                       (or all-headers gnus-show-all-headers))))
2111             (when (or (numberp article)
2112                       (stringp article))
2113               ;; Hooks for getting information from the article.
2114               ;; This hook must be called before being narrowed.
2115               (let (buffer-read-only)
2116                 (gnus-run-hooks 'internal-hook)
2117                 (gnus-run-hooks 'gnus-article-prepare-hook)
2118                 ;; Decode MIME message.
2119                 (when gnus-show-mime
2120                   (if (or (not gnus-strict-mime)
2121                           (gnus-fetch-field "Mime-Version"))
2122                       (let ((coding-system-for-write 'binary)
2123                             (coding-system-for-read 'binary))
2124                         (funcall gnus-show-mime-method))
2125                     (funcall gnus-decode-encoded-word-method)))
2126                 ;; Perform the article display hooks.
2127                 (gnus-run-hooks 'gnus-article-display-hook))
2128               ;; Do page break.
2129               (goto-char (point-min))
2130               (setq gnus-page-broken
2131                     (when gnus-break-pages
2132                       (gnus-narrow-to-page)
2133                       t)))
2134             (gnus-set-mode-line 'article)
2135             (gnus-configure-windows 'article)
2136             (goto-char (point-min))
2137             (search-forward "\n\n" nil t)
2138             (set-window-point (get-buffer-window (current-buffer)) (point))
2139             t))))))
2140
2141 (defun gnus-article-wash-status ()
2142   "Return a string which display status of article washing."
2143   (save-excursion
2144     (set-buffer gnus-article-buffer)
2145     (let ((cite (gnus-article-hidden-text-p 'cite))
2146           (headers (gnus-article-hidden-text-p 'headers))
2147           (boring (gnus-article-hidden-text-p 'boring-headers))
2148           (pgp (gnus-article-hidden-text-p 'pgp))
2149           (pem (gnus-article-hidden-text-p 'pem))
2150           (signature (gnus-article-hidden-text-p 'signature))
2151           (overstrike (gnus-article-hidden-text-p 'overstrike))
2152           (emphasis (gnus-article-hidden-text-p 'emphasis))
2153           (mime gnus-show-mime))
2154       (format "%c%c%c%c%c%c%c"
2155               (if cite ?c ? )
2156               (if (or headers boring) ?h ? )
2157               (if (or pgp pem) ?p ? )
2158               (if signature ?s ? )
2159               (if overstrike ?o ? )
2160               (if mime ?m ? )
2161               (if emphasis ?e ? )))))
2162
2163 (fset 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
2164
2165 (defun gnus-article-maybe-hide-headers ()
2166   "Hide unwanted headers if `gnus-have-all-headers' is nil.
2167 Provided for backwards compatibility."
2168   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
2169       gnus-inhibit-hiding
2170       (gnus-article-hide-headers)))
2171
2172 ;;; Article savers.
2173
2174 (defun gnus-output-to-file (file-name)
2175   "Append the current article to a file named FILE-NAME."
2176   (let ((artbuf (current-buffer)))
2177     (nnheader-temp-write nil
2178       (insert-buffer-substring artbuf)
2179       ;; Append newline at end of the buffer as separator, and then
2180       ;; save it to file.
2181       (goto-char (point-max))
2182       (insert "\n")
2183       (append-to-file (point-min) (point-max) file-name)
2184       t)))
2185
2186 (defun gnus-narrow-to-page (&optional arg)
2187   "Narrow the article buffer to a page.
2188 If given a numerical ARG, move forward ARG pages."
2189   (interactive "P")
2190   (setq arg (if arg (prefix-numeric-value arg) 0))
2191   (save-excursion
2192     (set-buffer gnus-article-buffer)
2193     (goto-char (point-min))
2194     (widen)
2195     ;; Remove any old next/prev buttons.
2196     (when (gnus-visual-p 'page-marker)
2197       (let ((buffer-read-only nil))
2198         (gnus-remove-text-with-property 'gnus-prev)
2199         (gnus-remove-text-with-property 'gnus-next)))
2200     (when
2201         (cond ((< arg 0)
2202                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
2203               ((> arg 0)
2204                (re-search-forward page-delimiter nil 'move arg)))
2205       (goto-char (match-end 0)))
2206     (narrow-to-region
2207      (point)
2208      (if (re-search-forward page-delimiter nil 'move)
2209          (match-beginning 0)
2210        (point)))
2211     (when (and (gnus-visual-p 'page-marker)
2212                (not (= (point-min) 1)))
2213       (save-excursion
2214         (goto-char (point-min))
2215         (gnus-insert-prev-page-button)))
2216     (when (and (gnus-visual-p 'page-marker)
2217                (< (+ (point-max) 2) (buffer-size)))
2218       (save-excursion
2219         (goto-char (point-max))
2220         (gnus-insert-next-page-button)))))
2221
2222 ;; Article mode commands
2223
2224 (defun gnus-article-goto-next-page ()
2225   "Show the next page of the article."
2226   (interactive)
2227   (when (gnus-article-next-page)
2228     (goto-char (point-min))
2229     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
2230
2231 (defun gnus-article-goto-prev-page ()
2232   "Show the next page of the article."
2233   (interactive)
2234   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
2235     (gnus-article-prev-page nil)))
2236
2237 (defun gnus-article-next-page (&optional lines)
2238   "Show the next page of the current article.
2239 If end of article, return non-nil.  Otherwise return nil.
2240 Argument LINES specifies lines to be scrolled up."
2241   (interactive "p")
2242   (move-to-window-line -1)
2243   (if (save-excursion
2244         (end-of-line)
2245         (and (pos-visible-in-window-p)  ;Not continuation line.
2246              (eobp)))
2247       ;; Nothing in this page.
2248       (if (or (not gnus-page-broken)
2249               (save-excursion
2250                 (save-restriction
2251                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
2252           t                             ;Nothing more.
2253         (gnus-narrow-to-page 1)         ;Go to next page.
2254         nil)
2255     ;; More in this page.
2256     (let ((scroll-in-place nil))
2257       (condition-case ()
2258           (scroll-up lines)
2259         (end-of-buffer
2260          ;; Long lines may cause an end-of-buffer error.
2261          (goto-char (point-max)))))
2262     (move-to-window-line 0)
2263     nil))
2264
2265 (defun gnus-article-prev-page (&optional lines)
2266   "Show previous page of current article.
2267 Argument LINES specifies lines to be scrolled down."
2268   (interactive "p")
2269   (move-to-window-line 0)
2270   (if (and gnus-page-broken
2271            (bobp)
2272            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
2273       (progn
2274         (gnus-narrow-to-page -1)        ;Go to previous page.
2275         (goto-char (point-max))
2276         (recenter -1))
2277     (let ((scroll-in-place nil))
2278       (prog1
2279           (condition-case ()
2280               (scroll-down lines)
2281             (beginning-of-buffer
2282              (goto-char (point-min))))
2283         (move-to-window-line 0)))))
2284
2285 (defun gnus-article-refer-article ()
2286   "Read article specified by message-id around point."
2287   (interactive)
2288   (let ((point (point)))
2289     (search-forward ">" nil t)          ;Move point to end of "<....>".
2290     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
2291         (let ((message-id (match-string 1)))
2292           (goto-char point)
2293           (set-buffer gnus-summary-buffer)
2294           (gnus-summary-refer-article message-id))
2295       (goto-char (point))
2296       (error "No references around point"))))
2297
2298 (defun gnus-article-show-summary ()
2299   "Reconfigure windows to show summary buffer."
2300   (interactive)
2301   (if (not (gnus-buffer-live-p gnus-summary-buffer))
2302       (error "There is no summary buffer for this article buffer")
2303     (gnus-article-set-globals)
2304     (gnus-configure-windows 'article)
2305     (gnus-summary-goto-subject gnus-current-article)
2306     (gnus-summary-position-point)))
2307
2308 (defun gnus-article-describe-briefly ()
2309   "Describe article mode commands briefly."
2310   (interactive)
2311   (gnus-message 6
2312                 (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")))
2313
2314 (defun gnus-article-summary-command ()
2315   "Execute the last keystroke in the summary buffer."
2316   (interactive)
2317   (let ((obuf (current-buffer))
2318         (owin (current-window-configuration))
2319         func)
2320     (switch-to-buffer gnus-article-current-summary 'norecord)
2321     (setq func (lookup-key (current-local-map) (this-command-keys)))
2322     (call-interactively func)
2323     (set-buffer obuf)
2324     (set-window-configuration owin)
2325     (set-window-point (get-buffer-window (current-buffer)) (point))))
2326
2327 (defun gnus-article-summary-command-nosave ()
2328   "Execute the last keystroke in the summary buffer."
2329   (interactive)
2330   (let (func)
2331     (pop-to-buffer gnus-article-current-summary 'norecord)
2332     (setq func (lookup-key (current-local-map) (this-command-keys)))
2333     (call-interactively func)))
2334
2335 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
2336   "Read a summary buffer key sequence and execute it from the article buffer."
2337   (interactive "P")
2338   (let ((nosaves
2339          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
2340            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
2341            "=" "^" "\M-^" "|"))
2342         (nosave-but-article
2343          '("A\r"))
2344         (nosave-in-article
2345          '("\C-d"))
2346         (up-to-top
2347          '("n" "Gn" "p" "Gp"))
2348         keys new-sum-point)
2349     (save-excursion
2350       (set-buffer gnus-article-current-summary)
2351       (let (gnus-pick-mode)
2352         (push (or key last-command-event) unread-command-events)
2353         (setq keys (read-key-sequence nil))))
2354     (message "")
2355
2356     (if (or (member keys nosaves)
2357             (member keys nosave-but-article)
2358             (member keys nosave-in-article))
2359         (let (func)
2360           (save-window-excursion
2361             (pop-to-buffer gnus-article-current-summary 'norecord)
2362             ;; We disable the pick minor mode commands.
2363             (let (gnus-pick-mode)
2364               (setq func (lookup-key (current-local-map) keys))))
2365           (if (not func)
2366               (ding)
2367             (unless (member keys nosave-in-article)
2368               (set-buffer gnus-article-current-summary))
2369             (call-interactively func)
2370             (setq new-sum-point (point)))
2371           (when (member keys nosave-but-article)
2372             (pop-to-buffer gnus-article-buffer 'norecord)))
2373       ;; These commands should restore window configuration.
2374       (let ((obuf (current-buffer))
2375             (owin (current-window-configuration))
2376             (opoint (point))
2377             (summary gnus-article-current-summary)
2378             func in-buffer selected)
2379         (if not-restore-window
2380             (pop-to-buffer summary 'norecord)
2381           (switch-to-buffer summary 'norecord))
2382         (setq in-buffer (current-buffer))
2383         ;; We disable the pick minor mode commands.
2384         (if (setq func (let (gnus-pick-mode)
2385                          (lookup-key (current-local-map) keys)))
2386             (progn
2387               (call-interactively func)
2388               (setq new-sum-point (point)))
2389           (ding))
2390         (when (eq in-buffer (current-buffer))
2391           (setq selected (gnus-summary-select-article))
2392           (set-buffer obuf)
2393           (unless not-restore-window
2394             (set-window-configuration owin))
2395           (unless (or (not (eq selected 'old)) (member keys up-to-top))
2396             (set-window-point (get-buffer-window (current-buffer))
2397                               opoint))
2398           (let ((win (get-buffer-window gnus-article-current-summary)))
2399             (when win
2400               (set-window-point win new-sum-point))))))))
2401
2402 (defun gnus-article-hide (&optional arg force)
2403   "Hide all the gruft in the current article.
2404 This means that PGP stuff, signatures, cited text and (some)
2405 headers will be hidden.
2406 If given a prefix, show the hidden text instead."
2407   (interactive (append (gnus-article-hidden-arg) (list 'force)))
2408   (gnus-article-hide-headers arg)
2409   (gnus-article-hide-pgp arg)
2410   (gnus-article-hide-citation-maybe arg force)
2411   (gnus-article-hide-signature arg))
2412
2413 (defun gnus-article-maybe-highlight ()
2414   "Do some article highlighting if article highlighting is requested."
2415   (when (gnus-visual-p 'article-highlight 'highlight)
2416     (gnus-article-highlight-some)))
2417
2418 (defun gnus-check-group-server ()
2419   ;; Make sure the connection to the server is alive.
2420   (unless (gnus-server-opened
2421            (gnus-find-method-for-group gnus-newsgroup-name))
2422     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
2423     (gnus-request-group gnus-newsgroup-name t)))
2424
2425 (defun gnus-request-article-this-buffer (article group)
2426   "Get an article and insert it into this buffer."
2427   (let (do-update-line sparse-header)
2428     (prog1
2429         (save-excursion
2430           (erase-buffer)
2431           (gnus-kill-all-overlays)
2432           (setq group (or group gnus-newsgroup-name))
2433
2434           ;; Using `gnus-request-article' directly will insert the article into
2435           ;; `nntp-server-buffer' - so we'll save some time by not having to
2436           ;; copy it from the server buffer into the article buffer.
2437
2438           ;; We only request an article by message-id when we do not have the
2439           ;; headers for it, so we'll have to get those.
2440           (when (stringp article)
2441             (let ((gnus-override-method gnus-refer-article-method))
2442               (gnus-read-header article)))
2443
2444           ;; If the article number is negative, that means that this article
2445           ;; doesn't belong in this newsgroup (possibly), so we find its
2446           ;; message-id and request it by id instead of number.
2447           (when (and (numberp article)
2448                      gnus-summary-buffer
2449                      (get-buffer gnus-summary-buffer)
2450                      (gnus-buffer-exists-p gnus-summary-buffer))
2451             (save-excursion
2452               (set-buffer gnus-summary-buffer)
2453               (let ((header (gnus-summary-article-header article)))
2454                 (when (< article 0)
2455                   (cond
2456                    ((memq article gnus-newsgroup-sparse)
2457                     ;; This is a sparse gap article.
2458                     (setq do-update-line article)
2459                     (setq article (mail-header-id header))
2460                     (let ((gnus-override-method gnus-refer-article-method))
2461                       (setq sparse-header (gnus-read-header article)))
2462                     (setq gnus-newsgroup-sparse
2463                           (delq article gnus-newsgroup-sparse)))
2464                    ((vectorp header)
2465                     ;; It's a real article.
2466                     (setq article (mail-header-id header)))
2467                    (t
2468                     ;; It is an extracted pseudo-article.
2469                     (setq article 'pseudo)
2470                     (gnus-request-pseudo-article header))))
2471
2472                 (let ((method (gnus-find-method-for-group
2473                                gnus-newsgroup-name)))
2474                   (when (and (eq (car method) 'nneething)
2475                              (vectorp header))
2476                     (let ((dir (concat (file-name-as-directory (nth 1 method))
2477                                        (mail-header-subject header))))
2478                       (when (file-directory-p dir)
2479                         (setq article 'nneething)
2480                         (gnus-group-enter-directory dir))))))))
2481
2482           (cond
2483            ;; Refuse to select canceled articles.
2484            ((and (numberp article)
2485                  gnus-summary-buffer
2486                  (get-buffer gnus-summary-buffer)
2487                  (gnus-buffer-exists-p gnus-summary-buffer)
2488                  (eq (cdr (save-excursion
2489                             (set-buffer gnus-summary-buffer)
2490                             (assq article gnus-newsgroup-reads)))
2491                      gnus-canceled-mark))
2492             nil)
2493            ;; We first check `gnus-original-article-buffer'.
2494            ((and (get-buffer gnus-original-article-buffer)
2495                  (numberp article)
2496                  (save-excursion
2497                    (set-buffer gnus-original-article-buffer)
2498                    (and (equal (car gnus-original-article) group)
2499                         (eq (cdr gnus-original-article) article))))
2500             (insert-buffer-substring gnus-original-article-buffer)
2501             'article)
2502            ;; Check the backlog.
2503            ((and gnus-keep-backlog
2504                  (gnus-backlog-request-article group article (current-buffer)))
2505             'article)
2506            ;; Check asynchronous pre-fetch.
2507            ((gnus-async-request-fetched-article group article (current-buffer))
2508             (gnus-async-prefetch-next group article gnus-summary-buffer)
2509             (when (and (numberp article) gnus-keep-backlog)
2510               (gnus-backlog-enter-article group article (current-buffer)))
2511             'article)
2512            ;; Check the cache.
2513            ((and gnus-use-cache
2514                  (numberp article)
2515                  (gnus-cache-request-article article group))
2516             'article)
2517            ;; Get the article and put into the article buffer.
2518            ((or (stringp article) (numberp article))
2519             (let ((gnus-override-method
2520                    (and (stringp article) gnus-refer-article-method))
2521                   (buffer-read-only nil))
2522               (erase-buffer)
2523               (gnus-kill-all-overlays)
2524               (gnus-check-group-server)
2525               (when (gnus-request-article article group (current-buffer))
2526                 (when (numberp article)
2527                   (gnus-async-prefetch-next group article gnus-summary-buffer)
2528                   (when gnus-keep-backlog
2529                     (gnus-backlog-enter-article
2530                      group article (current-buffer))))
2531                 'article)))
2532            ;; It was a pseudo.
2533            (t article)))
2534
2535       ;; Associate this article with the current summary buffer.
2536       (setq gnus-article-current-summary gnus-summary-buffer)
2537
2538       ;; Take the article from the original article buffer
2539       ;; and place it in the buffer it's supposed to be in.
2540       (when (and (get-buffer gnus-article-buffer)
2541                  (equal (buffer-name (current-buffer))
2542                         (buffer-name (get-buffer gnus-article-buffer))))
2543         (save-excursion
2544           (if (get-buffer gnus-original-article-buffer)
2545               (set-buffer gnus-original-article-buffer)
2546             (set-buffer (get-buffer-create gnus-original-article-buffer))
2547             (buffer-disable-undo (current-buffer))
2548             (setq major-mode 'gnus-original-article-mode)
2549             (setq buffer-read-only t)
2550             (gnus-add-current-to-buffer-list))
2551           (let (buffer-read-only)
2552             (erase-buffer)
2553             (insert-buffer-substring gnus-article-buffer))
2554           (setq gnus-original-article (cons group article))))
2555
2556       ;; Update sparse articles.
2557       (when (and do-update-line
2558                  (or (numberp article)
2559                      (stringp article)))
2560         (let ((buf (current-buffer)))
2561           (set-buffer gnus-summary-buffer)
2562           (gnus-summary-update-article do-update-line sparse-header)
2563           (gnus-summary-goto-subject do-update-line nil t)
2564           (set-window-point (get-buffer-window (current-buffer) t)
2565                             (point))
2566           (set-buffer buf))))))
2567
2568 ;;;
2569 ;;; Article editing
2570 ;;;
2571
2572 (defcustom gnus-article-edit-mode-hook nil
2573   "Hook run in article edit mode buffers."
2574   :group 'gnus-article-various
2575   :type 'hook)
2576
2577 (defvar gnus-article-edit-done-function nil)
2578
2579 (defvar gnus-article-edit-mode-map nil)
2580
2581 (unless gnus-article-edit-mode-map
2582   (setq gnus-article-edit-mode-map (copy-keymap text-mode-map))
2583
2584   (gnus-define-keys gnus-article-edit-mode-map
2585     "\C-c\C-c" gnus-article-edit-done
2586     "\C-c\C-k" gnus-article-edit-exit)
2587
2588   (gnus-define-keys (gnus-article-edit-wash-map
2589                      "\C-c\C-w" gnus-article-edit-mode-map)
2590     "f" gnus-article-edit-full-stops))
2591
2592 (defun gnus-article-edit-mode ()
2593   "Major mode for editing articles.
2594 This is an extended text-mode.
2595
2596 \\{gnus-article-edit-mode-map}"
2597   (interactive)
2598   (setq major-mode 'gnus-article-edit-mode)
2599   (setq mode-name "Article Edit")
2600   (use-local-map gnus-article-edit-mode-map)
2601   (make-local-variable 'gnus-article-edit-done-function)
2602   (make-local-variable 'gnus-prev-winconf)
2603   (setq buffer-read-only nil)
2604   (buffer-enable-undo)
2605   (widen)
2606   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
2607
2608 (defun gnus-article-edit (&optional force)
2609   "Edit the current article.
2610 This will have permanent effect only in mail groups.
2611 If FORCE is non-nil, allow editing of articles even in read-only
2612 groups."
2613   (interactive "P")
2614   (when (and (not force)
2615              (gnus-group-read-only-p))
2616     (error "The current newsgroup does not support article editing"))
2617   (gnus-article-date-original)
2618   (gnus-article-edit-article
2619    `(lambda (no-highlight)
2620       (gnus-summary-edit-article-done
2621        ,(or (mail-header-references gnus-current-headers) "")
2622        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
2623
2624 (defun gnus-article-edit-article (exit-func)
2625   "Start editing the contents of the current article buffer."
2626   (let ((winconf (current-window-configuration)))
2627     (set-buffer gnus-article-buffer)
2628     (gnus-article-edit-mode)
2629     (gnus-article-delete-text-of-type 'annotation)
2630     (gnus-set-text-properties (point-min) (point-max) nil)
2631     (gnus-configure-windows 'edit-article)
2632     (setq gnus-article-edit-done-function exit-func)
2633     (setq gnus-prev-winconf winconf)
2634     (gnus-message 6 "C-c C-c to end edits")))
2635
2636 (defun gnus-article-edit-done (&optional arg)
2637   "Update the article edits and exit."
2638   (interactive "P")
2639   (save-excursion
2640     (save-restriction
2641       (widen)
2642       (goto-char (point-min))
2643       (when (search-forward "\n\n" nil 1)
2644         (let ((lines (count-lines (point) (point-max)))
2645               (length (- (point-max) (point)))
2646               (case-fold-search t)
2647               (body (copy-marker (point))))
2648           (goto-char (point-min))
2649           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
2650             (delete-region (match-beginning 1) (match-end 1))
2651             (insert (number-to-string length)))
2652           (goto-char (point-min))
2653           (when (re-search-forward
2654                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
2655             (delete-region (match-beginning 1) (match-end 1))
2656             (insert (number-to-string length)))
2657           (goto-char (point-min))
2658           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
2659             (delete-region (match-beginning 1) (match-end 1))
2660             (insert (number-to-string lines)))))))
2661   (let ((func gnus-article-edit-done-function)
2662         (buf (current-buffer))
2663         (start (window-start)))
2664     (gnus-article-edit-exit)
2665     (save-excursion
2666       (set-buffer buf)
2667       (let ((buffer-read-only nil))
2668         (funcall func arg)))
2669     (set-buffer buf)
2670     (set-window-start (get-buffer-window buf) start)
2671     (set-window-point (get-buffer-window buf) (point))))
2672
2673 (defun gnus-article-edit-exit ()
2674   "Exit the article editing without updating."
2675   (interactive)
2676   ;; We remove all text props from the article buffer.
2677   (let ((buf (format "%s" (buffer-string)))
2678         (curbuf (current-buffer))
2679         (p (point))
2680         (window-start (window-start)))
2681     (erase-buffer)
2682     (insert buf)
2683     (let ((winconf gnus-prev-winconf))
2684       (gnus-article-mode)
2685       ;; The cache and backlog have to be flushed somewhat.
2686       (when gnus-use-cache
2687         (gnus-cache-update-article
2688          (car gnus-article-current) (cdr gnus-article-current)))
2689       (when gnus-keep-backlog
2690         (gnus-backlog-remove-article
2691          (car gnus-article-current) (cdr gnus-article-current)))
2692       ;; Flush original article as well.
2693       (save-excursion
2694         (when (get-buffer gnus-original-article-buffer)
2695           (set-buffer gnus-original-article-buffer)
2696           (setq gnus-original-article nil)))
2697       (set-window-configuration winconf)
2698       ;; Tippy-toe some to make sure that point remains where it was.
2699       (let ((buf (current-buffer)))
2700         (set-buffer curbuf)
2701         (set-window-start (get-buffer-window (current-buffer)) window-start)
2702         (goto-char p)
2703         (set-buffer buf)))))
2704
2705 (defun gnus-article-edit-full-stops ()
2706   "Interactively repair spacing at end of sentences."
2707   (interactive)
2708   (save-excursion
2709     (goto-char (point-min))
2710     (search-forward-regexp "^$" nil t)
2711     (let ((case-fold-search nil))
2712       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
2713
2714 ;;;
2715 ;;; Article highlights
2716 ;;;
2717
2718 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
2719
2720 ;;; Internal Variables:
2721
2722 (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\\)"
2723   "Regular expression that matches URLs."
2724   :group 'gnus-article-buttons
2725   :type 'regexp)
2726
2727 (defcustom gnus-button-alist
2728   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>" 0 t
2729      gnus-button-message-id 2)
2730     ("\\bnews:\\([^>\n\t ]*@[^>\n\t ]*\\)" 0 t gnus-button-message-id 1)
2731     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
2732      1 t
2733      gnus-button-fetch-group 4)
2734     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
2735     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
2736      t gnus-button-message-id 3)
2737     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
2738     ("mailto:\\([a-zA-Z.-@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
2739     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
2740     ;; This is how URLs _should_ be embedded in text...
2741     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
2742     ;; Raw URLs.
2743     (,gnus-button-url-regexp 0 t gnus-button-url 0))
2744   "*Alist of regexps matching buttons in article bodies.
2745
2746 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
2747 REGEXP: is the string matching text around the button,
2748 BUTTON: is the number of the regexp grouping actually matching the button,
2749 FORM: is a lisp expression which must eval to true for the button to
2750 be added,
2751 CALLBACK: is the function to call when the user push this button, and each
2752 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
2753
2754 CALLBACK can also be a variable, in that case the value of that
2755 variable it the real callback function."
2756   :group 'gnus-article-buttons
2757   :type '(repeat (list regexp
2758                        (integer :tag "Button")
2759                        (sexp :tag "Form")
2760                        (function :tag "Callback")
2761                        (repeat :tag "Par"
2762                                :inline t
2763                                (integer :tag "Regexp group")))))
2764
2765 (defcustom gnus-header-button-alist
2766   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
2767      0 t gnus-button-message-id 0)
2768     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
2769     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
2770      0 t gnus-button-mailto 0)
2771     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2772     ("^Subject:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2773     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2774     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
2775      gnus-button-message-id 3))
2776   "*Alist of headers and regexps to match buttons in article heads.
2777
2778 This alist is very similar to `gnus-button-alist', except that each
2779 alist has an additional HEADER element first in each entry:
2780
2781 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
2782
2783 HEADER is a regexp to match a header.  For a fuller explanation, see
2784 `gnus-button-alist'."
2785   :group 'gnus-article-buttons
2786   :group 'gnus-article-headers
2787   :type '(repeat (list (regexp :tag "Header")
2788                        regexp
2789                        (integer :tag "Button")
2790                        (sexp :tag "Form")
2791                        (function :tag "Callback")
2792                        (repeat :tag "Par"
2793                                :inline t
2794                                (integer :tag "Regexp group")))))
2795
2796 (defvar gnus-button-regexp nil)
2797 (defvar gnus-button-marker-list nil)
2798 ;; Regexp matching any of the regexps from `gnus-button-alist'.
2799
2800 (defvar gnus-button-last nil)
2801 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
2802
2803 ;;; Commands:
2804
2805 (defun gnus-article-push-button (event)
2806   "Check text under the mouse pointer for a callback function.
2807 If the text under the mouse pointer has a `gnus-callback' property,
2808 call it with the value of the `gnus-data' text property."
2809   (interactive "e")
2810   (set-buffer (window-buffer (posn-window (event-start event))))
2811   (let* ((pos (posn-point (event-start event)))
2812          (data (get-text-property pos 'gnus-data))
2813          (fun (get-text-property pos 'gnus-callback)))
2814     (when fun
2815       (funcall fun data))))
2816
2817 (defun gnus-article-press-button ()
2818   "Check text at point for a callback function.
2819 If the text at point has a `gnus-callback' property,
2820 call it with the value of the `gnus-data' text property."
2821   (interactive)
2822   (let* ((data (get-text-property (point) 'gnus-data))
2823          (fun (get-text-property (point) 'gnus-callback)))
2824     (when fun
2825       (funcall fun data))))
2826
2827 (defun gnus-article-prev-button (n)
2828   "Move point to N buttons backward.
2829 If N is negative, move forward instead."
2830   (interactive "p")
2831   (gnus-article-next-button (- n)))
2832
2833 (defun gnus-article-next-button (n)
2834   "Move point to N buttons forward.
2835 If N is negative, move backward instead."
2836   (interactive "p")
2837   (let ((function (if (< n 0) 'previous-single-property-change
2838                     'next-single-property-change))
2839         (inhibit-point-motion-hooks t)
2840         (backward (< n 0))
2841         (limit (if (< n 0) (point-min) (point-max))))
2842     (setq n (abs n))
2843     (while (and (not (= limit (point)))
2844                 (> n 0))
2845       ;; Skip past the current button.
2846       (when (get-text-property (point) 'gnus-callback)
2847         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2848       ;; Go to the next (or previous) button.
2849       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
2850       ;; Put point at the start of the button.
2851       (when (and backward (not (get-text-property (point) 'gnus-callback)))
2852         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2853       ;; Skip past intangible buttons.
2854       (when (get-text-property (point) 'intangible)
2855         (incf n))
2856       (decf n))
2857     (unless (zerop n)
2858       (gnus-message 5 "No more buttons"))
2859     n))
2860
2861 (defun gnus-article-highlight (&optional force)
2862   "Highlight current article.
2863 This function calls `gnus-article-highlight-headers',
2864 `gnus-article-highlight-citation',
2865 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2866 do the highlighting.  See the documentation for those functions."
2867   (interactive (list 'force))
2868   (gnus-article-highlight-headers)
2869   (gnus-article-highlight-citation force)
2870   (gnus-article-highlight-signature)
2871   (gnus-article-add-buttons force)
2872   (gnus-article-add-buttons-to-head))
2873
2874 (defun gnus-article-highlight-some (&optional force)
2875   "Highlight current article.
2876 This function calls `gnus-article-highlight-headers',
2877 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2878 do the highlighting.  See the documentation for those functions."
2879   (interactive (list 'force))
2880   (gnus-article-highlight-headers)
2881   (gnus-article-highlight-signature)
2882   (gnus-article-add-buttons))
2883
2884 (defun gnus-article-highlight-headers ()
2885   "Highlight article headers as specified by `gnus-header-face-alist'."
2886   (interactive)
2887   (save-excursion
2888     (set-buffer gnus-article-buffer)
2889     (save-restriction
2890       (let ((alist gnus-header-face-alist)
2891             (buffer-read-only nil)
2892             (case-fold-search t)
2893             (inhibit-point-motion-hooks t)
2894             entry regexp header-face field-face from hpoints fpoints)
2895         (message-narrow-to-head)
2896         (while (setq entry (pop alist))
2897           (goto-char (point-min))
2898           (setq regexp (concat "^\\("
2899                                (if (string-equal "" (nth 0 entry))
2900                                    "[^\t ]"
2901                                  (nth 0 entry))
2902                                "\\)")
2903                 header-face (nth 1 entry)
2904                 field-face (nth 2 entry))
2905           (while (and (re-search-forward regexp nil t)
2906                       (not (eobp)))
2907             (beginning-of-line)
2908             (setq from (point))
2909             (unless (search-forward ":" nil t)
2910               (forward-char 1))
2911             (when (and header-face
2912                        (not (memq (point) hpoints)))
2913               (push (point) hpoints)
2914               (gnus-put-text-property from (point) 'face header-face))
2915             (when (and field-face
2916                        (not (memq (setq from (point)) fpoints)))
2917               (push from fpoints)
2918               (if (re-search-forward "^[^ \t]" nil t)
2919                   (forward-char -2)
2920                 (goto-char (point-max)))
2921               (gnus-put-text-property from (point) 'face field-face))))))))
2922
2923 (defun gnus-article-highlight-signature ()
2924   "Highlight the signature in an article.
2925 It does this by highlighting everything after
2926 `gnus-signature-separator' using `gnus-signature-face'."
2927   (interactive)
2928   (save-excursion
2929     (set-buffer gnus-article-buffer)
2930     (let ((buffer-read-only nil)
2931           (inhibit-point-motion-hooks t))
2932       (save-restriction
2933         (when (and gnus-signature-face
2934                    (gnus-article-narrow-to-signature))
2935           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
2936                             'face gnus-signature-face)
2937           (widen)
2938           (gnus-article-search-signature)
2939           (let ((start (match-beginning 0))
2940                 (end (set-marker (make-marker) (1+ (match-end 0)))))
2941             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
2942                                      end)))))))
2943
2944 (defun gnus-button-in-region-p (b e prop)
2945   "Say whether PROP exists in the region."
2946   (text-property-not-all b e prop nil))
2947
2948 (defun gnus-article-add-buttons (&optional force)
2949   "Find external references in the article and make buttons of them.
2950 \"External references\" are things like Message-IDs and URLs, as
2951 specified by `gnus-button-alist'."
2952   (interactive (list 'force))
2953   (save-excursion
2954     (set-buffer gnus-article-buffer)
2955     (let ((buffer-read-only nil)
2956           (inhibit-point-motion-hooks t)
2957           (case-fold-search t)
2958           (alist gnus-button-alist)
2959           beg entry regexp)
2960       ;; Remove all old markers.
2961       (let (marker entry)
2962         (while (setq marker (pop gnus-button-marker-list))
2963           (goto-char marker)
2964           (when (setq entry (gnus-button-entry))
2965             (put-text-property (match-beginning (nth 1 entry))
2966                                (match-end (nth 1 entry))
2967                                'gnus-callback nil))
2968           (set-marker marker nil)))
2969       ;; We skip the headers.
2970       (goto-char (point-min))
2971       (unless (search-forward "\n\n" nil t)
2972         (goto-char (point-max)))
2973       (setq beg (point))
2974       (while (setq entry (pop alist))
2975         (setq regexp (car entry))
2976         (goto-char beg)
2977         (while (re-search-forward regexp nil t)
2978           (let* ((start (and entry (match-beginning (nth 1 entry))))
2979                  (end (and entry (match-end (nth 1 entry))))
2980                  (from (match-beginning 0)))
2981             (when (and (or (eq t (nth 2 entry))
2982                            (eval (nth 2 entry)))
2983                        (not (gnus-button-in-region-p
2984                              start end 'gnus-callback)))
2985               ;; That optional form returned non-nil, so we add the
2986               ;; button.
2987               (gnus-article-add-button
2988                start end 'gnus-button-push
2989                (car (push (set-marker (make-marker) from)
2990                           gnus-button-marker-list))))))))))
2991
2992 ;; Add buttons to the head of an article.
2993 (defun gnus-article-add-buttons-to-head ()
2994   "Add buttons to the head of the article."
2995   (interactive)
2996   (save-excursion
2997     (set-buffer gnus-article-buffer)
2998     (let ((buffer-read-only nil)
2999           (inhibit-point-motion-hooks t)
3000           (case-fold-search t)
3001           (alist gnus-header-button-alist)
3002           entry beg end)
3003       (nnheader-narrow-to-headers)
3004       (while alist
3005         ;; Each alist entry.
3006         (setq entry (car alist)
3007               alist (cdr alist))
3008         (goto-char (point-min))
3009         (while (re-search-forward (car entry) nil t)
3010           ;; Each header matching the entry.
3011           (setq beg (match-beginning 0))
3012           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
3013                              (match-beginning 0))
3014                         (point-max)))
3015           (goto-char beg)
3016           (while (re-search-forward (nth 1 entry) end t)
3017             ;; Each match within a header.
3018             (let* ((entry (cdr entry))
3019                    (start (match-beginning (nth 1 entry)))
3020                    (end (match-end (nth 1 entry)))
3021                    (form (nth 2 entry)))
3022               (goto-char (match-end 0))
3023               (when (eval form)
3024                 (gnus-article-add-button
3025                  start end (nth 3 entry)
3026                  (buffer-substring (match-beginning (nth 4 entry))
3027                                    (match-end (nth 4 entry)))))))
3028           (goto-char end))))
3029     (widen)))
3030
3031 ;;; External functions:
3032
3033 (defun gnus-article-add-button (from to fun &optional data)
3034   "Create a button between FROM and TO with callback FUN and data DATA."
3035   (when gnus-article-button-face
3036     (gnus-overlay-put (gnus-make-overlay from to)
3037                       'face gnus-article-button-face))
3038   (gnus-add-text-properties
3039    from to
3040    (nconc (and gnus-article-mouse-face
3041                (list gnus-mouse-face-prop gnus-article-mouse-face))
3042           (list 'gnus-callback fun)
3043           (and data (list 'gnus-data data)))))
3044
3045 ;;; Internal functions:
3046
3047 (defun gnus-article-set-globals ()
3048   (save-excursion
3049     (set-buffer gnus-summary-buffer)
3050     (gnus-set-global-variables)))
3051
3052 (defun gnus-signature-toggle (end)
3053   (save-excursion
3054     (set-buffer gnus-article-buffer)
3055     (let ((buffer-read-only nil)
3056           (inhibit-point-motion-hooks t))
3057       (if (get-text-property end 'invisible)
3058           (gnus-article-unhide-text end (point-max))
3059         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
3060
3061 (defun gnus-button-entry ()
3062   ;; Return the first entry in `gnus-button-alist' matching this place.
3063   (let ((alist gnus-button-alist)
3064         (entry nil))
3065     (while alist
3066       (setq entry (pop alist))
3067       (if (looking-at (car entry))
3068           (setq alist nil)
3069         (setq entry nil)))
3070     entry))
3071
3072 (defun gnus-button-push (marker)
3073   ;; Push button starting at MARKER.
3074   (save-excursion
3075     (set-buffer gnus-article-buffer)
3076     (goto-char marker)
3077     (let* ((entry (gnus-button-entry))
3078            (inhibit-point-motion-hooks t)
3079            (fun (nth 3 entry))
3080            (args (mapcar (lambda (group)
3081                            (let ((string (match-string group)))
3082                              (gnus-set-text-properties
3083                               0 (length string) nil string)
3084                              string))
3085                          (nthcdr 4 entry))))
3086       (cond
3087        ((fboundp fun)
3088         (apply fun args))
3089        ((and (boundp fun)
3090              (fboundp (symbol-value fun)))
3091         (apply (symbol-value fun) args))
3092        (t
3093         (gnus-message 1 "You must define `%S' to use this button"
3094                       (cons fun args)))))))
3095
3096 (defun gnus-button-message-id (message-id)
3097   "Fetch MESSAGE-ID."
3098   (save-excursion
3099     (set-buffer gnus-summary-buffer)
3100     (gnus-summary-refer-article message-id)))
3101
3102 (defun gnus-button-fetch-group (address)
3103   "Fetch GROUP specified by ADDRESS."
3104   (if (not (string-match "[:/]" address))
3105       ;; This is just a simple group url.
3106       (gnus-group-read-ephemeral-group address gnus-select-method)
3107     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
3108                            address))
3109         (error "Can't parse %s" address)
3110       (gnus-group-read-ephemeral-group
3111        (match-string 4 address)
3112        `(nntp ,(match-string 1 address)
3113               (nntp-address ,(match-string 1 address))
3114               (nntp-port-number ,(if (match-end 3)
3115                                      (match-string 3 address)
3116                                    "nntp")))))))
3117
3118 (defun gnus-url-parse-query-string (query &optional downcase)
3119   (let (retval pairs cur key val)
3120     (setq pairs (gnus-split-string query "&"))
3121     (while pairs
3122       (setq cur (car pairs)
3123             pairs (cdr pairs))
3124       (if (not (string-match "=" cur))
3125           nil                           ; Grace
3126         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
3127               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
3128         (if downcase
3129             (setq key (downcase key)))
3130         (setq cur (assoc key retval))
3131         (if cur
3132             (setcdr cur (cons val (cdr cur)))
3133           (setq retval (cons (list key val) retval)))))
3134     retval))
3135
3136 (defun gnus-url-unhex (x)
3137   (if (> x ?9)
3138       (if (>= x ?a)
3139           (+ 10 (- x ?a))
3140         (+ 10 (- x ?A)))
3141     (- x ?0)))
3142
3143 (defun gnus-url-unhex-string (str &optional allow-newlines)
3144   "Remove %XXX embedded spaces, etc in a url.
3145 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
3146 decoding of carriage returns and line feeds in the string, which is normally
3147 forbidden in URL encoding."
3148   (setq str (or str ""))
3149   (let ((tmp "")
3150         (case-fold-search t))
3151     (while (string-match "%[0-9a-f][0-9a-f]" str)
3152       (let* ((start (match-beginning 0))
3153              (ch1 (gnus-url-unhex (elt str (+ start 1))))
3154              (code (+ (* 16 ch1)
3155                       (gnus-url-unhex (elt str (+ start 2))))))
3156         (setq tmp (concat
3157                    tmp (substring str 0 start)
3158                    (cond
3159                     (allow-newlines
3160                      (char-to-string code))
3161                     ((or (= code ?\n) (= code ?\r))
3162                      " ")
3163                     (t (char-to-string code))))
3164               str (substring str (match-end 0)))))
3165     (setq tmp (concat tmp str))
3166     tmp))
3167
3168 (defun gnus-url-mailto (url)
3169   ;; Send mail to someone
3170   (when (string-match "mailto:/*\\(.*\\)" url)
3171     (setq url (substring url (match-beginning 1) nil)))
3172   (let (to args source-url subject func)
3173     (if (string-match (regexp-quote "?") url)
3174         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
3175               args (gnus-url-parse-query-string
3176                     (substring url (match-end 0) nil) t))
3177       (setq to (gnus-url-unhex-string url)))
3178     (setq args (cons (list "to" to) args)
3179           subject (cdr-safe (assoc "subject" args)))
3180     (message-mail)
3181     (while args
3182       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
3183       (if (fboundp func)
3184           (funcall func)
3185         (message-position-on-field (caar args)))
3186       (insert (mapconcat 'identity (cdar args) ", "))
3187       (setq args (cdr args)))
3188     (if subject
3189         (message-goto-body)
3190       (message-goto-subject))))
3191
3192 (defun gnus-button-mailto (address)
3193   ;; Mail to ADDRESS.
3194   (set-buffer (gnus-copy-article-buffer))
3195   (message-reply address))
3196
3197 (defun gnus-button-reply (address)
3198   ;; Reply to ADDRESS.
3199   (message-reply address))
3200
3201 (defun gnus-button-url (address)
3202   "Browse ADDRESS."
3203   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
3204   (if (listp browse-url-browser-function)
3205       (browse-url address)
3206     (funcall browse-url-browser-function address)))
3207
3208 (defun gnus-button-embedded-url (address)
3209   "Browse ADDRESS."
3210   ;; In Emacs 20, `browse-url-browser-function' may be an alist.
3211   (if (listp browse-url-browser-function)
3212       (browse-url (gnus-strip-whitespace address))
3213     (funcall browse-url-browser-function (gnus-strip-whitespace address))))
3214
3215 ;;; Next/prev buttons in the article buffer.
3216
3217 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
3218 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
3219
3220 (defvar gnus-prev-page-map nil)
3221 (unless gnus-prev-page-map
3222   (setq gnus-prev-page-map (make-sparse-keymap))
3223   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
3224   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
3225
3226 (defun gnus-insert-prev-page-button ()
3227   (let ((buffer-read-only nil))
3228     (gnus-eval-format
3229      gnus-prev-page-line-format nil
3230      `(gnus-prev t local-map ,gnus-prev-page-map
3231                  gnus-callback gnus-article-button-prev-page
3232                  gnus-type annotation))))
3233
3234 (defvar gnus-next-page-map nil)
3235 (unless gnus-next-page-map
3236   (setq gnus-next-page-map (make-keymap))
3237   (suppress-keymap gnus-prev-page-map)
3238   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
3239   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
3240
3241 (defun gnus-button-next-page ()
3242   "Go to the next page."
3243   (interactive)
3244   (let ((win (selected-window)))
3245     (select-window (get-buffer-window gnus-article-buffer t))
3246     (gnus-article-next-page)
3247     (select-window win)))
3248
3249 (defun gnus-button-prev-page ()
3250   "Go to the prev page."
3251   (interactive)
3252   (let ((win (selected-window)))
3253     (select-window (get-buffer-window gnus-article-buffer t))
3254     (gnus-article-prev-page)
3255     (select-window win)))
3256
3257 (defun gnus-insert-next-page-button ()
3258   (let ((buffer-read-only nil))
3259     (gnus-eval-format gnus-next-page-line-format nil
3260                       `(gnus-next
3261                         t local-map ,gnus-next-page-map
3262                         gnus-callback gnus-article-button-next-page
3263                         gnus-type annotation))))
3264
3265 (defun gnus-article-button-next-page (arg)
3266   "Go to the next page."
3267   (interactive "P")
3268   (let ((win (selected-window)))
3269     (select-window (get-buffer-window gnus-article-buffer t))
3270     (gnus-article-next-page)
3271     (select-window win)))
3272
3273 (defun gnus-article-button-prev-page (arg)
3274   "Go to the prev page."
3275   (interactive "P")
3276   (let ((win (selected-window)))
3277     (select-window (get-buffer-window gnus-article-buffer t))
3278     (gnus-article-prev-page)
3279     (select-window win)))
3280
3281 (gnus-ems-redefine)
3282
3283 (provide 'gnus-art)
3284
3285 (run-hooks 'gnus-art-load-hook)
3286
3287 ;;; gnus-art.el ends here