-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathsneak.vim
403 lines (343 loc) · 15.2 KB
/
sneak.vim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
" sneak.vim - The missing motion
" Author: Justin M. Keyes
" Version: 1.7.4
" License: MIT
if exists('g:loaded_sneak_plugin') || &compatible || v:version < 700
finish
endif
let g:loaded_sneak_plugin = 1
let s:cpo_save = &cpo
set cpo&vim
"persist state for repeat
let s:st = { 'rst':1, 'input':'', 'inputlen':0, 'reverse':0, 'bounds':[0,0], 'inclusive':0 }
func! sneak#init()
unlockvar g:sneak#opt
"options v-- for backwards-compatibility
let g:sneak#opt = { 'f_reset' : get(g:, 'sneak#nextprev_f', get(g:, 'sneak#f_reset', 1))
\ ,'t_reset' : get(g:, 'sneak#nextprev_t', get(g:, 'sneak#t_reset', 1))
\ ,'s_next' : get(g:, 'sneak#s_next', 0)
\ ,'absolute_dir' : get(g:, 'sneak#absolute_dir', 0)
\ ,'textobject_z' : get(g:, 'sneak#textobject_z', 1)
\ ,'use_ic_scs' : get(g:, 'sneak#use_ic_scs', 0)
\ ,'map_netrw' : get(g:, 'sneak#map_netrw', 1)
\ ,'streak' : get(g:, 'sneak#streak', 0) && (v:version >= 703) && has("conceal")
\ ,'streak_esc' : get(g:, 'sneak#streak_esc', "\<space>")
\ ,'prompt' : get(g:, 'sneak#prompt', '>')
\ }
for k in ['f', 't'] "if user mapped f/t to Sneak, then disable f/t reset.
if maparg(k, 'n') =~# 'Sneak'
let g:sneak#opt[k.'_reset'] = 0
endif
endfor
lockvar g:sneak#opt
endf
call sneak#init()
func! sneak#state()
return deepcopy(s:st)
endf
func! sneak#is_sneaking()
return exists("#SneakPlugin#CursorMoved")
endf
func! sneak#cancel()
call sneak#hl#removehl()
augroup SneakPlugin
autocmd!
augroup END
if maparg('<esc>', 'n') =~# 'sneak#cancel' "teardown temporary <esc> mapping
silent! unmap <esc>
endif
return ''
endf
" convenience wrapper for key bindings/mappings
func! sneak#wrap(op, inputlen, reverse, inclusive, streak) abort
let cnt = v:count1 "get count before doing _anything_, else it gets overwritten.
" don't clever-repeat the last 's' search if this is an 'f' search, etc.
let is_similar_invocation = a:inputlen == s:st.inputlen && a:inclusive == s:st.inclusive
if g:sneak#opt.s_next && is_similar_invocation && (sneak#util#isvisualop(a:op) || empty(a:op)) && sneak#is_sneaking()
call sneak#rpt(a:op, a:reverse) " s goes to next match
else " s invokes new search
call sneak#to(a:op, s:getnchars(a:inputlen, a:op), a:inputlen, cnt, 0, a:reverse, a:inclusive, a:streak)
endif
endf
"repeat *motion* (not operation)
func! sneak#rpt(op, reverse) abort
if s:st.rst "reset by f/F/t/T
exec "norm! ".(sneak#util#isvisualop(a:op) ? "gv" : "").v:count1.(a:reverse ? "," : ";")
return
endif
let l:relative_reverse = (a:reverse && !s:st.reverse) || (!a:reverse && s:st.reverse)
call sneak#to(a:op, s:st.input, s:st.inputlen, v:count1, 1,
\ (g:sneak#opt.absolute_dir ? a:reverse : l:relative_reverse), s:st.inclusive, 0)
endf
" input: may be shorter than inputlen if the user pressed <enter> at the prompt.
" inclusive: 0: t-like, 1: f-like, 2: /-like
func! sneak#to(op, input, inputlen, count, repeatmotion, reverse, inclusive, streak) abort "{{{
if empty(a:input) "user canceled
if a:op ==# 'c' " user <esc> during change-operation should return to previous mode.
call feedkeys((col('.') > 1 && col('.') < col('$') ? "\<RIGHT>" : '') . "\<C-\>\<C-G>", 'n')
endif
redraw | echo '' | return
endif
let is_v = sneak#util#isvisualop(a:op)
let [curlin, curcol] = [line('.'), virtcol('.')] "initial position
let is_op = !empty(a:op) && !is_v "operator-pending invocation
let s = g:sneak#search#instance
call s.init(a:input, a:repeatmotion, a:reverse)
if is_v && a:repeatmotion
norm! gv
endif
" [count] means 'skip this many' _only_ for operators/repeat-motion/2-char-search
" sanity check: max out at 999, to avoid searchpos() OOM.
let skip = (is_op || a:repeatmotion || a:inputlen < 2) ? min([999, a:count]) : 0
let l:gt_lt = a:reverse ? '<' : '>'
let bounds = a:repeatmotion ? s:st.bounds : [0,0] " [left_bound, right_bound]
let l:scope_pattern = '' " pattern used to highlight the vertical 'scope'
let l:match_bounds = ''
"scope to a column of width 2*(v:count1) _except_ for operators/repeat-motion/1-char-search
if ((!skip && a:count > 1) || max(bounds)) && !is_op
if !max(bounds) "derive bounds from count (_logical_ bounds highlighted in 'scope')
let bounds[0] = max([0, (virtcol('.') - a:count - 1)])
let bounds[1] = a:count + virtcol('.') + 1
endif
"Match *all* chars in scope. Use \%<42v (virtual column) instead of \%<42c (byte column).
let l:scope_pattern .= '\%>'.bounds[0].'v\%<'.bounds[1].'v'
endif
if max(bounds)
"adjust logical left-bound for the _match_ pattern by -length(s) so that if _any_
"char is within the logical bounds, it is considered a match.
let l:leftbound = max([0, (bounds[0] - a:inputlen) + 1])
let l:match_bounds = '\%>'.l:leftbound.'v\%<'.bounds[1].'v'
let s.match_pattern .= l:match_bounds
endif
"TODO: refactor vertical scope calculation into search.vim,
" so this can be done in s.init() instead of here.
call s.initpattern()
let s:st.rptreverse = a:reverse
if !a:repeatmotion "this is a new (not repeat) invocation
"persist even if the search fails, because the _reverse_ direction might have a match.
let s:st.rst = 0 | let s:st.input = a:input | let s:st.inputlen = a:inputlen
let s:st.reverse = a:reverse | let s:st.bounds = bounds | let s:st.inclusive = a:inclusive
"set temporary hooks on f/F/t/T so that we know when to reset Sneak.
call s:ft_hook()
endif
if is_op && 2 != a:inclusive && !a:reverse
norm! v
endif
let nextchar = searchpos('\_.', 'n'.(s.search_options_no_s))
let nudge = !a:inclusive && a:repeatmotion && nextchar == s.dosearch('n')
if nudge
let nudge = sneak#util#nudge(!a:reverse) "special case for t
endif
for i in range(1, max([1, skip])) "jump to the [count]th match
let matchpos = s.dosearch()
if 0 == max(matchpos)
break
else
let nudge = !a:inclusive
endif
endfor
if nudge && (!is_v || max(matchpos) > 0)
call sneak#util#nudge(a:reverse) "undo nudge for t
endif
if 0 == max(matchpos)
let km = empty(&keymap) ? '' : ' ('.&keymap.' keymap)'
call sneak#util#echo('not found'.(max(bounds) ? printf(km.' (in columns %d-%d): %s', bounds[0], bounds[1], a:input) : km.': '.a:input))
return
endif
"search succeeded
call sneak#hl#removehl()
if (!is_op || a:op ==# 'y') "position _after_ search
let curlin = string(line('.'))
let curcol = string(virtcol('.') + (a:reverse ? -1 : 1))
endif
"Might as well scope to window height (+/- 99).
let l:top = max([0, line('w0')-99])
let l:bot = line('w$')+99
let l:restrict_top_bot = '\%'.l:gt_lt.curlin.'l\%>'.l:top.'l\%<'.l:bot.'l'
let l:scope_pattern .= l:restrict_top_bot
let s.match_pattern .= l:restrict_top_bot
let curln_pattern = l:match_bounds.'\%'.curlin.'l\%'.l:gt_lt.curcol.'v'
"highlight the vertical 'tunnel' that the search is scoped-to
if max(bounds) "perform the scoped highlight...
let w:sneak_sc_hl = matchadd('SneakPluginScope', l:scope_pattern)
endif
call s:attach_autocmds()
"highlight actual matches at or below the cursor position
" - store in w: because matchadd() highlight is per-window.
let w:sneak_hl_id = matchadd('SneakPluginTarget',
\ (s.prefix).(s.match_pattern).(s.search).'\|'.curln_pattern.(s.search))
"Let user deactivate with <esc>
if (has('nvim') || has('gui_running')) && maparg('<esc>', 'n') ==# ""
nmap <expr> <silent> <esc> sneak#cancel() . "\<esc>"
endif
"enter streak-mode iff there are >=2 _additional_ on-screen matches.
let target = (2 == a:streak || (a:streak && g:sneak#opt.streak && (is_op || s.hasmatches(2)))) && !max(bounds)
\ ? sneak#streak#to(s, is_v, a:reverse) : ""
if is_op || "" != target
call sneak#hl#removehl()
endif
if is_op && a:op !=# 'y'
let change = a:op !=? "c" ? "" : "\<c-r>.\<esc>"
let rpt_input = a:input . (a:inputlen > sneak#util#strlen(a:input) ? "\<cr>" : "")
silent! call repeat#set(a:op."\<Plug>SneakRepeat".a:inputlen.a:reverse.a:inclusive.(2*!empty(target)).rpt_input.target.change, a:count)
endif
endf "}}}
func! s:attach_autocmds()
augroup SneakPlugin
autocmd!
autocmd InsertEnter,WinLeave,BufLeave * call sneak#cancel()
"_nested_ autocmd to skip the _first_ CursorMoved event.
"NOTE: CursorMoved is _not_ triggered if there is typeahead during a macro/script...
autocmd CursorMoved * autocmd SneakPlugin CursorMoved * call sneak#cancel()
augroup END
endf
func! sneak#reset(key)
let c = sneak#util#getchar()
let s:st.rst = 1
let s:st.reverse = 0
for k in ['f', 't'] "unmap the temp mappings
if g:sneak#opt[k.'_reset']
silent! exec 'unmap '.k
silent! exec 'unmap '.toupper(k)
endif
endfor
"count is prepended implicitly by the <expr> mapping
return a:key.c
endf
func! s:map_reset_key(key, mode)
exec printf("%snoremap <silent> <expr> %s sneak#reset('%s')", a:mode, a:key, a:key)
endf
func! s:ft_hook() "set up temporary mappings to 'hook' into f/F/t/T
for k in ['f', 't']
for m in ['n', 'x']
"if user mapped anything to f or t, do not map over it; unfortunately this
"also means we cannot reset ; or , when f or t is invoked.
if g:sneak#opt[k.'_reset'] && maparg(k, m) ==# ''
call s:map_reset_key(k, m) | call s:map_reset_key(toupper(k), m)
endif
endfor
endfor
endf
func! s:getnchars(n, mode)
let s = ''
echo g:sneak#opt.prompt
for i in range(1, a:n)
if sneak#util#isvisualop(a:mode) | exe 'norm! gv' | endif "preserve selection
let c = sneak#util#getchar()
if -1 != index(["\<esc>", "\<c-c>", "\<backspace>", "\<del>"], c)
return ""
endif
if c == "\<CR>"
if i > 1 "special case: accept the current input (#15)
break
else "special case: repeat the last search (useful for streak-mode).
return s:st.input
endif
else
let s .= c
if 1 == &iminsert && sneak#util#strlen(s) >= a:n
"HACK: this can happen if the user entered multiple characters while we
"were waiting to resolve a multi-char keymap.
"example for keymap 'bulgarian-phonetic':
" e:: => ё | resolved, strwidth=1
" eo => eo | unresolved, strwidth=2
break
endif
endif
redraw | echo g:sneak#opt.prompt . s
endfor
return s
endf
" 2-char sneak
nnoremap <silent> <Plug>Sneak_s :<c-u>call sneak#wrap('', 2, 0, 2, 1)<cr>
nnoremap <silent> <Plug>Sneak_S :<c-u>call sneak#wrap('', 2, 1, 2, 1)<cr>
xnoremap <silent> <Plug>Sneak_s :<c-u>call sneak#wrap(visualmode(), 2, 0, 2, 1)<cr>
xnoremap <silent> <Plug>Sneak_S :<c-u>call sneak#wrap(visualmode(), 2, 1, 2, 1)<cr>
onoremap <silent> <Plug>Sneak_s :<c-u>call sneak#wrap(v:operator, 2, 0, 2, 1)<cr>
onoremap <silent> <Plug>Sneak_S :<c-u>call sneak#wrap(v:operator, 2, 1, 2, 1)<cr>
onoremap <silent> <Plug>SneakRepeat :<c-u>call sneak#wrap(v:operator, sneak#util#getc(), sneak#util#getc(), sneak#util#getc(), sneak#util#getc())<cr>
" explicit repeat (as opposed to 'clever-s' implicit repeat)
nnoremap <silent> <Plug>SneakNext :<c-u>call sneak#rpt('', 0)<cr>
nnoremap <silent> <Plug>SneakPrevious :<c-u>call sneak#rpt('', 1)<cr>
xnoremap <silent> <Plug>SneakNext :<c-u>call sneak#rpt(visualmode(), 0)<cr>
xnoremap <silent> <Plug>SneakPrevious :<c-u>call sneak#rpt(visualmode(), 1)<cr>
onoremap <silent> <Plug>SneakNext :<c-u>call sneak#rpt(v:operator, 0)<cr>
onoremap <silent> <Plug>SneakPrevious :<c-u>call sneak#rpt(v:operator, 1)<cr>
if g:sneak#opt.textobject_z
omap z <Plug>Sneak_s
omap Z <Plug>Sneak_S
endif
" 1-char 'enhanced f' sneak
nnoremap <silent> <Plug>Sneak_f :<c-u>call sneak#wrap('', 1, 0, 1, 0)<cr>
nnoremap <silent> <Plug>Sneak_F :<c-u>call sneak#wrap('', 1, 1, 1, 0)<cr>
xnoremap <silent> <Plug>Sneak_f :<c-u>call sneak#wrap(visualmode(), 1, 0, 1, 0)<cr>
xnoremap <silent> <Plug>Sneak_F :<c-u>call sneak#wrap(visualmode(), 1, 1, 1, 0)<cr>
onoremap <silent> <Plug>Sneak_f :<c-u>call sneak#wrap(v:operator, 1, 0, 1, 0)<cr>
onoremap <silent> <Plug>Sneak_F :<c-u>call sneak#wrap(v:operator, 1, 1, 1, 0)<cr>
" 1-char 'enhanced t' sneak
nnoremap <silent> <Plug>Sneak_t :<c-u>call sneak#wrap('', 1, 0, 0, 0)<cr>
nnoremap <silent> <Plug>Sneak_T :<c-u>call sneak#wrap('', 1, 1, 0, 0)<cr>
xnoremap <silent> <Plug>Sneak_t :<c-u>call sneak#wrap(visualmode(), 1, 0, 0, 0)<cr>
xnoremap <silent> <Plug>Sneak_T :<c-u>call sneak#wrap(visualmode(), 1, 1, 0, 0)<cr>
onoremap <silent> <Plug>Sneak_t :<c-u>call sneak#wrap(v:operator, 1, 0, 0, 0)<cr>
onoremap <silent> <Plug>Sneak_T :<c-u>call sneak#wrap(v:operator, 1, 1, 0, 0)<cr>
nnoremap <silent> <Plug>(SneakStreak) :<c-u>call sneak#wrap('', 2, 0, 2, 2)<cr>
nnoremap <silent> <Plug>(SneakStreakBackward) :<c-u>call sneak#wrap('', 2, 1, 2, 2)<cr>
xnoremap <silent> <Plug>(SneakStreak) :<c-u>call sneak#wrap(visualmode(), 2, 0, 2, 2)<cr>
xnoremap <silent> <Plug>(SneakStreakBackward) :<c-u>call sneak#wrap(visualmode(), 2, 1, 2, 2)<cr>
onoremap <silent> <Plug>(SneakStreak) :<c-u>call sneak#wrap(v:operator, 2, 0, 2, 2)<cr>
onoremap <silent> <Plug>(SneakStreakBackward) :<c-u>call sneak#wrap(v:operator, 2, 1, 2, 2)<cr>
if !hasmapto('<Plug>SneakForward') && !hasmapto('<Plug>Sneak_s', 'n') && mapcheck('s', 'n') ==# ''
nmap s <Plug>Sneak_s
endif
if !hasmapto('<Plug>SneakBackward') && !hasmapto('<Plug>Sneak_S', 'n') && mapcheck('S', 'n') ==# ''
nmap S <Plug>Sneak_S
endif
if !hasmapto('<Plug>SneakNext', 'n') && mapcheck(';', 'n') ==# ''
nmap ; <Plug>SneakNext
omap ; <Plug>SneakNext
xmap ; <Plug>SneakNext
endif
if !hasmapto('<Plug>SneakPrevious', 'n')
if mapcheck(',', 'n') ==# ''
nmap , <Plug>SneakPrevious
omap , <Plug>SneakPrevious
xmap , <Plug>SneakPrevious
elseif mapcheck('\', 'n') ==# '' || mapcheck('\', 'n') ==# ','
nmap \ <Plug>SneakPrevious
omap \ <Plug>SneakPrevious
xmap \ <Plug>SneakPrevious
endif
endif
if !hasmapto('<Plug>VSneakForward') && !hasmapto('<Plug>Sneak_s', 'v') && mapcheck('s', 'x') ==# ''
xmap s <Plug>Sneak_s
endif
if !hasmapto('<Plug>VSneakBackward') && !hasmapto('<Plug>Sneak_S', 'v') && mapcheck('Z', 'x') ==# ''
xmap Z <Plug>Sneak_S
endif
" redundant legacy mappings for backwards compatibility (must come _after_ the hasmapto('<Plug>Sneak_S') checks above)
nmap <Plug>SneakForward <Plug>Sneak_s
nmap <Plug>SneakBackward <Plug>Sneak_S
xmap <Plug>VSneakForward <Plug>Sneak_s
xmap <Plug>VSneakBackward <Plug>Sneak_S
xmap <Plug>VSneakNext <Plug>SneakNext
xmap <Plug>VSneakPrevious <Plug>SneakPrevious
if g:sneak#opt.map_netrw && -1 != stridx(maparg("s", "n"), "Sneak")
func! s:map_netrw_key(key)
let expanded_map = maparg(a:key,'n')
if !strlen(expanded_map) || expanded_map =~# '_Net\|FileBeagle'
if strlen(expanded_map) > 0 "else, mapped to <nop>
silent exe (expanded_map =~# '<Plug>' ? 'nmap' : 'nnoremap').' <buffer> <silent> <leader>'.a:key.' '.expanded_map
endif
"unmap the default buffer-local mapping to allow Sneak's global mapping.
silent! exe 'nunmap <buffer> '.a:key
endif
endf
augroup SneakPluginNetrw
autocmd!
autocmd FileType netrw,filebeagle autocmd SneakPluginNetrw CursorMoved <buffer>
\ call <sid>map_netrw_key('s') | call <sid>map_netrw_key('S') | autocmd! SneakPluginNetrw * <buffer>
augroup END
endif
let &cpo = s:cpo_save
unlet s:cpo_save