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