|
| 1 | + |
| 2 | +if !get(g:, 'eregex_incsearch_enable', 1) |
| 3 | + \ || !exists('##CmdlineChanged') |
| 4 | + \ || !exists('##CmdlineLeave') |
| 5 | + finish |
| 6 | +endif |
| 7 | + |
| 8 | +augroup eregex_incsearch_augroup |
| 9 | + autocmd! |
| 10 | + autocmd CmdlineChanged * call s:onUpdate() |
| 11 | + autocmd CmdlineLeave * call s:onLeave() |
| 12 | +augroup END |
| 13 | + |
| 14 | +if !has('nvim') |
| 15 | + function! K_eregex_incsearch_abort_cr() |
| 16 | + let s:eregex_incsearch_abort = 0 |
| 17 | + return "\<cr>" |
| 18 | + endfunction |
| 19 | + cnoremap <expr> <cr> K_eregex_incsearch_abort_cr() |
| 20 | +endif |
| 21 | + |
| 22 | +function! s:onUpdate() |
| 23 | + if getcmdtype() != ':' |
| 24 | + \ || !get(g:, 'eregex_incsearch', get(b:, 'eregex_incsearch', &incsearch)) |
| 25 | + return |
| 26 | + endif |
| 27 | + let cmd = s:cmdParse(getcmdline()) |
| 28 | + if empty(cmd) |
| 29 | + return |
| 30 | + endif |
| 31 | + let pattern = E2v(cmd['pattern']) |
| 32 | + |
| 33 | + if !exists('s:hlsearchSaved') |
| 34 | + let s:hlsearchSaved = &hlsearch |
| 35 | + endif |
| 36 | + if !exists('s:patternSaved') |
| 37 | + let s:patternSaved = @/ |
| 38 | + if !has('nvim') |
| 39 | + let s:eregex_incsearch_abort = 1 |
| 40 | + endif |
| 41 | + endif |
| 42 | + if !exists('s:stateSaved') |
| 43 | + let s:stateSaved = winsaveview() |
| 44 | + endif |
| 45 | + |
| 46 | + set nohlsearch |
| 47 | + |
| 48 | + if !empty(pattern) |
| 49 | + let @/ = pattern |
| 50 | + |
| 51 | + let pos = searchpos(pattern, 'cnw') |
| 52 | + if pos[0] > 0 && pos[1] > 0 |
| 53 | + if pos[1] > 1 |
| 54 | + let pos[1] -= 1 |
| 55 | + endif |
| 56 | + let curpos = getpos('.') |
| 57 | + let curpos[1] = pos[0] |
| 58 | + let curpos[2] = pos[1] |
| 59 | + call setpos('.', curpos) |
| 60 | + endif |
| 61 | + endif |
| 62 | + |
| 63 | + if s:hlsearchSaved && !empty(pattern) |
| 64 | + set hlsearch |
| 65 | + else |
| 66 | + set nohlsearch |
| 67 | + endif |
| 68 | + redraw |
| 69 | +endfunction |
| 70 | + |
| 71 | +function! s:onLeave() |
| 72 | + if exists('s:hlsearchSaved') |
| 73 | + let hlsearchSaved = s:hlsearchSaved |
| 74 | + unlet s:hlsearchSaved |
| 75 | + endif |
| 76 | + if exists('s:patternSaved') |
| 77 | + let patternSaved = s:patternSaved |
| 78 | + unlet s:patternSaved |
| 79 | + endif |
| 80 | + if exists('s:stateSaved') |
| 81 | + let stateSaved = s:stateSaved |
| 82 | + unlet s:stateSaved |
| 83 | + endif |
| 84 | + |
| 85 | + if exists('hlsearchSaved') |
| 86 | + if hlsearchSaved |
| 87 | + set hlsearch |
| 88 | + else |
| 89 | + set nohlsearch |
| 90 | + endif |
| 91 | + endif |
| 92 | + |
| 93 | + if has('nvim') |
| 94 | + let abort = get(v:event, 'abort', 0) |
| 95 | + else |
| 96 | + let abort = get(s:, 'eregex_incsearch_abort', 0) |
| 97 | + endif |
| 98 | + if !abort |
| 99 | + return |
| 100 | + endif |
| 101 | + |
| 102 | + if exists('patternSaved') |
| 103 | + let @/ = patternSaved |
| 104 | + endif |
| 105 | + if exists('stateSaved') |
| 106 | + call winrestview(stateSaved) |
| 107 | + endif |
| 108 | + |
| 109 | + redraw! |
| 110 | +endfunction |
| 111 | + |
| 112 | +" input: M/abc |
| 113 | +" output: { |
| 114 | +" 'method' : 'M' |
| 115 | +" 'pattern' : 'abc' |
| 116 | +" } |
| 117 | +" |
| 118 | +" input: 1,3S/abc/xyz/g |
| 119 | +" output: { |
| 120 | +" 'method' : 'S', |
| 121 | +" 'pattern' : 'abc', |
| 122 | +" } |
| 123 | +function! s:cmdParse(cmdline) |
| 124 | + let token = nr2char(127) |
| 125 | + let items = split(substitute(a:cmdline, '\\/', token, 'g'), '/', 1) |
| 126 | + if len(items) < 2 |
| 127 | + return {} |
| 128 | + endif |
| 129 | + let modes = get(g:, 'eregex_incsearch_modes', get(b:, 'eregex_incsearch_modes', 'MSGV')) |
| 130 | + " ^[0-9,\.\$%]*([MSGV])\/.*$ |
| 131 | + let method = substitute(items[0], '^[0-9,\.\$%]*\([' . modes . ']\)\/.*$', '\1', '') |
| 132 | + if empty(method) |
| 133 | + return {} |
| 134 | + endif |
| 135 | + return { |
| 136 | + \ 'method' : method, |
| 137 | + \ 'pattern' : substitute(get(items, 1, ''), token, '\\/', 'g'), |
| 138 | + \ } |
| 139 | +endfunction |
| 140 | + |
0 commit comments