From mboxrd@z Thu Jan 1 00:00:00 1970 From: Aaron Ecay Subject: Re: [RFC] [PATCH] Warn about unexpanded macros on export Date: Sat, 27 Sep 2014 23:53:06 -0400 Message-ID: <8738bcmme5.fsf@gmail.com> References: <87k34zqv9r.fsf@gmail.com> <87bnqbv27b.fsf@nicolasgoaziou.fr> <87fvfjow6p.fsf@gmail.com> <87lhpaf5lq.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:59325) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XY5Y3-0006OH-Bi for emacs-orgmode@gnu.org; Sat, 27 Sep 2014 23:53:32 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XY5Xu-0002Wr-8z for emacs-orgmode@gnu.org; Sat, 27 Sep 2014 23:53:23 -0400 Received: from mail-qg0-x22b.google.com ([2607:f8b0:400d:c04::22b]:58697) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XY5Xu-0002W8-2u for emacs-orgmode@gnu.org; Sat, 27 Sep 2014 23:53:14 -0400 Received: by mail-qg0-f43.google.com with SMTP id j107so1923424qga.2 for ; Sat, 27 Sep 2014 20:53:08 -0700 (PDT) In-Reply-To: <87lhpaf5lq.fsf@nicolasgoaziou.fr> 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-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Nicolas Goaziou , Org-mode --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hi Nicolas, Thanks for the feedback. 2014ko irailak 23an, Nicolas Goaziou-ek idatzi zuen: >=20 > Hello, >=20 > Aaron Ecay writes: >=20 >> Um...but the patch I sent works precisely by defining a macro translator, >> which does get called for any unexpanded (because undefined) macros. >=20 > Macros are not expected to be seen by export back-ends. It happens when > a macro is undefined, but this is not a reliable feature. >=20 >> I guess you=E2=80=99re saying the code ought to block this at a lower/ea= rlier >> level. >=20 > Yes, at "org-macro.el" level. >=20 >> I think error is better than obnoxious message, because it=E2=80=99s pos= sible >> for the latter to slip through into a =E2=80=9Cproduction=E2=80=9D docum= ent. (We ought >> to proofread our documents carefully, of course...but no one=E2=80=99s >> perfect). >=20 > Sounds good. >=20 >> One issue is that the exporter does two macro expansion passes =E2=80=93= one >> for garden-variety macros, and the second for author, date, email, and >> title. So, we can=E2=80=99t make the macro expansion unconditionally ba= rf on >> undefined macros (since for the first pass e.g. author is undefined). >> I see three options: >> 1. explicitly whitelist the few =E2=80=9Cblessed=E2=80=9D macros like au= thor, and error >> on any other undefined macro >> 2. add an optional =E2=80=9Cfinal=E2=80=9D arg to org-macro-replace-all,= which will >> activate the undefined checking only if non-nil, and pass this flag >> in the exporter=E2=80=99s second (and last) call to org-macro-replace-all >> 3. in =E2=80=98org-export-as=E2=80=99, manually walk the parse tree afte= r expanding >> macros, and make sure no 'macro type objects are left >>=20 >> WDYT? >=20 > I have no strong opinion here but I lean towards option 2 as the error > stays internal to "org-macro.el" and is only triggered with an optional > argument. It also doesn't require to hardcode special macro names. Attached is a revised patch. WDYT? --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-Warn-about-unexpanded-macros-on-export.patch >From e386809601d99291885136de1d9be84193b69a2c Mon Sep 17 00:00:00 2001 From: Aaron Ecay Date: Sat, 27 Sep 2014 23:50:23 -0400 Subject: [PATCH] Warn about unexpanded macros on export * lisp/org-macro.el (org-macro-replace-all): Add optional `finalize' argument. * lisp/ox.el (org-export-as): Use it. --- lisp/org-macro.el | 35 ++++++++++++++++++++++------------- lisp/ox.el | 3 ++- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/lisp/org-macro.el b/lisp/org-macro.el index 50ce438..307e232 100644 --- a/lisp/org-macro.el +++ b/lisp/org-macro.el @@ -155,10 +155,14 @@ default value. Return nil if no template was found." ;; Return string. (format "%s" (or value "")))))) -(defun org-macro-replace-all (templates) +(defun org-macro-replace-all (templates &optional finalize) "Replace all macros in current buffer by their expansion. + TEMPLATES is an alist of templates used for expansion. See -`org-macro-templates' for a buffer-local default value." +`org-macro-templates' for a buffer-local default value. + +If optional arg FINALIZE is non-nil, raise an error if a macro is +found in the buffer with no definition in TEMPLATES." (save-excursion (goto-char (point-min)) (let (record) @@ -176,17 +180,22 @@ TEMPLATES is an alist of templates used for expansion. See (if (member signature record) (error "Circular macro expansion: %s" (org-element-property :key object)) - (when value - (push signature record) - (delete-region - begin - ;; Preserve white spaces after the macro. - (progn (goto-char (org-element-property :end object)) - (skip-chars-backward " \t") - (point))) - ;; Leave point before replacement in case of recursive - ;; expansions. - (save-excursion (insert value))))))))))) + (if value + (progn + (push signature record) + (delete-region + begin + ;; Preserve white spaces after the macro. + (progn (goto-char (org-element-property :end object)) + (skip-chars-backward " \t") + (point))) + ;; Leave point before replacement in case of recursive + ;; expansions. + (save-excursion (insert value))) + (when finalize + (error "Macro %s was undefined at line %s" + (org-element-property :key object) + (line-number-at-pos)))))))))))) (provide 'org-macro) diff --git a/lisp/ox.el b/lisp/ox.el index 59091fc..216a375 100644 --- a/lisp/ox.el +++ b/lisp/ox.el @@ -3136,7 +3136,8 @@ Return code as a string." ;; EMAIL is not a parsed keyword: store it as-is. (cons "email" (or (plist-get info :email) "")) (cons "title" - (org-element-interpret-data (plist-get info :title))))) + (org-element-interpret-data (plist-get info :title)))) + 'finalize) ;; Parse buffer. (setq tree (org-element-parse-buffer nil visible-only)) ;; Handle left-over uninterpreted elements or objects in -- 2.1.1 --=-=-= Content-Type: text/plain -- Aaron Ecay --=-=-=--