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