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