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