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