ee32d743a6f734a0e695a0eeb6486b7c337e2d2f
[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     (let (vm-use-toolbar vm-use-menus)
74       (vm-mode))
75     tmp-folder))
76   
77 (defun gnus-summary-save-article-vm (&optional arg)
78   "Append the current article to a vm folder.
79 If N is a positive number, save the N next articles.
80 If N is a negative number, save the N previous articles.
81 If N is nil and any articles have been marked with the process mark,
82 save those articles instead."
83   (interactive "P")
84   (let ((gnus-default-article-saver 'gnus-summary-save-in-vm))
85     (gnus-summary-save-article arg)))
86
87 (defun gnus-summary-save-in-vm (&optional folder)
88   (interactive)
89   (let ((default-name
90           (funcall gnus-mail-save-name gnus-newsgroup-name
91                    gnus-current-headers gnus-newsgroup-last-mail)))
92     (or folder
93         (setq folder
94               (read-file-name
95                (concat "Save article in VM folder: (default "
96                        (file-name-nondirectory default-name) ") ")
97                (file-name-directory default-name)
98                default-name)))
99     (setq folder
100           (expand-file-name folder
101                             (and default-name
102                                  (file-name-directory default-name))))
103     (gnus-make-directory (file-name-directory folder))
104     (set-buffer gnus-article-buffer)
105     (save-excursion
106       (save-restriction
107         (widen)
108         (let ((vm-folder (gnus-vm-make-folder)))
109           (vm-save-message folder)
110           (kill-buffer vm-folder))))
111     ;; Remember the directory name to save articles.
112     (setq gnus-newsgroup-last-mail folder)))
113   
114 (defun gnus-mail-forward-using-vm (&optional buffer)
115   "Forward the current message to another user using vm."
116   (let* ((gnus-buffer (or buffer (current-buffer)))
117          (subject (gnus-forward-make-subject gnus-buffer)))
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     (gnus-copy-article-buffer)
123     (set-buffer gnus-article-copy)
124     (save-excursion
125       (save-restriction
126         (widen)
127         (let ((vm-folder (gnus-vm-make-folder))
128               (vm-forward-message-hook
129                (append (symbol-value 'vm-forward-message-hook)
130                        '((lambda ()
131                            (save-excursion
132                              (mail-position-on-field "Subject")
133                              (beginning-of-line)
134                              (looking-at "^\\(Subject: \\).*$")
135                              (replace-match (concat "\\1" subject))))))))
136           (vm-forward-message)
137           (gnus-vm-init-reply-buffer gnus-buffer)
138           (run-hooks 'gnus-mail-hook)
139           (kill-buffer vm-folder))))))
140
141 (defun gnus-vm-init-reply-buffer (buffer)
142   (make-local-variable 'gnus-summary-buffer)
143   (setq gnus-summary-buffer buffer)
144   (set 'vm-mail-buffer nil)
145   (use-local-map (copy-keymap (current-local-map)))
146   (local-set-key "\C-c\C-y" 'gnus-yank-article))
147   
148 (defun gnus-mail-reply-using-vm (&optional yank)
149   "Compose reply mail using vm.
150 Optional argument YANK means yank original article.
151 The command \\[vm-yank-message] yank the original message into current buffer."
152   (let ((gnus-buffer (current-buffer)))
153     (gnus-copy-article-buffer)
154     (set-buffer gnus-article-copy)
155     (save-excursion
156       (save-restriction
157         (widen)
158         (let ((vm-folder (gnus-vm-make-folder gnus-article-copy)))
159           (vm-reply 1)
160           (gnus-vm-init-reply-buffer gnus-buffer)
161           (setq gnus-buffer (current-buffer))
162           (and yank
163                ;; nil will (magically :-)) yank the current article
164                (gnus-yank-article nil))
165           (kill-buffer vm-folder))))
166     (if (featurep 'win-vm) nil
167       (pop-to-buffer gnus-buffer))
168     (run-hooks 'gnus-mail-hook)))
169
170 (defun gnus-mail-other-window-using-vm ()
171   "Compose mail in the other window using VM."
172   (interactive)
173   (let ((gnus-buffer (current-buffer)))
174     (vm-mail)
175     (gnus-vm-init-reply-buffer gnus-buffer))
176   (run-hooks 'gnus-mail-hook))
177
178 (defun gnus-yank-article (article &optional prefix)
179   ;; Based on vm-yank-message by Kyle Jones.
180   "Yank article number N into the current buffer at point.
181 When called interactively N is read from the minibuffer.
182
183 This command is meant to be used in GNUS created Mail mode buffers;
184 the yanked article comes from the newsgroup containing the article
185 you are replying to or forwarding.
186
187 All article headers are yanked along with the text.  Point is left
188 before the inserted text, the mark after.  Any hook functions bound to
189 `mail-citation-hook' are run, after inserting the text and setting
190 point and mark.
191
192 Prefix arg means to ignore `mail-citation-hook', don't set the mark,
193 prepend the value of `vm-included-text-prefix' to every yanked line.
194 For backwards compatibility, if `mail-citation-hook' is set to nil,
195 `mail-yank-hooks' is run instead.  If that is also nil, a default
196 action is taken."
197   (interactive
198    (list
199     (let ((result 0)
200           default prompt)
201       (setq default (and gnus-summary-buffer
202                          (save-excursion
203                            (set-buffer gnus-summary-buffer)
204                            (and gnus-current-article
205                                 (int-to-string gnus-current-article))))
206             prompt (if default
207                        (format "Yank article number: (default %s) " default)
208                      "Yank article number: "))
209       (while (and (not (stringp result)) (zerop result))
210         (setq result (read-string prompt))
211         (and (string= result "") default (setq result default))
212         (or (string-match "^<.*>$" result)
213             (setq result (string-to-int result))))
214       result)
215     current-prefix-arg))
216   (if gnus-summary-buffer
217       (save-excursion
218         (let ((message (current-buffer))
219               (start (point)) end
220               (tmp (generate-new-buffer " *tmp-yank*")))
221           (set-buffer gnus-summary-buffer)
222           ;; Make sure the connection to the server is alive.
223           (or (gnus-server-opened (gnus-find-method-for-group
224                                    gnus-newsgroup-name))
225               (progn
226                 (gnus-check-server 
227                  (gnus-find-method-for-group gnus-newsgroup-name))
228                 (gnus-request-group gnus-newsgroup-name t)))
229           (and (stringp article) 
230                (let ((gnus-override-method gnus-refer-article-method))
231                  (gnus-read-header article)))
232           (gnus-request-article (or article
233                                     gnus-current-article)
234                                 gnus-newsgroup-name tmp)
235           (set-buffer tmp)
236           (run-hooks 'gnus-article-prepare-hook)
237           ;; Decode MIME message.
238           (if (and gnus-show-mime
239                    (gnus-fetch-field "Mime-Version"))
240               (funcall gnus-show-mime-method))
241           ;; Perform the article display hooks.
242           (let ((buffer-read-only nil))
243             (run-hooks 'gnus-article-display-hook))
244           (append-to-buffer message (point-min) (point-max))
245           (kill-buffer tmp)
246           (set-buffer message)
247           (setq end (point))
248           (goto-char start)
249           (if (or prefix
250                   (not (or mail-citation-hook mail-yank-hooks)))
251               (save-excursion
252                 (while (< (point) end)
253                   (insert (symbol-value 'vm-included-text-prefix))
254                   (forward-line 1)))
255             (push-mark end)
256             (cond
257              (mail-citation-hook (run-hooks 'mail-citation-hook))
258              (mail-yank-hooks (run-hooks 'mail-yank-hooks))))))))
259
260 (provide 'gnus-vm)
261
262 ;;; gnus-vm.el ends here.