-
-
Notifications
You must be signed in to change notification settings - Fork 359
/
Copy pathEasyMotion.vim
1610 lines (1451 loc) · 54.9 KB
/
EasyMotion.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
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
scriptencoding utf-8
" EasyMotion - Vim motions on speed!
"
" Author: Kim Silkebækken <[email protected]>
" haya14busa <[email protected]>
" Source: https://github.com/easymotion/vim-easymotion
"=============================================================================
" Saving 'cpoptions' {{{
let s:save_cpo = &cpo
set cpo&vim
" }}}
let s:TRUE = !0
let s:FALSE = 0
let s:DIRECTION = { 'forward': 0, 'backward': 1, 'bidirection': 2}
" Init: {{{
let s:loaded = s:FALSE
function! EasyMotion#init()
if s:loaded
return
endif
let s:loaded = s:TRUE
call EasyMotion#highlight#load()
" Store previous motion info
let s:previous = {}
" Store previous operator-pending motion info for '.' repeat
let s:dot_repeat = {}
" Prepare 1-key Migemo Dictionary
let s:migemo_dicts = {}
let s:EasyMotion_is_active = 0
call EasyMotion#reset()
" Anywhere regular expression: {{{
let re = '\v' .
\ '(<.|^$)' . '|' .
\ '(.>|^$)' . '|' .
\ '(\l)\zs(\u)' . '|' .
\ '(_\zs.)' . '|' .
\ '(#\zs.)'
" 1. word
" 2. end of word
" 3. CamelCase
" 4. after '_' hoge_foo
" 5. after '#' hoge#foo
let g:EasyMotion_re_anywhere = get(g:, 'EasyMotion_re_anywhere', re)
" Anywhere regular expression within line:
let re = '\v' .
\ '(<.|^$)' . '|' .
\ '(.>|^$)' . '|' .
\ '(\l)\zs(\u)' . '|' .
\ '(_\zs.)' . '|' .
\ '(#\zs.)'
let g:EasyMotion_re_line_anywhere = get(g:, 'EasyMotion_re_line_anywhere', re)
"}}}
" For other plugin
let s:EasyMotion_is_cancelled = 0
" 0 -> Success
" 1 -> Cancel
let g:EasyMotion_ignore_exception = 0
return ""
endfunction
"}}}
" Reset: {{{
function! EasyMotion#reset()
let s:flag = {
\ 'within_line' : 0,
\ 'dot_repeat' : 0,
\ 'regexp' : 0,
\ 'bd_t' : 0,
\ 'find_bd' : 0,
\ 'linewise' : 0,
\ 'count_dot_repeat' : 0,
\ }
" regexp: -> regular expression
" This value is used when multi input find motion. If this values is
" 1, input text is treated as regexp.(Default: escaped)
" bd_t: -> bi-directional 't' motion
" This value is used to re-define regexp only for bi-directional 't'
" motion
" find_bd: -> bi-directional find motion
" This value is used to recheck the motion is inclusive or exclusive
" because 'f' & 't' forward find motion is inclusive, but 'F' & 'T'
" backward find motion is exclusive
" count_dot_repeat: -> dot repeat with count
" https://github.com/easymotion/vim-easymotion/issues/164
let s:current = {
\ 'is_operator' : 0,
\ 'is_search' : 0,
\ 'dot_repeat_target_cnt' : 0,
\ 'dot_prompt_user_cnt' : 0,
\ 'changedtick' : 0,
\ }
" \ 'start_position' : [],
" \ 'cursor_position' : [],
" is_operator:
" Store is_operator value first because mode(1) value will be
" changed by some operation.
" dot_* :
" These values are used when '.' repeat for automatically
" select marker/label characters.(Using count avoid recursive
" prompt)
" changedtick:
" :h b:changedtick
" This value is used to avoid side effect of overwriting buffer text
" which will change b:changedtick value. To overwrite g:repeat_tick
" value(defined tpope/vim-repeat), I can avoid this side effect of
" conflicting with tpope/vim-repeat
" start_position:
" Original, start cursor position.
" cursor_position:
" Usually, this values is same with start_position, but in
" visualmode and 'n' key motion, this value could be different.
return ""
endfunction "}}}
" Motion Functions: {{{
" -- Find Motion -------------------------
" Note: {{{
" num_strokes:
" The number of input characters. Currently provide 1, 2, or -1.
" '-1' means no limit.
" visualmode:
" Vim script couldn't detect the function is called in visual mode by
" mode(1), so tell whether it is in visual mode by argument explicitly
" direction:
" 0 -> forward
" 1 -> backward
" 2 -> bi-direction (handle forward & backward at the same time) }}}
function! EasyMotion#S(num_strokes, visualmode, direction) " {{{
if a:direction == 1
let is_inclusive = 0
else
" Note: Handle bi-direction later because 'f' motion is inclusive but
" 'F' motion is exclusive
let is_inclusive = mode(1) ==# 'no' ? 1 : 0
endif
let s:flag.find_bd = a:direction == 2 ? 1 : 0
let re = s:findMotion(a:num_strokes, a:direction)
if s:handleEmpty(re, a:visualmode) | return | endif
call s:EasyMotion(re, a:direction, a:visualmode ? visualmode() : '', is_inclusive)
return s:EasyMotion_is_cancelled
endfunction " }}}
function! EasyMotion#OverwinF(num_strokes) " {{{
let re = s:findMotion(a:num_strokes, s:DIRECTION.bidirection)
call EasyMotion#reset()
if re isnot# ''
return EasyMotion#overwin#move(re)
endif
endfunction "}}}
function! EasyMotion#T(num_strokes, visualmode, direction) " {{{
if a:direction == 1
let is_inclusive = 0
else
" Note: Handle bi-direction later because 't' motion is inclusive but
" 'T' motion is exclusive
let is_inclusive = mode(1) ==# 'no' ? 1 : 0
endif
let s:flag.find_bd = a:direction == 2 ? 1 : 0
let re = s:findMotion(a:num_strokes, a:direction)
if s:handleEmpty(re, a:visualmode) | return | endif
if a:direction == 2
let s:flag.bd_t = 1
elseif a:direction == 1
let re = s:convert_t_regexp(re, 1) " backward
else
let re = s:convert_t_regexp(re, 0) " forward
endif
call s:EasyMotion(re, a:direction, a:visualmode ? visualmode() : '', is_inclusive)
return s:EasyMotion_is_cancelled
endfunction " }}}
" -- Word Motion -------------------------
function! EasyMotion#WB(visualmode, direction) " {{{
"FIXME: inconsistent with default vim motion
"FIXED: -> EasyMotion#WBK()
let s:current.is_operator = mode(1) ==# 'no' ? 1: 0
call s:EasyMotion('\(\<.\|^$\)', a:direction, a:visualmode ? visualmode() : '', 0)
return s:EasyMotion_is_cancelled
endfunction " }}}
function! EasyMotion#WBW(visualmode, direction) " {{{
let s:current.is_operator = mode(1) ==# 'no' ? 1: 0
let regex_without_file_ends = '\v(^|\s)\zs\S|^$'
let regex = l:regex_without_file_ends
\ . (a:direction == 1 ? '' : '|%$')
\ . (a:direction == 0 ? '' : '|%^')
call s:EasyMotion(l:regex, a:direction, a:visualmode ? visualmode() : '', 0)
return s:EasyMotion_is_cancelled
endfunction " }}}
function! EasyMotion#WBK(visualmode, direction) " {{{
" vim's iskeyword style word motion
let s:current.is_operator = mode(1) ==# 'no' ? 1: 0
let regex_without_file_ends = '\v<|^\S|\s\zs\S|>\zs\S|^$'
let regex = l:regex_without_file_ends
\ . (a:direction == 1 ? '' : '|%$')
\ . (a:direction == 0 ? '' : '|%^')
call s:EasyMotion(l:regex, a:direction, a:visualmode ? visualmode() : '', 0)
return s:EasyMotion_is_cancelled
endfunction " }}}
function! EasyMotion#E(visualmode, direction) " {{{
let s:current.is_operator = mode(1) ==# 'no' ? 1: 0
let is_inclusive = mode(1) ==# 'no' ? 1 : 0
call s:EasyMotion('\(.\>\|^$\)', a:direction, a:visualmode ? visualmode() : '', is_inclusive)
return s:EasyMotion_is_cancelled
endfunction " }}}
function! EasyMotion#EW(visualmode, direction) " {{{
let s:current.is_operator = mode(1) ==# 'no' ? 1: 0
let is_inclusive = mode(1) ==# 'no' ? 1 : 0
" Note: The stopping positions for 'E' and 'gE' differs. Thus, the regex
" for direction==2 cannot be the same in both directions. This will be
" ignored.
let regex_stub = '\v\S(\s|$)'
let regex = l:regex_stub
\ . (a:direction == 0 ? '' : '|^$|%^')
\ . (a:direction == 1 ? '' : '|%$')
call s:EasyMotion(l:regex, a:direction, a:visualmode ? visualmode() : '', 0)
return s:EasyMotion_is_cancelled
endfunction " }}}
function! EasyMotion#EK(visualmode, direction) " {{{
" vim's iskeyword style word motion
let s:current.is_operator = mode(1) ==# 'no' ? 1: 0
let is_inclusive = mode(1) ==# 'no' ? 1 : 0
" Note: The stopping positions for 'e' and 'ge' differs. Thus, the regex
" for direction==2 cannot be the same in both directions. This will be
" ignored.
let regex_stub = '\v.\ze>|\S\ze\s*$|\S\ze\s|\k\zs>\S\ze|\S<'
let regex = l:regex_stub
\ . (a:direction == 0 ? '' : '|^$|%^')
\ . (a:direction == 1 ? '' : '|%$')
call s:EasyMotion(l:regex, a:direction, a:visualmode ? visualmode() : '', 0)
return s:EasyMotion_is_cancelled
endfunction " }}}
" -- JK Motion ---------------------------
function! EasyMotion#JK(visualmode, direction) " {{{
let s:current.is_operator = mode(1) ==# 'no' ? 1: 0
let s:flag.linewise = 1
if g:EasyMotion_startofline
call s:EasyMotion('^\(\w\|\s*\zs\|$\)', a:direction, a:visualmode ? visualmode() : '', 0)
else
let vcol = EasyMotion#helper#vcol('.')
let pattern = printf('^.\{-}\zs\(\%%<%dv.\%%>%dv\|$\)', vcol + 1, vcol)
call s:EasyMotion(pattern, a:direction, a:visualmode ? visualmode() : '', 0)
endif
return s:EasyMotion_is_cancelled
endfunction " }}}
function! EasyMotion#Sol(visualmode, direction) " {{{
let s:current.is_operator = mode(1) ==# 'no' ? 1: 0
let s:flag.linewise = 1
call s:EasyMotion('^\(.\|$\)', a:direction, a:visualmode ? visualmode() : '', '')
return s:EasyMotion_is_cancelled
endfunction " }}}
function! EasyMotion#Eol(visualmode, direction) " {{{
let s:flag.linewise = 1
let s:current.is_operator = mode(1) ==# 'no' ? 1: 0
call s:EasyMotion('\(\w\|\s*\zs\|.\|^\)$', a:direction, a:visualmode ? visualmode() : '', '')
return s:EasyMotion_is_cancelled
endfunction " }}}
" -- Search Motion -----------------------
function! EasyMotion#Search(visualmode, direction, respect_direction) " {{{
let s:current.is_operator = mode(1) ==# 'no' ? 1: 0
let search_direction = a:respect_direction ?
\ (a:direction == 1 ? v:searchforward : 1-v:searchforward) :
\ (a:direction)
call s:EasyMotion(@/, search_direction, a:visualmode ? visualmode() : '', 0)
return s:EasyMotion_is_cancelled
endfunction " }}}
" -- JumpToAnywhere Motion ---------------
function! EasyMotion#JumpToAnywhere(visualmode, direction) " {{{
let s:current.is_operator = mode(1) ==# 'no' ? 1: 0
call s:EasyMotion( g:EasyMotion_re_anywhere, a:direction, a:visualmode ? visualmode() : '', 0)
return s:EasyMotion_is_cancelled
endfunction " }}}
" -- Line Motion -------------------------
function! EasyMotion#SL(num_strokes, visualmode, direction) " {{{
let s:flag.within_line = 1
call EasyMotion#S(a:num_strokes, a:visualmode, a:direction)
return s:EasyMotion_is_cancelled
endfunction " }}}
function! EasyMotion#TL(num_strokes, visualmode, direction) " {{{
let s:flag.within_line = 1
call EasyMotion#T(a:num_strokes, a:visualmode, a:direction)
return s:EasyMotion_is_cancelled
endfunction " }}}
function! EasyMotion#WBL(visualmode, direction) " {{{
let s:flag.within_line = 1
call EasyMotion#WBK(a:visualmode, a:direction)
return s:EasyMotion_is_cancelled
endfunction " }}}
function! EasyMotion#EL(visualmode, direction) " {{{
let s:flag.within_line = 1
call EasyMotion#EK(a:visualmode, a:direction)
return s:EasyMotion_is_cancelled
endfunction " }}}
function! EasyMotion#LineAnywhere(visualmode, direction) " {{{
let s:flag.within_line = 1
let s:current.is_operator = mode(1) ==# 'no' ? 1: 0
let re = g:EasyMotion_re_line_anywhere
call s:EasyMotion(re, a:direction, a:visualmode ? visualmode() : '', 0)
return s:EasyMotion_is_cancelled
endfunction " }}}
" -- User Motion -------------------------
let s:config = {
\ 'pattern': '',
\ 'visualmode': s:FALSE,
\ 'direction': s:DIRECTION.forward,
\ 'inclusive': s:FALSE,
\ 'accept_cursor_pos': s:FALSE,
\ 'overwin': s:FALSE
\ }
function! s:default_config() abort
let c = copy(s:config)
let m = mode(1)
let c.inclusive = m ==# 'no' ? s:TRUE : s:FALSE
return c
endfunction
function! EasyMotion#go(...) abort
let c = extend(s:default_config(), get(a:, 1, {}))
if c.overwin
return EasyMotion#overwin#move(c.pattern)
else
let s:current.is_operator = mode(1) ==# 'no' ? 1: 0
call s:EasyMotion(c.pattern, c.direction, c.visualmode ? visualmode() : '', c.inclusive, c)
return s:EasyMotion_is_cancelled
endif
endfunction
function! EasyMotion#User(pattern, visualmode, direction, inclusive, ...) " {{{
let s:current.is_operator = mode(1) ==# 'no' ? 1: 0
let is_inclusive = mode(1) ==# 'no' ? a:inclusive : 0
let re = a:pattern
call s:EasyMotion(re, a:direction, a:visualmode ? visualmode() : '', is_inclusive, get(a:, 1, {}))
return s:EasyMotion_is_cancelled
endfunction " }}}
" -- Repeat Motion -----------------------
function! EasyMotion#Repeat(visualmode) " {{{
" Repeat previous motion with previous targets
if !has_key(s:previous, 'regexp')
call s:Message("Previous targets doesn't exist")
let s:EasyMotion_is_cancelled = 1
return s:EasyMotion_is_cancelled
endif
let re = s:previous.regexp
let direction = s:previous.direction
let s:flag.within_line = s:previous.line_flag
let s:flag.bd_t = s:previous.bd_t_flag
let s:current.is_operator = mode(1) ==# 'no' ? 1: 0
" FIXME: is_inclusive value is inappropriate but handling this value is
" difficult and priorities is low because this motion maybe used usually
" as a 'normal' motion.
let is_inclusive = mode(1) ==# 'no' ? 1 : 0
call s:EasyMotion(re, direction, a:visualmode ? visualmode() : '', is_inclusive)
return s:EasyMotion_is_cancelled
endfunction " }}}
function! EasyMotion#DotRepeat() " {{{
let cnt = v:count1 " avoid overwriting
" Repeat previous '.' motion with previous targets and operator
if !has_key(s:dot_repeat, 'regexp')
call s:Message("Previous motion doesn't exist")
let s:EasyMotion_is_cancelled = 1
return s:EasyMotion_is_cancelled
endif
let re = s:dot_repeat.regexp
let direction = s:dot_repeat.direction
let is_inclusive = s:dot_repeat.is_inclusive
for i in range(cnt)
" s:EasyMotion() always call reset s:flag & s:current
let s:flag.dot_repeat = 1
let s:flag.within_line = s:dot_repeat.line_flag
let s:flag.bd_t = s:dot_repeat.bd_t_flag
let s:current.is_operator = 1
let s:flag.count_dot_repeat = (i > 0 ? 1 : 0)
silent call s:EasyMotion(re, direction, 0, is_inclusive)
endfor
return s:EasyMotion_is_cancelled
endfunction " }}}
function! EasyMotion#NextPrevious(visualmode, direction) " {{{
" Move next/previous destination using previous motion regexp
let cnt = v:count1 " avoid overwriting
if !has_key(s:previous, 'regexp')
call s:Message("Previous targets doesn't exist")
let s:EasyMotion_is_cancelled = 1
return s:EasyMotion_is_cancelled
endif
let re = s:previous.regexp
let search_direction = (a:direction == 1 ? 'b' : '')
if g:EasyMotion_move_highlight
call EasyMotion#highlight#attach_autocmd()
call EasyMotion#highlight#add_highlight(re, g:EasyMotion_hl_move)
endif
if ! empty(a:visualmode)
" FIXME: blink highlight
silent exec 'normal! gv'
endif
" Mark jump-list
if cnt > 1
" Consider Next/Previous motions as jump motion :h jump-motion
" Note: It should add jumplist even if the count isn't given
" considering vim's default behavior of `n` & `N`, but just
" I don't want to do it without the count. Should I add a
" option?
normal! m`
endif
" Jump
" @vimlint(EVL102, 1, l:_)
for _ in range(cnt)
keepjumps call searchpos(re, search_direction)
endfor
normal! zv
call EasyMotion#reset()
" -- Activate EasyMotion ----------------- {{{
let s:EasyMotion_is_active = 1
call EasyMotion#attach_active_autocmd() "}}}
return s:EasyMotion_is_cancelled
endfunction " }}}
" }}}
" Helper Functions: {{{
" -- Message -----------------------------
function! s:Message(message) " {{{
if g:EasyMotion_verbose
echo 'EasyMotion: ' . a:message
else
" Make the current message disappear
echo ''
" redraw
endif
endfunction " }}}
function! s:Prompt(message) " {{{
echohl Question
echo a:message . ': '
echohl None
endfunction " }}}
function! s:Throw(message) "{{{
throw 'EasyMotion: ' . a:message
endfunction "}}}
" -- Save & Restore values ---------------
function! s:SaveValue() "{{{
if ! s:current.is_search
call EasyMotion#helper#VarReset('&scrolloff', 0)
endif
call EasyMotion#helper#VarReset('&modified', 0)
call EasyMotion#helper#VarReset('&modifiable', 1)
call EasyMotion#helper#VarReset('&readonly', 0)
call EasyMotion#helper#VarReset('&spell', 0)
call EasyMotion#helper#VarReset('&virtualedit', '')
" if &foldmethod !=# 'expr'
call EasyMotion#helper#VarReset('&foldmethod', 'manual')
" endif
endfunction "}}}
function! s:RestoreValue() "{{{
call EasyMotion#helper#VarReset('&scrolloff')
call EasyMotion#helper#VarReset('&modified')
call EasyMotion#helper#VarReset('&modifiable')
call EasyMotion#helper#VarReset('&readonly')
call EasyMotion#helper#VarReset('&spell')
call EasyMotion#helper#VarReset('&virtualedit')
" if &foldmethod !=# 'expr'
call EasyMotion#helper#VarReset('&foldmethod')
" endif
endfunction "}}}
function! s:turn_off_hl_error() "{{{
let s:error_hl = EasyMotion#highlight#capture('Error')
call EasyMotion#highlight#turn_off(s:error_hl)
let s:matchparen_hl = EasyMotion#highlight#capture('MatchParen')
call EasyMotion#highlight#turn_off(s:matchparen_hl)
endfunction "}}}
function! s:turn_on_hl_error() "{{{
if exists('s:error_hl')
call EasyMotion#highlight#turn_on(s:error_hl)
unlet s:error_hl
endif
if exists('s:matchparen_hl')
call EasyMotion#highlight#turn_on(s:matchparen_hl)
unlet s:matchparen_hl
endif
endfunction "}}}
" -- Draw --------------------------------
function! s:SetLines(lines, key) " {{{
for [line_num, line] in a:lines
keepjumps call setline(line_num, line[a:key])
endfor
endfunction " }}}
" -- Get characters from user input ------
function! s:GetChar(...) abort "{{{
let mode = get(a:, 1, 0)
while 1
" Workaround for https://github.com/osyo-manga/vital-over/issues/53
try
let char = call('getchar', a:000)
catch /^Vim:Interrupt$/
let char = 3 " <C-c>
endtry
if char == 27 || char == 3
" Escape or <C-c> key pressed
redraw
call s:Message('Cancelled')
return ''
endif
" Workaround for the <expr> mappings
if string(char) !=# "\x80\xfd`"
return mode == 1 ? !!char
\ : type(char) == type(0) ? nr2char(char) : char
endif
endwhile
endfunction "}}}
" -- Find Motion Helper ------------------
function! s:findMotion(num_strokes, direction) "{{{
" Find Motion: S,F,T
let s:current.is_operator = mode(1) ==# 'no' ? 1: 0
" store cursor pos because 'n' key find motion could be jump to offscreen
let s:current.original_position = [line('.'), col('.')]
let s:current.is_search = a:num_strokes == -1 ? 1: 0
let s:flag.regexp = a:num_strokes == -1 ? 1 : 0 " TODO: remove?
if g:EasyMotion_add_search_history && a:num_strokes == -1
let s:previous['input'] = @/
else
let s:previous['input'] = get(s:previous, 'input', '')
endif
let input = EasyMotion#command_line#GetInput(
\ a:num_strokes, s:previous.input, a:direction)
let s:previous['input'] = input
" Check that we have an input char
if empty(input)
return ''
endif
let re = s:convertRegep(input)
if g:EasyMotion_add_search_history && a:num_strokes == -1
let history_re = substitute(re, '\\c\|\\C', '', '')
let @/ = history_re "For textobject: 'gn'
call histadd('search', history_re)
endif
return re
endfunction "}}}
function! s:convertRegep(input) "{{{
" 1. regexp
" 2. migemo
" 3. smartsign
" 4. smartcase
let use_migemo = s:should_use_migemo(a:input)
let re = use_migemo || s:should_use_regexp() ? a:input : s:escape_regexp_char(a:input)
" Convert space to match only start of spaces
if re ==# ' '
let re = '\s\+'
endif
if use_migemo
let re = s:convertMigemo(re)
endif
if s:should_use_smartsign(a:input)
let r = s:convertSmartsign(a:input)
if use_migemo
let re = re . '\m\|' . r
else
let re = r
endif
endif
let case_flag = EasyMotion#helper#should_case_sensitive(
\ a:input, s:current.is_search) ? '\c' : '\C'
let re = case_flag . re
return re
endfunction "}}}
function! s:convertMigemo(re) "{{{
let re = a:re
if len(re) > 1
" System cmigemo
return EasyMotion#cmigemo#getMigemoPattern(re)
endif
" EasyMotion migemo one key dict
if ! has_key(s:migemo_dicts, &l:encoding)
let s:migemo_dicts[&l:encoding] = EasyMotion#helper#load_migemo_dict()
endif
return get(s:migemo_dicts[&l:encoding], re, a:re)
endfunction "}}}
function! s:convertSmartsign(chars) "{{{
" Convert given chars to smartsign string
" Example: 12 -> [1!][2@]
" a] -> a[]}]
" Load smartsign dictionary
let smart_dict = s:load_smart_dict()
" Prepare converted string
let converted_str = ''
" Get `upper_sign` for each given chars
" Split chars into list
for char in split(a:chars, '\zs')
let upper_sign = s:get_escaped_group_char(smart_dict, char)
if upper_sign ==# ''
let converted_str .= s:escape_regexp_char(char)
else
" [1!]
let converted_str .= '[' . char . upper_sign . ']'
endif
endfor
return converted_str
endfunction "}}}
function! s:get_escaped_group_char(dict, char) "{{{
" Get escaped char from given dictionary
" return '' if char is not find
" Used inside `[]`
return escape(get(a:dict, a:char, ''), '^')
endfunction "}}}
function! s:escape_regexp_char(char) "{{{
return escape(a:char, '.$^~\[]*')
endfunction "}}}
function! s:convertSmartcase(re, char) "{{{
let re = a:re
if a:char =~# '\U' "nonuppercase
return '\c' . re
else "uppercase
return '\C' . re
endif
endfunction "}}}
function! s:should_use_regexp() "{{{
return g:EasyMotion_use_regexp == 1 && s:flag.regexp == 1
endfunction "}}}
function! s:should_use_migemo(char) "{{{
if ! g:EasyMotion_use_migemo || match(a:char, '[^!-~]') != -1
return 0
endif
" TODO: use direction to improve
if s:flag.within_line == 1
let first_line = line('.')
let end_line = line('.')
else
let first_line = line('w0')
let end_line = line('w$')
endif
" Skip folded line and check if text include multibyte characters
for line in range(first_line, end_line)
if EasyMotion#helper#is_folded(line)
continue
endif
if EasyMotion#helper#include_multibyte_char(getline(line)) == 1
return 1
endif
endfor
return 0
endfunction "}}}
function! s:should_use_smartsign(char) "{{{
" Smartsign Dictionary exists?
" \A: non-alphabetic character
" Do not use smartsign for n-key find search motions
if (exists('g:EasyMotion_use_smartsign_us') ||
\ exists('g:EasyMotion_use_smartsign_jp')) &&
\ match(a:char, '\A') != -1 &&
\ exists('s:current.is_search') && s:current.is_search == 0
return 1
else
return 0
endif
endfunction "}}}
function! s:convert_t_regexp(re, direction) "{{{
if a:direction == 0 "forward
return '\_.\ze\('.a:re.'\)'
elseif a:direction == 1 "backward
return '\('.a:re.'\)\@<=\_.'
endif
endfunction "}}}
" -- Handle Visual Mode ------------------
function! s:GetVisualStartPosition(c_pos, v_start, v_end, search_direction) "{{{
let vmode = mode(1)
if vmode !~# "^[Vv\<C-v>]"
call s:Throw('Unkown visual mode:'.vmode)
endif
if vmode ==# 'V' "line-wise Visual
" Line-wise Visual {{{
if a:v_start[0] == a:v_end[0]
if a:search_direction == ''
return a:v_start
elseif a:search_direction == 'b'
return a:v_end
else
call s:throw('Unkown search_direction')
endif
else
if a:c_pos[0] == a:v_start[0]
return a:v_end
elseif a:c_pos[0] == a:v_end[0]
return a:v_start
endif
endif
"}}}
else
" Character-wise or Block-wise Visual"{{{
if a:c_pos == a:v_start
return a:v_end
elseif a:c_pos == a:v_end
return a:v_start
endif
" virtualedit
if a:c_pos[0] == a:v_start[0]
return a:v_end
elseif a:c_pos[0] == a:v_end[0]
return a:v_start
elseif EasyMotion#helper#is_greater_coords(a:c_pos, a:v_start) == 1
return a:v_end
else
return a:v_start
endif
"}}}
endif
endfunction "}}}
" -- Others ------------------------------
function! s:handleEmpty(input, visualmode) "{{{
" if empty, reselect and return 1
if empty(a:input)
if ! empty(a:visualmode)
silent exec 'normal! gv'
endif
let s:EasyMotion_is_cancelled = 1 " Cancel
return 1
endif
return 0
endfunction "}}}
function! s:load_smart_dict() "{{{
if exists('g:EasyMotion_use_smartsign_us')
return g:EasyMotion#sticky_table#us
elseif exists('g:EasyMotion_use_smartsign_jp')
return g:EasyMotion#sticky_table#jp
else
return {}
endif
endfunction "}}}
function! EasyMotion#attach_active_autocmd() "{{{
" Reference: https://github.com/justinmk/vim-sneak
augroup plugin-easymotion-active
autocmd!
autocmd InsertEnter,WinLeave,BufLeave <buffer>
\ let s:EasyMotion_is_active = 0
\ | autocmd! plugin-easymotion-active * <buffer>
autocmd CursorMoved <buffer>
\ autocmd plugin-easymotion-active CursorMoved <buffer>
\ let s:EasyMotion_is_active = 0
\ | autocmd! plugin-easymotion-active * <buffer>
augroup END
endfunction "}}}
function! EasyMotion#is_active() "{{{
return s:EasyMotion_is_active
endfunction "}}}
function! EasyMotion#activate(is_visual) "{{{
let s:EasyMotion_is_active = 1
call EasyMotion#attach_active_autocmd()
call EasyMotion#highlight#add_highlight(s:previous.regexp,
\ g:EasyMotion_hl_move)
call EasyMotion#highlight#attach_autocmd()
if a:is_visual == 1
normal! gv
endif
endfunction "}}}
function! s:restore_cursor_state(visualmode) "{{{
" -- Restore original cursor position/selection
if ! empty(a:visualmode)
silent exec 'normal! gv'
keepjumps call cursor(s:current.cursor_position)
else
keepjumps call cursor(s:current.original_position)
endif
endfunction " }}}
" Grouping Algorithms: {{{
let s:grouping_algorithms = {
\ 1: 'SCTree'
\ , 2: 'Original'
\ }
" -- Single-key/closest target priority tree {{{
" This algorithm tries to assign one-key jumps to all the targets closest to the cursor.
" It works recursively and will work correctly with as few keys as two.
function! s:GroupingAlgorithmSCTree(targets, keys) "{{{
" Prepare variables for working
let targets_len = len(a:targets)
let keys_len = len(a:keys)
let groups = {}
let keys = reverse(copy(a:keys))
" Semi-recursively count targets {{{
" We need to know exactly how many child nodes (targets) this branch will have
" in order to pass the correct amount of targets to the recursive function.
" Prepare sorted target count list {{{
" This is horrible, I know. But dicts aren't sorted in vim, so we need to
" work around that. That is done by having one sorted list with key counts,
" and a dict which connects the key with the keys_count list.
let keys_count = []
let keys_count_keys = {}
let i = 0
for key in keys
call add(keys_count, 0)
let keys_count_keys[key] = i
let i += 1
endfor
" }}}
let targets_left = targets_len
let level = 0
let i = 0
while targets_left > 0
" Calculate the amount of child nodes based on the current level
let childs_len = (level == 0 ? 1 : (keys_len - 1) )
for key in keys
" Add child node count to the keys_count array
let keys_count[keys_count_keys[key]] += childs_len
" Subtract the child node count
let targets_left -= childs_len
if targets_left <= 0
" Subtract the targets left if we added too many too
" many child nodes to the key count
let keys_count[keys_count_keys[key]] += targets_left
break
endif
let i += 1
endfor
let level += 1
endwhile
" }}}
" Create group tree {{{
let i = 0
let key = 0
call reverse(keys_count)
for key_count in keys_count
if key_count > 1
" We need to create a subgroup
" Recurse one level deeper
let groups[a:keys[key]] = s:GroupingAlgorithmSCTree(a:targets[i : i + key_count - 1], a:keys)
elseif key_count == 1
" Assign single target key
let groups[a:keys[key]] = a:targets[i]
else
" No target
continue
endif
let key += 1
let i += key_count
endfor
" }}}
" Finally!
return groups
endfunction "}}}
" }}}
" -- Original ---------------------------- {{{
function! s:GroupingAlgorithmOriginal(targets, keys)
" Split targets into groups (1 level)
let targets_len = len(a:targets)
" let keys_len = len(a:keys)
let groups = {}
let i = 0
let root_group = 0
try
while root_group < targets_len
let groups[a:keys[root_group]] = {}
for key in a:keys
let groups[a:keys[root_group]][key] = a:targets[i]
let i += 1
endfor
let root_group += 1
endwhile
catch | endtry
" Flatten the group array
if len(groups) == 1
let groups = groups[a:keys[0]]
endif
return groups
endfunction
" }}}
" -- Coord/key dictionary creation ------- {{{
function! s:CreateCoordKeyDict(groups, ...)
" Dict structure:
" 1,2 : a
" 2,3 : b
let sort_list = []
let coord_keys = {}
let group_key = a:0 == 1 ? a:1 : ''
for [key, item] in items(a:groups)
let key = group_key . key
"let key = ( ! empty(group_key) ? group_key : key)
if type(item) == type([]) " List
" Destination coords
" The key needs to be zero-padded in order to
" sort correctly
let dict_key = printf('%05d,%05d', item[0], item[1])
let coord_keys[dict_key] = key
" We need a sorting list to loop correctly in
" PromptUser, dicts are unsorted
call add(sort_list, dict_key)
else
" Item is a dict (has children)
let coord_key_dict = s:CreateCoordKeyDict(item, key)
" Make sure to extend both the sort list and the
" coord key dict
call extend(sort_list, coord_key_dict[0])
call extend(coord_keys, coord_key_dict[1])
endif
unlet item
endfor
return [sort_list, coord_keys]
endfunction
" }}}
" }}}
"}}}
" Core Functions: {{{
function! s:PromptUser(groups) "{{{
" Recursive
let group_values = values(a:groups)
" -- If only one possible match, jump directly to it {{{
if len(group_values) == 1
if mode(1) ==# 'no'
" Consider jump to first match
" NOTE: matchstr() handles multibyte characters.
let s:dot_repeat['target'] = matchstr(g:EasyMotion_keys, '^.')
endif
redraw
return group_values[0]
endif
" }}}
" -- Prepare marker lines ---------------- {{{
let lines = {}
let coord_key_dict = s:CreateCoordKeyDict(a:groups)
let prev_col_num = 0
for dict_key in sort(coord_key_dict[0])
" NOTE: {{{
" let g:EasyMotion_keys = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
" Perform <Plug>(easymotion-w)
"
" lines[line_num]['orig']:
" Lorem ipsum dolor sit amet consectetur adipisicing
"
" {target_char}:
" {L}orem {i}psum {d}olor {s}it {a}met {c}onsectetur {a}dipisicing