pull/19/merge
York Zhao 2023-09-02 04:41:15 +02:00 committed by GitHub
commit 9ec9a075c8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 37 additions and 19 deletions

View File

@ -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 BODY the body of the tmux code block.
Argument PARAMS the org parameters of the code block." Argument PARAMS the org parameters of the code block."
(message "Sending source code block to interactive terminal session...") (message "Sending source code block to interactive terminal session...")
(setq body (format "cd %s\n%s" default-directory body))
(save-window-excursion (save-window-excursion
(let* ((org-session (cdr (assq :session params))) (let* ((org-session (cdr (assq :session params)))
(org-header-terminal (cdr (assq :terminal params))) (org-header-terminal (cdr (assq :terminal params)))
@ -102,7 +103,14 @@ Argument PARAMS the org parameters of the code block."
(session-alive (ob-tmux--session-alive-p ob-session)) (session-alive (ob-tmux--session-alive-p ob-session))
(window-alive (ob-tmux--window-alive-p ob-session))) (window-alive (ob-tmux--window-alive-p ob-session)))
;; Create tmux session and window if they do not yet exist ;; 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)) (unless window-alive (ob-tmux--create-window ob-session))
;; Start terminal window if the session does not yet exist ;; Start terminal window if the session does not yet exist
(unless session-alive (unless session-alive
@ -167,23 +175,29 @@ Argument OB-SESSION: the current ob-tmux session."
(target-window (if window (concat "=" window) "^"))) (target-window (if window (concat "=" window) "^")))
(concat target-session ":" target-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 ;; Process execution functions
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun ob-tmux--execute (ob-session &rest args) (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." Optional command-line arguments can be passed in ARGS."
(if (ob-tmux--socket ob-session) (let ((socket (ob-tmux--socket ob-session)))
(apply 'start-process "ob-tmux" "*Messages*" (unless (zerop (apply #'call-process org-babel-tmux-location
org-babel-tmux-location nil nil nil
"-S" (ob-tmux--socket ob-session) (if socket
args) (append `("-S" ,socket) args)
(apply 'start-process args)))
"ob-tmux" "*Messages*" org-babel-tmux-location args))) (error "Error executing tmux command"))))
(defun ob-tmux--execute-string (ob-session &rest args) (defun ob-tmux--execute-string (ob-session &rest args)
"Execute a tmux command with arguments as given. "Execute a tmux command with arguments as given.
@ -202,16 +216,13 @@ automatically space separated."
"Start a TERMINAL window with tmux attached to session. "Start a TERMINAL window with tmux attached to session.
Argument OB-SESSION: the current ob-tmux session." Argument OB-SESSION: the current ob-tmux session."
(let ((start-process-mandatory-args `("org-babel: terminal" (let ((tmux-cmd `(,org-babel-tmux-location
"*Messages*" "attach-session" "-t" ,(ob-tmux--target ob-session))))
,terminal))
(tmux-cmd `(,org-babel-tmux-location
"attach-session"
"-t" ,(ob-tmux--target ob-session))))
(unless (ob-tmux--socket ob-session) (unless (ob-tmux--socket ob-session)
(apply 'start-process (append start-process-mandatory-args (unless (zerop (apply #'call-process terminal nil nil nil
org-babel-tmux-terminal-opts (append org-babel-tmux-terminal-opts
tmux-cmd))))) tmux-cmd)))
(error "Error executing tmux command")))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Tmux interaction ;; Tmux interaction
@ -229,6 +240,13 @@ Argument OB-SESSION: the current ob-tmux session."
"-s" (ob-tmux--session ob-session) "-s" (ob-tmux--session ob-session)
"-n" (ob-tmux--window-default 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) (defun ob-tmux--create-window (ob-session)
"Create a tmux window in session if it does not yet exist. "Create a tmux window in session if it does not yet exist.