-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvimstate.lua
More file actions
1220 lines (1068 loc) · 41.5 KB
/
vimstate.lua
File metadata and controls
1220 lines (1068 loc) · 41.5 KB
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
local core = require "core"
local style = require "core.style"
local command = require "core.command"
local Object = require "core.object"
local config = require "core.config"
local ime = require "core.ime"
local litexl_keymap = require "core.keymap"
local vim_functions = require "plugins.vimxl.functions"
local vim_motions = require "plugins.vimxl.motions"
local vim_keymap = require "plugins.vimxl.keymap"
local vim_motionmodes = require "plugins.vimxl.motionmodes"
local vim_available_commands = require "plugins.vimxl.available-commands"
local constants = require "plugins.vimxl.constants"
---This object is inserted into a DocView to indicate that Vim-mode has been
---enabled for that DocView. This object is self-contained when it comes to
---all data it needs to properly implement Vim-mode.
---@class vimxl.vimstate : core.object
---@field super core.object
local VimState = Object:extend()
---All possible modes supported by VimXL by default.
---@alias vimxl.mode "i"|"v"|"n"|"v-block"|"v-line"
---Stuff relating to command_history:
---Stored inside of repeatable_commands
---We want to differentiate performs that take a number argument
---from those that don't so that the "dot" command can safely modify
---the numerical value.
---However, the nil value is also treated as a valid value for the
---numerical argument, hence why it is optional.
---@see vimxl.vimstate.begin_command_with_numerical_argument
---@alias vimxl.perform_with_optional_number fun(state: vimxl.vimstate, n: number | nil)
---
---Stored inside of repeatable_commands.
---@see vimxl.perform_with_number For explanation on its sibling type.
---@see vimxl.vimstate.begin_naive_repeatable_command
---@alias vimxl.perform_no_number fun(state: vimxl.vimstate)
---
---Base type for everything that goes into command_history.
---@class vimxl.repeatable_command
---@field type "dummy"
---
---@class vimxl.repeatable_text_input : vimxl.repeatable_command
---@field type "text_input"
---@field text string
---
---@class vimxl.repeatable_remove_text : vimxl.repeatable_command
---@field type "remove_text"
---@field amount number
---
---@class vimxl.repeatable_command_supporting_number : vimxl.repeatable_command
---@field type "command_supporting_number"
---@field perform vimxl.perform_with_optional_number
---@field number number | nil
---
---@class vimxl.repeatable_command_no_number : vimxl.repeatable_command
---@field type "command"
---@field perform vimxl.perform_no_number
---
---@class vimxl.repeatable_select_to : vimxl.repeatable_command
---@field type "select_to"
---@field perform vimxl.perform_no_number
---
---@class vimxl.repeatable_move_to : vimxl.repeatable_command
---@field type "move_to"
---@field translate vimxl.motion
---
---@class vimxl.repeatable_everything : vimxl.repeatable_command
---@field type "repeat_everything"
---@field number number
---
--- A collection of all known types that go into command_history.
---@alias vimxl.repeatable_generic vimxl.repeatable_text_input | vimxl.repeatable_remove_text | vimxl.repeatable_command_supporting_number | vimxl.repeatable_command_no_number | vimxl.repeatable_move_to | vimxl.repeatable_select_to | vimxl.repeatable_everything | vimxl.repeatable_command
function VimState:__tostring() return "VimState" end
---@param view core.docview
function VimState:new(view)
VimState.super.new(self)
---The view that this instance of Vim-emulation is attached to.
self.view = view
---Which Vim mode we are emulating, i.e normal-mode, insert-mode, etc...
---@type vimxl.mode
self.mode = "n"
---Since some keybinds are only available when preceeded by others we
---must track some kind of state to know which keys are available.
---Simply looking a self.mode is not enough.
---@type vimxl.keybind_map
self.keymap = vim_keymap.normal
---A portion of edit commands that can later be moved to repeatable_commands
---in order to repeat them.
---@type vimxl.repeatable_generic[]
self.command_history = {}
---A complete set of repeatable commands. To not append, only set.
---@type vimxl.repeatable_generic[]
self.repeatable_commands = {}
-- Should a repeat be performed?
self.repeat_requested = false
-- Tracked such that yy, dd & cc represent "entire line" motions.
self.operator_name = ""
---What function to call once we have obtained a motion.
---Note that a value of nil means that we are not expecting a motion.
--- @type vimxl.motion_cb | nil
self.operator_got_motion_cb = nil
---Accumulated thru 0-9 keys.
---@type number | nil
self.numerical_argument = nil
---Should we record basic function calls like Doc:insert to history?
---This is mainly used for a less intrusive way of tracking autocomplete.
self.track_primitives = false
end
---This callback has a chance to be triggered after a valid motion
---is detected. However it can also never be called if the user fails
---to provide a valid motion on the first try.
---@alias vimxl.motion_cb fun(state: vimxl.vimstate, motion_mode: vimxl.motion_mode, motion: vimxl.motion, numerical_argument: number)
---Inform Vim that we a command has kindly asked for a motion.
---@param cb vimxl.motion_cb
function VimState:expect_motion(cb)
self.operator_got_motion_cb = cb
self.keymap = vim_keymap.motions
end
local function is_selection_going_forward(l1, c1, l2, c2)
return l1 > l2 or l1 == l2 and c1 > c2
end
---Implements the Vim visual mode of navigation
---where the cursor is always on a character and not just
---in between two.
---@param doc core.doc
---@param start_line number
---@param start_col number
---@param view core.docview
---@param motion_mode vimxl.motion_mode
---@param translate_fn vimxl.motion
---@param numerical_argument? number
---@param end_line number
---@param end_col number
local function vim_style_visual_select_to_impl(doc, start_line, start_col, view, motion_mode, translate_fn, numerical_argument, end_line, end_col)
-- Detect if a character is not currently selected and if so correct that.
if start_line == end_line and start_col == end_col then
start_col = start_col + 1
end
local was_neutral = start_line == end_line and start_col == end_col + 1
local was_going_forward = is_selection_going_forward(start_line, start_col, end_line, end_col)
-- Steal a character so that we go from having the cursor be
-- on a character to having it be between characters.
if was_going_forward then
start_col = start_col - 1
end
local l1, c1, l2, c2 = translate_fn(doc, start_line, start_col, view, numerical_argument)
local got_text_object = false
-- Got text object. Extend current extension.
if motion_mode.is_text_object and l2 ~= nil and c2 ~= nil then
-- Sort them. Such that the default selection is "going forward".
if not is_selection_going_forward(l1, c1, l2, c2) then
l1, c1, l2, c2 = l2, c2, l1, c1
end
if was_neutral then
got_text_object = true
elseif was_going_forward then
-- Is l2 & c2 further away from cursor? If so use that.
if is_selection_going_forward(l2, c2, l1, c1) then
l1 = l2
c1 = c2
-- l2 and c2 is later on.
end
else
-- Is l2 & c2 further away from cursor? If so use that.
if is_selection_going_forward(l1, c1, l2, c2) then
l1 = l2
c1 = c2
-- l2 and c2 is later on.
end
end
end
if not got_text_object then
-- Extend the selection because we were doing a simple movement.
l2 = end_line
c2 = end_col
end
local is_selecting = l1 ~= l2 or c1 ~= c2
local is_going_forward = is_selection_going_forward(l1, c1, l2, c2)
if is_selecting and was_going_forward and not is_going_forward then
c2 = c2 + 1
elseif is_selecting and not was_going_forward and is_going_forward then
c2 = c2 - 1
elseif not is_selecting or is_going_forward and not got_text_object then
-- Give back the character we stole earlier.
c1 = c1 + 1
end
-- If we are selecting only one character, then make it a normal "neutral" selection
-- which is to say that c1 should be greater than c2, i.e selection goes forward.
if l1 == l2 and c1 + 1 == c2 then
c1, c2 = c2 or c1, c1 or c2
end
return l1, c1, l2, c2
end
---Implements the Vim visual block mode of navigation
---where the cursor is always on a character and not just
---in between two.
---@param l1 number
---@param c1 number
---@param l2 number
---@param c2 number
---@param was_going_forward boolean
local function vim_style_visual_block_select_to_impl(l1, c1, l2, c2, was_going_forward)
local is_selecting = c1 ~= c2
local is_going_forward = c1 > c2
if is_selecting and was_going_forward and not is_going_forward then
c2 = c2 + 1
elseif is_selecting and not was_going_forward and is_going_forward then
c2 = c2 - 1
elseif not is_selecting or is_going_forward then
-- Give back the character we stole earlier.
c1 = c1 + 1
end
-- If we are selecting only one character, then make it a normal "neutral" selection
-- which is to say that c1 should be greater than c2, i.e selection goes forward.
if c1 + 1 == c2 then
c1, c2 = c2 or c1, c1 or c2
end
return l1, c1, l2, c2
end
---@param l1 number Line of the cursor corner.
---@param c1 number Column of the cursor corner.
---@param l2 number Line of the starting-point corner.
---@param c2 number Column of the starting-point corner.
---@param existing_selections number How many existing selections are there? We clear them all.
function VimState:create_block_selection(l1, c1, l2, c2, existing_selections)
-- We use l2 which is the last line. If there is only one line,
-- then l1 is also l2. So everything is fine.
self.view.doc:set_selections(1, l2, c1, l2, c2, false, existing_selections * 4)
if l2 == l1 then
-- Only one line so stop here.
-- last_selection isn't set by set_selection,
-- but we're okayy with that because after our
-- call to set_selection there should only be one selection.
-- And as such get_selection() will return it.
return
end
-- There are more lines.
-- We want to make sure that the l1 is the last line that is added so that
-- get_selection() will return it.
local increment = l1 > l2 and 1 or -1
for line = l2 + increment, l1, increment do
self.view.doc:add_selection(line, c1, line, c2)
end
end
---The line selection has the cursor at the start of the selection.
---Also used during corner detection for block-select.
---@see vim_style_visual_line_select_impl
---@see find_corners_from_selection
local DIRECTION_BACKWARD = -1
---The line selection only contains one line.
---Also used during corner detection for block-select.
---@see vim_style_visual_line_select_impl
---@see find_corners_from_selection
local DIRECTION_NEUTRAL = 0
---The line selection has the cursor at the end of the selection.
---Also used during corner detection for block-select.
---@see vim_style_visual_line_select_impl
---@see find_corners_from_selection
local DIRECTION_FORWARD = 1
---@param doc core.doc
local function find_corners_from_selection(doc)
local start_line, start_col, end_line, end_col = doc:get_selection()
local direction = DIRECTION_NEUTRAL
local fused = 0
for _, other_line, _, l2, c2 in doc:get_selections() do
fused = fused + 1
if direction == DIRECTION_NEUTRAL and start_line ~= other_line then
if other_line > start_line then
direction = DIRECTION_BACKWARD
end_line, end_col = l2, c2
elseif other_line < start_line then
direction = DIRECTION_FORWARD
end_line, end_col = l2, c2
end
elseif direction == DIRECTION_FORWARD then
if other_line < end_line then
end_line, end_col = l2, c2
end
elseif direction == DIRECTION_BACKWARD then
if other_line > end_line then
end_line, end_col = l2, c2
end
end
end
return start_line, start_col, end_line, end_col, fused
end
---Implements linewise visual select for us.
---@param doc core.doc
---@param cursor_line number
---@param cursor_col number
---@param start_line number
---@param end_line number
---@param old_direction number
local function vim_style_visual_line_select_impl(doc, cursor_line, cursor_col, start_line, end_line, old_direction)
if old_direction == DIRECTION_NEUTRAL then
-- We just expand in any direction that fits us.
if cursor_line > start_line then
end_line = cursor_line + 1
end
if cursor_line < start_line then
start_line = cursor_line
end
elseif old_direction == DIRECTION_FORWARD then
if cursor_line < start_line then
-- Direction flipped.
end_line = start_line + 1
start_line = cursor_line
else
-- Shrinking or expanding!
end_line = cursor_line + 1
end
elseif old_direction == DIRECTION_BACKWARD then
if cursor_line >= end_line then
-- Direction flipped.
start_line = end_line - 1
end_line = cursor_line + 1
else
-- Shrinking or expanding!
start_line = cursor_line
end
end
if start_line > end_line then
core.error("Visual-line mode has gone insane!")
end
-- Remove everything and add two new selections.
-- TOOD: If there is a more API stable way of getting selection count then I want to know it.
doc:set_selections(1, cursor_line, cursor_col, start_line, 0, false, #doc.selections)
doc:add_selection(cursor_line, cursor_col, end_line, 0)
end
---Make a movement/selection within the current document, but also
---record to history if necessary so that the movement may be repeated.
---@param motion_mode vimxl.motion_mode
---@param translate_fn vimxl.motion
---@param numerical_argument? number
function VimState:move_or_select(motion_mode, translate_fn, numerical_argument)
-- We might want to switch modes if we are at a "neutral" position into line mode.
-- This behaviour depends on the motion mode.
if motion_mode.is_text_object then
local corner1_line, _, corner2_line, _ = find_corners_from_selection(self.view.doc)
if corner1_line == corner2_line and motion_mode.is_linewise then
self.mode = "v-line"
end
end
if self.mode == "v" then
---@param state vimxl.vimstate
local function vim_style_visual_select_to(state)
for idx, start_line, start_col, end_line, end_col in state.view.doc:get_selections() do
local l1, c1, l2, c2 = vim_style_visual_select_to_impl(state.view.doc, start_line, start_col, state.view, motion_mode, translate_fn, numerical_argument, end_line, end_col)
state.view.doc:set_selections(idx, l1, c1, l2, c2)
end
end
vim_style_visual_select_to(self)
table.insert(self.command_history, {
["type"] = "select_to",
["perform"] = vim_style_visual_select_to,
})
elseif self.mode == "v-block" then
---@param state vimxl.vimstate
local function vim_style_visual_block_select_to(state)
--local start_line, start_col = state.view.doc:get_selection()
--local _, _, end_line, end_col = state.view.doc:get_selection_idx(1)
local start_line, start_col, end_line, end_col, fused = find_corners_from_selection(state.view.doc)
-- Detect if a character is not currently selected and if so correct that.
if start_col == end_col then
start_col = start_col + 1
end
local was_going_forward = start_col > end_col
-- Steal a character so that we go from having the cursor be
-- on a character to having it be between characters.
if was_going_forward then
start_col = start_col - 1
end
local l1, c1, l2, c2 = translate_fn(state.view.doc, start_line, start_col, state.view, numerical_argument)
if state.mode == "v-block" and l2 ~= nil and c2 ~= nil then
-- If we get a text object, just select that text object
-- and enter ordinary visual mode. As is done in Vim.
-- TODO: This doesn't actually seem entirely correct, some text objects like inner word don't do this, while "i(" does.
-- TODO: Do :help text-objects, we can see which text-objects are supposed to force linewise, or force charwise there.
state:set_mode("v")
if c1 == c2 then
c1 = c1 + 1
end
if is_selection_going_forward(l2, c2, l1, c1) then
l1, c1, l2, c2 = l2, c2, l1, c1
end
state.view.doc.set_selection(l1, c1, l2, c2)
return
end
l1, c1, l2, c2 = vim_style_visual_block_select_to_impl(l1, c1, end_line, end_col, was_going_forward)
state:create_block_selection(l1, c1, l2, c2, fused)
end
vim_style_visual_block_select_to(self)
table.insert(self.command_history, {
["type"] = "select_to",
["perform"] = vim_style_visual_block_select_to,
})
elseif self.mode == "v-line" then
---@param state vimxl.vimstate
local function vim_style_visual_block_select_to(state)
local cursor_line, cursor_col, start_line = state.view.doc:get_selection_idx(1)
local end_line = start_line + 1
-- These are not set the first time we enter v-line mode. So therefor we don't
-- set end_line in that case.
local new_cursor_line, new_cursor_col, new_end_line, new_end_col = state.view.doc:get_selection_idx(2)
if new_cursor_line then
local line_count = #state.view.doc.lines
if new_end_line == line_count and new_end_col == #state.view.doc.lines[line_count] then
-- Make sure that we correctly handle the last line.
new_end_line = new_end_line + 1
end
cursor_line, cursor_col, end_line = new_cursor_line, new_cursor_col, new_end_line
end
local old_direction = DIRECTION_FORWARD
if cursor_line == start_line
and (cursor_line + 1 == end_line or cursor_line == end_line) then
old_direction = DIRECTION_NEUTRAL
elseif cursor_line <= start_line then
old_direction = DIRECTION_BACKWARD
end
local l1, c1, l2, c2 = translate_fn(state.view.doc, cursor_line, cursor_col, state.view, numerical_argument)
if state.mode == "v-line" and motion_mode.is_text_object and l2 ~= nil and c2 ~= nil then
if motion_mode.is_linewise then
if old_direction == DIRECTION_NEUTRAL then
-- We got a linewise text object, so select the spanning lines.
-- But only if we have not already started selecting other things.
vim_style_visual_line_select_impl(state.view.doc, l2, c2, l1, c1, DIRECTION_FORWARD)
return
elseif old_direction == DIRECTION_FORWARD then
-- Extend using the text object.
if is_selection_going_forward(l2, c2, l1, c1) then
l1, c1, l2, c2 = l2, c2, l1, c1
end
vim_style_visual_line_select_impl(state.view.doc, l1, c1, start_line, end_line, DIRECTION_FORWARD)
return
end
elseif motion_mode.is_charwise then
-- If we get a text object, just select that text object
-- and enter ordinary visual mode. As is done in Vim.
-- TOOD: This doesn't actually seem entirely correct, some text objects like inner word don't do this, while "i(" does.
state:set_mode("v")
if c1 == c2 then
c1 = c1 + 1
end
if is_selection_going_forward(l2, c2, l1, c1) then
l1, c1, l2, c2 = l2, c2, l1, c1
end
state.view.doc:set_selection(l1, c1, l2, c2)
return
end
end
vim_style_visual_line_select_impl(state.view.doc, l1, c1, start_line, end_line, old_direction)
end
vim_style_visual_block_select_to(self)
table.insert(self.command_history, {
["type"] = "select_to",
["perform"] = vim_style_visual_block_select_to,
})
elseif self.mode == "i" then
self.view.doc:move_to(translate_fn, self.view, numerical_argument)
table.insert(self.command_history, {
["type"] = "move_to",
["translate"] = function (doc, line, col, view)
-- We deliberately ignore l2 and c2 if they are passed.
local l1, c1 = translate_fn(doc, line, col, view, numerical_argument)
return l1, c1
end,
})
else
-- TODO: Should we deliberately remove l2 and c2 here as well?
self.view.doc:move_to(translate_fn, self.view, numerical_argument)
end
end
function VimState:get_mode_name()
if self.mode == "v" then
return "visual"
end
if self.mode == "v-line" then
return "visual line"
end
if self.mode == "v-block" then
return "visual block"
end
if self.mode == "i" then
return "insert"
end
if self.mode == "n" then
return "normal"
end
return self.mode
end
---Should return double nil if not in any visual mode.
---Otherwise it should return the starting location of the select mode.
function VimState:get_visual_start()
local line, col = nil, nil
local line_direction = 0
local start_line = 0
if self.mode == "v" then
local l1, c1, l2, c2 = self.view.doc:get_selection()
line = l2 or l1
col = c2 or c1
elseif self.mode == "v-line" then
line, col, start_line = self.view.doc:get_selection_idx(1)
local new_cursor_line, new_cursor_col, end_line = self.view.doc:get_selection_idx(2)
if new_cursor_line and new_cursor_col then
line = new_cursor_line
col = new_cursor_col
end
if line > start_line or end_line == start_line + 1 then
line_direction = 1
else
line_direction = -1
end
elseif self.mode == "v-block" then
local l1, c1, l2, c2 = find_corners_from_selection(self.view.doc)
col = math.min(c1, c2)
line = math.min(l1, l2)
end
return line, col, line_direction, start_line
end
function VimState:on_char_input(text)
-- This variable has no business being enabled if outside of i-mode.
self.track_primitives = false
if self.numerical_argument == nil and text == "0" then
text = constants.LEADING_ZERO
end
-- Operator got motion cb being set is the same as expecting a motion.
if self.operator_got_motion_cb ~= nil and text == self.operator_name then
text = constants.MOTION_LINE_REPEAT
end
---@type vimxl.keybind_value
local lookup_name = self.keymap[text]
local lookup_type = type(lookup_name)
local as_motion = nil
local as_function = nil
local as_keymap = nil
local as_litexl_command = nil
local as_number = nil
-- Resolve the a function from its name.
if lookup_type == "string" and vim_motions[lookup_name] then
as_motion = vim_motions[lookup_name]
elseif lookup_type == "string" and vim_functions[lookup_name] then
as_function = vim_functions[lookup_name]
elseif lookup_type == "string" and command.map[lookup_name] then
as_litexl_command = lookup_name
elseif lookup_type == "table" then
as_keymap = lookup_name
elseif type(text) == "string" and #text > 0 and string.ubyte(text, 1) <= 57 and string.ubyte(text, 1) >= 48 then
as_number = string.ubyte(text, 1) - 48
elseif lookup_type ~= "nil" then
-- We handle nil elsewhere
core.error("Unsupported type found in keymap for: %s", text)
end
local reset_numerical_argument = true
local reset_keymap = true
if as_keymap ~= nil then
self.keymap = as_keymap
reset_numerical_argument = false
reset_keymap = false
elseif as_litexl_command ~= nil then
command.perform(as_litexl_command)
elseif as_motion ~= nil then
-- Unset the cb to flag that we no longer expect
-- a motion. We do this before calling in case we
-- ask for two motions or something strange like that.
local motion_cb = self.operator_got_motion_cb
self.operator_got_motion_cb = nil
-- Text objects are not allowed in normal mode.
local motion_mode = vim_motionmodes.motion_modes[lookup_name] or vim_motionmodes.MOTION_MODE_CHARWISE
-- Either we move or we call the cb. Anything else would be silly...
if motion_cb then
motion_cb(self, motion_mode, as_motion, self.numerical_argument)
elseif self.mode == "n" then
-- Text objects do not make sense in normal mode.
-- So we ignore motion_mode.
-- However, we do allow them in motion pending mode (which is also mode=="n" in VimXL)
-- Which is why this is done after checking if motion_cb was set.
self:move_or_select(vim_motionmodes.MOTION_MODE_CHARWISE, as_motion, self.numerical_argument)
else
-- Because the key we used for our lookup was
-- found in the vim_motions table
-- we try to use it as such and do a move or select
-- depending on the mode.
self:move_or_select(motion_mode, as_motion, self.numerical_argument)
end
elseif as_function ~= nil and self.operator_got_motion_cb ~= nil then
core.error("Expected motion but got Vim function")
elseif as_function ~= nil then
-- Just a normal bound key, probably.
as_function(self, self.numerical_argument)
if self.operator_got_motion_cb ~= nil then
self.operator_name = text
reset_keymap = false
end
elseif as_number ~= nil then
-- Handle digits.
if self.numerical_argument == nil then self.numerical_argument = 0 end
self.numerical_argument = self.numerical_argument * 10 + as_number
reset_numerical_argument = false
reset_keymap = false
elseif lookup_type == "nil" then
core.error("Key %s not bound", text)
else
core.error("Something is wrong with the Vim if-else chain")
end
if reset_numerical_argument then self.numerical_argument = nil end
if reset_keymap then self:set_correct_keymap() end
if self.repeat_requested then
self.repeat_requested = false
self:repeat_commands(false)
end
end
function VimState:on_text_input(text)
if self.mode == "i" then
table.insert(self.command_history, {
["type"] = "text_input",
["text"] = text
})
self.view.doc:text_input(text)
return
end
if type(text) ~= "string" or #text <= 1 then
-- Since text is just one char (or less) we pass it directly.
-- If we are given a non-string, then it is a special-number, so pass it directly.
self:on_char_input(text)
else
-- The Quetta plugin will sometimes send batched key strokes.
-- There are probably other scenarios where batched keys can happen.
-- We break these up such that if on_text_input is called
-- with batched up commands, we still interpret them correctly
-- as if they were individual commands.
-- We don't want this for i-mode which is why we check for
-- it in the beginning of this function.
for char in text:gmatch(".") do
self:on_char_input(char)
end
end
end
---@alias vimxl.operator_selection_iter_invariant [any, any, vimxl.motion|nil, core.docview, vimxl.motion_mode, number]
---An iterator of document selections that will apply a motion before
---returning a result.
---@see get_operator_selections
---@param invariant vimxl.operator_selection_iter_invariant
---@param control number
local function get_operator_selections_iter(invariant, control)
local selection_invariant = invariant[1]
local selection_iterator = invariant[2]
local idx, l1, c1, l2, c2 = selection_iterator(selection_invariant, control)
local start_l, start_c = l1, c1
if not idx then
return nil
end
local motion = invariant[3]
local view = invariant[4]
local motion_mode = invariant[5]
local numerical_argument = invariant[6]
if motion then
l1, c1, l2, c2 = motion(view.doc, start_l, start_c, view, numerical_argument)
end
-- If the function wasn't a text-object then we make sure the selection
-- goes from the cursor to the new location.
if l2 == nil then l2 = start_l end
if c2 == nil then c2 = start_c end
-- Sanitize them for the is_linewise step.
-- Otherwise we end up incrementing the wrong thing.
if l1 > l2 or l1 == l2 and c1 > c2 then
l1, c1, l2, c2 = l2, c2, l1, c1
end
if motion_mode.is_linewise then
c1 = 0
c2 = 0
l2 = l2 + 1
end
return idx, l1, c1, l2, c2, 1
end
---In v-line mode we'd rather return the selection
---as one continuous selection for our operators to use.
---So we merge/fuse all selections into one that contains all
---of the selections.
---@see get_operator_selections
---@param doc core.doc
---@param control number
local function merged_selection_iter(doc, control)
if control > 0 then return end
---@type number
local start_line, start_col, end_line, end_col = #doc.lines + 1, 0, 0, 0
-- How many were combined?
local fused = 0
for _, l1, c1, l2, c2 in doc:get_selections() do
fused = fused + 1
-- TODO: If only get_selections was typed properly, then ugly casts like these wouldn't exist ;>
---@cast l1 number
if start_line > l1 or start_line == l1 and start_col > c1 then
start_line, start_col = l1, c1
end
if start_line > l2 or start_line == l2 and start_col > c2 then
start_line, start_col = l2, c2
end
if l1 > end_line or l1 == end_line and c1 > end_col then
end_line, end_col = l1, c1
end
if l2 > end_line or l2 == end_line and c2 > end_col then
end_line, end_col = l2, c2
end
end
return control + 1, start_line, start_col, end_line, end_col, fused
end
---Call this to get some selections that represent the area of the text
---that the operator should make changes to.
---It should support all known visual-select modes.
---And will apply the motion automatically for you.
---@param motion_mode vimxl.motion_mode
---@param motion? vimxl.motion
---@param numerical_argument? number
function VimState:get_operator_selections(motion_mode, motion, numerical_argument)
if self.mode == "v-line" and motion == nil then
return merged_selection_iter, self.view.doc, 0
end
local selection_iter, selection_invariant, control = self.view.doc:get_selections(false, true)
local invariant = { selection_invariant, selection_iter, motion, self.view, motion_mode, numerical_argument }
return get_operator_selections_iter, invariant, control
end
---Call this if you want the entire command history
---to be repeatable with an arbitrary amount.
---Default is taken from the numerical_argument of the view.
---@param numerical_argument? number
function VimState:begin_repeatable_history(numerical_argument)
table.insert(self.command_history, {
["type"] = "repeat_everything",
["number"] = numerical_argument or 1
})
end
---The simple case where we just rerun the command
---n times... We do so by prepending a repeat_everything
---to the repeatable_commands array.
---@param perform vimxl.perform_no_number
---@param numerical_argument? number
function VimState:begin_naive_repeatable_command(perform, numerical_argument)
if self.mode ~= "n" then
table.insert(self.command_history, {
["type"] = "repeat_everything",
["number"] = numerical_argument or 1,
})
table.insert(self.command_history, {
["type"] = "command",
["perform"] = perform,
})
perform(self)
else
self.repeatable_commands = {
{
["type"] = "repeat_everything",
["number"] = numerical_argument or 1,
},
{
["type"] = "command",
["perform"] = perform,
}
}
self.repeat_requested = true
end
end
---The most advanced case where the command decides by
---itself how it should handle the numerical argument.
---Note this is done so that the "." command can change this
---parameter by taking its own numerical_argument and
---substituting it in.
---@param perform vimxl.perform_with_optional_number
---@param numerical_argument? number
function VimState:begin_command_supporting_numerical_argument(perform, numerical_argument)
if self.mode ~= "n" then
table.insert(self.command_history, {
["type"] = "command_supporting_number",
["perform"] = perform,
["number"] = numerical_argument,
})
perform(self, numerical_argument)
else
self.repeatable_commands = {
{
["type"] = "command_supporting_number",
["perform"] = perform,
["number"] = numerical_argument,
}
}
self.repeat_requested = true
end
end
---Do not call directly. Set requested_repeats instead.
---@param minus_one boolean Should we do one less repeat_everything?
function VimState:repeat_commands(minus_one)
-- Not setting this to false could have disasterous rammification
-- if repeat-commands is somehow called in i-mode.
-- Which is to say, risk of recursive calls.
self.track_primitives = false
local n_times = 1
local repeat_everything_seen = false
local completed_iterations = 1
if minus_one then completed_iterations = 2 end
while n_times > 0 do
n_times = n_times - 1
for _, v in ipairs(self.repeatable_commands) do
if v.type == "text_input" then
self.view.doc:text_input(v.text)
elseif v.type == "command" then
v.perform(self)
elseif v.type == "repeat_everything" then
if not repeat_everything_seen then
repeat_everything_seen = true
n_times = v.number - completed_iterations
end
-- Otherwise a noop.
elseif v.type == "command_supporting_number" then
v.perform(self, v.number)
elseif v.type == "select_to" then
v.perform(self)
elseif v.type == "move_to" then
self.view.doc:move_to(v.translate, self)
elseif v.type == "remove_text" then
local _, _, l2, c2 = self.view.doc:get_selection_idx(1)
local l1, c1 = self.view.doc:position_offset(l2, c2, -v.amount)
self.view.doc:remove(l1, c1, l2, c2)
else
core.error("Unknown history type: %s", v.type)
end
end
end
end
---In i-mode there are no keybinds that are handled by this plugin.
---Therfor let this be empty!
---Only really exists for symmetry and correctness. But we could do without it.
---@see set_correct_keymap
local empty_keymap_for_i_mode = {}
---Based on the current mode, pick an appropriate keymap.
function VimState:set_correct_keymap()
-- Clear it, otherwise we have to set keymap to
-- a map containing motions. We don't want that as
-- that is not the Vim behaviour.
self.operator_got_motion_cb = nil
if self.mode == "n" then
self.keymap = vim_keymap.normal
elseif self.mode == "v" then
self.keymap = vim_keymap.visual
elseif self.mode == "v-block" then
self.keymap = vim_keymap.visual_block
elseif self.mode == "v-line" then
self.keymap = vim_keymap.visual_line
elseif self.mode == "i" then
self.keymap = empty_keymap_for_i_mode
else
core.error("VimState:set_correct_keymap() has gone insane")
end
end
---When entering different visual selects, they might want to
---setup a specific form LiteXL selections (i.e charwise).
---We can make sure at least a character is selected by navigating to it.
---@type vimxl.motion
local function translate_noop(_, line, col)
return line, col
end
---Makes sure nothing is selected. Useful for leaving visual modes mainly.
---@param state vimxl.vimstate
---@param is_charwise boolean When true, the operation will collapse as if the cursor is on a char, not between.
---@param go_one_back boolean Used if previous was insert-mode to mimic Vim better by going one char back.
local function collapse_selection(state, is_charwise, go_one_back)
local l1, c1, l2, c2 = state.view.doc:get_selection()
if is_charwise and is_selection_going_forward(l1, c1, l2, c2) then
c1 = c1 - 1
end
if go_one_back then
c1 = c1 - 1
end
state.view.doc:set_selection(l1, c1)
end
---@param mode vimxl.mode
function VimState:set_mode(mode)
local prev_mode = self.mode
self.mode = mode
-- We don't want the user to think that we are slow.