From 6c7d39bfb9be38b54d23fcffbb09f1fcb96751f4 Mon Sep 17 00:00:00 2001 From: Nicholas Savage Date: Thu, 6 May 2021 19:17:33 -0400 Subject: [PATCH] ob-shell.el: Fix bug where shell output was incorrectly truncated on special characters. * lisp/ob-comint.el (org-babel-comint-with-output): Add fifth meta optional argument for providing a custom prompt regexp. lisp/ob-shell.el (org-babel-sh-evaluate): Implements using new argument to prevent incorrect truncation on special characters. If shell output included special characters that also occasionally are included in shell prompts, such as "#" or "%", a regexp was tripping up on them and cutting out part of the line. As ob-shell already cuts out the shell prompts, this was not necessary and instead we can just use \n as the separator. Original functionality was retained for other ob-* files in case this was necessary. --- lisp/ob-comint.el | 20 ++++++++++++-------- lisp/ob-shell.el | 2 +- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/lisp/ob-comint.el b/lisp/ob-comint.el index 18d4f3c93..27ad6efd7 100644 --- a/lisp/ob-comint.el +++ b/lisp/ob-comint.el @@ -57,13 +57,14 @@ executed inside the protection of `save-excursion' and (defmacro org-babel-comint-with-output (meta &rest body) "Evaluate BODY in BUFFER and return process output. -Will wait until EOE-INDICATOR appears in the output, then return -all process output. If REMOVE-ECHO and FULL-BODY are present and -non-nil, then strip echo'd body from the returned output. META -should be a list containing the following where the last two -elements are optional. +Will wait until EOE-INDICATOR appears in the output, then return all +process output. If REMOVE-ECHO and FULL-BODY are present and non-nil, +then strip echo'd body from the returned output. PROMPT-REGEXP is a +filter that, if provided, overrides the default regexp that tries to +filter out the shell prompt. META should be a list containing the +following where the last three elements are optional. - (BUFFER EOE-INDICATOR REMOVE-ECHO FULL-BODY) + (BUFFER EOE-INDICATOR REMOVE-ECHO FULL-BODY PROMPT-REGEXP) This macro ensures that the filter is removed in case of an error or user `keyboard-quit' during execution of body." @@ -71,7 +72,10 @@ or user `keyboard-quit' during execution of body." (let ((buffer (nth 0 meta)) (eoe-indicator (nth 1 meta)) (remove-echo (nth 2 meta)) - (full-body (nth 3 meta))) + (full-body (nth 3 meta)) + (prompt-regexp (if (nth 4 meta) + (nth 4 meta) + comint-prompt-regexp))) `(org-babel-comint-in-buffer ,buffer (let* ((string-buffer "") (comint-output-filter-functions @@ -111,7 +115,7 @@ or user `keyboard-quit' during execution of body." "\n" "[\r\n]+" (regexp-quote (or ,full-body ""))) string-buffer)) (setq string-buffer (substring string-buffer (match-end 0)))) - (split-string string-buffer comint-prompt-regexp))))) + (split-string string-buffer ,prompt-regexp))))) (def-edebug-spec org-babel-comint-with-output (sexp body)) (defun org-babel-comint-input-command (buffer cmd) diff --git a/lisp/ob-shell.el b/lisp/ob-shell.el index 3eed0c164..9ec0425cb 100644 --- a/lisp/ob-shell.el +++ b/lisp/ob-shell.el @@ -263,7 +263,7 @@ return the value of the last statement in BODY." #'org-trim (butlast (org-babel-comint-with-output - (session org-babel-sh-eoe-output t body) + (session org-babel-sh-eoe-output t body "\n") (dolist (line (append (split-string (org-trim body) "\n") (list org-babel-sh-eoe-indicator))) (insert line) -- 2.20.1