]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-mua.el
emacs: Fix the References header in reply
[notmuch] / emacs / notmuch-mua.el
1 ;; notmuch-mua.el --- emacs style mail-user-agent
2 ;;
3 ;; Copyright © David Edmondson
4 ;;
5 ;; This file is part of Notmuch.
6 ;;
7 ;; Notmuch is free software: you can redistribute it and/or modify it
8 ;; under the terms of the GNU General Public License as published by
9 ;; the Free Software Foundation, either version 3 of the License, or
10 ;; (at your option) any later version.
11 ;;
12 ;; Notmuch is distributed in the hope that it will be useful, but
13 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
14 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 ;; General Public License for more details.
16 ;;
17 ;; You should have received a copy of the GNU General Public License
18 ;; along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.
19 ;;
20 ;; Authors: David Edmondson <dme@dme.org>
21
22 (require 'json)
23 (require 'message)
24 (require 'format-spec)
25
26 (require 'notmuch-lib)
27 (require 'notmuch-address)
28
29 (eval-when-compile (require 'cl))
30
31 ;;
32
33 (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
34   "Hook run before sending messages."
35   :type 'hook
36   :group 'notmuch-send
37   :group 'notmuch-hooks)
38
39 (defcustom notmuch-mua-user-agent-function 'notmuch-mua-user-agent-full
40   "Function used to generate a `User-Agent:' string. If this is
41 `nil' then no `User-Agent:' will be generated."
42   :type '(choice (const :tag "No user agent string" nil)
43                  (const :tag "Full" notmuch-mua-user-agent-full)
44                  (const :tag "Notmuch" notmuch-mua-user-agent-notmuch)
45                  (const :tag "Emacs" notmuch-mua-user-agent-emacs)
46                  (function :tag "Custom user agent function"
47                            :value notmuch-mua-user-agent-full))
48   :group 'notmuch-send)
49
50 (defcustom notmuch-mua-hidden-headers '("^User-Agent:")
51   "Headers that are added to the `message-mode' hidden headers
52 list."
53   :type '(repeat string)
54   :group 'notmuch-send)
55
56 ;;
57
58 (defun notmuch-mua-user-agent-full ()
59   "Generate a `User-Agent:' string suitable for notmuch."
60   (concat (notmuch-mua-user-agent-notmuch)
61           " "
62           (notmuch-mua-user-agent-emacs)))
63
64 (defun notmuch-mua-user-agent-notmuch ()
65   "Generate a `User-Agent:' string suitable for notmuch."
66   (concat "Notmuch/" (notmuch-version) " (http://notmuchmail.org)"))
67
68 (defun notmuch-mua-user-agent-emacs ()
69   "Generate a `User-Agent:' string suitable for notmuch."
70   (concat "Emacs/" emacs-version " (" system-configuration ")"))
71
72 (defun notmuch-mua-add-more-hidden-headers ()
73   "Add some headers to the list that are hidden by default."
74   (mapc (lambda (header)
75           (when (not (member header message-hidden-headers))
76             (push header message-hidden-headers)))
77         notmuch-mua-hidden-headers))
78
79 (defun notmuch-mua-get-quotable-parts (parts)
80   (loop for part in parts
81         if (notmuch-match-content-type (plist-get part :content-type) "multipart/alternative")
82           collect (let* ((subparts (plist-get part :content))
83                         (types (mapcar (lambda (part) (plist-get part :content-type)) subparts))
84                         (chosen-type (car (notmuch-multipart/alternative-choose types))))
85                    (loop for part in (reverse subparts)
86                          if (notmuch-match-content-type (plist-get part :content-type) chosen-type)
87                          return part))
88         else if (notmuch-match-content-type (plist-get part :content-type) "multipart/*")
89           append (notmuch-mua-get-quotable-parts (plist-get part :content))
90         else if (notmuch-match-content-type (plist-get part :content-type) "text/*")
91           collect part))
92
93 ;; There is a bug in emacs 23's message.el that results in a newline
94 ;; not being inserted after the References header, so the next header
95 ;; is concatenated to the end of it. This function fixes the problem,
96 ;; while guarding against the possibility that some current or future
97 ;; version of emacs has the bug fixed.
98 (defun notmuch-mua-insert-references (original-func header references)
99   (funcall original-func header references)
100   (unless (bolp) (insert "\n")))
101
102 (defun notmuch-mua-reply (query-string &optional sender reply-all)
103   (let ((args '("reply" "--format=json"))
104         reply
105         original)
106     (when notmuch-show-process-crypto
107       (setq args (append args '("--decrypt"))))
108
109     (if reply-all
110         (setq args (append args '("--reply-to=all")))
111       (setq args (append args '("--reply-to=sender"))))
112     (setq args (append args (list query-string)))
113
114     ;; Get the reply object as JSON, and parse it into an elisp object.
115     (with-temp-buffer
116       (apply 'call-process (append (list notmuch-command nil (list t t) nil) args))
117       (goto-char (point-min))
118       (let ((json-object-type 'plist)
119             (json-array-type 'list)
120             (json-false 'nil))
121         (setq reply (json-read))))
122
123     ;; Extract the original message to simplify the following code.
124     (setq original (plist-get reply :original))
125
126     ;; Extract the headers of both the reply and the original message.
127     (let* ((original-headers (plist-get original :headers))
128            (reply-headers (plist-get reply :reply-headers)))
129
130       ;; If sender is non-nil, set the From: header to its value.
131       (when sender
132         (plist-put reply-headers :From sender))
133       (let
134           ;; Overlay the composition window on that being used to read
135           ;; the original message.
136           ((same-window-regexps '("\\*mail .*")))
137
138         ;; We modify message-header-format-alist to get around a bug in message.el.
139         ;; See the comment above on notmuch-mua-insert-references.
140         (let ((message-header-format-alist
141                (loop for pair in message-header-format-alist
142                      if (eq (car pair) 'References)
143                      collect (cons 'References
144                                    (apply-partially
145                                     'notmuch-mua-insert-references
146                                     (cdr pair)))
147                      else
148                      collect pair)))
149           (notmuch-mua-mail (plist-get reply-headers :To)
150                             (plist-get reply-headers :Subject)
151                             (notmuch-headers-plist-to-alist reply-headers))))
152
153       ;; Insert the message body - but put it in front of the signature
154       ;; if one is present
155       (goto-char (point-max))
156       (if (re-search-backward message-signature-separator nil t)
157           (forward-line -1)
158         (goto-char (point-max)))
159
160       (let ((from (plist-get original-headers :From))
161             (date (plist-get original-headers :Date))
162             (start (point)))
163
164         ;; message-cite-original constructs a citation line based on the From and Date
165         ;; headers of the original message, which are assumed to be in the buffer.
166         (insert "From: " from "\n")
167         (insert "Date: " date "\n\n")
168
169         ;; Get the parts of the original message that should be quoted; this includes
170         ;; all the text parts, except the non-preferred ones in a multipart/alternative.
171         (let ((quotable-parts (notmuch-mua-get-quotable-parts (plist-get original :body))))
172           (mapc (lambda (part)
173                   (insert (notmuch-get-bodypart-content original part
174                                                         (plist-get part :id)
175                                                         notmuch-show-process-crypto)))
176                 quotable-parts))
177
178         (set-mark (point))
179         (goto-char start)
180         ;; Quote the original message according to the user's configured style.
181         (message-cite-original))))
182
183   (goto-char (point-max))
184   (push-mark)
185   (message-goto-body)
186   (set-buffer-modified-p nil))
187
188 (defun notmuch-mua-forward-message ()
189   (message-forward)
190
191   (when notmuch-mua-user-agent-function
192     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
193       (when (not (string= "" user-agent))
194         (message-add-header (format "User-Agent: %s" user-agent)))))
195   (message-sort-headers)
196   (message-hide-headers)
197   (set-buffer-modified-p nil)
198
199   (message-goto-to))
200
201 (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)
202   "Invoke the notmuch mail composition window.
203
204 OTHER-ARGS are passed through to `message-mail'."
205   (interactive)
206
207   (when notmuch-mua-user-agent-function
208     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
209       (when (not (string= "" user-agent))
210         (push (cons 'User-Agent user-agent) other-headers))))
211
212   (unless (assq 'From other-headers)
213     (push (cons 'From (concat
214                        (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
215
216   (apply #'message-mail to subject other-headers other-args)
217   (message-sort-headers)
218   (message-hide-headers)
219   (set-buffer-modified-p nil)
220
221   (message-goto-to))
222
223 (defcustom notmuch-identities nil
224   "Identities that can be used as the From: address when composing a new message.
225
226 If this variable is left unset, then a list will be constructed from the
227 name and addresses configured in the notmuch configuration file."
228   :type '(repeat string)
229   :group 'notmuch-send)
230
231 (defcustom notmuch-always-prompt-for-sender nil
232   "Always prompt for the From: address when composing or forwarding a message.
233
234 This is not taken into account when replying to a message, because in that case
235 the From: header is already filled in by notmuch."
236   :type 'boolean
237   :group 'notmuch-send)
238
239 (defvar notmuch-mua-sender-history nil)
240
241 (defun notmuch-mua-prompt-for-sender ()
242   (interactive)
243   (let (name addresses one-name-only)
244     ;; If notmuch-identities is non-nil, check if there is a fixed user name.
245     (if notmuch-identities
246         (let ((components (mapcar 'mail-extract-address-components notmuch-identities)))
247           (setq name          (caar components)
248                 addresses     (mapcar 'cadr components)
249                 one-name-only (eval
250                                (cons 'and
251                                      (mapcar (lambda (identity)
252                                                (string-equal name (car identity)))
253                                              components)))))
254       ;; If notmuch-identities is nil, use values from the notmuch configuration file.
255       (setq name          (notmuch-user-name)
256             addresses     (cons (notmuch-user-primary-email) (notmuch-user-other-email))
257             one-name-only t))
258     ;; Now prompt the user, either for an email address only or for a full identity.
259     (if one-name-only
260         (let ((address
261                (ido-completing-read (concat "Sender address for " name ": ") addresses
262                                     nil nil nil 'notmuch-mua-sender-history (car addresses))))
263           (concat name " <" address ">"))
264       (ido-completing-read "Send mail From: " notmuch-identities
265                            nil nil nil 'notmuch-mua-sender-history (car notmuch-identities)))))
266
267 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
268   "Invoke the notmuch mail composition window.
269
270 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
271 the From: address first."
272   (interactive "P")
273   (let ((other-headers
274          (when (or prompt-for-sender notmuch-always-prompt-for-sender)
275            (list (cons 'From (notmuch-mua-prompt-for-sender))))))
276     (notmuch-mua-mail nil nil other-headers)))
277
278 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
279   "Invoke the notmuch message forwarding window.
280
281 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
282 the From: address first."
283   (interactive "P")
284   (if (or prompt-for-sender notmuch-always-prompt-for-sender)
285       (let* ((sender (notmuch-mua-prompt-for-sender))
286              (address-components (mail-extract-address-components sender))
287              (user-full-name (car address-components))
288              (user-mail-address (cadr address-components)))
289         (notmuch-mua-forward-message))
290     (notmuch-mua-forward-message)))
291
292 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
293   "Invoke the notmuch reply window."
294   (interactive "P")
295   (let ((sender
296          (when prompt-for-sender
297            (notmuch-mua-prompt-for-sender))))
298     (notmuch-mua-reply query-string sender reply-all)))
299
300 (defun notmuch-mua-send-and-exit (&optional arg)
301   (interactive "P")
302   (message-send-and-exit arg))
303
304 (defun notmuch-mua-kill-buffer ()
305   (interactive)
306   (message-kill-buffer))
307
308 (defun notmuch-mua-message-send-hook ()
309   "The default function used for `notmuch-mua-send-hook', this
310 simply runs the corresponding `message-mode' hook functions."
311   (run-hooks 'message-send-hook))
312
313 ;;
314
315 (define-mail-user-agent 'notmuch-user-agent
316   'notmuch-mua-mail 'notmuch-mua-send-and-exit
317   'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
318
319 ;; Add some more headers to the list that `message-mode' hides when
320 ;; composing a message.
321 (notmuch-mua-add-more-hidden-headers)
322
323 ;;
324
325 (provide 'notmuch-mua)