emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* org-babel source block unevaluated into variable?
@ 2017-10-25 14:21 Johan W. Klüwer
  2017-10-25 15:36 ` Martin Alsinet
  0 siblings, 1 reply; 6+ messages in thread
From: Johan W. Klüwer @ 2017-10-25 14:21 UTC (permalink / raw)
  To: emacs-orgmode

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

Is there a way to assign the uninterpreted content of an executable source
block to a variable? Preferably, using a :var header argument? That is,
return the text in the block, not the result of evaluating it, and
preferably with noweb references expanded.

"example" blocks return text the way I want, but they can't be evaluated,
and of course noweb is ruled out for them.

The function org-babel-ref-resolve could to the job if there were a switch
to block evaluation.


Why this is interesting: I wish to use url-hexify-string on the text of a
named SPARQL query.

Cheers, Johan

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

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

* Re: org-babel source block unevaluated into variable?
  2017-10-25 14:21 org-babel source block unevaluated into variable? Johan W. Klüwer
@ 2017-10-25 15:36 ` Martin Alsinet
  2017-10-25 15:52   ` Martin Alsinet
  0 siblings, 1 reply; 6+ messages in thread
From: Martin Alsinet @ 2017-10-25 15:36 UTC (permalink / raw)
  To: Johan W. Klüwer, emacs-orgmode

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

Johan:

You can try the following:

#+NAME: lscode
#+BEGIN_ASCII
ls -alh
#+END_ASCII

#+BEGIN_SRC emacs-lisp :var code=lscode
(message code)
#+END_SRC

#+RESULTS:
: ls -alh

I haven't tried the noweb references, but it does return the code block in
the variable.


Martín

On Wed, Oct 25, 2017 at 9:22 AM Johan W. Klüwer <johan.w.kluwer@gmail.com>
wrote:

> Is there a way to assign the uninterpreted content of an executable source
> block to a variable? Preferably, using a :var header argument? That is,
> return the text in the block, not the result of evaluating it, and
> preferably with noweb references expanded.
>
> "example" blocks return text the way I want, but they can't be evaluated,
> and of course noweb is ruled out for them.
>
> The function org-babel-ref-resolve could to the job if there were a switch
> to block evaluation.
>
>
> Why this is interesting: I wish to use url-hexify-string on the text of a
> named SPARQL query.
>
> Cheers, Johan
>

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

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

* Re: org-babel source block unevaluated into variable?
  2017-10-25 15:36 ` Martin Alsinet
@ 2017-10-25 15:52   ` Martin Alsinet
  2017-10-26 11:17     ` Johan W. Klüwer
  0 siblings, 1 reply; 6+ messages in thread
From: Martin Alsinet @ 2017-10-25 15:52 UTC (permalink / raw)
  To: Johan W. Klüwer, emacs-orgmode

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

Johan:

To use expanded noweb references you can use text source blocks

#+NAME: lscode
#+BEGIN_SRC *text*
ls -alh
#+END_SRC


#+NAME: example
#+BEGIN_SRC sh :noweb yes
echo <<lscode>>
#+END_SRC

#+RESULTS: example
: ls -alh


#+BEGIN_SRC emacs-lisp :var code=example
(message code)
#+END_SRC

#+RESULTS:
: ls -alh


Martín

On Wed, Oct 25, 2017 at 10:36 AM Martin Alsinet <martin@alsinet.com.ar>
wrote:

> Johan:
>
> You can try the following:
>
> #+NAME: lscode
> #+BEGIN_ASCII
> ls -alh
> #+END_ASCII
>
> #+BEGIN_SRC emacs-lisp :var code=lscode
> (message code)
> #+END_SRC
>
> #+RESULTS:
> : ls -alh
>
> I haven't tried the noweb references, but it does return the code block in
> the variable.
>
>
> Martín
>
> On Wed, Oct 25, 2017 at 9:22 AM Johan W. Klüwer <johan.w.kluwer@gmail.com>
> wrote:
>
>> Is there a way to assign the uninterpreted content of an executable
>> source block to a variable? Preferably, using a :var header argument? That
>> is, return the text in the block, not the result of evaluating it, and
>> preferably with noweb references expanded.
>>
>> "example" blocks return text the way I want, but they can't be evaluated,
>> and of course noweb is ruled out for them.
>>
>> The function org-babel-ref-resolve could to the job if there were a
>> switch to block evaluation.
>>
>>
>> Why this is interesting: I wish to use url-hexify-string on the text of a
>> named SPARQL query.
>>
>> Cheers, Johan
>>
>

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

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

* Re: org-babel source block unevaluated into variable?
  2017-10-25 15:52   ` Martin Alsinet
@ 2017-10-26 11:17     ` Johan W. Klüwer
  2017-10-26 12:02       ` Johan W. Klüwer
  2017-10-26 12:06       ` Martin Alsinet
  0 siblings, 2 replies; 6+ messages in thread
From: Johan W. Klüwer @ 2017-10-26 11:17 UTC (permalink / raw)
  To: Martin Alsinet; +Cc: emacs-orgmode

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

Thanks Martin,

