diff options
Diffstat (limited to 'lisp/org/ob-shell.el')
-rw-r--r-- | lisp/org/ob-shell.el | 59 |
1 files changed, 43 insertions, 16 deletions
diff --git a/lisp/org/ob-shell.el b/lisp/org/ob-shell.el index 1383f42f259..c08ba50fe7e 100644 --- a/lisp/org/ob-shell.el +++ b/lisp/org/ob-shell.el @@ -71,6 +71,19 @@ outside the Customize interface." (set-default symbol value) (org-babel-shell-initialize))) +(defcustom org-babel-shell-results-defaults-to-output t + "Let shell execution defaults to \":results output\". + +When set to t, use \":results output\" when no :results setting +is set. This is especially useful for inline source blocks. + +When set to nil, stick to the convention of using :results value +as the default setting when no :results is set, the \"value\" of +a shell execution being its exit code." + :group 'org-babel + :type 'boolean + :package-version '(Org . "9.4")) + (defun org-babel-execute:shell (body params) "Execute a block of Shell commands with Babel. This function is called by `org-babel-execute-src-block'." @@ -79,9 +92,17 @@ This function is called by `org-babel-execute-src-block'." (stdin (let ((stdin (cdr (assq :stdin params)))) (when stdin (org-babel-sh-var-to-string (org-babel-ref-resolve stdin))))) + (results-params (cdr (assq :result-params params))) + (value-is-exit-status + (or (and + (equal '("replace") results-params) + (not org-babel-shell-results-defaults-to-output)) + (member "value" results-params))) (cmdline (cdr (assq :cmdline params))) - (full-body (org-babel-expand-body:generic - body params (org-babel-variable-assignments:shell params)))) + (full-body (concat + (org-babel-expand-body:generic + body params (org-babel-variable-assignments:shell params)) + (when value-is-exit-status "\necho $?")))) (org-babel-reassemble-table (org-babel-sh-evaluate session full-body params stdin cmdline) (org-babel-pick-name @@ -96,7 +117,8 @@ This function is called by `org-babel-execute-src-block'." (org-babel-comint-in-buffer session (mapc (lambda (var) (insert var) (comint-send-input nil t) - (org-babel-comint-wait-for-output session)) var-lines)) + (org-babel-comint-wait-for-output session)) + var-lines)) session)) (defun org-babel-load-session:shell (session body params) @@ -129,15 +151,15 @@ This function is called by `org-babel-execute-src-block'." (varname values &optional sep hline) "Return a list of statements declaring the values as bash associative array." (format "unset %s\ndeclare -A %s\n%s" - varname varname - (mapconcat - (lambda (items) - (format "%s[%s]=%s" - varname - (org-babel-sh-var-to-sh (car items) sep hline) - (org-babel-sh-var-to-sh (cdr items) sep hline))) - values - "\n"))) + varname varname + (mapconcat + (lambda (items) + (format "%s[%s]=%s" + varname + (org-babel-sh-var-to-sh (car items) sep hline) + (org-babel-sh-var-to-sh (cdr items) sep hline))) + values + "\n"))) (defun org-babel--variable-assignments:bash (varname values &optional sep hline) "Represent the parameters as useful Bash shell variables." @@ -208,6 +230,12 @@ If RESULT-TYPE equals `output' then return a list of the outputs of the statements in BODY, if RESULT-TYPE equals `value' then return the value of the last statement in BODY." (let* ((shebang (cdr (assq :shebang params))) + (results-params (cdr (assq :result-params params))) + (value-is-exit-status + (or (and + (equal '("replace") results-params) + (not org-babel-shell-results-defaults-to-output)) + (member "value" results-params))) (results (cond ((or stdin cmdline) ; external shell script w/STDIN @@ -259,8 +287,9 @@ return the value of the last statement in BODY." (insert body)) (set-file-modes script-file #o755) (org-babel-eval script-file ""))) - (t - (org-babel-eval shell-file-name (org-trim body)))))) + (t (org-babel-eval shell-file-name (org-trim body)))))) + (when value-is-exit-status + (setq results (car (reverse (split-string results "\n" t))))) (when results (let ((result-params (cdr (assq :result-params params)))) (org-babel-result-cond result-params @@ -277,6 +306,4 @@ return the value of the last statement in BODY." (provide 'ob-shell) - - ;;; ob-shell.el ends here |