From: Andrew Arensburger <arensb@ooblick.com>
To: emacs-orgmode@gnu.org
Subject: Patch: bug in template expansion in org-fill-template
Date: Thu, 7 Apr 2022 00:08:56 -0400 [thread overview]
Message-ID: <b098cbaf-e9fe-81f6-18ec-796b410ca2a6@ooblick.com> (raw)
[-- 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
next reply other threads:[~2022-04-07 4:43 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-04-07 4:08 Andrew Arensburger [this message]
2022-04-30 9:49 ` Patch: bug in template expansion in org-fill-template Ihor Radchenko
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.orgmode.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=b098cbaf-e9fe-81f6-18ec-796b410ca2a6@ooblick.com \
--to=arensb@ooblick.com \
--cc=emacs-orgmode@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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).