From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: RFC: String interpolation Date: Thu, 08 Dec 2016 14:05:55 -0500 Message-ID: References: <51825111-ace4-f750-4077-026a3b648d27@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1481224018 26884 195.159.176.226 (8 Dec 2016 19:06:58 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Thu, 8 Dec 2016 19:06:58 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.2.50 (gnu/linux) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Dec 08 20:06:54 2016 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cF41t-0005yf-Fx for ged-emacs-devel@m.gmane.org; Thu, 08 Dec 2016 20:06:53 +0100 Original-Received: from localhost ([::1]:48349 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cF41x-0002Nw-DU for ged-emacs-devel@m.gmane.org; Thu, 08 Dec 2016 14:06:57 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:60494) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cF41F-0002NG-RM for emacs-devel@gnu.org; Thu, 08 Dec 2016 14:06:14 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cF41B-0007sI-So for emacs-devel@gnu.org; Thu, 08 Dec 2016 14:06:13 -0500 Original-Received: from [195.159.176.226] (port=50610 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cF41B-0007rz-Jx for emacs-devel@gnu.org; Thu, 08 Dec 2016 14:06:09 -0500 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1cF415-0006ua-Cg for emacs-devel@gnu.org; Thu, 08 Dec 2016 20:06:03 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 51 Original-X-Complaints-To: usenet@blaine.gmane.org Cancel-Lock: sha1:6z63QiMXwI4oh2EJ++latkKpQ+I= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:210146 Archived-At: > (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))))