resolve named code blocks before named data
authorEric Schulte <schulte.eric@gmail.com>
Wed, 16 Nov 2011 03:13:41 +0000 (20:13 -0700)
committerEric Schulte <schulte.eric@gmail.com>
Wed, 16 Nov 2011 03:13:41 +0000 (20:13 -0700)
* 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.

lisp/ob-ref.el
lisp/ob.el
testing/lisp/test-ob.el

index b7988ee..0714087 100644 (file)
@@ -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)
index efcd8da..5e721ba 100644 (file)
@@ -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
index ceaafb3..c993fbe 100644 (file)
@@ -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