Permalink
Browse files

New way to eval `...`, and escape \

* \ is escaped as `
* the eval result in `substitute` may contain special chars,
  see |sub-replace-special|
* syntax update
  • Loading branch information...
lilydjwg authored and garbas committed Dec 18, 2010
1 parent 08945cc commit 2a83172c0bfe2d682e6862d9147410e96e7b5a19
Showing with 18 additions and 7 deletions.
  1. +15 −6 autoload/snipMate.vim
  2. +3 −1 syntax/snippet.vim
View
@@ -77,15 +77,24 @@ fun s:ProcessSnippet(snip)
let snippet = a:snip
" Evaluate eval (`...`) expressions.
" Backquotes prefixed with a backslash "\" are ignored.
+ " And backslash can be escaped by doubling it.
" Using a loop here instead of a regex fixes a bug with nested "\=".
if stridx(snippet, '`') != -1
- while match(snippet, '\(^\|[^\\]\)`.\{-}[^\\]`') != -1
- let snippet = substitute(snippet, '\(^\|[^\\]\)\zs`.\{-}[^\\]`\ze',
- \ substitute(eval(matchstr(snippet, '\(^\|[^\\]\)`\zs.\{-}[^\\]\ze`')),
- \ "\n\\%$", '', ''), '')
- endw
+ let new = []
+ let snip = split(snippet, '\%(\\\@<!\%(\\\\\)*\)\@<=`', 1)
+ let isexp = 0
+ for i in snip
+ if isexp
+ call add(new, substitute(eval(i), "\n\\%$", '', ''))
+ else
+ call add(new, i)
+ endif
+ let isexp = !isexp
+ endfor
+ let snippet = join(new, '')
let snippet = substitute(snippet, "\r", "\n", 'g')
- let snippet = substitute(snippet, '\\`', '`', 'g')
+ let snippet = substitute(snippet, '\\`', "`", 'g')
+ let snippet = substitute(snippet, '\\\\', "\\", 'g')
endif
" Place all text after a colon in a tab stop after the tab stop
View
@@ -3,7 +3,8 @@
syn match snipComment '^#.*'
syn match placeHolder '\${\d\+\(:.\{-}\)\=}' contains=snipCommand
syn match tabStop '\$\d\+'
-syn match snipCommand '[^\\]`.\{-}`'
+syn match snipEscape '\\\\\|\\`'
+syn match snipCommand '\%(\\\@<!\%(\\\\\)*\)\@<=`.\{-}\%(\\\@<!\%(\\\\\)*\)\@<=`'
syn match snippet '^snippet.*' transparent contains=multiSnipText,snipKeyword
syn match multiSnipText '\S\+ \zs.*' contained
syn match snipKeyword '^snippet'me=s+8 contained
@@ -12,6 +13,7 @@ syn match snipError "^[^#s\t].*$"
hi link snipComment Comment
hi link multiSnipText String
hi link snipKeyword Keyword
+hi link snipEscape SpecialChar
hi link snipComment Comment
hi link placeHolder Special
hi link tabStop Special

0 comments on commit 2a83172

Please sign in to comment.