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