cad3eef1a3a0f0e73c0051f82b52e88c703aa9e6
[elisp/gnus.git-] / lisp / gnus-draft.el
1 ;;; gnus-draft.el --- draft message support for Semi-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-hook)
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-activate-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 (defcustom gnus-draft-decoding-function
144   (function
145    (lambda ()
146      (mime-edit-decode-buffer nil)
147      (eword-decode-header)
148      ))
149   "Function called to decode the message from network representation."
150   :group 'gnus-agent
151   :type 'function)
152
153 ;;;!!!If this is byte-compiled, it fails miserably.
154 ;;;!!!I have no idea why.
155
156 (progn
157 (defun gnus-draft-setup (narticle group)
158   (gnus-setup-message 'forward
159     (let ((article narticle))
160       (message-mail)
161       (erase-buffer)
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