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-compose-in 'current-window
41 "Where to create the mail buffer used to compose a new message.
42 Possible values are `current-window' (default), `new-window' and
43 `new-frame'. If set to `current-window', the mail buffer will be
44 displayed in the current window, so the old buffer will be
45 restored when the mail buffer is killed. If set to `new-window'
46 or `new-frame', the mail buffer will be displayed in a new
47 window/frame that will be destroyed when the buffer is killed.
48 You may want to customize `message-kill-buffer-on-exit'
50 (when (< emacs-major-version 24)
51 " Due to a known bug in Emacs 23, you should not set
52 this to `new-window' if `message-kill-buffer-on-exit' is
53 disabled: this would result in an incorrect behavior."))
55 :type '(choice (const :tag "Compose in the current window" current-window)
56 (const :tag "Compose mail in a new window" new-window)
57 (const :tag "Compose mail in a new frame" new-frame)))
59 (defcustom notmuch-mua-user-agent-function 'notmuch-mua-user-agent-full
60 "Function used to generate a `User-Agent:' string. If this is
61 `nil' then no `User-Agent:' will be generated."
62 :type '(choice (const :tag "No user agent string" nil)
63 (const :tag "Full" notmuch-mua-user-agent-full)
64 (const :tag "Notmuch" notmuch-mua-user-agent-notmuch)
65 (const :tag "Emacs" notmuch-mua-user-agent-emacs)
66 (function :tag "Custom user agent function"
67 :value notmuch-mua-user-agent-full))
70 (defcustom notmuch-mua-hidden-headers '("^User-Agent:")
71 "Headers that are added to the `message-mode' hidden headers
73 :type '(repeat string)
78 (defun notmuch-mua-get-switch-function ()
79 "Get a switch function according to `notmuch-mua-compose-in'."
80 (cond ((eq notmuch-mua-compose-in 'current-window)
82 ((eq notmuch-mua-compose-in 'new-window)
83 'switch-to-buffer-other-window)
84 ((eq notmuch-mua-compose-in 'new-frame)
85 'switch-to-buffer-other-frame)
86 (t (error "Invalid value for `notmuch-mua-compose-in'"))))
88 (defun notmuch-mua-maybe-set-window-dedicated ()
89 "Set the selected window as dedicated according to
90 `notmuch-mua-compose-in'."
91 (when (or (eq notmuch-mua-compose-in 'new-frame)
92 (eq notmuch-mua-compose-in 'new-window))
93 (set-window-dedicated-p (selected-window) t)))
95 (defun notmuch-mua-user-agent-full ()
96 "Generate a `User-Agent:' string suitable for notmuch."
97 (concat (notmuch-mua-user-agent-notmuch)
99 (notmuch-mua-user-agent-emacs)))
101 (defun notmuch-mua-user-agent-notmuch ()
102 "Generate a `User-Agent:' string suitable for notmuch."
103 (concat "Notmuch/" (notmuch-version) " (http://notmuchmail.org)"))
105 (defun notmuch-mua-user-agent-emacs ()
106 "Generate a `User-Agent:' string suitable for notmuch."
107 (concat "Emacs/" emacs-version " (" system-configuration ")"))
109 (defun notmuch-mua-add-more-hidden-headers ()
110 "Add some headers to the list that are hidden by default."
111 (mapc (lambda (header)
112 (when (not (member header message-hidden-headers))
113 (push header message-hidden-headers)))
114 notmuch-mua-hidden-headers))
116 (defun notmuch-mua-get-quotable-parts (parts)
117 (loop for part in parts
118 if (notmuch-match-content-type (plist-get part :content-type) "multipart/alternative")
119 collect (let* ((subparts (plist-get part :content))
120 (types (mapcar (lambda (part) (plist-get part :content-type)) subparts))
121 (chosen-type (car (notmuch-multipart/alternative-choose types))))
122 (loop for part in (reverse subparts)
123 if (notmuch-match-content-type (plist-get part :content-type) chosen-type)
125 else if (notmuch-match-content-type (plist-get part :content-type) "multipart/*")
126 append (notmuch-mua-get-quotable-parts (plist-get part :content))
127 else if (notmuch-match-content-type (plist-get part :content-type) "text/*")
130 (defun notmuch-mua-insert-quotable-part (message part)
132 (narrow-to-region (point) (point))
133 (notmuch-mm-display-part-inline message part (plist-get part :id)
134 (plist-get part :content-type)
135 notmuch-show-process-crypto)
136 (goto-char (point-max))))
138 ;; There is a bug in emacs 23's message.el that results in a newline
139 ;; not being inserted after the References header, so the next header
140 ;; is concatenated to the end of it. This function fixes the problem,
141 ;; while guarding against the possibility that some current or future
142 ;; version of emacs has the bug fixed.
143 (defun notmuch-mua-insert-references (original-func header references)
144 (funcall original-func header references)
145 (unless (bolp) (insert "\n")))
147 (defun notmuch-mua-reply (query-string &optional sender reply-all)
148 (let ((args '("reply" "--format=sexp" "--format-version=1"))
151 (when notmuch-show-process-crypto
152 (setq args (append args '("--decrypt"))))
155 (setq args (append args '("--reply-to=all")))
156 (setq args (append args '("--reply-to=sender"))))
157 (setq args (append args (list query-string)))
159 ;; Get the reply object as SEXP, and parse it into an elisp object.
160 (setq reply (apply #'notmuch-call-notmuch-sexp args))
162 ;; Extract the original message to simplify the following code.
163 (setq original (plist-get reply :original))
165 ;; Extract the headers of both the reply and the original message.
166 (let* ((original-headers (plist-get original :headers))
167 (reply-headers (plist-get reply :reply-headers)))
169 ;; If sender is non-nil, set the From: header to its value.
171 (plist-put reply-headers :From sender))
173 ;; Overlay the composition window on that being used to read
174 ;; the original message.
175 ((same-window-regexps '("\\*mail .*")))
177 ;; We modify message-header-format-alist to get around a bug in message.el.
178 ;; See the comment above on notmuch-mua-insert-references.
179 (let ((message-header-format-alist
180 (loop for pair in message-header-format-alist
181 if (eq (car pair) 'References)
182 collect (cons 'References
184 'notmuch-mua-insert-references
188 (notmuch-mua-mail (plist-get reply-headers :To)
189 (plist-get reply-headers :Subject)
190 (notmuch-headers-plist-to-alist reply-headers)
191 nil (notmuch-mua-get-switch-function))))
193 ;; Insert the message body - but put it in front of the signature
195 (goto-char (point-max))
196 (if (re-search-backward message-signature-separator nil t)
198 (goto-char (point-max)))
200 (let ((from (plist-get original-headers :From))
201 (date (plist-get original-headers :Date))
204 ;; message-cite-original constructs a citation line based on the From and Date
205 ;; headers of the original message, which are assumed to be in the buffer.
206 (insert "From: " from "\n")
207 (insert "Date: " date "\n\n")
209 ;; Get the parts of the original message that should be quoted; this includes
210 ;; all the text parts, except the non-preferred ones in a multipart/alternative.
211 (let ((quotable-parts (notmuch-mua-get-quotable-parts (plist-get original :body))))
212 (mapc (apply-partially 'notmuch-mua-insert-quotable-part original) quotable-parts))
216 ;; Quote the original message according to the user's configured style.
217 (message-cite-original))))
219 (goto-char (point-max))
222 (set-buffer-modified-p nil))
224 (defun notmuch-mua-forward-message ()
225 (funcall (notmuch-mua-get-switch-function) (current-buffer))
228 (when notmuch-mua-user-agent-function
229 (let ((user-agent (funcall notmuch-mua-user-agent-function)))
230 (when (not (string= "" user-agent))
231 (message-add-header (format "User-Agent: %s" user-agent)))))
232 (message-sort-headers)
233 (message-hide-headers)
234 (set-buffer-modified-p nil)
235 (notmuch-mua-maybe-set-window-dedicated)
239 (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)
240 "Invoke the notmuch mail composition window.
242 OTHER-ARGS are passed through to `message-mail'."
245 (when notmuch-mua-user-agent-function
246 (let ((user-agent (funcall notmuch-mua-user-agent-function)))
247 (when (not (string= "" user-agent))
248 (push (cons 'User-Agent user-agent) other-headers))))
250 (unless (assq 'From other-headers)
251 (push (cons 'From (concat
252 (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
254 (apply #'message-mail to subject other-headers other-args)
255 (message-sort-headers)
256 (message-hide-headers)
257 (set-buffer-modified-p nil)
258 (notmuch-mua-maybe-set-window-dedicated)
262 (defcustom notmuch-identities nil
263 "Identities that can be used as the From: address when composing a new message.
265 If this variable is left unset, then a list will be constructed from the
266 name and addresses configured in the notmuch configuration file."
267 :type '(repeat string)
268 :group 'notmuch-send)
270 (defcustom notmuch-always-prompt-for-sender nil
271 "Always prompt for the From: address when composing or forwarding a message.
273 This is not taken into account when replying to a message, because in that case
274 the From: header is already filled in by notmuch."
276 :group 'notmuch-send)
278 (defvar notmuch-mua-sender-history nil)
280 (defun notmuch-mua-prompt-for-sender ()
282 (let (name addresses one-name-only)
283 ;; If notmuch-identities is non-nil, check if there is a fixed user name.
284 (if notmuch-identities
285 (let ((components (mapcar 'mail-extract-address-components notmuch-identities)))
286 (setq name (caar components)
287 addresses (mapcar 'cadr components)
290 (mapcar (lambda (identity)
291 (string-equal name (car identity)))
293 ;; If notmuch-identities is nil, use values from the notmuch configuration file.
294 (setq name (notmuch-user-name)
295 addresses (cons (notmuch-user-primary-email) (notmuch-user-other-email))
297 ;; Now prompt the user, either for an email address only or for a full identity.
300 (ido-completing-read (concat "Sender address for " name ": ") addresses
301 nil nil nil 'notmuch-mua-sender-history (car addresses))))
302 (concat name " <" address ">"))
303 (ido-completing-read "Send mail From: " notmuch-identities
304 nil nil nil 'notmuch-mua-sender-history (car notmuch-identities)))))
306 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
307 "Invoke the notmuch mail composition window.
309 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
310 the From: address first."
313 (when (or prompt-for-sender notmuch-always-prompt-for-sender)
314 (list (cons 'From (notmuch-mua-prompt-for-sender))))))
315 (notmuch-mua-mail nil nil other-headers nil (notmuch-mua-get-switch-function))))
317 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
318 "Invoke the notmuch message forwarding window.
320 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
321 the From: address first."
323 (if (or prompt-for-sender notmuch-always-prompt-for-sender)
324 (let* ((sender (notmuch-mua-prompt-for-sender))
325 (address-components (mail-extract-address-components sender))
326 (user-full-name (car address-components))
327 (user-mail-address (cadr address-components)))
328 (notmuch-mua-forward-message))
329 (notmuch-mua-forward-message)))
331 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
332 "Invoke the notmuch reply window."
335 (when prompt-for-sender
336 (notmuch-mua-prompt-for-sender))))
337 (notmuch-mua-reply query-string sender reply-all)))
339 (defun notmuch-mua-send-and-exit (&optional arg)
341 (message-send-and-exit arg))
343 (defun notmuch-mua-kill-buffer ()
345 (message-kill-buffer))
347 (defun notmuch-mua-message-send-hook ()
348 "The default function used for `notmuch-mua-send-hook', this
349 simply runs the corresponding `message-mode' hook functions."
350 (run-hooks 'message-send-hook))
354 (define-mail-user-agent 'notmuch-user-agent
355 'notmuch-mua-mail 'notmuch-mua-send-and-exit
356 'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
358 ;; Add some more headers to the list that `message-mode' hides when
359 ;; composing a message.
360 (notmuch-mua-add-more-hidden-headers)
364 (provide 'notmuch-mua)