]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-draft.el
emacs: always use elisp quoting style in doc-strings
[notmuch] / emacs / notmuch-draft.el
1 ;;; notmuch-draft.el --- functions for postponing and editing drafts
2 ;;
3 ;; Copyright © Mark Walters
4 ;; Copyright © David Bremner
5 ;; Copyright © Leo Gaspard
6 ;;
7 ;; This file is part of Notmuch.
8 ;;
9 ;; Notmuch is free software: you can redistribute it and/or modify it
10 ;; under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13 ;;
14 ;; Notmuch is distributed in the hope that it will be useful, but
15 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 ;; General Public License for more details.
18 ;;
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with Notmuch.  If not, see <https://www.gnu.org/licenses/>.
21 ;;
22 ;; Authors: Mark Walters <markwalters1009@gmail.com>
23 ;;          David Bremner <david@tethera.net>
24 ;;          Leo Gaspard <leo@gaspard.io>
25
26 ;;; Code:
27
28 (require 'notmuch-maildir-fcc)
29 (require 'notmuch-tag)
30
31 (declare-function notmuch-show-get-message-id "notmuch-show" (&optional bare))
32 (declare-function notmuch-message-mode "notmuch-mua")
33
34 (defgroup notmuch-draft nil
35   "Saving and editing drafts in Notmuch."
36   :group 'notmuch)
37
38 (defcustom notmuch-draft-tags '("+draft")
39   "List of tags changes to apply to a draft message when it is saved in the database.
40
41 Tags starting with \"+\" (or not starting with either \"+\" or
42 \"-\") in the list will be added, and tags starting with \"-\"
43 will be removed from the message being stored.
44
45 For example, if you wanted to give the message a \"draft\" tag
46 but not the (normally added by default) \"inbox\" tag, you would
47 set:
48     (\"+draft\" \"-inbox\")"
49   :type '(repeat string)
50   :group 'notmuch-draft)
51
52 (defcustom notmuch-draft-folder "drafts"
53   "Folder to save draft messages in.
54
55 This should be specified relative to the root of the notmuch
56 database. It will be created if necessary."
57   :type 'string
58   :group 'notmuch-draft)
59
60 (defcustom notmuch-draft-quoted-tags '()
61   "Mml tags to quote.
62
63 This should be a list of mml tags to quote before saving. You do
64 not need to include \"secure\" as that is handled separately.
65
66 If you include \"part\" then attachments will not be saved with
67 the draft -- if not then they will be saved with the draft. The
68 former means the attachments may not still exist when you resume
69 the message, the latter means that the attachments as they were
70 when you postponed will be sent with the resumed message.
71
72 Note you may get strange results if you change this between
73 postponing and resuming a message."
74   :type '(repeat string)
75   :group 'notmuch-send)
76
77 (defcustom notmuch-draft-save-plaintext 'ask
78   "Should notmuch save/postpone in plaintext messages that seem
79 like they are intended to be sent encrypted
80 (i.e with an mml encryption tag in it)."
81   :type '(radio
82           (const :tag "Never" nil)
83           (const :tag "Ask every time" ask)
84           (const :tag "Always" t))
85   :group 'notmuch-draft
86   :group 'notmuch-crypto)
87
88 (defvar notmuch-draft-encryption-tag-regex
89   "<#\\(part encrypt\\|secure.*mode=.*encrypt>\\)"
90   "Regular expression matching mml tags indicating encryption of part or message.")
91
92 (defvar notmuch-draft-id nil
93   "Message-id of the most recent saved draft of this message.")
94 (make-variable-buffer-local 'notmuch-draft-id)
95
96 (defun notmuch-draft--mark-deleted ()
97   "Tag the last saved draft deleted.
98
99 Used when a new version is saved, or the message is sent."
100   (when notmuch-draft-id
101     (notmuch-tag notmuch-draft-id '("+deleted"))))
102
103 (defun notmuch-draft-quote-some-mml ()
104   "Quote the mml tags in `notmuch-draft-quoted-tags'."
105   (save-excursion
106     ;; First we deal with any secure tag separately.
107     (message-goto-body)
108     (when (looking-at "<#secure[^\n]*>\n")
109       (let ((secure-tag (match-string 0)))
110         (delete-region (match-beginning 0) (match-end 0))
111         (message-add-header (concat "X-Notmuch-Emacs-Secure: " secure-tag))))
112     ;; This is copied from mml-quote-region but only quotes the
113     ;; specified tags.
114     (when notmuch-draft-quoted-tags
115       (let ((re (concat "<#!*/?\\("
116                         (mapconcat 'regexp-quote notmuch-draft-quoted-tags "\\|")
117                         "\\)")))
118         (message-goto-body)
119         (while (re-search-forward re nil t)
120           ;; Insert ! after the #.
121           (goto-char (+ (match-beginning 0) 2))
122           (insert "!"))))))
123
124 (defun notmuch-draft-unquote-some-mml ()
125   "Unquote the mml tags in `notmuch-draft-quoted-tags'."
126   (save-excursion
127     (when notmuch-draft-quoted-tags
128       (let ((re (concat "<#!+/?\\("
129                         (mapconcat 'regexp-quote notmuch-draft-quoted-tags "\\|")
130                         "\\)")))
131         (message-goto-body)
132         (while (re-search-forward re nil t)
133           ;; Remove one ! from after the #.
134           (goto-char (+ (match-beginning 0) 2))
135           (delete-char 1))))
136     (let (secure-tag)
137       (save-restriction
138         (message-narrow-to-headers)
139         (setq secure-tag (message-fetch-field "X-Notmuch-Emacs-Secure" 't))
140         (message-remove-header "X-Notmuch-Emacs-Secure"))
141       (message-goto-body)
142       (when secure-tag
143         (insert secure-tag "\n")))))
144
145 (defun notmuch-draft--has-encryption-tag ()
146   "Returns t if there is an mml secure tag."
147   (save-excursion
148     (message-goto-body)
149     (re-search-forward notmuch-draft-encryption-tag-regex nil 't)))
150
151 (defun notmuch-draft--query-encryption ()
152   "Checks if we should save a message that should be encrypted.
153
154 `notmuch-draft-save-plaintext' controls the behaviour."
155   (cl-case notmuch-draft-save-plaintext
156     ((ask)
157      (unless (yes-or-no-p
158               "(Customize `notmuch-draft-save-plaintext' to avoid this warning)
159 This message contains mml tags that suggest it is intended to be encrypted.
160 Really save and index an unencrypted copy? ")
161        (error "Save aborted")))
162     ((nil)
163      (error "Refusing to save draft with encryption tags (see `%s')"
164             'notmuch-draft-save-plaintext))
165     ((t)
166      (ignore))))
167
168 (defun notmuch-draft--make-message-id ()
169   ;; message-make-message-id gives the id inside a "<" ">" pair,
170   ;; but notmuch doesn't want that form, so remove them.
171   (concat "draft-" (substring (message-make-message-id) 1 -1)))
172
173 (defun notmuch-draft-save ()
174   "Save the current draft message in the notmuch database.
175
176 This saves the current message in the database with tags
177 `notmuch-draft-tags' (in addition to any default tags
178 applied to newly inserted messages)."
179   (interactive)
180   (when (notmuch-draft--has-encryption-tag)
181     (notmuch-draft--query-encryption))
182   (let ((id (notmuch-draft--make-message-id)))
183     (with-temporary-notmuch-message-buffer
184      ;; We insert a Date header and a Message-ID header, the former
185      ;; so that it is easier to search for the message, and the
186      ;; latter so we have a way of accessing the saved message (for
187      ;; example to delete it at a later time). We check that the
188      ;; user has these in `message-deletable-headers' (the default)
189      ;; as otherwise they are doing something strange and we
190      ;; shouldn't interfere. Note, since we are doing this in a new
191      ;; buffer we don't change the version in the compose buffer.
192      (cond
193       ((member 'Message-ID message-deletable-headers)
194        (message-remove-header "Message-ID")
195        (message-add-header (concat "Message-ID: <" id ">")))
196       (t
197        (message "You have customized emacs so Message-ID is not a %s"
198                 "deletable header, so not changing it")
199        (setq id nil)))
200      (cond
201       ((member 'Date message-deletable-headers)
202        (message-remove-header "Date")
203        (message-add-header (concat "Date: " (message-make-date))))
204       (t
205        (message "You have customized emacs so Date is not a deletable %s"
206                 "header, so not changing it")))
207      (message-add-header "X-Notmuch-Emacs-Draft: True")
208      (notmuch-draft-quote-some-mml)
209      (notmuch-maildir-setup-message-for-saving)
210      (notmuch-maildir-notmuch-insert-current-buffer
211       notmuch-draft-folder 't notmuch-draft-tags))
212     ;; We are now back in the original compose buffer. Note the
213     ;; function notmuch-call-notmuch-process (called by
214     ;; notmuch-maildir-notmuch-insert-current-buffer) signals an error
215     ;; on failure, so to get to this point it must have
216     ;; succeeded. Also, notmuch-draft-id is still the id of the
217     ;; previous draft, so it is safe to mark it deleted.
218     (notmuch-draft--mark-deleted)
219     (setq notmuch-draft-id (concat "id:" id))
220     (set-buffer-modified-p nil)))
221
222 (defun notmuch-draft-postpone ()
223   "Save the draft message in the notmuch database and exit buffer."
224   (interactive)
225   (notmuch-draft-save)
226   (kill-buffer))
227
228 (defun notmuch-draft-resume (id)
229   "Resume editing of message with id ID."
230   (let* ((tags (process-lines notmuch-command "search" "--output=tags"
231                               "--exclude=false" id))
232          (draft (equal tags (notmuch-update-tags tags notmuch-draft-tags))))
233     (when (or draft
234               (yes-or-no-p "Message does not appear to be a draft: edit as new? "))
235       (pop-to-buffer-same-window
236        (get-buffer-create (concat "*notmuch-draft-" id "*")))
237       (setq buffer-read-only nil)
238       (erase-buffer)
239       (let ((coding-system-for-read 'no-conversion))
240         (call-process notmuch-command nil t nil "show" "--format=raw" id))
241       (mime-to-mml)
242       (goto-char (point-min))
243       (when (re-search-forward "^$" nil t)
244         (replace-match mail-header-separator t t))
245       ;; Remove the Date and Message-ID headers (unless the user has
246       ;; explicitly customized emacs to tell us not to) as they will
247       ;; be replaced when the message is sent.
248       (save-restriction
249         (message-narrow-to-headers)
250         (when (member 'Message-ID message-deletable-headers)
251           (message-remove-header "Message-ID"))
252         (when (member 'Date message-deletable-headers)
253           (message-remove-header "Date"))
254         (unless draft (notmuch-fcc-header-setup))
255         ;; The X-Notmuch-Emacs-Draft header is a more reliable
256         ;; indication of whether the message really is a draft.
257         (setq draft (> (message-remove-header "X-Notmuch-Emacs-Draft") 0)))
258       ;; If the message is not a draft we should not unquote any mml.
259       (when draft
260         (notmuch-draft-unquote-some-mml))
261       (notmuch-message-mode)
262       (message-goto-body)
263       (set-buffer-modified-p nil)
264       ;; If the resumed message was a draft then set the draft
265       ;; message-id so that we can delete the current saved draft if the
266       ;; message is resaved or sent.
267       (setq notmuch-draft-id (and draft id)))))
268
269
270 (add-hook 'message-send-hook 'notmuch-draft--mark-deleted)
271
272
273 (provide 'notmuch-draft)
274
275 ;;; notmuch-draft.el ends here