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