]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-lib.el
d5ca0f404ff5de3d529c946c0e05e0927186a7f4
[notmuch] / emacs / notmuch-lib.el
1 ;; notmuch-lib.el --- common variables, functions and function declarations
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 ;; This is an part of an emacs-based interface to the notmuch mail system.
23
24 (defvar notmuch-command "notmuch"
25   "Command to run the notmuch binary.")
26
27 (defgroup notmuch nil
28   "Notmuch mail reader for Emacs."
29   :group 'mail)
30
31 (defcustom notmuch-search-oldest-first t
32   "Show the oldest mail first when searching."
33   :type 'boolean
34   :group 'notmuch)
35
36 ;;
37
38 (defcustom notmuch-saved-searches nil
39   "A list of saved searches to display."
40   :type '(alist :key-type string :value-type string)
41   :group 'notmuch)
42
43 (defvar notmuch-folders nil
44   "Deprecated name for what is now known as `notmuch-saved-searches'.")
45
46 (defun notmuch-saved-searches ()
47   "Common function for querying the notmuch-saved-searches variable.
48
49 We do this as a function to support the old name of the
50 variable (`notmuch-folders') as well as for the default value if
51 the user hasn't set this variable with the old or new value."
52   (if notmuch-saved-searches
53       notmuch-saved-searches
54     (if notmuch-folders
55         notmuch-folders
56       '(("inbox" . "tag:inbox")
57         ("unread" . "tag:unread")))))
58
59 (defun notmuch-version ()
60   "Return a string with the notmuch version number."
61   (let ((long-string
62          ;; Trim off the trailing newline.
63          (substring (shell-command-to-string
64                      (concat notmuch-command " --version"))
65                     0 -1)))
66     (if (string-match "^notmuch\\( version\\)? \\(.*\\)$"
67                       long-string)
68         (match-string 2 long-string)
69       "unknown")))
70
71 (defun notmuch-config-get (item)
72   "Return a value from the notmuch configuration."
73   ;; Trim off the trailing newline
74   (substring (shell-command-to-string
75               (concat notmuch-command " config get " item))
76               0 -1))
77
78 (defun notmuch-database-path ()
79   "Return the database.path value from the notmuch configuration."
80   (notmuch-config-get "database.path"))
81
82 (defun notmuch-user-name ()
83   "Return the user.name value from the notmuch configuration."
84   (notmuch-config-get "user.name"))
85
86 (defun notmuch-user-primary-email ()
87   "Return the user.primary_email value from the notmuch configuration."
88   (notmuch-config-get "user.primary_email"))
89
90 (defun notmuch-user-other-email ()
91   "Return the user.primary_email value (as a list) from the notmuch configuration."
92   (split-string (notmuch-config-get "user.other_email") "\n"))
93
94 (defun notmuch-kill-this-buffer ()
95   "Kill the current buffer."
96   (interactive)
97   (kill-buffer (current-buffer)))
98
99 ;;
100
101 (defun notmuch-common-do-stash (text)
102   "Common function to stash text in kill ring, and display in minibuffer."
103   (kill-new text)
104   (message "Stashed: %s" text))
105
106 ;;
107
108 ;; XXX: This should be a generic function in emacs somewhere, not
109 ;; here.
110 (defun point-invisible-p ()
111   "Return whether the character at point is invisible.
112
113 Here visibility is determined by `buffer-invisibility-spec' and
114 the invisible property of any overlays for point. It doesn't have
115 anything to do with whether point is currently being displayed
116 within the current window."
117   (let ((prop (get-char-property (point) 'invisible)))
118     (if (eq buffer-invisibility-spec t)
119         prop
120       (or (memq prop buffer-invisibility-spec)
121           (assq prop buffer-invisibility-spec)))))
122
123 (defun notmuch-remove-if-not (predicate list)
124   "Return a copy of LIST with all items not satisfying PREDICATE removed."
125   (let (out)
126     (while list
127       (when (funcall predicate (car list))
128         (push (car list) out))
129       (setq list (cdr list)))
130     (nreverse out)))
131
132 ; This lets us avoid compiling these replacement functions when emacs
133 ; is sufficiently new enough to supply them alone. We do the macro
134 ; treatment rather than just wrapping our defun calls in a when form
135 ; specifically so that the compiler never sees the code on new emacs,
136 ; (since the code is triggering warnings that we don't know how to get
137 ; rid of.
138 ;
139 ; A more clever macro here would accept a condition and a list of forms.
140 (defmacro compile-on-emacs-prior-to-23 (form)
141   "Conditionally evaluate form only on emacs < emacs-23."
142   (list 'when (< emacs-major-version 23)
143         form))
144
145 ;; Compatibility functions for versions of emacs before emacs 23.
146 ;;
147 ;; Both functions here were copied from emacs 23 with the following copyright:
148 ;;
149 ;; Copyright (C) 1985, 1986, 1992, 1994, 1995, 1999, 2000, 2001, 2002, 2003,
150 ;;   2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
151 ;;
152 ;; and under the GPL version 3 (or later) exactly as notmuch itself.
153 (compile-on-emacs-prior-to-23
154  (defun apply-partially (fun &rest args)
155    "Return a function that is a partial application of FUN to ARGS.
156 ARGS is a list of the first N arguments to pass to FUN.
157 The result is a new function which does the same as FUN, except that
158 the first N arguments are fixed at the values with which this function
159 was called."
160    (lexical-let ((fun fun) (args1 args))
161      (lambda (&rest args2) (apply fun (append args1 args2))))))
162
163 (compile-on-emacs-prior-to-23
164  (defun mouse-event-p (object)
165    "Return non-nil if OBJECT is a mouse click event."
166    (memq (event-basic-type object) '(mouse-1 mouse-2 mouse-3 mouse-movement))))
167
168 (provide 'notmuch-lib)
169