49312d3b987b5735de58ccc8047895597159e328
[gnus] / lisp / gnus-draft.el
1 ;;; gnus-draft.el --- draft message support for Gnus
2 ;; Copyright (C) 1997,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtgnus-run-hooks
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        ["Delete draft" gnus-summary-delete-article t]))))
61
62 (defun gnus-draft-mode (&optional arg)
63   "Minor mode for providing a draft summary buffers.
64
65 \\{gnus-draft-mode-map}"
66   (interactive "P")
67   (when (eq major-mode 'gnus-summary-mode)
68     (when (set (make-local-variable 'gnus-draft-mode)
69                   (if (null arg) (not gnus-draft-mode)
70                     (> (prefix-numeric-value arg) 0)))
71       ;; Set up the menu.
72       (when (gnus-visual-p 'draft-menu 'menu)
73         (gnus-draft-make-menu-bar))
74       (gnus-add-minor-mode 'gnus-draft-mode " Draft" gnus-draft-mode-map)
75       (gnus-run-hooks 'gnus-draft-mode-hook))))
76
77 ;;; Commands
78
79 (defun gnus-draft-toggle-sending (article)
80   "Toggle whether to send an article or not."
81   (interactive (list (gnus-summary-article-number)))
82   (if (gnus-draft-article-sendable-p article)
83       (progn
84         (push article gnus-newsgroup-unsendable)
85         (gnus-summary-mark-article article gnus-unsendable-mark))
86     (setq gnus-newsgroup-unsendable
87           (delq article gnus-newsgroup-unsendable))
88     (gnus-summary-mark-article article gnus-unread-mark))
89   (gnus-summary-position-point))
90
91 (defun gnus-draft-edit-message ()
92   "Enter a mail/post buffer to edit and send the draft."
93   (interactive)
94   (let ((article (gnus-summary-article-number)))
95     (gnus-summary-mark-as-read article gnus-canceled-mark)
96     (gnus-draft-setup article gnus-newsgroup-name)
97     (push
98      `((lambda ()
99          (when (buffer-name (get-buffer ,gnus-summary-buffer))
100            (save-excursion
101              (set-buffer (get-buffer ,gnus-summary-buffer))
102              (gnus-cache-possibly-remove-article ,article nil nil nil t)))))
103      message-send-actions)))
104
105 (defun gnus-draft-send-message (&optional n)
106   "Send the current draft."
107   (interactive "P")
108   (let ((articles (gnus-summary-work-articles n))
109         article)
110     (while (setq article (pop articles))
111       (gnus-summary-remove-process-mark article)
112       (unless (memq article gnus-newsgroup-unsendable)
113         (gnus-draft-send article gnus-newsgroup-name)
114         (gnus-summary-mark-article article gnus-canceled-mark)))))
115
116 (defun gnus-draft-send (article &optional group)
117   "Send message ARTICLE."
118   (gnus-draft-setup article (or group "nndraft:queue"))
119   (let ((message-syntax-checks 'dont-check-for-anything-just-trust-me)
120         message-send-hook)
121     (message-send-and-exit)))
122
123 (defun gnus-draft-send-all-messages ()
124   "Send all the sendable drafts."
125   (interactive)
126   (gnus-uu-mark-buffer)
127   (gnus-draft-send-message))
128
129 (defun gnus-group-send-drafts ()
130   "Send all sendable articles from the queue group."
131   (interactive)
132   (gnus-activate-group "nndraft:queue")
133   (save-excursion
134     (let ((articles (nndraft-articles))
135           (unsendable (gnus-uncompress-range
136                        (cdr (assq 'unsend
137                                   (gnus-info-marks
138                                    (gnus-get-info "nndraft:queue"))))))
139           article)
140       (while (setq article (pop articles))
141         (unless (memq article unsendable)
142           (gnus-draft-send article))))))
143
144 ;;; Utility functions
145
146 ;;;!!!If this is byte-compiled, it fails miserably.
147 ;;;!!!This is because `gnus-setup-message' uses uninterned symbols.
148 ;;;!!!This has been fixed in recent versions of Emacs and XEmacs,
149 ;;;!!!but for the time being, we'll just run this tiny function uncompiled.
150
151 (progn
152 (defun gnus-draft-setup (narticle group)
153   (gnus-setup-message 'forward
154     (let ((article narticle))
155       (message-mail)
156       (erase-buffer)
157       (if (not (gnus-request-restore-buffer article group))
158           (error "Couldn't restore the article")
159         ;; Insert the separator.
160         (goto-char (point-min))
161         (search-forward "\n\n")
162         (forward-char -1)
163         (insert mail-header-separator)
164         (forward-line 1)
165         (message-set-auto-save-file-name))))))
166
167 (defun gnus-draft-article-sendable-p (article)
168   "Say whether ARTICLE is sendable."
169   (not (memq article gnus-newsgroup-unsendable)))
170
171 (provide 'gnus-draft)
172
173 ;;; gnus-draft.el ends here