7679f3ea192f5f67e7baa00ed9932761e82df4d9
[gnus] / lisp / gnus-draft.el
1 ;;; gnus-draft.el --- draft message support for Gnus
2 ;; Copyright (C) 1997 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;; Keywords: news
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 ;;; Code:
27
28 (require 'gnus)
29 (require 'gnus-sum)
30 (require 'message)
31 (require 'gnus-msg)
32 (eval-when-compile (require 'cl))
33
34 ;;; Draft minor mode
35
36 (defvar gnus-draft-mode nil
37   "Minor mode for providing a draft summary buffers.")
38
39 (defvar gnus-draft-mode-map nil)
40
41 (unless gnus-draft-mode-map
42   (setq gnus-draft-mode-map (make-sparse-keymap))
43
44   (gnus-define-keys gnus-draft-mode-map
45     "Dt" gnus-draft-toggle-sending
46     "De" gnus-draft-edit-message
47     "Ds" gnus-draft-send-message
48     "DS" gnus-draft-send-all-messages))
49
50 (defun gnus-draft-make-menu-bar ()
51   (unless (boundp 'gnus-draft-menu)
52     (easy-menu-define
53      gnus-draft-menu gnus-draft-mode-map ""
54      '("Drafts"
55        ["Toggle whether to send" gnus-draft-toggle-sending t]))))
56
57 (defun gnus-draft-mode (&optional arg)
58   "Minor mode for providing a draft summary buffers.
59
60 \\{gnus-draft-mode-map}"
61   (interactive "P")
62   (when (eq major-mode 'gnus-summary-mode)
63     (if (not (set (make-local-variable 'gnus-draft-mode)
64                   (if (null arg) (not gnus-draft-mode)
65                     (> (prefix-numeric-value arg) 0))))
66         (remove-hook 'gnus-message-setup-hook 'gnus-draft-setup-message)
67       ;; Set up the menu.
68       (when (gnus-visual-p 'draft-menu 'menu)
69         (gnus-draft-make-menu-bar))
70       (gnus-add-minor-mode 'gnus-draft-mode " Draft" gnus-draft-mode-map)
71       (run-hooks 'gnus-draft-mode-hook))))
72
73 ;;; Commands
74
75 (defun gnus-draft-toggle-sending (article)
76   "Toggle whether to send an article or not."
77   (interactive (list (gnus-summary-article-number)))
78   (if (gnus-draft-article-sendable-p article)
79       (progn
80         (push article gnus-newsgroup-unsendable)
81         (gnus-summary-mark-article article gnus-unsendable-mark))
82     (setq gnus-newsgroup-unsendable
83           (delq article gnus-newsgroup-unsendable))
84     (gnus-summary-mark-article article gnus-unread-mark))
85   (gnus-summary-position-point))
86
87 (defun gnus-draft-edit-message ()
88   "Enter a mail/post buffer to edit and send the draft."
89   (interactive)
90   (gnus-set-global-variables)
91   (let ((article (gnus-summary-article-number)))
92     (gnus-draft-setup article)
93     (push
94      `((lambda ()
95          (when (buffer-name (get-buffer ,gnus-summary-buffer))
96            (save-excursion
97              (set-buffer (get-buffer ,gnus-summary-buffer))
98              (gnus-cache-possibly-remove-article ,article nil nil nil t)
99              (gnus-summary-mark-as-read ,article gnus-canceled-mark)))))
100      message-send-actions)))
101
102 (defun gnus-draft-send-message (&optional n)
103   "Send the current draft."
104   (interactive "P")
105   (gnus-set-global-variables)
106   (let ((articles (gnus-summary-work-articles n))
107         article)
108     (while (setq article (pop articles))
109       (gnus-summary-remove-process-mark article)
110       (unless (memq article gnus-newsgroup-unsendable)
111         (gnus-draft-send article)
112         (gnus-summary-mark-article article gnus-canceled-mark)))))
113
114 (defun gnus-draft-send (article)
115   "Send message ARTICLE."
116   (gnus-draft-setup article)
117   (message-send-and-exit))
118
119 (defun gnus-draft-send-all-messages ()
120   "Send all the sendable drafts."
121   (interactive)
122   (gnus-uu-mark-buffer)
123   (gnus-draft-send-message))
124
125 (defun gnus-group-send-drafts ()
126   "Send all sendable articles from the draft group."
127   (interactive)
128   (gnus-request-group "nndraft:draft")
129   (save-excursion
130     (let ((articles (nndraft-articles))
131           (unsendable (gnus-uncompress-range
132                        (cdr (assq 'unsend (gnus-info-marks
133                                            (gnus-get-info "nndraft:draft"))))))
134           article)
135       (while (setq article (pop articles))
136         (unless (memq article unsendable)
137           (gnus-draft-send article))))))
138
139 ;;; Utility functions
140
141 (defun gnus-draft-setup (article)
142   (gnus-setup-message 'forward
143     (message-mail)
144     (erase-buffer)
145     (if (not (gnus-request-restore-buffer
146               article (or gnus-newsgroup-name "nndraft:draft")))
147         (error "Couldn't restore the article")
148       ;; Insert the separator.
149       (goto-char (point-min))
150       (search-forward "\n\n")
151       (forward-char -1)
152       (insert mail-header-separator)
153       (forward-line 1)
154       (save-buffer 0))))
155
156 (defun gnus-draft-article-sendable-p (article)
157   "Say whether ARTICLE is sendable."
158   (not (memq article gnus-newsgroup-unsendable)))
159
160 (provide 'gnus-draft)
161
162 ;;; gnus-draft.el ends here
163