Re: [O] macro replacement in code block?

2017-11-01 Thread Kaushal Modi
On Wed, Nov 1, 2017 at 1:32 PM David Rogoff  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


Re: [O] macro replacement in code block?

2017-11-01 Thread David Rogoff
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  wrote:
> 
>> On Wed, Nov 1, 2017 at 11:51 AM Kaushal Modi  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 "
> <>
> \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
> <>
> parameter A = 1;
> #+END_SRC
> 
> * Section 2: {{{SEC_BAR}}}
> #+BEGIN_SRC verilog :noweb yes
> <>
> parameter A = 1;
> #+END_SRC 
> =
> -- 
> Kaushal Modi


Re: [O] macro replacement in code block?

2017-11-01 Thread Kaushal Modi
On Wed, Nov 1, 2017 at 11:51 AM Kaushal Modi  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 "
<>
\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
<>
parameter A = 1;
#+END_SRC

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

Kaushal Modi


Re: [O] macro replacement in code block?

2017-11-01 Thread Kaushal Modi
On Tue, Oct 31, 2017 at 11:21 PM David Rogoff  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 "<>\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 "<>\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
<>
parameter A = 1;
#+END_SRC
** COMMENT Section 2: {{{SEC_BAR}}}
#+BEGIN_SRC verilog :noweb yes
<>
parameter A = 1;
#+END_SRC
=

-- 

Kaushal Modi


Re: [O] macro replacement in code block?

2017-10-31 Thread David Rogoff


> On Oct 31, 2017, at 5:32 PM, Kaushal Modi  wrote:
> 
> On Tue, Oct 31, 2017, 7:14 PM David Rogoff  > 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



Re: [O] macro replacement in code block?

2017-10-31 Thread Kaushal Modi
On Tue, Oct 31, 2017, 7:14 PM David Rogoff  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


[O] macro replacement in code block?

2017-10-31 Thread David Rogoff
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