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