Browse Source

ob: expand noweb references to headline contents

* lisp/ob-ref.el (org-babel-ref-goto-headline-id): Split out into its
  own function.
  (org-babel-ref-headline-body): Split out into its own function.
  (org-babel-ref-resolve): Using new functions, and alignment.
* lisp/ob.el (org-babel-ref-goto-headline-id): Declare function.
  (org-babel-ref-headline-body): Declare function.
  (org-babel-expand-noweb-references): Now expands noweb references to
  headlines during expansion.
Eric Schulte 13 years ago
parent
commit
5b7646ce08
2 changed files with 54 additions and 42 deletions
  1. 26 21
      lisp/ob-ref.el
  2. 28 21
      lisp/ob.el

+ 26 - 21
lisp/ob-ref.el

@@ -81,6 +81,24 @@ the variable."
 		    (org-babel-ref-resolve ref))
 		out))))))
 
+(defun org-babel-ref-goto-headline-id (id)
+  (let ((rx (regexp-quote id)))
+    (or (re-search-forward
+	 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" rx "[ \t]*$") nil t)
+	(condition-case nil (progn (org-id-goto id) t) (error nil)))))
+
+(defun org-babel-ref-headline-body ()
+  (save-restriction
+    (org-narrow-to-subtree)
+    (buffer-substring
+     (save-excursion (goto-char (point-min))
+		     (forward-line 1)
+		     (when (looking-at "[ \t]*:PROPERTIES:")
+		       (re-search-forward ":END:" nil)
+		       (forward-char))
+		     (point))
+     (point-max))))
+
 (defvar org-babel-library-of-babel)
 (defun org-babel-ref-resolve (ref)
   "Resolve the reference REF and return its value."
@@ -127,16 +145,11 @@ the variable."
 		  (re-search-forward src-rx nil t)
 		  (re-search-backward src-rx nil t)
 		  ;; check for local or global headlines by id
-		  (setq id
-			(or
-			 (re-search-forward (concat "^[ \t]*:CUSTOM_ID:[ \t]+"
-						    rx "[ \t]*$") nil t)
-			 (condition-case nil (progn (org-id-goto ref) t)
-			     (error nil))))
+		  (setq id (org-babel-ref-goto-headline-id ref))
 		  ;; check the Library of Babel
 		  (setq lob-info (cdr (assoc (intern ref)
 					     org-babel-library-of-babel)))))
-	    (unless lob-info (goto-char (match-beginning 0)))
+	    (unless (or lob-info id) (goto-char (match-beginning 0)))
 	  ;; ;; TODO: allow searching for names in other buffers
 	  ;; (setq id-loc (org-id-find ref 'marker)
 	  ;;       buffer (marker-buffer id-loc)
@@ -155,21 +168,13 @@ the variable."
 	  (setq result
 		(case type
 		  (results-line (org-babel-read-result))
-		  (table (org-babel-read-table))
-		  (list (org-babel-read-list))
-		  (file (org-babel-read-link))
+		  (table        (org-babel-read-table))
+		  (list         (org-babel-read-list))
+		  (file         (org-babel-read-link))
 		  (source-block (org-babel-execute-src-block nil nil params))
-		  (lob (org-babel-execute-src-block nil lob-info params))
-		  (id (save-restriction
-			(org-narrow-to-subtree)
-			(buffer-substring
-			 (save-excursion (goto-char (point-min))
-					 (forward-line 1)
-					 (when (looking-at "[ \t]*:PROPERTIES:")
-					   (re-search-forward ":END:" nil)
-					   (forward-char))
-					 (point))
-			 (point-max)))))))
+		  (lob          (org-babel-execute-src-block
+				 nil lob-info params))
+		  (id           (org-babel-ref-headline-body)))))
 	(if (symbolp result)
 	    (format "%S" result)
 	  (if (and index (listp result))

+ 28 - 21
lisp/ob.el

@@ -68,6 +68,8 @@
 (declare-function org-babel-ref-split-args "ob-ref" (arg-string))
 (declare-function org-babel-ref-parse "ob-ref" (assignment))
 (declare-function org-babel-ref-resolve "ob-ref" (ref))
+(declare-function org-babel-ref-goto-headline-id "ob-ref" (id))
+(declare-function org-babel-ref-headline-body "ob-ref" ())
 (declare-function org-babel-lob-execute-maybe "ob-lob" ())
 (declare-function org-number-sequence "org-compat" (from &optional to inc))
 (declare-function org-at-item-p "org-list" ())
@@ -1893,28 +1895,33 @@ block but are passed literally to the \"example-block\"."
 		  ;; retrieve from the library of babel
 		  (nth 2 (assoc (intern source-name)
 				org-babel-library-of-babel))
+		  ;; return the contents of headlines literally
+		  (save-excursion
+		    (when (org-babel-ref-goto-headline-id source-name)
+		      (org-babel-ref-headline-body)))
 		  ;; find the expansion of reference in this buffer
-		  (or (mapconcat
-		       (lambda (i)
-			 (when (string= source-name
-					(or (cdr (assoc :noweb-ref (nth 2 i)))
-					    (nth 4 i)))
-			   (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))
-			       body))))
-		       (or blocks-in-buffer
-			   (setq blocks-in-buffer (blocks)))
-		       "")
-		      ;; possibly raise an error if named block doesn't exist
-		      (if (member lang org-babel-noweb-error-langs)
-			  (error "%s" (concat
-				       "<<" source-name ">> "
-				       "could not be resolved (see "
-				       "`org-babel-noweb-error-langs')"))
-			""))))
+		  (mapconcat
+		   (lambda (i)
+		     (when (string= source-name
+				    (or (cdr (assoc :noweb-ref (nth 2 i)))
+					(nth 4 i)))
+		       (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))
+			   body))))
+		   (or blocks-in-buffer
+		       (setq blocks-in-buffer (blocks)))
+		   "")
+		  ;; possibly raise an error if named block doesn't exist
+		  (if (member lang org-babel-noweb-error-langs)
+		      (error "%s" (concat
+				   "<<" source-name ">> "
+				   "could not be resolved (see "
+				   "`org-babel-noweb-error-langs')"))
+		    "")))
 	       "[\n\r]") (concat "\n" prefix)))))
         (nb-add (buffer-substring index (point-max)))))
     new-body))