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