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