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