]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-query.el
Merge Sebastian Spaeth's python bindings into bindings/python
[notmuch] / emacs / notmuch-query.el
1 ;; notmuch-query.el --- provide an emacs api to query notmuch
2 ;;
3 ;; Copyright © David Bremner
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: David Bremner <david@tethera.net>
21
22 (require 'notmuch-lib)
23 (require 'json)
24
25 (defun notmuch-query-get-threads (search-terms &rest options)
26   "Return a list of threads of messages matching SEARCH-TERMS.
27
28 A thread is a forest or list of trees. A tree is a two element
29 list where the first element is a message, and the second element
30 is a possibly empty forest of replies.
31 "
32   (let  ((args (append '("show" "--format=json") search-terms))
33          (json-object-type 'plist)
34          (json-array-type 'list)
35          (json-false 'nil))
36     (with-temp-buffer
37       (progn
38         (apply 'call-process (append (list notmuch-command nil t nil) args))
39         (goto-char (point-min))
40         (json-read)))))
41
42 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
43 ;; Mapping functions across collections of messages.
44
45 (defun notmuch-query-map-aux  (mapper function seq)
46   "private function to do the actual mapping and flattening"
47   (apply 'append
48          (mapcar
49            (lambda (tree)
50              (funcall mapper fn tree))
51            seq)))
52
53 (defun notmuch-query-map-threads (fn threads)
54   "apply FN to every thread in  THREADS. Flatten results to a list.
55
56 See the function notmuch-query-get-threads for more information."
57   (notmuch-query-map-aux 'notmuch-query-map-forest fn threads))
58
59 (defun notmuch-query-map-forest (fn forest)
60   "apply function to every message in a forest. Flatten results to a list.
61
62 See the function notmuch-query-get-threads for more information.
63 "
64   (notmuch-query-map-aux 'notmuch-query-map-tree fn forest))
65
66 (defun notmuch-query-map-tree (fn tree)
67   "Apply function FN to every message in TREE. Flatten results to a list
68
69 See the function notmuch-query-get-threads for more information."
70   (cons (funcall fn (car tree)) (notmuch-query-map-forest fn (cadr tree))))
71
72 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
73 ;; Predefined queries
74
75 (defun notmuch-query-get-message-ids (&rest search-terms)
76   "Return a list of message-ids of messages that match SEARCH-TERMS"
77   (notmuch-query-map-threads
78    (lambda (msg) (plist-get msg :id))
79    (notmuch-query-get-threads search-terms)))
80
81 (provide 'notmuch-query)