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