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