[notmuch] [PATCH] Do not segfault on empty mime parts
[notmuch-archives.git] / ca / a83d44c0585cae1aefd76d0f5e1466aebf3884
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 2EE0C431FC3\r
6         for <notmuch@notmuchmail.org>; Sun, 20 Dec 2009 12:31:46 -0800 (PST)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 Received: from olra.theworths.org ([127.0.0.1])\r
9         by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
10         with ESMTP id G2+nb504FIiW for <notmuch@notmuchmail.org>;\r
11         Sun, 20 Dec 2009 12:31:45 -0800 (PST)\r
12 Received: from pivot.cs.unb.ca (pivot.cs.unb.ca [131.202.240.57])\r
13         by olra.theworths.org (Postfix) with ESMTP id 3429C431FBF\r
14         for <notmuch@notmuchmail.org>; Sun, 20 Dec 2009 12:31:45 -0800 (PST)\r
15 Received: from\r
16         fctnnbsc30w-142167182194.pppoe-dynamic.high-speed.nb.bellaliant.net\r
17         ([142.167.182.194] helo=localhost)\r
18         by pivot.cs.unb.ca with esmtpsa (TLS1.0:RSA_AES_256_CBC_SHA1:32)\r
19         (Exim 4.69) (envelope-from <bremner@pivot.cs.unb.ca>)\r
20         id 1NMSRM-00083k-2B; Sun, 20 Dec 2009 16:31:44 -0400\r
21 Received: from bremner by localhost with local (Exim 4.71)\r
22         (envelope-from <bremner@pivot.cs.unb.ca>)\r
23         id 1NMSRG-0006tn-Hr; Sun, 20 Dec 2009 16:31:38 -0400\r
24 From: david@tethera.net\r
25 To: notmuch@notmuchmail.org\r
26 Date: Sun, 20 Dec 2009 16:31:21 -0400\r
27 Message-Id: <1261341081-26479-1-git-send-email-david@tethera.net>\r
28 X-Mailer: git-send-email 1.6.5.7\r
29 In-Reply-To: <1261234524-25522-2-git-send-email-david@tethera.net>\r
30 References: <1261234524-25522-2-git-send-email-david@tethera.net>\r
31 X-Sender-Verified: bremner@pivot.cs.unb.ca\r
32 Cc: David Bremner <bremner@unb.ca>\r
33 Subject: [notmuch] [PATCH] notmuch-query.el: new file to support access to\r
34         the notmuch database.\r
35 X-BeenThere: notmuch@notmuchmail.org\r
36 X-Mailman-Version: 2.1.12\r
37 Precedence: list\r
38 List-Id: "Use and development of the notmuch mail system."\r
39         <notmuch.notmuchmail.org>\r
40 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
41         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
42 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
43 List-Post: <mailto:notmuch@notmuchmail.org>\r
44 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
45 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
46         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
47 X-List-Received-Date: Sun, 20 Dec 2009 20:31:46 -0000\r
48 \r
49 From: David Bremner <bremner@unb.ca>\r
50 \r
51 Initially this file provides one main function\r
52 notmuch-query-get-threads, which takes a set of search terms, and\r
53 returns a parsed set of matching threads as a lisp data structure.\r
54 \r
55 A set of notmuch-query-map-* functions are provided to help map\r
56 functions over the data structure.\r
57 \r
58 The function notmuch-query-get-message-ids uses this machinery to get\r
59 the set of message-ids matching a query.\r
60 \r
61 This patch relies on the json patch of\r
62 <1261141195-5469-1-git-send-email-david@tethera.net> and the argument\r
63 parsing patch 1261234524-25522-2-git-send-email-david@tethera.net,\r
64 although the latter would be trivial to avoid, by replace --format\r
65 with --output in the file.\r
66 ---\r
67 \r
68 This patch is the start of a new emacs api built on top of the json\r
69 output.  It roughly mimics the notion of returning lists of messages\r
70 and traversing them as used in the C code.  If nothing else, the\r
71 docstring for notmuch-query-get-threads provides some documentation\r
72 explaining the structure of the json output by Scott's patch.\r
73 \r
74 I'd be interested if people who have extremely large threads find it\r
75 annoyingly slow.  The plan (subject to change as always) is to allow\r
76 the api functions (currently only notmuch-query-get-message-ids) to\r
77 pass options to the invokation of "notmuch show" to trim down the json\r
78 output (i.e. omit message bodies). The parsing and traversal code\r
79 should all unaffected by this change. In theory.\r
80 \r
81 I didn't know if you (Carl) want to do copyright assignment, or just\r
82 the GPL license headers are enough. I don't mind pretty much any\r
83 variation.\r
84 \r
85  notmuch-query.el |   89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++\r
86  1 files changed, 89 insertions(+), 0 deletions(-)\r
87  create mode 100644 notmuch-query.el\r
88 \r
89 diff --git a/notmuch-query.el b/notmuch-query.el\r
90 new file mode 100644\r
91 index 0000000..5911fa6\r
92 --- /dev/null\r
93 +++ b/notmuch-query.el\r
94 @@ -0,0 +1,89 @@\r
95 +; notmuch.el --- run notmuch within emacs\r
96 +;\r
97 +; Copyright © David Bremner\r
98 +;\r
99 +; This file is part of Notmuch.\r
100 +;\r
101 +; Notmuch is free software: you can redistribute it and/or modify it\r
102 +; under the terms of the GNU General Public License as published by\r
103 +; the Free Software Foundation, either version 3 of the License, or\r
104 +; (at your option) any later version.\r
105 +;\r
106 +; Notmuch is distributed in the hope that it will be useful, but\r
107 +; WITHOUT ANY WARRANTY; without even the implied warranty of\r
108 +; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
109 +; General Public License for more details.\r
110 +;\r
111 +; You should have received a copy of the GNU General Public License\r
112 +; along with Notmuch.  If not, see <http://www.gnu.org/licenses/>.\r
113 +;\r
114 +; Authors: David Bremner <david@tethera.net>\r
115 +\r
116 +(require 'json)\r
117 +\r
118 +(defun notmuch-query-get-threads (search-terms &rest options)\r
119 +  "Return a list of threads of messages matching SEARCH-TERMS. \r
120 +\r
121 +A thread is a forest or list of trees. A tree is a two element\r
122 +list where the first element is a message, and the second element\r
123 +is a possibly empty forest of replies.\r
124 +"\r
125 +  (let  ((args (append '("show" "--format=json") search-terms))\r
126 +        (json-object-type 'plist)\r
127 +        (json-array-type 'list)\r
128 +        (json-false 'nil))\r
129 +    (with-temp-buffer \r
130 +      (progn \r
131 +       (apply 'call-process (append (list notmuch-command nil t nil) args))\r
132 +       (goto-char (point-min))\r
133 +       (json-read)))))\r
134 +\r
135 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r
136 +;; Mapping functions across collections of messages.\r
137 +\r
138 +(defun notmuch-query-map-aux  (mapper function seq)\r
139 +  "private function to do the actual mapping and flattening"\r
140 +\r
141 +  (apply 'append \r
142 +        (mapcar \r
143 +          (lambda (tree) \r
144 +            (funcall mapper fn tree))\r
145 +          seq)))\r
146 +\r
147 +\r
148 +(defun notmuch-query-map-threads (fn threads)\r
149 +  "apply FN to every thread in  THREADS. Flatten results to a list.\r
150 +\r
151 +See the function notmuch-query-get-threads for more information."\r
152 +\r
153 +  (notmuch-query-map-aux 'notmuch-query-map-forest fn threads))\r
154 +\r
155 +\r
156 +(defun notmuch-query-map-forest (fn forest)\r
157 +  "apply function to every message in a forest. Flatten results to a list.\r
158 +\r
159 +See the function notmuch-query-get-threads for more information.\r
160 +"\r
161 +\r
162 +  (notmuch-query-map-aux 'notmuch-query-map-tree fn forest))\r
163 +\r
164 +\r
165 +(defun notmuch-query-map-tree (fn tree)\r
166 +  "Apply function FN to every message in TREE. Flatten results to a list\r
167 +\r
168 +See the function notmuch-query-get-threads for more information."\r
169 +\r
170 +  (cons (funcall fn (car tree)) (notmuch-query-map-forest fn (cadr tree))))\r
171 +\r
172 +\r
173 +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;\r
174 +;; Predefined queries\r
175 +\r
176 +(defun notmuch-query-get-message-ids (search-terms)\r
177 +  "Return a list of message-ids of messages that match SEARCH-TERMS"\r
178 +\r
179 +  (notmuch-query-map-threads \r
180 +   (lambda (msg) (plist-get msg :id))\r
181 +   (notmuch-query-get-threads search-terms)))\r
182 +    \r
183 +(provide 'notmuch-query)\r
184 \ No newline at end of file\r
185 -- \r
186 1.6.5.7\r
187 \r