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