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