emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Local variables in an org file
@ 2015-08-20  6:39 Jarmo Hurri
  2015-08-21  2:23 ` Grant Rettke
  0 siblings, 1 reply; 6+ messages in thread
From: Jarmo Hurri @ 2015-08-20  6:39 UTC (permalink / raw)
  To: emacs-orgmode


Greetings.

Is there a way to define a (preferably local) variable in an org file so
that the value of that variable could be referenced in both plain text
and code blocks?

I often bump into situations where I have a value, say 768, and I use
that value both in code blocks and in the accompanying text. Now basic
programming approach tells me that if and when I need to change that
value, I should be able to do that by changing the value in only one
location. That implies defining a variable for the value.

Properties sound like they might be the solution. But I don't know how
to refer to properties in text and code.

Jarmo

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Local variables in an org file
  2015-08-20  6:39 Local variables in an org file Jarmo Hurri
@ 2015-08-21  2:23 ` Grant Rettke
  2015-08-21  2:52   ` Thomas S. Dye
  0 siblings, 1 reply; 6+ messages in thread
From: Grant Rettke @ 2015-08-21  2:23 UTC (permalink / raw)
  To: Jarmo Hurri; +Cc: emacs-orgmode@gnu.org

I've been thinking about this all week. Must be in the global memory space.
Grant Rettke
--
gcr@wisdomandwonder.com | http://www.wisdomandwonder.com/
“Wisdom begins in wonder.” --Socrates
“All creativity is an extended form of a joke.” --Kay
((λ (x) (x x)) (λ (x) (x x)))
“Life has become immeasurably better since I have been forced to stop
taking it seriously.” --Thompson


On Thu, Aug 20, 2015 at 1:39 AM, Jarmo Hurri <jarmo.hurri@iki.fi> wrote:
>
> Greetings.
>
> Is there a way to define a (preferably local) variable in an org file so
> that the value of that variable could be referenced in both plain text
> and code blocks?
>
> I often bump into situations where I have a value, say 768, and I use
> that value both in code blocks and in the accompanying text. Now basic
> programming approach tells me that if and when I need to change that
> value, I should be able to do that by changing the value in only one
> location. That implies defining a variable for the value.
>
> Properties sound like they might be the solution. But I don't know how
> to refer to properties in text and code.
>
> Jarmo
>
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Local variables in an org file
  2015-08-21  2:23 ` Grant Rettke
@ 2015-08-21  2:52   ` Thomas S. Dye
  2015-08-21 10:54     ` Jarmo Hurri
  0 siblings, 1 reply; 6+ messages in thread
From: Thomas S. Dye @ 2015-08-21  2:52 UTC (permalink / raw)
  To: Grant Rettke; +Cc: Jarmo Hurri, emacs-orgmode@gnu.org

Aloha all,

Grant Rettke <gcr@wisdomandwonder.com> writes:

> I've been thinking about this all week. Must be in the global memory space.
> Grant Rettke

Here is one way.

* 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().

When I export this to an ASCII buffer, I get:

1 Define a local variable
=========================

  ,----
  | (+ x 1)
  `----

  ,----
  | 769
  `----

  The variable is `768'.

All the best,
Tom

-- 
Thomas S. Dye
http://www.tsdye.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Local variables in an org file
  2015-08-21  2:52   ` Thomas S. Dye
@ 2015-08-21 10:54     ` Jarmo Hurri
  2015-08-21 11:57       ` Rainer M Krug
  2015-08-21 17:08       ` Thomas S. Dye
  0 siblings, 2 replies; 6+ messages in thread
From: Jarmo Hurri @ 2015-08-21 10:54 UTC (permalink / raw)
  To: emacs-orgmode

Thomas S. Dye <tsd@tsdye.com> 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.

Jarmo

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Local variables in an org file
  2015-08-21 10:54     ` Jarmo Hurri
@ 2015-08-21 11:57       ` Rainer M Krug
  2015-08-21 17:08       ` Thomas S. Dye
  1 sibling, 0 replies; 6+ messages in thread
From: Rainer M Krug @ 2015-08-21 11:57 UTC (permalink / raw)
  To: Jarmo Hurri; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1843 bytes --]

Jarmo Hurri <jarmo.hurri@iki.fi> writes:

> Thomas S. Dye <tsd@tsdye.com> 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.

I agree - and I asked the same question some time (years?) ago.

But you could make this easier if you would use macro expansion and
to insert your value instead of the variable name - so

my-var is {{{my-var}}}

would be exported to

my-var is 769

Just don't ask me how to define the macro... - possibly

#+MACRO: my-var call_my-var()

?

Actually, I think it would be a cool if one could always use
{{{VARIABLE_NAME}}} and get the value of the variable VARIABLE_NAME back
to be inserted in text.
In other words, macro expansion would do the following:

1) Check if a macro is defined with the name, if yes, use that
2) if no macro is defined with this name, return the variable as string
3) if no variable is defined continue as usual

Cheers,

Rainer

>
> Jarmo
>
>

-- 
Rainer M. Krug, PhD (Conservation Ecology, SUN), MSc (Conservation Biology, UCT), Dipl. Phys. (Germany)

Centre of Excellence for Invasion Biology
Stellenbosch University
South Africa

Tel :       +33 - (0)9 53 10 27 44
Cell:       +33 - (0)6 85 62 59 98
Fax :       +33 - (0)9 58 10 27 44

Fax (D):    +49 - (0)3 21 21 25 22 44

email:      Rainer@krugs.de

Skype:      RMkrug

PGP: 0x0F52F982

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 480 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Local variables in an org file
  2015-08-21 10:54     ` Jarmo Hurri
  2015-08-21 11:57       ` Rainer M Krug
@ 2015-08-21 17:08       ` Thomas S. Dye
  1 sibling, 0 replies; 6+ messages in thread
From: Thomas S. Dye @ 2015-08-21 17:08 UTC (permalink / raw)
  To: Jarmo Hurri; +Cc: emacs-orgmode

Aloha Jarmo,

Jarmo Hurri <jarmo.hurri@iki.fi> writes:

> Thomas S. Dye <tsd@tsdye.com> 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
<<jk-keywords>>
(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

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-08-21 17:08 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-08-20  6:39 Local variables in an org file Jarmo Hurri
2015-08-21  2:23 ` Grant Rettke
2015-08-21  2:52   ` Thomas S. Dye
2015-08-21 10:54     ` Jarmo Hurri
2015-08-21 11:57       ` Rainer M Krug
2015-08-21 17:08       ` Thomas S. Dye

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).