forked from guillermooo/Vintageous
/
motion_cmds.py
1159 lines (895 loc) · 42.9 KB
/
motion_cmds.py
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
import sublime
import sublime_plugin
from Vintageous.vi.constants import regions_transformer
from Vintageous.vi.constants import MODE_VISUAL, MODE_NORMAL, _MODE_INTERNAL_NORMAL
from Vintageous.vi.constants import MODE_VISUAL_LINE
from Vintageous.state import VintageState, IrreversibleTextCommand
from Vintageous.vi import utils
from Vintageous.vi.search import reverse_search
from Vintageous.vi.search import reverse_search_by_pt
from Vintageous.vi.search import find_in_range
from Vintageous.vi.search import find_wrapping
from Vintageous.vi.search import reverse_find_wrapping
import Vintageous.state
from itertools import chain
class ViMoveToHardBol(sublime_plugin.TextCommand):
def run(self, edit, extend=False):
sels = list(self.view.sel())
self.view.sel().clear()
new_sels = []
for s in sels:
hard_bol = self.view.line(s.b).begin()
if s.a < s.b and (self.view.line(s.a) != self.view.line(s.b)) and self.view.full_line(hard_bol - 1).b == hard_bol:
hard_bol += 1
a, b = (hard_bol, hard_bol) if not extend else (s.a, hard_bol)
# Avoid ending up with a en empty selection while on visual mode.
if extend and s.a == hard_bol:
b = b + 1
new_sels.append(sublime.Region(a, b))
for s in new_sels:
self.view.sel().add(s)
# FIXME: Only find exact char counts. Vim ignores the command when the count is larger than the
# number of instances of the sought character.
class ViFindInLineInclusive(sublime_plugin.TextCommand):
def run(self, edit, extend=False, character=None, mode=None, count=1):
def f(view, s):
eol = view.line(s.b).end()
if not s.empty():
eol = view.line(s.b - 1).end()
match = s
for i in range(count):
# Define search range as 'rest of the line to the right'.
if state.mode != MODE_VISUAL:
search_range = sublime.Region(min(match.b + 1, eol), eol)
else:
search_range = sublime.Region(min(match.b, eol), eol)
match = find_in_range(view, character,
search_range.a,
search_range.b,
sublime.LITERAL)
# Count too high or simply no match; break.
if match is None:
match = s
break
if state.mode == MODE_VISUAL or mode == _MODE_INTERNAL_NORMAL:
if match == s:
# FIXME: It won't blink because the current light can't be highlighted right
# now (we are in command mode and there is a selection on the screen. Perhaps
# we can make the gutter blink instead.)
utils.blink()
return sublime.Region(s.a, match.b)
if match == s:
utils.blink()
return sublime.Region(match.a, match.a)
# TODO: Give feedback to the user that the search failed?
if character is None:
return
else:
state = VintageState(self.view)
state.last_character_search = character
regions_transformer(self.view, f)
# FIXME: Only find exact char counts. Vim ignores the command when the count is larger than the
# number of instances of the sought character.
class ViReverseFindInLineInclusive(sublime_plugin.TextCommand):
def run(self, edit, extend=False, character=None, mode=None, count=1):
def f(view, s):
# TODO: Refactor this mess.
line_text = view.substr(sublime.Region(view.line(s.b).a, s.b))
offset = 0
a, b = view.line(s.b).a, s.b
final_offset = -1
try:
for i in range(count):
line_text = view.substr(sublime.Region(a, b))
match_in_line = line_text.rindex(character)
final_offset = match_in_line
b = view.line(s.a).a + final_offset
except ValueError:
pass
if final_offset > -1:
pt = view.line(s.b).a + final_offset
state = VintageState(view)
if state.mode == MODE_VISUAL or mode == _MODE_INTERNAL_NORMAL:
if sublime.Region(s.b, pt) == s:
utils.blink()
return s
return sublime.Region(s.a, pt)
if pt == s.b:
utils.blink()
return s
return sublime.Region(pt, pt)
return s
if character is None:
return
else:
state = VintageState(self.view)
state.last_character_search = character
regions_transformer(self.view, f)
class ViFindInLineExclusive(sublime_plugin.TextCommand):
"""Contrary to *f*, *t* does not look past the caret's position, so if ``character`` is under
the caret, nothing happens.
"""
def run(self, edit, extend=False, character=None, mode=None, count=1):
def f(view, s):
eol = view.line(s.b).end()
if not s.empty():
eol = view.line(s.b - 1).end()
match = s
offset = 1 if count > 1 else 0
for i in range(count):
# Define search range as 'rest of the line to the right'.
if state.mode != MODE_VISUAL:
search_range = sublime.Region(min(match.b + 1 + offset, eol), eol)
else:
search_range = sublime.Region(min(match.b + offset, eol), eol)
match = find_in_range(view, character,
search_range.a,
search_range.b,
sublime.LITERAL)
# Count too high or simply no match; break.
if match is None:
match = s
break
if state.mode == MODE_VISUAL or mode == _MODE_INTERNAL_NORMAL:
if match == s:
# FIXME: It won't blink because the current light can't be highlighted right
# now (we are in command mode and there is a selection on the screen. Perhaps
# we can make the gutter blink instead.)
utils.blink()
return s
return sublime.Region(s.a, match.b - 1)
if match == s:
utils.blink()
return s
return sublime.Region(match.a - 1, match.a - 1)
# TODO: Give feedback to the user that the search failed?
if character is None:
return
else:
state = VintageState(self.view)
state.last_character_search = character
regions_transformer(self.view, f)
class ViReverseFindInLineExclusive(sublime_plugin.TextCommand):
"""Contrary to *F*, *T* does not look past the caret's position, so if ``character`` is right
before the caret, nothing happens.
"""
def run(self, edit, extend=False, character=None, mode=None, count=1):
def f(view, s):
line_text = view.substr(sublime.Region(view.line(s.b).a, s.b))
a, b = view.line(s.b).a, s.b
final_offset = -1
try:
for i in range(count):
line_text = view.substr(sublime.Region(a, b))
match_in_line = line_text.rindex(character)
final_offset = match_in_line
b = view.line(s.a).a + final_offset
except ValueError:
pass
if final_offset > -1:
pt = view.line(s.b).a + final_offset
state = VintageState(view)
if state.mode == MODE_VISUAL or mode == _MODE_INTERNAL_NORMAL:
return sublime.Region(s.a, pt + 1)
return sublime.Region(pt + 1, pt + 1)
return s
if character is None:
return
else:
state = VintageState(self.view)
state.last_character_search = character
regions_transformer(self.view, f)
class ViGoToLine(sublime_plugin.TextCommand):
def run(self, edit, extend=False, line=None):
line = line if line > 0 else 1
dest = self.view.text_point(line - 1, 0)
def f(view, s):
if not extend:
return sublime.Region(dest, dest)
else:
return sublime.Region(s.a, dest)
regions_transformer(self.view, f)
# FIXME: Bringing the selections into view will be undesirable in many cases. Maybe we
# should have an optional .scroll_selections_into_view() step during command execution.
self.view.show(self.view.sel()[0])
class ViPercent(sublime_plugin.TextCommand):
# TODO: Perhaps truly support multiple regions here?
pairs = (
('(', ')'),
('[', ']'),
('{', '}'),
)
def run(self, edit, extend=False, percent=None, mode=None):
if percent == None:
def move_to_bracket(view, s):
def find_bracket_location(pt):
bracket, brackets, bracket_pt = self.find_a_bracket(pt)
if not bracket:
return
if bracket == brackets[0]:
return self.find_balanced_closing_bracket(bracket_pt + 1, brackets)
else:
return self.find_balanced_opening_bracket(bracket_pt, brackets)
if mode == MODE_VISUAL:
# TODO: Improve handling of s.a < s.b and s.a > s.b cases.
a = find_bracket_location(s.b - 1)
if a is not None:
a = a + 1 if a > s.b else a
if a == s.a:
a += 1
return sublime.Region(s.a, a)
elif mode == MODE_NORMAL:
a = find_bracket_location(s.b)
if a is not None:
return sublime.Region(a, a)
# TODO: According to Vim we must swallow brackets in this case.
elif mode == _MODE_INTERNAL_NORMAL:
a = find_bracket_location(s.b)
if a is not None:
return sublime.Region(s.a, a)
return s
regions_transformer(self.view, move_to_bracket)
return
row = self.view.rowcol(self.view.size())[0] * (percent / 100)
def f(view, s):
pt = view.text_point(row, 0)
return sublime.Region(pt, pt)
regions_transformer(self.view, f)
# FIXME: Bringing the selections into view will be undesirable in many cases. Maybe we
# should have an optional .scroll_selections_into_view() step during command execution.
self.view.show(self.view.sel()[0])
def find_a_bracket(self, caret_pt):
"""Locates the next bracket after the caret in the current line.
If None is found, execution must be aborted.
Returns: (bracket, brackets, bracket_pt)
Example: ('(', ('(', ')'), 1337))
"""
caret_row, caret_col = self.view.rowcol(caret_pt)
line_text = self.view.substr(sublime.Region(caret_pt,
self.view.line(caret_pt).b))
try:
found_brackets = min([(line_text.index(bracket), bracket)
for bracket in chain(*self.pairs)
if bracket in line_text])
except ValueError:
return None, None, None
bracket_a, bracket_b = [(a, b) for (a, b) in self.pairs
if found_brackets[1] in (a, b)][0]
return (found_brackets[1], (bracket_a, bracket_b),
self.view.text_point(caret_row, caret_col + found_brackets[0]))
def find_balanced_closing_bracket(self, start, brackets, unbalanced=0):
new_start = start
for i in range(unbalanced or 1):
next_closing_bracket = find_in_range(self.view, brackets[1],
start=new_start,
end=self.view.size(),
flags=sublime.LITERAL)
if next_closing_bracket is None:
# Unbalanced brackets; nothing we can do.
return
new_start = next_closing_bracket.end()
nested = 0
while True:
next_opening_bracket = find_in_range(self.view, brackets[0],
start=start,
end=next_closing_bracket.end(),
flags=sublime.LITERAL)
if not next_opening_bracket:
break
nested += 1
start = next_opening_bracket.end()
if nested > 0:
return self.find_balanced_closing_bracket(next_closing_bracket.end(),
brackets, nested)
else:
return next_closing_bracket.begin()
def find_balanced_opening_bracket(self, start, brackets, unbalanced=0):
new_start = start
for i in range(unbalanced or 1):
prev_opening_bracket = reverse_search_by_pt(self.view, brackets[0],
start=0,
end=new_start,
flags=sublime.LITERAL)
if prev_opening_bracket is None:
# Unbalanced brackets; nothing we can do.
return
new_start = prev_opening_bracket.begin()
nested = 0
while True:
next_closing_bracket = reverse_search_by_pt(self.view, brackets[1],
start=prev_opening_bracket.a,
end=start,
flags=sublime.LITERAL)
if not next_closing_bracket:
break
nested += 1
start = next_closing_bracket.begin()
if nested > 0:
return self.find_balanced_opening_bracket(prev_opening_bracket.begin(),
brackets,
nested)
else:
return prev_opening_bracket.begin()
class _vi_big_h(sublime_plugin.TextCommand):
def run(self, edit, count=None, extend=False, mode=None):
def f(view, s):
if mode == MODE_NORMAL:
return sublime.Region(target, target)
elif mode == _MODE_INTERNAL_NORMAL:
return sublime.Region(s.a + 1, target)
elif mode == MODE_VISUAL:
new_target = utils.next_non_white_space_char(view, target)
return sublime.Region(s.a + 1, new_target)
else:
return s
r = self.view.visible_region()
row, _ = self.view.rowcol(r.a)
row += count + 1
target = self.view.text_point(row, 0)
regions_transformer(self.view, f)
self.view.show(target)
class ViBigL(sublime_plugin.TextCommand):
def run(self, edit, count=None, extend=False, mode=None):
def f(view, s):
if mode == MODE_NORMAL:
return sublime.Region(target, target)
elif mode == _MODE_INTERNAL_NORMAL:
if s.b >= target:
return sublime.Region(s.a + 1, target)
return sublime.Region(s.a, target)
elif mode == MODE_VISUAL:
if s.b >= target:
new_target = utils.next_non_white_space_char(view, target)
return sublime.Region(s.a + 1, new_target)
new_target = utils.next_non_white_space_char(view, target)
return sublime.Region(s.a, new_target + 1)
else:
return s
r = self.view.visible_region()
row, _ = self.view.rowcol(r.b)
row -= count + 1
# XXXX: Subtract 1 so that Sublime Text won't attempt to scroll the line into view, which
# would be quite annoying.
target = self.view.text_point(row - 1, 0)
regions_transformer(self.view, f)
self.view.show(target)
class ViBigM(sublime_plugin.TextCommand):
def run(self, edit, count=None, extend=False, mode=None):
def f(view, s):
if mode == MODE_NORMAL:
return sublime.Region(target, target)
elif mode == _MODE_INTERNAL_NORMAL:
if s.b >= target:
return sublime.Region(s.a + 1, target)
return sublime.Region(s.a, target)
elif mode == MODE_VISUAL:
if s.b >= target:
new_target = utils.next_non_white_space_char(view, target)
return sublime.Region(s.a + 1, new_target)
new_target = utils.next_non_white_space_char(view, target)
return sublime.Region(s.a, new_target + 1)
else:
return s
r = self.view.visible_region()
row_a, _ = self.view.rowcol(r.a)
row_b, _ = self.view.rowcol(r.b)
row = ((row_a + row_b) / 2)
target = self.view.text_point(row, 0)
regions_transformer(self.view, f)
self.view.show(target)
class ViStar(sublime_plugin.TextCommand):
def run(self, edit, mode=None, extend=False, exact_word=True):
def f(view, s):
if exact_word:
pattern = r'\b{0}\b'.format(query)
else:
pattern = query
flags = sublime.IGNORECASE
if mode == _MODE_INTERNAL_NORMAL:
match = find_wrapping(view,
term=pattern,
start=view.word(s.end()).end(),
end=view.size(),
flags=0,
times=1)
else:
match = find_wrapping(view,
term=pattern,
start=view.word(s.end()).end(),
end=view.size(),
flags=0,
times=1)
if match:
if mode == _MODE_INTERNAL_NORMAL:
return sublime.Region(s.a, match.begin())
elif state.mode == MODE_VISUAL:
return sublime.Region(s.a, match.begin())
elif state.mode == MODE_NORMAL:
return sublime.Region(match.begin(), match.begin())
return s
state = VintageState(self.view)
# TODO: make sure we swallow any leading white space.
query = self.view.substr(self.view.word(self.view.sel()[0].end()))
if query:
state.last_buffer_search = query
regions_transformer(self.view, f)
class ViOctothorp(sublime_plugin.TextCommand):
def run(self, edit, mode=None, extend=False, exact_word=True):
def f(view, s):
if exact_word:
pattern = r'\b{0}\b'.format(query)
else:
pattern = query
flags = sublime.IGNORECASE
if mode == _MODE_INTERNAL_NORMAL:
match = reverse_find_wrapping(view,
term=pattern,
start=0,
end=current_sel.a,
flags=0,
times=1)
else:
match = reverse_find_wrapping(view,
term=pattern,
start=0,
end=current_sel.a,
flags=0,
times=1)
if match:
if mode == _MODE_INTERNAL_NORMAL:
return sublime.Region(s.b, match.begin())
elif state.mode == MODE_VISUAL:
return sublime.Region(s.b, match.begin())
elif state.mode == MODE_NORMAL:
return sublime.Region(match.begin(), match.begin())
return s
state = VintageState(self.view)
# TODO: make sure we swallow any leading white space.
query = self.view.substr(self.view.word(self.view.sel()[0].end()))
if query:
state.last_buffer_search = query
current_sel = self.view.sel()[0]
regions_transformer(self.view, f)
class ViBufferSearch(IrreversibleTextCommand):
def run(self):
Vintageous.state._dont_reset_during_init = True
state = VintageState(self.view)
on_change = self.on_change if state.settings.vi['incsearch'] else None
self.view.window().show_input_panel('', '', self.on_done, on_change, self.on_cancel)
def on_done(self, s):
self.view.erase_regions('vi_inc_search')
state = VintageState(self.view)
state.motion = 'vi_forward_slash'
state.user_input = s
# Equivalent to /<CR>, which must repeat the last search.
if s == '':
state.user_input = state.last_buffer_search
if s != '':
state.last_buffer_search = s
state.eval()
def on_change(self, s):
self.view.erase_regions('vi_inc_search')
state = VintageState(self.view)
next_hit = find_wrapping(self.view,
term=s,
start=self.view.sel()[0].b + 1,
end=self.view.size(),
flags=0,
times=state.count)
if next_hit:
if state.mode == MODE_VISUAL:
next_hit = sublime.Region(self.view.sel()[0].a, next_hit.a + 1)
self.view.add_regions('vi_inc_search', [next_hit], 'comment', '')
if not self.view.visible_region().contains(next_hit.b):
self.view.show(next_hit.b)
def on_cancel(self):
self.view.erase_regions('vi_inc_search')
state = VintageState(self.view)
state.reset()
if not self.view.visible_region().contains(self.view.sel()[0]):
self.view.show(self.view.sel()[0])
class ViBufferReverseSearch(IrreversibleTextCommand):
def run(self):
Vintageous.state._dont_reset_during_init = True
state = VintageState(self.view)
on_change = self.on_change if state.settings.vi['incsearch'] else None
self.view.window().show_input_panel('', '', self.on_done, on_change, self.on_cancel)
def on_done(self, s):
self.view.erase_regions('vi_inc_search')
state = VintageState(self.view)
state.motion = 'vi_question_mark'
state.user_input = s
# Equivalent to ?<CR>, which must repeat the last search.
if s == '':
state.user_input = state.last_buffer_search
if s != '':
state.last_buffer_search = s
state.eval()
def on_change(self, s):
self.view.erase_regions('vi_inc_search')
state = VintageState(self.view)
occurrence = reverse_find_wrapping(self.view,
term=s,
start=0,
end=self.view.sel()[0].b,
flags=0,
times=state.count)
if occurrence:
if state.mode == MODE_VISUAL:
occurrence = sublime.Region(self.view.sel()[0].a, occurrence.a)
self.view.add_regions('vi_inc_search', [occurrence], 'comment', '')
if not self.view.visible_region().contains(occurrence):
self.view.show(occurrence)
def on_cancel(self):
self.view.erase_regions('vi_inc_search')
state = VintageState(self.view)
state.reset()
if not self.view.visible_region().contains(self.view.sel()[0]):
self.view.show(self.view.sel()[0])
class _vi_forward_slash(sublime_plugin.TextCommand):
def hilite(self, pattern):
# TODO: Implement smartcase?
flags = sublime.IGNORECASE
regs = self.view.find_all(pattern, flags)
if not regs:
self.view.erase_regions('vi_search')
return
state = VintageState(self.view)
if not state.settings.vi['hlsearch']:
return
self.view.add_regions('vi_search', regs, 'comment', '', sublime.DRAW_NO_FILL)
def run(self, edit, search_string, mode=None, count=1, extend=False):
def f(view, s):
if mode == MODE_VISUAL:
return sublime.Region(s.a, match.a + 1)
elif mode == _MODE_INTERNAL_NORMAL:
return sublime.Region(s.a, match.a)
elif mode == MODE_NORMAL:
return sublime.Region(match.a, match.a)
return s
# This happens when we attempt to repeat the search and there's no search term stored yet.
if search_string is None:
return
# We want to start searching right after the current selection.
current_sel = self.view.sel()[0]
start = current_sel.b if not current_sel.empty() else current_sel.b + 1
wrapped_end = self.view.size()
# TODO: What should we do here? Case-sensitive or case-insensitive search? Configurable?
# Search wrapping around the end of the buffer.
match = find_wrapping(self.view, search_string, start, wrapped_end, flags=0, times=count)
if not match:
return
regions_transformer(self.view, f)
self.hilite(search_string)
class _vi_question_mark(sublime_plugin.TextCommand):
def hilite(self, pattern):
# TODO: Implement smartcase?
flags = sublime.IGNORECASE
regs = self.view.find_all(pattern, flags)
if not regs:
self.view.erase_regions('vi_search')
return
state = VintageState(self.view)
if not state.settings.vi['hlsearch']:
return
self.view.add_regions('vi_search', regs, 'comment', '', sublime.DRAW_NO_FILL)
def run(self, edit, search_string, mode=None, count=1, extend=False):
def f(view, s):
# FIXME: Readjust carets if we searched for '\n'.
if mode == MODE_VISUAL:
return sublime.Region(s.end(), found.a)
elif mode == _MODE_INTERNAL_NORMAL:
return sublime.Region(s.end(), found.a)
elif mode == MODE_NORMAL:
return sublime.Region(found.a, found.a)
return s
# This happens when we attempt to repeat the search and there's no search term stored yet.
if search_string is None:
return
# FIXME: What should we do here? Case-sensitive or case-insensitive search? Configurable?
found = reverse_find_wrapping(self.view,
term=search_string,
start=0,
end=self.view.sel()[0].b,
flags=0,
times=count)
if not found:
print("Vintageous: Pattern not found.")
return
regions_transformer(self.view, f)
self.hilite(search_string)
class _vi_right_brace(sublime_plugin.TextCommand):
def run(self, edit, mode=None, extend=False):
def f(view, s):
# TODO: must skip empty paragraphs.
start = utils.next_non_white_space_char(view, s.b, white_space='\n \t')
par_as_region = view.expand_by_class(start, sublime.CLASS_EMPTY_LINE)
if mode == MODE_NORMAL:
min_pt = max(0, min(par_as_region.b, view.size() - 1))
return sublime.Region(min_pt, min_pt)
elif mode == MODE_VISUAL:
return sublime.Region(s.a, par_as_region.b + 1)
elif mode == _MODE_INTERNAL_NORMAL:
return sublime.Region(s.a, par_as_region.b - 1)
elif mode == MODE_VISUAL_LINE:
if s.a <= s.b:
return sublime.Region(s.a, par_as_region.b + 1)
else:
if par_as_region.b > s.a:
return sublime.Region(view.line(s.a - 1).a, par_as_region.b + 1)
return sublime.Region(s.a, par_as_region.b)
return s
regions_transformer(self.view, f)
class _vi_left_brace(sublime_plugin.TextCommand):
def run(self, edit, mode=None, extend=False):
def f(view, s):
# TODO: must skip empty paragraphs.
start = utils.previous_non_white_space_char(view, s.b - 1, white_space='\n \t')
par_as_region = view.expand_by_class(start, sublime.CLASS_EMPTY_LINE)
if mode == MODE_NORMAL:
return sublime.Region(par_as_region.a, par_as_region.a)
elif mode == MODE_VISUAL:
# FIXME: Improve motion when .b end crosses over .a end: must extend .a end
# by one.
if s.a == par_as_region.a:
return sublime.Region(s.a, s.a + 1)
return sublime.Region(s.a, par_as_region.a)
elif mode == _MODE_INTERNAL_NORMAL:
return sublime.Region(s.a, par_as_region.a)
elif mode == MODE_VISUAL_LINE:
if s.a <= s.b:
if par_as_region.a < s.a:
return sublime.Region(view.full_line(s.a).b, par_as_region.a)
return sublime.Region(s.a, par_as_region.a + 1)
else:
return sublime.Region(s.a, par_as_region.a)
return s
regions_transformer(self.view, f)
class _vi_right_parenthesis(sublime_plugin.TextCommand):
def find_next_sentence_end(self, r):
sen = r
non_ws = utils.next_non_white_space_char(self.view, sen.b, '\t \n')
sen = sublime.Region(non_ws, non_ws)
while True:
sen = self.view.expand_by_class(sen, sublime.CLASS_PUNCTUATION_START |
sublime.CLASS_LINE_END)
if (sen.b == self.view.size() or
self.view.substr(sublime.Region(sen.b, sen.b + 2)).endswith(('. ', '.\t')) or
self.view.substr(self.view.line(sen.b)).strip() == ''):
if self.view.substr(sen.b) == '.':
return sublime.Region(sen.a, sen.b + 1)
else:
if self.view.line(sen.b).empty():
return sublime.Region(sen.a, sen.b)
else:
return self.view.full_line(sen.b)
def run(self, edit, mode=None, extend=False):
def f(view, s):
# TODO: must skip empty paragraphs.
sen = self.find_next_sentence_end(s)
if mode == MODE_NORMAL:
target = min(sen.b, view.size() - 1)
return sublime.Region(target, target)
elif mode == MODE_VISUAL:
# TODO: Must encompass new line char too?
return sublime.Region(s.a, sen.b)
elif mode == _MODE_INTERNAL_NORMAL:
return sublime.Region(s.a, sen.b)
return s
regions_transformer(self.view, f)
class _vi_left_parenthesis(sublime_plugin.TextCommand):
def find_previous_sentence_end(self, r):
sen = r
pt = utils.previous_non_white_space_char(self.view, sen.a, white_space='\n \t')
sen = sublime.Region(pt, pt)
while True:
sen = self.view.expand_by_class(sen, sublime.CLASS_LINE_END | sublime.CLASS_PUNCTUATION_END)
if sen.a <= 0 or self.view.substr(sen.begin() - 1) in ('.', '\n'):
if self.view.substr(sen.begin() - 1) == '.' and not self.view.substr(sen.begin()) == ' ':
continue
return sen
def run(self, edit, mode=None, extend=False):
def f(view, s):
# TODO: must skip empty paragraphs.
sen = self.find_previous_sentence_end(s)
if mode == MODE_NORMAL:
return sublime.Region(sen.a, sen.a)
elif mode == MODE_VISUAL:
return sublime.Region(s.a + 1, sen.a + 1)
elif mode == _MODE_INTERNAL_NORMAL:
return sublime.Region(s.a, sen.a + 1)
return s
regions_transformer(self.view, f)
class _vi_go_to_symbol(sublime_plugin.TextCommand):
"""Go to local declaration. Differs from Vim because it leverages Sublime Text's ability to
actually locate symbols (Vim simply searches from the top of the file).
"""
def find_symbol(self, r, globally):
query = self.view.substr(self.view.word(r))
fname = self.view.file_name().replace('\\', '/')
locations = self.view.window().lookup_symbol_in_index(query)
if not locations:
return
try:
if not globally:
location = [hit[2] for hit in locations if fname.endswith(hit[1])][0]
return location[0] - 1, location[1] - 1
else:
# TODO: There might be many symbols with the same name.
return locations[0]
except IndexError:
return
def run(self, edit, mode=None, extend=False, globally=False):
def f(view, s):
if mode == MODE_NORMAL:
return sublime.Region(location, location)
elif mode == MODE_VISUAL:
return sublime.Region(s.a + 1, location)
elif mode == _MODE_INTERNAL_NORMAL:
return sublime.Region(s.a, location)
return s
current_sel = self.view.sel()[0]
self.view.sel().clear()
self.view.sel().add(current_sel)
location = self.find_symbol(current_sel, globally=globally)
if not location:
return
if globally:
# Global symbol; simply open the file; not a motion.
# TODO: Perhaps must be a motion if the target file happens to be the current one?
self.view.window().open_file(location[0] + ':' + ':'.join([str(x) for x in location[2]]), sublime.ENCODED_POSITION)
return
# Local symbol; select.
location = self.view.text_point(*location)
regions_transformer(self.view, f)
class _vi_double_single_quote(IrreversibleTextCommand):
# (view_id, row) so we know where we stand.
_latest_known_location = None
next_command = 'jump_back'
def run(self):
# TODO: Long jumps are already recorded automatically, but if we press j many times, for
# example, the current position won't be updated, and we'll jump back to unexpected
# places.
# We have jumped back before, but then we've moved rows, so we don't want to jump forward
# any more.
if (_vi_double_single_quote._latest_known_location and
_vi_double_single_quote.next_command == 'jump_forward'):
if (_vi_double_single_quote._latest_known_location !=
(self.view.view_id, self.view.rowcol(self.view.sel()[0].b)[0])):
_vi_double_single_quote.next_command = 'jump_back'
# FIXME: Whenever there's a motion (any motion) after jumping back, we need to reset
# this command to jumping back again. Otherwise the result will be confusing.
# FIXME: We should create a jump entry at the current position.
self.view.run_command(_vi_double_single_quote.next_command)
if _vi_double_single_quote.next_command == 'jump_back':
# Jumping back... Store the far location.
_vi_double_single_quote._latest_known_location = (self.view.view_id, self.view.rowcol(self.view.sel()[0].b)[0])
current = _vi_double_single_quote.next_command
_vi_double_single_quote.next_command = 'jump_forward' if current == 'jump_back' else 'jump_back'
class _vi_pipe(sublime_plugin.TextCommand):
def col_to_pt(self, pt, nr):
if self.view.line(pt).size() < nr:
return self.view.line(pt).b - 1
row = self.view.rowcol(pt)[0]
return self.view.text_point(row, nr) - 1
def run(self, edit, mode=None, extend=False, count=None):
def f(view, s):
if mode == MODE_NORMAL:
pt = self.col_to_pt(pt=s.b, nr=count)
return sublime.Region(pt, pt)
elif mode == MODE_VISUAL:
pt = self.col_to_pt(pt=s.b - 1, nr=count)
if s.a < s.b:
if pt < s.a:
return sublime.Region(s.a + 1, pt)
else:
return sublime.Region(s.a, pt + 1)