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