There appears to be a bug in `org-fill-template' (in lisp/org-macs.el): keys to be expanded are sorted by increasing length, so that "noweb" is processed before "noweb-ref". As a result, if a template includes "%noweb-ref", `org-fill-template' will expand it as "%{noweb}-ref" rather than "%{noweb-ref}".

As far as I can tell, this bug has existed since `org-fill-template' was added, in e8ef16306ca56af2dceb9e2be0b57c930e9b5584 . I tried to trace it as back as I could, to see whether there was a good reason for sorting keys this way, but couldn't find one.

I'm including a patch with a test to find buggy behavior, and another to fix it.
From c52ce631fbc2f8836e10ff41895892839b349da3 Mon Sep 17 00:00:00 2001
From: Andrew Arensburger <arensb>
Date: Wed, 6 Apr 2022 14:58:44 -0400
Subject: [PATCH 1/2] test-org-macs.el: Add test for template-expansion bug.

* testing/lisp/test-org-macs.el (ert-deftest test-org/fill-template):
There is a bug in `org-fill-template': it sorts and processes keys in
order of increasing length, so that "noweb" is seen before
"noweb-ref", and "tangle" before "tangle-mode". So in a template that
includes "%noweb-ref", it will substitute the value of "noweb".

This change includes a test for this bug.
---
 testing/lisp/test-org-macs.el | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/testing/lisp/test-org-macs.el b/testing/lisp/test-org-macs.el
index 6a7ccea3c..c32d891a2 100644
--- a/testing/lisp/test-org-macs.el
+++ b/testing/lisp/test-org-macs.el
@@ -102,7 +102,16 @@
   (should-not
    (org-test-with-temp-text "xx abc<point> xx"
      (org-in-regexp "abc" nil t))))
+
+;;; Template
 
+(ert-deftest test-org/fill-template ()
+  "Test `org-fill-template'"
+  (should
+   (string= "working"
+            (org-fill-template "%var-long"
+                               '(("var" . "broken")
+                                 ("var-long" . "working"))))))
 
 ;;; Time
 
-- 
2.25.1

From ab2ad947bc741ca42700dfa05bb2ce00903f8db8 Mon Sep 17 00:00:00 2001
From: Andrew Arensburger <arensb>
Date: Wed, 6 Apr 2022 17:02:19 -0400
Subject: [PATCH 2/2] org-macs.el: Fix template expansion.

* lisp/org-macs.el (org-fill-template): Fix a bug in template
expansion: if one key is a substring of another key (like "noweb" and
"noweb-ref", or "tangle" and "tangle-mode"), the second key wouldn't
get expanded properly.  The problem was that keys were processed in
order of increasing length; they are now sorted in order of descending
length.
---
 lisp/org-macs.el | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/lisp/org-macs.el b/lisp/org-macs.el
index b39af9103..f85e1f758 100644
--- a/lisp/org-macs.el
+++ b/lisp/org-macs.el
@@ -1058,7 +1058,10 @@ as-is if removal failed."
   "Find each %key of ALIST in TEMPLATE and replace it."
   (let ((case-fold-search nil))
     (dolist (entry (sort (copy-sequence alist)
-                         (lambda (a b) (< (length (car a)) (length (car b))))))
+                         ; Sort from longest key to shortest, so that
+                         ; "noweb-ref" and "tangle-mode" get processed
+                         ; before "noweb" and "tangle", respectively.
+                         (lambda (a b) (< (length (car b)) (length (car a))))))
       (setq template
 	    (replace-regexp-in-string
 	     (concat "%" (regexp-quote (car entry)))
-- 
2.25.1

Reply via email to