*** 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   ;; Fixed by enami@ptgd.sony.co.jp (enami tsugutomo)
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         keys)
989     (save-excursion
990       (set-buffer gnus-summary-buffer)
991       (push (or key last-command-event) unread-command-events)
992       (setq keys (read-key-sequence nil)))
993     (message "")
994
995     (if (or (member keys nosaves)
996             (member keys nosave-but-article))
997         (let (func)
998           (save-window-excursion
999             (pop-to-buffer gnus-summary-buffer 'norecord)
1000             (setq func (lookup-key (current-local-map) keys)))
1001           (if (not func)
1002               (ding)
1003             (set-buffer gnus-summary-buffer)
1004             (call-interactively func))
1005           (when (member keys nosave-but-article)
1006             (pop-to-buffer gnus-article-buffer 'norecord)))
1007       (let ((obuf (current-buffer))
1008             (owin (current-window-configuration))
1009             (opoint (point))
1010             func in-buffer)
1011         (if not-restore-window
1012             (pop-to-buffer gnus-summary-buffer 'norecord)
1013           (switch-to-buffer gnus-summary-buffer 'norecord))
1014         (setq in-buffer (current-buffer))
1015         (if (setq func (lookup-key (current-local-map) keys))
1016             (call-interactively func)
1017           (ding))
1018         (when (eq in-buffer (current-buffer))
1019           (set-buffer obuf)
1020           (unless not-restore-window
1021             (set-window-configuration owin))
1022           (set-window-point (get-buffer-window (current-buffer)) opoint))))))
1023
1024 (defun gnus-article-hide (&optional arg force)
1025   "Hide all the gruft in the current article.
1026 This means that PGP stuff, signatures, cited text and (some)
1027 headers will be hidden.
1028 If given a prefix, show the hidden text instead."
1029   (interactive (list current-prefix-arg 'force))
1030   (gnus-article-hide-headers arg)
1031   (gnus-article-hide-pgp arg)
1032   (gnus-article-hide-citation-maybe arg force)
1033   (gnus-article-hide-signature arg))
1034
1035 (defun gnus-article-maybe-highlight ()
1036   "Do some article highlighting if `article-visual' is non-nil."
1037   (if (gnus-visual-p 'article-highlight 'highlight)
1038       (gnus-article-highlight-some)))
1039
1040 (defun gnus-request-article-this-buffer (article group)
1041   "Get an article and insert it into this buffer."
1042   (let (do-update-line)
1043     (prog1
1044         (save-excursion
1045           (erase-buffer)
1046           (gnus-kill-all-overlays)
1047           (setq group (or group gnus-newsgroup-name))
1048
1049           ;; Open server if it has closed.
1050           (gnus-check-server (gnus-find-method-for-group group))
1051
1052           ;; Using `gnus-request-article' directly will insert the article into
1053           ;; `nntp-server-buffer' - so we'll save some time by not having to
1054           ;; copy it from the server buffer into the article buffer.
1055
1056           ;; We only request an article by message-id when we do not have the
1057           ;; headers for it, so we'll have to get those.
1058           (when (stringp article)
1059             (let ((gnus-override-method gnus-refer-article-method))
1060               (gnus-read-header article)))
1061
1062           ;; If the article number is negative, that means that this article
1063           ;; doesn't belong in this newsgroup (possibly), so we find its
1064           ;; message-id and request it by id instead of number.
1065           (when (and (numberp article)
1066                      gnus-summary-buffer
1067                      (get-buffer gnus-summary-buffer)
1068                      (buffer-name (get-buffer gnus-summary-buffer)))
1069             (save-excursion
1070               (set-buffer gnus-summary-buffer)
1071               (let ((header (gnus-summary-article-header article)))
1072                 (if (< article 0)
1073                     (cond 
1074                      ((memq article gnus-newsgroup-sparse)
1075                       ;; This is a sparse gap article.
1076                       (setq do-update-line article)
1077                       (setq article (mail-header-id header))
1078                       (let ((gnus-override-method gnus-refer-article-method))
1079                         (gnus-read-header article))
1080                       (setq gnus-newsgroup-sparse
1081                             (delq article gnus-newsgroup-sparse)))
1082                      ((vectorp header)
1083                       ;; It's a real article.
1084                       (setq article (mail-header-id header)))
1085                      (t
1086                       ;; It is an extracted pseudo-article.
1087                       (setq article 'pseudo)
1088                       (gnus-request-pseudo-article header))))
1089                 
1090                 (let ((method (gnus-find-method-for-group 
1091                                gnus-newsgroup-name)))
1092                   (if (not (eq (car method) 'nneething))
1093                       ()
1094                     (let ((dir (concat (file-name-as-directory (nth 1 method))
1095                                        (mail-header-subject header))))
1096                       (if (file-directory-p dir)
1097                           (progn
1098                             (setq article 'nneething)
1099                             (gnus-group-enter-directory dir)))))))))
1100
1101           (cond
1102            ;; Refuse to select canceled articles.
1103            ((and (numberp article)
1104                  gnus-summary-buffer
1105                  (get-buffer gnus-summary-buffer)
1106                  (buffer-name (get-buffer gnus-summary-buffer))
1107                  (eq (cdr (save-excursion
1108                             (set-buffer gnus-summary-buffer)
1109                             (assq article gnus-newsgroup-reads)))
1110                      gnus-canceled-mark))
1111             nil)
1112            ;; We first check `gnus-original-article-buffer'.
1113            ((and (get-buffer gnus-original-article-buffer)
1114                  (numberp article)
1115                  (save-excursion
1116                    (set-buffer gnus-original-article-buffer)
1117                    (and (equal (car gnus-original-article) group)
1118                         (eq (cdr gnus-original-article) article))))
1119             (insert-buffer-substring gnus-original-article-buffer)
1120             'article)
1121            ;; Check the backlog.
1122            ((and gnus-keep-backlog
1123                  (gnus-backlog-request-article group article (current-buffer)))
1124             'article)
1125            ;; Check asynchronous pre-fetch.
1126            ((gnus-async-request-fetched-article group article (current-buffer))
1127             (gnus-async-prefetch-next group article gnus-summary-buffer)
1128             'article)
1129            ;; Check the cache.
1130            ((and gnus-use-cache
1131                  (numberp article)
1132                  (gnus-cache-request-article article group))
1133             'article)
1134            ;; Get the article and put into the article buffer.
1135            ((or (stringp article) (numberp article))
1136             (let ((gnus-override-method
1137                    (and (stringp article) gnus-refer-article-method))
1138                   (buffer-read-only nil))
1139               (erase-buffer)
1140               (gnus-kill-all-overlays)
1141               (when (gnus-request-article article group (current-buffer))
1142                 (when (numberp article)
1143                   (gnus-async-prefetch-next group article gnus-summary-buffer)
1144                   (when gnus-keep-backlog
1145                     (gnus-backlog-enter-article 
1146                      group article (current-buffer))))
1147                 'article)))
1148            ;; It was a pseudo.
1149            (t article)))
1150
1151       ;; Take the article from the original article buffer
1152       ;; and place it in the buffer it's supposed to be in.
1153       (when (and (get-buffer gnus-article-buffer)
1154                  ;;(numberp article)
1155                  (equal (buffer-name (current-buffer))
1156                         (buffer-name (get-buffer gnus-article-buffer))))
1157         (save-excursion
1158           (if (get-buffer gnus-original-article-buffer)
1159               (set-buffer (get-buffer gnus-original-article-buffer))
1160             (set-buffer (get-buffer-create gnus-original-article-buffer))
1161             (buffer-disable-undo (current-buffer))
1162             (setq major-mode 'gnus-original-article-mode)
1163             (setq buffer-read-only t)
1164             (gnus-add-current-to-buffer-list))
1165           (let (buffer-read-only)
1166             (erase-buffer)
1167             (insert-buffer-substring gnus-article-buffer))
1168           (setq gnus-original-article (cons group article))))
1169     
1170       ;; Update sparse articles.
1171       (when (and do-update-line
1172                  (or (numberp article)
1173                      (stringp article)))
1174         (let ((buf (current-buffer)))
1175           (set-buffer gnus-summary-buffer)
1176           (gnus-summary-update-article do-update-line)
1177           (gnus-summary-goto-subject do-update-line nil t)
1178           (set-window-point (get-buffer-window (current-buffer) t)
1179                             (point))
1180           (set-buffer buf))))))
1181
1182 (defun gnus-article-date-ut (&optional type highlight)
1183   "Convert DATE date to universal time in the current article.
1184 If TYPE is `local', convert to local time; if it is `lapsed', output
1185 how much time has lapsed since DATE."
1186   (interactive (list 'ut t))
1187   (let ((headers (or gnus-current-headers (gnus-summary-article-header))))
1188     (save-excursion
1189       (set-buffer gnus-article-buffer)
1190       (article-date-ut type highlight headers))))
1191
1192 ;;;
1193 ;;; Article editing
1194 ;;;
1195
1196 (defvar gnus-article-edit-mode-hook nil
1197   "*Hook run in article edit mode buffers.")
1198
1199 (defvar gnus-article-edit-done-function nil)
1200
1201 (defvar gnus-article-edit-mode-map nil)
1202
1203 (unless gnus-article-edit-mode-map 
1204   (setq gnus-article-edit-mode-map (copy-keymap text-mode-map))
1205
1206   (gnus-define-keys gnus-article-edit-mode-map
1207     "\C-c\C-c" gnus-article-edit-done
1208     "\C-c\C-k" gnus-article-edit-exit)
1209
1210   (gnus-define-keys (gnus-article-edit-wash-map
1211                      "\C-c\C-w" gnus-article-edit-mode-map)
1212     "f" gnus-article-edit-full-stops))
1213
1214 (defun gnus-article-edit-mode ()
1215   "Major mode for editing articles.
1216 This is an extended text-mode.
1217
1218 \\{gnus-article-edit-mode-map}"
1219   (interactive)
1220   (kill-all-local-variables)
1221   (setq major-mode 'gnus-article-edit-mode)
1222   (setq mode-name "Article Edit")
1223   (use-local-map gnus-article-edit-mode-map)
1224   (make-local-variable 'gnus-article-edit-done-function)
1225   (make-local-variable 'gnus-prev-winconf)
1226   (setq buffer-read-only nil)
1227   (buffer-enable-undo)
1228   (widen)
1229   (run-hooks 'text-mode 'gnus-article-edit-mode-hook))
1230
1231 (defun gnus-article-edit (&optional force)
1232   "Edit the current article.
1233 This will have permanent effect only in mail groups.
1234 If FORCE is non-nil, allow editing of articles even in read-only
1235 groups."
1236   (interactive "P")
1237   (when (and (not force)
1238              (gnus-group-read-only-p))
1239     (error "The current newsgroup does not support article editing."))
1240   (gnus-article-edit-article
1241    `(lambda ()
1242       (gnus-summary-edit-article-done
1243        ,(or (mail-header-references gnus-current-headers) "")
1244        ,(gnus-group-read-only-p) ,gnus-summary-buffer))))
1245
1246 (defun gnus-article-edit-article (exit-func)
1247   "Start editing the contents of the current article buffer."
1248   (let ((winconf (current-window-configuration)))
1249     (set-buffer gnus-article-buffer)
1250     (gnus-article-edit-mode)
1251     (set-text-properties (point-min) (point-max) nil)
1252     (gnus-configure-windows 'edit-article)
1253     (setq gnus-article-edit-done-function exit-func)
1254     (setq gnus-prev-winconf winconf)
1255     (gnus-message 6 "C-c C-c to end edits")))
1256
1257 (defun gnus-article-edit-done ()
1258   "Update the article edits and exit."
1259   (interactive)
1260   (let ((func gnus-article-edit-done-function)
1261         (buf (current-buffer))
1262         (start (window-start)))
1263     (gnus-article-edit-exit)
1264     (let ((cur (current-buffer)))
1265       (save-excursion
1266         (set-buffer buf)
1267         (let ((buffer-read-only nil))
1268           (funcall func)))
1269       (set-buffer buf)
1270       (set-window-start (get-buffer-window buf) start)
1271       (set-window-point (get-buffer-window buf) (point)))))
1272
1273 (defun gnus-article-edit-exit ()
1274   "Exit the article editing without updating."
1275   (interactive)
1276   ;; We remove all text props from the article buffer.
1277   (let ((buf (format "%s" (buffer-string)))
1278         (curbuf (current-buffer))
1279         (p (point))
1280         (window-start (window-start)))
1281     (erase-buffer)
1282     (insert buf)
1283     (let ((winconf gnus-prev-winconf))
1284       (gnus-article-mode)
1285       ;; The cache and backlog have to be flushed somewhat.
1286       (when gnus-use-cache
1287         (gnus-cache-update-article      
1288          (car gnus-article-current) (cdr gnus-article-current)))
1289       (when gnus-keep-backlog
1290         (gnus-backlog-remove-article 
1291          (car gnus-article-current) (cdr gnus-article-current)))
1292       ;; Flush original article as well.
1293       (save-excursion
1294         (when (get-buffer gnus-original-article-buffer)
1295           (set-buffer gnus-original-article-buffer)
1296           (setq gnus-original-article nil)))
1297       (set-window-configuration winconf)
1298       ;; Tippy-toe some to make sure that point remains where it was.
1299       (let ((buf (current-buffer)))
1300         (set-buffer curbuf)
1301         (set-window-start (get-buffer-window (current-buffer)) window-start)
1302         (goto-char p)
1303         (set-buffer buf)))))
1304       
1305 (defun gnus-article-edit-full-stops ()
1306   "Interactively repair spacing at end of sentences."
1307   (interactive)
1308   (save-excursion
1309     (goto-char (point-min))
1310     (search-forward-regexp "^$" nil t)
1311     (let ((case-fold-search nil))
1312       (query-replace-regexp "\\([.!?][])}]* \\)\\([[({A-Z]\\)" "\\1 \\2"))))
1313
1314 ;;; 
1315 ;;; Article highlights
1316 ;;;
1317
1318 ;; Written by Per Abrahamsen <abraham@iesd.auc.dk>.
1319
1320 ;;; Internal Variables:
1321
1322 (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_=#$@~`%&*+|\\/]"
1323   "*Regular expression that matches URLs.")
1324
1325 (defvar gnus-button-alist 
1326   `(("\\bin\\( +article\\)? +\\(<\\([^\n @<>]+@[^\n @<>]+\\)>\\)" 2 
1327      t gnus-button-message-id 3)
1328     ("\\(<?\\(url: ?\\)?news:\\([^>\n\t ]*\\)>?\\)" 1 t
1329      gnus-button-message-id 3)
1330     ("\\(<URL: *\\)?mailto: *\\([^> \n\t]+\\)>?" 0 t gnus-button-reply 2)
1331     ;; This is how URLs _should_ be embedded in text...
1332     ("<URL: *\\([^\n\r>]*\\)>" 0 t gnus-button-url 1)
1333     ;; Next regexp stolen from highlight-headers.el.
1334     ;; Modified by Vladimir Alexiev.
1335     (,gnus-button-url-regexp 0 t gnus-button-url 0))
1336   "Alist of regexps matching buttons in article bodies.
1337
1338 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
1339 REGEXP: is the string matching text around the button,
1340 BUTTON: is the number of the regexp grouping actually matching the button,
1341 FORM: is a lisp expression which must eval to true for the button to
1342 be added, 
1343 CALLBACK: is the function to call when the user push this button, and each
1344 PAR: is a number of a regexp grouping whose text will be passed to CALLBACK.
1345
1346 CALLBACK can also be a variable, in that case the value of that
1347 variable it the real callback function.")
1348
1349 (defvar gnus-header-button-alist 
1350   `(("^\\(References\\|Message-I[Dd]\\):" "<[^>]+>"
1351      0 t gnus-button-message-id 0)
1352     ("^\\(From\\|Reply-To\\):" ": *\\(.+\\)$" 1 t gnus-button-reply 1)
1353     ("^\\(Cc\\|To\\):" "[^ \t\n<>,()\"]+@[^ \t\n<>,()\"]+" 
1354      0 t gnus-button-mailto 0)
1355     ("^X-[Uu][Rr][Ll]:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
1356     ("^[^:]+:" ,gnus-button-url-regexp 0 t gnus-button-url 0)
1357     ("^[^:]+:" "\\(<\\(url: \\)?news:\\([^>\n ]*\\)>\\)" 1 t
1358      gnus-button-message-id 3))
1359   "Alist of headers and regexps to match buttons in article heads.
1360
1361 This alist is very similar to `gnus-button-alist', except that each
1362 alist has an additional HEADER element first in each entry:
1363
1364 \(HEADER REGEXP BUTTON FORM CALLBACK PAR)
1365
1366 HEADER is a regexp to match a header.  For a fuller explanation, see
1367 `gnus-button-alist'.")
1368
1369 (defvar gnus-button-regexp nil)
1370 (defvar gnus-button-marker-list nil)
1371 ;; Regexp matching any of the regexps from `gnus-button-alist'.
1372
1373 (defvar gnus-button-last nil)
1374 ;; The value of `gnus-button-alist' when `gnus-button-regexp' was build.
1375
1376 ;;; Commands:
1377
1378 (defun gnus-article-push-button (event)
1379   "Check text under the mouse pointer for a callback function.
1380 If the text under the mouse pointer has a `gnus-callback' property,
1381 call it with the value of the `gnus-data' text property."
1382   (interactive "e")
1383   (set-buffer (window-buffer (posn-window (event-start event))))
1384   (let* ((pos (posn-point (event-start event)))
1385          (data (get-text-property pos 'gnus-data))
1386          (fun (get-text-property pos 'gnus-callback)))
1387     (if fun (funcall fun data))))
1388
1389 (defun gnus-article-press-button ()
1390   "Check text at point for a callback function.
1391 If the text at point has a `gnus-callback' property,
1392 call it with the value of the `gnus-data' text property."
1393   (interactive)
1394   (let* ((data (get-text-property (point) 'gnus-data))
1395          (fun (get-text-property (point) 'gnus-callback)))
1396     (if fun (funcall fun data))))
1397
1398 (defun gnus-article-prev-button (n)
1399   "Move point to N buttons backward.
1400 If N is negative, move forward instead."
1401   (interactive "p")
1402   (gnus-article-next-button (- n)))
1403
1404 (defun gnus-article-next-button (n)
1405   "Move point to N buttons forward.
1406 If N is negative, move backward instead."
1407   (interactive "p")
1408   (let ((function (if (< n 0) 'previous-single-property-change
1409                     'next-single-property-change))
1410         (inhibit-point-motion-hooks t)
1411         (backward (< n 0))
1412         (limit (if (< n 0) (point-min) (point-max))))
1413     (setq n (abs n))
1414     (while (and (not (= limit (point)))
1415                 (> n 0))
1416       ;; Skip past the current button.
1417       (when (get-text-property (point) 'gnus-callback)
1418         (goto-char (funcall function (point) 'gnus-callback nil limit)))
1419       ;; Go to the next (or previous) button.
1420       (gnus-goto-char (funcall function (point) 'gnus-callback nil limit))
1421       ;; Put point at the start of the button.
1422       (when (and backward (not (get-text-property (point) 'gnus-callback)))
1423         (goto-char (funcall function (point) 'gnus-callback nil limit)))
1424       ;; Skip past intangible buttons.
1425       (when (get-text-property (point) 'intangible)
1426         (incf n))
1427       (decf n))
1428     (unless (zerop n)
1429       (gnus-message 5 "No more buttons"))
1430     n))
1431
1432 (defun gnus-article-highlight (&optional force)
1433   "Highlight current article.
1434 This function calls `gnus-article-highlight-headers',
1435 `gnus-article-highlight-citation', 
1436 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
1437 do the highlighting.  See the documentation for those functions."
1438   (interactive (list 'force))
1439   (gnus-article-highlight-headers)
1440   (gnus-article-highlight-citation force)
1441   (gnus-article-highlight-signature)
1442   (gnus-article-add-buttons force)
1443   (gnus-article-add-buttons-to-head))
1444
1445 (defun gnus-article-highlight-some (&optional force)
1446   "Highlight current article.
1447 This function calls `gnus-article-highlight-headers',
1448 `gnus-article-highlight-signature', and `gnus-article-add-buttons' to
1449 do the highlighting.  See the documentation for those functions."
1450   (interactive (list 'force))
1451   (gnus-article-highlight-headers)
1452   (gnus-article-highlight-signature)
1453   (gnus-article-add-buttons))
1454
1455 (defun gnus-article-highlight-headers ()
1456   "Highlight article headers as specified by `gnus-header-face-alist'."
1457   (interactive)
1458   (save-excursion
1459     (set-buffer gnus-article-buffer)
1460     (save-restriction
1461       (let ((alist gnus-header-face-alist)
1462             (buffer-read-only nil)
1463             (case-fold-search t)
1464             (inhibit-point-motion-hooks t)
1465             entry regexp header-face field-face from hpoints fpoints)
1466         (goto-char (point-min))
1467         (when (search-forward "\n\n" nil t)
1468           (narrow-to-region (1- (point)) (point-min))
1469           (while (setq entry (pop alist))
1470             (goto-char (point-min))
1471             (setq regexp (concat "^\\("
1472                                  (if (string-equal "" (nth 0 entry))
1473                                      "[^\t ]"
1474                                    (nth 0 entry))
1475                                  "\\)")
1476                   header-face (nth 1 entry)
1477                   field-face (nth 2 entry))
1478             (while (and (re-search-forward regexp nil t)
1479                         (not (eobp)))
1480               (beginning-of-line)
1481               (setq from (point))
1482               (or (search-forward ":" nil t)
1483                   (forward-char 1))
1484               (when (and header-face
1485                          (not (memq (point) hpoints)))
1486                 (push (point) hpoints)
1487                 (gnus-put-text-property from (point) 'face header-face))
1488               (when (and field-face
1489                          (not (memq (setq from (point)) fpoints)))
1490                 (push from fpoints)
1491                 (if (re-search-forward "^[^ \t]" nil t)
1492                     (forward-char -2)
1493                   (goto-char (point-max)))
1494                 (gnus-put-text-property from (point) 'face field-face)))))))))
1495
1496 (defun gnus-article-highlight-signature ()
1497   "Highlight the signature in an article.
1498 It does this by highlighting everything after
1499 `gnus-signature-separator' using `gnus-signature-face'." 
1500   (interactive)
1501   (save-excursion
1502     (set-buffer gnus-article-buffer)
1503     (let ((buffer-read-only nil)
1504           (inhibit-point-motion-hooks t))
1505       (save-restriction
1506         (when (and gnus-signature-face
1507                    (article-narrow-to-signature))
1508           (gnus-overlay-put (gnus-make-overlay (point-min) (point-max))
1509                             'face gnus-signature-face)
1510           (widen)
1511           (article-search-signature)
1512           (let ((start (match-beginning 0))
1513                 (end (set-marker (make-marker) (1+ (match-end 0)))))
1514             (gnus-article-add-button start (1- end) 'gnus-signature-toggle
1515                                      end)))))))
1516
1517 (defun gnus-article-add-buttons (&optional force)
1518   "Find external references in the article and make buttons of them.
1519 \"External references\" are things like Message-IDs and URLs, as
1520 specified by `gnus-button-alist'."
1521   (interactive (list 'force))
1522   (save-excursion
1523     (set-buffer gnus-article-buffer)
1524     ;; Remove all old markers.
1525     (while gnus-button-marker-list
1526       (set-marker (pop gnus-button-marker-list) nil))
1527     (let ((buffer-read-only nil)
1528           (inhibit-point-motion-hooks t)
1529           (case-fold-search t)
1530           (alist gnus-button-alist)
1531           beg entry regexp)
1532       (goto-char (point-min))
1533       ;; We skip the headers.
1534       (unless (search-forward "\n\n" nil t)
1535         (goto-char (point-max)))
1536       (setq beg (point))
1537       (while (setq entry (pop alist))
1538         (setq regexp (car entry))
1539         (goto-char beg)
1540         (while (re-search-forward regexp nil t)
1541           (let* ((start (and entry (match-beginning (nth 1 entry))))
1542                  (end (and entry (match-end (nth 1 entry))))
1543                  (from (match-beginning 0)))
1544             (when (or (eq t (nth 1 entry))
1545                       (eval (nth 1 entry)))
1546               ;; That optional form returned non-nil, so we add the
1547               ;; button. 
1548               (gnus-article-add-button 
1549                start end 'gnus-button-push 
1550                (car (push (set-marker (make-marker) from)
1551                           gnus-button-marker-list))))))))))
1552
1553 ;; Add buttons to the head of an article.
1554 (defun gnus-article-add-buttons-to-head ()
1555   "Add buttons to the head of the article."
1556   (interactive)
1557   (save-excursion
1558     (set-buffer gnus-article-buffer)
1559     (let ((buffer-read-only nil)
1560           (inhibit-point-motion-hooks t)
1561           (case-fold-search t)
1562           (alist gnus-header-button-alist)
1563           entry beg end)
1564       (nnheader-narrow-to-headers)
1565       (while alist
1566         ;; Each alist entry.
1567         (setq entry (car alist)
1568               alist (cdr alist))
1569         (goto-char (point-min))
1570         (while (re-search-forward (car entry) nil t)
1571           ;; Each header matching the entry.
1572           (setq beg (match-beginning 0))
1573           (setq end (or (and (re-search-forward "^[^ \t]" nil t)
1574                              (match-beginning 0))
1575                         (point-max)))
1576           (goto-char beg)
1577           (while (re-search-forward (nth 1 entry) end t)
1578             ;; Each match within a header.
1579             (let* ((from (match-beginning 0))
1580                    (entry (cdr entry))
1581                    (start (match-beginning (nth 1 entry)))
1582                    (end (match-end (nth 1 entry)))
1583                    (form (nth 2 entry)))
1584               (goto-char (match-end 0))
1585               (and (eval form)
1586                    (gnus-article-add-button 
1587                     start end (nth 3 entry)
1588                     (buffer-substring (match-beginning (nth 4 entry))
1589                                       (match-end (nth 4 entry)))))))
1590           (goto-char end))))
1591     (widen)))
1592
1593 ;;; External functions:
1594
1595 (defun gnus-article-add-button (from to fun &optional data)
1596   "Create a button between FROM and TO with callback FUN and data DATA."
1597   (and gnus-article-button-face
1598        (gnus-overlay-put (gnus-make-overlay from to)
1599                          'face gnus-article-button-face))
1600   (gnus-add-text-properties 
1601    from to
1602    (nconc (and gnus-article-mouse-face
1603                (list gnus-mouse-face-prop gnus-article-mouse-face))
1604           (list 'gnus-callback fun)
1605           (and data (list 'gnus-data data)))))
1606
1607 ;;; Internal functions:
1608
1609 (defun gnus-signature-toggle (end)
1610   (save-excursion
1611     (set-buffer gnus-article-buffer)
1612     (let ((buffer-read-only nil)
1613           (inhibit-point-motion-hooks t))
1614       (if (get-text-property end 'invisible)
1615           (article-unhide-text end (point-max))
1616         (article-hide-text end (point-max) gnus-hidden-properties)))))
1617
1618 (defun gnus-button-entry ()
1619   ;; Return the first entry in `gnus-button-alist' matching this place.
1620   (let ((alist gnus-button-alist)
1621         (entry nil))
1622     (while alist
1623       (setq entry (pop alist))
1624       (if (looking-at (car entry))
1625           (setq alist nil)
1626         (setq entry nil)))
1627     entry))
1628
1629 (defun gnus-button-push (marker)
1630   ;; Push button starting at MARKER.
1631   (save-excursion
1632     (set-buffer gnus-article-buffer)
1633     (goto-char marker)
1634     (let* ((entry (gnus-button-entry))
1635            (inhibit-point-motion-hooks t)
1636            (fun (nth 3 entry))
1637            (args (mapcar (lambda (group) 
1638                            (let ((string (buffer-substring
1639                                           (match-beginning group)
1640                                           (match-end group))))
1641                              (gnus-set-text-properties
1642                               0 (length string) nil string)
1643                              string))
1644                          (nthcdr 4 entry))))
1645       (cond
1646        ((fboundp fun)
1647         (apply fun args))
1648        ((and (boundp fun)
1649              (fboundp (symbol-value fun)))
1650         (apply (symbol-value fun) args))
1651        (t
1652         (gnus-message 1 "You must define `%S' to use this button"
1653                       (cons fun args)))))))
1654
1655 (defun gnus-button-message-id (message-id)
1656   "Fetch MESSAGE-ID."
1657   (save-excursion
1658     (set-buffer gnus-summary-buffer)
1659     (gnus-summary-refer-article message-id)))
1660
1661 (defun gnus-button-mailto (address)
1662   ;; Mail to ADDRESS.
1663   (set-buffer (gnus-copy-article-buffer))
1664   (message-reply address))
1665
1666 (defun gnus-button-reply (address)
1667   ;; Reply to ADDRESS.
1668   (message-reply address))
1669
1670 (defun gnus-button-url (address)
1671   "Browse ADDRESS."
1672   (funcall browse-url-browser-function address))
1673
1674 ;;; Next/prev buttons in the article buffer.
1675
1676 (defvar gnus-next-page-line-format "%{%(Next page...%)%}\n")
1677 (defvar gnus-prev-page-line-format "%{%(Previous page...%)%}\n")
1678
1679 (defvar gnus-prev-page-map nil)
1680 (unless gnus-prev-page-map
1681   (setq gnus-prev-page-map (make-sparse-keymap))
1682   (define-key gnus-prev-page-map gnus-mouse-2 'gnus-button-prev-page)
1683   (define-key gnus-prev-page-map "\r" 'gnus-button-prev-page))
1684
1685 (defun gnus-insert-prev-page-button ()
1686   (let ((buffer-read-only nil))
1687     (gnus-eval-format 
1688      gnus-prev-page-line-format nil
1689      `(gnus-prev t local-map ,gnus-prev-page-map
1690                  gnus-callback gnus-article-button-prev-page))))
1691
1692 (defvar gnus-next-page-map nil)
1693 (unless gnus-next-page-map
1694   (setq gnus-next-page-map (make-keymap))
1695   (suppress-keymap gnus-prev-page-map)
1696   (define-key gnus-next-page-map gnus-mouse-2 'gnus-button-next-page)
1697   (define-key gnus-next-page-map "\r" 'gnus-button-next-page))
1698
1699 (defun gnus-button-next-page ()
1700   "Go to the next page."
1701   (interactive)
1702   (let ((win (selected-window)))
1703     (select-window (get-buffer-window gnus-article-buffer t))
1704     (gnus-article-next-page)
1705     (select-window win)))
1706
1707 (defun gnus-button-prev-page ()
1708   "Go to the prev page."
1709   (interactive)
1710   (let ((win (selected-window)))
1711     (select-window (get-buffer-window gnus-article-buffer t))
1712     (gnus-article-prev-page)
1713     (select-window win)))
1714
1715 (defun gnus-insert-next-page-button ()
1716   (let ((buffer-read-only nil))
1717     (gnus-eval-format gnus-next-page-line-format nil
1718                       `(gnus-next t local-map ,gnus-next-page-map
1719                                   gnus-callback 
1720                                   gnus-article-button-next-page))))
1721
1722 (defun gnus-article-button-next-page (arg)
1723   "Go to the next page."
1724   (interactive "P")
1725   (let ((win (selected-window)))
1726     (select-window (get-buffer-window gnus-article-buffer t))
1727     (gnus-article-next-page)
1728     (select-window win)))
1729
1730 (defun gnus-article-button-prev-page (arg)
1731   "Go to the prev page."
1732   (interactive "P")
1733   (let ((win (selected-window)))
1734     (select-window (get-buffer-window gnus-article-buffer t))
1735     (gnus-article-prev-page)
1736     (select-window win)))
1737
1738 (provide 'gnus-art)
1739
1740 ;;; gnus-art.el ends here