[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] Local variables in an org file

From: Thomas S . Dye
Subject: Re: [O] Local variables in an org file
Date: Fri, 21 Aug 2015 07:08:02 -1000

Aloha Jarmo,

Jarmo Hurri <address@hidden> writes:

> Thomas S. Dye <address@hidden> 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
  :my-var:   768

#+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)

#+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 
 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 

#+name: jk-org-kwd
#+header: :noweb yes
#+header: :var KEYWORD=""
#+begin_src emacs-lisp
(cdr (assoc KEYWORD (jk-org-kwds)))

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'.

Thomas S. Dye

reply via email to

[Prev in Thread] Current Thread [Next in Thread]