Re: [PATCH 0/4] Allow specifying alternate names for addresses in other_email
[notmuch-archives.git] / 4e / 961b502904a5bbfd381d9cb58b3afb1e7273ca
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 4495F431FC9\r
6         for <notmuch@notmuchmail.org>; Fri, 17 May 2013 21:33:06 -0700 (PDT)\r
7 X-Virus-Scanned: Debian amavisd-new at olra.theworths.org\r
8 X-Spam-Flag: NO\r
9 X-Spam-Score: -0.7\r
10 X-Spam-Level: \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 wHUQMiUjQ-Qo for <notmuch@notmuchmail.org>;\r
16         Fri, 17 May 2013 21:33:00 -0700 (PDT)\r
17 Received: from dmz-mailsec-scanner-8.mit.edu (DMZ-MAILSEC-SCANNER-8.MIT.EDU\r
18         [18.7.68.37])\r
19         by olra.theworths.org (Postfix) with ESMTP id CC397429E2F\r
20         for <notmuch@notmuchmail.org>; Fri, 17 May 2013 21:32:24 -0700 (PDT)\r
21 X-AuditID: 12074425-b7f986d00000082c-e9-519704581065\r
22 Received: from mailhub-auth-4.mit.edu ( [18.7.62.39])\r
23         by dmz-mailsec-scanner-8.mit.edu (Symantec Messaging Gateway) with SMTP\r
24         id FE.89.02092.85407915; Sat, 18 May 2013 00:32:24 -0400 (EDT)\r
25 Received: from outgoing.mit.edu (OUTGOING-AUTH-1.MIT.EDU [18.9.28.11])\r
26         by mailhub-auth-4.mit.edu (8.13.8/8.9.2) with ESMTP id r4I4WDPb009845; \r
27         Sat, 18 May 2013 00:32:13 -0400\r
28 Received: from drake.dyndns.org\r
29         (216-15-114-40.c3-0.arl-ubr1.sbo-arl.ma.cable.rcn.com\r
30         [216.15.114.40]) (authenticated bits=0)\r
31         (User authenticated as amdragon@ATHENA.MIT.EDU)\r
32         by outgoing.mit.edu (8.13.8/8.12.4) with ESMTP id r4I4WBMD018833\r
33         (version=TLSv1/SSLv3 cipher=AES256-SHA bits=256 verify=NOT);\r
34         Sat, 18 May 2013 00:32:12 -0400\r
35 Received: from amthrax by drake.dyndns.org with local (Exim 4.77)\r
36         (envelope-from <amdragon@mit.edu>)\r
37         id 1UdYo1-0001Yw-JW; Sat, 18 May 2013 00:31:41 -0400\r
38 From: Austin Clements <amdragon@MIT.EDU>\r
39 To: notmuch@notmuchmail.org\r
40 Subject: [PATCH 2/5] emacs: Utilities to manage asynchronous notmuch processes\r
41 Date: Sat, 18 May 2013 00:31:09 -0400\r
42 Message-Id: <1368851472-5382-3-git-send-email-amdragon@mit.edu>\r
43 X-Mailer: git-send-email 1.7.10.4\r
44 In-Reply-To: <1368851472-5382-1-git-send-email-amdragon@mit.edu>\r
45 References: <1368851472-5382-1-git-send-email-amdragon@mit.edu>\r
46 X-Brightmail-Tracker:\r
47  H4sIAAAAAAAAA+NgFtrDIsWRmVeSWpSXmKPExsUixG6nrhvBMj3Q4M8XPovVc3ksrt+cyezA\r
48         5LFz1l12j2erbjEHMEVx2aSk5mSWpRbp2yVwZby4cJOpYK1mxcttl5gaGCcqdjFyckgImEhM\r
49         n/+BEcIWk7hwbz0biC0ksI9RYsV24y5GLiB7I6PE3wmn2SCcO0wSHUves0M4cxklHh9bxg7S\r
50         wiagIbFt/3KwUSIC0hI7785mBbGZBRwlPu9fBDZWWMBP4tzhtWA1LAKqEn/+LgOL8wrYS/w4\r
51         280KcYaiRPezCWBxTgEHicPzj0GdZC/RdeEm2wRG/gWMDKsYZVNyq3RzEzNzilOTdYuTE/Py\r
52         Uot0LfRyM0v0UlNKNzGCA8lFdQfjhENKhxgFOBiVeHhnOE8LFGJNLCuuzD3EKMnBpCTKa/IP\r
53         KMSXlJ9SmZFYnBFfVJqTWnyIUYKDWUmE93kxUI43JbGyKrUoHyYlzcGiJM57I+Wmv5BAemJJ\r
54         anZqakFqEUxWhoNDSYK3iHl6oJBgUWp6akVaZk4JQpqJgxNkOA/Q8HKQGt7igsTc4sx0iPwp\r
55         RkUpcd5ikIQASCKjNA+uFxbprxjFgV4R5p0EUsUDTBJw3a+ABjMBDWa9NhVkcEkiQkqqgVGb\r
56         427wkn75uHePb3ZN49zFctL8yzqv7QLP+q5tt13FZ/jBhelF3cYqtsVeZTO43c6mCtt5a3h8\r
57         rd2i87FgiWLbcb9vjdofdL/OnjfzeAmPXc+DXcqXliqXLVvasuHexRk+N/SPNN+++PBygMPL\r
58         iMzDttNiZcVP7bQtmfP/6ZnJk2fd7F7kVvZWiaU4I9FQi7moOBEAG+GmBc8CAAA=\r
59 X-BeenThere: notmuch@notmuchmail.org\r
60 X-Mailman-Version: 2.1.13\r
61 Precedence: list\r
62 List-Id: "Use and development of the notmuch mail system."\r
63         <notmuch.notmuchmail.org>\r
64 List-Unsubscribe: <http://notmuchmail.org/mailman/options/notmuch>,\r
65         <mailto:notmuch-request@notmuchmail.org?subject=unsubscribe>\r
66 List-Archive: <http://notmuchmail.org/pipermail/notmuch>\r
67 List-Post: <mailto:notmuch@notmuchmail.org>\r
68 List-Help: <mailto:notmuch-request@notmuchmail.org?subject=help>\r
69 List-Subscribe: <http://notmuchmail.org/mailman/listinfo/notmuch>,\r
70         <mailto:notmuch-request@notmuchmail.org?subject=subscribe>\r
71 X-List-Received-Date: Sat, 18 May 2013 04:33:06 -0000\r
72 \r
73 This provides a new notmuch-lib utility to start an asynchronous\r
74 notmuch process that handles redirecting of stderr and checking of the\r
75 exit status.  This is similar to `notmuch-call-notmuch-json', but for\r
76 asynchronous processes (and it leaves output processing to the\r
77 caller).\r
78 ---\r
79  emacs/notmuch-lib.el |   73 +++++++++++++++++++++++++++++++++++++++++++++++---\r
80  1 file changed, 69 insertions(+), 4 deletions(-)\r
81 \r
82 diff --git a/emacs/notmuch-lib.el b/emacs/notmuch-lib.el\r
83 index 59b1ce3..a543471 100644\r
84 --- a/emacs/notmuch-lib.el\r
85 +++ b/emacs/notmuch-lib.el\r
86 @@ -383,18 +383,21 @@ signaled error.  This function does not return."\r
87    (error "%s" (concat msg (when extra\r
88                             " (see *Notmuch errors* for more details)"))))\r
89  \r
90 -(defun notmuch-check-async-exit-status (proc msg)\r
91 +(defun notmuch-check-async-exit-status (proc msg &optional command err-file)\r
92    "If PROC exited abnormally, pop up an error buffer and signal an error.\r
93  \r
94  This is a wrapper around `notmuch-check-exit-status' for\r
95  asynchronous process sentinels.  PROC and MSG must be the\r
96 -arguments passed to the sentinel."\r
97 +arguments passed to the sentinel.  COMMAND and ERR-FILE, if\r
98 +provided, are passed to `notmuch-check-exit-status'.  If COMMAND\r
99 +is not provided, it is taken from `process-command'."\r
100    (let ((exit-status\r
101          (case (process-status proc)\r
102            ((exit) (process-exit-status proc))\r
103            ((signal) msg))))\r
104      (when exit-status\r
105 -      (notmuch-check-exit-status exit-status (process-command proc)))))\r
106 +      (notmuch-check-exit-status exit-status (or command (process-command proc))\r
107 +                                nil err-file))))\r
108  \r
109  (defun notmuch-check-exit-status (exit-status command &optional output err-file)\r
110    "If EXIT-STATUS is non-zero, pop up an error buffer and signal an error.\r
111 @@ -448,7 +451,7 @@ You may need to restart Emacs or upgrade your notmuch package."))\r
112         ))))\r
113  \r
114  (defun notmuch-call-notmuch-json (&rest args)\r
115 -  "Invoke `notmuch-command' with `args' and return the parsed JSON output.\r
116 +  "Invoke `notmuch-command' with ARGS and return the parsed JSON output.\r
117  \r
118  The returned output will represent objects using property lists\r
119  and arrays as lists.  If notmuch exits with a non-zero status,\r
120 @@ -469,6 +472,68 @@ an error."\r
121               (json-read)))\r
122         (delete-file err-file)))))\r
123  \r
124 +(defun notmuch-start-notmuch (name buffer sentinel &rest args)\r
125 +  "Start and return an asynchronous notmuch command.\r
126 +\r
127 +This starts and returns an asynchronous process running\r
128 +`notmuch-command' with ARGS.  The exit status is checked via\r
129 +`notmuch-check-async-exit-status'.  Output written to stderr is\r
130 +redirected and displayed when the process exits (even if the\r
131 +process exits successfully).  NAME and BUFFER are the same as in\r
132 +`start-process'.  SENTINEL is a process sentinel function to call\r
133 +when the process exits, or nil for none.  The caller must *not*\r
134 +invoke `set-process-sentinel' directly on the returned process,\r
135 +as that will interfere with the handling of stderr and the exit\r
136 +status."\r
137 +\r
138 +  ;; There is no way (as of Emacs 24.3) to capture stdout and stderr\r
139 +  ;; separately for asynchronous processes, or even to redirect stderr\r
140 +  ;; to a file, so we use a trivial shell wrapper to send stderr to a\r
141 +  ;; temporary file and clean things up in the sentinel.\r
142 +  (let* ((err-file (make-temp-file "nmerr"))\r
143 +        ;; Use a pipe\r
144 +        (process-connection-type nil)\r
145 +        (proc (apply #'start-process name buffer\r
146 +                     "sh" "-c"\r
147 +                     "ERR=\"$1\"; shift; exec \"$0\" \"$@\" 2>\"$ERR\""\r
148 +                     notmuch-command err-file args)))\r
149 +    (process-put proc 'err-file err-file)\r
150 +    (process-put proc 'sub-sentinel sentinel)\r
151 +    (process-put proc 'real-command (cons notmuch-command args))\r
152 +    (set-process-sentinel proc #'notmuch-start-notmuch-sentinel)\r
153 +    proc))\r
154 +\r
155 +(defun notmuch-start-notmuch-sentinel (proc event)\r
156 +  (let ((err-file (process-get proc 'err-file))\r
157 +       (sub-sentinel (process-get proc 'sub-sentinel))\r
158 +       (real-command (process-get proc 'real-command)))\r
159 +    (condition-case-unless-debug err\r
160 +       (progn\r
161 +         ;; Invoke the sub-sentinel, if any\r
162 +         (when sub-sentinel\r
163 +           (funcall sub-sentinel proc event))\r
164 +         ;; Check the exit status.  This will signal an error if the\r
165 +         ;; exit status is non-zero.\r
166 +         (notmuch-check-async-exit-status proc event real-command err-file)\r
167 +         ;; If that didn't signal an error, then any error output was\r
168 +         ;; really warning output.  Show warnings, if any.\r
169 +         (let ((warnings\r
170 +                (with-temp-buffer\r
171 +                  (unless (= (second (insert-file-contents err-file)) 0)\r
172 +                    (end-of-line)\r
173 +                    ;; Show first line; stuff remaining lines in the\r
174 +                    ;; errors buffer.\r
175 +                    (let ((l1 (buffer-substring (point-min) (point))))\r
176 +                      (skip-chars-forward "\n")\r
177 +                      (cons l1 (unless (eobp)\r
178 +                                 (buffer-substring (point) (point-max)))))))))\r
179 +           (when warnings\r
180 +             (notmuch-logged-error (car warnings) (cdr warnings)))))\r
181 +      (error\r
182 +       ;; Don't signal an error from a sentinel\r
183 +       (message "%s" (error-message-string err))))\r
184 +    (ignore-errors (delete-file err-file))))\r
185 +\r
186  ;; This variable is used only buffer local, but it needs to be\r
187  ;; declared globally first to avoid compiler warnings.\r
188  (defvar notmuch-show-process-crypto nil)\r
189 -- \r
190 1.7.10.4\r
191 \r