]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-mua.el
emacs: Reuse rather than reinvent message header filtering
[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)
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     (let
89         ;; Overlay the composition window on that being used to read
90         ;; the original message.
91         ((same-window-regexps '("\\*mail .*")))
92       (notmuch-mua-mail (mail-header 'to headers)
93                         (mail-header 'subject headers)
94                         (message-headers-to-generate headers t '(to subject))))
95     ;; insert the message body - but put it in front of the signature
96     ;; if one is present
97     (goto-char (point-max))
98     (if (re-search-backward message-signature-separator nil t)
99           (forward-line -1)
100       (goto-char (point-max)))
101     (insert body))
102   (set-buffer-modified-p nil)
103
104   (message-goto-body))
105
106 (defun notmuch-mua-forward-message ()
107   (message-forward)
108
109   (when notmuch-mua-user-agent-function
110     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
111       (when (not (string= "" user-agent))
112         (message-add-header (format "User-Agent: %s" user-agent)))))
113   (message-sort-headers)
114   (message-hide-headers)
115   (set-buffer-modified-p nil)
116
117   (message-goto-to))
118
119 (defun notmuch-mua-mail (&optional to subject other-headers continue
120                                    switch-function yank-action send-actions)
121   (interactive)
122
123   (when notmuch-mua-user-agent-function
124     (let ((user-agent (funcall notmuch-mua-user-agent-function)))
125       (when (not (string= "" user-agent))
126         (push (cons "User-Agent" user-agent) other-headers))))
127
128   (message-mail to subject other-headers continue
129                 switch-function yank-action send-actions)
130   (message-sort-headers)
131   (message-hide-headers)
132   (set-buffer-modified-p nil)
133
134   (message-goto-to))
135
136 (defun notmuch-mua-send-and-exit (&optional arg)
137   (interactive "P")
138   (message-send-and-exit arg))
139
140 (defun notmuch-mua-kill-buffer ()
141   (interactive)
142   (message-kill-buffer))
143
144 (defun notmuch-mua-message-send-hook ()
145   "The default function used for `notmuch-mua-send-hook', this
146 simply runs the corresponding `message-mode' hook functions."
147   (run-hooks 'message-send-hook))
148
149 ;;
150
151 (define-mail-user-agent 'notmuch-user-agent
152   'notmuch-mua-mail 'notmuch-mua-send-and-exit
153   'notmuch-mua-kill-buffer 'notmuch-mua-send-hook)
154
155 ;; Add some more headers to the list that `message-mode' hides when
156 ;; composing a message.
157 (notmuch-mua-add-more-hidden-headers)
158
159 ;;
160
161 (provide 'notmuch-mua)