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