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