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