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