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