From 178d8c7100176561a395d545c193ea36225ab81c Mon Sep 17 00:00:00 2001 From: Michael Brand Date: Wed, 22 May 2013 18:59:24 +0200 Subject: [PATCH] Entry properties as code block arguments in Babel * doc/org.texi (var): Document loc ("Location Of Call"). * lisp/ob-ref.el (org-babel-ref-resolve): Add `loc' which is set to point as a marker object. * testing/examples/babel.org: Add test entry for ERT. * testing/lisp/test-ob-exp.el: Add expected code block results for ERT. --- doc/org.texi | 9 +++++++++ lisp/ob-ref.el | 4 +++- testing/examples/babel.org | 30 ++++++++++++++++++++++++++++++ testing/lisp/test-ob-exp.el | 8 ++++++++ 4 files changed, 50 insertions(+), 1 deletions(-) diff --git a/doc/org.texi b/doc/org.texi index 290c671..29362cd 100644 --- a/doc/org.texi +++ b/doc/org.texi @@ -14381,6 +14381,15 @@ evaluation of the code block body. #+END_SRC @end example +Within Emacs Lisp evaluation the Lisp variable @code{loc} provides the +``Location Of Call'' as a marker object which can be passed for example to +the Lisp function @code{org-entry-get} to read a property from that entry +where the code block has been called: + address@hidden +#+HEADERS: :var todo_state=(org-entry-get loc "TODO" t) address@hidden example + Note that values read from tables and lists will not be evaluated as Emacs Lisp, as shown in the following example. diff --git a/lisp/ob-ref.el b/lisp/ob-ref.el index a2814ea..9645b9e 100644 --- a/lisp/ob-ref.el +++ b/lisp/ob-ref.el @@ -124,7 +124,9 @@ the variable." (save-excursion (let ((case-fold-search t) type args new-refere new-header-args new-referent result - lob-info split-file split-ref index index-row index-col id) + lob-info split-file split-ref index index-row index-col id + ;; For Emacs Lisp evaluation of :var, see Org manual. + (loc (point-marker))) ;; if ref is indexed grab the indices -- beware nested indices (when (and (string-match "\\[\\([^\\[]+\\)\\]$" ref) (let ((str (substring ref 0 (match-beginning 0)))) diff --git a/testing/examples/babel.org b/testing/examples/babel.org index 7c30ab2..8b18c3f 100644 --- a/testing/examples/babel.org +++ b/testing/examples/babel.org @@ -359,3 +359,33 @@ Here is a call line with more than just the results exported. <> echo "1$i" #+END_SRC + +* Emacs Lisp evaluation of :var with "loc" (Location Of Call) + :PROPERTIES: + :ID: cc5fbc20-bca5-437a-a7b8-2b4d7a03f820 + :END: + +#+NAME: func +#+HEADERS: :var a=(string-to-number (or (org-entry-get loc "a" t) "0")) +#+HEADERS: :var b=(string-to-number (or (org-entry-get loc "b" t) "0")) +#+HEADERS: :var c=(string-to-number (or (org-entry-get loc "c" t) "0")) +#+HEADERS: :var d=(string-to-number (or (org-entry-get loc "d" t) "0")) +#+HEADERS: :var e=(string-to-number (or (org-entry-get loc "e" t) "0")) +#+BEGIN_SRC emacs-lisp + (message "a:%d, b:%d, c:%d, d:%d, e:%d" a b c d e) +#+END_SRC + +** section + :PROPERTIES: + :a: 1 + :c: 3 + :END: +- section: call_func() + +*** subsection + :PROPERTIES: + :b: 2 + :c: 4 + :END: +- subsec1: call_func() +- subsec2: call_func(c=5, e=6) diff --git a/testing/lisp/test-ob-exp.el b/testing/lisp/test-ob-exp.el index abfe230..ea671f0 100644 --- a/testing/lisp/test-ob-exp.el +++ b/testing/lisp/test-ob-exp.el @@ -216,6 +216,14 @@ Here is one at the end of a line. =2= (should-not (string-match (regexp-quote "<>") result)) (should-not (string-match (regexp-quote "i=\"10\"") result))))) +(ert-deftest ob-exp/location-of-call () + (org-test-at-id "cc5fbc20-bca5-437a-a7b8-2b4d7a03f820" + (org-narrow-to-subtree) + (let ((result (org-test-with-expanded-babel-code (buffer-string)))) + (should (string-match "section: =a:1, b:0, c:3, d:0, e:0=" result)) + (should (string-match "subsec1: =a:1, b:2, c:4, d:0, e:0=" result)) + (should (string-match "subsec2: =a:1, b:2, c:5, d:0, e:6=" result))))) + (ert-deftest ob-exp/export-from-a-temp-buffer () :expected-result :failed (org-test-with-temp-text -- 1.7.4.2