From 92c900ee6bca40b4b1eb09396e26c0d4c05310b1 Mon Sep 17 00:00:00 2001 From: Austin Clements Date: Fri, 31 May 2013 13:13:33 +2000 Subject: [PATCH] [PATCH v2 2/5] emacs: Utilities to manage asynchronous notmuch processes --- 47/d5ecc5a5d4421f70e173d2c65574de56c6c96f | 196 ++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 47/d5ecc5a5d4421f70e173d2c65574de56c6c96f diff --git a/47/d5ecc5a5d4421f70e173d2c65574de56c6c96f b/47/d5ecc5a5d4421f70e173d2c65574de56c6c96f new file mode 100644 index 000000000..1a9948169 --- /dev/null +++ b/47/d5ecc5a5d4421f70e173d2c65574de56c6c96f @@ -0,0 +1,196 @@ +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 151A3431FC4 + for ; Thu, 30 May 2013 10:13:53 -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 kT+ZeLWPDeAD for ; + Thu, 30 May 2013 10:13:45 -0700 (PDT) +Received: from dmz-mailsec-scanner-4.mit.edu (dmz-mailsec-scanner-4.mit.edu + [18.9.25.15]) + by olra.theworths.org (Postfix) with ESMTP id E4685431FBC + for ; Thu, 30 May 2013 10:13:44 -0700 (PDT) +X-AuditID: 1209190f-b7f256d000005616-ac-51a788c80362 +Received: from mailhub-auth-1.mit.edu ( [18.9.21.35]) + by dmz-mailsec-scanner-4.mit.edu (Symantec Messaging Gateway) with SMTP + id 8D.C2.22038.8C887A15; Thu, 30 May 2013 13:13:44 -0400 (EDT) +Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11]) + by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id r4UHDgQf027511; + Thu, 30 May 2013 13:13:43 -0400 +Received: from drake.dyndns.org (c-76-21-105-205.hsd1.ca.comcast.net + [76.21.105.205]) (authenticated bits=0) + (User authenticated as amdragon@ATHENA.MIT.EDU) + by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id r4UHDcnX009862 + (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT); + Thu, 30 May 2013 13:13:41 -0400 +Received: from amthrax by drake.dyndns.org with local (Exim 4.77) + (envelope-from ) + id 1Ui6Py-0005r2-8V; Thu, 30 May 2013 13:13:38 -0400 +From: Austin Clements +To: notmuch@notmuchmail.org +Subject: [PATCH v2 2/5] emacs: Utilities to manage asynchronous notmuch + processes +Date: Thu, 30 May 2013 13:13:33 -0400 +Message-Id: <1369934016-22308-3-git-send-email-amdragon@mit.edu> +X-Mailer: git-send-email 1.7.10.4 +In-Reply-To: <1369934016-22308-1-git-send-email-amdragon@mit.edu> +References: <1369934016-22308-1-git-send-email-amdragon@mit.edu> +X-Brightmail-Tracker: + H4sIAAAAAAAAA+NgFjrGIsWRmVeSWpSXmKPExsUixCmqrHuiY3mgwa7J0har5/JYXL85k9ni + zcp5rA7MHjtn3WX3OPx1IYvHs1W3mAOYo7hsUlJzMstSi/TtErgyHq0wL/ijVTHv0i/WBsb1 + Sl2MnBwSAiYS3W032CBsMYkL99YD2VwcQgL7GCUezHnIAuFsZJR4vKeDFcI5zSQxY/9XZghn + LqPEn4WrGEH62QQ0JLbtXw5miwhIS+y8O5sVxGYWiJPYMuU/WFxYIFDi7IvJ7CA2i4CqxIbV + d1lAbF4BB4lVd1ezQNyhKNH9bALYTZwCjhI7Jp8GmyMEVHNp71emCYz8CxgZVjHKpuRW6eYm + ZuYUpybrFicn5uWlFuma6OVmluilppRuYgQHlyT/DsZvB5UOMQpwMCrx8GYkLQ8UYk0sK67M + PcQoycGkJMq7vhUoxJeUn1KZkVicEV9UmpNafIhRgoNZSYS3wBkox5uSWFmVWpQPk5LmYFES + 572actNfSCA9sSQ1OzW1ILUIJivDwaEkwXu9HahRsCg1PbUiLTOnBCHNxMEJMpwHaPgFkBre + 4oLE3OLMdIj8KUZFKXHe+yAJAZBERmkeXC8s+l8xigO9Isz7CKSKB5g44LpfAQ1mAhr8xBps + cEkiQkqqgXE+24Uzd+Qnm4snfmhjndPH9nvZ0aOmRtKuf/t5Coo4wk2Ymcv2WsbseKyo9Ugr + TYHphOvVqt2iC7d6em+84PF5zs5/7CtYawQ412/d+Vr7w4a2kr0hPgeqtv3bWW7f2/qV+2Py + 6xNV13winx1IXBDVdoPnM0edr5ofqxLbR8fIrb/84w//OqLEUpyRaKjFXFScCAA6kvJK2QIA AA== +Cc: tomi.ollila@iki.fi +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: Thu, 30 May 2013 17:13:53 -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 | 77 +++++++++++++++++++++++++++++++++++++++++++++++--- + 1 file changed, 73 insertions(+), 4 deletions(-) + +diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el +index 59b1ce3..180f63d 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,72 @@ 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) ++ ;; Find notmuch using Emacs' `exec-path' ++ (command (or (executable-find notmuch-command) ++ (error "command not found: %s" notmuch-command))) ++ (proc (apply #'start-process name buffer ++ "sh" "-c" ++ "ERR=\"$1\"; shift; exec \"$0\" \"$@\" 2>\"$ERR\"" ++ 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 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 ++ ;; Emacs behaves strangely if error an error escapes from a ++ ;; sentinel, so turns errors into messages. ++ (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 + -- 2.26.2