Skip to content

Commit a35d163

Browse files
committed
ob-shell: Fix output containing strings matching `comint-prompt-regexp'
* lisp/ob-comint.el (org-babel-comint-wait-for-output): Do not rely on `face-at-point' returning non-nil. * lisp/ob-shell.el (org-babel-shell-set-prompt-commands): New constant holding shell-specific commands to change prompt. (org-babel-prompt-command): New variable holding command to be user to set distinguishable prompt. (org-babel-shell-initialize): Set `org-babel-prompt-command' according to shell name. (org-babel-sh-prompt): New variable holding default shell prompt. (org-babel-sh-initiate-session): Change the default prompt to `org-babel-sh-prompt' and alter `comint-prompt-regexp' to match it tightly. Fixes https://list.orgmode.org/CKK9TULBP2BG.2UITT31YJV03J@laptop/T/#mc8e3ca2f5f1b9a94040a68b4c6201234b209041c
1 parent 3c11e9d commit a35d163

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

lisp/ob-comint.el

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,7 @@ statement (not large blocks of code)."
124124
(while (progn
125125
(goto-char comint-last-input-end)
126126
(not (and (re-search-forward comint-prompt-regexp nil t)
127-
(goto-char (match-beginning 0))
128-
(string= (face-name (face-at-point))
129-
"comint-highlight-prompt"))))
127+
(goto-char (match-beginning 0)))))
130128
(accept-process-output (get-buffer-process buffer)))))
131129

132130
(defun org-babel-comint-eval-invisibly-and-wait-for-file

lisp/ob-shell.el

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,23 @@
4242
(defvar org-babel-default-header-args:shell '())
4343
(defvar org-babel-shell-names)
4444

45+
(defconst org-babel-shell-set-prompt-commands
46+
'(("fish" . "function fish_prompt\n\techo \"%s\"\nend")
47+
("csh" . "set prompt=\"%s\"")
48+
("posh" . "function prompt { \"%s\" }")
49+
(t . "PS1=\"%s\""))
50+
"Alist assigning shells with their prompt setting command.
51+
52+
Each element of the alist associates a shell type from
53+
`org-babel-shell-names' with a template used to create a command to
54+
change the default prompt. The template is an argument to `format'
55+
that will be called with a single additional argument: prompt string.
56+
57+
The fallback association template is defined in (t . \"template\")
58+
alist element.")
59+
60+
(defvar org-babel-prompt-command)
61+
4562
(defun org-babel-shell-initialize ()
4663
"Define execution functions associated to shell names.
4764
This function has to be called whenever `org-babel-shell-names'
@@ -51,7 +68,10 @@ is modified outside the Customize interface."
5168
(eval `(defun ,(intern (concat "org-babel-execute:" name))
5269
(body params)
5370
,(format "Execute a block of %s commands with Babel." name)
54-
(let ((shell-file-name ,name))
71+
(let ((shell-file-name ,name)
72+
(org-babel-prompt-command
73+
(or (alist-get ,name org-babel-shell-set-prompt-commands)
74+
(alist-get t org-babel-shell-set-prompt-commands))))
5575
(org-babel-execute:shell body params))))
5676
(eval `(defalias ',(intern (concat "org-babel-variable-assignments:" name))
5777
'org-babel-variable-assignments:shell
@@ -206,24 +226,34 @@ var of the same value."
206226
(mapconcat echo-var var "\n"))
207227
(t (funcall echo-var var)))))
208228

229+
(defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
230+
"String to indicate that evaluation has completed.")
231+
(defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
232+
"String to indicate that evaluation has completed.")
233+
(defvar org-babel-sh-prompt "org_babel_sh_prompt> "
234+
"String to set prompt in session shell.")
235+
209236
(defun org-babel-sh-initiate-session (&optional session _params)
210237
"Initiate a session named SESSION according to PARAMS."
211238
(when (and session (not (string= session "none")))
212239
(save-window-excursion
213240
(or (org-babel-comint-buffer-livep session)
214241
(progn
215242
(shell session)
243+
;; Set unique prompt for easier analysis of the output.
244+
(org-babel-comint-wait-for-output (current-buffer))
245+
(org-babel-comint-input-command
246+
(current-buffer)
247+
(format org-babel-prompt-command org-babel-sh-prompt))
248+
(setq-local comint-prompt-regexp
249+
(concat "^" (regexp-quote org-babel-sh-prompt)
250+
" *"))
216251
;; Needed for Emacs 23 since the marker is initially
217252
;; undefined and the filter functions try to use it without
218253
;; checking.
219254
(set-marker comint-last-output-start (point))
220255
(get-buffer (current-buffer)))))))
221256

222-
(defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
223-
"String to indicate that evaluation has completed.")
224-
(defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
225-
"String to indicate that evaluation has completed.")
226-
227257
(defun org-babel-sh-evaluate (session body &optional params stdin cmdline)
228258
"Pass BODY to the Shell process in BUFFER.
229259
If RESULT-TYPE equals `output' then return a list of the outputs

0 commit comments

Comments
 (0)