-
Notifications
You must be signed in to change notification settings - Fork 0
/
vim.lua
2059 lines (1777 loc) · 61.4 KB
/
vim.lua
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:2 -- lite-xl 2.0
--[[
vim.lua - see end of file for license information
TODO LIST
(IN PROGRESS) combos
- diw, cit, ...
(IN PROGRESS) commands
- :q!
- (IN PROGRESS) :s/foo/bar/g
(IN PROGRESS) number + command (123g, 50j, d3w, y10l)
(IN PROGRESS) marks (``, m)
- last change (`.)
- yank/change/delete to mark (y`a)
- uppercase marks (mA)
repeat (.)
delete other windows (ctrl+w o)
replace mode (shift+r)
replace in visual block
TOFIX LIST
(high) tab indent (<<, >>) is broken
(high) indent left (<<) when indent is too small deletes the line
(high) substitution with certain characters crashes
(low) cursor should always stay in view (ctrl+e/y, mouse wheel)
(low) autocomplete shows up when using find (f)
(low) visual block insert gets misaligned when moving cursor from bottom to top
(low) (@@) does not work
--]]
--
local core = require "core"
local keymap = require "core.keymap"
local command = require "core.command"
local CommandView = require "core.commandview"
local DocView = require "core.docview"
local StatusView = require "core.statusview"
local Doc = require "core.doc"
local style = require "core.style"
local translate = require "core.doc.translate"
local common = require "core.common"
local config = require "core.config"
local is_lite_xl = system.get_file_info "data/core/start.lua"
local function dv()
return core.active_view
end
local function doc()
return core.active_view.doc
end
local mode = "normal" -- one of: "normal", "visual", "insert"
local visual_submode -- one of: nil, "line", "block"
local visual_block_state = {
current = "stopped", -- one of: "stopped", "recording", "playing"
event_buffer = {},
selection = {}
}
local function normal_mode()
mode = "normal"
visual_submode = nil
end
local function visual_mode(submode)
mode = "visual"
visual_submode = submode
end
local function insert_mode()
mode = "insert"
visual_submode = nil
end
-- doc:get_selection, but it gets vim's visual region
local function get_selection(doc)
local l1, c1, l2, c2 = doc:get_selection(true)
if visual_submode == "block" then
core.error "get_selection while in visual block"
elseif visual_submode == "line" then
c1 = 1
c2 = #doc.lines[l2]
else
c2 = c2 + 1
end
return l1, c1, l2, c2
end
-- used for number + command (50j to go down 50 lines, y3w to copy 3 words, etc)
local n_repeat = 0
local relative_line_mode = false
local enable_linenum = true
local stroke_combo_tree = {
normal = {
["ctrl+w"] = {},
["shift+,"] = {},
["shift+."] = {},
["shift+z"] = {},
c = {},
d = {
g = {},
["#"] = {}
},
g = {},
y = {
["#"] = {}
}
},
visual = {
g = {},
["shift+,"] = {},
["shift+."] = {}
},
insert = {}
}
local stroke_combo_loc = stroke_combo_tree[mode] -- should be a reference to an item in stroke_combo_tree
local stroke_combo_string = "" -- key buffer
local previous_exec_command = ""
local exec_commands = {
w = "doc:save",
q = "root:close",
wq = "vim:save-and-close",
x = "vim:save-and-close",
nohl = "vim:nohl",
sort = "vim:sort-lines"
}
local substitute = {
from = "",
to = "",
mods = "",
display = false
}
local previous_search_command = ""
local search_text = ""
local should_highlight = false
local macros = {
state = "stopped",
key = nil,
previously_played = nil,
buffers = {}
}
local handled_events = {
keypressed = true,
keyreleased = true,
textinput = true
}
local core_on_event = core.on_event
function core.on_event(type, ...)
local res = core_on_event(type, ...)
if visual_block_state.current == "recording" and handled_events[type] then
table.insert(visual_block_state.event_buffer, {type, ...})
end
if macros.state == "recording" and handled_events[type] then
table.insert(macros.buffers[macros.key], {type, ...})
end
return res
end
local mini_mode -- either nil, or a function in mini_mode_callbacks
local previous_find_text
local mini_mode_callbacks = {
find = function(input_text)
previous_find_text = input_text
local l1, c1, l2, c2 = doc():get_selection()
local text = doc():get_text(l1, 1, l1, math.huge)
for i = c1 + 1, #text do
local c = text:sub(i, i)
if c == input_text then
if mode == "normal" then
doc():set_selection(l1, i)
elseif mode == "visual" then
doc():set_selection(l1, i, l2, c2)
end
return
end
end
core.error("Can't find " .. input_text)
end,
find_backwards = function(input_text)
previous_find_text = input_text
local l1, c1, l2, c2 = doc():get_selection()
local text = doc():get_text(l1, 1, l1, math.huge)
for i = c1 - 1, 1, -1 do
local c = text:sub(i, i)
if c == input_text then
if mode == "normal" then
doc():set_selection(l1, i)
elseif mode == "visual" then
doc():set_selection(l1, i, l2, c2)
end
return
end
end
core.error("Can't find " .. input_text)
end,
find_til = function(input_text)
previous_find_text = input_text
local l1, c1, l2, c2 = doc():get_selection()
local text = doc():get_text(l1, 1, l1, math.huge)
for i = c1 + 1, #text do
local c = text:sub(i, i)
if c == input_text then
if mode == "normal" then
doc():set_selection(l1, i - 1)
elseif mode == "visual" then
doc():set_selection(l1, i - 1, l2, c2)
end
return
end
end
core.error("Can't find " .. input_text)
end,
find_til_backwards = function(input_text)
previous_find_text = input_text
local l1, c1, l2, c2 = doc():get_selection()
local text = doc():get_text(l1, 1, l1, math.huge)
for i = c1 - 1, 1, -1 do
local c = text:sub(i, i)
if c == input_text then
if mode == "normal" then
doc():set_selection(l1, i - 1)
elseif mode == "visual" then
doc():set_selection(l1, i - 1, l2, c2)
end
return
end
end
core.error("Can't find " .. input_text)
end,
record_macro = function(input_text)
assert(macros.state == "stopped")
macros.state = "recording"
macros.buffers[input_text] = {}
macros.key = input_text
core.log(string.format("Recording @%s", input_text))
end,
play_macro = function(input_text)
if input_text == "@" then
input_text = macros.previously_played
end
local buffer = macros.buffers[input_text]
if not buffer then
core.error(string.format("No macro @%s", input_text))
return
end
assert(macros.state == "stopped")
macros.state = "playing"
for _, ev in ipairs(buffer) do
core_on_event(table.unpack(ev))
core.root_view:update()
end
macros.state = "stopped"
macros.previously_played = input_text
end,
replace = function(input_text)
if visual_submode == "block" then
-- TODO
return
end
local l1, c1, l2, c2 = get_selection(doc())
local text = doc():get_text(l1, c1, l2, c2)
doc():remove(l1, c1, l2, c2)
local sub = text:gsub("[^\n]", input_text)
doc():insert(l1, c1, sub)
if mode == "visual" then
doc():set_selection(l1, c1)
normal_mode()
end
end,
set_mark = function(input_text)
local line, col = doc():get_selection()
dv().vim_marks[input_text] = {line, col}
core.log("Set mark: " .. input_text)
end,
goto_mark = function(input_text)
local l1, c1, l2, c2 = doc():get_selection()
local mark = dv().vim_marks[input_text]
if mark then
if doc():has_selection() then
doc():set_selection(mark[1], mark[2], l2, c2)
else
doc():set_selection(mark[1], mark[2])
end
dv().vim_marks["`"] = {l1, c1}
else
core.error("Mark not set: " .. input_text)
end
end
}
local vim_search = {}
function vim_search.init_args(doc, line, col, text, opt)
opt = opt or {}
line, col = doc:sanitize_position(line, col)
if opt.no_case then
if opt.pattern then
text =
text:gsub(
"%%?.",
function()
if str:sub(1, 1) == "%" then
return str
end
return str:lower()
end
)
else
text = text:lower()
end
end
return doc, line, col, text, opt
end
function vim_search.find(doc, line, col, text, opt)
doc, line, col, text, opt = vim_search.init_args(doc, line, col, text, opt)
for line = line, #doc.lines do
local line_text = doc.lines[line]
if opt.no_case then
line_text = line_text:lower()
end
local s = line_text:find(text, col, not opt.pattern)
if s then
return line, s
end
col = 1
end
if opt.wrap then
opt = {no_case = opt.no_case, pattern = opt.pattern}
return vim_search.find(doc, 1, 1, text, opt)
end
end
local function string_rfind(str, pattern, init, plain)
str = str:sub(1, init)
local i = init
local s, e
-- lol
while not s and i ~= 0 do
s, e = str:find(pattern, i, plain)
i = i - 1
end
return s, e
end
function vim_search.find_backwards(doc, line, col, text, opt)
doc, line, col, text, opt = vim_search.init_args(doc, line, col, text, opt)
local line_text = doc.lines[line]
for line = line, 1, -1 do
if opt.no_case then
line_text = line_text:lower()
end
local s = string_rfind(line_text, text, col, not opt.pattern)
if s then
return line, s
end
if line > 1 then
line_text = doc.lines[line - 1]
col = #line_text
end
end
if opt.wrap then
opt = {no_case = opt.no_case, pattern = opt.pattern}
return vim_search.find_backwards(doc, #doc.lines, math.huge, text, opt)
end
end
local modkey_map = {
["left ctrl"] = "ctrl",
["right ctrl"] = "ctrl",
["left shift"] = "shift",
["right shift"] = "shift",
["left alt"] = "alt",
["right alt"] = "altgr"
}
local modkeys = {"ctrl", "alt", "altgr", "shift"}
local function key_to_stroke(k)
local stroke = ""
for _, mk in ipairs(modkeys) do
if keymap.modkeys[mk] then
stroke = stroke .. mk .. "+"
end
end
return stroke .. k
end
function keymap.on_key_pressed(k)
if mini_mode then
if k == "escape" then
mini_mode = nil
return true
else
return false
end
end
local mk = modkey_map[k]
if mk then
keymap.modkeys[mk] = true
-- work-around for windows where `altgr` is treated as `ctrl+alt`
if mk == "altgr" then
keymap.modkeys["ctrl"] = false
end
return false
end
local stroke = key_to_stroke(k)
-- check for normal+0, and any other command with numbers
if not (n_repeat ~= 0 and string.find("0123456789", stroke, 1, true)) then
local commands
if mode == "insert" then
commands = keymap.map[stroke]
elseif mode == "normal" then
commands = keymap.map["normal" .. stroke_combo_string .. " " .. stroke]
elseif mode == "visual" then
commands = keymap.map["visual" .. stroke_combo_string .. " " .. stroke]
else
core.error('Cannot handle key in unknown mode "%s"', mode)
end
if commands then
for _, cmd in ipairs(commands) do
local performed = command.perform(cmd)
if performed then
if not stroke_combo_string:find("#", 1, true) then
for n = 1, n_repeat - 1 do
command.perform(cmd)
end
end
n_repeat = 0
stroke_combo_string = ""
stroke_combo_loc = stroke_combo_tree[mode]
break
end
end
return true
end
end
if mode == "normal" or mode == "visual" then
local num = tonumber(stroke, 10)
if num ~= nil then
n_repeat = n_repeat * 10 + num
stroke = "#"
if stroke_combo_string == "" then
return true
end
end
if stroke_combo_string:sub(-1) == "#" and stroke == "#" then
return true
end
stroke_combo_loc = stroke_combo_loc[stroke]
if stroke_combo_loc then
stroke_combo_string = stroke_combo_string .. " " .. stroke
else
stroke_combo_loc = stroke_combo_tree[mode]
stroke_combo_string = ""
n_repeat = 0
end
return true
end
return false
end
local function mouse_selection(doc, clicks, l1, c1, l2, c2)
local swap = l2 < l1 or l2 == l1 and c2 <= c1
if swap then
l1, c1, l2, c2 = l2, c2, l1, c1
end
if clicks == 2 then
l1, c1 = translate.start_of_word(doc, l1, c1)
l2, c2 = translate.end_of_word(doc, l2, c2)
if mode ~= "insert" then
c2 = c2 - 1
end
elseif clicks == 3 then
if l2 == #doc.lines and doc.lines[#doc.lines] ~= "\n" then
doc:insert(math.huge, math.huge, "\n")
end
l1, c1, l2, c2 = l1, 1, l2, math.huge
end
if mode ~= "insert" and (l1 ~= l2 or c1 ~= c2) then
visual_mode()
if clicks == 3 then
visual_submode = "line"
end
end
if swap then
return l2, c2, l1, c1
end
return l1, c1, l2, c2
end
local previous_visual_submode
local command_view_enter = CommandView.enter
function CommandView:enter(text, submit, suggest, cancel)
previous_visual_submode = visual_submode
insert_mode()
command_view_enter(self, text, submit, suggest, cancel)
end
local command_view_exit = CommandView.exit
function CommandView:exit(submitted, inexplicit)
if core.last_active_view.doc and core.last_active_view.doc:has_selection() then
visual_mode(previous_visual_submode)
else
normal_mode()
end
command_view_exit(self, submitted, inexplicit)
end
local doc_undo = Doc.undo
function Doc:undo()
doc_undo(self)
if mode ~= "insert" then
local line, col = self:get_selection()
self:set_selection(line, col)
end
end
local docview_new = DocView.new
function DocView:new(doc)
DocView.super.new(self)
docview_new(self, doc)
self.vim_marks = {}
end
function DocView:on_mouse_pressed(button, x, y, clicks)
local caught = DocView.super.on_mouse_pressed(self, button, x, y, clicks)
if caught then
return
end
if keymap.modkeys["shift"] then
if clicks == 1 then
if mode == "normal" then
visual_mode()
end
local line1, col1 = select(3, self.doc:get_selection())
local line2, col2 = self:resolve_screen_position(x, y)
self.doc:set_selection(line2, col2, line1, col1)
end
else
if mode == "visual" then
normal_mode()
end
local line, col = self:resolve_screen_position(x, y)
self.doc:set_selection(mouse_selection(self.doc, clicks, line, col, line, col))
self.mouse_selecting = {line, col, clicks = clicks}
end
if not is_lite_xl then
self.blink_timer = 0
end
end
function DocView:on_mouse_moved(x, y, ...)
DocView.super.on_mouse_moved(self, x, y, ...)
if self:scrollbar_overlaps_point(x, y) or self.dragging_scrollbar then
self.cursor = "arrow"
else
self.cursor = "ibeam"
end
if self.mouse_selecting then
local l1, c1 = self:resolve_screen_position(x, y)
local l2, c2 = table.unpack(self.mouse_selecting)
if mode == "normal" and (l1 ~= l2 or c1 ~= c2) then
visual_mode()
end
local clicks = self.mouse_selecting.clicks
self.doc:set_selection(mouse_selection(self.doc, clicks, l1, c1, l2, c2))
end
end
local docview_on_mouse_released = DocView.on_mouse_released
function DocView:on_mouse_released(button)
docview_on_mouse_released(self, button)
local line, col = self.doc:get_selection()
if col == #self.doc.lines[line] then
if self.doc:has_selection() then
local l1, c1, l2, c2 = self.doc:get_selection()
self.doc:set_selection(l1, c1 - 1, l2, c2)
else
self.doc:set_selection(line, col - 1)
end
end
end
local on_text_input = DocView.on_text_input
function DocView:on_text_input(text)
if mini_mode then
local fn = mini_mode
mini_mode = nil
fn(text)
elseif mode == "insert" then
on_text_input(self, text)
end
end
local draw_line_text = DocView.draw_line_text
function DocView:draw_line_text(idx, x, y)
local line1, col1, line2, col2 = self.doc:get_selection(true)
local tx, ty = x, y + self:get_line_text_y_offset()
local font = self:get_font()
if substitute.display and dv() ~= self and idx >= line1 and idx <= line2 and not self:is(CommandView) then
local g = substitute.mods:find("g", 1, true)
local remaining = self.doc.lines[idx]
::top::
local s, e = remaining:find(substitute.from, 1, true)
if s then
tx = renderer.draw_text(font, remaining:sub(1, s - 1), tx, ty, style.syntax.comment)
tx = renderer.draw_text(font, remaining:sub(s, e), tx, ty, style.syntax.keyword2)
tx = renderer.draw_text(font, substitute.to, tx, ty, style.syntax["function"])
remaining = remaining:sub(e + 1)
if g then
goto top
end
end
renderer.draw_text(font, remaining, tx, ty, style.syntax.comment)
else
draw_line_text(self, idx, x, y)
end
end
local function draw_box(x, y, w, h, pad, color)
local r = renderer.draw_rect
pad = pad * math.ceil(SCALE)
r(x, y, w, pad, color)
r(x, y + h - pad, w, pad, color)
r(x, y + pad, pad, h - pad * 2, color)
r(x + w - pad, y + pad, pad, h - pad * 2, color)
end
function DocView:draw_line_body(idx, x, y)
local line, col = self.doc:get_selection()
local line1, col1, line2, col2 = self.doc:get_selection(true)
-- draw selection
if core.active_view == self and idx >= line1 and idx <= line2 then
local text = self.doc.lines[idx]
local x1
local x2
if visual_submode == "block" then
if col1 > col2 then
col1, col2 = col2, col1
end
x1 = x + self:get_col_x_offset(idx, col1)
x2 = x + self:get_col_x_offset(idx, col2 + 1)
elseif visual_submode == "line" then
x1 = x
x2 = x + self:get_col_x_offset(idx, #text + 1)
else
if line1 ~= idx then
col1 = 1
end
if line2 ~= idx then
col2 = #text + 1
end
x1 = x + self:get_col_x_offset(idx, col1)
if mode == "visual" and line2 == idx then
x2 = x + self:get_col_x_offset(idx, col2 + 1)
else
x2 = x + self:get_col_x_offset(idx, col2)
end
end
local lh = self:get_line_height()
renderer.draw_rect(x1, y, x2 - x1, lh, style.selection)
end
-- draw line highlight
if
config.highlight_current_line and mode ~= "visual" and not self.doc:has_selection() and line == idx and
core.active_view == self
then
self:draw_line_highlight(x + self.scroll.x, y)
end
-- draw search results
if should_highlight and not self:is(CommandView) then
local lh = self:get_line_height()
local text = self.doc.lines[idx]
if search_text == search_text:lower() then
text = text:lower()
end
local last_col = 1
local start_col, end_col = text:find(search_text, last_col, true)
while start_col do
local x1 = x + self:get_col_x_offset(idx, start_col)
local x2 = x + self:get_col_x_offset(idx, end_col + 1)
draw_box(x1, y, x2 - x1, lh, 2, style.text)
last_col = end_col + 1
start_col, end_col = text:find(search_text, last_col, true)
end
end
-- draw line's text
self:draw_line_text(idx, x, y)
local blink_period
local timer
if is_lite_xl then
blink_period = config.blink_period
timer = (core.blink_timer - core.blink_start) % blink_period
else
blink_period = 0.8
timer = self.blink_timer
end
-- draw caret
if line == idx and core.active_view == self and timer < blink_period / 2 and system.window_has_focus() then
local lh = self:get_line_height()
local x1 = x + self:get_col_x_offset(line, col)
if mode == "normal" or mode == "visual" then
local w = self:get_font():get_width " "
if mode == "visual" then
local visual_caret_color = style.visual_caret or style.syntax.string
renderer.draw_rect(x1, y, w, lh, visual_caret_color)
else
renderer.draw_rect(x1, y, w, lh, style.caret)
end
local ch = self.doc:get_text(line, col, line, col + 1)
renderer.draw_text(self:get_font(), ch, x1, y + self:get_line_text_y_offset(), style.background)
else
renderer.draw_rect(x1, y, style.caret_width, lh, style.caret)
end
end
end
function DocView:draw_line_gutter(idx, x, y)
if not enable_linenum then
return
end
local color = style.line_number
local line1, _, line2, _ = self.doc:get_selection()
local ln = idx
if relative_line_mode and idx ~= line1 and self == core.active_view and mode ~= "insert" then
ln = math.abs(idx - line1)
end
if line1 > line2 then
line1, line2 = line2, line1
end
if idx >= line1 and idx <= line2 then
color = style.line_number2
end
local yoffset = self:get_line_text_y_offset()
x = x + style.padding.x
renderer.draw_text(self:get_font(), ln, x, y + yoffset, color)
end
local get_gutter_width = DocView.get_gutter_width
function DocView:get_gutter_width()
if enable_linenum then
return get_gutter_width(self)
end
return style.padding.x
end
if is_lite_xl then
function DocView:draw_overlay()
-- pass
end
end
local status_view_get_items = StatusView.get_items
function StatusView:get_items()
local left, right = status_view_get_items(self)
if n_repeat ~= 0 then
if next(left) then
table.insert(left, style.dim)
table.insert(left, self.separator)
end
table.insert(left, style.text)
table.insert(left, tostring(n_repeat))
end
if mode == "normal" then
return left, right
end
if next(left) then
table.insert(left, style.dim)
table.insert(left, self.separator)
end
table.insert(left, style.accent or style.text)
if visual_submode then
table.insert(left, string.format("-- %s %s --", mode, visual_submode))
else
table.insert(left, string.format("-- %s --", mode))
end
if mode == "visual" and doc() then
table.insert(left, style.text)
table.insert(left, self.separator)
local l1, c1, l2, c2 = doc():get_selection(true)
local lines = math.abs(l1 - l2) + 1
if visual_submode == "block" then
table.insert(left, string.format("%dx%d", math.abs(l1 - l2) + 1, math.abs(c1 - c2) + 1))
elseif visual_submode == "line" then
if lines == 1 then
table.insert(left, "1 line")
else
table.insert(left, string.format("%d lines", lines))
end
else
if lines == 1 then
if c1 == c2 then
table.insert(left, "1 char")
else
table.insert(left, string.format("%d chars", math.abs(c1 - c2) + 1))
end
else
local count = 0
for line = l1 + 1, l2 - 1 do
count = count + #doc().lines[line] - 1
end
count = count + #doc().lines[l1] - c1 + 1
count = count + c2 - 1
table.insert(left, string.format("%d chars, %d lines", count, lines))
end
end
end
return left, right
end
local set_active_view = core.set_active_view
function core.set_active_view(view)
local av = core.active_view
set_active_view(view)
if av:is(DocView) and not av:is(CommandView) then
av.vim_last_selection = table.pack(av.doc:get_selection())
end
if av ~= view and view:is(DocView) then
if view.vim_last_selection then
view.doc:set_selection(table.unpack(view.vim_last_selection))
return
end
local min, max = view:get_visible_line_range()
local line = view.doc:get_selection()
if line < min or line > max then
view.doc:set_selection(min + math.floor((max - min) / 2), 1)
end
end
end
local function char_type(char)
if char:find "%s" then
return "whitespace"
elseif char:find "%w" then
return "word"
else
return "other"
end
end
local vim_translate = {}
function vim_translate.goto_first_line(doc, line, col, dv)
dv.vim_marks["`"] = {line, col}
if n_repeat ~= 0 then
local n = n_repeat
n_repeat = 0
return n, 1
else
return 1, 1
end
end
function vim_translate.goto_line(doc, line, col, dv)
dv.vim_marks["`"] = {line, col}
if n_repeat ~= 0 then
local n = n_repeat
n_repeat = 0
return n, 1
else
return #doc.lines, 1
end
end
function vim_translate.previous_char(doc, line, col)
local line2, col2 = line, col
repeat
line2, col2 = doc:position_offset(line2, col2, -1)
until not common.is_utf8_cont(doc:get_char(line2, col2))