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