Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default-directory' to body' in `org-babel-execute:tmux' #19

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 37 additions & 19 deletions ob-tmux.el
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand All @@ -102,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
Expand Down Expand Up @@ -167,23 +175,29 @@ 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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(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.
Expand All @@ -202,16 +216,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
Expand All @@ -229,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.

Expand Down