]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-mua.el
emacs: Make moving to the previous message move to the previous boundary
[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=json"))
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     (with-temp-buffer
162       (apply 'call-process (append (list notmuch-command nil (list t nil) nil) args))
163       (goto-char (point-min))
164       (let ((json-object-type 'plist)
165             (json-array-type 'list)
166             (json-false 'nil))
167         (setq reply (json-read))))
168
169     ;; Extract the original message to simplify the following code.
170     (setq original (plist-get reply :original))
171
172     ;; Extract the headers of both the reply and the original message.
173     (let* ((original-headers (plist-get original :headers))
174            (reply-headers (plist-get reply :reply-headers)))
175
176       ;; If sender is non-nil, set the From: header to its value.
177       (when sender
178         (plist-put reply-headers :From sender))
179       (let
180           ;; Overlay the composition window on that being used to read
181           ;; the original message.
182           ((same-window-regexps '("\\*mail .*")))
183
184         ;; We modify message-header-format-alist to get around a bug in message.el.
185         ;; See the comment above on notmuch-mua-insert-references.
186         (let ((message-header-format-alist
187                (loop for pair in message-header-format-alist
188                      if (eq (car pair) 'References)
189                      collect (cons 'References
190                                    (apply-partially
191                                     'notmuch-mua-insert-references
192                                     (cdr pair)))
193                      else
194                      collect pair)))
195           (notmuch-mua-mail (plist-get reply-headers :To)
196                             (plist-get reply-headers :Subject)
197                             (notmuch-headers-plist-to-alist reply-headers)
198                             nil (notmuch-mua-get-switch-function))))
199
200       ;; Insert the message body - but put it in front of the signature
201       ;; if one is present
202       (goto-char (point-max))
203       (if (re-search-backward message-signature-separator nil t)
204           (forward-line -1)
205         (goto-char (point-max)))
206
207       (let ((from (plist-get original-headers :From))
208             (date (plist-get original-headers :Date))
209             (start (point)))
210
211         ;; message-cite-original constructs a citation line based on the From and Date
212         ;; headers of the original message, which are assumed to be in the buffer.
213         (insert "From: " from "\n")
214         (insert "Date: " date "\n\n")
215
216         ;; Get the parts of the original message that should be quoted; this includes
217         ;; all the text parts, except the non-preferred ones in a multipart/alternative.
218         (let ((quotable-parts (notmuch-mua-get-quotable-parts (plist-get original :body))))
219           (mapc (apply-partially 'notmuch-mua-insert-quotable-part original) quotable-parts))
220
221         (set-mark (point))
222         (goto-char start)
223         ;; Quote the original message according to the user's configured style.
224         (message-cite-original))))
225
226   (goto-char (point-max))
227   (push-mark)
228   (message-goto-body)
229   (set-buffer-modified-p nil))
230
231 (defun notmuch-mua-forward-message ()
232   (funcall (notmuch-mua-get-switch-function) (current-buffer))
233   (message-forward)
234
235   (when notmuch-mua-user-agent-function
236     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
237       (when (not (string= "" user-agent))
238         (message-add-header (format "User-Agent: %s" user-agent)))))
239   (message-sort-headers)
240   (message-hide-headers)
241   (set-buffer-modified-p nil)
242   (notmuch-mua-maybe-set-window-dedicated)
243
244   (message-goto-to))
245
246 (defun notmuch-mua-mail (&optional to subject other-headers &rest other-args)
247   "Invoke the notmuch mail composition window.
248
249 OTHER-ARGS are passed through to `message-mail'."
250   (interactive)
251
252   (when notmuch-mua-user-agent-function
253     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
254       (when (not (string= "" user-agent))
255         (push (cons 'User-Agent user-agent) other-headers))))
256
257   (unless (assq 'From other-headers)
258     (push (cons 'From (concat
259                        (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
260
261   (apply #'message-mail to subject other-headers other-args)
262   (message-sort-headers)
263   (message-hide-headers)
264   (set-buffer-modified-p nil)
265   (notmuch-mua-maybe-set-window-dedicated)
266
267   (message-goto-to))
268
269 (defcustom notmuch-identities nil
270   "Identities that can be used as the From: address when composing a new message.
271
272 If this variable is left unset, then a list will be constructed from the
273 name and addresses configured in the notmuch configuration file."
274   :type '(repeat string)
275   :group 'notmuch-send)
276
277 (defcustom notmuch-always-prompt-for-sender nil
278   "Always prompt for the From: address when composing or forwarding a message.
279
280 This is not taken into account when replying to a message, because in that case
281 the From: header is already filled in by notmuch."
282   :type 'boolean
283   :group 'notmuch-send)
284
285 (defvar notmuch-mua-sender-history nil)
286
287 (defun notmuch-mua-prompt-for-sender ()
288   (interactive)
289   (let (name addresses one-name-only)
290     ;; If notmuch-identities is non-nil, check if there is a fixed user name.
291     (if notmuch-identities
292         (let ((components (mapcar 'mail-extract-address-components notmuch-identities)))
293           (setq name          (caar components)
294                 addresses     (mapcar 'cadr components)
295                 one-name-only (eval
296                                (cons 'and
297                                      (mapcar (lambda (identity)
298                                                (string-equal name (car identity)))
299                                              components)))))
300       ;; If notmuch-identities is nil, use values from the notmuch configuration file.
301       (setq name          (notmuch-user-name)
302             addresses     (cons (notmuch-user-primary-email) (notmuch-user-other-email))
303             one-name-only t))
304     ;; Now prompt the user, either for an email address only or for a full identity.
305     (if one-name-only
306         (let ((address
307                (ido-completing-read (concat "Sender address for " name ": ") addresses
308                                     nil nil nil 'notmuch-mua-sender-history (car addresses))))
309           (concat name " <" address ">"))
310       (ido-completing-read "Send mail From: " notmuch-identities
311                            nil nil nil 'notmuch-mua-sender-history (car notmuch-identities)))))
312
313 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
314   "Invoke the notmuch mail composition window.
315
316 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
317 the From: address first."
318   (interactive "P")
319   (let ((other-headers
320          (when (or prompt-for-sender notmuch-always-prompt-for-sender)
321            (list (cons 'From (notmuch-mua-prompt-for-sender))))))
322     (notmuch-mua-mail nil nil other-headers nil (notmuch-mua-get-switch-function))))
323
324 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
325   "Invoke the notmuch message forwarding window.
326
327 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
328 the From: address first."
329   (interactive "P")
330   (if (or prompt-for-sender notmuch-always-prompt-for-sender)
331       (let* ((sender (notmuch-mua-prompt-for-sender))
332              (address-components (mail-extract-address-components sender))
333              (user-full-name (car address-components))
334              (user-mail-address (cadr address-components)))
335         (notmuch-mua-forward-message))
336     (notmuch-mua-forward-message)))
337
338 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender reply-all)
339   "Invoke the notmuch reply window."
340   (interactive "P")
341   (let ((sender
342          (when prompt-for-sender
343            (notmuch-mua-prompt-for-sender))))
344     (notmuch-mua-reply query-string sender reply-all)))
345
346 (defun notmuch-mua-send-and-exit (&optional arg)
347   (interactive "P")
348   (message-send-and-exit arg))
349
350 (defun notmuch-mua-kill-buffer ()
351   (interactive)
352   (message-kill-buffer))
353
354 (defun notmuch-mua-message-send-hook ()
355   "The default function used for `notmuch-mua-send-hook', this
356 simply runs the corresponding `message-mode' hook functions."
357   (run-hooks 'message-send-hook))
358
359 ;;
360
361 (define-mail-user-agent 'notmuch-user-agent
362   'notmuch-mua-mail 'notmuch-mua-send-and-exit
363   'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
364
365 ;; Add some more headers to the list that `message-mode' hides when
366 ;; composing a message.
367 (notmuch-mua-add-more-hidden-headers)
368
369 ;;
370
371 (provide 'notmuch-mua)