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