* Org src blocks and multiline macros
@ 2018-08-02 6:33 Jarmo Hurri
2018-08-02 11:24 ` Kaushal Modi
0 siblings, 1 reply; 5+ messages in thread
From: Jarmo Hurri @ 2018-08-02 6:33 UTC (permalink / raw)
To: emacs-orgmode
Greetings.
I am once again facing a situation where I would like to define a
multiline org macro. If my memory is not failing me (it might well be),
this is not possible.
So this lead me to consider the use of Org source blocks in Babel. Which
might well be a discussion I have had here before, see the reference to
the state of my memory above.
1. Is it now possible to pass variables to these blocks?
2. If not, could we have this feature? This would give us immediate
multiline macros in the following style, where referring to the value
of the variable could be done macro-style.
#+HEADER: :var val1=foo
#+BEGIN_SRC org :var val2=" bar"
Currently this gives me nothing but {{{val1}}}{{{val2}}}.
This property would really help me structure some org documents in a
sensible way when common parts can be reused.
#+END_SRC
Jarmo
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Org src blocks and multiline macros
2018-08-02 6:33 Org src blocks and multiline macros Jarmo Hurri
@ 2018-08-02 11:24 ` Kaushal Modi
2018-08-02 12:32 ` Jarmo Hurri
0 siblings, 1 reply; 5+ messages in thread
From: Kaushal Modi @ 2018-08-02 11:24 UTC (permalink / raw)
To: Jarmo Hurri; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 801 bytes --]
Hello,
On Thu, Aug 2, 2018, 2:34 AM Jarmo Hurri <jarmo.hurri@iki.fi> wrote:
>
> Greetings.
>
> I am once again facing a situation where I would like to define a
> multiline org macro.
I'm not sure what that means, but based on your example a different
solution might work for you.
2. If not, could we have this feature? This would give us immediate
> multiline macros in the following style, where referring to the value
> of the variable could be done macro-style.
>
> #+HEADER: :var val1=foo
> #+BEGIN_SRC org :var val2=" bar"
> Currently this gives me nothing but {{{val1}}}{{{val2}}}.
> ...
>
Org macros don't get evaluated inside src blocks as far as I know. But
Noweb might help you. Look for this feature in Org manual. I use Noweb as
"macros for src blocks".
> --
Kaushal Modi
[-- Attachment #2: Type: text/html, Size: 1580 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Org src blocks and multiline macros
2018-08-02 11:24 ` Kaushal Modi
@ 2018-08-02 12:32 ` Jarmo Hurri
2018-08-02 16:50 ` Berry, Charles
0 siblings, 1 reply; 5+ messages in thread
From: Jarmo Hurri @ 2018-08-02 12:32 UTC (permalink / raw)
To: emacs-orgmode
>> I am once again facing a situation where I would like to define a
>> multiline org macro.
>
>
> I'm not sure what that means, but based on your example a different
> solution might work for you.
>
> 2. If not, could we have this feature? This would give us immediate
>> multiline macros in the following style, where referring to the value
>> of the variable could be done macro-style.
>>
>> #+HEADER: :var val1=foo
>> #+BEGIN_SRC org :var val2=" bar"
>> Currently this gives me nothing but {{{val1}}}{{{val2}}}.
>> ...
>>
>
> Org macros don't get evaluated inside src blocks as far as I know. But
> Noweb might help you. Look for this feature in Org manual. I use Noweb as
> "macros for src blocks".
Yep, that will give me something similar:
#+name: val1
#+BEGIN_SRC org :exports none
foo
#+END_SRC
#+name: val2
#+BEGIN_SRC org :exports none
bar
#+END_SRC
#+BEGIN_SRC org :noweb yes
Currently this gives me ~<<val1>><<val2>>~ indeed!
#+END_SRC
Compared to multiline macros, though, having to (re)define bunch of src
blocks for variables does seem like an overkill.
BTW, does anyone know how I could enforce the standard
Org-interpretation of '~' in the resulting, exported Org.
Jarmo
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Org src blocks and multiline macros
2018-08-02 12:32 ` Jarmo Hurri
@ 2018-08-02 16:50 ` Berry, Charles
2018-08-03 6:16 ` Jarmo Hurri
0 siblings, 1 reply; 5+ messages in thread
From: Berry, Charles @ 2018-08-02 16:50 UTC (permalink / raw)
To: Jarmo Hurri; +Cc: emacs-orgmode@gnu.org
> On Aug 2, 2018, at 5:32 AM, Jarmo Hurri <jarmo.hurri@iki.fi> wrote:
>
<Kaushal Modi replying here>
>>
>> Org macros don't get evaluated inside src blocks as far as I know. But
>> Noweb might help you. Look for this feature in Org manual. I use Noweb as
>> "macros for src blocks".
>
Me, too. But maybe Jarmo just wants a template for some text element. ??
> Yep, that will give me something similar:
>
> #+name: val1
> #+BEGIN_SRC org :exports none
> foo
> #+END_SRC
>
> #+name: val2
> #+BEGIN_SRC org :exports none
> bar
> #+END_SRC
>
> #+BEGIN_SRC org :noweb yes
> Currently this gives me ~<<val1>><<val2>>~ indeed!
> #+END_SRC
>
> Compared to multiline macros, though, having to (re)define bunch of src
> blocks for variables does seem like an overkill.
>
> BTW, does anyone know how I could enforce the standard
> Org-interpretation of '~' in the resulting, exported Org.
>
> Jarmo
There are some limitations on noweb expansion. C-c C-v C-v in a src block will show you what the expansion looks like and how :var args are handled.
ob-org does not provide for variables AFAICS. But using emacs-lisp with a :results drawer to render the output as org should help:
--8<---------------cut here---------------start------------->8---
#+header: :results drawer :exports results
#+begin_src emacs-lisp :var val1="foo" :var val2="bar"
(concat "Currently this gives me ~" val1 val2 "~ indeed!")
#+END_SRC
#+RESULTS:
:results:
Currently this gives me ~foobar~ indeed!
:end:
--8<---------------cut here---------------end--------------->8---
BTW, if you really do need a long or multiline MACRO, there is the possibility of using an `eval' style macro with a custom elisp function. e.g.
#+MACRO: longish-macro (eval (my-really-long-macro-def $1 $2 $3))
See
(info "(org) Macro Replacement")
But this carries the burden of having to defun `my-really-long-macro-def' before exporting your document.
HTH,
Chuck
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Org src blocks and multiline macros
2018-08-02 16:50 ` Berry, Charles
@ 2018-08-03 6:16 ` Jarmo Hurri
0 siblings, 0 replies; 5+ messages in thread
From: Jarmo Hurri @ 2018-08-03 6:16 UTC (permalink / raw)
To: emacs-orgmode
"Berry, Charles" <ccberry@ucsd.edu> writes:
> ob-org does not provide for variables AFAICS.
I have been trying to advocate for multiline macros for years, because
those are the _only_ thing I am really missing in org, again and
again. For example, right now I am writing teaching material where I
need to reuse certain parametrised org snippets across multiple files. I
_could_ use noweb. I _could_ use elisp code. But having a tool similar
to \newcommand in Latex would be so useful that every once in a while I
find myself bringing it up again.
The latest idea was to introduce variables into org blocks in hope it
would implement the desired functionality
> But using emacs-lisp with a :results drawer to render the output as
> org should help:
>
> #+header: :results drawer :exports results
> #+begin_src emacs-lisp :var val1="foo" :var val2="bar"
> (concat "Currently this gives me ~" val1 val2 "~ indeed!")
> #+END_SRC
>
> #+RESULTS:
> :results:
> Currently this gives me ~foobar~ indeed!
> :end:
Ok, this will work too. But my real document string contains multiple
paragraphs, org lists etc. In terms of readability and maintainability,
embedding that text as an argument in an elisp call is not a very clean
solution.
> BTW, if you really do need a long or multiline MACRO, there is the
> possibility of using an `eval' style macro with a custom elisp
> function. e.g.
>
> #+MACRO: longish-macro (eval (my-really-long-macro-def $1 $2 $3))
>
> (info "(org) Macro Replacement")
>
> But this carries the burden of having to defun
> `my-really-long-macro-def' before exporting your document.
Yes, this too will work, with similar reservations as in the previous
case.
Thanks for all the tips!
Jarmo
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2018-08-03 6:16 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2018-08-02 6:33 Org src blocks and multiline macros Jarmo Hurri
2018-08-02 11:24 ` Kaushal Modi
2018-08-02 12:32 ` Jarmo Hurri
2018-08-02 16:50 ` Berry, Charles
2018-08-03 6:16 ` Jarmo Hurri
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).