branch: externals/org
commit 76c5f2238fcd33c35d0a2c71e489c205067ec42c
Author: Ihor Radchenko <[email protected]>
Commit: Ihor Radchenko <[email protected]>

    ob-shell: Arrange unique prompt in manually started shell buffers
    
    * lisp/ob-shell.el (org-babel-sh--prompt-initialized): New variable
    flag that indicates whether ob-shell changed the prompt in current
    comint buffer.
    (org-babel-sh-initiate-session): Set unique prompt in existing
    sessions if it is not yet done.
    
    Link: https://orgmode.org/list/87o782gx7o.fsf@localhost
---
 lisp/ob-shell.el | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/lisp/ob-shell.el b/lisp/ob-shell.el
index 9222379a3f..7b0d6ddab0 100644
--- a/lisp/ob-shell.el
+++ b/lisp/ob-shell.el
@@ -276,16 +276,25 @@ var of the same value."
 (defvar org-babel-sh-prompt "org_babel_sh_prompt> "
   "String to set prompt in session shell.")
 
+(defvar-local org-babel-sh--prompt-initialized nil
+  "When non-nil, ob-shell already initialized the prompt in current buffer.")
+
 (defalias 'org-babel-shell-initiate-session #'org-babel-sh-initiate-session)
 (defun org-babel-sh-initiate-session (&optional session _params)
   "Initiate a session named SESSION according to PARAMS."
   (when (and session (not (string= session "none")))
     (save-window-excursion
-      (or (org-babel-comint-buffer-livep session)
+      (or (and (org-babel-comint-buffer-livep session)
+               (buffer-local-value
+                'org-babel-sh--prompt-initialized
+                (get-buffer session))
+               session)
           (progn
-           (shell session)
-            ;; Set unique prompt for easier analysis of the output.
-            (org-babel-comint-wait-for-output (current-buffer))
+            (if (org-babel-comint-buffer-livep session)
+                (set-buffer session)
+             (shell session)
+              ;; Set unique prompt for easier analysis of the output.
+              (org-babel-comint-wait-for-output (current-buffer)))
             (org-babel-comint-input-command
              (current-buffer)
              (format
@@ -298,6 +307,7 @@ var of the same value."
              comint-prompt-regexp
              (concat "^" (regexp-quote org-babel-sh-prompt)
                      " *"))
+            (setq org-babel-sh--prompt-initialized t)
            ;; Needed for Emacs 23 since the marker is initially
            ;; undefined and the filter functions try to use it without
            ;; checking.

Reply via email to