b55a0d4a6a4486087bf92086077619ce343b1faf
[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, 2008 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.1" ;; 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.1" ;; 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.1" ;; 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.1" ;; 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   `(with-current-buffer gnus-article-buffer
1718      (save-restriction
1719        (let ((inhibit-read-only t)
1720              (inhibit-point-motion-hooks t)
1721              (case-fold-search t))
1722          (article-narrow-to-head)
1723          ,@forms))))
1724
1725 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1726 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1727
1728 (defmacro gnus-with-article-buffer (&rest forms)
1729   `(with-current-buffer gnus-article-buffer
1730      (let ((inhibit-read-only t))
1731        ,@forms)))
1732
1733 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1734 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1735
1736 (defun gnus-article-goto-header (header)
1737   "Go to HEADER, which is a regular expression."
1738   (re-search-forward (concat "^\\(" header "\\):") nil t))
1739
1740 (defsubst gnus-article-hide-text (b e props)
1741   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1742   (gnus-add-text-properties-when 'article-type nil b e props)
1743   (when (memq 'intangible props)
1744     (put-text-property
1745      (max (1- b) (point-min))
1746      b 'intangible (cddr (memq 'intangible props)))))
1747
1748 (defsubst gnus-article-unhide-text (b e)
1749   "Remove hidden text properties from region between B and E."
1750   (remove-text-properties b e gnus-hidden-properties)
1751   (when (memq 'intangible gnus-hidden-properties)
1752     (put-text-property (max (1- b) (point-min))
1753                        b 'intangible nil)))
1754
1755 (defun gnus-article-hide-text-type (b e type)
1756   "Hide text of TYPE between B and E."
1757   (gnus-add-wash-type type)
1758   (gnus-article-hide-text
1759    b e (cons 'article-type (cons type gnus-hidden-properties))))
1760
1761 (defun gnus-article-unhide-text-type (b e type)
1762   "Unhide text of TYPE between B and E."
1763   (gnus-delete-wash-type type)
1764   (remove-text-properties
1765    b e (cons 'article-type (cons type gnus-hidden-properties)))
1766   (when (memq 'intangible gnus-hidden-properties)
1767     (put-text-property (max (1- b) (point-min))
1768                        b 'intangible nil)))
1769
1770 (defun gnus-article-hide-text-of-type (type)
1771   "Hide text of TYPE in the current buffer."
1772   (save-excursion
1773     (let ((b (point-min))
1774           (e (point-max)))
1775       (while (setq b (text-property-any b e 'article-type type))
1776         (add-text-properties b (incf b) gnus-hidden-properties)))))
1777
1778 (defun gnus-article-delete-text-of-type (type)
1779   "Delete text of TYPE in the current buffer."
1780   (save-excursion
1781     (let ((b (point-min)))
1782       (if (eq type 'multipart)
1783           ;; Remove MIME buttons associated with multipart/alternative parts.
1784           (progn
1785             (goto-char b)
1786             (while (if (get-text-property (point) 'gnus-part)
1787                        (setq b (point))
1788                      (when (setq b (next-single-property-change (point)
1789                                                                 'gnus-part))
1790                        (goto-char b)
1791                        t))
1792               (end-of-line)
1793               (skip-chars-forward "\n")
1794               (when (eq (get-text-property b 'article-type) 'multipart)
1795                 (delete-region b (point)))))
1796         (while (setq b (text-property-any b (point-max) 'article-type type))
1797           (delete-region
1798            b (or (text-property-not-all b (point-max) 'article-type type)
1799                  (point-max))))))))
1800
1801 (defun gnus-article-delete-invisible-text ()
1802   "Delete all invisible text in the current buffer."
1803   (save-excursion
1804     (let ((b (point-min)))
1805       (while (setq b (text-property-any b (point-max) 'invisible t))
1806         (delete-region
1807          b (or (text-property-not-all b (point-max) 'invisible t)
1808                (point-max)))))))
1809
1810 (defun gnus-article-text-type-exists-p (type)
1811   "Say whether any text of type TYPE exists in the buffer."
1812   (text-property-any (point-min) (point-max) 'article-type type))
1813
1814 (defsubst gnus-article-header-rank ()
1815   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1816   (let ((list gnus-sorted-header-list)
1817         (i 1))
1818     (while list
1819       (if (looking-at (car list))
1820           (setq list nil)
1821         (setq list (cdr list))
1822         (incf i)))
1823       i))
1824
1825 (defun article-hide-headers (&optional arg delete)
1826   "Hide unwanted headers and possibly sort them as well."
1827   (interactive)
1828   ;; This function might be inhibited.
1829   (unless gnus-inhibit-hiding
1830     (let ((inhibit-read-only t)
1831           (case-fold-search t)
1832           (max (1+ (length gnus-sorted-header-list)))
1833           (inhibit-point-motion-hooks t)
1834           (cur (current-buffer))
1835           ignored visible beg)
1836       (save-excursion
1837         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1838         ;; group parameters, so we should go to the summary buffer.
1839         (when (prog1
1840                   (condition-case nil
1841                       (progn (set-buffer gnus-summary-buffer) t)
1842                     (error nil))
1843                 (setq ignored (when (not gnus-visible-headers)
1844                                 (cond ((stringp gnus-ignored-headers)
1845                                        gnus-ignored-headers)
1846                                       ((listp gnus-ignored-headers)
1847                                        (mapconcat 'identity
1848                                                   gnus-ignored-headers
1849                                                   "\\|"))))
1850                       visible (cond ((stringp gnus-visible-headers)
1851                                      gnus-visible-headers)
1852                                     ((and gnus-visible-headers
1853                                           (listp gnus-visible-headers))
1854                                      (mapconcat 'identity
1855                                                 gnus-visible-headers
1856                                                 "\\|")))))
1857           (set-buffer cur))
1858         (save-restriction
1859           ;; First we narrow to just the headers.
1860           (article-narrow-to-head)
1861           ;; Hide any "From " lines at the beginning of (mail) articles.
1862           (while (looking-at "From ")
1863             (forward-line 1))
1864           (unless (bobp)
1865             (delete-region (point-min) (point)))
1866           ;; Then treat the rest of the header lines.
1867           ;; Then we use the two regular expressions
1868           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1869           ;; select which header lines is to remain visible in the
1870           ;; article buffer.
1871           (while (re-search-forward "^[^ \t:]*:" nil t)
1872             (beginning-of-line)
1873             ;; Mark the rank of the header.
1874             (put-text-property
1875              (point) (1+ (point)) 'message-rank
1876              (if (or (and visible (looking-at visible))
1877                      (and ignored
1878                           (not (looking-at ignored))))
1879                  (gnus-article-header-rank)
1880                (+ 2 max)))
1881             (forward-line 1))
1882           (message-sort-headers-1)
1883           (when (setq beg (text-property-any
1884                            (point-min) (point-max) 'message-rank (+ 2 max)))
1885             ;; We delete the unwanted headers.
1886             (gnus-add-wash-type 'headers)
1887             (add-text-properties (point-min) (+ 5 (point-min))
1888                                  '(article-type headers dummy-invisible t))
1889             (delete-region beg (point-max))))))))
1890
1891 (defun article-hide-boring-headers (&optional arg)
1892   "Toggle hiding of headers that aren't very interesting.
1893 If given a negative prefix, always show; if given a positive prefix,
1894 always hide."
1895   (interactive (gnus-article-hidden-arg))
1896   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1897              (not gnus-show-all-headers))
1898     (save-excursion
1899       (save-restriction
1900         (let ((inhibit-read-only t)
1901               (inhibit-point-motion-hooks t))
1902           (article-narrow-to-head)
1903           (dolist (elem gnus-boring-article-headers)
1904             (goto-char (point-min))
1905             (cond
1906              ;; Hide empty headers.
1907              ((eq elem 'empty)
1908               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1909                 (forward-line -1)
1910                 (gnus-article-hide-text-type
1911                  (point-at-bol)
1912                  (progn
1913                    (end-of-line)
1914                    (if (re-search-forward "^[^ \t]" nil t)
1915                        (match-beginning 0)
1916                      (point-max)))
1917                  'boring-headers)))
1918              ;; Hide boring Newsgroups header.
1919              ((eq elem 'newsgroups)
1920               (when (gnus-string-equal
1921                      (gnus-fetch-field "newsgroups")
1922                      (gnus-group-real-name
1923                       (if (boundp 'gnus-newsgroup-name)
1924                           gnus-newsgroup-name
1925                         "")))
1926                 (gnus-article-hide-header "newsgroups")))
1927              ((eq elem 'to-address)
1928               (let ((to (message-fetch-field "to"))
1929                     (to-address
1930                      (gnus-parameter-to-address
1931                       (if (boundp 'gnus-newsgroup-name)
1932                           gnus-newsgroup-name ""))))
1933                 (when (and to to-address
1934                            (ignore-errors
1935                              (gnus-string-equal
1936                               ;; only one address in To
1937                               (nth 1 (mail-extract-address-components to))
1938                               to-address)))
1939                   (gnus-article-hide-header "to"))))
1940              ((eq elem 'to-list)
1941               (let ((to (message-fetch-field "to"))
1942                     (to-list
1943                      (gnus-parameter-to-list
1944                       (if (boundp 'gnus-newsgroup-name)
1945                           gnus-newsgroup-name ""))))
1946                 (when (and to to-list
1947                            (ignore-errors
1948                              (gnus-string-equal
1949                               ;; only one address in To
1950                               (nth 1 (mail-extract-address-components to))
1951                               to-list)))
1952                   (gnus-article-hide-header "to"))))
1953              ((eq elem 'cc-list)
1954               (let ((cc (message-fetch-field "cc"))
1955                     (to-list
1956                      (gnus-parameter-to-list
1957                       (if (boundp 'gnus-newsgroup-name)
1958                           gnus-newsgroup-name ""))))
1959                 (when (and cc to-list
1960                            (ignore-errors
1961                              (gnus-string-equal
1962                               ;; only one address in CC
1963                               (nth 1 (mail-extract-address-components cc))
1964                               to-list)))
1965                   (gnus-article-hide-header "cc"))))
1966              ((eq elem 'followup-to)
1967               (when (gnus-string-equal
1968                      (message-fetch-field "followup-to")
1969                      (message-fetch-field "newsgroups"))
1970                 (gnus-article-hide-header "followup-to")))
1971              ((eq elem 'reply-to)
1972               (if (gnus-group-find-parameter
1973                    gnus-newsgroup-name 'broken-reply-to)
1974                   (gnus-article-hide-header "reply-to")
1975                 (let ((from (message-fetch-field "from"))
1976                       (reply-to (message-fetch-field "reply-to")))
1977                   (when
1978                       (and
1979                        from reply-to
1980                        (ignore-errors
1981                          (equal
1982                           (sort (mapcar
1983                                  (lambda (x) (downcase (cadr x)))
1984                                  (mail-extract-address-components from t))
1985                                 'string<)
1986                           (sort (mapcar
1987                                  (lambda (x) (downcase (cadr x)))
1988                                  (mail-extract-address-components reply-to t))
1989                                 'string<))))
1990                     (gnus-article-hide-header "reply-to")))))
1991              ((eq elem 'date)
1992               (let ((date (with-current-buffer gnus-original-article-buffer
1993                             ;; If date in `gnus-article-buffer' is localized
1994                             ;; (`gnus-treat-date-user-defined'),
1995                             ;; `days-between' might fail.
1996                             (message-fetch-field "date"))))
1997                 (when (and date
1998                            (< (days-between (current-time-string) date)
1999                               4))
2000                   (gnus-article-hide-header "date"))))
2001              ((eq elem 'long-to)
2002               (let ((to (message-fetch-field "to"))
2003                     (cc (message-fetch-field "cc")))
2004                 (when (> (length to) 1024)
2005                   (gnus-article-hide-header "to"))
2006                 (when (> (length cc) 1024)
2007                   (gnus-article-hide-header "cc"))))
2008              ((eq elem 'many-to)
2009               (let ((to-count 0)
2010                     (cc-count 0))
2011                 (goto-char (point-min))
2012                 (while (re-search-forward "^to:" nil t)
2013                   (setq to-count (1+ to-count)))
2014                 (when (> to-count 1)
2015                   (while (> to-count 0)
2016                     (goto-char (point-min))
2017                     (save-restriction
2018                       (re-search-forward "^to:" nil nil to-count)
2019                       (forward-line -1)
2020                       (narrow-to-region (point) (point-max))
2021                       (gnus-article-hide-header "to"))
2022                     (setq to-count (1- to-count))))
2023                 (goto-char (point-min))
2024                 (while (re-search-forward "^cc:" nil t)
2025                   (setq cc-count (1+ cc-count)))
2026                 (when (> cc-count 1)
2027                   (while (> cc-count 0)
2028                     (goto-char (point-min))
2029                     (save-restriction
2030                       (re-search-forward "^cc:" nil nil cc-count)
2031                       (forward-line -1)
2032                       (narrow-to-region (point) (point-max))
2033                       (gnus-article-hide-header "cc"))
2034                     (setq cc-count (1- cc-count)))))))))))))
2035
2036 (defun gnus-article-hide-header (header)
2037   (save-excursion
2038     (goto-char (point-min))
2039     (when (re-search-forward (concat "^" header ":") nil t)
2040       (gnus-article-hide-text-type
2041        (point-at-bol)
2042        (progn
2043          (end-of-line)
2044          (if (re-search-forward "^[^ \t]" nil t)
2045              (match-beginning 0)
2046            (point-max)))
2047        'boring-headers))))
2048
2049 (defvar gnus-article-normalized-header-length 40
2050   "Length of normalized headers.")
2051
2052 (defun article-normalize-headers ()
2053   "Make all header lines 40 characters long."
2054   (interactive)
2055   (let ((inhibit-read-only t)
2056         column)
2057     (save-excursion
2058       (save-restriction
2059         (article-narrow-to-head)
2060         (while (not (eobp))
2061           (cond
2062            ((< (setq column (- (point-at-eol) (point)))
2063                gnus-article-normalized-header-length)
2064             (end-of-line)
2065             (insert (make-string
2066                      (- gnus-article-normalized-header-length column)
2067                      ? )))
2068            ((> column gnus-article-normalized-header-length)
2069             (gnus-put-text-property
2070              (progn
2071                (forward-char gnus-article-normalized-header-length)
2072                (point))
2073              (point-at-eol)
2074              'invisible t))
2075            (t
2076             ;; Do nothing.
2077             ))
2078           (forward-line 1))))))
2079
2080 (defun article-treat-dumbquotes ()
2081   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2082 Note that this function guesses whether a character is a sm*rtq**t* or
2083 not, so it should only be used interactively.
2084
2085 Sm*rtq**t*s are M****s***'s unilateral extension to the
2086 iso-8859-1 character map in an attempt to provide more quoting
2087 characters.  If you see something like \\222 or \\264 where
2088 you're expecting some kind of apostrophe or quotation mark, then
2089 try this wash."
2090   (interactive)
2091   (article-translate-strings gnus-article-dumbquotes-map))
2092
2093 (defun article-translate-characters (from to)
2094   "Translate all characters in the body of the article according to FROM and TO.
2095 FROM is a string of characters to translate from; to is a string of
2096 characters to translate to."
2097   (save-excursion
2098     (when (article-goto-body)
2099       (let ((inhibit-read-only t)
2100             (x (make-string 225 ?x))
2101             (i -1))
2102         (while (< (incf i) (length x))
2103           (aset x i i))
2104         (setq i 0)
2105         (while (< i (length from))
2106           (aset x (aref from i) (aref to i))
2107           (incf i))
2108         (translate-region (point) (point-max) x)))))
2109
2110 (defun article-translate-strings (map)
2111   "Translate all string in the body of the article according to MAP.
2112 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2113   (save-excursion
2114     (when (article-goto-body)
2115       (let ((inhibit-read-only t))
2116         (dolist (elem map)
2117           (save-excursion
2118             (while (search-forward (car elem) nil t)
2119               (replace-match (cadr elem)))))))))
2120
2121 (defun article-treat-overstrike ()
2122   "Translate overstrikes into bold text."
2123   (interactive)
2124   (save-excursion
2125     (when (article-goto-body)
2126       (let ((inhibit-read-only t))
2127         (while (search-forward "\b" nil t)
2128           (let ((next (char-after))
2129                 (previous (char-after (- (point) 2))))
2130             ;; We do the boldification/underlining by hiding the
2131             ;; overstrikes and putting the proper text property
2132             ;; on the letters.
2133             (cond
2134              ((eq next previous)
2135               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2136               (put-text-property (point) (1+ (point)) 'face 'bold))
2137              ((eq next ?_)
2138               (gnus-article-hide-text-type
2139                (1- (point)) (1+ (point)) 'overstrike)
2140               (put-text-property
2141                (- (point) 2) (1- (point)) 'face 'underline))
2142              ((eq previous ?_)
2143               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2144               (put-text-property
2145                (point) (1+ (point)) 'face 'underline)))))))))
2146
2147 (defun article-treat-ansi-sequences ()
2148   "Translate ANSI SGR control sequences into overlays or extents."
2149   (interactive)
2150   (save-excursion
2151     (when (article-goto-body)
2152       (let ((inhibit-read-only t))
2153         (ansi-color-apply-on-region (point) (point-max))))))
2154
2155 (defun gnus-article-treat-unfold-headers ()
2156   "Unfold folded message headers.
2157 Only the headers that fit into the current window width will be
2158 unfolded."
2159   (interactive)
2160   (gnus-with-article-headers
2161     (let (length)
2162       (while (not (eobp))
2163         (save-restriction
2164           (mail-header-narrow-to-field)
2165           (let* ((header (buffer-string))
2166                  (unfoldable
2167                   (or (equal gnus-article-unfold-long-headers t)
2168                       (and (stringp gnus-article-unfold-long-headers)
2169                            (string-match gnus-article-unfold-long-headers header)))))
2170             (with-temp-buffer
2171               (insert header)
2172               (goto-char (point-min))
2173               (while (re-search-forward "\n[\t ]" nil t)
2174                 (replace-match " " t t)))
2175             (setq length (- (point-max) (point-min) 1))
2176             (when (or unfoldable
2177                       (< length (window-width)))
2178               (while (re-search-forward "\n[\t ]" nil t)
2179                 (replace-match " " t t))))
2180           (goto-char (point-max)))))))
2181
2182 (defun gnus-article-treat-fold-headers ()
2183   "Fold message headers."
2184   (interactive)
2185   (gnus-with-article-headers
2186     (while (not (eobp))
2187       (save-restriction
2188         (mail-header-narrow-to-field)
2189         (mail-header-fold-field)
2190         (goto-char (point-max))))))
2191
2192 (defun gnus-treat-smiley ()
2193   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2194   (interactive)
2195   (gnus-with-article-buffer
2196     (if (memq 'smiley gnus-article-wash-types)
2197         (gnus-delete-images 'smiley)
2198       (article-goto-body)
2199       (let ((images (smiley-region (point) (point-max))))
2200         (when images
2201           (gnus-add-wash-type 'smiley)
2202           (dolist (image images)
2203             (gnus-add-image 'smiley image)))))))
2204
2205 (defun gnus-article-remove-images ()
2206   "Remove all images from the article buffer."
2207   (interactive)
2208   (gnus-with-article-buffer
2209     (dolist (elem gnus-article-image-alist)
2210       (gnus-delete-images (car elem)))))
2211
2212 (defun gnus-article-treat-fold-newsgroups ()
2213   "Unfold folded message headers.
2214 Only the headers that fit into the current window width will be
2215 unfolded."
2216   (interactive)
2217   (gnus-with-article-headers
2218     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2219       (save-restriction
2220         (mail-header-narrow-to-field)
2221         (while (re-search-forward ", *" nil t)
2222           (replace-match ", " t t))
2223         (mail-header-fold-field)
2224         (goto-char (point-max))))))
2225
2226 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2227   "Value of `truncate-lines' in Gnus Article buffer.
2228 Valid values are nil, t, `head', `first', `last', an integer or a
2229 predicate.  See Info node `(gnus)Customizing Articles'."
2230   :version "23.1" ;; No Gnus
2231   :group 'gnus-article
2232   ;; :link '(custom-manual "(gnus)Customizing Articles")
2233   :type 'boolean)
2234
2235 (defun gnus-article-toggle-truncate-lines (&optional arg)
2236   "Toggle whether to fold or truncate long lines in article the buffer.
2237 If ARG is non-nil and not a number, toggle
2238 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2239 long lines iff arg is positive."
2240   (interactive "P")
2241   (cond
2242    ((and (numberp arg) (> arg 0))
2243     (setq gnus-article-truncate-lines t))
2244    ((numberp arg)
2245     (setq gnus-article-truncate-lines nil))
2246    (arg
2247     (setq gnus-article-truncate-lines
2248           (not gnus-article-truncate-lines))))
2249   (gnus-with-article-buffer
2250     (cond
2251      ((and (numberp arg) (> arg 0))
2252       (setq truncate-lines nil))
2253      ((numberp arg)
2254       (setq truncate-lines t)))
2255     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2256     ;; `toggle-truncate-lines' needs an argument.
2257     (toggle-truncate-lines)))
2258
2259 (defun gnus-article-treat-body-boundary ()
2260   "Place a boundary line at the end of the headers."
2261   (interactive)
2262   (when (and gnus-body-boundary-delimiter
2263              (> (length gnus-body-boundary-delimiter) 0))
2264     (gnus-with-article-headers
2265       (goto-char (point-max))
2266       (let ((start (point)))
2267         (insert "X-Boundary: ")
2268         (gnus-add-text-properties start (point) '(invisible t intangible t))
2269         (insert (let (str)
2270                   (while (>= (1- (window-width)) (length str))
2271                     (setq str (concat str gnus-body-boundary-delimiter)))
2272                   (substring str 0 (1- (window-width))))
2273                 "\n")
2274         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2275
2276 (defun article-fill-long-lines ()
2277   "Fill lines that are wider than the window width."
2278   (interactive)
2279   (save-excursion
2280     (let ((inhibit-read-only t)
2281           (width (window-width (get-buffer-window (current-buffer)))))
2282       (save-restriction
2283         (article-goto-body)
2284         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2285           (while (not (eobp))
2286             (end-of-line)
2287             (when (>= (current-column) (min fill-column width))
2288               (narrow-to-region (min (1+ (point)) (point-max))
2289                                 (point-at-bol))
2290               (let ((goback (point-marker)))
2291                 (fill-paragraph nil)
2292                 (goto-char (marker-position goback)))
2293               (widen))
2294             (forward-line 1)))))))
2295
2296 (defun article-capitalize-sentences ()
2297   "Capitalize the first word in each sentence."
2298   (interactive)
2299   (save-excursion
2300     (let ((inhibit-read-only t)
2301           (paragraph-start "^[\n\^L]"))
2302       (article-goto-body)
2303       (while (not (eobp))
2304         (capitalize-word 1)
2305         (forward-sentence)))))
2306
2307 (defun article-remove-cr ()
2308   "Remove trailing CRs and then translate remaining CRs into LFs."
2309   (interactive)
2310   (save-excursion
2311     (let ((inhibit-read-only t))
2312       (goto-char (point-min))
2313       (while (re-search-forward "\r+$" nil t)
2314         (replace-match "" t t))
2315       (goto-char (point-min))
2316       (while (search-forward "\r" nil t)
2317         (replace-match "\n" t t)))))
2318
2319 (defun article-remove-trailing-blank-lines ()
2320   "Remove all trailing blank lines from the article."
2321   (interactive)
2322   (save-excursion
2323     (let ((inhibit-read-only t))
2324       (goto-char (point-max))
2325       (delete-region
2326        (point)
2327        (progn
2328          (while (and (not (bobp))
2329                      (looking-at "^[ \t]*$")
2330                      (not (gnus-annotation-in-region-p
2331                            (point) (point-at-eol))))
2332            (forward-line -1))
2333          (forward-line 1)
2334          (point))))))
2335
2336 (defvar gnus-face-properties-alist)
2337
2338 (defun article-display-face (&optional force)
2339   "Display any Face headers in the header."
2340   (interactive (list 'force))
2341   (let ((wash-face-p buffer-read-only))
2342     (gnus-with-article-headers
2343       ;; When displaying parts, this function can be called several times on
2344       ;; the same article, without any intended toggle semantic (as typing `W
2345       ;; D d' would have). So face deletion must occur only when we come from
2346       ;; an interactive command, that is when the *Article* buffer is
2347       ;; read-only.
2348       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2349           (gnus-delete-images 'face)
2350         (let ((from (message-fetch-field "from"))
2351               face faces)
2352           (save-current-buffer
2353             (when (and wash-face-p
2354                        (gnus-buffer-live-p gnus-original-article-buffer)
2355                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2356               (set-buffer gnus-original-article-buffer))
2357             (save-restriction
2358               (mail-narrow-to-head)
2359               (when (or force
2360                         ;; Check whether this face is censored.
2361                         (not (and gnus-article-x-face-too-ugly
2362                                   (or from
2363                                       (setq from (message-fetch-field "from")))
2364                                   (string-match gnus-article-x-face-too-ugly
2365                                                 from))))
2366                 (while (gnus-article-goto-header "Face")
2367                   (push (mail-header-field-value) faces)))))
2368           (when faces
2369             (goto-char (point-min))
2370             (let (png image)
2371               (unless (setq from (gnus-article-goto-header "from"))
2372                 (insert "From:")
2373                 (setq from (point))
2374                 (insert " [no `from' set]\n"))
2375               (while faces
2376                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2377                   (setq image
2378                         (apply 'gnus-create-image png 'png t
2379                                (cdr (assq 'png gnus-face-properties-alist))))
2380                   (goto-char from)
2381                   (gnus-add-wash-type 'face)
2382                   (gnus-add-image 'face image)
2383                   (gnus-put-image image nil 'face))))))))))
2384
2385 (defun article-display-x-face (&optional force)
2386   "Look for an X-Face header and display it if present."
2387   (interactive (list 'force))
2388   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2389     (gnus-with-article-headers
2390       ;; Delete the old process, if any.
2391       (when (process-status "article-x-face")
2392         (delete-process "article-x-face"))
2393       ;; See the comment in `article-display-face'.
2394       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2395           ;; We have already displayed X-Faces, so we remove them
2396           ;; instead.
2397           (gnus-delete-images 'xface)
2398         ;; Display X-Faces.
2399         (let ((from (message-fetch-field "from"))
2400               x-faces face)
2401           (save-current-buffer
2402             (when (and wash-face-p
2403                        (gnus-buffer-live-p gnus-original-article-buffer)
2404                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2405               ;; If type `W f', use gnus-original-article-buffer,
2406               ;; otherwise use the current buffer because displaying
2407               ;; RFC822 parts calls this function too.
2408               (set-buffer gnus-original-article-buffer))
2409             (save-restriction
2410               (mail-narrow-to-head)
2411               (and gnus-article-x-face-command
2412                    (or force
2413                        ;; Check whether this face is censored.
2414                        (not (and gnus-article-x-face-too-ugly
2415                                  (or from
2416                                      (setq from (message-fetch-field "from")))
2417                                  (string-match gnus-article-x-face-too-ugly
2418                                                from))))
2419                    (while (gnus-article-goto-header "X-Face")
2420                      (push (mail-header-field-value) x-faces)))))
2421           (when x-faces
2422             ;; We display the face.
2423             (cond ((functionp gnus-article-x-face-command)
2424                    ;; The command is a lisp function, so we call it.
2425                    (mapc gnus-article-x-face-command x-faces))
2426                   ((stringp gnus-article-x-face-command)
2427                    ;; The command is a string, so we interpret the command
2428                    ;; as a, well, command, and fork it off.
2429                    (let ((process-connection-type nil))
2430                      (gnus-set-process-query-on-exit-flag
2431                       (start-process
2432                        "article-x-face" nil shell-file-name
2433                        shell-command-switch gnus-article-x-face-command)
2434                       nil)
2435                      ;; Sending multiple EOFs to xv doesn't work,
2436                      ;; so we only do a single external face.
2437                      (with-temp-buffer
2438                        (insert (car x-faces))
2439                        (process-send-region "article-x-face"
2440                                             (point-min) (point-max)))
2441                      (process-send-eof "article-x-face")))
2442                   (t
2443                    (error "`%s' set to `%s' is not a function"
2444                           gnus-article-x-face-command
2445                           'gnus-article-x-face-command)))))))))
2446
2447 (defun article-decode-mime-words ()
2448   "Decode all MIME-encoded words in the article."
2449   (interactive)
2450   (gnus-with-article-buffer
2451     (let ((inhibit-point-motion-hooks t)
2452           (mail-parse-charset gnus-newsgroup-charset)
2453           (mail-parse-ignored-charsets
2454            (with-current-buffer gnus-summary-buffer
2455              gnus-newsgroup-ignored-charsets)))
2456       (mail-decode-encoded-word-region (point-min) (point-max)))))
2457
2458 (defun article-decode-charset (&optional prompt)
2459   "Decode charset-encoded text in the article.
2460 If PROMPT (the prefix), prompt for a coding system to use."
2461   (interactive "P")
2462   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2463         (inhibit-read-only t)
2464         (mail-parse-charset gnus-newsgroup-charset)
2465         (mail-parse-ignored-charsets
2466          (save-excursion (condition-case nil
2467                              (set-buffer gnus-summary-buffer)
2468                            (error))
2469                          gnus-newsgroup-ignored-charsets))
2470         ct cte ctl charset format)
2471     (save-excursion
2472       (save-restriction
2473         (article-narrow-to-head)
2474         (setq ct (message-fetch-field "Content-Type" t)
2475               cte (message-fetch-field "Content-Transfer-Encoding" t)
2476               ctl (and ct (mail-header-parse-content-type ct))
2477               charset (cond
2478                        (prompt
2479                         (mm-read-coding-system "Charset to decode: "))
2480                        (ctl
2481                         (mail-content-type-get ctl 'charset)))
2482               format (and ctl (mail-content-type-get ctl 'format)))
2483         (when cte
2484           (setq cte (mail-header-strip cte)))
2485         (if (and ctl (not (string-match "/" (car ctl))))
2486             (setq ctl nil))
2487         (goto-char (point-max)))
2488       (forward-line 1)
2489       (save-restriction
2490         (narrow-to-region (point) (point-max))
2491         (when (and (eq mail-parse-charset 'gnus-decoded)
2492                    (eq (mm-body-7-or-8) '8bit))
2493           ;; The text code could have been decoded.
2494           (setq charset mail-parse-charset))
2495         (when (and (or (not ctl)
2496                        (equal (car ctl) "text/plain"))
2497                    (not format)) ;; article with format will decode later.
2498           (mm-decode-body
2499            charset (and cte (intern (downcase
2500                                      (gnus-strip-whitespace cte))))
2501            (car ctl)))))))
2502
2503 (defun article-decode-encoded-words ()
2504   "Remove encoded-word encoding from headers."
2505   (let ((inhibit-point-motion-hooks t)
2506         (mail-parse-charset gnus-newsgroup-charset)
2507         (mail-parse-ignored-charsets
2508          (save-excursion (condition-case nil
2509                              (set-buffer gnus-summary-buffer)
2510                            (error))
2511                          gnus-newsgroup-ignored-charsets))
2512         (inhibit-read-only t)
2513         end start)
2514     (goto-char (point-min))
2515     (when (search-forward "\n\n" nil 'move)
2516       (forward-line -1))
2517     (setq end (point))
2518     (while (not (bobp))
2519       (while (progn
2520                (forward-line -1)
2521                (and (not (bobp))
2522                     (memq (char-after) '(?\t ? )))))
2523       (setq start (point))
2524       (if (looking-at "\
2525 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2526 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2527           (funcall gnus-decode-address-function start end)
2528         (funcall gnus-decode-header-function start end))
2529       (goto-char (setq end start)))))
2530
2531 (defun article-decode-group-name ()
2532   "Decode group names in Newsgroups, Followup-To and Xref headers."
2533   (let ((inhibit-point-motion-hooks t)
2534         (inhibit-read-only t)
2535         (method (gnus-find-method-for-group gnus-newsgroup-name))
2536         regexp)
2537     (when (and (or gnus-group-name-charset-method-alist
2538                    gnus-group-name-charset-group-alist)
2539                (gnus-buffer-live-p gnus-original-article-buffer))
2540       (save-restriction
2541         (article-narrow-to-head)
2542         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2543           (with-current-buffer gnus-original-article-buffer
2544             (goto-char (point-min)))
2545           (setq regexp (concat "^" header
2546                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2547           (while (re-search-forward regexp nil t)
2548             (replace-match (save-match-data
2549                              (gnus-decode-newsgroups
2550                               ;; XXX how to use data in article buffer?
2551                               (with-current-buffer gnus-original-article-buffer
2552                                 (re-search-forward regexp nil t)
2553                                 (match-string 1))
2554                               gnus-newsgroup-name method))
2555                            t t nil 1))
2556           (goto-char (point-min)))))))
2557
2558 (autoload 'idna-to-unicode "idna")
2559
2560 (defun article-decode-idna-rhs ()
2561   "Decode IDNA strings in RHS in various headers in current buffer.
2562 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2563 Mail-Reply-To: and Mail-Followup-To:."
2564   (when gnus-use-idna
2565     (save-restriction
2566       (let ((inhibit-point-motion-hooks t)
2567             (inhibit-read-only t))
2568         (article-narrow-to-head)
2569         (goto-char (point-min))
2570         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2571           (let (ace unicode)
2572             (when (save-match-data
2573                     (and (setq ace (match-string 1))
2574                          (save-excursion
2575                            (and (re-search-backward "^[^ \t]" nil t)
2576                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2577                          (setq unicode (idna-to-unicode ace))))
2578               (unless (string= ace unicode)
2579                 (replace-match unicode nil nil nil 1)))))))))
2580
2581 (defun article-de-quoted-unreadable (&optional force read-charset)
2582   "Translate a quoted-printable-encoded article.
2583 If FORCE, decode the article whether it is marked as quoted-printable
2584 or not.
2585 If READ-CHARSET, ask for a coding system."
2586   (interactive (list 'force current-prefix-arg))
2587   (save-excursion
2588     (let ((inhibit-read-only t) type charset)
2589       (if (gnus-buffer-live-p gnus-original-article-buffer)
2590           (with-current-buffer gnus-original-article-buffer
2591             (setq type
2592                   (gnus-fetch-field "content-transfer-encoding"))
2593             (let* ((ct (gnus-fetch-field "content-type"))
2594                    (ctl (and ct (mail-header-parse-content-type ct))))
2595               (setq charset (and ctl
2596                                  (mail-content-type-get ctl 'charset)))
2597               (if (stringp charset)
2598                   (setq charset (intern (downcase charset)))))))
2599       (if read-charset
2600           (setq charset (mm-read-coding-system "Charset: " charset)))
2601       (unless charset
2602         (setq charset gnus-newsgroup-charset))
2603       (when (or force
2604                 (and type (let ((case-fold-search t))
2605                             (string-match "quoted-printable" type))))
2606         (article-goto-body)
2607         (quoted-printable-decode-region
2608          (point) (point-max) (mm-charset-to-coding-system charset))))))
2609
2610 (defun article-de-base64-unreadable (&optional force read-charset)
2611   "Translate a base64 article.
2612 If FORCE, decode the article whether it is marked as base64 not.
2613 If READ-CHARSET, ask for a coding system."
2614   (interactive (list 'force current-prefix-arg))
2615   (save-excursion
2616     (let ((inhibit-read-only t) type charset)
2617       (if (gnus-buffer-live-p gnus-original-article-buffer)
2618           (with-current-buffer gnus-original-article-buffer
2619             (setq type
2620                   (gnus-fetch-field "content-transfer-encoding"))
2621             (let* ((ct (gnus-fetch-field "content-type"))
2622                    (ctl (and ct (mail-header-parse-content-type ct))))
2623               (setq charset (and ctl
2624                                  (mail-content-type-get ctl 'charset)))
2625               (if (stringp charset)
2626                   (setq charset (intern (downcase charset)))))))
2627       (if read-charset
2628           (setq charset (mm-read-coding-system "Charset: " charset)))
2629       (unless charset
2630         (setq charset gnus-newsgroup-charset))
2631       (when (or force
2632                 (and type (let ((case-fold-search t))
2633                             (string-match "base64" type))))
2634         (article-goto-body)
2635         (save-restriction
2636           (narrow-to-region (point) (point-max))
2637           (base64-decode-region (point-min) (point-max))
2638           (mm-decode-coding-region
2639            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2640
2641 (eval-when-compile
2642   (require 'rfc1843))
2643
2644 (defun article-decode-HZ ()
2645   "Translate a HZ-encoded article."
2646   (interactive)
2647   (require 'rfc1843)
2648   (save-excursion
2649     (let ((inhibit-read-only t))
2650       (rfc1843-decode-region (point-min) (point-max)))))
2651
2652 (defun article-unsplit-urls ()
2653   "Remove the newlines that some other mailers insert into URLs."
2654   (interactive)
2655   (save-excursion
2656     (let ((inhibit-read-only t))
2657       (goto-char (point-min))
2658       (while (re-search-forward
2659               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2660         (replace-match "\\1\\3" t)))
2661     (when (interactive-p)
2662       (gnus-treat-article nil))))
2663
2664
2665 (defun article-wash-html (&optional read-charset)
2666   "Format an HTML article.
2667 If READ-CHARSET, ask for a coding system.  If it is a number, the
2668 charset defined in `gnus-summary-show-article-charset-alist' is used."
2669   (interactive "P")
2670   (save-excursion
2671     (let ((inhibit-read-only t)
2672           charset)
2673       (if read-charset
2674           (if (or (and (numberp read-charset)
2675                        (setq charset
2676                              (cdr
2677                               (assq read-charset
2678                                     gnus-summary-show-article-charset-alist))))
2679                   (setq charset (mm-read-coding-system "Charset: ")))
2680               (let ((gnus-summary-show-article-charset-alist
2681                      (list (cons 1 charset))))
2682                 (with-current-buffer gnus-summary-buffer
2683                   (gnus-summary-show-article 1)))
2684             (error "No charset is given"))
2685         (when (gnus-buffer-live-p gnus-original-article-buffer)
2686           (with-current-buffer gnus-original-article-buffer
2687             (let* ((ct (gnus-fetch-field "content-type"))
2688                    (ctl (and ct (mail-header-parse-content-type ct))))
2689               (setq charset (and ctl
2690                                  (mail-content-type-get ctl 'charset)))
2691               (when (stringp charset)
2692                 (setq charset (intern (downcase charset)))))))
2693         (unless charset
2694           (setq charset gnus-newsgroup-charset)))
2695       (article-goto-body)
2696       (save-window-excursion
2697         (save-restriction
2698           (narrow-to-region (point) (point-max))
2699           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2700                  (entry (assq func mm-text-html-washer-alist)))
2701             (when entry
2702               (setq func (cdr entry)))
2703             (cond
2704              ((functionp func)
2705               (funcall func))
2706              (t
2707               (apply (car func) (cdr func))))))))))
2708
2709 ;; External.
2710 (declare-function w3-region "ext:w3-display" (st nd))
2711
2712 (defun gnus-article-wash-html-with-w3 ()
2713   "Wash the current buffer with w3."
2714   (mm-setup-w3)
2715   (let ((w3-strict-width (window-width))
2716         (url-standalone-mode t)
2717         (url-gateway-unplugged t)
2718         (w3-honor-stylesheets nil))
2719     (condition-case ()
2720         (w3-region (point-min) (point-max))
2721       (error))))
2722
2723 ;; External.
2724 (declare-function w3m-region "ext:w3m" (start end &optional url charset))
2725
2726 (defun gnus-article-wash-html-with-w3m ()
2727   "Wash the current buffer with emacs-w3m."
2728   (mm-setup-w3m)
2729   (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
2730         w3m-force-redisplay)
2731     (w3m-region (point-min) (point-max)))
2732   (when (and mm-inline-text-html-with-w3m-keymap
2733              (boundp 'w3m-minor-mode-map)
2734              w3m-minor-mode-map)
2735     (add-text-properties
2736      (point-min) (point-max)
2737      (list 'keymap w3m-minor-mode-map
2738            ;; Put the mark meaning this part was rendered by emacs-w3m.
2739            'mm-inline-text-html-with-w3m t))))
2740
2741 (defvar charset) ;; Bound by `article-wash-html'.
2742
2743 (defun gnus-article-wash-html-with-w3m-standalone ()
2744   "Wash the current buffer with w3m."
2745   (if (mm-w3m-standalone-supports-m17n-p)
2746       (progn
2747         (unless (mm-coding-system-p charset) ;; Bound by `article-wash-html'.
2748           ;; The default.
2749           (setq charset 'iso-8859-1))
2750         (let ((coding-system-for-write charset)
2751               (coding-system-for-read charset))
2752           (call-process-region
2753            (point-min) (point-max)
2754            "w3m" t t nil "-dump" "-T" "text/html"
2755            "-I" (symbol-name charset) "-O" (symbol-name charset))))
2756     (mm-inline-wash-with-stdin nil "w3m" "-dump" "-T" "text/html")))
2757
2758 (defvar gnus-article-browse-html-temp-list nil
2759   "List of temporary files created by `gnus-article-browse-html-parts'.
2760 Internal variable.")
2761
2762 (defcustom gnus-article-browse-delete-temp 'ask
2763   "What to do with temporary files from `gnus-article-browse-html-parts'.
2764 If nil, don't delete temporary files.  If it is t, delete them on
2765 exit from the summary buffer.  If it is the symbol `file', query
2766 on each file, if it is `ask' ask once when exiting from the
2767 summary buffer."
2768   :group 'gnus-article
2769   :version "23.1" ;; No Gnus
2770   :type '(choice (const :tag "Don't delete" nil)
2771                  (const :tag "Don't ask" t)
2772                  (const :tag "Ask" ask)
2773                  (const :tag "Ask for each file" file)))
2774
2775 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2776
2777 (defun gnus-article-browse-delete-temp-files (&optional how)
2778   "Delete temp-files created by `gnus-article-browse-html-parts'."
2779   (when (and gnus-article-browse-html-temp-list
2780              (or how
2781                  (setq how gnus-article-browse-delete-temp)))
2782     (when (and (eq how 'ask)
2783                (gnus-y-or-n-p (format
2784                                "Delete all %s temporary HTML file(s)? "
2785                                (length gnus-article-browse-html-temp-list)))
2786                (setq how t)))
2787     (dolist (file gnus-article-browse-html-temp-list)
2788       (when (and (file-exists-p file)
2789                  (or (eq how t)
2790                      ;; `how' is neither `nil', `ask' nor `t' (i.e. `file'):
2791                      (gnus-y-or-n-p
2792                       (format "Delete temporary HTML file `%s'? " file))))
2793         (delete-file file)))
2794     ;; Also remove file from the list when not deleted or if file doesn't
2795     ;; exist anymore.
2796     (setq gnus-article-browse-html-temp-list nil))
2797   gnus-article-browse-html-temp-list)
2798
2799 (defun gnus-article-browse-html-parts (list &optional header)
2800   "View all \"text/html\" parts from LIST.
2801 Recurse into multiparts.  The optional HEADER that should be a decoded
2802 message header will be added to the bodies of the \"text/html\" parts."
2803   ;; Internal function used by `gnus-article-browse-html-article'.
2804   (let (type file charset tmp-file showed)
2805     ;; Find and show the html-parts.
2806     (dolist (handle list)
2807       ;; If HTML, show it:
2808       (cond ((not (listp handle)))
2809             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2810                  (and (equal (car type) "message/external-body")
2811                       (or header
2812                           (setq file (or (mail-content-type-get type 'name)
2813                                          (mail-content-type-get
2814                                           (mm-handle-disposition handle)
2815                                           'filename))))
2816                       (or (mm-handle-cache handle)
2817                           (condition-case code
2818                               (progn (mm-extern-cache-contents handle) t)
2819                             (error
2820                              (gnus-message 3 "%s" (error-message-string code))
2821                              (when (>= gnus-verbose 3) (sit-for 2))
2822                              nil)))
2823                       (progn
2824                         (setq handle (mm-handle-cache handle)
2825                               type (mm-handle-type handle))
2826                         (equal (car type) "text/html"))))
2827              (when (or (setq charset (mail-content-type-get type 'charset))
2828                        header
2829                        (not file))
2830                (setq tmp-file (mm-make-temp-file
2831                                ;; Do we need to care for 8.3 filenames?
2832                                "mm-" nil ".html")))
2833              ;; Add a meta html tag to specify charset and a header.
2834              (cond
2835               (header
2836                (let (title eheader body hcharset coding)
2837                  (with-temp-buffer
2838                    (mm-enable-multibyte)
2839                    (setq case-fold-search t)
2840                    (insert header "\n")
2841                    (setq title (message-fetch-field "subject"))
2842                    (goto-char (point-min))
2843                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2844                      (replace-match (cond ((match-beginning 1) "&lt;")
2845                                           ((match-beginning 2) "&gt;")
2846                                           (t "&amp;"))))
2847                    (goto-char (point-min))
2848                    (insert "<pre>\n")
2849                    (goto-char (point-max))
2850                    (insert "</pre>\n<hr>\n")
2851                    ;; We have to examine charset one by one since
2852                    ;; charset specified in parts might be different.
2853                    (if (eq charset 'gnus-decoded)
2854                        (setq charset 'utf-8
2855                              eheader (mm-encode-coding-string (buffer-string)
2856                                                               charset)
2857                              title (when title
2858                                      (mm-encode-coding-string title charset))
2859                              body (mm-encode-coding-string (mm-get-part handle)
2860                                                            charset))
2861                      (setq hcharset (mm-find-mime-charset-region (point-min)
2862                                                                  (point-max)))
2863                      (cond ((= (length hcharset) 1)
2864                             (setq hcharset (car hcharset)
2865                                   coding (mm-charset-to-coding-system
2866                                           hcharset)))
2867                            ((> (length hcharset) 1)
2868                             (setq hcharset 'utf-8
2869                                   coding hcharset)))
2870                      (if coding
2871                          (if charset
2872                              (progn
2873                                (setq body
2874                                      (mm-charset-to-coding-system charset))
2875                                (if (eq coding body)
2876                                    (setq eheader (mm-encode-coding-string
2877                                                   (buffer-string) coding)
2878                                          title (when title
2879                                                  (mm-encode-coding-string
2880                                                   title coding))
2881                                          body (mm-get-part handle))
2882                                  (setq charset 'utf-8
2883                                        eheader (mm-encode-coding-string
2884                                                 (buffer-string) charset)
2885                                        title (when title
2886                                                (mm-encode-coding-string
2887                                                 title charset))
2888                                        body (mm-encode-coding-string
2889                                              (mm-decode-coding-string
2890                                               (mm-get-part handle) body)
2891                                              charset))))
2892                            (setq charset hcharset
2893                                  eheader (mm-encode-coding-string
2894                                           (buffer-string) coding)
2895                                  title (when title
2896                                          (mm-encode-coding-string
2897                                           title coding))
2898                                  body (mm-get-part handle)))
2899                        (setq eheader (mm-string-as-unibyte (buffer-string))
2900                              body (mm-get-part handle))))
2901                    (erase-buffer)
2902                    (mm-disable-multibyte)
2903                    (insert body)
2904                    (when charset
2905                      (mm-add-meta-html-tag handle charset))
2906                    (when title
2907                      (goto-char (point-min))
2908                      (unless (search-forward "<title>" nil t)
2909                        (re-search-forward "<head>\\s-*" nil t)
2910                        (insert "<title>" title "</title>\n")))
2911                    (goto-char (point-min))
2912                    (or (re-search-forward
2913                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2914                        (re-search-forward
2915                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2916                    (insert eheader)
2917                    (mm-write-region (point-min) (point-max)
2918                                     tmp-file nil nil nil 'binary t))))
2919               (charset
2920                (mm-with-unibyte-buffer
2921                  (insert (if (eq charset 'gnus-decoded)
2922                              (mm-encode-coding-string
2923                               (mm-get-part handle)
2924                               (setq charset 'utf-8))
2925                            (mm-get-part handle)))
2926                  (if (or (mm-add-meta-html-tag handle charset)
2927                          (not file))
2928                      (mm-write-region (point-min) (point-max)
2929                                       tmp-file nil nil nil 'binary t)
2930                    (setq tmp-file nil))))
2931               (tmp-file
2932                (mm-save-part-to-file handle tmp-file)))
2933              (when tmp-file
2934                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2935              (add-hook 'gnus-summary-prepare-exit-hook
2936                        'gnus-article-browse-delete-temp-files)
2937              (add-hook 'gnus-exit-gnus-hook
2938                        (lambda  ()
2939                          (gnus-article-browse-delete-temp-files t)))
2940              ;; FIXME: Warn if there's an <img> tag?
2941              (browse-url-of-file (or tmp-file (expand-file-name file)))
2942              (setq showed t))
2943             ;; If multipart, recurse
2944             ((equal (mm-handle-media-supertype handle) "multipart")
2945              (when (gnus-article-browse-html-parts handle header)
2946                (setq showed t)))
2947             ((equal (mm-handle-media-type handle) "message/rfc822")
2948              (mm-with-multibyte-buffer
2949                (mm-insert-part handle)
2950                (setq handle (mm-dissect-buffer t t))
2951                (when (and (bufferp (car handle))
2952                           (stringp (car (mm-handle-type handle))))
2953                  (setq handle (list handle)))
2954                (when header
2955                  (article-decode-encoded-words)
2956                  (let ((gnus-visible-headers
2957                         (or (get 'gnus-visible-headers 'standard-value)
2958                             gnus-visible-headers)))
2959                    (article-hide-headers))
2960                  (goto-char (point-min))
2961                  (search-forward "\n\n" nil 'move)
2962                  (skip-chars-backward "\t\n ")
2963                  (setq header (buffer-substring (point-min) (point)))))
2964              (when (prog1
2965                        (gnus-article-browse-html-parts handle header)
2966                      (mm-destroy-parts handle))
2967                (setq showed t)))))
2968     showed))
2969
2970 ;; FIXME: Documentation in texi/gnus.texi missing.
2971 (defun gnus-article-browse-html-article (&optional arg)
2972   "View \"text/html\" parts of the current article with a WWW browser.
2973 The message header is added to the beginning of every html part unless
2974 the prefix argument ARG is given.
2975
2976 Warning: Spammers use links to images in HTML articles to verify
2977 whether you have read the message.  As
2978 `gnus-article-browse-html-article' passes the unmodified HTML
2979 content to the browser without eliminating these \"web bugs\" you
2980 should only use it for mails from trusted senders.
2981
2982 If you alwasy want to display HTML part in the browser, set
2983 `mm-text-html-renderer' to nil."
2984   ;; Cf. `mm-w3m-safe-url-regexp'
2985   (interactive "P")
2986   (if arg
2987       (gnus-summary-show-article)
2988     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
2989                                     gnus-visible-headers)))
2990       (gnus-summary-show-article)))
2991   (with-current-buffer gnus-article-buffer
2992     (let ((header (unless arg
2993                     (save-restriction
2994                       (widen)
2995                       (buffer-substring-no-properties
2996                        (goto-char (point-min))
2997                        (if (search-forward "\n\n" nil t)
2998                            (match-beginning 0)
2999                          (goto-char (point-max))
3000                          (skip-chars-backward "\t\n ")
3001                          (point))))))
3002           parts)
3003       (set-buffer gnus-original-article-buffer)
3004       (setq parts (mm-dissect-buffer t t))
3005       ;; If singlepart, enforce a list.
3006       (when (and (bufferp (car parts))
3007                  (stringp (car (mm-handle-type parts))))
3008         (setq parts (list parts)))
3009       ;; Process the list
3010       (unless (gnus-article-browse-html-parts parts header)
3011         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3012       (mm-destroy-parts parts)
3013       (unless arg
3014         (gnus-summary-show-article)))))
3015
3016 (defun article-hide-list-identifiers ()
3017   "Remove list identifies from the Subject header.
3018 The `gnus-list-identifiers' variable specifies what to do."
3019   (interactive)
3020   (let ((inhibit-point-motion-hooks t)
3021         (regexp (if (consp gnus-list-identifiers)
3022                     (mapconcat 'identity gnus-list-identifiers " *\\|")
3023                   gnus-list-identifiers))
3024         (inhibit-read-only t))
3025     (when regexp
3026       (save-excursion
3027         (save-restriction
3028           (article-narrow-to-head)
3029           (goto-char (point-min))
3030           (while (re-search-forward
3031                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3032                   nil t)
3033             (delete-region (match-beginning 2) (match-end 0))
3034             (beginning-of-line))
3035           (when (re-search-forward
3036                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3037             (delete-region (match-beginning 1) (match-end 1))))))))
3038
3039 (defun article-hide-pem (&optional arg)
3040   "Toggle hiding of any PEM headers and signatures in the current article.
3041 If given a negative prefix, always show; if given a positive prefix,
3042 always hide."
3043   (interactive (gnus-article-hidden-arg))
3044   (unless (gnus-article-check-hidden-text 'pem arg)
3045     (save-excursion
3046       (let ((inhibit-read-only t) end)
3047         (goto-char (point-min))
3048         ;; Hide the horrendously ugly "header".
3049         (when (and (search-forward
3050                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3051                     nil t)
3052                    (setq end (1+ (match-beginning 0))))
3053           (gnus-add-wash-type 'pem)
3054           (gnus-article-hide-text-type
3055            end
3056            (if (search-forward "\n\n" nil t)
3057                (match-end 0)
3058              (point-max))
3059            'pem)
3060           ;; Hide the trailer as well
3061           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3062                                 nil t)
3063             (gnus-article-hide-text-type
3064              (match-beginning 0) (match-end 0) 'pem)))))))
3065
3066 (defun article-strip-banner ()
3067   "Strip the banners specified by the `banner' group parameter and by
3068 `gnus-article-address-banner-alist'."
3069   (interactive)
3070   (save-excursion
3071     (save-restriction
3072       (let ((inhibit-point-motion-hooks t))
3073         (when (gnus-parameter-banner gnus-newsgroup-name)
3074           (article-really-strip-banner
3075            (gnus-parameter-banner gnus-newsgroup-name)))
3076         (when gnus-article-address-banner-alist
3077           ;; Note that the From header is decoded here, so it is
3078           ;; required that the *-extract-address-components function
3079           ;; supports non-ASCII text.
3080           (let ((from (save-restriction
3081                         (widen)
3082                         (article-narrow-to-head)
3083                         (mail-fetch-field "from"))))
3084             (when (and from
3085                        (setq from
3086                              (cadr (funcall gnus-extract-address-components
3087                                             from))))
3088               (catch 'found
3089                 (dolist (pair gnus-article-address-banner-alist)
3090                   (when (string-match (car pair) from)
3091                     (throw 'found
3092                            (article-really-strip-banner (cdr pair)))))))))))))
3093
3094 (defun article-really-strip-banner (banner)
3095   "Strip the banner specified by the argument."
3096   (save-excursion
3097     (save-restriction
3098       (let ((inhibit-point-motion-hooks t)
3099             (gnus-signature-limit nil)
3100             (inhibit-read-only t))
3101         (article-goto-body)
3102         (cond
3103          ((eq banner 'signature)
3104           (when (gnus-article-narrow-to-signature)
3105             (widen)
3106             (forward-line -1)
3107             (delete-region (point) (point-max))))
3108          ((symbolp banner)
3109           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3110               (while (re-search-forward banner nil t)
3111                 (delete-region (match-beginning 0) (match-end 0)))))
3112          ((stringp banner)
3113           (while (re-search-forward banner nil t)
3114             (delete-region (match-beginning 0) (match-end 0)))))))))
3115
3116 (defun article-babel ()
3117   "Translate article using an online translation service."
3118   (interactive)
3119   (require 'babel)
3120   (gnus-with-article-buffer
3121     (when (article-goto-body)
3122       (let* ((start (point))
3123              (end (point-max))
3124              (orig (buffer-substring start end))
3125              (trans (babel-as-string orig)))
3126         (save-restriction
3127           (narrow-to-region start end)
3128           (delete-region start end)
3129           (insert trans))))))
3130
3131 (defun article-hide-signature (&optional arg)
3132   "Hide the signature in the current article.
3133 If given a negative prefix, always show; if given a positive prefix,
3134 always hide."
3135   (interactive (gnus-article-hidden-arg))
3136   (unless (gnus-article-check-hidden-text 'signature arg)
3137     (save-excursion
3138       (save-restriction
3139         (let ((inhibit-read-only t))
3140           (when (gnus-article-narrow-to-signature)
3141             (gnus-article-hide-text-type
3142              (point-min) (point-max) 'signature))))))
3143   (gnus-set-mode-line 'article))
3144
3145 (defun article-strip-headers-in-body ()
3146   "Strip offensive headers from bodies."
3147   (interactive)
3148   (save-excursion
3149     (article-goto-body)
3150     (let ((case-fold-search t))
3151       (when (looking-at "x-no-archive:")
3152         (gnus-delete-line)))))
3153
3154 (defun article-strip-leading-blank-lines ()
3155   "Remove all blank lines from the beginning of the article."
3156   (interactive)
3157   (save-excursion
3158     (let ((inhibit-point-motion-hooks t)
3159           (inhibit-read-only t))
3160       (when (article-goto-body)
3161         (while (and (not (eobp))
3162                     (looking-at "[ \t]*$"))
3163           (gnus-delete-line))))))
3164
3165 (defun article-narrow-to-head ()
3166   "Narrow the buffer to the head of the message.
3167 Point is left at the beginning of the narrowed-to region."
3168   (narrow-to-region
3169    (goto-char (point-min))
3170    (if (search-forward "\n\n" nil 1)
3171        (1- (point))
3172      (point-max)))
3173   (goto-char (point-min)))
3174
3175 (defun article-goto-body ()
3176   "Place point at the start of the body."
3177   (goto-char (point-min))
3178   (cond
3179    ;; This variable is only bound when dealing with separate
3180    ;; MIME body parts.
3181    (article-goto-body-goes-to-point-min-p
3182     t)
3183    ((search-forward "\n\n" nil t)
3184     t)
3185    (t
3186     (goto-char (point-max))
3187     nil)))
3188
3189 (defun article-strip-multiple-blank-lines ()
3190   "Replace consecutive blank lines with one empty line."
3191   (interactive)
3192   (save-excursion
3193     (let ((inhibit-point-motion-hooks t)
3194           (inhibit-read-only t))
3195       ;; First make all blank lines empty.
3196       (article-goto-body)
3197       (while (re-search-forward "^[ \t]+$" nil t)
3198         (unless (gnus-annotation-in-region-p
3199                  (match-beginning 0) (match-end 0))
3200           (replace-match "" nil t)))
3201       ;; Then replace multiple empty lines with a single empty line.
3202       (article-goto-body)
3203       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3204         (unless (gnus-annotation-in-region-p
3205                  (match-beginning 0) (match-end 0))
3206           (delete-region (match-beginning 1) (match-end 1)))))))
3207
3208 (defun article-strip-leading-space ()
3209   "Remove all white space from the beginning of the lines in the article."
3210   (interactive)
3211   (save-excursion
3212     (let ((inhibit-point-motion-hooks t)
3213           (inhibit-read-only t))
3214       (article-goto-body)
3215       (while (re-search-forward "^[ \t]+" nil t)
3216         (replace-match "" t t)))))
3217
3218 (defun article-strip-trailing-space ()
3219   "Remove all white space from the end of the lines in the article."
3220   (interactive)
3221   (save-excursion
3222     (let ((inhibit-point-motion-hooks t)
3223           (inhibit-read-only t))
3224       (article-goto-body)
3225       (while (re-search-forward "[ \t]+$" nil t)
3226         (replace-match "" t t)))))
3227
3228 (defun article-strip-blank-lines ()
3229   "Strip leading, trailing and multiple blank lines."
3230   (interactive)
3231   (article-strip-leading-blank-lines)
3232   (article-remove-trailing-blank-lines)
3233   (article-strip-multiple-blank-lines))
3234
3235 (defun article-strip-all-blank-lines ()
3236   "Strip all blank lines."
3237   (interactive)
3238   (save-excursion
3239     (let ((inhibit-point-motion-hooks t)
3240           (inhibit-read-only t))
3241       (article-goto-body)
3242       (while (re-search-forward "^[ \t]*\n" nil t)
3243         (replace-match "" t t)))))
3244
3245 (defun gnus-article-narrow-to-signature ()
3246   "Narrow to the signature; return t if a signature is found, else nil."
3247   (let ((inhibit-point-motion-hooks t))
3248     (when (gnus-article-search-signature)
3249       (forward-line 1)
3250       ;; Check whether we have some limits to what we consider
3251       ;; to be a signature.
3252       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3253                       (list gnus-signature-limit)))
3254             limit limited)
3255         (while (setq limit (pop limits))
3256           (if (or (and (integerp limit)
3257                        (< (- (point-max) (point)) limit))
3258                   (and (floatp limit)
3259                        (< (count-lines (point) (point-max)) limit))
3260                   (and (functionp limit)
3261                        (funcall limit))
3262                   (and (stringp limit)
3263                        (not (re-search-forward limit nil t))))
3264               ()                        ; This limit did not succeed.
3265             (setq limited t
3266                   limits nil)))
3267         (unless limited
3268           (narrow-to-region (point) (point-max))
3269           t)))))
3270
3271 (defun gnus-article-search-signature ()
3272   "Search the current buffer for the signature separator.
3273 Put point at the beginning of the signature separator."
3274   (let ((cur (point)))
3275     (goto-char (point-max))
3276     (if (if (stringp gnus-signature-separator)
3277             (re-search-backward gnus-signature-separator nil t)
3278           (let ((seps gnus-signature-separator))
3279             (while (and seps
3280                         (not (re-search-backward (car seps) nil t)))
3281               (pop seps))
3282             seps))
3283         t
3284       (goto-char cur)
3285       nil)))
3286
3287 (defun gnus-article-hidden-arg ()
3288   "Return the current prefix arg as a number, or 0 if no prefix."
3289   (list (if current-prefix-arg
3290             (prefix-numeric-value current-prefix-arg)
3291           0)))
3292
3293 (defun gnus-article-check-hidden-text (type arg)
3294   "Return nil if hiding is necessary.
3295 Arg can be nil or a number.  nil and positive means hide, negative
3296 means show, 0 means toggle."
3297   (save-excursion
3298     (save-restriction
3299       (let ((hide (gnus-article-hidden-text-p type)))
3300         (cond
3301          ((or (null arg)
3302               (> arg 0))
3303           nil)
3304          ((< arg 0)
3305           (gnus-article-show-hidden-text type)
3306           t)
3307          (t
3308           (if (eq hide 'hidden)
3309               (progn
3310                 (gnus-article-show-hidden-text type)
3311                 t)
3312             nil)))))))
3313
3314 (defun gnus-article-hidden-text-p (type)
3315   "Say whether the current buffer contains hidden text of type TYPE."
3316   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3317     (while (and pos
3318                 (not (get-text-property pos 'invisible))
3319                 (not (get-text-property pos 'dummy-invisible)))
3320       (setq pos
3321             (text-property-any (1+ pos) (point-max) 'article-type type)))
3322     (if pos
3323         'hidden
3324       nil)))
3325
3326 (defun gnus-article-show-hidden-text (type &optional dummy)
3327   "Show all hidden text of type TYPE.
3328 Originally it is hide instead of DUMMY."
3329   (let ((inhibit-read-only t)
3330         (inhibit-point-motion-hooks t))
3331     (gnus-remove-text-properties-when
3332      'article-type type
3333      (point-min) (point-max)
3334      (cons 'article-type (cons type
3335                                gnus-hidden-properties)))
3336     (gnus-delete-wash-type type)))
3337
3338 (defconst article-time-units
3339   `((year . ,(* 365.25 24 60 60))
3340     (week . ,(* 7 24 60 60))
3341     (day . ,(* 24 60 60))
3342     (hour . ,(* 60 60))
3343     (minute . 60)
3344     (second . 1))
3345   "Mapping from time units to seconds.")
3346
3347 (defun gnus-article-forward-header ()
3348   "Move point to the start of the next header.
3349 If the current header is a continuation header, this can be several
3350 lines forward."
3351   (let ((ended nil))
3352     (while (not ended)
3353       (forward-line 1)
3354       (if (looking-at "[ \t]+[^ \t]")
3355           (forward-line 1)
3356         (setq ended t)))))
3357
3358 (defun article-date-ut (&optional type highlight)
3359   "Convert DATE date to universal time in the current article.
3360 If TYPE is `local', convert to local time; if it is `lapsed', output
3361 how much time has lapsed since DATE.  For `lapsed', the value of
3362 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
3363 should replace the \"Date:\" one, or should be added below it."
3364   (interactive (list 'ut t))
3365   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
3366          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
3367                              tdate-regexp)
3368                             ((eq type 'lapsed)
3369                              "^X-Sent:[ \t]")
3370                             (article-lapsed-timer
3371                              "^Date:[ \t]")
3372                             (t
3373                              tdate-regexp)))
3374          (case-fold-search t)
3375          (inhibit-read-only t)
3376          (inhibit-point-motion-hooks t)
3377          pos date bface eface)
3378     (save-excursion
3379       (save-restriction
3380         (widen)
3381         (goto-char (point-min))
3382         (while (or (setq date (get-text-property (setq pos (point))
3383                                                  'original-date))
3384                    (when (setq pos (next-single-property-change
3385                                     (point) 'original-date))
3386                      (setq date (get-text-property pos 'original-date))
3387                      t))
3388           (narrow-to-region pos (or (text-property-any pos (point-max)
3389                                                        'original-date nil)
3390                                     (point-max)))
3391           (goto-char (point-min))
3392           (when (re-search-forward tdate-regexp nil t)
3393             (setq bface (get-text-property (point-at-bol) 'face)
3394                   eface (get-text-property (1- (point-at-eol)) 'face)))
3395           (goto-char (point-min))
3396           (setq pos nil)
3397           ;; Delete any old Date headers.
3398           (while (re-search-forward date-regexp nil t)
3399             (if pos
3400                 (delete-region (point-at-bol) (progn
3401                                                 (gnus-article-forward-header)
3402                                                 (point)))
3403               (delete-region (point-at-bol) (progn
3404                                               (gnus-article-forward-header)
3405                                               (forward-char -1)
3406                                               (point)))
3407               (setq pos (point))))
3408           (when (and (not pos)
3409                      (re-search-forward tdate-regexp nil t))
3410             (forward-line 1))
3411           (gnus-goto-char pos)
3412           (insert (article-make-date-line date (or type 'ut)))
3413           (unless pos
3414             (insert "\n")
3415             (forward-line -1))
3416           ;; Do highlighting.
3417           (beginning-of-line)
3418           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3419             (put-text-property (match-beginning 1) (1+ (match-end 1))
3420                                'face bface)
3421             (put-text-property (match-beginning 2) (match-end 2)
3422                                'face eface))
3423           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3424           (goto-char (point-max))
3425           (widen))))))
3426
3427 (defun article-make-date-line (date type)
3428   "Return a DATE line of TYPE."
3429   (unless (memq type '(local ut original user iso8601 lapsed english))
3430     (error "Unknown conversion type: %s" type))
3431   (condition-case ()
3432       (let ((time (date-to-time date)))
3433         (cond
3434          ;; Convert to the local timezone.
3435          ((eq type 'local)
3436           (concat "Date: " (message-make-date time)))
3437          ;; Convert to Universal Time.
3438          ((eq type 'ut)
3439           (concat "Date: "
3440                   (substring
3441                    (message-make-date
3442                     (let* ((e (parse-time-string date))
3443                            (tm (apply 'encode-time e))
3444                            (ms (car tm))
3445                            (ls (- (cadr tm) (car (current-time-zone time)))))
3446                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3447                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3448                             (t (list ms ls)))))
3449                    0 -5)
3450                   "UT"))
3451          ;; Get the original date from the article.
3452          ((eq type 'original)
3453           (concat "Date: " (if (string-match "\n+$" date)
3454                                (substring date 0 (match-beginning 0))
3455                              date)))
3456          ;; Let the user define the format.
3457          ((eq type 'user)
3458           (let ((format (or (condition-case nil
3459                                 (with-current-buffer gnus-summary-buffer
3460                                   gnus-article-time-format)
3461                               (error nil))
3462                             gnus-article-time-format)))
3463             (if (functionp format)
3464                 (funcall format time)
3465               (concat "Date: " (format-time-string format time)))))
3466          ;; ISO 8601.
3467          ((eq type 'iso8601)
3468           (let ((tz (car (current-time-zone time))))
3469             (concat
3470              "Date: "
3471              (format-time-string "%Y%m%dT%H%M%S" time)
3472              (format "%s%02d%02d"
3473                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3474                      (/ (% (abs tz) 3600) 60)))))
3475          ;; Do an X-Sent lapsed format.
3476          ((eq type 'lapsed)
3477           ;; If the date is seriously mangled, the timezone functions are
3478           ;; liable to bug out, so we ignore all errors.
3479           (let* ((now (current-time))
3480                  (real-time (subtract-time now time))
3481                  (real-sec (and real-time
3482                                 (+ (* (float (car real-time)) 65536)
3483                                    (cadr real-time))))
3484                  (sec (and real-time (abs real-sec)))
3485                  num prev)
3486             (cond
3487              ((null real-time)
3488               "X-Sent: Unknown")
3489              ((zerop sec)
3490               "X-Sent: Now")
3491              (t
3492               (concat
3493                "X-Sent: "
3494                ;; This is a bit convoluted, but basically we go
3495                ;; through the time units for years, weeks, etc,
3496                ;; and divide things to see whether that results
3497                ;; in positive answers.
3498                (mapconcat
3499                 (lambda (unit)
3500                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3501                       ;; The (remaining) seconds are too few to
3502                       ;; be divided into this time unit.
3503                       ""
3504                     ;; It's big enough, so we output it.
3505                     (setq sec (- sec (* num (cdr unit))))
3506                     (prog1
3507                         (concat (if prev ", " "") (int-to-string
3508                                                    (floor num))
3509                                 " " (symbol-name (car unit))
3510                                 (if (> num 1) "s" ""))
3511                       (setq prev t))))
3512                 article-time-units "")
3513                ;; If dates are odd, then it might appear like the
3514                ;; article was sent in the future.
3515                (if (> real-sec 0)
3516                    " ago"
3517                  " in the future"))))))
3518          ;; Display the date in proper English
3519          ((eq type 'english)
3520           (let ((dtime (decode-time time)))
3521             (concat
3522              "Date: the "
3523              (number-to-string (nth 3 dtime))
3524              (let ((digit (% (nth 3 dtime) 10)))
3525                (cond
3526                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3527                 ((= digit 1) "st")
3528                 ((= digit 2) "nd")
3529                 ((= digit 3) "rd")
3530                 (t "th")))
3531              " of "
3532              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3533              " "
3534              (number-to-string (nth 5 dtime))
3535              " at "
3536              (format "%02d" (nth 2 dtime))
3537              ":"
3538              (format "%02d" (nth 1 dtime)))))))
3539     (error
3540      (format "Date: %s (from Gnus)" date))))
3541
3542 (defun article-date-local (&optional highlight)
3543   "Convert the current article date to the local timezone."
3544   (interactive (list t))
3545   (article-date-ut 'local highlight))
3546
3547 (defun article-date-english (&optional highlight)
3548   "Convert the current article date to something that is proper English."
3549   (interactive (list t))
3550   (article-date-ut 'english highlight))
3551
3552 (defun article-date-original (&optional highlight)
3553   "Convert the current article date to what it was originally.
3554 This is only useful if you have used some other date conversion
3555 function and want to see what the date was before converting."
3556   (interactive (list t))
3557   (article-date-ut 'original highlight))
3558
3559 (defun article-date-lapsed (&optional highlight)
3560   "Convert the current article date to time lapsed since it was sent."
3561   (interactive (list t))
3562   (article-date-ut 'lapsed highlight))
3563
3564 (defun article-update-date-lapsed ()
3565   "Function to be run from a timer to update the lapsed time line."
3566   (save-match-data
3567     (let (deactivate-mark)
3568       (save-excursion
3569         (ignore-errors
3570          (walk-windows
3571           (lambda (w)
3572             (set-buffer (window-buffer w))
3573             (when (eq major-mode 'gnus-article-mode)
3574               (let ((mark (point-marker)))
3575                 (goto-char (point-min))
3576                 (when (re-search-forward "^X-Sent:" nil t)
3577                   (article-date-lapsed t))
3578                 (goto-char (marker-position mark))
3579                 (move-marker mark nil))))
3580           nil 'visible))))))
3581
3582 (defun gnus-start-date-timer (&optional n)
3583   "Start a timer to update the X-Sent header in the article buffers.
3584 The numerical prefix says how frequently (in seconds) the function
3585 is to run."
3586   (interactive "p")
3587   (unless n
3588     (setq n 1))
3589   (gnus-stop-date-timer)
3590   (setq article-lapsed-timer
3591         (run-at-time 1 n 'article-update-date-lapsed)))
3592
3593 (defun gnus-stop-date-timer ()
3594   "Stop the X-Sent timer."
3595   (interactive)
3596   (when article-lapsed-timer
3597     (nnheader-cancel-timer article-lapsed-timer)
3598     (setq article-lapsed-timer nil)))
3599
3600 (defun article-date-user (&optional highlight)
3601   "Convert the current article date to the user-defined format.
3602 This format is defined by the `gnus-article-time-format' variable."
3603   (interactive (list t))
3604   (article-date-ut 'user highlight))
3605
3606 (defun article-date-iso8601 (&optional highlight)
3607   "Convert the current article date to ISO8601."
3608   (interactive (list t))
3609   (article-date-ut 'iso8601 highlight))
3610
3611 (defmacro gnus-article-save-original-date (&rest forms)
3612   "Save the original date as a text property and evaluate FORMS."
3613   `(let* ((case-fold-search t)
3614           (start (progn
3615                    (goto-char (point-min))
3616                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3617                               (not (bolp)))
3618                      (match-end 0))))
3619           (date (when (and start
3620                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3621                                               nil t))
3622                   (buffer-substring-no-properties start
3623                                                   (match-beginning 0)))))
3624      (goto-char (point-max))
3625      (skip-chars-backward "\n")
3626      (put-text-property (point-min) (point) 'original-date date)
3627      ,@forms
3628      (goto-char (point-max))
3629      (skip-chars-backward "\n")
3630      (put-text-property (point-min) (point) 'original-date date)))
3631
3632 ;; (defun article-show-all ()
3633 ;;   "Show all hidden text in the article buffer."
3634 ;;   (interactive)
3635 ;;   (save-excursion
3636 ;;     (let ((inhibit-read-only t))
3637 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3638
3639 (defun article-remove-leading-whitespace ()
3640   "Remove excessive whitespace from all headers."
3641   (interactive)
3642   (save-excursion
3643     (save-restriction
3644       (let ((inhibit-read-only t))
3645         (article-narrow-to-head)
3646         (goto-char (point-min))
3647         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3648           (delete-region (match-beginning 1) (match-end 1)))))))
3649
3650 (defun article-emphasize (&optional arg)
3651   "Emphasize text according to `gnus-emphasis-alist'."
3652   (interactive (gnus-article-hidden-arg))
3653   (unless (gnus-article-check-hidden-text 'emphasis arg)
3654     (save-excursion
3655       (let ((alist (or
3656                     (condition-case nil
3657                         (with-current-buffer gnus-summary-buffer
3658                           gnus-article-emphasis-alist)
3659                       (error))
3660                     gnus-emphasis-alist))
3661             (inhibit-read-only t)
3662             (props (append '(article-type emphasis)
3663                            gnus-hidden-properties))
3664             regexp elem beg invisible visible face)
3665         (article-goto-body)
3666         (setq beg (point))
3667         (while (setq elem (pop alist))
3668           (goto-char beg)
3669           (setq regexp (car elem)
3670                 invisible (nth 1 elem)
3671                 visible (nth 2 elem)
3672                 face (nth 3 elem))
3673           (while (re-search-forward regexp nil t)
3674             (when (and (match-beginning visible) (match-beginning invisible))
3675               (gnus-article-hide-text
3676                (match-beginning invisible) (match-end invisible) props)
3677               (gnus-article-unhide-text-type
3678                (match-beginning visible) (match-end visible) 'emphasis)
3679               (gnus-put-overlay-excluding-newlines
3680                (match-beginning visible) (match-end visible) 'face face)
3681               (gnus-add-wash-type 'emphasis)
3682               (goto-char (match-end invisible)))))))))
3683
3684 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3685   "Setup newsgroup emphasis alist."
3686   (unless gnus-article-emphasis-alist
3687     (let ((name (and gnus-newsgroup-name
3688                      (gnus-group-real-name gnus-newsgroup-name))))
3689       (make-local-variable 'gnus-article-emphasis-alist)
3690       (setq gnus-article-emphasis-alist
3691             (nconc
3692              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3693                (while (setq elem (pop alist))
3694                  (when (and name (string-match (car elem) name))
3695                    (setq alist nil
3696                          highlight (copy-sequence (cdr elem)))))
3697                highlight)
3698              (copy-sequence highlight-words)
3699              (if gnus-newsgroup-name
3700                  (copy-sequence (gnus-group-find-parameter
3701                                  gnus-newsgroup-name 'highlight-words t)))
3702              gnus-emphasis-alist)))))
3703
3704 (defvar gnus-summary-article-menu)
3705 (defvar gnus-summary-post-menu)
3706
3707 ;;; Saving functions.
3708
3709 (defun gnus-article-save (save-buffer file &optional num)
3710   "Save the currently selected article."
3711   (when (or (get gnus-default-article-saver :headers)
3712             (not gnus-save-all-headers))
3713     ;; Remove headers according to `gnus-saved-headers' or the value
3714     ;; of the `:headers' property that the saver function might have.
3715     (let ((gnus-visible-headers
3716            (or (symbol-value (get gnus-default-article-saver :headers))
3717                gnus-saved-headers gnus-visible-headers))
3718           (gnus-article-buffer save-buffer))
3719       (with-current-buffer save-buffer
3720         (article-hide-headers 1 t))))
3721   (save-window-excursion
3722     (if (not gnus-default-article-saver)
3723         (error "No default saver is defined")
3724       ;; !!! Magic!  The saving functions all save
3725       ;; `gnus-save-article-buffer' (or so they think), but we
3726       ;; bind that variable to our save-buffer.
3727       (set-buffer gnus-article-buffer)
3728       (let* ((gnus-save-article-buffer save-buffer)
3729              (filename
3730               (cond
3731                ((not gnus-prompt-before-saving) 'default)
3732                ((eq gnus-prompt-before-saving 'always) nil)
3733                (t file)))
3734              (gnus-number-of-articles-to-be-saved
3735               (when (eq gnus-prompt-before-saving t)
3736                 num)))                  ; Magic
3737         (set-buffer gnus-article-current-summary)
3738         (funcall gnus-default-article-saver filename)))))
3739
3740 (defun gnus-read-save-file-name (prompt &optional filename
3741                                         function group headers variable
3742                                         dir-var)
3743   (let ((default-name
3744           (funcall function group headers (symbol-value variable)))
3745         result)
3746     (setq result
3747           (expand-file-name
3748            (cond
3749             ((eq filename 'default)
3750              default-name)
3751             ((eq filename t)
3752              default-name)
3753             (filename filename)
3754             (t
3755              (when (symbol-value dir-var)
3756                (setq default-name (expand-file-name
3757                                    (file-name-nondirectory default-name)
3758                                    (symbol-value dir-var))))
3759              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3760                     (prompt
3761                      (format prompt
3762                              (if (and gnus-number-of-articles-to-be-saved
3763                                       (> gnus-number-of-articles-to-be-saved 1))
3764                                  (format "these %d articles"
3765                                          gnus-number-of-articles-to-be-saved)
3766                                "this article")))
3767                     (file
3768                      ;; Let the split methods have their say.
3769                      (cond
3770                       ;; No split name was found.
3771                       ((null split-name)
3772                        (read-file-name
3773                         (concat prompt " (default "
3774                                 (file-name-nondirectory default-name) "): ")
3775                         (file-name-directory default-name)
3776                         default-name))
3777                       ;; A single group name is returned.
3778                       ((stringp split-name)
3779                        (setq default-name
3780                              (funcall function split-name headers
3781                                       (symbol-value variable)))
3782                        (read-file-name
3783                         (concat prompt " (default "
3784                                 (file-name-nondirectory default-name) "): ")
3785                         (file-name-directory default-name)
3786                         default-name))
3787                       ;; A single split name was found
3788                       ((= 1 (length split-name))
3789                        (let* ((name (expand-file-name
3790                                      (car split-name)
3791                                      gnus-article-save-directory))
3792                               (dir (cond ((file-directory-p name)
3793                                           (file-name-as-directory name))
3794                                          ((file-exists-p name) name)
3795                                          (t gnus-article-save-directory))))
3796                          (read-file-name
3797                           (concat prompt " (default " name "): ")
3798                           dir name)))
3799                       ;; A list of splits was found.
3800                       (t
3801                        (setq split-name (nreverse split-name))
3802                        (let (result)
3803                          (let ((file-name-history
3804                                 (nconc split-name file-name-history)))
3805                            (setq result
3806                                  (expand-file-name
3807                                   (read-file-name
3808                                    (concat prompt " (`M-p' for defaults): ")
3809                                    gnus-article-save-directory
3810                                    (car split-name))
3811                                   gnus-article-save-directory)))
3812                          (car (push result file-name-history)))))))
3813                ;; Create the directory.
3814                (gnus-make-directory (file-name-directory file))
3815                ;; If we have read a directory, we append the default file name.
3816                (when (file-directory-p file)
3817                  (setq file (expand-file-name (file-name-nondirectory
3818                                                default-name)
3819                                               (file-name-as-directory file))))
3820                ;; Possibly translate some characters.
3821                (nnheader-translate-file-chars file))))))
3822     (gnus-make-directory (file-name-directory result))
3823     (when variable
3824       (set variable result))
3825     (when dir-var
3826       (set dir-var (file-name-directory result)))
3827     result))
3828
3829 (defun gnus-article-archive-name (group)
3830   "Return the first instance of an \"Archive-name\" in the current buffer."
3831   (let ((case-fold-search t))
3832     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3833       (nnheader-concat gnus-article-save-directory
3834                        (match-string 1)))))
3835
3836 (defun gnus-article-nndoc-name (group)
3837   "If GROUP is an nndoc group, return the name of the parent group."
3838   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3839     (gnus-group-get-parameter group 'save-article-group)))
3840
3841 (defun gnus-summary-save-in-rmail (&optional filename)
3842   "Append this article to Rmail file.
3843 Optional argument FILENAME specifies file name.
3844 Directory to save to is default to `gnus-article-save-directory'."
3845   (setq filename (gnus-read-save-file-name
3846                   "Save %s in rmail file" filename
3847                   gnus-rmail-save-name gnus-newsgroup-name
3848                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3849   (gnus-eval-in-buffer-window gnus-save-article-buffer
3850     (save-excursion
3851       (save-restriction
3852         (widen)
3853         (gnus-output-to-rmail filename))))
3854   filename)
3855
3856 (defun gnus-summary-save-in-mail (&optional filename)
3857   "Append this article to Unix mail file.
3858 Optional argument FILENAME specifies file name.
3859 Directory to save to is default to `gnus-article-save-directory'."
3860   (setq filename (gnus-read-save-file-name
3861                   "Save %s in Unix mail file" filename
3862                   gnus-mail-save-name gnus-newsgroup-name
3863                   gnus-current-headers 'gnus-newsgroup-last-mail))
3864   (gnus-eval-in-buffer-window gnus-save-article-buffer
3865     (save-excursion
3866       (save-restriction
3867         (widen)
3868         (if (and (file-readable-p filename)
3869                  (file-regular-p filename)
3870                  (mail-file-babyl-p filename))
3871             (rmail-output-to-rmail-file filename t)
3872           (gnus-output-to-mail filename)))))
3873   filename)
3874
3875 (put 'gnus-summary-save-in-file :decode t)
3876 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
3877 (defun gnus-summary-save-in-file (&optional filename overwrite)
3878   "Append this article to file.
3879 Optional argument FILENAME specifies file name.
3880 Directory to save to is default to `gnus-article-save-directory'."
3881   (setq filename (gnus-read-save-file-name
3882                   "Save %s in file" filename
3883                   gnus-file-save-name gnus-newsgroup-name
3884                   gnus-current-headers 'gnus-newsgroup-last-file))
3885   (gnus-eval-in-buffer-window gnus-save-article-buffer
3886     (save-excursion
3887       (save-restriction
3888         (widen)
3889         (when (and overwrite
3890                    (file-exists-p filename))
3891           (delete-file filename))
3892         (gnus-output-to-file filename))))
3893   filename)
3894
3895 (put 'gnus-summary-write-to-file :decode t)
3896 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
3897 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
3898 (defun gnus-summary-write-to-file (&optional filename)
3899   "Write this article to a file, overwriting it if the file exists.
3900 Optional argument FILENAME specifies file name.
3901 The directory to save in defaults to `gnus-article-save-directory'."
3902   (setq filename (gnus-read-save-file-name
3903                   "Save %s in file" filename
3904                   gnus-file-save-name gnus-newsgroup-name
3905                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
3906   (gnus-summary-save-in-file filename t))
3907
3908 (put 'gnus-summary-save-body-in-file :decode t)
3909 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
3910   "Append this article body to a file.
3911 Optional argument FILENAME specifies file name.
3912 The directory to save in defaults to `gnus-article-save-directory'."
3913   (setq filename (gnus-read-save-file-name
3914                   "Save %s body in file" filename
3915                   gnus-file-save-name gnus-newsgroup-name
3916                   gnus-current-headers 'gnus-newsgroup-last-file))
3917   (gnus-eval-in-buffer-window gnus-save-article-buffer
3918     (save-excursion
3919       (save-restriction
3920         (widen)
3921         (when (article-goto-body)
3922           (narrow-to-region (point) (point-max)))
3923         (when (and overwrite
3924                    (file-exists-p filename))
3925           (delete-file filename))
3926         (gnus-output-to-file filename))))
3927   filename)
3928
3929 (put 'gnus-summary-write-body-to-file :decode t)
3930 (put 'gnus-summary-write-body-to-file
3931      :function 'gnus-summary-save-body-in-file)
3932 (defun gnus-summary-write-body-to-file (&optional filename)
3933   "Write this article body to a file, overwriting it if the file exists.
3934 Optional argument FILENAME specifies file name.
3935 The directory to save in defaults to `gnus-article-save-directory'."
3936   (setq filename (gnus-read-save-file-name
3937                   "Save %s body in file" filename
3938                   gnus-file-save-name gnus-newsgroup-name
3939                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
3940   (gnus-summary-save-body-in-file filename t))
3941
3942 (defun gnus-summary-save-in-pipe (&optional command)
3943   "Pipe this article to subprocess."
3944   (setq command
3945         (cond ((and (eq command 'default)
3946                     gnus-last-shell-command)
3947                gnus-last-shell-command)
3948               ((stringp command)
3949                command)
3950               (t (read-string
3951                   (format
3952                    "Shell command on %s: "
3953                    (if (and gnus-number-of-articles-to-be-saved
3954                             (> gnus-number-of-articles-to-be-saved 1))
3955                        (format "these %d articles"
3956                                gnus-number-of-articles-to-be-saved)
3957                      "this article"))
3958                   gnus-last-shell-command))))
3959   (when (string-equal command "")
3960     (if gnus-last-shell-command
3961         (setq command gnus-last-shell-command)
3962       (error "A command is required")))
3963   (gnus-eval-in-buffer-window gnus-article-buffer
3964     (save-restriction
3965       (widen)
3966       (shell-command-on-region (point-min) (point-max) command nil)))
3967   (setq gnus-last-shell-command command))
3968
3969 (defun gnus-summary-pipe-to-muttprint (&optional command)
3970   "Pipe this article to muttprint."
3971   (setq command (read-string
3972                  "Print using command: " gnus-summary-muttprint-program
3973                  nil gnus-summary-muttprint-program))
3974   (gnus-summary-save-in-pipe command))
3975
3976 ;;; Article file names when saving.
3977
3978 (defun gnus-capitalize-newsgroup (newsgroup)
3979   "Capitalize NEWSGROUP name."
3980   (when (not (zerop (length newsgroup)))
3981     (concat (char-to-string (upcase (aref newsgroup 0)))
3982             (substring newsgroup 1))))
3983
3984 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
3985   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3986 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
3987 Otherwise, it is like ~/News/news/group/num."
3988   (let ((default
3989           (expand-file-name
3990            (concat (if (gnus-use-long-file-name 'not-save)
3991                        (gnus-capitalize-newsgroup newsgroup)
3992                      (gnus-newsgroup-directory-form newsgroup))
3993                    "/" (int-to-string (mail-header-number headers)))
3994            gnus-article-save-directory)))
3995     (if (and last-file
3996              (string-equal (file-name-directory default)
3997                            (file-name-directory last-file))
3998              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3999         default
4000       (or last-file default))))
4001
4002 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4003   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4004 If variable `gnus-use-long-file-name' is non-nil, it is
4005 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4006   (let ((default
4007           (expand-file-name
4008            (concat (if (gnus-use-long-file-name 'not-save)
4009                        newsgroup
4010                      (gnus-newsgroup-directory-form newsgroup))
4011                    "/" (int-to-string (mail-header-number headers)))
4012            gnus-article-save-directory)))
4013     (if (and last-file
4014              (string-equal (file-name-directory default)
4015                            (file-name-directory last-file))
4016              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4017         default
4018       (or last-file default))))
4019
4020 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4021   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4022 If variable `gnus-use-long-file-name' is non-nil, it is
4023 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4024   (or last-file
4025       (expand-file-name
4026        (if (gnus-use-long-file-name 'not-save)
4027            newsgroup
4028          (file-relative-name
4029           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4030           default-directory))
4031        gnus-article-save-directory)))
4032
4033 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4034   "Generate file name from sender."
4035   (let ((from (mail-header-from headers)))
4036     (expand-file-name
4037      (if (and from (string-match "\\([^ <]+\\)@" from))
4038          (match-string 1 from)
4039        "nobody")
4040      gnus-article-save-directory)))
4041
4042 (defun article-verify-x-pgp-sig ()
4043   "Verify X-PGP-Sig."
4044   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4045   (interactive)
4046   (if (gnus-buffer-live-p gnus-original-article-buffer)
4047       (let ((sig (with-current-buffer gnus-original-article-buffer
4048                    (gnus-fetch-field "X-PGP-Sig")))
4049             items info headers)
4050         (when (and sig
4051                    mml2015-use
4052                    (mml2015-clear-verify-function))
4053           (with-temp-buffer
4054             (insert-buffer-substring gnus-original-article-buffer)
4055             (setq items (split-string sig))
4056             (message-narrow-to-head)
4057             (let ((inhibit-point-motion-hooks t)
4058                   (case-fold-search t))
4059               ;; Don't verify multiple headers.
4060               (setq headers (mapconcat (lambda (header)
4061                                          (concat header ": "
4062                                                  (mail-fetch-field header)
4063                                                  "\n"))
4064                                        (split-string (nth 1 items) ",") "")))
4065             (delete-region (point-min) (point-max))
4066             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4067             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4068             (insert headers)
4069             (widen)
4070             (forward-line)
4071             (while (not (eobp))
4072               (if (looking-at "^-")
4073                   (insert "- "))
4074               (forward-line))
4075             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4076             (insert "Version: " (car items) "\n\n")
4077             (insert (mapconcat 'identity (cddr items) "\n"))
4078             (insert "\n-----END PGP SIGNATURE-----\n")
4079             (let ((mm-security-handle (list (format "multipart/signed"))))
4080               (mml2015-clean-buffer)
4081               (let ((coding-system-for-write (or gnus-newsgroup-charset
4082                                                  'iso-8859-1)))
4083                 (funcall (mml2015-clear-verify-function)))
4084               (setq info
4085                     (or (mm-handle-multipart-ctl-parameter
4086                          mm-security-handle 'gnus-details)
4087                         (mm-handle-multipart-ctl-parameter
4088                          mm-security-handle 'gnus-info)))))
4089           (when info
4090             (let ((inhibit-read-only t) bface eface)
4091               (save-restriction
4092                 (message-narrow-to-head)
4093                 (goto-char (point-max))
4094                 (forward-line -1)
4095                 (setq bface (get-text-property (point-at-bol) 'face)
4096                       eface (get-text-property (1- (point-at-eol)) 'face))
4097                 (message-remove-header "X-Gnus-PGP-Verify")
4098                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4099                     (forward-line)
4100                   (goto-char (point-max)))
4101                 (narrow-to-region (point) (point))
4102                 (insert "X-Gnus-PGP-Verify: " info "\n")
4103                 (goto-char (point-min))
4104                 (forward-line)
4105                 (while (not (eobp))
4106                   (if (not (looking-at "^[ \t]"))
4107                       (insert " "))
4108                   (forward-line))
4109                 ;; Do highlighting.
4110                 (goto-char (point-min))
4111                 (when (looking-at "\\([^:]+\\): *")
4112                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4113                                      'face bface)
4114                   (put-text-property (match-end 0) (point-max)
4115                                      'face eface)))))))))
4116
4117 (defun article-verify-cancel-lock ()
4118   "Verify Cancel-Lock header."
4119   (interactive)
4120   (if (gnus-buffer-live-p gnus-original-article-buffer)
4121       (canlock-verify gnus-original-article-buffer)))
4122
4123 (eval-and-compile
4124   (mapc
4125    (lambda (func)
4126      (let (afunc gfunc)
4127        (if (consp func)
4128            (setq afunc (car func)
4129                  gfunc (cdr func))
4130          (setq afunc func
4131                gfunc (intern (format "gnus-%s" func))))
4132        (defalias gfunc
4133          (when (fboundp afunc)
4134            `(lambda (&optional interactive &rest args)
4135               ,(documentation afunc t)
4136               (interactive (list t))
4137               (with-current-buffer gnus-article-buffer
4138                 (if interactive
4139                     (call-interactively ',afunc)
4140                   (apply ',afunc args))))))))
4141    '(article-hide-headers
4142      article-verify-x-pgp-sig
4143      article-verify-cancel-lock
4144      article-hide-boring-headers
4145      article-treat-overstrike
4146      article-treat-ansi-sequences
4147      article-fill-long-lines
4148      article-capitalize-sentences
4149      article-remove-cr
4150      article-remove-leading-whitespace
4151      article-display-x-face
4152      article-display-face
4153      article-de-quoted-unreadable
4154      article-de-base64-unreadable
4155      article-decode-HZ
4156      article-wash-html
4157      article-unsplit-urls
4158      article-hide-list-identifiers
4159      article-strip-banner
4160      article-babel
4161      article-hide-pem
4162      article-hide-signature
4163      article-strip-headers-in-body
4164      article-remove-trailing-blank-lines
4165      article-strip-leading-blank-lines
4166      article-strip-multiple-blank-lines
4167      article-strip-leading-space
4168      article-strip-trailing-space
4169      article-strip-blank-lines
4170      article-strip-all-blank-lines
4171      article-date-local
4172      article-date-english
4173      article-date-iso8601
4174      article-date-original
4175      article-date-ut
4176      article-decode-mime-words
4177      article-decode-charset
4178      article-decode-encoded-words
4179      article-date-user
4180      article-date-lapsed
4181      article-emphasize
4182      article-treat-dumbquotes
4183      article-normalize-headers
4184      ;;(article-show-all . gnus-article-show-all-headers)
4185      )))
4186 \f
4187 ;;;
4188 ;;; Gnus article mode
4189 ;;;
4190
4191 (put 'gnus-article-mode 'mode-class 'special)
4192
4193 (set-keymap-parent gnus-article-mode-map widget-keymap)
4194
4195 (gnus-define-keys gnus-article-mode-map
4196   " " gnus-article-goto-next-page
4197   "\177" gnus-article-goto-prev-page
4198   [delete] gnus-article-goto-prev-page
4199   [backspace] gnus-article-goto-prev-page
4200   "\C-c^" gnus-article-refer-article
4201   "h" gnus-article-show-summary
4202   "s" gnus-article-show-summary
4203   "\C-c\C-m" gnus-article-mail
4204   "?" gnus-article-describe-briefly
4205   "e" gnus-summary-edit-article
4206   "<" beginning-of-buffer
4207   ">" end-of-buffer
4208   "\C-c\C-i" gnus-info-find-node
4209   "\C-c\C-b" gnus-bug
4210   "R" gnus-article-reply-with-original
4211   "F" gnus-article-followup-with-original
4212   "\C-hk" gnus-article-describe-key
4213   "\C-hc" gnus-article-describe-key-briefly
4214   "\C-hb" gnus-article-describe-bindings
4215
4216   "\C-d" gnus-article-read-summary-keys
4217   "\M-*" gnus-article-read-summary-keys
4218   "\M-#" gnus-article-read-summary-keys
4219   "\M-^" gnus-article-read-summary-keys
4220   "\M-g" gnus-article-read-summary-keys)
4221
4222 (substitute-key-definition
4223  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4224
4225 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4226   "W" gnus-article-wide-reply-with-original)
4227 (if (featurep 'xemacs)
4228     (set-keymap-default-binding gnus-article-send-map
4229                                 'gnus-article-read-summary-send-keys)
4230   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4231
4232 (defun gnus-article-make-menu-bar ()
4233   (unless (boundp 'gnus-article-commands-menu)
4234     (gnus-summary-make-menu-bar))
4235   (gnus-turn-off-edit-menu 'article)
4236   (unless (boundp 'gnus-article-article-menu)
4237     (easy-menu-define
4238      gnus-article-article-menu gnus-article-mode-map ""
4239      '("Article"
4240        ["Scroll forwards" gnus-article-goto-next-page t]
4241        ["Scroll backwards" gnus-article-goto-prev-page t]
4242        ["Show summary" gnus-article-show-summary t]
4243        ["Fetch Message-ID at point" gnus-article-refer-article t]
4244        ["Mail to address at point" gnus-article-mail t]
4245        ["Send a bug report" gnus-bug t]))
4246
4247     (easy-menu-define
4248      gnus-article-treatment-menu gnus-article-mode-map ""
4249      ;; Fixme: this should use :active (and maybe :visible).
4250      '("Treatment"
4251        ["Hide headers" gnus-article-hide-headers t]
4252        ["Hide signature" gnus-article-hide-signature t]
4253        ["Hide citation" gnus-article-hide-citation t]
4254        ["Treat overstrike" gnus-article-treat-overstrike t]
4255        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4256        ["Remove carriage return" gnus-article-remove-cr t]
4257        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4258        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4259        ["Remove base64" gnus-article-de-base64-unreadable t]
4260        ["Treat html" gnus-article-wash-html t]
4261        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4262        ["Decode HZ" gnus-article-decode-HZ t]))
4263
4264     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4265
4266     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4267
4268     (gnus-run-hooks 'gnus-article-menu-hook)))
4269
4270 (defun gnus-article-mode ()
4271   "Major mode for displaying an article.
4272
4273 All normal editing commands are switched off.
4274
4275 The following commands are available in addition to all summary mode
4276 commands:
4277 \\<gnus-article-mode-map>
4278 \\[gnus-article-next-page]\t Scroll the article one page forwards
4279 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4280 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4281 \\[gnus-article-show-summary]\t Display the summary buffer
4282 \\[gnus-article-mail]\t Send a reply to the address near point
4283 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4284 \\[gnus-info-find-node]\t Go to the Gnus info node"
4285   (interactive)
4286   (kill-all-local-variables)
4287   (gnus-simplify-mode-line)
4288   (setq mode-name "Article")
4289   (setq major-mode 'gnus-article-mode)
4290   (make-local-variable 'minor-mode-alist)
4291   (use-local-map gnus-article-mode-map)
4292   (when (gnus-visual-p 'article-menu 'menu)
4293     (gnus-article-make-menu-bar)
4294     (when gnus-summary-tool-bar-map
4295       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4296   (gnus-update-format-specifications nil 'article-mode)
4297   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4298   (set (make-local-variable 'gnus-page-broken) nil)
4299   (make-local-variable 'gnus-button-marker-list)
4300   (make-local-variable 'gnus-article-current-summary)
4301   (make-local-variable 'gnus-article-mime-handles)
4302   (make-local-variable 'gnus-article-decoded-p)
4303   (make-local-variable 'gnus-article-mime-handle-alist)
4304   (make-local-variable 'gnus-article-wash-types)
4305   (make-local-variable 'gnus-article-image-alist)
4306   (make-local-variable 'gnus-article-charset)
4307   (make-local-variable 'gnus-article-ignored-charsets)
4308   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4309   ;; face.
4310   (set (make-local-variable 'nobreak-char-display) nil)
4311   (setq cursor-in-non-selected-windows nil)
4312   (setq truncate-lines gnus-article-truncate-lines)
4313   (gnus-set-default-directory)
4314   (buffer-disable-undo)
4315   (setq buffer-read-only t
4316         show-trailing-whitespace nil)
4317   (set-syntax-table gnus-article-mode-syntax-table)
4318   (mm-enable-multibyte)
4319   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4320
4321 (defvar gnus-button-marker-list nil
4322   "Regexp matching any of the regexps from `gnus-button-alist'.
4323 Internal variable.")
4324
4325 (defun gnus-article-setup-buffer ()
4326   "Initialize the article buffer."
4327   (let* ((name (if gnus-single-article-buffer "*Article*"
4328                  (concat "*Article " gnus-newsgroup-name "*")))
4329          (original
4330           (progn (string-match "\\*Article" name)
4331                  (concat " *Original Article"
4332                          (substring name (match-end 0))))))
4333     (setq gnus-article-buffer name)
4334     (setq gnus-original-article-buffer original)
4335     (setq gnus-article-mime-handle-alist nil)
4336     (with-current-buffer gnus-summary-buffer
4337       ;; This might be a variable local to the summary buffer.
4338       (unless gnus-single-article-buffer
4339         (setq gnus-article-buffer name)
4340         (setq gnus-original-article-buffer original)
4341         (gnus-set-global-variables)))
4342     (gnus-article-setup-highlight-words)
4343     ;; Init original article buffer.
4344     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4345       (mm-enable-multibyte)
4346       (setq major-mode 'gnus-original-article-mode)
4347       (make-local-variable 'gnus-original-article))
4348     (if (and (get-buffer name)
4349              (with-current-buffer name
4350                (if gnus-article-edit-mode
4351                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4352                        (progn
4353                          (set-buffer-modified-p nil)
4354                          (gnus-kill-buffer name)
4355                          (message "")
4356                          nil)
4357                      (error "Action aborted"))
4358                  t)))
4359         (with-current-buffer name
4360           (set (make-local-variable 'gnus-article-edit-mode) nil)
4361           (when gnus-article-mime-handles
4362             (mm-destroy-parts gnus-article-mime-handles)
4363             (setq gnus-article-mime-handles nil))
4364           ;; Set it to nil in article-buffer!
4365           (setq gnus-article-mime-handle-alist nil)
4366           (buffer-disable-undo)
4367           (setq buffer-read-only t)
4368           ;; This list just keeps growing if we don't reset it.
4369           (setq gnus-button-marker-list nil)
4370           (unless (eq major-mode 'gnus-article-mode)
4371             (gnus-article-mode))
4372           (current-buffer))
4373       (with-current-buffer (gnus-get-buffer-create name)
4374         (gnus-article-mode)
4375         (make-local-variable 'gnus-summary-buffer)
4376         (setq gnus-summary-buffer
4377               (gnus-summary-buffer-name gnus-newsgroup-name))
4378         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4379         (current-buffer)))))
4380
4381 ;; Set article window start at LINE, where LINE is the number of lines
4382 ;; from the head of the article.
4383 (defun gnus-article-set-window-start (&optional line)
4384   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4385     (when article-window
4386       (set-window-start
4387        article-window
4388        (with-current-buffer gnus-article-buffer
4389          (goto-char (point-min))
4390          (if (not line)
4391              (point-min)
4392            (gnus-message 6 "Moved to bookmark")
4393            (search-forward "\n\n" nil t)
4394            (forward-line line)
4395            (point)))))))
4396
4397 (defun gnus-article-prepare (article &optional all-headers header)
4398   "Prepare ARTICLE in article mode buffer.
4399 ARTICLE should either be an article number or a Message-ID.
4400 If ARTICLE is an id, HEADER should be the article headers.
4401 If ALL-HEADERS is non-nil, no headers are hidden."
4402   (save-excursion
4403     ;; Make sure we start in a summary buffer.
4404     (unless (eq major-mode 'gnus-summary-mode)
4405       (set-buffer gnus-summary-buffer))
4406     (setq gnus-summary-buffer (current-buffer))
4407     (let* ((gnus-article (if header (mail-header-number header) article))
4408            (summary-buffer (current-buffer))
4409            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4410            (group gnus-newsgroup-name)
4411            result)
4412       (save-excursion
4413         (gnus-article-setup-buffer)
4414         (set-buffer gnus-article-buffer)
4415         ;; Deactivate active regions.
4416         (when (and (boundp 'transient-mark-mode)
4417                    transient-mark-mode)
4418           (setq mark-active nil))
4419         (if (not (setq result (let ((inhibit-read-only t))
4420                                 (gnus-request-article-this-buffer
4421                                  article group))))
4422             ;; There is no such article.
4423             (save-excursion
4424               (when (and (numberp article)
4425                          (not (memq article gnus-newsgroup-sparse)))
4426                 (setq gnus-article-current
4427                       (cons gnus-newsgroup-name article))
4428                 (set-buffer gnus-summary-buffer)
4429                 (setq gnus-current-article article)
4430                 (if (and (memq article gnus-newsgroup-undownloaded)
4431                          (not (gnus-online (gnus-find-method-for-group
4432                                             gnus-newsgroup-name))))
4433                     (progn
4434                       (gnus-summary-set-agent-mark article)
4435                       (message "Message marked for downloading"))
4436                   (gnus-summary-mark-article article gnus-canceled-mark)
4437                   (unless (memq article gnus-newsgroup-sparse)
4438                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4439           (if (or (eq result 'pseudo)
4440                   (eq result 'nneething))
4441               (progn
4442                 (with-current-buffer summary-buffer
4443                   (push article gnus-newsgroup-history)
4444                   (setq gnus-last-article gnus-current-article
4445                         gnus-current-article 0
4446                         gnus-current-headers nil
4447                         gnus-article-current nil)
4448                   (if (eq result 'nneething)
4449                       (gnus-configure-windows 'summary)
4450                     (gnus-configure-windows 'article))
4451                   (gnus-set-global-variables))
4452                 (let ((gnus-article-mime-handle-alist-1
4453                        gnus-article-mime-handle-alist))
4454                   (gnus-set-mode-line 'article)))
4455             ;; The result from the `request' was an actual article -
4456             ;; or at least some text that is now displayed in the
4457             ;; article buffer.
4458             (when (and (numberp article)
4459                        (not (eq article gnus-current-article)))
4460               ;; Seems like a new article has been selected.
4461               ;; `gnus-current-article' must be an article number.
4462               (with-current-buffer summary-buffer
4463                 (push article gnus-newsgroup-history)
4464                 (setq gnus-last-article gnus-current-article
4465                       gnus-current-article article
4466                       gnus-current-headers
4467                       (gnus-summary-article-header gnus-current-article)
4468                       gnus-article-current
4469                       (cons gnus-newsgroup-name gnus-current-article))
4470                 (unless (vectorp gnus-current-headers)
4471                   (setq gnus-current-headers nil))
4472                 (gnus-summary-goto-subject gnus-current-article)
4473                 (when (gnus-summary-show-thread)
4474                   ;; If the summary buffer really was folded, the
4475                   ;; previous goto may not actually have gone to
4476                   ;; the right article, but the thread root instead.
4477                   ;; So we go again.
4478                   (gnus-summary-goto-subject gnus-current-article))
4479                 (gnus-run-hooks 'gnus-mark-article-hook)
4480                 (gnus-set-mode-line 'summary)
4481                 (when (gnus-visual-p 'article-highlight 'highlight)
4482                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4483                 ;; Set the global newsgroup variables here.
4484                 (gnus-set-global-variables)
4485                 (setq gnus-have-all-headers
4486                       (or all-headers gnus-show-all-headers))))
4487             (save-excursion
4488               (gnus-configure-windows 'article))
4489             (when (or (numberp article)
4490                       (stringp article))
4491               (gnus-article-prepare-display)
4492               ;; Do page break.
4493               (goto-char (point-min))
4494               (when gnus-break-pages
4495                 (gnus-narrow-to-page)))
4496             (let ((gnus-article-mime-handle-alist-1
4497                    gnus-article-mime-handle-alist))
4498               (gnus-set-mode-line 'article))
4499             (article-goto-body)
4500             (unless (bobp)
4501               (forward-line -1))
4502             (set-window-point (get-buffer-window (current-buffer)) (point))
4503             (gnus-configure-windows 'article)
4504             t))))))
4505
4506 ;;;###autoload
4507 (defun gnus-article-prepare-display ()
4508   "Make the current buffer look like a nice article."
4509   ;; Hooks for getting information from the article.
4510   ;; This hook must be called before being narrowed.
4511   (let ((gnus-article-buffer (current-buffer))
4512         buffer-read-only
4513         (inhibit-read-only t))
4514     (unless (eq major-mode 'gnus-article-mode)
4515       (gnus-article-mode))
4516     (setq buffer-read-only nil
4517           gnus-article-wash-types nil
4518           gnus-article-image-alist nil)
4519     (gnus-run-hooks 'gnus-tmp-internal-hook)
4520     (when gnus-display-mime-function
4521       (funcall gnus-display-mime-function))
4522     (gnus-run-hooks 'gnus-article-prepare-hook)))
4523
4524 ;;;
4525 ;;; Gnus Sticky Article Mode
4526 ;;;
4527
4528 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4529   "Mode for sticky articles."
4530   ;; Release bindings that won't work.
4531   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4532                              gnus-sticky-article-mode-map)
4533   (substitute-key-definition 'gnus-article-refer-article 'undefined
4534                              gnus-sticky-article-mode-map)
4535   (dolist (k '("e" "h" "s" "F" "R"))
4536     (define-key gnus-sticky-article-mode-map k nil))
4537   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4538   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4539   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4540   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4541
4542 (defun gnus-sticky-article (arg)
4543   "Make the current article sticky.
4544 If a prefix ARG is given, ask for a name for this sticky article buffer."
4545   (interactive "P")
4546   (gnus-summary-show-thread)
4547   (gnus-summary-select-article nil nil 'pseudo)
4548   (let (new-art-buf-name)
4549     (gnus-eval-in-buffer-window gnus-article-buffer
4550       (setq new-art-buf-name
4551             (concat
4552              "*Sticky Article: "
4553              (if arg
4554                  (read-from-minibuffer "Sticky article buffer name: ")
4555                (gnus-with-article-headers
4556                  (gnus-article-goto-header "subject")
4557                  (setq new-art-buf-name
4558                        (buffer-substring-no-properties
4559                         (line-beginning-position) (line-end-position)))
4560                  (goto-char (point-min))
4561                  (gnus-article-goto-header "from")
4562                  (setq new-art-buf-name
4563                        (concat
4564                         new-art-buf-name ", "
4565                         (buffer-substring-no-properties
4566                          (line-beginning-position) (line-end-position))))
4567                  (goto-char (point-min))
4568                  (gnus-article-goto-header "date")
4569                  (setq new-art-buf-name
4570                        (concat
4571                         new-art-buf-name ", "
4572                         (buffer-substring-no-properties
4573                          (line-beginning-position) (line-end-position))))))
4574              "*"))
4575       (if (and (gnus-buffer-live-p new-art-buf-name)
4576                (with-current-buffer new-art-buf-name
4577                  (eq major-mode 'gnus-sticky-article-mode)))
4578           (switch-to-buffer new-art-buf-name)
4579         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4580       (gnus-sticky-article-mode))
4581     (setq gnus-article-buffer new-art-buf-name))
4582   (gnus-summary-recenter)
4583   (gnus-summary-position-point))
4584
4585 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4586   "Kill the given sticky article BUFFER.
4587 If none is given, assume the current buffer and kill it if it has
4588 `gnus-sticky-article-mode'."
4589   (interactive)
4590   (unless buffer
4591     (setq buffer (current-buffer)))
4592   (with-current-buffer buffer
4593     (when (eq major-mode 'gnus-sticky-article-mode)
4594       (gnus-kill-buffer buffer))))
4595
4596 (defun gnus-kill-sticky-article-buffers (arg)
4597   "Kill all sticky article buffers.
4598 If a prefix ARG is given, ask for confirmation."
4599   (interactive "P")
4600   (dolist (buf (gnus-buffers))
4601     (with-current-buffer buf
4602       (when (eq major-mode 'gnus-sticky-article-mode)
4603         (if (not arg)
4604             (gnus-kill-buffer buf)
4605           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4606             (gnus-kill-buffer buf)))))))
4607
4608 ;;;
4609 ;;; Gnus MIME viewing functions
4610 ;;;
4611
4612 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4613   "Format of the MIME buttons.
4614
4615 Valid specifiers include:
4616 %t  The MIME type
4617 %T  MIME type, along with additional info
4618 %n  The `name' parameter
4619 %d  The description, if any
4620 %l  The length of the encoded part
4621 %p  The part identifier number
4622 %e  Dots if the part isn't displayed
4623
4624 General format specifiers can also be used.  See Info node
4625 `(gnus)Formatting Variables'.")
4626
4627 (defvar gnus-mime-button-line-format-alist
4628   '((?t gnus-tmp-type ?s)
4629     (?T gnus-tmp-type-long ?s)
4630     (?n gnus-tmp-name ?s)
4631     (?d gnus-tmp-description ?s)
4632     (?p gnus-tmp-id ?s)
4633     (?l gnus-tmp-length ?d)
4634     (?e gnus-tmp-dots ?s)))
4635
4636 (defvar gnus-mime-button-commands
4637   '((gnus-article-press-button "\r" "Toggle Display")
4638     (gnus-mime-view-part "v" "View Interactively...")
4639     (gnus-mime-view-part-as-type "t" "View As Type...")
4640     (gnus-mime-view-part-as-charset "C" "View As charset...")
4641     (gnus-mime-save-part "o" "Save...")
4642     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4643     (gnus-mime-replace-part "r" "Replace part")
4644     (gnus-mime-delete-part "d" "Delete part")
4645     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4646     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4647     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4648     (gnus-mime-view-part-externally "e" "View Externally")
4649     (gnus-mime-print-part "p" "Print")
4650     (gnus-mime-pipe-part "|" "Pipe To Command...")
4651     (gnus-mime-action-on-part "." "Take action on the part...")))
4652
4653 (defun gnus-article-mime-part-status ()
4654   (if gnus-article-mime-handle-alist-1
4655       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4656           " (1 part)"
4657         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4658     ""))
4659
4660 (defvar gnus-mime-button-map
4661   (let ((map (make-sparse-keymap)))
4662     (define-key map gnus-mouse-2 'gnus-article-push-button)
4663     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4664     (dolist (c gnus-mime-button-commands)
4665       (define-key map (cadr c) (car c)))
4666     map))
4667
4668 (easy-menu-define
4669   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4670   `("MIME Part"
4671     ,@(mapcar (lambda (c)
4672                 (vector (caddr c) (car c) :active t))
4673               gnus-mime-button-commands)))
4674
4675 (defun gnus-mime-button-menu (event prefix)
4676  "Construct a context-sensitive menu of MIME commands."
4677  (interactive "e\nP")
4678  (save-window-excursion
4679    (let ((pos (event-start event)))
4680      (select-window (posn-window pos))
4681      (goto-char (posn-point pos))
4682      (gnus-article-check-buffer)
4683      (popup-menu gnus-mime-button-menu nil prefix))))
4684
4685 (defun gnus-mime-view-all-parts (&optional handles)
4686   "View all the MIME parts."
4687   (interactive)
4688   (with-current-buffer gnus-article-buffer
4689     (let ((handles (or handles gnus-article-mime-handles))
4690           (mail-parse-charset gnus-newsgroup-charset)
4691           (mail-parse-ignored-charsets
4692            (with-current-buffer gnus-summary-buffer
4693              gnus-newsgroup-ignored-charsets)))
4694       (when handles
4695         (mm-remove-parts handles)
4696         (goto-char (point-min))
4697         (or (search-forward "\n\n") (goto-char (point-max)))
4698         (let ((inhibit-read-only t))
4699           (delete-region (point) (point-max))
4700           (mm-display-parts handles))))))
4701
4702 (defun gnus-article-jump-to-part (n)
4703   "Jump to MIME part N."
4704   (interactive "P")
4705   (pop-to-buffer gnus-article-buffer)
4706   ;; FIXME: why is it necessary?
4707   (sit-for 0)
4708   (let ((parts (length gnus-article-mime-handle-alist)))
4709     (or n (setq n
4710                 (string-to-number
4711                  (read-string ;; Emacs 21 doesn't have `read-number'.
4712                   (format "Jump to part (2..%s): " parts)))))
4713     (unless (and (integerp n) (<= n parts) (>= n 1))
4714       (setq n
4715             (progn
4716               (gnus-message 7 "Invalid part `%s', using %s instead."
4717                             n parts)
4718               parts)))
4719     (gnus-message 9 "Jumping to part %s." n)
4720     (cond ((>= gnus-auto-select-part 1)
4721            (while (and (<= n parts)
4722                        (not (gnus-article-goto-part n)))
4723              (setq n (1+ n))))
4724           ((< gnus-auto-select-part 0)
4725            (while (and (>= n 1)
4726                        (not (gnus-article-goto-part n)))
4727              (setq n (1- n))))
4728           (t
4729            (gnus-article-goto-part n)))))
4730
4731 (eval-when-compile
4732   (defsubst gnus-article-edit-part (handles &optional current-id)
4733     "Edit an article in order to delete a mime part.
4734 This function is exclusively used by `gnus-mime-save-part-and-strip'
4735 and `gnus-mime-delete-part', and not provided at run-time normally."
4736     (gnus-article-edit-article
4737      `(lambda ()
4738         (buffer-disable-undo)
4739         (erase-buffer)
4740         (let ((mail-parse-charset (or gnus-article-charset
4741                                       ',gnus-newsgroup-charset))
4742               (mail-parse-ignored-charsets
4743                (or gnus-article-ignored-charsets
4744                    ',gnus-newsgroup-ignored-charsets))
4745               (mbl mml-buffer-list))
4746           (setq mml-buffer-list nil)
4747           (insert-buffer-substring gnus-original-article-buffer)
4748           (mime-to-mml ',handles)
4749           (setq gnus-article-mime-handles nil)
4750           (let ((mbl1 mml-buffer-list))
4751             (setq mml-buffer-list mbl)
4752             (set (make-local-variable 'mml-buffer-list) mbl1))
4753           (gnus-make-local-hook 'kill-buffer-hook)
4754           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4755      `(lambda (no-highlight)
4756         (let ((mail-parse-charset (or gnus-article-charset
4757                                       ',gnus-newsgroup-charset))
4758               (message-options message-options)
4759               (message-options-set-recipient)
4760               (mail-parse-ignored-charsets
4761                (or gnus-article-ignored-charsets
4762                    ',gnus-newsgroup-ignored-charsets)))
4763           (mml-to-mime)
4764           (mml-destroy-buffers)
4765           (remove-hook 'kill-buffer-hook
4766                        'mml-destroy-buffers t)
4767           (kill-local-variable 'mml-buffer-list))
4768         (gnus-summary-edit-article-done
4769          ,(or (mail-header-references gnus-current-headers) "")
4770          ,(gnus-group-read-only-p)
4771          ,gnus-summary-buffer no-highlight))
4772      t)
4773     (gnus-article-edit-done)
4774     (gnus-summary-expand-window)
4775     (gnus-summary-show-article)
4776     (when (and current-id (integerp gnus-auto-select-part))
4777       (gnus-article-jump-to-part
4778        (if (text-property-any (point-min) (point-max)
4779                               'gnus-part (+ current-id gnus-auto-select-part))
4780            (+ current-id gnus-auto-select-part)
4781          (with-current-buffer gnus-article-buffer
4782            (length gnus-article-mime-handle-alist)))))))
4783
4784 (defun gnus-mime-replace-part (file)
4785   "Replace MIME part under point with an external body."
4786   ;; Useful if file has already been saved to disk
4787   (interactive
4788    (list
4789     (mm-with-multibyte
4790       (read-file-name "Replace MIME part with file: "
4791                       (or mm-default-directory default-directory)
4792                       nil nil))))
4793   (gnus-mime-save-part-and-strip file))
4794
4795 (defun gnus-mime-save-part-and-strip (&optional file)
4796   "Save the MIME part under point then replace it with an external body.
4797 If FILE is given, use it for the external part."
4798   (interactive)
4799   (gnus-article-check-buffer)
4800   (when (gnus-group-read-only-p)
4801     (error "The current group does not support deleting of parts"))
4802   (when (mm-complicated-handles gnus-article-mime-handles)
4803     (error "\
4804 The current article has a complicated MIME structure, giving up..."))
4805   (let* ((data (get-text-property (point) 'gnus-data))
4806          (id (get-text-property (point) 'gnus-part))
4807          param
4808          (handles gnus-article-mime-handles))
4809     (unless file
4810       (setq file
4811             (and data (mm-save-part data "Delete MIME part and save to: "))))
4812     (when file
4813       (with-current-buffer (mm-handle-buffer data)
4814         (erase-buffer)
4815         (insert "Content-Type: " (mm-handle-media-type data))
4816         (mml-insert-parameter-string (cdr (mm-handle-type data))
4817                                      '(charset))
4818         ;; Add a filename for the sake of saving the part again.
4819         (mml-insert-parameter
4820          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
4821         (insert "\n")
4822         (insert "Content-ID: " (message-make-message-id) "\n")
4823         (insert "Content-Transfer-Encoding: binary\n")
4824         (insert "\n"))
4825       (setcdr data
4826               (cdr (mm-make-handle nil
4827                                    `("message/external-body"
4828                                      (access-type . "LOCAL-FILE")
4829                                      (name . ,file)))))
4830       ;; (set-buffer gnus-summary-buffer)
4831       (gnus-article-edit-part handles id))))
4832
4833 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
4834 ;; parts...>') but with stripping would be nice.
4835
4836 (defun gnus-mime-delete-part ()
4837   "Delete the MIME part under point.
4838 Replace it with some information about the removed part."
4839   (interactive)
4840   (gnus-article-check-buffer)
4841   (when (gnus-group-read-only-p)
4842     (error "The current group does not support deleting of parts"))
4843   (when (mm-complicated-handles gnus-article-mime-handles)
4844     (error "\
4845 The current article has a complicated MIME structure, giving up..."))
4846   (when (or gnus-expert-user
4847             (gnus-yes-or-no-p "\
4848 Deleting parts may malfunction or destroy the article; continue? "))
4849     (let* ((data (get-text-property (point) 'gnus-data))
4850            (id (get-text-property (point) 'gnus-part))
4851            (handles gnus-article-mime-handles)
4852            (none "(none)")
4853            (description
4854             (let ((desc (mm-handle-description data)))
4855               (when desc
4856                 (mail-decode-encoded-word-string desc))))
4857            (filename
4858             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
4859                 none))
4860            (type (mm-handle-media-type data)))
4861       (unless data
4862         (error "No MIME part under point"))
4863       (with-current-buffer (mm-handle-buffer data)
4864         (let ((bsize (format "%s" (buffer-size))))
4865           (erase-buffer)
4866           (insert
4867            (concat
4868             ",----\n"
4869             "| The following attachment has been deleted:\n"
4870             "|\n"
4871             "| Type:           " type "\n"
4872             "| Filename:       " filename "\n"
4873             "| Size (encoded): " bsize " Byte\n"
4874             (when description
4875               (concat    "| Description:    " description "\n"))
4876             "`----\n"))
4877           (setcdr data
4878                   (cdr (mm-make-handle
4879                         nil `("text/plain") nil nil
4880                         (list "attachment")
4881                         (format "Deleted attachment (%s bytes)" bsize))))))
4882       ;; (set-buffer gnus-summary-buffer)
4883       (gnus-article-edit-part handles id))))
4884
4885 (defun gnus-mime-save-part ()
4886   "Save the MIME part under point."
4887   (interactive)
4888   (gnus-article-check-buffer)
4889   (let ((data (get-text-property (point) 'gnus-data)))
4890     (when data
4891       (mm-save-part data))))
4892
4893 (defun gnus-mime-pipe-part ()
4894   "Pipe the MIME part under point to a process."
4895   (interactive)
4896   (gnus-article-check-buffer)
4897   (let ((data (get-text-property (point) 'gnus-data)))
4898     (when data
4899       (mm-pipe-part data))))
4900
4901 (defun gnus-mime-view-part ()
4902   "Interactively choose a viewing method for the MIME part under point."
4903   (interactive)
4904   (gnus-article-check-buffer)
4905   (let ((data (get-text-property (point) 'gnus-data)))
4906     (when data
4907       (setq gnus-article-mime-handles
4908             (mm-merge-handles
4909              gnus-article-mime-handles (setq data (copy-sequence data))))
4910       (mm-interactively-view-part data))))
4911
4912 (defun gnus-mime-view-part-as-type-internal ()
4913   (gnus-article-check-buffer)
4914   (let* ((handle (get-text-property (point) 'gnus-data))
4915          (name (or
4916                 ;; Content-Type: foo/bar; name=...
4917                 (mail-content-type-get (mm-handle-type handle) 'name)
4918                 ;; Content-Disposition: attachment; filename=...
4919                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
4920          (def-type (and name (mm-default-file-encoding name))))
4921     (or (and def-type (cons def-type 0))
4922         (and handle
4923              (equal (mm-handle-media-supertype handle) "text")
4924              '("text/plain" . 0))
4925         '("application/octet-stream" . 0))))
4926
4927 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
4928   "Choose a MIME media type, and view the part as such.
4929 If non-nil, PRED is a predicate to use during completion to limit the
4930 available media-types."
4931   (interactive)
4932   (unless mime-type
4933     (setq mime-type
4934           (let ((default (gnus-mime-view-part-as-type-internal)))
4935             (completing-read
4936              (format "View as MIME type (default %s): "
4937                      (car default))
4938              (mapcar #'list (mailcap-mime-types))
4939              pred nil nil nil
4940              (car default)))))
4941   (gnus-article-check-buffer)
4942   (let ((handle (get-text-property (point) 'gnus-data)))
4943     (when handle
4944       (when (equal (mm-handle-media-type handle) "message/external-body")
4945         (unless (mm-handle-cache handle)
4946           (mm-extern-cache-contents handle))
4947         (setq handle (mm-handle-cache handle)))
4948       (setq handle
4949             (mm-make-handle (mm-handle-buffer handle)
4950                             (cons mime-type (cdr (mm-handle-type handle)))
4951                             (mm-handle-encoding handle)
4952                             (mm-handle-undisplayer handle)
4953                             (mm-handle-disposition handle)
4954                             (mm-handle-description handle)
4955                             nil
4956                             (mm-handle-id handle)))
4957       (setq gnus-article-mime-handles
4958             (mm-merge-handles gnus-article-mime-handles handle))
4959       (when (mm-handle-displayed-p handle)
4960         (mm-remove-part handle))
4961       (gnus-mm-display-part handle))))
4962
4963 (defun gnus-mime-copy-part (&optional handle arg)
4964   "Put the MIME part under point into a new buffer.
4965 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
4966 are decompressed."
4967   (interactive (list nil current-prefix-arg))
4968   (gnus-article-check-buffer)
4969   (unless handle
4970     (setq handle (get-text-property (point) 'gnus-data)))
4971   (when handle
4972     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
4973                                                'name)
4974                         (mail-content-type-get (mm-handle-disposition handle)
4975                                                'filename)))
4976           contents dont-decode charset coding-system)
4977       (mm-with-unibyte-buffer
4978         (mm-insert-part handle)
4979         (setq contents (or (condition-case nil
4980                                (mm-decompress-buffer filename nil 'sig)
4981                              (error
4982                               (setq dont-decode t)
4983                               nil))
4984                            (buffer-string))))
4985       (setq filename (cond (filename (file-name-nondirectory filename))
4986                            (dont-decode "*raw data*")
4987                            (t "*decoded*")))
4988       (cond
4989        (dont-decode)
4990        ((not arg)
4991         (unless (setq charset (mail-content-type-get
4992                                (mm-handle-type handle) 'charset))
4993           (unless (setq coding-system (mm-with-unibyte-buffer
4994                                         (insert contents)
4995                                         (mm-find-buffer-file-coding-system)))
4996             (setq charset gnus-newsgroup-charset))))
4997        ((numberp arg)
4998         (setq charset (or (cdr (assq arg
4999                                      gnus-summary-show-article-charset-alist))
5000                           (mm-read-coding-system "Charset: ")))))
5001       (switch-to-buffer (generate-new-buffer filename))
5002       (if (or coding-system
5003               (and charset
5004                    (setq coding-system (mm-charset-to-coding-system charset))
5005                    (not (eq charset 'ascii))))
5006           (progn
5007             (mm-enable-multibyte)
5008             (insert (mm-decode-coding-string contents coding-system))
5009             (setq buffer-file-coding-system
5010                   (if (boundp 'last-coding-system-used)
5011                       (symbol-value 'last-coding-system-used)
5012                     coding-system)))
5013         (mm-disable-multibyte)
5014         (insert contents)
5015         (setq buffer-file-coding-system mm-binary-coding-system))
5016       ;; We do it this way to make `normal-mode' set the appropriate mode.
5017       (unwind-protect
5018           (progn
5019             (setq buffer-file-name (expand-file-name filename))
5020             (normal-mode))
5021         (setq buffer-file-name nil))
5022       (goto-char (point-min)))))
5023
5024 (defun gnus-mime-print-part (&optional handle filename)
5025   "Print the MIME part under point."
5026   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5027   (gnus-article-check-buffer)
5028   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5029          (contents (and handle (mm-get-part handle)))
5030          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5031          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5032     (when contents
5033         (if printer
5034             (unwind-protect
5035                 (progn
5036                   (mm-save-part-to-file handle file)
5037                   (call-process shell-file-name nil
5038                                 (generate-new-buffer " *mm*")
5039                                 nil
5040                                 shell-command-switch
5041                                 (mm-mailcap-command
5042                                  printer file (mm-handle-type handle))))
5043               (delete-file file))
5044           (with-temp-buffer
5045             (insert contents)
5046             (gnus-print-buffer))
5047           (ps-despool filename)))))
5048
5049 (defun gnus-mime-inline-part (&optional handle arg)
5050   "Insert the MIME part under point into the current buffer.
5051 Compressed files like .gz and .bz2 are decompressed."
5052   (interactive (list nil current-prefix-arg))
5053   (gnus-article-check-buffer)
5054   (unless handle
5055     (setq handle (get-text-property (point) 'gnus-data)))
5056   (when handle
5057     (let ((b (point))
5058           (inhibit-read-only t)
5059           contents charset coding-system)
5060       (if (and (not arg) (mm-handle-undisplayer handle))
5061           (mm-remove-part handle)
5062         (mm-with-unibyte-buffer
5063           (mm-insert-part handle)
5064           (setq contents
5065                 (or (mm-decompress-buffer
5066                      (or (mail-content-type-get (mm-handle-type handle)
5067                                                 'name)
5068                          (mail-content-type-get (mm-handle-disposition handle)
5069                                                 'filename))
5070                      nil t)
5071                     (buffer-string))))
5072         (cond
5073          ((not arg)
5074           (unless (setq charset (mail-content-type-get
5075                                  (mm-handle-type handle) 'charset))
5076             (unless (setq coding-system
5077                           (mm-with-unibyte-buffer
5078                             (insert contents)
5079                             (mm-find-buffer-file-coding-system)))
5080               (setq charset gnus-newsgroup-charset))))
5081          ((numberp arg)
5082           (if (mm-handle-undisplayer handle)
5083               (mm-remove-part handle))
5084           (setq charset
5085                 (or (cdr (assq arg
5086                                gnus-summary-show-article-charset-alist))
5087                     (mm-read-coding-system "Charset: "))))
5088          (t
5089           (if (mm-handle-undisplayer handle)
5090               (mm-remove-part handle))))
5091         (forward-line 2)
5092         (mm-insert-inline
5093          handle
5094          (if (or coding-system
5095                  (and charset
5096                       (setq coding-system
5097                             (mm-charset-to-coding-system charset))
5098                       (not (eq coding-system 'ascii))))
5099              (mm-decode-coding-string contents coding-system)
5100            (mm-string-to-multibyte contents)))
5101         (goto-char b)))))
5102
5103 (defun gnus-mime-strip-charset-parameters (handle)
5104   "Strip charset parameters from HANDLE."
5105   (if (stringp (car handle))
5106       (mapc #'gnus-mime-strip-charset-parameters (cdr handle))
5107     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5108                                             "message/external-body")
5109                                      (progn
5110                                        (unless (mm-handle-cache handle)
5111                                          (mm-extern-cache-contents handle))
5112                                        (mm-handle-cache handle))
5113                                    handle)))
5114            (charset (assq 'charset (cdr type))))
5115       (when charset
5116         (delq charset type)))))
5117
5118 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5119   "Insert the MIME part under point into the current buffer using the
5120 specified charset."
5121   (interactive (list nil current-prefix-arg))
5122   (gnus-article-check-buffer)
5123   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5124         (fun (get-text-property (point) 'gnus-callback))
5125         (gnus-newsgroup-ignored-charsets 'gnus-all)
5126         gnus-newsgroup-charset form preferred parts)
5127     (when handle
5128       (when (prog1
5129                 (and fun
5130                      (setq gnus-newsgroup-charset
5131                            (or (cdr (assq
5132                                      arg
5133                                      gnus-summary-show-article-charset-alist))
5134                                (mm-read-coding-system "Charset: "))))
5135               (if (mm-handle-undisplayer handle)
5136                   (mm-remove-part handle)))
5137         (gnus-mime-strip-charset-parameters handle)
5138         (when (and (consp (setq form (cdr-safe fun)))
5139                    (setq form (ignore-errors
5140                                 (assq 'gnus-mime-display-alternative form)))
5141                    (setq preferred (caddr form))
5142                    (progn
5143                      (when (eq (car preferred) 'quote)
5144                        (setq preferred (cadr preferred)))
5145                      (not (equal preferred
5146                                  (get-text-property (point) 'gnus-data))))
5147                    (setq parts (get-text-property (point) 'gnus-part))
5148                    (setq parts (cdr (assq parts
5149                                           gnus-article-mime-handle-alist)))
5150                    (equal (mm-handle-media-type parts) "multipart/alternative")
5151                    (setq parts (reverse (cdr parts))))
5152           (setcar (cddr form)
5153                   (list 'quote (or (cadr (member preferred parts))
5154                                    (car parts)))))
5155         (funcall fun handle)))))
5156
5157 (defun gnus-mime-view-part-externally (&optional handle)
5158   "View the MIME part under point with an external viewer."
5159   (interactive)
5160   (gnus-article-check-buffer)
5161   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5162          (mm-user-display-methods nil)
5163          (mm-inlined-types nil)
5164          (mail-parse-charset gnus-newsgroup-charset)
5165          (mail-parse-ignored-charsets
5166           (with-current-buffer gnus-summary-buffer
5167             gnus-newsgroup-ignored-charsets))
5168          (type (mm-handle-media-type handle))
5169          (method (mailcap-mime-info type))
5170          (mm-enable-external t))
5171     (if (not (stringp method))
5172         (gnus-mime-view-part-as-type
5173          nil (lambda (types) (stringp (mailcap-mime-info (car types)))))
5174       (when handle
5175         (if (mm-handle-undisplayer handle)
5176             (mm-remove-part handle)
5177           (mm-display-part handle))))))
5178
5179 (defun gnus-mime-view-part-internally (&optional handle)
5180   "View the MIME part under point with an internal viewer.
5181 If no internal viewer is available, use an external viewer."
5182   (interactive)
5183   (gnus-article-check-buffer)
5184   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5185          (mm-inlined-types '(".*"))
5186          (mm-inline-large-images t)
5187          (mail-parse-charset gnus-newsgroup-charset)
5188          (mail-parse-ignored-charsets
5189           (with-current-buffer gnus-summary-buffer
5190             gnus-newsgroup-ignored-charsets))
5191          (inhibit-read-only t))
5192     (if (not (mm-inlinable-p handle))
5193         (gnus-mime-view-part-as-type
5194          nil (lambda (types) (mm-inlinable-p handle (car types))))
5195       (when handle
5196         (if (mm-handle-undisplayer handle)
5197             (mm-remove-part handle)
5198           (mm-display-part handle))))))
5199
5200 (defun gnus-mime-action-on-part (&optional action)
5201   "Do something with the MIME attachment at \(point\)."
5202   (interactive
5203    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
5204   (gnus-article-check-buffer)
5205   (let ((action-pair (assoc action gnus-mime-action-alist)))
5206     (if action-pair
5207         (funcall (cdr action-pair)))))
5208
5209 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5210   "Call FUNCTION on MIME part N.
5211 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5212 If INTERACTIVE, call FUNCTION interactivly."
5213   (let (window frame)
5214     ;; Check whether the article is displayed.
5215     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5216                  (setq window (get-buffer-window gnus-article-buffer t))
5217                  (frame-visible-p (setq frame (window-frame window))))
5218       (error "No article is displayed"))
5219     (with-current-buffer gnus-article-buffer
5220       ;; Check whether the article displays the right contents.
5221       (unless (with-current-buffer gnus-summary-buffer
5222                 (eq gnus-current-article (gnus-summary-article-number)))
5223         (error "You should select the right article first"))
5224       (if n
5225           (setq n (prefix-numeric-value n))
5226         (let ((pt (point)))
5227           (setq n (or (get-text-property pt 'gnus-part)
5228                       (and (not (bobp))
5229                            (get-text-property (1- pt) 'gnus-part))
5230                       (get-text-property (prog2
5231                                              (forward-line 1)
5232                                              (point)
5233                                            (goto-char pt))
5234                                          'gnus-part)
5235                       (get-text-property
5236                        (or (and (setq pt (previous-single-property-change
5237                                           pt 'gnus-part))
5238                                 (1- pt))
5239                            (next-single-property-change (point) 'gnus-part)
5240                            (point))
5241                        'gnus-part)
5242                       1))))
5243       ;; Check whether the specified part exists.
5244       (when (> n (length gnus-article-mime-handle-alist))
5245         (error "No such part")))
5246     (unless
5247         (progn
5248           ;; To select the window is needed so that the cursor
5249           ;; might be visible on the MIME button.
5250           (select-window (prog1
5251                              window
5252                            (setq window (selected-window))
5253                            ;; Article may be displayed in the other frame.
5254                            (gnus-select-frame-set-input-focus
5255                             (prog1
5256                                 frame
5257                               (setq frame (selected-frame))))))
5258           (when (gnus-article-goto-part n)
5259             ;; We point the cursor and the arrow at the MIME button
5260             ;; when the `function' prompt the user for something.
5261             (let ((cursor-in-non-selected-windows t)
5262                   (overlay-arrow-string "=>")
5263                   (overlay-arrow-position (point-marker)))
5264               (unwind-protect
5265                   (cond
5266                    ((and no-handle interactive)
5267                     (call-interactively function))
5268                    (no-handle
5269                     (funcall function))
5270                    (interactive
5271                     (call-interactively
5272                      function
5273                      (cdr (assq n gnus-article-mime-handle-alist))))
5274                    (t
5275                     (funcall function
5276                              (cdr (assq n gnus-article-mime-handle-alist)))))
5277                 (set-marker overlay-arrow-position nil)
5278                 (unless gnus-auto-select-part
5279                   (gnus-select-frame-set-input-focus frame)
5280                   (select-window window))))
5281             t))
5282       (if gnus-inhibit-mime-unbuttonizing
5283           ;; This is the default though the program shouldn't reach here.
5284           (error "No such part")
5285         ;; The part which doesn't have the MIME button is selected.
5286         ;; So, we display all the buttons and redo it.
5287         (let ((gnus-inhibit-mime-unbuttonizing t))
5288           (gnus-summary-show-article)
5289           (gnus-article-part-wrapper n function no-handle))))))
5290
5291 (defun gnus-article-pipe-part (n)
5292   "Pipe MIME part N, which is the numerical prefix."
5293   (interactive "P")
5294   (gnus-article-part-wrapper n 'mm-pipe-part))
5295
5296 (defun gnus-article-save-part (n)
5297   "Save MIME part N, which is the numerical prefix."
5298   (interactive "P")
5299   (gnus-article-part-wrapper n 'mm-save-part))
5300
5301 (defun gnus-article-interactively-view-part (n)
5302   "View MIME part N interactively, which is the numerical prefix."
5303   (interactive "P")
5304   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5305
5306 (defun gnus-article-copy-part (n)
5307   "Copy MIME part N, which is the numerical prefix."
5308   (interactive "P")
5309   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5310
5311 (defun gnus-article-view-part-as-charset (n)
5312   "View MIME part N using a specified charset.
5313 N is the numerical prefix."
5314   (interactive "P")
5315   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5316
5317 (defun gnus-article-view-part-externally (n)
5318   "View MIME part N externally, which is the numerical prefix."
5319   (interactive "P")
5320   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5321
5322 (defun gnus-article-inline-part (n)
5323   "Inline MIME part N, which is the numerical prefix."
5324   (interactive "P")
5325   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5326
5327 (defun gnus-article-save-part-and-strip (n)
5328   "Save MIME part N and replace it with an external body.
5329 N is the numerical prefix."
5330   (interactive "P")
5331   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5332
5333 (defun gnus-article-replace-part (n)
5334   "Replace MIME part N with an external body.
5335 N is the numerical prefix."
5336   (interactive "P")
5337   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5338
5339 (defun gnus-article-delete-part (n)
5340   "Delete MIME part N and add some information about the removed part.
5341 N is the numerical prefix."
5342   (interactive "P")
5343   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5344
5345 (defun gnus-article-view-part-as-type (n)
5346   "Choose a MIME media type, and view part N as such.
5347 N is the numerical prefix."
5348   (interactive "P")
5349   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5350
5351 (defun gnus-article-mime-match-handle-first (condition)
5352   (if condition
5353       (let (n)
5354         (dolist (ihandle gnus-article-mime-handle-alist)
5355           (if (and (cond
5356                     ((functionp condition)
5357                      (funcall condition (cdr ihandle)))
5358                     ((eq condition 'undisplayed)
5359                      (not (or (mm-handle-undisplayer (cdr ihandle))
5360                               (equal (mm-handle-media-type (cdr ihandle))
5361                                      "multipart/alternative"))))
5362                     ((eq condition 'undisplayed-alternative)
5363                      (not (mm-handle-undisplayer (cdr ihandle))))
5364                     (t t))
5365                    (gnus-article-goto-part (car ihandle))
5366                    (or (not n) (< (car ihandle) n)))
5367               (setq n (car ihandle))))
5368         (or n 1))
5369     1))
5370
5371 (defun gnus-article-view-part (&optional n)
5372   "View MIME part N, which is the numerical prefix."
5373   (interactive "P")
5374   (with-current-buffer gnus-article-buffer
5375     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5376                              gnus-article-mime-match-handle-function)))
5377     (when (> n (length gnus-article-mime-handle-alist))
5378       (error "No such part"))
5379     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5380       (when (gnus-article-goto-part n)
5381         (if (equal (car handle) "multipart/alternative")
5382             (gnus-article-press-button)
5383           (when (eq (gnus-mm-display-part handle) 'internal)
5384             (gnus-set-window-start)))))))
5385
5386 (defsubst gnus-article-mime-total-parts ()
5387   (if (bufferp (car gnus-article-mime-handles))
5388       1 ;; single part
5389     (1- (length gnus-article-mime-handles))))
5390
5391 (defun gnus-mm-display-part (handle)
5392   "Display HANDLE and fix MIME button."
5393   (let ((id (get-text-property (point) 'gnus-part))
5394         (point (point))
5395         (inhibit-read-only t))
5396     (forward-line 1)
5397     (prog1
5398         (let ((window (selected-window))
5399               (mail-parse-charset gnus-newsgroup-charset)
5400               (mail-parse-ignored-charsets
5401                (if (gnus-buffer-live-p gnus-summary-buffer)
5402                    (with-current-buffer gnus-summary-buffer
5403                      gnus-newsgroup-ignored-charsets)
5404                  nil)))
5405           (save-excursion
5406             (unwind-protect
5407                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5408                       (beg (point)))
5409                   (when win
5410                     (select-window win))
5411                   (goto-char point)
5412                   (forward-line)
5413                   (if (mm-handle-displayed-p handle)
5414                       ;; This will remove the part.
5415                       (mm-display-part handle)
5416                     (save-restriction
5417                       (narrow-to-region (point)
5418                                         (if (eobp) (point) (1+ (point))))
5419                       (mm-display-part handle)
5420                       ;; We narrow to the part itself and
5421                       ;; then call the treatment functions.
5422                       (goto-char (point-min))
5423                       (forward-line 1)
5424                       (narrow-to-region (point) (point-max))
5425                       (gnus-treat-article
5426                        nil id
5427                        (gnus-article-mime-total-parts)
5428                        (mm-handle-media-type handle)))))
5429               (if (window-live-p window)
5430                   (select-window window)))))
5431       (goto-char point)
5432       (gnus-delete-line)
5433       (gnus-insert-mime-button
5434        handle id (list (mm-handle-displayed-p handle)))
5435       (goto-char point))))
5436
5437 (defun gnus-article-goto-part (n)
5438   "Go to MIME part N."
5439   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
5440
5441 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5442   (let ((gnus-tmp-name
5443          (or (mail-content-type-get (mm-handle-type handle) 'name)
5444              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5445              (mail-content-type-get (mm-handle-type handle) 'url)
5446              ""))
5447         (gnus-tmp-type (mm-handle-media-type handle))
5448         (gnus-tmp-description (or (mm-handle-description handle) ""))
5449         (gnus-tmp-dots
5450          (if (if displayed (car displayed)
5451                (mm-handle-displayed-p handle))
5452              "" "..."))
5453         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5454                            (buffer-size)))
5455         gnus-tmp-type-long b e)
5456     (when (string-match ".*/" gnus-tmp-name)
5457       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5458     (setq gnus-tmp-type-long (concat gnus-tmp-type
5459                                      (and (not (equal gnus-tmp-name ""))
5460                                           (concat "; " gnus-tmp-name))))
5461     (unless (equal gnus-tmp-description "")
5462       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5463     (unless (bolp)
5464       (insert "\n"))
5465     (setq b (point))
5466     (gnus-eval-format
5467      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5468      `(keymap ,gnus-mime-button-map
5469               gnus-callback gnus-mm-display-part
5470               gnus-part ,gnus-tmp-id
5471               article-type annotation
5472               gnus-data ,handle))
5473     (setq e (if (bolp)
5474                 ;; Exclude a newline.
5475                 (1- (point))
5476               (point)))
5477     (when gnus-article-button-face
5478       (gnus-overlay-put (gnus-make-overlay b e nil t)
5479                         'face gnus-article-button-face))
5480     (widget-convert-button
5481      'link b e
5482      :mime-handle handle
5483      :action 'gnus-widget-press-button
5484      :button-keymap gnus-mime-button-map
5485      :help-echo
5486      (lambda (widget/window &optional overlay pos)
5487        ;; Needed to properly clear the message due to a bug in
5488        ;; wid-edit (XEmacs only).
5489        (if (boundp 'help-echo-owns-message)
5490            (setq help-echo-owns-message t))
5491        (format
5492         "%S: %s the MIME part; %S: more options"
5493         (aref gnus-mouse-2 0)
5494         ;; XEmacs will get a single widget arg; Emacs 21 will get
5495         ;; window, overlay, position.
5496         (if (mm-handle-displayed-p
5497              (if overlay
5498                  (with-current-buffer (gnus-overlay-buffer overlay)
5499                    (widget-get (widget-at (gnus-overlay-start overlay))
5500                                :mime-handle))
5501                (widget-get widget/window :mime-handle)))
5502             "hide" "show")
5503         (aref gnus-down-mouse-3 0))))))
5504
5505 (defun gnus-widget-press-button (elems el)
5506   (goto-char (widget-get elems :from))
5507   (gnus-article-press-button))
5508
5509 (defvar gnus-displaying-mime nil)
5510
5511 (defun gnus-display-mime (&optional ihandles)
5512   "Display the MIME parts."
5513   (save-excursion
5514     (save-selected-window
5515       (let ((window (get-buffer-window gnus-article-buffer))
5516             (point (point)))
5517         (when window
5518           (select-window window)
5519           ;; We have to do this since selecting the window
5520           ;; may change the point.  So we set the window point.
5521           (set-window-point window point)))
5522       (let ((handles ihandles)
5523             (inhibit-read-only t)
5524             handle)
5525         (cond (handles)
5526               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5527                (when gnus-article-emulate-mime
5528                  (mm-uu-dissect-text-parts handles)))
5529               (gnus-article-emulate-mime
5530                (setq handles (mm-uu-dissect))))
5531         (when (and (not ihandles)
5532                    (not gnus-displaying-mime))
5533           ;; Top-level call; we clean up.
5534           (when gnus-article-mime-handles
5535             (mm-destroy-parts gnus-article-mime-handles)
5536             (setq gnus-article-mime-handle-alist nil));; A trick.
5537           (setq gnus-article-mime-handles handles)
5538           ;; We allow users to glean info from the handles.
5539           (when gnus-article-mime-part-function
5540             (gnus-mime-part-function handles)))
5541         (if (and handles
5542                  (or (not (stringp (car handles)))
5543                      (cdr handles)))
5544             (progn
5545               (when (and (not ihandles)
5546                          (not gnus-displaying-mime))
5547                 ;; Clean up for mime parts.
5548                 (article-goto-body)
5549                 (delete-region (point) (point-max)))
5550               (let ((gnus-displaying-mime t))
5551                 (gnus-mime-display-part handles)))
5552           (save-restriction
5553             (article-goto-body)
5554             (narrow-to-region (point) (point-max))
5555             (gnus-treat-article nil 1 1)
5556             (widen)))
5557         (unless ihandles
5558           ;; Highlight the headers.
5559           (save-excursion
5560             (save-restriction
5561               (article-goto-body)
5562               (narrow-to-region (point-min) (point))
5563               (gnus-article-save-original-date
5564                (gnus-treat-article 'head)))))))
5565     ;; Cope with broken MIME messages.
5566     (goto-char (point-max))
5567     (unless (bolp)
5568       (insert "\n"))))
5569
5570 (defcustom gnus-mime-display-multipart-as-mixed nil
5571   "Display \"multipart\" parts as  \"multipart/mixed\".
5572
5573 If t, it overrides nil values of
5574 `gnus-mime-display-multipart-alternative-as-mixed' and
5575 `gnus-mime-display-multipart-related-as-mixed'."
5576   :group 'gnus-article-mime
5577   :type 'boolean)
5578
5579 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5580   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5581   :version "22.1"
5582   :group 'gnus-article-mime
5583   :type 'boolean)
5584
5585 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5586   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5587
5588 If displaying \"text/html\" is discouraged \(see
5589 `mm-discouraged-alternatives'\) images or other material inside a
5590 \"multipart/related\" part might be overlooked when this variable is nil."
5591   :version "22.1"
5592   :group 'gnus-article-mime
5593   :type 'boolean)
5594
5595 (defun gnus-mime-display-part (handle)
5596   (cond
5597    ;; Maybe a broken MIME message.
5598    ((null handle))
5599    ;; Single part.
5600    ((not (stringp (car handle)))
5601     (gnus-mime-display-single handle))
5602    ;; User-defined multipart
5603    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5604     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5605              handle))
5606    ;; multipart/alternative
5607    ((and (equal (car handle) "multipart/alternative")
5608          (not (or gnus-mime-display-multipart-as-mixed
5609                   gnus-mime-display-multipart-alternative-as-mixed)))
5610     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5611       (push (cons id handle) gnus-article-mime-handle-alist)
5612       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5613    ;; multipart/related
5614    ((and (equal (car handle) "multipart/related")
5615          (not (or gnus-mime-display-multipart-as-mixed
5616                   gnus-mime-display-multipart-related-as-mixed)))
5617     ;;;!!!We should find the start part, but we just default
5618     ;;;!!!to the first part.
5619     ;;(gnus-mime-display-part (cadr handle))
5620     ;;;!!! Most multipart/related is an HTML message plus images.
5621     ;;;!!! Unfortunately we are unable to let W3 display those
5622     ;;;!!! included images, so we just display it as a mixed multipart.
5623     ;;(gnus-mime-display-mixed (cdr handle))
5624     ;;;!!! No, w3 can display everything just fine.
5625     (gnus-mime-display-part (cadr handle)))
5626    ((equal (car handle) "multipart/signed")
5627     (gnus-add-wash-type 'signed)
5628     (gnus-mime-display-security handle))
5629    ((equal (car handle) "multipart/encrypted")
5630     (gnus-add-wash-type 'encrypted)
5631     (gnus-mime-display-security handle))
5632    ;; Other multiparts are handled like multipart/mixed.
5633    (t
5634     (gnus-mime-display-mixed (cdr handle)))))
5635
5636 (defun gnus-mime-part-function (handles)
5637   (if (stringp (car handles))
5638       (mapcar 'gnus-mime-part-function (cdr handles))
5639     (funcall gnus-article-mime-part-function handles)))
5640
5641 (defun gnus-mime-display-mixed (handles)
5642   (mapcar 'gnus-mime-display-part handles))
5643
5644 (defun gnus-mime-display-single (handle)
5645   (let ((type (mm-handle-media-type handle))
5646         (ignored gnus-ignored-mime-types)
5647         (not-attachment t)
5648         (move nil)
5649         display text)
5650     (catch 'ignored
5651       (progn
5652         (while ignored
5653           (when (string-match (pop ignored) type)
5654             (throw 'ignored nil)))
5655         (if (and (setq not-attachment
5656                        (and (not (mm-inline-override-p handle))
5657                             (or (not (mm-handle-disposition handle))
5658                                 (equal (car (mm-handle-disposition handle))
5659                                        "inline")
5660                                 (mm-attachment-override-p handle))))
5661                  (mm-automatic-display-p handle)
5662                  (or (and
5663                       (mm-inlinable-p handle)
5664                       (mm-inlined-p handle))
5665                      (mm-automatic-external-display-p type)))
5666             (setq display t)
5667           (when (equal (mm-handle-media-supertype handle) "text")
5668             (setq text t)))
5669         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5670               beg)
5671           (push (cons id handle) gnus-article-mime-handle-alist)
5672           (when (and display
5673                      (equal (mm-handle-media-supertype handle) "message"))
5674             (insert-char
5675              ?\n
5676              (cond ((not (bolp)) 2)
5677                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5678                    (t 1))))
5679           (when (or (not display)
5680                     (not (gnus-unbuttonized-mime-type-p type)))
5681             (gnus-insert-mime-button
5682              handle id (list (or display (and not-attachment text))))
5683             (gnus-article-insert-newline)
5684             ;; Remember modify the number of forward lines.
5685             (setq move t))
5686           (setq beg (point))
5687           (cond
5688            (display
5689             (when move
5690               (forward-line -1)
5691               (setq beg (point)))
5692             (let ((mail-parse-charset gnus-newsgroup-charset)
5693                   (mail-parse-ignored-charsets
5694                    (save-excursion (condition-case ()
5695                                        (set-buffer gnus-summary-buffer)
5696                                      (error))
5697                                    gnus-newsgroup-ignored-charsets)))
5698               (mm-display-part handle t))
5699             (goto-char (point-max)))
5700            ((and text not-attachment)
5701             (when move
5702               (forward-line -1)
5703               (setq beg (point)))
5704             (gnus-article-insert-newline)
5705             (mm-insert-inline
5706              handle
5707              (let ((charset (or (mail-content-type-get (mm-handle-type handle)
5708                                                        'charset)
5709                                 (and (equal type "text/calendar") 'utf-8))))
5710                (cond ((not charset)
5711                       (mm-string-as-multibyte (mm-get-part handle)))
5712                      ((eq charset 'gnus-decoded)
5713                       (with-current-buffer (mm-handle-buffer handle)
5714                         (buffer-string)))
5715                      (t
5716                       (mm-decode-string (mm-get-part handle) charset)))))
5717             (goto-char (point-max))))
5718           ;; Do highlighting.
5719           (save-excursion
5720             (save-restriction
5721               (narrow-to-region beg (point))
5722               (if (eq handle gnus-article-mime-handles)
5723                   ;; The format=flowed case.
5724                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5725                 ;; Don't count signature parts that are never displayed.
5726                 ;; The part number should be re-calculated supposing this
5727                 ;; might be a message/rfc822 part.
5728                 (let (handles)
5729                   (dolist (part gnus-article-mime-handles)
5730                     (unless (or (stringp part)
5731                                 (equal (car (mm-handle-type part))
5732                                        "application/pgp-signature"))
5733                       (push part handles)))
5734                   (gnus-treat-article
5735                    nil (length (memq handle handles)) (length handles)
5736                    (mm-handle-media-type handle)))))))))))
5737
5738 (defun gnus-unbuttonized-mime-type-p (type)
5739   "Say whether TYPE is to be unbuttonized."
5740   (unless gnus-inhibit-mime-unbuttonizing
5741     (when (catch 'found
5742             (let ((types gnus-unbuttonized-mime-types))
5743               (while types
5744                 (when (string-match (pop types) type)
5745                   (throw 'found t)))))
5746       (not (catch 'found
5747              (let ((types gnus-buttonized-mime-types))
5748                (while types
5749                  (when (string-match (pop types) type)
5750                    (throw 'found t)))))))))
5751
5752 (defun gnus-article-insert-newline ()
5753   "Insert a newline, but mark it as undeletable."
5754   (gnus-put-text-property
5755    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5756
5757 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5758   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5759          (ihandles handles)
5760          (point (point))
5761          handle (inhibit-read-only t) from props begend not-pref)
5762     (save-window-excursion
5763       (save-restriction
5764         (when ibegend
5765           (narrow-to-region (car ibegend)
5766                             (or (cdr ibegend)
5767                                 (progn
5768                                   (goto-char (car ibegend))
5769                                   (forward-line 2)
5770                                   (point))))
5771           (delete-region (point-min) (point-max))
5772           (mm-remove-parts handles))
5773         (setq begend (list (point-marker)))
5774         ;; Do the toggle.
5775         (unless (setq not-pref (cadr (member preferred ihandles)))
5776           (setq not-pref (car ihandles)))
5777         (when (or ibegend
5778                   (not preferred)
5779                   (not (gnus-unbuttonized-mime-type-p
5780                         "multipart/alternative")))
5781           (gnus-add-text-properties
5782            (setq from (point))
5783            (progn
5784              (insert (format "%d.  " id))
5785              (point))
5786            `(gnus-callback
5787              (lambda (handles)
5788                (unless ,(not ibegend)
5789                  (setq gnus-article-mime-handle-alist
5790                        ',gnus-article-mime-handle-alist))
5791                (gnus-mime-display-alternative
5792                 ',ihandles ',not-pref ',begend ,id))
5793              keymap ,gnus-mime-button-map
5794              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5795              face ,gnus-article-button-face
5796              gnus-part ,id
5797              article-type multipart))
5798           (widget-convert-button 'link from (point)
5799                                  :action 'gnus-widget-press-button
5800                                  :button-keymap gnus-widget-button-keymap)
5801           ;; Do the handles
5802           (while (setq handle (pop handles))
5803             (gnus-add-text-properties
5804              (setq from (point))
5805              (progn
5806                (insert (format "(%c) %-18s"
5807                                (if (equal handle preferred) ?* ? )
5808                                (mm-handle-media-type handle)))
5809                (point))
5810              `(gnus-callback
5811                (lambda (handles)
5812                  (unless ,(not ibegend)
5813                    (setq gnus-article-mime-handle-alist
5814                          ',gnus-article-mime-handle-alist))
5815                  (gnus-mime-display-alternative
5816                   ',ihandles ',handle ',begend ,id))
5817                keymap ,gnus-mime-button-map
5818                ,gnus-mouse-face-prop ,gnus-article-mouse-face
5819                face ,gnus-article-button-face
5820                gnus-part ,id
5821                gnus-data ,handle))
5822             (widget-convert-button 'link from (point)
5823                                    :action 'gnus-widget-press-button
5824                                    :button-keymap gnus-widget-button-keymap)
5825             (insert "  "))
5826           (insert "\n\n"))
5827         (when preferred
5828           (if (stringp (car preferred))
5829               (gnus-display-mime preferred)
5830             (let ((mail-parse-charset gnus-newsgroup-charset)
5831                   (mail-parse-ignored-charsets
5832                    (with-current-buffer gnus-summary-buffer
5833                      gnus-newsgroup-ignored-charsets)))
5834               (mm-display-part preferred)
5835               ;; Do highlighting.
5836               (save-excursion
5837                 (save-restriction
5838                   (narrow-to-region (car begend) (point-max))
5839                   (gnus-treat-article
5840                    nil (length gnus-article-mime-handle-alist)
5841                    (gnus-article-mime-total-parts)
5842                    (mm-handle-media-type handle))))))
5843           (goto-char (point-max))
5844           (setcdr begend (point-marker)))))
5845     (when ibegend
5846       (goto-char point))))
5847
5848 (defconst gnus-article-wash-status-strings
5849   (let ((alist '((cite "c" "Possible hidden citation text"
5850                        " " "All citation text visible")
5851                  (headers "h" "Hidden headers"
5852                           " " "All headers visible.")
5853                  (pgp "p" "Encrypted or signed message status hidden"
5854                       " " "No hidden encryption nor digital signature status")
5855                  (signature "s" "Signature has been hidden"
5856                             " " "Signature is visible")
5857                  (overstrike "o" "Overstrike (^H) characters applied"
5858                              " " "No overstrike characters applied")
5859                  (emphasis "e" "/*_Emphasis_*/ characters applied"
5860                            " " "No /*_emphasis_*/ characters applied")))
5861         result)
5862     (dolist (entry alist result)
5863       (let ((key (nth 0 entry))
5864             (on (copy-sequence (nth 1 entry)))
5865             (on-help (nth 2 entry))
5866             (off (copy-sequence (nth 3 entry)))
5867             (off-help (nth 4 entry)))
5868         (put-text-property 0 1 'help-echo on-help on)
5869         (put-text-property 0 1 'help-echo off-help off)
5870         (push (list key on off) result))))
5871   "Alist of strings describing wash status in the mode line.
5872 Each entry has the form (KEY ON OF), where the KEY is a symbol
5873 representing the particular washing function, ON is the string to use
5874 in the article mode line when the washing function is active, and OFF
5875 is the string to use when it is inactive.")
5876
5877 (defun gnus-article-wash-status-entry (key value)
5878   (let ((entry (assoc key gnus-article-wash-status-strings)))
5879     (if value (nth 1 entry) (nth 2 entry))))
5880
5881 (defun gnus-article-wash-status ()
5882   "Return a string which display status of article washing."
5883   (with-current-buffer gnus-article-buffer
5884     (let ((cite (memq 'cite gnus-article-wash-types))
5885           (headers (memq 'headers gnus-article-wash-types))
5886           (boring (memq 'boring-headers gnus-article-wash-types))
5887           (pgp (memq 'pgp gnus-article-wash-types))
5888           (pem (memq 'pem gnus-article-wash-types))
5889           (signed (memq 'signed gnus-article-wash-types))
5890           (encrypted (memq 'encrypted gnus-article-wash-types))
5891           (signature (memq 'signature gnus-article-wash-types))
5892           (overstrike (memq 'overstrike gnus-article-wash-types))
5893           (emphasis (memq 'emphasis gnus-article-wash-types)))
5894       (concat
5895        (gnus-article-wash-status-entry 'cite cite)
5896        (gnus-article-wash-status-entry 'headers (or headers boring))
5897        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
5898        (gnus-article-wash-status-entry 'signature signature)
5899        (gnus-article-wash-status-entry 'overstrike overstrike)
5900        (gnus-article-wash-status-entry 'emphasis emphasis)))))
5901
5902 (defun gnus-add-wash-type (type)
5903   "Add a washing of TYPE to the current status."
5904   (add-to-list 'gnus-article-wash-types type))
5905
5906 (defun gnus-delete-wash-type (type)
5907   "Add a washing of TYPE to the current status."
5908   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
5909
5910 (defun gnus-add-image (category image)
5911   "Add IMAGE of CATEGORY to the list of displayed images."
5912   (let ((entry (assq category gnus-article-image-alist)))
5913     (unless entry
5914       (setq entry (list category))
5915       (push entry gnus-article-image-alist))
5916     (nconc entry (list image))))
5917
5918 (defun gnus-delete-images (category)
5919   "Delete all images in CATEGORY."
5920   (let ((entry (assq category gnus-article-image-alist)))
5921     (dolist (image (cdr entry))
5922       (gnus-remove-image image category))
5923     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
5924     (gnus-delete-wash-type category)))
5925
5926 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
5927
5928 (defun gnus-article-maybe-hide-headers ()
5929   "Hide unwanted headers if `gnus-have-all-headers' is nil.
5930 Provided for backwards compatibility."
5931   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
5932                  (not (with-current-buffer gnus-summary-buffer
5933                         gnus-have-all-headers)))
5934              (not gnus-inhibit-hiding))
5935     (gnus-article-hide-headers)))
5936
5937 ;;; Article savers.
5938
5939 (defun gnus-output-to-file (file-name)
5940   "Append the current article to a file named FILE-NAME.
5941 If `gnus-article-save-coding-system' is non-nil, it is used to encode
5942 text and used as the value of the coding cookie which is added to the
5943 top of a file.  Otherwise, this function saves a raw article without
5944 the coding cookie."
5945   (let* ((artbuf (current-buffer))
5946          (file-name-coding-system nnmail-pathname-coding-system)
5947          (coding gnus-article-save-coding-system)
5948          (coding-system-for-read (if coding
5949                                      nil ;; Rely on the coding cookie.
5950                                    mm-text-coding-system))
5951          (coding-system-for-write (or coding
5952                                       mm-text-coding-system-for-write
5953                                       mm-text-coding-system))
5954          (exists (file-exists-p file-name)))
5955     (with-temp-buffer
5956       (when exists
5957         (insert-file-contents file-name)
5958         (goto-char (point-min))
5959         ;; Remove the existing coding cookie.
5960         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
5961           (delete-region (match-beginning 0) (match-end 0))))
5962       (goto-char (point-max))
5963       (insert-buffer-substring artbuf)
5964       ;; Append newline at end of the buffer as separator, and then
5965       ;; save it to file.
5966       (goto-char (point-max))
5967       (insert "\n")
5968       (when coding
5969         ;; If the coding system is not suitable to encode the text,
5970         ;; ask a user for a proper one.
5971         (when (fboundp 'select-safe-coding-system)
5972           (setq coding (coding-system-base
5973                         (save-window-excursion
5974                           (select-safe-coding-system (point-min) (point-max)
5975                                                      coding))))
5976           (setq coding-system-for-write
5977                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
5978                     coding)))
5979         (goto-char (point-min))
5980         ;; Add the coding cookie.
5981         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
5982                         coding-system-for-write)))
5983       (if exists
5984           (progn
5985             (write-region (point-min) (point-max) file-name nil 'no-message)
5986             (message "Appended to %s" file-name))
5987         (write-region (point-min) (point-max) file-name))))
5988   t)
5989
5990 (defun gnus-narrow-to-page (&optional arg)
5991   "Narrow the article buffer to a page.
5992 If given a numerical ARG, move forward ARG pages."
5993   (interactive "P")
5994   (setq arg (if arg (prefix-numeric-value arg) 0))
5995   (with-current-buffer gnus-article-buffer
5996     (goto-char (point-min))
5997     (widen)
5998     ;; Remove any old next/prev buttons.
5999     (when (gnus-visual-p 'page-marker)
6000       (let ((inhibit-read-only t))
6001         (gnus-remove-text-with-property 'gnus-prev)
6002         (gnus-remove-text-with-property 'gnus-next)))
6003     (if
6004         (cond ((< arg 0)
6005                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
6006               ((> arg 0)
6007                (re-search-forward page-delimiter nil 'move arg)))
6008         (goto-char (match-end 0))
6009       (save-excursion
6010         (goto-char (point-min))
6011         (setq gnus-page-broken
6012               (and (re-search-forward page-delimiter nil t) t))))
6013     (when gnus-page-broken
6014       (narrow-to-region
6015        (point)
6016        (if (re-search-forward page-delimiter nil 'move)
6017            (match-beginning 0)
6018          (point)))
6019       (when (and (gnus-visual-p 'page-marker)
6020                  (> (point-min) (save-restriction (widen) (point-min))))
6021         (save-excursion
6022           (goto-char (point-min))
6023           (gnus-insert-prev-page-button)))
6024       (when (and (gnus-visual-p 'page-marker)
6025                  (< (point-max) (save-restriction (widen) (point-max))))
6026         (save-excursion
6027           (goto-char (point-max))
6028           (gnus-insert-next-page-button))))))
6029
6030 ;; Article mode commands
6031
6032 (defun gnus-article-goto-next-page ()
6033   "Show the next page of the article."
6034   (interactive)
6035   (when (gnus-article-next-page)
6036     (goto-char (point-min))
6037     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6038
6039
6040 (defun gnus-article-goto-prev-page ()
6041   "Show the previous page of the article."
6042   (interactive)
6043   (if (bobp)
6044       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6045     (gnus-article-prev-page nil)))
6046
6047 ;; This is cleaner but currently breaks `gnus-pick-mode':
6048 ;;
6049 ;; (defun gnus-article-goto-next-page ()
6050 ;;   "Show the next page of the article."
6051 ;;   (interactive)
6052 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6053 ;;     (gnus-summary-next-page)))
6054 ;;
6055 ;; (defun gnus-article-goto-prev-page ()
6056 ;;   "Show the next page of the article."
6057 ;;   (interactive)
6058 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6059 ;;     (gnus-summary-prev-page)))
6060
6061 (defun gnus-article-next-page (&optional lines)
6062   "Show the next page of the current article.
6063 If end of article, return non-nil.  Otherwise return nil.
6064 Argument LINES specifies lines to be scrolled up."
6065   (interactive "p")
6066   (move-to-window-line -1)
6067   (if (and (not (and gnus-article-over-scroll
6068                      (> (count-lines (window-start) (point-max))
6069                         (+ (or lines (1- (window-height)))
6070                            (or (and (boundp 'scroll-margin)
6071                                     (symbol-value 'scroll-margin))
6072                                0)))))
6073            (save-excursion
6074              (end-of-line)
6075              (and (pos-visible-in-window-p)     ;Not continuation line.
6076                   (>= (1+ (point)) (point-max))))) ;Allow for trailing newline.
6077       ;; Nothing in this page.
6078       (if (or (not gnus-page-broken)
6079               (save-excursion
6080                 (save-restriction
6081                   (widen)
6082                   (forward-line)
6083                   (eobp)))) ;Real end-of-buffer?
6084           (progn
6085             (when gnus-article-over-scroll
6086               (gnus-article-next-page-1 lines))
6087             t)                  ;Nothing more.
6088         (gnus-narrow-to-page 1)         ;Go to next page.
6089         nil)
6090     ;; More in this page.
6091     (gnus-article-next-page-1 lines)
6092     nil))
6093
6094 (defmacro gnus-article-beginning-of-window ()
6095   "Move point to the beginning of the window.
6096 In Emacs, the point is placed at the line number which `scroll-margin'
6097 specifies."
6098   (if (featurep 'xemacs)
6099       '(move-to-window-line 0)
6100     '(move-to-window-line
6101       (min (max 0 scroll-margin)
6102            (max 1 (- (window-height)
6103                      (if mode-line-format 1 0)
6104                      (if header-line-format 1 0)))))))
6105
6106 (defun gnus-article-next-page-1 (lines)
6107   (when (and (not (featurep 'xemacs))
6108              (numberp lines)
6109              (> lines 0)
6110              (numberp (symbol-value 'scroll-margin))
6111              (> (symbol-value 'scroll-margin) 0))
6112     ;; Protect against the bug that Emacs 21.x hangs up when scrolling up for
6113     ;; too many number of lines if `scroll-margin' is set as two or greater.
6114     (setq lines (min lines
6115                      (max 0 (- (count-lines (window-start) (point-max))
6116                                (symbol-value 'scroll-margin))))))
6117   (condition-case ()
6118       (let ((scroll-in-place nil))
6119         (scroll-up lines))
6120     (end-of-buffer
6121      ;; Long lines may cause an end-of-buffer error.
6122      (goto-char (point-max))))
6123   (gnus-article-beginning-of-window))
6124
6125 (defun gnus-article-prev-page (&optional lines)
6126   "Show previous page of current article.
6127 Argument LINES specifies lines to be scrolled down."
6128   (interactive "p")
6129   (move-to-window-line 0)
6130   (if (and gnus-page-broken
6131            (bobp)
6132            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6133       (progn
6134         (gnus-narrow-to-page -1)        ;Go to previous page.
6135         (goto-char (point-max))
6136         (recenter (if gnus-article-over-scroll
6137                       (if lines
6138                           (max (+ lines (or (and (boundp 'scroll-margin)
6139                                                  (symbol-value 'scroll-margin))
6140                                             0))
6141                                3)
6142                         (- (window-height) 2))
6143                     -1)))
6144     (prog1
6145         (condition-case ()
6146             (let ((scroll-in-place nil))
6147               (scroll-down lines))
6148           (beginning-of-buffer
6149            (goto-char (point-min))))
6150       (gnus-article-beginning-of-window))))
6151
6152 (defun gnus-article-only-boring-p ()
6153   "Decide whether there is only boring text remaining in the article.
6154 Something \"interesting\" is a word of at least two letters that does
6155 not have a face in `gnus-article-boring-faces'."
6156   (when (and gnus-article-skip-boring
6157              (boundp 'gnus-article-boring-faces)
6158              (symbol-value 'gnus-article-boring-faces))
6159     (save-excursion
6160       (let ((inhibit-point-motion-hooks t))
6161         (catch 'only-boring
6162           (while (re-search-forward "\\b\\w\\w" nil t)
6163             (forward-char -1)
6164             (when (not (gnus-intersection
6165                         (gnus-faces-at (point))
6166                         (symbol-value 'gnus-article-boring-faces)))
6167               (throw 'only-boring nil)))
6168           (throw 'only-boring t))))))
6169
6170 (defun gnus-article-refer-article ()
6171   "Read article specified by message-id around point."
6172   (interactive)
6173   (save-excursion
6174     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6175     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6176     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6177         (let ((msg-id (concat "<" (match-string 0) ">")))
6178           (set-buffer gnus-summary-buffer)
6179           (gnus-summary-refer-article msg-id))
6180       (error "No references around point"))))
6181
6182 (defun gnus-article-show-summary ()
6183   "Reconfigure windows to show summary buffer."
6184   (interactive)
6185   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6186       (error "There is no summary buffer for this article buffer")
6187     (gnus-article-set-globals)
6188     (gnus-configure-windows 'article)
6189     (gnus-summary-goto-subject gnus-current-article)
6190     (gnus-summary-position-point)))
6191
6192 (defun gnus-article-describe-briefly ()
6193   "Describe article mode commands briefly."
6194   (interactive)
6195   (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")))
6196
6197 (defun gnus-article-summary-command ()
6198   "Execute the last keystroke in the summary buffer."
6199   (interactive)
6200   (let ((obuf (current-buffer))
6201         (owin (current-window-configuration))
6202         func)
6203     (switch-to-buffer gnus-article-current-summary 'norecord)
6204     (setq func (lookup-key (current-local-map) (this-command-keys)))
6205     (call-interactively func)
6206     (set-buffer obuf)
6207     (set-window-configuration owin)
6208     (set-window-point (get-buffer-window (current-buffer)) (point))))
6209
6210 (defun gnus-article-summary-command-nosave ()
6211   "Execute the last keystroke in the summary buffer."
6212   (interactive)
6213   (let (func)
6214     (pop-to-buffer gnus-article-current-summary)
6215     (setq func (lookup-key (current-local-map) (this-command-keys)))
6216     (call-interactively func)))
6217
6218 (defun gnus-article-check-buffer ()
6219   "Beep if not in an article buffer."
6220   (unless (equal major-mode 'gnus-article-mode)
6221     (error "Command invoked outside of a Gnus article buffer")))
6222
6223 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6224   "Read a summary buffer key sequence and execute it from the article buffer."
6225   (interactive "P")
6226   (gnus-article-check-buffer)
6227   (let ((nosaves
6228          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6229            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6230            "=" "^" "\M-^" "|"))
6231         (nosave-but-article
6232          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6233            "An" "Ap" [?A (meta return)] [?A delete]))
6234         (nosave-in-article
6235          '("AS" "\C-d"))
6236         (up-to-top
6237          '("n" "Gn" "p" "Gp"))
6238         keys new-sum-point)
6239     (with-current-buffer gnus-article-current-summary
6240       (let (gnus-pick-mode)
6241         (setq unread-command-events (nconc unread-command-events
6242                                            (list (or key last-command-event)))
6243               keys (if (featurep 'xemacs)
6244                        (events-to-keys (read-key-sequence nil t))
6245                      (read-key-sequence nil t)))))
6246
6247     (message "")
6248
6249     (cond
6250      ((eq (aref keys (1- (length keys))) ?\C-h)
6251       (gnus-article-describe-bindings (substring keys 0 -1)))
6252      ((or (member keys nosaves)
6253           (member keys nosave-but-article)
6254           (member keys nosave-in-article))
6255       (let (func)
6256         (save-window-excursion
6257           (pop-to-buffer gnus-article-current-summary)
6258           ;; We disable the pick minor mode commands.
6259           (let (gnus-pick-mode)
6260             (setq func (lookup-key (current-local-map) keys))))
6261         (if (or (not func)
6262                 (numberp func))
6263             (ding)
6264           (unless (member keys nosave-in-article)
6265             (set-buffer gnus-article-current-summary))
6266           (call-interactively func)
6267           (setq new-sum-point (point)))
6268         (when (member keys nosave-but-article)
6269           (pop-to-buffer gnus-article-buffer))))
6270      (t
6271       ;; These commands should restore window configuration.
6272       (let ((obuf (current-buffer))
6273             (owin (current-window-configuration))
6274             win func in-buffer selected new-sum-start new-sum-hscroll err)
6275         (cond (not-restore-window
6276                (pop-to-buffer gnus-article-current-summary)
6277                (setq win (selected-window)))
6278               ((setq win (get-buffer-window gnus-article-current-summary))
6279                (select-window win))
6280               (t
6281                (let ((summary-buffer gnus-article-current-summary))
6282                  (gnus-configure-windows 'article)
6283                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6284                    (let ((gnus-buffer-configuration
6285                           '(article ((vertical 1.0
6286                                                (summary 0.25 point)
6287                                                (article 1.0))))))
6288                      (gnus-configure-windows 'article))
6289                    (setq win (get-buffer-window summary-buffer 'visible)))
6290                  (gnus-select-frame-set-input-focus (window-frame win))
6291                  (select-window win))))
6292         (setq in-buffer (current-buffer))
6293         ;; We disable the pick minor mode commands.
6294         (if (and (setq func (let (gnus-pick-mode)
6295                               (lookup-key (current-local-map) keys)))
6296                  (functionp func)
6297                  (condition-case code
6298                      (progn
6299                        (call-interactively func)
6300                        t)
6301                    (error
6302                     (setq err code)
6303                     nil)))
6304             (progn
6305               (when (eq win (selected-window))
6306                 (setq new-sum-point (point)
6307                       new-sum-start (window-start win)
6308                       new-sum-hscroll (window-hscroll win)))
6309               (when (or (eq in-buffer (current-buffer))
6310                         (when (eq obuf (current-buffer))
6311                           (set-buffer in-buffer)
6312                           t))
6313                 (setq selected (gnus-summary-select-article))
6314                 (set-buffer obuf)
6315                 (unless not-restore-window
6316                   (set-window-configuration owin))
6317                 (when (and (eq selected 'old)
6318                            new-sum-point)
6319                   (set-window-start (get-buffer-window (current-buffer))
6320                                     1)
6321                   (set-window-point (get-buffer-window (current-buffer))
6322                                     (if (article-goto-body)
6323                                         (1- (point))
6324                                       (point))))
6325                 (when (and (not not-restore-window)
6326                            new-sum-point
6327                            (with-current-buffer (window-buffer win)
6328                              (eq major-mode 'gnus-summary-mode)))
6329                   (set-window-point win new-sum-point)
6330                   (set-window-start win new-sum-start)
6331                   (set-window-hscroll win new-sum-hscroll))))
6332           (set-window-configuration owin)
6333           (if err
6334               (signal (car err) (cdr err))
6335             (ding))))))))
6336
6337 (defun gnus-article-read-summary-send-keys ()
6338   (interactive)
6339   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6340     (gnus-article-read-summary-keys)))
6341
6342 (defun gnus-article-describe-key (key)
6343   "Display documentation of the function invoked by KEY.
6344 KEY is a string or a vector."
6345   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6346                        (read-key-sequence "Describe key: "))))
6347   (gnus-article-check-buffer)
6348   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6349                                   gnus-article-read-summary-send-keys))
6350       (with-current-buffer gnus-article-current-summary
6351         (setq unread-command-events
6352               (if (featurep 'xemacs)
6353                   (append key nil)
6354                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6355                                         (list 'meta (- x 128))
6356                                       x))
6357                         key)))
6358         (let ((cursor-in-echo-area t)
6359               gnus-pick-mode)
6360           (describe-key (read-key-sequence nil t))))
6361     (describe-key key)))
6362
6363 (defun gnus-article-describe-key-briefly (key &optional insert)
6364   "Display documentation of the function invoked by KEY.
6365 KEY is a string or a vector."
6366   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6367                        (read-key-sequence "Describe key: "))
6368                      current-prefix-arg))
6369   (gnus-article-check-buffer)
6370   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6371                                   gnus-article-read-summary-send-keys))
6372       (with-current-buffer gnus-article-current-summary
6373         (setq unread-command-events
6374               (if (featurep 'xemacs)
6375                   (append key nil)
6376                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6377                                         (list 'meta (- x 128))
6378                                       x))
6379                         key)))
6380         (let ((cursor-in-echo-area t)
6381               gnus-pick-mode)
6382           (describe-key-briefly (read-key-sequence nil t) insert)))
6383     (describe-key-briefly key insert)))
6384
6385 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6386 (defvar gnus-agent-summary-mode)
6387 (defvar gnus-draft-mode)
6388
6389 (defun gnus-article-describe-bindings (&optional prefix)
6390   "Show a list of all defined keys, and their definitions.
6391 The optional argument PREFIX, if non-nil, should be a key sequence;
6392 then we display only bindings that start with that prefix."
6393   (interactive)
6394   (gnus-article-check-buffer)
6395   (let ((keymap (copy-keymap gnus-article-mode-map))
6396         (map (copy-keymap gnus-article-send-map))
6397         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6398         agent draft)
6399     (define-key keymap "S" map)
6400     (define-key map [t] nil)
6401     (with-current-buffer gnus-article-current-summary
6402       (set-keymap-parent map (key-binding "S"))
6403       (let (key def gnus-pick-mode)
6404         (while sumkeys
6405           (setq key (pop sumkeys))
6406           (cond ((and (vectorp key) (= (length key) 1)
6407                       (consp (setq def (aref key 0)))
6408                       (numberp (car def)) (numberp (cdr def)))
6409                  (when (< (max (car def) (cdr def)) 128)
6410                    (setq sumkeys
6411                          (append (mapcar
6412                                   #'vector
6413                                   (nreverse (gnus-uncompress-range def)))
6414                                  sumkeys))))
6415                 ((setq def (key-binding key))
6416                  (unless (eq def 'undefined)
6417                    (define-key keymap key def))))))
6418       (when (boundp 'gnus-agent-summary-mode)
6419         (setq agent gnus-agent-summary-mode))
6420       (when (boundp 'gnus-draft-mode)
6421         (setq draft gnus-draft-mode)))
6422     (with-temp-buffer
6423       (use-local-map keymap)
6424       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6425       (set (make-local-variable 'gnus-draft-mode) draft)
6426       (describe-bindings prefix))
6427     (let ((item `((lambda (prefix)
6428                     (with-current-buffer ,(current-buffer)
6429                       (gnus-article-describe-bindings prefix)))
6430                   ,prefix)))
6431       (with-current-buffer (if (fboundp 'help-buffer)
6432                                (let (help-xref-following) (help-buffer))
6433                              "*Help*") ;; Emacs 21
6434         (setq help-xref-stack-item item)))))
6435
6436 (defun gnus-article-reply-with-original (&optional wide)
6437   "Start composing a reply mail to the current message.
6438 The text in the region will be yanked.  If the region isn't active,
6439 the entire article will be yanked."
6440   (interactive)
6441   (let ((article (cdr gnus-article-current))
6442         contents)
6443     (if (not (gnus-region-active-p))
6444         (with-current-buffer gnus-summary-buffer
6445           (gnus-summary-reply (list (list article)) wide))
6446       (setq contents (buffer-substring (point) (mark t)))
6447       ;; Deactivate active regions.
6448       (when (and (boundp 'transient-mark-mode)
6449                  transient-mark-mode)
6450         (setq mark-active nil))
6451       (with-current-buffer gnus-summary-buffer
6452         (gnus-summary-reply
6453          (list (list article contents)) wide)))))
6454
6455 (defun gnus-article-wide-reply-with-original ()
6456   "Start composing a wide reply mail to the current message.
6457 The text in the region will be yanked.  If the region isn't active,
6458 the entire article will be yanked."
6459   (interactive)
6460   (gnus-article-reply-with-original t))
6461
6462 (defun gnus-article-followup-with-original ()
6463   "Compose a followup to the current article.
6464 The text in the region will be yanked.  If the region isn't active,
6465 the entire article will be yanked."
6466   (interactive)
6467   (let ((article (cdr gnus-article-current))
6468         contents)
6469       (if (not (gnus-region-active-p))
6470           (with-current-buffer gnus-summary-buffer
6471             (gnus-summary-followup (list (list article))))
6472         (setq contents (buffer-substring (point) (mark t)))
6473         ;; Deactivate active regions.
6474         (when (and (boundp 'transient-mark-mode)
6475                    transient-mark-mode)
6476           (setq mark-active nil))
6477         (with-current-buffer gnus-summary-buffer
6478           (gnus-summary-followup
6479            (list (list article contents)))))))
6480
6481 (defun gnus-article-hide (&optional arg force)
6482   "Hide all the gruft in the current article.
6483 This means that signatures, cited text and (some) headers will be
6484 hidden.
6485 If given a prefix, show the hidden text instead."
6486   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6487   (gnus-article-hide-headers arg)
6488   (gnus-article-hide-list-identifiers arg)
6489   (gnus-article-hide-citation-maybe arg force)
6490   (gnus-article-hide-signature arg))
6491
6492 (defun gnus-article-maybe-highlight ()
6493   "Do some article highlighting if article highlighting is requested."
6494   (when (gnus-visual-p 'article-highlight 'highlight)
6495     (gnus-article-highlight-some)))
6496
6497 (defun gnus-check-group-server ()
6498   ;; Make sure the connection to the server is alive.
6499   (unless (gnus-server-opened
6500            (gnus-find-method-for-group gnus-newsgroup-name))
6501     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6502     (gnus-request-group gnus-newsgroup-name t)))
6503
6504 (eval-when-compile
6505   (autoload 'nneething-get-file-name "nneething"))
6506
6507 (defun gnus-request-article-this-buffer (article group)
6508   "Get an article and insert it into this buffer."
6509   (let (do-update-line sparse-header)
6510     (prog1
6511         (save-excursion
6512           (erase-buffer)
6513           (gnus-kill-all-overlays)
6514           (setq group (or group gnus-newsgroup-name))
6515
6516           ;; Using `gnus-request-article' directly will insert the article into
6517           ;; `nntp-server-buffer' - so we'll save some time by not having to
6518           ;; copy it from the server buffer into the article buffer.
6519
6520           ;; We only request an article by message-id when we do not have the
6521           ;; headers for it, so we'll have to get those.
6522           (when (stringp article)
6523             (gnus-read-header article))
6524
6525           ;; If the article number is negative, that means that this article
6526           ;; doesn't belong in this newsgroup (possibly), so we find its
6527           ;; message-id and request it by id instead of number.
6528           (when (and (numberp article)
6529                      gnus-summary-buffer
6530                      (get-buffer gnus-summary-buffer)
6531                      (gnus-buffer-exists-p gnus-summary-buffer))
6532             (with-current-buffer gnus-summary-buffer
6533               (let ((header (gnus-summary-article-header article)))
6534                 (when (< article 0)
6535                   (cond
6536                    ((memq article gnus-newsgroup-sparse)
6537                     ;; This is a sparse gap article.
6538                     (setq do-update-line article)
6539                     (setq article (mail-header-id header))
6540                     (setq sparse-header (gnus-read-header article))
6541                     (setq gnus-newsgroup-sparse
6542                           (delq article gnus-newsgroup-sparse)))
6543                    ((vectorp header)
6544                     ;; It's a real article.
6545                     (setq article (mail-header-id header)))
6546                    (t
6547                     ;; It is an extracted pseudo-article.
6548                     (setq article 'pseudo)
6549                     (gnus-request-pseudo-article header))))
6550
6551                 (let ((method (gnus-find-method-for-group
6552                                gnus-newsgroup-name)))
6553                   (when (and (eq (car method) 'nneething)
6554                              (vectorp header))
6555                     (let ((dir (nneething-get-file-name
6556                                 (mail-header-id header))))
6557                       (when (and (stringp dir)
6558                                  (file-directory-p dir))
6559                         (setq article 'nneething)
6560                         (gnus-group-enter-directory dir))))))))
6561
6562           (cond
6563            ;; Refuse to select canceled articles.
6564            ((and (numberp article)
6565                  gnus-summary-buffer
6566                  (get-buffer gnus-summary-buffer)
6567                  (gnus-buffer-exists-p gnus-summary-buffer)
6568                  (eq (cdr (with-current-buffer gnus-summary-buffer
6569                             (assq article gnus-newsgroup-reads)))
6570                      gnus-canceled-mark))
6571             nil)
6572            ;; We first check `gnus-original-article-buffer'.
6573            ((and (get-buffer gnus-original-article-buffer)
6574                  (numberp article)
6575                  (with-current-buffer gnus-original-article-buffer
6576                    (and (equal (car gnus-original-article) group)
6577                         (eq (cdr gnus-original-article) article))))
6578             (insert-buffer-substring gnus-original-article-buffer)
6579             'article)
6580            ;; Check the backlog.
6581            ((and gnus-keep-backlog
6582                  (gnus-backlog-request-article group article (current-buffer)))
6583             'article)
6584            ;; Check asynchronous pre-fetch.
6585            ((gnus-async-request-fetched-article group article (current-buffer))
6586             (gnus-async-prefetch-next group article gnus-summary-buffer)
6587             (when (and (numberp article) gnus-keep-backlog)
6588               (gnus-backlog-enter-article group article (current-buffer)))
6589             'article)
6590            ;; Check the cache.
6591            ((and gnus-use-cache
6592                  (numberp article)
6593                  (gnus-cache-request-article article group))
6594             'article)
6595            ;; Check the agent cache.
6596            ((gnus-agent-request-article article group)
6597             'article)
6598            ;; Get the article and put into the article buffer.
6599            ((or (stringp article)
6600                 (numberp article))
6601             (let ((gnus-override-method gnus-override-method)
6602                   (methods (and (stringp article)
6603                                 gnus-refer-article-method))
6604                   (backend (car (gnus-find-method-for-group
6605                                  gnus-newsgroup-name)))
6606                   result
6607                   (inhibit-read-only t))
6608               (if (or (not (listp methods))
6609                       (and (symbolp (car methods))
6610                            (assq (car methods) nnoo-definition-alist)))
6611                   (setq methods (list methods)))
6612               (when (and (null gnus-override-method)
6613                          methods)
6614                 (setq gnus-override-method (pop methods)))
6615               (while (not result)
6616                 (when (eq gnus-override-method 'current)
6617                   (setq gnus-override-method
6618                         (with-current-buffer gnus-summary-buffer
6619                           gnus-current-select-method)))
6620                 (erase-buffer)
6621                 (gnus-kill-all-overlays)
6622                 (let ((gnus-newsgroup-name group))
6623                   (gnus-check-group-server))
6624                 (cond
6625                  ((gnus-request-article article group (current-buffer))
6626                   (when (numberp article)
6627                     (gnus-async-prefetch-next group article
6628                                               gnus-summary-buffer)
6629                     (when gnus-keep-backlog
6630                       (gnus-backlog-enter-article
6631                        group article (current-buffer))))
6632                   (setq result 'article))
6633                  (methods
6634                   (setq gnus-override-method (pop methods)))
6635                  ((not (string-match "^400 "
6636                                      (nnheader-get-report backend)))
6637                   ;; If we get 400 server disconnect, reconnect and
6638                   ;; retry; otherwise, assume the article has expired.
6639                   (setq result 'done))))
6640               (and (eq result 'article) 'article)))
6641            ;; It was a pseudo.
6642            (t article)))
6643
6644       ;; Associate this article with the current summary buffer.
6645       (setq gnus-article-current-summary gnus-summary-buffer)
6646
6647       ;; Take the article from the original article buffer
6648       ;; and place it in the buffer it's supposed to be in.
6649       (when (and (get-buffer gnus-article-buffer)
6650                  (equal (buffer-name (current-buffer))
6651                         (buffer-name (get-buffer gnus-article-buffer))))
6652         (save-excursion
6653           (if (get-buffer gnus-original-article-buffer)
6654               (set-buffer gnus-original-article-buffer)
6655             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6656             (buffer-disable-undo)
6657             (setq major-mode 'gnus-original-article-mode)
6658             (setq buffer-read-only t))
6659           (let ((inhibit-read-only t))
6660             (erase-buffer)
6661             (insert-buffer-substring gnus-article-buffer))
6662           (setq gnus-original-article (cons group article)))
6663
6664         ;; Decode charsets.
6665         (run-hooks 'gnus-article-decode-hook)
6666         ;; Mark article as decoded or not.
6667         (setq gnus-article-decoded-p gnus-article-decode-hook))
6668
6669       ;; Update sparse articles.
6670       (when (and do-update-line
6671                  (or (numberp article)
6672                      (stringp article)))
6673         (let ((buf (current-buffer)))
6674           (set-buffer gnus-summary-buffer)
6675           (gnus-summary-update-article do-update-line sparse-header)
6676           (gnus-summary-goto-subject do-update-line nil t)
6677           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6678                             (point))
6679           (set-buffer buf))))))
6680
6681 ;;;
6682 ;;; Article editing
6683 ;;;
6684
6685 (defcustom gnus-article-edit-mode-hook nil
6686   "Hook run in article edit mode buffers."
6687   :group 'gnus-article-various
6688   :type 'hook)
6689
6690 (defvar gnus-article-edit-done-function nil)
6691
6692 (defvar gnus-article-edit-mode-map nil)
6693
6694 ;; Should we be using derived.el for this?
6695 (unless gnus-article-edit-mode-map
6696   (setq gnus-article-edit-mode-map (make-keymap))
6697   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6698
6699   (gnus-define-keys gnus-article-edit-mode-map
6700     "\C-c?"    describe-mode
6701     "\C-c\C-c" gnus-article-edit-done
6702     "\C-c\C-k" gnus-article-edit-exit
6703     "\C-c\C-f\C-t" message-goto-to
6704     "\C-c\C-f\C-o" message-goto-from
6705     "\C-c\C-f\C-b" message-goto-bcc
6706     ;;"\C-c\C-f\C-w" message-goto-fcc
6707     "\C-c\C-f\C-c" message-goto-cc
6708     "\C-c\C-f\C-s" message-goto-subject
6709     "\C-c\C-f\C-r" message-goto-reply-to
6710     "\C-c\C-f\C-n" message-goto-newsgroups
6711     "\C-c\C-f\C-d" message-goto-distribution
6712     "\C-c\C-f\C-f" message-goto-followup-to
6713     "\C-c\C-f\C-m" message-goto-mail-followup-to
6714     "\C-c\C-f\C-k" message-goto-keywords
6715     "\C-c\C-f\C-u" message-goto-summary
6716     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6717     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6718     "\C-c\C-b" message-goto-body
6719     "\C-c\C-i" message-goto-signature
6720
6721     "\C-c\C-t" message-insert-to
6722     "\C-c\C-n" message-insert-newsgroups
6723     "\C-c\C-o" message-sort-headers
6724     "\C-c\C-e" message-elide-region
6725     "\C-c\C-v" message-delete-not-region
6726     "\C-c\C-z" message-kill-to-signature
6727     "\M-\r" message-newline-and-reformat
6728     "\C-c\C-a" mml-attach-file
6729     "\C-a" message-beginning-of-line
6730     "\t" message-tab
6731     "\M-;" comment-region)
6732
6733   (gnus-define-keys (gnus-article-edit-wash-map
6734                      "\C-c\C-w" gnus-article-edit-mode-map)
6735     "f" gnus-article-edit-full-stops))
6736
6737 (easy-menu-define
6738   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6739   '("Field"
6740     ["Fetch To" message-insert-to t]
6741     ["Fetch Newsgroups" message-insert-newsgroups t]
6742     "----"
6743     ["To" message-goto-to t]
6744     ["From" message-goto-from t]
6745     ["Subject" message-goto-subject t]
6746     ["Cc" message-goto-cc t]
6747     ["Reply-To" message-goto-reply-to t]
6748     ["Summary" message-goto-summary t]
6749     ["Keywords" message-goto-keywords t]
6750     ["Newsgroups" message-goto-newsgroups t]
6751     ["Followup-To" message-goto-followup-to t]
6752     ["Mail-Followup-To" message-goto-mail-followup-to t]
6753     ["Distribution" message-goto-distribution t]
6754     ["Body" message-goto-body t]
6755     ["Signature" message-goto-signature t]))
6756
6757 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6758   "Major mode for editing articles.
6759 This is an extended text-mode.
6760
6761 \\{gnus-article-edit-mode-map}"
6762   (make-local-variable 'gnus-article-edit-done-function)
6763   (make-local-variable 'gnus-prev-winconf)
6764   (set (make-local-variable 'font-lock-defaults)
6765        '(message-font-lock-keywords t))
6766   (set (make-local-variable 'mail-header-separator) "")
6767   (set (make-local-variable 'gnus-article-edit-mode) t)
6768   (easy-menu-add message-mode-field-menu message-mode-map)
6769   (mml-mode)
6770   (setq buffer-read-only nil)
6771   (buffer-enable-undo)
6772   (widen))
6773
6774 (defun gnus-article-edit (&optional force)
6775   "Edit the current article.
6776 This will have permanent effect only in mail groups.
6777 If FORCE is non-nil, allow editing of articles even in read-only
6778 groups."
6779   (interactive "P")
6780   (when (and (not force)
6781              (gnus-group-read-only-p))
6782     (error "The current newsgroup does not support article editing"))
6783   (gnus-article-date-original)
6784   (gnus-article-edit-article
6785    'ignore
6786    `(lambda (no-highlight)
6787       'ignore
6788       (gnus-summary-edit-article-done
6789        ,(or (mail-header-references gnus-current-headers) "")
6790        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
6791
6792 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
6793   "Start editing the contents of the current article buffer."
6794   (let ((winconf (current-window-configuration)))
6795     (set-buffer gnus-article-buffer)
6796     (let ((message-auto-save-directory
6797            ;; Don't associate the article buffer with a draft file.
6798            nil))
6799       (gnus-article-edit-mode))
6800     (funcall start-func)
6801     (set-buffer-modified-p nil)
6802     (gnus-configure-windows 'edit-article)
6803     (setq gnus-article-edit-done-function exit-func)
6804     (setq gnus-prev-winconf winconf)
6805     (unless quiet
6806       (gnus-message 6 "C-c C-c to end edits"))))
6807
6808 (defun gnus-article-edit-done (&optional arg)
6809   "Update the article edits and exit."
6810   (interactive "P")
6811   (let ((func gnus-article-edit-done-function)
6812         (buf (current-buffer))
6813         (start (window-start))
6814         (p (point))
6815         (winconf gnus-prev-winconf))
6816     (widen) ;; Widen it in case that users narrowed the buffer.
6817     (funcall func arg)
6818     (set-buffer buf)
6819     ;; The cache and backlog have to be flushed somewhat.
6820     (when gnus-keep-backlog
6821       (gnus-backlog-remove-article
6822        (car gnus-article-current) (cdr gnus-article-current)))
6823     ;; Flush original article as well.
6824     (when (get-buffer gnus-original-article-buffer)
6825       (with-current-buffer gnus-original-article-buffer
6826         (setq gnus-original-article nil)))
6827     (when gnus-use-cache
6828       (gnus-cache-update-article
6829        (car gnus-article-current) (cdr gnus-article-current)))
6830     ;; We remove all text props from the article buffer.
6831     (kill-all-local-variables)
6832     (set-text-properties (point-min) (point-max) nil)
6833     (gnus-article-mode)
6834     (set-window-configuration winconf)
6835     (set-buffer buf)
6836     (set-window-start (get-buffer-window buf) start)
6837     (set-window-point (get-buffer-window buf) (point)))
6838   (gnus-summary-show-article))
6839
6840 (defun gnus-article-edit-exit ()
6841   "Exit the article editing without updating."
6842   (interactive)
6843   (when (or (not (buffer-modified-p))
6844             (yes-or-no-p "Article modified; kill anyway? "))
6845     (let ((curbuf (current-buffer))
6846           (p (point))
6847           (window-start (window-start)))
6848       (erase-buffer)
6849       (if (gnus-buffer-live-p gnus-original-article-buffer)
6850           (insert-buffer-substring gnus-original-article-buffer))
6851       (let ((winconf gnus-prev-winconf))
6852         (kill-all-local-variables)
6853         (gnus-article-mode)
6854         (set-window-configuration winconf)
6855         ;; Tippy-toe some to make sure that point remains where it was.
6856         (save-current-buffer
6857           (set-buffer curbuf)
6858           (set-window-start (get-buffer-window (current-buffer)) window-start)
6859           (goto-char p))))
6860     (gnus-summary-show-article)))
6861
6862 (defun gnus-article-edit-full-stops ()
6863   "Interactively repair spacing at end of sentences."
6864   (interactive)
6865   (save-excursion
6866     (goto-char (point-min))
6867     (search-forward-regexp "^$" nil t)
6868     (let ((case-fold-search nil))
6869       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
6870
6871 ;;;
6872 ;;; Article highlights
6873 ;;;
6874
6875 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
6876
6877 ;;; Internal Variables:
6878
6879 (defcustom gnus-button-url-regexp
6880   (concat
6881    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
6882    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
6883    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
6884    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
6885        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
6886              (punct "!?:;.,"))
6887          (concat
6888           "\\(?:"
6889           ;; Match paired parentheses, e.g. in Wikipedia URLs:
6890           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]"
6891           "\\|"
6892           "[" chars punct     "]+" "[" chars "]"
6893           "\\)"))
6894      (concat ;; XEmacs 21.4 doesn't support POSIX.
6895       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
6896       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
6897    "\\)")
6898   "Regular expression that matches URLs."
6899   :group 'gnus-article-buttons
6900   :type 'regexp)
6901
6902 (defcustom gnus-button-valid-fqdn-regexp
6903   message-valid-fqdn-regexp
6904   "Regular expression that matches a valid FQDN."
6905   :version "22.1"
6906   :group 'gnus-article-buttons
6907   :type 'regexp)
6908
6909 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
6910 (defcustom gnus-button-valid-localpart-regexp
6911   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
6912   "Regular expression that matches a localpart of mail addresses or MIDs."
6913   :version "22.1"
6914   :group 'gnus-article-buttons
6915   :type 'regexp)
6916
6917 (defcustom gnus-button-man-handler 'manual-entry
6918   "Function to use for displaying man pages.
6919 The function must take at least one argument with a string naming the
6920 man page."
6921   :version "22.1"
6922   :type '(choice (function-item :tag "Man" manual-entry)
6923                  (function-item :tag "Woman" woman)
6924                  (function :tag "Other"))
6925   :group 'gnus-article-buttons)
6926
6927 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
6928   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
6929 If the default site is too slow, try to find a CTAN mirror, see
6930 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
6931 the variable `gnus-button-handle-ctan'."
6932   :version "22.1"
6933   :group 'gnus-article-buttons
6934   :link '(custom-manual "(gnus)Group Parameters")
6935   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
6936                  (const "http://tug.ctan.org/tex-archive/")
6937                  (const "http://www.dante.de/CTAN/")
6938                  (string :tag "Other")))
6939
6940 (defcustom gnus-button-ctan-handler 'browse-url
6941   "Function to use for displaying CTAN links.
6942 The function must take one argument, the string naming the URL."
6943   :version "22.1"
6944   :type '(choice (function-item :tag "Browse Url" browse-url)
6945                  (function :tag "Other"))
6946   :group 'gnus-article-buttons)
6947
6948 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
6949   "Bogus strings removed from CTAN URLs."
6950   :version "22.1"
6951   :group 'gnus-article-buttons
6952   :type '(choice (const "^/?tex-archive/\\|/")
6953                  (regexp :tag "Other")))
6954
6955 (defcustom gnus-button-ctan-directory-regexp
6956   (regexp-opt
6957    (list "archive-tools" "biblio" "bibliography" "digests" "documentation"
6958          "dviware" "fonts" "graphics" "help" "indexing" "info" "language"
6959          "languages" "macros" "nonfree" "obsolete" "support" "systems"
6960          "tds" "tools" "usergrps" "web") t)
6961   "Regular expression for ctan directories.
6962 It should match all directories in the top level of `gnus-ctan-url'."
6963   :version "22.1"
6964   :group 'gnus-article-buttons
6965   :type 'regexp)
6966
6967 (defcustom gnus-button-mid-or-mail-regexp
6968   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
6969           gnus-button-valid-fqdn-regexp
6970           ">?\\)\\b")
6971   "Regular expression that matches a message ID or a mail address."
6972   :version "22.1"
6973   :group 'gnus-article-buttons
6974   :type 'regexp)
6975
6976 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
6977   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
6978 Strings like this can be either a message ID or a mail address.  If it is one
6979 of the symbols `mid' or `mail', Gnus will always assume that the string is a
6980 message ID or a mail address, respectively.  If this variable is set to the
6981 symbol `ask', always query the user what do do.  If it is a function, this
6982 function will be called with the string as its only argument.  The function
6983 must return `mid', `mail', `invalid' or `ask'."
6984   :version "22.1"
6985   :group 'gnus-article-buttons
6986   :type '(choice (function-item :tag "Heuristic function"
6987                                 gnus-button-mid-or-mail-heuristic)
6988                  (const ask)
6989                  (const mid)
6990                  (const mail)))
6991
6992 (defcustom gnus-button-mid-or-mail-heuristic-alist
6993   '((-10.0 . ".+\\$.+@")
6994     (-10.0 . "#")
6995     (-10.0 . "\\*")
6996     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
6997     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
6998     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
6999     (-1.0  . "^[^a-z]+@")
7000     ;;
7001     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7002     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7003     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7004     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7005     ;;
7006     (-2.0  . "^[0-9]")
7007     (-1.0  . "^[0-9][0-9]")
7008     ;;
7009     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7010     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7011     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7012     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7013     ;;
7014     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7015     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7016     ;;       "[0-9]{8,}.*\@"
7017     (-3.0
7018      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7019     ;; "[0-9]{12,}.*\@"
7020     ;; compensation for TDMA dated mail addresses:
7021     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7022     ;;
7023     (-20.0 . "\\.fsf@") ;; Gnus
7024     (-20.0 . "^slrn")
7025     (-20.0 . "^Pine")
7026     (-20.0 . "^alpine\\.")
7027     (-20.0 . "_-_") ;; Subject change in thread
7028     ;;
7029     (-20.0 . "\\.ln@") ;; leafnode
7030     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7031     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7032     ;;
7033     ;; (5.0 . "") ;; $local_part_len <= 7
7034     (10.0  . "^[^0-9]+@")
7035     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7036     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7037     (3.0   . "\@stud")
7038     ;;
7039     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7040     ;;
7041     (0.5   . "^[A-Z][a-z]")
7042     (0.5   . "^[A-Z][a-z][a-z]")
7043     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7044     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7045   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7046
7047 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7048 address.  The REGEXP is processed with `case-fold-search' set to nil."
7049   :version "22.1"
7050   :group 'gnus-article-buttons
7051   :type '(repeat (cons (number :tag "Rate")
7052                        (regexp :tag "Regexp"))))
7053
7054 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7055   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7056 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7057 address, `ask' if unsure and `invalid' if the string is invalid."
7058   (let ((case-fold-search nil)
7059         (list gnus-button-mid-or-mail-heuristic-alist)
7060         (result 0) rate regexp lpartlen elem)
7061     (setq lpartlen
7062           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7063     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7064     ;; Certain special cases...
7065     (when (string-match
7066            (concat
7067             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7068             "^[0-9]+\\.[0-9]+@compuserve\\|"
7069             "@public\\.gmane\\.org")
7070            mid-or-mail)
7071       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7072       (setq result 'mail))
7073     (when (string-match "@.*@\\| " mid-or-mail)
7074       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7075       (setq result 'invalid))
7076     ;; Nothing more to do, if result is not a number here...
7077     (when (numberp result)
7078       (while list
7079         (setq elem (car list)
7080               rate (car elem)
7081               regexp (cdr elem)
7082               list (cdr list))
7083         (when (string-match regexp mid-or-mail)
7084           (setq result (+ result rate))
7085           (gnus-message
7086            9 "`%s' matched `%s', rate `%s', result `%s'."
7087            mid-or-mail regexp rate result)))
7088       (when (<= lpartlen 7)
7089         (setq result (+ result 5.0))
7090         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7091                       mid-or-mail result))
7092       (when (>= lpartlen 12)
7093         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7094         (cond
7095          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7096           ;; Long local part should contain realname if e-mail address,
7097           ;; too many digits: message-id.
7098           ;; $score -= 5.0 + 0.1 * $local_part_len;
7099           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7100           (setq result (+ result rate))
7101           (gnus-message
7102            9 "Many digits in `%s', rate `%s', result `%s'."
7103            mid-or-mail rate result))
7104          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7105                         mid-or-mail)
7106           ;; Too few vowels [^aeiouy]{4,}.*\@
7107           (setq result (+ result -5.0))
7108           (gnus-message
7109            9 "Few vowels in `%s', rate `%s', result `%s'."
7110            mid-or-mail -5.0 result))
7111          (t
7112           (setq result (+ result 5.0))
7113           (gnus-message
7114            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7115     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7116     ;; Maybe we should make this a customizable alist: (condition . 'result)
7117     (cond
7118      ((symbolp result) result)
7119      ;; Now convert number into proper results:
7120      ((< result -10.0) 'mid)
7121      ((> result  10.0) 'mail)
7122      (t 'ask))))
7123
7124 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7125   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7126          (url-mid (concat "news" ":" mid-or-mail))
7127          (url-mailto (concat "mailto" ":" mid-or-mail)))
7128     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7129     (when (fboundp pref)
7130       (setq guessed
7131             ;; get rid of surrounding angles...
7132             (funcall pref
7133                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7134       (if (or (eq 'mid guessed) (eq 'mail guessed))
7135           (setq pref guessed)
7136         (setq pref 'ask)))
7137     (if (eq pref 'ask)
7138         (save-window-excursion
7139           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7140               (setq pref 'mail)
7141             (setq pref 'mid))))
7142     (cond ((eq pref 'mid)
7143            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7144            (gnus-button-handle-news url-mid))
7145           ((eq pref 'mail)
7146            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7147            (gnus-url-mailto url-mailto))
7148           (t (gnus-message 3 "Invalid string.")))))
7149
7150 (defun gnus-button-handle-custom (fun arg)
7151   "Call function FUN on argument ARG.
7152 Both FUN and ARG are supposed to be strings.  ARG will be passed
7153 as a symbol to FUN."
7154   (funcall (intern fun)
7155            (if (string-match "^customize-apropos" fun)
7156                arg
7157              (intern arg))))
7158
7159 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7160
7161 ;; FIXME: Maybe we should merge some of the functions that do quite similar
7162 ;; stuff?
7163
7164 (defun gnus-button-handle-describe-function (url)
7165   "Call `describe-function' when pushing the corresponding URL button."
7166   (describe-function
7167    (intern
7168     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7169
7170 (defun gnus-button-handle-describe-variable (url)
7171   "Call `describe-variable' when pushing the corresponding URL button."
7172   (describe-variable
7173    (intern
7174     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7175
7176 (defun gnus-button-handle-symbol (url)
7177 "Display help on variable or function.
7178 Calls `describe-variable' or `describe-function'."
7179   (let ((sym (intern url)))
7180     (cond
7181      ((fboundp sym) (describe-function sym))
7182      ((boundp sym) (describe-variable sym))
7183      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7184
7185 (defun gnus-button-handle-describe-key (url)
7186   "Call `describe-key' when pushing the corresponding URL button."
7187   (let* ((key-string
7188           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7189          (keys (ignore-errors (eval `(kbd ,key-string)))))
7190     (if keys
7191         (describe-key keys)
7192       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7193
7194 (defun gnus-button-handle-apropos (url)
7195   "Call `apropos' when pushing the corresponding URL button."
7196   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7197
7198 (defun gnus-button-handle-apropos-command (url)
7199   "Call `apropos' when pushing the corresponding URL button."
7200   (apropos-command
7201    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7202
7203 (defun gnus-button-handle-apropos-variable (url)
7204   "Call `apropos' when pushing the corresponding URL button."
7205   (funcall
7206    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7207    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7208
7209 (defun gnus-button-handle-apropos-documentation (url)
7210   "Call `apropos' when pushing the corresponding URL button."
7211   (funcall
7212    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7213    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7214
7215 (defun gnus-button-handle-library (url)
7216   "Call `locate-library' when pushing the corresponding URL button."
7217   (gnus-message 9 "url=`%s'" url)
7218   (let* ((lib (locate-library url))
7219          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7220     (if (not lib)
7221         (gnus-message 1 "Cannot locale library `%s'." url)
7222       (find-file-read-only file))))
7223
7224 (defun gnus-button-handle-ctan (url)
7225   "Call `browse-url' when pushing a CTAN URL button."
7226   (funcall
7227    gnus-button-ctan-handler
7228    (concat
7229     gnus-ctan-url
7230     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
7231
7232 (defcustom gnus-button-tex-level 5
7233   "*Integer that says how many TeX-related buttons Gnus will show.
7234 The higher the number, the more buttons will appear and the more false
7235 positives are possible.  Note that you can set this variable local to
7236 specific groups.  Setting it higher in TeX groups is probably a good idea.
7237 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7238 how to set variables in specific groups."
7239   :version "22.1"
7240   :group 'gnus-article-buttons
7241   :link '(custom-manual "(gnus)Group Parameters")
7242   :type 'integer)
7243
7244 (defcustom gnus-button-man-level 5
7245   "*Integer that says how many man-related buttons Gnus will show.
7246 The higher the number, the more buttons will appear and the more false
7247 positives are possible.  Note that you can set this variable local to
7248 specific groups.  Setting it higher in Unix groups is probably a good idea.
7249 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7250 how to set variables in specific groups."
7251   :version "22.1"
7252   :group 'gnus-article-buttons
7253   :link '(custom-manual "(gnus)Group Parameters")
7254   :type 'integer)
7255
7256 (defcustom gnus-button-emacs-level 5
7257   "*Integer that says how many emacs-related buttons Gnus will show.
7258 The higher the number, the more buttons will appear and the more false
7259 positives are possible.  Note that you can set this variable local to
7260 specific groups.  Setting it higher in Emacs or Gnus related groups is
7261 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7262 `gnus-parameters' on how to set variables in specific groups."
7263   :version "22.1"
7264   :group 'gnus-article-buttons
7265   :link '(custom-manual "(gnus)Group Parameters")
7266   :type 'integer)
7267
7268 (defcustom gnus-button-message-level 5
7269   "*Integer that says how many buttons for news or mail messages will appear.
7270 The higher the number, the more buttons will appear and the more false
7271 positives are possible."
7272   ;; mail addresses, MIDs, URLs for news, ...
7273   :version "22.1"
7274   :group 'gnus-article-buttons
7275   :type 'integer)
7276
7277 (defcustom gnus-button-browse-level 5
7278   "*Integer that says how many buttons for browsing will appear.
7279 The higher the number, the more buttons will appear and the more false
7280 positives are possible."
7281   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7282   :version "22.1"
7283   :group 'gnus-article-buttons
7284   :type 'integer)
7285
7286 (defcustom gnus-button-alist
7287   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7288      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7289     ((concat "\\b\\(nntp\\|news\\):\\("
7290              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7291      0 t gnus-button-handle-news 2)
7292     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7293      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7294     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7295      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7296     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7297     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7298      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7299     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7300      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7301     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7302      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7303     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7304      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7305     ;; RFC 2368 (The mailto URL scheme)
7306     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7307      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7308     ("\\bmailto:\\([^ \n\t]+\\)"
7309      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7310     ;; CTAN
7311     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
7312              gnus-button-ctan-directory-regexp
7313              "[^][>)!;:,'\n\t ]+\\)")
7314      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
7315     ((concat "\\btex-archive/\\("
7316              gnus-button-ctan-directory-regexp
7317              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
7318      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
7319     ((concat
7320       "\\b\\("
7321       gnus-button-ctan-directory-regexp
7322       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
7323      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
7324     ;; This is info (home-grown style) <info://foo/bar+baz>
7325     ("\\binfo://\\([^'\">\n\t ]+\\)"
7326      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7327     ;; Info GNOME style <info:foo#bar_baz>
7328     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7329      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7330     ;; Info KDE style <info:(foo)bar baz>
7331     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7332      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7333     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7334      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7335     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
7336      ;; Info links like `C-h i d m CC Mode RET'
7337      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 2)
7338     ;; This is custom
7339     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
7340      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7341     ;; Emacs help commands
7342     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7343      ;; regexp doesn't match arguments containing ` '.
7344      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7345     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7346      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7347     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7348      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7349     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7350      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7351     ;; The following entries may lead to many false positives so don't enable
7352     ;; them by default (use a high button level).
7353     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7354      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7355      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7356     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7357      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7358     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7359      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7360     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7361      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7362     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7363      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7364     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7365      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7366     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7367      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7368     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7369      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7370     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7371      ;; Unlike the other regexps we really have to require quoting
7372      ;; here to determine where it ends.
7373      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7374     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7375     ("<URL: *\\([^\n<>]*\\)>"
7376      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7377     ;; RFC 2396 (2.4.3., delims) ...
7378     ("\"URL: *\\([^\n\"]*\\)\""
7379      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7380     ;; Raw URLs.
7381     (gnus-button-url-regexp
7382      0 (>= gnus-button-browse-level 0) browse-url 0)
7383     ;; man pages
7384     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7385      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7386      gnus-button-handle-man 1)
7387     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7388     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7389      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7390      gnus-button-handle-man 1)
7391     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7392     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7393     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7394      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7395     ;; Recognizing patches to .el files.  This is somewhat obscure,
7396     ;; but considering the percentage of Gnus users who hack Emacs
7397     ;; Lisp files...
7398     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7399      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7400     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7401      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7402     ;; MID or mail: To avoid too many false positives we don't try to catch
7403     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7404     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7405     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7406     ;; so that non-ambiguous entries (see above) match first.
7407     (gnus-button-mid-or-mail-regexp
7408      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7409   "*Alist of regexps matching buttons in article bodies.
7410
7411 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7412 REGEXP: is the string (case insensitive) matching text around the button (can
7413 also be Lisp expression evaluating to a string),
7414 BUTTON: is the number of the regexp grouping actually matching the button,
7415 FORM: is a Lisp expression which must eval to true for the button to
7416 be added,
7417 CALLBACK: is the function to call when the user push this button, and each
7418 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7419
7420 CALLBACK can also be a variable, in that case the value of that
7421 variable it the real callback function."
7422   :group 'gnus-article-buttons
7423   :type '(repeat (list (choice regexp variable sexp)
7424                        (integer :tag "Button")
7425                        (sexp :tag "Form")
7426                        (function :tag "Callback")
7427                        (repeat :tag "Par"
7428                                :inline t
7429                                (integer :tag "Regexp group")))))
7430 (put 'gnus-button-alist 'risky-local-variable t)
7431
7432 (defcustom gnus-header-button-alist
7433   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7434      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7435     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7436      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7437     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7438      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7439     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7440      0 (>= gnus-button-browse-level 0) browse-url 0)
7441     ("^Subject:" gnus-button-url-regexp
7442      0 (>= gnus-button-browse-level 0) browse-url 0)
7443     ("^[^:]+:" gnus-button-url-regexp
7444      0 (>= gnus-button-browse-level 0) browse-url 0)
7445     ("^OpenPGP:.*url=" gnus-button-url-regexp
7446      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7447     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7448      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7449     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7450      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7451   "*Alist of headers and regexps to match buttons in article heads.
7452
7453 This alist is very similar to `gnus-button-alist', except that each
7454 alist has an additional HEADER element first in each entry:
7455
7456 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7457
7458 HEADER is a regexp to match a header.  For a fuller explanation, see
7459 `gnus-button-alist'."
7460   :group 'gnus-article-buttons
7461   :group 'gnus-article-headers
7462   :type '(repeat (list (regexp :tag "Header")
7463                        (choice regexp variable)
7464                        (integer :tag "Button")
7465                        (sexp :tag "Form")
7466                        (function :tag "Callback")
7467                        (repeat :tag "Par"
7468                                :inline t
7469                                (integer :tag "Regexp group")))))
7470 (put 'gnus-header-button-alist 'risky-local-variable t)
7471
7472 ;;; Commands:
7473
7474 (defun gnus-article-push-button (event)
7475   "Check text under the mouse pointer for a callback function.
7476 If the text under the mouse pointer has a `gnus-callback' property,
7477 call it with the value of the `gnus-data' text property."
7478   (interactive "e")
7479   (set-buffer (window-buffer (posn-window (event-start event))))
7480   (let* ((pos (posn-point (event-start event)))
7481          (data (get-text-property pos 'gnus-data))
7482          (fun (get-text-property pos 'gnus-callback)))
7483     (goto-char pos)
7484     (when fun
7485       (funcall fun data))))
7486
7487 (defun gnus-article-press-button ()
7488   "Check text at point for a callback function.
7489 If the text at point has a `gnus-callback' property,
7490 call it with the value of the `gnus-data' text property."
7491   (interactive)
7492   (let ((data (get-text-property (point) 'gnus-data))
7493         (fun (get-text-property (point) 'gnus-callback)))
7494     (when fun
7495       (funcall fun data))))
7496
7497 (defun gnus-article-highlight (&optional force)
7498   "Highlight current article.
7499 This function calls `gnus-article-highlight-headers',
7500 `gnus-article-highlight-citation',
7501 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7502 do the highlighting.  See the documentation for those functions."
7503   (interactive (list 'force))
7504   (gnus-article-highlight-headers)
7505   (gnus-article-highlight-citation force)
7506   (gnus-article-highlight-signature)
7507   (gnus-article-add-buttons force)
7508   (gnus-article-add-buttons-to-head))
7509
7510 (defun gnus-article-highlight-some (&optional force)
7511   "Highlight current article.
7512 This function calls `gnus-article-highlight-headers',
7513 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7514 do the highlighting.  See the documentation for those functions."
7515   (interactive (list 'force))
7516   (gnus-article-highlight-headers)
7517   (gnus-article-highlight-signature)
7518   (gnus-article-add-buttons))
7519
7520 (defun gnus-article-highlight-headers ()
7521   "Highlight article headers as specified by `gnus-header-face-alist'."
7522   (interactive)
7523   (gnus-with-article-headers
7524     (let (regexp header-face field-face from hpoints fpoints)
7525       (dolist (entry gnus-header-face-alist)
7526         (goto-char (point-min))
7527         (setq regexp (concat "^\\("
7528                              (if (string-equal "" (nth 0 entry))
7529                                  "[^\t ]"
7530                                (nth 0 entry))
7531                              "\\)")
7532               header-face (nth 1 entry)
7533               field-face (nth 2 entry))
7534         (while (and (re-search-forward regexp nil t)
7535                     (not (eobp)))
7536           (beginning-of-line)
7537           (setq from (point))
7538           (unless (search-forward ":" nil t)
7539             (forward-char 1))
7540           (when (and header-face
7541                      (not (memq (point) hpoints)))
7542             (push (point) hpoints)
7543             (gnus-put-text-property from (point) 'face header-face))
7544           (when (and field-face
7545                      (not (memq (setq from (point)) fpoints)))
7546             (push from fpoints)
7547             (if (re-search-forward "^[^ \t]" nil t)
7548                 (forward-char -2)
7549               (goto-char (point-max)))
7550             (gnus-put-text-property from (point) 'face field-face)))))))
7551
7552 (defun gnus-article-highlight-signature ()
7553   "Highlight the signature in an article.
7554 It does this by highlighting everything after
7555 `gnus-signature-separator' using the face `gnus-signature'."
7556   (interactive)
7557   (gnus-with-article-buffer
7558     (let ((inhibit-point-motion-hooks t))
7559       (save-restriction
7560         (when (and gnus-signature-face
7561                    (gnus-article-narrow-to-signature))
7562           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7563                             'face gnus-signature-face)
7564           (widen)
7565           (gnus-article-search-signature)
7566           (let ((start (match-beginning 0))
7567                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7568             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7569                                      end)))))))
7570
7571 (defun gnus-button-in-region-p (b e prop)
7572   "Say whether PROP exists in the region."
7573   (text-property-not-all b e prop nil))
7574
7575 (defun gnus-article-add-buttons (&optional force)
7576   "Find external references in the article and make buttons of them.
7577 \"External references\" are things like Message-IDs and URLs, as
7578 specified by `gnus-button-alist'."
7579   (interactive (list 'force))
7580   (gnus-with-article-buffer
7581     (let ((inhibit-point-motion-hooks t)
7582           (case-fold-search t)
7583           (alist gnus-button-alist)
7584           beg entry regexp)
7585       ;; Remove all old markers.
7586       (let (marker entry new-list)
7587         (while (setq marker (pop gnus-button-marker-list))
7588           (if (or (< marker (point-min)) (>= marker (point-max)))
7589               (push marker new-list)
7590             (goto-char marker)
7591             (when (setq entry (gnus-button-entry))
7592               (put-text-property (match-beginning (nth 1 entry))
7593                                  (match-end (nth 1 entry))
7594                                  'gnus-callback nil))
7595             (set-marker marker nil)))
7596         (setq gnus-button-marker-list new-list))
7597       ;; We skip the headers.
7598       (article-goto-body)
7599       (setq beg (point))
7600       (while (setq entry (pop alist))
7601         (setq regexp (eval (car entry)))
7602         (goto-char beg)
7603         (while (re-search-forward regexp nil t)
7604           (let ((start (match-beginning (nth 1 entry)))
7605                 (end (match-end (nth 1 entry)))
7606                 (from (match-beginning 0)))
7607             (when (and (or (eq t (nth 2 entry))
7608                            (eval (nth 2 entry)))
7609                        (not (gnus-button-in-region-p
7610                              start end 'gnus-callback)))
7611               ;; That optional form returned non-nil, so we add the
7612               ;; button.
7613               (setq from (set-marker (make-marker) from))
7614               (push from gnus-button-marker-list)
7615               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7616                            (gnus-article-extend-url-button from start end))
7617                 (gnus-article-add-button start end
7618                                          'gnus-button-push from)))))))))
7619
7620 (defun gnus-article-extend-url-button (beg start end)
7621   "Extend url button if url is folded into two or more lines.
7622 Return non-nil if button is extended.  BEG is a marker that points to
7623 the beginning position of a text containing url.  START and END are
7624 the endpoints of a url button before it is extended.  The concatenated
7625 url is put as the `gnus-button-url' overlay property on the button."
7626   (let ((opoint (point))
7627         (points (list start end))
7628         url delim regexp)
7629     (prog1
7630         (when (and (progn
7631                      (goto-char end)
7632                      (not (looking-at "[\t ]*[\">]")))
7633                    (progn
7634                      (goto-char start)
7635                      (string-match
7636                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7637                       (buffer-substring (point-at-bol) start)))
7638                    (progn
7639                      (setq url (list (buffer-substring start end))
7640                            delim (if (match-beginning 1) ">" "\""))
7641                      (beginning-of-line)
7642                      (setq regexp (concat
7643                                    (when (and (looking-at
7644                                                message-cite-prefix-regexp)
7645                                               (< (match-end 0) start))
7646                                      (regexp-quote (match-string 0)))
7647                                    "\
7648 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7649                                    delim "\\)"))
7650                      (while (progn
7651                               (forward-line 1)
7652                               (and (looking-at regexp)
7653                                    (prog1
7654                                        (match-beginning 1)
7655                                      (push (or (match-string 2)
7656                                                (match-string 1))
7657                                            url)
7658                                      (push (setq end (or (match-end 2)
7659                                                          (match-end 1)))
7660                                            points)
7661                                      (push (or (match-beginning 2)
7662                                                (match-beginning 1))
7663                                            points)))))
7664                      (match-beginning 2)))
7665           (let (gnus-article-mouse-face widget-mouse-face)
7666             (while points
7667               (gnus-article-add-button (pop points) (pop points)
7668                                        'gnus-button-push beg)))
7669           (let ((overlay (gnus-make-overlay start end)))
7670             (gnus-overlay-put overlay 'evaporate t)
7671             (gnus-overlay-put overlay 'gnus-button-url
7672                               (list (mapconcat 'identity (nreverse url) "")))
7673             (when gnus-article-mouse-face
7674               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7675           t)
7676       (goto-char opoint))))
7677
7678 ;; Add buttons to the head of an article.
7679 (defun gnus-article-add-buttons-to-head ()
7680   "Add buttons to the head of the article."
7681   (interactive)
7682   (gnus-with-article-headers
7683     (let (beg end)
7684       (dolist (entry gnus-header-button-alist)
7685         ;; Each alist entry.
7686         (goto-char (point-min))
7687         (while (re-search-forward (car entry) nil t)
7688           ;; Each header matching the entry.
7689           (setq beg (match-beginning 0))
7690           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7691                              (match-beginning 0))
7692                         (point-max)))
7693           (goto-char beg)
7694           (while (re-search-forward (eval (nth 1 entry)) end t)
7695             ;; Each match within a header.
7696             (let* ((entry (cdr entry))
7697                    (start (match-beginning (nth 1 entry)))
7698                    (end (match-end (nth 1 entry)))
7699                    (form (nth 2 entry)))
7700               (goto-char (match-end 0))
7701               (when (eval form)
7702                 (gnus-article-add-button
7703                  start end (nth 3 entry)
7704                  (buffer-substring (match-beginning (nth 4 entry))
7705                                    (match-end (nth 4 entry)))))))
7706           (goto-char end))))))
7707
7708 ;;; External functions:
7709
7710 (defun gnus-article-add-button (from to fun &optional data)
7711   "Create a button between FROM and TO with callback FUN and data DATA."
7712   (when gnus-article-button-face
7713     (gnus-overlay-put (gnus-make-overlay from to nil t)
7714                       'face gnus-article-button-face))
7715   (gnus-add-text-properties
7716    from to
7717    (nconc (and gnus-article-mouse-face
7718                (list gnus-mouse-face-prop gnus-article-mouse-face))
7719           (list 'gnus-callback fun)
7720           (and data (list 'gnus-data data))))
7721   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7722                          :button-keymap gnus-widget-button-keymap))
7723
7724 ;;; Internal functions:
7725
7726 (defun gnus-article-set-globals ()
7727   (with-current-buffer gnus-summary-buffer
7728     (gnus-set-global-variables)))
7729
7730 (defun gnus-signature-toggle (end)
7731   (gnus-with-article-buffer
7732     (let ((inhibit-point-motion-hooks t))
7733       (if (text-property-any end (point-max) 'article-type 'signature)
7734           (progn
7735             (gnus-delete-wash-type 'signature)
7736             (gnus-remove-text-properties-when
7737              'article-type 'signature end (point-max)
7738              (cons 'article-type (cons 'signature
7739                                        gnus-hidden-properties))))
7740         (gnus-add-wash-type 'signature)
7741         (gnus-add-text-properties-when
7742          'article-type nil end (point-max)
7743          (cons 'article-type (cons 'signature
7744                                    gnus-hidden-properties)))))
7745     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7746       (gnus-set-mode-line 'article))))
7747
7748 (defun gnus-button-entry ()
7749   ;; Return the first entry in `gnus-button-alist' matching this place.
7750   (let ((alist gnus-button-alist)
7751         (entry nil))
7752     (while alist
7753       (setq entry (pop alist))
7754       (if (looking-at (eval (car entry)))
7755           (setq alist nil)
7756         (setq entry nil)))
7757     entry))
7758
7759 (defun gnus-button-push (marker)
7760   ;; Push button starting at MARKER.
7761   (save-excursion
7762     (goto-char marker)
7763     (let* ((entry (gnus-button-entry))
7764            (inhibit-point-motion-hooks t)
7765            (fun (nth 3 entry))
7766            (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7767                           (get-char-property marker 'gnus-button-url))
7768                      (mapcar (lambda (group)
7769                                (let ((string (match-string group)))
7770                                  (set-text-properties
7771                                   0 (length string) nil string)
7772                                  string))
7773                              (nthcdr 4 entry)))))
7774       (cond
7775        ((fboundp fun)
7776         (apply fun args))
7777        ((and (boundp fun)
7778              (fboundp (symbol-value fun)))
7779         (apply (symbol-value fun) args))
7780        (t
7781         (gnus-message 1 "You must define `%S' to use this button"
7782                       (cons fun args)))))))
7783
7784 (defun gnus-parse-news-url (url)
7785   (let (scheme server port group message-id articles)
7786     (with-temp-buffer
7787       (insert url)
7788       (goto-char (point-min))
7789       (when (looking-at "\\([A-Za-z]+\\):")
7790         (setq scheme (match-string 1))
7791         (goto-char (match-end 0)))
7792       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7793         (setq server (match-string 1))
7794         (setq port (if (stringp (match-string 3))
7795                        (string-to-number (match-string 3))
7796                      (match-string 3)))
7797         (goto-char (match-end 0)))
7798
7799       (cond
7800        ((looking-at "\\(.*@.*\\)")
7801         (setq message-id (match-string 1)))
7802        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7803         (setq group (match-string 1)
7804               articles (split-string (match-string 2) "-")))
7805        ((looking-at "\\([^/]+\\)/?")
7806         (setq group (match-string 1)))
7807        (t
7808         (error "Unknown news URL syntax"))))
7809     (list scheme server port group message-id articles)))
7810
7811 (defun gnus-button-handle-news (url)
7812   "Fetch a news URL."
7813   (destructuring-bind (scheme server port group message-id articles)
7814       (gnus-parse-news-url url)
7815     (cond
7816      (message-id
7817       (with-current-buffer gnus-summary-buffer
7818         (if server
7819             (let ((gnus-refer-article-method
7820                    (nconc (list (list 'nntp server))
7821                           gnus-refer-article-method))
7822                   (nntp-port-number (or port "nntp")))
7823               (gnus-message 7 "Fetching %s with %s"
7824                             message-id gnus-refer-article-method)
7825               (gnus-summary-refer-article message-id))
7826           (gnus-summary-refer-article message-id))))
7827      (group
7828       (gnus-button-fetch-group url)))))
7829
7830 (defun gnus-button-patch (library line)
7831   "Visit an Emacs Lisp library LIBRARY on line LINE."
7832   (interactive)
7833   (let ((file (locate-library (file-name-nondirectory library))))
7834     (unless file
7835       (error "Couldn't find library %s" library))
7836     (find-file file)
7837     (goto-line (string-to-number line))))
7838
7839 (defun gnus-button-handle-man (url)
7840   "Fetch a man page."
7841   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7842   (when (eq gnus-button-man-handler 'woman)
7843     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
7844   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7845   (funcall gnus-button-man-handler url))
7846
7847 (defun gnus-button-handle-info-url (url)
7848   "Fetch an info URL."
7849   (setq url (mm-subst-char-in-string ?+ ?\  url))
7850   (cond
7851    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
7852     (gnus-info-find-node
7853      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
7854                      "Gnus")
7855              ")" (gnus-url-unhex-string (match-string 2 url)))))
7856    ((string-match "([^)\"]+)[^\"]+" url)
7857     (setq url
7858           (gnus-replace-in-string
7859            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
7860     (gnus-info-find-node url))
7861    (t (error "Can't parse %s" url))))
7862
7863 (defun gnus-button-handle-info-url-gnome (url)
7864   "Fetch GNOME style info URL."
7865   (setq url (mm-subst-char-in-string ?_ ?\  url))
7866   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
7867       (gnus-info-find-node
7868        (concat "("
7869                (gnus-url-unhex-string
7870                  (match-string 1 url))
7871                ")"
7872                (or (gnus-url-unhex-string
7873                     (match-string 2 url))
7874                    "Top")))
7875     (error "Can't parse %s" url)))
7876
7877 (defun gnus-button-handle-info-url-kde (url)
7878   "Fetch KDE style info URL."
7879   (gnus-info-find-node (gnus-url-unhex-string url)))
7880
7881 ;; (info) will autoload info.el
7882 (declare-function Info-menu "info" (menu-item &optional fork))
7883
7884 (defun gnus-button-handle-info-keystrokes (url)
7885   "Call `info' when pushing the corresponding URL button."
7886   ;; For links like `C-h i d m gnus RET', `C-h i d m CC Mode RET'.
7887   (info)
7888   (Info-directory)
7889   (Info-menu url))
7890
7891 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
7892 (declare-function pgg-display-output-buffer "pgg" (start end status))
7893
7894 (defun gnus-button-openpgp (url)
7895   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
7896   (with-temp-buffer
7897     (mm-url-insert-file-contents-external url)
7898     (pgg-snarf-keys-region (point-min) (point-max))
7899     (pgg-display-output-buffer nil nil nil)))
7900
7901 (defun gnus-button-message-id (message-id)
7902   "Fetch MESSAGE-ID."
7903   (with-current-buffer gnus-summary-buffer
7904     (gnus-summary-refer-article message-id)))
7905
7906 (defun gnus-button-fetch-group (address &rest ignore)
7907   "Fetch GROUP specified by ADDRESS."
7908   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
7909                       address)
7910     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
7911     ;; for nntp:// and news://
7912     (setq address (match-string 3 address)))
7913   (if (not (string-match "[:/]" address))
7914       ;; This is just a simple group url.
7915       (gnus-group-read-ephemeral-group address gnus-select-method)
7916     (if (not
7917          (string-match
7918           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
7919           address))
7920         (error "Can't parse %s" address)
7921       (gnus-group-read-ephemeral-group
7922        (match-string 4 address)
7923        `(nntp ,(match-string 1 address)
7924               (nntp-address ,(match-string 1 address))
7925               (nntp-port-number ,(if (match-end 3)
7926                                      (match-string 3 address)
7927                                    "nntp")))
7928        nil nil nil
7929        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
7930
7931 (defun gnus-url-parse-query-string (query &optional downcase)
7932   (let (retval pairs cur key val)
7933     (setq pairs (split-string query "&"))
7934     (while pairs
7935       (setq cur (car pairs)
7936             pairs (cdr pairs))
7937       (if (not (string-match "=" cur))
7938           nil                           ; Grace
7939         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
7940               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
7941         (if downcase
7942             (setq key (downcase key)))
7943         (setq cur (assoc key retval))
7944         (if cur
7945             (setcdr cur (cons val (cdr cur)))
7946           (setq retval (cons (list key val) retval)))))
7947     retval))
7948
7949 (defun gnus-url-mailto (url)
7950   ;; Send mail to someone
7951   (when (string-match "mailto:/*\\(.*\\)" url)
7952     (setq url (substring url (match-beginning 1) nil)))
7953   (let (to args subject func)
7954     (setq args (gnus-url-parse-query-string
7955                 (if (string-match "^\\?" url)
7956                     (substring url 1)
7957                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
7958                       (concat "to=" (match-string 1 url) "&"
7959                               (match-string 2 url))
7960                     (concat "to=" url)))
7961                 t)
7962           subject (cdr-safe (assoc "subject" args)))
7963     (gnus-msg-mail)
7964     (while args
7965       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
7966       (if (fboundp func)
7967           (funcall func)
7968         (message-position-on-field (caar args)))
7969       (insert (gnus-replace-in-string
7970                (mapconcat 'identity (reverse (cdar args)) ", ")
7971                "\r\n" "\n" t))
7972       (setq args (cdr args)))
7973     (if subject
7974         (message-goto-body)
7975       (message-goto-subject))))
7976
7977 (defun gnus-button-embedded-url (address)
7978   "Activate ADDRESS with `browse-url'."
7979   (browse-url (gnus-strip-whitespace address)))
7980
7981 ;;; Next/prev buttons in the article buffer.
7982
7983 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
7984 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
7985
7986 (defvar gnus-prev-page-map
7987   (let ((map (make-sparse-keymap)))
7988     (define-key map gnus-mouse-2 'gnus-button-prev-page)
7989     (define-key map "\r" 'gnus-button-prev-page)
7990     map))
7991
7992 (defvar gnus-next-page-map
7993   (let ((map (make-sparse-keymap)))
7994     (unless (>= emacs-major-version 21)
7995       ;; XEmacs doesn't care.
7996       (set-keymap-parent map gnus-article-mode-map))
7997     (define-key map gnus-mouse-2 'gnus-button-next-page)
7998     (define-key map "\r" 'gnus-button-next-page)
7999     map))
8000
8001 (defun gnus-insert-prev-page-button ()
8002   (let ((b (point)) e
8003         (inhibit-read-only t))
8004     (gnus-eval-format
8005      gnus-prev-page-line-format nil
8006      `(keymap ,gnus-prev-page-map
8007               gnus-prev t
8008               gnus-callback gnus-article-button-prev-page
8009               article-type annotation))
8010     (setq e (if (bolp)
8011                 ;; Exclude a newline.
8012                 (1- (point))
8013               (point)))
8014     (when gnus-article-button-face
8015       (gnus-overlay-put (gnus-make-overlay b e nil t)
8016                         'face gnus-article-button-face))
8017     (widget-convert-button
8018      'link b e
8019      :action 'gnus-button-prev-page
8020      :button-keymap gnus-prev-page-map)))
8021
8022 (defun gnus-button-next-page (&optional args more-args)
8023   "Go to the next page."
8024   (interactive)
8025   (let ((win (selected-window)))
8026     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8027     (gnus-article-next-page)
8028     (select-window win)))
8029
8030 (defun gnus-button-prev-page (&optional args more-args)
8031   "Go to the prev page."
8032   (interactive)
8033   (let ((win (selected-window)))
8034     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8035     (gnus-article-prev-page)
8036     (select-window win)))
8037
8038 (defun gnus-insert-next-page-button ()
8039   (let ((b (point)) e
8040         (inhibit-read-only t))
8041     (gnus-eval-format gnus-next-page-line-format nil
8042                       `(keymap ,gnus-next-page-map
8043                                gnus-next t
8044                                gnus-callback gnus-article-button-next-page
8045                                article-type annotation))
8046     (setq e (if (bolp)
8047                 ;; Exclude a newline.
8048                 (1- (point))
8049               (point)))
8050     (when gnus-article-button-face
8051       (gnus-overlay-put (gnus-make-overlay b e nil t)
8052                         'face gnus-article-button-face))
8053     (widget-convert-button
8054      'link b e
8055      :action 'gnus-button-next-page
8056      :button-keymap gnus-next-page-map)))
8057
8058 (defun gnus-article-button-next-page (arg)
8059   "Go to the next page."
8060   (interactive "P")
8061   (let ((win (selected-window)))
8062     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8063     (gnus-article-next-page)
8064     (select-window win)))
8065
8066 (defun gnus-article-button-prev-page (arg)
8067   "Go to the prev page."
8068   (interactive "P")
8069   (let ((win (selected-window)))
8070     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8071     (gnus-article-prev-page)
8072     (select-window win)))
8073
8074 (defvar gnus-decode-header-methods
8075   '(mail-decode-encoded-word-region)
8076   "List of methods used to decode headers.
8077
8078 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8079 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8080 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8081 whose names match REGEXP.
8082
8083 For example:
8084 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8085  mail-decode-encoded-word-region
8086  (\"chinese\" . rfc1843-decode-region))
8087 ")
8088
8089 (defvar gnus-decode-header-methods-cache nil)
8090
8091 (defun gnus-multi-decode-header (start end)
8092   "Apply the functions from `gnus-encoded-word-methods' that match."
8093   (unless (and gnus-decode-header-methods-cache
8094                (eq gnus-newsgroup-name
8095                    (car gnus-decode-header-methods-cache)))
8096     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8097     (dolist (x gnus-decode-header-methods)
8098       (if (symbolp x)
8099           (nconc gnus-decode-header-methods-cache (list x))
8100         (if (and gnus-newsgroup-name
8101                  (string-match (car x) gnus-newsgroup-name))
8102             (nconc gnus-decode-header-methods-cache
8103                    (list (cdr x)))))))
8104   (let ((xlist gnus-decode-header-methods-cache))
8105     (pop xlist)
8106     (save-restriction
8107       (narrow-to-region start end)
8108       (while xlist
8109         (funcall (pop xlist) (point-min) (point-max))))))
8110
8111 ;;;
8112 ;;; Treatment top-level handling.
8113 ;;;
8114
8115 (defun gnus-treat-article (condition &optional part-number total-parts type)
8116   (let ((length (- (point-max) (point-min)))
8117         (alist gnus-treatment-function-alist)
8118         (article-goto-body-goes-to-point-min-p t)
8119         (treated-type
8120          (or (not type)
8121              (catch 'found
8122                (let ((list gnus-article-treat-types))
8123                  (while list
8124                    (when (string-match (pop list) type)
8125                      (throw 'found t)))))))
8126         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8127         val elem)
8128     (gnus-run-hooks 'gnus-part-display-hook)
8129     (dolist (elem alist)
8130       (setq val
8131             (save-excursion
8132               (when (gnus-buffer-live-p gnus-summary-buffer)
8133                 (set-buffer gnus-summary-buffer))
8134               (symbol-value (car elem))))
8135       (when (and (or (consp val)
8136                      treated-type)
8137                  (gnus-treat-predicate val)
8138                  (or (not (get (car elem) 'highlight))
8139                      highlightp))
8140         (save-restriction
8141           (funcall (cadr elem)))))))
8142
8143 ;; Dynamic variables.
8144 (defvar part-number)
8145 (defvar total-parts)
8146 (defvar type)
8147 (defvar condition)
8148 (defvar length)
8149
8150 (defun gnus-treat-predicate (val)
8151   (cond
8152    ((null val)
8153     nil)
8154    (condition
8155     (eq condition val))
8156    ((and (listp val)
8157          (stringp (car val)))
8158     (apply 'gnus-or (mapcar `(lambda (s)
8159                                (string-match s ,(or gnus-newsgroup-name "")))
8160                             val)))
8161    ((listp val)
8162     (let ((pred (pop val)))
8163       (cond
8164        ((eq pred 'or)
8165         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8166        ((eq pred 'and)
8167         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8168        ((eq pred 'not)
8169         (not (gnus-treat-predicate (car val))))
8170        ((eq pred 'typep)
8171         (equal (car val) type))
8172        (t
8173         (error "%S is not a valid predicate" pred)))))
8174    ((eq val t)
8175     t)
8176    ((eq val 'head)
8177     nil)
8178    ((eq val 'first)
8179     (eq part-number 1))
8180    ((eq val 'last)
8181     (eq part-number total-parts))
8182    ((numberp val)
8183     (< length val))
8184    (t
8185     (error "%S is not a valid value" val))))
8186
8187 (defun gnus-article-encrypt-body (protocol &optional n)
8188   "Encrypt the article body."
8189   (interactive
8190    (list
8191     (or gnus-article-encrypt-protocol
8192         (completing-read "Encrypt protocol: "
8193                          gnus-article-encrypt-protocol-alist
8194                          nil t))
8195     current-prefix-arg))
8196   ;; User might hit `K E' instead of `K e', so prompt once.
8197   (when (and gnus-article-encrypt-protocol
8198              gnus-novice-user)
8199     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8200       (error "Encrypt aborted.")))
8201   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8202     (unless func
8203       (error "Can't find the encrypt protocol %s" protocol))
8204     (if (member gnus-newsgroup-name '("nndraft:delayed"
8205                                       "nndraft:drafts"
8206                                       "nndraft:queue"))
8207         (error "Can't encrypt the article in group %s"
8208                gnus-newsgroup-name))
8209     (gnus-summary-iterate n
8210       (with-current-buffer gnus-summary-buffer
8211         (let ((mail-parse-charset gnus-newsgroup-charset)
8212               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8213               (summary-buffer gnus-summary-buffer)
8214               references point)
8215           (gnus-set-global-variables)
8216           (when (gnus-group-read-only-p)
8217             (error "The current newsgroup does not support article encrypt"))
8218           (gnus-summary-show-article t)
8219           (setq references
8220               (or (mail-header-references gnus-current-headers) ""))
8221           (set-buffer gnus-article-buffer)
8222           (let* ((inhibit-read-only t)
8223                  (headers
8224                   (mapcar (lambda (field)
8225                             (and (save-restriction
8226                                    (message-narrow-to-head)
8227                                    (goto-char (point-min))
8228                                    (search-forward field nil t))
8229                                  (prog2
8230                                      (message-narrow-to-field)
8231                                      (buffer-string)
8232                                    (delete-region (point-min) (point-max))
8233                                    (widen))))
8234                           '("Content-Type:" "Content-Transfer-Encoding:"
8235                             "Content-Disposition:"))))
8236             (message-narrow-to-head)
8237             (message-remove-header "MIME-Version")
8238             (goto-char (point-max))
8239             (setq point (point))
8240             (insert (apply 'concat headers))
8241             (widen)
8242             (narrow-to-region point (point-max))
8243             (let ((message-options message-options))
8244               (message-options-set 'message-sender user-mail-address)
8245               (message-options-set 'message-recipients user-mail-address)
8246               (message-options-set 'message-sign-encrypt 'not)
8247               (funcall func))
8248             (goto-char (point-min))
8249             (insert "MIME-Version: 1.0\n")
8250             (widen)
8251             (gnus-summary-edit-article-done
8252              references nil summary-buffer t))
8253           (when gnus-keep-backlog
8254             (gnus-backlog-remove-article
8255              (car gnus-article-current) (cdr gnus-article-current)))
8256           (when (get-buffer gnus-original-article-buffer)
8257             (with-current-buffer gnus-original-article-buffer
8258               (setq gnus-original-article nil)))
8259           (when gnus-use-cache
8260             (gnus-cache-update-article
8261              (car gnus-article-current) (cdr gnus-article-current))))))))
8262
8263 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8264   "The following specs can be used:
8265 %t  The security MIME type
8266 %i  Additional info
8267 %d  Details
8268 %D  Details if button is pressed")
8269
8270 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8271   "The following specs can be used:
8272 %t  The security MIME type
8273 %i  Additional info
8274 %d  Details
8275 %D  Details if button is pressed")
8276
8277 (defvar gnus-mime-security-button-line-format-alist
8278   '((?t gnus-tmp-type ?s)
8279     (?i gnus-tmp-info ?s)
8280     (?d gnus-tmp-details ?s)
8281     (?D gnus-tmp-pressed-details ?s)))
8282
8283 (defvar gnus-mime-security-button-commands
8284   '((gnus-article-press-button "\r" "Show Detail")
8285     (undefined "v")
8286     (undefined "t")
8287     (undefined "C")
8288     (gnus-mime-security-save-part "o" "Save...")
8289     (undefined "\C-o")
8290     (undefined "r")
8291     (undefined "d")
8292     (undefined "c")
8293     (undefined "i")
8294     (undefined "E")
8295     (undefined "e")
8296     (undefined "p")
8297     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8298     (undefined ".")))
8299
8300 (defvar gnus-mime-security-button-map
8301   (let ((map (make-sparse-keymap)))
8302     (define-key map gnus-mouse-2 'gnus-article-push-button)
8303     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8304     (dolist (c gnus-mime-security-button-commands)
8305       (define-key map (cadr c) (car c)))
8306     map))
8307
8308 (easy-menu-define
8309   gnus-mime-security-button-menu gnus-mime-security-button-map
8310   "Security button menu."
8311   `("Security Part"
8312     ,@(delq nil
8313             (mapcar (lambda (c)
8314                       (unless (eq (car c) 'undefined)
8315                         (vector (caddr c) (car c) :active t)))
8316                     gnus-mime-security-button-commands))))
8317
8318 (defun gnus-mime-security-button-menu (event prefix)
8319   "Construct a context-sensitive menu of security commands."
8320   (interactive "e\nP")
8321   (save-window-excursion
8322     (let ((pos (event-start event)))
8323       (select-window (posn-window pos))
8324       (goto-char (posn-point pos))
8325       (gnus-article-check-buffer)
8326       (popup-menu gnus-mime-security-button-menu nil prefix))))
8327
8328 (defvar gnus-mime-security-details-buffer nil)
8329
8330 (defvar gnus-mime-security-button-pressed nil)
8331
8332 (defvar gnus-mime-security-show-details-inline t
8333   "If non-nil, show details in the article buffer.")
8334
8335 (defun gnus-mime-security-verify-or-decrypt (handle)
8336   (mm-remove-parts (cdr handle))
8337   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8338         point (inhibit-read-only t))
8339     (if region
8340         (goto-char (car region)))
8341     (setq point (point))
8342     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8343       (let* ((mm-verify-option 'known)
8344              (mm-decrypt-option 'known)
8345              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8346         (unless (eq nparts (cdr handle))
8347           (mm-destroy-parts (cdr handle))
8348           (setcdr handle nparts))))
8349     (gnus-mime-display-security handle)
8350     (when region
8351       (delete-region (point) (cdr region))
8352       (set-marker (car region) nil)
8353       (set-marker (cdr region) nil))
8354     (goto-char point)))
8355
8356 (defun gnus-mime-security-show-details (handle)
8357   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8358     (if (not details)
8359         (gnus-message 5 "No details.")
8360       (if gnus-mime-security-show-details-inline
8361           (let ((gnus-mime-security-button-pressed
8362                  (not (get-text-property (point) 'gnus-mime-details)))
8363                 (gnus-mime-security-button-line-format
8364                  (get-text-property (point) 'gnus-line-format))
8365                 (inhibit-read-only t))
8366             (forward-char -1)
8367             (while (eq (get-text-property (point) 'gnus-line-format)
8368                        gnus-mime-security-button-line-format)
8369               (forward-char -1))
8370             (forward-char)
8371             (save-restriction
8372               (narrow-to-region (point) (point))
8373               (gnus-insert-mime-security-button handle))
8374             (delete-region (point)
8375                            (or (text-property-not-all
8376                                 (point) (point-max)
8377                                 'gnus-line-format
8378                                 gnus-mime-security-button-line-format)
8379                                (point-max))))
8380         ;; Not inlined.
8381         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8382             (with-current-buffer gnus-mime-security-details-buffer
8383               (erase-buffer)
8384               t)
8385           (setq gnus-mime-security-details-buffer
8386                 (gnus-get-buffer-create "*MIME Security Details*")))
8387         (with-current-buffer gnus-mime-security-details-buffer
8388           (insert details)
8389           (goto-char (point-min)))
8390         (pop-to-buffer gnus-mime-security-details-buffer)))))
8391
8392 (defun gnus-mime-security-press-button (handle)
8393   (save-excursion
8394     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8395         (gnus-mime-security-show-details handle)
8396       (gnus-mime-security-verify-or-decrypt handle))))
8397
8398 (defun gnus-insert-mime-security-button (handle &optional displayed)
8399   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8400          (gnus-tmp-type
8401           (concat
8402            (or (nth 2 (assoc protocol mm-verify-function-alist))
8403                (nth 2 (assoc protocol mm-decrypt-function-alist))
8404                "Unknown")
8405            (if (equal (car handle) "multipart/signed")
8406                " Signed" " Encrypted")
8407            " Part"))
8408          (gnus-tmp-info
8409           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8410               "Undecided"))
8411          (gnus-tmp-details
8412           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8413          gnus-tmp-pressed-details
8414          b e)
8415     (setq gnus-tmp-details
8416           (if gnus-tmp-details
8417               (concat "\n" gnus-tmp-details)
8418             ""))
8419     (setq gnus-tmp-pressed-details
8420           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8421     (unless (bolp)
8422       (insert "\n"))
8423     (setq b (point))
8424     (gnus-eval-format
8425      gnus-mime-security-button-line-format
8426      gnus-mime-security-button-line-format-alist
8427      `(keymap ,gnus-mime-security-button-map
8428          gnus-callback gnus-mime-security-press-button
8429          gnus-line-format ,gnus-mime-security-button-line-format
8430          gnus-mime-details ,gnus-mime-security-button-pressed
8431          article-type annotation
8432          gnus-data ,handle))
8433     (setq e (if (bolp)
8434                 ;; Exclude a newline.
8435                 (1- (point))
8436               (point)))
8437     (when gnus-article-button-face
8438       (gnus-overlay-put (gnus-make-overlay b e nil t)
8439                         'face gnus-article-button-face))
8440     (widget-convert-button
8441      'link b e
8442      :mime-handle handle
8443      :action 'gnus-widget-press-button
8444      :button-keymap gnus-mime-security-button-map
8445      :help-echo
8446      (lambda (widget/window &optional overlay pos)
8447        ;; Needed to properly clear the message due to a bug in
8448        ;; wid-edit (XEmacs only).
8449        (when (boundp 'help-echo-owns-message)
8450          (setq help-echo-owns-message t))
8451        (format
8452         "%S: show detail; %S: more options"
8453         (aref gnus-mouse-2 0)
8454         (aref gnus-down-mouse-3 0))))))
8455
8456 (defun gnus-mime-display-security (handle)
8457   (save-restriction
8458     (narrow-to-region (point) (point))
8459     (unless (gnus-unbuttonized-mime-type-p (car handle))
8460       (gnus-insert-mime-security-button handle))
8461     (gnus-mime-display-part (cadr handle))
8462     (unless (bolp)
8463       (insert "\n"))
8464     (unless (gnus-unbuttonized-mime-type-p (car handle))
8465       (let ((gnus-mime-security-button-line-format
8466              gnus-mime-security-button-end-line-format))
8467         (gnus-insert-mime-security-button handle)))
8468     (mm-set-handle-multipart-parameter
8469      handle 'gnus-region
8470      (cons (set-marker (make-marker) (point-min))
8471            (set-marker (make-marker) (point-max))))
8472     (goto-char (point-max))))
8473
8474 (defun gnus-mime-security-run-function (function)
8475   "Run FUNCTION with the security part under point."
8476   (gnus-article-check-buffer)
8477   (let ((data (get-text-property (point) 'gnus-data))
8478         buffer handle)
8479     (when (and (stringp (car-safe data))
8480                (setq buffer (mm-handle-multipart-original-buffer data))
8481                (setq handle (cadr data)))
8482       (if (bufferp (mm-handle-buffer handle))
8483           (progn
8484             (setq handle (cons buffer (copy-sequence (cdr handle))))
8485             (mm-handle-set-undisplayer handle nil))
8486         (setq handle (mm-make-handle
8487                       buffer
8488                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8489                       nil nil nil nil nil nil)))
8490       (funcall function handle))))
8491
8492 (defun gnus-mime-security-save-part ()
8493   "Save the security part under point."
8494   (interactive)
8495   (gnus-mime-security-run-function 'mm-save-part))
8496
8497 (defun gnus-mime-security-pipe-part ()
8498   "Pipe the security part under point to a process."
8499   (interactive)
8500   (gnus-mime-security-run-function 'mm-pipe-part))
8501
8502 (gnus-ems-redefine)
8503
8504 (provide 'gnus-art)
8505
8506 (run-hooks 'gnus-art-load-hook)
8507
8508 ;; arch-tag: 2654516f-6279-48f9-a83b-05c1fa450c33
8509 ;;; gnus-art.el ends here