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 <https://www.gnu.org/licenses/>.
20 ;; Authors: David Edmondson <dme@dme.org>
26 (require 'format-spec)
28 (require 'notmuch-lib)
29 (require 'notmuch-address)
31 (eval-when-compile (require 'cl))
33 (declare-function notmuch-show-insert-body "notmuch-show" (msg body depth))
34 (declare-function notmuch-fcc-header-setup "notmuch-maildir-fcc" ())
35 (declare-function notmuch-fcc-handler "notmuch-maildir-fcc" (destdir))
36 (declare-function notmuch-maildir-message-do-fcc "notmuch-maildir-fcc" ())
40 (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
41 "Hook run before sending messages."
44 :group 'notmuch-hooks)
46 (defcustom notmuch-mua-compose-in 'current-window
48 "Where to create the mail buffer used to compose a new message.
49 Possible values are `current-window' (default), `new-window' and
50 `new-frame'. If set to `current-window', the mail buffer will be
51 displayed in the current window, so the old buffer will be
52 restored when the mail buffer is killed. If set to `new-window'
53 or `new-frame', the mail buffer will be displayed in a new
54 window/frame that will be destroyed when the buffer is killed.
55 You may want to customize `message-kill-buffer-on-exit'
57 (when (< emacs-major-version 24)
58 " Due to a known bug in Emacs 23, you should not set
59 this to `new-window' if `message-kill-buffer-on-exit' is
60 disabled: this would result in an incorrect behavior."))
62 :type '(choice (const :tag "Compose in the current window" current-window)
63 (const :tag "Compose mail in a new window" new-window)
64 (const :tag "Compose mail in a new frame" new-frame)))
66 (defcustom notmuch-mua-user-agent-function nil
67 "Function used to generate a `User-Agent:' string. If this is
68 `nil' then no `User-Agent:' will be generated."
69 :type '(choice (const :tag "No user agent string" nil)
70 (const :tag "Full" notmuch-mua-user-agent-full)
71 (const :tag "Notmuch" notmuch-mua-user-agent-notmuch)
72 (const :tag "Emacs" notmuch-mua-user-agent-emacs)
73 (function :tag "Custom user agent function"
74 :value notmuch-mua-user-agent-full))
77 (defcustom notmuch-mua-hidden-headers nil
78 "Headers that are added to the `message-mode' hidden headers
80 :type '(repeat string)
83 (defgroup notmuch-reply nil
84 "Replying to messages in notmuch"
87 (defcustom notmuch-mua-cite-function 'message-cite-original
88 "*Function for citing an original message.
89 Predefined functions include `message-cite-original' and
90 `message-cite-original-without-signature'.
91 Note that these functions use `mail-citation-hook' if that is non-nil."
92 :type '(radio (function-item message-cite-original)
93 (function-item message-cite-original-without-signature)
94 (function-item sc-cite-original)
95 (function :tag "Other"))
96 :link '(custom-manual "(message)Insertion Variables")
97 :group 'notmuch-reply)
99 (defcustom notmuch-mua-reply-insert-header-p-function
100 'notmuch-show-reply-insert-header-p-never
101 "Function to decide which parts get a header when replying.
103 This function specifies which parts of a mime message with
104 mutiple parts get a header."
105 :type '(radio (const :tag "No part headers"
106 notmuch-show-reply-insert-header-p-never)
107 (const :tag "All except multipart/* and hidden parts"
108 notmuch-show-reply-insert-header-p-trimmed)
109 (const :tag "Only for included text parts"
110 notmuch-show-reply-insert-header-p-minimal)
111 (const :tag "Exactly as in show view"
112 notmuch-show-insert-header-p)
113 (function :tag "Other"))
114 :group 'notmuch-reply)
118 (defun notmuch-mua-get-switch-function ()
119 "Get a switch function according to `notmuch-mua-compose-in'."
120 (cond ((eq notmuch-mua-compose-in 'current-window)
122 ((eq notmuch-mua-compose-in 'new-window)
123 'switch-to-buffer-other-window)
124 ((eq notmuch-mua-compose-in 'new-frame)
125 'switch-to-buffer-other-frame)
126 (t (error "Invalid value for `notmuch-mua-compose-in'"))))
128 (defun notmuch-mua-maybe-set-window-dedicated ()
129 "Set the selected window as dedicated according to
130 `notmuch-mua-compose-in'."
131 (when (or (eq notmuch-mua-compose-in 'new-frame)
132 (eq notmuch-mua-compose-in 'new-window))
133 (set-window-dedicated-p (selected-window) t)))
135 (defun notmuch-mua-user-agent-full ()
136 "Generate a `User-Agent:' string suitable for notmuch."
137 (concat (notmuch-mua-user-agent-notmuch)
139 (notmuch-mua-user-agent-emacs)))
141 (defun notmuch-mua-user-agent-notmuch ()
142 "Generate a `User-Agent:' string suitable for notmuch."
143 (let ((notmuch-version (if (string= notmuch-emacs-version "unknown")
144 (notmuch-cli-version)
145 notmuch-emacs-version)))
146 (concat "Notmuch/" notmuch-version " (https://notmuchmail.org)")))
148 (defun notmuch-mua-user-agent-emacs ()
149 "Generate a `User-Agent:' string suitable for notmuch."
150 (concat "Emacs/" emacs-version " (" system-configuration ")"))
152 (defun notmuch-mua-add-more-hidden-headers ()
153 "Add some headers to the list that are hidden by default."
154 (mapc (lambda (header)
155 (when (not (member header message-hidden-headers))
156 (push header message-hidden-headers)))
157 notmuch-mua-hidden-headers))
159 (defun notmuch-mua-reply-crypto (parts)
160 "Add mml sign-encrypt flag if any part of original message is encrypted."
161 (loop for part in parts
162 if (notmuch-match-content-type (plist-get part :content-type) "multipart/encrypted")
163 do (mml-secure-message-sign-encrypt)
164 else if (notmuch-match-content-type (plist-get part :content-type) "multipart/*")
165 do (notmuch-mua-reply-crypto (plist-get part :content))))
167 ;; There is a bug in emacs 23's message.el that results in a newline
168 ;; not being inserted after the References header, so the next header
169 ;; is concatenated to the end of it. This function fixes the problem,
170 ;; while guarding against the possibility that some current or future
171 ;; version of emacs has the bug fixed.
172 (defun notmuch-mua-insert-references (original-func header references)
173 (funcall original-func header references)
174 (unless (bolp) (insert "\n")))
176 (defun notmuch-mua-reply (query-string &optional sender reply-all)
177 (let ((args '("reply" "--format=sexp" "--format-version=1"))
178 (process-crypto notmuch-show-process-crypto)
182 (setq args (append args '("--decrypt"))))
185 (setq args (append args '("--reply-to=all")))
186 (setq args (append args '("--reply-to=sender"))))
187 (setq args (append args (list query-string)))
189 ;; Get the reply object as SEXP, and parse it into an elisp object.
190 (setq reply (apply #'notmuch-call-notmuch-sexp args))
192 ;; Extract the original message to simplify the following code.
193 (setq original (plist-get reply :original))
195 ;; Extract the headers of both the reply and the original message.
196 (let* ((original-headers (plist-get original :headers))
197 (reply-headers (plist-get reply :reply-headers)))
199 ;; If sender is non-nil, set the From: header to its value.
201 (plist-put reply-headers :From sender))
203 ;; Overlay the composition window on that being used to read
204 ;; the original message.
205 ((same-window-regexps '("\\*mail .*")))
207 ;; We modify message-header-format-alist to get around a bug in message.el.
208 ;; See the comment above on notmuch-mua-insert-references.
209 (let ((message-header-format-alist
210 (loop for pair in message-header-format-alist
211 if (eq (car pair) 'References)
212 collect (cons 'References
214 'notmuch-mua-insert-references
218 (notmuch-mua-mail (plist-get reply-headers :To)
219 (plist-get reply-headers :Subject)
220 (notmuch-headers-plist-to-alist reply-headers)
221 nil (notmuch-mua-get-switch-function))))
223 ;; Insert the message body - but put it in front of the signature
224 ;; if one is present, and after any other content
225 ;; message*setup-hooks may have added to the message body already.
228 (narrow-to-region (point) (point-max))
229 (goto-char (point-max))
230 (if (re-search-backward message-signature-separator nil t)
231 (if message-signature-insert-empty-line
233 (goto-char (point-max))))
235 (let ((from (plist-get original-headers :From))
236 (date (plist-get original-headers :Date))
239 ;; notmuch-mua-cite-function constructs a citation line based
240 ;; on the From and Date headers of the original message, which
241 ;; are assumed to be in the buffer.
242 (insert "From: " from "\n")
243 (insert "Date: " date "\n\n")
245 (insert (with-temp-buffer
247 ;; Don't attempt to clean up messages, excerpt
248 ;; citations, etc. in the original message before
250 ((notmuch-show-insert-text/plain-hook nil)
251 ;; Don't omit long parts.
252 (notmuch-show-max-text-part-size 0)
253 ;; Insert headers for parts as appropriate for replying.
254 (notmuch-show-insert-header-p-function notmuch-mua-reply-insert-header-p-function)
255 ;; Don't indent multipart sub-parts.
256 (notmuch-show-indent-multipart nil))
257 (notmuch-show-insert-body original (plist-get original :body) 0)
258 (buffer-substring-no-properties (point-min) (point-max)))))
262 ;; Quote the original message according to the user's configured style.
263 (funcall notmuch-mua-cite-function)))
265 ;; Crypto processing based crypto content of the original message
267 (notmuch-mua-reply-crypto (plist-get original :body))))
269 ;; Push mark right before signature, if any.
270 (message-goto-signature)
276 (set-buffer-modified-p nil))
278 (define-derived-mode notmuch-message-mode message-mode "Message[Notmuch]"
279 "Notmuch message composition mode. Mostly like `message-mode'"
280 (notmuch-address-setup))
282 (put 'notmuch-message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
284 (define-key notmuch-message-mode-map (kbd "C-c C-c") #'notmuch-mua-send-and-exit)
285 (define-key notmuch-message-mode-map (kbd "C-c C-s") #'notmuch-mua-send)
287 (defun notmuch-mua-pop-to-buffer (name switch-function)
288 "Pop to buffer NAME, and warn if it already exists and is
289 modified. This function is notmuch addaptation of
290 `message-pop-to-buffer'."
291 (let ((buffer (get-buffer name)))
293 (buffer-name buffer))
294 (let ((window (get-buffer-window buffer 0)))
296 ;; Raise the frame already displaying the message buffer.
298 (gnus-select-frame-set-input-focus (window-frame window))
299 (select-window window))
300 (funcall switch-function buffer)
302 (when (and (buffer-modified-p)
305 "Message already being composed; erase? ")
307 (error "Message being composed")))
308 (funcall switch-function name)
311 (notmuch-message-mode)))
313 (defun notmuch-mua-mail (&optional to subject other-headers continue
314 switch-function yank-action send-actions
315 return-action &rest ignored)
316 "Invoke the notmuch mail composition window."
319 (when notmuch-mua-user-agent-function
320 (let ((user-agent (funcall notmuch-mua-user-agent-function)))
321 (when (not (string= "" user-agent))
322 (push (cons 'User-Agent user-agent) other-headers))))
324 (unless (assq 'From other-headers)
325 (push (cons 'From (message-make-from
326 (notmuch-user-name) (notmuch-user-primary-email))) other-headers))
328 (notmuch-mua-pop-to-buffer (message-buffer-name "mail" to)
329 (or switch-function (notmuch-mua-get-switch-function)))
332 ;; The following is copied from `message-mail'
333 `((To . ,(or to "")) (Subject . ,(or subject "")))
334 ;; C-h f compose-mail says that headers should be specified as
335 ;; (string . value); however all the rest of message expects
336 ;; headers to be symbols, not strings (eg message-header-format-alist).
337 ;; https://lists.gnu.org/archive/html/emacs-devel/2011-01/msg00337.html
338 ;; We need to convert any string input, eg from rmail-start-mail.
339 (dolist (h other-headers other-headers)
340 (if (stringp (car h)) (setcar h (intern (capitalize (car h))))))))
341 (args (list yank-action send-actions))
342 ;; Cause `message-setup-1' to do things relevant for mail,
343 ;; such as observe `message-default-mail-headers'.
344 (message-this-is-mail t))
345 ;; message-setup-1 in Emacs 23 does not accept return-action
346 ;; argument. Pass it only if it is supplied by the caller. This
347 ;; will never be the case when we're called by `compose-mail' in
349 (when return-action (nconc args '(return-action)))
350 (apply 'message-setup-1 headers args))
351 (notmuch-fcc-header-setup)
352 (message-sort-headers)
353 (message-hide-headers)
354 (set-buffer-modified-p nil)
355 (notmuch-mua-maybe-set-window-dedicated)
359 (defcustom notmuch-identities nil
360 "Identities that can be used as the From: address when composing a new message.
362 If this variable is left unset, then a list will be constructed from the
363 name and addresses configured in the notmuch configuration file."
364 :type '(repeat string)
365 :group 'notmuch-send)
367 (defcustom notmuch-always-prompt-for-sender nil
368 "Always prompt for the From: address when composing or forwarding a message.
370 This is not taken into account when replying to a message, because in that case
371 the From: header is already filled in by notmuch."
373 :group 'notmuch-send)
375 (defvar notmuch-mua-sender-history nil)
377 ;; Workaround: Running `ido-completing-read' in emacs 23.1, 23.2 and 23.3
378 ;; without some explicit initialization fill freeze the operation.
379 ;; Hence, we advice `ido-completing-read' to ensure required initialization
381 (if (and (= emacs-major-version 23) (< emacs-minor-version 4))
382 (defadvice ido-completing-read (before notmuch-ido-mode-init activate)
383 (ido-init-completion-maps)
384 (add-hook 'minibuffer-setup-hook 'ido-minibuffer-setup)
385 (add-hook 'choose-completion-string-functions
386 'ido-choose-completion-string)
387 (ad-disable-advice 'ido-completing-read 'before 'notmuch-ido-mode-init)
388 (ad-activate 'ido-completing-read)))
390 (defun notmuch-mua-prompt-for-sender ()
391 "Prompt for a sender from the user's configured identities."
392 (if notmuch-identities
393 (ido-completing-read "Send mail from: " notmuch-identities
394 nil nil nil 'notmuch-mua-sender-history
395 (car notmuch-identities))
396 (let* ((name (notmuch-user-name))
397 (addrs (cons (notmuch-user-primary-email)
398 (notmuch-user-other-email)))
400 (ido-completing-read (concat "Sender address for " name ": ") addrs
401 nil nil nil 'notmuch-mua-sender-history
403 (message-make-from name address))))
405 (put 'notmuch-mua-new-mail 'notmuch-prefix-doc "... and prompt for sender")
406 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
409 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
410 the From: address first."
413 (when (or prompt-for-sender notmuch-always-prompt-for-sender)
414 (list (cons 'From (notmuch-mua-prompt-for-sender))))))
415 (notmuch-mua-mail nil nil other-headers nil (notmuch-mua-get-switch-function))))
417 (defun notmuch-mua-new-forward-messages (messages &optional prompt-for-sender)
418 "Compose a new message forwarding MESSAGES.
420 If PROMPT-FOR-SENDER is non-nil, the user will be prompteed for
422 (let* ((other-headers
423 (when (or prompt-for-sender notmuch-always-prompt-for-sender)
424 (list (cons 'From (notmuch-mua-prompt-for-sender)))))
425 forward-subject) ;; Comes from the first message and is
428 ;; Generate the template for the outgoing message.
429 (notmuch-mua-mail nil "" other-headers nil (notmuch-mua-get-switch-function))
432 ;; Insert all of the forwarded messages.
434 (let ((temp-buffer (get-buffer-create
435 (concat "*notmuch-fwd-raw-" id "*"))))
436 ;; Get the raw version of this message in the buffer.
437 (with-current-buffer temp-buffer
439 (let ((coding-system-for-read 'no-conversion))
440 (call-process notmuch-command nil t nil "show" "--format=raw" id))
441 ;; Because we process the messages in reverse order,
442 ;; always generate a forwarded subject, then use the
443 ;; last (i.e. first) one.
444 (setq forward-subject (message-make-forward-subject)))
445 ;; Make a copy ready to be forwarded in the
446 ;; composition buffer.
447 (message-forward-make-body temp-buffer)
448 ;; Kill the temporary buffer.
449 (kill-buffer temp-buffer)))
450 ;; `message-forward-make-body' always puts the message at
451 ;; the top, so do them in reverse order.
454 ;; Add in the appropriate subject.
456 (message-narrow-to-headers)
457 (message-remove-header "Subject")
458 (message-add-header (concat "Subject: " forward-subject)))
460 ;; `message-forward-make-body' shows the User-agent header. Hide
462 (message-hide-headers)
463 (set-buffer-modified-p nil))))
465 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
466 "Compose a reply to the message identified by QUERY-STRING.
468 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
469 the From: address first. If REPLY-ALL is non-nil, the message
470 will be addressed to all recipients of the source message."
472 ;; In current emacs (24.3) select-active-regions is set to t by
473 ;; default. The reply insertion code sets the region to the quoted
474 ;; message to make it easy to delete (kill-region or C-w). These two
475 ;; things combine to put the quoted message in the primary selection.
477 ;; This is not what the user wanted and is a privacy risk (accidental
478 ;; pasting of the quoted message). We can avoid some of the problems
479 ;; by let-binding select-active-regions to nil. This fixes if the
480 ;; primary selection was previously in a non-emacs window but not if
481 ;; it was in an emacs window. To avoid the problem in the latter case
482 ;; we deactivate mark.
485 (when prompt-for-sender
486 (notmuch-mua-prompt-for-sender)))
487 (select-active-regions nil))
488 (notmuch-mua-reply query-string sender reply-all)
491 (defun notmuch-mua-send-and-exit (&optional arg)
493 (let ((message-fcc-handler-function #'notmuch-fcc-handler))
494 (letf (((symbol-function 'message-do-fcc) #'notmuch-maildir-message-do-fcc))
495 (message-send-and-exit arg))))
497 (defun notmuch-mua-send (&optional arg)
499 (let ((message-fcc-handler-function #'notmuch-fcc-handler))
500 (letf (((symbol-function 'message-do-fcc) #'notmuch-maildir-message-do-fcc))
501 (message-send arg))))
503 (defun notmuch-mua-kill-buffer ()
505 (message-kill-buffer))
507 (defun notmuch-mua-message-send-hook ()
508 "The default function used for `notmuch-mua-send-hook', this
509 simply runs the corresponding `message-mode' hook functions."
510 (run-hooks 'message-send-hook))
514 (define-mail-user-agent 'notmuch-user-agent
515 'notmuch-mua-mail 'notmuch-mua-send-and-exit
516 'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
518 ;; Add some more headers to the list that `message-mode' hides when
519 ;; composing a message.
520 (notmuch-mua-add-more-hidden-headers)
524 (provide 'notmuch-mua)
526 ;;; notmuch-mua.el ends here