Created
September 25, 2011 10:48
-
-
Save 0branch/1240468 to your computer and use it in GitHub Desktop.
Emacs: string interpolation of bound variables
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defvar interpolate-capture "%{\\([^}]+\\)}") | |
(defun interpolate- (str pl) | |
(if (string-match interpolate-capture str 0) | |
(let* ((key (match-string 1 str)) | |
(new-str (replace-match "%" t nil str)) | |
(new-key (eval (intern key)))) | |
(interpolate- new-str (cons new-key pl))) | |
(cons str (reverse pl)))) | |
(defun interpolate (str) | |
(let* ((result (interpolate- str ()))) | |
(apply 'format result))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment