emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* macro replacement in code block?
@ 2017-10-31 23:13 David Rogoff
  2017-11-01  0:32 ` Kaushal Modi
  0 siblings, 1 reply; 8+ messages in thread
From: David Rogoff @ 2017-10-31 23:13 UTC (permalink / raw)
  To: emacs-orgmode

Hi.

Trying to set up a large orgmode file with source code for several files that is extracted using tangle and also exported to HTML for documentation (source is verilog-mode).  It works but I’m trying to clean it up by using orgmode macros.  However, it seems like these are not replaced when inside code blocks.  I found a couple of references to this by searching Google but they were very terse and also referenced from orgmode function that doesn’t exist.  Can anyone point me to some documentation or example?

Thanks,

 David 

using gnu emacs 25.2 Linux / orgmode 9.1.2

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

* Re: macro replacement in code block?
  2017-10-31 23:13 macro replacement in code block? David Rogoff
@ 2017-11-01  0:32 ` Kaushal Modi
  2017-11-01  3:21   ` David Rogoff
  0 siblings, 1 reply; 8+ messages in thread
From: Kaushal Modi @ 2017-11-01  0:32 UTC (permalink / raw)
  To: David Rogoff; +Cc: emacs-org list

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

On Tue, Oct 31, 2017, 7:14 PM David Rogoff <david@therogoffs.com> wrote:

> Hi.
>

Hello.

 I’m trying to clean it up by using orgmode macros.  However, it seems like
> these are not replaced when inside code blocks.
>

Do you have a minimal example of what you'd like to "macrofy" in the
verilog code blocks?

Probably the solution is to use noweb references with arguments? See the
Org manual.

> --

Kaushal Modi

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

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

* Re: macro replacement in code block?
  2017-11-01  0:32 ` Kaushal Modi
@ 2017-11-01  3:21   ` David Rogoff
  2017-11-01 15:51     ` Kaushal Modi
  0 siblings, 1 reply; 8+ messages in thread
From: David Rogoff @ 2017-11-01  3:21 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: emacs-org list

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



> On Oct 31, 2017, at 5:32 PM, Kaushal Modi <kaushal.modi@gmail.com> wrote:
> 
> On Tue, Oct 31, 2017, 7:14 PM David Rogoff <david@therogoffs.com <mailto:david@therogoffs.com>> wrote:
> Hi.
> 
> Hello. 
> 
>  I’m trying to clean it up by using orgmode macros.  However, it seems like these are not replaced when inside code blocks. 
> 
> Do you have a minimal example of what you'd like to "macrofy" in the verilog code blocks?

Pretty simple text substitution.  Something like this:

    #+MACRO: SEC_HDR1 // ---------------------------
    #+MACRO: SEC_HDR2 // 

    #+MACRO: SEC_TITLE This section declares local types and constants

    * File 1
    ** Section 1: {{{SEC_TITLE}}}
        #+BEGIN_SRC verilog
            {{{SEC_HDR1}}}
            {{{SEC_HDR2}}} {{{SEC_TITLE}}}
            parameter A = 1;
        #+END_SRC

So SEC_TITLE expands in the headline and also as a comment in the tangled verilog code.
It would be really great to be able to embed newlines in the macro to it could be one line / macro in the src block.

> 
> Probably the solution is to use noweb references with arguments? See the Org manual. 

Thanks - I’ll have to look into noweb.  I’m not familiar with it.

 David


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

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

* Re: macro replacement in code block?
  2017-11-01  3:21   ` David Rogoff
@ 2017-11-01 15:51     ` Kaushal Modi
  2017-11-01 16:04       ` Multi-line noweb expansion in elisp insert form (Was Re: macro replacement in code block?) Kaushal Modi
  2017-11-01 16:17       ` macro replacement in code block? Kaushal Modi
  0 siblings, 2 replies; 8+ messages in thread
From: Kaushal Modi @ 2017-11-01 15:51 UTC (permalink / raw)
  To: David Rogoff; +Cc: emacs-org list

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

On Tue, Oct 31, 2017 at 11:21 PM David Rogoff <david@therogoffs.com> wrote:

> Thanks - I’ll have to look into noweb.  I’m not familiar with it.
>

This turned out to be much complicated than I expected..

I still don't have a fully working solution (see below). Hopefully someone
can help figure out the reason for "Invalid read syntax: #" error.

Below example Org file will work right away. But it will fail with the
above error the moment second line (for more macros) is added to org-macros
block.

=====
1. Eval the =org-macros= block once (=C-c C-c=)
   - Always use that =org-macros= block to define the Org macros for this
file.
   - Hitting =C-c C-c= will insert those macros in this file.
   - This block is reused as a noweb-ref in other blocks.. so keeping just
this
     block as a source for all Org macros will be convenient.
2. Export this file (=C-c C-e h o=)

#+BEGIN_SRC org :noweb-ref org-macros :results output replace :exports none
,#+MACRO: SEC_FOO Foo Topic
#+END_SRC

#+NAME: ehdr
#+BEGIN_SRC emacs-lisp :var macro="n" :noweb yes :results raw :exports none
(with-temp-buffer
  (insert "<<org-macros>>\n")
  (let ((start (point)))
    (insert "// ---------------------------\n")
    (insert (concat "// {{{" macro "}}}"))
    (org-mode)
    (org-macro-replace-all
     (append org-macro-templates org-export-global-macros))
    (buffer-substring-no-properties start (point-max))))
#+END_SRC
** TODO Fix Invalid read syntax: "#"
This solution /almost/ works.

If the content of the =org-macros= block is changed from:
#+BEGIN_SRC org
,#+MACRO: SEC_FOO Foo Topic
#+END_SRC

To:
#+BEGIN_SRC org
,#+MACRO: SEC_FOO Foo Topic
,#+MACRO: SEC_BAR Bar Topic
#+END_SRC

We get this error:
#+BEGIN_EXAMPLE
Invalid read syntax: "#"
#+END_EXAMPLE

Looks like that happens due to ~(insert "<<org-macros>>\n")~ when the
~org-macros~ ~noweb-ref~ has multiple lines. But I cannot figure out why
having
multiple lines is a problem.
** Section 1: {{{SEC_FOO}}}
#+BEGIN_SRC verilog :noweb yes
<<ehdr(macro="SEC_FOO")>>
parameter A = 1;
#+END_SRC
** COMMENT Section 2: {{{SEC_BAR}}}
#+BEGIN_SRC verilog :noweb yes
<<ehdr(macro="SEC_BAR")>>
parameter A = 1;
#+END_SRC
=====

-- 

Kaushal Modi

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

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

* Multi-line noweb expansion in elisp insert form (Was Re: macro replacement in code block?)
  2017-11-01 15:51     ` Kaushal Modi
@ 2017-11-01 16:04       ` Kaushal Modi
  2017-11-01 16:17       ` macro replacement in code block? Kaushal Modi
  1 sibling, 0 replies; 8+ messages in thread
From: Kaushal Modi @ 2017-11-01 16:04 UTC (permalink / raw)
  To: emacs-org list

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

Hello all,

While trying to come up with a solution for this problem[1], I ended up
with a new problem :)

On Wed, Nov 1, 2017 at 11:51 AM Kaushal Modi <kaushal.modi@gmail.com> wrote:

> I still don't have a fully working solution (see below). Hopefully someone
> can help figure out the reason for "Invalid read syntax: #" error.
>
> Below example Org file will work right away. But it will fail with the
> above error the moment second line (for more macros) is added to org-macros
> block.
>

Here's a minimal working example to recreate that specific problem:

- The problem is seen when you C-c C-c the "noworky" block.

=====

#+BEGIN_SRC org :noweb-ref org-macros-1-line :results output replace
:exports none
,#+MACRO: AA 1
#+END_SRC

#+RESULTS:
#+MACRO: AA 1

#+BEGIN_SRC org :noweb-ref org-macros-2-lines :results output replace
:exports none
,#+MACRO: BB 2
,#+MACRO: CC 3
#+END_SRC

#+RESULTS:
#+MACRO: BB 2
#+MACRO: CC 3

* This works
#+NAME: works
#+BEGIN_SRC emacs-lisp :var macro="AA" :noweb yes :results raw :exports none
(with-temp-buffer
  (insert "<<org-macros-1-line>>\n")
  (insert (concat "{{{" macro "}}}"))
  (org-mode)
  (org-macro-replace-all
   (append org-macro-templates org-export-global-macros))
  (buffer-substring-no-properties (point-min) (point-max)))
#+END_SRC
#+CALL: works()

* This does not work
#+NAME: noworky
#+BEGIN_SRC emacs-lisp :var macro="BB" :noweb yes :results raw :exports none
(with-temp-buffer
  (insert "<<org-macros-2-lines>>\n")
  (insert "// ---------------------------\n")
  (insert (concat "{{{" macro "}}}"))
  (org-mode)
  (org-macro-replace-all
   (append org-macro-templates org-export-global-macros))
  (buffer-substring-no-properties (point-min) (point-max)))
#+END_SRC

If you uncomment the below =#+CALL= line, you will get:
#+BEGIN_EXAMPLE
org-babel-execute:emacs-lisp: Invalid read syntax: "#"
#+END_EXAMPLE

when you try to export (or even if you =C-c C-c= that =noworky= block.

# #+CALL: noworky()
=====

[1]: http://lists.gnu.org/archive/html/emacs-orgmode/2017-11/msg00007.html

-- 

Kaushal Modi

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

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

* Re: macro replacement in code block?
  2017-11-01 15:51     ` Kaushal Modi
  2017-11-01 16:04       ` Multi-line noweb expansion in elisp insert form (Was Re: macro replacement in code block?) Kaushal Modi
@ 2017-11-01 16:17       ` Kaushal Modi
  2017-11-01 17:32         ` David Rogoff
  1 sibling, 1 reply; 8+ messages in thread
From: Kaushal Modi @ 2017-11-01 16:17 UTC (permalink / raw)
  To: David Rogoff; +Cc: emacs-org list

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

On Wed, Nov 1, 2017 at 11:51 AM Kaushal Modi <kaushal.modi@gmail.com> wrote:

> I still don't have a fully working solution (see below). Hopefully someone
> can help figure out the reason for "Invalid read syntax: #" error.
>

OK, I found a workaround. Below solution works:

1. Eval the =org-macros= block once (=C-c C-c=)
   - Always use that =org-macros= block to define the Org macros for this
file.
   - Hitting =C-c C-c= will insert those macros in this file.
   - This block is reused as a noweb-ref in other blocks.. so keeping just
this
     block as a source for all Org macros will be convenient.
2. Then export that file (=C-c C-e h o=)

=====
#+BEGIN_SRC org :noweb-ref org-macros :results output replace :exports none
,#+MACRO: SEC_FOO Foo Topic
,#+MACRO: SEC_BAR Bar Topic
#+END_SRC

#+NAME: ehdr
#+BEGIN_SRC emacs-lisp :var macro="n" :noweb yes :results raw :exports none
(with-temp-buffer
  (insert "
<<org-macros>>
\n")
  (let ((start (point)))
    (insert "// ---------------------------\n")
    (insert (concat "// {{{" macro "}}}"))
    (org-mode)
    (org-macro-replace-all
     (append org-macro-templates org-export-global-macros))
    (buffer-substring-no-properties start (point-max))))
#+END_SRC

* Section 1: {{{SEC_FOO}}}
#+BEGIN_SRC verilog :noweb yes
<<ehdr(macro="SEC_FOO")>>
parameter A = 1;
#+END_SRC

* Section 2: {{{SEC_BAR}}}
#+BEGIN_SRC verilog :noweb yes
<<ehdr(macro="SEC_BAR")>>
parameter A = 1;
#+END_SRC
=====
-- 

Kaushal Modi

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

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

* Re: macro replacement in code block?
  2017-11-01 16:17       ` macro replacement in code block? Kaushal Modi
@ 2017-11-01 17:32         ` David Rogoff
  2017-11-01 18:48           ` Kaushal Modi
  0 siblings, 1 reply; 8+ messages in thread
From: David Rogoff @ 2017-11-01 17:32 UTC (permalink / raw)
  To: Kaushal Modi; +Cc: emacs-org list

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

Thanks so much.  It sort of works but with a bit of work-around:

