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