*** empty log message ***
[gnus] / lisp / gnus-art.el
1 ;;; gnus-art.el --- article mode commands for Gnus
2 ;; Copyright (C) 1996 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
23
24 ;;; Commentary:
25
26 ;;; Code:
27
28 (require 'custom)
29 (require 'gnus)
30 (require 'gnus-sum)
31 (require 'gnus-spec)
32 (require 'gnus-int)
33 (require 'browse-url)
34
35 (defgroup article nil
36   "Article display."
37   :group 'gnus)
38
39 (defcustom gnus-ignored-headers
40   '("^Path:" "^Posting-Version:" "^Article-I.D.:" "^Expires:"
41     "^Date-Received:" "^References:" "^Control:" "^Xref:" "^Lines:"
42     "^Posted:" "^Relay-Version:" "^Message-ID:" "^Nf-ID:" "^Nf-From:"
43     "^Approved:" "^Sender:" "^Received:" "^Mail-from:") 
44   "All headers that match this regexp will be hidden.
45 This variable can also be a list of regexps of headers to be ignored.
46 If `gnus-visible-headers' is non-nil, this variable will be ignored."
47   :type '(choice :custom-show nil
48                  regexp
49                  (repeat regexp))
50   :group 'article)
51
52 (defcustom gnus-visible-headers 
53   "^From:\\|^Newsgroups:\\|^Subject:\\|^Date:\\|^Followup-To:\\|^Reply-To:\\|^Organization:\\|^Summary:\\|^Keywords:\\|^To:\\|^Cc:\\|^Posted-To:\\|^Mail-Copies-To:\\|^Apparently-To:\\|^Gnus-Warning:\\|^Resent-"
54   "All headers that do not match this regexp will be hidden.
55 This variable can also be a list of regexp of headers to remain visible.
56 If this variable is non-nil, `gnus-ignored-headers' will be ignored."
57   :type '(repeat :value-to-internal (lambda (widget value)
58                                       (custom-split-regexp-maybe value))
59                  :match (lambda (widget value)
60                           (or (stringp value)
61                               (widget-editable-list-match widget value)))
62                  regexp)
63   :group 'article)
64
65 (defcustom gnus-sorted-header-list
66   '("^From:" "^Subject:" "^Summary:" "^Keywords:" "^Newsgroups:" "^To:"
67     "^Cc:" "^Date:" "^Organization:")
68   "This variable is a list of regular expressions.
69 If it is non-nil, headers that match the regular expressions will
70 be placed first in the article buffer in the sequence specified by
71 this list."
72   :type '(repeat regexp)
73   :group 'article)
74
75 (defcustom gnus-boring-article-headers '(empty followup-to reply-to)
76   "Headers that are only to be displayed if they have interesting data.
77 Possible values in this list are `empty', `newsgroups', `followup-to',
78 `reply-to', and `date'."
79   :type '(set (const :tag "Headers with no content." empty)
80               (const :tag "Newsgroups with only one group." newsgroups)
81               (const :tag "Followup-to identical to newsgroups." followup-to)
82               (const :tag "Reply-to identical to from." reply-to)
83               (const :tag "Date less than four days old." date))
84   :group 'article)
85
86 (defcustom gnus-signature-separator '("^-- $" "^-- *$")
87   "Regexp matching signature separator.
88 This can also be a list of regexps.  In that case, it will be checked
89 from head to tail looking for a separator.  Searches will be done from
90 the end of the buffer."
91   :type '(repeat string)
92   :group 'article)
93
94 (defcustom gnus-signature-limit nil
95    "Provide a limit to what is considered a signature.
96 If it is a number, no signature may not be longer (in characters) than
97 that number.  If it is a floating point number, no signature may be
98 longer (in lines) than that number.  If it is a function, the function
99 will be called without any parameters, and if it returns nil, there is
100 no signature in the buffer.  If it is a string, it will be used as a
101 regexp.  If it matches, the text in question is not a signature."
102   :type '(choice integer number function regexp)
103   :group 'article)
104
105 (defcustom gnus-hidden-properties '(invisible t intangible t)
106   "Property list to use for hiding text."
107   :type 'sexp 
108   :group 'article)
109
110 (defcustom gnus-article-x-face-command
111   "{ echo '/* Width=48, Height=48 */'; uncompface; } | icontopbm | xv -quit -"
112   "String or function to be executed to display an X-Face header.
113 If it is a string, the command will be executed in a sub-shell
114 asynchronously.  The compressed face will be piped to this command."
115   :type 'string                         ;Leave function case to Lisp.
116   :group 'article)
117
118 (defcustom gnus-article-x-face-too-ugly nil
119   "Regexp matching posters whose face shouldn't be shown automatically."
120   :type 'regexp
121   :group 'article)
122
123 (defcustom gnus-emphasis-alist
124   (let ((format
125          "\\(\\s-\\|^\\|[-\"\(]\\)\\(%s\\(\\w+\\(\\s-+\\w+\\)*\\)%s\\)\\(\\s-\\|[-?!.,;:\"\)]\\)")
126         (types
127          '(("_" "_" underline)
128            ("/" "/" italic)
129            ("\\*" "\\*" bold)
130            ("_/" "/_" underline-italic)
131            ("_\\*" "\\*_" underline-bold)
132            ("\\*/" "/\\*" bold-italic)
133            ("_\\*/" "/\\*_" underline-bold-italic))))
134     `(("\\(\\s-\\|^\\)\\(_\\(\\(\\w\\|_\\)+\\)_\\)\\(\\s-\\|[?!.,;]\\)"
135        2 3 gnus-emphasis-underline)
136       ,@(mapcar
137          (lambda (spec)
138            (list
139             (format format (car spec) (cadr spec))
140             2 3 (intern (format "gnus-emphasis-%s" (caddr spec)))))
141          types)))
142   "Alist that says how to fontify certain phrases.
143 Each item looks like this:
144
145   (\"_\\\\(\\\\w+\\\\)_\" 0 1 'underline)
146
147 The first element is a regular expression to be matched.  The second
148 is a number that says what regular expression grouping used to find
149 the entire emphasized word.  The third is a number that says what
150 regexp grouping should be displayed and highlighted.  The fourth
151 is the face used for highlighting."
152   :type '(repeat (list :value ("" 0 0 default)
153                        regexp
154                        (integer :tag "Match group")
155                        (integer :tag "Emphasize group")
156                        face))
157   :group 'article)
158
159 (defface gnus-emphasis-bold '((t (:bold t)))
160   "Face used for displaying strong emphasized text (*word*)."
161   :group 'article)
162
163 (defface gnus-emphasis-italic '((t (:italic t)))
164   "Face used for displaying italic emphasized text (/word/)."
165   :group 'article)
166
167 (defface gnus-emphasis-underline '((t (:underline t)))
168   "Face used for displaying underlined emphasized text (_word_)."
169   :group 'article)
170
171 (defface gnus-emphasis-underline-bold '((t (:bold t :underline t)))
172   "Face used for displaying underlined bold emphasized text (_*word*_)."
173   :group 'article)
174
175 (defface gnus-emphasis-underline-italic '((t (:italic t :underline t)))
176   "Face used for displaying underlined italic emphasized text (_*word*_)."
177   :group 'article)
178
179 (defface gnus-emphasis-bold-italic '((t (:bold t :italic t)))
180   "Face used for displaying bold italic emphasized text (/*word*/)."
181   :group 'article)
182
183 (defface gnus-emphasis-underline-bold-italic 
184   '((t (:bold t :italic t :underline t)))
185   "Face used for displaying underlined bold italic emphasized text (_/*word*/_)."
186   :group 'article)
187
188 (eval-and-compile
189   (autoload 'hexl-hex-string-to-integer "hexl")
190   (autoload 'timezone-make-date-arpa-standard "timezone")
191   (autoload 'mail-extract-address-components "mail-extr"))
192
193 (defcustom gnus-article-save-directory gnus-directory
194   "*Name of the directory articles will be saved in (default \"~/News\")."
195   :group 'article
196   :type 'directory)
197
198 (defcustom gnus-save-all-headers t
199   "*If non-nil, don't remove any headers before saving."
200   :group 'article
201   :type 'boolean)
202
203 (defcustom gnus-prompt-before-saving 'always
204   "*This variable says how much prompting is to be done when saving articles.
205 If it is nil, no prompting will be done, and the articles will be
206 saved to the default files.  If this variable is `always', each and
207 every article that is saved will be preceded by a prompt, even when
208 saving large batches of articles.  If this variable is neither nil not
209 `always', there the user will be prompted once for a file name for
210 each invocation of the saving commands."
211   :group 'article
212   :type '(choice (item always)
213                  (item :tag "never" nil)
214                  (sexp :tag "once" :format "%t")))
215
216 (defcustom gnus-saved-headers gnus-visible-headers
217   "Headers to keep if `gnus-save-all-headers' is nil.
218 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
219 If that variable is nil, however, all headers that match this regexp
220 will be kept while the rest will be deleted before saving."
221   :group 'article
222   :type '(repeat string))
223
224 (defcustom gnus-default-article-saver 'gnus-summary-save-in-rmail
225   "A function to save articles in your favourite format.
226 The function must be interactively callable (in other words, it must
227 be an Emacs command).
228
229 Gnus provides the following functions:
230
231 * gnus-summary-save-in-rmail (Rmail format)
232 * gnus-summary-save-in-mail (Unix mail format)
233 * gnus-summary-save-in-folder (MH folder)
234 * gnus-summary-save-in-file (article format)
235 * gnus-summary-save-in-vm (use VM's folder format)
236 * gnus-summary-write-to-file (article format -- overwrite)."
237   :group 'article
238   :type '(radio (function-item gnus-summary-save-in-rmail)
239                 (function-item gnus-summary-save-in-mail)
240                 (function-item gnus-summary-save-in-folder)
241                 (function-item gnus-summary-save-in-file)
242                 (function-item gnus-summary-save-in-vm)
243                 (function-item gnus-summary-write-to-file)))
244
245 (defcustom gnus-rmail-save-name 'gnus-plain-save-name
246   "A function generating a file name to save articles in Rmail format.
247 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
248   :group 'article
249   :type 'function)
250
251 (defcustom gnus-mail-save-name 'gnus-plain-save-name
252   "A function generating a file name to save articles in Unix mail format.
253 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE."
254   :group 'article
255   :type 'function)
256
257 (defcustom gnus-folder-save-name 'gnus-folder-save-name
258   "A function generating a file name to save articles in MH folder.
259 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER."
260   :group 'article
261   :type 'function)
262
263 (defcustom gnus-file-save-name 'gnus-numeric-save-name
264   "A function generating a file name to save articles in article format.
265 The function is called with NEWSGROUP, HEADERS, and optional
266 LAST-FILE."
267   :group 'article
268   :type 'function)
269
270 (defcustom gnus-split-methods
271   '((gnus-article-archive-name))
272   "Variable used to suggest where articles are to be saved.
273 For instance, if you would like to save articles related to Gnus in
274 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
275 you could set this variable to something like:
276
277  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
278    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
279
280 This variable is an alist where the where the key is the match and the
281 value is a list of possible files to save in if the match is non-nil.
282
283 If the match is a string, it is used as a regexp match on the
284 article.  If the match is a symbol, that symbol will be funcalled
285 from the buffer of the article to be saved with the newsgroup as the
286 parameter.  If it is a list, it will be evaled in the same buffer.
287
288 If this form or function returns a string, this string will be used as
289 a possible file name; and if it returns a non-nil list, that list will
290 be used as possible file names."
291   :group 'article
292   :type '(repeat (choice (list function)
293                          (cons regexp (repeat string))
294                          sexp)))
295
296 (defcustom gnus-strict-mime t
297   "*If nil, MIME-decode even if there is no Mime-Version header."
298   :group 'article
299   :type 'boolean)
300
301 (defcustom gnus-show-mime-method 'metamail-buffer
302   "Function to process a MIME message.
303 The function is called from the article buffer."
304   :group 'article
305   :type 'function)
306
307 (defcustom gnus-decode-encoded-word-method 'gnus-article-de-quoted-unreadable
308   "*Function to decode MIME encoded words.
309 The function is called from the article buffer."
310   :group 'article
311   :type 'function)
312
313 (defcustom gnus-page-delimiter "^\^L"
314   "*Regexp describing what to use as article page delimiters.
315 The default value is \"^\^L\", which is a form linefeed at the
316 beginning of a line."
317   :type 'regexp
318   :group 'article)
319
320 (defcustom gnus-article-mode-line-format "Gnus: %%b %S"
321   "*The format specification for the article mode line.
322 See `gnus-summary-mode-line-format' for a closer description."
323   :type 'string
324   :group 'article)
325
326 (defcustom gnus-article-mode-hook nil
327   "*A hook for Gnus article mode."
328   :type 'hook
329   :group 'article)
330
331 (defcustom gnus-article-menu-hook nil
332   "*Hook run after the creation of the article mode menu."
333   :type 'hook
334   :group 'article)
335
336 (defcustom gnus-article-prepare-hook nil
337   "*A hook called after an article has been prepared in the article buffer.
338 If you want to run a special decoding program like nkf, use this hook."
339   :type 'hook
340   :group 'article)
341
342 (defcustom gnus-article-button-face 'bold
343   "Face used for highlighting buttons in the article buffer.
344
345 An article button is a piece of text that you can activate by pressing
346 `RET' or `mouse-2' above it."
347   :type 'face
348   :group 'article)
349
350 (defcustom gnus-article-mouse-face 'highlight
351   "Face used for mouse highlighting in the article buffer.
352
353 Article buttons will be displayed in this face when the cursor is
354 above them."
355   :type 'face
356   :group 'article)
357
358 (defcustom gnus-signature-face 'italic
359   "Face used for highlighting a signature in the article buffer."
360   :type 'face
361   :group 'article)
362
363 (defface gnus-header-from-face 
364   '((((class color)
365       (background dark))
366      (:foreground "light blue" :bold t :italic t))
367     (((class color)
368       (background light))
369      (:foreground "MidnightBlue" :bold t :italic t))
370     (t 
371      (:bold t :italic t)))
372   "Face used for displaying from headers."
373   :group 'article)
374
375 (defface gnus-header-subject-face 
376   '((((class color)
377       (background dark))
378      (:foreground "pink" :bold t :italic t))
379     (((class color)
380       (background light))
381      (:foreground "firebrick" :bold t :italic t))
382     (t 
383      (:bold t :italic t)))
384   "Face used for displaying subject headers."
385   :group 'article)
386
387 (defface gnus-header-newsgroups-face 
388   '((((class color)
389       (background dark))
390      (:foreground "yellow" :bold t :italic t))
391     (((class color)
392       (background light))
393      (:foreground "indianred" :bold t :italic t))
394     (t 
395      (:bold t :italic t)))
396   "Face used for displaying newsgroups headers."
397   :group 'article)
398
399 (defface gnus-header-name-face 
400   '((((class color)
401       (background dark))
402      (:foreground "cyan" :bold t))
403     (((class color)
404       (background light))
405      (:foreground "DarkGreen" :bold t))
406     (t 
407      (:bold t)))
408   "Face used for displaying header names."
409   :group 'article)
410
411 (defface gnus-header-content-face
412   '((((class color)
413       (background dark))
414      (:foreground "forest green" :italic t))
415     (((class color)
416       (background light))
417      (:foreground "DarkGreen" :italic t))
418     (t 
419      (:italic t)))  "Face used for displaying header content."
420   :group 'article)
421
422 (defcustom gnus-header-face-alist
423   '(("From" nil gnus-header-from-face)
424     ("Subject" nil gnus-header-subject-face)
425     ("Newsgroups:.*," nil gnus-header-newsgroups-face)
426     ("" gnus-header-name-face gnus-header-content-face))
427   "Controls highlighting of article header.
428
429 An alist of the form (HEADER NAME CONTENT). 
430
431 HEADER is a regular expression which should match the name of an
432 header header and NAME and CONTENT are either face names or nil.
433
434 The name of each header field will be displayed using the face
435 specified by the first element in the list where HEADER match the
436 header name and NAME is non-nil.  Similarly, the content will be
437 displayed by the first non-nil matching CONTENT face."
438   :group 'article
439   :type '(repeat (list (regexp :tag "Header")
440                        (choice :tag "Name"
441                                (item :tag "skip" nil)
442                                (face :value default))
443                        (choice :tag "Content"
444                                (item :tag "skip" nil)
445                                (face :value default)))))
446
447 ;;; Internal variables
448
449 (defvar gnus-article-mode-syntax-table
450   (let ((table (copy-syntax-table text-mode-syntax-table)))
451     ;;(modify-syntax-entry ?_ "w" table)
452     table)
453   "Syntax table used in article mode buffers.
454 Initialized from `text-mode-syntax-table.")
455
456 (defvar gnus-save-article-buffer nil)
457
458 (defvar gnus-article-mode-line-format-alist
459     (nconc '((?w (gnus-article-wash-status) ?s))
460            gnus-summary-mode-line-format-alist))
461
462 (defvar gnus-number-of-articles-to-be-saved nil)
463
464 (defvar gnus-inhibit-hiding nil)
465 (defvar gnus-newsgroup-name)
466
467 (defsubst gnus-article-hide-text (b e props)
468   "Set text PROPS on the B to E region, extending `intangible' 1 past B."
469   (add-text-properties b e props)
470   (when (memq 'intangible props)
471     (put-text-property 
472      (max (1- b) (point-min))
473      b 'intangible (cddr (memq 'intangible props)))))
474
475 (defsubst gnus-article-unhide-text (b e)
476   "Remove hidden text properties from region between B and E."
477   (remove-text-properties b e gnus-hidden-properties)
478   (when (memq 'intangible gnus-hidden-properties)
479     (put-text-property (max (1- b) (point-min))
480                        b 'intangible nil)))
481
482 (defun gnus-article-hide-text-type (b e type)
483   "Hide text of TYPE between B and E."
484   (gnus-article-hide-text
485    b e (cons 'article-type (cons type gnus-hidden-properties))))
486
487 (defun gnus-article-unhide-text-type (b e type)
488   "Hide text of TYPE between B and E."
489   (remove-text-properties
490    b e (cons 'article-type (cons type gnus-hidden-properties)))
491   (when (memq 'intangible gnus-hidden-properties)
492     (put-text-property (max (1- b) (point-min))
493                        b 'intangible nil)))
494
495 (defun gnus-article-hide-text-of-type (type)
496   "Hide text of TYPE in the current buffer."
497   (save-excursion
498     (let ((b (point-min))
499           (e (point-max)))
500       (while (setq b (text-property-any b e 'article-type type))
501         (add-text-properties b (incf b) gnus-hidden-properties)))))
502
503 (defun gnus-article-delete-text-of-type (type)
504   "Delete text of TYPE in the current buffer."
505   (save-excursion
506     (let ((b (point-min)))
507       (while (setq b (text-property-any b (point-max) 'article-type type))
508         (delete-region b (incf b))))))
509
510 (defun gnus-article-delete-invisible-text ()
511   "Delete all invisible text in the current buffer."
512   (save-excursion
513     (let ((b (point-min)))
514       (while (setq b (text-property-any b (point-max) 'invisible t))
515         (delete-region b (incf b))))))
516
517 (defun gnus-article-text-type-exists-p (type)
518   "Say whether any text of type TYPE exists in the buffer."
519   (text-property-any (point-min) (point-max) 'article-type type))
520
521 (defsubst gnus-article-header-rank ()
522   "Give the rank of the string HEADER as given by `article-sorted-header-list'."
523   (let ((list gnus-sorted-header-list)
524         (i 0))
525     (while list
526       (when (looking-at (car list))
527         (setq list nil))
528       (setq list (cdr list))
529       (incf i))
530     i))
531
532 (defun gnus-article-hide-headers (&optional arg delete)
533   "Toggle whether to hide unwanted headers and possibly sort them as well.
534 If given a negative prefix, always show; if given a positive prefix,
535 always hide."
536   (interactive (gnus-article-hidden-arg))
537   (if (gnus-article-check-hidden-text 'headers arg)
538       ;; Show boring headers as well.
539       (gnus-article-show-hidden-text 'boring-headers)
540     ;; This function might be inhibited.
541     (unless gnus-inhibit-hiding
542       (save-excursion
543         (save-restriction
544           (let ((buffer-read-only nil)
545                 (props (nconc (list 'article-type 'headers)
546                               gnus-hidden-properties))
547                 (max (1+ (length gnus-sorted-header-list)))
548                 (ignored (when (not gnus-visible-headers)
549                            (cond ((stringp gnus-ignored-headers)
550                                   gnus-ignored-headers)
551                                  ((listp gnus-ignored-headers)
552                                   (mapconcat 'identity gnus-ignored-headers
553                                              "\\|")))))
554                 (visible
555                  (cond ((stringp gnus-visible-headers)
556                         gnus-visible-headers)
557                        ((and gnus-visible-headers
558                              (listp gnus-visible-headers))
559                         (mapconcat 'identity gnus-visible-headers "\\|"))))
560                 (inhibit-point-motion-hooks t)
561                 want-list beg)
562             ;; First we narrow to just the headers.
563             (widen)
564             (goto-char (point-min))
565             ;; Hide any "From " lines at the beginning of (mail) articles.
566             (while (looking-at "From ")
567               (forward-line 1))
568             (unless (bobp)
569               (if delete
570                   (delete-region (point-min) (point))
571                 (gnus-article-hide-text (point-min) (point) props)))
572             ;; Then treat the rest of the header lines.
573             (narrow-to-region
574              (point)
575              (if (search-forward "\n\n" nil t) ; if there's a body
576                  (progn (forward-line -1) (point))
577                (point-max)))
578             ;; Then we use the two regular expressions
579             ;; `gnus-ignored-headers' and `gnus-visible-headers' to
580             ;; select which header lines is to remain visible in the
581             ;; article buffer.
582             (goto-char (point-min))
583             (while (re-search-forward "^[^ \t]*:" nil t)
584               (beginning-of-line)
585               ;; Mark the rank of the header.
586               (put-text-property 
587                (point) (1+ (point)) 'message-rank
588                (if (or (and visible (looking-at visible))
589                        (and ignored
590                             (not (looking-at ignored))))
591                    (gnus-article-header-rank) 
592                  (+ 2 max)))
593               (forward-line 1))
594             (message-sort-headers-1)
595             (when (setq beg (text-property-any 
596                              (point-min) (point-max) 'message-rank (+ 2 max)))
597               ;; We make the unwanted headers invisible.
598               (if delete
599                   (delete-region beg (point-max))
600                 ;; Suggested by Sudish Joseph <joseph@cis.ohio-state.edu>.
601                 (gnus-article-hide-text-type beg (point-max) 'headers))
602               ;; Work around XEmacs lossage.
603               (put-text-property (point-min) beg 'invisible nil))))))))
604
605 (defun gnus-article-hide-boring-headers (&optional arg)
606   "Toggle hiding of headers that aren't very interesting.
607 If given a negative prefix, always show; if given a positive prefix,
608 always hide."
609   (interactive (gnus-article-hidden-arg))
610   (when (and (not (gnus-article-check-hidden-text 'boring-headers arg))
611              (not gnus-show-all-headers))
612     (save-excursion
613       (save-restriction
614         (let ((buffer-read-only nil)
615               (list gnus-boring-article-headers)
616               (inhibit-point-motion-hooks t)
617               elem)
618           (nnheader-narrow-to-headers)
619           (while list
620             (setq elem (pop list))
621             (goto-char (point-min))
622             (cond
623              ;; Hide empty headers.
624              ((eq elem 'empty)
625               (while (re-search-forward "^[^:]+:[ \t]*\n[^ \t]" nil t)
626                 (forward-line -1)
627                 (gnus-article-hide-text-type
628                  (progn (beginning-of-line) (point))
629                  (progn 
630                    (end-of-line)
631                    (if (re-search-forward "^[^ \t]" nil t)
632                        (match-beginning 0)
633                      (point-max)))
634                  'boring-headers)))
635              ;; Hide boring Newsgroups header.
636              ((eq elem 'newsgroups)
637               (when (equal (gnus-fetch-field "newsgroups")
638                            (gnus-group-real-name
639                             (if (boundp 'gnus-newsgroup-name)
640                                 gnus-newsgroup-name
641                               "")))
642                 (gnus-article-hide-header "newsgroups")))
643              ((eq elem 'followup-to)
644               (when (equal (message-fetch-field "followup-to")
645                            (message-fetch-field "newsgroups"))
646                 (gnus-article-hide-header "followup-to")))
647              ((eq elem 'reply-to)
648               (let ((from (message-fetch-field "from"))
649                     (reply-to (message-fetch-field "reply-to")))
650                 (when (and
651                        from reply-to
652                        (equal 
653                         (nth 1 (mail-extract-address-components from))
654                         (nth 1 (mail-extract-address-components reply-to))))
655                   (gnus-article-hide-header "reply-to"))))
656              ((eq elem 'date)
657               (let ((date (message-fetch-field "date")))
658                 (when (and date
659                            (< (gnus-days-between (current-time-string) date)
660                               4))
661                   (gnus-article-hide-header "date")))))))))))
662
663 (defun gnus-article-hide-header (header)
664   (save-excursion
665     (goto-char (point-min))
666     (when (re-search-forward (concat "^" header ":") nil t)
667       (gnus-article-hide-text-type
668        (progn (beginning-of-line) (point))
669        (progn 
670          (end-of-line)
671          (if (re-search-forward "^[^ \t]" nil t)
672              (match-beginning 0)
673            (point-max)))
674        'boring-headers))))
675
676 ;; Written by Per Abrahamsen <amanda@iesd.auc.dk>.
677 (defun gnus-article-treat-overstrike ()
678   "Translate overstrikes into bold text."
679   (interactive)
680   (save-excursion
681     (let ((buffer-read-only nil))
682       (while (search-forward "\b" nil t)
683         (let ((next (following-char))
684               (previous (char-after (- (point) 2))))
685           ;; We do the boldification/underlining by hiding the
686           ;; overstrikes and putting the proper text property
687           ;; on the letters.
688           (cond 
689            ((eq next previous)
690             (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
691             (put-text-property (point) (1+ (point)) 'face 'bold))
692            ((eq next ?_)
693             (gnus-article-hide-text-type (1- (point)) (1+ (point)) 'overstrike)
694             (put-text-property
695              (- (point) 2) (1- (point)) 'face 'underline))
696            ((eq previous ?_)
697             (gnus-article-hide-text-type (- (point) 2) (point) 'overstrike)
698             (put-text-property
699              (point) (1+ (point)) 'face 'underline))))))))
700
701 (defun gnus-article-fill ()
702   "Format too long lines."
703   (interactive)
704   (save-excursion
705     (let ((buffer-read-only nil))
706       (widen)
707       (goto-char (point-min))
708       (search-forward "\n\n" nil t)
709       (end-of-line 1)
710       (let ((paragraph-start "^[>|#:<;* ]*[ \t]*$")
711             (adaptive-fill-regexp "[ \t]*\\([|#:<;>*]+ *\\)?")
712             (adaptive-fill-mode t))
713         (while (not (eobp))
714           (and (>= (current-column) (min fill-column (window-width)))
715                (/= (preceding-char) ?:)
716                (fill-paragraph nil))
717           (end-of-line 2))))))
718
719 (defun gnus-article-remove-cr ()
720   "Remove carriage returns from an article."
721   (interactive)
722   (save-excursion
723     (let ((buffer-read-only nil))
724       (goto-char (point-min))
725       (while (search-forward "\r" nil t)
726         (replace-match "" t t)))))
727
728 (defun gnus-article-remove-trailing-blank-lines ()
729   "Remove all trailing blank lines from the article."
730   (interactive)
731   (save-excursion
732     (let ((buffer-read-only nil))
733       (goto-char (point-max))
734       (delete-region
735        (point)
736        (progn
737          (while (and (not (bobp))
738                      (looking-at "^[ \t]*$"))
739            (forward-line -1))
740          (forward-line 1)
741          (point))))))
742
743 (defun gnus-article-display-x-face (&optional force)
744   "Look for an X-Face header and display it if present."
745   (interactive (list 'force))
746   (save-excursion
747     ;; Delete the old process, if any.
748     (when (process-status "article-x-face")
749       (delete-process "article-x-face"))
750     (let ((inhibit-point-motion-hooks t)
751           (case-fold-search nil)
752           from)
753       (save-restriction
754         (nnheader-narrow-to-headers)
755         (setq from (message-fetch-field "from"))
756         (goto-char (point-min))
757         (when (and gnus-article-x-face-command
758                    (or force
759                        ;; Check whether this face is censored.
760                        (not gnus-article-x-face-too-ugly)
761                        (and gnus-article-x-face-too-ugly from
762                             (not (string-match gnus-article-x-face-too-ugly
763                                                from))))
764                    ;; Has to be present.
765                    (re-search-forward "^X-Face: " nil t))
766           ;; We now have the area of the buffer where the X-Face is stored.
767           (let ((beg (point))
768                 (end (1- (re-search-forward "^\\($\\|[^ \t]\\)" nil t))))
769             ;; We display the face.
770             (if (symbolp gnus-article-x-face-command)
771                 ;; The command is a lisp function, so we call it.
772                 (if (gnus-functionp gnus-article-x-face-command)
773                     (funcall gnus-article-x-face-command beg end)
774                   (error "%s is not a function" gnus-article-x-face-command))
775               ;; The command is a string, so we interpret the command
776               ;; as a, well, command, and fork it off.
777               (let ((process-connection-type nil))
778                 (process-kill-without-query
779                  (start-process
780                   "article-x-face" nil shell-file-name shell-command-switch
781                   gnus-article-x-face-command))
782                 (process-send-region "article-x-face" beg end)
783                 (process-send-eof "article-x-face")))))))))
784
785 (defalias 'gnus-decode-rfc1522 'gnus-article-decode-rfc1522)
786 (defun gnus-article-decode-rfc1522 ()
787   "Hack to remove QP encoding from headers."
788   (let ((case-fold-search t)
789         (inhibit-point-motion-hooks t)
790         (buffer-read-only nil)
791         string)
792     (save-restriction
793       (narrow-to-region
794        (goto-char (point-min))
795        (or (search-forward "\n\n" nil t) (point-max)))
796       (goto-char (point-min))
797       (while (re-search-forward 
798               "=\\?iso-8859-1\\?q\\?\\([^?\t\n]*\\)\\?=" nil t)
799         (setq string (match-string 1))
800         (save-restriction
801           (narrow-to-region (match-beginning 0) (match-end 0))
802           (delete-region (point-min) (point-max))
803           (insert string)
804           (gnus-article-mime-decode-quoted-printable 
805            (goto-char (point-min)) (point-max))
806           (subst-char-in-region (point-min) (point-max) ?_ ? )
807           (goto-char (point-max)))
808         (goto-char (point-min))))))
809
810 (defun gnus-article-de-quoted-unreadable (&optional force)
811   "Do a naive translation of a quoted-printable-encoded article.
812 This is in no way, shape or form meant as a replacement for real MIME
813 processing, but is simply a stop-gap measure until MIME support is
814 written.
815 If FORCE, decode the article whether it is marked as quoted-printable
816 or not."
817   (interactive (list 'force))
818   (save-excursion
819     (let ((case-fold-search t)
820           (buffer-read-only nil)
821           (type (gnus-fetch-field "content-transfer-encoding")))
822       (gnus-article-decode-rfc1522)
823       (when (or force
824                 (and type (string-match "quoted-printable" (downcase type))))
825         (goto-char (point-min))
826         (search-forward "\n\n" nil 'move)
827         (gnus-article-mime-decode-quoted-printable (point) (point-max))))))
828
829 (defun gnus-article-mime-decode-quoted-printable-buffer ()
830   "Decode Quoted-Printable in the current buffer."
831   (gnus-article-mime-decode-quoted-printable (point-min) (point-max)))
832   
833 (defun gnus-article-mime-decode-quoted-printable (from to)
834   "Decode Quoted-Printable in the region between FROM and TO."
835   (interactive "r")
836   (goto-char from)
837   (while (search-forward "=" to t)
838     (cond ((eq (following-char) ?\n)
839            (delete-char -1)
840            (delete-char 1))
841           ((looking-at "[0-9A-F][0-9A-F]")
842            (subst-char-in-region
843             (1- (point)) (point) ?=
844             (hexl-hex-string-to-integer
845              (buffer-substring (point) (+ 2 (point)))))
846            (delete-char 2))
847           ((looking-at "=")
848            (delete-char 1))
849           ((gnus-message 3 "Malformed MIME quoted-printable message")))))
850
851 (defun gnus-article-hide-pgp (&optional arg)
852   "Toggle hiding of any PGP headers and signatures in the current article.
853 If given a negative prefix, always show; if given a positive prefix,
854 always hide."
855   (interactive (gnus-article-hidden-arg))
856   (unless (gnus-article-check-hidden-text 'pgp arg)
857     (save-excursion
858       (let (buffer-read-only beg end)
859         (widen)
860         (goto-char (point-min))
861         ;; Hide the "header".
862         (and (search-forward "\n-----BEGIN PGP SIGNED MESSAGE-----\n" nil t)
863              (gnus-article-hide-text-type (match-beginning 0) (match-end 0) 'pgp))
864         (setq beg (point))
865         ;; Hide the actual signature.
866         (and (search-forward "\n-----BEGIN PGP SIGNATURE-----\n" nil t)
867              (setq end (1+ (match-beginning 0)))
868              (gnus-article-hide-text-type
869               end
870               (if (search-forward "\n-----END PGP SIGNATURE-----\n" nil t)
871                   (match-end 0)
872                 ;; Perhaps we shouldn't hide to the end of the buffer
873                 ;; if there is no end to the signature?
874                 (point-max))
875               'pgp))
876         ;; Hide "- " PGP quotation markers.
877         (when (and beg end)
878           (narrow-to-region beg end)
879           (goto-char (point-min))
880           (while (re-search-forward "^- " nil t)
881             (gnus-article-hide-text-type (match-beginning 0) (match-end 0) 'pgp))
882           (widen))))))
883
884 (defun gnus-article-hide-pem (&optional arg)
885   "Toggle hiding of any PEM headers and signatures in the current article.
886 If given a negative prefix, always show; if given a positive prefix,
887 always hide."
888   (interactive (gnus-article-hidden-arg))
889   (unless (gnus-article-check-hidden-text 'pem arg)
890     (save-excursion
891       (let (buffer-read-only end)
892         (widen)
893         (goto-char (point-min))
894         ;; hide the horrendously ugly "header".
895         (and (search-forward "\n-----BEGIN PRIVACY-ENHANCED MESSAGE-----\n"
896                              nil
897                              t)
898              (setq end (1+ (match-beginning 0)))
899              (gnus-article-hide-text-type
900               end
901               (if (search-forward "\n\n" nil t)
902                   (match-end 0)
903                 (point-max))
904               'pem))
905         ;; hide the trailer as well
906         (and (search-forward "\n-----END PRIVACY-ENHANCED MESSAGE-----\n"
907                              nil
908                              t)
909              (gnus-article-hide-text-type
910               (match-beginning 0) (match-end 0) 'pem))))))
911
912 (defun gnus-article-hide-signature (&optional arg)
913   "Hide the signature in the current article.
914 If given a negative prefix, always show; if given a positive prefix,
915 always hide."
916   (interactive (gnus-article-hidden-arg))
917   (unless (gnus-article-check-hidden-text 'signature arg)
918     (save-excursion
919       (save-restriction
920         (let ((buffer-read-only nil))
921           (when (gnus-article-narrow-to-signature)
922             (gnus-article-hide-text-type (point-min) (point-max) 'signature)))))))
923
924 (defun gnus-article-strip-leading-blank-lines ()
925   "Remove all blank lines from the beginning of the article."
926   (interactive)
927   (save-excursion
928     (let ((inhibit-point-motion-hooks t)
929           buffer-read-only)
930       (goto-char (point-min))
931       (when (search-forward "\n\n" nil t)
932         (while (and (not (eobp))
933                     (looking-at "[ \t]*$"))
934           (gnus-delete-line))))))
935
936 (defun gnus-article-strip-multiple-blank-lines ()
937   "Replace consecutive blank lines with one empty line."
938   (interactive)
939   (save-excursion
940     (let (buffer-read-only)
941       ;; First make all blank lines empty.
942       (goto-char (point-min))
943       (while (re-search-forward "^[ \t]+$" nil t)
944         (replace-match "" nil t))
945       ;; Then replace multiple empty lines with a single empty line.
946       (goto-char (point-min))
947       (while (re-search-forward "\n\n\n+" nil t)
948         (replace-match "\n\n" t t)))))
949
950 (defun gnus-article-strip-blank-lines ()
951   "Strip leading, trailing and multiple blank lines."
952   (interactive)
953   (gnus-article-strip-leading-blank-lines)
954   (gnus-article-remove-trailing-blank-lines)
955   (gnus-article-strip-multiple-blank-lines))
956
957 (defvar mime::preview/content-list)
958 (defvar mime::preview-content-info/point-min)
959 (defun gnus-article-narrow-to-signature ()
960   "Narrow to the signature; return t if a signature is found, else nil."
961   (widen)
962   (when (and (boundp 'mime::preview/content-list)
963              mime::preview/content-list)
964     ;; We have a MIMEish article, so we use the MIME data to narrow.
965     (let ((pcinfo (car (last mime::preview/content-list))))
966       (ignore-errors
967         (narrow-to-region
968          (funcall (intern "mime::preview-content-info/point-min") pcinfo)
969          (point-max)))))
970   
971   (when (gnus-article-search-signature)
972     (forward-line 1)
973     ;; Check whether we have some limits to what we consider
974     ;; to be a signature.
975     (let ((limits (if (listp gnus-signature-limit) gnus-signature-limit
976                     (list gnus-signature-limit)))
977           limit limited)
978       (while (setq limit (pop limits))
979         (if (or (and (integerp limit)
980                      (< (- (point-max) (point)) limit))
981                 (and (floatp limit)
982                      (< (count-lines (point) (point-max)) limit))
983                 (and (gnus-functionp limit)
984                      (funcall limit))
985                 (and (stringp limit)
986                      (not (re-search-forward limit nil t))))
987             ()                          ; This limit did not succeed.
988           (setq limited t
989                 limits nil)))
990       (unless limited
991         (narrow-to-region (point) (point-max))
992         t))))
993
994 (defun gnus-article-search-signature ()
995   "Search the current buffer for the signature separator.
996 Put point at the beginning of the signature separator."
997   (let ((cur (point)))
998     (goto-char (point-max))
999     (if (if (stringp gnus-signature-separator)
1000             (re-search-backward gnus-signature-separator nil t)
1001           (let ((seps gnus-signature-separator))
1002             (while (and seps
1003                         (not (re-search-backward (car seps) nil t)))
1004               (pop seps))
1005             seps))
1006         t
1007       (goto-char cur)
1008       nil)))
1009
1010 (defun gnus-article-hidden-arg ()
1011   "Return the current prefix arg as a number, or 0 if no prefix."
1012   (list (if current-prefix-arg
1013             (prefix-numeric-value current-prefix-arg)
1014           0)))
1015
1016 (defun gnus-article-check-hidden-text (type arg)
1017   "Return nil if hiding is necessary.
1018 Arg can be nil or a number.  Nil and positive means hide, negative
1019 means show, 0 means toggle."
1020   (save-excursion
1021     (let ((hide (gnus-article-hidden-text-p type)))
1022       (cond
1023        ((or (null arg)
1024             (> arg 0))
1025         nil)
1026        ((< arg 0)
1027         (gnus-article-show-hidden-text type))
1028        (t
1029         (if (eq hide 'hidden)
1030             (gnus-article-show-hidden-text type)
1031           nil))))))
1032
1033 (defun gnus-article-hidden-text-p (type)
1034   "Say whether the current buffer contains hidden text of type TYPE."
1035   (let ((pos (text-property-any (point-min) (point-max) 'article-type type)))
1036     (when pos
1037       (if (get-text-property pos 'invisible)
1038           'hidden
1039         'shown))))
1040
1041 (defun gnus-article-show-hidden-text (type &optional hide)
1042   "Show all hidden text of type TYPE.
1043 If HIDE, hide the text instead."
1044   (save-excursion
1045     (let ((buffer-read-only nil)
1046           (inhibit-point-motion-hooks t)
1047           (beg (point-min)))
1048       (while (gnus-goto-char (text-property-any
1049                               beg (point-max) 'article-type type))
1050         (setq beg (point))
1051         (forward-char)
1052         (if hide
1053             (gnus-article-hide-text beg (point) gnus-hidden-properties)
1054           (gnus-article-unhide-text beg (point)))
1055         (setq beg (point)))
1056       t)))
1057
1058 (defconst article-time-units
1059   `((year . ,(* 365.25 24 60 60))
1060     (week . ,(* 7 24 60 60))
1061     (day . ,(* 24 60 60))
1062     (hour . ,(* 60 60))
1063     (minute . 60)
1064     (second . 1))
1065   "Mapping from time units to seconds.")
1066
1067 (defun gnus-article-date-ut (&optional type highlight header)
1068   "Convert DATE date to universal time in the current article.
1069 If TYPE is `local', convert to local time; if it is `lapsed', output
1070 how much time has lapsed since DATE."
1071   (interactive (list 'ut t))
1072   (let* ((header (or header 
1073                      (mail-header-date gnus-current-headers)
1074                      (message-fetch-field "date")
1075                      ""))
1076          (date (if (vectorp header) (mail-header-date header)
1077                  header))
1078          (date-regexp "^Date:[ \t]\\|^X-Sent:[ \t]")
1079          (inhibit-point-motion-hooks t)
1080          bface eface)
1081     (when (and date (not (string= date "")))
1082       (save-excursion
1083         (save-restriction
1084           (nnheader-narrow-to-headers)
1085           (let ((buffer-read-only nil))
1086             ;; Delete any old Date headers.
1087             (if (re-search-forward date-regexp nil t)
1088                 (progn
1089                   (setq bface (get-text-property (gnus-point-at-bol) 'face)
1090                         eface (get-text-property (1- (gnus-point-at-eol))
1091                                                  'face))
1092                   (message-remove-header date-regexp t)
1093                   (beginning-of-line))
1094               (goto-char (point-max)))
1095             (insert (gnus-article-make-date-line date type))
1096             ;; Do highlighting.
1097             (forward-line -1)
1098             (when (looking-at "\\([^:]+\\): *\\(.*\\)$")
1099               (put-text-property (match-beginning 1) (match-end 1)
1100                                  'face bface)
1101               (put-text-property (match-beginning 2) (match-end 2)
1102                                  'face eface))))))))
1103
1104 (defun gnus-article-make-date-line (date type)
1105   "Return a DATE line of TYPE."
1106   (cond
1107    ;; Convert to the local timezone.  We have to slap a
1108    ;; `condition-case' round the calls to the timezone
1109    ;; functions since they aren't particularly resistant to
1110    ;; buggy dates.
1111    ((eq type 'local)
1112     (concat "Date: " (condition-case ()
1113                          (timezone-make-date-arpa-standard date)
1114                        (error date))
1115             "\n"))
1116    ;; Convert to Universal Time.
1117    ((eq type 'ut)
1118     (concat "Date: "
1119             (condition-case ()
1120                 (timezone-make-date-arpa-standard date nil "UT")
1121               (error date))
1122             "\n"))
1123    ;; Get the original date from the article.
1124    ((eq type 'original)
1125     (concat "Date: " date "\n"))
1126    ;; Do an X-Sent lapsed format.
1127    ((eq type 'lapsed)
1128     ;; If the date is seriously mangled, the timezone functions are
1129     ;; liable to bug out, so we ignore all errors.
1130     (let* ((now (current-time))
1131            (real-time
1132             (ignore-errors
1133               (gnus-time-minus
1134                (gnus-encode-date
1135                 (timezone-make-date-arpa-standard
1136                  (current-time-string now)
1137                  (current-time-zone now) "UT"))
1138                (gnus-encode-date
1139                 (timezone-make-date-arpa-standard
1140                  date nil "UT")))))
1141            (real-sec (and real-time
1142                           (+ (* (float (car real-time)) 65536)
1143                              (cadr real-time))))
1144            (sec (and real-time (abs real-sec)))
1145            num prev)
1146       (cond
1147        ((null real-time)
1148         "X-Sent: Unknown\n")
1149        ((zerop sec)
1150         "X-Sent: Now\n")
1151        (t
1152         (concat
1153          "X-Sent: "
1154          ;; This is a bit convoluted, but basically we go
1155          ;; through the time units for years, weeks, etc,
1156          ;; and divide things to see whether that results
1157          ;; in positive answers.
1158          (mapconcat
1159           (lambda (unit)
1160             (if (zerop (setq num (ffloor (/ sec (cdr unit)))))
1161                 ;; The (remaining) seconds are too few to
1162                 ;; be divided into this time unit.
1163                 ""
1164               ;; It's big enough, so we output it.
1165               (setq sec (- sec (* num (cdr unit))))
1166               (prog1
1167                   (concat (if prev ", " "") (int-to-string
1168                                              (floor num))
1169                           " " (symbol-name (car unit)) 
1170                           (if (> num 1) "s" ""))
1171                 (setq prev t))))
1172           article-time-units "")
1173          ;; If dates are odd, then it might appear like the
1174          ;; article was sent in the future.
1175          (if (> real-sec 0)
1176              " ago\n"
1177            " in the future\n"))))))
1178    (t
1179     (error "Unknown conversion type: %s" type))))
1180
1181 (defun gnus-article-date-local (&optional highlight)
1182   "Convert the current article date to the local timezone."
1183   (interactive (list t))
1184   (gnus-article-date-ut 'local highlight))
1185
1186 (defun gnus-article-date-original (&optional highlight)
1187   "Convert the current article date to what it was originally.
1188 This is only useful if you have used some other date conversion
1189 function and want to see what the date was before converting."
1190   (interactive (list t))
1191   (gnus-article-date-ut 'original highlight))
1192
1193 (defun gnus-article-date-lapsed (&optional highlight)
1194   "Convert the current article date to time lapsed since it was sent."
1195   (interactive (list t))
1196   (gnus-article-date-ut 'lapsed highlight))
1197
1198 (defun gnus-article-show-all ()
1199   "Show all hidden text in the article buffer."
1200   (interactive)
1201   (save-excursion
1202     (let ((buffer-read-only nil))
1203       (gnus-article-unhide-text (point-min) (point-max)))))
1204
1205 (defun gnus-article-emphasize (&optional arg)
1206   "Emphasize text according to `gnus-emphasis-alist'."
1207   (interactive (gnus-article-hidden-arg))
1208   (unless (gnus-article-check-hidden-text 'emphasis arg)
1209     (save-excursion
1210       (let ((alist gnus-emphasis-alist)
1211             (buffer-read-only nil)
1212             (props (append '(gnus-article-type emphasis)
1213                            gnus-hidden-properties))
1214             regexp elem beg invisible visible face)
1215         (goto-char (point-min))
1216         (search-forward "\n\n" nil t)
1217         (setq beg (point))
1218         (while (setq elem (pop alist))
1219           (goto-char beg)
1220           (setq regexp (car elem)
1221                 invisible (nth 1 elem)
1222                 visible (nth 2 elem)
1223                 face (nth 3 elem))
1224           (while (re-search-forward regexp nil t)
1225             (when (and (match-beginning visible) (match-beginning invisible))
1226               (gnus-article-hide-text
1227                (match-beginning invisible) (match-end invisible) props)
1228               (gnus-article-unhide-text-type
1229                (match-beginning visible) (match-end visible) 'emphasis)
1230               (gnus-put-text-property-excluding-newlines
1231                (match-beginning visible) (match-end visible) 'face face)
1232               (goto-char (match-end invisible)))))))))
1233
1234 (defvar gnus-summary-article-menu)
1235 (defvar gnus-summary-post-menu)
1236
1237 ;;; Saving functions.
1238
1239 (defun gnus-article-save (save-buffer file &optional num)
1240   "Save the currently selected article."
1241   (unless gnus-save-all-headers
1242     ;; Remove headers according to `gnus-saved-headers'.
1243     (let ((gnus-visible-headers
1244            (or gnus-saved-headers gnus-visible-headers))
1245           (gnus-article-buffer save-buffer))
1246       (gnus-article-hide-headers 1 t)))
1247   (save-window-excursion
1248     (if (not gnus-default-article-saver)
1249         (error "No default saver is defined.")
1250       ;; !!! Magic!  The saving functions all save
1251       ;; `gnus-original-article-buffer' (or so they think), but we
1252       ;; bind that variable to our save-buffer.
1253       (set-buffer gnus-article-buffer)
1254       (let* ((gnus-save-article-buffer save-buffer)
1255              (filename
1256               (cond
1257                ((not gnus-prompt-before-saving) 'default)
1258                ((eq gnus-prompt-before-saving 'always) nil)
1259                (t file)))
1260              (gnus-number-of-articles-to-be-saved
1261               (when (eq gnus-prompt-before-saving t)
1262                 num)))                  ; Magic
1263         (set-buffer gnus-summary-buffer)
1264         (funcall gnus-default-article-saver filename))))) 
1265
1266 (defun gnus-read-save-file-name (prompt default-name &optional filename)
1267   (cond
1268    ((eq filename 'default)
1269     default-name)
1270    (filename filename)
1271    (t
1272     (let* ((split-name (gnus-get-split-value gnus-split-methods))
1273            (prompt
1274             (format prompt (if (and gnus-number-of-articles-to-be-saved
1275                                     (> gnus-number-of-articles-to-be-saved 1))
1276                                (format "these %d articles"
1277                                        gnus-number-of-articles-to-be-saved)
1278                              "this article")))
1279            (file
1280             ;; Let the split methods have their say.
1281             (cond
1282              ;; No split name was found.
1283              ((null split-name)
1284               (read-file-name
1285                (concat prompt " (default "
1286                        (file-name-nondirectory default-name) ") ")
1287                (file-name-directory default-name)
1288                default-name))
1289              ;; A single split name was found
1290              ((= 1 (length split-name))
1291               (let* ((name (car split-name))
1292                      (dir (cond ((file-directory-p name)
1293                                  (file-name-as-directory name))
1294                                 ((file-exists-p name) name)
1295                                 (t gnus-article-save-directory))))
1296                 (read-file-name
1297                  (concat prompt " (default " name ") ")
1298                  dir name)))
1299              ;; A list of splits was found.
1300              (t
1301               (setq split-name (nreverse split-name))
1302               (let (result)
1303                 (let ((file-name-history (nconc split-name file-name-history)))
1304                   (setq result
1305                         (expand-file-name
1306                          (read-file-name
1307                           (concat prompt " (`M-p' for defaults) ")
1308                           gnus-article-save-directory
1309                           (car split-name))
1310                          gnus-article-save-directory)))
1311                 (car (push result file-name-history)))))))
1312       ;; Create the directory.
1313       (gnus-make-directory (file-name-directory file))
1314       ;; If we have read a directory, we append the default file name.
1315       (when (file-directory-p file)
1316         (setq file (concat (file-name-as-directory file)
1317                            (file-name-nondirectory default-name))))
1318       ;; Possibly translate some characters.
1319       (nnheader-translate-file-chars file)))))
1320
1321 (defun gnus-article-archive-name (group)
1322   "Return the first instance of an \"Archive-name\" in the current buffer."
1323   (let ((case-fold-search t))
1324     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
1325       (nnheader-concat gnus-article-save-directory
1326                        (match-string 1)))))
1327
1328 (defun gnus-summary-save-in-rmail (&optional filename)
1329   "Append this article to Rmail file.
1330 Optional argument FILENAME specifies file name.
1331 Directory to save to is default to `gnus-article-save-directory'."
1332   (interactive)
1333   (gnus-set-global-variables)
1334   (let ((default-name
1335           (funcall gnus-rmail-save-name gnus-newsgroup-name
1336                    gnus-current-headers gnus-newsgroup-last-rmail)))
1337     (setq filename (gnus-read-save-file-name
1338                     "Save %s in rmail file:" default-name filename))
1339     (gnus-make-directory (file-name-directory filename))
1340     (gnus-eval-in-buffer-window gnus-save-article-buffer
1341       (save-excursion
1342         (save-restriction
1343           (widen)
1344           (gnus-output-to-rmail filename))))
1345     ;; Remember the directory name to save articles
1346     (setq gnus-newsgroup-last-rmail filename)))
1347
1348 (defun gnus-summary-save-in-mail (&optional filename)
1349   "Append this article to Unix mail file.
1350 Optional argument FILENAME specifies file name.
1351 Directory to save to is default to `gnus-article-save-directory'."
1352   (interactive)
1353   (gnus-set-global-variables)
1354   (let ((default-name
1355           (funcall gnus-mail-save-name gnus-newsgroup-name
1356                    gnus-current-headers gnus-newsgroup-last-mail)))
1357     (setq filename (gnus-read-save-file-name
1358                     "Save %s in Unix mail file:" default-name filename))
1359     (setq filename
1360           (expand-file-name filename
1361                             (and default-name
1362                                  (file-name-directory default-name))))
1363     (gnus-make-directory (file-name-directory filename))
1364     (gnus-eval-in-buffer-window gnus-save-article-buffer
1365       (save-excursion
1366         (save-restriction
1367           (widen)
1368           (if (and (file-readable-p filename) (mail-file-babyl-p filename))
1369               (gnus-output-to-rmail filename)
1370             (let ((mail-use-rfc822 t))
1371               (rmail-output filename 1 t t))))))
1372     ;; Remember the directory name to save articles.
1373     (setq gnus-newsgroup-last-mail filename)))
1374
1375 (defun gnus-summary-save-in-file (&optional filename overwrite)
1376   "Append this article to file.
1377 Optional argument FILENAME specifies file name.
1378 Directory to save to is default to `gnus-article-save-directory'."
1379   (interactive)
1380   (gnus-set-global-variables)
1381   (let ((default-name
1382           (funcall gnus-file-save-name gnus-newsgroup-name
1383                    gnus-current-headers gnus-newsgroup-last-file)))
1384     (setq filename (gnus-read-save-file-name
1385                     "Save %s in file:" default-name filename))
1386     (gnus-make-directory (file-name-directory filename))
1387     (gnus-eval-in-buffer-window gnus-save-article-buffer
1388       (save-excursion
1389         (save-restriction
1390           (widen)
1391           (when (and overwrite
1392                      (file-exists-p filename))
1393             (delete-file filename))
1394           (gnus-output-to-file filename))))
1395     ;; Remember the directory name to save articles.
1396     (setq gnus-newsgroup-last-file filename)))
1397
1398 (defun gnus-summary-write-to-file (&optional filename)
1399   "Write this article to a file.
1400 Optional argument FILENAME specifies file name.
1401 The directory to save in defaults to `gnus-article-save-directory'."
1402   (interactive)
1403   (gnus-summary-save-in-file nil t))
1404
1405 (defun gnus-summary-save-body-in-file (&optional filename)
1406   "Append this article body to a file.
1407 Optional argument FILENAME specifies file name.
1408 The directory to save in defaults to `gnus-article-save-directory'."
1409   (interactive)
1410   (gnus-set-global-variables)
1411   (let ((default-name
1412           (funcall gnus-file-save-name gnus-newsgroup-name
1413                    gnus-current-headers gnus-newsgroup-last-file)))
1414     (setq filename (gnus-read-save-file-name
1415                     "Save %s body in file:" default-name filename))
1416     (gnus-make-directory (file-name-directory filename))
1417     (gnus-eval-in-buffer-window gnus-save-article-buffer
1418       (save-excursion
1419         (save-restriction
1420           (widen)
1421           (goto-char (point-min))
1422           (when (search-forward "\n\n" nil t)
1423             (narrow-to-region (point) (point-max)))
1424           (gnus-output-to-file filename))))
1425     ;; Remember the directory name to save articles.
1426     (setq gnus-newsgroup-last-file filename)))
1427
1428 (defun gnus-summary-save-in-pipe (&optional command)
1429   "Pipe this article to subprocess."
1430   (interactive)
1431   (gnus-set-global-variables)
1432   (setq command
1433         (cond ((eq command 'default)
1434                gnus-last-shell-command)
1435               (command command)
1436               (t (read-string 
1437                   (format
1438                    "Shell command on %s: "
1439                    (if (and gnus-number-of-articles-to-be-saved
1440                             (> gnus-number-of-articles-to-be-saved 1))
1441                        (format "these %d articles"
1442                                gnus-number-of-articles-to-be-saved)
1443                      "this article"))
1444                   gnus-last-shell-command))))
1445   (when (string-equal command "")
1446     (setq command gnus-last-shell-command))
1447   (gnus-eval-in-buffer-window gnus-article-buffer
1448     (save-restriction
1449       (widen)
1450       (shell-command-on-region (point-min) (point-max) command nil)))
1451   (setq gnus-last-shell-command command))
1452
1453 ;;; Article file names when saving.
1454
1455 (defun gnus-capitalize-newsgroup (newsgroup)
1456   "Capitalize NEWSGROUP name."
1457   (when (not (zerop (length newsgroup)))
1458     (concat (char-to-string (upcase (aref newsgroup 0)))
1459             (substring newsgroup 1))))
1460
1461 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
1462   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1463 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
1464 Otherwise, it is like ~/News/news/group/num."
1465   (let ((default
1466           (expand-file-name
1467            (concat (if (gnus-use-long-file-name 'not-save)
1468                        (gnus-capitalize-newsgroup newsgroup)
1469                      (gnus-newsgroup-directory-form newsgroup))
1470                    "/" (int-to-string (mail-header-number headers)))
1471            gnus-article-save-directory)))
1472     (if (and last-file
1473              (string-equal (file-name-directory default)
1474                            (file-name-directory last-file))
1475              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1476         default
1477       (or last-file default))))
1478
1479 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
1480   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1481 If variable `gnus-use-long-file-name' is non-nil, it is
1482 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
1483   (let ((default
1484           (expand-file-name
1485            (concat (if (gnus-use-long-file-name 'not-save)
1486                        newsgroup
1487                      (gnus-newsgroup-directory-form newsgroup))
1488                    "/" (int-to-string (mail-header-number headers)))
1489            gnus-article-save-directory)))
1490     (if (and last-file
1491              (string-equal (file-name-directory default)
1492                            (file-name-directory last-file))
1493              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
1494         default
1495       (or last-file default))))
1496
1497 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
1498   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1499 If variable `gnus-use-long-file-name' is non-nil, it is
1500 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
1501   (or last-file
1502       (expand-file-name
1503        (if (gnus-use-long-file-name 'not-save)
1504            (gnus-capitalize-newsgroup newsgroup)
1505          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1506        gnus-article-save-directory)))
1507
1508 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
1509   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
1510 If variable `gnus-use-long-file-name' is non-nil, it is
1511 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
1512   (or last-file
1513       (expand-file-name
1514        (if (gnus-use-long-file-name 'not-save)
1515            newsgroup
1516          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
1517        gnus-article-save-directory)))
1518
1519 \f
1520 ;;;
1521 ;;; Gnus article mode
1522 ;;;
1523
1524 (put 'gnus-article-mode 'mode-class 'special)
1525
1526 (when t
1527   (gnus-define-keys gnus-article-mode-map
1528     " " gnus-article-goto-next-page
1529     "\177" gnus-article-goto-prev-page
1530     [delete] gnus-article-goto-prev-page
1531     "\C-c^" gnus-article-refer-article
1532     "h" gnus-article-show-summary
1533     "s" gnus-article-show-summary
1534     "\C-c\C-m" gnus-article-mail
1535     "?" gnus-article-describe-briefly
1536     gnus-mouse-2 gnus-article-push-button
1537     "\r" gnus-article-press-button
1538     "\t" gnus-article-next-button
1539     "\M-\t" gnus-article-prev-button
1540     "e" gnus-article-edit
1541     "<" beginning-of-buffer
1542     ">" end-of-buffer
1543     "\C-c\C-i" gnus-info-find-node
1544     "\C-c\C-b" gnus-bug
1545
1546     "\C-d" gnus-article-read-summary-keys
1547     "\M-*" gnus-article-read-summary-keys
1548     "\M-g" gnus-article-read-summary-keys)
1549
1550   (substitute-key-definition
1551    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
1552
1553 (defun gnus-article-make-menu-bar ()
1554   (gnus-turn-off-edit-menu 'article)
1555   (unless (boundp 'gnus-article-article-menu)
1556     (easy-menu-define
1557      gnus-article-article-menu gnus-article-mode-map ""
1558      '("Article"
1559        ["Scroll forwards" gnus-article-goto-next-page t]
1560        ["Scroll backwards" gnus-article-goto-prev-page t]
1561        ["Show summary" gnus-article-show-summary t]
1562        ["Fetch Message-ID at point" gnus-article-refer-article t]
1563        ["Mail to address at point" gnus-article-mail t]))
1564
1565     (easy-menu-define
1566      gnus-article-treatment-menu gnus-article-mode-map ""
1567      '("Treatment"
1568        ["Hide headers" gnus-article-hide-headers t]
1569        ["Hide signature" gnus-article-hide-signature t]
1570        ["Hide citation" gnus-article-hide-citation t]
1571        ["Treat overstrike" gnus-article-treat-overstrike t]
1572        ["Remove carriage return" gnus-article-remove-cr t]
1573        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]))
1574
1575     (when (boundp 'gnus-summary-article-menu)
1576       (define-key gnus-article-mode-map [menu-bar commands]
1577         (cons "Commands" gnus-summary-article-menu)))
1578
1579     (when (boundp 'gnus-summary-post-menu)
1580       (define-key gnus-article-mode-map [menu-bar post]
1581         (cons "Post" gnus-summary-post-menu)))
1582
1583     (run-hooks 'gnus-article-menu-hook)))
1584
1585 (defun gnus-article-mode ()
1586   "Major mode for displaying an article.
1587
1588 All normal editing commands are switched off.
1589
1590 The following commands are available in addition to all summary mode
1591 commands:
1592 \\<gnus-article-mode-map>
1593 \\[gnus-article-next-page]\t Scroll the article one page forwards
1594 \\[gnus-article-prev-page]\t Scroll the article one page backwards
1595 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
1596 \\[gnus-article-show-summary]\t Display the summary buffer
1597 \\[gnus-article-mail]\t Send a reply to the address near point
1598 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
1599 \\[gnus-info-find-node]\t Go to the Gnus info node"
1600   (interactive)
1601   (when (gnus-visual-p 'article-menu 'menu)
1602     (gnus-article-make-menu-bar))
1603   (kill-all-local-variables)
1604   (gnus-simplify-mode-line)
1605   (setq mode-name "Article")
1606   (setq major-mode 'gnus-article-mode)
1607   (make-local-variable 'minor-mode-alist)
1608   (unless (assq 'gnus-show-mime minor-mode-alist)
1609     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
1610   (use-local-map gnus-article-mode-map)
1611   (gnus-update-format-specifications nil 'article-mode)
1612   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
1613   (gnus-set-default-directory)
1614   (buffer-disable-undo (current-buffer))
1615   (setq buffer-read-only t)
1616   (set-syntax-table gnus-article-mode-syntax-table)
1617   (run-hooks 'gnus-article-mode-hook))
1618
1619 (defun gnus-article-setup-buffer ()
1620   "Initialize the article buffer."
1621   (let* ((name (if gnus-single-article-buffer "*Article*"
1622                  (concat "*Article " gnus-newsgroup-name "*")))
1623          (original
1624           (progn (string-match "\\*Article" name)
1625                  (concat " *Original Article"
1626                          (substring name (match-end 0))))))
1627     (setq gnus-article-buffer name)
1628     (setq gnus-original-article-buffer original)
1629     ;; This might be a variable local to the summary buffer.
1630     (unless gnus-single-article-buffer
1631       (save-excursion
1632         (set-buffer gnus-summary-buffer)
1633         (setq gnus-article-buffer name)
1634         (setq gnus-original-article-buffer original)
1635         (gnus-set-global-variables)))
1636     ;; Init original article buffer.
1637     (save-excursion
1638       (set-buffer (get-buffer-create gnus-original-article-buffer))
1639       (buffer-disable-undo (current-buffer))
1640       (setq major-mode 'gnus-original-article-mode)
1641       (gnus-add-current-to-buffer-list)
1642       (make-local-variable 'gnus-original-article))
1643     (if (get-buffer name)
1644         (save-excursion
1645           (set-buffer name)
1646           (buffer-disable-undo (current-buffer))
1647           (setq buffer-read-only t)
1648           (gnus-add-current-to-buffer-list)
1649           (unless (eq major-mode 'gnus-article-mode)
1650             (gnus-article-mode))
1651           (current-buffer))
1652       (save-excursion
1653         (set-buffer (get-buffer-create name))
1654         (gnus-add-current-to-buffer-list)
1655         (gnus-article-mode)
1656         (make-local-variable 'gnus-summary-buffer)
1657         (current-buffer)))))
1658
1659 ;; Set article window start at LINE, where LINE is the number of lines
1660 ;; from the head of the article.
1661 (defun gnus-article-set-window-start (&optional line)
1662   (set-window-start
1663    (get-buffer-window gnus-article-buffer t)
1664    (save-excursion
1665      (set-buffer gnus-article-buffer)
1666      (goto-char (point-min))
1667      (if (not line)
1668          (point-min)
1669        (gnus-message 6 "Moved to bookmark")
1670        (search-forward "\n\n" nil t)
1671        (forward-line line)
1672        (point)))))
1673
1674 (defun gnus-article-prepare (article &optional all-headers header)
1675   "Prepare ARTICLE in article mode buffer.
1676 ARTICLE should either be an article number or a Message-ID.
1677 If ARTICLE is an id, HEADER should be the article headers.
1678 If ALL-HEADERS is non-nil, no headers are hidden."
1679   (save-excursion
1680     ;; Make sure we start in a summary buffer.
1681     (unless (eq major-mode 'gnus-summary-mode)
1682       (set-buffer gnus-summary-buffer))
1683     (setq gnus-summary-buffer (current-buffer))
1684     ;; Make sure the connection to the server is alive.
1685     (unless (gnus-server-opened
1686              (gnus-find-method-for-group gnus-newsgroup-name))
1687       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
1688       (gnus-request-group gnus-newsgroup-name t))
1689     (let* ((gnus-article (if header (mail-header-number header) article))
1690            (summary-buffer (current-buffer))
1691            (internal-hook gnus-article-internal-prepare-hook)
1692            (group gnus-newsgroup-name)
1693            result)
1694       (save-excursion
1695         (gnus-article-setup-buffer)
1696         (set-buffer gnus-article-buffer)
1697         ;; Deactivate active regions.
1698         (when (and (boundp 'transient-mark-mode)
1699                    transient-mark-mode)
1700           (setq mark-active nil))
1701         (if (not (setq result (let ((buffer-read-only nil))
1702                                 (gnus-request-article-this-buffer
1703                                  article group))))
1704             ;; There is no such article.
1705             (save-excursion
1706               (when (and (numberp article)
1707                          (not (memq article gnus-newsgroup-sparse)))
1708                 (setq gnus-article-current
1709                       (cons gnus-newsgroup-name article))
1710                 (set-buffer gnus-summary-buffer)
1711                 (setq gnus-current-article article)
1712                 (gnus-summary-mark-article article gnus-canceled-mark))
1713               (unless (memq article gnus-newsgroup-sparse)
1714                 (gnus-error
1715                  1 "No such article (may have expired or been canceled)")))
1716           (if (or (eq result 'pseudo) (eq result 'nneething))
1717               (progn
1718                 (save-excursion
1719                   (set-buffer summary-buffer)
1720                   (setq gnus-last-article gnus-current-article
1721                         gnus-newsgroup-history (cons gnus-current-article
1722                                                      gnus-newsgroup-history)
1723                         gnus-current-article 0
1724                         gnus-current-headers nil
1725                         gnus-article-current nil)
1726                   (if (eq result 'nneething)
1727                       (gnus-configure-windows 'summary)
1728                     (gnus-configure-windows 'article))
1729                   (gnus-set-global-variables))
1730                 (gnus-set-mode-line 'article))
1731             ;; The result from the `request' was an actual article -
1732             ;; or at least some text that is now displayed in the
1733             ;; article buffer.
1734             (when (and (numberp article)
1735                        (not (eq article gnus-current-article)))
1736               ;; Seems like a new article has been selected.
1737               ;; `gnus-current-article' must be an article number.
1738               (save-excursion
1739                 (set-buffer summary-buffer)
1740                 (setq gnus-last-article gnus-current-article
1741                       gnus-newsgroup-history (cons gnus-current-article
1742                                                    gnus-newsgroup-history)
1743                       gnus-current-article article
1744                       gnus-current-headers
1745                       (gnus-summary-article-header gnus-current-article)
1746                       gnus-article-current
1747                       (cons gnus-newsgroup-name gnus-current-article))
1748                 (unless (vectorp gnus-current-headers)
1749                   (setq gnus-current-headers nil))
1750                 (gnus-summary-show-thread)
1751                 (run-hooks 'gnus-mark-article-hook)
1752                 (gnus-set-mode-line 'summary)
1753                 (when (gnus-visual-p 'article-highlight 'highlight)
1754                   (run-hooks 'gnus-visual-mark-article-hook))
1755                 ;; Set the global newsgroup variables here.
1756                 ;; Suggested by Jim Sisolak
1757                 ;; <sisolak@trans4.neep.wisc.edu>.
1758                 (gnus-set-global-variables)
1759                 (setq gnus-have-all-headers
1760                       (or all-headers gnus-show-all-headers))
1761                 (and gnus-use-cache
1762                      (vectorp (gnus-summary-article-header article))
1763                      (gnus-cache-possibly-enter-article
1764                       group article
1765                       (gnus-summary-article-header article)
1766                       (memq article gnus-newsgroup-marked)
1767                       (memq article gnus-newsgroup-dormant)
1768                       (memq article gnus-newsgroup-unreads)))))
1769             (when (or (numberp article)
1770                       (stringp article))
1771               ;; Hooks for getting information from the article.
1772               ;; This hook must be called before being narrowed.
1773               (let (buffer-read-only)
1774                 (run-hooks 'internal-hook)
1775                 (run-hooks 'gnus-article-prepare-hook)
1776                 ;; Decode MIME message.
1777                 (when gnus-show-mime
1778                   (if (or (not gnus-strict-mime)
1779                           (gnus-fetch-field "Mime-Version"))
1780                       (funcall gnus-show-mime-method)
1781                     (funcall gnus-decode-encoded-word-method)))
1782                 ;; Perform the article display hooks.
1783                 (run-hooks 'gnus-article-display-hook))
1784               ;; Do page break.
1785               (goto-char (point-min))
1786               (when gnus-break-pages
1787                 (gnus-narrow-to-page)))
1788             (gnus-set-mode-line 'article)
1789             (gnus-configure-windows 'article)
1790             (goto-char (point-min))
1791             t))))))
1792
1793 (defun gnus-article-wash-status ()
1794   "Return a string which display status of article washing."
1795   (save-excursion
1796     (set-buffer gnus-article-buffer)
1797     (let ((cite (gnus-article-hidden-text-p 'cite))
1798           (headers (gnus-article-hidden-text-p 'headers))
1799           (boring (gnus-article-hidden-text-p 'boring-headers))
1800           (pgp (gnus-article-hidden-text-p 'pgp))
1801           (pem (gnus-article-hidden-text-p 'pem))
1802           (signature (gnus-article-hidden-text-p 'signature))
1803           (overstrike (gnus-article-hidden-text-p 'overstrike))
1804           (emphasis (gnus-article-hidden-text-p 'emphasis))
1805           (mime gnus-show-mime))
1806       (format "%c%c%c%c%c%c%c"
1807               (if cite ?c ? )
1808               (if (or headers boring) ?h ? )
1809               (if (or pgp pem) ?p ? )
1810               (if signature ?s ? )
1811               (if overstrike ?o ? )
1812               (if mime ?m ? )
1813               (if emphasis ?e ? )))))
1814
1815 (defun gnus-article-hide-headers-if-wanted ()
1816   "Hide unwanted headers if `gnus-have-all-headers' is nil.
1817 Provided for backwards compatibility."
1818   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
1819       gnus-inhibit-hiding
1820       (gnus-article-hide-headers)))
1821
1822 ;;; Article savers.
1823
1824 (defun gnus-output-to-rmail (file-name)
1825   "Append the current article to an Rmail file named FILE-NAME."
1826   (require 'rmail)
1827   ;; Most of these codes are borrowed from rmailout.el.
1828   (setq file-name (expand-file-name file-name))
1829   (setq rmail-default-rmail-file file-name)
1830   (let ((artbuf (current-buffer))
1831         (tmpbuf (get-buffer-create " *Gnus-output*")))
1832     (save-excursion
1833       (or (get-file-buffer file-name)
1834           (file-exists-p file-name)
1835           (if (gnus-yes-or-no-p
1836                (concat "\"" file-name "\" does not exist, create it? "))
1837               (let ((file-buffer (create-file-buffer file-name)))
1838                 (save-excursion
1839                   (set-buffer file-buffer)
1840                   (rmail-insert-rmail-file-header)
1841                   (let ((require-final-newline nil))
1842                     (gnus-write-buffer file-name)))
1843                 (kill-buffer file-buffer))
1844             (error "Output file does not exist")))
1845       (set-buffer tmpbuf)
1846       (buffer-disable-undo (current-buffer))
1847       (erase-buffer)
1848       (insert-buffer-substring artbuf)
1849       (gnus-convert-article-to-rmail)
1850       ;; Decide whether to append to a file or to an Emacs buffer.
1851       (let ((outbuf (get-file-buffer file-name)))
1852         (if (not outbuf)
1853             (append-to-file (point-min) (point-max) file-name)
1854           ;; File has been visited, in buffer OUTBUF.
1855           (set-buffer outbuf)
1856           (let ((buffer-read-only nil)
1857                 (msg (and (boundp 'rmail-current-message)
1858                           (symbol-value 'rmail-current-message))))
1859             ;; If MSG is non-nil, buffer is in RMAIL mode.
1860             (when msg
1861               (widen)
1862               (narrow-to-region (point-max) (point-max)))
1863             (insert-buffer-substring tmpbuf)
1864             (when msg
1865               (goto-char (point-min))
1866               (widen)
1867               (search-backward "\^_")
1868               (narrow-to-region (point) (point-max))
1869               (goto-char (1+ (point-min)))
1870               (rmail-count-new-messages t)
1871               (rmail-show-message msg))))))
1872     (kill-buffer tmpbuf)))
1873
1874 (defun gnus-output-to-file (file-name)
1875   "Append the current article to a file named FILE-NAME."
1876   (let ((artbuf (current-buffer)))
1877     (nnheader-temp-write nil
1878       (insert-buffer-substring artbuf)
1879       ;; Append newline at end of the buffer as separator, and then
1880       ;; save it to file.
1881       (goto-char (point-max))
1882       (insert "\n")
1883       (append-to-file (point-min) (point-max) file-name))))
1884
1885 (defun gnus-convert-article-to-rmail ()
1886   "Convert article in current buffer to Rmail message format."
1887   (let ((buffer-read-only nil))
1888     ;; Convert article directly into Babyl format.
1889     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
1890     (goto-char (point-min))
1891     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
1892     (while (search-forward "\n\^_" nil t) ;single char
1893       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
1894     (goto-char (point-max))
1895     (insert "\^_")))
1896
1897 (defun gnus-narrow-to-page (&optional arg)
1898   "Narrow the article buffer to a page.
1899 If given a numerical ARG, move forward ARG pages."
1900   (interactive "P")
1901   (setq arg (if arg (prefix-numeric-value arg) 0))
1902   (save-excursion
1903     (set-buffer gnus-article-buffer)
1904     (goto-char (point-min))
1905     (widen)
1906     ;; Remove any old next/prev buttons.
1907     (when (gnus-visual-p 'page-marker)
1908       (let ((buffer-read-only nil))
1909         (gnus-remove-text-with-property 'gnus-prev)
1910         (gnus-remove-text-with-property 'gnus-next)))
1911     (when
1912         (cond ((< arg 0)
1913                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
1914               ((> arg 0)
1915                (re-search-forward page-delimiter nil 'move arg)))
1916       (goto-char (match-end 0)))
1917     (narrow-to-region
1918      (point)
1919      (if (re-search-forward page-delimiter nil 'move)
1920          (match-beginning 0)
1921        (point)))
1922     (when (and (gnus-visual-p 'page-marker)
1923                (not (= (point-min) 1)))
1924       (save-excursion
1925         (goto-char (point-min))
1926         (gnus-insert-prev-page-button)))
1927     (when (and (gnus-visual-p 'page-marker)
1928                (< (+ (point-max) 2) (buffer-size)))
1929       (save-excursion
1930         (goto-char (point-max))
1931         (gnus-insert-next-page-button)))))
1932
1933 ;; Article mode commands
1934
1935 (defun gnus-article-goto-next-page ()
1936   "Show the next page of the article."
1937   (interactive)
1938   (when (gnus-article-next-page)
1939     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
1940
1941 (defun gnus-article-goto-prev-page ()
1942   "Show the next page of the article."
1943   (interactive)
1944   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
1945     (gnus-article-prev-page nil)))
1946
1947 (defun gnus-article-next-page (&optional lines)
1948   "Show the next page of the current article.
1949 If end of article, return non-nil.  Otherwise return nil.
1950 Argument LINES specifies lines to be scrolled up."
1951   (interactive "p")
1952   (move-to-window-line -1)
1953   (if (save-excursion
1954         (end-of-line)
1955         (and (pos-visible-in-window-p)  ;Not continuation line.
1956              (eobp)))
1957       ;; Nothing in this page.
1958       (if (or (not gnus-break-pages)
1959               (save-excursion
1960                 (save-restriction
1961                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
1962           t                             ;Nothing more.
1963         (gnus-narrow-to-page 1)         ;Go to next page.
1964         nil)
1965     ;; More in this page.
1966     (let ((scroll-in-place nil))
1967       (condition-case ()
1968           (scroll-up lines)
1969         (end-of-buffer
1970          ;; Long lines may cause an end-of-buffer error.
1971          (goto-char (point-max)))))
1972     (move-to-window-line 0)
1973     nil))
1974
1975 (defun gnus-article-prev-page (&optional lines)
1976   "Show previous page of current article.
1977 Argument LINES specifies lines to be scrolled down."
1978   (interactive "p")
1979   (move-to-window-line 0)
1980   (if (and gnus-break-pages
1981            (bobp)
1982            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
1983       (progn
1984         (gnus-narrow-to-page -1)        ;Go to previous page.
1985         (goto-char (point-max))
1986         (recenter -1))
1987     (let ((scroll-in-place nil))
1988       (prog1
1989           (ignore-errors
1990             (scroll-down lines))
1991         (move-to-window-line 0)))))
1992
1993 (defun gnus-article-refer-article ()
1994   "Read article specified by message-id around point."
1995   (interactive)
1996   (let ((point (point)))
1997     (search-forward ">" nil t)          ;Move point to end of "<....>".
1998     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
1999         (let ((message-id (match-string 1)))
2000           (goto-char point)
2001           (set-buffer gnus-summary-buffer)
2002           (gnus-summary-refer-article message-id))
2003       (goto-char (point))
2004       (error "No references around point"))))
2005
2006 (defun gnus-article-show-summary ()
2007   "Reconfigure windows to show summary buffer."
2008   (interactive)
2009   (gnus-configure-windows 'article)
2010   (gnus-summary-goto-subject gnus-current-article))
2011
2012 (defun gnus-article-describe-briefly ()
2013   "Describe article mode commands briefly."
2014   (interactive)
2015   (gnus-message 6
2016                 (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")))
2017
2018 (defun gnus-article-summary-command ()
2019   "Execute the last keystroke in the summary buffer."
2020   (interactive)
2021   (let ((obuf (current-buffer))
2022         (owin (current-window-configuration))
2023         func)
2024     (switch-to-buffer gnus-summary-buffer 'norecord)
2025     (setq func (lookup-key (current-local-map) (this-command-keys)))
2026     (call-interactively func)
2027     (set-buffer obuf)
2028     (set-window-configuration owin)
2029     (set-window-point (get-buffer-window (current-buffer)) (point))))
2030
2031 (defun gnus-article-summary-command-nosave ()
2032   "Execute the last keystroke in the summary buffer."
2033   (interactive)
2034   (let (func)
2035     (pop-to-buffer gnus-summary-buffer 'norecord)
2036     (setq func (lookup-key (current-local-map) (this-command-keys)))
2037     (call-interactively func)))
2038
2039 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
2040   "Read a summary buffer key sequence and execute it from the article buffer."
2041   (interactive "P")
2042   (let ((nosaves
2043          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
2044            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
2045            "=" "^" "\M-^" "|"))
2046         (nosave-but-article
2047          '("A\r"))
2048         (nosave-in-article
2049          '("\C-d"))
2050         keys)
2051     (save-excursion
2052       (set-buffer gnus-summary-buffer)
2053       (push (or key last-command-event) unread-command-events)
2054       (setq keys (read-key-sequence nil)))
2055     (message "")
2056
2057     (if (or (member keys nosaves)
2058             (member keys nosave-but-article)
2059             (member keys nosave-in-article))
2060         (let (func)
2061           (save-window-excursion
2062             (pop-to-buffer gnus-summary-buffer 'norecord)
2063             (setq func (lookup-key (current-local-map) keys)))
2064           (if (not func)
2065               (ding)
2066             (unless (member keys nosave-in-article)
2067               (set-buffer gnus-summary-buffer))
2068             (call-interactively func))
2069           (when (member keys nosave-but-article)
2070             (pop-to-buffer gnus-article-buffer 'norecord)))
2071       ;; These commands should restore window configuration.
2072       (let ((obuf (current-buffer))
2073             (owin (current-window-configuration))
2074             (opoint (point))
2075             func in-buffer)
2076         (if not-restore-window
2077             (pop-to-buffer gnus-summary-buffer 'norecord)
2078           (switch-to-buffer gnus-summary-buffer 'norecord))
2079         (setq in-buffer (current-buffer))
2080         (if (setq func (lookup-key (current-local-map) keys))
2081             (call-interactively func)
2082           (ding))
2083         (when (eq in-buffer (current-buffer))
2084           (set-buffer obuf)
2085           (unless not-restore-window
2086             (set-window-configuration owin))
2087           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
2088
2089 (defun gnus-article-hide (&optional arg force)
2090   "Hide all the gruft in the current article.
2091 This means that PGP stuff, signatures, cited text and (some)
2092 headers will be hidden.
2093 If given a prefix, show the hidden text instead."
2094   (interactive (list current-prefix-arg 'force))
2095   (gnus-article-hide-headers arg)
2096   (gnus-article-hide-pgp arg)
2097   (gnus-article-hide-citation-maybe arg force)
2098   (gnus-article-hide-signature arg))
2099
2100 (defun gnus-article-maybe-highlight ()
2101   "Do some article highlighting if `article-visual' is non-nil."
2102   (when (gnus-visual-p 'article-highlight 'highlight)
2103     (gnus-article-highlight-some)))
2104
2105 (defun gnus-request-article-this-buffer (article group)
2106   "Get an article and insert it into this buffer."
2107   (let (do-update-line)
2108     (prog1
2109         (save-excursion
2110           (erase-buffer)
2111           (gnus-kill-all-overlays)
2112           (setq group (or group gnus-newsgroup-name))
2113
2114           ;; Open server if it has closed.
2115           (gnus-check-server (gnus-find-method-for-group group))
2116
2117           ;; Using `gnus-request-article' directly will insert the article into
2118           ;; `nntp-server-buffer' - so we'll save some time by not having to
2119           ;; copy it from the server buffer into the article buffer.
2120
2121           ;; We only request an article by message-id when we do not have the
2122           ;; headers for it, so we'll have to get those.
2123           (when (stringp article)
2124             (let ((gnus-override-method gnus-refer-article-method))
2125               (gnus-read-header article)))
2126
2127           ;; If the article number is negative, that means that this article
2128           ;; doesn't belong in this newsgroup (possibly), so we find its
2129           ;; message-id and request it by id instead of number.
2130           (when (and (numberp article)
2131                      gnus-summary-buffer
2132                      (get-buffer gnus-summary-buffer)
2133                      (buffer-name (get-buffer gnus-summary-buffer)))
2134             (save-excursion
2135               (set-buffer gnus-summary-buffer)
2136               (let ((header (gnus-summary-article-header article)))
2137                 (when (< article 0)
2138                   (cond 
2139                    ((memq article gnus-newsgroup-sparse)
2140                     ;; This is a sparse gap article.
2141                     (setq do-update-line article)
2142                     (setq article (mail-header-id header))
2143                     (let ((gnus-override-method gnus-refer-article-method))
2144                       (gnus-read-header article))
2145                     (setq gnus-newsgroup-sparse
2146                           (delq article gnus-newsgroup-sparse)))
2147                    ((vectorp header)
2148                     ;; It's a real article.
2149                     (setq article (mail-header-id header)))
2150                    (t
2151                     ;; It is an extracted pseudo-article.
2152                     (setq article 'pseudo)
2153                     (gnus-request-pseudo-article header))))
2154                 
2155                 (let ((method (gnus-find-method-for-group 
2156                                gnus-newsgroup-name)))
2157                   (if (not (eq (car method) 'nneething))
2158                       ()
2159                     (let ((dir (concat (file-name-as-directory (nth 1 method))
2160                                        (mail-header-subject header))))
2161                       (when (file-directory-p dir)
2162                         (setq article 'nneething)
2163                         (gnus-group-enter-directory dir))))))))
2164
2165           (cond
2166            ;; Refuse to select canceled articles.
2167            ((and (numberp article)
2168                  gnus-summary-buffer
2169                  (get-buffer gnus-summary-buffer)
2170                  (buffer-name (get-buffer gnus-summary-buffer))
2171                  (eq (cdr (save-excursion
2172                             (set-buffer gnus-summary-buffer)
2173                             (assq article gnus-newsgroup-reads)))
2174                      gnus-canceled-mark))
2175             nil)
2176            ;; We first check `gnus-original-article-buffer'.
2177            ((and (get-buffer gnus-original-article-buffer)
2178                  (numberp article)
2179                  (save-excursion
2180                    (set-buffer gnus-original-article-buffer)
2181                    (and (equal (car gnus-original-article) group)
2182                         (eq (cdr gnus-original-article) article))))
2183             (insert-buffer-substring gnus-original-article-buffer)
2184             'article)
2185            ;; Check the backlog.
2186            ((and gnus-keep-backlog
2187                  (gnus-backlog-request-article group article (current-buffer)))
2188             'article)
2189            ;; Check asynchronous pre-fetch.
2190            ((gnus-async-request-fetched-article group article (current-buffer))
2191             (gnus-async-prefetch-next group article gnus-summary-buffer)
2192             'article)
2193            ;; Check the cache.
2194            ((and gnus-use-cache
2195                  (numberp article)
2196                  (gnus-cache-request-article article group))
2197             'article)
2198            ;; Get the article and put into the article buffer.
2199            ((or (stringp article) (numberp article))
2200             (let ((gnus-override-method
2201                    (and (stringp article) gnus-refer-article-method))
2202                   (buffer-read-only nil))
2203               (erase-buffer)
2204               (gnus-kill-all-overlays)
2205               (when (gnus-request-article article group (current-buffer))
2206                 (when (numberp article)
2207                   (gnus-async-prefetch-next group article gnus-summary-buffer)
2208                   (when gnus-keep-backlog
2209                     (gnus-backlog-enter-article 
2210                      group article (current-buffer))))
2211                 'article)))
2212            ;; It was a pseudo.
2213            (t article)))
2214
2215       ;; Take the article from the original article buffer
2216       ;; and place it in the buffer it's supposed to be in.
2217       (when (and (get-buffer gnus-article-buffer)
2218                  ;;(numberp article)
2219                  (equal (buffer-name (current-buffer))
2220                         (buffer-name (get-buffer gnus-article-buffer))))
2221         (save-excursion
2222           (if (get-buffer gnus-original-article-buffer)
2223               (set-buffer (get-buffer gnus-original-article-buffer))
2224             (set-buffer (get-buffer-create gnus-original-article-buffer))
2225             (buffer-disable-undo (current-buffer))
2226             (setq major-mode 'gnus-original-article-mode)
2227             (setq buffer-read-only t)
2228             (gnus-add-current-to-buffer-list))
2229           (let (buffer-read-only)
2230             (erase-buffer)
2231             (insert-buffer-substring gnus-article-buffer))
2232           (setq gnus-original-article (cons group article))))
2233     
2234       ;; Update sparse articles.
2235       (when (and do-update-line
2236                  (or (numberp article)
2237                      (stringp article)))
2238         (let ((buf (current-buffer)))
2239           (set-buffer gnus-summary-buffer)
2240           (gnus-summary-update-article do-update-line)
2241           (gnus-summary-goto-subject do-update-line nil t)
2242           (set-window-point (get-buffer-window (current-buffer) t)
2243                             (point))
2244           (set-buffer buf))))))
2245
2246 ;;;
2247 ;;; Article editing
2248 ;;;
2249
2250 (defcustom gnus-article-edit-mode-hook nil
2251   "Hook run in article edit mode buffers."
2252   :group 'article
2253   :type 'hook)
2254
2255 (defvar gnus-article-edit-done-function nil)
2256
2257 (defvar gnus-article-edit-mode-map nil)
2258
2259 (unless gnus-article-edit-mode-map 
2260   (setq gnus-article-edit-mode-map (copy-keymap text-mode-map))
2261
2262   (gnus-define-keys gnus-article-edit-mode-map
2263     "\C-c\C-c" gnus-article-edit-done
2264     "\C-c\C-k" gnus-article-edit-exit)
2265
2266   (gnus-define-keys (gnus-article-edit-wash-map
2267                      "\C-c\C-w" gnus-article-edit-mode-map)
2268     "f" gnus-article-edit-full-stops))
2269
2270 (defun gnus-article-edit-mode ()
2271   "Major mode for editing articles.
2272 This is an extended text-mode.
2273
2274 \\{gnus-article-edit-mode-map}"
2275   (interactive)
2276   (kill-all-local-variables)
2277   (setq major-mode 'gnus-article-edit-mode)
2278   (setq mode-name "Article Edit")
2279   (use-local-map gnus-article-edit-mode-map)
2280   (make-local-variable 'gnus-article-edit-done-function)
2281   (make-local-variable 'gnus-prev-winconf)
2282   (setq buffer-read-only nil)
2283   (buffer-enable-undo)
2284   (widen)
2285   (run-hooks 'text-mode 'gnus-article-edit-mode-hook))
2286
2287 (defun gnus-article-edit (&optional force)
2288   "Edit the current article.
2289 This will have permanent effect only in mail groups.
2290 If FORCE is non-nil, allow editing of articles even in read-only
2291 groups."
2292   (interactive "P")
2293   (when (and (not force)
2294              (gnus-group-read-only-p))
2295     (error "The current newsgroup does not support article editing."))
2296   (gnus-article-edit-article
2297    `(lambda ()
2298       (gnus-summary-edit-article-done
2299        ,(or (mail-header-references gnus-current-headers) "")
2300        ,(gnus-group-read-only-p) ,gnus-summary-buffer))))
2301
2302 (defun gnus-article-edit-article (exit-func)
2303   "Start editing the contents of the current article buffer."
2304   (let ((winconf (current-window-configuration)))
2305     (set-buffer gnus-article-buffer)
2306     (gnus-article-edit-mode)
2307     (set-text-properties (point-min) (point-max) nil)
2308     (gnus-configure-windows 'edit-article)
2309     (setq gnus-article-edit-done-function exit-func)
2310     (setq gnus-prev-winconf winconf)
2311     (gnus-message 6 "C-c C-c to end edits")))
2312
2313 (defun gnus-article-edit-done ()
2314   "Update the article edits and exit."
2315   (interactive)
2316   (let ((func gnus-article-edit-done-function)
2317         (buf (current-buffer))
2318         (start (window-start)))
2319     (gnus-article-edit-exit)
2320     (save-excursion
2321       (set-buffer buf)
2322       (let ((buffer-read-only nil))
2323         (funcall func)))
2324     (set-buffer buf)
2325     (set-window-start (get-buffer-window buf) start)
2326     (set-window-point (get-buffer-window buf) (point))))
2327
2328 (defun gnus-article-edit-exit ()
2329   "Exit the article editing without updating."
2330   (interactive)
2331   ;; We remove all text props from the article buffer.
2332   (let ((buf (format "%s" (buffer-string)))
2333         (curbuf (current-buffer))
2334         (p (point))
2335         (window-start (window-start)))
2336     (erase-buffer)
2337     (insert buf)
2338     (let ((winconf gnus-prev-winconf))
2339       (gnus-article-mode)
2340       ;; The cache and backlog have to be flushed somewhat.
2341       (when gnus-use-cache
2342         (gnus-cache-update-article      
2343          (car gnus-article-current) (cdr gnus-article-current)))
2344       (when gnus-keep-backlog
2345         (gnus-backlog-remove-article 
2346          (car gnus-article-current) (cdr gnus-article-current)))
2347       ;; Flush original article as well.
2348       (save-excursion
2349         (when (get-buffer gnus-original-article-buffer)
2350           (set-buffer gnus-original-article-buffer)
2351           (setq gnus-original-article nil)))
2352       (set-window-configuration winconf)
2353       ;; Tippy-toe some to make sure that point remains where it was.
2354       (let ((buf (current-buffer)))
2355         (set-buffer curbuf)
2356         (set-window-start (get-buffer-window (current-buffer)) window-start)
2357         (goto-char p)
2358         (set-buffer buf)))))
2359       
2360 (defun gnus-article-edit-full-stops ()
2361   "Interactively repair spacing at end of sentences."
2362   (interactive)
2363   (save-excursion
2364     (goto-char (point-min))
2365     (search-forward-regexp "^$" nil t)
2366     (let ((case-fold-search nil))
2367       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
2368
2369 ;;; 
2370 ;;; Article highlights
2371 ;;;
2372
2373 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
2374
2375 ;;; Internal Variables:
2376
2377 (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\\)"
2378   "Regular expression that matches URLs."
2379   :group 'article
2380   :type 'regexp)
2381
2382 (defcustom gnus-button-alist 
2383   `(("\\(<?\\(url: ?\\)?news:\\(//\\)?\\([^>\n\t ]*\\)>?\\)" 1 t
2384      gnus-button-fetch-group 4)
2385     ("\\bin\\( +article\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2 
2386      t gnus-button-message-id 3)
2387     ("\\(<?\\(url: ?\\)?news:\\([^>\n\t ]*\\)>?\\)" 1 t
2388      gnus-button-message-id 3)
2389     ("\\(<URL: *\\)?mailto: *\\([^> \n\t]+\\)>?" 0 t gnus-url-mailto 2)
2390     ;; This is how URLs _should_ be embedded in text...
2391     ("<URL: *\\([^>]*\\)>" 0 t gnus-button-embedded-url 1)
2392     ;; Raw URLs.
2393     (,gnus-button-url-regexp 0 t gnus-button-url 0))
2394   "Alist of regexps matching buttons in article bodies.
2395
2396 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
2397 REGEXP: is the string matching text around the button,
2398 BUTTON: is the number of the regexp grouping actually matching the button,
2399 FORM: is a lisp expression which must eval to true for the button to
2400 be added, 
2401 CALLBACK: is the function to call when the user push this button, and each
2402 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
2403
2404 CALLBACK can also be a variable, in that case the value of that
2405 variable it the real callback function."
2406   :group 'article
2407   :type '(repeat (list regexp 
2408                        (integer :tag "Button")
2409                        (sexp :tag "Form")
2410                        (function :tag "Callback")
2411                        (repeat :tag "Par"
2412                                :inline t
2413                                (integer :tag "Regexp group")))))
2414
2415 (defcustom gnus-header-button-alist 
2416   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
2417      0 t gnus-button-message-id 0)
2418     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
2419     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+" 
2420      0 t gnus-button-mailto 0)
2421     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2422     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
2423     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
2424      gnus-button-message-id 3))
2425   "Alist of headers and regexps to match buttons in article heads.
2426
2427 This alist is very similar to `gnus-button-alist', except that each
2428 alist has an additional HEADER element first in each entry:
2429
2430 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
2431
2432 HEADER is a regexp to match a header.  For a fuller explanation, see
2433 `gnus-button-alist'."
2434   :group 'article
2435   :type '(repeat (list (regexp :tag "Header")
2436                        regexp 
2437                        (integer :tag "Button")
2438                        (sexp :tag "Form")
2439                        (function :tag "Callback")
2440                        (repeat :tag "Par"
2441                                :inline t
2442                                (integer :tag "Regexp group")))))
2443
2444 (defvar gnus-button-regexp nil)
2445 (defvar gnus-button-marker-list nil)
2446 ;; Regexp matching any of the regexps from `gnus-button-alist'.
2447
2448 (defvar gnus-button-last nil)
2449 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
2450
2451 ;;; Commands:
2452
2453 (defun gnus-article-push-button (event)
2454   "Check text under the mouse pointer for a callback function.
2455 If the text under the mouse pointer has a `gnus-callback' property,
2456 call it with the value of the `gnus-data' text property."
2457   (interactive "e")
2458   (set-buffer (window-buffer (posn-window (event-start event))))
2459   (let* ((pos (posn-point (event-start event)))
2460          (data (get-text-property pos 'gnus-data))
2461          (fun (get-text-property pos 'gnus-callback)))
2462     (when fun
2463       (funcall fun data))))
2464
2465 (defun gnus-article-press-button ()
2466   "Check text at point for a callback function.
2467 If the text at point has a `gnus-callback' property,
2468 call it with the value of the `gnus-data' text property."
2469   (interactive)
2470   (let* ((data (get-text-property (point) 'gnus-data))
2471          (fun (get-text-property (point) 'gnus-callback)))
2472     (when fun
2473       (funcall fun data))))
2474
2475 (defun gnus-article-prev-button (n)
2476   "Move point to N buttons backward.
2477 If N is negative, move forward instead."
2478   (interactive "p")
2479   (gnus-article-next-button (- n)))
2480
2481 (defun gnus-article-next-button (n)
2482   "Move point to N buttons forward.
2483 If N is negative, move backward instead."
2484   (interactive "p")
2485   (let ((function (if (< n 0) 'previous-single-property-change
2486                     'next-single-property-change))
2487         (inhibit-point-motion-hooks t)
2488         (backward (< n 0))
2489         (limit (if (< n 0) (point-min) (point-max))))
2490     (setq n (abs n))
2491     (while (and (not (= limit (point)))
2492                 (> n 0))
2493       ;; Skip past the current button.
2494       (when (get-text-property (point) 'gnus-callback)
2495         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2496       ;; Go to the next (or previous) button.
2497       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
2498       ;; Put point at the start of the button.
2499       (when (and backward (not (get-text-property (point) 'gnus-callback)))
2500         (goto-char (funcall function (point) 'gnus-callback nil limit)))
2501       ;; Skip past intangible buttons.
2502       (when (get-text-property (point) 'intangible)
2503         (incf n))
2504       (decf n))
2505     (unless (zerop n)
2506       (gnus-message 5 "No more buttons"))
2507     n))
2508
2509 (defun gnus-article-highlight (&optional force)
2510   "Highlight current article.
2511 This function calls `gnus-article-highlight-headers',
2512 `gnus-article-highlight-citation', 
2513 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2514 do the highlighting.  See the documentation for those functions."
2515   (interactive (list 'force))
2516   (gnus-article-highlight-headers)
2517   (gnus-article-highlight-citation force)
2518   (gnus-article-highlight-signature)
2519   (gnus-article-add-buttons force)
2520   (gnus-article-add-buttons-to-head))
2521
2522 (defun gnus-article-highlight-some (&optional force)
2523   "Highlight current article.
2524 This function calls `gnus-article-highlight-headers',
2525 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
2526 do the highlighting.  See the documentation for those functions."
2527   (interactive (list 'force))
2528   (gnus-article-highlight-headers)
2529   (gnus-article-highlight-signature)
2530   (gnus-article-add-buttons))
2531
2532 (defun gnus-article-highlight-headers ()
2533   "Highlight article headers as specified by `gnus-header-face-alist'."
2534   (interactive)
2535   (save-excursion
2536     (set-buffer gnus-article-buffer)
2537     (save-restriction
2538       (let ((alist gnus-header-face-alist)
2539             (buffer-read-only nil)
2540             (case-fold-search t)
2541             (inhibit-point-motion-hooks t)
2542             entry regexp header-face field-face from hpoints fpoints)
2543         (goto-char (point-min))
2544         (when (search-forward "\n\n" nil t)
2545           (narrow-to-region (1- (point)) (point-min))
2546           (while (setq entry (pop alist))
2547             (goto-char (point-min))
2548             (setq regexp (concat "^\\("
2549                                  (if (string-equal "" (nth 0 entry))
2550                                      "[^\t ]"
2551                                    (nth 0 entry))
2552                                  "\\)")
2553                   header-face (nth 1 entry)
2554                   field-face (nth 2 entry))
2555             (while (and (re-search-forward regexp nil t)
2556                         (not (eobp)))
2557               (beginning-of-line)
2558               (setq from (point))
2559               (unless (search-forward ":" nil t)
2560                 (forward-char 1))
2561               (when (and header-face
2562                          (not (memq (point) hpoints)))
2563                 (push (point) hpoints)
2564                 (gnus-put-text-property from (point) 'face header-face))
2565               (when (and field-face
2566                          (not (memq (setq from (point)) fpoints)))
2567                 (push from fpoints)
2568                 (if (re-search-forward "^[^ \t]" nil t)
2569                     (forward-char -2)
2570                   (goto-char (point-max)))
2571                 (gnus-put-text-property from (point) 'face field-face)))))))))
2572
2573 (defun gnus-article-highlight-signature ()
2574   "Highlight the signature in an article.
2575 It does this by highlighting everything after
2576 `gnus-signature-separator' using `gnus-signature-face'." 
2577   (interactive)
2578   (save-excursion
2579     (set-buffer gnus-article-buffer)
2580     (let ((buffer-read-only nil)
2581           (inhibit-point-motion-hooks t))
2582       (save-restriction
2583         (when (and gnus-signature-face
2584                    (gnus-article-narrow-to-signature))
2585           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
2586                             'face gnus-signature-face)
2587           (widen)
2588           (gnus-article-search-signature)
2589           (let ((start (match-beginning 0))
2590                 (end (set-marker (make-marker) (1+ (match-end 0)))))
2591             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
2592                                      end)))))))
2593
2594 (defun gnus-article-add-buttons (&optional force)
2595   "Find external references in the article and make buttons of them.
2596 \"External references\" are things like Message-IDs and URLs, as
2597 specified by `gnus-button-alist'."
2598   (interactive (list 'force))
2599   (save-excursion
2600     (set-buffer gnus-article-buffer)
2601     ;; Remove all old markers.
2602     (while gnus-button-marker-list
2603       (set-marker (pop gnus-button-marker-list) nil))
2604     (let ((buffer-read-only nil)
2605           (inhibit-point-motion-hooks t)
2606           (case-fold-search t)
2607           (alist gnus-button-alist)
2608           beg entry regexp)
2609       (goto-char (point-min))
2610       ;; We skip the headers.
2611       (unless (search-forward "\n\n" nil t)
2612         (goto-char (point-max)))
2613       (setq beg (point))
2614       (while (setq entry (pop alist))
2615         (setq regexp (car entry))
2616         (goto-char beg)
2617         (while (re-search-forward regexp nil t)
2618           (let* ((start (and entry (match-beginning (nth 1 entry))))
2619                  (end (and entry (match-end (nth 1 entry))))
2620                  (from (match-beginning 0)))
2621             (when (and (or (eq t (nth 1 entry))
2622                            (eval (nth 1 entry)))
2623                        (not (get-text-property (point) 'gnus-callback)))
2624               ;; That optional form returned non-nil, so we add the
2625               ;; button. 
2626               (gnus-article-add-button 
2627                start end 'gnus-button-push 
2628                (car (push (set-marker (make-marker) from)
2629                           gnus-button-marker-list))))))))))
2630
2631 ;; Add buttons to the head of an article.
2632 (defun gnus-article-add-buttons-to-head ()
2633   "Add buttons to the head of the article."
2634   (interactive)
2635   (save-excursion
2636     (set-buffer gnus-article-buffer)
2637     (let ((buffer-read-only nil)
2638           (inhibit-point-motion-hooks t)
2639           (case-fold-search t)
2640           (alist gnus-header-button-alist)
2641           entry beg end)
2642       (nnheader-narrow-to-headers)
2643       (while alist
2644         ;; Each alist entry.
2645         (setq entry (car alist)
2646               alist (cdr alist))
2647         (goto-char (point-min))
2648         (while (re-search-forward (car entry) nil t)
2649           ;; Each header matching the entry.
2650           (setq beg (match-beginning 0))
2651           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
2652                              (match-beginning 0))
2653                         (point-max)))
2654           (goto-char beg)
2655           (while (re-search-forward (nth 1 entry) end t)
2656             ;; Each match within a header.
2657             (let* ((entry (cdr entry))
2658                    (start (match-beginning (nth 1 entry)))
2659                    (end (match-end (nth 1 entry)))
2660                    (form (nth 2 entry)))
2661               (goto-char (match-end 0))
2662               (when (eval form)
2663                 (gnus-article-add-button 
2664                  start end (nth 3 entry)
2665                  (buffer-substring (match-beginning (nth 4 entry))
2666                                    (match-end (nth 4 entry)))))))
2667           (goto-char end))))
2668     (widen)))
2669
2670 ;;; External functions:
2671
2672 (defun gnus-article-add-button (from to fun &optional data)
2673   "Create a button between FROM and TO with callback FUN and data DATA."
2674   (when gnus-article-button-face
2675     (gnus-overlay-put (gnus-make-overlay from to)
2676                       'face gnus-article-button-face))
2677   (gnus-add-text-properties 
2678    from to
2679    (nconc (and gnus-article-mouse-face
2680                (list gnus-mouse-face-prop gnus-article-mouse-face))
2681           (list 'gnus-callback fun)
2682           (and data (list 'gnus-data data)))))
2683
2684 ;;; Internal functions:
2685
2686 (defun gnus-signature-toggle (end)
2687   (save-excursion
2688     (set-buffer gnus-article-buffer)
2689     (let ((buffer-read-only nil)
2690           (inhibit-point-motion-hooks t))
2691       (if (get-text-property end 'invisible)
2692           (gnus-article-unhide-text end (point-max))
2693         (gnus-article-hide-text end (point-max) gnus-hidden-properties)))))
2694
2695 (defun gnus-button-entry ()
2696   ;; Return the first entry in `gnus-button-alist' matching this place.
2697   (let ((alist gnus-button-alist)
2698         (entry nil))
2699     (while alist
2700       (setq entry (pop alist))
2701       (if (looking-at (car entry))
2702           (setq alist nil)
2703         (setq entry nil)))
2704     entry))
2705
2706 (defun gnus-button-push (marker)
2707   ;; Push button starting at MARKER.
2708   (save-excursion
2709     (set-buffer gnus-article-buffer)
2710     (goto-char marker)
2711     (let* ((entry (gnus-button-entry))
2712            (inhibit-point-motion-hooks t)
2713            (fun (nth 3 entry))
2714            (args (mapcar (lambda (group)
2715                            (let ((string (match-string group)))
2716                              (gnus-set-text-properties
2717                               0 (length string) nil string)
2718                              string))
2719                          (nthcdr 4 entry))))
2720       (cond
2721        ((fboundp fun)
2722         (apply fun args))
2723        ((and (boundp fun)
2724              (fboundp (symbol-value fun)))
2725         (apply (symbol-value fun) args))
2726        (t
2727         (gnus-message 1 "You must define `%S' to use this button"
2728                       (cons fun args)))))))
2729
2730 (defun gnus-button-message-id (message-id)
2731   "Fetch MESSAGE-ID."
2732   (save-excursion
2733     (set-buffer gnus-summary-buffer)
2734     (gnus-summary-refer-article message-id)))
2735
2736 (defun gnus-button-fetch-group (address)
2737   "Fetch GROUP specified by ADDRESS."
2738   (if (not (string-match "^\\([^:/]+\\)\\(:\\([^/]+\\)\\)?/\\(.*\\)$" address))
2739       (error "Can't parse %s" address)
2740     (gnus-group-read-ephemeral-group
2741      (match-string 4 address)
2742      `(nntp ,(match-string 1 address) (nntp-address ,(match-string 1 address))
2743             (nntp-port-number ,(if (match-end 3)
2744                                    (match-string 3 address)
2745                                  "nntp"))))))
2746
2747 (defun gnus-split-string (string pattern)
2748   "Return a list of substrings of STRING which are separated by PATTERN."
2749   (let (parts (start 0))
2750     (while (string-match pattern string start)
2751       (setq parts (cons (substring string start (match-beginning 0)) parts)
2752             start (match-end 0)))
2753     (nreverse (cons (substring string start) parts))))
2754  
2755 (defun gnus-url-parse-query-string (query &optional downcase)
2756   (let (retval pairs cur key val)
2757     (setq pairs (gnus-split-string query "&"))
2758     (while pairs
2759       (setq cur (car pairs)
2760             pairs (cdr pairs))
2761       (if (not (string-match "=" cur))
2762           nil                           ; Grace
2763         (setq key (gnus-url-unhex-string (substring cur 0 (match-beginning 0)))
2764               val (gnus-url-unhex-string (substring cur (match-end 0) nil)))
2765         (if downcase
2766             (setq key (downcase key)))
2767         (setq cur (assoc key retval))
2768         (if cur
2769             (setcdr cur (cons val (cdr cur)))
2770           (setq retval (cons (list key val) retval)))))
2771     retval))
2772  
2773 (defun gnus-url-unhex (x)
2774   (if (> x ?9)
2775       (if (>= x ?a)
2776           (+ 10 (- x ?a))
2777         (+ 10 (- x ?A)))
2778     (- x ?0)))
2779  
2780 (defun gnus-url-unhex-string (str &optional allow-newlines)
2781   "Remove %XXX embedded spaces, etc in a url.
2782 If optional second argument ALLOW-NEWLINES is non-nil, then allow the
2783 decoding of carriage returns and line feeds in the string, which is normally
2784 forbidden in URL encoding."
2785   (setq str (or str ""))
2786   (let ((tmp "")
2787         (case-fold-search t))
2788     (while (string-match "%[0-9a-f][0-9a-f]" str)
2789       (let* ((start (match-beginning 0))
2790              (ch1 (gnus-url-unhex (elt str (+ start 1))))
2791              (code (+ (* 16 ch1)
2792                       (gnus-url-unhex (elt str (+ start 2))))))
2793         (setq tmp (concat 
2794                    tmp (substring str 0 start)
2795                    (cond
2796                     (allow-newlines
2797                      (char-to-string code))
2798                     ((or (= code ?\n) (= code ?\r))
2799                      " ")
2800                     (t (char-to-string code))))
2801               str (substring str (match-end 0)))))
2802     (setq tmp (concat tmp str))
2803     tmp))
2804  
2805 (defun gnus-url-mailto (url)
2806   ;; Send mail to someone
2807   (if (not (string-match "mailto:/*\\(.*\\)" url))
2808       (error "Malformed mailto link: %s" url))
2809   (setq url (substring url (match-beginning 1) nil))
2810   (let (to args source-url subject func)
2811     (if (string-match (regexp-quote "?") url)
2812         (setq to (gnus-url-unhex-string (substring url 0 (match-beginning 0)))
2813               args (gnus-url-parse-query-string
2814                     (substring url (match-end 0) nil) t))
2815       (setq to (gnus-url-unhex-string url)))
2816     (setq args (cons (list "to" to) args)
2817           subject (cdr-safe (assoc "subject" args)))
2818     (message-mail)
2819     (while args
2820       (setq func (intern-soft (concat "message-goto-" (downcase (caar args)))))
2821       (if (fboundp func)
2822           (funcall func)
2823         (message-position-on-field (caar args)))
2824       (insert (mapconcat 'identity (cdar args) ", "))
2825       (setq args (cdr args)))
2826     (if subject
2827         (message-goto-body)
2828       (message-goto-subject))))
2829
2830 (defun gnus-button-mailto (address)
2831   ;; Mail to ADDRESS.
2832   (set-buffer (gnus-copy-article-buffer))
2833   (message-reply address))
2834
2835 (defun gnus-button-reply (address)
2836   ;; Reply to ADDRESS.
2837   (message-reply address))
2838
2839 (defun gnus-button-url (address)
2840   "Browse ADDRESS."
2841   (funcall browse-url-browser-function address))
2842
2843 (defun gnus-button-embedded-url (address)
2844   "Browse ADDRESS."
2845   (funcall browse-url-browser-function (gnus-strip-whitespace address)))
2846
2847 ;;; Next/prev buttons in the article buffer.
2848
2849 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
2850 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
2851
2852 (defvar gnus-prev-page-map nil)
2853 (unless gnus-prev-page-map
2854   (setq gnus-prev-page-map (make-sparse-keymap))
2855   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
2856   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
2857
2858 (defun gnus-insert-prev-page-button ()
2859   (let ((buffer-read-only nil))
2860     (gnus-eval-format 
2861      gnus-prev-page-line-format nil
2862      `(gnus-prev t local-map ,gnus-prev-page-map
2863                  gnus-callback gnus-article-button-prev-page))))
2864
2865 (defvar gnus-next-page-map nil)
2866 (unless gnus-next-page-map
2867   (setq gnus-next-page-map (make-keymap))
2868   (suppress-keymap gnus-prev-page-map)
2869   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
2870   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
2871
2872 (defun gnus-button-next-page ()
2873   "Go to the next page."
2874   (interactive)
2875   (let ((win (selected-window)))
2876     (select-window (get-buffer-window gnus-article-buffer t))
2877     (gnus-article-next-page)
2878     (select-window win)))
2879
2880 (defun gnus-button-prev-page ()
2881   "Go to the prev page."
2882   (interactive)
2883   (let ((win (selected-window)))
2884     (select-window (get-buffer-window gnus-article-buffer t))
2885     (gnus-article-prev-page)
2886     (select-window win)))
2887
2888 (defun gnus-insert-next-page-button ()
2889   (let ((buffer-read-only nil))
2890     (gnus-eval-format gnus-next-page-line-format nil
2891                       `(gnus-next t local-map ,gnus-next-page-map
2892                                   gnus-callback 
2893                                   gnus-article-button-next-page))))
2894
2895 (defun gnus-article-button-next-page (arg)
2896   "Go to the next page."
2897   (interactive "P")
2898   (let ((win (selected-window)))
2899     (select-window (get-buffer-window gnus-article-buffer t))
2900     (gnus-article-next-page)
2901     (select-window win)))
2902
2903 (defun gnus-article-button-prev-page (arg)
2904   "Go to the prev page."
2905   (interactive "P")
2906   (let ((win (selected-window)))
2907     (select-window (get-buffer-window gnus-article-buffer t))
2908     (gnus-article-prev-page)
2909     (select-window win))) 
2910
2911 (provide 'gnus-art)
2912
2913 (run-hooks 'gnus-art-load-hook)
2914
2915 ;;; gnus-art.el ends here