]> git.notmuchmail.org Git - notmuch/blob - emacs/notmuch-query.el
emacs: Add new option notmuch-search-hide-excluded
[notmuch] / emacs / notmuch-query.el
1 ;;; notmuch-query.el --- provide an emacs api to query notmuch  -*- lexical-binding: t -*-
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 <https://www.gnu.org/licenses/>.
19 ;;
20 ;; Authors: David Bremner <david@tethera.net>
21
22 ;;; Code:
23
24 (require 'notmuch-lib)
25
26 ;;; Basic query function
27
28 (defun notmuch-query-get-threads (search-terms)
29   "Return a list of threads of messages matching SEARCH-TERMS.
30
31 A thread is a forest or list of trees. A tree is a two element
32 list where the first element is a message, and the second element
33 is a possibly empty forest of replies."
34   (let ((args '("show" "--format=sexp" "--format-version=5")))
35     (when notmuch-show-process-crypto
36       (setq args (append args '("--decrypt=true"))))
37     (setq args (append args search-terms))
38     (apply #'notmuch-call-notmuch-sexp args)))
39
40 ;;; Mapping functions across collections of messages
41
42 (defun notmuch-query-map-aux  (mapper function seq)
43   "Private function to do the actual mapping and flattening."
44   (cl-mapcan (lambda (tree)
45                (funcall mapper function tree))
46              seq))
47
48 (defun notmuch-query-map-threads (fn threads)
49   "Apply function FN to every thread in THREADS.
50 Flatten results to a list.  See the function
51 `notmuch-query-get-threads' for more information."
52   (notmuch-query-map-aux 'notmuch-query-map-forest fn threads))
53
54 (defun notmuch-query-map-forest (fn forest)
55   "Apply function FN to every message in FOREST.
56 Flatten results to a list.  See the function
57 `notmuch-query-get-threads' for more information."
58   (notmuch-query-map-aux 'notmuch-query-map-tree fn forest))
59
60 (defun notmuch-query-map-tree (fn tree)
61   "Apply function FN to every message in TREE.
62 Flatten results to a list.  See the function
63 `notmuch-query-get-threads' for more information."
64   (cons (funcall fn (car tree))
65         (notmuch-query-map-forest fn (cadr tree))))
66
67 ;;; Predefined queries
68
69 (defun notmuch-query-get-message-ids (&rest search-terms)
70   "Return a list of message-ids of messages that match SEARCH-TERMS."
71   (notmuch-query-map-threads
72    (lambda (msg) (plist-get msg :id))
73    (notmuch-query-get-threads search-terms)))
74
75 (provide 'notmuch-query)
76
77 ;;; notmuch-query.el ends here