]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
notmuch.el: Hide email headers by default.
[notmuch] / notmuch.el
1 ; notmuch.el --- run notmuch within emacs
2 ;
3 ; Copyright © Carl Worth
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: Carl Worth <cworth@cworth.org>
21 ;
22 ; Much of notmuch.el was written by looking at the implementation of
23 ; compile.el from the emacs distribution source which has the
24 ; following copyright and authorsip (and the identical license as
25 ; above):
26 ;
27 ; Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
28 ;   2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
29 ;   Free Software Foundation, Inc.
30
31 ; Authors: Roland McGrath <roland@gnu.org>,
32 ;           Daniel Pfeiffer <occitan@esperanto.org>
33
34 (defvar notmuch-show-mode-map
35   (let ((map (make-sparse-keymap)))
36     (define-key map "h" 'notmuch-show-toggle-headers-visible)
37     (define-key map "n" 'notmuch-show-next-message)
38     (define-key map "p" 'notmuch-show-previous-message)
39     (define-key map "q" 'kill-this-buffer)
40     (define-key map "x" 'kill-this-buffer)
41     map)
42   "Keymap for \"notmuch show\" buffers.")
43 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
44
45 (defvar notmuch-show-message-begin-regexp "\fmessage{")
46 (defvar notmuch-show-message-end-regexp   "\fmessage}")
47 (defvar notmuch-show-header-begin-regexp  "\fheader{")
48 (defvar notmuch-show-header-end-regexp    "\fheader}")
49
50 (defvar notmuch-show-headers-visible t)
51
52 (defun notmuch-show-next-message ()
53   "Advance point to the beginning of the next message in the buffer."
54   (interactive)
55   ; First, ensure we get off the current message marker
56   (if (not (eobp))
57       (forward-char))
58   (if (not (re-search-forward notmuch-show-message-begin-regexp nil t))
59       (goto-char (point-max)))
60   (beginning-of-line)
61   (recenter 0))
62
63 (defun notmuch-show-previous-message ()
64   "Advance point to the beginning of the previous message in the buffer."
65   (interactive)
66   ; First, ensure we get off the current message marker
67   (if (not (eobp))
68       (forward-char))
69   (if (not (re-search-backward notmuch-show-message-begin-regexp nil t))
70       (goto-char (point-min)))
71   (beginning-of-line)
72   (recenter 0))
73
74 (defun notmuch-show-markup-this-header ()
75   (if (re-search-forward notmuch-show-header-begin-regexp nil t)
76       (progn
77         (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
78                      'invisible 'notmuch-show-marker)
79         (next-line 1)
80         (beginning-of-line)
81         (let ((beg (point)))
82           (if (re-search-forward notmuch-show-header-end-regexp nil t)
83               (progn
84                 (overlay-put (make-overlay beg (match-beginning 0))
85                              'invisible 'notmuch-show-header)
86                 (overlay-put (make-overlay (match-beginning 0) (+ (match-end 0) 1))
87                              'invisible 'notmuch-show-marker)))))
88     (goto-char (point-max))))
89
90 (defun notmuch-show-markup-headers ()
91   (save-excursion
92     (goto-char (point-min))
93     (while (not (eobp))
94       (notmuch-show-markup-this-header))))
95
96 (defun notmuch-show-toggle-headers-visible ()
97   "Toggle visibility of header fields"
98   (interactive)
99   (if notmuch-show-headers-visible
100       (progn
101         (add-to-invisibility-spec 'notmuch-show-header)
102         (set 'notmuch-show-headers-visible nil)
103         ; Need to force the redisplay for some reason
104         (force-window-update)
105         (redisplay t))
106     (remove-from-invisibility-spec 'notmuch-show-header)
107     (set 'notmuch-show-headers-visible t)
108     (force-window-update)
109     (redisplay t)))
110
111 ;;;###autoload
112 (defun notmuch-show-mode ()
113   "Major mode for handling the output of \"notmuch show\""
114   (interactive)
115   (kill-all-local-variables)
116   (set (make-local-variable 'notmuch-show-headers-visible) t)
117   (notmuch-show-toggle-headers-visible)
118   (add-to-invisibility-spec 'notmuch-show-marker)
119   (use-local-map notmuch-show-mode-map)
120   (setq major-mode 'notmuch-show-mode
121         mode-name "notmuch-show")
122   (setq buffer-read-only t))
123
124 (defun notmuch-show (thread-id)
125   "Run \"notmuch show\" with the given thread ID and display results."
126   (interactive "sNotmuch show: ")
127   (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
128     (switch-to-buffer buffer)
129     (notmuch-show-mode)
130     (let ((proc (get-buffer-process (current-buffer)))
131           (inhibit-read-only t))
132       (if proc
133           (error "notmuch search process already running for query `%s'" query)
134         )
135       (erase-buffer)
136       (goto-char (point-min))
137       (save-excursion
138         (call-process "notmuch" nil t nil "show" thread-id)
139         (notmuch-show-markup-headers)
140         )
141       )))
142
143 (defvar notmuch-search-mode-map
144   (let ((map (make-sparse-keymap)))
145     (define-key map "a" 'notmuch-search-archive-thread)
146     (define-key map "f" 'notmuch-search-filter)
147     (define-key map "n" 'next-line)
148     (define-key map "p" 'previous-line)
149     (define-key map "q" 'kill-this-buffer)
150     (define-key map "s" 'notmuch-search)
151     (define-key map "x" 'kill-this-buffer)
152     (define-key map "\r" 'notmuch-search-show-thread)
153     (define-key map "+" 'notmuch-search-add-tag)
154     (define-key map "-" 'notmuch-search-remove-tag)
155     (define-key map "<" 'beginning-of-buffer)
156     (define-key map ">" 'notmuch-search-goto-last-thread)
157     (define-key map "\M->" 'notmuch-search-goto-last-thread)
158     map)
159   "Keymap for \"notmuch search\" buffers.")
160 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
161
162 (defun notmuch-search-goto-last-thread (&optional arg)
163   "Move point to the last thread in the buffer."
164   (interactive "^P")
165   (end-of-buffer arg)
166   (beginning-of-line))
167
168 ;;;###autoload
169 (defun notmuch-search-mode ()
170   "Major mode for handling the output of \"notmuch search\""
171   (interactive)
172   (kill-all-local-variables)
173   (make-local-variable 'notmuch-search-query-string)
174   (use-local-map notmuch-search-mode-map)
175   (setq major-mode 'notmuch-search-mode
176         mode-name "notmuch-search")
177   (setq buffer-read-only t))
178
179 (defun notmuch-search-find-thread-id ()
180   (save-excursion
181     (beginning-of-line)
182     (let ((beg (point)))
183       (re-search-forward "[a-fA-F0-9]*")
184       (filter-buffer-substring beg (point)))))
185
186 (defun notmuch-search-markup-this-thread-id ()
187   (beginning-of-line)
188   (let ((beg (point)))
189     (re-search-forward "[a-fA-F0-9]*")
190     (forward-char)
191     (overlay-put (make-overlay beg (point)) 'invisible 'notmuch-search)))
192
193 (defun notmuch-search-markup-thread-ids ()
194   (save-excursion
195     (goto-char (point-min))
196     (while (not (eobp))
197       (notmuch-search-markup-this-thread-id)
198       (next-line))))
199
200 (defun notmuch-search-hide-thread-ids ()
201   (interactive)
202   (add-to-invisibility-spec 'notmuch-search))
203
204 (defun notmuch-search-show-thread-ids ()
205   (interactive)
206   (remove-from-invisibility-spec 'notmuch-search))
207
208 (defun notmuch-search-show-thread ()
209   (interactive)
210   (notmuch-show (notmuch-search-find-thread-id)))
211
212 (defun notmuch-search-call-notmuch-process (&rest args)
213   (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
214     (with-current-buffer error-buffer
215         (erase-buffer))
216     (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
217         (point)
218       (progn
219         (with-current-buffer error-buffer
220           (let ((beg (point-min))
221                 (end (- (point-max) 1)))
222             (error (buffer-substring beg end))
223             ))))))
224
225 (defun notmuch-search-set-tags (tags)
226   (save-excursion
227     (end-of-line)
228     (re-search-backward "(")
229     (forward-char)
230     (let ((beg (point))
231           (inhibit-read-only t))
232       (re-search-forward ")")
233       (backward-char)
234       (let ((end (point)))
235         (delete-region beg end)
236         (insert (mapconcat  'identity tags " "))))))
237
238 (defun notmuch-search-get-tags ()
239   (save-excursion
240     (end-of-line)
241     (re-search-backward "(")
242     (let ((beg (+ (point) 1)))
243       (re-search-forward ")")
244       (let ((end (- (point) 1)))
245         (split-string (buffer-substring beg end))))))
246
247 (defun notmuch-search-add-tag (tag)
248   (interactive "sTag to add: ")
249   (notmuch-search-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id)))
250   (notmuch-search-set-tags (delete-dups (sort (cons tag (notmuch-search-get-tags)) 'string<))))
251
252 (defun notmuch-search-remove-tag (tag)
253   (interactive "sTag to remove: ")
254   (notmuch-search-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id)))
255   (notmuch-search-set-tags (delete tag (notmuch-search-get-tags))))
256
257 (defun notmuch-search-archive-thread ()
258   (interactive)
259   (notmuch-search-remove-tag "inbox"))
260
261 (defun notmuch-search (query)
262   "Run \"notmuch search\" with the given query string and display results."
263   (interactive "sNotmuch search: ")
264   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
265     (switch-to-buffer buffer)
266     (notmuch-search-mode)
267     (set 'notmuch-search-query-string query)
268     (let ((proc (get-buffer-process (current-buffer)))
269           (inhibit-read-only t))
270       (if proc
271           (error "notmuch search process already running for query `%s'" query)
272         )
273       (erase-buffer)
274       (goto-char (point-min))
275       (save-excursion
276         (call-process "notmuch" nil t nil "search" query)
277         (notmuch-search-markup-thread-ids)
278         ; A well-behaved program ends its output with a newline, but we
279         ; don't actually want the blank line at the end of the file.
280         (goto-char (point-max))
281         (if (looking-at "^$")
282             (delete-backward-char 1)
283           )
284         ))))
285
286 (defun notmuch-search-filter (query)
287   "Run \"notmuch search\" to refine the current search results.
288
289 A search string will be constructed by appending QUERY to the
290 current search string, and the results of \"notmuch search\" for
291 the combined query will be displayed."
292   (interactive "sFilter search: ")
293   (notmuch-search (concat notmuch-search-query-string " and " query)))
294
295 (defun notmuch ()
296   "Run notmuch to display all mail with tag of 'inbox'"
297   (interactive)
298   (notmuch-search "tag:inbox"))
299
300 (provide 'notmuch)