--- /dev/null
+Return-Path: <amdragon@mit.edu>\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 D84CC431FDE\r
+ for <notmuch@notmuchmail.org>; Fri, 31 May 2013 17:40:45 -0700 (PDT)\r
+X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
+X-Spam-Flag: NO\r
+X-Spam-Score: -0.7\r
+X-Spam-Level: \r
+X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5\r
+ tests=[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 gv0mi8ebGuU3 for <notmuch@notmuchmail.org>;\r
+ Fri, 31 May 2013 17:40:41 -0700 (PDT)\r
+Received: from dmz-mailsec-scanner-1.mit.edu (dmz-mailsec-scanner-1.mit.edu\r
+ [18.9.25.12])\r
+ by olra.theworths.org (Postfix) with ESMTP id 576E0429E38\r
+ for <notmuch@notmuchmail.org>; Fri, 31 May 2013 17:40:25 -0700 (PDT)\r
+X-AuditID: 1209190c-b7f566d000004c69-62-51a942f82a60\r
+Received: from mailhub-auth-1.mit.edu ( [18.9.21.35])\r
+ by dmz-mailsec-scanner-1.mit.edu (Symantec Messaging Gateway) with SMTP\r
+ id 56.73.19561.8F249A15; Fri, 31 May 2013 20:40:24 -0400 (EDT)\r
+Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])\r
+ by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id r510eD8R008173; \r
+ Fri, 31 May 2013 20:40:13 -0400\r
+Received: from drake.dyndns.org (c-76-21-105-205.hsd1.ca.comcast.net\r
+ [76.21.105.205]) (authenticated bits=0)\r
+ (User authenticated as amdragon@ATHENA.MIT.EDU)\r
+ by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id r510e9ve020941\r
+ (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
+ Fri, 31 May 2013 20:40:12 -0400\r
+Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
+ (envelope-from <amdragon@mit.edu>)\r
+ id 1UiZrd-0003QR-CA; Fri, 31 May 2013 20:40:09 -0400\r
+From: Austin Clements <amdragon@MIT.EDU>\r
+To: notmuch@notmuchmail.org\r
+Subject: [PATCH v3 2/6] emacs: Utilities to manage asynchronous notmuch\r
+ processes\r
+Date: Fri, 31 May 2013 20:40:04 -0400\r
+Message-Id: <1370047208-12785-3-git-send-email-amdragon@mit.edu>\r
+X-Mailer: git-send-email 1.7.10.4\r
+In-Reply-To: <1370047208-12785-1-git-send-email-amdragon@mit.edu>\r
+References: <1370047208-12785-1-git-send-email-amdragon@mit.edu>\r
+X-Brightmail-Tracker:\r
+ H4sIAAAAAAAAA+NgFjrGIsWRmVeSWpSXmKPExsUixCmqrPvDaWWgwaG/Fhar5/JYXL85k9ni\r
+ zcp5rA7MHjtn3WX3OPx1IYvHs1W3mAOYo7hsUlJzMstSi/TtErgyGqZZF3zSqmhcNJG1gXGZ\r
+ UhcjJ4eEgInEvkO/WSFsMYkL99azdTFycQgJ7GOUWL3mFjuEs5FR4uyka8wQzmkmiZtbZ0KV\r
+ zWWUmPXgKQtIP5uAhsS2/csZQWwRAWmJnXdng81lFoiT2DLlP1hcWCBQYnPHGTYQm0VAVeLz\r
+ m7dgcV4BB4nXDR/YIO5QlOh+NgHM5hRwlDh/9R8ziC0EVPP4xEnmCYz8CxgZVjHKpuRW6eYm\r
+ ZuYUpybrFicn5uWlFuka6uVmluilppRuYgQHlyTPDsY3B5UOMQpwMCrx8HasWhEoxJpYVlyZ\r
+ e4hRkoNJSZR3tuPKQCG+pPyUyozE4oz4otKc1OJDjBIczEoivPlzgMp5UxIrq1KL8mFS0hws\r
+ SuK8l1Nu+gsJpCeWpGanphakFsFkZTg4lCR41YBRJCRYlJqeWpGWmVOCkGbi4AQZzgM03A2k\r
+ hre4IDG3ODMdIn+KUVFKnDcIJCEAksgozYPrhUX/K0ZxoFeEIdp5gIkDrvsV0GAmoMFPrJeD\r
+ DC5JREhJNTDW72CzW3V85Zq9Pp94DEwUzwR6fjMU+jev8/Rz1ys7lqf6KO6tW7q/6/Tc3AN5\r
+ yyYX8vh1OpusyVgYsHiuSKn8npKLX6IPdahtEvznc5H5VL1vn47G1fmt07qk/zStKJ109+PW\r
+ tX3df14pf34y3ezd1x27/xbvirtlUNHP91Rh8fQi/U9LjnxOUWIpzkg01GIuKk4EADmM0hjZ AgAA\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: Sat, 01 Jun 2013 00:40:46 -0000\r
+\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 | 77 +++++++++++++++++++++++++++++++++++++++++++++++---\r
+ 1 file changed, 73 insertions(+), 4 deletions(-)\r
+\r
+diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el\r
+index 59b1ce3..a206808 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
+@@ -469,6 +472,72 @@ 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
++ ;; Find notmuch using Emacs' `exec-path'\r
++ (command (or (executable-find notmuch-command)\r
++ (error "command not found: %s" notmuch-command)))\r
++ (proc (apply #'start-process name buffer\r
++ "/bin/sh" "-c"\r
++ "exec 2>\"$1\"; shift; exec \"$0\" \"$@\""\r
++ command err-file args)))\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 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
++ ;; Emacs behaves strangely if an error escapes from a sentinel,\r
++ ;; so turn errors into messages.\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
+\r