These are good suggestions, but it's not quite what I am after. In your
second example, I would like ":var code=example" to make "code" carry the
full (and expanded) text of the "example" block, i.e. to have

  echo ls -alh

as the result -- the code itself, unevaluated.

Johan

2017-10-25 17:52 GMT+02:00 Martin Alsinet <martin@alsinet.com.ar>:

> Johan:
>
> To use expanded noweb references you can use text source blocks
>
> #+NAME: lscode
> #+BEGIN_SRC *text*
> ls -alh
> #+END_SRC
>
>
> #+NAME: example
> #+BEGIN_SRC sh :noweb yes
> echo <<lscode>>
> #+END_SRC
>
> #+RESULTS: example
> : ls -alh
>
>
> #+BEGIN_SRC emacs-lisp :var code=example
> (message code)
> #+END_SRC
>
> #+RESULTS:
> : ls -alh
>
>
> Martín
>
> On Wed, Oct 25, 2017 at 10:36 AM Martin Alsinet <martin@alsinet.com.ar>
> wrote:
>
>> Johan:
>>
>> You can try the following:
>>
>> #+NAME: lscode
>> #+BEGIN_ASCII
>> ls -alh
>> #+END_ASCII
>>
>> #+BEGIN_SRC emacs-lisp :var code=lscode
>> (message code)
>> #+END_SRC
>>
>> #+RESULTS:
>> : ls -alh
>>
>> I haven't tried the noweb references, but it does return the code block
>> in the variable.
>>
>>
>> Martín
>>
>> On Wed, Oct 25, 2017 at 9:22 AM Johan W. Klüwer <johan.w.kluwer@gmail.com>
>> wrote:
>>
>>> Is there a way to assign the uninterpreted content of an executable
>>> source block to a variable? Preferably, using a :var header argument? That
>>> is, return the text in the block, not the result of evaluating it, and
>>> preferably with noweb references expanded.
>>>
>>> "example" blocks return text the way I want, but they can't be
>>> evaluated, and of course noweb is ruled out for them.
>>>
>>> The function org-babel-ref-resolve could to the job if there were a
>>> switch to block evaluation.
>>>
>>>
>>> Why this is interesting: I wish to use url-hexify-string on the text of
>>> a named SPARQL query.
>>>
>>> Cheers, Johan
>>>
>>

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

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

* Re: org-babel source block unevaluated into variable?
  2017-10-26 11:17     ` Johan W. Klüwer
@ 2017-10-26 12:02       ` Johan W. Klüwer
  2017-10-26 12:06       ` Martin Alsinet
  1 sibling, 0 replies; 6+ messages in thread
From: Johan W. Klüwer @ 2017-10-26 12:02 UTC (permalink / raw)
  To: Martin Alsinet; +Cc: emacs-orgmode

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

Following up: A function like this one should help.

(defun expand-named-babel-block (block)
    (save-excursion
    (org-babel-goto-named-src-block block)
    (org-babel-expand-src-block)))

However ... there's something strange here with
org-babel-goto-named-src-block (org 9.0.9). It just jumps to the first
source block in the file, which is not that useful :) I'll try to update
org and see.

2017-10-26 13:17 GMT+02:00 Johan W. Klüwer <johan.w.kluwer@gmail.com>:

> Thanks Martin,
>
> These are good suggestions, but it's not quite what I am after. In your
> second example, I would like ":var code=example" to make "code" carry the
> full (and expanded) text of the "example" block, i.e. to have
>
>   echo ls -alh
>
> as the result -- the code itself, unevaluated.
>
> Johan
>
> 2017-10-25 17:52 GMT+02:00 Martin Alsinet <martin@alsinet.com.ar>:
>
>> Johan:
>>
>> To use expanded noweb references you can use text source blocks
>>
>> #+NAME: lscode
>> #+BEGIN_SRC *text*
>> ls -alh
>> #+END_SRC
>>
>>
>> #+NAME: example
>> #+BEGIN_SRC sh :noweb yes
>> echo <<lscode>>
>> #+END_SRC
>>
>> #+RESULTS: example
>> : ls -alh
>>
>>
>> #+BEGIN_SRC emacs-lisp :var code=example
>> (message code)
>> #+END_SRC
>>
>> #+RESULTS:
>> : ls -alh
>>
>>
>> Martín
>>
>> On Wed, Oct 25, 2017 at 10:36 AM Martin Alsinet <martin@alsinet.com.ar>
>> wrote:
>>
>>> Johan:
>>>
>>> You can try the following:
>>>
>>> #+NAME: lscode
>>> #+BEGIN_ASCII
>>> ls -alh
>>> #+END_ASCII
>>>
>>> #+BEGIN_SRC emacs-lisp :var code=lscode
>>> (message code)
>>> #+END_SRC
>>>
>>> #+RESULTS:
>>> : ls -alh
>>>
>>> I haven't tried the noweb references, but it does return the code block
>>> in the variable.
>>>
>>>
>>> Martín
>>>
>>> On Wed, Oct 25, 2017 at 9:22 AM Johan W. Klüwer <
>>> johan.w.kluwer@gmail.com> wrote:
>>>
>>>> Is there a way to assign the uninterpreted content of an executable
>>>> source block to a variable? Preferably, using a :var header argument? That
>>>> is, return the text in the block, not the result of evaluating it, and
>>>> preferably with noweb references expanded.
>>>>
>>>> "example" blocks return text the way I want, but they can't be
>>>> evaluated, and of course noweb is ruled out for them.
>>>>
>>>> The function org-babel-ref-resolve could to the job if there were a
>>>> switch to block evaluation.
>>>>
>>>>
>>>> Why this is interesting: I wish to use url-hexify-string on the text of
>>>> a named SPARQL query.
>>>>
>>>> Cheers, Johan
>>>>
>>>
>

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

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

* Re: org-babel source block unevaluated into variable?
  2017-10-26 11:17     ` Johan W. Klüwer
  2017-10-26 12:02       ` Johan W. Klüwer
@ 2017-10-26 12:06       ` Martin Alsinet
  1 sibling, 0 replies; 6+ messages in thread
