emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] [RFC] [PATCH] Warn about unexpanded macros on export


From: Aaron Ecay
Subject: Re: [O] [RFC] [PATCH] Warn about unexpanded macros on export
Date: Sat, 27 Sep 2014 23:53:06 -0400
User-agent: Notmuch/0.18.1+51~gbbbdf04 (http://notmuchmail.org) Emacs/24.4.50.2 (x86_64-unknown-linux-gnu)

Hi Nicolas,

Thanks for the feedback.

2014ko irailak 23an, Nicolas Goaziou-ek idatzi zuen:
> 
> Hello,
> 
> Aaron Ecay <address@hidden> writes:
> 
>> Um...but the patch I sent works precisely by defining a macro translator,
>> which does get called for any unexpanded (because undefined) macros.
> 
> 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.
> 
>> I guess you’re saying the code ought to block this at a lower/earlier
>> level.
> 
> Yes, at "org-macro.el" level.
> 
>> I think error is better than obnoxious message, because it’s possible
>> for the latter to slip through into a “production” document.  (We ought
>> to proofread our documents carefully, of course...but no one’s
>> perfect).
> 
> Sounds good.
> 
>> One issue is that the exporter does two macro expansion passes – one
>> for garden-variety macros, and the second for author, date, email, and
>> title.  So, we can’t make the macro expansion unconditionally barf on
>> undefined macros (since for the first pass e.g. author is undefined).
>> I see three options:
>> 1. explicitly whitelist the few “blessed” macros like author, and error
>> on any other undefined macro
>> 2. add an optional “final” arg to org-macro-replace-all, which will
>> activate the undefined checking only if non-nil, and pass this flag
>> in the exporter’s second (and last) call to org-macro-replace-all
>> 3. in ‘org-export-as’, manually walk the parse tree after expanding
>> macros, and make sure no 'macro type objects are left
>> 
>> WDYT?
> 
> 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?
>From e386809601d99291885136de1d9be84193b69a2c Mon Sep 17 00:00:00 2001
From: Aaron Ecay <address@hidden>
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

-- 
Aaron Ecay

reply via email to

[Prev in Thread] Current Thread [Next in Thread]