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