Synch with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001
3 ;;        Free Software Foundation, Inc.
4
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
7 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
8 ;; Keywords: mail, news, MIME
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;;; Code:
30
31 (eval-when-compile (require 'cl))
32 (eval-when-compile (require 'static))
33
34 (require 'path-util)
35 (require 'gnus)
36 (require 'gnus-sum)
37 (require 'gnus-spec)
38 (require 'gnus-int)
39 (require 'alist)
40 (require 'mime-view)
41 (require 'wid-edit)
42
43 ;; Avoid byte-compile warnings.
44 (eval-when-compile
45   (require 'mm-bodies)
46   (require 'mail-parse)
47   (require 'mm-decode)
48   (require 'mm-view)
49   (require 'mm-uu)
50   )
51
52 (autoload 'gnus-msg-mail "gnus-msg" nil t)
53 (autoload 'gnus-button-mailto "gnus-msg")
54 (autoload 'gnus-button-reply "gnus-msg" nil t)
55
56 (defgroup gnus-article nil
57   "Article display."
58   :link '(custom-manual "(gnus)The Article Buffer")
59   :group 'gnus)
60
61 (defgroup gnus-article-treat nil
62   "Treating article parts."
63   :link '(custom-manual "(gnus)Article Hiding")
64   :group 'gnus-article)
65
66 (defgroup gnus-article-hiding nil
67   "Hiding article parts."
68   :link '(custom-manual "(gnus)Article Hiding")
69   :group 'gnus-article)
70
71 (defgroup gnus-article-highlight nil
72   "Article highlighting."
73   :link '(custom-manual "(gnus)Article Highlighting")
74   :group 'gnus-article
75   :group 'gnus-visual)
76
77 (defgroup gnus-article-signature nil
78   "Article signatures."
79   :link '(custom-manual "(gnus)Article Signature")
80   :group 'gnus-article)
81
82 (defgroup gnus-article-headers nil
83   "Article headers."
84   :link '(custom-manual "(gnus)Hiding Headers")
85   :group 'gnus-article)
86
87 (defgroup gnus-article-washing nil
88   "Special commands on articles."
89   :link '(custom-manual "(gnus)Article Washing")
90   :group 'gnus-article)
91
92 (defgroup gnus-article-emphasis nil
93   "Fontisizing articles."
94   :link '(custom-manual "(gnus)Article Fontisizing")
95   :group 'gnus-article)
96
97 (defgroup gnus-article-saving nil
98   "Saving articles."
99   :link '(custom-manual "(gnus)Saving Articles")
100   :group 'gnus-article)
101
102 (defgroup gnus-article-mime nil
103   "Worshiping the MIME wonder."
104   :link '(custom-manual "(gnus)Using MIME")
105   :group 'gnus-article)
106
107 (defgroup gnus-article-buttons nil
108   "Pushable buttons in the article buffer."
109   :link '(custom-manual "(gnus)Article Buttons")
110   :group 'gnus-article)
111
112 (defgroup gnus-article-various nil
113   "Other article options."
114   :link '(custom-manual "(gnus)Misc Article")
115   :group 'gnus-article)
116
117 (defcustom gnus-ignored-headers
118   '("^Path:" "^Expires:" "^Date-Received:" "^References:" "^Xref:" "^Lines:"
119     "^Relay-Version:" "^Message-ID:" "^Approved:" "^Sender:" "^Received:"
120     "^X-UIDL:" "^MIME-Version:" "^Return-Path:" "^In-Reply-To:"
121     "^Content-Type:" "^Content-Transfer-Encoding:" "^X-WebTV-Signature:"
122     "^X-MimeOLE:" "^X-MSMail-Priority:" "^X-Priority:" "^X-Loop:"
123     "^X-Authentication-Warning:" "^X-MIME-Autoconverted:" "^X-Face:"
124     "^X-Attribution:" "^X-Originating-IP:" "^Delivered-To:"
125     "^NNTP-[-A-Za-z]+:" "^Distribution:" "^X-no-archive:" "^X-Trace:"
126     "^X-Complaints-To:" "^X-NNTP-Posting-Host:" "^X-Orig.*:"
127     "^Abuse-Reports-To:" "^Cache-Post-Path:" "^X-Article-Creation-Date:"
128     "^X-Poster:" "^X-Mail2News-Path:" "^X-Server-Date:" "^X-Cache:"
129     "^Originator:" "^X-Problems-To:" "^X-Auth-User:" "^X-Post-Time:"
130     "^X-Admin:" "^X-UID:" "^Resent-[-A-Za-z]+:" "^X-Mailing-List:"
131     "^Precedence:" "^Original-[-A-Za-z]+:" "^X-filename:" "^X-Orcpt:"
132     "^Old-Received:" "^X-Pgp" "^X-Auth:" "^X-From-Line:"
133     "^X-Gnus-Article-Number:" "^X-Majordomo:" "^X-Url:" "^X-Sender:"
134     "^MBOX-Line" "^Priority:" "^X400-[-A-Za-z]+:"
135     "^Status:" "^X-Gnus-Mail-Source:" "^Cancel-Lock:"
136     "^X-FTN" "^X-EXP32-SerialNo:" "^Encoding:" "^Importance:"
137     "^Autoforwarded:" "^Original-Encoded-Information-Types:" "^X-Ya-Pop3:"
138     "^X-Face-Version:" "^X-Vms-To:" "^X-ML-NAME:" "^X-ML-COUNT:"
139     "^Mailing-List:" "^X-finfo:" "^X-md5sum:" "^X-md5sum-Origin:"
140     "^X-Sun-Charset:" "^X-Accept-Language:" "^X-Envelope-Sender:"
141     "^List-[A-Za-z]+:" "^X-Listprocessor-Version:"
142     "^X-Received:" "^X-Distribute:" "^X-Sequence:" "^X-Juno-Line-Breaks:"
143     "^X-Notes-Item:" "^X-MS-TNEF-Correlator:" "^x-uunet-gateway:"
144     "^X-Received:" "^Content-length:" "X-precedence:"
145     "^X-Authenticated-User:" "^X-Comment" "^X-Report:" "^X-Abuse-Info:"
146     "^X-HTTP-Proxy:" "^X-Mydeja-Info:" "^X-Copyright" "^X-No-Markup:"
147     "^X-Abuse-Info:")
148   "*All headers that start with this regexp will be hidden.
149 This variable can also be a list of regexps of headers to be ignored.
150 If `gnus-visible-headers' is non-nil, this variable will be ignored."
151   :type '(choice :custom-show nil
152                  regexp
153                  (repeat regexp))
154   :group 'gnus-article-hiding)
155
156 (defcustom gnus-visible-headers
157   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Mail-Followup-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
158   "*All headers that do not match this regexp will be hidden.
159 This variable can also be a list of regexp of headers to remain visible.
160 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
161   :type '(repeat :value-to-internal (lambda (widget value)
162                                       (custom-split-regexp-maybe value))
163                  :match (lambda (widget value)
164                           (or (stringp value)
165                               (widget-editable-list-match widget value)))
166                  regexp)
167   :group 'gnus-article-hiding)
168
169 (defcustom gnus-sorted-header-list
170   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
171     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
172   "*This variable is a list of regular expressions.
173 If it is non-nil, headers that match the regular expressions will
174 be placed first in the article buffer in the sequence specified by
175 this list."
176   :type '(repeat regexp)
177   :group 'gnus-article-hiding)
178
179 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
180   "Headers that are only to be displayed if they have interesting data.
181 Possible values in this list are `empty', `newsgroups', `followup-to',
182 `to-address', `reply-to', `date', `long-to', and `many-to'."
183   :type '(set (const :tag "Headers with no content." empty)
184               (const :tag "Newsgroups with only one group." newsgroups)
185               (const :tag "To identical to to-address." to-address)
186               (const :tag "Followup-to identical to newsgroups." followup-to)
187               (const :tag "Reply-to identical to from." reply-to)
188               (const :tag "Date less than four days old." date)
189               (const :tag "Very long To and/or Cc header." long-to)
190               (const :tag "Multiple To and/or Cc headers." many-to))
191   :group 'gnus-article-hiding)
192
193 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
194   "Regexp matching signature separator.
195 This can also be a list of regexps.  In that case, it will be checked
196 from head to tail looking for a separator.  Searches will be done from
197 the end of the buffer."
198   :type '(repeat string)
199   :group 'gnus-article-signature)
200
201 (defcustom gnus-signature-limit nil
202   "Provide a limit to what is considered a signature.
203 If it is a number, no signature may not be longer (in characters) than
204 that number.  If it is a floating point number, no signature may be
205 longer (in lines) than that number.  If it is a function, the function
206 will be called without any parameters, and if it returns nil, there is
207 no signature in the buffer.  If it is a string, it will be used as a
208 regexp.  If it matches, the text in question is not a signature."
209   :type '(choice (integer :value 200)
210                  (number :value 4.0)
211                  (function :value fun)
212                  (regexp :value ".*"))
213   :group 'gnus-article-signature)
214
215 (defcustom gnus-hidden-properties '(invisible t intangible t)
216   "Property list to use for hiding text."
217   :type 'sexp
218   :group 'gnus-article-hiding)
219
220 ;; Fixme: This isn't the right thing for mixed graphical and and
221 ;; non-graphical frames in a session.
222 ;; gnus-xmas.el overrides this for XEmacs.
223 (defcustom gnus-article-x-face-command
224   (cond
225    ((and (fboundp 'image-type-available-p)
226          (module-installed-p 'x-face-e21))
227     'x-face-decode-message-header)
228    ((and (fboundp 'image-type-available-p)
229          (image-type-available-p 'xbm))
230     'gnus-article-display-xface)
231    ((and (not (featurep 'xemacs))
232          window-system
233          (module-installed-p 'x-face-mule))
234     'x-face-mule-gnus-article-display-x-face)
235    (gnus-article-compface-xbm
236     "{ echo '/* Width=48, Height=48 */'; uncompface; } | display -")
237    (t
238     "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
239 display -"))
240   "*String or function to be executed to display an X-Face header.
241 If it is a string, the command will be executed in a sub-shell
242 asynchronously.  The compressed face will be piped to this command."
243   :type '(choice string
244                  (function-item
245                   :tag "x-face-decode-message-header (x-face-e21)"
246                   x-face-decode-message-header)
247                  (function-item gnus-article-display-xface)
248                  (function-item x-face-mule-gnus-article-display-x-face)
249                  function)
250   ;;:version "21.1"
251   :group 'gnus-article-washing)
252
253 (defcustom gnus-article-x-face-too-ugly nil
254   "Regexp matching posters whose face shouldn't be shown automatically."
255   :type '(choice regexp (const nil))
256   :group 'gnus-article-washing)
257
258 (defcustom gnus-article-banner-alist nil
259   "Banner alist for stripping.
260 For example,
261      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( \\\\(e\\\\|Yahoo! \\\\)Groups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
262   :version "21.1"
263   :type '(repeat (cons symbol regexp))
264   :group 'gnus-article-washing)
265
266 (gnus-define-group-parameter
267  banner
268  :variable-document
269  "Alist of regexps (to match group names) and banner."
270  :variable-group gnus-article-washing
271  :parameter-type
272  '(choice :tag "Banner"
273           :value nil
274           (const :tag "Remove signature" signature)
275           (symbol :tag "Item in `gnus-article-banner-alist'" none)
276           regexp
277           (const :tag "None" nil))
278  :parameter-document
279  "If non-nil, specify how to remove `banners' from articles.
280
281 Symbol `signature' means to remove signatures delimited by
282 `gnus-signature-separator'.  Any other symbol is used to look up a
283 regular expression to match the banner in `gnus-article-banner-alist'.
284 A string is used as a regular expression to match the banner
285 directly.")
286
287 (defcustom gnus-emphasis-alist
288   (let ((format
289          "\\(\\s-\\|^\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\\(\\s-\\|[-,;:\"]\\s-\\|[?!.]+\\s-\\|\\s)\\)")
290         (types
291          '(("_" "_" underline)
292            ("/" "/" italic)
293            ("\\*" "\\*" bold)
294            ("_/" "/_" underline-italic)
295            ("_\\*" "\\*_" underline-bold)
296            ("\\*/" "/\\*" bold-italic)
297            ("_\\*/" "/\\*_" underline-bold-italic))))
298     `(("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
299        2 3 gnus-emphasis-underline)
300       ,@(mapcar
301          (lambda (spec)
302            (list
303             (format format (car spec) (car (cdr spec)))
304             2 3 (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
305          types)))
306   "*Alist that says how to fontify certain phrases.
307 Each item looks like this:
308
309   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
310
311 The first element is a regular expression to be matched.  The second
312 is a number that says what regular expression grouping used to find
313 the entire emphasized word.  The third is a number that says what
314 regexp grouping should be displayed and highlighted.  The fourth
315 is the face used for highlighting."
316   :type '(repeat (list :value ("" 0 0 default)
317                        regexp
318                        (integer :tag "Match group")
319                        (integer :tag "Emphasize group")
320                        face))
321   :group 'gnus-article-emphasis)
322
323 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
324   "A regexp to describe whitespace which should not be emphasized.
325 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
326 The former avoids underlining of leading and trailing whitespace,
327 and the latter avoids underlining any whitespace at all."
328   :version "21.1"
329   :group 'gnus-article-emphasis
330   :type 'regexp)
331
332 (defface gnus-emphasis-bold '((t (:bold t)))
333   "Face used for displaying strong emphasized text (*word*)."
334   :group 'gnus-article-emphasis)
335
336 (defface gnus-emphasis-italic '((t (:italic t)))
337   "Face used for displaying italic emphasized text (/word/)."
338   :group 'gnus-article-emphasis)
339
340 (defface gnus-emphasis-underline '((t (:underline t)))
341   "Face used for displaying underlined emphasized text (_word_)."
342   :group 'gnus-article-emphasis)
343
344 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
345   "Face used for displaying underlined bold emphasized text (_*word*_)."
346   :group 'gnus-article-emphasis)
347
348 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
349   "Face used for displaying underlined italic emphasized text (_/word/_)."
350   :group 'gnus-article-emphasis)
351
352 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
353   "Face used for displaying bold italic emphasized text (/*word*/)."
354   :group 'gnus-article-emphasis)
355
356 (defface gnus-emphasis-underline-bold-italic
357   '((t (:bold t :italic t :underline t)))
358   "Face used for displaying underlined bold italic emphasized text.
359 Esample: (_/*word*/_)."
360   :group 'gnus-article-emphasis)
361
362 (defface gnus-emphasis-highlight-words
363   '((t (:background "black" :foreground "yellow")))
364   "Face used for displaying highlighted words."
365   :group 'gnus-article-emphasis)
366
367 (defcustom gnus-article-time-format "%a, %b %d %Y %T %Z"
368   "Format for display of Date headers in article bodies.
369 See `format-time-string' for the possible values.
370
371 The variable can also be function, which should return a complete Date
372 header.  The function is called with one argument, the time, which can
373 be fed to `format-time-string'."
374   :type '(choice string symbol)
375   :link '(custom-manual "(gnus)Article Date")
376   :group 'gnus-article-washing)
377
378 (eval-and-compile
379   (autoload 'mail-extract-address-components "mail-extr"))
380
381 (defcustom gnus-save-all-headers t
382   "*If non-nil, don't remove any headers before saving."
383   :group 'gnus-article-saving
384   :type 'boolean)
385
386 (defcustom gnus-prompt-before-saving 'always
387   "*This variable says how much prompting is to be done when saving articles.
388 If it is nil, no prompting will be done, and the articles will be
389 saved to the default files.  If this variable is `always', each and
390 every article that is saved will be preceded by a prompt, even when
391 saving large batches of articles.  If this variable is neither nil not
392 `always', there the user will be prompted once for a file name for
393 each invocation of the saving commands."
394   :group 'gnus-article-saving
395   :type '(choice (item always)
396                  (item :tag "never" nil)
397                  (sexp :tag "once" :format "%t\n" :value t)))
398
399 (defcustom gnus-saved-headers gnus-visible-headers
400   "Headers to keep if `gnus-save-all-headers' is nil.
401 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
402 If that variable is nil, however, all headers that match this regexp
403 will be kept while the rest will be deleted before saving."
404   :group 'gnus-article-saving
405   :type 'regexp)
406
407 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
408   "A function to save articles in your favourite format.
409 The function must be interactively callable (in other words, it must
410 be an Emacs command).
411
412 Gnus provides the following functions:
413
414 * gnus-summary-save-in-rmail (Rmail format)
415 * gnus-summary-save-in-mail (Unix mail format)
416 * gnus-summary-save-in-folder (MH folder)
417 * gnus-summary-save-in-file (article format)
418 * gnus-summary-save-in-vm (use VM's folder format)
419 * gnus-summary-write-to-file (article format -- overwrite)."
420   :group 'gnus-article-saving
421   :type '(radio (function-item gnus-summary-save-in-rmail)
422                 (function-item gnus-summary-save-in-mail)
423                 (function-item gnus-summary-save-in-folder)
424                 (function-item gnus-summary-save-in-file)
425                 (function-item gnus-summary-save-in-vm)
426                 (function-item gnus-summary-write-to-file)))
427
428 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
429   "A function generating a file name to save articles in Rmail format.
430 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
431   :group 'gnus-article-saving
432   :type 'function)
433
434 (defcustom gnus-mail-save-name 'gnus-plain-save-name
435   "A function generating a file name to save articles in Unix mail format.
436 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
437   :group 'gnus-article-saving
438   :type 'function)
439
440 (defcustom gnus-folder-save-name 'gnus-folder-save-name
441   "A function generating a file name to save articles in MH folder.
442 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
443   :group 'gnus-article-saving
444   :type 'function)
445
446 (defcustom gnus-file-save-name 'gnus-numeric-save-name
447   "A function generating a file name to save articles in article format.
448 The function is called with NEWSGROUP, HEADERS, and optional
449 LAST-FILE."
450   :group 'gnus-article-saving
451   :type 'function)
452
453 (defcustom gnus-split-methods
454   '((gnus-article-archive-name)
455     (gnus-article-nndoc-name))
456   "*Variable used to suggest where articles are to be saved.
457 For instance, if you would like to save articles related to Gnus in
458 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
459 you could set this variable to something like:
460
461  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
462    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
463
464 This variable is an alist where the where the key is the match and the
465 value is a list of possible files to save in if the match is non-nil.
466
467 If the match is a string, it is used as a regexp match on the
468 article.  If the match is a symbol, that symbol will be funcalled
469 from the buffer of the article to be saved with the newsgroup as the
470 parameter.  If it is a list, it will be evaled in the same buffer.
471
472 If this form or function returns a string, this string will be used as
473 a possible file name; and if it returns a non-nil list, that list will
474 be used as possible file names."
475   :group 'gnus-article-saving
476   :type '(repeat (choice (list :value (fun) function)
477                          (cons :value ("" "") regexp (repeat string))
478                          (sexp :value nil))))
479
480 (defcustom gnus-article-display-method-for-mime
481   'gnus-article-display-mime-message
482   "Function to display a MIME message.
483 The function is called from the article buffer."
484   :group 'gnus-article-mime
485   :type 'function)
486
487 (defcustom gnus-article-display-method-for-traditional
488   'gnus-article-display-traditional-message
489   "*Function to display a traditional message.
490 The function is called from the article buffer."
491   :group 'gnus-article-mime
492   :type 'function)
493
494 (defcustom gnus-page-delimiter "^\^L"
495   "*Regexp describing what to use as article page delimiters.
496 The default value is \"^\^L\", which is a form linefeed at the
497 beginning of a line."
498   :type 'regexp
499   :group 'gnus-article-various)
500
501 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
502   "*The format specification for the article mode line.
503 See `gnus-summary-mode-line-format' for a closer description.
504
505 The following additional specs are available:
506
507 %w  The article washing status.
508 %m  The number of MIME parts in the article."
509   :type 'string
510   :group 'gnus-article-various)
511
512 (defcustom gnus-article-mode-hook nil
513   "*A hook for Gnus article mode."
514   :type 'hook
515   :group 'gnus-article-various)
516
517 (when (featurep 'xemacs)
518   ;; Extracted from gnus-xmas-define in order to preserve user settings
519   (when (fboundp 'turn-off-scroll-in-place)
520     (add-hook 'gnus-article-mode-hook 'turn-off-scroll-in-place))
521   ;; Extracted from gnus-xmas-redefine in order to preserve user settings
522   (add-hook 'gnus-article-mode-hook 'gnus-xmas-article-menu-add))
523
524 (defcustom gnus-article-menu-hook nil
525   "*Hook run after the creation of the article mode menu."
526   :type 'hook
527   :group 'gnus-article-various)
528
529 (defcustom gnus-article-prepare-hook nil
530   "*A hook called after an article has been prepared in the article buffer."
531   :type 'hook
532   :group 'gnus-article-various)
533
534 (defcustom gnus-article-hide-pgp-hook nil
535   "*A hook called after successfully hiding a PGP signature."
536   :type 'hook
537   :group 'gnus-article-various)
538
539 (defcustom gnus-article-button-face 'bold
540   "Face used for highlighting buttons in the article buffer.
541
542 An article button is a piece of text that you can activate by pressing
543 `RET' or `mouse-2' above it."
544   :type 'face
545   :group 'gnus-article-buttons)
546
547 (defcustom gnus-article-mouse-face 'highlight
548   "Face used for mouse highlighting in the article buffer.
549
550 Article buttons will be displayed in this face when the cursor is
551 above them."
552   :type 'face
553   :group 'gnus-article-buttons)
554
555 (defcustom gnus-signature-face 'gnus-signature-face
556   "Face used for highlighting a signature in the article buffer.
557 Obsolete; use the face `gnus-signature-face' for customizations instead."
558   :type 'face
559   :group 'gnus-article-highlight
560   :group 'gnus-article-signature)
561
562 (defface gnus-signature-face
563   '((t
564      (:italic t)))
565   "Face used for highlighting a signature in the article buffer."
566   :group 'gnus-article-highlight
567   :group 'gnus-article-signature)
568
569 (defface gnus-header-from-face
570   '((((class color)
571       (background dark))
572      (:foreground "spring green"))
573     (((class color)
574       (background light))
575      (:foreground "red3"))
576     (t
577      (:italic t)))
578   "Face used for displaying from headers."
579   :group 'gnus-article-headers
580   :group 'gnus-article-highlight)
581
582 (defface gnus-header-subject-face
583   '((((class color)
584       (background dark))
585      (:foreground "SeaGreen3"))
586     (((class color)
587       (background light))
588      (:foreground "red4"))
589     (t
590      (:bold t :italic t)))
591   "Face used for displaying subject headers."
592   :group 'gnus-article-headers
593   :group 'gnus-article-highlight)
594
595 (defface gnus-header-newsgroups-face
596   '((((class color)
597       (background dark))
598      (:foreground "yellow" :italic t))
599     (((class color)
600       (background light))
601      (:foreground "MidnightBlue" :italic t))
602     (t
603      (:italic t)))
604   "Face used for displaying newsgroups headers."
605   :group 'gnus-article-headers
606   :group 'gnus-article-highlight)
607
608 (defface gnus-header-name-face
609   '((((class color)
610       (background dark))
611      (:foreground "SeaGreen"))
612     (((class color)
613       (background light))
614      (:foreground "maroon"))
615     (t
616      (:bold t)))
617   "Face used for displaying header names."
618   :group 'gnus-article-headers
619   :group 'gnus-article-highlight)
620
621 (defface gnus-header-content-face
622   '((((class color)
623       (background dark))
624      (:foreground "forest green" :italic t))
625     (((class color)
626       (background light))
627      (:foreground "indianred4" :italic t))
628     (t
629      (:italic t)))  "Face used for displaying header content."
630   :group 'gnus-article-headers
631   :group 'gnus-article-highlight)
632
633 (defcustom gnus-header-face-alist
634   '(("From" nil gnus-header-from-face)
635     ("Subject" nil gnus-header-subject-face)
636     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
637     ("" gnus-header-name-face gnus-header-content-face))
638   "*Controls highlighting of article header.
639
640 An alist of the form (HEADER NAME CONTENT).
641
642 HEADER is a regular expression which should match the name of an
643 header header and NAME and CONTENT are either face names or nil.
644
645 The name of each header field will be displayed using the face
646 specified by the first element in the list where HEADER match the
647 header name and NAME is non-nil.  Similarly, the content will be
648 displayed by the first non-nil matching CONTENT face."
649   :group 'gnus-article-headers
650   :group 'gnus-article-highlight
651   :type '(repeat (list (regexp :tag "Header")
652                        (choice :tag "Name"
653                                (item :tag "skip" nil)
654                                (face :value default))
655                        (choice :tag "Content"
656                                (item :tag "skip" nil)
657                                (face :value default)))))
658
659 (defcustom gnus-article-decode-hook nil
660   "*Hook run to decode charsets in articles."
661   :group 'gnus-article-headers
662   :type 'hook)
663
664 (defcustom gnus-display-mime-function 'gnus-display-mime
665   "Function to display MIME articles."
666   :group 'gnus-article-mime
667   :type 'function)
668
669 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
670   "Function used to decode headers.")
671
672 (defvar gnus-article-dumbquotes-map
673   '(("\200" "EUR")
674     ("\202" ",")
675     ("\203" "f")
676     ("\204" ",,")
677     ("\205" "...")
678     ("\213" "<")
679     ("\214" "OE")
680     ("\221" "`")
681     ("\222" "'")
682     ("\223" "``")
683     ("\224" "\"")
684     ("\225" "*")
685     ("\226" "-")
686     ("\227" "--")
687     ("\231" "(TM)")
688     ("\233" ">")
689     ("\234" "oe")
690     ("\264" "'"))
691   "Table for MS-to-Latin1 translation.")
692
693 (defcustom gnus-ignored-mime-types nil
694   "List of MIME types that should be ignored by Gnus."
695   :version "21.1"
696   :group 'gnus-article-mime
697   :type '(repeat regexp))
698
699 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
700   "List of MIME types that should not be given buttons when rendered inline."
701   :version "21.1"
702   :group 'gnus-article-mime
703   :type '(repeat regexp))
704
705 (defcustom gnus-article-mime-part-function nil
706   "Function called with a MIME handle as the argument.
707 This is meant for people who want to do something automatic based
708 on parts -- for instance, adding Vcard info to a database."
709   :group 'gnus-article-mime
710   :type 'function)
711
712 (defcustom gnus-mime-multipart-functions nil
713   "An alist of MIME types to functions to display them."
714   :version "21.1"
715   :group 'gnus-article-mime
716   :type 'alist)
717
718 (defcustom gnus-article-date-lapsed-new-header nil
719   "Whether the X-Sent and Date headers can coexist.
720 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
721 either replace the old \"Date:\" header (if this variable is nil), or
722 be added below it (otherwise)."
723   :version "21.1"
724   :group 'gnus-article-headers
725   :type 'boolean)
726
727 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
728   "Function called with a MIME handle as the argument.
729 This is meant for people who want to view first matched part.
730 For `undisplayed-alternative' (default), the first undisplayed
731 part or alternative part is used.  For `undisplayed', the first
732 undisplayed part is used.  For a function, the first part which
733 the function return `t' is used.  For `nil', the first part is
734 used."
735   :version "21.1"
736   :group 'gnus-article-mime
737   :type '(choice
738           (item :tag "first" :value nil)
739           (item :tag "undisplayed" :value undisplayed)
740           (item :tag "undisplayed or alternative"
741                 :value undisplayed-alternative)
742           (function)))
743
744 (defcustom gnus-mime-action-alist
745   '(("save to file" . gnus-mime-save-part)
746     ("save and strip" . gnus-mime-save-part-and-strip)
747     ("display as text" . gnus-mime-inline-part)
748     ("view the part" . gnus-mime-view-part)
749     ("pipe to command" . gnus-mime-pipe-part)
750     ("toggle display" . gnus-article-press-button)
751     ("toggle display" . gnus-article-view-part-as-charset)
752     ("view as type" . gnus-mime-view-part-as-type)
753     ("internalize type" . gnus-mime-internalize-part)
754     ("externalize type" . gnus-mime-externalize-part))
755   "An alist of actions that run on the MIME attachment."
756   :group 'gnus-article-mime
757   :type '(repeat (cons (string :tag "name")
758                        (function))))
759
760 (defcustom gnus-mime-action-alist
761   '(("save to file" . gnus-mime-save-part)
762     ("display as text" . gnus-mime-inline-part)
763     ("view the part" . gnus-mime-view-part)
764     ("pipe to command" . gnus-mime-pipe-part)
765     ("toggle display" . gnus-article-press-button)
766     ("view as type" . gnus-mime-view-part-as-type)
767     ("internalize type" . gnus-mime-internalize-part)
768     ("externalize type" . gnus-mime-externalize-part))
769   "An alist of actions that run on the MIME attachment."
770   :version "21.1"
771   :group 'gnus-article-mime
772   :type '(repeat (cons (string :tag "name")
773                        (function))))
774
775 ;;;
776 ;;; The treatment variables
777 ;;;
778
779 (defvar gnus-part-display-hook nil
780   "Hook called on parts that are to receive treatment.")
781
782 (defvar gnus-article-treat-custom
783   '(choice (const :tag "Off" nil)
784            (const :tag "On" t)
785            (const :tag "Header" head)
786            (const :tag "Last" last)
787            (const :tag "Mime" mime)
788            (integer :tag "Less")
789            (repeat :tag "Groups" regexp)
790            (sexp :tag "Predicate")))
791
792 (defvar gnus-article-treat-head-custom
793   '(choice (const :tag "Off" nil)
794            (const :tag "Header" head)))
795
796 (defvar gnus-article-treat-types '("text/plain")
797   "Parts to treat.")
798
799 (defvar gnus-inhibit-treatment nil
800   "Whether to inhibit treatment.")
801
802 (defcustom gnus-treat-highlight-signature '(or last (typep "text/x-vcard"))
803   "Highlight the signature.
804 Valid values are nil, t, `head', `last', an integer or a predicate.
805 See the manual for details."
806   :group 'gnus-article-treat
807   :type gnus-article-treat-custom)
808 (put 'gnus-treat-highlight-signature 'highlight t)
809
810 (defcustom gnus-treat-buttonize 100000
811   "Add buttons.
812 Valid values are nil, t, `head', `last', an integer or a predicate.
813 See the manual for details."
814   :group 'gnus-article-treat
815   :type gnus-article-treat-custom)
816 (put 'gnus-treat-buttonize 'highlight t)
817
818 (defcustom gnus-treat-buttonize-head 'head
819   "Add buttons to the head.
820 Valid values are nil, t, `head', `last', an integer or a predicate.
821 See the manual for details."
822   :group 'gnus-article-treat
823   :type gnus-article-treat-head-custom)
824 (put 'gnus-treat-buttonize-head 'highlight t)
825
826 (defcustom gnus-treat-emphasize
827   (and (or window-system
828            (featurep 'xemacs)
829            (>= (string-to-number emacs-version) 21))
830        50000)
831   "Emphasize text.
832 Valid values are nil, t, `head', `last', an integer or a predicate.
833 See the manual for details."
834   :group 'gnus-article-treat
835   :type gnus-article-treat-custom)
836 (put 'gnus-treat-emphasize 'highlight t)
837
838 (defcustom gnus-treat-strip-cr nil
839   "Remove carriage returns.
840 Valid values are nil, t, `head', `last', an integer or a predicate.
841 See the manual for details."
842   :group 'gnus-article-treat
843   :type gnus-article-treat-custom)
844
845 (defcustom gnus-treat-leading-whitespace nil
846   "Remove leading whitespace in headers.
847 Valid values are nil, t, `head', `last', an integer or a predicate.
848 See the manual for details."
849   :group 'gnus-article-treat
850   :type gnus-article-treat-custom)
851
852 (defcustom gnus-treat-hide-headers 'head
853   "Hide headers.
854 Valid values are nil, t, `head', `last', an integer or a predicate.
855 See the manual for details."
856   :group 'gnus-article-treat
857   :type gnus-article-treat-head-custom)
858
859 (defcustom gnus-treat-hide-boring-headers nil
860   "Hide boring headers.
861 Valid values are nil, t, `head', `last', an integer or a predicate.
862 See the manual for details."
863   :group 'gnus-article-treat
864   :type gnus-article-treat-head-custom)
865
866 (defcustom gnus-treat-hide-signature nil
867   "Hide the signature.
868 Valid values are nil, t, `head', `last', an integer or a predicate.
869 See the manual for details."
870   :group 'gnus-article-treat
871   :type gnus-article-treat-custom)
872
873 (defcustom gnus-treat-fill-article nil
874   "Fill the article.
875 Valid values are nil, t, `head', `last', an integer or a predicate.
876 See the manual for details."
877   :group 'gnus-article-treat
878   :type gnus-article-treat-custom)
879
880 (defcustom gnus-treat-hide-citation nil
881   "Hide cited text.
882 Valid values are nil, t, `head', `last', an integer or a predicate.
883 See the manual for details."
884   :group 'gnus-article-treat
885   :type gnus-article-treat-custom)
886
887 (defcustom gnus-treat-hide-citation-maybe nil
888   "Hide cited text.
889 Valid values are nil, t, `head', `last', an integer or a predicate.
890 See the manual for details."
891   :group 'gnus-article-treat
892   :type gnus-article-treat-custom)
893
894 (defcustom gnus-treat-hide-citation-maybe nil
895   "Hide cited text.
896 Valid values are nil, t, `head', `last', an integer or a predicate.
897 See the manual for details."
898   :group 'gnus-article-treat
899   :type gnus-article-treat-custom)
900
901 (defcustom gnus-treat-strip-list-identifiers 'head
902   "Strip list identifiers from `gnus-list-identifiers`.
903 Valid values are nil, t, `head', `last', an integer or a predicate.
904 See the manual for details."
905   :version "21.1"
906   :group 'gnus-article-treat
907   :type gnus-article-treat-custom)
908
909 (defcustom gnus-treat-strip-pgp t
910   "Strip PGP signatures.
911 Valid values are nil, t, `head', `last', an integer or a predicate.
912 See the manual for details."
913   :group 'gnus-article-treat
914   :type gnus-article-treat-custom)
915
916 (defcustom gnus-treat-strip-pem nil
917   "Strip PEM signatures.
918 Valid values are nil, t, `head', `last', an integer or a predicate.
919 See the manual for details."
920   :group 'gnus-article-treat
921   :type gnus-article-treat-custom)
922
923 (defcustom gnus-treat-strip-banner t
924   "Strip banners from articles.
925 The banner to be stripped is specified in the `banner' group parameter.
926 Valid values are nil, t, `head', `last', an integer or a predicate.
927 See the manual for details."
928   :group 'gnus-article-treat
929   :type gnus-article-treat-custom)
930
931 (defcustom gnus-treat-highlight-headers 'head
932   "Highlight the headers.
933 Valid values are nil, t, `head', `last', an integer or a predicate.
934 See the manual for details."
935   :group 'gnus-article-treat
936   :type gnus-article-treat-head-custom)
937 (put 'gnus-treat-highlight-headers 'highlight t)
938
939 (defcustom gnus-treat-highlight-citation t
940   "Highlight cited text.
941 Valid values are nil, t, `head', `last', an integer or a predicate.
942 See the manual for details."
943   :group 'gnus-article-treat
944   :type gnus-article-treat-custom)
945 (put 'gnus-treat-highlight-citation 'highlight t)
946
947 (defcustom gnus-treat-date-ut nil
948   "Display the Date in UT (GMT).
949 Valid values are nil, t, `head', `last', an integer or a predicate.
950 See the manual for details."
951   :group 'gnus-article-treat
952   :type gnus-article-treat-head-custom)
953
954 (defcustom gnus-treat-date-local nil
955   "Display the Date in the local timezone.
956 Valid values are nil, t, `head', `last', an integer or a predicate.
957 See the manual for details."
958   :group 'gnus-article-treat
959   :type gnus-article-treat-head-custom)
960
961 (defcustom gnus-treat-date-english nil
962   "Display the Date in a format that can be read aloud in English.
963 Valid values are nil, t, `head', `last', an integer or a predicate.
964 See the manual for details."
965   :group 'gnus-article-treat
966   :type gnus-article-treat-head-custom)
967
968 (defcustom gnus-treat-date-lapsed nil
969   "Display the Date header in a way that says how much time has elapsed.
970 Valid values are nil, t, `head', `last', an integer or a predicate.
971 See the manual for details."
972   :group 'gnus-article-treat
973   :type gnus-article-treat-head-custom)
974
975 (defcustom gnus-treat-date-original nil
976   "Display the date in the original timezone.
977 Valid values are nil, t, `head', `last', an integer or a predicate.
978 See the manual for details."
979   :group 'gnus-article-treat
980   :type gnus-article-treat-head-custom)
981
982 (defcustom gnus-treat-date-iso8601 nil
983   "Display the date in the ISO8601 format.
984 Valid values are nil, t, `head', `last', an integer or a predicate.
985 See the manual for details."
986   :version "21.1"
987   :group 'gnus-article-treat
988   :type gnus-article-treat-head-custom)
989
990 (defcustom gnus-treat-date-user-defined nil
991   "Display the date in a user-defined format.
992 The format is defined by the `gnus-article-time-format' variable.
993 Valid values are nil, t, `head', `last', an integer or a predicate.
994 See the manual for details."
995   :group 'gnus-article-treat
996   :type gnus-article-treat-head-custom)
997
998 (defcustom gnus-treat-strip-headers-in-body t
999   "Strip the X-No-Archive header line from the beginning of the body.
1000 Valid values are nil, t, `head', `last', an integer or a predicate.
1001 See the manual for details."
1002   :version "21.1"
1003   :group 'gnus-article-treat
1004   :type gnus-article-treat-custom)
1005
1006 (defcustom gnus-treat-strip-trailing-blank-lines nil
1007   "Strip trailing blank lines.
1008 Valid values are nil, t, `head', `last', an integer or a predicate.
1009 See the manual for details."
1010   :group 'gnus-article-treat
1011   :type gnus-article-treat-custom)
1012
1013 (defcustom gnus-treat-strip-leading-blank-lines nil
1014   "Strip leading blank lines.
1015 Valid values are nil, t, `head', `last', an integer or a predicate.
1016 See the manual for details."
1017   :group 'gnus-article-treat
1018   :type gnus-article-treat-custom)
1019
1020 (defcustom gnus-treat-strip-multiple-blank-lines nil
1021   "Strip multiple blank lines.
1022 Valid values are nil, t, `head', `last', an integer or a predicate.
1023 See the manual for details."
1024   :group 'gnus-article-treat
1025   :type gnus-article-treat-custom)
1026
1027 (defcustom gnus-treat-overstrike t
1028   "Treat overstrike highlighting.
1029 Valid values are nil, t, `head', `last', an integer or a predicate.
1030 See the manual for details."
1031   :group 'gnus-article-treat
1032   :type gnus-article-treat-custom)
1033 (put 'gnus-treat-overstrike 'highlight t)
1034
1035 (defcustom gnus-treat-display-xface
1036   (and (or (and (fboundp 'image-type-available-p)
1037                 (image-type-available-p 'xbm)
1038                 (string-match "^0x" (shell-command-to-string "uncompface")))
1039            (and (featurep 'xemacs) (featurep 'xface))
1040            (eq 'x-face-mule-gnus-article-display-x-face
1041                gnus-article-x-face-command))
1042        'head)
1043   "Display X-Face headers.
1044 Valid values are nil, t, `head', `last', an integer or a predicate.
1045 See the manual for details."
1046   :group 'gnus-article-treat
1047   ;;:version "21.1"
1048   :type gnus-article-treat-head-custom)
1049 (put 'gnus-treat-display-xface 'highlight t)
1050
1051 (defcustom gnus-treat-display-smileys
1052   (if (or (and (featurep 'xemacs)
1053                (featurep 'xpm))
1054           (and (fboundp 'image-type-available-p)
1055                (image-type-available-p 'pbm))
1056           (and (not (featurep 'xemacs))
1057                window-system
1058                (module-installed-p 'smiley-mule)))
1059       t
1060     nil)
1061   "Display smileys.
1062 Valid values are nil, t, `head', `last', an integer or a predicate.
1063 See the manual for details."
1064   :group 'gnus-article-treat
1065   ;;:version "21.1"
1066   :type gnus-article-treat-custom)
1067 (put 'gnus-treat-display-smileys 'highlight t)
1068
1069 (defcustom gnus-treat-display-picons (if (featurep 'xemacs) 'head nil)
1070   "Display picons.
1071 Valid values are nil, t, `head', `last', an integer or a predicate.
1072 See the manual for details."
1073   :group 'gnus-article-treat
1074   :type gnus-article-treat-head-custom)
1075 (put 'gnus-treat-display-picons 'highlight t)
1076
1077 (defcustom gnus-treat-capitalize-sentences nil
1078   "Capitalize sentence-starting words.
1079 Valid values are nil, t, `head', `last', an integer or a predicate.
1080 See the manual for details."
1081   :version "21.1"
1082   :group 'gnus-article-treat
1083   :type gnus-article-treat-custom)
1084
1085 (defcustom gnus-treat-fill-long-lines nil
1086   "Fill long lines.
1087 Valid values are nil, t, `head', `last', an integer or a predicate.
1088 See the manual for details."
1089   :group 'gnus-article-treat
1090   :type gnus-article-treat-custom)
1091
1092 (defcustom gnus-treat-play-sounds nil
1093   "Play sounds.
1094 Valid values are nil, t, `head', `last', an integer or a predicate.
1095 See the manual for details."
1096   :version "21.1"
1097   :group 'gnus-article-treat
1098   :type gnus-article-treat-custom)
1099
1100 (defcustom gnus-treat-decode-article-as-default-mime-charset nil
1101   "Decode an article as `default-mime-charset'.  For instance, if you want to
1102 attempt to decode an article even if the value of `gnus-show-mime' is nil,
1103 you could set this variable to something like: nil for don't decode, t for
1104 decode the body, '(or header t) for the whole article, etc."
1105   :group 'gnus-article-treat
1106   :type '(radio (const :tag "Off" nil)
1107                 (const :tag "Decode body" t)
1108                 (const :tag "Decode all" (or head t))))
1109
1110 (defcustom gnus-treat-translate nil
1111   "Translate articles from one language to another.
1112 Valid values are nil, t, `head', `last', an integer or a predicate.
1113 See the manual for details."
1114   :version "21.1"
1115   :group 'gnus-article-treat
1116   :type gnus-article-treat-custom)
1117
1118 (defcustom gnus-treat-x-pgp-sig nil
1119   "Verify X-PGP-Sig.
1120 To automatically treat X-PGP-Sig, set it to head.
1121 Valid values are nil, t, `head', `last', an integer or a predicate.
1122 See the manual for details."
1123   :group 'gnus-article-treat
1124   :group 'mime-security
1125   :type gnus-article-treat-custom)
1126
1127 (defvar gnus-article-encrypt-protocol-alist
1128   '(("PGP" . mml2015-self-encrypt)))
1129
1130 ;; Set to nil if more than one protocol added to
1131 ;; gnus-article-encrypt-protocol-alist.
1132 (defcustom gnus-article-encrypt-protocol "PGP"
1133   "The protocol used for encrypt articles.
1134 It is a string, such as \"PGP\". If nil, ask user."
1135   :type 'string
1136   :group 'mime-security)
1137
1138 ;;; Internal variables
1139
1140 (defvar gnus-english-month-names
1141   '("January" "February" "March" "April" "May" "June" "July" "August"
1142     "September" "October" "November" "December"))
1143
1144 (defvar article-goto-body-goes-to-point-min-p nil)
1145 (defvar gnus-article-wash-types nil)
1146 (defvar gnus-article-emphasis-alist nil)
1147
1148 (defvar gnus-article-mime-handle-alist-1 nil)
1149 (defvar gnus-treatment-function-alist
1150   `((gnus-treat-decode-article-as-default-mime-charset
1151      gnus-article-decode-article-as-default-mime-charset)
1152     (gnus-treat-x-pgp-sig gnus-article-verify-x-pgp-sig)
1153     (gnus-treat-strip-banner gnus-article-strip-banner)
1154     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
1155     (gnus-treat-buttonize gnus-article-add-buttons)
1156     (gnus-treat-fill-article gnus-article-fill-cited-article)
1157     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
1158     (gnus-treat-strip-cr gnus-article-remove-cr)
1159     (gnus-treat-display-xface gnus-article-display-x-face)
1160     (gnus-treat-date-ut gnus-article-date-ut)
1161     (gnus-treat-date-local gnus-article-date-local)
1162     (gnus-treat-date-english gnus-article-date-english)
1163     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1164     (gnus-treat-date-original gnus-article-date-original)
1165     (gnus-treat-date-user-defined gnus-article-date-user)
1166     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1167     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
1168     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
1169     (gnus-treat-hide-signature gnus-article-hide-signature)
1170     (gnus-treat-hide-citation gnus-article-hide-citation)
1171     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
1172     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
1173     (gnus-treat-leading-whitespace gnus-article-remove-leading-whitespace)
1174     (gnus-treat-strip-pgp gnus-article-hide-pgp)
1175     (gnus-treat-strip-pem gnus-article-hide-pem)
1176     (gnus-treat-highlight-headers gnus-article-highlight-headers)
1177     (gnus-treat-emphasize gnus-article-emphasize)
1178     (gnus-treat-highlight-citation gnus-article-highlight-citation)
1179     (gnus-treat-highlight-signature gnus-article-highlight-signature)
1180     (gnus-treat-strip-trailing-blank-lines
1181      gnus-article-remove-trailing-blank-lines)
1182     (gnus-treat-strip-leading-blank-lines
1183      gnus-article-strip-leading-blank-lines)
1184     (gnus-treat-strip-multiple-blank-lines
1185      gnus-article-strip-multiple-blank-lines)
1186     (gnus-treat-overstrike gnus-article-treat-overstrike)
1187     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1188     (gnus-treat-display-smileys ,(if (or (featurep 'xemacs)
1189                                          (>= emacs-major-version 21))
1190                                      'gnus-smiley-display
1191                                    'gnus-article-smiley-display))
1192     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1193     (gnus-treat-display-picons gnus-article-display-picons)
1194     (gnus-treat-play-sounds gnus-earcon-display)))
1195
1196 (defvar gnus-article-mime-handle-alist nil)
1197 (defvar article-lapsed-timer nil)
1198 (defvar gnus-article-current-summary nil)
1199
1200 (defvar gnus-article-mode-syntax-table
1201   (let ((table (copy-syntax-table text-mode-syntax-table)))
1202     ;; This causes the citation match run O(2^n).
1203     ;; (modify-syntax-entry ?- "w" table)
1204     (modify-syntax-entry ?> ")" table)
1205     (modify-syntax-entry ?< "(" table)
1206     table)
1207   "Syntax table used in article mode buffers.
1208 Initialized from `text-mode-syntax-table.")
1209
1210 (defvar gnus-save-article-buffer nil)
1211
1212 (defvar gnus-article-mode-line-format-alist
1213   (nconc '((?w (gnus-article-wash-status) ?s)
1214            (?m (gnus-article-mime-part-status) ?s))
1215          gnus-summary-mode-line-format-alist))
1216
1217 (defvar gnus-number-of-articles-to-be-saved nil)
1218
1219 (defvar gnus-inhibit-hiding nil)
1220
1221 (defsubst gnus-article-hide-text (b e props)
1222   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1223   (gnus-add-text-properties-when 'article-type nil b e props)
1224   (when (memq 'intangible props)
1225     (put-text-property
1226      (max (1- b) (point-min))
1227      b 'intangible (cddr (memq 'intangible props)))))
1228
1229 (defsubst gnus-article-unhide-text (b e)
1230   "Remove hidden text properties from region between B and E."
1231   (remove-text-properties b e gnus-hidden-properties)
1232   (when (memq 'intangible gnus-hidden-properties)
1233     (put-text-property (max (1- b) (point-min))
1234                        b 'intangible nil)))
1235
1236 (defun gnus-article-hide-text-type (b e type)
1237   "Hide text of TYPE between B and E."
1238   (push type gnus-article-wash-types)
1239   (gnus-article-hide-text
1240    b e (cons 'article-type (cons type gnus-hidden-properties))))
1241
1242 (defun gnus-article-unhide-text-type (b e type)
1243   "Unhide text of TYPE between B and E."
1244   (setq gnus-article-wash-types
1245         (delq type gnus-article-wash-types))
1246   (remove-text-properties
1247    b e (cons 'article-type (cons type gnus-hidden-properties)))
1248   (when (memq 'intangible gnus-hidden-properties)
1249     (put-text-property (max (1- b) (point-min))
1250                        b 'intangible nil)))
1251
1252 (defun gnus-article-hide-text-of-type (type)
1253   "Hide text of TYPE in the current buffer."
1254   (save-excursion
1255     (let ((b (point-min))
1256           (e (point-max)))
1257       (while (setq b (text-property-any b e 'article-type type))
1258         (add-text-properties b (incf b) gnus-hidden-properties)))))
1259
1260 (defun gnus-article-delete-text-of-type (type)
1261   "Delete text of TYPE in the current buffer."
1262   (save-excursion
1263     (let ((b (point-min)))
1264       (while (setq b (text-property-any b (point-max) 'article-type type))
1265         (delete-region
1266          b (or (text-property-not-all b (point-max) 'article-type type)
1267                (point-max)))))))
1268
1269 (defun gnus-article-delete-invisible-text ()
1270   "Delete all invisible text in the current buffer."
1271   (save-excursion
1272     (let ((b (point-min)))
1273       (while (setq b (text-property-any b (point-max) 'invisible t))
1274         (delete-region
1275          b (or (text-property-not-all b (point-max) 'invisible t)
1276                (point-max)))))))
1277
1278 (defun gnus-article-text-type-exists-p (type)
1279   "Say whether any text of type TYPE exists in the buffer."
1280   (text-property-any (point-min) (point-max) 'article-type type))
1281
1282 (defsubst gnus-article-header-rank ()
1283   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1284   (let ((list gnus-sorted-header-list)
1285         (i 0))
1286     (while list
1287       (when (looking-at (car list))
1288         (setq list nil))
1289       (setq list (cdr list))
1290       (incf i))
1291     i))
1292
1293 (defun article-hide-headers (&optional arg delete)
1294   "Hide unwanted headers and possibly sort them as well."
1295   (interactive (gnus-article-hidden-arg))
1296   ;; Lars said that this function might be inhibited.
1297   (if (gnus-article-check-hidden-text 'headers arg)
1298       (progn
1299         ;; Show boring headers as well.
1300         (gnus-article-show-hidden-text 'boring-headers)
1301         (when (eq 1 (point-min))
1302           (set-window-start (get-buffer-window (current-buffer)) 1)))
1303   (unless gnus-inhibit-hiding
1304     (save-excursion
1305       (save-restriction
1306         (let ((buffer-read-only nil)
1307               (inhibit-read-only t)
1308               (case-fold-search t)
1309               (max (1+ (length gnus-sorted-header-list)))
1310               (ignored (when (not gnus-visible-headers)
1311                          (cond ((stringp gnus-ignored-headers)
1312                                 gnus-ignored-headers)
1313                                ((listp gnus-ignored-headers)
1314                                 (mapconcat 'identity gnus-ignored-headers
1315                                            "\\|")))))
1316               (visible
1317                (cond ((stringp gnus-visible-headers)
1318                       gnus-visible-headers)
1319                      ((and gnus-visible-headers
1320                            (listp gnus-visible-headers))
1321                       (mapconcat 'identity gnus-visible-headers "\\|"))))
1322               (inhibit-point-motion-hooks t)
1323               beg)
1324           ;; First we narrow to just the headers.
1325           (article-narrow-to-head)
1326           ;; Hide any "From " lines at the beginning of (mail) articles.
1327           (while (looking-at "From ")
1328             (forward-line 1))
1329           (unless (bobp)
1330             (if delete
1331                 (delete-region (point-min) (point))
1332               (gnus-article-hide-text (point-min) (point)
1333                                       (nconc (list 'article-type 'headers)
1334                                              gnus-hidden-properties))))
1335           ;; Then treat the rest of the header lines.
1336           ;; Then we use the two regular expressions
1337           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1338           ;; select which header lines is to remain visible in the
1339           ;; article buffer.
1340           (while (re-search-forward "^[^ \t]*:" nil t)
1341             (beginning-of-line)
1342             ;; Mark the rank of the header.
1343             (put-text-property
1344              (point) (1+ (point)) 'message-rank
1345              (if (or (and visible (looking-at visible))
1346                      (and ignored
1347                           (not (looking-at ignored))))
1348                  (gnus-article-header-rank)
1349                (+ 2 max)))
1350             (forward-line 1))
1351           (message-sort-headers-1)
1352           (when (setq beg (text-property-any
1353                            (point-min) (point-max) 'message-rank (+ 2 max)))
1354             ;; We delete or make invisible the unwanted headers.
1355             (push 'headers gnus-article-wash-types)
1356             (if delete
1357                 (progn
1358                   (add-text-properties
1359                    (point-min) (+ 5 (point-min))
1360                    '(article-type headers dummy-invisible t))
1361                   (delete-region beg (point-max)))
1362               (gnus-article-hide-text-type beg (point-max) 'headers))))))))
1363   )
1364
1365 (defun article-hide-boring-headers (&optional arg)
1366   "Toggle hiding of headers that aren't very interesting.
1367 If given a negative prefix, always show; if given a positive prefix,
1368 always hide."
1369   (interactive (gnus-article-hidden-arg))
1370   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1371              (not gnus-show-all-headers))
1372     (save-excursion
1373       (save-restriction
1374         (let ((buffer-read-only nil)
1375               (list gnus-boring-article-headers)
1376               (inhibit-point-motion-hooks t)
1377               elem)
1378           (article-narrow-to-head)
1379           (while list
1380             (setq elem (pop list))
1381             (goto-char (point-min))
1382             (cond
1383              ;; Hide empty headers.
1384              ((eq elem 'empty)
1385               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1386                 (forward-line -1)
1387                 (gnus-article-hide-text-type
1388                  (progn (beginning-of-line) (point))
1389                  (progn
1390                    (end-of-line)
1391                    (if (re-search-forward "^[^ \t]" nil t)
1392                        (match-beginning 0)
1393                      (point-max)))
1394                  'boring-headers)))
1395              ;; Hide boring Newsgroups header.
1396              ((eq elem 'newsgroups)
1397               (when (gnus-string-equal
1398                      (gnus-fetch-field "newsgroups")
1399                      (gnus-group-real-name
1400                       (if (boundp 'gnus-newsgroup-name)
1401                           gnus-newsgroup-name
1402                         "")))
1403                 (gnus-article-hide-header "newsgroups")))
1404              ((eq elem 'to-address)
1405               (let ((to (message-fetch-field "to"))
1406                     (to-address
1407                      (gnus-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   (gnus-run-hooks 'gnus-article-prepare-hook)
3548   ;; Display message.
3549   (let (mime-display-header-hook mime-display-text/plain-hook)
3550     (funcall (if gnus-show-mime
3551                  (progn
3552                    (setq mime-message-structure gnus-current-headers)
3553                    (mime-buffer-entity-set-buffer-internal
3554                     mime-message-structure
3555                     gnus-original-article-buffer)
3556                    (mime-entity-set-representation-type-internal
3557                     mime-message-structure 'mime-buffer-entity)
3558                    (luna-send mime-message-structure
3559                               'initialize-instance
3560                               mime-message-structure)
3561                    gnus-article-display-method-for-mime)
3562                gnus-article-display-method-for-traditional)))
3563   ;; Associate this article with the current summary buffer.
3564   (setq gnus-article-current-summary gnus-summary-buffer)
3565   ;; Call the treatment functions.
3566   (let ((inhibit-read-only t)
3567         buffer-read-only)
3568     (save-restriction
3569       (widen)
3570       (if gnus-show-mime
3571           (gnus-article-prepare-mime-display)
3572         (narrow-to-region (goto-char (point-min))
3573                           (if (search-forward "\n\n" nil t)
3574                               (point)
3575                             (point-max)))
3576         (gnus-treat-article 'head)
3577         (put-text-property (point-min) (point-max) 'article-treated-header t)
3578         (goto-char (point-max))
3579         (widen)
3580         (narrow-to-region (point) (point-max))
3581         (gnus-treat-article nil))
3582       (put-text-property (point-min) (point-max) 'read-only nil)))
3583   ;; Perform the article display hooks.  Incidentally, this hook is
3584   ;; an obsolete variable by now.
3585   (gnus-run-hooks 'gnus-article-display-hook))
3586
3587 (defun gnus-article-decode-article-as-default-mime-charset ()
3588   "Decode an article as `default-mime-charset'.  It won't work if the
3589 value of the variable `gnus-show-mime' is non-nil."
3590   (unless gnus-show-mime
3591     (decode-mime-charset-region (point-min) (point-max)
3592                                 (with-current-buffer gnus-summary-buffer
3593                                   default-mime-charset))))
3594
3595 ;;;
3596 ;;; Gnus MIME viewing functions
3597 ;;;
3598
3599 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
3600   "The following specs can be used:
3601 %t  The MIME type
3602 %T  MIME type, along with additional info
3603 %n  The `name' parameter
3604 %d  The description, if any
3605 %l  The length of the encoded part
3606 %p  The part identifier number
3607 %e  Dots if the part isn't displayed")
3608
3609 (defvar gnus-mime-button-line-format-alist
3610   '((?t gnus-tmp-type ?s)
3611     (?T gnus-tmp-type-long ?s)
3612     (?n gnus-tmp-name ?s)
3613     (?d gnus-tmp-description ?s)
3614     (?p gnus-tmp-id ?s)
3615     (?l gnus-tmp-length ?d)
3616     (?e gnus-tmp-dots ?s)))
3617
3618 (defvar gnus-mime-button-commands
3619   '((gnus-article-press-button "\r" "Toggle Display")
3620     (gnus-mime-view-part "v" "View Interactively...")
3621     (gnus-mime-view-part-as-type "t" "View As Type...")
3622     (gnus-mime-view-part-as-charset "C" "View As charset...")
3623     (gnus-mime-save-part "o" "Save...")
3624     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
3625     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
3626     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
3627     (gnus-mime-internalize-part "E" "View Internally")
3628     (gnus-mime-externalize-part "e" "View Externally")
3629     (gnus-mime-pipe-part "|" "Pipe To Command...")
3630     (gnus-mime-action-on-part "." "Take action on the part")))
3631
3632 (defun gnus-article-mime-part-status ()
3633   (with-current-buffer gnus-article-buffer
3634     (let ((entity (get-text-property (point-min) 'mime-view-entity)))
3635       (if (and entity (mime-entity-children entity))
3636           (format " (%d parts)" (length (mime-entity-children entity)))
3637         ""))))
3638
3639 (defvar gnus-mime-button-map
3640   (let ((map (make-sparse-keymap)))
3641     (unless (>= (string-to-number emacs-version) 21)
3642       ;; XEmacs doesn't care.
3643       (set-keymap-parent map gnus-article-mode-map))
3644     (define-key map gnus-mouse-2 'gnus-article-push-button)
3645     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
3646     (dolist (c gnus-mime-button-commands)
3647       (define-key map (cadr c) (car c)))
3648     map))
3649
3650 (defun gnus-mime-button-menu (event)
3651   "Construct a context-sensitive menu of MIME commands."
3652   (interactive "e")
3653   (save-window-excursion
3654     (let ((pos (event-start event)))
3655       (select-window (posn-window pos))
3656       (goto-char (posn-point pos))
3657       (gnus-article-check-buffer)
3658       (let ((response (x-popup-menu
3659                        t `("MIME Part"
3660                            ("" ,@(mapcar (lambda (c)
3661                                            (cons (caddr c) (car c)))
3662                                          gnus-mime-button-commands))))))
3663         (if response
3664             (call-interactively response))))))
3665
3666 (defun gnus-mime-view-all-parts (&optional handles)
3667   "View all the MIME parts."
3668   (interactive)
3669   (save-current-buffer
3670     (set-buffer gnus-article-buffer)
3671     (let ((handles (or handles gnus-article-mime-handles))
3672           (mail-parse-charset gnus-newsgroup-charset)
3673           (mail-parse-ignored-charsets
3674            (with-current-buffer gnus-summary-buffer
3675              gnus-newsgroup-ignored-charsets)))
3676       (when handles
3677         (mm-remove-parts handles)
3678         (goto-char (point-min))
3679         (or (search-forward "\n\n") (goto-char (point-max)))
3680         (let (buffer-read-only)
3681           (delete-region (point) (point-max)))
3682         (mm-display-parts handles)))))
3683
3684 (defun gnus-mime-save-part-and-strip ()
3685   "Save the MIME part under point then replace it with an external body."
3686   (interactive)
3687   (gnus-article-check-buffer)
3688   (let* ((data (get-text-property (point) 'gnus-data))
3689          file param)
3690     (if (mm-multiple-handles gnus-article-mime-handles)
3691         (error "This function is not implemented."))
3692     (setq file (and data (mm-save-part data)))
3693     (when file
3694       (with-current-buffer (mm-handle-buffer data)
3695         (erase-buffer)
3696         (insert "Content-Type: " (mm-handle-media-type data))
3697         (mml-insert-parameter-string (cdr (mm-handle-type data))
3698                                      '(charset))
3699         (insert "\n")
3700         (insert "Content-ID: " (message-make-message-id) "\n")
3701         (insert "Content-Transfer-Encoding: binary\n")
3702         (insert "\n"))
3703       (setcdr data
3704               (cdr (mm-make-handle nil
3705                                    `("message/external-body"
3706                                      (access-type . "LOCAL-FILE")
3707                                      (name . ,file)))))
3708       (set-buffer gnus-summary-buffer)
3709       (gnus-article-edit-article
3710        `(lambda ()
3711            (erase-buffer)
3712            (let ((mail-parse-charset (or gnus-article-charset
3713                                          ',gnus-newsgroup-charset))
3714                  (mail-parse-ignored-charsets
3715                   (or gnus-article-ignored-charsets
3716                       ',gnus-newsgroup-ignored-charsets))
3717                  (mbl mml-buffer-list))
3718              (setq mml-buffer-list nil)
3719              (insert-buffer gnus-original-article-buffer)
3720              (mime-to-mml gnus-article-mime-handles)
3721              (setq gnus-article-mime-handles nil)
3722              (make-local-hook 'kill-buffer-hook)
3723              (let ((mbl1 mml-buffer-list))
3724                (setq mml-buffer-list mbl)
3725                (set (make-local-variable 'mml-buffer-list) mbl1))
3726              (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
3727        `(lambda (no-highlight)
3728           (let ((mail-parse-charset (or gnus-article-charset
3729                                         ',gnus-newsgroup-charset))
3730                 (message-options message-options)
3731                 (message-options-set-recipient)
3732                 (mail-parse-ignored-charsets
3733                  (or gnus-article-ignored-charsets
3734                      ',gnus-newsgroup-ignored-charsets)))
3735            (mml-to-mime)
3736            (mml-destroy-buffers)
3737            (remove-hook 'kill-buffer-hook
3738                         'mml-destroy-buffers t)
3739            (kill-local-variable 'mml-buffer-list))
3740           (gnus-summary-edit-article-done
3741            ,(or (mail-header-references gnus-current-headers) "")
3742            ,(gnus-group-read-only-p)
3743            ,gnus-summary-buffer no-highlight))))))
3744
3745 (defun gnus-mime-save-part ()
3746   "Save the MIME part under point."
3747   (interactive)
3748   (gnus-article-check-buffer)
3749   (let ((data (get-text-property (point) 'gnus-data)))
3750     (when data
3751       (mm-save-part data))))
3752
3753 (defun gnus-mime-pipe-part ()
3754   "Pipe the MIME part under point to a process."
3755   (interactive)
3756   (gnus-article-check-buffer)
3757   (let ((data (get-text-property (point) 'gnus-data)))
3758     (when data
3759       (mm-pipe-part data))))
3760
3761 (defun gnus-mime-view-part ()
3762   "Interactively choose a viewing method for the MIME part under point."
3763   (interactive)
3764   (gnus-article-check-buffer)
3765   (let ((data (get-text-property (point) 'gnus-data)))
3766     (when data
3767       (setq gnus-article-mime-handles
3768             (mm-merge-handles
3769              gnus-article-mime-handles (setq data (copy-sequence data))))
3770       (mm-interactively-view-part data))))
3771
3772 (defun gnus-mime-view-part-as-type-internal ()
3773   (gnus-article-check-buffer)
3774   (let* ((name (mail-content-type-get
3775                 (mm-handle-type (get-text-property (point) 'gnus-data))
3776                 'name))
3777          (def-type (and name (mm-default-file-encoding name))))
3778     (and def-type (cons def-type 0))))
3779
3780 (defun gnus-mime-view-part-as-type (&optional mime-type)
3781   "Choose a MIME media type, and view the part as such."
3782   (interactive)
3783   (unless mime-type
3784     (setq mime-type (completing-read
3785                      "View as MIME type: "
3786                      (mapcar #'list (mailcap-mime-types))
3787                      nil nil
3788                      (gnus-mime-view-part-as-type-internal))))
3789   (gnus-article-check-buffer)
3790   (let ((handle (get-text-property (point) 'gnus-data)))
3791     (when handle
3792       (setq handle
3793             (mm-make-handle (mm-handle-buffer handle)
3794                             (cons mime-type (cdr (mm-handle-type handle)))
3795                             (mm-handle-encoding handle)
3796                             (mm-handle-undisplayer handle)
3797                             (mm-handle-disposition handle)
3798                             (mm-handle-description handle)
3799                             (mm-handle-cache handle)
3800                             (mm-handle-id handle)))
3801       (setq gnus-article-mime-handles
3802             (mm-merge-handles gnus-article-mime-handles handle))
3803       (gnus-mm-display-part handle))))
3804
3805 (defun gnus-mime-copy-part (&optional handle)
3806   "Put the the MIME part under point into a new buffer."
3807   (interactive)
3808   (gnus-article-check-buffer)
3809   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3810          (contents (and handle (mm-get-part handle)))
3811          (base (and handle
3812                     (file-name-nondirectory
3813                      (or
3814                       (mail-content-type-get (mm-handle-type handle) 'name)
3815                       (mail-content-type-get (mm-handle-type handle)
3816                                              'filename)
3817                       "*decoded*"))))
3818          (buffer (and base (generate-new-buffer base))))
3819     (when contents
3820       (switch-to-buffer buffer)
3821       (insert contents)
3822       ;; We do it this way to make `normal-mode' set the appropriate mode.
3823       (unwind-protect
3824           (progn
3825             (setq buffer-file-name (expand-file-name base))
3826             (normal-mode))
3827         (setq buffer-file-name nil))
3828       (goto-char (point-min)))))
3829
3830 (defun gnus-mime-inline-part (&optional handle arg)
3831   "Insert the MIME part under point into the current buffer."
3832   (interactive (list nil current-prefix-arg))
3833   (gnus-article-check-buffer)
3834   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3835          contents charset
3836          (b (point))
3837          buffer-read-only)
3838     (when handle
3839       (if (and (not arg) (mm-handle-undisplayer handle))
3840           (mm-remove-part handle)
3841         (setq contents (mm-get-part handle))
3842         (cond
3843          ((not arg)
3844           (setq charset (or (mail-content-type-get
3845                              (mm-handle-type handle) 'charset)
3846                             gnus-newsgroup-charset)))
3847          ((numberp arg)
3848           (if (mm-handle-undisplayer handle)
3849               (mm-remove-part handle))
3850           (setq charset
3851                 (or (cdr (assq arg
3852                                gnus-summary-show-article-charset-alist))
3853                     (read-coding-system "Charset: ")))))
3854         (forward-line 2)
3855         (mm-insert-inline handle
3856                           (if (and charset
3857                                    (setq charset (mm-charset-to-coding-system
3858                                                   charset))
3859                                    (not (eq charset 'ascii)))
3860                               (mm-decode-coding-string contents charset)
3861                             contents))
3862         (goto-char b)))))
3863
3864 (defun gnus-mime-view-part-as-charset (&optional handle arg)
3865   "Insert the MIME part under point into the current buffer."
3866   (interactive (list nil current-prefix-arg))
3867   (gnus-article-check-buffer)
3868   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3869          contents charset
3870          (b (point))
3871          buffer-read-only)
3872     (when handle
3873       (if (mm-handle-undisplayer handle)
3874           (mm-remove-part handle))
3875       (let ((gnus-newsgroup-charset
3876              (or (cdr (assq arg
3877                             gnus-summary-show-article-charset-alist))
3878                  (read-coding-system "Charset: ")))
3879           (gnus-newsgroup-ignored-charsets 'gnus-all))
3880         (gnus-article-press-button)))))
3881
3882 (defun gnus-mime-externalize-part (&optional handle)
3883   "View the MIME part under point with an external viewer."
3884   (interactive)
3885   (gnus-article-check-buffer)
3886   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3887          (mm-user-display-methods nil)
3888          (mm-inlined-types nil)
3889          (mail-parse-charset gnus-newsgroup-charset)
3890          (mail-parse-ignored-charsets
3891           (save-excursion (set-buffer gnus-summary-buffer)
3892                           gnus-newsgroup-ignored-charsets)))
3893     (when handle
3894       (if (mm-handle-undisplayer handle)
3895           (mm-remove-part handle)
3896         (mm-display-part handle)))))
3897
3898 (defun gnus-mime-internalize-part (&optional handle)
3899   "View the MIME part under point with an internal viewer.
3900 In no internal viewer is available, use an external viewer."
3901   (interactive)
3902   (gnus-article-check-buffer)
3903   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3904          (mm-inlined-types '(".*"))
3905          (mm-inline-large-images t)
3906          (mail-parse-charset gnus-newsgroup-charset)
3907          (mail-parse-ignored-charsets
3908           (save-excursion (set-buffer gnus-summary-buffer)
3909                           gnus-newsgroup-ignored-charsets)))
3910     (when handle
3911       (if (mm-handle-undisplayer handle)
3912           (mm-remove-part handle)
3913         (mm-display-part handle)))))
3914
3915 (defun gnus-mime-action-on-part (&optional action)
3916   "Do something with the MIME attachment at \(point\)."
3917   (interactive
3918    (list (completing-read "Action: " gnus-mime-action-alist)))
3919   (gnus-article-check-buffer)
3920   (let ((action-pair (assoc action gnus-mime-action-alist)))
3921     (if action-pair
3922         (funcall (cdr action-pair)))))
3923
3924 (defun gnus-article-part-wrapper (n function)
3925   (save-current-buffer
3926     (set-buffer gnus-article-buffer)
3927     (when (> n (length gnus-article-mime-handle-alist))
3928       (error "No such part"))
3929     (gnus-article-goto-part n)
3930     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3931       (funcall function handle))))
3932
3933 (defun gnus-article-pipe-part (n)
3934   "Pipe MIME part N, which is the numerical prefix."
3935   (interactive "p")
3936   (gnus-article-part-wrapper n 'mm-pipe-part))
3937
3938 (defun gnus-article-save-part (n)
3939   "Save MIME part N, which is the numerical prefix."
3940   (interactive "p")
3941   (gnus-article-part-wrapper n 'mm-save-part))
3942
3943 (defun gnus-article-interactively-view-part (n)
3944   "View MIME part N interactively, which is the numerical prefix."
3945   (interactive "p")
3946   (gnus-article-part-wrapper n 'mm-interactively-view-part))
3947
3948 (defun gnus-article-copy-part (n)
3949   "Copy MIME part N, which is the numerical prefix."
3950   (interactive "p")
3951   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
3952
3953 (defun gnus-article-view-part-as-charset (n)
3954   "Copy MIME part N, which is the numerical prefix."
3955   (interactive "p")
3956   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
3957
3958 (defun gnus-article-externalize-part (n)
3959   "View MIME part N externally, which is the numerical prefix."
3960   (interactive "p")
3961   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
3962
3963 (defun gnus-article-inline-part (n)
3964   "Inline MIME part N, which is the numerical prefix."
3965   (interactive "p")
3966   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
3967
3968 (defun gnus-article-mime-match-handle-first (condition)
3969   (if condition
3970       (let ((alist gnus-article-mime-handle-alist) ihandle n)
3971         (while (setq ihandle (pop alist))
3972           (if (and (cond
3973                     ((functionp condition)
3974                      (funcall condition (cdr ihandle)))
3975                     ((eq condition 'undisplayed)
3976                      (not (or (mm-handle-undisplayer (cdr ihandle))
3977                               (equal (mm-handle-media-type (cdr ihandle))
3978                                      "multipart/alternative"))))
3979                     ((eq condition 'undisplayed-alternative)
3980                      (not (mm-handle-undisplayer (cdr ihandle))))
3981                     (t t))
3982                    (gnus-article-goto-part (car ihandle))
3983                    (or (not n) (< (car ihandle) n)))
3984               (setq n (car ihandle))))
3985         (or n 1))
3986     1))
3987
3988 (defun gnus-article-view-part (&optional n)
3989   "View MIME part N, which is the numerical prefix."
3990   (interactive "P")
3991   (save-current-buffer
3992     (set-buffer gnus-article-buffer)
3993     (or (numberp n) (setq n (gnus-article-mime-match-handle-first
3994                              gnus-article-mime-match-handle-function)))
3995     (when (> n (length gnus-article-mime-handle-alist))
3996       (error "No such part"))
3997     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3998       (when (gnus-article-goto-part n)
3999         (if (equal (car handle) "multipart/alternative")
4000             (gnus-article-press-button)
4001           (when (eq (gnus-mm-display-part handle) 'internal)
4002             (gnus-set-window-start)))))))
4003
4004 (defsubst gnus-article-mime-total-parts ()
4005   (if (bufferp (car gnus-article-mime-handles))
4006       1 ;; single part
4007     (1- (length gnus-article-mime-handles))))
4008
4009 (defun gnus-mm-display-part (handle)
4010   "Display HANDLE and fix MIME button."
4011   (let ((id (get-text-property (point) 'gnus-part))
4012         (point (point))
4013         buffer-read-only)
4014     (forward-line 1)
4015     (prog1
4016         (let ((window (selected-window))
4017               (mail-parse-charset gnus-newsgroup-charset)
4018               (mail-parse-ignored-charsets
4019                (save-excursion (set-buffer gnus-summary-buffer)
4020                                gnus-newsgroup-ignored-charsets)))
4021           (save-excursion
4022             (unwind-protect
4023                 (let ((win (get-buffer-window (current-buffer) t))
4024                       (beg (point)))
4025                   (when win
4026                     (select-window win))
4027                   (goto-char point)
4028                   (forward-line)
4029                   (if (mm-handle-displayed-p handle)
4030                       ;; This will remove the part.
4031                       (mm-display-part handle)
4032                     (save-restriction
4033                       (narrow-to-region (point) (1+ (point)))
4034                       (mm-display-part handle)
4035                       ;; We narrow to the part itself and
4036                       ;; then call the treatment functions.
4037                       (goto-char (point-min))
4038                       (forward-line 1)
4039                       (narrow-to-region (point) (point-max))
4040                       (gnus-treat-article
4041                        nil id
4042                        (gnus-article-mime-total-parts)
4043                        (mm-handle-media-type handle)))))
4044               (select-window window))))
4045       (goto-char point)
4046       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
4047       (gnus-insert-mime-button
4048        handle id (list (mm-handle-displayed-p handle)))
4049       (goto-char point))))
4050
4051 (defun gnus-article-goto-part (n)
4052   "Go to MIME part N."
4053   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
4054     (when point
4055       (goto-char point))))
4056
4057 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
4058   (let ((gnus-tmp-name
4059          (or (mail-content-type-get (mm-handle-type handle)
4060                                     'name)
4061              (mail-content-type-get (mm-handle-disposition handle)
4062                                     'filename)
4063              (mail-content-type-get (mm-handle-type handle)
4064                                     'url)
4065              ""))
4066         (gnus-tmp-type (mm-handle-media-type handle))
4067         (gnus-tmp-description
4068          (mail-decode-encoded-word-string (or (mm-handle-description handle)
4069                                               "")))
4070         (gnus-tmp-dots
4071          (if (if displayed (car displayed)
4072                (mm-handle-displayed-p handle))
4073              "" "..."))
4074         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
4075                            (buffer-size)))
4076         gnus-tmp-type-long b e)
4077     (when (string-match ".*/" gnus-tmp-name)
4078       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
4079     (setq gnus-tmp-type-long (concat gnus-tmp-type
4080                                      (and (not (equal gnus-tmp-name ""))
4081                                           (concat "; " gnus-tmp-name))))
4082     (or (equal gnus-tmp-description "")
4083         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
4084     (unless (bolp)
4085       (insert "\n"))
4086     (setq b (point))
4087     (gnus-eval-format
4088      gnus-mime-button-line-format gnus-mime-button-line-format-alist
4089      `(keymap ,gnus-mime-button-map
4090               ,@(if (>= (string-to-number emacs-version) 21)
4091                     nil
4092                   (list 'local-map gnus-mime-button-map))
4093               gnus-callback gnus-mm-display-part
4094               gnus-part ,gnus-tmp-id
4095               article-type annotation
4096               gnus-data ,handle))
4097     (setq e (point))
4098     (widget-convert-button
4099      'link b e
4100      :mime-handle handle
4101      :action 'gnus-widget-press-button
4102      :button-keymap gnus-mime-button-map
4103      :help-echo
4104      (lambda (widget/window &optional overlay pos)
4105        ;; Needed to properly clear the message due to a bug in
4106        ;; wid-edit (XEmacs only).
4107        (if (boundp 'help-echo-owns-message)
4108            (setq help-echo-owns-message t))
4109        (format
4110         "%S: %s the MIME part; %S: more options"
4111         (aref gnus-mouse-2 0)
4112         ;; XEmacs will get a single widget arg; Emacs 21 will get
4113         ;; window, overlay, position.
4114         (if (mm-handle-displayed-p
4115              (if overlay
4116                  (with-current-buffer (gnus-overlay-buffer overlay)
4117                    (widget-get (widget-at (gnus-overlay-start overlay))
4118                                :mime-handle))
4119                (widget-get widget/window :mime-handle)))
4120             "hide" "show")
4121         (aref gnus-down-mouse-3 0))))))
4122
4123 (defun gnus-widget-press-button (elems el)
4124   (goto-char (widget-get elems :from))
4125   (gnus-article-press-button))
4126
4127 (defvar gnus-displaying-mime nil)
4128
4129 (defun gnus-display-mime (&optional ihandles)
4130   "Display the MIME parts."
4131   (save-excursion
4132     (save-selected-window
4133       (let ((window (get-buffer-window gnus-article-buffer))
4134             (point (point)))
4135         (when window
4136           (select-window window)
4137           ;; We have to do this since selecting the window
4138           ;; may change the point.  So we set the window point.
4139           (set-window-point window point)))
4140       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
4141              buffer-read-only handle name type b e display)
4142         (when (and (not ihandles)
4143                    (not gnus-displaying-mime))
4144           ;; Top-level call; we clean up.
4145           (when gnus-article-mime-handles
4146             (mm-destroy-parts gnus-article-mime-handles)
4147             (setq gnus-article-mime-handle-alist nil));; A trick.
4148           (setq gnus-article-mime-handles handles)
4149           ;; We allow users to glean info from the handles.
4150           (when gnus-article-mime-part-function
4151             (gnus-mime-part-function handles)))
4152         (if (and handles
4153                  (or (not (stringp (car handles)))
4154                      (cdr handles)))
4155             (progn
4156               (when (and (not ihandles)
4157                          (not gnus-displaying-mime))
4158                 ;; Clean up for mime parts.
4159                 (article-goto-body)
4160                 (delete-region (point) (point-max)))
4161               (let ((gnus-displaying-mime t))
4162                 (gnus-mime-display-part handles)))
4163           (save-restriction
4164             (article-goto-body)
4165             (narrow-to-region (point) (point-max))
4166             (gnus-treat-article nil 1 1)
4167             (widen)))
4168         (unless ihandles
4169           ;; Highlight the headers.
4170           (save-excursion
4171             (save-restriction
4172               (article-goto-body)
4173               (narrow-to-region (point-min) (point))
4174               (gnus-treat-article 'head))))))))
4175
4176 (defvar gnus-mime-display-multipart-as-mixed nil)
4177
4178 (defun gnus-mime-display-part (handle)
4179   (cond
4180    ;; Single part.
4181    ((not (stringp (car handle)))
4182     (gnus-mime-display-single handle))
4183    ;; User-defined multipart
4184    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
4185     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
4186              handle))
4187    ;; multipart/alternative
4188    ((and (equal (car handle) "multipart/alternative")
4189          (not gnus-mime-display-multipart-as-mixed))
4190     (let ((id (1+ (length gnus-article-mime-handle-alist))))
4191       (push (cons id handle) gnus-article-mime-handle-alist)
4192       (gnus-mime-display-alternative (cdr handle) nil nil id)))
4193    ;; multipart/related
4194    ((and (equal (car handle) "multipart/related")
4195          (not gnus-mime-display-multipart-as-mixed))
4196     ;;;!!!We should find the start part, but we just default
4197     ;;;!!!to the first part.
4198     ;;(gnus-mime-display-part (cadr handle))
4199     ;;;!!! Most multipart/related is an HTML message plus images.
4200     ;;;!!! Unfortunately we are unable to let W3 display those
4201     ;;;!!! included images, so we just display it as a mixed multipart.
4202     ;;(gnus-mime-display-mixed (cdr handle))
4203     ;;;!!! No, w3 can display everything just fine.
4204     (gnus-mime-display-part (cadr handle)))
4205    ((equal (car handle) "multipart/signed")
4206     (or (memq 'signed gnus-article-wash-types)
4207         (push 'signed gnus-article-wash-types))
4208     (gnus-mime-display-security handle))
4209    ((equal (car handle) "multipart/encrypted")
4210     (or (memq 'encrypted gnus-article-wash-types)
4211         (push 'encrypted gnus-article-wash-types))
4212     (gnus-mime-display-security handle))
4213    ;; Other multiparts are handled like multipart/mixed.
4214    (t
4215     (gnus-mime-display-mixed (cdr handle)))))
4216
4217 (defun gnus-mime-part-function (handles)
4218   (if (stringp (car handles))
4219       (mapcar 'gnus-mime-part-function (cdr handles))
4220     (funcall gnus-article-mime-part-function handles)))
4221
4222 (defun gnus-mime-display-mixed (handles)
4223   (mapcar 'gnus-mime-display-part handles))
4224
4225 (defun gnus-mime-display-single (handle)
4226   (let ((type (mm-handle-media-type handle))
4227         (ignored gnus-ignored-mime-types)
4228         (not-attachment t)
4229         (move nil)
4230         display text)
4231     (catch 'ignored
4232       (progn
4233         (while ignored
4234           (when (string-match (pop ignored) type)
4235             (throw 'ignored nil)))
4236         (if (and (setq not-attachment
4237                        (and (not (mm-inline-override-p handle))
4238                             (or (not (mm-handle-disposition handle))
4239                                 (equal (car (mm-handle-disposition handle))
4240                                        "inline")
4241                                 (mm-attachment-override-p handle))))
4242                  (mm-automatic-display-p handle)
4243                  (or (mm-inlined-p handle)
4244                      (mm-automatic-external-display-p type)))
4245             (setq display t)
4246           (when (equal (mm-handle-media-supertype handle) "text")
4247             (setq text t)))
4248         (let ((id (1+ (length gnus-article-mime-handle-alist)))
4249               beg)
4250           (push (cons id handle) gnus-article-mime-handle-alist)
4251           (when (or (not display)
4252                     (not (gnus-unbuttonized-mime-type-p type)))
4253             ;(gnus-article-insert-newline)
4254             (gnus-insert-mime-button
4255              handle id (list (or display (and not-attachment text))))
4256             (gnus-article-insert-newline)
4257             ;(gnus-article-insert-newline)
4258             ;; Remember modify the number of forward lines.
4259             (setq move t))
4260           (setq beg (point))
4261           (cond
4262            (display
4263             (when move
4264               (forward-line -1)
4265               (setq beg (point)))
4266             (let ((mail-parse-charset gnus-newsgroup-charset)
4267                   (mail-parse-ignored-charsets
4268                    (save-excursion (condition-case ()
4269                                        (set-buffer gnus-summary-buffer)
4270                                      (error))
4271                                    gnus-newsgroup-ignored-charsets)))
4272               (mm-display-part handle t))
4273             (goto-char (point-max)))
4274            ((and text not-attachment)
4275             (when move
4276               (forward-line -1)
4277               (setq beg (point)))
4278             (gnus-article-insert-newline)
4279             (mm-insert-inline handle (mm-get-part handle))
4280             (goto-char (point-max))))
4281           ;; Do highlighting.
4282           (save-excursion
4283             (save-restriction
4284               (narrow-to-region beg (point))
4285               (gnus-treat-article
4286                nil id
4287                (gnus-article-mime-total-parts)
4288                (mm-handle-media-type handle)))))))))
4289
4290 (defun gnus-unbuttonized-mime-type-p (type)
4291   "Say whether TYPE is to be unbuttonized."
4292   (unless gnus-inhibit-mime-unbuttonizing
4293     (catch 'found
4294       (let ((types gnus-unbuttonized-mime-types))
4295         (while types
4296           (when (string-match (pop types) type)
4297             (throw 'found t)))))))
4298
4299 (defun gnus-article-insert-newline ()
4300   "Insert a newline, but mark it as undeletable."
4301   (gnus-put-text-property
4302    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
4303
4304 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
4305   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
4306          (ihandles handles)
4307          (point (point))
4308          handle buffer-read-only from props begend not-pref)
4309     (save-window-excursion
4310       (save-restriction
4311         (when ibegend
4312           (narrow-to-region (car ibegend)
4313                             (or (cdr ibegend)
4314                                 (progn
4315                                   (goto-char (car ibegend))
4316                                   (forward-line 2)
4317                                   (point))))
4318           (delete-region (point-min) (point-max))
4319           (mm-remove-parts handles))
4320         (setq begend (list (point-marker)))
4321         ;; Do the toggle.
4322         (unless (setq not-pref (cadr (member preferred ihandles)))
4323           (setq not-pref (car ihandles)))
4324         (when (or ibegend
4325                   (not preferred)
4326                   (not (gnus-unbuttonized-mime-type-p
4327                         "multipart/alternative")))
4328           (gnus-add-text-properties
4329            (setq from (point))
4330            (progn
4331              (insert (format "%d.  " id))
4332              (point))
4333            `(gnus-callback
4334              (lambda (handles)
4335                (unless ,(not ibegend)
4336                  (setq gnus-article-mime-handle-alist
4337                        ',gnus-article-mime-handle-alist))
4338                (gnus-mime-display-alternative
4339                 ',ihandles ',not-pref ',begend ,id))
4340              ,@(if (>= (string-to-number emacs-version) 21)
4341                    nil ;; XEmacs doesn't care
4342                  (list 'local-map gnus-mime-button-map))
4343              ,gnus-mouse-face-prop ,gnus-article-mouse-face
4344              face ,gnus-article-button-face
4345              keymap ,gnus-mime-button-map
4346              gnus-part ,id
4347              gnus-data ,handle))
4348           (widget-convert-button 'link from (point)
4349                                  :action 'gnus-widget-press-button
4350                                  :button-keymap gnus-widget-button-keymap)
4351           ;; Do the handles
4352           (while (setq handle (pop handles))
4353             (gnus-add-text-properties
4354              (setq from (point))
4355              (progn
4356                (insert (format "(%c) %-18s"
4357                                (if (equal handle preferred) ?* ? )
4358                                (mm-handle-media-type handle)))
4359                (point))
4360              `(gnus-callback
4361                (lambda (handles)
4362                  (unless ,(not ibegend)
4363                    (setq gnus-article-mime-handle-alist
4364                          ',gnus-article-mime-handle-alist))
4365                  (gnus-mime-display-alternative
4366                   ',ihandles ',handle ',begend ,id))
4367                ,@(if (>= (string-to-number emacs-version) 21)
4368                      nil ;; XEmacs doesn't care
4369                    (list 'local-map gnus-mime-button-map))
4370                ,gnus-mouse-face-prop ,gnus-article-mouse-face
4371                face ,gnus-article-button-face
4372                keymap ,gnus-mime-button-map
4373                gnus-part ,id
4374                gnus-data ,handle))
4375             (widget-convert-button 'link from (point)
4376                                    :action 'gnus-widget-press-button
4377                                    :button-keymap gnus-widget-button-keymap)
4378             (insert "  "))
4379           (insert "\n\n"))
4380         (when preferred
4381           (if (stringp (car preferred))
4382               (gnus-display-mime preferred)
4383             (let ((mail-parse-charset gnus-newsgroup-charset)
4384                   (mail-parse-ignored-charsets
4385                    (save-excursion (set-buffer gnus-summary-buffer)
4386                                    gnus-newsgroup-ignored-charsets)))
4387               (mm-display-part preferred)
4388               ;; Do highlighting.
4389               (save-excursion
4390                 (save-restriction
4391                   (narrow-to-region (car begend) (point-max))
4392                   (gnus-treat-article
4393                    nil (length gnus-article-mime-handle-alist)
4394                    (gnus-article-mime-total-parts)
4395                    (mm-handle-media-type handle))))))
4396           (goto-char (point-max))
4397           (setcdr begend (point-marker)))))
4398     (when ibegend
4399       (goto-char point))))
4400
4401 (defun gnus-article-wash-status ()
4402   "Return a string which display status of article washing."
4403   (save-excursion
4404     (set-buffer gnus-article-buffer)
4405     (let ((cite (memq 'cite gnus-article-wash-types))
4406           (headers (memq 'headers gnus-article-wash-types))
4407           (boring (memq 'boring-headers gnus-article-wash-types))
4408           (pgp (memq 'pgp gnus-article-wash-types))
4409           (pem (memq 'pem gnus-article-wash-types))
4410           (signed (memq 'signed gnus-article-wash-types))
4411           (encrypted (memq 'encrypted gnus-article-wash-types))
4412           (signature (memq 'signature gnus-article-wash-types))
4413           (overstrike (memq 'overstrike gnus-article-wash-types))
4414           (emphasis (memq 'emphasis gnus-article-wash-types)))
4415       (format "%c%c%c%c%c%c%c"
4416               (if cite ?c ? )
4417               (if (or headers boring) ?h ? )
4418               (if (or pgp pem signed encrypted) ?p ? )
4419               (if signature ?s ? )
4420               (if overstrike ?o ? )
4421               (if gnus-show-mime ?m ? )
4422               (if emphasis ?e ? )))))
4423
4424 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
4425
4426 (defun gnus-article-maybe-hide-headers ()
4427   "Hide unwanted headers if `gnus-have-all-headers' is nil.
4428 Provided for backwards compatibility."
4429   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
4430                  (not (save-excursion (set-buffer gnus-summary-buffer)
4431                                       gnus-have-all-headers)))
4432              (not gnus-inhibit-hiding))
4433     (gnus-article-hide-headers)))
4434
4435 ;;; Article savers.
4436
4437 (defun gnus-output-to-file (file-name)
4438   "Append the current article to a file named FILE-NAME."
4439   (let ((artbuf (current-buffer)))
4440     (with-temp-buffer
4441       (insert-buffer-substring artbuf)
4442       ;; Append newline at end of the buffer as separator, and then
4443       ;; save it to file.
4444       (goto-char (point-max))
4445       (insert "\n")
4446       (write-region-as-binary (point-min) (point-max) file-name 'append)
4447       t)))
4448
4449 (defun gnus-narrow-to-page (&optional arg)
4450   "Narrow the article buffer to a page.
4451 If given a numerical ARG, move forward ARG pages."
4452   (interactive "P")
4453   (setq arg (if arg (prefix-numeric-value arg) 0))
4454   (save-excursion
4455     (set-buffer gnus-article-buffer)
4456     (goto-char (point-min))
4457     (widen)
4458     ;; Remove any old next/prev buttons.
4459     (when (gnus-visual-p 'page-marker)
4460       (let ((buffer-read-only nil))
4461         (gnus-remove-text-with-property 'gnus-prev)
4462         (gnus-remove-text-with-property 'gnus-next)))
4463     (when
4464         (cond ((< arg 0)
4465                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
4466               ((> arg 0)
4467                (re-search-forward page-delimiter nil 'move arg)))
4468       (goto-char (match-end 0)))
4469     (narrow-to-region
4470      (point)
4471      (if (re-search-forward page-delimiter nil 'move)
4472          (match-beginning 0)
4473        (point)))
4474     (when (and (gnus-visual-p 'page-marker)
4475                (not (= (point-min) 1)))
4476       (save-excursion
4477         (goto-char (point-min))
4478         (gnus-insert-prev-page-button)))
4479     (when (and (gnus-visual-p 'page-marker)
4480                (< (+ (point-max) 2) (buffer-size)))
4481       (save-excursion
4482         (goto-char (point-max))
4483         (gnus-insert-next-page-button)))))
4484
4485 ;; Article mode commands
4486
4487 (defun gnus-article-goto-next-page ()
4488   "Show the next page of the article."
4489   (interactive)
4490   (when (gnus-article-next-page)
4491     (goto-char (point-min))
4492     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
4493
4494 (defun gnus-article-goto-prev-page ()
4495   "Show the next page of the article."
4496   (interactive)
4497   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
4498     (gnus-article-prev-page nil)))
4499
4500 (defun gnus-article-next-page (&optional lines)
4501   "Show the next page of the current article.
4502 If end of article, return non-nil.  Otherwise return nil.
4503 Argument LINES specifies lines to be scrolled up."
4504   (interactive "p")
4505   (let ((start (window-start))
4506         end-of-buffer end-of-page)
4507     (save-excursion
4508       (move-to-window-line -1)
4509       (if (<= (point) start)
4510           (progn
4511             (forward-line 2)
4512             (setq start (point)))
4513         (forward-line 1)
4514         (setq start nil))
4515       (unless (or (cond ((eq (1+ (buffer-size)) (point))
4516                          (and (pos-visible-in-window-p)
4517                               (setq end-of-buffer t)))
4518                         ((eobp)
4519                          (setq end-of-page t)))
4520                   (not lines))
4521         (move-to-window-line lines)
4522         (unless (search-backward "\n\n" nil t)
4523           (setq start (point)))))
4524     (cond (end-of-buffer t)
4525           (end-of-page
4526            (gnus-narrow-to-page 1)
4527            nil)
4528           (t
4529            (if start
4530                (set-window-start (selected-window) start)
4531              (let (window-pixel-scroll-increment)
4532                (scroll-up lines)))
4533            nil))))
4534
4535 (defun gnus-article-prev-page (&optional lines)
4536   "Show previous page of current article.
4537 Argument LINES specifies lines to be scrolled down."
4538   (interactive "p")
4539   (let (beginning-of-buffer beginning-of-page)
4540     (save-excursion
4541       (move-to-window-line 0)
4542       (cond ((eq 1 (point))
4543              (setq beginning-of-buffer t))
4544             ((bobp)
4545              (setq beginning-of-page t))))
4546     (cond (beginning-of-buffer)
4547           (beginning-of-page
4548            (gnus-narrow-to-page -1))
4549           (t
4550            (condition-case nil
4551                (let (window-pixel-scroll-increment)
4552                  (scroll-down lines))
4553              (beginning-of-buffer
4554               (goto-char (point-min))))))))
4555
4556 (defun gnus-article-refer-article ()
4557   "Read article specified by message-id around point."
4558   (interactive)
4559   (let ((point (point)))
4560     (search-forward ">" nil t)          ;Move point to end of "<....>".
4561     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
4562         (let ((message-id (match-string 1)))
4563           (goto-char point)
4564           (set-buffer gnus-summary-buffer)
4565           (gnus-summary-refer-article message-id))
4566       (goto-char (point))
4567       (error "No references around point"))))
4568
4569 (defun gnus-article-show-summary ()
4570   "Reconfigure windows to show summary buffer."
4571   (interactive)
4572   (if (not (gnus-buffer-live-p gnus-summary-buffer))
4573       (error "There is no summary buffer for this article buffer")
4574     (gnus-article-set-globals)
4575     (gnus-configure-windows 'article)
4576     (gnus-summary-goto-subject gnus-current-article)
4577     (gnus-summary-position-point)))
4578
4579 (defun gnus-article-describe-briefly ()
4580   "Describe article mode commands briefly."
4581   (interactive)
4582   (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")))
4583
4584 (defun gnus-article-summary-command ()
4585   "Execute the last keystroke in the summary buffer."
4586   (interactive)
4587   (let ((obuf (current-buffer))
4588         (owin (current-window-configuration))
4589         func)
4590     (switch-to-buffer gnus-article-current-summary 'norecord)
4591     (setq func (lookup-key (current-local-map) (this-command-keys)))
4592     (call-interactively func)
4593     (set-buffer obuf)
4594     (set-window-configuration owin)
4595     (set-window-point (get-buffer-window (current-buffer)) (point))))
4596
4597 (defun gnus-article-summary-command-nosave ()
4598   "Execute the last keystroke in the summary buffer."
4599   (interactive)
4600   (let (func)
4601     (pop-to-buffer gnus-article-current-summary 'norecord)
4602     (setq func (lookup-key (current-local-map) (this-command-keys)))
4603     (call-interactively func)))
4604
4605 (defun gnus-article-check-buffer ()
4606   "Beep if not in an article buffer."
4607   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
4608     (error "Command invoked outside of a Gnus article buffer")))
4609
4610 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
4611   "Read a summary buffer key sequence and execute it from the article buffer."
4612   (interactive "P")
4613   (gnus-article-check-buffer)
4614   (let ((nosaves
4615          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
4616            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
4617            "=" "^" "\M-^" "|"))
4618         (nosave-but-article
4619          '("A\r"))
4620         (nosave-in-article
4621          '("\C-d"))
4622         (up-to-top
4623          '("n" "Gn" "p" "Gp"))
4624         keys new-sum-point)
4625     (save-excursion
4626       (set-buffer gnus-article-current-summary)
4627       (let (gnus-pick-mode)
4628         (push (or key last-command-event) unread-command-events)
4629         (setq keys (static-if (featurep 'xemacs)
4630                        (events-to-keys (read-key-sequence nil))
4631                      (read-key-sequence nil)))))
4632     (message "")
4633
4634     (if (or (member keys nosaves)
4635             (member keys nosave-but-article)
4636             (member keys nosave-in-article))
4637         (let (func)
4638           (save-window-excursion
4639             (pop-to-buffer gnus-article-current-summary 'norecord)
4640             ;; We disable the pick minor mode commands.
4641             (let (gnus-pick-mode)
4642               (setq func (lookup-key (current-local-map) keys))))
4643           (if (or (not func)
4644                   (numberp func))
4645               (ding)
4646             (unless (member keys nosave-in-article)
4647               (set-buffer gnus-article-current-summary))
4648             (call-interactively func)
4649             (setq new-sum-point (point)))
4650           (when (member keys nosave-but-article)
4651             (pop-to-buffer gnus-article-buffer 'norecord)))
4652       ;; These commands should restore window configuration.
4653       (let ((obuf (current-buffer))
4654             (owin (current-window-configuration))
4655             (opoint (point))
4656             (summary gnus-article-current-summary)
4657             func in-buffer selected)
4658         (if not-restore-window
4659             (pop-to-buffer summary 'norecord)
4660           (switch-to-buffer summary 'norecord))
4661         (setq in-buffer (current-buffer))
4662         ;; We disable the pick minor mode commands.
4663         (if (and (setq func (let (gnus-pick-mode)
4664                               (lookup-key (current-local-map) keys)))
4665                  (functionp func))
4666             (progn
4667               (call-interactively func)
4668               (setq new-sum-point (point))
4669               (when (eq in-buffer (current-buffer))
4670                 (setq selected (gnus-summary-select-article))
4671                 (set-buffer obuf)
4672                 (unless not-restore-window
4673                   (set-window-configuration owin))
4674                 (when (eq selected 'old)
4675                   (article-goto-body)
4676                   (set-window-start (get-buffer-window (current-buffer))
4677                                     1)
4678                   (set-window-point (get-buffer-window (current-buffer))
4679                                     (point)))
4680                 (let ((win (get-buffer-window gnus-article-current-summary)))
4681                   (when win
4682                     (set-window-point win new-sum-point))))    )
4683           (switch-to-buffer gnus-article-buffer)
4684           (ding))))))
4685
4686 (defun gnus-article-describe-key (key)
4687   "Display documentation of the function invoked by KEY.  KEY is a string."
4688   (interactive "kDescribe key: ")
4689   (gnus-article-check-buffer)
4690   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4691       (save-excursion
4692         (set-buffer gnus-article-current-summary)
4693         (let (gnus-pick-mode)
4694           (push (elt key 0) unread-command-events)
4695           (setq key (if (featurep 'xemacs)
4696                         (events-to-keys (read-key-sequence "Describe key: "))
4697                       (read-key-sequence "Describe key: "))))
4698         (describe-key key))
4699     (describe-key key)))
4700
4701 (defun gnus-article-describe-key-briefly (key &optional insert)
4702   "Display documentation of the function invoked by KEY.  KEY is a string."
4703   (interactive "kDescribe key: \nP")
4704   (gnus-article-check-buffer)
4705   (if (eq (key-binding key) 'gnus-article-read-summary-keys)
4706       (save-excursion
4707         (set-buffer gnus-article-current-summary)
4708         (let (gnus-pick-mode)
4709           (push (elt key 0) unread-command-events)
4710           (setq key (if (featurep 'xemacs)
4711                         (events-to-keys (read-key-sequence "Describe key: "))
4712                       (read-key-sequence "Describe key: "))))
4713         (describe-key-briefly key insert))
4714     (describe-key-briefly key insert)))
4715
4716 (defun gnus-article-hide (&optional arg force)
4717   "Hide all the gruft in the current article.
4718 This means that PGP stuff, signatures, cited text and (some)
4719 headers will be hidden.
4720 If given a prefix, show the hidden text instead."
4721   (interactive (append (gnus-article-hidden-arg) (list 'force)))
4722   (gnus-article-hide-headers arg)
4723   (gnus-article-hide-list-identifiers arg)
4724   (gnus-article-hide-pgp arg)
4725   (gnus-article-hide-citation-maybe arg force)
4726   (gnus-article-hide-signature arg))
4727
4728 (defun gnus-article-maybe-highlight ()
4729   "Do some article highlighting if article highlighting is requested."
4730   (when (gnus-visual-p 'article-highlight 'highlight)
4731     (gnus-article-highlight-some)))
4732
4733 (defun gnus-check-group-server ()
4734   ;; Make sure the connection to the server is alive.
4735   (unless (gnus-server-opened
4736            (gnus-find-method-for-group gnus-newsgroup-name))
4737     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
4738     (gnus-request-group gnus-newsgroup-name t)))
4739
4740 (defun gnus-request-article-this-buffer (article group)
4741   "Get an article and insert it into this buffer."
4742   (let (do-update-line sparse-header)
4743     (prog1
4744         (save-excursion
4745           (erase-buffer)
4746           (gnus-kill-all-overlays)
4747           (setq group (or group gnus-newsgroup-name))
4748
4749           ;; Using `gnus-request-article' directly will insert the article into
4750           ;; `nntp-server-buffer' - so we'll save some time by not having to
4751           ;; copy it from the server buffer into the article buffer.
4752
4753           ;; We only request an article by message-id when we do not have the
4754           ;; headers for it, so we'll have to get those.
4755           (when (stringp article)
4756             (gnus-read-header article))
4757
4758           ;; If the article number is negative, that means that this article
4759           ;; doesn't belong in this newsgroup (possibly), so we find its
4760           ;; message-id and request it by id instead of number.
4761           (when (and (numberp article)
4762                      gnus-summary-buffer
4763                      (get-buffer gnus-summary-buffer)
4764                      (gnus-buffer-exists-p gnus-summary-buffer))
4765             (save-excursion
4766               (set-buffer gnus-summary-buffer)
4767               (let ((header (gnus-summary-article-header article)))
4768                 (when (< article 0)
4769                   (cond
4770                    ((memq article gnus-newsgroup-sparse)
4771                     ;; This is a sparse gap article.
4772                     (setq do-update-line article)
4773                     (setq article (mail-header-id header))
4774                     (setq sparse-header (gnus-read-header article))
4775                     (setq gnus-newsgroup-sparse
4776                           (delq article gnus-newsgroup-sparse)))
4777                    ((vectorp header)
4778                     ;; It's a real article.
4779                     (setq article (mail-header-id header)))
4780                    (t
4781                     ;; It is an extracted pseudo-article.
4782                     (setq article 'pseudo)
4783                     (gnus-request-pseudo-article header))))
4784
4785                 (let ((method (gnus-find-method-for-group
4786                                gnus-newsgroup-name)))
4787                   (when (and (eq (car method) 'nneething)
4788                              (vectorp header))
4789                     (let ((dir (expand-file-name
4790                                 (mail-header-subject header)
4791                                 (file-name-as-directory
4792                                  (or (cadr (assq 'nneething-address method))
4793                                      (nth 1 method))))))
4794                       (when (file-directory-p dir)
4795                         (setq article 'nneething)
4796                         (gnus-group-enter-directory dir))))))))
4797
4798           (cond
4799            ;; Refuse to select canceled articles.
4800            ((and (numberp article)
4801                  gnus-summary-buffer
4802                  (get-buffer gnus-summary-buffer)
4803                  (gnus-buffer-exists-p gnus-summary-buffer)
4804                  (eq (cdr (save-excursion
4805                             (set-buffer gnus-summary-buffer)
4806                             (assq article gnus-newsgroup-reads)))
4807                      gnus-canceled-mark))
4808             nil)
4809            ;; Check the backlog.
4810            ((and gnus-keep-backlog
4811                  (gnus-backlog-request-article group article (current-buffer)))
4812             'article)
4813            ;; Check asynchronous pre-fetch.
4814            ((gnus-async-request-fetched-article group article (current-buffer))
4815             (gnus-async-prefetch-next group article gnus-summary-buffer)
4816             (when (and (numberp article) gnus-keep-backlog)
4817               (gnus-backlog-enter-article group article (current-buffer)))
4818             'article)
4819            ;; Check the cache.
4820            ((and gnus-use-cache
4821                  (numberp article)
4822                  (gnus-cache-request-article article group))
4823             'article)
4824            ;; Get the article and put into the article buffer.
4825            ((or (stringp article)
4826                 (numberp article))
4827             (let ((gnus-override-method gnus-override-method)
4828                   (methods (and (stringp article)
4829                                 gnus-refer-article-method))
4830                   result
4831                   (buffer-read-only nil))
4832               (if (or (not (listp methods))
4833                       (and (symbolp (car methods))
4834                            (assq (car methods) nnoo-definition-alist)))
4835                   (setq methods (list methods)))
4836               (when (and (null gnus-override-method)
4837                          methods)
4838                 (setq gnus-override-method (pop methods)))
4839               (while (not result)
4840                 (when (eq gnus-override-method 'current)
4841                   (setq gnus-override-method gnus-current-select-method))
4842                 (erase-buffer)
4843                 (gnus-kill-all-overlays)
4844                 (let ((gnus-newsgroup-name group))
4845                   (gnus-check-group-server))
4846                 (when (gnus-request-article article group (current-buffer))
4847                   (when (numberp article)
4848                     (gnus-async-prefetch-next group article
4849                                               gnus-summary-buffer)
4850                     (when gnus-keep-backlog
4851                       (gnus-backlog-enter-article
4852                        group article (current-buffer))))
4853                   (setq result 'article))
4854                 (if (not result)
4855                     (if methods
4856                         (setq gnus-override-method (pop methods))
4857                       (setq result 'done))))
4858               (and (eq result 'article) 'article)))
4859            ;; It was a pseudo.
4860            (t article)))
4861
4862       ;; Associate this article with the current summary buffer.
4863       (setq gnus-article-current-summary gnus-summary-buffer)
4864
4865       ;; Take the article from the original article buffer
4866       ;; and place it in the buffer it's supposed to be in.
4867       (when (and (get-buffer gnus-article-buffer)
4868                  (equal (buffer-name (current-buffer))
4869                         (buffer-name (get-buffer gnus-article-buffer))))
4870         (save-excursion
4871           (if (get-buffer gnus-original-article-buffer)
4872               (set-buffer gnus-original-article-buffer)
4873             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
4874             (set-buffer-multibyte nil)
4875             (buffer-disable-undo)
4876             (setq major-mode 'gnus-original-article-mode)
4877             (setq buffer-read-only t))
4878           (let (buffer-read-only)
4879             (erase-buffer)
4880             (insert-buffer-substring gnus-article-buffer))
4881           (setq gnus-original-article (cons group article)))
4882
4883         ;; Decode charsets.
4884         (run-hooks 'gnus-article-decode-hook)
4885         ;; Mark article as decoded or not.
4886         (setq gnus-article-decoded-p gnus-article-decode-hook))
4887
4888       ;; Update sparse articles.
4889       (when (and do-update-line
4890                  (or (numberp article)
4891                      (stringp article)))
4892         (let ((buf (current-buffer)))
4893           (set-buffer gnus-summary-buffer)
4894           (gnus-summary-update-article do-update-line sparse-header)
4895           (gnus-summary-goto-subject do-update-line nil t)
4896           (set-window-point (get-buffer-window (current-buffer) t)
4897                             (point))
4898           (set-buffer buf))))))
4899
4900 ;;;
4901 ;;; Article editing
4902 ;;;
4903
4904 (defcustom gnus-article-edit-mode-hook nil
4905   "Hook run in article edit mode buffers."
4906   :group 'gnus-article-various
4907   :type 'hook)
4908
4909 (defcustom gnus-article-edit-article-setup-function
4910   'gnus-article-mime-edit-article-setup
4911   "Function called to setup an editing article buffer."
4912   :group 'gnus-article-various
4913   :type 'function)
4914
4915 (defvar gnus-article-edit-done-function nil)
4916
4917 (defvar gnus-article-edit-mode-map nil)
4918
4919 ;; Should we be using derived.el for this?
4920 (unless gnus-article-edit-mode-map
4921   (setq gnus-article-edit-mode-map (make-sparse-keymap))
4922   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
4923
4924   (gnus-define-keys gnus-article-edit-mode-map
4925     "\C-c\C-c" gnus-article-edit-done
4926     "\C-c\C-k" gnus-article-edit-exit)
4927
4928   (gnus-define-keys (gnus-article-edit-wash-map
4929                      "\C-c\C-w" gnus-article-edit-mode-map)
4930     "f" gnus-article-edit-full-stops))
4931
4932 (defun gnus-article-edit-mode ()
4933   "Major mode for editing articles.
4934 This is an extended text-mode.
4935
4936 \\{gnus-article-edit-mode-map}"
4937   (interactive)
4938   (setq major-mode 'gnus-article-edit-mode)
4939   (setq mode-name "Article Edit")
4940   (use-local-map gnus-article-edit-mode-map)
4941   (make-local-variable 'gnus-article-edit-done-function)
4942   (make-local-variable 'gnus-prev-winconf)
4943   (setq buffer-read-only nil)
4944   (buffer-enable-undo)
4945   (widen)
4946   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
4947
4948 (defun gnus-article-edit (&optional force)
4949   "Edit the current article.
4950 This will have permanent effect only in mail groups.
4951 If FORCE is non-nil, allow editing of articles even in read-only
4952 groups."
4953   (interactive "P")
4954   (when (and (not force)
4955              (gnus-group-read-only-p))
4956     (error "The current newsgroup does not support article editing"))
4957   (gnus-article-date-original)
4958   (gnus-article-edit-article
4959    'ignore
4960    `(lambda (no-highlight)
4961       'ignore
4962       (gnus-summary-edit-article-done
4963        ,(or (mail-header-references gnus-current-headers) "")
4964        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
4965
4966 (defun gnus-article-edit-article (start-func exit-func)
4967   "Start editing the contents of the current article buffer."
4968   (let ((winconf (current-window-configuration)))
4969     (set-buffer gnus-article-buffer)
4970     (gnus-article-edit-mode)
4971     (funcall start-func)
4972     (gnus-configure-windows 'edit-article)
4973     (setq gnus-article-edit-done-function exit-func)
4974     (setq gnus-prev-winconf winconf)
4975     (when gnus-article-edit-article-setup-function
4976       (funcall gnus-article-edit-article-setup-function))
4977     (gnus-message 6 "C-c C-c to end edits")))
4978
4979 (defun gnus-article-edit-done (&optional arg)
4980   "Update the article edits and exit."
4981   (interactive "P")
4982   (let ((func gnus-article-edit-done-function)
4983         (buf (current-buffer))
4984         (start (window-start)))
4985     (remove-hook 'gnus-article-mode-hook
4986                  'gnus-article-mime-edit-article-unwind)
4987     (gnus-article-edit-exit)
4988     (save-excursion
4989       (set-buffer buf)
4990       (let ((buffer-read-only nil))
4991         (funcall func arg))
4992       ;; The cache and backlog have to be flushed somewhat.
4993       (when gnus-keep-backlog
4994         (gnus-backlog-remove-article
4995          (car gnus-article-current) (cdr gnus-article-current)))
4996       ;; Flush original article as well.
4997       (save-excursion
4998         (when (get-buffer gnus-original-article-buffer)
4999           (set-buffer gnus-original-article-buffer)
5000           (setq gnus-original-article nil)))
5001       (when gnus-use-cache
5002         (gnus-cache-update-article
5003          (car gnus-article-current) (cdr gnus-article-current))))
5004     (set-buffer buf)
5005     (set-window-start (get-buffer-window buf) start)
5006     (set-window-point (get-buffer-window buf) (point))))
5007
5008 (defun gnus-article-edit-exit ()
5009   "Exit the article editing without updating."
5010   (interactive)
5011   ;; We remove all text props from the article buffer.
5012   (let ((buf (buffer-substring-no-properties (point-min) (point-max)))
5013         (curbuf (current-buffer))
5014         (p (point))
5015         (window-start (window-start)))
5016     (erase-buffer)
5017     (insert buf)
5018     (let ((winconf gnus-prev-winconf))
5019       (gnus-article-mode)
5020       (set-window-configuration winconf)
5021       ;; Tippy-toe some to make sure that point remains where it was.
5022       (save-current-buffer
5023         (set-buffer curbuf)
5024         (set-window-start (get-buffer-window (current-buffer)) window-start)
5025         (goto-char p)))))
5026
5027 (defun gnus-article-edit-full-stops ()
5028   "Interactively repair spacing at end of sentences."
5029   (interactive)
5030   (save-excursion
5031     (goto-char (point-min))
5032     (search-forward-regexp "^$" nil t)
5033     (let ((case-fold-search nil))
5034       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
5035
5036 ;;;
5037 ;;; Article editing with MIME-Edit
5038 ;;;
5039
5040 (defcustom gnus-article-mime-edit-article-setup-hook nil
5041   "Hook run after setting up a MIME editing article buffer."
5042   :group 'gnus-article-various
5043   :type 'hook)
5044
5045 (defun gnus-article-mime-edit-article-unwind ()
5046   "Unwind `gnus-article-buffer' if article editing was given up."
5047   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
5048   (when mime-edit-mode-flag
5049     (mime-edit-exit 'nomime 'no-error)
5050     (message ""))
5051   (when (featurep 'font-lock)
5052     (setq font-lock-defaults nil)
5053     (font-lock-mode 0)))
5054
5055 (defun gnus-article-mime-edit-article-setup ()
5056   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
5057 after replacing with the original article."
5058   (setq gnus-show-mime t)
5059   (setq gnus-article-edit-done-function
5060         `(lambda (&rest args)
5061            (when mime-edit-mode-flag
5062              (mime-edit-exit)
5063              (message ""))
5064            (goto-char (point-min))
5065            (let (case-fold-search)
5066              (when (re-search-forward
5067                     (format "^%s$" (regexp-quote mail-header-separator))
5068                     nil t)
5069                (replace-match "")))
5070            (when (featurep 'font-lock)
5071              (setq font-lock-defaults nil)
5072              (font-lock-mode 0))
5073            (apply ,gnus-article-edit-done-function args)
5074            (set-buffer gnus-original-article-buffer)
5075            (erase-buffer)
5076            (insert-buffer gnus-article-buffer)
5077            (setq gnus-current-headers (gnus-article-make-full-mail-header))
5078            (gnus-article-prepare-display)))
5079   (substitute-key-definition
5080    'gnus-article-edit-exit 'gnus-article-mime-edit-exit
5081    gnus-article-edit-mode-map)
5082   (erase-buffer)
5083   (insert-buffer gnus-original-article-buffer)
5084   (mime-edit-again)
5085   (when (featurep 'font-lock)
5086     (set (make-local-variable 'font-lock-defaults)
5087          '(message-font-lock-keywords t))
5088     (font-lock-set-defaults)
5089     (turn-on-font-lock))
5090   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
5091   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
5092
5093 (defun gnus-article-mime-edit-exit ()
5094   "Exit the article MIME editing without updating."
5095   (interactive)
5096   (let ((winconf gnus-prev-winconf)
5097         buf)
5098     (when mime-edit-mode-flag
5099       (mime-edit-exit)
5100       (message ""))
5101     (goto-char (point-min))
5102     (let (case-fold-search)
5103       (when (re-search-forward
5104              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
5105         (replace-match "")))
5106     (when (featurep 'font-lock)
5107       (setq font-lock-defaults nil)
5108       (font-lock-mode 0))
5109     ;; We remove all text props from the article buffer.
5110     (setq buf (buffer-substring-no-properties (point-min) (point-max)))
5111     (set-buffer (get-buffer-create gnus-original-article-buffer))
5112     (erase-buffer)
5113     (insert buf)
5114     (setq gnus-current-headers (gnus-article-make-full-mail-header))
5115     (gnus-article-prepare-display)
5116     (set-window-configuration winconf)))
5117
5118 ;;;
5119 ;;; Article highlights
5120 ;;;
5121
5122 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
5123
5124 ;;; Internal Variables:
5125
5126 (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\\)\\)"
5127   "Regular expression that matches URLs."
5128   :group 'gnus-article-buttons
5129   :type 'regexp)
5130
5131 (defcustom gnus-button-alist
5132   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
5133      0 t gnus-button-message-id 2)
5134     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
5135     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
5136      1 t
5137      gnus-button-fetch-group 4)
5138     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
5139     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
5140      t gnus-button-message-id 3)
5141     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
5142     ("mailto:\\([-a-zA-Z.@_+0-9%=?]+\\)" 0 t gnus-url-mailto 1)
5143     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
5144     ;; This is how URLs _should_ be embedded in text...
5145     ("<URL: *\\([^<>]*\\)>" 0 t gnus-button-embedded-url 1)
5146     ;; Raw URLs.
5147     (,gnus-button-url-regexp 0 t browse-url 0))
5148   "*Alist of regexps matching buttons in article bodies.
5149
5150 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
5151 REGEXP: is the string matching text around the button,
5152 BUTTON: is the number of the regexp grouping actually matching the button,
5153 FORM: is a lisp expression which must eval to true for the button to
5154 be added,
5155 CALLBACK: is the function to call when the user push this button, and each
5156 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
5157
5158 CALLBACK can also be a variable, in that case the value of that
5159 variable it the real callback function."
5160   :group 'gnus-article-buttons
5161   :type '(repeat (list regexp
5162                        (integer :tag "Button")
5163                        (sexp :tag "Form")
5164                        (function :tag "Callback")
5165                        (repeat :tag "Par"
5166                                :inline t
5167                                (integer :tag "Regexp group")))))
5168
5169 (defcustom gnus-header-button-alist
5170   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
5171      0 t gnus-button-message-id 0)
5172     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
5173     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
5174      0 t gnus-button-mailto 0)
5175     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
5176     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
5177     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
5178     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
5179      gnus-button-message-id 3))
5180   "*Alist of headers and regexps to match buttons in article heads.
5181
5182 This alist is very similar to `gnus-button-alist', except that each
5183 alist has an additional HEADER element first in each entry:
5184
5185 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
5186
5187 HEADER is a regexp to match a header.  For a fuller explanation, see
5188 `gnus-button-alist'."
5189   :group 'gnus-article-buttons
5190   :group 'gnus-article-headers
5191   :type '(repeat (list (regexp :tag "Header")
5192                        regexp
5193                        (integer :tag "Button")
5194                        (sexp :tag "Form")
5195                        (function :tag "Callback")
5196                        (repeat :tag "Par"
5197                                :inline t
5198                                (integer :tag "Regexp group")))))
5199
5200 (defvar gnus-button-regexp nil)
5201 (defvar gnus-button-marker-list nil)
5202 ;; Regexp matching any of the regexps from `gnus-button-alist'.
5203
5204 (defvar gnus-button-last nil)
5205 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
5206
5207 ;;; Commands:
5208
5209 (defun gnus-article-push-button (event)
5210   "Check text under the mouse pointer for a callback function.
5211 If the text under the mouse pointer has a `gnus-callback' property,
5212 call it with the value of the `gnus-data' text property."
5213   (interactive "e")
5214   (set-buffer (window-buffer (posn-window (event-start event))))
5215   (let* ((pos (posn-point (event-start event)))
5216          (data (get-text-property pos 'gnus-data))
5217          (fun (get-text-property pos 'gnus-callback)))
5218     (goto-char pos)
5219     (when fun
5220       (funcall fun data))))
5221
5222 (defun gnus-article-press-button ()
5223   "Check text at point for a callback function.
5224 If the text at point has a `gnus-callback' property,
5225 call it with the value of the `gnus-data' text property."
5226   (interactive)
5227   (let* ((data (get-text-property (point) 'gnus-data))
5228          (fun (get-text-property (point) 'gnus-callback)))
5229     (when fun
5230       (funcall fun data))))
5231
5232 (defun gnus-article-prev-button (n)
5233   "Move point to N buttons backward.
5234 If N is negative, move forward instead."
5235   (interactive "p")
5236   (gnus-article-next-button (- n)))
5237
5238 (defun gnus-article-next-button (n)
5239   "Move point to N buttons forward.
5240 If N is negative, move backward instead."
5241   (interactive "p")
5242   (let ((function (if (< n 0) 'previous-single-property-change
5243                     'next-single-property-change))
5244         (inhibit-point-motion-hooks t)
5245         (backward (< n 0))
5246         (limit (if (< n 0) (point-min) (point-max))))
5247     (setq n (abs n))
5248     (while (and (not (= limit (point)))
5249                 (> n 0))
5250       ;; Skip past the current button.
5251       (when (get-text-property (point) 'gnus-callback)
5252         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5253       ;; Go to the next (or previous) button.
5254       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
5255       ;; Put point at the start of the button.
5256       (when (and backward (not (get-text-property (point) 'gnus-callback)))
5257         (goto-char (funcall function (point) 'gnus-callback nil limit)))
5258       ;; Skip past intangible buttons.
5259       (when (get-text-property (point) 'intangible)
5260         (incf n))
5261       (decf n))
5262     (unless (zerop n)
5263       (gnus-message 5 "No more buttons"))
5264     n))
5265
5266 (defun gnus-article-highlight (&optional force)
5267   "Highlight current article.
5268 This function calls `gnus-article-highlight-headers',
5269 `gnus-article-highlight-citation',
5270 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5271 do the highlighting.  See the documentation for those functions."
5272   (interactive (list 'force))
5273   (gnus-article-highlight-headers)
5274   (gnus-article-highlight-citation force)
5275   (gnus-article-highlight-signature)
5276   (gnus-article-add-buttons force)
5277   (gnus-article-add-buttons-to-head))
5278
5279 (defun gnus-article-highlight-some (&optional force)
5280   "Highlight current article.
5281 This function calls `gnus-article-highlight-headers',
5282 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
5283 do the highlighting.  See the documentation for those functions."
5284   (interactive (list 'force))
5285   (gnus-article-highlight-headers)
5286   (gnus-article-highlight-signature)
5287   (gnus-article-add-buttons))
5288
5289 (defun gnus-article-highlight-headers ()
5290   "Highlight article headers as specified by `gnus-header-face-alist'."
5291   (interactive)
5292   (save-excursion
5293     (set-buffer gnus-article-buffer)
5294     (save-restriction
5295       (let ((alist gnus-header-face-alist)
5296             (buffer-read-only nil)
5297             (case-fold-search t)
5298             (inhibit-point-motion-hooks t)
5299             entry regexp header-face field-face from hpoints fpoints)
5300         (article-narrow-to-head)
5301         (while (setq entry (pop alist))
5302           (goto-char (point-min))
5303           (setq regexp (concat "^\\("
5304                                (if (string-equal "" (nth 0 entry))
5305                                    "[^\t ]"
5306                                  (nth 0 entry))
5307                                "\\)")
5308                 header-face (nth 1 entry)
5309                 field-face (nth 2 entry))
5310           (while (and (re-search-forward regexp nil t)
5311                       (not (eobp)))
5312             (beginning-of-line)
5313             (setq from (point))
5314             (unless (search-forward ":" nil t)
5315               (forward-char 1))
5316             (when (and header-face
5317                        (not (memq (point) hpoints)))
5318               (push (point) hpoints)
5319               (gnus-put-text-property from (point) 'face header-face))
5320             (when (and field-face
5321                        (not (memq (setq from (point)) fpoints)))
5322               (push from fpoints)
5323               (if (re-search-forward "^[^ \t]" nil t)
5324                   (forward-char -2)
5325                 (goto-char (point-max)))
5326               (gnus-put-text-property from (point) 'face field-face))))))))
5327
5328 (defun gnus-article-highlight-signature ()
5329   "Highlight the signature in an article.
5330 It does this by highlighting everything after
5331 `gnus-signature-separator' using `gnus-signature-face'."
5332   (interactive)
5333   (when gnus-signature-face
5334     (save-excursion
5335       (set-buffer gnus-article-buffer)
5336       (let ((buffer-read-only nil)
5337             (inhibit-point-motion-hooks t))
5338         (save-restriction
5339           (when (gnus-article-narrow-to-signature)
5340             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
5341                               'face gnus-signature-face)))))))
5342
5343 (defun gnus-article-buttonize-signature ()
5344   "Add button to the signature."
5345   (interactive)
5346   (save-excursion
5347     (set-buffer gnus-article-buffer)
5348     (let ((buffer-read-only nil)
5349           (inhibit-point-motion-hooks t))
5350       (when (gnus-article-search-signature)
5351         (gnus-article-add-button (match-beginning 0) (match-end 0)
5352                                  'gnus-signature-toggle
5353                                  (set-marker (make-marker)
5354                                              (1+ (match-end 0))))))))
5355
5356 (defun gnus-button-in-region-p (b e prop)
5357   "Say whether PROP exists in the region."
5358   (text-property-not-all b e prop nil))
5359
5360 (defun gnus-article-add-buttons (&optional force)
5361   "Find external references in the article and make buttons of them.
5362 \"External references\" are things like Message-IDs and URLs, as
5363 specified by `gnus-button-alist'."
5364   (interactive (list 'force))
5365   (save-excursion
5366     (set-buffer gnus-article-buffer)
5367     (let ((buffer-read-only nil)
5368           (inhibit-point-motion-hooks t)
5369           (case-fold-search t)
5370           (alist gnus-button-alist)
5371           beg entry regexp)
5372       ;; Remove all old markers.
5373       (let (marker entry new-list)
5374         (while (setq marker (pop gnus-button-marker-list))
5375           (if (or (< marker (point-min)) (>= marker (point-max)))
5376               (push marker new-list)
5377             (goto-char marker)
5378             (when (setq entry (gnus-button-entry))
5379               (put-text-property (match-beginning (nth 1 entry))
5380                                  (match-end (nth 1 entry))
5381                                  'gnus-callback nil))
5382             (set-marker marker nil)))
5383         (setq gnus-button-marker-list new-list))
5384       ;; We skip the headers.
5385       (article-goto-body)
5386       (setq beg (point))
5387       (while (setq entry (pop alist))
5388         (setq regexp (car entry))
5389         (goto-char beg)
5390         (while (re-search-forward regexp nil t)
5391           (let* ((start (and entry (match-beginning (nth 1 entry))))
5392                  (end (and entry (match-end (nth 1 entry))))
5393                  (from (match-beginning 0)))
5394             (when (and (or (eq t (nth 2 entry))
5395                            (eval (nth 2 entry)))
5396                        (not (gnus-button-in-region-p
5397                              start end 'gnus-callback)))
5398               ;; That optional form returned non-nil, so we add the
5399               ;; button.
5400               (gnus-article-add-button
5401                start end 'gnus-button-push
5402                (car (push (set-marker (make-marker) from)
5403                           gnus-button-marker-list))))))))))
5404
5405 ;; Add buttons to the head of an article.
5406 (defun gnus-article-add-buttons-to-head ()
5407   "Add buttons to the head of the article."
5408   (interactive)
5409   (save-excursion
5410     (set-buffer gnus-article-buffer)
5411     (save-restriction
5412       (let ((buffer-read-only nil)
5413             (inhibit-point-motion-hooks t)
5414             (case-fold-search t)
5415             (alist gnus-header-button-alist)
5416             entry beg end)
5417         (article-narrow-to-head)
5418         (while alist
5419           ;; Each alist entry.
5420           (setq entry (car alist)
5421                 alist (cdr alist))
5422           (goto-char (point-min))
5423           (while (re-search-forward (car entry) nil t)
5424             ;; Each header matching the entry.
5425             (setq beg (match-beginning 0))
5426             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
5427                                (match-beginning 0))
5428                           (point-max)))
5429             (goto-char beg)
5430             (while (re-search-forward (nth 1 entry) end t)
5431               ;; Each match within a header.
5432               (let* ((entry (cdr entry))
5433                      (start (match-beginning (nth 1 entry)))
5434                      (end (match-end (nth 1 entry)))
5435                      (form (nth 2 entry)))
5436                 (goto-char (match-end 0))
5437                 (when (eval form)
5438                   (gnus-article-add-button
5439                    start end (nth 3 entry)
5440                    (buffer-substring (match-beginning (nth 4 entry))
5441                                      (match-end (nth 4 entry)))))))
5442             (goto-char end)))))))
5443
5444 ;;; External functions:
5445
5446 (defun gnus-article-add-button (from to fun &optional data)
5447   "Create a button between FROM and TO with callback FUN and data DATA."
5448   (when gnus-article-button-face
5449     (gnus-overlay-put (gnus-make-overlay from to)
5450                       'face gnus-article-button-face))
5451   (gnus-add-text-properties
5452    from to
5453    (nconc (and gnus-article-mouse-face
5454                (list gnus-mouse-face-prop gnus-article-mouse-face))
5455           (list 'gnus-callback fun)
5456           (and data (list 'gnus-data data))))
5457   (widget-convert-button 'link from to :action 'gnus-widget-press-button
5458                          ;; Quote `:button-keymap' for Mule 2.3
5459                          ;; but it won't work.
5460                          ':button-keymap gnus-widget-button-keymap))
5461
5462 ;;; Internal functions:
5463
5464 (defun gnus-article-set-globals ()
5465   (save-excursion
5466     (set-buffer gnus-summary-buffer)
5467     (gnus-set-global-variables)))
5468
5469 (defun gnus-signature-toggle (end)
5470   (save-excursion
5471     (set-buffer gnus-article-buffer)
5472     (let ((buffer-read-only nil)
5473           (inhibit-point-motion-hooks t)
5474           (limit (next-single-property-change end 'mime-view-entity
5475                                               nil (point-max))))
5476       (if (text-property-any end limit 'article-type 'signature)
5477           (gnus-remove-text-properties-when
5478            'article-type 'signature end limit
5479            (cons 'article-type (cons 'signature
5480                                      gnus-hidden-properties)))
5481         (gnus-add-text-properties-when
5482          'article-type nil end limit
5483          (cons 'article-type (cons 'signature
5484                                    gnus-hidden-properties)))))))
5485
5486 (defun gnus-button-entry ()
5487   ;; Return the first entry in `gnus-button-alist' matching this place.
5488   (let ((alist gnus-button-alist)
5489         (entry nil))
5490     (while alist
5491       (setq entry (pop alist))
5492       (if (looking-at (car entry))
5493           (setq alist nil)
5494         (setq entry nil)))
5495     entry))
5496
5497 (defun gnus-button-push (marker)
5498   ;; Push button starting at MARKER.
5499   (save-excursion
5500     (goto-char marker)
5501     (let* ((entry (gnus-button-entry))
5502            (inhibit-point-motion-hooks t)
5503            (fun (nth 3 entry))
5504            (args (mapcar (lambda (group)
5505                            (let ((string (match-string group)))
5506                              (gnus-set-text-properties
5507                               0 (length string) nil string)
5508                              string))
5509                          (nthcdr 4 entry))))
5510       (cond
5511        ((fboundp fun)
5512         (apply fun args))
5513        ((and (boundp fun)
5514              (fboundp (symbol-value fun)))
5515         (apply (symbol-value fun) args))
5516        (t
5517         (gnus-message 1 "You must define `%S' to use this button"
5518                       (cons fun args)))))))
5519
5520 (defun gnus-button-message-id (message-id)
5521   "Fetch MESSAGE-ID."
5522   (save-excursion
5523     (set-buffer gnus-summary-buffer)
5524     (gnus-summary-refer-article message-id)))
5525
5526 (defun gnus-button-fetch-group (address)
5527   "Fetch GROUP specified by ADDRESS."
5528   (if (not (string-match "[:/]" address))
5529       ;; This is just a simple group url.
5530       (gnus-group-read-ephemeral-group address gnus-select-method)
5531     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
5532                            address))
5533         (error "Can't parse %s" address)
5534       (gnus-group-read-ephemeral-group
5535        (match-string 4 address)
5536        `(nntp ,(match-string 1 address)
5537               (nntp-address ,(match-string 1 address))
5538               (nntp-port-number ,(if (match-end 3)
5539                                      (match-string 3 address)
5540                                    "nntp")))))))
5541
5542 (defun gnus-url-parse-query-string (query &optional downcase)
5543   (let (retval pairs cur key val)
5544     (setq pairs (split-string query "&"))
5545     (while pairs
5546       (setq cur (car pairs)
5547             pairs (cdr pairs))
5548       (if (not (string-match "=" cur))
5549           nil                           ; Grace
5550         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
5551               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
5552         (if downcase
5553             (setq key (downcase key)))
5554         (setq cur (assoc key retval))
5555         (if cur
5556             (setcdr cur (cons val (cdr cur)))
5557           (setq retval (cons (list key val) retval)))))
5558     retval))
5559
5560 (defun gnus-url-unhex (x)
5561   (if (> x ?9)
5562       (if (>= x ?a)
5563           (+ 10 (- x ?a))
5564         (+ 10 (- x ?A)))
5565     (- x ?0)))
5566
5567 (defun gnus-url-unhex-string (str &optional allow-newlines)
5568   "Remove %XXX embedded spaces, etc in a url.
5569 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
5570 decoding of carriage returns and line feeds in the string, which is normally
5571 forbidden in URL encoding."
5572   (setq str (or str ""))
5573   (let ((tmp "")
5574         (case-fold-search t))
5575     (while (string-match "%[0-9a-f][0-9a-f]" str)
5576       (let* ((start (match-beginning 0))
5577              (ch1 (gnus-url-unhex (elt str (+ start 1))))
5578              (code (+ (* 16 ch1)
5579                       (gnus-url-unhex (elt str (+ start 2))))))
5580         (setq tmp (concat
5581                    tmp (substring str 0 start)
5582                    (cond
5583                     (allow-newlines
5584                      (char-to-string code))
5585                     ((or (= code ?\n) (= code ?\r))
5586                      " ")
5587                     (t (char-to-string code))))
5588               str (substring str (match-end 0)))))
5589     (setq tmp (concat tmp str))
5590     tmp))
5591
5592 (defun gnus-url-mailto (url)
5593   ;; Send mail to someone
5594   (when (string-match "mailto:/*\\(.*\\)" url)
5595     (setq url (substring url (match-beginning 1) nil)))
5596   (let (to args subject func)
5597     (if (string-match (regexp-quote "?") url)
5598         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
5599               args (gnus-url-parse-query-string
5600                     (substring url (match-end 0) nil) t))
5601       (setq to (gnus-url-unhex-string url)))
5602     (setq args (cons (list "to" to) args)
5603           subject (cdr-safe (assoc "subject" args)))
5604     (gnus-msg-mail)
5605     (while args
5606       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
5607       (if (fboundp func)
5608           (funcall func)
5609         (message-position-on-field (caar args)))
5610       (insert (mapconcat 'identity (cdar args) ", "))
5611       (setq args (cdr args)))
5612     (if subject
5613         (message-goto-body)
5614       (message-goto-subject))))
5615
5616 (defun gnus-button-embedded-url (address)
5617   "Activate ADDRESS with `browse-url'."
5618   (browse-url (gnus-strip-whitespace address)))
5619
5620 (eval-when-compile
5621   ;; Silence the byte-compiler.
5622   (autoload 'smiley-toggle-buffer "gnus-bitmap"))
5623 (defun gnus-article-smiley-display ()
5624   "Display \"smileys\" as small graphical icons."
5625   (smiley-toggle-buffer 1 (current-buffer) (point-min) (point-max)))
5626
5627 ;;; Next/prev buttons in the article buffer.
5628
5629 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
5630 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
5631
5632 (defvar gnus-prev-page-map nil)
5633 (unless gnus-prev-page-map
5634   (setq gnus-prev-page-map (make-sparse-keymap))
5635   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
5636   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
5637
5638 (static-if (featurep 'xemacs)
5639     (defun gnus-insert-prev-page-button ()
5640       (let ((buffer-read-only nil))
5641         (gnus-eval-format
5642          gnus-prev-page-line-format nil
5643          `(gnus-prev t local-map ,gnus-prev-page-map
5644                      gnus-callback gnus-article-button-prev-page
5645                      article-type annotation))))
5646   (defun gnus-insert-prev-page-button ()
5647     (let ((buffer-read-only nil)
5648           (situation (get-text-property (point-min) 'mime-view-situation)))
5649       (set-keymap-parent gnus-prev-page-map (current-local-map))
5650       (gnus-eval-format
5651        gnus-prev-page-line-format nil
5652        `(gnus-prev t local-map ,gnus-prev-page-map
5653                    gnus-callback gnus-article-button-prev-page
5654                    article-type annotation
5655                    mime-view-situation ,situation))))
5656   )
5657
5658 (defvar gnus-next-page-map nil)
5659 (unless gnus-next-page-map
5660   (setq gnus-next-page-map (make-sparse-keymap))
5661   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
5662   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
5663
5664 (defun gnus-button-next-page ()
5665   "Go to the next page."
5666   (interactive)
5667   (let ((win (selected-window)))
5668     (select-window (get-buffer-window gnus-article-buffer t))
5669     (gnus-article-next-page)
5670     (select-window win)))
5671
5672 (defun gnus-button-prev-page ()
5673   "Go to the prev page."
5674   (interactive)
5675   (let ((win (selected-window)))
5676     (select-window (get-buffer-window gnus-article-buffer t))
5677     (gnus-article-prev-page)
5678     (select-window win)))
5679
5680 (static-if (featurep 'xemacs)
5681     (defun gnus-insert-next-page-button ()
5682       (let ((buffer-read-only nil))
5683         (gnus-eval-format gnus-next-page-line-format nil
5684                           `(gnus-next
5685                             t local-map ,gnus-next-page-map
5686                             gnus-callback gnus-article-button-next-page
5687                             article-type annotation))))
5688   (defun gnus-insert-next-page-button ()
5689     (let ((buffer-read-only nil)
5690           (situation (get-text-property (point-min) 'mime-view-situation)))
5691       (set-keymap-parent gnus-next-page-map (current-local-map))
5692       (gnus-eval-format gnus-next-page-line-format nil
5693                         `(gnus-next
5694                           t local-map ,gnus-next-page-map
5695                           gnus-callback gnus-article-button-next-page
5696                           article-type annotation
5697                           mime-view-situation ,situation))))
5698   )
5699
5700 (defun gnus-article-button-next-page (arg)
5701   "Go to the next page."
5702   (interactive "P")
5703   (let ((win (selected-window)))
5704     (select-window (get-buffer-window gnus-article-buffer t))
5705     (gnus-article-next-page)
5706     (select-window win)))
5707
5708 (defun gnus-article-button-prev-page (arg)
5709   "Go to the prev page."
5710   (interactive "P")
5711   (let ((win (selected-window)))
5712     (select-window (get-buffer-window gnus-article-buffer t))
5713     (gnus-article-prev-page)
5714     (select-window win)))
5715
5716 (defvar gnus-decode-header-methods
5717   '(mail-decode-encoded-word-region)
5718   "List of methods used to decode headers.
5719
5720 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
5721 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
5722 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
5723 whose names match REGEXP.
5724
5725 For example:
5726 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
5727  mail-decode-encoded-word-region
5728  (\"chinese\" . rfc1843-decode-region))
5729 ")
5730
5731 (defvar gnus-decode-header-methods-cache nil)
5732
5733 (defun gnus-multi-decode-header (start end)
5734   "Apply the functions from `gnus-encoded-word-methods' that match."
5735   (unless (and gnus-decode-header-methods-cache
5736                (eq gnus-newsgroup-name
5737                    (car gnus-decode-header-methods-cache)))
5738     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
5739     (mapcar (lambda (x)
5740               (if (symbolp x)
5741                   (nconc gnus-decode-header-methods-cache (list x))
5742                 (if (and gnus-newsgroup-name
5743                          (string-match (car x) gnus-newsgroup-name))
5744                     (nconc gnus-decode-header-methods-cache
5745                            (list (cdr x))))))
5746           gnus-decode-header-methods))
5747   (let ((xlist gnus-decode-header-methods-cache))
5748     (pop xlist)
5749     (save-restriction
5750       (narrow-to-region start end)
5751       (while xlist
5752         (funcall (pop xlist) (point-min) (point-max))))))
5753
5754 ;;;
5755 ;;; Treatment top-level handling.
5756 ;;;
5757
5758 (defun gnus-treat-article (condition &optional part-number total-parts type)
5759   (let ((length (- (point-max) (point-min)))
5760         (alist gnus-treatment-function-alist)
5761         (article-goto-body-goes-to-point-min-p t)
5762         (treated-type
5763          (or (not type)
5764              (catch 'found
5765                (let ((list gnus-article-treat-types))
5766                  (while list
5767                    (when (string-match (pop list) type)
5768                      (throw 'found t)))))))
5769         (highlightp (gnus-visual-p 'article-highlight 'highlight))
5770         (entity (static-unless (featurep 'xemacs)
5771                   (when (eq 'head condition)
5772                     (get-text-property (point-min) 'mime-view-entity))))
5773         val elem buttonized)
5774     (gnus-run-hooks 'gnus-part-display-hook)
5775     (unless gnus-inhibit-treatment
5776       (while (setq elem (pop alist))
5777         (setq val
5778               (save-excursion
5779                 (if (gnus-buffer-live-p gnus-summary-buffer)
5780                     (set-buffer gnus-summary-buffer))
5781                 (symbol-value (car elem))))
5782         (when (and (or (consp val)
5783                        treated-type)
5784                    (gnus-treat-predicate val)
5785                    (or (not (get (car elem) 'highlight))
5786                        highlightp))
5787           (when (and (not buttonized)
5788                      (memq (car elem)
5789                            '(gnus-treat-hide-signature
5790                              gnus-treat-highlight-signature)))
5791             (gnus-article-buttonize-signature)
5792             (setq buttonized t))
5793           (save-restriction
5794             (funcall (cadr elem)))))
5795       ;; FSF Emacsen does not inherit the existing text properties
5796       ;; in the new text, so we should do it for `mime-view-entity'.
5797       (static-unless (featurep 'xemacs)
5798         (when entity
5799           (put-text-property (point-min) (point-max)
5800                              'mime-view-entity entity))))))
5801
5802 ;; Dynamic variables.
5803 (eval-when-compile
5804   (defvar part-number)
5805   (defvar total-parts)
5806   (defvar type)
5807   (defvar condition)
5808   (defvar length))
5809
5810 (defun gnus-treat-predicate (val)
5811   (cond
5812    ((null val)
5813     nil)
5814    ((and (listp val)
5815          (stringp (car val)))
5816     (apply 'gnus-or (mapcar `(lambda (s)
5817                                (string-match s ,(or gnus-newsgroup-name "")))
5818                             val)))
5819    ((listp val)
5820     (let ((pred (pop val)))
5821       (cond
5822        ((eq pred 'or)
5823         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
5824        ((eq pred 'and)
5825         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
5826        ((eq pred 'not)
5827         (not (gnus-treat-predicate (car val))))
5828        ((eq pred 'typep)
5829         (equal (car val) type))
5830        (t
5831         (error "%S is not a valid predicate" pred)))))
5832    ((eq val 'mime)
5833     gnus-show-mime)
5834    (condition
5835     (eq condition val))
5836    ((eq val t)
5837     t)
5838    ((eq val 'head)
5839     nil)
5840    ((eq val 'last)
5841     (eq part-number total-parts))
5842    ((numberp val)
5843     (< length val))
5844    (t
5845     (error "%S is not a valid value" val))))
5846
5847 (defun gnus-article-encrypt-body (protocol &optional n)
5848   "Encrypt the article body."
5849   (interactive
5850    (list
5851     (or gnus-article-encrypt-protocol
5852         (completing-read "Encrypt protocol: "
5853                          gnus-article-encrypt-protocol-alist
5854                          nil t))
5855     current-prefix-arg))
5856   (let ((func (cdr (assoc protocol gnus-article-encrypt-protocol-alist))))
5857     (unless func
5858       (error (format "Can't find the encrypt protocol %s" protocol)))
5859     (if (equal gnus-newsgroup-name "nndraft:drafts")
5860         (error "Can't encrypt the article in group nndraft:drafts."))
5861     (if (equal gnus-newsgroup-name "nndraft:queue")
5862         (error "Don't encrypt the article in group nndraft:queue."))
5863     (gnus-summary-iterate n
5864       (save-excursion
5865         (set-buffer gnus-summary-buffer)
5866         (let ((mail-parse-charset gnus-newsgroup-charset)
5867               (mail-parse-ignored-charsets gnus-newsgroup-ignored-charsets)
5868               (summary-buffer gnus-summary-buffer)
5869               references point)
5870           (gnus-set-global-variables)
5871           (when (gnus-group-read-only-p)
5872             (error "The current newsgroup does not support article encrypt"))
5873           (gnus-summary-show-article t)
5874           (setq references
5875               (or (mail-header-references gnus-current-headers) ""))
5876           (set-buffer gnus-article-buffer)
5877           (let* ((buffer-read-only nil)
5878                  (headers
5879                   (mapcar (lambda (field)
5880                             (and (save-restriction
5881                                    (message-narrow-to-head)
5882                                    (goto-char (point-min))
5883                                    (search-forward field nil t))
5884                                  (prog2
5885                                      (message-narrow-to-field)
5886                                      (buffer-substring (point-min) (point-max))
5887                                    (delete-region (point-min) (point-max))
5888                                    (widen))))
5889                           '("Content-Type:" "Content-Transfer-Encoding:"
5890                             "Content-Disposition:"))))
5891             (message-narrow-to-head)
5892             (message-remove-header "MIME-Version")
5893             (goto-char (point-max))
5894             (setq point (point))
5895             (insert (apply 'concat headers))
5896             (widen)
5897             (narrow-to-region point (point-max))
5898             (let ((message-options message-options))
5899               (message-options-set 'message-sender user-mail-address)
5900               (message-options-set 'message-recipients user-mail-address)
5901               (message-options-set 'message-sign-encrypt 'not)
5902               (funcall func))
5903             (goto-char (point-min))
5904             (insert "MIME-Version: 1.0\n")
5905             (widen)
5906             (gnus-summary-edit-article-done
5907              references nil summary-buffer t))
5908           (when gnus-keep-backlog
5909             (gnus-backlog-remove-article
5910              (car gnus-article-current) (cdr gnus-article-current)))
5911           (save-excursion
5912             (when (get-buffer gnus-original-article-buffer)
5913               (set-buffer gnus-original-article-buffer)
5914               (setq gnus-original-article nil)))
5915           (when gnus-use-cache
5916             (gnus-cache-update-article
5917              (car gnus-article-current) (cdr gnus-article-current))))))))
5918
5919 (defvar gnus-mime-security-button-line-format "%{%([[%t:%i]%D]%)%}\n"
5920   "The following specs can be used:
5921 %t  The security MIME type
5922 %i  Additional info
5923 %d  Details
5924 %D  Details if button is pressed")
5925
5926 (defvar gnus-mime-security-button-end-line-format "%{%([[End of %t]%D]%)%}\n"
5927   "The following specs can be used:
5928 %t  The security MIME type
5929 %i  Additional info
5930 %d  Details
5931 %D  Details if button is pressed")
5932
5933 (defvar gnus-mime-security-button-line-format-alist
5934   '((?t gnus-tmp-type ?s)
5935     (?i gnus-tmp-info ?s)
5936     (?d gnus-tmp-details ?s)
5937     (?D gnus-tmp-pressed-details ?s)))
5938
5939 (defvar gnus-mime-security-button-map
5940   (let ((map (make-sparse-keymap)))
5941     (unless (>= (string-to-number emacs-version) 21)
5942       (set-keymap-parent map gnus-article-mode-map))
5943     (define-key map gnus-mouse-2 'gnus-article-push-button)
5944     (define-key map "\r" 'gnus-article-press-button)
5945     map))
5946
5947 (defvar gnus-mime-security-details-buffer nil)
5948
5949 (defvar gnus-mime-security-button-pressed nil)
5950
5951 (defvar gnus-mime-security-show-details-inline t
5952   "If non-nil, show details in the article buffer.")
5953
5954 (defun gnus-mime-security-verify-or-decrypt (handle)
5955   (mm-remove-parts (cdr handle))
5956   (let ((region (mm-handle-multipart-ctl-parameter handle 'gnus-region))
5957         buffer-read-only)
5958     (when region
5959       (delete-region (car region) (cdr region))
5960       (set-marker (car region) nil)
5961       (set-marker (cdr region) nil)))
5962   (with-current-buffer (mm-handle-multipart-original-buffer handle)
5963     (let* ((mm-verify-option 'known)
5964            (mm-decrypt-option 'known)
5965            (nparts (mm-possibly-verify-or-decrypt (cdr handle) handle)))
5966       (unless (eq nparts (cdr handle))
5967         (mm-destroy-parts (cdr handle))
5968         (setcdr handle nparts))))
5969   (let ((point (point))
5970         buffer-read-only)
5971     (gnus-mime-display-security handle)
5972     (goto-char point)))
5973
5974 (defun gnus-mime-security-show-details (handle)
5975   (let ((details (mm-handle-multipart-ctl-parameter handle 'gnus-details)))
5976     (if details
5977         (if gnus-mime-security-show-details-inline
5978             (let ((gnus-mime-security-button-pressed t)
5979                   (gnus-mime-security-button-line-format
5980                    (get-text-property (point) 'gnus-line-format))
5981                 buffer-read-only)
5982               (forward-char -1)
5983               (while (eq (get-text-property (point) 'gnus-line-format)
5984                          gnus-mime-security-button-line-format)
5985                 (forward-char -1))
5986               (forward-char)
5987               (delete-region (point)
5988                              (or (text-property-not-all
5989                                   (point) (point-max)
5990                                 'gnus-line-format
5991                                 gnus-mime-security-button-line-format)
5992                                  (point-max)))
5993               (gnus-insert-mime-security-button handle))
5994           (if (gnus-buffer-live-p gnus-mime-security-details-buffer)
5995               (with-current-buffer gnus-mime-security-details-buffer
5996                 (erase-buffer)
5997                 t)
5998             (setq gnus-mime-security-details-buffer
5999                   (gnus-get-buffer-create "*MIME Security Details*")))
6000           (with-current-buffer gnus-mime-security-details-buffer
6001             (insert details)
6002             (goto-char (point-min)))
6003           (pop-to-buffer gnus-mime-security-details-buffer))
6004       (gnus-message 5 "No details."))))
6005
6006 (defun gnus-mime-security-press-button (handle)
6007   (if (mm-handle-multipart-ctl-parameter handle 'gnus-info)
6008       (gnus-mime-security-show-details handle)
6009     (gnus-mime-security-verify-or-decrypt handle)))
6010
6011 (defun gnus-insert-mime-security-button (handle &optional displayed)
6012   (let* ((protocol (mm-handle-multipart-ctl-parameter handle 'protocol))
6013          (gnus-tmp-type
6014           (concat
6015            (or (nth 2 (assoc protocol mm-verify-function-alist))
6016                (nth 2 (assoc protocol mm-decrypt-function-alist))
6017                "Unknown")
6018            (if (equal (car handle) "multipart/signed")
6019                " Signed" " Encrypted")
6020            " Part"))
6021          (gnus-tmp-info
6022           (or (mm-handle-multipart-ctl-parameter handle 'gnus-info)
6023               "Undecided"))
6024          (gnus-tmp-details
6025           (mm-handle-multipart-ctl-parameter handle 'gnus-details))
6026          gnus-tmp-pressed-details
6027          b e)
6028     (setq gnus-tmp-details
6029           (if gnus-tmp-details
6030               (concat "\n" gnus-tmp-details) ""))
6031     (setq gnus-tmp-pressed-details
6032           (if gnus-mime-security-button-pressed gnus-tmp-details ""))
6033     (unless (bolp)
6034       (insert "\n"))
6035     (setq b (point))
6036     (gnus-eval-format
6037      gnus-mime-security-button-line-format
6038      gnus-mime-security-button-line-format-alist
6039      `(keymap ,gnus-mime-security-button-map
6040               ,@(if (>= (string-to-number emacs-version) 21)
6041                     nil ;; XEmacs doesn't care
6042                   (list 'local-map gnus-mime-security-button-map))
6043               gnus-callback gnus-mime-security-press-button
6044               gnus-line-format ,gnus-mime-security-button-line-format
6045               article-type annotation
6046               gnus-data ,handle))
6047     (setq e (point))
6048     (widget-convert-button
6049      'link b e
6050      :mime-handle handle
6051      :action 'gnus-widget-press-button
6052      :button-keymap gnus-mime-security-button-map
6053      :help-echo
6054      (lambda (widget/window &optional overlay pos)
6055        ;; Needed to properly clear the message due to a bug in
6056        ;; wid-edit (XEmacs only).
6057        (if (boundp 'help-echo-owns-message)
6058            (setq help-echo-owns-message t))
6059        (format
6060         "%S: show detail"
6061         (aref gnus-mouse-2 0))))))
6062
6063 (defun gnus-mime-display-security (handle)
6064   (save-restriction
6065     (narrow-to-region (point) (point))
6066     (gnus-insert-mime-security-button handle)
6067     (gnus-mime-display-mixed (cdr handle))
6068     (unless (bolp)
6069       (insert "\n"))
6070     (let ((gnus-mime-security-button-line-format
6071            gnus-mime-security-button-end-line-format))
6072       (gnus-insert-mime-security-button handle))
6073     (mm-set-handle-multipart-parameter
6074      handle 'gnus-region
6075      (cons (set-marker (make-marker) (point-min))
6076            (set-marker (make-marker) (point-max))))))
6077
6078
6079 ;;; @ for mime-view
6080 ;;;
6081
6082 (defun gnus-article-header-presentation-method (entity situation)
6083   (mime-insert-header entity)
6084   )
6085
6086 (set-alist 'mime-header-presentation-method-alist
6087            'gnus-original-article-mode
6088            #'gnus-article-header-presentation-method)
6089
6090 (defun gnus-mime-preview-quitting-method ()
6091   (mime-preview-kill-buffer)
6092   (delete-other-windows)
6093   (gnus-article-show-summary)
6094   (gnus-summary-select-article gnus-show-all-headers t))
6095
6096 (set-alist 'mime-preview-quitting-method-alist
6097            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
6098
6099 (set-alist 'mime-preview-following-method-alist
6100            'gnus-original-article-mode #'gnus-following-method)
6101
6102 (set-alist 'mime-preview-over-to-previous-method-alist
6103            'gnus-original-article-mode
6104            (lambda ()
6105              (if (> (point-min) 1)
6106                  (gnus-article-prev-page)
6107                (gnus-article-read-summary-keys
6108                 nil (gnus-character-to-event ?P)))))
6109
6110 (set-alist 'mime-preview-over-to-next-method-alist
6111            'gnus-original-article-mode'
6112            (lambda ()
6113              (if (< (point-max) (buffer-size))
6114                  (gnus-article-next-page)
6115                (gnus-article-read-summary-keys
6116                 nil (gnus-character-to-event ?N)))))
6117
6118
6119 ;;; @ end
6120 ;;;
6121
6122 (gnus-ems-redefine)
6123
6124 (provide 'gnus-art)
6125
6126 (run-hooks 'gnus-art-load-hook)
6127
6128 ;;; gnus-art.el ends here