1 ;; notmuch-mua.el --- emacs style mail-user-agent
3 ;; Copyright © David Edmondson
5 ;; This file is part of Notmuch.
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.
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.
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/>.
20 ;; Authors: David Edmondson <dme@dme.org>
24 (require 'format-spec)
26 (require 'notmuch-lib)
27 (require 'notmuch-address)
29 (eval-when-compile (require 'cl))
33 (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
34 "Hook run before sending messages."
37 :group 'notmuch-hooks)
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))
50 (defcustom notmuch-mua-hidden-headers '("^User-Agent:")
51 "Headers that are added to the `message-mode' hidden headers
53 :type '(repeat string)
58 (defun notmuch-mua-user-agent-full ()
59 "Generate a `User-Agent:' string suitable for notmuch."
60 (concat (notmuch-mua-user-agent-notmuch)
62 (notmuch-mua-user-agent-emacs)))
64 (defun notmuch-mua-user-agent-notmuch ()
65 "Generate a `User-Agent:' string suitable for notmuch."
66 (concat "Notmuch/" (notmuch-version) " (http://notmuchmail.org)"))
68 (defun notmuch-mua-user-agent-emacs ()
69 "Generate a `User-Agent:' string suitable for notmuch."
70 (concat "Emacs/" emacs-version " (" system-configuration ")"))
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))
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)
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/*")
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")))
102 (defun notmuch-mua-reply (query-string &optional sender reply-all)
103 (let ((args '("reply" "--format=json"))
106 (when notmuch-show-process-crypto
107 (setq args (append args '("--decrypt"))))
110 (setq args (append args '("--reply-to=all")))
111 (setq args (append args '("--reply-to=sender"))))
112 (setq args (append args (list query-string)))
114 ;; Get the reply object as JSON, and parse it into an elisp object.
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)
121 (setq reply (json-read))))
123 ;; Extract the original message to simplify the following code.
124 (setq original (plist-get reply :original))
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)))
130 ;; If sender is non-nil, set the From: header to its value.
132 (plist-put reply-headers :From sender))
134 ;; Overlay the composition window on that being used to read
135 ;; the original message.
136 ((same-window-regexps '("\\*mail .*")))
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
145 'notmuch-mua-insert-references
149 (notmuch-mua-mail (plist-get reply-headers :To)
150 (plist-get reply-headers :Subject)
151 (notmuch-headers-plist-to-alist reply-headers))))
153 ;; Insert the message body - but put it in front of the signature
155 (goto-char (point-max))
156 (if (re-search-backward message-signature-separator nil t)
158 (goto-char (point-max)))
160 (let ((from (plist-get original-headers :From))
161 (date (plist-get original-headers :Date))
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")
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))))
173 (insert (notmuch-get-bodypart-content original part
175 notmuch-show-process-crypto)))
180 ;; Quote the original message according to the user's configured style.
181 (message-cite-original))))
183 (goto-char (point-max))
186 (set-buffer-modified-p nil))
188 (defun notmuch-mua-forward-message ()
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)
201 (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)
202 "Invoke the notmuch mail composition window.
204 OTHER-ARGS are passed through to `message-mail'."
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))))
212 (unless (assq 'From other-headers)
213 (push (cons 'From (concat
214 (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
216 (apply #'message-mail to subject other-headers other-args)
217 (message-sort-headers)
218 (message-hide-headers)
219 (set-buffer-modified-p nil)
223 (defcustom notmuch-identities nil
224 "Identities that can be used as the From: address when composing a new message.
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)
231 (defcustom notmuch-always-prompt-for-sender nil
232 "Always prompt for the From: address when composing or forwarding a message.
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."
237 :group 'notmuch-send)
239 (defvar notmuch-mua-sender-history nil)
241 (defun notmuch-mua-prompt-for-sender ()
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)
251 (mapcar (lambda (identity)
252 (string-equal name (car identity)))
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))
258 ;; Now prompt the user, either for an email address only or for a full identity.
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)))))
267 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
268 "Invoke the notmuch mail composition window.
270 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
271 the From: address first."
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)))
278 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
279 "Invoke the notmuch message forwarding window.
281 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
282 the From: address first."
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)))
292 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
293 "Invoke the notmuch reply window."
296 (when prompt-for-sender
297 (notmuch-mua-prompt-for-sender))))
298 (notmuch-mua-reply query-string sender reply-all)))
300 (defun notmuch-mua-send-and-exit (&optional arg)
302 (message-send-and-exit arg))
304 (defun notmuch-mua-kill-buffer ()
306 (message-kill-buffer))
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))
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)
319 ;; Add some more headers to the list that `message-mode' hides when
320 ;; composing a message.
321 (notmuch-mua-add-more-hidden-headers)
325 (provide 'notmuch-mua)