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