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 'notmuch-lib)
25 (require 'notmuch-address)
29 (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
30 "Hook run before sending messages."
34 (defcustom notmuch-mua-user-agent-function 'notmuch-mua-user-agent-full
35 "Function used to generate a `User-Agent:' string. If this is
36 `nil' then no `User-Agent:' will be generated."
38 :type '(choice (const :tag "No user agent string" nil)
39 (const :tag "Full" notmuch-mua-user-agent-full)
40 (const :tag "Notmuch" notmuch-mua-user-agent-notmuch)
41 (const :tag "Emacs" notmuch-mua-user-agent-emacs)
42 (function :tag "Custom user agent function"
43 :value notmuch-mua-user-agent-full)))
45 (defcustom notmuch-mua-hidden-headers '("^User-Agent:")
46 "Headers that are added to the `message-mode' hidden headers
49 :type '(repeat string))
53 (defun notmuch-mua-user-agent-full ()
54 "Generate a `User-Agent:' string suitable for notmuch."
55 (concat (notmuch-mua-user-agent-notmuch)
57 (notmuch-mua-user-agent-emacs)))
59 (defun notmuch-mua-user-agent-notmuch ()
60 "Generate a `User-Agent:' string suitable for notmuch."
61 (concat "Notmuch/" (notmuch-version) " (http://notmuchmail.org)"))
63 (defun notmuch-mua-user-agent-emacs ()
64 "Generate a `User-Agent:' string suitable for notmuch."
65 (concat "Emacs/" emacs-version " (" system-configuration ")"))
67 (defun notmuch-mua-add-more-hidden-headers ()
68 "Add some headers to the list that are hidden by default."
69 (mapc (lambda (header)
70 (when (not (member header message-hidden-headers))
71 (push header message-hidden-headers)))
72 notmuch-mua-hidden-headers))
74 (defun notmuch-mua-reply (query-string &optional sender)
78 (if notmuch-show-process-crypto
79 (setq args (append args '("--decrypt"))))
80 (setq args (append args (list query-string)))
81 ;; This make assumptions about the output of `notmuch reply', but
82 ;; really only that the headers come first followed by a blank
83 ;; line and then the body.
85 (apply 'call-process (append (list notmuch-command nil (list t t) nil) args))
86 (goto-char (point-min))
87 (if (re-search-forward "^$" nil t)
90 (narrow-to-region (point-min) (point))
91 (goto-char (point-min))
92 (setq headers (mail-header-extract)))))
94 (setq body (buffer-substring (point) (point-max))))
95 ;; If sender is non-nil, set the From: header to its value.
97 (mail-header-set 'from sender headers))
99 ;; Overlay the composition window on that being used to read
100 ;; the original message.
101 ((same-window-regexps '("\\*mail .*")))
102 (notmuch-mua-mail (mail-header 'to headers)
103 (mail-header 'subject headers)
104 (message-headers-to-generate headers t '(to subject))))
105 ;; insert the message body - but put it in front of the signature
107 (goto-char (point-max))
108 (if (re-search-backward message-signature-separator nil t)
110 (goto-char (point-max)))
112 (set-buffer-modified-p nil)
116 (defun notmuch-mua-forward-message ()
119 (when notmuch-mua-user-agent-function
120 (let ((user-agent (funcall notmuch-mua-user-agent-function)))
121 (when (not (string= "" user-agent))
122 (message-add-header (format "User-Agent: %s" user-agent)))))
123 (message-sort-headers)
124 (message-hide-headers)
125 (set-buffer-modified-p nil)
129 (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)
130 "Invoke the notmuch mail composition window.
132 OTHER-ARGS are passed through to `message-mail'."
135 (when notmuch-mua-user-agent-function
136 (let ((user-agent (funcall notmuch-mua-user-agent-function)))
137 (when (not (string= "" user-agent))
138 (push (cons "User-Agent" user-agent) other-headers))))
140 (unless (mail-header 'from other-headers)
141 (push (cons "From" (concat
142 (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
144 (apply #'message-mail to subject other-headers other-args)
145 (message-sort-headers)
146 (message-hide-headers)
147 (set-buffer-modified-p nil)
151 (defcustom notmuch-identities nil
152 "Identities that can be used as the From: address when composing a new message.
154 If this variable is left unset, then a list will be constructed from the
155 name and addresses configured in the notmuch configuration file."
157 :type '(repeat string))
159 (defcustom notmuch-always-prompt-for-sender nil
160 "Always prompt for the From: address when composing or forwarding a message.
162 This is not taken into account when replying to a message, because in that case
163 the From: header is already filled in by notmuch."
167 (defvar notmuch-mua-sender-history nil)
169 (defun notmuch-mua-prompt-for-sender ()
171 (let (name addresses one-name-only)
172 ;; If notmuch-identities is non-nil, check if there is a fixed user name.
173 (if notmuch-identities
174 (let ((components (mapcar 'mail-extract-address-components notmuch-identities)))
175 (setq name (caar components)
176 addresses (mapcar 'cadr components)
179 (mapcar (lambda (identity)
180 (string-equal name (car identity)))
182 ;; If notmuch-identities is nil, use values from the notmuch configuration file.
183 (setq name (notmuch-user-name)
184 addresses (cons (notmuch-user-primary-email) (notmuch-user-other-email))
186 ;; Now prompt the user, either for an email address only or for a full identity.
189 (ido-completing-read (concat "Sender address for " name ": ") addresses
190 nil nil nil 'notmuch-mua-sender-history (car addresses))))
191 (concat name " <" address ">"))
192 (ido-completing-read "Send mail From: " notmuch-identities
193 nil nil nil 'notmuch-mua-sender-history (car notmuch-identities)))))
195 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
196 "Invoke the notmuch mail composition window.
198 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
199 the From: address first."
202 (when (or prompt-for-sender notmuch-always-prompt-for-sender)
203 (list (cons 'from (notmuch-mua-prompt-for-sender))))))
204 (notmuch-mua-mail nil nil other-headers)))
206 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
207 "Invoke the notmuch message forwarding window.
209 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
210 the From: address first."
212 (if (or prompt-for-sender notmuch-always-prompt-for-sender)
213 (let* ((sender (notmuch-mua-prompt-for-sender))
214 (address-components (mail-extract-address-components sender))
215 (user-full-name (car address-components))
216 (user-mail-address (cadr address-components)))
217 (notmuch-mua-forward-message))
218 (notmuch-mua-forward-message)))
220 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender)
221 "Invoke the notmuch reply window."
224 (when prompt-for-sender
225 (notmuch-mua-prompt-for-sender))))
226 (notmuch-mua-reply query-string sender)))
228 (defun notmuch-mua-send-and-exit (&optional arg)
230 (message-send-and-exit arg))
232 (defun notmuch-mua-kill-buffer ()
234 (message-kill-buffer))
236 (defun notmuch-mua-message-send-hook ()
237 "The default function used for `notmuch-mua-send-hook', this
238 simply runs the corresponding `message-mode' hook functions."
239 (run-hooks 'message-send-hook))
243 (define-mail-user-agent 'notmuch-user-agent
244 'notmuch-mua-mail 'notmuch-mua-send-and-exit
245 'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
247 ;; Add some more headers to the list that `message-mode' hides when
248 ;; composing a message.
249 (notmuch-mua-add-more-hidden-headers)
253 (provide 'notmuch-mua)