-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand.lua
More file actions
729 lines (655 loc) · 16.1 KB
/
command.lua
File metadata and controls
729 lines (655 loc) · 16.1 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
local micro = import("micro")
local buffer = import("micro/buffer")
local config = import("micro/config")
local plug_path = config.ConfigDir .. "/plug/?.lua"
if not package.path:find(plug_path, 1, true) then
package.path = package.path .. ";" .. plug_path
end
local utils = require("vi/utils")
local bell = require("vi/bell")
local mode = require("vi/mode")
local prompt = require("vi/prompt")
local move = require("vi/move")
local mark = require("vi/mark")
local view = require("vi/view")
local search = require("vi/search")
local find = require("vi/find")
local insert = require("vi/insert")
local operator = require("vi/operator")
local edit = require("vi/edit")
local misc = require("vi/misc")
local command_cache = nil
local undo_mode = true -- true: undo, false: redo
local function cache_command(no_num, num, op, no_subnum, subnum, mv, letter)
command_cache = {}
command_cache.no_num = no_num
command_cache.num = num
command_cache.op = op
command_cache.no_subnum = no_subnum
command_cache.subnum = subnum
command_cache.mv = mv
command_cache.letter = letter
end
local function get_command_cache()
return command_cache.no_num,
command_cache.num,
command_cache.op,
command_cache.no_subnum,
command_cache.subnum,
command_cache.mv,
command_cache.letter,
true -- replay
end
-- Note: Declaration is used in repeat_command(). Definition is far below.
local run
local function repeat_command(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
mode.show()
if not command_cache then
bell.vi_info("nothing to repeat yet")
return
end
for _ = 1, num do
run(get_command_cache())
end
end
local function undo(num, replay)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
if utils.xor(undo_mode, replay) then
for _ = 1, num do
micro.CurPane():Undo()
end
else -- redo
for _ = 1, num do
micro.CurPane():Redo()
end
end
if not replay then
undo_mode = not undo_mode
end
end
local function run_move(no_num, num, mv)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
-- Move by Character / Move by Line
if mv == "h" then
move.left(num)
return true
elseif mv == "j" then
move.down(num)
return true
elseif mv == "k" then
move.up(num)
return true
elseif mv == "l" then
move.right(num)
return true
end
-- Move in Line
if mv == "0" then
move.to_start()
return true
elseif mv == "$" then
move.to_end()
return true
elseif mv == "^" then
move.to_non_blank()
return true
elseif mv == "|" then
move.to_column(num)
return true
end
-- Move by Word / Move by Loose Word
if mv == "w" then
move.by_word(num)
return true
elseif mv == "b" then
move.backward_by_word(num)
return true
elseif mv == "e" then
move.to_end_of_word(num)
return true
elseif mv == "W" then
move.by_loose_word(num)
return true
elseif mv == "B" then
move.backward_by_loose_word(num)
return true
elseif mv == "E" then
move.to_end_of_loose_word(num)
return true
end
-- Move by Line
if mv == "\n" or mv == "+" then
move.to_non_blank_of_next_line(num)
return true
elseif mv == "-" then
move.to_non_blank_of_prev_line(num)
return true
elseif mv == "G" then
if no_num then
move.to_last_line()
else
move.to_line(num)
end
return true
end
-- Move by Block
if mv == ")" then
move.by_sentence(num)
return true
elseif mv == "(" then
move.backward_by_sentence(num)
return true
elseif mv == "}" then
move.by_paragraph(num)
return true
elseif mv == "{" then
move.backward_by_paragraph(num)
return true
elseif mv == "]]" then
move.by_section(num)
return true
elseif mv == "[[" then
move.backward_by_section(num)
return true
end
-- Move in View
if no_num and mv == "H" then
move.to_top_of_view()
return true
elseif mv == "M" then
move.to_middle_of_view()
return true
elseif no_num and mv == "L" then
move.to_bottom_of_view()
return true
elseif mv == "H" then
move.to_below_top_of_view(num)
return true
elseif mv == "L" then
move.to_above_bottom_of_view(num)
return true
end
return false
end
local function run_mark(op, mv, letter)
if letter ~= nil then
if #letter ~= 1 then
bell.program_error("1 ~= #letter == " .. #letter)
return
end
end
-- Set Mark / Move to Mark
if op == "m" and letter then
mark.set(letter)
return true
elseif mv == "`" and letter then
if letter == "`" then
mark.back()
else
mark.move_to(letter)
end
return true
elseif mv == "'" and letter then
if letter == "'" then
mark.back_to_line()
else
mark.move_to_line(letter)
end
return true
end
return false
end
local function run_view(op, mv)
-- Reposition
if op == "z" and mv == "\n" then
view.to_top()
return true
elseif op == "z." then
view.to_middle()
return true
elseif op == "z" and mv == "-" then
view.to_bottom()
return true
end
return false
end
local function run_search(num, mv)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
if mv == "/\n" then -- not works
search.repeat_forward()
return true
elseif mv == "?\n" then -- not works
search.repeat_backward()
return true
elseif mv == "/" then
search.forward()
return true
elseif mv == "?" then
search.backward()
return true
elseif mv == "n" then
search.next_match(num)
return true
elseif mv == "N" then
search.prev_match(num)
return true
end
return false
end
local function run_find(num, mv, letter)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
if letter ~= nil then
if #letter ~= 1 then
bell.program_error("1 ~= #letter == " .. letter)
return
end
end
if mv == "f" and letter then
find.forward(num, letter)
return true
elseif mv == "F" and letter then
find.backward(num, letter)
return true
elseif mv == "t" and letter then
find.before_forward(num, letter)
return true
elseif mv == "T" and letter then
find.before_backward(num, letter)
return true
elseif mv == ";" then
find.next_match(num)
return true
elseif mv == "," then
find.prev_match(num)
return true
end
return false
end
local function run_insert(num, op, replay)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
-- Enter Insert Mode
if op == "i" then
insert.before(num, replay)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
elseif op == "a" then
insert.after(num, replay)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
elseif op == "I" then
insert.before_non_blank(num, replay)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
elseif op == "A" then
insert.after_end(num, replay)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
elseif op == "R" then
insert.overwrite(num, replay)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
end
-- Open Line
if op == "o" then
insert.open_below(num, replay)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
elseif op == "O" then
insert.open_above(num, replay)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
end
return false
end
local function run_operator(num, op, replay)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
-- Copy (Yank)
if op == "yy" or op == "Y" then
operator.copy_line(num)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
elseif op == "yy" and false then -- TODO reg
operator.copy_line_into_reg(num)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
end
-- Paste (Put)
if op == "p" then
operator.paste(num)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
elseif op == "P" then
operator.paste_before(num)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
end
-- Delete
if op == "x" then
operator.delete(num)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
elseif op == "X" then
operator.delete_before(num)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
elseif op == "dd" then
operator.delete_line(num)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
elseif op == "D" then
operator.delete_to_end()
cache_command(false, 1, op, true, 1, "", nil)
undo_mode = true
return true
end
-- Change / Substitute
if op == "cc" then
operator.change_line(num, replay)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
elseif op == "C" then
operator.change_to_end(replay)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
elseif op == "s" then
operator.subst(num, replay)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
elseif op == "S" then
operator.subst_line(num, replay)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
end
return false
end
local function run_edit(num, op, letter)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
if letter ~= nil then
if #letter ~= 1 then
bell.program_error("1 ~= #letter == " .. #letter)
return
end
end
if op == "r" and letter then
edit.replace(num, letter)
cache_command(false, num, op, true, 1, "", letter)
undo_mode = true
return true
elseif op == "J" then
edit.join(num)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
elseif op == ">>" then
edit.indent(num)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
elseif op == "<<" then
edit.outdent(num)
cache_command(false, num, op, true, 1, "", nil)
undo_mode = true
return true
end
return false
end
local function run_misc(num, op, replay)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
if op == "." then
repeat_command(num)
return true
elseif op == "u" then
cache_command(false, num, op, true, 1, "", nil)
undo(num, replay)
return true
elseif op == "U" then
misc.restore()
return true
elseif op == "ZZ" then
misc.save_and_quit()
return true
end
return false
end
local function get_region(num, no_subnum, subnum, mv, letter, save)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
if letter ~= nil then
if #letter ~= 1 then
bell.program_error("1 ~= #letter == " .. #letter)
return
end
end
local cursor = micro.CurPane().Buf:GetActiveCursor()
local saved_x, saved_y
if save then
saved_x, saved_y = cursor.X, cursor.Y
end
local start_loc = buffer.Loc(cursor.X, cursor.Y)
for _ = 1, num do
if
not run_move(no_subnum, subnum, mv)
and not run_mark("", mv, letter)
and not run_search(subnum, mv)
and not run_find(subnum, mv, letter)
then
bell.program_error("invalid mv == " .. mv)
return nil, nil
end
end
if mv:match("[ft;,]") then
if cursor.X > start_loc.X then
cursor.X = cursor.X + 1
end
end
local end_loc = buffer.Loc(cursor.X, cursor.Y)
if save then
cursor.X, cursor.Y = saved_x, saved_y
end
return start_loc, end_loc
end
local function run_compound_operator(num, op, no_subnum, subnum, mv, letter, replay)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
if letter ~= nil then
if #letter ~= 1 then
bell.program_error("1 ~= #letter == " .. #letter)
return
end
end
local matched = false
if op == "y" and mv == "$" then
operator.copy_to_end()
matched = true
elseif op == "d" and mv == "$" then
operator.delete_to_end()
matched = true
elseif op == "c" and mv == "$" then
operator.change_to_end(replay)
matched = true
elseif op == "y" and mv == "w" then
operator.copy_word(num)
matched = true
elseif op == "d" and mv == "w" then
operator.delete_word(num)
matched = true
elseif op == "c" and mv == "w" then
operator.change_word(num, replay)
matched = true
elseif op == "y" and mv == "W" then
operator.copy_loose_word(num)
matched = true
elseif op == "d" and mv == "W" then
operator.delete_loose_word(num)
matched = true
elseif op == "c" and mv == "W" then
operator.change_loose_word(num, replay)
matched = true
elseif op == "y" and (mv:match("[hl0wbnN;,]+") or ((mv == "`" or mv:match("[fFtT]")) and letter)) then
local start_loc, end_loc = get_region(num, no_subnum, subnum, mv, letter)
operator.copy_region(start_loc, end_loc)
matched = true
elseif op == "d" and (mv:match("[hl0wbnN;,]+") or ((mv == "`" or mv:match("[fFtT]")) and letter)) then
local start_loc, end_loc = get_region(num, no_subnum, subnum, mv, letter)
operator.delete_region(start_loc, end_loc)
matched = true
elseif op == "c" and (mv:match("[hl0wbnN;,]+") or ((mv == "`" or mv:match("[fFtT]")) and letter)) then
local start_loc, end_loc = get_region(num, no_subnum, subnum, mv, letter)
operator.change_region(start_loc, end_loc, replay)
matched = true
elseif op == "y" and (mv:match("[jk\nG]+") or mv == "'" and letter) then
local start_loc, end_loc = get_region(num, no_subnum, subnum, mv, letter)
operator.copy_line_region(start_loc.Y, end_loc.Y)
matched = true
elseif op == "d" and (mv:match("[jk\nG]+") or mv == "'" and letter) then
local start_loc, end_loc = get_region(num, no_subnum, subnum, mv, letter)
operator.delete_line_region(start_loc.Y, end_loc.Y)
matched = true
elseif op == "c" and (mv:match("[jk\nG]+") or mv == "'" and letter) then
local start_loc, end_loc = get_region(num, no_subnum, subnum, mv, letter)
operator.change_line_region(start_loc.Y, end_loc.Y, replay)
matched = true
end
if matched then
cache_command(false, num, op, no_subnum, subnum, mv, letter)
undo_mode = true
return true
end
return false
end
local function run_compound_edit(num, op, no_subnum, subnum, mv, letter)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
if letter ~= nil then
if #letter ~= 1 then
bell.program_error("1 ~= #letter == " .. #letter)
return
end
end
local matched = false
if op == ">" and (mv:match("[hl0wbnN]+") or mv == "`" and letter) then
local start_loc, end_loc = get_region(1, no_subnum, subnum, mv, letter, true)
edit.indent_region(start_loc, end_loc, 1)
matched = true
elseif op == "<" and (mv:match("[hl0wbnN]+") or mv == "`" and letter) then
local start_loc, end_loc = get_region(1, no_subnum, subnum, mv, letter, true)
edit.outdent_region(start_loc, end_loc, 1)
matched = true
elseif op == ">" and (mv:match("[jk\nG]+") or mv == "'" and letter) then
local start_loc, end_loc = get_region(1, no_subnum, subnum, mv, letter, true)
edit.indent_region(start_loc, end_loc, num)
matched = true
elseif op == "<" and (mv:match("[jk\nG]+") or mv == "'" and letter) then
local start_loc, end_loc = get_region(1, no_subnum, subnum, mv, letter, true)
edit.outdent_region(start_loc, end_loc, num)
matched = true
end
if matched then
cache_command(false, num, op, no_subnum, subnum, mv, letter)
undo_mode = true
return true
end
return false
end
-- Note: Declared as local far above.
function run(no_num, num, op, no_subnum, subnum, mv, letter, replay)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
if letter ~= nil then
if #letter ~= 1 then
bell.program_error("1 ~= #letter == " .. #letter)
return
end
end
if op == ":" then
mode.prompt()
prompt.show()
return true
elseif run_compound_edit(num, op, no_subnum, subnum, mv, letter) then
return true
elseif run_compound_operator(num, op, no_subnum, subnum, mv, letter, replay) then
return true
elseif run_view(op, mv) then -- view run_must preceeds run_move
return true
elseif run_move(no_num, num, mv) then
return true
elseif run_mark(op, mv, letter) then
return true
elseif run_search(num, mv) then
return true
elseif run_find(num, mv, letter) then
return true
elseif run_insert(num, op, replay) then
return true
elseif run_operator(num, op, replay) then
return true
elseif run_edit(num, op, letter) then
return true
elseif run_misc(num, op, replay) then
return true
elseif mv == "g" then
move.by_word_for_change(num) -- XXX debug
return true
end
return false
end
-------------
-- Exports --
-------------
local M = {}
M.run = run
return M