1 Return-Path: <m.walters@qmul.ac.uk>
\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 DFCF5431FAF
\r
6 for <notmuch@notmuchmail.org>; Mon, 30 Jul 2012 13:35:41 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5
\r
12 tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,
\r
13 NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled
\r
14 Received: from olra.theworths.org ([127.0.0.1])
\r
15 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
16 with ESMTP id eu4rvh0TM589 for <notmuch@notmuchmail.org>;
\r
17 Mon, 30 Jul 2012 13:35:37 -0700 (PDT)
\r
18 Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])
\r
19 (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))
\r
20 (No client certificate requested)
\r
21 by olra.theworths.org (Postfix) with ESMTPS id 83CC2431FAE
\r
22 for <notmuch@notmuchmail.org>; Mon, 30 Jul 2012 13:35:37 -0700 (PDT)
\r
23 Received: from smtp.qmul.ac.uk ([138.37.6.40])
\r
24 by mail2.qmul.ac.uk with esmtp (Exim 4.71)
\r
25 (envelope-from <m.walters@qmul.ac.uk>)
\r
26 id 1Svwge-0001si-9F; Mon, 30 Jul 2012 21:35:32 +0100
\r
27 Received: from 94-192-233-223.zone6.bethere.co.uk ([94.192.233.223]
\r
29 by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69)
\r
30 (envelope-from <m.walters@qmul.ac.uk>)
\r
31 id 1Svwgd-00022y-R5; Mon, 30 Jul 2012 21:35:32 +0100
\r
32 From: Mark Walters <markwalters1009@gmail.com>
\r
33 To: Austin Clements <amdragon@MIT.EDU>
\r
34 Subject: Re: [PATCH] emacs: move async json parser to its own function
\r
35 In-Reply-To: <20120730013506.GF8502@mit.edu>
\r
36 References: <87k3xo85tv.fsf@qmul.ac.uk> <20120730013506.GF8502@mit.edu>
\r
37 User-Agent: Notmuch/0.13.2+96~g634443c (http://notmuchmail.org) Emacs/23.4.1
\r
38 (x86_64-pc-linux-gnu)
\r
39 Date: Mon, 30 Jul 2012 21:35:30 +0100
\r
40 Message-ID: <87obmxyokt.fsf@qmul.ac.uk>
\r
42 Content-Type: text/plain; charset=us-ascii
\r
43 X-Sender-Host-Address: 94.192.233.223
\r
44 X-QM-SPAM-Info: Sender has good ham record. :)
\r
45 X-QM-Body-MD5: 2a8679cc1c65562ecb10fffbf0b0b313 (of first 20000 bytes)
\r
46 X-SpamAssassin-Score: -1.8
\r
47 X-SpamAssassin-SpamBar: -
\r
48 X-SpamAssassin-Report: The QM spam filters have analysed this message to
\r
50 spam. We require at least 5.0 points to mark a message as spam.
\r
51 This message scored -1.8 points.
\r
52 Summary of the scoring:
\r
53 * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/,
\r
55 * [138.37.6.40 listed in list.dnswl.org]
\r
56 * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail
\r
57 provider * (markwalters1009[at]gmail.com)
\r
58 * -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay
\r
60 * 0.5 AWL AWL: From: address is in the auto white-list
\r
61 X-QM-Scan-Virus: ClamAV says the message is clean
\r
62 Cc: notmuch@notmuchmail.org
\r
63 X-BeenThere: notmuch@notmuchmail.org
\r
64 X-Mailman-Version: 2.1.13
\r
66 List-Id: "Use and development of the notmuch mail system."
\r
67 <notmuch.notmuchmail.org>
\r
68 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
69 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
70 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
71 List-Post: <mailto:notmuch@notmuchmail.org>
\r
72 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
73 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
74 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
75 X-List-Received-Date: Mon, 30 Jul 2012 20:35:42 -0000
\r
77 On Mon, 30 Jul 2012, Austin Clements <amdragon@MIT.EDU> wrote:
\r
78 > This seems like a good idea, but as a generic interface, this seems
\r
79 > suboptimal. In particular, it seems odd that a parsing function would
\r
80 > have to know about a process and require the caller to set up various
\r
81 > process properties and buffer-local variables. What about something
\r
82 > dedicated to incrementally parsing lists, like the async parser but
\r
83 > more specialized? Something along the lines of,
\r
85 > (defun notmuch-json-parse-partial-list (result-function error-function
\r
87 > "Parse a partial JSON list from BUFFER (or the current buffer).
\r
89 > This function consumes a JSON list from BUFFER, applying
\r
90 > RESULT-FUNCTION to each complete value in the list. It operates
\r
91 > incrementally and should be called whenever the buffer has been
\r
92 > extended with additional data.
\r
94 > If there is a syntax error, this will attempt to resynchronize with
\r
95 > the input and will apply ERROR-FUNCTION to any input that was
\r
98 > This calls RESULT-FUNCTION and ERROR-FUNCTION with the same current
\r
99 > buffer as this function is called with (that is, this function does
\r
100 > not visibly switch to BUFFER)."
\r
103 > This could use buffer-local variables for tracking the async parser
\r
104 > state as well its own state. It could even set these up automatically
\r
105 > when called for the first time in a buffer without them set, making it
\r
106 > very DWIM. It would clearly require a little more help from the
\r
107 > caller for process management than your patch does (and a little less
\r
108 > for parser setup), but I think the genericity would be worth it.
\r
110 This all seems good: I will send a draft patch in a reply to this
\r
111 email. The one change I have made is to make the function called with
\r
112 current-buffer the parse-buffer and give the results-buffer as an
\r
113 argument. This seems more natural to me as the caller has probably just
\r
114 added data to the parse-buffer, and it slightly simplifies the function.
\r
116 The other change from the current state is that the parser and state
\r
117 variable are buffer local to the parse-buffer rather than the
\r
126 > Quoth Mark Walters on Jul 28 at 12:48 pm:
\r
128 >> We separate out the json parser into its own function.
\r
133 >> Notmuch pick uses the new asynchronous json parser and the code to do so
\r
134 >> is almost identical to that for the search mode. Thus separate out the
\r
135 >> parsing in search mode into a more general function that can easily be
\r
136 >> used by both pick and search.
\r
138 >> This saves nearly 50 lines of duplicated code in notmuch-pick.el.
\r
140 >> The function notmuch-json-async-parse should probably be move in
\r
141 >> notmuch-lib but that can be a follow on patch.
\r
147 >> emacs/notmuch.el | 46 ++++++++++++++++++++++++++++++++++++----------
\r
148 >> 1 files changed, 36 insertions(+), 10 deletions(-)
\r
150 >> diff --git a/emacs/notmuch.el b/emacs/notmuch.el
\r
151 >> index fd1836f..ee01028 100644
\r
152 >> --- a/emacs/notmuch.el
\r
153 >> +++ b/emacs/notmuch.el
\r
154 >> @@ -816,7 +816,32 @@ non-authors is found, assume that all of the authors match."
\r
155 >> "Incremental JSON parser for the search process filter.")
\r
157 >> (defun notmuch-search-process-filter (proc string)
\r
158 >> - "Process and filter the output of \"notmuch search\""
\r
159 >> + "Process and filter the output of \"notmuch search\" using the asynchronous parser."
\r
160 >> + (setq notmuch-search-process-state
\r
161 >> + (notmuch-json-async-parse proc
\r
163 >> + notmuch-search-process-state
\r
164 >> + notmuch-search-json-parser
\r
165 >> + 'notmuch-search-show-result
\r
166 >> + 'notmuch-search-show-error)))
\r
168 >> +(defun notmuch-json-async-parse (proc string process-state parser result-function error-function)
\r
169 >> + "Process and filter the output using the asynchronous parser.
\r
171 >> +This function steps into the first level of JSON nesting and then
\r
172 >> +applies RESULT-FUNCTION to each complete JSON object as it comes
\r
175 >> +PROC is the process: it should have a results buffer as
\r
176 >> +process-buffer and a 'parse-buf for the incoming json.
\r
177 >> +PROCESS-STATE the current state of filter process
\r
178 >> +STRING the incoming data
\r
179 >> +PARSER the parser
\r
180 >> +RESULT-FUNCTION a function to call on complete pieces of json
\r
181 >> +ERROR-FUNCTION the function to call on errors
\r
183 >> +The function returns the new PROCESS-STATE"
\r
185 >> (let ((results-buf (process-buffer proc))
\r
186 >> (parse-buf (process-get proc 'parse-buf))
\r
187 >> (inhibit-read-only t)
\r
188 >> @@ -831,28 +856,28 @@ non-authors is found, assume that all of the authors match."
\r
189 >> (with-current-buffer results-buf
\r
190 >> (while (not done)
\r
191 >> (condition-case nil
\r
192 >> - (case notmuch-search-process-state
\r
193 >> + (case process-state
\r
195 >> ;; Enter the results list
\r
196 >> (if (eq (notmuch-json-begin-compound
\r
197 >> - notmuch-search-json-parser) 'retry)
\r
198 >> + parser) 'retry)
\r
200 >> - (setq notmuch-search-process-state 'result)))
\r
201 >> + (setq process-state 'result)))
\r
203 >> ;; Parse a result
\r
204 >> - (let ((result (notmuch-json-read notmuch-search-json-parser)))
\r
205 >> + (let ((result (notmuch-json-read parser)))
\r
207 >> ((retry) (setq done t))
\r
208 >> - ((end) (setq notmuch-search-process-state 'end))
\r
209 >> - (otherwise (notmuch-search-show-result result)))))
\r
210 >> + ((end) (setq process-state 'end))
\r
211 >> + (otherwise (funcall result-function result)))))
\r
213 >> ;; Any trailing data is unexpected
\r
214 >> - (notmuch-json-eof notmuch-search-json-parser)
\r
215 >> + (notmuch-json-eof parser)
\r
218 >> ;; Do our best to resynchronize and ensure forward
\r
220 >> - (notmuch-search-show-error
\r
221 >> + (funcall error-function
\r
223 >> (with-current-buffer parse-buf
\r
224 >> (let ((bad (buffer-substring (line-beginning-position)
\r
225 >> @@ -861,7 +886,8 @@ non-authors is found, assume that all of the authors match."
\r
227 >> ;; Clear out what we've parsed
\r
228 >> (with-current-buffer parse-buf
\r
229 >> - (delete-region (point-min) (point)))))))
\r
230 >> + (delete-region (point-min) (point))))
\r
231 >> + process-state)))
\r
233 >> (defun notmuch-search-tag-all (&optional tag-changes)
\r
234 >> "Add/remove tags from all messages in current search buffer.
\r