Browse Source

resolve named code blocks before named data

* lisp/ob-ref.el (org-babel-ref-resolve): Search for named code blocks
  before named data.
* lisp/ob.el (org-babel-named-data-regexp-for-name): New function for
  finding named data.
* testing/lisp/test-ob.el (test-ob/resolve-code-blocks-before-data-blocks):
  Test to ensure that named references are resolved in the correct
  order.
Eric Schulte 13 years ago
parent
commit
19884ab280
3 changed files with 33 additions and 14 deletions
  1. 12 14
      lisp/ob-ref.el
  2. 5 0
      lisp/ob.el
  3. 16 0
      testing/lisp/test-ob.el

+ 12 - 14
lisp/ob-ref.el

@@ -148,21 +148,19 @@ the variable."
       (save-restriction
 	(widen)
 	(goto-char (point-min))
-	(if (let* ((rx (regexp-quote ref))
-		   (res-rx (concat org-babel-result-regexp rx "[ \t]*.*$"))
-		   (src-rx (concat org-babel-src-name-regexp
-				   rx "\\(\(.*\)\\)?" "[ \t]*$")))
+	(if (let ((src-rx (org-babel-named-src-block-regexp-for-name ref))
+		  (res-rx (org-babel-named-data-regexp-for-name ref)))
 	      ;; goto ref in the current buffer
-	      (or (and (not args)
-		       (or (re-search-forward res-rx nil t)
-			   (re-search-backward res-rx nil t)))
-		  (re-search-forward src-rx nil t)
-		  (re-search-backward src-rx nil t)
-		  ;; check for local or global headlines by id
-		  (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)))))
+	      (or
+	       ;; check for code blocks
+	       (re-search-forward src-rx nil t)
+	       ;; check for named data
+	       (re-search-forward res-rx nil t)
+	       ;; check for local or global headlines by id
+	       (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 (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)

+ 5 - 0
lisp/ob.el

@@ -430,11 +430,16 @@ can not be resolved.")
 
 (defvar org-babel-after-execute-hook nil
   "Hook for functions to be called after `org-babel-execute-src-block'")
+
 (defun org-babel-named-src-block-regexp-for-name (name)
   "This generates a regexp used to match a src block named NAME."
   (concat org-babel-src-name-regexp (regexp-quote name) "[ \t\n]*"
 	  (substring org-babel-src-block-regexp 1)))
 
+(defun org-babel-named-data-regexp-for-name (name)
+  "This generates a regexp used to match data named NAME."
+  (concat org-babel-result-regexp (regexp-quote name) "[ \t]*.*$"))
+
 ;;; functions
 (defvar call-process-region)
 ;;;###autoload

+ 16 - 0
testing/lisp/test-ob.el

@@ -447,6 +447,22 @@ on two lines
     (should (string= (org-babel-execute-src-block)
 		     "A literal example\non two lines for me."))))
 
+(ert-deftest test-ob/resolve-code-blocks-before-data-blocks ()
+  (org-test-with-temp-text "
+#+name: foo
+: bar
+
+#+name: foo
+#+begin_src emacs-lisp
+  \"baz\"
+#+end_src
+
+#+begin_src emacs-lisp :var foo=foo
+  foo
+#+end_src"
+    (org-babel-next-src-block 2)
+    (should (string= (org-babel-execute-src-block) "baz"))))
+
 (provide 'test-ob)
 
 ;;; test-ob ends here