emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Using property values in source code blocks
@ 2016-07-06  4:10 Joon Ro
  2016-07-06 17:36 ` Charles C. Berry
  0 siblings, 1 reply; 13+ messages in thread
From: Joon Ro @ 2016-07-06  4:10 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org

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

Hi,
I'm aware of passing variables through header arguments, and also inserting another source code block using the noweb syntax. 
I was wondering, however, would it be possible to directly input values of properties inside source code blocks? For example,
* Subtree:PROPERTIES::DUMMY: Value:END:
#+BEGIN_SRC
{{DUMMY}} <- something like this?
#+END_SRC
Thank you so much,Joon 		 	   		  

[-- Attachment #2: Type: text/html, Size: 902 bytes --]

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

* Re: Using property values in source code blocks
  2016-07-06  4:10 Using property values in source code blocks Joon Ro
@ 2016-07-06 17:36 ` Charles C. Berry
  2016-07-06 20:03   ` Joon Ro
  0 siblings, 1 reply; 13+ messages in thread
From: Charles C. Berry @ 2016-07-06 17:36 UTC (permalink / raw)
  To: Joon Ro; +Cc: emacs-orgmode@gnu.org

On Tue, 5 Jul 2016, Joon Ro wrote:

> Hi,

> I'm aware of passing variables through header arguments, and also 
> inserting another source code block using the noweb syntax.

> I was wondering, however, would it be possible to directly input values 
> of properties inside source code blocks? For example,

Yes. `org-property-values' does the trick


* Subtree
:PROPERTIES:
:DUMMY: Value
:END:

#+BEGIN_SRC shell :var dumdum=(car (org-property-values "DUMMY"))
echo $dumdum
#+END_SRC

#+RESULTS:
: Value

#+NAME: get-property
#+BEGIN_SRC emacs-lisp :var prop="prop"
(org-property-values prop)
#+END_SRC

#+BEGIN_SRC emacs-lisp :noweb yes
(quote
<<get-property(prop="DUMMY")>>
)
#+END_SRC

#+RESULTS:
| Value |

HTH,

Chuck

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

* Re: Using property values in source code blocks
  2016-07-06 17:36 ` Charles C. Berry
@ 2016-07-06 20:03   ` Joon Ro
  2016-07-06 23:05     ` Charles C. Berry
  0 siblings, 1 reply; 13+ messages in thread
From: Joon Ro @ 2016-07-06 20:03 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode@gnu.org

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


> Yes. `org-property-values' does the trick
> 
> 
> * Subtree
> :PROPERTIES:
> :DUMMY: Value
> :END:
> 
> #+BEGIN_SRC shell :var dumdum=(car (org-property-values "DUMMY"))
> echo $dumdum
> #+END_SRC
> 
> #+RESULTS:
> : Value
> 
> #+NAME: get-property
> #+BEGIN_SRC emacs-lisp :var prop="prop"
> (org-property-values prop)
> #+END_SRC
> 
> #+BEGIN_SRC emacs-lisp :noweb yes
> (quote
> <<get-property(prop="DUMMY")>>
> )
> #+END_SRC
> 
> #+RESULTS:
> | Value |
> 
> HTH,
> 
> Chuck
> 

Thank you so much for the reply - this would be very useful. Would there be, however, a way to directly replace some place holder inside a source code block? The reason is I have to put those values inside a comment, so I cannot pass them as variables.
Best,Joon 		 	   		  

