]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-mua.el
emacs: Add a customization allowing to always prompt for the "From" address when...
[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
24 (require 'notmuch-lib)
25 (require 'notmuch-address)
26
27 ;;
28
29 (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
30   "Hook run before sending messages."
31   :group 'notmuch
32   :type 'hook)
33
34 (defcustom notmuch-mua-user-agent-function 'notmuch-mua-user-agent-full
35   "Function used to generate a `User-Agent:' string. If this is
36 `nil' then no `User-Agent:' will be generated."
37   :group 'notmuch
38   :type 'function
39   :options '(notmuch-mua-user-agent-full
40              notmuch-mua-user-agent-notmuch
41              notmuch-mua-user-agent-emacs))
42
43 (defcustom notmuch-mua-hidden-headers '("^User-Agent:")
44   "Headers that are added to the `message-mode' hidden headers
45 list."
46   :group 'notmuch
47   :type '(repeat string))
48
49 ;;
50
51 (defun notmuch-mua-user-agent-full ()
52   "Generate a `User-Agent:' string suitable for notmuch."
53   (concat (notmuch-mua-user-agent-notmuch)
54           " "
55           (notmuch-mua-user-agent-emacs)))
56
57 (defun notmuch-mua-user-agent-notmuch ()
58   "Generate a `User-Agent:' string suitable for notmuch."
59   (concat "Notmuch/" (notmuch-version) " (http://notmuchmail.org)"))
60
61 (defun notmuch-mua-user-agent-emacs ()
62   "Generate a `User-Agent:' string suitable for notmuch."
63   (concat "Emacs/" emacs-version " (" system-configuration ")"))
64
65 (defun notmuch-mua-add-more-hidden-headers ()
66   "Add some headers to the list that are hidden by default."
67   (mapc (lambda (header)
68           (when (not (member header 'message-hidden-headers))
69             (push header message-hidden-headers)))
70         notmuch-mua-hidden-headers))
71
72 (defun notmuch-mua-reply (query-string &optional sender)
73   (let (headers body)
74     ;; This make assumptions about the output of `notmuch reply', but
75     ;; really only that the headers come first followed by a blank
76     ;; line and then the body.
77     (with-temp-buffer
78       (call-process notmuch-command nil t nil "reply" query-string)
79       (goto-char (point-min))
80       (if (re-search-forward "^$" nil t)
81           (save-excursion
82             (save-restriction
83               (narrow-to-region (point-min) (point))
84               (goto-char (point-min))
85               (setq headers (mail-header-extract)))))
86       (forward-line 1)
87       (setq body (buffer-substring (point) (point-max))))
88     ;; If sender is non-nil, set the From: header to its value.
89     (when sender
90       (mail-header-set 'from sender headers))
91     (let
92         ;; Overlay the composition window on that being used to read
93         ;; the original message.
94         ((same-window-regexps '("\\*mail .*")))
95       (notmuch-mua-mail (mail-header 'to headers)
96                         (mail-header 'subject headers)
97                         (message-headers-to-generate headers t '(to subject))))
98     ;; insert the message body - but put it in front of the signature
99     ;; if one is present
100     (goto-char (point-max))
101     (if (re-search-backward message-signature-separator nil t)
102           (forward-line -1)
103       (goto-char (point-max)))
104     (insert body))
105   (set-buffer-modified-p nil)
106
107   (message-goto-body))
108
109 (defun notmuch-mua-forward-message ()
110   (message-forward)
111
112   (when notmuch-mua-user-agent-function
113     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
114       (when (not (string= "" user-agent))
115         (message-add-header (format "User-Agent: %s" user-agent)))))
116   (message-sort-headers)
117   (message-hide-headers)
118   (set-buffer-modified-p nil)
119
120   (message-goto-to))
121
122 (defun notmuch-mua-mail (&optional to subject other-headers continue
123                                    switch-function yank-action send-actions)
124   "Invoke the notmuch mail composition window."
125   (interactive)
126
127   (when notmuch-mua-user-agent-function
128     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
129       (when (not (string= "" user-agent))
130         (push (cons "User-Agent" user-agent) other-headers))))
131
132   (unless (mail-header 'from other-headers)
133     (push (cons "From" (concat
134                         (notmuch-user-name) " <" (notmuch-user-primary-email) ">")) other-headers))
135
136   (message-mail to subject other-headers continue
137                 switch-function yank-action send-actions)
138   (message-sort-headers)
139   (message-hide-headers)
140   (set-buffer-modified-p nil)
141
142   (message-goto-to))
143
144 (defcustom notmuch-identities nil
145   "Identities that can be used as the From: address when composing a new message.
146
147 If this variable is left unset, then a list will be constructed from the
148 name and addresses configured in the notmuch configuration file."
149   :group 'notmuch
150   :type '(repeat string))
151
152 (defcustom notmuch-always-prompt-for-sender nil
153   "Always prompt for the From: address when composing a new message."
154   :group 'notmuch
155   :type 'boolean)
156
157 (defun notmuch-mua-sender-collection ()
158   (if notmuch-identities
159       notmuch-identities
160     (mapcar (lambda (address)
161               (concat (notmuch-user-name) " <" address ">"))
162             (cons (notmuch-user-primary-email) (notmuch-user-other-email)))))
163
164 (defvar notmuch-mua-sender-history nil)
165
166 (defun notmuch-mua-prompt-for-sender ()
167   (interactive)
168   (let ((collection (notmuch-mua-sender-collection)))
169     (ido-completing-read "Send mail From: " collection
170                          nil 'confirm nil 'notmuch-mua-sender-history (car collection))))
171
172 (defun notmuch-mua-new-mail (&optional prompt-for-sender)
173   "Invoke the notmuch mail composition window.
174
175 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
176 the From: address first."
177   (interactive "P")
178   (let ((other-headers
179          (when (or prompt-for-sender notmuch-always-prompt-for-sender)
180            (list (cons 'from (notmuch-mua-prompt-for-sender))))))
181     (notmuch-mua-mail nil nil other-headers)))
182
183 (defun notmuch-mua-new-forward-message (&optional prompt-for-sender)
184   "Invoke the notmuch message forwarding window.
185
186 If PROMPT-FOR-SENDER is non-nil, the user will be prompted for
187 the From: address first."
188   (interactive "P")
189   (if (or prompt-for-sender notmuch-always-prompt-for-sender)
190       (let* ((sender (notmuch-mua-prompt-for-sender))
191              (address-components (mail-extract-address-components sender))
192              (user-full-name (car address-components))
193              (user-mail-address (cadr address-components)))
194         (notmuch-mua-forward-message))
195     (notmuch-mua-forward-message)))
196
197 (defun notmuch-mua-new-reply (query-string &optional prompt-for-sender)
198   "Invoke the notmuch reply window."
199   (interactive "P")
200   (let ((sender
201          (when (or prompt-for-sender notmuch-always-prompt-for-sender)
202            (notmuch-mua-prompt-for-sender))))
203     (notmuch-mua-reply query-string sender)))
204
205 (defun notmuch-mua-send-and-exit (&optional arg)
206   (interactive "P")
207   (message-send-and-exit arg))
208
209 (defun notmuch-mua-kill-buffer ()
210   (interactive)
211   (message-kill-buffer))
212
213 (defun notmuch-mua-message-send-hook ()
214   "The default function used for `notmuch-mua-send-hook', this
215 simply runs the corresponding `message-mode' hook functions."
216   (run-hooks 'message-send-hook))
217
218 ;;
219
220 (define-mail-user-agent 'notmuch-user-agent
221   'notmuch-mua-mail 'notmuch-mua-send-and-exit
222   'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
223
224 ;; Add some more headers to the list that `message-mode' hides when
225 ;; composing a message.
226 (notmuch-mua-add-more-hidden-headers)
227
228 ;;
229
230 (provide 'notmuch-mua)