]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-draft.el
emacs: postpone a message
[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 ;;
6 ;; This file is part of Notmuch.
7 ;;
8 ;; Notmuch is free software: you can redistribute it and/or modify it
9 ;; under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
12 ;;
13 ;; Notmuch is distributed in the hope that it will be useful, but
14 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16 ;; General Public License for more details.
17 ;;
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with Notmuch.  If not, see <https://www.gnu.org/licenses/>.
20 ;;
21 ;; Authors: Mark Walters <markwalters1009@gmail.com>
22 ;;          David Bremner <david@tethera.net>
23
24 ;;; Code:
25
26 (require 'notmuch-maildir-fcc)
27 (require 'notmuch-tag)
28
29 (declare-function notmuch-show-get-message-id "notmuch-show" (&optional bare))
30
31 (defgroup notmuch-draft nil
32   "Saving and editing drafts in Notmuch."
33   :group 'notmuch)
34
35 (defcustom notmuch-draft-tags '("+draft")
36   "List of tags changes to apply to a draft message when it is saved in the database.
37
38 Tags starting with \"+\" (or not starting with either \"+\" or
39 \"-\") in the list will be added, and tags starting with \"-\"
40 will be removed from the message being stored.
41
42 For example, if you wanted to give the message a \"draft\" tag
43 but not the (normally added by default) \"inbox\" tag, you would
44 set:
45     (\"+draft\" \"-inbox\")"
46   :type '(repeat string)
47   :group 'notmuch-draft)
48
49 (defcustom notmuch-draft-folder "drafts"
50   "Folder to save draft messages in.
51
52 This should be specified relative to the root of the notmuch
53 database. It will be created if necessary."
54   :type 'string
55   :group 'notmuch-draft)
56
57 (defcustom notmuch-draft-quoted-tags '()
58   "Mml tags to quote.
59
60 This should be a list of mml tags to quote before saving. You do
61 not need to include \"secure\" as that is handled separately.
62
63 If you include \"part\" then attachments will not be saved with
64 the draft -- if not then they will be saved with the draft. The
65 former means the attachments may not still exist when you resume
66 the message, the latter means that the attachments as they were
67 when you postponed will be sent with the resumed message.
68
69 Note you may get strange results if you change this between
70 postponing and resuming a message."
71   :type '(repeat string)
72   :group 'notmuch-send)
73
74 (defvar notmuch-draft-id nil
75   "Message-id of the most recent saved draft of this message")
76 (make-variable-buffer-local 'notmuch-draft-id)
77
78 (defun notmuch-draft--mark-deleted ()
79   "Tag the last saved draft deleted.
80
81 Used when a new version is saved, or the message is sent."
82   (when notmuch-draft-id
83     (notmuch-tag notmuch-draft-id '("+deleted"))))
84
85 (defun notmuch-draft-quote-some-mml ()
86   "Quote the mml tags in `notmuch-draft-quoted-tags`."
87   (save-excursion
88     ;; First we deal with any secure tag separately.
89     (message-goto-body)
90     (when (looking-at "<#secure[^\n]*>\n")
91       (let ((secure-tag (match-string 0)))
92         (delete-region (match-beginning 0) (match-end 0))
93         (message-add-header (concat "X-Notmuch-Emacs-Secure: " secure-tag))))
94     ;; This is copied from mml-quote-region but only quotes the
95     ;; specified tags.
96     (when notmuch-draft-quoted-tags
97       (let ((re (concat "<#!*/?\\("
98                         (mapconcat 'regexp-quote notmuch-draft-quoted-tags "\\|")
99                         "\\)")))
100         (message-goto-body)
101         (while (re-search-forward re nil t)
102           ;; Insert ! after the #.
103           (goto-char (+ (match-beginning 0) 2))
104           (insert "!"))))))
105
106 (defun notmuch-draft--make-message-id ()
107   ;; message-make-message-id gives the id inside a "<" ">" pair,
108   ;; but notmuch doesn't want that form, so remove them.
109   (concat "draft-" (substring (message-make-message-id) 1 -1)))
110
111 (defun notmuch-draft-save ()
112   "Save the current draft message in the notmuch database.
113
114 This saves the current message in the database with tags
115 `notmuch-draft-tags` (in addition to any default tags
116 applied to newly inserted messages)."
117   (interactive)
118   (let ((id (notmuch-draft--make-message-id)))
119     (with-temporary-notmuch-message-buffer
120      ;; We insert a Date header and a Message-ID header, the former
121      ;; so that it is easier to search for the message, and the
122      ;; latter so we have a way of accessing the saved message (for
123      ;; example to delete it at a later time). We check that the
124      ;; user has these in `message-deletable-headers` (the default)
125      ;; as otherwise they are doing something strange and we
126      ;; shouldn't interfere. Note, since we are doing this in a new
127      ;; buffer we don't change the version in the compose buffer.
128      (cond
129       ((member 'Message-ID message-deletable-headers)
130        (message-remove-header "Message-ID")
131        (message-add-header (concat "Message-ID: <" id ">")))
132       (t
133        (message "You have customized emacs so Message-ID is not a deletable header, so not changing it")
134        (setq id nil)))
135      (cond
136       ((member 'Date message-deletable-headers)
137        (message-remove-header "Date")
138        (message-add-header (concat "Date: " (message-make-date))))
139       (t
140        (message "You have customized emacs so Date is not a deletable header, so not changing it")))
141      (message-add-header "X-Notmuch-Emacs-Draft: True")
142      (notmuch-draft-quote-some-mml)
143      (notmuch-maildir-setup-message-for-saving)
144      (notmuch-maildir-notmuch-insert-current-buffer
145       notmuch-draft-folder 't notmuch-draft-tags))
146     ;; We are now back in the original compose buffer. Note the
147     ;; function notmuch-call-notmuch-process (called by
148     ;; notmuch-maildir-notmuch-insert-current-buffer) signals an error
149     ;; on failure, so to get to this point it must have
150     ;; succeeded. Also, notmuch-draft-id is still the id of the
151     ;; previous draft, so it is safe to mark it deleted.
152     (notmuch-draft--mark-deleted)
153     (setq notmuch-draft-id (concat "id:" id))
154     (set-buffer-modified-p nil)))
155
156 (defun notmuch-draft-postpone ()
157   "Save the draft message in the notmuch database and exit buffer."
158   (interactive)
159   (notmuch-draft-save)
160   (kill-buffer))
161
162 (add-hook 'message-send-hook 'notmuch-draft--mark-deleted)
163
164
165 (provide 'notmuch-draft)
166
167 ;;; notmuch-draft.el ends here