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