*** 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   (substitute-key-definition
497    'undefined 'gnus-article-read-summary-keys gnus-article-mode-map))
498
499 (defun gnus-article-make-menu-bar ()
500   (gnus-turn-off-edit-menu 'article)
501   (unless (boundp 'gnus-article-article-menu)
502     (easy-menu-define
503      gnus-article-article-menu gnus-article-mode-map ""
504      '("Article"
505        ["Scroll forwards" gnus-article-goto-next-page t]
506        ["Scroll backwards" gnus-article-goto-prev-page t]
507        ["Show summary" gnus-article-show-summary t]
508        ["Fetch Message-ID at point" gnus-article-refer-article t]
509        ["Mail to address at point" gnus-article-mail t]
510        ))
511
512     (easy-menu-define
513      gnus-article-treatment-menu gnus-article-mode-map ""
514      '("Treatment"
515        ["Hide headers" gnus-article-hide-headers t]
516        ["Hide signature" gnus-article-hide-signature t]
517        ["Hide citation" gnus-article-hide-citation t]
518        ["Treat overstrike" gnus-article-treat-overstrike t]
519        ["Remove carriage return" gnus-article-remove-cr t]
520        ["Remove quoted-unreadable" gnus-article-de-quoted-unreadable t]
521        ))
522     (run-hooks 'gnus-article-menu-hook)))
523
524 (defun gnus-article-mode ()
525   "Major mode for displaying an article.
526
527 All normal editing commands are switched off.
528
529 The following commands are available in addition to all summary mode
530 commands:
531 \\<gnus-article-mode-map>
532 \\[gnus-article-next-page]\t Scroll the article one page forwards
533 \\[gnus-article-prev-page]\t Scroll the article one page backwards
534 \\[gnus-article-refer-article]\t Go to the article referred to by an article id near point
535 \\[gnus-article-show-summary]\t Display the summary buffer
536 \\[gnus-article-mail]\t Send a reply to the address near point
537 \\[gnus-article-describe-briefly]\t Describe the current mode briefly
538 \\[gnus-info-find-node]\t Go to the Gnus info node"
539   (interactive)
540   (when (and menu-bar-mode
541              (gnus-visual-p 'article-menu 'menu))
542     (gnus-article-make-menu-bar))
543   (kill-all-local-variables)
544   (gnus-simplify-mode-line)
545   (setq mode-name "Article")
546   (setq major-mode 'gnus-article-mode)
547   (make-local-variable 'minor-mode-alist)
548   (or (assq 'gnus-show-mime minor-mode-alist)
549       (setq minor-mode-alist
550             (cons (list 'gnus-show-mime " MIME") minor-mode-alist)))
551   (use-local-map gnus-article-mode-map)
552   (gnus-update-format-specifications nil 'article-mode)
553   (set (make-local-variable 'page-delimiter) gnus-page-delimiter)
554   (gnus-set-default-directory)
555   (buffer-disable-undo (current-buffer))
556   (setq buffer-read-only t)             ;Disable modification
557   (run-hooks 'gnus-article-mode-hook))
558
559 (defun gnus-article-setup-buffer ()
560   "Initialize the article buffer."
561   (let* ((name (if gnus-single-article-buffer "*Article*"
562                  (concat "*Article " gnus-newsgroup-name "*")))
563          (original
564           (progn (string-match "\\*Article" name)
565                  (concat " *Original Article"
566                          (substring name (match-end 0))))))
567     (setq gnus-article-buffer name)
568     (setq gnus-original-article-buffer original)
569     ;; This might be a variable local to the summary buffer.
570     (unless gnus-single-article-buffer
571       (save-excursion
572         (set-buffer gnus-summary-buffer)
573         (setq gnus-article-buffer name)
574         (setq gnus-original-article-buffer original)
575         (gnus-set-global-variables))
576       (make-local-variable 'gnus-summary-buffer))
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           (or (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         (current-buffer)))))
598
599 ;; Set article window start at LINE, where LINE is the number of lines
600 ;; from the head of the article.
601 (defun gnus-article-set-window-start (&optional line)
602   (set-window-start
603    (get-buffer-window gnus-article-buffer t)
604    (save-excursion
605      (set-buffer gnus-article-buffer)
606      (goto-char (point-min))
607      (if (not line)
608          (point-min)
609        (gnus-message 6 "Moved to bookmark")
610        (search-forward "\n\n" nil t)
611        (forward-line line)
612        (point)))))
613
614 (defun gnus-article-prepare (article &optional all-headers header)
615   "Prepare ARTICLE in article mode buffer.
616 ARTICLE should either be an article number or a Message-ID.
617 If ARTICLE is an id, HEADER should be the article headers.
618 If ALL-HEADERS is non-nil, no headers are hidden."
619   (save-excursion
620     ;; Make sure we start in a summary buffer.
621     (unless (eq major-mode 'gnus-summary-mode)
622       (set-buffer gnus-summary-buffer))
623     (setq gnus-summary-buffer (current-buffer))
624     ;; Make sure the connection to the server is alive.
625     (unless (gnus-server-opened
626              (gnus-find-method-for-group gnus-newsgroup-name))
627       (gnus-check-server (gnus-find-method-for-group gnus-newsgroup-name))
628       (gnus-request-group gnus-newsgroup-name t))
629     (let* ((article (if header (mail-header-number header) article))
630            (summary-buffer (current-buffer))
631            (internal-hook gnus-article-internal-prepare-hook)
632            (group gnus-newsgroup-name)
633            result)
634       (save-excursion
635         (gnus-article-setup-buffer)
636         (set-buffer gnus-article-buffer)
637         ;; Deactivate active regions.
638         (when (and (boundp 'transient-mark-mode)
639                    transient-mark-mode)
640           (setq mark-active nil))
641         (if (not (setq result (let ((buffer-read-only nil))
642                                 (gnus-request-article-this-buffer
643                                  article group))))
644             ;; There is no such article.
645             (save-excursion
646               (when (and (numberp article)
647                          (not (memq article gnus-newsgroup-sparse)))
648                 (setq gnus-article-current
649                       (cons gnus-newsgroup-name article))
650                 (set-buffer gnus-summary-buffer)
651                 (setq gnus-current-article article)
652                 (gnus-summary-mark-article article gnus-canceled-mark))
653               (unless (memq article gnus-newsgroup-sparse)
654                 (gnus-error
655                  1 "No such article (may have expired or been canceled)")))
656           (if (or (eq result 'pseudo) (eq result 'nneething))
657               (progn
658                 (save-excursion
659                   (set-buffer summary-buffer)
660                   (setq gnus-last-article gnus-current-article
661                         gnus-newsgroup-history (cons gnus-current-article
662                                                      gnus-newsgroup-history)
663                         gnus-current-article 0
664                         gnus-current-headers nil
665                         gnus-article-current nil)
666                   (if (eq result 'nneething)
667                       (gnus-configure-windows 'summary)
668                     (gnus-configure-windows 'article))
669                   (gnus-set-global-variables))
670                 (gnus-set-mode-line 'article))
671             ;; The result from the `request' was an actual article -
672             ;; or at least some text that is now displayed in the
673             ;; article buffer.
674             (if (and (numberp article)
675                      (not (eq article gnus-current-article)))
676                 ;; Seems like a new article has been selected.
677                 ;; `gnus-current-article' must be an article number.
678                 (save-excursion
679                   (set-buffer summary-buffer)
680                   (setq gnus-last-article gnus-current-article
681                         gnus-newsgroup-history (cons gnus-current-article
682                                                      gnus-newsgroup-history)
683                         gnus-current-article article
684                         gnus-current-headers
685                         (gnus-summary-article-header gnus-current-article)
686                         gnus-article-current
687                         (cons gnus-newsgroup-name gnus-current-article))
688                   (unless (vectorp gnus-current-headers)
689                     (setq gnus-current-headers nil))
690                   (gnus-summary-show-thread)
691                   (run-hooks 'gnus-mark-article-hook)
692                   (gnus-set-mode-line 'summary)
693                   (and (gnus-visual-p 'article-highlight 'highlight)
694                        (run-hooks 'gnus-visual-mark-article-hook))
695                   ;; Set the global newsgroup variables here.
696                   ;; Suggested by Jim Sisolak
697                   ;; <sisolak@trans4.neep.wisc.edu>.
698                   (gnus-set-global-variables)
699                   (setq gnus-have-all-headers
700                         (or all-headers gnus-show-all-headers))
701                   (and gnus-use-cache
702                        (vectorp (gnus-summary-article-header article))
703                        (gnus-cache-possibly-enter-article
704                         group article
705                         (gnus-summary-article-header article)
706                         (memq article gnus-newsgroup-marked)
707                         (memq article gnus-newsgroup-dormant)
708                         (memq article gnus-newsgroup-unreads)))))
709             (when (or (numberp article)
710                       (stringp article))
711               ;; Hooks for getting information from the article.
712               ;; This hook must be called before being narrowed.
713               (let (buffer-read-only)
714                 (run-hooks 'internal-hook)
715                 (run-hooks 'gnus-article-prepare-hook)
716                 ;; Decode MIME message.
717                 (if gnus-show-mime
718                     (if (or (not gnus-strict-mime)
719                             (gnus-fetch-field "Mime-Version"))
720                         (funcall gnus-show-mime-method)
721                       (funcall gnus-decode-encoded-word-method)))
722                 ;; Perform the article display hooks.
723                 (run-hooks 'gnus-article-display-hook))
724               ;; Do page break.
725               (goto-char (point-min))
726               (and gnus-break-pages (gnus-narrow-to-page)))
727             (gnus-set-mode-line 'article)
728             (gnus-configure-windows 'article)
729             (goto-char (point-min))
730             t))))))
731
732 (defun gnus-article-wash-status ()
733   "Return a string which display status of article washing."
734   (save-excursion
735     (set-buffer gnus-article-buffer)
736     (let ((cite (article-hidden-text-p 'cite))
737           (headers (article-hidden-text-p 'headers))
738           (boring (article-hidden-text-p 'boring-headers))
739           (pgp (article-hidden-text-p 'pgp))
740           (pem (article-hidden-text-p 'pem))
741           (signature (article-hidden-text-p 'signature))
742           (overstrike (article-hidden-text-p 'overstrike))
743           (emphasis (article-hidden-text-p 'emphasis))
744           (mime gnus-show-mime))
745       (format "%c%c%c%c%c%c%c"
746               (if cite ?c ? )
747               (if (or headers boring) ?h ? )
748               (if (or pgp pem) ?p ? )
749               (if signature ?s ? )
750               (if overstrike ?o ? )
751               (if mime ?m ? )
752               (if emphasis ?e ? )))))
753
754 (defun gnus-article-hide-headers-if-wanted ()
755   "Hide unwanted headers if `gnus-have-all-headers' is nil.
756 Provided for backwards compatibility."
757   (or (save-excursion (set-buffer gnus-summary-buffer) gnus-have-all-headers)
758       gnus-inhibit-hiding
759       (gnus-article-hide-headers)))
760
761 ;;; Article savers.
762
763 (defun gnus-output-to-rmail (file-name)
764   "Append the current article to an Rmail file named FILE-NAME."
765   (require 'rmail)
766   ;; Most of these codes are borrowed from rmailout.el.
767   (setq file-name (expand-file-name file-name))
768   (setq rmail-default-rmail-file file-name)
769   (let ((artbuf (current-buffer))
770         (tmpbuf (get-buffer-create " *Gnus-output*")))
771     (save-excursion
772       (or (get-file-buffer file-name)
773           (file-exists-p file-name)
774           (if (gnus-yes-or-no-p
775                (concat "\"" file-name "\" does not exist, create it? "))
776               (let ((file-buffer (create-file-buffer file-name)))
777                 (save-excursion
778                   (set-buffer file-buffer)
779                   (rmail-insert-rmail-file-header)
780                   (let ((require-final-newline nil))
781                     (write-region (point-min) (point-max) file-name t 1)))
782                 (kill-buffer file-buffer))
783             (error "Output file does not exist")))
784       (set-buffer tmpbuf)
785       (buffer-disable-undo (current-buffer))
786       (erase-buffer)
787       (insert-buffer-substring artbuf)
788       (gnus-convert-article-to-rmail)
789       ;; Decide whether to append to a file or to an Emacs buffer.
790       (let ((outbuf (get-file-buffer file-name)))
791         (if (not outbuf)
792             (append-to-file (point-min) (point-max) file-name)
793           ;; File has been visited, in buffer OUTBUF.
794           (set-buffer outbuf)
795           (let ((buffer-read-only nil)
796                 (msg (and (boundp 'rmail-current-message)
797                           (symbol-value 'rmail-current-message))))
798             ;; If MSG is non-nil, buffer is in RMAIL mode.
799             (if msg
800                 (progn (widen)
801                        (narrow-to-region (point-max) (point-max))))
802             (insert-buffer-substring tmpbuf)
803             (if msg
804                 (progn
805                   (goto-char (point-min))
806                   (widen)
807                   (search-backward "\^_")
808                   (narrow-to-region (point) (point-max))
809                   (goto-char (1+ (point-min)))
810                   (rmail-count-new-messages t)
811                   (rmail-show-message msg)))))))
812     (kill-buffer tmpbuf)))
813
814 (defun gnus-output-to-file (file-name)
815   "Append the current article to a file named FILE-NAME."
816   (let ((artbuf (current-buffer)))
817     (nnheader-temp-write nil
818       (insert-buffer-substring artbuf)
819       ;; Append newline at end of the buffer as separator, and then
820       ;; save it to file.
821       (goto-char (point-max))
822       (insert "\n")
823       (append-to-file (point-min) (point-max) file-name))))
824
825 (defun gnus-convert-article-to-rmail ()
826   "Convert article in current buffer to Rmail message format."
827   (let ((buffer-read-only nil))
828     ;; Convert article directly into Babyl format.
829     ;; Suggested by Rob Austein <sra@lcs.mit.edu>
830     (goto-char (point-min))
831     (insert "\^L\n0, unseen,,\n*** EOOH ***\n")
832     (while (search-forward "\n\^_" nil t) ;single char
833       (replace-match "\n^_" t t))       ;2 chars: "^" and "_"
834     (goto-char (point-max))
835     (insert "\^_")))
836
837 (defun gnus-narrow-to-page (&optional arg)
838   "Narrow the article buffer to a page.
839 If given a numerical ARG, move forward ARG pages."
840   (interactive "P")
841   (setq arg (if arg (prefix-numeric-value arg) 0))
842   (save-excursion
843     (set-buffer gnus-article-buffer)
844     (goto-char (point-min))
845     (widen)
846     ;; Remove any old next/prev buttons.
847     (when (gnus-visual-p 'page-marker)
848       (let ((buffer-read-only nil))
849         (gnus-remove-text-with-property 'gnus-prev)
850         (gnus-remove-text-with-property 'gnus-next)))
851     (when
852         (cond ((< arg 0)
853                (re-search-backward page-delimiter nil 'move (1+ (abs arg))))
854               ((> arg 0)
855                (re-search-forward page-delimiter nil 'move arg)))
856       (goto-char (match-end 0)))
857     (narrow-to-region
858      (point)
859      (if (re-search-forward page-delimiter nil 'move)
860          (match-beginning 0)
861        (point)))
862     (when (and (gnus-visual-p 'page-marker)
863                (not (= (point-min) 1)))
864       (save-excursion
865         (goto-char (point-min))
866         (gnus-insert-prev-page-button)))
867     (when (and (gnus-visual-p 'page-marker)
868                (< (+ (point-max) 2) (buffer-size)))
869       (save-excursion
870         (goto-char (point-max))
871         (gnus-insert-next-page-button)))))
872
873 ;; Article mode commands
874
875 (defun gnus-article-goto-next-page ()
876   "Show the next page of the article."
877   (interactive)
878   (when (gnus-article-next-page)
879     (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))))
880
881 (defun gnus-article-goto-prev-page ()
882   "Show the next page of the article."
883   (interactive)
884   (if (bobp) (gnus-article-read-summary-keys nil (gnus-character-to-event ?n))
885     (gnus-article-prev-page nil)))
886
887 (defun gnus-article-next-page (&optional lines)
888   "Show the next page of the current article.
889 If end of article, return non-nil.  Otherwise return nil.
890 Argument LINES specifies lines to be scrolled up."
891   (interactive "p")
892   (move-to-window-line -1)
893   (if (save-excursion
894         (end-of-line)
895         (and (pos-visible-in-window-p)  ;Not continuation line.
896              (eobp)))
897       ;; Nothing in this page.
898       (if (or (not gnus-break-pages)
899               (save-excursion
900                 (save-restriction
901                   (widen) (forward-line 1) (eobp)))) ;Real end-of-buffer?
902           t                             ;Nothing more.
903         (gnus-narrow-to-page 1)         ;Go to next page.
904         nil)
905     ;; More in this page.
906     (condition-case ()
907         (scroll-up lines)
908       (end-of-buffer
909        ;; Long lines may cause an end-of-buffer error.
910        (goto-char (point-max))))
911     (move-to-window-line 0)
912     nil))
913
914 (defun gnus-article-prev-page (&optional lines)
915   "Show previous page of current article.
916 Argument LINES specifies lines to be scrolled down."
917   (interactive "p")
918   (move-to-window-line 0)
919   (if (and gnus-break-pages
920            (bobp)
921            (not (save-restriction (widen) (bobp)))) ;Real beginning-of-buffer?
922       (progn
923         (gnus-narrow-to-page -1)        ;Go to previous page.
924         (goto-char (point-max))
925         (recenter -1))
926     (prog1
927         (condition-case ()
928             (scroll-down lines)
929           (error nil))
930       (move-to-window-line 0))))
931
932 (defun gnus-article-refer-article ()
933   "Read article specified by message-id around point."
934   (interactive)
935   (let ((point (point)))
936     (search-forward ">" nil t)          ;Move point to end of "<....>".
937     (if (re-search-backward "\\(<[^<> \t\n]+>\\)" nil t)
938         (let ((message-id (match-string 1)))
939           (goto-char point)
940           (set-buffer gnus-summary-buffer)
941           (gnus-summary-refer-article message-id))
942       (goto-char (point))
943       (error "No references around point"))))
944
945 (defun gnus-article-show-summary ()
946   "Reconfigure windows to show summary buffer."
947   (interactive)
948   (gnus-configure-windows 'article)
949   (gnus-summary-goto-subject gnus-current-article))
950
951 (defun gnus-article-describe-briefly ()
952   "Describe article mode commands briefly."
953   (interactive)
954   (gnus-message 6
955                 (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")))
956
957 (defun gnus-article-summary-command ()
958   "Execute the last keystroke in the summary buffer."
959   (interactive)
960   (let ((obuf (current-buffer))
961         (owin (current-window-configuration))
962         func)
963     (switch-to-buffer gnus-summary-buffer 'norecord)
964     (setq func (lookup-key (current-local-map) (this-command-keys)))
965     (call-interactively func)
966     (set-buffer obuf)
967     (set-window-configuration owin)
968     (set-window-point (get-buffer-window (current-buffer)) (point))))
969
970 (defun gnus-article-summary-command-nosave ()
971   "Execute the last keystroke in the summary buffer."
972   (interactive)
973   (let (func)
974     (pop-to-buffer gnus-summary-buffer 'norecord)
975     (setq func (lookup-key (current-local-map) (this-command-keys)))
976     (call-interactively func)))
977
978 (defun gnus-article-read-summary-keys (&optional arg key not-restore-window)
979   "Read a summary buffer key sequence and execute it from the article buffer."
980   (interactive "P")
981   (let ((nosaves
982          '("q" "Q"  "c" "r" "R" "\C-c\C-f" "m"  "a" "f" "F"
983            "Zc" "ZC" "ZE" "ZQ" "ZZ" "Zn" "ZR" "ZG" "ZN" "ZP"
984            "=" "^" "\M-^" "|"))
985         (nosave-but-article
986          '("A\r"))
987         keys)
988     (save-excursion
989       (set-buffer gnus-summary-buffer)
990       (push (or key last-command-event) unread-command-events)
991       (setq keys (read-key-sequence nil)))
992     (message "")
993
994     (if (or (member keys nosaves)
995             (member keys nosave-but-article))
996         (let (func)
997           (save-window-excursion
998             (pop-to-buffer gnus-summary-buffer 'norecord)
999             (setq func (lookup-key (current-local-map) keys)))
1000           (if (not func)
1001               (ding)
1002             (set-buffer gnus-summary-buffer)
1003             (call-interactively func))
1004           (when (member keys nosave-but-article)
1005             (pop-to-buffer gnus-article-buffer 'norecord)))
1006       (let ((obuf (current-buffer))
1007             (owin (current-window-configuration))
1008             (opoint (point))
1009             func in-buffer)
1010         (if not-restore-window
1011             (pop-to-buffer gnus-summary-buffer 'norecord)
1012           (switch-to-buffer gnus-summary-buffer 'norecord))
1013         (setq in-buffer (current-buffer))
1014         (if (setq func (lookup-key (current-local-map) keys))
1015             (call-interactively func)
1016           (ding))
1017         (when (eq in-buffer (current-buffer))
1018           (set-buffer obuf)
1019           (unless not-restore-window
1020             (set-window-configuration owin))
1021           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
1022
1023 (defun gnus-article-hide (&optional arg force)
1024   "Hide all the gruft in the current article.
1025 This means that PGP stuff, signatures, cited text and (some)
1026 headers will be hidden.
1027 If given a prefix, show the hidden text instead."
1028   (interactive (list current-prefix-arg 'force))
1029   (gnus-article-hide-headers arg)
1030   (gnus-article-hide-pgp arg)
1031   (gnus-article-hide-citation-maybe arg force)
1032   (gnus-article-hide-signature arg))
1033
1034 (defun gnus-article-maybe-highlight ()
1035   "Do some article highlighting if `article-visual' is non-nil."
1036   (if (gnus-visual-p 'article-highlight 'highlight)
1037       (gnus-article-highlight-some)))
1038
1039 (defun gnus-request-article-this-buffer (article group)
1040   "Get an article and insert it into this buffer."
1041   (let (do-update-line)
1042     (prog1
1043         (save-excursion
1044           (erase-buffer)
1045           (gnus-kill-all-overlays)
1046           (setq group (or group gnus-newsgroup-name))
1047
1048           ;; Open server if it has closed.
1049           (gnus-check-server (gnus-find-method-for-group group))
1050
1051           ;; Using `gnus-request-article' directly will insert the article into
1052           ;; `nntp-server-buffer' - so we'll save some time by not having to
1053           ;; copy it from the server buffer into the article buffer.
1054
1055           ;; We only request an article by message-id when we do not have the
1056           ;; headers for it, so we'll have to get those.
1057           (when (stringp article)
1058             (let ((gnus-override-method gnus-refer-article-method))
1059               (gnus-read-header article)))
1060
1061           ;; If the article number is negative, that means that this article
1062           ;; doesn't belong in this newsgroup (possibly), so we find its
1063           ;; message-id and request it by id instead of number.
1064           (when (and (numberp article)
1065                      gnus-summary-buffer
1066                      (get-buffer gnus-summary-buffer)
1067                      (buffer-name (get-buffer gnus-summary-buffer)))
1068             (save-excursion
1069               (set-buffer gnus-summary-buffer)
1070               (let ((header (gnus-summary-article-header article)))
1071                 (if (< article 0)
1072                     (cond 
1073                      ((memq article gnus-newsgroup-sparse)
1074                       ;; This is a sparse gap article.
1075                       (setq do-update-line article)
1076                       (setq article (mail-header-id header))
1077                       (let ((gnus-override-method gnus-refer-article-method))
1078                         (gnus-read-header article))
1079                       (setq gnus-newsgroup-sparse
1080                             (delq article gnus-newsgroup-sparse)))
1081                      ((vectorp header)
1082                       ;; It's a real article.
1083                       (setq article (mail-header-id header)))
1084                      (t
1085                       ;; It is an extracted pseudo-article.
1086                       (setq article 'pseudo)
1087                       (gnus-request-pseudo-article header))))
1088                 
1089                 (let ((method (gnus-find-method-for-group 
1090                                gnus-newsgroup-name)))
1091                   (if (not (eq (car method) 'nneething))
1092                       ()
1093                     (let ((dir (concat (file-name-as-directory (nth 1 method))
1094                                        (mail-header-subject header))))
1095                       (if (file-directory-p dir)
1096                           (progn
1097                             (setq article 'nneething)
1098                             (gnus-group-enter-directory dir)))))))))
1099
1100           (cond
1101            ;; Refuse to select canceled articles.
1102            ((and (numberp article)
1103                  gnus-summary-buffer
1104                  (get-buffer gnus-summary-buffer)
1105                  (buffer-name (get-buffer gnus-summary-buffer))
1106                  (eq (cdr (save-excursion
1107                             (set-buffer gnus-summary-buffer)
1108                             (assq article gnus-newsgroup-reads)))
1109                      gnus-canceled-mark))
1110             nil)
1111            ;; We first check `gnus-original-article-buffer'.
1112            ((and (get-buffer gnus-original-article-buffer)
1113                  (numberp article)
1114                  (save-excursion
1115                    (set-buffer gnus-original-article-buffer)
1116                    (and (equal (car gnus-original-article) group)
1117                         (eq (cdr gnus-original-article) article))))
1118             (insert-buffer-substring gnus-original-article-buffer)
1119             'article)
1120            ;; Check the backlog.
1121            ((and gnus-keep-backlog
1122                  (gnus-backlog-request-article group article (current-buffer)))
1123             'article)
1124            ;; Check asynchronous pre-fetch.
1125            ((gnus-async-request-fetched-article group article (current-buffer))
1126             (gnus-async-prefetch-next group article gnus-summary-buffer)
1127             'article)
1128            ;; Check the cache.
1129            ((and gnus-use-cache
1130                  (numberp article)
1131                  (gnus-cache-request-article article group))
1132             'article)
1133            ;; Get the article and put into the article buffer.
1134            ((or (stringp article) (numberp article))
1135             (let ((gnus-override-method
1136                    (and (stringp article) gnus-refer-article-method))
1137                   (buffer-read-only nil))
1138               (erase-buffer)
1139               (gnus-kill-all-overlays)
1140               (when (gnus-request-article article group (current-buffer))
1141                 (when (numberp article)
1142                   (gnus-async-prefetch-next group article gnus-summary-buffer)
1143                   (when gnus-keep-backlog
1144                     (gnus-backlog-enter-article 
1145                      group article (current-buffer))))
1146                 'article)))
1147            ;; It was a pseudo.
1148            (t article)))
1149
1150       ;; Take the article from the original article buffer
1151       ;; and place it in the buffer it's supposed to be in.
1152       (when (and (get-buffer gnus-article-buffer)
1153                  ;;(numberp article)
1154                  (equal (buffer-name (current-buffer))
1155                         (buffer-name (get-buffer gnus-article-buffer))))
1156         (save-excursion
1157           (if (get-buffer gnus-original-article-buffer)
1158               (set-buffer (get-buffer gnus-original-article-buffer))
1159             (set-buffer (get-buffer-create gnus-original-article-buffer))
1160             (buffer-disable-undo (current-buffer))
1161             (setq major-mode 'gnus-original-article-mode)
1162             (setq buffer-read-only t)
1163             (gnus-add-current-to-buffer-list))
1164           (let (buffer-read-only)
1165             (erase-buffer)
1166             (insert-buffer-substring gnus-article-buffer))
1167           (setq gnus-original-article (cons group article))))
1168     
1169       ;; Update sparse articles.
1170       (when (and do-update-line
1171                  (or (numberp article)
1172                      (stringp article)))
1173         (let ((buf (current-buffer)))
1174           (set-buffer gnus-summary-buffer)
1175           (gnus-summary-update-article do-update-line)
1176           (gnus-summary-goto-subject do-update-line nil t)
1177           (set-window-point (get-buffer-window (current-buffer) t)
1178                             (point))
1179           (set-buffer buf))))))
1180
1181 (defun gnus-article-date-ut (&optional type highlight)
1182   "Convert DATE date to universal time in the current article.
1183 If TYPE is `local', convert to local time; if it is `lapsed', output
1184 how much time has lapsed since DATE."
1185   (interactive (list 'ut t))
1186   (let ((headers (or gnus-current-headers (gnus-summary-article-header))))
1187     (save-excursion
1188       (set-buffer gnus-article-buffer)
1189       (article-date-ut type highlight headers))))
1190
1191 ;;;
1192 ;;; Article editing
1193 ;;;
1194
1195 (defvar gnus-article-edit-mode-hook nil
1196   "*Hook run in article edit mode buffers.")
1197
1198 (defvar gnus-article-edit-done-function nil)
1199
1200 (defvar gnus-article-edit-mode-map nil)
1201
1202 (unless gnus-article-edit-mode-map 
1203   (setq gnus-article-edit-mode-map (copy-keymap text-mode-map))
1204
1205   (gnus-define-keys gnus-article-edit-mode-map
1206     "\C-c\C-c" gnus-article-edit-done
1207     "\C-c\C-k" gnus-article-edit-exit)
1208
1209   (gnus-define-keys (gnus-article-edit-wash-map
1210                      "\C-c\C-w" gnus-article-edit-mode-map)
1211     "f" gnus-article-edit-full-stops))
1212
1213 (defun gnus-article-edit-mode ()
1214   "Major mode for editing articles.
1215 This is an extended text-mode.
1216
1217 \\{gnus-article-edit-mode-map}"
1218   (interactive)
1219   (kill-all-local-variables)
1220   (setq major-mode 'gnus-article-edit-mode)
1221   (setq mode-name "Article Edit")
1222   (use-local-map gnus-article-edit-mode-map)
1223   (make-local-variable 'gnus-article-edit-done-function)
1224   (make-local-variable 'gnus-prev-winconf)
1225   (setq buffer-read-only nil)
1226   (buffer-enable-undo)
1227   (widen)
1228   (run-hooks 'text-mode 'gnus-article-edit-mode-hook))
1229
1230 (defun gnus-article-edit (&optional force)
1231   "Edit the current article.
1232 This will have permanent effect only in mail groups.
1233 If FORCE is non-nil, allow editing of articles even in read-only
1234 groups."
1235   (interactive "P")
1236   (when (and (not force)
1237              (gnus-group-read-only-p))
1238     (error "The current newsgroup does not support article editing."))
1239   (gnus-article-edit-article
1240    `(lambda ()
1241       (gnus-summary-edit-article-done
1242        ,(or (mail-header-references gnus-current-headers) "")
1243        ,(gnus-group-read-only-p) ,gnus-summary-buffer))))
1244
1245 (defun gnus-article-edit-article (exit-func)
1246   "Start editing the contents of the current article buffer."
1247   (let ((winconf (current-window-configuration)))
1248     (set-buffer gnus-article-buffer)
1249     (gnus-article-edit-mode)
1250     (set-text-properties (point-min) (point-max) nil)
1251     (gnus-configure-windows 'edit-article)
1252     (setq gnus-article-edit-done-function exit-func)
1253     (setq gnus-prev-winconf winconf)
1254     (gnus-message 6 "C-c C-c to end edits")))
1255
1256 (defun gnus-article-edit-done ()
1257   "Update the article edits and exit."
1258   (interactive)
1259   (let ((func gnus-article-edit-done-function)
1260         (buf (current-buffer))
1261         (start (window-start)))
1262     (gnus-article-edit-exit)
1263     (let ((cur (current-buffer)))
1264       (save-excursion
1265         (set-buffer buf)
1266         (let ((buffer-read-only nil))
1267           (funcall func)))
1268       (set-buffer buf)
1269       (set-window-start (get-buffer-window buf) start)
1270       (set-window-point (get-buffer-window buf) (point)))))
1271
1272 (defun gnus-article-edit-exit ()
1273   "Exit the article editing without updating."
1274   (interactive)
1275   ;; We remove all text props from the article buffer.
1276   (let ((buf (format "%s" (buffer-string)))
1277         (curbuf (current-buffer))
1278         (p (point))
1279         (window-start (window-start)))
1280     (erase-buffer)
1281     (insert buf)
1282     (let ((winconf gnus-prev-winconf))
1283       (gnus-article-mode)
1284       ;; The cache and backlog have to be flushed somewhat.
1285       (when gnus-use-cache
1286         (gnus-cache-update-article      
1287          (car gnus-article-current) (cdr gnus-article-current)))
1288       (when gnus-keep-backlog
1289         (gnus-backlog-remove-article 
1290          (car gnus-article-current) (cdr gnus-article-current)))
1291       ;; Flush original article as well.
1292       (save-excursion
1293         (when (get-buffer gnus-original-article-buffer)
1294           (set-buffer gnus-original-article-buffer)
1295           (setq gnus-original-article nil)))
1296       (set-window-configuration winconf)
1297       ;; Tippy-toe some to make sure that point remains where it was.
1298       (let ((buf (current-buffer)))
1299         (set-buffer curbuf)
1300         (set-window-start (get-buffer-window (current-buffer)) window-start)
1301         (goto-char p)
1302         (set-buffer buf)))))
1303       
1304 (defun gnus-article-edit-full-stops ()
1305   "Interactively repair spacing at end of sentences."
1306   (interactive)
1307   (save-excursion
1308     (goto-char (point-min))
1309     (search-forward-regexp "^$" nil t)
1310     (let ((case-fold-search nil))
1311       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
1312
1313 ;;; 
1314 ;;; Article highlights
1315 ;;;
1316
1317 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
1318
1319 ;;; Internal Variables:
1320
1321 (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_=#$@~`%&*+|\\/]"
1322   "*Regular expression that matches URLs.")
1323
1324 (defvar gnus-button-alist 
1325   `(("\\bin\\( +article\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2 
1326      t gnus-button-message-id 3)
1327     ("\\(<?\\(url: ?\\)?news:\\([^>\n\t ]*\\)>?\\)" 1 t
1328      gnus-button-message-id 3)
1329     ("\\(<URL: *\\)?mailto: *\\([^> \n\t]+\\)>?" 0 t gnus-button-reply 2)
1330     ;; This is how URLs _should_ be embedded in text...
1331     ("<URL: *\\([^\n\r>]*\\)>" 0 t gnus-button-url 1)
1332     ;; Next regexp stolen from highlight-headers.el.
1333     ;; Modified by Vladimir Alexiev.
1334     (,gnus-button-url-regexp 0 t gnus-button-url 0))
1335   "Alist of regexps matching buttons in article bodies.
1336
1337 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
1338 REGEXP: is the string matching text around the button,
1339 BUTTON: is the number of the regexp grouping actually matching the button,
1340 FORM: is a lisp expression which must eval to true for the button to
1341 be added, 
1342 CALLBACK: is the function to call when the user push this button, and each
1343 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
1344
1345 CALLBACK can also be a variable, in that case the value of that
1346 variable it the real callback function.")
1347
1348 (defvar gnus-header-button-alist 
1349   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
1350      0 t gnus-button-message-id 0)
1351     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
1352     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+" 
1353      0 t gnus-button-mailto 0)
1354     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
1355     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
1356     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
1357      gnus-button-message-id 3))
1358   "Alist of headers and regexps to match buttons in article heads.
1359
1360 This alist is very similar to `gnus-button-alist', except that each
1361 alist has an additional HEADER element first in each entry:
1362
1363 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
1364
1365 HEADER is a regexp to match a header.  For a fuller explanation, see
1366 `gnus-button-alist'.")
1367
1368 (defvar gnus-button-regexp nil)
1369 (defvar gnus-button-marker-list nil)
1370 ;; Regexp matching any of the regexps from `gnus-button-alist'.
1371
1372 (defvar gnus-button-last nil)
1373 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
1374
1375 ;;; Commands:
1376
1377 (defun gnus-article-push-button (event)
1378   "Check text under the mouse pointer for a callback function.
1379 If the text under the mouse pointer has a `gnus-callback' property,
1380 call it with the value of the `gnus-data' text property."
1381   (interactive "e")
1382   (set-buffer (window-buffer (posn-window (event-start event))))
1383   (let* ((pos (posn-point (event-start event)))
1384          (data (get-text-property pos 'gnus-data))
1385          (fun (get-text-property pos 'gnus-callback)))
1386     (if fun (funcall fun data))))
1387
1388 (defun gnus-article-press-button ()
1389   "Check text at point for a callback function.
1390 If the text at point has a `gnus-callback' property,
1391 call it with the value of the `gnus-data' text property."
1392   (interactive)
1393   (let* ((data (get-text-property (point) 'gnus-data))
1394          (fun (get-text-property (point) 'gnus-callback)))
1395     (if fun (funcall fun data))))
1396
1397 (defun gnus-article-prev-button (n)
1398   "Move point to N buttons backward.
1399 If N is negative, move forward instead."
1400   (interactive "p")
1401   (gnus-article-next-button (- n)))
1402
1403 (defun gnus-article-next-button (n)
1404   "Move point to N buttons forward.
1405 If N is negative, move backward instead."
1406   (interactive "p")
1407   (let ((function (if (< n 0) 'previous-single-property-change
1408                     'next-single-property-change))
1409         (inhibit-point-motion-hooks t)
1410         (backward (< n 0))
1411         (limit (if (< n 0) (point-min) (point-max))))
1412     (setq n (abs n))
1413     (while (and (not (= limit (point)))
1414                 (> n 0))
1415       ;; Skip past the current button.
1416       (when (get-text-property (point) 'gnus-callback)
1417         (goto-char (funcall function (point) 'gnus-callback nil limit)))
1418       ;; Go to the next (or previous) button.
1419       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
1420       ;; Put point at the start of the button.
1421       (when (and backward (not (get-text-property (point) 'gnus-callback)))
1422         (goto-char (funcall function (point) 'gnus-callback nil limit)))
1423       ;; Skip past intangible buttons.
1424       (when (get-text-property (point) 'intangible)
1425         (incf n))
1426       (decf n))
1427     (unless (zerop n)
1428       (gnus-message 5 "No more buttons"))
1429     n))
1430
1431 (defun gnus-article-highlight (&optional force)
1432   "Highlight current article.
1433 This function calls `gnus-article-highlight-headers',
1434 `gnus-article-highlight-citation', 
1435 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
1436 do the highlighting.  See the documentation for those functions."
1437   (interactive (list 'force))
1438   (gnus-article-highlight-headers)
1439   (gnus-article-highlight-citation force)
1440   (gnus-article-highlight-signature)
1441   (gnus-article-add-buttons force)
1442   (gnus-article-add-buttons-to-head))
1443
1444 (defun gnus-article-highlight-some (&optional force)
1445   "Highlight current article.
1446 This function calls `gnus-article-highlight-headers',
1447 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
1448 do the highlighting.  See the documentation for those functions."
1449   (interactive (list 'force))
1450   (gnus-article-highlight-headers)
1451   (gnus-article-highlight-signature)
1452   (gnus-article-add-buttons))
1453
1454 (defun gnus-article-highlight-headers ()
1455   "Highlight article headers as specified by `gnus-header-face-alist'."
1456   (interactive)
1457   (save-excursion
1458     (set-buffer gnus-article-buffer)
1459     (save-restriction
1460       (let ((alist gnus-header-face-alist)
1461             (buffer-read-only nil)
1462             (case-fold-search t)
1463             (inhibit-point-motion-hooks t)
1464             entry regexp header-face field-face from hpoints fpoints)
1465         (goto-char (point-min))
1466         (when (search-forward "\n\n" nil t)
1467           (narrow-to-region (1- (point)) (point-min))
1468           (while (setq entry (pop alist))
1469             (goto-char (point-min))
1470             (setq regexp (concat "^\\("
1471                                  (if (string-equal "" (nth 0 entry))
1472                                      "[^\t ]"
1473                                    (nth 0 entry))
1474                                  "\\)")
1475                   header-face (nth 1 entry)
1476                   field-face (nth 2 entry))
1477             (while (and (re-search-forward regexp nil t)
1478                         (not (eobp)))
1479               (beginning-of-line)
1480               (setq from (point))
1481               (or (search-forward ":" nil t)
1482                   (forward-char 1))
1483               (when (and header-face
1484                          (not (memq (point) hpoints)))
1485                 (push (point) hpoints)
1486                 (gnus-put-text-property from (point) 'face header-face))
1487               (when (and field-face
1488                          (not (memq (setq from (point)) fpoints)))
1489                 (push from fpoints)
1490                 (if (re-search-forward "^[^ \t]" nil t)
1491                     (forward-char -2)
1492                   (goto-char (point-max)))
1493                 (gnus-put-text-property from (point) 'face field-face)))))))))
1494
1495 (defun gnus-article-highlight-signature ()
1496   "Highlight the signature in an article.
1497 It does this by highlighting everything after
1498 `gnus-signature-separator' using `gnus-signature-face'." 
1499   (interactive)
1500   (save-excursion
1501     (set-buffer gnus-article-buffer)
1502     (let ((buffer-read-only nil)
1503           (inhibit-point-motion-hooks t))
1504       (save-restriction
1505         (when (and gnus-signature-face
1506                    (article-narrow-to-signature))
1507           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
1508                             'face gnus-signature-face)
1509           (widen)
1510           (article-search-signature)
1511           (let ((start (match-beginning 0))
1512                 (end (set-marker (make-marker) (1+ (match-end 0)))))
1513             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
1514                                      end)))))))
1515
1516 (defun gnus-article-add-buttons (&optional force)
1517   "Find external references in the article and make buttons of them.
1518 \"External references\" are things like Message-IDs and URLs, as
1519 specified by `gnus-button-alist'."
1520   (interactive (list 'force))
1521   (save-excursion
1522     (set-buffer gnus-article-buffer)
1523     ;; Remove all old markers.
1524     (while gnus-button-marker-list
1525       (set-marker (pop gnus-button-marker-list) nil))
1526     (let ((buffer-read-only nil)
1527           (inhibit-point-motion-hooks t)
1528           (case-fold-search t)
1529           (alist gnus-button-alist)
1530           beg entry regexp)
1531       (goto-char (point-min))
1532       ;; We skip the headers.
1533       (unless (search-forward "\n\n" nil t)
1534         (goto-char (point-max)))
1535       (setq beg (point))
1536       (while (setq entry (pop alist))
1537         (setq regexp (car entry))
1538         (goto-char beg)
1539         (while (re-search-forward regexp nil t)
1540           (let* ((start (and entry (match-beginning (nth 1 entry))))
1541                  (end (and entry (match-end (nth 1 entry))))
1542                  (from (match-beginning 0)))
1543             (when (or (eq t (nth 1 entry))
1544                       (eval (nth 1 entry)))
1545               ;; That optional form returned non-nil, so we add the
1546               ;; button. 
1547               (gnus-article-add-button 
1548                start end 'gnus-button-push 
1549                (car (push (set-marker (make-marker) from)
1550                           gnus-button-marker-list))))))))))
1551
1552 ;; Add buttons to the head of an article.
1553 (defun gnus-article-add-buttons-to-head ()
1554   "Add buttons to the head of the article."
1555   (interactive)
1556   (save-excursion
1557     (set-buffer gnus-article-buffer)
1558     (let ((buffer-read-only nil)
1559           (inhibit-point-motion-hooks t)
1560           (case-fold-search t)
1561           (alist gnus-header-button-alist)
1562           entry beg end)
1563       (nnheader-narrow-to-headers)
1564       (while alist
1565         ;; Each alist entry.
1566         (setq entry (car alist)
1567               alist (cdr alist))
1568         (goto-char (point-min))
1569         (while (re-search-forward (car entry) nil t)
1570           ;; Each header matching the entry.
1571           (setq beg (match-beginning 0))
1572           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
1573                              (match-beginning 0))
1574                         (point-max)))
1575           (goto-char beg)
1576           (while (re-search-forward (nth 1 entry) end t)
1577             ;; Each match within a header.
1578             (let* ((from (match-beginning 0))
1579                    (entry (cdr entry))
1580                    (start (match-beginning (nth 1 entry)))
1581                    (end (match-end (nth 1 entry)))
1582                    (form (nth 2 entry)))
1583               (goto-char (match-end 0))
1584               (and (eval form)
1585                    (gnus-article-add-button 
1586                     start end (nth 3 entry)
1587                     (buffer-substring (match-beginning (nth 4 entry))
1588                                       (match-end (nth 4 entry)))))))
1589           (goto-char end))))
1590     (widen)))
1591
1592 ;;; External functions:
1593
1594 (defun gnus-article-add-button (from to fun &optional data)
1595   "Create a button between FROM and TO with callback FUN and data DATA."
1596   (and gnus-article-button-face
1597        (gnus-overlay-put (gnus-make-overlay from to)
1598                          'face gnus-article-button-face))
1599   (gnus-add-text-properties 
1600    from to
1601    (nconc (and gnus-article-mouse-face
1602                (list gnus-mouse-face-prop gnus-article-mouse-face))
1603           (list 'gnus-callback fun)
1604           (and data (list 'gnus-data data)))))
1605
1606 ;;; Internal functions:
1607
1608 (defun gnus-signature-toggle (end)
1609   (save-excursion
1610     (set-buffer gnus-article-buffer)
1611     (let ((buffer-read-only nil)
1612           (inhibit-point-motion-hooks t))
1613       (if (get-text-property end 'invisible)
1614           (article-unhide-text end (point-max))
1615         (article-hide-text end (point-max) gnus-hidden-properties)))))
1616
1617 (defun gnus-button-entry ()
1618   ;; Return the first entry in `gnus-button-alist' matching this place.
1619   (let ((alist gnus-button-alist)
1620         (entry nil))
1621     (while alist
1622       (setq entry (pop alist))
1623       (if (looking-at (car entry))
1624           (setq alist nil)
1625         (setq entry nil)))
1626     entry))
1627
1628 (defun gnus-button-push (marker)
1629   ;; Push button starting at MARKER.
1630   (save-excursion
1631     (set-buffer gnus-article-buffer)
1632     (goto-char marker)
1633     (let* ((entry (gnus-button-entry))
1634            (inhibit-point-motion-hooks t)
1635            (fun (nth 3 entry))
1636            (args (mapcar (lambda (group) 
1637                            (let ((string (buffer-substring
1638                                           (match-beginning group)
1639                                           (match-end group))))
1640                              (gnus-set-text-properties
1641                               0 (length string) nil string)
1642                              string))
1643                          (nthcdr 4 entry))))
1644       (cond
1645        ((fboundp fun)
1646         (apply fun args))
1647        ((and (boundp fun)
1648              (fboundp (symbol-value fun)))
1649         (apply (symbol-value fun) args))
1650        (t
1651         (gnus-message 1 "You must define `%S' to use this button"
1652                       (cons fun args)))))))
1653
1654 (defun gnus-button-message-id (message-id)
1655   "Fetch MESSAGE-ID."
1656   (save-excursion
1657     (set-buffer gnus-summary-buffer)
1658     (gnus-summary-refer-article message-id)))
1659
1660 (defun gnus-button-mailto (address)
1661   ;; Mail to ADDRESS.
1662   (set-buffer (gnus-copy-article-buffer))
1663   (message-reply address))
1664
1665 (defun gnus-button-reply (address)
1666   ;; Reply to ADDRESS.
1667   (message-reply address))
1668
1669 (defun gnus-button-url (address)
1670   "Browse ADDRESS."
1671   (funcall browse-url-browser-function address))
1672
1673 ;;; Next/prev buttons in the article buffer.
1674
1675 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
1676 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
1677
1678 (defvar gnus-prev-page-map nil)
1679 (unless gnus-prev-page-map
1680   (setq gnus-prev-page-map (make-sparse-keymap))
1681   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
1682   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
1683
1684 (defun gnus-insert-prev-page-button ()
1685   (let ((buffer-read-only nil))
1686     (gnus-eval-format 
1687      gnus-prev-page-line-format nil
1688      `(gnus-prev t local-map ,gnus-prev-page-map
1689                  gnus-callback gnus-article-button-prev-page))))
1690
1691 (defvar gnus-next-page-map nil)
1692 (unless gnus-next-page-map
1693   (setq gnus-next-page-map (make-keymap))
1694   (suppress-keymap gnus-prev-page-map)
1695   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
1696   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
1697
1698 (defun gnus-button-next-page ()
1699   "Go to the next page."
1700   (interactive)
1701   (let ((win (selected-window)))
1702     (select-window (get-buffer-window gnus-article-buffer t))
1703     (gnus-article-next-page)
1704     (select-window win)))
1705
1706 (defun gnus-button-prev-page ()
1707   "Go to the prev page."
1708   (interactive)
1709   (let ((win (selected-window)))
1710     (select-window (get-buffer-window gnus-article-buffer t))
1711     (gnus-article-prev-page)
1712     (select-window win)))
1713
1714 (defun gnus-insert-next-page-button ()
1715   (let ((buffer-read-only nil))
1716     (gnus-eval-format gnus-next-page-line-format nil
1717                       `(gnus-next t local-map ,gnus-next-page-map
1718                                   gnus-callback 
1719                                   gnus-article-button-next-page))))
1720
1721 (defun gnus-article-button-next-page (arg)
1722   "Go to the next page."
1723   (interactive "P")
1724   (let ((win (selected-window)))
1725     (select-window (get-buffer-window gnus-article-buffer t))
1726     (gnus-article-next-page)
1727     (select-window win)))
1728
1729 (defun gnus-article-button-prev-page (arg)
1730   "Go to the prev page."
1731   (interactive "P")
1732   (let ((win (selected-window)))
1733     (select-window (get-buffer-window gnus-article-buffer t))
1734     (gnus-article-prev-page)
1735     (select-window win)))
1736
1737 (provide 'gnus-art)
1738
1739 ;;; gnus-art.el ends here