*** empty log message ***
[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
58 (defun gnus-draft-mode (&optional arg)
59   "Minor mode for providing a draft summary buffers.
60
61 \\{gnus-draft-mode-map}"
62   (interactive "P")
63   (when (eq major-mode 'gnus-summary-mode)
64     (when (set (make-local-variable 'gnus-draft-mode)
65                   (if (null arg) (not gnus-draft-mode)
66                     (> (prefix-numeric-value arg) 0)))
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 gnus-newsgroup-name)
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 "nndraft:queue")
117   (let ((message-syntax-checks 'dont-check-for-anything-just-trust-me))
118     (message-send-and-exit)))
119
120 (defun gnus-draft-send-all-messages ()
121   "Send all the sendable drafts."
122   (interactive)
123   (gnus-uu-mark-buffer)
124   (gnus-draft-send-message))
125
126 (defun gnus-group-send-drafts ()
127   "Send all sendable articles from the queue group."
128   (interactive)
129   (gnus-request-group "nndraft:queue")
130   (save-excursion
131     (let ((articles (nndraft-articles))
132           (unsendable (gnus-uncompress-range
133                        (cdr (assq 'unsend
134                                   (gnus-info-marks
135                                    (gnus-get-info "nndraft:queue"))))))
136           article)
137       (while (setq article (pop articles))
138         (unless (memq article unsendable)
139           (gnus-draft-send article))))))
140
141 ;;; Utility functions
142
143 (defun gnus-draft-setup (narticle group)
144   (gnus-setup-message 'forward
145     (message-mail)
146     (erase-buffer)
147     (if (not (gnus-request-restore-buffer narticle group))
148         (error "Couldn't restore the article")
149       ;; Insert the separator.
150       (goto-char (point-min))
151       (search-forward "\n\n")
152       (forward-char -1)
153       (insert mail-header-separator)
154       (forward-line 1))))
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