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