-
Notifications
You must be signed in to change notification settings - Fork 14
/
user.vim
587 lines (465 loc) · 17.7 KB
/
user.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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
" textobj-user - Support for user-defined text objects
" Version: 0.3.9
" Copyright (C) 2007-2010 kana <http://whileimautomaton.net/>
" License: So-called MIT/X license {{{
" Permission is hereby granted, free of charge, to any person obtaining
" a copy of this software and associated documentation files (the
" "Software"), to deal in the Software without restriction, including
" without limitation the rights to use, copy, modify, merge, publish,
" distribute, sublicense, and/or sell copies of the Software, and to
" permit persons to whom the Software is furnished to do so, subject to
" the following conditions:
"
" The above copyright notice and this permission notice shall be included
" in all copies or substantial portions of the Software.
"
" THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
" OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
" MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
" IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
" CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
" TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
" SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
" }}}
" Interfaces "{{{1
" simple "{{{2
function! textobj#user#move(pattern, flags, previous_mode)
call s:prepare_movement(a:previous_mode)
let i = v:count1
while 0 < i
let result = searchpos(a:pattern, a:flags.'W')
let i = i - 1
endwhile
return result
endfunction
" FIXME: growing the current selection like iw/aw, is/as, and others.
" FIXME: countable.
" FIXME: In a case of a:pattern matches with one character.
function! textobj#user#select(pattern, flags, previous_mode)
call s:prepare_selection(a:previous_mode)
let ORIG_POS = s:gpos_to_spos(getpos('.'))
let posf_tail = searchpos(a:pattern, 'ceW')
let posf_head = searchpos(a:pattern, 'bW')
call cursor(ORIG_POS)
let posb_head = searchpos(a:pattern, 'bcW')
let posb_tail = searchpos(a:pattern, 'eW')
" search() family with 'c' flag may not be matched to a pattern which
" matches to multiple lines. To choose appropriate range, we have to check
" another range whether it contains the cursor or not.
if (a:flags =~# 'b'
\ || (s:range_containsp(posb_head, posb_tail, ORIG_POS)
\ && s:range_validp(posb_head, posb_tail)))
let [pos_head, pos_tail] = [posb_head, posb_tail]
else
let [pos_head, pos_tail] = [posf_head, posf_tail]
endif
if s:range_validp(pos_head, pos_tail)
execute 'normal!' s:wise('v')
call cursor(pos_head)
normal! o
call cursor(pos_tail)
return [pos_head, pos_tail]
else
return s:cancel_selection(a:previous_mode, ORIG_POS)
endif
endfunction
" pair "{{{2
" FIXME: NIY, but is this necessary?
" function! textobj#user#move_pair(pattern1, pattern2, flags)
" endfunction
" BUGS: With o_CTRL-V, this may not work properly.
function! textobj#user#select_pair(pattern1, pattern2, flags, previous_mode)
call s:prepare_selection(a:previous_mode)
let ORIG_POS = s:gpos_to_spos(getpos('.'))
" adjust the cursor to the head of a:pattern2 if it's already in the range.
let pos2c_tail = searchpos(a:pattern2, 'ceW')
let pos2c_head = searchpos(a:pattern2, 'bcW')
if !s:range_validp(pos2c_head, pos2c_tail)
return s:cancel_selection(a:previous_mode, ORIG_POS)
endif
if s:range_containsp(pos2c_head, pos2c_tail, ORIG_POS)
let more_flags = 'c'
else
let more_flags = ''
call cursor(ORIG_POS)
endif
" get the positions of a:pattern1 and a:pattern2.
let pos2p_head = searchpairpos(a:pattern1, '', a:pattern2, 'W'.more_flags)
let pos2p_tail = searchpos(a:pattern2, 'ceW')
if !s:range_validp(pos2p_head, pos2p_tail)
return s:cancel_selection(a:previous_mode, ORIG_POS)
endif
call cursor(pos2p_head)
let pos1p_head = searchpairpos(a:pattern1, '', a:pattern2, 'bW')
let pos1p_tail = searchpos(a:pattern1, 'ceW')
if !s:range_validp(pos1p_head, pos1p_tail)
return s:cancel_selection(a:previous_mode, ORIG_POS)
endif
" select the range, then adjust if necessary.
if a:flags =~# 'i'
if s:range_no_text_without_edgesp(pos1p_tail, pos2p_head)
return s:cancel_selection(a:previous_mode, ORIG_POS)
endif
call s:range_select(pos1p_tail, pos2p_head)
" adjust the range.
let whichwrap_orig = &whichwrap
let &whichwrap = '<,>'
execute "normal! \<Left>o\<Right>"
let &whichwrap = whichwrap_orig
else
call s:range_select(pos1p_head, pos2p_tail)
endif
return
endfunction
function! textobj#user#define(pat0, pat1, pat2, guideline) "{{{2
let pat0 = s:rhs_escape(a:pat0)
let pat1 = s:rhs_escape(a:pat1)
let pat2 = s:rhs_escape(a:pat2)
for function_name in keys(a:guideline)
let _lhss = a:guideline[function_name]
if type(_lhss) == type('')
let lhss = [_lhss]
else
let lhss = _lhss
endif
for lhs in lhss
if function_name == 'move-to-next'
execute 'nnoremap' s:mapargs_single_move(lhs, pat0, '', 'n')
execute 'vnoremap' s:mapargs_single_move(lhs, pat0, '', 'v')
execute 'onoremap' s:mapargs_single_move(lhs, pat0, '', 'o')
elseif function_name == 'move-to-next-end'
execute 'nnoremap' s:mapargs_single_move(lhs, pat0, 'e', 'n')
execute 'vnoremap' s:mapargs_single_move(lhs, pat0, 'e', 'v')
execute 'onoremap' s:mapargs_single_move(lhs, pat0, 'e', 'o')
elseif function_name == 'move-to-prev'
execute 'nnoremap' s:mapargs_single_move(lhs, pat0, 'b', 'n')
execute 'vnoremap' s:mapargs_single_move(lhs, pat0, 'b', 'v')
execute 'onoremap' s:mapargs_single_move(lhs, pat0, 'b', 'o')
elseif function_name == 'move-to-prev-end'
execute 'nnoremap' s:mapargs_single_move(lhs, pat0, 'be', 'n')
execute 'vnoremap' s:mapargs_single_move(lhs, pat0, 'be', 'v')
execute 'onoremap' s:mapargs_single_move(lhs, pat0, 'be', 'o')
elseif function_name == 'select-next' || function_name == 'select'
execute 'vnoremap' s:mapargs_single_select(lhs, pat0, '', 'v')
execute 'onoremap' s:mapargs_single_select(lhs, pat0, '', 'o')
elseif function_name == 'select-prev'
execute 'vnoremap' s:mapargs_single_select(lhs, pat0, 'b', 'v')
execute 'onoremap' s:mapargs_single_select(lhs, pat0, 'b', 'o')
elseif function_name == 'select-pair-all'
execute 'vnoremap' s:mapargs_pair_select(lhs, pat1, pat2, 'a', 'v')
execute 'onoremap' s:mapargs_pair_select(lhs, pat1, pat2, 'a', 'o')
elseif function_name == 'select-pair-inner'
execute 'vnoremap' s:mapargs_pair_select(lhs, pat1, pat2, 'i', 'v')
execute 'onoremap' s:mapargs_pair_select(lhs, pat1, pat2, 'i', 'o')
else
throw 'Unknown function name: ' . string(function_name)
endif
endfor
endfor
endfunction
function! textobj#user#plugin(plugin_name, obj_specs) "{{{2
if a:plugin_name =~# '\L'
throw '{plugin} contains non-lowercase alphabet: ' . string(a:plugin_name)
endif
let plugin = a:plugin_name
let Plugin = substitute(a:plugin_name, '^\(\l\)', '\u\1', 0)
let g:__textobj_{plugin} = s:plugin.new(a:plugin_name, a:obj_specs)
execute
\ 'command! -bang -bar -nargs=0 Textobj'.Plugin.'DefaultKeyMappings'
\ 'call g:__textobj_'.plugin.'.define_default_key_mappings("<bang>" == "!")'
call g:__textobj_{plugin}.define_interface_key_mappings()
if (!has_key(a:obj_specs, '*no-default-key-mappings*'))
\ && (!exists('g:textobj_'.plugin.'_no_default_key_mappings'))
execute 'Textobj'.Plugin.'DefaultKeyMappings'
endif
return g:__textobj_{plugin}
endfunction
" Misc. "{{{1
" pos "{{{2
" Terms:
" gpos [bufnum, lnum, col, off] - a value returned by getpos()
" spos [lnum, col] - a value returned by searchpos()
" pos same as spos
function! s:gpos_to_spos(gpos)
return a:gpos[1:2]
endfunction
function! s:pos_headp(pos)
return a:pos[1] <= 1
endfunction
function! s:pos_lastp(pos)
return a:pos[1] == len(getline(a:pos[0]))
endfunction
function! s:pos_le(pos1, pos2) " less than or equal
return ((a:pos1[0] < a:pos2[0])
\ || (a:pos1[0] == a:pos2[0] && a:pos1[1] <= a:pos2[1]))
endfunction
" range "{{{2
function! s:range_containsp(range_head, range_tail, target_pos)
return (s:pos_le(a:range_head, a:target_pos)
\ && s:pos_le(a:target_pos, a:range_tail))
endfunction
function! s:range_no_text_without_edgesp(range_head, range_tail)
let [hl, hc] = a:range_head
let [tl, tc] = a:range_tail
return ((hl == tl && hc - tc == -1)
\ || (hl - tl == -1
\ && (s:pos_lastp(a:range_head) && s:pos_headp(a:range_tail))))
endfunction
function! s:range_validp(range_head, range_tail)
let NULL_POS = [0, 0]
return (a:range_head != NULL_POS) && (a:range_tail != NULL_POS)
endfunction
function! s:range_select(range_head, range_tail)
execute 'normal!' s:wise('v')
call cursor(a:range_head)
normal! o
call cursor(a:range_tail)
endfunction
" for textobj#user#define() "{{{2
function! s:rhs_escape(pattern)
let r = a:pattern
let r = substitute(r, '<', '<LT>', 'g')
let r = substitute(r, '|', '<Bar>', 'g')
return r
endfunction
function! s:mapargs_single_move(lhs, pattern, flags, previous_mode)
return printf('<silent> %s :<C-u>call textobj#user#move(%s, %s, %s)<CR>',
\ a:lhs,
\ string(a:pattern), string(a:flags), string(a:previous_mode))
endfunction
function! s:mapargs_single_select(lhs, pattern, flags, previous_mode)
return printf('<silent> %s :<C-u>call textobj#user#select(%s, %s, %s)<CR>',
\ a:lhs,
\ string(a:pattern), string(a:flags), string(a:previous_mode))
endfunction
function! s:mapargs_pair_select(lhs, pattern1, pattern2, flags, previous_mode)
return printf(
\ '<silent> %s :<C-u>call textobj#user#select_pair(%s,%s,%s,%s)<CR>',
\ a:lhs,
\ string(a:pattern1), string(a:pattern2),
\ string(a:flags), string(a:previous_mode)
\ )
endfunction
" for textobj#user#plugin() "{{{2
" basics "{{{3
let s:plugin = {}
function s:plugin.new(plugin_name, obj_specs)
let _ = extend({'name': a:plugin_name, 'obj_specs': a:obj_specs},
\ s:plugin, 'keep')
call _.normalize()
return _
endfunction
function s:plugin.normalize()
for [obj_name, specs] in items(self.obj_specs)
for [spec_name, spec_info] in items(specs)
if spec_name =~# '^\(move-[npNP]\|select\(\|-[ai]\)\)$'
if type(spec_info) == type('')
let specs[spec_name] = [spec_info]
endif
endif
if spec_name =~# '^\*.*-function\*$'
if spec_info =~# '^s:'
if has_key(specs, '*sfile*')
let specs[spec_name] = substitute(spec_info,
\ '^s:',
\ s:snr_prefix(specs['*sfile*']),
\ '')
else
echoerr 'Script-local function is given without *sfile*:'
\ string(spec_name) '/' string(spec_info)
endif
else
" Nothing to do.
endif
endif
unlet spec_info " to avoid E706.
endfor
endfor
endfunction
function! s:plugin.define_default_key_mappings(banged_p) "{{{3
for [obj_name, specs] in items(self.obj_specs)
for [spec_name, spec_info] in items(specs)
let rhs = self.interface_mapping_name(obj_name, spec_name)
if spec_name =~# '^\*.*\*$'
" ignore
elseif spec_name =~# '^move-[npNP]$'
for lhs in spec_info
call s:map(a:banged_p, lhs, rhs)
endfor
elseif spec_name =~# '^select\(\|-[ai]\)$'
for lhs in spec_info
call s:objmap(a:banged_p, lhs, rhs)
endfor
else
throw 'Unknown command: ' . string(spec_name)
endif
unlet spec_info " to avoid E706.
endfor
endfor
endfunction
function! s:plugin.define_interface_key_mappings() "{{{3
let RHS_PATTERN = ':<C-u>call g:__textobj_' . self.name . '.%s'
\ . '("%s", "%s", "<mode>")<Return>'
let RHS_FUNCTION = ':<C-u>call <SID>select_function_wrapper('
\ . 'g:__textobj_' . self.name . '.obj_specs["%s"]["%s"],'
\ . '"<mode>"'
\ . ')<Return>'
for [obj_name, specs] in items(self.obj_specs)
for spec_name in filter(keys(specs), 'v:val[0] != "*" && v:val[-1] != "*"')
" lhs
let lhs = '<silent> ' . self.interface_mapping_name(obj_name, spec_name)
" rhs
let _ = '*' . spec_name . '-function*'
if has_key(specs, _)
let rhs = printf(RHS_FUNCTION, obj_name, _)
elseif has_key(specs, '*pattern*')
if spec_name =~# '^move-[npNP]$'
let flags = ''
let flags .= (spec_name =~ '[pP]$' ? 'b' : '')
let flags .= (spec_name =~ '[NP]$' ? 'e' : '')
let impl_fname = 'move'
elseif spec_name ==# 'select'
let flags = ''
let impl_fname = 'select'
elseif spec_name =~# '^select-[ai]$'
let flags = ''
let flags .= (spec_name =~ 'a$' ? 'a' : '')
let flags .= (spec_name =~ 'i$' ? 'i' : '')
let impl_fname = 'select_pair'
else
echoerr 'Unknown spec:' string(spec_name)
continue
endif
let rhs = printf(RHS_PATTERN, impl_fname, obj_name, flags)
else
" skip to allow to define user's own {rhs} of the interface mapping.
continue
endif
" map
if spec_name =~# '^move'
let MapFunction = function('s:noremap')
else " spec_name =~# '^select'
let MapFunction = function('s:objnoremap')
endif
call MapFunction(1, lhs, rhs)
endfor
endfor
endfunction
function! s:plugin.interface_mapping_name(obj_name, spec_name) "{{{3
let _ = printf('<Plug>(textobj-%s-%s-%s)',
\ self.name,
\ a:obj_name,
\ substitute(a:spec_name, '^\(move\|select\)', '', ''))
let _ = substitute(_, '-\+', '-', 'g')
let _ = substitute(_, '-\ze)$', '', '')
return _
endfunction
" *pattern* implementations "{{{3
function! s:plugin.move(obj_name, flags, previous_mode)
let specs = self.obj_specs[a:obj_name]
call textobj#user#move(specs['*pattern*'], a:flags, a:previous_mode)
endfunction
function! s:plugin.select(obj_name, flags, previous_mode)
let specs = self.obj_specs[a:obj_name]
call textobj#user#select(specs['*pattern*'], a:flags, a:previous_mode)
endfunction
function! s:plugin.select_pair(obj_name, flags, previous_mode)
let specs = self.obj_specs[a:obj_name]
call textobj#user#select_pair(specs['*pattern*'][0], specs['*pattern*'][1],
\ a:flags, a:previous_mode)
endfunction
" map wrappers "{{{3
function! s:_map(map_commands, forced_p, lhs, rhs)
for _ in a:map_commands
execute 'silent!' (_) (a:forced_p ? '' : '<unique>') a:lhs
\ substitute(a:rhs, '<mode>', _[0], 'g')
endfor
endfunction
function! s:noremap(forced_p, lhs, rhs)
let v = s:proper_visual_mode(a:lhs)
call s:_map(['nnoremap', v.'noremap', 'onoremap'], a:forced_p, a:lhs, a:rhs)
endfunction
function! s:objnoremap(forced_p, lhs, rhs)
let v = s:proper_visual_mode(a:lhs)
call s:_map([v.'noremap', 'onoremap'], a:forced_p, a:lhs, a:rhs)
endfunction
function! s:map(forced_p, lhs, rhs)
let v = s:proper_visual_mode(a:lhs)
call s:_map(['nmap', v.'map', 'omap'], a:forced_p, a:lhs, a:rhs)
endfunction
function! s:objmap(forced_p, lhs, rhs)
let v = s:proper_visual_mode(a:lhs)
call s:_map([v.'map', 'omap'], a:forced_p, a:lhs, a:rhs)
endfunction
" *select-function* wrapper "{{{3
function! s:select_function_wrapper(function_name, previous_mode)
let ORIG_POS = s:gpos_to_spos(getpos('.'))
call s:prepare_selection(a:previous_mode)
let _ = function(a:function_name)()
if _ is 0
call s:cancel_selection(a:previous_mode, ORIG_POS)
else
let [motion_type, start_position, end_position] = _
execute 'normal!' s:wise(motion_type)
call setpos('.', start_position)
normal! o
call setpos('.', end_position)
endif
endfunction
" Etc "{{{2
function! s:prepare_movement(previous_mode)
if a:previous_mode ==# 'v'
normal! gv
endif
endfunction
function! s:prepare_selection(previous_mode)
if a:previous_mode ==# 'v'
execute 'normal!' "gv\<Esc>"
endif
endfunction
function! s:cancel_selection(previous_mode, orig_pos)
if a:previous_mode ==# 'v'
normal! gv
else " if a:previous_mode ==# 'o'
call cursor(a:orig_pos)
endif
endfunction
function! s:snr_prefix(sfile)
redir => result
silent scriptnames
redir END
for line in split(result, '\n')
let _ = matchlist(line, '^\s*\(\d\+\):\s*\(.*\)$')
if a:sfile ==# _[2]
return printf("\<SNR>%d_", _[1])
endif
endfor
return 's:'
endfunction
function! s:wise(default)
return (exists('v:motion_force') && v:motion_force != ''
\ ? v:motion_force
\ : a:default)
endfunction
function! s:proper_visual_mode(lhs)
" Return the mode prefix of proper "visual" mode for a:lhs key sequence.
" a:lhs should not be defined in Select mode if a:lhs starts with
" a printable character. Otherwise a:lhs may be defined in Select mode.
" a:lhs may be prefixed with :map-arguments such as <buffer>.
" It's necessary to remove them to determine the first character in a:lhs.
let s1 = substitute(
\ a:lhs,
\ '\v^(\<(buffer|silent|special|script|expr|unique)\>\s*)*',
\ '',
\ ''
\ )
" All characters in a:lhs are printable characters, so it's necessary to
" convert <>-escaped notation into corresponding characters.
let s2 = substitute(s1,
\ '^\(<[^<>]\+>\)',
\ '\=eval("\"\\" . submatch(1) . "\"")',
\ '')
return s2 =~# '^\p' ? 'x' : 'v'
endfunction
" __END__ "{{{1
" vim: foldmethod=marker