1 Return-Path: <amdragon@mit.edu>
\r
2 X-Original-To: notmuch@notmuchmail.org
\r
3 Delivered-To: notmuch@notmuchmail.org
\r
4 Received: from localhost (localhost [127.0.0.1])
\r
5 by olra.theworths.org (Postfix) with ESMTP id D84CC431FDE
\r
6 for <notmuch@notmuchmail.org>; Fri, 31 May 2013 17:40:45 -0700 (PDT)
\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org
\r
11 X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5
\r
12 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled
\r
13 Received: from olra.theworths.org ([127.0.0.1])
\r
14 by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024)
\r
15 with ESMTP id gv0mi8ebGuU3 for <notmuch@notmuchmail.org>;
\r
16 Fri, 31 May 2013 17:40:41 -0700 (PDT)
\r
17 Received: from dmz-mailsec-scanner-1.mit.edu (dmz-mailsec-scanner-1.mit.edu
\r
19 by olra.theworths.org (Postfix) with ESMTP id 576E0429E38
\r
20 for <notmuch@notmuchmail.org>; Fri, 31 May 2013 17:40:25 -0700 (PDT)
\r
21 X-AuditID: 1209190c-b7f566d000004c69-62-51a942f82a60
\r
22 Received: from mailhub-auth-1.mit.edu ( [18.9.21.35])
\r
23 by dmz-mailsec-scanner-1.mit.edu (Symantec Messaging Gateway) with SMTP
\r
24 id 56.73.19561.8F249A15; Fri, 31 May 2013 20:40:24 -0400 (EDT)
\r
25 Received: from outgoing.mit.edu (outgoing-auth-1.mit.edu [18.9.28.11])
\r
26 by mailhub-auth-1.mit.edu (8.13.8/8.9.2) with ESMTP id r510eD8R008173;
\r
27 Fri, 31 May 2013 20:40:13 -0400
\r
28 Received: from drake.dyndns.org (c-76-21-105-205.hsd1.ca.comcast.net
\r
29 [76.21.105.205]) (authenticated bits=0)
\r
30 (User authenticated as amdragon@ATHENA.MIT.EDU)
\r
31 by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id r510e9ve020941
\r
32 (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);
\r
33 Fri, 31 May 2013 20:40:12 -0400
\r
34 Received: from amthrax by drake.dyndns.org with local (Exim 4.77)
\r
35 (envelope-from <amdragon@mit.edu>)
\r
36 id 1UiZrd-0003QR-CA; Fri, 31 May 2013 20:40:09 -0400
\r
37 From: Austin Clements <amdragon@MIT.EDU>
\r
38 To: notmuch@notmuchmail.org
\r
39 Subject: [PATCH v3 2/6] emacs: Utilities to manage asynchronous notmuch
\r
41 Date: Fri, 31 May 2013 20:40:04 -0400
\r
42 Message-Id: <1370047208-12785-3-git-send-email-amdragon@mit.edu>
\r
43 X-Mailer: git-send-email 1.7.10.4
\r
44 In-Reply-To: <1370047208-12785-1-git-send-email-amdragon@mit.edu>
\r
45 References: <1370047208-12785-1-git-send-email-amdragon@mit.edu>
\r
46 X-Brightmail-Tracker:
\r
47 H4sIAAAAAAAAA+NgFjrGIsWRmVeSWpSXmKPExsUixCmqrPvDaWWgwaG/Fhar5/JYXL85k9ni
\r
48 zcp5rA7MHjtn3WX3OPx1IYvHs1W3mAOYo7hsUlJzMstSi/TtErgyGqZZF3zSqmhcNJG1gXGZ
\r
49 UhcjJ4eEgInEvkO/WSFsMYkL99azdTFycQgJ7GOUWL3mFjuEs5FR4uyka8wQzmkmiZtbZ0KV
\r
50 zWWUmPXgKQtIP5uAhsS2/csZQWwRAWmJnXdng81lFoiT2DLlP1hcWCBQYnPHGTYQm0VAVeLz
\r
51 m7dgcV4BB4nXDR/YIO5QlOh+NgHM5hRwlDh/9R8ziC0EVPP4xEnmCYz8CxgZVjHKpuRW6eYm
\r
52 ZuYUpybrFicn5uWlFuka6uVmluilppRuYgQHlyTPDsY3B5UOMQpwMCrx8HasWhEoxJpYVlyZ
\r
53 e4hRkoNJSZR3tuPKQCG+pPyUyozE4oz4otKc1OJDjBIczEoivPlzgMp5UxIrq1KL8mFS0hws
\r
54 SuK8l1Nu+gsJpCeWpGanphakFsFkZTg4lCR41YBRJCRYlJqeWpGWmVOCkGbi4AQZzgM03A2k
\r
55 hre4IDG3ODMdIn+KUVFKnDcIJCEAksgozYPrhUX/K0ZxoFeEIdp5gIkDrvsV0GAmoMFPrJeD
\r
56 DC5JREhJNTDW72CzW3V85Zq9Pp94DEwUzwR6fjMU+jev8/Rz1ys7lqf6KO6tW7q/6/Tc3AN5
\r
57 yyYX8vh1OpusyVgYsHiuSKn8npKLX6IPdahtEvznc5H5VL1vn47G1fmt07qk/zStKJ109+PW
\r
58 tX3df14pf34y3ezd1x27/xbvirtlUNHP91Rh8fQi/U9LjnxOUWIpzkg01GIuKk4EADmM0hjZ AgAA
\r
59 Cc: tomi.ollila@iki.fi
\r
60 X-BeenThere: notmuch@notmuchmail.org
\r
61 X-Mailman-Version: 2.1.13
\r
63 List-Id: "Use and development of the notmuch mail system."
\r
64 <notmuch.notmuchmail.org>
\r
65 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,
\r
66 <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>
\r
67 List-Archive: <http://notmuchmail.org/pipermail/notmuch>
\r
68 List-Post: <mailto:notmuch@notmuchmail.org>
\r
69 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>
\r
70 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,
\r
71 <mailto:notmuch-request@notmuchmail.org?subject=subscribe>
\r
72 X-List-Received-Date: Sat, 01 Jun 2013 00:40:46 -0000
\r
74 This provides a new notmuch-lib utility to start an asynchronous
\r
75 notmuch process that handles redirecting of stderr and checking of the
\r
76 exit status. This is similar to `notmuch-call-notmuch-json', but for
\r
77 asynchronous processes (and it leaves output processing to the
\r
80 emacs/notmuch-lib.el | 77 +++++++++++++++++++++++++++++++++++++++++++++++---
\r
81 1 file changed, 73 insertions(+), 4 deletions(-)
\r
83 diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el
\r
84 index 59b1ce3..a206808 100644
\r
85 --- a/emacs/notmuch-lib.el
\r
86 +++ b/emacs/notmuch-lib.el
\r
87 @@ -383,18 +383,21 @@ signaled error. This function does not return."
\r
88 (error "%s" (concat msg (when extra
\r
89 " (see *Notmuch errors* for more details)"))))
\r
91 -(defun notmuch-check-async-exit-status (proc msg)
\r
92 +(defun notmuch-check-async-exit-status (proc msg &optional command err-file)
\r
93 "If PROC exited abnormally, pop up an error buffer and signal an error.
\r
95 This is a wrapper around `notmuch-check-exit-status' for
\r
96 asynchronous process sentinels. PROC and MSG must be the
\r
97 -arguments passed to the sentinel."
\r
98 +arguments passed to the sentinel. COMMAND and ERR-FILE, if
\r
99 +provided, are passed to `notmuch-check-exit-status'. If COMMAND
\r
100 +is not provided, it is taken from `process-command'."
\r
102 (case (process-status proc)
\r
103 ((exit) (process-exit-status proc))
\r
106 - (notmuch-check-exit-status exit-status (process-command proc)))))
\r
107 + (notmuch-check-exit-status exit-status (or command (process-command proc))
\r
110 (defun notmuch-check-exit-status (exit-status command &optional output err-file)
\r
111 "If EXIT-STATUS is non-zero, pop up an error buffer and signal an error.
\r
112 @@ -448,7 +451,7 @@ You may need to restart Emacs or upgrade your notmuch package."))
\r
115 (defun notmuch-call-notmuch-json (&rest args)
\r
116 - "Invoke `notmuch-command' with `args' and return the parsed JSON output.
\r
117 + "Invoke `notmuch-command' with ARGS and return the parsed JSON output.
\r
119 The returned output will represent objects using property lists
\r
120 and arrays as lists. If notmuch exits with a non-zero status,
\r
121 @@ -469,6 +472,72 @@ an error."
\r
123 (delete-file err-file)))))
\r
125 +(defun notmuch-start-notmuch (name buffer sentinel &rest args)
\r
126 + "Start and return an asynchronous notmuch command.
\r
128 +This starts and returns an asynchronous process running
\r
129 +`notmuch-command' with ARGS. The exit status is checked via
\r
130 +`notmuch-check-async-exit-status'. Output written to stderr is
\r
131 +redirected and displayed when the process exits (even if the
\r
132 +process exits successfully). NAME and BUFFER are the same as in
\r
133 +`start-process'. SENTINEL is a process sentinel function to call
\r
134 +when the process exits, or nil for none. The caller must *not*
\r
135 +invoke `set-process-sentinel' directly on the returned process,
\r
136 +as that will interfere with the handling of stderr and the exit
\r
139 + ;; There is no way (as of Emacs 24.3) to capture stdout and stderr
\r
140 + ;; separately for asynchronous processes, or even to redirect stderr
\r
141 + ;; to a file, so we use a trivial shell wrapper to send stderr to a
\r
142 + ;; temporary file and clean things up in the sentinel.
\r
143 + (let* ((err-file (make-temp-file "nmerr"))
\r
145 + (process-connection-type nil)
\r
146 + ;; Find notmuch using Emacs' `exec-path'
\r
147 + (command (or (executable-find notmuch-command)
\r
148 + (error "command not found: %s" notmuch-command)))
\r
149 + (proc (apply #'start-process name buffer
\r
151 + "exec 2>\"$1\"; shift; exec \"$0\" \"$@\""
\r
152 + command err-file args)))
\r
153 + (process-put proc 'err-file err-file)
\r
154 + (process-put proc 'sub-sentinel sentinel)
\r
155 + (process-put proc 'real-command (cons notmuch-command args))
\r
156 + (set-process-sentinel proc #'notmuch-start-notmuch-sentinel)
\r
159 +(defun notmuch-start-notmuch-sentinel (proc event)
\r
160 + (let ((err-file (process-get proc 'err-file))
\r
161 + (sub-sentinel (process-get proc 'sub-sentinel))
\r
162 + (real-command (process-get proc 'real-command)))
\r
163 + (condition-case err
\r
165 + ;; Invoke the sub-sentinel, if any
\r
166 + (when sub-sentinel
\r
167 + (funcall sub-sentinel proc event))
\r
168 + ;; Check the exit status. This will signal an error if the
\r
169 + ;; exit status is non-zero.
\r
170 + (notmuch-check-async-exit-status proc event real-command err-file)
\r
171 + ;; If that didn't signal an error, then any error output was
\r
172 + ;; really warning output. Show warnings, if any.
\r
174 + (with-temp-buffer
\r
175 + (unless (= (second (insert-file-contents err-file)) 0)
\r
177 + ;; Show first line; stuff remaining lines in the
\r
178 + ;; errors buffer.
\r
179 + (let ((l1 (buffer-substring (point-min) (point))))
\r
180 + (skip-chars-forward "\n")
\r
181 + (cons l1 (unless (eobp)
\r
182 + (buffer-substring (point) (point-max)))))))))
\r
184 + (notmuch-logged-error (car warnings) (cdr warnings)))))
\r
186 + ;; Emacs behaves strangely if an error escapes from a sentinel,
\r
187 + ;; so turn errors into messages.
\r
188 + (message "%s" (error-message-string err))))
\r
189 + (ignore-errors (delete-file err-file))))
\r
191 ;; This variable is used only buffer local, but it needs to be
\r
192 ;; declared globally first to avoid compiler warnings.
\r
193 (defvar notmuch-show-process-crypto nil)
\r