From 367d81d9dcc631b038306d605210c2b32d2951d6 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Sun, 6 Jun 2010 19:13:14 -0700 Subject: [PATCH] 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} --- contrib/babel/lisp/org-babel-exp.el | 58 ++++++++++++++++------------- 1 file changed, 33 insertions(+), 25 deletions(-) diff --git a/contrib/babel/lisp/org-babel-exp.el b/contrib/babel/lisp/org-babel-exp.el index 2764412cb..0fc050e45 100644 --- a/contrib/babel/lisp/org-babel-exp.el +++ b/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