Merge from emacs--devo--0, emacs--rel--22
[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, 2007, 2008 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 3, 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 ;; For Emacs < 22.2.
31 (eval-and-compile
32   (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
33 (eval-when-compile
34   (require 'cl))
35 (defvar tool-bar-map)
36 (defvar w3m-minor-mode-map)
37
38 (require 'gnus)
39 ;; Avoid the "Recursive load suspected" error in Emacs 21.1.
40 (eval-and-compile
41   (let ((recursive-load-depth-limit 100))
42     (require 'gnus-sum)))
43 (require 'gnus-spec)
44 (require 'gnus-int)
45 (require 'gnus-win)
46 (require 'mm-bodies)
47 (require 'mail-parse)
48 (require 'mm-decode)
49 (require 'mm-view)
50 (require 'wid-edit)
51 (require 'mm-uu)
52 (require 'message)
53
54 (autoload 'gnus-msg-mail "gnus-msg" nil t)
55 (autoload 'gnus-button-mailto "gnus-msg")
56 (autoload 'gnus-button-reply "gnus-msg" nil t)
57 (autoload 'parse-time-string "parse-time" nil nil)
58 (autoload 'ansi-color-apply-on-region "ansi-color")
59 (autoload 'mm-url-insert-file-contents-external "mm-url")
60 (autoload 'mm-extern-cache-contents "mm-extern")
61
62 (defgroup gnus-article nil
63   "Article display."
64   :link '(custom-manual "(gnus)Article Buffer")
65   :group 'gnus)
66
67 (defgroup gnus-article-treat nil
68   "Treating article parts."
69   :link '(custom-manual "(gnus)Article Hiding")
70   :group 'gnus-article)
71
72 (defgroup gnus-article-hiding nil
73   "Hiding article parts."
74   :link '(custom-manual "(gnus)Article Hiding")
75   :group 'gnus-article)
76
77 (defgroup gnus-article-highlight nil
78   "Article highlighting."
79   :link '(custom-manual "(gnus)Article Highlighting")
80   :group 'gnus-article
81   :group 'gnus-visual)
82
83 (defgroup gnus-article-signature nil
84   "Article signatures."
85   :link '(custom-manual "(gnus)Article Signature")
86   :group 'gnus-article)
87
88 (defgroup gnus-article-headers nil
89   "Article headers."
90   :link '(custom-manual "(gnus)Hiding Headers")
91   :group 'gnus-article)
92
93 (defgroup gnus-article-washing nil
94   "Special commands on articles."
95   :link '(custom-manual "(gnus)Article Washing")
96   :group 'gnus-article)
97
98 (defgroup gnus-article-emphasis nil
99   "Fontisizing articles."
100   :link '(custom-manual "(gnus)Article Fontisizing")
101   :group 'gnus-article)
102
103 (defgroup gnus-article-saving nil
104   "Saving articles."
105   :link '(custom-manual "(gnus)Saving Articles")
106   :group 'gnus-article)
107
108 (defgroup gnus-article-mime nil
109   "Worshiping the MIME wonder."
110   :link '(custom-manual "(gnus)Using MIME")
111   :group 'gnus-article)
112
113 (defgroup gnus-article-buttons nil
114   "Pushable buttons in the article buffer."
115   :link '(custom-manual "(gnus)Article Buttons")
116   :group 'gnus-article)
117
118 (defgroup gnus-article-various nil
119   "Other article options."
120   :link '(custom-manual "(gnus)Misc Article")
121   :group 'gnus-article)
122
123 (defcustom gnus-ignored-headers
124   (mapcar
125    (lambda (header)
126      (concat "^" header ":"))
127    '("Path" "Expires" "Date-Received" "References" "Xref" "Lines"
128      "Relay-Version" "Message-ID" "Approved" "Sender" "Received"
129      "X-UIDL" "MIME-Version" "Return-Path" "In-Reply-To"
130      "Content-Type" "Content-Transfer-Encoding" "X-WebTV-Signature"
131      "X-MimeOLE" "X-MSMail-Priority" "X-Priority" "X-Loop"
132      "X-Authentication-Warning" "X-MIME-Autoconverted" "X-Face"
133      "X-Attribution" "X-Originating-IP" "Delivered-To"
134      "NNTP-[-A-Za-z]+" "Distribution" "X-no-archive" "X-Trace"
135      "X-Complaints-To" "X-NNTP-Posting-Host" "X-Orig.*"
136      "Abuse-Reports-To" "Cache-Post-Path" "X-Article-Creation-Date"
137      "X-Poster" "X-Mail2News-Path" "X-Server-Date" "X-Cache"
138      "Originator" "X-Problems-To" "X-Auth-User" "X-Post-Time"
139      "X-Admin" "X-UID" "Resent-[-A-Za-z]+" "X-Mailing-List"
140      "Precedence" "Original-[-A-Za-z]+" "X-filename" "X-Orcpt"
141      "Old-Received" "X-Pgp" "X-Auth" "X-From-Line"
142      "X-Gnus-Article-Number" "X-Majordomo" "X-Url" "X-Sender"
143      "MBOX-Line" "Priority" "X400-[-A-Za-z]+"
144      "Status" "X-Gnus-Mail-Source" "Cancel-Lock"
145      "X-FTN" "X-EXP32-SerialNo" "Encoding" "Importance"
146      "Autoforwarded" "Original-Encoded-Information-Types" "X-Ya-Pop3"
147      "X-Face-Version" "X-Vms-To" "X-ML-NAME" "X-ML-COUNT"
148      "Mailing-List" "X-finfo" "X-md5sum" "X-md5sum-Origin"
149      "X-Sun-Charset" "X-Accept-Language" "X-Envelope-Sender"
150      "List-[A-Za-z]+" "X-Listprocessor-Version"
151      "X-Received" "X-Distribute" "X-Sequence" "X-Juno-Line-Breaks"
152      "X-Notes-Item" "X-MS-TNEF-Correlator" "x-uunet-gateway"
153      "X-Received" "Content-length" "X-precedence"
154      "X-Authenticated-User" "X-Comment" "X-Report" "X-Abuse-Info"
155      "X-HTTP-Proxy" "X-Mydeja-Info" "X-Copyright" "X-No-Markup"
156      "X-Abuse-Info" "X-From_" "X-Accept-Language" "Errors-To"
157      "X-BeenThere" "X-Mailman-Version" "List-Help" "List-Post"
158      "List-Subscribe" "List-Id" "List-Unsubscribe" "List-Archive"
159      "X-Content-length" "X-Posting-Agent" "Original-Received"
160      "X-Request-PGP" "X-Fingerprint" "X-WRIEnvto" "X-WRIEnvfrom"
161      "X-Virus-Scanned" "X-Delivery-Agent" "Posted-Date" "X-Gateway"
162      "X-Local-Origin" "X-Local-Destination" "X-UserInfo1"
163      "X-Received-Date" "X-Hashcash" "Face" "X-DMCA-Notifications"
164      "X-Abuse-and-DMCA-Info" "X-Postfilter" "X-Gpg-.*" "X-Disclaimer"
165      "Envelope-To" "X-Spam-Score" "System-Type" "X-Injected-Via-Gmane"
166      "X-Gmane-NNTP-Posting-Host" "Jabber-ID" "Archived-At"
167      "Envelope-Sender" "Envelope-Recipients"))
168   "*All headers that start with this regexp will be hidden.
169 This variable can also be a list of regexps of headers to be ignored.
170 If `gnus-visible-headers' is non-nil, this variable will be ignored."
171   :type '(choice :custom-show nil
172                  regexp
173                  (repeat regexp))
174   :group 'gnus-article-hiding)
175
176 (defcustom gnus-visible-headers
177   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
178   "*All headers that do not match this regexp will be hidden.
179 This variable can also be a list of regexp of headers to remain visible.
180 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
181   :type '(choice
182           (repeat :value-to-internal (lambda (widget value)
183                                        (custom-split-regexp-maybe value))
184                   :match (lambda (widget value)
185                            (or (stringp value)
186                                (widget-editable-list-match widget value)))
187                   regexp)
188           (const :tag "Use gnus-ignored-headers" nil)
189           regexp)
190   :group 'gnus-article-hiding)
191
192 (defcustom gnus-sorted-header-list
193   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
194     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
195   "*This variable is a list of regular expressions.
196 If it is non-nil, headers that match the regular expressions will
197 be placed first in the article buffer in the sequence specified by
198 this list."
199   :type '(repeat regexp)
200   :group 'gnus-article-hiding)
201
202 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
203   "Headers that are only to be displayed if they have interesting data.
204 Possible values in this list are:
205
206   'empty       Headers with no content.
207   'newsgroups  Newsgroup identical to Gnus group.
208   'to-address  To identical to To-address.
209   'to-list     To identical to To-list.
210   'cc-list     CC identical to To-list.
211   'followup-to Followup-to identical to Newsgroups.
212   'reply-to    Reply-to identical to From.
213   'date        Date less than four days old.
214   'long-to     To and/or Cc longer than 1024 characters.
215   'many-to     Multiple To and/or Cc."
216   :type '(set (const :tag "Headers with no content." empty)
217               (const :tag "Newsgroups identical to Gnus group." newsgroups)
218               (const :tag "To identical to To-address." to-address)
219               (const :tag "To identical to To-list." to-list)
220               (const :tag "CC identical to To-list." cc-list)
221               (const :tag "Followup-to identical to Newsgroups." followup-to)
222               (const :tag "Reply-to identical to From." reply-to)
223               (const :tag "Date less than four days old." date)
224               (const :tag "To and/or Cc longer than 1024 characters." long-to)
225               (const :tag "Multiple To and/or Cc headers." many-to))
226   :group 'gnus-article-hiding)
227
228 (defcustom gnus-article-skip-boring nil
229   "Skip over text that is not worth reading.
230 By default, if you set this t, then Gnus will display citations and
231 signatures, but will never scroll down to show you a page consisting
232 only of boring text.  Boring text is controlled by
233 `gnus-article-boring-faces'."
234   :version "22.1"
235   :type 'boolean
236   :group 'gnus-article-hiding)
237
238 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
239   "Regexp matching signature separator.
240 This can also be a list of regexps.  In that case, it will be checked
241 from head to tail looking for a separator.  Searches will be done from
242 the end of the buffer."
243   :type '(choice :format "%{%t%}: %[Value Menu%]\n%v"
244                  (regexp)
245                  (repeat :tag "List of regexp" regexp))
246   :group 'gnus-article-signature)
247
248 (defcustom gnus-signature-limit nil
249   "Provide a limit to what is considered a signature.
250 If it is a number, no signature may not be longer (in characters) than
251 that number.  If it is a floating point number, no signature may be
252 longer (in lines) than that number.  If it is a function, the function
253 will be called without any parameters, and if it returns nil, there is
254 no signature in the buffer.  If it is a string, it will be used as a
255 regexp.  If it matches, the text in question is not a signature.
256
257 This can also be a list of the above values."
258   :type '(choice (const nil)
259                  (integer :value 200)
260                  (number :value 4.0)
261                  function
262                  (regexp :value ".*"))
263   :group 'gnus-article-signature)
264
265 (defcustom gnus-hidden-properties '(invisible t intangible t)
266   "Property list to use for hiding text."
267   :type 'sexp
268   :group 'gnus-article-hiding)
269
270 ;; Fixme: This isn't the right thing for mixed graphical and non-graphical
271 ;; frames in a session.
272 (defcustom gnus-article-x-face-command
273   (if (featurep 'xemacs)
274       (if (or (gnus-image-type-available-p 'xface)
275               (gnus-image-type-available-p 'pbm))
276           'gnus-display-x-face-in-from
277         "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | ee -")
278     (if (gnus-image-type-available-p 'pbm)
279         'gnus-display-x-face-in-from
280       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
281 display -"))
282   "*String or function to be executed to display an X-Face header.
283 If it is a string, the command will be executed in a sub-shell
284 asynchronously.  The compressed face will be piped to this command."
285   :type `(choice string
286                  (function-item gnus-display-x-face-in-from)
287                  function)
288   :version "21.1"
289   :group 'gnus-picon
290   :group 'gnus-article-washing)
291
292 (defcustom gnus-article-x-face-too-ugly nil
293   "Regexp matching posters whose face shouldn't be shown automatically."
294   :type '(choice regexp (const nil))
295   :group 'gnus-article-washing)
296
297 (defcustom gnus-article-banner-alist nil
298   "Banner alist for stripping.
299 For example,
300      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
301   :version "21.1"
302   :type '(repeat (cons symbol regexp))
303   :group 'gnus-article-washing)
304
305 (gnus-define-group-parameter
306  banner
307  :variable-document
308  "Alist of regexps (to match group names) and banner."
309  :variable-group gnus-article-washing
310  :parameter-type
311  '(choice :tag "Banner"
312           :value nil
313           (const :tag "Remove signature" signature)
314           (symbol :tag "Item in `gnus-article-banner-alist'" none)
315           regexp
316           (const :tag "None" nil))
317  :parameter-document
318  "If non-nil, specify how to remove `banners' from articles.
319
320 Symbol `signature' means to remove signatures delimited by
321 `gnus-signature-separator'.  Any other symbol is used to look up a
322 regular expression to match the banner in `gnus-article-banner-alist'.
323 A string is used as a regular expression to match the banner
324 directly.")
325
326 (defcustom gnus-article-address-banner-alist nil
327   "Alist of mail addresses and banners.
328 Each element has the form (ADDRESS . BANNER), where ADDRESS is a regexp
329 to match a mail address in the From: header, BANNER is one of a symbol
330 `signature', an item in `gnus-article-banner-alist', a regexp and nil.
331 If ADDRESS matches author's mail address, it will remove things like
332 advertisements.  For example:
333
334 \((\"@yoo-hoo\\\\.co\\\\.jp\\\\'\" . \"\\n_+\\nDo You Yoo-hoo!\\\\?\\n.*\\n.*\\n\"))
335 "
336   :type '(repeat
337           (cons
338            (regexp :tag "Address")
339            (choice :tag "Banner" :value nil
340                    (const :tag "Remove signature" signature)
341                    (symbol :tag "Item in `gnus-article-banner-alist'" none)
342                    regexp
343                    (const :tag "None" nil))))
344   :version "22.1"
345   :group 'gnus-article-washing)
346
347 (defmacro gnus-emphasis-custom-with-format (&rest body)
348   `(let ((format "\
349 \\(\\s-\\|^\\|\\=\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\
350 \\(\\([-,.;:!?\"]\\|\\s)\\)+\\s-\\|[?!.]\\s-\\|\\s)\\|\\s-\\)"))
351      ,@body))
352
353 (defun gnus-emphasis-custom-value-to-external (value)
354   (gnus-emphasis-custom-with-format
355    (if (consp (car value))
356        (list (format format (car (car value)) (cdr (car value)))
357              2
358              (if (nth 1 value) 2 3)
359              (nth 2 value))
360      value)))
361
362 (defun gnus-emphasis-custom-value-to-internal (value)
363   (gnus-emphasis-custom-with-format
364    (let ((regexp (concat "\\`"
365                          (format (regexp-quote format)
366                                  "\\([^()]+\\)" "\\([^()]+\\)")
367                          "\\'"))
368          pattern)
369      (if (string-match regexp (setq pattern (car value)))
370          (list (cons (match-string 1 pattern) (match-string 2 pattern))
371                (= (nth 2 value) 2)
372                (nth 3 value))
373        value))))
374
375 (defcustom gnus-emphasis-alist
376   (let ((types
377          '(("\\*" "\\*" bold nil 2)
378            ("_" "_" underline)
379            ("/" "/" italic)
380            ("_/" "/_" underline-italic)
381            ("_\\*" "\\*_" underline-bold)
382            ("\\*/" "/\\*" bold-italic)
383            ("_\\*/" "/\\*_" underline-bold-italic))))
384     (nconc
385      (gnus-emphasis-custom-with-format
386       (mapcar (lambda (spec)
387                 (list (format format (car spec) (cadr spec))
388                       (or (nth 3 spec) 2)
389                       (or (nth 4 spec) 3)
390                       (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
391               types))
392      '(;; I've never seen anyone use this strikethru convention whereas I've
393        ;; several times seen it triggered by normal text.  --Stef
394        ;; Miles suggests that this form is sometimes used but for italics,
395        ;; so maybe we should map it to `italic'.
396        ;; ("\\(\\s-\\|^\\)\\(-\\(\\(\\w\\|-[^-]\\)+\\)-\\)\\(\\s-\\|[?!.,;]\\)"
397        ;; 2 3 gnus-emphasis-strikethru)
398        ("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
399         2 3 gnus-emphasis-underline))))
400   "*Alist that says how to fontify certain phrases.
401 Each item looks like this:
402
403   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
404
405 The first element is a regular expression to be matched.  The second
406 is a number that says what regular expression grouping used to find
407 the entire emphasized word.  The third is a number that says what
408 regexp grouping should be displayed and highlighted.  The fourth
409 is the face used for highlighting."
410   :type
411   '(repeat
412     (menu-choice
413      :format "%[Customizing Style%]\n%v"
414      :indent 2
415      (group :tag "Default"
416             :value ("" 0 0 default)
417             :value-create
418             (lambda (widget)
419               (let ((value (widget-get
420                             (cadr (widget-get (widget-get widget :parent)
421                                               :args))
422                             :value)))
423                 (if (not (eq (nth 2 value) 'default))
424                     (widget-put
425                      widget
426                      :value
427                      (gnus-emphasis-custom-value-to-external value))))
428               (widget-group-value-create widget))
429             regexp
430             (integer :format "Match group: %v")
431             (integer :format "Emphasize group: %v")
432             face)
433      (group :tag "Simple"
434             :value (("_" . "_") nil default)
435             (cons :format "%v"
436                   (regexp :format "Start regexp: %v")
437                   (regexp :format "End regexp: %v"))
438             (boolean :format "Show start and end patterns: %[%v%]\n"
439                      :on " On " :off " Off ")
440             face)))
441   :get (lambda (symbol)
442          (mapcar 'gnus-emphasis-custom-value-to-internal
443                  (default-value symbol)))
444   :set (lambda (symbol value)
445          (set-default symbol (mapcar 'gnus-emphasis-custom-value-to-external
446                                      value)))
447   :group 'gnus-article-emphasis)
448
449 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
450   "A regexp to describe whitespace which should not be emphasized.
451 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
452 The former avoids underlining of leading and trailing whitespace,
453 and the latter avoids underlining any whitespace at all."
454   :version "21.1"
455   :group 'gnus-article-emphasis
456   :type 'regexp)
457
458 (defface gnus-emphasis-bold '((t (:bold t)))
459   "Face used for displaying strong emphasized text (*word*)."
460   :group 'gnus-article-emphasis)
461
462 (defface gnus-emphasis-italic '((t (:italic t)))
463   "Face used for displaying italic emphasized text (/word/)."
464   :group 'gnus-article-emphasis)
465
466 (defface gnus-emphasis-underline '((t (:underline t)))
467   "Face used for displaying underlined emphasized text (_word_)."
468   :group 'gnus-article-emphasis)
469
470 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
471   "Face used for displaying underlined bold emphasized text (_*word*_)."
472   :group 'gnus-article-emphasis)
473
474 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
475   "Face used for displaying underlined italic emphasized text (_/word/_)."
476   :group 'gnus-article-emphasis)
477
478 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
479   "Face used for displaying bold italic emphasized text (/*word*/)."
480   :group 'gnus-article-emphasis)
481
482 (defface gnus-emphasis-underline-bold-italic
483   '((t (:bold t :italic t :underline t)))
484   "Face used for displaying underlined bold italic emphasized text.
485 Example: (_/*word*/_)."
486   :group 'gnus-article-emphasis)
487
488 (defface gnus-emphasis-strikethru (if (featurep 'xemacs)
489                                       '((t (:strikethru t)))
490                                     '((t (:strike-through t))))
491   "Face used for displaying strike-through text (-word-)."
492   :group 'gnus-article-emphasis)
493
494 (defface gnus-emphasis-highlight-words
495   '((t (:background "black" :foreground "yellow")))
496   "Face used for displaying highlighted words."
497   :group 'gnus-article-emphasis)
498
499 (defcustom gnus-article-time-format "%a, %d %b %Y %T %Z"
500   "Format for display of Date headers in article bodies.
501 See `format-time-string' for the possible values.
502
503 The variable can also be function, which should return a complete Date
504 header.  The function is called with one argument, the time, which can
505 be fed to `format-time-string'."
506   :type '(choice string function)
507   :link '(custom-manual "(gnus)Article Date")
508   :group 'gnus-article-washing)
509
510 (defcustom gnus-save-all-headers t
511   "*If non-nil, don't remove any headers before saving.
512 This will be overridden by the `:headers' property that the symbol of
513 the saver function, which is specified by `gnus-default-article-saver',
514 might have."
515   :group 'gnus-article-saving
516   :type 'boolean)
517
518 (defcustom gnus-prompt-before-saving 'always
519   "*This variable says how much prompting is to be done when saving articles.
520 If it is nil, no prompting will be done, and the articles will be
521 saved to the default files.  If this variable is `always', each and
522 every article that is saved will be preceded by a prompt, even when
523 saving large batches of articles.  If this variable is neither nil not
524 `always', there the user will be prompted once for a file name for
525 each invocation of the saving commands."
526   :group 'gnus-article-saving
527   :type '(choice (item always)
528                  (item :tag "never" nil)
529                  (sexp :tag "once" :format "%t\n" :value t)))
530
531 (defcustom gnus-saved-headers gnus-visible-headers
532   "Headers to keep if `gnus-save-all-headers' is nil.
533 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
534 If that variable is nil, however, all headers that match this regexp
535 will be kept while the rest will be deleted before saving.  This and
536 `gnus-save-all-headers' will be overridden by the `:headers' property
537 that the symbol of the saver function, which is specified by
538 `gnus-default-article-saver', might have."
539   :group 'gnus-article-saving
540   :type 'regexp)
541
542 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
543   "A function to save articles in your favourite format.
544 The function will be called by way of the `gnus-summary-save-article'
545 command, and friends such as `gnus-summary-save-article-rmail'.
546
547 Gnus provides the following functions:
548
549 * gnus-summary-save-in-rmail (Rmail format)
550 * gnus-summary-save-in-mail (Unix mail format)
551 * gnus-summary-save-in-folder (MH folder)
552 * gnus-summary-save-in-file (article format)
553 * gnus-summary-save-body-in-file (article body)
554 * gnus-summary-save-in-vm (use VM's folder format)
555 * gnus-summary-write-to-file (article format -- overwrite)
556 * gnus-summary-write-body-to-file (article body -- overwrite)
557
558 The symbol of each function may have the following properties:
559
560 * :decode
561 The value non-nil means save decoded articles.  This is meaningful
562 only with `gnus-summary-save-in-file', `gnus-summary-save-body-in-file',
563 `gnus-summary-write-to-file', and `gnus-summary-write-body-to-file'.
564
565 * :function
566 The value specifies an alternative function which appends, not
567 overwrites, articles to a file.  This implies that when saving many
568 articles at a time, `gnus-prompt-before-saving' is bound to t and all
569 articles are saved in a single file.  This is meaningful only with
570 `gnus-summary-write-to-file' and `gnus-summary-write-body-to-file'.
571
572 * :headers
573 The value specifies the symbol of a variable of which the value
574 specifies headers to be saved.  If it is omitted,
575 `gnus-save-all-headers' and `gnus-saved-headers' control what
576 headers should be saved."
577   :group 'gnus-article-saving
578   :type '(radio (function-item gnus-summary-save-in-rmail)
579                 (function-item gnus-summary-save-in-mail)
580                 (function-item gnus-summary-save-in-folder)
581                 (function-item gnus-summary-save-in-file)
582                 (function-item gnus-summary-save-body-in-file)
583                 (function-item gnus-summary-save-in-vm)
584                 (function-item gnus-summary-write-to-file)
585                 (function-item gnus-summary-write-body-to-file)
586                 (function)))
587
588 (defcustom gnus-article-save-coding-system
589   (or (and (mm-coding-system-p 'utf-8) 'utf-8)
590       (and (mm-coding-system-p 'iso-2022-7bit) 'iso-2022-7bit)
591       (and (mm-coding-system-p 'emacs-mule) 'emacs-mule)
592       (and (mm-coding-system-p 'escape-quoted) 'escape-quoted))
593   "Coding system used to save decoded articles to a file.
594
595 The recommended coding systems are `utf-8', `iso-2022-7bit' and so on,
596 which can safely encode any characters in text.  This is used by the
597 commands including:
598
599 * gnus-summary-save-article-file
600 * gnus-summary-save-article-body-file
601 * gnus-summary-write-article-file
602 * gnus-summary-write-article-body-file
603
604 and the functions to which you may set `gnus-default-article-saver':
605
606 * gnus-summary-save-in-file
607 * gnus-summary-save-body-in-file
608 * gnus-summary-write-to-file
609 * gnus-summary-write-body-to-file
610
611 Those commands and functions save just text displayed in the article
612 buffer to a file if the value of this variable is non-nil.  Note that
613 buttonized MIME parts will be lost in a saved file in that case.
614 Otherwise, raw articles will be saved."
615   :group 'gnus-article-saving
616   :type `(choice
617           :format "%{%t%}:\n %[Value Menu%] %v"
618           (const :tag "Save raw articles" nil)
619           ,@(delq nil
620                   (mapcar
621                    (lambda (arg) (if (mm-coding-system-p (nth 3 arg)) arg))
622                    '((const :tag "UTF-8" utf-8)
623                      (const :tag "iso-2022-7bit" iso-2022-7bit)
624                      (const :tag "Emacs internal" emacs-mule)
625                      (const :tag "escape-quoted" escape-quoted))))
626           (symbol :tag "Coding system")))
627
628 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
629   "A function generating a file name to save articles in Rmail format.
630 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
631   :group 'gnus-article-saving
632   :type 'function)
633
634 (defcustom gnus-mail-save-name 'gnus-plain-save-name
635   "A function generating a file name to save articles in Unix mail format.
636 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
637   :group 'gnus-article-saving
638   :type 'function)
639
640 (defcustom gnus-folder-save-name 'gnus-folder-save-name
641   "A function generating a file name to save articles in MH folder.
642 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
643   :group 'gnus-article-saving
644   :type 'function)
645
646 (defcustom gnus-file-save-name 'gnus-numeric-save-name
647   "A function generating a file name to save articles in article format.
648 The function is called with NEWSGROUP, HEADERS, and optional
649 LAST-FILE."
650   :group 'gnus-article-saving
651   :type 'function)
652
653 (defcustom gnus-split-methods
654   '((gnus-article-archive-name)
655     (gnus-article-nndoc-name))
656   "*Variable used to suggest where articles are to be saved.
657 For instance, if you would like to save articles related to Gnus in
658 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
659 you could set this variable to something like:
660
661  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
662    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
663
664 This variable is an alist where the key is the match and the
665 value is a list of possible files to save in if the match is
666 non-nil.
667
668 If the match is a string, it is used as a regexp match on the
669 article.  If the match is a symbol, that symbol will be funcalled
670 from the buffer of the article to be saved with the newsgroup as the
671 parameter.  If it is a list, it will be evaled in the same buffer.
672
673 If this form or function returns a string, this string will be used as a
674 possible file name; and if it returns a non-nil list, that list will be
675 used as possible file names."
676   :group 'gnus-article-saving
677   :type '(repeat (choice (list :value (fun) function)
678                          (cons :value ("" "") regexp (repeat string))
679                          (sexp :value nil))))
680
681 (defcustom gnus-page-delimiter "^\^L"
682   "*Regexp describing what to use as article page delimiters.
683 The default value is \"^\^L\", which is a form linefeed at the
684 beginning of a line."
685   :type 'regexp
686   :group 'gnus-article-various)
687
688 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
689   "*The format specification for the article mode line.
690 See `gnus-summary-mode-line-format' for a closer description.
691
692 The following additional specs are available:
693
694 %w  The article washing status.
695 %m  The number of MIME parts in the article."
696   :type 'string
697   :group 'gnus-article-various)
698
699 (defcustom gnus-article-mode-hook nil
700   "*A hook for Gnus article mode."
701   :type 'hook
702   :group 'gnus-article-various)
703
704 (when (featurep 'xemacs)
705   ;; Extracted from gnus-xmas-define in order to preserve user settings
706   (when (fboundp 'turn-off-scroll-in-place)
707     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
708   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
709   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
710
711 (defcustom gnus-article-menu-hook nil
712   "*Hook run after the creation of the article mode menu."
713   :type 'hook
714   :group 'gnus-article-various)
715
716 (defcustom gnus-article-prepare-hook nil
717   "*A hook called after an article has been prepared in the article buffer."
718   :type 'hook
719   :group 'gnus-article-various)
720
721 (defcustom gnus-copy-article-ignored-headers nil
722   "List of headers to be removed when copying an article.
723 Each element is a regular expression."
724   :version "23.1" ;; No Gnus
725   :type '(repeat regexp)
726   :group 'gnus-article-various)
727
728 (make-obsolete-variable 'gnus-article-hide-pgp-hook
729                         "This variable is obsolete in Gnus 5.10.")
730
731 (defface gnus-button
732   '((t (:weight bold)))
733   "Face used for highlighting a button in the article buffer."
734   :group 'gnus-article-buttons)
735
736 (defcustom gnus-article-button-face 'gnus-button
737   "Face used for highlighting buttons in the article buffer.
738
739 An article button is a piece of text that you can activate by pressing
740 `RET' or `mouse-2' above it."
741   :type 'face
742   :group 'gnus-article-buttons)
743
744 (defcustom gnus-article-mouse-face 'highlight
745   "Face used for mouse highlighting in the article buffer.
746
747 Article buttons will be displayed in this face when the cursor is
748 above them."
749   :type 'face
750   :group 'gnus-article-buttons)
751
752 (defcustom gnus-signature-face 'gnus-signature
753   "Face used for highlighting a signature in the article buffer.
754 Obsolete; use the face `gnus-signature' for customizations instead."
755   :type 'face
756   :group 'gnus-article-highlight
757   :group 'gnus-article-signature)
758
759 (defface gnus-signature
760   '((t
761      (:italic t)))
762   "Face used for highlighting a signature in the article buffer."
763   :group 'gnus-article-highlight
764   :group 'gnus-article-signature)
765 ;; backward-compatibility alias
766 (put 'gnus-signature-face 'face-alias 'gnus-signature)
767
768 (defface gnus-header-from
769   '((((class color)
770       (background dark))
771      (:foreground "PaleGreen1"))
772     (((class color)
773       (background light))
774      (:foreground "red3"))
775     (t
776      (:italic t)))
777   "Face used for displaying from headers."
778   :group 'gnus-article-headers
779   :group 'gnus-article-highlight)
780 ;; backward-compatibility alias
781 (put 'gnus-header-from-face 'face-alias 'gnus-header-from)
782
783 (defface gnus-header-subject
784   '((((class color)
785       (background dark))
786      (:foreground "SeaGreen1"))
787     (((class color)
788       (background light))
789      (:foreground "red4"))
790     (t
791      (:bold t :italic t)))
792   "Face used for displaying subject headers."
793   :group 'gnus-article-headers
794   :group 'gnus-article-highlight)
795 ;; backward-compatibility alias
796 (put 'gnus-header-subject-face 'face-alias 'gnus-header-subject)
797
798 (defface gnus-header-newsgroups
799   '((((class color)
800       (background dark))
801      (:foreground "yellow" :italic t))
802     (((class color)
803       (background light))
804      (:foreground "MidnightBlue" :italic t))
805     (t
806      (:italic t)))
807   "Face used for displaying newsgroups headers.
808 In the default setup this face is only used for crossposted
809 articles."
810   :group 'gnus-article-headers
811   :group 'gnus-article-highlight)
812 ;; backward-compatibility alias
813 (put 'gnus-header-newsgroups-face 'face-alias 'gnus-header-newsgroups)
814
815 (defface gnus-header-name
816   '((((class color)
817       (background dark))
818      (:foreground "SpringGreen2"))
819     (((class color)
820       (background light))
821      (:foreground "maroon"))
822     (t
823      (:bold t)))
824   "Face used for displaying header names."
825   :group 'gnus-article-headers
826   :group 'gnus-article-highlight)
827 ;; backward-compatibility alias
828 (put 'gnus-header-name-face 'face-alias 'gnus-header-name)
829
830 (defface gnus-header-content
831   '((((class color)
832       (background dark))
833      (:foreground "SpringGreen1" :italic t))
834     (((class color)
835       (background light))
836      (:foreground "indianred4" :italic t))
837     (t
838      (:italic t)))  "Face used for displaying header content."
839   :group 'gnus-article-headers
840   :group 'gnus-article-highlight)
841 ;; backward-compatibility alias
842 (put 'gnus-header-content-face 'face-alias 'gnus-header-content)
843
844 (defcustom gnus-header-face-alist
845   '(("From" nil gnus-header-from)
846     ("Subject" nil gnus-header-subject)
847     ("Newsgroups:.*," nil gnus-header-newsgroups)
848     ("" gnus-header-name gnus-header-content))
849   "*Controls highlighting of article headers.
850
851 An alist of the form (HEADER NAME CONTENT).
852
853 HEADER is a regular expression which should match the name of a
854 header and NAME and CONTENT are either face names or nil.
855
856 The name of each header field will be displayed using the face
857 specified by the first element in the list where HEADER matches
858 the header name and NAME is non-nil.  Similarly, the content will
859 be displayed by the first non-nil matching CONTENT face."
860   :group 'gnus-article-headers
861   :group 'gnus-article-highlight
862   :type '(repeat (list (regexp :tag "Header")
863                        (choice :tag "Name"
864                                (item :tag "skip" nil)
865                                (face :value default))
866                        (choice :tag "Content"
867                                (item :tag "skip" nil)
868                                (face :value default)))))
869
870 (defcustom gnus-face-properties-alist (if (featurep 'xemacs)
871                                           '((xface . (:face gnus-x-face)))
872                                         '((pbm . (:face gnus-x-face))
873                                           (png . nil)))
874   "Alist of image types and properties applied to Face and X-Face images.
875 Here are examples:
876
877 ;; Specify the altitude of Face images in the From header.
878 \(setq gnus-face-properties-alist
879       '((pbm . (:face gnus-x-face :ascent 80))
880         (png . (:ascent 80))))
881
882 ;; Show Face images as pressed buttons.
883 \(setq gnus-face-properties-alist
884       '((pbm . (:face gnus-x-face :relief -2))
885         (png . (:relief -2))))
886
887 See the manual for the valid properties for various image types.
888 Currently, `pbm' is used for X-Face images and `png' is used for Face
889 images in Emacs.  Only the `:face' property is effective on the `xface'
890 image type in XEmacs if it is built with the libcompface library."
891   :version "23.1" ;; No Gnus
892   :group 'gnus-article-headers
893   :type '(repeat (cons :format "%v" (symbol :tag "Image type") plist)))
894
895 (defcustom gnus-article-decode-hook
896   '(article-decode-charset article-decode-encoded-words
897                            article-decode-group-name article-decode-idna-rhs)
898   "*Hook run to decode charsets in articles."
899   :group 'gnus-article-headers
900   :type 'hook)
901
902 (defcustom gnus-display-mime-function 'gnus-display-mime
903   "Function to display MIME articles."
904   :group 'gnus-article-mime
905   :type 'function)
906
907 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
908   "Function used to decode headers.")
909
910 (defvar gnus-decode-address-function 'mail-decode-encoded-address-region
911   "Function used to decode addresses.")
912
913 (defvar gnus-article-dumbquotes-map
914   '(("\200" "EUR")
915     ("\202" ",")
916     ("\203" "f")
917     ("\204" ",,")
918     ("\205" "...")
919     ("\213" "<")
920     ("\214" "OE")
921     ("\221" "`")
922     ("\222" "'")
923     ("\223" "``")
924     ("\224" "\"")
925     ("\225" "*")
926     ("\226" "-")
927     ("\227" "--")
928     ("\230" "~")
929     ("\231" "(TM)")
930     ("\233" ">")
931     ("\234" "oe")
932     ("\264" "'"))
933   "Table for MS-to-Latin1 translation.")
934
935 (defcustom gnus-ignored-mime-types nil
936   "List of MIME types that should be ignored by Gnus."
937   :version "21.1"
938   :group 'gnus-article-mime
939   :type '(repeat regexp))
940
941 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
942   "List of MIME types that should not be given buttons when rendered inline.
943 See also `gnus-buttonized-mime-types' which may override this variable.
944 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
945   :version "21.1"
946   :group 'gnus-article-mime
947   :type '(repeat regexp))
948
949 (defcustom gnus-buttonized-mime-types nil
950   "List of MIME types that should be given buttons when rendered inline.
951 If set, this variable overrides `gnus-unbuttonized-mime-types'.
952 To see e.g. security buttons you could set this to
953 `(\"multipart/signed\")'.  You could also add \"multipart/alternative\" to
954 this list to display radio buttons that allow you to choose one of two
955 media types those mails include.  See also `mm-discouraged-alternatives'.
956 This variable is only used when `gnus-inhibit-mime-unbuttonizing' is nil."
957   :version "22.1"
958   :group 'gnus-article-mime
959   :type '(repeat regexp))
960
961 (defcustom gnus-inhibit-mime-unbuttonizing nil
962   "If non-nil, all MIME parts get buttons.
963 When nil (the default value), then some MIME parts do not get buttons,
964 as described by the variables `gnus-buttonized-mime-types' and
965 `gnus-unbuttonized-mime-types'."
966   :version "22.1"
967   :group 'gnus-article-mime
968   :type 'boolean)
969
970 (defcustom gnus-body-boundary-delimiter "_"
971   "String used to delimit header and body.
972 This variable is used by `gnus-article-treat-body-boundary' which can
973 be controlled by `gnus-treat-body-boundary'."
974   :version "22.1"
975   :group 'gnus-article-various
976   :type '(choice (item :tag "None" :value nil)
977                  string))
978
979 (defcustom gnus-picon-databases '("/usr/lib/picon" "/usr/local/faces"
980                                   "/usr/share/picons")
981   "Defines the location of the faces database.
982 For information on obtaining this database of pretty pictures, please
983 see http://www.cs.indiana.edu/picons/ftp/index.html"
984   :version "22.1"
985   :type '(repeat directory)
986   :link '(url-link :tag "download"
987                    "http://www.cs.indiana.edu/picons/ftp/index.html")
988   :link '(custom-manual "(gnus)Picons")
989   :group 'gnus-picon)
990
991 (defun gnus-picons-installed-p ()
992   "Say whether picons are installed on your machine."
993   (let ((installed nil))
994     (dolist (database gnus-picon-databases)
995       (when (file-exists-p database)
996         (setq installed t)))
997     installed))
998
999 (defcustom gnus-article-mime-part-function nil
1000   "Function called with a MIME handle as the argument.
1001 This is meant for people who want to do something automatic based
1002 on parts -- for instance, adding Vcard info to a database."
1003   :group 'gnus-article-mime
1004   :type '(choice (const nil)
1005                  function))
1006
1007 (defcustom gnus-mime-multipart-functions nil
1008   "An alist of MIME types to functions to display them."
1009   :version "21.1"
1010   :group 'gnus-article-mime
1011   :type '(repeat (cons :format "%v" (string :tag "MIME type") function)))
1012
1013 (defcustom gnus-article-date-lapsed-new-header nil
1014   "Whether the X-Sent and Date headers can coexist.
1015 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
1016 either replace the old \"Date:\" header (if this variable is nil), or
1017 be added below it (otherwise)."
1018   :version "21.1"
1019   :group 'gnus-article-headers
1020   :type 'boolean)
1021
1022 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
1023   "Function called with a MIME handle as the argument.
1024 This is meant for people who want to view first matched part.
1025 For `undisplayed-alternative' (default), the first undisplayed
1026 part or alternative part is used.  For `undisplayed', the first
1027 undisplayed part is used.  For a function, the first part which
1028 the function return t is used.  For nil, the first part is
1029 used."
1030   :version "21.1"
1031   :group 'gnus-article-mime
1032   :type '(choice
1033           (item :tag "first" :value nil)
1034           (item :tag "undisplayed" :value undisplayed)
1035           (item :tag "undisplayed or alternative"
1036                 :value undisplayed-alternative)
1037           (function)))
1038
1039 (defcustom gnus-mime-action-alist
1040   '(("save to file" . gnus-mime-save-part)
1041     ("save and strip" . gnus-mime-save-part-and-strip)
1042     ("replace with file" . gnus-mime-replace-part)
1043     ("delete part" . gnus-mime-delete-part)
1044     ("display as text" . gnus-mime-inline-part)
1045     ("view the part" . gnus-mime-view-part)
1046     ("pipe to command" . gnus-mime-pipe-part)
1047     ("toggle display" . gnus-article-press-button)
1048     ("toggle display" . gnus-article-view-part-as-charset)
1049     ("view as type" . gnus-mime-view-part-as-type)
1050     ("view internally" . gnus-mime-view-part-internally)
1051     ("view externally" . gnus-mime-view-part-externally))
1052   "An alist of actions that run on the MIME attachment."
1053   :group 'gnus-article-mime
1054   :type '(repeat (cons (string :tag "name")
1055                        (function))))
1056
1057 (defcustom gnus-auto-select-part 1
1058   "Advance to next MIME part when deleting or stripping parts.
1059
1060 When 0, point will be placed on the same part as before.  When
1061 positive (negative), move point forward (backwards) this many
1062 parts.  When nil, redisplay article."
1063   :version "23.1" ;; No Gnus
1064   :group 'gnus-article-mime
1065   :type '(choice (const nil :tag "Redisplay article.")
1066                  (const 1 :tag "Next part.")
1067                  (const 0 :tag "Current part.")
1068                  integer))
1069
1070 ;;;
1071 ;;; The treatment variables
1072 ;;;
1073
1074 (defvar gnus-part-display-hook nil
1075   "Hook called on parts that are to receive treatment.")
1076
1077 (defvar gnus-article-treat-custom
1078   '(choice (const :tag "Off" nil)
1079            (const :tag "On" t)
1080            (const :tag "Header" head)
1081            (const :tag "First" first)
1082            (const :tag "Last" last)
1083            (integer :tag "Less")
1084            (repeat :tag "Groups" regexp)
1085            (sexp :tag "Predicate")))
1086
1087 (defvar gnus-article-treat-head-custom
1088   '(choice (const :tag "Off" nil)
1089            (const :tag "Header" head)))
1090
1091 (defvar gnus-article-treat-types '("text/plain" "text/x-verbatim"
1092                                    "text/x-patch")
1093   "Parts to treat.")
1094
1095 (defvar gnus-inhibit-treatment nil
1096   "Whether to inhibit treatment.")
1097
1098 (defcustom gnus-treat-highlight-signature '(or t (typep "text/x-vcard"))
1099   "Highlight the signature.
1100 Valid values are nil, t, `head', `first', `last', an integer or a
1101 predicate.  See Info node `(gnus)Customizing Articles'."
1102   :group 'gnus-article-treat
1103   :link '(custom-manual "(gnus)Customizing Articles")
1104   :type gnus-article-treat-custom)
1105 (put 'gnus-treat-highlight-signature 'highlight t)
1106
1107 (defcustom gnus-treat-buttonize 100000
1108   "Add buttons.
1109 Valid values are nil, t, `head', `first', `last', an integer or a
1110 predicate.  See Info node `(gnus)Customizing Articles'."
1111   :group 'gnus-article-treat
1112   :link '(custom-manual "(gnus)Customizing Articles")
1113   :type gnus-article-treat-custom)
1114 (put 'gnus-treat-buttonize 'highlight t)
1115
1116 (defcustom gnus-treat-buttonize-head 'head
1117   "Add buttons to the head.
1118 Valid values are nil, t, `head', `first', `last', an integer or a
1119 predicate.  See Info node `(gnus)Customizing Articles'."
1120   :group 'gnus-article-treat
1121   :link '(custom-manual "(gnus)Customizing Articles")
1122   :type gnus-article-treat-head-custom)
1123 (put 'gnus-treat-buttonize-head 'highlight t)
1124
1125 (defcustom gnus-treat-emphasize 50000
1126   "Emphasize text.
1127 Valid values are nil, t, `head', `first', `last', an integer or a
1128 predicate.  See Info node `(gnus)Customizing Articles'."
1129   :group 'gnus-article-treat
1130   :link '(custom-manual "(gnus)Customizing Articles")
1131   :type gnus-article-treat-custom)
1132 (put 'gnus-treat-emphasize 'highlight t)
1133
1134 (defcustom gnus-treat-strip-cr nil
1135   "Remove carriage returns.
1136 Valid values are nil, t, `head', `first', `last', an integer or a
1137 predicate.  See Info node `(gnus)Customizing Articles'."
1138   :version "22.1"
1139   :group 'gnus-article-treat
1140   :link '(custom-manual "(gnus)Customizing Articles")
1141   :type gnus-article-treat-custom)
1142
1143 (defcustom gnus-treat-unsplit-urls nil
1144   "Remove newlines from within URLs.
1145 Valid values are nil, t, `head', `first', `last', an integer or a
1146 predicate.  See Info node `(gnus)Customizing Articles'."
1147   :version "22.1"
1148   :group 'gnus-article-treat
1149   :link '(custom-manual "(gnus)Customizing Articles")
1150   :type gnus-article-treat-custom)
1151
1152 (defcustom gnus-treat-leading-whitespace nil
1153   "Remove leading whitespace in headers.
1154 Valid values are nil, t, `head', `first', `last', an integer or a
1155 predicate.  See Info node `(gnus)Customizing Articles'."
1156   :version "22.1"
1157   :group 'gnus-article-treat
1158   :link '(custom-manual "(gnus)Customizing Articles")
1159   :type gnus-article-treat-custom)
1160
1161 (defcustom gnus-treat-hide-headers 'head
1162   "Hide headers.
1163 Valid values are nil, t, `head', `first', `last', an integer or a
1164 predicate.  See Info node `(gnus)Customizing Articles'."
1165   :group 'gnus-article-treat
1166   :link '(custom-manual "(gnus)Customizing Articles")
1167   :type gnus-article-treat-head-custom)
1168
1169 (defcustom gnus-treat-hide-boring-headers nil
1170   "Hide boring headers.
1171 Valid values are nil, t, `head', `first', `last', an integer or a
1172 predicate.  See Info node `(gnus)Customizing Articles'."
1173   :group 'gnus-article-treat
1174   :link '(custom-manual "(gnus)Customizing Articles")
1175   :type gnus-article-treat-head-custom)
1176
1177 (defcustom gnus-treat-hide-signature nil
1178   "Hide the signature.
1179 Valid values are nil, t, `head', `first', `last', an integer or a
1180 predicate.  See Info node `(gnus)Customizing Articles'."
1181   :group 'gnus-article-treat
1182   :link '(custom-manual "(gnus)Customizing Articles")
1183   :type gnus-article-treat-custom)
1184
1185 (defcustom gnus-treat-fill-article nil
1186   "Fill the article.
1187 Valid values are nil, t, `head', `first', `last', an integer or a
1188 predicate.  See Info node `(gnus)Customizing Articles'."
1189   :group 'gnus-article-treat
1190   :link '(custom-manual "(gnus)Customizing Articles")
1191   :type gnus-article-treat-custom)
1192
1193 (defcustom gnus-treat-hide-citation nil
1194   "Hide cited text.
1195 Valid values are nil, t, `head', `first', `last', an integer or a
1196 predicate.  See Info node `(gnus)Customizing Articles'."
1197   :group 'gnus-article-treat
1198   :link '(custom-manual "(gnus)Customizing Articles")
1199   :type gnus-article-treat-custom)
1200
1201 (defcustom gnus-treat-hide-citation-maybe nil
1202   "Hide cited text.
1203 Valid values are nil, t, `head', `first', `last', an integer or a
1204 predicate.  See Info node `(gnus)Customizing Articles'."
1205   :group 'gnus-article-treat
1206   :link '(custom-manual "(gnus)Customizing Articles")
1207   :type gnus-article-treat-custom)
1208
1209 (defcustom gnus-treat-strip-list-identifiers 'head
1210   "Strip list identifiers from `gnus-list-identifiers`.
1211 Valid values are nil, t, `head', `first', `last', an integer or a
1212 predicate.  See Info node `(gnus)Customizing Articles'."
1213   :version "21.1"
1214   :group 'gnus-article-treat
1215   :link '(custom-manual "(gnus)Customizing Articles")
1216   :type gnus-article-treat-custom)
1217
1218 (make-obsolete-variable 'gnus-treat-strip-pgp
1219                         "This option is obsolete in Gnus 5.10.")
1220
1221 (defcustom gnus-treat-strip-pem nil
1222   "Strip PEM signatures.
1223 Valid values are nil, t, `head', `first', `last', an integer or a
1224 predicate.  See Info node `(gnus)Customizing Articles'."
1225   :group 'gnus-article-treat
1226   :link '(custom-manual "(gnus)Customizing Articles")
1227   :type gnus-article-treat-custom)
1228
1229 (defcustom gnus-treat-strip-banner t
1230   "Strip banners from articles.
1231 The banner to be stripped is specified in the `banner' group parameter.
1232 Valid values are nil, t, `head', `first', `last', an integer or a
1233 predicate.  See Info node `(gnus)Customizing Articles'."
1234   :group 'gnus-article-treat
1235   :link '(custom-manual "(gnus)Customizing Articles")
1236   :type gnus-article-treat-custom)
1237
1238 (defcustom gnus-treat-highlight-headers 'head
1239   "Highlight the headers.
1240 Valid values are nil, t, `head', `first', `last', an integer or a
1241 predicate.  See Info node `(gnus)Customizing Articles'."
1242   :group 'gnus-article-treat
1243   :link '(custom-manual "(gnus)Customizing Articles")
1244   :type gnus-article-treat-head-custom)
1245 (put 'gnus-treat-highlight-headers 'highlight t)
1246
1247 (defcustom gnus-treat-highlight-citation t
1248   "Highlight cited text.
1249 Valid values are nil, t, `head', `first', `last', an integer or a
1250 predicate.  See Info node `(gnus)Customizing Articles'."
1251   :group 'gnus-article-treat
1252   :link '(custom-manual "(gnus)Customizing Articles")
1253   :type gnus-article-treat-custom)
1254 (put 'gnus-treat-highlight-citation 'highlight t)
1255
1256 (defcustom gnus-treat-date-ut nil
1257   "Display the Date in UT (GMT).
1258 Valid values are nil, t, `head', `first', `last', an integer or a
1259 predicate.  See Info node `(gnus)Customizing Articles'."
1260   :group 'gnus-article-treat
1261   :link '(custom-manual "(gnus)Customizing Articles")
1262   :type gnus-article-treat-head-custom)
1263
1264 (defcustom gnus-treat-date-local nil
1265   "Display the Date in the local timezone.
1266 Valid values are nil, t, `head', `first', `last', an integer or a
1267 predicate.  See Info node `(gnus)Customizing Articles'."
1268   :group 'gnus-article-treat
1269   :link '(custom-manual "(gnus)Customizing Articles")
1270   :type gnus-article-treat-head-custom)
1271
1272 (defcustom gnus-treat-date-english nil
1273   "Display the Date in a format that can be read aloud in English.
1274 Valid values are nil, t, `head', `first', `last', an integer or a
1275 predicate.  See Info node `(gnus)Customizing Articles'."
1276   :version "22.1"
1277   :group 'gnus-article-treat
1278   :link '(custom-manual "(gnus)Customizing Articles")
1279   :type gnus-article-treat-head-custom)
1280
1281 (defcustom gnus-treat-date-lapsed nil
1282   "Display the Date header in a way that says how much time has elapsed.
1283 Valid values are nil, t, `head', `first', `last', an integer or a
1284 predicate.  See Info node `(gnus)Customizing Articles'."
1285   :group 'gnus-article-treat
1286   :link '(custom-manual "(gnus)Customizing Articles")
1287   :type gnus-article-treat-head-custom)
1288
1289 (defcustom gnus-treat-date-original nil
1290   "Display the date in the original timezone.
1291 Valid values are nil, t, `head', `first', `last', an integer or a
1292 predicate.  See Info node `(gnus)Customizing Articles'."
1293   :group 'gnus-article-treat
1294   :link '(custom-manual "(gnus)Customizing Articles")
1295   :type gnus-article-treat-head-custom)
1296
1297 (defcustom gnus-treat-date-iso8601 nil
1298   "Display the date in the ISO8601 format.
1299 Valid values are nil, t, `head', `first', `last', an integer or a
1300 predicate.  See Info node `(gnus)Customizing Articles'."
1301   :version "21.1"
1302   :group 'gnus-article-treat
1303   :link '(custom-manual "(gnus)Customizing Articles")
1304   :type gnus-article-treat-head-custom)
1305
1306 (defcustom gnus-treat-date-user-defined nil
1307   "Display the date in a user-defined format.
1308 The format is defined by the `gnus-article-time-format' variable.
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-head-custom)
1314
1315 (defcustom gnus-treat-strip-headers-in-body t
1316   "Strip the X-No-Archive header line from the beginning of the body.
1317 Valid values are nil, t, `head', `first', `last', an integer or a
1318 predicate.  See Info node `(gnus)Customizing Articles'."
1319   :version "21.1"
1320   :group 'gnus-article-treat
1321   :link '(custom-manual "(gnus)Customizing Articles")
1322   :type gnus-article-treat-custom)
1323
1324 (defcustom gnus-treat-strip-trailing-blank-lines nil
1325   "Strip trailing blank lines.
1326 Valid values are nil, t, `head', `first', `last', an integer or a
1327 predicate.  See Info node `(gnus)Customizing Articles'.
1328
1329 When set to t, it also strips trailing blanks in all MIME parts.
1330 Consider to use `last' instead."
1331   :group 'gnus-article-treat
1332   :link '(custom-manual "(gnus)Customizing Articles")
1333   :type gnus-article-treat-custom)
1334
1335 (defcustom gnus-treat-strip-leading-blank-lines nil
1336   "Strip leading blank lines.
1337 Valid values are nil, t, `head', `first', `last', an integer or a
1338 predicate.  See Info node `(gnus)Customizing Articles'.
1339
1340 When set to t, it also strips trailing blanks in all MIME parts."
1341   :group 'gnus-article-treat
1342   :link '(custom-manual "(gnus)Customizing Articles")
1343   :type gnus-article-treat-custom)
1344
1345 (defcustom gnus-treat-strip-multiple-blank-lines nil
1346   "Strip multiple blank lines.
1347 Valid values are nil, t, `head', `first', `last', an integer or a
1348 predicate.  See Info node `(gnus)Customizing Articles'."
1349   :group 'gnus-article-treat
1350   :link '(custom-manual "(gnus)Customizing Articles")
1351   :type gnus-article-treat-custom)
1352
1353 (defcustom gnus-treat-unfold-headers 'head
1354   "Unfold folded header lines.
1355 Valid values are nil, t, `head', `first', `last', an integer or a
1356 predicate.  See Info node `(gnus)Customizing Articles'."
1357   :version "22.1"
1358   :group 'gnus-article-treat
1359   :link '(custom-manual "(gnus)Customizing Articles")
1360   :type gnus-article-treat-custom)
1361
1362 (defcustom gnus-article-unfold-long-headers nil
1363   "If non-nil, allow unfolding headers even if the header is long.
1364 If it is a regexp, only long headers matching this regexp are unfolded.
1365 If it is t, all long headers are unfolded.
1366
1367 This variable has no effect if `gnus-treat-unfold-headers' is nil."
1368   :version "23.1" ;; No Gnus
1369   :group 'gnus-article-treat
1370   :type '(choice (const nil)
1371                  (const :tag "all" t)
1372                  (regexp)))
1373
1374 (defcustom gnus-treat-fold-headers nil
1375   "Fold headers.
1376 Valid values are nil, t, `head', `first', `last', an integer or a
1377 predicate.  See Info node `(gnus)Customizing Articles'."
1378   :version "22.1"
1379   :group 'gnus-article-treat
1380   :link '(custom-manual "(gnus)Customizing Articles")
1381   :type gnus-article-treat-custom)
1382
1383 (defcustom gnus-treat-fold-newsgroups 'head
1384   "Fold the Newsgroups and Followup-To headers.
1385 Valid values are nil, t, `head', `first', `last', an integer or a
1386 predicate.  See Info node `(gnus)Customizing Articles'."
1387   :version "22.1"
1388   :group 'gnus-article-treat
1389   :link '(custom-manual "(gnus)Customizing Articles")
1390   :type gnus-article-treat-custom)
1391
1392 (defcustom gnus-treat-overstrike t
1393   "Treat overstrike highlighting.
1394 Valid values are nil, t, `head', `first', `last', an integer or a
1395 predicate.  See Info node `(gnus)Customizing Articles'."
1396   :group 'gnus-article-treat
1397   :link '(custom-manual "(gnus)Customizing Articles")
1398   :type gnus-article-treat-custom)
1399 (put 'gnus-treat-overstrike 'highlight t)
1400
1401 (defcustom gnus-treat-ansi-sequences (if (locate-library "ansi-color") t)
1402   "Treat ANSI SGR control sequences.
1403 Valid values are nil, t, `head', `first', `last', an integer or a
1404 predicate.  See Info node `(gnus)Customizing Articles'."
1405   :group 'gnus-article-treat
1406   :link '(custom-manual "(gnus)Customizing Articles")
1407   :type gnus-article-treat-custom)
1408
1409 (make-obsolete-variable 'gnus-treat-display-xface
1410                         'gnus-treat-display-x-face)
1411
1412 (defcustom gnus-treat-display-x-face
1413   (and (not noninteractive)
1414        (gnus-image-type-available-p 'xbm)
1415        (if (featurep 'xemacs)
1416            (featurep 'xface)
1417          (and (string-match "^0x" (shell-command-to-string "uncompface"))
1418               (executable-find "icontopbm")))
1419        'head)
1420   "Display X-Face headers.
1421 Valid values are nil and `head'.
1422 See Info node `(gnus)Customizing Articles' and Info node
1423 `(gnus)X-Face' for details."
1424   :group 'gnus-article-treat
1425   :version "21.1"
1426   :link '(custom-manual "(gnus)Customizing Articles")
1427   :link '(custom-manual "(gnus)X-Face")
1428   :type gnus-article-treat-head-custom
1429   :set (lambda (symbol value)
1430          (set-default
1431           symbol
1432           (cond ((or (boundp symbol) (get symbol 'saved-value))
1433                  value)
1434                 ((boundp 'gnus-treat-display-xface)
1435                  (message "\
1436 ** gnus-treat-display-xface is an obsolete variable;\
1437  use gnus-treat-display-x-face instead")
1438                  (default-value 'gnus-treat-display-xface))
1439                 ((get 'gnus-treat-display-xface 'saved-value)
1440                  (message "\
1441 ** gnus-treat-display-xface is an obsolete variable;\
1442  use gnus-treat-display-x-face instead")
1443                  (eval (car (get 'gnus-treat-display-xface 'saved-value))))
1444                 (t
1445                  value)))))
1446 (put 'gnus-treat-display-x-face 'highlight t)
1447
1448 (defcustom gnus-treat-display-face
1449   (and (not noninteractive)
1450        (gnus-image-type-available-p 'png)
1451        'head)
1452   "Display Face headers.
1453 Valid values are nil, t, `head', `first', `last', an integer or a
1454 predicate.  See Info node `(gnus)Customizing Articles' and Info
1455 node `(gnus)X-Face' for details."
1456   :group 'gnus-article-treat
1457   :version "22.1"
1458   :link '(custom-manual "(gnus)Customizing Articles")
1459   :link '(custom-manual "(gnus)X-Face")
1460   :type gnus-article-treat-head-custom)
1461 (put 'gnus-treat-display-face 'highlight t)
1462
1463 (defcustom gnus-treat-display-smileys (gnus-image-type-available-p 'xpm)
1464   "Display smileys.
1465 Valid values are nil, t, `head', `first', `last', an integer or a
1466 predicate.  See Info node `(gnus)Customizing Articles' and Info
1467 node `(gnus)Smileys' for details."
1468   :group 'gnus-article-treat
1469   :version "21.1"
1470   :link '(custom-manual "(gnus)Customizing Articles")
1471   :link '(custom-manual "(gnus)Smileys")
1472   :type gnus-article-treat-custom)
1473 (put 'gnus-treat-display-smileys 'highlight t)
1474
1475 (defcustom gnus-treat-from-picon
1476   (if (and (gnus-image-type-available-p 'xpm)
1477            (gnus-picons-installed-p))
1478       'head nil)
1479   "Display picons in the From header.
1480 Valid values are nil, t, `head', `first', `last', an integer or a
1481 predicate.  See Info node `(gnus)Customizing Articles' and Info
1482 node `(gnus)Picons' for details."
1483   :version "22.1"
1484   :group 'gnus-article-treat
1485   :group 'gnus-picon
1486   :link '(custom-manual "(gnus)Customizing Articles")
1487   :link '(custom-manual "(gnus)Picons")
1488   :type gnus-article-treat-head-custom)
1489 (put 'gnus-treat-from-picon 'highlight t)
1490
1491 (defcustom gnus-treat-mail-picon
1492   (if (and (gnus-image-type-available-p 'xpm)
1493            (gnus-picons-installed-p))
1494       'head nil)
1495   "Display picons in To and Cc headers.
1496 Valid values are nil, t, `head', `first', `last', an integer or a
1497 predicate.  See Info node `(gnus)Customizing Articles' and Info
1498 node `(gnus)Picons' for details."
1499   :version "22.1"
1500   :group 'gnus-article-treat
1501   :group 'gnus-picon
1502   :link '(custom-manual "(gnus)Customizing Articles")
1503   :link '(custom-manual "(gnus)Picons")
1504   :type gnus-article-treat-head-custom)
1505 (put 'gnus-treat-mail-picon 'highlight t)
1506
1507 (defcustom gnus-treat-newsgroups-picon
1508   (if (and (gnus-image-type-available-p 'xpm)
1509            (gnus-picons-installed-p))
1510       'head nil)
1511   "Display picons in the Newsgroups and Followup-To headers.
1512 Valid values are nil, t, `head', `first', `last', an integer or a
1513 predicate.  See Info node `(gnus)Customizing Articles' and Info
1514 node `(gnus)Picons' for details."
1515   :version "22.1"
1516   :group 'gnus-article-treat
1517   :group 'gnus-picon
1518   :link '(custom-manual "(gnus)Customizing Articles")
1519   :link '(custom-manual "(gnus)Picons")
1520   :type gnus-article-treat-head-custom)
1521 (put 'gnus-treat-newsgroups-picon 'highlight t)
1522
1523 (defcustom gnus-treat-body-boundary
1524   (if (or gnus-treat-newsgroups-picon
1525           gnus-treat-mail-picon
1526           gnus-treat-from-picon)
1527       ;; If there's much decoration, the user might prefer a boundery.
1528       'head
1529     nil)
1530   "Draw a boundary at the end of the headers.
1531 Valid values are nil and `head'.
1532 See Info node `(gnus)Customizing Articles' for details."
1533   :version "22.1"
1534   :group 'gnus-article-treat
1535   :link '(custom-manual "(gnus)Customizing Articles")
1536   :type gnus-article-treat-head-custom)
1537
1538 (defcustom gnus-treat-capitalize-sentences nil
1539   "Capitalize sentence-starting words.
1540 Valid values are nil, t, `head', `first', `last', an integer or a
1541 predicate.  See Info node `(gnus)Customizing Articles'."
1542   :version "21.1"
1543   :group 'gnus-article-treat
1544   :link '(custom-manual "(gnus)Customizing Articles")
1545   :type gnus-article-treat-custom)
1546
1547 (defcustom gnus-treat-wash-html nil
1548   "Format as HTML.
1549 Valid values are nil, t, `head', `first', `last', an integer or a
1550 predicate.  See Info node `(gnus)Customizing Articles'."
1551   :version "22.1"
1552   :group 'gnus-article-treat
1553   :link '(custom-manual "(gnus)Customizing Articles")
1554   :type gnus-article-treat-custom)
1555
1556 (defcustom gnus-treat-fill-long-lines nil
1557   "Fill long lines.
1558 Valid values are nil, t, `head', `first', `last', an integer or a
1559 predicate.  See Info node `(gnus)Customizing Articles'."
1560   :group 'gnus-article-treat
1561   :link '(custom-manual "(gnus)Customizing Articles")
1562   :type gnus-article-treat-custom)
1563
1564 (defcustom gnus-treat-play-sounds nil
1565   "Play sounds.
1566 Valid values are nil, t, `head', `first', `last', an integer or a
1567 predicate.  See Info node `(gnus)Customizing Articles'."
1568   :version "21.1"
1569   :group 'gnus-article-treat
1570   :link '(custom-manual "(gnus)Customizing Articles")
1571   :type gnus-article-treat-custom)
1572
1573 (defcustom gnus-treat-translate nil
1574   "Translate articles from one language to another.
1575 Valid values are nil, t, `head', `first', `last', an integer or a
1576 predicate.  See Info node `(gnus)Customizing Articles'."
1577   :version "21.1"
1578   :group 'gnus-article-treat
1579   :link '(custom-manual "(gnus)Customizing Articles")
1580   :type gnus-article-treat-custom)
1581
1582 (defcustom gnus-treat-x-pgp-sig nil
1583   "Verify X-PGP-Sig.
1584 To automatically treat X-PGP-Sig, set it to head.
1585 Valid values are nil, t, `head', `first', `last', an integer or a
1586 predicate.  See Info node `(gnus)Customizing Articles'."
1587   :version "22.1"
1588   :group 'gnus-article-treat
1589   :group 'mime-security
1590   :link '(custom-manual "(gnus)Customizing Articles")
1591   :type gnus-article-treat-custom)
1592
1593 (defvar gnus-article-encrypt-protocol-alist
1594   '(("PGP" . mml2015-self-encrypt)))
1595
1596 ;; Set to nil if more than one protocol added to
1597 ;; gnus-article-encrypt-protocol-alist.
1598 (defcustom gnus-article-encrypt-protocol "PGP"
1599   "The protocol used for encrypt articles.
1600 It is a string, such as \"PGP\". If nil, ask user."
1601   :version "22.1"
1602   :type 'string
1603   :group 'mime-security)
1604
1605 (defvar gnus-article-wash-function nil
1606   "Function used for converting HTML into text.")
1607
1608 (defcustom gnus-use-idna (and (condition-case nil (require 'idna) (file-error))
1609                               (mm-coding-system-p 'utf-8)
1610                               (executable-find idna-program))
1611   "Whether IDNA decoding of headers is used when viewing messages.
1612 This requires GNU Libidn, and by default only enabled if it is found."
1613   :version "22.1"
1614   :group 'gnus-article-headers
1615   :type 'boolean)
1616
1617 (defcustom gnus-article-over-scroll nil
1618   "If non-nil, allow scrolling the article buffer even when there no more text."
1619   :version "22.1"
1620   :group 'gnus-article
1621   :type 'boolean)
1622
1623 ;;; Internal variables
1624
1625 (defvar gnus-english-month-names
1626   '("January" "February" "March" "April" "May" "June" "July" "August"
1627     "September" "October" "November" "December"))
1628
1629 (defvar article-goto-body-goes-to-point-min-p nil)
1630 (defvar gnus-article-wash-types nil)
1631 (defvar gnus-article-emphasis-alist nil)
1632 (defvar gnus-article-image-alist nil)
1633
1634 (defvar gnus-article-mime-handle-alist-1 nil)
1635 (defvar gnus-treatment-function-alist
1636   '((gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1637     (gnus-treat-strip-banner gnus-article-strip-banner)
1638     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1639     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1640     (gnus-treat-buttonize gnus-article-add-buttons)
1641     (gnus-treat-fill-article gnus-article-fill-cited-article)
1642     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1643     (gnus-treat-strip-cr gnus-article-remove-cr)
1644     (gnus-treat-unsplit-urls gnus-article-unsplit-urls)
1645     (gnus-treat-date-ut gnus-article-date-ut)
1646     (gnus-treat-date-local gnus-article-date-local)
1647     (gnus-treat-date-english gnus-article-date-english)
1648     (gnus-treat-date-original gnus-article-date-original)
1649     (gnus-treat-date-user-defined gnus-article-date-user)
1650     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1651     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1652     (gnus-treat-display-x-face gnus-article-display-x-face)
1653     (gnus-treat-display-face gnus-article-display-face)
1654     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1655     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1656     (gnus-treat-hide-signature gnus-article-hide-signature)
1657     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1658     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1659     (gnus-treat-strip-pem gnus-article-hide-pem)
1660     (gnus-treat-from-picon gnus-treat-from-picon)
1661     (gnus-treat-mail-picon gnus-treat-mail-picon)
1662     (gnus-treat-newsgroups-picon gnus-treat-newsgroups-picon)
1663     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1664     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1665     (gnus-treat-strip-trailing-blank-lines
1666      gnus-article-remove-trailing-blank-lines)
1667     (gnus-treat-strip-leading-blank-lines
1668      gnus-article-strip-leading-blank-lines)
1669     (gnus-treat-strip-multiple-blank-lines
1670      gnus-article-strip-multiple-blank-lines)
1671     (gnus-treat-overstrike gnus-article-treat-overstrike)
1672     (gnus-treat-ansi-sequences gnus-article-treat-ansi-sequences)
1673     (gnus-treat-unfold-headers gnus-article-treat-unfold-headers)
1674     (gnus-treat-fold-newsgroups gnus-article-treat-fold-newsgroups)
1675     (gnus-treat-fold-headers gnus-article-treat-fold-headers)
1676     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1677     (gnus-treat-display-smileys gnus-treat-smiley)
1678     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1679     (gnus-treat-wash-html gnus-article-wash-html)
1680     (gnus-treat-emphasize gnus-article-emphasize)
1681     (gnus-treat-hide-citation gnus-article-hide-citation)
1682     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1683     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1684     (gnus-treat-body-boundary gnus-article-treat-body-boundary)
1685     (gnus-treat-play-sounds gnus-earcon-display)))
1686
1687 (defvar gnus-article-mime-handle-alist nil)
1688 (defvar article-lapsed-timer nil)
1689 (defvar gnus-article-current-summary nil)
1690
1691 (defvar gnus-article-mode-syntax-table
1692   (let ((table (copy-syntax-table text-mode-syntax-table)))
1693     ;; This causes the citation match run O(2^n).
1694     ;; (modify-syntax-entry ?- "w" table)
1695     (modify-syntax-entry ?> ")<" table)
1696     (modify-syntax-entry ?< "(>" table)
1697     ;; make M-. in article buffers work for `foo' strings
1698     (modify-syntax-entry ?' " " table)
1699     (modify-syntax-entry ?` " " table)
1700     table)
1701   "Syntax table used in article mode buffers.
1702 Initialized from `text-mode-syntax-table.")
1703
1704 (defvar gnus-save-article-buffer nil)
1705
1706 (defvar gnus-number-of-articles-to-be-saved nil)
1707
1708 (defvar gnus-inhibit-hiding nil)
1709
1710 (defvar gnus-article-edit-mode nil)
1711
1712 ;;; Macros for dealing with the article buffer.
1713
1714 (defmacro gnus-with-article-headers (&rest forms)
1715   `(with-current-buffer gnus-article-buffer
1716      (save-restriction
1717        (let ((inhibit-read-only t)
1718              (inhibit-point-motion-hooks t)
1719              (case-fold-search t))
1720          (article-narrow-to-head)
1721          ,@forms))))
1722
1723 (put 'gnus-with-article-headers 'lisp-indent-function 0)
1724 (put 'gnus-with-article-headers 'edebug-form-spec '(body))
1725
1726 (defmacro gnus-with-article-buffer (&rest forms)
1727   `(with-current-buffer gnus-article-buffer
1728      (let ((inhibit-read-only t))
1729        ,@forms)))
1730
1731 (put 'gnus-with-article-buffer 'lisp-indent-function 0)
1732 (put 'gnus-with-article-buffer 'edebug-form-spec '(body))
1733
1734 (defun gnus-article-goto-header (header)
1735   "Go to HEADER, which is a regular expression."
1736   (re-search-forward (concat "^\\(" header "\\):") nil t))
1737
1738 (defsubst gnus-article-hide-text (b e props)
1739   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1740   (gnus-add-text-properties-when 'article-type nil b e props)
1741   (when (memq 'intangible props)
1742     (put-text-property
1743      (max (1- b) (point-min))
1744      b 'intangible (cddr (memq 'intangible props)))))
1745
1746 (defsubst gnus-article-unhide-text (b e)
1747   "Remove hidden text properties from region between B and E."
1748   (remove-text-properties b e gnus-hidden-properties)
1749   (when (memq 'intangible gnus-hidden-properties)
1750     (put-text-property (max (1- b) (point-min))
1751                        b 'intangible nil)))
1752
1753 (defun gnus-article-hide-text-type (b e type)
1754   "Hide text of TYPE between B and E."
1755   (gnus-add-wash-type type)
1756   (gnus-article-hide-text
1757    b e (cons 'article-type (cons type gnus-hidden-properties))))
1758
1759 (defun gnus-article-unhide-text-type (b e type)
1760   "Unhide text of TYPE between B and E."
1761   (gnus-delete-wash-type type)
1762   (remove-text-properties
1763    b e (cons 'article-type (cons type gnus-hidden-properties)))
1764   (when (memq 'intangible gnus-hidden-properties)
1765     (put-text-property (max (1- b) (point-min))
1766                        b 'intangible nil)))
1767
1768 (defun gnus-article-hide-text-of-type (type)
1769   "Hide text of TYPE in the current buffer."
1770   (save-excursion
1771     (let ((b (point-min))
1772           (e (point-max)))
1773       (while (setq b (text-property-any b e 'article-type type))
1774         (add-text-properties b (incf b) gnus-hidden-properties)))))
1775
1776 (defun gnus-article-delete-text-of-type (type)
1777   "Delete text of TYPE in the current buffer."
1778   (save-excursion
1779     (let ((b (point-min)))
1780       (if (eq type 'multipart)
1781           ;; Remove MIME buttons associated with multipart/alternative parts.
1782           (progn
1783             (goto-char b)
1784             (while (if (get-text-property (point) 'gnus-part)
1785                        (setq b (point))
1786                      (when (setq b (next-single-property-change (point)
1787                                                                 'gnus-part))
1788                        (goto-char b)
1789                        t))
1790               (end-of-line)
1791               (skip-chars-forward "\n")
1792               (when (eq (get-text-property b 'article-type) 'multipart)
1793                 (delete-region b (point)))))
1794         (while (setq b (text-property-any b (point-max) 'article-type type))
1795           (delete-region
1796            b (or (text-property-not-all b (point-max) 'article-type type)
1797                  (point-max))))))))
1798
1799 (defun gnus-article-delete-invisible-text ()
1800   "Delete all invisible text in the current buffer."
1801   (save-excursion
1802     (let ((b (point-min)))
1803       (while (setq b (text-property-any b (point-max) 'invisible t))
1804         (delete-region
1805          b (or (text-property-not-all b (point-max) 'invisible t)
1806                (point-max)))))))
1807
1808 (defun gnus-article-text-type-exists-p (type)
1809   "Say whether any text of type TYPE exists in the buffer."
1810   (text-property-any (point-min) (point-max) 'article-type type))
1811
1812 (defsubst gnus-article-header-rank ()
1813   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1814   (let ((list gnus-sorted-header-list)
1815         (i 1))
1816     (while list
1817       (if (looking-at (car list))
1818           (setq list nil)
1819         (setq list (cdr list))
1820         (incf i)))
1821       i))
1822
1823 (defun article-hide-headers (&optional arg delete)
1824   "Hide unwanted headers and possibly sort them as well."
1825   (interactive)
1826   ;; This function might be inhibited.
1827   (unless gnus-inhibit-hiding
1828     (let ((inhibit-read-only t)
1829           (case-fold-search t)
1830           (max (1+ (length gnus-sorted-header-list)))
1831           (inhibit-point-motion-hooks t)
1832           (cur (current-buffer))
1833           ignored visible beg)
1834       (save-excursion
1835         ;; `gnus-ignored-headers' and `gnus-visible-headers' may be
1836         ;; group parameters, so we should go to the summary buffer.
1837         (when (prog1
1838                   (condition-case nil
1839                       (progn (set-buffer gnus-summary-buffer) t)
1840                     (error nil))
1841                 (setq ignored (when (not gnus-visible-headers)
1842                                 (cond ((stringp gnus-ignored-headers)
1843                                        gnus-ignored-headers)
1844                                       ((listp gnus-ignored-headers)
1845                                        (mapconcat 'identity
1846                                                   gnus-ignored-headers
1847                                                   "\\|"))))
1848                       visible (cond ((stringp gnus-visible-headers)
1849                                      gnus-visible-headers)
1850                                     ((and gnus-visible-headers
1851                                           (listp gnus-visible-headers))
1852                                      (mapconcat 'identity
1853                                                 gnus-visible-headers
1854                                                 "\\|")))))
1855           (set-buffer cur))
1856         (save-restriction
1857           ;; First we narrow to just the headers.
1858           (article-narrow-to-head)
1859           ;; Hide any "From " lines at the beginning of (mail) articles.
1860           (while (looking-at "From ")
1861             (forward-line 1))
1862           (unless (bobp)
1863             (delete-region (point-min) (point)))
1864           ;; Then treat the rest of the header lines.
1865           ;; Then we use the two regular expressions
1866           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1867           ;; select which header lines is to remain visible in the
1868           ;; article buffer.
1869           (while (re-search-forward "^[^ \t:]*:" nil t)
1870             (beginning-of-line)
1871             ;; Mark the rank of the header.
1872             (put-text-property
1873              (point) (1+ (point)) 'message-rank
1874              (if (or (and visible (looking-at visible))
1875                      (and ignored
1876                           (not (looking-at ignored))))
1877                  (gnus-article-header-rank)
1878                (+ 2 max)))
1879             (forward-line 1))
1880           (message-sort-headers-1)
1881           (when (setq beg (text-property-any
1882                            (point-min) (point-max) 'message-rank (+ 2 max)))
1883             ;; We delete the unwanted headers.
1884             (gnus-add-wash-type 'headers)
1885             (add-text-properties (point-min) (+ 5 (point-min))
1886                                  '(article-type headers dummy-invisible t))
1887             (delete-region beg (point-max))))))))
1888
1889 (defun article-hide-boring-headers (&optional arg)
1890   "Toggle hiding of headers that aren't very interesting.
1891 If given a negative prefix, always show; if given a positive prefix,
1892 always hide."
1893   (interactive (gnus-article-hidden-arg))
1894   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1895              (not gnus-show-all-headers))
1896     (save-excursion
1897       (save-restriction
1898         (let ((inhibit-read-only t)
1899               (inhibit-point-motion-hooks t))
1900           (article-narrow-to-head)
1901           (dolist (elem gnus-boring-article-headers)
1902             (goto-char (point-min))
1903             (cond
1904              ;; Hide empty headers.
1905              ((eq elem 'empty)
1906               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1907                 (forward-line -1)
1908                 (gnus-article-hide-text-type
1909                  (point-at-bol)
1910                  (progn
1911                    (end-of-line)
1912                    (if (re-search-forward "^[^ \t]" nil t)
1913                        (match-beginning 0)
1914                      (point-max)))
1915                  'boring-headers)))
1916              ;; Hide boring Newsgroups header.
1917              ((eq elem 'newsgroups)
1918               (when (gnus-string-equal
1919                      (gnus-fetch-field "newsgroups")
1920                      (gnus-group-real-name
1921                       (if (boundp 'gnus-newsgroup-name)
1922                           gnus-newsgroup-name
1923                         "")))
1924                 (gnus-article-hide-header "newsgroups")))
1925              ((eq elem 'to-address)
1926               (let ((to (message-fetch-field "to"))
1927                     (to-address
1928                      (gnus-parameter-to-address
1929                       (if (boundp 'gnus-newsgroup-name)
1930                           gnus-newsgroup-name ""))))
1931                 (when (and to to-address
1932                            (ignore-errors
1933                              (gnus-string-equal
1934                               ;; only one address in To
1935                               (nth 1 (mail-extract-address-components to))
1936                               to-address)))
1937                   (gnus-article-hide-header "to"))))
1938              ((eq elem 'to-list)
1939               (let ((to (message-fetch-field "to"))
1940                     (to-list
1941                      (gnus-parameter-to-list
1942                       (if (boundp 'gnus-newsgroup-name)
1943                           gnus-newsgroup-name ""))))
1944                 (when (and to to-list
1945                            (ignore-errors
1946                              (gnus-string-equal
1947                               ;; only one address in To
1948                               (nth 1 (mail-extract-address-components to))
1949                               to-list)))
1950                   (gnus-article-hide-header "to"))))
1951              ((eq elem 'cc-list)
1952               (let ((cc (message-fetch-field "cc"))
1953                     (to-list
1954                      (gnus-parameter-to-list
1955                       (if (boundp 'gnus-newsgroup-name)
1956                           gnus-newsgroup-name ""))))
1957                 (when (and cc to-list
1958                            (ignore-errors
1959                              (gnus-string-equal
1960                               ;; only one address in CC
1961                               (nth 1 (mail-extract-address-components cc))
1962                               to-list)))
1963                   (gnus-article-hide-header "cc"))))
1964              ((eq elem 'followup-to)
1965               (when (gnus-string-equal
1966                      (message-fetch-field "followup-to")
1967                      (message-fetch-field "newsgroups"))
1968                 (gnus-article-hide-header "followup-to")))
1969              ((eq elem 'reply-to)
1970               (if (gnus-group-find-parameter
1971                    gnus-newsgroup-name 'broken-reply-to)
1972                   (gnus-article-hide-header "reply-to")
1973                 (let ((from (message-fetch-field "from"))
1974                       (reply-to (message-fetch-field "reply-to")))
1975                   (when
1976                       (and
1977                        from reply-to
1978                        (ignore-errors
1979                          (equal
1980                           (sort (mapcar
1981                                  (lambda (x) (downcase (cadr x)))
1982                                  (mail-extract-address-components from t))
1983                                 'string<)
1984                           (sort (mapcar
1985                                  (lambda (x) (downcase (cadr x)))
1986                                  (mail-extract-address-components reply-to t))
1987                                 'string<))))
1988                     (gnus-article-hide-header "reply-to")))))
1989              ((eq elem 'date)
1990               (let ((date (with-current-buffer gnus-original-article-buffer
1991                             ;; If date in `gnus-article-buffer' is localized
1992                             ;; (`gnus-treat-date-user-defined'),
1993                             ;; `days-between' might fail.
1994                             (message-fetch-field "date"))))
1995                 (when (and date
1996                            (< (days-between (current-time-string) date)
1997                               4))
1998                   (gnus-article-hide-header "date"))))
1999              ((eq elem 'long-to)
2000               (let ((to (message-fetch-field "to"))
2001                     (cc (message-fetch-field "cc")))
2002                 (when (> (length to) 1024)
2003                   (gnus-article-hide-header "to"))
2004                 (when (> (length cc) 1024)
2005                   (gnus-article-hide-header "cc"))))
2006              ((eq elem 'many-to)
2007               (let ((to-count 0)
2008                     (cc-count 0))
2009                 (goto-char (point-min))
2010                 (while (re-search-forward "^to:" nil t)
2011                   (setq to-count (1+ to-count)))
2012                 (when (> to-count 1)
2013                   (while (> to-count 0)
2014                     (goto-char (point-min))
2015                     (save-restriction
2016                       (re-search-forward "^to:" nil nil to-count)
2017                       (forward-line -1)
2018                       (narrow-to-region (point) (point-max))
2019                       (gnus-article-hide-header "to"))
2020                     (setq to-count (1- to-count))))
2021                 (goto-char (point-min))
2022                 (while (re-search-forward "^cc:" nil t)
2023                   (setq cc-count (1+ cc-count)))
2024                 (when (> cc-count 1)
2025                   (while (> cc-count 0)
2026                     (goto-char (point-min))
2027                     (save-restriction
2028                       (re-search-forward "^cc:" nil nil cc-count)
2029                       (forward-line -1)
2030                       (narrow-to-region (point) (point-max))
2031                       (gnus-article-hide-header "cc"))
2032                     (setq cc-count (1- cc-count)))))))))))))
2033
2034 (defun gnus-article-hide-header (header)
2035   (save-excursion
2036     (goto-char (point-min))
2037     (when (re-search-forward (concat "^" header ":") nil t)
2038       (gnus-article-hide-text-type
2039        (point-at-bol)
2040        (progn
2041          (end-of-line)
2042          (if (re-search-forward "^[^ \t]" nil t)
2043              (match-beginning 0)
2044            (point-max)))
2045        'boring-headers))))
2046
2047 (defvar gnus-article-normalized-header-length 40
2048   "Length of normalized headers.")
2049
2050 (defun article-normalize-headers ()
2051   "Make all header lines 40 characters long."
2052   (interactive)
2053   (let ((inhibit-read-only t)
2054         column)
2055     (save-excursion
2056       (save-restriction
2057         (article-narrow-to-head)
2058         (while (not (eobp))
2059           (cond
2060            ((< (setq column (- (point-at-eol) (point)))
2061                gnus-article-normalized-header-length)
2062             (end-of-line)
2063             (insert (make-string
2064                      (- gnus-article-normalized-header-length column)
2065                      ? )))
2066            ((> column gnus-article-normalized-header-length)
2067             (gnus-put-text-property
2068              (progn
2069                (forward-char gnus-article-normalized-header-length)
2070                (point))
2071              (point-at-eol)
2072              'invisible t))
2073            (t
2074             ;; Do nothing.
2075             ))
2076           (forward-line 1))))))
2077
2078 (defun article-treat-dumbquotes ()
2079   "Translate M****s*** sm*rtq**t*s and other symbols into proper text.
2080 Note that this function guesses whether a character is a sm*rtq**t* or
2081 not, so it should only be used interactively.
2082
2083 Sm*rtq**t*s are M****s***'s unilateral extension to the
2084 iso-8859-1 character map in an attempt to provide more quoting
2085 characters.  If you see something like \\222 or \\264 where
2086 you're expecting some kind of apostrophe or quotation mark, then
2087 try this wash."
2088   (interactive)
2089   (article-translate-strings gnus-article-dumbquotes-map))
2090
2091 (defun article-translate-characters (from to)
2092   "Translate all characters in the body of the article according to FROM and TO.
2093 FROM is a string of characters to translate from; to is a string of
2094 characters to translate to."
2095   (save-excursion
2096     (when (article-goto-body)
2097       (let ((inhibit-read-only t)
2098             (x (make-string 225 ?x))
2099             (i -1))
2100         (while (< (incf i) (length x))
2101           (aset x i i))
2102         (setq i 0)
2103         (while (< i (length from))
2104           (aset x (aref from i) (aref to i))
2105           (incf i))
2106         (translate-region (point) (point-max) x)))))
2107
2108 (defun article-translate-strings (map)
2109   "Translate all string in the body of the article according to MAP.
2110 MAP is an alist where the elements are on the form (\"from\" \"to\")."
2111   (save-excursion
2112     (when (article-goto-body)
2113       (let ((inhibit-read-only t))
2114         (dolist (elem map)
2115           (save-excursion
2116             (while (search-forward (car elem) nil t)
2117               (replace-match (cadr elem)))))))))
2118
2119 (defun article-treat-overstrike ()
2120   "Translate overstrikes into bold text."
2121   (interactive)
2122   (save-excursion
2123     (when (article-goto-body)
2124       (let ((inhibit-read-only t))
2125         (while (search-forward "\b" nil t)
2126           (let ((next (char-after))
2127                 (previous (char-after (- (point) 2))))
2128             ;; We do the boldification/underlining by hiding the
2129             ;; overstrikes and putting the proper text property
2130             ;; on the letters.
2131             (cond
2132              ((eq next previous)
2133               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2134               (put-text-property (point) (1+ (point)) 'face 'bold))
2135              ((eq next ?_)
2136               (gnus-article-hide-text-type
2137                (1- (point)) (1+ (point)) 'overstrike)
2138               (put-text-property
2139                (- (point) 2) (1- (point)) 'face 'underline))
2140              ((eq previous ?_)
2141               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
2142               (put-text-property
2143                (point) (1+ (point)) 'face 'underline)))))))))
2144
2145 (defun article-treat-ansi-sequences ()
2146   "Translate ANSI SGR control sequences into overlays or extents."
2147   (interactive)
2148   (save-excursion
2149     (when (article-goto-body)
2150       (let ((inhibit-read-only t))
2151         (ansi-color-apply-on-region (point) (point-max))))))
2152
2153 (defun gnus-article-treat-unfold-headers ()
2154   "Unfold folded message headers.
2155 Only the headers that fit into the current window width will be
2156 unfolded."
2157   (interactive)
2158   (gnus-with-article-headers
2159     (let (length)
2160       (while (not (eobp))
2161         (save-restriction
2162           (mail-header-narrow-to-field)
2163           (let* ((header (buffer-string))
2164                  (unfoldable
2165                   (or (equal gnus-article-unfold-long-headers t)
2166                       (and (stringp gnus-article-unfold-long-headers)
2167                            (string-match gnus-article-unfold-long-headers header)))))
2168             (with-temp-buffer
2169               (insert header)
2170               (goto-char (point-min))
2171               (while (re-search-forward "\n[\t ]" nil t)
2172                 (replace-match " " t t)))
2173             (setq length (- (point-max) (point-min) 1))
2174             (when (or unfoldable
2175                       (< length (window-width)))
2176               (while (re-search-forward "\n[\t ]" nil t)
2177                 (replace-match " " t t))))
2178           (goto-char (point-max)))))))
2179
2180 (defun gnus-article-treat-fold-headers ()
2181   "Fold message headers."
2182   (interactive)
2183   (gnus-with-article-headers
2184     (while (not (eobp))
2185       (save-restriction
2186         (mail-header-narrow-to-field)
2187         (mail-header-fold-field)
2188         (goto-char (point-max))))))
2189
2190 (defun gnus-treat-smiley ()
2191   "Toggle display of textual emoticons (\"smileys\") as small graphical icons."
2192   (interactive)
2193   (gnus-with-article-buffer
2194     (if (memq 'smiley gnus-article-wash-types)
2195         (gnus-delete-images 'smiley)
2196       (article-goto-body)
2197       (let ((images (smiley-region (point) (point-max))))
2198         (when images
2199           (gnus-add-wash-type 'smiley)
2200           (dolist (image images)
2201             (gnus-add-image 'smiley image)))))))
2202
2203 (defun gnus-article-remove-images ()
2204   "Remove all images from the article buffer."
2205   (interactive)
2206   (gnus-with-article-buffer
2207     (dolist (elem gnus-article-image-alist)
2208       (gnus-delete-images (car elem)))))
2209
2210 (defun gnus-article-treat-fold-newsgroups ()
2211   "Unfold folded message headers.
2212 Only the headers that fit into the current window width will be
2213 unfolded."
2214   (interactive)
2215   (gnus-with-article-headers
2216     (while (gnus-article-goto-header "newsgroups\\|followup-to")
2217       (save-restriction
2218         (mail-header-narrow-to-field)
2219         (while (re-search-forward ", *" nil t)
2220           (replace-match ", " t t))
2221         (mail-header-fold-field)
2222         (goto-char (point-max))))))
2223
2224 (defcustom gnus-article-truncate-lines (default-value 'truncate-lines)
2225   "Value of `truncate-lines' in Gnus Article buffer.
2226 Valid values are nil, t, `head', `first', `last', an integer or a
2227 predicate.  See Info node `(gnus)Customizing Articles'."
2228   :version "23.1" ;; No Gnus
2229   :group 'gnus-article
2230   ;; :link '(custom-manual "(gnus)Customizing Articles")
2231   :type 'boolean)
2232
2233 (defun gnus-article-toggle-truncate-lines (&optional arg)
2234   "Toggle whether to fold or truncate long lines in article the buffer.
2235 If ARG is non-nil and not a number, toggle
2236 `gnus-article-truncate-lines' too.  If ARG is a number, truncate
2237 long lines iff arg is positive."
2238   (interactive "P")
2239   (cond
2240    ((and (numberp arg) (> arg 0))
2241     (setq gnus-article-truncate-lines t))
2242    ((numberp arg)
2243     (setq gnus-article-truncate-lines nil))
2244    (arg
2245     (setq gnus-article-truncate-lines
2246           (not gnus-article-truncate-lines))))
2247   (gnus-with-article-buffer
2248     (cond
2249      ((and (numberp arg) (> arg 0))
2250       (setq truncate-lines nil))
2251      ((numberp arg)
2252       (setq truncate-lines t)))
2253     ;; In versions of Emacs 22 (CVS) before 2006-05-26,
2254     ;; `toggle-truncate-lines' needs an argument.
2255     (toggle-truncate-lines)))
2256
2257 (defun gnus-article-treat-body-boundary ()
2258   "Place a boundary line at the end of the headers."
2259   (interactive)
2260   (when (and gnus-body-boundary-delimiter
2261              (> (length gnus-body-boundary-delimiter) 0))
2262     (gnus-with-article-headers
2263       (goto-char (point-max))
2264       (let ((start (point)))
2265         (insert "X-Boundary: ")
2266         (gnus-add-text-properties start (point) '(invisible t intangible t))
2267         (insert (let (str)
2268                   (while (>= (1- (window-width)) (length str))
2269                     (setq str (concat str gnus-body-boundary-delimiter)))
2270                   (substring str 0 (1- (window-width))))
2271                 "\n")
2272         (gnus-put-text-property start (point) 'gnus-decoration 'header)))))
2273
2274 (defun article-fill-long-lines ()
2275   "Fill lines that are wider than the window width."
2276   (interactive)
2277   (save-excursion
2278     (let ((inhibit-read-only t)
2279           (width (window-width (get-buffer-window (current-buffer)))))
2280       (save-restriction
2281         (article-goto-body)
2282         (let ((adaptive-fill-mode nil)) ;Why?  -sm
2283           (while (not (eobp))
2284             (end-of-line)
2285             (when (>= (current-column) (min fill-column width))
2286               (narrow-to-region (min (1+ (point)) (point-max))
2287                                 (point-at-bol))
2288               (let ((goback (point-marker)))
2289                 (fill-paragraph nil)
2290                 (goto-char (marker-position goback)))
2291               (widen))
2292             (forward-line 1)))))))
2293
2294 (defun article-capitalize-sentences ()
2295   "Capitalize the first word in each sentence."
2296   (interactive)
2297   (save-excursion
2298     (let ((inhibit-read-only t)
2299           (paragraph-start "^[\n\^L]"))
2300       (article-goto-body)
2301       (while (not (eobp))
2302         (capitalize-word 1)
2303         (forward-sentence)))))
2304
2305 (defun article-remove-cr ()
2306   "Remove trailing CRs and then translate remaining CRs into LFs."
2307   (interactive)
2308   (save-excursion
2309     (let ((inhibit-read-only t))
2310       (goto-char (point-min))
2311       (while (re-search-forward "\r+$" nil t)
2312         (replace-match "" t t))
2313       (goto-char (point-min))
2314       (while (search-forward "\r" nil t)
2315         (replace-match "\n" t t)))))
2316
2317 (defun article-remove-trailing-blank-lines ()
2318   "Remove all trailing blank lines from the article."
2319   (interactive)
2320   (save-excursion
2321     (let ((inhibit-read-only t))
2322       (goto-char (point-max))
2323       (delete-region
2324        (point)
2325        (progn
2326          (while (and (not (bobp))
2327                      (looking-at "^[ \t]*$")
2328                      (not (gnus-annotation-in-region-p
2329                            (point) (point-at-eol))))
2330            (forward-line -1))
2331          (forward-line 1)
2332          (point))))))
2333
2334 (defvar gnus-face-properties-alist)
2335
2336 (defun article-display-face (&optional force)
2337   "Display any Face headers in the header."
2338   (interactive (list 'force))
2339   (let ((wash-face-p buffer-read-only))
2340     (gnus-with-article-headers
2341       ;; When displaying parts, this function can be called several times on
2342       ;; the same article, without any intended toggle semantic (as typing `W
2343       ;; D d' would have). So face deletion must occur only when we come from
2344       ;; an interactive command, that is when the *Article* buffer is
2345       ;; read-only.
2346       (if (and wash-face-p (memq 'face gnus-article-wash-types))
2347           (gnus-delete-images 'face)
2348         (let ((from (message-fetch-field "from"))
2349               face faces)
2350           (save-current-buffer
2351             (when (and wash-face-p
2352                        (gnus-buffer-live-p gnus-original-article-buffer)
2353                        (not (re-search-forward "^Face:[\t ]*" nil t)))
2354               (set-buffer gnus-original-article-buffer))
2355             (save-restriction
2356               (mail-narrow-to-head)
2357               (when (or force
2358                         ;; Check whether this face is censored.
2359                         (not (and gnus-article-x-face-too-ugly
2360                                   (or from
2361                                       (setq from (message-fetch-field "from")))
2362                                   (string-match gnus-article-x-face-too-ugly
2363                                                 from))))
2364                 (while (gnus-article-goto-header "Face")
2365                   (push (mail-header-field-value) faces)))))
2366           (when faces
2367             (goto-char (point-min))
2368             (let (png image)
2369               (unless (setq from (gnus-article-goto-header "from"))
2370                 (insert "From:")
2371                 (setq from (point))
2372                 (insert " [no `from' set]\n"))
2373               (while faces
2374                 (when (setq png (gnus-convert-face-to-png (pop faces)))
2375                   (setq image
2376                         (apply 'gnus-create-image png 'png t
2377                                (cdr (assq 'png gnus-face-properties-alist))))
2378                   (goto-char from)
2379                   (gnus-add-wash-type 'face)
2380                   (gnus-add-image 'face image)
2381                   (gnus-put-image image nil 'face))))))))))
2382
2383 (defun article-display-x-face (&optional force)
2384   "Look for an X-Face header and display it if present."
2385   (interactive (list 'force))
2386   (let ((wash-face-p buffer-read-only)) ;; When type `W f'
2387     (gnus-with-article-headers
2388       ;; Delete the old process, if any.
2389       (when (process-status "article-x-face")
2390         (delete-process "article-x-face"))
2391       ;; See the comment in `article-display-face'.
2392       (if (and wash-face-p (memq 'xface gnus-article-wash-types))
2393           ;; We have already displayed X-Faces, so we remove them
2394           ;; instead.
2395           (gnus-delete-images 'xface)
2396         ;; Display X-Faces.
2397         (let ((from (message-fetch-field "from"))
2398               x-faces face)
2399           (save-current-buffer
2400             (when (and wash-face-p
2401                        (gnus-buffer-live-p gnus-original-article-buffer)
2402                        (not (re-search-forward "^X-Face:[\t ]*" nil t)))
2403               ;; If type `W f', use gnus-original-article-buffer,
2404               ;; otherwise use the current buffer because displaying
2405               ;; RFC822 parts calls this function too.
2406               (set-buffer gnus-original-article-buffer))
2407             (save-restriction
2408               (mail-narrow-to-head)
2409               (and gnus-article-x-face-command
2410                    (or force
2411                        ;; Check whether this face is censored.
2412                        (not (and gnus-article-x-face-too-ugly
2413                                  (or from
2414                                      (setq from (message-fetch-field "from")))
2415                                  (string-match gnus-article-x-face-too-ugly
2416                                                from))))
2417                    (while (gnus-article-goto-header "X-Face")
2418                      (push (mail-header-field-value) x-faces)))))
2419           (when x-faces
2420             ;; We display the face.
2421             (cond ((functionp gnus-article-x-face-command)
2422                    ;; The command is a lisp function, so we call it.
2423                    (mapc gnus-article-x-face-command x-faces))
2424                   ((stringp gnus-article-x-face-command)
2425                    ;; The command is a string, so we interpret the command
2426                    ;; as a, well, command, and fork it off.
2427                    (let ((process-connection-type nil))
2428                      (gnus-set-process-query-on-exit-flag
2429                       (start-process
2430                        "article-x-face" nil shell-file-name
2431                        shell-command-switch gnus-article-x-face-command)
2432                       nil)
2433                      ;; Sending multiple EOFs to xv doesn't work,
2434                      ;; so we only do a single external face.
2435                      (with-temp-buffer
2436                        (insert (car x-faces))
2437                        (process-send-region "article-x-face"
2438                                             (point-min) (point-max)))
2439                      (process-send-eof "article-x-face")))
2440                   (t
2441                    (error "`%s' set to `%s' is not a function"
2442                           gnus-article-x-face-command
2443                           'gnus-article-x-face-command)))))))))
2444
2445 (defun article-decode-mime-words ()
2446   "Decode all MIME-encoded words in the article."
2447   (interactive)
2448   (gnus-with-article-buffer
2449     (let ((inhibit-point-motion-hooks t)
2450           (mail-parse-charset gnus-newsgroup-charset)
2451           (mail-parse-ignored-charsets
2452            (with-current-buffer gnus-summary-buffer
2453              gnus-newsgroup-ignored-charsets)))
2454       (mail-decode-encoded-word-region (point-min) (point-max)))))
2455
2456 (defun article-decode-charset (&optional prompt)
2457   "Decode charset-encoded text in the article.
2458 If PROMPT (the prefix), prompt for a coding system to use."
2459   (interactive "P")
2460   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
2461         (inhibit-read-only t)
2462         (mail-parse-charset gnus-newsgroup-charset)
2463         (mail-parse-ignored-charsets
2464          (save-excursion (condition-case nil
2465                              (set-buffer gnus-summary-buffer)
2466                            (error))
2467                          gnus-newsgroup-ignored-charsets))
2468         ct cte ctl charset format)
2469     (save-excursion
2470       (save-restriction
2471         (article-narrow-to-head)
2472         (setq ct (message-fetch-field "Content-Type" t)
2473               cte (message-fetch-field "Content-Transfer-Encoding" t)
2474               ctl (and ct (mail-header-parse-content-type ct))
2475               charset (cond
2476                        (prompt
2477                         (mm-read-coding-system "Charset to decode: "))
2478                        (ctl
2479                         (mail-content-type-get ctl 'charset)))
2480               format (and ctl (mail-content-type-get ctl 'format)))
2481         (when cte
2482           (setq cte (mail-header-strip cte)))
2483         (if (and ctl (not (string-match "/" (car ctl))))
2484             (setq ctl nil))
2485         (goto-char (point-max)))
2486       (forward-line 1)
2487       (save-restriction
2488         (narrow-to-region (point) (point-max))
2489         (when (and (eq mail-parse-charset 'gnus-decoded)
2490                    (eq (mm-body-7-or-8) '8bit))
2491           ;; The text code could have been decoded.
2492           (setq charset mail-parse-charset))
2493         (when (and (or (not ctl)
2494                        (equal (car ctl) "text/plain"))
2495                    (not format)) ;; article with format will decode later.
2496           (mm-decode-body
2497            charset (and cte (intern (downcase
2498                                      (gnus-strip-whitespace cte))))
2499            (car ctl)))))))
2500
2501 (defun article-decode-encoded-words ()
2502   "Remove encoded-word encoding from headers."
2503   (let ((inhibit-point-motion-hooks t)
2504         (mail-parse-charset gnus-newsgroup-charset)
2505         (mail-parse-ignored-charsets
2506          (save-excursion (condition-case nil
2507                              (set-buffer gnus-summary-buffer)
2508                            (error))
2509                          gnus-newsgroup-ignored-charsets))
2510         (inhibit-read-only t)
2511         end start)
2512     (goto-char (point-min))
2513     (when (search-forward "\n\n" nil 'move)
2514       (forward-line -1))
2515     (setq end (point))
2516     (while (not (bobp))
2517       (while (progn
2518                (forward-line -1)
2519                (and (not (bobp))
2520                     (memq (char-after) '(?\t ? )))))
2521       (setq start (point))
2522       (if (looking-at "\
2523 \\(?:Resent-\\)?\\(?:From\\|Cc\\|To\\|Bcc\\|\\(?:In-\\)?Reply-To\\|Sender\
2524 \\|Mail-Followup-To\\|Mail-Copies-To\\|Approved\\):")
2525           (funcall gnus-decode-address-function start end)
2526         (funcall gnus-decode-header-function start end))
2527       (goto-char (setq end start)))))
2528
2529 (defun article-decode-group-name ()
2530   "Decode group names in Newsgroups, Followup-To and Xref headers."
2531   (let ((inhibit-point-motion-hooks t)
2532         (inhibit-read-only t)
2533         (method (gnus-find-method-for-group gnus-newsgroup-name))
2534         regexp)
2535     (when (and (or gnus-group-name-charset-method-alist
2536                    gnus-group-name-charset-group-alist)
2537                (gnus-buffer-live-p gnus-original-article-buffer))
2538       (save-restriction
2539         (article-narrow-to-head)
2540         (dolist (header '("Newsgroups" "Followup-To" "Xref"))
2541           (with-current-buffer gnus-original-article-buffer
2542             (goto-char (point-min)))
2543           (setq regexp (concat "^" header
2544                                ":\\([^\n]*\\(?:\n[\t ]+[^\n]+\\)*\\)\n"))
2545           (while (re-search-forward regexp nil t)
2546             (replace-match (save-match-data
2547                              (gnus-decode-newsgroups
2548                               ;; XXX how to use data in article buffer?
2549                               (with-current-buffer gnus-original-article-buffer
2550                                 (re-search-forward regexp nil t)
2551                                 (match-string 1))
2552                               gnus-newsgroup-name method))
2553                            t t nil 1))
2554           (goto-char (point-min)))))))
2555
2556 (autoload 'idna-to-unicode "idna")
2557
2558 (defun article-decode-idna-rhs ()
2559   "Decode IDNA strings in RHS in various headers in current buffer.
2560 The following headers are decoded: From:, To:, Cc:, Reply-To:,
2561 Mail-Reply-To: and Mail-Followup-To:."
2562   (when gnus-use-idna
2563     (save-restriction
2564       (let ((inhibit-point-motion-hooks t)
2565             (inhibit-read-only t))
2566         (article-narrow-to-head)
2567         (goto-char (point-min))
2568         (while (re-search-forward "@[^ \t\n\r,>]*\\(xn--[-A-Za-z0-9.]*\\)[ \t\n\r,>]" nil t)
2569           (let (ace unicode)
2570             (when (save-match-data
2571                     (and (setq ace (match-string 1))
2572                          (save-excursion
2573                            (and (re-search-backward "^[^ \t]" nil t)
2574                                 (looking-at "From\\|To\\|Cc\\|Reply-To\\|Mail-Reply-To\\|Mail-Followup-To")))
2575                          (setq unicode (idna-to-unicode ace))))
2576               (unless (string= ace unicode)
2577                 (replace-match unicode nil nil nil 1)))))))))
2578
2579 (defun article-de-quoted-unreadable (&optional force read-charset)
2580   "Translate a quoted-printable-encoded article.
2581 If FORCE, decode the article whether it is marked as quoted-printable
2582 or not.
2583 If READ-CHARSET, ask for a coding system."
2584   (interactive (list 'force current-prefix-arg))
2585   (save-excursion
2586     (let ((inhibit-read-only t) type charset)
2587       (if (gnus-buffer-live-p gnus-original-article-buffer)
2588           (with-current-buffer gnus-original-article-buffer
2589             (setq type
2590                   (gnus-fetch-field "content-transfer-encoding"))
2591             (let* ((ct (gnus-fetch-field "content-type"))
2592                    (ctl (and ct (mail-header-parse-content-type ct))))
2593               (setq charset (and ctl
2594                                  (mail-content-type-get ctl 'charset)))
2595               (if (stringp charset)
2596                   (setq charset (intern (downcase charset)))))))
2597       (if read-charset
2598           (setq charset (mm-read-coding-system "Charset: " charset)))
2599       (unless charset
2600         (setq charset gnus-newsgroup-charset))
2601       (when (or force
2602                 (and type (let ((case-fold-search t))
2603                             (string-match "quoted-printable" type))))
2604         (article-goto-body)
2605         (quoted-printable-decode-region
2606          (point) (point-max) (mm-charset-to-coding-system charset))))))
2607
2608 (defun article-de-base64-unreadable (&optional force read-charset)
2609   "Translate a base64 article.
2610 If FORCE, decode the article whether it is marked as base64 not.
2611 If READ-CHARSET, ask for a coding system."
2612   (interactive (list 'force current-prefix-arg))
2613   (save-excursion
2614     (let ((inhibit-read-only t) type charset)
2615       (if (gnus-buffer-live-p gnus-original-article-buffer)
2616           (with-current-buffer gnus-original-article-buffer
2617             (setq type
2618                   (gnus-fetch-field "content-transfer-encoding"))
2619             (let* ((ct (gnus-fetch-field "content-type"))
2620                    (ctl (and ct (mail-header-parse-content-type ct))))
2621               (setq charset (and ctl
2622                                  (mail-content-type-get ctl 'charset)))
2623               (if (stringp charset)
2624                   (setq charset (intern (downcase charset)))))))
2625       (if read-charset
2626           (setq charset (mm-read-coding-system "Charset: " charset)))
2627       (unless charset
2628         (setq charset gnus-newsgroup-charset))
2629       (when (or force
2630                 (and type (let ((case-fold-search t))
2631                             (string-match "base64" type))))
2632         (article-goto-body)
2633         (save-restriction
2634           (narrow-to-region (point) (point-max))
2635           (base64-decode-region (point-min) (point-max))
2636           (mm-decode-coding-region
2637            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
2638
2639 (eval-when-compile
2640   (require 'rfc1843))
2641
2642 (defun article-decode-HZ ()
2643   "Translate a HZ-encoded article."
2644   (interactive)
2645   (require 'rfc1843)
2646   (save-excursion
2647     (let ((inhibit-read-only t))
2648       (rfc1843-decode-region (point-min) (point-max)))))
2649
2650 (defun article-unsplit-urls ()
2651   "Remove the newlines that some other mailers insert into URLs."
2652   (interactive)
2653   (save-excursion
2654     (let ((inhibit-read-only t))
2655       (goto-char (point-min))
2656       (while (re-search-forward
2657               "\\(\\(https?\\|ftp\\)://\\S-+\\) *\n\\(\\S-+\\)" nil t)
2658         (replace-match "\\1\\3" t)))
2659     (when (interactive-p)
2660       (gnus-treat-article nil))))
2661
2662
2663 (defun article-wash-html (&optional read-charset)
2664   "Format an HTML article.
2665 If READ-CHARSET, ask for a coding system.  If it is a number, the
2666 charset defined in `gnus-summary-show-article-charset-alist' is used."
2667   (interactive "P")
2668   (save-excursion
2669     (let ((inhibit-read-only t)
2670           charset)
2671       (if read-charset
2672           (if (or (and (numberp read-charset)
2673                        (setq charset
2674                              (cdr
2675                               (assq read-charset
2676                                     gnus-summary-show-article-charset-alist))))
2677                   (setq charset (mm-read-coding-system "Charset: ")))
2678               (let ((gnus-summary-show-article-charset-alist
2679                      (list (cons 1 charset))))
2680                 (with-current-buffer gnus-summary-buffer
2681                   (gnus-summary-show-article 1)))
2682             (error "No charset is given"))
2683         (when (gnus-buffer-live-p gnus-original-article-buffer)
2684           (with-current-buffer gnus-original-article-buffer
2685             (let* ((ct (gnus-fetch-field "content-type"))
2686                    (ctl (and ct (mail-header-parse-content-type ct))))
2687               (setq charset (and ctl
2688                                  (mail-content-type-get ctl 'charset)))
2689               (when (stringp charset)
2690                 (setq charset (intern (downcase charset)))))))
2691         (unless charset
2692           (setq charset gnus-newsgroup-charset)))
2693       (article-goto-body)
2694       (save-window-excursion
2695         (save-restriction
2696           (narrow-to-region (point) (point-max))
2697           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2698                  (entry (assq func mm-text-html-washer-alist)))
2699             (when entry
2700               (setq func (cdr entry)))
2701             (cond
2702              ((functionp func)
2703               (funcall func))
2704              (t
2705               (apply (car func) (cdr func))))))))))
2706
2707 ;; External.
2708 (declare-function w3-region "ext:w3-display" (st nd))
2709
2710 (defun gnus-article-wash-html-with-w3 ()
2711   "Wash the current buffer with w3."
2712   (mm-setup-w3)
2713   (let ((w3-strict-width (window-width))
2714         (url-standalone-mode t)
2715         (url-gateway-unplugged t)
2716         (w3-honor-stylesheets nil))
2717     (condition-case ()
2718         (w3-region (point-min) (point-max))
2719       (error))))
2720
2721 ;; External.
2722 (declare-function w3m-region "ext:w3m" (start end &optional url charset))
2723
2724 (defun gnus-article-wash-html-with-w3m ()
2725   "Wash the current buffer with emacs-w3m."
2726   (mm-setup-w3m)
2727   (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
2728         w3m-force-redisplay)
2729     (w3m-region (point-min) (point-max)))
2730   (when (and mm-inline-text-html-with-w3m-keymap
2731              (boundp 'w3m-minor-mode-map)
2732              w3m-minor-mode-map)
2733     (add-text-properties
2734      (point-min) (point-max)
2735      (list 'keymap w3m-minor-mode-map
2736            ;; Put the mark meaning this part was rendered by emacs-w3m.
2737            'mm-inline-text-html-with-w3m t))))
2738
2739 (defvar charset) ;; Bound by `article-wash-html'.
2740
2741 (defun gnus-article-wash-html-with-w3m-standalone ()
2742   "Wash the current buffer with w3m."
2743   (if (mm-w3m-standalone-supports-m17n-p)
2744       (progn
2745         (unless (mm-coding-system-p charset) ;; Bound by `article-wash-html'.
2746           ;; The default.
2747           (setq charset 'iso-8859-1))
2748         (let ((coding-system-for-write charset)
2749               (coding-system-for-read charset))
2750           (call-process-region
2751            (point-min) (point-max)
2752            "w3m" t t nil "-dump" "-T" "text/html"
2753            "-I" (symbol-name charset) "-O" (symbol-name charset))))
2754     (mm-inline-wash-with-stdin nil "w3m" "-dump" "-T" "text/html")))
2755
2756 (defvar gnus-article-browse-html-temp-list nil
2757   "List of temporary files created by `gnus-article-browse-html-parts'.
2758 Internal variable.")
2759
2760 (defcustom gnus-article-browse-delete-temp 'ask
2761   "What to do with temporary files from `gnus-article-browse-html-parts'.
2762 If nil, don't delete temporary files.  If it is t, delete them on
2763 exit from the summary buffer.  If it is the symbol `file', query
2764 on each file, if it is `ask' ask once when exiting from the
2765 summary buffer."
2766   :group 'gnus-article
2767   :version "23.1" ;; No Gnus
2768   :type '(choice (const :tag "Don't delete" nil)
2769                  (const :tag "Don't ask" t)
2770                  (const :tag "Ask" ask)
2771                  (const :tag "Ask for each file" file)))
2772
2773 ;; Cf. mm-postponed-undisplay-list / mm-destroy-postponed-undisplay-list.
2774
2775 (defun gnus-article-browse-delete-temp-files (&optional how)
2776   "Delete temp-files created by `gnus-article-browse-html-parts'."
2777   (when (and gnus-article-browse-html-temp-list
2778              (or how
2779                  (setq how gnus-article-browse-delete-temp)))
2780     (when (and (eq how 'ask)
2781                (gnus-y-or-n-p (format
2782                                "Delete all %s temporary HTML file(s)? "
2783                                (length gnus-article-browse-html-temp-list)))
2784                (setq how t)))
2785     (dolist (file gnus-article-browse-html-temp-list)
2786       (when (and (file-exists-p file)
2787                  (or (eq how t)
2788                      ;; `how' is neither `nil', `ask' nor `t' (i.e. `file'):
2789                      (gnus-y-or-n-p
2790                       (format "Delete temporary HTML file `%s'? " file))))
2791         (delete-file file)))
2792     ;; Also remove file from the list when not deleted or if file doesn't
2793     ;; exist anymore.
2794     (setq gnus-article-browse-html-temp-list nil))
2795   gnus-article-browse-html-temp-list)
2796
2797 (defun gnus-article-browse-html-parts (list &optional header)
2798   "View all \"text/html\" parts from LIST.
2799 Recurse into multiparts.  The optional HEADER that should be a decoded
2800 message header will be added to the bodies of the \"text/html\" parts."
2801   ;; Internal function used by `gnus-article-browse-html-article'.
2802   (let (type file charset tmp-file showed)
2803     ;; Find and show the html-parts.
2804     (dolist (handle list)
2805       ;; If HTML, show it:
2806       (cond ((not (listp handle)))
2807             ((or (equal (car (setq type (mm-handle-type handle))) "text/html")
2808                  (and (equal (car type) "message/external-body")
2809                       (or header
2810                           (setq file (or (mail-content-type-get type 'name)
2811                                          (mail-content-type-get
2812                                           (mm-handle-disposition handle)
2813                                           'filename))))
2814                       (or (mm-handle-cache handle)
2815                           (condition-case code
2816                               (progn (mm-extern-cache-contents handle) t)
2817                             (error
2818                              (gnus-message 3 "%s" (error-message-string code))
2819                              (when (>= gnus-verbose 3) (sit-for 2))
2820                              nil)))
2821                       (progn
2822                         (setq handle (mm-handle-cache handle)
2823                               type (mm-handle-type handle))
2824                         (equal (car type) "text/html"))))
2825              (when (or (setq charset (mail-content-type-get type 'charset))
2826                        header
2827                        (not file))
2828                (setq tmp-file (mm-make-temp-file
2829                                ;; Do we need to care for 8.3 filenames?
2830                                "mm-" nil ".html")))
2831              ;; Add a meta html tag to specify charset and a header.
2832              (cond
2833               (header
2834                (let (title eheader body hcharset coding)
2835                  (with-temp-buffer
2836                    (mm-enable-multibyte)
2837                    (setq case-fold-search t)
2838                    (insert header "\n")
2839                    (setq title (message-fetch-field "subject"))
2840                    (goto-char (point-min))
2841                    (while (re-search-forward "\\(<\\)\\|\\(>\\)\\|&" nil t)
2842                      (replace-match (cond ((match-beginning 1) "&lt;")
2843                                           ((match-beginning 2) "&gt;")
2844                                           (t "&amp;"))))
2845                    (goto-char (point-min))
2846                    (insert "<pre>\n")
2847                    (goto-char (point-max))
2848                    (insert "</pre>\n<hr>\n")
2849                    ;; We have to examine charset one by one since
2850                    ;; charset specified in parts might be different.
2851                    (if (eq charset 'gnus-decoded)
2852                        (setq charset 'utf-8
2853                              eheader (mm-encode-coding-string (buffer-string)
2854                                                               charset)
2855                              title (when title
2856                                      (mm-encode-coding-string title charset))
2857                              body (mm-encode-coding-string (mm-get-part handle)
2858                                                            charset))
2859                      (setq hcharset (mm-find-mime-charset-region (point-min)
2860                                                                  (point-max)))
2861                      (cond ((= (length hcharset) 1)
2862                             (setq hcharset (car hcharset)
2863                                   coding (mm-charset-to-coding-system
2864                                           hcharset)))
2865                            ((> (length hcharset) 1)
2866                             (setq hcharset 'utf-8
2867                                   coding hcharset)))
2868                      (if coding
2869                          (if charset
2870                              (progn
2871                                (setq body
2872                                      (mm-charset-to-coding-system charset))
2873                                (if (eq coding body)
2874                                    (setq eheader (mm-encode-coding-string
2875                                                   (buffer-string) coding)
2876                                          title (when title
2877                                                  (mm-encode-coding-string
2878                                                   title coding))
2879                                          body (mm-get-part handle))
2880                                  (setq charset 'utf-8
2881                                        eheader (mm-encode-coding-string
2882                                                 (buffer-string) charset)
2883                                        title (when title
2884                                                (mm-encode-coding-string
2885                                                 title charset))
2886                                        body (mm-encode-coding-string
2887                                              (mm-decode-coding-string
2888                                               (mm-get-part handle) body)
2889                                              charset))))
2890                            (setq charset hcharset
2891                                  eheader (mm-encode-coding-string
2892                                           (buffer-string) coding)
2893                                  title (when title
2894                                          (mm-encode-coding-string
2895                                           title coding))
2896                                  body (mm-get-part handle)))
2897                        (setq eheader (mm-string-as-unibyte (buffer-string))
2898                              body (mm-get-part handle))))
2899                    (erase-buffer)
2900                    (mm-disable-multibyte)
2901                    (insert body)
2902                    (when charset
2903                      (mm-add-meta-html-tag handle charset))
2904                    (when title
2905                      (goto-char (point-min))
2906                      (unless (search-forward "<title>" nil t)
2907                        (re-search-forward "<head>\\s-*" nil t)
2908                        (insert "<title>" title "</title>\n")))
2909                    (goto-char (point-min))
2910                    (or (re-search-forward
2911                         "<body\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t)
2912                        (re-search-forward
2913                         "</head\\(?:\\s-+[^>]+\\|\\s-*\\)>\\s-*" nil t))
2914                    (insert eheader)
2915                    (mm-write-region (point-min) (point-max)
2916                                     tmp-file nil nil nil 'binary t))))
2917               (charset
2918                (mm-with-unibyte-buffer
2919                  (insert (if (eq charset 'gnus-decoded)
2920                              (mm-encode-coding-string
2921                               (mm-get-part handle)
2922                               (setq charset 'utf-8))
2923                            (mm-get-part handle)))
2924                  (if (or (mm-add-meta-html-tag handle charset)
2925                          (not file))
2926                      (mm-write-region (point-min) (point-max)
2927                                       tmp-file nil nil nil 'binary t)
2928                    (setq tmp-file nil))))
2929               (tmp-file
2930                (mm-save-part-to-file handle tmp-file)))
2931              (when tmp-file
2932                (add-to-list 'gnus-article-browse-html-temp-list tmp-file))
2933              (add-hook 'gnus-summary-prepare-exit-hook
2934                        'gnus-article-browse-delete-temp-files)
2935              (add-hook 'gnus-exit-gnus-hook
2936                        (lambda  ()
2937                          (gnus-article-browse-delete-temp-files t)))
2938              ;; FIXME: Warn if there's an <img> tag?
2939              (browse-url-of-file (or tmp-file (expand-file-name file)))
2940              (setq showed t))
2941             ;; If multipart, recurse
2942             ((equal (mm-handle-media-supertype handle) "multipart")
2943              (when (gnus-article-browse-html-parts handle header)
2944                (setq showed t)))
2945             ((equal (mm-handle-media-type handle) "message/rfc822")
2946              (mm-with-multibyte-buffer
2947                (mm-insert-part handle)
2948                (setq handle (mm-dissect-buffer t t))
2949                (when (and (bufferp (car handle))
2950                           (stringp (car (mm-handle-type handle))))
2951                  (setq handle (list handle)))
2952                (when header
2953                  (article-decode-encoded-words)
2954                  (let ((gnus-visible-headers
2955                         (or (get 'gnus-visible-headers 'standard-value)
2956                             gnus-visible-headers)))
2957                    (article-hide-headers))
2958                  (goto-char (point-min))
2959                  (search-forward "\n\n" nil 'move)
2960                  (skip-chars-backward "\t\n ")
2961                  (setq header (buffer-substring (point-min) (point)))))
2962              (when (prog1
2963                        (gnus-article-browse-html-parts handle header)
2964                      (mm-destroy-parts handle))
2965                (setq showed t)))))
2966     showed))
2967
2968 (defun gnus-article-browse-html-article (&optional arg)
2969   "View \"text/html\" parts of the current article with a WWW browser.
2970 The message header is added to the beginning of every html part unless
2971 the prefix argument ARG is given.
2972
2973 Warning: Spammers use links to images in HTML articles to verify
2974 whether you have read the message.  As
2975 `gnus-article-browse-html-article' passes the HTML content to the
2976 browser without eliminating these \"web bugs\" you should only
2977 use it for mails from trusted senders.
2978
2979 If you always want to display HTML parts in the browser, set
2980 `mm-text-html-renderer' to nil."
2981   ;; Cf. `mm-w3m-safe-url-regexp'
2982   (interactive "P")
2983   (if arg
2984       (gnus-summary-show-article)
2985     (let ((gnus-visible-headers (or (get 'gnus-visible-headers 'standard-value)
2986                                     gnus-visible-headers))
2987           ;; As we insert a <hr>, there's no need for the body boundary.
2988           (gnus-treat-body-boundary nil))
2989       (gnus-summary-show-article)))
2990   (with-current-buffer gnus-article-buffer
2991     (let ((header (unless arg
2992                     (save-restriction
2993                       (widen)
2994                       (buffer-substring-no-properties
2995                        (goto-char (point-min))
2996                        (if (search-forward "\n\n" nil t)
2997                            (match-beginning 0)
2998                          (goto-char (point-max))
2999                          (skip-chars-backward "\t\n ")
3000                          (point))))))
3001           parts)
3002       (set-buffer gnus-original-article-buffer)
3003       (setq parts (mm-dissect-buffer t t))
3004       ;; If singlepart, enforce a list.
3005       (when (and (bufferp (car parts))
3006                  (stringp (car (mm-handle-type parts))))
3007         (setq parts (list parts)))
3008       ;; Process the list
3009       (unless (gnus-article-browse-html-parts parts header)
3010         (gnus-error 3 "Mail doesn't contain a \"text/html\" part!"))
3011       (mm-destroy-parts parts)
3012       (unless arg
3013         (gnus-summary-show-article)))))
3014
3015 (defun article-hide-list-identifiers ()
3016   "Remove list identifies from the Subject header.
3017 The `gnus-list-identifiers' variable specifies what to do."
3018   (interactive)
3019   (let ((inhibit-point-motion-hooks t)
3020         (regexp (if (consp gnus-list-identifiers)
3021                     (mapconcat 'identity gnus-list-identifiers " *\\|")
3022                   gnus-list-identifiers))
3023         (inhibit-read-only t))
3024     (when regexp
3025       (save-excursion
3026         (save-restriction
3027           (article-narrow-to-head)
3028           (goto-char (point-min))
3029           (while (re-search-forward
3030                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
3031                   nil t)
3032             (delete-region (match-beginning 2) (match-end 0))
3033             (beginning-of-line))
3034           (when (re-search-forward
3035                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
3036             (delete-region (match-beginning 1) (match-end 1))))))))
3037
3038 (defun article-hide-pem (&optional arg)
3039   "Toggle hiding of any PEM headers and signatures in the current article.
3040 If given a negative prefix, always show; if given a positive prefix,
3041 always hide."
3042   (interactive (gnus-article-hidden-arg))
3043   (unless (gnus-article-check-hidden-text 'pem arg)
3044     (save-excursion
3045       (let ((inhibit-read-only t) end)
3046         (goto-char (point-min))
3047         ;; Hide the horrendously ugly "header".
3048         (when (and (search-forward
3049                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
3050                     nil t)
3051                    (setq end (1+ (match-beginning 0))))
3052           (gnus-add-wash-type 'pem)
3053           (gnus-article-hide-text-type
3054            end
3055            (if (search-forward "\n\n" nil t)
3056                (match-end 0)
3057              (point-max))
3058            'pem)
3059           ;; Hide the trailer as well
3060           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
3061                                 nil t)
3062             (gnus-article-hide-text-type
3063              (match-beginning 0) (match-end 0) 'pem)))))))
3064
3065 (defun article-strip-banner ()
3066   "Strip the banners specified by the `banner' group parameter and by
3067 `gnus-article-address-banner-alist'."
3068   (interactive)
3069   (save-excursion
3070     (save-restriction
3071       (let ((inhibit-point-motion-hooks t))
3072         (when (gnus-parameter-banner gnus-newsgroup-name)
3073           (article-really-strip-banner
3074            (gnus-parameter-banner gnus-newsgroup-name)))
3075         (when gnus-article-address-banner-alist
3076           ;; Note that the From header is decoded here, so it is
3077           ;; required that the *-extract-address-components function
3078           ;; supports non-ASCII text.
3079           (let ((from (save-restriction
3080                         (widen)
3081                         (article-narrow-to-head)
3082                         (mail-fetch-field "from"))))
3083             (when (and from
3084                        (setq from
3085                              (cadr (funcall gnus-extract-address-components
3086                                             from))))
3087               (catch 'found
3088                 (dolist (pair gnus-article-address-banner-alist)
3089                   (when (string-match (car pair) from)
3090                     (throw 'found
3091                            (article-really-strip-banner (cdr pair)))))))))))))
3092
3093 (defun article-really-strip-banner (banner)
3094   "Strip the banner specified by the argument."
3095   (save-excursion
3096     (save-restriction
3097       (let ((inhibit-point-motion-hooks t)
3098             (gnus-signature-limit nil)
3099             (inhibit-read-only t))
3100         (article-goto-body)
3101         (cond
3102          ((eq banner 'signature)
3103           (when (gnus-article-narrow-to-signature)
3104             (widen)
3105             (forward-line -1)
3106             (delete-region (point) (point-max))))
3107          ((symbolp banner)
3108           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
3109               (while (re-search-forward banner nil t)
3110                 (delete-region (match-beginning 0) (match-end 0)))))
3111          ((stringp banner)
3112           (while (re-search-forward banner nil t)
3113             (delete-region (match-beginning 0) (match-end 0)))))))))
3114
3115 (defun article-babel ()
3116   "Translate article using an online translation service."
3117   (interactive)
3118   (require 'babel)
3119   (gnus-with-article-buffer
3120     (when (article-goto-body)
3121       (let* ((start (point))
3122              (end (point-max))
3123              (orig (buffer-substring start end))
3124              (trans (babel-as-string orig)))
3125         (save-restriction
3126           (narrow-to-region start end)
3127           (delete-region start end)
3128           (insert trans))))))
3129
3130 (defun article-hide-signature (&optional arg)
3131   "Hide the signature in the current article.
3132 If given a negative prefix, always show; if given a positive prefix,
3133 always hide."
3134   (interactive (gnus-article-hidden-arg))
3135   (unless (gnus-article-check-hidden-text 'signature arg)
3136     (save-excursion
3137       (save-restriction
3138         (let ((inhibit-read-only t))
3139           (when (gnus-article-narrow-to-signature)
3140             (gnus-article-hide-text-type
3141              (point-min) (point-max) 'signature))))))
3142   (gnus-set-mode-line 'article))
3143
3144 (defun article-strip-headers-in-body ()
3145   "Strip offensive headers from bodies."
3146   (interactive)
3147   (save-excursion
3148     (article-goto-body)
3149     (let ((case-fold-search t))
3150       (when (looking-at "x-no-archive:")
3151         (gnus-delete-line)))))
3152
3153 (defun article-strip-leading-blank-lines ()
3154   "Remove all blank lines from the beginning of the article."
3155   (interactive)
3156   (save-excursion
3157     (let ((inhibit-point-motion-hooks t)
3158           (inhibit-read-only t))
3159       (when (article-goto-body)
3160         (while (and (not (eobp))
3161                     (looking-at "[ \t]*$"))
3162           (gnus-delete-line))))))
3163
3164 (defun article-narrow-to-head ()
3165   "Narrow the buffer to the head of the message.
3166 Point is left at the beginning of the narrowed-to region."
3167   (narrow-to-region
3168    (goto-char (point-min))
3169    (if (search-forward "\n\n" nil 1)
3170        (1- (point))
3171      (point-max)))
3172   (goto-char (point-min)))
3173
3174 (defun article-goto-body ()
3175   "Place point at the start of the body."
3176   (goto-char (point-min))
3177   (cond
3178    ;; This variable is only bound when dealing with separate
3179    ;; MIME body parts.
3180    (article-goto-body-goes-to-point-min-p
3181     t)
3182    ((search-forward "\n\n" nil t)
3183     t)
3184    (t
3185     (goto-char (point-max))
3186     nil)))
3187
3188 (defun article-strip-multiple-blank-lines ()
3189   "Replace consecutive blank lines with one empty line."
3190   (interactive)
3191   (save-excursion
3192     (let ((inhibit-point-motion-hooks t)
3193           (inhibit-read-only t))
3194       ;; First make all blank lines empty.
3195       (article-goto-body)
3196       (while (re-search-forward "^[ \t]+$" nil t)
3197         (unless (gnus-annotation-in-region-p
3198                  (match-beginning 0) (match-end 0))
3199           (replace-match "" nil t)))
3200       ;; Then replace multiple empty lines with a single empty line.
3201       (article-goto-body)
3202       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
3203         (unless (gnus-annotation-in-region-p
3204                  (match-beginning 0) (match-end 0))
3205           (delete-region (match-beginning 1) (match-end 1)))))))
3206
3207 (defun article-strip-leading-space ()
3208   "Remove all white space from the beginning of the lines in the article."
3209   (interactive)
3210   (save-excursion
3211     (let ((inhibit-point-motion-hooks t)
3212           (inhibit-read-only t))
3213       (article-goto-body)
3214       (while (re-search-forward "^[ \t]+" nil t)
3215         (replace-match "" t t)))))
3216
3217 (defun article-strip-trailing-space ()
3218   "Remove all white space from the end of the lines in the article."
3219   (interactive)
3220   (save-excursion
3221     (let ((inhibit-point-motion-hooks t)
3222           (inhibit-read-only t))
3223       (article-goto-body)
3224       (while (re-search-forward "[ \t]+$" nil t)
3225         (replace-match "" t t)))))
3226
3227 (defun article-strip-blank-lines ()
3228   "Strip leading, trailing and multiple blank lines."
3229   (interactive)
3230   (article-strip-leading-blank-lines)
3231   (article-remove-trailing-blank-lines)
3232   (article-strip-multiple-blank-lines))
3233
3234 (defun article-strip-all-blank-lines ()
3235   "Strip all blank lines."
3236   (interactive)
3237   (save-excursion
3238     (let ((inhibit-point-motion-hooks t)
3239           (inhibit-read-only t))
3240       (article-goto-body)
3241       (while (re-search-forward "^[ \t]*\n" nil t)
3242         (replace-match "" t t)))))
3243
3244 (defun gnus-article-narrow-to-signature ()
3245   "Narrow to the signature; return t if a signature is found, else nil."
3246   (let ((inhibit-point-motion-hooks t))
3247     (when (gnus-article-search-signature)
3248       (forward-line 1)
3249       ;; Check whether we have some limits to what we consider
3250       ;; to be a signature.
3251       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
3252                       (list gnus-signature-limit)))
3253             limit limited)
3254         (while (setq limit (pop limits))
3255           (if (or (and (integerp limit)
3256                        (< (- (point-max) (point)) limit))
3257                   (and (floatp limit)
3258                        (< (count-lines (point) (point-max)) limit))
3259                   (and (functionp limit)
3260                        (funcall limit))
3261                   (and (stringp limit)
3262                        (not (re-search-forward limit nil t))))
3263               ()                        ; This limit did not succeed.
3264             (setq limited t
3265                   limits nil)))
3266         (unless limited
3267           (narrow-to-region (point) (point-max))
3268           t)))))
3269
3270 (defun gnus-article-search-signature ()
3271   "Search the current buffer for the signature separator.
3272 Put point at the beginning of the signature separator."
3273   (let ((cur (point)))
3274     (goto-char (point-max))
3275     (if (if (stringp gnus-signature-separator)
3276             (re-search-backward gnus-signature-separator nil t)
3277           (let ((seps gnus-signature-separator))
3278             (while (and seps
3279                         (not (re-search-backward (car seps) nil t)))
3280               (pop seps))
3281             seps))
3282         t
3283       (goto-char cur)
3284       nil)))
3285
3286 (defun gnus-article-hidden-arg ()
3287   "Return the current prefix arg as a number, or 0 if no prefix."
3288   (list (if current-prefix-arg
3289             (prefix-numeric-value current-prefix-arg)
3290           0)))
3291
3292 (defun gnus-article-check-hidden-text (type arg)
3293   "Return nil if hiding is necessary.
3294 Arg can be nil or a number.  nil and positive means hide, negative
3295 means show, 0 means toggle."
3296   (save-excursion
3297     (save-restriction
3298       (let ((hide (gnus-article-hidden-text-p type)))
3299         (cond
3300          ((or (null arg)
3301               (> arg 0))
3302           nil)
3303          ((< arg 0)
3304           (gnus-article-show-hidden-text type)
3305           t)
3306          (t
3307           (if (eq hide 'hidden)
3308               (progn
3309                 (gnus-article-show-hidden-text type)
3310                 t)
3311             nil)))))))
3312
3313 (defun gnus-article-hidden-text-p (type)
3314   "Say whether the current buffer contains hidden text of type TYPE."
3315   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3316     (while (and pos
3317                 (not (get-text-property pos 'invisible))
3318                 (not (get-text-property pos 'dummy-invisible)))
3319       (setq pos
3320             (text-property-any (1+ pos) (point-max) 'article-type type)))
3321     (if pos
3322         'hidden
3323       nil)))
3324
3325 (defun gnus-article-show-hidden-text (type &optional dummy)
3326   "Show all hidden text of type TYPE.
3327 Originally it is hide instead of DUMMY."
3328   (let ((inhibit-read-only t)
3329         (inhibit-point-motion-hooks t))
3330     (gnus-remove-text-properties-when
3331      'article-type type
3332      (point-min) (point-max)
3333      (cons 'article-type (cons type
3334                                gnus-hidden-properties)))
3335     (gnus-delete-wash-type type)))
3336
3337 (defconst article-time-units
3338   `((year . ,(* 365.25 24 60 60))
3339     (week . ,(* 7 24 60 60))
3340     (day . ,(* 24 60 60))
3341     (hour . ,(* 60 60))
3342     (minute . 60)
3343     (second . 1))
3344   "Mapping from time units to seconds.")
3345
3346 (defun gnus-article-forward-header ()
3347   "Move point to the start of the next header.
3348 If the current header is a continuation header, this can be several
3349 lines forward."
3350   (let ((ended nil))
3351     (while (not ended)
3352       (forward-line 1)
3353       (if (looking-at "[ \t]+[^ \t]")
3354           (forward-line 1)
3355         (setq ended t)))))
3356
3357 (defun article-date-ut (&optional type highlight)
3358   "Convert DATE date to universal time in the current article.
3359 If TYPE is `local', convert to local time; if it is `lapsed', output
3360 how much time has lapsed since DATE.  For `lapsed', the value of
3361 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
3362 should replace the \"Date:\" one, or should be added below it."
3363   (interactive (list 'ut t))
3364   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
3365          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
3366                              tdate-regexp)
3367                             ((eq type 'lapsed)
3368                              "^X-Sent:[ \t]")
3369                             (article-lapsed-timer
3370                              "^Date:[ \t]")
3371                             (t
3372                              tdate-regexp)))
3373          (case-fold-search t)
3374          (inhibit-read-only t)
3375          (inhibit-point-motion-hooks t)
3376          pos date bface eface)
3377     (save-excursion
3378       (save-restriction
3379         (widen)
3380         (goto-char (point-min))
3381         (while (or (setq date (get-text-property (setq pos (point))
3382                                                  'original-date))
3383                    (when (setq pos (next-single-property-change
3384                                     (point) 'original-date))
3385                      (setq date (get-text-property pos 'original-date))
3386                      t))
3387           (narrow-to-region pos (or (text-property-any pos (point-max)
3388                                                        'original-date nil)
3389                                     (point-max)))
3390           (goto-char (point-min))
3391           (when (re-search-forward tdate-regexp nil t)
3392             (setq bface (get-text-property (point-at-bol) 'face)
3393                   eface (get-text-property (1- (point-at-eol)) 'face)))
3394           (goto-char (point-min))
3395           (setq pos nil)
3396           ;; Delete any old Date headers.
3397           (while (re-search-forward date-regexp nil t)
3398             (if pos
3399                 (delete-region (point-at-bol) (progn
3400                                                 (gnus-article-forward-header)
3401                                                 (point)))
3402               (delete-region (point-at-bol) (progn
3403                                               (gnus-article-forward-header)
3404                                               (forward-char -1)
3405                                               (point)))
3406               (setq pos (point))))
3407           (when (and (not pos)
3408                      (re-search-forward tdate-regexp nil t))
3409             (forward-line 1))
3410           (gnus-goto-char pos)
3411           (insert (article-make-date-line date (or type 'ut)))
3412           (unless pos
3413             (insert "\n")
3414             (forward-line -1))
3415           ;; Do highlighting.
3416           (beginning-of-line)
3417           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3418             (put-text-property (match-beginning 1) (1+ (match-end 1))
3419                                'face bface)
3420             (put-text-property (match-beginning 2) (match-end 2)
3421                                'face eface))
3422           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3423           (goto-char (point-max))
3424           (widen))))))
3425
3426 (defun article-make-date-line (date type)
3427   "Return a DATE line of TYPE."
3428   (unless (memq type '(local ut original user iso8601 lapsed english))
3429     (error "Unknown conversion type: %s" type))
3430   (condition-case ()
3431       (let ((time (date-to-time date)))
3432         (cond
3433          ;; Convert to the local timezone.
3434          ((eq type 'local)
3435           (concat "Date: " (message-make-date time)))
3436          ;; Convert to Universal Time.
3437          ((eq type 'ut)
3438           (concat "Date: "
3439                   (substring
3440                    (message-make-date
3441                     (let* ((e (parse-time-string date))
3442                            (tm (apply 'encode-time e))
3443                            (ms (car tm))
3444                            (ls (- (cadr tm) (car (current-time-zone time)))))
3445                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3446                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3447                             (t (list ms ls)))))
3448                    0 -5)
3449                   "UT"))
3450          ;; Get the original date from the article.
3451          ((eq type 'original)
3452           (concat "Date: " (if (string-match "\n+$" date)
3453                                (substring date 0 (match-beginning 0))
3454                              date)))
3455          ;; Let the user define the format.
3456          ((eq type 'user)
3457           (let ((format (or (condition-case nil
3458                                 (with-current-buffer gnus-summary-buffer
3459                                   gnus-article-time-format)
3460                               (error nil))
3461                             gnus-article-time-format)))
3462             (if (functionp format)
3463                 (funcall format time)
3464               (concat "Date: " (format-time-string format time)))))
3465          ;; ISO 8601.
3466          ((eq type 'iso8601)
3467           (let ((tz (car (current-time-zone time))))
3468             (concat
3469              "Date: "
3470              (format-time-string "%Y%m%dT%H%M%S" time)
3471              (format "%s%02d%02d"
3472                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3473                      (/ (% (abs tz) 3600) 60)))))
3474          ;; Do an X-Sent lapsed format.
3475          ((eq type 'lapsed)
3476           ;; If the date is seriously mangled, the timezone functions are
3477           ;; liable to bug out, so we ignore all errors.
3478           (let* ((now (current-time))
3479                  (real-time (subtract-time now time))
3480                  (real-sec (and real-time
3481                                 (+ (* (float (car real-time)) 65536)
3482                                    (cadr real-time))))
3483                  (sec (and real-time (abs real-sec)))
3484                  num prev)
3485             (cond
3486              ((null real-time)
3487               "X-Sent: Unknown")
3488              ((zerop sec)
3489               "X-Sent: Now")
3490              (t
3491               (concat
3492                "X-Sent: "
3493                ;; This is a bit convoluted, but basically we go
3494                ;; through the time units for years, weeks, etc,
3495                ;; and divide things to see whether that results
3496                ;; in positive answers.
3497                (mapconcat
3498                 (lambda (unit)
3499                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3500                       ;; The (remaining) seconds are too few to
3501                       ;; be divided into this time unit.
3502                       ""
3503                     ;; It's big enough, so we output it.
3504                     (setq sec (- sec (* num (cdr unit))))
3505                     (prog1
3506                         (concat (if prev ", " "") (int-to-string
3507                                                    (floor num))
3508                                 " " (symbol-name (car unit))
3509                                 (if (> num 1) "s" ""))
3510                       (setq prev t))))
3511                 article-time-units "")
3512                ;; If dates are odd, then it might appear like the
3513                ;; article was sent in the future.
3514                (if (> real-sec 0)
3515                    " ago"
3516                  " in the future"))))))
3517          ;; Display the date in proper English
3518          ((eq type 'english)
3519           (let ((dtime (decode-time time)))
3520             (concat
3521              "Date: the "
3522              (number-to-string (nth 3 dtime))
3523              (let ((digit (% (nth 3 dtime) 10)))
3524                (cond
3525                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3526                 ((= digit 1) "st")
3527                 ((= digit 2) "nd")
3528                 ((= digit 3) "rd")
3529                 (t "th")))
3530              " of "
3531              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3532              " "
3533              (number-to-string (nth 5 dtime))
3534              " at "
3535              (format "%02d" (nth 2 dtime))
3536              ":"
3537              (format "%02d" (nth 1 dtime)))))))
3538     (error
3539      (format "Date: %s (from Gnus)" date))))
3540
3541 (defun article-date-local (&optional highlight)
3542   "Convert the current article date to the local timezone."
3543   (interactive (list t))
3544   (article-date-ut 'local highlight))
3545
3546 (defun article-date-english (&optional highlight)
3547   "Convert the current article date to something that is proper English."
3548   (interactive (list t))
3549   (article-date-ut 'english highlight))
3550
3551 (defun article-date-original (&optional highlight)
3552   "Convert the current article date to what it was originally.
3553 This is only useful if you have used some other date conversion
3554 function and want to see what the date was before converting."
3555   (interactive (list t))
3556   (article-date-ut 'original highlight))
3557
3558 (defun article-date-lapsed (&optional highlight)
3559   "Convert the current article date to time lapsed since it was sent."
3560   (interactive (list t))
3561   (article-date-ut 'lapsed highlight))
3562
3563 (defun article-update-date-lapsed ()
3564   "Function to be run from a timer to update the lapsed time line."
3565   (save-match-data
3566     (let (deactivate-mark)
3567       (save-excursion
3568         (ignore-errors
3569          (walk-windows
3570           (lambda (w)
3571             (set-buffer (window-buffer w))
3572             (when (eq major-mode 'gnus-article-mode)
3573               (let ((mark (point-marker)))
3574                 (goto-char (point-min))
3575                 (when (re-search-forward "^X-Sent:" nil t)
3576                   (article-date-lapsed t))
3577                 (goto-char (marker-position mark))
3578                 (move-marker mark nil))))
3579           nil 'visible))))))
3580
3581 (defun gnus-start-date-timer (&optional n)
3582   "Start a timer to update the X-Sent header in the article buffers.
3583 The numerical prefix says how frequently (in seconds) the function
3584 is to run."
3585   (interactive "p")
3586   (unless n
3587     (setq n 1))
3588   (gnus-stop-date-timer)
3589   (setq article-lapsed-timer
3590         (run-at-time 1 n 'article-update-date-lapsed)))
3591
3592 (defun gnus-stop-date-timer ()
3593   "Stop the X-Sent timer."
3594   (interactive)
3595   (when article-lapsed-timer
3596     (nnheader-cancel-timer article-lapsed-timer)
3597     (setq article-lapsed-timer nil)))
3598
3599 (defun article-date-user (&optional highlight)
3600   "Convert the current article date to the user-defined format.
3601 This format is defined by the `gnus-article-time-format' variable."
3602   (interactive (list t))
3603   (article-date-ut 'user highlight))
3604
3605 (defun article-date-iso8601 (&optional highlight)
3606   "Convert the current article date to ISO8601."
3607   (interactive (list t))
3608   (article-date-ut 'iso8601 highlight))
3609
3610 (defmacro gnus-article-save-original-date (&rest forms)
3611   "Save the original date as a text property and evaluate FORMS."
3612   `(let* ((case-fold-search t)
3613           (start (progn
3614                    (goto-char (point-min))
3615                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3616                               (not (bolp)))
3617                      (match-end 0))))
3618           (date (when (and start
3619                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3620                                               nil t))
3621                   (buffer-substring-no-properties start
3622                                                   (match-beginning 0)))))
3623      (goto-char (point-max))
3624      (skip-chars-backward "\n")
3625      (put-text-property (point-min) (point) 'original-date date)
3626      ,@forms
3627      (goto-char (point-max))
3628      (skip-chars-backward "\n")
3629      (put-text-property (point-min) (point) 'original-date date)))
3630
3631 ;; (defun article-show-all ()
3632 ;;   "Show all hidden text in the article buffer."
3633 ;;   (interactive)
3634 ;;   (save-excursion
3635 ;;     (let ((inhibit-read-only t))
3636 ;;       (gnus-article-unhide-text (point-min) (point-max)))))
3637
3638 (defun article-remove-leading-whitespace ()
3639   "Remove excessive whitespace from all headers."
3640   (interactive)
3641   (save-excursion
3642     (save-restriction
3643       (let ((inhibit-read-only t))
3644         (article-narrow-to-head)
3645         (goto-char (point-min))
3646         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3647           (delete-region (match-beginning 1) (match-end 1)))))))
3648
3649 (defun article-emphasize (&optional arg)
3650   "Emphasize text according to `gnus-emphasis-alist'."
3651   (interactive (gnus-article-hidden-arg))
3652   (unless (gnus-article-check-hidden-text 'emphasis arg)
3653     (save-excursion
3654       (let ((alist (or
3655                     (condition-case nil
3656                         (with-current-buffer gnus-summary-buffer
3657                           gnus-article-emphasis-alist)
3658                       (error))
3659                     gnus-emphasis-alist))
3660             (inhibit-read-only t)
3661             (props (append '(article-type emphasis)
3662                            gnus-hidden-properties))
3663             regexp elem beg invisible visible face)
3664         (article-goto-body)
3665         (setq beg (point))
3666         (while (setq elem (pop alist))
3667           (goto-char beg)
3668           (setq regexp (car elem)
3669                 invisible (nth 1 elem)
3670                 visible (nth 2 elem)
3671                 face (nth 3 elem))
3672           (while (re-search-forward regexp nil t)
3673             (when (and (match-beginning visible) (match-beginning invisible))
3674               (gnus-article-hide-text
3675                (match-beginning invisible) (match-end invisible) props)
3676               (gnus-article-unhide-text-type
3677                (match-beginning visible) (match-end visible) 'emphasis)
3678               (gnus-put-overlay-excluding-newlines
3679                (match-beginning visible) (match-end visible) 'face face)
3680               (gnus-add-wash-type 'emphasis)
3681               (goto-char (match-end invisible)))))))))
3682
3683 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3684   "Setup newsgroup emphasis alist."
3685   (unless gnus-article-emphasis-alist
3686     (let ((name (and gnus-newsgroup-name
3687                      (gnus-group-real-name gnus-newsgroup-name))))
3688       (make-local-variable 'gnus-article-emphasis-alist)
3689       (setq gnus-article-emphasis-alist
3690             (nconc
3691              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3692                (while (setq elem (pop alist))
3693                  (when (and name (string-match (car elem) name))
3694                    (setq alist nil
3695                          highlight (copy-sequence (cdr elem)))))
3696                highlight)
3697              (copy-sequence highlight-words)
3698              (if gnus-newsgroup-name
3699                  (copy-sequence (gnus-group-find-parameter
3700                                  gnus-newsgroup-name 'highlight-words t)))
3701              gnus-emphasis-alist)))))
3702
3703 (defvar gnus-summary-article-menu)
3704 (defvar gnus-summary-post-menu)
3705
3706 ;;; Saving functions.
3707
3708 (defun gnus-article-save (save-buffer file &optional num)
3709   "Save the currently selected article."
3710   (when (or (get gnus-default-article-saver :headers)
3711             (not gnus-save-all-headers))
3712     ;; Remove headers according to `gnus-saved-headers' or the value
3713     ;; of the `:headers' property that the saver function might have.
3714     (let ((gnus-visible-headers
3715            (or (symbol-value (get gnus-default-article-saver :headers))
3716                gnus-saved-headers gnus-visible-headers))
3717           (gnus-article-buffer save-buffer))
3718       (with-current-buffer save-buffer
3719         (article-hide-headers 1 t))))
3720   (save-window-excursion
3721     (if (not gnus-default-article-saver)
3722         (error "No default saver is defined")
3723       ;; !!! Magic!  The saving functions all save
3724       ;; `gnus-save-article-buffer' (or so they think), but we
3725       ;; bind that variable to our save-buffer.
3726       (set-buffer gnus-article-buffer)
3727       (let* ((gnus-save-article-buffer save-buffer)
3728              (filename
3729               (cond
3730                ((not gnus-prompt-before-saving) 'default)
3731                ((eq gnus-prompt-before-saving 'always) nil)
3732                (t file)))
3733              (gnus-number-of-articles-to-be-saved
3734               (when (eq gnus-prompt-before-saving t)
3735                 num)))                  ; Magic
3736         (set-buffer gnus-article-current-summary)
3737         (funcall gnus-default-article-saver filename)))))
3738
3739 (defun gnus-read-save-file-name (prompt &optional filename
3740                                         function group headers variable
3741                                         dir-var)
3742   (let ((default-name
3743           (funcall function group headers (symbol-value variable)))
3744         result)
3745     (setq result
3746           (expand-file-name
3747            (cond
3748             ((eq filename 'default)
3749              default-name)
3750             ((eq filename t)
3751              default-name)
3752             (filename filename)
3753             (t
3754              (when (symbol-value dir-var)
3755                (setq default-name (expand-file-name
3756                                    (file-name-nondirectory default-name)
3757                                    (symbol-value dir-var))))
3758              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3759                     (prompt
3760                      (format prompt
3761                              (if (and gnus-number-of-articles-to-be-saved
3762                                       (> gnus-number-of-articles-to-be-saved 1))
3763                                  (format "these %d articles"
3764                                          gnus-number-of-articles-to-be-saved)
3765                                "this article")))
3766                     (file
3767                      ;; Let the split methods have their say.
3768                      (cond
3769                       ;; No split name was found.
3770                       ((null split-name)
3771                        (read-file-name
3772                         (concat prompt " (default "
3773                                 (file-name-nondirectory default-name) "): ")
3774                         (file-name-directory default-name)
3775                         default-name))
3776                       ;; A single group name is returned.
3777                       ((stringp split-name)
3778                        (setq default-name
3779                              (funcall function split-name headers
3780                                       (symbol-value variable)))
3781                        (read-file-name
3782                         (concat prompt " (default "
3783                                 (file-name-nondirectory default-name) "): ")
3784                         (file-name-directory default-name)
3785                         default-name))
3786                       ;; A single split name was found
3787                       ((= 1 (length split-name))
3788                        (let* ((name (expand-file-name
3789                                      (car split-name)
3790                                      gnus-article-save-directory))
3791                               (dir (cond ((file-directory-p name)
3792                                           (file-name-as-directory name))
3793                                          ((file-exists-p name) name)
3794                                          (t gnus-article-save-directory))))
3795                          (read-file-name
3796                           (concat prompt " (default " name "): ")
3797                           dir name)))
3798                       ;; A list of splits was found.
3799                       (t
3800                        (setq split-name (nreverse split-name))
3801                        (let (result)
3802                          (let ((file-name-history
3803                                 (nconc split-name file-name-history)))
3804                            (setq result
3805                                  (expand-file-name
3806                                   (read-file-name
3807                                    (concat prompt " (`M-p' for defaults): ")
3808                                    gnus-article-save-directory
3809                                    (car split-name))
3810                                   gnus-article-save-directory)))
3811                          (car (push result file-name-history)))))))
3812                ;; Create the directory.
3813                (gnus-make-directory (file-name-directory file))
3814                ;; If we have read a directory, we append the default file name.
3815                (when (file-directory-p file)
3816                  (setq file (expand-file-name (file-name-nondirectory
3817                                                default-name)
3818                                               (file-name-as-directory file))))
3819                ;; Possibly translate some characters.
3820                (nnheader-translate-file-chars file))))))
3821     (gnus-make-directory (file-name-directory result))
3822     (when variable
3823       (set variable result))
3824     (when dir-var
3825       (set dir-var (file-name-directory result)))
3826     result))
3827
3828 (defun gnus-article-archive-name (group)
3829   "Return the first instance of an \"Archive-name\" in the current buffer."
3830   (let ((case-fold-search t))
3831     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3832       (nnheader-concat gnus-article-save-directory
3833                        (match-string 1)))))
3834
3835 (defun gnus-article-nndoc-name (group)
3836   "If GROUP is an nndoc group, return the name of the parent group."
3837   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3838     (gnus-group-get-parameter group 'save-article-group)))
3839
3840 (defun gnus-summary-save-in-rmail (&optional filename)
3841   "Append this article to Rmail file.
3842 Optional argument FILENAME specifies file name.
3843 Directory to save to is default to `gnus-article-save-directory'."
3844   (setq filename (gnus-read-save-file-name
3845                   "Save %s in rmail file" filename
3846                   gnus-rmail-save-name gnus-newsgroup-name
3847                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3848   (gnus-eval-in-buffer-window gnus-save-article-buffer
3849     (save-excursion
3850       (save-restriction
3851         (widen)
3852         (gnus-output-to-rmail filename))))
3853   filename)
3854
3855 (defun gnus-summary-save-in-mail (&optional filename)
3856   "Append this article to Unix mail file.
3857 Optional argument FILENAME specifies file name.
3858 Directory to save to is default to `gnus-article-save-directory'."
3859   (setq filename (gnus-read-save-file-name
3860                   "Save %s in Unix mail file" filename
3861                   gnus-mail-save-name gnus-newsgroup-name
3862                   gnus-current-headers 'gnus-newsgroup-last-mail))
3863   (gnus-eval-in-buffer-window gnus-save-article-buffer
3864     (save-excursion
3865       (save-restriction
3866         (widen)
3867         (if (and (file-readable-p filename)
3868                  (file-regular-p filename)
3869                  (mail-file-babyl-p filename))
3870             (rmail-output-to-rmail-file filename t)
3871           (gnus-output-to-mail filename)))))
3872   filename)
3873
3874 (put 'gnus-summary-save-in-file :decode t)
3875 (put 'gnus-summary-save-in-file :headers 'gnus-saved-headers)
3876 (defun gnus-summary-save-in-file (&optional filename overwrite)
3877   "Append this article to file.
3878 Optional argument FILENAME specifies file name.
3879 Directory to save to is default to `gnus-article-save-directory'."
3880   (setq filename (gnus-read-save-file-name
3881                   "Save %s in file" filename
3882                   gnus-file-save-name gnus-newsgroup-name
3883                   gnus-current-headers 'gnus-newsgroup-last-file))
3884   (gnus-eval-in-buffer-window gnus-save-article-buffer
3885     (save-excursion
3886       (save-restriction
3887         (widen)
3888         (when (and overwrite
3889                    (file-exists-p filename))
3890           (delete-file filename))
3891         (gnus-output-to-file filename))))
3892   filename)
3893
3894 (put 'gnus-summary-write-to-file :decode t)
3895 (put 'gnus-summary-write-to-file :function 'gnus-summary-save-in-file)
3896 (put 'gnus-summary-write-to-file :headers 'gnus-saved-headers)
3897 (defun gnus-summary-write-to-file (&optional filename)
3898   "Write this article to a file, overwriting it if the file exists.
3899 Optional argument FILENAME specifies file name.
3900 The directory to save in defaults to `gnus-article-save-directory'."
3901   (setq filename (gnus-read-save-file-name
3902                   "Save %s in file" filename
3903                   gnus-file-save-name gnus-newsgroup-name
3904                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
3905   (gnus-summary-save-in-file filename t))
3906
3907 (put 'gnus-summary-save-body-in-file :decode t)
3908 (defun gnus-summary-save-body-in-file (&optional filename overwrite)
3909   "Append this article body to a file.
3910 Optional argument FILENAME specifies file name.
3911 The directory to save in defaults to `gnus-article-save-directory'."
3912   (setq filename (gnus-read-save-file-name
3913                   "Save %s body in file" filename
3914                   gnus-file-save-name gnus-newsgroup-name
3915                   gnus-current-headers 'gnus-newsgroup-last-file))
3916   (gnus-eval-in-buffer-window gnus-save-article-buffer
3917     (save-excursion
3918       (save-restriction
3919         (widen)
3920         (when (article-goto-body)
3921           (narrow-to-region (point) (point-max)))
3922         (when (and overwrite
3923                    (file-exists-p filename))
3924           (delete-file filename))
3925         (gnus-output-to-file filename))))
3926   filename)
3927
3928 (put 'gnus-summary-write-body-to-file :decode t)
3929 (put 'gnus-summary-write-body-to-file
3930      :function 'gnus-summary-save-body-in-file)
3931 (defun gnus-summary-write-body-to-file (&optional filename)
3932   "Write this article body to a file, overwriting it if the file exists.
3933 Optional argument FILENAME specifies file name.
3934 The directory to save in defaults to `gnus-article-save-directory'."
3935   (setq filename (gnus-read-save-file-name
3936                   "Save %s body in file" filename
3937                   gnus-file-save-name gnus-newsgroup-name
3938                   gnus-current-headers nil 'gnus-newsgroup-last-directory))
3939   (gnus-summary-save-body-in-file filename t))
3940
3941 (defun gnus-summary-save-in-pipe (&optional command)
3942   "Pipe this article to subprocess."
3943   (setq command
3944         (cond ((and (eq command 'default)
3945                     gnus-last-shell-command)
3946                gnus-last-shell-command)
3947               ((stringp command)
3948                command)
3949               (t (read-string
3950                   (format
3951                    "Shell command on %s: "
3952                    (if (and gnus-number-of-articles-to-be-saved
3953                             (> gnus-number-of-articles-to-be-saved 1))
3954                        (format "these %d articles"
3955                                gnus-number-of-articles-to-be-saved)
3956                      "this article"))
3957                   gnus-last-shell-command))))
3958   (when (string-equal command "")
3959     (if gnus-last-shell-command
3960         (setq command gnus-last-shell-command)
3961       (error "A command is required")))
3962   (gnus-eval-in-buffer-window gnus-article-buffer
3963     (save-restriction
3964       (widen)
3965       (shell-command-on-region (point-min) (point-max) command nil)))
3966   (setq gnus-last-shell-command command))
3967
3968 (defun gnus-summary-pipe-to-muttprint (&optional command)
3969   "Pipe this article to muttprint."
3970   (setq command (read-string
3971                  "Print using command: " gnus-summary-muttprint-program
3972                  nil gnus-summary-muttprint-program))
3973   (gnus-summary-save-in-pipe command))
3974
3975 ;;; Article file names when saving.
3976
3977 (defun gnus-capitalize-newsgroup (newsgroup)
3978   "Capitalize NEWSGROUP name."
3979   (when (not (zerop (length newsgroup)))
3980     (concat (char-to-string (upcase (aref newsgroup 0)))
3981             (substring newsgroup 1))))
3982
3983 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
3984   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3985 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
3986 Otherwise, it is like ~/News/news/group/num."
3987   (let ((default
3988           (expand-file-name
3989            (concat (if (gnus-use-long-file-name 'not-save)
3990                        (gnus-capitalize-newsgroup newsgroup)
3991                      (gnus-newsgroup-directory-form newsgroup))
3992                    "/" (int-to-string (mail-header-number headers)))
3993            gnus-article-save-directory)))
3994     (if (and last-file
3995              (string-equal (file-name-directory default)
3996                            (file-name-directory last-file))
3997              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3998         default
3999       (or last-file default))))
4000
4001 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
4002   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4003 If variable `gnus-use-long-file-name' is non-nil, it is
4004 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
4005   (let ((default
4006           (expand-file-name
4007            (concat (if (gnus-use-long-file-name 'not-save)
4008                        newsgroup
4009                      (gnus-newsgroup-directory-form newsgroup))
4010                    "/" (int-to-string (mail-header-number headers)))
4011            gnus-article-save-directory)))
4012     (if (and last-file
4013              (string-equal (file-name-directory default)
4014                            (file-name-directory last-file))
4015              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
4016         default
4017       (or last-file default))))
4018
4019 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
4020   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
4021 If variable `gnus-use-long-file-name' is non-nil, it is
4022 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
4023   (or last-file
4024       (expand-file-name
4025        (if (gnus-use-long-file-name 'not-save)
4026            newsgroup
4027          (file-relative-name
4028           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
4029           default-directory))
4030        gnus-article-save-directory)))
4031
4032 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
4033   "Generate file name from sender."
4034   (let ((from (mail-header-from headers)))
4035     (expand-file-name
4036      (if (and from (string-match "\\([^ <]+\\)@" from))
4037          (match-string 1 from)
4038        "nobody")
4039      gnus-article-save-directory)))
4040
4041 (defun article-verify-x-pgp-sig ()
4042   "Verify X-PGP-Sig."
4043   ;; <ftp://ftp.isc.org/pub/pgpcontrol/FORMAT>
4044   (interactive)
4045   (if (gnus-buffer-live-p gnus-original-article-buffer)
4046       (let ((sig (with-current-buffer gnus-original-article-buffer
4047                    (gnus-fetch-field "X-PGP-Sig")))
4048             items info headers)
4049         (when (and sig
4050                    mml2015-use
4051                    (mml2015-clear-verify-function))
4052           (with-temp-buffer
4053             (insert-buffer-substring gnus-original-article-buffer)
4054             (setq items (split-string sig))
4055             (message-narrow-to-head)
4056             (let ((inhibit-point-motion-hooks t)
4057                   (case-fold-search t))
4058               ;; Don't verify multiple headers.
4059               (setq headers (mapconcat (lambda (header)
4060                                          (concat header ": "
4061                                                  (mail-fetch-field header)
4062                                                  "\n"))
4063                                        (split-string (nth 1 items) ",") "")))
4064             (delete-region (point-min) (point-max))
4065             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
4066             (insert "X-Signed-Headers: " (nth 1 items) "\n")
4067             (insert headers)
4068             (widen)
4069             (forward-line)
4070             (while (not (eobp))
4071               (if (looking-at "^-")
4072                   (insert "- "))
4073               (forward-line))
4074             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
4075             (insert "Version: " (car items) "\n\n")
4076             (insert (mapconcat 'identity (cddr items) "\n"))
4077             (insert "\n-----END PGP SIGNATURE-----\n")
4078             (let ((mm-security-handle (list (format "multipart/signed"))))
4079               (mml2015-clean-buffer)
4080               (let ((coding-system-for-write (or gnus-newsgroup-charset
4081                                                  'iso-8859-1)))
4082                 (funcall (mml2015-clear-verify-function)))
4083               (setq info
4084                     (or (mm-handle-multipart-ctl-parameter
4085                          mm-security-handle 'gnus-details)
4086                         (mm-handle-multipart-ctl-parameter
4087                          mm-security-handle 'gnus-info)))))
4088           (when info
4089             (let ((inhibit-read-only t) bface eface)
4090               (save-restriction
4091                 (message-narrow-to-head)
4092                 (goto-char (point-max))
4093                 (forward-line -1)
4094                 (setq bface (get-text-property (point-at-bol) 'face)
4095                       eface (get-text-property (1- (point-at-eol)) 'face))
4096                 (message-remove-header "X-Gnus-PGP-Verify")
4097                 (if (re-search-forward "^X-PGP-Sig:" nil t)
4098                     (forward-line)
4099                   (goto-char (point-max)))
4100                 (narrow-to-region (point) (point))
4101                 (insert "X-Gnus-PGP-Verify: " info "\n")
4102                 (goto-char (point-min))
4103                 (forward-line)
4104                 (while (not (eobp))
4105                   (if (not (looking-at "^[ \t]"))
4106                       (insert " "))
4107                   (forward-line))
4108                 ;; Do highlighting.
4109                 (goto-char (point-min))
4110                 (when (looking-at "\\([^:]+\\): *")
4111                   (put-text-property (match-beginning 1) (1+ (match-end 1))
4112                                      'face bface)
4113                   (put-text-property (match-end 0) (point-max)
4114                                      'face eface)))))))))
4115
4116 (defun article-verify-cancel-lock ()
4117   "Verify Cancel-Lock header."
4118   (interactive)
4119   (if (gnus-buffer-live-p gnus-original-article-buffer)
4120       (canlock-verify gnus-original-article-buffer)))
4121
4122 (eval-and-compile
4123   (mapc
4124    (lambda (func)
4125      (let (afunc gfunc)
4126        (if (consp func)
4127            (setq afunc (car func)
4128                  gfunc (cdr func))
4129          (setq afunc func
4130                gfunc (intern (format "gnus-%s" func))))
4131        (defalias gfunc
4132          (when (fboundp afunc)
4133            `(lambda (&optional interactive &rest args)
4134               ,(documentation afunc t)
4135               (interactive (list t))
4136               (with-current-buffer gnus-article-buffer
4137                 (if interactive
4138                     (call-interactively ',afunc)
4139                   (apply ',afunc args))))))))
4140    '(article-hide-headers
4141      article-verify-x-pgp-sig
4142      article-verify-cancel-lock
4143      article-hide-boring-headers
4144      article-treat-overstrike
4145      article-treat-ansi-sequences
4146      article-fill-long-lines
4147      article-capitalize-sentences
4148      article-remove-cr
4149      article-remove-leading-whitespace
4150      article-display-x-face
4151      article-display-face
4152      article-de-quoted-unreadable
4153      article-de-base64-unreadable
4154      article-decode-HZ
4155      article-wash-html
4156      article-unsplit-urls
4157      article-hide-list-identifiers
4158      article-strip-banner
4159      article-babel
4160      article-hide-pem
4161      article-hide-signature
4162      article-strip-headers-in-body
4163      article-remove-trailing-blank-lines
4164      article-strip-leading-blank-lines
4165      article-strip-multiple-blank-lines
4166      article-strip-leading-space
4167      article-strip-trailing-space
4168      article-strip-blank-lines
4169      article-strip-all-blank-lines
4170      article-date-local
4171      article-date-english
4172      article-date-iso8601
4173      article-date-original
4174      article-date-ut
4175      article-decode-mime-words
4176      article-decode-charset
4177      article-decode-encoded-words
4178      article-date-user
4179      article-date-lapsed
4180      article-emphasize
4181      article-treat-dumbquotes
4182      article-normalize-headers
4183      ;;(article-show-all . gnus-article-show-all-headers)
4184      )))
4185 \f
4186 ;;;
4187 ;;; Gnus article mode
4188 ;;;
4189
4190 (put 'gnus-article-mode 'mode-class 'special)
4191
4192 (set-keymap-parent gnus-article-mode-map widget-keymap)
4193
4194 (gnus-define-keys gnus-article-mode-map
4195   " " gnus-article-goto-next-page
4196   "\177" gnus-article-goto-prev-page
4197   [delete] gnus-article-goto-prev-page
4198   [backspace] gnus-article-goto-prev-page
4199   "\C-c^" gnus-article-refer-article
4200   "h" gnus-article-show-summary
4201   "s" gnus-article-show-summary
4202   "\C-c\C-m" gnus-article-mail
4203   "?" gnus-article-describe-briefly
4204   "e" gnus-summary-edit-article
4205   "<" beginning-of-buffer
4206   ">" end-of-buffer
4207   "\C-c\C-i" gnus-info-find-node
4208   "\C-c\C-b" gnus-bug
4209   "R" gnus-article-reply-with-original
4210   "F" gnus-article-followup-with-original
4211   "\C-hk" gnus-article-describe-key
4212   "\C-hc" gnus-article-describe-key-briefly
4213   "\C-hb" gnus-article-describe-bindings
4214
4215   "\C-d" gnus-article-read-summary-keys
4216   "\M-*" gnus-article-read-summary-keys
4217   "\M-#" gnus-article-read-summary-keys
4218   "\M-^" gnus-article-read-summary-keys
4219   "\M-g" gnus-article-read-summary-keys)
4220
4221 (substitute-key-definition
4222  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
4223
4224 (gnus-define-keys (gnus-article-send-map "S" gnus-article-mode-map)
4225   "W" gnus-article-wide-reply-with-original)
4226 (if (featurep 'xemacs)
4227     (set-keymap-default-binding gnus-article-send-map
4228                                 'gnus-article-read-summary-send-keys)
4229   (define-key gnus-article-send-map [t] 'gnus-article-read-summary-send-keys))
4230
4231 (defun gnus-article-make-menu-bar ()
4232   (unless (boundp 'gnus-article-commands-menu)
4233     (gnus-summary-make-menu-bar))
4234   (gnus-turn-off-edit-menu 'article)
4235   (unless (boundp 'gnus-article-article-menu)
4236     (easy-menu-define
4237      gnus-article-article-menu gnus-article-mode-map ""
4238      '("Article"
4239        ["Scroll forwards" gnus-article-goto-next-page t]
4240        ["Scroll backwards" gnus-article-goto-prev-page t]
4241        ["Show summary" gnus-article-show-summary t]
4242        ["Fetch Message-ID at point" gnus-article-refer-article t]
4243        ["Mail to address at point" gnus-article-mail t]
4244        ["Send a bug report" gnus-bug t]))
4245
4246     (easy-menu-define
4247      gnus-article-treatment-menu gnus-article-mode-map ""
4248      ;; Fixme: this should use :active (and maybe :visible).
4249      '("Treatment"
4250        ["Hide headers" gnus-article-hide-headers t]
4251        ["Hide signature" gnus-article-hide-signature t]
4252        ["Hide citation" gnus-article-hide-citation t]
4253        ["Treat overstrike" gnus-article-treat-overstrike t]
4254        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
4255        ["Remove carriage return" gnus-article-remove-cr t]
4256        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
4257        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
4258        ["Remove base64" gnus-article-de-base64-unreadable t]
4259        ["Treat html" gnus-article-wash-html t]
4260        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
4261        ["Decode HZ" gnus-article-decode-HZ t]))
4262
4263     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
4264
4265     ;; Note "Post" menu is defined in gnus-sum.el for consistency
4266
4267     (gnus-run-hooks 'gnus-article-menu-hook)))
4268
4269 (defun gnus-article-mode ()
4270   "Major mode for displaying an article.
4271
4272 All normal editing commands are switched off.
4273
4274 The following commands are available in addition to all summary mode
4275 commands:
4276 \\<gnus-article-mode-map>
4277 \\[gnus-article-next-page]\t Scroll the article one page forwards
4278 \\[gnus-article-prev-page]\t Scroll the article one page backwards
4279 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
4280 \\[gnus-article-show-summary]\t Display the summary buffer
4281 \\[gnus-article-mail]\t Send a reply to the address near point
4282 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
4283 \\[gnus-info-find-node]\t Go to the Gnus info node"
4284   (interactive)
4285   (kill-all-local-variables)
4286   (gnus-simplify-mode-line)
4287   (setq mode-name "Article")
4288   (setq major-mode 'gnus-article-mode)
4289   (make-local-variable 'minor-mode-alist)
4290   (use-local-map gnus-article-mode-map)
4291   (when (gnus-visual-p 'article-menu 'menu)
4292     (gnus-article-make-menu-bar)
4293     (when gnus-summary-tool-bar-map
4294       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
4295   (gnus-update-format-specifications nil 'article-mode)
4296   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
4297   (set (make-local-variable 'gnus-page-broken) nil)
4298   (make-local-variable 'gnus-button-marker-list)
4299   (make-local-variable 'gnus-article-current-summary)
4300   (make-local-variable 'gnus-article-mime-handles)
4301   (make-local-variable 'gnus-article-decoded-p)
4302   (make-local-variable 'gnus-article-mime-handle-alist)
4303   (make-local-variable 'gnus-article-wash-types)
4304   (make-local-variable 'gnus-article-image-alist)
4305   (make-local-variable 'gnus-article-charset)
4306   (make-local-variable 'gnus-article-ignored-charsets)
4307   ;; Prevent Emacs 22 from displaying non-break space with `nobreak-space'
4308   ;; face.
4309   (set (make-local-variable 'nobreak-char-display) nil)
4310   (setq cursor-in-non-selected-windows nil)
4311   (setq truncate-lines gnus-article-truncate-lines)
4312   (gnus-set-default-directory)
4313   (buffer-disable-undo)
4314   (setq buffer-read-only t
4315         show-trailing-whitespace nil)
4316   (set-syntax-table gnus-article-mode-syntax-table)
4317   (mm-enable-multibyte)
4318   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4319
4320 (defvar gnus-button-marker-list nil
4321   "Regexp matching any of the regexps from `gnus-button-alist'.
4322 Internal variable.")
4323
4324 (defun gnus-article-setup-buffer ()
4325   "Initialize the article buffer."
4326   (let* ((name (if gnus-single-article-buffer "*Article*"
4327                  (concat "*Article " gnus-newsgroup-name "*")))
4328          (original
4329           (progn (string-match "\\*Article" name)
4330                  (concat " *Original Article"
4331                          (substring name (match-end 0))))))
4332     (setq gnus-article-buffer name)
4333     (setq gnus-original-article-buffer original)
4334     (setq gnus-article-mime-handle-alist nil)
4335     (with-current-buffer gnus-summary-buffer
4336       ;; This might be a variable local to the summary buffer.
4337       (unless gnus-single-article-buffer
4338         (setq gnus-article-buffer name)
4339         (setq gnus-original-article-buffer original)
4340         (gnus-set-global-variables)))
4341     (gnus-article-setup-highlight-words)
4342     ;; Init original article buffer.
4343     (with-current-buffer (gnus-get-buffer-create gnus-original-article-buffer)
4344       (mm-enable-multibyte)
4345       (setq major-mode 'gnus-original-article-mode)
4346       (make-local-variable 'gnus-original-article))
4347     (if (and (get-buffer name)
4348              (with-current-buffer name
4349                (if gnus-article-edit-mode
4350                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4351                        (progn
4352                          (set-buffer-modified-p nil)
4353                          (gnus-kill-buffer name)
4354                          (message "")
4355                          nil)
4356                      (error "Action aborted"))
4357                  t)))
4358         (with-current-buffer name
4359           (set (make-local-variable 'gnus-article-edit-mode) nil)
4360           (when gnus-article-mime-handles
4361             (mm-destroy-parts gnus-article-mime-handles)
4362             (setq gnus-article-mime-handles nil))
4363           ;; Set it to nil in article-buffer!
4364           (setq gnus-article-mime-handle-alist nil)
4365           (buffer-disable-undo)
4366           (setq buffer-read-only t)
4367           ;; This list just keeps growing if we don't reset it.
4368           (setq gnus-button-marker-list nil)
4369           (unless (eq major-mode 'gnus-article-mode)
4370             (gnus-article-mode))
4371           (current-buffer))
4372       (with-current-buffer (gnus-get-buffer-create name)
4373         (gnus-article-mode)
4374         (make-local-variable 'gnus-summary-buffer)
4375         (setq gnus-summary-buffer
4376               (gnus-summary-buffer-name gnus-newsgroup-name))
4377         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4378         (current-buffer)))))
4379
4380 ;; Set article window start at LINE, where LINE is the number of lines
4381 ;; from the head of the article.
4382 (defun gnus-article-set-window-start (&optional line)
4383   (let ((article-window (gnus-get-buffer-window gnus-article-buffer t)))
4384     (when article-window
4385       (set-window-start
4386        article-window
4387        (with-current-buffer gnus-article-buffer
4388          (goto-char (point-min))
4389          (if (not line)
4390              (point-min)
4391            (gnus-message 6 "Moved to bookmark")
4392            (search-forward "\n\n" nil t)
4393            (forward-line line)
4394            (point)))))))
4395
4396 (defun gnus-article-prepare (article &optional all-headers header)
4397   "Prepare ARTICLE in article mode buffer.
4398 ARTICLE should either be an article number or a Message-ID.
4399 If ARTICLE is an id, HEADER should be the article headers.
4400 If ALL-HEADERS is non-nil, no headers are hidden."
4401   (save-excursion
4402     ;; Make sure we start in a summary buffer.
4403     (unless (eq major-mode 'gnus-summary-mode)
4404       (set-buffer gnus-summary-buffer))
4405     (setq gnus-summary-buffer (current-buffer))
4406     (let* ((gnus-article (if header (mail-header-number header) article))
4407            (summary-buffer (current-buffer))
4408            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4409            (group gnus-newsgroup-name)
4410            result)
4411       (save-excursion
4412         (gnus-article-setup-buffer)
4413         (set-buffer gnus-article-buffer)
4414         ;; Deactivate active regions.
4415         (when (and (boundp 'transient-mark-mode)
4416                    transient-mark-mode)
4417           (setq mark-active nil))
4418         (if (not (setq result (let ((inhibit-read-only t))
4419                                 (gnus-request-article-this-buffer
4420                                  article group))))
4421             ;; There is no such article.
4422             (save-excursion
4423               (when (and (numberp article)
4424                          (not (memq article gnus-newsgroup-sparse)))
4425                 (setq gnus-article-current
4426                       (cons gnus-newsgroup-name article))
4427                 (set-buffer gnus-summary-buffer)
4428                 (setq gnus-current-article article)
4429                 (if (and (memq article gnus-newsgroup-undownloaded)
4430                          (not (gnus-online (gnus-find-method-for-group
4431                                             gnus-newsgroup-name))))
4432                     (progn
4433                       (gnus-summary-set-agent-mark article)
4434                       (message "Message marked for downloading"))
4435                   (gnus-summary-mark-article article gnus-canceled-mark)
4436                   (unless (memq article gnus-newsgroup-sparse)
4437                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4438           (if (or (eq result 'pseudo)
4439                   (eq result 'nneething))
4440               (progn
4441                 (with-current-buffer summary-buffer
4442                   (push article gnus-newsgroup-history)
4443                   (setq gnus-last-article gnus-current-article
4444                         gnus-current-article 0
4445                         gnus-current-headers nil
4446                         gnus-article-current nil)
4447                   (if (eq result 'nneething)
4448                       (gnus-configure-windows 'summary)
4449                     (gnus-configure-windows 'article))
4450                   (gnus-set-global-variables))
4451                 (let ((gnus-article-mime-handle-alist-1
4452                        gnus-article-mime-handle-alist))
4453                   (gnus-set-mode-line 'article)))
4454             ;; The result from the `request' was an actual article -
4455             ;; or at least some text that is now displayed in the
4456             ;; article buffer.
4457             (when (and (numberp article)
4458                        (not (eq article gnus-current-article)))
4459               ;; Seems like a new article has been selected.
4460               ;; `gnus-current-article' must be an article number.
4461               (with-current-buffer summary-buffer
4462                 (push article gnus-newsgroup-history)
4463                 (setq gnus-last-article gnus-current-article
4464                       gnus-current-article article
4465                       gnus-current-headers
4466                       (gnus-summary-article-header gnus-current-article)
4467                       gnus-article-current
4468                       (cons gnus-newsgroup-name gnus-current-article))
4469                 (unless (vectorp gnus-current-headers)
4470                   (setq gnus-current-headers nil))
4471                 (gnus-summary-goto-subject gnus-current-article)
4472                 (when (gnus-summary-show-thread)
4473                   ;; If the summary buffer really was folded, the
4474                   ;; previous goto may not actually have gone to
4475                   ;; the right article, but the thread root instead.
4476                   ;; So we go again.
4477                   (gnus-summary-goto-subject gnus-current-article))
4478                 (gnus-run-hooks 'gnus-mark-article-hook)
4479                 (gnus-set-mode-line 'summary)
4480                 (when (gnus-visual-p 'article-highlight 'highlight)
4481                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4482                 ;; Set the global newsgroup variables here.
4483                 (gnus-set-global-variables)
4484                 (setq gnus-have-all-headers
4485                       (or all-headers gnus-show-all-headers))))
4486             (save-excursion
4487               (gnus-configure-windows 'article))
4488             (when (or (numberp article)
4489                       (stringp article))
4490               (gnus-article-prepare-display)
4491               ;; Do page break.
4492               (goto-char (point-min))
4493               (when gnus-break-pages
4494                 (gnus-narrow-to-page)))
4495             (let ((gnus-article-mime-handle-alist-1
4496                    gnus-article-mime-handle-alist))
4497               (gnus-set-mode-line 'article))
4498             (article-goto-body)
4499             (unless (bobp)
4500               (forward-line -1))
4501             (set-window-point (get-buffer-window (current-buffer)) (point))
4502             (gnus-configure-windows 'article)
4503             t))))))
4504
4505 ;;;###autoload
4506 (defun gnus-article-prepare-display ()
4507   "Make the current buffer look like a nice article."
4508   ;; Hooks for getting information from the article.
4509   ;; This hook must be called before being narrowed.
4510   (let ((gnus-article-buffer (current-buffer))
4511         buffer-read-only
4512         (inhibit-read-only t))
4513     (unless (eq major-mode 'gnus-article-mode)
4514       (gnus-article-mode))
4515     (setq buffer-read-only nil
4516           gnus-article-wash-types nil
4517           gnus-article-image-alist nil)
4518     (gnus-run-hooks 'gnus-tmp-internal-hook)
4519     (when gnus-display-mime-function
4520       (funcall gnus-display-mime-function))
4521     (gnus-run-hooks 'gnus-article-prepare-hook)))
4522
4523 ;;;
4524 ;;; Gnus Sticky Article Mode
4525 ;;;
4526
4527 (define-derived-mode gnus-sticky-article-mode gnus-article-mode "StickyArticle"
4528   "Mode for sticky articles."
4529   ;; Release bindings that won't work.
4530   (substitute-key-definition 'gnus-article-read-summary-keys 'undefined
4531                              gnus-sticky-article-mode-map)
4532   (substitute-key-definition 'gnus-article-refer-article 'undefined
4533                              gnus-sticky-article-mode-map)
4534   (dolist (k '("e" "h" "s" "F" "R"))
4535     (define-key gnus-sticky-article-mode-map k nil))
4536   (define-key gnus-sticky-article-mode-map "k" 'gnus-kill-sticky-article-buffer)
4537   (define-key gnus-sticky-article-mode-map "q" 'bury-buffer)
4538   (define-key gnus-sticky-article-mode-map "\C-hc" 'describe-key-briefly)
4539   (define-key gnus-sticky-article-mode-map "\C-hk" 'describe-key))
4540
4541 (defun gnus-sticky-article (arg)
4542   "Make the current article sticky.
4543 If a prefix ARG is given, ask for a name for this sticky article buffer."
4544   (interactive "P")
4545   (gnus-summary-show-thread)
4546   (gnus-summary-select-article nil nil 'pseudo)
4547   (let (new-art-buf-name)
4548     (gnus-eval-in-buffer-window gnus-article-buffer
4549       (setq new-art-buf-name
4550             (concat
4551              "*Sticky Article: "
4552              (if arg
4553                  (read-from-minibuffer "Sticky article buffer name: ")
4554                (gnus-with-article-headers
4555                  (gnus-article-goto-header "subject")
4556                  (setq new-art-buf-name
4557                        (buffer-substring-no-properties
4558                         (line-beginning-position) (line-end-position)))
4559                  (goto-char (point-min))
4560                  (gnus-article-goto-header "from")
4561                  (setq new-art-buf-name
4562                        (concat
4563                         new-art-buf-name ", "
4564                         (buffer-substring-no-properties
4565                          (line-beginning-position) (line-end-position))))
4566                  (goto-char (point-min))
4567                  (gnus-article-goto-header "date")
4568                  (setq new-art-buf-name
4569                        (concat
4570                         new-art-buf-name ", "
4571                         (buffer-substring-no-properties
4572                          (line-beginning-position) (line-end-position))))))
4573              "*"))
4574       (if (and (gnus-buffer-live-p new-art-buf-name)
4575                (with-current-buffer new-art-buf-name
4576                  (eq major-mode 'gnus-sticky-article-mode)))
4577           (switch-to-buffer new-art-buf-name)
4578         (setq new-art-buf-name (rename-buffer new-art-buf-name t)))
4579       (gnus-sticky-article-mode))
4580     (setq gnus-article-buffer new-art-buf-name))
4581   (gnus-summary-recenter)
4582   (gnus-summary-position-point))
4583
4584 (defun gnus-kill-sticky-article-buffer (&optional buffer)
4585   "Kill the given sticky article BUFFER.
4586 If none is given, assume the current buffer and kill it if it has
4587 `gnus-sticky-article-mode'."
4588   (interactive)
4589   (unless buffer
4590     (setq buffer (current-buffer)))
4591   (with-current-buffer buffer
4592     (when (eq major-mode 'gnus-sticky-article-mode)
4593       (gnus-kill-buffer buffer))))
4594
4595 (defun gnus-kill-sticky-article-buffers (arg)
4596   "Kill all sticky article buffers.
4597 If a prefix ARG is given, ask for confirmation."
4598   (interactive "P")
4599   (dolist (buf (gnus-buffers))
4600     (with-current-buffer buf
4601       (when (eq major-mode 'gnus-sticky-article-mode)
4602         (if (not arg)
4603             (gnus-kill-buffer buf)
4604           (when (yes-or-no-p (concat "Kill buffer " (buffer-name buf) "? "))
4605             (gnus-kill-buffer buf)))))))
4606
4607 ;;;
4608 ;;; Gnus MIME viewing functions
4609 ;;;
4610
4611 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4612   "Format of the MIME buttons.
4613
4614 Valid specifiers include:
4615 %t  The MIME type
4616 %T  MIME type, along with additional info
4617 %n  The `name' parameter
4618 %d  The description, if any
4619 %l  The length of the encoded part
4620 %p  The part identifier number
4621 %e  Dots if the part isn't displayed
4622
4623 General format specifiers can also be used.  See Info node
4624 `(gnus)Formatting Variables'.")
4625
4626 (defvar gnus-mime-button-line-format-alist
4627   '((?t gnus-tmp-type ?s)
4628     (?T gnus-tmp-type-long ?s)
4629     (?n gnus-tmp-name ?s)
4630     (?d gnus-tmp-description ?s)
4631     (?p gnus-tmp-id ?s)
4632     (?l gnus-tmp-length ?d)
4633     (?e gnus-tmp-dots ?s)))
4634
4635 (defvar gnus-mime-button-commands
4636   '((gnus-article-press-button "\r" "Toggle Display")
4637     (gnus-mime-view-part "v" "View Interactively...")
4638     (gnus-mime-view-part-as-type "t" "View As Type...")
4639     (gnus-mime-view-part-as-charset "C" "View As charset...")
4640     (gnus-mime-save-part "o" "Save...")
4641     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4642     (gnus-mime-replace-part "r" "Replace part")
4643     (gnus-mime-delete-part "d" "Delete part")
4644     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4645     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4646     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4647     (gnus-mime-view-part-externally "e" "View Externally")
4648     (gnus-mime-print-part "p" "Print")
4649     (gnus-mime-pipe-part "|" "Pipe To Command...")
4650     (gnus-mime-action-on-part "." "Take action on the part...")))
4651
4652 (defun gnus-article-mime-part-status ()
4653   (if gnus-article-mime-handle-alist-1
4654       (if (eq 1 (length gnus-article-mime-handle-alist-1))
4655           " (1 part)"
4656         (format " (%d parts)" (length gnus-article-mime-handle-alist-1)))
4657     ""))
4658
4659 (defvar gnus-mime-button-map
4660   (let ((map (make-sparse-keymap)))
4661     (define-key map gnus-mouse-2 'gnus-article-push-button)
4662     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4663     (dolist (c gnus-mime-button-commands)
4664       (define-key map (cadr c) (car c)))
4665     map))
4666
4667 (easy-menu-define
4668   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4669   `("MIME Part"
4670     ,@(mapcar (lambda (c)
4671                 (vector (caddr c) (car c) :active t))
4672               gnus-mime-button-commands)))
4673
4674 (defun gnus-mime-button-menu (event prefix)
4675  "Construct a context-sensitive menu of MIME commands."
4676  (interactive "e\nP")
4677  (save-window-excursion
4678    (let ((pos (event-start event)))
4679      (select-window (posn-window pos))
4680      (goto-char (posn-point pos))
4681      (gnus-article-check-buffer)
4682      (popup-menu gnus-mime-button-menu nil prefix))))
4683
4684 (defun gnus-mime-view-all-parts (&optional handles)
4685   "View all the MIME parts."
4686   (interactive)
4687   (with-current-buffer gnus-article-buffer
4688     (let ((handles (or handles gnus-article-mime-handles))
4689           (mail-parse-charset gnus-newsgroup-charset)
4690           (mail-parse-ignored-charsets
4691            (with-current-buffer gnus-summary-buffer
4692              gnus-newsgroup-ignored-charsets)))
4693       (when handles
4694         (mm-remove-parts handles)
4695         (goto-char (point-min))
4696         (or (search-forward "\n\n") (goto-char (point-max)))
4697         (let ((inhibit-read-only t))
4698           (delete-region (point) (point-max))
4699           (mm-display-parts handles))))))
4700
4701 (defun gnus-article-jump-to-part (n)
4702   "Jump to MIME part N."
4703   (interactive "P")
4704   (pop-to-buffer gnus-article-buffer)
4705   ;; FIXME: why is it necessary?
4706   (sit-for 0)
4707   (let ((parts (length gnus-article-mime-handle-alist)))
4708     (or n (setq n
4709                 (string-to-number
4710                  (read-string ;; Emacs 21 doesn't have `read-number'.
4711                   (format "Jump to part (2..%s): " parts)))))
4712     (unless (and (integerp n) (<= n parts) (>= n 1))
4713       (setq n
4714             (progn
4715               (gnus-message 7 "Invalid part `%s', using %s instead."
4716                             n parts)
4717               parts)))
4718     (gnus-message 9 "Jumping to part %s." n)
4719     (cond ((>= gnus-auto-select-part 1)
4720            (while (and (<= n parts)
4721                        (not (gnus-article-goto-part n)))
4722              (setq n (1+ n))))
4723           ((< gnus-auto-select-part 0)
4724            (while (and (>= n 1)
4725                        (not (gnus-article-goto-part n)))
4726              (setq n (1- n))))
4727           (t
4728            (gnus-article-goto-part n)))))
4729
4730 (eval-when-compile
4731   (defsubst gnus-article-edit-part (handles &optional current-id)
4732     "Edit an article in order to delete a mime part.
4733 This function is exclusively used by `gnus-mime-save-part-and-strip'
4734 and `gnus-mime-delete-part', and not provided at run-time normally."
4735     (gnus-article-edit-article
4736      `(lambda ()
4737         (buffer-disable-undo)
4738         (erase-buffer)
4739         (let ((mail-parse-charset (or gnus-article-charset
4740                                       ',gnus-newsgroup-charset))
4741               (mail-parse-ignored-charsets
4742                (or gnus-article-ignored-charsets
4743                    ',gnus-newsgroup-ignored-charsets))
4744               (mbl mml-buffer-list))
4745           (setq mml-buffer-list nil)
4746           (insert-buffer-substring gnus-original-article-buffer)
4747           (mime-to-mml ',handles)
4748           (setq gnus-article-mime-handles nil)
4749           (let ((mbl1 mml-buffer-list))
4750             (setq mml-buffer-list mbl)
4751             (set (make-local-variable 'mml-buffer-list) mbl1))
4752           (gnus-make-local-hook 'kill-buffer-hook)
4753           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4754      `(lambda (no-highlight)
4755         (let ((mail-parse-charset (or gnus-article-charset
4756                                       ',gnus-newsgroup-charset))
4757               (message-options message-options)
4758               (message-options-set-recipient)
4759               (mail-parse-ignored-charsets
4760                (or gnus-article-ignored-charsets
4761                    ',gnus-newsgroup-ignored-charsets)))
4762           (mml-to-mime)
4763           (mml-destroy-buffers)
4764           (remove-hook 'kill-buffer-hook
4765                        'mml-destroy-buffers t)
4766           (kill-local-variable 'mml-buffer-list))
4767         (gnus-summary-edit-article-done
4768          ,(or (mail-header-references gnus-current-headers) "")
4769          ,(gnus-group-read-only-p)
4770          ,gnus-summary-buffer no-highlight))
4771      t)
4772     (gnus-article-edit-done)
4773     (gnus-summary-expand-window)
4774     (gnus-summary-show-article)
4775     (when (and current-id (integerp gnus-auto-select-part))
4776       (gnus-article-jump-to-part
4777        (if (text-property-any (point-min) (point-max)
4778                               'gnus-part (+ current-id gnus-auto-select-part))
4779            (+ current-id gnus-auto-select-part)
4780          (with-current-buffer gnus-article-buffer
4781            (length gnus-article-mime-handle-alist)))))))
4782
4783 (defun gnus-mime-replace-part (file)
4784   "Replace MIME part under point with an external body."
4785   ;; Useful if file has already been saved to disk
4786   (interactive
4787    (list
4788     (read-file-name "Replace MIME part with file: "
4789                     (or mm-default-directory default-directory)
4790                     nil nil)))
4791   (gnus-mime-save-part-and-strip file))
4792
4793 (defun gnus-mime-save-part-and-strip (&optional file)
4794   "Save the MIME part under point then replace it with an external body.
4795 If FILE is given, use it for the external part."
4796   (interactive)
4797   (gnus-article-check-buffer)
4798   (when (gnus-group-read-only-p)
4799     (error "The current group does not support deleting of parts"))
4800   (when (mm-complicated-handles gnus-article-mime-handles)
4801     (error "\
4802 The current article has a complicated MIME structure, giving up..."))
4803   (let* ((data (get-text-property (point) 'gnus-data))
4804          (id (get-text-property (point) 'gnus-part))
4805          param
4806          (handles gnus-article-mime-handles))
4807     (unless file
4808       (setq file
4809             (and data (mm-save-part data "Delete MIME part and save to: "))))
4810     (when file
4811       (with-current-buffer (mm-handle-buffer data)
4812         (erase-buffer)
4813         (insert "Content-Type: " (mm-handle-media-type data))
4814         (mml-insert-parameter-string (cdr (mm-handle-type data))
4815                                      '(charset))
4816         ;; Add a filename for the sake of saving the part again.
4817         (mml-insert-parameter
4818          (mail-header-encode-parameter "name" (file-name-nondirectory file)))
4819         (insert "\n")
4820         (insert "Content-ID: " (message-make-message-id) "\n")
4821         (insert "Content-Transfer-Encoding: binary\n")
4822         (insert "\n"))
4823       (setcdr data
4824               (cdr (mm-make-handle nil
4825                                    `("message/external-body"
4826                                      (access-type . "LOCAL-FILE")
4827                                      (name . ,file)))))
4828       ;; (set-buffer gnus-summary-buffer)
4829       (gnus-article-edit-part handles id))))
4830
4831 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
4832 ;; parts...>') but with stripping would be nice.
4833
4834 (defun gnus-mime-delete-part ()
4835   "Delete the MIME part under point.
4836 Replace it with some information about the removed part."
4837   (interactive)
4838   (gnus-article-check-buffer)
4839   (when (gnus-group-read-only-p)
4840     (error "The current group does not support deleting of parts"))
4841   (when (mm-complicated-handles gnus-article-mime-handles)
4842     (error "\
4843 The current article has a complicated MIME structure, giving up..."))
4844   (when (or gnus-expert-user
4845             (gnus-yes-or-no-p "\
4846 Deleting parts may malfunction or destroy the article; continue? "))
4847     (let* ((data (get-text-property (point) 'gnus-data))
4848            (id (get-text-property (point) 'gnus-part))
4849            (handles gnus-article-mime-handles)
4850            (none "(none)")
4851            (description
4852             (let ((desc (mm-handle-description data)))
4853               (when desc
4854                 (mail-decode-encoded-word-string desc))))
4855            (filename
4856             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
4857                 none))
4858            (type (mm-handle-media-type data)))
4859       (unless data
4860         (error "No MIME part under point"))
4861       (with-current-buffer (mm-handle-buffer data)
4862         (let ((bsize (format "%s" (buffer-size))))
4863           (erase-buffer)
4864           (insert
4865            (concat
4866             ",----\n"
4867             "| The following attachment has been deleted:\n"
4868             "|\n"
4869             "| Type:           " type "\n"
4870             "| Filename:       " filename "\n"
4871             "| Size (encoded): " bsize " Byte\n"
4872             (when description
4873               (concat    "| Description:    " description "\n"))
4874             "`----\n"))
4875           (setcdr data
4876                   (cdr (mm-make-handle
4877                         nil `("text/plain") nil nil
4878                         (list "attachment")
4879                         (format "Deleted attachment (%s bytes)" bsize))))))
4880       ;; (set-buffer gnus-summary-buffer)
4881       (gnus-article-edit-part handles id))))
4882
4883 (defun gnus-mime-save-part ()
4884   "Save the MIME part under point."
4885   (interactive)
4886   (gnus-article-check-buffer)
4887   (let ((data (get-text-property (point) 'gnus-data)))
4888     (when data
4889       (mm-save-part data))))
4890
4891 (defun gnus-mime-pipe-part ()
4892   "Pipe the MIME part under point to a process."
4893   (interactive)
4894   (gnus-article-check-buffer)
4895   (let ((data (get-text-property (point) 'gnus-data)))
4896     (when data
4897       (mm-pipe-part data))))
4898
4899 (defun gnus-mime-view-part ()
4900   "Interactively choose a viewing method for the MIME part under point."
4901   (interactive)
4902   (gnus-article-check-buffer)
4903   (let ((data (get-text-property (point) 'gnus-data)))
4904     (when data
4905       (setq gnus-article-mime-handles
4906             (mm-merge-handles
4907              gnus-article-mime-handles (setq data (copy-sequence data))))
4908       (mm-interactively-view-part data))))
4909
4910 (defun gnus-mime-view-part-as-type-internal ()
4911   (gnus-article-check-buffer)
4912   (let* ((handle (get-text-property (point) 'gnus-data))
4913          (name (or
4914                 ;; Content-Type: foo/bar; name=...
4915                 (mail-content-type-get (mm-handle-type handle) 'name)
4916                 ;; Content-Disposition: attachment; filename=...
4917                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
4918          (def-type (and name (mm-default-file-encoding name))))
4919     (or (and def-type (cons def-type 0))
4920         (and handle
4921              (equal (mm-handle-media-supertype handle) "text")
4922              '("text/plain" . 0))
4923         '("application/octet-stream" . 0))))
4924
4925 (defun gnus-mime-view-part-as-type (&optional mime-type pred)
4926   "Choose a MIME media type, and view the part as such.
4927 If non-nil, PRED is a predicate to use during completion to limit the
4928 available media-types."
4929   (interactive)
4930   (unless mime-type
4931     (setq mime-type
4932           (let ((default (gnus-mime-view-part-as-type-internal)))
4933             (completing-read
4934              (format "View as MIME type (default %s): "
4935                      (car default))
4936              (mapcar #'list (mailcap-mime-types))
4937              pred nil nil nil
4938              (car default)))))
4939   (gnus-article-check-buffer)
4940   (let ((handle (get-text-property (point) 'gnus-data)))
4941     (when handle
4942       (when (equal (mm-handle-media-type handle) "message/external-body")
4943         (unless (mm-handle-cache handle)
4944           (mm-extern-cache-contents handle))
4945         (setq handle (mm-handle-cache handle)))
4946       (setq handle
4947             (mm-make-handle (mm-handle-buffer handle)
4948                             (cons mime-type (cdr (mm-handle-type handle)))
4949                             (mm-handle-encoding handle)
4950                             (mm-handle-undisplayer handle)
4951                             (mm-handle-disposition handle)
4952                             (mm-handle-description handle)
4953                             nil
4954                             (mm-handle-id handle)))
4955       (setq gnus-article-mime-handles
4956             (mm-merge-handles gnus-article-mime-handles handle))
4957       (when (mm-handle-displayed-p handle)
4958         (mm-remove-part handle))
4959       (gnus-mm-display-part handle))))
4960
4961 (defun gnus-mime-copy-part (&optional handle arg)
4962   "Put the MIME part under point into a new buffer.
4963 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
4964 are decompressed."
4965   (interactive (list nil current-prefix-arg))
4966   (gnus-article-check-buffer)
4967   (unless handle
4968     (setq handle (get-text-property (point) 'gnus-data)))
4969   (when handle
4970     (let ((filename (or (mail-content-type-get (mm-handle-type handle)
4971                                                'name)
4972                         (mail-content-type-get (mm-handle-disposition handle)
4973                                                'filename)))
4974           contents dont-decode charset coding-system)
4975       (mm-with-unibyte-buffer
4976         (mm-insert-part handle)
4977         (setq contents (or (condition-case nil
4978                                (mm-decompress-buffer filename nil 'sig)
4979                              (error
4980                               (setq dont-decode t)
4981                               nil))
4982                            (buffer-string))))
4983       (setq filename (cond (filename (file-name-nondirectory filename))
4984                            (dont-decode "*raw data*")
4985                            (t "*decoded*")))
4986       (cond
4987        (dont-decode)
4988        ((not arg)
4989         (unless (setq charset (mail-content-type-get
4990                                (mm-handle-type handle) 'charset))
4991           (unless (setq coding-system (mm-with-unibyte-buffer
4992                                         (insert contents)
4993                                         (mm-find-buffer-file-coding-system)))
4994             (setq charset gnus-newsgroup-charset))))
4995        ((numberp arg)
4996         (setq charset (or (cdr (assq arg
4997                                      gnus-summary-show-article-charset-alist))
4998                           (mm-read-coding-system "Charset: ")))))
4999       (switch-to-buffer (generate-new-buffer filename))
5000       (if (or coding-system
5001               (and charset
5002                    (setq coding-system (mm-charset-to-coding-system charset))
5003                    (not (eq charset 'ascii))))
5004           (progn
5005             (mm-enable-multibyte)
5006             (insert (mm-decode-coding-string contents coding-system))
5007             (setq buffer-file-coding-system
5008                   (if (boundp 'last-coding-system-used)
5009                       (symbol-value 'last-coding-system-used)
5010                     coding-system)))
5011         (mm-disable-multibyte)
5012         (insert contents)
5013         (setq buffer-file-coding-system mm-binary-coding-system))
5014       ;; We do it this way to make `normal-mode' set the appropriate mode.
5015       (unwind-protect
5016           (progn
5017             (setq buffer-file-name (expand-file-name filename))
5018             (normal-mode))
5019         (setq buffer-file-name nil))
5020       (goto-char (point-min)))))
5021
5022 (defun gnus-mime-print-part (&optional handle filename)
5023   "Print the MIME part under point."
5024   (interactive (list nil (ps-print-preprint current-prefix-arg)))
5025   (gnus-article-check-buffer)
5026   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5027          (contents (and handle (mm-get-part handle)))
5028          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
5029          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
5030     (when contents
5031         (if printer
5032             (unwind-protect
5033                 (progn
5034                   (mm-save-part-to-file handle file)
5035                   (call-process shell-file-name nil
5036                                 (generate-new-buffer " *mm*")
5037                                 nil
5038                                 shell-command-switch
5039                                 (mm-mailcap-command
5040                                  printer file (mm-handle-type handle))))
5041               (delete-file file))
5042           (with-temp-buffer
5043             (insert contents)
5044             (gnus-print-buffer))
5045           (ps-despool filename)))))
5046
5047 (defun gnus-mime-inline-part (&optional handle arg)
5048   "Insert the MIME part under point into the current buffer.
5049 Compressed files like .gz and .bz2 are decompressed."
5050   (interactive (list nil current-prefix-arg))
5051   (gnus-article-check-buffer)
5052   (unless handle
5053     (setq handle (get-text-property (point) 'gnus-data)))
5054   (when handle
5055     (let ((b (point))
5056           (inhibit-read-only t)
5057           contents charset coding-system)
5058       (if (and (not arg) (mm-handle-undisplayer handle))
5059           (mm-remove-part handle)
5060         (mm-with-unibyte-buffer
5061           (mm-insert-part handle)
5062           (setq contents
5063                 (or (mm-decompress-buffer
5064                      (or (mail-content-type-get (mm-handle-type handle)
5065                                                 'name)
5066                          (mail-content-type-get (mm-handle-disposition handle)
5067                                                 'filename))
5068                      nil t)
5069                     (buffer-string))))
5070         (cond
5071          ((not arg)
5072           (unless (setq charset (mail-content-type-get
5073                                  (mm-handle-type handle) 'charset))
5074             (unless (setq coding-system
5075                           (mm-with-unibyte-buffer
5076                             (insert contents)
5077                             (mm-find-buffer-file-coding-system)))
5078               (setq charset gnus-newsgroup-charset))))
5079          ((numberp arg)
5080           (if (mm-handle-undisplayer handle)
5081               (mm-remove-part handle))
5082           (setq charset
5083                 (or (cdr (assq arg
5084                                gnus-summary-show-article-charset-alist))
5085                     (mm-read-coding-system "Charset: "))))
5086          (t
5087           (if (mm-handle-undisplayer handle)
5088               (mm-remove-part handle))))
5089         (forward-line 2)
5090         (mm-insert-inline
5091          handle
5092          (if (or coding-system
5093                  (and charset
5094                       (setq coding-system
5095                             (mm-charset-to-coding-system charset))
5096                       (not (eq coding-system 'ascii))))
5097              (mm-decode-coding-string contents coding-system)
5098            (mm-string-to-multibyte contents)))
5099         (goto-char b)))))
5100
5101 (defun gnus-mime-strip-charset-parameters (handle)
5102   "Strip charset parameters from HANDLE."
5103   (if (stringp (car handle))
5104       (mapc #'gnus-mime-strip-charset-parameters (cdr handle))
5105     (let* ((type (mm-handle-type (if (equal (mm-handle-media-type handle)
5106                                             "message/external-body")
5107                                      (progn
5108                                        (unless (mm-handle-cache handle)
5109                                          (mm-extern-cache-contents handle))
5110                                        (mm-handle-cache handle))
5111                                    handle)))
5112            (charset (assq 'charset (cdr type))))
5113       (when charset
5114         (delq charset type)))))
5115
5116 (defun gnus-mime-view-part-as-charset (&optional handle arg)
5117   "Insert the MIME part under point into the current buffer using the
5118 specified charset."
5119   (interactive (list nil current-prefix-arg))
5120   (gnus-article-check-buffer)
5121   (let ((handle (or handle (get-text-property (point) 'gnus-data)))
5122         (fun (get-text-property (point) 'gnus-callback))
5123         (gnus-newsgroup-ignored-charsets 'gnus-all)
5124         gnus-newsgroup-charset form preferred parts)
5125     (when handle
5126       (when (prog1
5127                 (and fun
5128                      (setq gnus-newsgroup-charset
5129                            (or (cdr (assq
5130                                      arg
5131                                      gnus-summary-show-article-charset-alist))
5132                                (mm-read-coding-system "Charset: "))))
5133               (if (mm-handle-undisplayer handle)
5134                   (mm-remove-part handle)))
5135         (gnus-mime-strip-charset-parameters handle)
5136         (when (and (consp (setq form (cdr-safe fun)))
5137                    (setq form (ignore-errors
5138                                 (assq 'gnus-mime-display-alternative form)))
5139                    (setq preferred (caddr form))
5140                    (progn
5141                      (when (eq (car preferred) 'quote)
5142                        (setq preferred (cadr preferred)))
5143                      (not (equal preferred
5144                                  (get-text-property (point) 'gnus-data))))
5145                    (setq parts (get-text-property (point) 'gnus-part))
5146                    (setq parts (cdr (assq parts
5147                                           gnus-article-mime-handle-alist)))
5148                    (equal (mm-handle-media-type parts) "multipart/alternative")
5149                    (setq parts (reverse (cdr parts))))
5150           (setcar (cddr form)
5151                   (list 'quote (or (cadr (member preferred parts))
5152                                    (car parts)))))
5153         (funcall fun handle)))))
5154
5155 (defun gnus-mime-view-part-externally (&optional handle)
5156   "View the MIME part under point with an external viewer."
5157   (interactive)
5158   (gnus-article-check-buffer)
5159   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5160          (mm-user-display-methods nil)
5161          (mm-inlined-types nil)
5162          (mail-parse-charset gnus-newsgroup-charset)
5163          (mail-parse-ignored-charsets
5164           (with-current-buffer gnus-summary-buffer
5165             gnus-newsgroup-ignored-charsets))
5166          (type (mm-handle-media-type handle))
5167          (method (mailcap-mime-info type))
5168          (mm-enable-external t))
5169     (if (not (stringp method))
5170         (gnus-mime-view-part-as-type
5171          nil (lambda (types) (stringp (mailcap-mime-info (car types)))))
5172       (when handle
5173         (if (mm-handle-undisplayer handle)
5174             (mm-remove-part handle)
5175           (mm-display-part handle))))))
5176
5177 (defun gnus-mime-view-part-internally (&optional handle)
5178   "View the MIME part under point with an internal viewer.
5179 If no internal viewer is available, use an external viewer."
5180   (interactive)
5181   (gnus-article-check-buffer)
5182   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
5183          (mm-inlined-types '(".*"))
5184          (mm-inline-large-images t)
5185          (mail-parse-charset gnus-newsgroup-charset)
5186          (mail-parse-ignored-charsets
5187           (with-current-buffer gnus-summary-buffer
5188             gnus-newsgroup-ignored-charsets))
5189          (inhibit-read-only t))
5190     (if (not (mm-inlinable-p handle))
5191         (gnus-mime-view-part-as-type
5192          nil (lambda (types) (mm-inlinable-p handle (car types))))
5193       (when handle
5194         (if (mm-handle-undisplayer handle)
5195             (mm-remove-part handle)
5196           (mm-display-part handle))))))
5197
5198 (defun gnus-mime-action-on-part (&optional action)
5199   "Do something with the MIME attachment at \(point\)."
5200   (interactive
5201    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
5202   (gnus-article-check-buffer)
5203   (let ((action-pair (assoc action gnus-mime-action-alist)))
5204     (if action-pair
5205         (funcall (cdr action-pair)))))
5206
5207 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
5208   "Call FUNCTION on MIME part N.
5209 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
5210 If INTERACTIVE, call FUNCTION interactivly."
5211   (let (window frame)
5212     ;; Check whether the article is displayed.
5213     (unless (and (gnus-buffer-live-p gnus-article-buffer)
5214                  (setq window (get-buffer-window gnus-article-buffer t))
5215                  (frame-visible-p (setq frame (window-frame window))))
5216       (error "No article is displayed"))
5217     (with-current-buffer gnus-article-buffer
5218       ;; Check whether the article displays the right contents.
5219       (unless (with-current-buffer gnus-summary-buffer
5220                 (eq gnus-current-article (gnus-summary-article-number)))
5221         (error "You should select the right article first"))
5222       (if n
5223           (setq n (prefix-numeric-value n))
5224         (let ((pt (point)))
5225           (setq n (or (get-text-property pt 'gnus-part)
5226                       (and (not (bobp))
5227                            (get-text-property (1- pt) 'gnus-part))
5228                       (get-text-property (prog2
5229                                              (forward-line 1)
5230                                              (point)
5231                                            (goto-char pt))
5232                                          'gnus-part)
5233                       (get-text-property
5234                        (or (and (setq pt (previous-single-property-change
5235                                           pt 'gnus-part))
5236                                 (1- pt))
5237                            (next-single-property-change (point) 'gnus-part)
5238                            (point))
5239                        'gnus-part)
5240                       1))))
5241       ;; Check whether the specified part exists.
5242       (when (> n (length gnus-article-mime-handle-alist))
5243         (error "No such part")))
5244     (unless
5245         (progn
5246           ;; To select the window is needed so that the cursor
5247           ;; might be visible on the MIME button.
5248           (select-window (prog1
5249                              window
5250                            (setq window (selected-window))
5251                            ;; Article may be displayed in the other frame.
5252                            (gnus-select-frame-set-input-focus
5253                             (prog1
5254                                 frame
5255                               (setq frame (selected-frame))))))
5256           (when (gnus-article-goto-part n)
5257             ;; We point the cursor and the arrow at the MIME button
5258             ;; when the `function' prompt the user for something.
5259             (let ((cursor-in-non-selected-windows t)
5260                   (overlay-arrow-string "=>")
5261                   (overlay-arrow-position (point-marker)))
5262               (unwind-protect
5263                   (cond
5264                    ((and no-handle interactive)
5265                     (call-interactively function))
5266                    (no-handle
5267                     (funcall function))
5268                    (interactive
5269                     (call-interactively
5270                      function
5271                      (cdr (assq n gnus-article-mime-handle-alist))))
5272                    (t
5273                     (funcall function
5274                              (cdr (assq n gnus-article-mime-handle-alist)))))
5275                 (set-marker overlay-arrow-position nil)
5276                 (unless gnus-auto-select-part
5277                   (gnus-select-frame-set-input-focus frame)
5278                   (select-window window))))
5279             t))
5280       (if gnus-inhibit-mime-unbuttonizing
5281           ;; This is the default though the program shouldn't reach here.
5282           (error "No such part")
5283         ;; The part which doesn't have the MIME button is selected.
5284         ;; So, we display all the buttons and redo it.
5285         (let ((gnus-inhibit-mime-unbuttonizing t))
5286           (gnus-summary-show-article)
5287           (gnus-article-part-wrapper n function no-handle))))))
5288
5289 (defun gnus-article-pipe-part (n)
5290   "Pipe MIME part N, which is the numerical prefix."
5291   (interactive "P")
5292   (gnus-article-part-wrapper n 'mm-pipe-part))
5293
5294 (defun gnus-article-save-part (n)
5295   "Save MIME part N, which is the numerical prefix."
5296   (interactive "P")
5297   (gnus-article-part-wrapper n 'mm-save-part))
5298
5299 (defun gnus-article-interactively-view-part (n)
5300   "View MIME part N interactively, which is the numerical prefix."
5301   (interactive "P")
5302   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5303
5304 (defun gnus-article-copy-part (n)
5305   "Copy MIME part N, which is the numerical prefix."
5306   (interactive "P")
5307   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5308
5309 (defun gnus-article-view-part-as-charset (n)
5310   "View MIME part N using a specified charset.
5311 N is the numerical prefix."
5312   (interactive "P")
5313   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5314
5315 (defun gnus-article-view-part-externally (n)
5316   "View MIME part N externally, which is the numerical prefix."
5317   (interactive "P")
5318   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5319
5320 (defun gnus-article-inline-part (n)
5321   "Inline MIME part N, which is the numerical prefix."
5322   (interactive "P")
5323   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5324
5325 (defun gnus-article-save-part-and-strip (n)
5326   "Save MIME part N and replace it with an external body.
5327 N is the numerical prefix."
5328   (interactive "P")
5329   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5330
5331 (defun gnus-article-replace-part (n)
5332   "Replace MIME part N with an external body.
5333 N is the numerical prefix."
5334   (interactive "P")
5335   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5336
5337 (defun gnus-article-delete-part (n)
5338   "Delete MIME part N and add some information about the removed part.
5339 N is the numerical prefix."
5340   (interactive "P")
5341   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5342
5343 (defun gnus-article-view-part-as-type (n)
5344   "Choose a MIME media type, and view part N as such.
5345 N is the numerical prefix."
5346   (interactive "P")
5347   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-type t))
5348
5349 (defun gnus-article-mime-match-handle-first (condition)
5350   (if condition
5351       (let (n)
5352         (dolist (ihandle gnus-article-mime-handle-alist)
5353           (if (and (cond
5354                     ((functionp condition)
5355                      (funcall condition (cdr ihandle)))
5356                     ((eq condition 'undisplayed)
5357                      (not (or (mm-handle-undisplayer (cdr ihandle))
5358                               (equal (mm-handle-media-type (cdr ihandle))
5359                                      "multipart/alternative"))))
5360                     ((eq condition 'undisplayed-alternative)
5361                      (not (mm-handle-undisplayer (cdr ihandle))))
5362                     (t t))
5363                    (gnus-article-goto-part (car ihandle))
5364                    (or (not n) (< (car ihandle) n)))
5365               (setq n (car ihandle))))
5366         (or n 1))
5367     1))
5368
5369 (defun gnus-article-view-part (&optional n)
5370   "View MIME part N, which is the numerical prefix."
5371   (interactive "P")
5372   (with-current-buffer gnus-article-buffer
5373     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5374                              gnus-article-mime-match-handle-function)))
5375     (when (> n (length gnus-article-mime-handle-alist))
5376       (error "No such part"))
5377     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5378       (when (gnus-article-goto-part n)
5379         (if (equal (car handle) "multipart/alternative")
5380             (gnus-article-press-button)
5381           (when (eq (gnus-mm-display-part handle) 'internal)
5382             (gnus-set-window-start)))))))
5383
5384 (defsubst gnus-article-mime-total-parts ()
5385   (if (bufferp (car gnus-article-mime-handles))
5386       1 ;; single part
5387     (1- (length gnus-article-mime-handles))))
5388
5389 (defun gnus-mm-display-part (handle)
5390   "Display HANDLE and fix MIME button."
5391   (let ((id (get-text-property (point) 'gnus-part))
5392         (point (point))
5393         (inhibit-read-only t))
5394     (forward-line 1)
5395     (prog1
5396         (let ((window (selected-window))
5397               (mail-parse-charset gnus-newsgroup-charset)
5398               (mail-parse-ignored-charsets
5399                (if (gnus-buffer-live-p gnus-summary-buffer)
5400                    (with-current-buffer gnus-summary-buffer
5401                      gnus-newsgroup-ignored-charsets)
5402                  nil)))
5403           (save-excursion
5404             (unwind-protect
5405                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5406                       (beg (point)))
5407                   (when win
5408                     (select-window win))
5409                   (goto-char point)
5410                   (forward-line)
5411                   (if (mm-handle-displayed-p handle)
5412                       ;; This will remove the part.
5413                       (mm-display-part handle)
5414                     (save-restriction
5415                       (narrow-to-region (point)
5416                                         (if (eobp) (point) (1+ (point))))
5417                       (mm-display-part handle)
5418                       ;; We narrow to the part itself and
5419                       ;; then call the treatment functions.
5420                       (goto-char (point-min))
5421                       (forward-line 1)
5422                       (narrow-to-region (point) (point-max))
5423                       (gnus-treat-article
5424                        nil id
5425                        (gnus-article-mime-total-parts)
5426                        (mm-handle-media-type handle)))))
5427               (if (window-live-p window)
5428                   (select-window window)))))
5429       (goto-char point)
5430       (gnus-delete-line)
5431       (gnus-insert-mime-button
5432        handle id (list (mm-handle-displayed-p handle)))
5433       (goto-char point))))
5434
5435 (defun gnus-article-goto-part (n)
5436   "Go to MIME part N."
5437   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
5438
5439 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5440   (let ((gnus-tmp-name
5441          (or (mail-content-type-get (mm-handle-type handle) 'name)
5442              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5443              (mail-content-type-get (mm-handle-type handle) 'url)
5444              ""))
5445         (gnus-tmp-type (mm-handle-media-type handle))
5446         (gnus-tmp-description (or (mm-handle-description handle) ""))
5447         (gnus-tmp-dots
5448          (if (if displayed (car displayed)
5449                (mm-handle-displayed-p handle))
5450              "" "..."))
5451         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5452                            (buffer-size)))
5453         gnus-tmp-type-long b e)
5454     (when (string-match ".*/" gnus-tmp-name)
5455       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5456     (setq gnus-tmp-type-long (concat gnus-tmp-type
5457                                      (and (not (equal gnus-tmp-name ""))
5458                                           (concat "; " gnus-tmp-name))))
5459     (unless (equal gnus-tmp-description "")
5460       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5461     (unless (bolp)
5462       (insert "\n"))
5463     (setq b (point))
5464     (gnus-eval-format
5465      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5466      `(keymap ,gnus-mime-button-map
5467               gnus-callback gnus-mm-display-part
5468               gnus-part ,gnus-tmp-id
5469               article-type annotation
5470               gnus-data ,handle))
5471     (setq e (if (bolp)
5472                 ;; Exclude a newline.
5473                 (1- (point))
5474               (point)))
5475     (when gnus-article-button-face
5476       (gnus-overlay-put (gnus-make-overlay b e nil t)
5477                         'face gnus-article-button-face))
5478     (widget-convert-button
5479      'link b e
5480      :mime-handle handle
5481      :action 'gnus-widget-press-button
5482      :button-keymap gnus-mime-button-map
5483      :help-echo
5484      (lambda (widget/window &optional overlay pos)
5485        ;; Needed to properly clear the message due to a bug in
5486        ;; wid-edit (XEmacs only).
5487        (if (boundp 'help-echo-owns-message)
5488            (setq help-echo-owns-message t))
5489        (format
5490         "%S: %s the MIME part; %S: more options"
5491         (aref gnus-mouse-2 0)
5492         ;; XEmacs will get a single widget arg; Emacs 21 will get
5493         ;; window, overlay, position.
5494         (if (mm-handle-displayed-p
5495              (if overlay
5496                  (with-current-buffer (gnus-overlay-buffer overlay)
5497                    (widget-get (widget-at (gnus-overlay-start overlay))
5498                                :mime-handle))
5499                (widget-get widget/window :mime-handle)))
5500             "hide" "show")
5501         (aref gnus-down-mouse-3 0))))))
5502
5503 (defun gnus-widget-press-button (elems el)
5504   (goto-char (widget-get elems :from))
5505   (gnus-article-press-button))
5506
5507 (defvar gnus-displaying-mime nil)
5508
5509 (defun gnus-display-mime (&optional ihandles)
5510   "Display the MIME parts."
5511   (save-excursion
5512     (save-selected-window
5513       (let ((window (get-buffer-window gnus-article-buffer))
5514             (point (point)))
5515         (when window
5516           (select-window window)
5517           ;; We have to do this since selecting the window
5518           ;; may change the point.  So we set the window point.
5519           (set-window-point window point)))
5520       (let ((handles ihandles)
5521             (inhibit-read-only t)
5522             handle)
5523         (cond (handles)
5524               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5525                (when gnus-article-emulate-mime
5526                  (mm-uu-dissect-text-parts handles)))
5527               (gnus-article-emulate-mime
5528                (setq handles (mm-uu-dissect))))
5529         (when (and (not ihandles)
5530                    (not gnus-displaying-mime))
5531           ;; Top-level call; we clean up.
5532           (when gnus-article-mime-handles
5533             (mm-destroy-parts gnus-article-mime-handles)
5534             (setq gnus-article-mime-handle-alist nil));; A trick.
5535           (setq gnus-article-mime-handles handles)
5536           ;; We allow users to glean info from the handles.
5537           (when gnus-article-mime-part-function
5538             (gnus-mime-part-function handles)))
5539         (if (and handles
5540                  (or (not (stringp (car handles)))
5541                      (cdr handles)))
5542             (progn
5543               (when (and (not ihandles)
5544                          (not gnus-displaying-mime))
5545                 ;; Clean up for mime parts.
5546                 (article-goto-body)
5547                 (delete-region (point) (point-max)))
5548               (let ((gnus-displaying-mime t))
5549                 (gnus-mime-display-part handles)))
5550           (save-restriction
5551             (article-goto-body)
5552             (narrow-to-region (point) (point-max))
5553             (gnus-treat-article nil 1 1)
5554             (widen)))
5555         (unless ihandles
5556           ;; Highlight the headers.
5557           (save-excursion
5558             (save-restriction
5559               (article-goto-body)
5560               (narrow-to-region (point-min) (point))
5561               (gnus-article-save-original-date
5562                (gnus-treat-article 'head)))))))
5563     ;; Cope with broken MIME messages.
5564     (goto-char (point-max))
5565     (unless (bolp)
5566       (insert "\n"))))
5567
5568 (defcustom gnus-mime-display-multipart-as-mixed nil
5569   "Display \"multipart\" parts as  \"multipart/mixed\".
5570
5571 If t, it overrides nil values of
5572 `gnus-mime-display-multipart-alternative-as-mixed' and
5573 `gnus-mime-display-multipart-related-as-mixed'."
5574   :group 'gnus-article-mime
5575   :type 'boolean)
5576
5577 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5578   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5579   :version "22.1"
5580   :group 'gnus-article-mime
5581   :type 'boolean)
5582
5583 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5584   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5585
5586 If displaying \"text/html\" is discouraged \(see
5587 `mm-discouraged-alternatives'\) images or other material inside a
5588 \"multipart/related\" part might be overlooked when this variable is nil."
5589   :version "22.1"
5590   :group 'gnus-article-mime
5591   :type 'boolean)
5592
5593 (defun gnus-mime-display-part (handle)
5594   (cond
5595    ;; Maybe a broken MIME message.
5596    ((null handle))
5597    ;; Single part.
5598    ((not (stringp (car handle)))
5599     (gnus-mime-display-single handle))
5600    ;; User-defined multipart
5601    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5602     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5603              handle))
5604    ;; multipart/alternative
5605    ((and (equal (car handle) "multipart/alternative")
5606          (not (or gnus-mime-display-multipart-as-mixed
5607                   gnus-mime-display-multipart-alternative-as-mixed)))
5608     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5609       (push (cons id handle) gnus-article-mime-handle-alist)
5610       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5611    ;; multipart/related
5612    ((and (equal (car handle) "multipart/related")
5613          (not (or gnus-mime-display-multipart-as-mixed
5614                   gnus-mime-display-multipart-related-as-mixed)))
5615     ;;;!!!We should find the start part, but we just default
5616     ;;;!!!to the first part.
5617     ;;(gnus-mime-display-part (cadr handle))
5618     ;;;!!! Most multipart/related is an HTML message plus images.
5619     ;;;!!! Unfortunately we are unable to let W3 display those
5620     ;;;!!! included images, so we just display it as a mixed multipart.
5621     ;;(gnus-mime-display-mixed (cdr handle))
5622     ;;;!!! No, w3 can display everything just fine.
5623     (gnus-mime-display-part (cadr handle)))
5624    ((equal (car handle) "multipart/signed")
5625     (gnus-add-wash-type 'signed)
5626     (gnus-mime-display-security handle))
5627    ((equal (car handle) "multipart/encrypted")
5628     (gnus-add-wash-type 'encrypted)
5629     (gnus-mime-display-security handle))
5630    ;; Other multiparts are handled like multipart/mixed.
5631    (t
5632     (gnus-mime-display-mixed (cdr handle)))))
5633
5634 (defun gnus-mime-part-function (handles)
5635   (if (stringp (car handles))
5636       (mapcar 'gnus-mime-part-function (cdr handles))
5637     (funcall gnus-article-mime-part-function handles)))
5638
5639 (defun gnus-mime-display-mixed (handles)
5640   (mapcar 'gnus-mime-display-part handles))
5641
5642 (defun gnus-mime-display-single (handle)
5643   (let ((type (mm-handle-media-type handle))
5644         (ignored gnus-ignored-mime-types)
5645         (not-attachment t)
5646         (move nil)
5647         display text)
5648     (catch 'ignored
5649       (progn
5650         (while ignored
5651           (when (string-match (pop ignored) type)
5652             (throw 'ignored nil)))
5653         (if (and (setq not-attachment
5654                        (and (not (mm-inline-override-p handle))
5655                             (or (not (mm-handle-disposition handle))
5656                                 (equal (car (mm-handle-disposition handle))
5657                                        "inline")
5658                                 (mm-attachment-override-p handle))))
5659                  (mm-automatic-display-p handle)
5660                  (or (and
5661                       (mm-inlinable-p handle)
5662                       (mm-inlined-p handle))
5663                      (mm-automatic-external-display-p type)))
5664             (setq display t)
5665           (when (equal (mm-handle-media-supertype handle) "text")
5666             (setq text t)))
5667         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5668               beg)
5669           (push (cons id handle) gnus-article-mime-handle-alist)
5670           (when (and display
5671                      (equal (mm-handle-media-supertype handle) "message"))
5672             (insert-char
5673              ?\n
5674              (cond ((not (bolp)) 2)
5675                    ((or (bobp) (eq (char-before (1- (point))) ?\n)) 0)
5676                    (t 1))))
5677           (when (or (not display)
5678                     (not (gnus-unbuttonized-mime-type-p type)))
5679             (gnus-insert-mime-button
5680              handle id (list (or display (and not-attachment text))))
5681             (gnus-article-insert-newline)
5682             ;; Remember modify the number of forward lines.
5683             (setq move t))
5684           (setq beg (point))
5685           (cond
5686            (display
5687             (when move
5688               (forward-line -1)
5689               (setq beg (point)))
5690             (let ((mail-parse-charset gnus-newsgroup-charset)
5691                   (mail-parse-ignored-charsets
5692                    (save-excursion (condition-case ()
5693                                        (set-buffer gnus-summary-buffer)
5694                                      (error))
5695                                    gnus-newsgroup-ignored-charsets)))
5696               (mm-display-part handle t))
5697             (goto-char (point-max)))
5698            ((and text not-attachment)
5699             (when move
5700               (forward-line -1)
5701               (setq beg (point)))
5702             (gnus-article-insert-newline)
5703             (mm-insert-inline
5704              handle
5705              (let ((charset (or (mail-content-type-get (mm-handle-type handle)
5706                                                        'charset)
5707                                 (and (equal type "text/calendar") 'utf-8))))
5708                (cond ((not charset)
5709                       (mm-string-as-multibyte (mm-get-part handle)))
5710                      ((eq charset 'gnus-decoded)
5711                       (with-current-buffer (mm-handle-buffer handle)
5712                         (buffer-string)))
5713                      (t
5714                       (mm-decode-string (mm-get-part handle) charset)))))
5715             (goto-char (point-max))))
5716           ;; Do highlighting.
5717           (save-excursion
5718             (save-restriction
5719               (narrow-to-region beg (point))
5720               (if (eq handle gnus-article-mime-handles)
5721                   ;; The format=flowed case.
5722                   (gnus-treat-article nil 1 1 (mm-handle-media-type handle))
5723                 ;; Don't count signature parts that are never displayed.
5724                 ;; The part number should be re-calculated supposing this
5725                 ;; might be a message/rfc822 part.
5726                 (let (handles)
5727                   (dolist (part gnus-article-mime-handles)
5728                     (unless (or (stringp part)
5729                                 (equal (car (mm-handle-type part))
5730                                        "application/pgp-signature"))
5731                       (push part handles)))
5732                   (gnus-treat-article
5733                    nil (length (memq handle handles)) (length handles)
5734                    (mm-handle-media-type handle)))))))))))
5735
5736 (defun gnus-unbuttonized-mime-type-p (type)
5737   "Say whether TYPE is to be unbuttonized."
5738   (unless gnus-inhibit-mime-unbuttonizing
5739     (when (catch 'found
5740             (let ((types gnus-unbuttonized-mime-types))
5741               (while types
5742                 (when (string-match (pop types) type)
5743                   (throw 'found t)))))
5744       (not (catch 'found
5745              (let ((types gnus-buttonized-mime-types))
5746                (while types
5747                  (when (string-match (pop types) type)
5748                    (throw 'found t)))))))))
5749
5750 (defun gnus-article-insert-newline ()
5751   "Insert a newline, but mark it as undeletable."
5752   (gnus-put-text-property
5753    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5754
5755 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5756   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5757          (ihandles handles)
5758          (point (point))
5759          handle (inhibit-read-only t) from props begend not-pref)
5760     (save-window-excursion
5761       (save-restriction
5762         (when ibegend
5763           (narrow-to-region (car ibegend)
5764                             (or (cdr ibegend)
5765                                 (progn
5766                                   (goto-char (car ibegend))
5767                                   (forward-line 2)
5768                                   (point))))
5769           (delete-region (point-min) (point-max))
5770           (mm-remove-parts handles))
5771         (setq begend (list (point-marker)))
5772         ;; Do the toggle.
5773         (unless (setq not-pref (cadr (member preferred ihandles)))
5774           (setq not-pref (car ihandles)))
5775         (when (or ibegend
5776                   (not preferred)
5777                   (not (gnus-unbuttonized-mime-type-p
5778                         "multipart/alternative")))
5779           (gnus-add-text-properties
5780            (setq from (point))
5781            (progn
5782              (insert (format "%d.  " id))
5783              (point))
5784            `(gnus-callback
5785              (lambda (handles)
5786                (unless ,(not ibegend)
5787                  (setq gnus-article-mime-handle-alist
5788                        ',gnus-article-mime-handle-alist))
5789                (gnus-mime-display-alternative
5790                 ',ihandles ',not-pref ',begend ,id))
5791              keymap ,gnus-mime-button-map
5792              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5793              face ,gnus-article-button-face
5794              gnus-part ,id
5795              article-type multipart))
5796           (widget-convert-button 'link from (point)
5797                                  :action 'gnus-widget-press-button
5798                                  :button-keymap gnus-widget-button-keymap)
5799           ;; Do the handles
5800           (while (setq handle (pop handles))
5801             (gnus-add-text-properties
5802              (setq from (point))
5803              (progn
5804                (insert (format "(%c) %-18s"
5805                                (if (equal handle preferred) ?* ? )
5806                                (mm-handle-media-type handle)))
5807                (point))
5808              `(gnus-callback
5809                (lambda (handles)
5810                  (unless ,(not ibegend)
5811                    (setq gnus-article-mime-handle-alist
5812                          ',gnus-article-mime-handle-alist))
5813                  (gnus-mime-display-alternative
5814                   ',ihandles ',handle ',begend ,id))
5815                keymap ,gnus-mime-button-map
5816                ,gnus-mouse-face-prop ,gnus-article-mouse-face
5817                face ,gnus-article-button-face
5818                gnus-part ,id
5819                gnus-data ,handle))
5820             (widget-convert-button 'link from (point)
5821                                    :action 'gnus-widget-press-button
5822                                    :button-keymap gnus-widget-button-keymap)
5823             (insert "  "))
5824           (insert "\n\n"))
5825         (when preferred
5826           (if (stringp (car preferred))
5827               (gnus-display-mime preferred)
5828             (let ((mail-parse-charset gnus-newsgroup-charset)
5829                   (mail-parse-ignored-charsets
5830                    (with-current-buffer gnus-summary-buffer
5831                      gnus-newsgroup-ignored-charsets)))
5832               (mm-display-part preferred)
5833               ;; Do highlighting.
5834               (save-excursion
5835                 (save-restriction
5836                   (narrow-to-region (car begend) (point-max))
5837                   (gnus-treat-article
5838                    nil (length gnus-article-mime-handle-alist)
5839                    (gnus-article-mime-total-parts)
5840                    (mm-handle-media-type handle))))))
5841           (goto-char (point-max))
5842           (setcdr begend (point-marker)))))
5843     (when ibegend
5844       (goto-char point))))
5845
5846 (defconst gnus-article-wash-status-strings
5847   (let ((alist '((cite "c" "Possible hidden citation text"
5848                        " " "All citation text visible")
5849                  (headers "h" "Hidden headers"
5850                           " " "All headers visible.")
5851                  (pgp "p" "Encrypted or signed message status hidden"
5852                       " " "No hidden encryption nor digital signature status")
5853                  (signature "s" "Signature has been hidden"
5854                             " " "Signature is visible")
5855                  (overstrike "o" "Overstrike (^H) characters applied"
5856                              " " "No overstrike characters applied")
5857                  (emphasis "e" "/*_Emphasis_*/ characters applied"
5858                            " " "No /*_emphasis_*/ characters applied")))
5859         result)
5860     (dolist (entry alist result)
5861       (let ((key (nth 0 entry))
5862             (on (copy-sequence (nth 1 entry)))
5863             (on-help (nth 2 entry))
5864             (off (copy-sequence (nth 3 entry)))
5865             (off-help (nth 4 entry)))
5866         (put-text-property 0 1 'help-echo on-help on)
5867         (put-text-property 0 1 'help-echo off-help off)
5868         (push (list key on off) result))))
5869   "Alist of strings describing wash status in the mode line.
5870 Each entry has the form (KEY ON OF), where the KEY is a symbol
5871 representing the particular washing function, ON is the string to use
5872 in the article mode line when the washing function is active, and OFF
5873 is the string to use when it is inactive.")
5874
5875 (defun gnus-article-wash-status-entry (key value)
5876   (let ((entry (assoc key gnus-article-wash-status-strings)))
5877     (if value (nth 1 entry) (nth 2 entry))))
5878
5879 (defun gnus-article-wash-status ()
5880   "Return a string which display status of article washing."
5881   (with-current-buffer gnus-article-buffer
5882     (let ((cite (memq 'cite gnus-article-wash-types))
5883           (headers (memq 'headers gnus-article-wash-types))
5884           (boring (memq 'boring-headers gnus-article-wash-types))
5885           (pgp (memq 'pgp gnus-article-wash-types))
5886           (pem (memq 'pem gnus-article-wash-types))
5887           (signed (memq 'signed gnus-article-wash-types))
5888           (encrypted (memq 'encrypted gnus-article-wash-types))
5889           (signature (memq 'signature gnus-article-wash-types))
5890           (overstrike (memq 'overstrike gnus-article-wash-types))
5891           (emphasis (memq 'emphasis gnus-article-wash-types)))
5892       (concat
5893        (gnus-article-wash-status-entry 'cite cite)
5894        (gnus-article-wash-status-entry 'headers (or headers boring))
5895        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
5896        (gnus-article-wash-status-entry 'signature signature)
5897        (gnus-article-wash-status-entry 'overstrike overstrike)
5898        (gnus-article-wash-status-entry 'emphasis emphasis)))))
5899
5900 (defun gnus-add-wash-type (type)
5901   "Add a washing of TYPE to the current status."
5902   (add-to-list 'gnus-article-wash-types type))
5903
5904 (defun gnus-delete-wash-type (type)
5905   "Add a washing of TYPE to the current status."
5906   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
5907
5908 (defun gnus-add-image (category image)
5909   "Add IMAGE of CATEGORY to the list of displayed images."
5910   (let ((entry (assq category gnus-article-image-alist)))
5911     (unless entry
5912       (setq entry (list category))
5913       (push entry gnus-article-image-alist))
5914     (nconc entry (list image))))
5915
5916 (defun gnus-delete-images (category)
5917   "Delete all images in CATEGORY."
5918   (let ((entry (assq category gnus-article-image-alist)))
5919     (dolist (image (cdr entry))
5920       (gnus-remove-image image category))
5921     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
5922     (gnus-delete-wash-type category)))
5923
5924 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
5925
5926 (defun gnus-article-maybe-hide-headers ()
5927   "Hide unwanted headers if `gnus-have-all-headers' is nil.
5928 Provided for backwards compatibility."
5929   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
5930                  (not (with-current-buffer gnus-summary-buffer
5931                         gnus-have-all-headers)))
5932              (not gnus-inhibit-hiding))
5933     (gnus-article-hide-headers)))
5934
5935 ;;; Article savers.
5936
5937 (defun gnus-output-to-file (file-name)
5938   "Append the current article to a file named FILE-NAME.
5939 If `gnus-article-save-coding-system' is non-nil, it is used to encode
5940 text and used as the value of the coding cookie which is added to the
5941 top of a file.  Otherwise, this function saves a raw article without
5942 the coding cookie."
5943   (let* ((artbuf (current-buffer))
5944          (file-name-coding-system nnmail-pathname-coding-system)
5945          (coding gnus-article-save-coding-system)
5946          (coding-system-for-read (if coding
5947                                      nil ;; Rely on the coding cookie.
5948                                    mm-text-coding-system))
5949          (coding-system-for-write (or coding
5950                                       mm-text-coding-system-for-write
5951                                       mm-text-coding-system))
5952          (exists (file-exists-p file-name)))
5953     (with-temp-buffer
5954       (when exists
5955         (insert-file-contents file-name)
5956         (goto-char (point-min))
5957         ;; Remove the existing coding cookie.
5958         (when (looking-at "X-Gnus-Coding-System: .+\n\n")
5959           (delete-region (match-beginning 0) (match-end 0))))
5960       (goto-char (point-max))
5961       (insert-buffer-substring artbuf)
5962       ;; Append newline at end of the buffer as separator, and then
5963       ;; save it to file.
5964       (goto-char (point-max))
5965       (insert "\n")
5966       (when coding
5967         ;; If the coding system is not suitable to encode the text,
5968         ;; ask a user for a proper one.
5969         (when (fboundp 'select-safe-coding-system)
5970           (setq coding (coding-system-base
5971                         (save-window-excursion
5972                           (select-safe-coding-system (point-min) (point-max)
5973                                                      coding))))
5974           (setq coding-system-for-write
5975                 (or (cdr (assq coding '((mule-utf-8 . utf-8))))
5976                     coding)))
5977         (goto-char (point-min))
5978         ;; Add the coding cookie.
5979         (insert (format "X-Gnus-Coding-System: -*- coding: %s; -*-\n\n"
5980                         coding-system-for-write)))
5981       (if exists
5982           (progn
5983             (write-region (point-min) (point-max) file-name nil 'no-message)
5984             (message "Appended to %s" file-name))
5985         (write-region (point-min) (point-max) file-name))))
5986   t)
5987
5988 (defun gnus-narrow-to-page (&optional arg)
5989   "Narrow the article buffer to a page.
5990 If given a numerical ARG, move forward ARG pages."
5991   (interactive "P")
5992   (setq arg (if arg (prefix-numeric-value arg) 0))
5993   (with-current-buffer gnus-article-buffer
5994     (widen)
5995     ;; Remove any old next/prev buttons.
5996     (when (gnus-visual-p 'page-marker)
5997       (let ((inhibit-read-only t))
5998         (gnus-remove-text-with-property 'gnus-prev)
5999         (gnus-remove-text-with-property 'gnus-next)))
6000     (let (st nd pt)
6001       (when (save-excursion
6002               (cond ((< arg 0)
6003                      (if (re-search-backward page-delimiter nil 'move (abs arg))
6004                          (prog1
6005                              (setq nd (match-beginning 0)
6006                                    pt nd)
6007                            (when (re-search-backward page-delimiter nil t)
6008                              (setq st (match-end 0))))
6009                        (when (re-search-forward page-delimiter nil t)
6010                          (setq nd (match-beginning 0)
6011                                pt (point-min)))))
6012                     ((> arg 0)
6013                      (if (re-search-forward page-delimiter nil 'move arg)
6014                          (prog1
6015                              (setq st (match-end 0)
6016                                    pt st)
6017                            (when (re-search-forward page-delimiter nil t)
6018                              (setq nd (match-beginning 0))))
6019                        (when (re-search-backward page-delimiter nil t)
6020                          (setq st (match-end 0)
6021                                pt (point-max)))))
6022                     (t
6023                      (when (re-search-backward page-delimiter nil t)
6024                        (goto-char (setq st (match-end 0))))
6025                      (when (re-search-forward page-delimiter nil t)
6026                        (setq nd (match-beginning 0)))
6027                      (or st nd))))
6028         (setq gnus-page-broken t)
6029         (when pt (goto-char pt))
6030         (narrow-to-region (or st (point-min)) (or nd (point-max)))
6031         (when (gnus-visual-p 'page-marker)
6032           (save-excursion
6033             (when nd
6034               (goto-char nd)
6035               (gnus-insert-next-page-button))
6036             (when st
6037               (goto-char st)
6038               (gnus-insert-prev-page-button))))))))
6039
6040 ;; Article mode commands
6041
6042 (defun gnus-article-goto-next-page ()
6043   "Show the next page of the article."
6044   (interactive)
6045   (when (gnus-article-next-page)
6046     (goto-char (point-min))
6047     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
6048
6049
6050 (defun gnus-article-goto-prev-page ()
6051   "Show the previous page of the article."
6052   (interactive)
6053   (if (save-restriction (widen) (bobp)) ;; Real beginning-of-buffer?
6054       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
6055     (gnus-article-prev-page nil)))
6056
6057 ;; This is cleaner but currently breaks `gnus-pick-mode':
6058 ;;
6059 ;; (defun gnus-article-goto-next-page ()
6060 ;;   "Show the next page of the article."
6061 ;;   (interactive)
6062 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6063 ;;     (gnus-summary-next-page)))
6064 ;;
6065 ;; (defun gnus-article-goto-prev-page ()
6066 ;;   "Show the next page of the article."
6067 ;;   (interactive)
6068 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
6069 ;;     (gnus-summary-prev-page)))
6070
6071 (defun gnus-article-next-page (&optional lines)
6072   "Show the next page of the current article.
6073 If end of article, return non-nil.  Otherwise return nil.
6074 Argument LINES specifies lines to be scrolled up."
6075   (interactive "p")
6076   (move-to-window-line -1)
6077   (if (and (not (and gnus-article-over-scroll
6078                      (> (count-lines (window-start) (point-max))
6079                         (+ (or lines (1- (window-height)))
6080                            (or (and (boundp 'scroll-margin)
6081                                     (symbol-value 'scroll-margin))
6082                                0)))))
6083            (save-excursion
6084              (end-of-line)
6085              (and (pos-visible-in-window-p)     ;Not continuation line.
6086                   (>= (1+ (point)) (point-max))))) ;Allow for trailing newline.
6087       ;; Nothing in this page.
6088       (if (or (not gnus-page-broken)
6089               (save-excursion
6090                 (save-restriction
6091                   (widen)
6092                   (forward-line)
6093                   (eobp)))) ;Real end-of-buffer?
6094           (progn
6095             (when gnus-article-over-scroll
6096               (gnus-article-next-page-1 lines))
6097             t)                  ;Nothing more.
6098         (gnus-narrow-to-page 1)         ;Go to next page.
6099         nil)
6100     ;; More in this page.
6101     (gnus-article-next-page-1 lines)
6102     nil))
6103
6104 (defmacro gnus-article-beginning-of-window ()
6105   "Move point to the beginning of the window.
6106 In Emacs, the point is placed at the line number which `scroll-margin'
6107 specifies."
6108   (if (featurep 'xemacs)
6109       '(move-to-window-line 0)
6110     '(move-to-window-line
6111       (min (max 0 scroll-margin)
6112            (max 1 (- (window-height)
6113                      (if mode-line-format 1 0)
6114                      (if header-line-format 1 0)))))))
6115
6116 (defun gnus-article-next-page-1 (lines)
6117   (when (and (not (featurep 'xemacs))
6118              (numberp lines)
6119              (> lines 0)
6120              (numberp (symbol-value 'scroll-margin))
6121              (> (symbol-value 'scroll-margin) 0))
6122     ;; Protect against the bug that Emacs 21.x hangs up when scrolling up for
6123     ;; too many number of lines if `scroll-margin' is set as two or greater.
6124     (setq lines (min lines
6125                      (max 0 (- (count-lines (window-start) (point-max))
6126                                (symbol-value 'scroll-margin))))))
6127   (condition-case ()
6128       (let ((scroll-in-place nil))
6129         (scroll-up lines))
6130     (end-of-buffer
6131      ;; Long lines may cause an end-of-buffer error.
6132      (goto-char (point-max))))
6133   (gnus-article-beginning-of-window))
6134
6135 (defun gnus-article-prev-page (&optional lines)
6136   "Show previous page of current article.
6137 Argument LINES specifies lines to be scrolled down."
6138   (interactive "p")
6139   (move-to-window-line 0)
6140   (if (and gnus-page-broken
6141            (bobp)
6142            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
6143       (progn
6144         (gnus-narrow-to-page -1)        ;Go to previous page.
6145         (goto-char (point-max))
6146         (recenter (if gnus-article-over-scroll
6147                       (if lines
6148                           (max (+ lines (or (and (boundp 'scroll-margin)
6149                                                  (symbol-value 'scroll-margin))
6150                                             0))
6151                                3)
6152                         (- (window-height) 2))
6153                     -1)))
6154     (prog1
6155         (condition-case ()
6156             (let ((scroll-in-place nil))
6157               (scroll-down lines))
6158           (beginning-of-buffer
6159            (goto-char (point-min))))
6160       (gnus-article-beginning-of-window))))
6161
6162 (defun gnus-article-only-boring-p ()
6163   "Decide whether there is only boring text remaining in the article.
6164 Something \"interesting\" is a word of at least two letters that does
6165 not have a face in `gnus-article-boring-faces'."
6166   (when (and gnus-article-skip-boring
6167              (boundp 'gnus-article-boring-faces)
6168              (symbol-value 'gnus-article-boring-faces))
6169     (save-excursion
6170       (let ((inhibit-point-motion-hooks t))
6171         (catch 'only-boring
6172           (while (re-search-forward "\\b\\w\\w" nil t)
6173             (forward-char -1)
6174             (when (not (gnus-intersection
6175                         (gnus-faces-at (point))
6176                         (symbol-value 'gnus-article-boring-faces)))
6177               (throw 'only-boring nil)))
6178           (throw 'only-boring t))))))
6179
6180 (defun gnus-article-refer-article ()
6181   "Read article specified by message-id around point."
6182   (interactive)
6183   (save-excursion
6184     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
6185     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
6186     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
6187         (let ((msg-id (concat "<" (match-string 0) ">")))
6188           (set-buffer gnus-summary-buffer)
6189           (gnus-summary-refer-article msg-id))
6190       (error "No references around point"))))
6191
6192 (defun gnus-article-show-summary ()
6193   "Reconfigure windows to show summary buffer."
6194   (interactive)
6195   (if (not (gnus-buffer-live-p gnus-summary-buffer))
6196       (error "There is no summary buffer for this article buffer")
6197     (gnus-article-set-globals)
6198     (gnus-configure-windows 'article)
6199     (gnus-summary-goto-subject gnus-current-article)
6200     (gnus-summary-position-point)))
6201
6202 (defun gnus-article-describe-briefly ()
6203   "Describe article mode commands briefly."
6204   (interactive)
6205   (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")))
6206
6207 (defun gnus-article-summary-command ()
6208   "Execute the last keystroke in the summary buffer."
6209   (interactive)
6210   (let ((obuf (current-buffer))
6211         (owin (current-window-configuration))
6212         func)
6213     (switch-to-buffer gnus-article-current-summary 'norecord)
6214     (setq func (lookup-key (current-local-map) (this-command-keys)))
6215     (call-interactively func)
6216     (set-buffer obuf)
6217     (set-window-configuration owin)
6218     (set-window-point (get-buffer-window (current-buffer)) (point))))
6219
6220 (defun gnus-article-summary-command-nosave ()
6221   "Execute the last keystroke in the summary buffer."
6222   (interactive)
6223   (let (func)
6224     (pop-to-buffer gnus-article-current-summary)
6225     (setq func (lookup-key (current-local-map) (this-command-keys)))
6226     (call-interactively func)))
6227
6228 (defun gnus-article-check-buffer ()
6229   "Beep if not in an article buffer."
6230   (unless (equal major-mode 'gnus-article-mode)
6231     (error "Command invoked outside of a Gnus article buffer")))
6232
6233 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
6234   "Read a summary buffer key sequence and execute it from the article buffer."
6235   (interactive "P")
6236   (gnus-article-check-buffer)
6237   (let ((nosaves
6238          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
6239            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
6240            "=" "^" "\M-^" "|"))
6241         (nosave-but-article
6242          '("A " "A<" "A>" "AM" "AP" "AR" "AT" "A\C-?" "A\M-\r" "A\r" "Ab" "Ae"
6243            "An" "Ap" [?A (meta return)] [?A delete]))
6244         (nosave-in-article
6245          '("AS" "\C-d"))
6246         (up-to-top
6247          '("n" "Gn" "p" "Gp"))
6248         keys new-sum-point)
6249     (with-current-buffer gnus-article-current-summary
6250       (let (gnus-pick-mode)
6251         (setq unread-command-events (nconc unread-command-events
6252                                            (list (or key last-command-event)))
6253               keys (if (featurep 'xemacs)
6254                        (events-to-keys (read-key-sequence nil t))
6255                      (read-key-sequence nil t)))))
6256
6257     (message "")
6258
6259     (cond
6260      ((eq (aref keys (1- (length keys))) ?\C-h)
6261       (gnus-article-describe-bindings (substring keys 0 -1)))
6262      ((or (member keys nosaves)
6263           (member keys nosave-but-article)
6264           (member keys nosave-in-article))
6265       (let (func)
6266         (save-window-excursion
6267           (pop-to-buffer gnus-article-current-summary)
6268           ;; We disable the pick minor mode commands.
6269           (let (gnus-pick-mode)
6270             (setq func (lookup-key (current-local-map) keys))))
6271         (if (or (not func)
6272                 (numberp func))
6273             (ding)
6274           (unless (member keys nosave-in-article)
6275             (set-buffer gnus-article-current-summary))
6276           (call-interactively func)
6277           (setq new-sum-point (point)))
6278         (when (member keys nosave-but-article)
6279           (pop-to-buffer gnus-article-buffer))))
6280      (t
6281       ;; These commands should restore window configuration.
6282       (let ((obuf (current-buffer))
6283             (owin (current-window-configuration))
6284             win func in-buffer selected new-sum-start new-sum-hscroll err)
6285         (cond (not-restore-window
6286                (pop-to-buffer gnus-article-current-summary)
6287                (setq win (selected-window)))
6288               ((setq win (get-buffer-window gnus-article-current-summary))
6289                (select-window win))
6290               (t
6291                (let ((summary-buffer gnus-article-current-summary))
6292                  (gnus-configure-windows 'article)
6293                  (unless (setq win (get-buffer-window summary-buffer 'visible))
6294                    (let ((gnus-buffer-configuration
6295                           '(article ((vertical 1.0
6296                                                (summary 0.25 point)
6297                                                (article 1.0))))))
6298                      (gnus-configure-windows 'article))
6299                    (setq win (get-buffer-window summary-buffer 'visible)))
6300                  (gnus-select-frame-set-input-focus (window-frame win))
6301                  (select-window win))))
6302         (setq in-buffer (current-buffer))
6303         ;; We disable the pick minor mode commands.
6304         (if (and (setq func (let (gnus-pick-mode)
6305                               (lookup-key (current-local-map) keys)))
6306                  (functionp func)
6307                  (condition-case code
6308                      (progn
6309                        (call-interactively func)
6310                        t)
6311                    (error
6312                     (setq err code)
6313                     nil)))
6314             (progn
6315               (when (eq win (selected-window))
6316                 (setq new-sum-point (point)
6317                       new-sum-start (window-start win)
6318                       new-sum-hscroll (window-hscroll win)))
6319               (when (or (eq in-buffer (current-buffer))
6320                         (when (eq obuf (current-buffer))
6321                           (set-buffer in-buffer)
6322                           t))
6323                 (setq selected (gnus-summary-select-article))
6324                 (set-buffer obuf)
6325                 (unless not-restore-window
6326                   (set-window-configuration owin))
6327                 (when (and (eq selected 'old)
6328                            new-sum-point)
6329                   (set-window-start (get-buffer-window (current-buffer))
6330                                     1)
6331                   (set-window-point (get-buffer-window (current-buffer))
6332                                     (if (article-goto-body)
6333                                         (1- (point))
6334                                       (point))))
6335                 (when (and (not not-restore-window)
6336                            new-sum-point
6337                            (with-current-buffer (window-buffer win)
6338                              (eq major-mode 'gnus-summary-mode)))
6339                   (set-window-point win new-sum-point)
6340                   (set-window-start win new-sum-start)
6341                   (set-window-hscroll win new-sum-hscroll))))
6342           (set-window-configuration owin)
6343           (if err
6344               (signal (car err) (cdr err))
6345             (ding))))))))
6346
6347 (defun gnus-article-read-summary-send-keys ()
6348   (interactive)
6349   (let ((unread-command-events (list (gnus-character-to-event ?S))))
6350     (gnus-article-read-summary-keys)))
6351
6352 (defun gnus-article-describe-key (key)
6353   "Display documentation of the function invoked by KEY.
6354 KEY is a string or a vector."
6355   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6356                        (read-key-sequence "Describe key: "))))
6357   (gnus-article-check-buffer)
6358   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6359                                   gnus-article-read-summary-send-keys))
6360       (with-current-buffer gnus-article-current-summary
6361         (setq unread-command-events
6362               (if (featurep 'xemacs)
6363                   (append key nil)
6364                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6365                                         (list 'meta (- x 128))
6366                                       x))
6367                         key)))
6368         (let ((cursor-in-echo-area t)
6369               gnus-pick-mode)
6370           (describe-key (read-key-sequence nil t))))
6371     (describe-key key)))
6372
6373 (defun gnus-article-describe-key-briefly (key &optional insert)
6374   "Display documentation of the function invoked by KEY.
6375 KEY is a string or a vector."
6376   (interactive (list (let ((cursor-in-echo-area t)) ;; better for XEmacs.
6377                        (read-key-sequence "Describe key: "))
6378                      current-prefix-arg))
6379   (gnus-article-check-buffer)
6380   (if (memq (key-binding key t) '(gnus-article-read-summary-keys
6381                                   gnus-article-read-summary-send-keys))
6382       (with-current-buffer gnus-article-current-summary
6383         (setq unread-command-events
6384               (if (featurep 'xemacs)
6385                   (append key nil)
6386                 (mapcar (lambda (x) (if (and (integerp x) (>= x 128))
6387                                         (list 'meta (- x 128))
6388                                       x))
6389                         key)))
6390         (let ((cursor-in-echo-area t)
6391               gnus-pick-mode)
6392           (describe-key-briefly (read-key-sequence nil t) insert)))
6393     (describe-key-briefly key insert)))
6394
6395 ;;`gnus-agent-mode' in gnus-agent.el will define it.
6396 (defvar gnus-agent-summary-mode)
6397 (defvar gnus-draft-mode)
6398
6399 (defun gnus-article-describe-bindings (&optional prefix)
6400   "Show a list of all defined keys, and their definitions.
6401 The optional argument PREFIX, if non-nil, should be a key sequence;
6402 then we display only bindings that start with that prefix."
6403   (interactive)
6404   (gnus-article-check-buffer)
6405   (let ((keymap (copy-keymap gnus-article-mode-map))
6406         (map (copy-keymap gnus-article-send-map))
6407         (sumkeys (where-is-internal 'gnus-article-read-summary-keys))
6408         agent draft)
6409     (define-key keymap "S" map)
6410     (define-key map [t] nil)
6411     (with-current-buffer gnus-article-current-summary
6412       (set-keymap-parent map (key-binding "S"))
6413       (let (key def gnus-pick-mode)
6414         (while sumkeys
6415           (setq key (pop sumkeys))
6416           (cond ((and (vectorp key) (= (length key) 1)
6417                       (consp (setq def (aref key 0)))
6418                       (numberp (car def)) (numberp (cdr def)))
6419                  (when (< (max (car def) (cdr def)) 128)
6420                    (setq sumkeys
6421                          (append (mapcar
6422                                   #'vector
6423                                   (nreverse (gnus-uncompress-range def)))
6424                                  sumkeys))))
6425                 ((setq def (key-binding key))
6426                  (unless (eq def 'undefined)
6427                    (define-key keymap key def))))))
6428       (when (boundp 'gnus-agent-summary-mode)
6429         (setq agent gnus-agent-summary-mode))
6430       (when (boundp 'gnus-draft-mode)
6431         (setq draft gnus-draft-mode)))
6432     (with-temp-buffer
6433       (use-local-map keymap)
6434       (set (make-local-variable 'gnus-agent-summary-mode) agent)
6435       (set (make-local-variable 'gnus-draft-mode) draft)
6436       (describe-bindings prefix))
6437     (let ((item `((lambda (prefix)
6438                     (with-current-buffer ,(current-buffer)
6439                       (gnus-article-describe-bindings prefix)))
6440                   ,prefix)))
6441       (with-current-buffer (if (fboundp 'help-buffer)
6442                                (let (help-xref-following) (help-buffer))
6443                              "*Help*") ;; Emacs 21
6444         (setq help-xref-stack-item item)))))
6445
6446 (defun gnus-article-reply-with-original (&optional wide)
6447   "Start composing a reply mail to the current message.
6448 The text in the region will be yanked.  If the region isn't active,
6449 the entire article will be yanked."
6450   (interactive)
6451   (let ((article (cdr gnus-article-current))
6452         contents)
6453     (if (not (gnus-region-active-p))
6454         (with-current-buffer gnus-summary-buffer
6455           (gnus-summary-reply (list (list article)) wide))
6456       (setq contents (buffer-substring (point) (mark t)))
6457       ;; Deactivate active regions.
6458       (when (and (boundp 'transient-mark-mode)
6459                  transient-mark-mode)
6460         (setq mark-active nil))
6461       (with-current-buffer gnus-summary-buffer
6462         (gnus-summary-reply
6463          (list (list article contents)) wide)))))
6464
6465 (defun gnus-article-wide-reply-with-original ()
6466   "Start composing a wide reply mail to the current message.
6467 The text in the region will be yanked.  If the region isn't active,
6468 the entire article will be yanked."
6469   (interactive)
6470   (gnus-article-reply-with-original t))
6471
6472 (defun gnus-article-followup-with-original ()
6473   "Compose a followup to the current article.
6474 The text in the region will be yanked.  If the region isn't active,
6475 the entire article will be yanked."
6476   (interactive)
6477   (let ((article (cdr gnus-article-current))
6478         contents)
6479       (if (not (gnus-region-active-p))
6480           (with-current-buffer gnus-summary-buffer
6481             (gnus-summary-followup (list (list article))))
6482         (setq contents (buffer-substring (point) (mark t)))
6483         ;; Deactivate active regions.
6484         (when (and (boundp 'transient-mark-mode)
6485                    transient-mark-mode)
6486           (setq mark-active nil))
6487         (with-current-buffer gnus-summary-buffer
6488           (gnus-summary-followup
6489            (list (list article contents)))))))
6490
6491 (defun gnus-article-hide (&optional arg force)
6492   "Hide all the gruft in the current article.
6493 This means that signatures, cited text and (some) headers will be
6494 hidden.
6495 If given a prefix, show the hidden text instead."
6496   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6497   (gnus-article-hide-headers arg)
6498   (gnus-article-hide-list-identifiers arg)
6499   (gnus-article-hide-citation-maybe arg force)
6500   (gnus-article-hide-signature arg))
6501
6502 (defun gnus-article-maybe-highlight ()
6503   "Do some article highlighting if article highlighting is requested."
6504   (when (gnus-visual-p 'article-highlight 'highlight)
6505     (gnus-article-highlight-some)))
6506
6507 (defun gnus-check-group-server ()
6508   ;; Make sure the connection to the server is alive.
6509   (unless (gnus-server-opened
6510            (gnus-find-method-for-group gnus-newsgroup-name))
6511     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6512     (gnus-request-group gnus-newsgroup-name t)))
6513
6514 (eval-when-compile
6515   (autoload 'nneething-get-file-name "nneething"))
6516
6517 (defun gnus-request-article-this-buffer (article group)
6518   "Get an article and insert it into this buffer."
6519   (let (do-update-line sparse-header)
6520     (prog1
6521         (save-excursion
6522           (erase-buffer)
6523           (gnus-kill-all-overlays)
6524           (setq group (or group gnus-newsgroup-name))
6525
6526           ;; Using `gnus-request-article' directly will insert the article into
6527           ;; `nntp-server-buffer' - so we'll save some time by not having to
6528           ;; copy it from the server buffer into the article buffer.
6529
6530           ;; We only request an article by message-id when we do not have the
6531           ;; headers for it, so we'll have to get those.
6532           (when (stringp article)
6533             (gnus-read-header article))
6534
6535           ;; If the article number is negative, that means that this article
6536           ;; doesn't belong in this newsgroup (possibly), so we find its
6537           ;; message-id and request it by id instead of number.
6538           (when (and (numberp article)
6539                      gnus-summary-buffer
6540                      (get-buffer gnus-summary-buffer)
6541                      (gnus-buffer-exists-p gnus-summary-buffer))
6542             (with-current-buffer gnus-summary-buffer
6543               (let ((header (gnus-summary-article-header article)))
6544                 (when (< article 0)
6545                   (cond
6546                    ((memq article gnus-newsgroup-sparse)
6547                     ;; This is a sparse gap article.
6548                     (setq do-update-line article)
6549                     (setq article (mail-header-id header))
6550                     (setq sparse-header (gnus-read-header article))
6551                     (setq gnus-newsgroup-sparse
6552                           (delq article gnus-newsgroup-sparse)))
6553                    ((vectorp header)
6554                     ;; It's a real article.
6555                     (setq article (mail-header-id header)))
6556                    (t
6557                     ;; It is an extracted pseudo-article.
6558                     (setq article 'pseudo)
6559                     (gnus-request-pseudo-article header))))
6560
6561                 (let ((method (gnus-find-method-for-group
6562                                gnus-newsgroup-name)))
6563                   (when (and (eq (car method) 'nneething)
6564                              (vectorp header))
6565                     (let ((dir (nneething-get-file-name
6566                                 (mail-header-id header))))
6567                       (when (and (stringp dir)
6568                                  (file-directory-p dir))
6569                         (setq article 'nneething)
6570                         (gnus-group-enter-directory dir))))))))
6571
6572           (cond
6573            ;; Refuse to select canceled articles.
6574            ((and (numberp article)
6575                  gnus-summary-buffer
6576                  (get-buffer gnus-summary-buffer)
6577                  (gnus-buffer-exists-p gnus-summary-buffer)
6578                  (eq (cdr (with-current-buffer gnus-summary-buffer
6579                             (assq article gnus-newsgroup-reads)))
6580                      gnus-canceled-mark))
6581             nil)
6582            ;; We first check `gnus-original-article-buffer'.
6583            ((and (get-buffer gnus-original-article-buffer)
6584                  (numberp article)
6585                  (with-current-buffer gnus-original-article-buffer
6586                    (and (equal (car gnus-original-article) group)
6587                         (eq (cdr gnus-original-article) article))))
6588             ;; `insert-buffer-substring' would incorrectly use the
6589             ;; equivalent of string-make-multibyte which amount to decoding
6590             ;; with locale-coding-system, causing failure of
6591             ;; subsequent decoding.
6592             (insert (mm-string-to-multibyte
6593                      (with-current-buffer gnus-original-article-buffer
6594                        (buffer-substring (point-min) (point-max)))))
6595             'article)
6596            ;; Check the backlog.
6597            ((and gnus-keep-backlog
6598                  (gnus-backlog-request-article group article (current-buffer)))
6599             'article)
6600            ;; Check asynchronous pre-fetch.
6601            ((gnus-async-request-fetched-article group article (current-buffer))
6602             (gnus-async-prefetch-next group article gnus-summary-buffer)
6603             (when (and (numberp article) gnus-keep-backlog)
6604               (gnus-backlog-enter-article group article (current-buffer)))
6605             'article)
6606            ;; Check the cache.
6607            ((and gnus-use-cache
6608                  (numberp article)
6609                  (gnus-cache-request-article article group))
6610             'article)
6611            ;; Check the agent cache.
6612            ((gnus-agent-request-article article group)
6613             'article)
6614            ;; Get the article and put into the article buffer.
6615            ((or (stringp article)
6616                 (numberp article))
6617             (let ((gnus-override-method gnus-override-method)
6618                   (methods (and (stringp article)
6619                                 gnus-refer-article-method))
6620                   (backend (car (gnus-find-method-for-group
6621                                  gnus-newsgroup-name)))
6622                   result
6623                   (inhibit-read-only t))
6624               (if (or (not (listp methods))
6625                       (and (symbolp (car methods))
6626                            (assq (car methods) nnoo-definition-alist)))
6627                   (setq methods (list methods)))
6628               (when (and (null gnus-override-method)
6629                          methods)
6630                 (setq gnus-override-method (pop methods)))
6631               (while (not result)
6632                 (when (eq gnus-override-method 'current)
6633                   (setq gnus-override-method
6634                         (with-current-buffer gnus-summary-buffer
6635                           gnus-current-select-method)))
6636                 (erase-buffer)
6637                 (gnus-kill-all-overlays)
6638                 (let ((gnus-newsgroup-name group))
6639                   (gnus-check-group-server))
6640                 (cond
6641                  ((gnus-request-article article group (current-buffer))
6642                   (when (numberp article)
6643                     (gnus-async-prefetch-next group article
6644                                               gnus-summary-buffer)
6645                     (when gnus-keep-backlog
6646                       (gnus-backlog-enter-article
6647                        group article (current-buffer))))
6648                   (setq result 'article))
6649                  (methods
6650                   (setq gnus-override-method (pop methods)))
6651                  ((not (string-match "^400 "
6652                                      (nnheader-get-report backend)))
6653                   ;; If we get 400 server disconnect, reconnect and
6654                   ;; retry; otherwise, assume the article has expired.
6655                   (setq result 'done))))
6656               (and (eq result 'article) 'article)))
6657            ;; It was a pseudo.
6658            (t article)))
6659
6660       ;; Associate this article with the current summary buffer.
6661       (setq gnus-article-current-summary gnus-summary-buffer)
6662
6663       ;; Take the article from the original article buffer
6664       ;; and place it in the buffer it's supposed to be in.
6665       (when (and (get-buffer gnus-article-buffer)
6666                  (equal (buffer-name (current-buffer))
6667                         (buffer-name (get-buffer gnus-article-buffer))))
6668         (save-excursion
6669           (if (get-buffer gnus-original-article-buffer)
6670               (set-buffer gnus-original-article-buffer)
6671             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6672             (buffer-disable-undo)
6673             (setq major-mode 'gnus-original-article-mode)
6674             (setq buffer-read-only t))
6675           (let ((inhibit-read-only t))
6676             (erase-buffer)
6677             (insert-buffer-substring gnus-article-buffer))
6678           (setq gnus-original-article (cons group article)))
6679
6680         ;; Decode charsets.
6681         (run-hooks 'gnus-article-decode-hook)
6682         ;; Mark article as decoded or not.
6683         (setq gnus-article-decoded-p gnus-article-decode-hook))
6684
6685       ;; Update sparse articles.
6686       (when (and do-update-line
6687                  (or (numberp article)
6688                      (stringp article)))
6689         (let ((buf (current-buffer)))
6690           (set-buffer gnus-summary-buffer)
6691           (gnus-summary-update-article do-update-line sparse-header)
6692           (gnus-summary-goto-subject do-update-line nil t)
6693           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6694                             (point))
6695           (set-buffer buf))))))
6696
6697 ;;;
6698 ;;; Article editing
6699 ;;;
6700
6701 (defcustom gnus-article-edit-mode-hook nil
6702   "Hook run in article edit mode buffers."
6703   :group 'gnus-article-various
6704   :type 'hook)
6705
6706 (defvar gnus-article-edit-done-function nil)
6707
6708 (defvar gnus-article-edit-mode-map nil)
6709
6710 ;; Should we be using derived.el for this?
6711 (unless gnus-article-edit-mode-map
6712   (setq gnus-article-edit-mode-map (make-keymap))
6713   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6714
6715   (gnus-define-keys gnus-article-edit-mode-map
6716     "\C-c?"    describe-mode
6717     "\C-c\C-c" gnus-article-edit-done
6718     "\C-c\C-k" gnus-article-edit-exit
6719     "\C-c\C-f\C-t" message-goto-to
6720     "\C-c\C-f\C-o" message-goto-from
6721     "\C-c\C-f\C-b" message-goto-bcc
6722     ;;"\C-c\C-f\C-w" message-goto-fcc
6723     "\C-c\C-f\C-c" message-goto-cc
6724     "\C-c\C-f\C-s" message-goto-subject
6725     "\C-c\C-f\C-r" message-goto-reply-to
6726     "\C-c\C-f\C-n" message-goto-newsgroups
6727     "\C-c\C-f\C-d" message-goto-distribution
6728     "\C-c\C-f\C-f" message-goto-followup-to
6729     "\C-c\C-f\C-m" message-goto-mail-followup-to
6730     "\C-c\C-f\C-k" message-goto-keywords
6731     "\C-c\C-f\C-u" message-goto-summary
6732     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6733     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6734     "\C-c\C-b" message-goto-body
6735     "\C-c\C-i" message-goto-signature
6736
6737     "\C-c\C-t" message-insert-to
6738     "\C-c\C-n" message-insert-newsgroups
6739     "\C-c\C-o" message-sort-headers
6740     "\C-c\C-e" message-elide-region
6741     "\C-c\C-v" message-delete-not-region
6742     "\C-c\C-z" message-kill-to-signature
6743     "\M-\r" message-newline-and-reformat
6744     "\C-c\C-a" mml-attach-file
6745     "\C-a" message-beginning-of-line
6746     "\t" message-tab
6747     "\M-;" comment-region)
6748
6749   (gnus-define-keys (gnus-article-edit-wash-map
6750                      "\C-c\C-w" gnus-article-edit-mode-map)
6751     "f" gnus-article-edit-full-stops))
6752
6753 (easy-menu-define
6754   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6755   '("Field"
6756     ["Fetch To" message-insert-to t]
6757     ["Fetch Newsgroups" message-insert-newsgroups t]
6758     "----"
6759     ["To" message-goto-to t]
6760     ["From" message-goto-from t]
6761     ["Subject" message-goto-subject t]
6762     ["Cc" message-goto-cc t]
6763     ["Reply-To" message-goto-reply-to t]
6764     ["Summary" message-goto-summary t]
6765     ["Keywords" message-goto-keywords t]
6766     ["Newsgroups" message-goto-newsgroups t]
6767     ["Followup-To" message-goto-followup-to t]
6768     ["Mail-Followup-To" message-goto-mail-followup-to t]
6769     ["Distribution" message-goto-distribution t]
6770     ["Body" message-goto-body t]
6771     ["Signature" message-goto-signature t]))
6772
6773 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6774   "Major mode for editing articles.
6775 This is an extended text-mode.
6776
6777 \\{gnus-article-edit-mode-map}"
6778   (make-local-variable 'gnus-article-edit-done-function)
6779   (make-local-variable 'gnus-prev-winconf)
6780   (set (make-local-variable 'font-lock-defaults)
6781        '(message-font-lock-keywords t))
6782   (set (make-local-variable 'mail-header-separator) "")
6783   (set (make-local-variable 'gnus-article-edit-mode) t)
6784   (easy-menu-add message-mode-field-menu message-mode-map)
6785   (mml-mode)
6786   (setq buffer-read-only nil)
6787   (buffer-enable-undo)
6788   (widen))
6789
6790 (defun gnus-article-edit (&optional force)
6791   "Edit the current article.
6792 This will have permanent effect only in mail groups.
6793 If FORCE is non-nil, allow editing of articles even in read-only
6794 groups."
6795   (interactive "P")
6796   (when (and (not force)
6797              (gnus-group-read-only-p))
6798     (error "The current newsgroup does not support article editing"))
6799   (gnus-article-date-original)
6800   (gnus-article-edit-article
6801    'ignore
6802    `(lambda (no-highlight)
6803       'ignore
6804       (gnus-summary-edit-article-done
6805        ,(or (mail-header-references gnus-current-headers) "")
6806        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
6807
6808 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
6809   "Start editing the contents of the current article buffer."
6810   (let ((winconf (current-window-configuration)))
6811     (set-buffer gnus-article-buffer)
6812     (let ((message-auto-save-directory
6813            ;; Don't associate the article buffer with a draft file.
6814            nil))
6815       (gnus-article-edit-mode))
6816     (funcall start-func)
6817     (set-buffer-modified-p nil)
6818     (gnus-configure-windows 'edit-article)
6819     (setq gnus-article-edit-done-function exit-func)
6820     (setq gnus-prev-winconf winconf)
6821     (unless quiet
6822       (gnus-message 6 "C-c C-c to end edits"))))
6823
6824 (defun gnus-article-edit-done (&optional arg)
6825   "Update the article edits and exit."
6826   (interactive "P")
6827   (let ((func gnus-article-edit-done-function)
6828         (buf (current-buffer))
6829         (start (window-start))
6830         (p (point))
6831         (winconf gnus-prev-winconf))
6832     (widen) ;; Widen it in case that users narrowed the buffer.
6833     (funcall func arg)
6834     (set-buffer buf)
6835     ;; The cache and backlog have to be flushed somewhat.
6836     (when gnus-keep-backlog
6837       (gnus-backlog-remove-article
6838        (car gnus-article-current) (cdr gnus-article-current)))
6839     ;; Flush original article as well.
6840     (when (get-buffer gnus-original-article-buffer)
6841       (with-current-buffer gnus-original-article-buffer
6842         (setq gnus-original-article nil)))
6843     (when gnus-use-cache
6844       (gnus-cache-update-article
6845        (car gnus-article-current) (cdr gnus-article-current)))
6846     ;; We remove all text props from the article buffer.
6847     (kill-all-local-variables)
6848     (set-text-properties (point-min) (point-max) nil)
6849     (gnus-article-mode)
6850     (set-window-configuration winconf)
6851     (set-buffer buf)
6852     (set-window-start (get-buffer-window buf) start)
6853     (set-window-point (get-buffer-window buf) (point)))
6854   (gnus-summary-show-article))
6855
6856 (defun gnus-article-edit-exit ()
6857   "Exit the article editing without updating."
6858   (interactive)
6859   (when (or (not (buffer-modified-p))
6860             (yes-or-no-p "Article modified; kill anyway? "))
6861     (let ((curbuf (current-buffer))
6862           (p (point))
6863           (window-start (window-start)))
6864       (erase-buffer)
6865       (if (gnus-buffer-live-p gnus-original-article-buffer)
6866           (insert-buffer-substring gnus-original-article-buffer))
6867       (let ((winconf gnus-prev-winconf))
6868         (kill-all-local-variables)
6869         (gnus-article-mode)
6870         (set-window-configuration winconf)
6871         ;; Tippy-toe some to make sure that point remains where it was.
6872         (save-current-buffer
6873           (set-buffer curbuf)
6874           (set-window-start (get-buffer-window (current-buffer)) window-start)
6875           (goto-char p))))
6876     (gnus-summary-show-article)))
6877
6878 (defun gnus-article-edit-full-stops ()
6879   "Interactively repair spacing at end of sentences."
6880   (interactive)
6881   (save-excursion
6882     (goto-char (point-min))
6883     (search-forward-regexp "^$" nil t)
6884     (let ((case-fold-search nil))
6885       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
6886
6887 ;;;
6888 ;;; Article highlights
6889 ;;;
6890
6891 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
6892
6893 ;;; Internal Variables:
6894
6895 (defcustom gnus-button-url-regexp
6896   (concat
6897    "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|"
6898    "nntp\\|news\\|telnet\\|wais\\|mailto\\|info\\):\\)"
6899    "\\(//[-a-z0-9_.]+:[0-9]*\\)?"
6900    (if (string-match "[[:digit:]]" "1") ;; Support POSIX?
6901        (let ((chars "-a-z0-9_=#$@~%&*+\\/[:word:]")
6902              (punct "!?:;.,"))
6903          (concat
6904           "\\(?:"
6905           ;; Match paired parentheses, e.g. in Wikipedia URLs:
6906           ;; http://thread.gmane.org/47B4E3B2.3050402@gmail.com
6907           "[" chars punct "]+" "(" "[" chars punct "]+" "[" chars "]*)" "[" chars "]*"
6908           "\\|"
6909           "[" chars punct     "]+" "[" chars "]"
6910           "\\)"))
6911      (concat ;; XEmacs 21.4 doesn't support POSIX.
6912       "\\([-a-z0-9_=!?#$@~%&*+\\/:;.,]\\|\\w\\)+"
6913       "\\([-a-z0-9_=#$@~%&*+\\/]\\|\\w\\)"))
6914    "\\)")
6915   "Regular expression that matches URLs."
6916   :group 'gnus-article-buttons
6917   :type 'regexp)
6918
6919 (defcustom gnus-button-valid-fqdn-regexp
6920   message-valid-fqdn-regexp
6921   "Regular expression that matches a valid FQDN."
6922   :version "22.1"
6923   :group 'gnus-article-buttons
6924   :type 'regexp)
6925
6926 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
6927 (defcustom gnus-button-valid-localpart-regexp
6928   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t @]*"
6929   "Regular expression that matches a localpart of mail addresses or MIDs."
6930   :version "22.1"
6931   :group 'gnus-article-buttons
6932   :type 'regexp)
6933
6934 (defcustom gnus-button-man-handler 'manual-entry
6935   "Function to use for displaying man pages.
6936 The function must take at least one argument with a string naming the
6937 man page."
6938   :version "22.1"
6939   :type '(choice (function-item :tag "Man" manual-entry)
6940                  (function-item :tag "Woman" woman)
6941                  (function :tag "Other"))
6942   :group 'gnus-article-buttons)
6943
6944 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
6945   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
6946 If the default site is too slow, try to find a CTAN mirror, see
6947 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
6948 the variable `gnus-button-handle-ctan'."
6949   :version "22.1"
6950   :group 'gnus-article-buttons
6951   :link '(custom-manual "(gnus)Group Parameters")
6952   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
6953                  (const "http://tug.ctan.org/tex-archive/")
6954                  (const "http://www.dante.de/CTAN/")
6955                  (string :tag "Other")))
6956
6957 (defcustom gnus-button-ctan-handler 'browse-url
6958   "Function to use for displaying CTAN links.
6959 The function must take one argument, the string naming the URL."
6960   :version "22.1"
6961   :type '(choice (function-item :tag "Browse Url" browse-url)
6962                  (function :tag "Other"))
6963   :group 'gnus-article-buttons)
6964
6965 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
6966   "Bogus strings removed from CTAN URLs."
6967   :version "22.1"
6968   :group 'gnus-article-buttons
6969   :type '(choice (const "^/?tex-archive/\\|/")
6970                  (regexp :tag "Other")))
6971
6972 (defcustom gnus-button-ctan-directory-regexp
6973   (regexp-opt
6974    (list "archive-tools" "biblio" "bibliography" "digests" "documentation"
6975          "dviware" "fonts" "graphics" "help" "indexing" "info" "language"
6976          "languages" "macros" "nonfree" "obsolete" "support" "systems"
6977          "tds" "tools" "usergrps" "web") t)
6978   "Regular expression for ctan directories.
6979 It should match all directories in the top level of `gnus-ctan-url'."
6980   :version "22.1"
6981   :group 'gnus-article-buttons
6982   :type 'regexp)
6983
6984 (defcustom gnus-button-mid-or-mail-regexp
6985   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
6986           gnus-button-valid-fqdn-regexp
6987           ">?\\)\\b")
6988   "Regular expression that matches a message ID or a mail address."
6989   :version "22.1"
6990   :group 'gnus-article-buttons
6991   :type 'regexp)
6992
6993 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
6994   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
6995 Strings like this can be either a message ID or a mail address.  If it is one
6996 of the symbols `mid' or `mail', Gnus will always assume that the string is a
6997 message ID or a mail address, respectively.  If this variable is set to the
6998 symbol `ask', always query the user what do do.  If it is a function, this
6999 function will be called with the string as its only argument.  The function
7000 must return `mid', `mail', `invalid' or `ask'."
7001   :version "22.1"
7002   :group 'gnus-article-buttons
7003   :type '(choice (function-item :tag "Heuristic function"
7004                                 gnus-button-mid-or-mail-heuristic)
7005                  (const ask)
7006                  (const mid)
7007                  (const mail)))
7008
7009 (defcustom gnus-button-mid-or-mail-heuristic-alist
7010   '((-10.0 . ".+\\$.+@")
7011     (-10.0 . "#")
7012     (-10.0 . "\\*")
7013     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
7014     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
7015     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
7016     (-1.0  . "^[^a-z]+@")
7017     ;;
7018     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
7019     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
7020     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
7021     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
7022     ;;
7023     (-2.0  . "^[0-9]")
7024     (-1.0  . "^[0-9][0-9]")
7025     ;;
7026     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
7027     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7028     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
7029     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
7030     ;;
7031     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
7032     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
7033     ;;       "[0-9]{8,}.*\@"
7034     (-3.0
7035      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
7036     ;; "[0-9]{12,}.*\@"
7037     ;; compensation for TDMA dated mail addresses:
7038     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
7039     ;;
7040     (-20.0 . "\\.fsf@") ;; Gnus
7041     (-20.0 . "^slrn")
7042     (-20.0 . "^Pine")
7043     (-20.0 . "^alpine\\.")
7044     (-20.0 . "_-_") ;; Subject change in thread
7045     ;;
7046     (-20.0 . "\\.ln@") ;; leafnode
7047     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
7048     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
7049     ;;
7050     ;; (5.0 . "") ;; $local_part_len <= 7
7051     (10.0  . "^[^0-9]+@")
7052     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
7053     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
7054     (3.0   . "\@stud")
7055     ;;
7056     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
7057     ;;
7058     (0.5   . "^[A-Z][a-z]")
7059     (0.5   . "^[A-Z][a-z][a-z]")
7060     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
7061     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
7062   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
7063
7064 A negative RATE indicates a message IDs, whereas a positive indicates a mail
7065 address.  The REGEXP is processed with `case-fold-search' set to nil."
7066   :version "22.1"
7067   :group 'gnus-article-buttons
7068   :type '(repeat (cons (number :tag "Rate")
7069                        (regexp :tag "Regexp"))))
7070
7071 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
7072   "Guess whether MID-OR-MAIL is a message ID or a mail address.
7073 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
7074 address, `ask' if unsure and `invalid' if the string is invalid."
7075   (let ((case-fold-search nil)
7076         (list gnus-button-mid-or-mail-heuristic-alist)
7077         (result 0) rate regexp lpartlen elem)
7078     (setq lpartlen
7079           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
7080     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
7081     ;; Certain special cases...
7082     (when (string-match
7083            (concat
7084             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
7085             "^[0-9]+\\.[0-9]+@compuserve\\|"
7086             "@public\\.gmane\\.org")
7087            mid-or-mail)
7088       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
7089       (setq result 'mail))
7090     (when (string-match "@.*@\\| " mid-or-mail)
7091       (gnus-message 8 "`%s' is invalid." mid-or-mail)
7092       (setq result 'invalid))
7093     ;; Nothing more to do, if result is not a number here...
7094     (when (numberp result)
7095       (while list
7096         (setq elem (car list)
7097               rate (car elem)
7098               regexp (cdr elem)
7099               list (cdr list))
7100         (when (string-match regexp mid-or-mail)
7101           (setq result (+ result rate))
7102           (gnus-message
7103            9 "`%s' matched `%s', rate `%s', result `%s'."
7104            mid-or-mail regexp rate result)))
7105       (when (<= lpartlen 7)
7106         (setq result (+ result 5.0))
7107         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
7108                       mid-or-mail result))
7109       (when (>= lpartlen 12)
7110         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
7111         (cond
7112          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
7113           ;; Long local part should contain realname if e-mail address,
7114           ;; too many digits: message-id.
7115           ;; $score -= 5.0 + 0.1 * $local_part_len;
7116           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
7117           (setq result (+ result rate))
7118           (gnus-message
7119            9 "Many digits in `%s', rate `%s', result `%s'."
7120            mid-or-mail rate result))
7121          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
7122                         mid-or-mail)
7123           ;; Too few vowels [^aeiouy]{4,}.*\@
7124           (setq result (+ result -5.0))
7125           (gnus-message
7126            9 "Few vowels in `%s', rate `%s', result `%s'."
7127            mid-or-mail -5.0 result))
7128          (t
7129           (setq result (+ result 5.0))
7130           (gnus-message
7131            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
7132     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
7133     ;; Maybe we should make this a customizable alist: (condition . 'result)
7134     (cond
7135      ((symbolp result) result)
7136      ;; Now convert number into proper results:
7137      ((< result -10.0) 'mid)
7138      ((> result  10.0) 'mail)
7139      (t 'ask))))
7140
7141 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
7142   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
7143          (url-mid (concat "news" ":" mid-or-mail))
7144          (url-mailto (concat "mailto" ":" mid-or-mail)))
7145     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
7146     (when (fboundp pref)
7147       (setq guessed
7148             ;; get rid of surrounding angles...
7149             (funcall pref
7150                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
7151       (if (or (eq 'mid guessed) (eq 'mail guessed))
7152           (setq pref guessed)
7153         (setq pref 'ask)))
7154     (if (eq pref 'ask)
7155         (save-window-excursion
7156           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
7157               (setq pref 'mail)
7158             (setq pref 'mid))))
7159     (cond ((eq pref 'mid)
7160            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
7161            (gnus-button-handle-news url-mid))
7162           ((eq pref 'mail)
7163            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
7164            (gnus-url-mailto url-mailto))
7165           (t (gnus-message 3 "Invalid string.")))))
7166
7167 (defun gnus-button-handle-custom (fun arg)
7168   "Call function FUN on argument ARG.
7169 Both FUN and ARG are supposed to be strings.  ARG will be passed
7170 as a symbol to FUN."
7171   (funcall (intern fun)
7172            (if (string-match "^customize-apropos" fun)
7173                arg
7174              (intern arg))))
7175
7176 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
7177
7178 ;; FIXME: Maybe we should merge some of the functions that do quite similar
7179 ;; stuff?
7180
7181 (defun gnus-button-handle-describe-function (url)
7182   "Call `describe-function' when pushing the corresponding URL button."
7183   (describe-function
7184    (intern
7185     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7186
7187 (defun gnus-button-handle-describe-variable (url)
7188   "Call `describe-variable' when pushing the corresponding URL button."
7189   (describe-variable
7190    (intern
7191     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
7192
7193 (defun gnus-button-handle-symbol (url)
7194 "Display help on variable or function.
7195 Calls `describe-variable' or `describe-function'."
7196   (let ((sym (intern url)))
7197     (cond
7198      ((fboundp sym) (describe-function sym))
7199      ((boundp sym) (describe-variable sym))
7200      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
7201
7202 (defun gnus-button-handle-describe-key (url)
7203   "Call `describe-key' when pushing the corresponding URL button."
7204   (let* ((key-string
7205           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
7206          (keys (ignore-errors (eval `(kbd ,key-string)))))
7207     (if keys
7208         (describe-key keys)
7209       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
7210
7211 (defun gnus-button-handle-apropos (url)
7212   "Call `apropos' when pushing the corresponding URL button."
7213   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7214
7215 (defun gnus-button-handle-apropos-command (url)
7216   "Call `apropos' when pushing the corresponding URL button."
7217   (apropos-command
7218    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7219
7220 (defun gnus-button-handle-apropos-variable (url)
7221   "Call `apropos' when pushing the corresponding URL button."
7222   (funcall
7223    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
7224    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7225
7226 (defun gnus-button-handle-apropos-documentation (url)
7227   "Call `apropos' when pushing the corresponding URL button."
7228   (funcall
7229    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
7230    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
7231
7232 (defun gnus-button-handle-library (url)
7233   "Call `locate-library' when pushing the corresponding URL button."
7234   (gnus-message 9 "url=`%s'" url)
7235   (let* ((lib (locate-library url))
7236          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
7237     (if (not lib)
7238         (gnus-message 1 "Cannot locale library `%s'." url)
7239       (find-file-read-only file))))
7240
7241 (defun gnus-button-handle-ctan (url)
7242   "Call `browse-url' when pushing a CTAN URL button."
7243   (funcall
7244    gnus-button-ctan-handler
7245    (concat
7246     gnus-ctan-url
7247     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
7248
7249 (defcustom gnus-button-tex-level 5
7250   "*Integer that says how many TeX-related buttons Gnus will show.
7251 The higher the number, the more buttons will appear and the more false
7252 positives are possible.  Note that you can set this variable local to
7253 specific groups.  Setting it higher in TeX groups is probably a good idea.
7254 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7255 how to set variables in specific groups."
7256   :version "22.1"
7257   :group 'gnus-article-buttons
7258   :link '(custom-manual "(gnus)Group Parameters")
7259   :type 'integer)
7260
7261 (defcustom gnus-button-man-level 5
7262   "*Integer that says how many man-related buttons Gnus will show.
7263 The higher the number, the more buttons will appear and the more false
7264 positives are possible.  Note that you can set this variable local to
7265 specific groups.  Setting it higher in Unix groups is probably a good idea.
7266 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
7267 how to set variables in specific groups."
7268   :version "22.1"
7269   :group 'gnus-article-buttons
7270   :link '(custom-manual "(gnus)Group Parameters")
7271   :type 'integer)
7272
7273 (defcustom gnus-button-emacs-level 5
7274   "*Integer that says how many emacs-related buttons Gnus will show.
7275 The higher the number, the more buttons will appear and the more false
7276 positives are possible.  Note that you can set this variable local to
7277 specific groups.  Setting it higher in Emacs or Gnus related groups is
7278 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
7279 `gnus-parameters' on how to set variables in specific groups."
7280   :version "22.1"
7281   :group 'gnus-article-buttons
7282   :link '(custom-manual "(gnus)Group Parameters")
7283   :type 'integer)
7284
7285 (defcustom gnus-button-message-level 5
7286   "*Integer that says how many buttons for news or mail messages will appear.
7287 The higher the number, the more buttons will appear and the more false
7288 positives are possible."
7289   ;; mail addresses, MIDs, URLs for news, ...
7290   :version "22.1"
7291   :group 'gnus-article-buttons
7292   :type 'integer)
7293
7294 (defcustom gnus-button-browse-level 5
7295   "*Integer that says how many buttons for browsing will appear.
7296 The higher the number, the more buttons will appear and the more false
7297 positives are possible."
7298   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
7299   :version "22.1"
7300   :group 'gnus-article-buttons
7301   :type 'integer)
7302
7303 (defcustom gnus-button-alist
7304   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
7305      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
7306     ((concat "\\b\\(nntp\\|news\\):\\("
7307              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
7308      0 t gnus-button-handle-news 2)
7309     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
7310      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
7311     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
7312      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
7313     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
7314     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
7315      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
7316     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7317      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7318     ("\\b\\(mid\\|message-id\\):? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
7319      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
7320     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
7321      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
7322     ;; RFC 2368 (The mailto URL scheme)
7323     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7324      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7325     ("\\bmailto:\\([^ \n\t]+\\)"
7326      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7327     ;; CTAN
7328     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
7329              gnus-button-ctan-directory-regexp
7330              "[^][>)!;:,'\n\t ]+\\)")
7331      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
7332     ((concat "\\btex-archive/\\("
7333              gnus-button-ctan-directory-regexp
7334              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
7335      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
7336     ((concat
7337       "\\b\\("
7338       gnus-button-ctan-directory-regexp
7339       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
7340      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
7341     ;; This is info (home-grown style) <info://foo/bar+baz>
7342     ("\\binfo://\\([^'\">\n\t ]+\\)"
7343      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
7344     ;; Info GNOME style <info:foo#bar_baz>
7345     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
7346      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
7347     ;; Info KDE style <info:(foo)bar baz>
7348     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
7349      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
7350     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
7351      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
7352     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET\\([ \t\n,]*\\)\\)?"
7353      ;; Info links like `C-h i d m Gnus RET' or `C-h i d m Gnus RET i partial RET'
7354      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 0)
7355     ;; This is custom
7356     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
7357      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
7358     ;; Emacs help commands
7359     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7360      ;; regexp doesn't match arguments containing ` '.
7361      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
7362     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7363      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
7364     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7365      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
7366     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7367      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
7368     ;; The following entries may lead to many false positives so don't enable
7369     ;; them by default (use a high button level).
7370     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
7371      ;; Exclude [.?] for URLs in gmane.emacs.cvs
7372      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7373     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
7374      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
7375     ("`\\([a-z][a-z0-9]+-[a-z0-9]+-[-a-z0-9]*[a-z]\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
7376      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
7377     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
7378      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
7379     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
7380      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
7381     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7382      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
7383     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7384      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7385     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7386      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7387     ("`\\(\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7388      ;; Unlike the other regexps we really have to require quoting
7389      ;; here to determine where it ends.
7390      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7391     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7392     ("<URL: *\\([^\n<>]*\\)>"
7393      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7394     ;; RFC 2396 (2.4.3., delims) ...
7395     ("\"URL: *\\([^\n\"]*\\)\""
7396      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7397     ;; Raw URLs.
7398     (gnus-button-url-regexp
7399      0 (>= gnus-button-browse-level 0) browse-url 0)
7400     ;; man pages
7401     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7402      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7403      gnus-button-handle-man 1)
7404     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7405     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7406      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7407      gnus-button-handle-man 1)
7408     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7409     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7410     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7411      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7412     ;; Recognizing patches to .el files.  This is somewhat obscure,
7413     ;; but considering the percentage of Gnus users who hack Emacs
7414     ;; Lisp files...
7415     ("^--- \\([^ .]+\\.el\\).*\n.*\n@@ -?\\([0-9]+\\)" 1
7416      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7417     ("^\\*\\*\\* \\([^ .]+\\.el\\).*\n.*\n\\*+\n\\*\\*\\* \\([0-9]+\\)" 1
7418      (>= gnus-button-message-level 4) gnus-button-patch 1 2)
7419     ;; MID or mail: To avoid too many false positives we don't try to catch
7420     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7421     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7422     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7423     ;; so that non-ambiguous entries (see above) match first.
7424     (gnus-button-mid-or-mail-regexp
7425      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7426   "*Alist of regexps matching buttons in article bodies.
7427
7428 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7429 REGEXP: is the string (case insensitive) matching text around the button (can
7430 also be Lisp expression evaluating to a string),
7431 BUTTON: is the number of the regexp grouping actually matching the button,
7432 FORM: is a Lisp expression which must eval to true for the button to
7433 be added,
7434 CALLBACK: is the function to call when the user push this button, and each
7435 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7436
7437 CALLBACK can also be a variable, in that case the value of that
7438 variable it the real callback function."
7439   :group 'gnus-article-buttons
7440   :type '(repeat (list (choice regexp variable sexp)
7441                        (integer :tag "Button")
7442                        (sexp :tag "Form")
7443                        (function :tag "Callback")
7444                        (repeat :tag "Par"
7445                                :inline t
7446                                (integer :tag "Regexp group")))))
7447 (put 'gnus-button-alist 'risky-local-variable t)
7448
7449 (defcustom gnus-header-button-alist
7450   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7451      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7452     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7453      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7454     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7455      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7456     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7457      0 (>= gnus-button-browse-level 0) browse-url 0)
7458     ("^Subject:" gnus-button-url-regexp
7459      0 (>= gnus-button-browse-level 0) browse-url 0)
7460     ("^[^:]+:" gnus-button-url-regexp
7461      0 (>= gnus-button-browse-level 0) browse-url 0)
7462     ("^OpenPGP:.*url=" gnus-button-url-regexp
7463      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7464     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7465      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7466     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7467      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7468   "*Alist of headers and regexps to match buttons in article heads.
7469
7470 This alist is very similar to `gnus-button-alist', except that each
7471 alist has an additional HEADER element first in each entry:
7472
7473 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7474
7475 HEADER is a regexp to match a header.  For a fuller explanation, see
7476 `gnus-button-alist'."
7477   :group 'gnus-article-buttons
7478   :group 'gnus-article-headers
7479   :type '(repeat (list (regexp :tag "Header")
7480                        (choice regexp variable)
7481                        (integer :tag "Button")
7482                        (sexp :tag "Form")
7483                        (function :tag "Callback")
7484                        (repeat :tag "Par"
7485                                :inline t
7486                                (integer :tag "Regexp group")))))
7487 (put 'gnus-header-button-alist 'risky-local-variable t)
7488
7489 ;;; Commands:
7490
7491 (defun gnus-article-push-button (event)
7492   "Check text under the mouse pointer for a callback function.
7493 If the text under the mouse pointer has a `gnus-callback' property,
7494 call it with the value of the `gnus-data' text property."
7495   (interactive "e")
7496   (set-buffer (window-buffer (posn-window (event-start event))))
7497   (let* ((pos (posn-point (event-start event)))
7498          (data (get-text-property pos 'gnus-data))
7499          (fun (get-text-property pos 'gnus-callback)))
7500     (goto-char pos)
7501     (when fun
7502       (funcall fun data))))
7503
7504 (defun gnus-article-press-button ()
7505   "Check text at point for a callback function.
7506 If the text at point has a `gnus-callback' property,
7507 call it with the value of the `gnus-data' text property."
7508   (interactive)
7509   (let ((data (get-text-property (point) 'gnus-data))
7510         (fun (get-text-property (point) 'gnus-callback)))
7511     (when fun
7512       (funcall fun data))))
7513
7514 (defun gnus-article-highlight (&optional force)
7515   "Highlight current article.
7516 This function calls `gnus-article-highlight-headers',
7517 `gnus-article-highlight-citation',
7518 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7519 do the highlighting.  See the documentation for those functions."
7520   (interactive (list 'force))
7521   (gnus-article-highlight-headers)
7522   (gnus-article-highlight-citation force)
7523   (gnus-article-highlight-signature)
7524   (gnus-article-add-buttons force)
7525   (gnus-article-add-buttons-to-head))
7526
7527 (defun gnus-article-highlight-some (&optional force)
7528   "Highlight current article.
7529 This function calls `gnus-article-highlight-headers',
7530 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7531 do the highlighting.  See the documentation for those functions."
7532   (interactive (list 'force))
7533   (gnus-article-highlight-headers)
7534   (gnus-article-highlight-signature)
7535   (gnus-article-add-buttons))
7536
7537 (defun gnus-article-highlight-headers ()
7538   "Highlight article headers as specified by `gnus-header-face-alist'."
7539   (interactive)
7540   (gnus-with-article-headers
7541     (let (regexp header-face field-face from hpoints fpoints)
7542       (dolist (entry gnus-header-face-alist)
7543         (goto-char (point-min))
7544         (setq regexp (concat "^\\("
7545                              (if (string-equal "" (nth 0 entry))
7546                                  "[^\t ]"
7547                                (nth 0 entry))
7548                              "\\)")
7549               header-face (nth 1 entry)
7550               field-face (nth 2 entry))
7551         (while (and (re-search-forward regexp nil t)
7552                     (not (eobp)))
7553           (beginning-of-line)
7554           (setq from (point))
7555           (unless (search-forward ":" nil t)
7556             (forward-char 1))
7557           (when (and header-face
7558                      (not (memq (point) hpoints)))
7559             (push (point) hpoints)
7560             (gnus-put-text-property from (point) 'face header-face))
7561           (when (and field-face
7562                      (not (memq (setq from (point)) fpoints)))
7563             (push from fpoints)
7564             (if (re-search-forward "^[^ \t]" nil t)
7565                 (forward-char -2)
7566               (goto-char (point-max)))
7567             (gnus-put-text-property from (point) 'face field-face)))))))
7568
7569 (defun gnus-article-highlight-signature ()
7570   "Highlight the signature in an article.
7571 It does this by highlighting everything after
7572 `gnus-signature-separator' using the face `gnus-signature'."
7573   (interactive)
7574   (gnus-with-article-buffer
7575     (let ((inhibit-point-motion-hooks t))
7576       (save-restriction
7577         (when (and gnus-signature-face
7578                    (gnus-article-narrow-to-signature))
7579           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max) nil t)
7580                             'face gnus-signature-face)
7581           (widen)
7582           (gnus-article-search-signature)
7583           (let ((start (match-beginning 0))
7584                 (end (set-marker (make-marker) (1+ (match-end 0)))))
7585             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
7586                                      end)))))))
7587
7588 (defun gnus-button-in-region-p (b e prop)
7589   "Say whether PROP exists in the region."
7590   (text-property-not-all b e prop nil))
7591
7592 (defun gnus-article-add-buttons (&optional force)
7593   "Find external references in the article and make buttons of them.
7594 \"External references\" are things like Message-IDs and URLs, as
7595 specified by `gnus-button-alist'."
7596   (interactive (list 'force))
7597   (gnus-with-article-buffer
7598     (let ((inhibit-point-motion-hooks t)
7599           (case-fold-search t)
7600           (alist gnus-button-alist)
7601           beg entry regexp)
7602       ;; Remove all old markers.
7603       (let (marker entry new-list)
7604         (while (setq marker (pop gnus-button-marker-list))
7605           (if (or (< marker (point-min)) (>= marker (point-max)))
7606               (push marker new-list)
7607             (goto-char marker)
7608             (when (setq entry (gnus-button-entry))
7609               (put-text-property (match-beginning (nth 1 entry))
7610                                  (match-end (nth 1 entry))
7611                                  'gnus-callback nil))
7612             (set-marker marker nil)))
7613         (setq gnus-button-marker-list new-list))
7614       ;; We skip the headers.
7615       (article-goto-body)
7616       (setq beg (point))
7617       (while (setq entry (pop alist))
7618         (setq regexp (eval (car entry)))
7619         (goto-char beg)
7620         (while (re-search-forward regexp nil t)
7621           (let ((start (match-beginning (nth 1 entry)))
7622                 (end (match-end (nth 1 entry)))
7623                 (from (match-beginning 0)))
7624             (when (and (or (eq t (nth 2 entry))
7625                            (eval (nth 2 entry)))
7626                        (not (gnus-button-in-region-p
7627                              start end 'gnus-callback)))
7628               ;; That optional form returned non-nil, so we add the
7629               ;; button.
7630               (setq from (set-marker (make-marker) from))
7631               (push from gnus-button-marker-list)
7632               (unless (and (eq (car entry) 'gnus-button-url-regexp)
7633                            (gnus-article-extend-url-button from start end))
7634                 (gnus-article-add-button start end
7635                                          'gnus-button-push from)))))))))
7636
7637 (defun gnus-article-extend-url-button (beg start end)
7638   "Extend url button if url is folded into two or more lines.
7639 Return non-nil if button is extended.  BEG is a marker that points to
7640 the beginning position of a text containing url.  START and END are
7641 the endpoints of a url button before it is extended.  The concatenated
7642 url is put as the `gnus-button-url' overlay property on the button."
7643   (let ((opoint (point))
7644         (points (list start end))
7645         url delim regexp)
7646     (prog1
7647         (when (and (progn
7648                      (goto-char end)
7649                      (not (looking-at "[\t ]*[\">]")))
7650                    (progn
7651                      (goto-char start)
7652                      (string-match
7653                       "\\(?:\"\\|\\(<\\)\\)[\t ]*\\(?:url[\t ]*:[\t ]*\\)?\\'"
7654                       (buffer-substring (point-at-bol) start)))
7655                    (progn
7656                      (setq url (list (buffer-substring start end))
7657                            delim (if (match-beginning 1) ">" "\""))
7658                      (beginning-of-line)
7659                      (setq regexp (concat
7660                                    (when (and (looking-at
7661                                                message-cite-prefix-regexp)
7662                                               (< (match-end 0) start))
7663                                      (regexp-quote (match-string 0)))
7664                                    "\
7665 \[\t ]*\\(?:\\([^\t\n \">]+\\)[\t ]*$\\|\\([^\t\n \">]*\\)[\t ]*"
7666                                    delim "\\)"))
7667                      (while (progn
7668                               (forward-line 1)
7669                               (and (looking-at regexp)
7670                                    (prog1
7671                                        (match-beginning 1)
7672                                      (push (or (match-string 2)
7673                                                (match-string 1))
7674                                            url)
7675                                      (push (setq end (or (match-end 2)
7676                                                          (match-end 1)))
7677                                            points)
7678                                      (push (or (match-beginning 2)
7679                                                (match-beginning 1))
7680                                            points)))))
7681                      (match-beginning 2)))
7682           (let (gnus-article-mouse-face widget-mouse-face)
7683             (while points
7684               (gnus-article-add-button (pop points) (pop points)
7685                                        'gnus-button-push beg)))
7686           (let ((overlay (gnus-make-overlay start end)))
7687             (gnus-overlay-put overlay 'evaporate t)
7688             (gnus-overlay-put overlay 'gnus-button-url
7689                               (list (mapconcat 'identity (nreverse url) "")))
7690             (when gnus-article-mouse-face
7691               (gnus-overlay-put overlay 'mouse-face gnus-article-mouse-face)))
7692           t)
7693       (goto-char opoint))))
7694
7695 ;; Add buttons to the head of an article.
7696 (defun gnus-article-add-buttons-to-head ()
7697   "Add buttons to the head of the article."
7698   (interactive)
7699   (gnus-with-article-headers
7700     (let (beg end)
7701       (dolist (entry gnus-header-button-alist)
7702         ;; Each alist entry.
7703         (goto-char (point-min))
7704         (while (re-search-forward (car entry) nil t)
7705           ;; Each header matching the entry.
7706           (setq beg (match-beginning 0))
7707           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7708                              (match-beginning 0))
7709                         (point-max)))
7710           (goto-char beg)
7711           (while (re-search-forward (eval (nth 1 entry)) end t)
7712             ;; Each match within a header.
7713             (let* ((entry (cdr entry))
7714                    (start (match-beginning (nth 1 entry)))
7715                    (end (match-end (nth 1 entry)))
7716                    (form (nth 2 entry)))
7717               (goto-char (match-end 0))
7718               (when (eval form)
7719                 (gnus-article-add-button
7720                  start end (nth 3 entry)
7721                  (buffer-substring (match-beginning (nth 4 entry))
7722                                    (match-end (nth 4 entry)))))))
7723           (goto-char end))))))
7724
7725 ;;; External functions:
7726
7727 (defun gnus-article-add-button (from to fun &optional data)
7728   "Create a button between FROM and TO with callback FUN and data DATA."
7729   (when gnus-article-button-face
7730     (gnus-overlay-put (gnus-make-overlay from to nil t)
7731                       'face gnus-article-button-face))
7732   (gnus-add-text-properties
7733    from to
7734    (nconc (and gnus-article-mouse-face
7735                (list gnus-mouse-face-prop gnus-article-mouse-face))
7736           (list 'gnus-callback fun)
7737           (and data (list 'gnus-data data))))
7738   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7739                          :button-keymap gnus-widget-button-keymap))
7740
7741 ;;; Internal functions:
7742
7743 (defun gnus-article-set-globals ()
7744   (with-current-buffer gnus-summary-buffer
7745     (gnus-set-global-variables)))
7746
7747 (defun gnus-signature-toggle (end)
7748   (gnus-with-article-buffer
7749     (let ((inhibit-point-motion-hooks t))
7750       (if (text-property-any end (point-max) 'article-type 'signature)
7751           (progn
7752             (gnus-delete-wash-type 'signature)
7753             (gnus-remove-text-properties-when
7754              'article-type 'signature end (point-max)
7755              (cons 'article-type (cons 'signature
7756                                        gnus-hidden-properties))))
7757         (gnus-add-wash-type 'signature)
7758         (gnus-add-text-properties-when
7759          'article-type nil end (point-max)
7760          (cons 'article-type (cons 'signature
7761                                    gnus-hidden-properties)))))
7762     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7763       (gnus-set-mode-line 'article))))
7764
7765 (defun gnus-button-entry ()
7766   ;; Return the first entry in `gnus-button-alist' matching this place.
7767   (let ((alist gnus-button-alist)
7768         (entry nil))
7769     (while alist
7770       (setq entry (pop alist))
7771       (if (looking-at (eval (car entry)))
7772           (setq alist nil)
7773         (setq entry nil)))
7774     entry))
7775
7776 (defun gnus-button-push (marker)
7777   ;; Push button starting at MARKER.
7778   (save-excursion
7779     (goto-char marker)
7780     (let* ((entry (gnus-button-entry))
7781            (inhibit-point-motion-hooks t)
7782            (fun (nth 3 entry))
7783            (args (or (and (eq (car entry) 'gnus-button-url-regexp)
7784                           (get-char-property marker 'gnus-button-url))
7785                      (mapcar (lambda (group)
7786                                (let ((string (match-string group)))
7787                                  (set-text-properties
7788                                   0 (length string) nil string)
7789                                  string))
7790                              (nthcdr 4 entry)))))
7791       (cond
7792        ((fboundp fun)
7793         (apply fun args))
7794        ((and (boundp fun)
7795              (fboundp (symbol-value fun)))
7796         (apply (symbol-value fun) args))
7797        (t
7798         (gnus-message 1 "You must define `%S' to use this button"
7799                       (cons fun args)))))))
7800
7801 (defun gnus-parse-news-url (url)
7802   (let (scheme server port group message-id articles)
7803     (with-temp-buffer
7804       (insert url)
7805       (goto-char (point-min))
7806       (when (looking-at "\\([A-Za-z]+\\):")
7807         (setq scheme (match-string 1))
7808         (goto-char (match-end 0)))
7809       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7810         (setq server (match-string 1))
7811         (setq port (if (stringp (match-string 3))
7812                        (string-to-number (match-string 3))
7813                      (match-string 3)))
7814         (goto-char (match-end 0)))
7815
7816       (cond
7817        ((looking-at "\\(.*@.*\\)")
7818         (setq message-id (match-string 1)))
7819        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7820         (setq group (match-string 1)
7821               articles (split-string (match-string 2) "-")))
7822        ((looking-at "\\([^/]+\\)/?")
7823         (setq group (match-string 1)))
7824        (t
7825         (error "Unknown news URL syntax"))))
7826     (list scheme server port group message-id articles)))
7827
7828 (defun gnus-button-handle-news (url)
7829   "Fetch a news URL."
7830   (destructuring-bind (scheme server port group message-id articles)
7831       (gnus-parse-news-url url)
7832     (cond
7833      (message-id
7834       (with-current-buffer gnus-summary-buffer
7835         (if server
7836             (let ((gnus-refer-article-method
7837                    (nconc (list (list 'nntp server))
7838                           gnus-refer-article-method))
7839                   (nntp-port-number (or port "nntp")))
7840               (gnus-message 7 "Fetching %s with %s"
7841                             message-id gnus-refer-article-method)
7842               (gnus-summary-refer-article message-id))
7843           (gnus-summary-refer-article message-id))))
7844      (group
7845       (gnus-button-fetch-group url)))))
7846
7847 (defun gnus-button-patch (library line)
7848   "Visit an Emacs Lisp library LIBRARY on line LINE."
7849   (interactive)
7850   (let ((file (locate-library (file-name-nondirectory library))))
7851     (unless file
7852       (error "Couldn't find library %s" library))
7853     (find-file file)
7854     (goto-line (string-to-number line))))
7855
7856 (defun gnus-button-handle-man (url)
7857   "Fetch a man page."
7858   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7859   (when (eq gnus-button-man-handler 'woman)
7860     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
7861   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7862   (funcall gnus-button-man-handler url))
7863
7864 (defun gnus-button-handle-info-url (url)
7865   "Fetch an info URL."
7866   (setq url (mm-subst-char-in-string ?+ ?\  url))
7867   (cond
7868    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
7869     (gnus-info-find-node
7870      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
7871                      "Gnus")
7872              ")" (gnus-url-unhex-string (match-string 2 url)))))
7873    ((string-match "([^)\"]+)[^\"]+" url)
7874     (setq url
7875           (gnus-replace-in-string
7876            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
7877     (gnus-info-find-node url))
7878    (t (error "Can't parse %s" url))))
7879
7880 (defun gnus-button-handle-info-url-gnome (url)
7881   "Fetch GNOME style info URL."
7882   (setq url (mm-subst-char-in-string ?_ ?\  url))
7883   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
7884       (gnus-info-find-node
7885        (concat "("
7886                (gnus-url-unhex-string
7887                  (match-string 1 url))
7888                ")"
7889                (or (gnus-url-unhex-string
7890                     (match-string 2 url))
7891                    "Top")))
7892     (error "Can't parse %s" url)))
7893
7894 (defun gnus-button-handle-info-url-kde (url)
7895   "Fetch KDE style info URL."
7896   (gnus-info-find-node (gnus-url-unhex-string url)))
7897
7898 ;; (info) will autoload info.el
7899 (declare-function Info-menu "info" (menu-item &optional fork))
7900 (declare-function Info-index-next "info" (num))
7901
7902 (defun gnus-button-handle-info-keystrokes (url)
7903   "Call `info' when pushing the corresponding URL button."
7904   ;; For links like `C-h i d m gnus RET part RET , ,', `C-h i d m CC Mode RET'.
7905   (let (node indx comma)
7906     (if (string-match
7907          (concat "\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+"
7908                  "\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
7909                  "\\([ \t\n]+i[ \t\n]+[^ ]+ ?[^ ]+[ \t\n]+RET"
7910                  "\\(?:[ \t\n,]*\\)\\)?")
7911          url)
7912         (setq node (match-string 2 url)
7913               indx (match-string 3 url))
7914       (error "Can't parse %s" url))
7915     (info)
7916     (Info-directory)
7917     (Info-menu node)
7918     (when (> (length indx) 0)
7919       (string-match (concat "[ \t\n]+i[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
7920                             "\\([ \t\n,]*\\)")
7921                     indx)
7922       (setq comma (match-string 2 indx))
7923       (setq indx  (match-string 1 indx))
7924       (Info-index indx)
7925       (when comma
7926         (dotimes (i (with-temp-buffer
7927                       (insert comma)
7928                       ;; Note: the XEmacs version of `how-many' takes
7929                       ;; no optional argument.
7930                       (goto-char (point-min))
7931                       (how-many ",")))
7932           (Info-index-next 1)))
7933       nil)))
7934
7935 ;; Called after pgg-snarf-keys-region, which autoloads pgg.el.
7936 (declare-function pgg-display-output-buffer "pgg" (start end status))
7937
7938 (defun gnus-button-openpgp (url)
7939   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
7940   (with-temp-buffer
7941     (mm-url-insert-file-contents-external url)
7942     (pgg-snarf-keys-region (point-min) (point-max))
7943     (pgg-display-output-buffer nil nil nil)))
7944
7945 (defun gnus-button-message-id (message-id)
7946   "Fetch MESSAGE-ID."
7947   (with-current-buffer gnus-summary-buffer
7948     (gnus-summary-refer-article message-id)))
7949
7950 (defun gnus-button-fetch-group (address &rest ignore)
7951   "Fetch GROUP specified by ADDRESS."
7952   (when (string-match "\\`\\(nntp\\|news\\):\\(//\\)?\\(.*\\)\\'"
7953                       address)
7954     ;; Allow to use `gnus-button-fetch-group' in `browse-url-browser-function'
7955     ;; for nntp:// and news://
7956     (setq address (match-string 3 address)))
7957   (if (not (string-match "[:/]" address))
7958       ;; This is just a simple group url.
7959       (gnus-group-read-ephemeral-group address gnus-select-method)
7960     (if (not
7961          (string-match
7962           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
7963           address))
7964         (error "Can't parse %s" address)
7965       (gnus-group-read-ephemeral-group
7966        (match-string 4 address)
7967        `(nntp ,(match-string 1 address)
7968               (nntp-address ,(match-string 1 address))
7969               (nntp-port-number ,(if (match-end 3)
7970                                      (match-string 3 address)
7971                                    "nntp")))
7972        nil nil nil
7973        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
7974
7975 (defun gnus-url-parse-query-string (query &optional downcase)
7976   (let (retval pairs cur key val)
7977     (setq pairs (split-string query "&"))
7978     (while pairs
7979       (setq cur (car pairs)
7980             pairs (cdr pairs))
7981       (if (not (string-match "=" cur))
7982           nil                           ; Grace
7983         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
7984               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
7985         (if downcase
7986             (setq key (downcase key)))
7987         (setq cur (assoc key retval))
7988         (if cur
7989             (setcdr cur (cons val (cdr cur)))
7990           (setq retval (cons (list key val) retval)))))
7991     retval))
7992
7993 (defun gnus-url-mailto (url)
7994   ;; Send mail to someone
7995   (when (string-match "mailto:/*\\(.*\\)" url)
7996     (setq url (substring url (match-beginning 1) nil)))
7997   (let (to args subject func)
7998     (setq args (gnus-url-parse-query-string
7999                 (if (string-match "^\\?" url)
8000                     (substring url 1)
8001                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
8002                       (concat "to=" (match-string 1 url) "&"
8003                               (match-string 2 url))
8004                     (concat "to=" url)))
8005                 t)
8006           subject (cdr-safe (assoc "subject" args)))
8007     (gnus-msg-mail)
8008     (while args
8009       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
8010       (if (fboundp func)
8011           (funcall func)
8012         (message-position-on-field (caar args)))
8013       (insert (gnus-replace-in-string
8014                (mapconcat 'identity (reverse (cdar args)) ", ")
8015                "\r\n" "\n" t))
8016       (setq args (cdr args)))
8017     (if subject
8018         (message-goto-body)
8019       (message-goto-subject))))
8020
8021 (defun gnus-button-embedded-url (address)
8022   "Activate ADDRESS with `browse-url'."
8023   (browse-url (gnus-strip-whitespace address)))
8024
8025 ;;; Next/prev buttons in the article buffer.
8026
8027 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
8028 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
8029
8030 (defvar gnus-prev-page-map
8031   (let ((map (make-sparse-keymap)))
8032     (define-key map gnus-mouse-2 'gnus-button-prev-page)
8033     (define-key map "\r" 'gnus-button-prev-page)
8034     map))
8035
8036 (defvar gnus-next-page-map
8037   (let ((map (make-sparse-keymap)))
8038     (unless (>= emacs-major-version 21)
8039       ;; XEmacs doesn't care.
8040       (set-keymap-parent map gnus-article-mode-map))
8041     (define-key map gnus-mouse-2 'gnus-button-next-page)
8042     (define-key map "\r" 'gnus-button-next-page)
8043     map))
8044
8045 (defun gnus-insert-prev-page-button ()
8046   (let ((b (point)) e
8047         (inhibit-read-only t))
8048     (gnus-eval-format
8049      gnus-prev-page-line-format nil
8050      `(keymap ,gnus-prev-page-map
8051               gnus-prev t
8052               gnus-callback gnus-article-button-prev-page
8053               article-type annotation))
8054     (setq e (if (bolp)
8055                 ;; Exclude a newline.
8056                 (1- (point))
8057               (point)))
8058     (when gnus-article-button-face
8059       (gnus-overlay-put (gnus-make-overlay b e nil t)
8060                         'face gnus-article-button-face))
8061     (widget-convert-button
8062      'link b e
8063      :action 'gnus-button-prev-page
8064      :button-keymap gnus-prev-page-map)))
8065
8066 (defun gnus-button-next-page (&optional args more-args)
8067   "Go to the next page."
8068   (interactive)
8069   (let ((win (selected-window)))
8070     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8071     (gnus-article-next-page)
8072     (select-window win)))
8073
8074 (defun gnus-button-prev-page (&optional args more-args)
8075   "Go to the prev page."
8076   (interactive)
8077   (let ((win (selected-window)))
8078     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8079     (gnus-article-prev-page)
8080     (select-window win)))
8081
8082 (defun gnus-insert-next-page-button ()
8083   (let ((b (point)) e
8084         (inhibit-read-only t))
8085     (gnus-eval-format gnus-next-page-line-format nil
8086                       `(keymap ,gnus-next-page-map
8087                                gnus-next t
8088                                gnus-callback gnus-article-button-next-page
8089                                article-type annotation))
8090     (setq e (if (bolp)
8091                 ;; Exclude a newline.
8092                 (1- (point))
8093               (point)))
8094     (when gnus-article-button-face
8095       (gnus-overlay-put (gnus-make-overlay b e nil t)
8096                         'face gnus-article-button-face))
8097     (widget-convert-button
8098      'link b e
8099      :action 'gnus-button-next-page
8100      :button-keymap gnus-next-page-map)))
8101
8102 (defun gnus-article-button-next-page (arg)
8103   "Go to the next page."
8104   (interactive "P")
8105   (let ((win (selected-window)))
8106     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8107     (gnus-article-next-page)
8108     (select-window win)))
8109
8110 (defun gnus-article-button-prev-page (arg)
8111   "Go to the prev page."
8112   (interactive "P")
8113   (let ((win (selected-window)))
8114     (select-window (gnus-get-buffer-window gnus-article-buffer t))
8115     (gnus-article-prev-page)
8116     (select-window win)))
8117
8118 (defvar gnus-decode-header-methods
8119   '(mail-decode-encoded-word-region)
8120   "List of methods used to decode headers.
8121
8122 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
8123 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
8124 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
8125 whose names match REGEXP.
8126
8127 For example:
8128 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
8129  mail-decode-encoded-word-region
8130  (\"chinese\" . rfc1843-decode-region))
8131 ")
8132
8133 (defvar gnus-decode-header-methods-cache nil)
8134
8135 (defun gnus-multi-decode-header (start end)
8136   "Apply the functions from `gnus-encoded-word-methods' that match."
8137   (unless (and gnus-decode-header-methods-cache
8138                (eq gnus-newsgroup-name
8139                    (car gnus-decode-header-methods-cache)))
8140     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
8141     (dolist (x gnus-decode-header-methods)
8142       (if (symbolp x)
8143           (nconc gnus-decode-header-methods-cache (list x))
8144         (if (and gnus-newsgroup-name
8145                  (string-match (car x) gnus-newsgroup-name))
8146             (nconc gnus-decode-header-methods-cache
8147                    (list (cdr x)))))))
8148   (let ((xlist gnus-decode-header-methods-cache))
8149     (pop xlist)
8150     (save-restriction
8151       (narrow-to-region start end)
8152       (while xlist
8153         (funcall (pop xlist) (point-min) (point-max))))))
8154
8155 ;;;
8156 ;;; Treatment top-level handling.
8157 ;;;
8158
8159 (defun gnus-treat-article (condition &optional part-number total-parts type)
8160   (let ((length (- (point-max) (point-min)))
8161         (alist gnus-treatment-function-alist)
8162         (article-goto-body-goes-to-point-min-p t)
8163         (treated-type
8164          (or (not type)
8165              (catch 'found
8166                (let ((list gnus-article-treat-types))
8167                  (while list
8168                    (when (string-match (pop list) type)
8169                      (throw 'found t)))))))
8170         (highlightp (gnus-visual-p 'article-highlight 'highlight))
8171         val elem)
8172     (gnus-run-hooks 'gnus-part-display-hook)
8173     (dolist (elem alist)
8174       (setq val
8175             (save-excursion
8176               (when (gnus-buffer-live-p gnus-summary-buffer)
8177                 (set-buffer gnus-summary-buffer))
8178               (symbol-value (car elem))))
8179       (when (and (or (consp val)
8180                      treated-type)
8181                  (gnus-treat-predicate val)
8182                  (or (not (get (car elem) 'highlight))
8183                      highlightp))
8184         (save-restriction
8185           (funcall (cadr elem)))))))
8186
8187 ;; Dynamic variables.
8188 (defvar part-number)
8189 (defvar total-parts)
8190 (defvar type)
8191 (defvar condition)
8192 (defvar length)
8193
8194 (defun gnus-treat-predicate (val)
8195   (cond
8196    ((null val)
8197     nil)
8198    (condition
8199     (eq condition val))
8200    ((and (listp val)
8201          (stringp (car val)))
8202     (apply 'gnus-or (mapcar `(lambda (s)
8203                                (string-match s ,(or gnus-newsgroup-name "")))
8204                             val)))
8205    ((listp val)
8206     (let ((pred (pop val)))
8207       (cond
8208        ((eq pred 'or)
8209         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
8210        ((eq pred 'and)
8211         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
8212        ((eq pred 'not)
8213         (not (gnus-treat-predicate (car val))))
8214        ((eq pred 'typep)
8215         (equal (car val) type))
8216        (t
8217         (error "%S is not a valid predicate" pred)))))
8218    ((eq val t)
8219     t)
8220    ((eq val 'head)
8221     nil)
8222    ((eq val 'first)
8223     (eq part-number 1))
8224    ((eq val 'last)
8225     (eq part-number total-parts))
8226    ((numberp val)
8227     (< length val))
8228    (t
8229     (error "%S is not a valid value" val))))
8230
8231 (defun gnus-article-encrypt-body (protocol &optional n)
8232   "Encrypt the article body."
8233   (interactive
8234    (list
8235     (or gnus-article-encrypt-protocol
8236         (completing-read "Encrypt protocol: "
8237                          gnus-article-encrypt-protocol-alist
8238                          nil t))
8239     current-prefix-arg))
8240   ;; User might hit `K E' instead of `K e', so prompt once.
8241   (when (and gnus-article-encrypt-protocol
8242              gnus-novice-user)
8243     (unless (gnus-y-or-n-p "Really encrypt article(s)? ")
8244       (error "Encrypt aborted.")))
8245   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
8246     (unless func
8247       (error "Can't find the encrypt protocol %s" protocol))
8248     (if (member gnus-newsgroup-name '("nndraft:delayed"
8249                                       "nndraft:drafts"
8250                                       "nndraft:queue"))
8251         (error "Can't encrypt the article in group %s"
8252                gnus-newsgroup-name))
8253     (gnus-summary-iterate n
8254       (with-current-buffer gnus-summary-buffer
8255         (let ((mail-parse-charset gnus-newsgroup-charset)
8256               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
8257               (summary-buffer gnus-summary-buffer)
8258               references point)
8259           (gnus-set-global-variables)
8260           (when (gnus-group-read-only-p)
8261             (error "The current newsgroup does not support article encrypt"))
8262           (gnus-summary-show-article t)
8263           (setq references
8264               (or (mail-header-references gnus-current-headers) ""))
8265           (set-buffer gnus-article-buffer)
8266           (let* ((inhibit-read-only t)
8267                  (headers
8268                   (mapcar (lambda (field)
8269                             (and (save-restriction
8270                                    (message-narrow-to-head)
8271                                    (goto-char (point-min))
8272                                    (search-forward field nil t))
8273                                  (prog2
8274                                      (message-narrow-to-field)
8275                                      (buffer-string)
8276                                    (delete-region (point-min) (point-max))
8277                                    (widen))))
8278                           '("Content-Type:" "Content-Transfer-Encoding:"
8279                             "Content-Disposition:"))))
8280             (message-narrow-to-head)
8281             (message-remove-header "MIME-Version")
8282             (goto-char (point-max))
8283             (setq point (point))
8284             (insert (apply 'concat headers))
8285             (widen)
8286             (narrow-to-region point (point-max))
8287             (let ((message-options message-options))
8288               (message-options-set 'message-sender user-mail-address)
8289               (message-options-set 'message-recipients user-mail-address)
8290               (message-options-set 'message-sign-encrypt 'not)
8291               (funcall func))
8292             (goto-char (point-min))
8293             (insert "MIME-Version: 1.0\n")
8294             (widen)
8295             (gnus-summary-edit-article-done
8296              references nil summary-buffer t))
8297           (when gnus-keep-backlog
8298             (gnus-backlog-remove-article
8299              (car gnus-article-current) (cdr gnus-article-current)))
8300           (when (get-buffer gnus-original-article-buffer)
8301             (with-current-buffer gnus-original-article-buffer
8302               (setq gnus-original-article nil)))
8303           (when gnus-use-cache
8304             (gnus-cache-update-article
8305              (car gnus-article-current) (cdr gnus-article-current))))))))
8306
8307 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
8308   "The following specs can be used:
8309 %t  The security MIME type
8310 %i  Additional info
8311 %d  Details
8312 %D  Details if button is pressed")
8313
8314 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
8315   "The following specs can be used:
8316 %t  The security MIME type
8317 %i  Additional info
8318 %d  Details
8319 %D  Details if button is pressed")
8320
8321 (defvar gnus-mime-security-button-line-format-alist
8322   '((?t gnus-tmp-type ?s)
8323     (?i gnus-tmp-info ?s)
8324     (?d gnus-tmp-details ?s)
8325     (?D gnus-tmp-pressed-details ?s)))
8326
8327 (defvar gnus-mime-security-button-commands
8328   '((gnus-article-press-button "\r" "Show Detail")
8329     (undefined "v")
8330     (undefined "t")
8331     (undefined "C")
8332     (gnus-mime-security-save-part "o" "Save...")
8333     (undefined "\C-o")
8334     (undefined "r")
8335     (undefined "d")
8336     (undefined "c")
8337     (undefined "i")
8338     (undefined "E")
8339     (undefined "e")
8340     (undefined "p")
8341     (gnus-mime-security-pipe-part "|" "Pipe To Command...")
8342     (undefined ".")))
8343
8344 (defvar gnus-mime-security-button-map
8345   (let ((map (make-sparse-keymap)))
8346     (define-key map gnus-mouse-2 'gnus-article-push-button)
8347     (define-key map gnus-down-mouse-3 'gnus-mime-security-button-menu)
8348     (dolist (c gnus-mime-security-button-commands)
8349       (define-key map (cadr c) (car c)))
8350     map))
8351
8352 (easy-menu-define
8353   gnus-mime-security-button-menu gnus-mime-security-button-map
8354   "Security button menu."
8355   `("Security Part"
8356     ,@(delq nil
8357             (mapcar (lambda (c)
8358                       (unless (eq (car c) 'undefined)
8359                         (vector (caddr c) (car c) :active t)))
8360                     gnus-mime-security-button-commands))))
8361
8362 (defun gnus-mime-security-button-menu (event prefix)
8363   "Construct a context-sensitive menu of security commands."
8364   (interactive "e\nP")
8365   (save-window-excursion
8366     (let ((pos (event-start event)))
8367       (select-window (posn-window pos))
8368       (goto-char (posn-point pos))
8369       (gnus-article-check-buffer)
8370       (popup-menu gnus-mime-security-button-menu nil prefix))))
8371
8372 (defvar gnus-mime-security-details-buffer nil)
8373
8374 (defvar gnus-mime-security-button-pressed nil)
8375
8376 (defvar gnus-mime-security-show-details-inline t
8377   "If non-nil, show details in the article buffer.")
8378
8379 (defun gnus-mime-security-verify-or-decrypt (handle)
8380   (mm-remove-parts (cdr handle))
8381   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
8382         point (inhibit-read-only t))
8383     (if region
8384         (goto-char (car region)))
8385     (setq point (point))
8386     (with-current-buffer (mm-handle-multipart-original-buffer handle)
8387       (let* ((mm-verify-option 'known)
8388              (mm-decrypt-option 'known)
8389              (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
8390         (unless (eq nparts (cdr handle))
8391           (mm-destroy-parts (cdr handle))
8392           (setcdr handle nparts))))
8393     (gnus-mime-display-security handle)
8394     (when region
8395       (delete-region (point) (cdr region))
8396       (set-marker (car region) nil)
8397       (set-marker (cdr region) nil))
8398     (goto-char point)))
8399
8400 (defun gnus-mime-security-show-details (handle)
8401   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
8402     (if (not details)
8403         (gnus-message 5 "No details.")
8404       (if gnus-mime-security-show-details-inline
8405           (let ((gnus-mime-security-button-pressed
8406                  (not (get-text-property (point) 'gnus-mime-details)))
8407                 (gnus-mime-security-button-line-format
8408                  (get-text-property (point) 'gnus-line-format))
8409                 (inhibit-read-only t))
8410             (forward-char -1)
8411             (while (eq (get-text-property (point) 'gnus-line-format)
8412                        gnus-mime-security-button-line-format)
8413               (forward-char -1))
8414             (forward-char)
8415             (save-restriction
8416               (narrow-to-region (point) (point))
8417               (gnus-insert-mime-security-button handle))
8418             (delete-region (point)
8419                            (or (text-property-not-all
8420                                 (point) (point-max)
8421                                 'gnus-line-format
8422                                 gnus-mime-security-button-line-format)
8423                                (point-max))))
8424         ;; Not inlined.
8425         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
8426             (with-current-buffer gnus-mime-security-details-buffer
8427               (erase-buffer)
8428               t)
8429           (setq gnus-mime-security-details-buffer
8430                 (gnus-get-buffer-create "*MIME Security Details*")))
8431         (with-current-buffer gnus-mime-security-details-buffer
8432           (insert details)
8433           (goto-char (point-min)))
8434         (pop-to-buffer gnus-mime-security-details-buffer)))))
8435
8436 (defun gnus-mime-security-press-button (handle)
8437   (save-excursion
8438     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8439         (gnus-mime-security-show-details handle)
8440       (gnus-mime-security-verify-or-decrypt handle))))
8441
8442 (defun gnus-insert-mime-security-button (handle &optional displayed)
8443   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
8444          (gnus-tmp-type
8445           (concat
8446            (or (nth 2 (assoc protocol mm-verify-function-alist))
8447                (nth 2 (assoc protocol mm-decrypt-function-alist))
8448                "Unknown")
8449            (if (equal (car handle) "multipart/signed")
8450                " Signed" " Encrypted")
8451            " Part"))
8452          (gnus-tmp-info
8453           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
8454               "Undecided"))
8455          (gnus-tmp-details
8456           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
8457          gnus-tmp-pressed-details
8458          b e)
8459     (setq gnus-tmp-details
8460           (if gnus-tmp-details
8461               (concat "\n" gnus-tmp-details)
8462             ""))
8463     (setq gnus-tmp-pressed-details
8464           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
8465     (unless (bolp)
8466       (insert "\n"))
8467     (setq b (point))
8468     (gnus-eval-format
8469      gnus-mime-security-button-line-format
8470      gnus-mime-security-button-line-format-alist
8471      `(keymap ,gnus-mime-security-button-map
8472          gnus-callback gnus-mime-security-press-button
8473          gnus-line-format ,gnus-mime-security-button-line-format
8474          gnus-mime-details ,gnus-mime-security-button-pressed
8475          article-type annotation
8476          gnus-data ,handle))
8477     (setq e (if (bolp)
8478                 ;; Exclude a newline.
8479                 (1- (point))
8480               (point)))
8481     (when gnus-article-button-face
8482       (gnus-overlay-put (gnus-make-overlay b e nil t)
8483                         'face gnus-article-button-face))
8484     (widget-convert-button
8485      'link b e
8486      :mime-handle handle
8487      :action 'gnus-widget-press-button
8488      :button-keymap gnus-mime-security-button-map
8489      :help-echo
8490      (lambda (widget/window &optional overlay pos)
8491        ;; Needed to properly clear the message due to a bug in
8492        ;; wid-edit (XEmacs only).
8493        (when (boundp 'help-echo-owns-message)
8494          (setq help-echo-owns-message t))
8495        (format
8496         "%S: show detail; %S: more options"
8497         (aref gnus-mouse-2 0)
8498         (aref gnus-down-mouse-3 0))))))
8499
8500 (defun gnus-mime-display-security (handle)
8501   (save-restriction
8502     (narrow-to-region (point) (point))
8503     (unless (gnus-unbuttonized-mime-type-p (car handle))
8504       (gnus-insert-mime-security-button handle))
8505     (gnus-mime-display-part (cadr handle))
8506     (unless (bolp)
8507       (insert "\n"))
8508     (unless (gnus-unbuttonized-mime-type-p (car handle))
8509       (let ((gnus-mime-security-button-line-format
8510              gnus-mime-security-button-end-line-format))
8511         (gnus-insert-mime-security-button handle)))
8512     (mm-set-handle-multipart-parameter
8513      handle 'gnus-region
8514      (cons (set-marker (make-marker) (point-min))
8515            (set-marker (make-marker) (point-max))))
8516     (goto-char (point-max))))
8517
8518 (defun gnus-mime-security-run-function (function)
8519   "Run FUNCTION with the security part under point."
8520   (gnus-article-check-buffer)
8521   (let ((data (get-text-property (point) 'gnus-data))
8522         buffer handle)
8523     (when (and (stringp (car-safe data))
8524                (setq buffer (mm-handle-multipart-original-buffer data))
8525                (setq handle (cadr data)))
8526       (if (bufferp (mm-handle-buffer handle))
8527           (progn
8528             (setq handle (cons buffer (copy-sequence (cdr handle))))
8529             (mm-handle-set-undisplayer handle nil))
8530         (setq handle (mm-make-handle
8531                       buffer
8532                       (mm-handle-multipart-ctl-parameter handle 'protocol)
8533                       nil nil nil nil nil nil)))
8534       (funcall function handle))))
8535
8536 (defun gnus-mime-security-save-part ()
8537   "Save the security part under point."
8538   (interactive)
8539   (gnus-mime-security-run-function 'mm-save-part))
8540
8541 (defun gnus-mime-security-pipe-part ()
8542   "Pipe the security part under point to a process."
8543   (interactive)
8544   (gnus-mime-security-run-function 'mm-pipe-part))
8545
8546 (gnus-ems-redefine)
8547
8548 (provide 'gnus-art)
8549
8550 (run-hooks 'gnus-art-load-hook)
8551
8552 ;; arch-tag: 2654516f-6279-48f9-a83b-05c1fa450c33
8553 ;;; gnus-art.el ends here