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=sexp" "--format-version=1"))
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.
161 (setq reply (apply #'notmuch-call-notmuch-sexp args))
163 ;; Extract the original message to simplify the following code.
164 (setq original (plist-get reply :original))
166 ;; Extract the headers of both the reply and the original message.
167 (let* ((original-headers (plist-get original :headers))
168 (reply-headers (plist-get reply :reply-headers)))
170 ;; If sender is non-nil, set the From: header to its value.
172 (plist-put reply-headers :From sender))
174 ;; Overlay the composition window on that being used to read
175 ;; the original message.
176 ((same-window-regexps '("\\*mail .*")))
178 ;; We modify message-header-format-alist to get around a bug in message.el.
179 ;; See the comment above on notmuch-mua-insert-references.
180 (let ((message-header-format-alist
181 (loop for pair in message-header-format-alist
182 if (eq (car pair) 'References)
183 collect (cons 'References
185 'notmuch-mua-insert-references
189 (notmuch-mua-mail (plist-get reply-headers :To)
190 (plist-get reply-headers :Subject)
191 (notmuch-headers-plist-to-alist reply-headers)
192 nil (notmuch-mua-get-switch-function))))
194 ;; Insert the message body - but put it in front of the signature
196 (goto-char (point-max))
197 (if (re-search-backward message-signature-separator nil t)
199 (goto-char (point-max)))
201 (let ((from (plist-get original-headers :From))
202 (date (plist-get original-headers :Date))
205 ;; message-cite-original constructs a citation line based on the From and Date
206 ;; headers of the original message, which are assumed to be in the buffer.
207 (insert "From: " from "\n")
208 (insert "Date: " date "\n\n")
210 ;; Get the parts of the original message that should be quoted; this includes
211 ;; all the text parts, except the non-preferred ones in a multipart/alternative.
212 (let ((quotable-parts (notmuch-mua-get-quotable-parts (plist-get original :body))))
213 (mapc (apply-partially 'notmuch-mua-insert-quotable-part original) quotable-parts))
217 ;; Quote the original message according to the user's configured style.
218 (message-cite-original))))
220 (goto-char (point-max))
223 (set-buffer-modified-p nil))
225 (defun notmuch-mua-forward-message ()
226 (funcall (notmuch-mua-get-switch-function) (current-buffer))
229 (when notmuch-mua-user-agent-function
230 (let ((user-agent (funcall notmuch-mua-user-agent-function)))
231 (when (not (string= "" user-agent))
232 (message-add-header (format "User-Agent: %s" user-agent)))))
233 (message-sort-headers)
234 (message-hide-headers)
235 (set-buffer-modified-p nil)
236 (notmuch-mua-maybe-set-window-dedicated)
240 (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)
241 "Invoke the notmuch mail composition window.
243 OTHER-ARGS are passed through to `message-mail'."
246 (when notmuch-mua-user-agent-function
247 (let ((user-agent (funcall notmuch-mua-user-agent-function)))
248 (when (not (string= "" user-agent))
249 (push (cons 'User-Agent user-agent) other-headers))))
251 (unless (assq 'From other-headers)
252 (push (cons 'From (concat
253 (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
255 (apply #'message-mail to subject other-headers other-args)
256 (message-sort-headers)
257 (message-hide-headers)
258 (set-buffer-modified-p nil)
259 (notmuch-mua-maybe-set-window-dedicated)
263 (defcustom notmuch-identities nil
264 "Identities that can be used as the From: address when composing a new message.
266 If this variable is left unset, then a list will be constructed from the
267 name and addresses configured in the notmuch configuration file."
268 :type '(repeat string)
269 :group 'notmuch-send)
271 (defcustom notmuch-always-prompt-for-sender nil
272 "Always prompt for the From: address when composing or forwarding a message.
274 This is not taken into account when replying to a message, because in that case
275 the From: header is already filled in by notmuch."
277 :group 'notmuch-send)
279 (defvar notmuch-mua-sender-history nil)
281 (defun notmuch-mua-prompt-for-sender ()
283 (let (name addresses one-name-only)
284 ;; If notmuch-identities is non-nil, check if there is a fixed user name.
285 (if notmuch-identities
286 (let ((components (mapcar 'mail-extract-address-components notmuch-identities)))
287 (setq name (caar components)
288 addresses (mapcar 'cadr components)
291 (mapcar (lambda (identity)
292 (string-equal name (car identity)))
294 ;; If notmuch-identities is nil, use values from the notmuch configuration file.
295 (setq name (notmuch-user-name)
296 addresses (cons (notmuch-user-primary-email) (notmuch-user-other-email))
298 ;; Now prompt the user, either for an email address only or for a full identity.
301 (ido-completing-read (concat "Sender address for " name ": ") addresses
302 nil nil nil 'notmuch-mua-sender-history (car addresses))))
303 (concat name " <" address ">"))
304 (ido-completing-read "Send mail From: " notmuch-identities
305 nil nil nil 'notmuch-mua-sender-history (car notmuch-identities)))))
307 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
308 "Invoke the notmuch mail composition window.
310 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
311 the From: address first."
314 (when (or prompt-for-sender notmuch-always-prompt-for-sender)
315 (list (cons 'From (notmuch-mua-prompt-for-sender))))))
316 (notmuch-mua-mail nil nil other-headers nil (notmuch-mua-get-switch-function))))
318 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
319 "Invoke the notmuch message forwarding window.
321 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
322 the From: address first."
324 (if (or prompt-for-sender notmuch-always-prompt-for-sender)
325 (let* ((sender (notmuch-mua-prompt-for-sender))
326 (address-components (mail-extract-address-components sender))
327 (user-full-name (car address-components))
328 (user-mail-address (cadr address-components)))
329 (notmuch-mua-forward-message))
330 (notmuch-mua-forward-message)))
332 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
333 "Invoke the notmuch reply window."
336 (when prompt-for-sender
337 (notmuch-mua-prompt-for-sender))))
338 (notmuch-mua-reply query-string sender reply-all)))
340 (defun notmuch-mua-send-and-exit (&optional arg)
342 (message-send-and-exit arg))
344 (defun notmuch-mua-kill-buffer ()
346 (message-kill-buffer))
348 (defun notmuch-mua-message-send-hook ()
349 "The default function used for `notmuch-mua-send-hook', this
350 simply runs the corresponding `message-mode' hook functions."
351 (run-hooks 'message-send-hook))
355 (define-mail-user-agent 'notmuch-user-agent
356 'notmuch-mua-mail 'notmuch-mua-send-and-exit
357 'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
359 ;; Add some more headers to the list that `message-mode' hides when
360 ;; composing a message.
361 (notmuch-mua-add-more-hidden-headers)
365 (provide 'notmuch-mua)