From 9e4a6f37811088ceccbd6a6c64a162a99128c7e6 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Mon, 2 Jan 2012 10:59:53 -0700 Subject: [PATCH 1/2] optional "quick and dirty" :noweb reference expansion * lisp/ob.el (*org-babel-use-quick-and-dirty-noweb-expansion*): Controls the method in which noweb references are expanded. (org-babel-expand-noweb-references): Bring back the option for regexp-based noweb expansion. --- lisp/ob.el | 35 +++++++++++++++++++++++++++-------- 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/lisp/ob.el b/lisp/ob.el index a392c232a..3eee92a90 100644 --- a/lisp/ob.el +++ b/lisp/ob.el @@ -1979,6 +1979,12 @@ parameters when merging lists." '(results exports tangle noweb padline cache shebang comments)) params)) +(defvar *org-babel-use-quick-and-dirty-noweb-expansion* nil + "Set to true to use regular expressions to expand noweb references. +This results in much faster noweb reference expansion but does +not properly allow code blocks to inherit the \":noweb-ref\" +header argument from buffer or subtree wide properties.") + (defun org-babel-expand-noweb-references (&optional info parent-buffer) "Expand Noweb references in the body of the current source code block. @@ -2014,6 +2020,8 @@ block but are passed literally to the \"example-block\"." (lang (nth 0 info)) (body (nth 1 info)) (comment (string= "noweb" (cdr (assoc :comments (nth 2 info))))) + (rx-prefix (concat "\\(" org-babel-src-name-regexp "\\|" + ":noweb-ref[ \t]+" "\\)")) (new-body "") index source-name evaluate prefix blocks-in-buffer) (flet ((nb-add (text) (setq new-body (concat new-body text))) (c-wrap (text) @@ -2054,21 +2062,32 @@ block but are passed literally to the \"example-block\"." (when (org-babel-ref-goto-headline-id source-name) (org-babel-ref-headline-body))) ;; find the expansion of reference in this buffer - (let (expansion) + (let ((rx (concat rx-prefix source-name)) + expansion) (save-excursion (goto-char (point-min)) - (org-babel-map-src-blocks nil - (let ((i (org-babel-get-src-block-info 'light))) - (when (equal (or (cdr (assoc :noweb-ref (nth 2 i))) - (nth 4 i)) - source-name) - (let ((body (org-babel-expand-noweb-references i))) + (if *org-babel-use-quick-and-dirty-noweb-expansion* + (while (re-search-forward rx nil t) + (let* ((i (org-babel-get-src-block-info 'light)) + (body (org-babel-expand-noweb-references i))) (if comment ((lambda (cs) (concat (c-wrap (car cs)) "\n" body "\n" (c-wrap (cadr cs)))) (org-babel-tangle-comment-links i)) - (setq expansion (concat expansion body)))))))) + (setq expansion (concat expansion body))))) + (org-babel-map-src-blocks nil + (let ((i (org-babel-get-src-block-info 'light))) + (when (equal (or (cdr (assoc :noweb-ref (nth 2 i))) + (nth 4 i)) + source-name) + (let ((body (org-babel-expand-noweb-references i))) + (if comment + ((lambda (cs) + (concat (c-wrap (car cs)) "\n" + body "\n" (c-wrap (cadr cs)))) + (org-babel-tangle-comment-links i)) + (setq expansion (concat expansion body))))))))) expansion) ;; possibly raise an error if named block doesn't exist (if (member lang org-babel-noweb-error-langs) From c2ee31fa44204dae970576e6875388a3a2d5e3d9 Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Mon, 2 Jan 2012 11:10:48 -0700 Subject: [PATCH 2/2] documentation of *org-babel-use-quick-and-dirty-noweb-expansion* * doc/org.texi (Noweb reference syntax): Adding documentation of the *org-babel-use-quick-and-dirty-noweb-expansion* variable. --- doc/org.texi | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/doc/org.texi b/doc/org.texi index f224b1f3b..bcb69eb71 100644 --- a/doc/org.texi +++ b/doc/org.texi @@ -14001,7 +14001,8 @@ When a code block is tangled or evaluated, whether or not ``noweb'' references are expanded depends upon the value of the @code{:noweb} header argument. If @code{:noweb yes}, then a Noweb reference is expanded before evaluation. If @code{:noweb no}, the default, then the reference is not -expanded before evaluation. +expanded before evaluation. See the @ref{noweb-ref} header argument for +a more flexible way to resolve noweb references. Note: the default value, @code{:noweb no}, was chosen to ensure that correct code is not broken in a language, such as Ruby, where @@ -14009,6 +14010,12 @@ correct code is not broken in a language, such as Ruby, where syntactically valid in languages that you use, then please consider setting the default value. +Note: if noweb tangling is slow in large Org-mode files consider setting the +@code{*org-babel-use-quick-and-dirty-noweb-expansion*} variable to true. +This will result in faster noweb reference resolution at the expense of not +correctly resolving inherited values of the @code{:noweb-ref} header +argument. + @node Key bindings and useful functions, Batch execution, Noweb reference syntax, Working With Source Code @section Key bindings and useful functions @cindex code block, key bindings