From: Ken Mankoff <mankoff@gmail.com>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: Org-mode <emacs-orgmode@gnu.org>
Subject: Re: [O] Injecting properties with noweb
Date: Sat, 08 Apr 2023 08:01:17 -0700 [thread overview]
Message-ID: <87355a2zb6.fsf@t480.home> (raw)
In-Reply-To: <87h6tqk1hg.fsf@localhost>
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="<<get_property("foo")>>"
#+END_SRC
#+NAME: ex2-ssh-python
#+BEGIN_SRC bash
ssh somewhere
python
print("<<get_property("foo")>>")
#+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 "<<get_property("hellomessage")>>")
#+END_SRC
#+RESULTS:
: hello
#+BEGIN_SRC bash :noweb yes :results verbatim
echo "<<get_property("hellomessage")>>"
#+END_SRC
#+RESULTS:
: hello
If hope this helps someone if they need it.
-k.
On 2023-04-08 at 05:22 -07, Ihor Radchenko <yantar92@posteo.net> wrote...
> Ken Mankoff <mankoff@gmail.com> 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 <<get-prop(prop="FOO")>> # testing
>> echo export FOO=<<get-prop(prop="FOO")>>
>> echo export BAR=<<get-prop(prop="BAR")>>
>> echo export BAZ=<<get-prop(prop="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=<<identity(x=(org-macro--get-property prop "FOO"))
> ...
prev parent reply other threads:[~2023-04-08 15:02 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-20 16:07 [O] Injecting properties with noweb Ken Mankoff
2023-04-08 12:22 ` Ihor Radchenko
2023-04-08 15:01 ` Ken Mankoff [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.orgmode.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87355a2zb6.fsf@t480.home \
--to=mankoff@gmail.com \
--cc=emacs-orgmode@gnu.org \
--cc=yantar92@posteo.net \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).