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