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