org-export: Fix expansion of babel calls in included files

* contrib/lisp/org-export.el (org-export-as):
  `org-current-export-file' should refer to current, temporary, buffer
  containing included contents, not to original buffer with include
  keywords.
(org-export-with-current-buffer-copy): Buffer copy must contain the
whole buffer, possibly narrowed to a proper part, not only the
narrowed part.
* testing/lisp/test-org-export.el: Tweak tests.
This commit is contained in:
Nicolas Goaziou 2012-10-18 20:44:24 +02:00
parent a729fae0f7
commit ccc98ebc2d
2 changed files with 37 additions and 40 deletions

View File

@ -2564,31 +2564,29 @@ Return code as a string."
;; Instead, a temporary copy is created, where include ;; Instead, a temporary copy is created, where include
;; keywords and macros are expanded and code blocks ;; keywords and macros are expanded and code blocks
;; are evaluated. ;; are evaluated.
(tree (let ((buf (or (buffer-file-name (buffer-base-buffer)) (tree (org-export-with-current-buffer-copy
(current-buffer)))) (unless noexpand
(org-export-with-current-buffer-copy (org-export-expand-include-keyword)
(unless noexpand ;; Update radio targets since keyword
(org-export-expand-include-keyword) ;; inclusion might have added some more.
;; Update radio targets since keyword (org-update-radio-target-regexp)
;; inclusion might have added some more. (org-export-expand-macro info)
(org-update-radio-target-regexp) ;; TODO: Setting `org-current-export-file' is
(org-export-expand-macro info) ;; required by Org Babel to properly resolve
;; TODO: Setting `org-current-export-file' is ;; noweb references. Once "org-exp.el" is
;; required by Org Babel to properly resolve ;; removed, modify
;; noweb references. Once "org-exp.el" is ;; `org-export-blocks-preprocess' so it
;; removed, modify ;; accepts the value as an argument instead.
;; `org-export-blocks-preprocess' so it (let ((org-current-export-file (current-buffer)))
;; accepts the value as an argument instead. (org-export-blocks-preprocess)))
(let ((org-current-export-file buf)) (goto-char (point-min))
(org-export-blocks-preprocess))) ;; Run hook
(goto-char (point-min)) ;; `org-export-before-parsing-hook'. with current
;; Run hook ;; back-end as argument.
;; `org-export-before-parsing-hook'. with current (run-hook-with-args
;; back-end as argument. 'org-export-before-parsing-hook backend)
(run-hook-with-args ;; Eventually parse buffer.
'org-export-before-parsing-hook backend) (org-element-parse-buffer nil visible-only))))
;; Eventually parse buffer.
(org-element-parse-buffer nil visible-only)))))
;; 3. Call parse-tree filters to get the final tree. ;; 3. Call parse-tree filters to get the final tree.
(setq tree (setq tree
(org-export-filter-apply-functions (org-export-filter-apply-functions
@ -2717,28 +2715,25 @@ The copy preserves local variables and visibility of the original
buffer. buffer.
Point is at buffer's beginning when BODY is applied." Point is at buffer's beginning when BODY is applied."
(org-with-gensyms (original-buffer offset buffer-string overlays) (declare (debug (body)))
`(let ((,original-buffer (current-buffer)) (org-with-gensyms (original-buffer offset buffer-string overlays region)
(,offset (1- (point-min))) `(let* ((,original-buffer (current-buffer))
(,buffer-string (buffer-string)) (,region (list (point-min) (point-max)))
(,overlays (mapcar (,buffer-string (org-with-wide-buffer (buffer-string)))
'copy-overlay (overlays-in (point-min) (point-max))))) (,overlays (mapcar 'copy-overlay (apply 'overlays-in ,region))))
(with-temp-buffer (with-temp-buffer
(let ((buffer-invisibility-spec nil)) (let ((buffer-invisibility-spec nil))
(org-clone-local-variables (org-clone-local-variables
,original-buffer ,original-buffer
"^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)") "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
(insert ,buffer-string) (insert ,buffer-string)
(apply 'narrow-to-region ,region)
(mapc (lambda (ov) (mapc (lambda (ov)
(move-overlay (move-overlay
ov ov (overlay-start ov) (overlay-end ov) (current-buffer)))
(- (overlay-start ov) ,offset)
(- (overlay-end ov) ,offset)
(current-buffer)))
,overlays) ,overlays)
(goto-char (point-min)) (goto-char (point-min))
(progn ,@body)))))) (progn ,@body))))))
(def-edebug-spec org-export-with-current-buffer-copy (body))
(defun org-export-expand-macro (info) (defun org-export-expand-macro (info)
"Expand every macro in buffer. "Expand every macro in buffer.

View File

@ -368,7 +368,9 @@ text
(goto-char (point-at-eol)) (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. ;; Subtree with a code block calling another block outside.
(org-test-with-temp-text " (should
(equal ": 3\n"
(org-test-with-temp-text "
* Head1 * Head1
#+BEGIN_SRC emacs-lisp :noweb yes :exports results #+BEGIN_SRC emacs-lisp :noweb yes :exports results
<<test>> <<test>>
@ -378,9 +380,9 @@ text
#+BEGIN_SRC emacs-lisp #+BEGIN_SRC emacs-lisp
\(+ 1 2) \(+ 1 2)
#+END_SRC" #+END_SRC"
(org-test-with-backend test (org-test-with-backend test
(forward-line 1) (forward-line 1)
(should (equal (org-export-as 'test 'subtree) ": 3\n"))))) (org-export-as 'test 'subtree))))))
(ert-deftest test-org-export/expand-include () (ert-deftest test-org-export/expand-include ()
"Test file inclusion in an Org buffer." "Test file inclusion in an Org buffer."