From: Martin Alsinet @ 2017-10-26 12:06 UTC (permalink / raw)
  To: Johan W. Klüwer; +Cc: emacs-orgmode

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

Johan:

Maybe this one would work for you:

#+NAME: first
#+BEGIN_SRC js
function one() {
    return 1;
}
#+END_SRC


#+NAME: second
#+BEGIN_SRC js
function two() {
    return 2;
}
#+END_SRC


#+NAME: third
#+BEGIN_SRC js
function three() {
    return 3;
}
#+END_SRC


#+NAME: all
#+BEGIN_SRC sh :results output :noweb yes
echo "
<<second>>

<<first>>

function test(){ console.log('test');}

<<third>>"
#+END_SRC



#+BEGIN_SRC python :results output :var code=all
print code
#+END_SRC

#+RESULTS:
#+begin_example

function two() {
    return 2
}

function one() {
    return 1;
}

function test(){ console.log('test');}

function three() {
    return 3;
}

#+end_example


On Thu, Oct 26, 2017 at 6:17 AM Johan W. Klüwer <johan.w.kluwer@gmail.com>
wrote:

> Thanks Martin,
>
> These are good suggestions, but it's not quite what I am after. In your
> second example, I would like ":var code=example" to make "code" carry the
> full (and expanded) text of the "example" block, i.e. to have
>
>   echo ls -alh
>
> as the result -- the code itself, unevaluated.
>
> Johan
>
> 2017-10-25 17:52 GMT+02:00 Martin Alsinet <martin@alsinet.com.ar>:
>
>> Johan:
>>
>> To use expanded noweb references you can use text source blocks
>>
>> #+NAME: lscode
>> #+BEGIN_SRC *text*
>> ls -alh
>> #+END_SRC
>>
>>
>> #+NAME: example
>> #+BEGIN_SRC sh :noweb yes
>> echo <<lscode>>
>> #+END_SRC
>>
>> #+RESULTS: example
>> : ls -alh
>>
>>
>> #+BEGIN_SRC emacs-lisp :var code=example
>> (message code)
>> #+END_SRC
>>
>> #+RESULTS:
>> : ls -alh
>>
>>
>> Martín
>>
>> On Wed, Oct 25, 2017 at 10:36 AM Martin Alsinet <martin@alsinet.com.ar>
>> wrote:
>>
>>> Johan:
>>>
>>> You can try the following:
>>>
>>> #+NAME: lscode
>>> #+BEGIN_ASCII
>>> ls -alh
>>> #+END_ASCII
>>>
>>> #+BEGIN_SRC emacs-lisp :var code=lscode
>>> (message code)
>>> #+END_SRC
>>>
>>> #+RESULTS:
>>> : ls -alh
>>>
>>> I haven't tried the noweb references, but it does return the code block
>>> in the variable.
>>>
>>>
>>> Martín
>>>
>>> On Wed, Oct 25, 2017 at 9:22 AM Johan W. Klüwer <
>>> johan.w.kluwer@gmail.com> wrote:
>>>
>>>> Is there a way to assign the uninterpreted content of an executable
>>>> source block to a variable? Preferably, using a :var header argument? That
>>>> is, return the text in the block, not the result of evaluating it, and
>>>> preferably with noweb references expanded.
>>>>
>>>> "example" blocks return text the way I want, but they can't be
>>>> evaluated, and of course noweb is ruled out for them.
>>>>
>>>> The function org-babel-ref-resolve could to the job if there were a
>>>> switch to block evaluation.
>>>>
>>>>
>>>> Why this is interesting: I wish to use url-hexify-string on the text of
>>>> a named SPARQL query.
>>>>
>>>> Cheers, Johan
>>>>
>>>
>

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

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

end of thread, other threads:[~2017-10-26 12:06 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-25 14:21 org-babel source block unevaluated into variable? Johan W. Klüwer
2017-10-25 15:36 ` Martin Alsinet
2017-10-25 15:52   ` Martin Alsinet
2017-10-26 11:17     ` Johan W. Klüwer
2017-10-26 12:02       ` Johan W. Klüwer
2017-10-26 12:06       ` Martin Alsinet

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