1 ; notmuch.el --- run notmuch within emacs
3 ; Copyright © Carl Worth
5 ; This file is part of Notmuch.
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.
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.
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/>.
20 ; Authors: Carl Worth <cworth@cworth.org>
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
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.
31 ; Authors: Roland McGrath <roland@gnu.org>,
32 ; Daniel Pfeiffer <occitan@esperanto.org>
34 (defvar notmuch-show-mode-map
35 (let ((map (make-sparse-keymap)))
36 (define-key map "x" 'kill-this-buffer)
38 "Keymap for \"notmuch show\" buffers.")
39 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
42 (defun notmuch-show-mode ()
43 "Major mode for handling the output of \"notmuch show\""
45 (kill-all-local-variables)
46 (use-local-map notmuch-show-mode-map)
47 (setq major-mode 'notmuch-show-mode
48 mode-name "notmuch-show")
49 (setq buffer-read-only t))
51 (defun notmuch-show (thread-id)
52 "Run \"notmuch show\" with the given thread ID and display results."
53 (interactive "sNotmuch show: ")
54 (let ((buffer (get-buffer-create (concat "*notmuch-show-" thread-id "*"))))
55 (switch-to-buffer buffer)
57 (let ((proc (get-buffer-process (current-buffer)))
58 (inhibit-read-only t))
60 (error "notmuch search process already running for query `%s'" query)
65 (call-process "notmuch" nil t nil "show" thread-id)
69 (defvar notmuch-search-mode-map
70 (let ((map (make-sparse-keymap)))
71 (define-key map "a" 'notmuch-search-archive-thread)
72 (define-key map "n" 'next-line)
73 (define-key map "p" 'previous-line)
74 (define-key map "\r" 'notmuch-search-show-thread)
75 (define-key map "+" 'notmuch-search-add-tag)
76 (define-key map "-" 'notmuch-search-remove-tag)
78 "Keymap for \"notmuch search\" buffers.")
79 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
82 (defun notmuch-search-mode ()
83 "Major mode for handling the output of \"notmuch search\""
85 (kill-all-local-variables)
86 (use-local-map notmuch-search-mode-map)
87 (setq major-mode 'notmuch-search-mode
88 mode-name "notmuch-search")
89 (setq buffer-read-only t))
91 (defun notmuch-search-find-thread-id ()
95 (re-search-forward "[a-fA-F0-9]*")
96 (filter-buffer-substring beg (point)))))
98 (defun notmuch-search-show-thread ()
100 (notmuch-show (notmuch-search-find-thread-id)))
102 (defun notmuch-search-call-notmuch-process (&rest args)
103 (let ((error-buffer (get-buffer-create "*Notmuch errors*")))
104 (with-current-buffer error-buffer
106 (if (eq (apply 'call-process "notmuch" nil error-buffer nil args) 0)
109 (with-current-buffer error-buffer
110 (let ((beg (point-min))
111 (end (- (point-max) 1)))
112 (error (buffer-substring beg end))
115 (defun notmuch-search-add-tag (tag)
116 (interactive "sTag to add: ")
117 (notmuch-search-call-notmuch-process "tag" (concat "+" tag) (concat "thread:" (notmuch-search-find-thread-id))))
119 (defun notmuch-search-remove-tag (tag)
120 (interactive "sTag to remove: ")
121 (notmuch-search-call-notmuch-process "tag" (concat "-" tag) (concat "thread:" (notmuch-search-find-thread-id))))
123 (defun notmuch-search-archive-thread ()
125 (notmuch-search-remove-tag "inbox"))
127 (defun notmuch-search (query)
128 "Run \"notmuch search\" with the given query string and display results."
129 (interactive "sNotmuch search: ")
130 (let ((buffer (get-buffer-create (concat "*notmuch-search-" query "*"))))
131 (switch-to-buffer buffer)
132 (notmuch-search-mode)
133 (let ((proc (get-buffer-process (current-buffer)))
134 (inhibit-read-only t))
136 (error "notmuch search process already running for query `%s'" query)
139 (beginning-of-buffer)
141 (call-process "notmuch" nil t nil "search" query)
146 "Run notmuch to display all mail with tag of 'inbox'"
148 (notmuch-search "tag:inbox"))