From: Mark Walters Date: Sun, 29 Jul 2012 19:30:28 +0000 (+0100) Subject: Re: [PATCH] emacs: move async json parser to its own function X-Git-Url: http://git.tremily.us/gitweb.cgi?a=commitdiff_plain;h=9769a47db255782412063566b9f92b794c79d803;p=notmuch-archives.git Re: [PATCH] emacs: move async json parser to its own function --- diff --git a/e7/8199c51ae8a5d3a070326bb276912688774055 b/e7/8199c51ae8a5d3a070326bb276912688774055 new file mode 100644 index 000000000..e53e76871 --- /dev/null +++ b/e7/8199c51ae8a5d3a070326bb276912688774055 @@ -0,0 +1,199 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 68ED9431FAF + for ; Sun, 29 Jul 2012 12:30:38 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -1.098 +X-Spam-Level: +X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5 + tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001, + NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id 46HhlBRGLICn for ; + Sun, 29 Jul 2012 12:30:36 -0700 (PDT) +Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6]) + (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) + (No client certificate requested) + by olra.theworths.org (Postfix) with ESMTPS id 45B68431FAE + for ; Sun, 29 Jul 2012 12:30:36 -0700 (PDT) +Received: from smtp.qmul.ac.uk ([138.37.6.40]) + by mail2.qmul.ac.uk with esmtp (Exim 4.71) + (envelope-from ) + id 1SvZCA-0000oX-R2; Sun, 29 Jul 2012 20:30:31 +0100 +Received: from 94-192-233-223.zone6.bethere.co.uk ([94.192.233.223] + helo=localhost) + by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69) + (envelope-from ) + id 1SvZCA-0007gM-E0; Sun, 29 Jul 2012 20:30:30 +0100 +From: Mark Walters +To: Austin Clements , notmuch@notmuchmail.org +Subject: Re: [PATCH] emacs: move async json parser to its own function +In-Reply-To: <87k3xo85tv.fsf@qmul.ac.uk> +References: <87k3xo85tv.fsf@qmul.ac.uk> +User-Agent: Notmuch/0.13.2+96~g634443c (http://notmuchmail.org) Emacs/23.4.1 + (x86_64-pc-linux-gnu) +Date: Sun, 29 Jul 2012 20:30:28 +0100 +Message-ID: <878ve2wejv.fsf@qmul.ac.uk> +MIME-Version: 1.0 +Content-Type: text/plain; charset=us-ascii +X-Sender-Host-Address: 94.192.233.223 +X-QM-SPAM-Info: Sender has good ham record. :) +X-QM-Body-MD5: 8b0bd039762d575cd45016ec350c2e25 (of first 20000 bytes) +X-SpamAssassin-Score: -1.8 +X-SpamAssassin-SpamBar: - +X-SpamAssassin-Report: The QM spam filters have analysed this message to + determine if it is + spam. We require at least 5.0 points to mark a message as spam. + This message scored -1.8 points. + Summary of the scoring: + * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/, + * medium trust + * [138.37.6.40 listed in list.dnswl.org] + * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail + provider * (markwalters1009[at]gmail.com) + * -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay + * domain + * 0.5 AWL AWL: From: address is in the auto white-list +X-QM-Scan-Virus: ClamAV says the message is clean +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Sun, 29 Jul 2012 19:30:38 -0000 + + +Hi + +This patch is buggy as it doesn't take buffer local variables from the +right buffer (so it goes wrong if the buffer changes while the process +is still running). I have a patch which seems to be correct but I want +to do a bit more testing before posting. + +Best wishes + +Mark + + + +On Sat, 28 Jul 2012, Mark Walters wrote: +> We separate out the json parser into its own function. +> --- +> +> Hi +> +> Notmuch pick uses the new asynchronous json parser and the code to do so +> is almost identical to that for the search mode. Thus separate out the +> parsing in search mode into a more general function that can easily be +> used by both pick and search. +> +> This saves nearly 50 lines of duplicated code in notmuch-pick.el. +> +> The function notmuch-json-async-parse should probably be move in +> notmuch-lib but that can be a follow on patch. +> +> Best wishes +> +> Mark +> +> emacs/notmuch.el | 46 ++++++++++++++++++++++++++++++++++++---------- +> 1 files changed, 36 insertions(+), 10 deletions(-) +> +> diff --git a/emacs/notmuch.el b/emacs/notmuch.el +> index fd1836f..ee01028 100644 +> --- a/emacs/notmuch.el +> +++ b/emacs/notmuch.el +> @@ -816,7 +816,32 @@ non-authors is found, assume that all of the authors match." +> "Incremental JSON parser for the search process filter.") +> +> (defun notmuch-search-process-filter (proc string) +> - "Process and filter the output of \"notmuch search\"" +> + "Process and filter the output of \"notmuch search\" using the asynchronous parser." +> + (setq notmuch-search-process-state +> + (notmuch-json-async-parse proc +> + string +> + notmuch-search-process-state +> + notmuch-search-json-parser +> + 'notmuch-search-show-result +> + 'notmuch-search-show-error))) +> + +> +(defun notmuch-json-async-parse (proc string process-state parser result-function error-function) +> + "Process and filter the output using the asynchronous parser. +> + +> +This function steps into the first level of JSON nesting and then +> +applies RESULT-FUNCTION to each complete JSON object as it comes +> +in. +> + +> +PROC is the process: it should have a results buffer as +> +process-buffer and a 'parse-buf for the incoming json. +> +PROCESS-STATE the current state of filter process +> +STRING the incoming data +> +PARSER the parser +> +RESULT-FUNCTION a function to call on complete pieces of json +> +ERROR-FUNCTION the function to call on errors +> + +> +The function returns the new PROCESS-STATE" +> + +> (let ((results-buf (process-buffer proc)) +> (parse-buf (process-get proc 'parse-buf)) +> (inhibit-read-only t) +> @@ -831,28 +856,28 @@ non-authors is found, assume that all of the authors match." +> (with-current-buffer results-buf +> (while (not done) +> (condition-case nil +> - (case notmuch-search-process-state +> + (case process-state +> ((begin) +> ;; Enter the results list +> (if (eq (notmuch-json-begin-compound +> - notmuch-search-json-parser) 'retry) +> + parser) 'retry) +> (setq done t) +> - (setq notmuch-search-process-state 'result))) +> + (setq process-state 'result))) +> ((result) +> ;; Parse a result +> - (let ((result (notmuch-json-read notmuch-search-json-parser))) +> + (let ((result (notmuch-json-read parser))) +> (case result +> ((retry) (setq done t)) +> - ((end) (setq notmuch-search-process-state 'end)) +> - (otherwise (notmuch-search-show-result result))))) +> + ((end) (setq process-state 'end)) +> + (otherwise (funcall result-function result))))) +> ((end) +> ;; Any trailing data is unexpected +> - (notmuch-json-eof notmuch-search-json-parser) +> + (notmuch-json-eof parser) +> (setq done t))) +> (json-error +> ;; Do our best to resynchronize and ensure forward +> ;; progress +> - (notmuch-search-show-error +> + (funcall error-function +> "%s" +> (with-current-buffer parse-buf +> (let ((bad (buffer-substring (line-beginning-position) +> @@ -861,7 +886,8 @@ non-authors is found, assume that all of the authors match." +> bad)))))) +> ;; Clear out what we've parsed +> (with-current-buffer parse-buf +> - (delete-region (point-min) (point))))))) +> + (delete-region (point-min) (point)))) +> + process-state))) +> +> (defun notmuch-search-tag-all (&optional tag-changes) +> "Add/remove tags from all messages in current search buffer. +> -- +> 1.7.9.1