diff options
author | Nicolas Goaziou <mail@nicolasgoaziou.fr> | 2018-01-22 19:29:32 +0100 |
---|---|---|
committer | Nicolas Goaziou <mail@nicolasgoaziou.fr> | 2018-01-22 19:29:32 +0100 |
commit | 2367e25c6b40c7ea32d03a5e6e1e3051419f59dd (patch) | |
tree | bf183c95cf390bc7847c7987b151861e89a67046 | |
parent | da1b9fdca1d3546e6fe015cedbcc745ace216ff0 (diff) | |
parent | 4d46a20ea2f5d3a041e5b3aaf7c36222a8156485 (diff) | |
download | org-mode-2367e25c6b40c7ea32d03a5e6e1e3051419f59dd.tar.gz |
Merge branch 'maint'
-rw-r--r-- | lisp/ob-lob.el | 72 | ||||
-rw-r--r-- | testing/lisp/test-ob-lob.el | 55 |
2 files changed, 96 insertions, 31 deletions
diff --git a/lisp/ob-lob.el b/lisp/ob-lob.el index ac89141..9bf6858 100644 --- a/lisp/ob-lob.el +++ b/lisp/ob-lob.el @@ -79,44 +79,54 @@ if so then run the appropriate source block from the Library." (org-babel-execute-src-block nil info) t))) -(defun org-babel-lob--src-info (name) - "Return internal representation for Babel data named NAME. -NAME is a string. This function looks into the current document +(defun org-babel-lob--src-info (ref) + "Return internal representation for Babel data referenced as REF. +REF is a string. This function looks into the current document for a Babel call or source block. If none is found, it looks -after NAME in the Library of Babel. Eventually, if that also -fails, it returns nil." - ;; During export, look into the pristine copy of the document being - ;; exported instead of the current one, which could miss some data. - (with-current-buffer (or org-babel-exp-reference-buffer (current-buffer)) - (org-with-wide-buffer - (goto-char (point-min)) - (catch :found - (let ((case-fold-search t) - (regexp (org-babel-named-data-regexp-for-name name))) - (while (re-search-forward regexp nil t) - (let ((element (org-element-at-point))) - (when (equal name (org-element-property :name element)) - (throw :found - (pcase (org-element-type element) - (`src-block (org-babel-get-src-block-info t element)) - (`babel-call (org-babel-lob-get-info element)) - ;; Non-executable data found. Since names are - ;; supposed to be unique throughout a document, - ;; bail out. - (_ nil)))))) - ;; No element named NAME in buffer. Try Library of Babel. - (cdr (assoc-string name org-babel-library-of-babel))))))) +after REF in the Library of Babel." + (let ((name ref) + (file nil)) + ;; Extract the remote file, if specified in the reference. + (when (string-match "\\`\\(.+\\):\\(.+\\)\\'" ref) + (setq file (match-string 1 ref)) + (setq name (match-string 2 ref))) + ;; During export, look into the pristine copy of the document + ;; being exported instead of the current one, which could miss + ;; some data. + (with-current-buffer (cond (file (find-file-noselect file t)) + (org-babel-exp-reference-buffer) + (t (current-buffer))) + (org-with-point-at 1 + (catch :found + (let ((case-fold-search t) + (regexp (org-babel-named-data-regexp-for-name name))) + (while (re-search-forward regexp nil t) + (let ((element (org-element-at-point))) + (when (equal name (org-element-property :name element)) + (throw :found + (pcase (org-element-type element) + (`src-block (org-babel-get-src-block-info t element)) + (`babel-call (org-babel-lob-get-info element)) + ;; Non-executable data found. Since names + ;; are supposed to be unique throughout + ;; a document, bail out. + (_ nil)))))) + (cdr (assoc-string ref org-babel-library-of-babel)))))))) ;;;###autoload (defun org-babel-lob-get-info (&optional datum) "Return internal representation for Library of Babel function call. -Consider DATUM, when provided, or element at point. Return nil -when not on an appropriate location. Otherwise return a list -compatible with `org-babel-get-src-block-info', which see." + +Consider DATUM, when provided, or element at point otherwise. + +Return nil when not on an appropriate location. Otherwise return +a list compatible with `org-babel-get-src-block-info', which +see." (let* ((context (or datum (org-element-context))) - (type (org-element-type context))) + (type (org-element-type context)) + (reference (org-element-property :call context))) (when (memq type '(babel-call inline-babel-call)) - (pcase (org-babel-lob--src-info (org-element-property :call context)) + (pcase (org-babel-lob--src-info reference) (`(,language ,body ,header ,_ ,_ ,_ ,coderef) (let ((begin (org-element-property (if (eq type 'inline-babel-call) :begin diff --git a/testing/lisp/test-ob-lob.el b/testing/lisp/test-ob-lob.el index 49f1f24..316c53e 100644 --- a/testing/lisp/test-ob-lob.el +++ b/testing/lisp/test-ob-lob.el @@ -193,6 +193,61 @@ call_test-newline[:eval yes :results raw]('(1\n2))<point>" (org-babel-execute-src-block nil (org-babel-lob-get-info)) (buffer-substring (point) (point-max)))))) +(ert-deftest test-ob-lob/external-reference-syntax () + "Test external reference syntax for Babel calls." + (should + (= 2 + (org-test-with-temp-text-in-file + "#+name: foo\n#+begin_src emacs-lisp\n(+ 1 1)\n#+end_src" + (let ((file (buffer-file-name))) + (org-test-with-temp-text (format "#+call: %s:foo()" file) + (org-babel-execute-src-block nil (org-babel-lob-get-info)))))))) + +(ert-deftest test-ob-lob/call-with-indirection () + "Test calling code with indirection." + (should + (= 2 + (org-test-with-temp-text + " +#+name: foo +#+begin_src emacs-lisp +\(+ 1 1) +#+end_src + +#+name: bar +#+call: foo() + +<point>#+call: bar()" + (org-babel-execute-src-block nil (org-babel-lob-get-info))))) + (should + (= 10 + (org-test-with-temp-text + " +#+name: foo +#+begin_src emacs-lisp :var x=1 +\(* 2 x) +#+end_src + +#+name: bar +#+call: foo(x=3) + +<point>#+call: bar(x=5)" + (org-babel-execute-src-block nil (org-babel-lob-get-info))))) + (should + (= 6 + (org-test-with-temp-text + " +#+name: foo +#+begin_src emacs-lisp :var x=1 +\(* 2 x) +#+end_src + +#+name: bar +#+call: foo(x=3) + +<point>#+call: bar()" + (org-babel-execute-src-block nil (org-babel-lob-get-info)))))) + (provide 'test-ob-lob) ;;; test-ob-lob.el ends here |