emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Patch: bug in template expansion in org-fill-template
@ 2022-04-07  4:08 Andrew Arensburger
  2022-04-30  9:49 ` Ihor Radchenko
  0 siblings, 1 reply; 2+ messages in thread
From: Andrew Arensburger @ 2022-04-07  4:08 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 651 bytes --]

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.

[-- Attachment #2: 0001-test-org-macs.el-Add-test-for-template-expansion-bug.patch --]
[-- Type: text/x-patch, Size: 1291 bytes --]

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))))
+\f
+;;; Template
 
+(ert-deftest test-org/fill-template ()
+  "Test `org-fill-template'"
+  (should
+   (string= "working"
+            (org-fill-template "%var-long"
+                               '(("var" . "broken")
+                                 ("var-long" . "working"))))))
 \f
 ;;; Time
 
-- 
2.25.1


[-- Attachment #3: 0002-org-macs.el-Fix-template-expansion.patch --]
[-- Type: text/x-patch, Size: 1435 bytes --]

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


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: Patch: bug in template expansion in org-fill-template
  2022-04-07  4:08 Patch: bug in template expansion in org-fill-template Andrew Arensburger
@ 2022-04-30  9:49 ` Ihor Radchenko
  0 siblings, 0 replies; 2+ messages in thread
From: Ihor Radchenko @ 2022-04-30  9:49 UTC (permalink / raw)
  To: Andrew Arensburger; +Cc: emacs-orgmode

Andrew Arensburger <arensb@ooblick.com> writes:

> 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.

Thanks!
Applied to main as 82a09ba0a and 4a30e8cc0 with some amendments to the
commit messages: Fixed "." in the commit summary and added "  " between
sentences; (2) Added TINYCHANGE cookies as you do not appear to have
copyright assignment.

Note that you may need to do copyright paperwork if you decide to
contribute in future. See
https://orgmode.org/worg/org-contribute.html#copyright

Best,
Ihor


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-04-30  9:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-07  4:08 Patch: bug in template expansion in org-fill-template Andrew Arensburger
2022-04-30  9:49 ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).