diff --git a/lisp/ob-ref.el b/lisp/ob-ref.el index b7988eeb4..071408787 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 efcd8da34..5e721ba41 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 ceaafb347..c993fbef8 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