969e7fe9390802f2c78b9c667610a702181cc601
[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   "\C-hb" gnus-article-describe-bindings
4219
4220   "\C-d" gnus-article-read-summary-keys
4221   "\M-*" gnus-article-read-summary-keys
4222   "\M-#" gnus-article-read-summary-keys
4223   "\M-^" gnus-article-read-summary-keys
4224   "\M-g" gnus-article-read-summary-keys)
4225
4226 (substitute-key-definition
4227  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4228
4229 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4230   "W" gnus-article-wide-reply-with-original)
4231 (if (featurep 'xemacs)
4232     (set-keymap-default-binding gnus-article-send-map
4233                                 'gnus-article-read-summary-send-keys)
4234   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4235
4236 (defun gnus-article-make-menu-bar ()
4237   (unless (boundp 'gnus-article-commands-menu)
4238     (gnus-summary-make-menu-bar))
4239   (gnus-turn-off-edit-menu 'article)
4240   (unless (boundp 'gnus-article-article-menu)
4241     (easy-menu-define
4242      gnus-article-article-menu gnus-article-mode-map ""
4243      '("Article"
4244        ["Scroll forwards" gnus-article-goto-next-page t]
4245        ["Scroll backwards" gnus-article-goto-prev-page t]
4246        ["Show summary" gnus-article-show-summary t]
4247        ["Fetch Message-ID at point" gnus-article-refer-article t]
4248        ["Mail to address at point" gnus-article-mail t]
4249        ["Send a bug report" gnus-bug t]))
4250
4251     (easy-menu-define
4252      gnus-article-treatment-menu gnus-article-mode-map ""
4253      ;; Fixme: this should use :active (and maybe :visible).
4254      '("Treatment"
4255        ["Hide headers" gnus-article-hide-headers t]
4256        ["Hide signature" gnus-article-hide-signature t]
4257        ["Hide citation" gnus-article-hide-citation t]
4258        ["Treat overstrike" gnus-article-treat-overstrike t]
4259        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4260        ["Remove carriage return" gnus-article-remove-cr t]
4261        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4262        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4263        ["Remove base64" gnus-article-de-base64-unreadable t]
4264        ["Treat html" gnus-article-wash-html t]
4265        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4266        ["Decode HZ" gnus-article-decode-HZ t]))
4267
4268     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4269
4270     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4271
4272     (gnus-run-hooks 'gnus-article-menu-hook)))
4273
4274 (defun gnus-article-mode ()
4275   "Major mode for displaying an article.
4276
4277 All normal editing commands are switched off.
4278
4279 The following commands are available in addition to all summary mode
4280 commands:
4281 \\<gnus-article-mode-map>
4282 \\[gnus-article-next-page]\t Scroll the article one page forwards
4283 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4284 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4285 \\[gnus-article-show-summary]\t Display the summary buffer
4286 \\[gnus-article-mail]\t Send a reply to the address near point
4287 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4288 \\[gnus-info-find-node]\t Go to the Gnus info node"
4289   (interactive)
4290   (kill-all-local-variables)
4291   (gnus-simplify-mode-line)
4292   (setq mode-name "Article")
4293   (setq major-mode 'gnus-article-mode)
4294   (make-local-variable 'minor-mode-alist)
4295   (use-local-map gnus-article-mode-map)
4296   (when (gnus-visual-p 'article-menu 'menu)
4297     (gnus-article-make-menu-bar)
4298     (when gnus-summary-tool-bar-map
4299       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4300   (gnus-update-format-specifications nil 'article-mode)
4301   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4302   (set (make-local-variable 'gnus-page-broken) nil)
4303   (make-local-variable 'gnus-button-marker-list)
4304   (make-local-variable 'gnus-article-current-summary)
4305   (make-local-variable 'gnus-article-mime-handles)
4306   (make-local-variable 'gnus-article-decoded-p)
4307   (make-local-variable 'gnus-article-mime-handle-alist)
4308   (make-local-variable 'gnus-article-wash-types)
4309   (make-local-variable 'gnus-article-image-alist)
4310   (make-local-variable 'gnus-article-charset)
4311   (make-local-variable 'gnus-article-ignored-charsets)
4312   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4313   ;; face.
4314   (set (make-local-variable 'nobreak-char-display) nil)
4315   (setq cursor-in-non-selected-windows nil)
4316   (setq truncate-lines gnus-article-truncate-lines)
4317   (gnus-set-default-directory)
4318   (buffer-disable-undo)
4319   (setq buffer-read-only t
4320         show-trailing-whitespace nil)
4321   (set-syntax-table gnus-article-mode-syntax-table)
4322   (mm-enable-multibyte)
4323   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4324
4325 (defvar gnus-button-marker-list nil
4326   "Regexp matching any of the regexps from `gnus-button-alist'.
4327 Internal variable.")
4328
4329 (defun gnus-article-setup-buffer ()
4330   "Initialize the article buffer."
4331   (let* ((name (if gnus-single-article-buffer "*Article*"
4332                  (concat "*Article " gnus-newsgroup-name "*")))
4333          (original
4334           (progn (string-match "\\*Article" name)
4335                  (concat " *Original Article"
4336                          (substring name (match-end 0))))))
4337     (setq gnus-article-buffer name)
4338     (setq gnus-original-article-buffer original)
4339     (setq gnus-article-mime-handle-alist nil)
4340     (with-current-buffer gnus-summary-buffer
4341       ;; This might be a variable local to the summary buffer.
4342       (unless gnus-single-article-buffer
4343         (setq gnus-article-buffer name)
4344         (setq gnus-original-article-buffer original)
4345         (gnus-set-global-variables)))
4346     (gnus-article-setup-highlight-words)
4347     ;; Init original article buffer.
4348     (save-excursion
4349       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
4350       (mm-enable-multibyte)
4351       (setq major-mode 'gnus-original-article-mode)
4352       (make-local-variable 'gnus-original-article))
4353     (if (and (get-buffer name)
4354              (with-current-buffer name
4355                (if gnus-article-edit-mode
4356                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4357                        (progn
4358                          (set-buffer-modified-p nil)
4359                          (gnus-kill-buffer name)
4360                          (message "")
4361                          nil)
4362                      (error "Action aborted"))
4363                  t)))
4364         (save-excursion
4365           (set-buffer name)
4366           (set (make-local-variable 'gnus-article-edit-mode) nil)
4367           (when gnus-article-mime-handles
4368             (mm-destroy-parts gnus-article-mime-handles)
4369             (setq gnus-article-mime-handles nil))
4370           ;; Set it to nil in article-buffer!
4371           (setq gnus-article-mime-handle-alist nil)
4372           (buffer-disable-undo)
4373           (setq buffer-read-only t)
4374           ;; This list just keeps growing if we don't reset it.
4375           (setq gnus-button-marker-list nil)
4376           (unless (eq major-mode 'gnus-article-mode)
4377             (gnus-article-mode))
4378           (current-buffer))
4379       (save-excursion
4380         (set-buffer (gnus-get-buffer-create name))
4381         (gnus-article-mode)
4382         (make-local-variable 'gnus-summary-buffer)
4383         (setq gnus-summary-buffer
4384               (gnus-summary-buffer-name gnus-newsgroup-name))
4385         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4386         (current-buffer)))))
4387
4388 ;; Set article window start at LINE, where LINE is the number of lines
4389 ;; from the head of the article.
4390 (defun gnus-article-set-window-start (&optional line)
4391   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4392     (when article-window
4393       (set-window-start
4394        article-window
4395        (save-excursion
4396          (set-buffer gnus-article-buffer)
4397          (goto-char (point-min))
4398          (if (not line)
4399              (point-min)
4400            (gnus-message 6 "Moved to bookmark")
4401            (search-forward "\n\n" nil t)
4402            (forward-line line)
4403            (point)))))))
4404
4405 (defun gnus-article-prepare (article &optional all-headers header)
4406   "Prepare ARTICLE in article mode buffer.
4407 ARTICLE should either be an article number or a Message-ID.
4408 If ARTICLE is an id, HEADER should be the article headers.
4409 If ALL-HEADERS is non-nil, no headers are hidden."
4410   (save-excursion
4411     ;; Make sure we start in a summary buffer.
4412     (unless (eq major-mode 'gnus-summary-mode)
4413       (set-buffer gnus-summary-buffer))
4414     (setq gnus-summary-buffer (current-buffer))
4415     (let* ((gnus-article (if header (mail-header-number header) article))
4416            (summary-buffer (current-buffer))
4417            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4418            (group gnus-newsgroup-name)
4419            result)
4420       (save-excursion
4421         (gnus-article-setup-buffer)
4422         (set-buffer gnus-article-buffer)
4423         ;; Deactivate active regions.
4424         (when (and (boundp 'transient-mark-mode)
4425                    transient-mark-mode)
4426           (setq mark-active nil))
4427         (if (not (setq result (let ((inhibit-read-only t))
4428                                 (gnus-request-article-this-buffer
4429                                  article group))))
4430             ;; There is no such article.
4431             (save-excursion
4432               (when (and (numberp article)
4433                          (not (memq article gnus-newsgroup-sparse)))
4434                 (setq gnus-article-current
4435                       (cons gnus-newsgroup-name article))
4436                 (set-buffer gnus-summary-buffer)
4437                 (setq gnus-current-article article)
4438                 (if (and (memq article gnus-newsgroup-undownloaded)
4439                          (not (gnus-online (gnus-find-method-for-group
4440                                             gnus-newsgroup-name))))
4441                     (progn
4442                       (gnus-summary-set-agent-mark article)
4443                       (message "Message marked for downloading"))
4444                   (gnus-summary-mark-article article gnus-canceled-mark)
4445                   (unless (memq article gnus-newsgroup-sparse)
4446                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4447           (if (or (eq result 'pseudo)
4448                   (eq result 'nneething))
4449               (progn
4450                 (save-excursion
4451                   (set-buffer summary-buffer)
4452                   (push article gnus-newsgroup-history)
4453                   (setq gnus-last-article gnus-current-article
4454                         gnus-current-article 0
4455                         gnus-current-headers nil
4456                         gnus-article-current nil)
4457                   (if (eq result 'nneething)
4458                       (gnus-configure-windows 'summary)
4459                     (gnus-configure-windows 'article))
4460                   (gnus-set-global-variables))
4461                 (let ((gnus-article-mime-handle-alist-1
4462                        gnus-article-mime-handle-alist))
4463                   (gnus-set-mode-line 'article)))
4464             ;; The result from the `request' was an actual article -
4465             ;; or at least some text that is now displayed in the
4466             ;; article buffer.
4467             (when (and (numberp article)
4468                        (not (eq article gnus-current-article)))
4469               ;; Seems like a new article has been selected.
4470               ;; `gnus-current-article' must be an article number.
4471               (save-excursion
4472                 (set-buffer summary-buffer)
4473                 (push article gnus-newsgroup-history)
4474                 (setq gnus-last-article gnus-current-article
4475                       gnus-current-article article
4476                       gnus-current-headers
4477                       (gnus-summary-article-header gnus-current-article)
4478                       gnus-article-current
4479                       (cons gnus-newsgroup-name gnus-current-article))
4480                 (unless (vectorp gnus-current-headers)
4481                   (setq gnus-current-headers nil))
4482                 (gnus-summary-goto-subject gnus-current-article)
4483                 (when (gnus-summary-show-thread)
4484                   ;; If the summary buffer really was folded, the
4485                   ;; previous goto may not actually have gone to
4486                   ;; the right article, but the thread root instead.
4487                   ;; So we go again.
4488                   (gnus-summary-goto-subject gnus-current-article))
4489                 (gnus-run-hooks 'gnus-mark-article-hook)
4490                 (gnus-set-mode-line 'summary)
4491                 (when (gnus-visual-p 'article-highlight 'highlight)
4492                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4493                 ;; Set the global newsgroup variables here.
4494                 (gnus-set-global-variables)
4495                 (setq gnus-have-all-headers
4496                       (or all-headers gnus-show-all-headers))))
4497             (save-excursion
4498               (gnus-configure-windows 'article))
4499             (when (or (numberp article)
4500                       (stringp article))
4501               (gnus-article-prepare-display)
4502               ;; Do page break.
4503               (goto-char (point-min))
4504               (when gnus-break-pages
4505                 (gnus-narrow-to-page)))
4506             (let ((gnus-article-mime-handle-alist-1
4507                    gnus-article-mime-handle-alist))
4508               (gnus-set-mode-line 'article))
4509             (article-goto-body)
4510             (unless (bobp)
4511               (forward-line -1))
4512             (set-window-point (get-buffer-window (current-buffer)) (point))
4513             (gnus-configure-windows 'article)
4514             t))))))
4515
4516 ;;;###autoload
4517 (defun gnus-article-prepare-display ()
4518   "Make the current buffer look like a nice article."
4519   ;; Hooks for getting information from the article.
4520   ;; This hook must be called before being narrowed.
4521   (let ((gnus-article-buffer (current-buffer))
4522         buffer-read-only
4523         (inhibit-read-only t))
4524     (unless (eq major-mode 'gnus-article-mode)
4525       (gnus-article-mode))
4526     (setq buffer-read-only nil
4527           gnus-article-wash-types nil
4528           gnus-article-image-alist nil)
4529     (gnus-run-hooks 'gnus-tmp-internal-hook)
4530     (when gnus-display-mime-function
4531       (funcall gnus-display-mime-function))
4532     (gnus-run-hooks 'gnus-article-prepare-hook)))
4533
4534 ;;;
4535 ;;; Gnus Sticky Article Mode
4536 ;;;
4537
4538 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4539   "Mode for sticky articles."
4540   ;; Release bindings that won't work.
4541   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4542                              gnus-sticky-article-mode-map)
4543   (substitute-key-definition 'gnus-article-refer-article 'undefined
4544                              gnus-sticky-article-mode-map)
4545   (dolist (k '("e" "h" "s" "F" "R"))
4546     (define-key gnus-sticky-article-mode-map k nil))
4547   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4548   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4549   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4550   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4551
4552 (defun gnus-sticky-article (arg)
4553   "Make the current article sticky.
4554 If a prefix ARG is given, ask for a name for this sticky article buffer."
4555   (interactive "P")
4556   (gnus-summary-show-thread)
4557   (gnus-summary-select-article nil nil 'pseudo)
4558   (let (new-art-buf-name)
4559     (gnus-eval-in-buffer-window gnus-article-buffer
4560       (setq new-art-buf-name
4561             (concat
4562              "*Sticky Article: "
4563              (if arg
4564                  (read-from-minibuffer "Sticky article buffer name: ")
4565                (gnus-with-article-headers
4566                  (gnus-article-goto-header "subject")
4567                  (setq new-art-buf-name
4568                        (buffer-substring-no-properties
4569                         (line-beginning-position) (line-end-position)))
4570                  (goto-char (point-min))
4571                  (gnus-article-goto-header "from")
4572                  (setq new-art-buf-name
4573                        (concat
4574                         new-art-buf-name ", "
4575                         (buffer-substring-no-properties
4576                          (line-beginning-position) (line-end-position))))
4577                  (goto-char (point-min))
4578                  (gnus-article-goto-header "date")
4579                  (setq new-art-buf-name
4580                        (concat
4581                         new-art-buf-name ", "
4582                         (buffer-substring-no-properties
4583                          (line-beginning-position) (line-end-position))))))
4584              "*"))
4585       (if (and (gnus-buffer-live-p new-art-buf-name)
4586                (with-current-buffer new-art-buf-name
4587                  (eq major-mode 'gnus-sticky-article-mode)))
4588           (switch-to-buffer new-art-buf-name)
4589         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4590       (gnus-sticky-article-mode))
4591     (setq gnus-article-buffer new-art-buf-name))
4592   (gnus-summary-recenter)
4593   (gnus-summary-position-point))
4594
4595 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4596   "Kill the given sticky article BUFFER.
4597 If none is given, assume the current buffer and kill it if it has
4598 `gnus-sticky-article-mode'."
4599   (interactive)
4600   (unless buffer
4601     (setq buffer (current-buffer)))
4602   (with-current-buffer buffer
4603     (when (eq major-mode 'gnus-sticky-article-mode)
4604       (gnus-kill-buffer buffer))))
4605
4606 (defun gnus-kill-sticky-article-buffers (arg)
4607   "Kill all sticky article buffers.
4608 If a prefix ARG is given, ask for confirmation."
4609   (interactive "P")
4610   (dolist (buf (gnus-buffers))
4611     (with-current-buffer buf
4612       (when (eq major-mode 'gnus-sticky-article-mode)
4613         (if (not arg)
4614             (gnus-kill-buffer buf)
4615           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4616             (gnus-kill-buffer buf)))))))
4617
4618 ;;;
4619 ;;; Gnus MIME viewing functions
4620 ;;;
4621
4622 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4623   "Format of the MIME buttons.
4624
4625 Valid specifiers include:
4626 %t  The MIME type
4627 %T  MIME type, along with additional info
4628 %n  The `name' parameter
4629 %d  The description, if any
4630 %l  The length of the encoded part
4631 %p  The part identifier number
4632 %e  Dots if the part isn't displayed
4633
4634 General format specifiers can also be used.  See Info node
4635 `(gnus)Formatting Variables'.")
4636
4637 (defvar gnus-mime-button-line-format-alist
4638   '((?t gnus-tmp-type ?s)
4639     (?T gnus-tmp-type-long ?s)
4640     (?n gnus-tmp-name ?s)
4641     (?d gnus-tmp-description ?s)
4642     (?p gnus-tmp-id ?s)
4643     (?l gnus-tmp-length ?d)
4644     (?e gnus-tmp-dots ?s)))
4645
4646 (defvar gnus-mime-button-commands
4647   '((gnus-article-press-button "\r" "Toggle Display")
4648     (gnus-mime-view-part "v" "View Interactively...")
4649     (gnus-mime-view-part-as-type "t" "View As Type...")
4650     (gnus-mime-view-part-as-charset "C" "View As charset...")
4651     (gnus-mime-save-part "o" "Save...")
4652     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4653     (gnus-mime-replace-part "r" "Replace part")
4654     (gnus-mime-delete-part "d" "Delete part")
4655     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4656     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4657     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4658     (gnus-mime-view-part-externally "e" "View Externally")
4659     (gnus-mime-print-part "p" "Print")
4660     (gnus-mime-pipe-part "|" "Pipe To Command...")
4661     (gnus-mime-action-on-part "." "Take action on the part...")))
4662
4663 (defun gnus-article-mime-part-status ()
4664   (if gnus-article-mime-handle-alist-1
4665       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4666           " (1 part)"
4667         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4668     ""))
4669
4670 (defvar gnus-mime-button-map
4671   (let ((map (make-sparse-keymap)))
4672     (define-key map gnus-mouse-2 'gnus-article-push-button)
4673     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4674     (dolist (c gnus-mime-button-commands)
4675       (define-key map (cadr c) (car c)))
4676     map))
4677
4678 (easy-menu-define
4679   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4680   `("MIME Part"
4681     ,@(mapcar (lambda (c)
4682                 (vector (caddr c) (car c) :active t))
4683               gnus-mime-button-commands)))
4684
4685 (defun gnus-mime-button-menu (event prefix)
4686  "Construct a context-sensitive menu of MIME commands."
4687  (interactive "e\nP")
4688  (save-window-excursion
4689    (let ((pos (event-start event)))
4690      (select-window (posn-window pos))
4691      (goto-char (posn-point pos))
4692      (gnus-article-check-buffer)
4693      (popup-menu gnus-mime-button-menu nil prefix))))
4694
4695 (defun gnus-mime-view-all-parts (&optional handles)
4696   "View all the MIME parts."
4697   (interactive)
4698   (with-current-buffer gnus-article-buffer
4699     (let ((handles (or handles gnus-article-mime-handles))
4700           (mail-parse-charset gnus-newsgroup-charset)
4701           (mail-parse-ignored-charsets
4702            (with-current-buffer gnus-summary-buffer
4703              gnus-newsgroup-ignored-charsets)))
4704       (when handles
4705         (mm-remove-parts handles)
4706         (goto-char (point-min))
4707         (or (search-forward "\n\n") (goto-char (point-max)))
4708         (let ((inhibit-read-only t))
4709           (delete-region (point) (point-max))
4710           (mm-display-parts handles))))))
4711
4712 (defun gnus-article-jump-to-part (n)
4713   "Jump to MIME part N."
4714   (interactive "P")
4715   (pop-to-buffer gnus-article-buffer)
4716   ;; FIXME: why is it necessary?
4717   (sit-for 0)
4718   (let ((parts (length gnus-article-mime-handle-alist)))
4719     (or n (setq n
4720                 (string-to-number
4721                  (read-string ;; Emacs 21 doesn't have `read-number'.
4722                   (format "Jump to part (2..%s): " parts)))))
4723     (unless (and (integerp n) (<= n parts) (>= n 1))
4724       (setq n
4725             (progn
4726               (gnus-message 7 "Invalid part `%s', using %s instead."
4727                             n parts)
4728               parts)))
4729     (gnus-message 9 "Jumping to part %s." n)
4730     (cond ((>= gnus-auto-select-part 1)
4731            (while (and (<= n parts)
4732                        (not (gnus-article-goto-part n)))
4733              (setq n (1+ n))))
4734           ((< gnus-auto-select-part 0)
4735            (while (and (>= n 1)
4736                        (not (gnus-article-goto-part n)))
4737              (setq n (1- n))))
4738           (t
4739            (gnus-article-goto-part n)))))
4740
4741 (eval-when-compile
4742   (defsubst gnus-article-edit-part (handles &optional current-id)
4743     "Edit an article in order to delete a mime part.
4744 This function is exclusively used by `gnus-mime-save-part-and-strip'
4745 and `gnus-mime-delete-part', and not provided at run-time normally."
4746     (gnus-article-edit-article
4747      `(lambda ()
4748         (buffer-disable-undo)
4749         (erase-buffer)
4750         (let ((mail-parse-charset (or gnus-article-charset
4751                                       ',gnus-newsgroup-charset))
4752               (mail-parse-ignored-charsets
4753                (or gnus-article-ignored-charsets
4754                    ',gnus-newsgroup-ignored-charsets))
4755               (mbl mml-buffer-list))
4756           (setq mml-buffer-list nil)
4757           (insert-buffer-substring gnus-original-article-buffer)
4758           (mime-to-mml ',handles)
4759           (setq gnus-article-mime-handles nil)
4760           (let ((mbl1 mml-buffer-list))
4761             (setq mml-buffer-list mbl)
4762             (set (make-local-variable 'mml-buffer-list) mbl1))
4763           (gnus-make-local-hook 'kill-buffer-hook)
4764           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4765      `(lambda (no-highlight)
4766         (let ((mail-parse-charset (or gnus-article-charset
4767                                       ',gnus-newsgroup-charset))
4768               (message-options message-options)
4769               (message-options-set-recipient)
4770               (mail-parse-ignored-charsets
4771                (or gnus-article-ignored-charsets
4772                    ',gnus-newsgroup-ignored-charsets)))
4773           (mml-to-mime)
4774           (mml-destroy-buffers)
4775           (remove-hook 'kill-buffer-hook
4776                        'mml-destroy-buffers t)
4777           (kill-local-variable 'mml-buffer-list))
4778         (gnus-summary-edit-article-done
4779          ,(or (mail-header-references gnus-current-headers) "")
4780          ,(gnus-group-read-only-p)
4781          ,gnus-summary-buffer no-highlight))
4782      t)
4783     (gnus-article-edit-done)
4784     (gnus-summary-expand-window)
4785     (gnus-summary-show-article)
4786     (when (and current-id (integerp gnus-auto-select-part))
4787       (gnus-article-jump-to-part
4788        (if (text-property-any (point-min) (point-max)
4789                               'gnus-part (+ current-id gnus-auto-select-part))
4790            (+ current-id gnus-auto-select-part)
4791          (with-current-buffer gnus-article-buffer
4792            (length gnus-article-mime-handle-alist)))))))
4793
4794 (defun gnus-mime-replace-part (file)
4795   "Replace MIME part under point with an external body."
4796   ;; Useful if file has already been saved to disk
4797   (interactive
4798    (list
4799     (mm-with-multibyte
4800       (read-file-name "Replace MIME part with file: "
4801                       (or mm-default-directory default-directory)
4802                       nil nil))))
4803   (gnus-mime-save-part-and-strip file))
4804
4805 (defun gnus-mime-save-part-and-strip (&optional file)
4806   "Save the MIME part under point then replace it with an external body.
4807 If FILE is given, use it for the external part."
4808   (interactive)
4809   (gnus-article-check-buffer)
4810   (when (gnus-group-read-only-p)
4811     (error "The current group does not support deleting of parts"))
4812   (when (mm-complicated-handles gnus-article-mime-handles)
4813     (error "\
4814 The current article has a complicated MIME structure, giving up..."))
4815   (let* ((data (get-text-property (point) 'gnus-data))
4816          (id (get-text-property (point) 'gnus-part))
4817          param
4818          (handles gnus-article-mime-handles))
4819     (unless file
4820       (setq file
4821             (and data (mm-save-part data "Delete MIME part and save to: "))))
4822     (when file
4823       (with-current-buffer (mm-handle-buffer data)
4824         (erase-buffer)
4825         (insert "Content-Type: " (mm-handle-media-type data))
4826         (mml-insert-parameter-string (cdr (mm-handle-type data))
4827                                      '(charset))
4828         ;; Add a filename for the sake of saving the part again.
4829         (mml-insert-parameter
4830          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
4831         (insert "\n")
4832         (insert "Content-ID: " (message-make-message-id) "\n")
4833         (insert "Content-Transfer-Encoding: binary\n")
4834         (insert "\n"))
4835       (setcdr data
4836               (cdr (mm-make-handle nil
4837                                    `("message/external-body"
4838                                      (access-type . "LOCAL-FILE")
4839                                      (name . ,file)))))
4840       ;; (set-buffer gnus-summary-buffer)
4841       (gnus-article-edit-part handles id))))
4842
4843 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
4844 ;; parts...>') but with stripping would be nice.
4845
4846 (defun gnus-mime-delete-part ()
4847   "Delete the MIME part under point.
4848 Replace it with some information about the removed part."
4849   (interactive)
4850   (gnus-article-check-buffer)
4851   (when (gnus-group-read-only-p)
4852     (error "The current group does not support deleting of parts"))
4853   (when (mm-complicated-handles gnus-article-mime-handles)
4854     (error "\
4855 The current article has a complicated MIME structure, giving up..."))
4856   (when (or gnus-expert-user
4857             (gnus-yes-or-no-p "\
4858 Deleting parts may malfunction or destroy the article; continue? "))
4859     (let* ((data (get-text-property (point) 'gnus-data))
4860            (id (get-text-property (point) 'gnus-part))
4861            (handles gnus-article-mime-handles)
4862            (none "(none)")
4863            (description
4864             (let ((desc (mm-handle-description data)))
4865               (when desc
4866                 (mail-decode-encoded-word-string desc))))
4867            (filename
4868             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
4869                 none))
4870            (type (mm-handle-media-type data)))
4871       (unless data
4872         (error "No MIME part under point"))
4873       (with-current-buffer (mm-handle-buffer data)
4874         (let ((bsize (format "%s" (buffer-size))))
4875           (erase-buffer)
4876           (insert
4877            (concat
4878             ",----\n"
4879             "| The following attachment has been deleted:\n"
4880             "|\n"
4881             "| Type:           " type "\n"
4882             "| Filename:       " filename "\n"
4883             "| Size (encoded): " bsize " Byte\n"
4884             (when description
4885               (concat    "| Description:    " description "\n"))
4886             "`----\n"))
4887           (setcdr data
4888                   (cdr (mm-make-handle
4889                         nil `("text/plain") nil nil
4890                         (list "attachment")
4891                         (format "Deleted attachment (%s bytes)" bsize))))))
4892       ;; (set-buffer gnus-summary-buffer)
4893       (gnus-article-edit-part handles id))))
4894
4895 (defun gnus-mime-save-part ()
4896   "Save the MIME part under point."
4897   (interactive)
4898   (gnus-article-check-buffer)
4899   (let ((data (get-text-property (point) 'gnus-data)))
4900     (when data
4901       (mm-save-part data))))
4902
4903 (defun gnus-mime-pipe-part ()
4904   "Pipe the MIME part under point to a process."
4905   (interactive)
4906   (gnus-article-check-buffer)
4907   (let ((data (get-text-property (point) 'gnus-data)))
4908     (when data
4909       (mm-pipe-part data))))
4910
4911 (defun gnus-mime-view-part ()
4912   "Interactively choose a viewing method for the MIME part under point."
4913   (interactive)
4914   (gnus-article-check-buffer)
4915   (let ((data (get-text-property (point) 'gnus-data)))
4916     (when data
4917       (setq gnus-article-mime-handles
4918             (mm-merge-handles
4919              gnus-article-mime-handles (setq data (copy-sequence data))))
4920       (mm-interactively-view-part data))))
4921
4922 (defun gnus-mime-view-part-as-type-internal ()
4923   (gnus-article-check-buffer)
4924   (let* ((handle (get-text-property (point) 'gnus-data))
4925          (name (or
4926                 ;; Content-Type: foo/bar; name=...
4927                 (mail-content-type-get (mm-handle-type handle) 'name)
4928                 ;; Content-Disposition: attachment; filename=...
4929                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
4930          (def-type (and name (mm-default-file-encoding name))))
4931     (or (and def-type (cons def-type 0))
4932         (and handle
4933              (equal (mm-handle-media-supertype handle) "text")
4934              '("text/plain" . 0))
4935         '("application/octet-stream" . 0))))
4936
4937 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
4938   "Choose a MIME media type, and view the part as such.
4939 If non-nil, PRED is a predicate to use during completion to limit the
4940 available media-types."
4941   (interactive)
4942   (unless mime-type
4943     (setq mime-type
4944           (let ((default (gnus-mime-view-part-as-type-internal)))
4945             (completing-read
4946              (format "View as MIME type (default %s): "
4947                      (car default))
4948              (mapcar #'list (mailcap-mime-types))
4949              pred nil nil nil
4950              (car default)))))
4951   (gnus-article-check-buffer)
4952   (let ((handle (get-text-property (point) 'gnus-data)))
4953     (when handle
4954       (when (equal (mm-handle-media-type handle) "message/external-body")
4955         (unless (mm-handle-cache handle)
4956           (mm-extern-cache-contents handle))
4957         (setq handle (mm-handle-cache handle)))
4958       (setq handle
4959             (mm-make-handle (mm-handle-buffer handle)
4960                             (cons mime-type (cdr (mm-handle-type handle)))
4961                             (mm-handle-encoding handle)
4962                             (mm-handle-undisplayer handle)
4963                             (mm-handle-disposition handle)
4964                             (mm-handle-description handle)
4965                             nil
4966                             (mm-handle-id handle)))
4967       (setq gnus-article-mime-handles
4968             (mm-merge-handles gnus-article-mime-handles handle))
4969       (when (mm-handle-displayed-p handle)
4970         (mm-remove-part handle))
4971       (gnus-mm-display-part handle))))
4972
4973 (defun gnus-mime-copy-part (&optional handle arg)
4974   "Put the MIME part under point into a new buffer.
4975 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
4976 are decompressed."
4977   (interactive (list nil current-prefix-arg))
4978   (gnus-article-check-buffer)
4979   (unless handle
4980     (setq handle (get-text-property (point) 'gnus-data)))
4981   (when handle
4982     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
4983                                                'name)
4984                         (mail-content-type-get (mm-handle-disposition handle)
4985                                                'filename)))
4986           contents dont-decode charset coding-system)
4987       (mm-with-unibyte-buffer
4988         (mm-insert-part handle)
4989         (setq contents (or (condition-case nil
4990                                (mm-decompress-buffer filename nil 'sig)
4991                              (error
4992                               (setq dont-decode t)
4993                               nil))
4994                            (buffer-string))))
4995       (setq filename (cond (filename (file-name-nondirectory filename))
4996                            (dont-decode "*raw data*")
4997                            (t "*decoded*")))
4998       (cond
4999        (dont-decode)
5000        ((not arg)
5001         (unless (setq charset (mail-content-type-get
5002                                (mm-handle-type handle) 'charset))
5003           (unless (setq coding-system (mm-with-unibyte-buffer
5004                                         (insert contents)
5005                                         (mm-find-buffer-file-coding-system)))
5006             (setq charset gnus-newsgroup-charset))))
5007        ((numberp arg)
5008         (setq charset (or (cdr (assq arg
5009                                      gnus-summary-show-article-charset-alist))
5010                           (mm-read-coding-system "Charset: ")))))
5011       (switch-to-buffer (generate-new-buffer filename))
5012       (if (or coding-system
5013               (and charset
5014                    (setq coding-system (mm-charset-to-coding-system charset))
5015                    (not (eq charset 'ascii))))
5016           (progn
5017             (mm-enable-multibyte)
5018             (insert (mm-decode-coding-string contents coding-system))
5019             (setq buffer-file-coding-system
5020                   (if (boundp 'last-coding-system-used)
5021                       (symbol-value 'last-coding-system-used)
5022                     coding-system)))
5023         (mm-disable-multibyte)
5024         (insert contents)
5025         (setq buffer-file-coding-system mm-binary-coding-system))
5026       ;; We do it this way to make `normal-mode' set the appropriate mode.
5027       (unwind-protect
5028           (progn
5029             (setq buffer-file-name (expand-file-name filename))
5030             (normal-mode))
5031         (setq buffer-file-name nil))
5032       (goto-char (point-min)))))
5033
5034 (defun gnus-mime-print-part (&optional handle filename)
5035   "Print the MIME part under point."
5036   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5037   (gnus-article-check-buffer)
5038   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5039          (contents (and handle (mm-get-part handle)))
5040          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5041          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5042     (when contents
5043         (if printer
5044             (unwind-protect
5045                 (progn
5046                   (mm-save-part-to-file handle file)
5047                   (call-process shell-file-name nil
5048                                 (generate-new-buffer " *mm*")
5049                                 nil
5050                                 shell-command-switch
5051                                 (mm-mailcap-command
5052                                  printer file (mm-handle-type handle))))
5053               (delete-file file))
5054           (with-temp-buffer
5055             (insert contents)
5056             (gnus-print-buffer))
5057           (ps-despool filename)))))
5058
5059 (defun gnus-mime-inline-part (&optional handle arg)
5060   "Insert the MIME part under point into the current buffer.
5061 Compressed files like .gz and .bz2 are decompressed."
5062   (interactive (list nil current-prefix-arg))
5063   (gnus-article-check-buffer)
5064   (unless handle
5065     (setq handle (get-text-property (point) 'gnus-data)))
5066   (when handle
5067     (let ((b (point))
5068           (inhibit-read-only t)
5069           contents charset coding-system)
5070       (if (and (not arg) (mm-handle-undisplayer handle))
5071           (mm-remove-part handle)
5072         (mm-with-unibyte-buffer
5073           (mm-insert-part handle)
5074           (setq contents
5075                 (or (mm-decompress-buffer
5076                      (or (mail-content-type-get (mm-handle-type handle)
5077                                                 'name)
5078                          (mail-content-type-get (mm-handle-disposition handle)
5079                                                 'filename))
5080                      nil t)
5081                     (buffer-string))))
5082         (cond
5083          ((not arg)
5084           (unless (setq charset (mail-content-type-get
5085                                  (mm-handle-type handle) 'charset))
5086             (unless (setq coding-system
5087                           (mm-with-unibyte-buffer
5088                             (insert contents)
5089                             (mm-find-buffer-file-coding-system)))
5090               (setq charset gnus-newsgroup-charset))))
5091          ((numberp arg)
5092           (if (mm-handle-undisplayer handle)
5093               (mm-remove-part handle))
5094           (setq charset
5095                 (or (cdr (assq arg
5096                                gnus-summary-show-article-charset-alist))
5097                     (mm-read-coding-system "Charset: "))))
5098          (t
5099           (if (mm-handle-undisplayer handle)
5100               (mm-remove-part handle))))
5101         (forward-line 2)
5102         (mm-insert-inline
5103          handle
5104          (if (or coding-system
5105                  (and charset
5106                       (setq coding-system
5107                             (mm-charset-to-coding-system charset))
5108                       (not (eq coding-system 'ascii))))
5109              (mm-decode-coding-string contents coding-system)
5110            (mm-string-to-multibyte contents)))
5111         (goto-char b)))))
5112
5113 (defun gnus-mime-strip-charset-parameters (handle)
5114   "Strip charset parameters from HANDLE."
5115   (if (stringp (car handle))
5116       (mapc #'gnus-mime-strip-charset-parameters (cdr handle))
5117     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5118                                             "message/external-body")
5119                                      (progn
5120                                        (unless (mm-handle-cache handle)
5121                                          (mm-extern-cache-contents handle))
5122                                        (mm-handle-cache handle))
5123                                    handle)))
5124            (charset (assq 'charset (cdr type))))
5125       (when charset
5126         (delq charset type)))))
5127
5128 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5129   "Insert the MIME part under point into the current buffer using the
5130 specified charset."
5131   (interactive (list nil current-prefix-arg))
5132   (gnus-article-check-buffer)
5133   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5134         (fun (get-text-property (point) 'gnus-callback))
5135         (gnus-newsgroup-ignored-charsets 'gnus-all)
5136         gnus-newsgroup-charset form preferred parts)
5137     (when handle
5138       (when (prog1
5139                 (and fun
5140                      (setq gnus-newsgroup-charset
5141                            (or (cdr (assq
5142                                      arg
5143                                      gnus-summary-show-article-charset-alist))
5144                                (mm-read-coding-system "Charset: "))))
5145               (if (mm-handle-undisplayer handle)
5146                   (mm-remove-part handle)))
5147         (gnus-mime-strip-charset-parameters handle)
5148         (when (and (consp (setq form (cdr-safe fun)))
5149                    (setq form (ignore-errors
5150                                 (assq 'gnus-mime-display-alternative form)))
5151                    (setq preferred (caddr form))
5152                    (progn
5153                      (when (eq (car preferred) 'quote)
5154                        (setq preferred (cadr preferred)))
5155                      (not (equal preferred
5156                                  (get-text-property (point) 'gnus-data))))
5157                    (setq parts (get-text-property (point) 'gnus-part))
5158                    (setq parts (cdr (assq parts
5159                                           gnus-article-mime-handle-alist)))
5160                    (equal (mm-handle-media-type parts) "multipart/alternative")
5161                    (setq parts (reverse (cdr parts))))
5162           (setcar (cddr form)
5163                   (list 'quote (or (cadr (member preferred parts))
5164                                    (car parts)))))
5165         (funcall fun handle)))))
5166
5167 (defun gnus-mime-view-part-externally (&optional handle)
5168   "View the MIME part under point with an external viewer."
5169   (interactive)
5170   (gnus-article-check-buffer)
5171   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5172          (mm-user-display-methods nil)
5173          (mm-inlined-types nil)
5174          (mail-parse-charset gnus-newsgroup-charset)
5175          (mail-parse-ignored-charsets
5176           (with-current-buffer gnus-summary-buffer
5177             gnus-newsgroup-ignored-charsets))
5178          (type (mm-handle-media-type handle))
5179          (method (mailcap-mime-info type))
5180          (mm-enable-external t))
5181     (if (not (stringp method))
5182         (gnus-mime-view-part-as-type
5183          nil (lambda (types) (stringp (mailcap-mime-info (car types)))))
5184       (when handle
5185         (if (mm-handle-undisplayer handle)
5186             (mm-remove-part handle)
5187           (mm-display-part handle))))))
5188
5189 (defun gnus-mime-view-part-internally (&optional handle)
5190   "View the MIME part under point with an internal viewer.
5191 If no internal viewer is available, use an external viewer."
5192   (interactive)
5193   (gnus-article-check-buffer)
5194   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5195          (mm-inlined-types '(".*"))
5196          (mm-inline-large-images t)
5197          (mail-parse-charset gnus-newsgroup-charset)
5198          (mail-parse-ignored-charsets
5199           (with-current-buffer gnus-summary-buffer
5200             gnus-newsgroup-ignored-charsets))
5201          (inhibit-read-only t))
5202     (if (not (mm-inlinable-p handle))
5203         (gnus-mime-view-part-as-type
5204          nil (lambda (types) (mm-inlinable-p handle (car types))))
5205       (when handle
5206         (if (mm-handle-undisplayer handle)
5207             (mm-remove-part handle)
5208           (mm-display-part handle))))))
5209
5210 (defun gnus-mime-action-on-part (&optional action)
5211   "Do something with the MIME attachment at \(point\)."
5212   (interactive
5213    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
5214   (gnus-article-check-buffer)
5215   (let ((action-pair (assoc action gnus-mime-action-alist)))
5216     (if action-pair
5217         (funcall (cdr action-pair)))))
5218
5219 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5220   "Call FUNCTION on MIME part N.
5221 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5222 If INTERACTIVE, call FUNCTION interactivly."
5223   (let (window frame)
5224     ;; Check whether the article is displayed.
5225     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5226                  (setq window (get-buffer-window gnus-article-buffer t))
5227                  (frame-visible-p (setq frame (window-frame window))))
5228       (error "No article is displayed"))
5229     (with-current-buffer gnus-article-buffer
5230       ;; Check whether the article displays the right contents.
5231       (unless (with-current-buffer gnus-summary-buffer
5232                 (eq gnus-current-article (gnus-summary-article-number)))
5233         (error "You should select the right article first"))
5234       (if n
5235           (setq n (prefix-numeric-value n))
5236         (let ((pt (point)))
5237           (setq n (or (get-text-property pt 'gnus-part)
5238                       (and (not (bobp))
5239                            (get-text-property (1- pt) 'gnus-part))
5240                       (get-text-property (prog2
5241                                              (forward-line 1)
5242                                              (point)
5243                                            (goto-char pt))
5244                                          'gnus-part)
5245                       (get-text-property
5246                        (or (and (setq pt (previous-single-property-change
5247                                           pt 'gnus-part))
5248                                 (1- pt))
5249                            (next-single-property-change (point) 'gnus-part)
5250                            (point))
5251                        'gnus-part)
5252                       1))))
5253       ;; Check whether the specified part exists.
5254       (when (> n (length gnus-article-mime-handle-alist))
5255         (error "No such part")))
5256     (unless
5257         (progn
5258           ;; To select the window is needed so that the cursor
5259           ;; might be visible on the MIME button.
5260           (select-window (prog1
5261                              window
5262                            (setq window (selected-window))
5263                            ;; Article may be displayed in the other frame.
5264                            (gnus-select-frame-set-input-focus
5265                             (prog1
5266                                 frame
5267                               (setq frame (selected-frame))))))
5268           (when (gnus-article-goto-part n)
5269             ;; We point the cursor and the arrow at the MIME button
5270             ;; when the `function' prompt the user for something.
5271             (let ((cursor-in-non-selected-windows t)
5272                   (overlay-arrow-string "=>")
5273                   (overlay-arrow-position (point-marker)))
5274               (unwind-protect
5275                   (cond
5276                    ((and no-handle interactive)
5277                     (call-interactively function))
5278                    (no-handle
5279                     (funcall function))
5280                    (interactive
5281                     (call-interactively
5282                      function
5283                      (cdr (assq n gnus-article-mime-handle-alist))))
5284                    (t
5285                     (funcall function
5286                              (cdr (assq n gnus-article-mime-handle-alist)))))
5287                 (set-marker overlay-arrow-position nil)
5288                 (unless gnus-auto-select-part
5289                   (gnus-select-frame-set-input-focus frame)
5290                   (select-window window))))
5291             t))
5292       (if gnus-inhibit-mime-unbuttonizing
5293           ;; This is the default though the program shouldn't reach here.
5294           (error "No such part")
5295         ;; The part which doesn't have the MIME button is selected.
5296         ;; So, we display all the buttons and redo it.
5297         (let ((gnus-inhibit-mime-unbuttonizing t))
5298           (gnus-summary-show-article)
5299           (gnus-article-part-wrapper n function no-handle))))))
5300
5301 (defun gnus-article-pipe-part (n)
5302   "Pipe MIME part N, which is the numerical prefix."
5303   (interactive "P")
5304   (gnus-article-part-wrapper n 'mm-pipe-part))
5305
5306 (defun gnus-article-save-part (n)
5307   "Save MIME part N, which is the numerical prefix."
5308   (interactive "P")
5309   (gnus-article-part-wrapper n 'mm-save-part))
5310
5311 (defun gnus-article-interactively-view-part (n)
5312   "View MIME part N interactively, which is the numerical prefix."
5313   (interactive "P")
5314   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5315
5316 (defun gnus-article-copy-part (n)
5317   "Copy MIME part N, which is the numerical prefix."
5318   (interactive "P")
5319   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5320
5321 (defun gnus-article-view-part-as-charset (n)
5322   "View MIME part N using a specified charset.
5323 N is the numerical prefix."
5324   (interactive "P")
5325   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5326
5327 (defun gnus-article-view-part-externally (n)
5328   "View MIME part N externally, which is the numerical prefix."
5329   (interactive "P")
5330   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5331
5332 (defun gnus-article-inline-part (n)
5333   "Inline MIME part N, which is the numerical prefix."
5334   (interactive "P")
5335   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5336
5337 (defun gnus-article-save-part-and-strip (n)
5338   "Save MIME part N and replace it with an external body.
5339 N is the numerical prefix."
5340   (interactive "P")
5341   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5342
5343 (defun gnus-article-replace-part (n)
5344   "Replace MIME part N with an external body.
5345 N is the numerical prefix."
5346   (interactive "P")
5347   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5348
5349 (defun gnus-article-delete-part (n)
5350   "Delete MIME part N and add some information about the removed part.
5351 N is the numerical prefix."
5352   (interactive "P")
5353   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5354
5355 (defun gnus-article-view-part-as-type (n)
5356   "Choose a MIME media type, and view part N as such.
5357 N is the numerical prefix."
5358   (interactive "P")
5359   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5360
5361 (defun gnus-article-mime-match-handle-first (condition)
5362   (if condition
5363       (let (n)
5364         (dolist (ihandle gnus-article-mime-handle-alist)
5365           (if (and (cond
5366                     ((functionp condition)
5367                      (funcall condition (cdr ihandle)))
5368                     ((eq condition 'undisplayed)
5369                      (not (or (mm-handle-undisplayer (cdr ihandle))
5370                               (equal (mm-handle-media-type (cdr ihandle))
5371                                      "multipart/alternative"))))
5372                     ((eq condition 'undisplayed-alternative)
5373                      (not (mm-handle-undisplayer (cdr ihandle))))
5374                     (t t))
5375                    (gnus-article-goto-part (car ihandle))
5376                    (or (not n) (< (car ihandle) n)))
5377               (setq n (car ihandle))))
5378         (or n 1))
5379     1))
5380
5381 (defun gnus-article-view-part (&optional n)
5382   "View MIME part N, which is the numerical prefix."
5383   (interactive "P")
5384   (with-current-buffer gnus-article-buffer
5385     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5386                              gnus-article-mime-match-handle-function)))
5387     (when (> n (length gnus-article-mime-handle-alist))
5388       (error "No such part"))
5389     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5390       (when (gnus-article-goto-part n)
5391         (if (equal (car handle) "multipart/alternative")
5392             (gnus-article-press-button)
5393           (when (eq (gnus-mm-display-part handle) 'internal)
5394             (gnus-set-window-start)))))))
5395
5396 (defsubst gnus-article-mime-total-parts ()
5397   (if (bufferp (car gnus-article-mime-handles))
5398       1 ;; single part
5399     (1- (length gnus-article-mime-handles))))
5400
5401 (defun gnus-mm-display-part (handle)
5402   "Display HANDLE and fix MIME button."
5403   (let ((id (get-text-property (point) 'gnus-part))
5404         (point (point))
5405         (inhibit-read-only t))
5406     (forward-line 1)
5407     (prog1
5408         (let ((window (selected-window))
5409               (mail-parse-charset gnus-newsgroup-charset)
5410               (mail-parse-ignored-charsets
5411                (if (gnus-buffer-live-p gnus-summary-buffer)
5412                    (with-current-buffer gnus-summary-buffer
5413                      gnus-newsgroup-ignored-charsets)
5414                  nil)))
5415           (save-excursion
5416             (unwind-protect
5417                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5418                       (beg (point)))
5419                   (when win
5420                     (select-window win))
5421                   (goto-char point)
5422                   (forward-line)
5423                   (if (mm-handle-displayed-p handle)
5424                       ;; This will remove the part.
5425                       (mm-display-part handle)
5426                     (save-restriction
5427                       (narrow-to-region (point)
5428                                         (if (eobp) (point) (1+ (point))))
5429                       (mm-display-part handle)
5430                       ;; We narrow to the part itself and
5431                       ;; then call the treatment functions.
5432                       (goto-char (point-min))
5433                       (forward-line 1)
5434                       (narrow-to-region (point) (point-max))
5435                       (gnus-treat-article
5436                        nil id
5437                        (gnus-article-mime-total-parts)
5438                        (mm-handle-media-type handle)))))
5439               (if (window-live-p window)
5440                   (select-window window)))))
5441       (goto-char point)
5442       (gnus-delete-line)
5443       (gnus-insert-mime-button
5444        handle id (list (mm-handle-displayed-p handle)))
5445       (goto-char point))))
5446
5447 (defun gnus-article-goto-part (n)
5448   "Go to MIME part N."
5449   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
5450
5451 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5452   (let ((gnus-tmp-name
5453          (or (mail-content-type-get (mm-handle-type handle) 'name)
5454              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5455              (mail-content-type-get (mm-handle-type handle) 'url)
5456              ""))
5457         (gnus-tmp-type (mm-handle-media-type handle))
5458         (gnus-tmp-description
5459          (mail-decode-encoded-word-string (or (mm-handle-description handle)
5460                                               "")))
5461         (gnus-tmp-dots
5462          (if (if displayed (car displayed)
5463                (mm-handle-displayed-p handle))
5464              "" "..."))
5465         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5466                            (buffer-size)))
5467         gnus-tmp-type-long b e)
5468     (when (string-match ".*/" gnus-tmp-name)
5469       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5470     (setq gnus-tmp-type-long (concat gnus-tmp-type
5471                                      (and (not (equal gnus-tmp-name ""))
5472                                           (concat "; " gnus-tmp-name))))
5473     (unless (equal gnus-tmp-description "")
5474       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5475     (unless (bolp)
5476       (insert "\n"))
5477     (setq b (point))
5478     (gnus-eval-format
5479      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5480      `(keymap ,gnus-mime-button-map
5481               gnus-callback gnus-mm-display-part
5482               gnus-part ,gnus-tmp-id
5483               article-type annotation
5484               gnus-data ,handle))
5485     (setq e (if (bolp)
5486                 ;; Exclude a newline.
5487                 (1- (point))
5488               (point)))
5489     (when gnus-article-button-face
5490       (gnus-overlay-put (gnus-make-overlay b e nil t)
5491                         'face gnus-article-button-face))
5492     (widget-convert-button
5493      'link b e
5494      :mime-handle handle
5495      :action 'gnus-widget-press-button
5496      :button-keymap gnus-mime-button-map
5497      :help-echo
5498      (lambda (widget/window &optional overlay pos)
5499        ;; Needed to properly clear the message due to a bug in
5500        ;; wid-edit (XEmacs only).
5501        (if (boundp 'help-echo-owns-message)
5502            (setq help-echo-owns-message t))
5503        (format
5504         "%S: %s the MIME part; %S: more options"
5505         (aref gnus-mouse-2 0)
5506         ;; XEmacs will get a single widget arg; Emacs 21 will get
5507         ;; window, overlay, position.
5508         (if (mm-handle-displayed-p
5509              (if overlay
5510                  (with-current-buffer (gnus-overlay-buffer overlay)
5511                    (widget-get (widget-at (gnus-overlay-start overlay))
5512                                :mime-handle))
5513                (widget-get widget/window :mime-handle)))
5514             "hide" "show")
5515         (aref gnus-down-mouse-3 0))))))
5516
5517 (defun gnus-widget-press-button (elems el)
5518   (goto-char (widget-get elems :from))
5519   (gnus-article-press-button))
5520
5521 (defvar gnus-displaying-mime nil)
5522
5523 (defun gnus-display-mime (&optional ihandles)
5524   "Display the MIME parts."
5525   (save-excursion
5526     (save-selected-window
5527       (let ((window (get-buffer-window gnus-article-buffer))
5528             (point (point)))
5529         (when window
5530           (select-window window)
5531           ;; We have to do this since selecting the window
5532           ;; may change the point.  So we set the window point.
5533           (set-window-point window point)))
5534       (let ((handles ihandles)
5535             (inhibit-read-only t)
5536             handle)
5537         (cond (handles)
5538               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5539                (when gnus-article-emulate-mime
5540                  (mm-uu-dissect-text-parts handles)))
5541               (gnus-article-emulate-mime
5542                (setq handles (mm-uu-dissect))))
5543         (when (and (not ihandles)
5544                    (not gnus-displaying-mime))
5545           ;; Top-level call; we clean up.
5546           (when gnus-article-mime-handles
5547             (mm-destroy-parts gnus-article-mime-handles)
5548             (setq gnus-article-mime-handle-alist nil));; A trick.
5549           (setq gnus-article-mime-handles handles)
5550           ;; We allow users to glean info from the handles.
5551           (when gnus-article-mime-part-function
5552             (gnus-mime-part-function handles)))
5553         (if (and handles
5554                  (or (not (stringp (car handles)))
5555                      (cdr handles)))
5556             (progn
5557               (when (and (not ihandles)
5558                          (not gnus-displaying-mime))
5559                 ;; Clean up for mime parts.
5560                 (article-goto-body)
5561                 (delete-region (point) (point-max)))
5562               (let ((gnus-displaying-mime t))
5563                 (gnus-mime-display-part handles)))
5564           (save-restriction
5565             (article-goto-body)
5566             (narrow-to-region (point) (point-max))
5567             (gnus-treat-article nil 1 1)
5568             (widen)))
5569         (unless ihandles
5570           ;; Highlight the headers.
5571           (save-excursion
5572             (save-restriction
5573               (article-goto-body)
5574               (narrow-to-region (point-min) (point))
5575               (gnus-article-save-original-date
5576                (gnus-treat-article 'head)))))))
5577     ;; Cope with broken MIME messages.
5578     (goto-char (point-max))
5579     (unless (bolp)
5580       (insert "\n"))))
5581
5582 (defcustom gnus-mime-display-multipart-as-mixed nil
5583   "Display \"multipart\" parts as  \"multipart/mixed\".
5584
5585 If t, it overrides nil values of
5586 `gnus-mime-display-multipart-alternative-as-mixed' and
5587 `gnus-mime-display-multipart-related-as-mixed'."
5588   :group 'gnus-article-mime
5589   :type 'boolean)
5590
5591 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5592   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5593   :version "22.1"
5594   :group 'gnus-article-mime
5595   :type 'boolean)
5596
5597 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5598   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5599
5600 If displaying \"text/html\" is discouraged \(see
5601 `mm-discouraged-alternatives'\) images or other material inside a
5602 \"multipart/related\" part might be overlooked when this variable is nil."
5603   :version "22.1"
5604   :group 'gnus-article-mime
5605   :type 'boolean)
5606
5607 (defun gnus-mime-display-part (handle)
5608   (cond
5609    ;; Maybe a broken MIME message.
5610    ((null handle))
5611    ;; Single part.
5612    ((not (stringp (car handle)))
5613     (gnus-mime-display-single handle))
5614    ;; User-defined multipart
5615    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5616     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5617              handle))
5618    ;; multipart/alternative
5619    ((and (equal (car handle) "multipart/alternative")
5620          (not (or gnus-mime-display-multipart-as-mixed
5621                   gnus-mime-display-multipart-alternative-as-mixed)))
5622     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5623       (push (cons id handle) gnus-article-mime-handle-alist)
5624       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5625    ;; multipart/related
5626    ((and (equal (car handle) "multipart/related")
5627          (not (or gnus-mime-display-multipart-as-mixed
5628                   gnus-mime-display-multipart-related-as-mixed)))
5629     ;;;!!!We should find the start part, but we just default
5630     ;;;!!!to the first part.
5631     ;;(gnus-mime-display-part (cadr handle))
5632     ;;;!!! Most multipart/related is an HTML message plus images.
5633     ;;;!!! Unfortunately we are unable to let W3 display those
5634     ;;;!!! included images, so we just display it as a mixed multipart.
5635     ;;(gnus-mime-display-mixed (cdr handle))
5636     ;;;!!! No, w3 can display everything just fine.
5637     (gnus-mime-display-part (cadr handle)))
5638    ((equal (car handle) "multipart/signed")
5639     (gnus-add-wash-type 'signed)
5640     (gnus-mime-display-security handle))
5641    ((equal (car handle) "multipart/encrypted")
5642     (gnus-add-wash-type 'encrypted)
5643     (gnus-mime-display-security handle))
5644    ;; Other multiparts are handled like multipart/mixed.
5645    (t
5646     (gnus-mime-display-mixed (cdr handle)))))
5647
5648 (defun gnus-mime-part-function (handles)
5649   (if (stringp (car handles))
5650       (mapcar 'gnus-mime-part-function (cdr handles))
5651     (funcall gnus-article-mime-part-function handles)))
5652
5653 (defun gnus-mime-display-mixed (handles)
5654   (mapcar 'gnus-mime-display-part handles))
5655
5656 (defun gnus-mime-display-single (handle)
5657   (let ((type (mm-handle-media-type handle))
5658         (ignored gnus-ignored-mime-types)
5659         (not-attachment t)
5660         (move nil)
5661         display text)
5662     (catch 'ignored
5663       (progn
5664         (while ignored
5665           (when (string-match (pop ignored) type)
5666             (throw 'ignored nil)))
5667         (if (and (setq not-attachment
5668                        (and (not (mm-inline-override-p handle))
5669                             (or (not (mm-handle-disposition handle))
5670                                 (equal (car (mm-handle-disposition handle))
5671                                        "inline")
5672                                 (mm-attachment-override-p handle))))
5673                  (mm-automatic-display-p handle)
5674                  (or (and
5675                       (mm-inlinable-p handle)
5676                       (mm-inlined-p handle))
5677                      (mm-automatic-external-display-p type)))
5678             (setq display t)
5679           (when (equal (mm-handle-media-supertype handle) "text")
5680             (setq text t)))
5681         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5682               beg)
5683           (push (cons id handle) gnus-article-mime-handle-alist)
5684           (when (and display
5685                      (equal (mm-handle-media-supertype handle) "message"))
5686             (insert-char
5687              ?\n
5688              (cond ((not (bolp)) 2)
5689                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5690                    (t 1))))
5691           (when (or (not display)
5692                     (not (gnus-unbuttonized-mime-type-p type)))
5693             (gnus-insert-mime-button
5694              handle id (list (or display (and not-attachment text))))
5695             (gnus-article-insert-newline)
5696             ;; Remember modify the number of forward lines.
5697             (setq move t))
5698           (setq beg (point))
5699           (cond
5700            (display
5701             (when move
5702               (forward-line -1)
5703               (setq beg (point)))
5704             (let ((mail-parse-charset gnus-newsgroup-charset)
5705                   (mail-parse-ignored-charsets
5706                    (save-excursion (condition-case ()
5707                                        (set-buffer gnus-summary-buffer)
5708                                      (error))
5709                                    gnus-newsgroup-ignored-charsets)))
5710               (mm-display-part handle t))
5711             (goto-char (point-max)))
5712            ((and text not-attachment)
5713             (when move
5714               (forward-line -1)
5715               (setq beg (point)))
5716             (gnus-article-insert-newline)
5717             (mm-insert-inline
5718              handle
5719              (let ((charset (or (mail-content-type-get (mm-handle-type handle)
5720                                                        'charset)
5721                                 (and (equal type "text/calendar") 'utf-8))))
5722                (cond ((not charset)
5723                       (mm-string-as-multibyte (mm-get-part handle)))
5724                      ((eq charset 'gnus-decoded)
5725                       (with-current-buffer (mm-handle-buffer handle)
5726                         (buffer-string)))
5727                      (t
5728                       (mm-decode-string (mm-get-part handle) charset)))))
5729             (goto-char (point-max))))
5730           ;; Do highlighting.
5731           (save-excursion
5732             (save-restriction
5733               (narrow-to-region beg (point))
5734               (if (eq handle gnus-article-mime-handles)
5735                   ;; The format=flowed case.
5736                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5737                 ;; Don't count signature parts that are never displayed.
5738                 ;; The part number should be re-calculated supposing this
5739                 ;; might be a message/rfc822 part.
5740                 (let (handles)
5741                   (dolist (part gnus-article-mime-handles)
5742                     (unless (or (stringp part)
5743                                 (equal (car (mm-handle-type part))
5744                                        "application/pgp-signature"))
5745                       (push part handles)))
5746                   (gnus-treat-article
5747                    nil (length (memq handle handles)) (length handles)
5748                    (mm-handle-media-type handle)))))))))))
5749
5750 (defun gnus-unbuttonized-mime-type-p (type)
5751   "Say whether TYPE is to be unbuttonized."
5752   (unless gnus-inhibit-mime-unbuttonizing
5753     (when (catch 'found
5754             (let ((types gnus-unbuttonized-mime-types))
5755               (while types
5756                 (when (string-match (pop types) type)
5757                   (throw 'found t)))))
5758       (not (catch 'found
5759              (let ((types gnus-buttonized-mime-types))
5760                (while types
5761                  (when (string-match (pop types) type)
5762                    (throw 'found t)))))))))
5763
5764 (defun gnus-article-insert-newline ()
5765   "Insert a newline, but mark it as undeletable."
5766   (gnus-put-text-property
5767    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5768
5769 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5770   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5771          (ihandles handles)
5772          (point (point))
5773          handle (inhibit-read-only t) from props begend not-pref)
5774     (save-window-excursion
5775       (save-restriction
5776         (when ibegend
5777           (narrow-to-region (car ibegend)
5778                             (or (cdr ibegend)
5779                                 (progn
5780                                   (goto-char (car ibegend))
5781                                   (forward-line 2)
5782                                   (point))))
5783           (delete-region (point-min) (point-max))
5784           (mm-remove-parts handles))
5785         (setq begend (list (point-marker)))
5786         ;; Do the toggle.
5787         (unless (setq not-pref (cadr (member preferred ihandles)))
5788           (setq not-pref (car ihandles)))
5789         (when (or ibegend
5790                   (not preferred)
5791                   (not (gnus-unbuttonized-mime-type-p
5792                         "multipart/alternative")))
5793           (gnus-add-text-properties
5794            (setq from (point))
5795            (progn
5796              (insert (format "%d.  " id))
5797              (point))
5798            `(gnus-callback
5799              (lambda (handles)
5800                (unless ,(not ibegend)
5801                  (setq gnus-article-mime-handle-alist
5802                        ',gnus-article-mime-handle-alist))
5803                (gnus-mime-display-alternative
5804                 ',ihandles ',not-pref ',begend ,id))
5805              keymap ,gnus-mime-button-map
5806              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5807              face ,gnus-article-button-face
5808              gnus-part ,id
5809              article-type multipart))
5810           (widget-convert-button 'link from (point)
5811                                  :action 'gnus-widget-press-button
5812                                  :button-keymap gnus-widget-button-keymap)
5813           ;; Do the handles
5814           (while (setq handle (pop handles))
5815             (gnus-add-text-properties
5816              (setq from (point))
5817              (progn
5818                (insert (format "(%c) %-18s"
5819                                (if (equal handle preferred) ?* ? )
5820                                (mm-handle-media-type handle)))
5821                (point))
5822              `(gnus-callback
5823                (lambda (handles)
5824                  (unless ,(not ibegend)
5825                    (setq gnus-article-mime-handle-alist
5826                          ',gnus-article-mime-handle-alist))
5827                  (gnus-mime-display-alternative
5828                   ',ihandles ',handle ',begend ,id))
5829                keymap ,gnus-mime-button-map
5830                ,gnus-mouse-face-prop ,gnus-article-mouse-face
5831                face ,gnus-article-button-face
5832                gnus-part ,id
5833                gnus-data ,handle))
5834             (widget-convert-button 'link from (point)
5835                                    :action 'gnus-widget-press-button
5836                                    :button-keymap gnus-widget-button-keymap)
5837             (insert "  "))
5838           (insert "\n\n"))
5839         (when preferred
5840           (if (stringp (car preferred))
5841               (gnus-display-mime preferred)
5842             (let ((mail-parse-charset gnus-newsgroup-charset)
5843                   (mail-parse-ignored-charsets
5844                    (with-current-buffer gnus-summary-buffer
5845                      gnus-newsgroup-ignored-charsets)))
5846               (mm-display-part preferred)
5847               ;; Do highlighting.
5848               (save-excursion
5849                 (save-restriction
5850                   (narrow-to-region (car begend) (point-max))
5851                   (gnus-treat-article
5852                    nil (length gnus-article-mime-handle-alist)
5853                    (gnus-article-mime-total-parts)
5854                    (mm-handle-media-type handle))))))
5855           (goto-char (point-max))
5856           (setcdr begend (point-marker)))))
5857     (when ibegend
5858       (goto-char point))))
5859
5860 (defconst gnus-article-wash-status-strings
5861   (let ((alist '((cite "c" "Possible hidden citation text"
5862                        " " "All citation text visible")
5863                  (headers "h" "Hidden headers"
5864                           " " "All headers visible.")
5865                  (pgp "p" "Encrypted or signed message status hidden"
5866                       " " "No hidden encryption nor digital signature status")
5867                  (signature "s" "Signature has been hidden"
5868                             " " "Signature is visible")
5869                  (overstrike "o" "Overstrike (^H) characters applied"
5870                              " " "No overstrike characters applied")
5871                  (emphasis "e" "/*_Emphasis_*/ characters applied"
5872                            " " "No /*_emphasis_*/ characters applied")))
5873         result)
5874     (dolist (entry alist result)
5875       (let ((key (nth 0 entry))
5876             (on (copy-sequence (nth 1 entry)))
5877             (on-help (nth 2 entry))
5878             (off (copy-sequence (nth 3 entry)))
5879             (off-help (nth 4 entry)))
5880         (put-text-property 0 1 'help-echo on-help on)
5881         (put-text-property 0 1 'help-echo off-help off)
5882         (push (list key on off) result))))
5883   "Alist of strings describing wash status in the mode line.
5884 Each entry has the form (KEY ON OF), where the KEY is a symbol
5885 representing the particular washing function, ON is the string to use
5886 in the article mode line when the washing function is active, and OFF
5887 is the string to use when it is inactive.")
5888
5889 (defun gnus-article-wash-status-entry (key value)
5890   (let ((entry (assoc key gnus-article-wash-status-strings)))
5891     (if value (nth 1 entry) (nth 2 entry))))
5892
5893 (defun gnus-article-wash-status ()
5894   "Return a string which display status of article washing."
5895   (with-current-buffer gnus-article-buffer
5896     (let ((cite (memq 'cite gnus-article-wash-types))
5897           (headers (memq 'headers gnus-article-wash-types))
5898           (boring (memq 'boring-headers gnus-article-wash-types))
5899           (pgp (memq 'pgp gnus-article-wash-types))
5900           (pem (memq 'pem gnus-article-wash-types))
5901           (signed (memq 'signed gnus-article-wash-types))
5902           (encrypted (memq 'encrypted gnus-article-wash-types))
5903           (signature (memq 'signature gnus-article-wash-types))
5904           (overstrike (memq 'overstrike gnus-article-wash-types))
5905           (emphasis (memq 'emphasis gnus-article-wash-types)))
5906       (concat
5907        (gnus-article-wash-status-entry 'cite cite)
5908        (gnus-article-wash-status-entry 'headers (or headers boring))
5909        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
5910        (gnus-article-wash-status-entry 'signature signature)
5911        (gnus-article-wash-status-entry 'overstrike overstrike)
5912        (gnus-article-wash-status-entry 'emphasis emphasis)))))
5913
5914 (defun gnus-add-wash-type (type)
5915   "Add a washing of TYPE to the current status."
5916   (add-to-list 'gnus-article-wash-types type))
5917
5918 (defun gnus-delete-wash-type (type)
5919   "Add a washing of TYPE to the current status."
5920   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
5921
5922 (defun gnus-add-image (category image)
5923   "Add IMAGE of CATEGORY to the list of displayed images."
5924   (let ((entry (assq category gnus-article-image-alist)))
5925     (unless entry
5926       (setq entry (list category))
5927       (push entry gnus-article-image-alist))
5928     (nconc entry (list image))))
5929
5930 (defun gnus-delete-images (category)
5931   "Delete all images in CATEGORY."
5932   (let ((entry (assq category gnus-article-image-alist)))
5933     (dolist (image (cdr entry))
5934       (gnus-remove-image image category))
5935     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
5936     (gnus-delete-wash-type category)))
5937
5938 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
5939
5940 (defun gnus-article-maybe-hide-headers ()
5941   "Hide unwanted headers if `gnus-have-all-headers' is nil.
5942 Provided for backwards compatibility."
5943   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
5944                  (not (with-current-buffer gnus-summary-buffer
5945                         gnus-have-all-headers)))
5946              (not gnus-inhibit-hiding))
5947     (gnus-article-hide-headers)))
5948
5949 ;;; Article savers.
5950
5951 (defun gnus-output-to-file (file-name)
5952   "Append the current article to a file named FILE-NAME.
5953 If `gnus-article-save-coding-system' is non-nil, it is used to encode
5954 text and used as the value of the coding cookie which is added to the
5955 top of a file.  Otherwise, this function saves a raw article without
5956 the coding cookie."
5957   (let* ((artbuf (current-buffer))
5958          (file-name-coding-system nnmail-pathname-coding-system)
5959          (coding gnus-article-save-coding-system)
5960          (coding-system-for-read (if coding
5961                                      nil ;; Rely on the coding cookie.
5962                                    mm-text-coding-system))
5963          (coding-system-for-write (or coding
5964                                       mm-text-coding-system-for-write
5965                                       mm-text-coding-system))
5966          (exists (file-exists-p file-name)))
5967     (with-temp-buffer
5968       (when exists
5969         (insert-file-contents file-name)
5970         (goto-char (point-min))
5971         ;; Remove the existing coding cookie.
5972         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
5973           (delete-region (match-beginning 0) (match-end 0))))
5974       (goto-char (point-max))
5975       (insert-buffer-substring artbuf)
5976       ;; Append newline at end of the buffer as separator, and then
5977       ;; save it to file.
5978       (goto-char (point-max))
5979       (insert "\n")
5980       (when coding
5981         ;; If the coding system is not suitable to encode the text,
5982         ;; ask a user for a proper one.
5983         (when (fboundp 'select-safe-coding-system)
5984           (setq coding (coding-system-base
5985                         (save-window-excursion
5986                           (select-safe-coding-system (point-min) (point-max)
5987                                                      coding))))
5988           (setq coding-system-for-write
5989                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
5990                     coding)))
5991         (goto-char (point-min))
5992         ;; Add the coding cookie.
5993         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
5994                         coding-system-for-write)))
5995       (if exists
5996           (progn
5997             (write-region (point-min) (point-max) file-name nil 'no-message)
5998             (message "Appended to %s" file-name))
5999         (write-region (point-min) (point-max) file-name))))
6000   t)
6001
6002 (defun gnus-narrow-to-page (&optional arg)
6003   "Narrow the article buffer to a page.
6004 If given a numerical ARG, move forward ARG pages."
6005   (interactive "P")
6006   (setq arg (if arg (prefix-numeric-value arg) 0))
6007   (save-excursion
6008     (set-buffer gnus-article-buffer)
6009     (goto-char (point-min))
6010     (widen)
6011     ;; Remove any old next/prev buttons.
6012     (when (gnus-visual-p 'page-marker)
6013       (let ((inhibit-read-only t))
6014         (gnus-remove-text-with-property 'gnus-prev)
6015         (gnus-remove-text-with-property 'gnus-next)))
6016     (if
6017         (cond ((< arg 0)
6018                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
6019               ((> arg 0)
6020                (re-search-forward page-delimiter nil 'move arg)))
6021         (goto-char (match-end 0))
6022       (save-excursion
6023         (goto-char (point-min))
6024         (setq gnus-page-broken
6025               (and (re-search-forward page-delimiter nil t) t))))
6026     (when gnus-page-broken
6027       (narrow-to-region
6028        (point)
6029        (if (re-search-forward page-delimiter nil 'move)
6030            (match-beginning 0)
6031          (point)))
6032       (when (and (gnus-visual-p 'page-marker)
6033                  (> (point-min) (save-restriction (widen) (point-min))))
6034         (save-excursion
6035           (goto-char (point-min))
6036           (gnus-insert-prev-page-button)))
6037       (when (and (gnus-visual-p 'page-marker)
6038                  (< (point-max) (save-restriction (widen) (point-max))))
6039         (save-excursion
6040           (goto-char (point-max))
6041           (gnus-insert-next-page-button))))))
6042
6043 ;; Article mode commands
6044
6045 (defun gnus-article-goto-next-page ()
6046   "Show the next page of the article."
6047   (interactive)
6048   (when (gnus-article-next-page)
6049     (goto-char (point-min))
6050     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6051
6052
6053 (defun gnus-article-goto-prev-page ()
6054   "Show the previous page of the article."
6055   (interactive)
6056   (if (bobp)
6057       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6058     (gnus-article-prev-page nil)))
6059
6060 ;; This is cleaner but currently breaks `gnus-pick-mode':
6061 ;;
6062 ;; (defun gnus-article-goto-next-page ()
6063 ;;   "Show the next page of the article."
6064 ;;   (interactive)
6065 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6066 ;;     (gnus-summary-next-page)))
6067 ;;
6068 ;; (defun gnus-article-goto-prev-page ()
6069 ;;   "Show the next page of the article."
6070 ;;   (interactive)
6071 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6072 ;;     (gnus-summary-prev-page)))
6073
6074 (defun gnus-article-next-page (&optional lines)
6075   "Show the next page of the current article.
6076 If end of article, return non-nil.  Otherwise return nil.
6077 Argument LINES specifies lines to be scrolled up."
6078   (interactive "p")
6079   (move-to-window-line -1)
6080   (if (and (not (and gnus-article-over-scroll
6081                      (> (count-lines (window-start) (point-max))
6082                         (+ (or lines (1- (window-height)))
6083                            (or (and (boundp 'scroll-margin)
6084                                     (symbol-value 'scroll-margin))
6085                                0)))))
6086            (save-excursion
6087              (end-of-line)
6088              (and (pos-visible-in-window-p)     ;Not continuation line.
6089                   (>= (1+ (point)) (point-max))))) ;Allow for trailing newline.
6090       ;; Nothing in this page.
6091       (if (or (not gnus-page-broken)
6092               (save-excursion
6093                 (save-restriction
6094                   (widen)
6095                   (forward-line)
6096                   (eobp)))) ;Real end-of-buffer?
6097           (progn
6098             (when gnus-article-over-scroll
6099               (gnus-article-next-page-1 lines))
6100             t)                  ;Nothing more.
6101         (gnus-narrow-to-page 1)         ;Go to next page.
6102         nil)
6103     ;; More in this page.
6104     (gnus-article-next-page-1 lines)
6105     nil))
6106
6107 (defmacro gnus-article-beginning-of-window ()
6108   "Move point to the beginning of the window.
6109 In Emacs, the point is placed at the line number which `scroll-margin'
6110 specifies."
6111   (if (featurep 'xemacs)
6112       '(move-to-window-line 0)
6113     '(move-to-window-line
6114       (min (max 0 scroll-margin)
6115            (max 1 (- (window-height)
6116                      (if mode-line-format 1 0)
6117                      (if header-line-format 1 0)))))))
6118
6119 (defun gnus-article-next-page-1 (lines)
6120   (when (and (not (featurep 'xemacs))
6121              (numberp lines)
6122              (> lines 0)
6123              (numberp (symbol-value 'scroll-margin))
6124              (> (symbol-value 'scroll-margin) 0))
6125     ;; Protect against the bug that Emacs 21.x hangs up when scrolling up for
6126     ;; too many number of lines if `scroll-margin' is set as two or greater.
6127     (setq lines (min lines
6128                      (max 0 (- (count-lines (window-start) (point-max))
6129                                (symbol-value 'scroll-margin))))))
6130   (condition-case ()
6131       (let ((scroll-in-place nil))
6132         (scroll-up lines))
6133     (end-of-buffer
6134      ;; Long lines may cause an end-of-buffer error.
6135      (goto-char (point-max))))
6136   (gnus-article-beginning-of-window))
6137
6138 (defun gnus-article-prev-page (&optional lines)
6139   "Show previous page of current article.
6140 Argument LINES specifies lines to be scrolled down."
6141   (interactive "p")
6142   (move-to-window-line 0)
6143   (if (and gnus-page-broken
6144            (bobp)
6145            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6146       (progn
6147         (gnus-narrow-to-page -1)        ;Go to previous page.
6148         (goto-char (point-max))
6149         (recenter (if gnus-article-over-scroll
6150                       (if lines
6151                           (max (+ lines (or (and (boundp 'scroll-margin)
6152                                                  (symbol-value 'scroll-margin))
6153                                             0))
6154                                3)
6155                         (- (window-height) 2))
6156                     -1)))
6157     (prog1
6158         (condition-case ()
6159             (let ((scroll-in-place nil))
6160               (scroll-down lines))
6161           (beginning-of-buffer
6162            (goto-char (point-min))))
6163       (gnus-article-beginning-of-window))))
6164
6165 (defun gnus-article-only-boring-p ()
6166   "Decide whether there is only boring text remaining in the article.
6167 Something \"interesting\" is a word of at least two letters that does
6168 not have a face in `gnus-article-boring-faces'."
6169   (when (and gnus-article-skip-boring
6170              (boundp 'gnus-article-boring-faces)
6171              (symbol-value 'gnus-article-boring-faces))
6172     (save-excursion
6173       (let ((inhibit-point-motion-hooks t))
6174         (catch 'only-boring
6175           (while (re-search-forward "\\b\\w\\w" nil t)
6176             (forward-char -1)
6177             (when (not (gnus-intersection
6178                         (gnus-faces-at (point))
6179                         (symbol-value 'gnus-article-boring-faces)))
6180               (throw 'only-boring nil)))
6181           (throw 'only-boring t))))))
6182
6183 (defun gnus-article-refer-article ()
6184   "Read article specified by message-id around point."
6185   (interactive)
6186   (save-excursion
6187     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6188     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6189     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6190         (let ((msg-id (concat "<" (match-string 0) ">")))
6191           (set-buffer gnus-summary-buffer)
6192           (gnus-summary-refer-article msg-id))
6193       (error "No references around point"))))
6194
6195 (defun gnus-article-show-summary ()
6196   "Reconfigure windows to show summary buffer."
6197   (interactive)
6198   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6199       (error "There is no summary buffer for this article buffer")
6200     (gnus-article-set-globals)
6201     (gnus-configure-windows 'article)
6202     (gnus-summary-goto-subject gnus-current-article)
6203     (gnus-summary-position-point)))
6204
6205 (defun gnus-article-describe-briefly ()
6206   "Describe article mode commands briefly."
6207   (interactive)
6208   (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")))
6209
6210 (defun gnus-article-summary-command ()
6211   "Execute the last keystroke in the summary buffer."
6212   (interactive)
6213   (let ((obuf (current-buffer))
6214         (owin (current-window-configuration))
6215         func)
6216     (switch-to-buffer gnus-article-current-summary 'norecord)
6217     (setq func (lookup-key (current-local-map) (this-command-keys)))
6218     (call-interactively func)
6219     (set-buffer obuf)
6220     (set-window-configuration owin)
6221     (set-window-point (get-buffer-window (current-buffer)) (point))))
6222
6223 (defun gnus-article-summary-command-nosave ()
6224   "Execute the last keystroke in the summary buffer."
6225   (interactive)
6226   (let (func)
6227     (pop-to-buffer gnus-article-current-summary)
6228     (setq func (lookup-key (current-local-map) (this-command-keys)))
6229     (call-interactively func)))
6230
6231 (defun gnus-article-check-buffer ()
6232   "Beep if not in an article buffer."
6233   (unless (equal major-mode 'gnus-article-mode)
6234     (error "Command invoked outside of a Gnus article buffer")))
6235
6236 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6237   "Read a summary buffer key sequence and execute it from the article buffer."
6238   (interactive "P")
6239   (gnus-article-check-buffer)
6240   (let ((nosaves
6241          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6242            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6243            "=" "^" "\M-^" "|"))
6244         (nosave-but-article
6245          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6246            "An" "Ap" [?A (meta return)] [?A delete]))
6247         (nosave-in-article
6248          '("AS" "\C-d"))
6249         (up-to-top
6250          '("n" "Gn" "p" "Gp"))
6251         keys new-sum-point)
6252     (save-excursion
6253       (set-buffer gnus-article-current-summary)
6254       (let (gnus-pick-mode)
6255         (setq unread-command-events (nconc unread-command-events
6256                                            (list (or key last-command-event)))
6257               keys (if (featurep 'xemacs)
6258                        (events-to-keys (read-key-sequence nil t))
6259                      (read-key-sequence nil t)))))
6260
6261     (message "")
6262
6263     (cond
6264      ((eq (aref keys (1- (length keys))) ?\C-h)
6265       (gnus-article-describe-bindings (substring keys 0 -1)))
6266      ((or (member keys nosaves)
6267           (member keys nosave-but-article)
6268           (member keys nosave-in-article))
6269       (let (func)
6270         (save-window-excursion
6271           (pop-to-buffer gnus-article-current-summary)
6272           ;; We disable the pick minor mode commands.
6273           (let (gnus-pick-mode)
6274             (setq func (lookup-key (current-local-map) keys))))
6275         (if (or (not func)
6276                 (numberp func))
6277             (ding)
6278           (unless (member keys nosave-in-article)
6279             (set-buffer gnus-article-current-summary))
6280           (call-interactively func)
6281           (setq new-sum-point (point)))
6282         (when (member keys nosave-but-article)
6283           (pop-to-buffer gnus-article-buffer))))
6284      (t
6285       ;; These commands should restore window configuration.
6286       (let ((obuf (current-buffer))
6287             (owin (current-window-configuration))
6288             win func in-buffer selected new-sum-start new-sum-hscroll err)
6289         (cond (not-restore-window
6290                (pop-to-buffer gnus-article-current-summary)
6291                (setq win (selected-window)))
6292               ((setq win (get-buffer-window gnus-article-current-summary))
6293                (select-window win))
6294               (t
6295                (let ((summary-buffer gnus-article-current-summary))
6296                  (gnus-configure-windows 'article)
6297                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6298                    (let ((gnus-buffer-configuration
6299                           '(article ((vertical 1.0
6300                                                (summary 0.25 point)
6301                                                (article 1.0))))))
6302                      (gnus-configure-windows 'article))
6303                    (setq win (get-buffer-window summary-buffer 'visible)))
6304                  (gnus-select-frame-set-input-focus (window-frame win))
6305                  (select-window win))))
6306         (setq in-buffer (current-buffer))
6307         ;; We disable the pick minor mode commands.
6308         (if (and (setq func (let (gnus-pick-mode)
6309                               (lookup-key (current-local-map) keys)))
6310                  (functionp func)
6311                  (condition-case code
6312                      (progn
6313                        (call-interactively func)
6314                        t)
6315                    (error
6316                     (setq err code)
6317                     nil)))
6318             (progn
6319               (when (eq win (selected-window))
6320                 (setq new-sum-point (point)
6321                       new-sum-start (window-start win)
6322                       new-sum-hscroll (window-hscroll win)))
6323               (when (or (eq in-buffer (current-buffer))
6324                         (when (eq obuf (current-buffer))
6325                           (set-buffer in-buffer)
6326                           t))
6327                 (setq selected (gnus-summary-select-article))
6328                 (set-buffer obuf)
6329                 (unless not-restore-window
6330                   (set-window-configuration owin))
6331                 (when (and (eq selected 'old)
6332                            new-sum-point)
6333                   (set-window-start (get-buffer-window (current-buffer))
6334                                     1)
6335                   (set-window-point (get-buffer-window (current-buffer))
6336                                     (if (article-goto-body)
6337                                         (1- (point))
6338                                       (point))))
6339                 (when (and (not not-restore-window)
6340                            new-sum-point
6341                            (with-current-buffer (window-buffer win)
6342                              (eq major-mode 'gnus-summary-mode)))
6343                   (set-window-point win new-sum-point)
6344                   (set-window-start win new-sum-start)
6345                   (set-window-hscroll win new-sum-hscroll))))
6346           (set-window-configuration owin)
6347           (if err
6348               (signal (car err) (cdr err))
6349             (ding))))))))
6350
6351 (defun gnus-article-read-summary-send-keys ()
6352   (interactive)
6353   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6354     (gnus-article-read-summary-keys)))
6355
6356 (defun gnus-article-describe-key (key)
6357   "Display documentation of the function invoked by KEY.
6358 KEY is a string or a vector."
6359   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6360                        (read-key-sequence "Describe key: "))))
6361   (gnus-article-check-buffer)
6362   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6363                                   gnus-article-read-summary-send-keys))
6364       (save-excursion
6365         (set-buffer gnus-article-current-summary)
6366         (setq unread-command-events
6367               (if (featurep 'xemacs)
6368                   (append key nil)
6369                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6370                                         (list 'meta (- x 128))
6371                                       x))
6372                         key)))
6373         (let ((cursor-in-echo-area t)
6374               gnus-pick-mode)
6375           (describe-key (read-key-sequence nil t))))
6376     (describe-key key)))
6377
6378 (defun gnus-article-describe-key-briefly (key &optional insert)
6379   "Display documentation of the function invoked by KEY.
6380 KEY is a string or a vector."
6381   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6382                        (read-key-sequence "Describe key: "))
6383                      current-prefix-arg))
6384   (gnus-article-check-buffer)
6385   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6386                                   gnus-article-read-summary-send-keys))
6387       (save-excursion
6388         (set-buffer gnus-article-current-summary)
6389         (setq unread-command-events
6390               (if (featurep 'xemacs)
6391                   (append key nil)
6392                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6393                                         (list 'meta (- x 128))
6394                                       x))
6395                         key)))
6396         (let ((cursor-in-echo-area t)
6397               gnus-pick-mode)
6398           (describe-key-briefly (read-key-sequence nil t) insert)))
6399     (describe-key-briefly key insert)))
6400
6401 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6402 (defvar gnus-agent-summary-mode)
6403
6404 (defun gnus-article-describe-bindings (&optional prefix)
6405   "Show a list of all defined keys, and their definitions.
6406 The optional argument PREFIX, if non-nil, should be a key sequence;
6407 then we display only bindings that start with that prefix."
6408   (interactive)
6409   (gnus-article-check-buffer)
6410   (let ((keymap (copy-keymap gnus-article-mode-map))
6411         (map (copy-keymap gnus-article-send-map))
6412         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6413         agent)
6414     (define-key keymap "S" map)
6415     (define-key map [t] nil)
6416     (with-current-buffer gnus-article-current-summary
6417       (set-keymap-parent map (key-binding "S"))
6418       (let (def gnus-pick-mode)
6419         (dolist (key sumkeys)
6420           (when (setq def (key-binding key))
6421             (define-key keymap key def))))
6422       (when (boundp 'gnus-agent-summary-mode)
6423         (setq agent gnus-agent-summary-mode)))
6424     (with-temp-buffer
6425       (use-local-map keymap)
6426       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6427       (describe-bindings prefix))
6428     (let ((item `((lambda (prefix)
6429                     (save-excursion
6430                       (set-buffer ,(current-buffer))
6431                       (gnus-article-describe-bindings prefix)))
6432                   ,prefix)))
6433       (with-current-buffer (if (fboundp 'help-buffer)
6434                                (let (help-xref-following) (help-buffer))
6435                              "*Help*") ;; Emacs 21
6436         (setq help-xref-stack-item item)))))
6437
6438 (defun gnus-article-reply-with-original (&optional wide)
6439   "Start composing a reply mail to the current message.
6440 The text in the region will be yanked.  If the region isn't active,
6441 the entire article will be yanked."
6442   (interactive)
6443   (let ((article (cdr gnus-article-current))
6444         contents)
6445     (if (not (gnus-region-active-p))
6446         (with-current-buffer gnus-summary-buffer
6447           (gnus-summary-reply (list (list article)) wide))
6448       (setq contents (buffer-substring (point) (mark t)))
6449       ;; Deactivate active regions.
6450       (when (and (boundp 'transient-mark-mode)
6451                  transient-mark-mode)
6452         (setq mark-active nil))
6453       (with-current-buffer gnus-summary-buffer
6454         (gnus-summary-reply
6455          (list (list article contents)) wide)))))
6456
6457 (defun gnus-article-wide-reply-with-original ()
6458   "Start composing a wide reply mail to the current message.
6459 The text in the region will be yanked.  If the region isn't active,
6460 the entire article will be yanked."
6461   (interactive)
6462   (gnus-article-reply-with-original t))
6463
6464 (defun gnus-article-followup-with-original ()
6465   "Compose a followup to the current article.
6466 The text in the region will be yanked.  If the region isn't active,
6467 the entire article will be yanked."
6468   (interactive)
6469   (let ((article (cdr gnus-article-current))
6470         contents)
6471       (if (not (gnus-region-active-p))
6472           (with-current-buffer gnus-summary-buffer
6473             (gnus-summary-followup (list (list article))))
6474         (setq contents (buffer-substring (point) (mark t)))
6475         ;; Deactivate active regions.
6476         (when (and (boundp 'transient-mark-mode)
6477                    transient-mark-mode)
6478           (setq mark-active nil))
6479         (with-current-buffer gnus-summary-buffer
6480           (gnus-summary-followup
6481            (list (list article contents)))))))
6482
6483 (defun gnus-article-hide (&optional arg force)
6484   "Hide all the gruft in the current article.
6485 This means that signatures, cited text and (some) headers will be
6486 hidden.
6487 If given a prefix, show the hidden text instead."
6488   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6489   (gnus-article-hide-headers arg)
6490   (gnus-article-hide-list-identifiers arg)
6491   (gnus-article-hide-citation-maybe arg force)
6492   (gnus-article-hide-signature arg))
6493
6494 (defun gnus-article-maybe-highlight ()
6495   "Do some article highlighting if article highlighting is requested."
6496   (when (gnus-visual-p 'article-highlight 'highlight)
6497     (gnus-article-highlight-some)))
6498
6499 (defun gnus-check-group-server ()
6500   ;; Make sure the connection to the server is alive.
6501   (unless (gnus-server-opened
6502            (gnus-find-method-for-group gnus-newsgroup-name))
6503     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6504     (gnus-request-group gnus-newsgroup-name t)))
6505
6506 (eval-when-compile
6507   (autoload 'nneething-get-file-name "nneething"))
6508
6509 (defun gnus-request-article-this-buffer (article group)
6510   "Get an article and insert it into this buffer."
6511   (let (do-update-line sparse-header)
6512     (prog1
6513         (save-excursion
6514           (erase-buffer)
6515           (gnus-kill-all-overlays)
6516           (setq group (or group gnus-newsgroup-name))
6517
6518           ;; Using `gnus-request-article' directly will insert the article into
6519           ;; `nntp-server-buffer' - so we'll save some time by not having to
6520           ;; copy it from the server buffer into the article buffer.
6521
6522           ;; We only request an article by message-id when we do not have the
6523           ;; headers for it, so we'll have to get those.
6524           (when (stringp article)
6525             (gnus-read-header article))
6526
6527           ;; If the article number is negative, that means that this article
6528           ;; doesn't belong in this newsgroup (possibly), so we find its
6529           ;; message-id and request it by id instead of number.
6530           (when (and (numberp article)
6531                      gnus-summary-buffer
6532                      (get-buffer gnus-summary-buffer)
6533                      (gnus-buffer-exists-p gnus-summary-buffer))
6534             (save-excursion
6535               (set-buffer gnus-summary-buffer)
6536               (let ((header (gnus-summary-article-header article)))
6537                 (when (< article 0)
6538                   (cond
6539                    ((memq article gnus-newsgroup-sparse)
6540                     ;; This is a sparse gap article.
6541                     (setq do-update-line article)
6542                     (setq article (mail-header-id header))
6543                     (setq sparse-header (gnus-read-header article))
6544                     (setq gnus-newsgroup-sparse
6545                           (delq article gnus-newsgroup-sparse)))
6546                    ((vectorp header)
6547                     ;; It's a real article.
6548                     (setq article (mail-header-id header)))
6549                    (t
6550                     ;; It is an extracted pseudo-article.
6551                     (setq article 'pseudo)
6552                     (gnus-request-pseudo-article header))))
6553
6554                 (let ((method (gnus-find-method-for-group
6555                                gnus-newsgroup-name)))
6556                   (when (and (eq (car method) 'nneething)
6557                              (vectorp header))
6558                     (let ((dir (nneething-get-file-name
6559                                 (mail-header-id header))))
6560                       (when (and (stringp dir)
6561                                  (file-directory-p dir))
6562                         (setq article 'nneething)
6563                         (gnus-group-enter-directory dir))))))))
6564
6565           (cond
6566            ;; Refuse to select canceled articles.
6567            ((and (numberp article)
6568                  gnus-summary-buffer
6569                  (get-buffer gnus-summary-buffer)
6570                  (gnus-buffer-exists-p gnus-summary-buffer)
6571                  (eq (cdr (with-current-buffer gnus-summary-buffer
6572                             (assq article gnus-newsgroup-reads)))
6573                      gnus-canceled-mark))
6574             nil)
6575            ;; We first check `gnus-original-article-buffer'.
6576            ((and (get-buffer gnus-original-article-buffer)
6577                  (numberp article)
6578                  (with-current-buffer gnus-original-article-buffer
6579                    (and (equal (car gnus-original-article) group)
6580                         (eq (cdr gnus-original-article) article))))
6581             (insert-buffer-substring gnus-original-article-buffer)
6582             'article)
6583            ;; Check the backlog.
6584            ((and gnus-keep-backlog
6585                  (gnus-backlog-request-article group article (current-buffer)))
6586             'article)
6587            ;; Check asynchronous pre-fetch.
6588            ((gnus-async-request-fetched-article group article (current-buffer))
6589             (gnus-async-prefetch-next group article gnus-summary-buffer)
6590             (when (and (numberp article) gnus-keep-backlog)
6591               (gnus-backlog-enter-article group article (current-buffer)))
6592             'article)
6593            ;; Check the cache.
6594            ((and gnus-use-cache
6595                  (numberp article)
6596                  (gnus-cache-request-article article group))
6597             'article)
6598            ;; Check the agent cache.
6599            ((gnus-agent-request-article article group)
6600             'article)
6601            ;; Get the article and put into the article buffer.
6602            ((or (stringp article)
6603                 (numberp article))
6604             (let ((gnus-override-method gnus-override-method)
6605                   (methods (and (stringp article)
6606                                 gnus-refer-article-method))
6607                   (backend (car (gnus-find-method-for-group
6608                                  gnus-newsgroup-name)))
6609                   result
6610                   (inhibit-read-only t))
6611               (if (or (not (listp methods))
6612                       (and (symbolp (car methods))
6613                            (assq (car methods) nnoo-definition-alist)))
6614                   (setq methods (list methods)))
6615               (when (and (null gnus-override-method)
6616                          methods)
6617                 (setq gnus-override-method (pop methods)))
6618               (while (not result)
6619                 (when (eq gnus-override-method 'current)
6620                   (setq gnus-override-method
6621                         (with-current-buffer gnus-summary-buffer
6622                           gnus-current-select-method)))
6623                 (erase-buffer)
6624                 (gnus-kill-all-overlays)
6625                 (let ((gnus-newsgroup-name group))
6626                   (gnus-check-group-server))
6627                 (cond
6628                  ((gnus-request-article article group (current-buffer))
6629                   (when (numberp article)
6630                     (gnus-async-prefetch-next group article
6631                                               gnus-summary-buffer)
6632                     (when gnus-keep-backlog
6633                       (gnus-backlog-enter-article
6634                        group article (current-buffer))))
6635                   (setq result 'article))
6636                  (methods
6637                   (setq gnus-override-method (pop methods)))
6638                  ((not (string-match "^400 "
6639                                      (nnheader-get-report backend)))
6640                   ;; If we get 400 server disconnect, reconnect and
6641                   ;; retry; otherwise, assume the article has expired.
6642                   (setq result 'done))))
6643               (and (eq result 'article) 'article)))
6644            ;; It was a pseudo.
6645            (t article)))
6646
6647       ;; Associate this article with the current summary buffer.
6648       (setq gnus-article-current-summary gnus-summary-buffer)
6649
6650       ;; Take the article from the original article buffer
6651       ;; and place it in the buffer it's supposed to be in.
6652       (when (and (get-buffer gnus-article-buffer)
6653                  (equal (buffer-name (current-buffer))
6654                         (buffer-name (get-buffer gnus-article-buffer))))
6655         (save-excursion
6656           (if (get-buffer gnus-original-article-buffer)
6657               (set-buffer gnus-original-article-buffer)
6658             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6659             (buffer-disable-undo)
6660             (setq major-mode 'gnus-original-article-mode)
6661             (setq buffer-read-only t))
6662           (let ((inhibit-read-only t))
6663             (erase-buffer)
6664             (insert-buffer-substring gnus-article-buffer))
6665           (setq gnus-original-article (cons group article)))
6666
6667         ;; Decode charsets.
6668         (run-hooks 'gnus-article-decode-hook)
6669         ;; Mark article as decoded or not.
6670         (setq gnus-article-decoded-p gnus-article-decode-hook))
6671
6672       ;; Update sparse articles.
6673       (when (and do-update-line
6674                  (or (numberp article)
6675                      (stringp article)))
6676         (let ((buf (current-buffer)))
6677           (set-buffer gnus-summary-buffer)
6678           (gnus-summary-update-article do-update-line sparse-header)
6679           (gnus-summary-goto-subject do-update-line nil t)
6680           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6681                             (point))
6682           (set-buffer buf))))))
6683
6684 ;;;
6685 ;;; Article editing
6686 ;;;
6687
6688 (defcustom gnus-article-edit-mode-hook nil
6689   "Hook run in article edit mode buffers."
6690   :group 'gnus-article-various
6691   :type 'hook)
6692
6693 (defvar gnus-article-edit-done-function nil)
6694
6695 (defvar gnus-article-edit-mode-map nil)
6696
6697 ;; Should we be using derived.el for this?
6698 (unless gnus-article-edit-mode-map
6699   (setq gnus-article-edit-mode-map (make-keymap))
6700   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6701
6702   (gnus-define-keys gnus-article-edit-mode-map
6703     "\C-c?"    describe-mode
6704     "\C-c\C-c" gnus-article-edit-done
6705     "\C-c\C-k" gnus-article-edit-exit
6706     "\C-c\C-f\C-t" message-goto-to
6707     "\C-c\C-f\C-o" message-goto-from
6708     "\C-c\C-f\C-b" message-goto-bcc
6709     ;;"\C-c\C-f\C-w" message-goto-fcc
6710     "\C-c\C-f\C-c" message-goto-cc
6711     "\C-c\C-f\C-s" message-goto-subject
6712     "\C-c\C-f\C-r" message-goto-reply-to
6713     "\C-c\C-f\C-n" message-goto-newsgroups
6714     "\C-c\C-f\C-d" message-goto-distribution
6715     "\C-c\C-f\C-f" message-goto-followup-to
6716     "\C-c\C-f\C-m" message-goto-mail-followup-to
6717     "\C-c\C-f\C-k" message-goto-keywords
6718     "\C-c\C-f\C-u" message-goto-summary
6719     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6720     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6721     "\C-c\C-b" message-goto-body
6722     "\C-c\C-i" message-goto-signature
6723
6724     "\C-c\C-t" message-insert-to
6725     "\C-c\C-n" message-insert-newsgroups
6726     "\C-c\C-o" message-sort-headers
6727     "\C-c\C-e" message-elide-region
6728     "\C-c\C-v" message-delete-not-region
6729     "\C-c\C-z" message-kill-to-signature
6730     "\M-\r" message-newline-and-reformat
6731     "\C-c\C-a" mml-attach-file
6732     "\C-a" message-beginning-of-line
6733     "\t" message-tab
6734     "\M-;" comment-region)
6735
6736   (gnus-define-keys (gnus-article-edit-wash-map
6737                      "\C-c\C-w" gnus-article-edit-mode-map)
6738     "f" gnus-article-edit-full-stops))
6739
6740 (easy-menu-define
6741   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6742   '("Field"
6743     ["Fetch To" message-insert-to t]
6744     ["Fetch Newsgroups" message-insert-newsgroups t]
6745     "----"
6746     ["To" message-goto-to t]
6747     ["From" message-goto-from t]
6748     ["Subject" message-goto-subject t]
6749     ["Cc" message-goto-cc t]
6750     ["Reply-To" message-goto-reply-to t]
6751     ["Summary" message-goto-summary t]
6752     ["Keywords" message-goto-keywords t]
6753     ["Newsgroups" message-goto-newsgroups t]
6754     ["Followup-To" message-goto-followup-to t]
6755     ["Mail-Followup-To" message-goto-mail-followup-to t]
6756     ["Distribution" message-goto-distribution t]
6757     ["Body" message-goto-body t]
6758     ["Signature" message-goto-signature t]))
6759
6760 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6761   "Major mode for editing articles.
6762 This is an extended text-mode.
6763
6764 \\{gnus-article-edit-mode-map}"
6765   (make-local-variable 'gnus-article-edit-done-function)
6766   (make-local-variable 'gnus-prev-winconf)
6767   (set (make-local-variable 'font-lock-defaults)
6768        '(message-font-lock-keywords t))
6769   (set (make-local-variable 'mail-header-separator) "")
6770   (set (make-local-variable 'gnus-article-edit-mode) t)
6771   (easy-menu-add message-mode-field-menu message-mode-map)
6772   (mml-mode)
6773   (setq buffer-read-only nil)
6774   (buffer-enable-undo)
6775   (widen))
6776
6777 (defun gnus-article-edit (&optional force)
6778   "Edit the current article.
6779 This will have permanent effect only in mail groups.
6780 If FORCE is non-nil, allow editing of articles even in read-only
6781 groups."
6782   (interactive "P")
6783   (when (and (not force)
6784              (gnus-group-read-only-p))
6785     (error "The current newsgroup does not support article editing"))
6786   (gnus-article-date-original)
6787   (gnus-article-edit-article
6788    'ignore
6789    `(lambda (no-highlight)
6790       'ignore
6791       (gnus-summary-edit-article-done
6792        ,(or (mail-header-references gnus-current-headers) "")
6793        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
6794
6795 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
6796   "Start editing the contents of the current article buffer."
6797   (let ((winconf (current-window-configuration)))
6798     (set-buffer gnus-article-buffer)
6799     (let ((message-auto-save-directory
6800            ;; Don't associate the article buffer with a draft file.
6801            nil))
6802       (gnus-article-edit-mode))
6803     (funcall start-func)
6804     (set-buffer-modified-p nil)
6805     (gnus-configure-windows 'edit-article)
6806     (setq gnus-article-edit-done-function exit-func)
6807     (setq gnus-prev-winconf winconf)
6808     (unless quiet
6809       (gnus-message 6 "C-c C-c to end edits"))))
6810
6811 (defun gnus-article-edit-done (&optional arg)
6812   "Update the article edits and exit."
6813   (interactive "P")
6814   (let ((func gnus-article-edit-done-function)
6815         (buf (current-buffer))
6816         (start (window-start))
6817         (p (point))
6818         (winconf gnus-prev-winconf))
6819     (widen) ;; Widen it in case that users narrowed the buffer.
6820     (funcall func arg)
6821     (set-buffer buf)
6822     ;; The cache and backlog have to be flushed somewhat.
6823     (when gnus-keep-backlog
6824       (gnus-backlog-remove-article
6825        (car gnus-article-current) (cdr gnus-article-current)))
6826     ;; Flush original article as well.
6827     (save-excursion
6828       (when (get-buffer gnus-original-article-buffer)
6829         (set-buffer gnus-original-article-buffer)
6830         (setq gnus-original-article nil)))
6831     (when gnus-use-cache
6832       (gnus-cache-update-article
6833        (car gnus-article-current) (cdr gnus-article-current)))
6834     ;; We remove all text props from the article buffer.
6835     (kill-all-local-variables)
6836     (set-text-properties (point-min) (point-max) nil)
6837     (gnus-article-mode)
6838     (set-window-configuration winconf)
6839     (set-buffer buf)
6840     (set-window-start (get-buffer-window buf) start)
6841     (set-window-point (get-buffer-window buf) (point)))
6842   (gnus-summary-show-article))
6843
6844 (defun gnus-article-edit-exit ()
6845   "Exit the article editing without updating."
6846   (interactive)
6847   (when (or (not (buffer-modified-p))
6848             (yes-or-no-p "Article modified; kill anyway? "))
6849     (let ((curbuf (current-buffer))
6850           (p (point))
6851           (window-start (window-start)))
6852       (erase-buffer)
6853       (if (gnus-buffer-live-p gnus-original-article-buffer)
6854           (insert-buffer-substring gnus-original-article-buffer))
6855       (let ((winconf gnus-prev-winconf))
6856         (kill-all-local-variables)
6857         (gnus-article-mode)
6858         (set-window-configuration winconf)
6859         ;; Tippy-toe some to make sure that point remains where it was.
6860         (save-current-buffer
6861           (set-buffer curbuf)
6862           (set-window-start (get-buffer-window (current-buffer)) window-start)
6863           (goto-char p))))
6864     (gnus-summary-show-article)))
6865
6866 (defun gnus-article-edit-full-stops ()
6867   "Interactively repair spacing at end of sentences."
6868   (interactive)
6869   (save-excursion
6870     (goto-char (point-min))
6871     (search-forward-regexp "^$" nil t)
6872     (let ((case-fold-search nil))
6873       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
6874
6875 ;;;
6876 ;;; Article highlights
6877 ;;;
6878
6879 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
6880
6881 ;;; Internal Variables:
6882
6883 (defcustom gnus-button-url-regexp
6884   (concat
6885    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
6886    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
6887    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
6888    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
6889        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
6890              (punct "!?:;.,"))
6891          (concat
6892           "\\(?:"
6893           ;; Match paired parentheses, e.g. in Wikipedia URLs:
6894           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]"
6895           "\\|"
6896           "[" chars punct     "]+" "[" chars "]"
6897           "\\)"))
6898      (concat ;; XEmacs 21.4 doesn't support POSIX.
6899       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
6900       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
6901    "\\)")
6902   "Regular expression that matches URLs."
6903   :group 'gnus-article-buttons
6904   :type 'regexp)
6905
6906 (defcustom gnus-button-valid-fqdn-regexp
6907   message-valid-fqdn-regexp
6908   "Regular expression that matches a valid FQDN."
6909   :version "22.1"
6910   :group 'gnus-article-buttons
6911   :type 'regexp)
6912
6913 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
6914 (defcustom gnus-button-valid-localpart-regexp
6915   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
6916   "Regular expression that matches a localpart of mail addresses or MIDs."
6917   :version "22.1"
6918   :group 'gnus-article-buttons
6919   :type 'regexp)
6920
6921 (defcustom gnus-button-man-handler 'manual-entry
6922   "Function to use for displaying man pages.
6923 The function must take at least one argument with a string naming the
6924 man page."
6925   :version "22.1"
6926   :type '(choice (function-item :tag "Man" manual-entry)
6927                  (function-item :tag "Woman" woman)
6928                  (function :tag "Other"))
6929   :group 'gnus-article-buttons)
6930
6931 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
6932   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
6933 If the default site is too slow, try to find a CTAN mirror, see
6934 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
6935 the variable `gnus-button-handle-ctan'."
6936   :version "22.1"
6937   :group 'gnus-article-buttons
6938   :link '(custom-manual "(gnus)Group Parameters")
6939   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
6940                  (const "http://tug.ctan.org/tex-archive/")
6941                  (const "http://www.dante.de/CTAN/")
6942                  (string :tag "Other")))
6943
6944 (defcustom gnus-button-ctan-handler 'browse-url
6945   "Function to use for displaying CTAN links.
6946 The function must take one argument, the string naming the URL."
6947   :version "22.1"
6948   :type '(choice (function-item :tag "Browse Url" browse-url)
6949                  (function :tag "Other"))
6950   :group 'gnus-article-buttons)
6951
6952 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
6953   "Bogus strings removed from CTAN URLs."
6954   :version "22.1"
6955   :group 'gnus-article-buttons
6956   :type '(choice (const "^/?tex-archive/\\|/")
6957                  (regexp :tag "Other")))
6958
6959 (defcustom gnus-button-ctan-directory-regexp
6960   (regexp-opt
6961    (list "archive-tools" "biblio" "bibliography" "digests" "documentation"
6962          "dviware" "fonts" "graphics" "help" "indexing" "info" "language"
6963          "languages" "macros" "nonfree" "obsolete" "support" "systems"
6964          "tds" "tools" "usergrps" "web") t)
6965   "Regular expression for ctan directories.
6966 It should match all directories in the top level of `gnus-ctan-url'."
6967   :version "22.1"
6968   :group 'gnus-article-buttons
6969   :type 'regexp)
6970
6971 (defcustom gnus-button-mid-or-mail-regexp
6972   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
6973           gnus-button-valid-fqdn-regexp
6974           ">?\\)\\b")
6975   "Regular expression that matches a message ID or a mail address."
6976   :version "22.1"
6977   :group 'gnus-article-buttons
6978   :type 'regexp)
6979
6980 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
6981   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
6982 Strings like this can be either a message ID or a mail address.  If it is one
6983 of the symbols `mid' or `mail', Gnus will always assume that the string is a
6984 message ID or a mail address, respectively.  If this variable is set to the
6985 symbol `ask', always query the user what do do.  If it is a function, this
6986 function will be called with the string as its only argument.  The function
6987 must return `mid', `mail', `invalid' or `ask'."
6988   :version "22.1"
6989   :group 'gnus-article-buttons
6990   :type '(choice (function-item :tag "Heuristic function"
6991                                 gnus-button-mid-or-mail-heuristic)
6992                  (const ask)
6993                  (const mid)
6994                  (const mail)))
6995
6996 (defcustom gnus-button-mid-or-mail-heuristic-alist
6997   '((-10.0 . ".+\\$.+@")
6998     (-10.0 . "#")
6999     (-10.0 . "\\*")
7000     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7001     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7002     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7003     (-1.0  . "^[^a-z]+@")
7004     ;;
7005     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7006     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7007     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7008     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7009     ;;
7010     (-2.0  . "^[0-9]")
7011     (-1.0  . "^[0-9][0-9]")
7012     ;;
7013     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7014     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7015     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7016     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7017     ;;
7018     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7019     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7020     ;;       "[0-9]{8,}.*\@"
7021     (-3.0
7022      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7023     ;; "[0-9]{12,}.*\@"
7024     ;; compensation for TDMA dated mail addresses:
7025     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7026     ;;
7027     (-20.0 . "\\.fsf@") ;; Gnus
7028     (-20.0 . "^slrn")
7029     (-20.0 . "^Pine")
7030     (-20.0 . "^alpine\\.")
7031     (-20.0 . "_-_") ;; Subject change in thread
7032     ;;
7033     (-20.0 . "\\.ln@") ;; leafnode
7034     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7035     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7036     ;;
7037     ;; (5.0 . "") ;; $local_part_len <= 7
7038     (10.0  . "^[^0-9]+@")
7039     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7040     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7041     (3.0   . "\@stud")
7042     ;;
7043     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7044     ;;
7045     (0.5   . "^[A-Z][a-z]")
7046     (0.5   . "^[A-Z][a-z][a-z]")
7047     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7048     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7049   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7050
7051 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7052 address.  The REGEXP is processed with `case-fold-search' set to nil."
7053   :version "22.1"
7054   :group 'gnus-article-buttons
7055   :type '(repeat (cons (number :tag "Rate")
7056                        (regexp :tag "Regexp"))))
7057
7058 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7059   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7060 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7061 address, `ask' if unsure and `invalid' if the string is invalid."
7062   (let ((case-fold-search nil)
7063         (list gnus-button-mid-or-mail-heuristic-alist)
7064         (result 0) rate regexp lpartlen elem)
7065     (setq lpartlen
7066           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7067     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7068     ;; Certain special cases...
7069     (when (string-match
7070            (concat
7071             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7072             "^[0-9]+\\.[0-9]+@compuserve\\|"
7073             "@public\\.gmane\\.org")
7074            mid-or-mail)
7075       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7076       (setq result 'mail))
7077     (when (string-match "@.*@\\| " mid-or-mail)
7078       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7079       (setq result 'invalid))
7080     ;; Nothing more to do, if result is not a number here...
7081     (when (numberp result)
7082       (while list
7083         (setq elem (car list)
7084               rate (car elem)
7085               regexp (cdr elem)
7086               list (cdr list))
7087         (when (string-match regexp mid-or-mail)
7088           (setq result (+ result rate))
7089           (gnus-message
7090            9 "`%s' matched `%s', rate `%s', result `%s'."
7091            mid-or-mail regexp rate result)))
7092       (when (<= lpartlen 7)
7093         (setq result (+ result 5.0))
7094         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7095                       mid-or-mail result))
7096       (when (>= lpartlen 12)
7097         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7098         (cond
7099          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7100           ;; Long local part should contain realname if e-mail address,
7101           ;; too many digits: message-id.
7102           ;; $score -= 5.0 + 0.1 * $local_part_len;
7103           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7104           (setq result (+ result rate))
7105           (gnus-message
7106            9 "Many digits in `%s', rate `%s', result `%s'."
7107            mid-or-mail rate result))
7108          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7109                         mid-or-mail)
7110           ;; Too few vowels [^aeiouy]{4,}.*\@
7111           (setq result (+ result -5.0))
7112           (gnus-message
7113            9 "Few vowels in `%s', rate `%s', result `%s'."
7114            mid-or-mail -5.0 result))
7115          (t
7116           (setq result (+ result 5.0))
7117           (gnus-message
7118            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7119     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7120     ;; Maybe we should make this a customizable alist: (condition . 'result)
7121     (cond
7122      ((symbolp result) result)
7123      ;; Now convert number into proper results:
7124      ((< result -10.0) 'mid)
7125      ((> result  10.0) 'mail)
7126      (t 'ask))))
7127
7128 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7129   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7130          (url-mid (concat "news" ":" mid-or-mail))
7131          (url-mailto (concat "mailto" ":" mid-or-mail)))
7132     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7133     (when (fboundp pref)
7134       (setq guessed
7135             ;; get rid of surrounding angles...
7136             (funcall pref
7137                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7138       (if (or (eq 'mid guessed) (eq 'mail guessed))
7139           (setq pref guessed)
7140         (setq pref 'ask)))
7141     (if (eq pref 'ask)
7142         (save-window-excursion
7143           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7144               (setq pref 'mail)
7145             (setq pref 'mid))))
7146     (cond ((eq pref 'mid)
7147            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7148            (gnus-button-handle-news url-mid))
7149           ((eq pref 'mail)
7150            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7151            (gnus-url-mailto url-mailto))
7152           (t (gnus-message 3 "Invalid string.")))))
7153
7154 (defun gnus-button-handle-custom (fun arg)
7155   "Call function FUN on argument ARG.
7156 Both FUN and ARG are supposed to be strings.  ARG will be passed
7157 as a symbol to FUN."
7158   (funcall (intern fun)
7159            (if (string-match "^customize-apropos" fun)
7160                arg
7161              (intern arg))))
7162
7163 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7164
7165 ;; FIXME: Maybe we should merge some of the functions that do quite similar
7166 ;; stuff?
7167
7168 (defun gnus-button-handle-describe-function (url)
7169   "Call `describe-function' when pushing the corresponding URL button."
7170   (describe-function
7171    (intern
7172     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7173
7174 (defun gnus-button-handle-describe-variable (url)
7175   "Call `describe-variable' when pushing the corresponding URL button."
7176   (describe-variable
7177    (intern
7178     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7179
7180 (defun gnus-button-handle-symbol (url)
7181 "Display help on variable or function.
7182 Calls `describe-variable' or `describe-function'."
7183   (let ((sym (intern url)))
7184     (cond
7185      ((fboundp sym) (describe-function sym))
7186      ((boundp sym) (describe-variable sym))
7187      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7188
7189 (defun gnus-button-handle-describe-key (url)
7190   "Call `describe-key' when pushing the corresponding URL button."
7191   (let* ((key-string
7192           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7193          (keys (ignore-errors (eval `(kbd ,key-string)))))
7194     (if keys
7195         (describe-key keys)
7196       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7197
7198 (defun gnus-button-handle-apropos (url)
7199   "Call `apropos' when pushing the corresponding URL button."
7200   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7201
7202 (defun gnus-button-handle-apropos-command (url)
7203   "Call `apropos' when pushing the corresponding URL button."
7204   (apropos-command
7205    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7206
7207 (defun gnus-button-handle-apropos-variable (url)
7208   "Call `apropos' when pushing the corresponding URL button."
7209   (funcall
7210    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7211    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7212
7213 (defun gnus-button-handle-apropos-documentation (url)
7214   "Call `apropos' when pushing the corresponding URL button."
7215   (funcall
7216    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7217    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7218
7219 (defun gnus-button-handle-library (url)
7220   "Call `locate-library' when pushing the corresponding URL button."
7221   (gnus-message 9 "url=`%s'" url)
7222   (let* ((lib (locate-library url))
7223          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7224     (if (not lib)
7225         (gnus-message 1 "Cannot locale library `%s'." url)
7226       (find-file-read-only file))))
7227
7228 (defun gnus-button-handle-ctan (url)
7229   "Call `browse-url' when pushing a CTAN URL button."
7230   (funcall
7231    gnus-button-ctan-handler
7232    (concat
7233     gnus-ctan-url
7234     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
7235
7236 (defcustom gnus-button-tex-level 5
7237   "*Integer that says how many TeX-related buttons Gnus will show.
7238 The higher the number, the more buttons will appear and the more false
7239 positives are possible.  Note that you can set this variable local to
7240 specific groups.  Setting it higher in TeX groups is probably a good idea.
7241 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7242 how to set variables in specific groups."
7243   :version "22.1"
7244   :group 'gnus-article-buttons
7245   :link '(custom-manual "(gnus)Group Parameters")
7246   :type 'integer)
7247
7248 (defcustom gnus-button-man-level 5
7249   "*Integer that says how many man-related buttons Gnus will show.
7250 The higher the number, the more buttons will appear and the more false
7251 positives are possible.  Note that you can set this variable local to
7252 specific groups.  Setting it higher in Unix groups is probably a good idea.
7253 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7254 how to set variables in specific groups."
7255   :version "22.1"
7256   :group 'gnus-article-buttons
7257   :link '(custom-manual "(gnus)Group Parameters")
7258   :type 'integer)
7259
7260 (defcustom gnus-button-emacs-level 5
7261   "*Integer that says how many emacs-related buttons Gnus will show.
7262 The higher the number, the more buttons will appear and the more false
7263 positives are possible.  Note that you can set this variable local to
7264 specific groups.  Setting it higher in Emacs or Gnus related groups is
7265 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7266 `gnus-parameters' on how to set variables in specific groups."
7267   :version "22.1"
7268   :group 'gnus-article-buttons
7269   :link '(custom-manual "(gnus)Group Parameters")
7270   :type 'integer)
7271
7272 (defcustom gnus-button-message-level 5
7273   "*Integer that says how many buttons for news or mail messages will appear.
7274 The higher the number, the more buttons will appear and the more false
7275 positives are possible."
7276   ;; mail addresses, MIDs, URLs for news, ...
7277   :version "22.1"
7278   :group 'gnus-article-buttons
7279   :type 'integer)
7280
7281 (defcustom gnus-button-browse-level 5
7282   "*Integer that says how many buttons for browsing will appear.
7283 The higher the number, the more buttons will appear and the more false
7284 positives are possible."
7285   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7286   :version "22.1"
7287   :group 'gnus-article-buttons
7288   :type 'integer)
7289
7290 (defcustom gnus-button-alist
7291   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7292      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7293     ((concat "\\b\\(nntp\\|news\\):\\("
7294              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7295      0 t gnus-button-handle-news 2)
7296     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7297      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7298     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7299      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7300     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7301     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7302      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7303     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7304      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7305     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7306      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7307     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7308      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7309     ;; RFC 2368 (The mailto URL scheme)
7310     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7311      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7312     ("\\bmailto:\\([^ \n\t]+\\)"
7313      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7314     ;; CTAN
7315     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
7316              gnus-button-ctan-directory-regexp
7317              "[^][>)!;:,'\n\t ]+\\)")
7318      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
7319     ((concat "\\btex-archive/\\("
7320              gnus-button-ctan-directory-regexp
7321              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
7322      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
7323     ((concat
7324       "\\b\\("
7325       gnus-button-ctan-directory-regexp
7326       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
7327      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
7328     ;; This is info (home-grown style) <info://foo/bar+baz>
7329     ("\\binfo://\\([^'\">\n\t ]+\\)"
7330      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7331     ;; Info GNOME style <info:foo#bar_baz>
7332     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7333      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7334     ;; Info KDE style <info:(foo)bar baz>
7335     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7336      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7337     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7338      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7339     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
7340      ;; Info links like `C-h i d m CC Mode RET'
7341      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 2)
7342     ;; This is custom
7343     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
7344      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7345     ;; Emacs help commands
7346     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7347      ;; regexp doesn't match arguments containing ` '.
7348      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7349     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7350      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7351     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7352      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7353     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7354      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7355     ;; The following entries may lead to many false positives so don't enable
7356     ;; them by default (use a high button level).
7357     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7358      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7359      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7360     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7361      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7362     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7363      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7364     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7365      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7366     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7367      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7368     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7369      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7370     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7371      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7372     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7373      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7374     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7375      ;; Unlike the other regexps we really have to require quoting
7376      ;; here to determine where it ends.
7377      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7378     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7379     ("<URL: *\\([^\n<>]*\\)>"
7380      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7381     ;; RFC 2396 (2.4.3., delims) ...
7382     ("\"URL: *\\([^\n\"]*\\)\""
7383      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7384     ;; Raw URLs.
7385     (gnus-button-url-regexp
7386      0 (>= gnus-button-browse-level 0) browse-url 0)
7387     ;; man pages
7388     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7389      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7390      gnus-button-handle-man 1)
7391     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7392     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7393      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7394      gnus-button-handle-man 1)
7395     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7396     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7397     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7398      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7399     ;; Recognizing patches to .el files.  This is somewhat obscure,
7400     ;; but considering the percentage of Gnus users who hack Emacs
7401     ;; Lisp files...
7402     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7403      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7404     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7405      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7406     ;; MID or mail: To avoid too many false positives we don't try to catch
7407     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7408     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7409     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7410     ;; so that non-ambiguous entries (see above) match first.
7411     (gnus-button-mid-or-mail-regexp
7412      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7413   "*Alist of regexps matching buttons in article bodies.
7414
7415 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7416 REGEXP: is the string (case insensitive) matching text around the button (can
7417 also be Lisp expression evaluating to a string),
7418 BUTTON: is the number of the regexp grouping actually matching the button,
7419 FORM: is a Lisp expression which must eval to true for the button to
7420 be added,
7421 CALLBACK: is the function to call when the user push this button, and each
7422 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7423
7424 CALLBACK can also be a variable, in that case the value of that
7425 variable it the real callback function."
7426   :group 'gnus-article-buttons
7427   :type '(repeat (list (choice regexp variable sexp)
7428                        (integer :tag "Button")
7429                        (sexp :tag "Form")
7430                        (function :tag "Callback")
7431                        (repeat :tag "Par"
7432                                :inline t
7433                                (integer :tag "Regexp group")))))
7434 (put 'gnus-button-alist 'risky-local-variable t)
7435
7436 (defcustom gnus-header-button-alist
7437   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7438      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7439     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7440      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7441     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7442      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7443     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7444      0 (>= gnus-button-browse-level 0) browse-url 0)
7445     ("^Subject:" gnus-button-url-regexp
7446      0 (>= gnus-button-browse-level 0) browse-url 0)
7447     ("^[^:]+:" gnus-button-url-regexp
7448      0 (>= gnus-button-browse-level 0) browse-url 0)
7449     ("^OpenPGP:.*url=" gnus-button-url-regexp
7450      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7451     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7452      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7453     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7454      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7455   "*Alist of headers and regexps to match buttons in article heads.
7456
7457 This alist is very similar to `gnus-button-alist', except that each
7458 alist has an additional HEADER element first in each entry:
7459
7460 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7461
7462 HEADER is a regexp to match a header.  For a fuller explanation, see
7463 `gnus-button-alist'."
7464   :group 'gnus-article-buttons
7465   :group 'gnus-article-headers
7466   :type '(repeat (list (regexp :tag "Header")
7467                        (choice regexp variable)
7468                        (integer :tag "Button")
7469                        (sexp :tag "Form")
7470                        (function :tag "Callback")
7471                        (repeat :tag "Par"
7472                                :inline t
7473                                (integer :tag "Regexp group")))))
7474 (put 'gnus-header-button-alist 'risky-local-variable t)
7475
7476 ;;; Commands:
7477
7478 (defun gnus-article-push-button (event)
7479   "Check text under the mouse pointer for a callback function.
7480 If the text under the mouse pointer has a `gnus-callback' property,
7481 call it with the value of the `gnus-data' text property."
7482   (interactive "e")
7483   (set-buffer (window-buffer (posn-window (event-start event))))
7484   (let* ((pos (posn-point (event-start event)))
7485          (data (get-text-property pos 'gnus-data))
7486          (fun (get-text-property pos 'gnus-callback)))
7487     (goto-char pos)
7488     (when fun
7489       (funcall fun data))))
7490
7491 (defun gnus-article-press-button ()
7492   "Check text at point for a callback function.
7493 If the text at point has a `gnus-callback' property,
7494 call it with the value of the `gnus-data' text property."
7495   (interactive)
7496   (let ((data (get-text-property (point) 'gnus-data))
7497         (fun (get-text-property (point) 'gnus-callback)))
7498     (when fun
7499       (funcall fun data))))
7500
7501 (defun gnus-article-highlight (&optional force)
7502   "Highlight current article.
7503 This function calls `gnus-article-highlight-headers',
7504 `gnus-article-highlight-citation',
7505 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7506 do the highlighting.  See the documentation for those functions."
7507   (interactive (list 'force))
7508   (gnus-article-highlight-headers)
7509   (gnus-article-highlight-citation force)
7510   (gnus-article-highlight-signature)
7511   (gnus-article-add-buttons force)
7512   (gnus-article-add-buttons-to-head))
7513
7514 (defun gnus-article-highlight-some (&optional force)
7515   "Highlight current article.
7516 This function calls `gnus-article-highlight-headers',
7517 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7518 do the highlighting.  See the documentation for those functions."
7519   (interactive (list 'force))
7520   (gnus-article-highlight-headers)
7521   (gnus-article-highlight-signature)
7522   (gnus-article-add-buttons))
7523
7524 (defun gnus-article-highlight-headers ()
7525   "Highlight article headers as specified by `gnus-header-face-alist'."
7526   (interactive)
7527   (gnus-with-article-headers
7528     (let (regexp header-face field-face from hpoints fpoints)
7529       (dolist (entry gnus-header-face-alist)
7530         (goto-char (point-min))
7531         (setq regexp (concat "^\\("
7532                              (if (string-equal "" (nth 0 entry))
7533                                  "[^\t ]"
7534                                (nth 0 entry))
7535                              "\\)")
7536               header-face (nth 1 entry)
7537               field-face (nth 2 entry))
7538         (while (and (re-search-forward regexp nil t)
7539                     (not (eobp)))
7540           (beginning-of-line)
7541           (setq from (point))
7542           (unless (search-forward ":" nil t)
7543             (forward-char 1))
7544           (when (and header-face
7545                      (not (memq (point) hpoints)))
7546             (push (point) hpoints)
7547             (gnus-put-text-property from (point) 'face header-face))
7548           (when (and field-face
7549                      (not (memq (setq from (point)) fpoints)))
7550             (push from fpoints)
7551             (if (re-search-forward "^[^ \t]" nil t)
7552                 (forward-char -2)
7553               (goto-char (point-max)))
7554             (gnus-put-text-property from (point) 'face field-face)))))))
7555
7556 (defun gnus-article-highlight-signature ()
7557   "Highlight the signature in an article.
7558 It does this by highlighting everything after
7559 `gnus-signature-separator' using the face `gnus-signature'."
7560   (interactive)
7561   (gnus-with-article-buffer
7562     (let ((inhibit-point-motion-hooks t))
7563       (save-restriction
7564         (when (and gnus-signature-face
7565                    (gnus-article-narrow-to-signature))
7566           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7567                             'face gnus-signature-face)
7568           (widen)
7569           (gnus-article-search-signature)
7570           (let ((start (match-beginning 0))
7571                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7572             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7573                                      end)))))))
7574
7575 (defun gnus-button-in-region-p (b e prop)
7576   "Say whether PROP exists in the region."
7577   (text-property-not-all b e prop nil))
7578
7579 (defun gnus-article-add-buttons (&optional force)
7580   "Find external references in the article and make buttons of them.
7581 \"External references\" are things like Message-IDs and URLs, as
7582 specified by `gnus-button-alist'."
7583   (interactive (list 'force))
7584   (gnus-with-article-buffer
7585     (let ((inhibit-point-motion-hooks t)
7586           (case-fold-search t)
7587           (alist gnus-button-alist)
7588           beg entry regexp)
7589       ;; Remove all old markers.
7590       (let (marker entry new-list)
7591         (while (setq marker (pop gnus-button-marker-list))
7592           (if (or (< marker (point-min)) (>= marker (point-max)))
7593               (push marker new-list)
7594             (goto-char marker)
7595             (when (setq entry (gnus-button-entry))
7596               (put-text-property (match-beginning (nth 1 entry))
7597                                  (match-end (nth 1 entry))
7598                                  'gnus-callback nil))
7599             (set-marker marker nil)))
7600         (setq gnus-button-marker-list new-list))
7601       ;; We skip the headers.
7602       (article-goto-body)
7603       (setq beg (point))
7604       (while (setq entry (pop alist))
7605         (setq regexp (eval (car entry)))
7606         (goto-char beg)
7607         (while (re-search-forward regexp nil t)
7608           (let ((start (match-beginning (nth 1 entry)))
7609                 (end (match-end (nth 1 entry)))
7610                 (from (match-beginning 0)))
7611             (when (and (or (eq t (nth 2 entry))
7612                            (eval (nth 2 entry)))
7613                        (not (gnus-button-in-region-p
7614                              start end 'gnus-callback)))
7615               ;; That optional form returned non-nil, so we add the
7616               ;; button.
7617               (setq from (set-marker (make-marker) from))
7618               (push from gnus-button-marker-list)
7619               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7620                            (gnus-article-extend-url-button from start end))
7621                 (gnus-article-add-button start end
7622                                          'gnus-button-push from)))))))))
7623
7624 (defun gnus-article-extend-url-button (beg start end)
7625   "Extend url button if url is folded into two or more lines.
7626 Return non-nil if button is extended.  BEG is a marker that points to
7627 the beginning position of a text containing url.  START and END are
7628 the endpoints of a url button before it is extended.  The concatenated
7629 url is put as the `gnus-button-url' overlay property on the button."
7630   (let ((opoint (point))
7631         (points (list start end))
7632         url delim regexp)
7633     (prog1
7634         (when (and (progn
7635                      (goto-char end)
7636                      (not (looking-at "[\t ]*[\">]")))
7637                    (progn
7638                      (goto-char start)
7639                      (string-match
7640                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7641                       (buffer-substring (point-at-bol) start)))
7642                    (progn
7643                      (setq url (list (buffer-substring start end))
7644                            delim (if (match-beginning 1) ">" "\""))
7645                      (beginning-of-line)
7646                      (setq regexp (concat
7647                                    (when (and (looking-at
7648                                                message-cite-prefix-regexp)
7649                                               (< (match-end 0) start))
7650                                      (regexp-quote (match-string 0)))
7651                                    "\
7652 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7653                                    delim "\\)"))
7654                      (while (progn
7655                               (forward-line 1)
7656                               (and (looking-at regexp)
7657                                    (prog1
7658                                        (match-beginning 1)
7659                                      (push (or (match-string 2)
7660                                                (match-string 1))
7661                                            url)
7662                                      (push (setq end (or (match-end 2)
7663                                                          (match-end 1)))
7664                                            points)
7665                                      (push (or (match-beginning 2)
7666                                                (match-beginning 1))
7667                                            points)))))
7668                      (match-beginning 2)))
7669           (let (gnus-article-mouse-face widget-mouse-face)
7670             (while points
7671               (gnus-article-add-button (pop points) (pop points)
7672                                        'gnus-button-push beg)))
7673           (let ((overlay (gnus-make-overlay start end)))
7674             (gnus-overlay-put overlay 'evaporate t)
7675             (gnus-overlay-put overlay 'gnus-button-url
7676                               (list (mapconcat 'identity (nreverse url) "")))
7677             (when gnus-article-mouse-face
7678               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7679           t)
7680       (goto-char opoint))))
7681
7682 ;; Add buttons to the head of an article.
7683 (defun gnus-article-add-buttons-to-head ()
7684   "Add buttons to the head of the article."
7685   (interactive)
7686   (gnus-with-article-headers
7687     (let (beg end)
7688       (dolist (entry gnus-header-button-alist)
7689         ;; Each alist entry.
7690         (goto-char (point-min))
7691         (while (re-search-forward (car entry) nil t)
7692           ;; Each header matching the entry.
7693           (setq beg (match-beginning 0))
7694           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7695                              (match-beginning 0))
7696                         (point-max)))
7697           (goto-char beg)
7698           (while (re-search-forward (eval (nth 1 entry)) end t)
7699             ;; Each match within a header.
7700             (let* ((entry (cdr entry))
7701                    (start (match-beginning (nth 1 entry)))
7702                    (end (match-end (nth 1 entry)))
7703                    (form (nth 2 entry)))
7704               (goto-char (match-end 0))
7705               (when (eval form)
7706                 (gnus-article-add-button
7707                  start end (nth 3 entry)
7708                  (buffer-substring (match-beginning (nth 4 entry))
7709                                    (match-end (nth 4 entry)))))))
7710           (goto-char end))))))
7711
7712 ;;; External functions:
7713
7714 (defun gnus-article-add-button (from to fun &optional data)
7715   "Create a button between FROM and TO with callback FUN and data DATA."
7716   (when gnus-article-button-face
7717     (gnus-overlay-put (gnus-make-overlay from to nil t)
7718                       'face gnus-article-button-face))
7719   (gnus-add-text-properties
7720    from to
7721    (nconc (and gnus-article-mouse-face
7722                (list gnus-mouse-face-prop gnus-article-mouse-face))
7723           (list 'gnus-callback fun)
7724           (and data (list 'gnus-data data))))
7725   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7726                          :button-keymap gnus-widget-button-keymap))
7727
7728 ;;; Internal functions:
7729
7730 (defun gnus-article-set-globals ()
7731   (with-current-buffer gnus-summary-buffer
7732     (gnus-set-global-variables)))
7733
7734 (defun gnus-signature-toggle (end)
7735   (gnus-with-article-buffer
7736     (let ((inhibit-point-motion-hooks t))
7737       (if (text-property-any end (point-max) 'article-type 'signature)
7738           (progn
7739             (gnus-delete-wash-type 'signature)
7740             (gnus-remove-text-properties-when
7741              'article-type 'signature end (point-max)
7742              (cons 'article-type (cons 'signature
7743                                        gnus-hidden-properties))))
7744         (gnus-add-wash-type 'signature)
7745         (gnus-add-text-properties-when
7746          'article-type nil end (point-max)
7747          (cons 'article-type (cons 'signature
7748                                    gnus-hidden-properties)))))
7749     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7750       (gnus-set-mode-line 'article))))
7751
7752 (defun gnus-button-entry ()
7753   ;; Return the first entry in `gnus-button-alist' matching this place.
7754   (let ((alist gnus-button-alist)
7755         (entry nil))
7756     (while alist
7757       (setq entry (pop alist))
7758       (if (looking-at (eval (car entry)))
7759           (setq alist nil)
7760         (setq entry nil)))
7761     entry))
7762
7763 (defun gnus-button-push (marker)
7764   ;; Push button starting at MARKER.
7765   (save-excursion
7766     (goto-char marker)
7767     (let* ((entry (gnus-button-entry))
7768            (inhibit-point-motion-hooks t)
7769            (fun (nth 3 entry))
7770            (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7771                           (get-char-property marker 'gnus-button-url))
7772                      (mapcar (lambda (group)
7773                                (let ((string (match-string group)))
7774                                  (set-text-properties
7775                                   0 (length string) nil string)
7776                                  string))
7777                              (nthcdr 4 entry)))))
7778       (cond
7779        ((fboundp fun)
7780         (apply fun args))
7781        ((and (boundp fun)
7782              (fboundp (symbol-value fun)))
7783         (apply (symbol-value fun) args))
7784        (t
7785         (gnus-message 1 "You must define `%S' to use this button"
7786                       (cons fun args)))))))
7787
7788 (defun gnus-parse-news-url (url)
7789   (let (scheme server port group message-id articles)
7790     (with-temp-buffer
7791       (insert url)
7792       (goto-char (point-min))
7793       (when (looking-at "\\([A-Za-z]+\\):")
7794         (setq scheme (match-string 1))
7795         (goto-char (match-end 0)))
7796       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7797         (setq server (match-string 1))
7798         (setq port (if (stringp (match-string 3))
7799                        (string-to-number (match-string 3))
7800                      (match-string 3)))
7801         (goto-char (match-end 0)))
7802
7803       (cond
7804        ((looking-at "\\(.*@.*\\)")
7805         (setq message-id (match-string 1)))
7806        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7807         (setq group (match-string 1)
7808               articles (split-string (match-string 2) "-")))
7809        ((looking-at "\\([^/]+\\)/?")
7810         (setq group (match-string 1)))
7811        (t
7812         (error "Unknown news URL syntax"))))
7813     (list scheme server port group message-id articles)))
7814
7815 (defun gnus-button-handle-news (url)
7816   "Fetch a news URL."
7817   (destructuring-bind (scheme server port group message-id articles)
7818       (gnus-parse-news-url url)
7819     (cond
7820      (message-id
7821       (save-excursion
7822         (set-buffer gnus-summary-buffer)
7823         (if server
7824             (let ((gnus-refer-article-method
7825                    (nconc (list (list 'nntp server))
7826                           gnus-refer-article-method))
7827                   (nntp-port-number (or port "nntp")))
7828               (gnus-message 7 "Fetching %s with %s"
7829                             message-id gnus-refer-article-method)
7830               (gnus-summary-refer-article message-id))
7831           (gnus-summary-refer-article message-id))))
7832      (group
7833       (gnus-button-fetch-group url)))))
7834
7835 (defun gnus-button-patch (library line)
7836   "Visit an Emacs Lisp library LIBRARY on line LINE."
7837   (interactive)
7838   (let ((file (locate-library (file-name-nondirectory library))))
7839     (unless file
7840       (error "Couldn't find library %s" library))
7841     (find-file file)
7842     (goto-line (string-to-number line))))
7843
7844 (defun gnus-button-handle-man (url)
7845   "Fetch a man page."
7846   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7847   (when (eq gnus-button-man-handler 'woman)
7848     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
7849   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7850   (funcall gnus-button-man-handler url))
7851
7852 (defun gnus-button-handle-info-url (url)
7853   "Fetch an info URL."
7854   (setq url (mm-subst-char-in-string ?+ ?\  url))
7855   (cond
7856    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
7857     (gnus-info-find-node
7858      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
7859                      "Gnus")
7860              ")" (gnus-url-unhex-string (match-string 2 url)))))
7861    ((string-match "([^)\"]+)[^\"]+" url)
7862     (setq url
7863           (gnus-replace-in-string
7864            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
7865     (gnus-info-find-node url))
7866    (t (error "Can't parse %s" url))))
7867
7868 (defun gnus-button-handle-info-url-gnome (url)
7869   "Fetch GNOME style info URL."
7870   (setq url (mm-subst-char-in-string ?_ ?\  url))
7871   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
7872       (gnus-info-find-node
7873        (concat "("
7874                (gnus-url-unhex-string
7875                  (match-string 1 url))
7876                ")"
7877                (or (gnus-url-unhex-string
7878                     (match-string 2 url))
7879                    "Top")))
7880     (error "Can't parse %s" url)))
7881
7882 (defun gnus-button-handle-info-url-kde (url)
7883   "Fetch KDE style info URL."
7884   (gnus-info-find-node (gnus-url-unhex-string url)))
7885
7886 ;; (info) will autoload info.el
7887 (declare-function Info-menu "info" (menu-item &optional fork))
7888
7889 (defun gnus-button-handle-info-keystrokes (url)
7890   "Call `info' when pushing the corresponding URL button."
7891   ;; For links like `C-h i d m gnus RET', `C-h i d m CC Mode RET'.
7892   (info)
7893   (Info-directory)
7894   (Info-menu url))
7895
7896 (defun gnus-button-openpgp (url)
7897   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
7898   (with-temp-buffer
7899     (mm-url-insert-file-contents-external url)
7900     (pgg-snarf-keys-region (point-min) (point-max))
7901     (pgg-display-output-buffer nil nil nil)))
7902
7903 (defun gnus-button-message-id (message-id)
7904   "Fetch MESSAGE-ID."
7905   (with-current-buffer gnus-summary-buffer
7906     (gnus-summary-refer-article message-id)))
7907
7908 (defun gnus-button-fetch-group (address &rest ignore)
7909   "Fetch GROUP specified by ADDRESS."
7910   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
7911                       address)
7912     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
7913     ;; for nntp:// and news://
7914     (setq address (match-string 3 address)))
7915   (if (not (string-match "[:/]" address))
7916       ;; This is just a simple group url.
7917       (gnus-group-read-ephemeral-group address gnus-select-method)
7918     (if (not
7919          (string-match
7920           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
7921           address))
7922         (error "Can't parse %s" address)
7923       (gnus-group-read-ephemeral-group
7924        (match-string 4 address)
7925        `(nntp ,(match-string 1 address)
7926               (nntp-address ,(match-string 1 address))
7927               (nntp-port-number ,(if (match-end 3)
7928                                      (match-string 3 address)
7929                                    "nntp")))
7930        nil nil nil
7931        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
7932
7933 (defun gnus-url-parse-query-string (query &optional downcase)
7934   (let (retval pairs cur key val)
7935     (setq pairs (split-string query "&"))
7936     (while pairs
7937       (setq cur (car pairs)
7938             pairs (cdr pairs))
7939       (if (not (string-match "=" cur))
7940           nil                           ; Grace
7941         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
7942               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
7943         (if downcase
7944             (setq key (downcase key)))
7945         (setq cur (assoc key retval))
7946         (if cur
7947             (setcdr cur (cons val (cdr cur)))
7948           (setq retval (cons (list key val) retval)))))
7949     retval))
7950
7951 (defun gnus-url-mailto (url)
7952   ;; Send mail to someone
7953   (when (string-match "mailto:/*\\(.*\\)" url)
7954     (setq url (substring url (match-beginning 1) nil)))
7955   (let (to args subject func)
7956     (setq args (gnus-url-parse-query-string
7957                 (if (string-match "^\\?" url)
7958                     (substring url 1)
7959                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
7960                       (concat "to=" (match-string 1 url) "&"
7961                               (match-string 2 url))
7962                     (concat "to=" url)))
7963                 t)
7964           subject (cdr-safe (assoc "subject" args)))
7965     (gnus-msg-mail)
7966     (while args
7967       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
7968       (if (fboundp func)
7969           (funcall func)
7970         (message-position-on-field (caar args)))
7971       (insert (gnus-replace-in-string
7972                (mapconcat 'identity (reverse (cdar args)) ", ")
7973                "\r\n" "\n" t))
7974       (setq args (cdr args)))
7975     (if subject
7976         (message-goto-body)
7977       (message-goto-subject))))
7978
7979 (defun gnus-button-embedded-url (address)
7980   "Activate ADDRESS with `browse-url'."
7981   (browse-url (gnus-strip-whitespace address)))
7982
7983 ;;; Next/prev buttons in the article buffer.
7984
7985 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
7986 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
7987
7988 (defvar gnus-prev-page-map
7989   (let ((map (make-sparse-keymap)))
7990     (define-key map gnus-mouse-2 'gnus-button-prev-page)
7991     (define-key map "\r" 'gnus-button-prev-page)
7992     map))
7993
7994 (defvar gnus-next-page-map
7995   (let ((map (make-sparse-keymap)))
7996     (unless (>= emacs-major-version 21)
7997       ;; XEmacs doesn't care.
7998       (set-keymap-parent map gnus-article-mode-map))
7999     (define-key map gnus-mouse-2 'gnus-button-next-page)
8000     (define-key map "\r" 'gnus-button-next-page)
8001     map))
8002
8003 (defun gnus-insert-prev-page-button ()
8004   (let ((b (point)) e
8005         (inhibit-read-only t))
8006     (gnus-eval-format
8007      gnus-prev-page-line-format nil
8008      `(keymap ,gnus-prev-page-map
8009               gnus-prev t
8010               gnus-callback gnus-article-button-prev-page
8011               article-type annotation))
8012     (setq e (if (bolp)
8013                 ;; Exclude a newline.
8014                 (1- (point))
8015               (point)))
8016     (when gnus-article-button-face
8017       (gnus-overlay-put (gnus-make-overlay b e nil t)
8018                         'face gnus-article-button-face))
8019     (widget-convert-button
8020      'link b e
8021      :action 'gnus-button-prev-page
8022      :button-keymap gnus-prev-page-map)))
8023
8024 (defun gnus-button-next-page (&optional args more-args)
8025   "Go to the next page."
8026   (interactive)
8027   (let ((win (selected-window)))
8028     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8029     (gnus-article-next-page)
8030     (select-window win)))
8031
8032 (defun gnus-button-prev-page (&optional args more-args)
8033   "Go to the prev page."
8034   (interactive)
8035   (let ((win (selected-window)))
8036     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8037     (gnus-article-prev-page)
8038     (select-window win)))
8039
8040 (defun gnus-insert-next-page-button ()
8041   (let ((b (point)) e
8042         (inhibit-read-only t))
8043     (gnus-eval-format gnus-next-page-line-format nil
8044                       `(keymap ,gnus-next-page-map
8045                                gnus-next t
8046                                gnus-callback gnus-article-button-next-page
8047                                article-type annotation))
8048     (setq e (if (bolp)
8049                 ;; Exclude a newline.
8050                 (1- (point))
8051               (point)))
8052     (when gnus-article-button-face
8053       (gnus-overlay-put (gnus-make-overlay b e nil t)
8054                         'face gnus-article-button-face))
8055     (widget-convert-button
8056      'link b e
8057      :action 'gnus-button-next-page
8058      :button-keymap gnus-next-page-map)))
8059
8060 (defun gnus-article-button-next-page (arg)
8061   "Go to the next page."
8062   (interactive "P")
8063   (let ((win (selected-window)))
8064     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8065     (gnus-article-next-page)
8066     (select-window win)))
8067
8068 (defun gnus-article-button-prev-page (arg)
8069   "Go to the prev page."
8070   (interactive "P")
8071   (let ((win (selected-window)))
8072     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8073     (gnus-article-prev-page)
8074     (select-window win)))
8075
8076 (defvar gnus-decode-header-methods
8077   '(mail-decode-encoded-word-region)
8078   "List of methods used to decode headers.
8079
8080 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8081 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8082 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8083 whose names match REGEXP.
8084
8085 For example:
8086 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8087  mail-decode-encoded-word-region
8088  (\"chinese\" . rfc1843-decode-region))
8089 ")
8090
8091 (defvar gnus-decode-header-methods-cache nil)
8092
8093 (defun gnus-multi-decode-header (start end)
8094   "Apply the functions from `gnus-encoded-word-methods' that match."
8095   (unless (and gnus-decode-header-methods-cache
8096                (eq gnus-newsgroup-name
8097                    (car gnus-decode-header-methods-cache)))
8098     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8099     (dolist (x gnus-decode-header-methods)
8100       (if (symbolp x)
8101           (nconc gnus-decode-header-methods-cache (list x))
8102         (if (and gnus-newsgroup-name
8103                  (string-match (car x) gnus-newsgroup-name))
8104             (nconc gnus-decode-header-methods-cache
8105                    (list (cdr x)))))))
8106   (let ((xlist gnus-decode-header-methods-cache))
8107     (pop xlist)
8108     (save-restriction
8109       (narrow-to-region start end)
8110       (while xlist
8111         (funcall (pop xlist) (point-min) (point-max))))))
8112
8113 ;;;
8114 ;;; Treatment top-level handling.
8115 ;;;
8116
8117 (defun gnus-treat-article (condition &optional part-number total-parts type)
8118   (let ((length (- (point-max) (point-min)))
8119         (alist gnus-treatment-function-alist)
8120         (article-goto-body-goes-to-point-min-p t)
8121         (treated-type
8122          (or (not type)
8123              (catch 'found
8124                (let ((list gnus-article-treat-types))
8125                  (while list
8126                    (when (string-match (pop list) type)
8127                      (throw 'found t)))))))
8128         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8129         val elem)
8130     (gnus-run-hooks 'gnus-part-display-hook)
8131     (dolist (elem alist)
8132       (setq val
8133             (save-excursion
8134               (when (gnus-buffer-live-p gnus-summary-buffer)
8135                 (set-buffer gnus-summary-buffer))
8136               (symbol-value (car elem))))
8137       (when (and (or (consp val)
8138                      treated-type)
8139                  (gnus-treat-predicate val)
8140                  (or (not (get (car elem) 'highlight))
8141                      highlightp))
8142         (save-restriction
8143           (funcall (cadr elem)))))))
8144
8145 ;; Dynamic variables.
8146 (defvar part-number)
8147 (defvar total-parts)
8148 (defvar type)
8149 (defvar condition)
8150 (defvar length)
8151
8152 (defun gnus-treat-predicate (val)
8153   (cond
8154    ((null val)
8155     nil)
8156    (condition
8157     (eq condition val))
8158    ((and (listp val)
8159          (stringp (car val)))
8160     (apply 'gnus-or (mapcar `(lambda (s)
8161                                (string-match s ,(or gnus-newsgroup-name "")))
8162                             val)))
8163    ((listp val)
8164     (let ((pred (pop val)))
8165       (cond
8166        ((eq pred 'or)
8167         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8168        ((eq pred 'and)
8169         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8170        ((eq pred 'not)
8171         (not (gnus-treat-predicate (car val))))
8172        ((eq pred 'typep)
8173         (equal (car val) type))
8174        (t
8175         (error "%S is not a valid predicate" pred)))))
8176    ((eq val t)
8177     t)
8178    ((eq val 'head)
8179     nil)
8180    ((eq val 'first)
8181     (eq part-number 1))
8182    ((eq val 'last)
8183     (eq part-number total-parts))
8184    ((numberp val)
8185     (< length val))
8186    (t
8187     (error "%S is not a valid value" val))))
8188
8189 (defun gnus-article-encrypt-body (protocol &optional n)
8190   "Encrypt the article body."
8191   (interactive
8192    (list
8193     (or gnus-article-encrypt-protocol
8194         (completing-read "Encrypt protocol: "
8195                          gnus-article-encrypt-protocol-alist
8196                          nil t))
8197     current-prefix-arg))
8198   ;; User might hit `K E' instead of `K e', so prompt once.
8199   (when (and gnus-article-encrypt-protocol
8200              gnus-novice-user)
8201     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8202       (error "Encrypt aborted.")))
8203   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8204     (unless func
8205       (error "Can't find the encrypt protocol %s" protocol))
8206     (if (member gnus-newsgroup-name '("nndraft:delayed"
8207                                       "nndraft:drafts"
8208                                       "nndraft:queue"))
8209         (error "Can't encrypt the article in group %s"
8210                gnus-newsgroup-name))
8211     (gnus-summary-iterate n
8212       (save-excursion
8213         (set-buffer gnus-summary-buffer)
8214         (let ((mail-parse-charset gnus-newsgroup-charset)
8215               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8216               (summary-buffer gnus-summary-buffer)
8217               references point)
8218           (gnus-set-global-variables)
8219           (when (gnus-group-read-only-p)
8220             (error "The current newsgroup does not support article encrypt"))
8221           (gnus-summary-show-article t)
8222           (setq references
8223               (or (mail-header-references gnus-current-headers) ""))
8224           (set-buffer gnus-article-buffer)
8225           (let* ((inhibit-read-only t)
8226                  (headers
8227                   (mapcar (lambda (field)
8228                             (and (save-restriction
8229                                    (message-narrow-to-head)
8230                                    (goto-char (point-min))
8231                                    (search-forward field nil t))
8232                                  (prog2
8233                                      (message-narrow-to-field)
8234                                      (buffer-string)
8235                                    (delete-region (point-min) (point-max))
8236                                    (widen))))
8237                           '("Content-Type:" "Content-Transfer-Encoding:"
8238                             "Content-Disposition:"))))
8239             (message-narrow-to-head)
8240             (message-remove-header "MIME-Version")
8241             (goto-char (point-max))
8242             (setq point (point))
8243             (insert (apply 'concat headers))
8244             (widen)
8245             (narrow-to-region point (point-max))
8246             (let ((message-options message-options))
8247               (message-options-set 'message-sender user-mail-address)
8248               (message-options-set 'message-recipients user-mail-address)
8249               (message-options-set 'message-sign-encrypt 'not)
8250               (funcall func))
8251             (goto-char (point-min))
8252             (insert "MIME-Version: 1.0\n")
8253             (widen)
8254             (gnus-summary-edit-article-done
8255              references nil summary-buffer t))
8256           (when gnus-keep-backlog
8257             (gnus-backlog-remove-article
8258              (car gnus-article-current) (cdr gnus-article-current)))
8259           (save-excursion
8260             (when (get-buffer gnus-original-article-buffer)
8261               (set-buffer gnus-original-article-buffer)
8262               (setq gnus-original-article nil)))
8263           (when gnus-use-cache
8264             (gnus-cache-update-article
8265              (car gnus-article-current) (cdr gnus-article-current))))))))
8266
8267 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8268   "The following specs can be used:
8269 %t  The security MIME type
8270 %i  Additional info
8271 %d  Details
8272 %D  Details if button is pressed")
8273
8274 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8275   "The following specs can be used:
8276 %t  The security MIME type
8277 %i  Additional info
8278 %d  Details
8279 %D  Details if button is pressed")
8280
8281 (defvar gnus-mime-security-button-line-format-alist
8282   '((?t gnus-tmp-type ?s)
8283     (?i gnus-tmp-info ?s)
8284     (?d gnus-tmp-details ?s)
8285     (?D gnus-tmp-pressed-details ?s)))
8286
8287 (defvar gnus-mime-security-button-commands
8288   '((gnus-article-press-button "\r" "Show Detail")
8289     (undefined "v")
8290     (undefined "t")
8291     (undefined "C")
8292     (gnus-mime-security-save-part "o" "Save...")
8293     (undefined "\C-o")
8294     (undefined "r")
8295     (undefined "d")
8296     (undefined "c")
8297     (undefined "i")
8298     (undefined "E")
8299     (undefined "e")
8300     (undefined "p")
8301     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8302     (undefined ".")))
8303
8304 (defvar gnus-mime-security-button-map
8305   (let ((map (make-sparse-keymap)))
8306     (define-key map gnus-mouse-2 'gnus-article-push-button)
8307     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8308     (dolist (c gnus-mime-security-button-commands)
8309       (define-key map (cadr c) (car c)))
8310     map))
8311
8312 (easy-menu-define
8313   gnus-mime-security-button-menu gnus-mime-security-button-map
8314   "Security button menu."
8315   `("Security Part"
8316     ,@(delq nil
8317             (mapcar (lambda (c)
8318                       (unless (eq (car c) 'undefined)
8319                         (vector (caddr c) (car c) :active t)))
8320                     gnus-mime-security-button-commands))))
8321
8322 (defun gnus-mime-security-button-menu (event prefix)
8323   "Construct a context-sensitive menu of security commands."
8324   (interactive "e\nP")
8325   (save-window-excursion
8326     (let ((pos (event-start event)))
8327       (select-window (posn-window pos))
8328       (goto-char (posn-point pos))
8329       (gnus-article-check-buffer)
8330       (popup-menu gnus-mime-security-button-menu nil prefix))))
8331
8332 (defvar gnus-mime-security-details-buffer nil)
8333
8334 (defvar gnus-mime-security-button-pressed nil)
8335
8336 (defvar gnus-mime-security-show-details-inline t
8337   "If non-nil, show details in the article buffer.")
8338
8339 (defun gnus-mime-security-verify-or-decrypt (handle)
8340   (mm-remove-parts (cdr handle))
8341   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8342         point (inhibit-read-only t))
8343     (if region
8344         (goto-char (car region)))
8345     (setq point (point))
8346     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8347       (let* ((mm-verify-option 'known)
8348              (mm-decrypt-option 'known)
8349              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8350         (unless (eq nparts (cdr handle))
8351           (mm-destroy-parts (cdr handle))
8352           (setcdr handle nparts))))
8353     (gnus-mime-display-security handle)
8354     (when region
8355       (delete-region (point) (cdr region))
8356       (set-marker (car region) nil)
8357       (set-marker (cdr region) nil))
8358     (goto-char point)))
8359
8360 (defun gnus-mime-security-show-details (handle)
8361   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8362     (if (not details)
8363         (gnus-message 5 "No details.")
8364       (if gnus-mime-security-show-details-inline
8365           (let ((gnus-mime-security-button-pressed
8366                  (not (get-text-property (point) 'gnus-mime-details)))
8367                 (gnus-mime-security-button-line-format
8368                  (get-text-property (point) 'gnus-line-format))
8369                 (inhibit-read-only t))
8370             (forward-char -1)
8371             (while (eq (get-text-property (point) 'gnus-line-format)
8372                        gnus-mime-security-button-line-format)
8373               (forward-char -1))
8374             (forward-char)
8375             (save-restriction
8376               (narrow-to-region (point) (point))
8377               (gnus-insert-mime-security-button handle))
8378             (delete-region (point)
8379                            (or (text-property-not-all
8380                                 (point) (point-max)
8381                                 'gnus-line-format
8382                                 gnus-mime-security-button-line-format)
8383                                (point-max))))
8384         ;; Not inlined.
8385         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8386             (with-current-buffer gnus-mime-security-details-buffer
8387               (erase-buffer)
8388               t)
8389           (setq gnus-mime-security-details-buffer
8390                 (gnus-get-buffer-create "*MIME Security Details*")))
8391         (with-current-buffer gnus-mime-security-details-buffer
8392           (insert details)
8393           (goto-char (point-min)))
8394         (pop-to-buffer gnus-mime-security-details-buffer)))))
8395
8396 (defun gnus-mime-security-press-button (handle)
8397   (save-excursion
8398     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8399         (gnus-mime-security-show-details handle)
8400       (gnus-mime-security-verify-or-decrypt handle))))
8401
8402 (defun gnus-insert-mime-security-button (handle &optional displayed)
8403   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8404          (gnus-tmp-type
8405           (concat
8406            (or (nth 2 (assoc protocol mm-verify-function-alist))
8407                (nth 2 (assoc protocol mm-decrypt-function-alist))
8408                "Unknown")
8409            (if (equal (car handle) "multipart/signed")
8410                " Signed" " Encrypted")
8411            " Part"))
8412          (gnus-tmp-info
8413           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8414               "Undecided"))
8415          (gnus-tmp-details
8416           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8417          gnus-tmp-pressed-details
8418          b e)
8419     (setq gnus-tmp-details
8420           (if gnus-tmp-details
8421               (concat "\n" gnus-tmp-details)
8422             ""))
8423     (setq gnus-tmp-pressed-details
8424           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8425     (unless (bolp)
8426       (insert "\n"))
8427     (setq b (point))
8428     (gnus-eval-format
8429      gnus-mime-security-button-line-format
8430      gnus-mime-security-button-line-format-alist
8431      `(keymap ,gnus-mime-security-button-map
8432          gnus-callback gnus-mime-security-press-button
8433          gnus-line-format ,gnus-mime-security-button-line-format
8434          gnus-mime-details ,gnus-mime-security-button-pressed
8435          article-type annotation
8436          gnus-data ,handle))
8437     (setq e (if (bolp)
8438                 ;; Exclude a newline.
8439                 (1- (point))
8440               (point)))
8441     (when gnus-article-button-face
8442       (gnus-overlay-put (gnus-make-overlay b e nil t)
8443                         'face gnus-article-button-face))
8444     (widget-convert-button
8445      'link b e
8446      :mime-handle handle
8447      :action 'gnus-widget-press-button
8448      :button-keymap gnus-mime-security-button-map
8449      :help-echo
8450      (lambda (widget/window &optional overlay pos)
8451        ;; Needed to properly clear the message due to a bug in
8452        ;; wid-edit (XEmacs only).
8453        (when (boundp 'help-echo-owns-message)
8454          (setq help-echo-owns-message t))
8455        (format
8456         "%S: show detail; %S: more options"
8457         (aref gnus-mouse-2 0)
8458         (aref gnus-down-mouse-3 0))))))
8459
8460 (defun gnus-mime-display-security (handle)
8461   (save-restriction
8462     (narrow-to-region (point) (point))
8463     (unless (gnus-unbuttonized-mime-type-p (car handle))
8464       (gnus-insert-mime-security-button handle))
8465     (gnus-mime-display-part (cadr handle))
8466     (unless (bolp)
8467       (insert "\n"))
8468     (unless (gnus-unbuttonized-mime-type-p (car handle))
8469       (let ((gnus-mime-security-button-line-format
8470              gnus-mime-security-button-end-line-format))
8471         (gnus-insert-mime-security-button handle)))
8472     (mm-set-handle-multipart-parameter
8473      handle 'gnus-region
8474      (cons (set-marker (make-marker) (point-min))
8475            (set-marker (make-marker) (point-max))))
8476     (goto-char (point-max))))
8477
8478 (defun gnus-mime-security-run-function (function)
8479   "Run FUNCTION with the security part under point."
8480   (gnus-article-check-buffer)
8481   (let ((data (get-text-property (point) 'gnus-data))
8482         buffer handle)
8483     (when (and (stringp (car-safe data))
8484                (setq buffer (mm-handle-multipart-original-buffer data))
8485                (setq handle (cadr data)))
8486       (if (bufferp (mm-handle-buffer handle))
8487           (progn
8488             (setq handle (cons buffer (copy-sequence (cdr handle))))
8489             (mm-handle-set-undisplayer handle nil))
8490         (setq handle (mm-make-handle
8491                       buffer
8492                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8493                       nil nil nil nil nil nil)))
8494       (funcall function handle))))
8495
8496 (defun gnus-mime-security-save-part ()
8497   "Save the security part under point."
8498   (interactive)
8499   (gnus-mime-security-run-function 'mm-save-part))
8500
8501 (defun gnus-mime-security-pipe-part ()
8502   "Pipe the security part under point to a process."
8503   (interactive)
8504   (gnus-mime-security-run-function 'mm-pipe-part))
8505
8506 (gnus-ems-redefine)
8507
8508 (provide 'gnus-art)
8509
8510 (run-hooks 'gnus-art-load-hook)
8511
8512 ;;; arch-tag: 2654516f-6279-48f9-a83b-05c1fa450c33
8513 ;;; gnus-art.el ends here