Browse Source

org-export: Fix export with Babel calls outside export scope

* contrib/lisp/org-export.el (org-export-as): Fix export with Babel
  calls outside export scope by providing buffer's name holding full
  data instead of the one holding its copy limited to scope.
* testing/lisp/test-org-export.el (test-org-export/export-scope): Add
  a test.
Nicolas Goaziou 13 years ago
parent
commit
dd7aa8ece9
2 changed files with 39 additions and 21 deletions
  1. 24 20
      contrib/lisp/org-export.el
  2. 15 1
      testing/lisp/test-org-export.el

+ 24 - 20
contrib/lisp/org-export.el

@@ -2091,7 +2091,11 @@ Return code as a string."
       ;; resulting from that process.  Eventually call
       ;; `org-export-filter-parse-tree-functions'.
       (goto-char (point-min))
-      (let ((info (org-export-get-environment backend subtreep ext-plist)))
+      (let ((info (org-export-get-environment backend subtreep ext-plist))
+	    ;; Save original file name or buffer in order to properly
+	    ;; resolve babel block expansion when body is outside
+	    ;; scope.
+	    (buf (or (buffer-file-name (buffer-base-buffer)) (current-buffer))))
 	;; Remove subtree's headline from contents if subtree mode is
 	;; activated.
 	(when subtreep (forward-line) (narrow-to-region (point) (point-max)))
@@ -2105,28 +2109,28 @@ Return code as a string."
 		(if noexpand (org-element-parse-buffer nil visible-only)
 		  (org-export-with-current-buffer-copy
 		   (org-export-expand-include-keyword)
-		   (let ((org-current-export-file (current-buffer)))
+		   (let ((org-current-export-file buf))
 		     (org-export-blocks-preprocess))
 		   (org-element-parse-buffer nil visible-only)))
 		backend info)))
-	;; Complete communication channel with tree properties.
-	(setq info
-	      (org-combine-plists
-	       info
-	       (org-export-collect-tree-properties raw-data info backend)))
-	;; Transcode RAW-DATA.  Also call
-	;; `org-export-filter-final-output-functions'.
-	(let* ((body (org-element-normalize-string
-		      (org-export-data raw-data backend info)))
-	       (template (intern (format "org-%s-template" backend)))
-	       (output (org-export-filter-apply-functions
-			(plist-get info :filter-final-output)
-			(if (or (not (fboundp template)) body-only) body
-			  (funcall template body info))
-			backend info)))
-	  ;; Maybe add final OUTPUT to kill ring before returning it.
-	  (when org-export-copy-to-kill-ring (org-kill-new output))
-	  output))))))
+	  ;; Complete communication channel with tree properties.
+	  (setq info
+		(org-combine-plists
+		 info
+		 (org-export-collect-tree-properties raw-data info backend)))
+	  ;; Transcode RAW-DATA.  Also call
+	  ;; `org-export-filter-final-output-functions'.
+	  (let* ((body (org-element-normalize-string
+			(org-export-data raw-data backend info)))
+		 (template (intern (format "org-%s-template" backend)))
+		 (output (org-export-filter-apply-functions
+			  (plist-get info :filter-final-output)
+			  (if (or (not (fboundp template)) body-only) body
+			    (funcall template body info))
+			  backend info)))
+	    ;; Maybe add final OUTPUT to kill ring, then return it.
+	    (when org-export-copy-to-kill-ring (org-kill-new output))
+	    output))))))
 
 (defun org-export-to-buffer
   (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)

+ 15 - 1
testing/lisp/test-org-export.el

@@ -232,7 +232,21 @@ text
       (transient-mark-mode 1)
       (push-mark (point) t t)
       (goto-char (point-at-eol))
-      (should (equal (org-export-as 'test) "text\n")))))
+      (should (equal (org-export-as 'test) "text\n"))))
+  ;; Subtree with a code block calling another block outside.
+  (org-test-with-temp-text "
+* Head1
+#+BEGIN_SRC emacs-lisp :noweb yes :exports results
+<<test>>
+#+END_SRC
+* Head2
+#+NAME: test
+#+BEGIN_SRC emacs-lisp
+\(+ 1 2)
+#+END_SRC"
+    (org-test-with-backend "test"
+      (forward-line 1)
+      (should (equal (org-export-as 'test 'subtree) ": 3\n")))))
 
 (ert-deftest test-org-export/export-snippet ()
   "Test export snippets transcoding."