All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
3 " Last Change: 2012 Dec 19
5 " Author: Andy Wokula <anwoku@yahoo.de>
6 " License: Vim License, see :h license
11 " nwo#magic#MakeMagic({pat})
13 " remove embedded switches (\v, \m, \M and \V) from pattern {pat} by
14 " converting {pat} into a purely magic pattern. Return the converted
19 " - recognize [#-\\]], with spaces: [ #-\ \] ]
20 " (collection ends at second ']')
22 " 2011 Nov 01 copied from asneeded\makemagic.vim
23 " now asneeded\nwo\makemagic.vim (comments there!)
29 let g:nwo#magic#loaded = 1
32 func! nwo#magic#MakeMagic(pat, ...) "{{{
34 " {a:1} (boolean) initial magic mode (default follows the 'magic' option)
36 if a:0>=1 ? a:1 : &magic
38 let bracket_is_magic = 1
41 let bracket_is_magic = 0
44 let endpos = strlen(a:pat)
47 while spos >= 0 && spos < endpos
49 let mc2 = a:pat[spos+1]
53 if mc2 == '[' && !bracket_is_magic
56 elseif mc2 =~ '[vmMV]'
58 let bracket_is_magic = mc2 =~# '[vm]'
61 let mc3 = a:pat[spos+2]
66 elseif mc1 == '[' && bracket_is_magic
71 let nextpos = matchend(a:pat, s:collection_skip_pat, spos)
73 let magpart = strpart(a:pat, spos, nextpos-spos)
75 let magpart = strpart(a:pat, spos)
78 let nextpos = match(a:pat, s:switchpat[magic_mode], spos)
83 let part = strpart(a:pat, spos, nextpos-spos)
85 let part = strpart(a:pat, spos)
88 let magpart = substitute(part, s:vmagic_items_pat, '\=s:ToggleVmagicBslash(submatch(0))', 'g')
89 elseif magic_mode ==# 'm'
91 elseif magic_mode ==# 'M'
92 let s:rem_bslash_before = '.*[~'
93 " the first two branches are only to eat the matches:
94 let magpart = substitute(part, '\\%\[\|\\_\\\=.\|\\.\|[.*[~]', '\=s:ToggleBslash(submatch(0))', 'g')
95 elseif magic_mode ==# 'V'
96 let s:rem_bslash_before = '^$.*[~'
97 let magpart = substitute(part, '\\%\[\|\\_\\\=.\|\\.\|[\^$.*[~]', '\=s:ToggleBslash(submatch(0))', 'g')
101 let result_pat .= magpart
110 " pattern to match very magic items:
111 let s:vmagic_items_pat = '\\.\|%\%([#$(UV[\^cdlouvx]\|''.\|[<>]\%(''.\|[clv]\)\)\|[&()+<=>?|]\|@\%([!=>]\|<[!=]\)\|{'
113 " not escaped - require an even number of '\' (zero or more) to the left:
114 let s:not_escaped = '\%(\%(^\|[^\\]\)\%(\\\\\)*\)\@<='
116 " prohibit an unescaped match for '%' before what follows (used when trying
117 " to find '[', but not '%[', :h /\%[ )
118 let s:not_vmagic_opt_atoms = '\%(\%(^\|[^\\]\)\%(\\\\\)*%\)\@<!'
120 " not opt atoms - (used when trying to find '[', but not '\%[')
121 let s:not_opt_atoms = '\%(\%(^\|[^\\]\)\%(\\\\\)*\\%\)\@<!'
123 " match a switch (\V,\M,\m,\v) or the start of a collection:
125 \ "v": s:not_escaped.'\%('.s:not_vmagic_opt_atoms.'\[\|\\[vmMV]\)',
126 \ "m": s:not_escaped.'\%('.s:not_opt_atoms . '\[\|\\[vmMV]\)',
127 \ "M": s:not_escaped.'\%(\\_\=\[\|\\[vmMV]\)',
128 \ "V": s:not_escaped.'\%(\\_\=\[\|\\[vmMV]\)'}
130 " skip over a collection (starting at '[' (same for all magic modes) or
131 " starting at '\_[' (same for all modes))
132 let s:collection_skip_pat = '^\%(\\_\)\=\[\^\=]\=\%(\%(\\[\^\]\-\\bertn]\|\[:\w\+:]\|[^\]]\)\@>\)*]'
136 " for magic modes 'V' and 'M'
137 func! s:ToggleBslash(patitem) "{{{
138 " {patitem} magic char or '\'.char
139 if a:patitem =~ '^.$'
142 let mchar = matchstr(a:patitem, '^\\\zs.')
143 if stridx(s:rem_bslash_before, mchar) >= 0
151 func! s:ToggleVmagicBslash(patitem) "{{{
152 " {patitem} magic char or '\'.char
153 if a:patitem =~ '^\\'
154 let mchar = a:patitem[1]
155 if mchar =~ '[\^$.*[\]~\\[:alnum:]_]'
166 let &cpo = s:cpo_save
168 " vim:ts=8:fdm=marker: