253adb3842b04ac6cc7647a7ae96bb6a75aef3a0
[elisp/gnus.git-] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile
32   (require 'cl)
33   (require 'static)
34   (defvar tool-bar-map))
35
36 (require 'path-util)
37 (require 'gnus)
38 (require 'gnus-sum)
39 (require 'gnus-spec)
40 (require 'gnus-int)
41 (require 'gnus-win)
42 (require 'alist)
43 (require 'mime-view)
44 (when (static-if (featurep 'xemacs)
45           (console-on-window-system-p)
46         window-system)
47   (require 'mime-image))
48 (require 'wid-edit)
49
50 ;; Avoid byte-compile warnings.
51 (eval-when-compile
52   (require 'mm-bodies)
53   (require 'mail-parse)
54   (require 'mm-decode)
55   (require 'mm-view)
56   (require 'mm-uu))
57
58 (require 'message)
59
60 (autoload 'gnus-msg-mail "gnus-msg" nil t)
61 (autoload 'gnus-button-mailto "gnus-msg")
62 (autoload 'gnus-button-reply "gnus-msg" nil t)
63 (autoload 'ansi-color-apply-on-region "ansi-color")
64
65 (defgroup gnus-article nil
66   "Article display."
67   :link '(custom-manual "(gnus)The Article Buffer")
68   :group 'gnus)
69
70 (defgroup gnus-article-treat nil
71   "Treating article parts."
72   :link '(custom-manual "(gnus)Article Hiding")
73   :group 'gnus-article)
74
75 (defgroup gnus-article-hiding nil
76   "Hiding article parts."
77   :link '(custom-manual "(gnus)Article Hiding")
78   :group 'gnus-article)
79
80 (defgroup gnus-article-highlight nil
81   "Article highlighting."
82   :link '(custom-manual "(gnus)Article Highlighting")
83   :group 'gnus-article
84   :group 'gnus-visual)
85
86 (defgroup gnus-article-signature nil
87   "Article signatures."
88   :link '(custom-manual "(gnus)Article Signature")
89   :group 'gnus-article)
90
91 (defgroup gnus-article-headers nil
92   "Article headers."
93   :link '(custom-manual "(gnus)Hiding Headers")
94   :group 'gnus-article)
95
96 (defgroup gnus-article-washing nil
97   "Special commands on articles."
98   :link '(custom-manual "(gnus)Article Washing")
99   :group 'gnus-article)
100
101 (defgroup gnus-article-emphasis nil
102   "Fontisizing articles."
103   :link '(custom-manual "(gnus)Article Fontisizing")
104   :group 'gnus-article)
105
106 (defgroup gnus-article-saving nil
107   "Saving articles."
108   :link '(custom-manual "(gnus)Saving Articles")
109   :group 'gnus-article)
110
111 (defgroup gnus-article-mime nil
112   "Worshiping the MIME wonder."
113   :link '(custom-manual "(gnus)Using MIME")
114   :group 'gnus-article)
115
116 (defgroup gnus-article-buttons nil
117   "Pushable buttons in the article buffer."
118   :link '(custom-manual "(gnus)Article Buttons")
119   :group 'gnus-article)
120
121 (defgroup gnus-article-various nil
122   "Other article options."
123   :link '(custom-manual "(gnus)Misc Article")
124   :group 'gnus-article)
125
126 (defcustom gnus-ignored-headers
127   (mapcar
128    (lambda (header)
129      (concat "^" header ":"))
130    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
131      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
132      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
133      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
134      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
135      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
136      "X-Attribution" "X-Originating-IP" "Delivered-To"
137      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
138      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
139      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
140      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
141      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
142      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
143      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
144      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
145      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
146      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
147      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
148      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
149      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
150      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
151      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
152      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
153      "List-[A-Za-z]+" "X-Listprocessor-Version"
154      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
155      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
156      "X-Received" "Content-length" "X-precedence"
157      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
158      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
159      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
160      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
161      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
162      "X-Content-length" "X-Posting-Agent" "Original-Received"
163      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
164      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
165      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
166      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
167      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"))
168   "*All headers that start with this regexp will be hidden.
169 This variable can also be a list of regexps of headers to be ignored.
170 If `gnus-visible-headers' is non-nil, this variable will be ignored."
171   :type '(choice :custom-show nil
172                  regexp
173                  (repeat regexp))
174   :group 'gnus-article-hiding)
175
176 (defcustom gnus-visible-headers
177   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
178   "*All headers that do not match this regexp will be hidden.
179 This variable can also be a list of regexp of headers to remain visible.
180 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
181   :type '(repeat :value-to-internal (lambda (widget value)
182                                       (custom-split-regexp-maybe value))
183                  :match (lambda (widget value)
184                           (or (stringp value)
185                               (widget-editable-list-match widget value)))
186                  regexp)
187   :group 'gnus-article-hiding)
188
189 (defcustom gnus-sorted-header-list
190   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
191     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
192   "*This variable is a list of regular expressions.
193 If it is non-nil, headers that match the regular expressions will
194 be placed first in the article buffer in the sequence specified by
195 this list."
196   :type '(repeat regexp)
197   :group 'gnus-article-hiding)
198
199 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
200   "Headers that are only to be displayed if they have interesting data.
201 Possible values in this list are:
202
203   'empty       Headers with no content.
204   'newsgroups  Newsgroup identical to Gnus group.
205   'to-address  To identical to To-address.
206   'to-list     To identical to To-list.
207   'cc-list     CC identical to To-list.
208   'followup-to Followup-to identical to Newsgroups.
209   'reply-to    Reply-to identical to From.
210   'date        Date less than four days old.
211   'long-to     To and/or Cc longer than 1024 characters.
212   'many-to     Multiple To and/or Cc."
213   :type '(set (const :tag "Headers with no content." empty)
214               (const :tag "Newsgroups identical to Gnus group." newsgroups)
215               (const :tag "To identical to To-address." to-address)
216               (const :tag "To identical to To-list." to-list)
217               (const :tag "CC identical to To-list." cc-list)
218               (const :tag "Followup-to identical to Newsgroups." followup-to)
219               (const :tag "Reply-to identical to From." reply-to)
220               (const :tag "Date less than four days old." date)
221               (const :tag "To and/or Cc longer than 1024 characters." long-to)
222               (const :tag "Multiple To and/or Cc headers." many-to))
223   :group 'gnus-article-hiding)
224
225 (defcustom gnus-article-skip-boring nil
226   "Skip over text that is not worth reading.
227 By default, if you set this t, then Gnus will display citations and
228 signatures, but will never scroll down to show you a page consisting
229 only of boring text.  Boring text is controlled by
230 `gnus-article-boring-faces'."
231   :type 'boolean
232   :group 'gnus-article-hiding)
233
234 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
235   "Regexp matching signature separator.
236 This can also be a list of regexps.  In that case, it will be checked
237 from head to tail looking for a separator.  Searches will be done from
238 the end of the buffer."
239   :type '(repeat string)
240   :group 'gnus-article-signature)
241
242 (defcustom gnus-signature-limit nil
243   "Provide a limit to what is considered a signature.
244 If it is a number, no signature may not be longer (in characters) than
245 that number.  If it is a floating point number, no signature may be
246 longer (in lines) than that number.  If it is a function, the function
247 will be called without any parameters, and if it returns nil, there is
248 no signature in the buffer.  If it is a string, it will be used as a
249 regexp.  If it matches, the text in question is not a signature."
250   :type '(choice (integer :value 200)
251                  (number :value 4.0)
252                  (function :value fun)
253                  (regexp :value ".*"))
254   :group 'gnus-article-signature)
255
256 (defcustom gnus-hidden-properties '(invisible t intangible t)
257   "Property list to use for hiding text."
258   :type 'sexp
259   :group 'gnus-article-hiding)
260
261 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
262 ;; frames in a session.
263 (defcustom gnus-article-x-face-command
264   (cond
265    (noninteractive
266     'ignore)
267    ((featurep 'xemacs)
268     (if (or (gnus-image-type-available-p 'xface)
269             (gnus-image-type-available-p 'pbm))
270         'gnus-display-x-face-in-from
271       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -"))
272    ((and (fboundp 'image-type-available-p)
273          (module-installed-p 'x-face-e21))
274     'x-face-decode-message-header)
275    ((gnus-image-type-available-p 'pbm)
276     'gnus-display-x-face-in-from)
277    ((and window-system
278          (module-installed-p 'x-face-mule))
279     'x-face-mule-gnus-article-display-x-face)
280    (t
281     "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
282 display -"))
283   "*String or function to be executed to display an X-Face header.
284 If it is a string, the command will be executed in a sub-shell
285 asynchronously.  The compressed face will be piped to this command."
286   :type `(choice
287           ,@(let (x-face-e21 x-face-mule)
288               (if (featurep 'xemacs)
289                   nil
290                 (setq x-face-e21 (module-installed-p 'x-face-e21)
291                       x-face-mule (module-installed-p 'x-face-mule)))
292               (delq nil
293                     (list
294                      'string
295                      (if (or (gnus-image-type-available-p 'xface)
296                              (gnus-image-type-available-p 'pbm))
297                          '(function-item gnus-display-x-face-in-from))
298                      (if (and x-face-e21
299                               (fboundp 'image-type-available-p))
300                          '(function-item
301                            :tag "x-face-decode-message-header (x-face-e21)"
302                            x-face-decode-message-header))
303                      (if x-face-mule
304                          '(function-item
305                            x-face-mule-gnus-article-display-x-face))
306                      'function))))
307   ;;:version "21.1"
308   :group 'gnus-picon
309   :group 'gnus-article-washing)
310
311 (defcustom gnus-article-x-face-too-ugly nil
312   "Regexp matching posters whose face shouldn't be shown automatically."
313   :type '(choice regexp (const nil))
314   :group 'gnus-article-washing)
315
316 (defcustom gnus-article-banner-alist nil
317   "Banner alist for stripping.
318 For example,
319      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
320   :version "21.1"
321   :type '(repeat (cons symbol regexp))
322   :group 'gnus-article-washing)
323
324 (gnus-define-group-parameter
325  banner
326  :variable-document
327  "Alist of regexps (to match group names) and banner."
328  :variable-group gnus-article-washing
329  :parameter-type
330  '(choice :tag "Banner"
331           :value nil
332           (const :tag "Remove signature" signature)
333           (symbol :tag "Item in `gnus-article-banner-alist'" none)
334           regexp
335           (const :tag "None" nil))
336  :parameter-document
337  "If non-nil, specify how to remove `banners' from articles.
338
339 Symbol `signature' means to remove signatures delimited by
340 `gnus-signature-separator'.  Any other symbol is used to look up a
341 regular expression to match the banner in `gnus-article-banner-alist'.
342 A string is used as a regular expression to match the banner
343 directly.")
344
345 (defcustom gnus-article-address-banner-alist nil
346   "Alist of mail addresses and banners.
347 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
348 to match a mail address in the From: header, BANNER is one of a symbol
349 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
350 If ADDRESS matches author's mail address, it will remove things like
351 advertisements.  For example:
352
353 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
354 "
355   :type '(repeat
356           (cons
357            (regexp :tag "Address")
358            (choice :tag "Banner" :value nil
359                    (const :tag "Remove signature" signature)
360                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
361                    regexp
362                    (const :tag "None" nil))))
363   :group 'gnus-article-washing)
364
365 (defcustom gnus-emphasis-alist
366   (let ((format
367          "\\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)")
368         (types
369          '(("\\*" "\\*" bold)
370            ("_" "_" underline)
371            ("/" "/" italic)
372            ("_/" "/_" underline-italic)
373            ("_\\*" "\\*_" underline-bold)
374            ("\\*/" "/\\*" bold-italic)
375            ("_\\*/" "/\\*_" underline-bold-italic))))
376     `(,@(mapcar
377          (lambda (spec)
378            (list
379             (format format (car spec) (car (cdr spec)))
380             2 3 (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
381          types)
382         ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
383          2 3 gnus-emphasis-strikethru)
384         ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
385          2 3 gnus-emphasis-underline)))
386   "*Alist that says how to fontify certain phrases.
387 Each item looks like this:
388
389   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
390
391 The first element is a regular expression to be matched.  The second
392 is a number that says what regular expression grouping used to find
393 the entire emphasized word.  The third is a number that says what
394 regexp grouping should be displayed and highlighted.  The fourth
395 is the face used for highlighting."
396   :type '(repeat (list :value ("" 0 0 default)
397                        regexp
398                        (integer :tag "Match group")
399                        (integer :tag "Emphasize group")
400                        face))
401   :group 'gnus-article-emphasis)
402
403 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
404   "A regexp to describe whitespace which should not be emphasized.
405 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
406 The former avoids underlining of leading and trailing whitespace,
407 and the latter avoids underlining any whitespace at all."
408   :version "21.1"
409   :group 'gnus-article-emphasis
410   :type 'regexp)
411
412 (defface gnus-emphasis-bold '((t (:bold t)))
413   "Face used for displaying strong emphasized text (*word*)."
414   :group 'gnus-article-emphasis)
415
416 (defface gnus-emphasis-italic '((t (:italic t)))
417   "Face used for displaying italic emphasized text (/word/)."
418   :group 'gnus-article-emphasis)
419
420 (defface gnus-emphasis-underline '((t (:underline t)))
421   "Face used for displaying underlined emphasized text (_word_)."
422   :group 'gnus-article-emphasis)
423
424 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
425   "Face used for displaying underlined bold emphasized text (_*word*_)."
426   :group 'gnus-article-emphasis)
427
428 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
429   "Face used for displaying underlined italic emphasized text (_/word/_)."
430   :group 'gnus-article-emphasis)
431
432 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
433   "Face used for displaying bold italic emphasized text (/*word*/)."
434   :group 'gnus-article-emphasis)
435
436 (defface gnus-emphasis-underline-bold-italic
437   '((t (:bold t :italic t :underline t)))
438   "Face used for displaying underlined bold italic emphasized text.
439 Example: (_/*word*/_)."
440   :group 'gnus-article-emphasis)
441
442 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
443                                       '((t (:strikethru t)))
444                                     '((t (:strike-through t))))
445   "Face used for displaying strike-through text (-word-)."
446   :group 'gnus-article-emphasis)
447
448 (defface gnus-emphasis-highlight-words
449   '((t (:background "black" :foreground "yellow")))
450   "Face used for displaying highlighted words."
451   :group 'gnus-article-emphasis)
452
453 (defcustom gnus-article-time-format "%a, %b %d %Y %T %Z"
454   "Format for display of Date headers in article bodies.
455 See `format-time-string' for the possible values.
456
457 The variable can also be function, which should return a complete Date
458 header.  The function is called with one argument, the time, which can
459 be fed to `format-time-string'."
460   :type '(choice string symbol)
461   :link '(custom-manual "(gnus)Article Date")
462   :group 'gnus-article-washing)
463
464 (eval-and-compile
465   (autoload 'mail-extract-address-components "mail-extr"))
466
467 (defcustom gnus-save-all-headers t
468   "*If non-nil, don't remove any headers before saving."
469   :group 'gnus-article-saving
470   :type 'boolean)
471
472 (defcustom gnus-prompt-before-saving 'always
473   "*This variable says how much prompting is to be done when saving articles.
474 If it is nil, no prompting will be done, and the articles will be
475 saved to the default files.  If this variable is `always', each and
476 every article that is saved will be preceded by a prompt, even when
477 saving large batches of articles.  If this variable is neither nil not
478 `always', there the user will be prompted once for a file name for
479 each invocation of the saving commands."
480   :group 'gnus-article-saving
481   :type '(choice (item always)
482                  (item :tag "never" nil)
483                  (sexp :tag "once" :format "%t\n" :value t)))
484
485 (defcustom gnus-saved-headers gnus-visible-headers
486   "Headers to keep if `gnus-save-all-headers' is nil.
487 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
488 If that variable is nil, however, all headers that match this regexp
489 will be kept while the rest will be deleted before saving."
490   :group 'gnus-article-saving
491   :type 'regexp)
492
493 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
494   "A function to save articles in your favourite format.
495 The function must be interactively callable (in other words, it must
496 be an Emacs command).
497
498 Gnus provides the following functions:
499
500 * gnus-summary-save-in-rmail (Rmail format)
501 * gnus-summary-save-in-mail (Unix mail format)
502 * gnus-summary-save-in-folder (MH folder)
503 * gnus-summary-save-in-file (article format)
504 * gnus-summary-save-body-in-file (article body)
505 * gnus-summary-save-in-vm (use VM's folder format)
506 * gnus-summary-write-to-file (article format -- overwrite)."
507   :group 'gnus-article-saving
508   :type '(radio (function-item gnus-summary-save-in-rmail)
509                 (function-item gnus-summary-save-in-mail)
510                 (function-item gnus-summary-save-in-folder)
511                 (function-item gnus-summary-save-in-file)
512                 (function-item gnus-summary-save-body-in-file)
513                 (function-item gnus-summary-save-in-vm)
514                 (function-item gnus-summary-write-to-file)))
515
516 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
517   "A function generating a file name to save articles in Rmail format.
518 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
519   :group 'gnus-article-saving
520   :type 'function)
521
522 (defcustom gnus-mail-save-name 'gnus-plain-save-name
523   "A function generating a file name to save articles in Unix mail format.
524 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
525   :group 'gnus-article-saving
526   :type 'function)
527
528 (defcustom gnus-folder-save-name 'gnus-folder-save-name
529   "A function generating a file name to save articles in MH folder.
530 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
531   :group 'gnus-article-saving
532   :type 'function)
533
534 (defcustom gnus-file-save-name 'gnus-numeric-save-name
535   "A function generating a file name to save articles in article format.
536 The function is called with NEWSGROUP, HEADERS, and optional
537 LAST-FILE."
538   :group 'gnus-article-saving
539   :type 'function)
540
541 (defcustom gnus-split-methods
542   '((gnus-article-archive-name)
543     (gnus-article-nndoc-name))
544   "*Variable used to suggest where articles are to be saved.
545 For instance, if you would like to save articles related to Gnus in
546 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
547 you could set this variable to something like:
548
549  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
550    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
551
552 This variable is an alist where the where the key is the match and the
553 value is a list of possible files to save in if the match is non-nil.
554
555 If the match is a string, it is used as a regexp match on the
556 article.  If the match is a symbol, that symbol will be funcalled
557 from the buffer of the article to be saved with the newsgroup as the
558 parameter.  If it is a list, it will be evaled in the same buffer.
559
560 If this form or function returns a string, this string will be used as
561 a possible file name; and if it returns a non-nil list, that list will
562 be used as possible file names."
563   :group 'gnus-article-saving
564   :type '(repeat (choice (list :value (fun) function)
565                          (cons :value ("" "") regexp (repeat string))
566                          (sexp :value nil))))
567
568 (defcustom gnus-article-display-method-for-mime
569   'gnus-article-display-mime-message
570   "Function to display a MIME message.
571 The function is called from the article buffer."
572   :group 'gnus-article-mime
573   :type 'function)
574
575 (defcustom gnus-article-display-method-for-traditional
576   'gnus-article-display-traditional-message
577   "*Function to display a traditional message.
578 The function is called from the article buffer."
579   :group 'gnus-article-mime
580   :type 'function)
581
582 (defcustom gnus-page-delimiter "^\^L"
583   "*Regexp describing what to use as article page delimiters.
584 The default value is \"^\^L\", which is a form linefeed at the
585 beginning of a line."
586   :type 'regexp
587   :group 'gnus-article-various)
588
589 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
590   "*The format specification for the article mode line.
591 See `gnus-summary-mode-line-format' for a closer description.
592
593 The following additional specs are available:
594
595 %w  The article washing status.
596 %m  The number of MIME parts in the article."
597   :type 'string
598   :group 'gnus-article-various)
599
600 (defcustom gnus-article-mode-hook nil
601   "*A hook for Gnus article mode."
602   :type 'hook
603   :group 'gnus-article-various)
604
605 (when (featurep 'xemacs)
606   ;; Extracted from gnus-xmas-define in order to preserve user settings
607   (when (fboundp 'turn-off-scroll-in-place)
608     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
609   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
610   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
611
612 (defcustom gnus-article-menu-hook nil
613   "*Hook run after the creation of the article mode menu."
614   :type 'hook
615   :group 'gnus-article-various)
616
617 (defcustom gnus-article-prepare-hook nil
618   "*A hook called after an article has been prepared in the article buffer."
619   :type 'hook
620   :group 'gnus-article-various)
621
622 (make-obsolete-variable 'gnus-article-hide-pgp-hook
623                         "This variable is obsolete in Gnus 5.10.")
624
625 (defcustom gnus-article-button-face 'bold
626   "Face used for highlighting buttons in the article buffer.
627
628 An article button is a piece of text that you can activate by pressing
629 `RET' or `mouse-2' above it."
630   :type 'face
631   :group 'gnus-article-buttons)
632
633 (defcustom gnus-article-mouse-face 'highlight
634   "Face used for mouse highlighting in the article buffer.
635
636 Article buttons will be displayed in this face when the cursor is
637 above them."
638   :type 'face
639   :group 'gnus-article-buttons)
640
641 (defcustom gnus-signature-face 'gnus-signature-face
642   "Face used for highlighting a signature in the article buffer.
643 Obsolete; use the face `gnus-signature-face' for customizations instead."
644   :type 'face
645   :group 'gnus-article-highlight
646   :group 'gnus-article-signature)
647
648 (defface gnus-signature-face
649   '((t
650      (:italic t)))
651   "Face used for highlighting a signature in the article buffer."
652   :group 'gnus-article-highlight
653   :group 'gnus-article-signature)
654
655 (defface gnus-header-from-face
656   '((((class color)
657       (background dark))
658      (:foreground "spring green"))
659     (((class color)
660       (background light))
661      (:foreground "red3"))
662     (t
663      (:italic t)))
664   "Face used for displaying from headers."
665   :group 'gnus-article-headers
666   :group 'gnus-article-highlight)
667
668 (defface gnus-header-subject-face
669   '((((class color)
670       (background dark))
671      (:foreground "SeaGreen3"))
672     (((class color)
673       (background light))
674      (:foreground "red4"))
675     (t
676      (:bold t :italic t)))
677   "Face used for displaying subject headers."
678   :group 'gnus-article-headers
679   :group 'gnus-article-highlight)
680
681 (defface gnus-header-newsgroups-face
682   '((((class color)
683       (background dark))
684      (:foreground "yellow" :italic t))
685     (((class color)
686       (background light))
687      (:foreground "MidnightBlue" :italic t))
688     (t
689      (:italic t)))
690   "Face used for displaying newsgroups headers.
691 In the default setup this face is only used for crossposted
692 articles."
693   :group 'gnus-article-headers
694   :group 'gnus-article-highlight)
695
696 (defface gnus-header-name-face
697   '((((class color)
698       (background dark))
699      (:foreground "SeaGreen"))
700     (((class color)
701       (background light))
702      (:foreground "maroon"))
703     (t
704      (:bold t)))
705   "Face used for displaying header names."
706   :group 'gnus-article-headers
707   :group 'gnus-article-highlight)
708
709 (defface gnus-header-content-face
710   '((((class color)
711       (background dark))
712      (:foreground "forest green" :italic t))
713     (((class color)
714       (background light))
715      (:foreground "indianred4" :italic t))
716     (t
717      (:italic t)))
718   "Face used for displaying header content."
719   :group 'gnus-article-headers
720   :group 'gnus-article-highlight)
721
722 (defcustom gnus-header-face-alist
723   '(("From" nil gnus-header-from-face)
724     ("Subject" nil gnus-header-subject-face)
725     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
726     ("" gnus-header-name-face gnus-header-content-face))
727   "*Controls highlighting of article headers.
728
729 An alist of the form (HEADER NAME CONTENT).
730
731 HEADER is a regular expression which should match the name of a
732 header and NAME and CONTENT are either face names or nil.
733
734 The name of each header field will be displayed using the face
735 specified by the first element in the list where HEADER matches
736 the header name and NAME is non-nil.  Similarly, the content will
737 be displayed by the first non-nil matching CONTENT face."
738   :group 'gnus-article-headers
739   :group 'gnus-article-highlight
740   :type '(repeat (list (regexp :tag "Header")
741                        (choice :tag "Name"
742                                (item :tag "skip" nil)
743                                (face :value default))
744                        (choice :tag "Content"
745                                (item :tag "skip" nil)
746                                (face :value default)))))
747
748 (defcustom gnus-article-decode-hook
749   '(article-decode-group-name article-decode-idna-rhs)
750   "*Hook run to decode charsets in articles."
751   :group 'gnus-article-headers
752   :type 'hook)
753
754 (defcustom gnus-display-mime-function 'gnus-display-mime
755   "Function to display MIME articles."
756   :group 'gnus-article-mime
757   :type 'function)
758
759 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
760   "Function used to decode headers.")
761
762 (defvar gnus-article-dumbquotes-map
763   '(("\200" "EUR")
764     ("\202" ",")
765     ("\203" "f")
766     ("\204" ",,")
767     ("\205" "...")
768     ("\213" "<")
769     ("\214" "OE")
770     ("\221" "`")
771     ("\222" "'")
772     ("\223" "``")
773     ("\224" "\"")
774     ("\225" "*")
775     ("\226" "-")
776     ("\227" "--")
777     ("\230" "~")
778     ("\231" "(TM)")
779     ("\233" ">")
780     ("\234" "oe")
781     ("\264" "'"))
782   "Table for MS-to-Latin1 translation.")
783
784 (defcustom gnus-ignored-mime-types nil
785   "List of MIME types that should be ignored by Gnus."
786   :version "21.1"
787   :group 'gnus-article-mime
788   :type '(repeat regexp))
789
790 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
791   "List of MIME types that should not be given buttons when rendered inline.
792 See also `gnus-buttonized-mime-types' which may override this variable.
793 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
794   :version "21.1"
795   :group 'gnus-article-mime
796   :type '(repeat regexp))
797
798 (defcustom gnus-buttonized-mime-types nil
799   "List of MIME types that should be given buttons when rendered inline.
800 If set, this variable overrides `gnus-unbuttonized-mime-types'.
801 To see e.g. security buttons you could set this to
802 `(\"multipart/signed\")'.
803 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
804   :version "21.1"
805   :group 'gnus-article-mime
806   :type '(repeat regexp))
807
808 (defcustom gnus-inhibit-mime-unbuttonizing nil
809   "If non-nil, all MIME parts get buttons.
810 When nil (the default value), then some MIME parts do not get buttons,
811 as described by the variables `gnus-buttonized-mime-types' and
812 `gnus-unbuttonized-mime-types'."
813   :version "21.3"
814   :type 'boolean)
815
816 (defcustom gnus-body-boundary-delimiter "_"
817   "String used to delimit header and body.
818 This variable is used by `gnus-article-treat-body-boundary' which can
819 be controlled by `gnus-treat-body-boundary'."
820   :group 'gnus-article-various
821   :type '(choice (item :tag "None" :value nil)
822                  string))
823
824 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces")
825   "Defines the location of the faces database.
826 For information on obtaining this database of pretty pictures, please
827 see http://www.cs.indiana.edu/picons/ftp/index.html"
828   :type '(repeat directory)
829   :link '(url-link :tag "download"
830                    "http://www.cs.indiana.edu/picons/ftp/index.html")
831   :link '(custom-manual "(gnus)Picons")
832   :group 'gnus-picon)
833
834 (defun gnus-picons-installed-p ()
835   "Say whether picons are installed on your machine."
836   (let ((installed nil))
837     (dolist (database gnus-picon-databases)
838       (when (file-exists-p database)
839         (setq installed t)))
840     installed))
841
842 (defcustom gnus-article-mime-part-function nil
843   "Function called with a MIME handle as the argument.
844 This is meant for people who want to do something automatic based
845 on parts -- for instance, adding Vcard info to a database."
846   :group 'gnus-article-mime
847   :type 'function)
848
849 (defcustom gnus-mime-multipart-functions nil
850   "An alist of MIME types to functions to display them."
851   :version "21.1"
852   :group 'gnus-article-mime
853   :type 'alist)
854
855 (defcustom gnus-article-date-lapsed-new-header nil
856   "Whether the X-Sent and Date headers can coexist.
857 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
858 either replace the old \"Date:\" header (if this variable is nil), or
859 be added below it (otherwise)."
860   :version "21.1"
861   :group 'gnus-article-headers
862   :type 'boolean)
863
864 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
865   "Function called with a MIME handle as the argument.
866 This is meant for people who want to view first matched part.
867 For `undisplayed-alternative' (default), the first undisplayed
868 part or alternative part is used.  For `undisplayed', the first
869 undisplayed part is used.  For a function, the first part which
870 the function return t is used.  For nil, the first part is
871 used."
872   :version "21.1"
873   :group 'gnus-article-mime
874   :type '(choice
875           (item :tag "first" :value nil)
876           (item :tag "undisplayed" :value undisplayed)
877           (item :tag "undisplayed or alternative"
878                 :value undisplayed-alternative)
879           (function)))
880
881 (defcustom gnus-mime-action-alist
882   '(("save to file" . gnus-mime-save-part)
883     ("save and strip" . gnus-mime-save-part-and-strip)
884     ("delete part" . gnus-mime-delete-part)
885     ("display as text" . gnus-mime-inline-part)
886     ("view the part" . gnus-mime-view-part)
887     ("pipe to command" . gnus-mime-pipe-part)
888     ("toggle display" . gnus-article-press-button)
889     ("toggle display" . gnus-article-view-part-as-charset)
890     ("view as type" . gnus-mime-view-part-as-type)
891     ("view internally" . gnus-mime-view-part-internally)
892     ("view externally" . gnus-mime-view-part-externally))
893   "An alist of actions that run on the MIME attachment."
894   :group 'gnus-article-mime
895   :type '(repeat (cons (string :tag "name")
896                        (function))))
897
898 ;;;
899 ;;; The treatment variables
900 ;;;
901
902 (defvar gnus-part-display-hook nil
903   "Hook called on parts that are to receive treatment.")
904
905 (defvar gnus-article-treat-custom
906   '(choice (const :tag "Off" nil)
907            (const :tag "On" t)
908            (const :tag "Header" head)
909            (const :tag "Last" last)
910            (const :tag "Mime" mime)
911            (integer :tag "Less")
912            (repeat :tag "Groups" regexp)
913            (sexp :tag "Predicate")))
914
915 (defvar gnus-article-treat-head-custom
916   '(choice (const :tag "Off" nil)
917            (const :tag "Header" head)))
918
919 (defvar gnus-article-treat-types '("text/plain")
920   "Parts to treat.")
921
922 (defvar gnus-inhibit-treatment nil
923   "Whether to inhibit treatment.")
924
925 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
926   "Highlight the signature.
927 Valid values are nil, t, `head', `last', an integer or a predicate.
928 See Info node `(gnus)Customizing Articles'."
929   :group 'gnus-article-treat
930   :link '(custom-manual "(gnus)Customizing Articles")
931   :type gnus-article-treat-custom)
932 (put 'gnus-treat-highlight-signature 'highlight t)
933
934 (defcustom gnus-treat-buttonize 100000
935   "Add buttons.
936 Valid values are nil, t, `head', `last', an integer or a predicate.
937 See Info node `(gnus)Customizing Articles'."
938   :group 'gnus-article-treat
939   :link '(custom-manual "(gnus)Customizing Articles")
940   :type gnus-article-treat-custom)
941 (put 'gnus-treat-buttonize 'highlight t)
942
943 (defcustom gnus-treat-buttonize-head 'head
944   "Add buttons to the head.
945 Valid values are nil, t, `head', `last', an integer or a predicate.
946 See Info node `(gnus)Customizing Articles' for details."
947   :group 'gnus-article-treat
948   :link '(custom-manual "(gnus)Customizing Articles")
949   :type gnus-article-treat-head-custom)
950 (put 'gnus-treat-buttonize-head 'highlight t)
951
952 (defcustom gnus-treat-emphasize
953   (and (or window-system
954            (featurep 'xemacs)
955            (>= (string-to-number emacs-version) 21))
956        50000)
957   "Emphasize text.
958 Valid values are nil, t, `head', `last', an integer or a predicate.
959 See Info node `(gnus)Customizing Articles' for details."
960   :group 'gnus-article-treat
961   :link '(custom-manual "(gnus)Customizing Articles")
962   :type gnus-article-treat-custom)
963 (put 'gnus-treat-emphasize 'highlight t)
964
965 (defcustom gnus-treat-strip-cr nil
966   "Remove carriage returns.
967 Valid values are nil, t, `head', `last', an integer or a predicate.
968 See Info node `(gnus)Customizing Articles' for details."
969   :group 'gnus-article-treat
970   :link '(custom-manual "(gnus)Customizing Articles")
971   :type gnus-article-treat-custom)
972
973 (defcustom gnus-treat-unsplit-urls nil
974   "Remove newlines from within URLs.
975 Valid values are nil, t, `head', `last', an integer or a predicate.
976 See Info node `(gnus)Customizing Articles' for details."
977   :group 'gnus-article-treat
978   :link '(custom-manual "(gnus)Customizing Articles")
979   :type gnus-article-treat-custom)
980
981 (defcustom gnus-treat-leading-whitespace nil
982   "Remove leading whitespace in headers.
983 Valid values are nil, t, `head', `last', an integer or a predicate.
984 See Info node `(gnus)Customizing Articles' for details."
985   :group 'gnus-article-treat
986   :link '(custom-manual "(gnus)Customizing Articles")
987   :type gnus-article-treat-custom)
988
989 (defcustom gnus-treat-hide-headers 'head
990   "Hide headers.
991 Valid values are nil, t, `head', `last', an integer or a predicate.
992 See Info node `(gnus)Customizing Articles' for details."
993   :group 'gnus-article-treat
994   :link '(custom-manual "(gnus)Customizing Articles")
995   :type gnus-article-treat-head-custom)
996
997 (defcustom gnus-treat-hide-boring-headers nil
998   "Hide boring headers.
999 Valid values are nil, t, `head', `last', an integer or a predicate.
1000 See Info node `(gnus)Customizing Articles' for details."
1001   :group 'gnus-article-treat
1002   :link '(custom-manual "(gnus)Customizing Articles")
1003   :type gnus-article-treat-head-custom)
1004
1005 (defcustom gnus-treat-hide-signature nil
1006   "Hide the signature.
1007 Valid values are nil, t, `head', `last', an integer or a predicate.
1008 See Info node `(gnus)Customizing Articles' for details."
1009   :group 'gnus-article-treat
1010   :link '(custom-manual "(gnus)Customizing Articles")
1011   :type gnus-article-treat-custom)
1012
1013 (defcustom gnus-treat-fill-article nil
1014   "Fill the article.
1015 Valid values are nil, t, `head', `last', an integer or a predicate.
1016 See Info node `(gnus)Customizing Articles' for details."
1017   :group 'gnus-article-treat
1018   :link '(custom-manual "(gnus)Customizing Articles")
1019   :type gnus-article-treat-custom)
1020
1021 (defcustom gnus-treat-hide-citation nil
1022   "Hide cited text.
1023 Valid values are nil, t, `head', `last', an integer or a predicate.
1024 See Info node `(gnus)Customizing Articles' for details."
1025   :group 'gnus-article-treat
1026   :link '(custom-manual "(gnus)Customizing Articles")
1027   :type gnus-article-treat-custom)
1028
1029 (defcustom gnus-treat-hide-citation-maybe nil
1030   "Hide cited text.
1031 Valid values are nil, t, `head', `last', an integer or a predicate.
1032 See Info node `(gnus)Customizing Articles' for details."
1033   :group 'gnus-article-treat
1034   :link '(custom-manual "(gnus)Customizing Articles")
1035   :type gnus-article-treat-custom)
1036
1037 (defcustom gnus-treat-strip-list-identifiers 'head
1038   "Strip list identifiers from `gnus-list-identifiers`.
1039 Valid values are nil, t, `head', `last', an integer or a predicate.
1040 See Info node `(gnus)Customizing Articles' for details."
1041   :version "21.1"
1042   :group 'gnus-article-treat
1043   :link '(custom-manual "(gnus)Customizing Articles")
1044   :type gnus-article-treat-custom)
1045
1046 (make-obsolete-variable 'gnus-treat-strip-pgp
1047                         "This option is obsolete in Gnus 5.10.")
1048
1049 (defcustom gnus-treat-strip-pem nil
1050   "Strip PEM signatures.
1051 Valid values are nil, t, `head', `last', an integer or a predicate.
1052 See Info node `(gnus)Customizing Articles' for details."
1053   :group 'gnus-article-treat
1054   :link '(custom-manual "(gnus)Customizing Articles")
1055   :type gnus-article-treat-custom)
1056
1057 (defcustom gnus-treat-strip-banner t
1058   "Strip banners from articles.
1059 The banner to be stripped is specified in the `banner' group parameter.
1060 Valid values are nil, t, `head', `last', an integer or a predicate.
1061 See Info node `(gnus)Customizing Articles' for details."
1062   :group 'gnus-article-treat
1063   :link '(custom-manual "(gnus)Customizing Articles")
1064   :type gnus-article-treat-custom)
1065
1066 (defcustom gnus-treat-highlight-headers 'head
1067   "Highlight the headers.
1068 Valid values are nil, t, `head', `last', an integer or a predicate.
1069 See Info node `(gnus)Customizing Articles' for details."
1070   :group 'gnus-article-treat
1071   :link '(custom-manual "(gnus)Customizing Articles")
1072   :type gnus-article-treat-head-custom)
1073 (put 'gnus-treat-highlight-headers 'highlight t)
1074
1075 (defcustom gnus-treat-highlight-citation t
1076   "Highlight cited text.
1077 Valid values are nil, t, `head', `last', an integer or a predicate.
1078 See Info node `(gnus)Customizing Articles' for details."
1079   :group 'gnus-article-treat
1080   :link '(custom-manual "(gnus)Customizing Articles")
1081   :type gnus-article-treat-custom)
1082 (put 'gnus-treat-highlight-citation 'highlight t)
1083
1084 (defcustom gnus-treat-date-ut nil
1085   "Display the Date in UT (GMT).
1086 Valid values are nil, t, `head', `last', an integer or a predicate.
1087 See Info node `(gnus)Customizing Articles' for details."
1088   :group 'gnus-article-treat
1089   :link '(custom-manual "(gnus)Customizing Articles")
1090   :type gnus-article-treat-head-custom)
1091
1092 (defcustom gnus-treat-date-local nil
1093   "Display the Date in the local timezone.
1094 Valid values are nil, t, `head', `last', an integer or a predicate.
1095 See Info node `(gnus)Customizing Articles' for details."
1096   :group 'gnus-article-treat
1097   :link '(custom-manual "(gnus)Customizing Articles")
1098   :type gnus-article-treat-head-custom)
1099
1100 (defcustom gnus-treat-date-english nil
1101   "Display the Date in a format that can be read aloud in English.
1102 Valid values are nil, t, `head', `last', an integer or a predicate.
1103 See Info node `(gnus)Customizing Articles' for details."
1104   :group 'gnus-article-treat
1105   :link '(custom-manual "(gnus)Customizing Articles")
1106   :type gnus-article-treat-head-custom)
1107
1108 (defcustom gnus-treat-date-lapsed nil
1109   "Display the Date header in a way that says how much time has elapsed.
1110 Valid values are nil, t, `head', `last', an integer or a predicate.
1111 See Info node `(gnus)Customizing Articles' for details."
1112   :group 'gnus-article-treat
1113   :link '(custom-manual "(gnus)Customizing Articles")
1114   :type gnus-article-treat-head-custom)
1115
1116 (defcustom gnus-treat-date-original nil
1117   "Display the date in the original timezone.
1118 Valid values are nil, t, `head', `last', an integer or a predicate.
1119 See Info node `(gnus)Customizing Articles' for details."
1120   :group 'gnus-article-treat
1121   :link '(custom-manual "(gnus)Customizing Articles")
1122   :type gnus-article-treat-head-custom)
1123
1124 (defcustom gnus-treat-date-iso8601 nil
1125   "Display the date in the ISO8601 format.
1126 Valid values are nil, t, `head', `last', an integer or a predicate.
1127 See Info node `(gnus)Customizing Articles' for details."
1128   :version "21.1"
1129   :group 'gnus-article-treat
1130   :link '(custom-manual "(gnus)Customizing Articles")
1131   :type gnus-article-treat-head-custom)
1132
1133 (defcustom gnus-treat-date-user-defined nil
1134   "Display the date in a user-defined format.
1135 The format is defined by the `gnus-article-time-format' variable.
1136 Valid values are nil, t, `head', `last', an integer or a predicate.
1137 See Info node `(gnus)Customizing Articles' for details."
1138   :group 'gnus-article-treat
1139   :link '(custom-manual "(gnus)Customizing Articles")
1140   :type gnus-article-treat-head-custom)
1141
1142 (defcustom gnus-treat-strip-headers-in-body t
1143   "Strip the X-No-Archive header line from the beginning of the body.
1144 Valid values are nil, t, `head', `last', an integer or a predicate.
1145 See Info node `(gnus)Customizing Articles' for details."
1146   :version "21.1"
1147   :group 'gnus-article-treat
1148   :link '(custom-manual "(gnus)Customizing Articles")
1149   :type gnus-article-treat-custom)
1150
1151 (defcustom gnus-treat-strip-trailing-blank-lines nil
1152   "Strip trailing blank lines.
1153 Valid values are nil, t, `head', `last', an integer or a predicate.
1154 See Info node `(gnus)Customizing Articles' for details."
1155   :group 'gnus-article-treat
1156   :link '(custom-manual "(gnus)Customizing Articles")
1157   :type gnus-article-treat-custom)
1158
1159 (defcustom gnus-treat-strip-leading-blank-lines nil
1160   "Strip leading blank lines.
1161 Valid values are nil, t, `head', `last', an integer or a predicate.
1162 See Info node `(gnus)Customizing Articles' for details."
1163   :group 'gnus-article-treat
1164   :link '(custom-manual "(gnus)Customizing Articles")
1165   :type gnus-article-treat-custom)
1166
1167 (defcustom gnus-treat-strip-multiple-blank-lines nil
1168   "Strip multiple blank lines.
1169 Valid values are nil, t, `head', `last', an integer or a predicate.
1170 See Info node `(gnus)Customizing Articles' for details."
1171   :group 'gnus-article-treat
1172   :link '(custom-manual "(gnus)Customizing Articles")
1173   :type gnus-article-treat-custom)
1174
1175 (defcustom gnus-treat-unfold-headers 'head
1176   "Unfold folded header lines.
1177 Valid values are nil, t, `head', `last', an integer or a predicate.
1178 See Info node `(gnus)Customizing Articles' for details."
1179   :group 'gnus-article-treat
1180   :link '(custom-manual "(gnus)Customizing Articles")
1181   :type gnus-article-treat-custom)
1182
1183 (defcustom gnus-treat-fold-headers nil
1184   "Fold headers.
1185 Valid values are nil, t, `head', `last', an integer or a predicate.
1186 See Info node `(gnus)Customizing Articles' for details."
1187   :group 'gnus-article-treat
1188   :link '(custom-manual "(gnus)Customizing Articles")
1189   :type gnus-article-treat-custom)
1190
1191 (defcustom gnus-treat-fold-newsgroups 'head
1192   "Fold the Newsgroups and Followup-To headers.
1193 Valid values are nil, t, `head', `last', an integer or a predicate.
1194 See Info node `(gnus)Customizing Articles' for details."
1195   :group 'gnus-article-treat
1196   :link '(custom-manual "(gnus)Customizing Articles")
1197   :type gnus-article-treat-custom)
1198
1199 (defcustom gnus-treat-overstrike t
1200   "Treat overstrike highlighting.
1201 Valid values are nil, t, `head', `last', an integer or a predicate.
1202 See Info node `(gnus)Customizing Articles' for details."
1203   :group 'gnus-article-treat
1204   :link '(custom-manual "(gnus)Customizing Articles")
1205   :type gnus-article-treat-custom)
1206 (put 'gnus-treat-overstrike 'highlight t)
1207
1208 (defcustom gnus-treat-ansi-sequences t
1209   "Treat ANSI SGR control sequences.
1210 Valid values are nil, t, `head', `last', an integer or a predicate.
1211 See Info node `(gnus)Customizing Articles' for details."
1212   :group 'gnus-article-treat
1213   :link '(custom-manual "(gnus)Customizing Articles")
1214   :type gnus-article-treat-custom)
1215
1216 (make-obsolete-variable 'gnus-treat-display-xface
1217                         'gnus-treat-display-x-face)
1218
1219 (defcustom gnus-treat-display-x-face
1220   (and (not noninteractive)
1221        (or (memq gnus-article-x-face-command
1222                  '(x-face-decode-message-header
1223                    x-face-mule-gnus-article-display-x-face))
1224            (and (fboundp 'image-type-available-p)
1225                 (image-type-available-p 'xbm)
1226                 (string-match "^0x" (shell-command-to-string "uncompface"))
1227                 (executable-find "icontopbm"))
1228            (and (featurep 'xemacs)
1229                 (featurep 'xface)))
1230        'head)
1231   "Display X-Face headers.
1232 Valid values are nil, t, `head', `last', an integer or a predicate.
1233 See Info node `(gnus)Customizing Articles' and Info node
1234 `(gnus)X-Face' for details."
1235   :group 'gnus-article-treat
1236   ;;:version "21.1"
1237   :link '(custom-manual "(gnus)Customizing Articles")
1238   :link '(custom-manual "(gnus)X-Face")
1239   :type gnus-article-treat-head-custom
1240   :set (lambda (symbol value)
1241          (set-default
1242           symbol
1243           (cond ((or (boundp symbol) (get symbol 'saved-value))
1244                  value)
1245                 ((boundp 'gnus-treat-display-xface)
1246                  (message "\
1247 ** gnus-treat-display-xface is an obsolete variable;\
1248  use gnus-treat-display-x-face instead")
1249                  (default-value 'gnus-treat-display-xface))
1250                 ((get 'gnus-treat-display-xface 'saved-value)
1251                  (message "\
1252 ** gnus-treat-display-xface is an obsolete variable;\
1253  use gnus-treat-display-x-face instead")
1254                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1255                 (t
1256                  value)))))
1257 (put 'gnus-treat-display-x-face 'highlight t)
1258
1259 (defcustom gnus-article-should-use-smiley-mule
1260   (not (or (featurep 'xemacs)
1261            (gnus-image-type-available-p 'xpm)
1262            (gnus-image-type-available-p 'pbm)))
1263   "If non-nil, gnus uses `smiley-mule' for displaying smileys rather than
1264 `smiley'.  It defaults to t when Emacs 20 or earlier is running.
1265 `smiley-mule' is boundled in BITMAP-MULE package.  You can set it to t
1266 even if you are using Emacs 21+.  It has no effect on XEmacs."
1267   :group 'gnus-article-various
1268   :type 'boolean
1269   :get (lambda (symbol)
1270          (and (not noninteractive)
1271               (default-value symbol)
1272               (not (featurep 'xemacs))
1273               (module-installed-p 'smiley-mule)
1274               t))
1275   :set (lambda (symbol value)
1276          (set-default symbol (and (not noninteractive)
1277                                   value
1278                                   (not (featurep 'xemacs))
1279                                   (module-installed-p 'smiley-mule)
1280                                   t))))
1281
1282 (defvar gnus-article-smiley-mule-loaded-p nil
1283   "Internal variable used to say whether `smiley-mule' is loaded (whether
1284 smiley functions are not overridden by `smiley').")
1285
1286 (defcustom gnus-treat-display-face
1287   (and (not noninteractive)
1288        ;; x-face-e21 handles both X-Face and Face headers.
1289        (not (and (eq gnus-article-x-face-command 'x-face-decode-message-header)
1290                  (module-installed-p 'x-face-e21)))
1291        (or (and (fboundp 'image-type-available-p)
1292                 (image-type-available-p 'png))
1293            (and (featurep 'xemacs)
1294                 (featurep 'png)))
1295        'head)
1296   "Display Face headers.
1297 Valid values are nil, t, `head', `last', an integer or a predicate.
1298 See Info node `(gnus)Customizing Articles' and Info node
1299 `(gnus)X-Face' for details."
1300   :group 'gnus-article-treat
1301   :version "21.1"
1302   :link '(custom-manual "(gnus)Customizing Articles")
1303   :link '(custom-manual "(gnus)X-Face")
1304   :type gnus-article-treat-head-custom)
1305 (put 'gnus-treat-display-face 'highlight t)
1306
1307 (defcustom gnus-treat-display-smileys
1308   (if (and (not noninteractive)
1309            (or (and (featurep 'xemacs)
1310                     (featurep 'xpm))
1311                (gnus-image-type-available-p 'xpm)
1312                (gnus-image-type-available-p 'pbm)
1313                (and (not (featurep 'xemacs))
1314                     window-system
1315                     (module-installed-p 'smiley-mule))))
1316       t
1317     nil)
1318   "Display smileys.
1319 Valid values are nil, t, `head', `last', an integer or a predicate.
1320 See Info node `(gnus)Customizing Articles' and Info node
1321 `(gnus)Smileys' for details."
1322   :group 'gnus-article-treat
1323   ;;:version "21.1"
1324   :link '(custom-manual "(gnus)Customizing Articles")
1325   :link '(custom-manual "(gnus)Smileys")
1326   :type gnus-article-treat-custom)
1327 (put 'gnus-treat-display-smileys 'highlight t)
1328
1329 (defcustom gnus-treat-from-picon
1330   (if (and (gnus-image-type-available-p 'xpm)
1331            (gnus-picons-installed-p))
1332       'head nil)
1333   "Display picons in the From header.
1334 Valid values are nil, t, `head', `last', an integer or a predicate.
1335 See Info node `(gnus)Customizing Articles' and Info node
1336 `(gnus)Picons' for details."
1337   :group 'gnus-article-treat
1338   :group 'gnus-picon
1339   :link '(custom-manual "(gnus)Customizing Articles")
1340   :link '(custom-manual "(gnus)Picons")
1341   :type gnus-article-treat-head-custom)
1342 (put 'gnus-treat-from-picon 'highlight t)
1343
1344 (defcustom gnus-treat-mail-picon
1345   (if (and (gnus-image-type-available-p 'xpm)
1346            (gnus-picons-installed-p))
1347       'head nil)
1348   "Display picons in To and Cc headers.
1349 Valid values are nil, t, `head', `last', an integer or a predicate.
1350 See Info node `(gnus)Customizing Articles' and Info node
1351 `(gnus)Picons' for details."
1352   :group 'gnus-article-treat
1353   :group 'gnus-picon
1354   :link '(custom-manual "(gnus)Customizing Articles")
1355   :link '(custom-manual "(gnus)Picons")
1356   :type gnus-article-treat-head-custom)
1357 (put 'gnus-treat-mail-picon 'highlight t)
1358
1359 (defcustom gnus-treat-newsgroups-picon
1360   (if (and (gnus-image-type-available-p 'xpm)
1361            (gnus-picons-installed-p))
1362       'head nil)
1363   "Display picons in the Newsgroups and Followup-To headers.
1364 Valid values are nil, t, `head', `last', an integer or a predicate.
1365 See Info node `(gnus)Customizing Articles' and Info node
1366 `(gnus)Picons' for details."
1367   :group 'gnus-article-treat
1368   :group 'gnus-picon
1369   :link '(custom-manual "(gnus)Customizing Articles")
1370   :link '(custom-manual "(gnus)Picons")
1371   :type gnus-article-treat-head-custom)
1372 (put 'gnus-treat-newsgroups-picon 'highlight t)
1373
1374 (defcustom gnus-treat-body-boundary
1375   (if (or gnus-treat-newsgroups-picon
1376           gnus-treat-mail-picon
1377           gnus-treat-from-picon)
1378       'head nil)
1379   "Draw a boundary at the end of the headers.
1380 Valid values are nil and `head'.
1381 See Info node `(gnus)Customizing Articles' for details."
1382   :version "21.1"
1383   :group 'gnus-article-treat
1384   :link '(custom-manual "(gnus)Customizing Articles")
1385   :type gnus-article-treat-head-custom)
1386
1387 (defcustom gnus-treat-capitalize-sentences nil
1388   "Capitalize sentence-starting words.
1389 Valid values are nil, t, `head', `last', an integer or a predicate.
1390 See Info node `(gnus)Customizing Articles' for details."
1391   :version "21.1"
1392   :group 'gnus-article-treat
1393   :link '(custom-manual "(gnus)Customizing Articles")
1394   :type gnus-article-treat-custom)
1395
1396 (defcustom gnus-treat-wash-html nil
1397   "Format as HTML.
1398 Valid values are nil, t, `head', `last', an integer or a predicate.
1399 See Info node `(gnus)Customizing Articles' for details."
1400   :group 'gnus-article-treat
1401   :link '(custom-manual "(gnus)Customizing Articles")
1402   :type gnus-article-treat-custom)
1403
1404 (defcustom gnus-treat-fill-long-lines nil
1405   "Fill long lines.
1406 Valid values are nil, t, `head', `last', an integer or a predicate.
1407 See Info node `(gnus)Customizing Articles' for details."
1408   :group 'gnus-article-treat
1409   :link '(custom-manual "(gnus)Customizing Articles")
1410   :type gnus-article-treat-custom)
1411
1412 (defcustom gnus-treat-play-sounds nil
1413   "Play sounds.
1414 Valid values are nil, t, `head', `last', an integer or a predicate.
1415 See Info node `(gnus)Customizing Articles' for details."
1416   :version "21.1"
1417   :group 'gnus-article-treat
1418   :link '(custom-manual "(gnus)Customizing Articles")
1419   :type gnus-article-treat-custom)
1420
1421 (defcustom gnus-treat-decode-article-as-default-mime-charset nil
1422   "Decode an article as `default-mime-charset'.  For instance, if you want to
1423 attempt to decode an article even if the value of `gnus-show-mime' is nil,
1424 you could set this variable to something like: nil for don't decode, t for
1425 decode the body, '(or header t) for the whole article, etc."
1426   :group 'gnus-article-treat
1427   :type '(radio (const :tag "Off" nil)
1428                 (const :tag "Decode body" t)
1429                 (const :tag "Decode all" (or head t))))
1430
1431 (defcustom gnus-treat-translate nil
1432   "Translate articles from one language to another.
1433 Valid values are nil, t, `head', `last', an integer or a predicate.
1434 See Info node `(gnus)Customizing Articles' for details."
1435   :version "21.1"
1436   :group 'gnus-article-treat
1437   :link '(custom-manual "(gnus)Customizing Articles")
1438   :type gnus-article-treat-custom)
1439
1440 (defcustom gnus-treat-x-pgp-sig nil
1441   "Verify X-PGP-Sig.
1442 To automatically treat X-PGP-Sig, set it to head.
1443 Valid values are nil, t, `head', `last', an integer or a predicate.
1444 See Info node `(gnus)Customizing Articles' for details."
1445   :group 'gnus-article-treat
1446   :group 'mime-security
1447   :type gnus-article-treat-custom)
1448
1449 (defcustom gnus-treat-monafy nil
1450   "Display body part with mona font.
1451 Valid values are nil, t, `head', `last', an integer or a predicate.
1452 See Info node `(gnus)Customizing Articles' for details."
1453   :group 'gnus-article-treat
1454   :group 'mime-security
1455   :link '(custom-manual "(gnus)Customizing Articles")
1456   :type gnus-article-treat-custom)
1457
1458 (defvar gnus-article-encrypt-protocol-alist
1459   '(("PGP" . mml2015-self-encrypt)))
1460
1461 ;; Set to nil if more than one protocol added to
1462 ;; gnus-article-encrypt-protocol-alist.
1463 (defcustom gnus-article-encrypt-protocol "PGP"
1464   "The protocol used for encrypt articles.
1465 It is a string, such as \"PGP\". If nil, ask user."
1466   :type 'string
1467   :group 'mime-security)
1468
1469 (defvar gnus-article-wash-function nil
1470   "Function used for converting HTML into text.")
1471
1472 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1473                               (mm-coding-system-p 'utf-8)
1474                               (executable-find idna-program))
1475   "Whether IDNA decoding of headers is used when viewing messages.
1476 This requires GNU Libidn, and by default only enabled if it is found."
1477   :group 'gnus-article-headers
1478   :type 'boolean)
1479
1480 (defcustom gnus-article-over-scroll nil
1481   "If non-nil, allow scrolling the article buffer even when there no more text."
1482   :group 'gnus-article
1483   :type 'boolean)
1484
1485 ;;; Internal variables
1486
1487 (defvar gnus-english-month-names
1488   '("January" "February" "March" "April" "May" "June" "July" "August"
1489     "September" "October" "November" "December"))
1490
1491 (defvar article-goto-body-goes-to-point-min-p nil)
1492 (defvar gnus-article-wash-types nil)
1493 (defvar gnus-article-emphasis-alist nil)
1494 (defvar gnus-article-image-alist nil)
1495
1496 (defvar gnus-article-mime-handle-alist-1 nil)
1497 (defvar gnus-treatment-function-alist
1498   '((gnus-treat-decode-article-as-default-mime-charset
1499      gnus-article-decode-article-as-default-mime-charset)
1500     (gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1501     (gnus-treat-monafy gnus-article-monafy)
1502     (gnus-treat-strip-banner gnus-article-strip-banner)
1503     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1504     (gnus-treat-buttonize gnus-article-add-buttons)
1505     (gnus-treat-fill-article gnus-article-fill-cited-article)
1506     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1507     (gnus-treat-strip-cr gnus-article-remove-cr)
1508     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1509     (gnus-treat-date-ut gnus-article-date-ut)
1510     (gnus-treat-date-local gnus-article-date-local)
1511     (gnus-treat-date-english gnus-article-date-english)
1512     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1513     (gnus-treat-date-original gnus-article-date-original)
1514     (gnus-treat-date-user-defined gnus-article-date-user)
1515     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1516     (gnus-treat-display-face gnus-article-display-face)
1517     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1518     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1519     (gnus-treat-hide-signature gnus-article-hide-signature)
1520     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1521     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1522     (gnus-treat-strip-pem gnus-article-hide-pem)
1523     (gnus-treat-from-picon gnus-treat-from-picon)
1524     (gnus-treat-mail-picon gnus-treat-mail-picon)
1525     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1526     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1527     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1528     (gnus-treat-strip-trailing-blank-lines
1529      gnus-article-remove-trailing-blank-lines)
1530     (gnus-treat-strip-leading-blank-lines
1531      gnus-article-strip-leading-blank-lines)
1532     (gnus-treat-strip-multiple-blank-lines
1533      gnus-article-strip-multiple-blank-lines)
1534     (gnus-treat-overstrike gnus-article-treat-overstrike)
1535     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1536     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1537     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1538     ;; Displaying X-Face should be done after unfolding headers
1539     ;; to protect bitmap lines.
1540     (gnus-treat-display-x-face gnus-article-display-x-face)
1541     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1542     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1543     (gnus-treat-display-smileys gnus-treat-smiley)
1544     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1545     (gnus-treat-wash-html gnus-article-wash-html)
1546     (gnus-treat-emphasize gnus-article-emphasize)
1547     (gnus-treat-hide-citation gnus-article-hide-citation)
1548     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1549     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1550     (gnus-treat-body-boundary gnus-article-treat-body-boundary)
1551     (gnus-treat-play-sounds gnus-earcon-display)))
1552
1553 (defvar gnus-article-mime-handle-alist nil)
1554 (defvar article-lapsed-timer nil)
1555 (defvar gnus-article-current-summary nil)
1556
1557 (defvar gnus-article-mode-syntax-table
1558   (let ((table (copy-syntax-table text-mode-syntax-table)))
1559     ;; This causes the citation match run O(2^n).
1560     ;; (modify-syntax-entry ?- "w" table)
1561     (modify-syntax-entry ?> ")<" table)
1562     (modify-syntax-entry ?< "(>" table)
1563     ;; make M-. in article buffers work for `foo' strings
1564     (modify-syntax-entry ?' " " table)
1565     (modify-syntax-entry ?` " " table)
1566     table)
1567   "Syntax table used in article mode buffers.
1568 Initialized from `text-mode-syntax-table.")
1569
1570 (defvar gnus-save-article-buffer nil)
1571
1572 (defvar gnus-article-mode-line-format-alist
1573   (nconc '((?w (gnus-article-wash-status) ?s)
1574            (?m (gnus-article-mime-part-status) ?s))
1575          gnus-summary-mode-line-format-alist))
1576
1577 (defvar gnus-number-of-articles-to-be-saved nil)
1578
1579 (defvar gnus-inhibit-hiding nil)
1580
1581 ;;; Macros for dealing with the article buffer.
1582
1583 (defmacro gnus-with-article-headers (&rest forms)
1584   `(save-excursion
1585      (set-buffer gnus-article-buffer)
1586      (save-restriction
1587        (let ((buffer-read-only nil)
1588              (inhibit-point-motion-hooks t)
1589              (case-fold-search t))
1590          (article-narrow-to-head)
1591          ,@forms))))
1592
1593 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1594 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1595
1596 (defmacro gnus-with-article-buffer (&rest forms)
1597   `(save-excursion
1598      (set-buffer gnus-article-buffer)
1599      (let ((buffer-read-only nil))
1600        ,@forms)))
1601
1602 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1603 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1604
1605 (defun gnus-article-goto-header (header)
1606   "Go to HEADER, which is a regular expression."
1607   (re-search-forward (concat "^\\(" header "\\):") nil t))
1608
1609 (defsubst gnus-article-hide-text (b e props)
1610   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1611   (gnus-add-text-properties-when 'article-type nil b e props)
1612   (when (memq 'intangible props)
1613     (put-text-property
1614      (max (1- b) (point-min))
1615      b 'intangible (cddr (memq 'intangible props)))))
1616
1617 (defsubst gnus-article-unhide-text (b e)
1618   "Remove hidden text properties from region between B and E."
1619   (remove-text-properties b e gnus-hidden-properties)
1620   (when (memq 'intangible gnus-hidden-properties)
1621     (put-text-property (max (1- b) (point-min))
1622                        b 'intangible nil)))
1623
1624 (defun gnus-article-hide-text-type (b e type)
1625   "Hide text of TYPE between B and E."
1626   (gnus-add-wash-type type)
1627   (gnus-article-hide-text
1628    b e (cons 'article-type (cons type gnus-hidden-properties))))
1629
1630 (defun gnus-article-unhide-text-type (b e type)
1631   "Unhide text of TYPE between B and E."
1632   (gnus-delete-wash-type type)
1633   (remove-text-properties
1634    b e (cons 'article-type (cons type gnus-hidden-properties)))
1635   (when (memq 'intangible gnus-hidden-properties)
1636     (put-text-property (max (1- b) (point-min))
1637                        b 'intangible nil)))
1638
1639 (defun gnus-article-hide-text-of-type (type)
1640   "Hide text of TYPE in the current buffer."
1641   (save-excursion
1642     (let ((b (point-min))
1643           (e (point-max)))
1644       (while (setq b (text-property-any b e 'article-type type))
1645         (add-text-properties b (incf b) gnus-hidden-properties)))))
1646
1647 (defun gnus-article-delete-text-of-type (type)
1648   "Delete text of TYPE in the current buffer."
1649   (save-excursion
1650     (let ((b (point-min)))
1651       (while (setq b (text-property-any b (point-max) 'article-type type))
1652         (delete-region
1653          b (or (text-property-not-all b (point-max) 'article-type type)
1654                (point-max)))))))
1655
1656 (defun gnus-article-delete-invisible-text ()
1657   "Delete all invisible text in the current buffer."
1658   (save-excursion
1659     (let ((b (point-min)))
1660       (while (setq b (text-property-any b (point-max) 'invisible t))
1661         (delete-region
1662          b (or (text-property-not-all b (point-max) 'invisible t)
1663                (point-max)))))))
1664
1665 (defun gnus-article-text-type-exists-p (type)
1666   "Say whether any text of type TYPE exists in the buffer."
1667   (text-property-any (point-min) (point-max) 'article-type type))
1668
1669 (defsubst gnus-article-header-rank ()
1670   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1671   (let ((list gnus-sorted-header-list)
1672         (i 1))
1673     (while list
1674       (if (looking-at (car list))
1675           (setq list nil)
1676         (setq list (cdr list))
1677         (incf i)))
1678       i))
1679
1680 (defun article-hide-headers (&optional arg delete)
1681   "Hide unwanted headers and possibly sort them as well."
1682   (interactive (gnus-article-hidden-arg))
1683   ;; Lars said that this function might be inhibited.
1684   (if (gnus-article-check-hidden-text 'headers arg)
1685       (progn
1686         ;; Show boring headers as well.
1687         (gnus-article-show-hidden-text 'boring-headers)
1688         (when (eq 1 (point-min))
1689           (set-window-start (get-buffer-window (current-buffer)) 1)))
1690     (unless gnus-inhibit-hiding
1691       (save-excursion
1692         (save-restriction
1693           (let ((inhibit-read-only t)
1694                 (case-fold-search t)
1695                 (max (1+ (length gnus-sorted-header-list)))
1696                 (ignored (when (not gnus-visible-headers)
1697                            (cond ((stringp gnus-ignored-headers)
1698                                   gnus-ignored-headers)
1699                                  ((listp gnus-ignored-headers)
1700                                   (mapconcat 'identity gnus-ignored-headers
1701                                              "\\|")))))
1702                 (visible
1703                  (cond ((stringp gnus-visible-headers)
1704                         gnus-visible-headers)
1705                        ((and gnus-visible-headers
1706                              (listp gnus-visible-headers))
1707                         (mapconcat 'identity gnus-visible-headers "\\|"))))
1708                 (inhibit-point-motion-hooks t)
1709                 beg)
1710             ;; First we narrow to just the headers.
1711             (article-narrow-to-head)
1712             ;; Hide any "From " lines at the beginning of (mail) articles.
1713             (while (looking-at "From ")
1714               (forward-line 1))
1715             (unless (bobp)
1716               (if delete
1717                   (delete-region (point-min) (point))
1718                 (gnus-article-hide-text (point-min) (point)
1719                                         (nconc (list 'article-type 'headers)
1720                                                gnus-hidden-properties))))
1721             ;; Then treat the rest of the header lines.
1722             ;; Then we use the two regular expressions
1723             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1724             ;; select which header lines is to remain visible in the
1725             ;; article buffer.
1726             (while (re-search-forward "^[^ \t:]*:" nil t)
1727               (beginning-of-line)
1728               ;; Mark the rank of the header.
1729               (put-text-property
1730                (point) (1+ (point)) 'message-rank
1731                (if (or (and visible (looking-at visible))
1732                        (and ignored
1733                             (not (looking-at ignored))))
1734                    (gnus-article-header-rank)
1735                  (+ 2 max)))
1736               (forward-line 1))
1737             (message-sort-headers-1)
1738             (when (setq beg (text-property-any
1739                              (point-min) (point-max) 'message-rank (+ 2 max)))
1740               ;; We delete or make invisible the unwanted headers.
1741               (gnus-add-wash-type 'headers)
1742               (if delete
1743                   (progn
1744                     (add-text-properties
1745                      (point-min) (+ 5 (point-min))
1746                      '(article-type headers dummy-invisible t))
1747                     (delete-region beg (point-max)))
1748                 (gnus-article-hide-text-type beg (point-max) 'headers)))))))))
1749
1750 (defun article-hide-boring-headers (&optional arg)
1751   "Toggle hiding of headers that aren't very interesting.
1752 If given a negative prefix, always show; if given a positive prefix,
1753 always hide."
1754   (interactive (gnus-article-hidden-arg))
1755   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1756              (not gnus-show-all-headers))
1757     (save-excursion
1758       (save-restriction
1759         (let ((buffer-read-only nil)
1760               (list gnus-boring-article-headers)
1761               (inhibit-point-motion-hooks t)
1762               elem)
1763           (article-narrow-to-head)
1764           (while list
1765             (setq elem (pop list))
1766             (goto-char (point-min))
1767             (cond
1768              ;; Hide empty headers.
1769              ((eq elem 'empty)
1770               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1771                 (forward-line -1)
1772                 (gnus-article-hide-text-type
1773                  (point-at-bol)
1774                  (progn
1775                    (end-of-line)
1776                    (if (re-search-forward "^[^ \t]" nil t)
1777                        (match-beginning 0)
1778                      (point-max)))
1779                  'boring-headers)))
1780              ;; Hide boring Newsgroups header.
1781              ((eq elem 'newsgroups)
1782               (when (gnus-string-equal
1783                      (gnus-fetch-field "newsgroups")
1784                      (gnus-group-real-name
1785                       (if (boundp 'gnus-newsgroup-name)
1786                           gnus-newsgroup-name
1787                         "")))
1788                 (gnus-article-hide-header "newsgroups")))
1789              ((eq elem 'to-address)
1790               (let ((to (message-fetch-field "to"))
1791                     (to-address
1792                      (gnus-parameter-to-address
1793                       (if (boundp 'gnus-newsgroup-name)
1794                           gnus-newsgroup-name ""))))
1795                 (when (and to to-address
1796                            (ignore-errors
1797                              (gnus-string-equal
1798                               ;; only one address in To
1799                               (nth 1 (mail-extract-address-components to))
1800                               to-address)))
1801                   (gnus-article-hide-header "to"))))
1802              ((eq elem 'to-list)
1803               (let ((to (message-fetch-field "to"))
1804                     (to-list
1805                      (gnus-parameter-to-list
1806                       (if (boundp 'gnus-newsgroup-name)
1807                           gnus-newsgroup-name ""))))
1808                 (when (and to to-list
1809                            (ignore-errors
1810                              (gnus-string-equal
1811                               ;; only one address in To
1812                               (nth 1 (mail-extract-address-components to))
1813                               to-list)))
1814                   (gnus-article-hide-header "to"))))
1815              ((eq elem 'cc-list)
1816               (let ((cc (message-fetch-field "cc"))
1817                     (to-list
1818                      (gnus-parameter-to-list
1819                       (if (boundp 'gnus-newsgroup-name)
1820                           gnus-newsgroup-name ""))))
1821                 (when (and cc to-list
1822                            (ignore-errors
1823                              (gnus-string-equal
1824                               ;; only one address in CC
1825                               (nth 1 (mail-extract-address-components cc))
1826                               to-list)))
1827                   (gnus-article-hide-header "cc"))))
1828              ((eq elem 'followup-to)
1829               (when (gnus-string-equal
1830                      (message-fetch-field "followup-to")
1831                      (message-fetch-field "newsgroups"))
1832                 (gnus-article-hide-header "followup-to")))
1833              ((eq elem 'reply-to)
1834               (if (gnus-group-find-parameter
1835                    gnus-newsgroup-name 'broken-reply-to)
1836                   (gnus-article-hide-header "reply-to")
1837                 (let ((from (message-fetch-field "from"))
1838                       (reply-to (message-fetch-field "reply-to")))
1839                   (when
1840                       (and
1841                        from reply-to
1842                        (ignore-errors
1843                          (equal
1844                           (sort (mapcar
1845                                  (lambda (x) (downcase (cadr x)))
1846                                  (mail-extract-address-components from t))
1847                                 'string<)
1848                           (sort (mapcar
1849                                  (lambda (x) (downcase (cadr x)))
1850                                  (mail-extract-address-components reply-to t))
1851                                 'string<))))
1852                     (gnus-article-hide-header "reply-to")))))
1853              ((eq elem 'date)
1854               (let ((date (message-fetch-field "date")))
1855                 (when (and date
1856                            (< (days-between (current-time-string) date)
1857                               4))
1858                   (gnus-article-hide-header "date"))))
1859              ((eq elem 'long-to)
1860               (let ((to (message-fetch-field "to"))
1861                     (cc (message-fetch-field "cc")))
1862                 (when (> (length to) 1024)
1863                   (gnus-article-hide-header "to"))
1864                 (when (> (length cc) 1024)
1865                   (gnus-article-hide-header "cc"))))
1866              ((eq elem 'many-to)
1867               (let ((to-count 0)
1868                     (cc-count 0))
1869                 (goto-char (point-min))
1870                 (while (re-search-forward "^to:" nil t)
1871                   (setq to-count (1+ to-count)))
1872                 (when (> to-count 1)
1873                   (while (> to-count 0)
1874                     (goto-char (point-min))
1875                     (save-restriction
1876                       (re-search-forward "^to:" nil nil to-count)
1877                       (forward-line -1)
1878                       (narrow-to-region (point) (point-max))
1879                       (gnus-article-hide-header "to"))
1880                     (setq to-count (1- to-count))))
1881                 (goto-char (point-min))
1882                 (while (re-search-forward "^cc:" nil t)
1883                   (setq cc-count (1+ cc-count)))
1884                 (when (> cc-count 1)
1885                   (while (> cc-count 0)
1886                     (goto-char (point-min))
1887                     (save-restriction
1888                       (re-search-forward "^cc:" nil nil cc-count)
1889                       (forward-line -1)
1890                       (narrow-to-region (point) (point-max))
1891                       (gnus-article-hide-header "cc"))
1892                     (setq cc-count (1- cc-count)))))))))))))
1893
1894 (defun gnus-article-hide-header (header)
1895   (save-excursion
1896     (goto-char (point-min))
1897     (when (re-search-forward (concat "^" header ":") nil t)
1898       (gnus-article-hide-text-type
1899        (point-at-bol)
1900        (progn
1901          (end-of-line)
1902          (if (re-search-forward "^[^ \t]" nil t)
1903              (match-beginning 0)
1904            (point-max)))
1905        'boring-headers))))
1906
1907 (defvar gnus-article-normalized-header-length 40
1908   "Length of normalized headers.")
1909
1910 (defun article-normalize-headers ()
1911   "Make all header lines 40 characters long."
1912   (interactive)
1913   (let ((buffer-read-only nil)
1914         column)
1915     (save-excursion
1916       (save-restriction
1917         (article-narrow-to-head)
1918         (while (not (eobp))
1919           (cond
1920            ((< (setq column (- (point-at-eol) (point)))
1921                gnus-article-normalized-header-length)
1922             (end-of-line)
1923             (insert (make-string
1924                      (- gnus-article-normalized-header-length column)
1925                      ? )))
1926            ((> column gnus-article-normalized-header-length)
1927             (gnus-put-text-property
1928              (progn
1929                (forward-char gnus-article-normalized-header-length)
1930                (point))
1931              (point-at-eol)
1932              'invisible t))
1933            (t
1934             ;; Do nothing.
1935             ))
1936           (forward-line 1))))))
1937
1938 (defun article-treat-dumbquotes ()
1939   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
1940 Note that this function guesses whether a character is a sm*rtq**t* or
1941 not, so it should only be used interactively.
1942
1943 Sm*rtq**t*s are M****s***'s unilateral extension to the
1944 iso-8859-1 character map in an attempt to provide more quoting
1945 characters.  If you see something like \\222 or \\264 where
1946 you're expecting some kind of apostrophe or quotation mark, then
1947 try this wash."
1948   (interactive)
1949   (article-translate-strings gnus-article-dumbquotes-map))
1950
1951 (defun article-translate-characters (from to)
1952   "Translate all characters in the body of the article according to FROM and TO.
1953 FROM is a string of characters to translate from; to is a string of
1954 characters to translate to."
1955   (save-excursion
1956     (when (article-goto-body)
1957       (let ((buffer-read-only nil)
1958             (x (make-string 225 ?x))
1959             (i -1))
1960         (while (< (incf i) (length x))
1961           (aset x i i))
1962         (setq i 0)
1963         (while (< i (length from))
1964           (aset x (aref from i) (aref to i))
1965           (incf i))
1966         (translate-region (point) (point-max) x)))))
1967
1968 (defun article-translate-strings (map)
1969   "Translate all string in the body of the article according to MAP.
1970 MAP is an alist where the elements are on the form (\"from\" \"to\")."
1971   (save-excursion
1972     (when (article-goto-body)
1973       (let ((buffer-read-only nil)
1974             elem)
1975         (while (setq elem (pop map))
1976           (save-excursion
1977             (while (search-forward (car elem) nil t)
1978               (replace-match (cadr elem)))))))))
1979
1980 (defun article-treat-overstrike ()
1981   "Translate overstrikes into bold text."
1982   (interactive)
1983   (save-excursion
1984     (when (article-goto-body)
1985       (let ((buffer-read-only nil))
1986         (while (search-forward "\b" nil t)
1987           (let ((next (char-after))
1988                 start end previous)
1989             (backward-char 2)
1990             (setq start (point)
1991                   previous (char-after))
1992             (forward-char 3)
1993             (setq end (point))
1994             (backward-char)
1995             ;; We do the boldification/underlining by hiding the
1996             ;; overstrikes and putting the proper text property
1997             ;; on the letters.
1998             (cond
1999              ((eq next previous)
2000               (gnus-article-hide-text-type start (point) 'overstrike)
2001               (put-text-property (point) end 'face 'bold))
2002              ((eq next ?_)
2003               (gnus-article-hide-text-type
2004                (1- (point)) (1+ (point)) 'overstrike)
2005               (put-text-property
2006                start (1- (point)) 'face 'underline))
2007              ((eq previous ?_)
2008               (gnus-article-hide-text-type start (point) 'overstrike)
2009               (put-text-property
2010                (point) end 'face 'underline)))))))))
2011
2012 (defun article-treat-ansi-sequences ()
2013   "Translate ANSI SGR control sequences into overlays or extents."
2014   (interactive)
2015   (save-excursion
2016     (when (article-goto-body)
2017       (let ((buffer-read-only nil))
2018         (ansi-color-apply-on-region (point) (point-max))))))
2019
2020 (defun gnus-article-treat-unfold-headers ()
2021   "Unfold folded message headers.
2022 Only the headers that fit into the current window width will be
2023 unfolded."
2024   (interactive)
2025   (gnus-with-article-headers
2026     (let (length)
2027       (while (not (eobp))
2028         (save-restriction
2029           (mail-header-narrow-to-field)
2030           (let ((header (buffer-string)))
2031             (with-temp-buffer
2032               (insert header)
2033               (goto-char (point-min))
2034               (while (re-search-forward "\n[\t ]" nil t)
2035                 (replace-match " " t t)))
2036             (setq length (- (point-max) (point-min) 1)))
2037           (when (< length (window-width))
2038             (while (re-search-forward "\n[\t ]" nil t)
2039               (replace-match " " t t)))
2040           (goto-char (point-max)))))))
2041
2042 (defun gnus-article-treat-fold-headers ()
2043   "Fold message headers."
2044   (interactive)
2045   (gnus-with-article-headers
2046     (while (not (eobp))
2047       (save-restriction
2048         (mail-header-narrow-to-field)
2049         (mail-header-fold-field)
2050         (goto-char (point-max))))))
2051
2052 (defun gnus-treat-smiley ()
2053   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2054   (interactive)
2055   (unless (featurep 'xemacs)
2056     (when (and (>= emacs-major-version 21)
2057                (not gnus-article-should-use-smiley-mule)
2058                gnus-article-smiley-mule-loaded-p)
2059       (load "smiley" nil t)
2060       (setq gnus-article-smiley-mule-loaded-p nil))
2061     (when (and gnus-article-should-use-smiley-mule
2062                (not gnus-article-smiley-mule-loaded-p))
2063       (load "smiley-mule" nil t)
2064       (setq gnus-article-smiley-mule-loaded-p t)))
2065   (gnus-with-article-buffer
2066     (if (memq 'smiley gnus-article-wash-types)
2067         (gnus-delete-images 'smiley)
2068       (article-goto-body)
2069       (let ((images (smiley-region (point) (point-max))))
2070         (when images
2071           (gnus-add-wash-type 'smiley)
2072           (dolist (image images)
2073             (gnus-add-image 'smiley image)))))))
2074
2075 (defun gnus-article-remove-images ()
2076   "Remove all images from the article buffer."
2077   (interactive)
2078   (gnus-with-article-buffer
2079     (dolist (elem gnus-article-image-alist)
2080       (gnus-delete-images (car elem)))))
2081
2082 (defun gnus-article-treat-fold-newsgroups ()
2083   "Unfold folded message headers.
2084 Only the headers that fit into the current window width will be
2085 unfolded."
2086   (interactive)
2087   (gnus-with-article-headers
2088     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2089       (save-restriction
2090         (mail-header-narrow-to-field)
2091         (while (re-search-forward ", *" nil t)
2092           (replace-match ", " t t))
2093         (mail-header-fold-field)
2094         (goto-char (point-max))))))
2095
2096 (defun gnus-article-treat-body-boundary ()
2097   "Place a boundary line at the end of the headers."
2098   (interactive)
2099   (when (and gnus-body-boundary-delimiter
2100              (> (length gnus-body-boundary-delimiter) 0))
2101     (gnus-with-article-headers
2102       (goto-char (point-max))
2103       (let ((start (point)))
2104         (insert "X-Boundary: ")
2105         (gnus-add-text-properties start (point) '(invisible t intangible t))
2106         (insert (let (str)
2107                   (while (>= (1- (window-width)) (length str))
2108                     (setq str (concat str gnus-body-boundary-delimiter)))
2109                   (substring str 0 (1- (window-width))))
2110                 "\n")
2111         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2112
2113 (defun article-fill-long-lines ()
2114   "Fill lines that are wider than the window width."
2115   (interactive)
2116   (save-excursion
2117     (let ((buffer-read-only nil)
2118           (width (window-width (get-buffer-window (current-buffer)))))
2119       (save-restriction
2120         (article-goto-body)
2121         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2122           (while (not (eobp))
2123             (end-of-line)
2124             (when (>= (current-column) (min fill-column width))
2125               (narrow-to-region (min (1+ (point)) (point-max))
2126                                 (point-at-bol))
2127               (let ((goback (point-marker)))
2128                 (fill-paragraph nil)
2129                 (goto-char (marker-position goback)))
2130               (widen))
2131             (forward-line 1)))))))
2132
2133 (defun article-capitalize-sentences ()
2134   "Capitalize the first word in each sentence."
2135   (interactive)
2136   (save-excursion
2137     (let ((buffer-read-only nil)
2138           (paragraph-start "^[\n\^L]"))
2139       (article-goto-body)
2140       (while (not (eobp))
2141         (capitalize-word 1)
2142         (forward-sentence)))))
2143
2144 (defun article-remove-cr ()
2145   "Remove trailing CRs and then translate remaining CRs into LFs."
2146   (interactive)
2147   (save-excursion
2148     (let ((buffer-read-only nil))
2149       (goto-char (point-min))
2150       (while (re-search-forward "\r+$" nil t)
2151         (replace-match "" t t))
2152       (goto-char (point-min))
2153       (while (search-forward "\r" nil t)
2154         (replace-match "\n" t t)))))
2155
2156 (defun article-remove-trailing-blank-lines ()
2157   "Remove all trailing blank lines from the article."
2158   (interactive)
2159   (save-excursion
2160     (let ((buffer-read-only nil))
2161       (goto-char (point-max))
2162       (delete-region
2163        (point)
2164        (progn
2165          (while (and (not (bobp))
2166                      (looking-at "^[ \t]*$")
2167                      (not (gnus-annotation-in-region-p
2168                            (point) (point-at-eol))))
2169            (forward-line -1))
2170          (forward-line 1)
2171          (point))))))
2172
2173 (defun article-display-face ()
2174   "Display any Face headers in the header."
2175   (interactive)
2176   (let ((wash-face-p buffer-read-only))
2177     (gnus-with-article-headers
2178       ;; When displaying parts, this function can be called several times on
2179       ;; the same article, without any intended toggle semantic (as typing `W
2180       ;; D d' would have). So face deletion must occur only when we come from
2181       ;; an interactive command, that is when the *Article* buffer is
2182       ;; read-only.
2183       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2184           (gnus-delete-images 'face)
2185         (let (face faces)
2186           (save-excursion
2187             (when (and wash-face-p
2188                        (progn
2189                          (goto-char (point-min))
2190                          (not (re-search-forward "^Face:[\t ]*" nil t)))
2191                        (gnus-buffer-live-p gnus-original-article-buffer))
2192               (set-buffer gnus-original-article-buffer))
2193             (save-restriction
2194               (mail-narrow-to-head)
2195               (while (gnus-article-goto-header "Face")
2196                 (push (mail-header-field-value) faces))))
2197           (while (setq face (pop faces))
2198             (let ((png (gnus-convert-face-to-png face))
2199                   image)
2200               (when png
2201                 (setq image (gnus-create-image png 'png t))
2202                 (gnus-article-goto-header "from")
2203                 (when (bobp)
2204                   (insert "From: [no `from' set]\n")
2205                   (forward-char -17))
2206                 (gnus-add-wash-type 'face)
2207                 (gnus-add-image 'face image)
2208                 (gnus-put-image image nil 'face))))))
2209       )))
2210
2211 (defun article-display-x-face (&optional force)
2212   "Look for an X-Face header and display it if present."
2213   (interactive (list 'force))
2214   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2215     (gnus-with-article-headers
2216       ;; Delete the old process, if any.
2217       (when (process-status "article-x-face")
2218         (delete-process "article-x-face"))
2219       ;; See the comment in `article-display-face'.
2220       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2221           ;; We have already displayed X-Faces, so we remove them
2222           ;; instead.
2223           (gnus-delete-images 'xface)
2224         ;; Display X-Faces.
2225         (let (x-faces from face)
2226           (save-excursion
2227             (when (and wash-face-p
2228                        (progn
2229                          (goto-char (point-min))
2230                          (not (re-search-forward
2231                                "^X-Face\\(-[0-9]+\\)?:[\t ]*" nil t)))
2232                        (gnus-buffer-live-p gnus-original-article-buffer))
2233               ;; If type `W f', use gnus-original-article-buffer,
2234               ;; otherwise use the current buffer because displaying
2235               ;; RFC822 parts calls this function too.
2236               (set-buffer gnus-original-article-buffer))
2237             (save-restriction
2238               (mail-narrow-to-head)
2239               (while (gnus-article-goto-header "X-Face")
2240                 (push (mail-header-field-value) x-faces))
2241               (setq from (message-fetch-field "from"))))
2242           ;; Sending multiple EOFs to xv doesn't work, so we only do a
2243           ;; single external face.
2244           (when (stringp gnus-article-x-face-command)
2245             (setq x-faces (list (car x-faces))))
2246           (while (and (setq face (pop x-faces))
2247                       gnus-article-x-face-command
2248                       (or force
2249                           ;; Check whether this face is censored.
2250                           (not gnus-article-x-face-too-ugly)
2251                           (and gnus-article-x-face-too-ugly from
2252                                (not (string-match gnus-article-x-face-too-ugly
2253                                                   from)))))
2254             ;; We display the face.
2255             (cond ((stringp gnus-article-x-face-command)
2256                    ;; The command is a string, so we interpret the command
2257                    ;; as a, well, command, and fork it off.
2258                    (let ((process-connection-type nil))
2259                      (process-kill-without-query
2260                       (start-process
2261                        "article-x-face" nil shell-file-name
2262                        shell-command-switch gnus-article-x-face-command))
2263                      (with-temp-buffer
2264                        (insert face)
2265                        (process-send-region "article-x-face"
2266                                             (point-min) (point-max)))
2267                      (process-send-eof "article-x-face")))
2268                   ((functionp gnus-article-x-face-command)
2269                    ;; The command is a lisp function, so we call it.
2270                    (funcall gnus-article-x-face-command face))
2271                   (t
2272                    (error "%s is not a function"
2273                           gnus-article-x-face-command)))))))))
2274
2275 (defun article-decode-mime-words ()
2276   "Decode all MIME-encoded words in the article."
2277   (interactive)
2278   (save-excursion
2279     (set-buffer gnus-article-buffer)
2280     (let ((inhibit-point-motion-hooks t)
2281           buffer-read-only
2282           (mail-parse-charset gnus-newsgroup-charset)
2283           (mail-parse-ignored-charsets
2284            (save-excursion (set-buffer gnus-summary-buffer)
2285                            gnus-newsgroup-ignored-charsets)))
2286       (mail-decode-encoded-word-region (point-min) (point-max)))))
2287
2288 (defun article-decode-charset (&optional prompt)
2289   "Decode charset-encoded text in the article.
2290 If PROMPT (the prefix), prompt for a coding system to use."
2291   (interactive "P")
2292   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2293         buffer-read-only
2294         (mail-parse-charset gnus-newsgroup-charset)
2295         (mail-parse-ignored-charsets
2296          (save-excursion (condition-case nil
2297                              (set-buffer gnus-summary-buffer)
2298                            (error))
2299                          gnus-newsgroup-ignored-charsets))
2300         ct cte ctl charset format)
2301     (save-excursion
2302       (save-restriction
2303         (article-narrow-to-head)
2304         (setq ct (message-fetch-field "Content-Type" t)
2305               cte (message-fetch-field "Content-Transfer-Encoding" t)
2306               ctl (and ct (ignore-errors
2307                             (mail-header-parse-content-type ct)))
2308               charset (cond
2309                        (prompt
2310                         (mm-read-coding-system "Charset to decode: "))
2311                        (ctl
2312                         (mail-content-type-get ctl 'charset)))
2313               format (and ctl (mail-content-type-get ctl 'format)))
2314         (when cte
2315           (setq cte (mail-header-strip cte)))
2316         (if (and ctl (not (string-match "/" (car ctl))))
2317             (setq ctl nil))
2318         (goto-char (point-max)))
2319       (forward-line 1)
2320       (save-restriction
2321         (narrow-to-region (point) (point-max))
2322         (when (and (eq mail-parse-charset 'gnus-decoded)
2323                    (eq (mm-body-7-or-8) '8bit))
2324           ;; The text code could have been decoded.
2325           (setq charset mail-parse-charset))
2326         (when (and (or (not ctl)
2327                        (equal (car ctl) "text/plain"))
2328                    (not format)) ;; article with format will decode later.
2329           (mm-decode-body
2330            charset (and cte (intern (downcase
2331                                      (gnus-strip-whitespace cte))))
2332            (car ctl)))))))
2333
2334 (defun article-decode-encoded-words ()
2335   "Remove encoded-word encoding from headers."
2336   (let (buffer-read-only)
2337     (let ((charset (save-excursion
2338                      (set-buffer gnus-summary-buffer)
2339                      default-mime-charset)))
2340       (mime-decode-header-in-buffer charset))))
2341
2342 (defun article-decode-group-name ()
2343   "Decode group names in `Newsgroups:'."
2344   (let ((inhibit-point-motion-hooks t)
2345         buffer-read-only
2346         (method (gnus-find-method-for-group gnus-newsgroup-name)))
2347     (when (and (or gnus-group-name-charset-method-alist
2348                    gnus-group-name-charset-group-alist)
2349                (gnus-buffer-live-p gnus-original-article-buffer))
2350       (save-restriction
2351         (article-narrow-to-head)
2352         (with-current-buffer gnus-original-article-buffer
2353           (goto-char (point-min)))
2354         (while (re-search-forward
2355                 "^Newsgroups:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]" nil t)
2356           (replace-match (save-match-data
2357                            (gnus-decode-newsgroups
2358                             ;; XXX how to use data in article buffer?
2359                             (with-current-buffer gnus-original-article-buffer
2360                               (re-search-forward
2361                                "^Newsgroups:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]"
2362                                nil t)
2363                               (match-string 1))
2364                             gnus-newsgroup-name method))
2365                          t t nil 1))
2366         (goto-char (point-min))
2367         (with-current-buffer gnus-original-article-buffer
2368           (goto-char (point-min)))
2369         (while (re-search-forward
2370                 "^Followup-To:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]" nil t)
2371           (replace-match (save-match-data
2372                            (gnus-decode-newsgroups
2373                             ;; XXX how to use data in article buffer?
2374                             (with-current-buffer gnus-original-article-buffer
2375                               (re-search-forward
2376                                "^Followup-To:\\(\\(.\\|\n[\t ]\\)*\\)\n[^\t ]"
2377                                nil t)
2378                               (match-string 1))
2379                             gnus-newsgroup-name method))
2380                          t t nil 1))))))
2381
2382 (autoload 'idna-to-unicode "idna")
2383
2384 (defun article-decode-idna-rhs ()
2385   "Decode IDNA strings in RHS in From:, To: and Cc: headers in current buffer."
2386   (when gnus-use-idna
2387     (save-restriction
2388       (let ((inhibit-point-motion-hooks t)
2389             buffer-read-only)
2390         (article-narrow-to-head)
2391         (goto-char (point-min))
2392         (while (re-search-forward "\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2393           (let (ace unicode)
2394             (when (save-match-data
2395                     (and (setq ace (match-string 1))
2396                          (save-excursion
2397                            (and (re-search-backward "^[^ \t]" nil t)
2398                                 (looking-at "From\\|To\\|Cc")))
2399                          (save-excursion (backward-char)
2400                                          (message-idna-inside-rhs-p))
2401                          (setq unicode (idna-to-unicode ace))))
2402               (unless (string= ace unicode)
2403                 (replace-match unicode nil nil nil 1)))))))))
2404
2405 (defun article-de-quoted-unreadable (&optional force read-charset)
2406   "Translate a quoted-printable-encoded article.
2407 If FORCE, decode the article whether it is marked as quoted-printable
2408 or not.
2409 If READ-CHARSET, ask for a coding system."
2410   (interactive (list 'force current-prefix-arg))
2411   (save-excursion
2412     (let ((buffer-read-only nil) type charset)
2413       (if (gnus-buffer-live-p gnus-original-article-buffer)
2414           (with-current-buffer gnus-original-article-buffer
2415             (setq type
2416                   (gnus-fetch-field "content-transfer-encoding"))
2417             (let* ((ct (gnus-fetch-field "content-type"))
2418                    (ctl (and ct
2419                              (ignore-errors
2420                                (mail-header-parse-content-type ct)))))
2421               (setq charset (and ctl
2422                                  (mail-content-type-get ctl 'charset)))
2423               (if (stringp charset)
2424                   (setq charset (intern (downcase charset)))))))
2425       (if read-charset
2426           (setq charset (mm-read-coding-system "Charset: " charset)))
2427       (unless charset
2428         (setq charset gnus-newsgroup-charset))
2429       (when (or force
2430                 (and type (let ((case-fold-search t))
2431                             (string-match "quoted-printable" type))))
2432         (article-goto-body)
2433         (quoted-printable-decode-region
2434          (point) (point-max) (mm-charset-to-coding-system charset))))))
2435
2436 (defun article-de-base64-unreadable (&optional force read-charset)
2437   "Translate a base64 article.
2438 If FORCE, decode the article whether it is marked as base64 not.
2439 If READ-CHARSET, ask for a coding system."
2440   (interactive (list 'force current-prefix-arg))
2441   (save-excursion
2442     (let ((buffer-read-only nil) type charset)
2443       (if (gnus-buffer-live-p gnus-original-article-buffer)
2444           (with-current-buffer gnus-original-article-buffer
2445             (setq type
2446                   (gnus-fetch-field "content-transfer-encoding"))
2447             (let* ((ct (gnus-fetch-field "content-type"))
2448                    (ctl (and ct
2449                              (ignore-errors
2450                                (mail-header-parse-content-type ct)))))
2451               (setq charset (and ctl
2452                                  (mail-content-type-get ctl 'charset)))
2453               (if (stringp charset)
2454                   (setq charset (intern (downcase charset)))))))
2455       (if read-charset
2456           (setq charset (mm-read-coding-system "Charset: " charset)))
2457       (unless charset
2458         (setq charset gnus-newsgroup-charset))
2459       (when (or force
2460                 (and type (let ((case-fold-search t))
2461                             (string-match "base64" type))))
2462         (article-goto-body)
2463         (save-restriction
2464           (narrow-to-region (point) (point-max))
2465           (base64-decode-region (point-min) (point-max))
2466           (mm-decode-coding-region
2467            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2468
2469 (eval-when-compile
2470   (require 'rfc1843))
2471
2472 (defun article-decode-HZ ()
2473   "Translate a HZ-encoded article."
2474   (interactive)
2475   (require 'rfc1843)
2476   (save-excursion
2477     (let ((buffer-read-only nil))
2478       (rfc1843-decode-region (point-min) (point-max)))))
2479
2480 (defun article-unsplit-urls ()
2481   "Remove the newlines that some other mailers insert into URLs."
2482   (interactive)
2483   (save-excursion
2484     (let ((buffer-read-only nil))
2485       (goto-char (point-min))
2486       (while (re-search-forward
2487               "^\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2488         (replace-match "\\1\\3" t)))
2489     (when (interactive-p)
2490       (gnus-treat-article nil))))
2491
2492
2493 (defun article-wash-html (&optional read-charset)
2494   "Format an HTML article.
2495 If READ-CHARSET, ask for a coding system."
2496   (interactive "P")
2497   (save-excursion
2498     (let ((buffer-read-only nil)
2499           charset)
2500       (when (gnus-buffer-live-p gnus-original-article-buffer)
2501         (with-current-buffer gnus-original-article-buffer
2502           (let* ((ct (gnus-fetch-field "content-type"))
2503                  (ctl (and ct
2504                            (ignore-errors
2505                              (mail-header-parse-content-type ct)))))
2506             (setq charset (and ctl
2507                                (mail-content-type-get ctl 'charset)))
2508             (when (stringp charset)
2509               (setq charset (intern (downcase charset)))))))
2510       (when read-charset
2511         (setq charset (mm-read-coding-system "Charset: " charset)))
2512       (unless charset
2513         (setq charset gnus-newsgroup-charset))
2514       (article-goto-body)
2515       (save-window-excursion
2516         (save-restriction
2517           (narrow-to-region (point) (point-max))
2518           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2519                  (entry (assq func mm-text-html-washer-alist)))
2520             (when entry
2521               (setq func (cdr entry)))
2522             (cond
2523              ((functionp func)
2524               (funcall func))
2525              (t
2526               (apply (car func) (cdr func))))))))))
2527
2528 (defun gnus-article-wash-html-with-w3 ()
2529   "Wash the current buffer with w3."
2530   (mm-setup-w3)
2531   (let ((w3-strict-width (window-width))
2532         (url-standalone-mode t)
2533         (url-gateway-unplugged t)
2534         (w3-honor-stylesheets nil))
2535     (condition-case ()
2536         (w3-region (point-min) (point-max))
2537       (error))))
2538
2539 (defun gnus-article-wash-html-with-w3m ()
2540   "Wash the current buffer with emacs-w3m."
2541   (mm-setup-w3m)
2542   (save-restriction
2543     (narrow-to-region (point) (point-max))
2544     (let ((w3m-safe-url-regexp (if mm-inline-text-html-with-images
2545                                    nil
2546                                  "\\`cid:"))
2547           w3m-force-redisplay)
2548       (w3m-region (point-min) (point-max)))
2549     (when mm-inline-text-html-with-w3m-keymap
2550       (add-text-properties
2551        (point-min) (point-max)
2552        (nconc (mm-w3m-local-map-property)
2553               '(mm-inline-text-html-with-w3m t))))))
2554
2555 (defun article-hide-list-identifiers ()
2556   "Remove list identifies from the Subject header.
2557 The `gnus-list-identifiers' variable specifies what to do."
2558   (interactive)
2559   (let ((inhibit-point-motion-hooks t)
2560         (regexp (if (consp gnus-list-identifiers)
2561                     (mapconcat 'identity gnus-list-identifiers " *\\|")
2562                   gnus-list-identifiers))
2563         buffer-read-only)
2564     (when regexp
2565       (save-excursion
2566         (save-restriction
2567           (article-narrow-to-head)
2568           (goto-char (point-min))
2569           (while (re-search-forward
2570                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
2571                   nil t)
2572             (delete-region (match-beginning 2) (match-end 0))
2573             (beginning-of-line))
2574           (when (re-search-forward
2575                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
2576             (delete-region (match-beginning 1) (match-end 1))))))))
2577
2578 (defun article-hide-pem (&optional arg)
2579   "Toggle hiding of any PEM headers and signatures in the current article.
2580 If given a negative prefix, always show; if given a positive prefix,
2581 always hide."
2582   (interactive (gnus-article-hidden-arg))
2583   (unless (gnus-article-check-hidden-text 'pem arg)
2584     (save-excursion
2585       (let (buffer-read-only end)
2586         (goto-char (point-min))
2587         ;; Hide the horrendously ugly "header".
2588         (when (and (search-forward
2589                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
2590                     nil t)
2591                    (setq end (1+ (match-beginning 0))))
2592           (gnus-add-wash-type 'pem)
2593           (gnus-article-hide-text-type
2594            end
2595            (if (search-forward "\n\n" nil t)
2596                (match-end 0)
2597              (point-max))
2598            'pem)
2599           ;; Hide the trailer as well
2600           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
2601                                 nil t)
2602             (gnus-article-hide-text-type
2603              (match-beginning 0) (match-end 0) 'pem)))))))
2604
2605 (defun article-strip-banner ()
2606   "Strip the banners specified by the `banner' group parameter and by
2607 `gnus-article-address-banner-alist'."
2608   (interactive)
2609   (save-excursion
2610     (save-restriction
2611       (let ((inhibit-point-motion-hooks t))
2612         (when (gnus-parameter-banner gnus-newsgroup-name)
2613           (article-really-strip-banner
2614            (gnus-parameter-banner gnus-newsgroup-name)))
2615         (when gnus-article-address-banner-alist
2616           (article-really-strip-banner
2617            (let ((from (save-restriction
2618                          (widen)
2619                          (article-narrow-to-head)
2620                          (mail-fetch-field "from"))))
2621              (when (and from
2622                         (setq from
2623                               (caar (mail-header-parse-addresses from))))
2624                (catch 'found
2625                  (dolist (pair gnus-article-address-banner-alist)
2626                    (when (string-match (car pair) from)
2627                      (throw 'found (cdr pair)))))))))))))
2628
2629 (defun article-really-strip-banner (banner)
2630   "Strip the banner specified by the argument."
2631   (save-excursion
2632     (save-restriction
2633       (let ((inhibit-point-motion-hooks t)
2634             (gnus-signature-limit nil)
2635             buffer-read-only)
2636         (article-goto-body)
2637         (cond
2638          ((eq banner 'signature)
2639           (when (gnus-article-narrow-to-signature)
2640             (widen)
2641             (forward-line -1)
2642             (delete-region (point) (point-max))))
2643          ((symbolp banner)
2644           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
2645               (while (re-search-forward banner nil t)
2646                 (delete-region (match-beginning 0) (match-end 0)))))
2647          ((stringp banner)
2648           (while (re-search-forward banner nil t)
2649             (delete-region (match-beginning 0) (match-end 0)))))))))
2650
2651 (defun article-babel ()
2652   "Translate article using an online translation service."
2653   (interactive)
2654   (require 'babel)
2655   (save-excursion
2656     (set-buffer gnus-article-buffer)
2657     (when (article-goto-body)
2658       (let* ((buffer-read-only nil)
2659              (start (point))
2660              (end (point-max))
2661              (orig (buffer-substring start end))
2662              (trans (babel-as-string orig)))
2663         (save-restriction
2664           (narrow-to-region start end)
2665           (delete-region start end)
2666           (insert trans))))))
2667
2668 (defun article-hide-signature (&optional arg)
2669   "Hide the signature in the current article.
2670 If given a negative prefix, always show; if given a positive prefix,
2671 always hide."
2672   (interactive (gnus-article-hidden-arg))
2673   (save-excursion
2674     (save-restriction
2675       (if (interactive-p)
2676           (progn
2677             (widen)
2678             (article-goto-body))
2679         (goto-char (point-min)))
2680       (unless (gnus-article-check-hidden-text 'signature arg)
2681         (let ((buffer-read-only nil)
2682               (button (point)))
2683           (while (setq button (text-property-any button (point-max)
2684                                                  'gnus-callback
2685                                                  'gnus-signature-toggle))
2686             (setq button (text-property-not-all button (point-max)
2687                                                 'gnus-callback
2688                                                 'gnus-signature-toggle))
2689             (when (and button (not (eobp)))
2690               (gnus-article-hide-text-type
2691                (1+ button)
2692                (next-single-property-change (1+ button) 'mime-view-entity
2693                                             nil (point-max))
2694                'signature)))))))
2695   (gnus-set-mode-line 'article))
2696
2697 (defun article-strip-headers-in-body ()
2698   "Strip offensive headers from bodies."
2699   (interactive)
2700   (save-excursion
2701     (article-goto-body)
2702     (let ((case-fold-search t))
2703       (when (looking-at "x-no-archive:")
2704         (gnus-delete-line)))))
2705
2706 (defun article-strip-leading-blank-lines ()
2707   "Remove all blank lines from the beginning of the article."
2708   (interactive)
2709   (save-excursion
2710     (let ((inhibit-point-motion-hooks t)
2711           buffer-read-only)
2712       (when (article-goto-body)
2713         (while (and (not (eobp))
2714                     (looking-at "[ \t]*$"))
2715           (gnus-delete-line))))))
2716
2717 (defun article-narrow-to-head ()
2718   "Narrow the buffer to the head of the message.
2719 Point is left at the beginning of the narrowed-to region."
2720   (narrow-to-region
2721    (goto-char (point-min))
2722    (if (search-forward "\n\n" nil 1)
2723        (1- (point))
2724      (point-max)))
2725   (goto-char (point-min)))
2726
2727 (defun article-goto-body ()
2728   "Place point at the start of the body."
2729   (goto-char (point-min))
2730   (cond
2731    ;; This variable is only bound when dealing with separate
2732    ;; MIME body parts.
2733    (article-goto-body-goes-to-point-min-p
2734     t)
2735    ((search-forward "\n\n" nil t)
2736     t)
2737    (t
2738     (goto-char (point-max))
2739     nil)))
2740
2741 (defun article-strip-multiple-blank-lines ()
2742   "Replace consecutive blank lines with one empty line."
2743   (interactive)
2744   (save-excursion
2745     (let ((inhibit-point-motion-hooks t)
2746           buffer-read-only)
2747       ;; First make all blank lines empty.
2748       (article-goto-body)
2749       (while (re-search-forward "^[ \t]+$" nil t)
2750         (unless (gnus-annotation-in-region-p
2751                  (match-beginning 0) (match-end 0))
2752           (replace-match "" nil t)))
2753       ;; Then replace multiple empty lines with a single empty line.
2754       (article-goto-body)
2755       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
2756         (unless (gnus-annotation-in-region-p
2757                  (match-beginning 0) (match-end 0))
2758           (delete-region (match-beginning 1) (match-end 1)))))))
2759
2760 (defun article-strip-leading-space ()
2761   "Remove all white space from the beginning of the lines in the article."
2762   (interactive)
2763   (save-excursion
2764     (let ((inhibit-point-motion-hooks t)
2765           buffer-read-only)
2766       (article-goto-body)
2767       (while (re-search-forward "^[ \t]+" nil t)
2768         (replace-match "" t t)))))
2769
2770 (defun article-strip-trailing-space ()
2771   "Remove all white space from the end of the lines in the article."
2772   (interactive)
2773   (save-excursion
2774     (let ((inhibit-point-motion-hooks t)
2775           buffer-read-only)
2776       (article-goto-body)
2777       (while (re-search-forward "[ \t]+$" nil t)
2778         (replace-match "" t t)))))
2779
2780 (defun article-strip-blank-lines ()
2781   "Strip leading, trailing and multiple blank lines."
2782   (interactive)
2783   (article-strip-leading-blank-lines)
2784   (article-remove-trailing-blank-lines)
2785   (article-strip-multiple-blank-lines))
2786
2787 (defun article-strip-all-blank-lines ()
2788   "Strip all blank lines."
2789   (interactive)
2790   (save-excursion
2791     (let ((inhibit-point-motion-hooks t)
2792           buffer-read-only)
2793       (article-goto-body)
2794       (while (re-search-forward "^[ \t]*\n" nil t)
2795         (replace-match "" t t)))))
2796
2797 (defun gnus-article-narrow-to-signature ()
2798   "Narrow to the signature; return t if a signature is found, else nil."
2799   (let ((inhibit-point-motion-hooks t))
2800     (when (gnus-article-search-signature)
2801       (forward-line 1)
2802       ;; Check whether we have some limits to what we consider
2803       ;; to be a signature.
2804       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2805                       (list gnus-signature-limit)))
2806             limit limited)
2807         (while (setq limit (pop limits))
2808           (if (or (and (integerp limit)
2809                        (< (- (point-max) (point)) limit))
2810                   (and (floatp limit)
2811                        (< (count-lines (point) (point-max)) limit))
2812                   (and (functionp limit)
2813                        (funcall limit))
2814                   (and (stringp limit)
2815                        (not (re-search-forward limit nil t))))
2816               ()                        ; This limit did not succeed.
2817             (setq limited t
2818                   limits nil)))
2819         (unless limited
2820           (narrow-to-region (point) (point-max))
2821           t)))))
2822
2823 (defun gnus-article-search-signature ()
2824   "Search the current buffer for the signature separator.
2825 Put point at the beginning of the signature separator."
2826   (let ((cur (point)))
2827     (goto-char (point-max))
2828     (if (if (stringp gnus-signature-separator)
2829             (re-search-backward gnus-signature-separator nil t)
2830           (let ((seps gnus-signature-separator))
2831             (while (and seps
2832                         (not (re-search-backward (car seps) nil t)))
2833               (pop seps))
2834             seps))
2835         t
2836       (goto-char cur)
2837       nil)))
2838
2839 (defun gnus-article-hidden-arg ()
2840   "Return the current prefix arg as a number, or 0 if no prefix."
2841   (list (if current-prefix-arg
2842             (prefix-numeric-value current-prefix-arg)
2843           0)))
2844
2845 (defun gnus-article-check-hidden-text (type arg)
2846   "Return nil if hiding is necessary.
2847 Arg can be nil or a number.  nil and positive means hide, negative
2848 means show, 0 means toggle."
2849   (save-excursion
2850     (save-restriction
2851       (let ((hide (gnus-article-hidden-text-p type)))
2852         (cond
2853          ((or (null arg)
2854               (> arg 0))
2855           nil)
2856          ((< arg 0)
2857           (gnus-article-show-hidden-text type)
2858           t)
2859          (t
2860           (if (eq hide 'hidden)
2861               (progn
2862                 (gnus-article-show-hidden-text type)
2863                 t)
2864             nil)))))))
2865
2866 (defun gnus-article-hidden-text-p (type)
2867   "Say whether the current buffer contains hidden text of type TYPE."
2868   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
2869     (while (and pos
2870                 (not (get-text-property pos 'invisible))
2871                 (not (get-text-property pos 'dummy-invisible)))
2872       (setq pos
2873             (text-property-any (1+ pos) (point-max) 'article-type type)))
2874     (if pos
2875         'hidden
2876       nil)))
2877
2878 (defun gnus-article-show-hidden-text (type &optional dummy)
2879   "Show all hidden text of type TYPE.
2880 Originally it is hide instead of DUMMY."
2881   (let ((buffer-read-only nil)
2882         (inhibit-point-motion-hooks t))
2883     (gnus-remove-text-properties-when
2884      'article-type type
2885      (point-min) (point-max)
2886      (cons 'article-type (cons type
2887                                gnus-hidden-properties)))
2888     (gnus-delete-wash-type type)))
2889
2890 (defconst article-time-units
2891   `((year . ,(* 365.25 24 60 60))
2892     (week . ,(* 7 24 60 60))
2893     (day . ,(* 24 60 60))
2894     (hour . ,(* 60 60))
2895     (minute . 60)
2896     (second . 1))
2897   "Mapping from time units to seconds.")
2898
2899 (defun gnus-article-forward-header ()
2900   "Move point to the start of the next header.
2901 If the current header is a continuation header, this can be several
2902 lines forward."
2903   (let ((ended nil))
2904     (while (not ended)
2905       (forward-line 1)
2906       (if (looking-at "[ \t]+[^ \t]")
2907           (forward-line 1)
2908         (setq ended t)))))
2909
2910 (defun article-date-ut (&optional type highlight header)
2911   "Convert DATE date to universal time in the current article.
2912 If TYPE is `local', convert to local time; if it is `lapsed', output
2913 how much time has lapsed since DATE.  For `lapsed', the value of
2914 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
2915 should replace the \"Date:\" one, or should be added below it."
2916   (interactive (list 'ut t))
2917   (let* ((header (or header
2918                      (and (eq 1 (point-min))
2919                           (mail-header-date (save-excursion
2920                                               (set-buffer gnus-summary-buffer)
2921                                               gnus-current-headers)))
2922                      (message-fetch-field "date")
2923                      ""))
2924          (date (if (vectorp header) (mail-header-date header)
2925                  header))
2926          (inhibit-point-motion-hooks t)
2927          bface eface date-pos)
2928     (when (and date (not (string= date "")))
2929       (save-excursion
2930         (save-restriction
2931           (article-narrow-to-head)
2932           (when (or (and (eq type 'lapsed)
2933                          gnus-article-date-lapsed-new-header
2934                          ;; Attempt to get the face of X-Sent first.
2935                          (re-search-forward "^X-Sent:[ \t]" nil t))
2936                     (re-search-forward "^Date:[ \t]" nil t)
2937                     ;; If Date is missing, try again for X-Sent.
2938                     (re-search-forward "^X-Sent:[ \t]" nil t))
2939             (setq bface (get-text-property (point-at-bol) 'face)
2940                   date (or (get-text-property (point-at-bol)
2941                                               'original-date)
2942                            date)
2943                   eface (get-text-property (1- (point-at-eol))
2944                                            'face)))
2945           (let ((buffer-read-only nil))
2946             ;; Delete any old X-Sent headers.
2947             (when (setq date-pos
2948                         (text-property-any (point-min) (point-max)
2949                                            'article-date-lapsed t))
2950               (goto-char (setq date-pos (set-marker (make-marker) date-pos)))
2951               (delete-region (match-beginning 0)
2952                              (progn (forward-line 1) (point))))
2953             (goto-char (point-min))
2954             ;; Delete any old Date headers.
2955             (while (re-search-forward "^Date:[ \t]" nil t)
2956               (unless date-pos
2957                 (setq date-pos (match-beginning 0)))
2958               (unless (and (eq type 'lapsed)
2959                            gnus-article-date-lapsed-new-header)
2960                 (delete-region (match-beginning 0)
2961                                (progn (message-next-header) (point)))))
2962             (if date-pos
2963                 (progn
2964                   (goto-char date-pos)
2965                   (unless (bolp)
2966                     ;; Possibly, Date has been deleted.
2967                     (insert "\n"))
2968                   (when (and (eq type 'lapsed)
2969                              gnus-article-date-lapsed-new-header
2970                              (looking-at "Date:"))
2971                     (forward-line 1)))
2972               (goto-char (point-min)))
2973             (insert (article-make-date-line date type))
2974             (when (eq type 'lapsed)
2975               (put-text-property (point-at-bol) (point)
2976                                  'article-date-lapsed t))
2977             (insert "\n")
2978             (forward-line -1)
2979             ;; Do highlighting.
2980             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
2981               (put-text-property (match-beginning 1) (1+ (match-end 1))
2982                                  'original-date date)
2983               (put-text-property (match-beginning 1) (1+ (match-end 1))
2984                                  'face bface)
2985               (put-text-property (match-beginning 2) (match-end 2)
2986                                  'face eface))))))))
2987
2988 (defun article-make-date-line (date type)
2989   "Return a DATE line of TYPE."
2990   (unless (memq type '(local ut original user iso8601 lapsed english))
2991     (error "Unknown conversion type: %s" type))
2992   (condition-case ()
2993       (let ((time (date-to-time date)))
2994         (cond
2995          ;; Convert to the local timezone.
2996          ((eq type 'local)
2997           (let ((tz (car (current-time-zone time))))
2998             (format "Date: %s %s%02d%02d" (current-time-string time)
2999                     (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3000                     (/ (% (abs tz) 3600) 60))))
3001          ;; Convert to Universal Time.
3002          ((eq type 'ut)
3003           (concat "Date: "
3004                   (current-time-string
3005                    (let* ((e (parse-time-string date))
3006                           (tm (apply 'encode-time e))
3007                           (ms (car tm))
3008                           (ls (- (cadr tm) (car (current-time-zone time)))))
3009                      (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3010                            ((> ls 65535) (list (1+ ms) (- ls 65536)))
3011                            (t (list ms ls)))))
3012                   " UT"))
3013          ;; Get the original date from the article.
3014          ((eq type 'original)
3015           (concat "Date: " (if (string-match "\n+$" date)
3016                                (substring date 0 (match-beginning 0))
3017                              date)))
3018          ;; Let the user define the format.
3019          ((eq type 'user)
3020           (let ((format (or (condition-case nil
3021                                 (with-current-buffer gnus-summary-buffer
3022                                   gnus-article-time-format)
3023                               (error nil))
3024                             gnus-article-time-format)))
3025             (if (functionp format)
3026                 (funcall format time)
3027               (concat "Date: " (format-time-string format time)))))
3028          ;; ISO 8601.
3029          ((eq type 'iso8601)
3030           (let ((tz (car (current-time-zone time))))
3031             (concat
3032              "Date: "
3033              (format-time-string "%Y%m%dT%H%M%S" time)
3034              (format "%s%02d%02d"
3035                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3036                      (/ (% (abs tz) 3600) 60)))))
3037          ;; Do an X-Sent lapsed format.
3038          ((eq type 'lapsed)
3039           ;; If the date is seriously mangled, the timezone functions are
3040           ;; liable to bug out, so we ignore all errors.
3041           (let* ((now (current-time))
3042                  (real-time (subtract-time now time))
3043                  (real-sec (and real-time
3044                                 (+ (* (float (car real-time)) 65536)
3045                                    (cadr real-time))))
3046                  (sec (and real-time (abs real-sec)))
3047                  num prev)
3048             (cond
3049              ((null real-time)
3050               "X-Sent: Unknown")
3051              ((zerop sec)
3052               "X-Sent: Now")
3053              (t
3054               (concat
3055                "X-Sent: "
3056                ;; This is a bit convoluted, but basically we go
3057                ;; through the time units for years, weeks, etc,
3058                ;; and divide things to see whether that results
3059                ;; in positive answers.
3060                (mapconcat
3061                 (lambda (unit)
3062                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3063                       ;; The (remaining) seconds are too few to
3064                       ;; be divided into this time unit.
3065                       ""
3066                     ;; It's big enough, so we output it.
3067                     (setq sec (- sec (* num (cdr unit))))
3068                     (prog1
3069                         (concat (if prev ", " "") (int-to-string
3070                                                    (floor num))
3071                                 " " (symbol-name (car unit))
3072                                 (if (> num 1) "s" ""))
3073                       (setq prev t))))
3074                 article-time-units "")
3075                ;; If dates are odd, then it might appear like the
3076                ;; article was sent in the future.
3077                (if (> real-sec 0)
3078                    " ago"
3079                  " in the future"))))))
3080          ;; Display the date in proper English
3081          ((eq type 'english)
3082           (let ((dtime (decode-time time)))
3083             (concat
3084              "Date: the "
3085              (number-to-string (nth 3 dtime))
3086              (let ((digit (% (nth 3 dtime) 10)))
3087                (cond
3088                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3089                 ((= digit 1) "st")
3090                 ((= digit 2) "nd")
3091                 ((= digit 3) "rd")
3092                 (t "th")))
3093              " of "
3094              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3095              " "
3096              (number-to-string (nth 5 dtime))
3097              " at "
3098              (format "%02d" (nth 2 dtime))
3099              ":"
3100              (format "%02d" (nth 1 dtime)))))))
3101     (error
3102      (format "Date: %s (from T-gnus)" date))))
3103
3104 (defun article-date-local (&optional highlight)
3105   "Convert the current article date to the local timezone."
3106   (interactive (list t))
3107   (article-date-ut 'local highlight))
3108
3109 (defun article-date-english (&optional highlight)
3110   "Convert the current article date to something that is proper English."
3111   (interactive (list t))
3112   (article-date-ut 'english highlight))
3113
3114 (defun article-date-original (&optional highlight)
3115   "Convert the current article date to what it was originally.
3116 This is only useful if you have used some other date conversion
3117 function and want to see what the date was before converting."
3118   (interactive (list t))
3119   (article-date-ut 'original highlight))
3120
3121 (defun article-date-lapsed (&optional highlight)
3122   "Convert the current article date to time lapsed since it was sent."
3123   (interactive (list t))
3124   (article-date-ut 'lapsed highlight))
3125
3126 (defun article-update-date-lapsed ()
3127   "Function to be run from a timer to update the lapsed time line."
3128   (let (deactivate-mark)
3129     (save-excursion
3130       (ignore-errors
3131         (walk-windows
3132          (lambda (w)
3133            (set-buffer (window-buffer w))
3134            (when (eq major-mode 'gnus-article-mode)
3135              (let ((mark (point-marker)))
3136                (goto-char (point-min))
3137                (when (re-search-forward "^X-Sent:" nil t)
3138                  (article-date-lapsed t))
3139                (goto-char (marker-position mark))
3140                (move-marker mark nil))))
3141          nil 'visible)))))
3142
3143 (defun gnus-start-date-timer (&optional n)
3144   "Start a timer to update the X-Sent header in the article buffers.
3145 The numerical prefix says how frequently (in seconds) the function
3146 is to run."
3147   (interactive "p")
3148   (unless n
3149     (setq n 1))
3150   (gnus-stop-date-timer)
3151   (setq article-lapsed-timer
3152         (run-at-time 1 n 'article-update-date-lapsed)))
3153
3154 (defun gnus-stop-date-timer ()
3155   "Stop the X-Sent timer."
3156   (interactive)
3157   (when article-lapsed-timer
3158     (nnheader-cancel-timer article-lapsed-timer)
3159     (setq article-lapsed-timer nil)))
3160
3161 (defun article-date-user (&optional highlight)
3162   "Convert the current article date to the user-defined format.
3163 This format is defined by the `gnus-article-time-format' variable."
3164   (interactive (list t))
3165   (article-date-ut 'user highlight))
3166
3167 (defun article-date-iso8601 (&optional highlight)
3168   "Convert the current article date to ISO8601."
3169   (interactive (list t))
3170   (article-date-ut 'iso8601 highlight))
3171
3172 ;; (defun article-show-all ()
3173 ;;   "Show all hidden text in the article buffer."
3174 ;;   (interactive)
3175 ;;   (save-excursion
3176 ;;     (widen)
3177 ;;     (let ((buffer-read-only nil))
3178 ;;       (gnus-article-unhide-text (point-min) (point-max))
3179 ;;       (gnus-remove-text-with-property 'gnus-prev)
3180 ;;       (gnus-remove-text-with-property 'gnus-next))))
3181
3182 (defun article-show-all-headers ()
3183   "Show all hidden headers in the article buffer."
3184   (interactive)
3185   (save-excursion
3186     (save-restriction
3187       (widen)
3188       (article-narrow-to-head)
3189       (let ((buffer-read-only nil))
3190         (gnus-article-unhide-text (point-min) (point-max))))))
3191
3192 (defun article-remove-leading-whitespace ()
3193   "Remove excessive whitespace from all headers."
3194   (interactive)
3195   (save-excursion
3196     (save-restriction
3197       (let ((buffer-read-only nil))
3198         (article-narrow-to-head)
3199         (goto-char (point-min))
3200         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3201           (delete-region (match-beginning 1) (match-end 1)))))))
3202
3203 (defun article-emphasize (&optional arg)
3204   "Emphasize text according to `gnus-emphasis-alist'."
3205   (interactive (gnus-article-hidden-arg))
3206   (unless (gnus-article-check-hidden-text 'emphasis arg)
3207     (save-excursion
3208       (let ((alist (or
3209                     (condition-case nil
3210                         (with-current-buffer gnus-summary-buffer
3211                           gnus-article-emphasis-alist)
3212                       (error))
3213                     gnus-emphasis-alist))
3214             (buffer-read-only nil)
3215             (props (append '(article-type emphasis)
3216                            gnus-hidden-properties))
3217             regexp elem beg invisible visible face)
3218         (article-goto-body)
3219         (setq beg (point))
3220         (while (setq elem (pop alist))
3221           (goto-char beg)
3222           (setq regexp (car elem)
3223                 invisible (nth 1 elem)
3224                 visible (nth 2 elem)
3225                 face (nth 3 elem))
3226           (while (re-search-forward regexp nil t)
3227             (when (and (match-beginning visible) (match-beginning invisible))
3228               (gnus-article-hide-text
3229                (match-beginning invisible) (match-end invisible) props)
3230               (gnus-article-unhide-text-type
3231                (match-beginning visible) (match-end visible) 'emphasis)
3232               (gnus-put-overlay-excluding-newlines
3233                (match-beginning visible) (match-end visible) 'face face)
3234               (gnus-add-wash-type 'emphasis)
3235               (goto-char (match-end invisible)))))))))
3236
3237 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3238   "Setup newsgroup emphasis alist."
3239   (unless gnus-article-emphasis-alist
3240     (let ((name (and gnus-newsgroup-name
3241                      (gnus-group-real-name gnus-newsgroup-name))))
3242       (make-local-variable 'gnus-article-emphasis-alist)
3243       (setq gnus-article-emphasis-alist
3244             (nconc
3245              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3246                (while (setq elem (pop alist))
3247                  (when (and name (string-match (car elem) name))
3248                    (setq alist nil
3249                          highlight (copy-sequence (cdr elem)))))
3250                highlight)
3251              (copy-sequence highlight-words)
3252              (if gnus-newsgroup-name
3253                  (copy-sequence (gnus-group-find-parameter
3254                                  gnus-newsgroup-name 'highlight-words t)))
3255              gnus-emphasis-alist)))))
3256
3257 (eval-when-compile
3258   (defvar gnus-summary-article-menu)
3259   (defvar gnus-summary-post-menu))
3260
3261 ;;; Saving functions.
3262
3263 (defun gnus-article-save (save-buffer file &optional num)
3264   "Save the currently selected article."
3265   (unless gnus-save-all-headers
3266     ;; Remove headers according to `gnus-saved-headers'.
3267     (let ((gnus-visible-headers
3268            (or gnus-saved-headers gnus-visible-headers))
3269           (gnus-article-buffer save-buffer))
3270       (save-excursion
3271         (set-buffer save-buffer)
3272         (article-hide-headers 1 t))))
3273   (save-window-excursion
3274     (if (not gnus-default-article-saver)
3275         (error "No default saver is defined")
3276       ;; !!! Magic!  The saving functions all save
3277       ;; `gnus-save-article-buffer' (or so they think), but we
3278       ;; bind that variable to our save-buffer.
3279       (set-buffer gnus-article-buffer)
3280       (let* ((gnus-save-article-buffer save-buffer)
3281              (filename
3282               (cond
3283                ((not gnus-prompt-before-saving) 'default)
3284                ((eq gnus-prompt-before-saving 'always) nil)
3285                (t file)))
3286              (gnus-number-of-articles-to-be-saved
3287               (when (eq gnus-prompt-before-saving t)
3288                 num)))                  ; Magic
3289         (set-buffer gnus-article-current-summary)
3290         (funcall gnus-default-article-saver filename)))))
3291
3292 (defun gnus-read-save-file-name (prompt &optional filename
3293                                         function group headers variable)
3294   (let ((default-name
3295           (funcall function group headers (symbol-value variable)))
3296         result)
3297     (setq result
3298           (expand-file-name
3299            (cond
3300             ((eq filename 'default)
3301              default-name)
3302             ((eq filename t)
3303              default-name)
3304             (filename filename)
3305             (t
3306              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3307                     (prompt
3308                      (format prompt
3309                              (if (and gnus-number-of-articles-to-be-saved
3310                                       (> gnus-number-of-articles-to-be-saved 1))
3311                                  (format "these %d articles"
3312                                          gnus-number-of-articles-to-be-saved)
3313                                "this article")))
3314                     (file
3315                      ;; Let the split methods have their say.
3316                      (cond
3317                       ;; No split name was found.
3318                       ((null split-name)
3319                        (read-file-name
3320                         (concat prompt " (default "
3321                                 (file-name-nondirectory default-name) ") ")
3322                         (file-name-directory default-name)
3323                         default-name))
3324                       ;; A single group name is returned.
3325                       ((stringp split-name)
3326                        (setq default-name
3327                              (funcall function split-name headers
3328                                       (symbol-value variable)))
3329                        (read-file-name
3330                         (concat prompt " (default "
3331                                 (file-name-nondirectory default-name) ") ")
3332                         (file-name-directory default-name)
3333                         default-name))
3334                       ;; A single split name was found
3335                       ((= 1 (length split-name))
3336                        (let* ((name (expand-file-name
3337                                      (car split-name)
3338                                      gnus-article-save-directory))
3339                               (dir (cond ((file-directory-p name)
3340                                           (file-name-as-directory name))
3341                                          ((file-exists-p name) name)
3342                                          (t gnus-article-save-directory))))
3343                          (read-file-name
3344                           (concat prompt " (default " name ") ")
3345                           dir name)))
3346                       ;; A list of splits was found.
3347                       (t
3348                        (setq split-name (nreverse split-name))
3349                        (let (result)
3350                          (let ((file-name-history
3351                                 (nconc split-name file-name-history)))
3352                            (setq result
3353                                  (expand-file-name
3354                                   (read-file-name
3355                                    (concat prompt " (`M-p' for defaults) ")
3356                                    gnus-article-save-directory
3357                                    (car split-name))
3358                                   gnus-article-save-directory)))
3359                          (car (push result file-name-history)))))))
3360                ;; Create the directory.
3361                (gnus-make-directory (file-name-directory file))
3362                ;; If we have read a directory, we append the default file name.
3363                (when (file-directory-p file)
3364                  (setq file (expand-file-name (file-name-nondirectory
3365                                                default-name)
3366                                               (file-name-as-directory file))))
3367                ;; Possibly translate some characters.
3368                (nnheader-translate-file-chars file))))))
3369     (gnus-make-directory (file-name-directory result))
3370     (set variable result)))
3371
3372 (defun gnus-article-archive-name (group)
3373   "Return the first instance of an \"Archive-name\" in the current buffer."
3374   (let ((case-fold-search t))
3375     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3376       (nnheader-concat gnus-article-save-directory
3377                        (match-string 1)))))
3378
3379 (defun gnus-article-nndoc-name (group)
3380   "If GROUP is an nndoc group, return the name of the parent group."
3381   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3382     (gnus-group-get-parameter group 'save-article-group)))
3383
3384 (defun gnus-summary-save-in-rmail (&optional filename)
3385   "Append this article to Rmail file.
3386 Optional argument FILENAME specifies file name.
3387 Directory to save to is default to `gnus-article-save-directory'."
3388   (setq filename (gnus-read-save-file-name
3389                   "Save %s in rmail file:" filename
3390                   gnus-rmail-save-name gnus-newsgroup-name
3391                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3392   (gnus-eval-in-buffer-window gnus-save-article-buffer
3393     (save-excursion
3394       (save-restriction
3395         (widen)
3396         (gnus-output-to-rmail filename))))
3397   filename)
3398
3399 (defun gnus-summary-save-in-mail (&optional filename)
3400   "Append this article to Unix mail file.
3401 Optional argument FILENAME specifies file name.
3402 Directory to save to is default to `gnus-article-save-directory'."
3403   (setq filename (gnus-read-save-file-name
3404                   "Save %s in Unix mail file:" filename
3405                   gnus-mail-save-name gnus-newsgroup-name
3406                   gnus-current-headers 'gnus-newsgroup-last-mail))
3407   (gnus-eval-in-buffer-window gnus-save-article-buffer
3408     (save-excursion
3409       (save-restriction
3410         (widen)
3411         (if (and (file-readable-p filename)
3412                  (file-regular-p filename)
3413                  (mail-file-babyl-p filename))
3414             (rmail-output-to-rmail-file filename t)
3415           (gnus-output-to-mail filename)))))
3416   filename)
3417
3418 (defun gnus-summary-save-in-file (&optional filename overwrite)
3419   "Append this article to file.
3420 Optional argument FILENAME specifies file name.
3421 Directory to save to is default to `gnus-article-save-directory'."
3422   (setq filename (gnus-read-save-file-name
3423                   "Save %s in file:" filename
3424                   gnus-file-save-name gnus-newsgroup-name
3425                   gnus-current-headers 'gnus-newsgroup-last-file))
3426   (gnus-eval-in-buffer-window gnus-save-article-buffer
3427     (save-excursion
3428       (save-restriction
3429         (widen)
3430         (when (and overwrite
3431                    (file-exists-p filename))
3432           (delete-file filename))
3433         (gnus-output-to-file filename))))
3434   filename)
3435
3436 (defun gnus-summary-write-to-file (&optional filename)
3437   "Write this article to a file, overwriting it if the file exists.
3438 Optional argument FILENAME specifies file name.
3439 The directory to save in defaults to `gnus-article-save-directory'."
3440   (gnus-summary-save-in-file nil t))
3441
3442 (defun gnus-summary-save-body-in-file (&optional filename)
3443   "Append this article body to a file.
3444 Optional argument FILENAME specifies file name.
3445 The directory to save in defaults to `gnus-article-save-directory'."
3446   (setq filename (gnus-read-save-file-name
3447                   "Save %s body in file:" filename
3448                   gnus-file-save-name gnus-newsgroup-name
3449                   gnus-current-headers 'gnus-newsgroup-last-file))
3450   (gnus-eval-in-buffer-window gnus-save-article-buffer
3451     (save-excursion
3452       (save-restriction
3453         (widen)
3454         (when (article-goto-body)
3455           (narrow-to-region (point) (point-max)))
3456         (gnus-output-to-file filename))))
3457   filename)
3458
3459 (defun gnus-summary-save-in-pipe (&optional command)
3460   "Pipe this article to subprocess."
3461   (setq command
3462         (cond ((and (eq command 'default)
3463                     gnus-last-shell-command)
3464                gnus-last-shell-command)
3465               ((stringp command)
3466                command)
3467               (t (read-string
3468                   (format
3469                    "Shell command on %s: "
3470                    (if (and gnus-number-of-articles-to-be-saved
3471                             (> gnus-number-of-articles-to-be-saved 1))
3472                        (format "these %d articles"
3473                                gnus-number-of-articles-to-be-saved)
3474                      "this article"))
3475                   gnus-last-shell-command))))
3476   (when (string-equal command "")
3477     (if gnus-last-shell-command
3478         (setq command gnus-last-shell-command)
3479       (error "A command is required")))
3480   (gnus-eval-in-buffer-window gnus-article-buffer
3481     (save-restriction
3482       (widen)
3483       (shell-command-on-region (point-min) (point-max) command nil)))
3484   (setq gnus-last-shell-command command))
3485
3486 (defun gnus-summary-pipe-to-muttprint (&optional command)
3487   "Pipe this article to muttprint."
3488   (setq command (read-string
3489                  "Print using command: " gnus-summary-muttprint-program
3490                  nil gnus-summary-muttprint-program))
3491   (gnus-summary-save-in-pipe command))
3492
3493 ;;; Article file names when saving.
3494
3495 (defun gnus-capitalize-newsgroup (newsgroup)
3496   "Capitalize NEWSGROUP name."
3497   (when (not (zerop (length newsgroup)))
3498     (concat (char-to-string (upcase (aref newsgroup 0)))
3499             (substring newsgroup 1))))
3500
3501 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
3502   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3503 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
3504 Otherwise, it is like ~/News/news/group/num."
3505   (let ((default
3506           (expand-file-name
3507            (concat (if (gnus-use-long-file-name 'not-save)
3508                        (gnus-capitalize-newsgroup newsgroup)
3509                      (gnus-newsgroup-directory-form newsgroup))
3510                    "/" (int-to-string (mail-header-number headers)))
3511            gnus-article-save-directory)))
3512     (if (and last-file
3513              (string-equal (file-name-directory default)
3514                            (file-name-directory last-file))
3515              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3516         default
3517       (or last-file default))))
3518
3519 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
3520   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3521 If variable `gnus-use-long-file-name' is non-nil, it is
3522 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
3523   (let ((default
3524           (expand-file-name
3525            (concat (if (gnus-use-long-file-name 'not-save)
3526                        newsgroup
3527                      (gnus-newsgroup-directory-form newsgroup))
3528                    "/" (int-to-string (mail-header-number headers)))
3529            gnus-article-save-directory)))
3530     (if (and last-file
3531              (string-equal (file-name-directory default)
3532                            (file-name-directory last-file))
3533              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3534         default
3535       (or last-file default))))
3536
3537 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
3538   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3539 If variable `gnus-use-long-file-name' is non-nil, it is
3540 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
3541   (or last-file
3542       (expand-file-name
3543        (if (gnus-use-long-file-name 'not-save)
3544            newsgroup
3545          (file-relative-name
3546           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
3547           default-directory))
3548        gnus-article-save-directory)))
3549
3550 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
3551   "Generate file name from sender."
3552   (let ((from (mail-header-from headers)))
3553     (expand-file-name
3554      (if (and from (string-match "\\([^ <]+\\)@" from))
3555          (match-string 1 from)
3556        "nobody")
3557      gnus-article-save-directory)))
3558
3559 (defun article-verify-x-pgp-sig ()
3560   "Verify X-PGP-Sig."
3561   (interactive)
3562   (if (gnus-buffer-live-p gnus-original-article-buffer)
3563       (let ((sig (with-current-buffer gnus-original-article-buffer
3564                    (gnus-fetch-field "X-PGP-Sig")))
3565             items info headers)
3566         (when (and sig
3567                    mml2015-use
3568                    (mml2015-clear-verify-function))
3569           (with-temp-buffer
3570             (insert-buffer-substring gnus-original-article-buffer)
3571             (setq items (split-string sig))
3572             (message-narrow-to-head)
3573             (let ((inhibit-point-motion-hooks t)
3574                   (case-fold-search t))
3575               ;; Don't verify multiple headers.
3576               (setq headers (mapconcat (lambda (header)
3577                                          (concat header ": "
3578                                                  (mail-fetch-field header)
3579                                                  "\n"))
3580                                        (split-string (nth 1 items) ",") "")))
3581             (delete-region (point-min) (point-max))
3582             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
3583             (insert "X-Signed-Headers: " (nth 1 items) "\n")
3584             (insert headers)
3585             (widen)
3586             (forward-line)
3587             (while (not (eobp))
3588               (if (looking-at "^-")
3589                   (insert "- "))
3590               (forward-line))
3591             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
3592             (insert "Version: " (car items) "\n\n")
3593             (insert (mapconcat 'identity (cddr items) "\n"))
3594             (insert "\n-----END PGP SIGNATURE-----\n")
3595             (let ((mm-security-handle (list (format "multipart/signed"))))
3596               (mml2015-clean-buffer)
3597               (let ((coding-system-for-write (or gnus-newsgroup-charset
3598                                                  'iso-8859-1)))
3599                 (funcall (mml2015-clear-verify-function)))
3600               (setq info
3601                     (or (mm-handle-multipart-ctl-parameter
3602                          mm-security-handle 'gnus-details)
3603                         (mm-handle-multipart-ctl-parameter
3604                          mm-security-handle 'gnus-info)))))
3605           (when info
3606             (let (buffer-read-only bface eface)
3607               (save-restriction
3608                 (message-narrow-to-head)
3609                 (goto-char (point-max))
3610                 (forward-line -1)
3611                 (setq bface (get-text-property (point-at-bol) 'face)
3612                       eface (get-text-property (1- (point-at-eol)) 'face))
3613                 (message-remove-header "X-Gnus-PGP-Verify")
3614                 (if (re-search-forward "^X-PGP-Sig:" nil t)
3615                     (forward-line)
3616                   (goto-char (point-max)))
3617                 (narrow-to-region (point) (point))
3618                 (insert "X-Gnus-PGP-Verify: " info "\n")
3619                 (goto-char (point-min))
3620                 (forward-line)
3621                 (while (not (eobp))
3622                   (if (not (looking-at "^[ \t]"))
3623                       (insert " "))
3624                   (forward-line))
3625                 ;; Do highlighting.
3626                 (goto-char (point-min))
3627                 (when (looking-at "\\([^:]+\\): *")
3628                   (put-text-property (match-beginning 1) (1+ (match-end 1))
3629                                      'face bface)
3630                   (put-text-property (match-end 0) (point-max)
3631                                      'face eface)))))))))
3632
3633 (defun article-verify-cancel-lock ()
3634   "Verify Cancel-Lock header."
3635   (interactive)
3636   (if (gnus-buffer-live-p gnus-original-article-buffer)
3637       (canlock-verify gnus-original-article-buffer)))
3638
3639 (defun article-monafy ()
3640   "Display body part with mona font."
3641   (interactive)
3642   (unless (if (featurep 'xemacs)
3643               (find-face 'gnus-mona-face)
3644             (facep 'gnus-mona-face))
3645     (require 'navi2ch-mona)
3646     (set-face-font (make-face 'gnus-mona-face) navi2ch-mona-font))
3647   (save-excursion
3648     (let ((buffer-read-only nil))
3649       (article-goto-body)
3650       (gnus-overlay-put
3651        (gnus-make-overlay (point) (point-max))
3652        'face 'gnus-mona-face))))
3653
3654 (eval-and-compile
3655   (mapcar
3656    (lambda (func)
3657      (let (afunc gfunc)
3658        (if (consp func)
3659            (setq afunc (car func)
3660                  gfunc (cdr func))
3661          (setq afunc func
3662                gfunc (intern (format "gnus-%s" func))))
3663        (defalias gfunc
3664          (when (fboundp afunc)
3665            `(lambda (&optional interactive &rest args)
3666               ,(documentation afunc t)
3667               (interactive (list t))
3668               (save-excursion
3669                 (set-buffer gnus-article-buffer)
3670                 (if interactive
3671                     (call-interactively ',afunc)
3672                   (apply ',afunc args))))))))
3673    '(article-hide-headers
3674      article-verify-x-pgp-sig
3675      article-verify-cancel-lock
3676      article-monafy
3677      article-hide-boring-headers
3678      article-treat-overstrike
3679      article-treat-ansi-sequences
3680      article-fill-long-lines
3681      article-capitalize-sentences
3682      article-remove-cr
3683      article-remove-leading-whitespace
3684      article-display-x-face
3685      article-display-face
3686      article-de-quoted-unreadable
3687      article-de-base64-unreadable
3688      article-decode-HZ
3689      article-wash-html
3690      article-unsplit-urls
3691      article-hide-list-identifiers
3692      article-strip-banner
3693      article-babel
3694      article-hide-pem
3695      article-hide-signature
3696      article-strip-headers-in-body
3697      article-remove-trailing-blank-lines
3698      article-strip-leading-blank-lines
3699      article-strip-multiple-blank-lines
3700      article-strip-leading-space
3701      article-strip-trailing-space
3702      article-strip-blank-lines
3703      article-strip-all-blank-lines
3704      article-date-local
3705      article-date-english
3706      article-date-iso8601
3707      article-date-original
3708      article-date-ut
3709      article-decode-mime-words
3710      article-decode-charset
3711      article-decode-encoded-words
3712      article-date-user
3713      article-date-lapsed
3714      article-emphasize
3715      article-treat-dumbquotes
3716      article-normalize-headers
3717      (article-show-all-headers . gnus-article-show-all-headers)
3718 ;;      (article-show-all . gnus-article-show-all)
3719      )))
3720 \f
3721 ;;;
3722 ;;; Gnus article mode
3723 ;;;
3724
3725 (put 'gnus-article-mode 'mode-class 'special)
3726
3727 (gnus-define-keys gnus-article-mode-map
3728   " " gnus-article-goto-next-page
3729   "\177" gnus-article-goto-prev-page
3730   [delete] gnus-article-goto-prev-page
3731   [backspace] gnus-article-goto-prev-page
3732   "\C-c^" gnus-article-refer-article
3733   "h" gnus-article-show-summary
3734   "s" gnus-article-show-summary
3735   "\C-c\C-m" gnus-article-mail
3736   "?" gnus-article-describe-briefly
3737   "e" gnus-summary-edit-article
3738   "<" beginning-of-buffer
3739   ">" end-of-buffer
3740   "\C-c\C-i" gnus-info-find-node
3741   "\C-c\C-b" gnus-bug
3742   "R" gnus-article-reply-with-original
3743   "F" gnus-article-followup-with-original
3744   "\C-hk" gnus-article-describe-key
3745   "\C-hc" gnus-article-describe-key-briefly
3746
3747   "\C-d" gnus-article-read-summary-keys
3748   "\M-*" gnus-article-read-summary-keys
3749   "\M-#" gnus-article-read-summary-keys
3750   "\M-^" gnus-article-read-summary-keys
3751   "\M-g" gnus-article-read-summary-keys)
3752
3753 ;; Define almost undefined keys to `gnus-article-read-summary-keys'.
3754 (let (keys)
3755   (let ((key 32))
3756     (while (<= key 127)
3757       (push (char-to-string key) keys)
3758       (incf key))
3759     (dolist (elem (accessible-keymaps gnus-summary-mode-map))
3760       (setq key (car elem))
3761       (when (and (> (length key) 0)
3762                  (not (eq 'menu-bar (aref key 0)))
3763                  (symbolp (lookup-key gnus-summary-mode-map key)))
3764         (push key keys))))
3765   (dolist (key keys)
3766     (unless (lookup-key gnus-article-mode-map key)
3767       (define-key gnus-article-mode-map key 'gnus-article-read-summary-keys))))
3768
3769 (defun gnus-article-make-menu-bar ()
3770   (unless (boundp 'gnus-article-commands-menu)
3771     (gnus-summary-make-menu-bar))
3772   (gnus-turn-off-edit-menu 'article)
3773   (unless (boundp 'gnus-article-article-menu)
3774     (easy-menu-define
3775      gnus-article-article-menu gnus-article-mode-map ""
3776      '("Article"
3777        ["Scroll forwards" gnus-article-goto-next-page t]
3778        ["Scroll backwards" gnus-article-goto-prev-page t]
3779        ["Show summary" gnus-article-show-summary t]
3780        ["Fetch Message-ID at point" gnus-article-refer-article t]
3781        ["Mail to address at point" gnus-article-mail t]
3782        ["Send a bug report" gnus-bug t]))
3783
3784     (easy-menu-define
3785      gnus-article-treatment-menu gnus-article-mode-map ""
3786      ;; Fixme: this should use :active (and maybe :visible).
3787      '("Treatment"
3788        ["Hide headers" gnus-article-hide-headers t]
3789        ["Hide signature" gnus-article-hide-signature t]
3790        ["Hide citation" gnus-article-hide-citation t]
3791        ["Treat overstrike" gnus-article-treat-overstrike t]
3792        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
3793        ["Remove carriage return" gnus-article-remove-cr t]
3794        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
3795        ["Treat html" gnus-article-wash-html t]
3796        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
3797        ["Decode HZ" gnus-article-decode-HZ t]))
3798
3799     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3800
3801     ;; Note "Post" menu is defined in gnus-sum.el for consistency
3802
3803     (gnus-run-hooks 'gnus-article-menu-hook)))
3804
3805 (defun gnus-article-mode ()
3806   "Major mode for displaying an article.
3807
3808 All normal editing commands are switched off.
3809
3810 The following commands are available in addition to all summary mode
3811 commands:
3812 \\<gnus-article-mode-map>
3813 \\[gnus-article-next-page]\t Scroll the article one page forwards
3814 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3815 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3816 \\[gnus-article-show-summary]\t Display the summary buffer
3817 \\[gnus-article-mail]\t Send a reply to the address near point
3818 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3819 \\[gnus-info-find-node]\t Go to the Gnus info node"
3820   (interactive)
3821   (gnus-simplify-mode-line)
3822   (setq mode-name "Article")
3823   (setq major-mode 'gnus-article-mode)
3824   (make-local-variable 'minor-mode-alist)
3825   (unless (assq 'gnus-show-mime minor-mode-alist)
3826     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
3827   (use-local-map gnus-article-mode-map)
3828   (when (gnus-visual-p 'article-menu 'menu)
3829     (gnus-article-make-menu-bar)
3830     (when gnus-summary-tool-bar-map
3831       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
3832   (gnus-update-format-specifications nil 'article-mode)
3833   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3834   (set (make-local-variable 'gnus-page-broken) nil)
3835   (make-local-variable 'gnus-button-marker-list)
3836   (make-local-variable 'gnus-article-current-summary)
3837   (make-local-variable 'gnus-article-mime-handles)
3838   (make-local-variable 'gnus-article-decoded-p)
3839   (make-local-variable 'gnus-article-mime-handle-alist)
3840   (make-local-variable 'gnus-article-wash-types)
3841   (make-local-variable 'gnus-article-image-alist)
3842   (make-local-variable 'gnus-article-charset)
3843   (make-local-variable 'gnus-article-ignored-charsets)
3844   (gnus-set-default-directory)
3845   (buffer-disable-undo)
3846   (setq buffer-read-only t)
3847   (set-syntax-table gnus-article-mode-syntax-table)
3848   (gnus-run-hooks 'gnus-article-mode-hook))
3849
3850 (defun gnus-article-setup-buffer ()
3851   "Initialize the article buffer."
3852   (let* ((name (if gnus-single-article-buffer "*Article*"
3853                  (concat "*Article " gnus-newsgroup-name "*")))
3854          (original
3855           (progn (string-match "\\*Article" name)
3856                  (concat " *Original Article"
3857                          (substring name (match-end 0))))))
3858     (setq gnus-article-buffer name)
3859     (setq gnus-original-article-buffer original)
3860     (setq gnus-article-mime-handle-alist nil)
3861     ;; This might be a variable local to the summary buffer.
3862     (unless gnus-single-article-buffer
3863       (save-excursion
3864         (set-buffer gnus-summary-buffer)
3865         (setq gnus-article-buffer name)
3866         (setq gnus-original-article-buffer original)
3867         (gnus-set-global-variables)))
3868     (gnus-article-setup-highlight-words)
3869     ;; Init original article buffer.
3870     (save-excursion
3871       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3872       (set-buffer-multibyte t)
3873       (setq major-mode 'gnus-original-article-mode)
3874       (make-local-variable 'gnus-original-article))
3875     (if (get-buffer name)
3876         (save-excursion
3877           (set-buffer name)
3878           (when (and gnus-article-edit-mode
3879                      (buffer-modified-p)
3880                      (not
3881                       (y-or-n-p "Article mode edit in progress; discard? ")))
3882             (error "Action aborted"))
3883           (set (make-local-variable 'gnus-article-edit-mode) nil)
3884           (buffer-disable-undo)
3885           (setq buffer-read-only t)
3886           ;; This list just keeps growing if we don't reset it.
3887           (setq gnus-button-marker-list nil)
3888           (unless (eq major-mode 'gnus-article-mode)
3889             (gnus-article-mode))
3890           (current-buffer))
3891       (save-excursion
3892         (set-buffer (gnus-get-buffer-create name))
3893         (gnus-article-mode)
3894         (make-local-variable 'gnus-summary-buffer)
3895         (gnus-summary-set-local-parameters gnus-newsgroup-name)
3896         (current-buffer)))))
3897
3898 ;; Set article window start at LINE, where LINE is the number of lines
3899 ;; from the head of the article.
3900 (defun gnus-article-set-window-start (&optional line)
3901   (set-window-start
3902    (gnus-get-buffer-window gnus-article-buffer t)
3903    (save-excursion
3904      (set-buffer gnus-article-buffer)
3905      (goto-char (point-min))
3906      (if (not line)
3907          (point-min)
3908        (gnus-message 6 "Moved to bookmark")
3909        (search-forward "\n\n" nil t)
3910        (forward-line line)
3911        (point)))))
3912
3913 ;;; @@ article filters
3914 ;;;
3915
3916 (defun gnus-article-display-mime-message ()
3917   "Article display method for MIME message."
3918   ;; called from `gnus-original-article-buffer'.
3919   (let (charset all-headers)
3920     (with-current-buffer gnus-summary-buffer
3921       (setq charset default-mime-charset
3922             all-headers gnus-have-all-headers))
3923     (make-local-variable 'default-mime-charset)
3924     (setq default-mime-charset charset)
3925     (with-current-buffer (get-buffer-create gnus-article-buffer)
3926       (make-local-variable 'default-mime-charset)
3927       (setq default-mime-charset charset))
3928     (mime-display-message mime-message-structure
3929                           gnus-article-buffer nil gnus-article-mode-map)
3930     (when all-headers
3931       (gnus-article-hide-headers nil -1)))
3932   (run-hooks 'gnus-mime-article-prepare-hook))
3933
3934 (defun gnus-article-display-traditional-message ()
3935   "Article display method for traditional message."
3936   (set-buffer gnus-article-buffer)
3937   (let (buffer-read-only)
3938     (erase-buffer)
3939     (insert-buffer-substring gnus-original-article-buffer)))
3940
3941 (defun gnus-article-make-full-mail-header (&optional number charset)
3942   "Create a new mail header structure in a raw article buffer."
3943   (unless (and number charset)
3944     (save-current-buffer
3945       (set-buffer gnus-summary-buffer)
3946       (unless number
3947         (setq number (or (cdr gnus-article-current) 0)))
3948       (unless charset
3949         (setq charset (or default-mime-charset 'x-ctext)))))
3950   (goto-char (point-min))
3951   (let ((header-end (if (search-forward "\n\n" nil t)
3952                         (1- (point))
3953                       (goto-char (point-max))))
3954         (chars (- (point-max) (point)))
3955         (lines (count-lines (point) (point-max)))
3956         (default-mime-charset charset)
3957         xref)
3958     (narrow-to-region (point-min) header-end)
3959     (setq xref (std11-fetch-field "xref"))
3960     (prog1
3961         (make-full-mail-header
3962          number
3963          (std11-fetch-field "subject")
3964          (std11-fetch-field "from")
3965          (std11-fetch-field "date")
3966          (std11-fetch-field "message-id")
3967          (std11-fetch-field "references")
3968          chars
3969          lines
3970          (when xref (concat "Xref: " xref)))
3971       (widen))))
3972
3973 (defun gnus-article-prepare (article &optional all-headers header)
3974   "Prepare ARTICLE in article mode buffer.
3975 ARTICLE should either be an article number or a Message-ID.
3976 If ARTICLE is an id, HEADER should be the article headers.
3977 If ALL-HEADERS is non-nil, no headers are hidden."
3978   (save-excursion
3979     ;; Make sure we start in a summary buffer.
3980     (unless (eq major-mode 'gnus-summary-mode)
3981       (set-buffer gnus-summary-buffer))
3982     (setq gnus-summary-buffer (current-buffer))
3983     (let* ((gnus-article (if header (mail-header-number header) article))
3984            (summary-buffer (current-buffer))
3985            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3986            (group gnus-newsgroup-name)
3987            result)
3988       (save-excursion
3989         (gnus-article-setup-buffer)
3990         (set-buffer gnus-article-buffer)
3991         ;; Deactivate active regions.
3992         (when (and (boundp 'transient-mark-mode)
3993                    transient-mark-mode)
3994           (setq mark-active nil))
3995         (if (not (setq result (let ((buffer-read-only nil))
3996                                 (gnus-request-article-this-buffer
3997                                  article group))))
3998             ;; There is no such article.
3999             (save-excursion
4000               (when (and (numberp article)
4001                          (not (memq article gnus-newsgroup-sparse)))
4002                 (setq gnus-article-current
4003                       (cons gnus-newsgroup-name article))
4004                 (set-buffer gnus-summary-buffer)
4005                 (setq gnus-current-article article)
4006                 (if (and (memq article gnus-newsgroup-undownloaded)
4007                          (not (gnus-online (gnus-find-method-for-group
4008                                             gnus-newsgroup-name))))
4009                     (progn
4010                       (gnus-summary-set-agent-mark article)
4011                       (message "Message marked for downloading"))
4012                   (gnus-summary-mark-article article gnus-canceled-mark)
4013                   (unless (memq article gnus-newsgroup-sparse)
4014                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4015           (if (or (eq result 'pseudo)
4016                   (eq result 'nneething))
4017               (progn
4018                 (save-excursion
4019                   (set-buffer summary-buffer)
4020                   (push article gnus-newsgroup-history)
4021                   (setq gnus-last-article gnus-current-article
4022                         gnus-current-article 0
4023                         gnus-current-headers nil
4024                         gnus-article-current nil)
4025                   (if (eq result 'nneething)
4026                       (gnus-configure-windows 'summary)
4027                     (gnus-configure-windows 'article))
4028                   (gnus-set-global-variables))
4029                 (let ((gnus-article-mime-handle-alist-1
4030                        gnus-article-mime-handle-alist))
4031                   (gnus-set-mode-line 'article)))
4032             ;; The result from the `request' was an actual article -
4033             ;; or at least some text that is now displayed in the
4034             ;; article buffer.
4035             (when (and (numberp article)
4036                        (not (eq article gnus-current-article)))
4037               ;; Seems like a new article has been selected.
4038               ;; `gnus-current-article' must be an article number.
4039               (save-excursion
4040                 (set-buffer summary-buffer)
4041                 (push article gnus-newsgroup-history)
4042                 (setq gnus-last-article gnus-current-article
4043                       gnus-current-article article
4044                       gnus-current-headers
4045                       (gnus-summary-article-header gnus-current-article)
4046                       gnus-article-current
4047                       (cons gnus-newsgroup-name gnus-current-article))
4048                 (unless (vectorp gnus-current-headers)
4049                   (setq gnus-current-headers nil))
4050                 (gnus-summary-goto-subject gnus-current-article)
4051                 (when (gnus-summary-show-thread)
4052                   ;; If the summary buffer really was folded, the
4053                   ;; previous goto may not actually have gone to
4054                   ;; the right article, but the thread root instead.
4055                   ;; So we go again.
4056                   (gnus-summary-goto-subject gnus-current-article))
4057                 (gnus-run-hooks 'gnus-mark-article-hook)
4058                 (gnus-set-mode-line 'summary)
4059                 (when (gnus-visual-p 'article-highlight 'highlight)
4060                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4061                 ;; Set the global newsgroup variables here.
4062                 (gnus-set-global-variables)
4063                 (setq gnus-have-all-headers
4064                       (or all-headers gnus-show-all-headers))))
4065             (save-excursion
4066               (gnus-configure-windows 'article))
4067             (when (or (numberp article)
4068                       (stringp article))
4069               (gnus-article-prepare-display)
4070               ;; Do page break.
4071               (goto-char (point-min))
4072               (when gnus-break-pages
4073                 (gnus-narrow-to-page)))
4074             (let ((gnus-article-mime-handle-alist-1
4075                    gnus-article-mime-handle-alist))
4076               (gnus-set-mode-line 'article))
4077             (article-goto-body)
4078             (unless (bobp)
4079               (forward-line -1))
4080             (set-window-point (get-buffer-window (current-buffer)) (point))
4081             (gnus-configure-windows 'article)
4082             t))))))
4083
4084 (defun gnus-article-prepare-mime-display (&optional number)
4085   (goto-char (point-min))
4086   (when (re-search-forward "^[^\t ]+:" nil t)
4087     (goto-char (match-beginning 0)))
4088   (let ((entity (if (eq 1 (point-min))
4089                     (get-text-property 1 'mime-view-entity)
4090                   (get-text-property (point) 'mime-view-entity)))
4091         last-entity child-entity next type)
4092     (setq child-entity (mime-entity-children entity))
4093     (if child-entity
4094         (setq last-entity (nth (1- (length child-entity))
4095                                child-entity))
4096       (setq last-entity entity))
4097     (save-restriction
4098       (narrow-to-region (point)
4099                         (if (search-forward "\n\n" nil t)
4100                             (point)
4101                           (point-max)))
4102       (gnus-treat-article 'head)
4103       (put-text-property (point-min) (point-max) 'article-treated-header t)
4104       (goto-char (point-max)))
4105     (while (and (not (eobp)) entity)
4106       (setq next (set-marker
4107                   (make-marker)
4108                   (next-single-property-change (point) 'mime-view-entity
4109                                                nil (point-max))))
4110       (let ((types (mime-entity-content-type entity)))
4111         (while (eq 'multipart (mime-content-type-primary-type types))
4112           (setq entity (car (mime-entity-children entity))
4113                 types (mime-entity-content-type entity)))
4114         (when types
4115           (setq type (format "%s/%s"
4116                              (mime-content-type-primary-type types)
4117                              (mime-content-type-subtype types)))))
4118       (if (string-equal type "message/rfc822")
4119           (progn
4120             (setq next (point))
4121             (let ((children (mime-entity-children entity))
4122                   last-children)
4123               (when children
4124                 (setq last-children (nth (1- (length children)) children))
4125                 (while
4126                     (and
4127                      (not (eq last-children
4128                               (get-text-property next 'mime-view-entity)))
4129                      (setq next
4130                            (next-single-property-change next
4131                                                         'mime-view-entity
4132                                                         nil (point-max)))))))
4133             (setq next (next-single-property-change next 'mime-view-entity
4134                                                     nil (point-max)))
4135             (save-restriction
4136               (narrow-to-region (point) next)
4137               (gnus-article-prepare-mime-display)
4138               (goto-char (point-max)))
4139             (setq entity (get-text-property (point) 'mime-view-entity)))
4140         (save-restriction
4141           (narrow-to-region (point) next)
4142           ;; Kludge. We have to count true number, but for now,
4143           ;; part number is here only to achieve `last'.
4144           (gnus-treat-article nil 1
4145                               (if (eq entity last-entity)
4146                                   1 2)
4147                               type)
4148           (goto-char (point-max)))
4149         (setq entity (get-text-property next 'mime-view-entity))))))
4150
4151 ;;;###autoload
4152 (defun gnus-article-prepare-display ()
4153   "Make the current buffer look like a nice article."
4154   (let ((gnus-article-buffer (current-buffer))
4155         buffer-read-only)
4156     (unless (eq major-mode 'gnus-article-mode)
4157       (gnus-article-mode))
4158     (setq buffer-read-only nil
4159           gnus-button-marker-list nil
4160           gnus-article-wash-types nil
4161           gnus-article-image-alist nil)
4162     (save-restriction
4163       (widen)
4164       (static-if (featurep 'xemacs)
4165           (map-extents (lambda (extent maparg) (delete-extent extent)))
4166         (let ((lists (overlay-lists)))
4167           (dolist (overlay (nconc (car lists) (cdr lists)))
4168             (delete-overlay overlay)))))
4169     (gnus-run-hooks 'gnus-tmp-internal-hook))
4170   (let ((show-mime (unless (member gnus-newsgroup-name '("nndraft:delayed"
4171                                                          "nndraft:drafts"))
4172                      gnus-show-mime))
4173         (inhibit-read-only t))
4174     (set-buffer gnus-original-article-buffer)
4175     ;; Display message.
4176     (setq mime-message-structure gnus-current-headers)
4177     (mime-buffer-entity-set-buffer-internal mime-message-structure
4178                                             gnus-original-article-buffer)
4179     (mime-entity-set-representation-type-internal mime-message-structure
4180                                                   'mime-buffer-entity)
4181     (luna-send mime-message-structure 'initialize-instance
4182                mime-message-structure)
4183     (if show-mime
4184         (let (mime-display-header-hook mime-display-text/plain-hook)
4185           (funcall gnus-article-display-method-for-mime))
4186       (funcall gnus-article-display-method-for-traditional))
4187     ;; Call the treatment functions.
4188     (save-restriction
4189       (widen)
4190       (if show-mime
4191           (gnus-article-prepare-mime-display)
4192         (narrow-to-region (goto-char (point-min))
4193                           (if (search-forward "\n\n" nil t)
4194                               (point)
4195                             (point-max)))
4196         (gnus-treat-article 'head)
4197         (put-text-property (point-min) (point-max) 'article-treated-header t)
4198         (goto-char (point-max))
4199         (widen)
4200         (narrow-to-region (point) (point-max))
4201         (gnus-treat-article nil))
4202       (put-text-property (point-min) (point-max) 'read-only nil)))
4203   (gnus-run-hooks 'gnus-article-prepare-hook))
4204
4205 (defun gnus-article-decode-article-as-default-mime-charset ()
4206   "Decode an article as `default-mime-charset'.  It won't work if the
4207 value of the variable `gnus-show-mime' is non-nil."
4208   (unless (or gnus-show-mime
4209               (member gnus-newsgroup-name '("nndraft:delayed"
4210                                             "nndraft:drafts")))
4211     (set (make-local-variable 'default-mime-charset)
4212          (with-current-buffer gnus-summary-buffer
4213            default-mime-charset))
4214     (decode-mime-charset-region (point-min) (point-max)
4215                                 default-mime-charset)))
4216
4217 ;;;
4218 ;;; Gnus MIME viewing functions
4219 ;;;
4220
4221 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4222   "Format of the MIME buttons.
4223
4224 Valid specifiers include:
4225 %t  The MIME type
4226 %T  MIME type, along with additional info
4227 %n  The `name' parameter
4228 %d  The description, if any
4229 %l  The length of the encoded part
4230 %p  The part identifier number
4231 %e  Dots if the part isn't displayed
4232
4233 General format specifiers can also be used.  See Info node
4234 `(gnus)Formatting Variables'.")
4235
4236 (defvar gnus-mime-button-line-format-alist
4237   '((?t gnus-tmp-type ?s)
4238     (?T gnus-tmp-type-long ?s)
4239     (?n gnus-tmp-name ?s)
4240     (?d gnus-tmp-description ?s)
4241     (?p gnus-tmp-id ?s)
4242     (?l gnus-tmp-length ?d)
4243     (?e gnus-tmp-dots ?s)))
4244
4245 (defvar gnus-mime-button-commands
4246   '((gnus-article-press-button "\r" "Toggle Display")
4247     (gnus-mime-view-part "v" "View Interactively...")
4248     (gnus-mime-view-part-as-type "t" "View As Type...")
4249     (gnus-mime-view-part-as-charset "C" "View As charset...")
4250     (gnus-mime-save-part "o" "Save...")
4251     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4252     (gnus-mime-delete-part "d" "Delete part")
4253     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4254     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4255     (gnus-mime-view-part-internally "E" "View Internally")
4256     (gnus-mime-view-part-externally "e" "View Externally")
4257     (gnus-mime-print-part "p" "Print")
4258     (gnus-mime-pipe-part "|" "Pipe To Command...")
4259     (gnus-mime-action-on-part "." "Take action on the part...")))
4260
4261 (defun gnus-article-mime-part-status ()
4262   (with-current-buffer gnus-article-buffer
4263     (let ((entity (get-text-property (point-min) 'mime-view-entity))
4264           children)
4265       (if (and entity
4266                (setq children (mime-entity-children entity))
4267                (setq children (length children)))
4268           (if (eq 1 children)
4269               " (1 part)"
4270             (format " (%d parts)" children))
4271         ""))))
4272
4273 (defvar gnus-mime-button-map
4274   (let ((map (make-sparse-keymap)))
4275     (unless (>= (string-to-number emacs-version) 21)
4276       ;; XEmacs doesn't care.
4277       (set-keymap-parent map gnus-article-mode-map))
4278     (define-key map gnus-mouse-2 'gnus-article-push-button)
4279     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4280     (dolist (c gnus-mime-button-commands)
4281       (define-key map (cadr c) (car c)))
4282     map))
4283
4284 (easy-menu-define
4285   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4286   `("MIME Part"
4287     ,@(mapcar (lambda (c)
4288                 (vector (caddr c) (car c) :enable t))
4289               gnus-mime-button-commands)))
4290
4291 (defun gnus-mime-button-menu (event prefix)
4292  "Construct a context-sensitive menu of MIME commands."
4293  (interactive "e\nP")
4294  (save-window-excursion
4295    (let ((pos (event-start event)))
4296      (select-window (posn-window pos))
4297      (goto-char (posn-point pos))
4298      (gnus-article-check-buffer)
4299      (popup-menu gnus-mime-button-menu nil prefix))))
4300
4301 (defun gnus-mime-view-all-parts (&optional handles)
4302   "View all the MIME parts."
4303   (interactive)
4304   (save-current-buffer
4305     (set-buffer gnus-article-buffer)
4306     (let ((handles (or handles gnus-article-mime-handles))
4307           (mail-parse-charset gnus-newsgroup-charset)
4308           (mail-parse-ignored-charsets
4309            (with-current-buffer gnus-summary-buffer
4310              gnus-newsgroup-ignored-charsets)))
4311       (when handles
4312         (mm-remove-parts handles)
4313         (goto-char (point-min))
4314         (or (search-forward "\n\n") (goto-char (point-max)))
4315         (let (buffer-read-only)
4316           (delete-region (point) (point-max))
4317           (mm-display-parts handles))))))
4318
4319 (defun gnus-mime-save-part-and-strip ()
4320   "Save the MIME part under point then replace it with an external body."
4321   (interactive)
4322   (gnus-article-check-buffer)
4323   (let* ((data (get-text-property (point) 'gnus-data))
4324          file param
4325          (handles gnus-article-mime-handles))
4326     (if (mm-multiple-handles gnus-article-mime-handles)
4327         (error "This function is not implemented"))
4328     (setq file (and data (mm-save-part data)))
4329     (when file
4330       (with-current-buffer (mm-handle-buffer data)
4331         (erase-buffer)
4332         (insert "Content-Type: " (mm-handle-media-type data))
4333         (mml-insert-parameter-string (cdr (mm-handle-type data))
4334                                      '(charset))
4335         (insert "\n")
4336         (insert "Content-ID: " (message-make-message-id) "\n")
4337         (insert "Content-Transfer-Encoding: binary\n")
4338         (insert "\n"))
4339       (setcdr data
4340               (cdr (mm-make-handle nil
4341                                    `("message/external-body"
4342                                      (access-type . "LOCAL-FILE")
4343                                      (name . ,file)))))
4344       (set-buffer gnus-summary-buffer)
4345       (gnus-article-edit-article
4346        `(lambda ()
4347           (erase-buffer)
4348           (let ((mail-parse-charset (or gnus-article-charset
4349                                         ',gnus-newsgroup-charset))
4350                 (mail-parse-ignored-charsets
4351                  (or gnus-article-ignored-charsets
4352                      ',gnus-newsgroup-ignored-charsets))
4353                 (mbl mml-buffer-list))
4354             (setq mml-buffer-list nil)
4355             (insert-buffer gnus-original-article-buffer)
4356             (mime-to-mml ',handles)
4357             (setq gnus-article-mime-handles nil)
4358             (let ((mbl1 mml-buffer-list))
4359               (setq mml-buffer-list mbl)
4360               (set (make-local-variable 'mml-buffer-list) mbl1))
4361             (gnus-make-local-hook 'kill-buffer-hook)
4362             (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4363        `(lambda (no-highlight)
4364           (let ((mail-parse-charset (or gnus-article-charset
4365                                         ',gnus-newsgroup-charset))
4366                 (message-options message-options)
4367                 (message-options-set-recipient)
4368                 (mail-parse-ignored-charsets
4369                  (or gnus-article-ignored-charsets
4370                      ',gnus-newsgroup-ignored-charsets)))
4371             (mml-to-mime)
4372             (mml-destroy-buffers)
4373             (remove-hook 'kill-buffer-hook
4374                          'mml-destroy-buffers t)
4375             (kill-local-variable 'mml-buffer-list))
4376           (gnus-summary-edit-article-done
4377            ,(or (mail-header-references gnus-current-headers) "")
4378            ,(gnus-group-read-only-p)
4379            ,gnus-summary-buffer no-highlight))))))
4380
4381 (defun gnus-mime-delete-part ()
4382   "Delete the MIME part under point.
4383 Replace it with some information about the removed part."
4384   (interactive)
4385   (gnus-article-check-buffer)
4386   (unless (and gnus-novice-user
4387                (not (gnus-yes-or-no-p
4388                      "Really delete attachment forever? ")))
4389     (let* ((data (get-text-property (point) 'gnus-data))
4390            (handles gnus-article-mime-handles)
4391            (none "(none)")
4392            (description
4393             (or
4394              (mail-decode-encoded-word-string (or (mm-handle-description data)
4395                                                   none))))
4396            (filename
4397             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
4398                 none))
4399            (type (mm-handle-media-type data)))
4400       (if (mm-multiple-handles gnus-article-mime-handles)
4401           (error "This function is not implemented"))
4402       (with-current-buffer (mm-handle-buffer data)
4403         (let ((bsize (format "%s" (buffer-size))))
4404           (erase-buffer)
4405           (insert
4406            (concat
4407             ",----\n"
4408             "| The following attachment has been deleted:\n"
4409             "|\n"
4410             "| Type:           " type "\n"
4411             "| Filename:       " filename "\n"
4412             "| Size (encoded): " bsize " Byte\n"
4413             "| Description:    " description "\n"
4414             "`----\n"))
4415           (setcdr data
4416                   (cdr (mm-make-handle
4417                         nil `("text/plain") nil nil
4418                         (list "attachment")
4419                         (format "Deleted attachment (%s bytes)" bsize))))))
4420       (set-buffer gnus-summary-buffer)
4421       ;; FIXME: maybe some of the following code (borrowed from
4422       ;; `gnus-mime-save-part-and-strip') isn't necessary?
4423       (gnus-article-edit-article
4424        `(lambda ()
4425           (erase-buffer)
4426           (let ((mail-parse-charset (or gnus-article-charset
4427                                         ',gnus-newsgroup-charset))
4428                 (mail-parse-ignored-charsets
4429                  (or gnus-article-ignored-charsets
4430                      ',gnus-newsgroup-ignored-charsets))
4431                 (mbl mml-buffer-list))
4432             (setq mml-buffer-list nil)
4433             (insert-buffer gnus-original-article-buffer)
4434             (mime-to-mml ',handles)
4435             (setq gnus-article-mime-handles nil)
4436             (let ((mbl1 mml-buffer-list))
4437               (setq mml-buffer-list mbl)
4438               (set (make-local-variable 'mml-buffer-list) mbl1))
4439             (gnus-make-local-hook 'kill-buffer-hook)
4440             (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4441        `(lambda (no-highlight)
4442           (let ((mail-parse-charset (or gnus-article-charset
4443                                         ',gnus-newsgroup-charset))
4444                 (message-options message-options)
4445                 (message-options-set-recipient)
4446                 (mail-parse-ignored-charsets
4447                  (or gnus-article-ignored-charsets
4448                      ',gnus-newsgroup-ignored-charsets)))
4449             (mml-to-mime)
4450             (mml-destroy-buffers)
4451             (remove-hook 'kill-buffer-hook
4452                          'mml-destroy-buffers t)
4453             (kill-local-variable 'mml-buffer-list))
4454           (gnus-summary-edit-article-done
4455            ,(or (mail-header-references gnus-current-headers) "")
4456            ,(gnus-group-read-only-p)
4457            ,gnus-summary-buffer no-highlight)))))
4458   ;; Not in `gnus-mime-save-part-and-strip':
4459   (gnus-article-edit-done)
4460   (gnus-summary-expand-window)
4461   (gnus-summary-show-article))
4462
4463 (defun gnus-mime-save-part ()
4464   "Save the MIME part under point."
4465   (interactive)
4466   (gnus-article-check-buffer)
4467   (let ((data (get-text-property (point) 'gnus-data)))
4468     (when data
4469       (mm-save-part data))))
4470
4471 (defun gnus-mime-pipe-part ()
4472   "Pipe the MIME part under point to a process."
4473   (interactive)
4474   (gnus-article-check-buffer)
4475   (let ((data (get-text-property (point) 'gnus-data)))
4476     (when data
4477       (mm-pipe-part data))))
4478
4479 (defun gnus-mime-view-part ()
4480   "Interactively choose a viewing method for the MIME part under point."
4481   (interactive)
4482   (gnus-article-check-buffer)
4483   (let ((data (get-text-property (point) 'gnus-data)))
4484     (when data
4485       (setq gnus-article-mime-handles
4486             (mm-merge-handles
4487              gnus-article-mime-handles (setq data (copy-sequence data))))
4488       (mm-interactively-view-part data))))
4489
4490 (defun gnus-mime-view-part-as-type-internal ()
4491   (gnus-article-check-buffer)
4492   (let* ((name (mail-content-type-get
4493                 (mm-handle-type (get-text-property (point) 'gnus-data))
4494                 'name))
4495          (def-type (and name (mm-default-file-encoding name))))
4496     (and def-type (cons def-type 0))))
4497
4498 (defun gnus-mime-view-part-as-type (&optional mime-type)
4499   "Choose a MIME media type, and view the part as such."
4500   (interactive)
4501   (unless mime-type
4502     (setq mime-type (completing-read
4503                      "View as MIME type: "
4504                      (mapcar #'list (mailcap-mime-types))
4505                      nil nil
4506                      (gnus-mime-view-part-as-type-internal))))
4507   (gnus-article-check-buffer)
4508   (let ((handle (get-text-property (point) 'gnus-data)))
4509     (when handle
4510       (setq handle
4511             (mm-make-handle (mm-handle-buffer handle)
4512                             (cons mime-type (cdr (mm-handle-type handle)))
4513                             (mm-handle-encoding handle)
4514                             (mm-handle-undisplayer handle)
4515                             (mm-handle-disposition handle)
4516                             (mm-handle-description handle)
4517                             nil
4518                             (mm-handle-id handle)))
4519       (setq gnus-article-mime-handles
4520             (mm-merge-handles gnus-article-mime-handles handle))
4521       (gnus-mm-display-part handle))))
4522
4523 (eval-when-compile
4524   (require 'jka-compr))
4525
4526 ;; jka-compr.el uses a "sh -c" to direct stderr to err-file, but these days
4527 ;; emacs can do that itself.
4528 ;;
4529 (defun gnus-mime-jka-compr-maybe-uncompress ()
4530   "Uncompress the current buffer if `auto-compression-mode' is enabled.
4531 The uncompress method used is derived from `buffer-file-name'."
4532   (when (and (fboundp 'jka-compr-installed-p)
4533              (jka-compr-installed-p))
4534     (let ((info (jka-compr-get-compression-info buffer-file-name)))
4535       (when info
4536         (let ((basename (file-name-nondirectory buffer-file-name))
4537               (args     (jka-compr-info-uncompress-args    info))
4538               (prog     (jka-compr-info-uncompress-program info))
4539               (message  (jka-compr-info-uncompress-message info))
4540               (err-file (jka-compr-make-temp-name)))
4541           (if message
4542               (message "%s %s..." message basename))
4543           (unwind-protect
4544               (unless (memq (apply 'call-process-region
4545                                    (point-min) (point-max)
4546                                    prog
4547                                    t (list t err-file) nil
4548                                    args)
4549                             jka-compr-acceptable-retval-list)
4550                 (jka-compr-error prog args basename message err-file))
4551             (jka-compr-delete-temp-file err-file)))))))
4552
4553 (defun gnus-mime-copy-part (&optional handle)
4554   "Put the MIME part under point into a new buffer.
4555 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
4556 are decompressed."
4557   (interactive)
4558   (gnus-article-check-buffer)
4559   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4560          (contents (and handle (mm-get-part handle)))
4561          (base (and handle
4562                     (file-name-nondirectory
4563                      (or
4564                       (mail-content-type-get (mm-handle-type handle) 'name)
4565                       (mail-content-type-get (mm-handle-disposition handle)
4566                                              'filename)
4567                       "*decoded*"))))
4568          (buffer (and base (generate-new-buffer base))))
4569     (when contents
4570       (switch-to-buffer buffer)
4571       (insert contents)
4572       ;; We do it this way to make `normal-mode' set the appropriate mode.
4573       (unwind-protect
4574           (progn
4575             (setq buffer-file-name (expand-file-name base))
4576             (gnus-mime-jka-compr-maybe-uncompress)
4577             (normal-mode))
4578         (setq buffer-file-name nil))
4579       (goto-char (point-min)))))
4580
4581 (defun gnus-mime-print-part (&optional handle filename)
4582   "Print the MIME part under point."
4583   (interactive (list nil (ps-print-preprint current-prefix-arg)))
4584   (gnus-article-check-buffer)
4585   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4586          (contents (and handle (mm-get-part handle)))
4587          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
4588          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
4589     (when contents
4590         (if printer
4591             (unwind-protect
4592                 (progn
4593                   (mm-save-part-to-file handle file)
4594                   (call-process shell-file-name nil
4595                                 (generate-new-buffer " *mm*")
4596                                 nil
4597                                 shell-command-switch
4598                                 (mm-mailcap-command
4599                                  printer file (mm-handle-type handle))))
4600               (delete-file file))
4601           (with-temp-buffer
4602             (insert contents)
4603             (gnus-print-buffer))
4604           (ps-despool filename)))))
4605
4606 (defun gnus-mime-inline-part (&optional handle arg)
4607   "Insert the MIME part under point into the current buffer."
4608   (interactive (list nil current-prefix-arg))
4609   (gnus-article-check-buffer)
4610   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4611          contents charset
4612          (b (point))
4613          buffer-read-only)
4614     (when handle
4615       (if (and (not arg) (mm-handle-undisplayer handle))
4616           (mm-remove-part handle)
4617         (setq contents (mm-get-part handle))
4618         (cond
4619          ((not arg)
4620           (setq charset (or (mail-content-type-get
4621                              (mm-handle-type handle) 'charset)
4622                             gnus-newsgroup-charset)))
4623          ((numberp arg)
4624           (if (mm-handle-undisplayer handle)
4625               (mm-remove-part handle))
4626           (setq charset
4627                 (or (cdr (assq arg
4628                                gnus-summary-show-article-charset-alist))
4629                     (mm-read-coding-system "Charset: ")))))
4630         (forward-line 2)
4631         (mm-insert-inline handle
4632                           (if (and charset
4633                                    (setq charset (mm-charset-to-coding-system
4634                                                   charset))
4635                                    (not (eq charset 'ascii)))
4636                               (mm-decode-coding-string contents charset)
4637                             contents))
4638         (goto-char b)))))
4639
4640 (defun gnus-mime-view-part-as-charset (&optional handle arg)
4641   "Insert the MIME part under point into the current buffer using the
4642 specified charset."
4643   (interactive (list nil current-prefix-arg))
4644   (gnus-article-check-buffer)
4645   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4646          contents charset
4647          (b (point))
4648          buffer-read-only)
4649     (when handle
4650       (if (mm-handle-undisplayer handle)
4651           (mm-remove-part handle))
4652       (let ((gnus-newsgroup-charset
4653              (or (cdr (assq arg
4654                             gnus-summary-show-article-charset-alist))
4655                  (mm-read-coding-system "Charset: ")))
4656             (gnus-newsgroup-ignored-charsets 'gnus-all))
4657         (gnus-article-press-button)))))
4658
4659 (defun gnus-mime-view-part-externally (&optional handle)
4660   "View the MIME part under point with an external viewer."
4661   (interactive)
4662   (gnus-article-check-buffer)
4663   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4664          (mm-user-display-methods nil)
4665          (mm-inlined-types nil)
4666          (mail-parse-charset gnus-newsgroup-charset)
4667          (mail-parse-ignored-charsets
4668           (save-excursion (set-buffer gnus-summary-buffer)
4669                           gnus-newsgroup-ignored-charsets)))
4670     (when handle
4671       (if (mm-handle-undisplayer handle)
4672           (mm-remove-part handle)
4673         (mm-display-part handle)))))
4674
4675 (defun gnus-mime-view-part-internally (&optional handle)
4676   "View the MIME part under point with an internal viewer.
4677 If no internal viewer is available, use an external viewer."
4678   (interactive)
4679   (gnus-article-check-buffer)
4680   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4681          (mm-inlined-types '(".*"))
4682          (mm-inline-large-images t)
4683          (mail-parse-charset gnus-newsgroup-charset)
4684          (mail-parse-ignored-charsets
4685           (save-excursion (set-buffer gnus-summary-buffer)
4686                           gnus-newsgroup-ignored-charsets))
4687          buffer-read-only)
4688     (when handle
4689       (if (mm-handle-undisplayer handle)
4690           (mm-remove-part handle)
4691         (mm-display-part handle)))))
4692
4693 (defun gnus-mime-action-on-part (&optional action)
4694   "Do something with the MIME attachment at \(point\)."
4695   (interactive
4696    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
4697   (gnus-article-check-buffer)
4698   (let ((action-pair (assoc action gnus-mime-action-alist)))
4699     (if action-pair
4700         (funcall (cdr action-pair)))))
4701
4702 (defun gnus-article-part-wrapper (n function)
4703   (save-current-buffer
4704     (set-buffer gnus-article-buffer)
4705     (when (> n (length gnus-article-mime-handle-alist))
4706       (error "No such part"))
4707     (gnus-article-goto-part n)
4708     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4709       (funcall function handle))))
4710
4711 (defun gnus-article-pipe-part (n)
4712   "Pipe MIME part N, which is the numerical prefix."
4713   (interactive "p")
4714   (gnus-article-part-wrapper n 'mm-pipe-part))
4715
4716 (defun gnus-article-save-part (n)
4717   "Save MIME part N, which is the numerical prefix."
4718   (interactive "p")
4719   (gnus-article-part-wrapper n 'mm-save-part))
4720
4721 (defun gnus-article-interactively-view-part (n)
4722   "View MIME part N interactively, which is the numerical prefix."
4723   (interactive "p")
4724   (gnus-article-part-wrapper n 'mm-interactively-view-part))
4725
4726 (defun gnus-article-copy-part (n)
4727   "Copy MIME part N, which is the numerical prefix."
4728   (interactive "p")
4729   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
4730
4731 (defun gnus-article-view-part-as-charset (n)
4732   "View MIME part N using a specified charset.
4733 N is the numerical prefix."
4734   (interactive "p")
4735   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
4736
4737 (defun gnus-article-view-part-externally (n)
4738   "View MIME part N externally, which is the numerical prefix."
4739   (interactive "p")
4740   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
4741
4742 (defun gnus-article-inline-part (n)
4743   "Inline MIME part N, which is the numerical prefix."
4744   (interactive "p")
4745   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
4746
4747 (defun gnus-article-mime-match-handle-first (condition)
4748   (if condition
4749       (let ((alist gnus-article-mime-handle-alist) ihandle n)
4750         (while (setq ihandle (pop alist))
4751           (if (and (cond
4752                     ((functionp condition)
4753                      (funcall condition (cdr ihandle)))
4754                     ((eq condition 'undisplayed)
4755                      (not (or (mm-handle-undisplayer (cdr ihandle))
4756                               (equal (mm-handle-media-type (cdr ihandle))
4757                                      "multipart/alternative"))))
4758                     ((eq condition 'undisplayed-alternative)
4759                      (not (mm-handle-undisplayer (cdr ihandle))))
4760                     (t t))
4761                    (gnus-article-goto-part (car ihandle))
4762                    (or (not n) (< (car ihandle) n)))
4763               (setq n (car ihandle))))
4764         (or n 1))
4765     1))
4766
4767 (defun gnus-article-view-part (&optional n)
4768   "View MIME part N, which is the numerical prefix."
4769   (interactive "P")
4770   (save-current-buffer
4771     (set-buffer gnus-article-buffer)
4772     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
4773                              gnus-article-mime-match-handle-function)))
4774     (when (> n (length gnus-article-mime-handle-alist))
4775       (error "No such part"))
4776     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4777       (when (gnus-article-goto-part n)
4778         (if (equal (car handle) "multipart/alternative")
4779             (gnus-article-press-button)
4780           (when (eq (gnus-mm-display-part handle) 'internal)
4781             (gnus-set-window-start)))))))
4782
4783 (defsubst gnus-article-mime-total-parts ()
4784   (if (bufferp (car gnus-article-mime-handles))
4785       1 ;; single part
4786     (1- (length gnus-article-mime-handles))))
4787
4788 (defun gnus-mm-display-part (handle)
4789   "Display HANDLE and fix MIME button."
4790   (let ((id (get-text-property (point) 'gnus-part))
4791         (point (point))
4792         buffer-read-only)
4793     (forward-line 1)
4794     (prog1
4795         (let ((window (selected-window))
4796               (mail-parse-charset gnus-newsgroup-charset)
4797               (mail-parse-ignored-charsets
4798                (if (gnus-buffer-live-p gnus-summary-buffer)
4799                    (save-excursion
4800                      (set-buffer gnus-summary-buffer)
4801                      gnus-newsgroup-ignored-charsets)
4802                  nil)))
4803           (save-excursion
4804             (unwind-protect
4805                 (let ((win (gnus-get-buffer-window (current-buffer) t))
4806                       (beg (point)))
4807                   (when win
4808                     (select-window win))
4809                   (goto-char point)
4810                   (forward-line)
4811                   (if (mm-handle-displayed-p handle)
4812                       ;; This will remove the part.
4813                       (mm-display-part handle)
4814                     (save-restriction
4815                       (narrow-to-region (point)
4816                                         (if (eobp) (point) (1+ (point))))
4817                       (mm-display-part handle)
4818                       ;; We narrow to the part itself and
4819                       ;; then call the treatment functions.
4820                       (goto-char (point-min))
4821                       (forward-line 1)
4822                       (narrow-to-region (point) (point-max))
4823                       (gnus-treat-article
4824                        nil id
4825                        (gnus-article-mime-total-parts)
4826                        (mm-handle-media-type handle)))))
4827               (if (window-live-p window)
4828                   (select-window window)))))
4829       (goto-char point)
4830       (gnus-delete-line)
4831       (gnus-insert-mime-button
4832        handle id (list (mm-handle-displayed-p handle)))
4833       (goto-char point))))
4834
4835 (defun gnus-article-goto-part (n)
4836   "Go to MIME part N."
4837   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
4838
4839 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4840   (let ((gnus-tmp-name
4841          (or (mail-content-type-get (mm-handle-type handle) 'name)
4842              (mail-content-type-get (mm-handle-disposition handle) 'filename)
4843              (mail-content-type-get (mm-handle-type handle) 'url)
4844              ""))
4845         (gnus-tmp-type (mm-handle-media-type handle))
4846         (gnus-tmp-description
4847          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4848                                               "")))
4849         (gnus-tmp-dots
4850          (if (if displayed (car displayed)
4851                (mm-handle-displayed-p handle))
4852              "" "..."))
4853         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4854                            (buffer-size)))
4855         gnus-tmp-type-long b e)
4856     (when (string-match ".*/" gnus-tmp-name)
4857       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4858     (setq gnus-tmp-type-long (concat gnus-tmp-type
4859                                      (and (not (equal gnus-tmp-name ""))
4860                                           (concat "; " gnus-tmp-name))))
4861     (unless (equal gnus-tmp-description "")
4862       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4863     (unless (bolp)
4864       (insert "\n"))
4865     (setq b (point))
4866     (gnus-eval-format
4867      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4868      `(keymap ,gnus-mime-button-map
4869               gnus-callback gnus-mm-display-part
4870               gnus-part ,gnus-tmp-id
4871               article-type annotation
4872               gnus-data ,handle))
4873     (setq e (if (bolp)
4874                 ;; Exclude a newline.
4875                 (1- (point))
4876               (point)))
4877     (widget-convert-button
4878      'link b e
4879      :mime-handle handle
4880      :action 'gnus-widget-press-button
4881      :button-keymap gnus-mime-button-map
4882      :help-echo
4883      (lambda (widget/window &optional overlay pos)
4884        ;; Needed to properly clear the message due to a bug in
4885        ;; wid-edit (XEmacs only).
4886        (if (boundp 'help-echo-owns-message)
4887            (setq help-echo-owns-message t))
4888        (format
4889         "%S: %s the MIME part; %S: more options"
4890         (aref gnus-mouse-2 0)
4891         ;; XEmacs will get a single widget arg; Emacs 21 will get
4892         ;; window, overlay, position.
4893         (if (mm-handle-displayed-p
4894              (if overlay
4895                  (with-current-buffer (gnus-overlay-buffer overlay)
4896                    (widget-get (widget-at (gnus-overlay-start overlay))
4897                                :mime-handle))
4898                (widget-get widget/window :mime-handle)))
4899             "hide" "show")
4900         (aref gnus-down-mouse-3 0))))))
4901
4902 (defun gnus-widget-press-button (elems el)
4903   (goto-char (widget-get elems :from))
4904   (gnus-article-press-button))
4905
4906 (defvar gnus-displaying-mime nil)
4907
4908 (defun gnus-display-mime (&optional ihandles)
4909   "Display the MIME parts."
4910   (save-excursion
4911     (save-selected-window
4912       (let ((window (get-buffer-window gnus-article-buffer))
4913             (point (point)))
4914         (when window
4915           (select-window window)
4916           ;; We have to do this since selecting the window
4917           ;; may change the point.  So we set the window point.
4918           (set-window-point window point)))
4919       (let* ((handles (or ihandles
4920                           (mm-dissect-buffer nil gnus-article-loose-mime)
4921                           (and gnus-article-emulate-mime
4922                                (mm-uu-dissect))))
4923              buffer-read-only handle name type b e display)
4924         (when (and (not ihandles)
4925                    (not gnus-displaying-mime))
4926           ;; Top-level call; we clean up.
4927           (when gnus-article-mime-handles
4928             (mm-destroy-parts gnus-article-mime-handles)
4929             (setq gnus-article-mime-handle-alist nil));; A trick.
4930           (setq gnus-article-mime-handles handles)
4931           ;; We allow users to glean info from the handles.
4932           (when gnus-article-mime-part-function
4933             (gnus-mime-part-function handles)))
4934         (if (and handles
4935                  (or (not (stringp (car handles)))
4936                      (cdr handles)))
4937             (progn
4938               (when (and (not ihandles)
4939                          (not gnus-displaying-mime))
4940                 ;; Clean up for mime parts.
4941                 (article-goto-body)
4942                 (delete-region (point) (point-max)))
4943               (let ((gnus-displaying-mime t))
4944                 (gnus-mime-display-part handles)))
4945           (save-restriction
4946             (article-goto-body)
4947             (narrow-to-region (point) (point-max))
4948             (gnus-treat-article nil 1 1)
4949             (widen)))
4950         (unless ihandles
4951           ;; Highlight the headers.
4952           (save-excursion
4953             (save-restriction
4954               (article-goto-body)
4955               (narrow-to-region (point-min) (point))
4956               (gnus-treat-article 'head))))))))
4957
4958 (defcustom gnus-mime-display-multipart-as-mixed nil
4959   "Display \"multipart\" parts as  \"multipart/mixed\".
4960
4961 If t, it overrides nil values of
4962 `gnus-mime-display-multipart-alternative-as-mixed' and
4963 `gnus-mime-display-multipart-related-as-mixed'."
4964   :group 'gnus-article-mime
4965   :type 'boolean)
4966
4967 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
4968   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
4969   :group 'gnus-article-mime
4970   :type 'boolean)
4971
4972 (defcustom gnus-mime-display-multipart-related-as-mixed nil
4973   "Display \"multipart/related\" parts as  \"multipart/mixed\".
4974
4975 If displaying \"text/html\" is discouraged \(see
4976 `mm-discouraged-alternatives'\) images or other material inside a
4977 \"multipart/related\" part might be overlooked when this variable is nil."
4978   :group 'gnus-article-mime
4979   :type 'boolean)
4980
4981 (defun gnus-mime-display-part (handle)
4982   (cond
4983    ;; Single part.
4984    ((not (stringp (car handle)))
4985     (gnus-mime-display-single handle))
4986    ;; User-defined multipart
4987    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
4988     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
4989              handle))
4990    ;; multipart/alternative
4991    ((and (equal (car handle) "multipart/alternative")
4992          (not (or gnus-mime-display-multipart-as-mixed
4993                   gnus-mime-display-multipart-alternative-as-mixed)))
4994     (let ((id (1+ (length gnus-article-mime-handle-alist))))
4995       (push (cons id handle) gnus-article-mime-handle-alist)
4996       (gnus-mime-display-alternative (cdr handle) nil nil id)))
4997    ;; multipart/related
4998    ((and (equal (car handle) "multipart/related")
4999          (not (or gnus-mime-display-multipart-as-mixed
5000                   gnus-mime-display-multipart-related-as-mixed)))
5001     ;;;!!!We should find the start part, but we just default
5002     ;;;!!!to the first part.
5003     ;;(gnus-mime-display-part (cadr handle))
5004     ;;;!!! Most multipart/related is an HTML message plus images.
5005     ;;;!!! Unfortunately we are unable to let W3 display those
5006     ;;;!!! included images, so we just display it as a mixed multipart.
5007     ;;(gnus-mime-display-mixed (cdr handle))
5008     ;;;!!! No, w3 can display everything just fine.
5009     (gnus-mime-display-part (cadr handle)))
5010    ((equal (car handle) "multipart/signed")
5011     (gnus-add-wash-type 'signed)
5012     (gnus-mime-display-security handle))
5013    ((equal (car handle) "multipart/encrypted")
5014     (gnus-add-wash-type 'encrypted)
5015     (gnus-mime-display-security handle))
5016    ;; Other multiparts are handled like multipart/mixed.
5017    (t
5018     (gnus-mime-display-mixed (cdr handle)))))
5019
5020 (defun gnus-mime-part-function (handles)
5021   (if (stringp (car handles))
5022       (mapcar 'gnus-mime-part-function (cdr handles))
5023     (funcall gnus-article-mime-part-function handles)))
5024
5025 (defun gnus-mime-display-mixed (handles)
5026   (mapcar 'gnus-mime-display-part handles))
5027
5028 (defun gnus-mime-display-single (handle)
5029   (let ((type (mm-handle-media-type handle))
5030         (ignored gnus-ignored-mime-types)
5031         (not-attachment t)
5032         (move nil)
5033         display text)
5034     (catch 'ignored
5035       (progn
5036         (while ignored
5037           (when (string-match (pop ignored) type)
5038             (throw 'ignored nil)))
5039         (if (and (setq not-attachment
5040                        (and (not (mm-inline-override-p handle))
5041                             (or (not (mm-handle-disposition handle))
5042                                 (equal (car (mm-handle-disposition handle))
5043                                        "inline")
5044                                 (mm-attachment-override-p handle))))
5045                  (mm-automatic-display-p handle)
5046                  (or (and
5047                       (mm-inlinable-p handle)
5048                       (mm-inlined-p handle))
5049                      (mm-automatic-external-display-p type)))
5050             (setq display t)
5051           (when (equal (mm-handle-media-supertype handle) "text")
5052             (setq text t)))
5053         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5054               beg)
5055           (push (cons id handle) gnus-article-mime-handle-alist)
5056           (when (or (not display)
5057                     (not (gnus-unbuttonized-mime-type-p type)))
5058             ;(gnus-article-insert-newline)
5059             (gnus-insert-mime-button
5060              handle id (list (or display (and not-attachment text))))
5061             (gnus-article-insert-newline)
5062             ;(gnus-article-insert-newline)
5063             ;; Remember modify the number of forward lines.
5064             (setq move t))
5065           (setq beg (point))
5066           (cond
5067            (display
5068             (when move
5069               (forward-line -1)
5070               (setq beg (point)))
5071             (let ((mail-parse-charset gnus-newsgroup-charset)
5072                   (mail-parse-ignored-charsets
5073                    (save-excursion (condition-case ()
5074                                        (set-buffer gnus-summary-buffer)
5075                                      (error))
5076                                    gnus-newsgroup-ignored-charsets)))
5077               (mm-display-part handle t))
5078             (goto-char (point-max)))
5079            ((and text not-attachment)
5080             (when move
5081               (forward-line -1)
5082               (setq beg (point)))
5083             (gnus-article-insert-newline)
5084             (mm-insert-inline handle (mm-get-part handle))
5085             (goto-char (point-max))))
5086           ;; Do highlighting.
5087           (save-excursion
5088             (save-restriction
5089               (narrow-to-region beg (point))
5090               (gnus-treat-article
5091                nil id
5092                (gnus-article-mime-total-parts)
5093                (mm-handle-media-type handle)))))))))
5094
5095 (defun gnus-unbuttonized-mime-type-p (type)
5096   "Say whether TYPE is to be unbuttonized."
5097   (unless gnus-inhibit-mime-unbuttonizing
5098     (when (catch 'found
5099             (let ((types gnus-unbuttonized-mime-types))
5100               (while types
5101                 (when (string-match (pop types) type)
5102                   (throw 'found t)))))
5103       (not (catch 'found
5104              (let ((types gnus-buttonized-mime-types))
5105                (while types
5106                  (when (string-match (pop types) type)
5107                    (throw 'found t)))))))))
5108
5109 (defun gnus-article-insert-newline ()
5110   "Insert a newline, but mark it as undeletable."
5111   (gnus-put-text-property
5112    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5113
5114 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5115   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5116          (ihandles handles)
5117          (point (point))
5118          handle buffer-read-only from props begend not-pref)
5119     (save-window-excursion
5120       (save-restriction
5121         (when ibegend
5122           (narrow-to-region (car ibegend)
5123                             (or (cdr ibegend)
5124                                 (progn
5125                                   (goto-char (car ibegend))
5126                                   (forward-line 2)
5127                                   (point))))
5128           (delete-region (point-min) (point-max))
5129           (mm-remove-parts handles))
5130         (setq begend (list (point-marker)))
5131         ;; Do the toggle.
5132         (unless (setq not-pref (cadr (member preferred ihandles)))
5133           (setq not-pref (car ihandles)))
5134         (when (or ibegend
5135                   (not preferred)
5136                   (not (gnus-unbuttonized-mime-type-p
5137                         "multipart/alternative")))
5138           (gnus-add-text-properties
5139            (setq from (point))
5140            (progn
5141              (insert (format "%d.  " id))
5142              (point))
5143            `(gnus-callback
5144              (lambda (handles)
5145                (unless ,(not ibegend)
5146                  (setq gnus-article-mime-handle-alist
5147                        ',gnus-article-mime-handle-alist))
5148                (gnus-mime-display-alternative
5149                 ',ihandles ',not-pref ',begend ,id))
5150              keymap ,gnus-mime-button-map
5151              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5152              face ,gnus-article-button-face
5153              gnus-part ,id
5154              gnus-data ,handle))
5155           (widget-convert-button 'link from (point)
5156                                  :action 'gnus-widget-press-button
5157                                  :button-keymap gnus-widget-button-keymap)
5158           ;; Do the handles
5159           (while (setq handle (pop handles))
5160             (gnus-add-text-properties
5161              (setq from (point))
5162              (progn
5163                (insert (format "(%c) %-18s"
5164                                (if (equal handle preferred) ?* ? )
5165                                (mm-handle-media-type handle)))
5166                (point))
5167              `(gnus-callback
5168                (lambda (handles)
5169                  (unless ,(not ibegend)
5170                    (setq gnus-article-mime-handle-alist
5171                          ',gnus-article-mime-handle-alist))
5172                  (gnus-mime-display-alternative
5173                   ',ihandles ',handle ',begend ,id))
5174                keymap ,gnus-mime-button-map
5175                ,gnus-mouse-face-prop ,gnus-article-mouse-face
5176                face ,gnus-article-button-face
5177                gnus-part ,id
5178                gnus-data ,handle))
5179             (widget-convert-button 'link from (point)
5180                                    :action 'gnus-widget-press-button
5181                                    :button-keymap gnus-widget-button-keymap)
5182             (insert "  "))
5183           (insert "\n\n"))
5184         (when preferred
5185           (if (stringp (car preferred))
5186               (gnus-display-mime preferred)
5187             (let ((mail-parse-charset gnus-newsgroup-charset)
5188                   (mail-parse-ignored-charsets
5189                    (save-excursion (set-buffer gnus-summary-buffer)
5190                                    gnus-newsgroup-ignored-charsets)))
5191               (mm-display-part preferred)
5192               ;; Do highlighting.
5193               (save-excursion
5194                 (save-restriction
5195                   (narrow-to-region (car begend) (point-max))
5196                   (gnus-treat-article
5197                    nil (length gnus-article-mime-handle-alist)
5198                    (gnus-article-mime-total-parts)
5199                    (mm-handle-media-type handle))))))
5200           (goto-char (point-max))
5201           (setcdr begend (point-marker)))))
5202     (when ibegend
5203       (goto-char point))))
5204
5205 (defconst gnus-article-wash-status-strings
5206   (let ((alist '((cite "c" "Possible hidden citation text"
5207                        " " "All citation text visible")
5208                  (headers "h" "Hidden headers"
5209                           " " "All headers visible.")
5210                  (pgp "p" "Encrypted or signed message status hidden"
5211                       " " "No hidden encryption nor digital signature status")
5212                  (signature "s" "Signature has been hidden"
5213                             " " "Signature is visible")
5214                  (overstrike "o" "Overstrike (^H) characters applied"
5215                              " " "No overstrike characters applied")
5216                  (gnus-show-mime "m" "Mime processing is activated"
5217                                  " " "Mime processing is not activated")
5218                  (emphasis "e" "/*_Emphasis_*/ characters applied"
5219                            " " "No /*_emphasis_*/ characters applied")))
5220         result)
5221     (dolist (entry alist result)
5222       (let ((key (nth 0 entry))
5223             (on (copy-sequence (nth 1 entry)))
5224             (on-help (nth 2 entry))
5225             (off (copy-sequence (nth 3 entry)))
5226             (off-help (nth 4 entry)))
5227         (put-text-property 0 1 'help-echo on-help on)
5228         (put-text-property 0 1 'help-echo off-help off)
5229         (push (list key on off) result))))
5230   "Alist of strings describing wash status in the mode line.
5231 Each entry has the form (KEY ON OF), where the KEY is a symbol
5232 representing the particular washing function, ON is the string to use
5233 in the article mode line when the washing function is active, and OFF
5234 is the string to use when it is inactive.")
5235
5236 (defun gnus-article-wash-status-entry (key value)
5237   (let ((entry (assoc key gnus-article-wash-status-strings)))
5238     (if value (nth 1 entry) (nth 2 entry))))
5239
5240 (defun gnus-article-wash-status ()
5241   "Return a string which display status of article washing."
5242   (save-excursion
5243     (set-buffer gnus-article-buffer)
5244     (let ((cite (memq 'cite gnus-article-wash-types))
5245           (headers (memq 'headers gnus-article-wash-types))
5246           (boring (memq 'boring-headers gnus-article-wash-types))
5247           (pgp (memq 'pgp gnus-article-wash-types))
5248           (pem (memq 'pem gnus-article-wash-types))
5249           (signed (memq 'signed gnus-article-wash-types))
5250           (encrypted (memq 'encrypted gnus-article-wash-types))
5251           (signature (memq 'signature gnus-article-wash-types))
5252           (overstrike (memq 'overstrike gnus-article-wash-types))
5253           (emphasis (memq 'emphasis gnus-article-wash-types)))
5254       (concat
5255        (gnus-article-wash-status-entry 'cite cite)
5256        (gnus-article-wash-status-entry 'headers (or headers boring))
5257        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
5258        (gnus-article-wash-status-entry 'signature signature)
5259        (gnus-article-wash-status-entry 'overstrike overstrike)
5260        (gnus-article-wash-status-entry 'gnus-show-mime gnus-show-mime)
5261        (gnus-article-wash-status-entry 'emphasis emphasis)))))
5262
5263 (defun gnus-add-wash-type (type)
5264   "Add a washing of TYPE to the current status."
5265   (add-to-list 'gnus-article-wash-types type))
5266
5267 (defun gnus-delete-wash-type (type)
5268   "Add a washing of TYPE to the current status."
5269   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
5270
5271 (defun gnus-add-image (category image)
5272   "Add IMAGE of CATEGORY to the list of displayed images."
5273   (let ((entry (assq category gnus-article-image-alist)))
5274     (unless entry
5275       (setq entry (list category))
5276       (push entry gnus-article-image-alist))
5277     (nconc entry (list image))))
5278
5279 (defun gnus-delete-images (category)
5280   "Delete all images in CATEGORY."
5281   (let ((entry (assq category gnus-article-image-alist)))
5282     (dolist (image (cdr entry))
5283       (gnus-remove-image image category))
5284     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
5285     (gnus-delete-wash-type category)))
5286
5287 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
5288
5289 (defun gnus-article-maybe-hide-headers ()
5290   "Hide unwanted headers if `gnus-have-all-headers' is nil.
5291 Provided for backwards compatibility."
5292   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
5293                  (not (save-excursion (set-buffer gnus-summary-buffer)
5294                                       gnus-have-all-headers)))
5295              (not gnus-inhibit-hiding))
5296     (gnus-article-hide-headers)))
5297
5298 ;;; Article savers.
5299
5300 (defun gnus-output-to-file (file-name)
5301   "Append the current article to a file named FILE-NAME."
5302   (let ((artbuf (current-buffer)))
5303     (with-temp-buffer
5304       (insert-buffer-substring artbuf)
5305       ;; Append newline at end of the buffer as separator, and then
5306       ;; save it to file.
5307       (goto-char (point-max))
5308       (insert "\n")
5309       (let ((file-name-coding-system nnmail-pathname-coding-system))
5310         (write-region-as-binary (point-min) (point-max) file-name 'append))
5311       t)))
5312
5313 (defun gnus-narrow-to-page (&optional arg)
5314   "Narrow the article buffer to a page.
5315 If given a numerical ARG, move forward ARG pages."
5316   (interactive "P")
5317   (setq arg (if arg (prefix-numeric-value arg) 0))
5318   (save-excursion
5319     (set-buffer gnus-article-buffer)
5320     (goto-char (point-min))
5321     (widen)
5322     ;; Remove any old next/prev buttons.
5323     (when (gnus-visual-p 'page-marker)
5324       (let ((buffer-read-only nil))
5325         (gnus-remove-text-with-property 'gnus-prev)
5326         (gnus-remove-text-with-property 'gnus-next)))
5327     (if
5328         (cond ((< arg 0)
5329                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
5330               ((> arg 0)
5331                (re-search-forward page-delimiter nil 'move arg)))
5332         (goto-char (match-end 0))
5333       (save-excursion
5334         (goto-char (point-min))
5335         (setq gnus-page-broken
5336               (and (re-search-forward page-delimiter nil t) t))))
5337     (when gnus-page-broken
5338       (narrow-to-region
5339        (point)
5340        (if (re-search-forward page-delimiter nil 'move)
5341            (match-beginning 0)
5342          (point)))
5343       (when (and (gnus-visual-p 'page-marker)
5344                  (not (= (point-min) 1)))
5345         (save-excursion
5346           (goto-char (point-min))
5347           (gnus-insert-prev-page-button)))
5348       (when (and (gnus-visual-p 'page-marker)
5349                  (< (+ (point-max) 2) (buffer-size)))
5350         (save-excursion
5351           (goto-char (point-max))
5352           (gnus-insert-next-page-button))))))
5353
5354 ;; Article mode commands
5355
5356 (defun gnus-article-goto-next-page ()
5357   "Show the next page of the article."
5358   (interactive)
5359   (when (gnus-article-next-page)
5360     (goto-char (point-min))
5361     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
5362
5363
5364 (defun gnus-article-goto-prev-page ()
5365   "Show the previous page of the article."
5366   (interactive)
5367   (if (bobp)
5368       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
5369     (gnus-article-prev-page nil)))
5370
5371 ;; This is cleaner but currently breaks `gnus-pick-mode':
5372 ;;
5373 ;; (defun gnus-article-goto-next-page ()
5374 ;;   "Show the next page of the article."
5375 ;;   (interactive)
5376 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
5377 ;;     (gnus-summary-next-page)))
5378 ;;
5379 ;; (defun gnus-article-goto-prev-page ()
5380 ;;   "Show the next page of the article."
5381 ;;   (interactive)
5382 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
5383 ;;     (gnus-summary-prev-page)))
5384
5385 (defun gnus-article-next-page (&optional lines)
5386   "Show the next page of the current article.
5387 If end of article, return non-nil.  Otherwise return nil.
5388 Argument LINES specifies lines to be scrolled up."
5389   (interactive "p")
5390   (move-to-window-line -1)
5391   (if (save-excursion
5392         (end-of-line)
5393         (and (pos-visible-in-window-p)  ;Not continuation line.
5394              (>= (1+ (point)) (point-max)))) ;Allow for trailing newline.
5395       ;; Nothing in this page.
5396       (if (or (not gnus-page-broken)
5397               (save-excursion
5398                 (save-restriction
5399                   (widen)
5400                   (eobp)))) ;Real end-of-buffer?
5401           (progn
5402             (when gnus-article-over-scroll
5403               (gnus-article-next-page-1 lines))
5404             t)                  ;Nothing more.
5405         (gnus-narrow-to-page 1)         ;Go to next page.
5406         nil)
5407     ;; More in this page.
5408     (gnus-article-next-page-1 lines)
5409     nil))
5410
5411 (defun gnus-article-next-page-1 (lines)
5412   (let ((scroll-in-place nil))
5413     (condition-case ()
5414         (scroll-up lines)
5415       (end-of-buffer
5416        ;; Long lines may cause an end-of-buffer error.
5417        (goto-char (point-max)))))
5418   (move-to-window-line 0))
5419
5420 (defun gnus-article-prev-page (&optional lines)
5421   "Show previous page of current article.
5422 Argument LINES specifies lines to be scrolled down."
5423   (interactive "p")
5424   (move-to-window-line 0)
5425   (if (and gnus-page-broken
5426            (bobp)
5427            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
5428       (progn
5429         (gnus-narrow-to-page -1)        ;Go to previous page.
5430         (goto-char (point-max))
5431         (recenter -1))
5432     (let ((scroll-in-place nil))
5433       (prog1
5434           (condition-case ()
5435               (scroll-down lines)
5436             (beginning-of-buffer
5437              (goto-char (point-min))))
5438         (move-to-window-line 0)))))
5439
5440 (defun gnus-article-only-boring-p ()
5441   "Decide whether there is only boring text remaining in the article.
5442 Something \"interesting\" is a word of at least two letters that does
5443 not have a face in `gnus-article-boring-faces'."
5444   (when (and gnus-article-skip-boring
5445              (boundp 'gnus-article-boring-faces)
5446              (symbol-value 'gnus-article-boring-faces))
5447     (save-excursion
5448       (catch 'only-boring
5449         (while (re-search-forward "\\b\\w\\w" nil t)
5450           (forward-char -1)
5451           (when (not (gnus-intersection
5452                       (gnus-faces-at (point))
5453                       (symbol-value 'gnus-article-boring-faces)))
5454             (throw 'only-boring nil)))
5455         (throw 'only-boring t)))))
5456
5457 (defun gnus-article-refer-article ()
5458   "Read article specified by message-id around point."
5459   (interactive)
5460   (save-excursion
5461     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
5462     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
5463     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
5464         (let ((msg-id (concat "<" (match-string 0) ">")))
5465           (set-buffer gnus-summary-buffer)
5466           (gnus-summary-refer-article msg-id))
5467       (error "No references around point"))))
5468
5469 (defun gnus-article-show-summary ()
5470   "Reconfigure windows to show summary buffer."
5471   (interactive)
5472   (if (not (gnus-buffer-live-p gnus-summary-buffer))
5473       (error "There is no summary buffer for this article buffer")
5474     (gnus-article-set-globals)
5475     (gnus-configure-windows 'article)
5476     (gnus-summary-goto-subject gnus-current-article)
5477     (gnus-summary-position-point)))
5478
5479 (defun gnus-article-describe-briefly ()
5480   "Describe article mode commands briefly."
5481   (interactive)
5482   (gnus-message 6 (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")))
5483
5484 (defun gnus-article-summary-command ()
5485   "Execute the last keystroke in the summary buffer."
5486   (interactive)
5487   (let ((obuf (current-buffer))
5488         (owin (current-window-configuration))
5489         func)
5490     (switch-to-buffer gnus-article-current-summary 'norecord)
5491     (setq func (lookup-key (current-local-map) (this-command-keys)))
5492     (call-interactively func)
5493     (set-buffer obuf)
5494     (set-window-configuration owin)
5495     (set-window-point (get-buffer-window (current-buffer)) (point))))
5496
5497 (defun gnus-article-summary-command-nosave ()
5498   "Execute the last keystroke in the summary buffer."
5499   (interactive)
5500   (let (func)
5501     (pop-to-buffer gnus-article-current-summary 'norecord)
5502     (setq func (lookup-key (current-local-map) (this-command-keys)))
5503     (call-interactively func)))
5504
5505 (defun gnus-article-check-buffer ()
5506   "Beep if not in an article buffer."
5507   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
5508     (error "Command invoked outside of a Gnus article buffer")))
5509
5510 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
5511   "Read a summary buffer key sequence and execute it from the article buffer."
5512   (interactive "P")
5513   (gnus-article-check-buffer)
5514   (let ((nosaves
5515          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
5516            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
5517            "=" "^" "\M-^" "|"))
5518         (nosave-but-article
5519          '("A\r"))
5520         (nosave-in-article
5521          '("\C-d"))
5522         (up-to-top
5523          '("n" "Gn" "p" "Gp"))
5524         keys new-sum-point)
5525     (save-excursion
5526       (set-buffer gnus-article-current-summary)
5527       (let (gnus-pick-mode)
5528         (push (or key last-command-event) unread-command-events)
5529         (setq keys (static-if (featurep 'xemacs)
5530                        (events-to-keys (read-key-sequence nil))
5531                      (read-key-sequence nil)))))
5532     (message "")
5533
5534     (if (or (member keys nosaves)
5535             (member keys nosave-but-article)
5536             (member keys nosave-in-article))
5537         (let (func)
5538           (save-window-excursion
5539             (pop-to-buffer gnus-article-current-summary 'norecord)
5540             ;; We disable the pick minor mode commands.
5541             (let (gnus-pick-mode)
5542               (setq func (lookup-key (current-local-map) keys))))
5543           (if (or (not func)
5544                   (numberp func))
5545               (ding)
5546             (unless (member keys nosave-in-article)
5547               (set-buffer gnus-article-current-summary))
5548             (call-interactively func)
5549             (setq new-sum-point (point)))
5550           (when (member keys nosave-but-article)
5551             (pop-to-buffer gnus-article-buffer 'norecord)))
5552       ;; These commands should restore window configuration.
5553       (let ((obuf (current-buffer))
5554             (owin (current-window-configuration))
5555             (opoint (point))
5556             (summary gnus-article-current-summary)
5557             func in-buffer selected)
5558         (if not-restore-window
5559             (pop-to-buffer summary 'norecord)
5560           (switch-to-buffer summary 'norecord))
5561         (setq in-buffer (current-buffer))
5562         ;; We disable the pick minor mode commands.
5563         (if (and (setq func (let (gnus-pick-mode)
5564                               (lookup-key (current-local-map) keys)))
5565                  (functionp func))
5566             (progn
5567               (call-interactively func)
5568               (setq new-sum-point (point))
5569               (when (eq in-buffer (current-buffer))
5570                 (setq selected (gnus-summary-select-article))
5571                 (set-buffer obuf)
5572                 (unless not-restore-window
5573                   (set-window-configuration owin))
5574                 (when (eq selected 'old)
5575                   (article-goto-body)
5576                   (set-window-start (get-buffer-window (current-buffer))
5577                                     1)
5578                   (set-window-point (get-buffer-window (current-buffer))
5579                                     (point)))
5580                 (let ((win (get-buffer-window gnus-article-current-summary)))
5581                   (when win
5582                     (set-window-point win new-sum-point))))    )
5583           (switch-to-buffer gnus-article-buffer)
5584           (ding))))))
5585
5586 (defun gnus-article-describe-key (key)
5587   "Display documentation of the function invoked by KEY.  KEY is a string."
5588   (interactive "kDescribe key: ")
5589   (gnus-article-check-buffer)
5590   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5591       (save-excursion
5592         (set-buffer gnus-article-current-summary)
5593         (let (gnus-pick-mode)
5594           (if (featurep 'xemacs)
5595               (progn
5596                 (push (elt key 0) unread-command-events)
5597                 (setq key (events-to-keys
5598                            (read-key-sequence "Describe key: "))))
5599             (setq unread-command-events
5600                   (mapcar
5601                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5602                    key))
5603             (setq key (read-key-sequence "Describe key: "))))
5604         (describe-key key))
5605     (describe-key key)))
5606
5607 (defun gnus-article-describe-key-briefly (key &optional insert)
5608   "Display documentation of the function invoked by KEY.  KEY is a string."
5609   (interactive "kDescribe key: \nP")
5610   (gnus-article-check-buffer)
5611   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5612       (save-excursion
5613         (set-buffer gnus-article-current-summary)
5614         (let (gnus-pick-mode)
5615           (if (featurep 'xemacs)
5616               (progn
5617                 (push (elt key 0) unread-command-events)
5618                 (setq key (events-to-keys
5619                            (read-key-sequence "Describe key: "))))
5620             (setq unread-command-events
5621                   (mapcar
5622                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5623                    key))
5624             (setq key (read-key-sequence "Describe key: "))))
5625         (describe-key-briefly key insert))
5626     (describe-key-briefly key insert)))
5627
5628 (defun gnus-article-reply-with-original (&optional wide)
5629   "Start composing a reply mail to the current message.
5630 The text in the region will be yanked.  If the region isn't active,
5631 the entire article will be yanked."
5632   (interactive "P")
5633   (let ((article (cdr gnus-article-current))
5634         contents)
5635     (if (not (gnus-mark-active-p))
5636         (with-current-buffer gnus-summary-buffer
5637           (gnus-summary-reply (list (list article)) wide))
5638       (setq contents (buffer-substring (point) (mark t)))
5639       ;; Deactivate active regions.
5640       (when (and (boundp 'transient-mark-mode)
5641                  transient-mark-mode)
5642         (setq mark-active nil))
5643       (with-current-buffer gnus-summary-buffer
5644         (gnus-summary-reply
5645          (list (list article contents)) wide)))))
5646
5647 (defun gnus-article-followup-with-original ()
5648   "Compose a followup to the current article.
5649 The text in the region will be yanked.  If the region isn't active,
5650 the entire article will be yanked."
5651   (interactive)
5652   (let ((article (cdr gnus-article-current))
5653         contents)
5654       (if (not (gnus-mark-active-p))
5655           (with-current-buffer gnus-summary-buffer
5656             (gnus-summary-followup (list (list article))))
5657         (setq contents (buffer-substring (point) (mark t)))
5658         ;; Deactivate active regions.
5659         (when (and (boundp 'transient-mark-mode)
5660                    transient-mark-mode)
5661           (setq mark-active nil))
5662         (with-current-buffer gnus-summary-buffer
5663           (gnus-summary-followup
5664            (list (list article contents)))))))
5665
5666 (defun gnus-article-hide (&optional arg force)
5667   "Hide all the gruft in the current article.
5668 This means that signatures, cited text and (some) headers will be
5669 hidden.
5670 If given a prefix, show the hidden text instead."
5671   (interactive (append (gnus-article-hidden-arg) (list 'force)))
5672   (gnus-article-hide-headers arg)
5673   (gnus-article-hide-list-identifiers arg)
5674   (gnus-article-hide-citation-maybe arg force)
5675   (gnus-article-hide-signature arg))
5676
5677 (defun gnus-article-maybe-highlight ()
5678   "Do some article highlighting if article highlighting is requested."
5679   (when (gnus-visual-p 'article-highlight 'highlight)
5680     (gnus-article-highlight-some)))
5681
5682 (defun gnus-check-group-server ()
5683   ;; Make sure the connection to the server is alive.
5684   (unless (gnus-server-opened
5685            (gnus-find-method-for-group gnus-newsgroup-name))
5686     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
5687     (gnus-request-group gnus-newsgroup-name t)))
5688
5689 (eval-when-compile
5690   (autoload 'nneething-get-file-name "nneething"))
5691
5692 (defun gnus-request-article-this-buffer (article group)
5693   "Get an article and insert it into this buffer."
5694   (let (do-update-line sparse-header)
5695     (prog1
5696         (save-excursion
5697           (erase-buffer)
5698           (gnus-kill-all-overlays)
5699           (setq group (or group gnus-newsgroup-name))
5700
5701           ;; Using `gnus-request-article' directly will insert the article into
5702           ;; `nntp-server-buffer' - so we'll save some time by not having to
5703           ;; copy it from the server buffer into the article buffer.
5704
5705           ;; We only request an article by message-id when we do not have the
5706           ;; headers for it, so we'll have to get those.
5707           (when (stringp article)
5708             (gnus-read-header article))
5709
5710           ;; If the article number is negative, that means that this article
5711           ;; doesn't belong in this newsgroup (possibly), so we find its
5712           ;; message-id and request it by id instead of number.
5713           (when (and (numberp article)
5714                      gnus-summary-buffer
5715                      (get-buffer gnus-summary-buffer)
5716                      (gnus-buffer-exists-p gnus-summary-buffer))
5717             (save-excursion
5718               (set-buffer gnus-summary-buffer)
5719               (let ((header (gnus-summary-article-header article)))
5720                 (when (< article 0)
5721                   (cond
5722                    ((memq article gnus-newsgroup-sparse)
5723                     ;; This is a sparse gap article.
5724                     (setq do-update-line article)
5725                     (setq article (mail-header-id header))
5726                     (setq sparse-header (gnus-read-header article))
5727                     (setq gnus-newsgroup-sparse
5728                           (delq article gnus-newsgroup-sparse)))
5729                    ((vectorp header)
5730                     ;; It's a real article.
5731                     (setq article (mail-header-id header)))
5732                    (t
5733                     ;; It is an extracted pseudo-article.
5734                     (setq article 'pseudo)
5735                     (gnus-request-pseudo-article header))))
5736
5737                 (let ((method (gnus-find-method-for-group
5738                                gnus-newsgroup-name)))
5739                   (when (and (eq (car method) 'nneething)
5740                              (vectorp header))
5741                     (let ((dir (nneething-get-file-name
5742                                 (mail-header-id header))))
5743                       (when (and (stringp dir)
5744                                  (file-directory-p dir))
5745                         (setq article 'nneething)
5746                         (gnus-group-enter-directory dir))))))))
5747
5748           (cond
5749            ;; Refuse to select canceled articles.
5750            ((and (numberp article)
5751                  gnus-summary-buffer
5752                  (get-buffer gnus-summary-buffer)
5753                  (gnus-buffer-exists-p gnus-summary-buffer)
5754                  (eq (cdr (save-excursion
5755                             (set-buffer gnus-summary-buffer)
5756                             (assq article gnus-newsgroup-reads)))
5757                      gnus-canceled-mark))
5758             nil)
5759            ;; We first check `gnus-original-article-buffer'.
5760            ((and (get-buffer gnus-original-article-buffer)
5761                  (numberp article)
5762                  (save-excursion
5763                    (set-buffer gnus-original-article-buffer)
5764                    (and (equal (car gnus-original-article) group)
5765                         (eq (cdr gnus-original-article) article))))
5766             (insert-buffer-substring gnus-original-article-buffer)
5767             'article)
5768            ;; Check the backlog.
5769            ((and gnus-keep-backlog
5770                  (gnus-backlog-request-article group article (current-buffer)))
5771             'article)
5772            ;; Check asynchronous pre-fetch.
5773            ((gnus-async-request-fetched-article group article (current-buffer))
5774             (gnus-async-prefetch-next group article gnus-summary-buffer)
5775             (when (and (numberp article) gnus-keep-backlog)
5776               (gnus-backlog-enter-article group article (current-buffer)))
5777             'article)
5778            ;; Check the cache.
5779            ((and gnus-use-cache
5780                  (numberp article)
5781                  (gnus-cache-request-article article group))
5782             'article)
5783            ;; Check the agent cache.
5784            ((gnus-agent-request-article article group)
5785             'article)
5786            ;; Get the article and put into the article buffer.
5787            ((or (stringp article)
5788                 (numberp article))
5789             (let ((gnus-override-method gnus-override-method)
5790                   (methods (and (stringp article)
5791                                 gnus-refer-article-method))
5792                   (backend (car (gnus-find-method-for-group
5793                                  gnus-newsgroup-name)))
5794                   result
5795                   (buffer-read-only nil))
5796               (if (or (not (listp methods))
5797                       (and (symbolp (car methods))
5798                            (assq (car methods) nnoo-definition-alist)))
5799                   (setq methods (list methods)))
5800               (when (and (null gnus-override-method)
5801                          methods)
5802                 (setq gnus-override-method (pop methods)))
5803               (while (not result)
5804                 (when (eq gnus-override-method 'current)
5805                   (setq gnus-override-method
5806                         (with-current-buffer gnus-summary-buffer
5807                           gnus-current-select-method)))
5808                 (erase-buffer)
5809                 (gnus-kill-all-overlays)
5810                 (let ((gnus-newsgroup-name group))
5811                   (gnus-check-group-server))
5812                 (cond
5813                  ((gnus-request-article article group (current-buffer))
5814                   (when (numberp article)
5815                     (gnus-async-prefetch-next group article
5816                                               gnus-summary-buffer)
5817                     (when gnus-keep-backlog
5818                       (gnus-backlog-enter-article
5819                        group article (current-buffer))))
5820                   (setq result 'article))
5821                  (methods
5822                   (setq gnus-override-method (pop methods)))
5823                  ((not (string-match "^400 "
5824                                      (nnheader-get-report backend)))
5825                   ;; If we get 400 server disconnect, reconnect and
5826                   ;; retry; otherwise, assume the article has expired.
5827                   (setq result 'done))))
5828               (and (eq result 'article) 'article)))
5829            ;; It was a pseudo.
5830            (t article)))
5831
5832       ;; Associate this article with the current summary buffer.
5833       (setq gnus-article-current-summary gnus-summary-buffer)
5834
5835       ;; Take the article from the original article buffer
5836       ;; and place it in the buffer it's supposed to be in.
5837       (when (and (get-buffer gnus-article-buffer)
5838                  (equal (buffer-name (current-buffer))
5839                         (buffer-name (get-buffer gnus-article-buffer))))
5840         (save-excursion
5841           (if (get-buffer gnus-original-article-buffer)
5842               (set-buffer gnus-original-article-buffer)
5843             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
5844             (buffer-disable-undo)
5845             (setq major-mode 'gnus-original-article-mode)
5846             (setq buffer-read-only t))
5847           (let (buffer-read-only)
5848             (erase-buffer)
5849             (insert-buffer-substring gnus-article-buffer))
5850           (setq gnus-original-article (cons group article)))
5851
5852         ;; Decode charsets.
5853         (run-hooks 'gnus-article-decode-hook)
5854         ;; Mark article as decoded or not.
5855         (setq gnus-article-decoded-p gnus-article-decode-hook))
5856
5857       ;; Update sparse articles.
5858       (when (and do-update-line
5859                  (or (numberp article)
5860                      (stringp article)))
5861         (let ((buf (current-buffer)))
5862           (set-buffer gnus-summary-buffer)
5863           (gnus-summary-update-article do-update-line sparse-header)
5864           (gnus-summary-goto-subject do-update-line nil t)
5865           (set-window-point (gnus-get-buffer-window (current-buffer) t)
5866                             (point))
5867           (set-buffer buf))))))
5868
5869 ;;;
5870 ;;; Article editing
5871 ;;;
5872
5873 (defcustom gnus-article-edit-mode-hook nil
5874   "Hook run in article edit mode buffers."
5875   :group 'gnus-article-various
5876   :type 'hook)
5877
5878 (defcustom gnus-article-edit-article-setup-function
5879   'gnus-article-mime-edit-article-setup
5880   "Function called to setup an editing article buffer."
5881   :group 'gnus-article-various
5882   :type 'function)
5883
5884 (defvar gnus-article-edit-done-function nil)
5885
5886 (defvar gnus-article-edit-mode-map nil)
5887 (defvar gnus-article-edit-mode nil)
5888
5889 ;; Should we be using derived.el for this?
5890 (unless gnus-article-edit-mode-map
5891   (setq gnus-article-edit-mode-map (make-keymap))
5892   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
5893
5894   (gnus-define-keys gnus-article-edit-mode-map
5895     "\C-c?"    describe-mode
5896     "\C-c\C-c" gnus-article-edit-done
5897     "\C-c\C-k" gnus-article-edit-exit
5898     "\C-c\C-f\C-t" message-goto-to
5899     "\C-c\C-f\C-o" message-goto-from
5900     "\C-c\C-f\C-b" message-goto-bcc
5901     ;;"\C-c\C-f\C-w" message-goto-fcc
5902     "\C-c\C-f\C-c" message-goto-cc
5903     "\C-c\C-f\C-s" message-goto-subject
5904     "\C-c\C-f\C-r" message-goto-reply-to
5905     "\C-c\C-f\C-n" message-goto-newsgroups
5906     "\C-c\C-f\C-d" message-goto-distribution
5907     "\C-c\C-f\C-f" message-goto-followup-to
5908     "\C-c\C-f\C-m" message-goto-mail-followup-to
5909     "\C-c\C-f\C-k" message-goto-keywords
5910     "\C-c\C-f\C-u" message-goto-summary
5911     "\C-c\C-f\C-i" message-insert-or-toggle-importance
5912     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
5913     "\C-c\C-b" message-goto-body
5914     "\C-c\C-i" message-goto-signature
5915
5916     "\C-c\C-t" message-insert-to
5917     "\C-c\C-n" message-insert-newsgroups
5918     "\C-c\C-o" message-sort-headers
5919     "\C-c\C-e" message-elide-region
5920     "\C-c\C-v" message-delete-not-region
5921     "\C-c\C-z" message-kill-to-signature
5922     "\M-\r" message-newline-and-reformat
5923     "\C-c\C-a" mml-attach-file
5924     "\C-a" message-beginning-of-line
5925     "\t" message-tab
5926     "\M-;" comment-region)
5927
5928   (gnus-define-keys (gnus-article-edit-wash-map
5929                      "\C-c\C-w" gnus-article-edit-mode-map)
5930     "f" gnus-article-edit-full-stops))
5931
5932 (easy-menu-define
5933   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
5934   '("Field"
5935     ["Fetch To" message-insert-to t]
5936     ["Fetch Newsgroups" message-insert-newsgroups t]
5937     "----"
5938     ["To" message-goto-to t]
5939     ["From" message-goto-from t]
5940     ["Subject" message-goto-subject t]
5941     ["Cc" message-goto-cc t]
5942     ["Reply-To" message-goto-reply-to t]
5943     ["Summary" message-goto-summary t]
5944     ["Keywords" message-goto-keywords t]
5945     ["Newsgroups" message-goto-newsgroups t]
5946     ["Followup-To" message-goto-followup-to t]
5947     ["Mail-Followup-To" message-goto-mail-followup-to t]
5948     ["Distribution" message-goto-distribution t]
5949     ["Body" message-goto-body t]
5950     ["Signature" message-goto-signature t]))
5951
5952 (define-derived-mode gnus-article-edit-mode text-mode "Article Edit"
5953   "Major mode for editing articles.
5954 This is an extended text-mode.
5955
5956 \\{gnus-article-edit-mode-map}"
5957   (make-local-variable 'gnus-article-edit-done-function)
5958   (make-local-variable 'gnus-prev-winconf)
5959   (set (make-local-variable 'font-lock-defaults)
5960        '(message-font-lock-keywords t))
5961   (set (make-local-variable 'mail-header-separator) "")
5962   (set (make-local-variable 'gnus-article-edit-mode) t)
5963   (easy-menu-add message-mode-field-menu message-mode-map)
5964   (setq buffer-read-only nil)
5965   (buffer-enable-undo)
5966   (widen))
5967
5968 (defun gnus-article-edit (&optional force)
5969   "Edit the current article.
5970 This will have permanent effect only in mail groups.
5971 If FORCE is non-nil, allow editing of articles even in read-only
5972 groups."
5973   (interactive "P")
5974   (when (and (not force)
5975              (gnus-group-read-only-p))
5976     (error "The current newsgroup does not support article editing"))
5977   (gnus-article-date-original)
5978   (gnus-article-edit-article
5979    'ignore
5980    `(lambda (no-highlight)
5981       'ignore
5982       (gnus-summary-edit-article-done
5983        ,(or (mail-header-references gnus-current-headers) "")
5984        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
5985
5986 (defun gnus-article-edit-article (start-func exit-func)
5987   "Start editing the contents of the current article buffer."
5988   (let ((winconf (current-window-configuration)))
5989     (set-buffer gnus-article-buffer)
5990     (gnus-article-edit-mode)
5991     (funcall start-func)
5992     (set-buffer-modified-p nil)
5993     (gnus-configure-windows 'edit-article)
5994     (setq gnus-article-edit-done-function exit-func)
5995     (setq gnus-prev-winconf winconf)
5996     (when gnus-article-edit-article-setup-function
5997       (funcall gnus-article-edit-article-setup-function))
5998     (gnus-message 6 "C-c C-c to end edits; C-c C-k to exit")))
5999
6000 (defun gnus-article-edit-done (&optional arg)
6001   "Update the article edits and exit."
6002   (interactive "P")
6003   (let ((func gnus-article-edit-done-function)
6004         (buf (current-buffer))
6005         (start (window-start))
6006         (p (point))
6007         (winconf gnus-prev-winconf))
6008     (remove-hook 'gnus-article-mode-hook
6009                  'gnus-article-mime-edit-article-unwind)
6010     (widen) ;; Widen it in case that users narrowed the buffer.
6011     (funcall func arg)
6012     (set-buffer buf)
6013     ;; The cache and backlog have to be flushed somewhat.
6014     (when gnus-keep-backlog
6015       (gnus-backlog-remove-article
6016        (car gnus-article-current) (cdr gnus-article-current)))
6017     ;; Flush original article as well.
6018     (save-excursion
6019       (when (get-buffer gnus-original-article-buffer)
6020         (set-buffer gnus-original-article-buffer)
6021         (setq gnus-original-article nil)))
6022     (when gnus-use-cache
6023       (gnus-cache-update-article
6024        (car gnus-article-current) (cdr gnus-article-current)))
6025     ;; We remove all text props from the article buffer.
6026     (kill-all-local-variables)
6027     (set-text-properties (point-min) (point-max) nil)
6028     (gnus-article-mode)
6029     (set-window-configuration winconf)
6030     (set-buffer buf)
6031     (set-window-start (get-buffer-window buf) start)
6032     (set-window-point (get-buffer-window buf) (point)))
6033   (gnus-summary-show-article))
6034
6035 (defun gnus-article-edit-exit ()
6036   "Exit the article editing without updating."
6037   (interactive)
6038   (when (or (not (buffer-modified-p))
6039             (yes-or-no-p "Article modified; kill anyway? "))
6040     (let ((curbuf (current-buffer))
6041           (p (point))
6042           (window-start (window-start)))
6043       (erase-buffer)
6044       (if (gnus-buffer-live-p gnus-original-article-buffer)
6045           (insert-buffer-substring gnus-original-article-buffer))
6046       (let ((winconf gnus-prev-winconf))
6047         (kill-all-local-variables)
6048         (gnus-article-mode)
6049         (set-window-configuration winconf)
6050         ;; Tippy-toe some to make sure that point remains where it was.
6051         (save-current-buffer
6052           (set-buffer curbuf)
6053           (set-window-start (get-buffer-window (current-buffer)) window-start)
6054           (goto-char p))))
6055     (gnus-summary-show-article)))
6056
6057 (defun gnus-article-edit-full-stops ()
6058   "Interactively repair spacing at end of sentences."
6059   (interactive)
6060   (save-excursion
6061     (goto-char (point-min))
6062     (search-forward-regexp "^$" nil t)
6063     (let ((case-fold-search nil))
6064       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
6065
6066 ;;;
6067 ;;; Article editing with MIME-Edit
6068 ;;;
6069
6070 (defcustom gnus-article-mime-edit-article-setup-hook nil
6071   "Hook run after setting up a MIME editing article buffer."
6072   :group 'gnus-article-various
6073   :type 'hook)
6074
6075 (defun gnus-article-mime-edit-article-unwind ()
6076   "Unwind `gnus-article-buffer' if article editing was given up."
6077   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
6078   (when (featurep 'font-lock)
6079     (setq font-lock-defaults nil)
6080     (font-lock-mode -1))
6081   (when mime-edit-mode-flag
6082     (mime-edit-exit 'nomime 'no-error)
6083     (message "")))
6084
6085 (defun gnus-article-mime-edit-article-setup ()
6086   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
6087 after replacing with the original article."
6088   (setq gnus-show-mime t)
6089   (setq gnus-article-edit-done-function
6090         `(lambda (&rest args)
6091            (when mime-edit-mode-flag
6092              (let (mime-edit-insert-user-agent-field)
6093                (mime-edit-exit))
6094              (message ""))
6095            (goto-char (point-min))
6096            (let (case-fold-search)
6097              (when (re-search-forward
6098                     (format "^%s$" (regexp-quote mail-header-separator))
6099                     nil t)
6100                (replace-match "")))
6101            (apply ,gnus-article-edit-done-function args)
6102            (insert
6103             (prog1
6104                 (buffer-substring-no-properties (point-min) (point-max))
6105               (set-buffer (get-buffer-create gnus-original-article-buffer))
6106               (erase-buffer)))
6107            (setq gnus-current-headers (gnus-article-make-full-mail-header))
6108            (set-buffer gnus-article-buffer)
6109            (gnus-article-prepare-display)))
6110   (substitute-key-definition 'gnus-article-edit-done
6111                              'gnus-article-mime-edit-done
6112                              gnus-article-edit-mode-map)
6113   (substitute-key-definition 'gnus-article-edit-exit
6114                              'gnus-article-mime-edit-exit
6115                              gnus-article-edit-mode-map)
6116   (erase-buffer)
6117   (insert-buffer-substring gnus-original-article-buffer)
6118   (unless (member (with-current-buffer gnus-summary-buffer
6119                     gnus-newsgroup-name)
6120                   '("nndraft:delayed" "nndraft:drafts"))
6121     (let ((ofn (symbol-function 'mime-edit-decode-single-part-in-buffer)))
6122       (fset 'mime-edit-decode-single-part-in-buffer
6123             (lambda (&rest args)
6124               (if (let ((content-type (car args)))
6125                     (and (eq 'message (mime-content-type-primary-type
6126                                        content-type))
6127                          (eq 'rfc822 (mime-content-type-subtype
6128                                       content-type))))
6129                   (setcar (cdr args) 'not-decode-text))
6130               (apply ofn args)))
6131       (unwind-protect
6132           (mime-edit-again)
6133         (fset 'mime-edit-decode-single-part-in-buffer ofn))))
6134   (when (featurep 'font-lock)
6135     (set (make-local-variable 'font-lock-defaults)
6136          '(message-font-lock-keywords t))
6137     (font-lock-set-defaults)
6138     (turn-on-font-lock))
6139   (set-buffer-modified-p nil)
6140   (delete-other-windows)
6141   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
6142   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
6143
6144 (defun gnus-article-mime-edit-done (&optional arg)
6145   "Update the article MIME edits and exit."
6146   (interactive "P")
6147   (when (featurep 'font-lock)
6148     (setq font-lock-defaults nil)
6149     (font-lock-mode -1))
6150   (let ((inhibit-read-only t))
6151     (gnus-article-edit-done arg)))
6152
6153 (defun gnus-article-mime-edit-exit ()
6154   "Exit the article MIME editing without updating."
6155   (interactive)
6156   (when (or (not (buffer-modified-p))
6157             (yes-or-no-p "Article modified; kill anyway? "))
6158     (when (featurep 'font-lock)
6159       (setq font-lock-defaults nil)
6160       (font-lock-mode -1))
6161     (when mime-edit-mode-flag
6162       (let (mime-edit-insert-user-agent-field)
6163         (mime-edit-exit))
6164       (message ""))
6165     (goto-char (point-min))
6166     (let (case-fold-search)
6167       (when (re-search-forward
6168              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
6169         (replace-match "")))
6170     (let ((winconf gnus-prev-winconf))
6171       (insert (prog1
6172                   (buffer-substring-no-properties (point-min) (point-max))
6173                 (set-buffer (get-buffer-create gnus-original-article-buffer))
6174                 (erase-buffer)))
6175       (setq gnus-current-headers (gnus-article-make-full-mail-header))
6176       (set-buffer gnus-article-buffer)
6177       (gnus-article-prepare-display)
6178       (set-window-configuration winconf))))
6179
6180 ;;;
6181 ;;; Article highlights
6182 ;;;
6183
6184 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
6185
6186 ;;; Internal Variables:
6187
6188 (defcustom gnus-button-url-regexp
6189   (if (string-match "[[:digit:]]" "1") ;; support POSIX?
6190       "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)\\(//[-a-z0-9_.]+:[0-9]*\\)?[-a-z0-9_=!?#$@~%&*+\\/:;.,[:word:]]+[-a-z0-9_=#$@~%&*+\\/[:word:]]\\)"
6191     "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)\\(//[-a-z0-9_.]+:[0-9]*\\)?\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)\\)")
6192   "Regular expression that matches URLs."
6193   :group 'gnus-article-buttons
6194   :type 'regexp)
6195
6196 (defcustom gnus-button-valid-fqdn-regexp
6197   message-valid-fqdn-regexp
6198   "Regular expression that matches a valid FQDN."
6199   :group 'gnus-article-buttons
6200   :type 'regexp)
6201
6202 (defcustom gnus-button-man-handler 'manual-entry
6203   "Function to use for displaying man pages.
6204 The function must take at least one argument with a string naming the
6205 man page."
6206   :type '(choice (function-item :tag "Man" manual-entry)
6207                  (function-item :tag "Woman" woman)
6208                  (function :tag "Other"))
6209   :group 'gnus-article-buttons)
6210
6211 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
6212   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
6213 If the default site is too slow, try to find a CTAN mirror, see
6214 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
6215 the variable `gnus-button-handle-ctan'."
6216   :group 'gnus-article-buttons
6217   :link '(custom-manual "(gnus)Group Parameters")
6218   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
6219                  (const "http://tug.ctan.org/tex-archive/")
6220                  (const "http://www.dante.de/CTAN/")
6221                  (string :tag "Other")))
6222
6223 (defcustom gnus-button-ctan-handler 'browse-url
6224   "Function to use for displaying CTAN links.
6225 The function must take one argument, the string naming the URL."
6226   :type '(choice (function-item :tag "Browse Url" browse-url)
6227                  (function :tag "Other"))
6228   :group 'gnus-article-buttons)
6229
6230 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
6231   "Bogus strings removed from CTAN URLs."
6232   :group 'gnus-article-buttons
6233   :type '(choice (const "^/?tex-archive/\\|/")
6234                  (regexp :tag "Other")))
6235
6236 (defcustom gnus-button-ctan-directory-regexp
6237   (concat
6238    "\\("; Cannot use `\(?: ... \)' (compatibility with Emacs 20).
6239    "biblio\\|digests\\|dviware\\|fonts\\|graphics\\|help\\|"
6240    "indexing\\|info\\|language\\|macros\\|support\\|systems\\|"
6241    "tds\\|tools\\|usergrps\\|web\\|nonfree\\|obsolete"
6242    "\\)")
6243   "Regular expression for ctan directories.
6244 It should match all directories in the top level of `gnus-ctan-url'."
6245   :group 'gnus-article-buttons
6246   :type 'regexp)
6247
6248 (defcustom gnus-button-mid-or-mail-regexp
6249   (concat "\\b\\(<?[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t ]*@"
6250           ;; Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
6251           gnus-button-valid-fqdn-regexp
6252           ">?\\)\\b")
6253   "Regular expression that matches a message ID or a mail address."
6254   :group 'gnus-article-buttons
6255   :type 'regexp)
6256
6257 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
6258   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
6259 Strings like this can be either a message ID or a mail address.  If it is one
6260 of the symbols `mid' or `mail', Gnus will always assume that the string is a
6261 message ID or a mail address, respectively.  If this variable is set to the
6262 symbol `ask', always query the user what do do.  If it is a function, this
6263 function will be called with the string as it's only argument.  The function
6264 must return `mid', `mail', `invalid' or `ask'."
6265   :group 'gnus-article-buttons
6266   :type '(choice (function-item :tag "Heuristic function"
6267                                 gnus-button-mid-or-mail-heuristic)
6268                  (const ask)
6269                  (const mid)
6270                  (const mail)))
6271
6272 (defcustom gnus-button-mid-or-mail-heuristic-alist
6273   '((-10.0 . ".+\\$.+@")
6274     (-10.0 . "#")
6275     (-10.0 . "\\*")
6276     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
6277     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
6278     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
6279     (-1.0  . "^[^a-z]+@")
6280     ;;
6281     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
6282     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
6283     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
6284     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
6285     ;;
6286     (-2.0  . "^[0-9]")
6287     (-1.0  . "^[0-9][0-9]")
6288     ;;
6289     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
6290     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
6291     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
6292     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
6293     ;;
6294     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
6295     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
6296     ;;       "[0-9]{8,}.*\@"
6297     (-3.0
6298      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
6299     ;; "[0-9]{12,}.*\@"
6300     ;; compensation for TDMA dated mail addresses:
6301     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
6302     ;;
6303     (-20.0 . "\\.fsf@") ;; Gnus
6304     (-20.0 . "^slrn")
6305     (-20.0 . "^Pine")
6306     (-20.0 . "_-_") ;; Subject change in thread
6307     ;;
6308     (-20.0 . "\\.ln@") ;; leafnode
6309     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
6310     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
6311     ;;
6312     ;; (5.0 . "") ;; $local_part_len <= 7
6313     (10.0  . "^[^0-9]+@")
6314     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
6315     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
6316     (3.0   . "\@stud")
6317     ;;
6318     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
6319     ;;
6320     (0.5   . "^[A-Z][a-z]")
6321     (0.5   . "^[A-Z][a-z][a-z]")
6322     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
6323     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
6324   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
6325
6326 A negative RATE indicates a message IDs, whereas a positive indicates a mail
6327 address.  The REGEXP is processed with `case-fold-search' set to nil."
6328   :group 'gnus-article-buttons
6329   :type '(repeat (cons (number :tag "Rate")
6330                        (regexp :tag "Regexp"))))
6331
6332 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
6333   "Guess whether MID-OR-MAIL is a message ID or a mail address.
6334 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
6335 address, `ask' if unsure and `invalid' if the string is invalid."
6336   (let ((case-fold-search nil)
6337         (list gnus-button-mid-or-mail-heuristic-alist)
6338         (result 0) rate regexp lpartlen elem)
6339     (setq lpartlen
6340           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
6341     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
6342     ;; Certain special cases...
6343     (when (string-match
6344            (concat
6345             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
6346             "^[0-9]+\\.[0-9]+@compuserve\\|"
6347             "@public\\.gmane\\.org")
6348            mid-or-mail)
6349       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
6350       (setq result 'mail))
6351     (when (string-match "@.*@\\| " mid-or-mail)
6352       (gnus-message 8 "`%s' is invalid." mid-or-mail)
6353       (setq result 'invalid))
6354     ;; Nothing more to do, if result is not a number here...
6355     (when (numberp result)
6356       (while list
6357         (setq elem (car list)
6358               rate (car elem)
6359               regexp (cdr elem)
6360               list (cdr list))
6361         (when (string-match regexp mid-or-mail)
6362           (setq result (+ result rate))
6363           (gnus-message
6364            9 "`%s' matched `%s', rate `%s', result `%s'."
6365            mid-or-mail regexp rate result)))
6366       (when (<= lpartlen 7)
6367         (setq result (+ result 5.0))
6368         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
6369                       mid-or-mail result))
6370       (when (>= lpartlen 12)
6371         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
6372         (cond
6373          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
6374           ;; Long local part should contain realname if e-mail address,
6375           ;; too many digits: message-id.
6376           ;; $score -= 5.0 + 0.1 * $local_part_len;
6377           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
6378           (setq result (+ result rate))
6379           (gnus-message
6380            9 "Many digits in `%s', rate `%s', result `%s'."
6381            mid-or-mail rate result))
6382          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
6383                         mid-or-mail)
6384           ;; Too few vowels [^aeiouy]{4,}.*\@
6385           (setq result (+ result -5.0))
6386           (gnus-message
6387            9 "Few vowels in `%s', rate `%s', result `%s'."
6388            mid-or-mail -5.0 result))
6389          (t
6390           (setq result (+ result 5.0))
6391           (gnus-message
6392            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
6393     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
6394     ;; Maybe we should make this a customizable alist: (condition . 'result)
6395     (cond
6396      ((symbolp result) result)
6397      ;; Now convert number into proper results:
6398      ((< result -10.0) 'mid)
6399      ((> result  10.0) 'mail)
6400      (t 'ask))))
6401
6402 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
6403   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
6404          (url-mid (concat "news" ":" mid-or-mail))
6405          (url-mailto (concat "mailto" ":" mid-or-mail)))
6406     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
6407     (when (fboundp pref)
6408       (setq guessed
6409             ;; get rid of surrounding angles...
6410             (funcall pref
6411                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
6412       (if (or (eq 'mid guessed) (eq 'mail guessed))
6413           (setq pref guessed)
6414         (setq pref 'ask)))
6415     (if (eq pref 'ask)
6416         (save-window-excursion
6417           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
6418               (setq pref 'mail)
6419             (setq pref 'mid))))
6420     (cond ((eq pref 'mid)
6421            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
6422            (gnus-button-handle-news url-mid))
6423           ((eq pref 'mail)
6424            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
6425            (gnus-url-mailto url-mailto))
6426           (t (gnus-message 3 "Invalid string.")))))
6427
6428 (defun gnus-button-handle-custom (url)
6429   "Follow a Custom URL."
6430   (customize-apropos (gnus-url-unhex-string url)))
6431
6432 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
6433
6434 ;; FIXME: Maybe we should merge some of the functions that do quite similar
6435 ;; stuff?
6436
6437 (defun gnus-button-handle-describe-function (url)
6438   "Call `describe-function' when pushing the corresponding URL button."
6439   (describe-function
6440    (intern
6441     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6442
6443 (defun gnus-button-handle-describe-variable (url)
6444   "Call `describe-variable' when pushing the corresponding URL button."
6445   (describe-variable
6446    (intern
6447     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6448
6449 (defun gnus-button-handle-symbol (url)
6450 "Display help on variable or function.
6451 Calls `describe-variable' or `describe-function'."
6452   (let ((sym (intern url)))
6453     (cond
6454      ((fboundp sym) (describe-function sym))
6455      ((boundp sym) (describe-variable sym))
6456      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
6457
6458 (defun gnus-button-handle-describe-key (url)
6459   "Call `describe-key' when pushing the corresponding URL button."
6460   (let* ((key-string
6461           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
6462          (keys (ignore-errors (eval `(kbd ,key-string)))))
6463     (if keys
6464         (describe-key keys)
6465       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
6466
6467 (defun gnus-button-handle-apropos (url)
6468   "Call `apropos' when pushing the corresponding URL button."
6469   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6470
6471 (defun gnus-button-handle-apropos-command (url)
6472   "Call `apropos' when pushing the corresponding URL button."
6473   (apropos-command
6474    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6475
6476 (defun gnus-button-handle-apropos-variable (url)
6477   "Call `apropos' when pushing the corresponding URL button."
6478   (funcall
6479    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
6480    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6481
6482 (defun gnus-button-handle-apropos-documentation (url)
6483   "Call `apropos' when pushing the corresponding URL button."
6484   (funcall
6485    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
6486    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6487
6488 (defun gnus-button-handle-library (url)
6489   "Call `locate-library' when pushing the corresponding URL button."
6490   (gnus-message 9 "url=`%s'" url)
6491   (let* ((lib (locate-library url))
6492          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
6493     (if (not lib)
6494         (gnus-message 1 "Cannot locale library `%s'." url)
6495       (find-file-read-only file))))
6496
6497 (defun gnus-button-handle-ctan (url)
6498   "Call `browse-url' when pushing a CTAN URL button."
6499   (funcall
6500    gnus-button-ctan-handler
6501    (concat
6502     gnus-ctan-url
6503     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
6504
6505 (defcustom gnus-button-tex-level 5
6506   "*Integer that says how many TeX-related buttons Gnus will show.
6507 The higher the number, the more buttons will appear and the more false
6508 positives are possible.  Note that you can set this variable local to
6509 specific groups.  Setting it higher in TeX groups is probably a good idea.
6510 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6511 how to set variables in specific groups."
6512   :group 'gnus-article-buttons
6513   :link '(custom-manual "(gnus)Group Parameters")
6514   :type 'integer)
6515
6516 (defcustom gnus-button-man-level 5
6517   "*Integer that says how many man-related buttons Gnus will show.
6518 The higher the number, the more buttons will appear and the more false
6519 positives are possible.  Note that you can set this variable local to
6520 specific groups.  Setting it higher in Unix groups is probably a good idea.
6521 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6522 how to set variables in specific groups."
6523   :group 'gnus-article-buttons
6524   :link '(custom-manual "(gnus)Group Parameters")
6525   :type 'integer)
6526
6527 (defcustom gnus-button-emacs-level 5
6528   "*Integer that says how many emacs-related buttons Gnus will show.
6529 The higher the number, the more buttons will appear and the more false
6530 positives are possible.  Note that you can set this variable local to
6531 specific groups.  Setting it higher in Emacs or Gnus related groups is
6532 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
6533 `gnus-parameters' on how to set variables in specific groups."
6534   :group 'gnus-article-buttons
6535   :link '(custom-manual "(gnus)Group Parameters")
6536   :type 'integer)
6537
6538 (defcustom gnus-button-message-level 5
6539   "*Integer that says how many buttons for news or mail messages will appear.
6540 The higher the number, the more buttons will appear and the more false
6541 positives are possible."
6542   ;; mail addresses, MIDs, URLs for news, ...
6543   :group 'gnus-article-buttons
6544   :type 'integer)
6545
6546 (defcustom gnus-button-browse-level 5
6547   "*Integer that says how many buttons for browsing will appear.
6548 The higher the number, the more buttons will appear and the more false
6549 positives are possible."
6550   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
6551   :group 'gnus-article-buttons
6552   :type 'integer)
6553
6554 (defcustom gnus-button-alist
6555   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
6556      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
6557     ("\\b\\(nntp\\|news\\):\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t
6558      gnus-button-handle-news 2)
6559     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
6560      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
6561     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
6562      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
6563     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
6564     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
6565      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
6566     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
6567      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
6568     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
6569      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
6570     ;; RFC 2368 (The mailto URL scheme)
6571     ("mailto:\\([-a-z.@_+0-9%=?&]+\\)"
6572      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6573     ("\\bmailto:\\([^ \n\t]+\\)"
6574      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6575     ;; CTAN
6576     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
6577              gnus-button-ctan-directory-regexp
6578              "[^][>)!;:,'\n\t ]+\\)")
6579      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
6580     ((concat "\\btex-archive/\\("
6581              gnus-button-ctan-directory-regexp
6582              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
6583      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
6584     ((concat
6585       "\\b\\("
6586       gnus-button-ctan-directory-regexp
6587       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
6588      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
6589     ;; This is info (home-grown style) <info://foo/bar+baz>
6590     ("\\binfo://\\([^'\">\n\t ]+\\)"
6591      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
6592     ;; Info GNOME style <info:foo#bar_baz>
6593     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
6594      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
6595     ;; Info KDE style <info:(foo)bar baz>
6596     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
6597      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
6598     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
6599      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
6600     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
6601      ;; Info links like `C-h i d m CC Mode RET'
6602      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 2)
6603     ;; This is custom
6604     ("\\bcustom:\\(//\\)?\\([^'\">\n\t ]+\\)"
6605      0 (>= gnus-button-emacs-level 5) gnus-button-handle-custom 2)
6606     ("M-x[ \t\n]customize-[^ ]+[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
6607      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1)
6608     ;; Emacs help commands
6609     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6610      ;; regexp doesn't match arguments containing ` '.
6611      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
6612     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6613      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
6614     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6615      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
6616     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6617      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
6618     ;; The following entries may lead to many false positives so don't enable
6619     ;; them by default (use a high button level):
6620     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>"
6621      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
6622     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
6623      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
6624     ("`\\([a-z][a-z0-9]+-[a-z]+-[-a-z]+\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
6625      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
6626     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
6627      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
6628     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
6629      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
6630     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6631      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
6632     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6633      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
6634     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6635      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
6636     ("`\\(\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
6637      ;; Unlike the other regexps we really have to require quoting
6638      ;; here to determine where it ends.
6639      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
6640     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
6641     ("<URL: *\\([^<>]*\\)>"
6642      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6643     ;; RFC 2396 (2.4.3., delims) ...
6644     ("\"URL: *\\([^\"]*\\)\""
6645      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6646     ;; RFC 2396 (2.4.3., delims) ...
6647     ("\"URL: *\\([^\"]*\\)\""
6648      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
6649     ;; Raw URLs.
6650     (gnus-button-url-regexp
6651      0 (>= gnus-button-browse-level 0) browse-url 0)
6652     ;; man pages
6653     ("\\b\\([a-z][a-z]+\\)([1-9])\\W"
6654      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
6655      gnus-button-handle-man 1)
6656     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
6657     ("\\b\\([a-z][-_.a-z0-9]+\\)([1-9])\\W"
6658      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
6659      gnus-button-handle-man 1)
6660     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
6661     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
6662     ("\\b\\([a-z][-+_.:a-z0-9]+\\)([1-9][X1a-z]*)\\W\\|\\b\\(X\\)([1-9])\\W"
6663      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
6664     ;; MID or mail: To avoid too many false positives we don't try to catch
6665     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
6666     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
6667     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
6668     ;; so that non-ambiguous entries (see above) match first.
6669     (gnus-button-mid-or-mail-regexp
6670      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
6671   "*Alist of regexps matching buttons in article bodies.
6672
6673 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
6674 REGEXP: is the string (case insensitive) matching text around the button (can
6675 also be Lisp expression evaluating to a string),
6676 BUTTON: is the number of the regexp grouping actually matching the button,
6677 FORM: is a Lisp expression which must eval to true for the button to
6678 be added,
6679 CALLBACK: is the function to call when the user push this button, and each
6680 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
6681
6682 CALLBACK can also be a variable, in that case the value of that
6683 variable it the real callback function."
6684   :group 'gnus-article-buttons
6685   :type '(repeat (list (choice regexp variable sexp)
6686                        (integer :tag "Button")
6687                        (sexp :tag "Form")
6688                        (function :tag "Callback")
6689                        (repeat :tag "Par"
6690                                :inline t
6691                                (integer :tag "Regexp group")))))
6692
6693 (defcustom gnus-header-button-alist
6694   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
6695      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
6696     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
6697      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
6698     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
6699      0 (>= gnus-button-message-level 0) gnus-button-mailto 0)
6700     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
6701      0 (>= gnus-button-browse-level 0) browse-url 0)
6702     ("^Subject:" gnus-button-url-regexp
6703      0 (>= gnus-button-browse-level 0) browse-url 0)
6704     ("^[^:]+:" gnus-button-url-regexp
6705      0 (>= gnus-button-browse-level 0) browse-url 0)
6706     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&]+\\)"
6707      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6708     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
6709      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
6710   "*Alist of headers and regexps to match buttons in article heads.
6711
6712 This alist is very similar to `gnus-button-alist', except that each
6713 alist has an additional HEADER element first in each entry:
6714
6715 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
6716
6717 HEADER is a regexp to match a header.  For a fuller explanation, see
6718 `gnus-button-alist'."
6719   :group 'gnus-article-buttons
6720   :group 'gnus-article-headers
6721   :type '(repeat (list (regexp :tag "Header")
6722                        (choice regexp variable)
6723                        (integer :tag "Button")
6724                        (sexp :tag "Form")
6725                        (function :tag "Callback")
6726                        (repeat :tag "Par"
6727                                :inline t
6728                                (integer :tag "Regexp group")))))
6729
6730 (defvar gnus-button-regexp nil)
6731 (defvar gnus-button-marker-list nil)
6732 ;; Regexp matching any of the regexps from `gnus-button-alist'.
6733
6734 (defvar gnus-button-last nil)
6735 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
6736
6737 ;;; Commands:
6738
6739 (defun gnus-article-push-button (event)
6740   "Check text under the mouse pointer for a callback function.
6741 If the text under the mouse pointer has a `gnus-callback' property,
6742 call it with the value of the `gnus-data' text property."
6743   (interactive "e")
6744   (set-buffer (window-buffer (posn-window (event-start event))))
6745   (let* ((pos (posn-point (event-start event)))
6746          (data (get-text-property pos 'gnus-data))
6747          (fun (get-text-property pos 'gnus-callback)))
6748     (goto-char pos)
6749     (when fun
6750       (funcall fun data))))
6751
6752 (defun gnus-article-press-button ()
6753   "Check text at point for a callback function.
6754 If the text at point has a `gnus-callback' property,
6755 call it with the value of the `gnus-data' text property."
6756   (interactive)
6757   (let ((data (get-text-property (point) 'gnus-data))
6758         (fun (get-text-property (point) 'gnus-callback)))
6759     (when fun
6760       (funcall fun data))))
6761
6762 (defun gnus-article-prev-button (n)
6763   "Move point to N buttons backward.
6764 If N is negative, move forward instead."
6765   (interactive "p")
6766   (gnus-article-next-button (- n)))
6767
6768 (defun gnus-article-next-button (n)
6769   "Move point to N buttons forward.
6770 If N is negative, move backward instead."
6771   (interactive "p")
6772   (let ((function (if (< n 0) 'previous-single-property-change
6773                     'next-single-property-change))
6774         (inhibit-point-motion-hooks t)
6775         (backward (< n 0))
6776         (limit (if (< n 0) (point-min) (point-max))))
6777     (setq n (abs n))
6778     (while (and (not (= limit (point)))
6779                 (> n 0))
6780       ;; Skip past the current button.
6781       (when (get-text-property (point) 'gnus-callback)
6782         (goto-char (funcall function (point) 'gnus-callback nil limit)))
6783       ;; Go to the next (or previous) button.
6784       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
6785       ;; Put point at the start of the button.
6786       (when (and backward (not (get-text-property (point) 'gnus-callback)))
6787         (goto-char (funcall function (point) 'gnus-callback nil limit)))
6788       ;; Skip past intangible buttons.
6789       (when (get-text-property (point) 'intangible)
6790         (incf n))
6791       (decf n))
6792     (unless (zerop n)
6793       (gnus-message 5 "No more buttons"))
6794     n))
6795
6796 (defun gnus-article-highlight (&optional force)
6797   "Highlight current article.
6798 This function calls `gnus-article-highlight-headers',
6799 `gnus-article-highlight-citation',
6800 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
6801 do the highlighting.  See the documentation for those functions."
6802   (interactive (list 'force))
6803   (gnus-article-highlight-headers)
6804   (gnus-article-highlight-citation force)
6805   (gnus-article-highlight-signature)
6806   (gnus-article-add-buttons force)
6807   (gnus-article-add-buttons-to-head))
6808
6809 (defun gnus-article-highlight-some (&optional force)
6810   "Highlight current article.
6811 This function calls `gnus-article-highlight-headers',
6812 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
6813 do the highlighting.  See the documentation for those functions."
6814   (interactive (list 'force))
6815   (gnus-article-highlight-headers)
6816   (gnus-article-highlight-signature)
6817   (gnus-article-add-buttons))
6818
6819 (defun gnus-article-highlight-headers ()
6820   "Highlight article headers as specified by `gnus-header-face-alist'."
6821   (interactive)
6822   (save-excursion
6823     (set-buffer gnus-article-buffer)
6824     (save-restriction
6825       (let ((alist gnus-header-face-alist)
6826             (buffer-read-only nil)
6827             (case-fold-search t)
6828             (inhibit-point-motion-hooks t)
6829             entry regexp header-face field-face from hpoints fpoints)
6830         (article-narrow-to-head)
6831         (while (setq entry (pop alist))
6832           (goto-char (point-min))
6833           (setq regexp (concat "^\\("
6834                                (if (string-equal "" (nth 0 entry))
6835                                    "[^\t ]"
6836                                  (nth 0 entry))
6837                                "\\)")
6838                 header-face (nth 1 entry)
6839                 field-face (nth 2 entry))
6840           (while (and (re-search-forward regexp nil t)
6841                       (not (eobp)))
6842             (beginning-of-line)
6843             (setq from (point))
6844             (unless (search-forward ":" nil t)
6845               (forward-char 1))
6846             (when (and header-face
6847                        (not (memq (point) hpoints)))
6848               (push (point) hpoints)
6849               (gnus-put-text-property from (point) 'face header-face))
6850             (when (and field-face
6851                        (not (memq (setq from (point)) fpoints)))
6852               (push from fpoints)
6853               (if (re-search-forward "^[^ \t]" nil t)
6854                   (forward-char -2)
6855                 (goto-char (point-max)))
6856               (gnus-put-text-property from (point) 'face field-face))))))))
6857
6858 (defun gnus-article-highlight-signature ()
6859   "Highlight the signature in an article.
6860 It does this by highlighting everything after
6861 `gnus-signature-separator' using `gnus-signature-face'."
6862   (interactive)
6863   (when gnus-signature-face
6864     (save-excursion
6865       (set-buffer gnus-article-buffer)
6866       (let ((buffer-read-only nil)
6867             (inhibit-point-motion-hooks t))
6868         (save-restriction
6869           (when (gnus-article-narrow-to-signature)
6870             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
6871                               'face gnus-signature-face)))))))
6872
6873 (defun gnus-article-buttonize-signature ()
6874   "Add button to the signature."
6875   (interactive)
6876   (save-excursion
6877     (set-buffer gnus-article-buffer)
6878     (let ((buffer-read-only nil)
6879           (inhibit-point-motion-hooks t))
6880       (when (gnus-article-search-signature)
6881         (gnus-article-add-button (match-beginning 0) (match-end 0)
6882                                  'gnus-signature-toggle
6883                                  (set-marker (make-marker)
6884                                              (1+ (match-end 0))))))))
6885
6886 (defun gnus-button-in-region-p (b e prop)
6887   "Say whether PROP exists in the region."
6888   (text-property-not-all b e prop nil))
6889
6890 (defun gnus-article-add-buttons (&optional force)
6891   "Find external references in the article and make buttons of them.
6892 \"External references\" are things like Message-IDs and URLs, as
6893 specified by `gnus-button-alist'."
6894   (interactive (list 'force))
6895   (save-excursion
6896     (set-buffer gnus-article-buffer)
6897     (let ((buffer-read-only nil)
6898           (inhibit-point-motion-hooks t)
6899           (case-fold-search t)
6900           (alist gnus-button-alist)
6901           beg entry regexp)
6902       ;; Remove all old markers.
6903       (let (marker entry new-list)
6904         (while (setq marker (pop gnus-button-marker-list))
6905           (if (or (< marker (point-min)) (>= marker (point-max)))
6906               (push marker new-list)
6907             (goto-char marker)
6908             (when (setq entry (gnus-button-entry))
6909               (put-text-property (match-beginning (nth 1 entry))
6910                                  (match-end (nth 1 entry))
6911                                  'gnus-callback nil))
6912             (set-marker marker nil)))
6913         (setq gnus-button-marker-list new-list))
6914       ;; We skip the headers.
6915       (article-goto-body)
6916       (setq beg (point))
6917       (while (setq entry (pop alist))
6918         (setq regexp (eval (car entry)))
6919         (goto-char beg)
6920         (while (re-search-forward regexp nil t)
6921           (let* ((start (and entry (match-beginning (nth 1 entry))))
6922                  (end (and entry (match-end (nth 1 entry))))
6923                  (from (match-beginning 0)))
6924             (when (and (or (eq t (nth 2 entry))
6925                            (eval (nth 2 entry)))
6926                        (not (gnus-button-in-region-p
6927                              start end 'gnus-callback)))
6928               ;; That optional form returned non-nil, so we add the
6929               ;; button.
6930               (gnus-article-add-button
6931                start end 'gnus-button-push
6932                (car (push (set-marker (make-marker) from)
6933                           gnus-button-marker-list))))))))))
6934
6935 ;; Add buttons to the head of an article.
6936 (defun gnus-article-add-buttons-to-head ()
6937   "Add buttons to the head of the article."
6938   (interactive)
6939   (save-excursion
6940     (set-buffer gnus-article-buffer)
6941     (save-restriction
6942       (let ((buffer-read-only nil)
6943             (inhibit-point-motion-hooks t)
6944             (case-fold-search t)
6945             (alist gnus-header-button-alist)
6946             entry beg end)
6947         (article-narrow-to-head)
6948         (while alist
6949           ;; Each alist entry.
6950           (setq entry (car alist)
6951                 alist (cdr alist))
6952           (goto-char (point-min))
6953           (while (re-search-forward (car entry) nil t)
6954             ;; Each header matching the entry.
6955             (setq beg (match-beginning 0))
6956             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
6957                                (match-beginning 0))
6958                           (point-max)))
6959             (goto-char beg)
6960             (while (re-search-forward (eval (nth 1 entry)) end t)
6961               ;; Each match within a header.
6962               (let* ((entry (cdr entry))
6963                      (start (match-beginning (nth 1 entry)))
6964                      (end (match-end (nth 1 entry)))
6965                      (form (nth 2 entry)))
6966                 (goto-char (match-end 0))
6967                 (when (eval form)
6968                   (gnus-article-add-button
6969                    start end (nth 3 entry)
6970                    (buffer-substring (match-beginning (nth 4 entry))
6971                                      (match-end (nth 4 entry)))))))
6972             (goto-char end)))))))
6973
6974 ;;; External functions:
6975
6976 (defun gnus-article-add-button (from to fun &optional data)
6977   "Create a button between FROM and TO with callback FUN and data DATA."
6978   (when gnus-article-button-face
6979     (gnus-overlay-put (gnus-make-overlay from to)
6980                       'face gnus-article-button-face))
6981   (gnus-add-text-properties
6982    from to
6983    (nconc (and gnus-article-mouse-face
6984                (list gnus-mouse-face-prop gnus-article-mouse-face))
6985           (list 'gnus-callback fun)
6986           (and data (list 'gnus-data data))))
6987   (widget-convert-button 'link from to :action 'gnus-widget-press-button
6988                          ;; Quote `:button-keymap' for Mule 2.3
6989                          ;; but it won't work.
6990                          ':button-keymap gnus-widget-button-keymap))
6991
6992 ;;; Internal functions:
6993
6994 (defun gnus-article-set-globals ()
6995   (save-excursion
6996     (set-buffer gnus-summary-buffer)
6997     (gnus-set-global-variables)))
6998
6999 (defun gnus-signature-toggle (end)
7000   (save-excursion
7001     (set-buffer gnus-article-buffer)
7002     (let ((buffer-read-only nil)
7003           (inhibit-point-motion-hooks t)
7004           (limit (next-single-property-change end 'mime-view-entity
7005                                               nil (point-max))))
7006       (if (text-property-any end limit 'article-type 'signature)
7007           (progn
7008             (gnus-delete-wash-type 'signature)
7009             (gnus-remove-text-properties-when
7010              'article-type 'signature end limit
7011              (cons 'article-type (cons 'signature
7012                                        gnus-hidden-properties))))
7013         (gnus-add-wash-type 'signature)
7014         (gnus-add-text-properties-when
7015          'article-type nil end limit
7016          (cons 'article-type (cons 'signature
7017                                    gnus-hidden-properties)))))
7018     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7019       (gnus-set-mode-line 'article))))
7020
7021 (defun gnus-button-entry ()
7022   ;; Return the first entry in `gnus-button-alist' matching this place.
7023   (let ((alist gnus-button-alist)
7024         (entry nil))
7025     (while alist
7026       (setq entry (pop alist))
7027       (if (looking-at (eval (car entry)))
7028           (setq alist nil)
7029         (setq entry nil)))
7030     entry))
7031
7032 (defun gnus-button-push (marker)
7033   ;; Push button starting at MARKER.
7034   (save-excursion
7035     (goto-char marker)
7036     (let* ((entry (gnus-button-entry))
7037            (inhibit-point-motion-hooks t)
7038            (fun (nth 3 entry))
7039            (args (mapcar (lambda (group)
7040                            (let ((string (match-string group)))
7041                              (set-text-properties
7042                               0 (length string) nil string)
7043                              string))
7044                          (nthcdr 4 entry))))
7045       (cond
7046        ((fboundp fun)
7047         (apply fun args))
7048        ((and (boundp fun)
7049              (fboundp (symbol-value fun)))
7050         (apply (symbol-value fun) args))
7051        (t
7052         (gnus-message 1 "You must define `%S' to use this button"
7053                       (cons fun args)))))))
7054
7055 (defun gnus-parse-news-url (url)
7056   (let (scheme server group message-id articles)
7057     (with-temp-buffer
7058       (insert url)
7059       (goto-char (point-min))
7060       (when (looking-at "\\([A-Za-z]+\\):")
7061         (setq scheme (match-string 1))
7062         (goto-char (match-end 0)))
7063       (when (looking-at "//\\([^/]+\\)/")
7064         (setq server (match-string 1))
7065         (goto-char (match-end 0)))
7066
7067       (cond
7068        ((looking-at "\\(.*@.*\\)")
7069         (setq message-id (match-string 1)))
7070        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7071         (setq group (match-string 1)
7072               articles (split-string (match-string 2) "-")))
7073        ((looking-at "\\([^/]+\\)/?")
7074         (setq group (match-string 1)))
7075        (t
7076         (error "Unknown news URL syntax"))))
7077     (list scheme server group message-id articles)))
7078
7079 (defun gnus-button-handle-news (url)
7080   "Fetch a news URL."
7081   (destructuring-bind (scheme server group message-id articles)
7082       (gnus-parse-news-url url)
7083     (cond
7084      (message-id
7085       (save-excursion
7086         (set-buffer gnus-summary-buffer)
7087         (if server
7088             (let ((gnus-refer-article-method (list (list 'nntp server))))
7089               (gnus-summary-refer-article message-id))
7090           (gnus-summary-refer-article message-id))))
7091      (group
7092       (gnus-button-fetch-group url)))))
7093
7094 (defun gnus-button-handle-man (url)
7095   "Fetch a man page."
7096   (funcall gnus-button-man-handler url))
7097
7098 (defun gnus-button-handle-info-url (url)
7099   "Fetch an info URL."
7100   (setq url (mm-subst-char-in-string ?+ ?\  url))
7101   (cond
7102    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
7103     (gnus-info-find-node
7104      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
7105                      "Gnus")
7106              ")" (gnus-url-unhex-string (match-string 2 url)))))
7107    ((string-match "([^)\"]+)[^\"]+" url)
7108     (setq url
7109           (gnus-replace-in-string
7110            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
7111     (gnus-info-find-node url))
7112    (t (error "Can't parse %s" url))))
7113
7114 (defun gnus-button-handle-info-url-gnome (url)
7115   "Fetch GNOME style info URL."
7116   (setq url (mm-subst-char-in-string ?_ ?\  url))
7117   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
7118       (gnus-info-find-node
7119        (concat "("
7120                (gnus-url-unhex-string 
7121                  (match-string 1 url))
7122                ")"
7123                (or (gnus-url-unhex-string 
7124                     (match-string 2 url))
7125                    "Top")))
7126     (error "Can't parse %s" url)))
7127
7128 (defun gnus-button-handle-info-url-kde (url)
7129   "Fetch KDE style info URL."
7130   (gnus-info-find-node (gnus-url-unhex-string url)))
7131
7132 (defun gnus-button-handle-info-keystrokes (url)
7133   "Call `info' when pushing the corresponding URL button."
7134   ;; For links like `C-h i d m gnus RET', `C-h i d m CC Mode RET'.
7135   (info)
7136   (Info-directory)
7137   (Info-menu url))
7138
7139 (defun gnus-button-message-id (message-id)
7140   "Fetch MESSAGE-ID."
7141   (save-excursion
7142     (set-buffer gnus-summary-buffer)
7143     (gnus-summary-refer-article message-id)))
7144
7145 (defun gnus-button-fetch-group (address)
7146   "Fetch GROUP specified by ADDRESS."
7147   (if (not (string-match "[:/]" address))
7148       ;; This is just a simple group url.
7149       (gnus-group-read-ephemeral-group address gnus-select-method)
7150     (if (not
7151          (string-match
7152           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
7153           address))
7154         (error "Can't parse %s" address)
7155       (gnus-group-read-ephemeral-group
7156        (match-string 4 address)
7157        `(nntp ,(match-string 1 address)
7158               (nntp-address ,(match-string 1 address))
7159               (nntp-port-number ,(if (match-end 3)
7160                                      (match-string 3 address)
7161                                    "nntp")))
7162        nil nil nil
7163        (and (match-end 6) (list (string-to-int (match-string 6 address))))))))
7164
7165 (defun gnus-url-parse-query-string (query &optional downcase)
7166   (let (retval pairs cur key val)
7167     (setq pairs (split-string query "&"))
7168     (while pairs
7169       (setq cur (car pairs)
7170             pairs (cdr pairs))
7171       (if (not (string-match "=" cur))
7172           nil                           ; Grace
7173         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
7174               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
7175         (if downcase
7176             (setq key (downcase key)))
7177         (setq cur (assoc key retval))
7178         (if cur
7179             (setcdr cur (cons val (cdr cur)))
7180           (setq retval (cons (list key val) retval)))))
7181     retval))
7182
7183 (defun gnus-url-mailto (url)
7184   ;; Send mail to someone
7185   (when (string-match "mailto:/*\\(.*\\)" url)
7186     (setq url (substring url (match-beginning 1) nil)))
7187   (let (to args subject func)
7188     (setq args (gnus-url-parse-query-string
7189                 (if (string-match "^\\?" url)
7190                     (substring url 1)
7191                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
7192                       (concat "to=" (match-string 1 url) "&"
7193                               (match-string 2 url))
7194                     (concat "to=" url)))
7195                 t)
7196           subject (cdr-safe (assoc "subject" args)))
7197     (gnus-msg-mail)
7198     (while args
7199       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
7200       (if (fboundp func)
7201           (funcall func)
7202         (message-position-on-field (caar args)))
7203       (insert (gnus-replace-in-string
7204                (mapconcat 'identity (reverse (cdar args)) ", ")
7205                "\r\n" "\n" t))
7206       (setq args (cdr args)))
7207     (if subject
7208         (message-goto-body)
7209       (message-goto-subject))))
7210
7211 (defun gnus-button-embedded-url (address)
7212   "Activate ADDRESS with `browse-url'."
7213   (browse-url (gnus-strip-whitespace address)))
7214
7215 ;;; Next/prev buttons in the article buffer.
7216
7217 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
7218 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
7219
7220 (defvar gnus-prev-page-map
7221   (let ((map (make-sparse-keymap)))
7222     (unless (>= emacs-major-version 21)
7223       ;; XEmacs doesn't care.
7224       (set-keymap-parent map gnus-article-mode-map))
7225     (define-key map gnus-mouse-2 'gnus-button-prev-page)
7226     (define-key map "\r" 'gnus-button-prev-page)
7227     map))
7228
7229 (defun gnus-insert-prev-page-button ()
7230   (let ((b (point))
7231         (buffer-read-only nil)
7232         (situation (get-text-property (point-min) 'mime-view-situation)))
7233     (gnus-eval-format
7234      gnus-prev-page-line-format nil
7235      `(keymap ,gnus-prev-page-map
7236          gnus-prev t
7237          gnus-callback gnus-article-button-prev-page
7238          article-type annotation
7239          mime-view-situation ,situation))
7240     (widget-convert-button
7241      'link b (if (bolp)
7242                  ;; Exclude a newline.
7243                  (1- (point))
7244                (point))
7245      :action 'gnus-button-prev-page
7246      :button-keymap gnus-prev-page-map)))
7247
7248 (defvar gnus-next-page-map
7249   (let ((map (make-sparse-keymap)))
7250     (unless (>= emacs-major-version 21)
7251       ;; XEmacs doesn't care.
7252       (set-keymap-parent map gnus-article-mode-map))
7253     (define-key map gnus-mouse-2 'gnus-button-next-page)
7254     (define-key map "\r" 'gnus-button-next-page)
7255     map))
7256
7257 (defun gnus-button-next-page (&optional args more-args)
7258   "Go to the next page."
7259   (interactive)
7260   (let ((win (selected-window)))
7261     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7262     (gnus-article-next-page)
7263     (select-window win)))
7264
7265 (defun gnus-button-prev-page (&optional args more-args)
7266   "Go to the prev page."
7267   (interactive)
7268   (let ((win (selected-window)))
7269     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7270     (gnus-article-prev-page)
7271     (select-window win)))
7272
7273 (defun gnus-insert-next-page-button ()
7274   (let ((b (point))
7275         (buffer-read-only nil)
7276         (situation (get-text-property (point-min) 'mime-view-situation)))
7277     (gnus-eval-format gnus-next-page-line-format nil
7278                       `(keymap ,gnus-next-page-map
7279                           gnus-next t
7280                           gnus-callback gnus-article-button-next-page
7281                           article-type annotation
7282                           mime-view-situation ,situation))
7283     (widget-convert-button
7284      'link b (if (bolp)
7285                  ;; Exclude a newline.
7286                  (1- (point))
7287                (point))
7288      :action 'gnus-button-next-page
7289      :button-keymap gnus-next-page-map)))
7290
7291 (defun gnus-article-button-next-page (arg)
7292   "Go to the next page."
7293   (interactive "P")
7294   (let ((win (selected-window)))
7295     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7296     (gnus-article-next-page)
7297     (select-window win)))
7298
7299 (defun gnus-article-button-prev-page (arg)
7300   "Go to the prev page."
7301   (interactive "P")
7302   (let ((win (selected-window)))
7303     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7304     (gnus-article-prev-page)
7305     (select-window win)))
7306
7307 (defvar gnus-decode-header-methods
7308   '(mail-decode-encoded-word-region)
7309   "List of methods used to decode headers.
7310
7311 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
7312 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
7313 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
7314 whose names match REGEXP.
7315
7316 For example:
7317 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
7318  mail-decode-encoded-word-region
7319  (\"chinese\" . rfc1843-decode-region))
7320 ")
7321
7322 (defvar gnus-decode-header-methods-cache nil)
7323
7324 (defun gnus-multi-decode-header (start end)
7325   "Apply the functions from `gnus-encoded-word-methods' that match."
7326   (unless (and gnus-decode-header-methods-cache
7327                (eq gnus-newsgroup-name
7328                    (car gnus-decode-header-methods-cache)))
7329     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
7330     (mapcar (lambda (x)
7331               (if (symbolp x)
7332                   (nconc gnus-decode-header-methods-cache (list x))
7333                 (if (and gnus-newsgroup-name
7334                          (string-match (car x) gnus-newsgroup-name))
7335                     (nconc gnus-decode-header-methods-cache
7336                            (list (cdr x))))))
7337             gnus-decode-header-methods))
7338   (let ((xlist gnus-decode-header-methods-cache))
7339     (pop xlist)
7340     (save-restriction
7341       (narrow-to-region start end)
7342       (while xlist
7343         (funcall (pop xlist) (point-min) (point-max))))))
7344
7345 ;;;
7346 ;;; Treatment top-level handling.
7347 ;;;
7348
7349 (defun gnus-treat-article (condition &optional part-number total-parts type)
7350   (let ((length (- (point-max) (point-min)))
7351         (alist gnus-treatment-function-alist)
7352         (article-goto-body-goes-to-point-min-p t)
7353         (treated-type
7354          (or (not type)
7355              (catch 'found
7356                (let ((list gnus-article-treat-types))
7357                  (while list
7358                    (when (string-match (pop list) type)
7359                      (throw 'found t)))))))
7360         (highlightp (gnus-visual-p 'article-highlight 'highlight))
7361         (entity (static-unless (featurep 'xemacs)
7362                   (when (eq 'head condition)
7363                     (get-text-property (point-min) 'mime-view-entity))))
7364         val elem buttonized)
7365     (gnus-run-hooks 'gnus-part-display-hook)
7366     (unless gnus-inhibit-treatment
7367       (dolist (elem alist)
7368         (setq val
7369               (save-excursion
7370                 (when (gnus-buffer-live-p gnus-summary-buffer)
7371                   (set-buffer gnus-summary-buffer))
7372                 (symbol-value (car elem))))
7373         (when (and (or (consp val)
7374                        treated-type)
7375                    (gnus-treat-predicate val)
7376                    (or (not (get (car elem) 'highlight))
7377                        highlightp))
7378           (when (and (not buttonized)
7379                      (memq (car elem)
7380                            '(gnus-treat-hide-signature
7381                              gnus-treat-highlight-signature)))
7382             (gnus-article-buttonize-signature)
7383             (setq buttonized t))
7384           (save-restriction
7385             (funcall (cadr elem)))))
7386       ;; FSF Emacsen does not inherit the existing text properties
7387       ;; in the new text, so we should do it for `mime-view-entity'.
7388       (static-unless (featurep 'xemacs)
7389         (when entity
7390           (put-text-property (point-min) (point-max)
7391                              'mime-view-entity entity))))))
7392
7393 ;; Dynamic variables.
7394 (eval-when-compile
7395   (defvar part-number)
7396   (defvar total-parts)
7397   (defvar type)
7398   (defvar condition)
7399   (defvar length))
7400
7401 (defun gnus-treat-predicate (val)
7402   (cond
7403    ((null val)
7404     nil)
7405    ((and (listp val)
7406          (stringp (car val)))
7407     (apply 'gnus-or (mapcar `(lambda (s)
7408                                (string-match s ,(or gnus-newsgroup-name "")))
7409                             val)))
7410    ((listp val)
7411     (let ((pred (pop val)))
7412       (cond
7413        ((eq pred 'or)
7414         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
7415        ((eq pred 'and)
7416         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
7417        ((eq pred 'not)
7418         (not (gnus-treat-predicate (car val))))
7419        ((eq pred 'typep)
7420         (equal (car val) type))
7421        (t
7422         (error "%S is not a valid predicate" pred)))))
7423    ((eq val 'mime)
7424     gnus-show-mime)
7425    (condition
7426     (eq condition val))
7427    ((eq val t)
7428     t)
7429    ((eq val 'head)
7430     nil)
7431    ((eq val 'last)
7432     (eq part-number total-parts))
7433    ((numberp val)
7434     (< length val))
7435    (t
7436     (error "%S is not a valid value" val))))
7437
7438 (defun gnus-article-encrypt-body (protocol &optional n)
7439   "Encrypt the article body."
7440   (interactive
7441    (list
7442     (or gnus-article-encrypt-protocol
7443         (completing-read "Encrypt protocol: "
7444                          gnus-article-encrypt-protocol-alist
7445                          nil t))
7446     current-prefix-arg))
7447   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
7448     (unless func
7449       (error (format "Can't find the encrypt protocol %s" protocol)))
7450     (if (member gnus-newsgroup-name '("nndraft:delayed"
7451                                       "nndraft:drafts"
7452                                       "nndraft:queue"))
7453         (error "Can't encrypt the article in group %s"
7454                gnus-newsgroup-name))
7455     (gnus-summary-iterate n
7456       (save-excursion
7457         (set-buffer gnus-summary-buffer)
7458         (let ((mail-parse-charset gnus-newsgroup-charset)
7459               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
7460               (summary-buffer gnus-summary-buffer)
7461               references point)
7462           (gnus-set-global-variables)
7463           (when (gnus-group-read-only-p)
7464             (error "The current newsgroup does not support article encrypt"))
7465           (gnus-summary-show-article t)
7466           (setq references
7467                 (or (mail-header-references gnus-current-headers) ""))
7468           (set-buffer gnus-article-buffer)
7469           (let* ((buffer-read-only nil)
7470                  (headers
7471                   (mapcar (lambda (field)
7472                             (and (save-restriction
7473                                    (message-narrow-to-head)
7474                                    (goto-char (point-min))
7475                                    (search-forward field nil t))
7476                                  (prog2
7477                                      (message-narrow-to-field)
7478                                      (buffer-string)
7479                                    (delete-region (point-min) (point-max))
7480                                    (widen))))
7481                           '("Content-Type:" "Content-Transfer-Encoding:"
7482                             "Content-Disposition:"))))
7483             (message-narrow-to-head)
7484             (message-remove-header "MIME-Version")
7485             (goto-char (point-max))
7486             (setq point (point))
7487             (insert (apply 'concat headers))
7488             (widen)
7489             (narrow-to-region point (point-max))
7490             (let ((message-options message-options))
7491               (message-options-set 'message-sender user-mail-address)
7492               (message-options-set 'message-recipients user-mail-address)
7493               (message-options-set 'message-sign-encrypt 'not)
7494               (funcall func))
7495             (goto-char (point-min))
7496             (insert "MIME-Version: 1.0\n")
7497             (widen)
7498             (gnus-summary-edit-article-done
7499              references nil summary-buffer t))
7500           (when gnus-keep-backlog
7501             (gnus-backlog-remove-article
7502              (car gnus-article-current) (cdr gnus-article-current)))
7503           (save-excursion
7504             (when (get-buffer gnus-original-article-buffer)
7505               (set-buffer gnus-original-article-buffer)
7506               (setq gnus-original-article nil)))
7507           (when gnus-use-cache
7508             (gnus-cache-update-article
7509              (car gnus-article-current) (cdr gnus-article-current))))))))
7510
7511 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
7512   "The following specs can be used:
7513 %t  The security MIME type
7514 %i  Additional info
7515 %d  Details
7516 %D  Details if button is pressed")
7517
7518 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
7519   "The following specs can be used:
7520 %t  The security MIME type
7521 %i  Additional info
7522 %d  Details
7523 %D  Details if button is pressed")
7524
7525 (defvar gnus-mime-security-button-line-format-alist
7526   '((?t gnus-tmp-type ?s)
7527     (?i gnus-tmp-info ?s)
7528     (?d gnus-tmp-details ?s)
7529     (?D gnus-tmp-pressed-details ?s)))
7530
7531 (defvar gnus-mime-security-button-map
7532   (let ((map (make-sparse-keymap)))
7533     (unless (>= (string-to-number emacs-version) 21)
7534       (set-keymap-parent map gnus-article-mode-map))
7535     (define-key map gnus-mouse-2 'gnus-article-push-button)
7536     (define-key map "\r" 'gnus-article-press-button)
7537     map))
7538
7539 (defvar gnus-mime-security-details-buffer nil)
7540
7541 (defvar gnus-mime-security-button-pressed nil)
7542
7543 (defvar gnus-mime-security-show-details-inline t
7544   "If non-nil, show details in the article buffer.")
7545
7546 (defun gnus-mime-security-verify-or-decrypt (handle)
7547   (mm-remove-parts (cdr handle))
7548   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
7549         point buffer-read-only)
7550     (if region
7551         (goto-char (car region)))
7552     (save-restriction
7553       (narrow-to-region (point) (point))
7554       (with-current-buffer (mm-handle-multipart-original-buffer handle)
7555         (let* ((mm-verify-option 'known)
7556                (mm-decrypt-option 'known)
7557                (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
7558           (unless (eq nparts (cdr handle))
7559             (mm-destroy-parts (cdr handle))
7560             (setcdr handle nparts))))
7561       (setq point (point))
7562       (gnus-mime-display-security handle)
7563       (goto-char (point-max)))
7564     (when region
7565       (delete-region (point) (cdr region))
7566       (set-marker (car region) nil)
7567       (set-marker (cdr region) nil))
7568     (goto-char point)))
7569
7570 (defun gnus-mime-security-show-details (handle)
7571   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
7572     (if (not details)
7573         (gnus-message 5 "No details.")
7574       (if gnus-mime-security-show-details-inline
7575           (let ((gnus-mime-security-button-pressed
7576                  (not (get-text-property (point) 'gnus-mime-details)))
7577                 (gnus-mime-security-button-line-format
7578                  (get-text-property (point) 'gnus-line-format))
7579                 buffer-read-only)
7580             (forward-char -1)
7581             (while (eq (get-text-property (point) 'gnus-line-format)
7582                        gnus-mime-security-button-line-format)
7583               (forward-char -1))
7584             (forward-char)
7585             (save-restriction
7586               (narrow-to-region (point) (point))
7587               (gnus-insert-mime-security-button handle))
7588             (delete-region (point)
7589                            (or (text-property-not-all
7590                                 (point) (point-max)
7591                                 'gnus-line-format
7592                                 gnus-mime-security-button-line-format)
7593                                (point-max))))
7594         ;; Not inlined.
7595         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
7596             (with-current-buffer gnus-mime-security-details-buffer
7597               (erase-buffer)
7598               t)
7599           (setq gnus-mime-security-details-buffer
7600                 (gnus-get-buffer-create "*MIME Security Details*")))
7601         (with-current-buffer gnus-mime-security-details-buffer
7602           (insert details)
7603           (goto-char (point-min)))
7604         (pop-to-buffer gnus-mime-security-details-buffer)))))
7605
7606 (defun gnus-mime-security-press-button (handle)
7607   (save-excursion
7608     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7609         (gnus-mime-security-show-details handle)
7610       (gnus-mime-security-verify-or-decrypt handle))))
7611
7612 (defun gnus-insert-mime-security-button (handle &optional displayed)
7613   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
7614          (gnus-tmp-type
7615           (concat
7616            (or (nth 2 (assoc protocol mm-verify-function-alist))
7617                (nth 2 (assoc protocol mm-decrypt-function-alist))
7618                "Unknown")
7619            (if (equal (car handle) "multipart/signed")
7620                " Signed" " Encrypted")
7621            " Part"))
7622          (gnus-tmp-info
7623           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7624               "Undecided"))
7625          (gnus-tmp-details
7626           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
7627          gnus-tmp-pressed-details
7628          b e)
7629     (setq gnus-tmp-details
7630           (if gnus-tmp-details
7631               (concat "\n" gnus-tmp-details)
7632             ""))
7633     (setq gnus-tmp-pressed-details
7634           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
7635     (unless (bolp)
7636       (insert "\n"))
7637     (setq b (point))
7638     (gnus-eval-format
7639      gnus-mime-security-button-line-format
7640      gnus-mime-security-button-line-format-alist
7641      `(keymap ,gnus-mime-security-button-map
7642          gnus-callback gnus-mime-security-press-button
7643          gnus-line-format ,gnus-mime-security-button-line-format
7644          gnus-mime-details ,gnus-mime-security-button-pressed
7645          article-type annotation
7646          gnus-data ,handle))
7647     (setq e (if (bolp)
7648                 ;; Exclude a newline.
7649                 (1- (point))
7650               (point)))
7651     (widget-convert-button
7652      'link b e
7653      :mime-handle handle
7654      :action 'gnus-widget-press-button
7655      :button-keymap gnus-mime-security-button-map
7656      :help-echo
7657      (lambda (widget/window &optional overlay pos)
7658        ;; Needed to properly clear the message due to a bug in
7659        ;; wid-edit (XEmacs only).
7660        (when (boundp 'help-echo-owns-message)
7661          (setq help-echo-owns-message t))
7662        (format
7663         "%S: show detail"
7664         (aref gnus-mouse-2 0))))))
7665
7666 (defun gnus-mime-display-security (handle)
7667   (save-restriction
7668     (narrow-to-region (point) (point))
7669     (unless (gnus-unbuttonized-mime-type-p (car handle))
7670       (gnus-insert-mime-security-button handle))
7671     (gnus-mime-display-mixed (cdr handle))
7672     (unless (bolp)
7673       (insert "\n"))
7674     (unless (gnus-unbuttonized-mime-type-p (car handle))
7675       (let ((gnus-mime-security-button-line-format
7676              gnus-mime-security-button-end-line-format))
7677         (gnus-insert-mime-security-button handle)))
7678     (mm-set-handle-multipart-parameter
7679      handle 'gnus-region
7680      (cons (set-marker (make-marker) (point-min))
7681            (set-marker (make-marker) (point-max))))))
7682
7683
7684 ;;; @ for mime-view
7685 ;;;
7686
7687 (defun gnus-article-header-presentation-method (entity situation)
7688   (mime-insert-header entity)
7689   (article-decode-group-name))
7690
7691 (set-alist 'mime-header-presentation-method-alist
7692            'gnus-original-article-mode
7693            #'gnus-article-header-presentation-method)
7694
7695 (defun gnus-mime-preview-quitting-method ()
7696   (mime-preview-kill-buffer)
7697   (delete-other-windows)
7698   (gnus-article-show-summary)
7699   (gnus-summary-select-article gnus-show-all-headers t))
7700
7701 (set-alist 'mime-preview-quitting-method-alist
7702            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
7703
7704 (set-alist 'mime-preview-following-method-alist
7705            'gnus-original-article-mode #'gnus-following-method)
7706
7707 (set-alist 'mime-preview-over-to-previous-method-alist
7708            'gnus-original-article-mode
7709            (lambda ()
7710              (if (> (point-min) 1)
7711                  (gnus-article-prev-page)
7712                (gnus-article-read-summary-keys
7713                 nil (gnus-character-to-event ?P)))))
7714
7715 (set-alist 'mime-preview-over-to-next-method-alist
7716            'gnus-original-article-mode'
7717            (lambda ()
7718              (if (< (point-max) (buffer-size))
7719                  (gnus-article-next-page)
7720                (gnus-article-read-summary-keys
7721                 nil (gnus-character-to-event ?N)))))
7722
7723
7724 ;;; @ end
7725 ;;;
7726
7727 (gnus-ems-redefine)
7728
7729 (provide 'gnus-art)
7730
7731 (run-hooks 'gnus-art-load-hook)
7732
7733 ;;; gnus-art.el ends here