]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-lib.el
emacs: Fix two bugs in reply
[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 (eval-when-compile (require 'cl))
25
26 (defvar notmuch-command "notmuch"
27   "Command to run the notmuch binary.")
28
29 (defgroup notmuch nil
30   "Notmuch mail reader for Emacs."
31   :group 'mail)
32
33 (defgroup notmuch-hello nil
34   "Overview of saved searches, tags, etc."
35   :group 'notmuch)
36
37 (defgroup notmuch-search nil
38   "Searching and sorting mail."
39   :group 'notmuch)
40
41 (defgroup notmuch-show nil
42   "Showing messages and threads."
43   :group 'notmuch)
44
45 (defgroup notmuch-send nil
46   "Sending messages from Notmuch."
47   :group 'notmuch)
48
49 (custom-add-to-group 'notmuch-send 'message 'custom-group)
50
51 (defgroup notmuch-crypto nil
52   "Processing and display of cryptographic MIME parts."
53   :group 'notmuch)
54
55 (defgroup notmuch-hooks nil
56   "Running custom code on well-defined occasions."
57   :group 'notmuch)
58
59 (defgroup notmuch-external nil
60   "Running external commands from within Notmuch."
61   :group 'notmuch)
62
63 (defgroup notmuch-faces nil
64   "Graphical attributes for displaying text"
65   :group 'notmuch)
66
67 (defcustom notmuch-search-oldest-first t
68   "Show the oldest mail first when searching."
69   :type 'boolean
70   :group 'notmuch-search)
71
72 ;;
73
74 (defvar notmuch-search-history nil
75   "Variable to store notmuch searches history.")
76
77 (defcustom notmuch-saved-searches nil
78   "A list of saved searches to display."
79   :type '(alist :key-type string :value-type string)
80   :group 'notmuch-hello)
81
82 (defvar notmuch-folders nil
83   "Deprecated name for what is now known as `notmuch-saved-searches'.")
84
85 (defun notmuch-saved-searches ()
86   "Common function for querying the notmuch-saved-searches variable.
87
88 We do this as a function to support the old name of the
89 variable (`notmuch-folders') as well as for the default value if
90 the user hasn't set this variable with the old or new value."
91   (if notmuch-saved-searches
92       notmuch-saved-searches
93     (if notmuch-folders
94         notmuch-folders
95       '(("inbox" . "tag:inbox")
96         ("unread" . "tag:unread")))))
97
98 (defun notmuch-version ()
99   "Return a string with the notmuch version number."
100   (let ((long-string
101          ;; Trim off the trailing newline.
102          (substring (shell-command-to-string
103                      (concat notmuch-command " --version"))
104                     0 -1)))
105     (if (string-match "^notmuch\\( version\\)? \\(.*\\)$"
106                       long-string)
107         (match-string 2 long-string)
108       "unknown")))
109
110 (defun notmuch-config-get (item)
111   "Return a value from the notmuch configuration."
112   ;; Trim off the trailing newline
113   (substring (shell-command-to-string
114               (concat notmuch-command " config get " item))
115               0 -1))
116
117 (defun notmuch-database-path ()
118   "Return the database.path value from the notmuch configuration."
119   (notmuch-config-get "database.path"))
120
121 (defun notmuch-user-name ()
122   "Return the user.name value from the notmuch configuration."
123   (notmuch-config-get "user.name"))
124
125 (defun notmuch-user-primary-email ()
126   "Return the user.primary_email value from the notmuch configuration."
127   (notmuch-config-get "user.primary_email"))
128
129 (defun notmuch-user-other-email ()
130   "Return the user.other_email value (as a list) from the notmuch configuration."
131   (split-string (notmuch-config-get "user.other_email") "\n"))
132
133 (defun notmuch-kill-this-buffer ()
134   "Kill the current buffer."
135   (interactive)
136   (kill-buffer (current-buffer)))
137
138 (defun notmuch-prettify-subject (subject)
139   ;; This function is used by `notmuch-search-process-filter' which
140   ;; requires that we not disrupt its' matching state.
141   (save-match-data
142     (if (and subject
143              (string-match "^[ \t]*$" subject))
144         "[No Subject]"
145       subject)))
146
147 (defun notmuch-id-to-query (id)
148   "Return a query that matches the message with id ID."
149   (concat "id:\"" (replace-regexp-in-string "\"" "\"\"" id t t) "\""))
150
151 ;;
152
153 (defun notmuch-common-do-stash (text)
154   "Common function to stash text in kill ring, and display in minibuffer."
155   (kill-new text)
156   (message "Stashed: %s" text))
157
158 ;;
159
160 (defun notmuch-remove-if-not (predicate list)
161   "Return a copy of LIST with all items not satisfying PREDICATE removed."
162   (let (out)
163     (while list
164       (when (funcall predicate (car list))
165         (push (car list) out))
166       (setq list (cdr list)))
167     (nreverse out)))
168
169 ;; This lets us avoid compiling these replacement functions when emacs
170 ;; is sufficiently new enough to supply them alone. We do the macro
171 ;; treatment rather than just wrapping our defun calls in a when form
172 ;; specifically so that the compiler never sees the code on new emacs,
173 ;; (since the code is triggering warnings that we don't know how to get
174 ;; rid of.
175 ;;
176 ;; A more clever macro here would accept a condition and a list of forms.
177 (defmacro compile-on-emacs-prior-to-23 (form)
178   "Conditionally evaluate form only on emacs < emacs-23."
179   (list 'when (< emacs-major-version 23)
180         form))
181
182 (defun notmuch-split-content-type (content-type)
183   "Split content/type into 'content' and 'type'"
184   (split-string content-type "/"))
185
186 (defun notmuch-match-content-type (t1 t2)
187   "Return t if t1 and t2 are matching content types, taking wildcards into account"
188   (let ((st1 (notmuch-split-content-type t1))
189         (st2 (notmuch-split-content-type t2)))
190     (if (or (string= (cadr st1) "*")
191             (string= (cadr st2) "*"))
192         ;; Comparison of content types should be case insensitive.
193         (string= (downcase (car st1)) (downcase (car st2)))
194       (string= (downcase t1) (downcase t2)))))
195
196 (defvar notmuch-multipart/alternative-discouraged
197   '(
198     ;; Avoid HTML parts.
199     "text/html"
200     ;; multipart/related usually contain a text/html part and some associated graphics.
201     "multipart/related"
202     ))
203
204 (defun notmuch-multipart/alternative-choose (types)
205   "Return a list of preferred types from the given list of types"
206   ;; Based on `mm-preferred-alternative-precedence'.
207   (let ((seq types))
208     (dolist (pref (reverse notmuch-multipart/alternative-discouraged))
209       (dolist (elem (copy-sequence seq))
210         (when (string-match pref elem)
211           (setq seq (nconc (delete elem seq) (list elem))))))
212     seq))
213
214 (defun notmuch-parts-filter-by-type (parts type)
215   "Given a list of message parts, return a list containing the ones matching
216 the given type."
217   (remove-if-not
218    (lambda (part) (notmuch-match-content-type (plist-get part :content-type) type))
219    parts))
220
221 ;; Helper for parts which are generally not included in the default
222 ;; JSON output.
223 (defun notmuch-get-bodypart-internal (query part-number process-crypto)
224   (let ((args '("show" "--format=raw"))
225         (part-arg (format "--part=%s" part-number)))
226     (setq args (append args (list part-arg)))
227     (if process-crypto
228         (setq args (append args '("--decrypt"))))
229     (setq args (append args (list query)))
230     (with-temp-buffer
231       (let ((coding-system-for-read 'no-conversion))
232         (progn
233           (apply 'call-process (append (list notmuch-command nil (list t nil) nil) args))
234           (buffer-string))))))
235
236 (defun notmuch-get-bodypart-content (msg part nth process-crypto)
237   (or (plist-get part :content)
238       (notmuch-get-bodypart-internal (notmuch-id-to-query (plist-get msg :id)) nth process-crypto)))
239
240 ;; Converts a plist of headers to an alist of headers. The input plist should
241 ;; have symbols of the form :Header as keys, and the resulting alist will have
242 ;; symbols of the form 'Header as keys.
243 (defun notmuch-headers-plist-to-alist (plist)
244   (loop for (key value . rest) on plist by #'cddr
245         collect (cons (intern (substring (symbol-name key) 1)) value)))
246
247 ;; Compatibility functions for versions of emacs before emacs 23.
248 ;;
249 ;; Both functions here were copied from emacs 23 with the following copyright:
250 ;;
251 ;; Copyright (C) 1985, 1986, 1992, 1994, 1995, 1999, 2000, 2001, 2002, 2003,
252 ;;   2004, 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
253 ;;
254 ;; and under the GPL version 3 (or later) exactly as notmuch itself.
255 (compile-on-emacs-prior-to-23
256  (defun apply-partially (fun &rest args)
257    "Return a function that is a partial application of FUN to ARGS.
258 ARGS is a list of the first N arguments to pass to FUN.
259 The result is a new function which does the same as FUN, except that
260 the first N arguments are fixed at the values with which this function
261 was called."
262    (lexical-let ((fun fun) (args1 args))
263      (lambda (&rest args2) (apply fun (append args1 args2))))))
264
265 (compile-on-emacs-prior-to-23
266  (defun mouse-event-p (object)
267    "Return non-nil if OBJECT is a mouse click event."
268    (memq (event-basic-type object) '(mouse-1 mouse-2 mouse-3 mouse-movement))))
269
270 ;; This variable is used only buffer local, but it needs to be
271 ;; declared globally first to avoid compiler warnings.
272 (defvar notmuch-show-process-crypto nil)
273 (make-variable-buffer-local 'notmuch-show-process-crypto)
274
275 (provide 'notmuch-lib)
276