Explorar o código

ob-shell.el: New option `org-babel-shell-results-defaults-to-output'

* lisp/ob-shell.el (org-babel-shell-results-defaults-to-output):
New option.

* testing/lisp/test-ob-exp.el (ob-exp/exports-inline): Fix test.

See <https://orgmode.org/list/CA+A2iZaziAfMeGpBqL6qGrzrWEVvLvC0DUw++T4gCF3NGuW-DQ@mail.gmail.com>
Bastien %!s(int64=5) %!d(string=hai) anos
pai
achega
07c6b11258
Modificáronse 3 ficheiros con 47 adicións e 9 borrados
  1. 20 1
      etc/ORG-NEWS
  2. 23 4
      lisp/ob-shell.el
  3. 4 4
      testing/lisp/test-ob-exp.el

+ 20 - 1
etc/ORG-NEWS

@@ -359,6 +359,25 @@ You can turn this on/off on a per-file basis with =#+startup: num= or
 This new option allows you to convert a LaTeX fragment directly into
 This new option allows you to convert a LaTeX fragment directly into
 HTML.
 HTML.
 
 
+*** New option ~org-babel-shell-results-defaults-to-output~
+
+By default, source code blocks are executed in "functional mode": it
+means that the results of executing them are the value of their last
+statement (see [[https://orgmode.org/manual/Results-of-Evaluation.html][the documentation]].)
+
+The value of a shell script's execution is its exit code.  But most
+users expect the results of executing a shell script to be its output,
+not its exit code.
+
+So we introduced this option, that you can set to =nil= if you want
+to stick using ~:results value~ as the implicit header.
+
+In all Babel libraries, the absence of a ~:results~ header should
+produce the same result than setting ~:results value~, unless there is
+an option to explicitly create an exception.
+
+See [[https://orgmode.org/list/CA+A2iZaziAfMeGpBqL6qGrzrWEVvLvC0DUw++T4gCF3NGuW-DQ@mail.gmail.com/][this thread]] for more context.
+
 *** New hook ~org-agenda-filter-hook~
 *** New hook ~org-agenda-filter-hook~
 
 
 Functions in this hook are run after ~org-agenda-filter~ is called.
 Functions in this hook are run after ~org-agenda-filter~ is called.
@@ -5669,7 +5688,7 @@ that Calc formulas can operate on them.
 
 
      Thanks to Nicolas Goaziou for coding these changes.
      Thanks to Nicolas Goaziou for coding these changes.
 
 
-**** A property value of "nil" now means to unset a property
+**** A property value of =nil= now means to unset a property
 
 
      This can be useful in particular with property inheritance, if
      This can be useful in particular with property inheritance, if
      some upper level has the property, and some grandchild of it
      some upper level has the property, and some grandchild of it

+ 23 - 4
lisp/ob-shell.el

@@ -71,6 +71,19 @@ outside the Customize interface."
 	 (set-default symbol value)
 	 (set-default symbol value)
 	 (org-babel-shell-initialize)))
 	 (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)
 (defun org-babel-execute:shell (body params)
   "Execute a block of Shell commands with Babel.
   "Execute a block of Shell commands with Babel.
 This function is called by `org-babel-execute-src-block'."
 This function is called by `org-babel-execute-src-block'."
@@ -80,8 +93,11 @@ This function is called by `org-babel-execute-src-block'."
                   (when stdin (org-babel-sh-var-to-string
                   (when stdin (org-babel-sh-var-to-string
                                (org-babel-ref-resolve stdin)))))
                                (org-babel-ref-resolve stdin)))))
 	 (results-params (cdr (assq :result-params params)))
 	 (results-params (cdr (assq :result-params params)))
-	 (value-is-exit-status (or (equal '("replace") results-params)
-				   (member "value" results-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)))
 	 (cmdline (cdr (assq :cmdline params)))
          (full-body (concat
          (full-body (concat
 		     (org-babel-expand-body:generic
 		     (org-babel-expand-body:generic
@@ -215,8 +231,11 @@ of the statements in BODY, if RESULT-TYPE equals `value' then
 return the value of the last statement in BODY."
 return the value of the last statement in BODY."
   (let* ((shebang (cdr (assq :shebang params)))
   (let* ((shebang (cdr (assq :shebang params)))
 	 (results-params (cdr (assq :result-params params)))
 	 (results-params (cdr (assq :result-params params)))
-	 (value-is-exit-status (or (equal '("replace") results-params)
-				   (member "value" results-params)))
+	 (value-is-exit-status
+	  (or (and
+	       (equal '("replace") results-params)
+	       (not org-babel-shell-results-defaults-to-output))
+	      (member "value" results-params)))
 	 (results
 	 (results
 	  (cond
 	  (cond
 	   ((or stdin cmdline)	       ; external shell script w/STDIN
 	   ((or stdin cmdline)	       ; external shell script w/STDIN

+ 4 - 4
testing/lisp/test-ob-exp.el

@@ -190,9 +190,9 @@ a table."
 (ert-deftest ob-exp/exports-inline ()
 (ert-deftest ob-exp/exports-inline ()
   (should
   (should
    (string-match
    (string-match
-    (regexp-quote "Here is one in the middle {{{results(=0=)}}} of a line.
-Here is one at the end of a line. {{{results(=0=)}}}
-{{{results(=0=)}}} Here is one at the beginning of a line.")
+    (regexp-quote "Here is one in the middle {{{results(=1=)}}} of a line.
+Here is one at the end of a line. {{{results(=2=)}}}
+{{{results(=3=)}}} Here is one at the beginning of a line.")
     (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
     (org-test-at-id "54cb8dc3-298c-4883-a933-029b3c9d4b18"
       (org-narrow-to-subtree)
       (org-narrow-to-subtree)
       (let ((org-babel-inline-result-wrap "=%s="))
       (let ((org-babel-inline-result-wrap "=%s="))
@@ -252,7 +252,7 @@ Here is one at the end of a line. {{{results(=0=)}}}
      (regexp-quote "Here is one in the middle src_sh[]{echo 1} of a line.
      (regexp-quote "Here is one in the middle src_sh[]{echo 1} of a line.
 Here is one at the end of a line. src_sh[]{echo 2}
 Here is one at the end of a line. src_sh[]{echo 2}
 src_sh[]{echo 3} Here is one at the beginning of a line.
 src_sh[]{echo 3} Here is one at the beginning of a line.
-Here is one that is also evaluated: src_sh[]{echo 4} {{{results(=0=)}}}")
+Here is one that is also evaluated: src_sh[]{echo 4} {{{results(=4=)}}}")
      nil t)
      nil t)
     (org-test-at-id "cd54fc88-1b6b-45b6-8511-4d8fa7fc8076"
     (org-test-at-id "cd54fc88-1b6b-45b6-8511-4d8fa7fc8076"
       (org-narrow-to-subtree)
       (org-narrow-to-subtree)