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