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