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