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