Synch to No Gnus 200601101013.
[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 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."
2638   (interactive "P")
2639   (save-excursion
2640     (let ((inhibit-read-only t)
2641           charset)
2642       (when (gnus-buffer-live-p gnus-original-article-buffer)
2643         (with-current-buffer gnus-original-article-buffer
2644           (let* ((ct (gnus-fetch-field "content-type"))
2645                  (ctl (and ct
2646                            (ignore-errors
2647                              (mail-header-parse-content-type ct)))))
2648             (setq charset (and ctl
2649                                (mail-content-type-get ctl 'charset)))
2650             (when (stringp charset)
2651               (setq charset (intern (downcase charset)))))))
2652       (when read-charset
2653         (setq charset (mm-read-coding-system "Charset: " charset)))
2654       (unless charset
2655         (setq charset gnus-newsgroup-charset))
2656       (article-goto-body)
2657       (save-window-excursion
2658         (save-restriction
2659           (narrow-to-region (point) (point-max))
2660           (let* ((func (or gnus-article-wash-function mm-text-html-renderer))
2661                  (entry (assq func mm-text-html-washer-alist)))
2662             (when entry
2663               (setq func (cdr entry)))
2664             (cond
2665              ((functionp func)
2666               (funcall func))
2667              (t
2668               (apply (car func) (cdr func))))))))))
2669
2670 (defun gnus-article-wash-html-with-w3 ()
2671   "Wash the current buffer with w3."
2672   (mm-setup-w3)
2673   (let ((w3-strict-width (window-width))
2674         (url-standalone-mode t)
2675         (url-gateway-unplugged t)
2676         (w3-honor-stylesheets nil))
2677     (condition-case ()
2678         (w3-region (point-min) (point-max))
2679       (error))))
2680
2681 (defun gnus-article-wash-html-with-w3m ()
2682   "Wash the current buffer with emacs-w3m."
2683   (mm-setup-w3m)
2684   (let ((w3m-safe-url-regexp mm-w3m-safe-url-regexp)
2685         w3m-force-redisplay)
2686     (w3m-region (point-min) (point-max)))
2687   (when (and mm-inline-text-html-with-w3m-keymap
2688              (boundp 'w3m-minor-mode-map)
2689              w3m-minor-mode-map)
2690     (add-text-properties
2691      (point-min) (point-max)
2692      (list 'keymap w3m-minor-mode-map
2693            ;; Put the mark meaning this part was rendered by emacs-w3m.
2694            'mm-inline-text-html-with-w3m t))))
2695
2696 (defun article-hide-list-identifiers ()
2697   "Remove list identifies from the Subject header.
2698 The `gnus-list-identifiers' variable specifies what to do."
2699   (interactive)
2700   (let ((inhibit-point-motion-hooks t)
2701         (regexp (if (consp gnus-list-identifiers)
2702                     (mapconcat 'identity gnus-list-identifiers " *\\|")
2703                   gnus-list-identifiers))
2704         (inhibit-read-only t))
2705     (when regexp
2706       (save-excursion
2707         (save-restriction
2708           (article-narrow-to-head)
2709           (goto-char (point-min))
2710           (while (re-search-forward
2711                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
2712                   nil t)
2713             (delete-region (match-beginning 2) (match-end 0))
2714             (beginning-of-line))
2715           (when (re-search-forward
2716                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
2717             (delete-region (match-beginning 1) (match-end 1))))))))
2718
2719 (defun article-hide-pem (&optional arg)
2720   "Toggle hiding of any PEM headers and signatures in the current article.
2721 If given a negative prefix, always show; if given a positive prefix,
2722 always hide."
2723   (interactive (gnus-article-hidden-arg))
2724   (unless (gnus-article-check-hidden-text 'pem arg)
2725     (save-excursion
2726       (let ((inhibit-read-only t) end)
2727         (goto-char (point-min))
2728         ;; Hide the horrendously ugly "header".
2729         (when (and (search-forward
2730                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
2731                     nil t)
2732                    (setq end (1+ (match-beginning 0))))
2733           (gnus-add-wash-type 'pem)
2734           (gnus-article-hide-text-type
2735            end
2736            (if (search-forward "\n\n" nil t)
2737                (match-end 0)
2738              (point-max))
2739            'pem)
2740           ;; Hide the trailer as well
2741           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
2742                                 nil t)
2743             (gnus-article-hide-text-type
2744              (match-beginning 0) (match-end 0) 'pem)))))))
2745
2746 (defun article-strip-banner ()
2747   "Strip the banners specified by the `banner' group parameter and by
2748 `gnus-article-address-banner-alist'."
2749   (interactive)
2750   (save-excursion
2751     (save-restriction
2752       (let ((inhibit-point-motion-hooks t))
2753         (when (gnus-parameter-banner gnus-newsgroup-name)
2754           (article-really-strip-banner
2755            (gnus-parameter-banner gnus-newsgroup-name)))
2756         (when gnus-article-address-banner-alist
2757           ;; It is necessary to encode from fields before checking,
2758           ;; because `mail-header-parse-addresses' does not work
2759           ;; (reliably) on decoded headers.  And more, it is
2760           ;; impossible to use `gnus-fetch-original-field' here,
2761           ;; because `article-strip-banner' may be called in draft
2762           ;; buffers to preview them.
2763           (let ((from (save-restriction
2764                         (widen)
2765                         (article-narrow-to-head)
2766                         (mail-fetch-field "from"))))
2767             (when (and from
2768                        (setq from
2769                              (caar (mail-header-parse-addresses
2770                                     (mail-encode-encoded-word-string from)))))
2771               (catch 'found
2772                 (dolist (pair gnus-article-address-banner-alist)
2773                   (when (string-match (car pair) from)
2774                     (throw 'found
2775                            (article-really-strip-banner (cdr pair)))))))))))))
2776
2777 (defun article-really-strip-banner (banner)
2778   "Strip the banner specified by the argument."
2779   (save-excursion
2780     (save-restriction
2781       (let ((inhibit-point-motion-hooks t)
2782             (gnus-signature-limit nil)
2783             (inhibit-read-only t))
2784         (article-goto-body)
2785         (cond
2786          ((eq banner 'signature)
2787           (when (gnus-article-narrow-to-signature)
2788             (widen)
2789             (forward-line -1)
2790             (delete-region (point) (point-max))))
2791          ((symbolp banner)
2792           (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
2793               (while (re-search-forward banner nil t)
2794                 (delete-region (match-beginning 0) (match-end 0)))))
2795          ((stringp banner)
2796           (while (re-search-forward banner nil t)
2797             (delete-region (match-beginning 0) (match-end 0)))))))))
2798
2799 (defun article-babel ()
2800   "Translate article using an online translation service."
2801   (interactive)
2802   (require 'babel)
2803   (gnus-with-article-buffer
2804     (when (article-goto-body)
2805       (let* ((start (point))
2806              (end (point-max))
2807              (orig (buffer-substring start end))
2808              (trans (babel-as-string orig)))
2809         (save-restriction
2810           (narrow-to-region start end)
2811           (delete-region start end)
2812           (insert trans))))))
2813
2814 (defun article-hide-signature (&optional arg)
2815   "Hide the signature in the current article.
2816 If given a negative prefix, always show; if given a positive prefix,
2817 always hide."
2818   (interactive (gnus-article-hidden-arg))
2819   (save-excursion
2820     (save-restriction
2821       (if (interactive-p)
2822           (progn
2823             (widen)
2824             (article-goto-body))
2825         (goto-char (point-min)))
2826       (unless (gnus-article-check-hidden-text 'signature arg)
2827         (let ((inhibit-read-only t)
2828               (button (point)))
2829           (while (setq button (text-property-any button (point-max)
2830                                                  'gnus-callback
2831                                                  'gnus-signature-toggle))
2832             (setq button (text-property-not-all button (point-max)
2833                                                 'gnus-callback
2834                                                 'gnus-signature-toggle))
2835             (when (and button (not (eobp)))
2836               (gnus-article-hide-text-type
2837                (1+ button)
2838                (next-single-property-change (1+ button) 'mime-view-entity
2839                                             nil (point-max))
2840                'signature)))))))
2841   (gnus-set-mode-line 'article))
2842
2843 (defun article-strip-headers-in-body ()
2844   "Strip offensive headers from bodies."
2845   (interactive)
2846   (save-excursion
2847     (article-goto-body)
2848     (let ((case-fold-search t))
2849       (when (looking-at "x-no-archive:")
2850         (gnus-delete-line)))))
2851
2852 (defun article-strip-leading-blank-lines ()
2853   "Remove all blank lines from the beginning of the article."
2854   (interactive)
2855   (save-excursion
2856     (let ((inhibit-point-motion-hooks t)
2857           (inhibit-read-only t))
2858       (when (article-goto-body)
2859         (while (and (not (eobp))
2860                     (looking-at "[ \t]*$"))
2861           (gnus-delete-line))))))
2862
2863 (defun article-narrow-to-head ()
2864   "Narrow the buffer to the head of the message.
2865 Point is left at the beginning of the narrowed-to region."
2866   (narrow-to-region
2867    (goto-char (point-min))
2868    (if (search-forward "\n\n" nil 1)
2869        (1- (point))
2870      (point-max)))
2871   (goto-char (point-min)))
2872
2873 (defun article-goto-body ()
2874   "Place point at the start of the body."
2875   (goto-char (point-min))
2876   (cond
2877    ;; This variable is only bound when dealing with separate
2878    ;; MIME body parts.
2879    (article-goto-body-goes-to-point-min-p
2880     t)
2881    ((search-forward "\n\n" nil t)
2882     t)
2883    (t
2884     (goto-char (point-max))
2885     nil)))
2886
2887 (defun article-strip-multiple-blank-lines ()
2888   "Replace consecutive blank lines with one empty line."
2889   (interactive)
2890   (save-excursion
2891     (let ((inhibit-point-motion-hooks t)
2892           (inhibit-read-only t))
2893       ;; First make all blank lines empty.
2894       (article-goto-body)
2895       (while (re-search-forward "^[ \t]+$" nil t)
2896         (unless (gnus-annotation-in-region-p
2897                  (match-beginning 0) (match-end 0))
2898           (replace-match "" nil t)))
2899       ;; Then replace multiple empty lines with a single empty line.
2900       (article-goto-body)
2901       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
2902         (unless (gnus-annotation-in-region-p
2903                  (match-beginning 0) (match-end 0))
2904           (delete-region (match-beginning 1) (match-end 1)))))))
2905
2906 (defun article-strip-leading-space ()
2907   "Remove all white space from the beginning of the lines in the article."
2908   (interactive)
2909   (save-excursion
2910     (let ((inhibit-point-motion-hooks t)
2911           (inhibit-read-only t))
2912       (article-goto-body)
2913       (while (re-search-forward "^[ \t]+" nil t)
2914         (replace-match "" t t)))))
2915
2916 (defun article-strip-trailing-space ()
2917   "Remove all white space from the end of the lines in the article."
2918   (interactive)
2919   (save-excursion
2920     (let ((inhibit-point-motion-hooks t)
2921           (inhibit-read-only t))
2922       (article-goto-body)
2923       (while (re-search-forward "[ \t]+$" nil t)
2924         (replace-match "" t t)))))
2925
2926 (defun article-strip-blank-lines ()
2927   "Strip leading, trailing and multiple blank lines."
2928   (interactive)
2929   (article-strip-leading-blank-lines)
2930   (article-remove-trailing-blank-lines)
2931   (article-strip-multiple-blank-lines))
2932
2933 (defun article-strip-all-blank-lines ()
2934   "Strip all blank lines."
2935   (interactive)
2936   (save-excursion
2937     (let ((inhibit-point-motion-hooks t)
2938           (inhibit-read-only t))
2939       (article-goto-body)
2940       (while (re-search-forward "^[ \t]*\n" nil t)
2941         (replace-match "" t t)))))
2942
2943 (defun gnus-article-narrow-to-signature ()
2944   "Narrow to the signature; return t if a signature is found, else nil."
2945   (let ((inhibit-point-motion-hooks t))
2946     (when (gnus-article-search-signature)
2947       (forward-line 1)
2948       ;; Check whether we have some limits to what we consider
2949       ;; to be a signature.
2950       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2951                       (list gnus-signature-limit)))
2952             limit limited)
2953         (while (setq limit (pop limits))
2954           (if (or (and (integerp limit)
2955                        (< (- (point-max) (point)) limit))
2956                   (and (floatp limit)
2957                        (< (count-lines (point) (point-max)) limit))
2958                   (and (functionp limit)
2959                        (funcall limit))
2960                   (and (stringp limit)
2961                        (not (re-search-forward limit nil t))))
2962               ()                        ; This limit did not succeed.
2963             (setq limited t
2964                   limits nil)))
2965         (unless limited
2966           (narrow-to-region (point) (point-max))
2967           t)))))
2968
2969 (defun gnus-article-search-signature ()
2970   "Search the current buffer for the signature separator.
2971 Put point at the beginning of the signature separator."
2972   (let ((cur (point)))
2973     (goto-char (point-max))
2974     (if (if (stringp gnus-signature-separator)
2975             (re-search-backward gnus-signature-separator nil t)
2976           (let ((seps gnus-signature-separator))
2977             (while (and seps
2978                         (not (re-search-backward (car seps) nil t)))
2979               (pop seps))
2980             seps))
2981         t
2982       (goto-char cur)
2983       nil)))
2984
2985 (defun gnus-article-hidden-arg ()
2986   "Return the current prefix arg as a number, or 0 if no prefix."
2987   (list (if current-prefix-arg
2988             (prefix-numeric-value current-prefix-arg)
2989           0)))
2990
2991 (defun gnus-article-check-hidden-text (type arg)
2992   "Return nil if hiding is necessary.
2993 Arg can be nil or a number.  nil and positive means hide, negative
2994 means show, 0 means toggle."
2995   (save-excursion
2996     (save-restriction
2997       (let ((hide (gnus-article-hidden-text-p type)))
2998         (cond
2999          ((or (null arg)
3000               (> arg 0))
3001           nil)
3002          ((< arg 0)
3003           (gnus-article-show-hidden-text type)
3004           t)
3005          (t
3006           (if (eq hide 'hidden)
3007               (progn
3008                 (gnus-article-show-hidden-text type)
3009                 t)
3010             nil)))))))
3011
3012 (defun gnus-article-hidden-text-p (type)
3013   "Say whether the current buffer contains hidden text of type TYPE."
3014   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
3015     (while (and pos
3016                 (not (get-text-property pos 'invisible))
3017                 (not (get-text-property pos 'dummy-invisible)))
3018       (setq pos
3019             (text-property-any (1+ pos) (point-max) 'article-type type)))
3020     (if pos
3021         'hidden
3022       nil)))
3023
3024 (defun gnus-article-show-hidden-text (type &optional dummy)
3025   "Show all hidden text of type TYPE.
3026 Originally it is hide instead of DUMMY."
3027   (let ((inhibit-read-only t)
3028         (inhibit-point-motion-hooks t))
3029     (gnus-remove-text-properties-when
3030      'article-type type
3031      (point-min) (point-max)
3032      (cons 'article-type (cons type
3033                                gnus-hidden-properties)))
3034     (gnus-delete-wash-type type)))
3035
3036 (defconst article-time-units
3037   `((year . ,(* 365.25 24 60 60))
3038     (week . ,(* 7 24 60 60))
3039     (day . ,(* 24 60 60))
3040     (hour . ,(* 60 60))
3041     (minute . 60)
3042     (second . 1))
3043   "Mapping from time units to seconds.")
3044
3045 (defun gnus-article-forward-header ()
3046   "Move point to the start of the next header.
3047 If the current header is a continuation header, this can be several
3048 lines forward."
3049   (let ((ended nil))
3050     (while (not ended)
3051       (forward-line 1)
3052       (if (looking-at "[ \t]+[^ \t]")
3053           (forward-line 1)
3054         (setq ended t)))))
3055
3056 (defun article-date-ut (&optional type highlight)
3057   "Convert DATE date to universal time in the current article.
3058 If TYPE is `local', convert to local time; if it is `lapsed', output
3059 how much time has lapsed since DATE.  For `lapsed', the value of
3060 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
3061 should replace the \"Date:\" one, or should be added below it."
3062   (interactive (list 'ut t))
3063   (let* ((tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
3064          (date-regexp (cond ((not gnus-article-date-lapsed-new-header)
3065                              tdate-regexp)
3066                             ((eq type 'lapsed)
3067                              "^X-Sent:[ \t]")
3068                             (article-lapsed-timer
3069                              "^Date:[ \t]")
3070                             (t
3071                              tdate-regexp)))
3072          (case-fold-search t)
3073          (inhibit-read-only t)
3074          (inhibit-point-motion-hooks t)
3075          pos date bface eface)
3076     (save-excursion
3077       (save-restriction
3078         (widen)
3079         (goto-char (point-min))
3080         (while (or (setq date (get-text-property (setq pos (point))
3081                                                  'original-date))
3082                    (when (setq pos (next-single-property-change
3083                                     (point) 'original-date))
3084                      (setq date (get-text-property pos 'original-date))
3085                      t))
3086           (narrow-to-region pos (or (text-property-any pos (point-max)
3087                                                        'original-date nil)
3088                                     (point-max)))
3089           (goto-char (point-min))
3090           (when (re-search-forward tdate-regexp nil t)
3091             (setq bface (get-text-property (point-at-bol) 'face)
3092                   eface (get-text-property (1- (point-at-eol)) 'face)))
3093           (goto-char (point-min))
3094           (setq pos nil)
3095           ;; Delete any old Date headers.
3096           (while (re-search-forward date-regexp nil t)
3097             (if pos
3098                 (delete-region (point-at-bol) (progn
3099                                                 (gnus-article-forward-header)
3100                                                 (point)))
3101               (delete-region (point-at-bol) (progn
3102                                               (gnus-article-forward-header)
3103                                               (forward-char -1)
3104                                               (point)))
3105               (setq pos (point))))
3106           (when (and (not pos)
3107                      (re-search-forward tdate-regexp nil t))
3108             (forward-line 1))
3109           (gnus-goto-char pos)
3110           (insert (article-make-date-line date (or type 'ut)))
3111           (unless pos
3112             (insert "\n")
3113             (forward-line -1))
3114           ;; Do highlighting.
3115           (beginning-of-line)
3116           (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
3117             (put-text-property (match-beginning 1) (1+ (match-end 1))
3118                                'face bface)
3119             (put-text-property (match-beginning 2) (match-end 2)
3120                                'face eface))
3121           (put-text-property (point-min) (1- (point-max)) 'original-date date)
3122           (goto-char (point-max))
3123           (widen))))))
3124
3125 (defun article-make-date-line (date type)
3126   "Return a DATE line of TYPE."
3127   (unless (memq type '(local ut original user iso8601 lapsed english))
3128     (error "Unknown conversion type: %s" type))
3129   (condition-case ()
3130       (let ((time (date-to-time date)))
3131         (cond
3132          ;; Convert to the local timezone.
3133          ((eq type 'local)
3134           (concat "Date: " (message-make-date time)))
3135          ;; Convert to Universal Time.
3136          ((eq type 'ut)
3137           (concat "Date: "
3138                   (substring
3139                    (message-make-date
3140                     (let* ((e (parse-time-string date))
3141                            (tm (apply 'encode-time e))
3142                            (ms (car tm))
3143                            (ls (- (cadr tm) (car (current-time-zone time)))))
3144                       (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
3145                             ((> ls 65535) (list (1+ ms) (- ls 65536)))
3146                             (t (list ms ls)))))
3147                    0 -5)
3148                   "UT"))
3149          ;; Get the original date from the article.
3150          ((eq type 'original)
3151           (concat "Date: " (if (string-match "\n+$" date)
3152                                (substring date 0 (match-beginning 0))
3153                              date)))
3154          ;; Let the user define the format.
3155          ((eq type 'user)
3156           (let ((format (or (condition-case nil
3157                                 (with-current-buffer gnus-summary-buffer
3158                                   gnus-article-time-format)
3159                               (error nil))
3160                             gnus-article-time-format)))
3161             (if (functionp format)
3162                 (funcall format time)
3163               (concat "Date: " (format-time-string format time)))))
3164          ;; ISO 8601.
3165          ((eq type 'iso8601)
3166           (let ((tz (car (current-time-zone time))))
3167             (concat
3168              "Date: "
3169              (format-time-string "%Y%m%dT%H%M%S" time)
3170              (format "%s%02d%02d"
3171                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
3172                      (/ (% (abs tz) 3600) 60)))))
3173          ;; Do an X-Sent lapsed format.
3174          ((eq type 'lapsed)
3175           ;; If the date is seriously mangled, the timezone functions are
3176           ;; liable to bug out, so we ignore all errors.
3177           (let* ((now (current-time))
3178                  (real-time (subtract-time now time))
3179                  (real-sec (and real-time
3180                                 (+ (* (float (car real-time)) 65536)
3181                                    (cadr real-time))))
3182                  (sec (and real-time (abs real-sec)))
3183                  num prev)
3184             (cond
3185              ((null real-time)
3186               "X-Sent: Unknown")
3187              ((zerop sec)
3188               "X-Sent: Now")
3189              (t
3190               (concat
3191                "X-Sent: "
3192                ;; This is a bit convoluted, but basically we go
3193                ;; through the time units for years, weeks, etc,
3194                ;; and divide things to see whether that results
3195                ;; in positive answers.
3196                (mapconcat
3197                 (lambda (unit)
3198                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
3199                       ;; The (remaining) seconds are too few to
3200                       ;; be divided into this time unit.
3201                       ""
3202                     ;; It's big enough, so we output it.
3203                     (setq sec (- sec (* num (cdr unit))))
3204                     (prog1
3205                         (concat (if prev ", " "") (int-to-string
3206                                                    (floor num))
3207                                 " " (symbol-name (car unit))
3208                                 (if (> num 1) "s" ""))
3209                       (setq prev t))))
3210                 article-time-units "")
3211                ;; If dates are odd, then it might appear like the
3212                ;; article was sent in the future.
3213                (if (> real-sec 0)
3214                    " ago"
3215                  " in the future"))))))
3216          ;; Display the date in proper English
3217          ((eq type 'english)
3218           (let ((dtime (decode-time time)))
3219             (concat
3220              "Date: the "
3221              (number-to-string (nth 3 dtime))
3222              (let ((digit (% (nth 3 dtime) 10)))
3223                (cond
3224                 ((memq (nth 3 dtime) '(11 12 13)) "th")
3225                 ((= digit 1) "st")
3226                 ((= digit 2) "nd")
3227                 ((= digit 3) "rd")
3228                 (t "th")))
3229              " of "
3230              (nth (1- (nth 4 dtime)) gnus-english-month-names)
3231              " "
3232              (number-to-string (nth 5 dtime))
3233              " at "
3234              (format "%02d" (nth 2 dtime))
3235              ":"
3236              (format "%02d" (nth 1 dtime)))))))
3237     (error
3238      (format "Date: %s (from T-gnus)" date))))
3239
3240 (defun article-date-local (&optional highlight)
3241   "Convert the current article date to the local timezone."
3242   (interactive (list t))
3243   (article-date-ut 'local highlight))
3244
3245 (defun article-date-english (&optional highlight)
3246   "Convert the current article date to something that is proper English."
3247   (interactive (list t))
3248   (article-date-ut 'english highlight))
3249
3250 (defun article-date-original (&optional highlight)
3251   "Convert the current article date to what it was originally.
3252 This is only useful if you have used some other date conversion
3253 function and want to see what the date was before converting."
3254   (interactive (list t))
3255   (article-date-ut 'original highlight))
3256
3257 (defun article-date-lapsed (&optional highlight)
3258   "Convert the current article date to time lapsed since it was sent."
3259   (interactive (list t))
3260   (article-date-ut 'lapsed highlight))
3261
3262 (defun article-update-date-lapsed ()
3263   "Function to be run from a timer to update the lapsed time line."
3264   (save-match-data
3265     (let (deactivate-mark)
3266       (save-excursion
3267         (ignore-errors
3268          (walk-windows
3269           (lambda (w)
3270             (set-buffer (window-buffer w))
3271             (when (or (and (eq major-mode 'mime-view-mode)
3272                            (eq (mime-preview-original-major-mode)
3273                                'gnus-original-article-mode))
3274                       (eq major-mode 'gnus-article-mode))
3275               (let ((mark (point-marker)))
3276                 (goto-char (point-min))
3277                 (when (re-search-forward "^X-Sent:" nil t)
3278                   (article-date-lapsed t))
3279                 (goto-char (marker-position mark))
3280                 (move-marker mark nil))))
3281           nil 'visible))))))
3282
3283 (defun gnus-start-date-timer (&optional n)
3284   "Start a timer to update the X-Sent header in the article buffers.
3285 The numerical prefix says how frequently (in seconds) the function
3286 is to run."
3287   (interactive "p")
3288   (unless n
3289     (setq n 1))
3290   (gnus-stop-date-timer)
3291   (setq article-lapsed-timer
3292         (run-at-time 1 n 'article-update-date-lapsed)))
3293
3294 (defun gnus-stop-date-timer ()
3295   "Stop the X-Sent timer."
3296   (interactive)
3297   (when article-lapsed-timer
3298     (nnheader-cancel-timer article-lapsed-timer)
3299     (setq article-lapsed-timer nil)))
3300
3301 (defun article-date-user (&optional highlight)
3302   "Convert the current article date to the user-defined format.
3303 This format is defined by the `gnus-article-time-format' variable."
3304   (interactive (list t))
3305   (article-date-ut 'user highlight))
3306
3307 (defun article-date-iso8601 (&optional highlight)
3308   "Convert the current article date to ISO8601."
3309   (interactive (list t))
3310   (article-date-ut 'iso8601 highlight))
3311
3312 (defmacro gnus-article-save-original-date (&rest forms)
3313   "Save the original date as a text property and evaluate FORMS."
3314   `(let* ((case-fold-search t)
3315           (start (progn
3316                    (goto-char (point-min))
3317                    (when (and (re-search-forward "^date:[\t\n ]+" nil t)
3318                               (not (bolp)))
3319                      (match-end 0))))
3320           (date (when (and start
3321                            (re-search-forward "[\t ]*\n\\(?:[^\t ]\\|\\'\\)"
3322                                               nil t))
3323                   (buffer-substring-no-properties start
3324                                                   (match-beginning 0)))))
3325      (goto-char (point-max))
3326      (skip-chars-backward "\n")
3327      (put-text-property (point-min) (point) 'original-date date)
3328      ,@forms
3329      (goto-char (point-max))
3330      (skip-chars-backward "\n")
3331      (put-text-property (point-min) (point) 'original-date date)))
3332
3333 ;; (defun article-show-all ()
3334 ;;   "Show all hidden text in the article buffer."
3335 ;;   (interactive)
3336 ;;   (save-excursion
3337 ;;     (widen)
3338 ;;     (let ((inhibit-read-only t))
3339 ;;       (gnus-article-unhide-text (point-min) (point-max))
3340 ;;       (gnus-remove-text-with-property 'gnus-prev)
3341 ;;       (gnus-remove-text-with-property 'gnus-next))))
3342
3343 (defun article-show-all-headers ()
3344   "Show all hidden headers in the article buffer."
3345   (interactive)
3346   (save-excursion
3347     (save-restriction
3348       (widen)
3349       (article-narrow-to-head)
3350       (let ((inhibit-read-only t))
3351         (gnus-article-unhide-text (point-min) (point-max))))))
3352
3353 (defun article-remove-leading-whitespace ()
3354   "Remove excessive whitespace from all headers."
3355   (interactive)
3356   (save-excursion
3357     (save-restriction
3358       (let ((inhibit-read-only t))
3359         (article-narrow-to-head)
3360         (goto-char (point-min))
3361         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
3362           (delete-region (match-beginning 1) (match-end 1)))))))
3363
3364 (defun article-emphasize (&optional arg)
3365   "Emphasize text according to `gnus-emphasis-alist'."
3366   (interactive (gnus-article-hidden-arg))
3367   (unless (gnus-article-check-hidden-text 'emphasis arg)
3368     (save-excursion
3369       (let ((alist (or
3370                     (condition-case nil
3371                         (with-current-buffer gnus-summary-buffer
3372                           gnus-article-emphasis-alist)
3373                       (error))
3374                     gnus-emphasis-alist))
3375             (inhibit-read-only t)
3376             (props (append '(article-type emphasis)
3377                            gnus-hidden-properties))
3378             regexp elem beg invisible visible face)
3379         (article-goto-body)
3380         (setq beg (point))
3381         (while (setq elem (pop alist))
3382           (goto-char beg)
3383           (setq regexp (car elem)
3384                 invisible (nth 1 elem)
3385                 visible (nth 2 elem)
3386                 face (nth 3 elem))
3387           (while (re-search-forward regexp nil t)
3388             (when (and (match-beginning visible) (match-beginning invisible))
3389               (gnus-article-hide-text
3390                (match-beginning invisible) (match-end invisible) props)
3391               (gnus-article-unhide-text-type
3392                (match-beginning visible) (match-end visible) 'emphasis)
3393               (gnus-put-overlay-excluding-newlines
3394                (match-beginning visible) (match-end visible) 'face face)
3395               (gnus-add-wash-type 'emphasis)
3396               (goto-char (match-end invisible)))))))))
3397
3398 (defun gnus-article-setup-highlight-words (&optional highlight-words)
3399   "Setup newsgroup emphasis alist."
3400   (unless gnus-article-emphasis-alist
3401     (let ((name (and gnus-newsgroup-name
3402                      (gnus-group-real-name gnus-newsgroup-name))))
3403       (make-local-variable 'gnus-article-emphasis-alist)
3404       (setq gnus-article-emphasis-alist
3405             (nconc
3406              (let ((alist gnus-group-highlight-words-alist) elem highlight)
3407                (while (setq elem (pop alist))
3408                  (when (and name (string-match (car elem) name))
3409                    (setq alist nil
3410                          highlight (copy-sequence (cdr elem)))))
3411                highlight)
3412              (copy-sequence highlight-words)
3413              (if gnus-newsgroup-name
3414                  (copy-sequence (gnus-group-find-parameter
3415                                  gnus-newsgroup-name 'highlight-words t)))
3416              gnus-emphasis-alist)))))
3417
3418 (eval-when-compile
3419   (defvar gnus-summary-article-menu)
3420   (defvar gnus-summary-post-menu))
3421
3422 ;;; Saving functions.
3423
3424 (defun gnus-article-save (save-buffer file &optional num)
3425   "Save the currently selected article."
3426   (unless gnus-save-all-headers
3427     ;; Remove headers according to `gnus-saved-headers'.
3428     (let ((gnus-visible-headers
3429            (or gnus-saved-headers gnus-visible-headers))
3430           (gnus-article-buffer save-buffer))
3431       (save-excursion
3432         (set-buffer save-buffer)
3433         (article-hide-headers 1 t))))
3434   (save-window-excursion
3435     (if (not gnus-default-article-saver)
3436         (error "No default saver is defined")
3437       ;; !!! Magic!  The saving functions all save
3438       ;; `gnus-save-article-buffer' (or so they think), but we
3439       ;; bind that variable to our save-buffer.
3440       (set-buffer gnus-article-buffer)
3441       (let* ((gnus-save-article-buffer save-buffer)
3442              (filename
3443               (cond
3444                ((not gnus-prompt-before-saving) 'default)
3445                ((eq gnus-prompt-before-saving 'always) nil)
3446                (t file)))
3447              (gnus-number-of-articles-to-be-saved
3448               (when (eq gnus-prompt-before-saving t)
3449                 num)))                  ; Magic
3450         (set-buffer gnus-article-current-summary)
3451         (funcall gnus-default-article-saver filename)))))
3452
3453 (defun gnus-read-save-file-name (prompt &optional filename
3454                                         function group headers variable)
3455   (let ((default-name
3456           (funcall function group headers (symbol-value variable)))
3457         result)
3458     (setq result
3459           (expand-file-name
3460            (cond
3461             ((eq filename 'default)
3462              default-name)
3463             ((eq filename t)
3464              default-name)
3465             (filename filename)
3466             (t
3467              (let* ((split-name (gnus-get-split-value gnus-split-methods))
3468                     (prompt
3469                      (format prompt
3470                              (if (and gnus-number-of-articles-to-be-saved
3471                                       (> gnus-number-of-articles-to-be-saved 1))
3472                                  (format "these %d articles"
3473                                          gnus-number-of-articles-to-be-saved)
3474                                "this article")))
3475                     (file
3476                      ;; Let the split methods have their say.
3477                      (cond
3478                       ;; No split name was found.
3479                       ((null split-name)
3480                        (read-file-name
3481                         (concat prompt " (default "
3482                                 (file-name-nondirectory default-name) "): ")
3483                         (file-name-directory default-name)
3484                         default-name))
3485                       ;; A single group name is returned.
3486                       ((stringp split-name)
3487                        (setq default-name
3488                              (funcall function split-name headers
3489                                       (symbol-value variable)))
3490                        (read-file-name
3491                         (concat prompt " (default "
3492                                 (file-name-nondirectory default-name) "): ")
3493                         (file-name-directory default-name)
3494                         default-name))
3495                       ;; A single split name was found
3496                       ((= 1 (length split-name))
3497                        (let* ((name (expand-file-name
3498                                      (car split-name)
3499                                      gnus-article-save-directory))
3500                               (dir (cond ((file-directory-p name)
3501                                           (file-name-as-directory name))
3502                                          ((file-exists-p name) name)
3503                                          (t gnus-article-save-directory))))
3504                          (read-file-name
3505                           (concat prompt " (default " name "): ")
3506                           dir name)))
3507                       ;; A list of splits was found.
3508                       (t
3509                        (setq split-name (nreverse split-name))
3510                        (let (result)
3511                          (let ((file-name-history
3512                                 (nconc split-name file-name-history)))
3513                            (setq result
3514                                  (expand-file-name
3515                                   (read-file-name
3516                                    (concat prompt " (`M-p' for defaults): ")
3517                                    gnus-article-save-directory
3518                                    (car split-name))
3519                                   gnus-article-save-directory)))
3520                          (car (push result file-name-history)))))))
3521                ;; Create the directory.
3522                (gnus-make-directory (file-name-directory file))
3523                ;; If we have read a directory, we append the default file name.
3524                (when (file-directory-p file)
3525                  (setq file (expand-file-name (file-name-nondirectory
3526                                                default-name)
3527                                               (file-name-as-directory file))))
3528                ;; Possibly translate some characters.
3529                (nnheader-translate-file-chars file))))))
3530     (gnus-make-directory (file-name-directory result))
3531     (set variable result)))
3532
3533 (defun gnus-article-archive-name (group)
3534   "Return the first instance of an \"Archive-name\" in the current buffer."
3535   (let ((case-fold-search t))
3536     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
3537       (nnheader-concat gnus-article-save-directory
3538                        (match-string 1)))))
3539
3540 (defun gnus-article-nndoc-name (group)
3541   "If GROUP is an nndoc group, return the name of the parent group."
3542   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
3543     (gnus-group-get-parameter group 'save-article-group)))
3544
3545 (defun gnus-summary-save-in-rmail (&optional filename)
3546   "Append this article to Rmail file.
3547 Optional argument FILENAME specifies file name.
3548 Directory to save to is default to `gnus-article-save-directory'."
3549   (setq filename (gnus-read-save-file-name
3550                   "Save %s in rmail file" filename
3551                   gnus-rmail-save-name gnus-newsgroup-name
3552                   gnus-current-headers 'gnus-newsgroup-last-rmail))
3553   (gnus-eval-in-buffer-window gnus-save-article-buffer
3554     (save-excursion
3555       (save-restriction
3556         (widen)
3557         (gnus-output-to-rmail filename))))
3558   filename)
3559
3560 (defun gnus-summary-save-in-mail (&optional filename)
3561   "Append this article to Unix mail file.
3562 Optional argument FILENAME specifies file name.
3563 Directory to save to is default to `gnus-article-save-directory'."
3564   (setq filename (gnus-read-save-file-name
3565                   "Save %s in Unix mail file" filename
3566                   gnus-mail-save-name gnus-newsgroup-name
3567                   gnus-current-headers 'gnus-newsgroup-last-mail))
3568   (gnus-eval-in-buffer-window gnus-save-article-buffer
3569     (save-excursion
3570       (save-restriction
3571         (widen)
3572         (if (and (file-readable-p filename)
3573                  (file-regular-p filename)
3574                  (mail-file-babyl-p filename))
3575             (rmail-output-to-rmail-file filename t)
3576           (gnus-output-to-mail filename)))))
3577   filename)
3578
3579 (defun gnus-summary-save-in-file (&optional filename overwrite)
3580   "Append this article to file.
3581 Optional argument FILENAME specifies file name.
3582 Directory to save to is default to `gnus-article-save-directory'."
3583   (setq filename (gnus-read-save-file-name
3584                   "Save %s in file" filename
3585                   gnus-file-save-name gnus-newsgroup-name
3586                   gnus-current-headers 'gnus-newsgroup-last-file))
3587   (gnus-eval-in-buffer-window gnus-save-article-buffer
3588     (save-excursion
3589       (save-restriction
3590         (widen)
3591         (when (and overwrite
3592                    (file-exists-p filename))
3593           (delete-file filename))
3594         (gnus-output-to-file filename))))
3595   filename)
3596
3597 (defun gnus-summary-write-to-file (&optional filename)
3598   "Write this article to a file, overwriting it if the file exists.
3599 Optional argument FILENAME specifies file name.
3600 The directory to save in defaults to `gnus-article-save-directory'."
3601   (gnus-summary-save-in-file nil t))
3602
3603 (defun gnus-summary-save-body-in-file (&optional filename)
3604   "Append this article body to a file.
3605 Optional argument FILENAME specifies file name.
3606 The directory to save in defaults to `gnus-article-save-directory'."
3607   (setq filename (gnus-read-save-file-name
3608                   "Save %s body in file" filename
3609                   gnus-file-save-name gnus-newsgroup-name
3610                   gnus-current-headers 'gnus-newsgroup-last-file))
3611   (gnus-eval-in-buffer-window gnus-save-article-buffer
3612     (save-excursion
3613       (save-restriction
3614         (widen)
3615         (when (article-goto-body)
3616           (narrow-to-region (point) (point-max)))
3617         (gnus-output-to-file filename))))
3618   filename)
3619
3620 (defun gnus-summary-save-in-pipe (&optional command)
3621   "Pipe this article to subprocess."
3622   (setq command
3623         (cond ((and (eq command 'default)
3624                     gnus-last-shell-command)
3625                gnus-last-shell-command)
3626               ((stringp command)
3627                command)
3628               (t (read-string
3629                   (format
3630                    "Shell command on %s: "
3631                    (if (and gnus-number-of-articles-to-be-saved
3632                             (> gnus-number-of-articles-to-be-saved 1))
3633                        (format "these %d articles"
3634                                gnus-number-of-articles-to-be-saved)
3635                      "this article"))
3636                   gnus-last-shell-command))))
3637   (when (string-equal command "")
3638     (if gnus-last-shell-command
3639         (setq command gnus-last-shell-command)
3640       (error "A command is required")))
3641   (gnus-eval-in-buffer-window gnus-article-buffer
3642     (save-restriction
3643       (widen)
3644       (shell-command-on-region (point-min) (point-max) command nil)))
3645   (setq gnus-last-shell-command command))
3646
3647 (defun gnus-summary-pipe-to-muttprint (&optional command)
3648   "Pipe this article to muttprint."
3649   (setq command (read-string
3650                  "Print using command: " gnus-summary-muttprint-program
3651                  nil gnus-summary-muttprint-program))
3652   (gnus-summary-save-in-pipe command))
3653
3654 ;;; Article file names when saving.
3655
3656 (defun gnus-capitalize-newsgroup (newsgroup)
3657   "Capitalize NEWSGROUP name."
3658   (when (not (zerop (length newsgroup)))
3659     (concat (char-to-string (upcase (aref newsgroup 0)))
3660             (substring newsgroup 1))))
3661
3662 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
3663   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3664 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
3665 Otherwise, it is like ~/News/news/group/num."
3666   (let ((default
3667           (expand-file-name
3668            (concat (if (gnus-use-long-file-name 'not-save)
3669                        (gnus-capitalize-newsgroup newsgroup)
3670                      (gnus-newsgroup-directory-form newsgroup))
3671                    "/" (int-to-string (mail-header-number headers)))
3672            gnus-article-save-directory)))
3673     (if (and last-file
3674              (string-equal (file-name-directory default)
3675                            (file-name-directory last-file))
3676              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3677         default
3678       (or last-file default))))
3679
3680 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
3681   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3682 If variable `gnus-use-long-file-name' is non-nil, it is
3683 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
3684   (let ((default
3685           (expand-file-name
3686            (concat (if (gnus-use-long-file-name 'not-save)
3687                        newsgroup
3688                      (gnus-newsgroup-directory-form newsgroup))
3689                    "/" (int-to-string (mail-header-number headers)))
3690            gnus-article-save-directory)))
3691     (if (and last-file
3692              (string-equal (file-name-directory default)
3693                            (file-name-directory last-file))
3694              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
3695         default
3696       (or last-file default))))
3697
3698 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
3699   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
3700 If variable `gnus-use-long-file-name' is non-nil, it is
3701 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
3702   (or last-file
3703       (expand-file-name
3704        (if (gnus-use-long-file-name 'not-save)
3705            newsgroup
3706          (file-relative-name
3707           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
3708           default-directory))
3709        gnus-article-save-directory)))
3710
3711 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
3712   "Generate file name from sender."
3713   (let ((from (mail-header-from headers)))
3714     (expand-file-name
3715      (if (and from (string-match "\\([^ <]+\\)@" from))
3716          (match-string 1 from)
3717        "nobody")
3718      gnus-article-save-directory)))
3719
3720 (defun article-verify-x-pgp-sig ()
3721   "Verify X-PGP-Sig."
3722   (interactive)
3723   (if (gnus-buffer-live-p gnus-original-article-buffer)
3724       (let ((sig (with-current-buffer gnus-original-article-buffer
3725                    (gnus-fetch-field "X-PGP-Sig")))
3726             items info headers)
3727         (when (and sig
3728                    mml2015-use
3729                    (mml2015-clear-verify-function))
3730           (with-temp-buffer
3731             (insert-buffer-substring gnus-original-article-buffer)
3732             (setq items (split-string sig))
3733             (message-narrow-to-head)
3734             (let ((inhibit-point-motion-hooks t)
3735                   (case-fold-search t))
3736               ;; Don't verify multiple headers.
3737               (setq headers (mapconcat (lambda (header)
3738                                          (concat header ": "
3739                                                  (mail-fetch-field header)
3740                                                  "\n"))
3741                                        (split-string (nth 1 items) ",") "")))
3742             (delete-region (point-min) (point-max))
3743             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
3744             (insert "X-Signed-Headers: " (nth 1 items) "\n")
3745             (insert headers)
3746             (widen)
3747             (forward-line)
3748             (while (not (eobp))
3749               (if (looking-at "^-")
3750                   (insert "- "))
3751               (forward-line))
3752             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
3753             (insert "Version: " (car items) "\n\n")
3754             (insert (mapconcat 'identity (cddr items) "\n"))
3755             (insert "\n-----END PGP SIGNATURE-----\n")
3756             (let ((mm-security-handle (list (format "multipart/signed"))))
3757               (mml2015-clean-buffer)
3758               (let ((coding-system-for-write (or gnus-newsgroup-charset
3759                                                  'iso-8859-1)))
3760                 (funcall (mml2015-clear-verify-function)))
3761               (setq info
3762                     (or (mm-handle-multipart-ctl-parameter
3763                          mm-security-handle 'gnus-details)
3764                         (mm-handle-multipart-ctl-parameter
3765                          mm-security-handle 'gnus-info)))))
3766           (when info
3767             (let ((inhibit-read-only t) bface eface)
3768               (save-restriction
3769                 (message-narrow-to-head)
3770                 (goto-char (point-max))
3771                 (forward-line -1)
3772                 (setq bface (get-text-property (point-at-bol) 'face)
3773                       eface (get-text-property (1- (point-at-eol)) 'face))
3774                 (message-remove-header "X-Gnus-PGP-Verify")
3775                 (if (re-search-forward "^X-PGP-Sig:" nil t)
3776                     (forward-line)
3777                   (goto-char (point-max)))
3778                 (narrow-to-region (point) (point))
3779                 (insert "X-Gnus-PGP-Verify: " info "\n")
3780                 (goto-char (point-min))
3781                 (forward-line)
3782                 (while (not (eobp))
3783                   (if (not (looking-at "^[ \t]"))
3784                       (insert " "))
3785                   (forward-line))
3786                 ;; Do highlighting.
3787                 (goto-char (point-min))
3788                 (when (looking-at "\\([^:]+\\): *")
3789                   (put-text-property (match-beginning 1) (1+ (match-end 1))
3790                                      'face bface)
3791                   (put-text-property (match-end 0) (point-max)
3792                                      'face eface)))))))))
3793
3794 (defun article-verify-cancel-lock ()
3795   "Verify Cancel-Lock header."
3796   (interactive)
3797   (if (gnus-buffer-live-p gnus-original-article-buffer)
3798       (canlock-verify gnus-original-article-buffer)))
3799
3800 (defun article-monafy ()
3801   "Display body part with mona font."
3802   (interactive)
3803   (unless (if (featurep 'xemacs)
3804               (find-face 'gnus-mona-face)
3805             (facep 'gnus-mona-face))
3806     (require 'navi2ch-mona)
3807     (set-face-font (make-face 'gnus-mona-face) navi2ch-mona-font))
3808   (save-excursion
3809     (let ((inhibit-read-only t))
3810       (article-goto-body)
3811       (gnus-overlay-put
3812        (gnus-make-overlay (point) (point-max))
3813        'face 'gnus-mona-face))))
3814
3815 (eval-and-compile
3816   (mapcar
3817    (lambda (func)
3818      (let (afunc gfunc)
3819        (if (consp func)
3820            (setq afunc (car func)
3821                  gfunc (cdr func))
3822          (setq afunc func
3823                gfunc (intern (format "gnus-%s" func))))
3824        (defalias gfunc
3825          (when (fboundp afunc)
3826            `(lambda (&optional interactive &rest args)
3827               ,(documentation afunc t)
3828               (interactive (list t))
3829               (save-excursion
3830                 (set-buffer gnus-article-buffer)
3831                 (if interactive
3832                     (call-interactively ',afunc)
3833                   (apply ',afunc args))))))))
3834    '(article-hide-headers
3835      article-verify-x-pgp-sig
3836      article-verify-cancel-lock
3837      article-monafy
3838      article-hide-boring-headers
3839      article-treat-overstrike
3840      article-treat-ansi-sequences
3841      article-fill-long-lines
3842      article-capitalize-sentences
3843      article-remove-cr
3844      article-remove-leading-whitespace
3845      article-display-x-face
3846      article-display-face
3847      article-de-quoted-unreadable
3848      article-de-base64-unreadable
3849      article-decode-HZ
3850      article-wash-html
3851      article-unsplit-urls
3852      article-hide-list-identifiers
3853      article-strip-banner
3854      article-babel
3855      article-hide-pem
3856      article-hide-signature
3857      article-strip-headers-in-body
3858      article-remove-trailing-blank-lines
3859      article-strip-leading-blank-lines
3860      article-strip-multiple-blank-lines
3861      article-strip-leading-space
3862      article-strip-trailing-space
3863      article-strip-blank-lines
3864      article-strip-all-blank-lines
3865      article-date-local
3866      article-date-english
3867      article-date-iso8601
3868      article-date-original
3869      article-date-ut
3870      article-decode-mime-words
3871      article-decode-charset
3872      article-decode-encoded-words
3873      article-date-user
3874      article-date-lapsed
3875      article-emphasize
3876      article-treat-dumbquotes
3877      article-normalize-headers
3878      (article-show-all-headers . gnus-article-show-all-headers)
3879 ;;      (article-show-all . gnus-article-show-all)
3880      )))
3881 \f
3882 ;;;
3883 ;;; Gnus article mode
3884 ;;;
3885
3886 (put 'gnus-article-mode 'mode-class 'special)
3887
3888 (gnus-define-keys gnus-article-mode-map
3889   " " gnus-article-goto-next-page
3890   "\177" gnus-article-goto-prev-page
3891   [delete] gnus-article-goto-prev-page
3892   [backspace] gnus-article-goto-prev-page
3893   "\C-c^" gnus-article-refer-article
3894   "h" gnus-article-show-summary
3895   "s" gnus-article-show-summary
3896   "\C-c\C-m" gnus-article-mail
3897   "?" gnus-article-describe-briefly
3898   "e" gnus-summary-edit-article
3899   "<" beginning-of-buffer
3900   ">" end-of-buffer
3901   "\C-c\C-i" gnus-info-find-node
3902   "\C-c\C-b" gnus-bug
3903   "R" gnus-article-reply-with-original
3904   "F" gnus-article-followup-with-original
3905   "\C-hk" gnus-article-describe-key
3906   "\C-hc" gnus-article-describe-key-briefly
3907
3908   "\C-d" gnus-article-read-summary-keys
3909   "\M-*" gnus-article-read-summary-keys
3910   "\M-#" gnus-article-read-summary-keys
3911   "\M-^" gnus-article-read-summary-keys
3912   "\M-g" gnus-article-read-summary-keys)
3913
3914 ;; Define almost undefined keys to `gnus-article-read-summary-keys'.
3915 (let (keys)
3916   (let ((key 32))
3917     (while (<= key 127)
3918       (push (char-to-string key) keys)
3919       (incf key))
3920     (dolist (elem (accessible-keymaps gnus-summary-mode-map))
3921       (setq key (car elem))
3922       (when (and (> (length key) 0)
3923                  (not (eq 'menu-bar (aref key 0)))
3924                  (symbolp (lookup-key gnus-summary-mode-map key)))
3925         (push key keys))))
3926   (dolist (key keys)
3927     (unless (lookup-key gnus-article-mode-map key)
3928       (define-key gnus-article-mode-map key 'gnus-article-read-summary-keys))))
3929
3930 (defun gnus-article-make-menu-bar ()
3931   (unless (boundp 'gnus-article-commands-menu)
3932     (gnus-summary-make-menu-bar))
3933   (gnus-turn-off-edit-menu 'article)
3934   (unless (boundp 'gnus-article-article-menu)
3935     (easy-menu-define
3936      gnus-article-article-menu gnus-article-mode-map ""
3937      '("Article"
3938        ["Scroll forwards" gnus-article-goto-next-page t]
3939        ["Scroll backwards" gnus-article-goto-prev-page t]
3940        ["Show summary" gnus-article-show-summary t]
3941        ["Fetch Message-ID at point" gnus-article-refer-article t]
3942        ["Mail to address at point" gnus-article-mail t]
3943        ["Send a bug report" gnus-bug t]))
3944
3945     (easy-menu-define
3946      gnus-article-treatment-menu gnus-article-mode-map ""
3947      ;; Fixme: this should use :active (and maybe :visible).
3948      '("Treatment"
3949        ["Hide headers" gnus-article-hide-headers t]
3950        ["Hide signature" gnus-article-hide-signature t]
3951        ["Hide citation" gnus-article-hide-citation t]
3952        ["Treat overstrike" gnus-article-treat-overstrike t]
3953        ["Treat ANSI sequences" gnus-article-treat-ansi-sequences t]
3954        ["Remove carriage return" gnus-article-remove-cr t]
3955        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
3956        ["Treat html" gnus-article-wash-html t]
3957        ["Remove newlines from within URLs" gnus-article-unsplit-urls t]
3958        ["Decode HZ" gnus-article-decode-HZ t]))
3959
3960     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3961
3962     ;; Note "Post" menu is defined in gnus-sum.el for consistency
3963
3964     (gnus-run-hooks 'gnus-article-menu-hook)))
3965
3966 (defun gnus-article-mode ()
3967   "Major mode for displaying an article.
3968
3969 All normal editing commands are switched off.
3970
3971 The following commands are available in addition to all summary mode
3972 commands:
3973 \\<gnus-article-mode-map>
3974 \\[gnus-article-next-page]\t Scroll the article one page forwards
3975 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3976 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3977 \\[gnus-article-show-summary]\t Display the summary buffer
3978 \\[gnus-article-mail]\t Send a reply to the address near point
3979 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3980 \\[gnus-info-find-node]\t Go to the Gnus info node"
3981   (interactive)
3982   (kill-all-local-variables)
3983   (gnus-simplify-mode-line)
3984   (setq mode-name "Article")
3985   (setq major-mode 'gnus-article-mode)
3986   (make-local-variable 'minor-mode-alist)
3987   (unless (assq 'gnus-show-mime minor-mode-alist)
3988     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
3989   (use-local-map gnus-article-mode-map)
3990   (when (gnus-visual-p 'article-menu 'menu)
3991     (gnus-article-make-menu-bar)
3992     (when gnus-summary-tool-bar-map
3993       (set (make-local-variable 'tool-bar-map) gnus-summary-tool-bar-map)))
3994   (gnus-update-format-specifications nil 'article-mode)
3995   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3996   (set (make-local-variable 'gnus-page-broken) nil)
3997   (make-local-variable 'gnus-button-marker-list)
3998   (make-local-variable 'gnus-article-current-summary)
3999   (make-local-variable 'gnus-article-mime-handles)
4000   (make-local-variable 'gnus-article-decoded-p)
4001   (make-local-variable 'gnus-article-mime-handle-alist)
4002   (make-local-variable 'gnus-article-wash-types)
4003   (make-local-variable 'gnus-article-image-alist)
4004   (make-local-variable 'gnus-article-charset)
4005   (make-local-variable 'gnus-article-ignored-charsets)
4006   ;; Prevent recent Emacsen from displaying non-break space as "\ ".
4007   (set (make-local-variable 'nobreak-char-display) nil)
4008   (gnus-set-default-directory)
4009   (buffer-disable-undo)
4010   (setq buffer-read-only t
4011         show-trailing-whitespace nil)
4012   (set-syntax-table gnus-article-mode-syntax-table)
4013   (gnus-run-mode-hooks 'gnus-article-mode-hook))
4014
4015 (defun gnus-article-setup-buffer ()
4016   "Initialize the article buffer."
4017   (let* ((name (if gnus-single-article-buffer "*Article*"
4018                  (concat "*Article " gnus-newsgroup-name "*")))
4019          (original
4020           (progn (string-match "\\*Article" name)
4021                  (concat " *Original Article"
4022                          (substring name (match-end 0))))))
4023     (setq gnus-article-buffer name)
4024     (setq gnus-original-article-buffer original)
4025     (setq gnus-article-mime-handle-alist nil)
4026     ;; This might be a variable local to the summary buffer.
4027     (unless gnus-single-article-buffer
4028       (save-excursion
4029         (set-buffer gnus-summary-buffer)
4030         (setq gnus-article-buffer name)
4031         (setq gnus-original-article-buffer original)
4032         (gnus-set-global-variables)))
4033     (gnus-article-setup-highlight-words)
4034     ;; Init original article buffer.
4035     (save-excursion
4036       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
4037       (set-buffer-multibyte t)
4038       (setq major-mode 'gnus-original-article-mode)
4039       (make-local-variable 'gnus-original-article))
4040     (if (and (get-buffer name)
4041              (with-current-buffer name
4042                (if gnus-article-edit-mode
4043                    (if (y-or-n-p "Article mode edit in progress; discard? ")
4044                        (progn
4045                          (set-buffer-modified-p nil)
4046                          (gnus-kill-buffer name)
4047                          (message "")
4048                          nil)
4049                      (error "Action aborted"))
4050                  t)))
4051         (save-excursion
4052           (set-buffer name)
4053           (set (make-local-variable 'gnus-article-edit-mode) nil)
4054           (buffer-disable-undo)
4055           (setq buffer-read-only t)
4056           ;; This list just keeps growing if we don't reset it.
4057           (setq gnus-button-marker-list nil)
4058           (unless (eq major-mode 'gnus-article-mode)
4059             (gnus-article-mode))
4060           (current-buffer))
4061       (save-excursion
4062         (set-buffer (gnus-get-buffer-create name))
4063         (gnus-article-mode)
4064         (make-local-variable 'gnus-summary-buffer)
4065         (gnus-summary-set-local-parameters gnus-newsgroup-name)
4066         (current-buffer)))))
4067
4068 ;; Set article window start at LINE, where LINE is the number of lines
4069 ;; from the head of the article.
4070 (defun gnus-article-set-window-start (&optional line)
4071   (set-window-start
4072    (gnus-get-buffer-window gnus-article-buffer t)
4073    (save-excursion
4074      (set-buffer gnus-article-buffer)
4075      (goto-char (point-min))
4076      (if (not line)
4077          (point-min)
4078        (gnus-message 6 "Moved to bookmark")
4079        (search-forward "\n\n" nil t)
4080        (forward-line line)
4081        (point)))))
4082
4083 ;;; @@ article filters
4084 ;;;
4085
4086 (defun gnus-article-display-mime-message ()
4087   "Article display method for MIME message."
4088   ;; called from `gnus-original-article-buffer'.
4089   (let (charset all-headers)
4090     (with-current-buffer gnus-summary-buffer
4091       (setq charset default-mime-charset
4092             all-headers gnus-have-all-headers))
4093     (make-local-variable 'default-mime-charset)
4094     (setq default-mime-charset charset)
4095     (with-current-buffer (get-buffer-create gnus-article-buffer)
4096       (make-local-variable 'default-mime-charset)
4097       (setq default-mime-charset charset))
4098     (mime-display-message mime-message-structure
4099                           gnus-article-buffer nil gnus-article-mode-map)
4100     (when all-headers
4101       (gnus-article-hide-headers nil -1)))
4102   (run-hooks 'gnus-mime-article-prepare-hook))
4103
4104 (defun gnus-article-display-traditional-message ()
4105   "Article display method for traditional message."
4106   (set-buffer gnus-article-buffer)
4107   (let ((inhibit-read-only t))
4108     (erase-buffer)
4109     (insert-buffer-substring gnus-original-article-buffer)))
4110
4111 (defun gnus-article-make-full-mail-header (&optional number charset)
4112   "Create a new mail header structure in a raw article buffer."
4113   (unless (and number charset)
4114     (save-current-buffer
4115       (set-buffer gnus-summary-buffer)
4116       (unless number
4117         (setq number (or (cdr gnus-article-current) 0)))
4118       (unless charset
4119         (setq charset (or default-mime-charset 'x-ctext)))))
4120   (goto-char (point-min))
4121   (let ((header-end (if (search-forward "\n\n" nil t)
4122                         (1- (point))
4123                       (goto-char (point-max))))
4124         (chars (- (point-max) (point)))
4125         (lines (count-lines (point) (point-max)))
4126         (default-mime-charset charset)
4127         xref)
4128     (narrow-to-region (point-min) header-end)
4129     (setq xref (std11-fetch-field "xref"))
4130     (prog1
4131         (make-full-mail-header
4132          number
4133          (std11-fetch-field "subject")
4134          (std11-fetch-field "from")
4135          (std11-fetch-field "date")
4136          (std11-fetch-field "message-id")
4137          (std11-fetch-field "references")
4138          chars
4139          lines
4140          (when xref (concat "Xref: " xref)))
4141       (widen))))
4142
4143 (defun gnus-article-prepare (article &optional all-headers header)
4144   "Prepare ARTICLE in article mode buffer.
4145 ARTICLE should either be an article number or a Message-ID.
4146 If ARTICLE is an id, HEADER should be the article headers.
4147 If ALL-HEADERS is non-nil, no headers are hidden."
4148   (save-excursion
4149     ;; Make sure we start in a summary buffer.
4150     (unless (eq major-mode 'gnus-summary-mode)
4151       (set-buffer gnus-summary-buffer))
4152     (setq gnus-summary-buffer (current-buffer))
4153     (let* ((gnus-article (if header (mail-header-number header) article))
4154            (summary-buffer (current-buffer))
4155            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
4156            (group gnus-newsgroup-name)
4157            result)
4158       (save-excursion
4159         (gnus-article-setup-buffer)
4160         (set-buffer gnus-article-buffer)
4161         ;; Deactivate active regions.
4162         (when (and (boundp 'transient-mark-mode)
4163                    transient-mark-mode)
4164           (setq mark-active nil))
4165         (if (not (setq result (let ((inhibit-read-only t))
4166                                 (gnus-request-article-this-buffer
4167                                  article group))))
4168             ;; There is no such article.
4169             (save-excursion
4170               (when (and (numberp article)
4171                          (not (memq article gnus-newsgroup-sparse)))
4172                 (setq gnus-article-current
4173                       (cons gnus-newsgroup-name article))
4174                 (set-buffer gnus-summary-buffer)
4175                 (setq gnus-current-article article)
4176                 (if (and (memq article gnus-newsgroup-undownloaded)
4177                          (not (gnus-online (gnus-find-method-for-group
4178                                             gnus-newsgroup-name))))
4179                     (progn
4180                       (gnus-summary-set-agent-mark article)
4181                       (message "Message marked for downloading"))
4182                   (gnus-summary-mark-article article gnus-canceled-mark)
4183                   (unless (memq article gnus-newsgroup-sparse)
4184                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
4185           (if (or (eq result 'pseudo)
4186                   (eq result 'nneething))
4187               (progn
4188                 (save-excursion
4189                   (set-buffer summary-buffer)
4190                   (push article gnus-newsgroup-history)
4191                   (setq gnus-last-article gnus-current-article
4192                         gnus-current-article 0
4193                         gnus-current-headers nil
4194                         gnus-article-current nil)
4195                   (if (eq result 'nneething)
4196                       (gnus-configure-windows 'summary)
4197                     (gnus-configure-windows 'article))
4198                   (gnus-set-global-variables))
4199                 (let ((gnus-article-mime-handle-alist-1
4200                        gnus-article-mime-handle-alist))
4201                   (gnus-set-mode-line 'article)))
4202             ;; The result from the `request' was an actual article -
4203             ;; or at least some text that is now displayed in the
4204             ;; article buffer.
4205             (when (and (numberp article)
4206                        (not (eq article gnus-current-article)))
4207               ;; Seems like a new article has been selected.
4208               ;; `gnus-current-article' must be an article number.
4209               (save-excursion
4210                 (set-buffer summary-buffer)
4211                 (push article gnus-newsgroup-history)
4212                 (setq gnus-last-article gnus-current-article
4213                       gnus-current-article article
4214                       gnus-current-headers
4215                       (gnus-summary-article-header gnus-current-article)
4216                       gnus-article-current
4217                       (cons gnus-newsgroup-name gnus-current-article))
4218                 (unless (vectorp gnus-current-headers)
4219                   (setq gnus-current-headers nil))
4220                 (gnus-summary-goto-subject gnus-current-article)
4221                 (when (gnus-summary-show-thread)
4222                   ;; If the summary buffer really was folded, the
4223                   ;; previous goto may not actually have gone to
4224                   ;; the right article, but the thread root instead.
4225                   ;; So we go again.
4226                   (gnus-summary-goto-subject gnus-current-article))
4227                 (gnus-run-hooks 'gnus-mark-article-hook)
4228                 (gnus-set-mode-line 'summary)
4229                 (when (gnus-visual-p 'article-highlight 'highlight)
4230                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
4231                 ;; Set the global newsgroup variables here.
4232                 (gnus-set-global-variables)
4233                 (setq gnus-have-all-headers
4234                       (or all-headers gnus-show-all-headers))))
4235             (save-excursion
4236               (gnus-configure-windows 'article))
4237             (when (or (numberp article)
4238                       (stringp article))
4239               (gnus-article-prepare-display)
4240               ;; Do page break.
4241               (goto-char (point-min))
4242               (when gnus-break-pages
4243                 (gnus-narrow-to-page)))
4244             (let ((gnus-article-mime-handle-alist-1
4245                    gnus-article-mime-handle-alist))
4246               (gnus-set-mode-line 'article))
4247             (article-goto-body)
4248             (unless (bobp)
4249               (forward-line -1))
4250             (set-window-point (get-buffer-window (current-buffer)) (point))
4251             (gnus-configure-windows 'article)
4252             t))))))
4253
4254 (defun gnus-article-prepare-mime-display (&optional number)
4255   (goto-char (point-min))
4256   (when (re-search-forward "^[^\t ]+:" nil t)
4257     (goto-char (match-beginning 0)))
4258   (let ((entity (if (eq 1 (point-min))
4259                     (get-text-property 1 'mime-view-entity)
4260                   (get-text-property (point) 'mime-view-entity)))
4261         last-entity child-entity next type)
4262     (setq child-entity (mime-entity-children entity))
4263     (if child-entity
4264         (setq last-entity (nth (1- (length child-entity))
4265                                child-entity))
4266       (setq last-entity entity))
4267     (save-restriction
4268       (narrow-to-region (point)
4269                         (if (search-forward "\n\n" nil t)
4270                             (point)
4271                           (point-max)))
4272       (gnus-article-save-original-date (gnus-treat-article 'head))
4273       (put-text-property (point-min) (point-max) 'article-treated-header t)
4274       (goto-char (point-max)))
4275     (while (and (not (eobp)) entity)
4276       (setq next (set-marker
4277                   (make-marker)
4278                   (next-single-property-change (point) 'mime-view-entity
4279                                                nil (point-max))))
4280       (let ((types (mime-entity-content-type entity)))
4281         (while (eq 'multipart (mime-content-type-primary-type types))
4282           (setq entity (car (mime-entity-children entity))
4283                 types (mime-entity-content-type entity)))
4284         (when types
4285           (setq type (format "%s/%s"
4286                              (mime-content-type-primary-type types)
4287                              (mime-content-type-subtype types)))))
4288       (if (string-equal type "message/rfc822")
4289           (progn
4290             (setq next (point))
4291             (let ((children (mime-entity-children entity))
4292                   last-children)
4293               (when children
4294                 (setq last-children (nth (1- (length children)) children))
4295                 (while
4296                     (and
4297                      (not (eq last-children
4298                               (get-text-property next 'mime-view-entity)))
4299                      (setq next
4300                            (next-single-property-change next
4301                                                         'mime-view-entity
4302                                                         nil (point-max)))))))
4303             (setq next (next-single-property-change next 'mime-view-entity
4304                                                     nil (point-max)))
4305             (save-restriction
4306               (narrow-to-region (point) next)
4307               (gnus-article-prepare-mime-display)
4308               (goto-char (point-max)))
4309             (setq entity (get-text-property (point) 'mime-view-entity)))
4310         (save-restriction
4311           (narrow-to-region (point) next)
4312           ;; Kludge. We have to count true number, but for now,
4313           ;; part number is here only to achieve `last'.
4314           (gnus-treat-article nil 1
4315                               (if (eq entity last-entity)
4316                                   1 2)
4317                               type)
4318           (goto-char (point-max)))
4319         (setq entity (get-text-property next 'mime-view-entity))))))
4320
4321 ;;;###autoload
4322 (defun gnus-article-prepare-display ()
4323   "Make the current buffer look like a nice article."
4324   (let ((gnus-article-buffer (current-buffer))
4325         buffer-read-only
4326         (inhibit-read-only t))
4327     (unless (eq major-mode 'gnus-article-mode)
4328       (gnus-article-mode))
4329     (setq buffer-read-only nil
4330           gnus-button-marker-list nil
4331           gnus-article-wash-types nil
4332           gnus-article-image-alist nil)
4333     (save-restriction
4334       (widen)
4335       (static-if (featurep 'xemacs)
4336           (map-extents (lambda (extent maparg) (delete-extent extent)))
4337         (let ((lists (overlay-lists)))
4338           (dolist (overlay (nconc (car lists) (cdr lists)))
4339             (delete-overlay overlay)))))
4340     (gnus-run-hooks 'gnus-tmp-internal-hook))
4341   (let ((show-mime (unless (member gnus-newsgroup-name '("nndraft:delayed"
4342                                                          "nndraft:drafts"))
4343                      gnus-show-mime))
4344         (inhibit-read-only t))
4345     (set-buffer gnus-original-article-buffer)
4346     ;; Display message.
4347     (setq mime-message-structure gnus-current-headers)
4348     (mime-buffer-entity-set-buffer-internal mime-message-structure
4349                                             gnus-original-article-buffer)
4350     (mime-entity-set-representation-type-internal mime-message-structure
4351                                                   'mime-buffer-entity)
4352     (luna-send mime-message-structure 'initialize-instance
4353                mime-message-structure)
4354     (if show-mime
4355         (let (mime-display-header-hook mime-display-text/plain-hook)
4356           (funcall gnus-article-display-method-for-mime))
4357       (funcall gnus-article-display-method-for-traditional))
4358     ;; Call the treatment functions.
4359     (save-restriction
4360       (widen)
4361       (if show-mime
4362           (gnus-article-prepare-mime-display)
4363         (narrow-to-region (goto-char (point-min))
4364                           (if (search-forward "\n\n" nil t)
4365                               (point)
4366                             (point-max)))
4367         (gnus-article-save-original-date (gnus-treat-article 'head))
4368         (put-text-property (point-min) (point-max) 'article-treated-header t)
4369         (goto-char (point-max))
4370         (widen)
4371         (narrow-to-region (point) (point-max))
4372         (gnus-treat-article nil))
4373       (put-text-property (point-min) (point-max) 'read-only nil)))
4374   (gnus-run-hooks 'gnus-article-prepare-hook))
4375
4376 (defun gnus-article-decode-article-as-default-mime-charset ()
4377   "Decode an article as `default-mime-charset'.  It won't work if the
4378 value of the variable `gnus-show-mime' is non-nil."
4379   (unless (or gnus-show-mime
4380               (member gnus-newsgroup-name '("nndraft:delayed"
4381                                             "nndraft:drafts")))
4382     (set (make-local-variable 'default-mime-charset)
4383          (with-current-buffer gnus-summary-buffer
4384            default-mime-charset))
4385     (decode-mime-charset-region (point-min) (point-max)
4386                                 default-mime-charset)))
4387
4388 ;;;
4389 ;;; Gnus MIME viewing functions
4390 ;;;
4391
4392 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
4393   "Format of the MIME buttons.
4394
4395 Valid specifiers include:
4396 %t  The MIME type
4397 %T  MIME type, along with additional info
4398 %n  The `name' parameter
4399 %d  The description, if any
4400 %l  The length of the encoded part
4401 %p  The part identifier number
4402 %e  Dots if the part isn't displayed
4403
4404 General format specifiers can also be used.  See Info node
4405 `(gnus)Formatting Variables'.")
4406
4407 (defvar gnus-mime-button-line-format-alist
4408   '((?t gnus-tmp-type ?s)
4409     (?T gnus-tmp-type-long ?s)
4410     (?n gnus-tmp-name ?s)
4411     (?d gnus-tmp-description ?s)
4412     (?p gnus-tmp-id ?s)
4413     (?l gnus-tmp-length ?d)
4414     (?e gnus-tmp-dots ?s)))
4415
4416 (defvar gnus-mime-button-commands
4417   '((gnus-article-press-button "\r" "Toggle Display")
4418     (gnus-mime-view-part "v" "View Interactively...")
4419     (gnus-mime-view-part-as-type "t" "View As Type...")
4420     (gnus-mime-view-part-as-charset "C" "View As charset...")
4421     (gnus-mime-save-part "o" "Save...")
4422     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
4423     (gnus-mime-replace-part "r" "Replace part")
4424     (gnus-mime-delete-part "d" "Delete part")
4425     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
4426     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
4427     (gnus-mime-view-part-internally "E" "View Internally") ;; Why `E'?
4428     (gnus-mime-view-part-externally "e" "View Externally")
4429     (gnus-mime-print-part "p" "Print")
4430     (gnus-mime-pipe-part "|" "Pipe To Command...")
4431     (gnus-mime-action-on-part "." "Take action on the part...")))
4432
4433 (defun gnus-article-mime-part-status ()
4434   (with-current-buffer gnus-article-buffer
4435     (let ((entity (get-text-property (point-min) 'mime-view-entity))
4436           children)
4437       (if (and entity
4438                (setq children (mime-entity-children entity))
4439                (setq children (length children)))
4440           (if (eq 1 children)
4441               " (1 part)"
4442             (format " (%d parts)" children))
4443         ""))))
4444
4445 (defvar gnus-mime-button-map
4446   (let ((map (make-sparse-keymap)))
4447     (define-key map gnus-mouse-2 'gnus-article-push-button)
4448     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
4449     (dolist (c gnus-mime-button-commands)
4450       (define-key map (cadr c) (car c)))
4451     map))
4452
4453 (easy-menu-define
4454   gnus-mime-button-menu gnus-mime-button-map "MIME button menu."
4455   `("MIME Part"
4456     ,@(mapcar (lambda (c)
4457                 (vector (caddr c) (car c) :enable t))
4458               gnus-mime-button-commands)))
4459
4460 (defun gnus-mime-button-menu (event prefix)
4461  "Construct a context-sensitive menu of MIME commands."
4462  (interactive "e\nP")
4463  (save-window-excursion
4464    (let ((pos (event-start event)))
4465      (select-window (posn-window pos))
4466      (goto-char (posn-point pos))
4467      (gnus-article-check-buffer)
4468      (popup-menu gnus-mime-button-menu nil prefix))))
4469
4470 (defun gnus-mime-view-all-parts (&optional handles)
4471   "View all the MIME parts."
4472   (interactive)
4473   (with-current-buffer gnus-article-buffer
4474     (let ((handles (or handles gnus-article-mime-handles))
4475           (mail-parse-charset gnus-newsgroup-charset)
4476           (mail-parse-ignored-charsets
4477            (with-current-buffer gnus-summary-buffer
4478              gnus-newsgroup-ignored-charsets)))
4479       (when handles
4480         (mm-remove-parts handles)
4481         (goto-char (point-min))
4482         (or (search-forward "\n\n") (goto-char (point-max)))
4483         (let ((inhibit-read-only t))
4484           (delete-region (point) (point-max))
4485           (mm-display-parts handles))))))
4486
4487 (defun gnus-article-jump-to-part (n)
4488   "Jump to MIME part N."
4489   (interactive "P")
4490   (pop-to-buffer gnus-article-buffer)
4491   ;; FIXME: why is it necessary?
4492   (sit-for 0)
4493   (let ((parts (length gnus-article-mime-handle-alist)))
4494     (or n (setq n
4495                 (string-to-number
4496                  (read-string ;; Emacs 21 doesn't have `read-number'.
4497                   (format "Jump to part (2..%s): " parts)))))
4498     (unless (and (integerp n) (<= n parts) (>= n 1))
4499       (setq n
4500             (progn
4501               (gnus-message 7 "Invalid part `%s', using %s instead."
4502                             n parts)
4503               parts)))
4504     (gnus-message 9 "Jumping to part %s." n)
4505     (cond ((>= gnus-auto-select-part 1)
4506            (while (and (<= n parts)
4507                        (not (gnus-article-goto-part n)))
4508              (setq n (1+ n))))
4509           ((< gnus-auto-select-part 0)
4510            (while (and (>= n 1)
4511                        (not (gnus-article-goto-part n)))
4512              (setq n (1- n))))
4513           (t
4514            (gnus-article-goto-part n)))))
4515
4516 (eval-when-compile
4517   (defsubst gnus-article-edit-part (handles &optional current-id)
4518     "Edit an article in order to delete a mime part.
4519 This function is exclusively used by `gnus-mime-save-part-and-strip'
4520 and `gnus-mime-delete-part', and not provided at run-time normally."
4521     (gnus-article-edit-article
4522      `(lambda ()
4523         (buffer-disable-undo)
4524         (erase-buffer)
4525         (let ((mail-parse-charset (or gnus-article-charset
4526                                       ',gnus-newsgroup-charset))
4527               (mail-parse-ignored-charsets
4528                (or gnus-article-ignored-charsets
4529                    ',gnus-newsgroup-ignored-charsets))
4530               (mbl mml-buffer-list))
4531           (setq mml-buffer-list nil)
4532           (insert-buffer-substring gnus-original-article-buffer)
4533           (mime-to-mml ',handles)
4534           (setq gnus-article-mime-handles nil)
4535           (let ((mbl1 mml-buffer-list))
4536             (setq mml-buffer-list mbl)
4537             (set (make-local-variable 'mml-buffer-list) mbl1))
4538           (gnus-make-local-hook 'kill-buffer-hook)
4539           (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
4540      `(lambda (no-highlight)
4541         (let ((mail-parse-charset (or gnus-article-charset
4542                                       ',gnus-newsgroup-charset))
4543               (message-options message-options)
4544               (message-options-set-recipient)
4545               (mail-parse-ignored-charsets
4546                (or gnus-article-ignored-charsets
4547                    ',gnus-newsgroup-ignored-charsets)))
4548           (mml-to-mime)
4549           (mml-destroy-buffers)
4550           (remove-hook 'kill-buffer-hook
4551                        'mml-destroy-buffers t)
4552           (kill-local-variable 'mml-buffer-list))
4553         (gnus-summary-edit-article-done
4554          ,(or (mail-header-references gnus-current-headers) "")
4555          ,(gnus-group-read-only-p)
4556          ,gnus-summary-buffer no-highlight))
4557      t)
4558     (gnus-article-edit-done)
4559     (gnus-summary-expand-window)
4560     (gnus-summary-show-article)
4561     (when (and current-id (integerp gnus-auto-select-part))
4562       (gnus-article-jump-to-part
4563        (+ current-id gnus-auto-select-part)))))
4564
4565 (defun gnus-mime-replace-part (file)
4566   "Replace MIME part under point with an external body."
4567   ;; Useful if file has already been saved to disk
4568   (interactive
4569    (list
4570     (mm-with-multibyte
4571       (read-file-name "Replace MIME part with file: "
4572                       (or mm-default-directory default-directory)
4573                       nil nil))))
4574   (gnus-mime-save-part-and-strip file))
4575
4576 (defun gnus-mime-save-part-and-strip (&optional file)
4577   "Save the MIME part under point then replace it with an external body.
4578 If FILE is given, use it for the external part."
4579   (interactive)
4580   (gnus-article-check-buffer)
4581   (when (gnus-group-read-only-p)
4582     (error "The current group does not support deleting of parts"))
4583   (when (mm-complicated-handles gnus-article-mime-handles)
4584     (error "\
4585 The current article has a complicated MIME structure, giving up..."))
4586   (let* ((data (get-text-property (point) 'gnus-data))
4587          (id (get-text-property (point) 'gnus-part))
4588          param
4589          (handles gnus-article-mime-handles))
4590     (unless file
4591       (setq file
4592             (and data (mm-save-part data "Delete MIME part and save to: "))))
4593     (when file
4594       (with-current-buffer (mm-handle-buffer data)
4595         (erase-buffer)
4596         (insert "Content-Type: " (mm-handle-media-type data))
4597         (mml-insert-parameter-string (cdr (mm-handle-type data))
4598                                      '(charset))
4599         (insert "\n")
4600         (insert "Content-ID: " (message-make-message-id) "\n")
4601         (insert "Content-Transfer-Encoding: binary\n")
4602         (insert "\n"))
4603       (setcdr data
4604               (cdr (mm-make-handle nil
4605                                    `("message/external-body"
4606                                      (access-type . "LOCAL-FILE")
4607                                      (name . ,file)))))
4608       ;; (set-buffer gnus-summary-buffer)
4609       (gnus-article-edit-part handles id))))
4610
4611 ;; A function like `gnus-summary-save-parts' (`X m', `<MIME> <Extract all
4612 ;; parts...>') but with stripping would be nice.
4613
4614 (defun gnus-mime-delete-part ()
4615   "Delete the MIME part under point.
4616 Replace it with some information about the removed part."
4617   (interactive)
4618   (gnus-article-check-buffer)
4619   (when (gnus-group-read-only-p)
4620     (error "The current group does not support deleting of parts"))
4621   (when (mm-complicated-handles gnus-article-mime-handles)
4622     (error "\
4623 The current article has a complicated MIME structure, giving up..."))
4624   (when (or gnus-expert-user
4625             (gnus-yes-or-no-p "\
4626 Deleting parts may malfunction or destroy the article; continue? "))
4627     (let* ((data (get-text-property (point) 'gnus-data))
4628            (id (get-text-property (point) 'gnus-part))
4629            (handles gnus-article-mime-handles)
4630            (none "(none)")
4631            (description
4632             (or
4633              (mail-decode-encoded-word-string (or (mm-handle-description data)
4634                                                   none))))
4635            (filename
4636             (or (mail-content-type-get (mm-handle-disposition data) 'filename)
4637                 none))
4638            (type (mm-handle-media-type data)))
4639       (unless data
4640         (error "No MIME part under point"))
4641       (with-current-buffer (mm-handle-buffer data)
4642         (let ((bsize (format "%s" (buffer-size))))
4643           (erase-buffer)
4644           (insert
4645            (concat
4646             ",----\n"
4647             "| The following attachment has been deleted:\n"
4648             "|\n"
4649             "| Type:           " type "\n"
4650             "| Filename:       " filename "\n"
4651             "| Size (encoded): " bsize " Byte\n"
4652             "| Description:    " description "\n"
4653             "`----\n"))
4654           (setcdr data
4655                   (cdr (mm-make-handle
4656                         nil `("text/plain") nil nil
4657                         (list "attachment")
4658                         (format "Deleted attachment (%s bytes)" bsize))))))
4659       ;; (set-buffer gnus-summary-buffer)
4660       (gnus-article-edit-part handles id))))
4661
4662 (defun gnus-mime-save-part ()
4663   "Save the MIME part under point."
4664   (interactive)
4665   (gnus-article-check-buffer)
4666   (let ((data (get-text-property (point) 'gnus-data)))
4667     (when data
4668       (mm-save-part data))))
4669
4670 (defun gnus-mime-pipe-part ()
4671   "Pipe the MIME part under point to a process."
4672   (interactive)
4673   (gnus-article-check-buffer)
4674   (let ((data (get-text-property (point) 'gnus-data)))
4675     (when data
4676       (mm-pipe-part data))))
4677
4678 (defun gnus-mime-view-part ()
4679   "Interactively choose a viewing method for the MIME part under point."
4680   (interactive)
4681   (gnus-article-check-buffer)
4682   (let ((data (get-text-property (point) 'gnus-data)))
4683     (when data
4684       (setq gnus-article-mime-handles
4685             (mm-merge-handles
4686              gnus-article-mime-handles (setq data (copy-sequence data))))
4687       (mm-interactively-view-part data))))
4688
4689 (defun gnus-mime-view-part-as-type-internal ()
4690   (gnus-article-check-buffer)
4691   (let* ((handle (get-text-property (point) 'gnus-data))
4692          (name (or
4693                 ;; Content-Type: foo/bar; name=...
4694                 (mail-content-type-get (mm-handle-type handle) 'name)
4695                 ;; Content-Disposition: attachment; filename=...
4696                 (cdr (assq 'filename (cdr (mm-handle-disposition handle))))))
4697          (def-type (and name (mm-default-file-encoding name))))
4698     (and def-type (cons def-type 0))))
4699
4700 (defun gnus-mime-view-part-as-type (&optional mime-type)
4701   "Choose a MIME media type, and view the part as such."
4702   (interactive)
4703   (unless mime-type
4704     (setq mime-type
4705           (let ((default (gnus-mime-view-part-as-type-internal)))
4706             (completing-read
4707              (format "View as MIME type (default %s): "
4708                      (car default))
4709              (mapcar #'list (mailcap-mime-types))
4710              nil nil nil nil
4711              (car default)))))
4712   (gnus-article-check-buffer)
4713   (let ((handle (get-text-property (point) 'gnus-data)))
4714     (when handle
4715       (setq handle
4716             (mm-make-handle (mm-handle-buffer handle)
4717                             (cons mime-type (cdr (mm-handle-type handle)))
4718                             (mm-handle-encoding handle)
4719                             (mm-handle-undisplayer handle)
4720                             (mm-handle-disposition handle)
4721                             (mm-handle-description handle)
4722                             nil
4723                             (mm-handle-id handle)))
4724       (setq gnus-article-mime-handles
4725             (mm-merge-handles gnus-article-mime-handles handle))
4726       (gnus-mm-display-part handle))))
4727
4728 (defun gnus-mime-copy-part (&optional handle arg)
4729   "Put the MIME part under point into a new buffer.
4730 If `auto-compression-mode' is enabled, compressed files like .gz and .bz2
4731 are decompressed."
4732   (interactive (list nil current-prefix-arg))
4733   (gnus-article-check-buffer)
4734   (unless handle
4735     (setq handle (get-text-property (point) 'gnus-data)))
4736   (when handle
4737     (let ((filename (or (mail-content-type-get (mm-handle-disposition handle)
4738                                                'name)
4739                         (mail-content-type-get (mm-handle-disposition handle)
4740                                                'filename)))
4741           contents dont-decode charset coding-system)
4742       (mm-with-unibyte-buffer
4743         (mm-insert-part handle)
4744         (setq contents (or (condition-case nil
4745                                (mm-decompress-buffer filename nil 'sig)
4746                              (error
4747                               (setq dont-decode t)
4748                               nil))
4749                            (buffer-string))))
4750       (setq filename (cond (filename (file-name-nondirectory filename))
4751                            (dont-decode "*raw data*")
4752                            (t "*decoded*")))
4753       (cond
4754        (dont-decode)
4755        ((not arg)
4756         (unless (setq charset (mail-content-type-get
4757                                (mm-handle-type handle) 'charset))
4758           (unless (setq coding-system (mm-with-unibyte-buffer
4759                                         (insert contents)
4760                                         (mm-find-buffer-file-coding-system)))
4761             (setq charset gnus-newsgroup-charset))))
4762        ((numberp arg)
4763         (setq charset (or (cdr (assq arg
4764                                      gnus-summary-show-article-charset-alist))
4765                           (mm-read-coding-system "Charset: ")))))
4766       (switch-to-buffer (generate-new-buffer filename))
4767       (if (or coding-system
4768               (and charset
4769                    (setq coding-system (mm-charset-to-coding-system charset))
4770                    (not (eq charset 'ascii))))
4771           (progn
4772             (mm-enable-multibyte)
4773             (insert (mm-decode-coding-string contents coding-system))
4774             (setq buffer-file-coding-system
4775                   (if (boundp 'last-coding-system-used)
4776                       (symbol-value 'last-coding-system-used)
4777                     coding-system)))
4778         (mm-disable-multibyte)
4779         (insert contents)
4780         (setq buffer-file-coding-system mm-binary-coding-system))
4781       ;; We do it this way to make `normal-mode' set the appropriate mode.
4782       (unwind-protect
4783           (progn
4784             (setq buffer-file-name (expand-file-name filename))
4785             (normal-mode))
4786         (setq buffer-file-name nil))
4787       (goto-char (point-min)))))
4788
4789 (defun gnus-mime-print-part (&optional handle filename)
4790   "Print the MIME part under point."
4791   (interactive (list nil (ps-print-preprint current-prefix-arg)))
4792   (gnus-article-check-buffer)
4793   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4794          (contents (and handle (mm-get-part handle)))
4795          (file (mm-make-temp-file (expand-file-name "mm." mm-tmp-directory)))
4796          (printer (mailcap-mime-info (mm-handle-media-type handle) "print")))
4797     (when contents
4798         (if printer
4799             (unwind-protect
4800                 (progn
4801                   (mm-save-part-to-file handle file)
4802                   (call-process shell-file-name nil
4803                                 (generate-new-buffer " *mm*")
4804                                 nil
4805                                 shell-command-switch
4806                                 (mm-mailcap-command
4807                                  printer file (mm-handle-type handle))))
4808               (delete-file file))
4809           (with-temp-buffer
4810             (insert contents)
4811             (gnus-print-buffer))
4812           (ps-despool filename)))))
4813
4814 (defun gnus-mime-inline-part (&optional handle arg)
4815   "Insert the MIME part under point into the current buffer.
4816 Compressed files like .gz and .bz2 are decompressed."
4817   (interactive (list nil current-prefix-arg))
4818   (gnus-article-check-buffer)
4819   (unless handle
4820     (setq handle (get-text-property (point) 'gnus-data)))
4821   (when handle
4822     (let ((b (point))
4823           (inhibit-read-only t)
4824           contents charset coding-system)
4825       (if (and (not arg) (mm-handle-undisplayer handle))
4826           (mm-remove-part handle)
4827         (mm-with-unibyte-buffer
4828           (mm-insert-part handle)
4829           (setq contents
4830                 (or (mm-decompress-buffer
4831                      (or (mail-content-type-get (mm-handle-disposition handle)
4832                                                 'name)
4833                          (mail-content-type-get (mm-handle-disposition handle)
4834                                                 'filename))
4835                      nil t)
4836                     (buffer-string))))
4837         (cond
4838          ((not arg)
4839           (unless (setq charset (mail-content-type-get
4840                                  (mm-handle-type handle) 'charset))
4841             (unless (setq coding-system
4842                           (mm-with-unibyte-buffer
4843                             (insert contents)
4844                             (mm-find-buffer-file-coding-system)))
4845               (setq charset gnus-newsgroup-charset))))
4846          ((numberp arg)
4847           (if (mm-handle-undisplayer handle)
4848               (mm-remove-part handle))
4849           (setq charset
4850                 (or (cdr (assq arg
4851                                gnus-summary-show-article-charset-alist))
4852                     (mm-read-coding-system "Charset: "))))
4853          (t
4854           (if (mm-handle-undisplayer handle)
4855               (mm-remove-part handle))))
4856         (forward-line 2)
4857         (mm-insert-inline
4858          handle
4859          (if (or coding-system
4860                  (and charset
4861                       (setq coding-system
4862                             (mm-charset-to-coding-system charset))
4863                       (not (eq coding-system 'ascii))))
4864              (mm-decode-coding-string contents coding-system)
4865            (mm-string-to-multibyte contents)))
4866         (goto-char b)))))
4867
4868 (defun gnus-mime-view-part-as-charset (&optional handle arg)
4869   "Insert the MIME part under point into the current buffer using the
4870 specified charset."
4871   (interactive (list nil current-prefix-arg))
4872   (gnus-article-check-buffer)
4873   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4874          contents charset
4875          (b (point))
4876          (inhibit-read-only t))
4877     (when handle
4878       (if (mm-handle-undisplayer handle)
4879           (mm-remove-part handle))
4880       (let ((gnus-newsgroup-charset
4881              (or (cdr (assq arg
4882                             gnus-summary-show-article-charset-alist))
4883                  (mm-read-coding-system "Charset: ")))
4884             (gnus-newsgroup-ignored-charsets 'gnus-all))
4885         (gnus-article-press-button)))))
4886
4887 (defun gnus-mime-view-part-externally (&optional handle)
4888   "View the MIME part under point with an external viewer."
4889   (interactive)
4890   (gnus-article-check-buffer)
4891   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4892          (mm-user-display-methods nil)
4893          (mm-inlined-types nil)
4894          (mail-parse-charset gnus-newsgroup-charset)
4895          (mail-parse-ignored-charsets
4896           (with-current-buffer gnus-summary-buffer
4897             gnus-newsgroup-ignored-charsets)))
4898     (when handle
4899       (if (mm-handle-undisplayer handle)
4900           (mm-remove-part handle)
4901         (mm-display-part handle)))))
4902
4903 (defun gnus-mime-view-part-internally (&optional handle)
4904   "View the MIME part under point with an internal viewer.
4905 If no internal viewer is available, use an external viewer."
4906   (interactive)
4907   (gnus-article-check-buffer)
4908   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
4909          (mm-inlined-types '(".*"))
4910          (mm-inline-large-images t)
4911          (mail-parse-charset gnus-newsgroup-charset)
4912          (mail-parse-ignored-charsets
4913           (with-current-buffer gnus-summary-buffer
4914             gnus-newsgroup-ignored-charsets))
4915          (inhibit-read-only t))
4916     (when handle
4917       (if (mm-handle-undisplayer handle)
4918           (mm-remove-part handle)
4919         (mm-display-part handle)))))
4920
4921 (defun gnus-mime-action-on-part (&optional action)
4922   "Do something with the MIME attachment at \(point\)."
4923   (interactive
4924    (list (completing-read "Action: " gnus-mime-action-alist nil t)))
4925   (gnus-article-check-buffer)
4926   (let ((action-pair (assoc action gnus-mime-action-alist)))
4927     (if action-pair
4928         (funcall (cdr action-pair)))))
4929
4930 (defun gnus-article-part-wrapper (n function &optional no-handle interactive)
4931   "Call FUNCTION on MIME part N.
4932 Unless NO-HANDLE, call FUNCTION with N-th MIME handle as it's only argument.
4933 If INTERACTIVE, call FUNCTION interactivly."
4934   (let (window frame)
4935     ;; Check whether the article is displayed.
4936     (unless (and (gnus-buffer-live-p gnus-article-buffer)
4937                  (setq window (get-buffer-window gnus-article-buffer t))
4938                  (frame-visible-p (setq frame (window-frame window))))
4939       (error "No article is displayed"))
4940     (with-current-buffer gnus-article-buffer
4941       ;; Check whether the article displays the right contents.
4942       (unless (with-current-buffer gnus-summary-buffer
4943                 (eq gnus-current-article (gnus-summary-article-number)))
4944         (error "You should select the right article first"))
4945       ;; Check whether the specified part exists.
4946       (when (> n (length gnus-article-mime-handle-alist))
4947         (error "No such part")))
4948     (unless
4949         (progn
4950           ;; To select the window is needed so that the cursor
4951           ;; might be visible on the MIME button.
4952           (select-window (prog1
4953                              window
4954                            (setq window (selected-window))
4955                            ;; Article may be displayed in the other frame.
4956                            (gnus-select-frame-set-input-focus
4957                             (prog1
4958                                 frame
4959                               (setq frame (selected-frame))))))
4960           (when (gnus-article-goto-part n)
4961             ;; We point the cursor and the arrow at the MIME button
4962             ;; when the `function' prompt the user for something.
4963             (let ((cursor-in-non-selected-windows t)
4964                   (overlay-arrow-string "=>")
4965                   (overlay-arrow-position (point-marker)))
4966               (unwind-protect
4967                   (cond
4968                    ((and no-handle interactive)
4969                     (call-interactively function))
4970                    (no-handle
4971                     (funcall function))
4972                    (interactive
4973                     (call-interactively
4974                      function
4975                      (cdr (assq n gnus-article-mime-handle-alist))))
4976                    (t
4977                     (funcall function
4978                              (cdr (assq n gnus-article-mime-handle-alist)))))
4979                 (set-marker overlay-arrow-position nil)
4980                 (unless gnus-auto-select-part
4981                   (gnus-select-frame-set-input-focus frame)
4982                   (select-window window))))
4983             t))
4984       (if gnus-inhibit-mime-unbuttonizing
4985           ;; This is the default though the program shouldn't reach here.
4986           (error "No such part")
4987         ;; The part which doesn't have the MIME button is selected.
4988         ;; So, we display all the buttons and redo it.
4989         (let ((gnus-inhibit-mime-unbuttonizing t))
4990           (gnus-summary-show-article)
4991           (gnus-article-part-wrapper n function no-handle))))))
4992
4993 (defun gnus-article-pipe-part (n)
4994   "Pipe MIME part N, which is the numerical prefix."
4995   (interactive "p")
4996   (gnus-article-part-wrapper n 'mm-pipe-part))
4997
4998 (defun gnus-article-save-part (n)
4999   "Save MIME part N, which is the numerical prefix."
5000   (interactive "p")
5001   (gnus-article-part-wrapper n 'mm-save-part))
5002
5003 (defun gnus-article-interactively-view-part (n)
5004   "View MIME part N interactively, which is the numerical prefix."
5005   (interactive "p")
5006   (gnus-article-part-wrapper n 'mm-interactively-view-part))
5007
5008 (defun gnus-article-copy-part (n)
5009   "Copy MIME part N, which is the numerical prefix."
5010   (interactive "p")
5011   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
5012
5013 (defun gnus-article-view-part-as-charset (n)
5014   "View MIME part N using a specified charset.
5015 N is the numerical prefix."
5016   (interactive "p")
5017   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
5018
5019 (defun gnus-article-view-part-externally (n)
5020   "View MIME part N externally, which is the numerical prefix."
5021   (interactive "p")
5022   (gnus-article-part-wrapper n 'gnus-mime-view-part-externally))
5023
5024 (defun gnus-article-inline-part (n)
5025   "Inline MIME part N, which is the numerical prefix."
5026   (interactive "p")
5027   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
5028
5029 (defun gnus-article-save-part-and-strip (n)
5030   "Save MIME part N and replace it with an external body.
5031 N is the numerical prefix."
5032   (interactive "p")
5033   (gnus-article-part-wrapper n 'gnus-mime-save-part-and-strip t))
5034
5035 (defun gnus-article-replace-part (n)
5036   "Replace MIME part N with an external body.
5037 N is the numerical prefix."
5038   (interactive "p")
5039   (gnus-article-part-wrapper n 'gnus-mime-replace-part t t))
5040
5041 (defun gnus-article-delete-part (n)
5042   "Delete MIME part N and add some information about the removed part.
5043 N is the numerical prefix."
5044   (interactive "p")
5045   (gnus-article-part-wrapper n 'gnus-mime-delete-part t))
5046
5047 (defun gnus-article-mime-match-handle-first (condition)
5048   (if condition
5049       (let (n)
5050         (dolist (ihandle gnus-article-mime-handle-alist)
5051           (if (and (cond
5052                     ((functionp condition)
5053                      (funcall condition (cdr ihandle)))
5054                     ((eq condition 'undisplayed)
5055                      (not (or (mm-handle-undisplayer (cdr ihandle))
5056                               (equal (mm-handle-media-type (cdr ihandle))
5057                                      "multipart/alternative"))))
5058                     ((eq condition 'undisplayed-alternative)
5059                      (not (mm-handle-undisplayer (cdr ihandle))))
5060                     (t t))
5061                    (gnus-article-goto-part (car ihandle))
5062                    (or (not n) (< (car ihandle) n)))
5063               (setq n (car ihandle))))
5064         (or n 1))
5065     1))
5066
5067 (defun gnus-article-view-part (&optional n)
5068   "View MIME part N, which is the numerical prefix."
5069   (interactive "P")
5070   (with-current-buffer gnus-article-buffer
5071     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
5072                              gnus-article-mime-match-handle-function)))
5073     (when (> n (length gnus-article-mime-handle-alist))
5074       (error "No such part"))
5075     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
5076       (when (gnus-article-goto-part n)
5077         (if (equal (car handle) "multipart/alternative")
5078             (gnus-article-press-button)
5079           (when (eq (gnus-mm-display-part handle) 'internal)
5080             (gnus-set-window-start)))))))
5081
5082 (defsubst gnus-article-mime-total-parts ()
5083   (if (bufferp (car gnus-article-mime-handles))
5084       1 ;; single part
5085     (1- (length gnus-article-mime-handles))))
5086
5087 (defun gnus-mm-display-part (handle)
5088   "Display HANDLE and fix MIME button."
5089   (let ((id (get-text-property (point) 'gnus-part))
5090         (point (point))
5091         (inhibit-read-only t))
5092     (forward-line 1)
5093     (prog1
5094         (let ((window (selected-window))
5095               (mail-parse-charset gnus-newsgroup-charset)
5096               (mail-parse-ignored-charsets
5097                (if (gnus-buffer-live-p gnus-summary-buffer)
5098                    (with-current-buffer gnus-summary-buffer
5099                      gnus-newsgroup-ignored-charsets)
5100                  nil)))
5101           (save-excursion
5102             (unwind-protect
5103                 (let ((win (gnus-get-buffer-window (current-buffer) t))
5104                       (beg (point)))
5105                   (when win
5106                     (select-window win))
5107                   (goto-char point)
5108                   (forward-line)
5109                   (if (mm-handle-displayed-p handle)
5110                       ;; This will remove the part.
5111                       (mm-display-part handle)
5112                     (save-restriction
5113                       (narrow-to-region (point)
5114                                         (if (eobp) (point) (1+ (point))))
5115                       (mm-display-part handle)
5116                       ;; We narrow to the part itself and
5117                       ;; then call the treatment functions.
5118                       (goto-char (point-min))
5119                       (forward-line 1)
5120                       (narrow-to-region (point) (point-max))
5121                       (gnus-treat-article
5122                        nil id
5123                        (gnus-article-mime-total-parts)
5124                        (mm-handle-media-type handle)))))
5125               (if (window-live-p window)
5126                   (select-window window)))))
5127       (goto-char point)
5128       (gnus-delete-line)
5129       (gnus-insert-mime-button
5130        handle id (list (mm-handle-displayed-p handle)))
5131       (goto-char point))))
5132
5133 (defun gnus-article-goto-part (n)
5134   "Go to MIME part N."
5135   (gnus-goto-char (text-property-any (point-min) (point-max) 'gnus-part n)))
5136
5137 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
5138   (let ((gnus-tmp-name
5139          (or (mail-content-type-get (mm-handle-type handle) 'name)
5140              (mail-content-type-get (mm-handle-disposition handle) 'filename)
5141              (mail-content-type-get (mm-handle-type handle) 'url)
5142              ""))
5143         (gnus-tmp-type (mm-handle-media-type handle))
5144         (gnus-tmp-description
5145          (mail-decode-encoded-word-string (or (mm-handle-description handle)
5146                                               "")))
5147         (gnus-tmp-dots
5148          (if (if displayed (car displayed)
5149                (mm-handle-displayed-p handle))
5150              "" "..."))
5151         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
5152                            (buffer-size)))
5153         gnus-tmp-type-long b e)
5154     (when (string-match ".*/" gnus-tmp-name)
5155       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
5156     (setq gnus-tmp-type-long (concat gnus-tmp-type
5157                                      (and (not (equal gnus-tmp-name ""))
5158                                           (concat "; " gnus-tmp-name))))
5159     (unless (equal gnus-tmp-description "")
5160       (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
5161     (unless (bolp)
5162       (insert "\n"))
5163     (setq b (point))
5164     (gnus-eval-format
5165      gnus-mime-button-line-format gnus-mime-button-line-format-alist
5166      `(keymap ,gnus-mime-button-map
5167               gnus-callback gnus-mm-display-part
5168               gnus-part ,gnus-tmp-id
5169               article-type annotation
5170               gnus-data ,handle))
5171     (setq e (if (bolp)
5172                 ;; Exclude a newline.
5173                 (1- (point))
5174               (point)))
5175     (widget-convert-button
5176      'link b e
5177      :mime-handle handle
5178      :action 'gnus-widget-press-button
5179      :button-keymap gnus-mime-button-map
5180      :help-echo
5181      (lambda (widget/window &optional overlay pos)
5182        ;; Needed to properly clear the message due to a bug in
5183        ;; wid-edit (XEmacs only).
5184        (if (boundp 'help-echo-owns-message)
5185            (setq help-echo-owns-message t))
5186        (format
5187         "%S: %s the MIME part; %S: more options"
5188         (aref gnus-mouse-2 0)
5189         ;; XEmacs will get a single widget arg; Emacs 21 will get
5190         ;; window, overlay, position.
5191         (if (mm-handle-displayed-p
5192              (if overlay
5193                  (with-current-buffer (gnus-overlay-buffer overlay)
5194                    (widget-get (widget-at (gnus-overlay-start overlay))
5195                                :mime-handle))
5196                (widget-get widget/window :mime-handle)))
5197             "hide" "show")
5198         (aref gnus-down-mouse-3 0))))))
5199
5200 (defun gnus-widget-press-button (elems el)
5201   (goto-char (widget-get elems :from))
5202   (gnus-article-press-button))
5203
5204 (defvar gnus-displaying-mime nil)
5205
5206 (defun gnus-display-mime (&optional ihandles)
5207   "Display the MIME parts."
5208   (save-excursion
5209     (save-selected-window
5210       (let ((window (get-buffer-window gnus-article-buffer))
5211             (point (point)))
5212         (when window
5213           (select-window window)
5214           ;; We have to do this since selecting the window
5215           ;; may change the point.  So we set the window point.
5216           (set-window-point window point)))
5217       (let ((handles ihandles)
5218             (inhibit-read-only t)
5219             handle)
5220         (cond (handles)
5221               ((setq handles (mm-dissect-buffer nil gnus-article-loose-mime))
5222                (when gnus-article-emulate-mime
5223                  (mm-uu-dissect-text-parts handles)))
5224               (gnus-article-emulate-mime
5225                (setq handles (mm-uu-dissect))))
5226         (when (and (not ihandles)
5227                    (not gnus-displaying-mime))
5228           ;; Top-level call; we clean up.
5229           (when gnus-article-mime-handles
5230             (mm-destroy-parts gnus-article-mime-handles)
5231             (setq gnus-article-mime-handle-alist nil));; A trick.
5232           (setq gnus-article-mime-handles handles)
5233           ;; We allow users to glean info from the handles.
5234           (when gnus-article-mime-part-function
5235             (gnus-mime-part-function handles)))
5236         (if (and handles
5237                  (or (not (stringp (car handles)))
5238                      (cdr handles)))
5239             (progn
5240               (when (and (not ihandles)
5241                          (not gnus-displaying-mime))
5242                 ;; Clean up for mime parts.
5243                 (article-goto-body)
5244                 (delete-region (point) (point-max)))
5245               (let ((gnus-displaying-mime t))
5246                 (gnus-mime-display-part handles)))
5247           (save-restriction
5248             (article-goto-body)
5249             (narrow-to-region (point) (point-max))
5250             (gnus-treat-article nil 1 1)
5251             (widen)))
5252         (unless ihandles
5253           ;; Highlight the headers.
5254           (save-excursion
5255             (save-restriction
5256               (article-goto-body)
5257               (narrow-to-region (point-min) (point))
5258               (gnus-article-save-original-date
5259                (gnus-treat-article 'head)))))))))
5260
5261 (defcustom gnus-mime-display-multipart-as-mixed nil
5262   "Display \"multipart\" parts as  \"multipart/mixed\".
5263
5264 If t, it overrides nil values of
5265 `gnus-mime-display-multipart-alternative-as-mixed' and
5266 `gnus-mime-display-multipart-related-as-mixed'."
5267   :group 'gnus-article-mime
5268   :type 'boolean)
5269
5270 (defcustom gnus-mime-display-multipart-alternative-as-mixed nil
5271   "Display \"multipart/alternative\" parts as  \"multipart/mixed\"."
5272   :version "22.1"
5273   :group 'gnus-article-mime
5274   :type 'boolean)
5275
5276 (defcustom gnus-mime-display-multipart-related-as-mixed nil
5277   "Display \"multipart/related\" parts as  \"multipart/mixed\".
5278
5279 If displaying \"text/html\" is discouraged \(see
5280 `mm-discouraged-alternatives'\) images or other material inside a
5281 \"multipart/related\" part might be overlooked when this variable is nil."
5282   :version "22.1"
5283   :group 'gnus-article-mime
5284   :type 'boolean)
5285
5286 (defun gnus-mime-display-part (handle)
5287   (cond
5288    ;; Maybe a broken MIME message.
5289    ((null handle))
5290    ;; Single part.
5291    ((not (stringp (car handle)))
5292     (gnus-mime-display-single handle))
5293    ;; User-defined multipart
5294    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
5295     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
5296              handle))
5297    ;; multipart/alternative
5298    ((and (equal (car handle) "multipart/alternative")
5299          (not (or gnus-mime-display-multipart-as-mixed
5300                   gnus-mime-display-multipart-alternative-as-mixed)))
5301     (let ((id (1+ (length gnus-article-mime-handle-alist))))
5302       (push (cons id handle) gnus-article-mime-handle-alist)
5303       (gnus-mime-display-alternative (cdr handle) nil nil id)))
5304    ;; multipart/related
5305    ((and (equal (car handle) "multipart/related")
5306          (not (or gnus-mime-display-multipart-as-mixed
5307                   gnus-mime-display-multipart-related-as-mixed)))
5308     ;;;!!!We should find the start part, but we just default
5309     ;;;!!!to the first part.
5310     ;;(gnus-mime-display-part (cadr handle))
5311     ;;;!!! Most multipart/related is an HTML message plus images.
5312     ;;;!!! Unfortunately we are unable to let W3 display those
5313     ;;;!!! included images, so we just display it as a mixed multipart.
5314     ;;(gnus-mime-display-mixed (cdr handle))
5315     ;;;!!! No, w3 can display everything just fine.
5316     (gnus-mime-display-part (cadr handle)))
5317    ((equal (car handle) "multipart/signed")
5318     (gnus-add-wash-type 'signed)
5319     (gnus-mime-display-security handle))
5320    ((equal (car handle) "multipart/encrypted")
5321     (gnus-add-wash-type 'encrypted)
5322     (gnus-mime-display-security handle))
5323    ;; Other multiparts are handled like multipart/mixed.
5324    (t
5325     (gnus-mime-display-mixed (cdr handle)))))
5326
5327 (defun gnus-mime-part-function (handles)
5328   (if (stringp (car handles))
5329       (mapcar 'gnus-mime-part-function (cdr handles))
5330     (funcall gnus-article-mime-part-function handles)))
5331
5332 (defun gnus-mime-display-mixed (handles)
5333   (mapcar 'gnus-mime-display-part handles))
5334
5335 (defun gnus-mime-display-single (handle)
5336   (let ((type (mm-handle-media-type handle))
5337         (ignored gnus-ignored-mime-types)
5338         (not-attachment t)
5339         (move nil)
5340         display text)
5341     (catch 'ignored
5342       (progn
5343         (while ignored
5344           (when (string-match (pop ignored) type)
5345             (throw 'ignored nil)))
5346         (if (and (setq not-attachment
5347                        (and (not (mm-inline-override-p handle))
5348                             (or (not (mm-handle-disposition handle))
5349                                 (equal (car (mm-handle-disposition handle))
5350                                        "inline")
5351                                 (mm-attachment-override-p handle))))
5352                  (mm-automatic-display-p handle)
5353                  (or (and
5354                       (mm-inlinable-p handle)
5355                       (mm-inlined-p handle))
5356                      (mm-automatic-external-display-p type)))
5357             (setq display t)
5358           (when (equal (mm-handle-media-supertype handle) "text")
5359             (setq text t)))
5360         (let ((id (1+ (length gnus-article-mime-handle-alist)))
5361               beg)
5362           (push (cons id handle) gnus-article-mime-handle-alist)
5363           (when (or (not display)
5364                     (not (gnus-unbuttonized-mime-type-p type)))
5365             (gnus-insert-mime-button
5366              handle id (list (or display (and not-attachment text))))
5367             (gnus-article-insert-newline)
5368             ;; Remember modify the number of forward lines.
5369             (setq move t))
5370           (setq beg (point))
5371           (cond
5372            (display
5373             (when move
5374               (forward-line -1)
5375               (setq beg (point)))
5376             (let ((mail-parse-charset gnus-newsgroup-charset)
5377                   (mail-parse-ignored-charsets
5378                    (save-excursion (condition-case ()
5379                                        (set-buffer gnus-summary-buffer)
5380                                      (error))
5381                                    gnus-newsgroup-ignored-charsets)))
5382               (mm-display-part handle t))
5383             (goto-char (point-max)))
5384            ((and text not-attachment)
5385             (when move
5386               (forward-line -1)
5387               (setq beg (point)))
5388             (gnus-article-insert-newline)
5389             (mm-insert-inline
5390              handle
5391              (let ((charset (mail-content-type-get (mm-handle-type handle)
5392                                                    'charset)))
5393                (cond ((not charset)
5394                       (mm-string-as-multibyte (mm-get-part handle)))
5395                      ((eq charset 'gnus-decoded)
5396                       (with-current-buffer (mm-handle-buffer handle)
5397                         (buffer-string)))
5398                      (t
5399                       (mm-decode-string (mm-get-part handle) charset)))))
5400             (goto-char (point-max))))
5401           ;; Do highlighting.
5402           (save-excursion
5403             (save-restriction
5404               (narrow-to-region beg (point))
5405               (gnus-treat-article
5406                nil id
5407                (gnus-article-mime-total-parts)
5408                (mm-handle-media-type handle)))))))))
5409
5410 (defun gnus-unbuttonized-mime-type-p (type)
5411   "Say whether TYPE is to be unbuttonized."
5412   (unless gnus-inhibit-mime-unbuttonizing
5413     (when (catch 'found
5414             (let ((types gnus-unbuttonized-mime-types))
5415               (while types
5416                 (when (string-match (pop types) type)
5417                   (throw 'found t)))))
5418       (not (catch 'found
5419              (let ((types gnus-buttonized-mime-types))
5420                (while types
5421                  (when (string-match (pop types) type)
5422                    (throw 'found t)))))))))
5423
5424 (defun gnus-article-insert-newline ()
5425   "Insert a newline, but mark it as undeletable."
5426   (gnus-put-text-property
5427    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
5428
5429 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
5430   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
5431          (ihandles handles)
5432          (point (point))
5433          handle (inhibit-read-only t) from props begend not-pref)
5434     (save-window-excursion
5435       (save-restriction
5436         (when ibegend
5437           (narrow-to-region (car ibegend)
5438                             (or (cdr ibegend)
5439                                 (progn
5440                                   (goto-char (car ibegend))
5441                                   (forward-line 2)
5442                                   (point))))
5443           (delete-region (point-min) (point-max))
5444           (mm-remove-parts handles))
5445         (setq begend (list (point-marker)))
5446         ;; Do the toggle.
5447         (unless (setq not-pref (cadr (member preferred ihandles)))
5448           (setq not-pref (car ihandles)))
5449         (when (or ibegend
5450                   (not preferred)
5451                   (not (gnus-unbuttonized-mime-type-p
5452                         "multipart/alternative")))
5453           (gnus-add-text-properties
5454            (setq from (point))
5455            (progn
5456              (insert (format "%d.  " id))
5457              (point))
5458            `(gnus-callback
5459              (lambda (handles)
5460                (unless ,(not ibegend)
5461                  (setq gnus-article-mime-handle-alist
5462                        ',gnus-article-mime-handle-alist))
5463                (gnus-mime-display-alternative
5464                 ',ihandles ',not-pref ',begend ,id))
5465              keymap ,gnus-mime-button-map
5466              ,gnus-mouse-face-prop ,gnus-article-mouse-face
5467              face ,gnus-article-button-face
5468              gnus-part ,id
5469              article-type multipart))
5470           (widget-convert-button 'link from (point)
5471                                  :action 'gnus-widget-press-button
5472                                  :button-keymap gnus-widget-button-keymap)
5473           ;; Do the handles
5474           (while (setq handle (pop handles))
5475             (gnus-add-text-properties
5476              (setq from (point))
5477              (progn
5478                (insert (format "(%c) %-18s"
5479                                (if (equal handle preferred) ?* ? )
5480                                (mm-handle-media-type handle)))
5481                (point))
5482              `(gnus-callback
5483                (lambda (handles)
5484                  (unless ,(not ibegend)
5485                    (setq gnus-article-mime-handle-alist
5486                          ',gnus-article-mime-handle-alist))
5487                  (gnus-mime-display-alternative
5488                   ',ihandles ',handle ',begend ,id))
5489                keymap ,gnus-mime-button-map
5490                ,gnus-mouse-face-prop ,gnus-article-mouse-face
5491                face ,gnus-article-button-face
5492                gnus-part ,id
5493                gnus-data ,handle))
5494             (widget-convert-button 'link from (point)
5495                                    :action 'gnus-widget-press-button
5496                                    :button-keymap gnus-widget-button-keymap)
5497             (insert "  "))
5498           (insert "\n\n"))
5499         (when preferred
5500           (if (stringp (car preferred))
5501               (gnus-display-mime preferred)
5502             (let ((mail-parse-charset gnus-newsgroup-charset)
5503                   (mail-parse-ignored-charsets
5504                    (with-current-buffer gnus-summary-buffer
5505                      gnus-newsgroup-ignored-charsets)))
5506               (mm-display-part preferred)
5507               ;; Do highlighting.
5508               (save-excursion
5509                 (save-restriction
5510                   (narrow-to-region (car begend) (point-max))
5511                   (gnus-treat-article
5512                    nil (length gnus-article-mime-handle-alist)
5513                    (gnus-article-mime-total-parts)
5514                    (mm-handle-media-type handle))))))
5515           (goto-char (point-max))
5516           (setcdr begend (point-marker)))))
5517     (when ibegend
5518       (goto-char point))))
5519
5520 (defconst gnus-article-wash-status-strings
5521   (let ((alist '((cite "c" "Possible hidden citation text"
5522                        " " "All citation text visible")
5523                  (headers "h" "Hidden headers"
5524                           " " "All headers visible.")
5525                  (pgp "p" "Encrypted or signed message status hidden"
5526                       " " "No hidden encryption nor digital signature status")
5527                  (signature "s" "Signature has been hidden"
5528                             " " "Signature is visible")
5529                  (overstrike "o" "Overstrike (^H) characters applied"
5530                              " " "No overstrike characters applied")
5531                  (gnus-show-mime "m" "Mime processing is activated"
5532                                  " " "Mime processing is not activated")
5533                  (emphasis "e" "/*_Emphasis_*/ characters applied"
5534                            " " "No /*_emphasis_*/ characters applied")))
5535         result)
5536     (dolist (entry alist result)
5537       (let ((key (nth 0 entry))
5538             (on (copy-sequence (nth 1 entry)))
5539             (on-help (nth 2 entry))
5540             (off (copy-sequence (nth 3 entry)))
5541             (off-help (nth 4 entry)))
5542         (put-text-property 0 1 'help-echo on-help on)
5543         (put-text-property 0 1 'help-echo off-help off)
5544         (push (list key on off) result))))
5545   "Alist of strings describing wash status in the mode line.
5546 Each entry has the form (KEY ON OF), where the KEY is a symbol
5547 representing the particular washing function, ON is the string to use
5548 in the article mode line when the washing function is active, and OFF
5549 is the string to use when it is inactive.")
5550
5551 (defun gnus-article-wash-status-entry (key value)
5552   (let ((entry (assoc key gnus-article-wash-status-strings)))
5553     (if value (nth 1 entry) (nth 2 entry))))
5554
5555 (defun gnus-article-wash-status ()
5556   "Return a string which display status of article washing."
5557   (with-current-buffer gnus-article-buffer
5558     (let ((cite (memq 'cite gnus-article-wash-types))
5559           (headers (memq 'headers gnus-article-wash-types))
5560           (boring (memq 'boring-headers gnus-article-wash-types))
5561           (pgp (memq 'pgp gnus-article-wash-types))
5562           (pem (memq 'pem gnus-article-wash-types))
5563           (signed (memq 'signed gnus-article-wash-types))
5564           (encrypted (memq 'encrypted gnus-article-wash-types))
5565           (signature (memq 'signature gnus-article-wash-types))
5566           (overstrike (memq 'overstrike gnus-article-wash-types))
5567           (emphasis (memq 'emphasis gnus-article-wash-types)))
5568       (concat
5569        (gnus-article-wash-status-entry 'cite cite)
5570        (gnus-article-wash-status-entry 'headers (or headers boring))
5571        (gnus-article-wash-status-entry 'pgp (or pgp pem signed encrypted))
5572        (gnus-article-wash-status-entry 'signature signature)
5573        (gnus-article-wash-status-entry 'overstrike overstrike)
5574        (gnus-article-wash-status-entry 'gnus-show-mime gnus-show-mime)
5575        (gnus-article-wash-status-entry 'emphasis emphasis)))))
5576
5577 (defun gnus-add-wash-type (type)
5578   "Add a washing of TYPE to the current status."
5579   (add-to-list 'gnus-article-wash-types type))
5580
5581 (defun gnus-delete-wash-type (type)
5582   "Add a washing of TYPE to the current status."
5583   (setq gnus-article-wash-types (delq type gnus-article-wash-types)))
5584
5585 (defun gnus-add-image (category image)
5586   "Add IMAGE of CATEGORY to the list of displayed images."
5587   (let ((entry (assq category gnus-article-image-alist)))
5588     (unless entry
5589       (setq entry (list category))
5590       (push entry gnus-article-image-alist))
5591     (nconc entry (list image))))
5592
5593 (defun gnus-delete-images (category)
5594   "Delete all images in CATEGORY."
5595   (let ((entry (assq category gnus-article-image-alist)))
5596     (dolist (image (cdr entry))
5597       (gnus-remove-image image category))
5598     (setq gnus-article-image-alist (delq entry gnus-article-image-alist))
5599     (gnus-delete-wash-type category)))
5600
5601 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
5602
5603 (defun gnus-article-maybe-hide-headers ()
5604   "Hide unwanted headers if `gnus-have-all-headers' is nil.
5605 Provided for backwards compatibility."
5606   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
5607                  (not (with-current-buffer gnus-summary-buffer
5608                         gnus-have-all-headers)))
5609              (not gnus-inhibit-hiding))
5610     (gnus-article-hide-headers)))
5611
5612 ;;; Article savers.
5613
5614 (defun gnus-output-to-file (file-name)
5615   "Append the current article to a file named FILE-NAME."
5616   (let ((artbuf (current-buffer)))
5617     (with-temp-buffer
5618       (insert-buffer-substring artbuf)
5619       ;; Append newline at end of the buffer as separator, and then
5620       ;; save it to file.
5621       (goto-char (point-max))
5622       (insert "\n")
5623       (let ((file-name-coding-system nnmail-pathname-coding-system))
5624         (write-region-as-binary (point-min) (point-max) file-name 'append))
5625       t)))
5626
5627 (defun gnus-narrow-to-page (&optional arg)
5628   "Narrow the article buffer to a page.
5629 If given a numerical ARG, move forward ARG pages."
5630   (interactive "P")
5631   (setq arg (if arg (prefix-numeric-value arg) 0))
5632   (save-excursion
5633     (set-buffer gnus-article-buffer)
5634     (goto-char (point-min))
5635     (widen)
5636     ;; Remove any old next/prev buttons.
5637     (when (gnus-visual-p 'page-marker)
5638       (let ((inhibit-read-only t))
5639         (gnus-remove-text-with-property 'gnus-prev)
5640         (gnus-remove-text-with-property 'gnus-next)))
5641     (if
5642         (cond ((< arg 0)
5643                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
5644               ((> arg 0)
5645                (re-search-forward page-delimiter nil 'move arg)))
5646         (goto-char (match-end 0))
5647       (save-excursion
5648         (goto-char (point-min))
5649         (setq gnus-page-broken
5650               (and (re-search-forward page-delimiter nil t) t))))
5651     (when gnus-page-broken
5652       (narrow-to-region
5653        (point)
5654        (if (re-search-forward page-delimiter nil 'move)
5655            (match-beginning 0)
5656          (point)))
5657       (when (and (gnus-visual-p 'page-marker)
5658                  (> (point-min) (save-restriction (widen) (point-min))))
5659         (save-excursion
5660           (goto-char (point-min))
5661           (gnus-insert-prev-page-button)))
5662       (when (and (gnus-visual-p 'page-marker)
5663                  (< (point-max) (save-restriction (widen) (point-max))))
5664         (save-excursion
5665           (goto-char (point-max))
5666           (gnus-insert-next-page-button))))))
5667
5668 ;; Article mode commands
5669
5670 (defun gnus-article-goto-next-page ()
5671   "Show the next page of the article."
5672   (interactive)
5673   (when (gnus-article-next-page)
5674     (goto-char (point-min))
5675     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
5676
5677
5678 (defun gnus-article-goto-prev-page ()
5679   "Show the previous page of the article."
5680   (interactive)
5681   (if (bobp)
5682       (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
5683     (gnus-article-prev-page nil)))
5684
5685 ;; This is cleaner but currently breaks `gnus-pick-mode':
5686 ;;
5687 ;; (defun gnus-article-goto-next-page ()
5688 ;;   "Show the next page of the article."
5689 ;;   (interactive)
5690 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
5691 ;;     (gnus-summary-next-page)))
5692 ;;
5693 ;; (defun gnus-article-goto-prev-page ()
5694 ;;   "Show the next page of the article."
5695 ;;   (interactive)
5696 ;;   (gnus-eval-in-buffer-window gnus-summary-buffer
5697 ;;     (gnus-summary-prev-page)))
5698
5699 (defun gnus-article-next-page (&optional lines)
5700   "Show the next page of the current article.
5701 If end of article, return non-nil.  Otherwise return nil.
5702 Argument LINES specifies lines to be scrolled up."
5703   (interactive "p")
5704   (move-to-window-line -1)
5705   (if (save-excursion
5706         (end-of-line)
5707         (and (pos-visible-in-window-p)  ;Not continuation line.
5708              (>= (1+ (point)) (point-max)))) ;Allow for trailing newline.
5709       ;; Nothing in this page.
5710       (if (or (not gnus-page-broken)
5711               (save-excursion
5712                 (save-restriction
5713                   (widen)
5714                   (forward-line)
5715                   (eobp)))) ;Real end-of-buffer?
5716           (progn
5717             (when gnus-article-over-scroll
5718               (gnus-article-next-page-1 lines))
5719             t)                  ;Nothing more.
5720         (gnus-narrow-to-page 1)         ;Go to next page.
5721         nil)
5722     ;; More in this page.
5723     (gnus-article-next-page-1 lines)
5724     nil))
5725
5726 (defmacro gnus-article-beginning-of-window ()
5727   "Move point to the beginning of the window.
5728 In Emacs, the point is placed at the line number which `scroll-margin'
5729 specifies."
5730   (if (featurep 'xemacs)
5731       '(move-to-window-line 0)
5732     '(move-to-window-line
5733       (min (max 0 scroll-margin)
5734            (max 1 (- (window-height)
5735                      (if mode-line-format 1 0)
5736                      (if header-line-format 1 0)))))))
5737
5738 (defun gnus-article-next-page-1 (lines)
5739   (when (and (not (featurep 'xemacs))
5740              (numberp lines)
5741              (> lines 0)
5742              (numberp (symbol-value 'scroll-margin))
5743              (> (symbol-value 'scroll-margin) 0))
5744     ;; Protect against the bug that Emacs 21.x hangs up when scrolling up for
5745     ;; too many number of lines if `scroll-margin' is set as two or greater.
5746     (setq lines (min lines
5747                      (max 0 (- (count-lines (window-start) (point-max))
5748                                (symbol-value 'scroll-margin))))))
5749   (condition-case ()
5750       (let ((scroll-in-place nil))
5751         (scroll-up lines))
5752     (end-of-buffer
5753      ;; Long lines may cause an end-of-buffer error.
5754      (goto-char (point-max))))
5755   (gnus-article-beginning-of-window))
5756
5757 (defun gnus-article-prev-page (&optional lines)
5758   "Show previous page of current article.
5759 Argument LINES specifies lines to be scrolled down."
5760   (interactive "p")
5761   (move-to-window-line 0)
5762   (if (and gnus-page-broken
5763            (bobp)
5764            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
5765       (progn
5766         (gnus-narrow-to-page -1)        ;Go to previous page.
5767         (goto-char (point-max))
5768         (recenter -1))
5769     (prog1
5770         (condition-case ()
5771             (let ((scroll-in-place nil))
5772               (scroll-down lines))
5773           (beginning-of-buffer
5774            (goto-char (point-min))))
5775       (gnus-article-beginning-of-window))))
5776
5777 (defun gnus-article-only-boring-p ()
5778   "Decide whether there is only boring text remaining in the article.
5779 Something \"interesting\" is a word of at least two letters that does
5780 not have a face in `gnus-article-boring-faces'."
5781   (when (and gnus-article-skip-boring
5782              (boundp 'gnus-article-boring-faces)
5783              (symbol-value 'gnus-article-boring-faces))
5784     (save-excursion
5785       (catch 'only-boring
5786         (while (re-search-forward "\\b\\w\\w" nil t)
5787           (forward-char -1)
5788           (when (not (gnus-intersection
5789                       (gnus-faces-at (point))
5790                       (symbol-value 'gnus-article-boring-faces)))
5791             (throw 'only-boring nil)))
5792         (throw 'only-boring t)))))
5793
5794 (defun gnus-article-refer-article ()
5795   "Read article specified by message-id around point."
5796   (interactive)
5797   (save-excursion
5798     (re-search-backward "[ \t]\\|^" (point-at-bol) t)
5799     (re-search-forward "<?news:<?\\|<" (point-at-eol) t)
5800     (if (re-search-forward "[^@ ]+@[^ \t>]+" (point-at-eol) t)
5801         (let ((msg-id (concat "<" (match-string 0) ">")))
5802           (set-buffer gnus-summary-buffer)
5803           (gnus-summary-refer-article msg-id))
5804       (error "No references around point"))))
5805
5806 (defun gnus-article-show-summary ()
5807   "Reconfigure windows to show summary buffer."
5808   (interactive)
5809   (if (not (gnus-buffer-live-p gnus-summary-buffer))
5810       (error "There is no summary buffer for this article buffer")
5811     (gnus-article-set-globals)
5812     (gnus-configure-windows 'article)
5813     (gnus-summary-goto-subject gnus-current-article)
5814     (gnus-summary-position-point)))
5815
5816 (defun gnus-article-describe-briefly ()
5817   "Describe article mode commands briefly."
5818   (interactive)
5819   (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")))
5820
5821 (defun gnus-article-summary-command ()
5822   "Execute the last keystroke in the summary buffer."
5823   (interactive)
5824   (let ((obuf (current-buffer))
5825         (owin (current-window-configuration))
5826         func)
5827     (switch-to-buffer gnus-article-current-summary 'norecord)
5828     (setq func (lookup-key (current-local-map) (this-command-keys)))
5829     (call-interactively func)
5830     (set-buffer obuf)
5831     (set-window-configuration owin)
5832     (set-window-point (get-buffer-window (current-buffer)) (point))))
5833
5834 (defun gnus-article-summary-command-nosave ()
5835   "Execute the last keystroke in the summary buffer."
5836   (interactive)
5837   (let (func)
5838     (pop-to-buffer gnus-article-current-summary 'norecord)
5839     (setq func (lookup-key (current-local-map) (this-command-keys)))
5840     (call-interactively func)))
5841
5842 (defun gnus-article-check-buffer ()
5843   "Beep if not in an article buffer."
5844   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
5845     (error "Command invoked outside of a Gnus article buffer")))
5846
5847 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
5848   "Read a summary buffer key sequence and execute it from the article buffer."
5849   (interactive "P")
5850   (gnus-article-check-buffer)
5851   (let ((nosaves
5852          '("q" "Q"  "c" "r" "\C-c\C-f" "m"  "a" "f"
5853            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
5854            "=" "^" "\M-^" "|"))
5855         (nosave-but-article
5856          '("A\r"))
5857         (nosave-in-article
5858          '("\C-d"))
5859         (up-to-top
5860          '("n" "Gn" "p" "Gp"))
5861         keys new-sum-point)
5862     (save-excursion
5863       (set-buffer gnus-article-current-summary)
5864       (let (gnus-pick-mode)
5865         (push (or key last-command-event) unread-command-events)
5866         (setq keys (static-if (featurep 'xemacs)
5867                        (events-to-keys (read-key-sequence nil))
5868                      (read-key-sequence nil)))))
5869     (message "")
5870
5871     (if (or (member keys nosaves)
5872             (member keys nosave-but-article)
5873             (member keys nosave-in-article))
5874         (let (func)
5875           (save-window-excursion
5876             (pop-to-buffer gnus-article-current-summary 'norecord)
5877             ;; We disable the pick minor mode commands.
5878             (let (gnus-pick-mode)
5879               (setq func (lookup-key (current-local-map) keys))))
5880           (if (or (not func)
5881                   (numberp func))
5882               (ding)
5883             (unless (member keys nosave-in-article)
5884               (set-buffer gnus-article-current-summary))
5885             (call-interactively func)
5886             (setq new-sum-point (point)))
5887           (when (member keys nosave-but-article)
5888             (pop-to-buffer gnus-article-buffer 'norecord)))
5889       ;; These commands should restore window configuration.
5890       (let ((obuf (current-buffer))
5891             (owin (current-window-configuration))
5892             (opoint (point))
5893             win func in-buffer selected new-sum-start new-sum-hscroll)
5894         (cond (not-restore-window
5895                (pop-to-buffer gnus-article-current-summary 'norecord))
5896               ((setq win (get-buffer-window gnus-article-current-summary))
5897                (select-window win))
5898               (t
5899                (switch-to-buffer gnus-article-current-summary 'norecord)))
5900         (setq in-buffer (current-buffer))
5901         ;; We disable the pick minor mode commands.
5902         (if (and (setq func (let (gnus-pick-mode)
5903                               (lookup-key (current-local-map) keys)))
5904                  (functionp func))
5905             (progn
5906               (call-interactively func)
5907               (when (eq win (selected-window))
5908                 (setq new-sum-point (point)
5909                       new-sum-start (window-start win)
5910                       new-sum-hscroll (window-hscroll win)))
5911               (when (eq in-buffer (current-buffer))
5912                 (setq selected (gnus-summary-select-article))
5913                 (set-buffer obuf)
5914                 (unless not-restore-window
5915                   (set-window-configuration owin))
5916                 (when (eq selected 'old)
5917                   (article-goto-body)
5918                   (set-window-start (get-buffer-window (current-buffer))
5919                                     1)
5920                   (set-window-point (get-buffer-window (current-buffer))
5921                                     (point)))
5922                 (when (and (not not-restore-window)
5923                            new-sum-point)
5924                   (set-window-point win new-sum-point)
5925                   (set-window-start win new-sum-start)
5926                   (set-window-hscroll win new-sum-hscroll))))
5927           (set-window-configuration owin)
5928           (ding))))))
5929
5930 (defun gnus-article-describe-key (key)
5931   "Display documentation of the function invoked by KEY.  KEY is a string."
5932   (interactive "kDescribe key: ")
5933   (gnus-article-check-buffer)
5934   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5935       (save-excursion
5936         (set-buffer gnus-article-current-summary)
5937         (let (gnus-pick-mode)
5938           (if (featurep 'xemacs)
5939               (progn
5940                 (push (elt key 0) unread-command-events)
5941                 (setq key (events-to-keys
5942                            (read-key-sequence "Describe key: "))))
5943             (setq unread-command-events
5944                   (mapcar
5945                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5946                    key))
5947             (setq key (read-key-sequence "Describe key: "))))
5948         (describe-key key))
5949     (describe-key key)))
5950
5951 (defun gnus-article-describe-key-briefly (key &optional insert)
5952   "Display documentation of the function invoked by KEY.  KEY is a string."
5953   (interactive "kDescribe key: \nP")
5954   (gnus-article-check-buffer)
5955   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
5956       (save-excursion
5957         (set-buffer gnus-article-current-summary)
5958         (let (gnus-pick-mode)
5959           (if (featurep 'xemacs)
5960               (progn
5961                 (push (elt key 0) unread-command-events)
5962                 (setq key (events-to-keys
5963                            (read-key-sequence "Describe key: "))))
5964             (setq unread-command-events
5965                   (mapcar
5966                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
5967                    key))
5968             (setq key (read-key-sequence "Describe key: "))))
5969         (describe-key-briefly key insert))
5970     (describe-key-briefly key insert)))
5971
5972 (defun gnus-article-reply-with-original (&optional wide)
5973   "Start composing a reply mail to the current message.
5974 The text in the region will be yanked.  If the region isn't active,
5975 the entire article will be yanked."
5976   (interactive "P")
5977   (let ((article (cdr gnus-article-current))
5978         contents)
5979     (if (not (gnus-region-active-p))
5980         (with-current-buffer gnus-summary-buffer
5981           (gnus-summary-reply (list (list article)) wide))
5982       (setq contents (buffer-substring (point) (mark t)))
5983       ;; Deactivate active regions.
5984       (when (and (boundp 'transient-mark-mode)
5985                  transient-mark-mode)
5986         (setq mark-active nil))
5987       (with-current-buffer gnus-summary-buffer
5988         (gnus-summary-reply
5989          (list (list article contents)) wide)))))
5990
5991 (defun gnus-article-followup-with-original ()
5992   "Compose a followup to the current article.
5993 The text in the region will be yanked.  If the region isn't active,
5994 the entire article will be yanked."
5995   (interactive)
5996   (let ((article (cdr gnus-article-current))
5997         contents)
5998       (if (not (gnus-region-active-p))
5999           (with-current-buffer gnus-summary-buffer
6000             (gnus-summary-followup (list (list article))))
6001         (setq contents (buffer-substring (point) (mark t)))
6002         ;; Deactivate active regions.
6003         (when (and (boundp 'transient-mark-mode)
6004                    transient-mark-mode)
6005           (setq mark-active nil))
6006         (with-current-buffer gnus-summary-buffer
6007           (gnus-summary-followup
6008            (list (list article contents)))))))
6009
6010 (defun gnus-article-hide (&optional arg force)
6011   "Hide all the gruft in the current article.
6012 This means that signatures, cited text and (some) headers will be
6013 hidden.
6014 If given a prefix, show the hidden text instead."
6015   (interactive (append (gnus-article-hidden-arg) (list 'force)))
6016   (gnus-article-hide-headers arg)
6017   (gnus-article-hide-list-identifiers arg)
6018   (gnus-article-hide-citation-maybe arg force)
6019   (gnus-article-hide-signature arg))
6020
6021 (defun gnus-article-maybe-highlight ()
6022   "Do some article highlighting if article highlighting is requested."
6023   (when (gnus-visual-p 'article-highlight 'highlight)
6024     (gnus-article-highlight-some)))
6025
6026 (defun gnus-check-group-server ()
6027   ;; Make sure the connection to the server is alive.
6028   (unless (gnus-server-opened
6029            (gnus-find-method-for-group gnus-newsgroup-name))
6030     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
6031     (gnus-request-group gnus-newsgroup-name t)))
6032
6033 (eval-when-compile
6034   (autoload 'nneething-get-file-name "nneething"))
6035
6036 (defun gnus-request-article-this-buffer (article group)
6037   "Get an article and insert it into this buffer."
6038   (let (do-update-line sparse-header)
6039     (prog1
6040         (save-excursion
6041           (erase-buffer)
6042           (gnus-kill-all-overlays)
6043           (setq group (or group gnus-newsgroup-name))
6044
6045           ;; Using `gnus-request-article' directly will insert the article into
6046           ;; `nntp-server-buffer' - so we'll save some time by not having to
6047           ;; copy it from the server buffer into the article buffer.
6048
6049           ;; We only request an article by message-id when we do not have the
6050           ;; headers for it, so we'll have to get those.
6051           (when (stringp article)
6052             (gnus-read-header article))
6053
6054           ;; If the article number is negative, that means that this article
6055           ;; doesn't belong in this newsgroup (possibly), so we find its
6056           ;; message-id and request it by id instead of number.
6057           (when (and (numberp article)
6058                      gnus-summary-buffer
6059                      (get-buffer gnus-summary-buffer)
6060                      (gnus-buffer-exists-p gnus-summary-buffer))
6061             (save-excursion
6062               (set-buffer gnus-summary-buffer)
6063               (let ((header (gnus-summary-article-header article)))
6064                 (when (< article 0)
6065                   (cond
6066                    ((memq article gnus-newsgroup-sparse)
6067                     ;; This is a sparse gap article.
6068                     (setq do-update-line article)
6069                     (setq article (mail-header-id header))
6070                     (setq sparse-header (gnus-read-header article))
6071                     (setq gnus-newsgroup-sparse
6072                           (delq article gnus-newsgroup-sparse)))
6073                    ((vectorp header)
6074                     ;; It's a real article.
6075                     (setq article (mail-header-id header)))
6076                    (t
6077                     ;; It is an extracted pseudo-article.
6078                     (setq article 'pseudo)
6079                     (gnus-request-pseudo-article header))))
6080
6081                 (let ((method (gnus-find-method-for-group
6082                                gnus-newsgroup-name)))
6083                   (when (and (eq (car method) 'nneething)
6084                              (vectorp header))
6085                     (let ((dir (nneething-get-file-name
6086                                 (mail-header-id header))))
6087                       (when (and (stringp dir)
6088                                  (file-directory-p dir))
6089                         (setq article 'nneething)
6090                         (gnus-group-enter-directory dir))))))))
6091
6092           (cond
6093            ;; Refuse to select canceled articles.
6094            ((and (numberp article)
6095                  gnus-summary-buffer
6096                  (get-buffer gnus-summary-buffer)
6097                  (gnus-buffer-exists-p gnus-summary-buffer)
6098                  (eq (cdr (with-current-buffer gnus-summary-buffer
6099                             (assq article gnus-newsgroup-reads)))
6100                      gnus-canceled-mark))
6101             nil)
6102            ;; We first check `gnus-original-article-buffer'.
6103            ((and (get-buffer gnus-original-article-buffer)
6104                  (numberp article)
6105                  (with-current-buffer gnus-original-article-buffer
6106                    (and (equal (car gnus-original-article) group)
6107                         (eq (cdr gnus-original-article) article))))
6108             (insert-buffer-substring gnus-original-article-buffer)
6109             'article)
6110            ;; Check the backlog.
6111            ((and gnus-keep-backlog
6112                  (gnus-backlog-request-article group article (current-buffer)))
6113             'article)
6114            ;; Check asynchronous pre-fetch.
6115            ((gnus-async-request-fetched-article group article (current-buffer))
6116             (gnus-async-prefetch-next group article gnus-summary-buffer)
6117             (when (and (numberp article) gnus-keep-backlog)
6118               (gnus-backlog-enter-article group article (current-buffer)))
6119             'article)
6120            ;; Check the cache.
6121            ((and gnus-use-cache
6122                  (numberp article)
6123                  (gnus-cache-request-article article group))
6124             'article)
6125            ;; Check the agent cache.
6126            ((gnus-agent-request-article article group)
6127             'article)
6128            ;; Get the article and put into the article buffer.
6129            ((or (stringp article)
6130                 (numberp article))
6131             (let ((gnus-override-method gnus-override-method)
6132                   (methods (and (stringp article)
6133                                 gnus-refer-article-method))
6134                   (backend (car (gnus-find-method-for-group
6135                                  gnus-newsgroup-name)))
6136                   result
6137                   (inhibit-read-only t))
6138               (if (or (not (listp methods))
6139                       (and (symbolp (car methods))
6140                            (assq (car methods) nnoo-definition-alist)))
6141                   (setq methods (list methods)))
6142               (when (and (null gnus-override-method)
6143                          methods)
6144                 (setq gnus-override-method (pop methods)))
6145               (while (not result)
6146                 (when (eq gnus-override-method 'current)
6147                   (setq gnus-override-method
6148                         (with-current-buffer gnus-summary-buffer
6149                           gnus-current-select-method)))
6150                 (erase-buffer)
6151                 (gnus-kill-all-overlays)
6152                 (let ((gnus-newsgroup-name group))
6153                   (gnus-check-group-server))
6154                 (cond
6155                  ((gnus-request-article article group (current-buffer))
6156                   (when (numberp article)
6157                     (gnus-async-prefetch-next group article
6158                                               gnus-summary-buffer)
6159                     (when gnus-keep-backlog
6160                       (gnus-backlog-enter-article
6161                        group article (current-buffer))))
6162                   (setq result 'article))
6163                  (methods
6164                   (setq gnus-override-method (pop methods)))
6165                  ((not (string-match "^400 "
6166                                      (nnheader-get-report backend)))
6167                   ;; If we get 400 server disconnect, reconnect and
6168                   ;; retry; otherwise, assume the article has expired.
6169                   (setq result 'done))))
6170               (and (eq result 'article) 'article)))
6171            ;; It was a pseudo.
6172            (t article)))
6173
6174       ;; Associate this article with the current summary buffer.
6175       (setq gnus-article-current-summary gnus-summary-buffer)
6176
6177       ;; Take the article from the original article buffer
6178       ;; and place it in the buffer it's supposed to be in.
6179       (when (and (get-buffer gnus-article-buffer)
6180                  (equal (buffer-name (current-buffer))
6181                         (buffer-name (get-buffer gnus-article-buffer))))
6182         (save-excursion
6183           (if (get-buffer gnus-original-article-buffer)
6184               (set-buffer gnus-original-article-buffer)
6185             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
6186             (buffer-disable-undo)
6187             (setq major-mode 'gnus-original-article-mode)
6188             (setq buffer-read-only t))
6189           (let ((inhibit-read-only t))
6190             (erase-buffer)
6191             (insert-buffer-substring gnus-article-buffer))
6192           (setq gnus-original-article (cons group article)))
6193
6194         ;; Decode charsets.
6195         (run-hooks 'gnus-article-decode-hook)
6196         ;; Mark article as decoded or not.
6197         (setq gnus-article-decoded-p gnus-article-decode-hook))
6198
6199       ;; Update sparse articles.
6200       (when (and do-update-line
6201                  (or (numberp article)
6202                      (stringp article)))
6203         (let ((buf (current-buffer)))
6204           (set-buffer gnus-summary-buffer)
6205           (gnus-summary-update-article do-update-line sparse-header)
6206           (gnus-summary-goto-subject do-update-line nil t)
6207           (set-window-point (gnus-get-buffer-window (current-buffer) t)
6208                             (point))
6209           (set-buffer buf))))))
6210
6211 ;;;
6212 ;;; Article editing
6213 ;;;
6214
6215 (defcustom gnus-article-edit-mode-hook nil
6216   "Hook run in article edit mode buffers."
6217   :group 'gnus-article-various
6218   :type 'hook)
6219
6220 (defcustom gnus-article-edit-article-setup-function
6221   'gnus-article-mime-edit-article-setup
6222   "Function called to setup an editing article buffer."
6223   :group 'gnus-article-various
6224   :type 'function)
6225
6226 (defvar gnus-article-edit-done-function nil)
6227
6228 (defvar gnus-article-edit-mode-map nil)
6229
6230 ;; Should we be using derived.el for this?
6231 (unless gnus-article-edit-mode-map
6232   (setq gnus-article-edit-mode-map (make-keymap))
6233   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
6234
6235   (gnus-define-keys gnus-article-edit-mode-map
6236     "\C-c?"    describe-mode
6237     "\C-c\C-c" gnus-article-edit-done
6238     "\C-c\C-k" gnus-article-edit-exit
6239     "\C-c\C-f\C-t" message-goto-to
6240     "\C-c\C-f\C-o" message-goto-from
6241     "\C-c\C-f\C-b" message-goto-bcc
6242     ;;"\C-c\C-f\C-w" message-goto-fcc
6243     "\C-c\C-f\C-c" message-goto-cc
6244     "\C-c\C-f\C-s" message-goto-subject
6245     "\C-c\C-f\C-r" message-goto-reply-to
6246     "\C-c\C-f\C-n" message-goto-newsgroups
6247     "\C-c\C-f\C-d" message-goto-distribution
6248     "\C-c\C-f\C-f" message-goto-followup-to
6249     "\C-c\C-f\C-m" message-goto-mail-followup-to
6250     "\C-c\C-f\C-k" message-goto-keywords
6251     "\C-c\C-f\C-u" message-goto-summary
6252     "\C-c\C-f\C-i" message-insert-or-toggle-importance
6253     "\C-c\C-f\C-a" message-generate-unsubscribed-mail-followup-to
6254     "\C-c\C-b" message-goto-body
6255     "\C-c\C-i" message-goto-signature
6256
6257     "\C-c\C-t" message-insert-to
6258     "\C-c\C-n" message-insert-newsgroups
6259     "\C-c\C-o" message-sort-headers
6260     "\C-c\C-e" message-elide-region
6261     "\C-c\C-v" message-delete-not-region
6262     "\C-c\C-z" message-kill-to-signature
6263     "\M-\r" message-newline-and-reformat
6264     "\C-c\C-a" mml-attach-file
6265     "\C-a" message-beginning-of-line
6266     "\t" message-tab
6267     "\M-;" comment-region)
6268
6269   (gnus-define-keys (gnus-article-edit-wash-map
6270                      "\C-c\C-w" gnus-article-edit-mode-map)
6271     "f" gnus-article-edit-full-stops))
6272
6273 (easy-menu-define
6274   gnus-article-edit-mode-field-menu gnus-article-edit-mode-map ""
6275   '("Field"
6276     ["Fetch To" message-insert-to t]
6277     ["Fetch Newsgroups" message-insert-newsgroups t]
6278     "----"
6279     ["To" message-goto-to t]
6280     ["From" message-goto-from t]
6281     ["Subject" message-goto-subject t]
6282     ["Cc" message-goto-cc t]
6283     ["Reply-To" message-goto-reply-to t]
6284     ["Summary" message-goto-summary t]
6285     ["Keywords" message-goto-keywords t]
6286     ["Newsgroups" message-goto-newsgroups t]
6287     ["Followup-To" message-goto-followup-to t]
6288     ["Mail-Followup-To" message-goto-mail-followup-to t]
6289     ["Distribution" message-goto-distribution t]
6290     ["Body" message-goto-body t]
6291     ["Signature" message-goto-signature t]))
6292
6293 (define-derived-mode gnus-article-edit-mode message-mode "Article Edit"
6294   "Major mode for editing articles.
6295 This is an extended text-mode.
6296
6297 \\{gnus-article-edit-mode-map}"
6298   (make-local-variable 'gnus-article-edit-done-function)
6299   (make-local-variable 'gnus-prev-winconf)
6300   (set (make-local-variable 'font-lock-defaults)
6301        '(message-font-lock-keywords t))
6302   (set (make-local-variable 'mail-header-separator) "")
6303   (set (make-local-variable 'gnus-article-edit-mode) t)
6304   (easy-menu-add message-mode-field-menu message-mode-map)
6305   (setq buffer-read-only nil)
6306   (buffer-enable-undo)
6307   (widen))
6308
6309 (defun gnus-article-edit (&optional force)
6310   "Edit the current article.
6311 This will have permanent effect only in mail groups.
6312 If FORCE is non-nil, allow editing of articles even in read-only
6313 groups."
6314   (interactive "P")
6315   (when (and (not force)
6316              (gnus-group-read-only-p))
6317     (error "The current newsgroup does not support article editing"))
6318   (gnus-article-date-original)
6319   (gnus-article-edit-article
6320    'ignore
6321    `(lambda (no-highlight)
6322       'ignore
6323       (gnus-summary-edit-article-done
6324        ,(or (mail-header-references gnus-current-headers) "")
6325        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
6326
6327 (defun gnus-article-edit-article (start-func exit-func &optional quiet)
6328   "Start editing the contents of the current article buffer."
6329   (let ((winconf (current-window-configuration)))
6330     (set-buffer gnus-article-buffer)
6331     (let ((message-auto-save-directory
6332            ;; Don't associate the article buffer with a draft file.
6333            nil))
6334       (gnus-article-edit-mode))
6335     (funcall start-func)
6336     (set-buffer-modified-p nil)
6337     (gnus-configure-windows 'edit-article)
6338     (setq gnus-article-edit-done-function exit-func)
6339     (setq gnus-prev-winconf winconf)
6340     (when gnus-article-edit-article-setup-function
6341       (funcall gnus-article-edit-article-setup-function))
6342     (unless quiet
6343       (gnus-message 6 "C-c C-c to end edits; C-c C-k to exit"))))
6344
6345 (defun gnus-article-edit-done (&optional arg)
6346   "Update the article edits and exit."
6347   (interactive "P")
6348   (let ((func gnus-article-edit-done-function)
6349         (buf (current-buffer))
6350         (start (window-start))
6351         (p (point))
6352         (winconf gnus-prev-winconf))
6353     (remove-hook 'gnus-article-mode-hook
6354                  'gnus-article-mime-edit-article-unwind)
6355     (widen) ;; Widen it in case that users narrowed the buffer.
6356     (funcall func arg)
6357     (set-buffer buf)
6358     ;; The cache and backlog have to be flushed somewhat.
6359     (when gnus-keep-backlog
6360       (gnus-backlog-remove-article
6361        (car gnus-article-current) (cdr gnus-article-current)))
6362     ;; Flush original article as well.
6363     (save-excursion
6364       (when (get-buffer gnus-original-article-buffer)
6365         (set-buffer gnus-original-article-buffer)
6366         (setq gnus-original-article nil)))
6367     (when gnus-use-cache
6368       (gnus-cache-update-article
6369        (car gnus-article-current) (cdr gnus-article-current)))
6370     ;; We remove all text props from the article buffer.
6371     (kill-all-local-variables)
6372     (set-text-properties (point-min) (point-max) nil)
6373     (gnus-article-mode)
6374     (set-window-configuration winconf)
6375     (set-buffer buf)
6376     (set-window-start (get-buffer-window buf) start)
6377     (set-window-point (get-buffer-window buf) (point)))
6378   (gnus-summary-show-article))
6379
6380 (defun gnus-article-edit-exit ()
6381   "Exit the article editing without updating."
6382   (interactive)
6383   (when (or (not (buffer-modified-p))
6384             (yes-or-no-p "Article modified; kill anyway? "))
6385     (let ((curbuf (current-buffer))
6386           (p (point))
6387           (window-start (window-start)))
6388       (erase-buffer)
6389       (if (gnus-buffer-live-p gnus-original-article-buffer)
6390           (insert-buffer-substring gnus-original-article-buffer))
6391       (let ((winconf gnus-prev-winconf))
6392         (kill-all-local-variables)
6393         (gnus-article-mode)
6394         (set-window-configuration winconf)
6395         ;; Tippy-toe some to make sure that point remains where it was.
6396         (save-current-buffer
6397           (set-buffer curbuf)
6398           (set-window-start (get-buffer-window (current-buffer)) window-start)
6399           (goto-char p))))
6400     (gnus-summary-show-article)))
6401
6402 (defun gnus-article-edit-full-stops ()
6403   "Interactively repair spacing at end of sentences."
6404   (interactive)
6405   (save-excursion
6406     (goto-char (point-min))
6407     (search-forward-regexp "^$" nil t)
6408     (let ((case-fold-search nil))
6409       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
6410
6411 ;;;
6412 ;;; Article editing with MIME-Edit
6413 ;;;
6414
6415 (defcustom gnus-article-mime-edit-article-setup-hook nil
6416   "Hook run after setting up a MIME editing article buffer."
6417   :group 'gnus-article-various
6418   :type 'hook)
6419
6420 (defun gnus-article-mime-edit-article-unwind ()
6421   "Unwind `gnus-article-buffer' if article editing was given up."
6422   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
6423   (when (featurep 'font-lock)
6424     (setq font-lock-defaults nil)
6425     (font-lock-mode -1))
6426   (when mime-edit-mode-flag
6427     (mime-edit-exit 'nomime 'no-error)
6428     (message "")))
6429
6430 (defun gnus-article-mime-edit-article-setup ()
6431   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
6432 after replacing with the original article."
6433   (setq gnus-show-mime t)
6434   (setq gnus-article-edit-done-function
6435         `(lambda (&rest args)
6436            (when mime-edit-mode-flag
6437              (let (mime-edit-insert-user-agent-field)
6438                (mime-edit-exit))
6439              (message ""))
6440            (goto-char (point-min))
6441            (let (case-fold-search)
6442              (when (re-search-forward
6443                     (format "^%s$" (regexp-quote mail-header-separator))
6444                     nil t)
6445                (replace-match "")))
6446            (apply ,gnus-article-edit-done-function args)
6447            (insert
6448             (prog1
6449                 (buffer-substring-no-properties (point-min) (point-max))
6450               (set-buffer (get-buffer-create gnus-original-article-buffer))
6451               (erase-buffer)))
6452            (setq gnus-current-headers (gnus-article-make-full-mail-header))
6453            (set-buffer gnus-article-buffer)
6454            (gnus-article-prepare-display)))
6455   (substitute-key-definition 'gnus-article-edit-done
6456                              'gnus-article-mime-edit-done
6457                              gnus-article-edit-mode-map)
6458   (substitute-key-definition 'gnus-article-edit-exit
6459                              'gnus-article-mime-edit-exit
6460                              gnus-article-edit-mode-map)
6461   (erase-buffer)
6462   (insert-buffer-substring gnus-original-article-buffer)
6463   (unless (member (with-current-buffer gnus-summary-buffer
6464                     gnus-newsgroup-name)
6465                   '("nndraft:delayed" "nndraft:drafts"))
6466     (let ((ofn (symbol-function 'mime-edit-decode-single-part-in-buffer)))
6467       (fset 'mime-edit-decode-single-part-in-buffer
6468             (lambda (&rest args)
6469               (if (let ((content-type (car args)))
6470                     (and (eq 'message (mime-content-type-primary-type
6471                                        content-type))
6472                          (eq 'rfc822 (mime-content-type-subtype
6473                                       content-type))))
6474                   (setcar (cdr args) 'not-decode-text))
6475               (apply ofn args)))
6476       (unwind-protect
6477           (mime-edit-again)
6478         (fset 'mime-edit-decode-single-part-in-buffer ofn))))
6479   (when (featurep 'font-lock)
6480     (set (make-local-variable 'font-lock-defaults)
6481          '(message-font-lock-keywords t))
6482     (font-lock-set-defaults)
6483     (turn-on-font-lock))
6484   (set-buffer-modified-p nil)
6485   (delete-other-windows)
6486   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
6487   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
6488
6489 (defun gnus-article-mime-edit-done (&optional arg)
6490   "Update the article MIME edits and exit."
6491   (interactive "P")
6492   (when (featurep 'font-lock)
6493     (setq font-lock-defaults nil)
6494     (font-lock-mode -1))
6495   (let ((inhibit-read-only t))
6496     (gnus-article-edit-done arg)))
6497
6498 (defun gnus-article-mime-edit-exit ()
6499   "Exit the article MIME editing without updating."
6500   (interactive)
6501   (when (or (not (buffer-modified-p))
6502             (yes-or-no-p "Article modified; kill anyway? "))
6503     (when (featurep 'font-lock)
6504       (setq font-lock-defaults nil)
6505       (font-lock-mode -1))
6506     (when mime-edit-mode-flag
6507       (let (mime-edit-insert-user-agent-field)
6508         (mime-edit-exit))
6509       (message ""))
6510     (goto-char (point-min))
6511     (let (case-fold-search)
6512       (when (re-search-forward
6513              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
6514         (replace-match "")))
6515     (let ((winconf gnus-prev-winconf))
6516       (insert (prog1
6517                   (buffer-substring-no-properties (point-min) (point-max))
6518                 (set-buffer (get-buffer-create gnus-original-article-buffer))
6519                 (erase-buffer)))
6520       (setq gnus-current-headers (gnus-article-make-full-mail-header))
6521       (set-buffer gnus-article-buffer)
6522       (gnus-article-prepare-display)
6523       (set-window-configuration winconf))))
6524
6525 ;;;
6526 ;;; Article highlights
6527 ;;;
6528
6529 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
6530
6531 ;;; Internal Variables:
6532
6533 (defcustom gnus-button-url-regexp
6534   (if (string-match "[[:digit:]]" "1") ;; support POSIX?
6535       "\\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:]]\\)"
6536     "\\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\\)\\)")
6537   "Regular expression that matches URLs."
6538   :group 'gnus-article-buttons
6539   :type 'regexp)
6540
6541 (defcustom gnus-button-valid-fqdn-regexp
6542   message-valid-fqdn-regexp
6543   "Regular expression that matches a valid FQDN."
6544   :version "22.1"
6545   :group 'gnus-article-buttons
6546   :type 'regexp)
6547
6548 ;; Regexp suggested by Felix Wiemann in <87oeuomcz9.fsf@news2.ososo.de>
6549 (defcustom gnus-button-valid-localpart-regexp
6550   "[a-z0-9$%(*-=?[_][^<>\")!;:,{}\n\t ]*"
6551   "Regular expression that matches a localpart of mail addresses or MIDs."
6552   :version "22.1"
6553   :group 'gnus-article-buttons
6554   :type 'regexp)
6555
6556 (defcustom gnus-button-man-handler 'manual-entry
6557   "Function to use for displaying man pages.
6558 The function must take at least one argument with a string naming the
6559 man page."
6560   :version "22.1"
6561   :type '(choice (function-item :tag "Man" manual-entry)
6562                  (function-item :tag "Woman" woman)
6563                  (function :tag "Other"))
6564   :group 'gnus-article-buttons)
6565
6566 (defcustom gnus-ctan-url "http://tug.ctan.org/tex-archive/"
6567   "Top directory of a CTAN \(Comprehensive TeX Archive Network\) archive.
6568 If the default site is too slow, try to find a CTAN mirror, see
6569 <URL:http://tug.ctan.org/tex-archive/CTAN.sites?action=/index.html>.  See also
6570 the variable `gnus-button-handle-ctan'."
6571   :version "22.1"
6572   :group 'gnus-article-buttons
6573   :link '(custom-manual "(gnus)Group Parameters")
6574   :type '(choice (const "http://www.tex.ac.uk/tex-archive/")
6575                  (const "http://tug.ctan.org/tex-archive/")
6576                  (const "http://www.dante.de/CTAN/")
6577                  (string :tag "Other")))
6578
6579 (defcustom gnus-button-ctan-handler 'browse-url
6580   "Function to use for displaying CTAN links.
6581 The function must take one argument, the string naming the URL."
6582   :version "22.1"
6583   :type '(choice (function-item :tag "Browse Url" browse-url)
6584                  (function :tag "Other"))
6585   :group 'gnus-article-buttons)
6586
6587 (defcustom gnus-button-handle-ctan-bogus-regexp "^/?tex-archive/\\|^/"
6588   "Bogus strings removed from CTAN URLs."
6589   :version "22.1"
6590   :group 'gnus-article-buttons
6591   :type '(choice (const "^/?tex-archive/\\|/")
6592                  (regexp :tag "Other")))
6593
6594 (defcustom gnus-button-ctan-directory-regexp
6595   (regexp-opt
6596    (list "archive-tools" "biblio" "bibliography" "digests" "documentation"
6597          "dviware" "fonts" "graphics" "help" "indexing" "info" "language"
6598          "languages" "macros" "nonfree" "obsolete" "support" "systems"
6599          "tds" "tools" "usergrps" "web") t)
6600   "Regular expression for ctan directories.
6601 It should match all directories in the top level of `gnus-ctan-url'."
6602   :version "22.1"
6603   :group 'gnus-article-buttons
6604   :type 'regexp)
6605
6606 (defcustom gnus-button-mid-or-mail-regexp
6607   (concat "\\b\\(<?" gnus-button-valid-localpart-regexp "@"
6608           gnus-button-valid-fqdn-regexp
6609           ">?\\)\\b")
6610   "Regular expression that matches a message ID or a mail address."
6611   :version "22.1"
6612   :group 'gnus-article-buttons
6613   :type 'regexp)
6614
6615 (defcustom gnus-button-prefer-mid-or-mail 'gnus-button-mid-or-mail-heuristic
6616   "What to do when the button on a string as \"foo123@bar.invalid\" is pushed.
6617 Strings like this can be either a message ID or a mail address.  If it is one
6618 of the symbols `mid' or `mail', Gnus will always assume that the string is a
6619 message ID or a mail address, respectively.  If this variable is set to the
6620 symbol `ask', always query the user what do do.  If it is a function, this
6621 function will be called with the string as it's only argument.  The function
6622 must return `mid', `mail', `invalid' or `ask'."
6623   :version "22.1"
6624   :group 'gnus-article-buttons
6625   :type '(choice (function-item :tag "Heuristic function"
6626                                 gnus-button-mid-or-mail-heuristic)
6627                  (const ask)
6628                  (const mid)
6629                  (const mail)))
6630
6631 (defcustom gnus-button-mid-or-mail-heuristic-alist
6632   '((-10.0 . ".+\\$.+@")
6633     (-10.0 . "#")
6634     (-10.0 . "\\*")
6635     (-5.0  . "\\+[^+]*\\+.*@") ;; # two plus signs
6636     (-5.0  . "@[Nn][Ee][Ww][Ss]") ;; /\@news/i
6637     (-5.0  . "@.*[Dd][Ii][Aa][Ll][Uu][Pp]") ;; /\@.*dialup/i;
6638     (-1.0  . "^[^a-z]+@")
6639     ;;
6640     (-5.0  . "\\.[0-9][0-9]+.*@") ;; "\.[0-9]{2,}.*\@"
6641     (-5.0  . "[a-z].*[A-Z].*[a-z].*[A-Z].*@") ;; "([a-z].*[A-Z].*){2,}\@"
6642     (-3.0  . "[A-Z][A-Z][a-z][a-z].*@")
6643     (-5.0  . "\\...?.?@") ;; (-5.0 . "\..{1,3}\@")
6644     ;;
6645     (-2.0  . "^[0-9]")
6646     (-1.0  . "^[0-9][0-9]")
6647     ;;
6648     ;; -3.0 /^[0-9][0-9a-fA-F]{2,2}/;
6649     (-3.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
6650     ;; -5.0 /^[0-9][0-9a-fA-F]{3,3}/;
6651     (-5.0  . "^[0-9][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][^0-9a-fA-F]")
6652     ;;
6653     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][^0-9].*@") ;; "[0-9]{5,}.*\@"
6654     (-3.0  .  "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][^0-9].*@")
6655     ;;       "[0-9]{8,}.*\@"
6656     (-3.0
6657      . "[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9].*@")
6658     ;; "[0-9]{12,}.*\@"
6659     ;; compensation for TDMA dated mail addresses:
6660     (25.0  . "-dated-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]+.*@")
6661     ;;
6662     (-20.0 . "\\.fsf@") ;; Gnus
6663     (-20.0 . "^slrn")
6664     (-20.0 . "^Pine")
6665     (-20.0 . "_-_") ;; Subject change in thread
6666     ;;
6667     (-20.0 . "\\.ln@") ;; leafnode
6668     (-30.0 . "@ID-[0-9]+\\.[a-zA-Z]+\\.dfncis\\.de")
6669     (-30.0 . "@4[Aa][Xx]\\.com") ;; Forte Agent
6670     ;;
6671     ;; (5.0 . "") ;; $local_part_len <= 7
6672     (10.0  . "^[^0-9]+@")
6673     (3.0   . "^[^0-9]+[0-9][0-9]?[0-9]?@")
6674     ;;      ^[^0-9]+[0-9]{1,3}\@ digits only at end of local part
6675     (3.0   . "\@stud")
6676     ;;
6677     (2.0   . "[a-z][a-z][._-][A-Z][a-z].*@")
6678     ;;
6679     (0.5   . "^[A-Z][a-z]")
6680     (0.5   . "^[A-Z][a-z][a-z]")
6681     (1.5   . "^[A-Z][a-z][A-Z][a-z][^a-z]") ;; ^[A-Z][a-z]{3,3}
6682     (2.0   . "^[A-Z][a-z][A-Z][a-z][a-z][^a-z]")) ;; ^[A-Z][a-z]{4,4}
6683   "An alist of \(RATE . REGEXP\) pairs for `gnus-button-mid-or-mail-heuristic'.
6684
6685 A negative RATE indicates a message IDs, whereas a positive indicates a mail
6686 address.  The REGEXP is processed with `case-fold-search' set to nil."
6687   :version "22.1"
6688   :group 'gnus-article-buttons
6689   :type '(repeat (cons (number :tag "Rate")
6690                        (regexp :tag "Regexp"))))
6691
6692 (defun gnus-button-mid-or-mail-heuristic (mid-or-mail)
6693   "Guess whether MID-OR-MAIL is a message ID or a mail address.
6694 Returns `mid' if MID-OR-MAIL is a message IDs, `mail' if it's a mail
6695 address, `ask' if unsure and `invalid' if the string is invalid."
6696   (let ((case-fold-search nil)
6697         (list gnus-button-mid-or-mail-heuristic-alist)
6698         (result 0) rate regexp lpartlen elem)
6699     (setq lpartlen
6700           (length (gnus-replace-in-string mid-or-mail "^\\(.*\\)@.*$" "\\1")))
6701     (gnus-message 8 "`%s', length of local part=`%s'." mid-or-mail lpartlen)
6702     ;; Certain special cases...
6703     (when (string-match
6704            (concat
6705             "^0[0-9]+-[0-9][0-9][0-9][0-9]@t-online\\.de$\\|"
6706             "^[0-9]+\\.[0-9]+@compuserve\\|"
6707             "@public\\.gmane\\.org")
6708            mid-or-mail)
6709       (gnus-message 8 "`%s' is a known mail address." mid-or-mail)
6710       (setq result 'mail))
6711     (when (string-match "@.*@\\| " mid-or-mail)
6712       (gnus-message 8 "`%s' is invalid." mid-or-mail)
6713       (setq result 'invalid))
6714     ;; Nothing more to do, if result is not a number here...
6715     (when (numberp result)
6716       (while list
6717         (setq elem (car list)
6718               rate (car elem)
6719               regexp (cdr elem)
6720               list (cdr list))
6721         (when (string-match regexp mid-or-mail)
6722           (setq result (+ result rate))
6723           (gnus-message
6724            9 "`%s' matched `%s', rate `%s', result `%s'."
6725            mid-or-mail regexp rate result)))
6726       (when (<= lpartlen 7)
6727         (setq result (+ result 5.0))
6728         (gnus-message 9 "`%s' matched (<= lpartlen 7), result `%s'."
6729                       mid-or-mail result))
6730       (when (>= lpartlen 12)
6731         (gnus-message 9 "`%s' matched (>= lpartlen 12)" mid-or-mail)
6732         (cond
6733          ((string-match "[0-9][^0-9]+[0-9].*@" mid-or-mail)
6734           ;; Long local part should contain realname if e-mail address,
6735           ;; too many digits: message-id.
6736           ;; $score -= 5.0 + 0.1 * $local_part_len;
6737           (setq rate (* -1.0 (+ 5.0 (* 0.1 lpartlen))))
6738           (setq result (+ result rate))
6739           (gnus-message
6740            9 "Many digits in `%s', rate `%s', result `%s'."
6741            mid-or-mail rate result))
6742          ((string-match "[^aeiouy][^aeiouy][^aeiouy][^aeiouy]+.*\@"
6743                         mid-or-mail)
6744           ;; Too few vowels [^aeiouy]{4,}.*\@
6745           (setq result (+ result -5.0))
6746           (gnus-message
6747            9 "Few vowels in `%s', rate `%s', result `%s'."
6748            mid-or-mail -5.0 result))
6749          (t
6750           (setq result (+ result 5.0))
6751           (gnus-message
6752            9 "`%s', rate `%s', result `%s'." mid-or-mail 5.0 result)))))
6753     (gnus-message 8 "`%s': Final rate is `%s'." mid-or-mail result)
6754     ;; Maybe we should make this a customizable alist: (condition . 'result)
6755     (cond
6756      ((symbolp result) result)
6757      ;; Now convert number into proper results:
6758      ((< result -10.0) 'mid)
6759      ((> result  10.0) 'mail)
6760      (t 'ask))))
6761
6762 (defun gnus-button-handle-mid-or-mail (mid-or-mail)
6763   (let* ((pref gnus-button-prefer-mid-or-mail) guessed
6764          (url-mid (concat "news" ":" mid-or-mail))
6765          (url-mailto (concat "mailto" ":" mid-or-mail)))
6766     (gnus-message 9 "mid-or-mail=%s" mid-or-mail)
6767     (when (fboundp pref)
6768       (setq guessed
6769             ;; get rid of surrounding angles...
6770             (funcall pref
6771                      (gnus-replace-in-string mid-or-mail "^<\\|>$" "")))
6772       (if (or (eq 'mid guessed) (eq 'mail guessed))
6773           (setq pref guessed)
6774         (setq pref 'ask)))
6775     (if (eq pref 'ask)
6776         (save-window-excursion
6777           (if (y-or-n-p (concat "Is <" mid-or-mail "> a mail address? "))
6778               (setq pref 'mail)
6779             (setq pref 'mid))))
6780     (cond ((eq pref 'mid)
6781            (gnus-message 8 "calling `gnus-button-handle-news' %s" url-mid)
6782            (gnus-button-handle-news url-mid))
6783           ((eq pref 'mail)
6784            (gnus-message 8 "calling `gnus-url-mailto'  %s" url-mailto)
6785            (gnus-url-mailto url-mailto))
6786           (t (gnus-message 3 "Invalid string.")))))
6787
6788 (defun gnus-button-handle-custom (fun arg)
6789   "Call function FUN on argument ARG.
6790 Both FUN and ARG are supposed to be strings.  ARG will be passed
6791 as a symbol to FUN."
6792   (funcall (intern fun) (intern arg)))
6793
6794 (defvar gnus-button-handle-describe-prefix "^\\(C-h\\|<?[Ff]1>?\\)")
6795
6796 ;; FIXME: Maybe we should merge some of the functions that do quite similar
6797 ;; stuff?
6798
6799 (defun gnus-button-handle-describe-function (url)
6800   "Call `describe-function' when pushing the corresponding URL button."
6801   (describe-function
6802    (intern
6803     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6804
6805 (defun gnus-button-handle-describe-variable (url)
6806   "Call `describe-variable' when pushing the corresponding URL button."
6807   (describe-variable
6808    (intern
6809     (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))))
6810
6811 (defun gnus-button-handle-symbol (url)
6812 "Display help on variable or function.
6813 Calls `describe-variable' or `describe-function'."
6814   (let ((sym (intern url)))
6815     (cond
6816      ((fboundp sym) (describe-function sym))
6817      ((boundp sym) (describe-variable sym))
6818      (t (gnus-message 3 "`%s' is not a known function of variable." url)))))
6819
6820 (defun gnus-button-handle-describe-key (url)
6821   "Call `describe-key' when pushing the corresponding URL button."
6822   (let* ((key-string
6823           (gnus-replace-in-string url gnus-button-handle-describe-prefix ""))
6824          (keys (ignore-errors (eval `(kbd ,key-string)))))
6825     (if keys
6826         (describe-key keys)
6827       (gnus-message 3 "Invalid key sequence in button: %s" key-string))))
6828
6829 (defun gnus-button-handle-apropos (url)
6830   "Call `apropos' when pushing the corresponding URL button."
6831   (apropos (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6832
6833 (defun gnus-button-handle-apropos-command (url)
6834   "Call `apropos' when pushing the corresponding URL button."
6835   (apropos-command
6836    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6837
6838 (defun gnus-button-handle-apropos-variable (url)
6839   "Call `apropos' when pushing the corresponding URL button."
6840   (funcall
6841    (if (fboundp 'apropos-variable) 'apropos-variable 'apropos)
6842    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6843
6844 (defun gnus-button-handle-apropos-documentation (url)
6845   "Call `apropos' when pushing the corresponding URL button."
6846   (funcall
6847    (if (fboundp 'apropos-documentation) 'apropos-documentation 'apropos)
6848    (gnus-replace-in-string url gnus-button-handle-describe-prefix "")))
6849
6850 (defun gnus-button-handle-library (url)
6851   "Call `locate-library' when pushing the corresponding URL button."
6852   (gnus-message 9 "url=`%s'" url)
6853   (let* ((lib (locate-library url))
6854          (file (gnus-replace-in-string (or lib "") "\.elc" ".el")))
6855     (if (not lib)
6856         (gnus-message 1 "Cannot locale library `%s'." url)
6857       (find-file-read-only file))))
6858
6859 (defun gnus-button-handle-ctan (url)
6860   "Call `browse-url' when pushing a CTAN URL button."
6861   (funcall
6862    gnus-button-ctan-handler
6863    (concat
6864     gnus-ctan-url
6865     (gnus-replace-in-string url gnus-button-handle-ctan-bogus-regexp ""))))
6866
6867 (defcustom gnus-button-tex-level 5
6868   "*Integer that says how many TeX-related buttons Gnus will show.
6869 The higher the number, the more buttons will appear and the more false
6870 positives are possible.  Note that you can set this variable local to
6871 specific groups.  Setting it higher in TeX groups is probably a good idea.
6872 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6873 how to set variables in specific groups."
6874   :version "22.1"
6875   :group 'gnus-article-buttons
6876   :link '(custom-manual "(gnus)Group Parameters")
6877   :type 'integer)
6878
6879 (defcustom gnus-button-man-level 5
6880   "*Integer that says how many man-related buttons Gnus will show.
6881 The higher the number, the more buttons will appear and the more false
6882 positives are possible.  Note that you can set this variable local to
6883 specific groups.  Setting it higher in Unix groups is probably a good idea.
6884 See Info node `(gnus)Group Parameters' and the variable `gnus-parameters' on
6885 how to set variables in specific groups."
6886   :version "22.1"
6887   :group 'gnus-article-buttons
6888   :link '(custom-manual "(gnus)Group Parameters")
6889   :type 'integer)
6890
6891 (defcustom gnus-button-emacs-level 5
6892   "*Integer that says how many emacs-related buttons Gnus will show.
6893 The higher the number, the more buttons will appear and the more false
6894 positives are possible.  Note that you can set this variable local to
6895 specific groups.  Setting it higher in Emacs or Gnus related groups is
6896 probably a good idea.  See Info node `(gnus)Group Parameters' and the variable
6897 `gnus-parameters' on how to set variables in specific groups."
6898   :version "22.1"
6899   :group 'gnus-article-buttons
6900   :link '(custom-manual "(gnus)Group Parameters")
6901   :type 'integer)
6902
6903 (defcustom gnus-button-message-level 5
6904   "*Integer that says how many buttons for news or mail messages will appear.
6905 The higher the number, the more buttons will appear and the more false
6906 positives are possible."
6907   ;; mail addresses, MIDs, URLs for news, ...
6908   :version "22.1"
6909   :group 'gnus-article-buttons
6910   :type 'integer)
6911
6912 (defcustom gnus-button-browse-level 5
6913   "*Integer that says how many buttons for browsing will appear.
6914 The higher the number, the more buttons will appear and the more false
6915 positives are possible."
6916   ;; stuff handled by `browse-url' or `gnus-button-embedded-url'
6917   :version "22.1"
6918   :group 'gnus-article-buttons
6919   :type 'integer)
6920
6921 (defcustom gnus-button-alist
6922   '(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
6923      0 (>= gnus-button-message-level 0) gnus-button-handle-news 3)
6924     ((concat "\\b\\(nntp\\|news\\):\\("
6925              gnus-button-valid-localpart-regexp "@[a-z0-9.-]+[a-z]\\)")
6926      0 t gnus-button-handle-news 2)
6927     ("\\(\\b<\\(url:[>\n\t ]*\\)?\\(nntp\\|news\\):[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
6928      1 (>= gnus-button-message-level 0) gnus-button-fetch-group 5)
6929     ("\\b\\(nntp\\|news\\):\\(//\\)?\\([^'\">\n\t ]+\\)"
6930      0 (>= gnus-button-message-level 0) gnus-button-fetch-group 3)
6931     ;; RFC 2392 (Don't allow `/' in domain part --> CID)
6932     ("\\bmid:\\(//\\)?\\([^'\">\n\t ]+@[^'\">\n\t /]+\\)"
6933      0 (>= gnus-button-message-level 0) gnus-button-message-id 2)
6934     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)"
6935      2 (>= gnus-button-message-level 0) gnus-button-message-id 3)
6936     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>"
6937      0 (>= gnus-button-message-level 0) gnus-url-mailto 2)
6938     ;; RFC 2368 (The mailto URL scheme)
6939     ("\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
6940      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6941     ("\\bmailto:\\([^ \n\t]+\\)"
6942      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
6943     ;; CTAN
6944     ((concat "\\bCTAN:[ \t\n]?[^>)!;:,'\n\t ]*\\("
6945              gnus-button-ctan-directory-regexp
6946              "[^][>)!;:,'\n\t ]+\\)")
6947      0 (>= gnus-button-tex-level 1) gnus-button-handle-ctan 1)
6948     ((concat "\\btex-archive/\\("
6949              gnus-button-ctan-directory-regexp
6950              "/[-_.a-z0-9/]+[-_./a-z0-9]+[/a-z0-9]\\)")
6951      1 (>= gnus-button-tex-level 6) gnus-button-handle-ctan 1)
6952     ((concat
6953       "\\b\\("
6954       gnus-button-ctan-directory-regexp
6955       "/[-_.a-z0-9]+/[-_./a-z0-9]+[/a-z0-9]\\)")
6956      1 (>= gnus-button-tex-level 8) gnus-button-handle-ctan 1)
6957     ;; This is info (home-grown style) <info://foo/bar+baz>
6958     ("\\binfo://\\([^'\">\n\t ]+\\)"
6959      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 1)
6960     ;; Info GNOME style <info:foo#bar_baz>
6961     ("\\binfo:\\([^('\n\t\r \"><][^'\n\t\r \"><]*\\)"
6962      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-gnome 1)
6963     ;; Info KDE style <info:(foo)bar baz>
6964     ("<\\(info:\\(([^)]+)[^>\n\r]*\\)\\)>"
6965      1 (>= gnus-button-emacs-level 1) gnus-button-handle-info-url-kde 2)
6966     ("\\((Info-goto-node\\|(info\\)[ \t\n]*\\(\"[^\"]*\"\\))" 0
6967      (>= gnus-button-emacs-level 1) gnus-button-handle-info-url 2)
6968     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+i[ \t\n]+d?[ \t\n]?m[ \t\n]+\\([^ ]+ ?[^ ]+\\)[ \t\n]+RET"
6969      ;; Info links like `C-h i d m CC Mode RET'
6970      0 (>= gnus-button-emacs-level 1) gnus-button-handle-info-keystrokes 2)
6971     ;; This is custom
6972     ("M-x[ \t\n]\\(customize-[^ ]+\\)[ \t\n]RET[ \t\n]\\([^ ]+\\)[ \t\n]RET" 0
6973      (>= gnus-button-emacs-level 1) gnus-button-handle-custom 1 2)
6974     ;; Emacs help commands
6975     ("M-x[ \t\n]+apropos[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6976      ;; regexp doesn't match arguments containing ` '.
6977      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos 1)
6978     ("M-x[ \t\n]+apropos-command[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6979      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-command 1)
6980     ("M-x[ \t\n]+apropos-variable[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6981      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-variable 1)
6982     ("M-x[ \t\n]+apropos-documentation[ \t\n]+RET[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6983      0 (>= gnus-button-emacs-level 1) gnus-button-handle-apropos-documentation 1)
6984     ;; The following entries may lead to many false positives so don't enable
6985     ;; them by default (use a high button level).
6986     ("/\\([a-z][-a-z0-9]+\\.el\\)\\>[^.?]"
6987      ;; Exclude [.?] for URLs in gmane.emacs.cvs
6988      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
6989     ("`\\([a-z][-a-z0-9]+\\.el\\)'"
6990      1 (>= gnus-button-emacs-level 8) gnus-button-handle-library 1)
6991     ("`\\([a-z][a-z0-9]+-[a-z]+-[-a-z]+\\|\\(gnus\\|message\\)-[-a-z]+\\)'"
6992      0 (>= gnus-button-emacs-level 8) gnus-button-handle-symbol 1)
6993     ("`\\([a-z][a-z0-9]+-[a-z]+\\)'"
6994      0 (>= gnus-button-emacs-level 9) gnus-button-handle-symbol 1)
6995     ("(setq[ \t\n]+\\([a-z][a-z0-9]+-[-a-z0-9]+\\)[ \t\n]+.+)"
6996      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-variable 1)
6997     ("\\bM-x[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
6998      1 (>= gnus-button-emacs-level 7) gnus-button-handle-describe-function 1)
6999     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+f[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7000      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-function 2)
7001     ("\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+v[ \t\n]+\\([^ \t\n]+\\)[ \t\n]+RET"
7002      0 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-variable 2)
7003     ("`\\(\\b\\(C-h\\|<?[Ff]1>?\\)[ \t\n]+k[ \t\n]+\\([^']+\\)\\)'"
7004      ;; Unlike the other regexps we really have to require quoting
7005      ;; here to determine where it ends.
7006      1 (>= gnus-button-emacs-level 1) gnus-button-handle-describe-key 3)
7007     ;; This is how URLs _should_ be embedded in text (RFC 1738, RFC 2396)...
7008     ("<URL: *\\([^<>]*\\)>"
7009      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7010     ;; RFC 2396 (2.4.3., delims) ...
7011     ("\"URL: *\\([^\"]*\\)\""
7012      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7013     ;; RFC 2396 (2.4.3., delims) ...
7014     ("\"URL: *\\([^\"]*\\)\""
7015      1 (>= gnus-button-browse-level 0) gnus-button-embedded-url 1)
7016     ;; Raw URLs.
7017     (gnus-button-url-regexp
7018      0 (>= gnus-button-browse-level 0) browse-url 0)
7019     ;; man pages
7020     ("\\b\\([a-z][a-z]+([1-9])\\)\\W"
7021      0 (and (>= gnus-button-man-level 1) (< gnus-button-man-level 3))
7022      gnus-button-handle-man 1)
7023     ;; more man pages: resolv.conf(5), iso_8859-1(7), xterm(1x)
7024     ("\\b\\([a-z][-_.a-z0-9]+([1-9])\\)\\W"
7025      0 (and (>= gnus-button-man-level 3) (< gnus-button-man-level 5))
7026      gnus-button-handle-man 1)
7027     ;; even more: Apache::PerlRun(3pm), PDL::IO::FastRaw(3pm),
7028     ;; SoWWWAnchor(3iv), XSelectInput(3X11), X(1), X(7)
7029     ("\\b\\(\\(?:[a-z][-+_.:a-z0-9]+([1-9][X1a-z]*)\\)\\|\\b\\(?:X([1-9])\\)\\)\\W"
7030      0 (>= gnus-button-man-level 5) gnus-button-handle-man 1)
7031     ;; MID or mail: To avoid too many false positives we don't try to catch
7032     ;; all kind of allowed MIDs or mail addresses.  Domain part must contain
7033     ;; at least one dot.  TLD must contain two or three chars or be a know TLD
7034     ;; (info|name|...).  Put this entry near the _end_ of `gnus-button-alist'
7035     ;; so that non-ambiguous entries (see above) match first.
7036     (gnus-button-mid-or-mail-regexp
7037      0 (>= gnus-button-message-level 5) gnus-button-handle-mid-or-mail 1))
7038   "*Alist of regexps matching buttons in article bodies.
7039
7040 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
7041 REGEXP: is the string (case insensitive) matching text around the button (can
7042 also be Lisp expression evaluating to a string),
7043 BUTTON: is the number of the regexp grouping actually matching the button,
7044 FORM: is a Lisp expression which must eval to true for the button to
7045 be added,
7046 CALLBACK: is the function to call when the user push this button, and each
7047 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
7048
7049 CALLBACK can also be a variable, in that case the value of that
7050 variable it the real callback function."
7051   :group 'gnus-article-buttons
7052   :type '(repeat (list (choice regexp variable sexp)
7053                        (integer :tag "Button")
7054                        (sexp :tag "Form")
7055                        (function :tag "Callback")
7056                        (repeat :tag "Par"
7057                                :inline t
7058                                (integer :tag "Regexp group")))))
7059
7060 (defcustom gnus-header-button-alist
7061   '(("^\\(References\\|Message-I[Dd]\\|^In-Reply-To\\):" "<[^<>]+>"
7062      0 (>= gnus-button-message-level 0) gnus-button-message-id 0)
7063     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$"
7064      1 (>= gnus-button-message-level 0) gnus-button-reply 1)
7065     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
7066      0 (>= gnus-button-message-level 0) gnus-msg-mail 0)
7067     ("^X-[Uu][Rr][Ll]:" gnus-button-url-regexp
7068      0 (>= gnus-button-browse-level 0) browse-url 0)
7069     ("^Subject:" gnus-button-url-regexp
7070      0 (>= gnus-button-browse-level 0) browse-url 0)
7071     ("^[^:]+:" gnus-button-url-regexp
7072      0 (>= gnus-button-browse-level 0) browse-url 0)
7073     ("^OpenPGP:.*url=" gnus-button-url-regexp
7074      0 (>= gnus-button-browse-level 0) gnus-button-openpgp 0)
7075     ("^[^:]+:" "\\bmailto:\\([-a-z.@_+0-9%=?&/]+\\)"
7076      0 (>= gnus-button-message-level 0) gnus-url-mailto 1)
7077     ("^[^:]+:" "\\(<\\(url: \\)?\\(nntp\\|news\\):\\([^>\n ]*\\)>\\)"
7078      1 (>= gnus-button-message-level 0) gnus-button-message-id 4))
7079   "*Alist of headers and regexps to match buttons in article heads.
7080
7081 This alist is very similar to `gnus-button-alist', except that each
7082 alist has an additional HEADER element first in each entry:
7083
7084 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
7085
7086 HEADER is a regexp to match a header.  For a fuller explanation, see
7087 `gnus-button-alist'."
7088   :group 'gnus-article-buttons
7089   :group 'gnus-article-headers
7090   :type '(repeat (list (regexp :tag "Header")
7091                        (choice regexp variable)
7092                        (integer :tag "Button")
7093                        (sexp :tag "Form")
7094                        (function :tag "Callback")
7095                        (repeat :tag "Par"
7096                                :inline t
7097                                (integer :tag "Regexp group")))))
7098
7099 ;;; Commands:
7100
7101 (defun gnus-article-push-button (event)
7102   "Check text under the mouse pointer for a callback function.
7103 If the text under the mouse pointer has a `gnus-callback' property,
7104 call it with the value of the `gnus-data' text property."
7105   (interactive "e")
7106   (set-buffer (window-buffer (posn-window (event-start event))))
7107   (let* ((pos (posn-point (event-start event)))
7108          (data (get-text-property pos 'gnus-data))
7109          (fun (get-text-property pos 'gnus-callback)))
7110     (goto-char pos)
7111     (when fun
7112       (funcall fun data))))
7113
7114 (defun gnus-article-press-button ()
7115   "Check text at point for a callback function.
7116 If the text at point has a `gnus-callback' property,
7117 call it with the value of the `gnus-data' text property."
7118   (interactive)
7119   (let ((data (get-text-property (point) 'gnus-data))
7120         (fun (get-text-property (point) 'gnus-callback)))
7121     (when fun
7122       (funcall fun data))))
7123
7124 (defun gnus-article-prev-button (n)
7125   "Move point to N buttons backward.
7126 If N is negative, move forward instead."
7127   (interactive "p")
7128   (gnus-article-next-button (- n)))
7129
7130 (defun gnus-article-next-button (n)
7131   "Move point to N buttons forward.
7132 If N is negative, move backward instead."
7133   (interactive "p")
7134   (let ((function (if (< n 0) 'previous-single-property-change
7135                     'next-single-property-change))
7136         (inhibit-point-motion-hooks t)
7137         (backward (< n 0))
7138         (limit (if (< n 0) (point-min) (point-max))))
7139     (setq n (abs n))
7140     (while (and (not (= limit (point)))
7141                 (> n 0))
7142       ;; Skip past the current button.
7143       (when (get-text-property (point) 'gnus-callback)
7144         (goto-char (funcall function (point) 'gnus-callback nil limit)))
7145       ;; Go to the next (or previous) button.
7146       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
7147       ;; Put point at the start of the button.
7148       (when (and backward (not (get-text-property (point) 'gnus-callback)))
7149         (goto-char (funcall function (point) 'gnus-callback nil limit)))
7150       ;; Skip past intangible buttons.
7151       (when (get-text-property (point) 'intangible)
7152         (incf n))
7153       (decf n))
7154     (unless (zerop n)
7155       (gnus-message 5 "No more buttons"))
7156     n))
7157
7158 (defun gnus-article-highlight (&optional force)
7159   "Highlight current article.
7160 This function calls `gnus-article-highlight-headers',
7161 `gnus-article-highlight-citation',
7162 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7163 do the highlighting.  See the documentation for those functions."
7164   (interactive (list 'force))
7165   (gnus-article-highlight-headers)
7166   (gnus-article-highlight-citation force)
7167   (gnus-article-highlight-signature)
7168   (gnus-article-add-buttons force)
7169   (gnus-article-add-buttons-to-head))
7170
7171 (defun gnus-article-highlight-some (&optional force)
7172   "Highlight current article.
7173 This function calls `gnus-article-highlight-headers',
7174 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
7175 do the highlighting.  See the documentation for those functions."
7176   (interactive (list 'force))
7177   (gnus-article-highlight-headers)
7178   (gnus-article-highlight-signature)
7179   (gnus-article-add-buttons))
7180
7181 (defun gnus-article-highlight-headers ()
7182   "Highlight article headers as specified by `gnus-header-face-alist'."
7183   (interactive)
7184   (gnus-with-article-headers
7185     (let (regexp header-face field-face from hpoints fpoints)
7186       (dolist (entry gnus-header-face-alist)
7187         (goto-char (point-min))
7188         (setq regexp (concat "^\\("
7189                              (if (string-equal "" (nth 0 entry))
7190                                  "[^\t ]"
7191                                (nth 0 entry))
7192                              "\\)")
7193               header-face (nth 1 entry)
7194               field-face (nth 2 entry))
7195         (while (and (re-search-forward regexp nil t)
7196                     (not (eobp)))
7197           (beginning-of-line)
7198           (setq from (point))
7199           (unless (search-forward ":" nil t)
7200             (forward-char 1))
7201           (when (and header-face
7202                      (not (memq (point) hpoints)))
7203             (push (point) hpoints)
7204             (gnus-put-text-property from (point) 'face header-face))
7205           (when (and field-face
7206                      (not (memq (setq from (point)) fpoints)))
7207             (push from fpoints)
7208             (if (re-search-forward "^[^ \t]" nil t)
7209                 (forward-char -2)
7210               (goto-char (point-max)))
7211             (gnus-put-text-property from (point) 'face field-face)))))))
7212
7213 (defun gnus-article-highlight-signature ()
7214   "Highlight the signature in an article.
7215 It does this by highlighting everything after
7216 `gnus-signature-separator' using the face `gnus-signature'."
7217   (interactive)
7218   (when gnus-signature-face
7219     (gnus-with-article-buffer
7220       (let ((inhibit-point-motion-hooks t))
7221         (save-restriction
7222           (when (gnus-article-narrow-to-signature)
7223             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
7224                               'face gnus-signature-face)))))))
7225
7226 (defun gnus-article-buttonize-signature ()
7227   "Add button to the signature."
7228   (interactive)
7229   (save-excursion
7230     (set-buffer gnus-article-buffer)
7231     (let ((inhibit-read-only t)
7232           (inhibit-point-motion-hooks t))
7233       (when (gnus-article-search-signature)
7234         (gnus-article-add-button (match-beginning 0) (match-end 0)
7235                                  'gnus-signature-toggle
7236                                  (set-marker (make-marker)
7237                                              (1+ (match-end 0))))))))
7238
7239 (defun gnus-button-in-region-p (b e prop)
7240   "Say whether PROP exists in the region."
7241   (text-property-not-all b e prop nil))
7242
7243 (defun gnus-article-add-buttons (&optional force)
7244   "Find external references in the article and make buttons of them.
7245 \"External references\" are things like Message-IDs and URLs, as
7246 specified by `gnus-button-alist'."
7247   (interactive (list 'force))
7248   (gnus-with-article-buffer
7249     (let ((inhibit-point-motion-hooks t)
7250           (case-fold-search t)
7251           (alist gnus-button-alist)
7252           beg entry regexp)
7253       ;; Remove all old markers.
7254       (let (marker entry new-list)
7255         (while (setq marker (pop gnus-button-marker-list))
7256           (if (or (< marker (point-min)) (>= marker (point-max)))
7257               (push marker new-list)
7258             (goto-char marker)
7259             (when (setq entry (gnus-button-entry))
7260               (put-text-property (match-beginning (nth 1 entry))
7261                                  (match-end (nth 1 entry))
7262                                  'gnus-callback nil))
7263             (set-marker marker nil)))
7264         (setq gnus-button-marker-list new-list))
7265       ;; We skip the headers.
7266       (article-goto-body)
7267       (setq beg (point))
7268       (while (setq entry (pop alist))
7269         (setq regexp (eval (car entry)))
7270         (goto-char beg)
7271         (while (re-search-forward regexp nil t)
7272           (let* ((start (and entry (match-beginning (nth 1 entry))))
7273                  (end (and entry (match-end (nth 1 entry))))
7274                  (from (match-beginning 0)))
7275             (when (and (or (eq t (nth 2 entry))
7276                            (eval (nth 2 entry)))
7277                        (not (gnus-button-in-region-p
7278                              start end 'gnus-callback)))
7279               ;; That optional form returned non-nil, so we add the
7280               ;; button.
7281               (gnus-article-add-button
7282                start end 'gnus-button-push
7283                (car (push (set-marker (make-marker) from)
7284                           gnus-button-marker-list))))))))))
7285
7286 ;; Add buttons to the head of an article.
7287 (defun gnus-article-add-buttons-to-head ()
7288   "Add buttons to the head of the article."
7289   (interactive)
7290   (gnus-with-article-headers
7291     (let (beg end)
7292       (dolist (entry gnus-header-button-alist)
7293         ;; Each alist entry.
7294         (goto-char (point-min))
7295         (while (re-search-forward (car entry) nil t)
7296           ;; Each header matching the entry.
7297           (setq beg (match-beginning 0))
7298           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
7299                              (match-beginning 0))
7300                         (point-max)))
7301           (goto-char beg)
7302           (while (re-search-forward (eval (nth 1 entry)) end t)
7303             ;; Each match within a header.
7304             (let* ((entry (cdr entry))
7305                    (start (match-beginning (nth 1 entry)))
7306                    (end (match-end (nth 1 entry)))
7307                    (form (nth 2 entry)))
7308               (goto-char (match-end 0))
7309               (when (eval form)
7310                 (gnus-article-add-button
7311                  start end (nth 3 entry)
7312                  (buffer-substring (match-beginning (nth 4 entry))
7313                                    (match-end (nth 4 entry)))))))
7314           (goto-char end))))))
7315
7316 ;;; External functions:
7317
7318 (defun gnus-article-add-button (from to fun &optional data)
7319   "Create a button between FROM and TO with callback FUN and data DATA."
7320   (when gnus-article-button-face
7321     (gnus-overlay-put (gnus-make-overlay from to)
7322                       'face gnus-article-button-face))
7323   (gnus-add-text-properties
7324    from to
7325    (nconc (and gnus-article-mouse-face
7326                (list gnus-mouse-face-prop gnus-article-mouse-face))
7327           (list 'gnus-callback fun)
7328           (and data (list 'gnus-data data))))
7329   (widget-convert-button 'link from to :action 'gnus-widget-press-button
7330                          :button-keymap gnus-widget-button-keymap))
7331
7332 ;;; Internal functions:
7333
7334 (defun gnus-article-set-globals ()
7335   (with-current-buffer gnus-summary-buffer
7336     (gnus-set-global-variables)))
7337
7338 (defun gnus-signature-toggle (end)
7339   (gnus-with-article-buffer
7340     (let ((inhibit-point-motion-hooks t)
7341           (limit (next-single-property-change end 'mime-view-entity
7342                                               nil (point-max))))
7343       (if (text-property-any end limit 'article-type 'signature)
7344           (progn
7345             (gnus-delete-wash-type 'signature)
7346             (gnus-remove-text-properties-when
7347              'article-type 'signature end limit
7348              (cons 'article-type (cons 'signature
7349                                        gnus-hidden-properties))))
7350         (gnus-add-wash-type 'signature)
7351         (gnus-add-text-properties-when
7352          'article-type nil end limit
7353          (cons 'article-type (cons 'signature
7354                                    gnus-hidden-properties)))))
7355     (let ((gnus-article-mime-handle-alist-1 gnus-article-mime-handle-alist))
7356       (gnus-set-mode-line 'article))))
7357
7358 (defun gnus-button-entry ()
7359   ;; Return the first entry in `gnus-button-alist' matching this place.
7360   (let ((alist gnus-button-alist)
7361         (entry nil))
7362     (while alist
7363       (setq entry (pop alist))
7364       (if (looking-at (eval (car entry)))
7365           (setq alist nil)
7366         (setq entry nil)))
7367     entry))
7368
7369 (defun gnus-button-push (marker)
7370   ;; Push button starting at MARKER.
7371   (save-excursion
7372     (goto-char marker)
7373     (let* ((entry (gnus-button-entry))
7374            (inhibit-point-motion-hooks t)
7375            (fun (nth 3 entry))
7376            (args (mapcar (lambda (group)
7377                            (let ((string (match-string group)))
7378                              (set-text-properties
7379                               0 (length string) nil string)
7380                              string))
7381                          (nthcdr 4 entry))))
7382       (cond
7383        ((fboundp fun)
7384         (apply fun args))
7385        ((and (boundp fun)
7386              (fboundp (symbol-value fun)))
7387         (apply (symbol-value fun) args))
7388        (t
7389         (gnus-message 1 "You must define `%S' to use this button"
7390                       (cons fun args)))))))
7391
7392 (defun gnus-parse-news-url (url)
7393   (let (scheme server port group message-id articles)
7394     (with-temp-buffer
7395       (insert url)
7396       (goto-char (point-min))
7397       (when (looking-at "\\([A-Za-z]+\\):")
7398         (setq scheme (match-string 1))
7399         (goto-char (match-end 0)))
7400       (when (looking-at "//\\([^:/]+\\)\\(:?\\)\\([0-9]+\\)?/")
7401         (setq server (match-string 1))
7402         (setq port (if (stringp (match-string 3))
7403                        (string-to-number (match-string 3))
7404                      (match-string 3)))
7405         (goto-char (match-end 0)))
7406
7407       (cond
7408        ((looking-at "\\(.*@.*\\)")
7409         (setq message-id (match-string 1)))
7410        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
7411         (setq group (match-string 1)
7412               articles (split-string (match-string 2) "-")))
7413        ((looking-at "\\([^/]+\\)/?")
7414         (setq group (match-string 1)))
7415        (t
7416         (error "Unknown news URL syntax"))))
7417     (list scheme server port group message-id articles)))
7418
7419 (defun gnus-button-handle-news (url)
7420   "Fetch a news URL."
7421   (destructuring-bind (scheme server port group message-id articles)
7422       (gnus-parse-news-url url)
7423     (cond
7424      (message-id
7425       (save-excursion
7426         (set-buffer gnus-summary-buffer)
7427         (if server
7428             (let ((gnus-refer-article-method
7429                    (nconc (list (list 'nntp server))
7430                           gnus-refer-article-method))
7431                   (nntp-port-number (or port "nntp")))
7432               (gnus-message 7 "Fetching %s with %s"
7433                             message-id gnus-refer-article-method)
7434               (gnus-summary-refer-article message-id))
7435           (gnus-summary-refer-article message-id))))
7436      (group
7437       (gnus-button-fetch-group url)))))
7438
7439 (defun gnus-button-handle-man (url)
7440   "Fetch a man page."
7441   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7442   (when (eq gnus-button-man-handler 'woman)
7443     (setq url (gnus-replace-in-string url "([1-9][X1a-z]*).*\\'" "")))
7444   (gnus-message 9 "`%s' `%s'" gnus-button-man-handler url)
7445   (funcall gnus-button-man-handler url))
7446
7447 (defun gnus-button-handle-info-url (url)
7448   "Fetch an info URL."
7449   (setq url (mm-subst-char-in-string ?+ ?\  url))
7450   (cond
7451    ((string-match "^\\([^:/]+\\)?/\\(.*\\)" url)
7452     (gnus-info-find-node
7453      (concat "(" (or (gnus-url-unhex-string (match-string 1 url))
7454                      "Gnus")
7455              ")" (gnus-url-unhex-string (match-string 2 url)))))
7456    ((string-match "([^)\"]+)[^\"]+" url)
7457     (setq url
7458           (gnus-replace-in-string
7459            (gnus-replace-in-string url "[\n\t ]+" " ") "\"" ""))
7460     (gnus-info-find-node url))
7461    (t (error "Can't parse %s" url))))
7462
7463 (defun gnus-button-handle-info-url-gnome (url)
7464   "Fetch GNOME style info URL."
7465   (setq url (mm-subst-char-in-string ?_ ?\  url))
7466   (if (string-match "\\([^#]+\\)#?\\(.*\\)" url)
7467       (gnus-info-find-node
7468        (concat "("
7469                (gnus-url-unhex-string
7470                  (match-string 1 url))
7471                ")"
7472                (or (gnus-url-unhex-string
7473                     (match-string 2 url))
7474                    "Top")))
7475     (error "Can't parse %s" url)))
7476
7477 (defun gnus-button-handle-info-url-kde (url)
7478   "Fetch KDE style info URL."
7479   (gnus-info-find-node (gnus-url-unhex-string url)))
7480
7481 (defun gnus-button-handle-info-keystrokes (url)
7482   "Call `info' when pushing the corresponding URL button."
7483   ;; For links like `C-h i d m gnus RET', `C-h i d m CC Mode RET'.
7484   (info)
7485   (Info-directory)
7486   (Info-menu url))
7487
7488 (defun gnus-button-openpgp (url)
7489   "Retrieve and add an OpenPGP key given URL from an OpenPGP header."
7490   (with-temp-buffer
7491     (mm-url-insert-file-contents-external url)
7492     (pgg-snarf-keys-region (point-min) (point-max))
7493     (pgg-display-output-buffer nil nil nil)))
7494
7495 (defun gnus-button-message-id (message-id)
7496   "Fetch MESSAGE-ID."
7497   (with-current-buffer gnus-summary-buffer
7498     (gnus-summary-refer-article message-id)))
7499
7500 (defun gnus-button-fetch-group (address)
7501   "Fetch GROUP specified by ADDRESS."
7502   (if (not (string-match "[:/]" address))
7503       ;; This is just a simple group url.
7504       (gnus-group-read-ephemeral-group address gnus-select-method)
7505     (if (not
7506          (string-match
7507           "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\([^/]+\\)\\(/\\([0-9]+\\)\\)?"
7508           address))
7509         (error "Can't parse %s" address)
7510       (gnus-group-read-ephemeral-group
7511        (match-string 4 address)
7512        `(nntp ,(match-string 1 address)
7513               (nntp-address ,(match-string 1 address))
7514               (nntp-port-number ,(if (match-end 3)
7515                                      (match-string 3 address)
7516                                    "nntp")))
7517        nil nil nil
7518        (and (match-end 6) (list (string-to-number (match-string 6 address))))))))
7519
7520 (defun gnus-url-parse-query-string (query &optional downcase)
7521   (let (retval pairs cur key val)
7522     (setq pairs (split-string query "&"))
7523     (while pairs
7524       (setq cur (car pairs)
7525             pairs (cdr pairs))
7526       (if (not (string-match "=" cur))
7527           nil                           ; Grace
7528         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
7529               val (gnus-url-unhex-string (substring cur (match-end 0) nil) t))
7530         (if downcase
7531             (setq key (downcase key)))
7532         (setq cur (assoc key retval))
7533         (if cur
7534             (setcdr cur (cons val (cdr cur)))
7535           (setq retval (cons (list key val) retval)))))
7536     retval))
7537
7538 (defun gnus-url-mailto (url)
7539   ;; Send mail to someone
7540   (when (string-match "mailto:/*\\(.*\\)" url)
7541     (setq url (substring url (match-beginning 1) nil)))
7542   (let (to args subject func)
7543     (setq args (gnus-url-parse-query-string
7544                 (if (string-match "^\\?" url)
7545                     (substring url 1)
7546                   (if (string-match "^\\([^?]+\\)\\?\\(.*\\)" url)
7547                       (concat "to=" (match-string 1 url) "&"
7548                               (match-string 2 url))
7549                     (concat "to=" url)))
7550                 t)
7551           subject (cdr-safe (assoc "subject" args)))
7552     (gnus-msg-mail)
7553     (while args
7554       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
7555       (if (fboundp func)
7556           (funcall func)
7557         (message-position-on-field (caar args)))
7558       (insert (gnus-replace-in-string
7559                (mapconcat 'identity (reverse (cdar args)) ", ")
7560                "\r\n" "\n" t))
7561       (setq args (cdr args)))
7562     (if subject
7563         (message-goto-body)
7564       (message-goto-subject))))
7565
7566 (defun gnus-button-embedded-url (address)
7567   "Activate ADDRESS with `browse-url'."
7568   (browse-url (gnus-strip-whitespace address)))
7569
7570 ;;; Next/prev buttons in the article buffer.
7571
7572 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
7573 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
7574
7575 (defvar gnus-prev-page-map
7576   (let ((map (make-sparse-keymap)))
7577     (define-key map gnus-mouse-2 'gnus-button-prev-page)
7578     (define-key map "\r" 'gnus-button-prev-page)
7579     map))
7580
7581 (defvar gnus-next-page-map
7582   (let ((map (make-sparse-keymap)))
7583     (unless (>= emacs-major-version 21)
7584       ;; XEmacs doesn't care.
7585       (set-keymap-parent map gnus-article-mode-map))
7586     (define-key map gnus-mouse-2 'gnus-button-next-page)
7587     (define-key map "\r" 'gnus-button-next-page)
7588     map))
7589
7590 (defun gnus-insert-prev-page-button ()
7591   (let ((b (point))
7592         (inhibit-read-only t)
7593         (situation (get-text-property (point-min) 'mime-view-situation)))
7594     (gnus-eval-format
7595      gnus-prev-page-line-format nil
7596      `(keymap ,gnus-prev-page-map
7597          gnus-prev t
7598          gnus-callback gnus-article-button-prev-page
7599          article-type annotation
7600          mime-view-situation ,situation))
7601     (widget-convert-button
7602      'link b (if (bolp)
7603                  ;; Exclude a newline.
7604                  (1- (point))
7605                (point))
7606      :action 'gnus-button-prev-page
7607      :button-keymap gnus-prev-page-map)))
7608
7609 (defun gnus-button-next-page (&optional args more-args)
7610   "Go to the next page."
7611   (interactive)
7612   (let ((win (selected-window)))
7613     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7614     (gnus-article-next-page)
7615     (select-window win)))
7616
7617 (defun gnus-button-prev-page (&optional args more-args)
7618   "Go to the prev page."
7619   (interactive)
7620   (let ((win (selected-window)))
7621     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7622     (gnus-article-prev-page)
7623     (select-window win)))
7624
7625 (defun gnus-insert-next-page-button ()
7626   (let ((b (point))
7627         (inhibit-read-only t)
7628         (situation (get-text-property (point-min) 'mime-view-situation)))
7629     (gnus-eval-format gnus-next-page-line-format nil
7630                       `(keymap ,gnus-next-page-map
7631                           gnus-next t
7632                           gnus-callback gnus-article-button-next-page
7633                           article-type annotation
7634                           mime-view-situation ,situation))
7635     (widget-convert-button
7636      'link b (if (bolp)
7637                  ;; Exclude a newline.
7638                  (1- (point))
7639                (point))
7640      :action 'gnus-button-next-page
7641      :button-keymap gnus-next-page-map)))
7642
7643 (defun gnus-article-button-next-page (arg)
7644   "Go to the next page."
7645   (interactive "P")
7646   (let ((win (selected-window)))
7647     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7648     (gnus-article-next-page)
7649     (select-window win)))
7650
7651 (defun gnus-article-button-prev-page (arg)
7652   "Go to the prev page."
7653   (interactive "P")
7654   (let ((win (selected-window)))
7655     (select-window (gnus-get-buffer-window gnus-article-buffer t))
7656     (gnus-article-prev-page)
7657     (select-window win)))
7658
7659 (defvar gnus-decode-header-methods
7660   '(mail-decode-encoded-word-region)
7661   "List of methods used to decode headers.
7662
7663 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
7664 is FUNCTION, FUNCTION will be applied to all newsgroups.  If item is a
7665 \(REGEXP . FUNCTION), FUNCTION will be only apply to the newsgroups
7666 whose names match REGEXP.
7667
7668 For example:
7669 \((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
7670  mail-decode-encoded-word-region
7671  (\"chinese\" . rfc1843-decode-region))
7672 ")
7673
7674 (defvar gnus-decode-header-methods-cache nil)
7675
7676 (defun gnus-multi-decode-header (start end)
7677   "Apply the functions from `gnus-encoded-word-methods' that match."
7678   (unless (and gnus-decode-header-methods-cache
7679                (eq gnus-newsgroup-name
7680                    (car gnus-decode-header-methods-cache)))
7681     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
7682     (mapcar (lambda (x)
7683               (if (symbolp x)
7684                   (nconc gnus-decode-header-methods-cache (list x))
7685                 (if (and gnus-newsgroup-name
7686                          (string-match (car x) gnus-newsgroup-name))
7687                     (nconc gnus-decode-header-methods-cache
7688                            (list (cdr x))))))
7689             gnus-decode-header-methods))
7690   (let ((xlist gnus-decode-header-methods-cache))
7691     (pop xlist)
7692     (save-restriction
7693       (narrow-to-region start end)
7694       (while xlist
7695         (funcall (pop xlist) (point-min) (point-max))))))
7696
7697 ;;;
7698 ;;; Treatment top-level handling.
7699 ;;;
7700
7701 (defun gnus-treat-article (condition &optional part-number total-parts type)
7702   (let ((length (- (point-max) (point-min)))
7703         (alist gnus-treatment-function-alist)
7704         (article-goto-body-goes-to-point-min-p t)
7705         (treated-type
7706          (or (not type)
7707              (catch 'found
7708                (let ((list gnus-article-treat-types))
7709                  (while list
7710                    (when (string-match (pop list) type)
7711                      (throw 'found t)))))))
7712         (highlightp (gnus-visual-p 'article-highlight 'highlight))
7713         (entity (static-unless (featurep 'xemacs)
7714                   (when (eq 'head condition)
7715                     (get-text-property (point-min) 'mime-view-entity))))
7716         val elem buttonized)
7717     (gnus-run-hooks 'gnus-part-display-hook)
7718     (unless gnus-inhibit-treatment
7719       (dolist (elem alist)
7720         (setq val
7721               (save-excursion
7722                 (when (gnus-buffer-live-p gnus-summary-buffer)
7723                   (set-buffer gnus-summary-buffer))
7724                 (symbol-value (car elem))))
7725         (when (and (or (consp val)
7726                        treated-type)
7727                    (gnus-treat-predicate val)
7728                    (or (not (get (car elem) 'highlight))
7729                        highlightp))
7730           (when (and (not buttonized)
7731                      (memq (car elem)
7732                            '(gnus-treat-hide-signature
7733                              gnus-treat-highlight-signature)))
7734             (gnus-article-buttonize-signature)
7735             (setq buttonized t))
7736           (save-restriction
7737             (funcall (cadr elem)))))
7738       ;; FSF Emacsen does not inherit the existing text properties
7739       ;; in the new text, so we should do it for `mime-view-entity'.
7740       (static-unless (featurep 'xemacs)
7741         (when entity
7742           (put-text-property (point-min) (point-max)
7743                              'mime-view-entity entity))))))
7744
7745 ;; Dynamic variables.
7746 (eval-when-compile
7747   (defvar part-number)
7748   (defvar total-parts)
7749   (defvar type)
7750   (defvar condition)
7751   (defvar length))
7752
7753 (defun gnus-treat-predicate (val)
7754   (cond
7755    ((null val)
7756     nil)
7757    ((and (listp val)
7758          (stringp (car val)))
7759     (apply 'gnus-or (mapcar `(lambda (s)
7760                                (string-match s ,(or gnus-newsgroup-name "")))
7761                             val)))
7762    ((listp val)
7763     (let ((pred (pop val)))
7764       (cond
7765        ((eq pred 'or)
7766         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
7767        ((eq pred 'and)
7768         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
7769        ((eq pred 'not)
7770         (not (gnus-treat-predicate (car val))))
7771        ((eq pred 'typep)
7772         (equal (car val) type))
7773        (t
7774         (error "%S is not a valid predicate" pred)))))
7775    ((eq val 'mime)
7776     gnus-show-mime)
7777    (condition
7778     (eq condition val))
7779    ((eq val t)
7780     t)
7781    ((eq val 'head)
7782     nil)
7783    ((eq val 'first)
7784     (eq part-number 1))
7785    ((eq val 'last)
7786     (eq part-number total-parts))
7787    ((numberp val)
7788     (< length val))
7789    (t
7790     (error "%S is not a valid value" val))))
7791
7792 (defun gnus-article-encrypt-body (protocol &optional n)
7793   "Encrypt the article body."
7794   (interactive
7795    (list
7796     (or gnus-article-encrypt-protocol
7797         (completing-read "Encrypt protocol: "
7798                          gnus-article-encrypt-protocol-alist
7799                          nil t))
7800     current-prefix-arg))
7801   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
7802     (unless func
7803       (error "Can't find the encrypt protocol %s" protocol))
7804     (if (member gnus-newsgroup-name '("nndraft:delayed"
7805                                       "nndraft:drafts"
7806                                       "nndraft:queue"))
7807         (error "Can't encrypt the article in group %s"
7808                gnus-newsgroup-name))
7809     (gnus-summary-iterate n
7810       (save-excursion
7811         (set-buffer gnus-summary-buffer)
7812         (let ((mail-parse-charset gnus-newsgroup-charset)
7813               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
7814               (summary-buffer gnus-summary-buffer)
7815               references point)
7816           (gnus-set-global-variables)
7817           (when (gnus-group-read-only-p)
7818             (error "The current newsgroup does not support article encrypt"))
7819           (gnus-summary-show-article t)
7820           (setq references
7821                 (or (mail-header-references gnus-current-headers) ""))
7822           (set-buffer gnus-article-buffer)
7823           (let* ((inhibit-read-only t)
7824                  (headers
7825                   (mapcar (lambda (field)
7826                             (and (save-restriction
7827                                    (message-narrow-to-head)
7828                                    (goto-char (point-min))
7829                                    (search-forward field nil t))
7830                                  (prog2
7831                                      (message-narrow-to-field)
7832                                      (buffer-string)
7833                                    (delete-region (point-min) (point-max))
7834                                    (widen))))
7835                           '("Content-Type:" "Content-Transfer-Encoding:"
7836                             "Content-Disposition:"))))
7837             (message-narrow-to-head)
7838             (message-remove-header "MIME-Version")
7839             (goto-char (point-max))
7840             (setq point (point))
7841             (insert (apply 'concat headers))
7842             (widen)
7843             (narrow-to-region point (point-max))
7844             (let ((message-options message-options))
7845               (message-options-set 'message-sender user-mail-address)
7846               (message-options-set 'message-recipients user-mail-address)
7847               (message-options-set 'message-sign-encrypt 'not)
7848               (funcall func))
7849             (goto-char (point-min))
7850             (insert "MIME-Version: 1.0\n")
7851             (widen)
7852             (gnus-summary-edit-article-done
7853              references nil summary-buffer t))
7854           (when gnus-keep-backlog
7855             (gnus-backlog-remove-article
7856              (car gnus-article-current) (cdr gnus-article-current)))
7857           (save-excursion
7858             (when (get-buffer gnus-original-article-buffer)
7859               (set-buffer gnus-original-article-buffer)
7860               (setq gnus-original-article nil)))
7861           (when gnus-use-cache
7862             (gnus-cache-update-article
7863              (car gnus-article-current) (cdr gnus-article-current))))))))
7864
7865 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
7866   "The following specs can be used:
7867 %t  The security MIME type
7868 %i  Additional info
7869 %d  Details
7870 %D  Details if button is pressed")
7871
7872 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
7873   "The following specs can be used:
7874 %t  The security MIME type
7875 %i  Additional info
7876 %d  Details
7877 %D  Details if button is pressed")
7878
7879 (defvar gnus-mime-security-button-line-format-alist
7880   '((?t gnus-tmp-type ?s)
7881     (?i gnus-tmp-info ?s)
7882     (?d gnus-tmp-details ?s)
7883     (?D gnus-tmp-pressed-details ?s)))
7884
7885 (defvar gnus-mime-security-button-map
7886   (let ((map (make-sparse-keymap)))
7887     (define-key map gnus-mouse-2 'gnus-article-push-button)
7888     (define-key map "\r" 'gnus-article-press-button)
7889     map))
7890
7891 (defvar gnus-mime-security-details-buffer nil)
7892
7893 (defvar gnus-mime-security-button-pressed nil)
7894
7895 (defvar gnus-mime-security-show-details-inline t
7896   "If non-nil, show details in the article buffer.")
7897
7898 (defun gnus-mime-security-verify-or-decrypt (handle)
7899   (mm-remove-parts (cdr handle))
7900   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
7901         point (inhibit-read-only t))
7902     (if region
7903         (goto-char (car region)))
7904     (save-restriction
7905       (narrow-to-region (point) (point))
7906       (with-current-buffer (mm-handle-multipart-original-buffer handle)
7907         (let* ((mm-verify-option 'known)
7908                (mm-decrypt-option 'known)
7909                (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
7910           (unless (eq nparts (cdr handle))
7911             (mm-destroy-parts (cdr handle))
7912             (setcdr handle nparts))))
7913       (setq point (point))
7914       (gnus-mime-display-security handle)
7915       (goto-char (point-max)))
7916     (when region
7917       (delete-region (point) (cdr region))
7918       (set-marker (car region) nil)
7919       (set-marker (cdr region) nil))
7920     (goto-char point)))
7921
7922 (defun gnus-mime-security-show-details (handle)
7923   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
7924     (if (not details)
7925         (gnus-message 5 "No details.")
7926       (if gnus-mime-security-show-details-inline
7927           (let ((gnus-mime-security-button-pressed
7928                  (not (get-text-property (point) 'gnus-mime-details)))
7929                 (gnus-mime-security-button-line-format
7930                  (get-text-property (point) 'gnus-line-format))
7931                 (inhibit-read-only t))
7932             (forward-char -1)
7933             (while (eq (get-text-property (point) 'gnus-line-format)
7934                        gnus-mime-security-button-line-format)
7935               (forward-char -1))
7936             (forward-char)
7937             (save-restriction
7938               (narrow-to-region (point) (point))
7939               (gnus-insert-mime-security-button handle))
7940             (delete-region (point)
7941                            (or (text-property-not-all
7942                                 (point) (point-max)
7943                                 'gnus-line-format
7944                                 gnus-mime-security-button-line-format)
7945                                (point-max))))
7946         ;; Not inlined.
7947         (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
7948             (with-current-buffer gnus-mime-security-details-buffer
7949               (erase-buffer)
7950               t)
7951           (setq gnus-mime-security-details-buffer
7952                 (gnus-get-buffer-create "*MIME Security Details*")))
7953         (with-current-buffer gnus-mime-security-details-buffer
7954           (insert details)
7955           (goto-char (point-min)))
7956         (pop-to-buffer gnus-mime-security-details-buffer)))))
7957
7958 (defun gnus-mime-security-press-button (handle)
7959   (save-excursion
7960     (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7961         (gnus-mime-security-show-details handle)
7962       (gnus-mime-security-verify-or-decrypt handle))))
7963
7964 (defun gnus-insert-mime-security-button (handle &optional displayed)
7965   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
7966          (gnus-tmp-type
7967           (concat
7968            (or (nth 2 (assoc protocol mm-verify-function-alist))
7969                (nth 2 (assoc protocol mm-decrypt-function-alist))
7970                "Unknown")
7971            (if (equal (car handle) "multipart/signed")
7972                " Signed" " Encrypted")
7973            " Part"))
7974          (gnus-tmp-info
7975           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
7976               "Undecided"))
7977          (gnus-tmp-details
7978           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
7979          gnus-tmp-pressed-details
7980          b e)
7981     (setq gnus-tmp-details
7982           (if gnus-tmp-details
7983               (concat "\n" gnus-tmp-details)
7984             ""))
7985     (setq gnus-tmp-pressed-details
7986           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
7987     (unless (bolp)
7988       (insert "\n"))
7989     (setq b (point))
7990     (gnus-eval-format
7991      gnus-mime-security-button-line-format
7992      gnus-mime-security-button-line-format-alist
7993      `(keymap ,gnus-mime-security-button-map
7994          gnus-callback gnus-mime-security-press-button
7995          gnus-line-format ,gnus-mime-security-button-line-format
7996          gnus-mime-details ,gnus-mime-security-button-pressed
7997          article-type annotation
7998          gnus-data ,handle))
7999     (setq e (if (bolp)
8000                 ;; Exclude a newline.
8001                 (1- (point))
8002               (point)))
8003     (widget-convert-button
8004      'link b e
8005      :mime-handle handle
8006      :action 'gnus-widget-press-button
8007      :button-keymap gnus-mime-security-button-map
8008      :help-echo
8009      (lambda (widget/window &optional overlay pos)
8010        ;; Needed to properly clear the message due to a bug in
8011        ;; wid-edit (XEmacs only).
8012        (when (boundp 'help-echo-owns-message)
8013          (setq help-echo-owns-message t))
8014        (format
8015         "%S: show detail"
8016         (aref gnus-mouse-2 0))))))
8017
8018 (defun gnus-mime-display-security (handle)
8019   (save-restriction
8020     (narrow-to-region (point) (point))
8021     (unless (gnus-unbuttonized-mime-type-p (car handle))
8022       (gnus-insert-mime-security-button handle))
8023     (gnus-mime-display-part (cadr handle))
8024     (unless (bolp)
8025       (insert "\n"))
8026     (unless (gnus-unbuttonized-mime-type-p (car handle))
8027       (let ((gnus-mime-security-button-line-format
8028              gnus-mime-security-button-end-line-format))
8029         (gnus-insert-mime-security-button handle)))
8030     (mm-set-handle-multipart-parameter
8031      handle 'gnus-region
8032      (cons (set-marker (make-marker) (point-min))
8033            (set-marker (make-marker) (point-max))))))
8034
8035
8036 ;;; @ for mime-view
8037 ;;;
8038
8039 (defun gnus-article-header-presentation-method (entity situation)
8040   (mime-insert-header entity)
8041   (article-decode-group-name))
8042
8043 (set-alist 'mime-header-presentation-method-alist
8044            'gnus-original-article-mode
8045            #'gnus-article-header-presentation-method)
8046
8047 (defun gnus-mime-preview-quitting-method ()
8048   (mime-preview-kill-buffer)
8049   (delete-other-windows)
8050   (gnus-article-show-summary)
8051   (gnus-summary-select-article gnus-show-all-headers t))
8052
8053 (set-alist 'mime-preview-quitting-method-alist
8054            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
8055
8056 (set-alist 'mime-preview-following-method-alist
8057            'gnus-original-article-mode #'gnus-following-method)
8058
8059 (set-alist 'mime-preview-over-to-previous-method-alist
8060            'gnus-original-article-mode
8061            (lambda ()
8062              (if (> (point-min) 1)
8063                  (gnus-article-prev-page)
8064                (gnus-article-read-summary-keys
8065                 nil (gnus-character-to-event ?P)))))
8066
8067 (set-alist 'mime-preview-over-to-next-method-alist
8068            'gnus-original-article-mode'
8069            (lambda ()
8070              (if (< (point-max) (buffer-size))
8071                  (gnus-article-next-page)
8072                (gnus-article-read-summary-keys
8073                 nil (gnus-character-to-event ?N)))))
8074
8075
8076 ;;; @ end
8077 ;;;
8078
8079 (gnus-ems-redefine)
8080
8081 (provide 'gnus-art)
8082
8083 (run-hooks 'gnus-art-load-hook)
8084
8085 ;;; gnus-art.el ends here