*** 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 'gnus-load)
29 (require 'gnus-sum)
30 (require 'article)
31 (require 'gnus-spec)
32 (require 'gnus-int)
33
34 (defvar gnus-article-save-directory gnus-directory
35   "*Name of the directory articles will be saved in (default \"~/News\").")
36
37 (defvar gnus-save-all-headers t
38   "*If non-nil, don't remove any headers before saving.")
39
40 (defvar gnus-prompt-before-saving 'always
41   "*This variable says how much prompting is to be done when saving articles.
42 If it is nil, no prompting will be done, and the articles will be
43 saved to the default files.  If this variable is `always', each and
44 every article that is saved will be preceded by a prompt, even when
45 saving large batches of articles.  If this variable is neither nil not
46 `always', there the user will be prompted once for a file name for
47 each invocation of the saving commands.")
48
49 (defvar gnus-saved-headers gnus-visible-headers
50   "*Headers to keep if `gnus-save-all-headers' is nil.
51 If `gnus-save-all-headers' is non-nil, this variable will be ignored.
52 If that variable is nil, however, all headers that match this regexp
53 will be kept while the rest will be deleted before saving.")
54
55 (defvar gnus-default-article-saver 'gnus-summary-save-in-rmail
56   "*A function to save articles in your favorite format.
57 The function must be interactively callable (in other words, it must
58 be an Emacs command).
59
60 Gnus provides the following functions:
61
62 * gnus-summary-save-in-rmail (Rmail format)
63 * gnus-summary-save-in-mail (Unix mail format)
64 * gnus-summary-save-in-folder (MH folder)
65 * gnus-summary-save-in-file (article format).
66 * gnus-summary-save-in-vm (use VM's folder format).")
67
68 (defvar gnus-rmail-save-name (function gnus-plain-save-name)
69   "*A function generating a file name to save articles in Rmail format.
70 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
71
72 (defvar gnus-mail-save-name (function gnus-plain-save-name)
73   "*A function generating a file name to save articles in Unix mail format.
74 The function is called with NEWSGROUP, HEADERS, and optional LAST-FILE.")
75
76 (defvar gnus-folder-save-name (function gnus-folder-save-name)
77   "*A function generating a file name to save articles in MH folder.
78 The function is called with NEWSGROUP, HEADERS, and optional LAST-FOLDER.")
79
80 (defvar gnus-file-save-name (function gnus-numeric-save-name)
81   "*A function generating a file name to save articles in article format.
82 The function is called with NEWSGROUP, HEADERS, and optional
83 LAST-FILE.")
84
85 (defvar gnus-split-methods
86   '((gnus-article-archive-name))
87   "*Variable used to suggest where articles are to be saved.
88 For instance, if you would like to save articles related to Gnus in
89 the file \"gnus-stuff\", and articles related to VM in \"vm-stuff\",
90 you could set this variable to something like:
91
92  '((\"^Subject:.*gnus\\|^Newsgroups:.*gnus\" \"gnus-stuff\")
93    (\"^Subject:.*vm\\|^Xref:.*vm\" \"vm-stuff\"))
94
95 This variable is an alist where the where the key is the match and the
96 value is a list of possible files to save in if the match is non-nil.
97
98 If the match is a string, it is used as a regexp match on the
99 article.  If the match is a symbol, that symbol will be funcalled
100 from the buffer of the article to be saved with the newsgroup as the
101 parameter.  If it is a list, it will be evaled in the same buffer.
102
103 If this form or function returns a string, this string will be used as
104 a possible file name; and if it returns a non-nil list, that list will
105 be used as possible file names.")
106
107 (defvar gnus-strict-mime t
108   "*If nil, MIME-decode even if there is no Mime-Version header in the article.")
109
110 (defvar gnus-show-mime-method 'metamail-buffer
111   "*Function to process a MIME message.
112 The function is called from the article buffer.")
113
114 (defvar gnus-decode-encoded-word-method (lambda ())
115   "*Function to decode a MIME encoded-words.
116 The function is called from the article buffer.")
117
118 (defvar gnus-page-delimiter "^\^L"
119   "*Regexp describing what to use as article page delimiters.
120 The default value is \"^\^L\", which is a form linefeed at the
121 beginning of a line.")
122
123 (defvar gnus-article-mode-line-format "Gnus: %%b %S"
124   "*The format specification for the article mode line.
125 See `gnus-summary-mode-line-format' for a closer description.")
126
127 (defvar gnus-article-mode-hook nil
128   "*A hook for Gnus article mode.")
129
130 (defvar gnus-article-menu-hook nil
131   "*Hook run after the creation of the article mode menu.")
132
133 (defvar gnus-article-prepare-hook nil
134   "*A hook called after an article has been prepared in the article buffer.
135 If you want to run a special decoding program like nkf, use this hook.")
136
137 ;(defvar gnus-article-display-hook nil
138 ;  "*A hook called after the article is displayed in the article buffer.
139 ;The hook is designed to change the contents of the article
140 ;buffer.  Typical functions that this hook may contain are
141 ;`gnus-article-hide-headers' (hide selected headers),
142 ;`gnus-article-maybe-highlight' (perform fancy article highlighting),
143 ;`gnus-article-hide-signature' (hide signature) and
144 ;`gnus-article-treat-overstrike' (turn \"^H_\" into bold characters).")
145 ;(add-hook 'gnus-article-display-hook 'gnus-article-hide-headers-if-wanted)
146 ;(add-hook 'gnus-article-display-hook 'gnus-article-treat-overstrike)
147 ;(add-hook 'gnus-article-display-hook 'gnus-article-maybe-highlight)
148
149 ;;; Internal variables
150
151 (defvar gnus-article-mode-line-format-alist
152     (nconc '((?w (gnus-article-wash-status) ?s))
153            gnus-summary-mode-line-format-alist))
154
155 (defvar gnus-number-of-articles-to-be-saved nil)
156
157 ;;; Provide a mapping from `gnus-*' commands to Article commands.
158
159 (eval-and-compile
160   (mapcar
161    (lambda (func)
162      (let (afunc gfunc)
163        (if (consp func)
164            (setq afunc (car func)
165                  gfunc (cdr func))
166          (setq afunc func
167                gfunc (intern (format "gnus-%s" func))))
168        (fset gfunc 
169              `(lambda (&optional interactive &rest args)
170                 ,(documentation afunc t)
171                 (interactive (list t))
172                 (save-excursion
173                   (set-buffer gnus-article-buffer)
174                   (if interactive
175                       (call-interactively ',afunc)
176                     (apply ',afunc args)))))))
177    '(article-hide-headers
178      article-hide-boring-headers
179      article-treat-overstrike
180      (article-fill . gnus-article-word-wrap)
181      article-remove-cr
182      article-display-x-face
183      article-de-quoted-unreadable
184      article-mime-decode-quoted-printable
185      article-hide-pgp
186      article-hide-pem
187      article-hide-signature
188      article-remove-trailing-blank-lines
189      article-strip-leading-blank-lines
190      article-strip-multiple-blank-lines
191      article-strip-blank-lines
192      article-date-local
193      article-date-original
194      article-date-lapsed
195      article-emphasize
196      (article-show-all . gnus-article-show-all-headers))))
197
198 (defalias 'gnus-decode-rfc1522 'article-decode-rfc1522)
199
200 ;;; Saving functions.
201
202 (defun gnus-article-save (save-buffer file &optional num)
203   "Save the currently selected article."
204   (unless gnus-save-all-headers
205     ;; Remove headers accoring to `gnus-saved-headers'.
206     (let ((gnus-visible-headers
207            (or gnus-saved-headers gnus-visible-headers))
208           (gnus-article-buffer save-buffer))
209       (gnus-article-hide-headers 1 t)))
210   (save-window-excursion
211     (if (not gnus-default-article-saver)
212         (error "No default saver is defined.")
213       ;; !!! Magic!  The saving functions all save
214       ;; `gnus-original-article-buffer' (or so they think),
215       ;; but we bind that variable to our save-buffer.
216       (set-buffer gnus-article-buffer)
217       (let* ((gnus-original-article-buffer save-buffer)
218              (filename
219               (cond
220                ((not gnus-prompt-before-saving)
221                 'default)
222                ((eq gnus-prompt-before-saving 'always)
223                 nil)
224                (t file)))
225              (gnus-number-of-articles-to-be-saved
226               (when (stringp filename) num))) ; Magic
227         (set-buffer gnus-summary-buffer)
228         (funcall gnus-default-article-saver filename)))))
229
230 (defun gnus-read-save-file-name (prompt default-name &optional filename)
231   (cond
232    ((eq filename 'default)
233     default-name)
234    (filename filename)
235    (t
236     (let* ((split-name (gnus-get-split-value gnus-split-methods))
237            (prompt
238             (format prompt (if (and gnus-number-of-articles-to-be-saved
239                                     (> gnus-number-of-articles-to-be-saved 1))
240                                (format "these %d articles"
241                                        gnus-number-of-articles-to-be-saved)
242                              "this article")))
243            (file
244             ;; Let the split methods have their say.
245             (cond
246              ;; No split name was found.
247              ((null split-name)
248               (read-file-name
249                (concat prompt " (default "
250                        (file-name-nondirectory default-name) ") ")
251                (file-name-directory default-name)
252                default-name))
253              ;; A single split name was found
254              ((= 1 (length split-name))
255               (let* ((name (car split-name))
256                      (dir (cond ((file-directory-p name)
257                                  (file-name-as-directory name))
258                                 ((file-exists-p name) name)
259                                 (t gnus-article-save-directory))))
260                 (read-file-name
261                  (concat prompt " (default " name ") ")
262                  dir name)))
263              ;; A list of splits was found.
264              (t
265               (setq split-name (nreverse split-name))
266               (let (result)
267                 (let ((file-name-history (nconc split-name file-name-history)))
268                   (setq result
269                         (read-file-name
270                          (concat prompt " (`M-p' for defaults) ")
271                          gnus-article-save-directory
272                          (car split-name))))
273                 (car (push result file-name-history)))))))
274       ;; Create the directory.
275       (unless (equal (directory-file-name file) file)
276         (make-directory (file-name-directory file) t))
277       ;; If we have read a directory, we append the default file name.
278       (when (file-directory-p file)
279         (setq file (concat (file-name-as-directory file)
280                            (file-name-nondirectory default-name))))
281       ;; Possibly translate some characters.
282       (nnheader-translate-file-chars file)))))
283
284 (defun gnus-article-archive-name (group)
285   "Return the first instance of an \"Archive-name\" in the current buffer."
286   (let ((case-fold-search t))
287     (when (re-search-forward "archive-name: *\\([^ \n\t]+\\)[ \t]*$" nil t)
288       (nnheader-concat gnus-article-save-directory
289                        (match-string 1)))))
290
291 (defun gnus-summary-save-in-rmail (&optional filename)
292   "Append this article to Rmail file.
293 Optional argument FILENAME specifies file name.
294 Directory to save to is default to `gnus-article-save-directory'."
295   (interactive)
296   (gnus-set-global-variables)
297   (let ((default-name
298           (funcall gnus-rmail-save-name gnus-newsgroup-name
299                    gnus-current-headers gnus-newsgroup-last-rmail)))
300     (setq filename (gnus-read-save-file-name
301                     "Save %s in rmail file:" default-name filename))
302     (unless (file-exists-p (file-name-directory filename))
303       (make-directory (file-name-directory filename) t))
304     (gnus-eval-in-buffer-window gnus-original-article-buffer
305       (save-excursion
306         (save-restriction
307           (widen)
308           (gnus-output-to-rmail filename))))
309     ;; Remember the directory name to save articles
310     (setq gnus-newsgroup-last-rmail filename)))
311
312 (defun gnus-summary-save-in-mail (&optional filename)
313   "Append this article to Unix mail file.
314 Optional argument FILENAME specifies file name.
315 Directory to save to is default to `gnus-article-save-directory'."
316   (interactive)
317   (gnus-set-global-variables)
318   (let ((default-name
319           (funcall gnus-mail-save-name gnus-newsgroup-name
320                    gnus-current-headers gnus-newsgroup-last-mail)))
321     (setq filename (gnus-read-save-file-name
322                     "Save %s in Unix mail file:" default-name filename))
323     (setq filename
324           (expand-file-name filename
325                             (and default-name
326                                  (file-name-directory default-name))))
327     (unless (file-exists-p (file-name-directory filename))
328       (make-directory (file-name-directory filename) t))
329     (gnus-eval-in-buffer-window gnus-original-article-buffer
330       (save-excursion
331         (save-restriction
332           (widen)
333           (if (and (file-readable-p filename) (mail-file-babyl-p filename))
334               (gnus-output-to-rmail filename)
335             (let ((mail-use-rfc822 t))
336               (rmail-output filename 1 t t))))))
337     ;; Remember the directory name to save articles.
338     (setq gnus-newsgroup-last-mail filename)))
339
340 (defun gnus-summary-save-in-file (&optional filename)
341   "Append this article to file.
342 Optional argument FILENAME specifies file name.
343 Directory to save to is default to `gnus-article-save-directory'."
344   (interactive)
345   (gnus-set-global-variables)
346   (let ((default-name
347           (funcall gnus-file-save-name gnus-newsgroup-name
348                    gnus-current-headers gnus-newsgroup-last-file)))
349     (setq filename (gnus-read-save-file-name
350                     "Save %s in file:" default-name filename))
351     (unless (file-exists-p (file-name-directory filename))
352       (make-directory (file-name-directory filename) t))
353     (gnus-eval-in-buffer-window gnus-original-article-buffer
354       (save-excursion
355         (save-restriction
356           (widen)
357           (gnus-output-to-file filename))))
358     ;; Remember the directory name to save articles.
359     (setq gnus-newsgroup-last-file filename)))
360
361 (defun gnus-summary-save-body-in-file (&optional filename)
362   "Append this article body to a file.
363 Optional argument FILENAME specifies file name.
364 The directory to save in defaults to `gnus-article-save-directory'."
365   (interactive)
366   (gnus-set-global-variables)
367   (let ((default-name
368           (funcall gnus-file-save-name gnus-newsgroup-name
369                    gnus-current-headers gnus-newsgroup-last-file)))
370     (setq filename (gnus-read-save-file-name
371                     "Save %s body in file:" default-name filename))
372     (unless (file-exists-p (file-name-directory filename))
373       (make-directory (file-name-directory filename) t))
374     (gnus-eval-in-buffer-window gnus-original-article-buffer
375       (save-excursion
376         (save-restriction
377           (widen)
378           (goto-char (point-min))
379           (and (search-forward "\n\n" nil t)
380                (narrow-to-region (point) (point-max)))
381           (gnus-output-to-file filename))))
382     ;; Remember the directory name to save articles.
383     (setq gnus-newsgroup-last-file filename)))
384
385 (defun gnus-summary-save-in-pipe (&optional command)
386   "Pipe this article to subprocess."
387   (interactive)
388   (gnus-set-global-variables)
389   (setq command
390         (cond ((eq command 'default)
391                gnus-last-shell-command)
392               (command command)
393               (t (read-string "Shell command on article: "
394                               gnus-last-shell-command))))
395   (if (string-equal command "")
396       (setq command gnus-last-shell-command))
397   (gnus-eval-in-buffer-window gnus-article-buffer
398     (save-restriction
399       (widen)
400       (shell-command-on-region (point-min) (point-max) command nil)))
401   (setq gnus-last-shell-command command))
402
403 ;;; Article file names when saving.
404
405 (defun gnus-capitalize-newsgroup (newsgroup)
406   "Capitalize NEWSGROUP name."
407   (and (not (zerop (length newsgroup)))
408        (concat (char-to-string (upcase (aref newsgroup 0)))
409                (substring newsgroup 1))))
410
411 (defun gnus-Numeric-save-name (newsgroup headers &optional last-file)
412   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
413 If variable `gnus-use-long-file-name' is nil, it is ~/News/News.group/num.
414 Otherwise, it is like ~/News/news/group/num."
415   (let ((default
416           (expand-file-name
417            (concat (if (gnus-use-long-file-name 'not-save)
418                        (gnus-capitalize-newsgroup newsgroup)
419                      (gnus-newsgroup-directory-form newsgroup))
420                    "/" (int-to-string (mail-header-number headers)))
421            gnus-article-save-directory)))
422     (if (and last-file
423              (string-equal (file-name-directory default)
424                            (file-name-directory last-file))
425              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
426         default
427       (or last-file default))))
428
429 (defun gnus-numeric-save-name (newsgroup headers &optional last-file)
430   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
431 If variable `gnus-use-long-file-name' is non-nil, it is
432 ~/News/news.group/num.  Otherwise, it is like ~/News/news/group/num."
433   (let ((default
434           (expand-file-name
435            (concat (if (gnus-use-long-file-name 'not-save)
436                        newsgroup
437                      (gnus-newsgroup-directory-form newsgroup))
438                    "/" (int-to-string (mail-header-number headers)))
439            gnus-article-save-directory)))
440     (if (and last-file
441              (string-equal (file-name-directory default)
442                            (file-name-directory last-file))
443              (string-match "^[0-9]+$" (file-name-nondirectory last-file)))
444         default
445       (or last-file default))))
446
447 (defun gnus-Plain-save-name (newsgroup headers &optional last-file)
448   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
449 If variable `gnus-use-long-file-name' is non-nil, it is
450 ~/News/News.group.  Otherwise, it is like ~/News/news/group/news."
451   (or last-file
452       (expand-file-name
453        (if (gnus-use-long-file-name 'not-save)
454            (gnus-capitalize-newsgroup newsgroup)
455          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
456        gnus-article-save-directory)))
457
458 (defun gnus-plain-save-name (newsgroup headers &optional last-file)
459   "Generate file name from NEWSGROUP, HEADERS, and optional LAST-FILE.
460 If variable `gnus-use-long-file-name' is non-nil, it is
461 ~/News/news.group.  Otherwise, it is like ~/News/news/group/news."
462   (or last-file
463       (expand-file-name
464        (if (gnus-use-long-file-name 'not-save)
465            newsgroup
466          (concat (gnus-newsgroup-directory-form newsgroup) "/news"))
467        gnus-article-save-directory)))
468
469 \f
470 ;;;
471 ;;; Gnus article mode
472 ;;;
473
474 (put 'gnus-article-mode 'mode-class 'special)
475
476 (when t
477   (gnus-define-keys gnus-article-mode-map
478     " " gnus-article-goto-next-page
479     "\177" gnus-article-goto-prev-page
480     [delete] gnus-article-goto-prev-page
481     "\C-c^" gnus-article-refer-article
482     "h" gnus-article-show-summary
483     "s" gnus-article-show-summary
484     "\C-c\C-m" gnus-article-mail
485     "?" gnus-article-describe-briefly
486     gnus-mouse-2 gnus-article-push-button
487     "\r" gnus-article-press-button
488     "\t" gnus-article-next-button
489     "\M-\t" gnus-article-prev-button
490     "e" gnus-article-edit
491     "<" beginning-of-buffer
492     ">" end-of-buffer
493     "\C-c\C-i" gnus-info-find-node
494     "\C-c\C-b" gnus-bug
495
496     "\C-d" gnus-article-read-summary-keys)
497
498   (substitute-key-definition
499    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
500
501 (defun gnus-article-make-menu-bar ()
502   (gnus-turn-off-edit-menu 'article)
503   (unless (boundp 'gnus-article-article-menu)
504     (easy-menu-define
505      gnus-article-article-menu gnus-article-mode-map ""
506      '("Article"
507        ["Scroll forwards" gnus-article-goto-next-page t]
508        ["Scroll backwards" gnus-article-goto-prev-page t]
509        ["Show summary" gnus-article-show-summary t]
510        ["Fetch Message-ID at point" gnus-article-refer-article t]
511        ["Mail to address at point" gnus-article-mail t]
512        ))
513
514     (easy-menu-define
515      gnus-article-treatment-menu gnus-article-mode-map ""
516      '("Treatment"
517        ["Hide headers" gnus-article-hide-headers t]
518        ["Hide signature" gnus-article-hide-signature t]
519        ["Hide citation" gnus-article-hide-citation t]
520        ["Treat overstrike" gnus-article-treat-overstrike t]
521        ["Remove carriage return" gnus-article-remove-cr t]
522        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
523        ))
524     (run-hooks 'gnus-article-menu-hook)))
525
526 (defun gnus-article-mode ()
527   "Major mode for displaying an article.
528
529 All normal editing commands are switched off.
530
531 The following commands are available in addition to all summary mode
532 commands:
533 \\<gnus-article-mode-map>
534 \\[gnus-article-next-page]\t Scroll the article one page forwards
535 \\[gnus-article-prev-page]\t Scroll the article one page backwards
536 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
537 \\[gnus-article-show-summary]\t Display the summary buffer
538 \\[gnus-article-mail]\t Send a reply to the address near point
539 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
540 \\[gnus-info-find-node]\t Go to the Gnus info node"
541   (interactive)
542   (when (and menu-bar-mode
543              (gnus-visual-p 'article-menu 'menu))
544     (gnus-article-make-menu-bar))
545   (kill-all-local-variables)
546   (gnus-simplify-mode-line)
547   (setq mode-name "Article")
548   (setq major-mode 'gnus-article-mode)
549   (make-local-variable 'minor-mode-alist)
550   (unless (assq 'gnus-show-mime minor-mode-alist)
551     (push (list 'gnus-show-mime " MIME") minor-mode-alist))
552   (use-local-map gnus-article-mode-map)
553   (gnus-update-format-specifications nil 'article-mode)
554   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
555   (gnus-set-default-directory)
556   (buffer-disable-undo (current-buffer))
557   (setq buffer-read-only t)
558   (run-hooks 'gnus-article-mode-hook))
559
560 (defun gnus-article-setup-buffer ()
561   "Initialize the article buffer."
562   (let* ((name (if gnus-single-article-buffer "*Article*"
563                  (concat "*Article " gnus-newsgroup-name "*")))
564          (original
565           (progn (string-match "\\*Article" name)
566                  (concat " *Original Article"
567                          (substring name (match-end 0))))))
568     (setq gnus-article-buffer name)
569     (setq gnus-original-article-buffer original)
570     ;; This might be a variable local to the summary buffer.
571     (unless gnus-single-article-buffer
572       (save-excursion
573         (set-buffer gnus-summary-buffer)
574         (setq gnus-article-buffer name)
575         (setq gnus-original-article-buffer original)
576         (gnus-set-global-variables)))
577     ;; Init original article buffer.
578     (save-excursion
579       (set-buffer (get-buffer-create gnus-original-article-buffer))
580       (buffer-disable-undo (current-buffer))
581       (setq major-mode 'gnus-original-article-mode)
582       (gnus-add-current-to-buffer-list)
583       (make-local-variable 'gnus-original-article))
584     (if (get-buffer name)
585         (save-excursion
586           (set-buffer name)
587           (buffer-disable-undo (current-buffer))
588           (setq buffer-read-only t)
589           (gnus-add-current-to-buffer-list)
590           (unless (eq major-mode 'gnus-article-mode)
591               (gnus-article-mode))
592           (current-buffer))
593       (save-excursion
594         (set-buffer (get-buffer-create name))
595         (gnus-add-current-to-buffer-list)
596         (gnus-article-mode)
597         (make-local-variable 'gnus-summary-buffer)
598         (current-buffer)))))
599
600 ;; Set article window start at LINE, where LINE is the number of lines
601 ;; from the head of the article.
602 (defun gnus-article-set-window-start (&optional line)
603   (set-window-start
604    (get-buffer-window gnus-article-buffer t)
605    (save-excursion
606      (set-buffer gnus-article-buffer)
607      (goto-char (point-min))
608      (if (not line)
609          (point-min)
610        (gnus-message 6 "Moved to bookmark")
611        (search-forward "\n\n" nil t)
612        (forward-line line)
613        (point)))))
614
615 (defun gnus-article-prepare (article &optional all-headers header)
616   "Prepare ARTICLE in article mode buffer.
617 ARTICLE should either be an article number or a Message-ID.
618 If ARTICLE is an id, HEADER should be the article headers.
619 If ALL-HEADERS is non-nil, no headers are hidden."
620   (save-excursion
621     ;; Make sure we start in a summary buffer.
622     (unless (eq major-mode 'gnus-summary-mode)
623       (set-buffer gnus-summary-buffer))
624     (setq gnus-summary-buffer (current-buffer))
625     ;; Make sure the connection to the server is alive.
626     (unless (gnus-server-opened
627              (gnus-find-method-for-group gnus-newsgroup-name))
628       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
629       (gnus-request-group gnus-newsgroup-name t))
630     (let* ((article (if header (mail-header-number header) article))
631            (summary-buffer (current-buffer))
632            (internal-hook gnus-article-internal-prepare-hook)
633            (group gnus-newsgroup-name)
634            result)
635       (save-excursion
636         (gnus-article-setup-buffer)
637         (set-buffer gnus-article-buffer)
638         ;; Deactivate active regions.
639         (when (and (boundp 'transient-mark-mode)
640                    transient-mark-mode)
641           (setq mark-active nil))
642         (if (not (setq result (let ((buffer-read-only nil))
643                                 (gnus-request-article-this-buffer
644                                  article group))))
645             ;; There is no such article.
646             (save-excursion
647               (when (and (numberp article)
648                          (not (memq article gnus-newsgroup-sparse)))
649                 (setq gnus-article-current
650                       (cons gnus-newsgroup-name article))
651                 (set-buffer gnus-summary-buffer)
652                 (setq gnus-current-article article)
653                 (gnus-summary-mark-article article gnus-canceled-mark))
654               (unless (memq article gnus-newsgroup-sparse)
655                 (gnus-error
656                  1 "No such article (may have expired or been canceled)")))
657           (if (or (eq result 'pseudo) (eq result 'nneething))
658               (progn
659                 (save-excursion
660                   (set-buffer summary-buffer)
661                   (setq gnus-last-article gnus-current-article
662                         gnus-newsgroup-history (cons gnus-current-article
663                                                      gnus-newsgroup-history)
664                         gnus-current-article 0
665                         gnus-current-headers nil
666                         gnus-article-current nil)
667                   (if (eq result 'nneething)
668                       (gnus-configure-windows 'summary)
669                     (gnus-configure-windows 'article))
670                   (gnus-set-global-variables))
671                 (gnus-set-mode-line 'article))
672             ;; The result from the `request' was an actual article -
673             ;; or at least some text that is now displayed in the
674             ;; article buffer.
675             (if (and (numberp article)
676                      (not (eq article gnus-current-article)))
677                 ;; Seems like a new article has been selected.
678                 ;; `gnus-current-article' must be an article number.
679                 (save-excursion
680                   (set-buffer summary-buffer)
681                   (setq gnus-last-article gnus-current-article
682                         gnus-newsgroup-history (cons gnus-current-article
683                                                      gnus-newsgroup-history)
684                         gnus-current-article article
685                         gnus-current-headers
686                         (gnus-summary-article-header gnus-current-article)
687                         gnus-article-current
688                         (cons gnus-newsgroup-name gnus-current-article))
689                   (unless (vectorp gnus-current-headers)
690                     (setq gnus-current-headers nil))
691                   (gnus-summary-show-thread)
692                   (run-hooks 'gnus-mark-article-hook)
693                   (gnus-set-mode-line 'summary)
694                   (and (gnus-visual-p 'article-highlight 'highlight)
695                        (run-hooks 'gnus-visual-mark-article-hook))
696                   ;; Set the global newsgroup variables here.
697                   ;; Suggested by Jim Sisolak
698                   ;; <sisolak@trans4.neep.wisc.edu>.
699                   (gnus-set-global-variables)
700                   (setq gnus-have-all-headers
701                         (or all-headers gnus-show-all-headers))
702                   (and gnus-use-cache
703                        (vectorp (gnus-summary-article-header article))
704                        (gnus-cache-possibly-enter-article
705                         group article
706                         (gnus-summary-article-header article)
707                         (memq article gnus-newsgroup-marked)
708                         (memq article gnus-newsgroup-dormant)
709                         (memq article gnus-newsgroup-unreads)))))
710             (when (or (numberp article)
711                       (stringp article))
712               ;; Hooks for getting information from the article.
713               ;; This hook must be called before being narrowed.
714               (let (buffer-read-only)
715                 (run-hooks 'internal-hook)
716                 (run-hooks 'gnus-article-prepare-hook)
717                 ;; Decode MIME message.
718                 (if gnus-show-mime
719                     (if (or (not gnus-strict-mime)
720                             (gnus-fetch-field "Mime-Version"))
721                         (funcall gnus-show-mime-method)
722                       (funcall gnus-decode-encoded-word-method)))
723                 ;; Perform the article display hooks.
724                 (run-hooks 'gnus-article-display-hook))
725               ;; Do page break.
726               (goto-char (point-min))
727               (and gnus-break-pages (gnus-narrow-to-page)))
728             (gnus-set-mode-line 'article)
729             (gnus-configure-windows 'article)
730             (goto-char (point-min))
731             t))))))
732
733 (defun gnus-article-wash-status ()
734   "Return a string which display status of article washing."
735   (save-excursion
736     (set-buffer gnus-article-buffer)
737     (let ((cite (article-hidden-text-p 'cite))
738           (headers (article-hidden-text-p 'headers))
739           (boring (article-hidden-text-p 'boring-headers))
740           (pgp (article-hidden-text-p 'pgp))
741           (pem (article-hidden-text-p 'pem))
742           (signature (article-hidden-text-p 'signature))
743           (overstrike (article-hidden-text-p 'overstrike))
744           (emphasis (article-hidden-text-p 'emphasis))
745           (mime gnus-show-mime))
746       (format "%c%c%c%c%c%c%c"
747               (if cite ?c ? )
748               (if (or headers boring) ?h ? )
749               (if (or pgp pem) ?p ? )
750               (if signature ?s ? )
751               (if overstrike ?o ? )
752               (if mime ?m ? )
753               (if emphasis ?e ? )))))
754
755 (defun gnus-article-hide-headers-if-wanted ()
756   "Hide unwanted headers if `gnus-have-all-headers' is nil.
757 Provided for backwards compatibility."
758   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
759       gnus-inhibit-hiding
760       (gnus-article-hide-headers)))
761
762 ;;; Article savers.
763
764 (defun gnus-output-to-rmail (file-name)
765   "Append the current article to an Rmail file named FILE-NAME."
766   (require 'rmail)
767   ;; Most of these codes are borrowed from rmailout.el.
768   (setq file-name (expand-file-name file-name))
769   (setq rmail-default-rmail-file file-name)
770   (let ((artbuf (current-buffer))
771         (tmpbuf (get-buffer-create " *Gnus-output*")))
772     (save-excursion
773       (or (get-file-buffer file-name)
774           (file-exists-p file-name)
775           (if (gnus-yes-or-no-p
776                (concat "\"" file-name "\" does not exist, create it? "))
777               (let ((file-buffer (create-file-buffer file-name)))
778                 (save-excursion
779                   (set-buffer file-buffer)
780                   (rmail-insert-rmail-file-header)
781                   (let ((require-final-newline nil))
782                     (write-region (point-min) (point-max) file-name t 1)))
783                 (kill-buffer file-buffer))
784             (error "Output file does not exist")))
785       (set-buffer tmpbuf)
786       (buffer-disable-undo (current-buffer))
787       (erase-buffer)
788       (insert-buffer-substring artbuf)
789       (gnus-convert-article-to-rmail)
790       ;; Decide whether to append to a file or to an Emacs buffer.
791       (let ((outbuf (get-file-buffer file-name)))
792         (if (not outbuf)
793             (append-to-file (point-min) (point-max) file-name)
794           ;; File has been visited, in buffer OUTBUF.
795           (set-buffer outbuf)
796           (let ((buffer-read-only nil)
797                 (msg (and (boundp 'rmail-current-message)
798                           (symbol-value 'rmail-current-message))))
799             ;; If MSG is non-nil, buffer is in RMAIL mode.
800             (if msg
801                 (progn (widen)
802                        (narrow-to-region (point-max) (point-max))))
803             (insert-buffer-substring tmpbuf)
804             (if msg
805                 (progn
806                   (goto-char (point-min))
807                   (widen)
808                   (search-backward "\^_")
809                   (narrow-to-region (point) (point-max))
810                   (goto-char (1+ (point-min)))
811                   (rmail-count-new-messages t)
812                   (rmail-show-message msg)))))))
813     (kill-buffer tmpbuf)))
814
815 (defun gnus-output-to-file (file-name)
816   "Append the current article to a file named FILE-NAME."
817   (let ((artbuf (current-buffer)))
818     (nnheader-temp-write nil
819       (insert-buffer-substring artbuf)
820       ;; Append newline at end of the buffer as separator, and then
821       ;; save it to file.
822       (goto-char (point-max))
823       (insert "\n")
824       (append-to-file (point-min) (point-max) file-name))))
825
826 (defun gnus-convert-article-to-rmail ()
827   "Convert article in current buffer to Rmail message format."
828   (let ((buffer-read-only nil))
829     ;; Convert article directly into Babyl format.
830     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
831     (goto-char (point-min))
832     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
833     (while (search-forward "\n\^_" nil t) ;single char
834       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
835     (goto-char (point-max))
836     (insert "\^_")))
837
838 (defun gnus-narrow-to-page (&optional arg)
839   "Narrow the article buffer to a page.
840 If given a numerical ARG, move forward ARG pages."
841   (interactive "P")
842   (setq arg (if arg (prefix-numeric-value arg) 0))
843   (save-excursion
844     (set-buffer gnus-article-buffer)
845     (goto-char (point-min))
846     (widen)
847     ;; Remove any old next/prev buttons.
848     (when (gnus-visual-p 'page-marker)
849       (let ((buffer-read-only nil))
850         (gnus-remove-text-with-property 'gnus-prev)
851         (gnus-remove-text-with-property 'gnus-next)))
852     (when
853         (cond ((< arg 0)
854                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
855               ((> arg 0)
856                (re-search-forward page-delimiter nil 'move arg)))
857       (goto-char (match-end 0)))
858     (narrow-to-region
859      (point)
860      (if (re-search-forward page-delimiter nil 'move)
861          (match-beginning 0)
862        (point)))
863     (when (and (gnus-visual-p 'page-marker)
864                (not (= (point-min) 1)))
865       (save-excursion
866         (goto-char (point-min))
867         (gnus-insert-prev-page-button)))
868     (when (and (gnus-visual-p 'page-marker)
869                (< (+ (point-max) 2) (buffer-size)))
870       (save-excursion
871         (goto-char (point-max))
872         (gnus-insert-next-page-button)))))
873
874 ;; Article mode commands
875
876 (defun gnus-article-goto-next-page ()
877   "Show the next page of the article."
878   (interactive)
879   (when (gnus-article-next-page)
880     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
881
882 (defun gnus-article-goto-prev-page ()
883   "Show the next page of the article."
884   (interactive)
885   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
886     (gnus-article-prev-page nil)))
887
888 (defun gnus-article-next-page (&optional lines)
889   "Show the next page of the current article.
890 If end of article, return non-nil.  Otherwise return nil.
891 Argument LINES specifies lines to be scrolled up."
892   (interactive "p")
893   (move-to-window-line -1)
894   (if (save-excursion
895         (end-of-line)
896         (and (pos-visible-in-window-p)  ;Not continuation line.
897              (eobp)))
898       ;; Nothing in this page.
899       (if (or (not gnus-break-pages)
900               (save-excursion
901                 (save-restriction
902                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
903           t                             ;Nothing more.
904         (gnus-narrow-to-page 1)         ;Go to next page.
905         nil)
906     ;; More in this page.
907     (condition-case ()
908         (scroll-up lines)
909       (end-of-buffer
910        ;; Long lines may cause an end-of-buffer error.
911        (goto-char (point-max))))
912     (move-to-window-line 0)
913     nil))
914
915 (defun gnus-article-prev-page (&optional lines)
916   "Show previous page of current article.
917 Argument LINES specifies lines to be scrolled down."
918   (interactive "p")
919   (move-to-window-line 0)
920   (if (and gnus-break-pages
921            (bobp)
922            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
923       (progn
924         (gnus-narrow-to-page -1)        ;Go to previous page.
925         (goto-char (point-max))
926         (recenter -1))
927     (prog1
928         (condition-case ()
929             (scroll-down lines)
930           (error nil))
931       (move-to-window-line 0))))
932
933 (defun gnus-article-refer-article ()
934   "Read article specified by message-id around point."
935   (interactive)
936   (let ((point (point)))
937     (search-forward ">" nil t)          ;Move point to end of "<....>".
938     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
939         (let ((message-id (match-string 1)))
940           (goto-char point)
941           (set-buffer gnus-summary-buffer)
942           (gnus-summary-refer-article message-id))
943       (goto-char (point))
944       (error "No references around point"))))
945
946 (defun gnus-article-show-summary ()
947   "Reconfigure windows to show summary buffer."
948   (interactive)
949   (gnus-configure-windows 'article)
950   (gnus-summary-goto-subject gnus-current-article))
951
952 (defun gnus-article-describe-briefly ()
953   "Describe article mode commands briefly."
954   (interactive)
955   (gnus-message 6
956                 (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")))
957
958 (defun gnus-article-summary-command ()
959   "Execute the last keystroke in the summary buffer."
960   (interactive)
961   (let ((obuf (current-buffer))
962         (owin (current-window-configuration))
963         func)
964     (switch-to-buffer gnus-summary-buffer 'norecord)
965     (setq func (lookup-key (current-local-map) (this-command-keys)))
966     (call-interactively func)
967     (set-buffer obuf)
968     (set-window-configuration owin)
969     (set-window-point (get-buffer-window (current-buffer)) (point))))
970
971 (defun gnus-article-summary-command-nosave ()
972   "Execute the last keystroke in the summary buffer."
973   (interactive)
974   (let (func)
975     (pop-to-buffer gnus-summary-buffer 'norecord)
976     (setq func (lookup-key (current-local-map) (this-command-keys)))
977     (call-interactively func)))
978
979 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
980   "Read a summary buffer key sequence and execute it from the article buffer."
981   (interactive "P")
982   (let ((nosaves
983          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
984            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
985            "=" "^" "\M-^" "|"))
986         (nosave-but-article
987          '("A\r"))
988         (nosave-in-article
989          '("\C-d"))
990         keys)
991     (save-excursion
992       (set-buffer gnus-summary-buffer)
993       (push (or key last-command-event) unread-command-events)
994       (setq keys (read-key-sequence nil)))
995     (message "")
996
997     (if (or (member keys nosaves)
998             (member keys nosave-but-article)
999             (member keys nosave-in-article))
1000         (let (func)
1001           (save-window-excursion
1002             (pop-to-buffer gnus-summary-buffer 'norecord)
1003             (setq func (lookup-key (current-local-map) keys)))
1004           (if (not func)
1005               (ding)
1006             (unless (member keys nosave-in-article)
1007               (set-buffer gnus-summary-buffer))
1008             (call-interactively func))
1009           (when (member keys nosave-but-article)
1010             (pop-to-buffer gnus-article-buffer 'norecord)))
1011       ;; These commands should restore window configuration.
1012       (let ((obuf (current-buffer))
1013             (owin (current-window-configuration))
1014             (opoint (point))
1015             func in-buffer)
1016         (if not-restore-window
1017             (pop-to-buffer gnus-summary-buffer 'norecord)
1018           (switch-to-buffer gnus-summary-buffer 'norecord))
1019         (setq in-buffer (current-buffer))
1020         (if (setq func (lookup-key (current-local-map) keys))
1021             (call-interactively func)
1022           (ding))
1023         (when (eq in-buffer (current-buffer))
1024           (set-buffer obuf)
1025           (unless not-restore-window
1026             (set-window-configuration owin))
1027           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
1028
1029 (defun gnus-article-hide (&optional arg force)
1030   "Hide all the gruft in the current article.
1031 This means that PGP stuff, signatures, cited text and (some)
1032 headers will be hidden.
1033 If given a prefix, show the hidden text instead."
1034   (interactive (list current-prefix-arg 'force))
1035   (gnus-article-hide-headers arg)
1036   (gnus-article-hide-pgp arg)
1037   (gnus-article-hide-citation-maybe arg force)
1038   (gnus-article-hide-signature arg))
1039
1040 (defun gnus-article-maybe-highlight ()
1041   "Do some article highlighting if `article-visual' is non-nil."
1042   (if (gnus-visual-p 'article-highlight 'highlight)
1043       (gnus-article-highlight-some)))
1044
1045 (defun gnus-request-article-this-buffer (article group)
1046   "Get an article and insert it into this buffer."
1047   (let (do-update-line)
1048     (prog1
1049         (save-excursion
1050           (erase-buffer)
1051           (gnus-kill-all-overlays)
1052           (setq group (or group gnus-newsgroup-name))
1053
1054           ;; Open server if it has closed.
1055           (gnus-check-server (gnus-find-method-for-group group))
1056
1057           ;; Using `gnus-request-article' directly will insert the article into
1058           ;; `nntp-server-buffer' - so we'll save some time by not having to
1059           ;; copy it from the server buffer into the article buffer.
1060
1061           ;; We only request an article by message-id when we do not have the
1062           ;; headers for it, so we'll have to get those.
1063           (when (stringp article)
1064             (let ((gnus-override-method gnus-refer-article-method))
1065               (gnus-read-header article)))
1066
1067           ;; If the article number is negative, that means that this article
1068           ;; doesn't belong in this newsgroup (possibly), so we find its
1069           ;; message-id and request it by id instead of number.
1070           (when (and (numberp article)
1071                      gnus-summary-buffer
1072                      (get-buffer gnus-summary-buffer)
1073                      (buffer-name (get-buffer gnus-summary-buffer)))
1074             (save-excursion
1075               (set-buffer gnus-summary-buffer)
1076               (let ((header (gnus-summary-article-header article)))
1077                 (if (< article 0)
1078                     (cond 
1079                      ((memq article gnus-newsgroup-sparse)
1080                       ;; This is a sparse gap article.
1081                       (setq do-update-line article)
1082                       (setq article (mail-header-id header))
1083                       (let ((gnus-override-method gnus-refer-article-method))
1084                         (gnus-read-header article))
1085                       (setq gnus-newsgroup-sparse
1086                             (delq article gnus-newsgroup-sparse)))
1087                      ((vectorp header)
1088                       ;; It's a real article.
1089                       (setq article (mail-header-id header)))
1090                      (t
1091                       ;; It is an extracted pseudo-article.
1092                       (setq article 'pseudo)
1093                       (gnus-request-pseudo-article header))))
1094                 
1095                 (let ((method (gnus-find-method-for-group 
1096                                gnus-newsgroup-name)))
1097                   (if (not (eq (car method) 'nneething))
1098                       ()
1099                     (let ((dir (concat (file-name-as-directory (nth 1 method))
1100                                        (mail-header-subject header))))
1101                       (if (file-directory-p dir)
1102                           (progn
1103                             (setq article 'nneething)
1104                             (gnus-group-enter-directory dir)))))))))
1105
1106           (cond
1107            ;; Refuse to select canceled articles.
1108            ((and (numberp article)
1109                  gnus-summary-buffer
1110                  (get-buffer gnus-summary-buffer)
1111                  (buffer-name (get-buffer gnus-summary-buffer))
1112                  (eq (cdr (save-excursion
1113                             (set-buffer gnus-summary-buffer)
1114                             (assq article gnus-newsgroup-reads)))
1115                      gnus-canceled-mark))
1116             nil)
1117            ;; We first check `gnus-original-article-buffer'.
1118            ((and (get-buffer gnus-original-article-buffer)
1119                  (numberp article)
1120                  (save-excursion
1121                    (set-buffer gnus-original-article-buffer)
1122                    (and (equal (car gnus-original-article) group)
1123                         (eq (cdr gnus-original-article) article))))
1124             (insert-buffer-substring gnus-original-article-buffer)
1125             'article)
1126            ;; Check the backlog.
1127            ((and gnus-keep-backlog
1128                  (gnus-backlog-request-article group article (current-buffer)))
1129             'article)
1130            ;; Check asynchronous pre-fetch.
1131            ((gnus-async-request-fetched-article group article (current-buffer))
1132             (gnus-async-prefetch-next group article gnus-summary-buffer)
1133             'article)
1134            ;; Check the cache.
1135            ((and gnus-use-cache
1136                  (numberp article)
1137                  (gnus-cache-request-article article group))
1138             'article)
1139            ;; Get the article and put into the article buffer.
1140            ((or (stringp article) (numberp article))
1141             (let ((gnus-override-method
1142                    (and (stringp article) gnus-refer-article-method))
1143                   (buffer-read-only nil))
1144               (erase-buffer)
1145               (gnus-kill-all-overlays)
1146               (when (gnus-request-article article group (current-buffer))
1147                 (when (numberp article)
1148                   (gnus-async-prefetch-next group article gnus-summary-buffer)
1149                   (when gnus-keep-backlog
1150                     (gnus-backlog-enter-article 
1151                      group article (current-buffer))))
1152                 'article)))
1153            ;; It was a pseudo.
1154            (t article)))
1155
1156       ;; Take the article from the original article buffer
1157       ;; and place it in the buffer it's supposed to be in.
1158       (when (and (get-buffer gnus-article-buffer)
1159                  ;;(numberp article)
1160                  (equal (buffer-name (current-buffer))
1161                         (buffer-name (get-buffer gnus-article-buffer))))
1162         (save-excursion
1163           (if (get-buffer gnus-original-article-buffer)
1164               (set-buffer (get-buffer gnus-original-article-buffer))
1165             (set-buffer (get-buffer-create gnus-original-article-buffer))
1166             (buffer-disable-undo (current-buffer))
1167             (setq major-mode 'gnus-original-article-mode)
1168             (setq buffer-read-only t)
1169             (gnus-add-current-to-buffer-list))
1170           (let (buffer-read-only)
1171             (erase-buffer)
1172             (insert-buffer-substring gnus-article-buffer))
1173           (setq gnus-original-article (cons group article))))
1174     
1175       ;; Update sparse articles.
1176       (when (and do-update-line
1177                  (or (numberp article)
1178                      (stringp article)))
1179         (let ((buf (current-buffer)))
1180           (set-buffer gnus-summary-buffer)
1181           (gnus-summary-update-article do-update-line)
1182           (gnus-summary-goto-subject do-update-line nil t)
1183           (set-window-point (get-buffer-window (current-buffer) t)
1184                             (point))
1185           (set-buffer buf))))))
1186
1187 (defun gnus-article-date-ut (&optional type highlight)
1188   "Convert DATE date to universal time in the current article.
1189 If TYPE is `local', convert to local time; if it is `lapsed', output
1190 how much time has lapsed since DATE."
1191   (interactive (list 'ut t))
1192   (let ((headers (or gnus-current-headers (gnus-summary-article-header))))
1193     (save-excursion
1194       (set-buffer gnus-article-buffer)
1195       (article-date-ut type highlight headers))))
1196
1197 ;;;
1198 ;;; Article editing
1199 ;;;
1200
1201 (defvar gnus-article-edit-mode-hook nil
1202   "*Hook run in article edit mode buffers.")
1203
1204 (defvar gnus-article-edit-done-function nil)
1205
1206 (defvar gnus-article-edit-mode-map nil)
1207
1208 (unless gnus-article-edit-mode-map 
1209   (setq gnus-article-edit-mode-map (copy-keymap text-mode-map))
1210
1211   (gnus-define-keys gnus-article-edit-mode-map
1212     "\C-c\C-c" gnus-article-edit-done
1213     "\C-c\C-k" gnus-article-edit-exit)
1214
1215   (gnus-define-keys (gnus-article-edit-wash-map
1216                      "\C-c\C-w" gnus-article-edit-mode-map)
1217     "f" gnus-article-edit-full-stops))
1218
1219 (defun gnus-article-edit-mode ()
1220   "Major mode for editing articles.
1221 This is an extended text-mode.
1222
1223 \\{gnus-article-edit-mode-map}"
1224   (interactive)
1225   (kill-all-local-variables)
1226   (setq major-mode 'gnus-article-edit-mode)
1227   (setq mode-name "Article Edit")
1228   (use-local-map gnus-article-edit-mode-map)
1229   (make-local-variable 'gnus-article-edit-done-function)
1230   (make-local-variable 'gnus-prev-winconf)
1231   (setq buffer-read-only nil)
1232   (buffer-enable-undo)
1233   (widen)
1234   (run-hooks 'text-mode 'gnus-article-edit-mode-hook))
1235
1236 (defun gnus-article-edit (&optional force)
1237   "Edit the current article.
1238 This will have permanent effect only in mail groups.
1239 If FORCE is non-nil, allow editing of articles even in read-only
1240 groups."
1241   (interactive "P")
1242   (when (and (not force)
1243              (gnus-group-read-only-p))
1244     (error "The current newsgroup does not support article editing."))
1245   (gnus-article-edit-article
1246    `(lambda ()
1247       (gnus-summary-edit-article-done
1248        ,(or (mail-header-references gnus-current-headers) "")
1249        ,(gnus-group-read-only-p) ,gnus-summary-buffer))))
1250
1251 (defun gnus-article-edit-article (exit-func)
1252   "Start editing the contents of the current article buffer."
1253   (let ((winconf (current-window-configuration)))
1254     (set-buffer gnus-article-buffer)
1255     (gnus-article-edit-mode)
1256     (set-text-properties (point-min) (point-max) nil)
1257     (gnus-configure-windows 'edit-article)
1258     (setq gnus-article-edit-done-function exit-func)
1259     (setq gnus-prev-winconf winconf)
1260     (gnus-message 6 "C-c C-c to end edits")))
1261
1262 (defun gnus-article-edit-done ()
1263   "Update the article edits and exit."
1264   (interactive)
1265   (let ((func gnus-article-edit-done-function)
1266         (buf (current-buffer))
1267         (start (window-start)))
1268     (gnus-article-edit-exit)
1269     (save-excursion
1270       (set-buffer buf)
1271       (let ((buffer-read-only nil))
1272         (funcall func)))
1273     (set-buffer buf)
1274     (set-window-start (get-buffer-window buf) start)
1275     (set-window-point (get-buffer-window buf) (point))))
1276
1277 (defun gnus-article-edit-exit ()
1278   "Exit the article editing without updating."
1279   (interactive)
1280   ;; We remove all text props from the article buffer.
1281   (let ((buf (format "%s" (buffer-string)))
1282         (curbuf (current-buffer))
1283         (p (point))
1284         (window-start (window-start)))
1285     (erase-buffer)
1286     (insert buf)
1287     (let ((winconf gnus-prev-winconf))
1288       (gnus-article-mode)
1289       ;; The cache and backlog have to be flushed somewhat.
1290       (when gnus-use-cache
1291         (gnus-cache-update-article      
1292          (car gnus-article-current) (cdr gnus-article-current)))
1293       (when gnus-keep-backlog
1294         (gnus-backlog-remove-article 
1295          (car gnus-article-current) (cdr gnus-article-current)))
1296       ;; Flush original article as well.
1297       (save-excursion
1298         (when (get-buffer gnus-original-article-buffer)
1299           (set-buffer gnus-original-article-buffer)
1300           (setq gnus-original-article nil)))
1301       (set-window-configuration winconf)
1302       ;; Tippy-toe some to make sure that point remains where it was.
1303       (let ((buf (current-buffer)))
1304         (set-buffer curbuf)
1305         (set-window-start (get-buffer-window (current-buffer)) window-start)
1306         (goto-char p)
1307         (set-buffer buf)))))
1308       
1309 (defun gnus-article-edit-full-stops ()
1310   "Interactively repair spacing at end of sentences."
1311   (interactive)
1312   (save-excursion
1313     (goto-char (point-min))
1314     (search-forward-regexp "^$" nil t)
1315     (let ((case-fold-search nil))
1316       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
1317
1318 ;;; 
1319 ;;; Article highlights
1320 ;;;
1321
1322 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
1323
1324 ;;; Internal Variables:
1325
1326 (defvar gnus-button-url-regexp "\\b\\(s?https?\\|ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?[-\\wa-zA-Z0-9_=!?#$@~`%&*+|\\/.,]*[-\\wa-zA-Z0-9_=#$@~`%&*+|\\/]"
1327   "*Regular expression that matches URLs.")
1328
1329 (defvar gnus-button-alist 
1330   `(("\\bin\\( +article\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2 
1331      t gnus-button-message-id 3)
1332     ("\\(<?\\(url: ?\\)?news:\\([^>\n\t ]*\\)>?\\)" 1 t
1333      gnus-button-message-id 3)
1334     ("\\(<URL: *\\)?mailto: *\\([^> \n\t]+\\)>?" 0 t gnus-button-reply 2)
1335     ;; This is how URLs _should_ be embedded in text...
1336     ("<URL: *\\([^\n\r>]*\\)>" 0 t gnus-button-url 1)
1337     ;; Next regexp stolen from highlight-headers.el.
1338     ;; Modified by Vladimir Alexiev.
1339     (,gnus-button-url-regexp 0 t gnus-button-url 0))
1340   "Alist of regexps matching buttons in article bodies.
1341
1342 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
1343 REGEXP: is the string matching text around the button,
1344 BUTTON: is the number of the regexp grouping actually matching the button,
1345 FORM: is a lisp expression which must eval to true for the button to
1346 be added, 
1347 CALLBACK: is the function to call when the user push this button, and each
1348 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
1349
1350 CALLBACK can also be a variable, in that case the value of that
1351 variable it the real callback function.")
1352
1353 (defvar gnus-header-button-alist 
1354   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
1355      0 t gnus-button-message-id 0)
1356     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
1357     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+" 
1358      0 t gnus-button-mailto 0)
1359     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
1360     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
1361     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
1362      gnus-button-message-id 3))
1363   "Alist of headers and regexps to match buttons in article heads.
1364
1365 This alist is very similar to `gnus-button-alist', except that each
1366 alist has an additional HEADER element first in each entry:
1367
1368 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
1369
1370 HEADER is a regexp to match a header.  For a fuller explanation, see
1371 `gnus-button-alist'.")
1372
1373 (defvar gnus-button-regexp nil)
1374 (defvar gnus-button-marker-list nil)
1375 ;; Regexp matching any of the regexps from `gnus-button-alist'.
1376
1377 (defvar gnus-button-last nil)
1378 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
1379
1380 ;;; Commands:
1381
1382 (defun gnus-article-push-button (event)
1383   "Check text under the mouse pointer for a callback function.
1384 If the text under the mouse pointer has a `gnus-callback' property,
1385 call it with the value of the `gnus-data' text property."
1386   (interactive "e")
1387   (set-buffer (window-buffer (posn-window (event-start event))))
1388   (let* ((pos (posn-point (event-start event)))
1389          (data (get-text-property pos 'gnus-data))
1390          (fun (get-text-property pos 'gnus-callback)))
1391     (if fun (funcall fun data))))
1392
1393 (defun gnus-article-press-button ()
1394   "Check text at point for a callback function.
1395 If the text at point has a `gnus-callback' property,
1396 call it with the value of the `gnus-data' text property."
1397   (interactive)
1398   (let* ((data (get-text-property (point) 'gnus-data))
1399          (fun (get-text-property (point) 'gnus-callback)))
1400     (if fun (funcall fun data))))
1401
1402 (defun gnus-article-prev-button (n)
1403   "Move point to N buttons backward.
1404 If N is negative, move forward instead."
1405   (interactive "p")
1406   (gnus-article-next-button (- n)))
1407
1408 (defun gnus-article-next-button (n)
1409   "Move point to N buttons forward.
1410 If N is negative, move backward instead."
1411   (interactive "p")
1412   (let ((function (if (< n 0) 'previous-single-property-change
1413                     'next-single-property-change))
1414         (inhibit-point-motion-hooks t)
1415         (backward (< n 0))
1416         (limit (if (< n 0) (point-min) (point-max))))
1417     (setq n (abs n))
1418     (while (and (not (= limit (point)))
1419                 (> n 0))
1420       ;; Skip past the current button.
1421       (when (get-text-property (point) 'gnus-callback)
1422         (goto-char (funcall function (point) 'gnus-callback nil limit)))
1423       ;; Go to the next (or previous) button.
1424       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
1425       ;; Put point at the start of the button.
1426       (when (and backward (not (get-text-property (point) 'gnus-callback)))
1427         (goto-char (funcall function (point) 'gnus-callback nil limit)))
1428       ;; Skip past intangible buttons.
1429       (when (get-text-property (point) 'intangible)
1430         (incf n))
1431       (decf n))
1432     (unless (zerop n)
1433       (gnus-message 5 "No more buttons"))
1434     n))
1435
1436 (defun gnus-article-highlight (&optional force)
1437   "Highlight current article.
1438 This function calls `gnus-article-highlight-headers',
1439 `gnus-article-highlight-citation', 
1440 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
1441 do the highlighting.  See the documentation for those functions."
1442   (interactive (list 'force))
1443   (gnus-article-highlight-headers)
1444   (gnus-article-highlight-citation force)
1445   (gnus-article-highlight-signature)
1446   (gnus-article-add-buttons force)
1447   (gnus-article-add-buttons-to-head))
1448
1449 (defun gnus-article-highlight-some (&optional force)
1450   "Highlight current article.
1451 This function calls `gnus-article-highlight-headers',
1452 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
1453 do the highlighting.  See the documentation for those functions."
1454   (interactive (list 'force))
1455   (gnus-article-highlight-headers)
1456   (gnus-article-highlight-signature)
1457   (gnus-article-add-buttons))
1458
1459 (defun gnus-article-highlight-headers ()
1460   "Highlight article headers as specified by `gnus-header-face-alist'."
1461   (interactive)
1462   (save-excursion
1463     (set-buffer gnus-article-buffer)
1464     (save-restriction
1465       (let ((alist gnus-header-face-alist)
1466             (buffer-read-only nil)
1467             (case-fold-search t)
1468             (inhibit-point-motion-hooks t)
1469             entry regexp header-face field-face from hpoints fpoints)
1470         (goto-char (point-min))
1471         (when (search-forward "\n\n" nil t)
1472           (narrow-to-region (1- (point)) (point-min))
1473           (while (setq entry (pop alist))
1474             (goto-char (point-min))
1475             (setq regexp (concat "^\\("
1476                                  (if (string-equal "" (nth 0 entry))
1477                                      "[^\t ]"
1478                                    (nth 0 entry))
1479                                  "\\)")
1480                   header-face (nth 1 entry)
1481                   field-face (nth 2 entry))
1482             (while (and (re-search-forward regexp nil t)
1483                         (not (eobp)))
1484               (beginning-of-line)
1485               (setq from (point))
1486               (or (search-forward ":" nil t)
1487                   (forward-char 1))
1488               (when (and header-face
1489                          (not (memq (point) hpoints)))
1490                 (push (point) hpoints)
1491                 (gnus-put-text-property from (point) 'face header-face))
1492               (when (and field-face
1493                          (not (memq (setq from (point)) fpoints)))
1494                 (push from fpoints)
1495                 (if (re-search-forward "^[^ \t]" nil t)
1496                     (forward-char -2)
1497                   (goto-char (point-max)))
1498                 (gnus-put-text-property from (point) 'face field-face)))))))))
1499
1500 (defun gnus-article-highlight-signature ()
1501   "Highlight the signature in an article.
1502 It does this by highlighting everything after
1503 `gnus-signature-separator' using `gnus-signature-face'." 
1504   (interactive)
1505   (save-excursion
1506     (set-buffer gnus-article-buffer)
1507     (let ((buffer-read-only nil)
1508           (inhibit-point-motion-hooks t))
1509       (save-restriction
1510         (when (and gnus-signature-face
1511                    (article-narrow-to-signature))
1512           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
1513                             'face gnus-signature-face)
1514           (widen)
1515           (article-search-signature)
1516           (let ((start (match-beginning 0))
1517                 (end (set-marker (make-marker) (1+ (match-end 0)))))
1518             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
1519                                      end)))))))
1520
1521 (defun gnus-article-add-buttons (&optional force)
1522   "Find external references in the article and make buttons of them.
1523 \"External references\" are things like Message-IDs and URLs, as
1524 specified by `gnus-button-alist'."
1525   (interactive (list 'force))
1526   (save-excursion
1527     (set-buffer gnus-article-buffer)
1528     ;; Remove all old markers.
1529     (while gnus-button-marker-list
1530       (set-marker (pop gnus-button-marker-list) nil))
1531     (let ((buffer-read-only nil)
1532           (inhibit-point-motion-hooks t)
1533           (case-fold-search t)
1534           (alist gnus-button-alist)
1535           beg entry regexp)
1536       (goto-char (point-min))
1537       ;; We skip the headers.
1538       (unless (search-forward "\n\n" nil t)
1539         (goto-char (point-max)))
1540       (setq beg (point))
1541       (while (setq entry (pop alist))
1542         (setq regexp (car entry))
1543         (goto-char beg)
1544         (while (re-search-forward regexp nil t)
1545           (let* ((start (and entry (match-beginning (nth 1 entry))))
1546                  (end (and entry (match-end (nth 1 entry))))
1547                  (from (match-beginning 0)))
1548             (when (or (eq t (nth 1 entry))
1549                       (eval (nth 1 entry)))
1550               ;; That optional form returned non-nil, so we add the
1551               ;; button. 
1552               (gnus-article-add-button 
1553                start end 'gnus-button-push 
1554                (car (push (set-marker (make-marker) from)
1555                           gnus-button-marker-list))))))))))
1556
1557 ;; Add buttons to the head of an article.
1558 (defun gnus-article-add-buttons-to-head ()
1559   "Add buttons to the head of the article."
1560   (interactive)
1561   (save-excursion
1562     (set-buffer gnus-article-buffer)
1563     (let ((buffer-read-only nil)
1564           (inhibit-point-motion-hooks t)
1565           (case-fold-search t)
1566           (alist gnus-header-button-alist)
1567           entry beg end)
1568       (nnheader-narrow-to-headers)
1569       (while alist
1570         ;; Each alist entry.
1571         (setq entry (car alist)
1572               alist (cdr alist))
1573         (goto-char (point-min))
1574         (while (re-search-forward (car entry) nil t)
1575           ;; Each header matching the entry.
1576           (setq beg (match-beginning 0))
1577           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
1578                              (match-beginning 0))
1579                         (point-max)))
1580           (goto-char beg)
1581           (while (re-search-forward (nth 1 entry) end t)
1582             ;; Each match within a header.
1583             (let* ((entry (cdr entry))
1584                    (start (match-beginning (nth 1 entry)))
1585                    (end (match-end (nth 1 entry)))
1586                    (form (nth 2 entry)))
1587               (goto-char (match-end 0))
1588               (and (eval form)
1589                    (gnus-article-add-button 
1590                     start end (nth 3 entry)
1591                     (buffer-substring (match-beginning (nth 4 entry))
1592                                       (match-end (nth 4 entry)))))))
1593           (goto-char end))))
1594     (widen)))
1595
1596 ;;; External functions:
1597
1598 (defun gnus-article-add-button (from to fun &optional data)
1599   "Create a button between FROM and TO with callback FUN and data DATA."
1600   (and gnus-article-button-face
1601        (gnus-overlay-put (gnus-make-overlay from to)
1602                          'face gnus-article-button-face))
1603   (gnus-add-text-properties 
1604    from to
1605    (nconc (and gnus-article-mouse-face
1606                (list gnus-mouse-face-prop gnus-article-mouse-face))
1607           (list 'gnus-callback fun)
1608           (and data (list 'gnus-data data)))))
1609
1610 ;;; Internal functions:
1611
1612 (defun gnus-signature-toggle (end)
1613   (save-excursion
1614     (set-buffer gnus-article-buffer)
1615     (let ((buffer-read-only nil)
1616           (inhibit-point-motion-hooks t))
1617       (if (get-text-property end 'invisible)
1618           (article-unhide-text end (point-max))
1619         (article-hide-text end (point-max) gnus-hidden-properties)))))
1620
1621 (defun gnus-button-entry ()
1622   ;; Return the first entry in `gnus-button-alist' matching this place.
1623   (let ((alist gnus-button-alist)
1624         (entry nil))
1625     (while alist
1626       (setq entry (pop alist))
1627       (if (looking-at (car entry))
1628           (setq alist nil)
1629         (setq entry nil)))
1630     entry))
1631
1632 (defun gnus-button-push (marker)
1633   ;; Push button starting at MARKER.
1634   (save-excursion
1635     (set-buffer gnus-article-buffer)
1636     (goto-char marker)
1637     (let* ((entry (gnus-button-entry))
1638            (inhibit-point-motion-hooks t)
1639            (fun (nth 3 entry))
1640            (args (mapcar (lambda (group) 
1641                            (let ((string (buffer-substring
1642                                           (match-beginning group)
1643                                           (match-end group))))
1644                              (gnus-set-text-properties
1645                               0 (length string) nil string)
1646                              string))
1647                          (nthcdr 4 entry))))
1648       (cond
1649        ((fboundp fun)
1650         (apply fun args))
1651        ((and (boundp fun)
1652              (fboundp (symbol-value fun)))
1653         (apply (symbol-value fun) args))
1654        (t
1655         (gnus-message 1 "You must define `%S' to use this button"
1656                       (cons fun args)))))))
1657
1658 (defun gnus-button-message-id (message-id)
1659   "Fetch MESSAGE-ID."
1660   (save-excursion
1661     (set-buffer gnus-summary-buffer)
1662     (gnus-summary-refer-article message-id)))
1663
1664 (defun gnus-button-mailto (address)
1665   ;; Mail to ADDRESS.
1666   (set-buffer (gnus-copy-article-buffer))
1667   (message-reply address))
1668
1669 (defun gnus-button-reply (address)
1670   ;; Reply to ADDRESS.
1671   (message-reply address))
1672
1673 (defun gnus-button-url (address)
1674   "Browse ADDRESS."
1675   (funcall browse-url-browser-function address))
1676
1677 ;;; Next/prev buttons in the article buffer.
1678
1679 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
1680 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
1681
1682 (defvar gnus-prev-page-map nil)
1683 (unless gnus-prev-page-map
1684   (setq gnus-prev-page-map (make-sparse-keymap))
1685   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
1686   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
1687
1688 (defun gnus-insert-prev-page-button ()
1689   (let ((buffer-read-only nil))
1690     (gnus-eval-format 
1691      gnus-prev-page-line-format nil
1692      `(gnus-prev t local-map ,gnus-prev-page-map
1693                  gnus-callback gnus-article-button-prev-page))))
1694
1695 (defvar gnus-next-page-map nil)
1696 (unless gnus-next-page-map
1697   (setq gnus-next-page-map (make-keymap))
1698   (suppress-keymap gnus-prev-page-map)
1699   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
1700   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
1701
1702 (defun gnus-button-next-page ()
1703   "Go to the next page."
1704   (interactive)
1705   (let ((win (selected-window)))
1706     (select-window (get-buffer-window gnus-article-buffer t))
1707     (gnus-article-next-page)
1708     (select-window win)))
1709
1710 (defun gnus-button-prev-page ()
1711   "Go to the prev page."
1712   (interactive)
1713   (let ((win (selected-window)))
1714     (select-window (get-buffer-window gnus-article-buffer t))
1715     (gnus-article-prev-page)
1716     (select-window win)))
1717
1718 (defun gnus-insert-next-page-button ()
1719   (let ((buffer-read-only nil))
1720     (gnus-eval-format gnus-next-page-line-format nil
1721                       `(gnus-next t local-map ,gnus-next-page-map
1722                                   gnus-callback 
1723                                   gnus-article-button-next-page))))
1724
1725 (defun gnus-article-button-next-page (arg)
1726   "Go to the next page."
1727   (interactive "P")
1728   (let ((win (selected-window)))
1729     (select-window (get-buffer-window gnus-article-buffer t))
1730     (gnus-article-next-page)
1731     (select-window win)))
1732
1733 (defun gnus-article-button-prev-page (arg)
1734   "Go to the prev page."
1735   (interactive "P")
1736   (let ((win (selected-window)))
1737     (select-window (get-buffer-window gnus-article-buffer t))
1738     (gnus-article-prev-page)
1739     (select-window win)))
1740
1741 (provide 'gnus-art)
1742
1743 ;;; gnus-art.el ends here