000-10-27 Dave Love <fx@gnu.org>
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (eval-when-compile (require 'cl))
29
30 (require 'gnus)
31 (require 'gnus-sum)
32 (require 'gnus-spec)
33 (require 'gnus-int)
34 (require 'mm-bodies)
35 (require 'mail-parse)
36 (require 'mm-decode)
37 (require 'mm-view)
38 (require 'wid-edit)
39 (require 'mm-uu)
40
41 (defgroup gnus-article nil
42   "Article display."
43   :link '(custom-manual "(gnus)The Article Buffer")
44   :group 'gnus)
45
46 (defgroup gnus-article-treat nil
47   "Treating article parts."
48   :link '(custom-manual "(gnus)Article Hiding")
49   :group 'gnus-article)
50
51 (defgroup gnus-article-hiding nil
52   "Hiding article parts."
53   :link '(custom-manual "(gnus)Article Hiding")
54   :group 'gnus-article)
55
56 (defgroup gnus-article-highlight nil
57   "Article highlighting."
58   :link '(custom-manual "(gnus)Article Highlighting")
59   :group 'gnus-article
60   :group 'gnus-visual)
61
62 (defgroup gnus-article-signature nil
63   "Article signatures."
64   :link '(custom-manual "(gnus)Article Signature")
65   :group 'gnus-article)
66
67 (defgroup gnus-article-headers nil
68   "Article headers."
69   :link '(custom-manual "(gnus)Hiding Headers")
70   :group 'gnus-article)
71
72 (defgroup gnus-article-washing nil
73   "Special commands on articles."
74   :link '(custom-manual "(gnus)Article Washing")
75   :group 'gnus-article)
76
77 (defgroup gnus-article-emphasis nil
78   "Fontisizing articles."
79   :link '(custom-manual "(gnus)Article Fontisizing")
80   :group 'gnus-article)
81
82 (defgroup gnus-article-saving nil
83   "Saving articles."
84   :link '(custom-manual "(gnus)Saving Articles")
85   :group 'gnus-article)
86
87 (defgroup gnus-article-mime nil
88   "Worshiping the MIME wonder."
89   :link '(custom-manual "(gnus)Using MIME")
90   :group 'gnus-article)
91
92 (defgroup gnus-article-buttons nil
93   "Pushable buttons in the article buffer."
94   :link '(custom-manual "(gnus)Article Buttons")
95   :group 'gnus-article)
96
97 (defgroup gnus-article-various nil
98   "Other article options."
99   :link '(custom-manual "(gnus)Misc Article")
100   :group 'gnus-article)
101
102 (defcustom gnus-ignored-headers
103   '("^Path:" "^Expires:" "^Date-Received:" "^References:" "^Xref:" "^Lines:"
104     "^Relay-Version:" "^Message-ID:" "^Approved:" "^Sender:" "^Received:"
105     "^X-UIDL:" "^MIME-Version:" "^Return-Path:" "^In-Reply-To:"
106     "^Content-Type:" "^Content-Transfer-Encoding:" "^X-WebTV-Signature:"
107     "^X-MimeOLE:" "^X-MSMail-Priority:" "^X-Priority:" "^X-Loop:"
108     "^X-Authentication-Warning:" "^X-MIME-Autoconverted:" "^X-Face:"
109     "^X-Attribution:" "^X-Originating-IP:" "^Delivered-To:"
110     "^NNTP-[-A-Za-z]+:" "^Distribution:" "^X-no-archive:" "^X-Trace:"
111     "^X-Complaints-To:" "^X-NNTP-Posting-Host:" "^X-Orig.*:"
112     "^Abuse-Reports-To:" "^Cache-Post-Path:" "^X-Article-Creation-Date:"
113     "^X-Poster:" "^X-Mail2News-Path:" "^X-Server-Date:" "^X-Cache:"
114     "^Originator:" "^X-Problems-To:" "^X-Auth-User:" "^X-Post-Time:"
115     "^X-Admin:" "^X-UID:" "^Resent-[-A-Za-z]+:" "^X-Mailing-List:"
116     "^Precedence:" "^Original-[-A-Za-z]+:" "^X-filename:" "^X-Orcpt:"
117     "^Old-Received:" "^X-Pgp" "^X-Auth:" "^X-From-Line:"
118     "^X-Gnus-Article-Number:" "^X-Majordomo:" "^X-Url:" "^X-Sender:"
119     "^MBOX-Line" "^Priority:" "^X-Pgp" "^X400-[-A-Za-z]+:"
120     "^Status:" "^X-Gnus-Mail-Source:" "^Cancel-Lock:"
121     "^X-FTN" "^X-EXP32-SerialNo:" "^Encoding:" "^Importance:"
122     "^Autoforwarded:" "^Original-Encoded-Information-Types:" "^X-Ya-Pop3:"
123     "^X-Face-Version:" "^X-Vms-To:" "^X-ML-NAME:" "^X-ML-COUNT:"
124     "^Mailing-List:" "^X-finfo:" "^X-md5sum:" "^X-md5sum-Origin:"
125     "^X-Sun-Charset:" "^X-Accept-Language:" "^X-Envelope-Sender:"
126     "^List-[A-Za-z]+:" "^X-Listprocessor-Version:"
127     "^X-Received:" "^X-Distribute:" "^X-Sequence:" "^X-Juno-Line-Breaks:"
128     "^X-Notes-Item:" "^X-MS-TNEF-Correlator:" "^x-uunet-gateway:"
129     "^X-Received:" "^Content-length:" "X-precedence:")
130   "*All headers that start with this regexp will be hidden.
131 This variable can also be a list of regexps of headers to be ignored.
132 If `gnus-visible-headers' is non-nil, this variable will be ignored."
133   :type '(choice :custom-show nil
134                  regexp
135                  (repeat regexp))
136   :group 'gnus-article-hiding)
137
138 (defcustom gnus-visible-headers
139   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
140   "*All headers that do not match this regexp will be hidden.
141 This variable can also be a list of regexp of headers to remain visible.
142 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
143   :type '(repeat :value-to-internal (lambda (widget value)
144                                       (custom-split-regexp-maybe value))
145                  :match (lambda (widget value)
146                           (or (stringp value)
147                               (widget-editable-list-match widget value)))
148                  regexp)
149   :group 'gnus-article-hiding)
150
151 (defcustom gnus-sorted-header-list
152   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
153     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
154   "*This variable is a list of regular expressions.
155 If it is non-nil, headers that match the regular expressions will
156 be placed first in the article buffer in the sequence specified by
157 this list."
158   :type '(repeat regexp)
159   :group 'gnus-article-hiding)
160
161 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
162   "Headers that are only to be displayed if they have interesting data.
163 Possible values in this list are `empty', `newsgroups', `followup-to',
164 `reply-to', `date', `long-to', and `many-to'."
165   :type '(set (const :tag "Headers with no content." empty)
166               (const :tag "Newsgroups with only one group." newsgroups)
167               (const :tag "Followup-to identical to newsgroups." followup-to)
168               (const :tag "Reply-to identical to from." reply-to)
169               (const :tag "Date less than four days old." date)
170               (const :tag "Very long To and/or Cc header." long-to)
171               (const :tag "Multiple To and/or Cc headers." many-to))
172   :group 'gnus-article-hiding)
173
174 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
175   "Regexp matching signature separator.
176 This can also be a list of regexps.  In that case, it will be checked
177 from head to tail looking for a separator.  Searches will be done from
178 the end of the buffer."
179   :type '(repeat string)
180   :group 'gnus-article-signature)
181
182 (defcustom gnus-signature-limit nil
183   "Provide a limit to what is considered a signature.
184 If it is a number, no signature may not be longer (in characters) than
185 that number.  If it is a floating point number, no signature may be
186 longer (in lines) than that number.  If it is a function, the function
187 will be called without any parameters, and if it returns nil, there is
188 no signature in the buffer.  If it is a string, it will be used as a
189 regexp.  If it matches, the text in question is not a signature."
190   :type '(choice (integer :value 200)
191                  (number :value 4.0)
192                  (function :value fun)
193                  (regexp :value ".*"))
194   :group 'gnus-article-signature)
195
196 (defcustom gnus-hidden-properties '(invisible t intangible t)
197   "Property list to use for hiding text."
198   :type 'sexp
199   :group 'gnus-article-hiding)
200
201 ;; Fixme: This isn't the right thing for mixed graphical and and
202 ;; non-graphical frames in a session.
203 ;; gnus-xmas.el overrides this for XEmacs.
204 (defcustom gnus-article-x-face-command
205   (if (and (fboundp 'image-type-available-p)
206            (image-type-available-p 'xbm))
207       'gnus-article-display-xface
208     (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   (gnus-set-default-directory)
2705   (buffer-disable-undo)
2706   (setq buffer-read-only t)
2707   (set-syntax-table gnus-article-mode-syntax-table)
2708   (mm-enable-multibyte)
2709   (gnus-run-hooks 'gnus-article-mode-hook))
2710
2711 (defun gnus-article-setup-buffer ()
2712   "Initialize the article buffer."
2713   (let* ((name (if gnus-single-article-buffer "*Article*"
2714                  (concat "*Article " gnus-newsgroup-name "*")))
2715          (original
2716           (progn (string-match "\\*Article" name)
2717                  (concat " *Original Article"
2718                          (substring name (match-end 0))))))
2719     (setq gnus-article-buffer name)
2720     (setq gnus-original-article-buffer original)
2721     (setq gnus-article-mime-handle-alist nil)
2722     ;; This might be a variable local to the summary buffer.
2723     (unless gnus-single-article-buffer
2724       (save-excursion
2725         (set-buffer gnus-summary-buffer)
2726         (setq gnus-article-buffer name)
2727         (setq gnus-original-article-buffer original)
2728         (gnus-set-global-variables)))
2729     (gnus-article-setup-highlight-words)
2730     ;; Init original article buffer.
2731     (save-excursion
2732       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2733       (mm-enable-multibyte)
2734       (setq major-mode 'gnus-original-article-mode)
2735       (make-local-variable 'gnus-original-article))
2736     (if (get-buffer name)
2737         (save-excursion
2738           (set-buffer name)
2739           (when gnus-article-mime-handles
2740             (mm-destroy-parts gnus-article-mime-handles)
2741             (setq gnus-article-mime-handles nil))
2742           ;; Set it to nil in article-buffer!
2743           (setq gnus-article-mime-handle-alist nil) 
2744           (buffer-disable-undo)
2745           (setq buffer-read-only t)
2746           (unless (eq major-mode 'gnus-article-mode)
2747             (gnus-article-mode))
2748           (current-buffer))
2749       (save-excursion
2750         (set-buffer (gnus-get-buffer-create name))
2751         (gnus-article-mode)
2752         (make-local-variable 'gnus-summary-buffer)
2753         (gnus-summary-set-local-parameters gnus-newsgroup-name)
2754         (current-buffer)))))
2755
2756 ;; Set article window start at LINE, where LINE is the number of lines
2757 ;; from the head of the article.
2758 (defun gnus-article-set-window-start (&optional line)
2759   (set-window-start
2760    (get-buffer-window gnus-article-buffer t)
2761    (save-excursion
2762      (set-buffer gnus-article-buffer)
2763      (goto-char (point-min))
2764      (if (not line)
2765          (point-min)
2766        (gnus-message 6 "Moved to bookmark")
2767        (search-forward "\n\n" nil t)
2768        (forward-line line)
2769        (point)))))
2770
2771 (defun gnus-article-prepare (article &optional all-headers header)
2772   "Prepare ARTICLE in article mode buffer.
2773 ARTICLE should either be an article number or a Message-ID.
2774 If ARTICLE is an id, HEADER should be the article headers.
2775 If ALL-HEADERS is non-nil, no headers are hidden."
2776   (save-excursion
2777     ;; Make sure we start in a summary buffer.
2778     (unless (eq major-mode 'gnus-summary-mode)
2779       (set-buffer gnus-summary-buffer))
2780     (setq gnus-summary-buffer (current-buffer))
2781     (let* ((gnus-article (if header (mail-header-number header) article))
2782            (summary-buffer (current-buffer))
2783            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
2784            (group gnus-newsgroup-name)
2785            result)
2786       (save-excursion
2787         (gnus-article-setup-buffer)
2788         (set-buffer gnus-article-buffer)
2789         ;; Deactivate active regions.
2790         (when (and (boundp 'transient-mark-mode)
2791                    transient-mark-mode)
2792           (setq mark-active nil))
2793         (if (not (setq result (let ((buffer-read-only nil))
2794                                 (gnus-request-article-this-buffer
2795                                  article group))))
2796             ;; There is no such article.
2797             (save-excursion
2798               (when (and (numberp article)
2799                          (not (memq article gnus-newsgroup-sparse)))
2800                 (setq gnus-article-current
2801                       (cons gnus-newsgroup-name article))
2802                 (set-buffer gnus-summary-buffer)
2803                 (setq gnus-current-article article)
2804                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2805                     (progn
2806                       (gnus-summary-set-agent-mark article)
2807                       (message "Message marked for downloading"))
2808                   (gnus-summary-mark-article article gnus-canceled-mark)
2809                   (unless (memq article gnus-newsgroup-sparse)
2810                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
2811           (if (or (eq result 'pseudo)
2812                   (eq result 'nneething))
2813               (progn
2814                 (save-excursion
2815                   (set-buffer summary-buffer)
2816                   (push article gnus-newsgroup-history)
2817                   (setq gnus-last-article gnus-current-article
2818                         gnus-current-article 0
2819                         gnus-current-headers nil
2820                         gnus-article-current nil)
2821                   (if (eq result 'nneething)
2822                       (gnus-configure-windows 'summary)
2823                     (gnus-configure-windows 'article))
2824                   (gnus-set-global-variables))
2825                 (let ((gnus-article-mime-handle-alist-1
2826                        gnus-article-mime-handle-alist))
2827                   (gnus-set-mode-line 'article)))
2828             ;; The result from the `request' was an actual article -
2829             ;; or at least some text that is now displayed in the
2830             ;; article buffer.
2831             (when (and (numberp article)
2832                        (not (eq article gnus-current-article)))
2833               ;; Seems like a new article has been selected.
2834               ;; `gnus-current-article' must be an article number.
2835               (save-excursion
2836                 (set-buffer summary-buffer)
2837                 (push article gnus-newsgroup-history)
2838                 (setq gnus-last-article gnus-current-article
2839                       gnus-current-article article
2840                       gnus-current-headers
2841                       (gnus-summary-article-header gnus-current-article)
2842                       gnus-article-current
2843                       (cons gnus-newsgroup-name gnus-current-article))
2844                 (unless (vectorp gnus-current-headers)
2845                   (setq gnus-current-headers nil))
2846                 (gnus-summary-goto-subject gnus-current-article)
2847                 (when (gnus-summary-show-thread)
2848                   ;; If the summary buffer really was folded, the
2849                   ;; previous goto may not actually have gone to
2850                   ;; the right article, but the thread root instead.
2851                   ;; So we go again.
2852                   (gnus-summary-goto-subject gnus-current-article))
2853                 (gnus-run-hooks 'gnus-mark-article-hook)
2854                 (gnus-set-mode-line 'summary)
2855                 (when (gnus-visual-p 'article-highlight 'highlight)
2856                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2857                 ;; Set the global newsgroup variables here.
2858                 (gnus-set-global-variables)
2859                 (setq gnus-have-all-headers
2860                       (or all-headers gnus-show-all-headers))))
2861             (save-excursion
2862               (gnus-configure-windows 'article))
2863             (when (or (numberp article)
2864                       (stringp article))
2865               (gnus-article-prepare-display)
2866               ;; Do page break.
2867               (goto-char (point-min))
2868               (setq gnus-page-broken
2869                     (when gnus-break-pages
2870                       (gnus-narrow-to-page)
2871                       t)))
2872             (let ((gnus-article-mime-handle-alist-1
2873                    gnus-article-mime-handle-alist))
2874               (gnus-set-mode-line 'article))
2875             (article-goto-body)
2876             (set-window-point (get-buffer-window (current-buffer)) (point))
2877             (gnus-configure-windows 'article)
2878             t))))))
2879
2880 ;;;###autoload
2881 (defun gnus-article-prepare-display ()
2882   "Make the current buffer look like a nice article."
2883   ;; Hooks for getting information from the article.
2884   ;; This hook must be called before being narrowed.
2885   (let ((gnus-article-buffer (current-buffer))
2886         buffer-read-only)
2887     (unless (eq major-mode 'gnus-article-mode)
2888       (gnus-article-mode))
2889     (setq buffer-read-only nil
2890           gnus-article-wash-types nil)
2891     (gnus-run-hooks 'gnus-tmp-internal-hook)
2892     (when gnus-display-mime-function
2893       (funcall gnus-display-mime-function))
2894     (gnus-run-hooks 'gnus-article-prepare-hook)))
2895
2896 ;;;
2897 ;;; Gnus MIME viewing functions
2898 ;;;
2899
2900 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
2901   "The following specs can be used:
2902 %t  The MIME type
2903 %T  MIME type, along with additional info
2904 %n  The `name' parameter
2905 %d  The description, if any
2906 %l  The length of the encoded part
2907 %p  The part identifier number
2908 %e  Dots if the part isn't displayed")
2909
2910 (defvar gnus-mime-button-line-format-alist
2911   '((?t gnus-tmp-type ?s)
2912     (?T gnus-tmp-type-long ?s)
2913     (?n gnus-tmp-name ?s)
2914     (?d gnus-tmp-description ?s)
2915     (?p gnus-tmp-id ?s)
2916     (?l gnus-tmp-length ?d)
2917     (?e gnus-tmp-dots ?s)))
2918
2919 (defvar gnus-mime-button-commands
2920   '((gnus-article-press-button "\r" "Toggle Display")
2921     (gnus-mime-view-part "v" "View Interactively...")
2922     (gnus-mime-view-part-as-type "t" "View As Type...")
2923     (gnus-mime-save-part "o" "Save...")
2924     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
2925     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
2926     (gnus-mime-internalize-part "E" "View Internally")
2927     (gnus-mime-externalize-part "e" "View Externally")
2928     (gnus-mime-pipe-part "|" "Pipe To Command...")
2929     (gnus-mime-action-on-part "." "Take action on the part")))
2930
2931 (defun gnus-article-mime-part-status ()
2932   (if gnus-article-mime-handle-alist-1
2933       (format " (%d parts)" (length gnus-article-mime-handle-alist-1))
2934     ""))
2935
2936 (defvar gnus-mime-button-map
2937   (let ((map (make-sparse-keymap)))
2938     (set-keymap-parent map gnus-article-mode-map)
2939     (define-key map gnus-mouse-2 'gnus-article-push-button)
2940     (define-key map gnus-down-mouse-3 'gnus-mime-button-menu)
2941     (dolist (c gnus-mime-button-commands)
2942       (define-key map (cadr c) (car c)))
2943     map))
2944
2945 (defun gnus-mime-button-menu (event)
2946   "Construct a context-sensitive menu of MIME commands."
2947   (interactive "e")
2948   (save-excursion
2949     (let ((pos (event-start event)))
2950       (set-buffer (window-buffer (posn-window pos)))
2951       (goto-char (posn-point pos))
2952       (gnus-article-check-buffer)
2953       (let ((response (x-popup-menu
2954                        t `("MIME Part"
2955                            ("" ,@(mapcar (lambda (c)
2956                                            (cons (caddr c) (car c)))
2957                                          gnus-mime-button-commands))))))
2958         (if response
2959             (call-interactively response))))))
2960
2961 (defun gnus-mime-view-all-parts (&optional handles)
2962   "View all the MIME parts."
2963   (interactive)
2964   (save-current-buffer
2965     (set-buffer gnus-article-buffer)
2966     (let ((handles (or handles gnus-article-mime-handles))
2967           (mail-parse-charset gnus-newsgroup-charset)
2968           (mail-parse-ignored-charsets 
2969            (save-excursion (set-buffer gnus-summary-buffer)
2970                            gnus-newsgroup-ignored-charsets)))
2971       (if (stringp (car handles))
2972           (gnus-mime-view-all-parts (cdr handles))
2973         (mapcar 'mm-display-part handles)))))
2974
2975 (defun gnus-mime-save-part ()
2976   "Save the MIME part under point."
2977   (interactive)
2978   (gnus-article-check-buffer)
2979   (let ((data (get-text-property (point) 'gnus-data)))
2980     (mm-save-part data)))
2981
2982 (defun gnus-mime-pipe-part ()
2983   "Pipe the MIME part under point to a process."
2984   (interactive)
2985   (gnus-article-check-buffer)
2986   (let ((data (get-text-property (point) 'gnus-data)))
2987     (mm-pipe-part data)))
2988
2989 (defun gnus-mime-view-part ()
2990   "Interactively choose a viewing method for the MIME part under point."
2991   (interactive)
2992   (gnus-article-check-buffer)
2993   (let ((data (get-text-property (point) 'gnus-data)))
2994     (mm-interactively-view-part data)))
2995
2996 (defun gnus-mime-view-part-as-type-internal ()
2997   (gnus-article-check-buffer)
2998   (let* ((name (mail-content-type-get
2999                 (mm-handle-type (get-text-property (point) 'gnus-data))
3000                 'name))
3001          (def-type (and name (mm-default-file-encoding name))))
3002     (and def-type (cons def-type 0))))
3003
3004 (defun gnus-mime-view-part-as-type (mime-type)
3005   "Choose a MIME media type, and view the part as such."
3006   (interactive
3007    (list (completing-read
3008           "View as MIME type: "
3009           (mapcar #'list (mailcap-mime-types))
3010           nil nil
3011           (gnus-mime-view-part-as-type-internal))))
3012   (gnus-article-check-buffer)
3013   (let ((handle (get-text-property (point) 'gnus-data)))
3014     (gnus-mm-display-part
3015      (mm-make-handle (mm-handle-buffer handle)
3016                      (cons mime-type (cdr (mm-handle-type handle)))
3017                      (mm-handle-encoding handle)
3018                      (mm-handle-undisplayer handle)
3019                      (mm-handle-disposition handle)
3020                      (mm-handle-description handle)
3021                      (mm-handle-cache handle)
3022                      (mm-handle-id handle)))))
3023
3024 (defun gnus-mime-copy-part (&optional handle)
3025   "Put the the MIME part under point into a new buffer."
3026   (interactive)
3027   (gnus-article-check-buffer)
3028   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3029          (contents (mm-get-part handle))|
3030          (base (file-name-nondirectory
3031                 (or
3032                  (mail-content-type-get (mm-handle-type handle) 'name)
3033                  (mail-content-type-get (mm-handle-type handle)
3034                                         'filename)
3035                  "*decoded*")))
3036          (buffer (generate-new-buffer base)))
3037     (switch-to-buffer buffer)
3038     (insert contents)
3039     ;; We do it this way to make `normal-mode' set the appropriate mode.
3040     (unwind-protect
3041         (progn
3042           (setq buffer-file-name (expand-file-name base))
3043           (normal-mode))
3044       (setq buffer-file-name nil))
3045     (goto-char (point-min))))
3046
3047 (defun gnus-mime-inline-part (&optional handle arg)
3048   "Insert the MIME part under point into the current buffer."
3049   (interactive (list nil current-prefix-arg))
3050   (gnus-article-check-buffer)
3051   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3052          contents charset
3053          (b (point))
3054          buffer-read-only)
3055     (if (mm-handle-undisplayer handle)
3056         (mm-remove-part handle)
3057       (setq contents (mm-get-part handle))
3058       (cond
3059        ((not arg)
3060         (setq charset (or (mail-content-type-get
3061                            (mm-handle-type handle) 'charset)
3062                           gnus-newsgroup-charset)))
3063        ((numberp arg)
3064         (setq charset
3065               (or (cdr (assq arg 
3066                              gnus-summary-show-article-charset-alist))
3067                   (read-coding-system "Charset: ")))))
3068       (forward-line 2)
3069       (mm-insert-inline handle
3070                         (if (and charset 
3071                                  (setq charset (mm-charset-to-coding-system 
3072                                                 charset))
3073                                  (not (eq charset 'ascii)))
3074                             (mm-decode-coding-string contents charset)
3075                           contents))
3076       (goto-char b))))
3077
3078 (defun gnus-mime-externalize-part (&optional handle)
3079   "View the MIME part under point with an external viewer."
3080   (interactive)
3081   (gnus-article-check-buffer)
3082   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3083          (mm-user-display-methods nil)
3084          (mm-inlined-types nil)
3085          (mail-parse-charset gnus-newsgroup-charset)
3086          (mail-parse-ignored-charsets 
3087           (save-excursion (set-buffer gnus-summary-buffer)
3088                           gnus-newsgroup-ignored-charsets)))
3089     (if (mm-handle-undisplayer handle)
3090         (mm-remove-part handle)
3091       (mm-display-part handle))))
3092
3093 (defun gnus-mime-internalize-part (&optional handle)
3094   "View the MIME part under point with an internal viewer.
3095 In no internal viewer is available, use an external viewer."
3096   (interactive)
3097   (gnus-article-check-buffer)
3098   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3099          (mm-inlined-types '(".*"))
3100          (mm-inline-large-images t)
3101          (mail-parse-charset gnus-newsgroup-charset)
3102          (mail-parse-ignored-charsets 
3103           (save-excursion (set-buffer gnus-summary-buffer)
3104                           gnus-newsgroup-ignored-charsets)))
3105     (if (mm-handle-undisplayer handle)
3106         (mm-remove-part handle)
3107       (mm-display-part handle))))
3108
3109 (defun gnus-mime-action-on-part (&optional action)
3110   "Do something with the MIME attachment at \(point\)."
3111   (interactive
3112    (list (completing-read "Action: " gnus-mime-action-alist)))
3113   (gnus-article-check-buffer)
3114   (let ((action-pair (assoc action gnus-mime-action-alist)))
3115     (if action-pair
3116         (funcall (cdr action-pair)))))
3117
3118
3119 (defun gnus-article-part-wrapper (n function)
3120   (save-current-buffer
3121     (set-buffer gnus-article-buffer)
3122     (when (> n (length gnus-article-mime-handle-alist))
3123       (error "No such part"))
3124     (gnus-article-goto-part n)
3125     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3126       (funcall function handle))))
3127
3128 (defun gnus-article-pipe-part (n)
3129   "Pipe MIME part N, which is the numerical prefix."
3130   (interactive "p")
3131   (gnus-article-part-wrapper n 'mm-pipe-part))
3132
3133 (defun gnus-article-save-part (n)
3134   "Save MIME part N, which is the numerical prefix."
3135   (interactive "p")
3136   (gnus-article-part-wrapper n 'mm-save-part))
3137
3138 (defun gnus-article-interactively-view-part (n)
3139   "View MIME part N interactively, which is the numerical prefix."
3140   (interactive "p")
3141   (gnus-article-part-wrapper n 'mm-interactively-view-part))
3142
3143 (defun gnus-article-copy-part (n)
3144   "Copy MIME part N, which is the numerical prefix."
3145   (interactive "p")
3146   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
3147
3148 (defun gnus-article-externalize-part (n)
3149   "View MIME part N externally, which is the numerical prefix."
3150   (interactive "p")
3151   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
3152
3153 (defun gnus-article-inline-part (n)
3154   "Inline MIME part N, which is the numerical prefix."
3155   (interactive "p")
3156   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
3157
3158 (defun gnus-article-mime-match-handle-first (condition)
3159   (if condition
3160       (let ((alist gnus-article-mime-handle-alist) ihandle n)
3161         (while (setq ihandle (pop alist))
3162           (if (and (cond 
3163                     ((functionp condition)
3164                      (funcall condition (cdr ihandle)))
3165                     ((eq condition 'undisplayed) 
3166                      (not (or (mm-handle-undisplayer (cdr ihandle))
3167                               (equal (mm-handle-media-type (cdr ihandle))
3168                                      "multipart/alternative"))))
3169                     ((eq condition 'undisplayed-alternative)
3170                      (not (mm-handle-undisplayer (cdr ihandle))))
3171                     (t t))
3172                    (gnus-article-goto-part (car ihandle))
3173                    (or (not n) (< (car ihandle) n)))
3174               (setq n (car ihandle))))
3175         (or n 1))
3176     1))
3177
3178 (defun gnus-article-view-part (&optional n)
3179   "View MIME part N, which is the numerical prefix."
3180   (interactive "P")
3181   (save-current-buffer
3182     (set-buffer gnus-article-buffer)
3183     (or (numberp n) (setq n (gnus-article-mime-match-handle-first 
3184                              gnus-article-mime-match-handle-function)))
3185     (when (> n (length gnus-article-mime-handle-alist))
3186       (error "No such part"))
3187     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3188       (when (gnus-article-goto-part n)
3189         (if (equal (car handle) "multipart/alternative")
3190             (gnus-article-press-button)
3191           (when (eq (gnus-mm-display-part handle) 'internal)
3192             (gnus-set-window-start)))))))
3193
3194 (defsubst gnus-article-mime-total-parts ()
3195   (if (bufferp (car gnus-article-mime-handles))
3196       1 ;; single part
3197     (1- (length gnus-article-mime-handles))))
3198
3199 (defun gnus-mm-display-part (handle)
3200   "Display HANDLE and fix MIME button."
3201   (let ((id (get-text-property (point) 'gnus-part))
3202         (point (point))
3203         buffer-read-only)
3204     (forward-line 1)
3205     (prog1
3206         (let ((window (selected-window))
3207               (mail-parse-charset gnus-newsgroup-charset)
3208               (mail-parse-ignored-charsets 
3209                (save-excursion (set-buffer gnus-summary-buffer)
3210                                gnus-newsgroup-ignored-charsets)))
3211           (save-excursion
3212             (unwind-protect
3213                 (let ((win (get-buffer-window (current-buffer) t))
3214                       (beg (point)))
3215                   (when win
3216                     (select-window win))
3217                   (goto-char point)
3218                   (forward-line)
3219                   (if (mm-handle-displayed-p handle)
3220                       ;; This will remove the part.
3221                       (mm-display-part handle)
3222                     (save-restriction
3223                       (narrow-to-region (point) (1+ (point)))
3224                       (mm-display-part handle)
3225                       ;; We narrow to the part itself and
3226                       ;; then call the treatment functions.
3227                       (goto-char (point-min))
3228                       (forward-line 1)
3229                       (narrow-to-region (point) (point-max))
3230                       (gnus-treat-article
3231                        nil id
3232                        (gnus-article-mime-total-parts)
3233                        (mm-handle-media-type handle)))))
3234               (select-window window))))
3235       (goto-char point)
3236       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
3237       (gnus-insert-mime-button
3238        handle id (list (mm-handle-displayed-p handle)))
3239       (goto-char point))))
3240
3241 (defun gnus-article-goto-part (n)
3242   "Go to MIME part N."
3243   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
3244     (when point
3245       (goto-char point))))
3246
3247 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
3248   (let ((gnus-tmp-name
3249          (or (mail-content-type-get (mm-handle-type handle)
3250                                     'name)
3251              (mail-content-type-get (mm-handle-disposition handle)
3252                                     'filename)
3253              ""))
3254         (gnus-tmp-type (mm-handle-media-type handle))
3255         (gnus-tmp-description
3256          (mail-decode-encoded-word-string (or (mm-handle-description handle)
3257                                               "")))
3258         (gnus-tmp-dots
3259          (if (if displayed (car displayed)
3260                (mm-handle-displayed-p handle))
3261              "" "..."))
3262         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
3263                            (buffer-size)))
3264         gnus-tmp-type-long b e)
3265     (when (string-match ".*/" gnus-tmp-name)
3266       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
3267     (setq gnus-tmp-type-long (concat gnus-tmp-type
3268                                      (and (not (equal gnus-tmp-name ""))
3269                                           (concat "; " gnus-tmp-name))))
3270     (or (equal gnus-tmp-description "")
3271         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
3272     (unless (bolp)
3273       (insert "\n"))
3274     (setq b (point))
3275     (gnus-eval-format
3276      gnus-mime-button-line-format gnus-mime-button-line-format-alist
3277      `(local-map ,gnus-mime-button-map
3278                  keymap ,gnus-mime-button-map
3279                  gnus-callback gnus-mm-display-part
3280                  gnus-part ,gnus-tmp-id
3281                  article-type annotation
3282                  gnus-data ,handle))
3283     (setq e (point))
3284     (widget-convert-button
3285      'link b e
3286      :mime-handle handle
3287      :action 'gnus-widget-press-button
3288      :button-keymap gnus-mime-button-map
3289      :help-echo
3290      (lambda (widget/window &optional overlay pos)
3291        ;; Needed to properly clear the message due to a bug in
3292        ;; wid-edit (XEmacs only).
3293        (if (boundp 'help-echo-owns-message)
3294            (setq help-echo-owns-message t))
3295        (format
3296         "%S: %s the MIME part; %S: more options"
3297         (aref gnus-mouse-2 0)
3298         ;; XEmacs will get a single widget arg; Emacs 21 will get
3299         ;; window, overlay, position.
3300         (if (mm-handle-displayed-p
3301              (if overlay
3302                  (with-current-buffer (gnus-overlay-buffer overlay)
3303                    (widget-get (widget-at (gnus-overlay-start overlay))
3304                                :mime-handle))
3305                (widget-get widget/window :mime-handle)))
3306             "hide" "show")
3307         (aref gnus-down-mouse-3 0))))))
3308
3309 (defun gnus-widget-press-button (elems el)
3310   (goto-char (widget-get elems :from))
3311   (gnus-article-press-button))
3312
3313 (defvar gnus-displaying-mime nil)
3314
3315 (defun gnus-display-mime (&optional ihandles)
3316   "Display the MIME parts."
3317   (save-excursion
3318     (save-selected-window
3319       (let ((window (get-buffer-window gnus-article-buffer))
3320             (point (point)))
3321         (when window
3322           (select-window window)
3323           ;; We have to do this since selecting the window
3324           ;; may change the point.  So we set the window point.
3325           (set-window-point window point)))
3326       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
3327              buffer-read-only handle name type b e display)
3328         (when (and (not ihandles)
3329                    (not gnus-displaying-mime))
3330           ;; Top-level call; we clean up.
3331           (when gnus-article-mime-handles
3332             (mm-destroy-parts gnus-article-mime-handles)
3333             (setq gnus-article-mime-handle-alist nil));; A trick.
3334           (setq gnus-article-mime-handles handles)
3335           ;; We allow users to glean info from the handles.
3336           (when gnus-article-mime-part-function
3337             (gnus-mime-part-function handles)))
3338         (if (and handles
3339                  (or (not (stringp (car handles)))
3340                      (cdr handles)))
3341             (progn
3342               (when (and (not ihandles)
3343                          (not gnus-displaying-mime))
3344                 ;; Clean up for mime parts.
3345                 (article-goto-body)
3346                 (delete-region (point) (point-max)))
3347               (let ((gnus-displaying-mime t))
3348                 (gnus-mime-display-part handles)))
3349           (save-restriction
3350             (article-goto-body)
3351             (narrow-to-region (point) (point-max))
3352             (gnus-treat-article nil 1 1)
3353             (widen)))
3354         (unless ihandles
3355           ;; Highlight the headers.
3356           (save-excursion
3357             (save-restriction
3358               (article-goto-body)
3359               (narrow-to-region (point-min) (point))
3360               (gnus-treat-article 'head))))))))
3361
3362 (defvar gnus-mime-display-multipart-as-mixed nil)
3363
3364 (defun gnus-mime-display-part (handle)
3365   (cond
3366    ;; Single part.
3367    ((not (stringp (car handle)))
3368     (gnus-mime-display-single handle))
3369    ;; User-defined multipart
3370    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
3371     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
3372              handle))
3373    ;; multipart/alternative
3374    ((and (equal (car handle) "multipart/alternative")
3375          (not gnus-mime-display-multipart-as-mixed))
3376     (let ((id (1+ (length gnus-article-mime-handle-alist))))
3377       (push (cons id handle) gnus-article-mime-handle-alist)
3378       (gnus-mime-display-alternative (cdr handle) nil nil id)))
3379    ;; multipart/related
3380    ((and (equal (car handle) "multipart/related")
3381          (not gnus-mime-display-multipart-as-mixed))
3382     ;;;!!!We should find the start part, but we just default
3383     ;;;!!!to the first part.
3384     (gnus-mime-display-part (cadr handle)))
3385    ;; Other multiparts are handled like multipart/mixed.
3386    (t
3387     (gnus-mime-display-mixed (cdr handle)))))
3388
3389 (defun gnus-mime-part-function (handles)
3390   (if (stringp (car handles))
3391       (mapcar 'gnus-mime-part-function (cdr handles))
3392     (funcall gnus-article-mime-part-function handles)))
3393
3394 (defun gnus-mime-display-mixed (handles)
3395   (mapcar 'gnus-mime-display-part handles))
3396
3397 (defun gnus-mime-display-single (handle)
3398   (let ((type (mm-handle-media-type handle))
3399         (ignored gnus-ignored-mime-types)
3400         (not-attachment t)
3401         (move nil)
3402         display text)
3403     (catch 'ignored
3404       (progn
3405         (while ignored
3406           (when (string-match (pop ignored) type)
3407             (throw 'ignored nil)))
3408         (if (and (setq not-attachment
3409                        (and (not (mm-inline-override-p handle))
3410                             (or (not (mm-handle-disposition handle))
3411                                 (equal (car (mm-handle-disposition handle))
3412                                        "inline")
3413                                 (mm-attachment-override-p handle))))
3414                  (mm-automatic-display-p handle)
3415                  (or (mm-inlined-p handle)
3416                      (mm-automatic-external-display-p type)))
3417             (setq display t)
3418           (when (equal (mm-handle-media-supertype handle) "text")
3419             (setq text t)))
3420         (let ((id (1+ (length gnus-article-mime-handle-alist)))
3421               beg)
3422           (push (cons id handle) gnus-article-mime-handle-alist)
3423           (when (or (not display)
3424                     (not (gnus-unbuttonized-mime-type-p type)))
3425             ;(gnus-article-insert-newline)
3426             (gnus-insert-mime-button
3427              handle id (list (or display (and not-attachment text))))
3428             (gnus-article-insert-newline)
3429             ;(gnus-article-insert-newline)
3430             (setq move t))
3431           (setq beg (point))
3432           (cond
3433            (display
3434             (when move
3435               (forward-line -2)
3436               (setq beg (point)))
3437             (let ((mail-parse-charset gnus-newsgroup-charset)
3438                   (mail-parse-ignored-charsets 
3439                    (save-excursion (condition-case ()
3440                                        (set-buffer gnus-summary-buffer)
3441                                      (error))
3442                                    gnus-newsgroup-ignored-charsets)))
3443               (mm-display-part handle t))
3444             (goto-char (point-max)))
3445            ((and text not-attachment)
3446             (when move
3447               (forward-line -2)
3448               (setq beg (point)))
3449             (gnus-article-insert-newline)
3450             (mm-insert-inline handle (mm-get-part handle))
3451             (goto-char (point-max))))
3452           ;; Do highlighting.
3453           (save-excursion
3454             (save-restriction
3455               (narrow-to-region beg (point))
3456               (gnus-treat-article
3457                nil id 
3458                (gnus-article-mime-total-parts)
3459                (mm-handle-media-type handle)))))))))
3460
3461 (defun gnus-unbuttonized-mime-type-p (type)
3462   "Say whether TYPE is to be unbuttonized."
3463   (unless gnus-inhibit-mime-unbuttonizing
3464     (catch 'found
3465       (let ((types gnus-unbuttonized-mime-types))
3466         (while types
3467           (when (string-match (pop types) type)
3468             (throw 'found t)))))))
3469
3470 (defun gnus-article-insert-newline ()
3471   "Insert a newline, but mark it as undeletable."
3472   (gnus-put-text-property
3473    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
3474
3475 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
3476   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
3477          (ihandles handles)
3478          (point (point))
3479          handle buffer-read-only from props begend not-pref)
3480     (save-window-excursion
3481       (save-restriction
3482         (when ibegend
3483           (narrow-to-region (car ibegend)
3484                             (or (cdr ibegend)
3485                                 (progn
3486                                   (goto-char (car ibegend))
3487                                   (forward-line 2)
3488                                   (point))))
3489           (delete-region (point-min) (point-max))
3490           (mm-remove-parts handles))
3491         (setq begend (list (point-marker)))
3492         ;; Do the toggle.
3493         (unless (setq not-pref (cadr (member preferred ihandles)))
3494           (setq not-pref (car ihandles)))
3495         (when (or ibegend
3496                   (not (gnus-unbuttonized-mime-type-p
3497                         "multipart/alternative")))
3498           (gnus-add-text-properties
3499            (setq from (point))
3500            (progn
3501              (insert (format "%d.  " id))
3502              (point))
3503            `(gnus-callback
3504              (lambda (handles)
3505                (unless ,(not ibegend)
3506                  (setq gnus-article-mime-handle-alist
3507                        ',gnus-article-mime-handle-alist))
3508                (gnus-mime-display-alternative
3509                 ',ihandles ',not-pref ',begend ,id))
3510              local-map ,gnus-mime-button-map
3511              ,gnus-mouse-face-prop ,gnus-article-mouse-face
3512              face ,gnus-article-button-face
3513              keymap ,gnus-mime-button-map
3514              gnus-part ,id
3515              gnus-data ,handle))
3516           (widget-convert-button 'link from (point)
3517                                  :action 'gnus-widget-press-button
3518                                  :button-keymap gnus-widget-button-keymap)
3519           ;; Do the handles
3520           (while (setq handle (pop handles))
3521             (gnus-add-text-properties
3522              (setq from (point))
3523              (progn
3524                (insert (format "(%c) %-18s"
3525                                (if (equal handle preferred) ?* ? )
3526                                (mm-handle-media-type handle)))
3527                (point))
3528              `(gnus-callback
3529                (lambda (handles)
3530                  (unless ,(not ibegend)
3531                    (setq gnus-article-mime-handle-alist
3532                          ',gnus-article-mime-handle-alist))
3533                  (gnus-mime-display-alternative
3534                   ',ihandles ',handle ',begend ,id))
3535                local-map ,gnus-mime-button-map
3536                ,gnus-mouse-face-prop ,gnus-article-mouse-face
3537                face ,gnus-article-button-face
3538                keymap ,gnus-mime-button-map
3539                gnus-part ,id
3540                gnus-data ,handle))
3541             (widget-convert-button 'link from (point)
3542                                    :action 'gnus-widget-press-button
3543                                    :button-keymap gnus-widget-button-keymap)
3544             (insert "  "))
3545           (insert "\n\n"))
3546         (when preferred
3547           (if (stringp (car preferred))
3548               (gnus-display-mime preferred)
3549             (let ((mail-parse-charset gnus-newsgroup-charset)
3550                   (mail-parse-ignored-charsets 
3551                    (save-excursion (set-buffer gnus-summary-buffer)
3552                                    gnus-newsgroup-ignored-charsets)))
3553               (mm-display-part preferred)
3554               ;; Do highlighting.
3555               (save-excursion
3556                 (save-restriction
3557                   (narrow-to-region (car begend) (point-max))
3558                   (gnus-treat-article
3559                    nil (length gnus-article-mime-handle-alist)
3560                    (gnus-article-mime-total-parts)
3561                    (mm-handle-media-type handle))))))
3562           (goto-char (point-max))
3563           (setcdr begend (point-marker)))))
3564     (when ibegend
3565       (goto-char point))))
3566
3567 (defun gnus-article-wash-status ()
3568   "Return a string which display status of article washing."
3569   (save-excursion
3570     (set-buffer gnus-article-buffer)
3571     (let ((cite (memq 'cite gnus-article-wash-types))
3572           (headers (memq 'headers gnus-article-wash-types))
3573           (boring (memq 'boring-headers gnus-article-wash-types))
3574           (pgp (memq 'pgp gnus-article-wash-types))
3575           (pem (memq 'pem gnus-article-wash-types))
3576           (signature (memq 'signature gnus-article-wash-types))
3577           (overstrike (memq 'overstrike gnus-article-wash-types))
3578           (emphasis (memq 'emphasis gnus-article-wash-types)))
3579       (format "%c%c%c%c%c%c"
3580               (if cite ?c ? )
3581               (if (or headers boring) ?h ? )
3582               (if (or pgp pem) ?p ? )
3583               (if signature ?s ? )
3584               (if overstrike ?o ? )
3585               (if emphasis ?e ? )))))
3586
3587 (defalias 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
3588
3589 (defun gnus-article-maybe-hide-headers ()
3590   "Hide unwanted headers if `gnus-have-all-headers' is nil.
3591 Provided for backwards compatibility."
3592   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
3593                  (not (save-excursion (set-buffer gnus-summary-buffer)
3594                                       gnus-have-all-headers)))
3595              (not gnus-inhibit-hiding))
3596     (gnus-article-hide-headers)))
3597
3598 ;;; Article savers.
3599
3600 (defun gnus-output-to-file (file-name)
3601   "Append the current article to a file named FILE-NAME."
3602   (let ((artbuf (current-buffer)))
3603     (with-temp-buffer
3604       (insert-buffer-substring artbuf)
3605       ;; Append newline at end of the buffer as separator, and then
3606       ;; save it to file.
3607       (goto-char (point-max))
3608       (insert "\n")
3609       (mm-append-to-file (point-min) (point-max) file-name)
3610       t)))
3611
3612 (defun gnus-narrow-to-page (&optional arg)
3613   "Narrow the article buffer to a page.
3614 If given a numerical ARG, move forward ARG pages."
3615   (interactive "P")
3616   (setq arg (if arg (prefix-numeric-value arg) 0))
3617   (save-excursion
3618     (set-buffer gnus-article-buffer)
3619     (goto-char (point-min))
3620     (widen)
3621     ;; Remove any old next/prev buttons.
3622     (when (gnus-visual-p 'page-marker)
3623       (let ((buffer-read-only nil))
3624         (gnus-remove-text-with-property 'gnus-prev)
3625         (gnus-remove-text-with-property 'gnus-next)))
3626     (when
3627         (cond ((< arg 0)
3628                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
3629               ((> arg 0)
3630                (re-search-forward page-delimiter nil 'move arg)))
3631       (goto-char (match-end 0)))
3632     (narrow-to-region
3633      (point)
3634      (if (re-search-forward page-delimiter nil 'move)
3635          (match-beginning 0)
3636        (point)))
3637     (when (and (gnus-visual-p 'page-marker)
3638                (not (= (point-min) 1)))
3639       (save-excursion
3640         (goto-char (point-min))
3641         (gnus-insert-prev-page-button)))
3642     (when (and (gnus-visual-p 'page-marker)
3643                (< (+ (point-max) 2) (buffer-size)))
3644       (save-excursion
3645         (goto-char (point-max))
3646         (gnus-insert-next-page-button)))))
3647
3648 ;; Article mode commands
3649
3650 (defun gnus-article-goto-next-page ()
3651   "Show the next page of the article."
3652   (interactive)
3653   (when (gnus-article-next-page)
3654     (goto-char (point-min))
3655     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
3656
3657 (defun gnus-article-goto-prev-page ()
3658   "Show the next page of the article."
3659   (interactive)
3660   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
3661     (gnus-article-prev-page nil)))
3662
3663 (defun gnus-article-next-page (&optional lines)
3664   "Show the next page of the current article.
3665 If end of article, return non-nil.  Otherwise return nil.
3666 Argument LINES specifies lines to be scrolled up."
3667   (interactive "p")
3668   (move-to-window-line -1)
3669   (if (save-excursion
3670         (end-of-line)
3671         (and (pos-visible-in-window-p)  ;Not continuation line.
3672              (eobp)))
3673       ;; Nothing in this page.
3674       (if (or (not gnus-page-broken)
3675               (save-excursion
3676                 (save-restriction
3677                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
3678           t                             ;Nothing more.
3679         (gnus-narrow-to-page 1)         ;Go to next page.
3680         nil)
3681     ;; More in this page.
3682     (let ((scroll-in-place nil))
3683       (condition-case ()
3684           (scroll-up lines)
3685         (end-of-buffer
3686          ;; Long lines may cause an end-of-buffer error.
3687          (goto-char (point-max)))))
3688     (move-to-window-line 0)
3689     nil))
3690
3691 (defun gnus-article-prev-page (&optional lines)
3692   "Show previous page of current article.
3693 Argument LINES specifies lines to be scrolled down."
3694   (interactive "p")
3695   (move-to-window-line 0)
3696   (if (and gnus-page-broken
3697            (bobp)
3698            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
3699       (progn
3700         (gnus-narrow-to-page -1)        ;Go to previous page.
3701         (goto-char (point-max))
3702         (recenter -1))
3703     (let ((scroll-in-place nil))
3704       (prog1
3705           (condition-case ()
3706               (scroll-down lines)
3707             (beginning-of-buffer
3708              (goto-char (point-min))))
3709         (move-to-window-line 0)))))
3710
3711 (defun gnus-article-refer-article ()
3712   "Read article specified by message-id around point."
3713   (interactive)
3714   (let ((point (point)))
3715     (search-forward ">" nil t)          ;Move point to end of "<....>".
3716     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
3717         (let ((message-id (match-string 1)))
3718           (goto-char point)
3719           (set-buffer gnus-summary-buffer)
3720           (gnus-summary-refer-article message-id))
3721       (goto-char (point))
3722       (error "No references around point"))))
3723
3724 (defun gnus-article-show-summary ()
3725   "Reconfigure windows to show summary buffer."
3726   (interactive)
3727   (if (not (gnus-buffer-live-p gnus-summary-buffer))
3728       (error "There is no summary buffer for this article buffer")
3729     (gnus-article-set-globals)
3730     (gnus-configure-windows 'article)
3731     (gnus-summary-goto-subject gnus-current-article)
3732     (gnus-summary-position-point)))
3733
3734 (defun gnus-article-describe-briefly ()
3735   "Describe article mode commands briefly."
3736   (interactive)
3737   (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")))
3738
3739 (defun gnus-article-summary-command ()
3740   "Execute the last keystroke in the summary buffer."
3741   (interactive)
3742   (let ((obuf (current-buffer))
3743         (owin (current-window-configuration))
3744         func)
3745     (switch-to-buffer gnus-article-current-summary 'norecord)
3746     (setq func (lookup-key (current-local-map) (this-command-keys)))
3747     (call-interactively func)
3748     (set-buffer obuf)
3749     (set-window-configuration owin)
3750     (set-window-point (get-buffer-window (current-buffer)) (point))))
3751
3752 (defun gnus-article-summary-command-nosave ()
3753   "Execute the last keystroke in the summary buffer."
3754   (interactive)
3755   (let (func)
3756     (pop-to-buffer gnus-article-current-summary 'norecord)
3757     (setq func (lookup-key (current-local-map) (this-command-keys)))
3758     (call-interactively func)))
3759
3760 (defun gnus-article-check-buffer ()
3761   "Beep if not in an article buffer."
3762   (unless (equal major-mode 'gnus-article-mode)
3763     (error "Command invoked outside of a Gnus article buffer")))
3764
3765 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
3766   "Read a summary buffer key sequence and execute it from the article buffer."
3767   (interactive "P")
3768   (gnus-article-check-buffer)
3769   (let ((nosaves
3770          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
3771            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
3772            "=" "^" "\M-^" "|"))
3773         (nosave-but-article
3774          '("A\r"))
3775         (nosave-in-article
3776          '("\C-d"))
3777         (up-to-top
3778          '("n" "Gn" "p" "Gp"))
3779         keys new-sum-point)
3780     (save-excursion
3781       (set-buffer gnus-article-current-summary)
3782       (let (gnus-pick-mode)
3783         (push (or key last-command-event) unread-command-events)
3784         (setq keys (if (featurep 'xemacs)
3785                        (events-to-keys (read-key-sequence nil))
3786                      (read-key-sequence nil)))))
3787                      
3788     (message "")
3789
3790     (if (or (member keys nosaves)
3791             (member keys nosave-but-article)
3792             (member keys nosave-in-article))
3793         (let (func)
3794           (save-window-excursion
3795             (pop-to-buffer gnus-article-current-summary 'norecord)
3796             ;; We disable the pick minor mode commands.
3797             (let (gnus-pick-mode)
3798               (setq func (lookup-key (current-local-map) keys))))
3799           (if (or (not func)
3800                   (numberp func))
3801               (ding)
3802             (unless (member keys nosave-in-article)
3803               (set-buffer gnus-article-current-summary))
3804             (call-interactively func)
3805             (setq new-sum-point (point)))
3806           (when (member keys nosave-but-article)
3807             (pop-to-buffer gnus-article-buffer 'norecord)))
3808       ;; These commands should restore window configuration.
3809       (let ((obuf (current-buffer))
3810             (owin (current-window-configuration))
3811             (opoint (point))
3812             (summary gnus-article-current-summary)
3813             func in-buffer selected)
3814         (if not-restore-window
3815             (pop-to-buffer summary 'norecord)
3816           (switch-to-buffer summary 'norecord))
3817         (setq in-buffer (current-buffer))
3818         ;; We disable the pick minor mode commands.
3819         (if (setq func (let (gnus-pick-mode)
3820                          (lookup-key (current-local-map) keys)))
3821             (progn
3822               (call-interactively func)
3823               (setq new-sum-point (point)))
3824           (ding))
3825         (when (eq in-buffer (current-buffer))
3826           (setq selected (gnus-summary-select-article))
3827           (set-buffer obuf)
3828           (unless not-restore-window
3829             (set-window-configuration owin))
3830           (when (eq selected 'old)
3831             (article-goto-body)
3832             (set-window-start (get-buffer-window (current-buffer))
3833                               1)
3834             (set-window-point (get-buffer-window (current-buffer))
3835                               (point)))
3836           (let ((win (get-buffer-window gnus-article-current-summary)))
3837             (when win
3838               (set-window-point win new-sum-point))))))))
3839
3840 (defun gnus-article-hide (&optional arg force)
3841   "Hide all the gruft in the current article.
3842 This means that PGP stuff, signatures, cited text and (some)
3843 headers will be hidden.
3844 If given a prefix, show the hidden text instead."
3845   (interactive (append (gnus-article-hidden-arg) (list 'force)))
3846   (gnus-article-hide-headers arg)
3847   (gnus-article-hide-list-identifiers arg)
3848   (gnus-article-hide-pgp arg)
3849   (gnus-article-hide-citation-maybe arg force)
3850   (gnus-article-hide-signature arg))
3851
3852 (defun gnus-article-maybe-highlight ()
3853   "Do some article highlighting if article highlighting is requested."
3854   (when (gnus-visual-p 'article-highlight 'highlight)
3855     (gnus-article-highlight-some)))
3856
3857 (defun gnus-check-group-server ()
3858   ;; Make sure the connection to the server is alive.
3859   (unless (gnus-server-opened
3860            (gnus-find-method-for-group gnus-newsgroup-name))
3861     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
3862     (gnus-request-group gnus-newsgroup-name t)))
3863
3864 (defun gnus-request-article-this-buffer (article group)
3865   "Get an article and insert it into this buffer."
3866   (let (do-update-line sparse-header)
3867     (prog1
3868         (save-excursion
3869           (erase-buffer)
3870           (gnus-kill-all-overlays)
3871           (setq group (or group gnus-newsgroup-name))
3872
3873           ;; Using `gnus-request-article' directly will insert the article into
3874           ;; `nntp-server-buffer' - so we'll save some time by not having to
3875           ;; copy it from the server buffer into the article buffer.
3876
3877           ;; We only request an article by message-id when we do not have the
3878           ;; headers for it, so we'll have to get those.
3879           (when (stringp article)
3880             (gnus-read-header article))
3881
3882           ;; If the article number is negative, that means that this article
3883           ;; doesn't belong in this newsgroup (possibly), so we find its
3884           ;; message-id and request it by id instead of number.
3885           (when (and (numberp article)
3886                      gnus-summary-buffer
3887                      (get-buffer gnus-summary-buffer)
3888                      (gnus-buffer-exists-p gnus-summary-buffer))
3889             (save-excursion
3890               (set-buffer gnus-summary-buffer)
3891               (let ((header (gnus-summary-article-header article)))
3892                 (when (< article 0)
3893                   (cond
3894                    ((memq article gnus-newsgroup-sparse)
3895                     ;; This is a sparse gap article.
3896                     (setq do-update-line article)
3897                     (setq article (mail-header-id header))
3898                     (setq sparse-header (gnus-read-header article))
3899                     (setq gnus-newsgroup-sparse
3900                           (delq article gnus-newsgroup-sparse)))
3901                    ((vectorp header)
3902                     ;; It's a real article.
3903                     (setq article (mail-header-id header)))
3904                    (t
3905                     ;; It is an extracted pseudo-article.
3906                     (setq article 'pseudo)
3907                     (gnus-request-pseudo-article header))))
3908
3909                 (let ((method (gnus-find-method-for-group
3910                                gnus-newsgroup-name)))
3911                   (when (and (eq (car method) 'nneething)
3912                              (vectorp header))
3913                     (let ((dir (expand-file-name
3914                                 (mail-header-subject header)
3915                                 (file-name-as-directory
3916                                  (or (cadr (assq 'nneething-address method))
3917                                      (nth 1 method))))))
3918                       (when (file-directory-p dir)
3919                         (setq article 'nneething)
3920                         (gnus-group-enter-directory dir))))))))
3921
3922           (cond
3923            ;; Refuse to select canceled articles.
3924            ((and (numberp article)
3925                  gnus-summary-buffer
3926                  (get-buffer gnus-summary-buffer)
3927                  (gnus-buffer-exists-p gnus-summary-buffer)
3928                  (eq (cdr (save-excursion
3929                             (set-buffer gnus-summary-buffer)
3930                             (assq article gnus-newsgroup-reads)))
3931                      gnus-canceled-mark))
3932             nil)
3933            ;; We first check `gnus-original-article-buffer'.
3934            ((and (get-buffer gnus-original-article-buffer)
3935                  (numberp article)
3936                  (save-excursion
3937                    (set-buffer gnus-original-article-buffer)
3938                    (and (equal (car gnus-original-article) group)
3939                         (eq (cdr gnus-original-article) article))))
3940             (insert-buffer-substring gnus-original-article-buffer)
3941             'article)
3942            ;; Check the backlog.
3943            ((and gnus-keep-backlog
3944                  (gnus-backlog-request-article group article (current-buffer)))
3945             'article)
3946            ;; Check asynchronous pre-fetch.
3947            ((gnus-async-request-fetched-article group article (current-buffer))
3948             (gnus-async-prefetch-next group article gnus-summary-buffer)
3949             (when (and (numberp article) gnus-keep-backlog)
3950               (gnus-backlog-enter-article group article (current-buffer)))
3951             'article)
3952            ;; Check the cache.
3953            ((and gnus-use-cache
3954                  (numberp article)
3955                  (gnus-cache-request-article article group))
3956             'article)
3957            ;; Get the article and put into the article buffer.
3958            ((or (stringp article)
3959                 (numberp article))
3960             (let ((gnus-override-method gnus-override-method)
3961                   (methods (and (stringp article) 
3962                                 gnus-refer-article-method))
3963                   result
3964                   (buffer-read-only nil))
3965               (if (or (not (listp methods))
3966                       (and (symbolp (car methods))
3967                            (assq (car methods) nnoo-definition-alist)))
3968                   (setq methods (list methods)))
3969               (when (and (null gnus-override-method)
3970                          methods)
3971                 (setq gnus-override-method (pop methods)))
3972               (while (not result)
3973                 (when (eq gnus-override-method 'current)
3974                   (setq gnus-override-method gnus-current-select-method))
3975                 (erase-buffer)
3976                 (gnus-kill-all-overlays)
3977                 (let ((gnus-newsgroup-name group))
3978                   (gnus-check-group-server))
3979                 (when (gnus-request-article article group (current-buffer))
3980                   (when (numberp article)
3981                     (gnus-async-prefetch-next group article 
3982                                               gnus-summary-buffer)
3983                     (when gnus-keep-backlog
3984                       (gnus-backlog-enter-article
3985                        group article (current-buffer))))
3986                   (setq result 'article))
3987                 (if (not result)
3988                     (if methods
3989                         (setq gnus-override-method (pop methods))
3990                       (setq result 'done))))
3991               (and (eq result 'article) 'article)))
3992            ;; It was a pseudo.
3993            (t article)))
3994
3995       ;; Associate this article with the current summary buffer.
3996       (setq gnus-article-current-summary gnus-summary-buffer)
3997
3998       ;; Take the article from the original article buffer
3999       ;; and place it in the buffer it's supposed to be in.
4000       (when (and (get-buffer gnus-article-buffer)
4001                  (equal (buffer-name (current-buffer))
4002                         (buffer-name (get-buffer gnus-article-buffer))))
4003         (save-excursion
4004           (if (get-buffer gnus-original-article-buffer)
4005               (set-buffer gnus-original-article-buffer)
4006             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
4007             (buffer-disable-undo)
4008             (setq major-mode 'gnus-original-article-mode)
4009             (setq buffer-read-only t))
4010           (let (buffer-read-only)
4011             (erase-buffer)
4012             (insert-buffer-substring gnus-article-buffer))
4013           (setq gnus-original-article (cons group article)))
4014
4015         ;; Decode charsets.
4016         (run-hooks 'gnus-article-decode-hook)
4017         ;; Mark article as decoded or not.
4018         (setq gnus-article-decoded-p gnus-article-decode-hook))
4019
4020       ;; Update sparse articles.
4021       (when (and do-update-line
4022                  (or (numberp article)
4023                      (stringp article)))
4024         (let ((buf (current-buffer)))
4025           (set-buffer gnus-summary-buffer)
4026           (gnus-summary-update-article do-update-line sparse-header)
4027           (gnus-summary-goto-subject do-update-line nil t)
4028           (set-window-point (get-buffer-window (current-buffer) t)
4029                             (point))
4030           (set-buffer buf))))))
4031
4032 ;;;
4033 ;;; Article editing
4034 ;;;
4035
4036 (defcustom gnus-article-edit-mode-hook nil
4037   "Hook run in article edit mode buffers."
4038   :group 'gnus-article-various
4039   :type 'hook)
4040
4041 (defvar gnus-article-edit-done-function nil)
4042
4043 (defvar gnus-article-edit-mode-map nil)
4044
4045 ;; Should we be using derived.el for this?
4046 (unless gnus-article-edit-mode-map
4047   (setq gnus-article-edit-mode-map (make-sparse-keymap))
4048   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
4049
4050   (gnus-define-keys gnus-article-edit-mode-map
4051     "\C-c\C-c" gnus-article-edit-done
4052     "\C-c\C-k" gnus-article-edit-exit)
4053
4054   (gnus-define-keys (gnus-article-edit-wash-map
4055                      "\C-c\C-w" gnus-article-edit-mode-map)
4056     "f" gnus-article-edit-full-stops))
4057
4058 (defun gnus-article-edit-mode ()
4059   "Major mode for editing articles.
4060 This is an extended text-mode.
4061
4062 \\{gnus-article-edit-mode-map}"
4063   (interactive)
4064   (setq major-mode 'gnus-article-edit-mode)
4065   (setq mode-name "Article Edit")
4066   (use-local-map gnus-article-edit-mode-map)
4067   (make-local-variable 'gnus-article-edit-done-function)
4068   (make-local-variable 'gnus-prev-winconf)
4069   (setq buffer-read-only nil)
4070   (buffer-enable-undo)
4071   (widen)
4072   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
4073
4074 (defun gnus-article-edit (&optional force)
4075   "Edit the current article.
4076 This will have permanent effect only in mail groups.
4077 If FORCE is non-nil, allow editing of articles even in read-only
4078 groups."
4079   (interactive "P")
4080   (when (and (not force)
4081              (gnus-group-read-only-p))
4082     (error "The current newsgroup does not support article editing"))
4083   (gnus-article-date-original)
4084   (gnus-article-edit-article
4085    'ignore
4086    `(lambda (no-highlight)
4087       'ignore
4088       (gnus-summary-edit-article-done
4089        ,(or (mail-header-references gnus-current-headers) "")
4090        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
4091
4092 (defun gnus-article-edit-article (start-func exit-func)
4093   "Start editing the contents of the current article buffer."
4094   (let ((winconf (current-window-configuration)))
4095     (set-buffer gnus-article-buffer)
4096     (gnus-article-edit-mode)
4097     (funcall start-func)
4098     (gnus-configure-windows 'edit-article)
4099     (setq gnus-article-edit-done-function exit-func)
4100     (setq gnus-prev-winconf winconf)
4101     (gnus-message 6 "C-c C-c to end edits")))
4102
4103 (defun gnus-article-edit-done (&optional arg)
4104   "Update the article edits and exit."
4105   (interactive "P")
4106   (save-excursion
4107     (save-restriction
4108       (widen)
4109       (when (article-goto-body)
4110         (let ((lines (count-lines (point) (point-max)))
4111               (length (- (point-max) (point)))
4112               (case-fold-search t)
4113               (body (copy-marker (point))))
4114           (goto-char (point-min))
4115           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
4116             (delete-region (match-beginning 1) (match-end 1))
4117             (insert (number-to-string length)))
4118           (goto-char (point-min))
4119           (when (re-search-forward
4120                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
4121             (delete-region (match-beginning 1) (match-end 1))
4122             (insert (number-to-string length)))
4123           (goto-char (point-min))
4124           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
4125             (delete-region (match-beginning 1) (match-end 1))
4126             (insert (number-to-string lines)))))))
4127   (let ((func gnus-article-edit-done-function)
4128         (buf (current-buffer))
4129         (start (window-start)))
4130     (gnus-article-edit-exit)
4131     (save-excursion
4132       (set-buffer buf)
4133       (let ((buffer-read-only nil))
4134         (funcall func arg))
4135       ;; The cache and backlog have to be flushed somewhat.
4136       (when gnus-keep-backlog
4137         (gnus-backlog-remove-article
4138          (car gnus-article-current) (cdr gnus-article-current)))
4139       ;; Flush original article as well.
4140       (save-excursion
4141         (when (get-buffer gnus-original-article-buffer)
4142           (set-buffer gnus-original-article-buffer)
4143           (setq gnus-original-article nil)))
4144       (when gnus-use-cache
4145         (gnus-cache-update-article
4146          (car gnus-article-current) (cdr gnus-article-current))))
4147     (set-buffer buf)
4148     (set-window-start (get-buffer-window buf) start)
4149     (set-window-point (get-buffer-window buf) (point))))
4150
4151 (defun gnus-article-edit-exit ()
4152   "Exit the article editing without updating."
4153   (interactive)
4154   ;; We remove all text props from the article buffer.
4155   (let ((buf (buffer-substring-no-properties (point-min) (point-max)))
4156         (curbuf (current-buffer))
4157         (p (point))
4158         (window-start (window-start)))
4159     (erase-buffer)
4160     (insert buf)
4161     (let ((winconf gnus-prev-winconf))
4162       (gnus-article-mode)
4163       (set-window-configuration winconf)
4164       ;; Tippy-toe some to make sure that point remains where it was.
4165       (save-current-buffer
4166         (set-buffer curbuf)
4167         (set-window-start (get-buffer-window (current-buffer)) window-start)
4168         (goto-char p)))))
4169
4170 (defun gnus-article-edit-full-stops ()
4171   "Interactively repair spacing at end of sentences."
4172   (interactive)
4173   (save-excursion
4174     (goto-char (point-min))
4175     (search-forward-regexp "^$" nil t)
4176     (let ((case-fold-search nil))
4177       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
4178
4179 ;;;
4180 ;;; Article highlights
4181 ;;;
4182
4183 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
4184
4185 ;;; Internal Variables:
4186
4187 (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\\)\\)"
4188   "Regular expression that matches URLs."
4189   :group 'gnus-article-buttons
4190   :type 'regexp)
4191
4192 (defcustom gnus-button-alist
4193   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
4194      0 t gnus-button-message-id 2)
4195     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
4196     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
4197      1 t
4198      gnus-button-fetch-group 4)
4199     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
4200     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
4201      t gnus-button-message-id 3)
4202     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
4203     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
4204     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
4205     ;; This is how URLs _should_ be embedded in text...
4206     ("<URL: *\\([^<>]*\\)>" 0 t gnus-button-embedded-url 1)
4207     ;; Raw URLs.
4208     (,gnus-button-url-regexp 0 t browse-url 0))
4209   "*Alist of regexps matching buttons in article bodies.
4210
4211 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
4212 REGEXP: is the string matching text around the button,
4213 BUTTON: is the number of the regexp grouping actually matching the button,
4214 FORM: is a lisp expression which must eval to true for the button to
4215 be added,
4216 CALLBACK: is the function to call when the user push this button, and each
4217 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
4218
4219 CALLBACK can also be a variable, in that case the value of that
4220 variable it the real callback function."
4221   :group 'gnus-article-buttons
4222   :type '(repeat (list regexp
4223                        (integer :tag "Button")
4224                        (sexp :tag "Form")
4225                        (function :tag "Callback")
4226                        (repeat :tag "Par"
4227                                :inline t
4228                                (integer :tag "Regexp group")))))
4229
4230 (defcustom gnus-header-button-alist
4231   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
4232      0 t gnus-button-message-id 0)
4233     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
4234     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
4235      0 t gnus-button-mailto 0)
4236     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
4237     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
4238     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
4239     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
4240      gnus-button-message-id 3))
4241   "*Alist of headers and regexps to match buttons in article heads.
4242
4243 This alist is very similar to `gnus-button-alist', except that each
4244 alist has an additional HEADER element first in each entry:
4245
4246 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
4247
4248 HEADER is a regexp to match a header.  For a fuller explanation, see
4249 `gnus-button-alist'."
4250   :group 'gnus-article-buttons
4251   :group 'gnus-article-headers
4252   :type '(repeat (list (regexp :tag "Header")
4253                        regexp
4254                        (integer :tag "Button")
4255                        (sexp :tag "Form")
4256                        (function :tag "Callback")
4257                        (repeat :tag "Par"
4258                                :inline t
4259                                (integer :tag "Regexp group")))))
4260
4261 (defvar gnus-button-regexp nil)
4262 (defvar gnus-button-marker-list nil)
4263 ;; Regexp matching any of the regexps from `gnus-button-alist'.
4264
4265 (defvar gnus-button-last nil)
4266 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
4267
4268 ;;; Commands:
4269
4270 (defun gnus-article-push-button (event)
4271   "Check text under the mouse pointer for a callback function.
4272 If the text under the mouse pointer has a `gnus-callback' property,
4273 call it with the value of the `gnus-data' text property."
4274   (interactive "e")
4275   (set-buffer (window-buffer (posn-window (event-start event))))
4276   (let* ((pos (posn-point (event-start event)))
4277          (data (get-text-property pos 'gnus-data))
4278          (fun (get-text-property pos 'gnus-callback)))
4279     (goto-char pos)
4280     (when fun
4281       (funcall fun data))))
4282
4283 (defun gnus-article-press-button ()
4284   "Check text at point for a callback function.
4285 If the text at point has a `gnus-callback' property,
4286 call it with the value of the `gnus-data' text property."
4287   (interactive)
4288   (let* ((data (get-text-property (point) 'gnus-data))
4289          (fun (get-text-property (point) 'gnus-callback)))
4290     (when fun
4291       (funcall fun data))))
4292
4293 (defun gnus-article-highlight (&optional force)
4294   "Highlight current article.
4295 This function calls `gnus-article-highlight-headers',
4296 `gnus-article-highlight-citation',
4297 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4298 do the highlighting.  See the documentation for those functions."
4299   (interactive (list 'force))
4300   (gnus-article-highlight-headers)
4301   (gnus-article-highlight-citation force)
4302   (gnus-article-highlight-signature)
4303   (gnus-article-add-buttons force)
4304   (gnus-article-add-buttons-to-head))
4305
4306 (defun gnus-article-highlight-some (&optional force)
4307   "Highlight current article.
4308 This function calls `gnus-article-highlight-headers',
4309 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4310 do the highlighting.  See the documentation for those functions."
4311   (interactive (list 'force))
4312   (gnus-article-highlight-headers)
4313   (gnus-article-highlight-signature)
4314   (gnus-article-add-buttons))
4315
4316 (defun gnus-article-highlight-headers ()
4317   "Highlight article headers as specified by `gnus-header-face-alist'."
4318   (interactive)
4319   (save-excursion
4320     (set-buffer gnus-article-buffer)
4321     (save-restriction
4322       (let ((alist gnus-header-face-alist)
4323             (buffer-read-only nil)
4324             (case-fold-search t)
4325             (inhibit-point-motion-hooks t)
4326             entry regexp header-face field-face from hpoints fpoints)
4327         (article-narrow-to-head)
4328         (while (setq entry (pop alist))
4329           (goto-char (point-min))
4330           (setq regexp (concat "^\\("
4331                                (if (string-equal "" (nth 0 entry))
4332                                    "[^\t ]"
4333                                  (nth 0 entry))
4334                                "\\)")
4335                 header-face (nth 1 entry)
4336                 field-face (nth 2 entry))
4337           (while (and (re-search-forward regexp nil t)
4338                       (not (eobp)))
4339             (beginning-of-line)
4340             (setq from (point))
4341             (unless (search-forward ":" nil t)
4342               (forward-char 1))
4343             (when (and header-face
4344                        (not (memq (point) hpoints)))
4345               (push (point) hpoints)
4346               (gnus-put-text-property from (point) 'face header-face))
4347             (when (and field-face
4348                        (not (memq (setq from (point)) fpoints)))
4349               (push from fpoints)
4350               (if (re-search-forward "^[^ \t]" nil t)
4351                   (forward-char -2)
4352                 (goto-char (point-max)))
4353               (gnus-put-text-property from (point) 'face field-face))))))))
4354
4355 (defun gnus-article-highlight-signature ()
4356   "Highlight the signature in an article.
4357 It does this by highlighting everything after
4358 `gnus-signature-separator' using `gnus-signature-face'."
4359   (interactive)
4360   (save-excursion
4361     (set-buffer gnus-article-buffer)
4362     (let ((buffer-read-only nil)
4363           (inhibit-point-motion-hooks t))
4364       (save-restriction
4365         (when (and gnus-signature-face
4366                    (gnus-article-narrow-to-signature))
4367           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
4368                             'face gnus-signature-face)
4369           (widen)
4370           (gnus-article-search-signature)
4371           (let ((start (match-beginning 0))
4372                 (end (set-marker (make-marker) (1+ (match-end 0)))))
4373             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
4374                                      end)))))))
4375
4376 (defun gnus-button-in-region-p (b e prop)
4377   "Say whether PROP exists in the region."
4378   (text-property-not-all b e prop nil))
4379
4380 (defun gnus-article-add-buttons (&optional force)
4381   "Find external references in the article and make buttons of them.
4382 \"External references\" are things like Message-IDs and URLs, as
4383 specified by `gnus-button-alist'."
4384   (interactive (list 'force))
4385   (save-excursion
4386     (set-buffer gnus-article-buffer)
4387     (let ((buffer-read-only nil)
4388           (inhibit-point-motion-hooks t)
4389           (case-fold-search t)
4390           (alist gnus-button-alist)
4391           beg entry regexp)
4392       ;; Remove all old markers.
4393       (let (marker entry new-list)
4394         (while (setq marker (pop gnus-button-marker-list))
4395           (if (or (< marker (point-min)) (>= marker (point-max)))
4396               (push marker new-list)
4397             (goto-char marker)
4398             (when (setq entry (gnus-button-entry))
4399               (put-text-property (match-beginning (nth 1 entry))
4400                                  (match-end (nth 1 entry))
4401                                  'gnus-callback nil))
4402             (set-marker marker nil)))
4403         (setq gnus-button-marker-list new-list))
4404       ;; We skip the headers.
4405       (article-goto-body)
4406       (setq beg (point))
4407       (while (setq entry (pop alist))
4408         (setq regexp (car entry))
4409         (goto-char beg)
4410         (while (re-search-forward regexp nil t)
4411           (let* ((start (and entry (match-beginning (nth 1 entry))))
4412                  (end (and entry (match-end (nth 1 entry))))
4413                  (from (match-beginning 0)))
4414             (when (and (or (eq t (nth 2 entry))
4415                            (eval (nth 2 entry)))
4416                        (not (gnus-button-in-region-p
4417                              start end 'gnus-callback)))
4418               ;; That optional form returned non-nil, so we add the
4419               ;; button.
4420               (gnus-article-add-button
4421                start end 'gnus-button-push
4422                (car (push (set-marker (make-marker) from)
4423                           gnus-button-marker-list))))))))))
4424
4425 ;; Add buttons to the head of an article.
4426 (defun gnus-article-add-buttons-to-head ()
4427   "Add buttons to the head of the article."
4428   (interactive)
4429   (save-excursion
4430     (set-buffer gnus-article-buffer)
4431     (save-restriction
4432       (let ((buffer-read-only nil)
4433             (inhibit-point-motion-hooks t)
4434             (case-fold-search t)
4435             (alist gnus-header-button-alist)
4436             entry beg end)
4437         (article-narrow-to-head)
4438         (while alist
4439           ;; Each alist entry.
4440           (setq entry (car alist)
4441                 alist (cdr alist))
4442           (goto-char (point-min))
4443           (while (re-search-forward (car entry) nil t)
4444             ;; Each header matching the entry.
4445             (setq beg (match-beginning 0))
4446             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
4447                                (match-beginning 0))
4448                           (point-max)))
4449             (goto-char beg)
4450             (while (re-search-forward (nth 1 entry) end t)
4451               ;; Each match within a header.
4452               (let* ((entry (cdr entry))
4453                      (start (match-beginning (nth 1 entry)))
4454                      (end (match-end (nth 1 entry)))
4455                      (form (nth 2 entry)))
4456                 (goto-char (match-end 0))
4457                 (when (eval form)
4458                   (gnus-article-add-button
4459                    start end (nth 3 entry)
4460                    (buffer-substring (match-beginning (nth 4 entry))
4461                                      (match-end (nth 4 entry)))))))
4462             (goto-char end)))))))
4463
4464 ;;; External functions:
4465
4466 (defun gnus-article-add-button (from to fun &optional data)
4467   "Create a button between FROM and TO with callback FUN and data DATA."
4468   (when gnus-article-button-face
4469     (gnus-overlay-put (gnus-make-overlay from to)
4470                       'face gnus-article-button-face))
4471   (gnus-add-text-properties
4472    from to
4473    (nconc (and gnus-article-mouse-face
4474                (list gnus-mouse-face-prop gnus-article-mouse-face))
4475           (list 'gnus-callback fun)
4476           (and data (list 'gnus-data data))))
4477   (widget-convert-button 'link from to :action 'gnus-widget-press-button
4478                          :button-keymap gnus-widget-button-keymap))
4479
4480 ;;; Internal functions:
4481
4482 (defun gnus-article-set-globals ()
4483   (save-excursion
4484     (set-buffer gnus-summary-buffer)
4485     (gnus-set-global-variables)))
4486
4487 (defun gnus-signature-toggle (end)
4488   (save-excursion
4489     (set-buffer gnus-article-buffer)
4490     (let ((buffer-read-only nil)
4491           (inhibit-point-motion-hooks t))
4492       (if (get-text-property end 'invisible)
4493           (gnus-article-unhide-text end (point-max))
4494         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
4495
4496 (defun gnus-button-entry ()
4497   ;; Return the first entry in `gnus-button-alist' matching this place.
4498   (let ((alist gnus-button-alist)
4499         (entry nil))
4500     (while alist
4501       (setq entry (pop alist))
4502       (if (looking-at (car entry))
4503           (setq alist nil)
4504         (setq entry nil)))
4505     entry))
4506
4507 (defun gnus-button-push (marker)
4508   ;; Push button starting at MARKER.
4509   (save-excursion
4510     (goto-char marker)
4511     (let* ((entry (gnus-button-entry))
4512            (inhibit-point-motion-hooks t)
4513            (fun (nth 3 entry))
4514            (args (mapcar (lambda (group)
4515                            (let ((string (match-string group)))
4516                              (gnus-set-text-properties
4517                               0 (length string) nil string)
4518                              string))
4519                          (nthcdr 4 entry))))
4520       (cond
4521        ((fboundp fun)
4522         (apply fun args))
4523        ((and (boundp fun)
4524              (fboundp (symbol-value fun)))
4525         (apply (symbol-value fun) args))
4526        (t
4527         (gnus-message 1 "You must define `%S' to use this button"
4528                       (cons fun args)))))))
4529
4530 (defun gnus-button-message-id (message-id)
4531   "Fetch MESSAGE-ID."
4532   (save-excursion
4533     (set-buffer gnus-summary-buffer)
4534     (gnus-summary-refer-article message-id)))
4535
4536 (defun gnus-button-fetch-group (address)
4537   "Fetch GROUP specified by ADDRESS."
4538   (if (not (string-match "[:/]" address))
4539       ;; This is just a simple group url.
4540       (gnus-group-read-ephemeral-group address gnus-select-method)
4541     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
4542                            address))
4543         (error "Can't parse %s" address)
4544       (gnus-group-read-ephemeral-group
4545        (match-string 4 address)
4546        `(nntp ,(match-string 1 address)
4547               (nntp-address ,(match-string 1 address))
4548               (nntp-port-number ,(if (match-end 3)
4549                                      (match-string 3 address)
4550                                    "nntp")))))))
4551
4552 (defun gnus-url-parse-query-string (query &optional downcase)
4553   (let (retval pairs cur key val)
4554     (setq pairs (split-string query "&"))
4555     (while pairs
4556       (setq cur (car pairs)
4557             pairs (cdr pairs))
4558       (if (not (string-match "=" cur))
4559           nil                           ; Grace
4560         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
4561               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
4562         (if downcase
4563             (setq key (downcase key)))
4564         (setq cur (assoc key retval))
4565         (if cur
4566             (setcdr cur (cons val (cdr cur)))
4567           (setq retval (cons (list key val) retval)))))
4568     retval))
4569
4570 (defun gnus-url-unhex (x)
4571   (if (> x ?9)
4572       (if (>= x ?a)
4573           (+ 10 (- x ?a))
4574         (+ 10 (- x ?A)))
4575     (- x ?0)))
4576
4577 (defun gnus-url-unhex-string (str &optional allow-newlines)
4578   "Remove %XXX embedded spaces, etc in a url.
4579 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
4580 decoding of carriage returns and line feeds in the string, which is normally
4581 forbidden in URL encoding."
4582   (setq str (or str ""))
4583   (let ((tmp "")
4584         (case-fold-search t))
4585     (while (string-match "%[0-9a-f][0-9a-f]" str)
4586       (let* ((start (match-beginning 0))
4587              (ch1 (gnus-url-unhex (elt str (+ start 1))))
4588              (code (+ (* 16 ch1)
4589                       (gnus-url-unhex (elt str (+ start 2))))))
4590         (setq tmp (concat
4591                    tmp (substring str 0 start)
4592                    (cond
4593                     (allow-newlines
4594                      (char-to-string code))
4595                     ((or (= code ?\n) (= code ?\r))
4596                      " ")
4597                     (t (char-to-string code))))
4598               str (substring str (match-end 0)))))
4599     (setq tmp (concat tmp str))
4600     tmp))
4601
4602 (defun gnus-url-mailto (url)
4603   ;; Send mail to someone
4604   (when (string-match "mailto:/*\\(.*\\)" url)
4605     (setq url (substring url (match-beginning 1) nil)))
4606   (let (to args subject func)
4607     (if (string-match (regexp-quote "?") url)
4608         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
4609               args (gnus-url-parse-query-string
4610                     (substring url (match-end 0) nil) t))
4611       (setq to (gnus-url-unhex-string url)))
4612     (setq args (cons (list "to" to) args)
4613           subject (cdr-safe (assoc "subject" args)))
4614     (message-mail)
4615     (while args
4616       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
4617       (if (fboundp func)
4618           (funcall func)
4619         (message-position-on-field (caar args)))
4620       (insert (mapconcat 'identity (cdar args) ", "))
4621       (setq args (cdr args)))
4622     (if subject
4623         (message-goto-body)
4624       (message-goto-subject))))
4625
4626 (defun gnus-button-mailto (address)
4627   "Mail to ADDRESS."
4628   (set-buffer (gnus-copy-article-buffer))
4629   (message-reply address))
4630
4631 (defalias 'gnus-button-reply 'message-reply)
4632
4633 (defun gnus-button-embedded-url (address)
4634   "Activate ADDRESS with `browse-url'."
4635   (browse-url (gnus-strip-whitespace address)))
4636
4637 ;;; Next/prev buttons in the article buffer.
4638
4639 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
4640 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
4641
4642 (defvar gnus-prev-page-map nil)
4643 (unless gnus-prev-page-map
4644   (setq gnus-prev-page-map (make-sparse-keymap))
4645   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
4646   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
4647
4648 (defun gnus-insert-prev-page-button ()
4649   (let ((buffer-read-only nil))
4650     (gnus-eval-format
4651      gnus-prev-page-line-format nil
4652      `(gnus-prev t local-map ,gnus-prev-page-map
4653                  gnus-callback gnus-article-button-prev-page
4654                  article-type annotation))))
4655
4656 (defvar gnus-next-page-map nil)
4657 (unless gnus-next-page-map
4658   (setq gnus-next-page-map (make-keymap))
4659   (suppress-keymap gnus-prev-page-map)
4660   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
4661   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
4662
4663 (defun gnus-button-next-page ()
4664   "Go to the next page."
4665   (interactive)
4666   (let ((win (selected-window)))
4667     (select-window (get-buffer-window gnus-article-buffer t))
4668     (gnus-article-next-page)
4669     (select-window win)))
4670
4671 (defun gnus-button-prev-page ()
4672   "Go to the prev page."
4673   (interactive)
4674   (let ((win (selected-window)))
4675     (select-window (get-buffer-window gnus-article-buffer t))
4676     (gnus-article-prev-page)
4677     (select-window win)))
4678
4679 (defun gnus-insert-next-page-button ()
4680   (let ((buffer-read-only nil))
4681     (gnus-eval-format gnus-next-page-line-format nil
4682                       `(gnus-next
4683                         t local-map ,gnus-next-page-map
4684                         gnus-callback gnus-article-button-next-page
4685                         article-type annotation))))
4686
4687 (defun gnus-article-button-next-page (arg)
4688   "Go to the next page."
4689   (interactive "P")
4690   (let ((win (selected-window)))
4691     (select-window (get-buffer-window gnus-article-buffer t))
4692     (gnus-article-next-page)
4693     (select-window win)))
4694
4695 (defun gnus-article-button-prev-page (arg)
4696   "Go to the prev page."
4697   (interactive "P")
4698   (let ((win (selected-window)))
4699     (select-window (get-buffer-window gnus-article-buffer t))
4700     (gnus-article-prev-page)
4701     (select-window win)))
4702
4703 (defvar gnus-decode-header-methods
4704   '(mail-decode-encoded-word-region)
4705   "List of methods used to decode headers.
4706
4707 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
4708 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
4709 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
4710 whose names match REGEXP.
4711
4712 For example:
4713 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
4714  mail-decode-encoded-word-region
4715  (\"chinese\" . rfc1843-decode-region))
4716 ")
4717
4718 (defvar gnus-decode-header-methods-cache nil)
4719
4720 (defun gnus-multi-decode-header (start end)
4721   "Apply the functions from `gnus-encoded-word-methods' that match."
4722   (unless (and gnus-decode-header-methods-cache
4723                (eq gnus-newsgroup-name
4724                    (car gnus-decode-header-methods-cache)))
4725     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
4726     (mapcar (lambda (x)
4727               (if (symbolp x)
4728                   (nconc gnus-decode-header-methods-cache (list x))
4729                 (if (and gnus-newsgroup-name
4730                          (string-match (car x) gnus-newsgroup-name))
4731                     (nconc gnus-decode-header-methods-cache
4732                            (list (cdr x))))))
4733           gnus-decode-header-methods))
4734   (let ((xlist gnus-decode-header-methods-cache))
4735     (pop xlist)
4736     (save-restriction
4737       (narrow-to-region start end)
4738       (while xlist
4739         (funcall (pop xlist) (point-min) (point-max))))))
4740
4741 ;;;
4742 ;;; Treatment top-level handling.
4743 ;;;
4744
4745 (defun gnus-treat-article (condition &optional part-number total-parts type)
4746   (let ((length (- (point-max) (point-min)))
4747         (alist gnus-treatment-function-alist)
4748         (article-goto-body-goes-to-point-min-p t)
4749         (treated-type
4750          (or (not type)
4751              (catch 'found
4752                (let ((list gnus-article-treat-types))
4753                  (while list
4754                    (when (string-match (pop list) type)
4755                      (throw 'found t)))))))
4756         (highlightp (gnus-visual-p 'article-highlight 'highlight))
4757         val elem)
4758     (gnus-run-hooks 'gnus-part-display-hook)
4759     (while (setq elem (pop alist))
4760       (setq val
4761             (save-excursion
4762               (if (gnus-buffer-live-p gnus-summary-buffer)
4763                   (set-buffer gnus-summary-buffer))
4764               (symbol-value (car elem))))
4765       (when (and (or (consp val)
4766                      treated-type)
4767                  (gnus-treat-predicate val)
4768                  (or (not (get (car elem) 'highlight))
4769                      highlightp))
4770         (save-restriction
4771           (funcall (cadr elem)))))))
4772
4773 ;; Dynamic variables.
4774 (eval-when-compile
4775   (defvar part-number)
4776   (defvar total-parts)
4777   (defvar type)
4778   (defvar condition)
4779   (defvar length))
4780
4781 (defun gnus-treat-predicate (val)
4782   (cond
4783    ((null val)
4784     nil)
4785    ((and (listp val)
4786          (stringp (car val)))
4787     (apply 'gnus-or (mapcar `(lambda (s)
4788                                (string-match s ,(or gnus-newsgroup-name "")))
4789                             val)))
4790    ((listp val)
4791     (let ((pred (pop val)))
4792       (cond
4793        ((eq pred 'or)
4794         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
4795        ((eq pred 'and)
4796         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
4797        ((eq pred 'not)
4798         (not (gnus-treat-predicate (car val))))
4799        ((eq pred 'typep)
4800         (equal (car val) type))
4801        (t
4802         (error "%S is not a valid predicate" pred)))))
4803    (condition
4804     (eq condition val))
4805    ((eq val t)
4806     t)
4807    ((eq val 'head)
4808     nil)
4809    ((eq val 'last)
4810     (eq part-number total-parts))
4811    ((numberp val)
4812     (< length val))
4813    (t
4814     (error "%S is not a valid value" val))))
4815
4816 (gnus-ems-redefine)
4817
4818 (provide 'gnus-art)
4819
4820 (run-hooks 'gnus-art-load-hook)
4821
4822 ;;; gnus-art.el ends here