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