Change variable delimiter

Since the `__' is used quite a lot in, for example, Python code and
files it proved to be a sub-optimal as a delimiter. Now, by default,
`$!' is used, which should appear somewhat less (I hope) and it has
been made customizable. Look out when customizing, it is placed in a
regular expression without any escaping.
This commit is contained in:
Tom Willemse 2013-05-20 01:02:38 +02:00
parent 8c8fa18857
commit d760772a9a

View file

@ -1,8 +1,14 @@
(defvar pt-templates-directory "~/.emacs.d/templates" (defvar pt-templates-directory "~/.emacs.d/templates"
"Where templates are stored.") "Where templates are stored.")
(defvar pt-template-var-regexp "__\\([^_ \t\n]+\\)__" (defvar pt-template-var-delimiter "$!"
"Regexp which identifies a variable in a template.") "Strings used to delimit variable names.")
(defun pt-template-var-regexp ()
"Create the regexp which identifies a variable in a template."
(concat pt-template-var-delimiter
"\\([^" pt-template-var-delimiter " \t\n]+\\)"
pt-template-var-delimiter))
(defvar pt-value-alist nil (defvar pt-value-alist nil
"A placeholder where replacement values will be kept. This is "A placeholder where replacement values will be kept. This is
@ -27,7 +33,7 @@
(defun pt-parse-file-name (filename) (defun pt-parse-file-name (filename)
"Parse FILENAME and replace all __variables__ with values "Parse FILENAME and replace all __variables__ with values
provided by the user." provided by the user."
(while (string-match pt-template-var-regexp filename) (while (string-match (pt-template-var-regexp) filename)
(let* ((tpl-var (match-string 1 filename)) (let* ((tpl-var (match-string 1 filename))
(replacement-value (pt-get-replacement tpl-var))) (replacement-value (pt-get-replacement tpl-var)))
(set 'filename (replace-match (cdr replacement-value) t t (set 'filename (replace-match (cdr replacement-value) t t
@ -43,7 +49,7 @@ provided by the user."
the user." the user."
(insert-file-contents file) (insert-file-contents file)
(while (re-search-forward pt-template-var-regexp nil t) (while (re-search-forward (pt-template-var-regexp) nil t)
(let* ((tpl-var (match-string 1)) (let* ((tpl-var (match-string 1))
(replacement-value (pt-get-replacement tpl-var))) (replacement-value (pt-get-replacement tpl-var)))
(replace-match (cdr replacement-value) t t)))) (replace-match (cdr replacement-value) t t))))