842cee97228c9cecf577ee0a236265e12e5959d6
[gnus] / lisp / gnus-vm.el
1 ;;; gnus-vm.el --- vm interface for Gnus
2 ;; Copyright (C) 1994,95 Free Software Foundation, Inc.
3
4 ;; Author: Per Persson <pp@solace.mh.se>
5 ;; Keywords: news, mail
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
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Commentary:
24
25 ;; Major contributors: 
26 ;;      Christian Limpach <Christian.Limpach@nice.ch>
27 ;; Some code stolen from: 
28 ;;      Rick Sladkey <jrs@world.std.com>
29
30 ;;; Code:
31
32 (require 'sendmail)
33 (require 'gnus)
34 (require 'gnus-msg)
35
36 (eval-when-compile
37   (autoload 'vm-mode "vm")
38   (autoload 'vm-save-message "vm")
39   (autoload 'vm-forward-message "vm")
40   (autoload 'vm-reply "vm")
41   (autoload 'vm-mail "vm"))
42
43 (defvar gnus-vm-inhibit-window-system nil
44   "Inhibit loading `win-vm' if using a window-system.
45 Has to be set before gnus-vm is loaded.")
46
47 (or gnus-vm-inhibit-window-system
48     (condition-case nil
49         (if window-system
50             (require 'win-vm))
51       (error nil)))
52
53 (if (not (featurep 'vm))
54     (load "vm"))
55
56 (defun gnus-vm-make-folder (&optional buffer)
57   (let ((article (or buffer (current-buffer)))
58         (tmp-folder (generate-new-buffer " *tmp-folder*"))
59         (start (point-min))
60         (end (point-max)))
61     (set-buffer tmp-folder)
62     (insert-buffer-substring article start end)
63     (goto-char (point-min))
64     (if (looking-at "^\\(From [^ ]+ \\).*$")
65         (replace-match (concat "\\1" (current-time-string)))
66       (insert "From " gnus-newsgroup-name " "
67               (current-time-string) "\n"))
68     (while (re-search-forward "\n\nFrom " nil t)
69       (replace-match "\n\n>From "))
70     ;; insert a newline, otherwise the last line gets lost
71     (goto-char (point-max))
72     (insert "\n")
73     (vm-mode)
74     tmp-folder))
75   
76 (defun gnus-summary-save-article-vm (arg)
77   "Append the current article to a vm folder.
78 If N is a positive number, save the N next articles.
79 If N is a negative number, save the N previous articles.
80 If N is nil and any articles have been marked with the process mark,
81 save those articles instead."
82   (interactive "P")
83   (let ((gnus-default-article-saver 'gnus-summary-save-in-vm))
84     (gnus-summary-save-article arg)))
85
86 (defun gnus-summary-save-in-vm (&optional folder)
87   (interactive)
88   (let ((default-name
89           (funcall gnus-mail-save-name gnus-newsgroup-name
90                    gnus-current-headers gnus-newsgroup-last-mail)))
91     (or folder
92         (setq folder
93               (read-file-name
94                (concat "Save article in VM folder: (default "
95                        (file-name-nondirectory default-name) ") ")
96                (file-name-directory default-name)
97                default-name)))
98     (setq folder
99           (expand-file-name folder
100                             (and default-name
101                                  (file-name-directory default-name))))
102     (gnus-make-directory (file-name-directory folder))
103     (set-buffer gnus-article-buffer)
104     (save-excursion
105       (save-restriction
106         (widen)
107         (let ((vm-folder (gnus-vm-make-folder)))
108           (vm-save-message folder)
109           (kill-buffer vm-folder))))
110     ;; Remember the directory name to save articles.
111     (setq gnus-newsgroup-last-mail folder)))
112   
113 (defun gnus-mail-forward-using-vm (&optional buffer)
114   "Forward the current message to another user using vm."
115   (let ((gnus-buffer (or buffer (current-buffer)))
116         (subject (concat "[" gnus-newsgroup-name "] "
117                          (or (gnus-fetch-field "Subject") ""))))
118     (or (featurep 'win-vm)
119         (if gnus-use-full-window
120             (pop-to-buffer gnus-article-buffer)
121           (switch-to-buffer gnus-article-buffer)))
122     (set-buffer gnus-article-buffer)
123     (save-excursion
124       (save-restriction
125         (widen)
126         (let ((vm-folder (gnus-vm-make-folder))
127               (vm-forward-message-hook
128                (append (symbol-value 'vm-forward-message-hook)
129                        '((lambda ()
130                            (save-excursion
131                              (mail-position-on-field "Subject")
132                              (beginning-of-line)
133                              (looking-at "^\\(Subject: \\).*$")
134                              (replace-match (concat "\\1" subject))))))))
135           (vm-forward-message)
136           (gnus-vm-init-reply-buffer gnus-buffer)
137           (kill-buffer vm-folder))))))
138
139 (defun gnus-vm-init-reply-buffer (buffer)
140   (make-local-variable 'gnus-summary-buffer)
141   (setq gnus-summary-buffer buffer)
142   (set 'vm-mail-buffer nil)
143   (use-local-map (copy-keymap (current-local-map)))
144   (local-set-key "\C-c\C-y" 'gnus-yank-article))
145   
146 (defun gnus-mail-reply-using-vm (&optional yank)
147   "Compose reply mail using vm.
148 Optional argument YANK means yank original article.
149 The command \\[vm-yank-message] yank the original message into current buffer."
150   (let ((gnus-buffer (current-buffer)))
151     (set-buffer gnus-article-buffer)
152     (save-excursion
153       (save-restriction
154         (widen)
155         (let ((vm-folder (gnus-vm-make-folder gnus-article-buffer)))
156           (vm-reply 1)
157           (gnus-vm-init-reply-buffer gnus-buffer)
158           (setq gnus-buffer (current-buffer))
159           (and yank
160                ;; nil will (magically :-)) yank the current article
161                (gnus-yank-article nil))
162           (kill-buffer vm-folder))))
163     (if (featurep 'win-vm) nil
164       (pop-to-buffer gnus-buffer))))
165
166 (defun gnus-mail-other-window-using-vm ()
167   "Compose mail in the other window using VM."
168   (interactive)
169   (let ((gnus-buffer (current-buffer)))
170     (vm-mail)
171     (gnus-vm-init-reply-buffer gnus-buffer)))
172
173 (defun gnus-yank-article (article &optional prefix)
174   ;; Based on vm-yank-message by Kyle Jones.
175   "Yank article number N into the current buffer at point.
176 When called interactively N is read from the minibuffer.
177
178 This command is meant to be used in GNUS created Mail mode buffers;
179 the yanked article comes from the newsgroup containing the article
180 you are replying to or forwarding.
181
182 All article headers are yanked along with the text.  Point is left
183 before the inserted text, the mark after.  Any hook functions bound to
184 `mail-citation-hook' are run, after inserting the text and setting
185 point and mark.
186
187 Prefix arg means to ignore `mail-citation-hook', don't set the mark,
188 prepend the value of `vm-included-text-prefix' to every yanked line.
189 For backwards compatibility, if `mail-citation-hook' is set to nil,
190 `mail-yank-hooks' is run instead.  If that is also nil, a default
191 action is taken."
192   (interactive
193    (list
194     (let ((result 0)
195           default prompt)
196       (setq default (and gnus-summary-buffer
197                          (save-excursion
198                            (set-buffer gnus-summary-buffer)
199                            (and gnus-current-article
200                                 (int-to-string gnus-current-article))))
201             prompt (if default
202                        (format "Yank article number: (default %s) " default)
203                      "Yank article number: "))
204       (while (and (not (stringp result)) (zerop result))
205         (setq result (read-string prompt))
206         (and (string= result "") default (setq result default))
207         (or (string-match "^<.*>$" result)
208             (setq result (string-to-int result))))
209       result)
210     current-prefix-arg))
211   (if gnus-summary-buffer
212       (save-excursion
213         (let ((message (current-buffer))
214               (start (point)) end
215               (tmp (generate-new-buffer " *tmp-yank*")))
216           (set-buffer gnus-summary-buffer)
217           ;; Make sure the connection to the server is alive.
218           (or (gnus-server-opened (gnus-find-method-for-group
219                                    gnus-newsgroup-name))
220               (progn
221                 (gnus-check-news-server 
222                  (gnus-find-method-for-group gnus-newsgroup-name))
223                 (gnus-request-group gnus-newsgroup-name t)))
224           (and (stringp article) 
225                (let ((gnus-override-method gnus-refer-article-method))
226                  (gnus-read-header article)))
227           (gnus-request-article (or article
228                                     gnus-current-article)
229                                 gnus-newsgroup-name tmp)
230           (set-buffer tmp)
231           (run-hooks 'gnus-article-prepare-hook)
232           ;; Decode MIME message.
233           (if (and gnus-show-mime
234                    (gnus-fetch-field "Mime-Version"))
235               (funcall gnus-show-mime-method))
236           ;; Perform the article display hooks.
237           (let ((buffer-read-only nil))
238             (run-hooks 'gnus-article-display-hook))
239           (append-to-buffer message (point-min) (point-max))
240           (kill-buffer tmp)
241           (set-buffer message)
242           (setq end (point))
243           (goto-char start)
244           (if (or prefix
245                   (not (or mail-citation-hook mail-yank-hooks)))
246               (save-excursion
247                 (while (< (point) end)
248                   (insert (symbol-value 'vm-included-text-prefix))
249                   (forward-line 1)))
250             (push-mark end)
251             (cond
252              (mail-citation-hook (run-hooks 'mail-citation-hook))
253              (mail-yank-hooks (run-hooks 'mail-yank-hooks))))))))
254
255 (provide 'gnus-vm)
256
257 ;;; gnus-vm.el ends here.