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