From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thomas S. Dye Subject: Re: Local variables in an org file Date: Fri, 21 Aug 2015 07:08:02 -1000 Message-ID: References: <87fv3e4g63.fsf@iki.fi> <87twrssyhy.fsf@iki.fi> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:45537) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZSpnj-00042B-Gc for emacs-orgmode@gnu.org; Fri, 21 Aug 2015 13:08:24 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1ZSpng-0000EQ-77 for emacs-orgmode@gnu.org; Fri, 21 Aug 2015 13:08:23 -0400 Received: from gproxy5-pub.mail.unifiedlayer.com ([67.222.38.55]:48450) by eggs.gnu.org with smtp (Exim 4.71) (envelope-from ) id 1ZSpnf-0000E7-Vz for emacs-orgmode@gnu.org; Fri, 21 Aug 2015 13:08:20 -0400 In-reply-to: <87twrssyhy.fsf@iki.fi> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Jarmo Hurri Cc: emacs-orgmode@gnu.org Aloha Jarmo, Jarmo Hurri writes: > Thomas S. Dye writes: > >> * Define a local variable >> #+name: my-var >> #+header: :exports none >> #+begin_src R >> 768 >> #+end_src >> #+name: pass-my-var-to-code-block >> #+header: :var x=my-var >> #+header: :exports both >> #+begin_src emacs-lisp >> (+ x 1) >> #+end_src >> >> #+results: pass-my-var-to-code-block >> : 769 >> >> The variable is call_my-var(). > > Yeah, this will work, but it's a huge amount of work. > > It would be brilliant if one could easily refer to a property inside a > subtree. In both text and code. #+SELECT_TAGS: export #+EXCLUDE_TAGS: noexport * Define a my-var property :PROPERTIES: :my-var: 768 :END: #+name: pass-property-to-code-block #+header: :var my-var=(string-to-number (jk-org-kwd "my-var")) #+header: :exports both #+begin_src emacs-lisp (+ my-var 1) #+end_src #+results: pass-property-to-code-block : 769 The variable =my-var= is call_jk-org-kwd("my-var"). * Access keyword values :noexport: This is taken from [[http://jkitchin.github.io/blog/2013/05/05/Getting-keyword-options-in-org-files/][John Kitchin's blog]]. It has been modified so =org-element-map= returns node properties in addition to keywords. #+name: jk-keywords #+header: :results silent #+begin_src emacs-lisp (defun jk-org-kwds () "parse the buffer and return a cons list of (property . value) from lines like: #+PROPERTY: value" (org-element-map (org-element-parse-buffer 'element) '(keyword node-property) (lambda (keyword) (cons (org-element-property :key keyword) (org-element-property :value keyword))))) #+end_src #+name: jk-org-kwd #+header: :noweb yes #+header: :var KEYWORD="" #+begin_src emacs-lisp <> (cdr (assoc KEYWORD (jk-org-kwds))) #+end_src This might be a huge amount of work, too, but it keeps the variable in a property instead of a source code block. I keep John Kitchin's code in the library of Babel so that part doesn't need to be reproduced each time. Here is the ascii output: 1 Define a my-var property ========================== ,---- | (+ my-var 1) `---- ,---- | 769 `---- The variable `my-var' is `768'. hth, Tom -- Thomas S. Dye http://www.tsdye.com