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