summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEric Schulte <schulte.eric@gmail.com>2011-11-15 20:13:41 -0700
committerEric Schulte <schulte.eric@gmail.com>2011-11-15 20:13:41 -0700
commit19884ab280f3f255f6a56320fd4b93e692bed9b6 (patch)
tree56254d1721f1f92d083e166a15077a42a0e18d8f
parent8114cf2bc460fc85d07e3141815b90a1c9eb41fb (diff)
downloadorg-mode-19884ab280f3f255f6a56320fd4b93e692bed9b6.tar.gz
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.
-rw-r--r--lisp/ob-ref.el26
-rw-r--r--lisp/ob.el5
-rw-r--r--testing/lisp/test-ob.el16
3 files changed, 33 insertions, 14 deletions
diff --git a/lisp/ob-ref.el b/lisp/ob-ref.el
index b7988ee..0714087 100644
--- a/lisp/ob-ref.el
+++ b/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)
diff --git a/lisp/ob.el b/lisp/ob.el
index efcd8da..5e721ba 100644
--- a/lisp/ob.el
+++ b/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
diff --git a/testing/lisp/test-ob.el b/testing/lisp/test-ob.el
index ceaafb3..c993fbe 100644
--- a/testing/lisp/test-ob.el
+++ b/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