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 B1D12431FBD for ; Mon, 30 Jul 2012 13:39:18 -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 f39rU0JAFBxS for ; Mon, 30 Jul 2012 13:39:18 -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 DD908431FAE for ; Mon, 30 Jul 2012 13:39:17 -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 1SvwkG-0002PL-4Q; Mon, 30 Jul 2012 21:39:16 +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 1SvwkF-0002Fs-IZ; Mon, 30 Jul 2012 21:39:15 +0100 From: Mark Walters To: Austin Clements Subject: [PATCH v2 (Draft)] emacs: split async json parser into utility function In-Reply-To: <87obmxyokt.fsf@qmul.ac.uk> References: <87k3xo85tv.fsf@qmul.ac.uk> <20120730013506.GF8502@mit.edu> <87obmxyokt.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: Mon, 30 Jul 2012 21:39:15 +0100 Message-ID: <87k3xlyoek.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: b2244b3c95e352eb68571d6b4d806deb (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 Cc: notmuch@notmuchmail.org 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: Mon, 30 Jul 2012 20:39:18 -0000 Split out the json parser into a utility function. --- Most of this patch is code movement: but I don't see how to arrange the patch to show that. Best wishes Mark emacs/notmuch.el | 101 +++++++++++++++++++++++++++++++----------------------- 1 files changed, 58 insertions(+), 43 deletions(-) diff --git a/emacs/notmuch.el b/emacs/notmuch.el index fd1836f..23ab499 100644 --- a/emacs/notmuch.el +++ b/emacs/notmuch.el @@ -809,11 +809,60 @@ non-authors is found, assume that all of the authors match." (insert (apply #'format string objects)) (insert "\n"))) -(defvar notmuch-search-process-state nil - "Parsing state of the search process filter.") - -(defvar notmuch-search-json-parser nil - "Incremental JSON parser for the search process filter.") +(defvar notmuch-json-state nil + "State of the JSON parser.") + +(defvar notmuch-json-parser nil + "Incremental JSON parser.") + +(defun notmuch-json-parse-partial-list (result-function error-function results-buf) + "Parse a partial JSON list from current buffer. + +This function consumes a JSON list from the current buffer, +applying RESULT-FUNCTION in buffer RESULT-BUFFER to each complete +value in the list. It operates incrementally and should be +called whenever the buffer has been extended with additional +data. + +If there is a syntax error, this will attempt to resynchronize +with the input and will apply ERROR-FUNCTION in buffer +RESULT-BUFFER to any input that was skipped." + (let (done) + (unless (local-variable-p 'notmuch-json-parser) + (set (make-local-variable 'notmuch-json-parser) + (notmuch-json-create-parser (current-buffer))) + (set (make-local-variable 'notmuch-json-state) 'begin)) + (while (not done) + (condition-case nil + (case notmuch-json-state + ((begin) + ;; Enter the results list + (if (eq (notmuch-json-begin-compound + notmuch-json-parser) 'retry) + (setq done t) + (setq notmuch-json-state 'result))) + ((result) + ;; Parse a result + (let ((result (notmuch-json-read notmuch-json-parser))) + (case result + ((retry) (setq done t)) + ((end) (setq notmuch-json-state 'end)) + (otherwise (with-current-buffer results-buf + (funcall result-function result)))))) + ((end) + ;; Any trailing data is unexpected + (notmuch-json-eof notmuch-json-parser) + (setq done t))) + (json-error + ;; Do our best to resynchronize and ensure forward + ;; progress + (let ((bad (buffer-substring (line-beginning-position) + (line-end-position)))) + (forward-line) + (with-current-buffer results-buf + (funcall error-function bad)))))) + ;; Clear out what we've parsed + (delete-region (point-min) (point)))) (defun notmuch-search-process-filter (proc string) "Process and filter the output of \"notmuch search\"" @@ -827,41 +876,10 @@ non-authors is found, assume that all of the authors match." ;; Insert new data (save-excursion (goto-char (point-max)) - (insert string))) - (with-current-buffer results-buf - (while (not done) - (condition-case nil - (case notmuch-search-process-state - ((begin) - ;; Enter the results list - (if (eq (notmuch-json-begin-compound - notmuch-search-json-parser) 'retry) - (setq done t) - (setq notmuch-search-process-state 'result))) - ((result) - ;; Parse a result - (let ((result (notmuch-json-read notmuch-search-json-parser))) - (case result - ((retry) (setq done t)) - ((end) (setq notmuch-search-process-state 'end)) - (otherwise (notmuch-search-show-result result))))) - ((end) - ;; Any trailing data is unexpected - (notmuch-json-eof notmuch-search-json-parser) - (setq done t))) - (json-error - ;; Do our best to resynchronize and ensure forward - ;; progress - (notmuch-search-show-error - "%s" - (with-current-buffer parse-buf - (let ((bad (buffer-substring (line-beginning-position) - (line-end-position)))) - (forward-line) - bad)))))) - ;; Clear out what we've parsed - (with-current-buffer parse-buf - (delete-region (point-min) (point))))))) + (insert string)) + (notmuch-json-parse-partial-list 'notmuch-search-show-result + 'notmuch-search-show-error + results-buf))))) (defun notmuch-search-tag-all (&optional tag-changes) "Add/remove tags from all messages in current search buffer. @@ -973,9 +991,6 @@ Other optional parameters are used as follows: ;; This buffer will be killed by the sentinel, which ;; should be called no matter how the process dies. (parse-buf (generate-new-buffer " *notmuch search parse*"))) - (set (make-local-variable 'notmuch-search-process-state) 'begin) - (set (make-local-variable 'notmuch-search-json-parser) - (notmuch-json-create-parser parse-buf)) (process-put proc 'parse-buf parse-buf) (set-process-sentinel proc 'notmuch-search-process-sentinel) (set-process-filter proc 'notmuch-search-process-filter) -- 1.7.9.1