Created
December 6, 2011 23:19
-
-
Save 0branch/1440540 to your computer and use it in GitHub Desktop.
Emacs: string interpolation
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-wrap-key (str sym) | |
(get sym (intern (concat ":" str)))) | |
(defun interpolate- (str pl f) | |
(if (string-match interpolate-capture str 0) | |
(let* ((key (match-string 1 str)) | |
(new-str (replace-match "%" t nil str)) | |
(new-key (funcall f key))) | |
(interpolate- new-str (cons new-key pl) f)) | |
(cons str (reverse pl)))) | |
(defun interpolate (str) | |
(let ((result (interpolate- str () (lambda (x) (eval (intern x)))))) | |
(apply 'format result))) | |
(defun interpolate-plist (str sym) | |
(let* ((wrapper (lambda (str) (interpolate-wrap-key str sym))) | |
(result (interpolate- str () wrapper))) | |
(apply 'format result))) | |
(provide 'interpolate) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment