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