Obsolete sc-load-hook in favor of eval-after-load
[emacs.git] / lisp / gnus / gnus-draft.el
blobf5299bea806393ba3b392e846997091bfc5d46a0
1 ;;; gnus-draft.el --- draft message support for Gnus
3 ;; Copyright (C) 1997-2016 Free Software Foundation, Inc.
5 ;; Author: Lars Magne Ingebrigtsen <larsi@gnus.org>
6 ;; Keywords: news
8 ;; This file is part of GNU Emacs.
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 3 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (require 'gnus)
28 (require 'gnus-sum)
29 (require 'message)
30 (require 'gnus-msg)
31 (require 'nndraft)
32 (require 'gnus-agent)
33 (eval-when-compile (require 'cl))
35 ;;; Draft minor mode
37 (defvar gnus-draft-mode-map
38 (let ((map (make-sparse-keymap)))
39 (gnus-define-keys map
40 "Dt" gnus-draft-toggle-sending
41 "e" gnus-draft-edit-message ;; Use `B w' for `gnus-summary-edit-article'
42 "De" gnus-draft-edit-message
43 "Ds" gnus-draft-send-message
44 "DS" gnus-draft-send-all-messages)
45 map))
47 (defun gnus-draft-make-menu-bar ()
48 (unless (boundp 'gnus-draft-menu)
49 (easy-menu-define
50 gnus-draft-menu gnus-draft-mode-map ""
51 '("Drafts"
52 ["Toggle whether to send" gnus-draft-toggle-sending t]
53 ["Edit" gnus-draft-edit-message t]
54 ["Send selected message(s)" gnus-draft-send-message t]
55 ["Send all messages" gnus-draft-send-all-messages t]
56 ["Delete draft" gnus-summary-delete-article t]))))
58 (define-minor-mode gnus-draft-mode
59 "Minor mode for providing a draft summary buffers.
61 \\{gnus-draft-mode-map}"
62 :lighter " Draft" :keymap gnus-draft-mode-map
63 (cond
64 ((not (derived-mode-p 'gnus-summary-mode)) (setq gnus-draft-mode nil))
65 (gnus-draft-mode
66 ;; Set up the menu.
67 (when (gnus-visual-p 'draft-menu 'menu)
68 (gnus-draft-make-menu-bar))
69 (add-hook 'gnus-summary-prepare-exit-hook 'gnus-draft-clear-marks t t))))
71 ;;; Commands
73 (defun gnus-draft-toggle-sending (article)
74 "Toggle whether to send an article or not."
75 (interactive (list (gnus-summary-article-number)))
76 (if (gnus-draft-article-sendable-p article)
77 (progn
78 (push article gnus-newsgroup-unsendable)
79 (gnus-summary-mark-article article gnus-unsendable-mark))
80 (setq gnus-newsgroup-unsendable
81 (delq article gnus-newsgroup-unsendable))
82 (gnus-summary-mark-article article gnus-unread-mark))
83 (gnus-summary-position-point))
85 (defun gnus-draft-edit-message ()
86 "Enter a mail/post buffer to edit and send the draft."
87 (interactive)
88 (let ((article (gnus-summary-article-number))
89 (group gnus-newsgroup-name))
90 (gnus-draft-check-draft-articles (list article))
91 (gnus-summary-mark-as-read article gnus-canceled-mark)
92 (gnus-draft-setup article group t)
93 (set-buffer-modified-p t)
94 (save-excursion
95 (save-restriction
96 (message-narrow-to-headers)
97 (message-remove-header "date")))
98 (let ((message-draft-headers
99 (delq 'Date (copy-sequence message-draft-headers))))
100 (save-buffer))
101 (let ((gnus-verbose-backends nil))
102 (gnus-request-expire-articles (list article) group t))
103 (push
104 `((lambda ()
105 (when (gnus-buffer-exists-p ,gnus-summary-buffer)
106 (save-excursion
107 (set-buffer ,gnus-summary-buffer)
108 (gnus-cache-possibly-remove-article ,article nil nil nil t)))))
109 message-send-actions)))
111 (defun gnus-draft-send-message (&optional n)
112 "Send the current draft(s).
113 Obeys the standard process/prefix convention."
114 (interactive "P")
115 (let* ((articles (gnus-summary-work-articles n))
116 (total (length articles))
117 article)
118 (gnus-draft-check-draft-articles articles)
119 (while (setq article (pop articles))
120 (gnus-summary-remove-process-mark article)
121 (unless (memq article gnus-newsgroup-unsendable)
122 (let ((message-sending-message
123 (format "Sending message %d of %d..."
124 (- total (length articles)) total)))
125 (gnus-draft-send article gnus-newsgroup-name t))
126 (gnus-summary-mark-article article gnus-canceled-mark)))))
128 (defun gnus-draft-send (article &optional group interactive)
129 "Send message ARTICLE."
130 (let* ((is-queue (or (not group)
131 (equal group "nndraft:queue")))
132 (message-syntax-checks (if interactive message-syntax-checks
133 'dont-check-for-anything-just-trust-me))
134 (message-hidden-headers nil)
135 (message-inhibit-body-encoding (or is-queue
136 message-inhibit-body-encoding))
137 (message-send-hook (and (not is-queue)
138 message-send-hook))
139 (message-setup-hook (and (not is-queue)
140 message-setup-hook))
141 (gnus-message-setup-hook (and (not is-queue)
142 gnus-message-setup-hook))
143 (message-signature (and (not is-queue)
144 message-signature))
145 (gnus-agent-queue-mail (and (not is-queue)
146 gnus-agent-queue-mail))
147 (rfc2047-encode-encoded-words nil)
148 type method move-to)
149 (gnus-draft-setup article (or group "nndraft:queue") nil 'dont-pop)
150 ;; We read the meta-information that says how and where
151 ;; this message is to be sent.
152 (save-restriction
153 (message-narrow-to-headers)
154 (when (re-search-forward
155 (concat "^" (regexp-quote gnus-agent-target-move-group-header)
156 ":") nil t)
157 (skip-syntax-forward "-")
158 (setq move-to (buffer-substring (point) (point-at-eol)))
159 (message-remove-header gnus-agent-target-move-group-header))
160 (goto-char (point-min))
161 (when (re-search-forward
162 (concat "^" (regexp-quote gnus-agent-meta-information-header) ":")
163 nil t)
164 (setq type (ignore-errors (read (current-buffer)))
165 method (ignore-errors (read (current-buffer))))
166 (message-remove-header gnus-agent-meta-information-header)))
167 ;; Let Agent restore any GCC lines and have message.el perform them.
168 (gnus-agent-restore-gcc)
169 ;; Then we send it. If we have no meta-information, we just send
170 ;; it and let Message figure out how.
171 (when (and (or (null method)
172 (gnus-server-opened method)
173 (gnus-open-server method))
174 (if type
175 (let ((message-this-is-news (eq type 'news))
176 (message-this-is-mail (eq type 'mail))
177 (gnus-post-method method)
178 (message-post-method method))
179 (if move-to
180 (gnus-inews-do-gcc move-to)
181 (message-send-and-exit)))
182 (if move-to
183 (gnus-inews-do-gcc move-to)
184 (message-send-and-exit))))
185 (let ((gnus-verbose-backends nil))
186 (gnus-request-expire-articles
187 (list article) (or group "nndraft:queue") t)))))
189 (defun gnus-draft-send-all-messages ()
190 "Send all the sendable drafts."
191 (interactive)
192 (when (or
193 gnus-expert-user
194 (gnus-y-or-n-p
195 "Send all drafts? "))
196 (gnus-uu-mark-buffer)
197 (gnus-draft-send-message)))
199 (defun gnus-group-send-queue ()
200 "Send all sendable articles from the queue group."
201 (interactive)
202 (when (or gnus-plugged
203 (not gnus-agent-prompt-send-queue)
204 (gnus-y-or-n-p "Gnus is unplugged; really send queue? "))
205 (gnus-activate-group "nndraft:queue")
206 (save-excursion
207 (let* ((articles (nndraft-articles))
208 (unsendable (gnus-uncompress-range
209 (cdr (assq 'unsend
210 (gnus-info-marks
211 (gnus-get-info "nndraft:queue"))))))
212 (gnus-posting-styles nil)
213 message-send-mail-partially-limit
214 (total (length articles))
215 article)
216 (while (setq article (pop articles))
217 (unless (memq article unsendable)
218 (let ((message-sending-message
219 (format "Sending message %d of %d..."
220 (- total (length articles)) total)))
221 (gnus-draft-send article))))))
222 (gnus-group-refresh-group "nndraft:queue")))
224 ;;;###autoload
225 (defun gnus-draft-reminder ()
226 "Reminder user if there are unsent drafts."
227 (interactive)
228 (if (gnus-alive-p)
229 (let (active)
230 (catch 'continue
231 (dolist (group '("nndraft:drafts" "nndraft:queue"))
232 (setq active (gnus-activate-group group))
233 (if (and active (>= (cdr active) (car active)))
234 (if (y-or-n-p "There are unsent drafts. Confirm to exit? ")
235 (throw 'continue t)
236 (error "Stop!"))))))))
238 (defcustom gnus-draft-setup-hook nil
239 "Hook run after setting up a draft buffer."
240 :group 'gnus-message
241 :version "23.1" ;; No Gnus
242 :type 'hook)
245 (defun gnus-draft-setup (narticle group &optional restore dont-pop)
246 "Setup a mail draft buffer.
247 If DONT-POP is nil, display the buffer after setting it up."
248 (let (ga)
249 (gnus-setup-message 'forward
250 (let ((article narticle))
251 (message-mail nil nil nil nil
252 (if dont-pop
253 (lambda (buf) (set-buffer (get-buffer-create buf)))))
254 (let ((inhibit-read-only t))
255 (erase-buffer))
256 (if (not (gnus-request-restore-buffer article group))
257 (error "Couldn't restore the article")
258 (when (and restore
259 (equal group "nndraft:queue"))
260 (mime-to-mml))
261 ;; Insert the separator.
262 (goto-char (point-min))
263 (search-forward "\n\n")
264 (forward-char -1)
265 (save-restriction
266 (narrow-to-region (point-min) (point))
267 (setq ga
268 (message-fetch-field gnus-draft-meta-information-header)))
269 (insert mail-header-separator)
270 (forward-line 1)
271 (message-set-auto-save-file-name))))
272 (gnus-backlog-remove-article group narticle)
273 (when (and ga
274 (ignore-errors (setq ga (car (read-from-string ga)))))
275 (setq gnus-newsgroup-name
276 (if (equal (car ga) "") nil (car ga)))
277 (gnus-configure-posting-styles)
278 (setq gnus-message-group-art (cons gnus-newsgroup-name (cadr ga)))
279 (setq message-post-method
280 `(lambda (arg)
281 (gnus-post-method arg ,(car ga))))
282 (unless (equal (cadr ga) "")
283 (dolist (article (cdr ga))
284 (message-add-action
285 `(progn
286 (gnus-add-mark ,(car ga) 'replied ,article)
287 (gnus-request-set-mark ,(car ga) (list (list (list ,article)
288 'add '(reply)))))
289 'send))))
290 (run-hooks 'gnus-draft-setup-hook)))
292 (defun gnus-draft-article-sendable-p (article)
293 "Say whether ARTICLE is sendable."
294 (not (memq article gnus-newsgroup-unsendable)))
296 (defun gnus-draft-check-draft-articles (articles)
297 "Check whether the draft articles ARTICLES are under edit."
298 (when (equal gnus-newsgroup-name "nndraft:drafts")
299 (let ((buffers (buffer-list))
300 file buffs buff)
301 (save-current-buffer
302 (while (and articles
303 (not buff))
304 (setq file (nndraft-article-filename (pop articles))
305 buffs buffers)
306 (while buffs
307 (set-buffer (setq buff (pop buffs)))
308 (if (and buffer-file-name
309 (equal (file-remote-p file)
310 (file-remote-p buffer-file-name))
311 (string-equal (file-truename buffer-file-name)
312 (file-truename file))
313 (buffer-modified-p))
314 (setq buffs nil)
315 (setq buff nil)))))
316 (when buff
317 (let* ((window (get-buffer-window buff t))
318 (frame (and window (window-frame window))))
319 (if frame
320 (select-frame-set-input-focus frame)
321 (pop-to-buffer buff t)))
322 (error "The draft %s is under edit" file)))))
324 (defun gnus-draft-clear-marks ()
325 (setq gnus-newsgroup-reads nil
326 gnus-newsgroup-marked nil
327 gnus-newsgroup-unreads (nndraft-articles)))
329 (provide 'gnus-draft)
331 ;;; gnus-draft.el ends here