Re: [PATCH v4 13/16] add indexopts to notmuch python bindings.
[notmuch-archives.git] / 7e / 7bb3e23ff5b0b67498c9ed44e4b8eb74a84386
1 Return-Path: <bremner@pivot.cs.unb.ca>\r
2 X-Original-To: notmuch@notmuchmail.org\r
3 Delivered-To: notmuch@notmuchmail.org\r
4 Received: from localhost (localhost [127.0.0.1])\r
5         by olra.theworths.org (Postfix) with ESMTP id 8972B431FBF\r
6         for <notmuch@notmuchmail.org>; Wed, 24 Feb 2010 04:57:04 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -1.089\r
10 X-Spam-Level: \r
11 X-Spam-Status: No, score=-1.089 tagged_above=-999 required=5\r
12         tests=[AWL=-0.349, BAYES_20=-0.74] autolearn=ham\r
13 Received: from olra.theworths.org ([127.0.0.1])\r
14         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
15         with ESMTP id Z1yYQuLUzWe2 for <notmuch@notmuchmail.org>;\r
16         Wed, 24 Feb 2010 04:57:03 -0800 (PST)\r
17 Received: from pivot.cs.unb.ca (pivot.cs.unb.ca [131.202.240.57])\r
18         by olra.theworths.org (Postfix) with ESMTP id 58E8B431FBC\r
19         for <notmuch@notmuchmail.org>; Wed, 24 Feb 2010 04:57:03 -0800 (PST)\r
20 Received: from\r
21         fctnnbsc30w-142167166109.pppoe-dynamic.high-speed.nb.bellaliant.net\r
22         ([142.167.166.109] helo=rocinante.cs.unb.ca)\r
23         by pivot.cs.unb.ca with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32)\r
24         (Exim 4.69) (envelope-from <bremner@pivot.cs.unb.ca>)\r
25         id 1NkGnU-0004H2-6i; Wed, 24 Feb 2010 08:57:00 -0400\r
26 Received: from bremner by rocinante.cs.unb.ca with local (Exim 4.71)\r
27         (envelope-from <bremner@rocinante.cs.unb.ca>)\r
28         id 1NkGkV-0002OG-JP; Wed, 24 Feb 2010 08:53:55 -0400\r
29 From: david@tethera.net\r
30 To: notmuch@notmuchmail.org\r
31 Date: Wed, 24 Feb 2010 08:52:00 -0400\r
32 Message-Id: <1267015920-8999-1-git-send-email-david@tethera.net>\r
33 X-Mailer: git-send-email 1.6.5\r
34 In-Reply-To: <1261341081-26479-1-git-send-email-david@tethera.net>\r
35 References: <1261341081-26479-1-git-send-email-david@tethera.net>\r
36 X-Sender-Verified: bremner@pivot.cs.unb.ca\r
37 Cc: David Bremner <bremner@unb.ca>\r
38 Subject: [notmuch] [PATCH v2] notmuch-query.el: new file to support access\r
39         to the notmuch database.\r
40 X-BeenThere: notmuch@notmuchmail.org\r
41 X-Mailman-Version: 2.1.13\r
42 Precedence: list\r
43 List-Id: "Use and development of the notmuch mail system."\r
44         <notmuch.notmuchmail.org>\r
45 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
46         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
47 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
48 List-Post: <mailto:notmuch@notmuchmail.org>\r
49 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
50 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
51         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
52 X-List-Received-Date: Wed, 24 Feb 2010 12:57:04 -0000\r
53 \r
54 From: David Bremner <bremner@unb.ca>\r
55 \r
56 Initially this file provides one main function\r
57 notmuch-query-get-threads, which takes a set of search terms, and\r
58 returns a parsed set of matching threads as a lisp data structure.\r
59 \r
60 A set of notmuch-query-map-* functions are provided to help map\r
61 functions over the data structure.\r
62 \r
63 The function notmuch-query-get-message-ids uses this machinery to get\r
64 the set of message-ids matching a query.\r
65 \r
66 This patch relies on the emacs directory  patch of\r
67 <1265773528-30794-1-git-send-email-david@tethera.net>\r
68 ---\r
69 \r
70  This version is rebased against the json patches that eventually made\r
71  it into master. Oddly, no actual changes were required based on\r
72  Carl's change of "id" to "thread".   Some trailing whitespace was\r
73  also removed.\r
74 \r
75  emacs/Makefile.local   |    3 +-\r
76  emacs/notmuch-query.el |   89 ++++++++++++++++++++++++++++++++++++++++++++++++\r
77  2 files changed, 91 insertions(+), 1 deletions(-)\r
78  create mode 100644 emacs/notmuch-query.el\r
79 \r
80 diff --git a/emacs/Makefile.local b/emacs/Makefile.local\r
81 index c6ca142..6c87a60 100644\r
82 --- a/emacs/Makefile.local\r
83 +++ b/emacs/Makefile.local\r
84 @@ -1,6 +1,7 @@\r
85  dir=emacs\r
86  emacs_sources=                 \\r
87 -       $(dir)/notmuch.el\r
88 +       $(dir)/notmuch.el      \\r
89 +       $(dir)/notmuch-query.el\r
90  \r
91  emacs_bytecode=$(subst .el,.elc,$(emacs_sources))\r
92  \r
93 diff --git a/emacs/notmuch-query.el b/emacs/notmuch-query.el\r
94 new file mode 100644\r
95 index 0000000..86230ba\r
96 --- /dev/null\r
97 +++ b/emacs/notmuch-query.el\r
98 @@ -0,0 +1,89 @@\r
99 +; notmuch.el --- run notmuch within emacs\r
100 +;\r
101 +; Copyright © David Bremner\r
102 +;\r
103 +; This file is part of Notmuch.\r
104 +;\r
105 +; Notmuch is free software: you can redistribute it and/or modify it\r
106 +; under the terms of the GNU General Public License as published by\r
107 +; the Free Software Foundation, either version 3 of the License, or\r
108 +; (at your option) any later version.\r
109 +;\r
110 +; Notmuch is distributed in the hope that it will be useful, but\r
111 +; WITHOUT ANY WARRANTY; without even the implied warranty of\r
112 +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
113 +; General Public License for more details.\r
114 +;\r
115 +; You should have received a copy of the GNU General Public License\r
116 +; along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.\r
117 +;\r
118 +; Authors: David Bremner <david@tethera.net>\r
119 +\r
120 +(require 'json)\r
121 +\r
122 +(defun notmuch-query-get-threads (search-terms &rest options)\r
123 +  "Return a list of threads of messages matching SEARCH-TERMS.\r
124 +\r
125 +A thread is a forest or list of trees. A tree is a two element\r
126 +list where the first element is a message, and the second element\r
127 +is a possibly empty forest of replies.\r
128 +"\r
129 +  (let  ((args (append '("show" "--format=json") search-terms))\r
130 +        (json-object-type 'plist)\r
131 +        (json-array-type 'list)\r
132 +        (json-false 'nil))\r
133 +    (with-temp-buffer\r
134 +      (progn\r
135 +       (apply 'call-process (append (list notmuch-command nil t nil) args))\r
136 +       (goto-char (point-min))\r
137 +       (json-read)))))\r
138 +\r
139 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r
140 +;; Mapping functions across collections of messages.\r
141 +\r
142 +(defun notmuch-query-map-aux  (mapper function seq)\r
143 +  "private function to do the actual mapping and flattening"\r
144 +\r
145 +  (apply 'append\r
146 +        (mapcar\r
147 +          (lambda (tree)\r
148 +            (funcall mapper fn tree))\r
149 +          seq)))\r
150 +\r
151 +\r
152 +(defun notmuch-query-map-threads (fn threads)\r
153 +  "apply FN to every thread in  THREADS. Flatten results to a list.\r
154 +\r
155 +See the function notmuch-query-get-threads for more information."\r
156 +\r
157 +  (notmuch-query-map-aux 'notmuch-query-map-forest fn threads))\r
158 +\r
159 +\r
160 +(defun notmuch-query-map-forest (fn forest)\r
161 +  "apply function to every message in a forest. Flatten results to a list.\r
162 +\r
163 +See the function notmuch-query-get-threads for more information.\r
164 +"\r
165 +\r
166 +  (notmuch-query-map-aux 'notmuch-query-map-tree fn forest))\r
167 +\r
168 +\r
169 +(defun notmuch-query-map-tree (fn tree)\r
170 +  "Apply function FN to every message in TREE. Flatten results to a list\r
171 +\r
172 +See the function notmuch-query-get-threads for more information."\r
173 +\r
174 +  (cons (funcall fn (car tree)) (notmuch-query-map-forest fn (cadr tree))))\r
175 +\r
176 +\r
177 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r
178 +;; Predefined queries\r
179 +\r
180 +(defun notmuch-query-get-message-ids (&rest search-terms)\r
181 +  "Return a list of message-ids of messages that match SEARCH-TERMS"\r
182 +\r
183 +  (notmuch-query-map-threads\r
184 +   (lambda (msg) (plist-get msg :id))\r
185 +   (notmuch-query-get-threads search-terms)))\r
186 +\r
187 +(provide 'notmuch-query)\r
188 -- \r
189 1.6.5\r
190 \r