emacs-devel
[Top][All Lists]
Advanced

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

Re: RFC: String interpolation


From: Stefan Monnier
Subject: Re: RFC: String interpolation
Date: Thu, 08 Dec 2016 14:05:55 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2.50 (gnu/linux)

>     (let ((a 12) (b 15))
>       (fmt "$a + $b = $(+ a b)"))

Looks good.

>     (concat (fmt--print a) " + " (fmt--print b) " = " (fmt--print (+ a b) 
> 'format "%.2f"))

I'm surprised, I'd have expected to expand to a call to (format ...).
At least that's what my own take on it did (see below; incidentally it
looks otherwise eerily similar to yours in syntax).

FWIW, I don't much like adding extra cases, so I'd stick to a minimal
solution at least until we have enough experience with it to be sure
which extra cases are worth the trouble.

> * Should this go into MELPA, ELPA, or core?

I think it might be worth thinking about it in the larger context of other
special formatting we might have in strings.  I'm thinking here about
things like docstrings and their \\[...] and stuff.  If we could
imagine a path where those could end up merging, it would be great.


        Stefan



(defmacro preformat (string)
  (let ((strs '())
        (i 0)
        (args '()))
    (while (string-match "%\\(?:\\(%\\)\\|\\[\\([^]]+\\)]\\)?" string i)
      (let ((end (match-end 0)))
        (if (match-beginning 1)
            ;; An "escaped" %.
            (progn
              (push (substring string i end) strs)
              (setq i end))
          (push (substring string i (1+ (match-beginning 0))) strs)
          (push (if (match-beginning 2) (match-string 2 string) "s") strs)
          (if (and (eq ?\{ (aref string end))
                   (string-match "{\\([^}]+\\)}" string end))
              (progn
                (push (intern (match-string 1 string)) args)
                (setq i (match-end 0)))
            (let ((ee (read-from-string string end)))
              (push (car ee) args)
              (setq i (cdr ee)))))))
    (push (substring string i) strs)
    `(format ,(mapconcat #'identity (nreverse strs) "")
             ,@(nreverse args))))




reply via email to

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