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-compose-in 'current-window
42 "Where to create the mail buffer used to compose a new message.
43 Possible values are `current-window' (default), `new-window' and
44 `new-frame'. If set to `current-window', the mail buffer will be
45 displayed in the current window, so the old buffer will be
46 restored when the mail buffer is killed. If set to `new-window'
47 or `new-frame', the mail buffer will be displayed in a new
48 window/frame that will be destroyed when the buffer is killed.
49 You may want to customize `message-kill-buffer-on-exit'
51 (when (< emacs-major-version 24)
52 " Due to a known bug in Emacs 23, you should not set
53 this to `new-window' if `message-kill-buffer-on-exit' is
54 disabled: this would result in an incorrect behavior."))
56 :type '(choice (const :tag "Compose in the current window" current-window)
57 (const :tag "Compose mail in a new window" new-window)
58 (const :tag "Compose mail in a new frame" new-frame)))
60 (defcustom notmuch-mua-user-agent-function 'notmuch-mua-user-agent-full
61 "Function used to generate a `User-Agent:' string. If this is
62 `nil' then no `User-Agent:' will be generated."
63 :type '(choice (const :tag "No user agent string" nil)
64 (const :tag "Full" notmuch-mua-user-agent-full)
65 (const :tag "Notmuch" notmuch-mua-user-agent-notmuch)
66 (const :tag "Emacs" notmuch-mua-user-agent-emacs)
67 (function :tag "Custom user agent function"
68 :value notmuch-mua-user-agent-full))
71 (defcustom notmuch-mua-hidden-headers '("^User-Agent:")
72 "Headers that are added to the `message-mode' hidden headers
74 :type '(repeat string)
79 (defun notmuch-mua-get-switch-function ()
80 "Get a switch function according to `notmuch-mua-compose-in'."
81 (cond ((eq notmuch-mua-compose-in 'current-window)
83 ((eq notmuch-mua-compose-in 'new-window)
84 'switch-to-buffer-other-window)
85 ((eq notmuch-mua-compose-in 'new-frame)
86 'switch-to-buffer-other-frame)
87 (t (error "Invalid value for `notmuch-mua-compose-in'"))))
89 (defun notmuch-mua-maybe-set-window-dedicated ()
90 "Set the selected window as dedicated according to
91 `notmuch-mua-compose-in'."
92 (when (or (eq notmuch-mua-compose-in 'new-frame)
93 (eq notmuch-mua-compose-in 'new-window))
94 (set-window-dedicated-p (selected-window) t)))
96 (defun notmuch-mua-user-agent-full ()
97 "Generate a `User-Agent:' string suitable for notmuch."
98 (concat (notmuch-mua-user-agent-notmuch)
100 (notmuch-mua-user-agent-emacs)))
102 (defun notmuch-mua-user-agent-notmuch ()
103 "Generate a `User-Agent:' string suitable for notmuch."
104 (concat "Notmuch/" (notmuch-version) " (http://notmuchmail.org)"))
106 (defun notmuch-mua-user-agent-emacs ()
107 "Generate a `User-Agent:' string suitable for notmuch."
108 (concat "Emacs/" emacs-version " (" system-configuration ")"))
110 (defun notmuch-mua-add-more-hidden-headers ()
111 "Add some headers to the list that are hidden by default."
112 (mapc (lambda (header)
113 (when (not (member header message-hidden-headers))
114 (push header message-hidden-headers)))
115 notmuch-mua-hidden-headers))
117 (defun notmuch-mua-get-quotable-parts (parts)
118 (loop for part in parts
119 if (notmuch-match-content-type (plist-get part :content-type) "multipart/alternative")
120 collect (let* ((subparts (plist-get part :content))
121 (types (mapcar (lambda (part) (plist-get part :content-type)) subparts))
122 (chosen-type (car (notmuch-multipart/alternative-choose types))))
123 (loop for part in (reverse subparts)
124 if (notmuch-match-content-type (plist-get part :content-type) chosen-type)
126 else if (notmuch-match-content-type (plist-get part :content-type) "multipart/*")
127 append (notmuch-mua-get-quotable-parts (plist-get part :content))
128 else if (notmuch-match-content-type (plist-get part :content-type) "text/*")
131 (defun notmuch-mua-insert-quotable-part (message part)
133 (narrow-to-region (point) (point))
134 (notmuch-mm-display-part-inline message part (plist-get part :id)
135 (plist-get part :content-type)
136 notmuch-show-process-crypto)
137 (goto-char (point-max))))
139 ;; There is a bug in emacs 23's message.el that results in a newline
140 ;; not being inserted after the References header, so the next header
141 ;; is concatenated to the end of it. This function fixes the problem,
142 ;; while guarding against the possibility that some current or future
143 ;; version of emacs has the bug fixed.
144 (defun notmuch-mua-insert-references (original-func header references)
145 (funcall original-func header references)
146 (unless (bolp) (insert "\n")))
148 (defun notmuch-mua-reply (query-string &optional sender reply-all)
149 (let ((args '("reply" "--format=json"))
152 (when notmuch-show-process-crypto
153 (setq args (append args '("--decrypt"))))
156 (setq args (append args '("--reply-to=all")))
157 (setq args (append args '("--reply-to=sender"))))
158 (setq args (append args (list query-string)))
160 ;; Get the reply object as JSON, and parse it into an elisp object.
162 (apply 'call-process (append (list notmuch-command nil (list t nil) nil) args))
163 (goto-char (point-min))
164 (let ((json-object-type 'plist)
165 (json-array-type 'list)
167 (setq reply (json-read))))
169 ;; Extract the original message to simplify the following code.
170 (setq original (plist-get reply :original))
172 ;; Extract the headers of both the reply and the original message.
173 (let* ((original-headers (plist-get original :headers))
174 (reply-headers (plist-get reply :reply-headers)))
176 ;; If sender is non-nil, set the From: header to its value.
178 (plist-put reply-headers :From sender))
180 ;; Overlay the composition window on that being used to read
181 ;; the original message.
182 ((same-window-regexps '("\\*mail .*")))
184 ;; We modify message-header-format-alist to get around a bug in message.el.
185 ;; See the comment above on notmuch-mua-insert-references.
186 (let ((message-header-format-alist
187 (loop for pair in message-header-format-alist
188 if (eq (car pair) 'References)
189 collect (cons 'References
191 'notmuch-mua-insert-references
195 (notmuch-mua-mail (plist-get reply-headers :To)
196 (plist-get reply-headers :Subject)
197 (notmuch-headers-plist-to-alist reply-headers)
198 nil (notmuch-mua-get-switch-function))))
200 ;; Insert the message body - but put it in front of the signature
202 (goto-char (point-max))
203 (if (re-search-backward message-signature-separator nil t)
205 (goto-char (point-max)))
207 (let ((from (plist-get original-headers :From))
208 (date (plist-get original-headers :Date))
211 ;; message-cite-original constructs a citation line based on the From and Date
212 ;; headers of the original message, which are assumed to be in the buffer.
213 (insert "From: " from "\n")
214 (insert "Date: " date "\n\n")
216 ;; Get the parts of the original message that should be quoted; this includes
217 ;; all the text parts, except the non-preferred ones in a multipart/alternative.
218 (let ((quotable-parts (notmuch-mua-get-quotable-parts (plist-get original :body))))
219 (mapc (apply-partially 'notmuch-mua-insert-quotable-part original) quotable-parts))
223 ;; Quote the original message according to the user's configured style.
224 (message-cite-original))))
226 (goto-char (point-max))
229 (set-buffer-modified-p nil))
231 (defun notmuch-mua-forward-message ()
232 (funcall (notmuch-mua-get-switch-function) (current-buffer))
235 (when notmuch-mua-user-agent-function
236 (let ((user-agent (funcall notmuch-mua-user-agent-function)))
237 (when (not (string= "" user-agent))
238 (message-add-header (format "User-Agent: %s" user-agent)))))
239 (message-sort-headers)
240 (message-hide-headers)
241 (set-buffer-modified-p nil)
242 (notmuch-mua-maybe-set-window-dedicated)
246 (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)
247 "Invoke the notmuch mail composition window.
249 OTHER-ARGS are passed through to `message-mail'."
252 (when notmuch-mua-user-agent-function
253 (let ((user-agent (funcall notmuch-mua-user-agent-function)))
254 (when (not (string= "" user-agent))
255 (push (cons 'User-Agent user-agent) other-headers))))
257 (unless (assq 'From other-headers)
258 (push (cons 'From (concat
259 (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
261 (apply #'message-mail to subject other-headers other-args)
262 (message-sort-headers)
263 (message-hide-headers)
264 (set-buffer-modified-p nil)
265 (notmuch-mua-maybe-set-window-dedicated)
269 (defcustom notmuch-identities nil
270 "Identities that can be used as the From: address when composing a new message.
272 If this variable is left unset, then a list will be constructed from the
273 name and addresses configured in the notmuch configuration file."
274 :type '(repeat string)
275 :group 'notmuch-send)
277 (defcustom notmuch-always-prompt-for-sender nil
278 "Always prompt for the From: address when composing or forwarding a message.
280 This is not taken into account when replying to a message, because in that case
281 the From: header is already filled in by notmuch."
283 :group 'notmuch-send)
285 (defvar notmuch-mua-sender-history nil)
287 (defun notmuch-mua-prompt-for-sender ()
289 (let (name addresses one-name-only)
290 ;; If notmuch-identities is non-nil, check if there is a fixed user name.
291 (if notmuch-identities
292 (let ((components (mapcar 'mail-extract-address-components notmuch-identities)))
293 (setq name (caar components)
294 addresses (mapcar 'cadr components)
297 (mapcar (lambda (identity)
298 (string-equal name (car identity)))
300 ;; If notmuch-identities is nil, use values from the notmuch configuration file.
301 (setq name (notmuch-user-name)
302 addresses (cons (notmuch-user-primary-email) (notmuch-user-other-email))
304 ;; Now prompt the user, either for an email address only or for a full identity.
307 (ido-completing-read (concat "Sender address for " name ": ") addresses
308 nil nil nil 'notmuch-mua-sender-history (car addresses))))
309 (concat name " <" address ">"))
310 (ido-completing-read "Send mail From: " notmuch-identities
311 nil nil nil 'notmuch-mua-sender-history (car notmuch-identities)))))
313 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
314 "Invoke the notmuch mail composition window.
316 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
317 the From: address first."
320 (when (or prompt-for-sender notmuch-always-prompt-for-sender)
321 (list (cons 'From (notmuch-mua-prompt-for-sender))))))
322 (notmuch-mua-mail nil nil other-headers nil (notmuch-mua-get-switch-function))))
324 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
325 "Invoke the notmuch message forwarding window.
327 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
328 the From: address first."
330 (if (or prompt-for-sender notmuch-always-prompt-for-sender)
331 (let* ((sender (notmuch-mua-prompt-for-sender))
332 (address-components (mail-extract-address-components sender))
333 (user-full-name (car address-components))
334 (user-mail-address (cadr address-components)))
335 (notmuch-mua-forward-message))
336 (notmuch-mua-forward-message)))
338 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
339 "Invoke the notmuch reply window."
342 (when prompt-for-sender
343 (notmuch-mua-prompt-for-sender))))
344 (notmuch-mua-reply query-string sender reply-all)))
346 (defun notmuch-mua-send-and-exit (&optional arg)
348 (message-send-and-exit arg))
350 (defun notmuch-mua-kill-buffer ()
352 (message-kill-buffer))
354 (defun notmuch-mua-message-send-hook ()
355 "The default function used for `notmuch-mua-send-hook', this
356 simply runs the corresponding `message-mode' hook functions."
357 (run-hooks 'message-send-hook))
361 (define-mail-user-agent 'notmuch-user-agent
362 'notmuch-mua-mail 'notmuch-mua-send-and-exit
363 'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
365 ;; Add some more headers to the list that `message-mode' hides when
366 ;; composing a message.
367 (notmuch-mua-add-more-hidden-headers)
371 (provide 'notmuch-mua)