Synch with Oort Gnus.
[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 ((buffer-read-only nil)
1307               (inhibit-read-only t)
1308               (case-fold-search t)
1309               (max (1+ (length gnus-sorted-header-list)))
1310               (ignored (when (not gnus-visible-headers)
1311                          (cond ((stringp gnus-ignored-headers)
1312                                 gnus-ignored-headers)
1313                                ((listp gnus-ignored-headers)
1314                                 (mapconcat 'identity gnus-ignored-headers
1315                                            "\\|")))))
1316               (visible
1317                (cond ((stringp gnus-visible-headers)
1318                       gnus-visible-headers)
1319                      ((and gnus-visible-headers
1320                            (listp gnus-visible-headers))
1321                       (mapconcat 'identity gnus-visible-headers "\\|"))))
1322               (inhibit-point-motion-hooks t)
1323               beg)
1324           ;; First we narrow to just the headers.
1325           (article-narrow-to-head)
1326           ;; Hide any "From " lines at the beginning of (mail) articles.
1327           (while (looking-at "From ")
1328             (forward-line 1))
1329           (unless (bobp)
1330             (if delete
1331                 (delete-region (point-min) (point))
1332               (gnus-article-hide-text (point-min) (point)
1333                                       (nconc (list 'article-type 'headers)
1334                                              gnus-hidden-properties))))
1335           ;; Then treat the rest of the header lines.
1336           ;; Then we use the two regular expressions
1337           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1338           ;; select which header lines is to remain visible in the
1339           ;; article buffer.
1340           (while (re-search-forward "^[^ \t]*:" nil t)
1341             (beginning-of-line)
1342             ;; Mark the rank of the header.
1343             (put-text-property
1344              (point) (1+ (point)) 'message-rank
1345              (if (or (and visible (looking-at visible))
1346                      (and ignored
1347                           (not (looking-at ignored))))
1348                  (gnus-article-header-rank)
1349                (+ 2 max)))
1350             (forward-line 1))
1351           (message-sort-headers-1)
1352           (when (setq beg (text-property-any
1353                            (point-min) (point-max) 'message-rank (+ 2 max)))
1354             ;; We delete or make invisible the unwanted headers.
1355             (push 'headers gnus-article-wash-types)
1356             (if delete
1357                 (progn
1358                   (add-text-properties
1359                    (point-min) (+ 5 (point-min))
1360                    '(article-type headers dummy-invisible t))
1361                   (delete-region beg (point-max)))
1362               (gnus-article-hide-text-type beg (point-max) 'headers))))))))
1363   )
1364
1365 (defun article-hide-boring-headers (&optional arg)
1366   "Toggle hiding of headers that aren't very interesting.
1367 If given a negative prefix, always show; if given a positive prefix,
1368 always hide."
1369   (interactive (gnus-article-hidden-arg))
1370   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1371              (not gnus-show-all-headers))
1372     (save-excursion
1373       (save-restriction
1374         (let ((buffer-read-only nil)
1375               (list gnus-boring-article-headers)
1376               (inhibit-point-motion-hooks t)
1377               elem)
1378           (article-narrow-to-head)
1379           (while list
1380             (setq elem (pop list))
1381             (goto-char (point-min))
1382             (cond
1383              ;; Hide empty headers.
1384              ((eq elem 'empty)
1385               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1386                 (forward-line -1)
1387                 (gnus-article-hide-text-type
1388                  (progn (beginning-of-line) (point))
1389                  (progn
1390                    (end-of-line)
1391                    (if (re-search-forward "^[^ \t]" nil t)
1392                        (match-beginning 0)
1393                      (point-max)))
1394                  'boring-headers)))
1395              ;; Hide boring Newsgroups header.
1396              ((eq elem 'newsgroups)
1397               (when (gnus-string-equal
1398                      (gnus-fetch-field "newsgroups")
1399                      (gnus-group-real-name
1400                       (if (boundp 'gnus-newsgroup-name)
1401                           gnus-newsgroup-name
1402                         "")))
1403                 (gnus-article-hide-header "newsgroups")))
1404              ((eq elem 'to-address)
1405               (let ((to (message-fetch-field "to"))
1406                     (to-address
1407                      (gnus-parameter-to-address
1408                       (if (boundp 'gnus-newsgroup-name)
1409                           gnus-newsgroup-name ""))))
1410                 (when (and to to-address
1411                            (ignore-errors
1412                              (gnus-string-equal
1413                               ;; only one address in To
1414                               (nth 1 (mail-extract-address-components to))
1415                               to-address)))
1416                   (gnus-article-hide-header "to"))))
1417              ((eq elem 'followup-to)
1418               (when (gnus-string-equal
1419                      (message-fetch-field "followup-to")
1420                      (message-fetch-field "newsgroups"))
1421                 (gnus-article-hide-header "followup-to")))
1422              ((eq elem 'reply-to)
1423               (let ((from (message-fetch-field "from"))
1424                     (reply-to (message-fetch-field "reply-to")))
1425                 (when (and
1426                        from reply-to
1427                        (ignore-errors
1428                          (gnus-string-equal
1429                           (nth 1 (mail-extract-address-components from))
1430                           (nth 1 (mail-extract-address-components reply-to)))))
1431                   (gnus-article-hide-header "reply-to"))))
1432              ((eq elem 'date)
1433               (let ((date (message-fetch-field "date")))
1434                 (when (and date
1435                            (< (days-between (current-time-string) date)
1436                               4))
1437                   (gnus-article-hide-header "date"))))
1438              ((eq elem 'long-to)
1439               (let ((to (message-fetch-field "to"))
1440                     (cc (message-fetch-field "cc")))
1441                 (when (> (length to) 1024)
1442                   (gnus-article-hide-header "to"))
1443                 (when (> (length cc) 1024)
1444                   (gnus-article-hide-header "cc"))))
1445              ((eq elem 'many-to)
1446               (let ((to-count 0)
1447                     (cc-count 0))
1448                 (goto-char (point-min))
1449                 (while (re-search-forward "^to:" nil t)
1450                   (setq to-count (1+ to-count)))
1451                 (when (> to-count 1)
1452                   (while (> to-count 0)
1453                     (goto-char (point-min))
1454                     (save-restriction
1455                       (re-search-forward "^to:" nil nil to-count)
1456                       (forward-line -1)
1457                       (narrow-to-region (point) (point-max))
1458                       (gnus-article-hide-header "to"))
1459                     (setq to-count (1- to-count))))
1460                 (goto-char (point-min))
1461                 (while (re-search-forward "^cc:" nil t)
1462                   (setq cc-count (1+ cc-count)))
1463                 (when (> cc-count 1)
1464                   (while (> cc-count 0)
1465                     (goto-char (point-min))
1466                     (save-restriction
1467                       (re-search-forward "^cc:" nil nil cc-count)
1468                       (forward-line -1)
1469                       (narrow-to-region (point) (point-max))
1470                       (gnus-article-hide-header "cc"))
1471                     (setq cc-count (1- cc-count)))))))))))))
1472
1473 (defun gnus-article-hide-header (header)
1474   (save-excursion
1475     (goto-char (point-min))
1476     (when (re-search-forward (concat "^" header ":") nil t)
1477       (gnus-article-hide-text-type
1478        (progn (beginning-of-line) (point))
1479        (progn
1480          (end-of-line)
1481          (if (re-search-forward "^[^ \t]" nil t)
1482              (match-beginning 0)
1483            (point-max)))
1484        'boring-headers))))
1485
1486 (defun article-toggle-headers (&optional arg)
1487   "Toggle hiding of headers.  If given a negative prefix, always show;
1488 if given a positive prefix, always hide."
1489   (interactive (gnus-article-hidden-arg))
1490   (let ((force (when (numberp arg)
1491                  (cond ((> arg 0) 'always-hide)
1492                        ((< arg 0) 'always-show))))
1493         (window (get-buffer-window gnus-article-buffer))
1494         (header-end (point-min))
1495         header-start field-end field-start
1496         (inhibit-point-motion-hooks t)
1497         (inhibit-read-only t)
1498         buffer-read-only)
1499     (save-restriction
1500       (widen)
1501       (while (and (setq header-start
1502                         (text-property-any header-end (point-max)
1503                                            'article-treated-header t))
1504                   (setq header-end
1505                         (text-property-not-all header-start (point-max)
1506                                                'article-treated-header t)))
1507         (setq field-end header-start)
1508         (cond
1509          (;; Hide exposed invisible fields.
1510           (and (not (eq 'always-show force))
1511                (setq field-start
1512                      (text-property-any field-end header-end
1513                                         'exposed-invisible-field t)))
1514           (while (and field-start
1515                       (setq field-end (text-property-not-all
1516                                        field-start header-end
1517                                        'exposed-invisible-field t)))
1518             (add-text-properties field-start field-end gnus-hidden-properties)
1519             (setq field-start (text-property-any field-end header-end
1520                                                  'exposed-invisible-field t)))
1521           (put-text-property header-start header-end
1522                              'exposed-invisible-field nil))
1523          (;; Expose invisible fields.
1524           (and (not (eq 'always-hide force))
1525                (setq field-start
1526                      (text-property-any field-end header-end 'invisible t)))
1527           (while (and field-start
1528                       (setq field-end (text-property-not-all
1529                                        field-start header-end
1530                                        'invisible t)))
1531             ;; If the invisible text is not terminated with newline, we
1532             ;; won't expose it.  Because it may be created by x-face-mule.
1533             ;; BTW, XEmacs sometimes fail in putting a invisible text
1534             ;; property with `gnus-article-hide-text' (really?).  In that
1535             ;; case, the invisible text might be started from the middle of
1536             ;; a line so we will expose the sort of thing.
1537             (when (or (not (or (eq header-start field-start)
1538                                (eq ?\n (char-before field-start))))
1539                       (eq ?\n (char-before field-end)))
1540               (remove-text-properties field-start field-end
1541                                       gnus-hidden-properties)
1542               (put-text-property field-start field-end
1543                                  'exposed-invisible-field t))
1544             (setq field-start (text-property-any field-end header-end
1545                                                  'invisible t))))
1546          (;; Hide fields.
1547           (not (eq 'always-show force))
1548           (narrow-to-region header-start header-end)
1549           (article-hide-headers)
1550           ;; Re-display X-Face image under XEmacs.
1551           (when (and (featurep 'xemacs)
1552                      (gnus-functionp gnus-article-x-face-command))
1553             (let ((func (cadr (assq 'gnus-treat-display-xface
1554                                     gnus-treatment-function-alist)))
1555                   (condition 'head))
1556               (when (and (not gnus-inhibit-treatment)
1557                          func
1558                          (gnus-treat-predicate gnus-treat-display-xface))
1559                 (funcall func)
1560                 (put-text-property header-start header-end 'read-only nil))))
1561           (widen))
1562          ))
1563       (goto-char (point-min))
1564       (when window
1565         (set-window-start window (point-min))))))
1566
1567 (defvar gnus-article-normalized-header-length 40
1568   "Length of normalized headers.")
1569
1570 (defun article-normalize-headers ()
1571   "Make all header lines 40 characters long."
1572   (interactive)
1573   (let ((buffer-read-only nil)
1574         column)
1575     (save-excursion
1576       (save-restriction
1577         (article-narrow-to-head)
1578         (while (not (eobp))
1579           (cond
1580            ((< (setq column (- (gnus-point-at-eol) (point)))
1581                gnus-article-normalized-header-length)
1582             (end-of-line)
1583             (insert (make-string
1584                      (- gnus-article-normalized-header-length column)
1585                      ? )))
1586            ((> column gnus-article-normalized-header-length)
1587             (gnus-put-text-property
1588              (progn
1589                (forward-char gnus-article-normalized-header-length)
1590                (point))
1591              (gnus-point-at-eol)
1592              'invisible t))
1593            (t
1594             ;; Do nothing.
1595             ))
1596           (forward-line 1))))))
1597
1598 (defun article-treat-dumbquotes ()
1599   "Translate M****s*** sm*rtq**t*s into proper text.
1600 Note that this function guesses whether a character is a sm*rtq**t* or
1601 not, so it should only be used interactively.
1602
1603 Sm*rtq**t*s are M****s***'s unilateral extension to the character map
1604 in an attempt to provide more quoting characters.  If you see
1605 something like \\222 or \\264 where you're expecting some kind of
1606 apostrophe or quotation mark, then try this wash."
1607   (interactive)
1608   (article-translate-strings gnus-article-dumbquotes-map))
1609
1610 (defun article-translate-characters (from to)
1611   "Translate all characters in the body of the article according to FROM and TO.
1612 FROM is a string of characters to translate from; to is a string of
1613 characters to translate to."
1614   (save-excursion
1615     (when (article-goto-body)
1616       (let ((buffer-read-only nil)
1617             (x (make-string 225 ?x))
1618             (i -1))
1619         (while (< (incf i) (length x))
1620           (aset x i i))
1621         (setq i 0)
1622         (while (< i (length from))
1623           (aset x (aref from i) (aref to i))
1624           (incf i))
1625         (translate-region (point) (point-max) x)))))
1626
1627 (defun article-translate-strings (map)
1628   "Translate all string in the body of the article according to MAP.
1629 MAP is an alist where the elements are on the form (\"from\" \"to\")."
1630   (save-excursion
1631     (when (article-goto-body)
1632       (let ((buffer-read-only nil)
1633             elem)
1634         (while (setq elem (pop map))
1635           (save-excursion
1636             (while (search-forward (car elem) nil t)
1637               (replace-match (cadr elem)))))))))
1638
1639 (defun article-treat-overstrike ()
1640   "Translate overstrikes into bold text."
1641   (interactive)
1642   (save-excursion
1643     (when (article-goto-body)
1644       (let ((buffer-read-only nil))
1645         (while (search-forward "\b" nil t)
1646           (let ((next (char-after))
1647                 start end previous)
1648             (backward-char 2)
1649             (setq start (point)
1650                   previous (char-after))
1651             (forward-char 3)
1652             (setq end (point))
1653             (backward-char)
1654             ;; We do the boldification/underlining by hiding the
1655             ;; overstrikes and putting the proper text property
1656             ;; on the letters.
1657             (cond
1658              ((eq next previous)
1659               (gnus-article-hide-text-type start (point) 'overstrike)
1660               (put-text-property (point) end 'face 'bold))
1661              ((eq next ?_)
1662               (gnus-article-hide-text-type
1663                (1- (point)) (1+ (point)) 'overstrike)
1664               (put-text-property
1665                start (1- (point)) 'face 'underline))
1666              ((eq previous ?_)
1667               (gnus-article-hide-text-type start (point) 'overstrike)
1668               (put-text-property
1669                (point) end 'face 'underline)))))))))
1670
1671 (defun article-fill-long-lines ()
1672   "Fill lines that are wider than the window width."
1673   (interactive)
1674   (save-excursion
1675     (let ((buffer-read-only nil)
1676           (width (window-width (get-buffer-window (current-buffer)))))
1677       (save-restriction
1678         (article-goto-body)
1679         (let ((adaptive-fill-mode nil))
1680           (while (not (eobp))
1681             (end-of-line)
1682             (when (>= (current-column) (min fill-column width))
1683               (narrow-to-region (point) (gnus-point-at-bol))
1684               (fill-paragraph nil)
1685               (goto-char (point-max))
1686               (widen))
1687             (forward-line 1)))))))
1688
1689 (defun article-capitalize-sentences ()
1690   "Capitalize the first word in each sentence."
1691   (interactive)
1692   (save-excursion
1693     (let ((buffer-read-only nil)
1694           (paragraph-start "^[\n\^L]"))
1695       (article-goto-body)
1696       (while (not (eobp))
1697         (capitalize-word 1)
1698         (forward-sentence)))))
1699
1700 (defun article-remove-cr ()
1701   "Remove trailing CRs and then translate remaining CRs into LFs."
1702   (interactive)
1703   (save-excursion
1704     (let ((buffer-read-only nil))
1705       (goto-char (point-min))
1706       (while (re-search-forward "\r+$" nil t)
1707         (replace-match "" t t))
1708       (goto-char (point-min))
1709       (while (search-forward "\r" nil t)
1710         (replace-match "\n" t t)))))
1711
1712 (defun article-remove-trailing-blank-lines ()
1713   "Remove all trailing blank lines from the article."
1714   (interactive)
1715   (save-excursion
1716     (let ((buffer-read-only nil))
1717       (goto-char (point-max))
1718       (delete-region
1719        (point)
1720        (progn
1721          (while (and (not (bobp))
1722                      (looking-at "^[ \t]*$")
1723                      (not (gnus-annotation-in-region-p
1724                            (point) (gnus-point-at-eol))))
1725            (forward-line -1))
1726          (forward-line 1)
1727          (point))))))
1728
1729 (defun article-display-x-face (&optional force)
1730   "Look for an X-Face header and display it if present."
1731   (interactive (list 'force))
1732   (save-excursion
1733     ;; Delete the old process, if any.
1734     (when (process-status "article-x-face")
1735       (delete-process "article-x-face"))
1736     (let ((inhibit-point-motion-hooks t)
1737           x-faces
1738           (case-fold-search t)
1739           from last)
1740       (save-restriction
1741         (article-narrow-to-head)
1742         (when (and buffer-read-only ;; When type `W f'
1743                    (progn
1744                      (goto-char (point-min))
1745                      (not (re-search-forward "^X-Face:[\t ]*" nil t)))
1746                    (gnus-buffer-live-p gnus-original-article-buffer))
1747           (with-current-buffer gnus-original-article-buffer
1748             (save-restriction
1749               (article-narrow-to-head)
1750               (while (re-search-forward "^X-Face:" nil t)
1751                 (setq x-faces
1752                       (concat
1753                        (or x-faces "")
1754                        (buffer-substring
1755                         (match-beginning 0)
1756                         (1- (re-search-forward
1757                              "^\\($\\|[^ \t]\\)" nil t))))))))
1758           (if x-faces
1759               (let (point start bface eface buffer-read-only)
1760                 (goto-char (point-max))
1761                 (forward-line -1)
1762                 (setq bface (get-text-property (gnus-point-at-bol) 'face)
1763                       eface (get-text-property (1- (gnus-point-at-eol)) 'face))
1764                 (goto-char (point-max))
1765                 (setq point (point))
1766                 (insert x-faces)
1767                 (goto-char point)
1768                 (while (looking-at "\\([^:]+\\): *")
1769                   (put-text-property (match-beginning 1) (1+ (match-end 1))
1770                                      'face bface)
1771                   (setq start (match-end 0))
1772                   (forward-line 1)
1773                   (while (looking-at "[\t ]")
1774                     (forward-line 1))
1775                   (put-text-property start (point)
1776                                      'face eface)))))
1777         (goto-char (point-min))
1778         (setq from (message-fetch-field "from"))
1779         (goto-char (point-min))
1780         (while (and gnus-article-x-face-command
1781                     (not last)
1782                     (or force
1783                         ;; Check whether this face is censored.
1784                         (not gnus-article-x-face-too-ugly)
1785                         (and gnus-article-x-face-too-ugly from
1786                              (not (string-match gnus-article-x-face-too-ugly
1787                                                 from))))
1788                     ;; Has to be present.
1789                     (re-search-forward "^X-Face:[\t ]*" nil t))
1790           ;; This used to try to do multiple faces (`while' instead of
1791           ;; `when' above), but (a) sending multiple EOFs to xv doesn't
1792           ;; work (b) it can crash some versions of Emacs (c) are
1793           ;; multiple faces really something to encourage?
1794           (when (stringp gnus-article-x-face-command)
1795             (setq last t))
1796           ;; We now have the area of the buffer where the X-Face is stored.
1797           (save-excursion
1798             (let ((beg (point))
1799                   (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
1800               ;; We display the face.
1801               (if (symbolp gnus-article-x-face-command)
1802                   ;; The command is a lisp function, so we call it.
1803                   (if (gnus-functionp gnus-article-x-face-command)
1804                       (funcall gnus-article-x-face-command beg end)
1805                     (error "%s is not a function" gnus-article-x-face-command))
1806                 ;; The command is a string, so we interpret the command
1807                 ;; as a, well, command, and fork it off.
1808                 (let ((process-connection-type nil))
1809                   (process-kill-without-query
1810                    (start-process
1811                     "article-x-face" nil shell-file-name shell-command-switch
1812                     gnus-article-x-face-command))
1813                   (process-send-region "article-x-face" beg end)
1814                   (process-send-eof "article-x-face"))))))))))
1815
1816 (defun article-decode-mime-words ()
1817   "Decode all MIME-encoded words in the article."
1818   (interactive)
1819   (save-excursion
1820     (set-buffer gnus-article-buffer)
1821     (let ((inhibit-point-motion-hooks t)
1822           buffer-read-only
1823           (mail-parse-charset gnus-newsgroup-charset)
1824           (mail-parse-ignored-charsets
1825            (save-excursion (set-buffer gnus-summary-buffer)
1826                            gnus-newsgroup-ignored-charsets)))
1827       (mail-decode-encoded-word-region (point-min) (point-max)))))
1828
1829 (defun article-decode-charset (&optional prompt)
1830   "Decode charset-encoded text in the article.
1831 If PROMPT (the prefix), prompt for a coding system to use."
1832   (interactive "P")
1833   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
1834         buffer-read-only
1835         (mail-parse-charset gnus-newsgroup-charset)
1836         (mail-parse-ignored-charsets
1837          (save-excursion (condition-case nil
1838                              (set-buffer gnus-summary-buffer)
1839                            (error))
1840                          gnus-newsgroup-ignored-charsets))
1841         ct cte ctl charset format)
1842   (save-excursion
1843     (save-restriction
1844       (article-narrow-to-head)
1845       (setq ct (message-fetch-field "Content-Type" t)
1846             cte (message-fetch-field "Content-Transfer-Encoding" t)
1847             ctl (and ct (ignore-errors
1848                           (mail-header-parse-content-type ct)))
1849             charset (cond
1850                      (prompt
1851                       (mm-read-coding-system "Charset to decode: "))
1852                      (ctl
1853                       (mail-content-type-get ctl 'charset)))
1854             format (and ctl (mail-content-type-get ctl 'format)))
1855       (when cte
1856         (setq cte (mail-header-strip cte)))
1857       (if (and ctl (not (string-match "/" (car ctl))))
1858           (setq ctl nil))
1859       (goto-char (point-max)))
1860     (forward-line 1)
1861     (save-restriction
1862       (narrow-to-region (point) (point-max))
1863       (when (and (eq mail-parse-charset 'gnus-decoded)
1864                  (eq (mm-body-7-or-8) '8bit))
1865         ;; The text code could have been decoded.
1866         (setq charset mail-parse-charset))
1867       (when (and (or (not ctl)
1868                      (equal (car ctl) "text/plain"))
1869                  (not format)) ;; article with format will decode later.
1870         (mm-decode-body
1871          charset (and cte (intern (downcase
1872                                    (gnus-strip-whitespace cte))))
1873          (car ctl)))))))
1874
1875 (defun article-decode-encoded-words ()
1876   "Remove encoded-word encoding from headers."
1877   (let (buffer-read-only)
1878     (let ((charset (save-excursion
1879                      (set-buffer gnus-summary-buffer)
1880                      default-mime-charset)))
1881       (mime-decode-header-in-buffer charset)
1882       )))
1883
1884 (defun article-de-quoted-unreadable (&optional force)
1885   "Translate a quoted-printable-encoded article.
1886 If FORCE, decode the article whether it is marked as quoted-printable
1887 or not."
1888   (interactive (list 'force))
1889   (save-excursion
1890     (let ((buffer-read-only nil) type charset)
1891       (if (gnus-buffer-live-p gnus-original-article-buffer)
1892           (with-current-buffer gnus-original-article-buffer
1893             (setq type
1894                   (gnus-fetch-field "content-transfer-encoding"))
1895             (let* ((ct (gnus-fetch-field "content-type"))
1896                    (ctl (and ct
1897                              (ignore-errors
1898                                (mail-header-parse-content-type ct)))))
1899               (setq charset (and ctl
1900                                  (mail-content-type-get ctl 'charset)))
1901               (if (stringp charset)
1902                   (setq charset (intern (downcase charset)))))))
1903       (unless charset
1904         (setq charset gnus-newsgroup-charset))
1905       (when (or force
1906                 (and type (let ((case-fold-search t))
1907                             (string-match "quoted-printable" type))))
1908         (article-goto-body)
1909         (quoted-printable-decode-region
1910          (point) (point-max) (mm-charset-to-coding-system charset))))))
1911
1912 (defun article-de-base64-unreadable (&optional force)
1913   "Translate a base64 article.
1914 If FORCE, decode the article whether it is marked as base64 not."
1915   (interactive (list 'force))
1916   (save-excursion
1917     (let ((buffer-read-only nil) type charset)
1918       (if (gnus-buffer-live-p gnus-original-article-buffer)
1919           (with-current-buffer gnus-original-article-buffer
1920             (setq type
1921                   (gnus-fetch-field "content-transfer-encoding"))
1922             (let* ((ct (gnus-fetch-field "content-type"))
1923                    (ctl (and ct
1924                              (ignore-errors
1925                                (mail-header-parse-content-type ct)))))
1926               (setq charset (and ctl
1927                                  (mail-content-type-get ctl 'charset)))
1928               (if (stringp charset)
1929                   (setq charset (intern (downcase charset)))))))
1930       (unless charset
1931         (setq charset gnus-newsgroup-charset))
1932       (when (or force
1933                 (and type (let ((case-fold-search t))
1934                             (string-match "base64" type))))
1935         (article-goto-body)
1936         (save-restriction
1937           (narrow-to-region (point) (point-max))
1938           (base64-decode-region (point-min) (point-max))
1939           (mm-decode-coding-region
1940            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
1941
1942 (eval-when-compile
1943   (require 'rfc1843))
1944
1945 (defun article-decode-HZ ()
1946   "Translate a HZ-encoded article."
1947   (interactive)
1948   (require 'rfc1843)
1949   (save-excursion
1950     (let ((buffer-read-only nil))
1951       (rfc1843-decode-region (point-min) (point-max)))))
1952
1953 (defun article-wash-html ()
1954   "Format an html article."
1955   (interactive)
1956   (save-excursion
1957     (let ((buffer-read-only nil)
1958           charset)
1959       (if (gnus-buffer-live-p gnus-original-article-buffer)
1960           (with-current-buffer gnus-original-article-buffer
1961             (let* ((ct (gnus-fetch-field "content-type"))
1962                    (ctl (and ct
1963                              (ignore-errors
1964                                (mail-header-parse-content-type ct)))))
1965               (setq charset (and ctl
1966                                  (mail-content-type-get ctl 'charset)))
1967               (if (stringp charset)
1968                   (setq charset (intern (downcase charset)))))))
1969       (unless charset
1970         (setq charset gnus-newsgroup-charset))
1971       (article-goto-body)
1972       (save-window-excursion
1973         (save-restriction
1974           (narrow-to-region (point) (point-max))
1975           (mm-setup-w3)
1976           (let ((w3-strict-width (window-width))
1977                 (url-standalone-mode t))
1978             (condition-case var
1979                 (w3-region (point-min) (point-max))
1980               (error))))))))
1981
1982 (defun article-hide-list-identifiers ()
1983   "Remove list identifies from the Subject header.
1984 The `gnus-list-identifiers' variable specifies what to do."
1985   (interactive)
1986   (let ((inhibit-point-motion-hooks t)
1987         (regexp (if (consp gnus-list-identifiers)
1988                     (mapconcat 'identity gnus-list-identifiers " *\\|")
1989                   gnus-list-identifiers))
1990         buffer-read-only)
1991     (when regexp
1992       (save-excursion
1993         (save-restriction
1994           (article-narrow-to-head)
1995           (goto-char (point-min))
1996           (while (re-search-forward
1997                   (concat "^Subject: +\\(R[Ee]: +\\)*\\(" regexp " *\\)")
1998                   nil t)
1999             (delete-region (match-beginning 2) (match-end 0))
2000             (beginning-of-line))
2001           (when (re-search-forward
2002                  "^Subject: +\\(\\(R[Ee]: +\\)+\\)R[Ee]: +" nil t)
2003             (delete-region (match-beginning 1) (match-end 1))))))))
2004
2005 (defun article-hide-pgp ()
2006   "Remove any PGP headers and signatures in the current article."
2007   (interactive)
2008   (save-excursion
2009     (save-restriction
2010       (let ((inhibit-point-motion-hooks t)
2011             buffer-read-only beg end)
2012         (article-goto-body)
2013         ;; Hide the "header".
2014         (when (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
2015           (push 'pgp gnus-article-wash-types)
2016           (delete-region (match-beginning 0) (match-end 0))
2017           ;; Remove armor headers (rfc2440 6.2)
2018           (delete-region (point) (or (re-search-forward "^[ \t]*\n" nil t)
2019                                      (point)))
2020           (setq beg (point))
2021           ;; Hide the actual signature.
2022           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
2023                (setq end (1+ (match-beginning 0)))
2024                (delete-region
2025                 end
2026                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
2027                     (match-end 0)
2028                   ;; Perhaps we shouldn't hide to the end of the buffer
2029                   ;; if there is no end to the signature?
2030                   (point-max))))
2031           ;; Hide "- " PGP quotation markers.
2032           (when (and beg end)
2033             (narrow-to-region beg end)
2034             (goto-char (point-min))
2035             (while (re-search-forward "^- " nil t)
2036               (delete-region
2037                (match-beginning 0) (match-end 0)))
2038             (widen))
2039           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
2040
2041 (defun article-hide-pem (&optional arg)
2042   "Toggle hiding of any PEM headers and signatures in the current article.
2043 If given a negative prefix, always show; if given a positive prefix,
2044 always hide."
2045   (interactive (gnus-article-hidden-arg))
2046   (unless (gnus-article-check-hidden-text 'pem arg)
2047     (save-excursion
2048       (let (buffer-read-only end)
2049         (goto-char (point-min))
2050         ;; Hide the horrendously ugly "header".
2051         (when (and (search-forward
2052                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
2053                     nil t)
2054                    (setq end (1+ (match-beginning 0))))
2055           (push 'pem gnus-article-wash-types)
2056           (gnus-article-hide-text-type
2057            end
2058            (if (search-forward "\n\n" nil t)
2059                (match-end 0)
2060              (point-max))
2061            'pem)
2062           ;; Hide the trailer as well
2063           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
2064                                 nil t)
2065             (gnus-article-hide-text-type
2066              (match-beginning 0) (match-end 0) 'pem)))))))
2067
2068 (defun article-strip-banner ()
2069   "Strip the banner specified by the `banner' group parameter."
2070   (interactive)
2071   (save-excursion
2072     (save-restriction
2073       (let ((inhibit-point-motion-hooks t)
2074             (banner (gnus-parameter-banner gnus-newsgroup-name))
2075             (gnus-signature-limit nil)
2076             buffer-read-only beg end)
2077         (when banner
2078           (article-goto-body)
2079           (cond
2080            ((eq banner 'signature)
2081             (when (gnus-article-narrow-to-signature)
2082               (widen)
2083               (forward-line -1)
2084               (delete-region (point) (point-max))))
2085            ((symbolp banner)
2086             (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
2087                 (while (re-search-forward banner nil t)
2088                   (delete-region (match-beginning 0) (match-end 0)))))
2089            ((stringp banner)
2090             (while (re-search-forward banner nil t)
2091               (delete-region (match-beginning 0) (match-end 0))))))))))
2092
2093 (defun article-babel ()
2094   "Translate article using an online translation service."
2095   (interactive)
2096   (require 'babel)
2097   (save-excursion
2098     (set-buffer gnus-article-buffer)
2099     (when (article-goto-body)
2100       (let* ((buffer-read-only nil)
2101              (start (point))
2102              (end (point-max))
2103              (orig (buffer-substring start end))
2104              (trans (babel-as-string orig)))
2105         (save-restriction
2106           (narrow-to-region start end)
2107           (delete-region start end)
2108           (insert trans))))))
2109
2110 (defun article-hide-signature (&optional arg)
2111   "Hide the signature in the current article.
2112 If given a negative prefix, always show; if given a positive prefix,
2113 always hide."
2114   (interactive (gnus-article-hidden-arg))
2115   (save-excursion
2116     (save-restriction
2117       (if (interactive-p)
2118           (progn
2119             (widen)
2120             (article-goto-body))
2121         (goto-char (point-min)))
2122       (unless (gnus-article-check-hidden-text 'signature arg)
2123         (let ((buffer-read-only nil)
2124               (button (point)))
2125           (while (setq button (text-property-any button (point-max)
2126                                                  'gnus-callback
2127                                                  'gnus-signature-toggle))
2128             (setq button (text-property-not-all button (point-max)
2129                                                 'gnus-callback
2130                                                 'gnus-signature-toggle))
2131             (when (and button (not (eobp)))
2132               (gnus-article-hide-text-type
2133                (1+ button)
2134                (next-single-property-change (1+ button) 'mime-view-entity
2135                                             nil (point-max))
2136                'signature))))))))
2137
2138 (defun article-strip-headers-in-body ()
2139   "Strip offensive headers from bodies."
2140   (interactive)
2141   (save-excursion
2142     (article-goto-body)
2143     (let ((case-fold-search t))
2144       (when (looking-at "x-no-archive:")
2145         (gnus-delete-line)))))
2146
2147 (defun article-strip-leading-blank-lines ()
2148   "Remove all blank lines from the beginning of the article."
2149   (interactive)
2150   (save-excursion
2151     (let ((inhibit-point-motion-hooks t)
2152           buffer-read-only)
2153       (when (article-goto-body)
2154         (while (and (not (eobp))
2155                     (looking-at "[ \t]*$"))
2156           (gnus-delete-line))))))
2157
2158 (defun article-replace-with-quoted-text ()
2159   "Replace the entire article with the quoted text in the article."
2160   (interactive)
2161   (unless gnus-cite-prefix-alist
2162     (error "No quoted text in the article"))
2163   (gnus-summary-show-article t)
2164   (save-excursion
2165     (set-buffer gnus-article-buffer)
2166     (gnus-cite-parse-maybe t)
2167     (let ((prefix (concat "^" (caar gnus-cite-prefix-alist)))
2168           (buffer-read-only nil)
2169           (body nil))
2170       (dolist (line (sort (copy-sequence (cdar gnus-cite-prefix-alist)) '<))
2171         (save-excursion
2172           (set-buffer gnus-original-article-buffer)
2173           (goto-char (point-min))
2174           (forward-line (1- line))
2175           (push (buffer-substring (point) (progn (forward-line 1) (point)))
2176                 body)))
2177       (article-goto-body)
2178       (forward-line -1)
2179       (delete-region (point) (point-max))
2180       (mapcar #'insert (mapcar #'string-as-unibyte (nreverse body)))
2181       (goto-char (point-min))
2182       (while (re-search-forward prefix nil t)
2183         (replace-match "" t t))
2184       (gnus-article-prepare-display))))
2185
2186 (defun article-narrow-to-head ()
2187   "Narrow the buffer to the head of the message.
2188 Point is left at the beginning of the narrowed-to region."
2189   (narrow-to-region
2190    (goto-char (point-min))
2191    (if (search-forward "\n\n" nil 1)
2192        (1- (point))
2193      (point-max)))
2194   (goto-char (point-min)))
2195
2196 (defun article-goto-body ()
2197   "Place point at the start of the body."
2198   (goto-char (point-min))
2199   (cond
2200    ;; This variable is only bound when dealing with separate
2201    ;; MIME body parts.
2202    (article-goto-body-goes-to-point-min-p
2203     t)
2204    ((search-forward "\n\n" nil t)
2205     t)
2206    (t
2207     (goto-char (point-max))
2208     nil)))
2209
2210 (defun article-strip-multiple-blank-lines ()
2211   "Replace consecutive blank lines with one empty line."
2212   (interactive)
2213   (save-excursion
2214     (let ((inhibit-point-motion-hooks t)
2215           buffer-read-only)
2216       ;; First make all blank lines empty.
2217       (article-goto-body)
2218       (while (re-search-forward "^[ \t]+$" nil t)
2219         (unless (gnus-annotation-in-region-p
2220                  (match-beginning 0) (match-end 0))
2221           (replace-match "" nil t)))
2222       ;; Then replace multiple empty lines with a single empty line.
2223       (article-goto-body)
2224       (while (re-search-forward "\n\n\n+" nil t)
2225         (unless (gnus-annotation-in-region-p
2226                  (match-beginning 0) (match-end 0))
2227           (replace-match "\n\n" t t))))))
2228
2229 (defun article-strip-leading-space ()
2230   "Remove all white space from the beginning of the lines in the article."
2231   (interactive)
2232   (save-excursion
2233     (let ((inhibit-point-motion-hooks t)
2234           buffer-read-only)
2235       (article-goto-body)
2236       (while (re-search-forward "^[ \t]+" nil t)
2237         (replace-match "" t t)))))
2238
2239 (defun article-strip-trailing-space ()
2240   "Remove all white space from the end of the lines in the article."
2241   (interactive)
2242   (save-excursion
2243     (let ((inhibit-point-motion-hooks t)
2244           buffer-read-only)
2245       (article-goto-body)
2246       (while (re-search-forward "[ \t]+$" nil t)
2247         (replace-match "" t t)))))
2248
2249 (defun article-strip-blank-lines ()
2250   "Strip leading, trailing and multiple blank lines."
2251   (interactive)
2252   (article-strip-leading-blank-lines)
2253   (article-remove-trailing-blank-lines)
2254   (article-strip-multiple-blank-lines))
2255
2256 (defun article-strip-all-blank-lines ()
2257   "Strip all blank lines."
2258   (interactive)
2259   (save-excursion
2260     (let ((inhibit-point-motion-hooks t)
2261           buffer-read-only)
2262       (article-goto-body)
2263       (while (re-search-forward "^[ \t]*\n" nil t)
2264         (replace-match "" t t)))))
2265
2266 (defun gnus-article-narrow-to-signature ()
2267   "Narrow to the signature; return t if a signature is found, else nil."
2268   (let ((inhibit-point-motion-hooks t))
2269     (when (gnus-article-search-signature)
2270       (forward-line 1)
2271       ;; Check whether we have some limits to what we consider
2272       ;; to be a signature.
2273       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
2274                       (list gnus-signature-limit)))
2275             limit limited)
2276         (while (setq limit (pop limits))
2277           (if (or (and (integerp limit)
2278                        (< (- (point-max) (point)) limit))
2279                   (and (floatp limit)
2280                        (< (count-lines (point) (point-max)) limit))
2281                   (and (gnus-functionp limit)
2282                        (funcall limit))
2283                   (and (stringp limit)
2284                        (not (re-search-forward limit nil t))))
2285               ()                        ; This limit did not succeed.
2286             (setq limited t
2287                   limits nil)))
2288         (unless limited
2289           (narrow-to-region (point) (point-max))
2290           t)))))
2291
2292 (defun gnus-article-search-signature ()
2293   "Search the current buffer for the signature separator.
2294 Put point at the beginning of the signature separator."
2295   (let ((cur (point)))
2296     (goto-char (point-max))
2297     (if (if (stringp gnus-signature-separator)
2298             (re-search-backward gnus-signature-separator nil t)
2299           (let ((seps gnus-signature-separator))
2300             (while (and seps
2301                         (not (re-search-backward (car seps) nil t)))
2302               (pop seps))
2303             seps))
2304         t
2305       (goto-char cur)
2306       nil)))
2307
2308 (defun gnus-article-hidden-arg ()
2309   "Return the current prefix arg as a number, or 0 if no prefix."
2310   (list (if current-prefix-arg
2311             (prefix-numeric-value current-prefix-arg)
2312           0)))
2313
2314 (defun gnus-article-check-hidden-text (type arg)
2315   "Return nil if hiding is necessary.
2316 Arg can be nil or a number.  Nil and positive means hide, negative
2317 means show, 0 means toggle."
2318   (save-excursion
2319     (save-restriction
2320       (let ((hide (gnus-article-hidden-text-p type)))
2321         (cond
2322          ((or (null arg)
2323               (> arg 0))
2324           nil)
2325          ((< arg 0)
2326           (gnus-article-show-hidden-text type)
2327           t)
2328          (t
2329           (if (eq hide 'hidden)
2330               (progn
2331                 (gnus-article-show-hidden-text type)
2332                 t)
2333             nil)))))))
2334
2335 (defun gnus-article-hidden-text-p (type)
2336   "Say whether the current buffer contains hidden text of type TYPE."
2337   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
2338     (while (and pos
2339                 (not (get-text-property pos 'invisible))
2340                 (not (get-text-property pos 'dummy-invisible)))
2341       (setq pos
2342             (text-property-any (1+ pos) (point-max) 'article-type type)))
2343     (if pos
2344         'hidden
2345       nil)))
2346
2347 (defun gnus-article-show-hidden-text (type &optional dummy)
2348   "Show all hidden text of type TYPE.
2349 Originally it is hide instead of DUMMY."
2350   (let ((buffer-read-only nil)
2351         (inhibit-point-motion-hooks t))
2352     (gnus-remove-text-properties-when
2353      'article-type type
2354      (point-min) (point-max)
2355      (cons 'article-type (cons type
2356                                gnus-hidden-properties)))))
2357
2358 (defconst article-time-units
2359   `((year . ,(* 365.25 24 60 60))
2360     (week . ,(* 7 24 60 60))
2361     (day . ,(* 24 60 60))
2362     (hour . ,(* 60 60))
2363     (minute . 60)
2364     (second . 1))
2365   "Mapping from time units to seconds.")
2366
2367 (defun article-date-ut (&optional type highlight header)
2368   "Convert DATE date to universal time in the current article.
2369 If TYPE is `local', convert to local time; if it is `lapsed', output
2370 how much time has lapsed since DATE.  For `lapsed', the value of
2371 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
2372 should replace the \"Date:\" one, or should be added below it."
2373   (interactive (list 'ut t))
2374   (let* ((header (or header
2375                      (and (eq 1 (point-min))
2376                           (mail-header-date (save-excursion
2377                                               (set-buffer gnus-summary-buffer)
2378                                               gnus-current-headers)))
2379                      (message-fetch-field "date")
2380                      ""))
2381          (date (if (vectorp header) (mail-header-date header)
2382                  header))
2383          (inhibit-point-motion-hooks t)
2384          bface eface date-pos)
2385     (when (and date (not (string= date "")))
2386       (save-excursion
2387         (save-restriction
2388           (article-narrow-to-head)
2389           (when (or (and (eq type 'lapsed)
2390                          gnus-article-date-lapsed-new-header
2391                          ;; Attempt to get the face of X-Sent first.
2392                          (re-search-forward "^X-Sent:[ \t]" nil t))
2393                     (re-search-forward "^Date:[ \t]" nil t)
2394                     ;; If Date is missing, try again for X-Sent.
2395                     (re-search-forward "^X-Sent:[ \t]" nil t))
2396             (setq bface (get-text-property (gnus-point-at-bol) 'face)
2397                   date (or (get-text-property (gnus-point-at-bol)
2398                                                 'original-date)
2399                              date)
2400                   eface (get-text-property (1- (gnus-point-at-eol))
2401                                            'face)))
2402           (let ((buffer-read-only nil))
2403             ;; Delete any old X-Sent headers.
2404             (when (setq date-pos
2405                         (text-property-any (point-min) (point-max)
2406                                            'article-date-lapsed t))
2407               (goto-char (setq date-pos (set-marker (make-marker) date-pos)))
2408               (delete-region (match-beginning 0)
2409                              (progn (forward-line 1) (point))))
2410             (goto-char (point-min))
2411             ;; Delete any old Date headers.
2412             (while (re-search-forward "^Date:[ \t]" nil t)
2413               (unless date-pos
2414                 (setq date-pos (match-beginning 0)))
2415               (unless (and (eq type 'lapsed)
2416                            gnus-article-date-lapsed-new-header)
2417                 (delete-region (match-beginning 0)
2418                                (progn (message-next-header) (point)))))
2419             (if date-pos
2420                 (progn
2421                   (goto-char date-pos)
2422                   (unless (bolp)
2423                     ;; Possibly, Date has been deleted.
2424                     (insert "\n"))
2425                   (when (and (eq type 'lapsed)
2426                              gnus-article-date-lapsed-new-header
2427                              (looking-at "Date:"))
2428                     (forward-line 1)))
2429               (goto-char (point-min)))
2430             (insert (article-make-date-line date type))
2431             (when (eq type 'lapsed)
2432               (put-text-property (gnus-point-at-bol) (point)
2433                                  'article-date-lapsed t))
2434             (insert "\n")
2435             (forward-line -1)
2436             ;; Do highlighting.
2437             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
2438               (put-text-property (match-beginning 1) (1+ (match-end 1))
2439                                  'original-date date)
2440               (put-text-property (match-beginning 1) (1+ (match-end 1))
2441                                  'face bface)
2442               (put-text-property (match-beginning 2) (match-end 2)
2443                                  'face eface))))))))
2444
2445 (defun article-make-date-line (date type)
2446   "Return a DATE line of TYPE."
2447   (unless (memq type '(local ut original user iso8601 lapsed english))
2448     (error "Unknown conversion type: %s" type))
2449   (condition-case ()
2450       (let ((time (date-to-time date)))
2451         (cond
2452          ;; Convert to the local timezone.
2453          ((eq type 'local)
2454           (let ((tz (car (current-time-zone time))))
2455             (format "Date: %s %s%02d%02d" (current-time-string time)
2456                     (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2457                     (/ (% (abs tz) 3600) 60))))
2458          ;; Convert to Universal Time.
2459          ((eq type 'ut)
2460           (concat "Date: "
2461                   (current-time-string
2462                    (let* ((e (parse-time-string date))
2463                           (tm (apply 'encode-time e))
2464                           (ms (car tm))
2465                           (ls (- (cadr tm) (car (current-time-zone time)))))
2466                      (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
2467                            ((> ls 65535) (list (1+ ms) (- ls 65536)))
2468                            (t (list ms ls)))))
2469                   " UT"))
2470          ;; Get the original date from the article.
2471          ((eq type 'original)
2472           (concat "Date: " (if (string-match "\n+$" date)
2473                                (substring date 0 (match-beginning 0))
2474                              date)))
2475          ;; Let the user define the format.
2476          ((eq type 'user)
2477           (if (gnus-functionp gnus-article-time-format)
2478               (funcall gnus-article-time-format time)
2479             (concat
2480              "Date: "
2481              (format-time-string gnus-article-time-format time))))
2482          ;; ISO 8601.
2483          ((eq type 'iso8601)
2484           (let ((tz (car (current-time-zone time))))
2485             (concat
2486              "Date: "
2487              (format-time-string "%Y%m%dT%H%M%S" time)
2488              (format "%s%02d%02d"
2489                      (if (> tz 0) "+" "-") (/ (abs tz) 3600)
2490                      (/ (% (abs tz) 3600) 60)))))
2491          ;; Do an X-Sent lapsed format.
2492          ((eq type 'lapsed)
2493           ;; If the date is seriously mangled, the timezone functions are
2494           ;; liable to bug out, so we ignore all errors.
2495           (let* ((now (current-time))
2496                  (real-time (subtract-time now time))
2497                  (real-sec (and real-time
2498                                 (+ (* (float (car real-time)) 65536)
2499                                    (cadr real-time))))
2500                  (sec (and real-time (abs real-sec)))
2501                  num prev)
2502             (cond
2503              ((null real-time)
2504               "X-Sent: Unknown")
2505              ((zerop sec)
2506               "X-Sent: Now")
2507              (t
2508               (concat
2509                "X-Sent: "
2510                ;; This is a bit convoluted, but basically we go
2511                ;; through the time units for years, weeks, etc,
2512                ;; and divide things to see whether that results
2513                ;; in positive answers.
2514                (mapconcat
2515                 (lambda (unit)
2516                   (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
2517                       ;; The (remaining) seconds are too few to
2518                       ;; be divided into this time unit.
2519                       ""
2520                     ;; It's big enough, so we output it.
2521                     (setq sec (- sec (* num (cdr unit))))
2522                     (prog1
2523                         (concat (if prev ", " "") (int-to-string
2524                                                    (floor num))
2525                                 " " (symbol-name (car unit))
2526                                 (if (> num 1) "s" ""))
2527                       (setq prev t))))
2528                 article-time-units "")
2529                ;; If dates are odd, then it might appear like the
2530                ;; article was sent in the future.
2531                (if (> real-sec 0)
2532                    " ago"
2533                  " in the future"))))))
2534          ;; Display the date in proper English
2535          ((eq type 'english)
2536           (let ((dtime (decode-time time)))
2537             (concat
2538              "Date: the "
2539              (number-to-string (nth 3 dtime))
2540              (let ((digit (% (nth 3 dtime) 10)))
2541                (cond
2542                 ((memq (nth 3 dtime) '(11 12 13)) "th")
2543                 ((= digit 1) "st")
2544                 ((= digit 2) "nd")
2545                 ((= digit 3) "rd")
2546                 (t "th")))
2547              " of "
2548              (nth (1- (nth 4 dtime)) gnus-english-month-names)
2549              " "
2550              (number-to-string (nth 5 dtime))
2551              " at "
2552              (format "%02d" (nth 2 dtime))
2553              ":"
2554              (format "%02d" (nth 1 dtime)))))))
2555         (error
2556          (format "Date: %s (from Oort)" date))))
2557
2558 (defun article-date-local (&optional highlight)
2559   "Convert the current article date to the local timezone."
2560   (interactive (list t))
2561   (article-date-ut 'local highlight))
2562
2563 (defun article-date-english (&optional highlight)
2564   "Convert the current article date to something that is proper English."
2565   (interactive (list t))
2566   (article-date-ut 'english highlight))
2567
2568 (defun article-date-original (&optional highlight)
2569   "Convert the current article date to what it was originally.
2570 This is only useful if you have used some other date conversion
2571 function and want to see what the date was before converting."
2572   (interactive (list t))
2573   (article-date-ut 'original highlight))
2574
2575 (defun article-date-lapsed (&optional highlight)
2576   "Convert the current article date to time lapsed since it was sent."
2577   (interactive (list t))
2578   (article-date-ut 'lapsed highlight))
2579
2580 (defun article-update-date-lapsed ()
2581   "Function to be run from a timer to update the lapsed time line."
2582   (let (deactivate-mark)
2583     (save-excursion
2584       (ignore-errors
2585         (walk-windows
2586          (lambda (w)
2587            (set-buffer (window-buffer w))
2588            (when (eq major-mode 'gnus-article-mode)
2589              (goto-char (point-min))
2590              (when (re-search-forward "^X-Sent:" nil t)
2591                (article-date-lapsed t))))
2592          nil 'visible)))))
2593
2594 (defun gnus-start-date-timer (&optional n)
2595   "Start a timer to update the X-Sent header in the article buffers.
2596 The numerical prefix says how frequently (in seconds) the function
2597 is to run."
2598   (interactive "p")
2599   (unless n
2600     (setq n 1))
2601   (gnus-stop-date-timer)
2602   (setq article-lapsed-timer
2603         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
2604
2605 (defun gnus-stop-date-timer ()
2606   "Stop the X-Sent timer."
2607   (interactive)
2608   (when article-lapsed-timer
2609     (nnheader-cancel-timer article-lapsed-timer)
2610     (setq article-lapsed-timer nil)))
2611
2612 (defun article-date-user (&optional highlight)
2613   "Convert the current article date to the user-defined format.
2614 This format is defined by the `gnus-article-time-format' variable."
2615   (interactive (list t))
2616   (article-date-ut 'user highlight))
2617
2618 (defun article-date-iso8601 (&optional highlight)
2619   "Convert the current article date to ISO8601."
2620   (interactive (list t))
2621   (article-date-ut 'iso8601 highlight))
2622
2623 (defun article-show-all ()
2624   "Show all hidden text in the article buffer."
2625   (interactive)
2626   (save-excursion
2627     (widen)
2628     (let ((buffer-read-only nil))
2629       (gnus-article-unhide-text (point-min) (point-max))
2630       (gnus-remove-text-with-property 'gnus-prev)
2631       (gnus-remove-text-with-property 'gnus-next))))
2632
2633 (defun article-show-all-headers ()
2634   "Show all hidden headers in the article buffer."
2635   (interactive)
2636   (save-excursion
2637     (save-restriction
2638       (widen)
2639       (article-narrow-to-head)
2640       (let ((buffer-read-only nil))
2641         (gnus-article-unhide-text (point-min) (point-max))))))
2642
2643 (defun article-remove-leading-whitespace ()
2644   "Remove excessive whitespace from all headers."
2645   (interactive)
2646   (save-excursion
2647     (save-restriction
2648       (let ((buffer-read-only nil))
2649         (article-narrow-to-head)
2650         (goto-char (point-min))
2651         (while (re-search-forward "^[^ :]+: \\([ \t]+\\)" nil t)
2652           (delete-region (match-beginning 1) (match-end 1)))))))
2653
2654 (defun article-emphasize (&optional arg)
2655   "Emphasize text according to `gnus-emphasis-alist'."
2656   (interactive (gnus-article-hidden-arg))
2657   (unless (gnus-article-check-hidden-text 'emphasis arg)
2658     (save-excursion
2659       (let ((alist (or
2660                     (condition-case nil
2661                         (with-current-buffer gnus-summary-buffer
2662                           gnus-article-emphasis-alist)
2663                       (error))
2664                     gnus-emphasis-alist))
2665             (buffer-read-only nil)
2666             (props (append '(article-type emphasis)
2667                            gnus-hidden-properties))
2668             regexp elem beg invisible visible face)
2669         (article-goto-body)
2670         (setq beg (point))
2671         (while (setq elem (pop alist))
2672           (goto-char beg)
2673           (setq regexp (car elem)
2674                 invisible (nth 1 elem)
2675                 visible (nth 2 elem)
2676                 face (nth 3 elem))
2677           (while (re-search-forward regexp nil t)
2678             (when (and (match-beginning visible) (match-beginning invisible))
2679               (push 'emphasis gnus-article-wash-types)
2680               (gnus-article-hide-text
2681                (match-beginning invisible) (match-end invisible) props)
2682               (gnus-article-unhide-text-type
2683                (match-beginning visible) (match-end visible) 'emphasis)
2684               (gnus-put-text-property-excluding-newlines
2685                (match-beginning visible) (match-end visible) 'face face)
2686               (goto-char (match-end invisible)))))))))
2687
2688 (defun gnus-article-setup-highlight-words (&optional highlight-words)
2689   "Setup newsgroup emphasis alist."
2690   (unless gnus-article-emphasis-alist
2691     (let ((name (and gnus-newsgroup-name
2692                      (gnus-group-real-name gnus-newsgroup-name))))
2693       (make-local-variable 'gnus-article-emphasis-alist)
2694       (setq gnus-article-emphasis-alist
2695             (nconc
2696              (let ((alist gnus-group-highlight-words-alist) elem highlight)
2697                (while (setq elem (pop alist))
2698                  (when (and name (string-match (car elem) name))
2699                    (setq alist nil
2700                          highlight (copy-sequence (cdr elem)))))
2701                highlight)
2702              (copy-sequence highlight-words)
2703              (if gnus-newsgroup-name
2704                  (copy-sequence (gnus-group-find-parameter
2705                                  gnus-newsgroup-name 'highlight-words t)))
2706              gnus-emphasis-alist)))))
2707
2708 (eval-when-compile
2709   (defvar gnus-summary-article-menu)
2710   (defvar gnus-summary-post-menu))
2711
2712 ;;; Saving functions.
2713
2714 (defun gnus-article-save (save-buffer file &optional num)
2715   "Save the currently selected article."
2716   (unless gnus-save-all-headers
2717     ;; Remove headers according to `gnus-saved-headers'.
2718     (let ((gnus-visible-headers
2719            (or gnus-saved-headers gnus-visible-headers))
2720           (gnus-article-buffer save-buffer))
2721       (save-excursion
2722         (set-buffer save-buffer)
2723         (article-hide-headers 1 t))))
2724   (save-window-excursion
2725     (if (not gnus-default-article-saver)
2726         (error "No default saver is defined")
2727       ;; !!! Magic!  The saving functions all save
2728       ;; `gnus-save-article-buffer' (or so they think), but we
2729       ;; bind that variable to our save-buffer.
2730       (set-buffer gnus-article-buffer)
2731       (let* ((gnus-save-article-buffer save-buffer)
2732              (filename
2733               (cond
2734                ((not gnus-prompt-before-saving) 'default)
2735                ((eq gnus-prompt-before-saving 'always) nil)
2736                (t file)))
2737              (gnus-number-of-articles-to-be-saved
2738               (when (eq gnus-prompt-before-saving t)
2739                 num)))                  ; Magic
2740         (set-buffer gnus-article-current-summary)
2741         (funcall gnus-default-article-saver filename)))))
2742
2743 (defun gnus-read-save-file-name (prompt &optional filename
2744                                         function group headers variable)
2745   (let ((default-name
2746           (funcall function group headers (symbol-value variable)))
2747         result)
2748     (setq result
2749           (expand-file-name
2750            (cond
2751             ((eq filename 'default)
2752              default-name)
2753             ((eq filename t)
2754              default-name)
2755             (filename filename)
2756             (t
2757              (let* ((split-name (gnus-get-split-value gnus-split-methods))
2758                     (prompt
2759                      (format prompt
2760                              (if (and gnus-number-of-articles-to-be-saved
2761                                       (> gnus-number-of-articles-to-be-saved 1))
2762                                  (format "these %d articles"
2763                                          gnus-number-of-articles-to-be-saved)
2764                                "this article")))
2765                     (file
2766                      ;; Let the split methods have their say.
2767                      (cond
2768                       ;; No split name was found.
2769                       ((null split-name)
2770                        (read-file-name
2771                         (concat prompt " (default "
2772                                 (file-name-nondirectory default-name) ") ")
2773                         (file-name-directory default-name)
2774                         default-name))
2775                       ;; A single group name is returned.
2776                       ((stringp split-name)
2777                        (setq default-name
2778                              (funcall function split-name headers
2779                                       (symbol-value variable)))
2780                        (read-file-name
2781                         (concat prompt " (default "
2782                                 (file-name-nondirectory default-name) ") ")
2783                         (file-name-directory default-name)
2784                         default-name))
2785                       ;; A single split name was found
2786                       ((= 1 (length split-name))
2787                        (let* ((name (expand-file-name
2788                                      (car split-name)
2789                                      gnus-article-save-directory))
2790                               (dir (cond ((file-directory-p name)
2791                                           (file-name-as-directory name))
2792                                          ((file-exists-p name) name)
2793                                          (t gnus-article-save-directory))))
2794                          (read-file-name
2795                           (concat prompt " (default " name ") ")
2796                           dir name)))
2797                       ;; A list of splits was found.
2798                       (t
2799                        (setq split-name (nreverse split-name))
2800                        (let (result)
2801                          (let ((file-name-history
2802                                 (nconc split-name file-name-history)))
2803                            (setq result
2804                                  (expand-file-name
2805                                   (read-file-name
2806                                    (concat prompt " (`M-p' for defaults) ")
2807                                    gnus-article-save-directory
2808                                    (car split-name))
2809                                   gnus-article-save-directory)))
2810                          (car (push result file-name-history)))))))
2811                ;; Create the directory.
2812                (gnus-make-directory (file-name-directory file))
2813       ;; If we have read a directory, we append the default file name.
2814                (when (file-directory-p file)
2815                  (setq file (expand-file-name (file-name-nondirectory
2816                                                default-name)
2817                                               (file-name-as-directory file))))
2818                ;; Possibly translate some characters.
2819                (nnheader-translate-file-chars file))))))
2820     (gnus-make-directory (file-name-directory result))
2821     (set variable result)))
2822
2823 (defun gnus-article-archive-name (group)
2824   "Return the first instance of an \"Archive-name\" in the current buffer."
2825   (let ((case-fold-search t))
2826     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
2827       (nnheader-concat gnus-article-save-directory
2828                        (match-string 1)))))
2829
2830 (defun gnus-article-nndoc-name (group)
2831   "If GROUP is an nndoc group, return the name of the parent group."
2832   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
2833     (gnus-group-get-parameter group 'save-article-group)))
2834
2835 (defun gnus-summary-save-in-rmail (&optional filename)
2836   "Append this article to Rmail file.
2837 Optional argument FILENAME specifies file name.
2838 Directory to save to is default to `gnus-article-save-directory'."
2839   (setq filename (gnus-read-save-file-name
2840                   "Save %s in rmail file:" filename
2841                   gnus-rmail-save-name gnus-newsgroup-name
2842                   gnus-current-headers 'gnus-newsgroup-last-rmail))
2843   (gnus-eval-in-buffer-window gnus-save-article-buffer
2844     (save-excursion
2845       (save-restriction
2846         (widen)
2847         (gnus-output-to-rmail filename))))
2848   filename)
2849
2850 (defun gnus-summary-save-in-mail (&optional filename)
2851   "Append this article to Unix mail file.
2852 Optional argument FILENAME specifies file name.
2853 Directory to save to is default to `gnus-article-save-directory'."
2854   (setq filename (gnus-read-save-file-name
2855                   "Save %s in Unix mail file:" filename
2856                   gnus-mail-save-name gnus-newsgroup-name
2857                   gnus-current-headers 'gnus-newsgroup-last-mail))
2858   (gnus-eval-in-buffer-window gnus-save-article-buffer
2859     (save-excursion
2860       (save-restriction
2861         (widen)
2862         (if (and (file-readable-p filename)
2863                  (mail-file-babyl-p filename))
2864             (rmail-output-to-rmail-file filename t)
2865           (gnus-output-to-mail filename)))))
2866   filename)
2867
2868 (defun gnus-summary-save-in-file (&optional filename overwrite)
2869   "Append this article to file.
2870 Optional argument FILENAME specifies file name.
2871 Directory to save to is default to `gnus-article-save-directory'."
2872   (setq filename (gnus-read-save-file-name
2873                   "Save %s in file:" filename
2874                   gnus-file-save-name gnus-newsgroup-name
2875                   gnus-current-headers 'gnus-newsgroup-last-file))
2876   (gnus-eval-in-buffer-window gnus-save-article-buffer
2877     (save-excursion
2878       (save-restriction
2879         (widen)
2880         (when (and overwrite
2881                    (file-exists-p filename))
2882           (delete-file filename))
2883         (gnus-output-to-file filename))))
2884   filename)
2885
2886 (defun gnus-summary-write-to-file (&optional filename)
2887   "Write this article to a file.
2888 Optional argument FILENAME specifies file name.
2889 The directory to save in defaults to `gnus-article-save-directory'."
2890   (gnus-summary-save-in-file nil t))
2891
2892 (defun gnus-summary-save-body-in-file (&optional filename)
2893   "Append this article body to a file.
2894 Optional argument FILENAME specifies file name.
2895 The directory to save in defaults to `gnus-article-save-directory'."
2896   (setq filename (gnus-read-save-file-name
2897                   "Save %s body in file:" filename
2898                   gnus-file-save-name gnus-newsgroup-name
2899                   gnus-current-headers 'gnus-newsgroup-last-file))
2900   (gnus-eval-in-buffer-window gnus-save-article-buffer
2901     (save-excursion
2902       (save-restriction
2903         (widen)
2904         (when (article-goto-body)
2905           (narrow-to-region (point) (point-max)))
2906         (gnus-output-to-file filename))))
2907   filename)
2908
2909 (defun gnus-summary-save-in-pipe (&optional command)
2910   "Pipe this article to subprocess."
2911   (setq command
2912         (cond ((and (eq command 'default)
2913                     gnus-last-shell-command)
2914                gnus-last-shell-command)
2915               ((stringp command)
2916                command)
2917               (t (read-string
2918                   (format
2919                    "Shell command on %s: "
2920                    (if (and gnus-number-of-articles-to-be-saved
2921                             (> gnus-number-of-articles-to-be-saved 1))
2922                        (format "these %d articles"
2923                                gnus-number-of-articles-to-be-saved)
2924                      "this article"))
2925                   gnus-last-shell-command))))
2926   (when (string-equal command "")
2927     (if gnus-last-shell-command
2928         (setq command gnus-last-shell-command)
2929       (error "A command is required.")))
2930   (gnus-eval-in-buffer-window gnus-article-buffer
2931     (save-restriction
2932       (widen)
2933       (shell-command-on-region (point-min) (point-max) command nil)))
2934   (setq gnus-last-shell-command command))
2935
2936 ;;; Article file names when saving.
2937
2938 (defun gnus-capitalize-newsgroup (newsgroup)
2939   "Capitalize NEWSGROUP name."
2940   (when (not (zerop (length newsgroup)))
2941     (concat (char-to-string (upcase (aref newsgroup 0)))
2942             (substring newsgroup 1))))
2943
2944 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2945   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2946 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
2947 Otherwise, it is like ~/News/news/group/num."
2948   (let ((default
2949           (expand-file-name
2950            (concat (if (gnus-use-long-file-name 'not-save)
2951                        (gnus-capitalize-newsgroup newsgroup)
2952                      (gnus-newsgroup-directory-form newsgroup))
2953                    "/" (int-to-string (mail-header-number headers)))
2954            gnus-article-save-directory)))
2955     (if (and last-file
2956              (string-equal (file-name-directory default)
2957                            (file-name-directory last-file))
2958              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2959         default
2960       (or last-file default))))
2961
2962 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2963   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2964 If variable `gnus-use-long-file-name' is non-nil, it is
2965 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2966   (let ((default
2967           (expand-file-name
2968            (concat (if (gnus-use-long-file-name 'not-save)
2969                        newsgroup
2970                      (gnus-newsgroup-directory-form newsgroup))
2971                    "/" (int-to-string (mail-header-number headers)))
2972            gnus-article-save-directory)))
2973     (if (and last-file
2974              (string-equal (file-name-directory default)
2975                            (file-name-directory last-file))
2976              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2977         default
2978       (or last-file default))))
2979
2980 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2981   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2982 If variable `gnus-use-long-file-name' is non-nil, it is
2983 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2984   (or last-file
2985       (expand-file-name
2986        (if (gnus-use-long-file-name 'not-save)
2987            newsgroup
2988          (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup)))
2989        gnus-article-save-directory)))
2990
2991 (defun gnus-sender-save-name (newsgroup headers &optional last-file)
2992   "Generate file name from sender."
2993   (let ((from (mail-header-from headers)))
2994     (expand-file-name
2995      (if (and from (string-match "\\([^ <]+\\)@" from))
2996          (match-string 1 from)
2997        "nobody")
2998      gnus-article-save-directory)))
2999
3000 (defun article-verify-x-pgp-sig ()
3001   "Verify X-PGP-Sig."
3002   (interactive)
3003   (if (gnus-buffer-live-p gnus-original-article-buffer)
3004       (let ((sig (with-current-buffer gnus-original-article-buffer
3005                    (gnus-fetch-field "X-PGP-Sig")))
3006             items info headers)
3007         (when (and sig
3008                    mml2015-use
3009                    (mml2015-clear-verify-function))
3010           (with-temp-buffer
3011             (insert-buffer gnus-original-article-buffer)
3012             (setq items (split-string sig))
3013             (message-narrow-to-head)
3014             (let ((inhibit-point-motion-hooks t)
3015                   (case-fold-search t))
3016               ;; Don't verify multiple headers.
3017               (setq headers (mapconcat (lambda (header)
3018                                          (concat header ": "
3019                                                  (mail-fetch-field header) "\n"))
3020                                        (split-string (nth 1 items) ",") "")))
3021             (delete-region (point-min) (point-max))
3022             (insert "-----BEGIN PGP SIGNED MESSAGE-----\n\n")
3023             (insert "X-Signed-Headers: " (nth 1 items) "\n")
3024             (insert headers)
3025             (widen)
3026             (forward-line)
3027             (while (not (eobp))
3028               (if (looking-at "^-")
3029                   (insert "- "))
3030               (forward-line))
3031             (insert "\n-----BEGIN PGP SIGNATURE-----\n")
3032             (insert "Version: " (car items) "\n\n")
3033             (insert (mapconcat 'identity (cddr items) "\n"))
3034             (insert "\n-----END PGP SIGNATURE-----\n")
3035             (let ((mm-security-handle (list (format "multipart/signed"))))
3036               (mml2015-clean-buffer)
3037               (let ((coding-system-for-write (or gnus-newsgroup-charset
3038                                                  'iso-8859-1)))
3039                 (funcall (mml2015-clear-verify-function)))
3040               (setq info
3041                     (or (mm-handle-multipart-ctl-parameter
3042                          mm-security-handle 'gnus-details)
3043                         (mm-handle-multipart-ctl-parameter
3044                          mm-security-handle 'gnus-info)))))
3045           (when info
3046             (let (buffer-read-only bface eface)
3047               (save-restriction
3048                 (message-narrow-to-head)
3049                 (goto-char (point-max))
3050                 (forward-line -1)
3051                 (setq bface (get-text-property (gnus-point-at-bol) 'face)
3052                       eface (get-text-property (1- (gnus-point-at-eol)) 'face))
3053                 (message-remove-header "X-Gnus-PGP-Verify")
3054                 (if (re-search-forward "^X-PGP-Sig:" nil t)
3055                     (forward-line)
3056                   (goto-char (point-max)))
3057                 (narrow-to-region (point) (point))
3058                 (insert "X-Gnus-PGP-Verify: " info "\n")
3059                 (goto-char (point-min))
3060                 (forward-line)
3061                 (while (not (eobp))
3062                   (if (not (looking-at "^[ \t]"))
3063                       (insert " "))
3064                   (forward-line))
3065                 ;; Do highlighting.
3066                 (goto-char (point-min))
3067                 (when (looking-at "\\([^:]+\\): *")
3068                   (put-text-property (match-beginning 1) (1+ (match-end 1))
3069                                      'face bface)
3070                   (put-text-property (match-end 0) (point-max)
3071                                      'face eface)))))))))
3072
3073 (eval-and-compile
3074   (mapcar
3075    (lambda (func)
3076      (let (afunc gfunc)
3077        (if (consp func)
3078            (setq afunc (car func)
3079                  gfunc (cdr func))
3080          (setq afunc func
3081                gfunc (intern (format "gnus-%s" func))))
3082        (defalias gfunc
3083          (if (fboundp afunc)
3084            `(lambda (&optional interactive &rest args)
3085               ,(documentation afunc t)
3086               (interactive (list t))
3087               (save-excursion
3088                 (set-buffer gnus-article-buffer)
3089                 (if interactive
3090                     (call-interactively ',afunc)
3091                   (apply ',afunc args))))))))
3092    '(article-hide-headers
3093      article-verify-x-pgp-sig
3094      article-hide-boring-headers
3095      article-toggle-headers
3096      article-treat-overstrike
3097      article-fill-long-lines
3098      article-capitalize-sentences
3099      article-remove-cr
3100      article-remove-leading-whitespace
3101      article-display-x-face
3102      article-de-quoted-unreadable
3103      article-de-base64-unreadable
3104      article-decode-HZ
3105      article-wash-html
3106      article-hide-list-identifiers
3107      article-hide-pgp
3108      article-strip-banner
3109      article-babel
3110      article-hide-pem
3111      article-hide-signature
3112      article-strip-headers-in-body
3113      article-remove-trailing-blank-lines
3114      article-strip-leading-blank-lines
3115      article-strip-multiple-blank-lines
3116      article-strip-leading-space
3117      article-strip-trailing-space
3118      article-strip-blank-lines
3119      article-strip-all-blank-lines
3120      article-replace-with-quoted-text
3121      article-date-local
3122      article-date-english
3123      article-date-iso8601
3124      article-date-original
3125      article-date-ut
3126      article-decode-mime-words
3127      article-decode-charset
3128      article-decode-encoded-words
3129      article-date-user
3130      article-date-lapsed
3131      article-emphasize
3132      article-treat-dumbquotes
3133      article-normalize-headers
3134      (article-show-all-headers . gnus-article-show-all-headers)
3135      (article-show-all . gnus-article-show-all))))
3136 \f
3137 ;;;
3138 ;;; Gnus article mode
3139 ;;;
3140
3141 (put 'gnus-article-mode 'mode-class 'special)
3142
3143 (gnus-define-keys gnus-article-mode-map
3144   " " gnus-article-goto-next-page
3145   "\177" gnus-article-goto-prev-page
3146   [delete] gnus-article-goto-prev-page
3147   [backspace] gnus-article-goto-prev-page
3148   "\C-c^" gnus-article-refer-article
3149   "h" gnus-article-show-summary
3150   "s" gnus-article-show-summary
3151   "\C-c\C-m" gnus-article-mail
3152   "?" gnus-article-describe-briefly
3153   "e" gnus-summary-edit-article
3154   "<" beginning-of-buffer
3155   ">" end-of-buffer
3156   "\C-c\C-i" gnus-info-find-node
3157   "\C-c\C-b" gnus-bug
3158   "\C-hk" gnus-article-describe-key
3159   "\C-hc" gnus-article-describe-key-briefly
3160
3161   "\C-d" gnus-article-read-summary-keys
3162   "\M-*" gnus-article-read-summary-keys
3163   "\M-#" gnus-article-read-summary-keys
3164   "\M-^" gnus-article-read-summary-keys
3165   "\M-g" gnus-article-read-summary-keys)
3166
3167 ;; Define almost undefined keys to `gnus-article-read-summary-keys'.
3168 (let (keys)
3169   (let ((key 32))
3170     (while (<= key 127)
3171       (push (char-to-string key) keys)
3172       (incf key))
3173     (dolist (elem (accessible-keymaps gnus-summary-mode-map))
3174       (setq key (car elem))
3175       (when (and (> (length key) 0)
3176                  (not (eq 'menu-bar (aref key 0)))
3177                  (symbolp (lookup-key gnus-summary-mode-map key)))
3178         (push key keys))))
3179   (dolist (key keys)
3180     (unless (lookup-key gnus-article-mode-map key)
3181       (define-key gnus-article-mode-map key 'gnus-article-read-summary-keys))))
3182
3183 (defun gnus-article-make-menu-bar ()
3184   (unless (boundp 'gnus-article-commands-menu)
3185     (gnus-summary-make-menu-bar))
3186   (gnus-turn-off-edit-menu 'article)
3187   (unless (boundp 'gnus-article-article-menu)
3188     (easy-menu-define
3189      gnus-article-article-menu gnus-article-mode-map ""
3190      '("Article"
3191        ["Scroll forwards" gnus-article-goto-next-page t]
3192        ["Scroll backwards" gnus-article-goto-prev-page t]
3193        ["Show summary" gnus-article-show-summary t]
3194        ["Fetch Message-ID at point" gnus-article-refer-article t]
3195        ["Mail to address at point" gnus-article-mail t]
3196        ["Send a bug report" gnus-bug t]))
3197
3198     (easy-menu-define
3199      gnus-article-treatment-menu gnus-article-mode-map ""
3200      ;; Fixme: this should use :active (and maybe :visible).
3201      '("Treatment"
3202        ["Hide headers" gnus-article-toggle-headers t]
3203        ["Hide signature" gnus-article-hide-signature t]
3204        ["Hide citation" gnus-article-hide-citation t]
3205        ["Treat overstrike" gnus-article-treat-overstrike t]
3206        ["Remove carriage return" gnus-article-remove-cr t]
3207        ["Remove leading whitespace" gnus-article-remove-leading-whitespace t]
3208        ["Decode HZ" gnus-article-decode-HZ t]))
3209
3210     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
3211
3212     ;; Note "Post" menu is defined in gnus-sum.el for consistency
3213
3214     (gnus-run-hooks 'gnus-article-menu-hook)))
3215
3216 ;; Fixme: do something for the Emacs tool bar in Article mode a la
3217 ;; Summary.
3218
3219 (defun gnus-article-mode ()
3220   "Major mode for displaying an article.
3221
3222 All normal editing commands are switched off.
3223
3224 The following commands are available in addition to all summary mode
3225 commands:
3226 \\<gnus-article-mode-map>
3227 \\[gnus-article-next-page]\t Scroll the article one page forwards
3228 \\[gnus-article-prev-page]\t Scroll the article one page backwards
3229 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
3230 \\[gnus-article-show-summary]\t Display the summary buffer
3231 \\[gnus-article-mail]\t Send a reply to the address near point
3232 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
3233 \\[gnus-info-find-node]\t Go to the Gnus info node"
3234   (interactive)
3235   (gnus-simplify-mode-line)
3236   (setq mode-name "Article")
3237   (setq major-mode 'gnus-article-mode)
3238   (make-local-variable 'minor-mode-alist)
3239   (unless (assq 'gnus-show-mime minor-mode-alist)
3240     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
3241   (use-local-map gnus-article-mode-map)
3242   (when (gnus-visual-p 'article-menu 'menu)
3243     (gnus-article-make-menu-bar))
3244   (gnus-update-format-specifications nil 'article-mode)
3245   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
3246   (make-local-variable 'gnus-page-broken)
3247   (make-local-variable 'gnus-button-marker-list)
3248   (make-local-variable 'gnus-article-current-summary)
3249   (make-local-variable 'gnus-article-mime-handles)
3250   (make-local-variable 'gnus-article-decoded-p)
3251   (make-local-variable 'gnus-article-mime-handle-alist)
3252   (make-local-variable 'gnus-article-wash-types)
3253   (make-local-variable 'gnus-article-charset)
3254   (make-local-variable 'gnus-article-ignored-charsets)
3255   (gnus-set-default-directory)
3256   (buffer-disable-undo)
3257   (setq buffer-read-only t)
3258   (set-syntax-table gnus-article-mode-syntax-table)
3259   (gnus-run-hooks 'gnus-article-mode-hook))
3260
3261 (defun gnus-article-setup-buffer ()
3262   "Initialize the article buffer."
3263   (let* ((name (if gnus-single-article-buffer "*Article*"
3264                  (concat "*Article " gnus-newsgroup-name "*")))
3265          (original
3266           (progn (string-match "\\*Article" name)
3267                  (concat " *Original Article"
3268                          (substring name (match-end 0))))))
3269     (setq gnus-article-buffer name)
3270     (setq gnus-original-article-buffer original)
3271     (setq gnus-article-mime-handle-alist nil)
3272     ;; This might be a variable local to the summary buffer.
3273     (unless gnus-single-article-buffer
3274       (save-excursion
3275         (set-buffer gnus-summary-buffer)
3276         (setq gnus-article-buffer name)
3277         (setq gnus-original-article-buffer original)
3278         (gnus-set-global-variables)))
3279     (gnus-article-setup-highlight-words)
3280     ;; Init original article buffer.
3281     (save-excursion
3282       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3283       (set-buffer-multibyte nil)
3284       (setq major-mode 'gnus-original-article-mode)
3285       (make-local-variable 'gnus-original-article))
3286     (if (get-buffer name)
3287         (save-excursion
3288           (set-buffer name)
3289           (buffer-disable-undo)
3290           (setq buffer-read-only t)
3291           (unless (eq major-mode 'gnus-article-mode)
3292             (gnus-article-mode))
3293           (current-buffer))
3294       (save-excursion
3295         (set-buffer (gnus-get-buffer-create name))
3296         (gnus-article-mode)
3297         (make-local-variable 'gnus-summary-buffer)
3298         (gnus-summary-set-local-parameters gnus-newsgroup-name)
3299         (current-buffer)))))
3300
3301 ;; Set article window start at LINE, where LINE is the number of lines
3302 ;; from the head of the article.
3303 (defun gnus-article-set-window-start (&optional line)
3304   (set-window-start
3305    (get-buffer-window gnus-article-buffer t)
3306    (save-excursion
3307      (set-buffer gnus-article-buffer)
3308      (goto-char (point-min))
3309      (if (not line)
3310          (point-min)
3311        (gnus-message 6 "Moved to bookmark")
3312        (search-forward "\n\n" nil t)
3313        (forward-line line)
3314        (point)))))
3315
3316 ;;; @@ article filters
3317 ;;;
3318
3319 (defun gnus-article-display-mime-message ()
3320   "Article display method for MIME message."
3321   ;; called from `gnus-original-article-buffer'.
3322   (let (charset all-headers)
3323     (with-current-buffer gnus-summary-buffer
3324       (setq charset default-mime-charset
3325             all-headers gnus-have-all-headers))
3326     (make-local-variable 'default-mime-charset)
3327     (setq default-mime-charset charset)
3328     (with-current-buffer (get-buffer-create gnus-article-buffer)
3329       (make-local-variable 'default-mime-charset)
3330       (setq default-mime-charset charset))
3331     (mime-display-message mime-message-structure
3332                           gnus-article-buffer nil gnus-article-mode-map)
3333     (when all-headers
3334       (gnus-article-hide-headers nil -1)))
3335   (run-hooks 'gnus-mime-article-prepare-hook))
3336
3337 (defun gnus-article-display-traditional-message ()
3338   "Article display method for traditional message."
3339   (set-buffer gnus-article-buffer)
3340   (let (buffer-read-only)
3341     (erase-buffer)
3342     (insert-buffer-substring gnus-original-article-buffer)))
3343
3344 (defun gnus-article-make-full-mail-header (&optional number charset)
3345   "Create a new mail header structure in a raw article buffer."
3346   (unless (and number charset)
3347     (save-current-buffer
3348       (set-buffer gnus-summary-buffer)
3349       (unless number
3350         (setq number (or (cdr gnus-article-current) 0)))
3351       (unless charset
3352         (setq charset (or default-mime-charset 'x-ctext)))))
3353   (goto-char (point-min))
3354   (let ((header-end (if (search-forward "\n\n" nil t)
3355                         (1- (point))
3356                       (goto-char (point-max))))
3357         (chars (- (point-max) (point)))
3358         (lines (count-lines (point) (point-max)))
3359         (default-mime-charset charset)
3360         xref)
3361     (narrow-to-region (point-min) header-end)
3362     (setq xref (std11-fetch-field "xref"))
3363     (prog1
3364         (make-full-mail-header
3365          number
3366          (std11-fetch-field "subject")
3367          (std11-fetch-field "from")
3368          (std11-fetch-field "date")
3369          (std11-fetch-field "message-id")
3370          (std11-fetch-field "references")
3371          chars
3372          lines
3373          (when xref (concat "Xref: " xref)))
3374       (widen))))
3375
3376 (defun gnus-article-prepare (article &optional all-headers header)
3377   "Prepare ARTICLE in article mode buffer.
3378 ARTICLE should either be an article number or a Message-ID.
3379 If ARTICLE is an id, HEADER should be the article headers.
3380 If ALL-HEADERS is non-nil, no headers are hidden."
3381   (save-excursion
3382     ;; Make sure we start in a summary buffer.
3383     (unless (eq major-mode 'gnus-summary-mode)
3384       (set-buffer gnus-summary-buffer))
3385     (setq gnus-summary-buffer (current-buffer))
3386     (let* ((gnus-article (if header (mail-header-number header) article))
3387            (summary-buffer (current-buffer))
3388            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
3389            (group gnus-newsgroup-name)
3390            result)
3391       (save-excursion
3392         (gnus-article-setup-buffer)
3393         (set-buffer gnus-original-article-buffer)
3394         ;; Deactivate active regions.
3395         (when (and (boundp 'transient-mark-mode)
3396                    transient-mark-mode)
3397           (setq mark-active nil))
3398         (if (not (setq result (let ((buffer-read-only nil))
3399                                 (gnus-request-article-this-buffer
3400                                  article group))))
3401             ;; There is no such article.
3402             (save-excursion
3403               (when (and (numberp article)
3404                          (not (memq article gnus-newsgroup-sparse)))
3405                 (setq gnus-article-current
3406                       (cons gnus-newsgroup-name article))
3407                 (set-buffer gnus-summary-buffer)
3408                 (setq gnus-current-article article)
3409                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
3410                     (progn
3411                       (gnus-summary-set-agent-mark article)
3412                       (message "Message marked for downloading"))
3413                   (gnus-summary-mark-article article gnus-canceled-mark)
3414                   (unless (memq article gnus-newsgroup-sparse)
3415                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
3416           (if (or (eq result 'pseudo)
3417                   (eq result 'nneething))
3418               (progn
3419                 (save-excursion
3420                   (set-buffer summary-buffer)
3421                   (push article gnus-newsgroup-history)
3422                   (setq gnus-last-article gnus-current-article
3423                         gnus-current-article 0
3424                         gnus-current-headers nil
3425                         gnus-article-current nil)
3426                   (if (eq result 'nneething)
3427                       (gnus-configure-windows 'summary)
3428                     (gnus-configure-windows 'article))
3429                   (gnus-set-global-variables))
3430                 (let ((gnus-article-mime-handle-alist-1
3431                        gnus-article-mime-handle-alist))
3432                   (gnus-set-mode-line 'article)))
3433             ;; The result from the `request' was an actual article -
3434             ;; or at least some text that is now displayed in the
3435             ;; article buffer.
3436             (when (and (numberp article)
3437                        (not (eq article gnus-current-article)))
3438               ;; Seems like a new article has been selected.
3439               ;; `gnus-current-article' must be an article number.
3440               (save-excursion
3441                 (set-buffer summary-buffer)
3442                 (push article gnus-newsgroup-history)
3443                 (setq gnus-last-article gnus-current-article
3444                       gnus-current-article article
3445                       gnus-current-headers
3446                       (gnus-summary-article-header gnus-current-article)
3447                       gnus-article-current
3448                       (cons gnus-newsgroup-name gnus-current-article))
3449                 (unless (vectorp gnus-current-headers)
3450                   (setq gnus-current-headers nil))
3451                 (gnus-summary-goto-subject gnus-current-article)
3452                 (when (gnus-summary-show-thread)
3453                   ;; If the summary buffer really was folded, the
3454                   ;; previous goto may not actually have gone to
3455                   ;; the right article, but the thread root instead.
3456                   ;; So we go again.
3457                   (gnus-summary-goto-subject gnus-current-article))
3458                 (gnus-run-hooks 'gnus-mark-article-hook)
3459                 (gnus-set-mode-line 'summary)
3460                 (when (gnus-visual-p 'article-highlight 'highlight)
3461                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
3462                 ;; Set the global newsgroup variables here.
3463                 (gnus-set-global-variables)
3464                 (setq gnus-have-all-headers
3465                       (or all-headers gnus-show-all-headers))))
3466             (save-excursion
3467               (gnus-configure-windows 'article))
3468             (when (or (numberp article)
3469                       (stringp article))
3470               (gnus-article-prepare-display)
3471               ;; Do page break.
3472               (goto-char (point-min))
3473               (setq gnus-page-broken
3474                     (when gnus-break-pages
3475                       (gnus-narrow-to-page)
3476                       t)))
3477             (let ((gnus-article-mime-handle-alist-1
3478                    gnus-article-mime-handle-alist))
3479               (gnus-set-mode-line 'article))
3480             (article-goto-body)
3481             (set-window-point (get-buffer-window (current-buffer)) (point))
3482             (gnus-configure-windows 'article)
3483             t))))))
3484
3485 (defun gnus-article-prepare-mime-display (&optional number)
3486   (goto-char (point-min))
3487   (when (re-search-forward "^[^\t ]+:" nil t)
3488     (goto-char (match-beginning 0)))
3489   (let ((entity (if (eq 1 (point-min))
3490                     (get-text-property 1 'mime-view-entity)
3491                   (get-text-property (point) 'mime-view-entity)))
3492         last-entity child-entity next type)
3493     (setq child-entity (mime-entity-children entity))
3494     (if child-entity
3495         (setq last-entity (nth (1- (length child-entity))
3496                                child-entity))
3497       (setq last-entity entity))
3498     (save-restriction
3499       (narrow-to-region (point)
3500                         (if (search-forward "\n\n" nil t)
3501                             (point)
3502                           (point-max)))
3503       (gnus-treat-article 'head)
3504       (put-text-property (point-min) (point-max) 'article-treated-header t)
3505       (goto-char (point-max)))
3506     (while (and (not (eobp)) entity)
3507       (setq next (set-marker
3508                   (make-marker)
3509                   (next-single-property-change (point) 'mime-view-entity
3510                                                nil (point-max))))
3511       (let ((types (mime-entity-content-type entity)))
3512         (while (eq 'multipart (mime-content-type-primary-type types))
3513           (setq entity (car (mime-entity-children entity))
3514                 types (mime-entity-content-type entity)))
3515         (when types
3516           (setq type (format "%s/%s"
3517                              (mime-content-type-primary-type types)
3518                              (mime-content-type-subtype types)))))
3519       (if (string-equal type "message/rfc822")
3520           (progn
3521             (setq next (point))
3522             (let ((children (mime-entity-children entity))
3523                   last-children)
3524               (when children
3525                 (setq last-children (nth (1- (length children)) children))
3526                 (while
3527                     (and
3528                      (not (eq last-children
3529                               (get-text-property next 'mime-view-entity)))
3530                      (setq next
3531                            (next-single-property-change next
3532                                                         'mime-view-entity
3533                                                         nil (point-max)))))))
3534             (setq next (next-single-property-change next 'mime-view-entity
3535                                                     nil (point-max)))
3536             (save-restriction
3537               (narrow-to-region (point) next)
3538               (gnus-article-prepare-mime-display)
3539               (goto-char (point-max)))
3540             (setq entity (get-text-property (point) 'mime-view-entity)))
3541         (save-restriction
3542           (narrow-to-region (point) next)
3543           ;; Kludge. We have to count true number, but for now,
3544           ;; part number is here only to achieve `last'.
3545           (gnus-treat-article nil 1
3546                               (if (eq entity last-entity)
3547                                   1 2)
3548                               type)
3549           (goto-char (point-max)))
3550         (setq entity (get-text-property next 'mime-view-entity))))))
3551
3552 ;;;###autoload
3553 (defun gnus-article-prepare-display ()
3554   "Make the current buffer look like a nice article."
3555   (setq gnus-article-wash-types nil)
3556   (gnus-run-hooks 'gnus-tmp-internal-hook)
3557   ;; Display message.
3558   (setq mime-message-structure gnus-current-headers)
3559   (mime-buffer-entity-set-buffer-internal mime-message-structure
3560                                           gnus-original-article-buffer)
3561   (mime-entity-set-representation-type-internal mime-message-structure
3562                                                 'mime-buffer-entity)
3563   (luna-send mime-message-structure 'initialize-instance
3564              mime-message-structure)
3565   (if gnus-show-mime
3566       (let (mime-display-header-hook mime-display-text/plain-hook)
3567         (funcall gnus-article-display-method-for-mime))
3568     (funcall gnus-article-display-method-for-traditional))
3569   ;; Associate this article with the current summary buffer.
3570   (setq gnus-article-current-summary gnus-summary-buffer)
3571   ;; Call the treatment functions.
3572   (let ((inhibit-read-only t)
3573         buffer-read-only)
3574     (save-restriction
3575       (widen)
3576       (if gnus-show-mime
3577           (gnus-article-prepare-mime-display)
3578         (narrow-to-region (goto-char (point-min))
3579                           (if (search-forward "\n\n" nil t)
3580                               (point)
3581                             (point-max)))
3582         (gnus-treat-article 'head)
3583         (put-text-property (point-min) (point-max) 'article-treated-header t)
3584         (goto-char (point-max))
3585         (widen)
3586         (narrow-to-region (point) (point-max))
3587         (gnus-treat-article nil))
3588       (put-text-property (point-min) (point-max) 'read-only nil)))
3589   (gnus-run-hooks 'gnus-article-prepare-hook))
3590
3591 (defun gnus-article-decode-article-as-default-mime-charset ()
3592   "Decode an article as `default-mime-charset'.  It won't work if the
3593 value of the variable `gnus-show-mime' is non-nil."
3594   (unless gnus-show-mime
3595     (set (make-local-variable 'default-mime-charset)
3596          (with-current-buffer gnus-summary-buffer
3597            default-mime-charset))
3598     (decode-mime-charset-region (point-min) (point-max)
3599                                 default-mime-charset)))
3600
3601 ;;;
3602 ;;; Gnus MIME viewing functions
3603 ;;;
3604
3605 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
3606   "The following specs can be used:
3607 %t  The MIME type
3608 %T  MIME type, along with additional info
3609 %n  The `name' parameter
3610 %d  The description, if any
3611 %l  The length of the encoded part
3612 %p  The part identifier number
3613 %e  Dots if the part isn't displayed")
3614
3615 (defvar gnus-mime-button-line-format-alist
3616   '((?t gnus-tmp-type ?s)
3617     (?T gnus-tmp-type-long ?s)
3618     (?n gnus-tmp-name ?s)
3619     (?d gnus-tmp-description ?s)
3620     (?p gnus-tmp-id ?s)
3621     (?l gnus-tmp-length ?d)
3622     (?e gnus-tmp-dots ?s)))
3623
3624 (defvar gnus-mime-button-commands
3625   '((gnus-article-press-button "\r" "Toggle Display")
3626     (gnus-mime-view-part "v" "View Interactively...")
3627     (gnus-mime-view-part-as-type "t" "View As Type...")
3628     (gnus-mime-view-part-as-charset "C" "View As charset...")
3629     (gnus-mime-save-part "o" "Save...")
3630     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
3631     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
3632     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
3633     (gnus-mime-internalize-part "E" "View Internally")
3634     (gnus-mime-externalize-part "e" "View Externally")
3635     (gnus-mime-pipe-part "|" "Pipe To Command...")
3636     (gnus-mime-action-on-part "." "Take action on the part")))
3637
3638 (defun gnus-article-mime-part-status ()
3639   (with-current-buffer gnus-article-buffer
3640     (let ((entity (get-text-property (point-min) 'mime-view-entity))
3641           children)
3642       (if (and entity
3643                (setq children (mime-entity-children entity))
3644                (setq children (length children)))
3645           (if (eq 1 children)
3646               " (1 part)"
3647             (format " (%d parts)" children))
3648         ""))))
3649
3650 (defvar gnus-mime-button-map
3651   (let ((map (make-sparse-keymap)))
3652     (unless (>= (string-to-number emacs-version) 21)
3653       ;; XEmacs doesn't care.
3654       (set-keymap-parent map gnus-article-mode-map))
3655     (define-key map gnus-mouse-2 'gnus-article-push-button)
3656     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
3657     (dolist (c gnus-mime-button-commands)
3658       (define-key map (cadr c) (car c)))
3659     map))
3660
3661 (defun gnus-mime-button-menu (event)
3662   "Construct a context-sensitive menu of MIME commands."
3663   (interactive "e")
3664   (save-window-excursion
3665     (let ((pos (event-start event)))
3666       (select-window (posn-window pos))
3667       (goto-char (posn-point pos))
3668       (gnus-article-check-buffer)
3669       (let ((response (x-popup-menu
3670                        t `("MIME Part"
3671                            ("" ,@(mapcar (lambda (c)
3672                                            (cons (caddr c) (car c)))
3673                                          gnus-mime-button-commands))))))
3674         (if response
3675             (call-interactively response))))))
3676
3677 (defun gnus-mime-view-all-parts (&optional handles)
3678   "View all the MIME parts."
3679   (interactive)
3680   (save-current-buffer
3681     (set-buffer gnus-article-buffer)
3682     (let ((handles (or handles gnus-article-mime-handles))
3683           (mail-parse-charset gnus-newsgroup-charset)
3684           (mail-parse-ignored-charsets
3685            (with-current-buffer gnus-summary-buffer
3686              gnus-newsgroup-ignored-charsets)))
3687       (when handles
3688         (mm-remove-parts handles)
3689         (goto-char (point-min))
3690         (or (search-forward "\n\n") (goto-char (point-max)))
3691         (let (buffer-read-only)
3692           (delete-region (point) (point-max)))
3693         (mm-display-parts handles)))))
3694
3695 (defun gnus-mime-save-part-and-strip ()
3696   "Save the MIME part under point then replace it with an external body."
3697   (interactive)
3698   (gnus-article-check-buffer)
3699   (let* ((data (get-text-property (point) 'gnus-data))
3700          file param)
3701     (if (mm-multiple-handles gnus-article-mime-handles)
3702         (error "This function is not implemented."))
3703     (setq file (and data (mm-save-part data)))
3704     (when file
3705       (with-current-buffer (mm-handle-buffer data)
3706         (erase-buffer)
3707         (insert "Content-Type: " (mm-handle-media-type data))
3708         (mml-insert-parameter-string (cdr (mm-handle-type data))
3709                                      '(charset))
3710         (insert "\n")
3711         (insert "Content-ID: " (message-make-message-id) "\n")
3712         (insert "Content-Transfer-Encoding: binary\n")
3713         (insert "\n"))
3714       (setcdr data
3715               (cdr (mm-make-handle nil
3716                                    `("message/external-body"
3717                                      (access-type . "LOCAL-FILE")
3718                                      (name . ,file)))))
3719       (set-buffer gnus-summary-buffer)
3720       (gnus-article-edit-article
3721        `(lambda ()
3722            (erase-buffer)
3723            (let ((mail-parse-charset (or gnus-article-charset
3724                                          ',gnus-newsgroup-charset))
3725                  (mail-parse-ignored-charsets
3726                   (or gnus-article-ignored-charsets
3727                       ',gnus-newsgroup-ignored-charsets))
3728                  (mbl mml-buffer-list))
3729              (setq mml-buffer-list nil)
3730              (insert-buffer gnus-original-article-buffer)
3731              (mime-to-mml gnus-article-mime-handles)
3732              (setq gnus-article-mime-handles nil)
3733              (make-local-hook 'kill-buffer-hook)
3734              (let ((mbl1 mml-buffer-list))
3735                (setq mml-buffer-list mbl)
3736                (set (make-local-variable 'mml-buffer-list) mbl1))
3737              (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
3738        `(lambda (no-highlight)
3739           (let ((mail-parse-charset (or gnus-article-charset
3740                                         ',gnus-newsgroup-charset))
3741                 (message-options message-options)
3742                 (message-options-set-recipient)
3743                 (mail-parse-ignored-charsets
3744                  (or gnus-article-ignored-charsets
3745                      ',gnus-newsgroup-ignored-charsets)))
3746            (mml-to-mime)
3747            (mml-destroy-buffers)
3748            (remove-hook 'kill-buffer-hook
3749                         'mml-destroy-buffers t)
3750            (kill-local-variable 'mml-buffer-list))
3751           (gnus-summary-edit-article-done
3752            ,(or (mail-header-references gnus-current-headers) "")
3753            ,(gnus-group-read-only-p)
3754            ,gnus-summary-buffer no-highlight))))))
3755
3756 (defun gnus-mime-save-part ()
3757   "Save the MIME part under point."
3758   (interactive)
3759   (gnus-article-check-buffer)
3760   (let ((data (get-text-property (point) 'gnus-data)))
3761     (when data
3762       (mm-save-part data))))
3763
3764 (defun gnus-mime-pipe-part ()
3765   "Pipe the MIME part under point to a process."
3766   (interactive)
3767   (gnus-article-check-buffer)
3768   (let ((data (get-text-property (point) 'gnus-data)))
3769     (when data
3770       (mm-pipe-part data))))
3771
3772 (defun gnus-mime-view-part ()
3773   "Interactively choose a viewing method for the MIME part under point."
3774   (interactive)
3775   (gnus-article-check-buffer)
3776   (let ((data (get-text-property (point) 'gnus-data)))
3777     (when data
3778       (setq gnus-article-mime-handles
3779             (mm-merge-handles
3780              gnus-article-mime-handles (setq data (copy-sequence data))))
3781       (mm-interactively-view-part data))))
3782
3783 (defun gnus-mime-view-part-as-type-internal ()
3784   (gnus-article-check-buffer)
3785   (let* ((name (mail-content-type-get
3786                 (mm-handle-type (get-text-property (point) 'gnus-data))
3787                 'name))
3788          (def-type (and name (mm-default-file-encoding name))))
3789     (and def-type (cons def-type 0))))
3790
3791 (defun gnus-mime-view-part-as-type (&optional mime-type)
3792   "Choose a MIME media type, and view the part as such."
3793   (interactive)
3794   (unless mime-type
3795     (setq mime-type (completing-read
3796                      "View as MIME type: "
3797                      (mapcar #'list (mailcap-mime-types))
3798                      nil nil
3799                      (gnus-mime-view-part-as-type-internal))))
3800   (gnus-article-check-buffer)
3801   (let ((handle (get-text-property (point) 'gnus-data)))
3802     (when handle
3803       (setq handle
3804             (mm-make-handle (mm-handle-buffer handle)
3805                             (cons mime-type (cdr (mm-handle-type handle)))
3806                             (mm-handle-encoding handle)
3807                             (mm-handle-undisplayer handle)
3808                             (mm-handle-disposition handle)
3809                             (mm-handle-description handle)
3810                             (mm-handle-cache handle)
3811                             (mm-handle-id handle)))
3812       (setq gnus-article-mime-handles
3813             (mm-merge-handles gnus-article-mime-handles handle))
3814       (gnus-mm-display-part handle))))
3815
3816 (defun gnus-mime-copy-part (&optional handle)
3817   "Put the the MIME part under point into a new buffer."
3818   (interactive)
3819   (gnus-article-check-buffer)
3820   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3821          (contents (and handle (mm-get-part handle)))
3822          (base (and handle
3823                     (file-name-nondirectory
3824                      (or
3825                       (mail-content-type-get (mm-handle-type handle) 'name)
3826                       (mail-content-type-get (mm-handle-type handle)
3827                                              'filename)
3828                       "*decoded*"))))
3829          (buffer (and base (generate-new-buffer base))))
3830     (when contents
3831       (switch-to-buffer buffer)
3832       (insert contents)
3833       ;; We do it this way to make `normal-mode' set the appropriate mode.
3834       (unwind-protect
3835           (progn
3836             (setq buffer-file-name (expand-file-name base))
3837             (normal-mode))
3838         (setq buffer-file-name nil))
3839       (goto-char (point-min)))))
3840
3841 (defun gnus-mime-inline-part (&optional handle arg)
3842   "Insert the MIME part under point into the current buffer."
3843   (interactive (list nil current-prefix-arg))
3844   (gnus-article-check-buffer)
3845   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3846          contents charset
3847          (b (point))
3848          buffer-read-only)
3849     (when handle
3850       (if (and (not arg) (mm-handle-undisplayer handle))
3851           (mm-remove-part handle)
3852         (setq contents (mm-get-part handle))
3853         (cond
3854          ((not arg)
3855           (setq charset (or (mail-content-type-get
3856                              (mm-handle-type handle) 'charset)
3857                             gnus-newsgroup-charset)))
3858          ((numberp arg)
3859           (if (mm-handle-undisplayer handle)
3860               (mm-remove-part handle))
3861           (setq charset
3862                 (or (cdr (assq arg
3863                                gnus-summary-show-article-charset-alist))
3864                     (read-coding-system "Charset: ")))))
3865         (forward-line 2)
3866         (mm-insert-inline handle
3867                           (if (and charset
3868                                    (setq charset (mm-charset-to-coding-system
3869                                                   charset))
3870                                    (not (eq charset 'ascii)))
3871                               (mm-decode-coding-string contents charset)
3872                             contents))
3873         (goto-char b)))))
3874
3875 (defun gnus-mime-view-part-as-charset (&optional handle arg)
3876   "Insert the MIME part under point into the current buffer."
3877   (interactive (list nil current-prefix-arg))
3878   (gnus-article-check-buffer)
3879   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3880          contents charset
3881          (b (point))
3882          buffer-read-only)
3883     (when handle
3884       (if (mm-handle-undisplayer handle)
3885           (mm-remove-part handle))
3886       (let ((gnus-newsgroup-charset
3887              (or (cdr (assq arg
3888                             gnus-summary-show-article-charset-alist))
3889                  (read-coding-system "Charset: ")))
3890           (gnus-newsgroup-ignored-charsets 'gnus-all))
3891         (gnus-article-press-button)))))
3892
3893 (defun gnus-mime-externalize-part (&optional handle)
3894   "View the MIME part under point with an external viewer."
3895   (interactive)
3896   (gnus-article-check-buffer)
3897   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3898          (mm-user-display-methods nil)
3899          (mm-inlined-types nil)
3900          (mail-parse-charset gnus-newsgroup-charset)
3901          (mail-parse-ignored-charsets
3902           (save-excursion (set-buffer gnus-summary-buffer)
3903                           gnus-newsgroup-ignored-charsets)))
3904     (when handle
3905       (if (mm-handle-undisplayer handle)
3906           (mm-remove-part handle)
3907         (mm-display-part handle)))))
3908
3909 (defun gnus-mime-internalize-part (&optional handle)
3910   "View the MIME part under point with an internal viewer.
3911 In no internal viewer is available, use an external viewer."
3912   (interactive)
3913   (gnus-article-check-buffer)
3914   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3915          (mm-inlined-types '(".*"))
3916          (mm-inline-large-images t)
3917          (mail-parse-charset gnus-newsgroup-charset)
3918          (mail-parse-ignored-charsets
3919           (save-excursion (set-buffer gnus-summary-buffer)
3920                           gnus-newsgroup-ignored-charsets)))
3921     (when handle
3922       (if (mm-handle-undisplayer handle)
3923           (mm-remove-part handle)
3924         (mm-display-part handle)))))
3925
3926 (defun gnus-mime-action-on-part (&optional action)
3927   "Do something with the MIME attachment at \(point\)."
3928   (interactive
3929    (list (completing-read "Action: " gnus-mime-action-alist)))
3930   (gnus-article-check-buffer)
3931   (let ((action-pair (assoc action gnus-mime-action-alist)))
3932     (if action-pair
3933         (funcall (cdr action-pair)))))
3934
3935 (defun gnus-article-part-wrapper (n function)
3936   (save-current-buffer
3937     (set-buffer gnus-article-buffer)
3938     (when (> n (length gnus-article-mime-handle-alist))
3939       (error "No such part"))
3940     (gnus-article-goto-part n)
3941     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3942       (funcall function handle))))
3943
3944 (defun gnus-article-pipe-part (n)
3945   "Pipe MIME part N, which is the numerical prefix."
3946   (interactive "p")
3947   (gnus-article-part-wrapper n 'mm-pipe-part))
3948
3949 (defun gnus-article-save-part (n)
3950   "Save MIME part N, which is the numerical prefix."
3951   (interactive "p")
3952   (gnus-article-part-wrapper n 'mm-save-part))
3953
3954 (defun gnus-article-interactively-view-part (n)
3955   "View MIME part N interactively, which is the numerical prefix."
3956   (interactive "p")
3957   (gnus-article-part-wrapper n 'mm-interactively-view-part))
3958
3959 (defun gnus-article-copy-part (n)
3960   "Copy MIME part N, which is the numerical prefix."
3961   (interactive "p")
3962   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
3963
3964 (defun gnus-article-view-part-as-charset (n)
3965   "Copy MIME part N, which is the numerical prefix."
3966   (interactive "p")
3967   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
3968
3969 (defun gnus-article-externalize-part (n)
3970   "View MIME part N externally, which is the numerical prefix."
3971   (interactive "p")
3972   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
3973
3974 (defun gnus-article-inline-part (n)
3975   "Inline MIME part N, which is the numerical prefix."
3976   (interactive "p")
3977   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
3978
3979 (defun gnus-article-mime-match-handle-first (condition)
3980   (if condition
3981       (let ((alist gnus-article-mime-handle-alist) ihandle n)
3982         (while (setq ihandle (pop alist))
3983           (if (and (cond
3984                     ((functionp condition)
3985                      (funcall condition (cdr ihandle)))
3986                     ((eq condition 'undisplayed)
3987                      (not (or (mm-handle-undisplayer (cdr ihandle))
3988                               (equal (mm-handle-media-type (cdr ihandle))
3989                                      "multipart/alternative"))))
3990                     ((eq condition 'undisplayed-alternative)
3991                      (not (mm-handle-undisplayer (cdr ihandle))))
3992                     (t t))
3993                    (gnus-article-goto-part (car ihandle))
3994                    (or (not n) (< (car ihandle) n)))
3995               (setq n (car ihandle))))
3996         (or n 1))
3997     1))
3998
3999 (defun gnus-article-view-part (&optional n)
4000   "View MIME part N, which is the numerical prefix."
4001   (interactive "P")
4002   (save-current-buffer
4003     (set-buffer gnus-article-buffer)
4004     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
4005                              gnus-article-mime-match-handle-function)))
4006     (when (> n (length gnus-article-mime-handle-alist))
4007       (error "No such part"))
4008     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
4009       (when (gnus-article-goto-part n)
4010         (if (equal (car handle) "multipart/alternative")
4011             (gnus-article-press-button)
4012           (when (eq (gnus-mm-display-part handle) 'internal)
4013             (gnus-set-window-start)))))))
4014
4015 (defsubst gnus-article-mime-total-parts ()
4016   (if (bufferp (car gnus-article-mime-handles))
4017       1 ;; single part
4018     (1- (length gnus-article-mime-handles))))
4019
4020 (defun gnus-mm-display-part (handle)
4021   "Display HANDLE and fix MIME button."
4022   (let ((id (get-text-property (point) 'gnus-part))
4023         (point (point))
4024         buffer-read-only)
4025     (forward-line 1)
4026     (prog1
4027         (let ((window (selected-window))
4028               (mail-parse-charset gnus-newsgroup-charset)
4029               (mail-parse-ignored-charsets
4030                (save-excursion (set-buffer gnus-summary-buffer)
4031                                gnus-newsgroup-ignored-charsets)))
4032           (save-excursion
4033             (unwind-protect
4034                 (let ((win (get-buffer-window (current-buffer) t))
4035                       (beg (point)))
4036                   (when win
4037                     (select-window win))
4038                   (goto-char point)
4039                   (forward-line)
4040                   (if (mm-handle-displayed-p handle)
4041                       ;; This will remove the part.
4042                       (mm-display-part handle)
4043                     (save-restriction
4044                       (narrow-to-region (point) (1+ (point)))
4045                       (mm-display-part handle)
4046                       ;; We narrow to the part itself and
4047                       ;; then call the treatment functions.
4048                       (goto-char (point-min))
4049                       (forward-line 1)
4050                       (narrow-to-region (point) (point-max))
4051                       (gnus-treat-article
4052                        nil id
4053                        (gnus-article-mime-total-parts)
4054                        (mm-handle-media-type handle)))))
4055               (select-window window))))
4056       (goto-char point)
4057       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
4058       (gnus-insert-mime-button
4059        handle id (list (mm-handle-displayed-p handle)))
4060       (goto-char point))))
4061
4062 (defun gnus-article-goto-part (n)
4063   "Go to MIME part N."
4064   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
4065     (when point
4066       (goto-char point))))
4067
4068 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4069   (let ((gnus-tmp-name
4070          (or (mail-content-type-get (mm-handle-type handle)
4071                                     'name)
4072              (mail-content-type-get (mm-handle-disposition handle)
4073                                     'filename)
4074              (mail-content-type-get (mm-handle-type handle)
4075                                     'url)
4076              ""))
4077         (gnus-tmp-type (mm-handle-media-type handle))
4078         (gnus-tmp-description
4079          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4080                                               "")))
4081         (gnus-tmp-dots
4082          (if (if displayed (car displayed)
4083                (mm-handle-displayed-p handle))
4084              "" "..."))
4085         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4086                            (buffer-size)))
4087         gnus-tmp-type-long b e)
4088     (when (string-match ".*/" gnus-tmp-name)
4089       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4090     (setq gnus-tmp-type-long (concat gnus-tmp-type
4091                                      (and (not (equal gnus-tmp-name ""))
4092                                           (concat "; " gnus-tmp-name))))
4093     (or (equal gnus-tmp-description "")
4094         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4095     (unless (bolp)
4096       (insert "\n"))
4097     (setq b (point))
4098     (gnus-eval-format
4099      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4100      `(keymap ,gnus-mime-button-map
4101               ,@(if (>= (string-to-number emacs-version) 21)
4102                     nil
4103                   (list 'local-map gnus-mime-button-map))
4104               gnus-callback gnus-mm-display-part
4105               gnus-part ,gnus-tmp-id
4106               article-type annotation
4107               gnus-data ,handle))
4108     (setq e (point))
4109     (widget-convert-button
4110      'link b e
4111      :mime-handle handle
4112      :action 'gnus-widget-press-button
4113      :button-keymap gnus-mime-button-map
4114      :help-echo
4115      (lambda (widget/window &optional overlay pos)
4116        ;; Needed to properly clear the message due to a bug in
4117        ;; wid-edit (XEmacs only).
4118        (if (boundp 'help-echo-owns-message)
4119            (setq help-echo-owns-message t))
4120        (format
4121         "%S: %s the MIME part; %S: more options"
4122         (aref gnus-mouse-2 0)
4123         ;; XEmacs will get a single widget arg; Emacs 21 will get
4124         ;; window, overlay, position.
4125         (if (mm-handle-displayed-p
4126              (if overlay
4127                  (with-current-buffer (gnus-overlay-buffer overlay)
4128                    (widget-get (widget-at (gnus-overlay-start overlay))
4129                                :mime-handle))
4130                (widget-get widget/window :mime-handle)))
4131             "hide" "show")
4132         (aref gnus-down-mouse-3 0))))))
4133
4134 (defun gnus-widget-press-button (elems el)
4135   (goto-char (widget-get elems :from))
4136   (gnus-article-press-button))
4137
4138 (defvar gnus-displaying-mime nil)
4139
4140 (defun gnus-display-mime (&optional ihandles)
4141   "Display the MIME parts."
4142   (save-excursion
4143     (save-selected-window
4144       (let ((window (get-buffer-window gnus-article-buffer))
4145             (point (point)))
4146         (when window
4147           (select-window window)
4148           ;; We have to do this since selecting the window
4149           ;; may change the point.  So we set the window point.
4150           (set-window-point window point)))
4151       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
4152              buffer-read-only handle name type b e display)
4153         (when (and (not ihandles)
4154                    (not gnus-displaying-mime))
4155           ;; Top-level call; we clean up.
4156           (when gnus-article-mime-handles
4157             (mm-destroy-parts gnus-article-mime-handles)
4158             (setq gnus-article-mime-handle-alist nil));; A trick.
4159           (setq gnus-article-mime-handles handles)
4160           ;; We allow users to glean info from the handles.
4161           (when gnus-article-mime-part-function
4162             (gnus-mime-part-function handles)))
4163         (if (and handles
4164                  (or (not (stringp (car handles)))
4165                      (cdr handles)))
4166             (progn
4167               (when (and (not ihandles)
4168                          (not gnus-displaying-mime))
4169                 ;; Clean up for mime parts.
4170                 (article-goto-body)
4171                 (delete-region (point) (point-max)))
4172               (let ((gnus-displaying-mime t))
4173                 (gnus-mime-display-part handles)))
4174           (save-restriction
4175             (article-goto-body)
4176             (narrow-to-region (point) (point-max))
4177             (gnus-treat-article nil 1 1)
4178             (widen)))
4179         (unless ihandles
4180           ;; Highlight the headers.
4181           (save-excursion
4182             (save-restriction
4183               (article-goto-body)
4184               (narrow-to-region (point-min) (point))
4185               (gnus-treat-article 'head))))))))
4186
4187 (defvar gnus-mime-display-multipart-as-mixed nil)
4188 (defvar gnus-mime-display-multipart-alternative-as-mixed nil)
4189 (defvar gnus-mime-display-multipart-related-as-mixed nil)
4190
4191 (defun gnus-mime-display-part (handle)
4192   (cond
4193    ;; Single part.
4194    ((not (stringp (car handle)))
4195     (gnus-mime-display-single handle))
4196    ;; User-defined multipart
4197    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
4198     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
4199              handle))
4200    ;; multipart/alternative
4201    ((and (equal (car handle) "multipart/alternative")
4202          (not (or gnus-mime-display-multipart-as-mixed
4203                   gnus-mime-display-multipart-alternative-as-mixed)))
4204     (let ((id (1+ (length gnus-article-mime-handle-alist))))
4205       (push (cons id handle) gnus-article-mime-handle-alist)
4206       (gnus-mime-display-alternative (cdr handle) nil nil id)))
4207    ;; multipart/related
4208    ((and (equal (car handle) "multipart/related")
4209          (not (or gnus-mime-display-multipart-as-mixed
4210                   gnus-mime-display-multipart-related-as-mixed)))
4211     ;;;!!!We should find the start part, but we just default
4212     ;;;!!!to the first part.
4213     ;;(gnus-mime-display-part (cadr handle))
4214     ;;;!!! Most multipart/related is an HTML message plus images.
4215     ;;;!!! Unfortunately we are unable to let W3 display those
4216     ;;;!!! included images, so we just display it as a mixed multipart.
4217     ;;(gnus-mime-display-mixed (cdr handle))
4218     ;;;!!! No, w3 can display everything just fine.
4219     (gnus-mime-display-part (cadr handle)))
4220    ((equal (car handle) "multipart/signed")
4221     (or (memq 'signed gnus-article-wash-types)
4222         (push 'signed gnus-article-wash-types))
4223     (gnus-mime-display-security handle))
4224    ((equal (car handle) "multipart/encrypted")
4225     (or (memq 'encrypted gnus-article-wash-types)
4226         (push 'encrypted gnus-article-wash-types))
4227     (gnus-mime-display-security handle))
4228    ;; Other multiparts are handled like multipart/mixed.
4229    (t
4230     (gnus-mime-display-mixed (cdr handle)))))
4231
4232 (defun gnus-mime-part-function (handles)
4233   (if (stringp (car handles))
4234       (mapcar 'gnus-mime-part-function (cdr handles))
4235     (funcall gnus-article-mime-part-function handles)))
4236
4237 (defun gnus-mime-display-mixed (handles)
4238   (mapcar 'gnus-mime-display-part handles))
4239
4240 (defun gnus-mime-display-single (handle)
4241   (let ((type (mm-handle-media-type handle))
4242         (ignored gnus-ignored-mime-types)
4243         (not-attachment t)
4244         (move nil)
4245         display text)
4246     (catch 'ignored
4247       (progn
4248         (while ignored
4249           (when (string-match (pop ignored) type)
4250             (throw 'ignored nil)))
4251         (if (and (setq not-attachment
4252                        (and (not (mm-inline-override-p handle))
4253                             (or (not (mm-handle-disposition handle))
4254                                 (equal (car (mm-handle-disposition handle))
4255                                        "inline")
4256                                 (mm-attachment-override-p handle))))
4257                  (mm-automatic-display-p handle)
4258                  (or (mm-inlined-p handle)
4259                      (mm-automatic-external-display-p type)))
4260             (setq display t)
4261           (when (equal (mm-handle-media-supertype handle) "text")
4262             (setq text t)))
4263         (let ((id (1+ (length gnus-article-mime-handle-alist)))
4264               beg)
4265           (push (cons id handle) gnus-article-mime-handle-alist)
4266           (when (or (not display)
4267                     (not (gnus-unbuttonized-mime-type-p type)))
4268             ;(gnus-article-insert-newline)
4269             (gnus-insert-mime-button
4270              handle id (list (or display (and not-attachment text))))
4271             (gnus-article-insert-newline)
4272             ;(gnus-article-insert-newline)
4273             ;; Remember modify the number of forward lines.
4274             (setq move t))
4275           (setq beg (point))
4276           (cond
4277            (display
4278             (when move
4279               (forward-line -1)
4280               (setq beg (point)))
4281             (let ((mail-parse-charset gnus-newsgroup-charset)
4282                   (mail-parse-ignored-charsets
4283                    (save-excursion (condition-case ()
4284                                        (set-buffer gnus-summary-buffer)
4285                                      (error))
4286                                    gnus-newsgroup-ignored-charsets)))
4287               (mm-display-part handle t))
4288             (goto-char (point-max)))
4289            ((and text not-attachment)
4290             (when move
4291               (forward-line -1)
4292               (setq beg (point)))
4293             (gnus-article-insert-newline)
4294             (mm-insert-inline handle (mm-get-part handle))
4295             (goto-char (point-max))))
4296           ;; Do highlighting.
4297           (save-excursion
4298             (save-restriction
4299               (narrow-to-region beg (point))
4300               (gnus-treat-article
4301                nil id
4302                (gnus-article-mime-total-parts)
4303                (mm-handle-media-type handle)))))))))
4304
4305 (defun gnus-unbuttonized-mime-type-p (type)
4306   "Say whether TYPE is to be unbuttonized."
4307   (unless gnus-inhibit-mime-unbuttonizing
4308     (catch 'found
4309       (let ((types gnus-unbuttonized-mime-types))
4310         (while types
4311           (when (string-match (pop types) type)
4312             (throw 'found t)))))))
4313
4314 (defun gnus-article-insert-newline ()
4315   "Insert a newline, but mark it as undeletable."
4316   (gnus-put-text-property
4317    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
4318
4319 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
4320   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
4321          (ihandles handles)
4322          (point (point))
4323          handle buffer-read-only from props begend not-pref)
4324     (save-window-excursion
4325       (save-restriction
4326         (when ibegend
4327           (narrow-to-region (car ibegend)
4328                             (or (cdr ibegend)
4329                                 (progn
4330                                   (goto-char (car ibegend))
4331                                   (forward-line 2)
4332                                   (point))))
4333           (delete-region (point-min) (point-max))
4334           (mm-remove-parts handles))
4335         (setq begend (list (point-marker)))
4336         ;; Do the toggle.
4337         (unless (setq not-pref (cadr (member preferred ihandles)))
4338           (setq not-pref (car ihandles)))
4339         (when (or ibegend
4340                   (not preferred)
4341                   (not (gnus-unbuttonized-mime-type-p
4342                         "multipart/alternative")))
4343           (gnus-add-text-properties
4344            (setq from (point))
4345            (progn
4346              (insert (format "%d.  " id))
4347              (point))
4348            `(gnus-callback
4349              (lambda (handles)
4350                (unless ,(not ibegend)
4351                  (setq gnus-article-mime-handle-alist
4352                        ',gnus-article-mime-handle-alist))
4353                (gnus-mime-display-alternative
4354                 ',ihandles ',not-pref ',begend ,id))
4355              ,@(if (>= (string-to-number emacs-version) 21)
4356                    nil ;; XEmacs doesn't care
4357                  (list 'local-map gnus-mime-button-map))
4358              ,gnus-mouse-face-prop ,gnus-article-mouse-face
4359              face ,gnus-article-button-face
4360              keymap ,gnus-mime-button-map
4361              gnus-part ,id
4362              gnus-data ,handle))
4363           (widget-convert-button 'link from (point)
4364                                  :action 'gnus-widget-press-button
4365                                  :button-keymap gnus-widget-button-keymap)
4366           ;; Do the handles
4367           (while (setq handle (pop handles))
4368             (gnus-add-text-properties
4369              (setq from (point))
4370              (progn
4371                (insert (format "(%c) %-18s"
4372                                (if (equal handle preferred) ?* ? )
4373                                (mm-handle-media-type handle)))
4374                (point))
4375              `(gnus-callback
4376                (lambda (handles)
4377                  (unless ,(not ibegend)
4378                    (setq gnus-article-mime-handle-alist
4379                          ',gnus-article-mime-handle-alist))
4380                  (gnus-mime-display-alternative
4381                   ',ihandles ',handle ',begend ,id))
4382                ,@(if (>= (string-to-number emacs-version) 21)
4383                      nil ;; XEmacs doesn't care
4384                    (list 'local-map gnus-mime-button-map))
4385                ,gnus-mouse-face-prop ,gnus-article-mouse-face
4386                face ,gnus-article-button-face
4387                keymap ,gnus-mime-button-map
4388                gnus-part ,id
4389                gnus-data ,handle))
4390             (widget-convert-button 'link from (point)
4391                                    :action 'gnus-widget-press-button
4392                                    :button-keymap gnus-widget-button-keymap)
4393             (insert "  "))
4394           (insert "\n\n"))
4395         (when preferred
4396           (if (stringp (car preferred))
4397               (gnus-display-mime preferred)
4398             (let ((mail-parse-charset gnus-newsgroup-charset)
4399                   (mail-parse-ignored-charsets
4400                    (save-excursion (set-buffer gnus-summary-buffer)
4401                                    gnus-newsgroup-ignored-charsets)))
4402               (mm-display-part preferred)
4403               ;; Do highlighting.
4404               (save-excursion
4405                 (save-restriction
4406                   (narrow-to-region (car begend) (point-max))
4407                   (gnus-treat-article
4408                    nil (length gnus-article-mime-handle-alist)
4409                    (gnus-article-mime-total-parts)
4410                    (mm-handle-media-type handle))))))
4411           (goto-char (point-max))
4412           (setcdr begend (point-marker)))))
4413     (when ibegend
4414       (goto-char point))))
4415
4416 (defun gnus-article-wash-status ()
4417   "Return a string which display status of article washing."
4418   (save-excursion
4419     (set-buffer gnus-article-buffer)
4420     (let ((cite (memq 'cite gnus-article-wash-types))
4421           (headers (memq 'headers gnus-article-wash-types))
4422           (boring (memq 'boring-headers gnus-article-wash-types))
4423           (pgp (memq 'pgp gnus-article-wash-types))
4424           (pem (memq 'pem gnus-article-wash-types))
4425           (signed (memq 'signed gnus-article-wash-types))
4426           (encrypted (memq 'encrypted gnus-article-wash-types))
4427           (signature (memq 'signature gnus-article-wash-types))
4428           (overstrike (memq 'overstrike gnus-article-wash-types))
4429           (emphasis (memq 'emphasis gnus-article-wash-types)))
4430       (format "%c%c%c%c%c%c%c"
4431               (if cite ?c ? )
4432               (if (or headers boring) ?h ? )
4433               (if (or pgp pem signed encrypted) ?p ? )
4434               (if signature ?s ? )
4435               (if overstrike ?o ? )
4436               (if gnus-show-mime ?m ? )
4437               (if emphasis ?e ? )))))
4438
4439 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
4440
4441 (defun gnus-article-maybe-hide-headers ()
4442   "Hide unwanted headers if `gnus-have-all-headers' is nil.
4443 Provided for backwards compatibility."
4444   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
4445                  (not (save-excursion (set-buffer gnus-summary-buffer)
4446                                       gnus-have-all-headers)))
4447              (not gnus-inhibit-hiding))
4448     (gnus-article-hide-headers)))
4449
4450 ;;; Article savers.
4451
4452 (defun gnus-output-to-file (file-name)
4453   "Append the current article to a file named FILE-NAME."
4454   (let ((artbuf (current-buffer)))
4455     (with-temp-buffer
4456       (insert-buffer-substring artbuf)
4457       ;; Append newline at end of the buffer as separator, and then
4458       ;; save it to file.
4459       (goto-char (point-max))
4460       (insert "\n")
4461       (write-region-as-binary (point-min) (point-max) file-name 'append)
4462       t)))
4463
4464 (defun gnus-narrow-to-page (&optional arg)
4465   "Narrow the article buffer to a page.
4466 If given a numerical ARG, move forward ARG pages."
4467   (interactive "P")
4468   (setq arg (if arg (prefix-numeric-value arg) 0))
4469   (save-excursion
4470     (set-buffer gnus-article-buffer)
4471     (goto-char (point-min))
4472     (widen)
4473     ;; Remove any old next/prev buttons.
4474     (when (gnus-visual-p 'page-marker)
4475       (let ((buffer-read-only nil))
4476         (gnus-remove-text-with-property 'gnus-prev)
4477         (gnus-remove-text-with-property 'gnus-next)))
4478     (when
4479         (cond ((< arg 0)
4480                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
4481               ((> arg 0)
4482                (re-search-forward page-delimiter nil 'move arg)))
4483       (goto-char (match-end 0)))
4484     (narrow-to-region
4485      (point)
4486      (if (re-search-forward page-delimiter nil 'move)
4487          (match-beginning 0)
4488        (point)))
4489     (when (and (gnus-visual-p 'page-marker)
4490                (not (= (point-min) 1)))
4491       (save-excursion
4492         (goto-char (point-min))
4493         (gnus-insert-prev-page-button)))
4494     (when (and (gnus-visual-p 'page-marker)
4495                (< (+ (point-max) 2) (buffer-size)))
4496       (save-excursion
4497         (goto-char (point-max))
4498         (gnus-insert-next-page-button)))))
4499
4500 ;; Article mode commands
4501
4502 (defun gnus-article-goto-next-page ()
4503   "Show the next page of the article."
4504   (interactive)
4505   (when (gnus-article-next-page)
4506     (goto-char (point-min))
4507     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
4508
4509 (defun gnus-article-goto-prev-page ()
4510   "Show the next page of the article."
4511   (interactive)
4512   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
4513     (gnus-article-prev-page nil)))
4514
4515 (defun gnus-article-next-page (&optional lines)
4516   "Show the next page of the current article.
4517 If end of article, return non-nil.  Otherwise return nil.
4518 Argument LINES specifies lines to be scrolled up."
4519   (interactive "p")
4520   (let ((start (window-start))
4521         end-of-buffer end-of-page)
4522     (save-excursion
4523       (move-to-window-line -1)
4524       (if (<= (point) start)
4525           (progn
4526             (forward-line 2)
4527             (setq start (point)))
4528         (forward-line 1)
4529         (setq start nil))
4530       (unless (or (cond ((eq (1+ (buffer-size)) (point))
4531                          (and (pos-visible-in-window-p)
4532                               (setq end-of-buffer t)))
4533                         ((eobp)
4534                          (setq end-of-page t)))
4535                   (not lines))
4536         (move-to-window-line lines)
4537         (unless (search-backward "\n\n" nil t)
4538           (setq start (point)))))
4539     (cond (end-of-buffer t)
4540           (end-of-page
4541            (gnus-narrow-to-page 1)
4542            nil)
4543           (t
4544            (if start
4545                (set-window-start (selected-window) start)
4546              (let (window-pixel-scroll-increment)
4547                (scroll-up lines)))
4548            nil))))
4549
4550 (defun gnus-article-prev-page (&optional lines)
4551   "Show previous page of current article.
4552 Argument LINES specifies lines to be scrolled down."
4553   (interactive "p")
4554   (let (beginning-of-buffer beginning-of-page)
4555     (save-excursion
4556       (move-to-window-line 0)
4557       (cond ((eq 1 (point))
4558              (setq beginning-of-buffer t))
4559             ((bobp)
4560              (setq beginning-of-page t))))
4561     (cond (beginning-of-buffer)
4562           (beginning-of-page
4563            (gnus-narrow-to-page -1))
4564           (t
4565            (condition-case nil
4566                (let (window-pixel-scroll-increment)
4567                  (scroll-down lines))
4568              (beginning-of-buffer
4569               (goto-char (point-min))))))))
4570
4571 (defun gnus-article-refer-article ()
4572   "Read article specified by message-id around point."
4573   (interactive)
4574   (let ((point (point)))
4575     (search-forward ">" nil t)          ;Move point to end of "<....>".
4576     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
4577         (let ((message-id (match-string 1)))
4578           (goto-char point)
4579           (set-buffer gnus-summary-buffer)
4580           (gnus-summary-refer-article message-id))
4581       (goto-char (point))
4582       (error "No references around point"))))
4583
4584 (defun gnus-article-show-summary ()
4585   "Reconfigure windows to show summary buffer."
4586   (interactive)
4587   (if (not (gnus-buffer-live-p gnus-summary-buffer))
4588       (error "There is no summary buffer for this article buffer")
4589     (gnus-article-set-globals)
4590     (gnus-configure-windows 'article)
4591     (gnus-summary-goto-subject gnus-current-article)
4592     (gnus-summary-position-point)))
4593
4594 (defun gnus-article-describe-briefly ()
4595   "Describe article mode commands briefly."
4596   (interactive)
4597   (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")))
4598
4599 (defun gnus-article-summary-command ()
4600   "Execute the last keystroke in the summary buffer."
4601   (interactive)
4602   (let ((obuf (current-buffer))
4603         (owin (current-window-configuration))
4604         func)
4605     (switch-to-buffer gnus-article-current-summary 'norecord)
4606     (setq func (lookup-key (current-local-map) (this-command-keys)))
4607     (call-interactively func)
4608     (set-buffer obuf)
4609     (set-window-configuration owin)
4610     (set-window-point (get-buffer-window (current-buffer)) (point))))
4611
4612 (defun gnus-article-summary-command-nosave ()
4613   "Execute the last keystroke in the summary buffer."
4614   (interactive)
4615   (let (func)
4616     (pop-to-buffer gnus-article-current-summary 'norecord)
4617     (setq func (lookup-key (current-local-map) (this-command-keys)))
4618     (call-interactively func)))
4619
4620 (defun gnus-article-check-buffer ()
4621   "Beep if not in an article buffer."
4622   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
4623     (error "Command invoked outside of a Gnus article buffer")))
4624
4625 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
4626   "Read a summary buffer key sequence and execute it from the article buffer."
4627   (interactive "P")
4628   (gnus-article-check-buffer)
4629   (let ((nosaves
4630          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
4631            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
4632            "=" "^" "\M-^" "|"))
4633         (nosave-but-article
4634          '("A\r"))
4635         (nosave-in-article
4636          '("\C-d"))
4637         (up-to-top
4638          '("n" "Gn" "p" "Gp"))
4639         keys new-sum-point)
4640     (save-excursion
4641       (set-buffer gnus-article-current-summary)
4642       (let (gnus-pick-mode)
4643         (push (or key last-command-event) unread-command-events)
4644         (setq keys (static-if (featurep 'xemacs)
4645                        (events-to-keys (read-key-sequence nil))
4646                      (read-key-sequence nil)))))
4647     (message "")
4648
4649     (if (or (member keys nosaves)
4650             (member keys nosave-but-article)
4651             (member keys nosave-in-article))
4652         (let (func)
4653           (save-window-excursion
4654             (pop-to-buffer gnus-article-current-summary 'norecord)
4655             ;; We disable the pick minor mode commands.
4656             (let (gnus-pick-mode)
4657               (setq func (lookup-key (current-local-map) keys))))
4658           (if (or (not func)
4659                   (numberp func))
4660               (ding)
4661             (unless (member keys nosave-in-article)
4662               (set-buffer gnus-article-current-summary))
4663             (call-interactively func)
4664             (setq new-sum-point (point)))
4665           (when (member keys nosave-but-article)
4666             (pop-to-buffer gnus-article-buffer 'norecord)))
4667       ;; These commands should restore window configuration.
4668       (let ((obuf (current-buffer))
4669             (owin (current-window-configuration))
4670             (opoint (point))
4671             (summary gnus-article-current-summary)
4672             func in-buffer selected)
4673         (if not-restore-window
4674             (pop-to-buffer summary 'norecord)
4675           (switch-to-buffer summary 'norecord))
4676         (setq in-buffer (current-buffer))
4677         ;; We disable the pick minor mode commands.
4678         (if (and (setq func (let (gnus-pick-mode)
4679                               (lookup-key (current-local-map) keys)))
4680                  (functionp func))
4681             (progn
4682               (call-interactively func)
4683               (setq new-sum-point (point))
4684               (when (eq in-buffer (current-buffer))
4685                 (setq selected (gnus-summary-select-article))
4686                 (set-buffer obuf)
4687                 (unless not-restore-window
4688                   (set-window-configuration owin))
4689                 (when (eq selected 'old)
4690                   (article-goto-body)
4691                   (set-window-start (get-buffer-window (current-buffer))
4692                                     1)
4693                   (set-window-point (get-buffer-window (current-buffer))
4694                                     (point)))
4695                 (let ((win (get-buffer-window gnus-article-current-summary)))
4696                   (when win
4697                     (set-window-point win new-sum-point))))    )
4698           (switch-to-buffer gnus-article-buffer)
4699           (ding))))))
4700
4701 (defun gnus-article-describe-key (key)
4702   "Display documentation of the function invoked by KEY.  KEY is a string."
4703   (interactive "kDescribe key: ")
4704   (gnus-article-check-buffer)
4705   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4706       (save-excursion
4707         (set-buffer gnus-article-current-summary)
4708         (let (gnus-pick-mode)
4709           (if (featurep 'xemacs)
4710               (progn
4711                 (push (elt key 0) unread-command-events)
4712                 (setq key (events-to-keys
4713                            (read-key-sequence "Describe key: "))))
4714             (setq unread-command-events
4715                   (mapcar
4716                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
4717                    key))
4718             (setq key (read-key-sequence "Describe key: "))))
4719         (describe-key key))
4720     (describe-key key)))
4721
4722 (defun gnus-article-describe-key-briefly (key &optional insert)
4723   "Display documentation of the function invoked by KEY.  KEY is a string."
4724   (interactive "kDescribe key: \nP")
4725   (gnus-article-check-buffer)
4726   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4727       (save-excursion
4728         (set-buffer gnus-article-current-summary)
4729         (let (gnus-pick-mode)
4730           (if (featurep 'xemacs)
4731               (progn
4732                 (push (elt key 0) unread-command-events)
4733                 (setq key (events-to-keys
4734                            (read-key-sequence "Describe key: "))))
4735             (setq unread-command-events
4736                   (mapcar
4737                    (lambda (x) (if (>= x 128) (list 'meta (- x 128)) x))
4738                    key))
4739             (setq key (read-key-sequence "Describe key: "))))
4740         (describe-key-briefly key insert))
4741     (describe-key-briefly key insert)))
4742
4743 (defun gnus-article-hide (&optional arg force)
4744   "Hide all the gruft in the current article.
4745 This means that PGP stuff, signatures, cited text and (some)
4746 headers will be hidden.
4747 If given a prefix, show the hidden text instead."
4748   (interactive (append (gnus-article-hidden-arg) (list 'force)))
4749   (gnus-article-hide-headers arg)
4750   (gnus-article-hide-list-identifiers arg)
4751   (gnus-article-hide-pgp arg)
4752   (gnus-article-hide-citation-maybe arg force)
4753   (gnus-article-hide-signature arg))
4754
4755 (defun gnus-article-maybe-highlight ()
4756   "Do some article highlighting if article highlighting is requested."
4757   (when (gnus-visual-p 'article-highlight 'highlight)
4758     (gnus-article-highlight-some)))
4759
4760 (defun gnus-check-group-server ()
4761   ;; Make sure the connection to the server is alive.
4762   (unless (gnus-server-opened
4763            (gnus-find-method-for-group gnus-newsgroup-name))
4764     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
4765     (gnus-request-group gnus-newsgroup-name t)))
4766
4767 (defun gnus-request-article-this-buffer (article group)
4768   "Get an article and insert it into this buffer."
4769   (let (do-update-line sparse-header)
4770     (prog1
4771         (save-excursion
4772           (erase-buffer)
4773           (gnus-kill-all-overlays)
4774           (setq group (or group gnus-newsgroup-name))
4775
4776           ;; Using `gnus-request-article' directly will insert the article into
4777           ;; `nntp-server-buffer' - so we'll save some time by not having to
4778           ;; copy it from the server buffer into the article buffer.
4779
4780           ;; We only request an article by message-id when we do not have the
4781           ;; headers for it, so we'll have to get those.
4782           (when (stringp article)
4783             (gnus-read-header article))
4784
4785           ;; If the article number is negative, that means that this article
4786           ;; doesn't belong in this newsgroup (possibly), so we find its
4787           ;; message-id and request it by id instead of number.
4788           (when (and (numberp article)
4789                      gnus-summary-buffer
4790                      (get-buffer gnus-summary-buffer)
4791                      (gnus-buffer-exists-p gnus-summary-buffer))
4792             (save-excursion
4793               (set-buffer gnus-summary-buffer)
4794               (let ((header (gnus-summary-article-header article)))
4795                 (when (< article 0)
4796                   (cond
4797                    ((memq article gnus-newsgroup-sparse)
4798                     ;; This is a sparse gap article.
4799                     (setq do-update-line article)
4800                     (setq article (mail-header-id header))
4801                     (setq sparse-header (gnus-read-header article))
4802                     (setq gnus-newsgroup-sparse
4803                           (delq article gnus-newsgroup-sparse)))
4804                    ((vectorp header)
4805                     ;; It's a real article.
4806                     (setq article (mail-header-id header)))
4807                    (t
4808                     ;; It is an extracted pseudo-article.
4809                     (setq article 'pseudo)
4810                     (gnus-request-pseudo-article header))))
4811
4812                 (let ((method (gnus-find-method-for-group
4813                                gnus-newsgroup-name)))
4814                   (when (and (eq (car method) 'nneething)
4815                              (vectorp header))
4816                     (let ((dir (expand-file-name
4817                                 (mail-header-subject header)
4818                                 (file-name-as-directory
4819                                  (or (cadr (assq 'nneething-address method))
4820                                      (nth 1 method))))))
4821                       (when (file-directory-p dir)
4822                         (setq article 'nneething)
4823                         (gnus-group-enter-directory dir))))))))
4824
4825           (cond
4826            ;; Refuse to select canceled articles.
4827            ((and (numberp article)
4828                  gnus-summary-buffer
4829                  (get-buffer gnus-summary-buffer)
4830                  (gnus-buffer-exists-p gnus-summary-buffer)
4831                  (eq (cdr (save-excursion
4832                             (set-buffer gnus-summary-buffer)
4833                             (assq article gnus-newsgroup-reads)))
4834                      gnus-canceled-mark))
4835             nil)
4836            ;; Check the backlog.
4837            ((and gnus-keep-backlog
4838                  (gnus-backlog-request-article group article (current-buffer)))
4839             'article)
4840            ;; Check asynchronous pre-fetch.
4841            ((gnus-async-request-fetched-article group article (current-buffer))
4842             (gnus-async-prefetch-next group article gnus-summary-buffer)
4843             (when (and (numberp article) gnus-keep-backlog)
4844               (gnus-backlog-enter-article group article (current-buffer)))
4845             'article)
4846            ;; Check the cache.
4847            ((and gnus-use-cache
4848                  (numberp article)
4849                  (gnus-cache-request-article article group))
4850             'article)
4851            ;; Get the article and put into the article buffer.
4852            ((or (stringp article)
4853                 (numberp article))
4854             (let ((gnus-override-method gnus-override-method)
4855                   (methods (and (stringp article)
4856                                 gnus-refer-article-method))
4857                   result
4858                   (buffer-read-only nil))
4859               (if (or (not (listp methods))
4860                       (and (symbolp (car methods))
4861                            (assq (car methods) nnoo-definition-alist)))
4862                   (setq methods (list methods)))
4863               (when (and (null gnus-override-method)
4864                          methods)
4865                 (setq gnus-override-method (pop methods)))
4866               (while (not result)
4867                 (when (eq gnus-override-method 'current)
4868                   (setq gnus-override-method gnus-current-select-method))
4869                 (erase-buffer)
4870                 (gnus-kill-all-overlays)
4871                 (let ((gnus-newsgroup-name group))
4872                   (gnus-check-group-server))
4873                 (when (gnus-request-article article group (current-buffer))
4874                   (when (numberp article)
4875                     (gnus-async-prefetch-next group article
4876                                               gnus-summary-buffer)
4877                     (when gnus-keep-backlog
4878                       (gnus-backlog-enter-article
4879                        group article (current-buffer))))
4880                   (setq result 'article))
4881                 (if (not result)
4882                     (if methods
4883                         (setq gnus-override-method (pop methods))
4884                       (setq result 'done))))
4885               (and (eq result 'article) 'article)))
4886            ;; It was a pseudo.
4887            (t article)))
4888
4889       ;; Associate this article with the current summary buffer.
4890       (setq gnus-article-current-summary gnus-summary-buffer)
4891
4892       ;; Take the article from the original article buffer
4893       ;; and place it in the buffer it's supposed to be in.
4894       (when (and (get-buffer gnus-article-buffer)
4895                  (equal (buffer-name (current-buffer))
4896                         (buffer-name (get-buffer gnus-article-buffer))))
4897         (save-excursion
4898           (if (get-buffer gnus-original-article-buffer)
4899               (set-buffer gnus-original-article-buffer)
4900             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
4901             (set-buffer-multibyte nil)
4902             (buffer-disable-undo)
4903             (setq major-mode 'gnus-original-article-mode)
4904             (setq buffer-read-only t))
4905           (let (buffer-read-only)
4906             (erase-buffer)
4907             (insert-buffer-substring gnus-article-buffer))
4908           (setq gnus-original-article (cons group article)))
4909
4910         ;; Decode charsets.
4911         (run-hooks 'gnus-article-decode-hook)
4912         ;; Mark article as decoded or not.
4913         (setq gnus-article-decoded-p gnus-article-decode-hook))
4914
4915       ;; Update sparse articles.
4916       (when (and do-update-line
4917                  (or (numberp article)
4918                      (stringp article)))
4919         (let ((buf (current-buffer)))
4920           (set-buffer gnus-summary-buffer)
4921           (gnus-summary-update-article do-update-line sparse-header)
4922           (gnus-summary-goto-subject do-update-line nil t)
4923           (set-window-point (get-buffer-window (current-buffer) t)
4924                             (point))
4925           (set-buffer buf))))))
4926
4927 ;;;
4928 ;;; Article editing
4929 ;;;
4930
4931 (defcustom gnus-article-edit-mode-hook nil
4932   "Hook run in article edit mode buffers."
4933   :group 'gnus-article-various
4934   :type 'hook)
4935
4936 (defcustom gnus-article-edit-article-setup-function
4937   'gnus-article-mime-edit-article-setup
4938   "Function called to setup an editing article buffer."
4939   :group 'gnus-article-various
4940   :type 'function)
4941
4942 (defvar gnus-article-edit-done-function nil)
4943
4944 (defvar gnus-article-edit-mode-map nil)
4945
4946 ;; Should we be using derived.el for this?
4947 (unless gnus-article-edit-mode-map
4948   (setq gnus-article-edit-mode-map (make-sparse-keymap))
4949   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
4950
4951   (gnus-define-keys gnus-article-edit-mode-map
4952     "\C-c\C-c" gnus-article-edit-done
4953     "\C-c\C-k" gnus-article-edit-exit)
4954
4955   (gnus-define-keys (gnus-article-edit-wash-map
4956                      "\C-c\C-w" gnus-article-edit-mode-map)
4957     "f" gnus-article-edit-full-stops))
4958
4959 (defun gnus-article-edit-mode ()
4960   "Major mode for editing articles.
4961 This is an extended text-mode.
4962
4963 \\{gnus-article-edit-mode-map}"
4964   (interactive)
4965   (setq major-mode 'gnus-article-edit-mode)
4966   (setq mode-name "Article Edit")
4967   (use-local-map gnus-article-edit-mode-map)
4968   (make-local-variable 'gnus-article-edit-done-function)
4969   (make-local-variable 'gnus-prev-winconf)
4970   (setq buffer-read-only nil)
4971   (buffer-enable-undo)
4972   (widen)
4973   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
4974
4975 (defun gnus-article-edit (&optional force)
4976   "Edit the current article.
4977 This will have permanent effect only in mail groups.
4978 If FORCE is non-nil, allow editing of articles even in read-only
4979 groups."
4980   (interactive "P")
4981   (when (and (not force)
4982              (gnus-group-read-only-p))
4983     (error "The current newsgroup does not support article editing"))
4984   (gnus-article-date-original)
4985   (gnus-article-edit-article
4986    'ignore
4987    `(lambda (no-highlight)
4988       'ignore
4989       (gnus-summary-edit-article-done
4990        ,(or (mail-header-references gnus-current-headers) "")
4991        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
4992
4993 (defun gnus-article-edit-article (start-func exit-func)
4994   "Start editing the contents of the current article buffer."
4995   (let ((winconf (current-window-configuration)))
4996     (set-buffer gnus-article-buffer)
4997     (gnus-article-edit-mode)
4998     (funcall start-func)
4999     (set-buffer-modified-p nil)
5000     (gnus-configure-windows 'edit-article)
5001     (setq gnus-article-edit-done-function exit-func)
5002     (setq gnus-prev-winconf winconf)
5003     (when gnus-article-edit-article-setup-function
5004       (funcall gnus-article-edit-article-setup-function))
5005     (gnus-message 6 "C-c C-c to end edits; C-c C-k to exit")))
5006
5007 (defun gnus-article-edit-done (&optional arg)
5008   "Update the article edits and exit."
5009   (interactive "P")
5010   (let ((func gnus-article-edit-done-function)
5011         (buf (current-buffer))
5012         (start (window-start)))
5013     (remove-hook 'gnus-article-mode-hook
5014                  'gnus-article-mime-edit-article-unwind)
5015     ;; We remove all text props from the article buffer.
5016     (let ((content
5017            (buffer-substring-no-properties (point-min) (point-max)))
5018           (p (point)))
5019       (erase-buffer)
5020       (insert content)
5021       (let ((winconf gnus-prev-winconf))
5022         (gnus-article-mode)
5023         (set-window-configuration winconf)
5024         ;; Tippy-toe some to make sure that point remains where it was.
5025         (save-current-buffer
5026           (set-buffer buf)
5027           (set-window-start (get-buffer-window (current-buffer)) start)
5028           (goto-char p))))
5029     (save-excursion
5030       (set-buffer buf)
5031       (let ((buffer-read-only nil))
5032         (funcall func arg))
5033       ;; The cache and backlog have to be flushed somewhat.
5034       (when gnus-keep-backlog
5035         (gnus-backlog-remove-article
5036          (car gnus-article-current) (cdr gnus-article-current)))
5037       ;; Flush original article as well.
5038       (save-excursion
5039         (when (get-buffer gnus-original-article-buffer)
5040           (set-buffer gnus-original-article-buffer)
5041           (setq gnus-original-article nil)))
5042       (when gnus-use-cache
5043         (gnus-cache-update-article
5044          (car gnus-article-current) (cdr gnus-article-current))))
5045     (set-buffer buf)
5046     (set-window-start (get-buffer-window buf) start)
5047     (set-window-point (get-buffer-window buf) (point))))
5048
5049 (defun gnus-article-edit-exit ()
5050   "Exit the article editing without updating."
5051   (interactive)
5052   (when (or (not (buffer-modified-p))
5053             (yes-or-no-p "Article modified; kill anyway? "))
5054     (let ((curbuf (current-buffer))
5055           (p (point))
5056           (window-start (window-start)))
5057       (erase-buffer)
5058       (if (gnus-buffer-live-p gnus-original-article-buffer)
5059           (insert-buffer gnus-original-article-buffer))
5060       (let ((winconf gnus-prev-winconf))
5061         (gnus-article-mode)
5062         (set-window-configuration winconf)
5063         ;; Tippy-toe some to make sure that point remains where it was.
5064         (save-current-buffer
5065           (set-buffer curbuf)
5066           (set-window-start (get-buffer-window (current-buffer)) window-start)
5067           (goto-char p))))))
5068
5069 (defun gnus-article-edit-full-stops ()
5070   "Interactively repair spacing at end of sentences."
5071   (interactive)
5072   (save-excursion
5073     (goto-char (point-min))
5074     (search-forward-regexp "^$" nil t)
5075     (let ((case-fold-search nil))
5076       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
5077
5078 ;;;
5079 ;;; Article editing with MIME-Edit
5080 ;;;
5081
5082 (defcustom gnus-article-mime-edit-article-setup-hook nil
5083   "Hook run after setting up a MIME editing article buffer."
5084   :group 'gnus-article-various
5085   :type 'hook)
5086
5087 (defun gnus-article-mime-edit-article-unwind ()
5088   "Unwind `gnus-article-buffer' if article editing was given up."
5089   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
5090   (when (featurep 'font-lock)
5091     (setq font-lock-defaults nil)
5092     (font-lock-mode -1))
5093   (when mime-edit-mode-flag
5094     (mime-edit-exit 'nomime 'no-error)
5095     (message "")))
5096
5097 (defun gnus-article-mime-edit-article-setup ()
5098   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
5099 after replacing with the original article."
5100   (setq gnus-show-mime t)
5101   (setq gnus-article-edit-done-function
5102         `(lambda (&rest args)
5103            (when mime-edit-mode-flag
5104              (let (mime-edit-insert-user-agent-field)
5105                (mime-edit-exit))
5106              (message ""))
5107            (goto-char (point-min))
5108            (let (case-fold-search)
5109              (when (re-search-forward
5110                     (format "^%s$" (regexp-quote mail-header-separator))
5111                     nil t)
5112                (replace-match "")))
5113            (apply ,gnus-article-edit-done-function args)
5114            (set-buffer (get-buffer-create gnus-original-article-buffer))
5115            (erase-buffer)
5116            (insert-buffer gnus-article-buffer)
5117            (setq gnus-current-headers (gnus-article-make-full-mail-header))
5118            (gnus-article-prepare-display)))
5119   (substitute-key-definition 'gnus-article-edit-done
5120                              'gnus-article-mime-edit-done
5121                              gnus-article-edit-mode-map)
5122   (substitute-key-definition 'gnus-article-edit-exit
5123                              'gnus-article-mime-edit-exit
5124                              gnus-article-edit-mode-map)
5125   (erase-buffer)
5126   (insert-buffer gnus-original-article-buffer)
5127   (let ((ofn (symbol-function 'mime-edit-decode-single-part-in-buffer)))
5128     (fset 'mime-edit-decode-single-part-in-buffer
5129           (lambda (&rest args)
5130             (if (let ((content-type (car args)))
5131                   (and (eq 'message (mime-content-type-primary-type
5132                                      content-type))
5133                        (eq 'rfc822 (mime-content-type-subtype content-type))))
5134                 (setcar (cdr args) 'not-decode-text))
5135             (apply ofn args)))
5136     (unwind-protect
5137         (mime-edit-again)
5138       (fset 'mime-edit-decode-single-part-in-buffer ofn)))
5139   (when (featurep 'font-lock)
5140     (set (make-local-variable 'font-lock-defaults)
5141          '(message-font-lock-keywords t))
5142     (font-lock-set-defaults)
5143     (turn-on-font-lock))
5144   (set-buffer-modified-p nil)
5145   (delete-other-windows)
5146   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
5147   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
5148
5149 (defun gnus-article-mime-edit-done (&optional arg)
5150   "Update the article MIME edits and exit."
5151   (interactive "P")
5152   (when (featurep 'font-lock)
5153     (setq font-lock-defaults nil)
5154     (font-lock-mode -1))
5155   (gnus-article-edit-done arg))
5156
5157 (defun gnus-article-mime-edit-exit ()
5158   "Exit the article MIME editing without updating."
5159   (interactive)
5160   (when (or (not (buffer-modified-p))
5161             (yes-or-no-p "Article modified; kill anyway? "))
5162     (when (featurep 'font-lock)
5163       (setq font-lock-defaults nil)
5164       (font-lock-mode -1))
5165     (when mime-edit-mode-flag
5166       (let (mime-edit-insert-user-agent-field)
5167         (mime-edit-exit))
5168       (message ""))
5169     (goto-char (point-min))
5170     (let (case-fold-search)
5171       (when (re-search-forward
5172              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
5173         (replace-match "")))
5174     (let ((winconf gnus-prev-winconf))
5175       (insert (prog1
5176                   (buffer-substring-no-properties (point-min) (point-max))
5177                 (set-buffer (get-buffer-create gnus-original-article-buffer))
5178                 (erase-buffer)))
5179       (setq gnus-current-headers (gnus-article-make-full-mail-header))
5180       (gnus-article-prepare-display)
5181       (set-window-configuration winconf))))
5182
5183 ;;;
5184 ;;; Article highlights
5185 ;;;
5186
5187 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
5188
5189 ;;; Internal Variables:
5190
5191 (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\\)\\)"
5192   "Regular expression that matches URLs."
5193   :group 'gnus-article-buttons
5194   :type 'regexp)
5195
5196 (defcustom gnus-button-alist
5197   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
5198      0 t gnus-button-message-id 2)
5199     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
5200     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
5201      1 t
5202      gnus-button-fetch-group 4)
5203     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
5204     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
5205      t gnus-button-message-id 3)
5206     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
5207     ("mailto:\\([-a-zA-Z.@_+0-9%=?]+\\)" 0 t gnus-url-mailto 1)
5208     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
5209     ;; This is how URLs _should_ be embedded in text...
5210     ("<URL: *\\([^<>]*\\)>" 0 t gnus-button-embedded-url 1)
5211     ;; Raw URLs.
5212     (,gnus-button-url-regexp 0 t browse-url 0))
5213   "*Alist of regexps matching buttons in article bodies.
5214
5215 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
5216 REGEXP: is the string matching text around the button,
5217 BUTTON: is the number of the regexp grouping actually matching the button,
5218 FORM: is a lisp expression which must eval to true for the button to
5219 be added,
5220 CALLBACK: is the function to call when the user push this button, and each
5221 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
5222
5223 CALLBACK can also be a variable, in that case the value of that
5224 variable it the real callback function."
5225   :group 'gnus-article-buttons
5226   :type '(repeat (list regexp
5227                        (integer :tag "Button")
5228                        (sexp :tag "Form")
5229                        (function :tag "Callback")
5230                        (repeat :tag "Par"
5231                                :inline t
5232                                (integer :tag "Regexp group")))))
5233
5234 (defcustom gnus-header-button-alist
5235   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
5236      0 t gnus-button-message-id 0)
5237     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
5238     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
5239      0 t gnus-button-mailto 0)
5240     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
5241     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
5242     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
5243     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
5244      gnus-button-message-id 3))
5245   "*Alist of headers and regexps to match buttons in article heads.
5246
5247 This alist is very similar to `gnus-button-alist', except that each
5248 alist has an additional HEADER element first in each entry:
5249
5250 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
5251
5252 HEADER is a regexp to match a header.  For a fuller explanation, see
5253 `gnus-button-alist'."
5254   :group 'gnus-article-buttons
5255   :group 'gnus-article-headers
5256   :type '(repeat (list (regexp :tag "Header")
5257                        regexp
5258                        (integer :tag "Button")
5259                        (sexp :tag "Form")
5260                        (function :tag "Callback")
5261                        (repeat :tag "Par"
5262                                :inline t
5263                                (integer :tag "Regexp group")))))
5264
5265 (defvar gnus-button-regexp nil)
5266 (defvar gnus-button-marker-list nil)
5267 ;; Regexp matching any of the regexps from `gnus-button-alist'.
5268
5269 (defvar gnus-button-last nil)
5270 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
5271
5272 ;;; Commands:
5273
5274 (defun gnus-article-push-button (event)
5275   "Check text under the mouse pointer for a callback function.
5276 If the text under the mouse pointer has a `gnus-callback' property,
5277 call it with the value of the `gnus-data' text property."
5278   (interactive "e")
5279   (set-buffer (window-buffer (posn-window (event-start event))))
5280   (let* ((pos (posn-point (event-start event)))
5281          (data (get-text-property pos 'gnus-data))
5282          (fun (get-text-property pos 'gnus-callback)))
5283     (goto-char pos)
5284     (when fun
5285       (funcall fun data))))
5286
5287 (defun gnus-article-press-button ()
5288   "Check text at point for a callback function.
5289 If the text at point has a `gnus-callback' property,
5290 call it with the value of the `gnus-data' text property."
5291   (interactive)
5292   (let* ((data (get-text-property (point) 'gnus-data))
5293          (fun (get-text-property (point) 'gnus-callback)))
5294     (when fun
5295       (funcall fun data))))
5296
5297 (defun gnus-article-prev-button (n)
5298   "Move point to N buttons backward.
5299 If N is negative, move forward instead."
5300   (interactive "p")
5301   (gnus-article-next-button (- n)))
5302
5303 (defun gnus-article-next-button (n)
5304   "Move point to N buttons forward.
5305 If N is negative, move backward instead."
5306   (interactive "p")
5307   (let ((function (if (< n 0) 'previous-single-property-change
5308                     'next-single-property-change))
5309         (inhibit-point-motion-hooks t)
5310         (backward (< n 0))
5311         (limit (if (< n 0) (point-min) (point-max))))
5312     (setq n (abs n))
5313     (while (and (not (= limit (point)))
5314                 (> n 0))
5315       ;; Skip past the current button.
5316       (when (get-text-property (point) 'gnus-callback)
5317         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5318       ;; Go to the next (or previous) button.
5319       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
5320       ;; Put point at the start of the button.
5321       (when (and backward (not (get-text-property (point) 'gnus-callback)))
5322         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5323       ;; Skip past intangible buttons.
5324       (when (get-text-property (point) 'intangible)
5325         (incf n))
5326       (decf n))
5327     (unless (zerop n)
5328       (gnus-message 5 "No more buttons"))
5329     n))
5330
5331 (defun gnus-article-highlight (&optional force)
5332   "Highlight current article.
5333 This function calls `gnus-article-highlight-headers',
5334 `gnus-article-highlight-citation',
5335 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5336 do the highlighting.  See the documentation for those functions."
5337   (interactive (list 'force))
5338   (gnus-article-highlight-headers)
5339   (gnus-article-highlight-citation force)
5340   (gnus-article-highlight-signature)
5341   (gnus-article-add-buttons force)
5342   (gnus-article-add-buttons-to-head))
5343
5344 (defun gnus-article-highlight-some (&optional force)
5345   "Highlight current article.
5346 This function calls `gnus-article-highlight-headers',
5347 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5348 do the highlighting.  See the documentation for those functions."
5349   (interactive (list 'force))
5350   (gnus-article-highlight-headers)
5351   (gnus-article-highlight-signature)
5352   (gnus-article-add-buttons))
5353
5354 (defun gnus-article-highlight-headers ()
5355   "Highlight article headers as specified by `gnus-header-face-alist'."
5356   (interactive)
5357   (save-excursion
5358     (set-buffer gnus-article-buffer)
5359     (save-restriction
5360       (let ((alist gnus-header-face-alist)
5361             (buffer-read-only nil)
5362             (case-fold-search t)
5363             (inhibit-point-motion-hooks t)
5364             entry regexp header-face field-face from hpoints fpoints)
5365         (article-narrow-to-head)
5366         (while (setq entry (pop alist))
5367           (goto-char (point-min))
5368           (setq regexp (concat "^\\("
5369                                (if (string-equal "" (nth 0 entry))
5370                                    "[^\t ]"
5371                                  (nth 0 entry))
5372                                "\\)")
5373                 header-face (nth 1 entry)
5374                 field-face (nth 2 entry))
5375           (while (and (re-search-forward regexp nil t)
5376                       (not (eobp)))
5377             (beginning-of-line)
5378             (setq from (point))
5379             (unless (search-forward ":" nil t)
5380               (forward-char 1))
5381             (when (and header-face
5382                        (not (memq (point) hpoints)))
5383               (push (point) hpoints)
5384               (gnus-put-text-property from (point) 'face header-face))
5385             (when (and field-face
5386                        (not (memq (setq from (point)) fpoints)))
5387               (push from fpoints)
5388               (if (re-search-forward "^[^ \t]" nil t)
5389                   (forward-char -2)
5390                 (goto-char (point-max)))
5391               (gnus-put-text-property from (point) 'face field-face))))))))
5392
5393 (defun gnus-article-highlight-signature ()
5394   "Highlight the signature in an article.
5395 It does this by highlighting everything after
5396 `gnus-signature-separator' using `gnus-signature-face'."
5397   (interactive)
5398   (when gnus-signature-face
5399     (save-excursion
5400       (set-buffer gnus-article-buffer)
5401       (let ((buffer-read-only nil)
5402             (inhibit-point-motion-hooks t))
5403         (save-restriction
5404           (when (gnus-article-narrow-to-signature)
5405             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
5406                               'face gnus-signature-face)))))))
5407
5408 (defun gnus-article-buttonize-signature ()
5409   "Add button to the signature."
5410   (interactive)
5411   (save-excursion
5412     (set-buffer gnus-article-buffer)
5413     (let ((buffer-read-only nil)
5414           (inhibit-point-motion-hooks t))
5415       (when (gnus-article-search-signature)
5416         (gnus-article-add-button (match-beginning 0) (match-end 0)
5417                                  'gnus-signature-toggle
5418                                  (set-marker (make-marker)
5419                                              (1+ (match-end 0))))))))
5420
5421 (defun gnus-button-in-region-p (b e prop)
5422   "Say whether PROP exists in the region."
5423   (text-property-not-all b e prop nil))
5424
5425 (defun gnus-article-add-buttons (&optional force)
5426   "Find external references in the article and make buttons of them.
5427 \"External references\" are things like Message-IDs and URLs, as
5428 specified by `gnus-button-alist'."
5429   (interactive (list 'force))
5430   (save-excursion
5431     (set-buffer gnus-article-buffer)
5432     (let ((buffer-read-only nil)
5433           (inhibit-point-motion-hooks t)
5434           (case-fold-search t)
5435           (alist gnus-button-alist)
5436           beg entry regexp)
5437       ;; Remove all old markers.
5438       (let (marker entry new-list)
5439         (while (setq marker (pop gnus-button-marker-list))
5440           (if (or (< marker (point-min)) (>= marker (point-max)))
5441               (push marker new-list)
5442             (goto-char marker)
5443             (when (setq entry (gnus-button-entry))
5444               (put-text-property (match-beginning (nth 1 entry))
5445                                  (match-end (nth 1 entry))
5446                                  'gnus-callback nil))
5447             (set-marker marker nil)))
5448         (setq gnus-button-marker-list new-list))
5449       ;; We skip the headers.
5450       (article-goto-body)
5451       (setq beg (point))
5452       (while (setq entry (pop alist))
5453         (setq regexp (car entry))
5454         (goto-char beg)
5455         (while (re-search-forward regexp nil t)
5456           (let* ((start (and entry (match-beginning (nth 1 entry))))
5457                  (end (and entry (match-end (nth 1 entry))))
5458                  (from (match-beginning 0)))
5459             (when (and (or (eq t (nth 2 entry))
5460                            (eval (nth 2 entry)))
5461                        (not (gnus-button-in-region-p
5462                              start end 'gnus-callback)))
5463               ;; That optional form returned non-nil, so we add the
5464               ;; button.
5465               (gnus-article-add-button
5466                start end 'gnus-button-push
5467                (car (push (set-marker (make-marker) from)
5468                           gnus-button-marker-list))))))))))
5469
5470 ;; Add buttons to the head of an article.
5471 (defun gnus-article-add-buttons-to-head ()
5472   "Add buttons to the head of the article."
5473   (interactive)
5474   (save-excursion
5475     (set-buffer gnus-article-buffer)
5476     (save-restriction
5477       (let ((buffer-read-only nil)
5478             (inhibit-point-motion-hooks t)
5479             (case-fold-search t)
5480             (alist gnus-header-button-alist)
5481             entry beg end)
5482         (article-narrow-to-head)
5483         (while alist
5484           ;; Each alist entry.
5485           (setq entry (car alist)
5486                 alist (cdr alist))
5487           (goto-char (point-min))
5488           (while (re-search-forward (car entry) nil t)
5489             ;; Each header matching the entry.
5490             (setq beg (match-beginning 0))
5491             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
5492                                (match-beginning 0))
5493                           (point-max)))
5494             (goto-char beg)
5495             (while (re-search-forward (nth 1 entry) end t)
5496               ;; Each match within a header.
5497               (let* ((entry (cdr entry))
5498                      (start (match-beginning (nth 1 entry)))
5499                      (end (match-end (nth 1 entry)))
5500                      (form (nth 2 entry)))
5501                 (goto-char (match-end 0))
5502                 (when (eval form)
5503                   (gnus-article-add-button
5504                    start end (nth 3 entry)
5505                    (buffer-substring (match-beginning (nth 4 entry))
5506                                      (match-end (nth 4 entry)))))))
5507             (goto-char end)))))))
5508
5509 ;;; External functions:
5510
5511 (defun gnus-article-add-button (from to fun &optional data)
5512   "Create a button between FROM and TO with callback FUN and data DATA."
5513   (when gnus-article-button-face
5514     (gnus-overlay-put (gnus-make-overlay from to)
5515                       'face gnus-article-button-face))
5516   (gnus-add-text-properties
5517    from to
5518    (nconc (and gnus-article-mouse-face
5519                (list gnus-mouse-face-prop gnus-article-mouse-face))
5520           (list 'gnus-callback fun)
5521           (and data (list 'gnus-data data))))
5522   (widget-convert-button 'link from to :action 'gnus-widget-press-button
5523                          ;; Quote `:button-keymap' for Mule 2.3
5524                          ;; but it won't work.
5525                          ':button-keymap gnus-widget-button-keymap))
5526
5527 ;;; Internal functions:
5528
5529 (defun gnus-article-set-globals ()
5530   (save-excursion
5531     (set-buffer gnus-summary-buffer)
5532     (gnus-set-global-variables)))
5533
5534 (defun gnus-signature-toggle (end)
5535   (save-excursion
5536     (set-buffer gnus-article-buffer)
5537     (let ((buffer-read-only nil)
5538           (inhibit-point-motion-hooks t)
5539           (limit (next-single-property-change end 'mime-view-entity
5540                                               nil (point-max))))
5541       (if (text-property-any end limit 'article-type 'signature)
5542           (gnus-remove-text-properties-when
5543            'article-type 'signature end limit
5544            (cons 'article-type (cons 'signature
5545                                      gnus-hidden-properties)))
5546         (gnus-add-text-properties-when
5547          'article-type nil end limit
5548          (cons 'article-type (cons 'signature
5549                                    gnus-hidden-properties)))))))
5550
5551 (defun gnus-button-entry ()
5552   ;; Return the first entry in `gnus-button-alist' matching this place.
5553   (let ((alist gnus-button-alist)
5554         (entry nil))
5555     (while alist
5556       (setq entry (pop alist))
5557       (if (looking-at (car entry))
5558           (setq alist nil)
5559         (setq entry nil)))
5560     entry))
5561
5562 (defun gnus-button-push (marker)
5563   ;; Push button starting at MARKER.
5564   (save-excursion
5565     (goto-char marker)
5566     (let* ((entry (gnus-button-entry))
5567            (inhibit-point-motion-hooks t)
5568            (fun (nth 3 entry))
5569            (args (mapcar (lambda (group)
5570                            (let ((string (match-string group)))
5571                              (gnus-set-text-properties
5572                               0 (length string) nil string)
5573                              string))
5574                          (nthcdr 4 entry))))
5575       (cond
5576        ((fboundp fun)
5577         (apply fun args))
5578        ((and (boundp fun)
5579              (fboundp (symbol-value fun)))
5580         (apply (symbol-value fun) args))
5581        (t
5582         (gnus-message 1 "You must define `%S' to use this button"
5583                       (cons fun args)))))))
5584
5585 (defun gnus-button-message-id (message-id)
5586   "Fetch MESSAGE-ID."
5587   (save-excursion
5588     (set-buffer gnus-summary-buffer)
5589     (gnus-summary-refer-article message-id)))
5590
5591 (defun gnus-button-fetch-group (address)
5592   "Fetch GROUP specified by ADDRESS."
5593   (if (not (string-match "[:/]" address))
5594       ;; This is just a simple group url.
5595       (gnus-group-read-ephemeral-group address gnus-select-method)
5596     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
5597                            address))
5598         (error "Can't parse %s" address)
5599       (gnus-group-read-ephemeral-group
5600        (match-string 4 address)
5601        `(nntp ,(match-string 1 address)
5602               (nntp-address ,(match-string 1 address))
5603               (nntp-port-number ,(if (match-end 3)
5604                                      (match-string 3 address)
5605                                    "nntp")))))))
5606
5607 (defun gnus-url-parse-query-string (query &optional downcase)
5608   (let (retval pairs cur key val)
5609     (setq pairs (split-string query "&"))
5610     (while pairs
5611       (setq cur (car pairs)
5612             pairs (cdr pairs))
5613       (if (not (string-match "=" cur))
5614           nil                           ; Grace
5615         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
5616               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
5617         (if downcase
5618             (setq key (downcase key)))
5619         (setq cur (assoc key retval))
5620         (if cur
5621             (setcdr cur (cons val (cdr cur)))
5622           (setq retval (cons (list key val) retval)))))
5623     retval))
5624
5625 (defun gnus-url-unhex (x)
5626   (if (> x ?9)
5627       (if (>= x ?a)
5628           (+ 10 (- x ?a))
5629         (+ 10 (- x ?A)))
5630     (- x ?0)))
5631
5632 (defun gnus-url-unhex-string (str &optional allow-newlines)
5633   "Remove %XXX embedded spaces, etc in a url.
5634 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
5635 decoding of carriage returns and line feeds in the string, which is normally
5636 forbidden in URL encoding."
5637   (setq str (or str ""))
5638   (let ((tmp "")
5639         (case-fold-search t))
5640     (while (string-match "%[0-9a-f][0-9a-f]" str)
5641       (let* ((start (match-beginning 0))
5642              (ch1 (gnus-url-unhex (elt str (+ start 1))))
5643              (code (+ (* 16 ch1)
5644                       (gnus-url-unhex (elt str (+ start 2))))))
5645         (setq tmp (concat
5646                    tmp (substring str 0 start)
5647                    (cond
5648                     (allow-newlines
5649                      (char-to-string code))
5650                     ((or (= code ?\n) (= code ?\r))
5651                      " ")
5652                     (t (char-to-string code))))
5653               str (substring str (match-end 0)))))
5654     (setq tmp (concat tmp str))
5655     tmp))
5656
5657 (defun gnus-url-mailto (url)
5658   ;; Send mail to someone
5659   (when (string-match "mailto:/*\\(.*\\)" url)
5660     (setq url (substring url (match-beginning 1) nil)))
5661   (let (to args subject func)
5662     (if (string-match (regexp-quote "?") url)
5663         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
5664               args (gnus-url-parse-query-string
5665                     (substring url (match-end 0) nil) t))
5666       (setq to (gnus-url-unhex-string url)))
5667     (setq args (cons (list "to" to) args)
5668           subject (cdr-safe (assoc "subject" args)))
5669     (gnus-msg-mail)
5670     (while args
5671       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
5672       (if (fboundp func)
5673           (funcall func)
5674         (message-position-on-field (caar args)))
5675       (insert (mapconcat 'identity (cdar args) ", "))
5676       (setq args (cdr args)))
5677     (if subject
5678         (message-goto-body)
5679       (message-goto-subject))))
5680
5681 (defun gnus-button-embedded-url (address)
5682   "Activate ADDRESS with `browse-url'."
5683   (browse-url (gnus-strip-whitespace address)))
5684
5685 (eval-when-compile
5686   ;; Silence the byte-compiler.
5687   (autoload 'smiley-toggle-buffer "gnus-bitmap"))
5688 (defun gnus-article-smiley-display ()
5689   "Display \"smileys\" as small graphical icons."
5690   (smiley-toggle-buffer 1 (current-buffer) (point-min) (point-max)))
5691
5692 ;;; Next/prev buttons in the article buffer.
5693
5694 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
5695 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
5696
5697 (defvar gnus-prev-page-map nil)
5698 (unless gnus-prev-page-map
5699   (setq gnus-prev-page-map (make-sparse-keymap))
5700   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
5701   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
5702
5703 (static-if (featurep 'xemacs)
5704     (defun gnus-insert-prev-page-button ()
5705       (let ((buffer-read-only nil))
5706         (gnus-eval-format
5707          gnus-prev-page-line-format nil
5708          `(gnus-prev t local-map ,gnus-prev-page-map
5709                      gnus-callback gnus-article-button-prev-page
5710                      article-type annotation))))
5711   (defun gnus-insert-prev-page-button ()
5712     (let ((buffer-read-only nil)
5713           (situation (get-text-property (point-min) 'mime-view-situation)))
5714       (set-keymap-parent gnus-prev-page-map (current-local-map))
5715       (gnus-eval-format
5716        gnus-prev-page-line-format nil
5717        `(gnus-prev t local-map ,gnus-prev-page-map
5718                    gnus-callback gnus-article-button-prev-page
5719                    article-type annotation
5720                    mime-view-situation ,situation))))
5721   )
5722
5723 (defvar gnus-next-page-map nil)
5724 (unless gnus-next-page-map
5725   (setq gnus-next-page-map (make-sparse-keymap))
5726   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
5727   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
5728
5729 (defun gnus-button-next-page ()
5730   "Go to the next page."
5731   (interactive)
5732   (let ((win (selected-window)))
5733     (select-window (get-buffer-window gnus-article-buffer t))
5734     (gnus-article-next-page)
5735     (select-window win)))
5736
5737 (defun gnus-button-prev-page ()
5738   "Go to the prev page."
5739   (interactive)
5740   (let ((win (selected-window)))
5741     (select-window (get-buffer-window gnus-article-buffer t))
5742     (gnus-article-prev-page)
5743     (select-window win)))
5744
5745 (static-if (featurep 'xemacs)
5746     (defun gnus-insert-next-page-button ()
5747       (let ((buffer-read-only nil))
5748         (gnus-eval-format gnus-next-page-line-format nil
5749                           `(gnus-next
5750                             t local-map ,gnus-next-page-map
5751                             gnus-callback gnus-article-button-next-page
5752                             article-type annotation))))
5753   (defun gnus-insert-next-page-button ()
5754     (let ((buffer-read-only nil)
5755           (situation (get-text-property (point-min) 'mime-view-situation)))
5756       (set-keymap-parent gnus-next-page-map (current-local-map))
5757       (gnus-eval-format gnus-next-page-line-format nil
5758                         `(gnus-next
5759                           t local-map ,gnus-next-page-map
5760                           gnus-callback gnus-article-button-next-page
5761                           article-type annotation
5762                           mime-view-situation ,situation))))
5763   )
5764
5765 (defun gnus-article-button-next-page (arg)
5766   "Go to the next page."
5767   (interactive "P")
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-article-button-prev-page (arg)
5774   "Go to the prev page."
5775   (interactive "P")
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 (defvar gnus-decode-header-methods
5782   '(mail-decode-encoded-word-region)
5783   "List of methods used to decode headers.
5784
5785 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
5786 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
5787 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
5788 whose names match REGEXP.
5789
5790 For example:
5791 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
5792  mail-decode-encoded-word-region
5793  (\"chinese\" . rfc1843-decode-region))
5794 ")
5795
5796 (defvar gnus-decode-header-methods-cache nil)
5797
5798 (defun gnus-multi-decode-header (start end)
5799   "Apply the functions from `gnus-encoded-word-methods' that match."
5800   (unless (and gnus-decode-header-methods-cache
5801                (eq gnus-newsgroup-name
5802                    (car gnus-decode-header-methods-cache)))
5803     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
5804     (mapcar (lambda (x)
5805               (if (symbolp x)
5806                   (nconc gnus-decode-header-methods-cache (list x))
5807                 (if (and gnus-newsgroup-name
5808                          (string-match (car x) gnus-newsgroup-name))
5809                     (nconc gnus-decode-header-methods-cache
5810                            (list (cdr x))))))
5811           gnus-decode-header-methods))
5812   (let ((xlist gnus-decode-header-methods-cache))
5813     (pop xlist)
5814     (save-restriction
5815       (narrow-to-region start end)
5816       (while xlist
5817         (funcall (pop xlist) (point-min) (point-max))))))
5818
5819 ;;;
5820 ;;; Treatment top-level handling.
5821 ;;;
5822
5823 (defun gnus-treat-article (condition &optional part-number total-parts type)
5824   (let ((length (- (point-max) (point-min)))
5825         (alist gnus-treatment-function-alist)
5826         (article-goto-body-goes-to-point-min-p t)
5827         (treated-type
5828          (or (not type)
5829              (catch 'found
5830                (let ((list gnus-article-treat-types))
5831                  (while list
5832                    (when (string-match (pop list) type)
5833                      (throw 'found t)))))))
5834         (highlightp (gnus-visual-p 'article-highlight 'highlight))
5835         (entity (static-unless (featurep 'xemacs)
5836                   (when (eq 'head condition)
5837                     (get-text-property (point-min) 'mime-view-entity))))
5838         val elem buttonized)
5839     (gnus-run-hooks 'gnus-part-display-hook)
5840     (unless gnus-inhibit-treatment
5841       (while (setq elem (pop alist))
5842         (setq val
5843               (save-excursion
5844                 (if (gnus-buffer-live-p gnus-summary-buffer)
5845                     (set-buffer gnus-summary-buffer))
5846                 (symbol-value (car elem))))
5847         (when (and (or (consp val)
5848                        treated-type)
5849                    (gnus-treat-predicate val)
5850                    (or (not (get (car elem) 'highlight))
5851                        highlightp))
5852           (when (and (not buttonized)
5853                      (memq (car elem)
5854                            '(gnus-treat-hide-signature
5855                              gnus-treat-highlight-signature)))
5856             (gnus-article-buttonize-signature)
5857             (setq buttonized t))
5858           (save-restriction
5859             (funcall (cadr elem)))))
5860       ;; FSF Emacsen does not inherit the existing text properties
5861       ;; in the new text, so we should do it for `mime-view-entity'.
5862       (static-unless (featurep 'xemacs)
5863         (when entity
5864           (put-text-property (point-min) (point-max)
5865                              'mime-view-entity entity))))))
5866
5867 ;; Dynamic variables.
5868 (eval-when-compile
5869   (defvar part-number)
5870   (defvar total-parts)
5871   (defvar type)
5872   (defvar condition)
5873   (defvar length))
5874
5875 (defun gnus-treat-predicate (val)
5876   (cond
5877    ((null val)
5878     nil)
5879    ((and (listp val)
5880          (stringp (car val)))
5881     (apply 'gnus-or (mapcar `(lambda (s)
5882                                (string-match s ,(or gnus-newsgroup-name "")))
5883                             val)))
5884    ((listp val)
5885     (let ((pred (pop val)))
5886       (cond
5887        ((eq pred 'or)
5888         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
5889        ((eq pred 'and)
5890         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
5891        ((eq pred 'not)
5892         (not (gnus-treat-predicate (car val))))
5893        ((eq pred 'typep)
5894         (equal (car val) type))
5895        (t
5896         (error "%S is not a valid predicate" pred)))))
5897    ((eq val 'mime)
5898     gnus-show-mime)
5899    (condition
5900     (eq condition val))
5901    ((eq val t)
5902     t)
5903    ((eq val 'head)
5904     nil)
5905    ((eq val 'last)
5906     (eq part-number total-parts))
5907    ((numberp val)
5908     (< length val))
5909    (t
5910     (error "%S is not a valid value" val))))
5911
5912 (defun gnus-article-encrypt-body (protocol &optional n)
5913   "Encrypt the article body."
5914   (interactive
5915    (list
5916     (or gnus-article-encrypt-protocol
5917         (completing-read "Encrypt protocol: "
5918                          gnus-article-encrypt-protocol-alist
5919                          nil t))
5920     current-prefix-arg))
5921   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
5922     (unless func
5923       (error (format "Can't find the encrypt protocol %s" protocol)))
5924     (if (equal gnus-newsgroup-name "nndraft:drafts")
5925         (error "Can't encrypt the article in group nndraft:drafts."))
5926     (if (equal gnus-newsgroup-name "nndraft:queue")
5927         (error "Don't encrypt the article in group nndraft:queue."))
5928     (gnus-summary-iterate n
5929       (save-excursion
5930         (set-buffer gnus-summary-buffer)
5931         (let ((mail-parse-charset gnus-newsgroup-charset)
5932               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5933               (summary-buffer gnus-summary-buffer)
5934               references point)
5935           (gnus-set-global-variables)
5936           (when (gnus-group-read-only-p)
5937             (error "The current newsgroup does not support article encrypt"))
5938           (gnus-summary-show-article t)
5939           (setq references
5940               (or (mail-header-references gnus-current-headers) ""))
5941           (set-buffer gnus-article-buffer)
5942           (let* ((buffer-read-only nil)
5943                  (headers
5944                   (mapcar (lambda (field)
5945                             (and (save-restriction
5946                                    (message-narrow-to-head)
5947                                    (goto-char (point-min))
5948                                    (search-forward field nil t))
5949                                  (prog2
5950                                      (message-narrow-to-field)
5951                                      (buffer-substring (point-min) (point-max))
5952                                    (delete-region (point-min) (point-max))
5953                                    (widen))))
5954                           '("Content-Type:" "Content-Transfer-Encoding:"
5955                             "Content-Disposition:"))))
5956             (message-narrow-to-head)
5957             (message-remove-header "MIME-Version")
5958             (goto-char (point-max))
5959             (setq point (point))
5960             (insert (apply 'concat headers))
5961             (widen)
5962             (narrow-to-region point (point-max))
5963             (let ((message-options message-options))
5964               (message-options-set 'message-sender user-mail-address)
5965               (message-options-set 'message-recipients user-mail-address)
5966               (message-options-set 'message-sign-encrypt 'not)
5967               (funcall func))
5968             (goto-char (point-min))
5969             (insert "MIME-Version: 1.0\n")
5970             (widen)
5971             (gnus-summary-edit-article-done
5972              references nil summary-buffer t))
5973           (when gnus-keep-backlog
5974             (gnus-backlog-remove-article
5975              (car gnus-article-current) (cdr gnus-article-current)))
5976           (save-excursion
5977             (when (get-buffer gnus-original-article-buffer)
5978               (set-buffer gnus-original-article-buffer)
5979               (setq gnus-original-article nil)))
5980           (when gnus-use-cache
5981             (gnus-cache-update-article
5982              (car gnus-article-current) (cdr gnus-article-current))))))))
5983
5984 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
5985   "The following specs can be used:
5986 %t  The security MIME type
5987 %i  Additional info
5988 %d  Details
5989 %D  Details if button is pressed")
5990
5991 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
5992   "The following specs can be used:
5993 %t  The security MIME type
5994 %i  Additional info
5995 %d  Details
5996 %D  Details if button is pressed")
5997
5998 (defvar gnus-mime-security-button-line-format-alist
5999   '((?t gnus-tmp-type ?s)
6000     (?i gnus-tmp-info ?s)
6001     (?d gnus-tmp-details ?s)
6002     (?D gnus-tmp-pressed-details ?s)))
6003
6004 (defvar gnus-mime-security-button-map
6005   (let ((map (make-sparse-keymap)))
6006     (unless (>= (string-to-number emacs-version) 21)
6007       (set-keymap-parent map gnus-article-mode-map))
6008     (define-key map gnus-mouse-2 'gnus-article-push-button)
6009     (define-key map "\r" 'gnus-article-press-button)
6010     map))
6011
6012 (defvar gnus-mime-security-details-buffer nil)
6013
6014 (defvar gnus-mime-security-button-pressed nil)
6015
6016 (defvar gnus-mime-security-show-details-inline t
6017   "If non-nil, show details in the article buffer.")
6018
6019 (defun gnus-mime-security-verify-or-decrypt (handle)
6020   (mm-remove-parts (cdr handle))
6021   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
6022         buffer-read-only)
6023     (when region
6024       (delete-region (car region) (cdr region))
6025       (set-marker (car region) nil)
6026       (set-marker (cdr region) nil)))
6027   (with-current-buffer (mm-handle-multipart-original-buffer handle)
6028     (let* ((mm-verify-option 'known)
6029            (mm-decrypt-option 'known)
6030            (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
6031       (unless (eq nparts (cdr handle))
6032         (mm-destroy-parts (cdr handle))
6033         (setcdr handle nparts))))
6034   (let ((point (point))
6035         buffer-read-only)
6036     (gnus-mime-display-security handle)
6037     (goto-char point)))
6038
6039 (defun gnus-mime-security-show-details (handle)
6040   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
6041     (if details
6042         (if gnus-mime-security-show-details-inline
6043             (let ((gnus-mime-security-button-pressed t)
6044                   (gnus-mime-security-button-line-format
6045                    (get-text-property (point) 'gnus-line-format))
6046                 buffer-read-only)
6047               (forward-char -1)
6048               (while (eq (get-text-property (point) 'gnus-line-format)
6049                          gnus-mime-security-button-line-format)
6050                 (forward-char -1))
6051               (forward-char)
6052               (delete-region (point)
6053                              (or (text-property-not-all
6054                                   (point) (point-max)
6055                                 'gnus-line-format
6056                                 gnus-mime-security-button-line-format)
6057                                  (point-max)))
6058               (gnus-insert-mime-security-button handle))
6059           (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
6060               (with-current-buffer gnus-mime-security-details-buffer
6061                 (erase-buffer)
6062                 t)
6063             (setq gnus-mime-security-details-buffer
6064                   (gnus-get-buffer-create "*MIME Security Details*")))
6065           (with-current-buffer gnus-mime-security-details-buffer
6066             (insert details)
6067             (goto-char (point-min)))
6068           (pop-to-buffer gnus-mime-security-details-buffer))
6069       (gnus-message 5 "No details."))))
6070
6071 (defun gnus-mime-security-press-button (handle)
6072   (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
6073       (gnus-mime-security-show-details handle)
6074     (gnus-mime-security-verify-or-decrypt handle)))
6075
6076 (defun gnus-insert-mime-security-button (handle &optional displayed)
6077   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
6078          (gnus-tmp-type
6079           (concat
6080            (or (nth 2 (assoc protocol mm-verify-function-alist))
6081                (nth 2 (assoc protocol mm-decrypt-function-alist))
6082                "Unknown")
6083            (if (equal (car handle) "multipart/signed")
6084                " Signed" " Encrypted")
6085            " Part"))
6086          (gnus-tmp-info
6087           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
6088               "Undecided"))
6089          (gnus-tmp-details
6090           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
6091          gnus-tmp-pressed-details
6092          b e)
6093     (setq gnus-tmp-details
6094           (if gnus-tmp-details
6095               (concat "\n" gnus-tmp-details) ""))
6096     (setq gnus-tmp-pressed-details
6097           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
6098     (unless (bolp)
6099       (insert "\n"))
6100     (setq b (point))
6101     (gnus-eval-format
6102      gnus-mime-security-button-line-format
6103      gnus-mime-security-button-line-format-alist
6104      `(keymap ,gnus-mime-security-button-map
6105               ,@(if (>= (string-to-number emacs-version) 21)
6106                     nil ;; XEmacs doesn't care
6107                   (list 'local-map gnus-mime-security-button-map))
6108               gnus-callback gnus-mime-security-press-button
6109               gnus-line-format ,gnus-mime-security-button-line-format
6110               article-type annotation
6111               gnus-data ,handle))
6112     (setq e (point))
6113     (widget-convert-button
6114      'link b e
6115      :mime-handle handle
6116      :action 'gnus-widget-press-button
6117      :button-keymap gnus-mime-security-button-map
6118      :help-echo
6119      (lambda (widget/window &optional overlay pos)
6120        ;; Needed to properly clear the message due to a bug in
6121        ;; wid-edit (XEmacs only).
6122        (if (boundp 'help-echo-owns-message)
6123            (setq help-echo-owns-message t))
6124        (format
6125         "%S: show detail"
6126         (aref gnus-mouse-2 0))))))
6127
6128 (defun gnus-mime-display-security (handle)
6129   (save-restriction
6130     (narrow-to-region (point) (point))
6131     (gnus-insert-mime-security-button handle)
6132     (gnus-mime-display-mixed (cdr handle))
6133     (unless (bolp)
6134       (insert "\n"))
6135     (let ((gnus-mime-security-button-line-format
6136            gnus-mime-security-button-end-line-format))
6137       (gnus-insert-mime-security-button handle))
6138     (mm-set-handle-multipart-parameter
6139      handle 'gnus-region
6140      (cons (set-marker (make-marker) (point-min))
6141            (set-marker (make-marker) (point-max))))))
6142
6143
6144 ;;; @ for mime-view
6145 ;;;
6146
6147 (defun gnus-article-header-presentation-method (entity situation)
6148   (mime-insert-header entity)
6149   )
6150
6151 (set-alist 'mime-header-presentation-method-alist
6152            'gnus-original-article-mode
6153            #'gnus-article-header-presentation-method)
6154
6155 (defun gnus-mime-preview-quitting-method ()
6156   (mime-preview-kill-buffer)
6157   (delete-other-windows)
6158   (gnus-article-show-summary)
6159   (gnus-summary-select-article gnus-show-all-headers t))
6160
6161 (set-alist 'mime-preview-quitting-method-alist
6162            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
6163
6164 (set-alist 'mime-preview-following-method-alist
6165            'gnus-original-article-mode #'gnus-following-method)
6166
6167 (set-alist 'mime-preview-over-to-previous-method-alist
6168            'gnus-original-article-mode
6169            (lambda ()
6170              (if (> (point-min) 1)
6171                  (gnus-article-prev-page)
6172                (gnus-article-read-summary-keys
6173                 nil (gnus-character-to-event ?P)))))
6174
6175 (set-alist 'mime-preview-over-to-next-method-alist
6176            'gnus-original-article-mode'
6177            (lambda ()
6178              (if (< (point-max) (buffer-size))
6179                  (gnus-article-next-page)
6180                (gnus-article-read-summary-keys
6181                 nil (gnus-character-to-event ?N)))))
6182
6183
6184 ;;; @ end
6185 ;;;
6186
6187 (gnus-ems-redefine)
6188
6189 (provide 'gnus-art)
6190
6191 (run-hooks 'gnus-art-load-hook)
6192
6193 ;;; gnus-art.el ends here