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