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