--- /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 28257431FC0\r
+ for <notmuch@notmuchmail.org>; Tue, 21 May 2013 07:24:29 -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 oH2lzD4WqwCW for <notmuch@notmuchmail.org>;\r
+ Tue, 21 May 2013 07:24:23 -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 E0BB4431FB6\r
+ for <notmuch@notmuchmail.org>; Tue, 21 May 2013 07:24:22 -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 1UenUA-0005zN-Sf; Tue, 21 May 2013 15:24:19 +0100\r
+Received: from 93-97-24-31.zone5.bethere.co.uk ([93.97.24.31] helo=localhost)\r
+ by smtp.qmul.ac.uk with esmtpsa (TLSv1:AES128-SHA:128) (Exim 4.71)\r
+ (envelope-from <m.walters@qmul.ac.uk>)\r
+ id 1UenUA-00010W-Df; Tue, 21 May 2013 15:24:18 +0100\r
+From: Mark Walters <markwalters1009@gmail.com>\r
+To: Austin Clements <amdragon@MIT.EDU>, notmuch@notmuchmail.org\r
+Subject: Re: [PATCH 2/5] emacs: Utilities to manage asynchronous notmuch\r
+ processes\r
+In-Reply-To: <1368851472-5382-3-git-send-email-amdragon@mit.edu>\r
+References: <1368851472-5382-1-git-send-email-amdragon@mit.edu>\r
+ <1368851472-5382-3-git-send-email-amdragon@mit.edu>\r
+User-Agent: Notmuch/0.14+255~gff3cc55 (http://notmuchmail.org) Emacs/23.4.1\r
+ (x86_64-pc-linux-gnu)\r
+Date: Tue, 21 May 2013 15:24:17 +0100\r
+Message-ID: <87li782yy6.fsf@qmul.ac.uk>\r
+MIME-Version: 1.0\r
+Content-Type: text/plain; charset=us-ascii\r
+X-Sender-Host-Address: 93.97.24.31\r
+X-QM-SPAM-Info: Sender has good ham record. :)\r
+X-QM-Body-MD5: 186b403f88e335fa20af8637280609d5 (of first 20000 bytes)\r
+X-SpamAssassin-Score: -0.2\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 -0.2 points.\r
+ Summary of the scoring: \r
+ * 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail\r
+ provider * (markwalters1009[at]gmail.com)\r
+ * -0.2 AWL AWL: From: address is in the auto white-list\r
+X-QM-Scan-Virus: ClamAV says the message is clean\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: Tue, 21 May 2013 14:24:29 -0000\r
+\r
+\r
+Hi\r
+\r
+I am working my way through this set. \r
+\r
+On Sat, 18 May 2013, Austin Clements <amdragon@MIT.EDU> wrote:\r
+> This provides a new notmuch-lib utility to start an asynchronous\r
+> notmuch process that handles redirecting of stderr and checking of the\r
+> exit status. This is similar to `notmuch-call-notmuch-json', but for\r
+> asynchronous processes (and it leaves output processing to the\r
+> caller).\r
+> ---\r
+> emacs/notmuch-lib.el | 73 +++++++++++++++++++++++++++++++++++++++++++++++---\r
+> 1 file changed, 69 insertions(+), 4 deletions(-)\r
+>\r
+> diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el\r
+> index 59b1ce3..a543471 100644\r
+> --- a/emacs/notmuch-lib.el\r
+> +++ b/emacs/notmuch-lib.el\r
+> @@ -383,18 +383,21 @@ signaled error. This function does not return."\r
+> (error "%s" (concat msg (when extra\r
+> " (see *Notmuch errors* for more details)"))))\r
+> \r
+> -(defun notmuch-check-async-exit-status (proc msg)\r
+> +(defun notmuch-check-async-exit-status (proc msg &optional command err-file)\r
+> "If PROC exited abnormally, pop up an error buffer and signal an error.\r
+> \r
+> This is a wrapper around `notmuch-check-exit-status' for\r
+> asynchronous process sentinels. PROC and MSG must be the\r
+> -arguments passed to the sentinel."\r
+> +arguments passed to the sentinel. COMMAND and ERR-FILE, if\r
+> +provided, are passed to `notmuch-check-exit-status'. If COMMAND\r
+> +is not provided, it is taken from `process-command'."\r
+> (let ((exit-status\r
+> (case (process-status proc)\r
+> ((exit) (process-exit-status proc))\r
+> ((signal) msg))))\r
+> (when exit-status\r
+> - (notmuch-check-exit-status exit-status (process-command proc)))))\r
+> + (notmuch-check-exit-status exit-status (or command (process-command proc))\r
+> + nil err-file))))\r
+> \r
+> (defun notmuch-check-exit-status (exit-status command &optional output err-file)\r
+> "If EXIT-STATUS is non-zero, pop up an error buffer and signal an error.\r
+> @@ -448,7 +451,7 @@ You may need to restart Emacs or upgrade your notmuch package."))\r
+> ))))\r
+> \r
+> (defun notmuch-call-notmuch-json (&rest args)\r
+> - "Invoke `notmuch-command' with `args' and return the parsed JSON output.\r
+> + "Invoke `notmuch-command' with ARGS and return the parsed JSON output.\r
+> \r
+> The returned output will represent objects using property lists\r
+> and arrays as lists. If notmuch exits with a non-zero status,\r
+\r
+I think I would prefer that this patch is split here. The stuff above is\r
+"obviously correct" and could go in independently of the rest.\r
+ \r
+> @@ -469,6 +472,68 @@ an error."\r
+> (json-read)))\r
+> (delete-file err-file)))))\r
+> \r
+> +(defun notmuch-start-notmuch (name buffer sentinel &rest args)\r
+> + "Start and return an asynchronous notmuch command.\r
+> +\r
+> +This starts and returns an asynchronous process running\r
+> +`notmuch-command' with ARGS. The exit status is checked via\r
+> +`notmuch-check-async-exit-status'. Output written to stderr is\r
+> +redirected and displayed when the process exits (even if the\r
+> +process exits successfully). NAME and BUFFER are the same as in\r
+> +`start-process'. SENTINEL is a process sentinel function to call\r
+> +when the process exits, or nil for none. The caller must *not*\r
+> +invoke `set-process-sentinel' directly on the returned process,\r
+> +as that will interfere with the handling of stderr and the exit\r
+> +status."\r
+> +\r
+> + ;; There is no way (as of Emacs 24.3) to capture stdout and stderr\r
+> + ;; separately for asynchronous processes, or even to redirect stderr\r
+> + ;; to a file, so we use a trivial shell wrapper to send stderr to a\r
+> + ;; temporary file and clean things up in the sentinel.\r
+> + (let* ((err-file (make-temp-file "nmerr"))\r
+> + ;; Use a pipe\r
+> + (process-connection-type nil)\r
+> + (proc (apply #'start-process name buffer\r
+> + "sh" "-c"\r
+> + "ERR=\"$1\"; shift; exec \"$0\" \"$@\" 2>\"$ERR\""\r
+> + notmuch-command err-file args)))\r
+\r
+I would like some other people to look at this carefully as I won't spot\r
+errors in quoting or shell side effects or whether PATH is the same as\r
+emacs would use etc.\r
+\r
+> + (process-put proc 'err-file err-file)\r
+> + (process-put proc 'sub-sentinel sentinel)\r
+> + (process-put proc 'real-command (cons notmuch-command args))\r
+> + (set-process-sentinel proc #'notmuch-start-notmuch-sentinel)\r
+> + proc))\r
+> +\r
+> +(defun notmuch-start-notmuch-sentinel (proc event)\r
+> + (let ((err-file (process-get proc 'err-file))\r
+> + (sub-sentinel (process-get proc 'sub-sentinel))\r
+> + (real-command (process-get proc 'real-command)))\r
+> + (condition-case-unless-debug err\r
+> + (progn\r
+> + ;; Invoke the sub-sentinel, if any\r
+> + (when sub-sentinel\r
+> + (funcall sub-sentinel proc event))\r
+> + ;; Check the exit status. This will signal an error if the\r
+> + ;; exit status is non-zero.\r
+> + (notmuch-check-async-exit-status proc event real-command err-file)\r
+> + ;; If that didn't signal an error, then any error output was\r
+> + ;; really warning output. Show warnings, if any.\r
+> + (let ((warnings\r
+> + (with-temp-buffer\r
+> + (unless (= (second (insert-file-contents err-file)) 0)\r
+> + (end-of-line)\r
+> + ;; Show first line; stuff remaining lines in the\r
+> + ;; errors buffer.\r
+> + (let ((l1 (buffer-substring (point-min) (point))))\r
+> + (skip-chars-forward "\n")\r
+> + (cons l1 (unless (eobp)\r
+> + (buffer-substring (point) (point-max)))))))))\r
+> + (when warnings\r
+> + (notmuch-logged-error (car warnings) (cdr warnings)))))\r
+> + (error\r
+> + ;; Don't signal an error from a sentinel\r
+\r
+I found this comment confusing as I was thinking of signal in the\r
+non-technical sense of "tell the user".\r
+\r
+Best wishes\r
+\r
+Mark\r
+> + (message "%s" (error-message-string err))))\r
+> + (ignore-errors (delete-file err-file))))\r
+> +\r
+> ;; This variable is used only buffer local, but it needs to be\r
+> ;; declared globally first to avoid compiler warnings.\r
+> (defvar notmuch-show-process-crypto nil)\r
+> -- \r
+> 1.7.10.4\r