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