1 ;;; async --- Asynchronous processing in Emacs
3 ;; Copyright (C) 2012 John Wiegley
5 ;; Author: John Wiegley <jwiegley@gmail.com>
6 ;; Created: 18 Jun 2012
9 ;; X-URL: https://github.com/jwiegley/emacs-async
11 ;; This program is free software; you can redistribute it and/or
12 ;; modify it under the terms of the GNU General Public License as
13 ;; published by the Free Software Foundation; either version 2, or (at
14 ;; your option) any later version.
16 ;; This program is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ;; General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; Adds the ability to call asynchronous functions and process with ease. See
29 ;; the documentation for `async-start' and `async-start-process'.
34 "Simple asynchronous processing in Emacs"
37 (defvar async-callback nil)
38 (defvar async-callback-for-process nil)
39 (defvar async-callback-value nil)
40 (defvar async-callback-value-set nil)
42 (defmacro async-inject-variables
43 (include-regexp &optional predicate exclude-regexp)
44 "Return a `setq' form that replicates part of the calling environment.
45 It sets the value for every variable matching INCLUDE-REGEXP and
46 also PREDICATE. It will not perform injection for any variable
47 matching EXCLUDE-REGEXP (if present). It is intended to be used
54 (insert ,(buffer-substring-no-properties (point-min) (point-max)))
55 ;; Pass in the variable environment for smtpmail
56 ,(async-inject-variables \"\\`\\(smtpmail\\|\\(user-\\)?mail\\)-\")
64 (or (null include-regexp)
65 (string-match include-regexp (symbol-name sym)))
67 (or exclude-regexp "-syntax-table\\'")
69 (let ((value (symbol-value sym)))
70 (when (funcall (or predicate
72 (let ((value (symbol-value sym)))
73 (or (not (functionp value))
76 (setq bindings (cons `(quote ,value) bindings)
77 bindings (cons sym bindings)))))))
80 (defalias 'async-inject-environment 'async-inject-variables)
82 (defun async-when-done (proc &optional change)
83 "Process sentinal used to retrieve the value from the child process."
84 (when (eq 'exit (process-status proc))
85 (with-current-buffer (process-buffer proc)
86 (if (= 0 (process-exit-status proc))
87 (if async-callback-for-process
90 (funcall async-callback proc)
91 (kill-buffer (current-buffer)))
92 (set (make-local-variable 'async-callback-value) proc)
93 (set (make-local-variable 'async-callback-value-set) t))
94 (goto-char (point-max))
96 (let ((result (read (current-buffer))))
97 (if (and (listp result)
98 (eq 'async-signal (car result)))
99 (if (eq 'error (car (cdr result)))
100 (error (cadr (cdr result)))
101 (signal (cadr result)
105 (funcall async-callback result)
106 (kill-buffer (current-buffer)))
107 (set (make-local-variable 'async-callback-value) result)
108 (set (make-local-variable 'async-callback-value-set) t)))))
109 (set (make-local-variable 'async-callback-value) 'error)
110 (set (make-local-variable 'async-callback-value-set) t)
111 (error "Async Emacs process failed with exit code %d"
112 (process-exit-status proc))))))
114 (defun async-batch-invoke ()
115 "Called from the child Emacs process' command-line."
117 (prin1 (funcall (eval (read nil))))
119 (prin1 `(async-signal . ,err)))))
121 (defun async-ready (future)
122 "Query a FUTURE to see if the ready is ready -- i.e., if no blocking
123 would result from a call to `async-get' on that FUTURE."
124 (and (eq 'exit (process-status future))
125 async-callback-value-set))
127 (defun async-wait (future)
128 "Wait for FUTURE to become ready."
129 (while (not (async-ready future))
132 (defun async-get (future)
133 "Get the value from an asynchronously function when it is ready.
134 FUTURE is returned by `async-start' or `async-start-process' when
135 its FINISH-FUNC is nil."
137 (with-current-buffer (process-buffer future)
140 (kill-buffer (current-buffer)))))
143 (defmacro async-start-process (name program finish-func &rest program-args)
144 "Start the executable PROGRAM asynchronously. See `async-start'.
145 PROGRAM is passed PROGRAM-ARGS, calling FINISH-FUNC with the
146 process object when done. If FINISH-FUNC is nil, the future
147 object will return the process object when the program is
149 (let ((bufvar (make-symbol "buf"))
150 (procvar (make-symbol "proc")))
151 `(let* ((,bufvar (generate-new-buffer ,(concat "*" name "*")))
153 ,`(apply #'start-process ,name ,bufvar ,program
154 (quote ,program-args))))
155 (with-current-buffer ,bufvar
156 (set (make-local-variable 'async-callback) ,finish-func)
157 (set-process-sentinel ,procvar #'async-when-done)
158 ,(unless (string= name "emacs")
159 '(set (make-local-variable 'async-callback-for-process) t))
163 (defmacro async-start (start-func &optional finish-func)
164 "Execute START-FUNC (often a lambda) in a subordinate Emacs process.
165 When done, the return value is passed to FINISH-FUNC. Example:
168 ;; What to do in the child process
170 (message \"This is a test\")
174 ;; What to do when it finishes
176 (message \"Async process done, result should be 222: %s\"
179 If FINISH-FUNC is nil or missing, a future is returned that can
180 be inspected using `async-get', blocking until the value is
183 (let ((proc (async-start
184 ;; What to do in the child process
186 (message \"This is a test\")
190 (message \"I'm going to do some work here\") ;; ....
192 (message \"Waiting on async process, result should be 222: %s\"
195 If you don't want to use a callback, and you don't care about any
196 return value form the child process, pass the `ignore' symbol as
197 the second argument (if you don't, and never call `async-get', it
198 will leave *emacs* process buffers hanging around):
202 (delete-file \"a remote file on a slow link\" nil))
205 Note: Even when FINISH-FUNC is present, a future is still
206 returned except that it yields no value (since the value is
207 passed to FINISH-FUNC). Call `async-get' on such a future always
208 returns nil. It can still be useful, however, as an argument to
209 `async-ready' or `async-wait'."
211 (let ((procvar (make-symbol "proc")))
213 (async-start-process "emacs" (expand-file-name invocation-name
214 invocation-directory)
216 "-Q" "-l" ,(find-library-name "async")
217 "-batch" "-f" "async-batch-invoke")))
218 (with-current-buffer (process-buffer ,procvar)
220 (let ((print-escape-newlines t))
221 (prin1 (list 'quote ,start-func) (current-buffer)))
223 (process-send-region ,procvar (point-min) (point-max))
224 (process-send-eof ,procvar))
227 (defun async-test-1 ()
229 (message "Starting async-test-1...")
231 ;; What to do in the child process
233 (message "This is a test")
237 ;; What to do when it finishes
239 (message "Async process done, result should be 222: %s" result)))
240 (message "Starting async-test-1...done"))
242 (defun async-test-2 ()
244 (message "Starting async-test-2...")
245 (let ((proc (async-start
246 ;; What to do in the child process
248 (message "This is a test")
251 (message "I'm going to do some work here")
253 (message "Async process done, result should be 222: %s"
256 (defun async-test-3 ()
258 (message "Starting async-test-3...")
260 ;; What to do in the child process
262 (message "This is a test")
264 (error "Error in child process")
267 ;; What to do when it finishes
269 (message "Async process done, result should be 222: %s" result)))
270 (message "Starting async-test-1...done"))
272 (defun async-test-4 ()
274 (message "Starting async-test-4...")
275 (async-start-process "sleep" "sleep"
276 ;; What to do when it finishes
278 (message "Sleep done, exit code was %d"
279 (process-exit-status proc)))
281 (message "Starting async-test-4...done"))
285 ;;; async.el ends here