]> git.notmuchmail.org Git - notmuch/blob - notmuch.el
010fc2ff048a883db1a27400f59b64ca89ba0b4a
[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 "x" 'kill-this-buffer)
37     map)
38   "Keymap for \"notmuch show\" buffers.")
39 (fset 'notmuch-show-mode-map notmuch-show-mode-map)
40
41 ;;;###autoload
42 (defun notmuch-show-mode ()
43   "Major mode for handling the output of \"notmuch show\""
44   (interactive)
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))
50
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)
56     (notmuch-show-mode)
57     (let ((proc (get-buffer-process (current-buffer)))
58           (inhibit-read-only t))
59       (if proc
60           (error "notmuch search process already running for query `%s'" query)
61         )
62       (erase-buffer)
63       (beginning-of-buffer)
64       (save-excursion
65         (call-process "notmuch" nil t nil "show" thread-id)
66         )
67       )))
68
69 (defvar notmuch-search-mode-map
70   (let ((map (make-sparse-keymap)))
71     (define-key map "n" 'next-line)
72     (define-key map "p" 'previous-line)
73     (define-key map "\r" 'notmuch-search-show-thread)
74     map)
75   "Keymap for \"notmuch search\" buffers.")
76 (fset 'notmuch-search-mode-map notmuch-search-mode-map)
77
78 ;;;###autoload
79 (defun notmuch-search-mode ()
80   "Major mode for handling the output of \"notmuch search\""
81   (interactive)
82   (kill-all-local-variables)
83   (use-local-map notmuch-search-mode-map)
84   (setq major-mode 'notmuch-search-mode
85         mode-name "notmuch-search")
86   (setq buffer-read-only t))
87
88 (defun notmuch-search-find-thread-id ()
89   (save-excursion
90     (beginning-of-line)
91     (let ((beg (point)))
92       (re-search-forward "[a-fA-F0-9]*")
93       (filter-buffer-substring beg (point)))))
94
95 (defun notmuch-search-show-thread ()
96   (interactive)
97   (notmuch-show (notmuch-search-find-thread-id)))
98
99 (defun notmuch-search (query)
100   "Run \"notmuch search\" with the given query string and display results."
101   (interactive "sNotmuch search: ")
102   (let ((buffer (get-buffer-create (concat "*notmuch-search-" query))))
103     (switch-to-buffer buffer)
104     (notmuch-search-mode)
105     (let ((proc (get-buffer-process (current-buffer)))
106           (inhibit-read-only t))
107       (if proc
108           (error "notmuch search process already running for query `%s'" query)
109         )
110       (erase-buffer)
111       (beginning-of-buffer)
112       (save-excursion
113         (call-process "notmuch" nil t nil "search" query)
114         )
115       )))
116
117 (defun notmuch ()
118   "Run notmuch to display all mail with tag of 'inbox'"
119   (interactive)
120   (notmuch-search "tag:inbox"))
121
122 (provide 'notmuch)