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