[PATCH] emacs: move async json parser to its own function

Subject: [PATCH] emacs: move async json parser to its own function

Date: Sat, 28 Jul 2012 12:48:12 +0100

To: Austin Clements, notmuch@notmuchmail.org

Cc:

From: Mark Walters


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


Thread: