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