9d00c55af34967801e03ecae7630037b11de0442
[elisp/gnus.git-] / 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 ;;         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
59 (defun gnus-draft-mode (&optional arg)
60   "Minor mode for providing a draft summary buffers.
61
62 \\{gnus-draft-mode-map}"
63   (interactive "P")
64   (when (eq major-mode 'gnus-summary-mode)
65     (when (set (make-local-variable 'gnus-draft-mode)
66                   (if (null arg) (not gnus-draft-mode)
67                     (> (prefix-numeric-value arg) 0)))
68       ;; Set up the menu.
69       (when (gnus-visual-p 'draft-menu 'menu)
70         (gnus-draft-make-menu-bar))
71       (gnus-add-minor-mode 'gnus-draft-mode " Draft" gnus-draft-mode-map)
72       (run-hooks 'gnus-draft-mode-hook))))
73
74 ;;; Commands
75
76 (defun gnus-draft-toggle-sending (article)
77   "Toggle whether to send an article or not."
78   (interactive (list (gnus-summary-article-number)))
79   (if (gnus-draft-article-sendable-p article)
80       (progn
81         (push article gnus-newsgroup-unsendable)
82         (gnus-summary-mark-article article gnus-unsendable-mark))
83     (setq gnus-newsgroup-unsendable
84           (delq article gnus-newsgroup-unsendable))
85     (gnus-summary-mark-article article gnus-unread-mark))
86   (gnus-summary-position-point))
87
88 (defun gnus-draft-edit-message ()
89   "Enter a mail/post buffer to edit and send the draft."
90   (interactive)
91   (let ((article (gnus-summary-article-number)))
92     (gnus-summary-mark-as-read article gnus-canceled-mark)
93     (gnus-draft-setup article gnus-newsgroup-name)
94     (push
95      `((lambda ()
96          (when (buffer-name (get-buffer ,gnus-summary-buffer))
97            (save-excursion
98              (set-buffer (get-buffer ,gnus-summary-buffer))
99              (gnus-cache-possibly-remove-article ,article nil nil nil t)))))
100      message-send-actions)))
101
102 (defun gnus-draft-send-message (&optional n)
103   "Send the current draft."
104   (interactive "P")
105   (let ((articles (gnus-summary-work-articles n))
106         article)
107     (while (setq article (pop articles))
108       (gnus-summary-remove-process-mark article)
109       (unless (memq article gnus-newsgroup-unsendable)
110         (gnus-draft-send article gnus-newsgroup-name)
111         (gnus-summary-mark-article article gnus-canceled-mark)))))
112
113 (defun gnus-draft-send (article &optional group)
114   "Send message ARTICLE."
115   (gnus-draft-setup article (or group "nndraft:queue"))
116   (let ((message-syntax-checks 'dont-check-for-anything-just-trust-me))
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 queue group."
127   (interactive)
128   (gnus-activate-group "nndraft:queue")
129   (save-excursion
130     (let ((articles (nndraft-articles))
131           (unsendable (gnus-uncompress-range
132                        (cdr (assq 'unsend
133                                   (gnus-info-marks
134                                    (gnus-get-info "nndraft:queue"))))))
135           article)
136       (while (setq article (pop articles))
137         (unless (memq article unsendable)
138           (gnus-draft-send article))))))
139
140 ;;; Utility functions
141
142 (defcustom gnus-draft-decoding-function
143   (function
144    (lambda ()
145      (mime-edit-decode-buffer nil)
146      (eword-decode-header)
147      ))
148   "Function called to decode the message from network representation."
149   :group 'gnus-agent
150   :type 'function)
151
152 ;;;!!!If this is byte-compiled, it fails miserably.
153 ;;;!!!I have no idea why.
154
155 (progn
156 (defun gnus-draft-setup (narticle group)
157   (gnus-setup-message 'forward
158     (let ((article narticle))
159       (message-mail)
160       (erase-buffer)
161       (message "%s %s" group article)
162       (if (not (gnus-request-restore-buffer article group))
163           (error "Couldn't restore the article")
164         ;; Insert the separator.
165         (funcall gnus-draft-decoding-function)
166         (goto-char (point-min))
167         (search-forward "\n\n")
168         (forward-char -1)
169         (insert mail-header-separator)
170         (forward-line 1))))))
171
172 (defun gnus-draft-article-sendable-p (article)
173   "Say whether ARTICLE is sendable."
174   (not (memq article gnus-newsgroup-unsendable)))
175
176 (provide 'gnus-draft)
177
178 ;;; gnus-draft.el ends here