2000-11-06 13:01:27 ShengHuo ZHU <zsh@cs.rochester.edu>
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-sum)
32 (require 'gnus-spec)
33 (require 'gnus-int)
34 (require 'mm-bodies)
35 (require 'mail-parse)
36 (require 'mm-decode)
37 (require 'mm-view)
38 (require 'wid-edit)
39 (require 'mm-uu)
40
41 (defgroup gnus-article nil
42   "Article display."
43   :link '(custom-manual "(gnus)The Article Buffer")
44   :group 'gnus)
45
46 (defgroup gnus-article-treat nil
47   "Treating article parts."
48   :link '(custom-manual "(gnus)Article Hiding")
49   :group 'gnus-article)
50
51 (defgroup gnus-article-hiding nil
52   "Hiding article parts."
53   :link '(custom-manual "(gnus)Article Hiding")
54   :group 'gnus-article)
55
56 (defgroup gnus-article-highlight nil
57   "Article highlighting."
58   :link '(custom-manual "(gnus)Article Highlighting")
59   :group 'gnus-article
60   :group 'gnus-visual)
61
62 (defgroup gnus-article-signature nil
63   "Article signatures."
64   :link '(custom-manual "(gnus)Article Signature")
65   :group 'gnus-article)
66
67 (defgroup gnus-article-headers nil
68   "Article headers."
69   :link '(custom-manual "(gnus)Hiding Headers")
70   :group 'gnus-article)
71
72 (defgroup gnus-article-washing nil
73   "Special commands on articles."
74   :link '(custom-manual "(gnus)Article Washing")
75   :group 'gnus-article)
76
77 (defgroup gnus-article-emphasis nil
78   "Fontisizing articles."
79   :link '(custom-manual "(gnus)Article Fontisizing")
80   :group 'gnus-article)
81
82 (defgroup gnus-article-saving nil
83   "Saving articles."
84   :link '(custom-manual "(gnus)Saving Articles")
85   :group 'gnus-article)
86
87 (defgroup gnus-article-mime nil
88   "Worshiping the MIME wonder."
89   :link '(custom-manual "(gnus)Using MIME")
90   :group 'gnus-article)
91
92 (defgroup gnus-article-buttons nil
93   "Pushable buttons in the article buffer."
94   :link '(custom-manual "(gnus)Article Buttons")
95   :group 'gnus-article)
96
97 (defgroup gnus-article-various nil
98   "Other article options."
99   :link '(custom-manual "(gnus)Misc Article")
100   :group 'gnus-article)
101
102 (defcustom gnus-ignored-headers
103   '("^Path:" "^Expires:" "^Date-Received:" "^References:" "^Xref:" "^Lines:"
104     "^Relay-Version:" "^Message-ID:" "^Approved:" "^Sender:" "^Received:"
105     "^X-UIDL:" "^MIME-Version:" "^Return-Path:" "^In-Reply-To:"
106     "^Content-Type:" "^Content-Transfer-Encoding:" "^X-WebTV-Signature:"
107     "^X-MimeOLE:" "^X-MSMail-Priority:" "^X-Priority:" "^X-Loop:"
108     "^X-Authentication-Warning:" "^X-MIME-Autoconverted:" "^X-Face:"
109     "^X-Attribution:" "^X-Originating-IP:" "^Delivered-To:"
110     "^NNTP-[-A-Za-z]+:" "^Distribution:" "^X-no-archive:" "^X-Trace:"
111     "^X-Complaints-To:" "^X-NNTP-Posting-Host:" "^X-Orig.*:"
112     "^Abuse-Reports-To:" "^Cache-Post-Path:" "^X-Article-Creation-Date:"
113     "^X-Poster:" "^X-Mail2News-Path:" "^X-Server-Date:" "^X-Cache:"
114     "^Originator:" "^X-Problems-To:" "^X-Auth-User:" "^X-Post-Time:"
115     "^X-Admin:" "^X-UID:" "^Resent-[-A-Za-z]+:" "^X-Mailing-List:"
116     "^Precedence:" "^Original-[-A-Za-z]+:" "^X-filename:" "^X-Orcpt:"
117     "^Old-Received:" "^X-Pgp" "^X-Auth:" "^X-From-Line:"
118     "^X-Gnus-Article-Number:" "^X-Majordomo:" "^X-Url:" "^X-Sender:"
119     "^MBOX-Line" "^Priority:" "^X-Pgp" "^X400-[-A-Za-z]+:"
120     "^Status:" "^X-Gnus-Mail-Source:" "^Cancel-Lock:"
121     "^X-FTN" "^X-EXP32-SerialNo:" "^Encoding:" "^Importance:"
122     "^Autoforwarded:" "^Original-Encoded-Information-Types:" "^X-Ya-Pop3:"
123     "^X-Face-Version:" "^X-Vms-To:" "^X-ML-NAME:" "^X-ML-COUNT:"
124     "^Mailing-List:" "^X-finfo:" "^X-md5sum:" "^X-md5sum-Origin:"
125     "^X-Sun-Charset:" "^X-Accept-Language:" "^X-Envelope-Sender:"
126     "^List-[A-Za-z]+:" "^X-Listprocessor-Version:"
127     "^X-Received:" "^X-Distribute:" "^X-Sequence:" "^X-Juno-Line-Breaks:"
128     "^X-Notes-Item:" "^X-MS-TNEF-Correlator:" "^x-uunet-gateway:"
129     "^X-Received:" "^Content-length:" "X-precedence:")
130   "*All headers that start with this regexp will be hidden.
131 This variable can also be a list of regexps of headers to be ignored.
132 If `gnus-visible-headers' is non-nil, this variable will be ignored."
133   :type '(choice :custom-show nil
134                  regexp
135                  (repeat regexp))
136   :group 'gnus-article-hiding)
137
138 (defcustom gnus-visible-headers
139   "^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:"
140   "*All headers that do not match this regexp will be hidden.
141 This variable can also be a list of regexp of headers to remain visible.
142 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
143   :type '(repeat :value-to-internal (lambda (widget value)
144                                       (custom-split-regexp-maybe value))
145                  :match (lambda (widget value)
146                           (or (stringp value)
147                               (widget-editable-list-match widget value)))
148                  regexp)
149   :group 'gnus-article-hiding)
150
151 (defcustom gnus-sorted-header-list
152   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
153     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
154   "*This variable is a list of regular expressions.
155 If it is non-nil, headers that match the regular expressions will
156 be placed first in the article buffer in the sequence specified by
157 this list."
158   :type '(repeat regexp)
159   :group 'gnus-article-hiding)
160
161 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
162   "Headers that are only to be displayed if they have interesting data.
163 Possible values in this list are `empty', `newsgroups', `followup-to',
164 `reply-to', `date', `long-to', and `many-to'."
165   :type '(set (const :tag "Headers with no content." empty)
166               (const :tag "Newsgroups with only one group." newsgroups)
167               (const :tag "Followup-to identical to newsgroups." followup-to)
168               (const :tag "Reply-to identical to from." reply-to)
169               (const :tag "Date less than four days old." date)
170               (const :tag "Very long To and/or Cc header." long-to)
171               (const :tag "Multiple To and/or Cc headers." many-to))
172   :group 'gnus-article-hiding)
173
174 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
175   "Regexp matching signature separator.
176 This can also be a list of regexps.  In that case, it will be checked
177 from head to tail looking for a separator.  Searches will be done from
178 the end of the buffer."
179   :type '(repeat string)
180   :group 'gnus-article-signature)
181
182 (defcustom gnus-signature-limit nil
183   "Provide a limit to what is considered a signature.
184 If it is a number, no signature may not be longer (in characters) than
185 that number.  If it is a floating point number, no signature may be
186 longer (in lines) than that number.  If it is a function, the function
187 will be called without any parameters, and if it returns nil, there is
188 no signature in the buffer.  If it is a string, it will be used as a
189 regexp.  If it matches, the text in question is not a signature."
190   :type '(choice (integer :value 200)
191                  (number :value 4.0)
192                  (function :value fun)
193                  (regexp :value ".*"))
194   :group 'gnus-article-signature)
195
196 (defcustom gnus-hidden-properties '(invisible t intangible t)
197   "Property list to use for hiding text."
198   :type 'sexp
199   :group 'gnus-article-hiding)
200
201 ;; Fixme: This isn't the right thing for mixed graphical and and
202 ;; non-graphical frames in a session.
203 ;; gnus-xmas.el overrides this for XEmacs.
204 (defcustom gnus-article-x-face-command
205   (if (and (fboundp 'image-type-available-p)
206            (image-type-available-p 'xbm))
207       'gnus-article-display-xface
208     (if gnus-article-compface-xbm
209         "{ echo '/* Width=48, Height=48 */'; uncompface; } | display -"
210       "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | \
211 display -"))
212   "*String or function to be executed to display an X-Face header.
213 If it is a string, the command will be executed in a sub-shell
214 asynchronously.  The compressed face will be piped to this command."
215   :type '(choice string
216                  (function-item gnus-article-display-xface)
217                  function)
218   :group 'gnus-article-washing)
219
220 (defcustom gnus-article-x-face-too-ugly nil
221   "Regexp matching posters whose face shouldn't be shown automatically."
222   :type '(choice regexp (const nil))
223   :group 'gnus-article-washing)
224
225 (defcustom gnus-article-banner-alist nil
226   "Banner alist for stripping.
227 For example, 
228      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( eGroups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
229   :type '(repeat (cons symbol regexp))
230   :group 'gnus-article-washing)
231
232 (defcustom gnus-emphasis-alist
233   (let ((format
234          "\\(\\s-\\|^\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\\(\\s-\\|[-,;:\"]\\s-\\|[?!.]+\\s-\\|\\s)\\)")
235         (types
236          '(("_" "_" underline)
237            ("/" "/" italic)
238            ("\\*" "\\*" bold)
239            ("_/" "/_" underline-italic)
240            ("_\\*" "\\*_" underline-bold)
241            ("\\*/" "/\\*" bold-italic)
242            ("_\\*/" "/\\*_" underline-bold-italic))))
243     `(("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
244        2 3 gnus-emphasis-underline)
245       ,@(mapcar
246          (lambda (spec)
247            (list
248             (format format (car spec) (cadr spec))
249             2 3 (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
250          types)))
251   "*Alist that says how to fontify certain phrases.
252 Each item looks like this:
253
254   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
255
256 The first element is a regular expression to be matched.  The second
257 is a number that says what regular expression grouping used to find
258 the entire emphasized word.  The third is a number that says what
259 regexp grouping should be displayed and highlighted.  The fourth
260 is the face used for highlighting."
261   :type '(repeat (list :value ("" 0 0 default)
262                        regexp
263                        (integer :tag "Match group")
264                        (integer :tag "Emphasize group")
265                        face))
266   :group 'gnus-article-emphasis)
267
268 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
269   "A regexp to describe whitespace which should not be emphasized.
270 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
271 The former avoids underlining of leading and trailing whitespace,
272 and the latter avoids underlining any whitespace at all."
273   :group 'gnus-article-emphasis
274   :type 'regexp)
275
276 (defface gnus-emphasis-bold '((t (:bold t)))
277   "Face used for displaying strong emphasized text (*word*)."
278   :group 'gnus-article-emphasis)
279
280 (defface gnus-emphasis-italic '((t (:italic t)))
281   "Face used for displaying italic emphasized text (/word/)."
282   :group 'gnus-article-emphasis)
283
284 (defface gnus-emphasis-underline '((t (:underline t)))
285   "Face used for displaying underlined emphasized text (_word_)."
286   :group 'gnus-article-emphasis)
287
288 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
289   "Face used for displaying underlined bold emphasized text (_*word*_)."
290   :group 'gnus-article-emphasis)
291
292 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
293   "Face used for displaying underlined italic emphasized text (_/word/_)."
294   :group 'gnus-article-emphasis)
295
296 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
297   "Face used for displaying bold italic emphasized text (/*word*/)."
298   :group 'gnus-article-emphasis)
299
300 (defface gnus-emphasis-underline-bold-italic
301   '((t (:bold t :italic t :underline t)))
302   "Face used for displaying underlined bold italic emphasized text.
303 Esample: (_/*word*/_)."
304   :group 'gnus-article-emphasis)
305
306 (defface gnus-emphasis-highlight-words
307   '((t (:background "black" :foreground "yellow")))
308   "Face used for displaying highlighted words."
309   :group 'gnus-article-emphasis)
310
311 (defcustom gnus-article-time-format "%a, %b %d %Y %T %Z"
312   "Format for display of Date headers in article bodies.
313 See `format-time-string' for the possible values.
314
315 The variable can also be function, which should return a complete Date
316 header.  The function is called with one argument, the time, which can
317 be fed to `format-time-string'."
318   :type '(choice string symbol)
319   :link '(custom-manual "(gnus)Article Date")
320   :group 'gnus-article-washing)
321
322 (eval-and-compile
323   (autoload 'mail-extract-address-components "mail-extr"))
324
325 (defcustom gnus-save-all-headers t
326   "*If non-nil, don't remove any headers before saving."
327   :group 'gnus-article-saving
328   :type 'boolean)
329
330 (defcustom gnus-prompt-before-saving 'always
331   "*This variable says how much prompting is to be done when saving articles.
332 If it is nil, no prompting will be done, and the articles will be
333 saved to the default files.  If this variable is `always', each and
334 every article that is saved will be preceded by a prompt, even when
335 saving large batches of articles.  If this variable is neither nil not
336 `always', there the user will be prompted once for a file name for
337 each invocation of the saving commands."
338   :group 'gnus-article-saving
339   :type '(choice (item always)
340                  (item :tag "never" nil)
341                  (sexp :tag "once" :format "%t\n" :value t)))
342
343 (defcustom gnus-saved-headers gnus-visible-headers
344   "Headers to keep if `gnus-save-all-headers' is nil.
345 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
346 If that variable is nil, however, all headers that match this regexp
347 will be kept while the rest will be deleted before saving."
348   :group 'gnus-article-saving
349   :type 'regexp)
350
351 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
352   "A function to save articles in your favourite format.
353 The function must be interactively callable (in other words, it must
354 be an Emacs command).
355
356 Gnus provides the following functions:
357
358 * gnus-summary-save-in-rmail (Rmail format)
359 * gnus-summary-save-in-mail (Unix mail format)
360 * gnus-summary-save-in-folder (MH folder)
361 * gnus-summary-save-in-file (article format)
362 * gnus-summary-save-in-vm (use VM's folder format)
363 * gnus-summary-write-to-file (article format -- overwrite)."
364   :group 'gnus-article-saving
365   :type '(radio (function-item gnus-summary-save-in-rmail)
366                 (function-item gnus-summary-save-in-mail)
367                 (function-item gnus-summary-save-in-folder)
368                 (function-item gnus-summary-save-in-file)
369                 (function-item gnus-summary-save-in-vm)
370                 (function-item gnus-summary-write-to-file)))
371
372 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
373   "A function generating a file name to save articles in Rmail format.
374 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
375   :group 'gnus-article-saving
376   :type 'function)
377
378 (defcustom gnus-mail-save-name 'gnus-plain-save-name
379   "A function generating a file name to save articles in Unix mail format.
380 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
381   :group 'gnus-article-saving
382   :type 'function)
383
384 (defcustom gnus-folder-save-name 'gnus-folder-save-name
385   "A function generating a file name to save articles in MH folder.
386 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
387   :group 'gnus-article-saving
388   :type 'function)
389
390 (defcustom gnus-file-save-name 'gnus-numeric-save-name
391   "A function generating a file name to save articles in article format.
392 The function is called with NEWSGROUP, HEADERS, and optional
393 LAST-FILE."
394   :group 'gnus-article-saving
395   :type 'function)
396
397 (defcustom gnus-split-methods
398   '((gnus-article-archive-name)
399     (gnus-article-nndoc-name))
400   "*Variable used to suggest where articles are to be saved.
401 For instance, if you would like to save articles related to Gnus in
402 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
403 you could set this variable to something like:
404
405  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
406    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
407
408 This variable is an alist where the where the key is the match and the
409 value is a list of possible files to save in if the match is non-nil.
410
411 If the match is a string, it is used as a regexp match on the
412 article.  If the match is a symbol, that symbol will be funcalled
413 from the buffer of the article to be saved with the newsgroup as the
414 parameter.  If it is a list, it will be evaled in the same buffer.
415
416 If this form or function returns a string, this string will be used as
417 a possible file name; and if it returns a non-nil list, that list will
418 be used as possible file names."
419   :group 'gnus-article-saving
420   :type '(repeat (choice (list :value (fun) function)
421                          (cons :value ("" "") regexp (repeat string))
422                          (sexp :value nil))))
423
424 (defcustom gnus-page-delimiter "^\^L"
425   "*Regexp describing what to use as article page delimiters.
426 The default value is \"^\^L\", which is a form linefeed at the
427 beginning of a line."
428   :type 'regexp
429   :group 'gnus-article-various)
430
431 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
432   "*The format specification for the article mode line.
433 See `gnus-summary-mode-line-format' for a closer description.
434
435 The following additional specs are available:
436
437 %w  The article washing status.
438 %m  The number of MIME parts in the article."
439   :type 'string
440   :group 'gnus-article-various)
441
442 (defcustom gnus-article-mode-hook nil
443   "*A hook for Gnus article mode."
444   :type 'hook
445   :group 'gnus-article-various)
446
447 (defcustom gnus-article-menu-hook nil
448   "*Hook run after the creation of the article mode menu."
449   :type 'hook
450   :group 'gnus-article-various)
451
452 (defcustom gnus-article-prepare-hook nil
453   "*A hook called after an article has been prepared in the article buffer."
454   :type 'hook
455   :group 'gnus-article-various)
456
457 (defcustom gnus-article-hide-pgp-hook nil
458   "*A hook called after successfully hiding a PGP signature."
459   :type 'hook
460   :group 'gnus-article-various)
461
462 (defcustom gnus-article-button-face 'bold
463   "Face used for highlighting buttons in the article buffer.
464
465 An article button is a piece of text that you can activate by pressing
466 `RET' or `mouse-2' above it."
467   :type 'face
468   :group 'gnus-article-buttons)
469
470 (defcustom gnus-article-mouse-face 'highlight
471   "Face used for mouse highlighting in the article buffer.
472
473 Article buttons will be displayed in this face when the cursor is
474 above them."
475   :type 'face
476   :group 'gnus-article-buttons)
477
478 (defcustom gnus-signature-face 'gnus-signature-face
479   "Face used for highlighting a signature in the article buffer.
480 Obsolete; use the face `gnus-signature-face' for customizations instead."
481   :type 'face
482   :group 'gnus-article-highlight
483   :group 'gnus-article-signature)
484
485 (defface gnus-signature-face
486   '((t
487      (:italic t)))
488   "Face used for highlighting a signature in the article buffer."
489   :group 'gnus-article-highlight
490   :group 'gnus-article-signature)
491
492 (defface gnus-header-from-face
493   '((((class color)
494       (background dark))
495      (:foreground "spring green"))
496     (((class color)
497       (background light))
498      (:foreground "red3"))
499     (t
500      (:italic t)))
501   "Face used for displaying from headers."
502   :group 'gnus-article-headers
503   :group 'gnus-article-highlight)
504
505 (defface gnus-header-subject-face
506   '((((class color)
507       (background dark))
508      (:foreground "SeaGreen3"))
509     (((class color)
510       (background light))
511      (:foreground "red4"))
512     (t
513      (:bold t :italic t)))
514   "Face used for displaying subject headers."
515   :group 'gnus-article-headers
516   :group 'gnus-article-highlight)
517
518 (defface gnus-header-newsgroups-face
519   '((((class color)
520       (background dark))
521      (:foreground "yellow" :italic t))
522     (((class color)
523       (background light))
524      (:foreground "MidnightBlue" :italic t))
525     (t
526      (:italic t)))
527   "Face used for displaying newsgroups headers."
528   :group 'gnus-article-headers
529   :group 'gnus-article-highlight)
530
531 (defface gnus-header-name-face
532   '((((class color)
533       (background dark))
534      (:foreground "SeaGreen"))
535     (((class color)
536       (background light))
537      (:foreground "maroon"))
538     (t
539      (:bold t)))
540   "Face used for displaying header names."
541   :group 'gnus-article-headers
542   :group 'gnus-article-highlight)
543
544 (defface gnus-header-content-face
545   '((((class color)
546       (background dark))
547      (:foreground "forest green" :italic t))
548     (((class color)
549       (background light))
550      (:foreground "indianred4" :italic t))
551     (t
552      (:italic t)))  "Face used for displaying header content."
553   :group 'gnus-article-headers
554   :group 'gnus-article-highlight)
555
556 (defcustom gnus-header-face-alist
557   '(("From" nil gnus-header-from-face)
558     ("Subject" nil gnus-header-subject-face)
559     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
560     ("" gnus-header-name-face gnus-header-content-face))
561   "*Controls highlighting of article header.
562
563 An alist of the form (HEADER NAME CONTENT).
564
565 HEADER is a regular expression which should match the name of an
566 header header and NAME and CONTENT are either face names or nil.
567
568 The name of each header field will be displayed using the face
569 specified by the first element in the list where HEADER match the
570 header name and NAME is non-nil.  Similarly, the content will be
571 displayed by the first non-nil matching CONTENT face."
572   :group 'gnus-article-headers
573   :group 'gnus-article-highlight
574   :type '(repeat (list (regexp :tag "Header")
575                        (choice :tag "Name"
576                                (item :tag "skip" nil)
577                                (face :value default))
578                        (choice :tag "Content"
579                                (item :tag "skip" nil)
580                                (face :value default)))))
581
582 (defcustom gnus-article-decode-hook
583   '(article-decode-charset article-decode-encoded-words)
584   "*Hook run to decode charsets in articles."
585   :group 'gnus-article-headers
586   :type 'hook)
587
588 (defcustom gnus-display-mime-function 'gnus-display-mime
589   "Function to display MIME articles."
590   :group 'gnus-article-mime
591   :type 'function)
592
593 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
594   "Function used to decode headers.")
595
596 (defvar gnus-article-dumbquotes-map
597   '(("\202" ",")
598     ("\203" "f")
599     ("\204" ",,")
600     ("\205" "...")
601     ("\213" "<")
602     ("\214" "OE")
603     ("\221" "`")
604     ("\222" "'")
605     ("\223" "``")
606     ("\224" "\"")
607     ("\225" "*")
608     ("\226" "-")
609     ("\227" "--")
610     ("\231" "(TM)")
611     ("\233" ">")
612     ("\234" "oe")
613     ("\264" "'"))
614   "Table for MS-to-Latin1 translation.")
615
616 (defcustom gnus-ignored-mime-types nil
617   "List of MIME types that should be ignored by Gnus."
618   :group 'gnus-article-mime
619   :type '(repeat regexp))
620
621 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
622   "List of MIME types that should not be given buttons when rendered inline."
623   :group 'gnus-article-mime
624   :type '(repeat regexp))
625
626 (defcustom gnus-article-mime-part-function nil
627   "Function called with a MIME handle as the argument.
628 This is meant for people who want to do something automatic based
629 on parts -- for instance, adding Vcard info to a database."
630   :group 'gnus-article-mime
631   :type 'function)
632
633 (defcustom gnus-mime-multipart-functions nil
634   "An alist of MIME types to functions to display them.")
635
636 (defcustom gnus-article-date-lapsed-new-header nil
637   "Whether the X-Sent and Date headers can coexist.
638 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
639 either replace the old \"Date:\" header (if this variable is nil), or
640 be added below it (otherwise)."
641   :group 'gnus-article-headers
642   :type 'boolean)
643
644 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
645   "Function called with a MIME handle as the argument.
646 This is meant for people who want to view first matched part.
647 For `undisplayed-alternative' (default), the first undisplayed 
648 part or alternative part is used.  For `undisplayed', the first 
649 undisplayed part is used.  For a function, the first part which 
650 the function return `t' is used.  For `nil', the first part is
651 used."
652   :group 'gnus-article-mime
653   :type '(choice 
654           (item :tag "first" :value nil)
655           (item :tag "undisplayed" :value undisplayed)
656           (item :tag "undisplayed or alternative" 
657                 :value undisplayed-alternative)
658           (function)))
659
660 (defcustom gnus-mime-action-alist
661   '(("save to file" . gnus-mime-save-part)
662     ("save and strip" . gnus-mime-save-part-and-strip)
663     ("display as text" . gnus-mime-inline-part)
664     ("view the part" . gnus-mime-view-part)
665     ("pipe to command" . gnus-mime-pipe-part)
666     ("toggle display" . gnus-article-press-button)
667     ("toggle display" . gnus-article-view-part-as-charset)
668     ("view as type" . gnus-mime-view-part-as-type)
669     ("internalize type" . gnus-mime-internalize-part)
670     ("externalize type" . gnus-mime-externalize-part))
671   "An alist of actions that run on the MIME attachment."
672   :group 'gnus-article-mime
673   :type '(repeat (cons (string :tag "name")
674                        (function))))
675
676 ;;;
677 ;;; The treatment variables
678 ;;;
679
680 (defvar gnus-part-display-hook nil
681   "Hook called on parts that are to receive treatment.")
682
683 (defvar gnus-article-treat-custom
684   '(choice (const :tag "Off" nil)
685            (const :tag "On" t)
686            (const :tag "Header" head)
687            (const :tag "Last" last)
688            (integer :tag "Less")
689            (repeat :tag "Groups" regexp)
690            (sexp :tag "Predicate")))
691
692 (defvar gnus-article-treat-head-custom
693   '(choice (const :tag "Off" nil)
694            (const :tag "Header" head)))
695
696 (defvar gnus-article-treat-types '("text/plain")
697   "Parts to treat.")
698
699 (defvar gnus-inhibit-treatment nil
700   "Whether to inhibit treatment.")
701
702 (defcustom gnus-treat-highlight-signature '(or last (typep "text/x-vcard"))
703   "Highlight the signature.
704 Valid values are nil, t, `head', `last', an integer or a predicate.
705 See the manual for details."
706   :group 'gnus-article-treat
707   :type gnus-article-treat-custom)
708 (put 'gnus-treat-highlight-signature 'highlight t)
709
710 (defcustom gnus-treat-buttonize 100000
711   "Add buttons.
712 Valid values are nil, t, `head', `last', an integer or a predicate.
713 See the manual for details."
714   :group 'gnus-article-treat
715   :type gnus-article-treat-custom)
716 (put 'gnus-treat-buttonize 'highlight t)
717
718 (defcustom gnus-treat-buttonize-head 'head
719   "Add buttons to the head.
720 Valid values are nil, t, `head', `last', an integer or a predicate.
721 See the manual for details."
722   :group 'gnus-article-treat
723   :type gnus-article-treat-head-custom)
724 (put 'gnus-treat-buttonize-head 'highlight t)
725
726 (defcustom gnus-treat-emphasize 50000
727   "Emphasize text.
728 Valid values are nil, t, `head', `last', an integer or a predicate.
729 See the manual for details."
730   :group 'gnus-article-treat
731   :type gnus-article-treat-custom)
732 (put 'gnus-treat-emphasize 'highlight t)
733
734 (defcustom gnus-treat-strip-cr nil
735   "Remove carriage returns.
736 Valid values are nil, t, `head', `last', an integer or a predicate.
737 See the manual for details."
738   :group 'gnus-article-treat
739   :type gnus-article-treat-custom)
740
741 (defcustom gnus-treat-hide-headers 'head
742   "Hide headers.
743 Valid values are nil, t, `head', `last', an integer or a predicate.
744 See the manual for details."
745   :group 'gnus-article-treat
746   :type gnus-article-treat-head-custom)
747
748 (defcustom gnus-treat-hide-boring-headers nil
749   "Hide boring headers.
750 Valid values are nil, t, `head', `last', an integer or a predicate.
751 See the manual for details."
752   :group 'gnus-article-treat
753   :type gnus-article-treat-head-custom)
754
755 (defcustom gnus-treat-hide-signature nil
756   "Hide the signature.
757 Valid values are nil, t, `head', `last', an integer or a predicate.
758 See the manual for details."
759   :group 'gnus-article-treat
760   :type gnus-article-treat-custom)
761
762 (defcustom gnus-treat-fill-article nil
763   "Fill the article.
764 Valid values are nil, t, `head', `last', an integer or a predicate.
765 See the manual for details."
766   :group 'gnus-article-treat
767   :type gnus-article-treat-custom)
768
769 (defcustom gnus-treat-hide-citation nil
770   "Hide cited text.
771 Valid values are nil, t, `head', `last', an integer or a predicate.
772 See the manual for details."
773   :group 'gnus-article-treat
774   :type gnus-article-treat-custom)
775
776 (defcustom gnus-treat-hide-citation-maybe nil
777   "Hide cited text.
778 Valid values are nil, t, `head', `last', an integer or a predicate.
779 See the manual for details."
780   :group 'gnus-article-treat
781   :type gnus-article-treat-custom)
782
783 (defcustom gnus-treat-strip-list-identifiers 'head
784   "Strip list identifiers from `gnus-list-identifiers`.
785 Valid values are nil, t, `head', `last', an integer or a predicate.
786 See the manual for details."
787   :group 'gnus-article-treat
788   :type gnus-article-treat-custom)
789
790 (defcustom gnus-treat-strip-pgp t
791   "Strip PGP signatures.
792 Valid values are nil, t, `head', `last', an integer or a predicate.
793 See the manual for details."
794   :group 'gnus-article-treat
795   :type gnus-article-treat-custom)
796
797 (defcustom gnus-treat-strip-pem nil
798   "Strip PEM signatures.
799 Valid values are nil, t, `head', `last', an integer or a predicate.
800 See the manual for details."
801   :group 'gnus-article-treat
802   :type gnus-article-treat-custom)
803
804 (defcustom gnus-treat-strip-banner t
805   "Strip banners from articles.
806 The banner to be stripped is specified in the `banner' group parameter.
807 Valid values are nil, t, `head', `last', an integer or a predicate.
808 See the manual for details."
809   :group 'gnus-article-treat
810   :type gnus-article-treat-custom)
811
812 (defcustom gnus-treat-highlight-headers 'head
813   "Highlight the headers.
814 Valid values are nil, t, `head', `last', an integer or a predicate.
815 See the manual for details."
816   :group 'gnus-article-treat
817   :type gnus-article-treat-head-custom)
818 (put 'gnus-treat-highlight-headers 'highlight t)
819
820 (defcustom gnus-treat-highlight-citation t
821   "Highlight cited text.
822 Valid values are nil, t, `head', `last', an integer or a predicate.
823 See the manual for details."
824   :group 'gnus-article-treat
825   :type gnus-article-treat-custom)
826 (put 'gnus-treat-highlight-citation 'highlight t)
827
828 (defcustom gnus-treat-date-ut nil
829   "Display the Date in UT (GMT).
830 Valid values are nil, t, `head', `last', an integer or a predicate.
831 See the manual for details."
832   :group 'gnus-article-treat
833   :type gnus-article-treat-head-custom)
834
835 (defcustom gnus-treat-date-local nil
836   "Display the Date in the local timezone.
837 Valid values are nil, t, `head', `last', an integer or a predicate.
838 See the manual for details."
839   :group 'gnus-article-treat
840   :type gnus-article-treat-head-custom)
841
842 (defcustom gnus-treat-date-lapsed nil
843   "Display the Date header in a way that says how much time has elapsed.
844 Valid values are nil, t, `head', `last', an integer or a predicate.
845 See the manual for details."
846   :group 'gnus-article-treat
847   :type gnus-article-treat-head-custom)
848
849 (defcustom gnus-treat-date-original nil
850   "Display the date in the original timezone.
851 Valid values are nil, t, `head', `last', an integer or a predicate.
852 See the manual for details."
853   :group 'gnus-article-treat
854   :type gnus-article-treat-head-custom)
855
856 (defcustom gnus-treat-date-iso8601 nil
857   "Display the date in the ISO8601 format.
858 Valid values are nil, t, `head', `last', an integer or a predicate.
859 See the manual for details."
860   :group 'gnus-article-treat
861   :type gnus-article-treat-head-custom)
862
863 (defcustom gnus-treat-date-user-defined nil
864   "Display the date in a user-defined format.
865 The format is defined by the `gnus-article-time-format' variable.
866 Valid values are nil, t, `head', `last', an integer or a predicate.
867 See the manual for details."
868   :group 'gnus-article-treat
869   :type gnus-article-treat-head-custom)
870
871 (defcustom gnus-treat-strip-headers-in-body t
872   "Strip the X-No-Archive header line from the beginning of the body.
873 Valid values are nil, t, `head', `last', an integer or a predicate.
874 See the manual for details."
875   :group 'gnus-article-treat
876   :type gnus-article-treat-custom)
877
878 (defcustom gnus-treat-strip-trailing-blank-lines nil
879   "Strip trailing blank lines.
880 Valid values are nil, t, `head', `last', an integer or a predicate.
881 See the manual for details."
882   :group 'gnus-article-treat
883   :type gnus-article-treat-custom)
884
885 (defcustom gnus-treat-strip-leading-blank-lines nil
886   "Strip leading blank lines.
887 Valid values are nil, t, `head', `last', an integer or a predicate.
888 See the manual for details."
889   :group 'gnus-article-treat
890   :type gnus-article-treat-custom)
891
892 (defcustom gnus-treat-strip-multiple-blank-lines nil
893   "Strip multiple blank lines.
894 Valid values are nil, t, `head', `last', an integer or a predicate.
895 See the manual for details."
896   :group 'gnus-article-treat
897   :type gnus-article-treat-custom)
898
899 (defcustom gnus-treat-overstrike t
900   "Treat overstrike highlighting.
901 Valid values are nil, t, `head', `last', an integer or a predicate.
902 See the manual for details."
903   :group 'gnus-article-treat
904   :type gnus-article-treat-custom)
905 (put 'gnus-treat-overstrike 'highlight t)
906
907 (defcustom gnus-treat-display-xface 
908   (and (or (and (fboundp 'image-type-available-p)
909                 (image-type-available-p 'xbm)
910                 (string-match "^0x" (shell-command-to-string "uncompface")))
911            (and (featurep 'xemacs) (featurep 'xface)))
912        'head)
913   "Display X-Face headers.
914 Valid values are nil, t, `head', `last', an integer or a predicate.
915 See the manual for details."
916   :group 'gnus-article-treat
917   :type gnus-article-treat-head-custom)
918 (put 'gnus-treat-display-xface 'highlight t)
919
920 (defcustom gnus-treat-display-smileys 
921   (if (or (and (featurep 'xemacs)
922                (featurep 'xpm))
923           (and (fboundp 'image-type-available-p)
924                (image-type-available-p 'pbm)))
925       t nil)
926   "Display smileys.
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 (put 'gnus-treat-display-smileys 'highlight t)
932
933 (defcustom gnus-treat-display-picons (if (featurep 'xemacs) 'head nil)
934   "Display picons.
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-head-custom)
939 (put 'gnus-treat-display-picons 'highlight t)
940
941 (defcustom gnus-treat-capitalize-sentences nil
942   "Capitalize sentence-starting words.
943 Valid values are nil, t, `head', `last', an integer or a predicate.
944 See the manual for details."
945   :group 'gnus-article-treat
946   :type gnus-article-treat-custom)
947
948 (defcustom gnus-treat-fill-long-lines nil
949   "Fill long lines.
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
955 (defcustom gnus-treat-play-sounds nil
956   "Play sounds.
957 Valid values are nil, t, `head', `last', an integer or a predicate.
958 See the manual for details."
959   :group 'gnus-article-treat
960   :type gnus-article-treat-custom)
961
962 (defcustom gnus-treat-translate nil
963   "Translate articles from one language to another.
964 Valid values are nil, t, `head', `last', an integer or a predicate.
965 See the manual for details."
966   :group 'gnus-article-treat
967   :type gnus-article-treat-custom)
968
969 ;;; Internal variables
970
971 (defvar article-goto-body-goes-to-point-min-p nil)
972 (defvar gnus-article-wash-types nil)
973 (defvar gnus-article-emphasis-alist nil)
974
975 (defvar gnus-article-mime-handle-alist-1 nil)
976 (defvar gnus-treatment-function-alist
977   '((gnus-treat-strip-banner gnus-article-strip-banner)
978     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
979     (gnus-treat-highlight-signature gnus-article-highlight-signature)
980     (gnus-treat-buttonize gnus-article-add-buttons)
981     (gnus-treat-fill-article gnus-article-fill-cited-article)
982     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
983     (gnus-treat-strip-cr gnus-article-remove-cr)
984     (gnus-treat-emphasize gnus-article-emphasize)
985     (gnus-treat-display-xface gnus-article-display-x-face)
986     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
987     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
988     (gnus-treat-hide-signature gnus-article-hide-signature)
989     (gnus-treat-hide-citation gnus-article-hide-citation)
990     (gnus-treat-hide-citation-maybe gnus-article-hide-citation-maybe)
991     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
992     (gnus-treat-strip-pgp gnus-article-hide-pgp)
993     (gnus-treat-strip-pem gnus-article-hide-pem)
994     (gnus-treat-highlight-headers gnus-article-highlight-headers)
995     (gnus-treat-highlight-citation gnus-article-highlight-citation)
996     (gnus-treat-highlight-signature gnus-article-highlight-signature)
997     (gnus-treat-date-ut gnus-article-date-ut)
998     (gnus-treat-date-local gnus-article-date-local)
999     (gnus-treat-date-lapsed gnus-article-date-lapsed)
1000     (gnus-treat-date-original gnus-article-date-original)
1001     (gnus-treat-date-user-defined gnus-article-date-user)
1002     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
1003     (gnus-treat-strip-trailing-blank-lines
1004      gnus-article-remove-trailing-blank-lines)
1005     (gnus-treat-strip-leading-blank-lines
1006      gnus-article-strip-leading-blank-lines)
1007     (gnus-treat-strip-multiple-blank-lines
1008      gnus-article-strip-multiple-blank-lines)
1009     (gnus-treat-overstrike gnus-article-treat-overstrike)
1010     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
1011     (gnus-treat-display-smileys gnus-smiley-display)
1012     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
1013     (gnus-treat-display-picons gnus-article-display-picons)
1014     (gnus-treat-play-sounds gnus-earcon-display)))
1015
1016 (defvar gnus-article-mime-handle-alist nil)
1017 (defvar article-lapsed-timer nil)
1018 (defvar gnus-article-current-summary nil)
1019
1020 (defvar gnus-article-mode-syntax-table
1021   (let ((table (copy-syntax-table text-mode-syntax-table)))
1022     (modify-syntax-entry ?- "w" table)
1023     (modify-syntax-entry ?> ")" table)
1024     (modify-syntax-entry ?< "(" table)
1025     table)
1026   "Syntax table used in article mode buffers.
1027 Initialized from `text-mode-syntax-table.")
1028
1029 (defvar gnus-save-article-buffer nil)
1030
1031 (defvar gnus-article-mode-line-format-alist
1032   (nconc '((?w (gnus-article-wash-status) ?s)
1033            (?m (gnus-article-mime-part-status) ?s))
1034          gnus-summary-mode-line-format-alist))
1035
1036 (defvar gnus-number-of-articles-to-be-saved nil)
1037
1038 (defvar gnus-inhibit-hiding nil)
1039
1040 (defsubst gnus-article-hide-text (b e props)
1041   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1042   (add-text-properties b e props)
1043   (when (memq 'intangible props)
1044     (put-text-property
1045      (max (1- b) (point-min))
1046      b 'intangible (cddr (memq 'intangible props)))))
1047 (defsubst gnus-article-unhide-text (b e)
1048   "Remove hidden text properties from region between B and E."
1049   (remove-text-properties b e gnus-hidden-properties)
1050   (when (memq 'intangible gnus-hidden-properties)
1051     (put-text-property (max (1- b) (point-min))
1052                        b 'intangible nil)))
1053
1054 (defun gnus-article-hide-text-type (b e type)
1055   "Hide text of TYPE between B and E."
1056   (push type gnus-article-wash-types)
1057   (gnus-article-hide-text
1058    b e (cons 'article-type (cons type gnus-hidden-properties))))
1059
1060 (defun gnus-article-unhide-text-type (b e type)
1061   "Unhide text of TYPE between B and E."
1062   (setq gnus-article-wash-types
1063         (delq type gnus-article-wash-types))
1064   (remove-text-properties
1065    b e (cons 'article-type (cons type gnus-hidden-properties)))
1066   (when (memq 'intangible gnus-hidden-properties)
1067     (put-text-property (max (1- b) (point-min))
1068                        b 'intangible nil)))
1069
1070 (defun gnus-article-hide-text-of-type (type)
1071   "Hide text of TYPE in the current buffer."
1072   (save-excursion
1073     (let ((b (point-min))
1074           (e (point-max)))
1075       (while (setq b (text-property-any b e 'article-type type))
1076         (add-text-properties b (incf b) gnus-hidden-properties)))))
1077
1078 (defun gnus-article-delete-text-of-type (type)
1079   "Delete text of TYPE in the current buffer."
1080   (save-excursion
1081     (let ((b (point-min)))
1082       (while (setq b (text-property-any b (point-max) 'article-type type))
1083         (delete-region
1084          b (or (text-property-not-all b (point-max) 'article-type type)
1085                (point-max)))))))
1086
1087 (defun gnus-article-delete-invisible-text ()
1088   "Delete all invisible text in the current buffer."
1089   (save-excursion
1090     (let ((b (point-min)))
1091       (while (setq b (text-property-any b (point-max) 'invisible t))
1092         (delete-region
1093          b (or (text-property-not-all b (point-max) 'invisible t)
1094                (point-max)))))))
1095
1096 (defun gnus-article-text-type-exists-p (type)
1097   "Say whether any text of type TYPE exists in the buffer."
1098   (text-property-any (point-min) (point-max) 'article-type type))
1099
1100 (defsubst gnus-article-header-rank ()
1101   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1102   (let ((list gnus-sorted-header-list)
1103         (i 0))
1104     (while list
1105       (when (looking-at (car list))
1106         (setq list nil))
1107       (setq list (cdr list))
1108       (incf i))
1109     i))
1110
1111 (defun article-hide-headers (&optional arg delete)
1112   "Hide unwanted headers and possibly sort them as well."
1113   (interactive)
1114   ;; This function might be inhibited.
1115   (unless gnus-inhibit-hiding
1116     (save-excursion
1117       (save-restriction
1118         (let ((buffer-read-only nil)
1119               (case-fold-search t)
1120               (max (1+ (length gnus-sorted-header-list)))
1121               (ignored (when (not gnus-visible-headers)
1122                          (cond ((stringp gnus-ignored-headers)
1123                                 gnus-ignored-headers)
1124                                ((listp gnus-ignored-headers)
1125                                 (mapconcat 'identity gnus-ignored-headers
1126                                            "\\|")))))
1127               (visible
1128                (cond ((stringp gnus-visible-headers)
1129                       gnus-visible-headers)
1130                      ((and gnus-visible-headers
1131                            (listp gnus-visible-headers))
1132                       (mapconcat 'identity gnus-visible-headers "\\|"))))
1133               (inhibit-point-motion-hooks t)
1134               beg)
1135           ;; First we narrow to just the headers.
1136           (article-narrow-to-head)
1137           ;; Hide any "From " lines at the beginning of (mail) articles.
1138           (while (looking-at "From ")
1139             (forward-line 1))
1140           (unless (bobp)
1141             (delete-region (point-min) (point)))
1142           ;; Then treat the rest of the header lines.
1143           ;; Then we use the two regular expressions
1144           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1145           ;; select which header lines is to remain visible in the
1146           ;; article buffer.
1147           (while (re-search-forward "^[^ \t]*:" nil t)
1148             (beginning-of-line)
1149             ;; Mark the rank of the header.
1150             (put-text-property
1151              (point) (1+ (point)) 'message-rank
1152              (if (or (and visible (looking-at visible))
1153                      (and ignored
1154                           (not (looking-at ignored))))
1155                  (gnus-article-header-rank)
1156                (+ 2 max)))
1157             (forward-line 1))
1158           (message-sort-headers-1)
1159           (when (setq beg (text-property-any
1160                            (point-min) (point-max) 'message-rank (+ 2 max)))
1161             ;; We delete the unwanted headers.
1162             (push 'headers gnus-article-wash-types)
1163             (add-text-properties (point-min) (+ 5 (point-min))
1164                                  '(article-type headers dummy-invisible t))
1165             (delete-region beg (point-max))))))))
1166
1167 (defun article-hide-boring-headers (&optional arg)
1168   "Toggle hiding of headers that aren't very interesting.
1169 If given a negative prefix, always show; if given a positive prefix,
1170 always hide."
1171   (interactive (gnus-article-hidden-arg))
1172   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1173              (not gnus-show-all-headers))
1174     (save-excursion
1175       (save-restriction
1176         (let ((buffer-read-only nil)
1177               (list gnus-boring-article-headers)
1178               (inhibit-point-motion-hooks t)
1179               elem)
1180           (article-narrow-to-head)
1181           (while list
1182             (setq elem (pop list))
1183             (goto-char (point-min))
1184             (cond
1185              ;; Hide empty headers.
1186              ((eq elem 'empty)
1187               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1188                 (forward-line -1)
1189                 (gnus-article-hide-text-type
1190                  (progn (beginning-of-line) (point))
1191                  (progn
1192                    (end-of-line)
1193                    (if (re-search-forward "^[^ \t]" nil t)
1194                        (match-beginning 0)
1195                      (point-max)))
1196                  'boring-headers)))
1197              ;; Hide boring Newsgroups header.
1198              ((eq elem 'newsgroups)
1199               (when (equal (gnus-fetch-field "newsgroups")
1200                            (gnus-group-real-name
1201                             (if (boundp 'gnus-newsgroup-name)
1202                                 gnus-newsgroup-name
1203                               "")))
1204                 (gnus-article-hide-header "newsgroups")))
1205              ((eq elem 'followup-to)
1206               (when (equal (message-fetch-field "followup-to")
1207                            (message-fetch-field "newsgroups"))
1208                 (gnus-article-hide-header "followup-to")))
1209              ((eq elem 'reply-to)
1210               (let ((from (message-fetch-field "from"))
1211                     (reply-to (message-fetch-field "reply-to")))
1212                 (when (and
1213                        from reply-to
1214                        (ignore-errors
1215                          (equal
1216                           (nth 1 (mail-extract-address-components from))
1217                           (nth 1 (mail-extract-address-components reply-to)))))
1218                   (gnus-article-hide-header "reply-to"))))
1219              ((eq elem 'date)
1220               (let ((date (message-fetch-field "date")))
1221                 (when (and date
1222                            (< (days-between (current-time-string) date)
1223                               4))
1224                   (gnus-article-hide-header "date"))))
1225              ((eq elem 'long-to)
1226               (let ((to (message-fetch-field "to"))
1227                     (cc (message-fetch-field "cc")))
1228                 (when (> (length to) 1024)
1229                   (gnus-article-hide-header "to"))
1230                 (when (> (length cc) 1024)
1231                   (gnus-article-hide-header "cc"))))
1232              ((eq elem 'many-to)
1233               (let ((to-count 0)
1234                     (cc-count 0))
1235                 (goto-char (point-min))
1236                 (while (re-search-forward "^to:" nil t)
1237                   (setq to-count (1+ to-count)))
1238                 (when (> to-count 1)
1239                   (while (> to-count 0)
1240                     (goto-char (point-min))
1241                     (save-restriction
1242                       (re-search-forward "^to:" nil nil to-count)
1243                       (forward-line -1)
1244                       (narrow-to-region (point) (point-max))
1245                       (gnus-article-hide-header "to"))
1246                     (setq to-count (1- to-count))))
1247                 (goto-char (point-min))
1248                 (while (re-search-forward "^cc:" nil t)
1249                   (setq cc-count (1+ cc-count)))
1250                 (when (> cc-count 1)
1251                   (while (> cc-count 0)
1252                     (goto-char (point-min))
1253                     (save-restriction
1254                       (re-search-forward "^cc:" nil nil cc-count)
1255                       (forward-line -1)
1256                       (narrow-to-region (point) (point-max))
1257                       (gnus-article-hide-header "cc"))
1258                     (setq cc-count (1- cc-count)))))))))))))
1259
1260 (defun gnus-article-hide-header (header)
1261   (save-excursion
1262     (goto-char (point-min))
1263     (when (re-search-forward (concat "^" header ":") nil t)
1264       (gnus-article-hide-text-type
1265        (progn (beginning-of-line) (point))
1266        (progn
1267          (end-of-line)
1268          (if (re-search-forward "^[^ \t]" nil t)
1269              (match-beginning 0)
1270            (point-max)))
1271        'boring-headers))))
1272
1273 (defvar gnus-article-normalized-header-length 40
1274   "Length of normalized headers.")
1275
1276 (defun article-normalize-headers ()
1277   "Make all header lines 40 characters long."
1278   (interactive)
1279   (let ((buffer-read-only nil)
1280         column)
1281     (save-excursion
1282       (save-restriction
1283         (article-narrow-to-head)
1284         (while (not (eobp))
1285           (cond
1286            ((< (setq column (- (gnus-point-at-eol) (point)))
1287                gnus-article-normalized-header-length)
1288             (end-of-line)
1289             (insert (make-string
1290                      (- gnus-article-normalized-header-length column)
1291                      ? )))
1292            ((> column gnus-article-normalized-header-length)
1293             (gnus-put-text-property
1294              (progn
1295                (forward-char gnus-article-normalized-header-length)
1296                (point))
1297              (gnus-point-at-eol)
1298              'invisible t))
1299            (t
1300             ;; Do nothing.
1301             ))
1302           (forward-line 1))))))
1303
1304 (defun article-treat-dumbquotes ()
1305   "Translate M******** sm*rtq**t*s into proper text.
1306 Note that this function guesses whether a character is a sm*rtq**t* or
1307 not, so it should only be used interactively."
1308   (interactive)
1309   (article-translate-strings gnus-article-dumbquotes-map))
1310
1311 (defun article-translate-characters (from to)
1312   "Translate all characters in the body of the article according to FROM and TO.
1313 FROM is a string of characters to translate from; to is a string of
1314 characters to translate to."
1315   (save-excursion
1316     (when (article-goto-body)
1317       (let ((buffer-read-only nil)
1318             (x (make-string 225 ?x))
1319             (i -1))
1320         (while (< (incf i) (length x))
1321           (aset x i i))
1322         (setq i 0)
1323         (while (< i (length from))
1324           (aset x (aref from i) (aref to i))
1325           (incf i))
1326         (translate-region (point) (point-max) x)))))
1327
1328 (defun article-translate-strings (map)
1329   "Translate all string in the body of the article according to MAP.
1330 MAP is an alist where the elements are on the form (\"from\" \"to\")."
1331   (save-excursion
1332     (when (article-goto-body)
1333       (let ((buffer-read-only nil)
1334             elem)
1335         (while (setq elem (pop map))
1336           (save-excursion
1337             (while (search-forward (car elem) nil t)
1338               (replace-match (cadr elem)))))))))
1339
1340 (defun article-treat-overstrike ()
1341   "Translate overstrikes into bold text."
1342   (interactive)
1343   (save-excursion
1344     (when (article-goto-body)
1345       (let ((buffer-read-only nil))
1346         (while (search-forward "\b" nil t)
1347           (let ((next (char-after))
1348                 (previous (char-after (- (point) 2))))
1349             ;; We do the boldification/underlining by hiding the
1350             ;; overstrikes and putting the proper text property
1351             ;; on the letters.
1352             (cond
1353              ((eq next previous)
1354               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
1355               (put-text-property (point) (1+ (point)) 'face 'bold))
1356              ((eq next ?_)
1357               (gnus-article-hide-text-type
1358                (1- (point)) (1+ (point)) 'overstrike)
1359               (put-text-property
1360                (- (point) 2) (1- (point)) 'face 'underline))
1361              ((eq previous ?_)
1362               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
1363               (put-text-property
1364                (point) (1+ (point)) 'face 'underline)))))))))
1365
1366 (defun article-fill-long-lines ()
1367   "Fill lines that are wider than the window width."
1368   (interactive)
1369   (save-excursion
1370     (let ((buffer-read-only nil)
1371           (width (window-width (get-buffer-window (current-buffer)))))
1372       (save-restriction
1373         (article-goto-body)
1374         (let ((adaptive-fill-mode nil))
1375           (while (not (eobp))
1376             (end-of-line)
1377             (when (>= (current-column) (min fill-column width))
1378               (narrow-to-region (point) (gnus-point-at-bol))
1379               (fill-paragraph nil)
1380               (goto-char (point-max))
1381               (widen))
1382             (forward-line 1)))))))
1383
1384 (defun article-capitalize-sentences ()
1385   "Capitalize the first word in each sentence."
1386   (interactive)
1387   (save-excursion
1388     (let ((buffer-read-only nil)
1389           (paragraph-start "^[\n\^L]"))
1390       (article-goto-body)
1391       (while (not (eobp))
1392         (capitalize-word 1)
1393         (forward-sentence)))))
1394
1395 (defun article-remove-cr ()
1396   "Remove trailing CRs and then translate remaining CRs into LFs."
1397   (interactive)
1398   (save-excursion
1399     (let ((buffer-read-only nil))
1400       (goto-char (point-min))
1401       (while (re-search-forward "\r+$" nil t)
1402         (replace-match "" t t))
1403       (goto-char (point-min))
1404       (while (search-forward "\r" nil t)
1405         (replace-match "\n" t t)))))
1406
1407 (defun article-remove-trailing-blank-lines ()
1408   "Remove all trailing blank lines from the article."
1409   (interactive)
1410   (save-excursion
1411     (let ((buffer-read-only nil))
1412       (goto-char (point-max))
1413       (delete-region
1414        (point)
1415        (progn
1416          (while (and (not (bobp))
1417                      (looking-at "^[ \t]*$")
1418                      (not (gnus-annotation-in-region-p
1419                            (point) (gnus-point-at-eol))))
1420            (forward-line -1))
1421          (forward-line 1)
1422          (point))))))
1423
1424 (defun article-display-x-face (&optional force)
1425   "Look for an X-Face header and display it if present."
1426   (interactive (list 'force))
1427   (save-excursion
1428     ;; Delete the old process, if any.
1429     (when (process-status "article-x-face")
1430       (delete-process "article-x-face"))
1431     (let ((inhibit-point-motion-hooks t)
1432           (case-fold-search t)
1433           from last)
1434       (save-restriction
1435         (article-narrow-to-head)
1436         (goto-char (point-min))
1437         (setq from (message-fetch-field "from"))
1438         (goto-char (point-min))
1439         (while (and gnus-article-x-face-command
1440                     (not last)
1441                     (or force
1442                         ;; Check whether this face is censored.
1443                         (not gnus-article-x-face-too-ugly)
1444                         (and gnus-article-x-face-too-ugly from
1445                              (not (string-match gnus-article-x-face-too-ugly
1446                                                 from))))
1447                     ;; Has to be present.
1448                     (re-search-forward "^X-Face: " nil t))
1449           ;; This used to try to do multiple faces (`while' instead of
1450           ;; `when' above), but (a) sending multiple EOFs to xv doesn't
1451           ;; work (b) it can crash some versions of Emacs (c) are
1452           ;; multiple faces really something to encourage?
1453           (when (stringp gnus-article-x-face-command)
1454             (setq last t))
1455           ;; We now have the area of the buffer where the X-Face is stored.
1456           (save-excursion
1457             (let ((beg (point))
1458                   (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
1459               ;; We display the face.
1460               (if (symbolp gnus-article-x-face-command)
1461                   ;; The command is a lisp function, so we call it.
1462                   (if (gnus-functionp gnus-article-x-face-command)
1463                       (funcall gnus-article-x-face-command beg end)
1464                     (error "%s is not a function" gnus-article-x-face-command))
1465                 ;; The command is a string, so we interpret the command
1466                 ;; as a, well, command, and fork it off.
1467                 (let ((process-connection-type nil))
1468                   (process-kill-without-query
1469                    (start-process
1470                     "article-x-face" nil shell-file-name shell-command-switch
1471                     gnus-article-x-face-command))
1472                   (process-send-region "article-x-face" beg end)
1473                   (process-send-eof "article-x-face"))))))))))
1474
1475 (defun article-decode-mime-words ()
1476   "Decode all MIME-encoded words in the article."
1477   (interactive)
1478   (save-excursion
1479     (set-buffer gnus-article-buffer)
1480     (let ((inhibit-point-motion-hooks t)
1481           buffer-read-only
1482           (mail-parse-charset gnus-newsgroup-charset)
1483           (mail-parse-ignored-charsets 
1484            (save-excursion (set-buffer gnus-summary-buffer)
1485                            gnus-newsgroup-ignored-charsets)))
1486       (mail-decode-encoded-word-region (point-min) (point-max)))))
1487
1488 (defun article-decode-charset (&optional prompt)
1489   "Decode charset-encoded text in the article.
1490 If PROMPT (the prefix), prompt for a coding system to use."
1491   (interactive "P")
1492   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
1493         buffer-read-only
1494         (mail-parse-charset gnus-newsgroup-charset)
1495         (mail-parse-ignored-charsets 
1496          (save-excursion (condition-case nil
1497                              (set-buffer gnus-summary-buffer)
1498                            (error))
1499                          gnus-newsgroup-ignored-charsets))
1500         ct cte ctl charset format)
1501   (save-excursion
1502     (save-restriction
1503       (article-narrow-to-head)
1504       (setq ct (message-fetch-field "Content-Type" t)
1505             cte (message-fetch-field "Content-Transfer-Encoding" t)
1506             ctl (and ct (ignore-errors
1507                           (mail-header-parse-content-type ct)))
1508             charset (cond
1509                      (prompt
1510                       (mm-read-coding-system "Charset to decode: "))
1511                      (ctl
1512                       (mail-content-type-get ctl 'charset)))
1513             format (and ctl (mail-content-type-get ctl 'format)))
1514       (when cte
1515         (setq cte (mail-header-strip cte)))
1516       (if (and ctl (not (string-match "/" (car ctl)))) 
1517           (setq ctl nil))
1518       (goto-char (point-max)))
1519     (forward-line 1)
1520     (save-restriction
1521       (narrow-to-region (point) (point-max))
1522       (when (and (eq mail-parse-charset 'gnus-decoded)
1523                  (eq (mm-body-7-or-8) '8bit))
1524         ;; The text code could have been decoded.
1525         (setq charset mail-parse-charset))
1526       (when (and (or (not ctl)
1527                      (equal (car ctl) "text/plain"))
1528                  (not format)) ;; article with format will decode later.
1529         (mm-decode-body
1530          charset (and cte (intern (downcase
1531                                    (gnus-strip-whitespace cte))))
1532          (car ctl)))))))
1533
1534 (defun article-decode-encoded-words ()
1535   "Remove encoded-word encoding from headers."
1536   (let ((inhibit-point-motion-hooks t)
1537         (mail-parse-charset gnus-newsgroup-charset)
1538         (mail-parse-ignored-charsets 
1539          (save-excursion (condition-case nil
1540                              (set-buffer gnus-summary-buffer)
1541                            (error))
1542                          gnus-newsgroup-ignored-charsets))
1543         buffer-read-only)
1544     (save-restriction
1545       (article-narrow-to-head)
1546       (funcall gnus-decode-header-function (point-min) (point-max)))))
1547
1548 (defun article-de-quoted-unreadable (&optional force)
1549   "Translate a quoted-printable-encoded article.
1550 If FORCE, decode the article whether it is marked as quoted-printable
1551 or not."
1552   (interactive (list 'force))
1553   (save-excursion
1554     (let ((buffer-read-only nil) type charset)
1555       (if (gnus-buffer-live-p gnus-original-article-buffer)
1556           (with-current-buffer gnus-original-article-buffer
1557             (setq type
1558                   (gnus-fetch-field "content-transfer-encoding"))
1559             (let* ((ct (gnus-fetch-field "content-type"))
1560                    (ctl (and ct 
1561                              (ignore-errors
1562                                (mail-header-parse-content-type ct)))))
1563               (setq charset (and ctl
1564                                  (mail-content-type-get ctl 'charset)))
1565               (if (stringp charset)
1566                   (setq charset (intern (downcase charset)))))))
1567       (unless charset 
1568         (setq charset gnus-newsgroup-charset))
1569       (when (or force
1570                 (and type (let ((case-fold-search t))
1571                             (string-match "quoted-printable" type))))
1572         (article-goto-body)
1573         (quoted-printable-decode-region
1574          (point) (point-max) (mm-charset-to-coding-system charset))))))
1575
1576 (defun article-de-base64-unreadable (&optional force)
1577   "Translate a base64 article.
1578 If FORCE, decode the article whether it is marked as base64 not."
1579   (interactive (list 'force))
1580   (save-excursion
1581     (let ((buffer-read-only nil) type charset)
1582       (if (gnus-buffer-live-p gnus-original-article-buffer)
1583           (with-current-buffer gnus-original-article-buffer
1584             (setq type
1585                   (gnus-fetch-field "content-transfer-encoding"))
1586             (let* ((ct (gnus-fetch-field "content-type"))
1587                    (ctl (and ct 
1588                              (ignore-errors
1589                                (mail-header-parse-content-type ct)))))
1590               (setq charset (and ctl
1591                                  (mail-content-type-get ctl 'charset)))
1592               (if (stringp charset)
1593                   (setq charset (intern (downcase charset)))))))
1594       (unless charset 
1595         (setq charset gnus-newsgroup-charset))
1596       (when (or force
1597                 (and type (let ((case-fold-search t))
1598                             (string-match "base64" type))))
1599         (article-goto-body)
1600         (save-restriction
1601           (narrow-to-region (point) (point-max))
1602           (base64-decode-region (point-min) (point-max))
1603           (mm-decode-coding-region
1604            (point-min) (point-max) (mm-charset-to-coding-system charset)))))))
1605
1606 (eval-when-compile
1607   (require 'rfc1843))
1608
1609 (defun article-decode-HZ ()
1610   "Translate a HZ-encoded article."
1611   (interactive)
1612   (require 'rfc1843)
1613   (save-excursion
1614     (let ((buffer-read-only nil))
1615       (rfc1843-decode-region (point-min) (point-max)))))
1616
1617 (defun article-wash-html ()
1618   "Format an html article."
1619   (interactive)
1620   (save-excursion
1621     (let ((buffer-read-only nil)
1622           charset)
1623       (if (gnus-buffer-live-p gnus-original-article-buffer)
1624           (with-current-buffer gnus-original-article-buffer
1625             (let* ((ct (gnus-fetch-field "content-type"))
1626                    (ctl (and ct 
1627                              (ignore-errors
1628                                (mail-header-parse-content-type ct)))))
1629               (setq charset (and ctl
1630                                  (mail-content-type-get ctl 'charset)))
1631               (if (stringp charset)
1632                   (setq charset (intern (downcase charset)))))))
1633       (unless charset 
1634         (setq charset gnus-newsgroup-charset))
1635       (article-goto-body)
1636       (save-window-excursion
1637         (save-restriction
1638           (narrow-to-region (point) (point-max))
1639           (mm-setup-w3)
1640           (let ((w3-strict-width (window-width))
1641                 (url-standalone-mode t))
1642             (condition-case var
1643                 (w3-region (point-min) (point-max))
1644               (error))))))))
1645
1646 (defun article-hide-list-identifiers ()
1647   "Remove list identifies from the Subject header.
1648 The `gnus-list-identifiers' variable specifies what to do."
1649   (interactive)
1650   (save-excursion
1651     (save-restriction
1652       (let ((inhibit-point-motion-hooks t)
1653             buffer-read-only)
1654         (article-narrow-to-head)
1655         (let ((regexp (if (stringp gnus-list-identifiers) gnus-list-identifiers
1656                         (mapconcat 'identity gnus-list-identifiers " *\\|"))))
1657           (when regexp
1658             (goto-char (point-min))
1659             (when (re-search-forward
1660                    (concat "^Subject: +\\(\\(\\(Re: +\\)?\\(" regexp 
1661                            " *\\)\\)+\\(Re: +\\)?\\)")
1662                    nil t)
1663               (let ((s (or (match-string 3) (match-string 5))))
1664                 (delete-region (match-beginning 1) (match-end 1))
1665                 (when s
1666                   (goto-char (match-beginning 1))
1667                   (insert s))))))))))
1668
1669 (defun article-hide-pgp ()
1670   "Remove any PGP headers and signatures in the current article."
1671   (interactive)
1672   (save-excursion
1673     (save-restriction
1674       (let ((inhibit-point-motion-hooks t)
1675             buffer-read-only beg end)
1676         (article-goto-body)
1677         ;; Hide the "header".
1678         (when (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
1679           (push 'pgp gnus-article-wash-types)
1680           (delete-region (match-beginning 0) (match-end 0))
1681           ;; Remove armor headers (rfc2440 6.2)
1682           (delete-region (point) (or (re-search-forward "^[ \t]*\n" nil t)
1683                                      (point)))
1684           (setq beg (point))
1685           ;; Hide the actual signature.
1686           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
1687                (setq end (1+ (match-beginning 0)))
1688                (delete-region
1689                 end
1690                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
1691                     (match-end 0)
1692                   ;; Perhaps we shouldn't hide to the end of the buffer
1693                   ;; if there is no end to the signature?
1694                   (point-max))))
1695           ;; Hide "- " PGP quotation markers.
1696           (when (and beg end)
1697             (narrow-to-region beg end)
1698             (goto-char (point-min))
1699             (while (re-search-forward "^- " nil t)
1700               (delete-region
1701                (match-beginning 0) (match-end 0)))
1702             (widen))
1703           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
1704
1705 (defun article-hide-pem (&optional arg)
1706   "Toggle hiding of any PEM headers and signatures in the current article.
1707 If given a negative prefix, always show; if given a positive prefix,
1708 always hide."
1709   (interactive (gnus-article-hidden-arg))
1710   (unless (gnus-article-check-hidden-text 'pem arg)
1711     (save-excursion
1712       (let (buffer-read-only end)
1713         (goto-char (point-min))
1714         ;; Hide the horrendously ugly "header".
1715         (when (and (search-forward
1716                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
1717                     nil t)
1718                    (setq end (1+ (match-beginning 0))))
1719           (push 'pem gnus-article-wash-types)
1720           (gnus-article-hide-text-type
1721            end
1722            (if (search-forward "\n\n" nil t)
1723                (match-end 0)
1724              (point-max))
1725            'pem)
1726           ;; Hide the trailer as well
1727           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
1728                                 nil t)
1729             (gnus-article-hide-text-type
1730              (match-beginning 0) (match-end 0) 'pem)))))))
1731
1732 (defun article-strip-banner ()
1733   "Strip the banner specified by the `banner' group parameter."
1734   (interactive)
1735   (save-excursion
1736     (save-restriction
1737       (let ((inhibit-point-motion-hooks t)
1738             (banner (gnus-group-find-parameter gnus-newsgroup-name 'banner))
1739             (gnus-signature-limit nil)
1740             buffer-read-only beg end)
1741         (when banner
1742           (article-goto-body)
1743           (cond
1744            ((eq banner 'signature)
1745             (when (gnus-article-narrow-to-signature)
1746               (widen)
1747               (forward-line -1)
1748               (delete-region (point) (point-max))))
1749            ((symbolp banner)
1750             (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
1751                 (while (re-search-forward banner nil t)
1752                   (delete-region (match-beginning 0) (match-end 0)))))
1753            ((stringp banner)
1754             (while (re-search-forward banner nil t)
1755               (delete-region (match-beginning 0) (match-end 0))))))))))
1756
1757 (defun article-babel ()
1758   "Translate article using an online translation service."
1759   (interactive)
1760   (require 'babel)
1761   (save-excursion
1762     (set-buffer gnus-article-buffer)
1763     (when (article-goto-body)
1764       (let* ((buffer-read-only nil)
1765              (start (point))
1766              (end (point-max))
1767              (orig (buffer-substring start end))
1768              (trans (babel-as-string orig)))
1769         (save-restriction
1770           (narrow-to-region start end)
1771           (delete-region start end)
1772           (insert trans))))))
1773
1774 (defun article-hide-signature (&optional arg)
1775   "Hide the signature in the current article.
1776 If given a negative prefix, always show; if given a positive prefix,
1777 always hide."
1778   (interactive (gnus-article-hidden-arg))
1779   (unless (gnus-article-check-hidden-text 'signature arg)
1780     (save-excursion
1781       (save-restriction
1782         (let ((buffer-read-only nil))
1783           (when (gnus-article-narrow-to-signature)
1784             (gnus-article-hide-text-type
1785              (point-min) (point-max) 'signature)))))))
1786
1787 (defun article-strip-headers-in-body ()
1788   "Strip offensive headers from bodies."
1789   (interactive)
1790   (save-excursion
1791     (article-goto-body)
1792     (let ((case-fold-search t))
1793       (when (looking-at "x-no-archive:")
1794         (gnus-delete-line)))))
1795
1796 (defun article-strip-leading-blank-lines ()
1797   "Remove all blank lines from the beginning of the article."
1798   (interactive)
1799   (save-excursion
1800     (let ((inhibit-point-motion-hooks t)
1801           buffer-read-only)
1802       (when (article-goto-body)
1803         (while (and (not (eobp))
1804                     (looking-at "[ \t]*$"))
1805           (gnus-delete-line))))))
1806
1807 (defun article-narrow-to-head ()
1808   "Narrow the buffer to the head of the message.
1809 Point is left at the beginning of the narrowed-to region."
1810   (narrow-to-region
1811    (goto-char (point-min))
1812    (if (search-forward "\n\n" nil 1)
1813        (1- (point))
1814      (point-max)))
1815   (goto-char (point-min)))
1816
1817 (defun article-goto-body ()
1818   "Place point at the start of the body."
1819   (goto-char (point-min))
1820   (cond
1821    ;; This variable is only bound when dealing with separate
1822    ;; MIME body parts.
1823    (article-goto-body-goes-to-point-min-p
1824     t)
1825    ((search-forward "\n\n" nil t)
1826     t)
1827    (t
1828     (goto-char (point-max))
1829     nil)))
1830
1831 (defun article-strip-multiple-blank-lines ()
1832   "Replace consecutive blank lines with one empty line."
1833   (interactive)
1834   (save-excursion
1835     (let ((inhibit-point-motion-hooks t)
1836           buffer-read-only)
1837       ;; First make all blank lines empty.
1838       (article-goto-body)
1839       (while (re-search-forward "^[ \t]+$" nil t)
1840         (unless (gnus-annotation-in-region-p
1841                  (match-beginning 0) (match-end 0))
1842           (replace-match "" nil t)))
1843       ;; Then replace multiple empty lines with a single empty line.
1844       (article-goto-body)
1845       (while (re-search-forward "\n\n\n+" nil t)
1846         (unless (gnus-annotation-in-region-p
1847                  (match-beginning 0) (match-end 0))
1848           (replace-match "\n\n" t t))))))
1849
1850 (defun article-strip-leading-space ()
1851   "Remove all white space from the beginning of the lines in the article."
1852   (interactive)
1853   (save-excursion
1854     (let ((inhibit-point-motion-hooks t)
1855           buffer-read-only)
1856       (article-goto-body)
1857       (while (re-search-forward "^[ \t]+" nil t)
1858         (replace-match "" t t)))))
1859
1860 (defun article-strip-trailing-space ()
1861   "Remove all white space from the end of the lines in the article."
1862   (interactive)
1863   (save-excursion
1864     (let ((inhibit-point-motion-hooks t)
1865           buffer-read-only)
1866       (article-goto-body)
1867       (while (re-search-forward "[ \t]+$" nil t)
1868         (replace-match "" t t)))))
1869
1870 (defun article-strip-blank-lines ()
1871   "Strip leading, trailing and multiple blank lines."
1872   (interactive)
1873   (article-strip-leading-blank-lines)
1874   (article-remove-trailing-blank-lines)
1875   (article-strip-multiple-blank-lines))
1876
1877 (defun article-strip-all-blank-lines ()
1878   "Strip all blank lines."
1879   (interactive)
1880   (save-excursion
1881     (let ((inhibit-point-motion-hooks t)
1882           buffer-read-only)
1883       (article-goto-body)
1884       (while (re-search-forward "^[ \t]*\n" nil t)
1885         (replace-match "" t t)))))
1886
1887 (defun gnus-article-narrow-to-signature ()
1888   "Narrow to the signature; return t if a signature is found, else nil."
1889   (let ((inhibit-point-motion-hooks t))
1890     (when (gnus-article-search-signature)
1891       (forward-line 1)
1892       ;; Check whether we have some limits to what we consider
1893       ;; to be a signature.
1894       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
1895                       (list gnus-signature-limit)))
1896             limit limited)
1897         (while (setq limit (pop limits))
1898           (if (or (and (integerp limit)
1899                        (< (- (point-max) (point)) limit))
1900                   (and (floatp limit)
1901                        (< (count-lines (point) (point-max)) limit))
1902                   (and (gnus-functionp limit)
1903                        (funcall limit))
1904                   (and (stringp limit)
1905                        (not (re-search-forward limit nil t))))
1906               ()                        ; This limit did not succeed.
1907             (setq limited t
1908                   limits nil)))
1909         (unless limited
1910           (narrow-to-region (point) (point-max))
1911           t)))))
1912
1913 (defun gnus-article-search-signature ()
1914   "Search the current buffer for the signature separator.
1915 Put point at the beginning of the signature separator."
1916   (let ((cur (point)))
1917     (goto-char (point-max))
1918     (if (if (stringp gnus-signature-separator)
1919             (re-search-backward gnus-signature-separator nil t)
1920           (let ((seps gnus-signature-separator))
1921             (while (and seps
1922                         (not (re-search-backward (car seps) nil t)))
1923               (pop seps))
1924             seps))
1925         t
1926       (goto-char cur)
1927       nil)))
1928
1929 (defun gnus-article-hidden-arg ()
1930   "Return the current prefix arg as a number, or 0 if no prefix."
1931   (list (if current-prefix-arg
1932             (prefix-numeric-value current-prefix-arg)
1933           0)))
1934
1935 (defun gnus-article-check-hidden-text (type arg)
1936   "Return nil if hiding is necessary.
1937 Arg can be nil or a number.  Nil and positive means hide, negative
1938 means show, 0 means toggle."
1939   (save-excursion
1940     (save-restriction
1941       (let ((hide (gnus-article-hidden-text-p type)))
1942         (cond
1943          ((or (null arg)
1944               (> arg 0))
1945           nil)
1946          ((< arg 0)
1947           (gnus-article-show-hidden-text type))
1948          (t
1949           (if (eq hide 'hidden)
1950               (gnus-article-show-hidden-text type)
1951             nil)))))))
1952
1953 (defun gnus-article-hidden-text-p (type)
1954   "Say whether the current buffer contains hidden text of type TYPE."
1955   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
1956     (while (and pos
1957                 (not (get-text-property pos 'invisible))
1958                 (not (get-text-property pos 'dummy-invisible)))
1959       (setq pos
1960             (text-property-any (1+ pos) (point-max) 'article-type type)))
1961     (if pos
1962         'hidden
1963       nil)))
1964
1965 (defun gnus-article-show-hidden-text (type &optional hide)
1966   "Show all hidden text of type TYPE.
1967 If HIDE, hide the text instead."
1968   (save-excursion
1969     (let ((buffer-read-only nil)
1970           (inhibit-point-motion-hooks t)
1971           (end (point-min))
1972           beg)
1973       (while (setq beg (text-property-any end (point-max) 'article-type type))
1974         (goto-char beg)
1975         (setq end (or
1976                    (text-property-not-all beg (point-max) 'article-type type)
1977                    (point-max)))
1978         (if hide
1979             (gnus-article-hide-text beg end gnus-hidden-properties)
1980           (gnus-article-unhide-text beg end))
1981         (goto-char end))
1982       t)))
1983
1984 (defconst article-time-units
1985   `((year . ,(* 365.25 24 60 60))
1986     (week . ,(* 7 24 60 60))
1987     (day . ,(* 24 60 60))
1988     (hour . ,(* 60 60))
1989     (minute . 60)
1990     (second . 1))
1991   "Mapping from time units to seconds.")
1992
1993 (defun article-date-ut (&optional type highlight header)
1994   "Convert DATE date to universal time in the current article.
1995 If TYPE is `local', convert to local time; if it is `lapsed', output
1996 how much time has lapsed since DATE.  For `lapsed', the value of
1997 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
1998 should replace the \"Date:\" one, or should be added below it."
1999   (interactive (list 'ut t))
2000   (let* ((header (or header
2001                      (message-fetch-field "date")
2002                      ""))
2003          (tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
2004          (date-regexp
2005           (cond
2006            ((not gnus-article-date-lapsed-new-header)
2007             tdate-regexp)
2008            ((eq type 'lapsed)
2009             "^X-Sent:[ \t]")
2010            (t
2011             "^Date:[ \t]")))
2012          (date (if (vectorp header) (mail-header-date header)
2013                  header))
2014          (inhibit-point-motion-hooks t)
2015          pos
2016          bface eface)
2017     (save-excursion
2018       (save-restriction
2019         (article-narrow-to-head)
2020         (when (re-search-forward tdate-regexp nil t)
2021           (setq bface (get-text-property (gnus-point-at-bol) 'face)
2022                 date (or (get-text-property (gnus-point-at-bol)
2023                                             'original-date)
2024                          date)
2025                 eface (get-text-property (1- (gnus-point-at-eol)) 'face))
2026           (forward-line 1))
2027         (when (and date (not (string= date "")))
2028           (goto-char (point-min))
2029           (let ((buffer-read-only nil))
2030             ;; Delete any old Date headers.
2031             (while (re-search-forward date-regexp nil t)
2032               (if pos
2033                   (delete-region (progn (beginning-of-line) (point))
2034                                  (progn (forward-line 1) (point)))
2035                 (delete-region (progn (beginning-of-line) (point))
2036                                (progn (end-of-line) (point)))
2037                 (setq pos (point))))
2038             (when (and (not pos) (re-search-forward tdate-regexp nil t))
2039               (forward-line 1))
2040             (if pos (goto-char pos))
2041             (insert (article-make-date-line date (or type 'ut)))
2042             (when (not pos)
2043               (insert "\n")
2044               (forward-line -1))
2045             ;; Do highlighting.
2046             (beginning-of-line)
2047             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
2048               (put-text-property (match-beginning 1) (1+ (match-end 1))
2049                                  'original-date date)
2050               (put-text-property (match-beginning 1) (1+ (match-end 1))
2051                                  'face bface)
2052               (put-text-property (match-beginning 2) (match-end 2)
2053                                  'face eface))))))))
2054
2055 (defun article-make-date-line (date type)
2056   "Return a DATE line of TYPE."
2057   (let ((time (condition-case ()
2058                   (date-to-time date)
2059                 (error '(0 0)))))
2060     (cond
2061      ;; Convert to the local timezone.  We have to slap a
2062      ;; `condition-case' round the calls to the timezone
2063      ;; functions since they aren't particularly resistant to
2064      ;; buggy dates.
2065      ((eq type 'local)
2066       (let ((tz (car (current-time-zone time))))
2067         (format "Date: %s %s%02d%02d" (current-time-string time)
2068                 (if (> tz 0) "+" "-") (/ (abs tz) 3600) 
2069                 (/ (% (abs tz) 3600) 60))))
2070      ;; Convert to Universal Time.
2071      ((eq type 'ut)
2072       (concat "Date: "
2073               (current-time-string
2074                (let* ((e (parse-time-string date))
2075                       (tm (apply 'encode-time e))
2076                       (ms (car tm))
2077                       (ls (- (cadr tm) (car (current-time-zone time)))))
2078                  (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
2079                        ((> ls 65535) (list (1+ ms) (- ls 65536)))
2080                        (t (list ms ls)))))
2081               " UT"))
2082      ;; Get the original date from the article.
2083      ((eq type 'original)
2084       (concat "Date: " (if (string-match "\n+$" date)
2085                            (substring date 0 (match-beginning 0))
2086                          date)))
2087      ;; Let the user define the format.
2088      ((eq type 'user)
2089       (if (gnus-functionp gnus-article-time-format)
2090           (funcall gnus-article-time-format time)
2091         (concat
2092          "Date: "
2093          (format-time-string gnus-article-time-format time))))
2094      ;; ISO 8601.
2095      ((eq type 'iso8601)
2096       (let ((tz (car (current-time-zone time))))
2097         (concat
2098          "Date: "
2099          (format-time-string "%Y%m%dT%H%M%S" time)
2100          (format "%s%02d%02d"
2101                  (if (> tz 0) "+" "-") (/ (abs tz) 3600) 
2102                  (/ (% (abs tz) 3600) 60)))))
2103      ;; Do an X-Sent lapsed format.
2104      ((eq type 'lapsed)
2105       ;; If the date is seriously mangled, the timezone functions are
2106       ;; liable to bug out, so we ignore all errors.
2107       (let* ((now (current-time))
2108              (real-time (subtract-time now time))
2109              (real-sec (and real-time
2110                             (+ (* (float (car real-time)) 65536)
2111                                (cadr real-time))))
2112              (sec (and real-time (abs real-sec)))
2113              num prev)
2114         (cond
2115          ((null real-time)
2116           "X-Sent: Unknown")
2117          ((zerop sec)
2118           "X-Sent: Now")
2119          (t
2120           (concat
2121            "X-Sent: "
2122            ;; This is a bit convoluted, but basically we go
2123            ;; through the time units for years, weeks, etc,
2124            ;; and divide things to see whether that results
2125            ;; in positive answers.
2126            (mapconcat
2127             (lambda (unit)
2128               (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
2129                   ;; The (remaining) seconds are too few to
2130                   ;; be divided into this time unit.
2131                   ""
2132                 ;; It's big enough, so we output it.
2133                 (setq sec (- sec (* num (cdr unit))))
2134                 (prog1
2135                     (concat (if prev ", " "") (int-to-string
2136                                                (floor num))
2137                             " " (symbol-name (car unit))
2138                             (if (> num 1) "s" ""))
2139                   (setq prev t))))
2140             article-time-units "")
2141            ;; If dates are odd, then it might appear like the
2142            ;; article was sent in the future.
2143            (if (> real-sec 0)
2144                " ago"
2145              " in the future"))))))
2146      (t
2147       (error "Unknown conversion type: %s" type)))))
2148
2149 (defun article-date-local (&optional highlight)
2150   "Convert the current article date to the local timezone."
2151   (interactive (list t))
2152   (article-date-ut 'local highlight))
2153
2154 (defun article-date-original (&optional highlight)
2155   "Convert the current article date to what it was originally.
2156 This is only useful if you have used some other date conversion
2157 function and want to see what the date was before converting."
2158   (interactive (list t))
2159   (article-date-ut 'original highlight))
2160
2161 (defun article-date-lapsed (&optional highlight)
2162   "Convert the current article date to time lapsed since it was sent."
2163   (interactive (list t))
2164   (article-date-ut 'lapsed highlight))
2165
2166 (defun article-update-date-lapsed ()
2167   "Function to be run from a timer to update the lapsed time line."
2168   (let (deactivate-mark)
2169     (save-excursion
2170       (ignore-errors
2171         (walk-windows
2172          (lambda (w)
2173            (set-buffer (window-buffer w))
2174            (when (eq major-mode 'gnus-article-mode)
2175              (goto-char (point-min))
2176              (when (re-search-forward "^X-Sent:" nil t)
2177                (article-date-lapsed t))))
2178          nil 'visible)))))
2179
2180 (defun gnus-start-date-timer (&optional n)
2181   "Start a timer to update the X-Sent header in the article buffers.
2182 The numerical prefix says how frequently (in seconds) the function
2183 is to run."
2184   (interactive "p")
2185   (unless n
2186     (setq n 1))
2187   (gnus-stop-date-timer)
2188   (setq article-lapsed-timer
2189         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
2190
2191 (defun gnus-stop-date-timer ()
2192   "Stop the X-Sent timer."
2193   (interactive)
2194   (when article-lapsed-timer
2195     (nnheader-cancel-timer article-lapsed-timer)
2196     (setq article-lapsed-timer nil)))
2197
2198 (defun article-date-user (&optional highlight)
2199   "Convert the current article date to the user-defined format.
2200 This format is defined by the `gnus-article-time-format' variable."
2201   (interactive (list t))
2202   (article-date-ut 'user highlight))
2203
2204 (defun article-date-iso8601 (&optional highlight)
2205   "Convert the current article date to ISO8601."
2206   (interactive (list t))
2207   (article-date-ut 'iso8601 highlight))
2208
2209 (defun article-show-all ()
2210   "Show all hidden text in the article buffer."
2211   (interactive)
2212   (save-excursion
2213     (let ((buffer-read-only nil))
2214       (gnus-article-unhide-text (point-min) (point-max)))))
2215
2216 (defun article-emphasize (&optional arg)
2217   "Emphasize text according to `gnus-emphasis-alist'."
2218   (interactive (gnus-article-hidden-arg))
2219   (unless (gnus-article-check-hidden-text 'emphasis arg)
2220     (save-excursion
2221       (let ((alist (or 
2222                     (condition-case nil
2223                         (with-current-buffer gnus-summary-buffer 
2224                           gnus-article-emphasis-alist) 
2225                       (error))
2226                     gnus-emphasis-alist))
2227             (buffer-read-only nil)
2228             (props (append '(article-type emphasis)
2229                            gnus-hidden-properties))
2230             regexp elem beg invisible visible face)
2231         (article-goto-body)
2232         (setq beg (point))
2233         (while (setq elem (pop alist))
2234           (goto-char beg)
2235           (setq regexp (car elem)
2236                 invisible (nth 1 elem)
2237                 visible (nth 2 elem)
2238                 face (nth 3 elem))
2239           (while (re-search-forward regexp nil t)
2240             (when (and (match-beginning visible) (match-beginning invisible))
2241               (push 'emphasis gnus-article-wash-types)
2242               (gnus-article-hide-text
2243                (match-beginning invisible) (match-end invisible) props)
2244               (gnus-article-unhide-text-type
2245                (match-beginning visible) (match-end visible) 'emphasis)
2246               (gnus-put-text-property-excluding-newlines
2247                (match-beginning visible) (match-end visible) 'face face)
2248               (goto-char (match-end invisible)))))))))
2249
2250 (defun gnus-article-setup-highlight-words (&optional highlight-words)
2251   "Setup newsgroup emphasis alist."
2252   (unless gnus-article-emphasis-alist
2253     (let ((name (and gnus-newsgroup-name
2254                      (gnus-group-real-name gnus-newsgroup-name))))
2255       (make-local-variable 'gnus-article-emphasis-alist)
2256       (setq gnus-article-emphasis-alist 
2257             (nconc 
2258              (let ((alist gnus-group-highlight-words-alist) elem highlight)
2259                (while (setq elem (pop alist))
2260                  (when (and name (string-match (car elem) name))
2261                    (setq alist nil
2262                          highlight (copy-sequence (cdr elem)))))
2263                highlight)
2264              (copy-sequence highlight-words)
2265              (if gnus-newsgroup-name
2266                  (copy-sequence (gnus-group-find-parameter 
2267                                  gnus-newsgroup-name 'highlight-words t)))
2268              gnus-emphasis-alist)))))
2269
2270 (defvar gnus-summary-article-menu)
2271 (defvar gnus-summary-post-menu)
2272
2273 ;;; Saving functions.
2274
2275 (defun gnus-article-save (save-buffer file &optional num)
2276   "Save the currently selected article."
2277   (unless gnus-save-all-headers
2278     ;; Remove headers according to `gnus-saved-headers'.
2279     (let ((gnus-visible-headers
2280            (or gnus-saved-headers gnus-visible-headers))
2281           (gnus-article-buffer save-buffer))
2282       (save-excursion
2283         (set-buffer save-buffer)
2284         (article-hide-headers 1 t))))
2285   (save-window-excursion
2286     (if (not gnus-default-article-saver)
2287         (error "No default saver is defined")
2288       ;; !!! Magic!  The saving functions all save
2289       ;; `gnus-save-article-buffer' (or so they think), but we
2290       ;; bind that variable to our save-buffer.
2291       (set-buffer gnus-article-buffer)
2292       (let* ((gnus-save-article-buffer save-buffer)
2293              (filename
2294               (cond
2295                ((not gnus-prompt-before-saving) 'default)
2296                ((eq gnus-prompt-before-saving 'always) nil)
2297                (t file)))
2298              (gnus-number-of-articles-to-be-saved
2299               (when (eq gnus-prompt-before-saving t)
2300                 num)))                  ; Magic
2301         (set-buffer gnus-article-current-summary)
2302         (funcall gnus-default-article-saver filename)))))
2303
2304 (defun gnus-read-save-file-name (prompt &optional filename
2305                                         function group headers variable)
2306   (let ((default-name
2307           (funcall function group headers (symbol-value variable)))
2308         result)
2309     (setq result
2310           (expand-file-name
2311            (cond
2312             ((eq filename 'default)
2313              default-name)
2314             ((eq filename t)
2315              default-name)
2316             (filename filename)
2317             (t
2318              (let* ((split-name (gnus-get-split-value gnus-split-methods))
2319                     (prompt
2320                      (format prompt
2321                              (if (and gnus-number-of-articles-to-be-saved
2322                                       (> gnus-number-of-articles-to-be-saved 1))
2323                                  (format "these %d articles"
2324                                          gnus-number-of-articles-to-be-saved)
2325                                "this article")))
2326                     (file
2327                      ;; Let the split methods have their say.
2328                      (cond
2329                       ;; No split name was found.
2330                       ((null split-name)
2331                        (read-file-name
2332                         (concat prompt " (default "
2333                                 (file-name-nondirectory default-name) ") ")
2334                         (file-name-directory default-name)
2335                         default-name))
2336                       ;; A single group name is returned.
2337                       ((stringp split-name)
2338                        (setq default-name
2339                              (funcall function split-name headers
2340                                       (symbol-value variable)))
2341                        (read-file-name
2342                         (concat prompt " (default "
2343                                 (file-name-nondirectory default-name) ") ")
2344                         (file-name-directory default-name)
2345                         default-name))
2346                       ;; A single split name was found
2347                       ((= 1 (length split-name))
2348                        (let* ((name (expand-file-name
2349                                      (car split-name)
2350                                      gnus-article-save-directory))
2351                               (dir (cond ((file-directory-p name)
2352                                           (file-name-as-directory name))
2353                                          ((file-exists-p name) name)
2354                                          (t gnus-article-save-directory))))
2355                          (read-file-name
2356                           (concat prompt " (default " name ") ")
2357                           dir name)))
2358                       ;; A list of splits was found.
2359                       (t
2360                        (setq split-name (nreverse split-name))
2361                        (let (result)
2362                          (let ((file-name-history
2363                                 (nconc split-name file-name-history)))
2364                            (setq result
2365                                  (expand-file-name
2366                                   (read-file-name
2367                                    (concat prompt " (`M-p' for defaults) ")
2368                                    gnus-article-save-directory
2369                                    (car split-name))
2370                                   gnus-article-save-directory)))
2371                          (car (push result file-name-history)))))))
2372                ;; Create the directory.
2373                (gnus-make-directory (file-name-directory file))
2374       ;; If we have read a directory, we append the default file name.
2375                (when (file-directory-p file)
2376                  (setq file (expand-file-name (file-name-nondirectory
2377                                                default-name)
2378                                               (file-name-as-directory file))))
2379                ;; Possibly translate some characters.
2380                (nnheader-translate-file-chars file))))))
2381     (gnus-make-directory (file-name-directory result))
2382     (set variable result)))
2383
2384 (defun gnus-article-archive-name (group)
2385   "Return the first instance of an \"Archive-name\" in the current buffer."
2386   (let ((case-fold-search t))
2387     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
2388       (nnheader-concat gnus-article-save-directory
2389                        (match-string 1)))))
2390
2391 (defun gnus-article-nndoc-name (group)
2392   "If GROUP is an nndoc group, return the name of the parent group."
2393   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
2394     (gnus-group-get-parameter group 'save-article-group)))
2395
2396 (defun gnus-summary-save-in-rmail (&optional filename)
2397   "Append this article to Rmail file.
2398 Optional argument FILENAME specifies file name.
2399 Directory to save to is default to `gnus-article-save-directory'."
2400   (setq filename (gnus-read-save-file-name
2401                   "Save %s in rmail file:" filename
2402                   gnus-rmail-save-name gnus-newsgroup-name
2403                   gnus-current-headers 'gnus-newsgroup-last-rmail))
2404   (gnus-eval-in-buffer-window gnus-save-article-buffer
2405     (save-excursion
2406       (save-restriction
2407         (widen)
2408         (gnus-output-to-rmail filename))))
2409   filename)
2410
2411 (defun gnus-summary-save-in-mail (&optional filename)
2412   "Append this article to Unix mail file.
2413 Optional argument FILENAME specifies file name.
2414 Directory to save to is default to `gnus-article-save-directory'."
2415   (setq filename (gnus-read-save-file-name
2416                   "Save %s in Unix mail file:" filename
2417                   gnus-mail-save-name gnus-newsgroup-name
2418                   gnus-current-headers 'gnus-newsgroup-last-mail))
2419   (gnus-eval-in-buffer-window gnus-save-article-buffer
2420     (save-excursion
2421       (save-restriction
2422         (widen)
2423         (if (and (file-readable-p filename)
2424                  (mail-file-babyl-p filename))
2425             (rmail-output-to-rmail-file filename t)
2426           (gnus-output-to-mail filename)))))
2427   filename)
2428
2429 (defun gnus-summary-save-in-file (&optional filename overwrite)
2430   "Append this article to file.
2431 Optional argument FILENAME specifies file name.
2432 Directory to save to is default to `gnus-article-save-directory'."
2433   (setq filename (gnus-read-save-file-name
2434                   "Save %s in file:" filename
2435                   gnus-file-save-name gnus-newsgroup-name
2436                   gnus-current-headers 'gnus-newsgroup-last-file))
2437   (gnus-eval-in-buffer-window gnus-save-article-buffer
2438     (save-excursion
2439       (save-restriction
2440         (widen)
2441         (when (and overwrite
2442                    (file-exists-p filename))
2443           (delete-file filename))
2444         (gnus-output-to-file filename))))
2445   filename)
2446
2447 (defun gnus-summary-write-to-file (&optional filename)
2448   "Write this article to a file.
2449 Optional argument FILENAME specifies file name.
2450 The directory to save in defaults to `gnus-article-save-directory'."
2451   (gnus-summary-save-in-file nil t))
2452
2453 (defun gnus-summary-save-body-in-file (&optional filename)
2454   "Append this article body to a file.
2455 Optional argument FILENAME specifies file name.
2456 The directory to save in defaults to `gnus-article-save-directory'."
2457   (setq filename (gnus-read-save-file-name
2458                   "Save %s body in file:" filename
2459                   gnus-file-save-name gnus-newsgroup-name
2460                   gnus-current-headers 'gnus-newsgroup-last-file))
2461   (gnus-eval-in-buffer-window gnus-save-article-buffer
2462     (save-excursion
2463       (save-restriction
2464         (widen)
2465         (when (article-goto-body)
2466           (narrow-to-region (point) (point-max)))
2467         (gnus-output-to-file filename))))
2468   filename)
2469
2470 (defun gnus-summary-save-in-pipe (&optional command)
2471   "Pipe this article to subprocess."
2472   (setq command
2473         (cond ((and (eq command 'default)
2474                     gnus-last-shell-command)
2475                gnus-last-shell-command)
2476               (command command)
2477               (t (read-string
2478                   (format
2479                    "Shell command on %s: "
2480                    (if (and gnus-number-of-articles-to-be-saved
2481                             (> gnus-number-of-articles-to-be-saved 1))
2482                        (format "these %d articles"
2483                                gnus-number-of-articles-to-be-saved)
2484                      "this article"))
2485                   gnus-last-shell-command))))
2486   (when (string-equal command "")
2487     (setq command gnus-last-shell-command))
2488   (gnus-eval-in-buffer-window gnus-article-buffer
2489     (save-restriction
2490       (widen)
2491       (shell-command-on-region (point-min) (point-max) command nil)))
2492   (setq gnus-last-shell-command command))
2493
2494 ;;; Article file names when saving.
2495
2496 (defun gnus-capitalize-newsgroup (newsgroup)
2497   "Capitalize NEWSGROUP name."
2498   (when (not (zerop (length newsgroup)))
2499     (concat (char-to-string (upcase (aref newsgroup 0)))
2500             (substring newsgroup 1))))
2501
2502 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2503   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2504 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
2505 Otherwise, it is like ~/News/news/group/num."
2506   (let ((default
2507           (expand-file-name
2508            (concat (if (gnus-use-long-file-name 'not-save)
2509                        (gnus-capitalize-newsgroup newsgroup)
2510                      (gnus-newsgroup-directory-form newsgroup))
2511                    "/" (int-to-string (mail-header-number headers)))
2512            gnus-article-save-directory)))
2513     (if (and last-file
2514              (string-equal (file-name-directory default)
2515                            (file-name-directory last-file))
2516              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2517         default
2518       (or last-file default))))
2519
2520 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2521   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2522 If variable `gnus-use-long-file-name' is non-nil, it is
2523 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2524   (let ((default
2525           (expand-file-name
2526            (concat (if (gnus-use-long-file-name 'not-save)
2527                        newsgroup
2528                      (gnus-newsgroup-directory-form newsgroup))
2529                    "/" (int-to-string (mail-header-number headers)))
2530            gnus-article-save-directory)))
2531     (if (and last-file
2532              (string-equal (file-name-directory default)
2533                            (file-name-directory last-file))
2534              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2535         default
2536       (or last-file default))))
2537
2538 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2539   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2540 If variable `gnus-use-long-file-name' is non-nil, it is
2541 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2542   (or last-file
2543       (expand-file-name
2544        (if (gnus-use-long-file-name 'not-save)
2545            newsgroup
2546          (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup)))
2547        gnus-article-save-directory)))
2548
2549 (eval-and-compile
2550   (mapcar
2551    (lambda (func)
2552      (let (afunc gfunc)
2553        (if (consp func)
2554            (setq afunc (car func)
2555                  gfunc (cdr func))
2556          (setq afunc func
2557                gfunc (intern (format "gnus-%s" func))))
2558        (defalias gfunc
2559          (if (fboundp afunc)
2560            `(lambda (&optional interactive &rest args)
2561               ,(documentation afunc t)
2562               (interactive (list t))
2563               (save-excursion
2564                 (set-buffer gnus-article-buffer)
2565                 (if interactive
2566                     (call-interactively ',afunc)
2567                   (apply ',afunc args))))))))
2568    '(article-hide-headers
2569      article-hide-boring-headers
2570      article-treat-overstrike
2571      article-fill-long-lines
2572      article-capitalize-sentences
2573      article-remove-cr
2574      article-display-x-face
2575      article-de-quoted-unreadable
2576      article-de-base64-unreadable
2577      article-decode-HZ
2578      article-wash-html
2579      article-hide-list-identifiers
2580      article-hide-pgp
2581      article-strip-banner
2582      article-babel
2583      article-hide-pem
2584      article-hide-signature
2585      article-strip-headers-in-body
2586      article-remove-trailing-blank-lines
2587      article-strip-leading-blank-lines
2588      article-strip-multiple-blank-lines
2589      article-strip-leading-space
2590      article-strip-trailing-space
2591      article-strip-blank-lines
2592      article-strip-all-blank-lines
2593      article-date-local
2594      article-date-iso8601
2595      article-date-original
2596      article-date-ut
2597      article-decode-mime-words
2598      article-decode-charset
2599      article-decode-encoded-words
2600      article-date-user
2601      article-date-lapsed
2602      article-emphasize
2603      article-treat-dumbquotes
2604      article-normalize-headers
2605      (article-show-all . gnus-article-show-all-headers))))
2606 \f
2607 ;;;
2608 ;;; Gnus article mode
2609 ;;;
2610
2611 (put 'gnus-article-mode 'mode-class 'special)
2612
2613 (set-keymap-parent gnus-article-mode-map widget-keymap)
2614
2615 (gnus-define-keys gnus-article-mode-map
2616   " " gnus-article-goto-next-page
2617   "\177" gnus-article-goto-prev-page
2618   [delete] gnus-article-goto-prev-page
2619   [backspace] gnus-article-goto-prev-page
2620   "\C-c^" gnus-article-refer-article
2621   "h" gnus-article-show-summary
2622   "s" gnus-article-show-summary
2623   "\C-c\C-m" gnus-article-mail
2624   "?" gnus-article-describe-briefly
2625   "e" gnus-summary-edit-article
2626   "<" beginning-of-buffer
2627   ">" end-of-buffer
2628   "\C-c\C-i" gnus-info-find-node
2629   "\C-c\C-b" gnus-bug
2630
2631   "\C-d" gnus-article-read-summary-keys
2632   "\M-*" gnus-article-read-summary-keys
2633   "\M-#" gnus-article-read-summary-keys
2634   "\M-^" gnus-article-read-summary-keys
2635   "\M-g" gnus-article-read-summary-keys)
2636
2637 (substitute-key-definition
2638  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
2639
2640 (defun gnus-article-make-menu-bar ()
2641   (gnus-turn-off-edit-menu 'article)
2642   (unless (boundp 'gnus-article-article-menu)
2643     (easy-menu-define
2644      gnus-article-article-menu gnus-article-mode-map ""
2645      '("Article"
2646        ["Scroll forwards" gnus-article-goto-next-page t]
2647        ["Scroll backwards" gnus-article-goto-prev-page t]
2648        ["Show summary" gnus-article-show-summary t]
2649        ["Fetch Message-ID at point" gnus-article-refer-article t]
2650        ["Mail to address at point" gnus-article-mail t]
2651        ["Send a bug report" gnus-bug t]))
2652
2653     (easy-menu-define
2654      gnus-article-treatment-menu gnus-article-mode-map ""
2655      '("Treatment"
2656        ["Hide headers" gnus-article-hide-headers t]
2657        ["Hide signature" gnus-article-hide-signature t]
2658        ["Hide citation" gnus-article-hide-citation t]
2659        ["Treat overstrike" gnus-article-treat-overstrike t]
2660        ["Remove carriage return" gnus-article-remove-cr t]
2661        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
2662        ["Remove base64" gnus-article-de-base64-unreadable t]
2663        ["Treat html" gnus-article-wash-html t]
2664        ["Decode HZ" gnus-article-decode-HZ t]))
2665
2666     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
2667
2668     (when (boundp 'gnus-summary-post-menu)
2669       (define-key gnus-article-mode-map [menu-bar post]
2670         (cons "Post" gnus-summary-post-menu)))
2671
2672     (gnus-run-hooks 'gnus-article-menu-hook)))
2673
2674 (defun gnus-article-mode ()
2675   "Major mode for displaying an article.
2676
2677 All normal editing commands are switched off.
2678
2679 The following commands are available in addition to all summary mode
2680 commands:
2681 \\<gnus-article-mode-map>
2682 \\[gnus-article-next-page]\t Scroll the article one page forwards
2683 \\[gnus-article-prev-page]\t Scroll the article one page backwards
2684 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
2685 \\[gnus-article-show-summary]\t Display the summary buffer
2686 \\[gnus-article-mail]\t Send a reply to the address near point
2687 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
2688 \\[gnus-info-find-node]\t Go to the Gnus info node"
2689   (interactive)
2690   (when (gnus-visual-p 'article-menu 'menu)
2691     (gnus-article-make-menu-bar))
2692   (gnus-simplify-mode-line)
2693   (setq mode-name "Article")
2694   (setq major-mode 'gnus-article-mode)
2695   (make-local-variable 'minor-mode-alist)
2696   (use-local-map gnus-article-mode-map)
2697   (gnus-update-format-specifications nil 'article-mode)
2698   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
2699   (make-local-variable 'gnus-page-broken)
2700   (make-local-variable 'gnus-button-marker-list)
2701   (make-local-variable 'gnus-article-current-summary)
2702   (make-local-variable 'gnus-article-mime-handles)
2703   (make-local-variable 'gnus-article-decoded-p)
2704   (make-local-variable 'gnus-article-mime-handle-alist)
2705   (make-local-variable 'gnus-article-wash-types)
2706   (make-local-variable 'gnus-article-charset)
2707   (make-local-variable 'gnus-article-ignored-charsets)
2708   (gnus-set-default-directory)
2709   (buffer-disable-undo)
2710   (setq buffer-read-only t)
2711   (set-syntax-table gnus-article-mode-syntax-table)
2712   (mm-enable-multibyte)
2713   (gnus-run-hooks 'gnus-article-mode-hook))
2714
2715 (defun gnus-article-setup-buffer ()
2716   "Initialize the article buffer."
2717   (let* ((name (if gnus-single-article-buffer "*Article*"
2718                  (concat "*Article " gnus-newsgroup-name "*")))
2719          (original
2720           (progn (string-match "\\*Article" name)
2721                  (concat " *Original Article"
2722                          (substring name (match-end 0))))))
2723     (setq gnus-article-buffer name)
2724     (setq gnus-original-article-buffer original)
2725     (setq gnus-article-mime-handle-alist nil)
2726     ;; This might be a variable local to the summary buffer.
2727     (unless gnus-single-article-buffer
2728       (save-excursion
2729         (set-buffer gnus-summary-buffer)
2730         (setq gnus-article-buffer name)
2731         (setq gnus-original-article-buffer original)
2732         (gnus-set-global-variables)))
2733     (gnus-article-setup-highlight-words)
2734     ;; Init original article buffer.
2735     (save-excursion
2736       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2737       (mm-enable-multibyte)
2738       (setq major-mode 'gnus-original-article-mode)
2739       (make-local-variable 'gnus-original-article))
2740     (if (get-buffer name)
2741         (save-excursion
2742           (set-buffer name)
2743           (when gnus-article-mime-handles
2744             (mm-destroy-parts gnus-article-mime-handles)
2745             (setq gnus-article-mime-handles nil))
2746           ;; Set it to nil in article-buffer!
2747           (setq gnus-article-mime-handle-alist nil) 
2748           (buffer-disable-undo)
2749           (setq buffer-read-only t)
2750           (unless (eq major-mode 'gnus-article-mode)
2751             (gnus-article-mode))
2752           (current-buffer))
2753       (save-excursion
2754         (set-buffer (gnus-get-buffer-create name))
2755         (gnus-article-mode)
2756         (make-local-variable 'gnus-summary-buffer)
2757         (gnus-summary-set-local-parameters gnus-newsgroup-name)
2758         (current-buffer)))))
2759
2760 ;; Set article window start at LINE, where LINE is the number of lines
2761 ;; from the head of the article.
2762 (defun gnus-article-set-window-start (&optional line)
2763   (set-window-start
2764    (get-buffer-window gnus-article-buffer t)
2765    (save-excursion
2766      (set-buffer gnus-article-buffer)
2767      (goto-char (point-min))
2768      (if (not line)
2769          (point-min)
2770        (gnus-message 6 "Moved to bookmark")
2771        (search-forward "\n\n" nil t)
2772        (forward-line line)
2773        (point)))))
2774
2775 (defun gnus-article-prepare (article &optional all-headers header)
2776   "Prepare ARTICLE in article mode buffer.
2777 ARTICLE should either be an article number or a Message-ID.
2778 If ARTICLE is an id, HEADER should be the article headers.
2779 If ALL-HEADERS is non-nil, no headers are hidden."
2780   (save-excursion
2781     ;; Make sure we start in a summary buffer.
2782     (unless (eq major-mode 'gnus-summary-mode)
2783       (set-buffer gnus-summary-buffer))
2784     (setq gnus-summary-buffer (current-buffer))
2785     (let* ((gnus-article (if header (mail-header-number header) article))
2786            (summary-buffer (current-buffer))
2787            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
2788            (group gnus-newsgroup-name)
2789            result)
2790       (save-excursion
2791         (gnus-article-setup-buffer)
2792         (set-buffer gnus-article-buffer)
2793         ;; Deactivate active regions.
2794         (when (and (boundp 'transient-mark-mode)
2795                    transient-mark-mode)
2796           (setq mark-active nil))
2797         (if (not (setq result (let ((buffer-read-only nil))
2798                                 (gnus-request-article-this-buffer
2799                                  article group))))
2800             ;; There is no such article.
2801             (save-excursion
2802               (when (and (numberp article)
2803                          (not (memq article gnus-newsgroup-sparse)))
2804                 (setq gnus-article-current
2805                       (cons gnus-newsgroup-name article))
2806                 (set-buffer gnus-summary-buffer)
2807                 (setq gnus-current-article article)
2808                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2809                     (progn
2810                       (gnus-summary-set-agent-mark article)
2811                       (message "Message marked for downloading"))
2812                   (gnus-summary-mark-article article gnus-canceled-mark)
2813                   (unless (memq article gnus-newsgroup-sparse)
2814                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
2815           (if (or (eq result 'pseudo)
2816                   (eq result 'nneething))
2817               (progn
2818                 (save-excursion
2819                   (set-buffer summary-buffer)
2820                   (push article gnus-newsgroup-history)
2821                   (setq gnus-last-article gnus-current-article
2822                         gnus-current-article 0
2823                         gnus-current-headers nil
2824                         gnus-article-current nil)
2825                   (if (eq result 'nneething)
2826                       (gnus-configure-windows 'summary)
2827                     (gnus-configure-windows 'article))
2828                   (gnus-set-global-variables))
2829                 (let ((gnus-article-mime-handle-alist-1
2830                        gnus-article-mime-handle-alist))
2831                   (gnus-set-mode-line 'article)))
2832             ;; The result from the `request' was an actual article -
2833             ;; or at least some text that is now displayed in the
2834             ;; article buffer.
2835             (when (and (numberp article)
2836                        (not (eq article gnus-current-article)))
2837               ;; Seems like a new article has been selected.
2838               ;; `gnus-current-article' must be an article number.
2839               (save-excursion
2840                 (set-buffer summary-buffer)
2841                 (push article gnus-newsgroup-history)
2842                 (setq gnus-last-article gnus-current-article
2843                       gnus-current-article article
2844                       gnus-current-headers
2845                       (gnus-summary-article-header gnus-current-article)
2846                       gnus-article-current
2847                       (cons gnus-newsgroup-name gnus-current-article))
2848                 (unless (vectorp gnus-current-headers)
2849                   (setq gnus-current-headers nil))
2850                 (gnus-summary-goto-subject gnus-current-article)
2851                 (when (gnus-summary-show-thread)
2852                   ;; If the summary buffer really was folded, the
2853                   ;; previous goto may not actually have gone to
2854                   ;; the right article, but the thread root instead.
2855                   ;; So we go again.
2856                   (gnus-summary-goto-subject gnus-current-article))
2857                 (gnus-run-hooks 'gnus-mark-article-hook)
2858                 (gnus-set-mode-line 'summary)
2859                 (when (gnus-visual-p 'article-highlight 'highlight)
2860                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2861                 ;; Set the global newsgroup variables here.
2862                 (gnus-set-global-variables)
2863                 (setq gnus-have-all-headers
2864                       (or all-headers gnus-show-all-headers))))
2865             (save-excursion
2866               (gnus-configure-windows 'article))
2867             (when (or (numberp article)
2868                       (stringp article))
2869               (gnus-article-prepare-display)
2870               ;; Do page break.
2871               (goto-char (point-min))
2872               (setq gnus-page-broken
2873                     (when gnus-break-pages
2874                       (gnus-narrow-to-page)
2875                       t)))
2876             (let ((gnus-article-mime-handle-alist-1
2877                    gnus-article-mime-handle-alist))
2878               (gnus-set-mode-line 'article))
2879             (article-goto-body)
2880             (set-window-point (get-buffer-window (current-buffer)) (point))
2881             (gnus-configure-windows 'article)
2882             t))))))
2883
2884 ;;;###autoload
2885 (defun gnus-article-prepare-display ()
2886   "Make the current buffer look like a nice article."
2887   ;; Hooks for getting information from the article.
2888   ;; This hook must be called before being narrowed.
2889   (let ((gnus-article-buffer (current-buffer))
2890         buffer-read-only)
2891     (unless (eq major-mode 'gnus-article-mode)
2892       (gnus-article-mode))
2893     (setq buffer-read-only nil
2894           gnus-article-wash-types nil)
2895     (gnus-run-hooks 'gnus-tmp-internal-hook)
2896     (when gnus-display-mime-function
2897       (funcall gnus-display-mime-function))
2898     (gnus-run-hooks 'gnus-article-prepare-hook)))
2899
2900 ;;;
2901 ;;; Gnus MIME viewing functions
2902 ;;;
2903
2904 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
2905   "The following specs can be used:
2906 %t  The MIME type
2907 %T  MIME type, along with additional info
2908 %n  The `name' parameter
2909 %d  The description, if any
2910 %l  The length of the encoded part
2911 %p  The part identifier number
2912 %e  Dots if the part isn't displayed")
2913
2914 (defvar gnus-mime-button-line-format-alist
2915   '((?t gnus-tmp-type ?s)
2916     (?T gnus-tmp-type-long ?s)
2917     (?n gnus-tmp-name ?s)
2918     (?d gnus-tmp-description ?s)
2919     (?p gnus-tmp-id ?s)
2920     (?l gnus-tmp-length ?d)
2921     (?e gnus-tmp-dots ?s)))
2922
2923 (defvar gnus-mime-button-commands
2924   '((gnus-article-press-button "\r" "Toggle Display")
2925     (gnus-mime-view-part "v" "View Interactively...")
2926     (gnus-mime-view-part-as-type "t" "View As Type...")
2927     (gnus-mime-view-part-as-charset "C" "View As charset...")
2928     (gnus-mime-save-part "o" "Save...")
2929     (gnus-mime-save-part-and-strip "\C-o" "Save and Strip")
2930     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
2931     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
2932     (gnus-mime-internalize-part "E" "View Internally")
2933     (gnus-mime-externalize-part "e" "View Externally")
2934     (gnus-mime-pipe-part "|" "Pipe To Command...")
2935     (gnus-mime-action-on-part "." "Take action on the part")))
2936
2937 (defun gnus-article-mime-part-status ()
2938   (if gnus-article-mime-handle-alist-1
2939       (format " (%d parts)" (length gnus-article-mime-handle-alist-1))
2940     ""))
2941
2942 (defvar gnus-mime-button-map
2943   (let ((map (make-sparse-keymap)))
2944     (set-keymap-parent map gnus-article-mode-map)
2945     (define-key map gnus-mouse-2 'gnus-article-push-button)
2946     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
2947     (dolist (c gnus-mime-button-commands)
2948       (define-key map (cadr c) (car c)))
2949     map))
2950
2951 (defun gnus-mime-button-menu (event)
2952   "Construct a context-sensitive menu of MIME commands."
2953   (interactive "e")
2954   (save-excursion
2955     (let ((pos (event-start event)))
2956       (set-buffer (window-buffer (posn-window pos)))
2957       (goto-char (posn-point pos))
2958       (gnus-article-check-buffer)
2959       (let ((response (x-popup-menu
2960                        t `("MIME Part"
2961                            ("" ,@(mapcar (lambda (c)
2962                                            (cons (caddr c) (car c)))
2963                                          gnus-mime-button-commands))))))
2964         (if response
2965             (call-interactively response))))))
2966
2967 (defun gnus-mime-view-all-parts (&optional handles)
2968   "View all the MIME parts."
2969   (interactive)
2970   (save-current-buffer
2971     (set-buffer gnus-article-buffer)
2972     (let ((handles (or handles gnus-article-mime-handles))
2973           (mail-parse-charset gnus-newsgroup-charset)
2974           (mail-parse-ignored-charsets 
2975            (save-excursion (set-buffer gnus-summary-buffer)
2976                            gnus-newsgroup-ignored-charsets)))
2977       (if (stringp (car handles))
2978           (gnus-mime-view-all-parts (cdr handles))
2979         (mapcar 'mm-display-part handles)))))
2980
2981 (defun gnus-mime-save-part-and-strip ()
2982   "Save the MIME part under point then replace it with an external body."
2983   (interactive)
2984   (gnus-article-check-buffer)
2985   (let* ((data (get-text-property (point) 'gnus-data)) 
2986          (file (mm-save-part data))
2987          param)
2988     (when file
2989       (with-current-buffer (mm-handle-buffer data)
2990         (erase-buffer)
2991         (insert "Content-Type: " (mm-handle-media-type data))
2992         (mml-insert-parameter-string (cdr (mm-handle-type data))
2993                                      '(charset))
2994         (insert "\n")
2995         (insert "Content-ID: " (message-make-message-id) "\n")
2996         (insert "Content-Transfer-Encoding: binary\n")
2997         (insert "\n"))
2998       (setcdr data
2999               (cdr (mm-make-handle nil 
3000                                    `("message/external-body"
3001                                      (access-type . "LOCAL-FILE")
3002                                      (name . ,file)))))
3003       (set-buffer gnus-summary-buffer)
3004       (gnus-article-edit-article
3005        `(lambda () 
3006            (erase-buffer)
3007            (let ((mail-parse-charset (or gnus-article-charset 
3008                                          ',gnus-newsgroup-charset))
3009                  (mail-parse-ignored-charsets 
3010                   (or gnus-article-ignored-charsets
3011                       ',gnus-newsgroup-ignored-charsets))
3012                  (mbl mml-buffer-list))
3013              (insert-buffer gnus-original-article-buffer)
3014              (save-restriction
3015                (message-narrow-to-head)
3016                (message-remove-header "Content-Type")
3017                (message-remove-header "MIME-Version")
3018                (message-remove-header "Content-Transfer-Encoding")
3019                (mail-decode-encoded-word-region (point-min) (point-max))
3020                (goto-char (point-max)))
3021              (forward-char 1)
3022              (delete-region (point) (point-max))
3023              (setq mml-buffer-list nil)
3024              (if (stringp (car gnus-article-mime-handles))
3025                  (mml-insert-mime gnus-article-mime-handles)
3026                (mml-insert-mime gnus-article-mime-handles t))
3027              (mm-destroy-parts gnus-article-mime-handles)
3028              (setq gnus-article-mime-handles nil)
3029              (make-local-hook 'kill-buffer-hook)
3030              (let ((mbl1 mml-buffer-list))
3031                (setq mml-buffer-list mbl)
3032                (set (make-local-variable 'mml-buffer-list) mbl1))
3033              (add-hook 'kill-buffer-hook 'mml-destroy-buffers t t)))
3034        `(lambda (no-highlight)
3035           (let ((mail-parse-charset (or gnus-article-charset
3036                                         ',gnus-newsgroup-charset))
3037                 (message-options message-options)
3038                 (message-options-set-recipient)
3039                 (mail-parse-ignored-charsets  
3040                  (or gnus-article-ignored-charsets
3041                      ',gnus-newsgroup-ignored-charsets)))
3042            (mml-to-mime)
3043            (mml-destroy-buffers)
3044            (remove-hook 'kill-buffer-hook 
3045                         'mml-destroy-buffers t)
3046            (kill-local-variable 'mml-buffer-list))
3047           (gnus-summary-edit-article-done
3048            ,(or (mail-header-references gnus-current-headers) "")
3049            ,(gnus-group-read-only-p) 
3050            ,gnus-summary-buffer no-highlight))))))
3051
3052 (defun gnus-mime-save-part ()
3053   "Save the MIME part under point."
3054   (interactive)
3055   (gnus-article-check-buffer)
3056   (let ((data (get-text-property (point) 'gnus-data)))
3057     (mm-save-part data)))
3058
3059 (defun gnus-mime-pipe-part ()
3060   "Pipe the MIME part under point to a process."
3061   (interactive)
3062   (gnus-article-check-buffer)
3063   (let ((data (get-text-property (point) 'gnus-data)))
3064     (mm-pipe-part data)))
3065
3066 (defun gnus-mime-view-part ()
3067   "Interactively choose a viewing method for the MIME part under point."
3068   (interactive)
3069   (gnus-article-check-buffer)
3070   (let ((data (get-text-property (point) 'gnus-data)))
3071     (mm-interactively-view-part data)))
3072
3073 (defun gnus-mime-view-part-as-type-internal ()
3074   (gnus-article-check-buffer)
3075   (let* ((name (mail-content-type-get
3076                 (mm-handle-type (get-text-property (point) 'gnus-data))
3077                 'name))
3078          (def-type (and name (mm-default-file-encoding name))))
3079     (and def-type (cons def-type 0))))
3080
3081 (defun gnus-mime-view-part-as-type (mime-type)
3082   "Choose a MIME media type, and view the part as such."
3083   (interactive
3084    (list (completing-read
3085           "View as MIME type: "
3086           (mapcar #'list (mailcap-mime-types))
3087           nil nil
3088           (gnus-mime-view-part-as-type-internal))))
3089   (gnus-article-check-buffer)
3090   (let ((handle (get-text-property (point) 'gnus-data)))
3091     (gnus-mm-display-part
3092      (mm-make-handle (mm-handle-buffer handle)
3093                      (cons mime-type (cdr (mm-handle-type handle)))
3094                      (mm-handle-encoding handle)
3095                      (mm-handle-undisplayer handle)
3096                      (mm-handle-disposition handle)
3097                      (mm-handle-description handle)
3098                      (mm-handle-cache handle)
3099                      (mm-handle-id handle)))))
3100
3101 (defun gnus-mime-copy-part (&optional handle)
3102   "Put the the MIME part under point into a new buffer."
3103   (interactive)
3104   (gnus-article-check-buffer)
3105   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3106          (contents (mm-get-part handle))|
3107          (base (file-name-nondirectory
3108                 (or
3109                  (mail-content-type-get (mm-handle-type handle) 'name)
3110                  (mail-content-type-get (mm-handle-type handle)
3111                                         'filename)
3112                  "*decoded*")))
3113          (buffer (generate-new-buffer base)))
3114     (switch-to-buffer buffer)
3115     (insert contents)
3116     ;; We do it this way to make `normal-mode' set the appropriate mode.
3117     (unwind-protect
3118         (progn
3119           (setq buffer-file-name (expand-file-name base))
3120           (normal-mode))
3121       (setq buffer-file-name nil))
3122     (goto-char (point-min))))
3123
3124 (defun gnus-mime-inline-part (&optional handle arg)
3125   "Insert the MIME part under point into the current buffer."
3126   (interactive (list nil current-prefix-arg))
3127   (gnus-article-check-buffer)
3128   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3129          contents charset
3130          (b (point))
3131          buffer-read-only)
3132     (if (and (not arg) (mm-handle-undisplayer handle))
3133         (mm-remove-part handle)
3134       (setq contents (mm-get-part handle))
3135       (cond
3136        ((not arg)
3137         (setq charset (or (mail-content-type-get
3138                            (mm-handle-type handle) 'charset)
3139                           gnus-newsgroup-charset)))
3140        ((numberp arg)
3141         (if (mm-handle-undisplayer handle)
3142             (mm-remove-part handle))
3143         (setq charset
3144               (or (cdr (assq arg 
3145                              gnus-summary-show-article-charset-alist))
3146                   (read-coding-system "Charset: ")))))
3147       (forward-line 2)
3148       (mm-insert-inline handle
3149                         (if (and charset 
3150                                  (setq charset (mm-charset-to-coding-system 
3151                                                 charset))
3152                                  (not (eq charset 'ascii)))
3153                             (mm-decode-coding-string contents charset)
3154                           contents))
3155       (goto-char b))))
3156
3157 (defun gnus-mime-view-part-as-charset (&optional handle arg)
3158   "Insert the MIME part under point into the current buffer."
3159   (interactive (list nil current-prefix-arg))
3160   (gnus-article-check-buffer)
3161   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3162          contents charset
3163          (b (point))
3164          buffer-read-only)
3165     (if (mm-handle-undisplayer handle)
3166         (mm-remove-part handle))
3167     (let ((gnus-newsgroup-charset
3168            (or (cdr (assq arg 
3169                           gnus-summary-show-article-charset-alist))
3170                (read-coding-system "Charset: ")))
3171           (gnus-newsgroup-ignored-charsets 'gnus-all))
3172       (gnus-article-press-button))))
3173
3174 (defun gnus-mime-externalize-part (&optional handle)
3175   "View the MIME part under point with an external viewer."
3176   (interactive)
3177   (gnus-article-check-buffer)
3178   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3179          (mm-user-display-methods nil)
3180          (mm-inlined-types nil)
3181          (mail-parse-charset gnus-newsgroup-charset)
3182          (mail-parse-ignored-charsets 
3183           (save-excursion (set-buffer gnus-summary-buffer)
3184                           gnus-newsgroup-ignored-charsets)))
3185     (if (mm-handle-undisplayer handle)
3186         (mm-remove-part handle)
3187       (mm-display-part handle))))
3188
3189 (defun gnus-mime-internalize-part (&optional handle)
3190   "View the MIME part under point with an internal viewer.
3191 In no internal viewer is available, use an external viewer."
3192   (interactive)
3193   (gnus-article-check-buffer)
3194   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3195          (mm-inlined-types '(".*"))
3196          (mm-inline-large-images t)
3197          (mail-parse-charset gnus-newsgroup-charset)
3198          (mail-parse-ignored-charsets 
3199           (save-excursion (set-buffer gnus-summary-buffer)
3200                           gnus-newsgroup-ignored-charsets)))
3201     (if (mm-handle-undisplayer handle)
3202         (mm-remove-part handle)
3203       (mm-display-part handle))))
3204
3205 (defun gnus-mime-action-on-part (&optional action)
3206   "Do something with the MIME attachment at \(point\)."
3207   (interactive
3208    (list (completing-read "Action: " gnus-mime-action-alist)))
3209   (gnus-article-check-buffer)
3210   (let ((action-pair (assoc action gnus-mime-action-alist)))
3211     (if action-pair
3212         (funcall (cdr action-pair)))))
3213
3214
3215 (defun gnus-article-part-wrapper (n function)
3216   (save-current-buffer
3217     (set-buffer gnus-article-buffer)
3218     (when (> n (length gnus-article-mime-handle-alist))
3219       (error "No such part"))
3220     (gnus-article-goto-part n)
3221     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3222       (funcall function handle))))
3223
3224 (defun gnus-article-pipe-part (n)
3225   "Pipe MIME part N, which is the numerical prefix."
3226   (interactive "p")
3227   (gnus-article-part-wrapper n 'mm-pipe-part))
3228
3229 (defun gnus-article-save-part (n)
3230   "Save MIME part N, which is the numerical prefix."
3231   (interactive "p")
3232   (gnus-article-part-wrapper n 'mm-save-part))
3233
3234 (defun gnus-article-interactively-view-part (n)
3235   "View MIME part N interactively, which is the numerical prefix."
3236   (interactive "p")
3237   (gnus-article-part-wrapper n 'mm-interactively-view-part))
3238
3239 (defun gnus-article-copy-part (n)
3240   "Copy MIME part N, which is the numerical prefix."
3241   (interactive "p")
3242   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
3243
3244 (defun gnus-article-view-part-as-charset (n)
3245   "Copy MIME part N, which is the numerical prefix."
3246   (interactive "p")
3247   (gnus-article-part-wrapper n 'gnus-mime-view-part-as-charset))
3248
3249 (defun gnus-article-externalize-part (n)
3250   "View MIME part N externally, which is the numerical prefix."
3251   (interactive "p")
3252   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
3253
3254 (defun gnus-article-inline-part (n)
3255   "Inline MIME part N, which is the numerical prefix."
3256   (interactive "p")
3257   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
3258
3259 (defun gnus-article-mime-match-handle-first (condition)
3260   (if condition
3261       (let ((alist gnus-article-mime-handle-alist) ihandle n)
3262         (while (setq ihandle (pop alist))
3263           (if (and (cond 
3264                     ((functionp condition)
3265                      (funcall condition (cdr ihandle)))
3266                     ((eq condition 'undisplayed) 
3267                      (not (or (mm-handle-undisplayer (cdr ihandle))
3268                               (equal (mm-handle-media-type (cdr ihandle))
3269                                      "multipart/alternative"))))
3270                     ((eq condition 'undisplayed-alternative)
3271                      (not (mm-handle-undisplayer (cdr ihandle))))
3272                     (t t))
3273                    (gnus-article-goto-part (car ihandle))
3274                    (or (not n) (< (car ihandle) n)))
3275               (setq n (car ihandle))))
3276         (or n 1))
3277     1))
3278
3279 (defun gnus-article-view-part (&optional n)
3280   "View MIME part N, which is the numerical prefix."
3281   (interactive "P")
3282   (save-current-buffer
3283     (set-buffer gnus-article-buffer)
3284     (or (numberp n) (setq n (gnus-article-mime-match-handle-first 
3285                              gnus-article-mime-match-handle-function)))
3286     (when (> n (length gnus-article-mime-handle-alist))
3287       (error "No such part"))
3288     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3289       (when (gnus-article-goto-part n)
3290         (if (equal (car handle) "multipart/alternative")
3291             (gnus-article-press-button)
3292           (when (eq (gnus-mm-display-part handle) 'internal)
3293             (gnus-set-window-start)))))))
3294
3295 (defsubst gnus-article-mime-total-parts ()
3296   (if (bufferp (car gnus-article-mime-handles))
3297       1 ;; single part
3298     (1- (length gnus-article-mime-handles))))
3299
3300 (defun gnus-mm-display-part (handle)
3301   "Display HANDLE and fix MIME button."
3302   (let ((id (get-text-property (point) 'gnus-part))
3303         (point (point))
3304         buffer-read-only)
3305     (forward-line 1)
3306     (prog1
3307         (let ((window (selected-window))
3308               (mail-parse-charset gnus-newsgroup-charset)
3309               (mail-parse-ignored-charsets 
3310                (save-excursion (set-buffer gnus-summary-buffer)
3311                                gnus-newsgroup-ignored-charsets)))
3312           (save-excursion
3313             (unwind-protect
3314                 (let ((win (get-buffer-window (current-buffer) t))
3315                       (beg (point)))
3316                   (when win
3317                     (select-window win))
3318                   (goto-char point)
3319                   (forward-line)
3320                   (if (mm-handle-displayed-p handle)
3321                       ;; This will remove the part.
3322                       (mm-display-part handle)
3323                     (save-restriction
3324                       (narrow-to-region (point) (1+ (point)))
3325                       (mm-display-part handle)
3326                       ;; We narrow to the part itself and
3327                       ;; then call the treatment functions.
3328                       (goto-char (point-min))
3329                       (forward-line 1)
3330                       (narrow-to-region (point) (point-max))
3331                       (gnus-treat-article
3332                        nil id
3333                        (gnus-article-mime-total-parts)
3334                        (mm-handle-media-type handle)))))
3335               (select-window window))))
3336       (goto-char point)
3337       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
3338       (gnus-insert-mime-button
3339        handle id (list (mm-handle-displayed-p handle)))
3340       (goto-char point))))
3341
3342 (defun gnus-article-goto-part (n)
3343   "Go to MIME part N."
3344   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
3345     (when point
3346       (goto-char point))))
3347
3348 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
3349   (let ((gnus-tmp-name
3350          (or (mail-content-type-get (mm-handle-type handle)
3351                                     'name)
3352              (mail-content-type-get (mm-handle-disposition handle)
3353                                     'filename)
3354              (mail-content-type-get (mm-handle-type handle)
3355                                     'url)
3356              ""))
3357         (gnus-tmp-type (mm-handle-media-type handle))
3358         (gnus-tmp-description
3359          (mail-decode-encoded-word-string (or (mm-handle-description handle)
3360                                               "")))
3361         (gnus-tmp-dots
3362          (if (if displayed (car displayed)
3363                (mm-handle-displayed-p handle))
3364              "" "..."))
3365         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
3366                            (buffer-size)))
3367         gnus-tmp-type-long b e)
3368     (when (string-match ".*/" gnus-tmp-name)
3369       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
3370     (setq gnus-tmp-type-long (concat gnus-tmp-type
3371                                      (and (not (equal gnus-tmp-name ""))
3372                                           (concat "; " gnus-tmp-name))))
3373     (or (equal gnus-tmp-description "")
3374         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
3375     (unless (bolp)
3376       (insert "\n"))
3377     (setq b (point))
3378     (gnus-eval-format
3379      gnus-mime-button-line-format gnus-mime-button-line-format-alist
3380      `(local-map ,gnus-mime-button-map
3381                  keymap ,gnus-mime-button-map
3382                  gnus-callback gnus-mm-display-part
3383                  gnus-part ,gnus-tmp-id
3384                  article-type annotation
3385                  gnus-data ,handle))
3386     (setq e (point))
3387     (widget-convert-button
3388      'link b e
3389      :mime-handle handle
3390      :action 'gnus-widget-press-button
3391      :button-keymap gnus-mime-button-map
3392      :help-echo
3393      (lambda (widget/window &optional overlay pos)
3394        ;; Needed to properly clear the message due to a bug in
3395        ;; wid-edit (XEmacs only).
3396        (if (boundp 'help-echo-owns-message)
3397            (setq help-echo-owns-message t))
3398        (format
3399         "%S: %s the MIME part; %S: more options"
3400         (aref gnus-mouse-2 0)
3401         ;; XEmacs will get a single widget arg; Emacs 21 will get
3402         ;; window, overlay, position.
3403         (if (mm-handle-displayed-p
3404              (if overlay
3405                  (with-current-buffer (gnus-overlay-buffer overlay)
3406                    (widget-get (widget-at (gnus-overlay-start overlay))
3407                                :mime-handle))
3408                (widget-get widget/window :mime-handle)))
3409             "hide" "show")
3410         (aref gnus-down-mouse-3 0))))))
3411
3412 (defun gnus-widget-press-button (elems el)
3413   (goto-char (widget-get elems :from))
3414   (gnus-article-press-button))
3415
3416 (defvar gnus-displaying-mime nil)
3417
3418 (defun gnus-display-mime (&optional ihandles)
3419   "Display the MIME parts."
3420   (save-excursion
3421     (save-selected-window
3422       (let ((window (get-buffer-window gnus-article-buffer))
3423             (point (point)))
3424         (when window
3425           (select-window window)
3426           ;; We have to do this since selecting the window
3427           ;; may change the point.  So we set the window point.
3428           (set-window-point window point)))
3429       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
3430              buffer-read-only handle name type b e display)