(gnus-button-alist): Extend gnus-button-handle-info-keystrokes entry.
[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 always 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     (widen)
5997     ;; Remove any old next/prev buttons.
5998     (when (gnus-visual-p 'page-marker)
5999       (let ((inhibit-read-only t))
6000         (gnus-remove-text-with-property 'gnus-prev)
6001         (gnus-remove-text-with-property 'gnus-next)))
6002     (let (st nd pt)
6003       (when (save-excursion
6004               (cond ((< arg 0)
6005                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6006                          (prog1
6007                              (setq nd (match-beginning 0)
6008                                    pt nd)
6009                            (when (re-search-backward page-delimiter nil t)
6010                              (setq st (match-end 0))))
6011                        (when (re-search-forward page-delimiter nil t)
6012                          (setq nd (match-beginning 0)
6013                                pt (point-min)))))
6014                     ((> arg 0)
6015                      (if (re-search-forward page-delimiter nil 'move arg)
6016                          (prog1
6017                              (setq st (match-end 0)
6018                                    pt st)
6019                            (when (re-search-forward page-delimiter nil t)
6020                              (setq nd (match-beginning 0))))
6021                        (when (re-search-backward page-delimiter nil t)
6022                          (setq st (match-end 0)
6023                                pt (point-max)))))
6024                     (t
6025                      (when (re-search-backward page-delimiter nil t)
6026                        (goto-char (setq st (match-end 0))))
6027                      (when (re-search-forward page-delimiter nil t)
6028                        (setq nd (match-beginning 0)))
6029                      (or st nd))))
6030         (setq gnus-page-broken t)
6031         (when pt (goto-char pt))
6032         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6033         (when (gnus-visual-p 'page-marker)
6034           (save-excursion
6035             (when nd
6036               (goto-char nd)
6037               (gnus-insert-next-page-button))
6038             (when st
6039               (goto-char st)
6040               (gnus-insert-prev-page-button))))))))
6041
6042 ;; Article mode commands
6043
6044 (defun gnus-article-goto-next-page ()
6045   "Show the next page of the article."
6046   (interactive)
6047   (when (gnus-article-next-page)
6048     (goto-char (point-min))
6049     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6050
6051
6052 (defun gnus-article-goto-prev-page ()
6053   "Show the previous page of the article."
6054   (interactive)
6055   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6056       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6057     (gnus-article-prev-page nil)))
6058
6059 ;; This is cleaner but currently breaks `gnus-pick-mode':
6060 ;;
6061 ;; (defun gnus-article-goto-next-page ()
6062 ;;   "Show the next page of the article."
6063 ;;   (interactive)
6064 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6065 ;;     (gnus-summary-next-page)))
6066 ;;
6067 ;; (defun gnus-article-goto-prev-page ()
6068 ;;   "Show the next page of the article."
6069 ;;   (interactive)
6070 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6071 ;;     (gnus-summary-prev-page)))
6072
6073 (defun gnus-article-next-page (&optional lines)
6074   "Show the next page of the current article.
6075 If end of article, return non-nil.  Otherwise return nil.
6076 Argument LINES specifies lines to be scrolled up."
6077   (interactive "p")
6078   (move-to-window-line -1)
6079   (if (and (not (and gnus-article-over-scroll
6080                      (> (count-lines (window-start) (point-max))
6081                         (+ (or lines (1- (window-height)))
6082                            (or (and (boundp 'scroll-margin)
6083                                     (symbol-value 'scroll-margin))
6084                                0)))))
6085            (save-excursion
6086              (end-of-line)
6087              (and (pos-visible-in-window-p)     ;Not continuation line.
6088                   (>= (1+ (point)) (point-max))))) ;Allow for trailing newline.
6089       ;; Nothing in this page.
6090       (if (or (not gnus-page-broken)
6091               (save-excursion
6092                 (save-restriction
6093                   (widen)
6094                   (forward-line)
6095                   (eobp)))) ;Real end-of-buffer?
6096           (progn
6097             (when gnus-article-over-scroll
6098               (gnus-article-next-page-1 lines))
6099             t)                  ;Nothing more.
6100         (gnus-narrow-to-page 1)         ;Go to next page.
6101         nil)
6102     ;; More in this page.
6103     (gnus-article-next-page-1 lines)
6104     nil))
6105
6106 (defmacro gnus-article-beginning-of-window ()
6107   "Move point to the beginning of the window.
6108 In Emacs, the point is placed at the line number which `scroll-margin'
6109 specifies."
6110   (if (featurep 'xemacs)
6111       '(move-to-window-line 0)
6112     '(move-to-window-line
6113       (min (max 0 scroll-margin)
6114            (max 1 (- (window-height)
6115                      (if mode-line-format 1 0)
6116                      (if header-line-format 1 0)))))))
6117
6118 (defun gnus-article-next-page-1 (lines)
6119   (when (and (not (featurep 'xemacs))
6120              (numberp lines)
6121              (> lines 0)
6122              (numberp (symbol-value 'scroll-margin))
6123              (> (symbol-value 'scroll-margin) 0))
6124     ;; Protect against the bug that Emacs 21.x hangs up when scrolling up for
6125     ;; too many number of lines if `scroll-margin' is set as two or greater.
6126     (setq lines (min lines
6127                      (max 0 (- (count-lines (window-start) (point-max))
6128                                (symbol-value 'scroll-margin))))))
6129   (condition-case ()
6130       (let ((scroll-in-place nil))
6131         (scroll-up lines))
6132     (end-of-buffer
6133      ;; Long lines may cause an end-of-buffer error.
6134      (goto-char (point-max))))
6135   (gnus-article-beginning-of-window))
6136
6137 (defun gnus-article-prev-page (&optional lines)
6138   "Show previous page of current article.
6139 Argument LINES specifies lines to be scrolled down."
6140   (interactive "p")
6141   (move-to-window-line 0)
6142   (if (and gnus-page-broken
6143            (bobp)
6144            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6145       (progn
6146         (gnus-narrow-to-page -1)        ;Go to previous page.
6147         (goto-char (point-max))
6148         (recenter (if gnus-article-over-scroll
6149                       (if lines
6150                           (max (+ lines (or (and (boundp 'scroll-margin)
6151                                                  (symbol-value 'scroll-margin))
6152                                             0))
6153                                3)
6154                         (- (window-height) 2))
6155                     -1)))
6156     (prog1
6157         (condition-case ()
6158             (let ((scroll-in-place nil))
6159               (scroll-down lines))
6160           (beginning-of-buffer
6161            (goto-char (point-min))))
6162       (gnus-article-beginning-of-window))))
6163
6164 (defun gnus-article-only-boring-p ()
6165   "Decide whether there is only boring text remaining in the article.
6166 Something \"interesting\" is a word of at least two letters that does
6167 not have a face in `gnus-article-boring-faces'."
6168   (when (and gnus-article-skip-boring
6169              (boundp 'gnus-article-boring-faces)
6170              (symbol-value 'gnus-article-boring-faces))
6171     (save-excursion
6172       (let ((inhibit-point-motion-hooks t))
6173         (catch 'only-boring
6174           (while (re-search-forward "\\b\\w\\w" nil t)
6175             (forward-char -1)
6176             (when (not (gnus-intersection
6177                         (gnus-faces-at (point))
6178                         (symbol-value 'gnus-article-boring-faces)))
6179               (throw 'only-boring nil)))
6180           (throw 'only-boring t))))))
6181
6182 (defun gnus-article-refer-article ()
6183   "Read article specified by message-id around point."
6184   (interactive)
6185   (save-excursion
6186     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6187     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6188     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6189         (let ((msg-id (concat "<" (match-string 0) ">")))
6190           (set-buffer gnus-summary-buffer)
6191           (gnus-summary-refer-article msg-id))
6192       (error "No references around point"))))
6193
6194 (defun gnus-article-show-summary ()
6195   "Reconfigure windows to show summary buffer."
6196   (interactive)
6197   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6198       (error "There is no summary buffer for this article buffer")
6199     (gnus-article-set-globals)
6200     (gnus-configure-windows 'article)
6201     (gnus-summary-goto-subject gnus-current-article)
6202     (gnus-summary-position-point)))
6203
6204 (defun gnus-article-describe-briefly ()
6205   "Describe article mode commands briefly."
6206   (interactive)
6207   (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")))
6208
6209 (defun gnus-article-summary-command ()
6210   "Execute the last keystroke in the summary buffer."
6211   (interactive)
6212   (let ((obuf (current-buffer))
6213         (owin (current-window-configuration))
6214         func)
6215     (switch-to-buffer gnus-article-current-summary 'norecord)
6216     (setq func (lookup-key (current-local-map) (this-command-keys)))
6217     (call-interactively func)
6218     (set-buffer obuf)
6219     (set-window-configuration owin)
6220     (set-window-point (get-buffer-window (current-buffer)) (point))))
6221
6222 (defun gnus-article-summary-command-nosave ()
6223   "Execute the last keystroke in the summary buffer."
6224   (interactive)
6225   (let (func)
6226     (pop-to-buffer gnus-article-current-summary)
6227     (setq func (lookup-key (current-local-map) (this-command-keys)))
6228     (call-interactively func)))
6229
6230 (defun gnus-article-check-buffer ()
6231   "Beep if not in an article buffer."
6232   (unless (equal major-mode 'gnus-article-mode)
6233     (error "Command invoked outside of a Gnus article buffer")))
6234
6235 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6236   "Read a summary buffer key sequence and execute it from the article buffer."
6237   (interactive "P")
6238   (gnus-article-check-buffer)
6239   (let ((nosaves
6240          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6241            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6242            "=" "^" "\M-^" "|"))
6243         (nosave-but-article
6244          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6245            "An" "Ap" [?A (meta return)] [?A delete]))
6246         (nosave-in-article
6247          '("AS" "\C-d"))
6248         (up-to-top
6249          '("n" "Gn" "p" "Gp"))
6250         keys new-sum-point)
6251     (with-current-buffer gnus-article-current-summary
6252       (let (gnus-pick-mode)
6253         (setq unread-command-events (nconc unread-command-events
6254                                            (list (or key last-command-event)))
6255               keys (if (featurep 'xemacs)
6256                        (events-to-keys (read-key-sequence nil t))
6257                      (read-key-sequence nil t)))))
6258
6259     (message "")
6260
6261     (cond
6262      ((eq (aref keys (1- (length keys))) ?\C-h)
6263       (gnus-article-describe-bindings (substring keys 0 -1)))
6264      ((or (member keys nosaves)
6265           (member keys nosave-but-article)
6266           (member keys nosave-in-article))
6267       (let (func)
6268         (save-window-excursion
6269           (pop-to-buffer gnus-article-current-summary)
6270           ;; We disable the pick minor mode commands.
6271           (let (gnus-pick-mode)
6272             (setq func (lookup-key (current-local-map) keys))))
6273         (if (or (not func)
6274                 (numberp func))
6275             (ding)
6276           (unless (member keys nosave-in-article)
6277             (set-buffer gnus-article-current-summary))
6278           (call-interactively func)
6279           (setq new-sum-point (point)))
6280         (when (member keys nosave-but-article)
6281           (pop-to-buffer gnus-article-buffer))))
6282      (t
6283       ;; These commands should restore window configuration.
6284       (let ((obuf (current-buffer))
6285             (owin (current-window-configuration))
6286             win func in-buffer selected new-sum-start new-sum-hscroll err)
6287         (cond (not-restore-window
6288                (pop-to-buffer gnus-article-current-summary)
6289                (setq win (selected-window)))
6290               ((setq win (get-buffer-window gnus-article-current-summary))
6291                (select-window win))
6292               (t
6293                (let ((summary-buffer gnus-article-current-summary))
6294                  (gnus-configure-windows 'article)
6295                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6296                    (let ((gnus-buffer-configuration
6297                           '(article ((vertical 1.0
6298                                                (summary 0.25 point)
6299                                                (article 1.0))))))
6300                      (gnus-configure-windows 'article))
6301                    (setq win (get-buffer-window summary-buffer 'visible)))
6302                  (gnus-select-frame-set-input-focus (window-frame win))
6303                  (select-window win))))
6304         (setq in-buffer (current-buffer))
6305         ;; We disable the pick minor mode commands.
6306         (if (and (setq func (let (gnus-pick-mode)
6307                               (lookup-key (current-local-map) keys)))
6308                  (functionp func)
6309                  (condition-case code
6310                      (progn
6311                        (call-interactively func)
6312                        t)
6313                    (error
6314                     (setq err code)
6315                     nil)))
6316             (progn
6317               (when (eq win (selected-window))
6318                 (setq new-sum-point (point)
6319                       new-sum-start (window-start win)
6320                       new-sum-hscroll (window-hscroll win)))
6321               (when (or (eq in-buffer (current-buffer))
6322                         (when (eq obuf (current-buffer))
6323                           (set-buffer in-buffer)
6324                           t))
6325                 (setq selected (gnus-summary-select-article))
6326                 (set-buffer obuf)
6327                 (unless not-restore-window
6328                   (set-window-configuration owin))
6329                 (when (and (eq selected 'old)
6330                            new-sum-point)
6331                   (set-window-start (get-buffer-window (current-buffer))
6332                                     1)
6333                   (set-window-point (get-buffer-window (current-buffer))
6334                                     (if (article-goto-body)
6335                                         (1- (point))
6336                                       (point))))
6337                 (when (and (not not-restore-window)
6338                            new-sum-point
6339                            (with-current-buffer (window-buffer win)
6340                              (eq major-mode 'gnus-summary-mode)))
6341                   (set-window-point win new-sum-point)
6342                   (set-window-start win new-sum-start)
6343                   (set-window-hscroll win new-sum-hscroll))))
6344           (set-window-configuration owin)
6345           (if err
6346               (signal (car err) (cdr err))
6347             (ding))))))))
6348
6349 (defun gnus-article-read-summary-send-keys ()
6350   (interactive)
6351   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6352     (gnus-article-read-summary-keys)))
6353
6354 (defun gnus-article-describe-key (key)
6355   "Display documentation of the function invoked by KEY.
6356 KEY is a string or a vector."
6357   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6358                        (read-key-sequence "Describe key: "))))
6359   (gnus-article-check-buffer)
6360   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6361                                   gnus-article-read-summary-send-keys))
6362       (with-current-buffer gnus-article-current-summary
6363         (setq unread-command-events
6364               (if (featurep 'xemacs)
6365                   (append key nil)
6366                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6367                                         (list 'meta (- x 128))
6368                                       x))
6369                         key)))
6370         (let ((cursor-in-echo-area t)
6371               gnus-pick-mode)
6372           (describe-key (read-key-sequence nil t))))
6373     (describe-key key)))
6374
6375 (defun gnus-article-describe-key-briefly (key &optional insert)
6376   "Display documentation of the function invoked by KEY.
6377 KEY is a string or a vector."
6378   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6379                        (read-key-sequence "Describe key: "))
6380                      current-prefix-arg))
6381   (gnus-article-check-buffer)
6382   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6383                                   gnus-article-read-summary-send-keys))
6384       (with-current-buffer gnus-article-current-summary
6385         (setq unread-command-events
6386               (if (featurep 'xemacs)
6387                   (append key nil)
6388                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6389                                         (list 'meta (- x 128))
6390                                       x))
6391                         key)))
6392         (let ((cursor-in-echo-area t)
6393               gnus-pick-mode)
6394           (describe-key-briefly (read-key-sequence nil t) insert)))
6395     (describe-key-briefly key insert)))
6396
6397 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6398 (defvar gnus-agent-summary-mode)
6399 (defvar gnus-draft-mode)
6400
6401 (defun gnus-article-describe-bindings (&optional prefix)
6402   "Show a list of all defined keys, and their definitions.
6403 The optional argument PREFIX, if non-nil, should be a key sequence;
6404 then we display only bindings that start with that prefix."
6405   (interactive)
6406   (gnus-article-check-buffer)
6407   (let ((keymap (copy-keymap gnus-article-mode-map))
6408         (map (copy-keymap gnus-article-send-map))
6409         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6410         agent draft)
6411     (define-key keymap "S" map)
6412     (define-key map [t] nil)
6413     (with-current-buffer gnus-article-current-summary
6414       (set-keymap-parent map (key-binding "S"))
6415       (let (key def gnus-pick-mode)
6416         (while sumkeys
6417           (setq key (pop sumkeys))
6418           (cond ((and (vectorp key) (= (length key) 1)
6419                       (consp (setq def (aref key 0)))
6420                       (numberp (car def)) (numberp (cdr def)))
6421                  (when (< (max (car def) (cdr def)) 128)
6422                    (setq sumkeys
6423                          (append (mapcar
6424                                   #'vector
6425                                   (nreverse (gnus-uncompress-range def)))
6426                                  sumkeys))))
6427                 ((setq def (key-binding key))
6428                  (unless (eq def 'undefined)
6429                    (define-key keymap key def))))))
6430       (when (boundp 'gnus-agent-summary-mode)
6431         (setq agent gnus-agent-summary-mode))
6432       (when (boundp 'gnus-draft-mode)
6433         (setq draft gnus-draft-mode)))
6434     (with-temp-buffer
6435       (use-local-map keymap)
6436       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6437       (set (make-local-variable 'gnus-draft-mode) draft)
6438       (describe-bindings prefix))
6439     (let ((item `((lambda (prefix)
6440                     (with-current-buffer ,(current-buffer)
6441                       (gnus-article-describe-bindings prefix)))
6442                   ,prefix)))
6443       (with-current-buffer (if (fboundp 'help-buffer)
6444                                (let (help-xref-following) (help-buffer))
6445                              "*Help*") ;; Emacs 21
6446         (setq help-xref-stack-item item)))))
6447
6448 (defun gnus-article-reply-with-original (&optional wide)
6449   "Start composing a reply mail to the current message.
6450 The text in the region will be yanked.  If the region isn't active,
6451 the entire article will be yanked."
6452   (interactive)
6453   (let ((article (cdr gnus-article-current))
6454         contents)
6455     (if (not (gnus-region-active-p))
6456         (with-current-buffer gnus-summary-buffer
6457           (gnus-summary-reply (list (list article)) wide))
6458       (setq contents (buffer-substring (point) (mark t)))
6459       ;; Deactivate active regions.
6460       (when (and (boundp 'transient-mark-mode)
6461                  transient-mark-mode)
6462         (setq mark-active nil))
6463       (with-current-buffer gnus-summary-buffer
6464         (gnus-summary-reply
6465          (list (list article contents)) wide)))))
6466
6467 (defun gnus-article-wide-reply-with-original ()
6468   "Start composing a wide reply mail to the current message.
6469 The text in the region will be yanked.  If the region isn't active,
6470 the entire article will be yanked."
6471   (interactive)
6472   (gnus-article-reply-with-original t))
6473
6474 (defun gnus-article-followup-with-original ()
6475   "Compose a followup to the current article.
6476 The text in the region will be yanked.  If the region isn't active,
6477 the entire article will be yanked."
6478   (interactive)
6479   (let ((article (cdr gnus-article-current))
6480         contents)
6481       (if (not (gnus-region-active-p))
6482           (with-current-buffer gnus-summary-buffer
6483             (gnus-summary-followup (list (list article))))
6484         (setq contents (buffer-substring (point) (mark t)))
6485         ;; Deactivate active regions.
6486         (when (and (boundp 'transient-mark-mode)
6487                    transient-mark-mode)
6488           (setq mark-active nil))
6489         (with-current-buffer gnus-summary-buffer
6490           (gnus-summary-followup
6491            (list (list article contents)))))))
6492
6493 (defun gnus-article-hide (&optional arg force)
6494   "Hide all the gruft in the current article.
6495 This means that signatures, cited text and (some) headers will be
6496 hidden.
6497 If given a prefix, show the hidden text instead."
6498   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6499   (gnus-article-hide-headers arg)
6500   (gnus-article-hide-list-identifiers arg)
6501   (gnus-article-hide-citation-maybe arg force)
6502   (gnus-article-hide-signature arg))
6503
6504 (defun gnus-article-maybe-highlight ()
6505   "Do some article highlighting if article highlighting is requested."
6506   (when (gnus-visual-p 'article-highlight 'highlight)
6507     (gnus-article-highlight-some)))
6508
6509 (defun gnus-check-group-server ()
6510   ;; Make sure the connection to the server is alive.
6511   (unless (gnus-server-opened
6512            (gnus-find-method-for-group gnus-newsgroup-name))
6513     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6514     (gnus-request-group gnus-newsgroup-name t)))
6515
6516 (eval-when-compile
6517   (autoload 'nneething-get-file-name "nneething"))
6518
6519 (defun gnus-request-article-this-buffer (article group)
6520   "Get an article and insert it into this buffer."
6521   (let (do-update-line sparse-header)
6522     (prog1
6523         (save-excursion
6524           (erase-buffer)
6525           (gnus-kill-all-overlays)
6526           (setq group (or group gnus-newsgroup-name))
6527
6528           ;; Using `gnus-request-article' directly will insert the article into
6529           ;; `nntp-server-buffer' - so we'll save some time by not having to
6530           ;; copy it from the server buffer into the article buffer.
6531
6532           ;; We only request an article by message-id when we do not have the
6533           ;; headers for it, so we'll have to get those.
6534           (when (stringp article)
6535             (gnus-read-header article))
6536
6537           ;; If the article number is negative, that means that this article
6538           ;; doesn't belong in this newsgroup (possibly), so we find its
6539           ;; message-id and request it by id instead of number.
6540           (when (and (numberp article)
6541                      gnus-summary-buffer
6542                      (get-buffer gnus-summary-buffer)
6543                      (gnus-buffer-exists-p gnus-summary-buffer))
6544             (with-current-buffer gnus-summary-buffer
6545               (let ((header (gnus-summary-article-header article)))
6546                 (when (< article 0)
6547                   (cond
6548                    ((memq article gnus-newsgroup-sparse)
6549                     ;; This is a sparse gap article.
6550                     (setq do-update-line article)
6551                     (setq article (mail-header-id header))
6552                     (setq sparse-header (gnus-read-header article))
6553                     (setq gnus-newsgroup-sparse
6554                           (delq article gnus-newsgroup-sparse)))
6555                    ((vectorp header)
6556                     ;; It's a real article.
6557                     (setq article (mail-header-id header)))
6558                    (t
6559                     ;; It is an extracted pseudo-article.
6560                     (setq article 'pseudo)
6561                     (gnus-request-pseudo-article header))))
6562
6563                 (let ((method (gnus-find-method-for-group
6564                                gnus-newsgroup-name)))
6565                   (when (and (eq (car method) 'nneething)
6566                              (vectorp header))
6567                     (let ((dir (nneething-get-file-name
6568                                 (mail-header-id header))))
6569                       (when (and (stringp dir)
6570                                  (file-directory-p dir))
6571                         (setq article 'nneething)
6572                         (gnus-group-enter-directory dir))))))))
6573
6574           (cond
6575            ;; Refuse to select canceled articles.
6576            ((and (numberp article)
6577                  gnus-summary-buffer
6578                  (get-buffer gnus-summary-buffer)
6579                  (gnus-buffer-exists-p gnus-summary-buffer)
6580                  (eq (cdr (with-current-buffer gnus-summary-buffer
6581                             (assq article gnus-newsgroup-reads)))
6582                      gnus-canceled-mark))
6583             nil)
6584            ;; We first check `gnus-original-article-buffer'.
6585            ((and (get-buffer gnus-original-article-buffer)
6586                  (numberp article)
6587                  (with-current-buffer gnus-original-article-buffer
6588                    (and (equal (car gnus-original-article) group)
6589                         (eq (cdr gnus-original-article) article))))
6590             (insert-buffer-substring gnus-original-article-buffer)
6591             'article)
6592            ;; Check the backlog.
6593            ((and gnus-keep-backlog
6594                  (gnus-backlog-request-article group article (current-buffer)))
6595             'article)
6596            ;; Check asynchronous pre-fetch.
6597            ((gnus-async-request-fetched-article group article (current-buffer))
6598             (gnus-async-prefetch-next group article gnus-summary-buffer)
6599             (when (and (numberp article) gnus-keep-backlog)
6600               (gnus-backlog-enter-article group article (current-buffer)))
6601             'article)
6602            ;; Check the cache.
6603            ((and gnus-use-cache
6604                  (numberp article)
6605                  (gnus-cache-request-article article group))
6606             'article)
6607            ;; Check the agent cache.
6608            ((gnus-agent-request-article article group)
6609             'article)
6610            ;; Get the article and put into the article buffer.
6611            ((or (stringp article)
6612                 (numberp article))
6613             (let ((gnus-override-method gnus-override-method)
6614                   (methods (and (stringp article)
6615                                 gnus-refer-article-method))
6616                   (backend (car (gnus-find-method-for-group
6617                                  gnus-newsgroup-name)))
6618                   result
6619                   (inhibit-read-only t))
6620               (if (or (not (listp methods))
6621                       (and (symbolp (car methods))
6622                            (assq (car methods) nnoo-definition-alist)))
6623                   (setq methods (list methods)))
6624               (when (and (null gnus-override-method)
6625                          methods)
6626                 (setq gnus-override-method (pop methods)))
6627               (while (not result)
6628                 (when (eq gnus-override-method 'current)
6629                   (setq gnus-override-method
6630                         (with-current-buffer gnus-summary-buffer
6631                           gnus-current-select-method)))
6632                 (erase-buffer)
6633                 (gnus-kill-all-overlays)
6634                 (let ((gnus-newsgroup-name group))
6635                   (gnus-check-group-server))
6636                 (cond
6637                  ((gnus-request-article article group (current-buffer))
6638                   (when (numberp article)
6639                     (gnus-async-prefetch-next group article
6640                                               gnus-summary-buffer)
6641                     (when gnus-keep-backlog
6642                       (gnus-backlog-enter-article
6643                        group article (current-buffer))))
6644                   (setq result 'article))
6645                  (methods
6646                   (setq gnus-override-method (pop methods)))
6647                  ((not (string-match "^400 "
6648                                      (nnheader-get-report backend)))
6649                   ;; If we get 400 server disconnect, reconnect and
6650                   ;; retry; otherwise, assume the article has expired.
6651                   (setq result 'done))))
6652               (and (eq result 'article) 'article)))
6653            ;; It was a pseudo.
6654            (t article)))
6655
6656       ;; Associate this article with the current summary buffer.
6657       (setq gnus-article-current-summary gnus-summary-buffer)
6658
6659       ;; Take the article from the original article buffer
6660       ;; and place it in the buffer it's supposed to be in.
6661       (when (and (get-buffer gnus-article-buffer)
6662                  (equal (buffer-name (current-buffer))
6663                         (buffer-name (get-buffer gnus-article-buffer))))
6664         (save-excursion
6665           (if (get-buffer gnus-original-article-buffer)
6666               (set-buffer gnus-original-article-buffer)
6667             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6668             (buffer-disable-undo)
6669             (setq major-mode 'gnus-original-article-mode)
6670             (setq buffer-read-only t))
6671           (let ((inhibit-read-only t))
6672             (erase-buffer)
6673             (insert-buffer-substring gnus-article-buffer))
6674           (setq gnus-original-article (cons group article)))
6675
6676         ;; Decode charsets.
6677         (run-hooks 'gnus-article-decode-hook)
6678         ;; Mark article as decoded or not.
6679         (setq gnus-article-decoded-p gnus-article-decode-hook))
6680
6681       ;; Update sparse articles.
6682       (when (and do-update-line
6683                  (or (numberp article)
6684                      (stringp article)))
6685         (let ((buf (current-buffer)))
6686           (set-buffer gnus-summary-buffer)
6687           (gnus-summary-update-article do-update-line sparse-header)
6688           (gnus-summary-goto-subject do-update-line nil t)
6689           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6690                             (point))
6691           (set-buffer buf))))))
6692
6693 ;;;
6694 ;;; Article editing
6695 ;;;
6696
6697 (defcustom gnus-article-edit-mode-hook nil
6698   "Hook run in article edit mode buffers."
6699   :group 'gnus-article-various
6700   :type 'hook)
6701
6702 (defvar gnus-article-edit-done-function nil)
6703
6704 (defvar gnus-article-edit-mode-map nil)
6705
6706 ;; Should we be using derived.el for this?
6707 (unless gnus-article-edit-mode-map
6708   (setq gnus-article-edit-mode-map (make-keymap))
6709   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6710
6711   (gnus-define-keys gnus-article-edit-mode-map
6712     "\C-c?"    describe-mode
6713     "\C-c\C-c" gnus-article-edit-done
6714     "\C-c\C-k" gnus-article-edit-exit
6715     "\C-c\C-f\C-t" message-goto-to
6716     "\C-c\C-f\C-o" message-goto-from
6717     "\C-c\C-f\C-b" message-goto-bcc
6718     ;;"\C-c\C-f\C-w" message-goto-fcc
6719     "\C-c\C-f\C-c" message-goto-cc
6720     "\C-c\C-f\C-s" message-goto-subject
6721     "\C-c\C-f\C-r" message-goto-reply-to
6722     "\C-c\C-f\C-n" message-goto-newsgroups
6723     "\C-c\C-f\C-d" message-goto-distribution
6724     "\C-c\C-f\C-f" message-goto-followup-to
6725     "\C-c\C-f\C-m" message-goto-mail-followup-to
6726     "\C-c\C-f\C-k" message-goto-keywords
6727     "\C-c\C-f\C-u" message-goto-summary
6728     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6729     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6730     "\C-c\C-b" message-goto-body
6731     "\C-c\C-i" message-goto-signature
6732
6733     "\C-c\C-t" message-insert-to
6734     "\C-c\C-n" message-insert-newsgroups
6735     "\C-c\C-o" message-sort-headers
6736     "\C-c\C-e" message-elide-region
6737     "\C-c\C-v" message-delete-not-region
6738     "\C-c\C-z" message-kill-to-signature
6739     "\M-\r" message-newline-and-reformat
6740     "\C-c\C-a" mml-attach-file
6741     "\C-a" message-beginning-of-line
6742     "\t" message-tab
6743     "\M-;" comment-region)
6744
6745   (gnus-define-keys (gnus-article-edit-wash-map
6746                      "\C-c\C-w" gnus-article-edit-mode-map)
6747     "f" gnus-article-edit-full-stops))
6748
6749 (easy-menu-define
6750   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6751   '("Field"
6752     ["Fetch To" message-insert-to t]
6753     ["Fetch Newsgroups" message-insert-newsgroups t]
6754     "----"
6755     ["To" message-goto-to t]
6756     ["From" message-goto-from t]
6757     ["Subject" message-goto-subject t]
6758     ["Cc" message-goto-cc t]
6759     ["Reply-To" message-goto-reply-to t]
6760     ["Summary" message-goto-summary t]
6761     ["Keywords" message-goto-keywords t]
6762     ["Newsgroups" message-goto-newsgroups t]
6763     ["Followup-To" message-goto-followup-to t]
6764     ["Mail-Followup-To" message-goto-mail-followup-to t]
6765     ["Distribution" message-goto-distribution t]
6766     ["Body" message-goto-body t]
6767     ["Signature" message-goto-signature t]))
6768
6769 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6770   "Major mode for editing articles.
6771 This is an extended text-mode.
6772
6773 \\{gnus-article-edit-mode-map}"
6774   (make-local-variable 'gnus-article-edit-done-function)
6775   (make-local-variable 'gnus-prev-winconf)
6776   (set (make-local-variable 'font-lock-defaults)
6777        '(message-font-lock-keywords t))
6778   (set (make-local-variable 'mail-header-separator) "")
6779   (set (make-local-variable 'gnus-article-edit-mode) t)
6780   (easy-menu-add message-mode-field-menu message-mode-map)
6781   (mml-mode)
6782   (setq buffer-read-only nil)
6783   (buffer-enable-undo)
6784   (widen))
6785
6786 (defun gnus-article-edit (&optional force)
6787   "Edit the current article.
6788 This will have permanent effect only in mail groups.
6789 If FORCE is non-nil, allow editing of articles even in read-only
6790 groups."
6791   (interactive "P")
6792   (when (and (not force)
6793              (gnus-group-read-only-p))
6794     (error "The current newsgroup does not support article editing"))
6795   (gnus-article-date-original)
6796   (gnus-article-edit-article
6797    'ignore
6798    `(lambda (no-highlight)
6799       'ignore
6800       (gnus-summary-edit-article-done
6801        ,(or (mail-header-references gnus-current-headers) "")
6802        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
6803
6804 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
6805   "Start editing the contents of the current article buffer."
6806   (let ((winconf (current-window-configuration)))
6807     (set-buffer gnus-article-buffer)
6808     (let ((message-auto-save-directory
6809            ;; Don't associate the article buffer with a draft file.
6810            nil))
6811       (gnus-article-edit-mode))
6812     (funcall start-func)
6813     (set-buffer-modified-p nil)
6814     (gnus-configure-windows 'edit-article)
6815     (setq gnus-article-edit-done-function exit-func)
6816     (setq gnus-prev-winconf winconf)
6817     (unless quiet
6818       (gnus-message 6 "C-c C-c to end edits"))))
6819
6820 (defun gnus-article-edit-done (&optional arg)
6821   "Update the article edits and exit."
6822   (interactive "P")
6823   (let ((func gnus-article-edit-done-function)
6824         (buf (current-buffer))
6825         (start (window-start))
6826         (p (point))
6827         (winconf gnus-prev-winconf))
6828     (widen) ;; Widen it in case that users narrowed the buffer.
6829     (funcall func arg)
6830     (set-buffer buf)
6831     ;; The cache and backlog have to be flushed somewhat.
6832     (when gnus-keep-backlog
6833       (gnus-backlog-remove-article
6834        (car gnus-article-current) (cdr gnus-article-current)))
6835     ;; Flush original article as well.
6836     (when (get-buffer gnus-original-article-buffer)
6837       (with-current-buffer gnus-original-article-buffer
6838         (setq gnus-original-article nil)))
6839     (when gnus-use-cache
6840       (gnus-cache-update-article
6841        (car gnus-article-current) (cdr gnus-article-current)))
6842     ;; We remove all text props from the article buffer.
6843     (kill-all-local-variables)
6844     (set-text-properties (point-min) (point-max) nil)
6845     (gnus-article-mode)
6846     (set-window-configuration winconf)
6847     (set-buffer buf)
6848     (set-window-start (get-buffer-window buf) start)
6849     (set-window-point (get-buffer-window buf) (point)))
6850   (gnus-summary-show-article))
6851
6852 (defun gnus-article-edit-exit ()
6853   "Exit the article editing without updating."
6854   (interactive)
6855   (when (or (not (buffer-modified-p))
6856             (yes-or-no-p "Article modified; kill anyway? "))
6857     (let ((curbuf (current-buffer))
6858           (p (point))
6859           (window-start (window-start)))
6860       (erase-buffer)
6861       (if (gnus-buffer-live-p gnus-original-article-buffer)
6862           (insert-buffer-substring gnus-original-article-buffer))
6863       (let ((winconf gnus-prev-winconf))
6864         (kill-all-local-variables)
6865         (gnus-article-mode)
6866         (set-window-configuration winconf)
6867         ;; Tippy-toe some to make sure that point remains where it was.
6868         (save-current-buffer
6869           (set-buffer curbuf)
6870           (set-window-start (get-buffer-window (current-buffer)) window-start)
6871           (goto-char p))))
6872     (gnus-summary-show-article)))
6873
6874 (defun gnus-article-edit-full-stops ()
6875   "Interactively repair spacing at end of sentences."
6876   (interactive)
6877   (save-excursion
6878     (goto-char (point-min))
6879     (search-forward-regexp "^$" nil t)
6880     (let ((case-fold-search nil))
6881       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
6882
6883 ;;;
6884 ;;; Article highlights
6885 ;;;
6886
6887 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
6888
6889 ;;; Internal Variables:
6890
6891 (defcustom gnus-button-url-regexp
6892   (concat
6893    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
6894    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
6895    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
6896    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
6897        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
6898              (punct "!?:;.,"))
6899          (concat
6900           "\\(?:"
6901           ;; Match paired parentheses, e.g. in Wikipedia URLs:
6902           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
6903           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
6904           "\\|"
6905           "[" chars punct     "]+" "[" chars "]"
6906           "\\)"))
6907      (concat ;; XEmacs 21.4 doesn't support POSIX.
6908       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
6909       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
6910    "\\)")
6911   "Regular expression that matches URLs."
6912   :group 'gnus-article-buttons
6913   :type 'regexp)
6914
6915 (defcustom gnus-button-valid-fqdn-regexp
6916   message-valid-fqdn-regexp
6917   "Regular expression that matches a valid FQDN."
6918   :version "22.1"
6919   :group 'gnus-article-buttons
6920   :type 'regexp)
6921
6922 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
6923 (defcustom gnus-button-valid-localpart-regexp
6924   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
6925   "Regular expression that matches a localpart of mail addresses or MIDs."
6926   :version "22.1"
6927   :group 'gnus-article-buttons
6928   :type 'regexp)
6929
6930 (defcustom gnus-button-man-handler 'manual-entry
6931   "Function to use for displaying man pages.
6932 The function must take at least one argument with a string naming the
6933 man page."
6934   :version "22.1"
6935   :type '(choice (function-item :tag "Man" manual-entry)
6936                  (function-item :tag "Woman" woman)
6937                  (function :tag "Other"))
6938   :group 'gnus-article-buttons)
6939
6940 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
6941   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
6942 If the default site is too slow, try to find a CTAN mirror, see
6943 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
6944 the variable `gnus-button-handle-ctan'."
6945   :version "22.1"
6946   :group 'gnus-article-buttons
6947   :link '(custom-manual "(gnus)Group Parameters")
6948   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
6949                  (const "http://tug.ctan.org/tex-archive/")
6950                  (const "http://www.dante.de/CTAN/")
6951                  (string :tag "Other")))
6952
6953 (defcustom gnus-button-ctan-handler 'browse-url
6954   "Function to use for displaying CTAN links.
6955 The function must take one argument, the string naming the URL."
6956   :version "22.1"
6957   :type '(choice (function-item :tag "Browse Url" browse-url)
6958                  (function :tag "Other"))
6959   :group 'gnus-article-buttons)
6960
6961 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
6962   "Bogus strings removed from CTAN URLs."
6963   :version "22.1"
6964   :group 'gnus-article-buttons
6965   :type '(choice (const "^/?tex-archive/\\|/")
6966                  (regexp :tag "Other")))
6967
6968 (defcustom gnus-button-ctan-directory-regexp
6969   (regexp-opt
6970    (list "archive-tools" "biblio" "bibliography" "digests" "documentation"
6971          "dviware" "fonts" "graphics" "help" "indexing" "info" "language"
6972          "languages" "macros" "nonfree" "obsolete" "support" "systems"
6973          "tds" "tools" "usergrps" "web") t)
6974   "Regular expression for ctan directories.
6975 It should match all directories in the top level of `gnus-ctan-url'."
6976   :version "22.1"
6977   :group 'gnus-article-buttons
6978   :type 'regexp)
6979
6980 (defcustom gnus-button-mid-or-mail-regexp
6981   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
6982           gnus-button-valid-fqdn-regexp
6983           ">?\\)\\b")
6984   "Regular expression that matches a message ID or a mail address."
6985   :version "22.1"
6986   :group 'gnus-article-buttons
6987   :type 'regexp)
6988
6989 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
6990   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
6991 Strings like this can be either a message ID or a mail address.  If it is one
6992 of the symbols `mid' or `mail', Gnus will always assume that the string is a
6993 message ID or a mail address, respectively.  If this variable is set to the
6994 symbol `ask', always query the user what do do.  If it is a function, this
6995 function will be called with the string as its only argument.  The function
6996 must return `mid', `mail', `invalid' or `ask'."
6997   :version "22.1"
6998   :group 'gnus-article-buttons
6999   :type '(choice (function-item :tag "Heuristic function"
7000                                 gnus-button-mid-or-mail-heuristic)
7001                  (const ask)
7002                  (const mid)
7003                  (const mail)))
7004
7005 (defcustom gnus-button-mid-or-mail-heuristic-alist
7006   '((-10.0 . ".+\\$.+@")
7007     (-10.0 . "#")
7008     (-10.0 . "\\*")
7009     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7010     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7011     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7012     (-1.0  . "^[^a-z]+@")
7013     ;;
7014     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7015     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7016     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7017     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7018     ;;
7019     (-2.0  . "^[0-9]")
7020     (-1.0  . "^[0-9][0-9]")
7021     ;;
7022     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7023     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7024     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7025     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7026     ;;
7027     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7028     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7029     ;;       "[0-9]{8,}.*\@"
7030     (-3.0
7031      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7032     ;; "[0-9]{12,}.*\@"
7033     ;; compensation for TDMA dated mail addresses:
7034     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7035     ;;
7036     (-20.0 . "\\.fsf@") ;; Gnus
7037     (-20.0 . "^slrn")
7038     (-20.0 . "^Pine")
7039     (-20.0 . "^alpine\\.")
7040     (-20.0 . "_-_") ;; Subject change in thread
7041     ;;
7042     (-20.0 . "\\.ln@") ;; leafnode
7043     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7044     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7045     ;;
7046     ;; (5.0 . "") ;; $local_part_len <= 7
7047     (10.0  . "^[^0-9]+@")
7048     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7049     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7050     (3.0   . "\@stud")
7051     ;;
7052     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7053     ;;
7054     (0.5   . "^[A-Z][a-z]")
7055     (0.5   . "^[A-Z][a-z][a-z]")
7056     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7057     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7058   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7059
7060 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7061 address.  The REGEXP is processed with `case-fold-search' set to nil."
7062   :version "22.1"
7063   :group 'gnus-article-buttons
7064   :type '(repeat (cons (number :tag "Rate")
7065                        (regexp :tag "Regexp"))))
7066
7067 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7068   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7069 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7070 address, `ask' if unsure and `invalid' if the string is invalid."
7071   (let ((case-fold-search nil)
7072         (list gnus-button-mid-or-mail-heuristic-alist)
7073         (result 0) rate regexp lpartlen elem)
7074     (setq lpartlen
7075           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7076     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7077     ;; Certain special cases...
7078     (when (string-match
7079            (concat
7080             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7081             "^[0-9]+\\.[0-9]+@compuserve\\|"
7082             "@public\\.gmane\\.org")
7083            mid-or-mail)
7084       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7085       (setq result 'mail))
7086     (when (string-match "@.*@\\| " mid-or-mail)
7087       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7088       (setq result 'invalid))
7089     ;; Nothing more to do, if result is not a number here...
7090     (when (numberp result)
7091       (while list
7092         (setq elem (car list)
7093               rate (car elem)
7094               regexp (cdr elem)
7095               list (cdr list))
7096         (when (string-match regexp mid-or-mail)
7097           (setq result (+ result rate))
7098           (gnus-message
7099            9 "`%s' matched `%s', rate `%s', result `%s'."
7100            mid-or-mail regexp rate result)))
7101       (when (<= lpartlen 7)
7102         (setq result (+ result 5.0))
7103         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7104                       mid-or-mail result))
7105       (when (>= lpartlen 12)
7106         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7107         (cond
7108          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7109           ;; Long local part should contain realname if e-mail address,
7110           ;; too many digits: message-id.
7111           ;; $score -= 5.0 + 0.1 * $local_part_len;
7112           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7113           (setq result (+ result rate))
7114           (gnus-message
7115            9 "Many digits in `%s', rate `%s', result `%s'."
7116            mid-or-mail rate result))
7117          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7118                         mid-or-mail)
7119           ;; Too few vowels [^aeiouy]{4,}.*\@
7120           (setq result (+ result -5.0))
7121           (gnus-message
7122            9 "Few vowels in `%s', rate `%s', result `%s'."
7123            mid-or-mail -5.0 result))
7124          (t
7125           (setq result (+ result 5.0))
7126           (gnus-message
7127            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7128     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7129     ;; Maybe we should make this a customizable alist: (condition . 'result)
7130     (cond
7131      ((symbolp result) result)
7132      ;; Now convert number into proper results:
7133      ((< result -10.0) 'mid)
7134      ((> result  10.0) 'mail)
7135      (t 'ask))))
7136
7137 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7138   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7139          (url-mid (concat "news" ":" mid-or-mail))
7140          (url-mailto (concat "mailto" ":" mid-or-mail)))
7141     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7142     (when (fboundp pref)
7143       (setq guessed
7144             ;; get rid of surrounding angles...
7145             (funcall pref
7146                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7147       (if (or (eq 'mid guessed) (eq 'mail guessed))
7148           (setq pref guessed)
7149         (setq pref 'ask)))
7150     (if (eq pref 'ask)
7151         (save-window-excursion
7152           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7153               (setq pref 'mail)
7154             (setq pref 'mid))))
7155     (cond ((eq pref 'mid)
7156            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7157            (gnus-button-handle-news url-mid))
7158           ((eq pref 'mail)
7159            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7160            (gnus-url-mailto url-mailto))
7161           (t (gnus-message 3 "Invalid string.")))))
7162
7163 (defun gnus-button-handle-custom (fun arg)
7164   "Call function FUN on argument ARG.
7165 Both FUN and ARG are supposed to be strings.  ARG will be passed
7166 as a symbol to FUN."
7167   (funcall (intern fun)
7168            (if (string-match "^customize-apropos" fun)
7169                arg
7170              (intern arg))))
7171
7172 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7173
7174 ;; FIXME: Maybe we should merge some of the functions that do quite similar
7175 ;; stuff?
7176
7177 (defun gnus-button-handle-describe-function (url)
7178   "Call `describe-function' when pushing the corresponding URL button."
7179   (describe-function
7180    (intern
7181     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7182
7183 (defun gnus-button-handle-describe-variable (url)
7184   "Call `describe-variable' when pushing the corresponding URL button."
7185   (describe-variable
7186    (intern
7187     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7188
7189 (defun gnus-button-handle-symbol (url)
7190 "Display help on variable or function.
7191 Calls `describe-variable' or `describe-function'."
7192   (let ((sym (intern url)))
7193     (cond
7194      ((fboundp sym) (describe-function sym))
7195      ((boundp sym) (describe-variable sym))
7196      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7197
7198 (defun gnus-button-handle-describe-key (url)
7199   "Call `describe-key' when pushing the corresponding URL button."
7200   (let* ((key-string
7201           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7202          (keys (ignore-errors (eval `(kbd ,key-string)))))
7203     (if keys
7204         (describe-key keys)
7205       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7206
7207 (defun gnus-button-handle-apropos (url)
7208   "Call `apropos' when pushing the corresponding URL button."
7209   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7210
7211 (defun gnus-button-handle-apropos-command (url)
7212   "Call `apropos' when pushing the corresponding URL button."
7213   (apropos-command
7214    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7215
7216 (defun gnus-button-handle-apropos-variable (url)
7217   "Call `apropos' when pushing the corresponding URL button."
7218   (funcall
7219    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7220    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7221
7222 (defun gnus-button-handle-apropos-documentation (url)
7223   "Call `apropos' when pushing the corresponding URL button."
7224   (funcall
7225    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7226    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7227
7228 (defun gnus-button-handle-library (url)
7229   "Call `locate-library' when pushing the corresponding URL button."
7230   (gnus-message 9 "url=`%s'" url)
7231   (let* ((lib (locate-library url))
7232          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7233     (if (not lib)
7234         (gnus-message 1 "Cannot locale library `%s'." url)
7235       (find-file-read-only file))))
7236
7237 (defun gnus-button-handle-ctan (url)
7238   "Call `browse-url' when pushing a CTAN URL button."
7239   (funcall
7240    gnus-button-ctan-handler
7241    (concat
7242     gnus-ctan-url
7243     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
7244
7245 (defcustom gnus-button-tex-level 5
7246   "*Integer that says how many TeX-related buttons Gnus will show.
7247 The higher the number, the more buttons will appear and the more false
7248 positives are possible.  Note that you can set this variable local to
7249 specific groups.  Setting it higher in TeX groups is probably a good idea.
7250 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7251 how to set variables in specific groups."
7252   :version "22.1"
7253   :group 'gnus-article-buttons
7254   :link '(custom-manual "(gnus)Group Parameters")
7255   :type 'integer)
7256
7257 (defcustom gnus-button-man-level 5
7258   "*Integer that says how many man-related buttons Gnus will show.
7259 The higher the number, the more buttons will appear and the more false
7260 positives are possible.  Note that you can set this variable local to
7261 specific groups.  Setting it higher in Unix groups is probably a good idea.
7262 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7263 how to set variables in specific groups."
7264   :version "22.1"
7265   :group 'gnus-article-buttons
7266   :link '(custom-manual "(gnus)Group Parameters")
7267   :type 'integer)
7268
7269 (defcustom gnus-button-emacs-level 5
7270   "*Integer that says how many emacs-related buttons Gnus will show.
7271 The higher the number, the more buttons will appear and the more false
7272 positives are possible.  Note that you can set this variable local to
7273 specific groups.  Setting it higher in Emacs or Gnus related groups is
7274 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7275 `gnus-parameters' on how to set variables in specific groups."
7276   :version "22.1"
7277   :group 'gnus-article-buttons
7278   :link '(custom-manual "(gnus)Group Parameters")
7279   :type 'integer)
7280
7281 (defcustom gnus-button-message-level 5
7282   "*Integer that says how many buttons for news or mail messages will appear.
7283 The higher the number, the more buttons will appear and the more false
7284 positives are possible."
7285   ;; mail addresses, MIDs, URLs for news, ...
7286   :version "22.1"
7287   :group 'gnus-article-buttons
7288   :type 'integer)
7289
7290 (defcustom gnus-button-browse-level 5
7291   "*Integer that says how many buttons for browsing will appear.
7292 The higher the number, the more buttons will appear and the more false
7293 positives are possible."
7294   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7295   :version "22.1"
7296   :group 'gnus-article-buttons
7297   :type 'integer)
7298
7299 (defcustom gnus-button-alist
7300   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7301      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7302     ((concat "\\b\\(nntp\\|news\\):\\("
7303              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7304      0 t gnus-button-handle-news 2)
7305     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7306      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7307     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7308      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7309     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7310     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7311      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7312     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7313      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7314     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7315      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7316     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7317      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7318     ;; RFC 2368 (The mailto URL scheme)
7319     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7320      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7321     ("\\bmailto:\\([^ \n\t]+\\)"
7322      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7323     ;; CTAN
7324     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
7325              gnus-button-ctan-directory-regexp
7326              "[^][>)!;:,'\n\t ]+\\)")
7327      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
7328     ((concat "\\btex-archive/\\("
7329              gnus-button-ctan-directory-regexp
7330              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
7331      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
7332     ((concat
7333       "\\b\\("
7334       gnus-button-ctan-directory-regexp
7335       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
7336      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
7337     ;; This is info (home-grown style) <info://foo/bar+baz>
7338     ("\\binfo://\\([^'\">\n\t ]+\\)"
7339      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7340     ;; Info GNOME style <info:foo#bar_baz>
7341     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7342      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7343     ;; Info KDE style <info:(foo)bar baz>
7344     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7345      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7346     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7347      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7348     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\([ \t\n,]*\\)\\)?"
7349      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7350      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7351     ;; This is custom
7352     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
7353      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7354     ;; Emacs help commands
7355     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7356      ;; regexp doesn't match arguments containing ` '.
7357      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7358     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7359      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7360     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7361      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7362     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7363      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7364     ;; The following entries may lead to many false positives so don't enable
7365     ;; them by default (use a high button level).
7366     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7367      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7368      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7369     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7370      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7371     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7372      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7373     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7374      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7375     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7376      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7377     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7378      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7379     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7380      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7381     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7382      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7383     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7384      ;; Unlike the other regexps we really have to require quoting
7385      ;; here to determine where it ends.
7386      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7387     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7388     ("<URL: *\\([^\n<>]*\\)>"
7389      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7390     ;; RFC 2396 (2.4.3., delims) ...
7391     ("\"URL: *\\([^\n\"]*\\)\""
7392      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7393     ;; Raw URLs.
7394     (gnus-button-url-regexp
7395      0 (>= gnus-button-browse-level 0) browse-url 0)
7396     ;; man pages
7397     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7398      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7399      gnus-button-handle-man 1)
7400     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7401     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7402      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7403      gnus-button-handle-man 1)
7404     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7405     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7406     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7407      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7408     ;; Recognizing patches to .el files.  This is somewhat obscure,
7409     ;; but considering the percentage of Gnus users who hack Emacs
7410     ;; Lisp files...
7411     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7412      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7413     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7414      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7415     ;; MID or mail: To avoid too many false positives we don't try to catch
7416     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7417     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7418     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7419     ;; so that non-ambiguous entries (see above) match first.
7420     (gnus-button-mid-or-mail-regexp
7421      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7422   "*Alist of regexps matching buttons in article bodies.
7423
7424 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7425 REGEXP: is the string (case insensitive) matching text around the button (can
7426 also be Lisp expression evaluating to a string),
7427 BUTTON: is the number of the regexp grouping actually matching the button,
7428 FORM: is a Lisp expression which must eval to true for the button to
7429 be added,
7430 CALLBACK: is the function to call when the user push this button, and each
7431 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7432
7433 CALLBACK can also be a variable, in that case the value of that
7434 variable it the real callback function."
7435   :group 'gnus-article-buttons
7436   :type '(repeat (list (choice regexp variable sexp)
7437                        (integer :tag "Button")
7438                        (sexp :tag "Form")
7439                        (function :tag "Callback")
7440                        (repeat :tag "Par"
7441                                :inline t
7442                                (integer :tag "Regexp group")))))
7443 (put 'gnus-button-alist 'risky-local-variable t)
7444
7445 (defcustom gnus-header-button-alist
7446   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7447      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7448     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7449      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7450     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7451      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7452     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7453      0 (>= gnus-button-browse-level 0) browse-url 0)
7454     ("^Subject:" gnus-button-url-regexp
7455      0 (>= gnus-button-browse-level 0) browse-url 0)
7456     ("^[^:]+:" gnus-button-url-regexp
7457      0 (>= gnus-button-browse-level 0) browse-url 0)
7458     ("^OpenPGP:.*url=" gnus-button-url-regexp
7459      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7460     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7461      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7462     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7463      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7464   "*Alist of headers and regexps to match buttons in article heads.
7465
7466 This alist is very similar to `gnus-button-alist', except that each
7467 alist has an additional HEADER element first in each entry:
7468
7469 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7470
7471 HEADER is a regexp to match a header.  For a fuller explanation, see
7472 `gnus-button-alist'."
7473   :group 'gnus-article-buttons
7474   :group 'gnus-article-headers
7475   :type '(repeat (list (regexp :tag "Header")
7476                        (choice regexp variable)
7477                        (integer :tag "Button")
7478                        (sexp :tag "Form")
7479                        (function :tag "Callback")
7480                        (repeat :tag "Par"
7481                                :inline t
7482                                (integer :tag "Regexp group")))))
7483 (put 'gnus-header-button-alist 'risky-local-variable t)
7484
7485 ;;; Commands:
7486
7487 (defun gnus-article-push-button (event)
7488   "Check text under the mouse pointer for a callback function.
7489 If the text under the mouse pointer has a `gnus-callback' property,
7490 call it with the value of the `gnus-data' text property."
7491   (interactive "e")
7492   (set-buffer (window-buffer (posn-window (event-start event))))
7493   (let* ((pos (posn-point (event-start event)))
7494          (data (get-text-property pos 'gnus-data))
7495          (fun (get-text-property pos 'gnus-callback)))
7496     (goto-char pos)
7497     (when fun
7498       (funcall fun data))))
7499
7500 (defun gnus-article-press-button ()
7501   "Check text at point for a callback function.
7502 If the text at point has a `gnus-callback' property,
7503 call it with the value of the `gnus-data' text property."
7504   (interactive)
7505   (let ((data (get-text-property (point) 'gnus-data))
7506         (fun (get-text-property (point) 'gnus-callback)))
7507     (when fun
7508       (funcall fun data))))
7509
7510 (defun gnus-article-highlight (&optional force)
7511   "Highlight current article.
7512 This function calls `gnus-article-highlight-headers',
7513 `gnus-article-highlight-citation',
7514 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7515 do the highlighting.  See the documentation for those functions."
7516   (interactive (list 'force))
7517   (gnus-article-highlight-headers)
7518   (gnus-article-highlight-citation force)
7519   (gnus-article-highlight-signature)
7520   (gnus-article-add-buttons force)
7521   (gnus-article-add-buttons-to-head))
7522
7523 (defun gnus-article-highlight-some (&optional force)
7524   "Highlight current article.
7525 This function calls `gnus-article-highlight-headers',
7526 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7527 do the highlighting.  See the documentation for those functions."
7528   (interactive (list 'force))
7529   (gnus-article-highlight-headers)
7530   (gnus-article-highlight-signature)
7531   (gnus-article-add-buttons))
7532
7533 (defun gnus-article-highlight-headers ()
7534   "Highlight article headers as specified by `gnus-header-face-alist'."
7535   (interactive)
7536   (gnus-with-article-headers
7537     (let (regexp header-face field-face from hpoints fpoints)
7538       (dolist (entry gnus-header-face-alist)
7539         (goto-char (point-min))
7540         (setq regexp (concat "^\\("
7541                              (if (string-equal "" (nth 0 entry))
7542                                  "[^\t ]"
7543                                (nth 0 entry))
7544                              "\\)")
7545               header-face (nth 1 entry)
7546               field-face (nth 2 entry))
7547         (while (and (re-search-forward regexp nil t)
7548                     (not (eobp)))
7549           (beginning-of-line)
7550           (setq from (point))
7551           (unless (search-forward ":" nil t)
7552             (forward-char 1))
7553           (when (and header-face
7554                      (not (memq (point) hpoints)))
7555             (push (point) hpoints)
7556             (gnus-put-text-property from (point) 'face header-face))
7557           (when (and field-face
7558                      (not (memq (setq from (point)) fpoints)))
7559             (push from fpoints)
7560             (if (re-search-forward "^[^ \t]" nil t)
7561                 (forward-char -2)
7562               (goto-char (point-max)))
7563             (gnus-put-text-property from (point) 'face field-face)))))))
7564
7565 (defun gnus-article-highlight-signature ()
7566   "Highlight the signature in an article.
7567 It does this by highlighting everything after
7568 `gnus-signature-separator' using the face `gnus-signature'."
7569   (interactive)
7570   (gnus-with-article-buffer
7571     (let ((inhibit-point-motion-hooks t))
7572       (save-restriction
7573         (when (and gnus-signature-face
7574                    (gnus-article-narrow-to-signature))
7575           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7576                             'face gnus-signature-face)
7577           (widen)
7578           (gnus-article-search-signature)
7579           (let ((start (match-beginning 0))
7580                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7581             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7582                                      end)))))))
7583
7584 (defun gnus-button-in-region-p (b e prop)
7585   "Say whether PROP exists in the region."
7586   (text-property-not-all b e prop nil))
7587
7588 (defun gnus-article-add-buttons (&optional force)
7589   "Find external references in the article and make buttons of them.
7590 \"External references\" are things like Message-IDs and URLs, as
7591 specified by `gnus-button-alist'."
7592   (interactive (list 'force))
7593   (gnus-with-article-buffer
7594     (let ((inhibit-point-motion-hooks t)
7595           (case-fold-search t)
7596           (alist gnus-button-alist)
7597           beg entry regexp)
7598       ;; Remove all old markers.
7599       (let (marker entry new-list)
7600         (while (setq marker (pop gnus-button-marker-list))
7601           (if (or (< marker (point-min)) (>= marker (point-max)))
7602               (push marker new-list)
7603             (goto-char marker)
7604             (when (setq entry (gnus-button-entry))
7605               (put-text-property (match-beginning (nth 1 entry))
7606                                  (match-end (nth 1 entry))
7607                                  'gnus-callback nil))
7608             (set-marker marker nil)))
7609         (setq gnus-button-marker-list new-list))
7610       ;; We skip the headers.
7611       (article-goto-body)
7612       (setq beg (point))
7613       (while (setq entry (pop alist))
7614         (setq regexp (eval (car entry)))
7615         (goto-char beg)
7616         (while (re-search-forward regexp nil t)
7617           (let ((start (match-beginning (nth 1 entry)))
7618                 (end (match-end (nth 1 entry)))
7619                 (from (match-beginning 0)))
7620             (when (and (or (eq t (nth 2 entry))
7621                            (eval (nth 2 entry)))
7622                        (not (gnus-button-in-region-p
7623                              start end 'gnus-callback)))
7624               ;; That optional form returned non-nil, so we add the
7625               ;; button.
7626               (setq from (set-marker (make-marker) from))
7627               (push from gnus-button-marker-list)
7628               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7629                            (gnus-article-extend-url-button from start end))
7630                 (gnus-article-add-button start end
7631                                          'gnus-button-push from)))))))))
7632
7633 (defun gnus-article-extend-url-button (beg start end)
7634   "Extend url button if url is folded into two or more lines.
7635 Return non-nil if button is extended.  BEG is a marker that points to
7636 the beginning position of a text containing url.  START and END are
7637 the endpoints of a url button before it is extended.  The concatenated
7638 url is put as the `gnus-button-url' overlay property on the button."
7639   (let ((opoint (point))
7640         (points (list start end))
7641         url delim regexp)
7642     (prog1
7643         (when (and (progn
7644                      (goto-char end)
7645                      (not (looking-at "[\t ]*[\">]")))
7646                    (progn
7647                      (goto-char start)
7648                      (string-match
7649                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7650                       (buffer-substring (point-at-bol) start)))
7651                    (progn
7652                      (setq url (list (buffer-substring start end))
7653                            delim (if (match-beginning 1) ">" "\""))
7654                      (beginning-of-line)
7655                      (setq regexp (concat
7656                                    (when (and (looking-at
7657                                                message-cite-prefix-regexp)
7658                                               (< (match-end 0) start))
7659                                      (regexp-quote (match-string 0)))
7660                                    "\
7661 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7662                                    delim "\\)"))
7663                      (while (progn
7664                               (forward-line 1)
7665                               (and (looking-at regexp)
7666                                    (prog1
7667                                        (match-beginning 1)
7668                                      (push (or (match-string 2)
7669                                                (match-string 1))
7670                                            url)
7671                                      (push (setq end (or (match-end 2)
7672                                                          (match-end 1)))
7673                                            points)
7674                                      (push (or (match-beginning 2)
7675                                                (match-beginning 1))
7676                                            points)))))
7677                      (match-beginning 2)))
7678           (let (gnus-article-mouse-face widget-mouse-face)
7679             (while points
7680               (gnus-article-add-button (pop points) (pop points)
7681                                        'gnus-button-push beg)))
7682           (let ((overlay (gnus-make-overlay start end)))
7683             (gnus-overlay-put overlay 'evaporate t)
7684             (gnus-overlay-put overlay 'gnus-button-url
7685                               (list (mapconcat 'identity (nreverse url) "")))
7686             (when gnus-article-mouse-face
7687               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7688           t)
7689       (goto-char opoint))))
7690
7691 ;; Add buttons to the head of an article.
7692 (defun gnus-article-add-buttons-to-head ()
7693   "Add buttons to the head of the article."
7694   (interactive)
7695   (gnus-with-article-headers
7696     (let (beg end)
7697       (dolist (entry gnus-header-button-alist)
7698         ;; Each alist entry.
7699         (goto-char (point-min))
7700         (while (re-search-forward (car entry) nil t)
7701           ;; Each header matching the entry.
7702           (setq beg (match-beginning 0))
7703           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7704                              (match-beginning 0))
7705                         (point-max)))
7706           (goto-char beg)
7707           (while (re-search-forward (eval (nth 1 entry)) end t)
7708             ;; Each match within a header.
7709             (let* ((entry (cdr entry))
7710                    (start (match-beginning (nth 1 entry)))
7711                    (end (match-end (nth 1 entry)))
7712                    (form (nth 2 entry)))
7713               (goto-char (match-end 0))
7714               (when (eval form)
7715                 (gnus-article-add-button
7716                  start end (nth 3 entry)
7717                  (buffer-substring (match-beginning (nth 4 entry))
7718                                    (match-end (nth 4 entry)))))))
7719           (goto-char end))))))
7720
7721 ;;; External functions:
7722
7723 (defun gnus-article-add-button (from to fun &optional data)
7724   "Create a button between FROM and TO with callback FUN and data DATA."
7725   (when gnus-article-button-face
7726     (gnus-overlay-put (gnus-make-overlay from to nil t)
7727                       'face gnus-article-button-face))
7728   (gnus-add-text-properties
7729    from to
7730    (nconc (and gnus-article-mouse-face
7731                (list gnus-mouse-face-prop gnus-article-mouse-face))
7732           (list 'gnus-callback fun)
7733           (and data (list 'gnus-data data))))
7734   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7735                          :button-keymap gnus-widget-button-keymap))
7736
7737 ;;; Internal functions:
7738
7739 (defun gnus-article-set-globals ()
7740   (with-current-buffer gnus-summary-buffer
7741     (gnus-set-global-variables)))
7742
7743 (defun gnus-signature-toggle (end)
7744   (gnus-with-article-buffer
7745     (let ((inhibit-point-motion-hooks t))
7746       (if (text-property-any end (point-max) 'article-type 'signature)
7747           (progn
7748             (gnus-delete-wash-type 'signature)
7749             (gnus-remove-text-properties-when
7750              'article-type 'signature end (point-max)
7751              (cons 'article-type (cons 'signature
7752                                        gnus-hidden-properties))))
7753         (gnus-add-wash-type 'signature)
7754         (gnus-add-text-properties-when
7755          'article-type nil end (point-max)
7756          (cons 'article-type (cons 'signature
7757                                    gnus-hidden-properties)))))
7758     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7759       (gnus-set-mode-line 'article))))
7760
7761 (defun gnus-button-entry ()
7762   ;; Return the first entry in `gnus-button-alist' matching this place.
7763   (let ((alist gnus-button-alist)
7764         (entry nil))
7765     (while alist
7766       (setq entry (pop alist))
7767       (if (looking-at (eval (car entry)))
7768           (setq alist nil)
7769         (setq entry nil)))
7770     entry))
7771
7772 (defun gnus-button-push (marker)
7773   ;; Push button starting at MARKER.
7774   (save-excursion
7775     (goto-char marker)
7776     (let* ((entry (gnus-button-entry))
7777            (inhibit-point-motion-hooks t)
7778            (fun (nth 3 entry))
7779            (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7780                           (get-char-property marker 'gnus-button-url))
7781                      (mapcar (lambda (group)
7782                                (let ((string (match-string group)))
7783                                  (set-text-properties
7784                                   0 (length string) nil string)
7785                                  string))
7786                              (nthcdr 4 entry)))))
7787       (cond
7788        ((fboundp fun)
7789         (apply fun args))
7790        ((and (boundp fun)
7791              (fboundp (symbol-value fun)))
7792         (apply (symbol-value fun) args))
7793        (t
7794         (gnus-message 1 "You must define `%S' to use this button"
7795                       (cons fun args)))))))
7796
7797 (defun gnus-parse-news-url (url)
7798   (let (scheme server port group message-id articles)
7799     (with-temp-buffer
7800       (insert url)
7801       (goto-char (point-min))
7802       (when (looking-at "\\([A-Za-z]+\\):")
7803         (setq scheme (match-string 1))
7804         (goto-char (match-end 0)))
7805       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7806         (setq server (match-string 1))
7807         (setq port (if (stringp (match-string 3))
7808                        (string-to-number (match-string 3))
7809                      (match-string 3)))
7810         (goto-char (match-end 0)))
7811
7812       (cond
7813        ((looking-at "\\(.*@.*\\)")
7814         (setq message-id (match-string 1)))
7815        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7816         (setq group (match-string 1)
7817               articles (split-string (match-string 2) "-")))
7818        ((looking-at "\\([^/]+\\)/?")
7819         (setq group (match-string 1)))
7820        (t
7821         (error "Unknown news URL syntax"))))
7822     (list scheme server port group message-id articles)))
7823
7824 (defun gnus-button-handle-news (url)
7825   "Fetch a news URL."
7826   (destructuring-bind (scheme server port group message-id articles)
7827       (gnus-parse-news-url url)
7828     (cond
7829      (message-id
7830       (with-current-buffer gnus-summary-buffer
7831         (if server
7832             (let ((gnus-refer-article-method
7833                    (nconc (list (list 'nntp server))
7834                           gnus-refer-article-method))
7835                   (nntp-port-number (or port "nntp")))
7836               (gnus-message 7 "Fetching %s with %s"
7837                             message-id gnus-refer-article-method)
7838               (gnus-summary-refer-article message-id))
7839           (gnus-summary-refer-article message-id))))
7840      (group
7841       (gnus-button-fetch-group url)))))
7842
7843 (defun gnus-button-patch (library line)
7844   "Visit an Emacs Lisp library LIBRARY on line LINE."
7845   (interactive)
7846   (let ((file (locate-library (file-name-nondirectory library))))
7847     (unless file
7848       (error "Couldn't find library %s" library))
7849     (find-file file)
7850     (goto-line (string-to-number line))))
7851
7852 (defun gnus-button-handle-man (url)
7853   "Fetch a man page."
7854   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7855   (when (eq gnus-button-man-handler 'woman)
7856     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
7857   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7858   (funcall gnus-button-man-handler url))
7859
7860 (defun gnus-button-handle-info-url (url)
7861   "Fetch an info URL."
7862   (setq url (mm-subst-char-in-string ?+ ?\  url))
7863   (cond
7864    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
7865     (gnus-info-find-node
7866      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
7867                      "Gnus")
7868              ")" (gnus-url-unhex-string (match-string 2 url)))))
7869    ((string-match "([^)\"]+)[^\"]+" url)
7870     (setq url
7871           (gnus-replace-in-string
7872            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
7873     (gnus-info-find-node url))
7874    (t (error "Can't parse %s" url))))
7875
7876 (defun gnus-button-handle-info-url-gnome (url)
7877   "Fetch GNOME style info URL."
7878   (setq url (mm-subst-char-in-string ?_ ?\  url))
7879   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
7880       (gnus-info-find-node
7881        (concat "("
7882                (gnus-url-unhex-string
7883                  (match-string 1 url))
7884                ")"
7885                (or (gnus-url-unhex-string
7886                     (match-string 2 url))
7887                    "Top")))
7888     (error "Can't parse %s" url)))
7889
7890 (defun gnus-button-handle-info-url-kde (url)
7891   "Fetch KDE style info URL."
7892   (gnus-info-find-node (gnus-url-unhex-string url)))
7893
7894 ;; (info) will autoload info.el
7895 (declare-function Info-menu "info" (menu-item &optional fork))
7896
7897 (defun gnus-button-handle-info-keystrokes (url)
7898   "Call `info' when pushing the corresponding URL button."
7899   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
7900   (let (node indx comma)
7901     (if (string-match
7902          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
7903                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
7904                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET"
7905                  "\\(?:[ \t\n,]*\\)\\)?")
7906          url)
7907         (setq node (match-string 2 url)
7908               indx (match-string 3 url))
7909       (error "Can't parse %s" url))
7910     (info)
7911     (Info-directory)
7912     (Info-menu node)
7913     (when (> (length indx) 0)
7914       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
7915                             "\\([ \t\n,]*\\)")
7916                     indx)
7917       (setq comma (match-string 2 indx))
7918       (setq indx  (match-string 1 indx))
7919       (Info-index indx)
7920       (when comma
7921         (dotimes (i (with-temp-buffer
7922                       (insert comma)
7923                       (how-many "," (point-min) (point-max))))
7924           (Info-index-next 1)))
7925       nil)))
7926
7927 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
7928 (declare-function pgg-display-output-buffer "pgg" (start end status))
7929
7930 (defun gnus-button-openpgp (url)
7931   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
7932   (with-temp-buffer
7933     (mm-url-insert-file-contents-external url)
7934     (pgg-snarf-keys-region (point-min) (point-max))
7935     (pgg-display-output-buffer nil nil nil)))
7936
7937 (defun gnus-button-message-id (message-id)
7938   "Fetch MESSAGE-ID."
7939   (with-current-buffer gnus-summary-buffer
7940     (gnus-summary-refer-article message-id)))
7941
7942 (defun gnus-button-fetch-group (address &rest ignore)
7943   "Fetch GROUP specified by ADDRESS."
7944   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
7945                       address)
7946     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
7947     ;; for nntp:// and news://
7948     (setq address (match-string 3 address)))
7949   (if (not (string-match "[:/]" address))
7950       ;; This is just a simple group url.
7951       (gnus-group-read-ephemeral-group address gnus-select-method)
7952     (if (not
7953          (string-match
7954           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
7955           address))
7956         (error "Can't parse %s" address)
7957       (gnus-group-read-ephemeral-group
7958        (match-string 4 address)
7959        `(nntp ,(match-string 1 address)
7960               (nntp-address ,(match-string 1 address))
7961               (nntp-port-number ,(if (match-end 3)
7962                                      (match-string 3 address)
7963                                    "nntp")))
7964        nil nil nil
7965        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
7966
7967 (defun gnus-url-parse-query-string (query &optional downcase)
7968   (let (retval pairs cur key val)
7969     (setq pairs (split-string query "&"))
7970     (while pairs
7971       (setq cur (car pairs)
7972             pairs (cdr pairs))
7973       (if (not (string-match "=" cur))
7974           nil                           ; Grace
7975         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
7976               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
7977         (if downcase
7978             (setq key (downcase key)))
7979         (setq cur (assoc key retval))
7980         (if cur
7981             (setcdr cur (cons val (cdr cur)))
7982           (setq retval (cons (list key val) retval)))))
7983     retval))
7984
7985 (defun gnus-url-mailto (url)
7986   ;; Send mail to someone
7987   (when (string-match "mailto:/*\\(.*\\)" url)
7988     (setq url (substring url (match-beginning 1) nil)))
7989   (let (to args subject func)
7990     (setq args (gnus-url-parse-query-string
7991                 (if (string-match "^\\?" url)
7992                     (substring url 1)
7993                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
7994                       (concat "to=" (match-string 1 url) "&"
7995                               (match-string 2 url))
7996                     (concat "to=" url)))
7997                 t)
7998           subject (cdr-safe (assoc "subject" args)))
7999     (gnus-msg-mail)
8000     (while args
8001       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8002       (if (fboundp func)
8003           (funcall func)
8004         (message-position-on-field (caar args)))
8005       (insert (gnus-replace-in-string
8006                (mapconcat 'identity (reverse (cdar args)) ", ")
8007                "\r\n" "\n" t))
8008       (setq args (cdr args)))
8009     (if subject
8010         (message-goto-body)
8011       (message-goto-subject))))
8012
8013 (defun gnus-button-embedded-url (address)
8014   "Activate ADDRESS with `browse-url'."
8015   (browse-url (gnus-strip-whitespace address)))
8016
8017 ;;; Next/prev buttons in the article buffer.
8018
8019 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8020 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8021
8022 (defvar gnus-prev-page-map
8023   (let ((map (make-sparse-keymap)))
8024     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8025     (define-key map "\r" 'gnus-button-prev-page)
8026     map))
8027
8028 (defvar gnus-next-page-map
8029   (let ((map (make-sparse-keymap)))
8030     (unless (>= emacs-major-version 21)
8031       ;; XEmacs doesn't care.
8032       (set-keymap-parent map gnus-article-mode-map))
8033     (define-key map gnus-mouse-2 'gnus-button-next-page)
8034     (define-key map "\r" 'gnus-button-next-page)
8035     map))
8036
8037 (defun gnus-insert-prev-page-button ()
8038   (let ((b (point)) e
8039         (inhibit-read-only t))
8040     (gnus-eval-format
8041      gnus-prev-page-line-format nil
8042      `(keymap ,gnus-prev-page-map
8043               gnus-prev t
8044               gnus-callback gnus-article-button-prev-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-prev-page
8056      :button-keymap gnus-prev-page-map)))
8057
8058 (defun gnus-button-next-page (&optional args more-args)
8059   "Go to the next page."
8060   (interactive)
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-button-prev-page (&optional args more-args)
8067   "Go to the prev page."
8068   (interactive)
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 (defun gnus-insert-next-page-button ()
8075   (let ((b (point)) e
8076         (inhibit-read-only t))
8077     (gnus-eval-format gnus-next-page-line-format nil
8078                       `(keymap ,gnus-next-page-map
8079                                gnus-next t
8080                                gnus-callback gnus-article-button-next-page
8081                                article-type annotation))
8082     (setq e (if (bolp)
8083                 ;; Exclude a newline.
8084                 (1- (point))
8085               (point)))
8086     (when gnus-article-button-face
8087       (gnus-overlay-put (gnus-make-overlay b e nil t)
8088                         'face gnus-article-button-face))
8089     (widget-convert-button
8090      'link b e
8091      :action 'gnus-button-next-page
8092      :button-keymap gnus-next-page-map)))
8093
8094 (defun gnus-article-button-next-page (arg)
8095   "Go to the next page."
8096   (interactive "P")
8097   (let ((win (selected-window)))
8098     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8099     (gnus-article-next-page)
8100     (select-window win)))
8101
8102 (defun gnus-article-button-prev-page (arg)
8103   "Go to the prev page."
8104   (interactive "P")
8105   (let ((win (selected-window)))
8106     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8107     (gnus-article-prev-page)
8108     (select-window win)))
8109
8110 (defvar gnus-decode-header-methods
8111   '(mail-decode-encoded-word-region)
8112   "List of methods used to decode headers.
8113
8114 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8115 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8116 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8117 whose names match REGEXP.
8118
8119 For example:
8120 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8121  mail-decode-encoded-word-region
8122  (\"chinese\" . rfc1843-decode-region))
8123 ")
8124
8125 (defvar gnus-decode-header-methods-cache nil)
8126
8127 (defun gnus-multi-decode-header (start end)
8128   "Apply the functions from `gnus-encoded-word-methods' that match."
8129   (unless (and gnus-decode-header-methods-cache
8130                (eq gnus-newsgroup-name
8131                    (car gnus-decode-header-methods-cache)))
8132     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8133     (dolist (x gnus-decode-header-methods)
8134       (if (symbolp x)
8135           (nconc gnus-decode-header-methods-cache (list x))
8136         (if (and gnus-newsgroup-name
8137                  (string-match (car x) gnus-newsgroup-name))
8138             (nconc gnus-decode-header-methods-cache
8139                    (list (cdr x)))))))
8140   (let ((xlist gnus-decode-header-methods-cache))
8141     (pop xlist)
8142     (save-restriction
8143       (narrow-to-region start end)
8144       (while xlist
8145         (funcall (pop xlist) (point-min) (point-max))))))
8146
8147 ;;;
8148 ;;; Treatment top-level handling.
8149 ;;;
8150
8151 (defun gnus-treat-article (condition &optional part-number total-parts type)
8152   (let ((length (- (point-max) (point-min)))
8153         (alist gnus-treatment-function-alist)
8154         (article-goto-body-goes-to-point-min-p t)
8155         (treated-type
8156          (or (not type)
8157              (catch 'found
8158                (let ((list gnus-article-treat-types))
8159                  (while list
8160                    (when (string-match (pop list) type)
8161                      (throw 'found t)))))))
8162         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8163         val elem)
8164     (gnus-run-hooks 'gnus-part-display-hook)
8165     (dolist (elem alist)
8166       (setq val
8167             (save-excursion
8168               (when (gnus-buffer-live-p gnus-summary-buffer)
8169                 (set-buffer gnus-summary-buffer))
8170               (symbol-value (car elem))))
8171       (when (and (or (consp val)
8172                      treated-type)
8173                  (gnus-treat-predicate val)
8174                  (or (not (get (car elem) 'highlight))
8175                      highlightp))
8176         (save-restriction
8177           (funcall (cadr elem)))))))
8178
8179 ;; Dynamic variables.
8180 (defvar part-number)
8181 (defvar total-parts)
8182 (defvar type)
8183 (defvar condition)
8184 (defvar length)
8185
8186 (defun gnus-treat-predicate (val)
8187   (cond
8188    ((null val)
8189     nil)
8190    (condition
8191     (eq condition val))
8192    ((and (listp val)
8193          (stringp (car val)))
8194     (apply 'gnus-or (mapcar `(lambda (s)
8195                                (string-match s ,(or gnus-newsgroup-name "")))
8196                             val)))
8197    ((listp val)
8198     (let ((pred (pop val)))
8199       (cond
8200        ((eq pred 'or)
8201         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8202        ((eq pred 'and)
8203         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8204        ((eq pred 'not)
8205         (not (gnus-treat-predicate (car val))))
8206        ((eq pred 'typep)
8207         (equal (car val) type))
8208        (t
8209         (error "%S is not a valid predicate" pred)))))
8210    ((eq val t)
8211     t)
8212    ((eq val 'head)
8213     nil)
8214    ((eq val 'first)
8215     (eq part-number 1))
8216    ((eq val 'last)
8217     (eq part-number total-parts))
8218    ((numberp val)
8219     (< length val))
8220    (t
8221     (error "%S is not a valid value" val))))
8222
8223 (defun gnus-article-encrypt-body (protocol &optional n)
8224   "Encrypt the article body."
8225   (interactive
8226    (list
8227     (or gnus-article-encrypt-protocol
8228         (completing-read "Encrypt protocol: "
8229                          gnus-article-encrypt-protocol-alist
8230                          nil t))
8231     current-prefix-arg))
8232   ;; User might hit `K E' instead of `K e', so prompt once.
8233   (when (and gnus-article-encrypt-protocol
8234              gnus-novice-user)
8235     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8236       (error "Encrypt aborted.")))
8237   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8238     (unless func
8239       (error "Can't find the encrypt protocol %s" protocol))
8240     (if (member gnus-newsgroup-name '("nndraft:delayed"
8241                                       "nndraft:drafts"
8242                                       "nndraft:queue"))
8243         (error "Can't encrypt the article in group %s"
8244                gnus-newsgroup-name))
8245     (gnus-summary-iterate n
8246       (with-current-buffer gnus-summary-buffer
8247         (let ((mail-parse-charset gnus-newsgroup-charset)
8248               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8249               (summary-buffer gnus-summary-buffer)
8250               references point)
8251           (gnus-set-global-variables)
8252           (when (gnus-group-read-only-p)
8253             (error "The current newsgroup does not support article encrypt"))
8254           (gnus-summary-show-article t)
8255           (setq references
8256               (or (mail-header-references gnus-current-headers) ""))
8257           (set-buffer gnus-article-buffer)
8258           (let* ((inhibit-read-only t)
8259                  (headers
8260                   (mapcar (lambda (field)
8261                             (and (save-restriction
8262                                    (message-narrow-to-head)
8263                                    (goto-char (point-min))
8264                                    (search-forward field nil t))
8265                                  (prog2
8266                                      (message-narrow-to-field)
8267                                      (buffer-string)
8268                                    (delete-region (point-min) (point-max))
8269                                    (widen))))
8270                           '("Content-Type:" "Content-Transfer-Encoding:"
8271                             "Content-Disposition:"))))
8272             (message-narrow-to-head)
8273             (message-remove-header "MIME-Version")
8274             (goto-char (point-max))
8275             (setq point (point))
8276             (insert (apply 'concat headers))
8277             (widen)
8278             (narrow-to-region point (point-max))
8279             (let ((message-options message-options))
8280               (message-options-set 'message-sender user-mail-address)
8281               (message-options-set 'message-recipients user-mail-address)
8282               (message-options-set 'message-sign-encrypt 'not)
8283               (funcall func))
8284             (goto-char (point-min))
8285             (insert "MIME-Version: 1.0\n")
8286             (widen)
8287             (gnus-summary-edit-article-done
8288              references nil summary-buffer t))
8289           (when gnus-keep-backlog
8290             (gnus-backlog-remove-article
8291              (car gnus-article-current) (cdr gnus-article-current)))
8292           (when (get-buffer gnus-original-article-buffer)
8293             (with-current-buffer gnus-original-article-buffer
8294               (setq gnus-original-article nil)))
8295           (when gnus-use-cache
8296             (gnus-cache-update-article
8297              (car gnus-article-current) (cdr gnus-article-current))))))))
8298
8299 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8300   "The following specs can be used:
8301 %t  The security MIME type
8302 %i  Additional info
8303 %d  Details
8304 %D  Details if button is pressed")
8305
8306 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8307   "The following specs can be used:
8308 %t  The security MIME type
8309 %i  Additional info
8310 %d  Details
8311 %D  Details if button is pressed")
8312
8313 (defvar gnus-mime-security-button-line-format-alist
8314   '((?t gnus-tmp-type ?s)
8315     (?i gnus-tmp-info ?s)
8316     (?d gnus-tmp-details ?s)
8317     (?D gnus-tmp-pressed-details ?s)))
8318
8319 (defvar gnus-mime-security-button-commands
8320   '((gnus-article-press-button "\r" "Show Detail")
8321     (undefined "v")
8322     (undefined "t")
8323     (undefined "C")
8324     (gnus-mime-security-save-part "o" "Save...")
8325     (undefined "\C-o")
8326     (undefined "r")
8327     (undefined "d")
8328     (undefined "c")
8329     (undefined "i")
8330     (undefined "E")
8331     (undefined "e")
8332     (undefined "p")
8333     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8334     (undefined ".")))
8335
8336 (defvar gnus-mime-security-button-map
8337   (let ((map (make-sparse-keymap)))
8338     (define-key map gnus-mouse-2 'gnus-article-push-button)
8339     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8340     (dolist (c gnus-mime-security-button-commands)
8341       (define-key map (cadr c) (car c)))
8342     map))
8343
8344 (easy-menu-define
8345   gnus-mime-security-button-menu gnus-mime-security-button-map
8346   "Security button menu."
8347   `("Security Part"
8348     ,@(delq nil
8349             (mapcar (lambda (c)
8350                       (unless (eq (car c) 'undefined)
8351                         (vector (caddr c) (car c) :active t)))
8352                     gnus-mime-security-button-commands))))
8353
8354 (defun gnus-mime-security-button-menu (event prefix)
8355   "Construct a context-sensitive menu of security commands."
8356   (interactive "e\nP")
8357   (save-window-excursion
8358     (let ((pos (event-start event)))
8359       (select-window (posn-window pos))
8360       (goto-char (posn-point pos))
8361       (gnus-article-check-buffer)
8362       (popup-menu gnus-mime-security-button-menu nil prefix))))
8363
8364 (defvar gnus-mime-security-details-buffer nil)
8365
8366 (defvar gnus-mime-security-button-pressed nil)
8367
8368 (defvar gnus-mime-security-show-details-inline t
8369   "If non-nil, show details in the article buffer.")
8370
8371 (defun gnus-mime-security-verify-or-decrypt (handle)
8372   (mm-remove-parts (cdr handle))
8373   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8374         point (inhibit-read-only t))
8375     (if region
8376         (goto-char (car region)))
8377     (setq point (point))
8378     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8379       (let* ((mm-verify-option 'known)
8380              (mm-decrypt-option 'known)
8381              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8382         (unless (eq nparts (cdr handle))
8383           (mm-destroy-parts (cdr handle))
8384           (setcdr handle nparts))))
8385     (gnus-mime-display-security handle)
8386     (when region
8387       (delete-region (point) (cdr region))
8388       (set-marker (car region) nil)
8389       (set-marker (cdr region) nil))
8390     (goto-char point)))
8391
8392 (defun gnus-mime-security-show-details (handle)
8393   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8394     (if (not details)
8395         (gnus-message 5 "No details.")
8396       (if gnus-mime-security-show-details-inline
8397           (let ((gnus-mime-security-button-pressed
8398                  (not (get-text-property (point) 'gnus-mime-details)))
8399                 (gnus-mime-security-button-line-format
8400                  (get-text-property (point) 'gnus-line-format))
8401                 (inhibit-read-only t))
8402             (forward-char -1)
8403             (while (eq (get-text-property (point) 'gnus-line-format)
8404                        gnus-mime-security-button-line-format)
8405               (forward-char -1))
8406             (forward-char)
8407             (save-restriction
8408               (narrow-to-region (point) (point))
8409               (gnus-insert-mime-security-button handle))
8410             (delete-region (point)
8411                            (or (text-property-not-all
8412                                 (point) (point-max)
8413                                 'gnus-line-format
8414                                 gnus-mime-security-button-line-format)
8415                                (point-max))))
8416         ;; Not inlined.
8417         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8418             (with-current-buffer gnus-mime-security-details-buffer
8419               (erase-buffer)
8420               t)
8421           (setq gnus-mime-security-details-buffer
8422                 (gnus-get-buffer-create "*MIME Security Details*")))
8423         (with-current-buffer gnus-mime-security-details-buffer
8424           (insert details)
8425           (goto-char (point-min)))
8426         (pop-to-buffer gnus-mime-security-details-buffer)))))
8427
8428 (defun gnus-mime-security-press-button (handle)
8429   (save-excursion
8430     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8431         (gnus-mime-security-show-details handle)
8432       (gnus-mime-security-verify-or-decrypt handle))))
8433
8434 (defun gnus-insert-mime-security-button (handle &optional displayed)
8435   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8436          (gnus-tmp-type
8437           (concat
8438            (or (nth 2 (assoc protocol mm-verify-function-alist))
8439                (nth 2 (assoc protocol mm-decrypt-function-alist))
8440                "Unknown")
8441            (if (equal (car handle) "multipart/signed")
8442                " Signed" " Encrypted")
8443            " Part"))
8444          (gnus-tmp-info
8445           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8446               "Undecided"))
8447          (gnus-tmp-details
8448           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8449          gnus-tmp-pressed-details
8450          b e)
8451     (setq gnus-tmp-details
8452           (if gnus-tmp-details
8453               (concat "\n" gnus-tmp-details)
8454             ""))
8455     (setq gnus-tmp-pressed-details
8456           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8457     (unless (bolp)
8458       (insert "\n"))
8459     (setq b (point))
8460     (gnus-eval-format
8461      gnus-mime-security-button-line-format
8462      gnus-mime-security-button-line-format-alist
8463      `(keymap ,gnus-mime-security-button-map
8464          gnus-callback gnus-mime-security-press-button
8465          gnus-line-format ,gnus-mime-security-button-line-format
8466          gnus-mime-details ,gnus-mime-security-button-pressed
8467          article-type annotation
8468          gnus-data ,handle))
8469     (setq e (if (bolp)
8470                 ;; Exclude a newline.
8471                 (1- (point))
8472               (point)))
8473     (when gnus-article-button-face
8474       (gnus-overlay-put (gnus-make-overlay b e nil t)
8475                         'face gnus-article-button-face))
8476     (widget-convert-button
8477      'link b e
8478      :mime-handle handle
8479      :action 'gnus-widget-press-button
8480      :button-keymap gnus-mime-security-button-map
8481      :help-echo
8482      (lambda (widget/window &optional overlay pos)
8483        ;; Needed to properly clear the message due to a bug in
8484        ;; wid-edit (XEmacs only).
8485        (when (boundp 'help-echo-owns-message)
8486          (setq help-echo-owns-message t))
8487        (format
8488         "%S: show detail; %S: more options"
8489         (aref gnus-mouse-2 0)
8490         (aref gnus-down-mouse-3 0))))))
8491
8492 (defun gnus-mime-display-security (handle)
8493   (save-restriction
8494     (narrow-to-region (point) (point))
8495     (unless (gnus-unbuttonized-mime-type-p (car handle))
8496       (gnus-insert-mime-security-button handle))
8497     (gnus-mime-display-part (cadr handle))
8498     (unless (bolp)
8499       (insert "\n"))
8500     (unless (gnus-unbuttonized-mime-type-p (car handle))
8501       (let ((gnus-mime-security-button-line-format
8502              gnus-mime-security-button-end-line-format))
8503         (gnus-insert-mime-security-button handle)))
8504     (mm-set-handle-multipart-parameter
8505      handle 'gnus-region
8506      (cons (set-marker (make-marker) (point-min))
8507            (set-marker (make-marker) (point-max))))
8508     (goto-char (point-max))))
8509
8510 (defun gnus-mime-security-run-function (function)
8511   "Run FUNCTION with the security part under point."
8512   (gnus-article-check-buffer)
8513   (let ((data (get-text-property (point) 'gnus-data))
8514         buffer handle)
8515     (when (and (stringp (car-safe data))
8516                (setq buffer (mm-handle-multipart-original-buffer data))
8517                (setq handle (cadr data)))
8518       (if (bufferp (mm-handle-buffer handle))
8519           (progn
8520             (setq handle (cons buffer (copy-sequence (cdr handle))))
8521             (mm-handle-set-undisplayer handle nil))
8522         (setq handle (mm-make-handle
8523                       buffer
8524                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8525                       nil nil nil nil nil nil)))
8526       (funcall function handle))))
8527
8528 (defun gnus-mime-security-save-part ()
8529   "Save the security part under point."
8530   (interactive)
8531   (gnus-mime-security-run-function 'mm-save-part))
8532
8533 (defun gnus-mime-security-pipe-part ()
8534   "Pipe the security part under point to a process."
8535   (interactive)
8536   (gnus-mime-security-run-function 'mm-pipe-part))
8537
8538 (gnus-ems-redefine)
8539
8540 (provide 'gnus-art)
8541
8542 (run-hooks 'gnus-art-load-hook)
8543
8544 ;; arch-tag: 2654516f-6279-48f9-a83b-05c1fa450c33
8545 ;;; gnus-art.el ends here