Hi Andy!
On Di, 01 Nov 2011, Andy Wokula wrote:
> Am 31.10.2011 22:59, schrieb Christian Brabandt:
> >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
>
> Neat! attached is a mod with backslash/'ff'-bugs fixed and simpler
> notation, shorter gen'd patterns and preparation for multi-char items,
> otherwise no feats added.
>
Nice. Here is your version extended by not replacing inside collations.
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
" Toggle backslash before custom pattern items in the search cmdline
" File: search_escape.vim
" Created: 2011 Oct 31
" Last Change: 2011 Nov 01
"
" Orig by Christian Brabandt @ vim_dev
" Mod by Andy Wokula
" TODO
" - useful default for g:search_escape_items
" + don't replace chars within collections [...]
" + like orig script, prepare for special items that are longer than one
" char
if !exists("g:search_escape_items")
if &magic
let g:search_escape_items = '+ ( ) | ? @='
" no '\' allowed
else
" literal chars with 'nomagic':
let g:search_escape_items = '. * [ ~'
endif
endif
cnoremap <F8> <C-\>e ToggleEscape(g:search_escape_items)<CR>
func! ToggleEscape(special_items)
" {special_items} string of space-separated pattern items
let pat = getcmdline()
if getcmdtype() !~ '[/?]' || a:special_items == ""
return pat
elseif a:special_items =~ '\\'
echoerr 'No ''\'' allowed in g:search_escape_items'
return pat
endif
" pattern to match instances of special items
let si_pat = '\V\%('. join(split(a:special_items),'\|'). '\)'
" (1) pattern for unescaped instances:
let subst1 = '\m'. s:notesc. si_pat
" (2) pattern for escaped instances:
let subst2 = '\m'. s:notesc. '\\'. si_pat
" (3) pattern for collation instances:
let subst3 = '\m'. s:notesc1. si_pat
" decorate escaped instances, for later removal of the backslash:
let pat = substitute(pat, subst2. '\|'. subst3, '#FIX#&#ME#', 'g')
"let pat = substitute(pat, subst3, '#FIX#&#ME#', 'g')
" add backslash before unescaped instances:
let pat = substitute(pat, subst1, '\\&', 'g')
" remove decoration and backslash from decorated instances:
let pat = substitute(pat, '\m#FIX#\\\(.\{1,3}\)#ME#', '\1', 'g')
return pat
endfunc
" don't match either escaped values
let s:notesc = '\%(\\\@<!\%(\\\\\)*\)\@<='
" or values inside collations
let s:notesc1 = '\%(%\@<!\[[^]]*\)\@<='