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