Sync up with qgnus-0.26.
[elisp/gnus.git-] / lisp / gnus-draft.el
1 ;;; gnus-draft.el --- draft message support for Semi-gnus
2 ;; Copyright (C) 1997,98 Free Software Foundation, Inc.
3
4 ;; Author: Lars Magne Ingebrigtsen <larsi@ifi.uio.no>
5 ;;         MORIOKA Tomohiko <morioka@jaist.ac.jp>
6 ;; Keywords: mail, news, MIME, offline
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'gnus)
30 (require 'gnus-sum)
31 (require 'message)
32 (require 'gnus-msg)
33 (require 'nndraft)
34 (eval-when-compile (require 'cl))
35
36 ;;; Draft minor mode
37
38 (defvar gnus-draft-mode nil
39   "Minor mode for providing a draft summary buffers.")
40
41 (defvar gnus-draft-mode-map nil)
42
43 (unless gnus-draft-mode-map
44   (setq gnus-draft-mode-map (make-sparse-keymap))
45
46   (gnus-define-keys gnus-draft-mode-map
47     "Dt" gnus-draft-toggle-sending
48     "De" gnus-draft-edit-message
49     "Ds" gnus-draft-send-message
50     "DS" gnus-draft-send-all-messages))
51
52 (defun gnus-draft-make-menu-bar ()
53   (unless (boundp 'gnus-draft-menu)
54     (easy-menu-define
55      gnus-draft-menu gnus-draft-mode-map ""
56      '("Drafts"
57        ["Toggle whether to send" gnus-draft-toggle-sending t]
58        ["Edit" gnus-draft-edit-message t]
59        ["Send selected message(s)" gnus-draft-send-message t]
60        ["Send all messages" gnus-draft-send-all-messages t]
61        ["Delete draft" gnus-summary-delete-article t]))))
62
63 (defun gnus-draft-mode (&optional arg)
64   "Minor mode for providing a draft summary buffers.
65
66 \\{gnus-draft-mode-map}"
67   (interactive "P")
68   (when (eq major-mode 'gnus-summary-mode)
69     (when (set (make-local-variable 'gnus-draft-mode)
70                   (if (null arg) (not gnus-draft-mode)
71                     (> (prefix-numeric-value arg) 0)))
72       ;; Set up the menu.
73       (when (gnus-visual-p 'draft-menu 'menu)
74         (gnus-draft-make-menu-bar))
75       (gnus-add-minor-mode 'gnus-draft-mode " Draft" gnus-draft-mode-map)
76       (gnus-run-hooks 'gnus-draft-mode-hook))))
77
78 ;;; Commands
79
80 (defun gnus-draft-toggle-sending (article)
81   "Toggle whether to send an article or not."
82   (interactive (list (gnus-summary-article-number)))
83   (if (gnus-draft-article-sendable-p article)
84       (progn
85         (push article gnus-newsgroup-unsendable)
86         (gnus-summary-mark-article article gnus-unsendable-mark))
87     (setq gnus-newsgroup-unsendable
88           (delq article gnus-newsgroup-unsendable))
89     (gnus-summary-mark-article article gnus-unread-mark))
90   (gnus-summary-position-point))
91
92 (defun gnus-draft-edit-message ()
93   "Enter a mail/post buffer to edit and send the draft."
94   (interactive)
95   (let ((article (gnus-summary-article-number)))
96     (gnus-summary-mark-as-read article gnus-canceled-mark)
97     (gnus-draft-setup article gnus-newsgroup-name)
98     (push
99      `((lambda ()
100          (when (buffer-name (get-buffer ,gnus-summary-buffer))
101            (save-excursion
102              (set-buffer (get-buffer ,gnus-summary-buffer))
103              (gnus-cache-possibly-remove-article ,article nil nil nil t)))))
104      message-send-actions)))
105
106 (defun gnus-draft-send-message (&optional n)
107   "Send the current draft."
108   (interactive "P")
109   (let ((articles (gnus-summary-work-articles n))
110         article)
111     (while (setq article (pop articles))
112       (gnus-summary-remove-process-mark article)
113       (unless (memq article gnus-newsgroup-unsendable)
114         (gnus-draft-send article gnus-newsgroup-name)
115         (gnus-summary-mark-article article gnus-canceled-mark)))))
116
117 (defun gnus-draft-send (article &optional group)
118   "Send message ARTICLE."
119   (gnus-draft-setup article (or group "nndraft:queue"))
120   (let ((message-syntax-checks 'dont-check-for-anything-just-trust-me)
121         message-send-hook)
122     (message-send-and-exit)))
123
124 (defun gnus-draft-send-all-messages ()
125   "Send all the sendable drafts."
126   (interactive)
127   (gnus-uu-mark-buffer)
128   (gnus-draft-send-message))
129
130 (defun gnus-group-send-drafts ()
131   "Send all sendable articles from the queue group."
132   (interactive)
133   (gnus-activate-group "nndraft:queue")
134   (save-excursion
135     (let ((articles (nndraft-articles))
136           (unsendable (gnus-uncompress-range
137                        (cdr (assq 'unsend
138                                   (gnus-info-marks
139                                    (gnus-get-info "nndraft:queue"))))))
140           article)
141       (while (setq article (pop articles))
142         (unless (memq article unsendable)
143           (gnus-draft-send article))))))
144
145 ;;; Utility functions
146
147 (defcustom gnus-draft-decoding-function
148   (function
149    (lambda ()
150      (mime-edit-decode-buffer nil)
151      (eword-decode-header)
152      ))
153   "*Function called to decode the message from network representation."
154   :group 'gnus-agent
155   :type 'function)
156
157 ;;;!!!If this is byte-compiled, it fails miserably.
158 ;;;!!!I have no idea why.
159
160 (progn
161 (defun gnus-draft-setup (narticle group)
162   (gnus-setup-message 'forward
163     (let ((article narticle))
164       (message-mail)
165       (erase-buffer)
166       (if (not (gnus-request-restore-buffer article group))
167           (error "Couldn't restore the article")
168         ;; Insert the separator.
169         (funcall gnus-draft-decoding-function)
170         (goto-char (point-min))
171         (search-forward "\n\n")
172         (forward-char -1)
173         (insert mail-header-separator)
174         (forward-line 1)
175         (message-set-auto-save-file-name))))))
176
177 (defun gnus-draft-article-sendable-p (article)
178   "Say whether ARTICLE is sendable."
179   (not (memq article gnus-newsgroup-unsendable)))
180
181 (provide 'gnus-draft)
182
183 ;;; gnus-draft.el ends here