From mboxrd@z Thu Jan 1 00:00:00 1970 From: Kaushal Modi Subject: Re: macro replacement in code block? Date: Wed, 01 Nov 2017 15:51:18 +0000 Message-ID: References: <17470BC7-0FB7-458A-AE92-30EC6A7A303F@therogoffs.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="94eb2c08d7ce530e51055cedd8bf" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:34950) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1e9vIi-0005Xh-82 for emacs-orgmode@gnu.org; Wed, 01 Nov 2017 11:51:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1e9vIh-0003kF-3S for emacs-orgmode@gnu.org; Wed, 01 Nov 2017 11:51:32 -0400 Received: from mail-yw0-x22e.google.com ([2607:f8b0:4002:c05::22e]:52667) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1e9vIg-0003k8-TG for emacs-orgmode@gnu.org; Wed, 01 Nov 2017 11:51:31 -0400 Received: by mail-yw0-x22e.google.com with SMTP id w2so2196199ywa.9 for ; Wed, 01 Nov 2017 08:51:30 -0700 (PDT) In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: David Rogoff Cc: emacs-org list --94eb2c08d7ce530e51055cedd8bf Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Oct 31, 2017 at 11:21 PM David Rogoff wrote: > Thanks - I=E2=80=99ll have to look into noweb. I=E2=80=99m 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. =3D=3D=3D=3D=3D 1. Eval the =3Dorg-macros=3D block once (=3DC-c C-c=3D) - Always use that =3Dorg-macros=3D block to define the Org macros for th= is file. - Hitting =3DC-c C-c=3D 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 (=3DC-c C-e h o=3D) #+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=3D"n" :noweb yes :results raw :exports no= ne (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 =3Dorg-macros=3D 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 =3D 1; #+END_SRC ** COMMENT Section 2: {{{SEC_BAR}}} #+BEGIN_SRC verilog :noweb yes <> parameter A =3D 1; #+END_SRC =3D=3D=3D=3D=3D --=20 Kaushal Modi --94eb2c08d7ce530e51055cedd8bf Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
On Tue, Oct 31= , 2017 at 11:21 PM David Rogoff <david@therogoffs.com> wrote:
Tha= nks - I=E2=80=99ll have to look into noweb.=C2=A0 I=E2=80=99m 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 fi= gure out the reason for "Invalid read syntax: #" error.

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

=3D=3D=3D=3D=3D
1. Eval the =3Dorg-macros=3D block once (=3DC-c C-c=3D)
=C2=A0 = =C2=A0- Always use that =3Dorg-macros=3D block to define the Org macros for= this file.
=C2=A0 =C2=A0- Hitting =3DC-c C-c=3D will insert thos= e macros in this file.
=C2=A0 =C2=A0- This block is reused as a n= oweb-ref in other blocks.. so keeping just this
=C2=A0 =C2=A0 =C2= =A0block as a source for all Org macros will be convenient.
2. Ex= port this file (=3DC-c C-e h o=3D)

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

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

If the content of the =3Dorg-macros= =3D block is changed from:
#+BEGIN_SRC org
,#+MACRO: SE= C_FOO Foo Topic
#+END_SRC

To:
= #+BEGIN_SRC org
,#+MACRO: SEC_FOO Foo Topic
,#+MACRO: S= EC_BAR Bar Topic
#+END_SRC

We get this e= rror:
#+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: {{{S= EC_FOO}}}
#+BEGIN_SRC verilog :noweb yes
<<ehdr(m= acro=3D"SEC_FOO")>>
parameter A =3D 1;
= #+END_SRC
** COMMENT Section 2: {{{SEC_BAR}}}
#+BEGIN_S= RC verilog :noweb yes
<<ehdr(macro=3D"SEC_BAR")&g= t;>
parameter A =3D 1;
#+END_SRC
=3D= =3D=3D=3D=3D

--
=

Kaushal Modi

--94eb2c08d7ce530e51055cedd8bf--