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