From: Austin Clements <amdragon@MIT.EDU>
To: notmuch@notmuchmail.org
Subject: [PATCH 2/5] emacs: Utilities to manage asynchronous notmuch processes
Date: Sat, 18 May 2013 00:31:09 -0400 [thread overview]
Message-ID: <1368851472-5382-3-git-send-email-amdragon@mit.edu> (raw)
In-Reply-To: <1368851472-5382-1-git-send-email-amdragon@mit.edu>
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
next prev parent reply other threads:[~2013-05-18 4:33 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-05-18 4:31 [PATCH 0/5] Make Emacs search use sexp format Austin Clements
2013-05-18 4:31 ` [PATCH 1/5] test: Remove extraneous Emacs error handling test Austin Clements
2013-05-18 4:31 ` Austin Clements [this message]
2013-05-21 14:24 ` [PATCH 2/5] emacs: Utilities to manage asynchronous notmuch processes Mark Walters
2013-05-21 18:44 ` Tomi Ollila
2013-05-18 4:31 ` [PATCH 3/5] emacs: Use async process helper for search Austin Clements
2013-05-18 7:14 ` Mark Walters
2013-05-18 10:48 ` David Bremner
2013-05-18 20:13 ` Austin Clements
2013-05-18 4:31 ` [PATCH 4/5] emacs: Streaming S-expression parser Austin Clements
2013-05-21 15:20 ` Mark Walters
2013-05-22 0:03 ` Austin Clements
2013-05-25 8:59 ` Mark Walters
2013-05-27 19:04 ` Austin Clements
2013-05-27 20:58 ` Mark Walters
2013-05-18 4:31 ` [PATCH 5/5] emacs: Use streaming S-expr parser for search Austin Clements
2013-05-21 14:27 ` Mark Walters
2013-05-21 15:26 ` [PATCH 0/5] Make Emacs search use sexp format Mark Walters
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://notmuchmail.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1368851472-5382-3-git-send-email-amdragon@mit.edu \
--to=amdragon@mit.edu \
--cc=notmuch@notmuchmail.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhetil.org/notmuch.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).