branch: externals/detached commit 09b37f681c68572aa18496994852a041e6c0704a Author: Niklas Eklund <niklas.ekl...@posteo.net> Commit: Niklas Eklund <niklas.ekl...@posteo.net>
Add detached-with-session macro --- detached-compile.el | 37 ++++++++++++--------------- detached.el | 73 ++++++++++++++++++++++++++++------------------------- 2 files changed, 54 insertions(+), 56 deletions(-) diff --git a/detached-compile.el b/detached-compile.el index a9b9cb9b63..26b3926c18 100644 --- a/detached-compile.el +++ b/detached-compile.el @@ -65,19 +65,16 @@ Optionally enable COMINT if prefix-argument is provided." "Re-compile by running `compile' but in a 'detached' session. Optionally EDIT-COMMAND." (interactive "P") - (let* ((detached-enabled t) - (detached-session-action detached-compile-session-action) - (detached-session-origin 'compile) - (detached-session-mode 'attached) - (command - (if edit-command - (compilation-read-command - (detached-session-command detached-buffer-session)) - (detached-session-command detached-buffer-session))) - (detached-session-environment - (detached--session-env detached-buffer-session)) - (detached-current-session (detached-create-session command))) - (apply #'compilation-start `(,command)))) + (detached-with-session detached-buffer-session + (let* ((detached-enabled t) + (detached-session-mode 'attached) + (detached-session-command + (if edit-command + (compilation-read-command + (detached-session-command detached-buffer-session)) + (detached-session-command detached-buffer-session))) + (detached-current-session (detached-create-session detached-session-command))) + (apply #'compilation-start `(,detached-session-command))))) (defun detached-compile-kill () "Kill a 'detached' session." @@ -90,18 +87,16 @@ Optionally EDIT-COMMAND." (defun detached-compile-attach (session) "Attach to SESSION with `compile'." (when (detached-valid-session session) - (let* ((detached-enabled t) - (detached-current-session session) - (detached-local-session (detached-session-local-p session)) - (default-directory (detached-session-directory session))) - (compilation-start (detached-session-command session))))) + (detached-with-session session + (let* ((detached-enabled t)) + (compilation-start detached-session-command))))) ;;;###autoload (defun detached-compile-start-session (session) "Start SESSION with `detached-compile'." - (let* ((detached-enabled t) - (detached-current-session session)) - (detached-compile (detached-session-command session)))) + (detached-with-session session + (let* ((detached-enabled t)) + (detached-compile detached-session-command)))) ;;;;; Support functions diff --git a/detached.el b/detached.el index bb14c9bf07..a63e37d157 100644 --- a/detached.el +++ b/detached.el @@ -416,6 +416,19 @@ This version is encoded as [package-version].[revision].") (progn ,@body)))) +(defmacro detached-with-session (session &rest body) + "A macro that set up SESSION's environment before evaluating BODY." + (declare (indent 1)) + `(let ((default-directory (detached-session-working-directory ,session)) + (detached-session-origin (detached--session-origin ,session)) + (detached-local-session (detached-session-local-p ,session)) + (detached-session-mode (detached--session-initial-mode ,session)) + (detached-session-action (detached--session-action ,session)) + (detached-session-command (detached-session-command ,session)) + (detached-session-environment (detached--session-env ,session)) + (detached-current-session ,session)) + ,@body)) + ;;;; Commands ;;;###autoload @@ -495,22 +508,20 @@ Optionally TOGGLE-SESSION-MODE." (interactive (list (detached-session-in-context) current-prefix-arg)) - (when-let* ((detached-session-command - (read-string "Edit command: " - (detached-session-command session)))) - (let* ((detached-session-mode - (if toggle-session-mode - (if (eq 'detached (detached--session-initial-mode session)) - 'attached - 'detached) - (detached--session-initial-mode session))) - (default-directory (detached-session-working-directory session)) - (detached-local-session (detached-session-local-p session)) - (detached-session-action (detached--session-action session)) - (detached-session-environment (detached--session-env session)) - (detached-current-session - (detached-create-session detached-session-command))) - (detached-start-session detached-current-session)))) + (when session + (detached-with-session session + (when-let* ((detached-session-command + (read-string "Edit command: " + (detached-session-command session))) + (detached-session-mode + (if toggle-session-mode + (if (eq 'detached (detached--session-initial-mode session)) + 'attached + 'detached) + (detached--session-initial-mode session))) + (detached-current-session + (detached-create-session detached-session-command))) + (detached-start-session detached-current-session))))) ;;;###autoload (defun detached-rerun-session (session &optional toggle-session-mode) @@ -520,20 +531,16 @@ Optionally TOGGLE-SESSION-MODE." (interactive (list (detached-session-in-context) current-prefix-arg)) - (let* ((detached-session-mode - (if toggle-session-mode - (if (eq 'detached (detached--session-initial-mode session)) - 'attached - 'detached) - (detached--session-initial-mode session))) - ;; TODO: Implement macro detached-with-session - (default-directory (detached-session-working-directory session)) - (detached-local-session (detached-session-local-p session)) - (detached-session-action (detached--session-action session)) - (detached-session-environment (detached--session-env session)) - (detached-current-session - (detached-create-session (detached-session-command session)))) - (detached-start-session detached-current-session))) + (when session + (detached-with-session session + (let* ((detached-session-mode + (if toggle-session-mode + (if (eq 'detached (detached--session-initial-mode session)) + 'attached + 'detached) + (detached--session-initial-mode session))) + (detached-current-session (detached-create-session detached-session-command))) + (detached-start-session detached-current-session))))) ;;;###autoload (defun detached-describe-session () @@ -925,11 +932,7 @@ This function uses the `notifications' library." (detached-session-start-command session :type 'string)) - (let* ((default-directory (detached-session-working-directory session)) - (detached-local-session (detached-session-local-p session)) - (detached-session-mode (detached--session-initial-mode session)) - (detached-session-action (detached--session-action session)) - (detached-session-environment (detached--session-env session))) + (detached-with-session session (funcall (detached-session-run-function session) session)))) (cl-defun detached-session-start-command (session &key type)