On Mo, 31 Okt 2011, Ernie Rael wrote:
> It's similar, but a list of chars that need escaping is more flexible.
> I could specify that '(' needs to be escaped to mean grouping and that
> '?' does not need to be escaped to mean optional. Using the list could
> be dependent on the "magic-ness of a pattern".
Hm, interesting concept. Attached is a simple script to try out.
Use
:let g:re_dont_escape = '()|?'
to specify which chars have a special meaning and don't need to be
escaped (only for using the literal version). So in this example, '()
wouldn't need to be escaped for grouping, '|' means OR and '?' means
optional match.
When searching, press <f7> to translate the pattern into a vim pattern.
It basically only adds/removes the backslashes (so you need to know all
vim specific atoms, like '\@<=' and can't use e.g. Perl look-arounds).
Disclaimer, only very basically tested.
regards,
Christian
--
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
fu! CheckEscape()
let pat = getcmdline()
if getcmdtype() !~# '?\|/'
return pat
endif
let escape = exists("g:re_dont_escape") ?
\ split(g:re_dont_escape, '\zs') : []
if !empty(escape)
let esc1 = map(copy(escape),
\ '''\%(\%(\%([^\\]\|^\)\\\)\@<!\V''.v:val.''\m\)''')
let esc2 = map(copy(escape),
\ '''\%(\%(\%([^\\]\|^\)\)\@<=\\\V''.v:val.''\m\)''')
let subst1 = '\m' . join(esc1, '\|')
let subst2 = '\m' . join(esc2, '\|')
" decorate all backslates, that shall later be removed
let pat = substitute(pat, subst2, '##&##', 'g')
" replace all atoms by their backslash escaped values
let pat = substitute(pat, subst1, '\\&', 'g')
" Remove backslashes from unwanted escaped atoms
let pat = substitute(pat, '\m##\\\(\V' . join(escape, '\|\V') .
'\m\)##',
\ '\1', 'g')
endif
return pat
endfu
if !exists("g:re_dont_escape")
let g:re_dont_escape = ''
if !&magic
" Is this a sensible default for nomagic?
let g:re_dont_escape = '.+'
endif
endif
cnoremap <f7> <c-\>eCheckEscape()<cr>
" Feeling adventurouse? Enable the next mapping
" Note, that if this script is buggy, you possibly won't be
" able to press <enter> then.
"cnoremap <cr> <c-\>eCheckEscape()<cr><cr>