From: Austin Clements Date: Sat, 18 May 2013 04:31:09 +0000 (+2000) Subject: [PATCH 2/5] emacs: Utilities to manage asynchronous notmuch processes X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=030a15141d46eed31d5f27552d5c2440df248027;p=notmuch-archives.git [PATCH 2/5] emacs: Utilities to manage asynchronous notmuch processes --- diff --git a/4e/961b502904a5bbfd381d9cb58b3afb1e7273ca b/4e/961b502904a5bbfd381d9cb58b3afb1e7273ca new file mode 100644 index 000000000..c46dd2739 --- /dev/null +++ b/4e/961b502904a5bbfd381d9cb58b3afb1e7273ca @@ -0,0 +1,191 @@ +Return-Path: +X-Original-To: notmuch@notmuchmail.org +Delivered-To: notmuch@notmuchmail.org +Received: from localhost (localhost [127.0.0.1]) + by olra.theworths.org (Postfix) with ESMTP id 4495F431FC9 + for ; Fri, 17 May 2013 21:33:06 -0700 (PDT) +X-Virus-Scanned: Debian amavisd-new at olra.theworths.org +X-Spam-Flag: NO +X-Spam-Score: -0.7 +X-Spam-Level: +X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 + tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled +Received: from olra.theworths.org ([127.0.0.1]) + by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) + with ESMTP id wHUQMiUjQ-Qo for ; + Fri, 17 May 2013 21:33:00 -0700 (PDT) +Received: from dmz-mailsec-scanner-8.mit.edu (DMZ-MAILSEC-SCANNER-8.MIT.EDU + [18.7.68.37]) + by olra.theworths.org (Postfix) with ESMTP id CC397429E2F + for ; Fri, 17 May 2013 21:32:24 -0700 (PDT) +X-AuditID: 12074425-b7f986d00000082c-e9-519704581065 +Received: from mailhub-auth-4.mit.edu ( [18.7.62.39]) + by dmz-mailsec-scanner-8.mit.edu (Symantec Messaging Gateway) with SMTP + id FE.89.02092.85407915; Sat, 18 May 2013 00:32:24 -0400 (EDT) +Received: from outgoing.mit.edu (OUTGOING-AUTH-1.MIT.EDU [18.9.28.11]) + by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id r4I4WDPb009845; + Sat, 18 May 2013 00:32:13 -0400 +Received: from drake.dyndns.org + (216-15-114-40.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com + [216.15.114.40]) (authenticated bits=0) + (User authenticated as amdragon@ATHENA.MIT.EDU) + by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id r4I4WBMD018833 + (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); + Sat, 18 May 2013 00:32:12 -0400 +Received: from amthrax by drake.dyndns.org with local (Exim 4.77) + (envelope-from ) + id 1UdYo1-0001Yw-JW; Sat, 18 May 2013 00:31:41 -0400 +From: Austin Clements +To: notmuch@notmuchmail.org +Subject: [PATCH 2/5] emacs: Utilities to manage asynchronous notmuch processes +Date: Sat, 18 May 2013 00:31:09 -0400 +Message-Id: <1368851472-5382-3-git-send-email-amdragon@mit.edu> +X-Mailer: git-send-email 1.7.10.4 +In-Reply-To: <1368851472-5382-1-git-send-email-amdragon@mit.edu> +References: <1368851472-5382-1-git-send-email-amdragon@mit.edu> +X-Brightmail-Tracker: + H4sIAAAAAAAAA+NgFtrDIsWRmVeSWpSXmKPExsUixG6nrhvBMj3Q4M8XPovVc3ksrt+cyezA + 5LFz1l12j2erbjEHMEVx2aSk5mSWpRbp2yVwZby4cJOpYK1mxcttl5gaGCcqdjFyckgImEhM + n/+BEcIWk7hwbz0biC0ksI9RYsV24y5GLiB7I6PE3wmn2SCcO0wSHUves0M4cxklHh9bxg7S + wiagIbFt/3KwUSIC0hI7785mBbGZBRwlPu9fBDZWWMBP4tzhtWA1LAKqEn/+LgOL8wrYS/w4 + 280KcYaiRPezCWBxTgEHicPzj0GdZC/RdeEm2wRG/gWMDKsYZVNyq3RzEzNzilOTdYuTE/Py + Uot0LfRyM0v0UlNKNzGCA8lFdQfjhENKhxgFOBiVeHhnOE8LFGJNLCuuzD3EKMnBpCTKa/IP + KMSXlJ9SmZFYnBFfVJqTWnyIUYKDWUmE93kxUI43JbGyKrUoHyYlzcGiJM57I+Wmv5BAemJJ + anZqakFqEUxWhoNDSYK3iHl6oJBgUWp6akVaZk4JQpqJgxNkOA/Q8HKQGt7igsTc4sx0iPwp + RkUpcd5ikIQASCKjNA+uFxbprxjFgV4R5p0EUsUDTBJw3a+ABjMBDWa9NhVkcEkiQkqqgVGb + 427wkn75uHePb3ZN49zFctL8yzqv7QLP+q5tt13FZ/jBhelF3cYqtsVeZTO43c6mCtt5a3h8 + rd2i87FgiWLbcb9vjdofdL/OnjfzeAmPXc+DXcqXliqXLVvasuHexRk+N/SPNN+++PBygMPL + iMzDttNiZcVP7bQtmfP/6ZnJk2fd7F7kVvZWiaU4I9FQi7moOBEAG+GmBc8CAAA= +X-BeenThere: notmuch@notmuchmail.org +X-Mailman-Version: 2.1.13 +Precedence: list +List-Id: "Use and development of the notmuch mail system." + +List-Unsubscribe: , + +List-Archive: +List-Post: +List-Help: +List-Subscribe: , + +X-List-Received-Date: Sat, 18 May 2013 04:33:06 -0000 + +This provides a new notmuch-lib utility to start an asynchronous +notmuch process that handles redirecting of stderr and checking of the +exit status. This is similar to `notmuch-call-notmuch-json', but for +asynchronous processes (and it leaves output processing to the +caller). +--- + emacs/notmuch-lib.el | 73 +++++++++++++++++++++++++++++++++++++++++++++++--- + 1 file changed, 69 insertions(+), 4 deletions(-) + +diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el +index 59b1ce3..a543471 100644 +--- a/emacs/notmuch-lib.el ++++ b/emacs/notmuch-lib.el +@@ -383,18 +383,21 @@ signaled error. This function does not return." + (error "%s" (concat msg (when extra + " (see *Notmuch errors* for more details)")))) + +-(defun notmuch-check-async-exit-status (proc msg) ++(defun notmuch-check-async-exit-status (proc msg &optional command err-file) + "If PROC exited abnormally, pop up an error buffer and signal an error. + + This is a wrapper around `notmuch-check-exit-status' for + asynchronous process sentinels. PROC and MSG must be the +-arguments passed to the sentinel." ++arguments passed to the sentinel. COMMAND and ERR-FILE, if ++provided, are passed to `notmuch-check-exit-status'. If COMMAND ++is not provided, it is taken from `process-command'." + (let ((exit-status + (case (process-status proc) + ((exit) (process-exit-status proc)) + ((signal) msg)))) + (when exit-status +- (notmuch-check-exit-status exit-status (process-command proc))))) ++ (notmuch-check-exit-status exit-status (or command (process-command proc)) ++ nil err-file)))) + + (defun notmuch-check-exit-status (exit-status command &optional output err-file) + "If EXIT-STATUS is non-zero, pop up an error buffer and signal an error. +@@ -448,7 +451,7 @@ You may need to restart Emacs or upgrade your notmuch package.")) + )))) + + (defun notmuch-call-notmuch-json (&rest args) +- "Invoke `notmuch-command' with `args' and return the parsed JSON output. ++ "Invoke `notmuch-command' with ARGS and return the parsed JSON output. + + The returned output will represent objects using property lists + and arrays as lists. If notmuch exits with a non-zero status, +@@ -469,6 +472,68 @@ an error." + (json-read))) + (delete-file err-file))))) + ++(defun notmuch-start-notmuch (name buffer sentinel &rest args) ++ "Start and return an asynchronous notmuch command. ++ ++This starts and returns an asynchronous process running ++`notmuch-command' with ARGS. The exit status is checked via ++`notmuch-check-async-exit-status'. Output written to stderr is ++redirected and displayed when the process exits (even if the ++process exits successfully). NAME and BUFFER are the same as in ++`start-process'. SENTINEL is a process sentinel function to call ++when the process exits, or nil for none. The caller must *not* ++invoke `set-process-sentinel' directly on the returned process, ++as that will interfere with the handling of stderr and the exit ++status." ++ ++ ;; There is no way (as of Emacs 24.3) to capture stdout and stderr ++ ;; separately for asynchronous processes, or even to redirect stderr ++ ;; to a file, so we use a trivial shell wrapper to send stderr to a ++ ;; temporary file and clean things up in the sentinel. ++ (let* ((err-file (make-temp-file "nmerr")) ++ ;; Use a pipe ++ (process-connection-type nil) ++ (proc (apply #'start-process name buffer ++ "sh" "-c" ++ "ERR=\"$1\"; shift; exec \"$0\" \"$@\" 2>\"$ERR\"" ++ notmuch-command err-file args))) ++ (process-put proc 'err-file err-file) ++ (process-put proc 'sub-sentinel sentinel) ++ (process-put proc 'real-command (cons notmuch-command args)) ++ (set-process-sentinel proc #'notmuch-start-notmuch-sentinel) ++ proc)) ++ ++(defun notmuch-start-notmuch-sentinel (proc event) ++ (let ((err-file (process-get proc 'err-file)) ++ (sub-sentinel (process-get proc 'sub-sentinel)) ++ (real-command (process-get proc 'real-command))) ++ (condition-case-unless-debug err ++ (progn ++ ;; Invoke the sub-sentinel, if any ++ (when sub-sentinel ++ (funcall sub-sentinel proc event)) ++ ;; Check the exit status. This will signal an error if the ++ ;; exit status is non-zero. ++ (notmuch-check-async-exit-status proc event real-command err-file) ++ ;; If that didn't signal an error, then any error output was ++ ;; really warning output. Show warnings, if any. ++ (let ((warnings ++ (with-temp-buffer ++ (unless (= (second (insert-file-contents err-file)) 0) ++ (end-of-line) ++ ;; Show first line; stuff remaining lines in the ++ ;; errors buffer. ++ (let ((l1 (buffer-substring (point-min) (point)))) ++ (skip-chars-forward "\n") ++ (cons l1 (unless (eobp) ++ (buffer-substring (point) (point-max))))))))) ++ (when warnings ++ (notmuch-logged-error (car warnings) (cdr warnings))))) ++ (error ++ ;; Don't signal an error from a sentinel ++ (message "%s" (error-message-string err)))) ++ (ignore-errors (delete-file err-file)))) ++ + ;; This variable is used only buffer local, but it needs to be + ;; declared globally first to avoid compiler warnings. + (defvar notmuch-show-process-crypto nil) +-- +1.7.10.4 +