]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-mua.el
emacs: add stash support for git send-email command line
[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 (require 'mm-view)
24 (require 'format-spec)
25
26 (require 'notmuch-lib)
27 (require 'notmuch-address)
28
29 (eval-when-compile (require 'cl))
30
31 (declare-function notmuch-show-insert-bodypart "notmuch-show" (msg part depth &optional hide))
32
33 ;;
34
35 (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
36   "Hook run before sending messages."
37   :type 'hook
38   :group 'notmuch-send
39   :group 'notmuch-hooks)
40
41 (defcustom notmuch-mua-compose-in 'current-window
42   (concat
43    "Where to create the mail buffer used to compose a new message.
44 Possible values are `current-window' (default), `new-window' and
45 `new-frame'. If set to `current-window', the mail buffer will be
46 displayed in the current window, so the old buffer will be
47 restored when the mail buffer is killed. If set to `new-window'
48 or `new-frame', the mail buffer will be displayed in a new
49 window/frame that will be destroyed when the buffer is killed.
50 You may want to customize `message-kill-buffer-on-exit'
51 accordingly."
52    (when (< emacs-major-version 24)
53            " Due to a known bug in Emacs 23, you should not set
54 this to `new-window' if `message-kill-buffer-on-exit' is
55 disabled: this would result in an incorrect behavior."))
56   :group 'notmuch-send
57   :type '(choice (const :tag "Compose in the current window" current-window)
58                  (const :tag "Compose mail in a new window"  new-window)
59                  (const :tag "Compose mail in a new frame"   new-frame)))
60
61 (defcustom notmuch-mua-user-agent-function 'notmuch-mua-user-agent-full
62   "Function used to generate a `User-Agent:' string. If this is
63 `nil' then no `User-Agent:' will be generated."
64   :type '(choice (const :tag "No user agent string" nil)
65                  (const :tag "Full" notmuch-mua-user-agent-full)
66                  (const :tag "Notmuch" notmuch-mua-user-agent-notmuch)
67                  (const :tag "Emacs" notmuch-mua-user-agent-emacs)
68                  (function :tag "Custom user agent function"
69                            :value notmuch-mua-user-agent-full))
70   :group 'notmuch-send)
71
72 (defcustom notmuch-mua-hidden-headers '("^User-Agent:")
73   "Headers that are added to the `message-mode' hidden headers
74 list."
75   :type '(repeat string)
76   :group 'notmuch-send)
77
78 (defgroup notmuch-reply nil
79   "Replying to messages in notmuch"
80   :group 'notmuch)
81
82 (defcustom notmuch-mua-cite-function 'message-cite-original
83   "*Function for citing an original message.
84 Predefined functions include `message-cite-original' and
85 `message-cite-original-without-signature'.
86 Note that these functions use `mail-citation-hook' if that is non-nil."
87   :type '(radio (function-item message-cite-original)
88                 (function-item message-cite-original-without-signature)
89                 (function-item sc-cite-original)
90                 (function :tag "Other"))
91   :link '(custom-manual "(message)Insertion Variables")
92   :group 'notmuch-reply)
93
94 ;;
95
96 (defun notmuch-mua-get-switch-function ()
97   "Get a switch function according to `notmuch-mua-compose-in'."
98   (cond ((eq notmuch-mua-compose-in 'current-window)
99          'switch-to-buffer)
100         ((eq notmuch-mua-compose-in 'new-window)
101          'switch-to-buffer-other-window)
102         ((eq notmuch-mua-compose-in 'new-frame)
103          'switch-to-buffer-other-frame)
104         (t (error "Invalid value for `notmuch-mua-compose-in'"))))
105
106 (defun notmuch-mua-maybe-set-window-dedicated ()
107   "Set the selected window as dedicated according to
108 `notmuch-mua-compose-in'."
109   (when (or (eq notmuch-mua-compose-in 'new-frame)
110             (eq notmuch-mua-compose-in 'new-window))
111     (set-window-dedicated-p (selected-window) t)))
112
113 (defun notmuch-mua-user-agent-full ()
114   "Generate a `User-Agent:' string suitable for notmuch."
115   (concat (notmuch-mua-user-agent-notmuch)
116           " "
117           (notmuch-mua-user-agent-emacs)))
118
119 (defun notmuch-mua-user-agent-notmuch ()
120   "Generate a `User-Agent:' string suitable for notmuch."
121   (concat "Notmuch/" (notmuch-version) " (http://notmuchmail.org)"))
122
123 (defun notmuch-mua-user-agent-emacs ()
124   "Generate a `User-Agent:' string suitable for notmuch."
125   (concat "Emacs/" emacs-version " (" system-configuration ")"))
126
127 (defun notmuch-mua-add-more-hidden-headers ()
128   "Add some headers to the list that are hidden by default."
129   (mapc (lambda (header)
130           (when (not (member header message-hidden-headers))
131             (push header message-hidden-headers)))
132         notmuch-mua-hidden-headers))
133
134 (defun notmuch-mua-reply-crypto (parts)
135   "Add mml sign-encrypt flag if any part of original message is encrypted."
136   (loop for part in parts
137         if (notmuch-match-content-type (plist-get part :content-type) "multipart/encrypted")
138           do (mml-secure-message-sign-encrypt)
139         else if (notmuch-match-content-type (plist-get part :content-type) "multipart/*")
140           do (notmuch-mua-reply-crypto (plist-get part :content))))
141
142 (defun notmuch-mua-get-quotable-parts (parts)
143   (loop for part in parts
144         if (notmuch-match-content-type (plist-get part :content-type) "multipart/alternative")
145           collect (let* ((subparts (plist-get part :content))
146                         (types (mapcar (lambda (part) (plist-get part :content-type)) subparts))
147                         (chosen-type (car (notmuch-multipart/alternative-choose types))))
148                    (loop for part in (reverse subparts)
149                          if (notmuch-match-content-type (plist-get part :content-type) chosen-type)
150                          return part))
151         else if (notmuch-match-content-type (plist-get part :content-type) "multipart/*")
152           append (notmuch-mua-get-quotable-parts (plist-get part :content))
153         else if (notmuch-match-content-type (plist-get part :content-type) "text/*")
154           collect part))
155
156 (defun notmuch-mua-insert-quotable-part (message part)
157   ;; We don't want text properties leaking from the show renderer into
158   ;; the reply so we use a temp buffer. Also we don't want hooks, such
159   ;; as notmuch-wash-*, to be run on the quotable part so we set
160   ;; notmuch-show-insert-text/plain-hook to nil.
161   (insert (with-temp-buffer
162             (let ((notmuch-show-insert-text/plain-hook nil))
163               ;; Show the part but do not add buttons.
164               (notmuch-show-insert-bodypart message part 0 'no-buttons))
165             (buffer-substring-no-properties (point-min) (point-max)))))
166
167 ;; There is a bug in emacs 23's message.el that results in a newline
168 ;; not being inserted after the References header, so the next header
169 ;; is concatenated to the end of it. This function fixes the problem,
170 ;; while guarding against the possibility that some current or future
171 ;; version of emacs has the bug fixed.
172 (defun notmuch-mua-insert-references (original-func header references)
173   (funcall original-func header references)
174   (unless (bolp) (insert "\n")))
175
176 (defun notmuch-mua-reply (query-string &optional sender reply-all)
177   (let ((args '("reply" "--format=sexp" "--format-version=1"))
178         (process-crypto notmuch-show-process-crypto)
179         reply
180         original)
181     (when process-crypto
182       (setq args (append args '("--decrypt"))))
183
184     (if reply-all
185         (setq args (append args '("--reply-to=all")))
186       (setq args (append args '("--reply-to=sender"))))
187     (setq args (append args (list query-string)))
188
189     ;; Get the reply object as SEXP, and parse it into an elisp object.
190     (setq reply (apply #'notmuch-call-notmuch-sexp args))
191
192     ;; Extract the original message to simplify the following code.
193     (setq original (plist-get reply :original))
194
195     ;; Extract the headers of both the reply and the original message.
196     (let* ((original-headers (plist-get original :headers))
197            (reply-headers (plist-get reply :reply-headers)))
198
199       ;; If sender is non-nil, set the From: header to its value.
200       (when sender
201         (plist-put reply-headers :From sender))
202       (let
203           ;; Overlay the composition window on that being used to read
204           ;; the original message.
205           ((same-window-regexps '("\\*mail .*")))
206
207         ;; We modify message-header-format-alist to get around a bug in message.el.
208         ;; See the comment above on notmuch-mua-insert-references.
209         (let ((message-header-format-alist
210                (loop for pair in message-header-format-alist
211                      if (eq (car pair) 'References)
212                      collect (cons 'References
213                                    (apply-partially
214                                     'notmuch-mua-insert-references
215                                     (cdr pair)))
216                      else
217                      collect pair)))
218           (notmuch-mua-mail (plist-get reply-headers :To)
219                             (plist-get reply-headers :Subject)
220                             (notmuch-headers-plist-to-alist reply-headers)
221                             nil (notmuch-mua-get-switch-function))))
222
223       ;; Insert the message body - but put it in front of the signature
224       ;; if one is present, and after any other content
225       ;; message*setup-hooks may have added to the message body already.
226       (save-restriction
227         (message-goto-body)
228         (narrow-to-region (point) (point-max))
229         (goto-char (point-max))
230         (if (re-search-backward message-signature-separator nil t)
231             (if message-signature-insert-empty-line
232                 (forward-line -1))
233           (goto-char (point-max))))
234
235       (let ((from (plist-get original-headers :From))
236             (date (plist-get original-headers :Date))
237             (start (point)))
238
239         ;; notmuch-mua-cite-function constructs a citation line based
240         ;; on the From and Date headers of the original message, which
241         ;; are assumed to be in the buffer.
242         (insert "From: " from "\n")
243         (insert "Date: " date "\n\n")
244
245         ;; Get the parts of the original message that should be quoted; this includes
246         ;; all the text parts, except the non-preferred ones in a multipart/alternative.
247         (let ((quotable-parts (notmuch-mua-get-quotable-parts (plist-get original :body))))
248           (mapc (apply-partially 'notmuch-mua-insert-quotable-part original) quotable-parts))
249
250         (set-mark (point))
251         (goto-char start)
252         ;; Quote the original message according to the user's configured style.
253         (funcall notmuch-mua-cite-function)))
254
255     ;; Crypto processing based crypto content of the original message
256     (when process-crypto
257       (notmuch-mua-reply-crypto (plist-get original :body))))
258
259   ;; Push mark right before signature, if any.
260   (message-goto-signature)
261   (unless (eobp)
262     (end-of-line -1))
263   (push-mark)
264
265   (message-goto-body)
266   (set-buffer-modified-p nil))
267
268 (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)
269   "Invoke the notmuch mail composition window.
270
271 OTHER-ARGS are passed through to `message-mail'."
272   (interactive)
273
274   (when notmuch-mua-user-agent-function
275     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
276       (when (not (string= "" user-agent))
277         (push (cons 'User-Agent user-agent) other-headers))))
278
279   (unless (assq 'From other-headers)
280     (push (cons 'From (concat
281                        (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
282
283   (apply #'message-mail to subject other-headers other-args)
284   (message-sort-headers)
285   (message-hide-headers)
286   (set-buffer-modified-p nil)
287   (notmuch-mua-maybe-set-window-dedicated)
288
289   (message-goto-to))
290
291 (defcustom notmuch-identities nil
292   "Identities that can be used as the From: address when composing a new message.
293
294 If this variable is left unset, then a list will be constructed from the
295 name and addresses configured in the notmuch configuration file."
296   :type '(repeat string)
297   :group 'notmuch-send)
298
299 (defcustom notmuch-always-prompt-for-sender nil
300   "Always prompt for the From: address when composing or forwarding a message.
301
302 This is not taken into account when replying to a message, because in that case
303 the From: header is already filled in by notmuch."
304   :type 'boolean
305   :group 'notmuch-send)
306
307 (defvar notmuch-mua-sender-history nil)
308
309 ;; Workaround: Running `ido-completing-read' in emacs 23.1, 23.2 and 23.3
310 ;; without some explicit initialization fill freeze the operation.
311 ;; Hence, we advice `ido-completing-read' to ensure required initialization
312 ;; is done.
313 (if (and (= emacs-major-version 23) (< emacs-minor-version 4))
314     (defadvice ido-completing-read (before notmuch-ido-mode-init activate)
315       (ido-init-completion-maps)
316       (add-hook 'minibuffer-setup-hook 'ido-minibuffer-setup)
317       (add-hook 'choose-completion-string-functions
318                 'ido-choose-completion-string)
319       (ad-disable-advice 'ido-completing-read 'before 'notmuch-ido-mode-init)
320       (ad-activate 'ido-completing-read)))
321
322 (defun notmuch-mua-prompt-for-sender ()
323   "Prompt for a sender from the user's configured identities."
324   (if notmuch-identities
325       (ido-completing-read "Send mail from: " notmuch-identities
326                            nil nil nil 'notmuch-mua-sender-history
327                            (car notmuch-identities))
328     (let* ((name (notmuch-user-name))
329            (addrs (cons (notmuch-user-primary-email)
330                         (notmuch-user-other-email)))
331            (address
332             (ido-completing-read (concat "Sender address for " name ": ") addrs
333                                  nil nil nil 'notmuch-mua-sender-history
334                                  (car addrs))))
335       (concat name " <" address ">"))))
336
337 (put 'notmuch-mua-new-mail 'notmuch-prefix-doc "... and prompt for sender")
338 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
339   "Compose new mail.
340
341 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
342 the From: address first."
343   (interactive "P")
344   (let ((other-headers
345          (when (or prompt-for-sender notmuch-always-prompt-for-sender)
346            (list (cons 'From (notmuch-mua-prompt-for-sender))))))
347     (notmuch-mua-mail nil nil other-headers nil (notmuch-mua-get-switch-function))))
348
349 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
350   "Invoke the notmuch message forwarding window.
351
352 The current buffer must contain an RFC2822 message to forward.
353
354 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
355 the From: address first."
356   (let* ((cur (current-buffer))
357          (message-forward-decoded-p nil)
358          (subject (message-make-forward-subject))
359          (other-headers
360           (when (or prompt-for-sender notmuch-always-prompt-for-sender)
361             (list (cons 'From (notmuch-mua-prompt-for-sender))))))
362     (notmuch-mua-mail nil subject other-headers nil (notmuch-mua-get-switch-function))
363     (message-forward-make-body cur)
364     ;; `message-forward-make-body' shows the User-agent header.  Hide
365     ;; it again.
366     (message-hide-headers)
367     (set-buffer-modified-p nil)))
368
369 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
370   "Compose a reply to the message identified by QUERY-STRING.
371
372 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
373 the From: address first.  If REPLY-ALL is non-nil, the message
374 will be addressed to all recipients of the source message."
375
376 ;; In current emacs (24.3) select-active-regions is set to t by
377 ;; default. The reply insertion code sets the region to the quoted
378 ;; message to make it easy to delete (kill-region or C-w). These two
379 ;; things combine to put the quoted message in the primary selection.
380 ;;
381 ;; This is not what the user wanted and is a privacy risk (accidental
382 ;; pasting of the quoted message). We can avoid some of the problems
383 ;; by let-binding select-active-regions to nil. This fixes if the
384 ;; primary selection was previously in a non-emacs window but not if
385 ;; it was in an emacs window. To avoid the problem in the latter case
386 ;; we deactivate mark.
387
388   (let ((sender
389          (when prompt-for-sender
390            (notmuch-mua-prompt-for-sender)))
391         (select-active-regions nil))
392     (notmuch-mua-reply query-string sender reply-all)
393     (deactivate-mark)))
394
395 (defun notmuch-mua-send-and-exit (&optional arg)
396   (interactive "P")
397   (message-send-and-exit arg))
398
399 (defun notmuch-mua-kill-buffer ()
400   (interactive)
401   (message-kill-buffer))
402
403 (defun notmuch-mua-message-send-hook ()
404   "The default function used for `notmuch-mua-send-hook', this
405 simply runs the corresponding `message-mode' hook functions."
406   (run-hooks 'message-send-hook))
407
408 ;;
409
410 (define-mail-user-agent 'notmuch-user-agent
411   'notmuch-mua-mail 'notmuch-mua-send-and-exit
412   'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
413
414 ;; Add some more headers to the list that `message-mode' hides when
415 ;; composing a message.
416 (notmuch-mua-add-more-hidden-headers)
417
418 ;;
419
420 (provide 'notmuch-mua)