1) tangle works fine

2) for export, I had to move the 
> ,#+MACRO: SEC_FOO 
lines out of the code block.  If I didn’t and tried to eval that block, I got the error

   No org-babel-execute function for org!

But I think this will do what I need. Of course emacs / org couldn’t have a simple preproccessor-like macro :)

  David 

Sent from my iPhone

> On Nov 1, 2017, at 9:17 AM, Kaushal Modi <kaushal.modi@gmail.com> wrote:
> 
>> On Wed, Nov 1, 2017 at 11:51 AM Kaushal Modi <kaushal.modi@gmail.com> wrote:
>> I still don't have a fully working solution (see below). Hopefully someone can help figure out the reason for "Invalid read syntax: #" error.
> 
> OK, I found a workaround. Below solution works:
> 
> 1. Eval the =org-macros= block once (=C-c C-c=)
>    - Always use that =org-macros= block to define the Org macros for this file.
>    - Hitting =C-c C-c= will insert those macros in this file.
>    - This block is reused as a noweb-ref in other blocks.. so keeping just this
>      block as a source for all Org macros will be convenient.
> 2. Then export that file (=C-c C-e h o=)
> 
> =====
> #+BEGIN_SRC org :noweb-ref org-macros :results output replace :exports none
> ,#+MACRO: SEC_FOO Foo Topic
> ,#+MACRO: SEC_BAR Bar Topic
> #+END_SRC
> 
> #+NAME: ehdr
> #+BEGIN_SRC emacs-lisp :var macro="n" :noweb yes :results raw :exports none
> (with-temp-buffer
>   (insert "
> <<org-macros>>
> \n")
>   (let ((start (point)))
>     (insert "// ---------------------------\n")
>     (insert (concat "// {{{" macro "}}}"))
>     (org-mode)
>     (org-macro-replace-all
>      (append org-macro-templates org-export-global-macros))
>     (buffer-substring-no-properties start (point-max))))
> #+END_SRC
> 
> * Section 1: {{{SEC_FOO}}}
> #+BEGIN_SRC verilog :noweb yes
> <<ehdr(macro="SEC_FOO")>>
> parameter A = 1;
> #+END_SRC
> 
> * Section 2: {{{SEC_BAR}}}
> #+BEGIN_SRC verilog :noweb yes
> <<ehdr(macro="SEC_BAR")>>
> parameter A = 1;
> #+END_SRC 
> =====
> -- 
> Kaushal Modi

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

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

* Re: macro replacement in code block?
  2017-11-01 17:32         ` David Rogoff
@ 2017-11-01 18:48           ` Kaushal Modi
  0 siblings, 0 replies; 8+ messages in thread
From: Kaushal Modi @ 2017-11-01 18:48 UTC (permalink / raw)
  To: David Rogoff; +Cc: emacs-org list

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

On Wed, Nov 1, 2017 at 1:32 PM David Rogoff <david@therogoffs.com> wrote:

> Thanks so much.  It sort of works but with a bit of work-around:
>

Great!


> 2) for export, I had to move the
>
> ,#+MACRO: SEC_FOO
>
> lines out of the code block.  If I didn’t and tried to eval that block, I
> got the error
>
>    No org-babel-execute function for org!
>

You just need to add org as an org babel language:

  (let (ob-lang-alist)
    (add-to-list 'ob-lang-alist '(org . t))
    (org-babel-do-load-languages 'org-babel-load-languages ob-lang-alist))

You don't necessarily need the let form above, but that is in a form that
allows easily adding more languages in future.
-- 

Kaushal Modi

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

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

end of thread, other threads:[~2017-11-01 18:49 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-10-31 23:13 macro replacement in code block? David Rogoff
2017-11-01  0:32 ` Kaushal Modi
2017-11-01  3:21   ` David Rogoff
2017-11-01 15:51     ` Kaushal Modi
2017-11-01 16:04       ` Multi-line noweb expansion in elisp insert form (Was Re: macro replacement in code block?) Kaushal Modi
2017-11-01 16:17       ` macro replacement in code block? Kaushal Modi
2017-11-01 17:32         ` David Rogoff
2017-11-01 18:48           ` Kaushal Modi

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