|
| 1 | + |
| 2 | +if !exists('##CmdlineChanged') |
| 3 | + \ || !exists('##CmdlineLeave') |
| 4 | + finish |
| 5 | +endif |
| 6 | + |
| 7 | +augroup eregex_incsearch_augroup |
| 8 | + autocmd! |
| 9 | + autocmd CmdlineChanged * call s:onUpdate() |
| 10 | + autocmd CmdlineLeave * call s:onLeave() |
| 11 | +augroup END |
| 12 | + |
| 13 | +if !has('nvim') |
| 14 | + function! K_eregex_incsearch_abort_cr() |
| 15 | + let s:eregex_incsearch_abort = 0 |
| 16 | + return "\<cr>" |
| 17 | + endfunction |
| 18 | + cnoremap <expr> <cr> K_eregex_incsearch_abort_cr() |
| 19 | +endif |
| 20 | + |
| 21 | +function! s:onUpdate() |
| 22 | + if getcmdtype() != ':' |
| 23 | + \ || !get(g:, 'eregex_incsearch', get(b:, 'eregex_incsearch', &incsearch)) |
| 24 | + return |
| 25 | + endif |
| 26 | + let cmd = s:cmdParse(getcmdline()) |
| 27 | + if empty(cmd) |
| 28 | + return |
| 29 | + endif |
| 30 | + let hlsearchSaved = &hlsearch |
| 31 | + set nohlsearch |
| 32 | + if !exists('s:patternSaved') |
| 33 | + let s:patternSaved = @/ |
| 34 | + if !has('nvim') |
| 35 | + let s:eregex_incsearch_abort = 1 |
| 36 | + endif |
| 37 | + endif |
| 38 | + let @/ = E2v(cmd['pattern']) |
| 39 | + if hlsearchSaved |
| 40 | + set hlsearch |
| 41 | + endif |
| 42 | + redraw |
| 43 | +endfunction |
| 44 | + |
| 45 | +function! s:onLeave() |
| 46 | + if !exists('s:patternSaved') |
| 47 | + return |
| 48 | + endif |
| 49 | + let patternSaved = s:patternSaved |
| 50 | + unlet s:patternSaved |
| 51 | + if has('nvim') |
| 52 | + let abort = get(v:event, 'abort', 0) |
| 53 | + else |
| 54 | + let abort = get(s:, 'eregex_incsearch_abort', 0) |
| 55 | + endif |
| 56 | + if abort |
| 57 | + let @/ = patternSaved |
| 58 | + redraw! |
| 59 | + endif |
| 60 | +endfunction |
| 61 | + |
| 62 | +" input: M/abc |
| 63 | +" output: { |
| 64 | +" 'method' : 'M' |
| 65 | +" 'pattern' : 'abc' |
| 66 | +" } |
| 67 | +" |
| 68 | +" input: 1,3S/abc/xyz/g |
| 69 | +" output: { |
| 70 | +" 'method' : 'S', |
| 71 | +" 'pattern' : 'abc', |
| 72 | +" } |
| 73 | +function! s:cmdParse(cmdline) |
| 74 | + let token = nr2char(127) |
| 75 | + let items = split(substitute(a:cmdline, '\\/', token, 'g'), '/') |
| 76 | + if len(items) <= 1 |
| 77 | + return {} |
| 78 | + endif |
| 79 | + let modes = get(g:, 'eregex_incsearch_modes', get(b:, 'eregex_incsearch_modes', 'MSGV')) |
| 80 | + " ^[0-9,\.\$%]*([MSGV])\/.*$ |
| 81 | + let method = substitute(items[0], '^[0-9,\.\$%]*\([' . modes . ']\)\/.*$', '\1', '') |
| 82 | + if empty(method) |
| 83 | + return {} |
| 84 | + endif |
| 85 | + return { |
| 86 | + \ 'method' : method, |
| 87 | + \ 'pattern' : substitute(items[1], token, '\\/', 'g'), |
| 88 | + \ } |
| 89 | +endfunction |
| 90 | + |
0 commit comments