2000-10-04 15:24:46 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:\\|^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     "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | display -")
209   "*String or function to be executed to display an X-Face header.
210 If it is a string, the command will be executed in a sub-shell
211 asynchronously.  The compressed face will be piped to this command."
212   :type '(choice string
213                  (function-item gnus-article-display-xface)
214                  function)
215   :group 'gnus-article-washing)
216
217 (defcustom gnus-article-x-face-too-ugly nil
218   "Regexp matching posters whose face shouldn't be shown automatically."
219   :type '(choice regexp (const nil))
220   :group 'gnus-article-washing)
221
222 (defcustom gnus-article-banner-alist nil
223   "Banner alist for stripping.
224 For example, 
225      ((egroups . \"^[ \\t\\n]*-------------------+\\\\( eGroups Sponsor -+\\\\)?....\\n\\\\(.+\\n\\\\)+\"))"
226   :type '(repeat (cons symbol regexp))
227   :group 'gnus-article-washing)
228
229 (defcustom gnus-emphasis-alist
230   (let ((format
231          "\\(\\s-\\|^\\|[-\"]\\|\\s(\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*[.,]?\\)%s\\)\\(\\s-\\|[-,;:\"]\\s-\\|[?!.]+\\s-\\|\\s)\\)")
232         (types
233          '(("_" "_" underline)
234            ("/" "/" italic)
235            ("\\*" "\\*" bold)
236            ("_/" "/_" underline-italic)
237            ("_\\*" "\\*_" underline-bold)
238            ("\\*/" "/\\*" bold-italic)
239            ("_\\*/" "/\\*_" underline-bold-italic))))
240     `(("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_[^_]\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
241        2 3 gnus-emphasis-underline)
242       ,@(mapcar
243          (lambda (spec)
244            (list
245             (format format (car spec) (cadr spec))
246             2 3 (intern (format "gnus-emphasis-%s" (nth 2 spec)))))
247          types)))
248   "*Alist that says how to fontify certain phrases.
249 Each item looks like this:
250
251   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
252
253 The first element is a regular expression to be matched.  The second
254 is a number that says what regular expression grouping used to find
255 the entire emphasized word.  The third is a number that says what
256 regexp grouping should be displayed and highlighted.  The fourth
257 is the face used for highlighting."
258   :type '(repeat (list :value ("" 0 0 default)
259                        regexp
260                        (integer :tag "Match group")
261                        (integer :tag "Emphasize group")
262                        face))
263   :group 'gnus-article-emphasis)
264
265 (defcustom gnus-emphasize-whitespace-regexp "^[ \t]+\\|[ \t]*\n"
266   "A regexp to describe whitespace which should not be emphasized.
267 Typical values are \"^[ \\t]+\\\\|[ \\t]*\\n\" and \"[ \\t]+\\\\|[ \\t]*\\n\".
268 The former avoids underlining of leading and trailing whitespace,
269 and the latter avoids underlining any whitespace at all."
270   :group 'gnus-article-emphasis
271   :type 'regexp)
272
273 (defface gnus-emphasis-bold '((t (:bold t)))
274   "Face used for displaying strong emphasized text (*word*)."
275   :group 'gnus-article-emphasis)
276
277 (defface gnus-emphasis-italic '((t (:italic t)))
278   "Face used for displaying italic emphasized text (/word/)."
279   :group 'gnus-article-emphasis)
280
281 (defface gnus-emphasis-underline '((t (:underline t)))
282   "Face used for displaying underlined emphasized text (_word_)."
283   :group 'gnus-article-emphasis)
284
285 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
286   "Face used for displaying underlined bold emphasized text (_*word*_)."
287   :group 'gnus-article-emphasis)
288
289 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
290   "Face used for displaying underlined italic emphasized text (_/word/_)."
291   :group 'gnus-article-emphasis)
292
293 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
294   "Face used for displaying bold italic emphasized text (/*word*/)."
295   :group 'gnus-article-emphasis)
296
297 (defface gnus-emphasis-underline-bold-italic
298   '((t (:bold t :italic t :underline t)))
299   "Face used for displaying underlined bold italic emphasized text.
300 Esample: (_/*word*/_)."
301   :group 'gnus-article-emphasis)
302
303 (defface gnus-emphasis-highlight-words
304   '((t (:background "black" :foreground "yellow")))
305   "Face used for displaying highlighted words."
306   :group 'gnus-article-emphasis)
307
308 (defcustom gnus-article-time-format "%a, %b %d %Y %T %Z"
309   "Format for display of Date headers in article bodies.
310 See `format-time-string' for the possible values.
311
312 The variable can also be function, which should return a complete Date
313 header.  The function is called with one argument, the time, which can
314 be fed to `format-time-string'."
315   :type '(choice string symbol)
316   :link '(custom-manual "(gnus)Article Date")
317   :group 'gnus-article-washing)
318
319 (eval-and-compile
320   (autoload 'mail-extract-address-components "mail-extr"))
321
322 (defcustom gnus-save-all-headers t
323   "*If non-nil, don't remove any headers before saving."
324   :group 'gnus-article-saving
325   :type 'boolean)
326
327 (defcustom gnus-prompt-before-saving 'always
328   "*This variable says how much prompting is to be done when saving articles.
329 If it is nil, no prompting will be done, and the articles will be
330 saved to the default files.  If this variable is `always', each and
331 every article that is saved will be preceded by a prompt, even when
332 saving large batches of articles.  If this variable is neither nil not
333 `always', there the user will be prompted once for a file name for
334 each invocation of the saving commands."
335   :group 'gnus-article-saving
336   :type '(choice (item always)
337                  (item :tag "never" nil)
338                  (sexp :tag "once" :format "%t\n" :value t)))
339
340 (defcustom gnus-saved-headers gnus-visible-headers
341   "Headers to keep if `gnus-save-all-headers' is nil.
342 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
343 If that variable is nil, however, all headers that match this regexp
344 will be kept while the rest will be deleted before saving."
345   :group 'gnus-article-saving
346   :type 'regexp)
347
348 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
349   "A function to save articles in your favourite format.
350 The function must be interactively callable (in other words, it must
351 be an Emacs command).
352
353 Gnus provides the following functions:
354
355 * gnus-summary-save-in-rmail (Rmail format)
356 * gnus-summary-save-in-mail (Unix mail format)
357 * gnus-summary-save-in-folder (MH folder)
358 * gnus-summary-save-in-file (article format)
359 * gnus-summary-save-in-vm (use VM's folder format)
360 * gnus-summary-write-to-file (article format -- overwrite)."
361   :group 'gnus-article-saving
362   :type '(radio (function-item gnus-summary-save-in-rmail)
363                 (function-item gnus-summary-save-in-mail)
364                 (function-item gnus-summary-save-in-folder)
365                 (function-item gnus-summary-save-in-file)
366                 (function-item gnus-summary-save-in-vm)
367                 (function-item gnus-summary-write-to-file)))
368
369 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
370   "A function generating a file name to save articles in Rmail format.
371 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
372   :group 'gnus-article-saving
373   :type 'function)
374
375 (defcustom gnus-mail-save-name 'gnus-plain-save-name
376   "A function generating a file name to save articles in Unix mail format.
377 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
378   :group 'gnus-article-saving
379   :type 'function)
380
381 (defcustom gnus-folder-save-name 'gnus-folder-save-name
382   "A function generating a file name to save articles in MH folder.
383 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
384   :group 'gnus-article-saving
385   :type 'function)
386
387 (defcustom gnus-file-save-name 'gnus-numeric-save-name
388   "A function generating a file name to save articles in article format.
389 The function is called with NEWSGROUP, HEADERS, and optional
390 LAST-FILE."
391   :group 'gnus-article-saving
392   :type 'function)
393
394 (defcustom gnus-split-methods
395   '((gnus-article-archive-name)
396     (gnus-article-nndoc-name))
397   "*Variable used to suggest where articles are to be saved.
398 For instance, if you would like to save articles related to Gnus in
399 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
400 you could set this variable to something like:
401
402  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
403    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
404
405 This variable is an alist where the where the key is the match and the
406 value is a list of possible files to save in if the match is non-nil.
407
408 If the match is a string, it is used as a regexp match on the
409 article.  If the match is a symbol, that symbol will be funcalled
410 from the buffer of the article to be saved with the newsgroup as the
411 parameter.  If it is a list, it will be evaled in the same buffer.
412
413 If this form or function returns a string, this string will be used as
414 a possible file name; and if it returns a non-nil list, that list will
415 be used as possible file names."
416   :group 'gnus-article-saving
417   :type '(repeat (choice (list :value (fun) function)
418                          (cons :value ("" "") regexp (repeat string))
419                          (sexp :value nil))))
420
421 (defcustom gnus-page-delimiter "^\^L"
422   "*Regexp describing what to use as article page delimiters.
423 The default value is \"^\^L\", which is a form linefeed at the
424 beginning of a line."
425   :type 'regexp
426   :group 'gnus-article-various)
427
428 (defcustom gnus-article-mode-line-format "Gnus: %g [%w] %S%m"
429   "*The format specification for the article mode line.
430 See `gnus-summary-mode-line-format' for a closer description.
431
432 The following additional specs are available:
433
434 %w  The article washing status.
435 %m  The number of MIME parts in the article."
436   :type 'string
437   :group 'gnus-article-various)
438
439 (defcustom gnus-article-mode-hook nil
440   "*A hook for Gnus article mode."
441   :type 'hook
442   :group 'gnus-article-various)
443
444 (defcustom gnus-article-menu-hook nil
445   "*Hook run after the creation of the article mode menu."
446   :type 'hook
447   :group 'gnus-article-various)
448
449 (defcustom gnus-article-prepare-hook nil
450   "*A hook called after an article has been prepared in the article buffer."
451   :type 'hook
452   :group 'gnus-article-various)
453
454 (defcustom gnus-article-hide-pgp-hook nil
455   "*A hook called after successfully hiding a PGP signature."
456   :type 'hook
457   :group 'gnus-article-various)
458
459 (defcustom gnus-article-button-face 'bold
460   "Face used for highlighting buttons in the article buffer.
461
462 An article button is a piece of text that you can activate by pressing
463 `RET' or `mouse-2' above it."
464   :type 'face
465   :group 'gnus-article-buttons)
466
467 (defcustom gnus-article-mouse-face 'highlight
468   "Face used for mouse highlighting in the article buffer.
469
470 Article buttons will be displayed in this face when the cursor is
471 above them."
472   :type 'face
473   :group 'gnus-article-buttons)
474
475 (defcustom gnus-signature-face 'gnus-signature-face
476   "Face used for highlighting a signature in the article buffer.
477 Obsolete; use the face `gnus-signature-face' for customizations instead."
478   :type 'face
479   :group 'gnus-article-highlight
480   :group 'gnus-article-signature)
481
482 (defface gnus-signature-face
483   '((((type x))
484      (:italic t)))
485   "Face used for highlighting a signature in the article buffer."
486   :group 'gnus-article-highlight
487   :group 'gnus-article-signature)
488
489 (defface gnus-header-from-face
490   '((((class color)
491       (background dark))
492      (:foreground "spring green"))
493     (((class color)
494       (background light))
495      (:foreground "red3"))
496     (t
497      (:italic t)))
498   "Face used for displaying from headers."
499   :group 'gnus-article-headers
500   :group 'gnus-article-highlight)
501
502 (defface gnus-header-subject-face
503   '((((class color)
504       (background dark))
505      (:foreground "SeaGreen3"))
506     (((class color)
507       (background light))
508      (:foreground "red4"))
509     (t
510      (:bold t :italic t)))
511   "Face used for displaying subject headers."
512   :group 'gnus-article-headers
513   :group 'gnus-article-highlight)
514
515 (defface gnus-header-newsgroups-face
516   '((((class color)
517       (background dark))
518      (:foreground "yellow" :italic t))
519     (((class color)
520       (background light))
521      (:foreground "MidnightBlue" :italic t))
522     (t
523      (:italic t)))
524   "Face used for displaying newsgroups headers."
525   :group 'gnus-article-headers
526   :group 'gnus-article-highlight)
527
528 (defface gnus-header-name-face
529   '((((class color)
530       (background dark))
531      (:foreground "SeaGreen"))
532     (((class color)
533       (background light))
534      (:foreground "maroon"))
535     (t
536      (:bold t)))
537   "Face used for displaying header names."
538   :group 'gnus-article-headers
539   :group 'gnus-article-highlight)
540
541 (defface gnus-header-content-face
542   '((((class color)
543       (background dark))
544      (:foreground "forest green" :italic t))
545     (((class color)
546       (background light))
547      (:foreground "indianred4" :italic t))
548     (t
549      (:italic t)))  "Face used for displaying header content."
550   :group 'gnus-article-headers
551   :group 'gnus-article-highlight)
552
553 (defcustom gnus-header-face-alist
554   '(("From" nil gnus-header-from-face)
555     ("Subject" nil gnus-header-subject-face)
556     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
557     ("" gnus-header-name-face gnus-header-content-face))
558   "*Controls highlighting of article header.
559
560 An alist of the form (HEADER NAME CONTENT).
561
562 HEADER is a regular expression which should match the name of an
563 header header and NAME and CONTENT are either face names or nil.
564
565 The name of each header field will be displayed using the face
566 specified by the first element in the list where HEADER match the
567 header name and NAME is non-nil.  Similarly, the content will be
568 displayed by the first non-nil matching CONTENT face."
569   :group 'gnus-article-headers
570   :group 'gnus-article-highlight
571   :type '(repeat (list (regexp :tag "Header")
572                        (choice :tag "Name"
573                                (item :tag "skip" nil)
574                                (face :value default))
575                        (choice :tag "Content"
576                                (item :tag "skip" nil)
577                                (face :value default)))))
578
579 (defcustom gnus-article-decode-hook
580   '(article-decode-charset article-decode-encoded-words)
581   "*Hook run to decode charsets in articles."
582   :group 'gnus-article-headers
583   :type 'hook)
584
585 (defcustom gnus-display-mime-function 'gnus-display-mime
586   "Function to display MIME articles."
587   :group 'gnus-article-mime
588   :type 'function)
589
590 (defvar gnus-decode-header-function 'mail-decode-encoded-word-region
591   "Function used to decode headers.")
592
593 (defvar gnus-article-dumbquotes-map
594   '(("\202" ",")
595     ("\203" "f")
596     ("\204" ",,")
597     ("\205" "...")
598     ("\213" "<")
599     ("\214" "OE")
600     ("\221" "`")
601     ("\222" "'")
602     ("\223" "``")
603     ("\224" "\"")
604     ("\225" "*")
605     ("\226" "---")
606     ("\227" "-")
607     ("\231" "(TM)")
608     ("\233" ">")
609     ("\234" "oe")
610     ("\264" "'"))
611   "Table for MS-to-Latin1 translation.")
612
613 (defcustom gnus-ignored-mime-types nil
614   "List of MIME types that should be ignored by Gnus."
615   :group 'gnus-article-mime
616   :type '(repeat regexp))
617
618 (defcustom gnus-unbuttonized-mime-types '(".*/.*")
619   "List of MIME types that should not be given buttons when rendered inline."
620   :group 'gnus-article-mime
621   :type '(repeat regexp))
622
623 (defcustom gnus-article-mime-part-function nil
624   "Function called with a MIME handle as the argument.
625 This is meant for people who want to do something automatic based
626 on parts -- for instance, adding Vcard info to a database."
627   :group 'gnus-article-mime
628   :type 'function)
629
630 (defcustom gnus-mime-multipart-functions nil
631   "An alist of MIME types to functions to display them.")
632
633 (defcustom gnus-article-date-lapsed-new-header nil
634   "Whether the X-Sent and Date headers can coexist.
635 When using `gnus-treat-date-lapsed', the \"X-Sent:\" header will
636 either replace the old \"Date:\" header (if this variable is nil), or
637 be added below it (otherwise)."
638   :group 'gnus-article-headers
639   :type 'boolean)
640
641 (defcustom gnus-article-mime-match-handle-function 'undisplayed-alternative
642   "Function called with a MIME handle as the argument.
643 This is meant for people who want to view first matched part.
644 For `undisplayed-alternative' (default), the first undisplayed 
645 part or alternative part is used.  For `undisplayed', the first 
646 undisplayed part is used.  For a function, the first part which 
647 the function return `t' is used.  For `nil', the first part is
648 used."
649   :group 'gnus-article-mime
650   :type '(choice 
651           (item :tag "first" :value nil)
652           (item :tag "undisplayed" :value undisplayed)
653           (item :tag "undisplayed or alternative" 
654                 :value undisplayed-alternative)
655           (function)))
656
657 (defcustom gnus-mime-action-alist
658   '(("save to file" . gnus-mime-save-part)
659     ("display as text" . gnus-mime-inline-part)
660     ("view the part" . gnus-mime-view-part)
661     ("pipe to command" . gnus-mime-pipe-part)
662     ("toggle display" . gnus-article-press-button)
663     ("view as type" . gnus-mime-view-part-as-type)
664     ("internalize type" . gnus-mime-internalize-part)
665     ("externalize type" . gnus-mime-externalize-part))
666   "An alist of actions that run on the MIME attachment."
667   :group 'gnus-article-mime
668   :type '(repeat (cons (string :tag "name")
669                        (function))))
670
671 ;;;
672 ;;; The treatment variables
673 ;;;
674
675 (defvar gnus-part-display-hook nil
676   "Hook called on parts that are to receive treatment.")
677
678 (defvar gnus-article-treat-custom
679   '(choice (const :tag "Off" nil)
680            (const :tag "On" t)
681            (const :tag "Header" head)
682            (const :tag "Last" last)
683            (integer :tag "Less")
684            (repeat :tag "Groups" regexp)
685            (sexp :tag "Predicate")))
686
687 (defvar gnus-article-treat-head-custom
688   '(choice (const :tag "Off" nil)
689            (const :tag "Header" head)))
690
691 (defvar gnus-article-treat-types '("text/plain")
692   "Parts to treat.")
693
694 (defvar gnus-inhibit-treatment nil
695   "Whether to inhibit treatment.")
696
697 (defcustom gnus-treat-highlight-signature '(or last (typep "text/x-vcard"))
698   "Highlight the signature.
699 Valid values are nil, t, `head', `last', an integer or a predicate.
700 See the manual for details."
701   :group 'gnus-article-treat
702   :type gnus-article-treat-custom)
703 (put 'gnus-treat-highlight-signature 'highlight t)
704
705 (defcustom gnus-treat-buttonize 100000
706   "Add buttons.
707 Valid values are nil, t, `head', `last', an integer or a predicate.
708 See the manual for details."
709   :group 'gnus-article-treat
710   :type gnus-article-treat-custom)
711 (put 'gnus-treat-buttonize 'highlight t)
712
713 (defcustom gnus-treat-buttonize-head 'head
714   "Add buttons to the head.
715 Valid values are nil, t, `head', `last', an integer or a predicate.
716 See the manual for details."
717   :group 'gnus-article-treat
718   :type gnus-article-treat-head-custom)
719 (put 'gnus-treat-buttonize-head 'highlight t)
720
721 (defcustom gnus-treat-emphasize 50000
722   "Emphasize text.
723 Valid values are nil, t, `head', `last', an integer or a predicate.
724 See the manual for details."
725   :group 'gnus-article-treat
726   :type gnus-article-treat-custom)
727 (put 'gnus-treat-emphasize 'highlight t)
728
729 (defcustom gnus-treat-strip-cr nil
730   "Remove carriage returns.
731 Valid values are nil, t, `head', `last', an integer or a predicate.
732 See the manual for details."
733   :group 'gnus-article-treat
734   :type gnus-article-treat-custom)
735
736 (defcustom gnus-treat-hide-headers 'head
737   "Hide headers.
738 Valid values are nil, t, `head', `last', an integer or a predicate.
739 See the manual for details."
740   :group 'gnus-article-treat
741   :type gnus-article-treat-head-custom)
742
743 (defcustom gnus-treat-hide-boring-headers nil
744   "Hide boring headers.
745 Valid values are nil, t, `head', `last', an integer or a predicate.
746 See the manual for details."
747   :group 'gnus-article-treat
748   :type gnus-article-treat-head-custom)
749
750 (defcustom gnus-treat-hide-signature nil
751   "Hide the signature.
752 Valid values are nil, t, `head', `last', an integer or a predicate.
753 See the manual for details."
754   :group 'gnus-article-treat
755   :type gnus-article-treat-custom)
756
757 (defcustom gnus-treat-fill-article nil
758   "Fill the article.
759 Valid values are nil, t, `head', `last', an integer or a predicate.
760 See the manual for details."
761   :group 'gnus-article-treat
762   :type gnus-article-treat-custom)
763
764 (defcustom gnus-treat-hide-citation nil
765   "Hide cited text.
766 Valid values are nil, t, `head', `last', an integer or a predicate.
767 See the manual for details."
768   :group 'gnus-article-treat
769   :type gnus-article-treat-custom)
770
771 (defcustom gnus-treat-strip-list-identifiers 'head
772   "Strip list identifiers from `gnus-list-identifiers`.
773 Valid values are nil, t, `head', `last', an integer or a predicate.
774 See the manual for details."
775   :group 'gnus-article-treat
776   :type gnus-article-treat-custom)
777
778 (defcustom gnus-treat-strip-pgp t
779   "Strip PGP signatures.
780 Valid values are nil, t, `head', `last', an integer or a predicate.
781 See the manual for details."
782   :group 'gnus-article-treat
783   :type gnus-article-treat-custom)
784
785 (defcustom gnus-treat-strip-pem nil
786   "Strip PEM signatures.
787 Valid values are nil, t, `head', `last', an integer or a predicate.
788 See the manual for details."
789   :group 'gnus-article-treat
790   :type gnus-article-treat-custom)
791
792 (defcustom gnus-treat-strip-banner t
793   "Strip banners from articles.
794 The banner to be stripped is specified in the `banner' group parameter.
795 Valid values are nil, t, `head', `last', an integer or a predicate.
796 See the manual for details."
797   :group 'gnus-article-treat
798   :type gnus-article-treat-custom)
799
800 (defcustom gnus-treat-highlight-headers 'head
801   "Highlight the headers.
802 Valid values are nil, t, `head', `last', an integer or a predicate.
803 See the manual for details."
804   :group 'gnus-article-treat
805   :type gnus-article-treat-head-custom)
806 (put 'gnus-treat-highlight-headers 'highlight t)
807
808 (defcustom gnus-treat-highlight-citation t
809   "Highlight cited text.
810 Valid values are nil, t, `head', `last', an integer or a predicate.
811 See the manual for details."
812   :group 'gnus-article-treat
813   :type gnus-article-treat-custom)
814 (put 'gnus-treat-highlight-citation 'highlight t)
815
816 (defcustom gnus-treat-date-ut nil
817   "Display the Date in UT (GMT).
818 Valid values are nil, t, `head', `last', an integer or a predicate.
819 See the manual for details."
820   :group 'gnus-article-treat
821   :type gnus-article-treat-head-custom)
822
823 (defcustom gnus-treat-date-local nil
824   "Display the Date in the local timezone.
825 Valid values are nil, t, `head', `last', an integer or a predicate.
826 See the manual for details."
827   :group 'gnus-article-treat
828   :type gnus-article-treat-head-custom)
829
830 (defcustom gnus-treat-date-lapsed nil
831   "Display the Date header in a way that says how much time has elapsed.
832 Valid values are nil, t, `head', `last', an integer or a predicate.
833 See the manual for details."
834   :group 'gnus-article-treat
835   :type gnus-article-treat-head-custom)
836
837 (defcustom gnus-treat-date-original nil
838   "Display the date in the original timezone.
839 Valid values are nil, t, `head', `last', an integer or a predicate.
840 See the manual for details."
841   :group 'gnus-article-treat
842   :type gnus-article-treat-head-custom)
843
844 (defcustom gnus-treat-date-iso8601 nil
845   "Display the date in the ISO8601 format.
846 Valid values are nil, t, `head', `last', an integer or a predicate.
847 See the manual for details."
848   :group 'gnus-article-treat
849   :type gnus-article-treat-head-custom)
850
851 (defcustom gnus-treat-date-user-defined nil
852   "Display the date in a user-defined format.
853 The format is defined by the `gnus-article-time-format' variable.
854 Valid values are nil, t, `head', `last', an integer or a predicate.
855 See the manual for details."
856   :group 'gnus-article-treat
857   :type gnus-article-treat-head-custom)
858
859 (defcustom gnus-treat-strip-headers-in-body t
860   "Strip the X-No-Archive header line from the beginning of the body.
861 Valid values are nil, t, `head', `last', an integer or a predicate.
862 See the manual for details."
863   :group 'gnus-article-treat
864   :type gnus-article-treat-custom)
865
866 (defcustom gnus-treat-strip-trailing-blank-lines nil
867   "Strip trailing blank lines.
868 Valid values are nil, t, `head', `last', an integer or a predicate.
869 See the manual for details."
870   :group 'gnus-article-treat
871   :type gnus-article-treat-custom)
872
873 (defcustom gnus-treat-strip-leading-blank-lines nil
874   "Strip leading blank lines.
875 Valid values are nil, t, `head', `last', an integer or a predicate.
876 See the manual for details."
877   :group 'gnus-article-treat
878   :type gnus-article-treat-custom)
879
880 (defcustom gnus-treat-strip-multiple-blank-lines nil
881   "Strip multiple blank lines.
882 Valid values are nil, t, `head', `last', an integer or a predicate.
883 See the manual for details."
884   :group 'gnus-article-treat
885   :type gnus-article-treat-custom)
886
887 (defcustom gnus-treat-overstrike t
888   "Treat overstrike highlighting.
889 Valid values are nil, t, `head', `last', an integer or a predicate.
890 See the manual for details."
891   :group 'gnus-article-treat
892   :type gnus-article-treat-custom)
893 (put 'gnus-treat-overstrike 'highlight t)
894
895 (defcustom gnus-treat-display-xface 
896   (and (or (and (fboundp 'image-type-available-p)
897                 (image-type-available-p 'xbm))
898            (and gnus-xemacs (featurep 'xface)))
899        'head)
900   "Display X-Face headers.
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-head-custom)
905 (put 'gnus-treat-display-xface 'highlight t)
906
907 (defcustom gnus-treat-display-smileys (if (and gnus-xemacs
908                                                (featurep 'xpm))
909                                           t nil)
910   "Display smileys.
911 Valid values are nil, t, `head', `last', an integer or a predicate.
912 See the manual for details."
913   :group 'gnus-article-treat
914   :type gnus-article-treat-custom)
915 (put 'gnus-treat-display-smileys 'highlight t)
916
917 (defcustom gnus-treat-display-picons (if gnus-xemacs 'head nil)
918   "Display picons.
919 Valid values are nil, t, `head', `last', an integer or a predicate.
920 See the manual for details."
921   :group 'gnus-article-treat
922   :type gnus-article-treat-head-custom)
923 (put 'gnus-treat-display-picons 'highlight t)
924
925 (defcustom gnus-treat-capitalize-sentences nil
926   "Capitalize sentence-starting words.
927 Valid values are nil, t, `head', `last', an integer or a predicate.
928 See the manual for details."
929   :group 'gnus-article-treat
930   :type gnus-article-treat-custom)
931
932 (defcustom gnus-treat-fill-long-lines nil
933   "Fill long lines.
934 Valid values are nil, t, `head', `last', an integer or a predicate.
935 See the manual for details."
936   :group 'gnus-article-treat
937   :type gnus-article-treat-custom)
938
939 (defcustom gnus-treat-play-sounds nil
940   "Play sounds.
941 Valid values are nil, t, `head', `last', an integer or a predicate.
942 See the manual for details."
943   :group 'gnus-article-treat
944   :type gnus-article-treat-custom)
945
946 (defcustom gnus-treat-translate nil
947   "Translate articles from one language to another.
948 Valid values are nil, t, `head', `last', an integer or a predicate.
949 See the manual for details."
950   :group 'gnus-article-treat
951   :type gnus-article-treat-custom)
952
953 ;;; Internal variables
954
955 (defvar article-goto-body-goes-to-point-min-p nil)
956 (defvar gnus-article-wash-types nil)
957 (defvar gnus-article-emphasis-alist nil)
958
959 (defvar gnus-article-mime-handle-alist-1 nil)
960 (defvar gnus-treatment-function-alist
961   '((gnus-treat-strip-banner gnus-article-strip-banner)
962     (gnus-treat-strip-headers-in-body gnus-article-strip-headers-in-body)
963     (gnus-treat-highlight-signature gnus-article-highlight-signature)
964     (gnus-treat-buttonize gnus-article-add-buttons)
965     (gnus-treat-fill-article gnus-article-fill-cited-article)
966     (gnus-treat-fill-long-lines gnus-article-fill-long-lines)
967     (gnus-treat-strip-cr gnus-article-remove-cr)
968     (gnus-treat-emphasize gnus-article-emphasize)
969     (gnus-treat-display-xface gnus-article-display-x-face)
970     (gnus-treat-hide-headers gnus-article-maybe-hide-headers)
971     (gnus-treat-hide-boring-headers gnus-article-hide-boring-headers)
972     (gnus-treat-hide-signature gnus-article-hide-signature)
973     (gnus-treat-hide-citation gnus-article-hide-citation)
974     (gnus-treat-strip-list-identifiers gnus-article-hide-list-identifiers)
975     (gnus-treat-strip-pgp gnus-article-hide-pgp)
976     (gnus-treat-strip-pem gnus-article-hide-pem)
977     (gnus-treat-highlight-headers gnus-article-highlight-headers)
978     (gnus-treat-highlight-citation gnus-article-highlight-citation)
979     (gnus-treat-highlight-signature gnus-article-highlight-signature)
980     (gnus-treat-date-ut gnus-article-date-ut)
981     (gnus-treat-date-local gnus-article-date-local)
982     (gnus-treat-date-lapsed gnus-article-date-lapsed)
983     (gnus-treat-date-original gnus-article-date-original)
984     (gnus-treat-date-user-defined gnus-article-date-user)
985     (gnus-treat-date-iso8601 gnus-article-date-iso8601)
986     (gnus-treat-strip-trailing-blank-lines
987      gnus-article-remove-trailing-blank-lines)
988     (gnus-treat-strip-leading-blank-lines
989      gnus-article-strip-leading-blank-lines)
990     (gnus-treat-strip-multiple-blank-lines
991      gnus-article-strip-multiple-blank-lines)
992     (gnus-treat-overstrike gnus-article-treat-overstrike)
993     (gnus-treat-buttonize-head gnus-article-add-buttons-to-head)
994     (gnus-treat-display-smileys gnus-smiley-display)
995     (gnus-treat-capitalize-sentences gnus-article-capitalize-sentences)
996     (gnus-treat-display-picons gnus-article-display-picons)
997     (gnus-treat-play-sounds gnus-earcon-display)))
998
999 (defvar gnus-article-mime-handle-alist nil)
1000 (defvar article-lapsed-timer nil)
1001 (defvar gnus-article-current-summary nil)
1002
1003 (defvar gnus-article-mode-syntax-table
1004   (let ((table (copy-syntax-table text-mode-syntax-table)))
1005     (modify-syntax-entry ?- "w" table)
1006     (modify-syntax-entry ?> ")" table)
1007     (modify-syntax-entry ?< "(" table)
1008     table)
1009   "Syntax table used in article mode buffers.
1010 Initialized from `text-mode-syntax-table.")
1011
1012 (defvar gnus-save-article-buffer nil)
1013
1014 (defvar gnus-article-mode-line-format-alist
1015   (nconc '((?w (gnus-article-wash-status) ?s)
1016            (?m (gnus-article-mime-part-status) ?s))
1017          gnus-summary-mode-line-format-alist))
1018
1019 (defvar gnus-number-of-articles-to-be-saved nil)
1020
1021 (defvar gnus-inhibit-hiding nil)
1022
1023 (defsubst gnus-article-hide-text (b e props)
1024   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
1025   (add-text-properties b e props)
1026   (when (memq 'intangible props)
1027     (put-text-property
1028      (max (1- b) (point-min))
1029      b 'intangible (cddr (memq 'intangible props)))))
1030 (defsubst gnus-article-unhide-text (b e)
1031   "Remove hidden text properties from region between B and E."
1032   (remove-text-properties b e gnus-hidden-properties)
1033   (when (memq 'intangible gnus-hidden-properties)
1034     (put-text-property (max (1- b) (point-min))
1035                        b 'intangible nil)))
1036
1037 (defun gnus-article-hide-text-type (b e type)
1038   "Hide text of TYPE between B and E."
1039   (push type gnus-article-wash-types)
1040   (gnus-article-hide-text
1041    b e (cons 'article-type (cons type gnus-hidden-properties))))
1042
1043 (defun gnus-article-unhide-text-type (b e type)
1044   "Unhide text of TYPE between B and E."
1045   (setq gnus-article-wash-types
1046         (delq type gnus-article-wash-types))
1047   (remove-text-properties
1048    b e (cons 'article-type (cons type gnus-hidden-properties)))
1049   (when (memq 'intangible gnus-hidden-properties)
1050     (put-text-property (max (1- b) (point-min))
1051                        b 'intangible nil)))
1052
1053 (defun gnus-article-hide-text-of-type (type)
1054   "Hide text of TYPE in the current buffer."
1055   (save-excursion
1056     (let ((b (point-min))
1057           (e (point-max)))
1058       (while (setq b (text-property-any b e 'article-type type))
1059         (add-text-properties b (incf b) gnus-hidden-properties)))))
1060
1061 (defun gnus-article-delete-text-of-type (type)
1062   "Delete text of TYPE in the current buffer."
1063   (save-excursion
1064     (let ((b (point-min)))
1065       (while (setq b (text-property-any b (point-max) 'article-type type))
1066         (delete-region
1067          b (or (text-property-not-all b (point-max) 'article-type type)
1068                (point-max)))))))
1069
1070 (defun gnus-article-delete-invisible-text ()
1071   "Delete all invisible text in the current buffer."
1072   (save-excursion
1073     (let ((b (point-min)))
1074       (while (setq b (text-property-any b (point-max) 'invisible t))
1075         (delete-region
1076          b (or (text-property-not-all b (point-max) 'invisible t)
1077                (point-max)))))))
1078
1079 (defun gnus-article-text-type-exists-p (type)
1080   "Say whether any text of type TYPE exists in the buffer."
1081   (text-property-any (point-min) (point-max) 'article-type type))
1082
1083 (defsubst gnus-article-header-rank ()
1084   "Give the rank of the string HEADER as given by `gnus-sorted-header-list'."
1085   (let ((list gnus-sorted-header-list)
1086         (i 0))
1087     (while list
1088       (when (looking-at (car list))
1089         (setq list nil))
1090       (setq list (cdr list))
1091       (incf i))
1092     i))
1093
1094 (defun article-hide-headers (&optional arg delete)
1095   "Hide unwanted headers and possibly sort them as well."
1096   (interactive)
1097   ;; This function might be inhibited.
1098   (unless gnus-inhibit-hiding
1099     (save-excursion
1100       (save-restriction
1101         (let ((buffer-read-only nil)
1102               (case-fold-search t)
1103               (max (1+ (length gnus-sorted-header-list)))
1104               (ignored (when (not gnus-visible-headers)
1105                          (cond ((stringp gnus-ignored-headers)
1106                                 gnus-ignored-headers)
1107                                ((listp gnus-ignored-headers)
1108                                 (mapconcat 'identity gnus-ignored-headers
1109                                            "\\|")))))
1110               (visible
1111                (cond ((stringp gnus-visible-headers)
1112                       gnus-visible-headers)
1113                      ((and gnus-visible-headers
1114                            (listp gnus-visible-headers))
1115                       (mapconcat 'identity gnus-visible-headers "\\|"))))
1116               (inhibit-point-motion-hooks t)
1117               beg)
1118           ;; First we narrow to just the headers.
1119           (article-narrow-to-head)
1120           ;; Hide any "From " lines at the beginning of (mail) articles.
1121           (while (looking-at "From ")
1122             (forward-line 1))
1123           (unless (bobp)
1124             (delete-region (point-min) (point)))
1125           ;; Then treat the rest of the header lines.
1126           ;; Then we use the two regular expressions
1127           ;; `gnus-ignored-headers' and `gnus-visible-headers' to
1128           ;; select which header lines is to remain visible in the
1129           ;; article buffer.
1130           (while (re-search-forward "^[^ \t]*:" nil t)
1131             (beginning-of-line)
1132             ;; Mark the rank of the header.
1133             (put-text-property
1134              (point) (1+ (point)) 'message-rank
1135              (if (or (and visible (looking-at visible))
1136                      (and ignored
1137                           (not (looking-at ignored))))
1138                  (gnus-article-header-rank)
1139                (+ 2 max)))
1140             (forward-line 1))
1141           (message-sort-headers-1)
1142           (when (setq beg (text-property-any
1143                            (point-min) (point-max) 'message-rank (+ 2 max)))
1144             ;; We delete the unwanted headers.
1145             (push 'headers gnus-article-wash-types)
1146             (add-text-properties (point-min) (+ 5 (point-min))
1147                                  '(article-type headers dummy-invisible t))
1148             (delete-region beg (point-max))))))))
1149
1150 (defun article-hide-boring-headers (&optional arg)
1151   "Toggle hiding of headers that aren't very interesting.
1152 If given a negative prefix, always show; if given a positive prefix,
1153 always hide."
1154   (interactive (gnus-article-hidden-arg))
1155   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
1156              (not gnus-show-all-headers))
1157     (save-excursion
1158       (save-restriction
1159         (let ((buffer-read-only nil)
1160               (list gnus-boring-article-headers)
1161               (inhibit-point-motion-hooks t)
1162               elem)
1163           (article-narrow-to-head)
1164           (while list
1165             (setq elem (pop list))
1166             (goto-char (point-min))
1167             (cond
1168              ;; Hide empty headers.
1169              ((eq elem 'empty)
1170               (while (re-search-forward "^[^: \t]+:[ \t]*\n[^ \t]" nil t)
1171                 (forward-line -1)
1172                 (gnus-article-hide-text-type
1173                  (progn (beginning-of-line) (point))
1174                  (progn
1175                    (end-of-line)
1176                    (if (re-search-forward "^[^ \t]" nil t)
1177                        (match-beginning 0)
1178                      (point-max)))
1179                  'boring-headers)))
1180              ;; Hide boring Newsgroups header.
1181              ((eq elem 'newsgroups)
1182               (when (equal (gnus-fetch-field "newsgroups")
1183                            (gnus-group-real-name
1184                             (if (boundp 'gnus-newsgroup-name)
1185                                 gnus-newsgroup-name
1186                               "")))
1187                 (gnus-article-hide-header "newsgroups")))
1188              ((eq elem 'followup-to)
1189               (when (equal (message-fetch-field "followup-to")
1190                            (message-fetch-field "newsgroups"))
1191                 (gnus-article-hide-header "followup-to")))
1192              ((eq elem 'reply-to)
1193               (let ((from (message-fetch-field "from"))
1194                     (reply-to (message-fetch-field "reply-to")))
1195                 (when (and
1196                        from reply-to
1197                        (ignore-errors
1198                          (equal
1199                           (nth 1 (mail-extract-address-components from))
1200                           (nth 1 (mail-extract-address-components reply-to)))))
1201                   (gnus-article-hide-header "reply-to"))))
1202              ((eq elem 'date)
1203               (let ((date (message-fetch-field "date")))
1204                 (when (and date
1205                            (< (days-between (current-time-string) date)
1206                               4))
1207                   (gnus-article-hide-header "date"))))
1208              ((eq elem 'long-to)
1209               (let ((to (message-fetch-field "to"))
1210                     (cc (message-fetch-field "cc")))
1211                 (when (> (length to) 1024)
1212                   (gnus-article-hide-header "to"))
1213                 (when (> (length cc) 1024)
1214                   (gnus-article-hide-header "cc"))))
1215              ((eq elem 'many-to)
1216               (let ((to-count 0)
1217                     (cc-count 0))
1218                 (goto-char (point-min))
1219                 (while (re-search-forward "^to:" nil t)
1220                   (setq to-count (1+ to-count)))
1221                 (when (> to-count 1)
1222                   (while (> to-count 0)
1223                     (goto-char (point-min))
1224                     (save-restriction
1225                       (re-search-forward "^to:" nil nil to-count)
1226                       (forward-line -1)
1227                       (narrow-to-region (point) (point-max))
1228                       (gnus-article-hide-header "to"))
1229                     (setq to-count (1- to-count))))
1230                 (goto-char (point-min))
1231                 (while (re-search-forward "^cc:" nil t)
1232                   (setq cc-count (1+ cc-count)))
1233                 (when (> cc-count 1)
1234                   (while (> cc-count 0)
1235                     (goto-char (point-min))
1236                     (save-restriction
1237                       (re-search-forward "^cc:" nil nil cc-count)
1238                       (forward-line -1)
1239                       (narrow-to-region (point) (point-max))
1240                       (gnus-article-hide-header "cc"))
1241                     (setq cc-count (1- cc-count)))))))))))))
1242
1243 (defun gnus-article-hide-header (header)
1244   (save-excursion
1245     (goto-char (point-min))
1246     (when (re-search-forward (concat "^" header ":") nil t)
1247       (gnus-article-hide-text-type
1248        (progn (beginning-of-line) (point))
1249        (progn
1250          (end-of-line)
1251          (if (re-search-forward "^[^ \t]" nil t)
1252              (match-beginning 0)
1253            (point-max)))
1254        'boring-headers))))
1255
1256 (defvar gnus-article-normalized-header-length 40
1257   "Length of normalized headers.")
1258
1259 (defun article-normalize-headers ()
1260   "Make all header lines 40 characters long."
1261   (interactive)
1262   (let ((buffer-read-only nil)
1263         column)
1264     (save-excursion
1265       (save-restriction
1266         (article-narrow-to-head)
1267         (while (not (eobp))
1268           (cond
1269            ((< (setq column (- (gnus-point-at-eol) (point)))
1270                gnus-article-normalized-header-length)
1271             (end-of-line)
1272             (insert (make-string
1273                      (- gnus-article-normalized-header-length column)
1274                      ? )))
1275            ((> column gnus-article-normalized-header-length)
1276             (gnus-put-text-property
1277              (progn
1278                (forward-char gnus-article-normalized-header-length)
1279                (point))
1280              (gnus-point-at-eol)
1281              'invisible t))
1282            (t
1283             ;; Do nothing.
1284             ))
1285           (forward-line 1))))))
1286
1287 (defun article-treat-dumbquotes ()
1288   "Translate M******** sm*rtq**t*s into proper text.
1289 Note that this function guesses whether a character is a sm*rtq**t* or
1290 not, so it should only be used interactively."
1291   (interactive)
1292   (article-translate-strings gnus-article-dumbquotes-map))
1293
1294 (defun article-translate-characters (from to)
1295   "Translate all characters in the body of the article according to FROM and TO.
1296 FROM is a string of characters to translate from; to is a string of
1297 characters to translate to."
1298   (save-excursion
1299     (when (article-goto-body)
1300       (let ((buffer-read-only nil)
1301             (x (make-string 225 ?x))
1302             (i -1))
1303         (while (< (incf i) (length x))
1304           (aset x i i))
1305         (setq i 0)
1306         (while (< i (length from))
1307           (aset x (aref from i) (aref to i))
1308           (incf i))
1309         (translate-region (point) (point-max) x)))))
1310
1311 (defun article-translate-strings (map)
1312   "Translate all string in the body of the article according to MAP.
1313 MAP is an alist where the elements are on the form (\"from\" \"to\")."
1314   (save-excursion
1315     (when (article-goto-body)
1316       (let ((buffer-read-only nil)
1317             elem)
1318         (while (setq elem (pop map))
1319           (save-excursion
1320             (while (search-forward (car elem) nil t)
1321               (replace-match (cadr elem)))))))))
1322
1323 (defun article-treat-overstrike ()
1324   "Translate overstrikes into bold text."
1325   (interactive)
1326   (save-excursion
1327     (when (article-goto-body)
1328       (let ((buffer-read-only nil))
1329         (while (search-forward "\b" nil t)
1330           (let ((next (char-after))
1331                 (previous (char-after (- (point) 2))))
1332             ;; We do the boldification/underlining by hiding the
1333             ;; overstrikes and putting the proper text property
1334             ;; on the letters.
1335             (cond
1336              ((eq next previous)
1337               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
1338               (put-text-property (point) (1+ (point)) 'face 'bold))
1339              ((eq next ?_)
1340               (gnus-article-hide-text-type
1341                (1- (point)) (1+ (point)) 'overstrike)
1342               (put-text-property
1343                (- (point) 2) (1- (point)) 'face 'underline))
1344              ((eq previous ?_)
1345               (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
1346               (put-text-property
1347                (point) (1+ (point)) 'face 'underline)))))))))
1348
1349 (defun article-fill-long-lines ()
1350   "Fill lines that are wider than the window width."
1351   (interactive)
1352   (save-excursion
1353     (let ((buffer-read-only nil)
1354           (width (window-width (get-buffer-window (current-buffer)))))
1355       (save-restriction
1356         (article-goto-body)
1357         (let ((adaptive-fill-mode nil))
1358           (while (not (eobp))
1359             (end-of-line)
1360             (when (>= (current-column) (min fill-column width))
1361               (narrow-to-region (point) (gnus-point-at-bol))
1362               (fill-paragraph nil)
1363               (goto-char (point-max))
1364               (widen))
1365             (forward-line 1)))))))
1366
1367 (defun article-capitalize-sentences ()
1368   "Capitalize the first word in each sentence."
1369   (interactive)
1370   (save-excursion
1371     (let ((buffer-read-only nil)
1372           (paragraph-start "^[\n\^L]"))
1373       (article-goto-body)
1374       (while (not (eobp))
1375         (capitalize-word 1)
1376         (forward-sentence)))))
1377
1378 (defun article-remove-cr ()
1379   "Remove trailing CRs and then translate remaining CRs into LFs."
1380   (interactive)
1381   (save-excursion
1382     (let ((buffer-read-only nil))
1383       (goto-char (point-min))
1384       (while (re-search-forward "\r+$" nil t)
1385         (replace-match "" t t))
1386       (goto-char (point-min))
1387       (while (search-forward "\r" nil t)
1388         (replace-match "\n" t t)))))
1389
1390 (defun article-remove-trailing-blank-lines ()
1391   "Remove all trailing blank lines from the article."
1392   (interactive)
1393   (save-excursion
1394     (let ((buffer-read-only nil))
1395       (goto-char (point-max))
1396       (delete-region
1397        (point)
1398        (progn
1399          (while (and (not (bobp))
1400                      (looking-at "^[ \t]*$")
1401                      (not (gnus-annotation-in-region-p
1402                            (point) (gnus-point-at-eol))))
1403            (forward-line -1))
1404          (forward-line 1)
1405          (point))))))
1406
1407 (defun article-display-x-face (&optional force)
1408   "Look for an X-Face header and display it if present."
1409   (interactive (list 'force))
1410   (save-excursion
1411     ;; Delete the old process, if any.
1412     (when (process-status "article-x-face")
1413       (delete-process "article-x-face"))
1414     (let ((inhibit-point-motion-hooks t)
1415           (case-fold-search t)
1416           from last)
1417       (save-restriction
1418         (article-narrow-to-head)
1419         (goto-char (point-min))
1420         (setq from (message-fetch-field "from"))
1421         (goto-char (point-min))
1422         (while (and gnus-article-x-face-command
1423                     (not last)
1424                     (or force
1425                         ;; Check whether this face is censored.
1426                         (not gnus-article-x-face-too-ugly)
1427                         (and gnus-article-x-face-too-ugly from
1428                              (not (string-match gnus-article-x-face-too-ugly
1429                                                 from))))
1430                     ;; Has to be present.
1431                     (re-search-forward "^X-Face: " nil t))
1432           ;; This used to try to do multiple faces (`while' instead of
1433           ;; `when' above), but (a) sending multiple EOFs to xv doesn't
1434           ;; work (b) it can crash some versions of Emacs (c) are
1435           ;; multiple faces really something to encourage?
1436           (when (stringp gnus-article-x-face-command)
1437             (setq last t))
1438           ;; We now have the area of the buffer where the X-Face is stored.
1439           (save-excursion
1440             (let ((beg (point))
1441                   (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
1442               ;; We display the face.
1443               (if (symbolp gnus-article-x-face-command)
1444                   ;; The command is a lisp function, so we call it.
1445                   (if (gnus-functionp gnus-article-x-face-command)
1446                       (funcall gnus-article-x-face-command beg end)
1447                     (error "%s is not a function" gnus-article-x-face-command))
1448                 ;; The command is a string, so we interpret the command
1449                 ;; as a, well, command, and fork it off.
1450                 (let ((process-connection-type nil))
1451                   (process-kill-without-query
1452                    (start-process
1453                     "article-x-face" nil shell-file-name shell-command-switch
1454                     gnus-article-x-face-command))
1455                   (process-send-region "article-x-face" beg end)
1456                   (process-send-eof "article-x-face"))))))))))
1457
1458 (defun article-decode-mime-words ()
1459   "Decode all MIME-encoded words in the article."
1460   (interactive)
1461   (save-excursion
1462     (set-buffer gnus-article-buffer)
1463     (let ((inhibit-point-motion-hooks t)
1464           buffer-read-only
1465           (mail-parse-charset gnus-newsgroup-charset)
1466           (mail-parse-ignored-charsets 
1467            (save-excursion (set-buffer gnus-summary-buffer)
1468                            gnus-newsgroup-ignored-charsets)))
1469       (mail-decode-encoded-word-region (point-min) (point-max)))))
1470
1471 (defun article-decode-charset (&optional prompt)
1472   "Decode charset-encoded text in the article.
1473 If PROMPT (the prefix), prompt for a coding system to use."
1474   (interactive "P")
1475   (let ((inhibit-point-motion-hooks t) (case-fold-search t)
1476         buffer-read-only
1477         (mail-parse-charset gnus-newsgroup-charset)
1478         (mail-parse-ignored-charsets 
1479          (save-excursion (condition-case nil
1480                              (set-buffer gnus-summary-buffer)
1481                            (error))
1482                          gnus-newsgroup-ignored-charsets))
1483         ct cte ctl charset format)
1484   (save-excursion
1485     (save-restriction
1486       (article-narrow-to-head)
1487       (setq ct (message-fetch-field "Content-Type" t)
1488             cte (message-fetch-field "Content-Transfer-Encoding" t)
1489             ctl (and ct (ignore-errors
1490                           (mail-header-parse-content-type ct)))
1491             charset (cond
1492                      (prompt
1493                       (mm-read-coding-system "Charset to decode: "))
1494                      (ctl
1495                       (mail-content-type-get ctl 'charset)))
1496             format (and ctl (mail-content-type-get ctl 'format)))
1497       (when cte
1498         (setq cte (mail-header-strip cte)))
1499       (if (and ctl (not (string-match "/" (car ctl)))) 
1500           (setq ctl nil))
1501       (goto-char (point-max)))
1502     (forward-line 1)
1503     (save-restriction
1504       (narrow-to-region (point) (point-max))
1505       (when (and (eq mail-parse-charset 'gnus-decoded)
1506                  (eq (mm-body-7-or-8) '8bit))
1507         ;; The text code could have been decoded.
1508         (setq charset mail-parse-charset))
1509       (when (and (or (not ctl)
1510                      (equal (car ctl) "text/plain"))
1511                  (not format)) ;; article with format will decode later.
1512         (mm-decode-body
1513          charset (and cte (intern (downcase
1514                                    (gnus-strip-whitespace cte))))
1515          (car ctl)))))))
1516
1517 (defun article-decode-encoded-words ()
1518   "Remove encoded-word encoding from headers."
1519   (let ((inhibit-point-motion-hooks t)
1520         (mail-parse-charset gnus-newsgroup-charset)
1521         (mail-parse-ignored-charsets 
1522          (save-excursion (condition-case nil
1523                              (set-buffer gnus-summary-buffer)
1524                            (error))
1525                          gnus-newsgroup-ignored-charsets))
1526         buffer-read-only)
1527     (save-restriction
1528       (article-narrow-to-head)
1529       (funcall gnus-decode-header-function (point-min) (point-max)))))
1530
1531 (defun article-de-quoted-unreadable (&optional force)
1532   "Translate a quoted-printable-encoded article.
1533 If FORCE, decode the article whether it is marked as quoted-printable
1534 or not."
1535   (interactive (list 'force))
1536   (save-excursion
1537     (let ((buffer-read-only nil) type charset)
1538       (if (gnus-buffer-live-p gnus-original-article-buffer)
1539           (with-current-buffer gnus-original-article-buffer
1540             (setq type
1541                   (gnus-fetch-field "content-transfer-encoding"))
1542             (let* ((ct (gnus-fetch-field "content-type"))
1543                    (ctl (and ct 
1544                              (ignore-errors
1545                                (mail-header-parse-content-type ct)))))
1546               (setq charset (and ctl
1547                                  (mail-content-type-get ctl 'charset)))
1548               (if (stringp charset)
1549                   (setq charset (intern (downcase charset)))))))
1550       (unless charset 
1551         (setq charset gnus-newsgroup-charset))
1552       (when (or force
1553                 (and type (string-match "quoted-printable" (downcase type))))
1554         (article-goto-body)
1555         (quoted-printable-decode-region (point) (point-max) charset)))))
1556
1557 (defun article-de-base64-unreadable (&optional force)
1558   "Translate a base64 article.
1559 If FORCE, decode the article whether it is marked as base64 not."
1560   (interactive (list 'force))
1561   (save-excursion
1562     (let ((buffer-read-only nil) type charset)
1563       (if (gnus-buffer-live-p gnus-original-article-buffer)
1564           (with-current-buffer gnus-original-article-buffer
1565             (setq type
1566                   (gnus-fetch-field "content-transfer-encoding"))
1567             (let* ((ct (gnus-fetch-field "content-type"))
1568                    (ctl (and ct 
1569                              (ignore-errors
1570                                (mail-header-parse-content-type ct)))))
1571               (setq charset (and ctl
1572                                  (mail-content-type-get ctl 'charset)))
1573               (if (stringp charset)
1574                   (setq charset (intern (downcase charset)))))))
1575       (unless charset 
1576         (setq charset gnus-newsgroup-charset))
1577       (when (or force
1578                 (and type (string-match "base64" (downcase type))))
1579         (article-goto-body)
1580         (save-restriction
1581           (narrow-to-region (point) (point-max))
1582           (base64-decode-region (point-min) (point-max))
1583           (if (mm-coding-system-p charset)
1584               (mm-decode-coding-region (point-min) (point-max) charset)))))))
1585
1586 (eval-when-compile
1587   (require 'rfc1843))
1588
1589 (defun article-decode-HZ ()
1590   "Translate a HZ-encoded article."
1591   (interactive)
1592   (require 'rfc1843)
1593   (save-excursion
1594     (let ((buffer-read-only nil))
1595       (rfc1843-decode-region (point-min) (point-max)))))
1596
1597 (defun article-wash-html ()
1598   "Format an html article."
1599   (interactive)
1600   (save-excursion
1601     (let ((buffer-read-only nil)
1602           charset)
1603       (if (gnus-buffer-live-p gnus-original-article-buffer)
1604           (with-current-buffer gnus-original-article-buffer
1605             (let* ((ct (gnus-fetch-field "content-type"))
1606                    (ctl (and ct 
1607                              (ignore-errors
1608                                (mail-header-parse-content-type ct)))))
1609               (setq charset (and ctl
1610                                  (mail-content-type-get ctl 'charset)))
1611               (if (stringp charset)
1612                   (setq charset (intern (downcase charset)))))))
1613       (unless charset 
1614         (setq charset gnus-newsgroup-charset))
1615       (article-goto-body)
1616       (save-window-excursion
1617         (save-restriction
1618           (narrow-to-region (point) (point-max))
1619           (mm-setup-w3)
1620           (let ((w3-strict-width (window-width))
1621                 (url-standalone-mode t))
1622             (condition-case var
1623                 (w3-region (point-min) (point-max))
1624               (error))))))))
1625
1626 (defun article-hide-list-identifiers ()
1627   "Remove list identifies from the Subject header.
1628 The `gnus-list-identifiers' variable specifies what to do."
1629   (interactive)
1630   (save-excursion
1631     (save-restriction
1632       (let ((inhibit-point-motion-hooks t)
1633             buffer-read-only)
1634         (article-narrow-to-head)
1635         (let ((regexp (if (stringp gnus-list-identifiers) gnus-list-identifiers
1636                         (mapconcat 'identity gnus-list-identifiers " *\\|"))))
1637           (when regexp
1638             (goto-char (point-min))
1639             (when (re-search-forward
1640                    (concat "^Subject: +\\(\\(\\(Re: +\\)?\\(" regexp 
1641                            " *\\)\\)+\\(Re: +\\)?\\)")
1642                    nil t)
1643               (let ((s (or (match-string 3) (match-string 5))))
1644                 (delete-region (match-beginning 1) (match-end 1))
1645                 (when s
1646                   (goto-char (match-beginning 1))
1647                   (insert s))))))))))
1648
1649 (defun article-hide-pgp ()
1650   "Remove any PGP headers and signatures in the current article."
1651   (interactive)
1652   (save-excursion
1653     (save-restriction
1654       (let ((inhibit-point-motion-hooks t)
1655             buffer-read-only beg end)
1656         (article-goto-body)
1657         ;; Hide the "header".
1658         (when (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
1659           (push 'pgp gnus-article-wash-types)
1660           (delete-region (match-beginning 0) (match-end 0))
1661           ;; Remove armor headers (rfc2440 6.2)
1662           (delete-region (point) (or (re-search-forward "^[ \t]*\n" nil t)
1663                                      (point)))
1664           (setq beg (point))
1665           ;; Hide the actual signature.
1666           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
1667                (setq end (1+ (match-beginning 0)))
1668                (delete-region
1669                 end
1670                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
1671                     (match-end 0)
1672                   ;; Perhaps we shouldn't hide to the end of the buffer
1673                   ;; if there is no end to the signature?
1674                   (point-max))))
1675           ;; Hide "- " PGP quotation markers.
1676           (when (and beg end)
1677             (narrow-to-region beg end)
1678             (goto-char (point-min))
1679             (while (re-search-forward "^- " nil t)
1680               (delete-region
1681                (match-beginning 0) (match-end 0)))
1682             (widen))
1683           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
1684
1685 (defun article-hide-pem (&optional arg)
1686   "Toggle hiding of any PEM headers and signatures in the current article.
1687 If given a negative prefix, always show; if given a positive prefix,
1688 always hide."
1689   (interactive (gnus-article-hidden-arg))
1690   (unless (gnus-article-check-hidden-text 'pem arg)
1691     (save-excursion
1692       (let (buffer-read-only end)
1693         (goto-char (point-min))
1694         ;; Hide the horrendously ugly "header".
1695         (when (and (search-forward
1696                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
1697                     nil t)
1698                    (setq end (1+ (match-beginning 0))))
1699           (push 'pem gnus-article-wash-types)
1700           (gnus-article-hide-text-type
1701            end
1702            (if (search-forward "\n\n" nil t)
1703                (match-end 0)
1704              (point-max))
1705            'pem)
1706           ;; Hide the trailer as well
1707           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
1708                                 nil t)
1709             (gnus-article-hide-text-type
1710              (match-beginning 0) (match-end 0) 'pem)))))))
1711
1712 (defun article-strip-banner ()
1713   "Strip the banner specified by the `banner' group parameter."
1714   (interactive)
1715   (save-excursion
1716     (save-restriction
1717       (let ((inhibit-point-motion-hooks t)
1718             (banner (gnus-group-find-parameter gnus-newsgroup-name 'banner))
1719             (gnus-signature-limit nil)
1720             buffer-read-only beg end)
1721         (when banner
1722           (article-goto-body)
1723           (cond
1724            ((eq banner 'signature)
1725             (when (gnus-article-narrow-to-signature)
1726               (widen)
1727               (forward-line -1)
1728               (delete-region (point) (point-max))))
1729            ((symbolp banner)
1730             (if (setq banner (cdr (assq banner gnus-article-banner-alist)))
1731                 (while (re-search-forward banner nil t)
1732                   (delete-region (match-beginning 0) (match-end 0)))))
1733            ((stringp banner)
1734             (while (re-search-forward banner nil t)
1735               (delete-region (match-beginning 0) (match-end 0))))))))))
1736
1737 (defun article-babel ()
1738   "Translate article using an online translation service."
1739   (interactive)
1740   (require 'babel)
1741   (save-excursion
1742     (set-buffer gnus-article-buffer)
1743     (when (article-goto-body)
1744       (let* ((buffer-read-only nil)
1745              (start (point))
1746              (end (point-max))
1747              (orig (buffer-substring start end))
1748              (trans (babel-as-string orig)))
1749         (save-restriction
1750           (narrow-to-region start end)
1751           (delete-region start end)
1752           (insert trans))))))
1753
1754 (defun article-hide-signature (&optional arg)
1755   "Hide the signature in the current article.
1756 If given a negative prefix, always show; if given a positive prefix,
1757 always hide."
1758   (interactive (gnus-article-hidden-arg))
1759   (unless (gnus-article-check-hidden-text 'signature arg)
1760     (save-excursion
1761       (save-restriction
1762         (let ((buffer-read-only nil))
1763           (when (gnus-article-narrow-to-signature)
1764             (gnus-article-hide-text-type
1765              (point-min) (point-max) 'signature)))))))
1766
1767 (defun article-strip-headers-in-body ()
1768   "Strip offensive headers from bodies."
1769   (interactive)
1770   (save-excursion
1771     (article-goto-body)
1772     (let ((case-fold-search t))
1773       (when (looking-at "x-no-archive:")
1774         (gnus-delete-line)))))
1775
1776 (defun article-strip-leading-blank-lines ()
1777   "Remove all blank lines from the beginning of the article."
1778   (interactive)
1779   (save-excursion
1780     (let ((inhibit-point-motion-hooks t)
1781           buffer-read-only)
1782       (when (article-goto-body)
1783         (while (and (not (eobp))
1784                     (looking-at "[ \t]*$"))
1785           (gnus-delete-line))))))
1786
1787 (defun article-narrow-to-head ()
1788   "Narrow the buffer to the head of the message.
1789 Point is left at the beginning of the narrowed-to region."
1790   (narrow-to-region
1791    (goto-char (point-min))
1792    (if (search-forward "\n\n" nil 1)
1793        (1- (point))
1794      (point-max)))
1795   (goto-char (point-min)))
1796
1797 (defun article-goto-body ()
1798   "Place point at the start of the body."
1799   (goto-char (point-min))
1800   (cond
1801    ;; This variable is only bound when dealing with separate
1802    ;; MIME body parts.
1803    (article-goto-body-goes-to-point-min-p
1804     t)
1805    ((search-forward "\n\n" nil t)
1806     t)
1807    (t
1808     (goto-char (point-max))
1809     nil)))
1810
1811 (defun article-strip-multiple-blank-lines ()
1812   "Replace consecutive blank lines with one empty line."
1813   (interactive)
1814   (save-excursion
1815     (let ((inhibit-point-motion-hooks t)
1816           buffer-read-only)
1817       ;; First make all blank lines empty.
1818       (article-goto-body)
1819       (while (re-search-forward "^[ \t]+$" nil t)
1820         (unless (gnus-annotation-in-region-p
1821                  (match-beginning 0) (match-end 0))
1822           (replace-match "" nil t)))
1823       ;; Then replace multiple empty lines with a single empty line.
1824       (article-goto-body)
1825       (while (re-search-forward "\n\n\n+" nil t)
1826         (unless (gnus-annotation-in-region-p
1827                  (match-beginning 0) (match-end 0))
1828           (replace-match "\n\n" t t))))))
1829
1830 (defun article-strip-leading-space ()
1831   "Remove all white space from the beginning of the lines in the article."
1832   (interactive)
1833   (save-excursion
1834     (let ((inhibit-point-motion-hooks t)
1835           buffer-read-only)
1836       (article-goto-body)
1837       (while (re-search-forward "^[ \t]+" nil t)
1838         (replace-match "" t t)))))
1839
1840 (defun article-strip-trailing-space ()
1841   "Remove all white space from the end of the lines in the article."
1842   (interactive)
1843   (save-excursion
1844     (let ((inhibit-point-motion-hooks t)
1845           buffer-read-only)
1846       (article-goto-body)
1847       (while (re-search-forward "[ \t]+$" nil t)
1848         (replace-match "" t t)))))
1849
1850 (defun article-strip-blank-lines ()
1851   "Strip leading, trailing and multiple blank lines."
1852   (interactive)
1853   (article-strip-leading-blank-lines)
1854   (article-remove-trailing-blank-lines)
1855   (article-strip-multiple-blank-lines))
1856
1857 (defun article-strip-all-blank-lines ()
1858   "Strip all blank lines."
1859   (interactive)
1860   (save-excursion
1861     (let ((inhibit-point-motion-hooks t)
1862           buffer-read-only)
1863       (article-goto-body)
1864       (while (re-search-forward "^[ \t]*\n" nil t)
1865         (replace-match "" t t)))))
1866
1867 (defun gnus-article-narrow-to-signature ()
1868   "Narrow to the signature; return t if a signature is found, else nil."
1869   (let ((inhibit-point-motion-hooks t))
1870     (when (gnus-article-search-signature)
1871       (forward-line 1)
1872       ;; Check whether we have some limits to what we consider
1873       ;; to be a signature.
1874       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
1875                       (list gnus-signature-limit)))
1876             limit limited)
1877         (while (setq limit (pop limits))
1878           (if (or (and (integerp limit)
1879                        (< (- (point-max) (point)) limit))
1880                   (and (floatp limit)
1881                        (< (count-lines (point) (point-max)) limit))
1882                   (and (gnus-functionp limit)
1883                        (funcall limit))
1884                   (and (stringp limit)
1885                        (not (re-search-forward limit nil t))))
1886               ()                        ; This limit did not succeed.
1887             (setq limited t
1888                   limits nil)))
1889         (unless limited
1890           (narrow-to-region (point) (point-max))
1891           t)))))
1892
1893 (defun gnus-article-search-signature ()
1894   "Search the current buffer for the signature separator.
1895 Put point at the beginning of the signature separator."
1896   (let ((cur (point)))
1897     (goto-char (point-max))
1898     (if (if (stringp gnus-signature-separator)
1899             (re-search-backward gnus-signature-separator nil t)
1900           (let ((seps gnus-signature-separator))
1901             (while (and seps
1902                         (not (re-search-backward (car seps) nil t)))
1903               (pop seps))
1904             seps))
1905         t
1906       (goto-char cur)
1907       nil)))
1908
1909 (defun gnus-article-hidden-arg ()
1910   "Return the current prefix arg as a number, or 0 if no prefix."
1911   (list (if current-prefix-arg
1912             (prefix-numeric-value current-prefix-arg)
1913           0)))
1914
1915 (defun gnus-article-check-hidden-text (type arg)
1916   "Return nil if hiding is necessary.
1917 Arg can be nil or a number.  Nil and positive means hide, negative
1918 means show, 0 means toggle."
1919   (save-excursion
1920     (save-restriction
1921       (let ((hide (gnus-article-hidden-text-p type)))
1922         (cond
1923          ((or (null arg)
1924               (> arg 0))
1925           nil)
1926          ((< arg 0)
1927           (gnus-article-show-hidden-text type))
1928          (t
1929           (if (eq hide 'hidden)
1930               (gnus-article-show-hidden-text type)
1931             nil)))))))
1932
1933 (defun gnus-article-hidden-text-p (type)
1934   "Say whether the current buffer contains hidden text of type TYPE."
1935   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
1936     (while (and pos
1937                 (not (get-text-property pos 'invisible))
1938                 (not (get-text-property pos 'dummy-invisible)))
1939       (setq pos
1940             (text-property-any (1+ pos) (point-max) 'article-type type)))
1941     (if pos
1942         'hidden
1943       nil)))
1944
1945 (defun gnus-article-show-hidden-text (type &optional hide)
1946   "Show all hidden text of type TYPE.
1947 If HIDE, hide the text instead."
1948   (save-excursion
1949     (let ((buffer-read-only nil)
1950           (inhibit-point-motion-hooks t)
1951           (end (point-min))
1952           beg)
1953       (while (setq beg (text-property-any end (point-max) 'article-type type))
1954         (goto-char beg)
1955         (setq end (or
1956                    (text-property-not-all beg (point-max) 'article-type type)
1957                    (point-max)))
1958         (if hide
1959             (gnus-article-hide-text beg end gnus-hidden-properties)
1960           (gnus-article-unhide-text beg end))
1961         (goto-char end))
1962       t)))
1963
1964 (defconst article-time-units
1965   `((year . ,(* 365.25 24 60 60))
1966     (week . ,(* 7 24 60 60))
1967     (day . ,(* 24 60 60))
1968     (hour . ,(* 60 60))
1969     (minute . 60)
1970     (second . 1))
1971   "Mapping from time units to seconds.")
1972
1973 (defun article-date-ut (&optional type highlight header)
1974   "Convert DATE date to universal time in the current article.
1975 If TYPE is `local', convert to local time; if it is `lapsed', output
1976 how much time has lapsed since DATE.  For `lapsed', the value of
1977 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
1978 should replace the \"Date:\" one, or should be added below it."
1979   (interactive (list 'ut t))
1980   (let* ((header (or header
1981                      (message-fetch-field "date")
1982                      ""))
1983          (tdate-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
1984          (date-regexp
1985           (cond
1986            ((not gnus-article-date-lapsed-new-header)
1987             tdate-regexp)
1988            ((eq type 'lapsed)
1989             "^X-Sent:[ \t]")
1990            (t
1991             "^Date:[ \t]")))
1992          (date (if (vectorp header) (mail-header-date header)
1993                  header))
1994          (inhibit-point-motion-hooks t)
1995          pos
1996          bface eface)
1997     (save-excursion
1998       (save-restriction
1999         (article-narrow-to-head)
2000         (when (re-search-forward tdate-regexp nil t)
2001           (setq bface (get-text-property (gnus-point-at-bol) 'face)
2002                 date (or (get-text-property (gnus-point-at-bol)
2003                                             'original-date)
2004                          date)
2005                 eface (get-text-property (1- (gnus-point-at-eol)) 'face))
2006           (forward-line 1))
2007         (when (and date (not (string= date "")))
2008           (goto-char (point-min))
2009           (let ((buffer-read-only nil))
2010             ;; Delete any old Date headers.
2011             (while (re-search-forward date-regexp nil t)
2012               (if pos
2013                   (delete-region (progn (beginning-of-line) (point))
2014                                  (progn (forward-line 1) (point)))
2015                 (delete-region (progn (beginning-of-line) (point))
2016                                (progn (end-of-line) (point)))
2017                 (setq pos (point))))
2018             (when (and (not pos) (re-search-forward tdate-regexp nil t))
2019               (forward-line 1))
2020             (if pos (goto-char pos))
2021             (insert (article-make-date-line date (or type 'ut)))
2022             (when (not pos)
2023               (insert "\n")
2024               (forward-line -1))
2025             ;; Do highlighting.
2026             (beginning-of-line)
2027             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
2028               (put-text-property (match-beginning 1) (1+ (match-end 1))
2029                                  'original-date date)
2030               (put-text-property (match-beginning 1) (1+ (match-end 1))
2031                                  'face bface)
2032               (put-text-property (match-beginning 2) (match-end 2)
2033                                  'face eface))))))))
2034
2035 (defun article-make-date-line (date type)
2036   "Return a DATE line of TYPE."
2037   (let ((time (condition-case ()
2038                   (date-to-time date)
2039                 (error '(0 0)))))
2040     (cond
2041      ;; Convert to the local timezone.  We have to slap a
2042      ;; `condition-case' round the calls to the timezone
2043      ;; functions since they aren't particularly resistant to
2044      ;; buggy dates.
2045      ((eq type 'local)
2046       (let ((tz (car (current-time-zone time))))
2047         (format "Date: %s %s%02d%02d" (current-time-string time)
2048                 (if (> tz 0) "+" "-") (/ (abs tz) 3600) 
2049                 (/ (% (abs tz) 3600) 60))))
2050      ;; Convert to Universal Time.
2051      ((eq type 'ut)
2052       (concat "Date: "
2053               (current-time-string
2054                (let* ((e (parse-time-string date))
2055                       (tm (apply 'encode-time e))
2056                       (ms (car tm))
2057                       (ls (- (cadr tm) (car (current-time-zone time)))))
2058                  (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
2059                        ((> ls 65535) (list (1+ ms) (- ls 65536)))
2060                        (t (list ms ls)))))
2061               " UT"))
2062      ;; Get the original date from the article.
2063      ((eq type 'original)
2064       (concat "Date: " (if (string-match "\n+$" date)
2065                            (substring date 0 (match-beginning 0))
2066                          date)))
2067      ;; Let the user define the format.
2068      ((eq type 'user)
2069       (if (gnus-functionp gnus-article-time-format)
2070           (funcall gnus-article-time-format time)
2071         (concat
2072          "Date: "
2073          (format-time-string gnus-article-time-format time))))
2074      ;; ISO 8601.
2075      ((eq type 'iso8601)
2076       (let ((tz (car (current-time-zone time))))
2077         (concat
2078          "Date: "
2079          (format-time-string "%Y%m%dT%H%M%S" time)
2080          (format "%s%02d%02d"
2081                  (if (> tz 0) "+" "-") (/ (abs tz) 3600) 
2082                  (/ (% (abs tz) 3600) 60)))))
2083      ;; Do an X-Sent lapsed format.
2084      ((eq type 'lapsed)
2085       ;; If the date is seriously mangled, the timezone functions are
2086       ;; liable to bug out, so we ignore all errors.
2087       (let* ((now (current-time))
2088              (real-time (subtract-time now time))
2089              (real-sec (and real-time
2090                             (+ (* (float (car real-time)) 65536)
2091                                (cadr real-time))))
2092              (sec (and real-time (abs real-sec)))
2093              num prev)
2094         (cond
2095          ((null real-time)
2096           "X-Sent: Unknown")
2097          ((zerop sec)
2098           "X-Sent: Now")
2099          (t
2100           (concat
2101            "X-Sent: "
2102            ;; This is a bit convoluted, but basically we go
2103            ;; through the time units for years, weeks, etc,
2104            ;; and divide things to see whether that results
2105            ;; in positive answers.
2106            (mapconcat
2107             (lambda (unit)
2108               (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
2109                   ;; The (remaining) seconds are too few to
2110                   ;; be divided into this time unit.
2111                   ""
2112                 ;; It's big enough, so we output it.
2113                 (setq sec (- sec (* num (cdr unit))))
2114                 (prog1
2115                     (concat (if prev ", " "") (int-to-string
2116                                                (floor num))
2117                             " " (symbol-name (car unit))
2118                             (if (> num 1) "s" ""))
2119                   (setq prev t))))
2120             article-time-units "")
2121            ;; If dates are odd, then it might appear like the
2122            ;; article was sent in the future.
2123            (if (> real-sec 0)
2124                " ago"
2125              " in the future"))))))
2126      (t
2127       (error "Unknown conversion type: %s" type)))))
2128
2129 (defun article-date-local (&optional highlight)
2130   "Convert the current article date to the local timezone."
2131   (interactive (list t))
2132   (article-date-ut 'local highlight))
2133
2134 (defun article-date-original (&optional highlight)
2135   "Convert the current article date to what it was originally.
2136 This is only useful if you have used some other date conversion
2137 function and want to see what the date was before converting."
2138   (interactive (list t))
2139   (article-date-ut 'original highlight))
2140
2141 (defun article-date-lapsed (&optional highlight)
2142   "Convert the current article date to time lapsed since it was sent."
2143   (interactive (list t))
2144   (article-date-ut 'lapsed highlight))
2145
2146 (defun article-update-date-lapsed ()
2147   "Function to be run from a timer to update the lapsed time line."
2148   (let (deactivate-mark)
2149     (save-excursion
2150       (ignore-errors
2151         (walk-windows
2152          (lambda (w)
2153            (set-buffer (window-buffer w))
2154            (when (eq major-mode 'gnus-article-mode)
2155              (goto-char (point-min))
2156              (when (re-search-forward "^X-Sent:" nil t)
2157                (article-date-lapsed t))))
2158          nil 'visible)))))
2159
2160 (defun gnus-start-date-timer (&optional n)
2161   "Start a timer to update the X-Sent header in the article buffers.
2162 The numerical prefix says how frequently (in seconds) the function
2163 is to run."
2164   (interactive "p")
2165   (unless n
2166     (setq n 1))
2167   (gnus-stop-date-timer)
2168   (setq article-lapsed-timer
2169         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
2170
2171 (defun gnus-stop-date-timer ()
2172   "Stop the X-Sent timer."
2173   (interactive)
2174   (when article-lapsed-timer
2175     (nnheader-cancel-timer article-lapsed-timer)
2176     (setq article-lapsed-timer nil)))
2177
2178 (defun article-date-user (&optional highlight)
2179   "Convert the current article date to the user-defined format.
2180 This format is defined by the `gnus-article-time-format' variable."
2181   (interactive (list t))
2182   (article-date-ut 'user highlight))
2183
2184 (defun article-date-iso8601 (&optional highlight)
2185   "Convert the current article date to ISO8601."
2186   (interactive (list t))
2187   (article-date-ut 'iso8601 highlight))
2188
2189 (defun article-show-all ()
2190   "Show all hidden text in the article buffer."
2191   (interactive)
2192   (save-excursion
2193     (let ((buffer-read-only nil))
2194       (gnus-article-unhide-text (point-min) (point-max)))))
2195
2196 (defun article-emphasize (&optional arg)
2197   "Emphasize text according to `gnus-emphasis-alist'."
2198   (interactive (gnus-article-hidden-arg))
2199   (unless (gnus-article-check-hidden-text 'emphasis arg)
2200     (save-excursion
2201       (let ((alist (or 
2202                     (condition-case nil
2203                         (with-current-buffer gnus-summary-buffer 
2204                           gnus-article-emphasis-alist) 
2205                       (error))
2206                     gnus-emphasis-alist))
2207             (buffer-read-only nil)
2208             (props (append '(article-type emphasis)
2209                            gnus-hidden-properties))
2210             regexp elem beg invisible visible face)
2211         (article-goto-body)
2212         (setq beg (point))
2213         (while (setq elem (pop alist))
2214           (goto-char beg)
2215           (setq regexp (car elem)
2216                 invisible (nth 1 elem)
2217                 visible (nth 2 elem)
2218                 face (nth 3 elem))
2219           (while (re-search-forward regexp nil t)
2220             (when (and (match-beginning visible) (match-beginning invisible))
2221               (push 'emphasis gnus-article-wash-types)
2222               (gnus-article-hide-text
2223                (match-beginning invisible) (match-end invisible) props)
2224               (gnus-article-unhide-text-type
2225                (match-beginning visible) (match-end visible) 'emphasis)
2226               (gnus-put-text-property-excluding-newlines
2227                (match-beginning visible) (match-end visible) 'face face)
2228               (goto-char (match-end invisible)))))))))
2229
2230 (defun gnus-article-setup-highlight-words (&optional highlight-words)
2231   "Setup newsgroup emphasis alist."
2232   (unless gnus-article-emphasis-alist
2233     (let ((name (and gnus-newsgroup-name
2234                      (gnus-group-real-name gnus-newsgroup-name))))
2235       (make-local-variable 'gnus-article-emphasis-alist)
2236       (setq gnus-article-emphasis-alist 
2237             (nconc 
2238              (let ((alist gnus-group-highlight-words-alist) elem highlight)
2239                (while (setq elem (pop alist))
2240                  (when (and name (string-match (car elem) name))
2241                    (setq alist nil
2242                          highlight (copy-sequence (cdr elem)))))
2243                highlight)
2244              (copy-sequence highlight-words)
2245              (if gnus-newsgroup-name
2246                  (copy-sequence (gnus-group-find-parameter 
2247                                  gnus-newsgroup-name 'highlight-words t)))
2248              gnus-emphasis-alist)))))
2249
2250 (defvar gnus-summary-article-menu)
2251 (defvar gnus-summary-post-menu)
2252
2253 ;;; Saving functions.
2254
2255 (defun gnus-article-save (save-buffer file &optional num)
2256   "Save the currently selected article."
2257   (unless gnus-save-all-headers
2258     ;; Remove headers according to `gnus-saved-headers'.
2259     (let ((gnus-visible-headers
2260            (or gnus-saved-headers gnus-visible-headers))
2261           (gnus-article-buffer save-buffer))
2262       (save-excursion
2263         (set-buffer save-buffer)
2264         (article-hide-headers 1 t))))
2265   (save-window-excursion
2266     (if (not gnus-default-article-saver)
2267         (error "No default saver is defined")
2268       ;; !!! Magic!  The saving functions all save
2269       ;; `gnus-save-article-buffer' (or so they think), but we
2270       ;; bind that variable to our save-buffer.
2271       (set-buffer gnus-article-buffer)
2272       (let* ((gnus-save-article-buffer save-buffer)
2273              (filename
2274               (cond
2275                ((not gnus-prompt-before-saving) 'default)
2276                ((eq gnus-prompt-before-saving 'always) nil)
2277                (t file)))
2278              (gnus-number-of-articles-to-be-saved
2279               (when (eq gnus-prompt-before-saving t)
2280                 num)))                  ; Magic
2281         (set-buffer gnus-article-current-summary)
2282         (funcall gnus-default-article-saver filename)))))
2283
2284 (defun gnus-read-save-file-name (prompt &optional filename
2285                                         function group headers variable)
2286   (let ((default-name
2287           (funcall function group headers (symbol-value variable)))
2288         result)
2289     (setq result
2290         (expand-file-name
2291      (cond
2292       ((eq filename 'default)
2293        default-name)
2294       ((eq filename t)
2295        default-name)
2296       (filename filename)
2297       (t
2298        (let* ((split-name (gnus-get-split-value gnus-split-methods))
2299               (prompt
2300                (format prompt
2301                        (if (and gnus-number-of-articles-to-be-saved
2302                                 (> gnus-number-of-articles-to-be-saved 1))
2303                            (format "these %d articles"
2304                                    gnus-number-of-articles-to-be-saved)
2305                          "this article")))
2306               (file
2307                ;; Let the split methods have their say.
2308                (cond
2309                 ;; No split name was found.
2310                 ((null split-name)
2311                  (read-file-name
2312                   (concat prompt " (default "
2313                           (file-name-nondirectory default-name) ") ")
2314                   (file-name-directory default-name)
2315                   default-name))
2316                 ;; A single group name is returned.
2317                 ((stringp split-name)
2318                  (setq default-name
2319                        (funcall function split-name headers
2320                                 (symbol-value variable)))
2321                  (read-file-name
2322                   (concat prompt " (default "
2323                           (file-name-nondirectory default-name) ") ")
2324                   (file-name-directory default-name)
2325                   default-name))
2326                 ;; A single split name was found
2327                 ((= 1 (length split-name))
2328                  (let* ((name (expand-file-name
2329                                (car split-name) gnus-article-save-directory))
2330                         (dir (cond ((file-directory-p name)
2331                                     (file-name-as-directory name))
2332                                    ((file-exists-p name) name)
2333                                    (t gnus-article-save-directory))))
2334                    (read-file-name
2335                     (concat prompt " (default " name ") ")
2336                     dir name)))
2337                 ;; A list of splits was found.
2338                 (t
2339                  (setq split-name (nreverse split-name))
2340                  (let (result)
2341                    (let ((file-name-history
2342                           (nconc split-name file-name-history)))
2343                      (setq result
2344                            (expand-file-name
2345                             (read-file-name
2346                              (concat prompt " (`M-p' for defaults) ")
2347                              gnus-article-save-directory
2348                              (car split-name))
2349                             gnus-article-save-directory)))
2350                    (car (push result file-name-history)))))))
2351          ;; Create the directory.
2352          (gnus-make-directory (file-name-directory file))
2353          ;; If we have read a directory, we append the default file name.
2354          (when (file-directory-p file)
2355            (setq file (expand-file-name (file-name-nondirectory default-name)
2356                                         (file-name-as-directory file))))
2357          ;; Possibly translate some characters.
2358          (nnheader-translate-file-chars file))))))
2359     (gnus-make-directory (file-name-directory result))
2360     (set variable result)))
2361
2362 (defun gnus-article-archive-name (group)
2363   "Return the first instance of an \"Archive-name\" in the current buffer."
2364   (let ((case-fold-search t))
2365     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
2366       (nnheader-concat gnus-article-save-directory
2367                        (match-string 1)))))
2368
2369 (defun gnus-article-nndoc-name (group)
2370   "If GROUP is an nndoc group, return the name of the parent group."
2371   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
2372     (gnus-group-get-parameter group 'save-article-group)))
2373
2374 (defun gnus-summary-save-in-rmail (&optional filename)
2375   "Append this article to Rmail file.
2376 Optional argument FILENAME specifies file name.
2377 Directory to save to is default to `gnus-article-save-directory'."
2378   (setq filename (gnus-read-save-file-name
2379                   "Save %s in rmail file:" filename
2380                   gnus-rmail-save-name gnus-newsgroup-name
2381                   gnus-current-headers 'gnus-newsgroup-last-rmail))
2382   (gnus-eval-in-buffer-window gnus-save-article-buffer
2383     (save-excursion
2384       (save-restriction
2385         (widen)
2386         (gnus-output-to-rmail filename))))
2387   filename)
2388
2389 (defun gnus-summary-save-in-mail (&optional filename)
2390   "Append this article to Unix mail file.
2391 Optional argument FILENAME specifies file name.
2392 Directory to save to is default to `gnus-article-save-directory'."
2393   (setq filename (gnus-read-save-file-name
2394                   "Save %s in Unix mail file:" filename
2395                   gnus-mail-save-name gnus-newsgroup-name
2396                   gnus-current-headers 'gnus-newsgroup-last-mail))
2397   (gnus-eval-in-buffer-window gnus-save-article-buffer
2398     (save-excursion
2399       (save-restriction
2400         (widen)
2401         (if (and (file-readable-p filename)
2402                  (mail-file-babyl-p filename))
2403             (rmail-output-to-rmail-file filename t)
2404           (gnus-output-to-mail filename)))))
2405   filename)
2406
2407 (defun gnus-summary-save-in-file (&optional filename overwrite)
2408   "Append this article to file.
2409 Optional argument FILENAME specifies file name.
2410 Directory to save to is default to `gnus-article-save-directory'."
2411   (setq filename (gnus-read-save-file-name
2412                   "Save %s in file:" filename
2413                   gnus-file-save-name gnus-newsgroup-name
2414                   gnus-current-headers 'gnus-newsgroup-last-file))
2415   (gnus-eval-in-buffer-window gnus-save-article-buffer
2416     (save-excursion
2417       (save-restriction
2418         (widen)
2419         (when (and overwrite
2420                    (file-exists-p filename))
2421           (delete-file filename))
2422         (gnus-output-to-file filename))))
2423   filename)
2424
2425 (defun gnus-summary-write-to-file (&optional filename)
2426   "Write this article to a file.
2427 Optional argument FILENAME specifies file name.
2428 The directory to save in defaults to `gnus-article-save-directory'."
2429   (gnus-summary-save-in-file nil t))
2430
2431 (defun gnus-summary-save-body-in-file (&optional filename)
2432   "Append this article body to a file.
2433 Optional argument FILENAME specifies file name.
2434 The directory to save in defaults to `gnus-article-save-directory'."
2435   (setq filename (gnus-read-save-file-name
2436                   "Save %s body in file:" filename
2437                   gnus-file-save-name gnus-newsgroup-name
2438                   gnus-current-headers 'gnus-newsgroup-last-file))
2439   (gnus-eval-in-buffer-window gnus-save-article-buffer
2440     (save-excursion
2441       (save-restriction
2442         (widen)
2443         (when (article-goto-body)
2444           (narrow-to-region (point) (point-max)))
2445         (gnus-output-to-file filename))))
2446   filename)
2447
2448 (defun gnus-summary-save-in-pipe (&optional command)
2449   "Pipe this article to subprocess."
2450   (setq command
2451         (cond ((and (eq command 'default)
2452                     gnus-last-shell-command)
2453                gnus-last-shell-command)
2454               (command command)
2455               (t (read-string
2456                   (format
2457                    "Shell command on %s: "
2458                    (if (and gnus-number-of-articles-to-be-saved
2459                             (> gnus-number-of-articles-to-be-saved 1))
2460                        (format "these %d articles"
2461                                gnus-number-of-articles-to-be-saved)
2462                      "this article"))
2463                   gnus-last-shell-command))))
2464   (when (string-equal command "")
2465     (setq command gnus-last-shell-command))
2466   (gnus-eval-in-buffer-window gnus-article-buffer
2467     (save-restriction
2468       (widen)
2469       (shell-command-on-region (point-min) (point-max) command nil)))
2470   (setq gnus-last-shell-command command))
2471
2472 ;;; Article file names when saving.
2473
2474 (defun gnus-capitalize-newsgroup (newsgroup)
2475   "Capitalize NEWSGROUP name."
2476   (when (not (zerop (length newsgroup)))
2477     (concat (char-to-string (upcase (aref newsgroup 0)))
2478             (substring newsgroup 1))))
2479
2480 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2481   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2482 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
2483 Otherwise, it is like ~/News/news/group/num."
2484   (let ((default
2485           (expand-file-name
2486            (concat (if (gnus-use-long-file-name 'not-save)
2487                        (gnus-capitalize-newsgroup newsgroup)
2488                      (gnus-newsgroup-directory-form newsgroup))
2489                    "/" (int-to-string (mail-header-number headers)))
2490            gnus-article-save-directory)))
2491     (if (and last-file
2492              (string-equal (file-name-directory default)
2493                            (file-name-directory last-file))
2494              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2495         default
2496       (or last-file default))))
2497
2498 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2499   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2500 If variable `gnus-use-long-file-name' is non-nil, it is
2501 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2502   (let ((default
2503           (expand-file-name
2504            (concat (if (gnus-use-long-file-name 'not-save)
2505                        newsgroup
2506                      (gnus-newsgroup-directory-form newsgroup))
2507                    "/" (int-to-string (mail-header-number headers)))
2508            gnus-article-save-directory)))
2509     (if (and last-file
2510              (string-equal (file-name-directory default)
2511                            (file-name-directory last-file))
2512              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2513         default
2514       (or last-file default))))
2515
2516 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2517   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2518 If variable `gnus-use-long-file-name' is non-nil, it is
2519 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2520   (or last-file
2521       (expand-file-name
2522        (if (gnus-use-long-file-name 'not-save)
2523            newsgroup
2524          (expand-file-name "news" (gnus-newsgroup-directory-form newsgroup)))
2525        gnus-article-save-directory)))
2526
2527 (eval-and-compile
2528   (mapcar
2529    (lambda (func)
2530      (let (afunc gfunc)
2531        (if (consp func)
2532            (setq afunc (car func)
2533                  gfunc (cdr func))
2534          (setq afunc func
2535                gfunc (intern (format "gnus-%s" func))))
2536        (defalias gfunc
2537          (if (fboundp afunc)
2538            `(lambda (&optional interactive &rest args)
2539               ,(documentation afunc t)
2540               (interactive (list t))
2541               (save-excursion
2542                 (set-buffer gnus-article-buffer)
2543                 (if interactive
2544                     (call-interactively ',afunc)
2545                   (apply ',afunc args))))))))
2546    '(article-hide-headers
2547      article-hide-boring-headers
2548      article-treat-overstrike
2549      article-fill-long-lines
2550      article-capitalize-sentences
2551      article-remove-cr
2552      article-display-x-face
2553      article-de-quoted-unreadable
2554      article-de-base64-unreadable
2555      article-decode-HZ
2556      article-wash-html
2557      article-hide-list-identifiers
2558      article-hide-pgp
2559      article-strip-banner
2560      article-babel
2561      article-hide-pem
2562      article-hide-signature
2563      article-strip-headers-in-body
2564      article-remove-trailing-blank-lines
2565      article-strip-leading-blank-lines
2566      article-strip-multiple-blank-lines
2567      article-strip-leading-space
2568      article-strip-trailing-space
2569      article-strip-blank-lines
2570      article-strip-all-blank-lines
2571      article-date-local
2572      article-date-iso8601
2573      article-date-original
2574      article-date-ut
2575      article-decode-mime-words
2576      article-decode-charset
2577      article-decode-encoded-words
2578      article-date-user
2579      article-date-lapsed
2580      article-emphasize
2581      article-treat-dumbquotes
2582      article-normalize-headers
2583      (article-show-all . gnus-article-show-all-headers))))
2584 \f
2585 ;;;
2586 ;;; Gnus article mode
2587 ;;;
2588
2589 (put 'gnus-article-mode 'mode-class 'special)
2590
2591 (set-keymap-parent gnus-article-mode-map widget-keymap)
2592
2593 (gnus-define-keys gnus-article-mode-map
2594   " " gnus-article-goto-next-page
2595   "\177" gnus-article-goto-prev-page
2596   [delete] gnus-article-goto-prev-page
2597   [backspace] gnus-article-goto-prev-page
2598   "\C-c^" gnus-article-refer-article
2599   "h" gnus-article-show-summary
2600   "s" gnus-article-show-summary
2601   "\C-c\C-m" gnus-article-mail
2602   "?" gnus-article-describe-briefly
2603   "e" gnus-summary-edit-article
2604   "<" beginning-of-buffer
2605   ">" end-of-buffer
2606   "\C-c\C-i" gnus-info-find-node
2607   "\C-c\C-b" gnus-bug
2608
2609   "\C-d" gnus-article-read-summary-keys
2610   "\M-*" gnus-article-read-summary-keys
2611   "\M-#" gnus-article-read-summary-keys
2612   "\M-^" gnus-article-read-summary-keys
2613   "\M-g" gnus-article-read-summary-keys)
2614
2615 (substitute-key-definition
2616  'undefined 'gnus-article-read-summary-keys gnus-article-mode-map)
2617
2618 (defun gnus-article-make-menu-bar ()
2619   (gnus-turn-off-edit-menu 'article)
2620   (unless (boundp 'gnus-article-article-menu)
2621     (easy-menu-define
2622      gnus-article-article-menu gnus-article-mode-map ""
2623      '("Article"
2624        ["Scroll forwards" gnus-article-goto-next-page t]
2625        ["Scroll backwards" gnus-article-goto-prev-page t]
2626        ["Show summary" gnus-article-show-summary t]
2627        ["Fetch Message-ID at point" gnus-article-refer-article t]
2628        ["Mail to address at point" gnus-article-mail t]
2629        ["Send a bug report" gnus-bug t]))
2630
2631     (easy-menu-define
2632      gnus-article-treatment-menu gnus-article-mode-map ""
2633      '("Treatment"
2634        ["Hide headers" gnus-article-hide-headers t]
2635        ["Hide signature" gnus-article-hide-signature t]
2636        ["Hide citation" gnus-article-hide-citation t]
2637        ["Treat overstrike" gnus-article-treat-overstrike t]
2638        ["Remove carriage return" gnus-article-remove-cr t]
2639        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
2640        ["Remove base64" gnus-article-de-base64-unreadable t]
2641        ["Treat html" gnus-article-wash-html t]
2642        ["Decode HZ" gnus-article-decode-HZ t]))
2643
2644     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
2645
2646     (when (boundp 'gnus-summary-post-menu)
2647       (define-key gnus-article-mode-map [menu-bar post]
2648         (cons "Post" gnus-summary-post-menu)))
2649
2650     (gnus-run-hooks 'gnus-article-menu-hook)))
2651
2652 (defun gnus-article-mode ()
2653   "Major mode for displaying an article.
2654
2655 All normal editing commands are switched off.
2656
2657 The following commands are available in addition to all summary mode
2658 commands:
2659 \\<gnus-article-mode-map>
2660 \\[gnus-article-next-page]\t Scroll the article one page forwards
2661 \\[gnus-article-prev-page]\t Scroll the article one page backwards
2662 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
2663 \\[gnus-article-show-summary]\t Display the summary buffer
2664 \\[gnus-article-mail]\t Send a reply to the address near point
2665 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
2666 \\[gnus-info-find-node]\t Go to the Gnus info node"
2667   (interactive)
2668   (when (gnus-visual-p 'article-menu 'menu)
2669     (gnus-article-make-menu-bar))
2670   (gnus-simplify-mode-line)
2671   (setq mode-name "Article")
2672   (setq major-mode 'gnus-article-mode)
2673   (make-local-variable 'minor-mode-alist)
2674   (use-local-map gnus-article-mode-map)
2675   (gnus-update-format-specifications nil 'article-mode)
2676   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
2677   (make-local-variable 'gnus-page-broken)
2678   (make-local-variable 'gnus-button-marker-list)
2679   (make-local-variable 'gnus-article-current-summary)
2680   (make-local-variable 'gnus-article-mime-handles)
2681   (make-local-variable 'gnus-article-decoded-p)
2682   (make-local-variable 'gnus-article-mime-handle-alist)
2683   (make-local-variable 'gnus-article-wash-types)
2684   (gnus-set-default-directory)
2685   (buffer-disable-undo)
2686   (setq buffer-read-only t)
2687   (set-syntax-table gnus-article-mode-syntax-table)
2688   (mm-enable-multibyte)
2689   (gnus-run-hooks 'gnus-article-mode-hook))
2690
2691 (defun gnus-article-setup-buffer ()
2692   "Initialize the article buffer."
2693   (let* ((name (if gnus-single-article-buffer "*Article*"
2694                  (concat "*Article " gnus-newsgroup-name "*")))
2695          (original
2696           (progn (string-match "\\*Article" name)
2697                  (concat " *Original Article"
2698                          (substring name (match-end 0))))))
2699     (setq gnus-article-buffer name)
2700     (setq gnus-original-article-buffer original)
2701     (setq gnus-article-mime-handle-alist nil)
2702     ;; This might be a variable local to the summary buffer.
2703     (unless gnus-single-article-buffer
2704       (save-excursion
2705         (set-buffer gnus-summary-buffer)
2706         (setq gnus-article-buffer name)
2707         (setq gnus-original-article-buffer original)
2708         (gnus-set-global-variables)))
2709     (gnus-article-setup-highlight-words)
2710     ;; Init original article buffer.
2711     (save-excursion
2712       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2713       (mm-enable-multibyte)
2714       (setq major-mode 'gnus-original-article-mode)
2715       (make-local-variable 'gnus-original-article))
2716     (if (get-buffer name)
2717         (save-excursion
2718           (set-buffer name)
2719           (when gnus-article-mime-handles
2720             (mm-destroy-parts gnus-article-mime-handles)
2721             (setq gnus-article-mime-handles nil))
2722           ;; Set it to nil in article-buffer!
2723           (setq gnus-article-mime-handle-alist nil) 
2724           (buffer-disable-undo)
2725           (setq buffer-read-only t)
2726           (unless (eq major-mode 'gnus-article-mode)
2727             (gnus-article-mode))
2728           (current-buffer))
2729       (save-excursion
2730         (set-buffer (gnus-get-buffer-create name))
2731         (gnus-article-mode)
2732         (make-local-variable 'gnus-summary-buffer)
2733         (gnus-summary-set-local-parameters gnus-newsgroup-name)
2734         (current-buffer)))))
2735
2736 ;; Set article window start at LINE, where LINE is the number of lines
2737 ;; from the head of the article.
2738 (defun gnus-article-set-window-start (&optional line)
2739   (set-window-start
2740    (get-buffer-window gnus-article-buffer t)
2741    (save-excursion
2742      (set-buffer gnus-article-buffer)
2743      (goto-char (point-min))
2744      (if (not line)
2745          (point-min)
2746        (gnus-message 6 "Moved to bookmark")
2747        (search-forward "\n\n" nil t)
2748        (forward-line line)
2749        (point)))))
2750
2751 (defun gnus-article-prepare (article &optional all-headers header)
2752   "Prepare ARTICLE in article mode buffer.
2753 ARTICLE should either be an article number or a Message-ID.
2754 If ARTICLE is an id, HEADER should be the article headers.
2755 If ALL-HEADERS is non-nil, no headers are hidden."
2756   (save-excursion
2757     ;; Make sure we start in a summary buffer.
2758     (unless (eq major-mode 'gnus-summary-mode)
2759       (set-buffer gnus-summary-buffer))
2760     (setq gnus-summary-buffer (current-buffer))
2761     (let* ((gnus-article (if header (mail-header-number header) article))
2762            (summary-buffer (current-buffer))
2763            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
2764            (group gnus-newsgroup-name)
2765            result)
2766       (save-excursion
2767         (gnus-article-setup-buffer)
2768         (set-buffer gnus-article-buffer)
2769         ;; Deactivate active regions.
2770         (when (and (boundp 'transient-mark-mode)
2771                    transient-mark-mode)
2772           (setq mark-active nil))
2773         (if (not (setq result (let ((buffer-read-only nil))
2774                                 (gnus-request-article-this-buffer
2775                                  article group))))
2776             ;; There is no such article.
2777             (save-excursion
2778               (when (and (numberp article)
2779                          (not (memq article gnus-newsgroup-sparse)))
2780                 (setq gnus-article-current
2781                       (cons gnus-newsgroup-name article))
2782                 (set-buffer gnus-summary-buffer)
2783                 (setq gnus-current-article article)
2784                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2785                     (progn
2786                       (gnus-summary-set-agent-mark article)
2787                       (message "Message marked for downloading"))
2788                   (gnus-summary-mark-article article gnus-canceled-mark)
2789                   (unless (memq article gnus-newsgroup-sparse)
2790                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
2791           (if (or (eq result 'pseudo)
2792                   (eq result 'nneething))
2793               (progn
2794                 (save-excursion
2795                   (set-buffer summary-buffer)
2796                   (push article gnus-newsgroup-history)
2797                   (setq gnus-last-article gnus-current-article
2798                         gnus-current-article 0
2799                         gnus-current-headers nil
2800                         gnus-article-current nil)
2801                   (if (eq result 'nneething)
2802                       (gnus-configure-windows 'summary)
2803                     (gnus-configure-windows 'article))
2804                   (gnus-set-global-variables))
2805                 (let ((gnus-article-mime-handle-alist-1
2806                        gnus-article-mime-handle-alist))
2807                   (gnus-set-mode-line 'article)))
2808             ;; The result from the `request' was an actual article -
2809             ;; or at least some text that is now displayed in the
2810             ;; article buffer.
2811             (when (and (numberp article)
2812                        (not (eq article gnus-current-article)))
2813               ;; Seems like a new article has been selected.
2814               ;; `gnus-current-article' must be an article number.
2815               (save-excursion
2816                 (set-buffer summary-buffer)
2817                 (push article gnus-newsgroup-history)
2818                 (setq gnus-last-article gnus-current-article
2819                       gnus-current-article article
2820                       gnus-current-headers
2821                       (gnus-summary-article-header gnus-current-article)
2822                       gnus-article-current
2823                       (cons gnus-newsgroup-name gnus-current-article))
2824                 (unless (vectorp gnus-current-headers)
2825                   (setq gnus-current-headers nil))
2826                 (gnus-summary-goto-subject gnus-current-article)
2827                 (when (gnus-summary-show-thread)
2828                   ;; If the summary buffer really was folded, the
2829                   ;; previous goto may not actually have gone to
2830                   ;; the right article, but the thread root instead.
2831                   ;; So we go again.
2832                   (gnus-summary-goto-subject gnus-current-article))
2833                 (gnus-run-hooks 'gnus-mark-article-hook)
2834                 (gnus-set-mode-line 'summary)
2835                 (when (gnus-visual-p 'article-highlight 'highlight)
2836                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2837                 ;; Set the global newsgroup variables here.
2838                 (gnus-set-global-variables)
2839                 (setq gnus-have-all-headers
2840                       (or all-headers gnus-show-all-headers))))
2841             (save-excursion
2842               (gnus-configure-windows 'article))
2843             (when (or (numberp article)
2844                       (stringp article))
2845               (gnus-article-prepare-display)
2846               ;; Do page break.
2847               (goto-char (point-min))
2848               (setq gnus-page-broken
2849                     (when gnus-break-pages
2850                       (gnus-narrow-to-page)
2851                       t)))
2852             (let ((gnus-article-mime-handle-alist-1
2853                    gnus-article-mime-handle-alist))
2854               (gnus-set-mode-line 'article))
2855             (article-goto-body)
2856             (set-window-point (get-buffer-window (current-buffer)) (point))
2857             t))))))
2858
2859 ;;;###autoload
2860 (defun gnus-article-prepare-display ()
2861   "Make the current buffer look like a nice article."
2862   ;; Hooks for getting information from the article.
2863   ;; This hook must be called before being narrowed.
2864   (let ((gnus-article-buffer (current-buffer))
2865         buffer-read-only)
2866     (unless (eq major-mode 'gnus-article-mode)
2867       (gnus-article-mode))
2868     (setq buffer-read-only nil
2869           gnus-article-wash-types nil)
2870     (gnus-run-hooks 'gnus-tmp-internal-hook)
2871     (when gnus-display-mime-function
2872       (funcall gnus-display-mime-function))
2873     (gnus-run-hooks 'gnus-article-prepare-hook)))
2874
2875 ;;;
2876 ;;; Gnus MIME viewing functions
2877 ;;;
2878
2879 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
2880   "The following specs can be used:
2881 %t  The MIME type
2882 %T  MIME type, along with additional info
2883 %n  The `name' parameter
2884 %d  The description, if any
2885 %l  The length of the encoded part
2886 %p  The part identifier number
2887 %e  Dots if the part isn't displayed")
2888
2889 (defvar gnus-mime-button-line-format-alist
2890   '((?t gnus-tmp-type ?s)
2891     (?T gnus-tmp-type-long ?s)
2892     (?n gnus-tmp-name ?s)
2893     (?d gnus-tmp-description ?s)
2894     (?p gnus-tmp-id ?s)
2895     (?l gnus-tmp-length ?d)
2896     (?e gnus-tmp-dots ?s)))
2897
2898 (defvar gnus-mime-button-commands
2899   '((gnus-article-press-button "\r" "Toggle Display")
2900     (gnus-mime-view-part "v" "View Interactively...")
2901     (gnus-mime-view-part-as-type "t" "View As Type...")
2902     (gnus-mime-save-part "o" "Save...")
2903     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
2904     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
2905     (gnus-mime-internalize-part "E" "View Internally")
2906     (gnus-mime-externalize-part "e" "View Externally")
2907     (gnus-mime-pipe-part "|" "Pipe To Command...")
2908     (gnus-mime-action-on-part "." "Take action on the part")))
2909
2910 (defun gnus-article-mime-part-status ()
2911   (if gnus-article-mime-handle-alist-1
2912       (format " (%d parts)" (length gnus-article-mime-handle-alist-1))
2913     ""))
2914
2915 (defvar gnus-mime-button-map
2916   (let ((map (make-sparse-keymap)))
2917     (set-keymap-parent map gnus-article-mode-map)
2918     (define-key map gnus-mouse-2 'gnus-article-push-button)
2919     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
2920     (dolist (c gnus-mime-button-commands)
2921       (define-key map (cadr c) (car c)))
2922     map))
2923
2924 (defun gnus-mime-button-menu (event)
2925   "Construct a context-sensitive menu of MIME commands."
2926   (interactive "e")
2927   (save-excursion
2928     (let ((pos (event-start event)))
2929       (set-buffer (window-buffer (posn-window pos)))
2930       (goto-char (posn-point pos))
2931       (gnus-article-check-buffer)
2932       (let ((response (x-popup-menu
2933                        t `("MIME Part"
2934                            ("" ,@(mapcar (lambda (c)
2935                                            (cons (caddr c) (car c)))
2936                                          gnus-mime-button-commands))))))
2937         (if response
2938             (call-interactively response))))))
2939
2940 (defun gnus-mime-view-all-parts (&optional handles)
2941   "View all the MIME parts."
2942   (interactive)
2943   (save-current-buffer
2944     (set-buffer gnus-article-buffer)
2945     (let ((handles (or handles gnus-article-mime-handles))
2946           (mail-parse-charset gnus-newsgroup-charset)
2947           (mail-parse-ignored-charsets 
2948            (save-excursion (set-buffer gnus-summary-buffer)
2949                            gnus-newsgroup-ignored-charsets)))
2950       (if (stringp (car handles))
2951           (gnus-mime-view-all-parts (cdr handles))
2952         (mapcar 'mm-display-part handles)))))
2953
2954 (defun gnus-mime-save-part ()
2955   "Save the MIME part under point."
2956   (interactive)
2957   (gnus-article-check-buffer)
2958   (let ((data (get-text-property (point) 'gnus-data)))
2959     (mm-save-part data)))
2960
2961 (defun gnus-mime-pipe-part ()
2962   "Pipe the MIME part under point to a process."
2963   (interactive)
2964   (gnus-article-check-buffer)
2965   (let ((data (get-text-property (point) 'gnus-data)))
2966     (mm-pipe-part data)))
2967
2968 (defun gnus-mime-view-part ()
2969   "Interactively choose a viewing method for the MIME part under point."
2970   (interactive)
2971   (gnus-article-check-buffer)
2972   (let ((data (get-text-property (point) 'gnus-data)))
2973     (mm-interactively-view-part data)))
2974
2975 (defun gnus-mime-view-part-as-type-internal ()
2976   (gnus-article-check-buffer)
2977   (let* ((name (mail-content-type-get
2978                 (mm-handle-type (get-text-property (point) 'gnus-data))
2979                 'name))
2980          (def-type (and name (mm-default-file-encoding name))))
2981     (and def-type (cons def-type 0))))
2982
2983 (defun gnus-mime-view-part-as-type (mime-type)
2984   "Choose a MIME media type, and view the part as such."
2985   (interactive
2986    (list (completing-read
2987           "View as MIME type: "
2988           (mapcar #'list (mailcap-mime-types))
2989           nil nil
2990           (gnus-mime-view-part-as-type-internal))))
2991   (gnus-article-check-buffer)
2992   (let ((handle (get-text-property (point) 'gnus-data)))
2993     (gnus-mm-display-part
2994      (mm-make-handle (mm-handle-buffer handle)
2995                      (cons mime-type (cdr (mm-handle-type handle)))
2996                      (mm-handle-encoding handle)
2997                      (mm-handle-undisplayer handle)
2998                      (mm-handle-disposition handle)
2999                      (mm-handle-description handle)
3000                      (mm-handle-cache handle)
3001                      (mm-handle-id handle)))))
3002
3003 (defun gnus-mime-copy-part (&optional handle)
3004   "Put the the MIME part under point into a new buffer."
3005   (interactive)
3006   (gnus-article-check-buffer)
3007   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3008          (contents (mm-get-part handle))|
3009          (base (file-name-nondirectory
3010                 (or
3011                  (mail-content-type-get (mm-handle-type handle) 'name)
3012                  (mail-content-type-get (mm-handle-type handle)
3013                                         'filename)
3014                  "*decoded*")))
3015          (buffer (generate-new-buffer base)))
3016     (switch-to-buffer buffer)
3017     (insert contents)
3018     ;; We do it this way to make `normal-mode' set the appropriate mode.
3019     (unwind-protect
3020         (progn
3021           (setq buffer-file-name (expand-file-name base))
3022           (normal-mode))
3023       (setq buffer-file-name nil))
3024     (goto-char (point-min))))
3025
3026 (defun gnus-mime-inline-part (&optional handle arg)
3027   "Insert the MIME part under point into the current buffer."
3028   (interactive (list nil current-prefix-arg))
3029   (gnus-article-check-buffer)
3030   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3031          contents charset
3032          (b (point))
3033          buffer-read-only)
3034     (if (mm-handle-undisplayer handle)
3035         (mm-remove-part handle)
3036       (setq contents (mm-get-part handle))
3037       (cond
3038        ((not arg)
3039         (setq charset (or (mail-content-type-get
3040                            (mm-handle-type handle) 'charset)
3041                           gnus-newsgroup-charset)))
3042        ((numberp arg)
3043         (setq charset
3044               (or (cdr (assq arg 
3045                              gnus-summary-show-article-charset-alist))
3046                   (read-coding-system "Charset: ")))))
3047       (forward-line 2)
3048       (mm-insert-inline handle (if charset 
3049                                    (mm-decode-coding-string contents charset)
3050                                  contents))
3051       (goto-char b))))
3052
3053 (defun gnus-mime-externalize-part (&optional handle)
3054   "View the MIME part under point with an external viewer."
3055   (interactive)
3056   (gnus-article-check-buffer)
3057   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3058          (mm-user-display-methods nil)
3059          (mm-inlined-types nil)
3060          (mail-parse-charset gnus-newsgroup-charset)
3061          (mail-parse-ignored-charsets 
3062           (save-excursion (set-buffer gnus-summary-buffer)
3063                           gnus-newsgroup-ignored-charsets)))
3064     (if (mm-handle-undisplayer handle)
3065         (mm-remove-part handle)
3066       (mm-display-part handle))))
3067
3068 (defun gnus-mime-internalize-part (&optional handle)
3069   "View the MIME part under point with an internal viewer.
3070 In no internal viewer is available, use an external viewer."
3071   (interactive)
3072   (gnus-article-check-buffer)
3073   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3074          (mm-inlined-types '(".*"))
3075          (mm-inline-large-images t)
3076          (mail-parse-charset gnus-newsgroup-charset)
3077          (mail-parse-ignored-charsets 
3078           (save-excursion (set-buffer gnus-summary-buffer)
3079                           gnus-newsgroup-ignored-charsets)))
3080     (if (mm-handle-undisplayer handle)
3081         (mm-remove-part handle)
3082       (mm-display-part handle))))
3083
3084 (defun gnus-mime-action-on-part (&optional action)
3085   "Do something with the MIME attachment at \(point\)."
3086   (interactive
3087    (list (completing-read "Action: " gnus-mime-action-alist)))
3088   (gnus-article-check-buffer)
3089   (let ((action-pair (assoc action gnus-mime-action-alist)))
3090     (if action-pair
3091         (funcall (cdr action-pair)))))
3092
3093
3094 (defun gnus-article-part-wrapper (n function)
3095   (save-current-buffer
3096     (set-buffer gnus-article-buffer)
3097     (when (> n (length gnus-article-mime-handle-alist))
3098       (error "No such part"))
3099     (gnus-article-goto-part n)
3100     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3101       (funcall function handle))))
3102
3103 (defun gnus-article-pipe-part (n)
3104   "Pipe MIME part N, which is the numerical prefix."
3105   (interactive "p")
3106   (gnus-article-part-wrapper n 'mm-pipe-part))
3107
3108 (defun gnus-article-save-part (n)
3109   "Save MIME part N, which is the numerical prefix."
3110   (interactive "p")
3111   (gnus-article-part-wrapper n 'mm-save-part))
3112
3113 (defun gnus-article-interactively-view-part (n)
3114   "View MIME part N interactively, which is the numerical prefix."
3115   (interactive "p")
3116   (gnus-article-part-wrapper n 'mm-interactively-view-part))
3117
3118 (defun gnus-article-copy-part (n)
3119   "Copy MIME part N, which is the numerical prefix."
3120   (interactive "p")
3121   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
3122
3123 (defun gnus-article-externalize-part (n)
3124   "View MIME part N externally, which is the numerical prefix."
3125   (interactive "p")
3126   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
3127
3128 (defun gnus-article-inline-part (n)
3129   "Inline MIME part N, which is the numerical prefix."
3130   (interactive "p")
3131   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
3132
3133 (defun gnus-article-mime-match-handle-first (condition)
3134   (if condition
3135       (let ((alist gnus-article-mime-handle-alist) ihandle n)
3136         (while (setq ihandle (pop alist))
3137           (if (and (cond 
3138                     ((functionp condition)
3139                      (funcall condition (cdr ihandle)))
3140                     ((eq condition 'undisplayed) 
3141                      (not (or (mm-handle-undisplayer (cdr ihandle))
3142                               (equal (mm-handle-media-type (cdr ihandle))
3143                                      "multipart/alternative"))))
3144                     ((eq condition 'undisplayed-alternative)
3145                      (not (mm-handle-undisplayer (cdr ihandle))))
3146                     (t t))
3147                    (gnus-article-goto-part (car ihandle))
3148                    (or (not n) (< (car ihandle) n)))
3149               (setq n (car ihandle))))
3150         (or n 1))
3151     1))
3152
3153 (defun gnus-article-view-part (&optional n)
3154   "View MIME part N, which is the numerical prefix."
3155   (interactive "P")
3156   (save-current-buffer
3157     (set-buffer gnus-article-buffer)
3158     (or (numberp n) (setq n (gnus-article-mime-match-handle-first 
3159                              gnus-article-mime-match-handle-function)))
3160     (when (> n (length gnus-article-mime-handle-alist))
3161       (error "No such part"))
3162     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3163       (when (gnus-article-goto-part n)
3164         (if (equal (car handle) "multipart/alternative")
3165             (gnus-article-press-button)
3166           (when (eq (gnus-mm-display-part handle) 'internal)
3167             (gnus-set-window-start)))))))
3168
3169 (defun gnus-mm-display-part (handle)
3170   "Display HANDLE and fix MIME button."
3171   (let ((id (get-text-property (point) 'gnus-part))
3172         (point (point))
3173         buffer-read-only)
3174     (forward-line 1)
3175     (prog1
3176         (let ((window (selected-window))
3177               (mail-parse-charset gnus-newsgroup-charset)
3178               (mail-parse-ignored-charsets 
3179                (save-excursion (set-buffer gnus-summary-buffer)
3180                                gnus-newsgroup-ignored-charsets)))
3181           (save-excursion
3182             (unwind-protect
3183                 (let ((win (get-buffer-window (current-buffer) t))
3184                       (beg (point)))
3185                   (when win
3186                     (select-window win))
3187                   (goto-char point)
3188                   (forward-line)
3189                   (if (mm-handle-displayed-p handle)
3190                       ;; This will remove the part.
3191                       (mm-display-part handle)
3192                     (save-restriction
3193                       (narrow-to-region (point) (1+ (point)))
3194                       (mm-display-part handle)
3195                       ;; We narrow to the part itself and
3196                       ;; then call the treatment functions.
3197                       (goto-char (point-min))
3198                       (forward-line 1)
3199                       (narrow-to-region (point) (point-max))
3200                       (gnus-treat-article
3201                        nil id
3202                        (1- (length gnus-article-mime-handles))
3203                        (mm-handle-media-type handle)))))
3204               (select-window window))))
3205       (goto-char point)
3206       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
3207       (gnus-insert-mime-button
3208        handle id (list (mm-handle-displayed-p handle)))
3209       (goto-char point))))
3210
3211 (defun gnus-article-goto-part (n)
3212   "Go to MIME part N."
3213   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
3214     (when point
3215       (goto-char point))))
3216
3217 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
3218   (let ((gnus-tmp-name
3219          (or (mail-content-type-get (mm-handle-type handle)
3220                                     'name)
3221              (mail-content-type-get (mm-handle-disposition handle)
3222                                     'filename)
3223              ""))
3224         (gnus-tmp-type (mm-handle-media-type handle))
3225         (gnus-tmp-description
3226          (mail-decode-encoded-word-string (or (mm-handle-description handle)
3227                                               "")))
3228         (gnus-tmp-dots
3229          (if (if displayed (car displayed)
3230                (mm-handle-displayed-p handle))
3231              "" "..."))
3232         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
3233                            (buffer-size)))
3234         gnus-tmp-type-long b e)
3235     (when (string-match ".*/" gnus-tmp-name)
3236       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
3237     (setq gnus-tmp-type-long (concat gnus-tmp-type
3238                                      (and (not (equal gnus-tmp-name ""))
3239                                           (concat "; " gnus-tmp-name))))
3240     (or (equal gnus-tmp-description "")
3241         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
3242     (unless (bolp)
3243       (insert "\n"))
3244     (setq b (point))
3245     (gnus-eval-format
3246      gnus-mime-button-line-format gnus-mime-button-line-format-alist
3247      `(local-map ,gnus-mime-button-map
3248                  keymap ,gnus-mime-button-map
3249                  gnus-callback gnus-mm-display-part
3250                  gnus-part ,gnus-tmp-id
3251                  article-type annotation
3252                  gnus-data ,handle))
3253     (setq e (point))
3254     (widget-convert-button
3255      'link b e
3256      :mime-handle handle
3257      :action 'gnus-widget-press-button
3258      :button-keymap gnus-mime-button-map
3259      :help-echo
3260      (lambda (widget/window &optional overlay pos)
3261        ;; Needed to properly clear the message due to a bug in
3262        ;; wid-edit (XEmacs only).
3263        (if (boundp 'help-echo-owns-message)
3264            (setq help-echo-owns-message t))
3265        (format
3266         "%S: %s the MIME part; %S: more options"
3267         (aref gnus-mouse-2 0)
3268         ;; XEmacs will get a single widget arg; Emacs 21 will get
3269         ;; window, overlay, position.
3270         (if (mm-handle-displayed-p
3271              (if overlay
3272                  (with-current-buffer (overlay-buffer overlay)
3273                    (widget-get (widget-at (overlay-start overlay))
3274                                :mime-handle))
3275                (widget-get widget/window :mime-handle)))
3276             "hide" "show")
3277         (aref gnus-down-mouse-3 0))))))
3278
3279 (defun gnus-widget-press-button (elems el)
3280   (goto-char (widget-get elems :from))
3281   (gnus-article-press-button))
3282
3283 (defvar gnus-displaying-mime nil)
3284
3285 (defun gnus-display-mime (&optional ihandles)
3286   "Display the MIME parts."
3287   (save-excursion
3288     (save-selected-window
3289       (let ((window (get-buffer-window gnus-article-buffer))
3290             (point (point)))
3291         (when window
3292           (select-window window)
3293           ;; We have to do this since selecting the window
3294           ;; may change the point.  So we set the window point.
3295           (set-window-point window point)))
3296       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
3297              buffer-read-only handle name type b e display)
3298         (when (and (not ihandles)
3299                    (not gnus-displaying-mime))
3300           ;; Top-level call; we clean up.
3301           (when gnus-article-mime-handles
3302             (mm-destroy-parts gnus-article-mime-handles)
3303             (setq gnus-article-mime-handle-alist nil));; A trick.
3304           (setq gnus-article-mime-handles handles)
3305           ;; We allow users to glean info from the handles.
3306           (when gnus-article-mime-part-function
3307             (gnus-mime-part-function handles)))
3308         (if (and handles
3309                  (or (not (stringp (car handles)))
3310                      (cdr handles)))
3311             (progn
3312               (when (and (not ihandles)
3313                          (not gnus-displaying-mime))
3314                 ;; Clean up for mime parts.
3315                 (article-goto-body)
3316                 (delete-region (point) (point-max)))
3317               (let ((gnus-displaying-mime t))
3318                 (gnus-mime-display-part handles)))
3319           (save-restriction
3320             (article-goto-body)
3321             (narrow-to-region (point) (point-max))
3322             (gnus-treat-article nil 1 1)
3323             (widen)))
3324         (unless ihandles
3325           ;; Highlight the headers.
3326           (save-excursion
3327             (save-restriction
3328               (article-goto-body)
3329               (narrow-to-region (point-min) (point))
3330               (gnus-treat-article 'head))))))))
3331
3332 (defvar gnus-mime-display-multipart-as-mixed nil)
3333
3334 (defun gnus-mime-display-part (handle)
3335   (cond
3336    ;; Single part.
3337    ((not (stringp (car handle)))
3338     (gnus-mime-display-single handle))
3339    ;; User-defined multipart
3340    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
3341     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
3342              handle))
3343    ;; multipart/alternative
3344    ((and (equal (car handle) "multipart/alternative")
3345          (not gnus-mime-display-multipart-as-mixed))
3346     (let ((id (1+ (length gnus-article-mime-handle-alist))))
3347       (push (cons id handle) gnus-article-mime-handle-alist)
3348       (gnus-mime-display-alternative (cdr handle) nil nil id)))
3349    ;; multipart/related
3350    ((and (equal (car handle) "multipart/related")
3351          (not gnus-mime-display-multipart-as-mixed))
3352     ;;;!!!We should find the start part, but we just default
3353     ;;;!!!to the first part.
3354     (gnus-mime-display-part (cadr handle)))
3355    ;; Other multiparts are handled like multipart/mixed.
3356    (t
3357     (gnus-mime-display-mixed (cdr handle)))))
3358
3359 (defun gnus-mime-part-function (handles)
3360   (if (stringp (car handles))
3361       (mapcar 'gnus-mime-part-function (cdr handles))
3362     (funcall gnus-article-mime-part-function handles)))
3363
3364 (defun gnus-mime-display-mixed (handles)
3365   (mapcar 'gnus-mime-display-part handles))
3366
3367 (defun gnus-mime-display-single (handle)
3368   (let ((type (mm-handle-media-type handle))
3369         (ignored gnus-ignored-mime-types)
3370         (not-attachment t)
3371         (move nil)
3372         display text)
3373     (catch 'ignored
3374       (progn
3375         (while ignored
3376           (when (string-match (pop ignored) type)
3377             (throw 'ignored nil)))
3378         (if (and (setq not-attachment
3379                        (and (not (mm-inline-override-p handle))
3380                             (or (not (mm-handle-disposition handle))
3381                                 (equal (car (mm-handle-disposition handle))
3382                                        "inline")
3383                                 (mm-attachment-override-p handle))))
3384                  (mm-automatic-display-p handle)
3385                  (or (mm-inlined-p handle)
3386                      (mm-automatic-external-display-p type)))
3387             (setq display t)
3388           (when (equal (mm-handle-media-supertype handle) "text")
3389             (setq text t)))
3390         (let ((id (1+ (length gnus-article-mime-handle-alist))))
3391           (push (cons id handle) gnus-article-mime-handle-alist)
3392           (when (or (not display)
3393                     (not (gnus-unbuttonized-mime-type-p type)))
3394             ;(gnus-article-insert-newline)
3395             (gnus-insert-mime-button
3396              handle id (list (or display (and not-attachment text))))
3397             (gnus-article-insert-newline)
3398             ;(gnus-article-insert-newline)
3399             (setq move t)))
3400         (let ((beg (point)))
3401           (cond
3402            (display
3403             (when move
3404               (forward-line -2)
3405               (setq beg (point)))
3406             (let ((mail-parse-charset gnus-newsgroup-charset)
3407                   (mail-parse-ignored-charsets 
3408                    (save-excursion (condition-case ()
3409                                        (set-buffer gnus-summary-buffer)
3410                                      (error))
3411                                    gnus-newsgroup-ignored-charsets)))
3412               (mm-display-part handle t))
3413             (goto-char (point-max)))
3414            ((and text not-attachment)
3415             (when move
3416               (forward-line -2)
3417               (setq beg (point)))
3418             (gnus-article-insert-newline)
3419             (mm-insert-inline handle (mm-get-part handle))
3420             (goto-char (point-max))))
3421           ;; Do highlighting.
3422           (save-excursion
3423             (save-restriction
3424               (narrow-to-region beg (point))
3425               (gnus-treat-article
3426                nil (length gnus-article-mime-handle-alist)
3427                (1- (length gnus-article-mime-handles))
3428                (mm-handle-media-type handle)))))))))
3429
3430 (defun gnus-unbuttonized-mime-type-p (type)
3431   "Say whether TYPE is to be unbuttonized."
3432   (unless gnus-inhibit-mime-unbuttonizing
3433     (catch 'found
3434       (let ((types gnus-unbuttonized-mime-types))
3435         (while types
3436           (when (string-match (pop types) type)
3437             (throw 'found t)))))))
3438
3439 (defun gnus-article-insert-newline ()
3440   "Insert a newline, but mark it as undeletable."
3441   (gnus-put-text-property
3442    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
3443
3444 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
3445   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
3446          (ihandles handles)
3447          (point (point))
3448          handle buffer-read-only from props begend not-pref)
3449     (save-window-excursion
3450       (save-restriction
3451         (when ibegend
3452           (narrow-to-region (car ibegend)
3453                             (or (cdr ibegend)
3454                                 (progn
3455                                   (goto-char (car ibegend))
3456                                   (forward-line 2)
3457                                   (point))))
3458           (delete-region (point-min) (point-max))
3459           (mm-remove-parts handles))
3460         (setq begend (list (point-marker)))
3461         ;; Do the toggle.
3462         (unless (setq not-pref (cadr (member preferred ihandles)))
3463           (setq not-pref (car ihandles)))
3464         (when (or ibegend
3465                   (not (gnus-unbuttonized-mime-type-p
3466                         "multipart/alternative")))
3467           (gnus-add-text-properties
3468            (setq from (point))
3469            (progn
3470              (insert (format "%d.  " id))
3471              (point))
3472            `(gnus-callback
3473              (lambda (handles)
3474                (unless ,(not ibegend)
3475                  (setq gnus-article-mime-handle-alist
3476                        ',gnus-article-mime-handle-alist))
3477                (gnus-mime-display-alternative
3478                 ',ihandles ',not-pref ',begend ,id))
3479              local-map ,gnus-mime-button-map
3480              ,gnus-mouse-face-prop ,gnus-article-mouse-face
3481              face ,gnus-article-button-face
3482              keymap ,gnus-mime-button-map
3483              gnus-part ,id
3484              gnus-data ,handle))
3485           (widget-convert-button 'link from (point)
3486                                  :action 'gnus-widget-press-button
3487                                  :button-keymap gnus-widget-button-keymap)
3488           ;; Do the handles
3489           (while (setq handle (pop handles))
3490             (gnus-add-text-properties
3491              (setq from (point))
3492              (progn
3493                (insert (format "(%c) %-18s"
3494                                (if (equal handle preferred) ?* ? )
3495                                (mm-handle-media-type handle)))
3496                (point))
3497              `(gnus-callback
3498                (lambda (handles)
3499                  (unless ,(not ibegend)
3500                    (setq gnus-article-mime-handle-alist
3501                          ',gnus-article-mime-handle-alist))
3502                  (gnus-mime-display-alternative
3503                   ',ihandles ',handle ',begend ,id))
3504                local-map ,gnus-mime-button-map
3505                ,gnus-mouse-face-prop ,gnus-article-mouse-face
3506                face ,gnus-article-button-face
3507                keymap ,gnus-mime-button-map
3508                gnus-part ,id
3509                gnus-data ,handle))
3510             (widget-convert-button 'link from (point)
3511                                    :action 'gnus-widget-press-button
3512                                    :button-keymap gnus-widget-button-keymap)
3513             (insert "  "))
3514           (insert "\n\n"))
3515         (when preferred
3516           (if (stringp (car preferred))
3517               (gnus-display-mime preferred)
3518             (let ((mail-parse-charset gnus-newsgroup-charset)
3519                   (mail-parse-ignored-charsets 
3520                    (save-excursion (set-buffer gnus-summary-buffer)
3521                                    gnus-newsgroup-ignored-charsets)))
3522               (mm-display-part preferred)
3523               ;; Do highlighting.
3524               (save-excursion
3525                 (save-restriction
3526                   (narrow-to-region (car begend) (point-max))
3527                   (gnus-treat-article
3528                    nil (length gnus-article-mime-handle-alist)
3529                    (1- (length gnus-article-mime-handles))
3530                    (mm-handle-media-type handle))))))
3531           (goto-char (point-max))
3532           (setcdr begend (point-marker)))))
3533     (when ibegend
3534       (goto-char point))))
3535
3536 (defun gnus-article-wash-status ()
3537   "Return a string which display status of article washing."
3538   (save-excursion
3539     (set-buffer gnus-article-buffer)
3540     (let ((cite (memq 'cite gnus-article-wash-types))
3541           (headers (memq 'headers gnus-article-wash-types))
3542           (boring (memq 'boring-headers gnus-article-wash-types))
3543           (pgp (memq 'pgp gnus-article-wash-types))
3544           (pem (memq 'pem gnus-article-wash-types))
3545           (signature (memq 'signature gnus-article-wash-types))
3546           (overstrike (memq 'overstrike gnus-article-wash-types))
3547           (emphasis (memq 'emphasis gnus-article-wash-types)))
3548       (format "%c%c%c%c%c%c"
3549               (if cite ?c ? )
3550               (if (or headers boring) ?h ? )
3551               (if (or pgp pem) ?p ? )
3552               (if signature ?s ? )
3553               (if overstrike ?o ? )
3554               (if emphasis ?e ? )))))
3555
3556 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
3557
3558 (defun gnus-article-maybe-hide-headers ()
3559   "Hide unwanted headers if `gnus-have-all-headers' is nil.
3560 Provided for backwards compatibility."
3561   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
3562                  (not (save-excursion (set-buffer gnus-summary-buffer)
3563                                       gnus-have-all-headers)))
3564              (not gnus-inhibit-hiding))
3565     (gnus-article-hide-headers)))
3566
3567 ;;; Article savers.
3568
3569 (defun gnus-output-to-file (file-name)
3570   "Append the current article to a file named FILE-NAME."
3571   (let ((artbuf (current-buffer)))
3572     (with-temp-buffer
3573       (insert-buffer-substring artbuf)
3574       ;; Append newline at end of the buffer as separator, and then
3575       ;; save it to file.
3576       (goto-char (point-max))
3577       (insert "\n")
3578       (mm-append-to-file (point-min) (point-max) file-name)
3579       t)))
3580
3581 (defun gnus-narrow-to-page (&optional arg)
3582   "Narrow the article buffer to a page.
3583 If given a numerical ARG, move forward ARG pages."
3584   (interactive "P")
3585   (setq arg (if arg (prefix-numeric-value arg) 0))
3586   (save-excursion
3587     (set-buffer gnus-article-buffer)
3588     (goto-char (point-min))
3589     (widen)
3590     ;; Remove any old next/prev buttons.
3591     (when (gnus-visual-p 'page-marker)
3592       (let ((buffer-read-only nil))
3593         (gnus-remove-text-with-property 'gnus-prev)
3594         (gnus-remove-text-with-property 'gnus-next)))
3595     (when
3596         (cond ((< arg 0)
3597                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
3598               ((> arg 0)
3599                (re-search-forward page-delimiter nil 'move arg)))
3600       (goto-char (match-end 0)))
3601     (narrow-to-region
3602      (point)
3603      (if (re-search-forward page-delimiter nil 'move)
3604          (match-beginning 0)
3605        (point)))
3606     (when (and (gnus-visual-p 'page-marker)
3607                (not (= (point-min) 1)))
3608       (save-excursion
3609         (goto-char (point-min))
3610         (gnus-insert-prev-page-button)))
3611     (when (and (gnus-visual-p 'page-marker)
3612                (< (+ (point-max) 2) (buffer-size)))
3613       (save-excursion
3614         (goto-char (point-max))
3615         (gnus-insert-next-page-button)))))
3616
3617 ;; Article mode commands
3618
3619 (defun gnus-article-goto-next-page ()
3620   "Show the next page of the article."
3621   (interactive)
3622   (when (gnus-article-next-page)
3623     (goto-char (point-min))
3624     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
3625
3626 (defun gnus-article-goto-prev-page ()
3627   "Show the next page of the article."
3628   (interactive)
3629   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
3630     (gnus-article-prev-page nil)))
3631
3632 (defun gnus-article-next-page (&optional lines)
3633   "Show the next page of the current article.
3634 If end of article, return non-nil.  Otherwise return nil.
3635 Argument LINES specifies lines to be scrolled up."
3636   (interactive "p")
3637   (move-to-window-line -1)
3638   (if (save-excursion
3639         (end-of-line)
3640         (and (pos-visible-in-window-p)  ;Not continuation line.
3641              (eobp)))
3642       ;; Nothing in this page.
3643       (if (or (not gnus-page-broken)
3644               (save-excursion
3645                 (save-restriction
3646                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
3647           t                             ;Nothing more.
3648         (gnus-narrow-to-page 1)         ;Go to next page.
3649         nil)
3650     ;; More in this page.
3651     (let ((scroll-in-place nil))
3652       (condition-case ()
3653           (scroll-up lines)
3654         (end-of-buffer
3655          ;; Long lines may cause an end-of-buffer error.
3656          (goto-char (point-max)))))
3657     (move-to-window-line 0)
3658     nil))
3659
3660 (defun gnus-article-prev-page (&optional lines)
3661   "Show previous page of current article.
3662 Argument LINES specifies lines to be scrolled down."
3663   (interactive "p")
3664   (move-to-window-line 0)
3665   (if (and gnus-page-broken
3666            (bobp)
3667            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
3668       (progn
3669         (gnus-narrow-to-page -1)        ;Go to previous page.
3670         (goto-char (point-max))
3671         (recenter -1))
3672     (let ((scroll-in-place nil))
3673       (prog1
3674           (condition-case ()
3675               (scroll-down lines)
3676             (beginning-of-buffer
3677              (goto-char (point-min))))
3678         (move-to-window-line 0)))))
3679
3680 (defun gnus-article-refer-article ()
3681   "Read article specified by message-id around point."
3682   (interactive)
3683   (let ((point (point)))
3684     (search-forward ">" nil t)          ;Move point to end of "<....>".
3685     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
3686         (let ((message-id (match-string 1)))
3687           (goto-char point)
3688           (set-buffer gnus-summary-buffer)
3689           (gnus-summary-refer-article message-id))
3690       (goto-char (point))
3691       (error "No references around point"))))
3692
3693 (defun gnus-article-show-summary ()
3694   "Reconfigure windows to show summary buffer."
3695   (interactive)
3696   (if (not (gnus-buffer-live-p gnus-summary-buffer))
3697       (error "There is no summary buffer for this article buffer")
3698     (gnus-article-set-globals)
3699     (gnus-configure-windows 'article)
3700     (gnus-summary-goto-subject gnus-current-article)
3701     (gnus-summary-position-point)))
3702
3703 (defun gnus-article-describe-briefly ()
3704   "Describe article mode commands briefly."
3705   (interactive)
3706   (gnus-message 6 (substitute-command-keys "\\<gnus-article-mode-map>\\[gnus-article-goto-next-page]:Next page   \\[gnus-article-goto-prev-page]:Prev page  \\[gnus-article-show-summary]:Show summary  \\[gnus-info-find-node]:Run Info  \\[gnus-article-describe-briefly]:This help")))
3707
3708 (defun gnus-article-summary-command ()
3709   "Execute the last keystroke in the summary buffer."
3710   (interactive)
3711   (let ((obuf (current-buffer))
3712         (owin (current-window-configuration))
3713         func)
3714     (switch-to-buffer gnus-article-current-summary 'norecord)
3715     (setq func (lookup-key (current-local-map) (this-command-keys)))
3716     (call-interactively func)
3717     (set-buffer obuf)
3718     (set-window-configuration owin)
3719     (set-window-point (get-buffer-window (current-buffer)) (point))))
3720
3721 (defun gnus-article-summary-command-nosave ()
3722   "Execute the last keystroke in the summary buffer."
3723   (interactive)
3724   (let (func)
3725     (pop-to-buffer gnus-article-current-summary 'norecord)
3726     (setq func (lookup-key (current-local-map) (this-command-keys)))
3727     (call-interactively func)))
3728
3729 (defun gnus-article-check-buffer ()
3730   "Beep if not in an article buffer."
3731   (unless (equal major-mode 'gnus-article-mode)
3732     (error "Command invoked outside of a Gnus article buffer")))
3733
3734 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
3735   "Read a summary buffer key sequence and execute it from the article buffer."
3736   (interactive "P")
3737   (gnus-article-check-buffer)
3738   (let ((nosaves
3739          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
3740            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
3741            "=" "^" "\M-^" "|"))
3742         (nosave-but-article
3743          '("A\r"))
3744         (nosave-in-article
3745          '("\C-d"))
3746         (up-to-top
3747          '("n" "Gn" "p" "Gp"))
3748         keys new-sum-point)
3749     (save-excursion
3750       (set-buffer gnus-article-current-summary)
3751       (let (gnus-pick-mode)
3752         (push (or key last-command-event) unread-command-events)
3753         (setq keys (if gnus-xemacs
3754                        (events-to-keys (read-key-sequence nil))
3755                      (read-key-sequence nil)))))
3756                      
3757     (message "")
3758
3759     (if (or (member keys nosaves)
3760             (member keys nosave-but-article)
3761             (member keys nosave-in-article))
3762         (let (func)
3763           (save-window-excursion
3764             (pop-to-buffer gnus-article-current-summary 'norecord)
3765             ;; We disable the pick minor mode commands.
3766             (let (gnus-pick-mode)
3767               (setq func (lookup-key (current-local-map) keys))))
3768           (if (or (not func)
3769                   (numberp func))
3770               (ding)
3771             (unless (member keys nosave-in-article)
3772               (set-buffer gnus-article-current-summary))
3773             (call-interactively func)
3774             (setq new-sum-point (point)))
3775           (when (member keys nosave-but-article)
3776             (pop-to-buffer gnus-article-buffer 'norecord)))
3777       ;; These commands should restore window configuration.
3778       (let ((obuf (current-buffer))
3779             (owin (current-window-configuration))
3780             (opoint (point))
3781             (summary gnus-article-current-summary)
3782             func in-buffer selected)
3783         (if not-restore-window
3784             (pop-to-buffer summary 'norecord)
3785           (switch-to-buffer summary 'norecord))
3786         (setq in-buffer (current-buffer))
3787         ;; We disable the pick minor mode commands.
3788         (if (setq func (let (gnus-pick-mode)
3789                          (lookup-key (current-local-map) keys)))
3790             (progn
3791               (call-interactively func)
3792               (setq new-sum-point (point)))
3793           (ding))
3794         (when (eq in-buffer (current-buffer))
3795           (setq selected (gnus-summary-select-article))
3796           (set-buffer obuf)
3797           (unless not-restore-window
3798             (set-window-configuration owin))
3799           (when (eq selected 'old)
3800             (article-goto-body)
3801             (set-window-start (get-buffer-window (current-buffer))
3802                               1)
3803             (set-window-point (get-buffer-window (current-buffer))
3804                               (point)))
3805           (let ((win (get-buffer-window gnus-article-current-summary)))
3806             (when win
3807               (set-window-point win new-sum-point))))))))
3808
3809 (defun gnus-article-hide (&optional arg force)
3810   "Hide all the gruft in the current article.
3811 This means that PGP stuff, signatures, cited text and (some)
3812 headers will be hidden.
3813 If given a prefix, show the hidden text instead."
3814   (interactive (append (gnus-article-hidden-arg) (list 'force)))
3815   (gnus-article-hide-headers arg)
3816   (gnus-article-hide-list-identifiers arg)
3817   (gnus-article-hide-pgp arg)
3818   (gnus-article-hide-citation-maybe arg force)
3819   (gnus-article-hide-signature arg))
3820
3821 (defun gnus-article-maybe-highlight ()
3822   "Do some article highlighting if article highlighting is requested."
3823   (when (gnus-visual-p 'article-highlight 'highlight)
3824     (gnus-article-highlight-some)))
3825
3826 (defun gnus-check-group-server ()
3827   ;; Make sure the connection to the server is alive.
3828   (unless (gnus-server-opened
3829            (gnus-find-method-for-group gnus-newsgroup-name))
3830     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
3831     (gnus-request-group gnus-newsgroup-name t)))
3832
3833 (defun gnus-request-article-this-buffer (article group)
3834   "Get an article and insert it into this buffer."
3835   (let (do-update-line sparse-header)
3836     (prog1
3837         (save-excursion
3838           (erase-buffer)
3839           (gnus-kill-all-overlays)
3840           (setq group (or group gnus-newsgroup-name))
3841
3842           ;; Using `gnus-request-article' directly will insert the article into
3843           ;; `nntp-server-buffer' - so we'll save some time by not having to
3844           ;; copy it from the server buffer into the article buffer.
3845
3846           ;; We only request an article by message-id when we do not have the
3847           ;; headers for it, so we'll have to get those.
3848           (when (stringp article)
3849             (gnus-read-header article))
3850
3851           ;; If the article number is negative, that means that this article
3852           ;; doesn't belong in this newsgroup (possibly), so we find its
3853           ;; message-id and request it by id instead of number.
3854           (when (and (numberp article)
3855                      gnus-summary-buffer
3856                      (get-buffer gnus-summary-buffer)
3857                      (gnus-buffer-exists-p gnus-summary-buffer))
3858             (save-excursion
3859               (set-buffer gnus-summary-buffer)
3860               (let ((header (gnus-summary-article-header article)))
3861                 (when (< article 0)
3862                   (cond
3863                    ((memq article gnus-newsgroup-sparse)
3864                     ;; This is a sparse gap article.
3865                     (setq do-update-line article)
3866                     (setq article (mail-header-id header))
3867                     (setq sparse-header (gnus-read-header article))
3868                     (setq gnus-newsgroup-sparse
3869                           (delq article gnus-newsgroup-sparse)))
3870                    ((vectorp header)
3871                     ;; It's a real article.
3872                     (setq article (mail-header-id header)))
3873                    (t
3874                     ;; It is an extracted pseudo-article.
3875                     (setq article 'pseudo)
3876                     (gnus-request-pseudo-article header))))
3877
3878                 (let ((method (gnus-find-method-for-group
3879                                gnus-newsgroup-name)))
3880                   (when (and (eq (car method) 'nneething)
3881                              (vectorp header))
3882                     (let ((dir (expand-file-name
3883                                 (mail-header-subject header)
3884                                 (file-name-as-directory
3885                                  (or (cadr (assq 'nneething-address method))
3886                                      (nth 1 method))))))
3887                       (when (file-directory-p dir)
3888                         (setq article 'nneething)
3889                         (gnus-group-enter-directory dir))))))))
3890
3891           (cond
3892            ;; Refuse to select canceled articles.
3893            ((and (numberp article)
3894                  gnus-summary-buffer
3895                  (get-buffer gnus-summary-buffer)
3896                  (gnus-buffer-exists-p gnus-summary-buffer)
3897                  (eq (cdr (save-excursion
3898                             (set-buffer gnus-summary-buffer)
3899                             (assq article gnus-newsgroup-reads)))
3900                      gnus-canceled-mark))
3901             nil)
3902            ;; We first check `gnus-original-article-buffer'.
3903            ((and (get-buffer gnus-original-article-buffer)
3904                  (numberp article)
3905                  (save-excursion
3906                    (set-buffer gnus-original-article-buffer)
3907                    (and (equal (car gnus-original-article) group)
3908                         (eq (cdr gnus-original-article) article))))
3909             (insert-buffer-substring gnus-original-article-buffer)
3910             'article)
3911            ;; Check the backlog.
3912            ((and gnus-keep-backlog
3913                  (gnus-backlog-request-article group article (current-buffer)))
3914             'article)
3915            ;; Check asynchronous pre-fetch.
3916            ((gnus-async-request-fetched-article group article (current-buffer))
3917             (gnus-async-prefetch-next group article gnus-summary-buffer)
3918             (when (and (numberp article) gnus-keep-backlog)
3919               (gnus-backlog-enter-article group article (current-buffer)))
3920             'article)
3921            ;; Check the cache.
3922            ((and gnus-use-cache
3923                  (numberp article)
3924                  (gnus-cache-request-article article group))
3925             'article)
3926            ;; Get the article and put into the article buffer.
3927            ((or (stringp article)
3928                 (numberp article))
3929             (let ((gnus-override-method gnus-override-method)
3930                   (methods (and (stringp article) 
3931                                 gnus-refer-article-method))
3932                   result
3933                   (buffer-read-only nil))
3934               (setq methods
3935                     (if (listp methods)
3936                         methods
3937                       (list methods)))
3938               (when (and (null gnus-override-method)
3939                          methods)
3940                 (setq gnus-override-method (pop methods)))
3941               (while (not result)
3942                 (when (eq gnus-override-method 'current)
3943                   (setq gnus-override-method gnus-current-select-method))
3944                 (erase-buffer)
3945                 (gnus-kill-all-overlays)
3946                 (let ((gnus-newsgroup-name group))
3947                   (gnus-check-group-server))
3948                 (when (gnus-request-article article group (current-buffer))
3949                   (when (numberp article)
3950                     (gnus-async-prefetch-next group article 
3951                                               gnus-summary-buffer)
3952                     (when gnus-keep-backlog
3953                       (gnus-backlog-enter-article
3954                        group article (current-buffer))))
3955                   (setq result 'article))
3956                 (if (not result)
3957                     (if methods
3958                         (setq gnus-override-method (pop methods))
3959                       (setq result 'done))))
3960               (and (eq result 'article) 'article)))
3961            ;; It was a pseudo.
3962            (t article)))
3963
3964       ;; Associate this article with the current summary buffer.
3965       (setq gnus-article-current-summary gnus-summary-buffer)
3966
3967       ;; Take the article from the original article buffer
3968       ;; and place it in the buffer it's supposed to be in.
3969       (when (and (get-buffer gnus-article-buffer)
3970                  (equal (buffer-name (current-buffer))
3971                         (buffer-name (get-buffer gnus-article-buffer))))
3972         (save-excursion
3973           (if (get-buffer gnus-original-article-buffer)
3974               (set-buffer gnus-original-article-buffer)
3975             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
3976             (buffer-disable-undo)
3977             (setq major-mode 'gnus-original-article-mode)
3978             (setq buffer-read-only t))
3979           (let (buffer-read-only)
3980             (erase-buffer)
3981             (insert-buffer-substring gnus-article-buffer))
3982           (setq gnus-original-article (cons group article)))
3983
3984         ;; Decode charsets.
3985         (run-hooks 'gnus-article-decode-hook)
3986         ;; Mark article as decoded or not.
3987         (setq gnus-article-decoded-p gnus-article-decode-hook))
3988
3989       ;; Update sparse articles.
3990       (when (and do-update-line
3991                  (or (numberp article)
3992                      (stringp article)))
3993         (let ((buf (current-buffer)))
3994           (set-buffer gnus-summary-buffer)
3995           (gnus-summary-update-article do-update-line sparse-header)
3996           (gnus-summary-goto-subject do-update-line nil t)
3997           (set-window-point (get-buffer-window (current-buffer) t)
3998                             (point))
3999           (set-buffer buf))))))
4000
4001 ;;;
4002 ;;; Article editing
4003 ;;;
4004
4005 (defcustom gnus-article-edit-mode-hook nil
4006   "Hook run in article edit mode buffers."
4007   :group 'gnus-article-various
4008   :type 'hook)
4009
4010 (defvar gnus-article-edit-done-function nil)
4011
4012 (defvar gnus-article-edit-mode-map nil)
4013
4014 ;; Should we be using derived.el for this?
4015 (unless gnus-article-edit-mode-map
4016   (setq gnus-article-edit-mode-map (make-sparse-keymap))
4017   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
4018
4019   (gnus-define-keys gnus-article-edit-mode-map
4020     "\C-c\C-c" gnus-article-edit-done
4021     "\C-c\C-k" gnus-article-edit-exit)
4022
4023   (gnus-define-keys (gnus-article-edit-wash-map
4024                      "\C-c\C-w" gnus-article-edit-mode-map)
4025     "f" gnus-article-edit-full-stops))
4026
4027 (defun gnus-article-edit-mode ()
4028   "Major mode for editing articles.
4029 This is an extended text-mode.
4030
4031 \\{gnus-article-edit-mode-map}"
4032   (interactive)
4033   (setq major-mode 'gnus-article-edit-mode)
4034   (setq mode-name "Article Edit")
4035   (use-local-map gnus-article-edit-mode-map)
4036   (make-local-variable 'gnus-article-edit-done-function)
4037   (make-local-variable 'gnus-prev-winconf)
4038   (setq buffer-read-only nil)
4039   (buffer-enable-undo)
4040   (widen)
4041   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
4042
4043 (defun gnus-article-edit (&optional force)
4044   "Edit the current article.
4045 This will have permanent effect only in mail groups.
4046 If FORCE is non-nil, allow editing of articles even in read-only
4047 groups."
4048   (interactive "P")
4049   (when (and (not force)
4050              (gnus-group-read-only-p))
4051     (error "The current newsgroup does not support article editing"))
4052   (gnus-article-date-original)
4053   (gnus-article-edit-article
4054    'ignore
4055    `(lambda (no-highlight)
4056       'ignore
4057       (gnus-summary-edit-article-done
4058        ,(or (mail-header-references gnus-current-headers) "")
4059        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
4060
4061 (defun gnus-article-edit-article (start-func exit-func)
4062   "Start editing the contents of the current article buffer."
4063   (let ((winconf (current-window-configuration)))
4064     (set-buffer gnus-article-buffer)
4065     (gnus-article-edit-mode)
4066     (funcall start-func)
4067     (gnus-configure-windows 'edit-article)
4068     (setq gnus-article-edit-done-function exit-func)
4069     (setq gnus-prev-winconf winconf)
4070     (gnus-message 6 "C-c C-c to end edits")))
4071
4072 (defun gnus-article-edit-done (&optional arg)
4073   "Update the article edits and exit."
4074   (interactive "P")
4075   (save-excursion
4076     (save-restriction
4077       (widen)
4078       (when (article-goto-body)
4079         (let ((lines (count-lines (point) (point-max)))
4080               (length (- (point-max) (point)))
4081               (case-fold-search t)
4082               (body (copy-marker (point))))
4083           (goto-char (point-min))
4084           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
4085             (delete-region (match-beginning 1) (match-end 1))
4086             (insert (number-to-string length)))
4087           (goto-char (point-min))
4088           (when (re-search-forward
4089                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
4090             (delete-region (match-beginning 1) (match-end 1))
4091             (insert (number-to-string length)))
4092           (goto-char (point-min))
4093           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
4094             (delete-region (match-beginning 1) (match-end 1))
4095             (insert (number-to-string lines)))))))
4096   (let ((func gnus-article-edit-done-function)
4097         (buf (current-buffer))
4098         (start (window-start)))
4099     (gnus-article-edit-exit)
4100     (save-excursion
4101       (set-buffer buf)
4102       (let ((buffer-read-only nil))
4103         (funcall func arg))
4104       ;; The cache and backlog have to be flushed somewhat.
4105       (when gnus-keep-backlog
4106         (gnus-backlog-remove-article
4107          (car gnus-article-current) (cdr gnus-article-current)))
4108       ;; Flush original article as well.
4109       (save-excursion
4110         (when (get-buffer gnus-original-article-buffer)
4111           (set-buffer gnus-original-article-buffer)
4112           (setq gnus-original-article nil)))
4113       (when gnus-use-cache
4114         (gnus-cache-update-article
4115          (car gnus-article-current) (cdr gnus-article-current))))
4116     (set-buffer buf)
4117     (set-window-start (get-buffer-window buf) start)
4118     (set-window-point (get-buffer-window buf) (point))))
4119
4120 (defun gnus-article-edit-exit ()
4121   "Exit the article editing without updating."
4122   (interactive)
4123   ;; We remove all text props from the article buffer.
4124   (let ((buf (buffer-substring-no-properties (point-min) (point-max)))
4125         (curbuf (current-buffer))
4126         (p (point))
4127         (window-start (window-start)))
4128     (erase-buffer)
4129     (insert buf)
4130     (let ((winconf gnus-prev-winconf))
4131       (gnus-article-mode)
4132       (set-window-configuration winconf)
4133       ;; Tippy-toe some to make sure that point remains where it was.
4134       (save-current-buffer
4135         (set-buffer curbuf)
4136         (set-window-start (get-buffer-window (current-buffer)) window-start)
4137         (goto-char p)))))
4138
4139 (defun gnus-article-edit-full-stops ()
4140   "Interactively repair spacing at end of sentences."
4141   (interactive)
4142   (save-excursion
4143     (goto-char (point-min))
4144     (search-forward-regexp "^$" nil t)
4145     (let ((case-fold-search nil))
4146       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
4147
4148 ;;;
4149 ;;; Article highlights
4150 ;;;
4151
4152 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
4153
4154 ;;; Internal Variables:
4155
4156 (defcustom gnus-button-url-regexp "\\b\\(\\(www\\.\\|\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\)\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?\\([-a-zA-Z0-9_=!?#$@~`%&*+|\\/:;.,]\\|\\w\\)+\\([-a-zA-Z0-9_=#$@~`%&*+|\\/]\\|\\w\\)\\)"
4157   "Regular expression that matches URLs."
4158   :group 'gnus-article-buttons
4159   :type 'regexp)
4160
4161 (defcustom gnus-button-alist
4162   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
4163      0 t gnus-button-message-id 2)
4164     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
4165     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
4166      1 t
4167      gnus-button-fetch-group 4)
4168     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
4169     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
4170      t gnus-button-message-id 3)
4171     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
4172     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
4173     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
4174     ;; This is how URLs _should_ be embedded in text...
4175     ("<URL: *\\([^<>]*\\)>" 0 t gnus-button-embedded-url 1)
4176     ;; Raw URLs.
4177     (,gnus-button-url-regexp 0 t browse-url 0))
4178   "*Alist of regexps matching buttons in article bodies.
4179
4180 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
4181 REGEXP: is the string matching text around the button,
4182 BUTTON: is the number of the regexp grouping actually matching the button,
4183 FORM: is a lisp expression which must eval to true for the button to
4184 be added,
4185 CALLBACK: is the function to call when the user push this button, and each
4186 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
4187
4188 CALLBACK can also be a variable, in that case the value of that
4189 variable it the real callback function."
4190   :group 'gnus-article-buttons
4191   :type '(repeat (list regexp
4192                        (integer :tag "Button")
4193                        (sexp :tag "Form")
4194                        (function :tag "Callback")
4195                        (repeat :tag "Par"
4196                                :inline t
4197                                (integer :tag "Regexp group")))))
4198
4199 (defcustom gnus-header-button-alist
4200   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
4201      0 t gnus-button-message-id 0)
4202     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
4203     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
4204      0 t gnus-button-mailto 0)
4205     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
4206     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
4207     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
4208     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
4209      gnus-button-message-id 3))
4210   "*Alist of headers and regexps to match buttons in article heads.
4211
4212 This alist is very similar to `gnus-button-alist', except that each
4213 alist has an additional HEADER element first in each entry:
4214
4215 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
4216
4217 HEADER is a regexp to match a header.  For a fuller explanation, see
4218 `gnus-button-alist'."
4219   :group 'gnus-article-buttons
4220   :group 'gnus-article-headers
4221   :type '(repeat (list (regexp :tag "Header")
4222                        regexp
4223                        (integer :tag "Button")
4224                        (sexp :tag "Form")
4225                        (function :tag "Callback")
4226                        (repeat :tag "Par"
4227                                :inline t
4228                                (integer :tag "Regexp group")))))
4229
4230 (defvar gnus-button-regexp nil)
4231 (defvar gnus-button-marker-list nil)
4232 ;; Regexp matching any of the regexps from `gnus-button-alist'.
4233
4234 (defvar gnus-button-last nil)
4235 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
4236
4237 ;;; Commands:
4238
4239 (defun gnus-article-push-button (event)
4240   "Check text under the mouse pointer for a callback function.
4241 If the text under the mouse pointer has a `gnus-callback' property,
4242 call it with the value of the `gnus-data' text property."
4243   (interactive "e")
4244   (set-buffer (window-buffer (posn-window (event-start event))))
4245   (let* ((pos (posn-point (event-start event)))
4246          (data (get-text-property pos 'gnus-data))
4247          (fun (get-text-property pos 'gnus-callback)))
4248     (goto-char pos)
4249     (when fun
4250       (funcall fun data))))
4251
4252 (defun gnus-article-press-button ()
4253   "Check text at point for a callback function.
4254 If the text at point has a `gnus-callback' property,
4255 call it with the value of the `gnus-data' text property."
4256   (interactive)
4257   (let* ((data (get-text-property (point) 'gnus-data))
4258          (fun (get-text-property (point) 'gnus-callback)))
4259     (when fun
4260       (funcall fun data))))
4261
4262 (defun gnus-article-highlight (&optional force)
4263   "Highlight current article.
4264 This function calls `gnus-article-highlight-headers',
4265 `gnus-article-highlight-citation',
4266 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4267 do the highlighting.  See the documentation for those functions."
4268   (interactive (list 'force))
4269   (gnus-article-highlight-headers)
4270   (gnus-article-highlight-citation force)
4271   (gnus-article-highlight-signature)
4272   (gnus-article-add-buttons force)
4273   (gnus-article-add-buttons-to-head))
4274
4275 (defun gnus-article-highlight-some (&optional force)
4276   "Highlight current article.
4277 This function calls `gnus-article-highlight-headers',
4278 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4279 do the highlighting.  See the documentation for those functions."
4280   (interactive (list 'force))
4281   (gnus-article-highlight-headers)
4282   (gnus-article-highlight-signature)
4283   (gnus-article-add-buttons))
4284
4285 (defun gnus-article-highlight-headers ()
4286   "Highlight article headers as specified by `gnus-header-face-alist'."
4287   (interactive)
4288   (save-excursion
4289     (set-buffer gnus-article-buffer)
4290     (save-restriction
4291       (let ((alist gnus-header-face-alist)
4292             (buffer-read-only nil)
4293             (case-fold-search t)
4294             (inhibit-point-motion-hooks t)
4295             entry regexp header-face field-face from hpoints fpoints)
4296         (article-narrow-to-head)
4297         (while (setq entry (pop alist))
4298           (goto-char (point-min))
4299           (setq regexp (concat "^\\("
4300                                (if (string-equal "" (nth 0 entry))
4301                                    "[^\t ]"
4302                                  (nth 0 entry))
4303                                "\\)")
4304                 header-face (nth 1 entry)
4305                 field-face (nth 2 entry))
4306           (while (and (re-search-forward regexp nil t)
4307                       (not (eobp)))
4308             (beginning-of-line)
4309             (setq from (point))
4310             (unless (search-forward ":" nil t)
4311               (forward-char 1))
4312             (when (and header-face
4313                        (not (memq (point) hpoints)))
4314               (push (point) hpoints)
4315               (gnus-put-text-property from (point) 'face header-face))
4316             (when (and field-face
4317                        (not (memq (setq from (point)) fpoints)))
4318               (push from fpoints)
4319               (if (re-search-forward "^[^ \t]" nil t)
4320                   (forward-char -2)
4321                 (goto-char (point-max)))
4322               (gnus-put-text-property from (point) 'face field-face))))))))
4323
4324 (defun gnus-article-highlight-signature ()
4325   "Highlight the signature in an article.
4326 It does this by highlighting everything after
4327 `gnus-signature-separator' using `gnus-signature-face'."
4328   (interactive)
4329   (save-excursion
4330     (set-buffer gnus-article-buffer)
4331     (let ((buffer-read-only nil)
4332           (inhibit-point-motion-hooks t))
4333       (save-restriction
4334         (when (and gnus-signature-face
4335                    (gnus-article-narrow-to-signature))
4336           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
4337                             'face gnus-signature-face)
4338           (widen)
4339           (gnus-article-search-signature)
4340           (let ((start (match-beginning 0))
4341                 (end (set-marker (make-marker) (1+ (match-end 0)))))
4342             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
4343                                      end)))))))
4344
4345 (defun gnus-button-in-region-p (b e prop)
4346   "Say whether PROP exists in the region."
4347   (text-property-not-all b e prop nil))
4348
4349 (defun gnus-article-add-buttons (&optional force)
4350   "Find external references in the article and make buttons of them.
4351 \"External references\" are things like Message-IDs and URLs, as
4352 specified by `gnus-button-alist'."
4353   (interactive (list 'force))
4354   (save-excursion
4355     (set-buffer gnus-article-buffer)
4356     (let ((buffer-read-only nil)
4357           (inhibit-point-motion-hooks t)
4358           (case-fold-search t)
4359           (alist gnus-button-alist)
4360           beg entry regexp)
4361       ;; Remove all old markers.
4362       (let (marker entry new-list)
4363         (while (setq marker (pop gnus-button-marker-list))
4364           (if (or (< marker (point-min)) (>= marker (point-max)))
4365               (push marker new-list)
4366             (goto-char marker)
4367             (when (setq entry (gnus-button-entry))
4368               (put-text-property (match-beginning (nth 1 entry))
4369                                  (match-end (nth 1 entry))
4370                                  'gnus-callback nil))
4371             (set-marker marker nil)))
4372         (setq gnus-button-marker-list new-list))
4373       ;; We skip the headers.
4374       (article-goto-body)
4375       (setq beg (point))
4376       (while (setq entry (pop alist))
4377         (setq regexp (car entry))
4378         (goto-char beg)
4379         (while (re-search-forward regexp nil t)
4380           (let* ((start (and entry (match-beginning (nth 1 entry))))
4381                  (end (and entry (match-end (nth 1 entry))))
4382                  (from (match-beginning 0)))
4383             (when (and (or (eq t (nth 2 entry))
4384                            (eval (nth 2 entry)))
4385                        (not (gnus-button-in-region-p
4386                              start end 'gnus-callback)))
4387               ;; That optional form returned non-nil, so we add the
4388               ;; button.
4389               (gnus-article-add-button
4390                start end 'gnus-button-push
4391                (car (push (set-marker (make-marker) from)
4392                           gnus-button-marker-list))))))))))
4393
4394 ;; Add buttons to the head of an article.
4395 (defun gnus-article-add-buttons-to-head ()
4396   "Add buttons to the head of the article."
4397   (interactive)
4398   (save-excursion
4399     (set-buffer gnus-article-buffer)
4400     (save-restriction
4401       (let ((buffer-read-only nil)
4402             (inhibit-point-motion-hooks t)
4403             (case-fold-search t)
4404             (alist gnus-header-button-alist)
4405             entry beg end)
4406         (article-narrow-to-head)
4407         (while alist
4408           ;; Each alist entry.
4409           (setq entry (car alist)
4410                 alist (cdr alist))
4411           (goto-char (point-min))
4412           (while (re-search-forward (car entry) nil t)
4413             ;; Each header matching the entry.
4414             (setq beg (match-beginning 0))
4415             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
4416                                (match-beginning 0))
4417                           (point-max)))
4418             (goto-char beg)
4419             (while (re-search-forward (nth 1 entry) end t)
4420               ;; Each match within a header.
4421               (let* ((entry (cdr entry))
4422                      (start (match-beginning (nth 1 entry)))
4423                      (end (match-end (nth 1 entry)))
4424                      (form (nth 2 entry)))
4425                 (goto-char (match-end 0))
4426                 (when (eval form)
4427                   (gnus-article-add-button
4428                    start end (nth 3 entry)
4429                    (buffer-substring (match-beginning (nth 4 entry))
4430                                      (match-end (nth 4 entry)))))))
4431             (goto-char end)))))))
4432
4433 ;;; External functions:
4434
4435 (defun gnus-article-add-button (from to fun &optional data)
4436   "Create a button between FROM and TO with callback FUN and data DATA."
4437   (when gnus-article-button-face
4438     (gnus-overlay-put (gnus-make-overlay from to)
4439                       'face gnus-article-button-face))
4440   (gnus-add-text-properties
4441    from to
4442    (nconc (and gnus-article-mouse-face
4443                (list gnus-mouse-face-prop gnus-article-mouse-face))
4444           (list 'gnus-callback fun)
4445           (and data (list 'gnus-data data))))
4446   (widget-convert-button 'link from to :action 'gnus-widget-press-button
4447                          :button-keymap gnus-widget-button-keymap))
4448
4449 ;;; Internal functions:
4450
4451 (defun gnus-article-set-globals ()
4452   (save-excursion
4453     (set-buffer gnus-summary-buffer)
4454     (gnus-set-global-variables)))
4455
4456 (defun gnus-signature-toggle (end)
4457   (save-excursion
4458     (set-buffer gnus-article-buffer)
4459     (let ((buffer-read-only nil)
4460           (inhibit-point-motion-hooks t))
4461       (if (get-text-property end 'invisible)
4462           (gnus-article-unhide-text end (point-max))
4463         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
4464
4465 (defun gnus-button-entry ()
4466   ;; Return the first entry in `gnus-button-alist' matching this place.
4467   (let ((alist gnus-button-alist)
4468         (entry nil))
4469     (while alist
4470       (setq entry (pop alist))
4471       (if (looking-at (car entry))
4472           (setq alist nil)
4473         (setq entry nil)))
4474     entry))
4475
4476 (defun gnus-button-push (marker)
4477   ;; Push button starting at MARKER.
4478   (save-excursion
4479     (goto-char marker)
4480     (let* ((entry (gnus-button-entry))
4481            (inhibit-point-motion-hooks t)
4482            (fun (nth 3 entry))
4483            (args (mapcar (lambda (group)
4484                            (let ((string (match-string group)))
4485                              (gnus-set-text-properties
4486                               0 (length string) nil string)
4487                              string))
4488                          (nthcdr 4 entry))))
4489       (cond
4490        ((fboundp fun)
4491         (apply fun args))
4492        ((and (boundp fun)
4493              (fboundp (symbol-value fun)))
4494         (apply (symbol-value fun) args))
4495        (t
4496         (gnus-message 1 "You must define `%S' to use this button"
4497                       (cons fun args)))))))
4498
4499 (defun gnus-button-message-id (message-id)
4500   "Fetch MESSAGE-ID."
4501   (save-excursion
4502     (set-buffer gnus-summary-buffer)
4503     (gnus-summary-refer-article message-id)))
4504
4505 (defun gnus-button-fetch-group (address)
4506   "Fetch GROUP specified by ADDRESS."
4507   (if (not (string-match "[:/]" address))
4508       ;; This is just a simple group url.
4509       (gnus-group-read-ephemeral-group address gnus-select-method)
4510     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
4511                            address))
4512         (error "Can't parse %s" address)
4513       (gnus-group-read-ephemeral-group
4514        (match-string 4 address)
4515        `(nntp ,(match-string 1 address)
4516               (nntp-address ,(match-string 1 address))
4517               (nntp-port-number ,(if (match-end 3)
4518                                      (match-string 3 address)
4519                                    "nntp")))))))
4520
4521 (defun gnus-url-parse-query-string (query &optional downcase)
4522   (let (retval pairs cur key val)
4523     (setq pairs (split-string query "&"))
4524     (while pairs
4525       (setq cur (car pairs)
4526             pairs (cdr pairs))
4527       (if (not (string-match "=" cur))
4528           nil                           ; Grace
4529         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
4530               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
4531         (if downcase
4532             (setq key (downcase key)))
4533         (setq cur (assoc key retval))
4534         (if cur
4535             (setcdr cur (cons val (cdr cur)))
4536           (setq retval (cons (list key val) retval)))))
4537     retval))
4538
4539 (defun gnus-url-unhex (x)
4540   (if (> x ?9)
4541       (if (>= x ?a)
4542           (+ 10 (- x ?a))
4543         (+ 10 (- x ?A)))
4544     (- x ?0)))
4545
4546 (defun gnus-url-unhex-string (str &optional allow-newlines)
4547   "Remove %XXX embedded spaces, etc in a url.
4548 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
4549 decoding of carriage returns and line feeds in the string, which is normally
4550 forbidden in URL encoding."
4551   (setq str (or str ""))
4552   (let ((tmp "")
4553         (case-fold-search t))
4554     (while (string-match "%[0-9a-f][0-9a-f]" str)
4555       (let* ((start (match-beginning 0))
4556              (ch1 (gnus-url-unhex (elt str (+ start 1))))
4557              (code (+ (* 16 ch1)
4558                       (gnus-url-unhex (elt str (+ start 2))))))
4559         (setq tmp (concat
4560                    tmp (substring str 0 start)
4561                    (cond
4562                     (allow-newlines
4563                      (char-to-string code))
4564                     ((or (= code ?\n) (= code ?\r))
4565                      " ")
4566                     (t (char-to-string code))))
4567               str (substring str (match-end 0)))))
4568     (setq tmp (concat tmp str))
4569     tmp))
4570
4571 (defun gnus-url-mailto (url)
4572   ;; Send mail to someone
4573   (when (string-match "mailto:/*\\(.*\\)" url)
4574     (setq url (substring url (match-beginning 1) nil)))
4575   (let (to args subject func)
4576     (if (string-match (regexp-quote "?") url)
4577         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
4578               args (gnus-url-parse-query-string
4579                     (substring url (match-end 0) nil) t))
4580       (setq to (gnus-url-unhex-string url)))
4581     (setq args (cons (list "to" to) args)
4582           subject (cdr-safe (assoc "subject" args)))
4583     (message-mail)
4584     (while args
4585       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
4586       (if (fboundp func)
4587           (funcall func)
4588         (message-position-on-field (caar args)))
4589       (insert (mapconcat 'identity (cdar args) ", "))
4590       (setq args (cdr args)))
4591     (if subject
4592         (message-goto-body)
4593       (message-goto-subject))))
4594
4595 (defun gnus-button-mailto (address)
4596   ;; Mail to ADDRESS.
4597   (set-buffer (gnus-copy-article-buffer))
4598   (message-reply address))
4599
4600 (defun gnus-button-reply (address)
4601   ;; Reply to ADDRESS.
4602   (message-reply address))
4603
4604 (defun gnus-button-embedded-url (address)
4605   "Browse ADDRESS."
4606   (browse-url (gnus-strip-whitespace address)))
4607
4608 ;;; Next/prev buttons in the article buffer.
4609
4610 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
4611 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
4612
4613 (defvar gnus-prev-page-map nil)
4614 (unless gnus-prev-page-map
4615   (setq gnus-prev-page-map (make-sparse-keymap))
4616   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
4617   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
4618
4619 (defun gnus-insert-prev-page-button ()
4620   (let ((buffer-read-only nil))
4621     (gnus-eval-format
4622      gnus-prev-page-line-format nil
4623      `(gnus-prev t local-map ,gnus-prev-page-map
4624                  gnus-callback gnus-article-button-prev-page
4625                  article-type annotation))))
4626
4627 (defvar gnus-next-page-map nil)
4628 (unless gnus-next-page-map
4629   (setq gnus-next-page-map (make-keymap))
4630   (suppress-keymap gnus-prev-page-map)
4631   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
4632   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
4633
4634 (defun gnus-button-next-page ()
4635   "Go to the next page."
4636   (interactive)
4637   (let ((win (selected-window)))
4638     (select-window (get-buffer-window gnus-article-buffer t))
4639     (gnus-article-next-page)
4640     (select-window win)))
4641
4642 (defun gnus-button-prev-page ()
4643   "Go to the prev page."
4644   (interactive)
4645   (let ((win (selected-window)))
4646     (select-window (get-buffer-window gnus-article-buffer t))
4647     (gnus-article-prev-page)
4648     (select-window win)))
4649
4650 (defun gnus-insert-next-page-button ()
4651   (let ((buffer-read-only nil))
4652     (gnus-eval-format gnus-next-page-line-format nil
4653                       `(gnus-next
4654                         t local-map ,gnus-next-page-map
4655                         gnus-callback gnus-article-button-next-page
4656                         article-type annotation))))
4657
4658 (defun gnus-article-button-next-page (arg)
4659   "Go to the next page."
4660   (interactive "P")
4661   (let ((win (selected-window)))
4662     (select-window (get-buffer-window gnus-article-buffer t))
4663     (gnus-article-next-page)
4664     (select-window win)))
4665
4666 (defun gnus-article-button-prev-page (arg)
4667   "Go to the prev page."
4668   (interactive "P")
4669   (let ((win (selected-window)))
4670     (select-window (get-buffer-window gnus-article-buffer t))
4671     (gnus-article-prev-page)
4672     (select-window win)))
4673
4674 (defvar gnus-decode-header-methods
4675   '(mail-decode-encoded-word-region)
4676   "List of methods used to decode headers.
4677
4678 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
4679 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
4680 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
4681 whose names match REGEXP.
4682
4683 For example:
4684 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
4685  mail-decode-encoded-word-region
4686  (\"chinese\" . rfc1843-decode-region))
4687 ")
4688
4689 (defvar gnus-decode-header-methods-cache nil)
4690
4691 (defun gnus-multi-decode-header (start end)
4692   "Apply the functions from `gnus-encoded-word-methods' that match."
4693   (unless (and gnus-decode-header-methods-cache
4694                (eq gnus-newsgroup-name
4695                    (car gnus-decode-header-methods-cache)))
4696     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
4697     (mapcar (lambda (x)
4698               (if (symbolp x)
4699                   (nconc gnus-decode-header-methods-cache (list x))
4700                 (if (and gnus-newsgroup-name
4701                          (string-match (car x) gnus-newsgroup-name))
4702                     (nconc gnus-decode-header-methods-cache
4703                            (list (cdr x))))))
4704           gnus-decode-header-methods))
4705   (let ((xlist gnus-decode-header-methods-cache))
4706     (pop xlist)
4707     (save-restriction
4708       (narrow-to-region start end)
4709       (while xlist
4710         (funcall (pop xlist) (point-min) (point-max))))))
4711
4712 ;;;
4713 ;;; Treatment top-level handling.
4714 ;;;
4715
4716 (defun gnus-treat-article (condition &optional part-number total-parts type)
4717   (let ((length (- (point-max) (point-min)))
4718         (alist gnus-treatment-function-alist)
4719         (article-goto-body-goes-to-point-min-p t)
4720         (treated-type
4721          (or (not type)
4722              (catch 'found
4723                (let ((list gnus-article-treat-types))
4724                  (while list
4725                    (when (string-match (pop list) type)
4726                      (throw 'found t)))))))
4727         (highlightp (gnus-visual-p 'article-highlight 'highlight))
4728         val elem)
4729     (gnus-run-hooks 'gnus-part-display-hook)
4730     (while (setq elem (pop alist))
4731       (setq val
4732             (save-excursion
4733               (if (gnus-buffer-live-p gnus-summary-buffer)
4734                   (set-buffer gnus-summary-buffer))
4735               (symbol-value (car elem))))
4736       (when (and (or (consp val)
4737                      treated-type)
4738                  (gnus-treat-predicate val)
4739                  (or (not (get (car elem) 'highlight))
4740                      highlightp))
4741         (save-restriction
4742           (funcall (cadr elem)))))))
4743
4744 ;; Dynamic variables.
4745 (defvar part-number)
4746 (defvar total-parts)
4747 (defvar type)
4748 (defvar condition)
4749 (defvar length)
4750 (defun gnus-treat-predicate (val)
4751   (cond
4752    ((null val)
4753     nil)
4754    ((and (listp val)
4755          (stringp (car val)))
4756     (apply 'gnus-or (mapcar `(lambda (s)
4757                                (string-match s ,(or gnus-newsgroup-name "")))
4758                             val)))
4759    ((listp val)
4760     (let ((pred (pop val)))
4761       (cond
4762        ((eq pred 'or)
4763         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
4764        ((eq pred 'and)
4765         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
4766        ((eq pred 'not)
4767         (not (gnus-treat-predicate (car val))))
4768        ((eq pred 'typep)
4769         (equal (car val) type))
4770        (t
4771         (error "%S is not a valid predicate" pred)))))
4772    (condition
4773     (eq condition val))
4774    ((eq val t)
4775     t)
4776    ((eq val 'head)
4777     nil)
4778    ((eq val 'last)
4779     (eq part-number total-parts))
4780    ((numberp val)
4781     (< length val))
4782    (t
4783     (error "%S is not a valid value" val))))
4784
4785 (gnus-ems-redefine)
4786
4787 (provide 'gnus-art)
4788
4789 (run-hooks 'gnus-art-load-hook)
4790
4791 ;;; gnus-art.el ends here