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