2795f700142e9f05cad5d7881fd07e5d6bf80586
[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
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 (require 'cl))
32 (eval-when-compile (require 'static))
33
34 (require 'path-util)
35 (require 'gnus)
36 (require 'gnus-sum)
37 (require 'gnus-spec)
38 (require 'gnus-int)
39 (require 'alist)
40 (require 'mime-view)
41 (require 'wid-edit)
42
43 ;; Avoid byte-compile warnings.
44 (eval-when-compile
45   (require 'mm-bodies)
46   (require 'mail-parse)
47   (require 'mm-decode)
48   (require 'mm-view)
49   (require 'mm-uu)
50   )
51
52 (autoload 'gnus-msg-mail "gnus-msg" nil t)
53 (autoload 'gnus-button-mailto "gnus-msg")
54 (autoload 'gnus-button-reply "gnus-msg" nil t)
55
56 (defgroup gnus-article nil
57   "Article display."
58   :link '(custom-manual "(gnus)The Article Buffer")
59   :group 'gnus)
60
61 (defgroup gnus-article-treat nil
62   "Treating article parts."
63   :link '(custom-manual "(gnus)Article Hiding")
64   :group 'gnus-article)
65
66 (defgroup gnus-article-hiding nil
67   "Hiding article parts."
68   :link '(custom-manual "(gnus)Article Hiding")
69   :group 'gnus-article)
70
71 (defgroup gnus-article-highlight nil
72   "Article highlighting."
73   :link '(custom-manual "(gnus)Article Highlighting")
74   :group 'gnus-article
75   :group 'gnus-visual)
76
77 (defgroup gnus-article-signature nil
78   "Article signatures."
79   :link '(custom-manual "(gnus)Article Signature")
80   :group 'gnus-article)
81
82 (defgroup gnus-article-headers nil
83   "Article headers."
84   :link '(custom-manual "(gnus)Hiding Headers")
85   :group 'gnus-article)
86
87 (defgroup gnus-article-washing nil
88   "Special commands on articles."
89   :link '(custom-manual "(gnus)Article Washing")
90   :group 'gnus-article)
91
92 (defgroup gnus-article-emphasis nil
93   "Fontisizing articles."
94   :link '(custom-manual "(gnus)Article Fontisizing")
95   :group 'gnus-article)
96
97 (defgroup gnus-article-saving nil
98   "Saving articles."
99   :link '(custom-manual "(gnus)Saving Articles")
100   :group 'gnus-article)
101
102 (defgroup gnus-article-mime nil
103   "Worshiping the MIME wonder."
104   :link '(custom-manual "(gnus)Using MIME")
105   :group 'gnus-article)
106
107 (defgroup gnus-article-buttons nil
108   "Pushable buttons in the article buffer."
109   :link '(custom-manual "(gnus)Article Buttons")
110   :group 'gnus-article)
111
112 (defgroup gnus-article-various nil
113   "Other article options."
114   :link '(custom-manual "(gnus)Misc Article")
115   :group 'gnus-article)
116
117 (defcustom gnus-ignored-headers
118   '("^Path:" "^Expires:" "^Date-Received:" "^References:" "^Xref:" "^Lines:"
119     "^Relay-Version:" "^Message-ID:" "^Approved:" "^Sender:" "^Received:"
120     "^X-UIDL:" "^MIME-Version:" "^Return-Path:" "^In-Reply-To:"
121     "^Content-Type:" "^Content-Transfer-Encoding:" "^X-WebTV-Signature:"
122     "^X-MimeOLE:" "^X-MSMail-Priority:" "^X-Priority:" "^X-Loop:"
123     "^X-Authentication-Warning:" "^X-MIME-Autoconverted:" "^X-Face:"
124     "^X-Attribution:" "^X-Originating-IP:" "^Delivered-To:"
125     "^NNTP-[-A-Za-z]+:" "^Distribution:" "^X-no-archive:" "^X-Trace:"
126     "^X-Complaints-To:" "^X-NNTP-Posting-Host:" "^X-Orig.*:"
127     "^Abuse-Reports-To:" "^Cache-Post-Path:" "^X-Article-Creation-Date:"
128     "^X-Poster:" "^X-Mail2News-Path:" "^X-Server-Date:" "^X-Cache:"
129     "^Originator:" "^X-Problems-To:" "^X-Auth-User:" "^X-Post-Time:"
130     "^X-Admin:" "^X-UID:" "^Resent-[-A-Za-z]+:" "^X-Mailing-List:"
131     "^Precedence:" "^Original-[-A-Za-z]+:" "^X-filename:" "^X-Orcpt:"
132     "^Old-Received:" "^X-Pgp" "^X-Auth:" "^X-From-Line:"
133     "^X-Gnus-Article-Number:" "^X-Majordomo:" "^X-Url:" "^X-Sender:"
134     "^MBOX-Line" "^Priority:" "^X400-[-A-Za-z]+:"
135     "^Status:" "^X-Gnus-Mail-Source:" "^Cancel-Lock:"
136     "^X-FTN" "^X-EXP32-SerialNo:" "^Encoding:" "^Importance:"
137     "^Autoforwarded:" "^Original-Encoded-Information-Types:" "^X-Ya-Pop3:"
138     "^X-Face-Version:" "^X-Vms-To:" "^X-ML-NAME:" "^X-ML-COUNT:"
139     "^Mailing-List:" "^X-finfo:" "^X-md5sum:" "^X-md5sum-Origin:"
140     "^X-Sun-Charset:" "^X-Accept-Language:" "^X-Envelope-Sender:"
141     "^List-[A-Za-z]+:" "^X-Listprocessor-Version:"
142     "^X-Received:" "^X-Distribute:" "^X-Sequence:" "^X-Juno-Line-Breaks:"
143     "^X-Notes-Item:" "^X-MS-TNEF-Correlator:" "^x-uunet-gateway:"
144     "^X-Received:" "^Content-length:" "X-precedence:"
145     "^X-Authenticated-User:" "^X-Comment" "^X-Report:" "^X-Abuse-Info:"
146     "^X-HTTP-Proxy:" "^X-Mydeja-Info:" "^X-Copyright" "^X-No-Markup:"
147     "^X-Abuse-Info:")
148   "*All headers that start with this regexp will be hidden.
149 This variable can also be a list of regexps of headers to be ignored.
150 If `gnus-visible-headers' is non-nil, this variable will be ignored."
151   :type '(choice :custom-show nil
152                  regexp
153                  (repeat regexp))
154   :group 'gnus-article-hiding)
155
156 (defcustom gnus-visible-headers
157   "^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:"
158   "*All headers that do not match this regexp will be hidden.
159 This variable can also be a list of regexp of headers to remain visible.
160 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
161   :type '(repeat :value-to-internal (lambda (widget value)
162                                       (custom-split-regexp-maybe value))
163                  :match (lambda (widget value)
164                           (or (stringp value)
165                               (widget-editable-list-match widget value)))
166                  regexp)
167   :group 'gnus-article-hiding)
168
169 (defcustom gnus-sorted-header-list
170   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
171     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
172   "*This variable is a list of regular expressions.
173 If it is non-nil, headers that match the regular expressions will
174 be placed first in the article buffer in the sequence specified by
175 this list."
176   :type '(repeat regexp)
177   :group 'gnus-article-hiding)
178
179 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
180   "Headers that are only to be displayed if they have interesting data.
181 Possible values in this list are `empty', `newsgroups', `followup-to',
182 `to-address', `reply-to', `date', `long-to', and `many-to'."
183   :type '(set (const :tag "Headers with no content." empty)
184               (const :tag "Newsgroups with only one group." newsgroups)
185               (const :tag "To identical to to-address." to-address)
186               (const :tag "Followup-to identical to newsgroups." followup-to)
187               (const :tag "Reply-to identical to from." reply-to)
188               (const :tag "Date less than four days old." date)
189               (const :tag "Very long To and/or Cc header." long-to)
190               (const :tag "Multiple To and/or Cc headers." many-to))
191   :group 'gnus-article-hiding)
192
193 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
194   "Regexp matching signature separator.
195 This can also be a list of regexps.  In that case, it will be checked
196 from head to tail looking for a separator.  Searches will be done from
197 the end of the buffer."
198   :type '(repeat string)
199   :group 'gnus-article-signature)
200
201 (defcustom gnus-signature-limit nil
202   "Provide a limit to what is considered a signature.
203 If it is a number, no signature may not be longer (in characters) than
204 that number.  If it is a floating point number, no signature may be
205 longer (in lines) than that number.  If it is a function, the function
206 will be called without any parameters, and if it returns nil, there is
207 no signature in the buffer.  If it is a string, it will be used as a
208 regexp.  If it matches, the text in question is not a signature."
209   :type '(choice (integer :value 200)
210                  (number :value 4.0)
211                  (function :value fun)
212                  (regexp :value ".*"))
213   :group 'gnus-article-signature)
214
215 (defcustom gnus-hidden-properties '(invisible t intangible t)
216   "Property list to use for hiding text."
217   :type 'sexp
218   :group 'gnus-article-hiding)
219
220 ;; Fixme: This isn't the right thing for mixed graphical and and
221 ;; non-graphical frames in a session.
222 ;; gnus-xmas.el overrides this for XEmacs.
223 (defcustom gnus-article-x-face-command
224   (cond
225    ((and (fboundp 'image-type-available-p)
226          (module-installed-p 'x-face-e21))
227     'x-face-decode-message-header)
228    ((and (fboundp 'image-type-available-p)
229          (image-type-available-p 'xbm))
230     'gnus-article-display-xface)
231    ((and (not (featurep 'xemacs))
232          window-system
233          (module-installed-p 'x-face-mule))
234     'x-face-mule-gnus-article-display-x-face)
235    (gnus-article-compface-xbm
236     "{ echo '/* Width=48, Height=48 */'; uncompface; } | display -")
237    (t
238     "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
239 display -"))
240   "*String or function to be executed to display an X-Face header.
241 If it is a string, the command will be executed in a sub-shell
242 asynchronously.  The compressed face will be piped to this command."
243   :type '(choice string
244                  (function-item
245                   :tag "x-face-decode-message-header (x-face-e21)"
246                   x-face-decode-message-header)
247                  (function-item gnus-article-display-xface)
248                  (function-item x-face-mule-gnus-article-display-x-face)
249                  function)
250   ;;:version "21.1"
251   :group 'gnus-article-washing)
252
253 (defcustom gnus-article-x-face-too-ugly nil
254   "Regexp matching posters whose face shouldn't be shown automatically."
255   :type '(choice regexp (const nil))
256   :group 'gnus-article-washing)
257
258 (defcustom gnus-article-banner-alist nil
259   "Banner alist for stripping.
260 For example,
261      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
262   :version "21.1"
263   :type '(repeat (cons symbol regexp))
264   :group 'gnus-article-washing)
265
266 (gnus-define-group-parameter
267  banner
268  :variable-document
269  "Alist of regexps (to match group names) and banner."
270  :variable-group gnus-article-washing
271  :parameter-type
272  '(choice :tag "Banner"
273           :value nil
274           (const :tag "Remove signature" signature)
275           (symbol :tag "Item in `gnus-article-banner-alist'" none)
276           regexp
277           (const :tag "None" nil))
278  :parameter-document
279  "If non-nil, specify how to remove `banners' from articles.
280
281 Symbol `signature' means to remove signatures delimited by
282 `gnus-signature-separator'.  Any other symbol is used to look up a
283 regular expression to match the banner in `gnus-article-banner-alist'.
284 A string is used as a regular expression to match the banner
285 directly.")
286
287 (defcustom gnus-emphasis-alist
288   (let ((format
289          "\\(\\s-\\|^\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\\(\\s-\\|[-,;:\"]\\s-\\|[?!.]+\\s-\\|\\s)\\)")
290         (types
291          '(("_" "_" underline)
292            ("/" "/" italic)
293            ("\\*" "\\*" bold)
294            ("_/" "/_" underline-italic)
295            ("_\\*" "\\*_" underline-bold)
296            ("\\*/" "/\\*" bold-italic)
297            ("_\\*/" "/\\*_" underline-bold-italic))))
298     `(("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
299        2 3 gnus-emphasis-underline)
300       ,@(mapcar
301          (lambda (spec)
302            (list
303             (format format (car spec) (car (cdr spec)))
304             2 3 (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
305          types)))
306   "*Alist that says how to fontify certain phrases.
307 Each item looks like this:
308
309   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
310
311 The first element is a regular expression to be matched.  The second
312 is a number that says what regular expression grouping used to find
313 the entire emphasized word.  The third is a number that says what
314 regexp grouping should be displayed and highlighted.  The fourth
315 is the face used for highlighting."
316   :type '(repeat (list :value ("" 0 0 default)
317                        regexp
318                        (integer :tag "Match group")
319                        (integer :tag "Emphasize group")
320                        face))
321   :group 'gnus-article-emphasis)
322
323 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
324   "A regexp to describe whitespace which should not be emphasized.
325 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
326 The former avoids underlining of leading and trailing whitespace,
327 and the latter avoids underlining any whitespace at all."
328   :version "21.1"
329   :group 'gnus-article-emphasis
330   :type 'regexp)
331
332 (defface gnus-emphasis-bold '((t (:bold t)))
333   "Face used for displaying strong emphasized text (*word*)."
334   :group 'gnus-article-emphasis)
335
336 (defface gnus-emphasis-italic '((t (:italic t)))
337   "Face used for displaying italic emphasized text (/word/)."
338   :group 'gnus-article-emphasis)
339
340 (defface gnus-emphasis-underline '((t (:underline t)))
341   "Face used for displaying underlined emphasized text (_word_)."
342   :group 'gnus-article-emphasis)
343
344 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
345   "Face used for displaying underlined bold emphasized text (_*word*_)."
346   :group 'gnus-article-emphasis)
347
348 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
349   "Face used for displaying underlined italic emphasized text (_/word/_)."
350   :group 'gnus-article-emphasis)
351
352 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
353   "Face used for displaying bold italic emphasized text (/*word*/)."
354   :group 'gnus-article-emphasis)
355
356 (defface gnus-emphasis-underline-bold-italic
357   '((t (:bold t :italic t :underline t)))
358   "Face used for displaying underlined bold italic emphasized text.
359 Esample: (_/*word*/_)."
360   :group 'gnus-article-emphasis)
361
362 (defface gnus-emphasis-highlight-words
363   '((t (:background "black" :foreground "yellow")))
364   "Face used for displaying highlighted words."
365   :group 'gnus-article-emphasis)
366
367 (defcustom gnus-article-time-format "%a, %b %d %Y %T %Z"
368   "Format for display of Date headers in article bodies.
369 See `format-time-string' for the possible values.
370
371 The variable can also be function, which should return a complete Date
372 header.  The function is called with one argument, the time, which can
373 be fed to `format-time-string'."
374   :type '(choice string symbol)
375   :link '(custom-manual "(gnus)Article Date")
376   :group 'gnus-article-washing)
377
378 (eval-and-compile
379   (autoload 'mail-extract-address-components "mail-extr"))
380
381 (defcustom gnus-save-all-headers t
382   "*If non-nil, don't remove any headers before saving."
383   :group 'gnus-article-saving
384   :type 'boolean)
385
386 (defcustom gnus-prompt-before-saving 'always
387   "*This variable says how much prompting is to be done when saving articles.
388 If it is nil, no prompting will be done, and the articles will be
389 saved to the default files.  If this variable is `always', each and
390 every article that is saved will be preceded by a prompt, even when
391 saving large batches of articles.  If this variable is neither nil not
392 `always', there the user will be prompted once for a file name for
393 each invocation of the saving commands."
394   :group 'gnus-article-saving
395   :type '(choice (item always)
396                  (item :tag "never" nil)
397                  (sexp :tag "once" :format "%t\n" :value t)))
398
399 (defcustom gnus-saved-headers gnus-visible-headers
400   "Headers to keep if `gnus-save-all-headers' is nil.
401 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
402 If that variable is nil, however, all headers that match this regexp
403 will be kept while the rest will be deleted before saving."
404   :group 'gnus-article-saving
405   :type 'regexp)
406
407 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
408   "A function to save articles in your favourite format.
409 The function must be interactively callable (in other words, it must
410 be an Emacs command).
411
412 Gnus provides the following functions:
413
414 * gnus-summary-save-in-rmail (Rmail format)
415 * gnus-summary-save-in-mail (Unix mail format)
416 * gnus-summary-save-in-folder (MH folder)
417 * gnus-summary-save-in-file (article format)
418 * gnus-summary-save-in-vm (use VM's folder format)
419 * gnus-summary-write-to-file (article format -- overwrite)."
420   :group 'gnus-article-saving
421   :type '(radio (function-item gnus-summary-save-in-rmail)
422                 (function-item gnus-summary-save-in-mail)
423                 (function-item gnus-summary-save-in-folder)
424                 (function-item gnus-summary-save-in-file)
425                 (function-item gnus-summary-save-in-vm)
426                 (function-item gnus-summary-write-to-file)))
427
428 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
429   "A function generating a file name to save articles in Rmail format.
430 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
431   :group 'gnus-article-saving
432   :type 'function)
433
434 (defcustom gnus-mail-save-name 'gnus-plain-save-name
435   "A function generating a file name to save articles in Unix mail format.
436 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
437   :group 'gnus-article-saving
438   :type 'function)
439
440 (defcustom gnus-folder-save-name 'gnus-folder-save-name
441   "A function generating a file name to save articles in MH folder.
442 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
443   :group 'gnus-article-saving
444   :type 'function)
445
446 (defcustom gnus-file-save-name 'gnus-numeric-save-name
447   "A function generating a file name to save articles in article format.
448 The function is called with NEWSGROUP, HEADERS, and optional
449 LAST-FILE."
450   :group 'gnus-article-saving
451   :type 'function)
452
453 (defcustom gnus-split-methods
454   '((gnus-article-archive-name)
455     (gnus-article-nndoc-name))
456   "*Variable used to suggest where articles are to be saved.
457 For instance, if you would like to save articles related to Gnus in
458 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
459 you could set this variable to something like:
460
461  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
462    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
463
464 This variable is an alist where the where the key is the match and the
465 value is a list of possible files to save in if the match is non-nil.
466
467 If the match is a string, it is used as a regexp match on the
468 article.  If the match is a symbol, that symbol will be funcalled
469 from the buffer of the article to be saved with the newsgroup as the
470 parameter.  If it is a list, it will be evaled in the same buffer.
471
472 If this form or function returns a string, this string will be used as
473 a possible file name; and if it returns a non-nil list, that list will
474 be used as possible file names."
475   :group 'gnus-article-saving
476   :type '(repeat (choice (list :value (fun) function)
477                          (cons :value ("" "") regexp (repeat string))
478                          (sexp :value nil))))
479
480 (defcustom gnus-article-display-method-for-mime
481   'gnus-article-display-mime-message
482   "Function to display a MIME message.
483 The function is called from the article buffer."
484   :group 'gnus-article-mime
485   :type 'function)
486
487 (defcustom gnus-article-display-method-for-traditional
488   'gnus-article-display-traditional-message
489   "*Function to display a traditional message.
490 The function is called from the article buffer."
491   :group 'gnus-article-mime
492   :type 'function)
493
494 (defcustom gnus-page-delimiter "^\^L"
495   "*Regexp describing what to use as article page delimiters.
496 The default value is \"^\^L\", which is a form linefeed at the
497 beginning of a line."
498   :type 'regexp
499   :group 'gnus-article-various)
500
501 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
502   "*The format specification for the article mode line.
503 See `gnus-summary-mode-line-format' for a closer description.
504
505 The following additional specs are available:
506
507 %w  The article washing status.
508 %m  The number of MIME parts in the article."
509   :type 'string
510   :group 'gnus-article-various)
511
512 (defcustom gnus-article-mode-hook nil
513   "*A hook for Gnus article mode."
514   :type 'hook
515   :group 'gnus-article-various)
516
517 (when (featurep 'xemacs)
518   ;; Extracted from gnus-xmas-define in order to preserve user settings
519   (when (fboundp 'turn-off-scroll-in-place)
520     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
521   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
522   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
523
524 (defcustom gnus-article-menu-hook nil
525   "*Hook run after the creation of the article mode menu."
526   :type 'hook
527   :group 'gnus-article-various)
528
529 (defcustom gnus-article-prepare-hook nil
530   "*A hook called after an article has been prepared in the article buffer."
531   :type 'hook
532   :group 'gnus-article-various)
533
534 (defcustom gnus-article-hide-pgp-hook nil
535   "*A hook called after successfully hiding a PGP signature."
536   :type 'hook
537   :group 'gnus-article-various)
538
539 (defcustom gnus-article-button-face 'bold
540   "Face used for highlighting buttons in the article buffer.
541
542 An article button is a piece of text that you can activate by pressing
543 `RET' or `mouse-2' above it."
544   :type 'face
545   :group 'gnus-article-buttons)
546
547 (defcustom gnus-article-mouse-face 'highlight
548   "Face used for mouse highlighting in the article buffer.
549
550 Article buttons will be displayed in this face when the cursor is
551 above them."
552   :type 'face
553   :group 'gnus-article-buttons)
554
555 (defcustom gnus-signature-face 'gnus-signature-face
556   "Face used for highlighting a signature in the article buffer.
557 Obsolete; use the face `gnus-signature-face' for customizations instead."
558   :type 'face
559   :group 'gnus-article-highlight
560   :group 'gnus-article-signature)
561
562 (defface gnus-signature-face
563   '((t
564      (:italic t)))
565   "Face used for highlighting a signature in the article buffer."
566   :group 'gnus-article-highlight
567   :group 'gnus-article-signature)
568
569 (defface gnus-header-from-face
570   '((((class color)
571       (background dark))
572      (:foreground "spring green"))
573     (((class color)
574       (background light))
575      (:foreground "red3"))
576     (t
577      (:italic t)))
578   "Face used for displaying from headers."
579   :group 'gnus-article-headers
580   :group 'gnus-article-highlight)
581
582 (defface gnus-header-subject-face
583   '((((class color)
584       (background dark))
585      (:foreground "SeaGreen3"))
586     (((class color)
587       (background light))
588      (:foreground "red4"))
589     (t
590      (:bold t :italic t)))
591   "Face used for displaying subject headers."
592   :group 'gnus-article-headers
593   :group 'gnus-article-highlight)
594
595 (defface gnus-header-newsgroups-face
596   '((((class color)
597       (background dark))
598      (:foreground "yellow" :italic t))
599     (((class color)
600       (background light))
601      (:foreground "MidnightBlue" :italic t))
602     (t
603      (:italic t)))
604   "Face used for displaying newsgroups headers."
605   :group 'gnus-article-headers
606   :group 'gnus-article-highlight)
607
608 (defface gnus-header-name-face
609   '((((class color)
610       (background dark))
611      (:foreground "SeaGreen"))
612     (((class color)
613       (background light))
614      (:foreground "maroon"))
615     (t
616      (:bold t)))
617   "Face used for displaying header names."
618   :group 'gnus-article-headers
619   :group 'gnus-article-highlight)
620
621 (defface gnus-header-content-face
622   '((((class color)
623       (background dark))
624      (:foreground "forest green" :italic t))
625     (((class color)
626       (background light))
627      (:foreground "indianred4" :italic t))
628     (t
629      (:italic t)))  "Face used for displaying header content."
630   :group 'gnus-article-headers
631   :group 'gnus-article-highlight)
632
633 (defcustom gnus-header-face-alist
634   '(("From" nil gnus-header-from-face)
635     ("Subject" nil gnus-header-subject-face)
636     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
637     ("" gnus-header-name-face gnus-header-content-face))
638   "*Controls highlighting of article header.
639
640 An alist of the form (HEADER NAME CONTENT).
641
642 HEADER is a regular expression which should match the name of an
643 header header and NAME and CONTENT are either face names or nil.
644
645 The name of each header field will be displayed using the face
646 specified by the first element in the list where HEADER match the
647 header name and NAME is non-nil.  Similarly, the content will be
648 displayed by the first non-nil matching CONTENT face."
649   :group 'gnus-article-headers
650   :group 'gnus-article-highlight
651   :type '(repeat (list (regexp :tag "Header")
652                        (choice :tag "Name"
653                                (item :tag "skip" nil)
654                                (face :value default))
655                        (choice :tag "Content"
656                                (item :tag "skip" nil)
657                                (face :value default)))))
658
659 (defcustom gnus-article-decode-hook nil
660   "*Hook run to decode charsets in articles."
661   :group 'gnus-article-headers
662   :type 'hook)
663
664 (defcustom gnus-display-mime-function 'gnus-display-mime
665   "Function to display MIME articles."
666   :group 'gnus-article-mime
667   :type 'function)
668
669 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
670   "Function used to decode headers.")
671
672 (defvar gnus-article-dumbquotes-map
673   '(("\200" "EUR")
674     ("\202" ",")
675     ("\203" "f")
676     ("\204" ",,")
677     ("\205" "...")
678     ("\213" "<")
679     ("\214" "OE")
680     ("\221" "`")
681     ("\222" "'")
682     ("\223" "``")
683     ("\224" "\"")
684     ("\225" "*")
685     ("\226" "-")
686     ("\227" "--")
687     ("\231" "(TM)")
688     ("\233" ">")
689     ("\234" "oe")
690     ("\264" "'"))
691   "Table for MS-to-Latin1 translation.")
692
693 (defcustom gnus-ignored-mime-types nil
694   "List of MIME types that should be ignored by Gnus."
695   :version "21.1"
696   :group 'gnus-article-mime
697   :type '(repeat regexp))
698
699 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
700   "List of MIME types that should not be given buttons when rendered inline."
701   :version "21.1"
702   :group 'gnus-article-mime
703   :type '(repeat regexp))
704
705 (defcustom gnus-article-mime-part-function nil
706   "Function called with a MIME handle as the argument.
707 This is meant for people who want to do something automatic based
708 on parts -- for instance, adding Vcard info to a database."
709   :group 'gnus-article-mime
710   :type 'function)
711
712 (defcustom gnus-mime-multipart-functions nil
713   "An alist of MIME types to functions to display them."
714   :version "21.1"
715   :group 'gnus-article-mime
716   :type 'alist)
717
718 (defcustom gnus-article-date-lapsed-new-header nil
719   "Whether the X-Sent and Date headers can coexist.
720 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
721 either replace the old \"Date:\" header (if this variable is nil), or
722 be added below it (otherwise)."
723   :version "21.1"
724   :group 'gnus-article-headers
725   :type 'boolean)
726
727 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
728   "Function called with a MIME handle as the argument.
729 This is meant for people who want to view first matched part.
730 For `undisplayed-alternative' (default), the first undisplayed
731 part or alternative part is used.  For `undisplayed', the first
732 undisplayed part is used.  For a function, the first part which
733 the function return `t' is used.  For `nil', the first part is
734 used."
735   :version "21.1"
736   :group 'gnus-article-mime
737   :type '(choice
738           (item :tag "first" :value nil)
739           (item :tag "undisplayed" :value undisplayed)
740           (item :tag "undisplayed or alternative"
741                 :value undisplayed-alternative)
742           (function)))
743
744 (defcustom gnus-mime-action-alist
745   '(("save to file" . gnus-mime-save-part)
746     ("save and strip" . gnus-mime-save-part-and-strip)
747     ("display as text" . gnus-mime-inline-part)
748     ("view the part" . gnus-mime-view-part)
749     ("pipe to command" . gnus-mime-pipe-part)
750     ("toggle display" . gnus-article-press-button)
751     ("toggle display" . gnus-article-view-part-as-charset)
752     ("view as type" . gnus-mime-view-part-as-type)
753     ("internalize type" . gnus-mime-internalize-part)
754     ("externalize type" . gnus-mime-externalize-part))
755   "An alist of actions that run on the MIME attachment."
756   :group 'gnus-article-mime
757   :type '(repeat (cons (string :tag "name")
758                        (function))))
759
760 (defcustom gnus-mime-action-alist
761   '(("save to file" . gnus-mime-save-part)
762     ("display as text" . gnus-mime-inline-part)
763     ("view the part" . gnus-mime-view-part)
764     ("pipe to command" . gnus-mime-pipe-part)
765     ("toggle display" . gnus-article-press-button)
766     ("view as type" . gnus-mime-view-part-as-type)
767     ("internalize type" . gnus-mime-internalize-part)
768     ("externalize type" . gnus-mime-externalize-part))
769   "An alist of actions that run on the MIME attachment."
770   :version "21.1"
771   :group 'gnus-article-mime
772   :type '(repeat (cons (string :tag "name")
773                        (function))))
774
775 ;;;
776 ;;; The treatment variables
777 ;;;
778
779 (defvar gnus-part-display-hook nil
780   "Hook called on parts that are to receive treatment.")
781
782 (defvar gnus-article-treat-custom
783   '(choice (const :tag "Off" nil)
784            (const :tag "On" t)
785            (const :tag "Header" head)
786            (const :tag "Last" last)
787            (const :tag "Mime" mime)
788            (integer :tag "Less")
789            (repeat :tag "Groups" regexp)
790            (sexp :tag "Predicate")))
791
792 (defvar gnus-article-treat-head-custom
793   '(choice (const :tag "Off" nil)
794            (const :tag "Header" head)))
795
796 (defvar gnus-article-treat-types '("text/plain")
797   "Parts to treat.")
798
799 (defvar gnus-inhibit-treatment nil
800   "Whether to inhibit treatment.")
801
802 (defcustom gnus-treat-highlight-signature '(or last (typep "text/x-vcard"))
803   "Highlight the signature.
804 Valid values are nil, t, `head', `last', an integer or a predicate.
805 See the manual for details."
806   :group 'gnus-article-treat
807   :type gnus-article-treat-custom)
808 (put 'gnus-treat-highlight-signature 'highlight t)
809
810 (defcustom gnus-treat-buttonize 100000
811   "Add buttons.
812 Valid values are nil, t, `head', `last', an integer or a predicate.
813 See the manual for details."
814   :group 'gnus-article-treat
815   :type gnus-article-treat-custom)
816 (put 'gnus-treat-buttonize 'highlight t)
817
818 (defcustom gnus-treat-buttonize-head 'head
819   "Add buttons to the head.
820 Valid values are nil, t, `head', `last', an integer or a predicate.
821 See the manual for details."
822   :group 'gnus-article-treat
823   :type gnus-article-treat-head-custom)
824 (put 'gnus-treat-buttonize-head 'highlight t)
825
826 (defcustom gnus-treat-emphasize
827   (and (or window-system
828            (featurep 'xemacs)
829            (>= (string-to-number emacs-version) 21))
830        50000)
831   "Emphasize text.
832 Valid values are nil, t, `head', `last', an integer or a predicate.
833 See the manual for details."
834   :group 'gnus-article-treat
835   :type gnus-article-treat-custom)
836 (put 'gnus-treat-emphasize 'highlight t)
837
838 (defcustom gnus-treat-strip-cr nil
839   "Remove carriage returns.
840 Valid values are nil, t, `head', `last', an integer or a predicate.
841 See the manual for details."
842   :group 'gnus-article-treat
843   :type gnus-article-treat-custom)
844
845 (defcustom gnus-treat-leading-whitespace nil
846   "Remove leading whitespace in headers.
847 Valid values are nil, t, `head', `last', an integer or a predicate.
848 See the manual for details."
849   :group 'gnus-article-treat
850   :type gnus-article-treat-custom)
851
852 (defcustom gnus-treat-hide-headers 'head
853   "Hide headers.
854 Valid values are nil, t, `head', `last', an integer or a predicate.
855 See the manual for details."
856   :group 'gnus-article-treat
857   :type gnus-article-treat-head-custom)
858
859 (defcustom gnus-treat-hide-boring-headers nil
860   "Hide boring headers.
861 Valid values are nil, t, `head', `last', an integer or a predicate.
862 See the manual for details."
863   :group 'gnus-article-treat
864   :type gnus-article-treat-head-custom)
865
866 (defcustom gnus-treat-hide-signature nil
867   "Hide the signature.
868 Valid values are nil, t, `head', `last', an integer or a predicate.
869 See the manual for details."
870   :group 'gnus-article-treat
871   :type gnus-article-treat-custom)
872
873 (defcustom gnus-treat-fill-article nil
874   "Fill the article.
875 Valid values are nil, t, `head', `last', an integer or a predicate.
876 See the manual for details."
877   :group 'gnus-article-treat
878   :type gnus-article-treat-custom)
879
880 (defcustom gnus-treat-hide-citation nil
881   "Hide cited text.
882 Valid values are nil, t, `head', `last', an integer or a predicate.
883 See the manual for details."
884   :group 'gnus-article-treat
885   :type gnus-article-treat-custom)
886
887 (defcustom gnus-treat-hide-citation-maybe nil
888   "Hide cited text.
889 Valid values are nil, t, `head', `last', an integer or a predicate.
890 See the manual for details."
891   :group 'gnus-article-treat
892   :type gnus-article-treat-custom)
893
894 (defcustom gnus-treat-hide-citation-maybe nil
895   "Hide cited text.
896 Valid values are nil, t, `head', `last', an integer or a predicate.
897 See the manual for details."
898   :group 'gnus-article-treat
899   :type gnus-article-treat-custom)
900
901 (defcustom gnus-treat-strip-list-identifiers 'head
902   "Strip list identifiers from `gnus-list-identifiers`.
903 Valid values are nil, t, `head', `last', an integer or a predicate.
904 See the manual for details."
905   :version "21.1"
906   :group 'gnus-article-treat
907   :type gnus-article-treat-custom)
908
909 (defcustom gnus-treat-strip-pgp t
910   "Strip PGP signatures.
911 Valid values are nil, t, `head', `last', an integer or a predicate.
912 See the manual for details."
913   :group 'gnus-article-treat
914   :type gnus-article-treat-custom)
915
916 (defcustom gnus-treat-strip-pem nil
917   "Strip PEM signatures.
918 Valid values are nil, t, `head', `last', an integer or a predicate.
919 See the manual for details."
920   :group 'gnus-article-treat
921   :type gnus-article-treat-custom)
922
923 (defcustom gnus-treat-strip-banner t
924   "Strip banners from articles.
925 The banner to be stripped is specified in the `banner' group parameter.
926 Valid values are nil, t, `head', `last', an integer or a predicate.
927 See the manual for details."
928   :group 'gnus-article-treat
929   :type gnus-article-treat-custom)
930
931 (defcustom gnus-treat-highlight-headers 'head
932   "Highlight the headers.
933 Valid values are nil, t, `head', `last', an integer or a predicate.
934 See the manual for details."
935   :group 'gnus-article-treat
936   :type gnus-article-treat-head-custom)
937 (put 'gnus-treat-highlight-headers 'highlight t)
938
939 (defcustom gnus-treat-highlight-citation t
940   "Highlight cited text.
941 Valid values are nil, t, `head', `last', an integer or a predicate.
942 See the manual for details."
943   :group 'gnus-article-treat
944   :type gnus-article-treat-custom)
945 (put 'gnus-treat-highlight-citation 'highlight t)
946
947 (defcustom gnus-treat-date-ut nil
948   "Display the Date in UT (GMT).
949 Valid values are nil, t, `head', `last', an integer or a predicate.
950 See the manual for details."
951   :group 'gnus-article-treat
952   :type gnus-article-treat-head-custom)
953
954 (defcustom gnus-treat-date-local nil
955   "Display the Date in the local timezone.
956 Valid values are nil, t, `head', `last', an integer or a predicate.
957 See the manual for details."
958   :group 'gnus-article-treat
959   :type gnus-article-treat-head-custom)
960
961 (defcustom gnus-treat-date-english nil
962   "Display the Date in a format that can be read aloud in English.
963 Valid values are nil, t, `head', `last', an integer or a predicate.
964 See the manual for details."
965   :group 'gnus-article-treat
966   :type gnus-article-treat-head-custom)
967
968 (defcustom gnus-treat-date-lapsed nil
969   "Display the Date header in a way that says how much time has elapsed.
970 Valid values are nil, t, `head', `last', an integer or a predicate.
971 See the manual for details."
972   :group 'gnus-article-treat
973   :type gnus-article-treat-head-custom)
974
975 (defcustom gnus-treat-date-original nil
976   "Display the date in the original timezone.
977 Valid values are nil, t, `head', `last', an integer or a predicate.
978 See the manual for details."
979   :group 'gnus-article-treat
980   :type gnus-article-treat-head-custom)
981
982 (defcustom gnus-treat-date-iso8601 nil
983   "Display the date in the ISO8601 format.
984 Valid values are nil, t, `head', `last', an integer or a predicate.
985 See the manual for details."
986   :version "21.1"
987   :group 'gnus-article-treat
988   :type gnus-article-treat-head-custom)
989
990 (defcustom gnus-treat-date-user-defined nil
991   "Display the date in a user-defined format.
992 The format is defined by the `gnus-article-time-format' variable.
993 Valid values are nil, t, `head', `last', an integer or a predicate.
994 See the manual for details."
995   :group 'gnus-article-treat
996   :type gnus-article-treat-head-custom)
997
998 (defcustom gnus-treat-strip-headers-in-body t
999   "Strip the X-No-Archive header line from the beginning of the body.
1000 Valid values are nil, t, `head', `last', an integer or a predicate.
1001 See the manual for details."
1002   :version "21.1"
1003   :group 'gnus-article-treat
1004   :type gnus-article-treat-custom)
1005
1006 (defcustom gnus-treat-strip-trailing-blank-lines nil
1007   "Strip trailing blank lines.
1008 Valid values are nil, t, `head', `last', an integer or a predicate.
1009 See the manual for details."
1010   :group 'gnus-article-treat
1011   :type gnus-article-treat-custom)
1012
1013 (defcustom gnus-treat-strip-leading-blank-lines nil
1014   "Strip leading blank lines.
1015 Valid values are nil, t, `head', `last', an integer or a predicate.
1016 See the manual for details."
1017   :group 'gnus-article-treat
1018   :type gnus-article-treat-custom)
1019
1020 (defcustom gnus-treat-strip-multiple-blank-lines nil
1021   "Strip multiple blank lines.
1022 Valid values are nil, t, `head', `last', an integer or a predicate.
1023 See the manual for details."
1024   :group 'gnus-article-treat
1025   :type gnus-article-treat-custom)
1026
1027 (defcustom gnus-treat-overstrike t
1028   "Treat overstrike highlighting.
1029 Valid values are nil, t, `head', `last', an integer or a predicate.
1030 See the manual for details."
1031   :group 'gnus-article-treat
1032   :type gnus-article-treat-custom)
1033 (put 'gnus-treat-overstrike 'highlight t)
1034
1035 (defcustom gnus-treat-display-xface
1036   (and (or (and (fboundp 'image-type-available-p)
1037                 (image-type-available-p 'xbm)
1038                 (string-match "^0x" (shell-command-to-string "uncompface")))
1039            (and (featurep 'xemacs) (featurep 'xface))
1040            (eq 'x-face-mule-gnus-article-display-x-face
1041                gnus-article-x-face-command))
1042        'head)
1043   "Display X-Face headers.
1044 Valid values are nil, t, `head', `last', an integer or a predicate.
1045 See the manual for details."
1046   :group 'gnus-article-treat
1047   ;;:version "21.1"
1048   :type gnus-article-treat-head-custom)
1049 (put 'gnus-treat-display-xface 'highlight t)
1050
1051 (defcustom gnus-treat-display-smileys
1052   (if (or (and (featurep 'xemacs)
1053                (featurep 'xpm))
1054           (and (fboundp 'image-type-available-p)
1055                (image-type-available-p 'pbm))
1056           (and (not (featurep 'xemacs))
1057                window-system
1058                (module-installed-p 'smiley-mule)))
1059       t
1060     nil)
1061   "Display smileys.
1062 Valid values are nil, t, `head', `last', an integer or a predicate.
1063 See the manual for details."
1064   :group 'gnus-article-treat
1065   ;;:version "21.1"
1066   :type gnus-article-treat-custom)
1067 (put 'gnus-treat-display-smileys 'highlight t)
1068
1069 (defcustom gnus-treat-display-picons (if (featurep 'xemacs) 'head nil)
1070   "Display picons.
1071 Valid values are nil, t, `head', `last', an integer or a predicate.
1072 See the manual for details."
1073   :group 'gnus-article-treat
1074   :type gnus-article-treat-head-custom)
1075 (put 'gnus-treat-display-picons 'highlight t)
1076
1077 (defcustom gnus-treat-capitalize-sentences nil
1078   "Capitalize sentence-starting words.
1079 Valid values are nil, t, `head', `last', an integer or a predicate.
1080 See the manual for details."
1081   :version "21.1"
1082   :group 'gnus-article-treat
1083   :type gnus-article-treat-custom)
1084
1085 (defcustom gnus-treat-fill-long-lines nil
1086   "Fill long lines.
1087 Valid values are nil, t, `head', `last', an integer or a predicate.
1088 See the manual for details."
1089   :group 'gnus-article-treat
1090   :type gnus-article-treat-custom)
1091
1092 (defcustom gnus-treat-play-sounds nil
1093   "Play sounds.
1094 Valid values are nil, t, `head', `last', an integer or a predicate.
1095 See the manual for details."
1096   :version "21.1"
1097   :group 'gnus-article-treat
1098   :type gnus-article-treat-custom)
1099
1100 (defcustom gnus-treat-decode-article-as-default-mime-charset nil
1101   "Decode an article as `default-mime-charset'.  For instance, if you want to
1102 attempt to decode an article even if the value of `gnus-show-mime' is nil,
1103 you could set this variable to something like: nil for don't decode, t for
1104 decode the body, '(or header t) for the whole article, etc."
1105   :group 'gnus-article-treat
1106   :type '(radio (const :tag "Off" nil)
1107                 (const :tag "Decode body" t)
1108                 (const :tag "Decode all" (or head t))))
1109
1110 (defcustom gnus-treat-translate nil
1111   "Translate articles from one language to another.
1112 Valid values are nil, t, `head', `last', an integer or a predicate.
1113 See the manual for details."
1114   :version "21.1"
1115   :group 'gnus-article-treat
1116   :type gnus-article-treat-custom)
1117
1118 (defcustom gnus-treat-x-pgp-sig nil
1119   "Verify X-PGP-Sig.
1120 To automatically treat X-PGP-Sig, set it to head.
1121 Valid values are nil, t, `head', `last', an integer or a predicate.
1122 See the manual for details."
1123   :group 'gnus-article-treat
1124   :group 'mime-security
1125   :type gnus-article-treat-custom)
1126
1127 (defvar gnus-article-encrypt-protocol-alist
1128   '(("PGP" . mml2015-self-encrypt)))
1129
1130 ;; Set to nil if more than one protocol added to
1131 ;; gnus-article-encrypt-protocol-alist.
1132 (defcustom gnus-article-encrypt-protocol "PGP"
1133   "The protocol used for encrypt articles.
1134 It is a string, such as \"PGP\". If nil, ask user."
1135   :type 'string
1136   :group 'mime-security)
1137
1138 ;;; Internal variables
1139
1140 (defvar gnus-english-month-names
1141   '("January" "February" "March" "April" "May" "June" "July" "August"
1142     "September" "October" "November" "December"))
1143
1144 (defvar article-goto-body-goes-to-point-min-p nil)
1145 (defvar gnus-article-wash-types nil)
1146 (defvar gnus-article-emphasis-alist nil)
1147
1148 (defvar gnus-article-mime-handle-alist-1 nil)
1149 (defvar gnus-treatment-function-alist
1150   `((gnus-treat-decode-article-as-default-mime-charset
1151      gnus-article-decode-article-as-default-mime-charset)
1152     (gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1153     (gnus-treat-strip-banner gnus-article-strip-banner)
1154     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1155     (gnus-treat-buttonize gnus-article-add-buttons)
1156     (gnus-treat-fill-article gnus-article-fill-cited-article)
1157     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1158     (gnus-treat-strip-cr gnus-article-remove-cr)
1159     (gnus-treat-display-xface gnus-article-display-x-face)
1160     (gnus-treat-date-ut gnus-article-date-ut)
1161     (gnus-treat-date-local gnus-article-date-local)
1162     (gnus-treat-date-english gnus-article-date-english)
1163     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1164     (gnus-treat-date-original gnus-article-date-original)
1165     (gnus-treat-date-user-defined gnus-article-date-user)
1166     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1167     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1168     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1169     (gnus-treat-hide-signature gnus-article-hide-signature)
1170     (gnus-treat-hide-citation gnus-article-hide-citation)
1171     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1172     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1173     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1174     (gnus-treat-strip-pgp gnus-article-hide-pgp)
1175     (gnus-treat-strip-pem gnus-article-hide-pem)
1176     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1177     (gnus-treat-emphasize gnus-article-emphasize)
1178     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1179     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1180     (gnus-treat-strip-trailing-blank-lines
1181      gnus-article-remove-trailing-blank-lines)
1182     (gnus-treat-strip-leading-blank-lines
1183      gnus-article-strip-leading-blank-lines)
1184     (gnus-treat-strip-multiple-blank-lines
1185      gnus-article-strip-multiple-blank-lines)
1186     (gnus-treat-overstrike gnus-article-treat-overstrike)
1187     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1188     (gnus-treat-display-smileys ,(if (or (featurep 'xemacs)
1189                                          (>= emacs-major-version 21))
1190                                      'gnus-smiley-display
1191                                    'gnus-article-smiley-display))
1192     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1193     (gnus-treat-display-picons gnus-article-display-picons)
1194     (gnus-treat-play-sounds gnus-earcon-display)))
1195
1196 (defvar gnus-article-mime-handle-alist nil)
1197 (defvar article-lapsed-timer nil)
1198 (defvar gnus-article-current-summary nil)
1199
1200 (defvar gnus-article-mode-syntax-table
1201   (let ((table (copy-syntax-table text-mode-syntax-table)))
1202     ;; This causes the citation match run O(2^n).
1203     ;; (modify-syntax-entry ?- "w" table)
1204     (modify-syntax-entry ?> ")" table)
1205     (modify-syntax-entry ?< "(" table)
1206     table)
1207   "Syntax table used in article mode buffers.
1208 Initialized from `text-mode-syntax-table.")
1209
1210 (defvar gnus-save-article-buffer nil)
1211
1212 (defvar gnus-article-mode-line-format-alist
1213   (nconc '((?w (gnus-article-wash-status) ?s)
1214            (?m (gnus-article-mime-part-status) ?s))
1215          gnus-summary-mode-line-format-alist))
1216
1217 (defvar gnus-number-of-articles-to-be-saved nil)
1218
1219 (defvar gnus-inhibit-hiding nil)
1220
1221 (defsubst gnus-article-hide-text (b e props)
1222   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1223   (gnus-add-text-properties-when 'article-type nil b e props)
1224   (when (memq 'intangible props)
1225     (put-text-property
1226      (max (1- b) (point-min))
1227      b 'intangible (cddr (memq 'intangible props)))))
1228
1229 (defsubst gnus-article-unhide-text (b e)
1230   "Remove hidden text properties from region between B and E."
1231   (remove-text-properties b e gnus-hidden-properties)
1232   (when (memq 'intangible gnus-hidden-properties)
1233     (put-text-property (max (1- b) (point-min))
1234                        b 'intangible nil)))
1235
1236 (defun gnus-article-hide-text-type (b e type)
1237   "Hide text of TYPE between B and E."
1238   (push type gnus-article-wash-types)
1239   (gnus-article-hide-text
1240    b e (cons 'article-type (cons type gnus-hidden-properties))))
1241
1242 (defun gnus-article-unhide-text-type (b e type)
1243   "Unhide text of TYPE between B and E."
1244   (setq gnus-article-wash-types
1245         (delq type gnus-article-wash-types))
1246   (remove-text-properties
1247    b e (cons 'article-type (cons type gnus-hidden-properties)))
1248   (when (memq 'intangible gnus-hidden-properties)
1249     (put-text-property (max (1- b) (point-min))
1250                        b 'intangible nil)))
1251
1252 (defun gnus-article-hide-text-of-type (type)
1253   "Hide text of TYPE in the current buffer."
1254   (save-excursion
1255     (let ((b (point-min))
1256           (e (point-max)))
1257       (while (setq b (text-property-any b e 'article-type type))
1258         (add-text-properties b (incf b) gnus-hidden-properties)))))
1259
1260 (defun gnus-article-delete-text-of-type (type)
1261   "Delete text of TYPE in the current buffer."
1262   (save-excursion
1263     (let ((b (point-min)))
1264       (while (setq b (text-property-any b (point-max) 'article-type type))
1265         (delete-region
1266          b (or (text-property-not-all b (point-max) 'article-type type)
1267                (point-max)))))))
1268
1269 (defun gnus-article-delete-invisible-text ()
1270   "Delete all invisible text in the current buffer."
1271   (save-excursion
1272     (let ((b (point-min)))
1273       (while (setq b (text-property-any b (point-max) 'invisible t))
1274         (delete-region
1275          b (or (text-property-not-all b (point-max) 'invisible t)
1276                (point-max)))))))
1277
1278 (defun gnus-article-text-type-exists-p (type)
1279   "Say whether any text of type TYPE exists in the buffer."
1280   (text-property-any (point-min) (point-max) 'article-type type))
1281
1282 (defsubst gnus-article-header-rank ()
1283   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1284   (let ((list gnus-sorted-header-list)
1285         (i 0))
1286     (while list
1287       (when (looking-at (car list))
1288         (setq list nil))
1289       (setq list (cdr list))
1290       (incf i))
1291     i))
1292
1293 (defun article-hide-headers (&optional arg delete)
1294   "Hide unwanted headers and possibly sort them as well."
1295   (interactive (gnus-article-hidden-arg))
1296   ;; Lars said that this function might be inhibited.
1297   (if (gnus-article-check-hidden-text 'headers arg)
1298       (progn
1299         ;; Show boring headers as well.
1300         (gnus-article-show-hidden-text 'boring-headers)
1301         (when (eq 1 (point-min))
1302           (set-window-start (get-buffer-window (current-buffer)) 1)))
1303   (unless gnus-inhibit-hiding
1304     (save-excursion
1305       (save-restriction
1306         (let ((inhibit-read-only t)
1307               (case-fold-search t)
1308               (max (1+ (length gnus-sorted-header-list)))
1309               (ignored (when (not gnus-visible-headers)
1310                          (cond ((stringp gnus-ignored-headers)
1311                                 gnus-ignored-headers)
1312                                ((listp gnus-ignored-headers)
1313                                 (mapconcat 'identity gnus-ignored-headers
1314                                            "\\|")))))
1315               (visible
1316                (cond ((stringp gnus-visible-headers)
1317                       gnus-visible-headers)
1318                      ((and gnus-visible-headers
1319                            (listp gnus-visible-headers))
1320                       (mapconcat 'identity gnus-visible-headers "\\|"))))
1321               (inhibit-point-motion-hooks t)
1322               beg)
1323           ;; First we narrow to just the headers.
1324           (article-narrow-to-head)
1325           ;; Hide any "From " lines at the beginning of (mail) articles.
1326           (while (looking-at "From ")
1327             (forward-line 1))
1328           (unless (bobp)
1329             (if delete
1330                 (delete-region (point-min) (point))
1331               (gnus-article-hide-text (point-min) (point)
1332                                       (nconc (list 'article-type 'headers)
1333                                              gnus-hidden-properties))))
1334           ;; Then treat the rest of the header lines.
1335           ;; Then we use the two regular expressions
1336           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1337           ;; select which header lines is to remain visible in the
1338           ;; article buffer.
1339           (while (re-search-forward "^[^ \t]*:" nil t)
1340             (beginning-of-line)
1341             ;; Mark the rank of the header.
1342             (put-text-property
1343              (point) (1+ (point)) 'message-rank
1344              (if (or (and visible (looking-at visible))
1345                      (and ignored
1346                           (not (looking-at ignored))))
1347                  (gnus-article-header-rank)
1348                (+ 2 max)))
1349             (forward-line 1))
1350           (message-sort-headers-1)
1351           (when (setq beg (text-property-any
1352                            (point-min) (point-max) 'message-rank (+ 2 max)))
1353             ;; We delete or make invisible the unwanted headers.
1354             (push 'headers gnus-article-wash-types)
1355             (if delete
1356                 (progn
1357                   (add-text-properties
1358                    (point-min) (+ 5 (point-min))
1359                    '(article-type headers dummy-invisible t))
1360                   (delete-region beg (point-max)))
1361               (gnus-article-hide-text-type beg (point-max) 'headers))))))))
1362   )
1363
1364 (defun article-hide-boring-headers (&optional arg)
1365   "Toggle hiding of headers that aren't very interesting.
1366 If given a negative prefix, always show; if given a positive prefix,
1367 always hide."
1368   (interactive (gnus-article-hidden-arg))
1369   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1370              (not gnus-show-all-headers))
1371     (save-excursion
1372       (save-restriction
1373         (let ((buffer-read-only nil)
1374               (list gnus-boring-article-headers)
1375               (inhibit-point-motion-hooks t)
1376               elem)
1377           (article-narrow-to-head)
1378           (while list
1379             (setq elem (pop list))
1380             (goto-char (point-min))
1381             (cond
1382              ;; Hide empty headers.
1383              ((eq elem 'empty)
1384               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1385                 (forward-line -1)
1386                 (gnus-article-hide-text-type
1387                  (progn (beginning-of-line) (point))
1388                  (progn
1389                    (end-of-line)
1390                    (if (re-search-forward "^[^ \t]" nil t)
1391                        (match-beginning 0)
1392                      (point-max)))
1393                  'boring-headers)))
1394              ;; Hide boring Newsgroups header.
1395              ((eq elem 'newsgroups)
1396               (when (gnus-string-equal
1397                      (gnus-fetch-field "newsgroups")
1398                      (gnus-group-real-name
1399                       (if (boundp 'gnus-newsgroup-name)
1400                           gnus-newsgroup-name
1401                         "")))
1402                 (gnus-article-hide-header "newsgroups")))
1403              ((eq elem 'to-address)
1404               (let ((to (message-fetch-field "to"))
1405                     (to-address
1406                      (gnus-parameter-to-address
1407                       (if (boundp 'gnus-newsgroup-name)
1408                           gnus-newsgroup-name ""))))
1409                 (when (and to to-address
1410                            (ignore-errors
1411                              (gnus-string-equal
1412                               ;; only one address in To
1413                               (nth 1 (mail-extract-address-components to))
1414                               to-address)))
1415                   (gnus-article-hide-header "to"))))
1416              ((eq elem 'followup-to)
1417               (when (gnus-string-equal
1418                      (message-fetch-field "followup-to")
1419                      (message-fetch-field "newsgroups"))
1420                 (gnus-article-hide-header "followup-to")))
1421              ((eq elem 'reply-to)
1422               (let ((from (message-fetch-field "from"))
1423                     (reply-to (message-fetch-field "reply-to")))
1424                 (when (and
1425                        from reply-to
1426                        (ignore-errors
1427                          (gnus-string-equal
1428                           (nth 1 (mail-extract-address-components from))
1429                           (nth 1 (mail-extract-address-components reply-to)))))
1430                   (gnus-article-hide-header "reply-to"))))
1431              ((eq elem 'date)
1432               (let ((date (message-fetch-field "date")))
1433                 (when (and date
1434                            (< (days-between (current-time-string) date)
1435                               4))
1436                   (gnus-article-hide-header "date"))))
1437              ((eq elem 'long-to)
1438               (let ((to (message-fetch-field "to"))
1439                     (cc (message-fetch-field "cc")))
1440                 (when (> (length to) 1024)
1441                   (gnus-article-hide-header "to"))
1442                 (when (> (length cc) 1024)
1443                   (gnus-article-hide-header "cc"))))
1444              ((eq elem 'many-to)
1445               (let ((to-count 0)
1446                     (cc-count 0))
1447                 (goto-char (point-min))
1448                 (while (re-search-forward "^to:" nil t)
1449                   (setq to-count (1+ to-count)))
1450                 (when (> to-count 1)
1451                   (while (> to-count 0)
1452                     (goto-char (point-min))
1453                     (save-restriction
1454                       (re-search-forward "^to:" nil nil to-count)
1455                       (forward-line -1)
1456                       (narrow-to-region (point) (point-max))
1457                       (gnus-article-hide-header "to"))
1458                     (setq to-count (1- to-count))))
1459                 (goto-char (point-min))
1460                 (while (re-search-forward "^cc:" nil t)
1461                   (setq cc-count (1+ cc-count)))
1462                 (when (> cc-count 1)
1463                   (while (> cc-count 0)
1464                     (goto-char (point-min))
1465                     (save-restriction
1466                       (re-search-forward "^cc:" nil nil cc-count)
1467                       (forward-line -1)
1468                       (narrow-to-region (point) (point-max))
1469                       (gnus-article-hide-header "cc"))
1470                     (setq cc-count (1- cc-count)))))))))))))
1471
1472 (defun gnus-article-hide-header (header)
1473   (save-excursion
1474     (goto-char (point-min))
1475     (when (re-search-forward (concat "^" header ":") nil t)
1476       (gnus-article-hide-text-type
1477        (progn (beginning-of-line) (point))
1478        (progn
1479          (end-of-line)
1480          (if (re-search-forward "^[^ \t]" nil t)
1481              (match-beginning 0)
1482            (point-max)))
1483        'boring-headers))))
1484
1485 (defun article-toggle-headers (&optional arg)
1486   "Toggle hiding of headers.  If given a negative prefix, always show;
1487 if given a positive prefix, always hide."
1488   (interactive (gnus-article-hidden-arg))
1489   (let ((force (when (numberp arg)
1490                  (cond ((> arg 0) 'always-hide)
1491                        ((< arg 0) 'always-show))))
1492         (window (get-buffer-window gnus-article-buffer))
1493         (header-end (point-min))
1494         header-start field-end field-start
1495         (inhibit-point-motion-hooks t)
1496         (inhibit-read-only t))
1497     (save-restriction
1498       (widen)
1499       (while (and (setq header-start
1500                         (text-property-any header-end (point-max)
1501                                            'article-treated-header t))
1502                   (setq header-end
1503                         (text-property-not-all header-start (point-max)
1504                                                'article-treated-header t)))
1505         (setq field-end header-start)
1506         (cond
1507          (;; Hide exposed invisible fields.
1508           (and (not (eq 'always-show force))
1509                (setq field-start
1510                      (text-property-any field-end header-end
1511                                         'exposed-invisible-field t)))
1512           (while (and field-start
1513                       (setq field-end (text-property-not-all
1514                                        field-start header-end
1515                                        'exposed-invisible-field t)))
1516             (add-text-properties field-start field-end gnus-hidden-properties)
1517             (setq field-start (text-property-any field-end header-end
1518                                                  'exposed-invisible-field t)))
1519           (put-text-property header-start header-end
1520                              'exposed-invisible-field nil))
1521          (;; Expose invisible fields.
1522           (and (not (eq 'always-hide force))
1523                (setq field-start
1524                      (text-property-any field-end header-end 'invisible t)))
1525           (while (and field-start
1526                       (setq field-end (text-property-not-all
1527                                        field-start header-end
1528                                        'invisible t)))
1529             ;; If the invisible text is not terminated with newline, we
1530             ;; won't expose it.  Because it may be created by x-face-mule.
1531             ;; BTW, XEmacs sometimes fail in putting a invisible text
1532             ;; property with `gnus-article-hide-text' (really?).  In that
1533             ;; case, the invisible text might be started from the middle of
1534             ;; a line so we will expose the sort of thing.
1535             (when (or (not (or (eq header-start field-start)
1536                                (eq ?\n (char-before field-start))))
1537                       (eq ?\n (char-before field-end)))
1538               (remove-text-properties field-start field-end
1539                                       gnus-hidden-properties)
1540               (put-text-property field-start field-end
1541                                  'exposed-invisible-field t))
1542             (setq field-start (text-property-any field-end header-end
1543                                                  'invisible t))))
1544          (;; Hide fields.
1545           (not (eq 'always-show force))
1546           (narrow-to-region header-start header-end)
1547           (article-hide-headers)
1548           ;; Re-display X-Face image under XEmacs.
1549           (when (and (featurep 'xemacs)
1550                      (gnus-functionp gnus-article-x-face-command))
1551             (let ((func (cadr (assq 'gnus-treat-display-xface
1552                                     gnus-treatment-function-alist)))
1553                   (condition 'head))
1554               (when (and (not gnus-inhibit-treatment)
1555                          func
1556                          (gnus-treat-predicate gnus-treat-display-xface))
1557                 (funcall func)
1558                 (put-text-property header-start header-end 'read-only nil))))
1559           (widen))
1560          ))
1561       (goto-char (point-min))
1562       (when window
1563         (set-window-start window (point-min))))))
1564
1565 (defvar gnus-article-normalized-header-length 40
1566   "Length of normalized headers.")
1567
1568 (defun article-normalize-headers ()
1569   "Make all header lines 40 characters long."
1570   (interactive)
1571   (let ((buffer-read-only nil)
1572         column)
1573     (save-excursion
1574       (save-restriction
1575         (article-narrow-to-head)
1576         (while (not (eobp))
1577           (cond
1578            ((< (setq column (- (gnus-point-at-eol) (point)))
1579                gnus-article-normalized-header-length)
1580             (end-of-line)
1581             (insert (make-string
1582                      (- gnus-article-normalized-header-length column)
1583                      ? )))
1584            ((> column gnus-article-normalized-header-length)
1585             (gnus-put-text-property
1586              (progn
1587                (forward-char gnus-article-normalized-header-length)
1588                (point))
1589              (gnus-point-at-eol)
1590              'invisible t))
1591            (t
1592             ;; Do nothing.
1593             ))
1594           (forward-line 1))))))
1595
1596 (defun article-treat-dumbquotes ()
1597   "Translate M****s*** sm*rtq**t*s into proper text.
1598 Note that this function guesses whether a character is a sm*rtq**t* or
1599 not, so it should only be used interactively.
1600
1601 Sm*rtq**t*s are M****s***'s unilateral extension to the character map
1602 in an attempt to provide more quoting characters.  If you see
1603 something like \\222 or \\264 where you're expecting some kind of
1604 apostrophe or quotation mark, then try this wash."
1605   (interactive)
1606   (article-translate-strings gnus-article-dumbquotes-map))
1607
1608 (defun article-translate-characters (from to)
1609   "Translate all characters in the body of the article according to FROM and TO.
1610 FROM is a string of characters to translate from; to is a string of
1611 characters to translate to."
1612   (save-excursion
1613     (when (article-goto-body)
1614       (let ((buffer-read-only nil)
1615             (x (make-string 225 ?x))
1616             (i -1))
1617         (while (< (incf i) (length x))
1618           (aset x i i))
1619         (setq i 0)
1620         (while (< i (length from))
1621           (aset x (aref from i) (aref to i))
1622           (incf i))
1623         (translate-region (point) (point-max) x)))))
1624
1625 (defun article-translate-strings (map)
1626   "Translate all string in the body of the article according to MAP.
1627 MAP is an alist where the elements are on the form (\"from\" \"to\")."
1628   (save-excursion
1629     (when (article-goto-body)
1630       (let ((buffer-read-only nil)
1631             elem)
1632         (while (setq elem (pop map))
1633           (save-excursion
1634             (while (search-forward (car elem) nil t)
1635               (replace-match (cadr elem)))))))))
1636
1637 (defun article-treat-overstrike ()
1638   "Translate overstrikes into bold text."
1639   (interactive)
1640   (save-excursion
1641     (when (article-goto-body)
1642       (let ((buffer-read-only nil))
1643         (while (search-forward "\b" nil t)
1644           (let ((next (char-after))
1645                 start end previous)
1646             (backward-char 2)
1647             (setq start (point)
1648                   previous (char-after))
1649             (forward-char 3)
1650             (setq end (point))
1651             (backward-char)
1652             ;; We do the boldification/underlining by hiding the
1653             ;; overstrikes and putting the proper text property
1654             ;; on the letters.
1655             (cond
1656              ((eq next previous)
1657               (gnus-article-hide-text-type start (point) 'overstrike)
1658               (put-text-property (point) end 'face 'bold))
1659              ((eq next ?_)
1660               (gnus-article-hide-text-type
1661                (1- (point)) (1+ (point)) 'overstrike)
1662               (put-text-property
1663                start (1- (point)) 'face 'underline))
1664              ((eq previous ?_)
1665               (gnus-article-hide-text-type start (point) 'overstrike)
1666               (put-text-property
1667                (point) end 'face 'underline)))))))))
1668
1669 (defun article-fill-long-lines ()
1670   "Fill lines that are wider than the window width."
1671   (interactive)
1672   (save-excursion
1673     (let ((buffer-read-only nil)
1674           (width (window-width (get-buffer-window (current-buffer)))))
1675       (save-restriction
1676         (article-goto-body)
1677         (let ((adaptive-fill-mode nil))
1678           (while (not (eobp))
1679             (end-of-line)
1680             (when (>= (current-column) (min fill-column width))
1681               (narrow-to-region (point) (gnus-point-at-bol))
1682               (fill-paragraph nil)
1683               (goto-char (point-max))
1684               (widen))
1685             (forward-line 1)))))))
1686
1687 (defun article-capitalize-sentences ()
1688   "Capitalize the first word in each sentence."
1689   (interactive)
1690   (save-excursion
1691     (let ((buffer-read-only nil)
1692           (paragraph-start "^[\n\^L]"))
1693       (article-goto-body)
1694       (while (not (eobp))
1695         (capitalize-word 1)
1696         (forward-sentence)))))
1697
1698 (defun article-remove-cr ()
1699   "Remove trailing CRs and then translate remaining CRs into LFs."
1700   (interactive)
1701   (save-excursion
1702     (let ((buffer-read-only nil))
1703       (goto-char (point-min))
1704       (while (re-search-forward "\r+$" nil t)
1705         (replace-match "" t t))
1706       (goto-char (point-min))
1707       (while (search-forward "\r" nil t)
1708         (replace-match "\n" t t)))))
1709
1710 (defun article-remove-trailing-blank-lines ()
1711   "Remove all trailing blank lines from the article."
1712   (interactive)
1713   (save-excursion
1714     (let ((buffer-read-only nil))
1715       (goto-char (point-max))
1716       (delete-region
1717        (point)
1718        (progn
1719          (while (and (not (bobp))
1720                      (looking-at "^[ \t]*$")
1721                      (not (gnus-annotation-in-region-p
1722                            (point) (gnus-point-at-eol))))
1723            (forward-line -1))
1724          (forward-line 1)
1725          (point))))))
1726
1727 (defun article-display-x-face (&optional force)
1728   "Look for an X-Face header and display it if present."
1729   (interactive (list 'force))
1730   (save-excursion
1731     ;; Delete the old process, if any.
1732     (when (process-status "article-x-face")
1733       (delete-process "article-x-face"))
1734     (let ((inhibit-point-motion-hooks t)
1735           x-faces
1736           (case-fold-search t)
1737           from last)
1738       (save-restriction
1739         (article-narrow-to-head)
1740 ;;      (when (and buffer-read-only ;; When type `W f'
1741 ;;                 (progn
1742 ;;                   (goto-char (point-min))
1743 ;;                   (not (re-search-forward "^X-Face:[\t ]*" nil t)))
1744 ;;                 (gnus-buffer-live-p gnus-original-article-buffer))
1745 ;;        (with-current-buffer gnus-original-article-buffer
1746 ;;          (save-restriction
1747 ;;            (article-narrow-to-head)
1748 ;;            (while (re-search-forward "^X-Face:" nil t)
1749 ;;              (setq x-faces
1750 ;;                    (concat
1751 ;;                     (or x-faces "")
1752 ;;                     (buffer-substring
1753 ;;                      (match-beginning 0)
1754 ;;                      (1- (re-search-forward
1755 ;;                           "^\\($\\|[^ \t]\\)" nil t))))))))
1756 ;;        (if x-faces
1757 ;;            (let (point start bface eface buffer-read-only)
1758 ;;              (goto-char (point-max))
1759 ;;              (forward-line -1)
1760 ;;              (setq bface (get-text-property (gnus-point-at-bol) 'face)
1761 ;;                    eface (get-text-property (1- (gnus-point-at-eol)) 'face))
1762 ;;              (goto-char (point-max))
1763 ;;              (setq point (point))
1764 ;;              (insert x-faces)
1765 ;;              (goto-char point)
1766 ;;              (while (looking-at "\\([^:]+\\): *")
1767 ;;                (put-text-property (match-beginning 1) (1+ (match-end 1))
1768 ;;                                   'face bface)
1769 ;;                (setq start (match-end 0))
1770 ;;                (forward-line 1)
1771 ;;                (while (looking-at "[\t ]")
1772 ;;                  (forward-line 1))
1773 ;;                (put-text-property start (point)
1774 ;;                                   'face eface)))))
1775         (goto-char (point-min))
1776         (setq from (message-fetch-field "from"))
1777         (goto-char (point-min))
1778         (while (and gnus-article-x-face-command
1779                     (not last)
1780                     (or force
1781                         ;; Check whether this face is censored.
1782                         (not gnus-article-x-face-too-ugly)
1783                         (and gnus-article-x-face-too-ugly from
1784                              (not (string-match gnus-article-x-face-too-ugly
1785                                                 from))))
1786                     ;; Has to be present.
1787                     (re-search-forward "^X-Face:[\t ]*" nil t))
1788           ;; This used to try to do multiple faces (`while' instead of
1789           ;; `when' above), but (a) sending multiple EOFs to xv doesn't
1790           ;; work (b) it can crash some versions of Emacs (c) are
1791           ;; multiple faces really something to encourage?
1792           (when (stringp gnus-article-x-face-command)
1793             (setq last t))
1794           ;; We now have the area of the buffer where the X-Face is stored.
1795           (save-excursion
1796             (let ((beg (point))
1797                   (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
1798               ;; We display the face.
1799               (if (symbolp gnus-article-x-face-command)
1800                   ;; The command is a lisp function, so we call it.
1801                   (if (gnus-functionp gnus-article-x-face-command)
1802                       (funcall gnus-article-x-face-command beg end)
1803                     (error "%s is not a function" gnus-article-x-face-command))
1804                 ;; The command is a string, so we interpret the command
1805                 ;; as a, well, command, and fork it off.
1806                 (let ((process-connection-type nil))
1807                   (process-kill-without-query
1808                    (start-process
1809                     "article-x-face" nil shell-file-name shell-command-switch
1810                     gnus-article-x-face-command))
1811                   (process-send-region "article-x-face" beg end)
1812                   (process-send-eof "article-x-face"))))))))))
1813
1814 (defun article-decode-mime-words ()
1815   "Decode all MIME-encoded words in the article."
1816   (interactive)
1817   (save-excursion
1818     (set-buffer gnus-article-buffer)
1819     (let ((inhibit-point-motion-hooks t)
1820           buffer-read-only
1821           (mail-parse-charset gnus-newsgroup-charset)
1822           (mail-parse-ignored-charsets
1823            (save-excursion (set-buffer gnus-summary-buffer)
1824                            gnus-newsgroup-ignored-charsets)))
1825       (mail-decode-encoded-word-region (point-min) (point-max)))))
1826
1827 (defun article-decode-charset (&optional prompt)
1828   "Decode charset-encoded text in the article.
1829 If PROMPT (the prefix), prompt for a coding system to use."
1830   (interactive "P")
1831   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
1832         buffer-read-only
1833         (mail-parse-charset gnus-newsgroup-charset)
1834         (mail-parse-ignored-charsets
1835          (save-excursion (condition-case nil
1836                              (set-buffer gnus-summary-buffer)
1837                            (error))
1838                          gnus-newsgroup-ignored-charsets))
1839         ct cte ctl charset format)
1840   (save-excursion
1841     (save-restriction
1842       (article-narrow-to-head)
1843       (setq ct (message-fetch-field "Content-Type" t)
1844             cte (message-fetch-field "Content-Transfer-Encoding" t)
1845             ctl (and ct (ignore-errors
1846                           (mail-header-parse-content-type ct)))
1847             charset (cond
1848                      (prompt
1849                       (mm-read-coding-system "Charset to decode: "))
1850                      (ctl
1851                       (mail-content-type-get ctl 'charset)))
1852             format (and ctl (mail-content-type-get ctl 'format)))
1853       (when cte
1854         (setq cte (mail-header-strip cte)))
1855       (if (and ctl (not (string-match "/" (car ctl))))
1856           (setq ctl nil))
1857       (goto-char (point-max)))
1858     (forward-line 1)
1859     (save-restriction
1860       (narrow-to-region (point) (point-max))
1861       (when (and (eq mail-parse-charset 'gnus-decoded)
1862                  (eq (mm-body-7-or-8) '8bit))
1863         ;; The text code could have been decoded.
1864         (setq charset mail-parse-charset))
1865       (when (and (or (not ctl)
1866                      (equal (car ctl) "text/plain"))
1867                  (not format)) ;; article with format will decode later.
1868         (mm-decode-body
1869          charset (and cte (intern (downcase
1870                                    (gnus-strip-whitespace cte))))
1871          (car ctl)))))))
1872
1873 (defun article-decode-encoded-words ()
1874   "Remove encoded-word encoding from headers."
1875   (let (buffer-read-only)
1876     (let ((charset (save-excursion
1877                      (set-buffer gnus-summary-buffer)
1878                      default-mime-charset)))
1879       (mime-decode-header-in-buffer charset)
1880       )))
1881
1882 (defun article-de-quoted-unreadable (&optional force read-charset)
1883   "Translate a quoted-printable-encoded article.
1884 If FORCE, decode the article whether it is marked as quoted-printable
1885 or not.
1886 If READ-CHARSET, ask for a coding system."
1887   (interactive (list 'force current-prefix-arg))
1888   (save-excursion
1889     (let ((buffer-read-only nil) type charset)
1890       (if (gnus-buffer-live-p gnus-original-article-buffer)
1891           (with-current-buffer gnus-original-article-buffer
1892             (setq type
1893                   (gnus-fetch-field "content-transfer-encoding"))
1894             (let* ((ct (gnus-fetch-field "content-type"))
1895                    (ctl (and ct
1896                              (ignore-errors
1897                                (mail-header-parse-content-type ct)))))
1898               (setq charset (and ctl
1899                                  (mail-content-type-get ctl 'charset)))
1900               (if (stringp charset)
1901                   (setq charset (intern (downcase charset)))))))
1902       (if read-charset
1903           (setq charset (read-coding-system "Charset: " charset)))
1904       (unless charset
1905         (setq charset gnus-newsgroup-charset))
1906       (when (or force
1907                 (and type (let ((case-fold-search t))
1908                             (string-match "quoted-printable" type))))
1909         (article-goto-body)
1910         (quoted-printable-decode-region
1911          (point) (point-max) (mm-charset-to-coding-system charset))))))
1912
1913 (defun article-de-base64-unreadable (&optional force read-charset)
1914   "Translate a base64 article.
1915 If FORCE, decode the article whether it is marked as base64 not.
1916 If READ-CHARSET, ask for a coding system."
1917   (interactive (list 'force current-prefix-arg))
1918   (save-excursion
1919     (let ((buffer-read-only nil) type charset)
1920       (if (gnus-buffer-live-p gnus-original-article-buffer)
1921           (with-current-buffer gnus-original-article-buffer
1922             (setq type
1923                   (gnus-fetch-field "content-transfer-encoding"))
1924             (let* ((ct (gnus-fetch-field "content-type"))
1925                    (ctl (and ct
1926                              (ignore-errors
1927                                (mail-header-parse-content-type ct)))))
1928               (setq charset (and ctl
1929                                  (mail-content-type-get ctl 'charset)))
1930               (if (stringp charset)
1931                   (setq charset (intern (downcase charset)))))))
1932       (if read-charset
1933           (setq charset (read-coding-system "Charset: " charset)))
1934       (unless charset
1935         (setq charset gnus-newsgroup-charset))
1936       (when (or force
1937                 (and type (let ((case-fold-search t))
1938                             (string-match "base64" type))))
1939         (article-goto-body)
1940         (save-restriction
1941           (narrow-to-region (point) (point-max))
1942           (base64-decode-region (point-min) (point-max))
1943           (mm-decode-coding-region
1944            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
1945
1946 (eval-when-compile
1947   (require 'rfc1843))
1948
1949 (defun article-decode-HZ ()
1950   "Translate a HZ-encoded article."
1951   (interactive)
1952   (require 'rfc1843)
1953   (save-excursion
1954     (let ((buffer-read-only nil))
1955       (rfc1843-decode-region (point-min) (point-max)))))
1956
1957 (defun article-wash-html (&optional read-charset)
1958   "Format an html article.
1959 If READ-CHARSET, ask for a coding system."
1960   (interactive "P")
1961   (save-excursion
1962     (let ((buffer-read-only nil)
1963           charset)
1964       (if (gnus-buffer-live-p gnus-original-article-buffer)
1965           (with-current-buffer gnus-original-article-buffer
1966             (let* ((ct (gnus-fetch-field "content-type"))
1967                    (ctl (and ct
1968                              (ignore-errors
1969                                (mail-header-parse-content-type ct)))))
1970               (setq charset (and ctl
1971                                  (mail-content-type-get ctl 'charset)))
1972               (if (stringp charset)
1973                   (setq charset (intern (downcase charset)))))))
1974       (if read-charset
1975           (setq charset (read-coding-system "Charset: " charset)))
1976       (unless charset
1977         (setq charset gnus-newsgroup-charset))
1978       (article-goto-body)
1979       (save-window-excursion
1980         (save-restriction
1981           (narrow-to-region (point) (point-max))
1982           (mm-setup-w3)
1983           (let ((w3-strict-width (window-width))
1984                 (url-standalone-mode t))
1985             (condition-case var
1986                 (w3-region (point-min) (point-max))
1987               (error))))))))
1988
1989 (defun article-hide-list-identifiers ()
1990   "Remove list identifies from the Subject header.
1991 The `gnus-list-identifiers' variable specifies what to do."
1992   (interactive)
1993   (let ((inhibit-point-motion-hooks t)
1994         (regexp (if (consp gnus-list-identifiers)
1995                     (mapconcat 'identity gnus-list-identifiers " *\\|")
1996                   gnus-list-identifiers))
1997         buffer-read-only)
1998     (when regexp
1999       (save-excursion
2000         (save-restriction
2001           (article-narrow-to-head)
2002           (goto-char (point-min))
2003           (while (re-search-forward
2004                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
2005                   nil t)
2006             (delete-region (match-beginning 2) (match-end 0))
2007             (beginning-of-line))
2008           (when (re-search-forward
2009                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
2010             (delete-region (match-beginning 1) (match-end 1))))))))
2011
2012 (defun article-hide-pgp ()
2013   "Remove any PGP headers and signatures in the current article."
2014   (interactive)
2015   (save-excursion
2016     (save-restriction
2017       (let ((inhibit-point-motion-hooks t)
2018             buffer-read-only beg end)
2019         (article-goto-body)
2020         ;; Hide the "header".
2021         (when (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
2022           (push 'pgp gnus-article-wash-types)
2023           (delete-region (match-beginning 0) (match-end 0))
2024           ;; Remove armor headers (rfc2440 6.2)
2025           (delete-region (point) (or (re-search-forward "^[ \t]*\n" nil t)
2026                                      (point)))
2027           (setq beg (point))
2028           ;; Hide the actual signature.
2029           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
2030                (setq end (1+ (match-beginning 0)))
2031                (delete-region
2032                 end
2033                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
2034                     (match-end 0)
2035                   ;; Perhaps we shouldn't hide to the end of the buffer
2036                   ;; if there is no end to the signature?
2037                   (point-max))))
2038           ;; Hide "- " PGP quotation markers.
2039           (when (and beg end)
2040             (narrow-to-region beg end)
2041             (goto-char (point-min))
2042             (while (re-search-forward "^- " nil t)
2043               (delete-region
2044                (match-beginning 0) (match-end 0)))
2045             (widen))
2046           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
2047
2048 (defun article-hide-pem (&optional arg)
2049   "Toggle hiding of any PEM headers and signatures in the current article.
2050 If given a negative prefix, always show; if given a positive prefix,
2051 always hide."
2052   (interactive (gnus-article-hidden-arg))
2053   (unless (gnus-article-check-hidden-text 'pem arg)
2054     (save-excursion
2055       (let (buffer-read-only end)
2056         (goto-char (point-min))
2057         ;; Hide the horrendously ugly "header".
2058         (when (and (search-forward
2059                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
2060                     nil t)
2061                    (setq end (1+ (match-beginning 0))))
2062           (push 'pem gnus-article-wash-types)
2063           (gnus-article-hide-text-type
2064            end
2065            (if (search-forward "\n\n" nil t)
2066                (match-end 0)
2067              (point-max))
2068            'pem)
2069           ;; Hide the trailer as well
2070           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
2071                                 nil t)
2072             (gnus-article-hide-text-type
2073              (match-beginning 0) (match-end 0) 'pem)))))))
2074
2075 (defun article-strip-banner ()
2076   "Strip the banner specified by the `banner' group parameter."
2077   (interactive)
2078   (save-excursion
2079     (save-restriction
2080       (let ((inhibit-point-motion-hooks t)
2081             (banner (gnus-parameter-banner gnus-newsgroup-name))
2082             (gnus-signature-limit nil)
2083             buffer-read-only beg end)
2084         (when banner
2085           (article-goto-body)
2086           (cond
2087            ((eq banner 'signature)
2088             (when (gnus-article-narrow-to-signature)
2089               (widen)
2090               (forward-line -1)
2091               (delete-region (point) (point-max))))
2092            ((symbolp banner)
2093             (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
2094                 (while (re-search-forward banner nil t)
2095                   (delete-region (match-beginning 0) (match-end 0)))))
2096            ((stringp banner)
2097             (while (re-search-forward banner nil t)
2098               (delete-region (match-beginning 0) (match-end 0))))))))))
2099
2100 (defun article-babel ()
2101   "Translate article using an online translation service."
2102   (interactive)
2103   (require 'babel)
2104   (save-excursion
2105     (set-buffer gnus-article-buffer)
2106     (when (article-goto-body)
2107       (let* ((buffer-read-only nil)
2108              (start (point))
2109              (end (point-max))
2110              (orig (buffer-substring start end))
2111              (trans (babel-as-string orig)))
2112         (save-restriction
2113           (narrow-to-region start end)
2114           (delete-region start end)
2115           (insert trans))))))
2116
2117 (defun article-hide-signature (&optional arg)
2118   "Hide the signature in the current article.
2119 If given a negative prefix, always show; if given a positive prefix,
2120 always hide."
2121   (interactive (gnus-article-hidden-arg))
2122   (save-excursion
2123     (save-restriction
2124       (if (interactive-p)
2125           (progn
2126             (widen)
2127             (article-goto-body))
2128         (goto-char (point-min)))
2129       (unless (gnus-article-check-hidden-text 'signature arg)
2130         (let ((buffer-read-only nil)
2131               (button (point)))
2132           (while (setq button (text-property-any button (point-max)
2133                                                  'gnus-callback
2134                                                  'gnus-signature-toggle))
2135             (setq button (text-property-not-all button (point-max)
2136                                                 'gnus-callback
2137                                                 'gnus-signature-toggle))
2138             (when (and button (not (eobp)))
2139               (gnus-article-hide-text-type
2140                (1+ button)
2141                (next-single-property-change (1+ button) 'mime-view-entity
2142                                             nil (point-max))
2143                'signature))))))))
2144
2145 (defun article-strip-headers-in-body ()
2146   "Strip offensive headers from bodies."
2147   (interactive)
2148   (save-excursion
2149     (article-goto-body)
2150     (let ((case-fold-search t))
2151       (when (looking-at "x-no-archive:")
2152         (gnus-delete-line)))))
2153
2154 (defun article-strip-leading-blank-lines ()
2155   "Remove all blank lines from the beginning of the article."
2156   (interactive)
2157   (save-excursion
2158     (let ((inhibit-point-motion-hooks t)
2159           buffer-read-only)
2160       (when (article-goto-body)
2161         (while (and (not (eobp))
2162                     (looking-at "[ \t]*$"))
2163           (gnus-delete-line))))))
2164
2165 (defun article-narrow-to-head ()
2166   "Narrow the buffer to the head of the message.
2167 Point is left at the beginning of the narrowed-to region."
2168   (narrow-to-region
2169    (goto-char (point-min))
2170    (if (search-forward "\n\n" nil 1)
2171        (1- (point))
2172      (point-max)))
2173   (goto-char (point-min)))
2174
2175 (defun article-goto-body ()
2176   "Place point at the start of the body."
2177   (goto-char (point-min))
2178   (cond
2179    ;; This variable is only bound when dealing with separate
2180    ;; MIME body parts.
2181    (article-goto-body-goes-to-point-min-p
2182     t)
2183    ((search-forward "\n\n" nil t)
2184     t)
2185    (t
2186     (goto-char (point-max))
2187     nil)))
2188
2189 (defun article-strip-multiple-blank-lines ()
2190   "Replace consecutive blank lines with one empty line."
2191   (interactive)
2192   (save-excursion
2193     (let ((inhibit-point-motion-hooks t)
2194           buffer-read-only)
2195       ;; First make all blank lines empty.
2196       (article-goto-body)
2197       (while (re-search-forward "^[ \t]+$" nil t)
2198         (unless (gnus-annotation-in-region-p
2199                  (match-beginning 0) (match-end 0))
2200           (replace-match "" nil t)))
2201       ;; Then replace multiple empty lines with a single empty line.
2202       (article-goto-body)
2203       (while (re-search-forward "\n\n\\(\n+\\)" nil t)
2204         (unless (gnus-annotation-in-region-p
2205                  (match-beginning 0) (match-end 0))
2206           (delete-region (match-beginning 1) (match-end 1)))))))
2207
2208 (defun article-strip-leading-space ()
2209   "Remove all white space from the beginning of the lines in the article."
2210   (interactive)
2211   (save-excursion
2212     (let ((inhibit-point-motion-hooks t)
2213           buffer-read-only)
2214       (article-goto-body)
2215       (while (re-search-forward "^[ \t]+" nil t)
2216         (replace-match "" t t)))))
2217
2218 (defun article-strip-trailing-space ()
2219   "Remove all white space from the end of the lines in the article."
2220   (interactive)
2221   (save-excursion
2222     (let ((inhibit-point-motion-hooks t)
2223           buffer-read-only)
2224       (article-goto-body)
2225       (while (re-search-forward "[ \t]+$" nil t)
2226         (replace-match "" t t)))))
2227
2228 (defun article-strip-blank-lines ()
2229   "Strip leading, trailing and multiple blank lines."
2230   (interactive)
2231   (article-strip-leading-blank-lines)
2232   (article-remove-trailing-blank-lines)
2233   (article-strip-multiple-blank-lines))
2234
2235 (defun article-strip-all-blank-lines ()
2236   "Strip all blank lines."
2237   (interactive)
2238   (save-excursion
2239     (let ((inhibit-point-motion-hooks t)
2240           buffer-read-only)
2241       (article-goto-body)
2242       (while (re-search-forward "^[ \t]*\n" nil t)
2243         (replace-match "" t t)))))
2244
2245 (defun gnus-article-narrow-to-signature ()
2246   "Narrow to the signature; return t if a signature is found, else nil."
2247   (let ((inhibit-point-motion-hooks t))
2248     (when (gnus-article-search-signature)
2249       (forward-line 1)
2250       ;; Check whether we have some limits to what we consider
2251       ;; to be a signature.
2252       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2253                       (list gnus-signature-limit)))
2254             limit limited)
2255         (while (setq limit (pop limits))
2256           (if (or (and (integerp limit)
2257                        (< (- (point-max) (point)) limit))
2258                   (and (floatp limit)
2259                        (< (count-lines (point) (point-max)) limit))
2260                   (and (gnus-functionp limit)
2261                        (funcall limit))
2262                   (and (stringp limit)
2263                        (not (re-search-forward limit nil t))))
2264               ()                        ; This limit did not succeed.
2265             (setq limited t
2266                   limits nil)))
2267         (unless limited
2268           (narrow-to-region (point) (point-max))
2269           t)))))
2270
2271 (defun gnus-article-search-signature ()
2272   "Search the current buffer for the signature separator.
2273 Put point at the beginning of the signature separator."
2274   (let ((cur (point)))
2275     (goto-char (point-max))
2276     (if (if (stringp gnus-signature-separator)
2277             (re-search-backward gnus-signature-separator nil t)
2278           (let ((seps gnus-signature-separator))
2279             (while (and seps
2280                         (not (re-search-backward (car seps) nil t)))
2281               (pop seps))
2282             seps))
2283         t
2284       (goto-char cur)
2285       nil)))
2286
2287 (defun gnus-article-hidden-arg ()
2288   "Return the current prefix arg as a number, or 0 if no prefix."
2289   (list (if current-prefix-arg
2290             (prefix-numeric-value current-prefix-arg)
2291           0)))
2292
2293 (defun gnus-article-check-hidden-text (type arg)
2294   "Return nil if hiding is necessary.
2295 Arg can be nil or a number.  Nil and positive means hide, negative
2296 means show, 0 means toggle."
2297   (save-excursion
2298     (save-restriction
2299       (let ((hide (gnus-article-hidden-text-p type)))
2300         (cond
2301          ((or (null arg)
2302               (> arg 0))
2303           nil)
2304          ((< arg 0)
2305           (gnus-article-show-hidden-text type)
2306           t)
2307          (t
2308           (if (eq hide 'hidden)
2309               (progn
2310                 (gnus-article-show-hidden-text type)
2311                 t)
2312             nil)))))))
2313
2314 (defun gnus-article-hidden-text-p (type)
2315   "Say whether the current buffer contains hidden text of type TYPE."
2316   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
2317     (while (and pos
2318                 (not (get-text-property pos 'invisible))
2319                 (not (get-text-property pos 'dummy-invisible)))
2320       (setq pos
2321             (text-property-any (1+ pos) (point-max) 'article-type type)))
2322     (if pos
2323         'hidden
2324       nil)))
2325
2326 (defun gnus-article-show-hidden-text (type &optional dummy)
2327   "Show all hidden text of type TYPE.
2328 Originally it is hide instead of DUMMY."
2329   (let ((buffer-read-only nil)
2330         (inhibit-point-motion-hooks t))
2331     (gnus-remove-text-properties-when
2332      'article-type type
2333      (point-min) (point-max)
2334      (cons 'article-type (cons type
2335                                gnus-hidden-properties)))))
2336
2337 (defconst article-time-units
2338   `((year . ,(* 365.25 24 60 60))
2339     (week . ,(* 7 24 60 60))
2340     (day . ,(* 24 60 60))
2341     (hour . ,(* 60 60))
2342     (minute . 60)
2343     (second . 1))
2344   "Mapping from time units to seconds.")
2345
2346 (defun article-date-ut (&optional type highlight header)
2347   "Convert DATE date to universal time in the current article.
2348 If TYPE is `local', convert to local time; if it is `lapsed', output
2349 how much time has lapsed since DATE.  For `lapsed', the value of
2350 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
2351 should replace the \"Date:\" one, or should be added below it."
2352   (interactive (list 'ut t))
2353   (let* ((header (or header
2354                      (and (eq 1 (point-min))
2355                           (mail-header-date (save-excursion
2356                                               (set-buffer gnus-summary-buffer)
2357                                               gnus-current-headers)))
2358                      (message-fetch-field "date")
2359                      ""))
2360          (date (if (vectorp header) (mail-header-date header)
2361                  header))
2362          (inhibit-point-motion-hooks t)
2363          bface eface date-pos)
2364     (when (and date (not (string= date "")))
2365       (save-excursion
2366         (save-restriction
2367           (article-narrow-to-head)
2368           (when (or (and (eq type 'lapsed)
2369                          gnus-article-date-lapsed-new-header
2370                          ;; Attempt to get the face of X-Sent first.
2371                          (re-search-forward "^X-Sent:[ \t]" nil t))
2372                     (re-search-forward "^Date:[ \t]" nil t)
2373                     ;; If Date is missing, try again for X-Sent.
2374                     (re-search-forward "^X-Sent:[ \t]" nil t))
2375             (setq bface (get-text-property (gnus-point-at-bol) 'face)
2376                   date (or (get-text-property (gnus-point-at-bol)
2377                                                 'original-date)
2378                              date)
2379                   eface (get-text-property (1- (gnus-point-at-eol))
2380                                            'face)))
2381           (let ((buffer-read-only nil))
2382             ;; Delete any old X-Sent headers.
2383             (when (setq date-pos
2384                         (text-property-any (point-min) (point-max)
2385                                            'article-date-lapsed t))
2386               (goto-char (setq date-pos (set-marker (make-marker) date-pos)))
2387               (delete-region (match-beginning 0)
2388                              (progn (forward-line 1) (point))))
2389             (goto-char (point-min))
2390             ;; Delete any old Date headers.
2391             (while (re-search-forward "^Date:[ \t]" nil t)
2392               (unless date-pos
2393                 (setq date-pos (match-beginning 0)))
2394               (unless (and (eq type 'lapsed)
2395                            gnus-article-date-lapsed-new-header)
2396                 (delete-region (match-beginning 0)
2397                                (progn (message-next-header) (point)))))
2398             (if date-pos
2399                 (progn
2400                   (goto-char date-pos)
2401                   (unless (bolp)
2402                     ;; Possibly, Date has been deleted.
2403                     (insert "\n"))
2404                   (when (and (eq type 'lapsed)
2405                              gnus-article-date-lapsed-new-header
2406                              (looking-at "Date:"))
2407                     (forward-line 1)))
2408               (goto-char (point-min)))
2409             (insert (article-make-date-line date type))
2410             (when (eq type 'lapsed)
2411               (put-text-property (gnus-point-at-bol) (point)
2412                                  'article-date-lapsed t))
2413             (insert "\n")
2414             (forward-line -1)
2415             ;; Do highlighting.
2416             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
2417               (put-text-property (match-beginning 1) (1+ (match-end 1))
2418                                  'original-date date)
2419               (put-text-property (match-beginning 1) (1+ (match-end 1))
2420                                  'face bface)
2421               (put-text-property (match-beginning 2) (match-end 2)
2422                                  'face eface))))))))
2423
2424 (defun article-make-date-line (date type)
2425   "Return a DATE line of TYPE."
2426   (unless (memq type '(local ut original user iso8601 lapsed english))
2427     (error "Unknown conversion type: %s" type))
2428   (condition-case ()
2429       (let ((time (date-to-time date)))
2430         (cond
2431          ;; Convert to the local timezone.
2432          ((eq type 'local)
2433           (let ((tz (car (current-time-zone time))))
2434             (format "Date: %s %s%02d%02d" (current-time-string time)
2435                     (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2436                     (/ (% (abs tz) 3600) 60))))
2437          ;; Convert to Universal Time.
2438          ((eq type 'ut)
2439           (concat "Date: "
2440                   (current-time-string
2441                    (let* ((e (parse-time-string date))
2442                           (tm (apply 'encode-time e))
2443                           (ms (car tm))
2444                           (ls (- (cadr tm) (car (current-time-zone time)))))
2445                      (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
2446                            ((> ls 65535) (list (1+ ms) (- ls 65536)))
2447                            (t (list ms ls)))))
2448                   " UT"))
2449          ;; Get the original date from the article.
2450          ((eq type 'original)
2451           (concat "Date: " (if (string-match "\n+$" date)
2452                                (substring date 0 (match-beginning 0))
2453                              date)))
2454          ;; Let the user define the format.
2455          ((eq type 'user)
2456           (if (gnus-functionp gnus-article-time-format)
2457               (funcall gnus-article-time-format time)
2458             (concat
2459              "Date: "
2460              (format-time-string gnus-article-time-format time))))
2461          ;; ISO 8601.
2462          ((eq type 'iso8601)
2463           (let ((tz (car (current-time-zone time))))
2464             (concat
2465              "Date: "
2466              (format-time-string "%Y%m%dT%H%M%S" time)
2467              (format "%s%02d%02d"
2468                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2469                      (/ (% (abs tz) 3600) 60)))))
2470          ;; Do an X-Sent lapsed format.
2471          ((eq type 'lapsed)
2472           ;; If the date is seriously mangled, the timezone functions are
2473           ;; liable to bug out, so we ignore all errors.
2474           (let* ((now (current-time))
2475                  (real-time (subtract-time now time))
2476                  (real-sec (and real-time
2477                                 (+ (* (float (car real-time)) 65536)
2478                                    (cadr real-time))))
2479                  (sec (and real-time (abs real-sec)))
2480                  num prev)
2481             (cond
2482              ((null real-time)
2483               "X-Sent: Unknown")
2484              ((zerop sec)
2485               "X-Sent: Now")
2486              (t
2487               (concat
2488                "X-Sent: "
2489                ;; This is a bit convoluted, but basically we go
2490                ;; through the time units for years, weeks, etc,
2491                ;; and divide things to see whether that results
2492                ;; in positive answers.
2493                (mapconcat
2494                 (lambda (unit)
2495                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
2496                       ;; The (remaining) seconds are too few to
2497                       ;; be divided into this time unit.
2498                       ""
2499                     ;; It's big enough, so we output it.
2500                     (setq sec (- sec (* num (cdr unit))))
2501                     (prog1
2502                         (concat (if prev ", " "") (int-to-string
2503                                                    (floor num))
2504                                 " " (symbol-name (car unit))
2505                                 (if (> num 1) "s" ""))
2506                       (setq prev t))))
2507                 article-time-units "")
2508                ;; If dates are odd, then it might appear like the
2509                ;; article was sent in the future.
2510                (if (> real-sec 0)
2511                    " ago"
2512                  " in the future"))))))
2513          ;; Display the date in proper English
2514          ((eq type 'english)
2515           (let ((dtime (decode-time time)))
2516             (concat
2517              "Date: the "
2518              (number-to-string (nth 3 dtime))
2519              (let ((digit (% (nth 3 dtime) 10)))
2520                (cond
2521                 ((memq (nth 3 dtime) '(11 12 13)) "th")
2522                 ((= digit 1) "st")
2523                 ((= digit 2) "nd")
2524                 ((= digit 3) "rd")
2525                 (t "th")))
2526              " of "
2527              (nth (1- (nth 4 dtime)) gnus-english-month-names)
2528              " "
2529              (number-to-string (nth 5 dtime))
2530              " at "
2531              (format "%02d" (nth 2 dtime))
2532              ":"
2533              (format "%02d" (nth 1 dtime)))))))
2534         (error
2535          (format "Date: %s (from Oort)" date))))
2536
2537 (defun article-date-local (&optional highlight)
2538   "Convert the current article date to the local timezone."
2539   (interactive (list t))
2540   (article-date-ut 'local highlight))
2541
2542 (defun article-date-english (&optional highlight)
2543   "Convert the current article date to something that is proper English."
2544   (interactive (list t))
2545   (article-date-ut 'english highlight))
2546
2547 (defun article-date-original (&optional highlight)
2548   "Convert the current article date to what it was originally.
2549 This is only useful if you have used some other date conversion
2550 function and want to see what the date was before converting."
2551   (interactive (list t))
2552   (article-date-ut 'original highlight))
2553
2554 (defun article-date-lapsed (&optional highlight)
2555   "Convert the current article date to time lapsed since it was sent."
2556   (interactive (list t))
2557   (article-date-ut 'lapsed highlight))
2558
2559 (defun article-update-date-lapsed ()
2560   "Function to be run from a timer to update the lapsed time line."
2561   (let (deactivate-mark)
2562     (save-excursion
2563       (ignore-errors
2564         (walk-windows
2565          (lambda (w)
2566            (set-buffer (window-buffer w))
2567            (when (eq major-mode 'gnus-article-mode)
2568              (goto-char (point-min))
2569              (when (re-search-forward "^X-Sent:" nil t)
2570                (article-date-lapsed t))))
2571          nil 'visible)))))
2572
2573 (defun gnus-start-date-timer (&optional n)
2574   "Start a timer to update the X-Sent header in the article buffers.
2575 The numerical prefix says how frequently (in seconds) the function
2576 is to run."
2577   (interactive "p")
2578   (unless n
2579     (setq n 1))
2580   (gnus-stop-date-timer)
2581   (setq article-lapsed-timer
2582         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
2583
2584 (defun gnus-stop-date-timer ()
2585   "Stop the X-Sent timer."
2586   (interactive)
2587   (when article-lapsed-timer
2588     (nnheader-cancel-timer article-lapsed-timer)
2589     (setq article-lapsed-timer nil)))
2590
2591 (defun article-date-user (&optional highlight)
2592   "Convert the current article date to the user-defined format.
2593 This format is defined by the `gnus-article-time-format' variable."
2594   (interactive (list t))
2595   (article-date-ut 'user highlight))
2596
2597 (defun article-date-iso8601 (&optional highlight)
2598   "Convert the current article date to ISO8601."
2599   (interactive (list t))
2600   (article-date-ut 'iso8601 highlight))
2601
2602 (defun article-show-all ()
2603   "Show all hidden text in the article buffer."
2604   (interactive)
2605   (save-excursion
2606     (widen)
2607     (let ((buffer-read-only nil))
2608       (gnus-article-unhide-text (point-min) (point-max))
2609       (gnus-remove-text-with-property 'gnus-prev)
2610       (gnus-remove-text-with-property 'gnus-next))))
2611
2612 (defun article-show-all-headers ()
2613   "Show all hidden headers in the article buffer."
2614   (interactive)
2615   (save-excursion
2616     (save-restriction
2617       (widen)
2618       (article-narrow-to-head)
2619       (let ((buffer-read-only nil))
2620         (gnus-article-unhide-text (point-min) (point-max))))))
2621
2622 (defun article-remove-leading-whitespace ()
2623   "Remove excessive whitespace from all headers."
2624   (interactive)
2625   (save-excursion
2626     (save-restriction
2627       (let ((buffer-read-only nil))
2628         (article-narrow-to-head)
2629         (goto-char (point-min))
2630         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
2631           (delete-region (match-beginning 1) (match-end 1)))))))
2632
2633 (defun article-emphasize (&optional arg)
2634   "Emphasize text according to `gnus-emphasis-alist'."
2635   (interactive (gnus-article-hidden-arg))
2636   (unless (gnus-article-check-hidden-text 'emphasis arg)
2637     (save-excursion
2638       (let ((alist (or
2639                     (condition-case nil
2640                         (with-current-buffer gnus-summary-buffer
2641                           gnus-article-emphasis-alist)
2642                       (error))
2643                     gnus-emphasis-alist))
2644             (buffer-read-only nil)
2645             (props (append '(article-type emphasis)
2646                            gnus-hidden-properties))
2647             regexp elem beg invisible visible face)
2648         (article-goto-body)
2649         (setq beg (point))
2650         (while (setq elem (pop alist))
2651           (goto-char beg)
2652           (setq regexp (car elem)
2653                 invisible (nth 1 elem)
2654                 visible (nth 2 elem)
2655                 face (nth 3 elem))
2656           (while (re-search-forward regexp nil t)
2657             (when (and (match-beginning visible) (match-beginning invisible))
2658               (push 'emphasis gnus-article-wash-types)
2659               (gnus-article-hide-text
2660                (match-beginning invisible) (match-end invisible) props)
2661               (gnus-article-unhide-text-type
2662                (match-beginning visible) (match-end visible) 'emphasis)
2663               (gnus-put-text-property-excluding-newlines
2664                (match-beginning visible) (match-end visible) 'face face)
2665               (goto-char (match-end invisible)))))))))
2666
2667 (defun gnus-article-setup-highlight-words (&optional highlight-words)
2668   "Setup newsgroup emphasis alist."
2669   (unless gnus-article-emphasis-alist
2670     (let ((name (and gnus-newsgroup-name
2671                      (gnus-group-real-name gnus-newsgroup-name))))
2672       (make-local-variable 'gnus-article-emphasis-alist)
2673       (setq gnus-article-emphasis-alist
2674             (nconc
2675              (let ((alist gnus-group-highlight-words-alist) elem highlight)
2676                (while (setq elem (pop alist))
2677                  (when (and name (string-match (car elem) name))
2678                    (setq alist nil
2679                          highlight (copy-sequence (cdr elem)))))
2680                highlight)
2681              (copy-sequence highlight-words)
2682              (if gnus-newsgroup-name
2683                  (copy-sequence (gnus-group-find-parameter
2684                                  gnus-newsgroup-name 'highlight-words t)))
2685              gnus-emphasis-alist)))))
2686
2687 (eval-when-compile
2688   (defvar gnus-summary-article-menu)
2689   (defvar gnus-summary-post-menu))
2690
2691 ;;; Saving functions.
2692
2693 (defun gnus-article-save (save-buffer file &optional num)
2694   "Save the currently selected article."
2695   (unless gnus-save-all-headers
2696     ;; Remove headers according to `gnus-saved-headers'.
2697     (let ((gnus-visible-headers
2698            (or gnus-saved-headers gnus-visible-headers))
2699           (gnus-article-buffer save-buffer))
2700       (save-excursion
2701         (set-buffer save-buffer)
2702         (article-hide-headers 1 t))))
2703   (save-window-excursion
2704     (if (not gnus-default-article-saver)
2705         (error "No default saver is defined")
2706       ;; !!! Magic!  The saving functions all save
2707       ;; `gnus-save-article-buffer' (or so they think), but we
2708       ;; bind that variable to our save-buffer.
2709       (set-buffer gnus-article-buffer)
2710       (let* ((gnus-save-article-buffer save-buffer)
2711              (filename
2712               (cond
2713                ((not gnus-prompt-before-saving) 'default)
2714                ((eq gnus-prompt-before-saving 'always) nil)
2715                (t file)))
2716              (gnus-number-of-articles-to-be-saved
2717               (when (eq gnus-prompt-before-saving t)
2718                 num)))                  ; Magic
2719         (set-buffer gnus-article-current-summary)
2720         (funcall gnus-default-article-saver filename)))))
2721
2722 (defun gnus-read-save-file-name (prompt &optional filename
2723                                         function group headers variable)
2724   (let ((default-name
2725           (funcall function group headers (symbol-value variable)))
2726         result)
2727     (setq result
2728           (expand-file-name
2729            (cond
2730             ((eq filename 'default)
2731              default-name)
2732             ((eq filename t)
2733              default-name)
2734             (filename filename)
2735             (t
2736              (let* ((split-name (gnus-get-split-value gnus-split-methods))
2737                     (prompt
2738                      (format prompt
2739                              (if (and gnus-number-of-articles-to-be-saved
2740                                       (> gnus-number-of-articles-to-be-saved 1))
2741                                  (format "these %d articles"
2742                                          gnus-number-of-articles-to-be-saved)
2743                                "this article")))
2744                     (file
2745                      ;; Let the split methods have their say.
2746                      (cond
2747                       ;; No split name was found.
2748                       ((null split-name)
2749                        (read-file-name
2750                         (concat prompt " (default "
2751                                 (file-name-nondirectory default-name) ") ")
2752                         (file-name-directory default-name)
2753                         default-name))
2754                       ;; A single group name is returned.
2755                       ((stringp split-name)
2756                        (setq default-name
2757                              (funcall function split-name headers
2758                                       (symbol-value variable)))
2759                        (read-file-name
2760                         (concat prompt " (default "
2761                                 (file-name-nondirectory default-name) ") ")
2762                         (file-name-directory default-name)
2763                         default-name))
2764                       ;; A single split name was found
2765                       ((= 1 (length split-name))
2766                        (let* ((name (expand-file-name
2767                                      (car split-name)
2768                                      gnus-article-save-directory))
2769                               (dir (cond ((file-directory-p name)
2770                                           (file-name-as-directory name))
2771                                          ((file-exists-p name) name)
2772                                          (t gnus-article-save-directory))))
2773                          (read-file-name
2774                           (concat prompt " (default " name ") ")
2775                           dir name)))
2776                       ;; A list of splits was found.
2777                       (t
2778                        (setq split-name (nreverse split-name))
2779                        (let (result)
2780                          (let ((file-name-history
2781                                 (nconc split-name file-name-history)))
2782                            (setq result
2783                                  (expand-file-name
2784                                   (read-file-name
2785                                    (concat prompt " (`M-p' for defaults) ")
2786                                    gnus-article-save-directory
2787                                    (car split-name))
2788                                   gnus-article-save-directory)))
2789                          (car (push result file-name-history)))))))
2790                ;; Create the directory.
2791                (gnus-make-directory (file-name-directory file))
2792       ;; If we have read a directory, we append the default file name.
2793                (when (file-directory-p file)
2794                  (setq file (expand-file-name (file-name-nondirectory
2795                                                default-name)
2796                                               (file-name-as-directory file))))
2797                ;; Possibly translate some characters.
2798                (nnheader-translate-file-chars file))))))
2799     (gnus-make-directory (file-name-directory result))
2800     (set variable result)))
2801
2802 (defun gnus-article-archive-name (group)
2803   "Return the first instance of an \"Archive-name\" in the current buffer."
2804   (let ((case-fold-search t))
2805     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
2806       (nnheader-concat gnus-article-save-directory
2807                        (match-string 1)))))
2808
2809 (defun gnus-article-nndoc-name (group)
2810   "If GROUP is an nndoc group, return the name of the parent group."
2811   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
2812     (gnus-group-get-parameter group 'save-article-group)))
2813
2814 (defun gnus-summary-save-in-rmail (&optional filename)
2815   "Append this article to Rmail file.
2816 Optional argument FILENAME specifies file name.
2817 Directory to save to is default to `gnus-article-save-directory'."
2818   (setq filename (gnus-read-save-file-name
2819                   "Save %s in rmail file:" filename
2820                   gnus-rmail-save-name gnus-newsgroup-name
2821                   gnus-current-headers 'gnus-newsgroup-last-rmail))
2822   (gnus-eval-in-buffer-window gnus-save-article-buffer
2823     (save-excursion
2824       (save-restriction
2825         (widen)
2826         (gnus-output-to-rmail filename))))
2827   filename)
2828
2829 (defun gnus-summary-save-in-mail (&optional filename)
2830   "Append this article to Unix mail file.
2831 Optional argument FILENAME specifies file name.
2832 Directory to save to is default to `gnus-article-save-directory'."
2833   (setq filename (gnus-read-save-file-name
2834                   "Save %s in Unix mail file:" filename
2835                   gnus-mail-save-name gnus-newsgroup-name
2836                   gnus-current-headers 'gnus-newsgroup-last-mail))
2837   (gnus-eval-in-buffer-window gnus-save-article-buffer
2838     (save-excursion
2839       (save-restriction
2840         (widen)
2841         (if (and (file-readable-p filename)
2842                  (mail-file-babyl-p filename))
2843             (rmail-output-to-rmail-file filename t)
2844           (gnus-output-to-mail filename)))))
2845   filename)
2846
2847 (defun gnus-summary-save-in-file (&optional filename overwrite)
2848   "Append this article to file.
2849 Optional argument FILENAME specifies file name.
2850 Directory to save to is default to `gnus-article-save-directory'."
2851   (setq filename (gnus-read-save-file-name
2852                   "Save %s in file:" filename
2853                   gnus-file-save-name gnus-newsgroup-name
2854                   gnus-current-headers 'gnus-newsgroup-last-file))
2855   (gnus-eval-in-buffer-window gnus-save-article-buffer
2856     (save-excursion
2857       (save-restriction
2858         (widen)
2859         (when (and overwrite
2860                    (file-exists-p filename))
2861           (delete-file filename))
2862         (gnus-output-to-file filename))))
2863   filename)
2864
2865 (defun gnus-summary-write-to-file (&optional filename)
2866   "Write this article to a file.
2867 Optional argument FILENAME specifies file name.
2868 The directory to save in defaults to `gnus-article-save-directory'."
2869   (gnus-summary-save-in-file nil t))
2870
2871 (defun gnus-summary-save-body-in-file (&optional filename)
2872   "Append this article body to a file.
2873 Optional argument FILENAME specifies file name.
2874 The directory to save in defaults to `gnus-article-save-directory'."
2875   (setq filename (gnus-read-save-file-name
2876                   "Save %s body in file:" filename
2877                   gnus-file-save-name gnus-newsgroup-name
2878                   gnus-current-headers 'gnus-newsgroup-last-file))
2879   (gnus-eval-in-buffer-window gnus-save-article-buffer
2880     (save-excursion
2881       (save-restriction
2882         (widen)
2883         (when (article-goto-body)
2884           (narrow-to-region (point) (point-max)))
2885         (gnus-output-to-file filename))))
2886   filename)
2887
2888 (defun gnus-summary-save-in-pipe (&optional command)
2889   "Pipe this article to subprocess."
2890   (setq command
2891         (cond ((and (eq command 'default)
2892                     gnus-last-shell-command)
2893                gnus-last-shell-command)
2894               ((stringp command)
2895                command)
2896               (t (read-string
2897                   (format
2898                    "Shell command on %s: "
2899                    (if (and gnus-number-of-articles-to-be-saved
2900                             (> gnus-number-of-articles-to-be-saved 1))
2901                        (format "these %d articles"
2902                                gnus-number-of-articles-to-be-saved)
2903                      "this article"))
2904                   gnus-last-shell-command))))
2905   (when (string-equal command "")
2906     (if gnus-last-shell-command
2907         (setq command gnus-last-shell-command)
2908       (error "A command is required.")))
2909   (gnus-eval-in-buffer-window gnus-article-buffer
2910     (save-restriction
2911       (widen)
2912       (shell-command-on-region (point-min) (point-max) command nil)))
2913   (setq gnus-last-shell-command command))
2914
2915 ;;; Article file names when saving.
2916
2917 (defun gnus-capitalize-newsgroup (newsgroup)
2918   "Capitalize NEWSGROUP name."
2919   (when (not (zerop (length newsgroup)))
2920     (concat (char-to-string (upcase (aref newsgroup 0)))
2921             (substring newsgroup 1))))
2922
2923 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2924   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2925 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
2926 Otherwise, it is like ~/News/news/group/num."
2927   (let ((default
2928           (expand-file-name
2929            (concat (if (gnus-use-long-file-name 'not-save)
2930                        (gnus-capitalize-newsgroup newsgroup)
2931                      (gnus-newsgroup-directory-form newsgroup))
2932                    "/" (int-to-string (mail-header-number headers)))
2933            gnus-article-save-directory)))
2934     (if (and last-file
2935              (string-equal (file-name-directory default)
2936                            (file-name-directory last-file))
2937              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2938         default
2939       (or last-file default))))
2940
2941 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2942   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2943 If variable `gnus-use-long-file-name' is non-nil, it is
2944 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2945   (let ((default
2946           (expand-file-name
2947            (concat (if (gnus-use-long-file-name 'not-save)
2948                        newsgroup
2949                      (gnus-newsgroup-directory-form newsgroup))
2950                    "/" (int-to-string (mail-header-number headers)))
2951            gnus-article-save-directory)))
2952     (if (and last-file
2953              (string-equal (file-name-directory default)
2954                            (file-name-directory last-file))
2955              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2956         default
2957       (or last-file default))))
2958
2959 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2960   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2961 If variable `gnus-use-long-file-name' is non-nil, it is
2962 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2963   (or last-file
2964       (expand-file-name
2965        (if (gnus-use-long-file-name 'not-save)
2966            newsgroup
2967          (file-relative-name
2968           (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup))
2969           default-directory))
2970        gnus-article-save-directory)))
2971
2972 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
2973   "Generate file name from sender."
2974   (let ((from (mail-header-from headers)))
2975     (expand-file-name
2976      (if (and from (string-match "\\([^ <]+\\)@" from))
2977          (match-string 1 from)
2978        "nobody")
2979      gnus-article-save-directory)))
2980
2981 (defun article-verify-x-pgp-sig ()
2982   "Verify X-PGP-Sig."
2983   (interactive)
2984   (if (gnus-buffer-live-p gnus-original-article-buffer)
2985       (let ((sig (with-current-buffer gnus-original-article-buffer
2986                    (gnus-fetch-field "X-PGP-Sig")))
2987             items info headers)
2988         (when (and sig
2989                    mml2015-use
2990                    (mml2015-clear-verify-function))
2991           (with-temp-buffer
2992             (insert-buffer gnus-original-article-buffer)
2993             (setq items (split-string sig))
2994             (message-narrow-to-head)
2995             (let ((inhibit-point-motion-hooks t)
2996                   (case-fold-search t))
2997               ;; Don't verify multiple headers.
2998               (setq headers (mapconcat (lambda (header)
2999                                          (concat header ": "
3000                                                  (mail-fetch-field header) "\n"))
3001                                        (split-string (nth 1 items) ",") "")))
3002             (delete-region (point-min) (point-max))
3003             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
3004             (insert "X-Signed-Headers: " (nth 1 items) "\n")
3005             (insert headers)
3006             (widen)
3007             (forward-line)
3008             (while (not (eobp))
3009               (if (looking-at "^-")
3010                   (insert "- "))
3011               (forward-line))
3012             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
3013             (insert "Version: " (car items) "\n\n")
3014             (insert (mapconcat 'identity (cddr items) "\n"))
3015             (insert "\n-----END PGP SIGNATURE-----\n")
3016             (let ((mm-security-handle (list (format "multipart/signed"))))
3017               (mml2015-clean-buffer)
3018               (let ((coding-system-for-write (or gnus-newsgroup-charset
3019                                                  'iso-8859-1)))
3020                 (funcall (mml2015-clear-verify-function)))
3021               (setq info
3022                     (or (mm-handle-multipart-ctl-parameter
3023                          mm-security-handle 'gnus-details)
3024                         (mm-handle-multipart-ctl-parameter
3025                          mm-security-handle 'gnus-info)))))
3026           (when info
3027             (let (buffer-read-only bface eface)
3028               (save-restriction
3029                 (message-narrow-to-head)
3030                 (goto-char (point-max))
3031                 (forward-line -1)
3032                 (setq bface (get-text-property (gnus-point-at-bol) 'face)
3033                       eface (get-text-property (1- (gnus-point-at-eol)) 'face))
3034                 (message-remove-header "X-Gnus-PGP-Verify")
3035                 (if (re-search-forward "^X-PGP-Sig:" nil t)
3036                     (forward-line)
3037                   (goto-char (point-max)))
3038                 (narrow-to-region (point) (point))
3039                 (insert "X-Gnus-PGP-Verify: " info "\n")
3040                 (goto-char (point-min))
3041                 (forward-line)
3042                 (while (not (eobp))
3043                   (if (not (looking-at "^[ \t]"))
3044                       (insert " "))
3045                   (forward-line))
3046                 ;; Do highlighting.
3047                 (goto-char (point-min))
3048                 (when (looking-at "\\([^:]+\\): *")
3049                   (put-text-property (match-beginning 1) (1+ (match-end 1))
3050                                      'face bface)
3051                   (put-text-property (match-end 0) (point-max)
3052                                      'face eface)))))))))
3053
3054 (eval-and-compile
3055   (mapcar
3056    (lambda (func)
3057      (let (afunc gfunc)
3058        (if (consp func)
3059            (setq afunc (car func)
3060                  gfunc (cdr func))
3061          (setq afunc func
3062                gfunc (intern (format "gnus-%s" func))))
3063        (defalias gfunc
3064          (if (fboundp afunc)
3065            `(lambda (&optional interactive &rest args)
3066               ,(documentation afunc t)
3067               (interactive (list t))
3068               (save-excursion
3069                 (set-buffer gnus-article-buffer)
3070                 (if interactive
3071                     (call-interactively ',afunc)
3072                   (apply ',afunc args))))))))
3073    '(article-hide-headers
3074      article-verify-x-pgp-sig
3075      article-hide-boring-headers
3076      article-toggle-headers
3077      article-treat-overstrike
3078      article-fill-long-lines
3079      article-capitalize-sentences
3080      article-remove-cr
3081      article-remove-leading-whitespace
3082      article-display-x-face
3083      article-de-quoted-unreadable
3084      article-de-base64-unreadable
3085      article-decode-HZ
3086      article-wash-html
3087      article-hide-list-identifiers
3088      article-hide-pgp
3089      article-strip-banner
3090      article-babel
3091      article-hide-pem
3092      article-hide-signature
3093      article-strip-headers-in-body
3094      article-remove-trailing-blank-lines
3095      article-strip-leading-blank-lines
3096      article-strip-multiple-blank-lines
3097      article-strip-leading-space
3098      article-strip-trailing-space
3099      article-strip-blank-lines
3100      article-strip-all-blank-lines
3101      article-date-local
3102      article-date-english
3103      article-date-iso8601
3104      article-date-original
3105      article-date-ut
3106      article-decode-mime-words
3107      article-decode-charset
3108      article-decode-encoded-words
3109      article-date-user
3110      article-date-lapsed
3111      article-emphasize
3112      article-treat-dumbquotes
3113      article-normalize-headers
3114      (article-show-all-headers . gnus-article-show-all-headers)
3115      (article-show-all . gnus-article-show-all))))
3116 \f
3117 ;;;
3118 ;;; Gnus article mode
3119 ;;;
3120
3121 (put 'gnus-article-mode 'mode-class 'special)
3122
3123 (gnus-define-keys gnus-article-mode-map
3124   " " gnus-article-goto-next-page
3125   "\177" gnus-article-goto-prev-page
3126   [delete] gnus-article-goto-prev-page
3127   [backspace] gnus-article-goto-prev-page
3128   "\C-c^" gnus-article-refer-article
3129   "h" gnus-article-show-summary
3130   "s" gnus-article-show-summary
3131   "\C-c\C-m" gnus-article-mail
3132   "?" gnus-article-describe-briefly
3133   "e" gnus-summary-edit-article
3134   "<" beginning-of-buffer
3135   ">" end-of-buffer
3136   "\C-c\C-i" gnus-info-find-node
3137   "\C-c\C-b" gnus-bug
3138   "\C-hk" gnus-article-describe-key
3139   "\C-hc" gnus-article-describe-key-briefly
3140
3141   "\C-d" gnus-article-read-summary-keys
3142   "\M-*" gnus-article-read-summary-keys
3143   "\M-#" gnus-article-read-summary-keys
3144   "\M-^" gnus-article-read-summary-keys
3145   "\M-g" gnus-article-read-summary-keys)
3146
3147 ;; Define almost undefined keys to `gnus-article-read-summary-keys'.
3148 (let (keys)
3149   (let ((key 32))
3150     (while (<= key 127)
3151       (push (char-to-string key) keys)
3152       (incf key))
3153     (dolist (elem (accessible-keymaps gnus-summary-mode-map))
3154       (setq key (car elem))
3155       (when (and (> (length key) 0)
3156                  (not (eq 'menu-bar (aref key 0)))
3157                  (symbolp (lookup-key gnus-summary-mode-map key)))
3158         (push key keys))))
3159   (dolist (key keys)
3160     (unless (lookup-key gnus-article-mode-map key)
3161       (define-key gnus-article-mode-map key 'gnus-article-read-summary-keys))))
3162
3163 (defun gnus-article-make-menu-bar ()
3164   (unless (boundp 'gnus-article-commands-menu)
3165     (gnus-summary-make-menu-bar))
3166   (gnus-turn-off-edit-menu 'article)
3167   (unless (boundp 'gnus-article-article-menu)
3168     (easy-menu-define
3169      gnus-article-article-menu gnus-article-mode-map ""
3170      '("Article"
3171        ["Scroll forwards" gnus-article-goto-next-page t]
3172        ["Scroll backwards" gnus-article-goto-prev-page t]
3173        ["Show summary" gnus-article-show-summary t]
3174        ["Fetch Message-ID at point" gnus-article-refer-article t]
3175        ["Mail to address at point" gnus-article-mail t]
3176        ["Send a bug report" gnus-bug t]))
3177
3178     (easy-menu-define
3179      gnus-article-treatment-menu gnus-article-mode-map ""
3180      ;; Fixme: this should use :active (and maybe :visible).
3181      '("Treatment"
3182        ["Hide headers" gnus-article-toggle-headers t]
3183        ["Hide signature" gnus-article-hide-signature t]
3184        ["Hide citation" gnus-article-hide-citation t]
3185        ["Treat overstrike" gnus-article-treat-overstrike t]
3186        ["Remove carriage return" gnus-article-remove-cr t]
3187        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
3188        ["Decode HZ" gnus-article-decode-HZ t]))
3189
3190     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3191
3192     ;; Note "Post" menu is defined in gnus-sum.el for consistency
3193
3194     (gnus-run-hooks 'gnus-article-menu-hook)))
3195
3196 ;; Fixme: do something for the Emacs tool bar in Article mode a la
3197 ;; Summary.
3198
3199 (defun gnus-article-mode ()
3200   "Major mode for displaying an article.
3201
3202 All normal editing commands are switched off.
3203
3204 The following commands are available in addition to all summary mode
3205 commands:
3206 \\<gnus-article-mode-map>
3207 \\[gnus-article-next-page]\t Scroll the article one page forwards
3208 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3209 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3210 \\[gnus-article-show-summary]\t Display the summary buffer
3211 \\[gnus-article-mail]\t Send a reply to the address near point
3212 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3213 \\[gnus-info-find-node]\t Go to the Gnus info node"
3214   (interactive)
3215   (gnus-simplify-mode-line)
3216   (setq mode-name "Article")
3217   (setq major-mode 'gnus-article-mode)
3218   (make-local-variable 'minor-mode-alist)
3219   (unless (assq 'gnus-show-mime minor-mode-alist)
3220     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
3221   (use-local-map gnus-article-mode-map)
3222   (when (gnus-visual-p 'article-menu 'menu)
3223     (gnus-article-make-menu-bar))
3224   (gnus-update-format-specifications nil 'article-mode)
3225   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3226   (make-local-variable 'gnus-page-broken)
3227   (make-local-variable 'gnus-button-marker-list)
3228   (make-local-variable 'gnus-article-current-summary)
3229   (make-local-variable 'gnus-article-mime-handles)
3230   (make-local-variable 'gnus-article-decoded-p)
3231   (make-local-variable 'gnus-article-mime-handle-alist)
3232   (make-local-variable 'gnus-article-wash-types)
3233   (make-local-variable 'gnus-article-charset)
3234   (make-local-variable 'gnus-article-ignored-charsets)
3235   (gnus-set-default-directory)
3236   (buffer-disable-undo)
3237   (setq buffer-read-only t)
3238   (set-syntax-table gnus-article-mode-syntax-table)
3239   (gnus-run-hooks 'gnus-article-mode-hook))
3240
3241 (defun gnus-article-setup-buffer ()
3242   "Initialize the article buffer."
3243   (let* ((name (if gnus-single-article-buffer "*Article*"
3244                  (concat "*Article " gnus-newsgroup-name "*")))
3245          (original
3246           (progn (string-match "\\*Article" name)
3247                  (concat " *Original Article"
3248                          (substring name (match-end 0))))))
3249     (setq gnus-article-buffer name)
3250     (setq gnus-original-article-buffer original)
3251     (setq gnus-article-mime-handle-alist nil)
3252     ;; This might be a variable local to the summary buffer.
3253     (unless gnus-single-article-buffer
3254       (save-excursion
3255         (set-buffer gnus-summary-buffer)
3256         (setq gnus-article-buffer name)
3257         (setq gnus-original-article-buffer original)
3258         (gnus-set-global-variables)))
3259     (gnus-article-setup-highlight-words)
3260     ;; Init original article buffer.
3261     (save-excursion
3262       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3263       (set-buffer-multibyte nil)
3264       (setq major-mode 'gnus-original-article-mode)
3265       (make-local-variable 'gnus-original-article))
3266     (if (get-buffer name)
3267         (save-excursion
3268           (set-buffer name)
3269           (buffer-disable-undo)
3270           (setq buffer-read-only t)
3271           (unless (eq major-mode 'gnus-article-mode)
3272             (gnus-article-mode))
3273           (current-buffer))
3274       (save-excursion
3275         (set-buffer (gnus-get-buffer-create name))
3276         (gnus-article-mode)
3277         (make-local-variable 'gnus-summary-buffer)
3278         (gnus-summary-set-local-parameters gnus-newsgroup-name)
3279         (current-buffer)))))
3280
3281 ;; Set article window start at LINE, where LINE is the number of lines
3282 ;; from the head of the article.
3283 (defun gnus-article-set-window-start (&optional line)
3284   (set-window-start
3285    (get-buffer-window gnus-article-buffer t)
3286    (save-excursion
3287      (set-buffer gnus-article-buffer)
3288      (goto-char (point-min))
3289      (if (not line)
3290          (point-min)
3291        (gnus-message 6 "Moved to bookmark")
3292        (search-forward "\n\n" nil t)
3293        (forward-line line)
3294        (point)))))
3295
3296 ;;; @@ article filters
3297 ;;;
3298
3299 (defun gnus-article-display-mime-message ()
3300   "Article display method for MIME message."
3301   ;; called from `gnus-original-article-buffer'.
3302   (let (charset all-headers)
3303     (with-current-buffer gnus-summary-buffer
3304       (setq charset default-mime-charset
3305             all-headers gnus-have-all-headers))
3306     (make-local-variable 'default-mime-charset)
3307     (setq default-mime-charset charset)
3308     (with-current-buffer (get-buffer-create gnus-article-buffer)
3309       (make-local-variable 'default-mime-charset)
3310       (setq default-mime-charset charset))
3311     (mime-display-message mime-message-structure
3312                           gnus-article-buffer nil gnus-article-mode-map)
3313     (when all-headers
3314       (gnus-article-hide-headers nil -1)))
3315   (run-hooks 'gnus-mime-article-prepare-hook))
3316
3317 (defun gnus-article-display-traditional-message ()
3318   "Article display method for traditional message."
3319   (set-buffer gnus-article-buffer)
3320   (let (buffer-read-only)
3321     (erase-buffer)
3322     (insert-buffer-substring gnus-original-article-buffer)))
3323
3324 (defun gnus-article-make-full-mail-header (&optional number charset)
3325   "Create a new mail header structure in a raw article buffer."
3326   (unless (and number charset)
3327     (save-current-buffer
3328       (set-buffer gnus-summary-buffer)
3329       (unless number
3330         (setq number (or (cdr gnus-article-current) 0)))
3331       (unless charset
3332         (setq charset (or default-mime-charset 'x-ctext)))))
3333   (goto-char (point-min))
3334   (let ((header-end (if (search-forward "\n\n" nil t)
3335                         (1- (point))
3336                       (goto-char (point-max))))
3337         (chars (- (point-max) (point)))
3338         (lines (count-lines (point) (point-max)))
3339         (default-mime-charset charset)
3340         xref)
3341     (narrow-to-region (point-min) header-end)
3342     (setq xref (std11-fetch-field "xref"))
3343     (prog1
3344         (make-full-mail-header
3345          number
3346          (std11-fetch-field "subject")
3347          (std11-fetch-field "from")
3348          (std11-fetch-field "date")
3349          (std11-fetch-field "message-id")
3350          (std11-fetch-field "references")
3351          chars
3352          lines
3353          (when xref (concat "Xref: " xref)))
3354       (widen))))
3355
3356 (defun gnus-article-prepare (article &optional all-headers header)
3357   "Prepare ARTICLE in article mode buffer.
3358 ARTICLE should either be an article number or a Message-ID.
3359 If ARTICLE is an id, HEADER should be the article headers.
3360 If ALL-HEADERS is non-nil, no headers are hidden."
3361   (save-excursion
3362     ;; Make sure we start in a summary buffer.
3363     (unless (eq major-mode 'gnus-summary-mode)
3364       (set-buffer gnus-summary-buffer))
3365     (setq gnus-summary-buffer (current-buffer))
3366     (let* ((gnus-article (if header (mail-header-number header) article))
3367            (summary-buffer (current-buffer))
3368            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3369            (group gnus-newsgroup-name)
3370            result)
3371       (save-excursion
3372         (gnus-article-setup-buffer)
3373         (set-buffer gnus-article-buffer)
3374         ;; Deactivate active regions.
3375         (when (and (boundp 'transient-mark-mode)
3376                    transient-mark-mode)
3377           (setq mark-active nil))
3378         (if (not (setq result (let ((buffer-read-only nil))
3379                                 (gnus-request-article-this-buffer
3380                                  article group))))
3381             ;; There is no such article.
3382             (save-excursion
3383               (when (and (numberp article)
3384                          (not (memq article gnus-newsgroup-sparse)))
3385                 (setq gnus-article-current
3386                       (cons gnus-newsgroup-name article))
3387                 (set-buffer gnus-summary-buffer)
3388                 (setq gnus-current-article article)
3389                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
3390                     (progn
3391                       (gnus-summary-set-agent-mark article)
3392                       (message "Message marked for downloading"))
3393                   (gnus-summary-mark-article article gnus-canceled-mark)
3394                   (unless (memq article gnus-newsgroup-sparse)
3395                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
3396           (if (or (eq result 'pseudo)
3397                   (eq result 'nneething))
3398               (progn
3399                 (save-excursion
3400                   (set-buffer summary-buffer)
3401                   (push article gnus-newsgroup-history)
3402                   (setq gnus-last-article gnus-current-article
3403                         gnus-current-article 0
3404                         gnus-current-headers nil
3405                         gnus-article-current nil)
3406                   (if (eq result 'nneething)
3407                       (gnus-configure-windows 'summary)
3408                     (gnus-configure-windows 'article))
3409                   (gnus-set-global-variables))
3410                 (let ((gnus-article-mime-handle-alist-1
3411                        gnus-article-mime-handle-alist))
3412                   (gnus-set-mode-line 'article)))
3413             ;; The result from the `request' was an actual article -
3414             ;; or at least some text that is now displayed in the
3415             ;; article buffer.
3416             (when (and (numberp article)
3417                        (not (eq article gnus-current-article)))
3418               ;; Seems like a new article has been selected.
3419               ;; `gnus-current-article' must be an article number.
3420               (save-excursion
3421                 (set-buffer summary-buffer)
3422                 (push article gnus-newsgroup-history)
3423                 (setq gnus-last-article gnus-current-article
3424                       gnus-current-article article
3425                       gnus-current-headers
3426                       (gnus-summary-article-header gnus-current-article)
3427                       gnus-article-current
3428                       (cons gnus-newsgroup-name gnus-current-article))
3429                 (unless (vectorp gnus-current-headers)
3430                   (setq gnus-current-headers nil))
3431                 (gnus-summary-goto-subject gnus-current-article)
3432                 (when (gnus-summary-show-thread)
3433                   ;; If the summary buffer really was folded, the
3434                   ;; previous goto may not actually have gone to
3435                   ;; the right article, but the thread root instead.
3436                   ;; So we go again.
3437                   (gnus-summary-goto-subject gnus-current-article))
3438                 (gnus-run-hooks 'gnus-mark-article-hook)
3439                 (gnus-set-mode-line 'summary)
3440                 (when (gnus-visual-p 'article-highlight 'highlight)
3441                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
3442                 ;; Set the global newsgroup variables here.
3443                 (gnus-set-global-variables)
3444                 (setq gnus-have-all-headers
3445                       (or all-headers gnus-show-all-headers))))
3446             (save-excursion
3447               (gnus-configure-windows 'article))
3448             (when (or (numberp article)
3449                       (stringp article))
3450               (gnus-article-prepare-display)
3451               ;; Do page break.
3452               (goto-char (point-min))
3453               (setq gnus-page-broken
3454                     (when gnus-break-pages
3455                       (gnus-narrow-to-page)
3456                       t)))
3457             (let ((gnus-article-mime-handle-alist-1
3458                    gnus-article-mime-handle-alist))
3459               (gnus-set-mode-line 'article))
3460             (article-goto-body)
3461             (set-window-point (get-buffer-window (current-buffer)) (point))
3462             (gnus-configure-windows 'article)
3463             t))))))
3464
3465 (defun gnus-article-prepare-mime-display (&optional number)
3466   (goto-char (point-min))
3467   (when (re-search-forward "^[^\t ]+:" nil t)
3468     (goto-char (match-beginning 0)))
3469   (let ((entity (if (eq 1 (point-min))
3470                     (get-text-property 1 'mime-view-entity)
3471                   (get-text-property (point) 'mime-view-entity)))
3472         last-entity child-entity next type)
3473     (setq child-entity (mime-entity-children entity))
3474     (if child-entity
3475         (setq last-entity (nth (1- (length child-entity))
3476                                child-entity))
3477       (setq last-entity entity))
3478     (save-restriction
3479       (narrow-to-region (point)
3480                         (if (search-forward "\n\n" nil t)
3481                             (point)
3482                           (point-max)))
3483       (gnus-treat-article 'head)
3484       (put-text-property (point-min) (point-max) 'article-treated-header t)
3485       (goto-char (point-max)))
3486     (while (and (not (eobp)) entity)
3487       (setq next (set-marker
3488                   (make-marker)
3489                   (next-single-property-change (point) 'mime-view-entity
3490                                                nil (point-max))))
3491       (let ((types (mime-entity-content-type entity)))
3492         (while (eq 'multipart (mime-content-type-primary-type types))
3493           (setq entity (car (mime-entity-children entity))
3494                 types (mime-entity-content-type entity)))
3495         (when types
3496           (setq type (format "%s/%s"
3497                              (mime-content-type-primary-type types)
3498                              (mime-content-type-subtype types)))))
3499       (if (string-equal type "message/rfc822")
3500           (progn
3501             (setq next (point))
3502             (let ((children (mime-entity-children entity))
3503                   last-children)
3504               (when children
3505                 (setq last-children (nth (1- (length children)) children))
3506                 (while
3507                     (and
3508                      (not (eq last-children
3509                               (get-text-property next 'mime-view-entity)))
3510                      (setq next
3511                            (next-single-property-change next
3512                                                         'mime-view-entity
3513                                                         nil (point-max)))))))
3514             (setq next (next-single-property-change next 'mime-view-entity
3515                                                     nil (point-max)))
3516             (save-restriction
3517               (narrow-to-region (point) next)
3518               (gnus-article-prepare-mime-display)
3519               (goto-char (point-max)))
3520             (setq entity (get-text-property (point) 'mime-view-entity)))
3521         (save-restriction
3522           (narrow-to-region (point) next)
3523           ;; Kludge. We have to count true number, but for now,
3524           ;; part number is here only to achieve `last'.
3525           (gnus-treat-article nil 1
3526                               (if (eq entity last-entity)
3527                                   1 2)
3528                               type)
3529           (goto-char (point-max)))
3530         (setq entity (get-text-property next 'mime-view-entity))))))
3531
3532 ;;;###autoload
3533 (defun gnus-article-prepare-display ()
3534   "Make the current buffer look like a nice article."
3535   (let ((gnus-article-buffer (current-buffer))
3536         buffer-read-only)
3537     (unless (eq major-mode 'gnus-article-mode)
3538       (gnus-article-mode))
3539     (setq buffer-read-only nil
3540           gnus-button-marker-list nil
3541           gnus-article-wash-types nil)
3542     (save-restriction
3543       (widen)
3544       (static-if (featurep 'xemacs)
3545           (map-extents (lambda (extent maparg) (delete-extent extent)))
3546         (let ((lists (overlay-lists)))
3547           (dolist (overlay (nconc (car lists) (cdr lists)))
3548             (delete-overlay overlay)))))
3549     (gnus-run-hooks 'gnus-tmp-internal-hook))
3550   (set-buffer gnus-original-article-buffer)
3551   ;; Display message.
3552   (setq mime-message-structure gnus-current-headers)
3553   (mime-buffer-entity-set-buffer-internal mime-message-structure
3554                                           gnus-original-article-buffer)
3555   (mime-entity-set-representation-type-internal mime-message-structure
3556                                                 'mime-buffer-entity)
3557   (luna-send mime-message-structure 'initialize-instance
3558              mime-message-structure)
3559   (if gnus-show-mime
3560       (let (mime-display-header-hook mime-display-text/plain-hook)
3561         (funcall gnus-article-display-method-for-mime))
3562     (funcall gnus-article-display-method-for-traditional))
3563   ;; Call the treatment functions.
3564   (let ((inhibit-read-only t))
3565     (save-restriction
3566       (widen)
3567       (if gnus-show-mime
3568           (gnus-article-prepare-mime-display)
3569         (narrow-to-region (goto-char (point-min))
3570                           (if (search-forward "\n\n" nil t)
3571                               (point)
3572                             (point-max)))
3573         (gnus-treat-article 'head)
3574         (put-text-property (point-min) (point-max) 'article-treated-header t)
3575         (goto-char (point-max))
3576         (widen)
3577         (narrow-to-region (point) (point-max))
3578         (gnus-treat-article nil))
3579       (put-text-property (point-min) (point-max) 'read-only nil)))
3580   (gnus-run-hooks 'gnus-article-prepare-hook))
3581
3582 (defun gnus-article-decode-article-as-default-mime-charset ()
3583   "Decode an article as `default-mime-charset'.  It won't work if the
3584 value of the variable `gnus-show-mime' is non-nil."
3585   (unless gnus-show-mime
3586     (set (make-local-variable 'default-mime-charset)
3587          (with-current-buffer gnus-summary-buffer
3588            default-mime-charset))
3589     (decode-mime-charset-region (point-min) (point-max)
3590                                 default-mime-charset)))
3591
3592 ;;;
3593 ;;; Gnus MIME viewing functions
3594 ;;;
3595
3596 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
3597   "The following specs can be used:
3598 %t  The MIME type
3599 %T  MIME type, along with additional info
3600 %n  The `name' parameter
3601 %d  The description, if any
3602 %l  The length of the encoded part
3603 %p  The part identifier number
3604 %e  Dots if the part isn't displayed")
3605
3606 (defvar gnus-mime-button-line-format-alist
3607   '((?t gnus-tmp-type ?s)
3608     (?T gnus-tmp-type-long ?s)
3609     (?n gnus-tmp-name ?s)
3610     (?d gnus-tmp-description ?s)
3611     (?p gnus-tmp-id ?s)
3612     (?l gnus-tmp-length ?d)
3613     (?e gnus-tmp-dots ?s)))
3614
3615 (defvar gnus-mime-button-commands
3616   '((gnus-article-press-button "\r" "Toggle Display")
3617     (gnus-mime-view-part "v" "View Interactively...")
3618     (gnus-mime-view-part-as-type "t" "View As Type...")
3619     (gnus-mime-view-part-as-charset "C" "View As charset...")
3620     (gnus-mime-save-part "o" "Save...")
3621     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
3622     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
3623     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
3624     (gnus-mime-internalize-part "E" "View Internally")
3625     (gnus-mime-externalize-part "e" "View Externally")
3626     (gnus-mime-pipe-part "|" "Pipe To Command...")
3627     (gnus-mime-action-on-part "." "Take action on the part")))
3628
3629 (defun gnus-article-mime-part-status ()
3630   (with-current-buffer gnus-article-buffer
3631     (let ((entity (get-text-property (point-min) 'mime-view-entity))
3632           children)
3633       (if (and entity
3634                (setq children (mime-entity-children entity))
3635                (setq children (length children)))
3636           (if (eq 1 children)
3637               " (1 part)"
3638             (format " (%d parts)" children))
3639         ""))))
3640
3641 (defvar gnus-mime-button-map
3642   (let ((map (make-sparse-keymap)))
3643     (unless (>= (string-to-number emacs-version) 21)
3644       ;; XEmacs doesn't care.
3645       (set-keymap-parent map gnus-article-mode-map))
3646     (define-key map gnus-mouse-2 'gnus-article-push-button)
3647     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
3648     (dolist (c gnus-mime-button-commands)
3649       (define-key map (cadr c) (car c)))
3650     map))
3651
3652 (defun gnus-mime-button-menu (event)
3653   "Construct a context-sensitive menu of MIME commands."
3654   (interactive "e")
3655   (save-window-excursion
3656     (let ((pos (event-start event)))
3657       (select-window (posn-window pos))
3658       (goto-char (posn-point pos))
3659       (gnus-article-check-buffer)
3660       (let ((response (x-popup-menu
3661                        t `("MIME Part"
3662                            ("" ,@(mapcar (lambda (c)
3663                                            (cons (caddr c) (car c)))
3664                                          gnus-mime-button-commands))))))
3665         (if response
3666             (call-interactively response))))))
3667
3668 (defun gnus-mime-view-all-parts (&optional handles)
3669   "View all the MIME parts."
3670   (interactive)
3671   (save-current-buffer
3672     (set-buffer gnus-article-buffer)
3673     (let ((handles (or handles gnus-article-mime-handles))
3674           (mail-parse-charset gnus-newsgroup-charset)
3675           (mail-parse-ignored-charsets
3676            (with-current-buffer gnus-summary-buffer
3677              gnus-newsgroup-ignored-charsets)))
3678       (when handles
3679         (mm-remove-parts handles)
3680         (goto-char (point-min))
3681         (or (search-forward "\n\n") (goto-char (point-max)))
3682         (let (buffer-read-only)
3683           (delete-region (point) (point-max)))
3684         (mm-display-parts handles)))))
3685
3686 (defun gnus-mime-save-part-and-strip ()
3687   "Save the MIME part under point then replace it with an external body."
3688   (interactive)
3689   (gnus-article-check-buffer)
3690   (let* ((data (get-text-property (point) 'gnus-data))
3691          file param)
3692     (if (mm-multiple-handles gnus-article-mime-handles)
3693         (error "This function is not implemented."))
3694     (setq file (and data (mm-save-part data)))
3695     (when file
3696       (with-current-buffer (mm-handle-buffer data)
3697         (erase-buffer)
3698         (insert "Content-Type: " (mm-handle-media-type data))
3699         (mml-insert-parameter-string (cdr (mm-handle-type data))
3700                                      '(charset))
3701         (insert "\n")
3702         (insert "Content-ID: " (message-make-message-id) "\n")
3703         (insert "Content-Transfer-Encoding: binary\n")
3704         (insert "\n"))
3705       (setcdr data
3706               (cdr (mm-make-handle nil
3707                                    `("message/external-body"
3708                                      (access-type . "LOCAL-FILE")
3709                                      (name . ,file)))))
3710       (set-buffer gnus-summary-buffer)
3711       (gnus-article-edit-article
3712        `(lambda ()
3713            (erase-buffer)
3714            (let ((mail-parse-charset (or gnus-article-charset
3715                                          ',gnus-newsgroup-charset))
3716                  (mail-parse-ignored-charsets
3717                   (or gnus-article-ignored-charsets
3718                       ',gnus-newsgroup-ignored-charsets))
3719                  (mbl mml-buffer-list))
3720              (setq mml-buffer-list nil)
3721              (insert-buffer gnus-original-article-buffer)
3722              (mime-to-mml gnus-article-mime-handles)
3723              (setq gnus-article-mime-handles nil)
3724              (make-local-hook 'kill-buffer-hook)
3725              (let ((mbl1 mml-buffer-list))
3726                (setq mml-buffer-list mbl)
3727                (set (make-local-variable 'mml-buffer-list) mbl1))
3728              (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
3729        `(lambda (no-highlight)
3730           (let ((mail-parse-charset (or gnus-article-charset
3731                                         ',gnus-newsgroup-charset))
3732                 (message-options message-options)
3733                 (message-options-set-recipient)
3734                 (mail-parse-ignored-charsets
3735                  (or gnus-article-ignored-charsets
3736                      ',gnus-newsgroup-ignored-charsets)))
3737            (mml-to-mime)
3738            (mml-destroy-buffers)
3739            (remove-hook 'kill-buffer-hook
3740                         'mml-destroy-buffers t)
3741            (kill-local-variable 'mml-buffer-list))
3742           (gnus-summary-edit-article-done
3743            ,(or (mail-header-references gnus-current-headers) "")
3744            ,(gnus-group-read-only-p)
3745            ,gnus-summary-buffer no-highlight))))))
3746
3747 (defun gnus-mime-save-part ()
3748   "Save the MIME part under point."
3749   (interactive)
3750   (gnus-article-check-buffer)
3751   (let ((data (get-text-property (point) 'gnus-data)))
3752     (when data
3753       (mm-save-part data))))
3754
3755 (defun gnus-mime-pipe-part ()
3756   "Pipe the MIME part under point to a process."
3757   (interactive)
3758   (gnus-article-check-buffer)
3759   (let ((data (get-text-property (point) 'gnus-data)))
3760     (when data
3761       (mm-pipe-part data))))
3762
3763 (defun gnus-mime-view-part ()
3764   "Interactively choose a viewing method for the MIME part under point."
3765   (interactive)
3766   (gnus-article-check-buffer)
3767   (let ((data (get-text-property (point) 'gnus-data)))
3768     (when data
3769       (setq gnus-article-mime-handles
3770             (mm-merge-handles
3771              gnus-article-mime-handles (setq data (copy-sequence data))))
3772       (mm-interactively-view-part data))))
3773
3774 (defun gnus-mime-view-part-as-type-internal ()
3775   (gnus-article-check-buffer)
3776   (let* ((name (mail-content-type-get
3777                 (mm-handle-type (get-text-property (point) 'gnus-data))
3778                 'name))
3779          (def-type (and name (mm-default-file-encoding name))))
3780     (and def-type (cons def-type 0))))
3781
3782 (defun gnus-mime-view-part-as-type (&optional mime-type)
3783   "Choose a MIME media type, and view the part as such."
3784   (interactive)
3785   (unless mime-type
3786     (setq mime-type (completing-read
3787                      "View as MIME type: "
3788                      (mapcar #'list (mailcap-mime-types))
3789                      nil nil
3790                      (gnus-mime-view-part-as-type-internal))))
3791   (gnus-article-check-buffer)
3792   (let ((handle (get-text-property (point) 'gnus-data)))
3793     (when handle
3794       (setq handle
3795             (mm-make-handle (mm-handle-buffer handle)
3796                             (cons mime-type (cdr (mm-handle-type handle)))
3797                             (mm-handle-encoding handle)
3798                             (mm-handle-undisplayer handle)
3799                             (mm-handle-disposition handle)
3800                             (mm-handle-description handle)
3801                             (mm-handle-cache handle)
3802                             (mm-handle-id handle)))
3803       (setq gnus-article-mime-handles
3804             (mm-merge-handles gnus-article-mime-handles handle))
3805       (gnus-mm-display-part handle))))
3806
3807 (defun gnus-mime-copy-part (&optional handle)
3808   "Put the the MIME part under point into a new buffer."
3809   (interactive)
3810   (gnus-article-check-buffer)
3811   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3812          (contents (and handle (mm-get-part handle)))
3813          (base (and handle
3814                     (file-name-nondirectory
3815                      (or
3816                       (mail-content-type-get (mm-handle-type handle) 'name)
3817                       (mail-content-type-get (mm-handle-type handle)
3818                                              'filename)
3819                       "*decoded*"))))
3820          (buffer (and base (generate-new-buffer base))))
3821     (when contents
3822       (switch-to-buffer buffer)
3823       (insert contents)
3824       ;; We do it this way to make `normal-mode' set the appropriate mode.
3825       (unwind-protect
3826           (progn
3827             (setq buffer-file-name (expand-file-name base))
3828             (normal-mode))
3829         (setq buffer-file-name nil))
3830       (goto-char (point-min)))))
3831
3832 (defun gnus-mime-inline-part (&optional handle arg)
3833   "Insert the MIME part under point into the current buffer."
3834   (interactive (list nil current-prefix-arg))
3835   (gnus-article-check-buffer)
3836   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3837          contents charset
3838          (b (point))
3839          buffer-read-only)
3840     (when handle
3841       (if (and (not arg) (mm-handle-undisplayer handle))
3842           (mm-remove-part handle)
3843         (setq contents (mm-get-part handle))
3844         (cond
3845          ((not arg)
3846           (setq charset (or (mail-content-type-get
3847                              (mm-handle-type handle) 'charset)
3848                             gnus-newsgroup-charset)))
3849          ((numberp arg)
3850           (if (mm-handle-undisplayer handle)
3851               (mm-remove-part handle))
3852           (setq charset
3853                 (or (cdr (assq arg
3854                                gnus-summary-show-article-charset-alist))
3855                     (read-coding-system "Charset: ")))))
3856         (forward-line 2)
3857         (mm-insert-inline handle
3858                           (if (and charset
3859                                    (setq charset (mm-charset-to-coding-system
3860                                                   charset))
3861                                    (not (eq charset 'ascii)))
3862                               (mm-decode-coding-string contents charset)
3863                             contents))
3864         (goto-char b)))))
3865
3866 (defun gnus-mime-view-part-as-charset (&optional handle arg)
3867   "Insert the MIME part under point into the current buffer using the
3868 specified charset."
3869   (interactive (list nil current-prefix-arg))
3870   (gnus-article-check-buffer)
3871   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3872          contents charset
3873          (b (point))
3874          buffer-read-only)
3875     (when handle
3876       (if (mm-handle-undisplayer handle)
3877           (mm-remove-part handle))
3878       (let ((gnus-newsgroup-charset
3879              (or (cdr (assq arg
3880                             gnus-summary-show-article-charset-alist))
3881                  (read-coding-system "Charset: ")))
3882           (gnus-newsgroup-ignored-charsets 'gnus-all))
3883         (gnus-article-press-button)))))
3884
3885 (defun gnus-mime-externalize-part (&optional handle)
3886   "View the MIME part under point with an external viewer."
3887   (interactive)
3888   (gnus-article-check-buffer)
3889   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3890          (mm-user-display-methods nil)
3891          (mm-inlined-types nil)
3892          (mail-parse-charset gnus-newsgroup-charset)
3893          (mail-parse-ignored-charsets
3894           (save-excursion (set-buffer gnus-summary-buffer)
3895                           gnus-newsgroup-ignored-charsets)))
3896     (when handle
3897       (if (mm-handle-undisplayer handle)
3898           (mm-remove-part handle)
3899         (mm-display-part handle)))))
3900
3901 (defun gnus-mime-internalize-part (&optional handle)
3902   "View the MIME part under point with an internal viewer.
3903 If no internal viewer is available, use an external viewer."
3904   (interactive)
3905   (gnus-article-check-buffer)
3906   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3907          (mm-inlined-types '(".*"))
3908          (mm-inline-large-images t)
3909          (mail-parse-charset gnus-newsgroup-charset)
3910          (mail-parse-ignored-charsets
3911           (save-excursion (set-buffer gnus-summary-buffer)
3912                           gnus-newsgroup-ignored-charsets)))
3913     (when handle
3914       (if (mm-handle-undisplayer handle)
3915           (mm-remove-part handle)
3916         (mm-display-part handle)))))
3917
3918 (defun gnus-mime-action-on-part (&optional action)
3919   "Do something with the MIME attachment at \(point\)."
3920   (interactive
3921    (list (completing-read "Action: " gnus-mime-action-alist)))
3922   (gnus-article-check-buffer)
3923   (let ((action-pair (assoc action gnus-mime-action-alist)))
3924     (if action-pair
3925         (funcall (cdr action-pair)))))
3926
3927 (defun gnus-article-part-wrapper (n function)
3928   (save-current-buffer
3929     (set-buffer gnus-article-buffer)
3930     (when (> n (length gnus-article-mime-handle-alist))
3931       (error "No such part"))
3932     (gnus-article-goto-part n)
3933     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3934       (funcall function handle))))
3935
3936 (defun gnus-article-pipe-part (n)
3937   "Pipe MIME part N, which is the numerical prefix."
3938   (interactive "p")
3939   (gnus-article-part-wrapper n 'mm-pipe-part))
3940
3941 (defun gnus-article-save-part (n)
3942   "Save MIME part N, which is the numerical prefix."
3943   (interactive "p")
3944   (gnus-article-part-wrapper n 'mm-save-part))
3945
3946 (defun gnus-article-interactively-view-part (n)
3947   "View MIME part N interactively, which is the numerical prefix."
3948   (interactive "p")
3949   (gnus-article-part-wrapper n 'mm-interactively-view-part))
3950
3951 (defun gnus-article-copy-part (n)
3952   "Copy MIME part N, which is the numerical prefix."
3953   (interactive "p")
3954   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
3955
3956 (defun gnus-article-view-part-as-charset (n)
3957   "Copy MIME part N, which is the numerical prefix."
3958   (interactive "p")
3959   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
3960
3961 (defun gnus-article-externalize-part (n)
3962   "View MIME part N externally, which is the numerical prefix."
3963   (interactive "p")
3964   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
3965
3966 (defun gnus-article-inline-part (n)
3967   "Inline MIME part N, which is the numerical prefix."
3968   (interactive "p")
3969   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
3970
3971 (defun gnus-article-mime-match-handle-first (condition)
3972   (if condition
3973       (let ((alist gnus-article-mime-handle-alist) ihandle n)
3974         (while (setq ihandle (pop alist))
3975           (if (and (cond
3976                     ((functionp condition)
3977                      (funcall condition (cdr ihandle)))
3978                     ((eq condition 'undisplayed)
3979                      (not (or (mm-handle-undisplayer (cdr ihandle))
3980                               (equal (mm-handle-media-type (cdr ihandle))
3981                                      "multipart/alternative"))))
3982                     ((eq condition 'undisplayed-alternative)
3983                      (not (mm-handle-undisplayer (cdr ihandle))))
3984                     (t t))
3985                    (gnus-article-goto-part (car ihandle))
3986                    (or (not n) (< (car ihandle) n)))
3987               (setq n (car ihandle))))
3988         (or n 1))
3989     1))
3990
3991 (defun gnus-article-view-part (&optional n)
3992   "View MIME part N, which is the numerical prefix."
3993   (interactive "P")
3994   (save-current-buffer
3995     (set-buffer gnus-article-buffer)
3996     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
3997                              gnus-article-mime-match-handle-function)))
3998     (when (> n (length gnus-article-mime-handle-alist))
3999       (error "No such part"))
4000     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4001       (when (gnus-article-goto-part n)
4002         (if (equal (car handle) "multipart/alternative")
4003             (gnus-article-press-button)
4004           (when (eq (gnus-mm-display-part handle) 'internal)
4005             (gnus-set-window-start)))))))
4006
4007 (defsubst gnus-article-mime-total-parts ()
4008   (if (bufferp (car gnus-article-mime-handles))
4009       1 ;; single part
4010     (1- (length gnus-article-mime-handles))))
4011
4012 (defun gnus-mm-display-part (handle)
4013   "Display HANDLE and fix MIME button."
4014   (let ((id (get-text-property (point) 'gnus-part))
4015         (point (point))
4016         buffer-read-only)
4017     (forward-line 1)
4018     (prog1
4019         (let ((window (selected-window))
4020               (mail-parse-charset gnus-newsgroup-charset)
4021               (mail-parse-ignored-charsets
4022                (save-excursion (set-buffer gnus-summary-buffer)
4023                                gnus-newsgroup-ignored-charsets)))
4024           (save-excursion
4025             (unwind-protect
4026                 (let ((win (get-buffer-window (current-buffer) t))
4027                       (beg (point)))
4028                   (when win
4029                     (select-window win))
4030                   (goto-char point)
4031                   (forward-line)
4032                   (if (mm-handle-displayed-p handle)
4033                       ;; This will remove the part.
4034                       (mm-display-part handle)
4035                     (save-restriction
4036                       (narrow-to-region (point) (1+ (point)))
4037                       (mm-display-part handle)
4038                       ;; We narrow to the part itself and
4039                       ;; then call the treatment functions.
4040                       (goto-char (point-min))
4041                       (forward-line 1)
4042                       (narrow-to-region (point) (point-max))
4043                       (gnus-treat-article
4044                        nil id
4045                        (gnus-article-mime-total-parts)
4046                        (mm-handle-media-type handle)))))
4047               (select-window window))))
4048       (goto-char point)
4049       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
4050       (gnus-insert-mime-button
4051        handle id (list (mm-handle-displayed-p handle)))
4052       (goto-char point))))
4053
4054 (defun gnus-article-goto-part (n)
4055   "Go to MIME part N."
4056   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
4057     (when point
4058       (goto-char point))))
4059
4060 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4061   (let ((gnus-tmp-name
4062          (or (mail-content-type-get (mm-handle-type handle)
4063                                     'name)
4064              (mail-content-type-get (mm-handle-disposition handle)
4065                                     'filename)
4066              (mail-content-type-get (mm-handle-type handle)
4067                                     'url)
4068              ""))
4069         (gnus-tmp-type (mm-handle-media-type handle))
4070         (gnus-tmp-description
4071          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4072                                               "")))
4073         (gnus-tmp-dots
4074          (if (if displayed (car displayed)
4075                (mm-handle-displayed-p handle))
4076              "" "..."))
4077         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4078                            (buffer-size)))
4079         gnus-tmp-type-long b e)
4080     (when (string-match ".*/" gnus-tmp-name)
4081       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4082     (setq gnus-tmp-type-long (concat gnus-tmp-type
4083                                      (and (not (equal gnus-tmp-name ""))
4084                                           (concat "; " gnus-tmp-name))))
4085     (or (equal gnus-tmp-description "")
4086         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4087     (unless (bolp)
4088       (insert "\n"))
4089     (setq b (point))
4090     (gnus-eval-format
4091      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4092      `(keymap ,gnus-mime-button-map
4093               ,@(if (>= (string-to-number emacs-version) 21)
4094                     nil
4095                   (list 'local-map gnus-mime-button-map))
4096               gnus-callback gnus-mm-display-part
4097               gnus-part ,gnus-tmp-id
4098               article-type annotation
4099               gnus-data ,handle))
4100     (setq e (point))
4101     (widget-convert-button
4102      'link b e
4103      :mime-handle handle
4104      :action 'gnus-widget-press-button
4105      :button-keymap gnus-mime-button-map
4106      :help-echo
4107      (lambda (widget/window &optional overlay pos)
4108        ;; Needed to properly clear the message due to a bug in
4109        ;; wid-edit (XEmacs only).
4110        (if (boundp 'help-echo-owns-message)
4111            (setq help-echo-owns-message t))
4112        (format
4113         "%S: %s the MIME part; %S: more options"
4114         (aref gnus-mouse-2 0)
4115         ;; XEmacs will get a single widget arg; Emacs 21 will get
4116         ;; window, overlay, position.
4117         (if (mm-handle-displayed-p
4118              (if overlay
4119                  (with-current-buffer (gnus-overlay-buffer overlay)
4120                    (widget-get (widget-at (gnus-overlay-start overlay))
4121                                :mime-handle))
4122                (widget-get widget/window :mime-handle)))
4123             "hide" "show")
4124         (aref gnus-down-mouse-3 0))))))
4125
4126 (defun gnus-widget-press-button (elems el)
4127   (goto-char (widget-get elems :from))
4128   (gnus-article-press-button))
4129
4130 (defvar gnus-displaying-mime nil)
4131
4132 (defun gnus-display-mime (&optional ihandles)
4133   "Display the MIME parts."
4134   (save-excursion
4135     (save-selected-window
4136       (let ((window (get-buffer-window gnus-article-buffer))
4137             (point (point)))
4138         (when window
4139           (select-window window)
4140           ;; We have to do this since selecting the window
4141           ;; may change the point.  So we set the window point.
4142           (set-window-point window point)))
4143       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
4144              buffer-read-only handle name type b e display)
4145         (when (and (not ihandles)
4146                    (not gnus-displaying-mime))
4147           ;; Top-level call; we clean up.
4148           (when gnus-article-mime-handles
4149             (mm-destroy-parts gnus-article-mime-handles)
4150             (setq gnus-article-mime-handle-alist nil));; A trick.
4151           (setq gnus-article-mime-handles handles)
4152           ;; We allow users to glean info from the handles.
4153           (when gnus-article-mime-part-function
4154             (gnus-mime-part-function handles)))
4155         (if (and handles
4156                  (or (not (stringp (car handles)))
4157                      (cdr handles)))
4158             (progn
4159               (when (and (not ihandles)
4160                          (not gnus-displaying-mime))
4161                 ;; Clean up for mime parts.
4162                 (article-goto-body)
4163                 (delete-region (point) (point-max)))
4164               (let ((gnus-displaying-mime t))
4165                 (gnus-mime-display-part handles)))
4166           (save-restriction
4167             (article-goto-body)
4168             (narrow-to-region (point) (point-max))
4169             (gnus-treat-article nil 1 1)
4170             (widen)))
4171         (unless ihandles
4172           ;; Highlight the headers.
4173           (save-excursion
4174             (save-restriction
4175               (article-goto-body)
4176               (narrow-to-region (point-min) (point))
4177               (gnus-treat-article 'head))))))))
4178
4179 (defvar gnus-mime-display-multipart-as-mixed nil)
4180 (defvar gnus-mime-display-multipart-alternative-as-mixed nil)
4181 (defvar gnus-mime-display-multipart-related-as-mixed nil)
4182
4183 (defun gnus-mime-display-part (handle)
4184   (cond
4185    ;; Single part.
4186    ((not (stringp (car handle)))
4187     (gnus-mime-display-single handle))
4188    ;; User-defined multipart
4189    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
4190     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
4191              handle))
4192    ;; multipart/alternative
4193    ((and (equal (car handle) "multipart/alternative")
4194          (not (or gnus-mime-display-multipart-as-mixed
4195                   gnus-mime-display-multipart-alternative-as-mixed)))
4196     (let ((id (1+ (length gnus-article-mime-handle-alist))))
4197       (push (cons id handle) gnus-article-mime-handle-alist)
4198       (gnus-mime-display-alternative (cdr handle) nil nil id)))
4199    ;; multipart/related
4200    ((and (equal (car handle) "multipart/related")
4201          (not (or gnus-mime-display-multipart-as-mixed
4202                   gnus-mime-display-multipart-related-as-mixed)))
4203     ;;;!!!We should find the start part, but we just default
4204     ;;;!!!to the first part.
4205     ;;(gnus-mime-display-part (cadr handle))
4206     ;;;!!! Most multipart/related is an HTML message plus images.
4207     ;;;!!! Unfortunately we are unable to let W3 display those
4208     ;;;!!! included images, so we just display it as a mixed multipart.
4209     ;;(gnus-mime-display-mixed (cdr handle))
4210     ;;;!!! No, w3 can display everything just fine.
4211     (gnus-mime-display-part (cadr handle)))
4212    ((equal (car handle) "multipart/signed")
4213     (or (memq 'signed gnus-article-wash-types)
4214         (push 'signed gnus-article-wash-types))
4215     (gnus-mime-display-security handle))
4216    ((equal (car handle) "multipart/encrypted")
4217     (or (memq 'encrypted gnus-article-wash-types)
4218         (push 'encrypted gnus-article-wash-types))
4219     (gnus-mime-display-security handle))
4220    ;; Other multiparts are handled like multipart/mixed.
4221    (t
4222     (gnus-mime-display-mixed (cdr handle)))))
4223
4224 (defun gnus-mime-part-function (handles)
4225   (if (stringp (car handles))
4226       (mapcar 'gnus-mime-part-function (cdr handles))
4227     (funcall gnus-article-mime-part-function handles)))
4228
4229 (defun gnus-mime-display-mixed (handles)
4230   (mapcar 'gnus-mime-display-part handles))
4231
4232 (defun gnus-mime-display-single (handle)
4233   (let ((type (mm-handle-media-type handle))
4234         (ignored gnus-ignored-mime-types)
4235         (not-attachment t)
4236         (move nil)
4237         display text)
4238     (catch 'ignored
4239       (progn
4240         (while ignored
4241           (when (string-match (pop ignored) type)
4242             (throw 'ignored nil)))
4243         (if (and (setq not-attachment
4244                        (and (not (mm-inline-override-p handle))
4245                             (or (not (mm-handle-disposition handle))
4246                                 (equal (car (mm-handle-disposition handle))
4247                                        "inline")
4248                                 (mm-attachment-override-p handle))))
4249                  (mm-automatic-display-p handle)
4250                  (or (mm-inlined-p handle)
4251                      (mm-automatic-external-display-p type)))
4252             (setq display t)
4253           (when (equal (mm-handle-media-supertype handle) "text")
4254             (setq text t)))
4255         (let ((id (1+ (length gnus-article-mime-handle-alist)))
4256               beg)
4257           (push (cons id handle) gnus-article-mime-handle-alist)
4258           (when (or (not display)
4259                     (not (gnus-unbuttonized-mime-type-p type)))
4260             ;(gnus-article-insert-newline)
4261             (gnus-insert-mime-button
4262              handle id (list (or display (and not-attachment text))))
4263             (gnus-article-insert-newline)
4264             ;(gnus-article-insert-newline)
4265             ;; Remember modify the number of forward lines.
4266             (setq move t))
4267           (setq beg (point))
4268           (cond
4269            (display
4270             (when move
4271               (forward-line -1)
4272               (setq beg (point)))
4273             (let ((mail-parse-charset gnus-newsgroup-charset)
4274                   (mail-parse-ignored-charsets
4275                    (save-excursion (condition-case ()
4276                                        (set-buffer gnus-summary-buffer)
4277                                      (error))
4278                                    gnus-newsgroup-ignored-charsets)))
4279               (mm-display-part handle t))
4280             (goto-char (point-max)))
4281            ((and text not-attachment)
4282             (when move
4283               (forward-line -1)
4284               (setq beg (point)))
4285             (gnus-article-insert-newline)
4286             (mm-insert-inline handle (mm-get-part handle))
4287             (goto-char (point-max))))
4288           ;; Do highlighting.
4289           (save-excursion
4290             (save-restriction
4291               (narrow-to-region beg (point))
4292               (gnus-treat-article
4293                nil id
4294                (gnus-article-mime-total-parts)
4295                (mm-handle-media-type handle)))))))))
4296
4297 (defun gnus-unbuttonized-mime-type-p (type)
4298   "Say whether TYPE is to be unbuttonized."
4299   (unless gnus-inhibit-mime-unbuttonizing
4300     (catch 'found
4301       (let ((types gnus-unbuttonized-mime-types))
4302         (while types
4303           (when (string-match (pop types) type)
4304             (throw 'found t)))))))
4305
4306 (defun gnus-article-insert-newline ()
4307   "Insert a newline, but mark it as undeletable."
4308   (gnus-put-text-property
4309    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
4310
4311 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
4312   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
4313          (ihandles handles)
4314          (point (point))
4315          handle buffer-read-only from props begend not-pref)
4316     (save-window-excursion
4317       (save-restriction
4318         (when ibegend
4319           (narrow-to-region (car ibegend)
4320                             (or (cdr ibegend)
4321                                 (progn
4322                                   (goto-char (car ibegend))
4323                                   (forward-line 2)
4324                                   (point))))
4325           (delete-region (point-min) (point-max))
4326           (mm-remove-parts handles))
4327         (setq begend (list (point-marker)))
4328         ;; Do the toggle.
4329         (unless (setq not-pref (cadr (member preferred ihandles)))
4330           (setq not-pref (car ihandles)))
4331         (when (or ibegend
4332                   (not preferred)
4333                   (not (gnus-unbuttonized-mime-type-p
4334                         "multipart/alternative")))
4335           (gnus-add-text-properties
4336            (setq from (point))
4337            (progn
4338              (insert (format "%d.  " id))
4339              (point))
4340            `(gnus-callback
4341              (lambda (handles)
4342                (unless ,(not ibegend)
4343                  (setq gnus-article-mime-handle-alist
4344                        ',gnus-article-mime-handle-alist))
4345                (gnus-mime-display-alternative
4346                 ',ihandles ',not-pref ',begend ,id))
4347              ,@(if (>= (string-to-number emacs-version) 21)
4348                    nil ;; XEmacs doesn't care
4349                  (list 'local-map gnus-mime-button-map))
4350              ,gnus-mouse-face-prop ,gnus-article-mouse-face
4351              face ,gnus-article-button-face
4352              keymap ,gnus-mime-button-map
4353              gnus-part ,id
4354              gnus-data ,handle))
4355           (widget-convert-button 'link from (point)
4356                                  :action 'gnus-widget-press-button
4357                                  :button-keymap gnus-widget-button-keymap)
4358           ;; Do the handles
4359           (while (setq handle (pop handles))
4360             (gnus-add-text-properties
4361              (setq from (point))
4362              (progn
4363                (insert (format "(%c) %-18s"
4364                                (if (equal handle preferred) ?* ? )
4365                                (mm-handle-media-type handle)))
4366                (point))
4367              `(gnus-callback
4368                (lambda (handles)
4369                  (unless ,(not ibegend)
4370                    (setq gnus-article-mime-handle-alist
4371                          ',gnus-article-mime-handle-alist))
4372                  (gnus-mime-display-alternative
4373                   ',ihandles ',handle ',begend ,id))
4374                ,@(if (>= (string-to-number emacs-version) 21)
4375                      nil ;; XEmacs doesn't care
4376                    (list 'local-map gnus-mime-button-map))
4377                ,gnus-mouse-face-prop ,gnus-article-mouse-face
4378                face ,gnus-article-button-face
4379                keymap ,gnus-mime-button-map
4380                gnus-part ,id
4381                gnus-data ,handle))
4382             (widget-convert-button 'link from (point)
4383                                    :action 'gnus-widget-press-button
4384                                    :button-keymap gnus-widget-button-keymap)
4385             (insert "  "))
4386           (insert "\n\n"))
4387         (when preferred
4388           (if (stringp (car preferred))
4389               (gnus-display-mime preferred)
4390             (let ((mail-parse-charset gnus-newsgroup-charset)
4391                   (mail-parse-ignored-charsets
4392                    (save-excursion (set-buffer gnus-summary-buffer)
4393                                    gnus-newsgroup-ignored-charsets)))
4394               (mm-display-part preferred)
4395               ;; Do highlighting.
4396               (save-excursion
4397                 (save-restriction
4398                   (narrow-to-region (car begend) (point-max))
4399                   (gnus-treat-article
4400                    nil (length gnus-article-mime-handle-alist)
4401                    (gnus-article-mime-total-parts)
4402                    (mm-handle-media-type handle))))))
4403           (goto-char (point-max))
4404           (setcdr begend (point-marker)))))
4405     (when ibegend
4406       (goto-char point))))
4407
4408 (defun gnus-article-wash-status ()
4409   "Return a string which display status of article washing."
4410   (save-excursion
4411     (set-buffer gnus-article-buffer)
4412     (let ((cite (memq 'cite gnus-article-wash-types))
4413           (headers (memq 'headers gnus-article-wash-types))
4414           (boring (memq 'boring-headers gnus-article-wash-types))
4415           (pgp (memq 'pgp gnus-article-wash-types))
4416           (pem (memq 'pem gnus-article-wash-types))
4417           (signed (memq 'signed gnus-article-wash-types))
4418           (encrypted (memq 'encrypted gnus-article-wash-types))
4419           (signature (memq 'signature gnus-article-wash-types))
4420           (overstrike (memq 'overstrike gnus-article-wash-types))
4421           (emphasis (memq 'emphasis gnus-article-wash-types)))
4422       (format "%c%c%c%c%c%c%c"
4423               (if cite ?c ? )
4424               (if (or headers boring) ?h ? )
4425               (if (or pgp pem signed encrypted) ?p ? )
4426               (if signature ?s ? )
4427               (if overstrike ?o ? )
4428               (if gnus-show-mime ?m ? )
4429               (if emphasis ?e ? )))))
4430
4431 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
4432
4433 (defun gnus-article-maybe-hide-headers ()
4434   "Hide unwanted headers if `gnus-have-all-headers' is nil.
4435 Provided for backwards compatibility."
4436   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
4437                  (not (save-excursion (set-buffer gnus-summary-buffer)
4438                                       gnus-have-all-headers)))
4439              (not gnus-inhibit-hiding))
4440     (gnus-article-hide-headers)))
4441
4442 ;;; Article savers.
4443
4444 (defun gnus-output-to-file (file-name)
4445   "Append the current article to a file named FILE-NAME."
4446   (let ((artbuf (current-buffer)))
4447     (with-temp-buffer
4448       (insert-buffer-substring artbuf)
4449       ;; Append newline at end of the buffer as separator, and then
4450       ;; save it to file.
4451       (goto-char (point-max))
4452       (insert "\n")
4453       (write-region-as-binary (point-min) (point-max) file-name 'append)
4454       t)))
4455
4456 (defun gnus-narrow-to-page (&optional arg)
4457   "Narrow the article buffer to a page.
4458 If given a numerical ARG, move forward ARG pages."
4459   (interactive "P")
4460   (setq arg (if arg (prefix-numeric-value arg) 0))
4461   (save-excursion
4462     (set-buffer gnus-article-buffer)
4463     (goto-char (point-min))
4464     (widen)
4465     ;; Remove any old next/prev buttons.
4466     (when (gnus-visual-p 'page-marker)
4467       (let ((buffer-read-only nil))
4468         (gnus-remove-text-with-property 'gnus-prev)
4469         (gnus-remove-text-with-property 'gnus-next)))
4470     (when
4471         (cond ((< arg 0)
4472                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
4473               ((> arg 0)
4474                (re-search-forward page-delimiter nil 'move arg)))
4475       (goto-char (match-end 0)))
4476     (narrow-to-region
4477      (point)
4478      (if (re-search-forward page-delimiter nil 'move)
4479          (match-beginning 0)
4480        (point)))
4481     (when (and (gnus-visual-p 'page-marker)
4482                (not (= (point-min) 1)))
4483       (save-excursion
4484         (goto-char (point-min))
4485         (gnus-insert-prev-page-button)))
4486     (when (and (gnus-visual-p 'page-marker)
4487                (< (+ (point-max) 2) (buffer-size)))
4488       (save-excursion
4489         (goto-char (point-max))
4490         (gnus-insert-next-page-button)))))
4491
4492 ;; Article mode commands
4493
4494 (defun gnus-article-goto-next-page ()
4495   "Show the next page of the article."
4496   (interactive)
4497   (when (gnus-article-next-page)
4498     (goto-char (point-min))
4499     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
4500
4501 (defun gnus-article-goto-prev-page ()
4502   "Show the next page of the article."
4503   (interactive)
4504   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
4505     (gnus-article-prev-page nil)))
4506
4507 (defun gnus-article-next-page (&optional lines)
4508   "Show the next page of the current article.
4509 If end of article, return non-nil.  Otherwise return nil.
4510 Argument LINES specifies lines to be scrolled up."
4511   (interactive "p")
4512   (let ((start (window-start))
4513         end-of-buffer end-of-page)
4514     (save-excursion
4515       (move-to-window-line -1)
4516       (if (<= (point) start)
4517           (progn
4518             (forward-line 2)
4519             (setq start (point)))
4520         (forward-line 1)
4521         (setq start nil))
4522       (unless (or (cond ((eq (1+ (buffer-size)) (point))
4523                          (and (pos-visible-in-window-p)
4524                               (setq end-of-buffer t)))
4525                         ((eobp)
4526                          (setq end-of-page t)))
4527                   (not lines))
4528         (move-to-window-line lines)
4529         (unless (search-backward "\n\n" nil t)
4530           (setq start (point)))))
4531     (cond (end-of-buffer t)
4532           (end-of-page
4533            (gnus-narrow-to-page 1)
4534            nil)
4535           (t
4536            (if start
4537                (set-window-start (selected-window) start)
4538              (let (window-pixel-scroll-increment)
4539                (scroll-up lines)))
4540            nil))))
4541
4542 (defun gnus-article-prev-page (&optional lines)
4543   "Show previous page of current article.
4544 Argument LINES specifies lines to be scrolled down."
4545   (interactive "p")
4546   (let (beginning-of-buffer beginning-of-page)
4547     (save-excursion
4548       (move-to-window-line 0)
4549       (cond ((eq 1 (point))
4550              (setq beginning-of-buffer t))
4551             ((bobp)
4552              (setq beginning-of-page t))))
4553     (cond (beginning-of-buffer)
4554           (beginning-of-page
4555            (gnus-narrow-to-page -1))
4556           (t
4557            (condition-case nil
4558                (let (window-pixel-scroll-increment)
4559                  (scroll-down lines))
4560              (beginning-of-buffer
4561               (goto-char (point-min))))))))
4562
4563 (defun gnus-article-refer-article ()
4564   "Read article specified by message-id around point."
4565   (interactive)
4566   (let ((point (point)))
4567     (search-forward ">" nil t)          ;Move point to end of "<....>".
4568     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
4569         (let ((message-id (match-string 1)))
4570           (goto-char point)
4571           (set-buffer gnus-summary-buffer)
4572           (gnus-summary-refer-article message-id))
4573       (goto-char (point))
4574       (error "No references around point"))))
4575
4576 (defun gnus-article-show-summary ()
4577   "Reconfigure windows to show summary buffer."
4578   (interactive)
4579   (if (not (gnus-buffer-live-p gnus-summary-buffer))
4580       (error "There is no summary buffer for this article buffer")
4581     (gnus-article-set-globals)
4582     (gnus-configure-windows 'article)
4583     (gnus-summary-goto-subject gnus-current-article)
4584     (gnus-summary-position-point)))
4585
4586 (defun gnus-article-describe-briefly ()
4587   "Describe article mode commands briefly."
4588   (interactive)
4589   (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")))
4590
4591 (defun gnus-article-summary-command ()
4592   "Execute the last keystroke in the summary buffer."
4593   (interactive)
4594   (let ((obuf (current-buffer))
4595         (owin (current-window-configuration))
4596         func)
4597     (switch-to-buffer gnus-article-current-summary 'norecord)
4598     (setq func (lookup-key (current-local-map) (this-command-keys)))
4599     (call-interactively func)
4600     (set-buffer obuf)
4601     (set-window-configuration owin)
4602     (set-window-point (get-buffer-window (current-buffer)) (point))))
4603
4604 (defun gnus-article-summary-command-nosave ()
4605   "Execute the last keystroke in the summary buffer."
4606   (interactive)
4607   (let (func)
4608     (pop-to-buffer gnus-article-current-summary 'norecord)
4609     (setq func (lookup-key (current-local-map) (this-command-keys)))
4610     (call-interactively func)))
4611
4612 (defun gnus-article-check-buffer ()
4613   "Beep if not in an article buffer."
4614   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
4615     (error "Command invoked outside of a Gnus article buffer")))
4616
4617 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
4618   "Read a summary buffer key sequence and execute it from the article buffer."
4619   (interactive "P")
4620   (gnus-article-check-buffer)
4621   (let ((nosaves
4622          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
4623            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
4624            "=" "^" "\M-^" "|"))
4625         (nosave-but-article
4626          '("A\r"))
4627         (nosave-in-article
4628          '("\C-d"))
4629         (up-to-top
4630          '("n" "Gn" "p" "Gp"))
4631         keys new-sum-point)
4632     (save-excursion
4633       (set-buffer gnus-article-current-summary)
4634       (let (gnus-pick-mode)
4635         (push (or key last-command-event) unread-command-events)
4636         (setq keys (static-if (featurep 'xemacs)
4637                        (events-to-keys (read-key-sequence nil))
4638                      (read-key-sequence nil)))))
4639     (message "")
4640
4641     (if (or (member keys nosaves)
4642             (member keys nosave-but-article)
4643             (member keys nosave-in-article))
4644         (let (func)
4645           (save-window-excursion
4646             (pop-to-buffer gnus-article-current-summary 'norecord)
4647             ;; We disable the pick minor mode commands.
4648             (let (gnus-pick-mode)
4649               (setq func (lookup-key (current-local-map) keys))))
4650           (if (or (not func)
4651                   (numberp func))
4652               (ding)
4653             (unless (member keys nosave-in-article)
4654               (set-buffer gnus-article-current-summary))
4655             (call-interactively func)
4656             (setq new-sum-point (point)))
4657           (when (member keys nosave-but-article)
4658             (pop-to-buffer gnus-article-buffer 'norecord)))
4659       ;; These commands should restore window configuration.
4660       (let ((obuf (current-buffer))
4661             (owin (current-window-configuration))
4662             (opoint (point))
4663             (summary gnus-article-current-summary)
4664             func in-buffer selected)
4665         (if not-restore-window
4666             (pop-to-buffer summary 'norecord)
4667           (switch-to-buffer summary 'norecord))
4668         (setq in-buffer (current-buffer))
4669         ;; We disable the pick minor mode commands.
4670         (if (and (setq func (let (gnus-pick-mode)
4671                               (lookup-key (current-local-map) keys)))
4672                  (functionp func))
4673             (progn
4674               (call-interactively func)
4675               (setq new-sum-point (point))
4676               (when (eq in-buffer (current-buffer))
4677                 (setq selected (gnus-summary-select-article))
4678                 (set-buffer obuf)
4679                 (unless not-restore-window
4680                   (set-window-configuration owin))
4681                 (when (eq selected 'old)
4682                   (article-goto-body)
4683                   (set-window-start (get-buffer-window (current-buffer))
4684                                     1)
4685                   (set-window-point (get-buffer-window (current-buffer))
4686                                     (point)))
4687                 (let ((win (get-buffer-window gnus-article-current-summary)))
4688                   (when win
4689                     (set-window-point win new-sum-point))))    )
4690           (switch-to-buffer gnus-article-buffer)
4691           (ding))))))
4692
4693 (defun gnus-article-describe-key (key)
4694   "Display documentation of the function invoked by KEY.  KEY is a string."
4695   (interactive "kDescribe key: ")
4696   (gnus-article-check-buffer)
4697   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4698       (save-excursion
4699         (set-buffer gnus-article-current-summary)
4700         (let (gnus-pick-mode)
4701           (if (featurep 'xemacs)
4702               (progn
4703                 (push (elt key 0) unread-command-events)
4704                 (setq key (events-to-keys
4705                            (read-key-sequence "Describe key: "))))
4706             (setq unread-command-events
4707                   (mapcar
4708                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
4709                    key))
4710             (setq key (read-key-sequence "Describe key: "))))
4711         (describe-key key))
4712     (describe-key key)))
4713
4714 (defun gnus-article-describe-key-briefly (key &optional insert)
4715   "Display documentation of the function invoked by KEY.  KEY is a string."
4716   (interactive "kDescribe key: \nP")
4717   (gnus-article-check-buffer)
4718   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4719       (save-excursion
4720         (set-buffer gnus-article-current-summary)
4721         (let (gnus-pick-mode)
4722           (if (featurep 'xemacs)
4723               (progn
4724                 (push (elt key 0) unread-command-events)
4725                 (setq key (events-to-keys
4726                            (read-key-sequence "Describe key: "))))
4727             (setq unread-command-events
4728                   (mapcar
4729                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
4730                    key))
4731             (setq key (read-key-sequence "Describe key: "))))
4732         (describe-key-briefly key insert))
4733     (describe-key-briefly key insert)))
4734
4735 (defun gnus-article-hide (&optional arg force)
4736   "Hide all the gruft in the current article.
4737 This means that PGP stuff, signatures, cited text and (some)
4738 headers will be hidden.
4739 If given a prefix, show the hidden text instead."
4740   (interactive (append (gnus-article-hidden-arg) (list 'force)))
4741   (gnus-article-hide-headers arg)
4742   (gnus-article-hide-list-identifiers arg)
4743   (gnus-article-hide-pgp arg)
4744   (gnus-article-hide-citation-maybe arg force)
4745   (gnus-article-hide-signature arg))
4746
4747 (defun gnus-article-maybe-highlight ()
4748   "Do some article highlighting if article highlighting is requested."
4749   (when (gnus-visual-p 'article-highlight 'highlight)
4750     (gnus-article-highlight-some)))
4751
4752 (defun gnus-check-group-server ()
4753   ;; Make sure the connection to the server is alive.
4754   (unless (gnus-server-opened
4755            (gnus-find-method-for-group gnus-newsgroup-name))
4756     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
4757     (gnus-request-group gnus-newsgroup-name t)))
4758
4759 (defun gnus-request-article-this-buffer (article group)
4760   "Get an article and insert it into this buffer."
4761   (let (do-update-line sparse-header)
4762     (prog1
4763         (save-excursion
4764           (erase-buffer)
4765           (gnus-kill-all-overlays)
4766           (setq group (or group gnus-newsgroup-name))
4767
4768           ;; Using `gnus-request-article' directly will insert the article into
4769           ;; `nntp-server-buffer' - so we'll save some time by not having to
4770           ;; copy it from the server buffer into the article buffer.
4771
4772           ;; We only request an article by message-id when we do not have the
4773           ;; headers for it, so we'll have to get those.
4774           (when (stringp article)
4775             (gnus-read-header article))
4776
4777           ;; If the article number is negative, that means that this article
4778           ;; doesn't belong in this newsgroup (possibly), so we find its
4779           ;; message-id and request it by id instead of number.
4780           (when (and (numberp article)
4781                      gnus-summary-buffer
4782                      (get-buffer gnus-summary-buffer)
4783                      (gnus-buffer-exists-p gnus-summary-buffer))
4784             (save-excursion
4785               (set-buffer gnus-summary-buffer)
4786               (let ((header (gnus-summary-article-header article)))
4787                 (when (< article 0)
4788                   (cond
4789                    ((memq article gnus-newsgroup-sparse)
4790                     ;; This is a sparse gap article.
4791                     (setq do-update-line article)
4792                     (setq article (mail-header-id header))
4793                     (setq sparse-header (gnus-read-header article))
4794                     (setq gnus-newsgroup-sparse
4795                           (delq article gnus-newsgroup-sparse)))
4796                    ((vectorp header)
4797                     ;; It's a real article.
4798                     (setq article (mail-header-id header)))
4799                    (t
4800                     ;; It is an extracted pseudo-article.
4801                     (setq article 'pseudo)
4802                     (gnus-request-pseudo-article header))))
4803
4804                 (let ((method (gnus-find-method-for-group
4805                                gnus-newsgroup-name)))
4806                   (when (and (eq (car method) 'nneething)
4807                              (vectorp header))
4808                     (let ((dir (expand-file-name
4809                                 (mail-header-subject header)
4810                                 (file-name-as-directory
4811                                  (or (cadr (assq 'nneething-address method))
4812                                      (nth 1 method))))))
4813                       (when (file-directory-p dir)
4814                         (setq article 'nneething)
4815                         (gnus-group-enter-directory dir))))))))
4816
4817           (cond
4818            ;; Refuse to select canceled articles.
4819            ((and (numberp article)
4820                  gnus-summary-buffer
4821                  (get-buffer gnus-summary-buffer)
4822                  (gnus-buffer-exists-p gnus-summary-buffer)
4823                  (eq (cdr (save-excursion
4824                             (set-buffer gnus-summary-buffer)
4825                             (assq article gnus-newsgroup-reads)))
4826                      gnus-canceled-mark))
4827             nil)
4828            ;; Check the backlog.
4829            ((and gnus-keep-backlog
4830                  (gnus-backlog-request-article group article (current-buffer)))
4831             'article)
4832            ;; Check asynchronous pre-fetch.
4833            ((gnus-async-request-fetched-article group article (current-buffer))
4834             (gnus-async-prefetch-next group article gnus-summary-buffer)
4835             (when (and (numberp article) gnus-keep-backlog)
4836               (gnus-backlog-enter-article group article (current-buffer)))
4837             'article)
4838            ;; Check the cache.
4839            ((and gnus-use-cache
4840                  (numberp article)
4841                  (gnus-cache-request-article article group))
4842             'article)
4843            ;; Get the article and put into the article buffer.
4844            ((or (stringp article)
4845                 (numberp article))
4846             (let ((gnus-override-method gnus-override-method)
4847                   (methods (and (stringp article)
4848                                 gnus-refer-article-method))
4849                   result
4850                   (buffer-read-only nil))
4851               (if (or (not (listp methods))
4852                       (and (symbolp (car methods))
4853                            (assq (car methods) nnoo-definition-alist)))
4854                   (setq methods (list methods)))
4855               (when (and (null gnus-override-method)
4856                          methods)
4857                 (setq gnus-override-method (pop methods)))
4858               (while (not result)
4859                 (when (eq gnus-override-method 'current)
4860                   (setq gnus-override-method gnus-current-select-method))
4861                 (erase-buffer)
4862                 (gnus-kill-all-overlays)
4863                 (let ((gnus-newsgroup-name group))
4864                   (gnus-check-group-server))
4865                 (when (gnus-request-article article group (current-buffer))
4866                   (when (numberp article)
4867                     (gnus-async-prefetch-next group article
4868                                               gnus-summary-buffer)
4869                     (when gnus-keep-backlog
4870                       (gnus-backlog-enter-article
4871                        group article (current-buffer))))
4872                   (setq result 'article))
4873                 (if (not result)
4874                     (if methods
4875                         (setq gnus-override-method (pop methods))
4876                       (setq result 'done))))
4877               (and (eq result 'article) 'article)))
4878            ;; It was a pseudo.
4879            (t article)))
4880
4881       ;; Associate this article with the current summary buffer.
4882       (setq gnus-article-current-summary gnus-summary-buffer)
4883
4884       ;; Take the article from the original article buffer
4885       ;; and place it in the buffer it's supposed to be in.
4886       (when (and (get-buffer gnus-article-buffer)
4887                  (equal (buffer-name (current-buffer))
4888                         (buffer-name (get-buffer gnus-article-buffer))))
4889         (save-excursion
4890           (if (get-buffer gnus-original-article-buffer)
4891               (set-buffer gnus-original-article-buffer)
4892             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
4893             (set-buffer-multibyte nil)
4894             (buffer-disable-undo)
4895             (setq major-mode 'gnus-original-article-mode)
4896             (setq buffer-read-only t))
4897           (let (buffer-read-only)
4898             (erase-buffer)
4899             (insert-buffer-substring gnus-article-buffer))
4900           (setq gnus-original-article (cons group article)))
4901
4902         ;; Decode charsets.
4903         (run-hooks 'gnus-article-decode-hook)
4904         ;; Mark article as decoded or not.
4905         (setq gnus-article-decoded-p gnus-article-decode-hook))
4906
4907       ;; Update sparse articles.
4908       (when (and do-update-line
4909                  (or (numberp article)
4910                      (stringp article)))
4911         (let ((buf (current-buffer)))
4912           (set-buffer gnus-summary-buffer)
4913           (gnus-summary-update-article do-update-line sparse-header)
4914           (gnus-summary-goto-subject do-update-line nil t)
4915           (set-window-point (get-buffer-window (current-buffer) t)
4916                             (point))
4917           (set-buffer buf))))))
4918
4919 ;;;
4920 ;;; Article editing
4921 ;;;
4922
4923 (defcustom gnus-article-edit-mode-hook nil
4924   "Hook run in article edit mode buffers."
4925   :group 'gnus-article-various
4926   :type 'hook)
4927
4928 (defcustom gnus-article-edit-article-setup-function
4929   'gnus-article-mime-edit-article-setup
4930   "Function called to setup an editing article buffer."
4931   :group 'gnus-article-various
4932   :type 'function)
4933
4934 (defvar gnus-article-edit-done-function nil)
4935
4936 (defvar gnus-article-edit-mode-map nil)
4937
4938 ;; Should we be using derived.el for this?
4939 (unless gnus-article-edit-mode-map
4940   (setq gnus-article-edit-mode-map (make-sparse-keymap))
4941   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
4942
4943   (gnus-define-keys gnus-article-edit-mode-map
4944     "\C-c\C-c" gnus-article-edit-done
4945     "\C-c\C-k" gnus-article-edit-exit)
4946
4947   (gnus-define-keys (gnus-article-edit-wash-map
4948                      "\C-c\C-w" gnus-article-edit-mode-map)
4949     "f" gnus-article-edit-full-stops))
4950
4951 (defun gnus-article-edit-mode ()
4952   "Major mode for editing articles.
4953 This is an extended text-mode.
4954
4955 \\{gnus-article-edit-mode-map}"
4956   (interactive)
4957   (setq major-mode 'gnus-article-edit-mode)
4958   (setq mode-name "Article Edit")
4959   (use-local-map gnus-article-edit-mode-map)
4960   (make-local-variable 'gnus-article-edit-done-function)
4961   (make-local-variable 'gnus-prev-winconf)
4962   (setq buffer-read-only nil)
4963   (buffer-enable-undo)
4964   (widen)
4965   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
4966
4967 (defun gnus-article-edit (&optional force)
4968   "Edit the current article.
4969 This will have permanent effect only in mail groups.
4970 If FORCE is non-nil, allow editing of articles even in read-only
4971 groups."
4972   (interactive "P")
4973   (when (and (not force)
4974              (gnus-group-read-only-p))
4975     (error "The current newsgroup does not support article editing"))
4976   (gnus-article-date-original)
4977   (gnus-article-edit-article
4978    'ignore
4979    `(lambda (no-highlight)
4980       'ignore
4981       (gnus-summary-edit-article-done
4982        ,(or (mail-header-references gnus-current-headers) "")
4983        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
4984
4985 (defun gnus-article-edit-article (start-func exit-func)
4986   "Start editing the contents of the current article buffer."
4987   (let ((winconf (current-window-configuration)))
4988     (set-buffer gnus-article-buffer)
4989     (gnus-article-edit-mode)
4990     (funcall start-func)
4991     (set-buffer-modified-p nil)
4992     (gnus-configure-windows 'edit-article)
4993     (setq gnus-article-edit-done-function exit-func)
4994     (setq gnus-prev-winconf winconf)
4995     (when gnus-article-edit-article-setup-function
4996       (funcall gnus-article-edit-article-setup-function))
4997     (gnus-message 6 "C-c C-c to end edits; C-c C-k to exit")))
4998
4999 (defun gnus-article-edit-done (&optional arg)
5000   "Update the article edits and exit."
5001   (interactive "P")
5002   (let ((func gnus-article-edit-done-function)
5003         (buf (current-buffer))
5004         (start (window-start)))
5005     (remove-hook 'gnus-article-mode-hook
5006                  'gnus-article-mime-edit-article-unwind)
5007     ;; We remove all text props from the article buffer.
5008     (let ((content
5009            (buffer-substring-no-properties (point-min) (point-max)))
5010           (p (point)))
5011       (erase-buffer)
5012       (insert content)
5013       (let ((winconf gnus-prev-winconf))
5014         (gnus-article-mode)
5015         (set-window-configuration winconf)
5016         ;; Tippy-toe some to make sure that point remains where it was.
5017         (save-current-buffer
5018           (set-buffer buf)
5019           (set-window-start (get-buffer-window (current-buffer)) start)
5020           (goto-char p))))
5021     (save-excursion
5022       (set-buffer buf)
5023       (let ((buffer-read-only nil))
5024         (funcall func arg))
5025       ;; The cache and backlog have to be flushed somewhat.
5026       (when gnus-keep-backlog
5027         (gnus-backlog-remove-article
5028          (car gnus-article-current) (cdr gnus-article-current)))
5029       ;; Flush original article as well.
5030       (save-excursion
5031         (when (get-buffer gnus-original-article-buffer)
5032           (set-buffer gnus-original-article-buffer)
5033           (setq gnus-original-article nil)))
5034       (when gnus-use-cache
5035         (gnus-cache-update-article
5036          (car gnus-article-current) (cdr gnus-article-current))))
5037     (set-buffer buf)
5038     (set-window-start (get-buffer-window buf) start)
5039     (set-window-point (get-buffer-window buf) (point))))
5040
5041 (defun gnus-article-edit-exit ()
5042   "Exit the article editing without updating."
5043   (interactive)
5044   (when (or (not (buffer-modified-p))
5045             (yes-or-no-p "Article modified; kill anyway? "))
5046     (let ((curbuf (current-buffer))
5047           (p (point))
5048           (window-start (window-start)))
5049       (erase-buffer)
5050       (if (gnus-buffer-live-p gnus-original-article-buffer)
5051           (insert-buffer gnus-original-article-buffer))
5052       (let ((winconf gnus-prev-winconf))
5053         (gnus-article-mode)
5054         (set-window-configuration winconf)
5055         ;; Tippy-toe some to make sure that point remains where it was.
5056         (save-current-buffer
5057           (set-buffer curbuf)
5058           (set-window-start (get-buffer-window (current-buffer)) window-start)
5059           (goto-char p))))))
5060
5061 (defun gnus-article-edit-full-stops ()
5062   "Interactively repair spacing at end of sentences."
5063   (interactive)
5064   (save-excursion
5065     (goto-char (point-min))
5066     (search-forward-regexp "^$" nil t)
5067     (let ((case-fold-search nil))
5068       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
5069
5070 ;;;
5071 ;;; Article editing with MIME-Edit
5072 ;;;
5073
5074 (defcustom gnus-article-mime-edit-article-setup-hook nil
5075   "Hook run after setting up a MIME editing article buffer."
5076   :group 'gnus-article-various
5077   :type 'hook)
5078
5079 (defun gnus-article-mime-edit-article-unwind ()
5080   "Unwind `gnus-article-buffer' if article editing was given up."
5081   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
5082   (when (featurep 'font-lock)
5083     (setq font-lock-defaults nil)
5084     (font-lock-mode -1))
5085   (when mime-edit-mode-flag
5086     (mime-edit-exit 'nomime 'no-error)
5087     (message "")))
5088
5089 (defun gnus-article-mime-edit-article-setup ()
5090   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
5091 after replacing with the original article."
5092   (setq gnus-show-mime t)
5093   (setq gnus-article-edit-done-function
5094         `(lambda (&rest args)
5095            (when mime-edit-mode-flag
5096              (let (mime-edit-insert-user-agent-field)
5097                (mime-edit-exit))
5098              (message ""))
5099            (goto-char (point-min))
5100            (let (case-fold-search)
5101              (when (re-search-forward
5102                     (format "^%s$" (regexp-quote mail-header-separator))
5103                     nil t)
5104                (replace-match "")))
5105            (apply ,gnus-article-edit-done-function args)
5106            (insert
5107             (prog1
5108                 (buffer-substring-no-properties (point-min) (point-max))
5109               (set-buffer (get-buffer-create gnus-original-article-buffer))
5110               (erase-buffer)))
5111            (setq gnus-current-headers (gnus-article-make-full-mail-header))
5112            (set-buffer gnus-article-buffer)
5113            (gnus-article-prepare-display)))
5114   (substitute-key-definition 'gnus-article-edit-done
5115                              'gnus-article-mime-edit-done
5116                              gnus-article-edit-mode-map)
5117   (substitute-key-definition 'gnus-article-edit-exit
5118                              'gnus-article-mime-edit-exit
5119                              gnus-article-edit-mode-map)
5120   (erase-buffer)
5121   (insert-buffer gnus-original-article-buffer)
5122   (let ((ofn (symbol-function 'mime-edit-decode-single-part-in-buffer)))
5123     (fset 'mime-edit-decode-single-part-in-buffer
5124           (lambda (&rest args)
5125             (if (let ((content-type (car args)))
5126                   (and (eq 'message (mime-content-type-primary-type
5127                                      content-type))
5128                        (eq 'rfc822 (mime-content-type-subtype content-type))))
5129                 (setcar (cdr args) 'not-decode-text))
5130             (apply ofn args)))
5131     (unwind-protect
5132         (mime-edit-again)
5133       (fset 'mime-edit-decode-single-part-in-buffer ofn)))
5134   (when (featurep 'font-lock)
5135     (set (make-local-variable 'font-lock-defaults)
5136          '(message-font-lock-keywords t))
5137     (font-lock-set-defaults)
5138     (turn-on-font-lock))
5139   (set-buffer-modified-p nil)
5140   (delete-other-windows)
5141   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
5142   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
5143
5144 (defun gnus-article-mime-edit-done (&optional arg)
5145   "Update the article MIME edits and exit."
5146   (interactive "P")
5147   (when (featurep 'font-lock)
5148     (setq font-lock-defaults nil)
5149     (font-lock-mode -1))
5150   (gnus-article-edit-done arg))
5151
5152 (defun gnus-article-mime-edit-exit ()
5153   "Exit the article MIME editing without updating."
5154   (interactive)
5155   (when (or (not (buffer-modified-p))
5156             (yes-or-no-p "Article modified; kill anyway? "))
5157     (when (featurep 'font-lock)
5158       (setq font-lock-defaults nil)
5159       (font-lock-mode -1))
5160     (when mime-edit-mode-flag
5161       (let (mime-edit-insert-user-agent-field)
5162         (mime-edit-exit))
5163       (message ""))
5164     (goto-char (point-min))
5165     (let (case-fold-search)
5166       (when (re-search-forward
5167              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
5168         (replace-match "")))
5169     (let ((winconf gnus-prev-winconf))
5170       (insert (prog1
5171                   (buffer-substring-no-properties (point-min) (point-max))
5172                 (set-buffer (get-buffer-create gnus-original-article-buffer))
5173                 (erase-buffer)))
5174       (setq gnus-current-headers (gnus-article-make-full-mail-header))
5175       (set-buffer gnus-article-buffer)
5176       (gnus-article-prepare-display)
5177       (set-window-configuration winconf))))
5178
5179 ;;;
5180 ;;; Article highlights
5181 ;;;
5182
5183 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
5184
5185 ;;; Internal Variables:
5186
5187 (defcustom gnus-button-url-regexp "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\)\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?\\([-a-zA-Z0-9_=!?#$@~`%&*+|\\/:;.,]\\|\\w\\)+\\([-a-zA-Z0-9_=#$@~`%&*+|\\/]\\|\\w\\)\\)"
5188   "Regular expression that matches URLs."
5189   :group 'gnus-article-buttons
5190   :type 'regexp)
5191
5192 (defcustom gnus-button-alist
5193   `(("<\\(url:[>\n\t ]*?\\)?\\(nntp\\|news\\):[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
5194      0 t gnus-button-handle-news 3)
5195     ("\\b\\(nntp\\|news\\):\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t
5196      gnus-button-handle-news 2)
5197     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
5198      1 t
5199      gnus-button-fetch-group 4)
5200     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
5201     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
5202      t gnus-button-message-id 3)
5203     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
5204     ("mailto:\\([-a-zA-Z.@_+0-9%=?]+\\)" 0 t gnus-url-mailto 1)
5205     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
5206     ;; This is how URLs _should_ be embedded in text...
5207     ("<URL: *\\([^<>]*\\)>" 0 t gnus-button-embedded-url 1)
5208     ;; Raw URLs.
5209     (,gnus-button-url-regexp 0 t browse-url 0))
5210   "*Alist of regexps matching buttons in article bodies.
5211
5212 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
5213 REGEXP: is the string matching text around the button,
5214 BUTTON: is the number of the regexp grouping actually matching the button,
5215 FORM: is a lisp expression which must eval to true for the button to
5216 be added,
5217 CALLBACK: is the function to call when the user push this button, and each
5218 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
5219
5220 CALLBACK can also be a variable, in that case the value of that
5221 variable it the real callback function."
5222   :group 'gnus-article-buttons
5223   :type '(repeat (list regexp
5224                        (integer :tag "Button")
5225                        (sexp :tag "Form")
5226                        (function :tag "Callback")
5227                        (repeat :tag "Par"
5228                                :inline t
5229                                (integer :tag "Regexp group")))))
5230
5231 (defcustom gnus-header-button-alist
5232   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
5233      0 t gnus-button-message-id 0)
5234     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
5235     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
5236      0 t gnus-button-mailto 0)
5237     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
5238     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
5239     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
5240     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
5241      gnus-button-message-id 3))
5242   "*Alist of headers and regexps to match buttons in article heads.
5243
5244 This alist is very similar to `gnus-button-alist', except that each
5245 alist has an additional HEADER element first in each entry:
5246
5247 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
5248
5249 HEADER is a regexp to match a header.  For a fuller explanation, see
5250 `gnus-button-alist'."
5251   :group 'gnus-article-buttons
5252   :group 'gnus-article-headers
5253   :type '(repeat (list (regexp :tag "Header")
5254                        regexp
5255                        (integer :tag "Button")
5256                        (sexp :tag "Form")
5257                        (function :tag "Callback")
5258                        (repeat :tag "Par"
5259                                :inline t
5260                                (integer :tag "Regexp group")))))
5261
5262 (defvar gnus-button-regexp nil)
5263 (defvar gnus-button-marker-list nil)
5264 ;; Regexp matching any of the regexps from `gnus-button-alist'.
5265
5266 (defvar gnus-button-last nil)
5267 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
5268
5269 ;;; Commands:
5270
5271 (defun gnus-article-push-button (event)
5272   "Check text under the mouse pointer for a callback function.
5273 If the text under the mouse pointer has a `gnus-callback' property,
5274 call it with the value of the `gnus-data' text property."
5275   (interactive "e")
5276   (set-buffer (window-buffer (posn-window (event-start event))))
5277   (let* ((pos (posn-point (event-start event)))
5278          (data (get-text-property pos 'gnus-data))
5279          (fun (get-text-property pos 'gnus-callback)))
5280     (goto-char pos)
5281     (when fun
5282       (funcall fun data))))
5283
5284 (defun gnus-article-press-button ()
5285   "Check text at point for a callback function.
5286 If the text at point has a `gnus-callback' property,
5287 call it with the value of the `gnus-data' text property."
5288   (interactive)
5289   (let* ((data (get-text-property (point) 'gnus-data))
5290          (fun (get-text-property (point) 'gnus-callback)))
5291     (when fun
5292       (funcall fun data))))
5293
5294 (defun gnus-article-prev-button (n)
5295   "Move point to N buttons backward.
5296 If N is negative, move forward instead."
5297   (interactive "p")
5298   (gnus-article-next-button (- n)))
5299
5300 (defun gnus-article-next-button (n)
5301   "Move point to N buttons forward.
5302 If N is negative, move backward instead."
5303   (interactive "p")
5304   (let ((function (if (< n 0) 'previous-single-property-change
5305                     'next-single-property-change))
5306         (inhibit-point-motion-hooks t)
5307         (backward (< n 0))
5308         (limit (if (< n 0) (point-min) (point-max))))
5309     (setq n (abs n))
5310     (while (and (not (= limit (point)))
5311                 (> n 0))
5312       ;; Skip past the current button.
5313       (when (get-text-property (point) 'gnus-callback)
5314         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5315       ;; Go to the next (or previous) button.
5316       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
5317       ;; Put point at the start of the button.
5318       (when (and backward (not (get-text-property (point) 'gnus-callback)))
5319         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5320       ;; Skip past intangible buttons.
5321       (when (get-text-property (point) 'intangible)
5322         (incf n))
5323       (decf n))
5324     (unless (zerop n)
5325       (gnus-message 5 "No more buttons"))
5326     n))
5327
5328 (defun gnus-article-highlight (&optional force)
5329   "Highlight current article.
5330 This function calls `gnus-article-highlight-headers',
5331 `gnus-article-highlight-citation',
5332 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5333 do the highlighting.  See the documentation for those functions."
5334   (interactive (list 'force))
5335   (gnus-article-highlight-headers)
5336   (gnus-article-highlight-citation force)
5337   (gnus-article-highlight-signature)
5338   (gnus-article-add-buttons force)
5339   (gnus-article-add-buttons-to-head))
5340
5341 (defun gnus-article-highlight-some (&optional force)
5342   "Highlight current article.
5343 This function calls `gnus-article-highlight-headers',
5344 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5345 do the highlighting.  See the documentation for those functions."
5346   (interactive (list 'force))
5347   (gnus-article-highlight-headers)
5348   (gnus-article-highlight-signature)
5349   (gnus-article-add-buttons))
5350
5351 (defun gnus-article-highlight-headers ()
5352   "Highlight article headers as specified by `gnus-header-face-alist'."
5353   (interactive)
5354   (save-excursion
5355     (set-buffer gnus-article-buffer)
5356     (save-restriction
5357       (let ((alist gnus-header-face-alist)
5358             (buffer-read-only nil)
5359             (case-fold-search t)
5360             (inhibit-point-motion-hooks t)
5361             entry regexp header-face field-face from hpoints fpoints)
5362         (article-narrow-to-head)
5363         (while (setq entry (pop alist))
5364           (goto-char (point-min))
5365           (setq regexp (concat "^\\("
5366                                (if (string-equal "" (nth 0 entry))
5367                                    "[^\t ]"
5368                                  (nth 0 entry))
5369                                "\\)")
5370                 header-face (nth 1 entry)
5371                 field-face (nth 2 entry))
5372           (while (and (re-search-forward regexp nil t)
5373                       (not (eobp)))
5374             (beginning-of-line)
5375             (setq from (point))
5376             (unless (search-forward ":" nil t)
5377               (forward-char 1))
5378             (when (and header-face
5379                        (not (memq (point) hpoints)))
5380               (push (point) hpoints)
5381               (gnus-put-text-property from (point) 'face header-face))
5382             (when (and field-face
5383                        (not (memq (setq from (point)) fpoints)))
5384               (push from fpoints)
5385               (if (re-search-forward "^[^ \t]" nil t)
5386                   (forward-char -2)
5387                 (goto-char (point-max)))
5388               (gnus-put-text-property from (point) 'face field-face))))))))
5389
5390 (defun gnus-article-highlight-signature ()
5391   "Highlight the signature in an article.
5392 It does this by highlighting everything after
5393 `gnus-signature-separator' using `gnus-signature-face'."
5394   (interactive)
5395   (when gnus-signature-face
5396     (save-excursion
5397       (set-buffer gnus-article-buffer)
5398       (let ((buffer-read-only nil)
5399             (inhibit-point-motion-hooks t))
5400         (save-restriction
5401           (when (gnus-article-narrow-to-signature)
5402             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
5403                               'face gnus-signature-face)))))))
5404
5405 (defun gnus-article-buttonize-signature ()
5406   "Add button to the signature."
5407   (interactive)
5408   (save-excursion
5409     (set-buffer gnus-article-buffer)
5410     (let ((buffer-read-only nil)
5411           (inhibit-point-motion-hooks t))
5412       (when (gnus-article-search-signature)
5413         (gnus-article-add-button (match-beginning 0) (match-end 0)
5414                                  'gnus-signature-toggle
5415                                  (set-marker (make-marker)
5416                                              (1+ (match-end 0))))))))
5417
5418 (defun gnus-button-in-region-p (b e prop)
5419   "Say whether PROP exists in the region."
5420   (text-property-not-all b e prop nil))
5421
5422 (defun gnus-article-add-buttons (&optional force)
5423   "Find external references in the article and make buttons of them.
5424 \"External references\" are things like Message-IDs and URLs, as
5425 specified by `gnus-button-alist'."
5426   (interactive (list 'force))
5427   (save-excursion
5428     (set-buffer gnus-article-buffer)
5429     (let ((buffer-read-only nil)
5430           (inhibit-point-motion-hooks t)
5431           (case-fold-search t)
5432           (alist gnus-button-alist)
5433           beg entry regexp)
5434       ;; Remove all old markers.
5435       (let (marker entry new-list)
5436         (while (setq marker (pop gnus-button-marker-list))
5437           (if (or (< marker (point-min)) (>= marker (point-max)))
5438               (push marker new-list)
5439             (goto-char marker)
5440             (when (setq entry (gnus-button-entry))
5441               (put-text-property (match-beginning (nth 1 entry))
5442                                  (match-end (nth 1 entry))
5443                                  'gnus-callback nil))
5444             (set-marker marker nil)))
5445         (setq gnus-button-marker-list new-list))
5446       ;; We skip the headers.
5447       (article-goto-body)
5448       (setq beg (point))
5449       (while (setq entry (pop alist))
5450         (setq regexp (car entry))
5451         (goto-char beg)
5452         (while (re-search-forward regexp nil t)
5453           (let* ((start (and entry (match-beginning (nth 1 entry))))
5454                  (end (and entry (match-end (nth 1 entry))))
5455                  (from (match-beginning 0)))
5456             (when (and (or (eq t (nth 2 entry))
5457                            (eval (nth 2 entry)))
5458                        (not (gnus-button-in-region-p
5459                              start end 'gnus-callback)))
5460               ;; That optional form returned non-nil, so we add the
5461               ;; button.
5462               (gnus-article-add-button
5463                start end 'gnus-button-push
5464                (car (push (set-marker (make-marker) from)
5465                           gnus-button-marker-list))))))))))
5466
5467 ;; Add buttons to the head of an article.
5468 (defun gnus-article-add-buttons-to-head ()
5469   "Add buttons to the head of the article."
5470   (interactive)
5471   (save-excursion
5472     (set-buffer gnus-article-buffer)
5473     (save-restriction
5474       (let ((buffer-read-only nil)
5475             (inhibit-point-motion-hooks t)
5476             (case-fold-search t)
5477             (alist gnus-header-button-alist)
5478             entry beg end)
5479         (article-narrow-to-head)
5480         (while alist
5481           ;; Each alist entry.
5482           (setq entry (car alist)
5483                 alist (cdr alist))
5484           (goto-char (point-min))
5485           (while (re-search-forward (car entry) nil t)
5486             ;; Each header matching the entry.
5487             (setq beg (match-beginning 0))
5488             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
5489                                (match-beginning 0))
5490                           (point-max)))
5491             (goto-char beg)
5492             (while (re-search-forward (nth 1 entry) end t)
5493               ;; Each match within a header.
5494               (let* ((entry (cdr entry))
5495                      (start (match-beginning (nth 1 entry)))
5496                      (end (match-end (nth 1 entry)))
5497                      (form (nth 2 entry)))
5498                 (goto-char (match-end 0))
5499                 (when (eval form)
5500                   (gnus-article-add-button
5501                    start end (nth 3 entry)
5502                    (buffer-substring (match-beginning (nth 4 entry))
5503                                      (match-end (nth 4 entry)))))))
5504             (goto-char end)))))))
5505
5506 ;;; External functions:
5507
5508 (defun gnus-article-add-button (from to fun &optional data)
5509   "Create a button between FROM and TO with callback FUN and data DATA."
5510   (when gnus-article-button-face
5511     (gnus-overlay-put (gnus-make-overlay from to)
5512                       'face gnus-article-button-face))
5513   (gnus-add-text-properties
5514    from to
5515    (nconc (and gnus-article-mouse-face
5516                (list gnus-mouse-face-prop gnus-article-mouse-face))
5517           (list 'gnus-callback fun)
5518           (and data (list 'gnus-data data))))
5519   (widget-convert-button 'link from to :action 'gnus-widget-press-button
5520                          ;; Quote `:button-keymap' for Mule 2.3
5521                          ;; but it won't work.
5522                          ':button-keymap gnus-widget-button-keymap))
5523
5524 ;;; Internal functions:
5525
5526 (defun gnus-article-set-globals ()
5527   (save-excursion
5528     (set-buffer gnus-summary-buffer)
5529     (gnus-set-global-variables)))
5530
5531 (defun gnus-signature-toggle (end)
5532   (save-excursion
5533     (set-buffer gnus-article-buffer)
5534     (let ((buffer-read-only nil)
5535           (inhibit-point-motion-hooks t)
5536           (limit (next-single-property-change end 'mime-view-entity
5537                                               nil (point-max))))
5538       (if (text-property-any end limit 'article-type 'signature)
5539           (gnus-remove-text-properties-when
5540            'article-type 'signature end limit
5541            (cons 'article-type (cons 'signature
5542                                      gnus-hidden-properties)))
5543         (gnus-add-text-properties-when
5544          'article-type nil end limit
5545          (cons 'article-type (cons 'signature
5546                                    gnus-hidden-properties)))))))
5547
5548 (defun gnus-button-entry ()
5549   ;; Return the first entry in `gnus-button-alist' matching this place.
5550   (let ((alist gnus-button-alist)
5551         (entry nil))
5552     (while alist
5553       (setq entry (pop alist))
5554       (if (looking-at (car entry))
5555           (setq alist nil)
5556         (setq entry nil)))
5557     entry))
5558
5559 (defun gnus-button-push (marker)
5560   ;; Push button starting at MARKER.
5561   (save-excursion
5562     (goto-char marker)
5563     (let* ((entry (gnus-button-entry))
5564            (inhibit-point-motion-hooks t)
5565            (fun (nth 3 entry))
5566            (args (mapcar (lambda (group)
5567                            (let ((string (match-string group)))
5568                              (gnus-set-text-properties
5569                               0 (length string) nil string)
5570                              string))
5571                          (nthcdr 4 entry))))
5572       (cond
5573        ((fboundp fun)
5574         (apply fun args))
5575        ((and (boundp fun)
5576              (fboundp (symbol-value fun)))
5577         (apply (symbol-value fun) args))
5578        (t
5579         (gnus-message 1 "You must define `%S' to use this button"
5580                       (cons fun args)))))))
5581
5582 (defun gnus-parse-news-url (url)
5583   (let (scheme server group message-id articles)
5584     (with-temp-buffer
5585       (insert url)
5586       (goto-char (point-min))
5587       (when (looking-at "\\([A-Za-z]+\\):")
5588         (setq scheme (match-string 1))
5589         (goto-char (match-end 0)))
5590       (when (looking-at "//\\([^/]+\\)/")
5591         (setq server (match-string 1))
5592         (goto-char (match-end 0)))
5593         
5594       (cond
5595        ((looking-at "\\(.*@.*\\)")
5596         (setq message-id (match-string 1)))
5597        ((looking-at "\\([^/]+\\)/\\([-0-9]+\\)")
5598         (setq group (match-string 1)
5599               articles (split-string (match-string 2) "-")))
5600        ((looking-at "\\([^/]+\\)/?")
5601         (setq group (match-string 1)))
5602        (t
5603         (error "Unknown news URL syntax"))))
5604     (list scheme server group message-id articles)))
5605
5606 (defun gnus-button-handle-news (url)
5607   "Fetch a news URL."
5608   (destructuring-bind (scheme server group message-id articles)
5609       (gnus-parse-news-url url)
5610     (cond
5611      (message-id
5612       (save-excursion
5613         (set-buffer gnus-summary-buffer)
5614         (if server
5615             (let ((gnus-refer-article-method (list (list 'nntp server))))
5616               (gnus-summary-refer-article message-id))
5617           (gnus-summary-refer-article message-id))))
5618      (group
5619       (gnus-button-fetch-group url)))))
5620
5621 (defun gnus-button-message-id (message-id)
5622   "Fetch MESSAGE-ID."
5623   (save-excursion
5624     (set-buffer gnus-summary-buffer)
5625     (gnus-summary-refer-article message-id)))
5626
5627 (defun gnus-button-fetch-group (address)
5628   "Fetch GROUP specified by ADDRESS."
5629   (if (not (string-match "[:/]" address))
5630       ;; This is just a simple group url.
5631       (gnus-group-read-ephemeral-group address gnus-select-method)
5632     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
5633                            address))
5634         (error "Can't parse %s" address)
5635       (gnus-group-read-ephemeral-group
5636        (match-string 4 address)
5637        `(nntp ,(match-string 1 address)
5638               (nntp-address ,(match-string 1 address))
5639               (nntp-port-number ,(if (match-end 3)
5640                                      (match-string 3 address)
5641                                    "nntp")))))))
5642
5643 (defun gnus-url-parse-query-string (query &optional downcase)
5644   (let (retval pairs cur key val)
5645     (setq pairs (split-string query "&"))
5646     (while pairs
5647       (setq cur (car pairs)
5648             pairs (cdr pairs))
5649       (if (not (string-match "=" cur))
5650           nil                           ; Grace
5651         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
5652               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
5653         (if downcase
5654             (setq key (downcase key)))
5655         (setq cur (assoc key retval))
5656         (if cur
5657             (setcdr cur (cons val (cdr cur)))
5658           (setq retval (cons (list key val) retval)))))
5659     retval))
5660
5661 (defun gnus-url-unhex (x)
5662   (if (> x ?9)
5663       (if (>= x ?a)
5664           (+ 10 (- x ?a))
5665         (+ 10 (- x ?A)))
5666     (- x ?0)))
5667
5668 (defun gnus-url-unhex-string (str &optional allow-newlines)
5669   "Remove %XXX embedded spaces, etc in a url.
5670 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
5671 decoding of carriage returns and line feeds in the string, which is normally
5672 forbidden in URL encoding."
5673   (setq str (or str ""))
5674   (let ((tmp "")
5675         (case-fold-search t))
5676     (while (string-match "%[0-9a-f][0-9a-f]" str)
5677       (let* ((start (match-beginning 0))
5678              (ch1 (gnus-url-unhex (elt str (+ start 1))))
5679              (code (+ (* 16 ch1)
5680                       (gnus-url-unhex (elt str (+ start 2))))))
5681         (setq tmp (concat
5682                    tmp (substring str 0 start)
5683                    (cond
5684                     (allow-newlines
5685                      (char-to-string code))
5686                     ((or (= code ?\n) (= code ?\r))
5687                      " ")
5688                     (t (char-to-string code))))
5689               str (substring str (match-end 0)))))
5690     (setq tmp (concat tmp str))
5691     tmp))
5692
5693 (defun gnus-url-mailto (url)
5694   ;; Send mail to someone
5695   (when (string-match "mailto:/*\\(.*\\)" url)
5696     (setq url (substring url (match-beginning 1) nil)))
5697   (let (to args subject func)
5698     (if (string-match (regexp-quote "?") url)
5699         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
5700               args (gnus-url-parse-query-string
5701                     (substring url (match-end 0) nil) t))
5702       (setq to (gnus-url-unhex-string url)))
5703     (setq args (cons (list "to" to) args)
5704           subject (cdr-safe (assoc "subject" args)))
5705     (gnus-msg-mail)
5706     (while args
5707       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
5708       (if (fboundp func)
5709           (funcall func)
5710         (message-position-on-field (caar args)))
5711       (insert (mapconcat 'identity (cdar args) ", "))
5712       (setq args (cdr args)))
5713     (if subject
5714         (message-goto-body)
5715       (message-goto-subject))))
5716
5717 (defun gnus-button-embedded-url (address)
5718   "Activate ADDRESS with `browse-url'."
5719   (browse-url (gnus-strip-whitespace address)))
5720
5721 (eval-when-compile
5722   ;; Silence the byte-compiler.
5723   (autoload 'smiley-toggle-buffer "gnus-bitmap"))
5724 (defun gnus-article-smiley-display ()
5725   "Display \"smileys\" as small graphical icons."
5726   (smiley-toggle-buffer 1 (current-buffer) (point-min) (point-max)))
5727
5728 ;;; Next/prev buttons in the article buffer.
5729
5730 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
5731 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
5732
5733 (defvar gnus-prev-page-map nil)
5734 (unless gnus-prev-page-map
5735   (setq gnus-prev-page-map (make-sparse-keymap))
5736   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
5737   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
5738
5739 (static-if (featurep 'xemacs)
5740     (defun gnus-insert-prev-page-button ()
5741       (let ((buffer-read-only nil))
5742         (gnus-eval-format
5743          gnus-prev-page-line-format nil
5744          `(gnus-prev t local-map ,gnus-prev-page-map
5745                      gnus-callback gnus-article-button-prev-page
5746                      article-type annotation))))
5747   (defun gnus-insert-prev-page-button ()
5748     (let ((buffer-read-only nil)
5749           (situation (get-text-property (point-min) 'mime-view-situation)))
5750       (set-keymap-parent gnus-prev-page-map (current-local-map))
5751       (gnus-eval-format
5752        gnus-prev-page-line-format nil
5753        `(gnus-prev t local-map ,gnus-prev-page-map
5754                    gnus-callback gnus-article-button-prev-page
5755                    article-type annotation
5756                    mime-view-situation ,situation))))
5757   )
5758
5759 (defvar gnus-next-page-map nil)
5760 (unless gnus-next-page-map
5761   (setq gnus-next-page-map (make-sparse-keymap))
5762   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
5763   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
5764
5765 (defun gnus-button-next-page ()
5766   "Go to the next page."
5767   (interactive)
5768   (let ((win (selected-window)))
5769     (select-window (get-buffer-window gnus-article-buffer t))
5770     (gnus-article-next-page)
5771     (select-window win)))
5772
5773 (defun gnus-button-prev-page ()
5774   "Go to the prev page."
5775   (interactive)
5776   (let ((win (selected-window)))
5777     (select-window (get-buffer-window gnus-article-buffer t))
5778     (gnus-article-prev-page)
5779     (select-window win)))
5780
5781 (static-if (featurep 'xemacs)
5782     (defun gnus-insert-next-page-button ()
5783       (let ((buffer-read-only nil))
5784         (gnus-eval-format gnus-next-page-line-format nil
5785                           `(gnus-next
5786                             t local-map ,gnus-next-page-map
5787                             gnus-callback gnus-article-button-next-page
5788                             article-type annotation))))
5789   (defun gnus-insert-next-page-button ()
5790     (let ((buffer-read-only nil)
5791           (situation (get-text-property (point-min) 'mime-view-situation)))
5792       (set-keymap-parent gnus-next-page-map (current-local-map))
5793       (gnus-eval-format gnus-next-page-line-format nil
5794                         `(gnus-next
5795                           t local-map ,gnus-next-page-map
5796                           gnus-callback gnus-article-button-next-page
5797                           article-type annotation
5798                           mime-view-situation ,situation))))
5799   )
5800
5801 (defun gnus-article-button-next-page (arg)
5802   "Go to the next page."
5803   (interactive "P")
5804   (let ((win (selected-window)))
5805     (select-window (get-buffer-window gnus-article-buffer t))
5806     (gnus-article-next-page)
5807     (select-window win)))
5808
5809 (defun gnus-article-button-prev-page (arg)
5810   "Go to the prev page."
5811   (interactive "P")
5812   (let ((win (selected-window)))
5813     (select-window (get-buffer-window gnus-article-buffer t))
5814     (gnus-article-prev-page)
5815     (select-window win)))
5816
5817 (defvar gnus-decode-header-methods
5818   '(mail-decode-encoded-word-region)
5819   "List of methods used to decode headers.
5820
5821 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
5822 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
5823 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
5824 whose names match REGEXP.
5825
5826 For example:
5827 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
5828  mail-decode-encoded-word-region
5829  (\"chinese\" . rfc1843-decode-region))
5830 ")
5831
5832 (defvar gnus-decode-header-methods-cache nil)
5833
5834 (defun gnus-multi-decode-header (start end)
5835   "Apply the functions from `gnus-encoded-word-methods' that match."
5836   (unless (and gnus-decode-header-methods-cache
5837                (eq gnus-newsgroup-name
5838                    (car gnus-decode-header-methods-cache)))
5839     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
5840     (mapcar (lambda (x)
5841               (if (symbolp x)
5842                   (nconc gnus-decode-header-methods-cache (list x))
5843                 (if (and gnus-newsgroup-name
5844                          (string-match (car x) gnus-newsgroup-name))
5845                     (nconc gnus-decode-header-methods-cache
5846                            (list (cdr x))))))
5847           gnus-decode-header-methods))
5848   (let ((xlist gnus-decode-header-methods-cache))
5849     (pop xlist)
5850     (save-restriction
5851       (narrow-to-region start end)
5852       (while xlist
5853         (funcall (pop xlist) (point-min) (point-max))))))
5854
5855 ;;;
5856 ;;; Treatment top-level handling.
5857 ;;;
5858
5859 (defun gnus-treat-article (condition &optional part-number total-parts type)
5860   (let ((length (- (point-max) (point-min)))
5861         (alist gnus-treatment-function-alist)
5862         (article-goto-body-goes-to-point-min-p t)
5863         (treated-type
5864          (or (not type)
5865              (catch 'found
5866                (let ((list gnus-article-treat-types))
5867                  (while list
5868                    (when (string-match (pop list) type)
5869                      (throw 'found t)))))))
5870         (highlightp (gnus-visual-p 'article-highlight 'highlight))
5871         (entity (static-unless (featurep 'xemacs)
5872                   (when (eq 'head condition)
5873                     (get-text-property (point-min) 'mime-view-entity))))
5874         val elem buttonized)
5875     (gnus-run-hooks 'gnus-part-display-hook)
5876     (unless gnus-inhibit-treatment
5877       (while (setq elem (pop alist))
5878         (setq val
5879               (save-excursion
5880                 (if (gnus-buffer-live-p gnus-summary-buffer)
5881                     (set-buffer gnus-summary-buffer))
5882                 (symbol-value (car elem))))
5883         (when (and (or (consp val)
5884                        treated-type)
5885                    (gnus-treat-predicate val)
5886                    (or (not (get (car elem) 'highlight))
5887                        highlightp))
5888           (when (and (not buttonized)
5889                      (memq (car elem)
5890                            '(gnus-treat-hide-signature
5891                              gnus-treat-highlight-signature)))
5892             (gnus-article-buttonize-signature)
5893             (setq buttonized t))
5894           (save-restriction
5895             (funcall (cadr elem)))))
5896       ;; FSF Emacsen does not inherit the existing text properties
5897       ;; in the new text, so we should do it for `mime-view-entity'.
5898       (static-unless (featurep 'xemacs)
5899         (when entity
5900           (put-text-property (point-min) (point-max)
5901                              'mime-view-entity entity))))))
5902
5903 ;; Dynamic variables.
5904 (eval-when-compile
5905   (defvar part-number)
5906   (defvar total-parts)
5907   (defvar type)
5908   (defvar condition)
5909   (defvar length))
5910
5911 (defun gnus-treat-predicate (val)
5912   (cond
5913    ((null val)
5914     nil)
5915    ((and (listp val)
5916          (stringp (car val)))
5917     (apply 'gnus-or (mapcar `(lambda (s)
5918                                (string-match s ,(or gnus-newsgroup-name "")))
5919                             val)))
5920    ((listp val)
5921     (let ((pred (pop val)))
5922       (cond
5923        ((eq pred 'or)
5924         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
5925        ((eq pred 'and)
5926         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
5927        ((eq pred 'not)
5928         (not (gnus-treat-predicate (car val))))
5929        ((eq pred 'typep)
5930         (equal (car val) type))
5931        (t
5932         (error "%S is not a valid predicate" pred)))))
5933    ((eq val 'mime)
5934     gnus-show-mime)
5935    (condition
5936     (eq condition val))
5937    ((eq val t)
5938     t)
5939    ((eq val 'head)
5940     nil)
5941    ((eq val 'last)
5942     (eq part-number total-parts))
5943    ((numberp val)
5944     (< length val))
5945    (t
5946     (error "%S is not a valid value" val))))
5947
5948 (defun gnus-article-encrypt-body (protocol &optional n)
5949   "Encrypt the article body."
5950   (interactive
5951    (list
5952     (or gnus-article-encrypt-protocol
5953         (completing-read "Encrypt protocol: "
5954                          gnus-article-encrypt-protocol-alist
5955                          nil t))
5956     current-prefix-arg))
5957   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
5958     (unless func
5959       (error (format "Can't find the encrypt protocol %s" protocol)))
5960     (if (equal gnus-newsgroup-name "nndraft:drafts")
5961         (error "Can't encrypt the article in group nndraft:drafts."))
5962     (if (equal gnus-newsgroup-name "nndraft:queue")
5963         (error "Don't encrypt the article in group nndraft:queue."))
5964     (gnus-summary-iterate n
5965       (save-excursion
5966         (set-buffer gnus-summary-buffer)
5967         (let ((mail-parse-charset gnus-newsgroup-charset)
5968               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5969               (summary-buffer gnus-summary-buffer)
5970               references point)
5971           (gnus-set-global-variables)
5972           (when (gnus-group-read-only-p)
5973             (error "The current newsgroup does not support article encrypt"))
5974           (gnus-summary-show-article t)
5975           (setq references
5976               (or (mail-header-references gnus-current-headers) ""))
5977           (set-buffer gnus-article-buffer)
5978           (let* ((buffer-read-only nil)
5979                  (headers
5980                   (mapcar (lambda (field)
5981                             (and (save-restriction
5982                                    (message-narrow-to-head)
5983                                    (goto-char (point-min))
5984                                    (search-forward field nil t))
5985                                  (prog2
5986                                      (message-narrow-to-field)
5987                                      (buffer-substring (point-min) (point-max))
5988                                    (delete-region (point-min) (point-max))
5989                                    (widen))))
5990                           '("Content-Type:" "Content-Transfer-Encoding:"
5991                             "Content-Disposition:"))))
5992             (message-narrow-to-head)
5993             (message-remove-header "MIME-Version")
5994             (goto-char (point-max))
5995             (setq point (point))
5996             (insert (apply 'concat headers))
5997             (widen)
5998             (narrow-to-region point (point-max))
5999             (let ((message-options message-options))
6000               (message-options-set 'message-sender user-mail-address)
6001               (message-options-set 'message-recipients user-mail-address)
6002               (message-options-set 'message-sign-encrypt 'not)
6003               (funcall func))
6004             (goto-char (point-min))
6005             (insert "MIME-Version: 1.0\n")
6006             (widen)
6007             (gnus-summary-edit-article-done
6008              references nil summary-buffer t))
6009           (when gnus-keep-backlog
6010             (gnus-backlog-remove-article
6011              (car gnus-article-current) (cdr gnus-article-current)))
6012           (save-excursion
6013             (when (get-buffer gnus-original-article-buffer)
6014               (set-buffer gnus-original-article-buffer)
6015               (setq gnus-original-article nil)))
6016           (when gnus-use-cache
6017             (gnus-cache-update-article
6018              (car gnus-article-current) (cdr gnus-article-current))))))))
6019
6020 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
6021   "The following specs can be used:
6022 %t  The security MIME type
6023 %i  Additional info
6024 %d  Details
6025 %D  Details if button is pressed")
6026
6027 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
6028   "The following specs can be used:
6029 %t  The security MIME type
6030 %i  Additional info
6031 %d  Details
6032 %D  Details if button is pressed")
6033
6034 (defvar gnus-mime-security-button-line-format-alist
6035   '((?t gnus-tmp-type ?s)
6036     (?i gnus-tmp-info ?s)
6037     (?d gnus-tmp-details ?s)
6038     (?D gnus-tmp-pressed-details ?s)))
6039
6040 (defvar gnus-mime-security-button-map
6041   (let ((map (make-sparse-keymap)))
6042     (unless (>= (string-to-number emacs-version) 21)
6043       (set-keymap-parent map gnus-article-mode-map))
6044     (define-key map gnus-mouse-2 'gnus-article-push-button)
6045     (define-key map "\r" 'gnus-article-press-button)
6046     map))
6047
6048 (defvar gnus-mime-security-details-buffer nil)
6049
6050 (defvar gnus-mime-security-button-pressed nil)
6051
6052 (defvar gnus-mime-security-show-details-inline t
6053   "If non-nil, show details in the article buffer.")
6054
6055 (defun gnus-mime-security-verify-or-decrypt (handle)
6056   (mm-remove-parts (cdr handle))
6057   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
6058         buffer-read-only)
6059     (when region
6060       (delete-region (car region) (cdr region))
6061       (set-marker (car region) nil)
6062       (set-marker (cdr region) nil)))
6063   (with-current-buffer (mm-handle-multipart-original-buffer handle)
6064     (let* ((mm-verify-option 'known)
6065            (mm-decrypt-option 'known)
6066            (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
6067       (unless (eq nparts (cdr handle))
6068         (mm-destroy-parts (cdr handle))
6069         (setcdr handle nparts))))
6070   (let ((point (point))
6071         buffer-read-only)
6072     (gnus-mime-display-security handle)
6073     (goto-char point)))
6074
6075 (defun gnus-mime-security-show-details (handle)
6076   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
6077     (if details
6078         (if gnus-mime-security-show-details-inline
6079             (let ((gnus-mime-security-button-pressed t)
6080                   (gnus-mime-security-button-line-format
6081                    (get-text-property (point) 'gnus-line-format))
6082                 buffer-read-only)
6083               (forward-char -1)
6084               (while (eq (get-text-property (point) 'gnus-line-format)
6085                          gnus-mime-security-button-line-format)
6086                 (forward-char -1))
6087               (forward-char)
6088               (delete-region (point)
6089                              (or (text-property-not-all
6090                                   (point) (point-max)
6091                                 'gnus-line-format
6092                                 gnus-mime-security-button-line-format)
6093                                  (point-max)))
6094               (gnus-insert-mime-security-button handle))
6095           (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
6096               (with-current-buffer gnus-mime-security-details-buffer
6097                 (erase-buffer)
6098                 t)
6099             (setq gnus-mime-security-details-buffer
6100                   (gnus-get-buffer-create "*MIME Security Details*")))
6101           (with-current-buffer gnus-mime-security-details-buffer
6102             (insert details)
6103             (goto-char (point-min)))
6104           (pop-to-buffer gnus-mime-security-details-buffer))
6105       (gnus-message 5 "No details."))))
6106
6107 (defun gnus-mime-security-press-button (handle)
6108   (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
6109       (gnus-mime-security-show-details handle)
6110     (gnus-mime-security-verify-or-decrypt handle)))
6111
6112 (defun gnus-insert-mime-security-button (handle &optional displayed)
6113   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
6114          (gnus-tmp-type
6115           (concat
6116            (or (nth 2 (assoc protocol mm-verify-function-alist))
6117                (nth 2 (assoc protocol mm-decrypt-function-alist))
6118                "Unknown")
6119            (if (equal (car handle) "multipart/signed")
6120                " Signed" " Encrypted")
6121            " Part"))
6122          (gnus-tmp-info
6123           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
6124               "Undecided"))
6125          (gnus-tmp-details
6126           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
6127          gnus-tmp-pressed-details
6128          b e)
6129     (setq gnus-tmp-details
6130           (if gnus-tmp-details
6131               (concat "\n" gnus-tmp-details) ""))
6132     (setq gnus-tmp-pressed-details
6133           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
6134     (unless (bolp)
6135       (insert "\n"))
6136     (setq b (point))
6137     (gnus-eval-format
6138      gnus-mime-security-button-line-format
6139      gnus-mime-security-button-line-format-alist
6140      `(keymap ,gnus-mime-security-button-map
6141               ,@(if (>= (string-to-number emacs-version) 21)
6142                     nil ;; XEmacs doesn't care
6143                   (list 'local-map gnus-mime-security-button-map))
6144               gnus-callback gnus-mime-security-press-button
6145               gnus-line-format ,gnus-mime-security-button-line-format
6146               article-type annotation
6147               gnus-data ,handle))
6148     (setq e (point))
6149     (widget-convert-button
6150      'link b e
6151      :mime-handle handle
6152      :action 'gnus-widget-press-button
6153      :button-keymap gnus-mime-security-button-map
6154      :help-echo
6155      (lambda (widget/window &optional overlay pos)
6156        ;; Needed to properly clear the message due to a bug in
6157        ;; wid-edit (XEmacs only).
6158        (if (boundp 'help-echo-owns-message)
6159            (setq help-echo-owns-message t))
6160        (format
6161         "%S: show detail"
6162         (aref gnus-mouse-2 0))))))
6163
6164 (defun gnus-mime-display-security (handle)
6165   (save-restriction
6166     (narrow-to-region (point) (point))
6167     (gnus-insert-mime-security-button handle)
6168     (gnus-mime-display-mixed (cdr handle))
6169     (unless (bolp)
6170       (insert "\n"))
6171     (let ((gnus-mime-security-button-line-format
6172            gnus-mime-security-button-end-line-format))
6173       (gnus-insert-mime-security-button handle))
6174     (mm-set-handle-multipart-parameter
6175      handle 'gnus-region
6176      (cons (set-marker (make-marker) (point-min))
6177            (set-marker (make-marker) (point-max))))))
6178
6179
6180 ;;; @ for mime-view
6181 ;;;
6182
6183 (defun gnus-article-header-presentation-method (entity situation)
6184   (mime-insert-header entity)
6185   )
6186
6187 (set-alist 'mime-header-presentation-method-alist
6188            'gnus-original-article-mode
6189            #'gnus-article-header-presentation-method)
6190
6191 (defun gnus-mime-preview-quitting-method ()
6192   (mime-preview-kill-buffer)
6193   (delete-other-windows)
6194   (gnus-article-show-summary)
6195   (gnus-summary-select-article gnus-show-all-headers t))
6196
6197 (set-alist 'mime-preview-quitting-method-alist
6198            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
6199
6200 (set-alist 'mime-preview-following-method-alist
6201            'gnus-original-article-mode #'gnus-following-method)
6202
6203 (set-alist 'mime-preview-over-to-previous-method-alist
6204            'gnus-original-article-mode
6205            (lambda ()
6206              (if (> (point-min) 1)
6207                  (gnus-article-prev-page)
6208                (gnus-article-read-summary-keys
6209                 nil (gnus-character-to-event ?P)))))
6210
6211 (set-alist 'mime-preview-over-to-next-method-alist
6212            'gnus-original-article-mode'
6213            (lambda ()
6214              (if (< (point-max) (buffer-size))
6215                  (gnus-article-next-page)
6216                (gnus-article-read-summary-keys
6217                 nil (gnus-character-to-event ?N)))))
6218
6219
6220 ;;; @ end
6221 ;;;
6222
6223 (gnus-ems-redefine)
6224
6225 (provide 'gnus-art)
6226
6227 (run-hooks 'gnus-art-load-hook)
6228
6229 ;;; gnus-art.el ends here