Re: [O] Injecting properties with noweb

2023-04-08 Thread Ken Mankoff
Hi Ihor,

I had posted this in another thread, but repeat it here for anyone interested. 
I think it is a similar end result to what you posted, but skips the =identity= 
 step. The examples below show (1) setting a bash environment variable in 
screen, or (2) printing from a Python prompt after sshing to a remote computer. 
It is language agnostic. Because it uses PROPERTIES and not :var, it also lets 
me work in Org Column View mode.


* Header
:PROPERTIES:
:foo: 42
:END:

#+NAME: ex1-screen-bash
#+BEGIN_SRC screen

export foo="<>"
#+END_SRC

#+NAME: ex2-ssh-python
#+BEGIN_SRC bash

ssh somewhere
python
print("<>")
#+END_SRC

#+CALL: ex2-ssh-python()

#+RESULTS:
: foo


The relevant section from my library-of-babel is:

* Properties into header args
:PROPERTIES:
:hellomessage: hello
:END:

https://emacs.stackexchange.com/questions/41922/

#+NAME: get_property
#+BEGIN_SRC emacs-lisp :var prop_name="" :results silent

(org-with-point-at org-babel-current-src-block-location
  (org-entry-get nil prop_name t))
#+END_SRC

** Example Usage

*** Header arg

#+HEADER: :var prop_message=(org-entry-get nil "hellomessage" t)
#+BEGIN_SRC emacs-lisp
  (message prop_message)
#+END_SRC

#+RESULTS:
: hello

*** Noweb

#+BEGIN_SRC emacs-lisp :noweb yes
  (message "<>")
#+END_SRC


#+RESULTS:
: hello

#+BEGIN_SRC bash :noweb yes :results verbatim
echo "<>"
#+END_SRC

#+RESULTS:
: hello

If hope this helps someone if they need it.

  -k.




On 2023-04-08 at 05:22 -07, Ihor Radchenko  wrote...
> Ken Mankoff  writes:
>
>> Is it possible to set variables using Org Babel inside screen, which
>> does not support ":var" header args? I'd actually lke a double-nested
>> screen over ssh, and the ability to re-use Babel code blocks under
>> different headings, using header args or PROPERTIES to change
>> variables. That is, something like:
>
> Yes.
>
>> ...
>> #+NAME: get-prop
>> #+BEGIN_SRC emacs-lisp :var prop="FOO" :noweb yes
>> (org-macro--get-property prop "")
>> #+END_SRC
>>
>> #+NAME: inject_vars
>> #+BEGIN_SRC shell :noweb yes
>> # echo <> # testing
>> echo export FOO=<>
>> echo export BAR=<>
>> echo export BAZ=<>
>> #+END_SRC
>
> This did not work as you expected because noweb evaluates code block
> with point at that code block.
>
> To get the property value at the code block where you expand noweb
> reference, you need to compute the value in the arguments to the
> reference. Something like
>
> #+name: identity
> #+begin_src elisp :var x=""
>
> x
> #+end_src
>
> ...
> echo export FOO=< ...




Re: [O] Injecting properties with noweb

2023-04-08 Thread Ihor Radchenko
Ken Mankoff  writes:

> Is it possible to set variables using Org Babel inside screen, which does not 
> support ":var" header args? I'd actually lke a double-nested screen over ssh, 
> and the ability to re-use Babel code blocks under different headings, using 
> header args or PROPERTIES to change variables. That is, something like:

Yes.

> ...
> #+NAME: get-prop
> #+BEGIN_SRC emacs-lisp :var prop="FOO" :noweb yes
> (org-macro--get-property prop "")
> #+END_SRC
>
> #+NAME: inject_vars
> #+BEGIN_SRC shell :noweb yes
> # echo <> # testing
> echo export FOO=<>
> echo export BAR=<>
> echo export BAZ=<>
> #+END_SRC

This did not work as you expected because noweb evaluates code block
with point at that code block.

To get the property value at the code block where you expand noweb
reference, you need to compute the value in the arguments to the
reference. Something like

#+name: identity
#+begin_src elisp :var x=""
x
#+end_src

...
echo export FOO=.
Support Org development at ,
or support my work at 



[O] Injecting properties with noweb

2023-02-20 Thread Ken Mankoff
Hello,

Is it possible to set variables using Org Babel inside screen, which does not 
support ":var" header args? I'd actually lke a double-nested screen over ssh, 
and the ability to re-use Babel code blocks under different headings, using 
header args or PROPERTIES to change variables. That is, something like:

* Setup
:SETTINGS:
:FOO: default
:BAR: one
:header-args:screen+: :cmd /bin/bash :session (org-macro--get-property "FOO" "")
:END:

#+NAME: setup
#+BEGIN_SRC screen
if [[ ! $(hostname) =~ "host"* ]]; then ssh host; fi
<>
# eval <> ??
echo $FOO
#+END_SRC

Should print out "one" (the default setting under Setup) in the screen terminal.

** OTHER
:PROPERTIES:
:FOO: two
:END:

#+BEGIN_SRC screen
<>
#+END_SRC

Should print out "two" (the sub-heding adjusted property) in the screen 
terminal.


I'm OK with not being able to inject arbitrary variables, only the ~10 or so 
that I need to be able to set, and having a code block that has these 10 
hard-coded on the LHS, but with some <> or something on the RHS so that 
they value of the variable can be controlled using PROPERTIES under headings 
(better yet, header-args, but I don't think that is possible).  

That's the behavior I'm after, but am having trouble. I thought something like:

#+NAME: get-prop
#+BEGIN_SRC emacs-lisp :var prop="FOO" :noweb yes
(org-macro--get-property prop "")
#+END_SRC

#+NAME: inject_vars
#+BEGIN_SRC shell :noweb yes
# echo <> # testing
echo export FOO=<>
echo export BAR=<>
echo export BAZ=<>
#+END_SRC

#+BEGIN_SRC screen
<>
echo $FOO
#+END_SRC

might work, but it's just printing nil.


Thanks for any suggestions,

  -k.