mirror of
https://github.com/joaotavora/yasnippet.git
synced 2025-10-14 05:23:04 +00:00
refine escape.
This commit is contained in:
parent
9be049c264
commit
dd90bee74c
40
yasnippet.el
40
yasnippet.el
@ -49,18 +49,22 @@ current column if this variable is non-`nil'.")
|
|||||||
"The snippet-field structure of yasnippet."
|
"The snippet-field structure of yasnippet."
|
||||||
overlay state)
|
overlay state)
|
||||||
|
|
||||||
(defconst yas/escape-dollar "\\$")
|
(defconst yas/escape-backslash
|
||||||
(defconst yas/escape-backquote "\\`")
|
(concat "YASESCAPE" "BACKSLASH" "PROTECTGUARD"))
|
||||||
(defconst yas/escape-dollar-guard
|
(defconst yas/escape-dollar
|
||||||
(concat "YASESCAPE" "DOLLAR" "PROTECTGUARD"))
|
(concat "YASESCAPE" "DOLLAR" "PROTECTGUARD"))
|
||||||
(defconst yas/escape-backquote-guard
|
(defconst yas/escape-backquote
|
||||||
(concat "YASESCAPE" "BACKQUOTE" "PROTECTGUARD"))
|
(concat "YASESCAPE" "BACKQUOTE" "PROTECTGUARD"))
|
||||||
|
|
||||||
(defconst yas/elisp-regexp "`\\([^`]*\\)`")
|
|
||||||
(defconst yas/elisp-regexp-content-group 1)
|
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
;; Internal functions
|
;; Internal functions
|
||||||
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
|
||||||
|
(defun yas/eval-string (string)
|
||||||
|
"Evaluate STRING and convert the result to string."
|
||||||
|
(condition-case err
|
||||||
|
(format "%s" (eval (read string)))
|
||||||
|
(error (format "(error in elisp evaluation: %s)"
|
||||||
|
(error-message-string err)))))
|
||||||
(defsubst yas/replace-all (from to)
|
(defsubst yas/replace-all (from to)
|
||||||
"Replace all occurance from FROM to TO."
|
"Replace all occurance from FROM to TO."
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
@ -110,24 +114,26 @@ current column if `yas/indent-line' is non-`nil'."
|
|||||||
(insert indent)
|
(insert indent)
|
||||||
(end-of-line))))
|
(end-of-line))))
|
||||||
|
|
||||||
;; Step 2: protect backquote
|
;; Step 2: protect backslash and backquote
|
||||||
(yas/replace-all yas/escape-backquote yas/escape-backquote-guard)
|
(yas/replace-all "\\\\" yas/escape-backslash)
|
||||||
|
(yas/replace-all "\\`" yas/escape-backquote)
|
||||||
|
|
||||||
;; Step 3: evaluate all backquotes
|
;; Step 3: evaluate all backquotes
|
||||||
(goto-char (point-min))
|
(goto-char (point-min))
|
||||||
(while (re-search-forward yas/elisp-regexp nil t)
|
(while (re-search-forward "`\\([^`]*\\)`" nil t)
|
||||||
(replace-match (format "%s" (eval
|
(replace-match (yas/eval-string (match-string-no-properties 1))
|
||||||
(read
|
|
||||||
(match-string-no-properties
|
|
||||||
yas/elisp-regexp-content-group))))
|
|
||||||
t t))
|
t t))
|
||||||
|
|
||||||
;; Step 4: protect dollar
|
;; Step 4: protect all escapes, including backslash and backquot
|
||||||
(yas/replace-all yas/escape-dollar yas/escape-dollar-guard)
|
;; which may be produced in Step 3
|
||||||
|
(yas/replace-all "\\\\" yas/escape-backslash)
|
||||||
|
(yas/replace-all "\\`" yas/escape-backquote)
|
||||||
|
(yas/replace-all "\\$" yas/escape-dollar)
|
||||||
|
|
||||||
;; Step : restore all escape characters
|
;; Step : restore all escape characters
|
||||||
(yas/replace-all yas/escape-dollar-guard yas/escape-dollar)
|
(yas/replace-all yas/escape-dollar "$")
|
||||||
(yas/replace-all yas/escape-backquote-guard yas/escape-backquote)
|
(yas/replace-all yas/escape-backquote "`")
|
||||||
|
(yas/replace-all yas/escape-backslash "\\")
|
||||||
|
|
||||||
(buffer-string)))
|
(buffer-string)))
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user