From 4c71e77b83c0c35d39f091f47267377feb5585df Mon Sep 17 00:00:00 2001 From: York Zhao Date: Wed, 28 Dec 2022 09:23:34 -0500 Subject: [PATCH 1/3] Add `default-directory' to `body' in `org-babel-execute:tmux' Change the current directory to `default-directory' before executing commands in `org-babel-execute:tmux'. This allows the `:dir' header argument to take effect in the tmux code block. --- ob-tmux.el | 1 + 1 file changed, 1 insertion(+) diff --git a/ob-tmux.el b/ob-tmux.el index 4c52124..f86a8db 100644 --- a/ob-tmux.el +++ b/ob-tmux.el @@ -89,6 +89,7 @@ Change in case you want to use a different tmux than the one in your $PATH." Argument BODY the body of the tmux code block. Argument PARAMS the org parameters of the code block." (message "Sending source code block to interactive terminal session...") + (setq body (format "cd %s\n%s" default-directory body)) (save-window-excursion (let* ((org-session (cdr (assq :session params))) (org-header-terminal (cdr (assq :terminal params))) From fb833c72bb8bf3a60986e06ea02acb7bf6e33891 Mon Sep 17 00:00:00 2001 From: York Zhao Date: Wed, 28 Dec 2022 19:43:00 -0500 Subject: [PATCH 2/3] Use blocking call-process instead of non-blocking start-process In the `ob-tmux--execute' and `ob-tmux--start-terminal-window' functions, replace the non-blocking `start-process' function with the blocking `call-process' function to ensure that the operation is completed before returning. This also allows the function to handle errors by checking the return value of `call-process'. - Use `call-process' instead of `start-process' - Check return value of `call-process' and handle error --- ob-tmux.el | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/ob-tmux.el b/ob-tmux.el index f86a8db..c3f6d0e 100644 --- a/ob-tmux.el +++ b/ob-tmux.el @@ -174,17 +174,17 @@ Argument OB-SESSION: the current ob-tmux session." ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (defun ob-tmux--execute (ob-session &rest args) - "Execute a tmux command with arguments as given. + "Execute a tmux command with the given arguments. -Argument OB-SESSION: the current ob-tmux session. +OB-SESSION: the current ob-tmux session. Optional command-line arguments can be passed in ARGS." - (if (ob-tmux--socket ob-session) - (apply 'start-process "ob-tmux" "*Messages*" - org-babel-tmux-location - "-S" (ob-tmux--socket ob-session) - args) - (apply 'start-process - "ob-tmux" "*Messages*" org-babel-tmux-location args))) + (let ((socket (ob-tmux--socket ob-session))) + (unless (zerop (apply #'call-process org-babel-tmux-location + nil nil nil + (if socket + (append `("-S" ,socket) args) + args))) + (error "Error executing tmux command")))) (defun ob-tmux--execute-string (ob-session &rest args) "Execute a tmux command with arguments as given. @@ -203,16 +203,13 @@ automatically space separated." "Start a TERMINAL window with tmux attached to session. Argument OB-SESSION: the current ob-tmux session." - (let ((start-process-mandatory-args `("org-babel: terminal" - "*Messages*" - ,terminal)) - (tmux-cmd `(,org-babel-tmux-location - "attach-session" - "-t" ,(ob-tmux--target ob-session)))) + (let ((tmux-cmd `(,org-babel-tmux-location + "attach-session" "-t" ,(ob-tmux--target ob-session)))) (unless (ob-tmux--socket ob-session) - (apply 'start-process (append start-process-mandatory-args - org-babel-tmux-terminal-opts - tmux-cmd))))) + (unless (zerop (apply #'call-process terminal nil nil nil + (append org-babel-tmux-terminal-opts + tmux-cmd))) + (error "Error executing tmux command"))))) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Tmux interaction From ee13473320237b18492cd1a79f53fc6eba19db72 Mon Sep 17 00:00:00 2001 From: York Zhao Date: Wed, 28 Dec 2022 22:05:05 -0500 Subject: [PATCH 3/3] Kill default session before creating new one In the `org-babel-execute:tmux' function, if the current session is a default session, kill it before creating a new one. This ensures that a new session is always created, even if a default session already exists. This is necessary for safety concerns, as reusing the default session may result in unexpected behavior due to leftover variables from previous sessions. - Check if current session is default session - Kill default session before creating new one --- ob-tmux.el | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/ob-tmux.el b/ob-tmux.el index c3f6d0e..445b899 100644 --- a/ob-tmux.el +++ b/ob-tmux.el @@ -103,7 +103,14 @@ Argument PARAMS the org parameters of the code block." (session-alive (ob-tmux--session-alive-p ob-session)) (window-alive (ob-tmux--window-alive-p ob-session))) ;; Create tmux session and window if they do not yet exist - (unless session-alive (ob-tmux--create-session ob-session)) + (when (and session-alive + (ob-tmux--default-session-p ob-session)) + (ob-tmux--kill-session ob-session) + (setq session-alive nil) + (message "ob-tmux: killed default session %s" + (ob-tmux--session ob-session))) + (unless session-alive + (ob-tmux--create-session ob-session)) (unless window-alive (ob-tmux--create-window ob-session)) ;; Start terminal window if the session does not yet exist (unless session-alive @@ -168,6 +175,12 @@ Argument OB-SESSION: the current ob-tmux session." (target-window (if window (concat "=" window) "^"))) (concat target-session ":" target-window))) +(defun ob-tmux--default-session-p (session) + "Whether SESSION is a default session." + (let ((default-session (assoc-default :session + org-babel-default-header-args:tmux))) + (string-suffix-p default-session (ob-tmux--session session)))) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Process execution functions @@ -227,6 +240,13 @@ Argument OB-SESSION: the current ob-tmux session." "-s" (ob-tmux--session ob-session) "-n" (ob-tmux--window-default ob-session)))) +(defun ob-tmux--kill-session (ob-session) + "Kill the tmux session OB-SESSION." + (when (ob-tmux--session-alive-p ob-session) + (ob-tmux--execute ob-session + "kill-session" + "-t" (ob-tmux--target ob-session)))) + (defun ob-tmux--create-window (ob-session) "Create a tmux window in session if it does not yet exist.