Browse Source

org-babel-result-cond - unified handling of results

* lisp/ob.el (org-babel-result-cond): This function should now be used
  by all language backends to handle the processing of raw code block
  output into scalar results, table results, or ignored results
  depending on the value of result-params.

* lisp/ob-C.el (org-babel-C-execute): Use org-babel-result-cond.
* lisp/ob-R.el (org-babel-R-evaluate-external-process): Use
  org-babel-result-cond.
  (org-babel-R-evaluate-session): Use org-babel-result-cond.
* lisp/ob-awk.el (org-babel-execute:awk): Use org-babel-result-cond.
* lisp/ob-clojure.el (org-babel-execute:clojure): Use
  org-babel-result-cond.
* lisp/ob-emacs-lisp.el (org-babel-execute:emacs-lisp): Use
  org-babel-result-cond.
* lisp/ob-fortran.el (org-babel-execute:fortran): Use
  org-babel-result-cond.
* lisp/ob-io.el (org-babel-io-evaluate): Use org-babel-result-cond.
* lisp/ob-java.el (org-babel-execute:java): Use org-babel-result-cond.
* lisp/ob-lisp.el (org-babel-execute:lisp): Use org-babel-result-cond.
* lisp/ob-maxima.el (org-babel-execute:maxima): Use
  org-babel-result-cond.
* lisp/ob-picolisp.el (org-babel-execute:picolisp): Use
  org-babel-result-cond.
* lisp/ob-python.el (org-babel-python-evaluate-external-process): Use
  org-babel-result-cond.
  (org-babel-python-evaluate-session): Use org-babel-result-cond.
* lisp/ob-scala.el (org-babel-scala-evaluate): Use org-babel-result-cond.
* lisp/ob-sh.el (org-babel-sh-evaluate): Use org-babel-result-cond.
* lisp/ob-shen.el (org-babel-execute:shen): Use org-babel-result-cond.
* lisp/ob-sql.el (org-babel-execute:sql): Use org-babel-result-cond.
* lisp/ob-sqlite.el (org-babel-execute:sqlite): Use
  org-babel-result-cond.
Eric Schulte 6 years ago
parent
commit
78cdf14939
18 changed files with 79 additions and 93 deletions
  1. 5 5
      lisp/ob-C.el
  2. 8 10
      lisp/ob-R.el
  3. 2 4
      lisp/ob-awk.el
  4. 2 3
      lisp/ob-clojure.el
  5. 4 5
      lisp/ob-emacs-lisp.el
  6. 5 5
      lisp/ob-fortran.el
  7. 2 2
      lisp/ob-io.el
  8. 4 4
      lisp/ob-java.el
  9. 2 2
      lisp/ob-lisp.el
  10. 2 4
      lisp/ob-maxima.el
  11. 2 7
      lisp/ob-picolisp.el
  12. 4 10
      lisp/ob-python.el
  13. 2 2
      lisp/ob-scala.el
  14. 2 4
      lisp/ob-sh.el
  15. 2 3
      lisp/ob-shen.el
  16. 2 6
      lisp/ob-sql.el
  17. 15 17
      lisp/ob-sqlite.el
  18. 14 0
      lisp/ob.el

+ 5 - 5
lisp/ob-C.el

