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