]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-mua.el
d8ab822a2697db2e81aca8591e385d81adc5fa59
[notmuch] / emacs / notmuch-mua.el
1 ;; notmuch-mua.el --- emacs style mail-user-agent
2 ;;
3 ;; Copyright © David Edmondson
4 ;;
5 ;; This file is part of Notmuch.
6 ;;
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.
11 ;;
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.
16 ;;
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/>.
19 ;;
20 ;; Authors: David Edmondson <dme@dme.org>
21
22 (require 'message)
23
24 (require 'notmuch-lib)
25 (require 'notmuch-address)
26
27 ;;
28
29 (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
30   "Hook run before sending messages."
31   :group 'notmuch
32   :type 'hook)
33
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."
37   :group 'notmuch
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)))
44
45 (defcustom notmuch-mua-hidden-headers '("^User-Agent:")
46   "Headers that are added to the `message-mode' hidden headers
47 list."
48   :group 'notmuch
49   :type '(repeat string))
50
51 ;;
52
53 (defun notmuch-mua-user-agent-full ()
54   "Generate a `User-Agent:' string suitable for notmuch."
55   (concat (notmuch-mua-user-agent-notmuch)
56           " "
57           (notmuch-mua-user-agent-emacs)))
58
59 (defun notmuch-mua-user-agent-notmuch ()
60   "Generate a `User-Agent:' string suitable for notmuch."
61   (concat "Notmuch/" (notmuch-version) " (http://notmuchmail.org)"))
62
63 (defun notmuch-mua-user-agent-emacs ()
64   "Generate a `User-Agent:' string suitable for notmuch."
65   (concat "Emacs/" emacs-version " (" system-configuration ")"))
66
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))
73
74 (defun notmuch-mua-reply (query-string &optional sender reply-all)
75   (let (headers
76         body
77         (args '("reply")))
78     (if notmuch-show-process-crypto
79         (setq args (append args '("--decrypt"))))
80     (if reply-all
81         (setq args (append args '("--reply-to=all")))
82       (setq args (append args '("--reply-to=sender"))))
83     (setq args (append args (list query-string)))
84     ;; This make assumptions about the output of `notmuch reply', but
85     ;; really only that the headers come first followed by a blank
86     ;; line and then the body.
87     (with-temp-buffer
88       (apply 'call-process (append (list notmuch-command nil (list t t) nil) args))
89       (goto-char (point-min))
90       (if (re-search-forward "^$" nil t)
91           (save-excursion
92             (save-restriction
93               (narrow-to-region (point-min) (point))
94               (goto-char (point-min))
95               (setq headers (mail-header-extract)))))
96       (forward-line 1)
97       (setq body (buffer-substring (point) (point-max))))
98     ;; If sender is non-nil, set the From: header to its value.
99     (when sender
100       (mail-header-set 'from sender headers))
101     (let
102         ;; Overlay the composition window on that being used to read
103         ;; the original message.
104         ((same-window-regexps '("\\*mail .*")))
105       (notmuch-mua-mail (mail-header 'to headers)
106                         (mail-header 'subject headers)
107                         (message-headers-to-generate headers t '(to subject))))
108     ;; insert the message body - but put it in front of the signature
109     ;; if one is present
110     (goto-char (point-max))
111     (if (re-search-backward message-signature-separator nil t)
112           (forward-line -1)
113       (goto-char (point-max)))
114     (insert body)
115     (push-mark))
116   (set-buffer-modified-p nil)
117
118   (message-goto-body))
119
120 (defun notmuch-mua-forward-message ()
121   (message-forward)
122
123   (when notmuch-mua-user-agent-function
124     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
125       (when (not (string= "" user-agent))
126         (message-add-header (format "User-Agent: %s" user-agent)))))
127   (message-sort-headers)
128   (message-hide-headers)
129   (set-buffer-modified-p nil)
130
131   (message-goto-to))
132
133 (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)
134   "Invoke the notmuch mail composition window.
135
136 OTHER-ARGS are passed through to `message-mail'."
137   (interactive)
138
139   (when notmuch-mua-user-agent-function
140     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
141       (when (not (string= "" user-agent))
142         (push (cons "User-Agent" user-agent) other-headers))))
143
144   (unless (mail-header 'from other-headers)
145     (push (cons "From" (concat
146                         (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
147
148   (apply #'message-mail to subject other-headers other-args)
149   (message-sort-headers)
150   (message-hide-headers)
151   (set-buffer-modified-p nil)
152
153   (message-goto-to))
154
155 (defcustom notmuch-identities nil
156   "Identities that can be used as the From: address when composing a new message.
157
158 If this variable is left unset, then a list will be constructed from the
159 name and addresses configured in the notmuch configuration file."
160   :group 'notmuch
161   :type '(repeat string))
162
163 (defcustom notmuch-always-prompt-for-sender nil
164   "Always prompt for the From: address when composing or forwarding a message.
165
166 This is not taken into account when replying to a message, because in that case
167 the From: header is already filled in by notmuch."
168   :group 'notmuch
169   :type 'boolean)
170
171 (defvar notmuch-mua-sender-history nil)
172
173 (defun notmuch-mua-prompt-for-sender ()
174   (interactive)
175   (let (name addresses one-name-only)
176     ;; If notmuch-identities is non-nil, check if there is a fixed user name.
177     (if notmuch-identities
178         (let ((components (mapcar 'mail-extract-address-components notmuch-identities)))
179           (setq name          (caar components)
180                 addresses     (mapcar 'cadr components)
181                 one-name-only (eval
182                                (cons 'and
183                                      (mapcar (lambda (identity)
184                                                (string-equal name (car identity)))
185                                              components)))))
186       ;; If notmuch-identities is nil, use values from the notmuch configuration file.
187       (setq name          (notmuch-user-name)
188             addresses     (cons (notmuch-user-primary-email) (notmuch-user-other-email))
189             one-name-only t))
190     ;; Now prompt the user, either for an email address only or for a full identity.
191     (if one-name-only
192         (let ((address
193                (ido-completing-read (concat "Sender address for " name ": ") addresses
194                                     nil nil nil 'notmuch-mua-sender-history (car addresses))))
195           (concat name " <" address ">"))
196       (ido-completing-read "Send mail From: " notmuch-identities
197                            nil nil nil 'notmuch-mua-sender-history (car notmuch-identities)))))
198
199 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
200   "Invoke the notmuch mail composition window.
201
202 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
203 the From: address first."
204   (interactive "P")
205   (let ((other-headers
206          (when (or prompt-for-sender notmuch-always-prompt-for-sender)
207            (list (cons 'from (notmuch-mua-prompt-for-sender))))))
208     (notmuch-mua-mail nil nil other-headers)))
209
210 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
211   "Invoke the notmuch message forwarding window.
212
213 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
214 the From: address first."
215   (interactive "P")
216   (if (or prompt-for-sender notmuch-always-prompt-for-sender)
217       (let* ((sender (notmuch-mua-prompt-for-sender))
218              (address-components (mail-extract-address-components sender))
219              (user-full-name (car address-components))
220              (user-mail-address (cadr address-components)))
221         (notmuch-mua-forward-message))
222     (notmuch-mua-forward-message)))
223
224 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
225   "Invoke the notmuch reply window."
226   (interactive "P")
227   (let ((sender
228          (when prompt-for-sender
229            (notmuch-mua-prompt-for-sender))))
230     (notmuch-mua-reply query-string sender reply-all)))
231
232 (defun notmuch-mua-send-and-exit (&optional arg)
233   (interactive "P")
234   (message-send-and-exit arg))
235
236 (defun notmuch-mua-kill-buffer ()
237   (interactive)
238   (message-kill-buffer))
239
240 (defun notmuch-mua-message-send-hook ()
241   "The default function used for `notmuch-mua-send-hook', this
242 simply runs the corresponding `message-mode' hook functions."
243   (run-hooks 'message-send-hook))
244
245 ;;
246
247 (define-mail-user-agent 'notmuch-user-agent
248   'notmuch-mua-mail 'notmuch-mua-send-and-exit
249   'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
250
251 ;; Add some more headers to the list that `message-mode' hides when
252 ;; composing a message.
253 (notmuch-mua-add-more-hidden-headers)
254
255 ;;
256
257 (provide 'notmuch-mua)