1 ;; notmuch-query.el --- provide an emacs api to query notmuch
3 ;; Copyright © David Bremner
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: David Bremner <david@tethera.net>
22 (require 'notmuch-lib)
25 (defun notmuch-query-get-threads (search-terms)
26 "Return a list of threads of messages matching SEARCH-TERMS.
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.
32 (let ((args '("show" "--format=json"))
33 (json-object-type 'plist)
34 (json-array-type 'list)
36 (if notmuch-show-process-crypto
37 (setq args (append args '("--decrypt"))))
38 (setq args (append args search-terms))
41 (apply 'call-process (append (list notmuch-command nil (list t nil) nil) args))
42 (goto-char (point-min))
45 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46 ;; Mapping functions across collections of messages.
48 (defun notmuch-query-map-aux (mapper function seq)
49 "private function to do the actual mapping and flattening"
53 (funcall mapper function tree))
56 (defun notmuch-query-map-threads (fn threads)
57 "apply FN to every thread in THREADS. Flatten results to a list.
59 See the function notmuch-query-get-threads for more information."
60 (notmuch-query-map-aux 'notmuch-query-map-forest fn threads))
62 (defun notmuch-query-map-forest (fn forest)
63 "apply function to every message in a forest. Flatten results to a list.
65 See the function notmuch-query-get-threads for more information.
67 (notmuch-query-map-aux 'notmuch-query-map-tree fn forest))
69 (defun notmuch-query-map-tree (fn tree)
70 "Apply function FN to every message in TREE. Flatten results to a list
72 See the function notmuch-query-get-threads for more information."
73 (cons (funcall fn (car tree)) (notmuch-query-map-forest fn (cadr tree))))
75 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
78 (defun notmuch-query-get-message-ids (&rest search-terms)
79 "Return a list of message-ids of messages that match SEARCH-TERMS"
80 (notmuch-query-map-threads
81 (lambda (msg) (plist-get msg :id))
82 (notmuch-query-get-threads search-terms)))
84 (provide 'notmuch-query)