--- /dev/null
+Return-Path: <m.walters@qmul.ac.uk>\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 DD328431FB6\r
+ for <notmuch@notmuchmail.org>; Thu, 5 Jul 2012 01:37:26 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -1.098\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-1.098 tagged_above=-999 required=5\r
+ tests=[DKIM_ADSP_CUSTOM_MED=0.001, FREEMAIL_FROM=0.001,\r
+ NML_ADSP_CUSTOM_MED=1.2, RCVD_IN_DNSWL_MED=-2.3] 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 H-lCJLaWauyi for <notmuch@notmuchmail.org>;\r
+ Thu, 5 Jul 2012 01:37:26 -0700 (PDT)\r
+Received: from mail2.qmul.ac.uk (mail2.qmul.ac.uk [138.37.6.6])\r
+ (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits))\r
+ (No client certificate requested)\r
+ by olra.theworths.org (Postfix) with ESMTPS id 8CF28431FAE\r
+ for <notmuch@notmuchmail.org>; Thu, 5 Jul 2012 01:37:25 -0700 (PDT)\r
+Received: from smtp.qmul.ac.uk ([138.37.6.40])\r
+ by mail2.qmul.ac.uk with esmtp (Exim 4.71)\r
+ (envelope-from <m.walters@qmul.ac.uk>)\r
+ id 1SmhYx-0001IN-Cm; Thu, 05 Jul 2012 09:37:23 +0100\r
+Received: from 94-192-233-223.zone6.bethere.co.uk ([94.192.233.223]\r
+ helo=localhost)\r
+ by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.69)\r
+ (envelope-from <m.walters@qmul.ac.uk>)\r
+ id 1SmhYw-0003nK-TQ; Thu, 05 Jul 2012 09:37:23 +0100\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: Austin Clements <amdragon@MIT.EDU>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH 8/8] emacs: Switch from text to JSON format for search\r
+ results\r
+In-Reply-To: <1341354059-29396-9-git-send-email-amdragon@mit.edu>\r
+References: <1341354059-29396-1-git-send-email-amdragon@mit.edu>\r
+ <1341354059-29396-9-git-send-email-amdragon@mit.edu>\r
+User-Agent: Notmuch/0.13.2+70~gb6a56e7 (http://notmuchmail.org) Emacs/23.4.1\r
+ (x86_64-pc-linux-gnu)\r
+Date: Thu, 05 Jul 2012 09:37:19 +0100\r
+Message-ID: <87y5myeh8g.fsf@qmul.ac.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Sender-Host-Address: 94.192.233.223\r
+X-QM-SPAM-Info: Sender has good ham record. :)\r
+X-QM-Body-MD5: dee2ed397367a44f97b87e5657d11775 (of first 20000 bytes)\r
+X-SpamAssassin-Score: -1.8\r
+X-SpamAssassin-SpamBar: -\r
+X-SpamAssassin-Report: The QM spam filters have analysed this message to\r
+ determine if it is\r
+ spam. We require at least 5.0 points to mark a message as spam.\r
+ This message scored -1.8 points.\r
+ Summary of the scoring: \r
+ * -2.3 RCVD_IN_DNSWL_MED RBL: Sender listed at http://www.dnswl.org/,\r
+ * medium trust\r
+ * [138.37.6.40 listed in list.dnswl.org]\r
+ * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
+ provider * (markwalters1009[at]gmail.com)\r
+ * -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay\r
+ * domain\r
+ * 0.5 AWL AWL: From: address is in the auto white-list\r
+X-QM-Scan-Virus: ClamAV says the message is clean\r
+Cc: tomi.ollila@iki.fi\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: Thu, 05 Jul 2012 08:37:27 -0000\r
+\r
+On Tue, 03 Jul 2012, Austin Clements <amdragon@MIT.EDU> wrote:\r
+> The JSON format eliminates the complex escaping issues that have\r
+> plagued the text search format. This uses the incremental JSON parser\r
+> so that, like the text parser, it can output search results\r
+> incrementally.\r
+>\r
+> This slows down the parser by about ~4X, but puts us in a good\r
+> position to optimize either by improving the JSON parser (evidence\r
+> suggests this can reduce the overhead to ~40% over the text format) or\r
+> by switching to S-expressions (evidence suggests this will more than\r
+> double performance over the text parser). [1]\r
+>\r
+> This also fixes the incremental search parsing test.\r
+>\r
+> [1] id:"20110720205007.GB21316@mit.edu"\r
+> ---\r
+> emacs/notmuch.el | 113 ++++++++++++++++++++++++++++++++----------------------\r
+> test/emacs | 1 -\r
+> 2 files changed, 67 insertions(+), 47 deletions(-)\r
+>\r
+> diff --git a/emacs/notmuch.el b/emacs/notmuch.el\r
+> index 084cec6..2a09a98 100644\r
+> --- a/emacs/notmuch.el\r
+> +++ b/emacs/notmuch.el\r
+> @@ -60,7 +60,7 @@\r
+> (require 'notmuch-message)\r
+> \r
+> (defcustom notmuch-search-result-format\r
+> - `(("date" . "%s ")\r
+> + `(("date" . "%12s ")\r
+> ("count" . "%-7s ")\r
+> ("authors" . "%-20s ")\r
+> ("subject" . "%s ")\r
+> @@ -557,17 +557,14 @@ This function advances the next thread when finished."\r
+> (notmuch-search-tag '("-inbox"))\r
+> (notmuch-search-next-thread))\r
+> \r
+> -(defvar notmuch-search-process-filter-data nil\r
+> - "Data that has not yet been processed.")\r
+> -(make-variable-buffer-local 'notmuch-search-process-filter-data)\r
+> -\r
+> (defun notmuch-search-process-sentinel (proc msg)\r
+> "Add a message to let user know when \"notmuch search\" exits"\r
+> (let ((buffer (process-buffer proc))\r
+> (status (process-status proc))\r
+> (exit-status (process-exit-status proc))\r
+> (never-found-target-thread nil))\r
+> - (if (memq status '(exit signal))\r
+> + (when (memq status '(exit signal))\r
+> + (kill-buffer (process-get proc 'parse-buf))\r
+> (if (buffer-live-p buffer)\r
+> (with-current-buffer buffer\r
+> (save-excursion\r
+> @@ -577,8 +574,6 @@ This function advances the next thread when finished."\r
+> (if (eq status 'signal)\r
+> (insert "Incomplete search results (search process was killed).\n"))\r
+> (when (eq status 'exit)\r
+> - (if notmuch-search-process-filter-data\r
+> - (insert (concat "Error: Unexpected output from notmuch search:\n" notmuch-search-process-filter-data)))\r
+> (insert "End of search results.")\r
+> (unless (= exit-status 0)\r
+> (insert (format " (process returned %d)" exit-status)))\r
+> @@ -757,45 +752,62 @@ non-authors is found, assume that all of the authors match."\r
+> (insert (apply #'format string objects))\r
+> (insert "\n")))\r
+> \r
+> +(defvar notmuch-search-process-state nil\r
+> + "Parsing state of the search process filter.")\r
+> +\r
+> +(defvar notmuch-search-json-parser nil\r
+> + "Incremental JSON parser for the search process filter.")\r
+> +\r
+> (defun notmuch-search-process-filter (proc string)\r
+> "Process and filter the output of \"notmuch search\""\r
+> - (let ((buffer (process-buffer proc)))\r
+> - (if (buffer-live-p buffer)\r
+> - (with-current-buffer buffer\r
+> - (let ((line 0)\r
+> - (more t)\r
+> - (inhibit-read-only t)\r
+> - (string (concat notmuch-search-process-filter-data string)))\r
+> - (setq notmuch-search-process-filter-data nil)\r
+> - (while more\r
+> - (while (and (< line (length string)) (= (elt string line) ?\n))\r
+> - (setq line (1+ line)))\r
+> - (if (string-match "^thread:\\([0-9A-Fa-f]*\\) \\([^][]*\\) \\[\\([0-9]*\\)/\\([0-9]*\\)\\] \\([^;]*\\); \\(.*\\) (\\([^()]*\\))$" string line)\r
+> - (let* ((thread-id (match-string 1 string))\r
+> - (tags-str (match-string 7 string))\r
+> - (result (list :thread thread-id\r
+> - :date_relative (match-string 2 string)\r
+> - :matched (string-to-number\r
+> - (match-string 3 string))\r
+> - :total (string-to-number\r
+> - (match-string 4 string))\r
+> - :authors (match-string 5 string)\r
+> - :subject (match-string 6 string)\r
+> - :tags (if tags-str\r
+> - (save-match-data\r
+> - (split-string tags-str))))))\r
+> - (if (/= (match-beginning 0) line)\r
+> - (notmuch-search-show-error\r
+> - (substring string line (match-beginning 0))))\r
+> - (notmuch-search-show-result result)\r
+> - (set 'line (match-end 0)))\r
+> - (set 'more nil)\r
+> - (while (and (< line (length string)) (= (elt string line) ?\n))\r
+> - (setq line (1+ line)))\r
+> - (if (< line (length string))\r
+> - (setq notmuch-search-process-filter-data (substring string line)))\r
+> - ))))\r
+> - (delete-process proc))))\r
+> + (let ((results-buf (process-buffer proc))\r
+> + (parse-buf (process-get proc 'parse-buf))\r
+> + (inhibit-read-only t)\r
+> + done)\r
+> + (if (not (buffer-live-p results-buf))\r
+> + (delete-process proc)\r
+> + (with-current-buffer parse-buf\r
+> + ;; Insert new data\r
+> + (save-excursion\r
+> + (goto-char (point-max))\r
+> + (insert string)))\r
+> + (with-current-buffer results-buf\r
+> + (while (not done)\r
+> + (condition-case nil\r
+> + (case notmuch-search-process-state\r
+> + ((begin)\r
+> + ;; Enter the results list\r
+> + (if (eq (notmuch-json-begin-compound\r
+> + notmuch-search-json-parser) 'retry)\r
+> + (setq done t)\r
+> + (setq notmuch-search-process-state 'result)))\r
+> + ((result)\r
+> + ;; Parse a result\r
+> + (let ((result (notmuch-json-read notmuch-search-json-parser)))\r
+> + (case result\r
+> + ((retry) (setq done t))\r
+> + ((end) (setq notmuch-search-process-state 'end))\r
+> + (otherwise (notmuch-search-show-result result)))))\r
+> + ((end)\r
+> + ;; Any trailing data is unexpected\r
+> + (with-current-buffer parse-buf\r
+> + (skip-chars-forward " \t\r\n")\r
+> + (if (eobp)\r
+> + (setq done t)\r
+> + (signal 'json-error nil)))))\r
+\r
+This looks good to me. Would it make sense to put the "Any trailing\r
+data" as a tiny function in notmuch-lib? something like \r
+\r
+(defun notmuch-json-assert-end-of-data ()\r
+ (skip-chars-forward " \t\r\n") \r
+ (if (eobp)\r
+ (setq done t) \r
+ (signal 'json-error nil)))\r
+\r
+Best wishes\r
+\r
+Mark\r
+\r
+\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
+> + ;; Clear out what we've parsed\r
+> + (with-current-buffer parse-buf\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
+> @@ -898,10 +910,19 @@ Other optional parameters are used as follows:\r
+> (let ((proc (start-process\r
+> "notmuch-search" buffer\r
+> notmuch-command "search"\r
+> + "--format=json"\r
+> (if oldest-first\r
+> "--sort=oldest-first"\r
+> "--sort=newest-first")\r
+> - query)))\r
+> + query))\r
+> + ;; Use a scratch buffer to accumulate partial output.\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
+> (set-process-query-on-exit-flag proc nil))))\r
+> diff --git a/test/emacs b/test/emacs\r
+> index 293b12a..afe35ba 100755\r
+> --- a/test/emacs\r
+> +++ b/test/emacs\r
+> @@ -36,7 +36,6 @@ test_emacs '(notmuch-search "tag:inbox")\r
+> test_expect_equal_file OUTPUT $EXPECTED/notmuch-search-tag-inbox\r
+> \r
+> test_begin_subtest "Incremental parsing of search results"\r
+> -test_subtest_known_broken\r
+> test_emacs "(ad-enable-advice 'notmuch-search-process-filter 'around 'pessimal)\r
+> (ad-activate 'notmuch-search-process-filter)\r
+> (notmuch-search \"tag:inbox\")\r
+> -- \r
+> 1.7.10\r
+>\r
+> _______________________________________________\r
+> notmuch mailing list\r
+> notmuch@notmuchmail.org\r
+> http://notmuchmail.org/mailman/listinfo/notmuch\r