Skip to content

Instantly share code, notes, and snippets.

@0branch
Created December 6, 2011 23:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0branch/1440540 to your computer and use it in GitHub Desktop.
Save 0branch/1440540 to your computer and use it in GitHub Desktop.
Emacs: string interpolation
(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