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