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: 2013 Mar 06
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 ']')
23 " 2011 Nov 01 copied from asneeded\makemagic.vim
24 " now asneeded\nwo\makemagic.vim (comments there!)
30 let g:nwo#magic#loaded = 1
33 func! nwo#magic#MakeMagic(pat, ...) "{{{
35 " {a:1} (boolean) initial magic mode (default follows the 'magic' option)
37 if a:0>=1 ? a:1 : &magic
39 let bracket_is_magic = 1
42 let bracket_is_magic = 0
45 let endpos = strlen(a:pat)
48 while spos >= 0 && spos < endpos
50 let mc2 = a:pat[spos+1]
54 if mc2 == '[' && !bracket_is_magic
57 elseif mc2 =~ '[vmMV]'
59 let bracket_is_magic = mc2 =~# '[vm]'
62 let mc3 = a:pat[spos+2]
67 elseif mc1 == '[' && bracket_is_magic
72 let nextpos = matchend(a:pat, s:collection_skip_pat, spos)
74 let magpart = strpart(a:pat, spos, nextpos-spos)
76 let magpart = strpart(a:pat, spos)
79 let nextpos = match(a:pat, s:switchpat[magic_mode], spos)
84 let part = strpart(a:pat, spos, nextpos-spos)
86 let part = strpart(a:pat, spos)
89 let magpart = substitute(part, s:vmagic_items_pat, '\=s:ToggleVmagicBslash(submatch(0))', 'g')
90 elseif magic_mode ==# 'm'
92 elseif magic_mode ==# 'M'
93 let s:rem_bslash_before = '.*[~'
94 " the first two branches are only to eat the matches:
95 let magpart = substitute(part, '\\%\[\|\\_\\\=.\|\\.\|[.*[~]', '\=s:ToggleBslash(submatch(0))', 'g')
96 elseif magic_mode ==# 'V'
97 let s:rem_bslash_before = '^$.*[~'
98 let magpart = substitute(part, '\\%\[\|\\_\\\=.\|\\.\|[\^$.*[~]', '\=s:ToggleBslash(submatch(0))', 'g')
102 let result_pat .= magpart
111 " pattern to match very magic items:
112 let s:vmagic_items_pat = '\C\\\%(z(\|.\)\|%\%([#$(UV[\^cdlouvx]\|[<>]\=\%(''.\|\d\+[clv]\)\)\|[&()+<=>?|]\|@\%([!=>]\|<[!=]\)\|{'
114 " not escaped - require an even number of '\' (zero or more) to the left:
115 let s:not_escaped = '\%(\%(^\|[^\\]\)\%(\\\\\)*\)\@<='
117 " prohibit an unescaped match for '%' before what follows (used when trying
118 " to find '[', but not '%[', :h /\%[ )
119 let s:not_vmagic_opt_atoms = '\%(\%(^\|[^\\]\)\%(\\\\\)*%\)\@<!'
121 " not opt atoms - (used when trying to find '[', but not '\%[')
122 let s:not_opt_atoms = '\%(\%(^\|[^\\]\)\%(\\\\\)*\\%\)\@<!'
124 " match a switch (\V,\M,\m,\v) or the start of a collection:
126 \ "v": s:not_escaped.'\%('.s:not_vmagic_opt_atoms.'\[\|\\[vmMV]\)',
127 \ "m": s:not_escaped.'\%('.s:not_opt_atoms . '\[\|\\[vmMV]\)',
128 \ "M": s:not_escaped.'\%(\\_\=\[\|\\[vmMV]\)',
129 \ "V": s:not_escaped.'\%(\\_\=\[\|\\[vmMV]\)'}
131 " skip over a collection (starting at '[' (same for all magic modes) or
132 " starting at '\_[' (same for all modes))
133 let s:collection_skip_pat = '^\%(\\_\)\=\[\^\=]\=\%(\%(\\[\^\]\-\\bertn]\|\[:\w\+:]\|\[=.=]\|\[\..\.]\|[^\]]\)\@>\)*]'
137 " for magic modes 'V' and 'M'
138 func! s:ToggleBslash(patitem) "{{{
139 " {patitem} magic char or '\'.char
140 if a:patitem =~ '^.$'
143 let mchar = matchstr(a:patitem, '^\\\zs.')
144 if stridx(s:rem_bslash_before, mchar) >= 0
152 func! s:ToggleVmagicBslash(patitem) "{{{
153 " {patitem} magic char or '\'.char
154 if a:patitem =~ '^\\'
155 let mchar = a:patitem[1]
156 if mchar =~ '[\^$.*[\]~\\[:alnum:]_]'
167 let &cpo = s:cpo_save
169 " vim:ts=8:fdm=marker: