-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
1707 lines (1541 loc) · 44.5 KB
/
Copy pathinit.lua
File metadata and controls
1707 lines (1541 loc) · 44.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
-- mod-version:3
-- Module: vim
-- Author: S.Ghamri
-------------------------------------------------------------
-- TODO: implement t: before occurrence and T: after occurrence
-- TODO: how to repeat last command
-- FIX: resolve_motion like: follow delete example
-- yy must work, it should select the full line
-- 5yw not working
local core = require("core")
local common = require("core.common")
local command = require("core.command")
local style = require("core.style")
local translate = require("core.doc.translate")
local DocView = require("core.docview")
local config = require("core.config")
local search = require("core.doc.search")
local keymap = require("core.keymap")
config.vim = {
unified_search = true,
unnamedplus = true,
flash_color = style.caret,
expandtab = (config.tab_type == "soft"),
}
-- m: forward definitions
local get_translation
local resolve_motion
local get_region
local find_match
local vim = {
mode = "normal", -- normal, visual, command, insert, delete
command_buffer = "",
search_query = "",
command_map = {},
operators = {},
motions = {},
max_col = 0,
normal_keys = {},
visual_keys = {},
remap_keys = {},
known_keymaps = {},
dir = 0,
}
vim.registers = {
['"'] = "", -- unnamed register
["0"] = "", -- yank register
["1"] = "", -- last delete
["+"] = "", -- system clipboard
["*"] = "", -- primary selection (linux)
-- "% : abs_filename
-- "/ : last search query (vim.search_query)
}
local state = { -- state to track commands
count = "",
count2 = "",
operator = nil,
text_object = nil,
motion_prefix = nil,
motion = nil,
register = nil,
}
-- m: helpers
local function get_count()
return tonumber(state.count) or 1
end
local function get_doc() -- gets active document view
local dv = core.active_view
if not dv or not dv.doc then
return nil
end
return dv.doc
end
local function center_selection_in_view(_, line) -- put selected line in middle of visible view
local docview = core.active_view
if not docview then
return
end
local minl, maxl = docview:get_visible_line_range()
local center = true
if line <= maxl + 1 and line >= minl - 1 then
return
end
docview:scroll_to_line(line, false, center)
end
local function set_vim_caret() -- reduce caret width when in insert mode
if vim.mode == "insert" then
style.caret_width = common.round(1.5 * SCALE)
else
style.caret_width = common.round(8 * SCALE)
end
end
local function deselect(to_start) -- collapse selection
local doc = get_doc()
if not doc then
return
end
for idx, start_line, start_col, end_line, end_col in doc:get_selections(true) do
if to_start then
doc:set_selections(idx, start_line, start_col)
else
doc:set_selections(idx, end_line, end_col)
end
end
end
local function reset_state()
state.count = ""
state.operator = nil
state.motion = nil
state.motions_prefix = nil
state.register = nil
state.text_object = nil
vim.command_buffer = "" -- debug
end
-- m: command line vim integration
local command_line = require("plugins.command-line")
command_line.set_item_name("status:vim")
command_line.add_status_item()
command_line.minimal_status_view = true -- only item will show
local function decorate_with_vim(instance)
local orig_start = instance.start_command
function instance:start_command(opts)
local result = orig_start(self, opts)
vim.set_mode("command")
return result
end
local orig_exec = instance.execute_or_return_command
function instance:execute_or_return_command()
local result = orig_exec(self) -- if closed in_command here is false
if not command_line.is_active() then -- no command line prompt is open
vim.set_mode("normal")
end
return result
end
local orig_cancel = instance.cancel_command
function instance:cancel_command()
local result = orig_cancel(self)
vim.set_mode("normal")
return result
end
return instance
end
local instance_command = decorate_with_vim(command_line.new())
instance_command:set_prompt(":")
local forward_search = decorate_with_vim(command_line.new())
forward_search:set_prompt("/")
local backward_search = decorate_with_vim(command_line.new())
backward_search:set_prompt("?")
-- keys that can input
local input_keys = {
["return"] = true,
["backspace"] = true,
["tab"] = true,
["delete"] = true,
["insert"] = true,
["space"] = true,
}
vim.mod_keys = {
ctrl = false,
alt = false,
shift = false,
}
vim.modifier_keys = {
["left ctrl"] = false,
["right ctrl"] = false,
["left shift"] = false,
["right shift"] = false,
["left alt"] = false,
["right alt"] = false,
}
-- keys remaps
vim.remap_keys = {
["down"] = "j",
["up"] = "k",
["left"] = "h",
["right"] = "l",
}
-- known vim prefixes
vim.prefix = {
i = true,
a = true,
f = true,
F = true,
t = true,
T = true,
g = true,
G = true,
z = true,
Z = true,
}
-- accumulate keys
local pending = ""
local prev_dig = false
-- m: input_handler
local function handle_input(key)
if vim.mode == "insert" or vim.mode == "command" then
return false -- release input
end
-- accumulate count digits only if no operator pending
if key:match("[1-9]") or (key == "0" and prev_dig) then
prev_dig = true
state.count = (state.count or "") .. key
return true
end
-- smoothing: x count <operator> x count (prefix+motion)
if vim.motions[key] == nil
and vim.operators[key] == nil
and vim.normal_keys[key] == nil
and vim.visual_keys[key] == nil
and vim.prefix[key] == nil
and not vim.prefix[pending:sub(1, 1)]
or #pending > 2
then
vim.echo("!!!smoothness filter!!!")
pending = ""
reset_state()
return true
end
pending = pending .. key -- by now all numbers are filtred
prev_dig = false -- other key then digit is pressed
vim.echo("Command: %s <%s> %s", state.count, pending, state.count2)
if vim.mode == "normal" then
state.operator = vim.operators["move"] -- default normal mode operator
if not (#pending == 2 and vim.prefix[pending:sub(1, 1)]) then -- not in a motion
if vim.normal_keys and vim.normal_keys[key] then
vim.normal_keys[key]()
pending = ""
return true
end
if vim.normal_keys and vim.normal_keys[pending] then -- normal keymaps combos
vim.normal_keys[pending]()
pending = ""
reset_state()
return true
end
end
elseif vim.mode == "visual" or vim.mode == "visual-line" then
if vim.visual_keys and vim.visual_keys[key] then
vim.visual_keys[key]()
pending = ""
reset_state()
vim.set_mode("normal")
return true
end
end
if vim.mode == "visual" then
state.operator = vim.operators["select"]
end
if vim.mode == "visual-line" then
state.operator = vim.operators["line_select"]
end
-- operator go o-pending waiting for motion
if vim.operators[pending] and vim.mode ~= "o-pending" then
state.operator = vim.operators[pending]
local count = get_count()
if vim.mode == "visual" or vim.mode == "visual-line" then
state.operator(count)
reset_state()
else
vim.set_mode("o-pending")
end
pending = ""
return true
end
-- execute motions
if vim.motions[pending] then
local count = get_count()
if state.operator then
state.operator(count, vim.motions[pending], nil, nil)
end
pending = ""
reset_state()
return true
end
-- pending with prefix i or a
if #pending == 2 and pending:sub(1, 1):match("[iafFtT]") then
state.motion_prefix = pending:sub(1, 1)
state.text_object = pending:sub(2, 2)
if state.motion_prefix and state.text_object then
local count = get_count()
state.operator(count, nil, state.motion_prefix, state.text_object)
pending = ""
reset_state()
return true
end
end
return true
end
local function update_mod_keys()
vim.mod_keys.ctrl = (vim.modifier_keys["left ctrl"] or vim.modifier_keys["right ctrl"]) or false
vim.mod_keys.shift = (vim.modifier_keys["left shift"] or vim.modifier_keys["right shift"]) or false
vim.mod_keys.alt = (vim.modifier_keys["left alt"] or vim.modifier_keys["right alt"]) or false
end
local function build_combo(key) -- detects a combo
local mods = {}
if vim.mod_keys.ctrl then
table.insert(mods, "ctrl")
end
if vim.mod_keys.alt then
table.insert(mods, "alt")
end
if vim.mod_keys.shift then
table.insert(mods, "shift")
end
table.insert(mods, key:lower())
return table.concat(mods, "+")
end
-- m: on_event override
local original_on_event = core.on_event
function core.on_event(type, a, ...)
local doc = get_doc()
if doc and doc.filename then
if type == "textinput" then
if handle_input(a) then
return true -- block
end
elseif type == "keypressed" then
if vim.modifier_keys[a] == nil and (vim.mod_keys.ctrl or vim.mod_keys.alt or vim.mod_keys.shift) then
local cmb = build_combo(a)
if vim.known_keymaps[cmb] == nil then -- nothing, let key reach original on_event
else
if handle_input(cmb) then
return true
end
end
end
if vim.modifier_keys[a] ~= nil then
vim.modifier_keys[a] = true
update_mod_keys()
end
-- some key remaps to input
if vim.remap_keys[a] and vim.mode ~= "normal" then
if handle_input(vim.remap_keys[a]) then
return true
end
end
if a == "escape" then -- no return to let escape reach other modules
instance_command:cancel_command()
forward_search:cancel_command()
deselect(true)
pending = ""
reset_state()
vim.set_mode("normal")
end
if vim.mode == "normal" then
if input_keys[a] then
return true -- block in normal mode
end
end
elseif type == "keyreleased" then
if vim.modifier_keys[a] ~= nil then
vim.modifier_keys[a] = false
update_mod_keys()
end
end
end
return original_on_event(type, a, ...)
end
local function return_to_line()
local doc = get_doc()
if not doc then
return
end
local l, c = doc:get_selection()
local line_text = doc.lines[l] or ""
if c > #line_text - 1 then
doc:set_selection(l, #line_text - 1, l, #line_text - 1)
end
end
-- m: mode_switch
function vim.set_mode(m)
local message = {}
set_vim_caret()
if m == "normal" then
return_to_line()
command_line.show_message({}, 0) -- 0 = permanent
elseif m == "insert" then
style.caret_width = common.round(1.5 * SCALE)
message = {
style.accent,
"-- INSERT --",
}
command_line.show_message(message, 0) -- 0 = permanent
elseif m == "visual" then
local doc = get_doc()
if not doc then
return
end
local l, c = doc:get_selection() -- c starts from 1
message = {
style.text,
"-- VISUAL --",
}
command_line.show_message(message, 0) -- 0 = permanent
elseif m == "visual-line" then
local doc = get_doc()
if not doc then
return
end
local l, c = doc:get_selection() -- c starts from 1
doc:set_selection(l, #doc.lines[l], l, 1)
message = {
style.text,
"-- VISUAL-LINE --",
}
command_line.show_message(message, 0) -- 0 = permanent
elseif m == "o-pending" then
message = {
style.text,
"-- O-PENDING --",
}
command_line.show_message(message, 0) -- 0 = permanent
end
vim.mode = m
end
-- default ex commands
local vim_ex_commands = {
["w"] = { action = "doc:save", desc = "Save file" },
["q"] = { action = "core:quit", desc = "Quit editor" },
["q!"] = { action = "core:force-quit", desc = "Force quit" },
["qa"] = { action = "core:quit", desc = "Quit all" },
["e!"] = { action = "doc:reload", desc = "Reload fresh" },
["bd"] = { action = "root:close", desc = "Delete buffer" },
["path"] = {
action = function()
local doc = get_doc()
if not doc then
return
end
local filepath = doc.abs_filename
system.set_clipboard(filepath)
vim.echo("%s", filepath)
end,
desc = "Retrieve File Path",
},
}
-- vim.register_command("greet", function(name) core.log("Hello, " .. (name or "world") .. "!") end, "Greet someone")
function vim.register_command(cmd, action, desc)
if not cmd or not action then
core.log("invalid command registration")
return
end
vim_ex_commands[cmd] = { action = action, desc = desc or "" }
end
-- run command with optional arguments
function vim.run_command(cmd, ...)
local entry = vim_ex_commands[cmd]
if not entry then
core.log("vim unknown command: " .. cmd)
return
end
if type(entry.action) == "function" then
entry.action(...)
else
command.perform(entry.action, ...)
end
end
-- m: keymaps registration
-- TODO: not tested, test it
function vim.register_keymap(mode, combo, fn)
local function clean(s)
return s:lower():match("^%s*(.-)%s*$")
end
local parts = {}
for part in combo:gmatch("[^%+]+") do
table.insert(parts, clean(part))
end
local key
if #parts > 1 then
key = table.concat(parts, "+") -- "ctrl+x"
else
key = parts[1] -- "x"
end
vim.known_keymaps[key] = true
if mode == "normal" then
vim.normal_keys[key] = fn
elseif mode == "visual" then
vim.visual_keys[key] = fn
end
end
function vim.forward_search(query)
local doc = get_doc()
if not doc then
return
end
local line, col, line2, col2
local l, c = doc:get_selection()
line, col, line2, col2 = search.find(doc, l, c, query, { wrap = config.vim.unified_search })
if line and col and line2 and col2 then
vim.search_query = query
doc:set_selection(line2, col2 - 1, line, col)
center_selection_in_view(doc, line)
end
end
function vim.backward_search(query)
local doc = get_doc()
if not doc then
return
end
local l, c = doc:get_selection()
local line, col, line2, col2 = search.find(doc, l, c, query, { wrap = config.vim.unified_search, reverse = true })
if line and col and line2 and col2 then
vim.search_query = query
doc:set_selection(line, col, line2, col2 - 1)
center_selection_in_view(doc, line)
end
end
function vim.get_suggests(input)
local suggestions = {}
for name, entry in pairs(vim_ex_commands) do
if name:find("^" .. input) then
table.insert(suggestions, {
name = name,
desc = entry.desc or "",
action = entry.action,
})
end
end
return suggestions
end
local function get_text(line1, col1, line2, col2) -- m: get_text
local doc = get_doc()
if not doc then
return
end
if line1 > line2 or (line1 == line2 and col1 > col2) then
line1, col1, line2, col2 = line2, col2, line1, col1
end
local out = {}
for line = line1, line2 do
local t = doc.lines[line] or ""
local s = (line == line1) and col1 or 1
local e = (line == line2) and col2 or #t
out[#out + 1] = t:sub(s, e)
end
return table.concat(out, "")
end
-- m: flash()
local function flash(l1, c1, l2, c2, color, time, doc)
core.add_thread(function()
local old_caret_width = style.caret_width
style.caret_width = 0
local old_selection_style = style.selection
style.selection = color
doc:set_selection(l1, c1, l2, c2)
coroutine.yield(time)
style.caret_width = old_caret_width
style.selection = old_selection_style
core.redraw = true
deselect(true) -- selection is lost here
end)
end
local function get_yank_type(doc, text, l1, c1, l2, c2)
local line_width = 0
if l1 == l2 then -- single line case
line_width = #doc.lines[l1]
if #text == line_width then
return "line"
end
else
if (c1 == 1 and c2 == #doc.lines[l2]) or (c2 == 1 and c1 == #doc.lines[l1]) then
return "line"
end
end
return "char"
end
-- m: yank()
local function yank(l1, c1, l2, c2, flash_time)
local ft = flash_time or 0
local doc = get_doc()
if not doc then
return
end
if not (l1 and c1 and l2 and c2) then
l1, c1, l2, c2 = doc:get_selection()
end
local yank_type = "char"
local text = get_text(l1, c1, l2, c2)
if not text then
return
end
yank_type = get_yank_type(doc, text, l1, c1, l2, c2)
-- update registers
if config.vim.unnamedplus then
system.set_clipboard(text)
vim.registers["+"] = { text = text, type = yank_type } -- clipboard does not have metadata
end
vim.registers['"'] = { text = text, type = yank_type }
-- only when yank
vim.registers["0"] = { text = text, type = yank_type }
if ft ~= 0 then
flash(l1, c1, l2, c2, config.vim.flash_color, ft, doc)
end
end
-- m: delete()
local function delete(el, ec, sl, sc)
local doc = get_doc()
if not doc then
return
end
if not (el and ec and sl and sc) then
el, ec, sl, sc = doc:get_selection()
end
local reg_text = get_text(sl, sc, el, ec)
if not reg_text then
return
end
local yank_type = get_yank_type(doc, reg_text, sl, sc, el, ec)
local text = ""
if el == sl then
text = doc.lines[el]
if ec >= sc then
if ec == #text then
el, ec = el + 1, 1
else
ec = ec + 1
end
else
if sc == #text then
sl, sc = sl + 1, 1
else
sc = sc + 1
end
end
elseif el > sl then
text = doc.lines[el]
if ec == #text then
el, ec = el + 1, 1
else
ec = ec + 1
end
else
text = doc.lines[sl]
if sc == #text then
sl, sc = sl + 1, 1
else
sc = sc + 1
end
end
-- TODO: do i need this part ?
if sl > el or (sl == el and sc > ec) then
sl, el, sc, ec = el, sl, ec, sc
end
if config.vim.unnamedplus then
system.set_clipboard(reg_text)
vim.registers["+"] = { text = reg_text, type = yank_type }
end
vim.registers['"'] = { text = reg_text, type = yank_type }
vim.registers["1"] = { text = reg_text, type = yank_type }
doc:remove(sl, sc, el, ec)
end
-- insert lines
local function get_insert_line(doc, direction, l, c)
if not doc then
return
end
local insert_line = 0
if direction == "down" then
if l == #doc.lines and doc.lines[l] == "" then
insert_line = l
else
insert_line = l + 1
end
elseif direction == "up" then
insert_line = l
end
return insert_line
end
-- m: put()
local function put(direction, count)
local doc = get_doc()
if not doc then
return
end
local flash_time = 0.2
count = count or 1
local l, c = doc:get_selection()
local reg = vim.registers['"'] or { text = "", type = "char" }
local yank_type = reg.type or "char"
if config.vim.unnamedplus then
reg = { text = system.get_clipboard(), type = "char" }
if vim.registers["+"].text == reg.text then
yank_type = vim.registers["+"].type
else
vim.registers["+"] = reg
end
end
if not reg.text then
return
end
local text = reg.text or ""
if direction ~= "up" then
direction = "down"
end
local lines = {}
for line in text:gmatch("([^\n]+)") do
table.insert(lines, line)
end
for _ = 1, count do
if yank_type == "line" then
local line_textt = ""
local insert_line = get_insert_line(doc, direction, l, c)
if insert_line > #doc.lines then -- add empty line
local t = doc.lines[#doc.lines] or ""
doc:insert(#doc.lines, #t, "\n")
doc:set_selection(#doc.lines, 1)
end
if direction == "down" then -- downward
for i = #lines, 1, -1 do
line_textt = lines[i]
if not line_textt:match("\n$") then
line_textt = line_textt .. "\n"
end
doc:insert(insert_line, 1, line_textt)
doc:set_selection(insert_line, 1)
end
flash(insert_line, 1, insert_line + #lines - 1, #lines[#lines], config.vim.flash_color, flash_time, doc)
else -- upward
for i, line_text in ipairs(lines) do
if not line_text:match("\n$") then
line_textt = line_text .. "\n"
end
doc:insert(insert_line + i - 1, 1, line_textt)
doc:set_selection(insert_line + i - 1, 1)
end
flash(insert_line, 1, insert_line + #lines - 1, #line_textt, config.vim.flash_color, flash_time, doc)
end
else -- char
doc:insert(l, c + 1, text) -- +1 after cursor
local nl, nc = doc:position_offset(l, c, #text)
doc:set_selection(nl, nc)
flash(l, c + 1, nl, nc, config.vim.flash_color, flash_time, doc)
end
end
end
resolve_motion = function(l, c, motion, motion_prefix, text_object) -- only region return 4 values
local doc = get_doc()
if not doc then
return
end
if not l or not c then
l, c = doc:get_selection()
end
if motion_prefix and text_object then
if motion_prefix == "a" or motion_prefix == "i" then
return get_region(l, c, motion_prefix, text_object)
elseif motion_prefix == "f" or motion_prefix == "F" then
local this_char = get_translation("this-char")
local dir = motion_prefix == "f" and 1 or -1
local newl, newc = this_char(doc, l, c, text_object, dir)
if newl ~= l then
newl = l
newc = c
end
return newl, newc, nil, nil
elseif motion_prefix == "t" or motion_prefix == "T" then
-- TODO: implement 't' and 'T'
end
end
if motion and type(motion) == "function" then
-- new l, new c, old l, old c
return motion(doc, l, c)
end
end
-- m: operators
vim.operators = {
["move"] = function(count, motion, motion_prefix, text_object)
local doc = get_doc()
if not doc then
return
end
local l1, c1, l2, c2
for _ = 1, count do
l1, c1, _, _ = resolve_motion(nil, nil, motion, motion_prefix, text_object)
if not (l1 and c1) then
return
end
doc:set_selection(l1, c1, l1, c1)
center_selection_in_view(doc, l1)
end
end,
["select"] = function(count, motion, motion_prefix, text_object)
local doc = get_doc()
if not doc then
return
end
local endl, endc, startl, startc = doc:get_selection()
local l, c
for i = 1, count do
endl, endc, l, c = resolve_motion(endl, endc, motion, motion_prefix, text_object)
end
if l and c then
if not (endl == l and endc == c) then -- region
startl = l
startc = c
end
end
if not (endl and endc and startl and startc) then
return
end
doc:set_selection(endl, endc, startl, startc)
end,
["line_select"] = function(_, motion, motion_prefix, text_object)
local doc = get_doc()
if not doc then
return
end
local endl, endc, startl, startc = doc:get_selection() -- line already selected
endl, endc = resolve_motion(nil, nil, motion, motion_prefix, text_object)
if not (endl and endc and startl and startc) then
return
end
local text = doc.lines[endl]
if startl == endl then
if vim.dir > 0 then
startc = #text
endc = 1
elseif vim.dir < 0 then
startc = 1
endc = #text
else
-- pass
end
elseif startl > endl then
text = doc.lines[startl]
if not text then
return
end
startc = #text
endc = 1
elseif startl < endl then
text = doc.lines[endl]
if not text then
return
end
startc = 1
endc = #text
end
doc:set_selection(endl, endc, startl, startc)
end,
["d"] = function(_, motion, motion_prefix, text_object)
if motion or (motion_prefix and text_object) then
local doc = get_doc()
if not doc then
return
end
local l1, c1, l2, c2 = resolve_motion(nil, nil, motion, motion_prefix, text_object)
doc:set_selection(l1, c1, l2, c2)
end
delete()
vim.set_mode("normal") -- after y and p return to normal
end,
["y"] = function(count, motion, motion_prefix, text_object)
local doc = get_doc()
if not doc then
return
end
local l, c = doc:get_selection()
if motion or (motion_prefix and text_object) then
local oldl, oldc = l, c
local newl, newc = l, c
for i = 1, count do
newl, newc, oldl, oldc = resolve_motion(newl, newc, motion, motion_prefix, text_object)
end
yank(newl, newc, l, c, 0.6)
else
yank(nil, nil, nil, nil, 0.6)
end
vim.set_mode("normal")
end,
[">"] = function(count, motion, motion_prefix, text_object)
local doc = get_doc()
local endl, endc, startl, startc = doc:get_selection()
if startl == endl and endc == startc then
local l = startl
for i = 0, count - 1 do
if (l + i) < #doc.lines then
if config.vim.expandtab then
doc:insert(l + i, 1, string.rep(" ", config.indent_size))
else
doc:insert(l + i, 1, "\t")
end
end
end
else
local from = math.min(startl, endl)
local to = math.max(startl, endl)
for i = from, to do
if config.vim.expandtab then
doc:insert(i, 1, string.rep(" ", count * config.indent_size))
else
doc:insert(i, 1, string.rep("\t", count))
end
end