From cb8bf4a0d0262772624e4427e95d8ff4ced0f111 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Sat, 17 Sep 2022 15:39:02 +0800 Subject: [PATCH] org-babel-expand-references: Fix :noweb-ref expansion * lisp/ob-core.el (org-babel-expand-noweb-references): Do not rely on reference cache being populated as a condition that all the references in buffer have been processed. Set this information explicitly. * testing/lisp/test-ob.el (test-ob/noweb-expansion): Add new test. --- lisp/ob-core.el | 4 +++- testing/lisp/test-ob.el | 47 ++++++++++++++++++++++++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 9843bb53a..8a963fa8e 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -2994,7 +2994,8 @@ block but are passed literally to the \"example-block\"." ;; run. Yet, ID is not in cache (see the above ;; condition). Process missing reference in ;; `expand-references'. - ((hash-table-p org-babel-expand-noweb-references--cache) + ((and (hash-table-p org-babel-expand-noweb-references--cache) + (gethash 'buffer-processed org-babel-expand-noweb-references--cache)) (expand-references id)) ;; Though luck. We go into the long process of ;; checking each source block and expand those @@ -3010,6 +3011,7 @@ block but are passed literally to the \"example-block\"." (let* ((info (org-babel-get-src-block-info t)) (ref (cdr (assq :noweb-ref (nth 2 info))))) (push info (gethash ref org-babel-expand-noweb-references--cache)))))) + (puthash 'buffer-processed t org-babel-expand-noweb-references--cache) (expand-references id))))) ;; Interpose PREFIX between every line. (if noweb-prefix diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el index 7fa330fe1..99912fe63 100644 --- a/testing/lisp/test-ob.el +++ b/testing/lisp/test-ob.el @@ -883,7 +883,52 @@ x #+begin_src emacs-lisp :noweb yes <> #+end_src" - (org-babel-expand-noweb-references))))) + (org-babel-expand-noweb-references)))) + ;; Test :noweb-ref expansion. + (should + (equal "(message \"!! %s\" \"Running confpkg-test-setup\") + +(message \"- Ran `%s'\" 'confpkg-test-strip-package-statements) + +(message \"!! %s\" \"Still running confpkg-test-setup\") + +(message \"- Ran elisp blocks in `%s'\" 'confpkg-test-dependency-analysis) + +(message \"!! %s\" \"End of confpkg-test-setup\")" + (org-test-with-temp-text " +* Setup + +#+name: confpkg-test-setup +#+begin_src emacs-lisp :results silent :noweb no-export +(message \"!! %s\" \"Running confpkg-test-setup\") + +<> + +(message \"!! %s\" \"Still running confpkg-test-setup\") + +<> + +(message \"!! %s\" \"End of confpkg-test-setup\") +#+end_src + +#+call: confpkg-test-setup[:results none]() + +* Identify cross-package dependencies + +#+begin_src emacs-lisp :noweb-ref confpkg-test-dependency-analysis +(message \"- Ran elisp blocks in `%s'\" 'confpkg-test-dependency-analysis) +#+end_src + +* Commenting out ~package!~ statements + +#+name: confpkg-test-strip-package-statements +#+begin_src emacs-lisp +(message \"- Ran `%s'\" 'confpkg-test-strip-package-statements) +#+end_src +" + (goto-char (point-min)) + (search-forward "begin_src") + (org-babel-expand-noweb-references))))) (ert-deftest test-ob/splitting-variable-lists-in-references () (org-test-with-temp-text ""