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