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