forked from guillermooo/Vintageous
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xmotions.py
1882 lines (1506 loc) · 73.4 KB
/
xmotions.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
# TODO: weird name to avoid init issues with state.py::State.
import sublime
import sublime_plugin
from itertools import chain
from Vintageous import state as state_module
from Vintageous.vi import units
from Vintageous.vi import utils
from Vintageous.vi.core import ViTextCommandBase
from Vintageous.vi.keys import cmd_defs
from Vintageous.vi.keys import cmds
from Vintageous.vi.keys import mappings
from Vintageous.vi.keys import seqs
from Vintageous.vi.search import BufferSearchBase
from Vintageous.vi.search import ExactWordBufferSearchBase
from Vintageous.vi.search import find_in_range
from Vintageous.vi.search import find_wrapping
from Vintageous.vi.search import reverse_find_wrapping
from Vintageous.vi.search import reverse_search
from Vintageous.vi.search import reverse_search_by_pt
from Vintageous.state import State
from Vintageous.vi.text_objects import get_text_object_region
from Vintageous.vi.utils import col_at
from Vintageous.vi.utils import directions
from Vintageous.vi.utils import IrreversibleTextCommand
from Vintageous.vi.utils import modes
from Vintageous.vi.utils import regions_transformer
from Vintageous.vi.utils import mark_as_widget
class _vi_find_in_line(ViTextCommandBase):
"""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, char=None, mode=None, count=1, change_direction=True,
inclusive=True):
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 != modes.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, char,
search_range.a,
search_range.b,
sublime.LITERAL)
# Count too high or simply no match; break.
if match is None:
return s
break
if (mode == modes.VISUAL) or (mode == modes.INTERNAL_NORMAL):
if match.a == s.b:
return s
if not inclusive:
return sublime.Region(s.a, match.b - 1)
else:
return sublime.Region(s.a, match.b)
if not inclusive:
return sublime.Region(match.a - 1)
else:
return sublime.Region(match.a)
if not all([char, mode]):
print('char', char, 'mode', mode)
raise ValueError('bad parameters')
char = utils.translate_char(char)
state = self.state
# TODO: Move this to command definition/parser.
state.last_character_search = char
# Change only if the command is f, F, t, T; not if it's ',' or ';'.
if change_direction:
state.last_character_search_forward = True
regions_transformer(self.view, f)
class _vi_reverse_find_in_line(ViTextCommandBase):
"""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, char=None, mode=None, count=1, change_direction=True,
inclusive=True):
def f(view, s):
if mode not in (modes.VISUAL, modes.VISUAL_LINE, modes.VISUAL_BLOCK):
a, b = view.line(s.b).a, s.b
else:
a, b = view.line(s.b - 1).a, s.b
final_offset = -1
try:
# search backwards
for i in range(count):
line_text = view.substr(sublime.Region(a, b))
found_at = line_text.rindex(char)
final_offset = found_at
b = view.line(s.a).a + final_offset
except ValueError:
pass
if final_offset > -1:
pt = view.line(s.b).a + final_offset
if mode == modes.VISUAL or mode == modes.INTERNAL_NORMAL:
if not inclusive:
return sublime.Region(s.a, pt + 1)
else:
return sublime.Region(s.a, pt)
if not inclusive:
return sublime.Region(pt + 1)
else:
return sublime.Region(pt)
return s
if not all([char, mode]):
raise ValueError('bad parameters')
char = utils.translate_char(char)
state = self.state
# TODO: store state per view.
state.last_character_search = char
# Change only if the command is f, F, t, T; not if it's ',' or ';'.
if change_direction:
state.last_character_search_forward = False
regions_transformer(self.view, f)
class _vi_slash(IrreversibleTextCommand, ViTextCommandBase, BufferSearchBase):
"""
Collects input for the / motion.
"""
def run(self, default=''):
self.state.reset_during_init = False
# TODO: re-enable this.
# on_change = self.on_change if state.settings.vi['incsearch'] else None
on_change = self.on_change
mark_as_widget(self.view.window().show_input_panel(
'',
default,
self.on_done,
on_change,
self.on_cancel))
def on_done(self, s):
state = self.state
state.sequence += s + '<CR>'
self.view.erase_regions('vi_inc_search')
state.motion = cmd_defs[state.mode][cmds.SLASH_IMPL]
# If s is empty, we must repeat the last search.
# The next time we set .user_input, the vi_forward_slash input parser will kick in. We
# therefore need to ensure we only set .user_input once.
state.user_input = s or state.last_buffer_search
state.last_buffer_search = s or state.last_buffer_search
if state.input_parsers:
new_parsers = state.input_parsers
new_parsers.pop()
state.input_parsers = new_parsers
state.eval()
def on_change(self, s):
state = self.state
flags = self.calculate_flags()
self.view.erase_regions('vi_inc_search')
next_hit = find_wrapping(self.view,
term=s,
start=self.view.sel()[0].b + 1,
end=self.view.size(),
flags=flags,
times=state.count)
if next_hit:
if state.mode == modes.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):
state = self.state
self.view.erase_regions('vi_inc_search')
state.reset_command_data()
if not self.view.visible_region().contains(self.view.sel()[0]):
self.view.show(self.view.sel()[0])
class _vi_slash_impl(BufferSearchBase):
def run(self, edit, search_string='', mode=None, count=1):
def f(view, s):
if mode == modes.VISUAL:
return sublime.Region(s.a, match.a + 1)
elif mode == modes.INTERNAL_NORMAL:
return sublime.Region(s.a, match.a)
elif mode == modes.NORMAL:
return sublime.Region(match.a, match.a)
elif mode == modes.VISUAL_LINE:
return sublime.Region(s.a, view.full_line(match.b - 1).b)
return s
# This happens when we attempt to repeat the search and there's no search term stored yet.
if not search_string:
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.
# flags = sublime.IGNORECASE | sublime.LITERAL
flags = self.calculate_flags()
match = find_wrapping(self.view, search_string, start, wrapped_end, flags=flags, times=count)
if not match:
return
regions_transformer(self.view, f)
self.hilite(search_string)
class _vi_l(ViTextCommandBase):
def run(self, edit, mode=None, count=None):
def f(view, s):
if mode == modes.NORMAL:
if view.line(s.b).empty():
return s
x_limit = min(view.line(s.b).b - 1, s.b + count, view.size())
return sublime.Region(x_limit, x_limit)
if mode == modes.INTERNAL_NORMAL:
x_limit = min(view.line(s.b).b, s.b + count)
x_limit = max(0, x_limit)
return sublime.Region(s.a, x_limit)
if mode in (modes.VISUAL, modes.VISUAL_BLOCK):
if s.a < s.b:
x_limit = min(view.full_line(s.b - 1).b, s.b + count)
return sublime.Region(s.a, x_limit)
if s.a > s.b:
x_limit = min(view.full_line(s.b).b - 1, s.b + count)
if view.substr(s.b) == '\n':
return s
if view.line(s.a) == view.line(s.b) and count >= s.size():
x_limit = min(view.full_line(s.b).b, s.b + count + 1)
return sublime.Region(s.a - 1, x_limit)
return sublime.Region(s.a, x_limit)
return s
regions_transformer(self.view, f)
state = self.state
state.xpos = self.view.rowcol(self.view.sel()[0].b)[1]
class _vi_h(ViTextCommandBase):
def run(self, edit, count=None, mode=None):
def f(view, s):
if mode == modes.INTERNAL_NORMAL:
x_limit = max(view.line(s.b).a, s.b - count)
return sublime.Region(s.a, x_limit)
# TODO: Split handling of the two modes for clarity.
elif mode in (modes.VISUAL, modes.VISUAL_BLOCK):
if s.a < s.b:
if mode == modes.VISUAL_BLOCK and self.view.rowcol(s.b - 1)[1] == baseline:
return s
x_limit = max(view.line(s.b - 1).a + 1, s.b - count)
if view.line(s.a) == view.line(s.b - 1) and count >= s.size():
x_limit = max(view.line(s.b - 1).a, s.b - count - 1)
return sublime.Region(s.a + 1, x_limit)
return sublime.Region(s.a, x_limit)
if s.a > s.b:
x_limit = max(view.line(s.b).a, s.b - count)
return sublime.Region(s.a, x_limit)
elif mode == modes.NORMAL:
x_limit = max(view.line(s.b).a, s.b - count)
return sublime.Region(x_limit, x_limit)
# XXX: We should never reach this.
return s
# For jagged selections (on the rhs), only those sticking out need to move leftwards.
# Example ([] denotes the selection):
#
# 10 foo bar foo [bar]
# 11 foo bar foo [bar foo bar]
# 12 foo bar foo [bar foo]
#
# Only lines 11 and 12 should move when we press h.
baseline = 0
if mode == modes.VISUAL_BLOCK:
sel = self.view.sel()[0]
if sel.a < sel.b:
min_ = min(self.view.rowcol(r.b - 1)[1] for r in self.view.sel())
if any(self.view.rowcol(r.b - 1)[1] != min_ for r in self.view.sel()):
baseline = min_
regions_transformer(self.view, f)
state = self.state
state.xpos = self.view.rowcol(self.view.sel()[0].b)[1]
class _vi_j(ViTextCommandBase):
def folded_rows(self, pt):
folds = self.view.folded_regions()
try:
fold = [f for f in folds if f.contains(pt)][0]
fold_row_a = self.view.rowcol(fold.a)[0]
fold_row_b = self.view.rowcol(fold.b - 1)[0]
# Return no. of hidden lines.
return (fold_row_b - fold_row_a)
except IndexError:
return 0
def next_non_folded_pt(self, pt):
# FIXME: If we have two contiguous folds, this method will fail.
# Handle folded regions.
folds = self.view.folded_regions()
try:
fold = [f for f in folds if f.contains(pt)][0]
non_folded_row = self.view.rowcol(self.view.full_line(fold.b).b)[0]
pt = self.view.text_point(non_folded_row, 0)
except IndexError:
pass
return pt
def run(self, edit, count=None, mode=None, xpos=0, no_translation=False):
def f(view, s):
if mode == modes.NORMAL:
current_row = view.rowcol(s.b)[0]
target_row = min(current_row + count, view.rowcol(view.size())[0])
invisible_rows = self.folded_rows(view.line(s.b).b + 1)
target_pt = view.text_point(target_row + invisible_rows, 0)
target_pt = self.next_non_folded_pt(target_pt)
if view.line(target_pt).empty():
return sublime.Region(target_pt, target_pt)
target_pt = min(target_pt + xpos, view.line(target_pt).b - 1)
return sublime.Region(target_pt, target_pt)
if mode == modes.INTERNAL_NORMAL:
current_row = view.rowcol(s.b)[0]
target_row = min(current_row + count, view.rowcol(view.size())[0])
target_pt = view.text_point(target_row, 0)
return sublime.Region(view.line(s.a).a, view.full_line(target_pt).b)
if mode == modes.VISUAL:
exact_position = s.b - 1 if (s.a < s.b) else s.b
current_row = view.rowcol(exact_position)[0]
target_row = min(current_row + count, view.rowcol(view.size())[0])
target_pt = view.text_point(target_row, 0)
is_long_enough = view.full_line(target_pt).size() > xpos
# We're crossing over to the other side of .a; we need to modify .a.
crosses_a = False
if (s.a > s.b) and (view.rowcol(s.a)[0] < target_row):
crosses_a = True
if view.line(s.begin()) == view.line(s.end() - 1):
if s.a > s.b:
if is_long_enough:
return sublime.Region(s.a - 1, view.text_point(target_row, xpos) + 1)
else:
return sublime.Region(s.a - 1, view.full_line(target_pt).b)
# Returning to the same line...
if not crosses_a and abs(view.rowcol(s.begin())[0] - view.rowcol(s.end())[0]) == 1:
if s.a > s.b:
if is_long_enough:
if view.rowcol(s.a - 1)[1] <= view.rowcol(s.b)[1]:
return sublime.Region(s.a - 1, view.text_point(target_row, xpos) + 1)
if is_long_enough:
if s.a < s.b:
return sublime.Region(s.a, view.text_point(target_row, xpos) + 1)
elif s.a > s.b:
start = s.a if not crosses_a else s.a - 1
end = view.text_point(target_row, xpos)
end = end if (end < s.a) else end + 1
return sublime.Region(start, end)
else:
if s.a < s.b:
return sublime.Region(s.a, view.full_line(target_pt).b)
elif s.a > s.b:
end = view.full_line(target_pt).b
end = end - 1 if not crosses_a else end
return sublime.Region(s.a, end)
if mode == modes.VISUAL_LINE:
if s.a < s.b:
current_row = view.rowcol(s.b - 1)[0]
target_row = min(current_row + count, view.rowcol(view.size())[0])
target_pt = view.text_point(target_row, 0)
return sublime.Region(s.a, view.full_line(target_pt).b)
elif s.a > s.b:
current_row = view.rowcol(s.b)[0]
target_row = min(current_row + count, view.rowcol(view.size())[0])
target_pt = view.text_point(target_row, 0)
if target_row > view.rowcol(s.a - 1)[0]:
return sublime.Region(view.line(s.a - 1).a, view.full_line(target_pt).b)
return sublime.Region(s.a, view.full_line(target_pt).a)
return s
state = State(self.view)
if mode == modes.VISUAL_BLOCK:
if len(self.view.sel()) == 1:
state.visual_block_direction = directions.DOWN
# Don't do anything if we have reversed selections.
if any((r.b < r.a) for r in self.view.sel()):
return
if state.visual_block_direction == directions.DOWN:
for i in range(count):
# FIXME: When there are multiple rectangular selections, S3 considers sel 0 to be the
# active one in all cases, so we can't know the 'direction' of such a selection and,
# therefore, we can't shrink it when we press k or j. We can only easily expand it.
# We could, however, have some more global state to keep track of the direction of
# visual block selections.
row, rect_b = self.view.rowcol(self.view.sel()[-1].b - 1)
# Don't do anything if the next row is empty or too short. Vim does a crazy thing: it
# doesn't select it and it doesn't include it in actions, but you have to still navigate
# your way through them.
# TODO: Match Vim's behavior.
next_line = self.view.line(self.view.text_point(row + 1, 0))
if next_line.empty() or self.view.rowcol(next_line.b)[1] < rect_b:
return
max_size = max(r.size() for r in self.view.sel())
row, col = self.view.rowcol(self.view.sel()[-1].a)
start = self.view.text_point(row + 1, col)
new_region = sublime.Region(start, start + max_size)
self.view.sel().add(new_region)
# FIXME: Perhaps we should scroll into view in a more general way...
self.view.show(new_region, False)
return
else:
# Must delete last sel.
self.view.sel().subtract(self.view.sel()[0])
return
regions_transformer(self.view, f)
class _vi_k(ViTextCommandBase):
def previous_non_folded_pt(self, pt):
# FIXME: If we have two contiguous folds, this method will fail.
# Handle folded regions.
folds = self.view.folded_regions()
try:
fold = [f for f in folds if f.contains(pt)][0]
non_folded_row = self.view.rowcol(fold.a - 1)[0]
pt = self.view.text_point(non_folded_row, 0)
except IndexError:
pass
return pt
def run(self, edit, count=None, mode=None, xpos=0, no_translation=False):
def f(view, s):
if mode == modes.NORMAL:
current_row = view.rowcol(s.b)[0]
target_row = min(current_row - count, view.rowcol(view.size())[0])
target_pt = view.text_point(target_row, 0)
target_pt = self.previous_non_folded_pt(target_pt)
if view.line(target_pt).empty():
return sublime.Region(target_pt, target_pt)
target_pt = min(target_pt + xpos, view.line(target_pt).b - 1)
return sublime.Region(target_pt, target_pt)
if mode == modes.INTERNAL_NORMAL:
current_row = view.rowcol(s.b)[0]
target_row = min(current_row - count, view.rowcol(view.size())[0])
target_pt = view.text_point(target_row, 0)
return sublime.Region(view.full_line(s.a).b, view.line(target_pt).a)
if mode == modes.VISUAL:
exact_position = s.b - 1 if (s.a < s.b) else s.b
current_row = view.rowcol(exact_position)[0]
target_row = max(current_row - count, 0)
target_pt = view.text_point(target_row, 0)
is_long_enough = view.full_line(target_pt).size() > xpos
# We're crossing over to the other side of .a; we need to modify .a.
crosses_a = False
if (s.a < s.b) and (view.rowcol(s.a)[0] > target_row):
crosses_a = True
if view.line(s.begin()) == view.line(s.end() - 1):
if s.a < s.b:
if is_long_enough:
return sublime.Region(s.a + 1, view.text_point(target_row, xpos))
else:
return sublime.Region(s.a + 1, view.line(target_pt).b)
# Returning to the same line...
if not crosses_a and abs(view.rowcol(s.begin())[0] - view.rowcol(s.end() - 1)[0]) == 1:
if s.a < s.b:
if is_long_enough:
if view.rowcol(s.a)[1] <= view.rowcol(s.b - 1)[1]:
return sublime.Region(s.a, view.text_point(target_row, xpos) + 1)
else:
r = sublime.Region(s.a + 1, view.text_point(target_row, xpos))
return r
if is_long_enough:
if s.a < s.b:
# FIXME: The following if-else could use some brains.
offset = 0
if xpos == 0 and not crosses_a:
offset = 1
elif crosses_a:
pass
else:
offset = 1
start = s.a if not crosses_a else s.a + 1
return sublime.Region(start, view.text_point(target_row, xpos + offset))
elif s.a > s.b:
return sublime.Region(s.a, view.text_point(target_row, xpos))
else:
if s.a < s.b:
end = view.full_line(target_pt).b
end = end if not crosses_a else end - 1
return sublime.Region(s.a, end)
elif s.a > s.b:
return sublime.Region(s.a, view.line(target_pt).b)
if mode == modes.VISUAL_LINE:
if s.a < s.b:
current_row = view.rowcol(s.b - 1)[0]
target_row = min(current_row - count, view.rowcol(view.size())[0])
target_pt = view.text_point(target_row, 0)
if target_row < view.rowcol(s.begin())[0]:
return sublime.Region(view.full_line(s.a).b, view.full_line(target_pt).a)
return sublime.Region(s.a, view.full_line(target_pt).b)
elif s.a > s.b:
current_row = view.rowcol(s.b)[0]
target_row = max(current_row - count, 0)
target_pt = view.text_point(target_row, 0)
return sublime.Region(s.a, view.full_line(target_pt).a)
state = State(self.view)
if mode == modes.VISUAL_BLOCK:
if len(self.view.sel()) == 1:
state.visual_block_direction = directions.UP
# Don't do anything if we have reversed selections.
if any((r.b < r.a) for r in self.view.sel()):
return
if state.visual_block_direction == directions.UP:
for i in range(count):
rect_b = max(self.view.rowcol(r.b - 1)[1] for r in self.view.sel())
row, rect_a = self.view.rowcol(self.view.sel()[0].a)
previous_line = self.view.line(self.view.text_point(row - 1, 0))
# Don't do anything if previous row is empty. Vim does crazy stuff in that case.
# Don't do anything either if the previous line can't accomodate a rectangular selection
# of the required size.
if (previous_line.empty() or
self.view.rowcol(previous_line.b)[1] < rect_b):
return
rect_size = max(r.size() for r in self.view.sel())
rect_a_pt = self.view.text_point(row - 1, rect_a)
new_region = sublime.Region(rect_a_pt, rect_a_pt + rect_size)
self.view.sel().add(new_region)
# FIXME: We should probably scroll into view in a more general way.
# Or maybe every motion should handle this on their own.
self.view.show(new_region, False)
return
else:
# Must remove last selection.
self.view.sel().subtract(self.view.sel()[-1])
return
regions_transformer(self.view, f)
class _vi_k_select(ViTextCommandBase):
def run(self, edit, count=1, mode=None):
# FIXME: It isn't working.
if mode != modes.SELECT:
utils.blink()
return
for i in range(count):
self.view.window().run_command('soft_undo')
return
class _vi_gg(ViTextCommandBase):
def run(self, edit, mode=None, count=1):
def f(view, s):
if mode == modes.NORMAL:
return sublime.Region(0)
elif mode == modes.VISUAL:
if s.a < s.b:
return sublime.Region(s.a + 1, 0)
else:
return sublime.Region(s.a, 0)
elif mode == modes.INTERNAL_NORMAL:
return sublime.Region(view.full_line(s.b).b, 0)
elif mode == modes.VISUAL_LINE:
if s.a < s.b:
return sublime.Region(0, s.b)
else:
return sublime.Region(0, s.a)
return s
self.view.window().run_command('_vi_add_to_jump_list')
regions_transformer(self.view, f)
self.view.window().run_command('_vi_add_to_jump_list')
class _vi_go_to_line(ViTextCommandBase):
def run(self, edit, line=None, mode=None):
line = line if line > 0 else 1
dest = self.view.text_point(line - 1, 0)
def f(view, s):
if mode == modes.NORMAL:
non_ws = utils.next_non_white_space_char(view, dest)
return sublime.Region(non_ws, non_ws)
elif mode == modes.INTERNAL_NORMAL:
return sublime.Region(view.line(s.a).a, view.line(dest).b)
elif mode == modes.VISUAL:
if dest < s.a and s.a < s.b:
return sublime.Region(s.a + 1, dest)
elif dest < s.a:
return sublime.Region(s.a, dest)
elif dest > s.b and s.a > s.b:
return sublime.Region(s.a - 1, dest + 1)
return sublime.Region(s.a, dest + 1)
elif mode == modes.VISUAL_LINE:
if dest < s.a and s.a < s.b:
return sublime.Region(view.full_line(s.a).b, dest)
elif dest < s.a:
return sublime.Region(s.a, dest)
elif dest > s.a and s.a > s.b:
return sublime.Region(view.full_line(s.a - 1).a, view.full_line(dest).b)
return sublime.Region(s.a, view.full_line(dest).b)
return s
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 _vi_big_g(ViTextCommandBase):
def run(self, edit, mode=None, count=None):
def f(view, s):
if mode == modes.NORMAL:
pt = eof
if not view.line(eof).empty():
pt = utils.previous_non_white_space_char(view, eof - 1,
white_space='\n')
return sublime.Region(pt, pt)
elif mode == modes.VISUAL:
return sublime.Region(s.a, eof)
elif mode == modes.INTERNAL_NORMAL:
begin = view.line(s.b).a
begin = max(0, begin - 1)
return sublime.Region(begin, eof)
elif mode == modes.VISUAL_LINE:
return sublime.Region(s.a, eof)
return s
self.view.window().run_command('_vi_add_to_jump_list')
eof = self.view.size()
regions_transformer(self.view, f)
self.view.window().run_command('_vi_add_to_jump_list')
class _vi_dollar(ViTextCommandBase):
def run(self, edit, mode=None, count=None):
def f(view, s):
if mode == modes.NORMAL:
if count > 1:
pt = view.line(target_row_pt).b
else:
pt = view.line(s.b).b
if not view.line(pt).empty():
return sublime.Region(pt - 1, pt - 1)
return sublime.Region(pt, pt)
elif mode == modes.VISUAL:
current_line_pt = (s.b - 1) if (s.a < s.b) else s.b
if count > 1:
end = view.full_line(target_row_pt).b
else:
end = s.end()
if not end == view.full_line(s.b - 1).b:
end = view.full_line(s.b).b
end = end if (s.a < end) else (end - 1)
start = s.a if ((s.a < s.b) or (end < s.a)) else s.a - 1
return sublime.Region(start, end)
elif mode == modes.INTERNAL_NORMAL:
if count > 1:
pt = view.line(target_row_pt).b
else:
pt = view.line(s.b).b
if count == 1:
return sublime.Region(s.a, pt)
return sublime.Region(s.a, pt + 1)
elif mode == modes.VISUAL_LINE:
# TODO: Implement this. Not too useful, though.
return s
return s
sel = self.view.sel()[0]
target_row_pt = (sel.b - 1) if (sel.b > sel.a) else sel.b
if count > 1:
current_row = self.view.rowcol(target_row_pt)[0]
target_row = current_row + count - 1
target_row_pt = self.view.text_point(target_row, 0)
regions_transformer(self.view, f)
class _vi_w(ViTextCommandBase):
def run(self, edit, mode=None, count=1):
def f(view, s):
if mode == modes.NORMAL:
pt = units.word_starts(view, start=s.b, count=count)
if ((pt == view.size()) and (not view.line(pt).empty())):
pt = utils.previous_non_white_space_char(view, pt - 1,
white_space='\n')
return sublime.Region(pt, pt)
elif mode == modes.VISUAL:
pt = units.word_starts(view, start=s.b - 1, count=count)
if s.a > s.b and pt >= s.a:
return sublime.Region(s.a - 1, pt + 1)
elif s.a > s.b:
return sublime.Region(s.a, pt)
elif (view.size() == pt):
pt -= 1
return sublime.Region(s.a, pt + 1)
elif mode == modes.INTERNAL_NORMAL:
a = s.a
pt = units.word_starts(view, start=s.b, count=count, internal=True)
if (not view.substr(view.line(s.a)).strip() and
(view.line(s.b) != view.line(pt))):
a = view.line(s.a).a
return sublime.Region(a, pt)
return s
regions_transformer(self.view, f)
class _vi_big_w(ViTextCommandBase):
def run(self, edit, mode=None, count=1):
def f(view, s):
if mode == modes.NORMAL:
pt = units.big_word_starts(view, start=s.b, count=count)
if ((pt == view.size()) and (not view.line(pt).empty())):
pt = utils.previous_non_white_space_char(view, pt - 1,
white_space='\n')
return sublime.Region(pt, pt)
elif mode == modes.VISUAL:
pt = units.big_word_starts(view, start=s.b - 1, count=count)
if s.a > s.b and pt >= s.a:
return sublime.Region(s.a - 1, pt + 1)
elif s.a > s.b:
return sublime.Region(s.a, pt)
elif (view.size() == pt):
pt -= 1
return sublime.Region(s.a, pt + 1)
elif mode == modes.INTERNAL_NORMAL:
a = s.a
pt = units.big_word_starts(view, start=s.b, count=count, internal=True)
if (not view.substr(view.line(s.a)).strip() and
(view.line(s.b) != view.line(pt))):
a = view.line(s.a).a
return sublime.Region(a, pt)
return s
regions_transformer(self.view, f)
class _vi_e(ViTextCommandBase):
def run(self, edit, mode=None, count=1):
def f(view, s):
if mode == modes.NORMAL:
pt = units.word_ends(view, start=s.b, count=count)
if (view.substr(pt) == '\n' and not view.line(pt).empty()):
return sublime.Region(pt - 1, pt - 1)
elif (view.line(pt).empty()):
return s
return sublime.Region(pt, pt)
elif mode == modes.VISUAL:
pt = units.word_ends(view, start=s.b - 1, count=count)
if s.a > s.b and pt >= s.a:
return sublime.Region(s.a - 1, pt + 1)
elif s.a > s.b:
return sublime.Region(s.a, pt)
elif (view.size() == pt):
pt -= 1
return sublime.Region(s.a, pt + 1)
elif mode == modes.INTERNAL_NORMAL:
a = s.a
pt = units.word_ends(view, start=s.b, count=count, internal=True)
if (not view.substr(view.line(s.a)).strip() and
(view.line(s.b) != view.line(pt))):
a = view.line(s.a).a
return sublime.Region(a, pt + 1)
return s
regions_transformer(self.view, f)
class _vi_zero(ViTextCommandBase):
def run(self, edit, mode=None, count=1):
def f(view, s):
if mode == modes.NORMAL:
return sublime.Region(view.line(s.b).a)
elif mode == modes.INTERNAL_NORMAL:
return sublime.Region(s.a, view.line(s.b).a)
elif mode == modes.VISUAL:
if s.a < s.b:
return sublime.Region(s.a, view.line(s.b - 1).a + 1)
else:
return sublime.Region(s.a, view.line(s.b).a)
return s
regions_transformer(self.view, f)
class _vi_right_brace(ViTextCommandBase):
def run(self, edit, mode=None, count=1):
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 == modes.NORMAL:
min_pt = max(0, min(par_as_region.b, view.size() - 1))
return sublime.Region(min_pt, min_pt)
elif mode == modes.VISUAL:
return sublime.Region(s.a, par_as_region.b + 1)
elif mode == modes.INTERNAL_NORMAL:
return sublime.Region(s.a, par_as_region.b - 1)
elif mode == modes.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(ViTextCommandBase):
def run(self, edit, mode=None, count=1):
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 == modes.NORMAL:
return sublime.Region(par_as_region.a, par_as_region.a)
elif mode == modes.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 == modes.INTERNAL_NORMAL:
return sublime.Region(s.a, par_as_region.a)
elif mode == modes.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_percent(ViTextCommandBase):
# TODO: Perhaps truly support multiple regions here?
pairs = (
('(', ')'),
('[', ']'),
('{', '}'),
)
def run(self, edit, 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 == modes.VISUAL:
b = (s.b - 1) if (s.a < s.b) else s.b
found = find_bracket_location(b)
if found:
end = (found + 1) if (found > s.b) else found
end = (end + 1) if end == s.a else end
begin = (s.a + 1) if (s.a < s.b) else s.a
return sublime.Region(begin, end)
if mode == modes.VISUAL_LINE:
# TODO: Improve handling of s.a < s.b and s.a > s.b cases.
a = find_bracket_location(s.begin())
if a is not None:
a = self.view.full_line(a).b
return sublime.Region(s.begin(), a)
elif mode == modes.NORMAL:
a = find_bracket_location(s.b)
if a is not None:
return sublime.Region(a, a)