@@ -106,11 +106,11 @@ or `org-babel-execute:C++'."
 		     (org-babel-process-file-name tmp-src-file)) ""))))
     ((lambda (results)
        (org-babel-reassemble-table
-	(if (member "vector" (cdr (assoc :result-params params)))
-	    (let ((tmp-file (org-babel-temp-file "c-")))
-	      (with-temp-file tmp-file (insert results))
-	      (org-babel-import-elisp-from-file tmp-file))
-	  (org-babel-read results))
+	(org-babel-result-cond (cdr (assoc :result-params params))
+	  (org-babel-read results)
+	  (let ((tmp-file (org-babel-temp-file "c-")))
+	    (with-temp-file tmp-file (insert results))
+	    (org-babel-import-elisp-from-file tmp-file)))
 	(org-babel-pick-name
 	 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
 	(org-babel-pick-name

+ 8 - 10
lisp/ob-R.el

@@ -302,11 +302,10 @@ last statement in BODY, as elisp."
 			       (format "{function ()\n{\n%s\n}}()" body)
 			       (org-babel-process-file-name tmp-file 'noquote)))
        (org-babel-R-process-value-result
-	(if (or (member "scalar" result-params)
-		(member "verbatim" result-params))
-	    (with-temp-buffer
-	      (insert-file-contents tmp-file)
-	      (buffer-string))
+	(org-babel-result-cond result-params
+	  (with-temp-buffer
+	    (insert-file-contents tmp-file)
+	    (buffer-string))
 	  (org-babel-import-elisp-from-file tmp-file '(16)))
 	column-names-p)))
     (output (org-babel-eval org-babel-R-command body))))
@@ -335,11 +334,10 @@ last statement in BODY, as elisp."
 		  "FALSE")
 		".Last.value" (org-babel-process-file-name tmp-file 'noquote)))
        (org-babel-R-process-value-result
-	(if (or (member "scalar" result-params)
-		(member "verbatim" result-params))
-	    (with-temp-buffer
-	      (insert-file-contents tmp-file)
-	      (buffer-string))
+	(org-babel-result-cond result-params
+	  (with-temp-buffer
+	    (insert-file-contents tmp-file)
+	    (buffer-string))
 	  (org-babel-import-elisp-from-file tmp-file '(16)))
 	column-names-p)))
     (output

+ 2 - 4
lisp/ob-awk.el

@@ -78,10 +78,8 @@ called by `org-babel-execute-src-block'"
     (org-babel-reassemble-table
      ((lambda (results)
 	(when results
-	  (if (or (member "scalar" result-params)
-		  (member "verbatim" result-params)
-		  (member "output" result-params))
-	      results
+	  (org-babel-result-cond result-params
+	    results
 	    (let ((tmp (org-babel-temp-file "awk-results-")))
 	      (with-temp-file tmp (insert results))
 	      (org-babel-import-elisp-from-file tmp)))))

+ 2 - 3
lisp/ob-clojure.el

@@ -79,9 +79,8 @@
     (insert (org-babel-expand-body:clojure body params))
     ((lambda (result)
        (let ((result-params (cdr (assoc :result-params params))))
-	 (if (or (member "scalar" result-params)
-		 (member "verbatim" result-params))
-	     result
+	 (org-babel-result-cond result-params
+	   result
 	   (condition-case nil (org-babel-script-escape result)
 	     (error result)))))
      (slime-eval

+ 4 - 5
lisp/ob-emacs-lisp.el

@@ -56,11 +56,10 @@
   "Execute a block of emacs-lisp code with Babel."
   (save-window-excursion
     ((lambda (result)
-       (if (or (member "scalar" (cdr (assoc :result-params params)))
-	       (member "verbatim" (cdr (assoc :result-params params))))
-	   (let ((print-level nil)
-		 (print-length nil))
-	     (format "%S" result))
+       (org-babel-result-cond (cdr (assoc :result-params params))
+	 (let ((print-level nil)
+	       (print-length nil))
+	   (format "%S" result))
 	 (org-babel-reassemble-table
 	  result
 	  (org-babel-pick-name (cdr (assoc :colname-names params))

+ 5 - 5
lisp/ob-fortran.el

@@ -62,11 +62,11 @@
 		     (org-babel-process-file-name tmp-src-file)) ""))))
     ((lambda (results)
        (org-babel-reassemble-table
-	(if (member "vector" (cdr (assoc :result-params params)))
-	    (let ((tmp-file (org-babel-temp-file "f-")))
-	      (with-temp-file tmp-file (insert results))
-	      (org-babel-import-elisp-from-file tmp-file))
-	  (org-babel-read results))
+	(org-babel-result-cond (cdr (assoc :result-params params))
+	  (org-babel-read results)
+	  (let ((tmp-file (org-babel-temp-file "f-")))
+	    (with-temp-file tmp-file (insert results))
+	    (org-babel-import-elisp-from-file tmp-file)))
 	(org-babel-pick-name
 	 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
 	(org-babel-pick-name

+ 2 - 2
lisp/ob-io.el

@@ -98,8 +98,8 @@ in BODY as elisp."
                   (wrapper (format org-babel-io-wrapper-method body)))
              (with-temp-file src-file (insert wrapper))
              ((lambda (raw)
-                (if (member "code" result-params)
-                    raw
+                (org-babel-result-cond result-params
+		  raw
                   (org-babel-io-table-or-string raw)))
               (org-babel-eval
                (concat org-babel-io-command " " src-file) ""))))))

+ 4 - 4
lisp/ob-java.el

@@ -58,11 +58,11 @@
       (make-directory packagename 'parents))
     ((lambda (results)
        (org-babel-reassemble-table
-	(if (member "vector" (cdr (assoc :result-params params)))
-	    (let ((tmp-file (org-babel-temp-file "c-")))
+	(org-babel-result-cond (cdr (assoc :result-params params))
+	  (org-babel-read results)
+	  (let ((tmp-file (org-babel-temp-file "c-")))
 	      (with-temp-file tmp-file (insert results))
-	      (org-babel-import-elisp-from-file tmp-file))
-	  (org-babel-read results))
+	      (org-babel-import-elisp-from-file tmp-file)))
 	(org-babel-pick-name
 	 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
 	(org-babel-pick-name

+ 2 - 2
lisp/ob-lisp.el

@@ -76,8 +76,8 @@ current directory string."
   (require 'slime)
   (org-babel-reassemble-table
    ((lambda (result)
-      (if (member "output" (cdr (assoc :result-params params)))
-	  (car result)
+      (org-babel-result-cond (cdr (assoc :result-params params))
+	(car result)
 	(condition-case nil
 	    (read (org-babel-lisp-vector-to-list (cadr result)))
 	  (error (cadr result)))))

+ 2 - 4
lisp/ob-maxima.el

@@ -89,10 +89,8 @@ called by `org-babel-execute-src-block'."
 	    (org-babel-eval cmd "")))))
     (if (org-babel-maxima-graphical-output-file params)
 	nil
-      (if (or (member "scalar" result-params)
-	      (member "verbatim" result-params)
-	      (member "output" result-params))
-	  result
+      (org-babel-result-cond result-params
+	result
 	(let ((tmp-file (org-babel-temp-file "maxima-res-")))
 	  (with-temp-file tmp-file (insert result))
 	  (org-babel-import-elisp-from-file tmp-file))))))

+ 2 - 7
lisp/ob-picolisp.el

@@ -123,13 +123,8 @@
            (t full-body))))
 
     ((lambda (result)
-       (if (or (member "verbatim" result-params)
-               (member "scalar" result-params)
-               (member "output" result-params)
-               (member "code" result-params)
-               (member "pp" result-params)
-               (= (length result) 0))
-           result
+       (org-babel-result-cond result-params
+	 result
          (read result)))
      (if (not (string= session-name "none"))
          ;; session based evaluation

+ 4 - 10
lisp/ob-python.el

@@ -216,11 +216,8 @@ If RESULT-TYPE equals 'output then return standard output as a
 string.  If RESULT-TYPE equals 'value then return the value of the
 last statement in BODY, as elisp."
   ((lambda (raw)
-     (if (or (member "code" result-params)
-	     (member "pp" result-params)
-	     (and (member "output" result-params)
-		  (not (member "table" result-params))))
-	 raw
+     (org-babel-result-cond result-params
+       raw
        (org-babel-python-table-or-string (org-babel-trim raw))))
    (case result-type
      (output (org-babel-eval org-babel-python-command
@@ -269,11 +266,8 @@ last statement in BODY, as elisp."
 		       (funcall send-wait))))
     ((lambda (results)
        (unless (string= (substring org-babel-python-eoe-indicator 1 -1) results)
-	 (if (or (member "code" result-params)
-		 (member "pp" result-params)
-		 (and (member "output" result-params)
-		      (not (member "table" result-params))))
-	     results
+	 (org-babel-result-cond result-params
+	   results
 	   (org-babel-python-table-or-string results))))
      (case result-type
        (output

+ 2 - 2
lisp/ob-scala.el

@@ -104,8 +104,8 @@ in BODY as elisp."
             (wrapper (format org-babel-scala-wrapper-method body)))
        (with-temp-file src-file (insert wrapper))
        ((lambda (raw)
-          (if (member "code" result-params)
-              raw
+          (org-babel-result-cond result-params
+	    raw
             (org-babel-scala-table-or-string raw)))
         (org-babel-eval
          (concat org-babel-scala-command " " src-file) ""))))))

+ 2 - 4
lisp/ob-sh.el

@@ -141,10 +141,8 @@ return the value of the last statement in BODY."
   ((lambda (results)
      (when results
        (let ((result-params (cdr (assoc :result-params params))))
-	 (if (or (member "scalar" result-params)
-		 (member "verbatim" result-params)
-		 (member "output" result-params))
-	     results
+	 (org-babel-result-cond result-params
+	   results
 	   (let ((tmp-file (org-babel-temp-file "sh-")))
 	     (with-temp-file tmp-file (insert results))
 	     (org-babel-import-elisp-from-file tmp-file))))))

+ 2 - 3
lisp/ob-shen.el

@@ -66,9 +66,8 @@ This function is called by `org-babel-execute-src-block'"
 	 (result-params (cdr (assoc :result-params params)))
          (full-body (org-babel-expand-body:shen body params)))
     ((lambda (results)
-       (if (or (member 'scalar result-params)
-	       (member 'verbatim result-params))
-	   results
+       (org-babel-result-cond result-params 
+	 results
 	 (condition-case nil (org-babel-script-escape results)
 	   (error results))))
      (with-temp-buffer

+ 2 - 6
lisp/ob-sql.el

@@ -103,12 +103,8 @@ This function is called by `org-babel-execute-src-block'."
        (org-babel-expand-body:sql body params)))
     (message command)
     (shell-command command)
-    (if (or (member "scalar" result-params)
-	    (member "verbatim" result-params)
-	    (member "html" result-params)
-	    (member "code" result-params)
-	    (equal (point-min) (point-max)))
-	(with-temp-buffer
+    (org-babel-result-cond result-params
+      (with-temp-buffer
 	  (progn (insert-file-contents-literally out-file) (buffer-string)))
       (with-temp-buffer
 	;; need to figure out what the delimiter is for the header row

+ 15 - 17
lisp/ob-sqlite.el

@@ -98,23 +98,21 @@ This function is called by `org-babel-execute-src-block'."
 	  (cons "db " db)))
 	;; body of the code block
 	(org-babel-expand-body:sqlite body params)))
-      (if (or (member "scalar" result-params)
-	      (member "verbatim" result-params)
-	      (member "html" result-params)
-	      (member "code" result-params)
-	      (equal (point-min) (point-max)))
-	  (buffer-string)
-	(org-table-convert-region (point-min) (point-max)
-				  (if (or (member :csv others)
-					  (member :column others)
-					  (member :line others)
-					  (member :list others)
-					  (member :html others) separator)
-				      nil
-				    '(4)))
-	(org-babel-sqlite-table-or-scalar
-	 (org-babel-sqlite-offset-colnames
-	  (org-table-to-lisp) headers-p))))))
+      (org-babel-result-cond result-params
+	(buffer-string)
+	(if (equal (point-min) (point-max))
+	    ""
+	  (org-table-convert-region (point-min) (point-max)
+				    (if (or (member :csv others)
+					    (member :column others)
+					    (member :line others)
+					    (member :list others)
+					    (member :html others) separator)
+					nil
+				      '(4)))
+	  (org-babel-sqlite-table-or-scalar
+	   (org-babel-sqlite-offset-colnames
+	    (org-table-to-lisp) headers-p)))))))
 
 (defun org-babel-sqlite-expand-vars (body vars)
   "Expand the variables held in VARS in BODY."

+ 14 - 0
lisp/ob.el

@@ -2561,6 +2561,20 @@ additionally processed by `shell-quote-argument'"
 Used by `org-babel-temp-file'.  This directory will be removed on
 Emacs shutdown."))
 
+(defmacro org-babel-result-cond (result-params scalar-form &rest table-forms)
+  "Call the code to parse raw string results according to RESULT-PARAMS."
+  (declare (indent 1))
+  `(unless (member "none" result-params)
+     (if (or (member "scalar" result-params)
+	     (member "verbatim" result-params)
+	     (member "html" result-params)
+	     (member "code" result-params)
+	     (member "pp" result-params)
+	     (and (member "output" result-params)
+		  (not (member "table" result-params))))
+	 ,scalar-form
+       ,@table-forms)))
+
 (defun org-babel-temp-file (prefix &optional suffix)
   "Create a temporary file in the `org-babel-temporary-directory'.
 Passes PREFIX and SUFFIX directly to `make-temp-file' with the