From e8fa5c812caadbeb827272c1e30b1938f78589d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20Benn=C3=A9e?= Date: Mon, 20 Feb 2017 17:11:52 +0000 Subject: [PATCH] async: allow passing of initfile to child process (FR#39) Sometimes passing all your state setup in the async elisp call can be painful, especially if the library is being used by another one (in my case ob-async for org-mode). Luckily we have a method for dealing with boilerplate setup called an initfile. This allows an initfile to be specified for all async calls. It does not re-use the existing initfile as the interactive one is likely too cluttered for a child process. --- async.el | 43 ++++++++++++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/async.el b/async.el index 30476e2..7ce6058 100644 --- a/async.el +++ b/async.el @@ -44,6 +44,13 @@ (defvar async-callback-value-set nil) (defvar async-current-process nil) (defvar async--procvar nil) +(defvar async-child-init nil + "Initialisation file for async child Emacs. + +If defined this allows for an init file to setup the child Emacs. It +should not be your normal init.el as that would likely load more +things that you require. It should limit itself to ensuring paths have +been setup so any async code can load libraries you expect.") (defun async-inject-variables (include-regexp &optional predicate exclude-regexp) @@ -231,6 +238,20 @@ working directory." (set (make-local-variable 'async-callback-for-process) t)) proc))) +(defun async--emacs-program-args (&optional sexp) + "Return a list of arguments for invoking the child Emacs." + ;; Using `locate-library' ensure we use the right file + ;; when the .elc have been deleted. + (let ((args (list "-Q" "-l" (locate-library "async")))) + (when async-child-init + (setq args (append args (list "-l" async-child-init)))) + (append args (list "-batch" "-f" "async-batch-invoke" + (if sexp + (with-temp-buffer + (async--insert-sexp (list 'quote sexp)) + (buffer-string)) + ""))))) + ;;;###autoload (defun async-start (start-func &optional finish-func) "Execute START-FUNC (often a lambda) in a subordinate Emacs process. @@ -283,21 +304,13 @@ returns nil. It can still be useful, however, as an argument to ;; Subordinate Emacs will send text encoded in UTF-8. (coding-system-for-read 'utf-8-unix)) (setq async--procvar - (async-start-process - "emacs" (file-truename - (expand-file-name invocation-name - invocation-directory)) - finish-func - "-Q" "-l" - ;; Using `locate-library' ensure we use the right file - ;; when the .elc have been deleted. - (locate-library "async") - "-batch" "-f" "async-batch-invoke" - (if async-send-over-pipe - "" - (with-temp-buffer - (async--insert-sexp (list 'quote sexp)) - (buffer-string))))) + (apply 'async-start-process + "emacs" (file-truename + (expand-file-name invocation-name + invocation-directory)) + finish-func + (async--emacs-program-args (if (not async-send-over-pipe) sexp)))) + (if async-send-over-pipe (async--transmit-sexp async--procvar (list 'quote sexp))) async--procvar))