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