emacs-devel
[Top][All Lists]
Advanced

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

Re: Make format-spec accept a function as the substitution


From: Thierry Volpiatto
Subject: Re: Make format-spec accept a function as the substitution
Date: Thu, 29 Sep 2022 06:57:07 +0000

Hello Stefan,

Stefan Kangas <stefankangas@gmail.com> writes:

> Eli Zaretskii <eliz@gnu.org> writes:
>
>> And then I'd suggest something like
>>
>>   If the substitution for a specification character is a function, the
>>   function will be called only if FORMAT uses that character.
>
> Thanks.  Please find attached a complete patch with documentation.
>
> [2. text/x-diff; 0001-Make-format-spec-support-functions.patch]...

After thinking at it, it would be great to allow using interactive calls
in specs without wrapping them inside a function:

Using this should ask for line number and file:
(format-spec "Go to line %n in file %f %g" '((?n . (read-number "Number: "))
                                             (?g . "now")
                                             (?f . (read-file-name "File: "))))

Whereas using this should not ask for anything:
(format-spec "Go to line %g" '((?n . (read-number "Number: "))
                               (?g . "now")
                               (?f . (read-file-name "File: "))))


For this specs should be looked one by one and evaluated when needed,
here a naive version of format-spec that allow this (doesn't handle 0,
space, -_> etc..):

(defun tv/format-spec-1 (format spec)
  (cl-loop with fma = format
           with result
           for (char . val) in spec
           for target = (if (string-match "%\\([[:alpha:]]\\)" fma)
                            (string-to-char (match-string 1 fma))
                          fma)
           if (eq target char)
           return (eval val t)
           finally return target))

(defun tv/format-spec (format spec)
  (let (strs fmt-str)
    (with-temp-buffer
      (save-excursion
        (insert format))
      (setq strs (cl-loop while (re-search-forward "%\\([[:alpha:]]\\)" nil t)
                          collect (match-string 0)
                          do (replace-match "s" nil nil nil 1)))
      (setq fmt-str (buffer-string)))
    (apply #'format fmt-str
           (cl-loop for str in strs
                    collect (tv/format-spec-1 str spec)))))

Probably the idea handled by this code could be ported to format-spec?

-- 
Thierry

Attachment: signature.asc
Description: PGP signature


reply via email to

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