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>
25 (require 'format-spec)
27 (require 'notmuch-lib)
28 (require 'notmuch-address)
30 (eval-when-compile (require 'cl))
34 (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
35 "Hook run before sending messages."
38 :group 'notmuch-hooks)
40 (defcustom notmuch-mua-user-agent-function 'notmuch-mua-user-agent-full
41 "Function used to generate a `User-Agent:' string. If this is
42 `nil' then no `User-Agent:' will be generated."
43 :type '(choice (const :tag "No user agent string" nil)
44 (const :tag "Full" notmuch-mua-user-agent-full)
45 (const :tag "Notmuch" notmuch-mua-user-agent-notmuch)
46 (const :tag "Emacs" notmuch-mua-user-agent-emacs)
47 (function :tag "Custom user agent function"
48 :value notmuch-mua-user-agent-full))
51 (defcustom notmuch-mua-hidden-headers '("^User-Agent:")
52 "Headers that are added to the `message-mode' hidden headers
54 :type '(repeat string)
59 (defun notmuch-mua-user-agent-full ()
60 "Generate a `User-Agent:' string suitable for notmuch."
61 (concat (notmuch-mua-user-agent-notmuch)
63 (notmuch-mua-user-agent-emacs)))
65 (defun notmuch-mua-user-agent-notmuch ()
66 "Generate a `User-Agent:' string suitable for notmuch."
67 (concat "Notmuch/" (notmuch-version) " (http://notmuchmail.org)"))
69 (defun notmuch-mua-user-agent-emacs ()
70 "Generate a `User-Agent:' string suitable for notmuch."
71 (concat "Emacs/" emacs-version " (" system-configuration ")"))
73 (defun notmuch-mua-add-more-hidden-headers ()
74 "Add some headers to the list that are hidden by default."
75 (mapc (lambda (header)
76 (when (not (member header message-hidden-headers))
77 (push header message-hidden-headers)))
78 notmuch-mua-hidden-headers))
80 (defun notmuch-mua-get-quotable-parts (parts)
81 (loop for part in parts
82 if (notmuch-match-content-type (plist-get part :content-type) "multipart/alternative")
83 collect (let* ((subparts (plist-get part :content))
84 (types (mapcar (lambda (part) (plist-get part :content-type)) subparts))
85 (chosen-type (car (notmuch-multipart/alternative-choose types))))
86 (loop for part in (reverse subparts)
87 if (notmuch-match-content-type (plist-get part :content-type) chosen-type)
89 else if (notmuch-match-content-type (plist-get part :content-type) "multipart/*")
90 append (notmuch-mua-get-quotable-parts (plist-get part :content))
91 else if (notmuch-match-content-type (plist-get part :content-type) "text/*")
94 (defun notmuch-mua-insert-quotable-part (message part)
96 (narrow-to-region (point) (point))
97 (notmuch-mm-display-part-inline message part (plist-get part :id)
98 (plist-get part :content-type)
99 notmuch-show-process-crypto)
100 (goto-char (point-max))))
102 ;; There is a bug in emacs 23's message.el that results in a newline
103 ;; not being inserted after the References header, so the next header
104 ;; is concatenated to the end of it. This function fixes the problem,
105 ;; while guarding against the possibility that some current or future
106 ;; version of emacs has the bug fixed.
107 (defun notmuch-mua-insert-references (original-func header references)
108 (funcall original-func header references)
109 (unless (bolp) (insert "\n")))
111 (defun notmuch-mua-reply (query-string &optional sender reply-all)
112 (let ((args '("reply" "--format=json"))
115 (when notmuch-show-process-crypto
116 (setq args (append args '("--decrypt"))))
119 (setq args (append args '("--reply-to=all")))
120 (setq args (append args '("--reply-to=sender"))))
121 (setq args (append args (list query-string)))
123 ;; Get the reply object as JSON, and parse it into an elisp object.
125 (apply 'call-process (append (list notmuch-command nil (list t t) nil) args))
126 (goto-char (point-min))
127 (let ((json-object-type 'plist)
128 (json-array-type 'list)
130 (setq reply (json-read))))
132 ;; Extract the original message to simplify the following code.
133 (setq original (plist-get reply :original))
135 ;; Extract the headers of both the reply and the original message.
136 (let* ((original-headers (plist-get original :headers))
137 (reply-headers (plist-get reply :reply-headers)))
139 ;; If sender is non-nil, set the From: header to its value.
141 (plist-put reply-headers :From sender))
143 ;; Overlay the composition window on that being used to read
144 ;; the original message.
145 ((same-window-regexps '("\\*mail .*")))
147 ;; We modify message-header-format-alist to get around a bug in message.el.
148 ;; See the comment above on notmuch-mua-insert-references.
149 (let ((message-header-format-alist
150 (loop for pair in message-header-format-alist
151 if (eq (car pair) 'References)
152 collect (cons 'References
154 'notmuch-mua-insert-references
158 (notmuch-mua-mail (plist-get reply-headers :To)
159 (plist-get reply-headers :Subject)
160 (notmuch-headers-plist-to-alist reply-headers))))
162 ;; Insert the message body - but put it in front of the signature
164 (goto-char (point-max))
165 (if (re-search-backward message-signature-separator nil t)
167 (goto-char (point-max)))
169 (let ((from (plist-get original-headers :From))
170 (date (plist-get original-headers :Date))
173 ;; message-cite-original constructs a citation line based on the From and Date
174 ;; headers of the original message, which are assumed to be in the buffer.
175 (insert "From: " from "\n")
176 (insert "Date: " date "\n\n")
178 ;; Get the parts of the original message that should be quoted; this includes
179 ;; all the text parts, except the non-preferred ones in a multipart/alternative.
180 (let ((quotable-parts (notmuch-mua-get-quotable-parts (plist-get original :body))))
181 (mapc (apply-partially 'notmuch-mua-insert-quotable-part original) quotable-parts))
185 ;; Quote the original message according to the user's configured style.
186 (message-cite-original))))
188 (goto-char (point-max))
191 (set-buffer-modified-p nil))
193 (defun notmuch-mua-forward-message ()
196 (when notmuch-mua-user-agent-function
197 (let ((user-agent (funcall notmuch-mua-user-agent-function)))
198 (when (not (string= "" user-agent))
199 (message-add-header (format "User-Agent: %s" user-agent)))))
200 (message-sort-headers)
201 (message-hide-headers)
202 (set-buffer-modified-p nil)
206 (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)
207 "Invoke the notmuch mail composition window.
209 OTHER-ARGS are passed through to `message-mail'."
212 (when notmuch-mua-user-agent-function
213 (let ((user-agent (funcall notmuch-mua-user-agent-function)))
214 (when (not (string= "" user-agent))
215 (push (cons 'User-Agent user-agent) other-headers))))
217 (unless (assq 'From other-headers)
218 (push (cons 'From (concat
219 (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
221 (apply #'message-mail to subject other-headers other-args)
222 (message-sort-headers)
223 (message-hide-headers)
224 (set-buffer-modified-p nil)
228 (defcustom notmuch-identities nil
229 "Identities that can be used as the From: address when composing a new message.
231 If this variable is left unset, then a list will be constructed from the
232 name and addresses configured in the notmuch configuration file."
233 :type '(repeat string)
234 :group 'notmuch-send)
236 (defcustom notmuch-always-prompt-for-sender nil
237 "Always prompt for the From: address when composing or forwarding a message.
239 This is not taken into account when replying to a message, because in that case
240 the From: header is already filled in by notmuch."
242 :group 'notmuch-send)
244 (defvar notmuch-mua-sender-history nil)
246 (defun notmuch-mua-prompt-for-sender ()
248 (let (name addresses one-name-only)
249 ;; If notmuch-identities is non-nil, check if there is a fixed user name.
250 (if notmuch-identities
251 (let ((components (mapcar 'mail-extract-address-components notmuch-identities)))
252 (setq name (caar components)
253 addresses (mapcar 'cadr components)
256 (mapcar (lambda (identity)
257 (string-equal name (car identity)))
259 ;; If notmuch-identities is nil, use values from the notmuch configuration file.
260 (setq name (notmuch-user-name)
261 addresses (cons (notmuch-user-primary-email) (notmuch-user-other-email))
263 ;; Now prompt the user, either for an email address only or for a full identity.
266 (ido-completing-read (concat "Sender address for " name ": ") addresses
267 nil nil nil 'notmuch-mua-sender-history (car addresses))))
268 (concat name " <" address ">"))
269 (ido-completing-read "Send mail From: " notmuch-identities
270 nil nil nil 'notmuch-mua-sender-history (car notmuch-identities)))))
272 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
273 "Invoke the notmuch mail composition window.
275 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
276 the From: address first."
279 (when (or prompt-for-sender notmuch-always-prompt-for-sender)
280 (list (cons 'From (notmuch-mua-prompt-for-sender))))))
281 (notmuch-mua-mail nil nil other-headers)))
283 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
284 "Invoke the notmuch message forwarding window.
286 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
287 the From: address first."
289 (if (or prompt-for-sender notmuch-always-prompt-for-sender)
290 (let* ((sender (notmuch-mua-prompt-for-sender))
291 (address-components (mail-extract-address-components sender))
292 (user-full-name (car address-components))
293 (user-mail-address (cadr address-components)))
294 (notmuch-mua-forward-message))
295 (notmuch-mua-forward-message)))
297 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
298 "Invoke the notmuch reply window."
301 (when prompt-for-sender
302 (notmuch-mua-prompt-for-sender))))
303 (notmuch-mua-reply query-string sender reply-all)))
305 (defun notmuch-mua-send-and-exit (&optional arg)
307 (message-send-and-exit arg))
309 (defun notmuch-mua-kill-buffer ()
311 (message-kill-buffer))
313 (defun notmuch-mua-message-send-hook ()
314 "The default function used for `notmuch-mua-send-hook', this
315 simply runs the corresponding `message-mode' hook functions."
316 (run-hooks 'message-send-hook))
320 (define-mail-user-agent 'notmuch-user-agent
321 'notmuch-mua-mail 'notmuch-mua-send-and-exit
322 'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
324 ;; Add some more headers to the list that `message-mode' hides when
325 ;; composing a message.
326 (notmuch-mua-add-more-hidden-headers)
330 (provide 'notmuch-mua)