[-- Attachment #2: Type: text/html, Size: 1342 bytes --]

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

* Re: Using property values in source code blocks
  2016-07-06 20:03   ` Joon Ro
@ 2016-07-06 23:05     ` Charles C. Berry
  2016-07-07  3:50       ` Joon Ro
  0 siblings, 1 reply; 13+ messages in thread
From: Charles C. Berry @ 2016-07-06 23:05 UTC (permalink / raw)
  To: Joon Ro; +Cc: emacs-orgmode@gnu.org

On Wed, 6 Jul 2016, Joon Ro wrote:

>
>> Yes. `org-property-values' does the trick
>>
>>
>> * Subtree
>> :PROPERTIES:
>> :DUMMY: Value
>> :END:
>>
>> #+BEGIN_SRC shell :var dumdum=(car (org-property-values "DUMMY"))
>> echo $dumdum
>> #+END_SRC
>>
[deleted]

>
> Thank you so much for the reply - this would be very useful. Would there 
> be, however, a way to directly replace some place holder inside a source 
> code block?

I have no idea what you are asking.

> The reason is I have to put those values inside a comment, 
> so I cannot pass them as variables.

So your original query about accessing property values from src blocks was 
not what you wanted?

Examples of what one is trying to do - even if they do not work exactly as 
intended - are often more helpful than verbal explanations of what one 
would like to do.

Chuck

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

* Re: Using property values in source code blocks
  2016-07-06 23:05     ` Charles C. Berry
@ 2016-07-07  3:50       ` Joon Ro
  2016-07-07 15:48         ` Charles C. Berry
  0 siblings, 1 reply; 13+ messages in thread
From: Joon Ro @ 2016-07-07  3:50 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode@gnu.org

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




> I have no idea what you are asking.
> 
> > The reason is I have to put those values inside a comment, 
> > so I cannot pass them as variables.
> 
> So your original query about accessing property values from src blocks was 
> not what you wanted?
> 
> Examples of what one is trying to do - even if they do not work exactly as 
> intended - are often more helpful than verbal explanations of what one 
> would like to do.
> 
> Chuck
> 

I'm sorry if my explanation was not clear, but the original example I provided shows exactly what I wanted to do:
* Subtree:PROPERTIES::DUMMY: VALUE:END:#+BEGIN_SRC{{DUMMY}} <- something like this?#+END_SRC
Basically, I want to replace {{DUMMY}} with VALUE, without passing it as a variable. So just like <<noweb>> replacement, but with a property value instead of a code block. I get the feeling that this is not possible, but I wanted to ask just in case. 
Best,Joon


 		 	   		  

[-- Attachment #2: Type: text/html, Size: 3649 bytes --]

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

* Re: Using property values in source code blocks
  2016-07-07  3:50       ` Joon Ro
@ 2016-07-07 15:48         ` Charles C. Berry
  2016-07-07 15:53           ` Joon Ro
  0 siblings, 1 reply; 13+ messages in thread
From: Charles C. Berry @ 2016-07-07 15:48 UTC (permalink / raw)
  To: Joon Ro; +Cc: emacs-orgmode@gnu.org

On Wed, 6 Jul 2016, Joon Ro wrote:

>
>
>
>> I have no idea what you are asking.
>>

[snip]

> I'm sorry if my explanation was not clear, but the original example I 
> provided shows exactly what I wanted to do:

And my response showed how to do it: you construct a src block with a 
noweb reference that places the *results* of evaluating another src block 
in the code (or comment). The src block in the named in the noweb 
reference handles retrieving the property value, viz.

--8<---------------cut here---------------end--------------->8---

* Subtree
:PROPERTIES:
:DUMMY: Value
:END:

#+NAME: get-property
#+BEGIN_SRC emacs-lisp :var prop="prop"
(car (org-property-values prop))
#+END_SRC

#+BEGIN_SRC shell :noweb yes

echo <<get-property(prop="DUMMY")>>

#+END_SRC

#+RESULTS:
: Value

--8<---------------cut here---------------end--------------->8---


See

   (info "(org) Noweb reference syntax")


Chuck

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

* Re: Using property values in source code blocks
  2016-07-07 15:48         ` Charles C. Berry
@ 2016-07-07 15:53           ` Joon Ro
  2016-07-15 23:38             ` Joon Ro
  0 siblings, 1 reply; 13+ messages in thread
From: Joon Ro @ 2016-07-07 15:53 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode@gnu.org

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



> Date: Thu, 7 Jul 2016 08:48:03 -0700
> From: ccberry@ucsd.edu
> To: joon.ro@outlook.com
> Subject: Re: [O] Using property values in source code blocks
> CC: emacs-orgmode@gnu.org
> 
> On Wed, 6 Jul 2016, Joon Ro wrote:
> 
> >
> >
> >
> >> I have no idea what you are asking.
> >>
> 
> [snip]
> 
> > I'm sorry if my explanation was not clear, but the original example I 
> > provided shows exactly what I wanted to do:
> 
> And my response showed how to do it: you construct a src block with a 
> noweb reference that places the *results* of evaluating another src block 
> in the code (or comment). The src block in the named in the noweb 
> reference handles retrieving the property value, viz.
> 
> --8<---------------cut here---------------end--------------->8---
> 
> * Subtree
> :PROPERTIES:
> :DUMMY: Value
> :END:
> 
> #+NAME: get-property
> #+BEGIN_SRC emacs-lisp :var prop="prop"
> (car (org-property-values prop))
> #+END_SRC
> 
> #+BEGIN_SRC shell :noweb yes
> 
> echo <<get-property(prop="DUMMY")>>
> 
> #+END_SRC
> 
> #+RESULTS:
> : Value
> 
> --8<---------------cut here---------------end--------------->8---
> 
> 
> See
> 
>    (info "(org) Noweb reference syntax")
> 
> 
> Chuck
> 

I see. I must have misunderstood that example. I will try that - thank you so much!

 		 	   		  

[-- Attachment #2: Type: text/html, Size: 1968 bytes --]

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

* Re: Using property values in source code blocks
  2016-07-07 15:53           ` Joon Ro
@ 2016-07-15 23:38             ` Joon Ro
  2016-07-15 23:44               ` Joon Ro
  0 siblings, 1 reply; 13+ messages in thread
From: Joon Ro @ 2016-07-15 23:38 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode@gnu.org

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

> --8<---------------cut here---------------end--------------->8---
> 
> * Subtree
> :PROPERTIES:
> :DUMMY: Value
> :END:
> 
> #+NAME: get-property
> #+BEGIN_SRC emacs-lisp :var prop="prop"
> (car (org-property-values prop))
> #+END_SRC
> 
> #+BEGIN_SRC shell :noweb yes
> 
> echo <<get-property(prop="DUMMY")>>
> 
> #+END_SRC
> 
> #+RESULTS:
> : Value
> 
> --8<---------------cut here---------------end--------------->8---
> 

It seems org-property-values gets the property value for the buffer - it ends up getting the property value from the last subtree in the buffer. I solved this by using either (org-entry-get nil prop) or (car (org-entry-get-multivalued-property nil prop)) instead of (car (org-property-values prop)). 
Thanks again!-Joon
 		 	   		   		 	   		  

[-- Attachment #2: Type: text/html, Size: 1711 bytes --]

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

* Re: Using property values in source code blocks
  2016-07-15 23:38             ` Joon Ro
@ 2016-07-15 23:44               ` Joon Ro
  2016-07-16  2:05                 ` Charles C. Berry
  0 siblings, 1 reply; 13+ messages in thread
From: Joon Ro @ 2016-07-15 23:44 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode@gnu.org

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


It seems org-property-values gets the property value for the buffer - it ends up getting the property value from the last subtree in the buffer. I solved this by using either (org-entry-get nil prop) or (car (org-entry-get-multivalued-property nil prop)) instead of (car (org-property-values prop)). 
I spoke too early - with (org-entry-get nil prop) I have to put the code block under the subtree I want to extract the property value from. Would it be possible to get the property value of the current subtree? For example, the following
* Top Heading#+NAME: get-property#+BEGIN_SRC emacs-lisp :noweb yes :var prop="prop"(car (org-entry-get-multivalued-property nil prop))#+END_SRC
** Subheading:PROPERTIES::DUMMY:    100:END:
#+BEGIN_SRC shell :noweb yesecho <<get-property(prop="DUMMY")>>#+END_SRC
yields nil. 
Best Regards,Joon
 		 	   		   		 	   		   		 	   		  

[-- Attachment #2: Type: text/html, Size: 3958 bytes --]

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

* Re: Using property values in source code blocks
  2016-07-15 23:44               ` Joon Ro
@ 2016-07-16  2:05                 ` Charles C. Berry
  2016-07-16  2:47                   ` Joon Ro
  0 siblings, 1 reply; 13+ messages in thread
From: Charles C. Berry @ 2016-07-16  2:05 UTC (permalink / raw)
  To: Joon Ro; +Cc: emacs-orgmode@gnu.org

On Fri, 15 Jul 2016, Joon Ro wrote:

>
> I spoke too early - with (org-entry-get nil prop) I have to put the code 
> block under the subtree I want to extract the property value from. Would 
> it be possible to get the property value of the current subtree?

Give org-entry-get a suitable value for `pom'.

See the docstring for org-entry-get.

Example:

--8<---------------cut here---------------start------------->8---

* Top Heading

#+NAME: get-property
#+BEGIN_SRC emacs-lisp :noweb yes :var prop="prop" :var pom=0
(car (org-entry-get-multivalued-property pom prop))
#+END_SRC

** Subheading
:PROPERTIES:
:DUMMY: 100
:END:

#+BEGIN_SRC shell :noweb yes
echo <<get-property(prop="DUMMY",pom=(point))>>
#+END_SRC

#+RESULTS:
: 100

--8<---------------cut here---------------end--------------->8---


Chuck

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

* Re: Using property values in source code blocks
  2016-07-16  2:05                 ` Charles C. Berry
@ 2016-07-16  2:47                   ` Joon Ro
  2016-07-16 16:02                     ` Charles C. Berry
  0 siblings, 1 reply; 13+ messages in thread
From: Joon Ro @ 2016-07-16  2:47 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode@gnu.org

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



> 
> Example:
> 
> --8<---------------cut here---------------start------------->8---
> 
> * Top Heading
> 
> #+NAME: get-property
> #+BEGIN_SRC emacs-lisp :noweb yes :var prop="prop" :var pom=0
> (car (org-entry-get-multivalued-property pom prop))
> #+END_SRC
> 
> ** Subheading
> :PROPERTIES:
> :DUMMY: 100
> :END:
> 
> #+BEGIN_SRC shell :noweb yes
> echo <<get-property(prop="DUMMY",pom=(point))>>
> #+END_SRC
> 
> #+RESULTS:
> : 100
> 
> --8<---------------cut here---------------end--------------->8---
> 

Hi Chuck,
Thank you so much. It definitely works when I evaluate the code block - but my ultimate goal is to tangle the source code (I want to generate bunch of script files with a template using property values), and when I tangle the code block it returns nil. For example:
* Top Heading#+NAME: get-property#+BEGIN_SRC emacs-lisp :noweb yes :var prop="prop" :var pom=0(car (org-entry-get-multivalued-property pom prop))#+END_SRC** Subheading:PROPERTIES::DUMMY: 100:END:#+BEGIN_SRC shell :noweb yes :tangle ./tangle-test.shecho <<get-property(prop="DUMMY",pom=(point))>>#+END_SRC
When I evaluate the code block, I get : 100 results. However, when I tangle the block, the resulting tangle-test.sh has echo nil instead of echo 100. 
Am I doing something wrong? Thank you so much for your help.
-Joon



 		 	   		  

[-- Attachment #2: Type: text/html, Size: 2297 bytes --]

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

* Re: Using property values in source code blocks
  2016-07-16  2:47                   ` Joon Ro
@ 2016-07-16 16:02                     ` Charles C. Berry
  2016-07-16 17:00                       ` Joon Ro
  0 siblings, 1 reply; 13+ messages in thread
From: Charles C. Berry @ 2016-07-16 16:02 UTC (permalink / raw)
  To: Joon Ro; +Cc: emacs-orgmode@gnu.org

On Fri, 15 Jul 2016, Joon Ro wrote:

>
>
>>
>> Example:
>>
>> --8<---------------cut here---------------start------------->8---
>>
>> * Top Heading
>>
>> #+NAME: get-property
>> #+BEGIN_SRC emacs-lisp :noweb yes :var prop="prop" :var pom=0
>> (car (org-entry-get-multivalued-property pom prop))
>> #+END_SRC
>>
>> ** Subheading
>> :PROPERTIES:
>> :DUMMY: 100
>> :END:
>>
>> #+BEGIN_SRC shell :noweb yes
>> echo <<get-property(prop="DUMMY",pom=(point))>>
>> #+END_SRC
>>
>> #+RESULTS:
>> : 100
>>
>> --8<---------------cut here---------------end--------------->8---
>>
>
> Hi Chuck,

> Thank you so much. It definitely works when I evaluate the code block - 
> but my ultimate goal is to tangle the source code (I want to generate 
> bunch of script files with a template using property values), and when I 
> tangle the code block it returns nil.

I do not see an easy way to do what you want only with tangle.

There are hooks you can define that might enable you to set things up. 
Maybe you can use `org-babel-pre-tangle-hook' to collect the property 
values and put them somewhere that `org-babel-post-tangle-hook' can get at 
them and use them to modify the tangled results accordingly. Or maybe 
`org-babel-post-tangle-hook' can do it by using methods like those in 
`org-babel-detangle' to look back at the *.org file.

Another alternative is to write all of your code blocks so they will 
execute to create the code you want as results and then tangle those 
results. You would use :wrap like this:

--8<---------------cut here---------------start------------->8---

#+BEGIN_SRC shell :noweb yes :wrap "src shell :tangle ./tangle-test.sh"
echo "echo " <<get-property(prop="DUMMY",pom=(point))>>
#+END_SRC

--8<---------------cut here---------------end--------------->8---

Execute the src block and then tangle the buffer.

In fact, if you go that route it might make more sense to write elisp 
code blocks to find the prpoerty values and then format the results.


Chuck

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

* Re: Using property values in source code blocks
  2016-07-16 16:02                     ` Charles C. Berry
@ 2016-07-16 17:00                       ` Joon Ro
  0 siblings, 0 replies; 13+ messages in thread
From: Joon Ro @ 2016-07-16 17:00 UTC (permalink / raw)
  To: Charles C. Berry; +Cc: emacs-orgmode@gnu.org

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

> I do not see an easy way to do what you want only with tangle.
> 
> There are hooks you can define that might enable you to set things up. 
> Maybe you can use `org-babel-pre-tangle-hook' to collect the property 
> values and put them somewhere that `org-babel-post-tangle-hook' can get at 
> them and use them to modify the tangled results accordingly. Or maybe 
> `org-babel-post-tangle-hook' can do it by using methods like those in 
> `org-babel-detangle' to look back at the *.org file.
> 
> Another alternative is to write all of your code blocks so they will 
> execute to create the code you want as results and then tangle those 
> results. You would use :wrap like this:
> 
> --8<---------------cut here---------------start------------->8---
> 
> #+BEGIN_SRC shell :noweb yes :wrap "src shell :tangle ./tangle-test.sh"
> echo "echo " <<get-property(prop="DUMMY",pom=(point))>>
> #+END_SRC
> 
> --8<---------------cut here---------------end--------------->8---
> 
> Execute the src block and then tangle the buffer.
> 
> In fact, if you go that route it might make more sense to write elisp 
> code blocks to find the prpoerty values and then format the results.
> 

I see - thank you so much for your help. It does look like I have to write some code to do this.
Since I'm not that familiar with elisp, I think I will write a Python code block which has the template as a multi-line string, and then just pass values as arguments to the source code block to generate the shell scripts.
Best,Joon

 		 	   		  

[-- Attachment #2: Type: text/html, Size: 2108 bytes --]

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

end of thread, other threads:[~2016-07-16 17:00 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-07-06  4:10 Using property values in source code blocks Joon Ro
2016-07-06 17:36 ` Charles C. Berry
2016-07-06 20:03   ` Joon Ro
2016-07-06 23:05     ` Charles C. Berry
2016-07-07  3:50       ` Joon Ro
2016-07-07 15:48         ` Charles C. Berry
2016-07-07 15:53           ` Joon Ro
2016-07-15 23:38             ` Joon Ro
2016-07-15 23:44               ` Joon Ro
2016-07-16  2:05                 ` Charles C. Berry
2016-07-16  2:47                   ` Joon Ro
2016-07-16 16:02                     ` Charles C. Berry
2016-07-16 17:00                       ` Joon Ro

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