[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Template substitution in Emacs?
From: |
Paul W. Rankin |
Subject: |
Re: Template substitution in Emacs? |
Date: |
Mon, 10 Nov 2014 12:16:22 +1000 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.4 (darwin) |
Marcin Borkowski <mbork@wmi.amu.edu.pl> writes:
> Hi there,
>
> assume that I want to insert an (almost) constant string in Emacs
> buffer. For instance, it may be a string of the form
>
> "I am a {my-variable}."
>
> However, I want the "{my-variable}" part to be, well, variable: I want
> to substitute the current value of `my-variable' for this. So that
>
> (setq my-variable "sentence")
> (insert (template-substitute "I am a {my-variable}."))
>
> would insert "I am a sentence." at point.
>
> Of course, I could just use (format "I am a %s." my-variable), or even
> define a function to do this. The drawback would be that if the
> "template" is a user-customizable thing, then the user would have to
> supply %s's and not meaningful names. What's more, the /order/ of the
> substituted strings would be hardcoded in the "template".
>
> Obviously, I could come up with such a function within 10-15 minutes or
> so. But why reinvent the wheel? Is there anything like this in Emacs?
>
> TIA,
`s-format' in the `s' package kind of does this. See discussion here:
https://github.com/magnars/s.el/issues/57
Basically...
--8<---------------cut here---------------start------------->8---
(let ((template "I am a ${my-variable}")
(my-variable "sentence"))
(s-format template
'(lambda (var)
(symbol-value (intern var)))))
=>
"I am a sentence"
--8<---------------cut here---------------end--------------->8---
--
Paul W. Rankin
http://www.paulwrankin.com