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