Bläddra i källkod

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.
Eric Schulte 13 år sedan
förälder
incheckning
9e4a6f3781
1 ändrade filer med 27 tillägg och 8 borttagningar
  1. 27 8
      lisp/ob.el

+ 27 - 8
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)