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