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 <https://www.gnu.org/licenses/>.
20 ;; Authors: David Bremner <david@tethera.net>
24 (require 'notmuch-lib)
26 (defun notmuch-query-get-threads (search-terms)
27 "Return a list of threads of messages matching SEARCH-TERMS.
29 A thread is a forest or list of trees. A tree is a two element
30 list where the first element is a message, and the second element
31 is a possibly empty forest of replies."
32 (let ((args '("show" "--format=sexp" "--format-version=4")))
33 (when notmuch-show-process-crypto
34 (setq args (append args '("--decrypt=true"))))
35 (setq args (append args search-terms))
36 (apply #'notmuch-call-notmuch-sexp args)))
38 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
39 ;; Mapping functions across collections of messages.
41 (defun notmuch-query-map-aux (mapper function seq)
42 "Private function to do the actual mapping and flattening."
46 (funcall mapper function tree))
49 (defun notmuch-query-map-threads (fn threads)
50 "Apply function FN to every thread in THREADS.
51 Flatten results to a list. See the function
52 `notmuch-query-get-threads' for more information."
53 (notmuch-query-map-aux 'notmuch-query-map-forest fn threads))
55 (defun notmuch-query-map-forest (fn forest)
56 "Apply function FN to every message in FOREST.
57 Flatten results to a list. See the function
58 `notmuch-query-get-threads' for more information."
59 (notmuch-query-map-aux 'notmuch-query-map-tree fn forest))
61 (defun notmuch-query-map-tree (fn tree)
62 "Apply function FN to every message in TREE.
63 Flatten results to a list. See the function
64 `notmuch-query-get-threads' for more information."
65 (cons (funcall fn (car tree)) (notmuch-query-map-forest fn (cadr tree))))
67 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
70 (defun notmuch-query-get-message-ids (&rest search-terms)
71 "Return a list of message-ids of messages that match SEARCH-TERMS."
72 (notmuch-query-map-threads
73 (lambda (msg) (plist-get msg :id))
74 (notmuch-query-get-threads search-terms)))
76 (provide 'notmuch-query)
78 ;;; notmuch-query.el ends here