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