From: Stefan Kangas <stefankangas@gmail.com>
To: Visuwesh <visuweshm@gmail.com>
Cc: Thierry Volpiatto <thievol@posteo.net>,
Po Lu <luangruo@yahoo.com>,
emacs-devel@gnu.org
Subject: Re: master fe7c015b20: Support XFCE in wallpaper.el
Date: Tue, 27 Sep 2022 12:19:32 -0400 [thread overview]
Message-ID: <CADwFkmk5yDt1gGupxejqF6PSnVg3S8cNZ9o8BgZiAMo7u0Miug@mail.gmail.com> (raw)
In-Reply-To: <87edvwrfg7.fsf@gmail.com>
[-- Attachment #1: Type: text/plain, Size: 477 bytes --]
Visuwesh <visuweshm@gmail.com> writes:
> [செவ்வாய் செப்டம்பர் 27, 2022] Stefan Kangas wrote:
>
>> Could we change `format-spec' to accept functions in the substitutions,
>> that are then only called when actually needed?
>
> +1. I was thinking of raising such a proposal myself. It seems wasteful to
> pre-calculate all the specifications instead of lazily evaluating them.
The change is just the attached, plus documentation.
[-- Attachment #2: 0001-Make-format-spec-support-functions.patch --]
[-- Type: text/x-diff, Size: 2945 bytes --]
From e0a1a678e27a6a3da5e12edfcc7323c5204f6d2a Mon Sep 17 00:00:00 2001
From: Stefan Kangas <stefankangas@gmail.com>
Date: Tue, 27 Sep 2022 18:16:51 +0200
Subject: [PATCH] Make format-spec support functions
---
lisp/format-spec.el | 15 +++++++++++++--
test/lisp/format-spec-tests.el | 12 ++++++++++++
2 files changed, 25 insertions(+), 2 deletions(-)
diff --git a/lisp/format-spec.el b/lisp/format-spec.el
index 45c19aebc8..31e1d861c2 100644
--- a/lisp/format-spec.el
+++ b/lisp/format-spec.el
@@ -59,6 +59,16 @@ format-spec
leading zeros or truncating leading characters until it's ten
characters wide\".
+If the value is a function, it will be evaluated only when
+needed. For example:
+
+ (format-spec \"%n\"
+ \\=`((?n . ,(lambda ()
+ (read-number \"Number: \")))))
+
+Note that it is better to make sure the lambda is not quoted,
+like above, so that it is compiled by the byte-compiler.
+
Any text properties of FORMAT are copied to the result, with any
text properties of a %-spec itself copied to its substitution.
@@ -94,14 +104,15 @@ format-spec
(width (match-string 2))
(trunc (match-string 3))
(char (string-to-char (match-string 4)))
- (text (assq char specification)))
+ (text (let ((res (cdr (assq char specification))))
+ (if (and (functionp res)) (funcall res) res))))
(when (and split
(not (= (1- beg) split-start)))
(push (buffer-substring split-start (1- beg)) split-result))
(cond (text
;; Handle flags.
(setq text (format-spec--do-flags
- (format "%s" (cdr text))
+ (format "%s" text)
(format-spec--parse-flags flags)
(and width (string-to-number width))
(and trunc (car (read-from-string trunc 1)))))
diff --git a/test/lisp/format-spec-tests.el b/test/lisp/format-spec-tests.el
index 4a3cc74c33..f137f94aaa 100644
--- a/test/lisp/format-spec-tests.el
+++ b/test/lisp/format-spec-tests.el
@@ -148,6 +148,18 @@ format-spec
(format-spec fmt '((?b . "asd") (?a . "fgh")))
#("fgh%asdasd" 0 3 (a b) 3 4 (c d) 7 10 (e f))))))
+(ert-deftest format-spec/function ()
+ (let* (called
+ (spec `((?a . "foo")
+ (?f . ,(lambda ()
+ (setq called t)
+ "bar")))))
+ (should (equal (format-spec "%a" spec) "foo"))
+ (should-not called)
+ (should (equal (format-spec "%f" spec) "bar"))
+ (should called)))
+
+
(ert-deftest format-spec-unknown ()
(should-error (format-spec "foo %b %z zot" '((?b . "bar"))))
(should-error (format-spec "foo %b %%%z zot" '((?b . "bar"))))
--
2.30.2
next prev parent reply other threads:[~2022-09-27 16:19 UTC|newest]
Thread overview: 61+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <166336105908.23797.12319380359602540227@vcs2.savannah.gnu.org>
[not found] ` <20220916204419.B124FC00872@vcs2.savannah.gnu.org>
2022-09-17 2:00 ` master fe7c015b20: Support XFCE in wallpaper.el Po Lu
2022-09-17 18:03 ` Stefan Kangas
2022-09-18 1:18 ` Po Lu
2022-09-18 2:13 ` Po Lu
2022-09-18 4:48 ` Thierry Volpiatto
2022-09-18 5:15 ` Po Lu
2022-09-18 14:44 ` Thierry Volpiatto
2022-09-19 4:49 ` Po Lu
2022-09-19 6:06 ` Thierry Volpiatto
2022-09-19 7:24 ` Po Lu
2022-09-19 8:40 ` Thierry Volpiatto
2022-09-19 8:52 ` Po Lu
2022-09-19 17:46 ` Thierry Volpiatto
2022-09-19 18:05 ` Thierry Volpiatto
2022-09-19 18:59 ` Stefan Kangas
2022-09-19 20:16 ` Tomas Hlavaty
2022-09-19 21:19 ` Stefan Monnier
2022-09-20 6:16 ` Thierry Volpiatto
2022-09-20 2:32 ` Eli Zaretskii
2022-09-20 2:43 ` Po Lu
2022-09-20 6:25 ` Thierry Volpiatto
2022-09-20 8:22 ` Po Lu
2022-09-19 7:23 ` Thierry Volpiatto
2022-09-26 19:42 ` Stefan Kangas
2022-09-26 21:01 ` Stephen Berman
2022-09-26 22:46 ` Stefan Kangas
2022-09-27 7:17 ` Stephen Berman
2022-09-27 7:45 ` Po Lu
2022-09-27 13:42 ` Stefan Kangas
2022-09-27 0:33 ` Po Lu
2022-09-27 5:56 ` Eli Zaretskii
2022-09-27 6:13 ` Po Lu
2022-09-27 6:45 ` Eli Zaretskii
2022-09-27 7:50 ` Po Lu
2022-09-27 7:56 ` Eli Zaretskii
2022-09-27 8:31 ` Po Lu
2022-09-27 7:21 ` Thierry Volpiatto
2022-09-27 7:42 ` Po Lu
2022-09-27 7:55 ` Thierry Volpiatto
2022-09-27 8:35 ` Po Lu
2022-09-27 18:33 ` Stefan Kangas
2022-09-28 0:34 ` Po Lu
2022-09-27 13:57 ` Stefan Kangas
2022-09-27 15:43 ` Visuwesh
2022-09-27 16:19 ` Stefan Kangas [this message]
2022-09-27 16:39 ` Thierry Volpiatto
2022-09-27 16:40 ` Eli Zaretskii
2022-09-27 17:01 ` Stefan Kangas
2022-09-27 17:14 ` Eli Zaretskii
2022-09-27 18:02 ` Make format-spec accept a function as the substitution Stefan Kangas
2022-09-29 6:57 ` Thierry Volpiatto
2022-09-29 12:36 ` Stefan Monnier
2022-09-29 12:59 ` Stefan Kangas
2022-09-29 15:02 ` Philip Kaludercic
2022-09-29 16:34 ` Stefan Kangas
2022-09-29 16:53 ` Philip Kaludercic
2022-09-29 17:06 ` Stefan Kangas
2022-09-29 16:56 ` Stefan Monnier
2022-09-29 15:09 ` Thierry Volpiatto
2022-09-29 16:34 ` Stefan Kangas
2022-09-29 14:16 ` master fe7c015b20: Support XFCE in wallpaper.el Stefan Kangas
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CADwFkmk5yDt1gGupxejqF6PSnVg3S8cNZ9o8BgZiAMo7u0Miug@mail.gmail.com \
--to=stefankangas@gmail.com \
--cc=emacs-devel@gnu.org \
--cc=luangruo@yahoo.com \
--cc=thievol@posteo.net \
--cc=visuweshm@gmail.com \
/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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.