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-maildir-message-do-fcc "notmuch-maildir-fcc" ())
39 (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
40 "Hook run before sending messages."
43 :group 'notmuch-hooks)
45 (defcustom notmuch-mua-compose-in 'current-window
47 "Where to create the mail buffer used to compose a new message.
48 Possible values are `current-window' (default), `new-window' and
49 `new-frame'. If set to `current-window', the mail buffer will be
50 displayed in the current window, so the old buffer will be
51 restored when the mail buffer is killed. If set to `new-window'
52 or `new-frame', the mail buffer will be displayed in a new
53 window/frame that will be destroyed when the buffer is killed.
54 You may want to customize `message-kill-buffer-on-exit'
56 (when (< emacs-major-version 24)
57 " Due to a known bug in Emacs 23, you should not set
58 this to `new-window' if `message-kill-buffer-on-exit' is
59 disabled: this would result in an incorrect behavior."))
61 :type '(choice (const :tag "Compose in the current window" current-window)
62 (const :tag "Compose mail in a new window" new-window)
63 (const :tag "Compose mail in a new frame" new-frame)))
65 (defcustom notmuch-mua-user-agent-function nil
66 "Function used to generate a `User-Agent:' string. If this is
67 `nil' then no `User-Agent:' will be generated."
68 :type '(choice (const :tag "No user agent string" nil)
69 (const :tag "Full" notmuch-mua-user-agent-full)
70 (const :tag "Notmuch" notmuch-mua-user-agent-notmuch)
71 (const :tag "Emacs" notmuch-mua-user-agent-emacs)
72 (function :tag "Custom user agent function"
73 :value notmuch-mua-user-agent-full))
76 (defcustom notmuch-mua-hidden-headers nil
77 "Headers that are added to the `message-mode' hidden headers
79 :type '(repeat string)
82 (defgroup notmuch-reply nil
83 "Replying to messages in notmuch"
86 (defcustom notmuch-mua-cite-function 'message-cite-original
87 "*Function for citing an original message.
88 Predefined functions include `message-cite-original' and
89 `message-cite-original-without-signature'.
90 Note that these functions use `mail-citation-hook' if that is non-nil."
91 :type '(radio (function-item message-cite-original)
92 (function-item message-cite-original-without-signature)
93 (function-item sc-cite-original)
94 (function :tag "Other"))
95 :link '(custom-manual "(message)Insertion Variables")
96 :group 'notmuch-reply)
98 (defcustom notmuch-mua-reply-insert-header-p-function
99 'notmuch-show-reply-insert-header-p-never
100 "Function to decide which parts get a header when replying.
102 This function specifies which parts of a mime message with
103 mutiple parts get a header."
104 :type '(radio (const :tag "No part headers"
105 notmuch-show-reply-insert-header-p-never)
106 (const :tag "All except multipart/* and hidden parts"
107 notmuch-show-reply-insert-header-p-trimmed)
108 (const :tag "Only for included text parts"
109 notmuch-show-reply-insert-header-p-minimal)
110 (const :tag "Exactly as in show view"
111 notmuch-show-insert-header-p)
112 (function :tag "Other"))
113 :group 'notmuch-reply)
117 (defun notmuch-mua-get-switch-function ()
118 "Get a switch function according to `notmuch-mua-compose-in'."
119 (cond ((eq notmuch-mua-compose-in 'current-window)
121 ((eq notmuch-mua-compose-in 'new-window)
122 'switch-to-buffer-other-window)
123 ((eq notmuch-mua-compose-in 'new-frame)
124 'switch-to-buffer-other-frame)
125 (t (error "Invalid value for `notmuch-mua-compose-in'"))))
127 (defun notmuch-mua-maybe-set-window-dedicated ()
128 "Set the selected window as dedicated according to
129 `notmuch-mua-compose-in'."
130 (when (or (eq notmuch-mua-compose-in 'new-frame)
131 (eq notmuch-mua-compose-in 'new-window))
132 (set-window-dedicated-p (selected-window) t)))
134 (defun notmuch-mua-user-agent-full ()
135 "Generate a `User-Agent:' string suitable for notmuch."
136 (concat (notmuch-mua-user-agent-notmuch)
138 (notmuch-mua-user-agent-emacs)))
140 (defun notmuch-mua-user-agent-notmuch ()
141 "Generate a `User-Agent:' string suitable for notmuch."
142 (let ((notmuch-version (if (string= notmuch-emacs-version "unknown")
143 (notmuch-cli-version)
144 notmuch-emacs-version)))
145 (concat "Notmuch/" notmuch-version " (https://notmuchmail.org)")))
147 (defun notmuch-mua-user-agent-emacs ()
148 "Generate a `User-Agent:' string suitable for notmuch."
149 (concat "Emacs/" emacs-version " (" system-configuration ")"))
151 (defun notmuch-mua-add-more-hidden-headers ()
152 "Add some headers to the list that are hidden by default."
153 (mapc (lambda (header)
154 (when (not (member header message-hidden-headers))
155 (push header message-hidden-headers)))
156 notmuch-mua-hidden-headers))
158 (defun notmuch-mua-reply-crypto (parts)
159 "Add mml sign-encrypt flag if any part of original message is encrypted."
160 (loop for part in parts
161 if (notmuch-match-content-type (plist-get part :content-type) "multipart/encrypted")
162 do (mml-secure-message-sign-encrypt)
163 else if (notmuch-match-content-type (plist-get part :content-type) "multipart/*")
164 do (notmuch-mua-reply-crypto (plist-get part :content))))
166 ;; There is a bug in emacs 23's message.el that results in a newline
167 ;; not being inserted after the References header, so the next header
168 ;; is concatenated to the end of it. This function fixes the problem,
169 ;; while guarding against the possibility that some current or future
170 ;; version of emacs has the bug fixed.
171 (defun notmuch-mua-insert-references (original-func header references)
172 (funcall original-func header references)
173 (unless (bolp) (insert "\n")))
175 (defun notmuch-mua-reply (query-string &optional sender reply-all)
176 (let ((args '("reply" "--format=sexp" "--format-version=1"))
177 (process-crypto notmuch-show-process-crypto)
181 (setq args (append args '("--decrypt"))))
184 (setq args (append args '("--reply-to=all")))
185 (setq args (append args '("--reply-to=sender"))))
186 (setq args (append args (list query-string)))
188 ;; Get the reply object as SEXP, and parse it into an elisp object.
189 (setq reply (apply #'notmuch-call-notmuch-sexp args))
191 ;; Extract the original message to simplify the following code.
192 (setq original (plist-get reply :original))
194 ;; Extract the headers of both the reply and the original message.
195 (let* ((original-headers (plist-get original :headers))
196 (reply-headers (plist-get reply :reply-headers)))
198 ;; If sender is non-nil, set the From: header to its value.
200 (plist-put reply-headers :From sender))
202 ;; Overlay the composition window on that being used to read
203 ;; the original message.
204 ((same-window-regexps '("\\*mail .*")))
206 ;; We modify message-header-format-alist to get around a bug in message.el.
207 ;; See the comment above on notmuch-mua-insert-references.
208 (let ((message-header-format-alist
209 (loop for pair in message-header-format-alist
210 if (eq (car pair) 'References)
211 collect (cons 'References
213 'notmuch-mua-insert-references
217 (notmuch-mua-mail (plist-get reply-headers :To)
218 (plist-get reply-headers :Subject)
219 (notmuch-headers-plist-to-alist reply-headers)
220 nil (notmuch-mua-get-switch-function))))
222 ;; Insert the message body - but put it in front of the signature
223 ;; if one is present, and after any other content
224 ;; message*setup-hooks may have added to the message body already.
227 (narrow-to-region (point) (point-max))
228 (goto-char (point-max))
229 (if (re-search-backward message-signature-separator nil t)
230 (if message-signature-insert-empty-line
232 (goto-char (point-max))))
234 (let ((from (plist-get original-headers :From))
235 (date (plist-get original-headers :Date))
238 ;; notmuch-mua-cite-function constructs a citation line based
239 ;; on the From and Date headers of the original message, which
240 ;; are assumed to be in the buffer.
241 (insert "From: " from "\n")
242 (insert "Date: " date "\n\n")
244 (insert (with-temp-buffer
246 ;; Don't attempt to clean up messages, excerpt
247 ;; citations, etc. in the original message before
249 ((notmuch-show-insert-text/plain-hook nil)
250 ;; Don't omit long parts.
251 (notmuch-show-max-text-part-size 0)
252 ;; Insert headers for parts as appropriate for replying.
253 (notmuch-show-insert-header-p-function notmuch-mua-reply-insert-header-p-function)
254 ;; Don't indent multipart sub-parts.
255 (notmuch-show-indent-multipart nil))
256 ;; We don't want sigstatus buttons (an information leak and usually wrong anyway).
257 (letf (((symbol-function 'notmuch-crypto-insert-sigstatus-button) #'ignore)
258 ((symbol-function 'notmuch-crypto-insert-encstatus-button) #'ignore))
259 (notmuch-show-insert-body original (plist-get original :body) 0)
260 (buffer-substring-no-properties (point-min) (point-max))))))
264 ;; Quote the original message according to the user's configured style.
265 (funcall notmuch-mua-cite-function)))
267 ;; Crypto processing based crypto content of the original message
269 (notmuch-mua-reply-crypto (plist-get original :body))))
271 ;; Push mark right before signature, if any.
272 (message-goto-signature)
278 (set-buffer-modified-p nil))
280 (define-derived-mode notmuch-message-mode message-mode "Message[Notmuch]"
281 "Notmuch message composition mode. Mostly like `message-mode'"
282 (notmuch-address-setup))
284 (put 'notmuch-message-mode 'flyspell-mode-predicate 'mail-mode-flyspell-verify)
286 (define-key notmuch-message-mode-map (kbd "C-c C-c") #'notmuch-mua-send-and-exit)
287 (define-key notmuch-message-mode-map (kbd "C-c C-s") #'notmuch-mua-send)
289 (defun notmuch-mua-pop-to-buffer (name switch-function)
290 "Pop to buffer NAME, and warn if it already exists and is
291 modified. This function is notmuch addaptation of
292 `message-pop-to-buffer'."
293 (let ((buffer (get-buffer name)))
295 (buffer-name buffer))
296 (let ((window (get-buffer-window buffer 0)))
298 ;; Raise the frame already displaying the message buffer.
300 (gnus-select-frame-set-input-focus (window-frame window))
301 (select-window window))
302 (funcall switch-function buffer)
304 (when (and (buffer-modified-p)
307 "Message already being composed; erase? ")
309 (error "Message being composed")))
310 (funcall switch-function name)
313 (notmuch-message-mode)))
315 (defun notmuch-mua-mail (&optional to subject other-headers continue
316 switch-function yank-action send-actions
317 return-action &rest ignored)
318 "Invoke the notmuch mail composition window."
321 (when notmuch-mua-user-agent-function
322 (let ((user-agent (funcall notmuch-mua-user-agent-function)))
323 (when (not (string= "" user-agent))
324 (push (cons 'User-Agent user-agent) other-headers))))
326 (unless (assq 'From other-headers)
327 (push (cons 'From (message-make-from
328 (notmuch-user-name) (notmuch-user-primary-email))) other-headers))
330 (notmuch-mua-pop-to-buffer (message-buffer-name "mail" to)
331 (or switch-function (notmuch-mua-get-switch-function)))
334 ;; The following is copied from `message-mail'
335 `((To . ,(or to "")) (Subject . ,(or subject "")))
336 ;; C-h f compose-mail says that headers should be specified as
337 ;; (string . value); however all the rest of message expects
338 ;; headers to be symbols, not strings (eg message-header-format-alist).
339 ;; https://lists.gnu.org/archive/html/emacs-devel/2011-01/msg00337.html
340 ;; We need to convert any string input, eg from rmail-start-mail.
341 (dolist (h other-headers other-headers)
342 (if (stringp (car h)) (setcar h (intern (capitalize (car h))))))))
343 (args (list yank-action send-actions))
344 ;; Cause `message-setup-1' to do things relevant for mail,
345 ;; such as observe `message-default-mail-headers'.
346 (message-this-is-mail t))
347 ;; message-setup-1 in Emacs 23 does not accept return-action
348 ;; argument. Pass it only if it is supplied by the caller. This
349 ;; will never be the case when we're called by `compose-mail' in
351 (when return-action (nconc args '(return-action)))
352 (apply 'message-setup-1 headers args))
353 (notmuch-fcc-header-setup)
354 (message-sort-headers)
355 (message-hide-headers)
356 (set-buffer-modified-p nil)
357 (notmuch-mua-maybe-set-window-dedicated)
361 (defcustom notmuch-identities nil
362 "Identities that can be used as the From: address when composing a new message.
364 If this variable is left unset, then a list will be constructed from the
365 name and addresses configured in the notmuch configuration file."
366 :type '(repeat string)
367 :group 'notmuch-send)
369 (defcustom notmuch-always-prompt-for-sender nil
370 "Always prompt for the From: address when composing or forwarding a message.
372 This is not taken into account when replying to a message, because in that case
373 the From: header is already filled in by notmuch."
375 :group 'notmuch-send)
377 (defvar notmuch-mua-sender-history nil)
379 ;; Workaround: Running `ido-completing-read' in emacs 23.1, 23.2 and 23.3
380 ;; without some explicit initialization fill freeze the operation.
381 ;; Hence, we advice `ido-completing-read' to ensure required initialization
383 (if (and (= emacs-major-version 23) (< emacs-minor-version 4))
384 (defadvice ido-completing-read (before notmuch-ido-mode-init activate)
385 (ido-init-completion-maps)
386 (add-hook 'minibuffer-setup-hook 'ido-minibuffer-setup)
387 (add-hook 'choose-completion-string-functions
388 'ido-choose-completion-string)
389 (ad-disable-advice 'ido-completing-read 'before 'notmuch-ido-mode-init)
390 (ad-activate 'ido-completing-read)))
392 (defun notmuch-mua-prompt-for-sender ()
393 "Prompt for a sender from the user's configured identities."
394 (if notmuch-identities
395 (ido-completing-read "Send mail from: " notmuch-identities
396 nil nil nil 'notmuch-mua-sender-history
397 (car notmuch-identities))
398 (let* ((name (notmuch-user-name))
399 (addrs (cons (notmuch-user-primary-email)
400 (notmuch-user-other-email)))
402 (ido-completing-read (concat "Sender address for " name ": ") addrs
403 nil nil nil 'notmuch-mua-sender-history
405 (message-make-from name address))))
407 (put 'notmuch-mua-new-mail 'notmuch-prefix-doc "... and prompt for sender")
408 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
411 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
412 the From: address first."
415 (when (or prompt-for-sender notmuch-always-prompt-for-sender)
416 (list (cons 'From (notmuch-mua-prompt-for-sender))))))
417 (notmuch-mua-mail nil nil other-headers nil (notmuch-mua-get-switch-function))))
419 (defun notmuch-mua-new-forward-messages (messages &optional prompt-for-sender)
420 "Compose a new message forwarding MESSAGES.
422 If PROMPT-FOR-SENDER is non-nil, the user will be prompteed for
424 (let* ((other-headers
425 (when (or prompt-for-sender notmuch-always-prompt-for-sender)
426 (list (cons 'From (notmuch-mua-prompt-for-sender)))))
427 forward-subject) ;; Comes from the first message and is
430 ;; Generate the template for the outgoing message.
431 (notmuch-mua-mail nil "" other-headers nil (notmuch-mua-get-switch-function))
434 ;; Insert all of the forwarded messages.
436 (let ((temp-buffer (get-buffer-create
437 (concat "*notmuch-fwd-raw-" id "*"))))
438 ;; Get the raw version of this message in the buffer.
439 (with-current-buffer temp-buffer
441 (let ((coding-system-for-read 'no-conversion))
442 (call-process notmuch-command nil t nil "show" "--format=raw" id))
443 ;; Because we process the messages in reverse order,
444 ;; always generate a forwarded subject, then use the
445 ;; last (i.e. first) one.
446 (setq forward-subject (message-make-forward-subject)))
447 ;; Make a copy ready to be forwarded in the
448 ;; composition buffer.
449 (message-forward-make-body temp-buffer)
450 ;; Kill the temporary buffer.
451 (kill-buffer temp-buffer)))
452 ;; `message-forward-make-body' always puts the message at
453 ;; the top, so do them in reverse order.
456 ;; Add in the appropriate subject.
458 (message-narrow-to-headers)
459 (message-remove-header "Subject")
460 (message-add-header (concat "Subject: " forward-subject)))
462 ;; `message-forward-make-body' shows the User-agent header. Hide
464 (message-hide-headers)
465 (set-buffer-modified-p nil))))
467 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
468 "Compose a reply to the message identified by QUERY-STRING.
470 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
471 the From: address first. If REPLY-ALL is non-nil, the message
472 will be addressed to all recipients of the source message."
474 ;; In current emacs (24.3) select-active-regions is set to t by
475 ;; default. The reply insertion code sets the region to the quoted
476 ;; message to make it easy to delete (kill-region or C-w). These two
477 ;; things combine to put the quoted message in the primary selection.
479 ;; This is not what the user wanted and is a privacy risk (accidental
480 ;; pasting of the quoted message). We can avoid some of the problems
481 ;; by let-binding select-active-regions to nil. This fixes if the
482 ;; primary selection was previously in a non-emacs window but not if
483 ;; it was in an emacs window. To avoid the problem in the latter case
484 ;; we deactivate mark.
487 (when prompt-for-sender
488 (notmuch-mua-prompt-for-sender)))
489 (select-active-regions nil))
490 (notmuch-mua-reply query-string sender reply-all)
493 (defun notmuch-mua-send-and-exit (&optional arg)
495 (letf (((symbol-function 'message-do-fcc) #'notmuch-maildir-message-do-fcc))
496 (message-send-and-exit arg)))
498 (defun notmuch-mua-send (&optional arg)
500 (letf (((symbol-function 'message-do-fcc) #'notmuch-maildir-message-do-fcc))
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