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