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