Sync with latest Gnus.
[elisp/gnus.git-] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Semi-gnus
2 ;; Copyright (C) 1996, 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
5 ;;      MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;;      Katsumi Yamaoka <yamaoka@jpl.org>
7 ;; Keywords: mail, news, MIME
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (eval-when-compile (require 'cl))
31 (eval-when-compile (require 'static))
32
33 (require 'path-util)
34 (require 'gnus)
35 (require 'gnus-sum)
36 (require 'gnus-spec)
37 (require 'gnus-int)
38 (require 'alist)
39 (require 'mime-view)
40
41 ;; Avoid byte-compile warnings.
42 (eval-when-compile
43   (require 'mm-bodies)
44   (require 'mail-parse)
45   (require 'mm-decode)
46   (require 'mm-view)
47   (require 'wid-edit)
48   (require 'mm-uu)
49   )
50
51 (defgroup gnus-article nil
52   "Article display."
53   :link '(custom-manual "(gnus)The Article Buffer")
54   :group 'gnus)
55
56 (defgroup gnus-article-treat nil
57   "Treating article parts."
58   :link '(custom-manual "(gnus)Article Hiding")
59   :group 'gnus-article)
60
61 (defgroup gnus-article-hiding nil
62   "Hiding article parts."
63   :link '(custom-manual "(gnus)Article Hiding")
64   :group 'gnus-article)
65
66 (defgroup gnus-article-highlight nil
67   "Article highlighting."
68   :link '(custom-manual "(gnus)Article Highlighting")
69   :group 'gnus-article
70   :group 'gnus-visual)
71
72 (defgroup gnus-article-signature nil
73   "Article signatures."
74   :link '(custom-manual "(gnus)Article Signature")
75   :group 'gnus-article)
76
77 (defgroup gnus-article-headers nil
78   "Article headers."
79   :link '(custom-manual "(gnus)Hiding Headers")
80   :group 'gnus-article)
81
82 (defgroup gnus-article-washing nil
83   "Special commands on articles."
84   :link '(custom-manual "(gnus)Article Washing")
85   :group 'gnus-article)
86
87 (defgroup gnus-article-emphasis nil
88   "Fontisizing articles."
89   :link '(custom-manual "(gnus)Article Fontisizing")
90   :group 'gnus-article)
91
92 (defgroup gnus-article-saving nil
93   "Saving articles."
94   :link '(custom-manual "(gnus)Saving Articles")
95   :group 'gnus-article)
96
97 (defgroup gnus-article-mime nil
98   "Worshiping the MIME wonder."
99   :link '(custom-manual "(gnus)Using MIME")
100   :group 'gnus-article)
101
102 (defgroup gnus-article-buttons nil
103   "Pushable buttons in the article buffer."
104   :link '(custom-manual "(gnus)Article Buttons")
105   :group 'gnus-article)
106
107 (defgroup gnus-article-various nil
108   "Other article options."
109   :link '(custom-manual "(gnus)Misc Article")
110   :group 'gnus-article)
111
112 (defcustom gnus-ignored-headers
113   '("^Path:" "^Expires:" "^Date-Received:" "^References:" "^Xref:" "^Lines:"
114     "^Relay-Version:" "^Message-ID:" "^Approved:" "^Sender:" "^Received:"
115     "^X-UIDL:" "^MIME-Version:" "^Return-Path:" "^In-Reply-To:"
116     "^Content-Type:" "^Content-Transfer-Encoding:" "^X-WebTV-Signature:"
117     "^X-MimeOLE:" "^X-MSMail-Priority:" "^X-Priority:" "^X-Loop:"
118     "^X-Authentication-Warning:" "^X-MIME-Autoconverted:" "^X-Face:"
119     "^X-Attribution:" "^X-Originating-IP:" "^Delivered-To:"
120     "^NNTP-[-A-Za-z]+:" "^Distribution:" "^X-no-archive:" "^X-Trace:"
121     "^X-Complaints-To:" "^X-NNTP-Posting-Host:" "^X-Orig.*:"
122     "^Abuse-Reports-To:" "^Cache-Post-Path:" "^X-Article-Creation-Date:"
123     "^X-Poster:" "^X-Mail2News-Path:" "^X-Server-Date:" "^X-Cache:"
124     "^Originator:" "^X-Problems-To:" "^X-Auth-User:" "^X-Post-Time:"
125     "^X-Admin:" "^X-UID:" "^Resent-[-A-Za-z]+:" "^X-Mailing-List:"
126     "^Precedence:" "^Original-[-A-Za-z]+:" "^X-filename:" "^X-Orcpt:"
127     "^Old-Received:" "^X-Pgp" "^X-Auth:" "^X-From-Line:"
128     "^X-Gnus-Article-Number:" "^X-Majordomo:" "^X-Url:" "^X-Sender:"
129     "^MBOX-Line" "^Priority:" "^X-Pgp" "^X400-[-A-Za-z]+:"
130     "^Status:" "^X-Gnus-Mail-Source:" "^Cancel-Lock:"
131     "^X-FTN" "^X-EXP32-SerialNo:" "^Encoding:" "^Importance:"
132     "^Autoforwarded:" "^Original-Encoded-Information-Types:" "^X-Ya-Pop3:"
133     "^X-Face-Version:" "^X-Vms-To:" "^X-ML-NAME:" "^X-ML-COUNT:"
134     "^Mailing-List:" "^X-finfo:" "^X-md5sum:" "^X-md5sum-Origin:"
135     "^X-Sun-Charset:" "^X-Accept-Language:" "^X-Envelope-Sender:"
136     "^List-[A-Za-z]+:" "^X-Listprocessor-Version:"
137     "^X-Received:" "^X-Distribute:" "^X-Sequence:" "^X-Juno-Line-Breaks:"
138     "^X-Notes-Item:" "^X-MS-TNEF-Correlator:" "^x-uunet-gateway:"
139     "^X-Received:" "^Content-length:" "X-precedence:")
140   "*All headers that start with this regexp will be hidden.
141 This variable can also be a list of regexps of headers to be ignored.
142 If `gnus-visible-headers' is non-nil, this variable will be ignored."
143   :type '(choice :custom-show nil
144                  regexp
145                  (repeat regexp))
146   :group 'gnus-article-hiding)
147
148 (defcustom gnus-visible-headers
149   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^[BGF]?Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-From:\\|^X-Sent:"
150   "*All headers that do not match this regexp will be hidden.
151 This variable can also be a list of regexp of headers to remain visible.
152 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
153   :type '(repeat :value-to-internal (lambda (widget value)
154                                       (custom-split-regexp-maybe value))
155                  :match (lambda (widget value)
156                           (or (stringp value)
157                               (widget-editable-list-match widget value)))
158                  regexp)
159   :group 'gnus-article-hiding)
160
161 (defcustom gnus-sorted-header-list
162   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:"
163     "^Followup-To:" "^To:" "^Cc:" "^Date:" "^Organization:")
164   "*This variable is a list of regular expressions.
165 If it is non-nil, headers that match the regular expressions will
166 be placed first in the article buffer in the sequence specified by
167 this list."
168   :type '(repeat regexp)
169   :group 'gnus-article-hiding)
170
171 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
172   "Headers that are only to be displayed if they have interesting data.
173 Possible values in this list are `empty', `newsgroups', `followup-to',
174 `reply-to', `date', `long-to', and `many-to'."
175   :type '(set (const :tag "Headers with no content." empty)
176               (const :tag "Newsgroups with only one group." newsgroups)
177               (const :tag "Followup-to identical to newsgroups." followup-to)
178               (const :tag "Reply-to identical to from." reply-to)
179               (const :tag "Date less than four days old." date)
180               (const :tag "Very long To and/or Cc header." long-to)
181               (const :tag "Multiple To and/or Cc headers." many-to))
182   :group 'gnus-article-hiding)
183
184 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
185   "Regexp matching signature separator.
186 This can also be a list of regexps.  In that case, it will be checked
187 from head to tail looking for a separator.  Searches will be done from
188 the end of the buffer."
189   :type '(repeat string)
190   :group 'gnus-article-signature)
191
192 (defcustom gnus-signature-limit nil
193   "Provide a limit to what is considered a signature.
194 If it is a number, no signature may not be longer (in characters) than
195 that number.  If it is a floating point number, no signature may be
196 longer (in lines) than that number.  If it is a function, the function
197 will be called without any parameters, and if it returns nil, there is
198 no signature in the buffer.  If it is a string, it will be used as a
199 regexp.  If it matches, the text in question is not a signature."
200   :type '(choice (integer :value 200)
201                  (number :value 4.0)
202                  (function :value fun)
203                  (regexp :value ".*"))
204   :group 'gnus-article-signature)
205
206 (defcustom gnus-hidden-properties '(invisible t intangible t)
207   "Property list to use for hiding text."
208   :type 'sexp
209   :group 'gnus-article-hiding)
210
211 (defcustom gnus-article-x-face-command
212   (cond
213    ((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)
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       (when cte
1615         (setq cte (mail-header-strip cte)))
1616       (if (and ctl (not (string-match "/" (car ctl)))) 
1617           (setq ctl nil))
1618       (goto-char (point-max)))
1619     (forward-line 1)
1620     (save-restriction
1621       (narrow-to-region (point) (point-max))
1622       (when (and (or (not ctl)
1623                      (equal (car ctl) "text/plain")))
1624         (mm-decode-body
1625          charset (and cte (intern (downcase
1626                                    (gnus-strip-whitespace cte))))
1627          (car ctl)))))))
1628
1629 (defun article-decode-encoded-words ()
1630   "Remove encoded-word encoding from headers."
1631   (let (buffer-read-only)
1632     (let ((charset (save-excursion
1633                      (set-buffer gnus-summary-buffer)
1634                      default-mime-charset)))
1635       (mime-decode-header-in-buffer charset)
1636       )))
1637
1638 (defun article-de-quoted-unreadable (&optional force)
1639   "Translate a quoted-printable-encoded article.
1640 If FORCE, decode the article whether it is marked as quoted-printable
1641 or not."
1642   (interactive (list 'force))
1643   (save-excursion
1644     (let ((buffer-read-only nil)
1645           (type (gnus-fetch-field "content-transfer-encoding"))
1646           (charset gnus-newsgroup-charset))
1647       (when (or force
1648                 (and type (string-match "quoted-printable" (downcase type))))
1649         (article-goto-body)
1650         (quoted-printable-decode-region (point) (point-max) charset)))))
1651
1652 (eval-when-compile
1653   (require 'rfc1843))
1654
1655 (defun article-decode-HZ ()
1656   "Translate a HZ-encoded article."
1657   (interactive)
1658   (require 'rfc1843)
1659   (save-excursion
1660     (let ((buffer-read-only nil))
1661       (rfc1843-decode-region (point-min) (point-max)))))
1662
1663 (defun article-hide-list-identifiers ()
1664   "Remove list identifies from the Subject header.
1665 The `gnus-list-identifiers' variable specifies what to do."
1666   (interactive)
1667   (save-excursion
1668     (save-restriction
1669       (let ((inhibit-point-motion-hooks t)
1670             buffer-read-only)
1671         (article-narrow-to-head)
1672         (let ((regexp (if (stringp gnus-list-identifiers) gnus-list-identifiers
1673                         (mapconcat 'identity gnus-list-identifiers " *\\|"))))
1674           (when regexp
1675             (goto-char (point-min))
1676             (when (re-search-forward
1677                    (concat "^Subject: +\\(Re: +\\)?\\(" regexp " *\\)")
1678                    nil t)
1679               (delete-region (match-beginning 2) (match-end 0)))))))))
1680
1681 (defun article-hide-pgp ()
1682   "Remove any PGP headers and signatures in the current article."
1683   (interactive)
1684   (save-excursion
1685     (save-restriction
1686       (let ((inhibit-point-motion-hooks t)
1687             buffer-read-only beg end)
1688         (article-goto-body)
1689         ;; Hide the "header".
1690         (when (re-search-forward "^-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
1691           (push 'pgp gnus-article-wash-types)
1692           (delete-region (match-beginning 0) (match-end 0))
1693           ;; Remove armor headers (rfc2440 6.2)
1694           (delete-region (point) (or (re-search-forward "^[ \t]*\n" nil t)
1695                                      (point)))
1696           (setq beg (point))
1697           ;; Hide the actual signature.
1698           (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
1699                (setq end (1+ (match-beginning 0)))
1700                (delete-region
1701                 end
1702                 (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
1703                     (match-end 0)
1704                   ;; Perhaps we shouldn't hide to the end of the buffer
1705                   ;; if there is no end to the signature?
1706                   (point-max))))
1707           ;; Hide "- " PGP quotation markers.
1708           (when (and beg end)
1709             (narrow-to-region beg end)
1710             (goto-char (point-min))
1711             (while (re-search-forward "^- " nil t)
1712               (delete-region
1713                (match-beginning 0) (match-end 0)))
1714             (widen))
1715           (gnus-run-hooks 'gnus-article-hide-pgp-hook))))))
1716
1717 (defun article-hide-pem (&optional arg)
1718   "Toggle hiding of any PEM headers and signatures in the current article.
1719 If given a negative prefix, always show; if given a positive prefix,
1720 always hide."
1721   (interactive (gnus-article-hidden-arg))
1722   (unless (gnus-article-check-hidden-text 'pem arg)
1723     (save-excursion
1724       (let (buffer-read-only end)
1725         (goto-char (point-min))
1726         ;; Hide the horrendously ugly "header".
1727         (when (and (search-forward
1728                     "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
1729                     nil t)
1730                    (setq end (1+ (match-beginning 0))))
1731           (push 'pem gnus-article-wash-types)
1732           (gnus-article-hide-text-type
1733            end
1734            (if (search-forward "\n\n" nil t)
1735                (match-end 0)
1736              (point-max))
1737            'pem)
1738           ;; Hide the trailer as well
1739           (when (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
1740                                 nil t)
1741             (gnus-article-hide-text-type
1742              (match-beginning 0) (match-end 0) 'pem)))))))
1743
1744 (defun article-strip-banner ()
1745   "Strip the banner specified by the `banner' group parameter."
1746   (interactive)
1747   (save-excursion
1748     (save-restriction
1749       (let ((inhibit-point-motion-hooks t)
1750             (banner (gnus-group-get-parameter gnus-newsgroup-name 'banner))
1751             (gnus-signature-limit nil)
1752             buffer-read-only beg end)
1753         (when banner
1754           (article-goto-body)
1755           (cond
1756            ((eq banner 'signature)
1757             (when (gnus-article-narrow-to-signature)
1758               (widen)
1759               (forward-line -1)
1760               (delete-region (point) (point-max))))
1761            ((stringp banner)
1762             (while (re-search-forward banner nil t)
1763               (delete-region (match-beginning 0) (match-end 0))))))))))
1764
1765 (defun article-babel ()
1766   "Translate article using an online translation service."
1767   (interactive)
1768   (require 'babel)
1769   (save-excursion
1770     (set-buffer gnus-article-buffer)
1771     (when (article-goto-body)
1772       (let* ((buffer-read-only nil)
1773              (start (point))
1774              (end (point-max))
1775              (orig (buffer-substring start end))
1776              (trans (babel-as-string orig)))
1777         (save-restriction
1778           (narrow-to-region start end)
1779           (delete-region start end)
1780           (insert trans))))))
1781
1782 (defun article-hide-signature (&optional arg)
1783   "Hide the signature in the current article.
1784 If given a negative prefix, always show; if given a positive prefix,
1785 always hide."
1786   (interactive (gnus-article-hidden-arg))
1787   (save-excursion
1788     (save-restriction
1789       (if (interactive-p)
1790           (progn
1791             (widen)
1792             (article-goto-body))
1793         (goto-char (point-min)))
1794       (unless (gnus-article-check-hidden-text 'signature arg)
1795         (let ((buffer-read-only nil)
1796               (button (point)))
1797           (while (setq button (text-property-any button (point-max)
1798                                                  'gnus-callback
1799                                                  'gnus-signature-toggle))
1800             (setq button (text-property-not-all button (point-max)
1801                                                 'gnus-callback
1802                                                 'gnus-signature-toggle))
1803             (when (and button (not (eobp)))
1804               (gnus-article-hide-text-type
1805                (1+ button)
1806                (next-single-property-change (1+ button) 'mime-view-entity
1807                                             nil (point-max))
1808                'signature))))))))
1809
1810 (defun article-strip-headers-in-body ()
1811   "Strip offensive headers from bodies."
1812   (interactive)
1813   (save-excursion
1814     (article-goto-body)
1815     (let ((case-fold-search t))
1816       (when (looking-at "x-no-archive:")
1817         (gnus-delete-line)))))
1818
1819 (defun article-strip-leading-blank-lines ()
1820   "Remove all blank lines from the beginning of the article."
1821   (interactive)
1822   (save-excursion
1823     (let ((inhibit-point-motion-hooks t)
1824           buffer-read-only)
1825       (when (article-goto-body)
1826         (while (and (not (eobp))
1827                     (looking-at "[ \t]*$"))
1828           (gnus-delete-line))))))
1829
1830 (defun article-narrow-to-head ()
1831   "Narrow the buffer to the head of the message.
1832 Point is left at the beginning of the narrowed-to region."
1833   (narrow-to-region
1834    (goto-char (point-min))
1835    (if (search-forward "\n\n" nil 1)
1836        (1- (point))
1837      (point-max)))
1838   (goto-char (point-min)))
1839
1840 (defun article-goto-body ()
1841   "Place point at the start of the body."
1842   (goto-char (point-min))
1843   (cond
1844    ;; This variable is only bound when dealing with separate
1845    ;; MIME body parts.
1846    (article-goto-body-goes-to-point-min-p
1847     t)
1848    ((search-forward "\n\n" nil t)
1849     t)
1850    (t
1851     (goto-char (point-max))
1852     nil)))
1853
1854 (defun article-strip-multiple-blank-lines ()
1855   "Replace consecutive blank lines with one empty line."
1856   (interactive)
1857   (save-excursion
1858     (let ((inhibit-point-motion-hooks t)
1859           buffer-read-only)
1860       ;; First make all blank lines empty.
1861       (article-goto-body)
1862       (while (re-search-forward "^[ \t]+$" nil t)
1863         (unless (gnus-annotation-in-region-p
1864                  (match-beginning 0) (match-end 0))
1865           (replace-match "" nil t)))
1866       ;; Then replace multiple empty lines with a single empty line.
1867       (article-goto-body)
1868       (while (re-search-forward "\n\n\n+" nil t)
1869         (unless (gnus-annotation-in-region-p
1870                  (match-beginning 0) (match-end 0))
1871           (replace-match "\n\n" t t))))))
1872
1873 (defun article-strip-leading-space ()
1874   "Remove all white space from the beginning of the lines in the article."
1875   (interactive)
1876   (save-excursion
1877     (let ((inhibit-point-motion-hooks t)
1878           buffer-read-only)
1879       (article-goto-body)
1880       (while (re-search-forward "^[ \t]+" nil t)
1881         (replace-match "" t t)))))
1882
1883 (defun article-strip-trailing-space ()
1884   "Remove all white space from the end of the lines in the article."
1885   (interactive)
1886   (save-excursion
1887     (let ((inhibit-point-motion-hooks t)
1888           buffer-read-only)
1889       (article-goto-body)
1890       (while (re-search-forward "[ \t]+$" nil t)
1891         (replace-match "" t t)))))
1892
1893 (defun article-strip-blank-lines ()
1894   "Strip leading, trailing and multiple blank lines."
1895   (interactive)
1896   (article-strip-leading-blank-lines)
1897   (article-remove-trailing-blank-lines)
1898   (article-strip-multiple-blank-lines))
1899
1900 (defun article-strip-all-blank-lines ()
1901   "Strip all blank lines."
1902   (interactive)
1903   (save-excursion
1904     (let ((inhibit-point-motion-hooks t)
1905           buffer-read-only)
1906       (article-goto-body)
1907       (while (re-search-forward "^[ \t]*\n" nil t)
1908         (replace-match "" t t)))))
1909
1910 (defun gnus-article-narrow-to-signature ()
1911   "Narrow to the signature; return t if a signature is found, else nil."
1912   (let ((inhibit-point-motion-hooks t))
1913     (when (gnus-article-search-signature)
1914       (forward-line 1)
1915       ;; Check whether we have some limits to what we consider
1916       ;; to be a signature.
1917       (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
1918                       (list gnus-signature-limit)))
1919             limit limited)
1920         (while (setq limit (pop limits))
1921           (if (or (and (integerp limit)
1922                        (< (- (point-max) (point)) limit))
1923                   (and (floatp limit)
1924                        (< (count-lines (point) (point-max)) limit))
1925                   (and (gnus-functionp limit)
1926                        (funcall limit))
1927                   (and (stringp limit)
1928                        (not (re-search-forward limit nil t))))
1929               ()                        ; This limit did not succeed.
1930             (setq limited t
1931                   limits nil)))
1932         (unless limited
1933           (narrow-to-region (point) (point-max))
1934           t)))))
1935
1936 (defun gnus-article-search-signature ()
1937   "Search the current buffer for the signature separator.
1938 Put point at the beginning of the signature separator."
1939   (let ((cur (point)))
1940     (goto-char (point-max))
1941     (if (if (stringp gnus-signature-separator)
1942             (re-search-backward gnus-signature-separator nil t)
1943           (let ((seps gnus-signature-separator))
1944             (while (and seps
1945                         (not (re-search-backward (car seps) nil t)))
1946               (pop seps))
1947             seps))
1948         t
1949       (goto-char cur)
1950       nil)))
1951
1952 (defun gnus-article-hidden-arg ()
1953   "Return the current prefix arg as a number, or 0 if no prefix."
1954   (list (if current-prefix-arg
1955             (prefix-numeric-value current-prefix-arg)
1956           0)))
1957
1958 (defun gnus-article-check-hidden-text (type arg)
1959   "Return nil if hiding is necessary.
1960 Arg can be nil or a number.  Nil and positive means hide, negative
1961 means show, 0 means toggle."
1962   (save-excursion
1963     (save-restriction
1964       (let ((hide (gnus-article-hidden-text-p type)))
1965         (cond
1966          ((or (null arg)
1967               (> arg 0))
1968           nil)
1969          ((< arg 0)
1970           (gnus-article-show-hidden-text type))
1971          (t
1972           (if (eq hide 'hidden)
1973               (gnus-article-show-hidden-text type)
1974             nil)))))))
1975
1976 (defun gnus-article-hidden-text-p (type)
1977   "Say whether the current buffer contains hidden text of type TYPE."
1978   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
1979     (while (and pos
1980                 (not (get-text-property pos 'invisible))
1981                 (not (get-text-property pos 'dummy-invisible)))
1982       (setq pos
1983             (text-property-any (1+ pos) (point-max) 'article-type type)))
1984     (if pos
1985         'hidden
1986       nil)))
1987
1988 (defun gnus-article-show-hidden-text (type &optional hide)
1989   "Show all hidden text of type TYPE.
1990 If HIDE, hide the text instead."
1991   (save-excursion
1992     (let ((buffer-read-only nil)
1993           (inhibit-point-motion-hooks t)
1994           (end (point-min))
1995           beg)
1996       (while (setq beg (text-property-any end (point-max) 'article-type type))
1997         (goto-char beg)
1998         (setq end (or
1999                    (text-property-not-all beg (point-max) 'article-type type)
2000                    (point-max)))
2001         (if hide
2002             (gnus-article-hide-text beg end gnus-hidden-properties)
2003           (gnus-article-unhide-text beg end))
2004         (goto-char end))
2005       t)))
2006
2007 (defconst article-time-units
2008   `((year . ,(* 365.25 24 60 60))
2009     (week . ,(* 7 24 60 60))
2010     (day . ,(* 24 60 60))
2011     (hour . ,(* 60 60))
2012     (minute . 60)
2013     (second . 1))
2014   "Mapping from time units to seconds.")
2015
2016 (defun article-date-ut (&optional type highlight header)
2017   "Convert DATE date to universal time in the current article.
2018 If TYPE is `local', convert to local time; if it is `lapsed', output
2019 how much time has lapsed since DATE.  For `lapsed', the value of
2020 `gnus-article-date-lapsed-new-header' says whether the \"X-Sent:\" header
2021 should replace the \"Date:\" one, or should be added below it."
2022   (interactive (list 'ut t))
2023   (let* ((header (or header
2024                      (and (eq 1 (point-min))
2025                           (mail-header-date (save-excursion
2026                                               (set-buffer gnus-summary-buffer)
2027                                               gnus-current-headers)))
2028                      (message-fetch-field "date")
2029                      ""))
2030          (date (if (vectorp header) (mail-header-date header)
2031                  header))
2032          (inhibit-point-motion-hooks t)
2033          bface eface date-pos)
2034     (when (and date (not (string= date "")))
2035       (save-excursion
2036         (save-restriction
2037           (article-narrow-to-head)
2038           (when (or (and (eq type 'lapsed)
2039                          gnus-article-date-lapsed-new-header
2040                          ;; Attempt to get the face of X-Sent first.
2041                          (re-search-forward "^X-Sent:[ \t]" nil t))
2042                     (re-search-forward "^Date:[ \t]" nil t)
2043                     ;; If Date is missing, try again for X-Sent.
2044                     (re-search-forward "^X-Sent:[ \t]" nil t))
2045             (setq bface (get-text-property (gnus-point-at-bol) 'face)
2046                   date (or (get-text-property (gnus-point-at-bol)
2047                                                 'original-date)
2048                              date)
2049                   eface (get-text-property (1- (gnus-point-at-eol))
2050                                            'face)))
2051           (let ((buffer-read-only nil))
2052             ;; Delete any old X-Sent headers.
2053             (when (setq date-pos
2054                         (text-property-any (point-min) (point-max)
2055                                            'article-date-lapsed t))
2056               (goto-char (setq date-pos (set-marker (make-marker) date-pos)))
2057               (delete-region (match-beginning 0)
2058                              (progn (forward-line 1) (point))))
2059             (goto-char (point-min))
2060             ;; Delete any old Date headers.
2061             (while (re-search-forward "^Date:[ \t]" nil t)
2062               (unless date-pos
2063                 (setq date-pos (match-beginning 0)))
2064               (unless (and (eq type 'lapsed)
2065                            gnus-article-date-lapsed-new-header)
2066                 (delete-region (match-beginning 0)
2067                                (progn (message-next-header) (point)))))
2068             (if date-pos
2069                 (progn
2070                   (goto-char date-pos)
2071                   (unless (bolp)
2072                     ;; Possibly, Date has been deleted.
2073                     (insert "\n"))
2074                   (when (and (eq type 'lapsed)
2075                              gnus-article-date-lapsed-new-header
2076                              (looking-at "Date:"))
2077                     (forward-line 1)))
2078               (goto-char (point-min)))
2079             (insert (article-make-date-line date type))
2080             (when (eq type 'lapsed)
2081               (put-text-property (gnus-point-at-bol) (point)
2082                                  'article-date-lapsed t))
2083             (insert "\n")
2084             (forward-line -1)
2085             ;; Do highlighting.
2086             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
2087               (put-text-property (match-beginning 1) (1+ (match-end 1))
2088                                  'original-date date)
2089               (put-text-property (match-beginning 1) (1+ (match-end 1))
2090                                  'face bface)
2091               (put-text-property (match-beginning 2) (match-end 2)
2092                                  'face eface))))))))
2093
2094 (defun article-make-date-line (date type)
2095   "Return a DATE line of TYPE."
2096   (let ((time (condition-case ()
2097                   (date-to-time date)
2098                 (error '(0 0)))))
2099     (cond
2100      ;; Convert to the local timezone.  We have to slap a
2101      ;; `condition-case' round the calls to the timezone
2102      ;; functions since they aren't particularly resistant to
2103      ;; buggy dates.
2104      ((eq type 'local)
2105       (let ((tz (car (current-time-zone time))))
2106         (format "Date: %s %s%02d%02d" (current-time-string time)
2107                 (if (> tz 0) "+" "-") (/ (abs tz) 3600) 
2108                 (/ (% (abs tz) 3600) 60))))
2109      ;; Convert to Universal Time.
2110      ((eq type 'ut)
2111       (concat "Date: "
2112               (current-time-string
2113                (let* ((e (parse-time-string date))
2114                       (tm (apply 'encode-time e))
2115                       (ms (car tm))
2116                       (ls (- (cadr tm) (car (current-time-zone time)))))
2117                  (cond ((< ls 0) (list (1- ms) (+ ls 65536)))
2118                        ((> ls 65535) (list (1+ ms) (- ls 65536)))
2119                        (t (list ms ls)))))
2120               " UT"))
2121      ;; Get the original date from the article.
2122      ((eq type 'original)
2123       (concat "Date: " (if (string-match "\n+$" date)
2124                            (substring date 0 (match-beginning 0))
2125                          date)))
2126      ;; Let the user define the format.
2127      ((eq type 'user)
2128       (if (gnus-functionp gnus-article-time-format)
2129           (funcall gnus-article-time-format time)
2130         (concat
2131          "Date: "
2132          (format-time-string gnus-article-time-format time))))
2133      ;; ISO 8601.
2134      ((eq type 'iso8601)
2135       (let ((tz (car (current-time-zone time))))
2136         (concat
2137          "Date: "
2138          (format-time-string "%Y%m%dT%H%M%S" time)
2139          (format "%s%02d%02d"
2140                  (if (> tz 0) "+" "-") (/ (abs tz) 3600) 
2141                  (/ (% (abs tz) 3600) 60)))))
2142      ;; Do an X-Sent lapsed format.
2143      ((eq type 'lapsed)
2144       ;; If the date is seriously mangled, the timezone functions are
2145       ;; liable to bug out, so we ignore all errors.
2146       (let* ((now (current-time))
2147              (real-time (subtract-time now time))
2148              (real-sec (and real-time
2149                             (+ (* (float (car real-time)) 65536)
2150                                (cadr real-time))))
2151              (sec (and real-time (abs real-sec)))
2152              num prev)
2153         (cond
2154          ((null real-time)
2155           "X-Sent: Unknown")
2156          ((zerop sec)
2157           "X-Sent: Now")
2158          (t
2159           (concat
2160            "X-Sent: "
2161            ;; This is a bit convoluted, but basically we go
2162            ;; through the time units for years, weeks, etc,
2163            ;; and divide things to see whether that results
2164            ;; in positive answers.
2165            (mapconcat
2166             (lambda (unit)
2167               (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
2168                   ;; The (remaining) seconds are too few to
2169                   ;; be divided into this time unit.
2170                   ""
2171                 ;; It's big enough, so we output it.
2172                 (setq sec (- sec (* num (cdr unit))))
2173                 (prog1
2174                     (concat (if prev ", " "") (int-to-string
2175                                                (floor num))
2176                             " " (symbol-name (car unit))
2177                             (if (> num 1) "s" ""))
2178                   (setq prev t))))
2179             article-time-units "")
2180            ;; If dates are odd, then it might appear like the
2181            ;; article was sent in the future.
2182            (if (> real-sec 0)
2183                " ago"
2184              " in the future"))))))
2185      (t
2186       (error "Unknown conversion type: %s" type)))))
2187
2188 (defun article-date-local (&optional highlight)
2189   "Convert the current article date to the local timezone."
2190   (interactive (list t))
2191   (article-date-ut 'local highlight))
2192
2193 (defun article-date-original (&optional highlight)
2194   "Convert the current article date to what it was originally.
2195 This is only useful if you have used some other date conversion
2196 function and want to see what the date was before converting."
2197   (interactive (list t))
2198   (article-date-ut 'original highlight))
2199
2200 (defun article-date-lapsed (&optional highlight)
2201   "Convert the current article date to time lapsed since it was sent."
2202   (interactive (list t))
2203   (article-date-ut 'lapsed highlight))
2204
2205 (defun article-update-date-lapsed ()
2206   "Function to be run from a timer to update the lapsed time line."
2207   (let (deactivate-mark)
2208     (save-excursion
2209       (ignore-errors
2210         (walk-windows
2211          (lambda (w)
2212            (set-buffer (window-buffer w))
2213            (when (eq major-mode 'gnus-article-mode)
2214              (goto-char (point-min))
2215              (when (re-search-forward "^X-Sent:" nil t)
2216                (article-date-lapsed t))))
2217          nil 'visible)))))
2218
2219 (defun gnus-start-date-timer (&optional n)
2220   "Start a timer to update the X-Sent header in the article buffers.
2221 The numerical prefix says how frequently (in seconds) the function
2222 is to run."
2223   (interactive "p")
2224   (unless n
2225     (setq n 1))
2226   (gnus-stop-date-timer)
2227   (setq article-lapsed-timer
2228         (nnheader-run-at-time 1 n 'article-update-date-lapsed)))
2229
2230 (defun gnus-stop-date-timer ()
2231   "Stop the X-Sent timer."
2232   (interactive)
2233   (when article-lapsed-timer
2234     (nnheader-cancel-timer article-lapsed-timer)
2235     (setq article-lapsed-timer nil)))
2236
2237 (defun article-date-user (&optional highlight)
2238   "Convert the current article date to the user-defined format.
2239 This format is defined by the `gnus-article-time-format' variable."
2240   (interactive (list t))
2241   (article-date-ut 'user highlight))
2242
2243 (defun article-date-iso8601 (&optional highlight)
2244   "Convert the current article date to ISO8601."
2245   (interactive (list t))
2246   (article-date-ut 'iso8601 highlight))
2247
2248 (defun article-show-all ()
2249   "Show all hidden text in the article buffer."
2250   (interactive)
2251   (save-excursion
2252     (widen)
2253     (let ((buffer-read-only nil))
2254       (gnus-article-unhide-text (point-min) (point-max))
2255       (gnus-remove-text-with-property 'gnus-prev)
2256       (gnus-remove-text-with-property 'gnus-next))))
2257
2258 (defun article-show-all-headers ()
2259   "Show all hidden headers in the article buffer."
2260   (interactive)
2261   (save-excursion
2262     (save-restriction
2263       (widen)
2264       (article-narrow-to-head)
2265       (let ((buffer-read-only nil))
2266         (gnus-article-unhide-text (point-min) (point-max))))))
2267
2268 (defun article-emphasize (&optional arg)
2269   "Emphasize text according to `gnus-emphasis-alist'."
2270   (interactive (gnus-article-hidden-arg))
2271   (unless (gnus-article-check-hidden-text 'emphasis arg)
2272     (save-excursion
2273       (let ((alist (or 
2274                     (condition-case nil
2275                         (with-current-buffer gnus-summary-buffer 
2276                           gnus-article-emphasis-alist) 
2277                       (error))
2278                     gnus-emphasis-alist))
2279             (buffer-read-only nil)
2280             (props (append '(article-type emphasis)
2281                            gnus-hidden-properties))
2282             regexp elem beg invisible visible face)
2283         (article-goto-body)
2284         (setq beg (point))
2285         (while (setq elem (pop alist))
2286           (goto-char beg)
2287           (setq regexp (car elem)
2288                 invisible (nth 1 elem)
2289                 visible (nth 2 elem)
2290                 face (nth 3 elem))
2291           (while (re-search-forward regexp nil t)
2292             (when (and (match-beginning visible) (match-beginning invisible))
2293               (push 'emphasis gnus-article-wash-types)
2294               (gnus-article-hide-text
2295                (match-beginning invisible) (match-end invisible) props)
2296               (gnus-article-unhide-text-type
2297                (match-beginning visible) (match-end visible) 'emphasis)
2298               (gnus-put-text-property-excluding-newlines
2299                (match-beginning visible) (match-end visible) 'face face)
2300               (goto-char (match-end invisible)))))))))
2301
2302 (defun gnus-article-setup-highlight-words (&optional highlight-words)
2303   "Setup newsgroup emphasis alist."
2304   (unless gnus-article-emphasis-alist
2305     (let ((name (and gnus-newsgroup-name
2306                      (gnus-group-real-name gnus-newsgroup-name))))
2307       (make-local-variable 'gnus-article-emphasis-alist)
2308       (setq gnus-article-emphasis-alist
2309             (nconc
2310              (let ((alist gnus-group-highlight-words-alist) elem highlight)
2311                (while (setq elem (pop alist))
2312                  (when (and name (string-match (car elem) name))
2313                    (setq alist nil
2314                          highlight (copy-sequence (cdr elem)))))
2315                highlight)
2316              (copy-sequence highlight-words)
2317              (if gnus-newsgroup-name
2318                  (copy-sequence (gnus-group-find-parameter
2319                                  gnus-newsgroup-name 'highlight-words t)))
2320              gnus-emphasis-alist)))))
2321
2322 (defvar gnus-summary-article-menu)
2323 (defvar gnus-summary-post-menu)
2324
2325 ;;; Saving functions.
2326
2327 (defun gnus-article-save (save-buffer file &optional num)
2328   "Save the currently selected article."
2329   (unless gnus-save-all-headers
2330     ;; Remove headers according to `gnus-saved-headers'.
2331     (let ((gnus-visible-headers
2332            (or gnus-saved-headers gnus-visible-headers))
2333           (gnus-article-buffer save-buffer))
2334       (save-excursion
2335         (set-buffer save-buffer)
2336         (article-hide-headers 1 t))))
2337   (save-window-excursion
2338     (if (not gnus-default-article-saver)
2339         (error "No default saver is defined")
2340       ;; !!! Magic!  The saving functions all save
2341       ;; `gnus-save-article-buffer' (or so they think), but we
2342       ;; bind that variable to our save-buffer.
2343       (set-buffer gnus-article-buffer)
2344       (let* ((gnus-save-article-buffer save-buffer)
2345              (filename
2346               (cond
2347                ((not gnus-prompt-before-saving) 'default)
2348                ((eq gnus-prompt-before-saving 'always) nil)
2349                (t file)))
2350              (gnus-number-of-articles-to-be-saved
2351               (when (eq gnus-prompt-before-saving t)
2352                 num)))                  ; Magic
2353         (set-buffer gnus-article-current-summary)
2354         (funcall gnus-default-article-saver filename)))))
2355
2356 (defun gnus-read-save-file-name (prompt &optional filename
2357                                         function group headers variable)
2358   (let ((default-name
2359           (funcall function group headers (symbol-value variable)))
2360         result)
2361     (setq
2362      result
2363      (cond
2364       ((eq filename 'default)
2365        default-name)
2366       ((eq filename t)
2367        default-name)
2368       (filename filename)
2369       (t
2370        (let* ((split-name (gnus-get-split-value gnus-split-methods))
2371               (prompt
2372                (format prompt
2373                        (if (and gnus-number-of-articles-to-be-saved
2374                                 (> gnus-number-of-articles-to-be-saved 1))
2375                            (format "these %d articles"
2376                                    gnus-number-of-articles-to-be-saved)
2377                          "this article")))
2378               (file
2379                ;; Let the split methods have their say.
2380                (cond
2381                 ;; No split name was found.
2382                 ((null split-name)
2383                  (read-file-name
2384                   (concat prompt " (default "
2385                           (file-name-nondirectory default-name) ") ")
2386                   (file-name-directory default-name)
2387                   default-name))
2388                 ;; A single group name is returned.
2389                 ((stringp split-name)
2390                  (setq default-name
2391                        (funcall function split-name headers
2392                                 (symbol-value variable)))
2393                  (read-file-name
2394                   (concat prompt " (default "
2395                           (file-name-nondirectory default-name) ") ")
2396                   (file-name-directory default-name)
2397                   default-name))
2398                 ;; A single split name was found
2399                 ((= 1 (length split-name))
2400                  (let* ((name (expand-file-name
2401                                (car split-name) gnus-article-save-directory))
2402                         (dir (cond ((file-directory-p name)
2403                                     (file-name-as-directory name))
2404                                    ((file-exists-p name) name)
2405                                    (t gnus-article-save-directory))))
2406                    (read-file-name
2407                     (concat prompt " (default " name ") ")
2408                     dir name)))
2409                 ;; A list of splits was found.
2410                 (t
2411                  (setq split-name (nreverse split-name))
2412                  (let (result)
2413                    (let ((file-name-history
2414                           (nconc split-name file-name-history)))
2415                      (setq result
2416                            (expand-file-name
2417                             (read-file-name
2418                              (concat prompt " (`M-p' for defaults) ")
2419                              gnus-article-save-directory
2420                              (car split-name))
2421                             gnus-article-save-directory)))
2422                    (car (push result file-name-history)))))))
2423          ;; Create the directory.
2424          (gnus-make-directory (file-name-directory file))
2425          ;; If we have read a directory, we append the default file name.
2426          (when (file-directory-p file)
2427            (setq file (concat (file-name-as-directory file)
2428                               (file-name-nondirectory default-name))))
2429          ;; Possibly translate some characters.
2430          (nnheader-translate-file-chars file)))))
2431     (gnus-make-directory (file-name-directory result))
2432     (set variable result)))
2433
2434 (defun gnus-article-archive-name (group)
2435   "Return the first instance of an \"Archive-name\" in the current buffer."
2436   (let ((case-fold-search t))
2437     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
2438       (nnheader-concat gnus-article-save-directory
2439                        (match-string 1)))))
2440
2441 (defun gnus-article-nndoc-name (group)
2442   "If GROUP is an nndoc group, return the name of the parent group."
2443   (when (eq (car (gnus-find-method-for-group group)) 'nndoc)
2444     (gnus-group-get-parameter group 'save-article-group)))
2445
2446 (defun gnus-summary-save-in-rmail (&optional filename)
2447   "Append this article to Rmail file.
2448 Optional argument FILENAME specifies file name.
2449 Directory to save to is default to `gnus-article-save-directory'."
2450   (setq filename (gnus-read-save-file-name
2451                   "Save %s in rmail file:" filename
2452                   gnus-rmail-save-name gnus-newsgroup-name
2453                   gnus-current-headers 'gnus-newsgroup-last-rmail))
2454   (gnus-eval-in-buffer-window gnus-save-article-buffer
2455     (save-excursion
2456       (save-restriction
2457         (widen)
2458         (gnus-output-to-rmail filename))))
2459   filename)
2460
2461 (defun gnus-summary-save-in-mail (&optional filename)
2462   "Append this article to Unix mail file.
2463 Optional argument FILENAME specifies file name.
2464 Directory to save to is default to `gnus-article-save-directory'."
2465   (setq filename (gnus-read-save-file-name
2466                   "Save %s in Unix mail file:" filename
2467                   gnus-mail-save-name gnus-newsgroup-name
2468                   gnus-current-headers 'gnus-newsgroup-last-mail))
2469   (gnus-eval-in-buffer-window gnus-save-article-buffer
2470     (save-excursion
2471       (save-restriction
2472         (widen)
2473         (if (and (file-readable-p filename)
2474                  (mail-file-babyl-p filename))
2475             (rmail-output-to-rmail-file filename t)
2476           (gnus-output-to-mail filename)))))
2477   filename)
2478
2479 (defun gnus-summary-save-in-file (&optional filename overwrite)
2480   "Append this article to file.
2481 Optional argument FILENAME specifies file name.
2482 Directory to save to is default to `gnus-article-save-directory'."
2483   (setq filename (gnus-read-save-file-name
2484                   "Save %s in file:" filename
2485                   gnus-file-save-name gnus-newsgroup-name
2486                   gnus-current-headers 'gnus-newsgroup-last-file))
2487   (gnus-eval-in-buffer-window gnus-save-article-buffer
2488     (save-excursion
2489       (save-restriction
2490         (widen)
2491         (when (and overwrite
2492                    (file-exists-p filename))
2493           (delete-file filename))
2494         (gnus-output-to-file filename))))
2495   filename)
2496
2497 (defun gnus-summary-write-to-file (&optional filename)
2498   "Write this article to a file.
2499 Optional argument FILENAME specifies file name.
2500 The directory to save in defaults to `gnus-article-save-directory'."
2501   (gnus-summary-save-in-file nil t))
2502
2503 (defun gnus-summary-save-body-in-file (&optional filename)
2504   "Append this article body to a file.
2505 Optional argument FILENAME specifies file name.
2506 The directory to save in defaults to `gnus-article-save-directory'."
2507   (setq filename (gnus-read-save-file-name
2508                   "Save %s body in file:" filename
2509                   gnus-file-save-name gnus-newsgroup-name
2510                   gnus-current-headers 'gnus-newsgroup-last-file))
2511   (gnus-eval-in-buffer-window gnus-save-article-buffer
2512     (save-excursion
2513       (save-restriction
2514         (widen)
2515         (when (article-goto-body)
2516           (narrow-to-region (point) (point-max)))
2517         (gnus-output-to-file filename))))
2518   filename)
2519
2520 (defun gnus-summary-save-in-pipe (&optional command)
2521   "Pipe this article to subprocess."
2522   (setq command
2523         (cond ((and (eq command 'default)
2524                     gnus-last-shell-command)
2525                gnus-last-shell-command)
2526               (command command)
2527               (t (read-string
2528                   (format
2529                    "Shell command on %s: "
2530                    (if (and gnus-number-of-articles-to-be-saved
2531                             (> gnus-number-of-articles-to-be-saved 1))
2532                        (format "these %d articles"
2533                                gnus-number-of-articles-to-be-saved)
2534                      "this article"))
2535                   gnus-last-shell-command))))
2536   (when (string-equal command "")
2537     (setq command gnus-last-shell-command))
2538   (gnus-eval-in-buffer-window gnus-article-buffer
2539     (save-restriction
2540       (widen)
2541       (shell-command-on-region (point-min) (point-max) command nil)))
2542   (setq gnus-last-shell-command command))
2543
2544 ;;; Article file names when saving.
2545
2546 (defun gnus-capitalize-newsgroup (newsgroup)
2547   "Capitalize NEWSGROUP name."
2548   (when (not (zerop (length newsgroup)))
2549     (concat (char-to-string (upcase (aref newsgroup 0)))
2550             (substring newsgroup 1))))
2551
2552 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
2553   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2554 If variable `gnus-use-long-file-name' is non-nil, it is ~/News/News.group/num.
2555 Otherwise, it is like ~/News/news/group/num."
2556   (let ((default
2557           (expand-file-name
2558            (concat (if (gnus-use-long-file-name 'not-save)
2559                        (gnus-capitalize-newsgroup newsgroup)
2560                      (gnus-newsgroup-directory-form newsgroup))
2561                    "/" (int-to-string (mail-header-number headers)))
2562            gnus-article-save-directory)))
2563     (if (and last-file
2564              (string-equal (file-name-directory default)
2565                            (file-name-directory last-file))
2566              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2567         default
2568       (or last-file default))))
2569
2570 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
2571   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2572 If variable `gnus-use-long-file-name' is non-nil, it is
2573 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
2574   (let ((default
2575           (expand-file-name
2576            (concat (if (gnus-use-long-file-name 'not-save)
2577                        newsgroup
2578                      (gnus-newsgroup-directory-form newsgroup))
2579                    "/" (int-to-string (mail-header-number headers)))
2580            gnus-article-save-directory)))
2581     (if (and last-file
2582              (string-equal (file-name-directory default)
2583                            (file-name-directory last-file))
2584              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
2585         default
2586       (or last-file default))))
2587
2588 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
2589   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2590 If variable `gnus-use-long-file-name' is non-nil, it is
2591 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
2592   (or last-file
2593       (expand-file-name
2594        (if (gnus-use-long-file-name 'not-save)
2595            (gnus-capitalize-newsgroup newsgroup)
2596          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2597        gnus-article-save-directory)))
2598
2599 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
2600   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
2601 If variable `gnus-use-long-file-name' is non-nil, it is
2602 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
2603   (or last-file
2604       (expand-file-name
2605        (if (gnus-use-long-file-name 'not-save)
2606            newsgroup
2607          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
2608        gnus-article-save-directory)))
2609
2610 (eval-and-compile
2611   (mapcar
2612    (lambda (func)
2613      (let (afunc gfunc)
2614        (if (consp func)
2615            (setq afunc (car func)
2616                  gfunc (cdr func))
2617          (setq afunc func
2618                gfunc (intern (format "gnus-%s" func))))
2619        (fset gfunc
2620              (if (not (fboundp afunc))
2621                  nil
2622                `(lambda (&optional interactive &rest args)
2623                   ,(documentation afunc t)
2624                   (interactive (list t))
2625                   (save-excursion
2626                     (set-buffer gnus-article-buffer)
2627                     (if interactive
2628                         (call-interactively ',afunc)
2629                       (apply ',afunc args))))))))
2630    '(article-hide-headers
2631      article-hide-boring-headers
2632      article-toggle-headers
2633      article-treat-overstrike
2634      article-fill-long-lines
2635      article-capitalize-sentences
2636      article-remove-cr
2637      article-display-x-face
2638      article-de-quoted-unreadable
2639      article-decode-HZ
2640      article-hide-list-identifiers
2641      article-hide-pgp
2642      article-strip-banner
2643      article-babel
2644      article-hide-pem
2645      article-hide-signature
2646      article-strip-headers-in-body
2647      article-remove-trailing-blank-lines
2648      article-strip-leading-blank-lines
2649      article-strip-multiple-blank-lines
2650      article-strip-leading-space
2651      article-strip-trailing-space
2652      article-strip-blank-lines
2653      article-strip-all-blank-lines
2654      article-date-local
2655      article-date-iso8601
2656      article-date-original
2657      article-date-ut
2658      article-decode-mime-words
2659      article-decode-charset
2660      article-decode-encoded-words
2661      article-date-user
2662      article-date-lapsed
2663      article-emphasize
2664      article-treat-dumbquotes
2665      article-normalize-headers
2666      (article-show-all-headers . gnus-article-show-all-headers)
2667      (article-show-all . gnus-article-show-all))))
2668 \f
2669 ;;;
2670 ;;; Gnus article mode
2671 ;;;
2672
2673 (put 'gnus-article-mode 'mode-class 'special)
2674
2675 (gnus-define-keys gnus-article-mode-map
2676   " " gnus-article-goto-next-page
2677   "\177" gnus-article-goto-prev-page
2678   [delete] gnus-article-goto-prev-page
2679   [backspace] gnus-article-goto-prev-page
2680   "\C-c^" gnus-article-refer-article
2681   "h" gnus-article-show-summary
2682   "s" gnus-article-show-summary
2683   "\C-c\C-m" gnus-article-mail
2684   "?" gnus-article-describe-briefly
2685   "e" gnus-summary-edit-article
2686   "<" beginning-of-buffer
2687   ">" end-of-buffer
2688   "\C-c\C-i" gnus-info-find-node
2689   "\C-c\C-b" gnus-bug
2690
2691   "\C-d" gnus-article-read-summary-keys
2692   "\M-*" gnus-article-read-summary-keys
2693   "\M-#" gnus-article-read-summary-keys
2694   "\M-^" gnus-article-read-summary-keys
2695   "\M-g" gnus-article-read-summary-keys)
2696
2697 ;; Define almost undefined keys to `gnus-article-read-summary-keys'.
2698 (mapcar
2699  (lambda (key)
2700    (unless (lookup-key gnus-article-mode-map key)
2701      (define-key gnus-article-mode-map key
2702        'gnus-article-read-summary-keys)))
2703  (delq nil
2704        (append
2705         (mapcar
2706          (lambda (elt)
2707            (let ((key (car elt)))
2708              (and (> (length key) 0)
2709                   (not (eq 'menu-bar (aref key 0)))
2710                   (symbolp (lookup-key gnus-summary-mode-map key))
2711                   key)))
2712          (accessible-keymaps gnus-summary-mode-map))
2713         (let ((c 127)
2714               keys)
2715           (while (>= c 32)
2716             (push (char-to-string c) keys)
2717             (decf c))
2718           keys))))
2719
2720 (defun gnus-article-make-menu-bar ()
2721   (gnus-turn-off-edit-menu 'article)
2722   (unless (boundp 'gnus-article-article-menu)
2723     (easy-menu-define
2724      gnus-article-article-menu gnus-article-mode-map ""
2725      '("Article"
2726        ["Scroll forwards" gnus-article-goto-next-page t]
2727        ["Scroll backwards" gnus-article-goto-prev-page t]
2728        ["Show summary" gnus-article-show-summary t]
2729        ["Fetch Message-ID at point" gnus-article-refer-article t]
2730        ["Mail to address at point" gnus-article-mail t]
2731        ["Send a bug report" gnus-bug t]))
2732
2733     (easy-menu-define
2734      gnus-article-treatment-menu gnus-article-mode-map ""
2735      '("Treatment"
2736        ["Hide headers" gnus-article-toggle-headers t]
2737        ["Hide signature" gnus-article-hide-signature t]
2738        ["Hide citation" gnus-article-hide-citation t]
2739        ["Treat overstrike" gnus-article-treat-overstrike t]
2740        ["Remove carriage return" gnus-article-remove-cr t]
2741        ["Decode HZ" gnus-article-decode-HZ t]))
2742
2743     ;; Note "Commands" menu is defined in gnus-sum.el for consistency
2744
2745     (when (boundp 'gnus-summary-post-menu)
2746       (define-key gnus-article-mode-map [menu-bar post]
2747         (cons "Post" gnus-summary-post-menu)))
2748
2749     (gnus-run-hooks 'gnus-article-menu-hook)))
2750
2751 (defun gnus-article-mode ()
2752   "Major mode for displaying an article.
2753
2754 All normal editing commands are switched off.
2755
2756 The following commands are available in addition to all summary mode
2757 commands:
2758 \\<gnus-article-mode-map>
2759 \\[gnus-article-next-page]\t Scroll the article one page forwards
2760 \\[gnus-article-prev-page]\t Scroll the article one page backwards
2761 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
2762 \\[gnus-article-show-summary]\t Display the summary buffer
2763 \\[gnus-article-mail]\t Send a reply to the address near point
2764 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
2765 \\[gnus-info-find-node]\t Go to the Gnus info node"
2766   (interactive)
2767   (when (gnus-visual-p 'article-menu 'menu)
2768     (gnus-article-make-menu-bar))
2769   (gnus-simplify-mode-line)
2770   (setq mode-name "Article")
2771   (setq major-mode 'gnus-article-mode)
2772   (make-local-variable 'minor-mode-alist)
2773   (unless (assq 'gnus-show-mime minor-mode-alist)
2774     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
2775   (use-local-map gnus-article-mode-map)
2776   (gnus-update-format-specifications nil 'article-mode)
2777   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
2778   (make-local-variable 'gnus-page-broken)
2779   (make-local-variable 'gnus-button-marker-list)
2780   (make-local-variable 'gnus-article-current-summary)
2781   (make-local-variable 'gnus-article-mime-handles)
2782   (make-local-variable 'gnus-article-decoded-p)
2783   (make-local-variable 'gnus-article-mime-handle-alist)
2784   (make-local-variable 'gnus-article-wash-types)
2785   (gnus-set-default-directory)
2786   (buffer-disable-undo)
2787   (setq buffer-read-only t)
2788   (set-syntax-table gnus-article-mode-syntax-table)
2789   (gnus-run-hooks 'gnus-article-mode-hook))
2790
2791 (defun gnus-article-setup-buffer ()
2792   "Initialize the article buffer."
2793   (let* ((name (if gnus-single-article-buffer "*Article*"
2794                  (concat "*Article " gnus-newsgroup-name "*")))
2795          (original
2796           (progn (string-match "\\*Article" name)
2797                  (concat " *Original Article"
2798                          (substring name (match-end 0))))))
2799     (setq gnus-article-buffer name)
2800     (setq gnus-original-article-buffer original)
2801     (setq gnus-article-mime-handle-alist nil)
2802     ;; This might be a variable local to the summary buffer.
2803     (unless gnus-single-article-buffer
2804       (save-excursion
2805         (set-buffer gnus-summary-buffer)
2806         (setq gnus-article-buffer name)
2807         (setq gnus-original-article-buffer original)
2808         (gnus-set-global-variables)))
2809     (gnus-article-setup-highlight-words)
2810     ;; Init original article buffer.
2811     (save-excursion
2812       (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
2813       (setq major-mode 'gnus-original-article-mode)
2814       (make-local-variable 'gnus-original-article))
2815     (if (get-buffer name)
2816         (save-excursion
2817           (set-buffer name)
2818           (buffer-disable-undo)
2819           (setq buffer-read-only t)
2820           (unless (eq major-mode 'gnus-article-mode)
2821             (gnus-article-mode))
2822           (current-buffer))
2823       (save-excursion
2824         (set-buffer (gnus-get-buffer-create name))
2825         (gnus-article-mode)
2826         (make-local-variable 'gnus-summary-buffer)
2827         (gnus-summary-set-local-parameters gnus-newsgroup-name)
2828         (current-buffer)))))
2829
2830 ;; Set article window start at LINE, where LINE is the number of lines
2831 ;; from the head of the article.
2832 (defun gnus-article-set-window-start (&optional line)
2833   (set-window-start
2834    (get-buffer-window gnus-article-buffer t)
2835    (save-excursion
2836      (set-buffer gnus-article-buffer)
2837      (goto-char (point-min))
2838      (if (not line)
2839          (point-min)
2840        (gnus-message 6 "Moved to bookmark")
2841        (search-forward "\n\n" nil t)
2842        (forward-line line)
2843        (point)))))
2844
2845 ;;; @@ article filters
2846 ;;;
2847
2848 (defun gnus-article-display-mime-message ()
2849   "Article display method for MIME message."
2850   ;; called from `gnus-original-article-buffer'.
2851   (let (charset all-headers)
2852     (with-current-buffer gnus-summary-buffer
2853       (setq charset default-mime-charset
2854             all-headers gnus-have-all-headers))
2855     (make-local-variable 'default-mime-charset)
2856     (setq default-mime-charset charset)
2857     (with-current-buffer (get-buffer-create gnus-article-buffer)
2858       (make-local-variable 'default-mime-charset)
2859       (setq default-mime-charset charset))
2860     (mime-display-message mime-message-structure
2861                           gnus-article-buffer nil gnus-article-mode-map)
2862     (when all-headers
2863       (gnus-article-hide-headers nil -1))
2864     )
2865   ;; `mime-display-message' changes current buffer to `gnus-article-buffer'.
2866   (make-local-variable 'mime-button-mother-dispatcher)
2867   (setq mime-button-mother-dispatcher
2868         (function gnus-article-push-button))
2869   (run-hooks 'gnus-mime-article-prepare-hook))
2870
2871 (defun gnus-article-display-traditional-message ()
2872   "Article display method for traditional message."
2873   (set-buffer gnus-article-buffer)
2874   (let (buffer-read-only)
2875     (erase-buffer)
2876     (insert-buffer-substring gnus-original-article-buffer)))
2877
2878 (defun gnus-article-make-full-mail-header (&optional number charset)
2879   "Create a new mail header structure in a raw article buffer."
2880   (unless (and number charset)
2881     (save-current-buffer
2882       (set-buffer gnus-summary-buffer)
2883       (unless number
2884         (setq number (or (cdr gnus-article-current) 0)))
2885       (unless charset
2886         (setq charset (or default-mime-charset 'x-ctext)))))
2887   (goto-char (point-min))
2888   (let ((header-end (if (search-forward "\n\n" nil t)
2889                         (1- (point))
2890                       (goto-char (point-max))))
2891         (chars (- (point-max) (point)))
2892         (lines (count-lines (point) (point-max)))
2893         (default-mime-charset charset)
2894         xref)
2895     (narrow-to-region (point-min) header-end)
2896     (setq xref (std11-fetch-field "xref"))
2897     (prog1
2898         (make-full-mail-header
2899          number
2900          (std11-fetch-field "subject")
2901          (std11-fetch-field "from")
2902          (std11-fetch-field "date")
2903          (std11-fetch-field "message-id")
2904          (std11-fetch-field "references")
2905          chars
2906          lines
2907          (when xref (concat "Xref: " xref)))
2908       (widen))))
2909
2910 (defun gnus-article-prepare (article &optional all-headers header)
2911   "Prepare ARTICLE in article mode buffer.
2912 ARTICLE should either be an article number or a Message-ID.
2913 If ARTICLE is an id, HEADER should be the article headers.
2914 If ALL-HEADERS is non-nil, no headers are hidden."
2915   (save-excursion
2916     ;; Make sure we start in a summary buffer.
2917     (unless (eq major-mode 'gnus-summary-mode)
2918       (set-buffer gnus-summary-buffer))
2919     (setq gnus-summary-buffer (current-buffer))
2920     (let* ((gnus-article (if header (mail-header-number header) article))
2921            (summary-buffer (current-buffer))
2922            (gnus-tmp-internal-hook gnus-article-internal-prepare-hook)
2923            (group gnus-newsgroup-name)
2924            result)
2925       (save-excursion
2926         (gnus-article-setup-buffer)
2927         (set-buffer gnus-original-article-buffer)
2928         ;; Deactivate active regions.
2929         (when (and (boundp 'transient-mark-mode)
2930                    transient-mark-mode)
2931           (setq mark-active nil))
2932         (if (not (setq result (let ((buffer-read-only nil))
2933                                 (gnus-request-article-this-buffer
2934                                  article group))))
2935             ;; There is no such article.
2936             (save-excursion
2937               (when (and (numberp article)
2938                          (not (memq article gnus-newsgroup-sparse)))
2939                 (setq gnus-article-current
2940                       (cons gnus-newsgroup-name article))
2941                 (set-buffer gnus-summary-buffer)
2942                 (setq gnus-current-article article)
2943                 (if (eq (gnus-article-mark article) gnus-undownloaded-mark)
2944                     (progn
2945                       (gnus-summary-set-agent-mark article)
2946                       (message "Message marked for downloading"))
2947                   (gnus-summary-mark-article article gnus-canceled-mark)
2948                   (unless (memq article gnus-newsgroup-sparse)
2949                     (gnus-error 1 "No such article (may have expired or been canceled)")))))
2950           (if (or (eq result 'pseudo)
2951                   (eq result 'nneething))
2952               (progn
2953                 (save-excursion
2954                   (set-buffer summary-buffer)
2955                   (push article gnus-newsgroup-history)
2956                   (setq gnus-last-article gnus-current-article
2957                         gnus-current-article 0
2958                         gnus-current-headers nil
2959                         gnus-article-current nil)
2960                   (if (eq result 'nneething)
2961                       (gnus-configure-windows 'summary)
2962                     (gnus-configure-windows 'article))
2963                   (gnus-set-global-variables))
2964                 (let ((gnus-article-mime-handle-alist-1
2965                        gnus-article-mime-handle-alist))
2966                   (gnus-set-mode-line 'article)))
2967             ;; The result from the `request' was an actual article -
2968             ;; or at least some text that is now displayed in the
2969             ;; article buffer.
2970             (when (and (numberp article)
2971                        (not (eq article gnus-current-article)))
2972               ;; Seems like a new article has been selected.
2973               ;; `gnus-current-article' must be an article number.
2974               (save-excursion
2975                 (set-buffer summary-buffer)
2976                 (push article gnus-newsgroup-history)
2977                 (setq gnus-last-article gnus-current-article
2978                       gnus-current-article article
2979                       gnus-current-headers
2980                       (gnus-summary-article-header gnus-current-article)
2981                       gnus-article-current
2982                       (cons gnus-newsgroup-name gnus-current-article))
2983                 (unless (vectorp gnus-current-headers)
2984                   (setq gnus-current-headers nil))
2985                 (gnus-summary-goto-subject gnus-current-article)
2986                 (when (gnus-summary-show-thread)
2987                   ;; If the summary buffer really was folded, the
2988                   ;; previous goto may not actually have gone to
2989                   ;; the right article, but the thread root instead.
2990                   ;; So we go again.
2991                   (gnus-summary-goto-subject gnus-current-article))
2992                 (gnus-run-hooks 'gnus-mark-article-hook)
2993                 (gnus-set-mode-line 'summary)
2994                 (when (gnus-visual-p 'article-highlight 'highlight)
2995                   (gnus-run-hooks 'gnus-visual-mark-article-hook))
2996                 ;; Set the global newsgroup variables here.
2997                 (gnus-set-global-variables)
2998                 (setq gnus-have-all-headers
2999                       (or all-headers gnus-show-all-headers))))
3000             (when (or (numberp article)
3001                       (stringp article))
3002               (gnus-article-prepare-display)
3003               ;; Do page break.
3004               (goto-char (point-min))
3005               (setq gnus-page-broken
3006                     (when gnus-break-pages
3007                       (gnus-narrow-to-page)
3008                       t)))
3009             (let ((gnus-article-mime-handle-alist-1
3010                    gnus-article-mime-handle-alist))
3011               (gnus-set-mode-line 'article))
3012             (article-goto-body)
3013             (set-window-point (get-buffer-window (current-buffer)) (point))
3014             (gnus-configure-windows 'article)
3015             t))))))
3016
3017 (defun gnus-article-prepare-mime-display (&optional number)
3018   (goto-char (point-min))
3019   (when (re-search-forward "^[^\t ]+:" nil t)
3020     (goto-char (match-beginning 0)))
3021   (let ((entity (if (eq 1 (point-min))
3022                     (get-text-property 1 'mime-view-entity)
3023                   (get-text-property (point) 'mime-view-entity)))
3024         last-entity child-entity next type)
3025     (setq child-entity (mime-entity-children entity))
3026     (if child-entity
3027         (setq last-entity (nth (1- (length child-entity))
3028                                child-entity))
3029       (setq last-entity entity))
3030     (save-restriction
3031       (narrow-to-region (point)
3032                         (if (search-forward "\n\n" nil t)
3033                             (point)
3034                           (point-max)))
3035       (gnus-treat-article 'head)
3036       (put-text-property (point-min) (point-max) 'article-treated-header t)
3037       (goto-char (point-max)))
3038     (while (and (not (eobp)) entity)
3039       (setq next (set-marker
3040                   (make-marker)
3041                   (next-single-property-change (point) 'mime-view-entity
3042                                                nil (point-max))))
3043       (let ((types (mime-entity-content-type entity)))
3044         (while (eq 'multipart (mime-content-type-primary-type types))
3045           (setq entity (car (mime-entity-children entity))
3046                 types (mime-entity-content-type entity)))
3047         (when types
3048           (setq type (format "%s/%s"
3049                              (mime-content-type-primary-type types)
3050                              (mime-content-type-subtype types)))))
3051       (if (string-equal type "message/rfc822")
3052           (progn
3053             (setq next (point))
3054             (let ((children (mime-entity-children entity))
3055                   last-children)
3056               (when children
3057                 (setq last-children (nth (1- (length children)) children))
3058                 (while
3059                     (and
3060                      (not (eq last-children
3061                               (get-text-property next 'mime-view-entity)))
3062                      (setq next
3063                            (next-single-property-change next
3064                                                         'mime-view-entity
3065                                                         nil (point-max)))))))
3066             (setq next (next-single-property-change next 'mime-view-entity
3067                                                     nil (point-max)))
3068             (save-restriction
3069               (narrow-to-region (point) next)
3070               (gnus-article-prepare-mime-display)
3071               (goto-char (point-max)))
3072             (setq entity (get-text-property (point) 'mime-view-entity)))
3073         (save-restriction
3074           (narrow-to-region (point) next)
3075           ;; Kludge. We have to count true number, but for now,
3076           ;; part number is here only to achieve `last'.
3077           (gnus-treat-article nil 1
3078                               (if (eq entity last-entity)
3079                                   1 2)
3080                               type)
3081           (goto-char (point-max)))
3082         (setq entity (get-text-property next 'mime-view-entity))))))
3083
3084 ;;;###autoload
3085 (defun gnus-article-prepare-display ()
3086   "Make the current buffer look like a nice article."
3087   (setq gnus-article-wash-types nil)
3088   (gnus-run-hooks 'gnus-tmp-internal-hook)
3089   (gnus-run-hooks 'gnus-article-prepare-hook)
3090   ;; Display message.
3091   (let (mime-display-header-hook mime-display-text/plain-hook)
3092     (funcall (if gnus-show-mime
3093                  (progn
3094                    (setq mime-message-structure gnus-current-headers)
3095                    (mime-buffer-entity-set-buffer-internal
3096                     mime-message-structure
3097                     gnus-original-article-buffer)
3098                    (mime-entity-set-representation-type-internal
3099                     mime-message-structure 'mime-buffer-entity)
3100                    (luna-send mime-message-structure
3101                               'initialize-instance
3102                               mime-message-structure)
3103                    gnus-article-display-method-for-mime)
3104                gnus-article-display-method-for-traditional)))
3105   ;; Associate this article with the current summary buffer.
3106   (setq gnus-article-current-summary gnus-summary-buffer)
3107   ;; Call the treatment functions.
3108   (let ((inhibit-read-only t)
3109         buffer-read-only)
3110     (save-restriction
3111       (widen)
3112       (if gnus-show-mime
3113           (gnus-article-prepare-mime-display)
3114         (narrow-to-region (goto-char (point-min))
3115                           (if (search-forward "\n\n" nil t)
3116                               (point)
3117                             (point-max)))
3118         (gnus-treat-article 'head)
3119         (put-text-property (point-min) (point-max) 'article-treated-header t)
3120         (goto-char (point-max))
3121         (widen)
3122         (narrow-to-region (point) (point-max))
3123         (gnus-treat-article nil))
3124       (put-text-property (point-min) (point-max) 'read-only nil)))
3125   ;; Perform the article display hooks.  Incidentally, this hook is
3126   ;; an obsolete variable by now.
3127   (gnus-run-hooks 'gnus-article-display-hook))
3128
3129 (defun gnus-article-decode-article-as-default-mime-charset ()
3130   "Decode an article as `default-mime-charset'.  It won't work if the
3131 value of the variable `gnus-show-mime' is non-nil."
3132   (unless gnus-show-mime
3133     (decode-mime-charset-region (point-min) (point-max)
3134                                 (with-current-buffer gnus-summary-buffer
3135                                   default-mime-charset))))
3136
3137 ;;;
3138 ;;; Gnus MIME viewing functions
3139 ;;;
3140
3141 (defvar gnus-mime-button-line-format "%{%([%p. %d%T]%)%}%e\n"
3142   "The following specs can be used:
3143 %t  The MIME type
3144 %T  MIME type, along with additional info
3145 %n  The `name' parameter
3146 %d  The description, if any
3147 %l  The length of the encoded part
3148 %p  The part identifier number
3149 %e  Dots if the part isn't displayed")
3150
3151 (defvar gnus-mime-button-line-format-alist
3152   '((?t gnus-tmp-type ?s)
3153     (?T gnus-tmp-type-long ?s)
3154     (?n gnus-tmp-name ?s)
3155     (?d gnus-tmp-description ?s)
3156     (?p gnus-tmp-id ?s)
3157     (?l gnus-tmp-length ?d)
3158     (?e gnus-tmp-dots ?s)))
3159
3160 (defvar gnus-mime-button-commands
3161   '((gnus-article-press-button "\r" "Toggle Display")
3162     (gnus-mime-view-part "v" "View Interactively...")
3163     (gnus-mime-view-part-as-type "t" "View As Type...")
3164     (gnus-mime-save-part "o" "Save...")
3165     (gnus-mime-copy-part "c" "View As Text, In Other Buffer")
3166     (gnus-mime-inline-part "i" "View As Text, In This Buffer")
3167     (gnus-mime-internalize-part "E" "View Internally")
3168     (gnus-mime-externalize-part "e" "View Externally")
3169     (gnus-mime-pipe-part "|" "Pipe To Command...")))
3170
3171 (defun gnus-article-mime-part-status ()
3172   (with-current-buffer gnus-article-buffer
3173     (let ((entity (get-text-property (point-min) 'mime-view-entity)))
3174       (if (and entity (mime-entity-children entity))
3175           (format " (%d parts)" (length (mime-entity-children entity)))
3176         ""))))
3177
3178 (defvar gnus-mime-button-map nil)
3179 (unless gnus-mime-button-map
3180   (setq gnus-mime-button-map (make-sparse-keymap))
3181   (set-keymap-parent gnus-mime-button-map gnus-article-mode-map)
3182   (define-key gnus-mime-button-map gnus-mouse-2 'gnus-article-push-button)
3183   (define-key gnus-mime-button-map gnus-down-mouse-3 'gnus-mime-button-menu)
3184   (mapcar (lambda (c)
3185             (define-key gnus-mime-button-map (cadr c) (car c)))
3186           gnus-mime-button-commands))
3187
3188 (defun gnus-mime-button-menu (event)
3189   "Construct a context-sensitive menu of MIME commands."
3190   (interactive "e")
3191   (save-excursion
3192     (let ((pos (event-start event)))
3193       (set-buffer (window-buffer (posn-window pos)))
3194       (goto-char (posn-point pos))
3195       (gnus-article-check-buffer)
3196       (let ((response (x-popup-menu
3197                        t `("MIME Part"
3198                            ("" ,@(mapcar (lambda (c)
3199                                            (cons (caddr c) (car c)))
3200                                          gnus-mime-button-commands))))))
3201         (if response
3202             (funcall response))))))
3203
3204 (defun gnus-mime-view-all-parts (&optional handles)
3205   "View all the MIME parts."
3206   (interactive)
3207   (save-current-buffer
3208     (set-buffer gnus-article-buffer)
3209     (let ((handles (or handles gnus-article-mime-handles))
3210           (mail-parse-charset gnus-newsgroup-charset)
3211           (mail-parse-ignored-charsets 
3212            (save-excursion (set-buffer gnus-summary-buffer)
3213                            gnus-newsgroup-ignored-charsets)))
3214       (if (stringp (car handles))
3215           (gnus-mime-view-all-parts (cdr handles))
3216         (mapcar 'mm-display-part handles)))))
3217
3218 (defun gnus-mime-save-part ()
3219   "Save the MIME part under point."
3220   (interactive)
3221   (gnus-article-check-buffer)
3222   (let ((data (get-text-property (point) 'gnus-data)))
3223     (mm-save-part data)))
3224
3225 (defun gnus-mime-pipe-part ()
3226   "Pipe the MIME part under point to a process."
3227   (interactive)
3228   (gnus-article-check-buffer)
3229   (let ((data (get-text-property (point) 'gnus-data)))
3230     (mm-pipe-part data)))
3231
3232 (defun gnus-mime-view-part ()
3233   "Interactively choose a viewing method for the MIME part under point."
3234   (interactive)
3235   (gnus-article-check-buffer)
3236   (let ((data (get-text-property (point) 'gnus-data)))
3237     (mm-interactively-view-part data)))
3238
3239 (defun gnus-mime-view-part-as-type-internal ()
3240   (gnus-article-check-buffer)
3241   (let* ((name (mail-content-type-get
3242                 (mm-handle-type (get-text-property (point) 'gnus-data))
3243                 'name))
3244          (def-type (and name (mm-default-file-encoding name))))
3245     (and def-type (cons def-type 0))))
3246
3247 (defun gnus-mime-view-part-as-type (mime-type)
3248   "Choose a MIME media type, and view the part as such."
3249   (interactive
3250    (list (completing-read
3251           "View as MIME type: "
3252           (mapcar (lambda (i) (list i i)) (mailcap-mime-types))
3253           nil nil
3254           (gnus-mime-view-part-as-type-internal))))
3255   (gnus-article-check-buffer)
3256   (let ((handle (get-text-property (point) 'gnus-data)))
3257     (gnus-mm-display-part
3258      (mm-make-handle (mm-handle-buffer handle)
3259                      (cons mime-type (cdr (mm-handle-type handle)))
3260                      (mm-handle-encoding handle)
3261                      (mm-handle-undisplayer handle)
3262                      (mm-handle-disposition handle)
3263                      (mm-handle-description handle)
3264                      (mm-handle-cache handle)
3265                      (mm-handle-id handle)))))
3266
3267 (defun gnus-mime-copy-part (&optional handle)
3268   "Put the the MIME part under point into a new buffer."
3269   (interactive)
3270   (gnus-article-check-buffer)
3271   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3272          (contents (mm-get-part handle))|
3273          (base (file-name-nondirectory
3274                 (or
3275                  (mail-content-type-get (mm-handle-type handle) 'name)
3276                  (mail-content-type-get (mm-handle-type handle)
3277                                         'filename)
3278                  "*decoded*")))
3279          (buffer (generate-new-buffer base)))
3280     (switch-to-buffer buffer)
3281     (insert contents)
3282     ;; We do it this way to make `normal-mode' set the appropriate mode.
3283     (unwind-protect
3284         (progn
3285           (setq buffer-file-name (expand-file-name base))
3286           (normal-mode))
3287       (setq buffer-file-name nil))
3288     (goto-char (point-min))))
3289
3290 (defun gnus-mime-inline-part (&optional handle)
3291   "Insert the MIME part under point into the current buffer."
3292   (interactive)
3293   (gnus-article-check-buffer)
3294   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3295          contents
3296          (b (point))
3297          buffer-read-only)
3298     (if (mm-handle-undisplayer handle)
3299         (mm-remove-part handle)
3300       (setq contents (mm-get-part handle))
3301       (forward-line 2)
3302       (mm-insert-inline handle contents)
3303       (goto-char b))))
3304
3305 (defun gnus-mime-externalize-part (&optional handle)
3306   "View the MIME part under point with an external viewer."
3307   (interactive)
3308   (gnus-article-check-buffer)
3309   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3310          (mm-user-display-methods nil)
3311          (mm-inline-large-images nil)
3312          (mail-parse-charset gnus-newsgroup-charset)
3313          (mail-parse-ignored-charsets 
3314           (save-excursion (set-buffer gnus-summary-buffer)
3315                           gnus-newsgroup-ignored-charsets)))
3316     (if (mm-handle-undisplayer handle)
3317         (mm-remove-part handle)
3318       (mm-display-part handle))))
3319
3320 (defun gnus-mime-internalize-part (&optional handle)
3321   "View the MIME part under point with an internal viewer.
3322 In no internal viewer is available, use an external viewer."
3323   (interactive)
3324   (gnus-article-check-buffer)
3325   (let* ((handle (or handle (get-text-property (point) 'gnus-data)))
3326          (mm-inlined-types '(".*"))
3327          (mm-inline-large-images t)
3328          (mail-parse-charset gnus-newsgroup-charset)
3329          (mail-parse-ignored-charsets 
3330           (save-excursion (set-buffer gnus-summary-buffer)
3331                           gnus-newsgroup-ignored-charsets)))
3332     (if (mm-handle-undisplayer handle)
3333         (mm-remove-part handle)
3334       (mm-display-part handle))))
3335
3336 (defun gnus-article-part-wrapper (n function)
3337   (save-current-buffer
3338     (set-buffer gnus-article-buffer)
3339     (when (> n (length gnus-article-mime-handle-alist))
3340       (error "No such part"))
3341     (gnus-article-goto-part n)
3342     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3343       (funcall function handle))))
3344
3345 (defun gnus-article-pipe-part (n)
3346   "Pipe MIME part N, which is the numerical prefix."
3347   (interactive "p")
3348   (gnus-article-part-wrapper n 'mm-pipe-part))
3349
3350 (defun gnus-article-save-part (n)
3351   "Save MIME part N, which is the numerical prefix."
3352   (interactive "p")
3353   (gnus-article-part-wrapper n 'mm-save-part))
3354
3355 (defun gnus-article-interactively-view-part (n)
3356   "View MIME part N interactively, which is the numerical prefix."
3357   (interactive "p")
3358   (gnus-article-part-wrapper n 'mm-interactively-view-part))
3359
3360 (defun gnus-article-copy-part (n)
3361   "Copy MIME part N, which is the numerical prefix."
3362   (interactive "p")
3363   (gnus-article-part-wrapper n 'gnus-mime-copy-part))
3364
3365 (defun gnus-article-externalize-part (n)
3366   "View MIME part N externally, which is the numerical prefix."
3367   (interactive "p")
3368   (gnus-article-part-wrapper n 'gnus-mime-externalize-part))
3369
3370 (defun gnus-article-inline-part (n)
3371   "Inline MIME part N, which is the numerical prefix."
3372   (interactive "p")
3373   (gnus-article-part-wrapper n 'gnus-mime-inline-part))
3374
3375 (defun gnus-article-mime-match-handle-first (condition)
3376   (if condition
3377       (let ((alist gnus-article-mime-handle-alist) ihandle n)
3378         (while (setq ihandle (pop alist))
3379           (if (and (cond 
3380                     ((functionp condition)
3381                      (funcall condition (cdr ihandle)))
3382                     ((eq condition 'undisplayed) 
3383                      (not (or (mm-handle-undisplayer (cdr ihandle))
3384                               (equal (mm-handle-media-type (cdr ihandle))
3385                                      "multipart/alternative"))))
3386                     ((eq condition 'undisplayed-alternative)
3387                      (not (mm-handle-undisplayer (cdr ihandle))))
3388                     (t t))
3389                    (gnus-article-goto-part (car ihandle))
3390                    (or (not n) (< (car ihandle) n)))
3391               (setq n (car ihandle))))
3392         (or n 1))
3393     1))
3394
3395 (defun gnus-article-view-part (&optional n)
3396   "View MIME part N, which is the numerical prefix."
3397   (interactive "P")
3398   (save-current-buffer
3399     (set-buffer gnus-article-buffer)
3400     (or (numberp n) (setq n (gnus-article-mime-match-handle-first 
3401                              gnus-article-mime-match-handle-function)))
3402     (when (> n (length gnus-article-mime-handle-alist))
3403       (error "No such part"))
3404     (let ((handle (cdr (assq n gnus-article-mime-handle-alist))))
3405       (when (gnus-article-goto-part n)
3406         (if (equal (car handle) "multipart/alternative")
3407             (gnus-article-press-button)
3408           (when (eq (gnus-mm-display-part handle) 'internal)
3409             (gnus-set-window-start)))))))
3410
3411 (defun gnus-mm-display-part (handle)
3412   "Display HANDLE and fix MIME button."
3413   (let ((id (get-text-property (point) 'gnus-part))
3414         (point (point))
3415         buffer-read-only)
3416     (forward-line 1)
3417     (prog1
3418         (let ((window (selected-window))
3419               (mail-parse-charset gnus-newsgroup-charset)
3420               (mail-parse-ignored-charsets 
3421                (save-excursion (set-buffer gnus-summary-buffer)
3422                                gnus-newsgroup-ignored-charsets)))
3423           (save-excursion
3424             (unwind-protect
3425                 (let ((win (get-buffer-window (current-buffer) t))
3426                       (beg (point)))
3427                   (when win
3428                     (select-window win))
3429                   (goto-char point)
3430                   (forward-line)
3431                   (if (mm-handle-displayed-p handle)
3432                       ;; This will remove the part.
3433                       (mm-display-part handle)
3434                     (save-restriction
3435                       (narrow-to-region (point) (1+ (point)))
3436                       (mm-display-part handle)
3437                       ;; We narrow to the part itself and
3438                       ;; then call the treatment functions.
3439                       (goto-char (point-min))
3440                       (forward-line 1)
3441                       (narrow-to-region (point) (point-max))
3442                       (gnus-treat-article
3443                        nil id
3444                        (1- (length gnus-article-mime-handles))
3445                        (mm-handle-media-type handle)))))
3446               (select-window window))))
3447       (goto-char point)
3448       (delete-region (gnus-point-at-bol) (progn (forward-line 1) (point)))
3449       (gnus-insert-mime-button
3450        handle id (list (mm-handle-displayed-p handle)))
3451       (goto-char point))))
3452
3453 (defun gnus-article-goto-part (n)
3454   "Go to MIME part N."
3455   (let ((point (text-property-any (point-min) (point-max) 'gnus-part n)))
3456     (when point
3457       (goto-char point))))
3458
3459 (defun gnus-insert-mime-button (handle gnus-tmp-id &optional displayed)
3460   (let ((gnus-tmp-name
3461          (or (mail-content-type-get (mm-handle-type handle)
3462                                     'name)
3463              (mail-content-type-get (mm-handle-disposition handle)
3464                                     'filename)
3465              ""))
3466         (gnus-tmp-type (mm-handle-media-type handle))
3467         (gnus-tmp-description
3468          (mail-decode-encoded-word-string (or (mm-handle-description handle)
3469                                               "")))
3470         (gnus-tmp-dots
3471          (if (if displayed (car displayed)
3472                (mm-handle-displayed-p handle))
3473              "" "..."))
3474         (gnus-tmp-length (with-current-buffer (mm-handle-buffer handle)
3475                            (buffer-size)))
3476         gnus-tmp-type-long b e)
3477     (when (string-match ".*/" gnus-tmp-name)
3478       (setq gnus-tmp-name (replace-match "" t t gnus-tmp-name)))
3479     (setq gnus-tmp-type-long (concat gnus-tmp-type
3480                                      (and (not (equal gnus-tmp-name ""))
3481                                           (concat "; " gnus-tmp-name))))
3482     (or (equal gnus-tmp-description "")
3483         (setq gnus-tmp-type-long (concat " --- " gnus-tmp-type-long)))
3484     (unless (bolp)
3485       (insert "\n"))
3486     (setq b (point))
3487     (gnus-eval-format
3488      gnus-mime-button-line-format gnus-mime-button-line-format-alist
3489      `(local-map ,gnus-mime-button-map
3490                  keymap ,gnus-mime-button-map
3491                  gnus-callback gnus-mm-display-part
3492                  gnus-part ,gnus-tmp-id
3493                  article-type annotation
3494                  gnus-data ,handle))
3495     (setq e (point))
3496     (widget-convert-button 'link b e
3497                            :mime-handle handle
3498                            :action 'gnus-widget-press-button
3499                            :button-keymap gnus-mime-button-map
3500                            :help-echo
3501                            (lambda (widget)
3502                              ;; Needed to properly clear the message
3503                              ;; due to a bug in wid-edit
3504                              (setq help-echo-owns-message t)
3505                              (format
3506                               "Click to %s the MIME part; %s for more options"
3507                               (if (mm-handle-displayed-p
3508                                    (widget-get widget :mime-handle))
3509                                   "hide" "show")
3510                               (if gnus-xemacs "button3" "mouse-3"))))))
3511
3512 (defun gnus-widget-press-button (elems el)
3513   (goto-char (widget-get elems :from))
3514   (gnus-article-press-button))
3515
3516 (defvar gnus-displaying-mime nil)
3517
3518 (defun gnus-display-mime (&optional ihandles)
3519   "Display the MIME parts."
3520   (save-excursion
3521     (save-selected-window
3522       (let ((window (get-buffer-window gnus-article-buffer))
3523             (point (point)))
3524         (when window
3525           (select-window window)
3526           ;; We have to do this since selecting the window
3527           ;; may change the point.  So we set the window point.
3528           (set-window-point window point)))
3529       (let* ((handles (or ihandles (mm-dissect-buffer) (mm-uu-dissect)))
3530              buffer-read-only handle name type b e display)
3531         (when (and (not ihandles)
3532                    (not gnus-displaying-mime))
3533           ;; Top-level call; we clean up.
3534           (when gnus-article-mime-handles
3535             (mm-destroy-parts gnus-article-mime-handles)
3536             (setq gnus-article-mime-handle-alist nil));; A trick.
3537           (setq gnus-article-mime-handles handles)
3538           ;; We allow users to glean info from the handles.
3539           (when gnus-article-mime-part-function
3540             (gnus-mime-part-function handles)))
3541         (if (and handles
3542                  (or (not (stringp (car handles)))
3543                      (cdr handles)))
3544             (progn
3545               (when (and (not ihandles)
3546                          (not gnus-displaying-mime))
3547                 ;; Clean up for mime parts.
3548                 (article-goto-body)
3549                 (delete-region (point) (point-max)))
3550               (let ((gnus-displaying-mime t))
3551                 (gnus-mime-display-part handles)))
3552           (save-restriction
3553             (article-goto-body)
3554             (narrow-to-region (point) (point-max))
3555             (gnus-treat-article nil 1 1)
3556             (widen)))
3557         (unless ihandles
3558           ;; Highlight the headers.
3559           (save-excursion
3560             (save-restriction
3561               (article-goto-body)
3562               (narrow-to-region (point-min) (point))
3563               (gnus-treat-article 'head))))))))
3564
3565 (defvar gnus-mime-display-multipart-as-mixed nil)
3566
3567 (defun gnus-mime-display-part (handle)
3568   (cond
3569    ;; Single part.
3570    ((not (stringp (car handle)))
3571     (gnus-mime-display-single handle))
3572    ;; User-defined multipart
3573    ((cdr (assoc (car handle) gnus-mime-multipart-functions))
3574     (funcall (cdr (assoc (car handle) gnus-mime-multipart-functions))
3575              handle))
3576    ;; multipart/alternative
3577    ((and (equal (car handle) "multipart/alternative")
3578          (not gnus-mime-display-multipart-as-mixed))
3579     (let ((id (1+ (length gnus-article-mime-handle-alist))))
3580       (push (cons id handle) gnus-article-mime-handle-alist)
3581       (gnus-mime-display-alternative (cdr handle) nil nil id)))
3582    ;; multipart/related
3583    ((and (equal (car handle) "multipart/related")
3584          (not gnus-mime-display-multipart-as-mixed))
3585     ;;;!!!We should find the start part, but we just default
3586     ;;;!!!to the first part.
3587     (gnus-mime-display-part (cadr handle)))
3588    ;; Other multiparts are handled like multipart/mixed.
3589    (t
3590     (gnus-mime-display-mixed (cdr handle)))))
3591
3592 (defun gnus-mime-part-function (handles)
3593   (if (stringp (car handles))
3594       (mapcar 'gnus-mime-part-function (cdr handles))
3595     (funcall gnus-article-mime-part-function handles)))
3596
3597 (defun gnus-mime-display-mixed (handles)
3598   (mapcar 'gnus-mime-display-part handles))
3599
3600 (defun gnus-mime-display-single (handle)
3601   (let ((type (mm-handle-media-type handle))
3602         (ignored gnus-ignored-mime-types)
3603         (not-attachment t)
3604         (move nil)
3605         display text)
3606     (catch 'ignored
3607       (progn
3608         (while ignored
3609           (when (string-match (pop ignored) type)
3610             (throw 'ignored nil)))
3611         (if (and (setq not-attachment
3612                        (and (not (mm-inline-override-p handle))
3613                             (or (not (mm-handle-disposition handle))
3614                                 (equal (car (mm-handle-disposition handle))
3615                                        "inline")
3616                                 (mm-attachment-override-p handle))))
3617                  (mm-automatic-display-p handle)
3618                  (or (mm-inlined-p handle)
3619                      (mm-automatic-external-display-p type)))
3620             (setq display t)
3621           (when (equal (mm-handle-media-supertype handle) "text")
3622             (setq text t)))
3623         (let ((id (1+ (length gnus-article-mime-handle-alist))))
3624           (push (cons id handle) gnus-article-mime-handle-alist)
3625           (when (or (not display)
3626                     (not (gnus-unbuttonized-mime-type-p type)))
3627             ;(gnus-article-insert-newline)
3628             (gnus-insert-mime-button
3629              handle id (list (or display (and not-attachment text))))
3630             (gnus-article-insert-newline)
3631             ;(gnus-article-insert-newline)
3632             (setq move t)))
3633         (let ((beg (point)))
3634           (cond
3635            (display
3636             (when move
3637               (forward-line -2)
3638               (setq beg (point)))
3639             (let ((mail-parse-charset gnus-newsgroup-charset)
3640                   (mail-parse-ignored-charsets 
3641                    (save-excursion (condition-case ()
3642                                        (set-buffer gnus-summary-buffer)
3643                                      (error))
3644                                    gnus-newsgroup-ignored-charsets)))
3645               (mm-display-part handle t))
3646             (goto-char (point-max)))
3647            ((and text not-attachment)
3648             (when move
3649               (forward-line -2)
3650               (setq beg (point)))
3651             (gnus-article-insert-newline)
3652             (mm-insert-inline handle (mm-get-part handle))
3653             (goto-char (point-max))))
3654           ;; Do highlighting.
3655           (save-excursion
3656             (save-restriction
3657               (narrow-to-region beg (point))
3658               (gnus-treat-article
3659                nil (length gnus-article-mime-handle-alist)
3660                (1- (length gnus-article-mime-handles))
3661                (mm-handle-media-type handle)))))))))
3662
3663 (defun gnus-unbuttonized-mime-type-p (type)
3664   "Say whether TYPE is to be unbuttonized."
3665   (unless gnus-inhibit-mime-unbuttonizing
3666     (catch 'found
3667       (let ((types gnus-unbuttonized-mime-types))
3668         (while types
3669           (when (string-match (pop types) type)
3670             (throw 'found t)))))))
3671
3672 (defun gnus-article-insert-newline ()
3673   "Insert a newline, but mark it as undeletable."
3674   (gnus-put-text-property
3675    (point) (progn (insert "\n") (point)) 'gnus-undeletable t))
3676
3677 (defun gnus-mime-display-alternative (handles &optional preferred ibegend id)
3678   (let* ((preferred (or preferred (mm-preferred-alternative handles)))
3679          (ihandles handles)
3680          (point (point))
3681          handle buffer-read-only from props begend not-pref)
3682     (save-window-excursion
3683       (save-restriction
3684         (when ibegend
3685           (narrow-to-region (car ibegend)
3686                             (or (cdr ibegend)
3687                                 (progn
3688                                   (goto-char (car ibegend))
3689                                   (forward-line 2)
3690                                   (point))))
3691           (delete-region (point-min) (point-max))
3692           (mm-remove-parts handles))
3693         (setq begend (list (point-marker)))
3694         ;; Do the toggle.
3695         (unless (setq not-pref (cadr (member preferred ihandles)))
3696           (setq not-pref (car ihandles)))
3697         (when (or ibegend
3698                   (not (gnus-unbuttonized-mime-type-p
3699                         "multipart/alternative")))
3700           (gnus-add-text-properties
3701            (setq from (point))
3702            (progn
3703              (insert (format "%d.  " id))
3704              (point))
3705            `(gnus-callback
3706              (lambda (handles)
3707                (unless ,(not ibegend)
3708                  (setq gnus-article-mime-handle-alist
3709                        ',gnus-article-mime-handle-alist))
3710                (gnus-mime-display-alternative
3711                 ',ihandles ',not-pref ',begend ,id))
3712              local-map ,gnus-mime-button-map
3713              ,gnus-mouse-face-prop ,gnus-article-mouse-face
3714              face ,gnus-article-button-face
3715              keymap ,gnus-mime-button-map
3716              gnus-part ,id
3717              gnus-data ,handle))
3718           (widget-convert-button 'link from (point)
3719                                  :action 'gnus-widget-press-button
3720                                  :button-keymap gnus-widget-button-keymap)
3721           ;; Do the handles
3722           (while (setq handle (pop handles))
3723             (gnus-add-text-properties
3724              (setq from (point))
3725              (progn
3726                (insert (format "(%c) %-18s"
3727                                (if (equal handle preferred) ?* ? )
3728                                (mm-handle-media-type handle)))
3729                (point))
3730              `(gnus-callback
3731                (lambda (handles)
3732                  (unless ,(not ibegend)
3733                    (setq gnus-article-mime-handle-alist
3734                          ',gnus-article-mime-handle-alist))
3735                  (gnus-mime-display-alternative
3736                   ',ihandles ',handle ',begend ,id))
3737                local-map ,gnus-mime-button-map
3738                ,gnus-mouse-face-prop ,gnus-article-mouse-face
3739                face ,gnus-article-button-face
3740                keymap ,gnus-mime-button-map
3741                gnus-part ,id
3742                gnus-data ,handle))
3743             (widget-convert-button 'link from (point)
3744                                    :action 'gnus-widget-press-button
3745                                    :button-keymap gnus-widget-button-keymap)
3746             (insert "  "))
3747           (insert "\n\n"))
3748         (when preferred
3749           (if (stringp (car preferred))
3750               (gnus-display-mime preferred)
3751             (let ((mail-parse-charset gnus-newsgroup-charset)
3752                   (mail-parse-ignored-charsets 
3753                    (save-excursion (set-buffer gnus-summary-buffer)
3754                                    gnus-newsgroup-ignored-charsets)))
3755               (mm-display-part preferred)
3756               ;; Do highlighting.
3757               (save-excursion
3758                 (save-restriction
3759                   (narrow-to-region (car begend) (point-max))
3760                   (gnus-treat-article
3761                    nil (length gnus-article-mime-handle-alist)
3762                    (1- (length gnus-article-mime-handles))
3763                    (mm-handle-media-type handle))))))
3764           (goto-char (point-max))
3765           (setcdr begend (point-marker)))))
3766     (when ibegend
3767       (goto-char point))))
3768
3769 (defun gnus-article-wash-status ()
3770   "Return a string which display status of article washing."
3771   (save-excursion
3772     (set-buffer gnus-article-buffer)
3773     (let ((cite (memq 'cite gnus-article-wash-types))
3774           (headers (memq 'headers gnus-article-wash-types))
3775           (boring (memq 'boring-headers gnus-article-wash-types))
3776           (pgp (memq 'pgp gnus-article-wash-types))
3777           (pem (memq 'pem gnus-article-wash-types))
3778           (signature (memq 'signature gnus-article-wash-types))
3779           (overstrike (memq 'overstrike gnus-article-wash-types))
3780           (emphasis (memq 'emphasis gnus-article-wash-types)))
3781       (format "%c%c%c%c%c%c%c"
3782               (if cite ?c ? )
3783               (if (or headers boring) ?h ? )
3784               (if (or pgp pem) ?p ? )
3785               (if signature ?s ? )
3786               (if overstrike ?o ? )
3787               (if gnus-show-mime ?m ? )
3788               (if emphasis ?e ? )))))
3789
3790 (fset 'gnus-article-hide-headers-if-wanted 'gnus-article-maybe-hide-headers)
3791
3792 (defun gnus-article-maybe-hide-headers ()
3793   "Hide unwanted headers if `gnus-have-all-headers' is nil.
3794 Provided for backwards compatibility."
3795   (when (and (or (not (gnus-buffer-live-p gnus-summary-buffer))
3796                  (not (save-excursion (set-buffer gnus-summary-buffer)
3797                                       gnus-have-all-headers)))
3798              (not gnus-inhibit-hiding))
3799     (gnus-article-hide-headers)))
3800
3801 ;;; Article savers.
3802
3803 (defun gnus-output-to-file (file-name)
3804   "Append the current article to a file named FILE-NAME."
3805   (let ((artbuf (current-buffer)))
3806     (with-temp-buffer
3807       (insert-buffer-substring artbuf)
3808       ;; Append newline at end of the buffer as separator, and then
3809       ;; save it to file.
3810       (goto-char (point-max))
3811       (insert "\n")
3812       (write-region-as-binary (point-min) (point-max) file-name 'append)
3813       t)))
3814
3815 (defun gnus-narrow-to-page (&optional arg)
3816   "Narrow the article buffer to a page.
3817 If given a numerical ARG, move forward ARG pages."
3818   (interactive "P")
3819   (setq arg (if arg (prefix-numeric-value arg) 0))
3820   (save-excursion
3821     (set-buffer gnus-article-buffer)
3822     (goto-char (point-min))
3823     (widen)
3824     ;; Remove any old next/prev buttons.
3825     (when (gnus-visual-p 'page-marker)
3826       (let ((buffer-read-only nil))
3827         (gnus-remove-text-with-property 'gnus-prev)
3828         (gnus-remove-text-with-property 'gnus-next)))
3829     (when
3830         (cond ((< arg 0)
3831                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
3832               ((> arg 0)
3833                (re-search-forward page-delimiter nil 'move arg)))
3834       (goto-char (match-end 0)))
3835     (narrow-to-region
3836      (point)
3837      (if (re-search-forward page-delimiter nil 'move)
3838          (match-beginning 0)
3839        (point)))
3840     (when (and (gnus-visual-p 'page-marker)
3841                (not (= (point-min) 1)))
3842       (save-excursion
3843         (goto-char (point-min))
3844         (gnus-insert-prev-page-button)))
3845     (when (and (gnus-visual-p 'page-marker)
3846                (< (+ (point-max) 2) (buffer-size)))
3847       (save-excursion
3848         (goto-char (point-max))
3849         (gnus-insert-next-page-button)))))
3850
3851 ;; Article mode commands
3852
3853 (defun gnus-article-goto-next-page ()
3854   "Show the next page of the article."
3855   (interactive)
3856   (when (gnus-article-next-page)
3857     (goto-char (point-min))
3858     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
3859
3860 (defun gnus-article-goto-prev-page ()
3861   "Show the next page of the article."
3862   (interactive)
3863   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?p))
3864     (gnus-article-prev-page nil)))
3865
3866 (defun gnus-article-next-page (&optional lines)
3867   "Show the next page of the current article.
3868 If end of article, return non-nil.  Otherwise return nil.
3869 Argument LINES specifies lines to be scrolled up."
3870   (interactive "p")
3871   (let ((start (window-start))
3872         end-of-buffer end-of-page)
3873     (save-excursion
3874       (move-to-window-line -1)
3875       (if (<= (point) start)
3876           (progn
3877             (forward-line 2)
3878             (setq start (point)))
3879         (forward-line 1)
3880         (setq start nil))
3881       (unless (or (cond ((eq (1+ (buffer-size)) (point))
3882                          (and (pos-visible-in-window-p)
3883                               (setq end-of-buffer t)))
3884                         ((eobp)
3885                          (setq end-of-page t)))
3886                   (not lines))
3887         (move-to-window-line lines)
3888         (unless (search-backward "\n\n" nil t)
3889           (setq start (point)))))
3890     (cond (end-of-buffer t)
3891           (end-of-page
3892            (gnus-narrow-to-page 1)
3893            nil)
3894           (t
3895            (if start
3896                (set-window-start (selected-window) start)
3897              (let (window-pixel-scroll-increment)
3898                (scroll-up lines)))
3899            nil))))
3900
3901 (defun gnus-article-prev-page (&optional lines)
3902   "Show previous page of current article.
3903 Argument LINES specifies lines to be scrolled down."
3904   (interactive "p")
3905   (let (beginning-of-buffer beginning-of-page)
3906     (save-excursion
3907       (move-to-window-line 0)
3908       (cond ((eq 1 (point))
3909              (setq beginning-of-buffer t))
3910             ((bobp)
3911              (setq beginning-of-page t))))
3912     (cond (beginning-of-buffer)
3913           (beginning-of-page
3914            (gnus-narrow-to-page -1))
3915           (t
3916            (condition-case nil
3917                (let (window-pixel-scroll-increment)
3918                  (scroll-down lines))
3919              (beginning-of-buffer
3920               (goto-char (point-min))))))))
3921
3922 (defun gnus-article-refer-article ()
3923   "Read article specified by message-id around point."
3924   (interactive)
3925   (let ((point (point)))
3926     (search-forward ">" nil t)          ;Move point to end of "<....>".
3927     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
3928         (let ((message-id (match-string 1)))
3929           (goto-char point)
3930           (set-buffer gnus-summary-buffer)
3931           (gnus-summary-refer-article message-id))
3932       (goto-char (point))
3933       (error "No references around point"))))
3934
3935 (defun gnus-article-show-summary ()
3936   "Reconfigure windows to show summary buffer."
3937   (interactive)
3938   (if (not (gnus-buffer-live-p gnus-summary-buffer))
3939       (error "There is no summary buffer for this article buffer")
3940     (gnus-article-set-globals)
3941     (gnus-configure-windows 'article)
3942     (gnus-summary-goto-subject gnus-current-article)
3943     (gnus-summary-position-point)))
3944
3945 (defun gnus-article-describe-briefly ()
3946   "Describe article mode commands briefly."
3947   (interactive)
3948   (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")))
3949
3950 (defun gnus-article-summary-command ()
3951   "Execute the last keystroke in the summary buffer."
3952   (interactive)
3953   (let ((obuf (current-buffer))
3954         (owin (current-window-configuration))
3955         func)
3956     (switch-to-buffer gnus-article-current-summary 'norecord)
3957     (setq func (lookup-key (current-local-map) (this-command-keys)))
3958     (call-interactively func)
3959     (set-buffer obuf)
3960     (set-window-configuration owin)
3961     (set-window-point (get-buffer-window (current-buffer)) (point))))
3962
3963 (defun gnus-article-summary-command-nosave ()
3964   "Execute the last keystroke in the summary buffer."
3965   (interactive)
3966   (let (func)
3967     (pop-to-buffer gnus-article-current-summary 'norecord)
3968     (setq func (lookup-key (current-local-map) (this-command-keys)))
3969     (call-interactively func)))
3970
3971 (defun gnus-article-check-buffer ()
3972   "Beep if not in an article buffer."
3973   (unless (eq (get-buffer gnus-article-buffer) (current-buffer))
3974     (error "Command invoked outside of a Gnus article buffer")))
3975
3976 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
3977   "Read a summary buffer key sequence and execute it from the article buffer."
3978   (interactive "P")
3979   (gnus-article-check-buffer)
3980   (let ((nosaves
3981          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
3982            "Zc" "ZC" "ZE" "ZJ" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
3983            "=" "^" "\M-^" "|"))
3984         (nosave-but-article
3985          '("A\r"))
3986         (nosave-in-article
3987          '("\C-d"))
3988         (up-to-top
3989          '("n" "Gn" "p" "Gp"))
3990         keys new-sum-point)
3991     (save-excursion
3992       (set-buffer gnus-article-current-summary)
3993       (let (gnus-pick-mode)
3994         (push (or key last-command-event) unread-command-events)
3995         (setq keys (static-if (featurep 'xemacs)
3996                        (events-to-keys (read-key-sequence nil))
3997                      (read-key-sequence nil)))))
3998     (message "")
3999
4000     (if (or (member keys nosaves)
4001             (member keys nosave-but-article)
4002             (member keys nosave-in-article))
4003         (let (func)
4004           (save-window-excursion
4005             (pop-to-buffer gnus-article-current-summary 'norecord)
4006             ;; We disable the pick minor mode commands.
4007             (let (gnus-pick-mode)
4008               (setq func (lookup-key (current-local-map) keys))))
4009           (if (or (not func)
4010                   (numberp func))
4011               (ding)
4012             (unless (member keys nosave-in-article)
4013               (set-buffer gnus-article-current-summary))
4014             (call-interactively func)
4015             (setq new-sum-point (point)))
4016           (when (member keys nosave-but-article)
4017             (pop-to-buffer gnus-article-buffer 'norecord)))
4018       ;; These commands should restore window configuration.
4019       (let ((obuf (current-buffer))
4020             (owin (current-window-configuration))
4021             (opoint (point))
4022             (summary gnus-article-current-summary)
4023             func in-buffer selected)
4024         (if not-restore-window
4025             (pop-to-buffer summary 'norecord)
4026           (switch-to-buffer summary 'norecord))
4027         (setq in-buffer (current-buffer))
4028         ;; We disable the pick minor mode commands.
4029         (if (setq func (let (gnus-pick-mode)
4030                          (lookup-key (current-local-map) keys)))
4031             (progn
4032               (call-interactively func)
4033               (setq new-sum-point (point)))
4034           (ding))
4035         (when (eq in-buffer (current-buffer))
4036           (setq selected (gnus-summary-select-article))
4037           (set-buffer obuf)
4038           (unless not-restore-window
4039             (set-window-configuration owin))
4040           (when (eq selected 'old)
4041             (article-goto-body)
4042             (set-window-start (get-buffer-window (current-buffer))
4043                               1)
4044             (set-window-point (get-buffer-window (current-buffer))
4045                               (point)))
4046           (let ((win (get-buffer-window gnus-article-current-summary)))
4047             (when win
4048               (set-window-point win new-sum-point))))))))
4049
4050 (defun gnus-article-hide (&optional arg force)
4051   "Hide all the gruft in the current article.
4052 This means that PGP stuff, signatures, cited text and (some)
4053 headers will be hidden.
4054 If given a prefix, show the hidden text instead."
4055   (interactive (append (gnus-article-hidden-arg) (list 'force)))
4056   (gnus-article-hide-headers arg)
4057   (gnus-article-hide-list-identifiers arg)
4058   (gnus-article-hide-pgp arg)
4059   (gnus-article-hide-citation-maybe arg force)
4060   (gnus-article-hide-signature arg))
4061
4062 (defun gnus-article-maybe-highlight ()
4063   "Do some article highlighting if article highlighting is requested."
4064   (when (gnus-visual-p 'article-highlight 'highlight)
4065     (gnus-article-highlight-some)))
4066
4067 (defun gnus-check-group-server ()
4068   ;; Make sure the connection to the server is alive.
4069   (unless (gnus-server-opened
4070            (gnus-find-method-for-group gnus-newsgroup-name))
4071     (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
4072     (gnus-request-group gnus-newsgroup-name t)))
4073
4074 (defun gnus-request-article-this-buffer (article group)
4075   "Get an article and insert it into this buffer."
4076   (let (do-update-line sparse-header)
4077     (prog1
4078         (save-excursion
4079           (erase-buffer)
4080           (gnus-kill-all-overlays)
4081           (setq group (or group gnus-newsgroup-name))
4082
4083           ;; Using `gnus-request-article' directly will insert the article into
4084           ;; `nntp-server-buffer' - so we'll save some time by not having to
4085           ;; copy it from the server buffer into the article buffer.
4086
4087           ;; We only request an article by message-id when we do not have the
4088           ;; headers for it, so we'll have to get those.
4089           (when (stringp article)
4090             (gnus-read-header article))
4091
4092           ;; If the article number is negative, that means that this article
4093           ;; doesn't belong in this newsgroup (possibly), so we find its
4094           ;; message-id and request it by id instead of number.
4095           (when (and (numberp article)
4096                      gnus-summary-buffer
4097                      (get-buffer gnus-summary-buffer)
4098                      (gnus-buffer-exists-p gnus-summary-buffer))
4099             (save-excursion
4100               (set-buffer gnus-summary-buffer)
4101               (let ((header (gnus-summary-article-header article)))
4102                 (when (< article 0)
4103                   (cond
4104                    ((memq article gnus-newsgroup-sparse)
4105                     ;; This is a sparse gap article.
4106                     (setq do-update-line article)
4107                     (setq article (mail-header-id header))
4108                     (setq sparse-header (gnus-read-header article))
4109                     (setq gnus-newsgroup-sparse
4110                           (delq article gnus-newsgroup-sparse)))
4111                    ((vectorp header)
4112                     ;; It's a real article.
4113                     (setq article (mail-header-id header)))
4114                    (t
4115                     ;; It is an extracted pseudo-article.
4116                     (setq article 'pseudo)
4117                     (gnus-request-pseudo-article header))))
4118
4119                 (let ((method (gnus-find-method-for-group
4120                                gnus-newsgroup-name)))
4121                   (when (and (eq (car method) 'nneething)
4122                              (vectorp header))
4123                     (let ((dir (concat
4124                                 (file-name-as-directory
4125                                  (or (cadr (assq 'nneething-address method))
4126                                      (nth 1 method)))
4127                                 (mail-header-subject header))))
4128                       (when (file-directory-p dir)
4129                         (setq article 'nneething)
4130                         (gnus-group-enter-directory dir))))))))
4131
4132           (cond
4133            ;; Refuse to select canceled articles.
4134            ((and (numberp article)
4135                  gnus-summary-buffer
4136                  (get-buffer gnus-summary-buffer)
4137                  (gnus-buffer-exists-p gnus-summary-buffer)
4138                  (eq (cdr (save-excursion
4139                             (set-buffer gnus-summary-buffer)
4140                             (assq article gnus-newsgroup-reads)))
4141                      gnus-canceled-mark))
4142             nil)
4143            ;; Check the backlog.
4144            ((and gnus-keep-backlog
4145                  (gnus-backlog-request-article group article (current-buffer)))
4146             'article)
4147            ;; Check asynchronous pre-fetch.
4148            ((gnus-async-request-fetched-article group article (current-buffer))
4149             (gnus-async-prefetch-next group article gnus-summary-buffer)
4150             (when (and (numberp article) gnus-keep-backlog)
4151               (gnus-backlog-enter-article group article (current-buffer)))
4152             'article)
4153            ;; Check the cache.
4154            ((and gnus-use-cache
4155                  (numberp article)
4156                  (gnus-cache-request-article article group))
4157             'article)
4158            ;; Get the article and put into the article buffer.
4159            ((or (stringp article)
4160                 (numberp article))
4161             (let ((gnus-override-method gnus-override-method)
4162                   (methods (and (stringp article) 
4163                                 gnus-refer-article-method))
4164                   result
4165                   (buffer-read-only nil))
4166               (setq methods
4167                     (if (listp methods)
4168                         methods
4169                       (list methods)))
4170               (when (and (null gnus-override-method)
4171                          methods)
4172                 (setq gnus-override-method (pop methods)))
4173               (while (not result)
4174                 (when (eq gnus-override-method 'current)
4175                   (setq gnus-override-method gnus-current-select-method))
4176                 (erase-buffer)
4177                 (gnus-kill-all-overlays)
4178                 (let ((gnus-newsgroup-name group))
4179                   (gnus-check-group-server))
4180                 (when (gnus-request-article article group (current-buffer))
4181                   (when (numberp article)
4182                     (gnus-async-prefetch-next group article 
4183                                               gnus-summary-buffer)
4184                     (when gnus-keep-backlog
4185                       (gnus-backlog-enter-article
4186                        group article (current-buffer))))
4187                   (setq result 'article))
4188                 (if (not result)
4189                     (if methods
4190                         (setq gnus-override-method (pop methods))
4191                       (setq result 'done))))
4192               (and (eq result 'article) 'article)))
4193            ;; It was a pseudo.
4194            (t article)))
4195
4196       ;; Associate this article with the current summary buffer.
4197       (setq gnus-article-current-summary gnus-summary-buffer)
4198
4199       ;; Take the article from the original article buffer
4200       ;; and place it in the buffer it's supposed to be in.
4201       (when (and (get-buffer gnus-article-buffer)
4202                  (equal (buffer-name (current-buffer))
4203                         (buffer-name (get-buffer gnus-article-buffer))))
4204         (save-excursion
4205           (if (get-buffer gnus-original-article-buffer)
4206               (set-buffer gnus-original-article-buffer)
4207             (set-buffer (gnus-get-buffer-create gnus-original-article-buffer))
4208             (buffer-disable-undo)
4209             (setq major-mode 'gnus-original-article-mode)
4210             (setq buffer-read-only t))
4211           (let (buffer-read-only)
4212             (erase-buffer)
4213             (insert-buffer-substring gnus-article-buffer))
4214           (setq gnus-original-article (cons group article)))
4215
4216         ;; Decode charsets.
4217         (run-hooks 'gnus-article-decode-hook)
4218         ;; Mark article as decoded or not.
4219         (setq gnus-article-decoded-p gnus-article-decode-hook))
4220
4221       ;; Update sparse articles.
4222       (when (and do-update-line
4223                  (or (numberp article)
4224                      (stringp article)))
4225         (let ((buf (current-buffer)))
4226           (set-buffer gnus-summary-buffer)
4227           (gnus-summary-update-article do-update-line sparse-header)
4228           (gnus-summary-goto-subject do-update-line nil t)
4229           (set-window-point (get-buffer-window (current-buffer) t)
4230                             (point))
4231           (set-buffer buf))))))
4232
4233 ;;;
4234 ;;; Article editing
4235 ;;;
4236
4237 (defcustom gnus-article-edit-mode-hook nil
4238   "Hook run in article edit mode buffers."
4239   :group 'gnus-article-various
4240   :type 'hook)
4241
4242 (defcustom gnus-article-edit-article-setup-function
4243   'gnus-article-mime-edit-article-setup
4244   "Function called to setup an editing article buffer."
4245   :group 'gnus-article-various
4246   :type 'function)
4247
4248 (defvar gnus-article-edit-done-function nil)
4249
4250 (defvar gnus-article-edit-mode-map nil)
4251
4252 ;; Should we be using derived.el for this?
4253 (unless gnus-article-edit-mode-map
4254   (setq gnus-article-edit-mode-map (make-sparse-keymap))
4255   (set-keymap-parent gnus-article-edit-mode-map text-mode-map)
4256
4257   (gnus-define-keys gnus-article-edit-mode-map
4258     "\C-c\C-c" gnus-article-edit-done
4259     "\C-c\C-k" gnus-article-edit-exit)
4260
4261   (gnus-define-keys (gnus-article-edit-wash-map
4262                      "\C-c\C-w" gnus-article-edit-mode-map)
4263     "f" gnus-article-edit-full-stops))
4264
4265 (defun gnus-article-edit-mode ()
4266   "Major mode for editing articles.
4267 This is an extended text-mode.
4268
4269 \\{gnus-article-edit-mode-map}"
4270   (interactive)
4271   (setq major-mode 'gnus-article-edit-mode)
4272   (setq mode-name "Article Edit")
4273   (use-local-map gnus-article-edit-mode-map)
4274   (make-local-variable 'gnus-article-edit-done-function)
4275   (make-local-variable 'gnus-prev-winconf)
4276   (setq buffer-read-only nil)
4277   (buffer-enable-undo)
4278   (widen)
4279   (gnus-run-hooks 'text-mode-hook 'gnus-article-edit-mode-hook))
4280
4281 (defun gnus-article-edit (&optional force)
4282   "Edit the current article.
4283 This will have permanent effect only in mail groups.
4284 If FORCE is non-nil, allow editing of articles even in read-only
4285 groups."
4286   (interactive "P")
4287   (when (and (not force)
4288              (gnus-group-read-only-p))
4289     (error "The current newsgroup does not support article editing"))
4290   (gnus-article-date-original)
4291   (gnus-article-edit-article
4292    'ignore
4293    `(lambda (no-highlight)
4294       'ignore
4295       (gnus-summary-edit-article-done
4296        ,(or (mail-header-references gnus-current-headers) "")
4297        ,(gnus-group-read-only-p) ,gnus-summary-buffer no-highlight))))
4298
4299 (defun gnus-article-edit-article (start-func exit-func)
4300   "Start editing the contents of the current article buffer."
4301   (let ((winconf (current-window-configuration)))
4302     (set-buffer gnus-article-buffer)
4303     (gnus-article-edit-mode)
4304     (funcall start-func)
4305     (gnus-configure-windows 'edit-article)
4306     (setq gnus-article-edit-done-function exit-func)
4307     (setq gnus-prev-winconf winconf)
4308     (when gnus-article-edit-article-setup-function
4309       (funcall gnus-article-edit-article-setup-function))
4310     (gnus-message 6 "C-c C-c to end edits")))
4311
4312 (defun gnus-article-edit-done (&optional arg)
4313   "Update the article edits and exit."
4314   (interactive "P")
4315   (save-excursion
4316     (save-restriction
4317       (widen)
4318       (when (article-goto-body)
4319         (let ((lines (count-lines (point) (point-max)))
4320               (length (- (point-max) (point)))
4321               (case-fold-search t)
4322               (body (copy-marker (point))))
4323           (goto-char (point-min))
4324           (when (re-search-forward "^content-length:[ \t]\\([0-9]+\\)" body t)
4325             (delete-region (match-beginning 1) (match-end 1))
4326             (insert (number-to-string length)))
4327           (goto-char (point-min))
4328           (when (re-search-forward
4329                  "^x-content-length:[ \t]\\([0-9]+\\)" body t)
4330             (delete-region (match-beginning 1) (match-end 1))
4331             (insert (number-to-string length)))
4332           (goto-char (point-min))
4333           (when (re-search-forward "^lines:[ \t]\\([0-9]+\\)" body t)
4334             (delete-region (match-beginning 1) (match-end 1))
4335             (insert (number-to-string lines)))))))
4336   (let ((func gnus-article-edit-done-function)
4337         (buf (current-buffer))
4338         (start (window-start)))
4339     (remove-hook 'gnus-article-mode-hook
4340                  'gnus-article-mime-edit-article-unwind)
4341     (gnus-article-edit-exit)
4342     (save-excursion
4343       (set-buffer buf)
4344       (let ((buffer-read-only nil))
4345         (funcall func arg))
4346       ;; The cache and backlog have to be flushed somewhat.
4347       (when gnus-keep-backlog
4348         (gnus-backlog-remove-article
4349          (car gnus-article-current) (cdr gnus-article-current)))
4350       ;; Flush original article as well.
4351       (save-excursion
4352         (when (get-buffer gnus-original-article-buffer)
4353           (set-buffer gnus-original-article-buffer)
4354           (setq gnus-original-article nil)))
4355       (when gnus-use-cache
4356         (gnus-cache-update-article
4357          (car gnus-article-current) (cdr gnus-article-current))))
4358     (set-buffer buf)
4359     (set-window-start (get-buffer-window buf) start)
4360     (set-window-point (get-buffer-window buf) (point))))
4361
4362 (defun gnus-article-edit-exit ()
4363   "Exit the article editing without updating."
4364   (interactive)
4365   ;; We remove all text props from the article buffer.
4366   (let ((buf (format "%s" (buffer-string)))
4367         (curbuf (current-buffer))
4368         (p (point))
4369         (window-start (window-start)))
4370     (erase-buffer)
4371     (insert buf)
4372     (let ((winconf gnus-prev-winconf))
4373       (gnus-article-mode)
4374       (set-window-configuration winconf)
4375       ;; Tippy-toe some to make sure that point remains where it was.
4376       (save-current-buffer
4377         (set-buffer curbuf)
4378         (set-window-start (get-buffer-window (current-buffer)) window-start)
4379         (goto-char p)))))
4380
4381 (defun gnus-article-edit-full-stops ()
4382   "Interactively repair spacing at end of sentences."
4383   (interactive)
4384   (save-excursion
4385     (goto-char (point-min))
4386     (search-forward-regexp "^$" nil t)
4387     (let ((case-fold-search nil))
4388       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
4389
4390 ;;;
4391 ;;; Article editing with MIME-Edit
4392 ;;;
4393
4394 (defcustom gnus-article-mime-edit-article-setup-hook nil
4395   "Hook run after setting up a MIME editing article buffer."
4396   :group 'gnus-article-various
4397   :type 'hook)
4398
4399 (defun gnus-article-mime-edit-article-unwind ()
4400   "Unwind `gnus-article-buffer' if article editing was given up."
4401   (remove-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
4402   (when mime-edit-mode-flag
4403     (mime-edit-exit 'nomime 'no-error)
4404     (message ""))
4405   (when (featurep 'font-lock)
4406     (setq font-lock-defaults nil)
4407     (font-lock-mode 0)))
4408
4409 (defun gnus-article-mime-edit-article-setup ()
4410   "Convert current buffer to MIME-Edit buffer and turn on MIME-Edit mode
4411 after replacing with the original article."
4412   (setq gnus-show-mime t)
4413   (setq gnus-article-edit-done-function
4414         `(lambda (&rest args)
4415            (when mime-edit-mode-flag
4416              (mime-edit-exit)
4417              (message ""))
4418            (goto-char (point-min))
4419            (let (case-fold-search)
4420              (when (re-search-forward
4421                     (format "^%s$" (regexp-quote mail-header-separator))
4422                     nil t)
4423                (replace-match "")))
4424            (when (featurep 'font-lock)
4425              (setq font-lock-defaults nil)
4426              (font-lock-mode 0))
4427            (apply ,gnus-article-edit-done-function args)
4428            (set-buffer gnus-original-article-buffer)
4429            (erase-buffer)
4430            (insert-buffer gnus-article-buffer)
4431            (setq gnus-current-headers (gnus-article-make-full-mail-header))
4432            (gnus-article-prepare-display)))
4433   (substitute-key-definition
4434    'gnus-article-edit-exit 'gnus-article-mime-edit-exit
4435    gnus-article-edit-mode-map)
4436   (erase-buffer)
4437   (insert-buffer gnus-original-article-buffer)
4438   (mime-edit-again)
4439   (when (featurep 'font-lock)
4440     (set (make-local-variable 'font-lock-defaults)
4441          '(message-font-lock-keywords t))
4442     (font-lock-set-defaults)
4443     (turn-on-font-lock))
4444   (add-hook 'gnus-article-mode-hook 'gnus-article-mime-edit-article-unwind)
4445   (gnus-run-hooks 'gnus-article-mime-edit-article-setup-hook))
4446
4447 (defun gnus-article-mime-edit-exit ()
4448   "Exit the article MIME editing without updating."
4449   (interactive)
4450   (let ((winconf gnus-prev-winconf)
4451         buf)
4452     (when mime-edit-mode-flag
4453       (mime-edit-exit)
4454       (message ""))
4455     (goto-char (point-min))
4456     (let (case-fold-search)
4457       (when (re-search-forward
4458              (format "^%s$" (regexp-quote mail-header-separator)) nil t)
4459         (replace-match "")))
4460     (when (featurep 'font-lock)
4461       (setq font-lock-defaults nil)
4462       (font-lock-mode 0))
4463     ;; We remove all text props from the article buffer.
4464     (setq buf (format "%s" (buffer-string)))
4465     (set-buffer (get-buffer-create gnus-original-article-buffer))
4466     (erase-buffer)
4467     (insert buf)
4468     (setq gnus-current-headers (gnus-article-make-full-mail-header))
4469     (gnus-article-prepare-display)
4470     (set-window-configuration winconf)))
4471
4472 ;;;
4473 ;;; Article highlights
4474 ;;;
4475
4476 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
4477
4478 ;;; Internal Variables:
4479
4480 (defcustom gnus-button-url-regexp "\\b\\(\\(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\\)\\)\\|[-a-zA-Z0-9_]+\\.[-a-zA-Z0-9_]+\\(\\.[-a-zA-Z0-9_]+[-a-zA-Z0-9_/]+\\)+"
4481   "Regular expression that matches URLs."
4482   :group 'gnus-article-buttons
4483   :type 'regexp)
4484
4485 (defcustom gnus-button-alist
4486   `(("<\\(url:[>\n\t ]*?\\)?news:[>\n\t ]*\\([^>\n\t ]*@[^>\n\t ]*\\)>"
4487      0 t gnus-button-message-id 2)
4488     ("\\bnews:\\([^>\n\t ]*@[^>)!;:,\n\t ]*\\)" 0 t gnus-button-message-id 1)
4489     ("\\(\\b<\\(url:[>\n\t ]*\\)?news:[>\n\t ]*\\(//\\)?\\([^>\n\t ]*\\)>\\)"
4490      1 t
4491      gnus-button-fetch-group 4)
4492     ("\\bnews:\\(//\\)?\\([^'\">\n\t ]+\\)" 0 t gnus-button-fetch-group 2)
4493     ("\\bin\\( +article\\| +message\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2
4494      t gnus-button-message-id 3)
4495     ("\\(<URL: *\\)mailto: *\\([^> \n\t]+\\)>" 0 t gnus-url-mailto 2)
4496     ("mailto:\\([-a-zA-Z.@_+0-9%]+\\)" 0 t gnus-url-mailto 1)
4497     ("\\bmailto:\\([^ \n\t]+\\)" 0 t gnus-url-mailto 1)
4498     ;; This is how URLs _should_ be embedded in text...
4499     ("<URL: *\\([^<>]*\\)>" 0 t gnus-button-embedded-url 1)
4500     ;; Raw URLs.
4501     (,gnus-button-url-regexp 0 t browse-url 0))
4502   "*Alist of regexps matching buttons in article bodies.
4503
4504 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
4505 REGEXP: is the string matching text around the button,
4506 BUTTON: is the number of the regexp grouping actually matching the button,
4507 FORM: is a lisp expression which must eval to true for the button to
4508 be added,
4509 CALLBACK: is the function to call when the user push this button, and each
4510 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
4511
4512 CALLBACK can also be a variable, in that case the value of that
4513 variable it the real callback function."
4514   :group 'gnus-article-buttons
4515   :type '(repeat (list regexp
4516                        (integer :tag "Button")
4517                        (sexp :tag "Form")
4518                        (function :tag "Callback")
4519                        (repeat :tag "Par"
4520                                :inline t
4521                                (integer :tag "Regexp group")))))
4522
4523 (defcustom gnus-header-button-alist
4524   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
4525      0 t gnus-button-message-id 0)
4526     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
4527     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+"
4528      0 t gnus-button-mailto 0)
4529     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t browse-url 0)
4530     ("^Subject:" ,gnus-button-url-regexp 0 t browse-url 0)
4531     ("^[^:]+:" ,gnus-button-url-regexp 0 t browse-url 0)
4532     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
4533      gnus-button-message-id 3))
4534   "*Alist of headers and regexps to match buttons in article heads.
4535
4536 This alist is very similar to `gnus-button-alist', except that each
4537 alist has an additional HEADER element first in each entry:
4538
4539 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
4540
4541 HEADER is a regexp to match a header.  For a fuller explanation, see
4542 `gnus-button-alist'."
4543   :group 'gnus-article-buttons
4544   :group 'gnus-article-headers
4545   :type '(repeat (list (regexp :tag "Header")
4546                        regexp
4547                        (integer :tag "Button")
4548                        (sexp :tag "Form")
4549                        (function :tag "Callback")
4550                        (repeat :tag "Par"
4551                                :inline t
4552                                (integer :tag "Regexp group")))))
4553
4554 (defvar gnus-button-regexp nil)
4555 (defvar gnus-button-marker-list nil)
4556 ;; Regexp matching any of the regexps from `gnus-button-alist'.
4557
4558 (defvar gnus-button-last nil)
4559 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
4560
4561 ;;; Commands:
4562
4563 (defun gnus-article-push-button (event)
4564   "Check text under the mouse pointer for a callback function.
4565 If the text under the mouse pointer has a `gnus-callback' property,
4566 call it with the value of the `gnus-data' text property."
4567   (interactive "e")
4568   (set-buffer (window-buffer (posn-window (event-start event))))
4569   (let* ((pos (posn-point (event-start event)))
4570          (data (get-text-property pos 'gnus-data))
4571          (fun (get-text-property pos 'gnus-callback)))
4572     (goto-char pos)
4573     (when fun
4574       (funcall fun data))))
4575
4576 (defun gnus-article-press-button ()
4577   "Check text at point for a callback function.
4578 If the text at point has a `gnus-callback' property,
4579 call it with the value of the `gnus-data' text property."
4580   (interactive)
4581   (let* ((data (get-text-property (point) 'gnus-data))
4582          (fun (get-text-property (point) 'gnus-callback)))
4583     (when fun
4584       (funcall fun data))))
4585
4586 (defun gnus-article-prev-button (n)
4587   "Move point to N buttons backward.
4588 If N is negative, move forward instead."
4589   (interactive "p")
4590   (gnus-article-next-button (- n)))
4591
4592 (defun gnus-article-next-button (n)
4593   "Move point to N buttons forward.
4594 If N is negative, move backward instead."
4595   (interactive "p")
4596   (let ((function (if (< n 0) 'previous-single-property-change
4597                     'next-single-property-change))
4598         (inhibit-point-motion-hooks t)
4599         (backward (< n 0))
4600         (limit (if (< n 0) (point-min) (point-max))))
4601     (setq n (abs n))
4602     (while (and (not (= limit (point)))
4603                 (> n 0))
4604       ;; Skip past the current button.
4605       (when (get-text-property (point) 'gnus-callback)
4606         (goto-char (funcall function (point) 'gnus-callback nil limit)))
4607       ;; Go to the next (or previous) button.
4608       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
4609       ;; Put point at the start of the button.
4610       (when (and backward (not (get-text-property (point) 'gnus-callback)))
4611         (goto-char (funcall function (point) 'gnus-callback nil limit)))
4612       ;; Skip past intangible buttons.
4613       (when (get-text-property (point) 'intangible)
4614         (incf n))
4615       (decf n))
4616     (unless (zerop n)
4617       (gnus-message 5 "No more buttons"))
4618     n))
4619
4620 (defun gnus-article-highlight (&optional force)
4621   "Highlight current article.
4622 This function calls `gnus-article-highlight-headers',
4623 `gnus-article-highlight-citation',
4624 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4625 do the highlighting.  See the documentation for those functions."
4626   (interactive (list 'force))
4627   (gnus-article-highlight-headers)
4628   (gnus-article-highlight-citation force)
4629   (gnus-article-highlight-signature)
4630   (gnus-article-add-buttons force)
4631   (gnus-article-add-buttons-to-head))
4632
4633 (defun gnus-article-highlight-some (&optional force)
4634   "Highlight current article.
4635 This function calls `gnus-article-highlight-headers',
4636 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
4637 do the highlighting.  See the documentation for those functions."
4638   (interactive (list 'force))
4639   (gnus-article-highlight-headers)
4640   (gnus-article-highlight-signature)
4641   (gnus-article-add-buttons))
4642
4643 (defun gnus-article-highlight-headers ()
4644   "Highlight article headers as specified by `gnus-header-face-alist'."
4645   (interactive)
4646   (save-excursion
4647     (set-buffer gnus-article-buffer)
4648     (save-restriction
4649       (let ((alist gnus-header-face-alist)
4650             (buffer-read-only nil)
4651             (case-fold-search t)
4652             (inhibit-point-motion-hooks t)
4653             entry regexp header-face field-face from hpoints fpoints)
4654         (article-narrow-to-head)
4655         (while (setq entry (pop alist))
4656           (goto-char (point-min))
4657           (setq regexp (concat "^\\("
4658                                (if (string-equal "" (nth 0 entry))
4659                                    "[^\t ]"
4660                                  (nth 0 entry))
4661                                "\\)")
4662                 header-face (nth 1 entry)
4663                 field-face (nth 2 entry))
4664           (while (and (re-search-forward regexp nil t)
4665                       (not (eobp)))
4666             (beginning-of-line)
4667             (setq from (point))
4668             (unless (search-forward ":" nil t)
4669               (forward-char 1))
4670             (when (and header-face
4671                        (not (memq (point) hpoints)))
4672               (push (point) hpoints)
4673               (gnus-put-text-property from (point) 'face header-face))
4674             (when (and field-face
4675                        (not (memq (setq from (point)) fpoints)))
4676               (push from fpoints)
4677               (if (re-search-forward "^[^ \t]" nil t)
4678                   (forward-char -2)
4679                 (goto-char (point-max)))
4680               (gnus-put-text-property from (point) 'face field-face))))))))
4681
4682 (defun gnus-article-highlight-signature ()
4683   "Highlight the signature in an article.
4684 It does this by highlighting everything after
4685 `gnus-signature-separator' using `gnus-signature-face'."
4686   (interactive)
4687   (when gnus-signature-face
4688     (save-excursion
4689       (set-buffer gnus-article-buffer)
4690       (let ((buffer-read-only nil)
4691             (inhibit-point-motion-hooks t))
4692         (save-restriction
4693           (when (gnus-article-narrow-to-signature)
4694             (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
4695                               'face gnus-signature-face)))))))
4696
4697 (defun gnus-article-buttonize-signature ()
4698   "Add button to the signature."
4699   (interactive)
4700   (save-excursion
4701     (set-buffer gnus-article-buffer)
4702     (let ((buffer-read-only nil)
4703           (inhibit-point-motion-hooks t))
4704       (when (gnus-article-search-signature)
4705         (gnus-article-add-button (match-beginning 0) (match-end 0)
4706                                  'gnus-signature-toggle
4707                                  (set-marker (make-marker)
4708                                              (1+ (match-end 0))))))))
4709
4710 (defun gnus-button-in-region-p (b e prop)
4711   "Say whether PROP exists in the region."
4712   (text-property-not-all b e prop nil))
4713
4714 (defun gnus-article-add-buttons (&optional force)
4715   "Find external references in the article and make buttons of them.
4716 \"External references\" are things like Message-IDs and URLs, as
4717 specified by `gnus-button-alist'."
4718   (interactive (list 'force))
4719   (save-excursion
4720     (set-buffer gnus-article-buffer)
4721     (let ((buffer-read-only nil)
4722           (inhibit-point-motion-hooks t)
4723           (case-fold-search t)
4724           (alist gnus-button-alist)
4725           beg entry regexp)
4726       ;; Remove all old markers.
4727       (let (marker entry new-list)
4728         (while (setq marker (pop gnus-button-marker-list))
4729           (if (or (< marker (point-min)) (>= marker (point-max)))
4730               (push marker new-list)
4731             (goto-char marker)
4732             (when (setq entry (gnus-button-entry))
4733               (put-text-property (match-beginning (nth 1 entry))
4734                                  (match-end (nth 1 entry))
4735                                  'gnus-callback nil))
4736             (set-marker marker nil)))
4737         (setq gnus-button-marker-list new-list))
4738       ;; We skip the headers.
4739       (article-goto-body)
4740       (setq beg (point))
4741       (while (setq entry (pop alist))
4742         (setq regexp (car entry))
4743         (goto-char beg)
4744         (while (re-search-forward regexp nil t)
4745           (let* ((start (and entry (match-beginning (nth 1 entry))))
4746                  (end (and entry (match-end (nth 1 entry))))
4747                  (from (match-beginning 0)))
4748             (when (and (or (eq t (nth 2 entry))
4749                            (eval (nth 2 entry)))
4750                        (not (gnus-button-in-region-p
4751                              start end 'gnus-callback)))
4752               ;; That optional form returned non-nil, so we add the
4753               ;; button.
4754               (gnus-article-add-button
4755                start end 'gnus-button-push
4756                (car (push (set-marker (make-marker) from)
4757                           gnus-button-marker-list))))))))))
4758
4759 ;; Add buttons to the head of an article.
4760 (defun gnus-article-add-buttons-to-head ()
4761   "Add buttons to the head of the article."
4762   (interactive)
4763   (save-excursion
4764     (set-buffer gnus-article-buffer)
4765     (save-restriction
4766       (let ((buffer-read-only nil)
4767             (inhibit-point-motion-hooks t)
4768             (case-fold-search t)
4769             (alist gnus-header-button-alist)
4770             entry beg end)
4771         (article-narrow-to-head)
4772         (while alist
4773           ;; Each alist entry.
4774           (setq entry (car alist)
4775                 alist (cdr alist))
4776           (goto-char (point-min))
4777           (while (re-search-forward (car entry) nil t)
4778             ;; Each header matching the entry.
4779             (setq beg (match-beginning 0))
4780             (setq end (or (and (re-search-forward "^[^ \t]" nil t)
4781                                (match-beginning 0))
4782                           (point-max)))
4783             (goto-char beg)
4784             (while (re-search-forward (nth 1 entry) end t)
4785               ;; Each match within a header.
4786               (let* ((entry (cdr entry))
4787                      (start (match-beginning (nth 1 entry)))
4788                      (end (match-end (nth 1 entry)))
4789                      (form (nth 2 entry)))
4790                 (goto-char (match-end 0))
4791                 (when (eval form)
4792                   (gnus-article-add-button
4793                    start end (nth 3 entry)
4794                    (buffer-substring (match-beginning (nth 4 entry))
4795                                      (match-end (nth 4 entry)))))))
4796             (goto-char end)))))))
4797
4798 ;;; External functions:
4799
4800 (defun gnus-article-add-button (from to fun &optional data)
4801   "Create a button between FROM and TO with callback FUN and data DATA."
4802   (when gnus-article-button-face
4803     (gnus-overlay-put (gnus-make-overlay from to)
4804                       'face gnus-article-button-face))
4805   (gnus-add-text-properties
4806    from to
4807    (nconc (and gnus-article-mouse-face
4808                (list gnus-mouse-face-prop gnus-article-mouse-face))
4809           (list 'gnus-callback fun)
4810           (and data (list 'gnus-data data)))))
4811
4812 ;;; Internal functions:
4813
4814 (defun gnus-article-set-globals ()
4815   (save-excursion
4816     (set-buffer gnus-summary-buffer)
4817     (gnus-set-global-variables)))
4818
4819 (defun gnus-signature-toggle (end)
4820   (save-excursion
4821     (set-buffer gnus-article-buffer)
4822     (let ((buffer-read-only nil)
4823           (inhibit-point-motion-hooks t)
4824           (limit (next-single-property-change end 'mime-view-entity
4825                                               nil (point-max))))
4826       (if (get-text-property end 'invisible)
4827           (gnus-article-unhide-text end limit)
4828         (gnus-article-hide-text end limit gnus-hidden-properties)))))
4829
4830 (defun gnus-button-entry ()
4831   ;; Return the first entry in `gnus-button-alist' matching this place.
4832   (let ((alist gnus-button-alist)
4833         (entry nil))
4834     (while alist
4835       (setq entry (pop alist))
4836       (if (looking-at (car entry))
4837           (setq alist nil)
4838         (setq entry nil)))
4839     entry))
4840
4841 (defun gnus-button-push (marker)
4842   ;; Push button starting at MARKER.
4843   (save-excursion
4844     (goto-char marker)
4845     (let* ((entry (gnus-button-entry))
4846            (inhibit-point-motion-hooks t)
4847            (fun (nth 3 entry))
4848            (args (mapcar (lambda (group)
4849                            (let ((string (match-string group)))
4850                              (gnus-set-text-properties
4851                               0 (length string) nil string)
4852                              string))
4853                          (nthcdr 4 entry))))
4854       (cond
4855        ((fboundp fun)
4856         (apply fun args))
4857        ((and (boundp fun)
4858              (fboundp (symbol-value fun)))
4859         (apply (symbol-value fun) args))
4860        (t
4861         (gnus-message 1 "You must define `%S' to use this button"
4862                       (cons fun args)))))))
4863
4864 (defun gnus-button-message-id (message-id)
4865   "Fetch MESSAGE-ID."
4866   (save-excursion
4867     (set-buffer gnus-summary-buffer)
4868     (gnus-summary-refer-article message-id)))
4869
4870 (defun gnus-button-fetch-group (address)
4871   "Fetch GROUP specified by ADDRESS."
4872   (if (not (string-match "[:/]" address))
4873       ;; This is just a simple group url.
4874       (gnus-group-read-ephemeral-group address gnus-select-method)
4875     (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)/\\)?\\(.*\\)$"
4876                            address))
4877         (error "Can't parse %s" address)
4878       (gnus-group-read-ephemeral-group
4879        (match-string 4 address)
4880        `(nntp ,(match-string 1 address)
4881               (nntp-address ,(match-string 1 address))
4882               (nntp-port-number ,(if (match-end 3)
4883                                      (match-string 3 address)
4884                                    "nntp")))))))
4885
4886 (defun gnus-url-parse-query-string (query &optional downcase)
4887   (let (retval pairs cur key val)
4888     (setq pairs (split-string query "&"))
4889     (while pairs
4890       (setq cur (car pairs)
4891             pairs (cdr pairs))
4892       (if (not (string-match "=" cur))
4893           nil                           ; Grace
4894         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
4895               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
4896         (if downcase
4897             (setq key (downcase key)))
4898         (setq cur (assoc key retval))
4899         (if cur
4900             (setcdr cur (cons val (cdr cur)))
4901           (setq retval (cons (list key val) retval)))))
4902     retval))
4903
4904 (defun gnus-url-unhex (x)
4905   (if (> x ?9)
4906       (if (>= x ?a)
4907           (+ 10 (- x ?a))
4908         (+ 10 (- x ?A)))
4909     (- x ?0)))
4910
4911 (defun gnus-url-unhex-string (str &optional allow-newlines)
4912   "Remove %XXX embedded spaces, etc in a url.
4913 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
4914 decoding of carriage returns and line feeds in the string, which is normally
4915 forbidden in URL encoding."
4916   (setq str (or str ""))
4917   (let ((tmp "")
4918         (case-fold-search t))
4919     (while (string-match "%[0-9a-f][0-9a-f]" str)
4920       (let* ((start (match-beginning 0))
4921              (ch1 (gnus-url-unhex (elt str (+ start 1))))
4922              (code (+ (* 16 ch1)
4923                       (gnus-url-unhex (elt str (+ start 2))))))
4924         (setq tmp (concat
4925                    tmp (substring str 0 start)
4926                    (cond
4927                     (allow-newlines
4928                      (char-to-string code))
4929                     ((or (= code ?\n) (= code ?\r))
4930                      " ")
4931                     (t (char-to-string code))))
4932               str (substring str (match-end 0)))))
4933     (setq tmp (concat tmp str))
4934     tmp))
4935
4936 (defun gnus-url-mailto (url)
4937   ;; Send mail to someone
4938   (when (string-match "mailto:/*\\(.*\\)" url)
4939     (setq url (substring url (match-beginning 1) nil)))
4940   (let (to args subject func)
4941     (if (string-match (regexp-quote "?") url)
4942         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
4943               args (gnus-url-parse-query-string
4944                     (substring url (match-end 0) nil) t))
4945       (setq to (gnus-url-unhex-string url)))
4946     (setq args (cons (list "to" to) args)
4947           subject (cdr-safe (assoc "subject" args)))
4948     (gnus-setup-message 'reply
4949       (message-mail)
4950       (while args
4951         (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
4952         (if (fboundp func)
4953             (funcall func)
4954           (message-position-on-field (caar args)))
4955         (insert (mapconcat 'identity (cdar args) ", "))
4956         (setq args (cdr args)))
4957       (if subject
4958           (message-goto-body)
4959         (message-goto-subject)))))
4960
4961 (defun gnus-button-mailto (address)
4962   ;; Mail to ADDRESS.
4963   (set-buffer (gnus-copy-article-buffer))
4964   (gnus-setup-message 'reply
4965     (message-reply address)))
4966
4967 (defun gnus-button-reply (address)
4968   ;; Reply to ADDRESS.
4969   (gnus-setup-message 'reply
4970     (message-reply address)))
4971
4972 (defun gnus-button-embedded-url (address)
4973   "Browse ADDRESS."
4974   (browse-url (gnus-strip-whitespace address)))
4975
4976 (defun gnus-article-smiley-display ()
4977   "Display \"smileys\" as small graphical icons."
4978   (smiley-toggle-buffer 1 (current-buffer) (point-min) (point-max)))
4979
4980 ;;; Next/prev buttons in the article buffer.
4981
4982 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
4983 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
4984
4985 (defvar gnus-prev-page-map nil)
4986 (unless gnus-prev-page-map
4987   (setq gnus-prev-page-map (make-sparse-keymap))
4988   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
4989   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
4990
4991 (static-if (featurep 'xemacs)
4992     (defun gnus-insert-prev-page-button ()
4993       (let ((buffer-read-only nil))
4994         (gnus-eval-format
4995          gnus-prev-page-line-format nil
4996          `(gnus-prev t local-map ,gnus-prev-page-map
4997                      gnus-callback gnus-article-button-prev-page
4998                      article-type annotation))))
4999   (defun gnus-insert-prev-page-button ()
5000     (let ((buffer-read-only nil)
5001           (situation (get-text-property (point-min) 'mime-view-situation)))
5002       (set-keymap-parent gnus-prev-page-map (current-local-map))
5003       (gnus-eval-format
5004        gnus-prev-page-line-format nil
5005        `(gnus-prev t local-map ,gnus-prev-page-map
5006                    gnus-callback gnus-article-button-prev-page
5007                    article-type annotation
5008                    mime-view-situation ,situation))))
5009   )
5010
5011 (defvar gnus-next-page-map nil)
5012 (unless gnus-next-page-map
5013   (setq gnus-next-page-map (make-sparse-keymap))
5014   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
5015   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
5016
5017 (defun gnus-button-next-page ()
5018   "Go to the next page."
5019   (interactive)
5020   (let ((win (selected-window)))
5021     (select-window (get-buffer-window gnus-article-buffer t))
5022     (gnus-article-next-page)
5023     (select-window win)))
5024
5025 (defun gnus-button-prev-page ()
5026   "Go to the prev page."
5027   (interactive)
5028   (let ((win (selected-window)))
5029     (select-window (get-buffer-window gnus-article-buffer t))
5030     (gnus-article-prev-page)
5031     (select-window win)))
5032
5033 (static-if (featurep 'xemacs)
5034     (defun gnus-insert-next-page-button ()
5035       (let ((buffer-read-only nil))
5036         (gnus-eval-format gnus-next-page-line-format nil
5037                           `(gnus-next
5038                             t local-map ,gnus-next-page-map
5039                             gnus-callback gnus-article-button-next-page
5040                             article-type annotation))))
5041   (defun gnus-insert-next-page-button ()
5042     (let ((buffer-read-only nil)
5043           (situation (get-text-property (point-min) 'mime-view-situation)))
5044       (set-keymap-parent gnus-next-page-map (current-local-map))
5045       (gnus-eval-format gnus-next-page-line-format nil
5046                         `(gnus-next
5047                           t local-map ,gnus-next-page-map
5048                           gnus-callback gnus-article-button-next-page
5049                           article-type annotation
5050                           mime-view-situation ,situation))))
5051   )
5052
5053 (defun gnus-article-button-next-page (arg)
5054   "Go to the next page."
5055   (interactive "P")
5056   (let ((win (selected-window)))
5057     (select-window (get-buffer-window gnus-article-buffer t))
5058     (gnus-article-next-page)
5059     (select-window win)))
5060
5061 (defun gnus-article-button-prev-page (arg)
5062   "Go to the prev page."
5063   (interactive "P")
5064   (let ((win (selected-window)))
5065     (select-window (get-buffer-window gnus-article-buffer t))
5066     (gnus-article-prev-page)
5067     (select-window win)))
5068
5069 (defvar gnus-decode-header-methods
5070   '(mail-decode-encoded-word-region)
5071   "List of methods used to decode headers.
5072
5073 This variable is a list of FUNCTION or (REGEXP . FUNCTION).  If item
5074 is FUNCTION, FUNCTION will be apply to all newsgroups.  If item is a
5075 (REGEXP . FUNCTION), FUNCTION will be only apply to thes newsgroups
5076 whose names match REGEXP.
5077
5078 For example:
5079 ((\"chinese\" . gnus-decode-encoded-word-region-by-guess)
5080  mail-decode-encoded-word-region
5081  (\"chinese\" . rfc1843-decode-region))
5082 ")
5083
5084 (defvar gnus-decode-header-methods-cache nil)
5085
5086 (defun gnus-multi-decode-header (start end)
5087   "Apply the functions from `gnus-encoded-word-methods' that match."
5088   (unless (and gnus-decode-header-methods-cache
5089                (eq gnus-newsgroup-name
5090                    (car gnus-decode-header-methods-cache)))
5091     (setq gnus-decode-header-methods-cache (list gnus-newsgroup-name))
5092     (mapcar (lambda (x)
5093               (if (symbolp x)
5094                   (nconc gnus-decode-header-methods-cache (list x))
5095                 (if (and gnus-newsgroup-name
5096                          (string-match (car x) gnus-newsgroup-name))
5097                     (nconc gnus-decode-header-methods-cache
5098                            (list (cdr x))))))
5099           gnus-decode-header-methods))
5100   (let ((xlist gnus-decode-header-methods-cache))
5101     (pop xlist)
5102     (save-restriction
5103       (narrow-to-region start end)
5104       (while xlist
5105         (funcall (pop xlist) (point-min) (point-max))))))
5106
5107 ;;;
5108 ;;; Treatment top-level handling.
5109 ;;;
5110
5111 (defun gnus-treat-article (condition &optional part-number total-parts type)
5112   (let ((length (- (point-max) (point-min)))
5113         (alist gnus-treatment-function-alist)
5114         (article-goto-body-goes-to-point-min-p t)
5115         (treated-type
5116          (or (not type)
5117              (catch 'found
5118                (let ((list gnus-article-treat-types))
5119                  (while list
5120                    (when (string-match (pop list) type)
5121                      (throw 'found t)))))))
5122         (highlightp (gnus-visual-p 'article-highlight 'highlight))
5123         (entity (static-unless (featurep 'xemacs)
5124                   (when (eq 'head condition)
5125                     (get-text-property (point-min) 'mime-view-entity))))
5126         val elem buttonized)
5127     (gnus-run-hooks 'gnus-part-display-hook)
5128     (unless gnus-inhibit-treatment
5129       (while (setq elem (pop alist))
5130         (setq val
5131               (save-excursion
5132                 (if (gnus-buffer-live-p gnus-summary-buffer)
5133                     (set-buffer gnus-summary-buffer))
5134                 (symbol-value (car elem))))
5135         (when (and (or (consp val)
5136                        treated-type)
5137                    (gnus-treat-predicate val)
5138                    (or (not (get (car elem) 'highlight))
5139                        highlightp))
5140           (when (and (not buttonized)
5141                      (memq (car elem)
5142                            '(gnus-treat-hide-signature
5143                              gnus-treat-highlight-signature)))
5144             (gnus-article-buttonize-signature)
5145             (setq buttonized t))
5146           (save-restriction
5147             (funcall (cadr elem)))))
5148       ;; FSF Emacsen does not inherit the existing text properties
5149       ;; in the new text, so we should do it for `mime-view-entity'.
5150       (static-unless (featurep 'xemacs)
5151         (when entity
5152           (put-text-property (point-min) (point-max)
5153                              'mime-view-entity entity))))))
5154
5155 ;; Dynamic variables.
5156 (defvar part-number)
5157 (defvar total-parts)
5158 (defvar type)
5159 (defvar condition)
5160 (defvar length)
5161 (defun gnus-treat-predicate (val)
5162   (cond
5163    ((null val)
5164     nil)
5165    ((and (listp val)
5166          (stringp (car val)))
5167     (apply 'gnus-or (mapcar `(lambda (s)
5168                                (string-match s ,(or gnus-newsgroup-name "")))
5169                             val)))
5170    ((listp val)
5171     (let ((pred (pop val)))
5172       (cond
5173        ((eq pred 'or)
5174         (apply 'gnus-or (mapcar 'gnus-treat-predicate val)))
5175        ((eq pred 'and)
5176         (apply 'gnus-and (mapcar 'gnus-treat-predicate val)))
5177        ((eq pred 'not)
5178         (not (gnus-treat-predicate (car val))))
5179        ((eq pred 'typep)
5180         (equal (car val) type))
5181        (t
5182         (error "%S is not a valid predicate" pred)))))
5183    ((eq val 'mime)
5184     gnus-show-mime)
5185    (condition
5186     (eq condition val))
5187    ((eq val t)
5188     t)
5189    ((eq val 'head)
5190     nil)
5191    ((eq val 'last)
5192     (eq part-number total-parts))
5193    ((numberp val)
5194     (< length val))
5195    (t
5196     (error "%S is not a valid value" val))))
5197
5198
5199 ;;; @ for mime-view
5200 ;;;
5201
5202 (defun gnus-article-header-presentation-method (entity situation)
5203   (mime-insert-header entity)
5204   )
5205
5206 (set-alist 'mime-header-presentation-method-alist
5207            'gnus-original-article-mode
5208            #'gnus-article-header-presentation-method)
5209
5210 (defun gnus-mime-preview-quitting-method ()
5211   (mime-preview-kill-buffer)
5212   (delete-other-windows)
5213   (gnus-article-show-summary)
5214   (gnus-summary-select-article gnus-show-all-headers t))
5215
5216 (set-alist 'mime-preview-quitting-method-alist
5217            'gnus-original-article-mode #'gnus-mime-preview-quitting-method)
5218
5219 (set-alist 'mime-preview-following-method-alist
5220            'gnus-original-article-mode #'gnus-following-method)
5221
5222 (set-alist 'mime-preview-over-to-previous-method-alist
5223            'gnus-original-article-mode
5224            (lambda ()
5225              (if (> (point-min) 1)
5226                  (gnus-article-prev-page)
5227                (gnus-article-read-summary-keys
5228                 nil (gnus-character-to-event ?P)))))
5229
5230 (set-alist 'mime-preview-over-to-next-method-alist
5231            'gnus-original-article-mode'
5232            (lambda ()
5233              (if (< (point-max) (buffer-size))
5234                  (gnus-article-next-page)
5235                (gnus-article-read-summary-keys
5236                 nil (gnus-character-to-event ?N)))))
5237
5238
5239 ;;; @ end
5240 ;;;
5241
5242 (gnus-ems-redefine)
5243
5244 (provide 'gnus-art)
5245
5246 (run-hooks 'gnus-art-load-hook)
5247
5248 ;;; gnus-art.el ends here