From 2501d949970104c31201b747584c2a32c65fea65 Mon Sep 17 00:00:00 2001 From: Eugene Kim Date: Wed, 23 Aug 2023 23:30:23 +0900 Subject: [PATCH] execute line by line or combine them :line-mode line executes a line where a cursor is at :line-mode combine :newline-replace ; executes src block as single command after combining with the ";" --- ob-tmux.el | 50 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/ob-tmux.el b/ob-tmux.el index 4c52124..a92dc1b 100644 --- a/ob-tmux.el +++ b/ob-tmux.el @@ -112,13 +112,57 @@ Argument PARAMS the org parameters of the code block." ;; Disable window renaming from within tmux (ob-tmux--disable-renaming ob-session) (ob-tmux--send-body - ob-session (org-babel-expand-body:generic body params vars)) + ob-session (org-babel-expand-body:tmux body params vars)) ;; Warn that setting the terminal from the org source block ;; header arguments is going to be deprecated. (message "ob-tmux terminal: %s" org-header-terminal) (when org-header-terminal (ob-tmux--deprecation-warning org-header-terminal))))) + +(defun org-babel-expand-body:tmux (body params &optional var-lines) + + (let* ((line-mode (cdr (assq :line-mode params))) + (combiner (cdr (assq :combiner params))) + (new-body body)) + + (if combiner + (setq combiner (concat combiner " ")) + (setq combiner "\n") + ) + + (if line-mode + (let* ((context (org-element-context (org-element-at-point))) + (cur-line (line-number-at-pos)) + (beg (org-element-property :begin context)) + (beg-line (save-excursion + (goto-char beg) + (+ (line-number-at-pos) 1))) + (all-lines (split-string body "\n")) + (cur-idx (- cur-line beg-line)) + (lines ()) + ) + (pcase line-mode + ("current" + (setq lines (list (nth cur-idx all-lines))) + ) + ("below" + (setq lines (cl-subseq all-lines cur-idx (length all-lines))) + ) + ("above" + (setq lines (cl-subseq all-lines 0 (+ cur-idx 1))) + ) + (_ + (setq lines all-lines) + ) + ) + (setq new-body (string-join lines combiner))) + (setq new-body (string-replace "\n" combiner (string-trim body)))) + (org-babel-expand-body:generic new-body params var-lines) + ) + ) + + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ob-tmux object ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -388,7 +432,3 @@ Argument PATH: the location of the file." "DOESN'T work."))))) (provide 'ob-tmux) - - - -;;; ob-tmux.el ends here