[PATCH 1/4] emacs: JSON based search improvements.

David Edmondson dme at dme.org
Tue Nov 30 03:00:22 PST 2010


Fix variable name typos.

Attempt to avoid display flashing by removing newlines from the
inserted string.

Ease debugging by moving the `condition-case' closer into the code
that it is really intended to cover (the parsing of the JSON object),
allowing errors in the remaining code to still fire correctly. As a
consequence, test for :json-eof when examining the next character to
be parsed, as that throws an error that was previously covered by the
`condition-case'.
---
 emacs/notmuch.el |   74 ++++++++++++++++++++++++++++++-----------------------
 1 files changed, 42 insertions(+), 32 deletions(-)

diff --git a/emacs/notmuch.el b/emacs/notmuch.el
index bde8c47..f4aefc8 100644
--- a/emacs/notmuch.el
+++ b/emacs/notmuch.el
@@ -717,57 +717,67 @@ foreground and blue background."
     (put-text-property beg (point-marker) 'notmuch-search-subject subject)))
 
 (defvar notmuch-search-parse-start nil)
-(make-variable-buffer-local 'notmuch-show-parse-start)
+(make-variable-buffer-local 'notmuch-search-parse-start)
 
 (defun notmuch-search-process-insert (proc buffer string)
   (with-current-buffer buffer
     (let ((inhibit-read-only t)
 	  (inhibit-redisplay t)
 	  ;; Vectors are not as useful here.
-	  (json-array-type 'list)
-	  object)
+	  (json-array-type 'list))
       (save-excursion
 	;; Insert the text, advancing the process marker
 	(goto-char (point-max))
-	(insert string)
+	;; Flatten the string (remove newlines) to reduce the flashing
+	;; that occurs when we insert the multi-line object and then
+	;; replace it with a single line summary. This is safe because
+	;; we know that none of the JSON fields can contain newlines -
+	;; only the whitespace between fields.
+	(insert (replace-regexp-in-string "\n" "" string))
 	(set-marker (process-mark proc) (point)))
 
       (save-excursion
 	(goto-char notmuch-search-parse-start)
-	(condition-case nil
 	    (while
-		(cond
-		 ;; Opening bracket or comma separator between
-		 ;; objects.
-		 ((or (char-equal (json-peek) ?\[)
-		      (char-equal (json-peek) ?\,))
-		  (json-advance)
-		  (delete-region notmuch-search-parse-start (point))
-		  t)
-
-		 ;; Closing array.
-		 ((char-equal (json-peek) ?\])
-		  ;; Consume both the closing bracket and any trailing
-		  ;; whitespace (typically a carriage return).
-		  (json-advance)
-		  (json-skip-whitespace)
-		  (delete-region notmuch-search-parse-start (point))
-		  nil)
-
-		 ;; Single object.
-		 ((setq object (json-read-object))
-		  ;; Delete the object that we consumed.
-		  (delete-region notmuch-search-parse-start (point))
-		  ;; Insert the corresponding results.
-		  (notmuch-search-process-insert-object object)
-		  t))
+		(let ((next-char (json-peek)))
+		  (cond
+		   ;; No more data (yet).
+		   ((eq next-char :json-eof)
+		    nil)
+
+		   ;; Opening bracket or comma separator between
+		   ;; objects.
+		   ((or (char-equal next-char ?\[)
+			(char-equal next-char ?\,))
+		    (json-advance)
+		    (delete-region notmuch-search-parse-start (point))
+		    t)
+
+		   ;; Closing array.
+		   ((char-equal next-char ?\])
+		    ;; Consume both the closing bracket and any trailing
+		    ;; whitespace (typically a carriage return).
+		    (json-advance)
+		    (json-skip-whitespace)
+		    (delete-region notmuch-search-parse-start (point))
+		    nil)
+
+		   ;; Single object.
+		   ((condition-case nil
+			(let ((object (json-read-object)))
+			  ;; Delete the object that we consumed.
+			  (delete-region notmuch-search-parse-start (point))
+			  ;; Insert the corresponding results.
+			  (notmuch-search-process-insert-object object)
+			  t)
+		      (error nil)))))
+
 	      ;; Consume any white space between terms.
 	      (let ((p (point)))
 		(json-skip-whitespace)
 		(delete-region p (point)))
 	      ;; Remember where we got up to.
-	      (setq notmuch-search-parse-start (point)))
-	  (error nil))))))
+	      (setq notmuch-search-parse-start (point)))))))
 
 (defun notmuch-search-process-filter (proc string)
   "Process and filter the output of `notmuch search'."
-- 
1.7.2.3



More information about the notmuch mailing list