From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id 4F9D5431FC9 for ; Sat, 27 Oct 2012 02:34:21 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: 0.201 X-Spam-Level: X-Spam-Status: No, score=0.201 tagged_above=-999 required=5 tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, FREEMAIL_ENVFROM_END_DIGIT=1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7] 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 F7BY5auE0Br9 for ; Sat, 27 Oct 2012 02:34:20 -0700 (PDT) Received: from mail-we0-f181.google.com (mail-we0-f181.google.com [74.125.82.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 9210E431FAE for ; Sat, 27 Oct 2012 02:34:20 -0700 (PDT) Received: by mail-we0-f181.google.com with SMTP id u54so1954323wey.26 for ; Sat, 27 Oct 2012 02:34:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references; bh=Uo70E8Np15psAByXD1zOejUXsIrNAhh4PO8Oe3rFk3M=; b=GZcIwL35/e4VQLBQ99ZYJu838AEQPd5vkgd0gns5hIo3GCNvEEdAVuwWT5cO7+2sG8 fQZHotOTYQt2IgiqkEk9W+3q8UJlbv2q3gLtqDU67e74+CPpjlLb8DSiFBG+V0QN4BU+ 4A6dCRWCKGUruQV9XWLDa0xJv/QFcZjiZXaKMiU13KeXG4/Bjlcx7ZHBnB1ena87uQYi 2hsszan5+KQVrzzK+M3ou+iCLXJ7YoH5EUghXDtaMcnN9Sp5iNorGMOe8Czdi/xtQHIY Bmo+XkXCyVowrADZAkcpFtU4Mm0W2FByYkSvKCZ5L0WsQiVq8LdHIrzCac8rQb867Ms7 c9cg== Received: by 10.216.207.160 with SMTP id n32mr15265675weo.61.1351330459461; Sat, 27 Oct 2012 02:34:19 -0700 (PDT) Received: from localhost (93-97-24-31.zone5.bethere.co.uk. [93.97.24.31]) by mx.google.com with ESMTPS id k20sm1539660wiv.11.2012.10.27.02.34.18 (version=TLSv1/SSLv3 cipher=OTHER); Sat, 27 Oct 2012 02:34:18 -0700 (PDT) From: Mark Walters To: notmuch@notmuchmail.org Subject: [PATCH v4 1/3] emacs: Split out the incremental json parser into its own function Date: Sat, 27 Oct 2012 10:34:10 +0100 Message-Id: <1351330452-8348-2-git-send-email-markwalters1009@gmail.com> X-Mailer: git-send-email 1.7.9.1 In-Reply-To: <1351330452-8348-1-git-send-email-markwalters1009@gmail.com> References: <1351330452-8348-1-git-send-email-markwalters1009@gmail.com> 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: Sat, 27 Oct 2012 09:34:21 -0000 This patch splits out the incremental json parser into its own function. It moves the main logic of the parser to happen inside the parse buffer rather than inside the results buffer, but makes sure all results and all errors are displayed in the results buffer. It also changes the local parser variables from being buffer local to the results buffer to being buffer local to the parse buffer, and sets them up automatically so the caller does not need to. Finally to keep the diff small this patch does not fix the whitespace, nor complete the code movement (these are done in subsequent patches) but it should contain all the functional changes. --- emacs/notmuch.el | 39 +++++++++++++++++++++++++++------------ 1 files changed, 27 insertions(+), 12 deletions(-) diff --git a/emacs/notmuch.el b/emacs/notmuch.el index a8a85ce..799c621 100644 --- a/emacs/notmuch.el +++ b/emacs/notmuch.el @@ -838,8 +838,28 @@ 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 + (insert string)) + (notmuch-json-parse-partial-list 'notmuch-search-show-result + 'notmuch-search-show-error + results-buf))))) + +(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-search-json-parser) + (set (make-local-variable 'notmuch-search-json-parser) + (notmuch-json-create-parser (current-buffer))) + (set (make-local-variable 'notmuch-search-process-state) 'begin)) (while (not done) (condition-case nil (case notmuch-search-process-state @@ -855,7 +875,8 @@ non-authors is found, assume that all of the authors match." (case result ((retry) (setq done t)) ((end) (setq notmuch-search-process-state 'end)) - (otherwise (notmuch-search-show-result result))))) + (otherwise (with-current-buffer results-buf + (funcall result-function result)))))) ((end) ;; Any trailing data is unexpected (notmuch-json-eof notmuch-search-json-parser) @@ -863,16 +884,13 @@ non-authors is found, assume that all of the authors match." (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)))))) + (with-current-buffer results-buf + (funcall error-function "%s" bad)))))) ;; Clear out what we've parsed - (with-current-buffer parse-buf - (delete-region (point-min) (point))))))) + (delete-region (point-min) (point)))) (defun notmuch-search-tag-all (&optional tag-changes) "Add/remove tags from all messages in current search buffer. @@ -984,9 +1002,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