Synch with Oort Gnus.
[elisp/gnus.git-] / lisp / gnus-vm.el
1 ;;; gnus-vm.el --- vm interface for Gnus
2
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000
4 ;;      Free Software Foundation, Inc.
5
6 ;; Author: Per Persson <pp@gnu.ai.mit.edu>
7 ;;         Katsumi Yamaoka <yamaoka@jpl.org>
8 ;; Keywords: news, mail
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; Major contributors:
30 ;;      Christian Limpach <Christian.Limpach@nice.ch>
31 ;; Some code stolen from:
32 ;;      Rick Sladkey <jrs@world.std.com>
33
34 ;;; Code:
35
36 (eval-when-compile (require 'cl))
37
38 (require 'gnus-art)
39
40 (eval-when-compile
41   (autoload 'vm-mode "vm")
42   (autoload 'vm-read-file-name "vm")
43   (autoload 'vm-save-message "vm"))
44
45 (when (not (featurep 'vm))
46   (load "vm"))
47
48 (defvar vm-folder-directory)
49 (defvar vm-folder-history)
50 (defvar vm-primary-inbox)
51 (defvar vm-use-toolbar)
52
53 (defun gnus-vm-make-folder (&optional buffer)
54   (let ((article (or buffer (current-buffer)))
55         (tmp-folder (generate-new-buffer " *tmp-folder*"))
56         (start (point-min))
57         (end (point-max)))
58     (set-buffer tmp-folder)
59     (insert-buffer-substring article start end)
60     (goto-char (point-min))
61     (if (looking-at "^\\(From [^ ]+ \\).*$")
62         (replace-match (concat "\\1" (current-time-string)))
63       (insert "From " gnus-newsgroup-name " "
64               (current-time-string) "\n"))
65     (while (re-search-forward "\n\nFrom " nil t)
66       (replace-match "\n\n>From "))
67     ;; insert a newline, otherwise the last line gets lost
68     (goto-char (point-max))
69     (insert "\n")
70     (let (mime-display-header-hook
71           mime-display-text/plain-hook mime-text-decode-hook
72           mime-view-define-keymap-hook mime-view-mode-hook)
73       (vm-mode))
74     tmp-folder))
75
76 (defvar gnus-summary-save-article-vm-folder nil)
77 (defvar gnus-summary-save-article-vm-count nil)
78
79 (defun gnus-summary-save-article-vm (&optional arg folder)
80   "Append the current article to a vm folder.
81 If N is a positive number, save the N next articles.
82 If N is a negative number, save the N previous articles.
83 If N is nil and any articles have been marked with the process mark,
84 save those articles instead."
85   (interactive
86    (let ((prefix-arg current-prefix-arg)
87          articles marks default-folder)
88      (setq default-folder (or (car vm-folder-history) vm-primary-inbox))
89      (if (numberp prefix-arg)
90          (setq articles prefix-arg)
91        (setq marks (delq nil (gnus-summary-work-articles nil))
92              articles (length marks)))
93      (list
94       prefix-arg
95       (unless (zerop articles)
96         (vm-read-file-name
97          (format
98           "Save %s in VM folder: "
99           (cond ((eq 1 articles)
100                  (if (or (not marks) (eq gnus-current-article (car marks)))
101                      "this article"
102                    "the marked article"))
103                 ((< 0 articles)
104                  (if marks
105                      (format "the marked %d articles" articles)
106                    (format "the %d next articles" articles)))
107                 ((> 0 articles)
108                  (format "the %d previous articles" (- articles)))))
109          (if default-folder "" vm-folder-directory)
110          nil nil default-folder 'vm-folder-history)))))
111   (if (interactive-p)
112       (unless folder
113         (error "No articles to be saved"))
114     (unless (setq folder (or folder gnus-summary-save-article-vm-folder))
115       (error "No VM folder is specified")))
116   (unwind-protect
117       (progn
118         (setq gnus-summary-save-article-vm-folder folder
119               gnus-summary-save-article-vm-count 0)
120         (let ((gnus-default-article-saver 'gnus-summary-save-in-vm)
121               mime-display-header-hook mime-display-text/plain-hook
122               mime-text-decode-hook mime-view-define-keymap-hook
123               mime-view-mode-hook)
124           (gnus-summary-save-article arg))
125         (cond ((eq 1 gnus-summary-save-article-vm-count)
126                (message "One article is saved in %s" folder))
127               ((< 0 gnus-summary-save-article-vm-count)
128                (message "%d articles are saved in %s"
129                         gnus-summary-save-article-vm-count folder))
130               (t
131                (message "Maybe no articles are saved in %s" folder))))
132     (setq gnus-summary-save-article-vm-folder nil
133           gnus-summary-save-article-vm-count nil)))
134
135 (defun gnus-summary-save-in-vm (&optional folder)
136   (interactive
137    (let (default-folder)
138      (setq default-folder (or (car vm-folder-history) vm-primary-inbox))
139      (list (vm-read-file-name "Save this article in VM folder: "
140                               (if default-folder "" vm-folder-directory)
141                               nil nil default-folder 'vm-folder-history))))
142   (unless (interactive-p)
143     (setq folder (or folder gnus-summary-save-article-vm-folder)))
144   (unless folder
145     (error "No VM folder is specified"))
146   (unless (interactive-p)
147     (message "Saving the article %d in %s..." gnus-current-article folder)
148     (when (numberp gnus-summary-save-article-vm-count)
149       (incf gnus-summary-save-article-vm-count)))
150   (save-window-excursion
151     (apply 'gnus-summary-select-article gnus-show-all-headers
152            (unless (interactive-p)
153              (list nil nil gnus-current-article)))
154     (gnus-eval-in-buffer-window gnus-original-article-buffer
155       (save-excursion
156         (save-restriction
157           (widen)
158           (let* ((vm-use-toolbar nil)
159                  (vm-folder (gnus-vm-make-folder)))
160             (vm-save-message folder)
161             (when (interactive-p)
162               (message "This article is saved in %s" folder))
163             (kill-buffer vm-folder)))))))
164
165 (provide 'gnus-vm)
166
167 ;;; gnus-vm.el ends here