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