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