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