]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-mua.el
emacs: Use message-signature-separator rather than hard-coded string.
[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 'cl)
23 (require 'message)
24
25 (require 'notmuch-lib)
26 (require 'notmuch-address)
27
28 ;;
29
30 (defcustom notmuch-mua-send-hook '(notmuch-mua-message-send-hook)
31   "Hook run before sending messages."
32   :group 'notmuch
33   :type 'hook)
34
35 (defcustom notmuch-mua-user-agent-function 'notmuch-mua-user-agent-full
36   "Function used to generate a `User-Agent:' string. If this is
37 `nil' then no `User-Agent:' will be generated."
38   :group 'notmuch
39   :type 'function
40   :options '(notmuch-mua-user-agent-full
41              notmuch-mua-user-agent-notmuch
42              notmuch-mua-user-agent-emacs))
43
44 (defcustom notmuch-mua-hidden-headers '("^User-Agent:")
45   "Headers that are added to the `message-mode' hidden headers
46 list."
47   :group 'notmuch
48   :type '(repeat string))
49
50 ;;
51
52 (defun notmuch-mua-user-agent-full ()
53   "Generate a `User-Agent:' string suitable for notmuch."
54   (concat (notmuch-mua-user-agent-notmuch)
55           " "
56           (notmuch-mua-user-agent-emacs)))
57
58 (defun notmuch-mua-user-agent-notmuch ()
59   "Generate a `User-Agent:' string suitable for notmuch."
60   (concat "Notmuch/" (notmuch-version) " (http://notmuchmail.org)"))
61
62 (defun notmuch-mua-user-agent-emacs ()
63   "Generate a `User-Agent:' string suitable for notmuch."
64   (concat "Emacs/" emacs-version " (" system-configuration ")"))
65
66 (defun notmuch-mua-add-more-hidden-headers ()
67   "Add some headers to the list that are hidden by default."
68   (mapc (lambda (header)
69           (when (not (member header 'message-hidden-headers))
70             (push header message-hidden-headers)))
71         notmuch-mua-hidden-headers))
72
73 (defun notmuch-mua-reply (query-string)
74   (let (headers body)
75     ;; This make assumptions about the output of `notmuch reply', but
76     ;; really only that the headers come first followed by a blank
77     ;; line and then the body.
78     (with-temp-buffer
79       (call-process notmuch-command nil t nil "reply" query-string)
80       (goto-char (point-min))
81       (if (re-search-forward "^$" nil t)
82           (save-excursion
83             (save-restriction
84               (narrow-to-region (point-min) (point))
85               (goto-char (point-min))
86               (setq headers (mail-header-extract)))))
87       (forward-line 1)
88       (setq body (buffer-substring (point) (point-max))))
89     (let
90         ;; Overlay the composition window on that being used to read
91         ;; the original message.
92         ((same-window-regexps '("\\*mail .*")))
93       (notmuch-mua-mail (mail-header 'to headers)
94                         (mail-header 'subject headers)
95                         (loop for header in headers
96                               if (not (or (eq 'to (car header))
97                                           (eq 'subject (car header))))
98                               collect header)))
99     (message-sort-headers)
100     (message-hide-headers)
101     ;; insert the message body - but put it in front of the signature
102     ;; if one is present
103     (goto-char (point-max))
104     (if (re-search-backward message-signature-separator nil t)
105           (forward-line -1)
106       (goto-char (point-max)))
107     (insert body))
108   (set-buffer-modified-p nil)
109
110   (message-goto-body))
111
112 (defun notmuch-mua-forward-message ()
113   (message-forward)
114
115   (when notmuch-mua-user-agent-function
116     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
117       (when (not (string= "" user-agent))
118         (message-add-header (format "User-Agent: %s" user-agent)))))
119   (message-sort-headers)
120   (message-hide-headers)
121   (set-buffer-modified-p nil)
122
123   (message-goto-to))
124
125 (defun notmuch-mua-mail (&optional to subject other-headers continue
126                                    switch-function yank-action send-actions)
127   (interactive)
128
129   (when notmuch-mua-user-agent-function
130     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
131       (when (not (string= "" user-agent))
132         (push (cons "User-Agent" user-agent) other-headers))))
133
134   (message-mail to subject other-headers continue
135                 switch-function yank-action send-actions)
136   (message-sort-headers)
137   (message-hide-headers)
138   (set-buffer-modified-p nil)
139
140   (message-goto-to))
141
142 (defun notmuch-mua-send-and-exit (&optional arg)
143   (interactive "P")
144   (message-send-and-exit arg))
145
146 (defun notmuch-mua-kill-buffer ()
147   (interactive)
148   (message-kill-buffer))
149
150 (defun notmuch-mua-message-send-hook ()
151   "The default function used for `notmuch-mua-send-hook', this
152 simply runs the corresponding `message-mode' hook functions."
153   (run-hooks 'message-send-hook))
154
155 ;;
156
157 (define-mail-user-agent 'notmuch-user-agent
158   'notmuch-mua-mail 'notmuch-mua-send-and-exit
159   'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
160
161 ;; Add some more headers to the list that `message-mode' hides when
162 ;; composing a message.
163 (notmuch-mua-add-more-hidden-headers)
164
165 ;;
166
167 (provide 'notmuch-mua)