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