瀏覽代碼

Fixing results processing code.

When :results is 'value, the org-babel-LANG-evaluate functions are
responsible for returning an elisp representation of the *value* of
the block. This stage is maintained in the language-specific code
because different languages have different ways of doing it: python
and ruby use org-babel-LANG-table-or-string, whereas R and shell write
to file and then use org-babel-import-elisp-from-file. It could
however be put in the org-babel-execute:LANG function.
Dan Davison 16 年之前
父節點
當前提交
df3b1b7223
共有 5 個文件被更改,包括 30 次插入25 次删除
  1. 6 6
      lisp/langs/org-babel-R.el
  2. 1 1
      lisp/langs/org-babel-python.el
  3. 1 2
      lisp/langs/org-babel-ruby.el
  4. 12 5
      lisp/langs/org-babel-sh.el
  5. 10 11
      lisp/org-babel.el

+ 6 - 6
lisp/langs/org-babel-R.el

@@ -98,7 +98,7 @@ write.table(main(), file=\"%s\", sep=\"\\t\", na=\"nil\",row.names=FALSE, col.na
   "Pass BODY to the R process in BUFFER.  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."
+last statement in BODY, as elisp."
   (if (not session)
       ;; external process evaluation
       (let ((in-tmp-file (make-temp-file "R-in-functional-results"))
@@ -107,12 +107,13 @@ last statement in BODY."
           (output
            (with-temp-file in-tmp-file (insert body))
            (shell-command-to-string (format "R --slave --no-save < '%s' > '%s'"
-					    in-tmp-file out-tmp-file)))
+					    in-tmp-file out-tmp-file))
+	   (with-temp-buffer (insert-file-contents out-tmp-file) (buffer-string)))
           (value
            (with-temp-file in-tmp-file
              (insert (format org-babel-R-wrapper-method body out-tmp-file)))
-           (shell-command (format "R --no-save < '%s'" in-tmp-file))))
-        (with-temp-buffer (insert-file-contents out-tmp-file) (buffer-string)))
+           (shell-command (format "R --no-save < '%s'" in-tmp-file))
+	   (org-babel-import-elisp-from-file out-tmp-file))))
     ;; comint session evaluation
     (org-babel-comint-in-buffer buffer
       (let* ((tmp-file (make-temp-file "org-babel-R"))
@@ -140,8 +141,7 @@ last statement in BODY."
 			 (mapcar #'org-babel-trim raw))))))
         (case result-type
           (output (org-babel-trim (mapconcat #'identity results "\n")))
-          (value (org-babel-trim
-		  (with-temp-buffer (insert-file-contents tmp-file) (buffer-string)))))))))
+          (value (org-babel-import-elisp-from-file tmp-file)))))))
 
 
 (provide 'org-babel-R)

+ 1 - 1
lisp/langs/org-babel-python.el

@@ -126,7 +126,7 @@ open('%s', 'w').write( str(main()) )")
   "Pass BODY to the Python process in BUFFER.  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."
+last statement in BODY, as elisp."
   (if (not session)
       ;; external process evaluation
       (save-window-excursion

+ 1 - 2
lisp/langs/org-babel-ruby.el

@@ -123,7 +123,7 @@ File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.
   "Pass BODY to the Ruby process in BUFFER.  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."
+last statement in BODY, as elisp."
   (if (not session)
       ;; external process evaluation
       (save-window-excursion
@@ -143,7 +143,6 @@ last statement in BODY."
              (org-babel-ruby-table-or-string
 	      (with-temp-buffer (insert-file-contents tmp-file) (buffer-string)))))))
     ;; comint session evaluation
-    (message "session evaluation")
     (let* ((full-body
 	    (mapconcat
 	     #'org-babel-chomp

+ 12 - 5
lisp/langs/org-babel-sh.el

@@ -130,13 +130,20 @@ last statement in BODY."
   (if (not session)
       ;; external process evaluation
       (save-window-excursion
-        (with-temp-buffer ;; TODO: figure out how to return non-output values from shell scripts
+        (with-temp-buffer
           (insert body)
           ;; (message "buffer=%s" (buffer-string)) ;; debugging
           (shell-command-on-region (point-min) (point-max) "sh" 'replace)
-          (buffer-string)))
+	  (case result-type
+	    (output (buffer-string))
+	    (value ;; TODO: figure out how to return non-output values from shell scripts
+	     (let ((tmp-file (make-temp-file "org-babel-sh"))
+		   (results (buffer-string)))
+	       (with-temp-file tmp-file (insert results))
+	       (org-babel-import-elisp-from-file tmp-file))))))
     ;; comint session evaluation
-    (let* ((full-body (mapconcat #'org-babel-chomp
+    (let* ((tmp-file (make-temp-file "org-babel-sh"))
+	   (full-body (mapconcat #'org-babel-chomp
                                  (list body org-babel-sh-eoe-indicator) "\n"))
            (raw (org-babel-comint-with-output buffer org-babel-sh-eoe-output nil
                   (insert full-body) (comint-send-input nil t)))
@@ -146,8 +153,8 @@ last statement in BODY."
       ;; (message (replace-regexp-in-string "%" "%%" (format "processed-results=%S" results))) ;; debugging
       (or (case result-type
             (output (org-babel-trim (mapconcat #'org-babel-trim (reverse results) "\n")))
-            (value (car results))
-            (t (reverse results))) ""))))
+            (value (with-temp-file tmp-file (insert (car results)))
+		   (org-babel-import-elisp-from-file tmp-file)))) "")))
 
 (defun org-babel-sh-strip-weird-long-prompt (string)
   (while (string-match "^% +[\r\n$]+ *" string)

+ 10 - 11
lisp/org-babel.el

@@ -161,6 +161,7 @@ the header arguments specified at the source code block."
                             (t 'value)))
          (cmd (intern (concat "org-babel-execute:" lang)))
          result)
+    (message (format "params=%S" params)) ;; debugging
     (unless (member lang org-babel-interpreters)
       (error "Language is not in `org-babel-interpreters': %s" lang))
     (setq result (org-babel-process-result (funcall cmd body params) result-type))
@@ -171,6 +172,15 @@ the header arguments specified at the source code block."
 			      
 (defun org-babel-process-result (result result-type)
   result)
+;; ;; ruby
+;;     (if (member "scalar" result-params)
+;;         results
+;;       (case result-type ;; process results based on the result-type
+;;         ('output (let ((tmp-file (make-temp-file "org-babel-ruby")))
+;;                    (with-temp-file tmp-file (insert results))
+;;                    (org-babel-import-elisp-from-file tmp-file)))
+;;         ('value (org-babel-ruby-table-or-results results))))))
+
 ;; python
 ;;    (if (member "scalar" result-params)
 ;;        results
@@ -205,17 +215,6 @@ the header arguments specified at the source code block."
 ;;         results))))
 
 
-;; ;; ruby
-;;     (if (member "scalar" result-params)
-;;         results
-;;       (case result-type ;; process results based on the result-type
-;;         ('output (let ((tmp-file (make-temp-file "org-babel-ruby")))
-;;                    (with-temp-file tmp-file (insert results))
-;;                    (org-babel-import-elisp-from-file tmp-file)))
-;;         ('value (org-babel-ruby-table-or-results results))))))
-
-
-
 ;; ;; rest of org-babel-execute-src-block
 
 ;;     ;; possibly force result into a vector