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