]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-mua.el
emacs: insert quotable parts in reply as they are displayed in show view
[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 ;;
79
80 (defun notmuch-mua-get-switch-function ()
81   "Get a switch function according to `notmuch-mua-compose-in'."
82   (cond ((eq notmuch-mua-compose-in 'current-window)
83          'switch-to-buffer)
84         ((eq notmuch-mua-compose-in 'new-window)
85          'switch-to-buffer-other-window)
86         ((eq notmuch-mua-compose-in 'new-frame)
87          'switch-to-buffer-other-frame)
88         (t (error "Invalid value for `notmuch-mua-compose-in'"))))
89
90 (defun notmuch-mua-maybe-set-window-dedicated ()
91   "Set the selected window as dedicated according to
92 `notmuch-mua-compose-in'."
93   (when (or (eq notmuch-mua-compose-in 'new-frame)
94             (eq notmuch-mua-compose-in 'new-window))
95     (set-window-dedicated-p (selected-window) t)))
96
97 (defun notmuch-mua-user-agent-full ()
98   "Generate a `User-Agent:' string suitable for notmuch."
99   (concat (notmuch-mua-user-agent-notmuch)
100           " "
101           (notmuch-mua-user-agent-emacs)))
102
103 (defun notmuch-mua-user-agent-notmuch ()
104   "Generate a `User-Agent:' string suitable for notmuch."
105   (concat "Notmuch/" (notmuch-version) " (http://notmuchmail.org)"))
106
107 (defun notmuch-mua-user-agent-emacs ()
108   "Generate a `User-Agent:' string suitable for notmuch."
109   (concat "Emacs/" emacs-version " (" system-configuration ")"))
110
111 (defun notmuch-mua-add-more-hidden-headers ()
112   "Add some headers to the list that are hidden by default."
113   (mapc (lambda (header)
114           (when (not (member header message-hidden-headers))
115             (push header message-hidden-headers)))
116         notmuch-mua-hidden-headers))
117
118 (defun notmuch-mua-get-quotable-parts (parts)
119   (loop for part in parts
120         if (notmuch-match-content-type (plist-get part :content-type) "multipart/alternative")
121           collect (let* ((subparts (plist-get part :content))
122                         (types (mapcar (lambda (part) (plist-get part :content-type)) subparts))
123                         (chosen-type (car (notmuch-multipart/alternative-choose types))))
124                    (loop for part in (reverse subparts)
125                          if (notmuch-match-content-type (plist-get part :content-type) chosen-type)
126                          return part))
127         else if (notmuch-match-content-type (plist-get part :content-type) "multipart/*")
128           append (notmuch-mua-get-quotable-parts (plist-get part :content))
129         else if (notmuch-match-content-type (plist-get part :content-type) "text/*")
130           collect part))
131
132 (defun notmuch-mua-insert-quotable-part (message part)
133   ;; We don't want text properties leaking from the show renderer into
134   ;; the reply so we use a temp buffer. Also we don't want hooks, such
135   ;; as notmuch-wash-*, to be run on the quotable part so we set
136   ;; notmuch-show-insert-text/plain-hook to nil.
137   (insert (with-temp-buffer
138             (let ((notmuch-show-insert-text/plain-hook nil))
139               ;; Show the part but do not add buttons.
140               (notmuch-show-insert-bodypart message part 0 'no-buttons))
141             (buffer-substring-no-properties (point-min) (point-max)))))
142
143 ;; There is a bug in emacs 23's message.el that results in a newline
144 ;; not being inserted after the References header, so the next header
145 ;; is concatenated to the end of it. This function fixes the problem,
146 ;; while guarding against the possibility that some current or future
147 ;; version of emacs has the bug fixed.
148 (defun notmuch-mua-insert-references (original-func header references)
149   (funcall original-func header references)
150   (unless (bolp) (insert "\n")))
151
152 (defun notmuch-mua-reply (query-string &optional sender reply-all)
153   (let ((args '("reply" "--format=sexp" "--format-version=1"))
154         reply
155         original)
156     (when notmuch-show-process-crypto
157       (setq args (append args '("--decrypt"))))
158
159     (if reply-all
160         (setq args (append args '("--reply-to=all")))
161       (setq args (append args '("--reply-to=sender"))))
162     (setq args (append args (list query-string)))
163
164     ;; Get the reply object as SEXP, and parse it into an elisp object.
165     (setq reply (apply #'notmuch-call-notmuch-sexp args))
166
167     ;; Extract the original message to simplify the following code.
168     (setq original (plist-get reply :original))
169
170     ;; Extract the headers of both the reply and the original message.
171     (let* ((original-headers (plist-get original :headers))
172            (reply-headers (plist-get reply :reply-headers)))
173
174       ;; If sender is non-nil, set the From: header to its value.
175       (when sender
176         (plist-put reply-headers :From sender))
177       (let
178           ;; Overlay the composition window on that being used to read
179           ;; the original message.
180           ((same-window-regexps '("\\*mail .*")))
181
182         ;; We modify message-header-format-alist to get around a bug in message.el.
183         ;; See the comment above on notmuch-mua-insert-references.
184         (let ((message-header-format-alist
185                (loop for pair in message-header-format-alist
186                      if (eq (car pair) 'References)
187                      collect (cons 'References
188                                    (apply-partially
189                                     'notmuch-mua-insert-references
190                                     (cdr pair)))
191                      else
192                      collect pair)))
193           (notmuch-mua-mail (plist-get reply-headers :To)
194                             (plist-get reply-headers :Subject)
195                             (notmuch-headers-plist-to-alist reply-headers)
196                             nil (notmuch-mua-get-switch-function))))
197
198       ;; Insert the message body - but put it in front of the signature
199       ;; if one is present
200       (goto-char (point-max))
201       (if (re-search-backward message-signature-separator nil t)
202           (forward-line -1)
203         (goto-char (point-max)))
204
205       (let ((from (plist-get original-headers :From))
206             (date (plist-get original-headers :Date))
207             (start (point)))
208
209         ;; message-cite-original constructs a citation line based on the From and Date
210         ;; headers of the original message, which are assumed to be in the buffer.
211         (insert "From: " from "\n")
212         (insert "Date: " date "\n\n")
213
214         ;; Get the parts of the original message that should be quoted; this includes
215         ;; all the text parts, except the non-preferred ones in a multipart/alternative.
216         (let ((quotable-parts (notmuch-mua-get-quotable-parts (plist-get original :body))))
217           (mapc (apply-partially 'notmuch-mua-insert-quotable-part original) quotable-parts))
218
219         (set-mark (point))
220         (goto-char start)
221         ;; Quote the original message according to the user's configured style.
222         (message-cite-original))))
223
224   (goto-char (point-max))
225   (push-mark)
226   (message-goto-body)
227   (set-buffer-modified-p nil))
228
229 (defun notmuch-mua-forward-message ()
230   (funcall (notmuch-mua-get-switch-function) (current-buffer))
231   (message-forward)
232
233   (when notmuch-mua-user-agent-function
234     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
235       (when (not (string= "" user-agent))
236         (message-add-header (format "User-Agent: %s" user-agent)))))
237   (message-sort-headers)
238   (message-hide-headers)
239   (set-buffer-modified-p nil)
240   (notmuch-mua-maybe-set-window-dedicated)
241
242   (message-goto-to))
243
244 (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)
245   "Invoke the notmuch mail composition window.
246
247 OTHER-ARGS are passed through to `message-mail'."
248   (interactive)
249
250   (when notmuch-mua-user-agent-function
251     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
252       (when (not (string= "" user-agent))
253         (push (cons 'User-Agent user-agent) other-headers))))
254
255   (unless (assq 'From other-headers)
256     (push (cons 'From (concat
257                        (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
258
259   (apply #'message-mail to subject other-headers other-args)
260   (message-sort-headers)
261   (message-hide-headers)
262   (set-buffer-modified-p nil)
263   (notmuch-mua-maybe-set-window-dedicated)
264
265   (message-goto-to))
266
267 (defcustom notmuch-identities nil
268   "Identities that can be used as the From: address when composing a new message.
269
270 If this variable is left unset, then a list will be constructed from the
271 name and addresses configured in the notmuch configuration file."
272   :type '(repeat string)
273   :group 'notmuch-send)
274
275 (defcustom notmuch-always-prompt-for-sender nil
276   "Always prompt for the From: address when composing or forwarding a message.
277
278 This is not taken into account when replying to a message, because in that case
279 the From: header is already filled in by notmuch."
280   :type 'boolean
281   :group 'notmuch-send)
282
283 (defvar notmuch-mua-sender-history nil)
284
285 (defun notmuch-mua-prompt-for-sender ()
286   (interactive)
287   (let (name addresses one-name-only)
288     ;; If notmuch-identities is non-nil, check if there is a fixed user name.
289     (if notmuch-identities
290         (let ((components (mapcar 'mail-extract-address-components notmuch-identities)))
291           (setq name          (caar components)
292                 addresses     (mapcar 'cadr components)
293                 one-name-only (eval
294                                (cons 'and
295                                      (mapcar (lambda (identity)
296                                                (string-equal name (car identity)))
297                                              components)))))
298       ;; If notmuch-identities is nil, use values from the notmuch configuration file.
299       (setq name          (notmuch-user-name)
300             addresses     (cons (notmuch-user-primary-email) (notmuch-user-other-email))
301             one-name-only t))
302     ;; Now prompt the user, either for an email address only or for a full identity.
303     (if one-name-only
304         (let ((address
305                (ido-completing-read (concat "Sender address for " name ": ") addresses
306                                     nil nil nil 'notmuch-mua-sender-history (car addresses))))
307           (concat name " <" address ">"))
308       (ido-completing-read "Send mail From: " notmuch-identities
309                            nil nil nil 'notmuch-mua-sender-history (car notmuch-identities)))))
310
311 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
312   "Invoke the notmuch mail composition window.
313
314 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
315 the From: address first."
316   (interactive "P")
317   (let ((other-headers
318          (when (or prompt-for-sender notmuch-always-prompt-for-sender)
319            (list (cons 'From (notmuch-mua-prompt-for-sender))))))
320     (notmuch-mua-mail nil nil other-headers nil (notmuch-mua-get-switch-function))))
321
322 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
323   "Invoke the notmuch message forwarding window.
324
325 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
326 the From: address first."
327   (interactive "P")
328   (if (or prompt-for-sender notmuch-always-prompt-for-sender)
329       (let* ((sender (notmuch-mua-prompt-for-sender))
330              (address-components (mail-extract-address-components sender))
331              (user-full-name (car address-components))
332              (user-mail-address (cadr address-components)))
333         (notmuch-mua-forward-message))
334     (notmuch-mua-forward-message)))
335
336 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
337   "Invoke the notmuch reply window."
338   (interactive "P")
339   (let ((sender
340          (when prompt-for-sender
341            (notmuch-mua-prompt-for-sender))))
342     (notmuch-mua-reply query-string sender reply-all)))
343
344 (defun notmuch-mua-send-and-exit (&optional arg)
345   (interactive "P")
346   (message-send-and-exit arg))
347
348 (defun notmuch-mua-kill-buffer ()
349   (interactive)
350   (message-kill-buffer))
351
352 (defun notmuch-mua-message-send-hook ()
353   "The default function used for `notmuch-mua-send-hook', this
354 simply runs the corresponding `message-mode' hook functions."
355   (run-hooks 'message-send-hook))
356
357 ;;
358
359 (define-mail-user-agent 'notmuch-user-agent
360   'notmuch-mua-mail 'notmuch-mua-send-and-exit
361   'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
362
363 ;; Add some more headers to the list that `message-mode' hides when
364 ;; composing a message.
365 (notmuch-mua-add-more-hidden-headers)
366
367 ;;
368
369 (provide 'notmuch-mua)