Browse Source

babel: all code blocks with sessions are now evaluated on export

* contrib/babel/lisp/org-babel-exp.el (org-babel-exp-do-export):

  This brings babel more inline with Sweave, by ensuring that any code
  block which could change the state in a persistent session is
  executed.  Prior to this change the following org-mode text like would
  not export as expected because the x variable would not be
  initialized.

  ** inline expressions
     :PROPERTIES:
     :session:  *R*
     :END:

  #+begin_src R :exports code :results silent
    x<-5
  #+end_src

  the sum of 1 and 4 is equal to src_R{x}
Eric Schulte 14 years ago
parent
commit
367d81d9dc
1 changed files with 33 additions and 25 deletions
  1. 33 25
      contrib/babel/lisp/org-babel-exp.el

+ 33 - 25
contrib/babel/lisp/org-babel-exp.el

@@ -121,18 +121,21 @@ options are taken from `org-babel-default-header-args'."
 (defun org-babel-exp-do-export (info type)
   "Return a string containing the exported content of the current
 code block respecting the value of the :exports header argument."
-  (case (intern (or (cdr (assoc :exports (third info))) "code"))
-    ('none "")
-    ('code (org-babel-exp-code info type))
-    ('results (org-babel-exp-results info type))
-    ('both (concat (org-babel-exp-code info type)
-                   "\n\n"
-                   (org-babel-exp-results info type)))))
+  (flet ((silently () (when (cdr (assoc :session (third info)))
+			(org-babel-exp-results info type 'silent))))
+    (case (intern (or (cdr (assoc :exports (third info))) "code"))
+      ('none (silently) "")
+      ('code (silently) (org-babel-exp-code info type))
+      ('results (org-babel-exp-results info type))
+      ('both (concat (org-babel-exp-code info type)
+		     "\n\n"
+		     (org-babel-exp-results info type))))))
 
 (defun org-babel-exp-code (info type)
   "Return the code the current code block in a manner suitable
 for exportation by org-mode.  This function is called by
-`org-babel-exp-do-export'."
+`org-babel-exp-do-export'.  The code block will not be
+evaluated."
   (let ((lang (first info))
         (body (second info))
         (switches (fourth info))
@@ -165,10 +168,12 @@ for exportation by org-mode.  This function is called by
 		   call-line))
           ((t (format ": %s\n" call-line)))))))))
 
-(defun org-babel-exp-results (info type)
+(defun org-babel-exp-results (info type &optional silent)
   "Return the results of the current code block in a manner
 suitable for exportation by org-mode.  This function is called by
-`org-babel-exp-do-export'."
+`org-babel-exp-do-export'.  The code block will be evaluated.
+Optional argument SILENT can be used to inhibit insertion of
+results into the buffer."
   (let ((lang (first info))
 	(body (second info))
 	(params
@@ -189,28 +194,31 @@ suitable for exportation by org-mode.  This function is called by
         (let ((raw (org-babel-execute-src-block
                     nil info '((:results . "silent"))))
               (result-params (split-string (cdr (assoc :results params)))))
-          (cond ;; respect the value of the :results header argument
-           ((member "file" result-params)
-            (org-babel-result-to-file raw))
-           ((or (member "raw" result-params) (member "org" result-params))
-            (format "%s" raw))
-           ((member "code" result-params)
-            (format "src_%s{%s}" lang raw))
-           (t
-            (if (stringp raw)
-		(if (= 0 (length raw)) "=(no results)="
-		  (format "=%s=" raw))
-	      (format "=%S=" raw))))))
+          (unless silent
+	    (cond ;; respect the value of the :results header argument
+	     ((member "file" result-params)
+	      (org-babel-result-to-file raw))
+	     ((or (member "raw" result-params) (member "org" result-params))
+	      (format "%s" raw))
+	     ((member "code" result-params)
+	      (format "src_%s{%s}" lang raw))
+	     (t
+	      (if (stringp raw)
+		  (if (= 0 (length raw)) "=(no results)="
+		    (format "=%s=" raw))
+		(format "=%S=" raw)))))))
       ('block
           (org-babel-execute-src-block
-           nil nil (org-babel-merge-params params '((:results . "replace"))))
+           nil nil (org-babel-merge-params
+		    params `((:results . ,(if silent "silent" "replace")))))
         "")
       ('lob
        (save-excursion
 	 (re-search-backward org-babel-lob-one-liner-regexp nil t)
 	 (org-babel-execute-src-block
-	  nil (list lang body (org-babel-merge-params
-			       params '((:results . "replace"))))) "")))))
+	  nil (list lang body
+		    (org-babel-merge-params
+		     params `((:results . ,(if silent "silent" "replace")))))) "")))))
 
 (provide 'org-babel-exp)
 ;;; org-babel-exp.el ends here