--- /dev/null
+Return-Path: <markwalters1009@gmail.com>\r
+X-Original-To: notmuch@notmuchmail.org\r
+Delivered-To: notmuch@notmuchmail.org\r
+Received: from localhost (localhost [127.0.0.1])\r
+ by olra.theworths.org (Postfix) with ESMTP id 4F9D5431FC9\r
+ for <notmuch@notmuchmail.org>; Sat, 27 Oct 2012 02:34:21 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: 0.201\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=0.201 tagged_above=-999 required=5\r
+ tests=[DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1,\r
+ FREEMAIL_ENVFROM_END_DIGIT=1, FREEMAIL_FROM=0.001,\r
+ RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled\r
+Received: from olra.theworths.org ([127.0.0.1])\r
+ by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)\r
+ with ESMTP id F7BY5auE0Br9 for <notmuch@notmuchmail.org>;\r
+ Sat, 27 Oct 2012 02:34:20 -0700 (PDT)\r
+Received: from mail-we0-f181.google.com (mail-we0-f181.google.com\r
+ [74.125.82.181]) (using TLSv1 with cipher RC4-SHA (128/128 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 9210E431FAE\r
+ for <notmuch@notmuchmail.org>; Sat, 27 Oct 2012 02:34:20 -0700 (PDT)\r
+Received: by mail-we0-f181.google.com with SMTP id u54so1954323wey.26\r
+ for <notmuch@notmuchmail.org>; Sat, 27 Oct 2012 02:34:19 -0700 (PDT)\r
+DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113;\r
+ h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references;\r
+ bh=Uo70E8Np15psAByXD1zOejUXsIrNAhh4PO8Oe3rFk3M=;\r
+ b=GZcIwL35/e4VQLBQ99ZYJu838AEQPd5vkgd0gns5hIo3GCNvEEdAVuwWT5cO7+2sG8\r
+ fQZHotOTYQt2IgiqkEk9W+3q8UJlbv2q3gLtqDU67e74+CPpjlLb8DSiFBG+V0QN4BU+\r
+ 4A6dCRWCKGUruQV9XWLDa0xJv/QFcZjiZXaKMiU13KeXG4/Bjlcx7ZHBnB1ena87uQYi\r
+ 2hsszan5+KQVrzzK+M3ou+iCLXJ7YoH5EUghXDtaMcnN9Sp5iNorGMOe8Czdi/xtQHIY\r
+ Bmo+XkXCyVowrADZAkcpFtU4Mm0W2FByYkSvKCZ5L0WsQiVq8LdHIrzCac8rQb867Ms7\r
+ c9cg==\r
+Received: by 10.216.207.160 with SMTP id n32mr15265675weo.61.1351330459461;\r
+ Sat, 27 Oct 2012 02:34:19 -0700 (PDT)\r
+Received: from localhost (93-97-24-31.zone5.bethere.co.uk. [93.97.24.31])\r
+ by mx.google.com with ESMTPS id k20sm1539660wiv.11.2012.10.27.02.34.18\r
+ (version=TLSv1/SSLv3 cipher=OTHER);\r
+ Sat, 27 Oct 2012 02:34:18 -0700 (PDT)\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v4 1/3] emacs: Split out the incremental json parser into its\r
+ own function\r
+Date: Sat, 27 Oct 2012 10:34:10 +0100\r
+Message-Id: <1351330452-8348-2-git-send-email-markwalters1009@gmail.com>\r
+X-Mailer: git-send-email 1.7.9.1\r
+In-Reply-To: <1351330452-8348-1-git-send-email-markwalters1009@gmail.com>\r
+References: <1351330452-8348-1-git-send-email-markwalters1009@gmail.com>\r
+X-BeenThere: notmuch@notmuchmail.org\r
+X-Mailman-Version: 2.1.13\r
+Precedence: list\r
+List-Id: "Use and development of the notmuch mail system."\r
+ <notmuch.notmuchmail.org>\r
+List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
+List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
+List-Post: <mailto:notmuch@notmuchmail.org>\r
+List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
+List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
+ <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
+X-List-Received-Date: Sat, 27 Oct 2012 09:34:21 -0000\r
+\r
+This patch splits out the incremental json parser into its own\r
+function.\r
+\r
+It moves the main logic of the parser to happen inside the parse\r
+buffer rather than inside the results buffer, but makes sure all\r
+results and all errors are displayed in the results buffer.\r
+\r
+It also changes the local parser variables from being buffer\r
+local to the results buffer to being buffer local to the parse buffer,\r
+and sets them up automatically so the caller does not need to.\r
+\r
+Finally to keep the diff small this patch does not fix the whitespace,\r
+nor complete the code movement (these are done in subsequent patches)\r
+but it should contain all the functional changes.\r
+---\r
+ emacs/notmuch.el | 39 +++++++++++++++++++++++++++------------\r
+ 1 files changed, 27 insertions(+), 12 deletions(-)\r
+\r
+diff --git a/emacs/notmuch.el b/emacs/notmuch.el\r
+index a8a85ce..799c621 100644\r
+--- a/emacs/notmuch.el\r
++++ b/emacs/notmuch.el\r
+@@ -838,8 +838,28 @@ non-authors is found, assume that all of the authors match."\r
+ ;; Insert new data\r
+ (save-excursion\r
+ (goto-char (point-max))\r
+- (insert string)))\r
+- (with-current-buffer results-buf\r
++ (insert string))\r
++ (notmuch-json-parse-partial-list 'notmuch-search-show-result\r
++ 'notmuch-search-show-error\r
++ results-buf)))))\r
++\r
++(defun notmuch-json-parse-partial-list (result-function error-function results-buf)\r
++ "Parse a partial JSON list from current buffer.\r
++\r
++This function consumes a JSON list from the current buffer,\r
++applying RESULT-FUNCTION in buffer RESULT-BUFFER to each complete\r
++value in the list. It operates incrementally and should be\r
++called whenever the buffer has been extended with additional\r
++data.\r
++\r
++If there is a syntax error, this will attempt to resynchronize\r
++with the input and will apply ERROR-FUNCTION in buffer\r
++RESULT-BUFFER to any input that was skipped."\r
++ (let (done)\r
++ (unless (local-variable-p 'notmuch-search-json-parser)\r
++ (set (make-local-variable 'notmuch-search-json-parser)\r
++ (notmuch-json-create-parser (current-buffer)))\r
++ (set (make-local-variable 'notmuch-search-process-state) 'begin))\r
+ (while (not done)\r
+ (condition-case nil\r
+ (case notmuch-search-process-state\r
+@@ -855,7 +875,8 @@ non-authors is found, assume that all of the authors match."\r
+ (case result\r
+ ((retry) (setq done t))\r
+ ((end) (setq notmuch-search-process-state 'end))\r
+- (otherwise (notmuch-search-show-result result)))))\r
++ (otherwise (with-current-buffer results-buf\r
++ (funcall result-function result))))))\r
+ ((end)\r
+ ;; Any trailing data is unexpected\r
+ (notmuch-json-eof notmuch-search-json-parser)\r
+@@ -863,16 +884,13 @@ non-authors is found, assume that all of the authors match."\r
+ (json-error\r
+ ;; Do our best to resynchronize and ensure forward\r
+ ;; progress\r
+- (notmuch-search-show-error\r
+- "%s"\r
+- (with-current-buffer parse-buf\r
+ (let ((bad (buffer-substring (line-beginning-position)\r
+ (line-end-position))))\r
+ (forward-line)\r
+- bad))))))\r
++ (with-current-buffer results-buf\r
++ (funcall error-function "%s" bad))))))\r
+ ;; Clear out what we've parsed\r
+- (with-current-buffer parse-buf\r
+- (delete-region (point-min) (point)))))))\r
++ (delete-region (point-min) (point))))\r
+ \r
+ (defun notmuch-search-tag-all (&optional tag-changes)\r
+ "Add/remove tags from all messages in current search buffer.\r
+@@ -984,9 +1002,6 @@ Other optional parameters are used as follows:\r
+ ;; This buffer will be killed by the sentinel, which\r
+ ;; should be called no matter how the process dies.\r
+ (parse-buf (generate-new-buffer " *notmuch search parse*")))\r
+- (set (make-local-variable 'notmuch-search-process-state) 'begin)\r
+- (set (make-local-variable 'notmuch-search-json-parser)\r
+- (notmuch-json-create-parser parse-buf))\r
+ (process-put proc 'parse-buf parse-buf)\r
+ (set-process-sentinel proc 'notmuch-search-process-sentinel)\r
+ (set-process-filter proc 'notmuch-search-process-filter)\r
+-- \r
+1.7.9.1\r
+\r