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