-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperator.lua
More file actions
729 lines (596 loc) · 15.8 KB
/
Copy pathoperator.lua
File metadata and controls
729 lines (596 loc) · 15.8 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
-- Operator Commands
local micro = import("micro")
local buffer = import("micro/buffer")
local utf8 = import("unicode/utf8")
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 snapshot = require("vi/snapshot")
local move = require("vi/move")
local insert = require("vi/insert")
local kill_buffer = nil
local kill_lines = nil
--
local function clear_kill_buffer()
kill_buffer = {}
end
--
local function insert_killed_lines(lines)
table.insert(kill_buffer, lines)
kill_lines = true
end
--
local function insert_killed_chars(chars)
table.insert(kill_buffer, chars)
kill_lines = false
end
--
-- Copy (Yank)
--
-- yy Y : Copy current line.
local function copy_line(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
mode.show()
local buf = micro.CurPane().Buf
local cursor = buf:GetActiveCursor()
local last_line_index = utils.last_line_index(buf)
if cursor.Y + num - 1 > last_line_index then
bell.ring("cannot copy" .. num .. " lines, only " .. last_line_index - cursor.Y + 1 .. " below")
return
end
clear_kill_buffer()
for i = 1, num do
local line = buf:Line(cursor.Y + i - 1)
insert_killed_lines(line)
end
end
-- y<mv> : Copy region from current cursor to destination of motion <mv>.
local function copy_region(start_loc, end_loc)
mode.show()
if not utils.is_locs_ordered(start_loc, end_loc) then
-- start_loc, end_loc = end_loc, start_loc -- swap
start_loc, end_loc = utils.swap(start_loc, end_loc)
end
local buf = micro.CurPane().Buf
local substr = buf:Substr(start_loc, end_loc)
substr = utils.bytes_to_string(substr)
clear_kill_buffer()
insert_killed_chars(substr)
end
-- key: y<mv> : Copy line region from current cursor to destination of motion <mv>.
local function copy_line_region(start_y, end_y)
if end_y < start_y then
-- start_y, end_y = end_y, start_y -- swap
start_y, end_y = utils.swap(start_y, end_y)
end
local cursor = micro.CurPane().Buf:GetActiveCursor()
cursor.X = 0
cursor.Y = start_y
copy_line(end_y - start_y + 1)
end
-- yw : Copy word.
local function copy_word(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
local cursor = micro.CurPane().Buf:GetActiveCursor()
local start_loc = buffer.Loc(cursor.X, cursor.Y)
move.by_word(num)
local end_loc = buffer.Loc(cursor.X, cursor.Y)
cursor.X = start_loc.X
cursor.Y = start_loc.Y
copy_region(start_loc, end_loc)
end
-- yW : Copy loose word.
local function copy_loose_word(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
local cursor = micro.CurPane().Buf:GetActiveCursor()
local start_loc = buffer.Loc(cursor.X, cursor.Y)
move.by_loose_word(num)
local end_loc = buffer.Loc(cursor.X, cursor.Y)
cursor.X = start_loc.X
cursor.Y = start_loc.Y
copy_region(start_loc, end_loc)
end
-- y$ : Copy to end of current line.
local function copy_to_end()
mode.show()
local buf = micro.CurPane().Buf
local cursor = buf:GetActiveCursor()
local line = buf:Line(cursor.Y)
local length = utf8.RuneCount(line)
if length < 1 then
bell.ring("nothing to copy, line is empty")
return
end
clear_kill_buffer()
insert_killed_chars(line:sub(1 + cursor.X))
end
-- "<reg>yy : Copy current line into register <reg>.
local function copy_line_into_reg(reg, num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
bell.planned('"<reg>yy (operator.copy_line_into_reg)')
end
--
-- Paste (Put)
--
-- p : Paste after cursor.
local function paste(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
mode.show()
if not kill_buffer then
bell.vi_info("nothing to paste yet")
return
end
if not kill_lines then
snapshot.update()
end
local text
if kill_lines then
text = "\n" .. table.concat(kill_buffer, "\n")
else -- kill chars
text = table.concat(kill_buffer)
end
mode.insert()
local buf = micro.CurPane().Buf
local cursor = buf:GetActiveCursor()
local line = buf:Line(cursor.Y)
local saved_length = utf8.RuneCount(line)
local saved_x = cursor.X
local saved_y
for _ = 1, num do
saved_y = cursor.Y
if kill_lines then
line = buf:Line(cursor.Y)
cursor.X = utf8.RuneCount(line)
else -- kill chars
line = buf:Line(cursor.Y)
local length = utf8.RuneCount(line)
cursor.X = math.min(saved_x + 1, math.max(length, 0))
end
buf:Insert(buffer.Loc(cursor.X, cursor.Y), text)
if kill_lines then
cursor.Y = saved_y + 1
end
end
utils.next_tick(function()
if kill_lines then
cursor.Y = saved_y + 1
line = buf:Line(cursor.Y)
local spaces = line:match("^(%s*)")
cursor.X = utf8.RuneCount(spaces)
move.update_virtual_cursor()
else -- kill chars
if saved_length < 1 then
cursor.X = saved_x
else
cursor.X = saved_x + 1
end
end
move.update_virtual_cursor()
mode.command()
end)
end
-- P : Paste before cursor.
local function paste_before(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
mode.show()
if not kill_lines then
snapshot.update()
end
if not kill_buffer then
bell.vi_info("nothing to paste yet")
return
end
local text
if kill_lines then
text = table.concat(kill_buffer, "\n") .. "\n"
else -- kill chars
text = table.concat(kill_buffer)
end
mode.insert()
local buf = micro.CurPane().Buf
local cursor = buf:GetActiveCursor()
local saved_x = cursor.X
local saved_y
for _ = 1, num do
saved_y = cursor.Y
if kill_lines then
cursor.X = 0
end
buf:Insert(buffer.Loc(cursor.X, cursor.Y), text)
if kill_lines then
cursor.Y = saved_y
end
end
utils.next_tick(function()
if kill_lines then
cursor.Y = saved_y
local line = buf:Line(cursor.Y)
local spaces = line:match("^(%s*)")
cursor.X = utf8.RuneCount(spaces)
else -- kill chars
cursor.X = saved_x
end
move.update_virtual_cursor()
mode.command()
end)
end
-- "<reg>p : Paste from register <reg>.
local function paste_from_reg(reg, num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
bell.planned('"<reg>p (operator.paste_from_reg)')
end
--
-- Delete
--
-- x : Delete character under cursor.
local function delete(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
mode.show()
snapshot.update()
local pane = micro.CurPane()
local buf = pane.Buf
local cursor = buf:GetActiveCursor()
local line = buf:Line(cursor.Y)
local length = utf8.RuneCount(line)
if length < 1 then
bell.ring("nothing to delete, line is empty")
return
end
local n = math.min(num, length - cursor.X)
clear_kill_buffer()
insert_killed_chars(utils.utf8_sub(line, cursor.X + 1, cursor.X + n))
local saved_x = cursor.X
for _ = 1, n do
pane:Delete()
end
utils.next_tick(function()
line = buf:Line(cursor.Y)
length = utf8.RuneCount(line)
cursor.X = math.min(saved_x, math.max(length - 1, 0))
move.update_virtual_cursor()
end)
end
-- X : Delete character before cursor.
local function delete_before(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
mode.show()
snapshot.update()
local pane = micro.CurPane()
local buf = pane.Buf
local cursor = buf:GetActiveCursor()
local line = buf:Line(cursor.Y)
local length = utf8.RuneCount(line)
if length < 1 then
bell.ring("nothing to delete, line is empty")
return
end
local n = math.min(num, cursor.X)
clear_kill_buffer()
insert_killed_chars(utils.utf8_sub(line, cursor.X - n + 1, cursor.X))
local saved_x = cursor.X
cursor.X = cursor.X - n
for _ = 1, n do
pane:Delete()
end
utils.next_tick(function()
line = buf:Line(cursor.Y)
length = utf8.RuneCount(line)
cursor.X = math.min(math.max(saved_x - n, 0), math.max(length - 1, 0))
move.update_virtual_cursor()
end)
end
-- dd : Delete current line.
local function delete_line(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
mode.show()
local pane = micro.CurPane()
local buf = pane.Buf
local cursor = buf:GetActiveCursor()
local last_line_index = utils.last_line_index(buf)
if cursor.Y + num - 1 > last_line_index then
bell.ring("cannot delete " .. num .. " lines, only " .. last_line_index - cursor.Y + 1 .. " below")
return
end
clear_kill_buffer()
cursor.X = 0
for _ = 1, num do
local line = buf:Line(cursor.Y)
insert_killed_lines(line)
pane:DeleteLine()
last_line_index = utils.last_line_index(buf)
cursor.Y = math.min(cursor.Y, last_line_index)
end
utils.next_tick(function()
local line = buf:Line(cursor.Y)
local spaces = line:match("^(%s*)")
cursor.X = utf8.RuneCount(spaces)
move.update_virtual_cursor()
end)
end
-- d<mv> : Delete region from current cursor to destination of motion <mv>.
local function delete_region(start_loc, end_loc)
mode.show()
snapshot.update()
if not utils.is_locs_ordered(start_loc, end_loc) then
-- start_loc, end_loc = end_loc, start_loc -- swap
start_loc, end_loc = utils.swap(start_loc, end_loc)
end
local buf = micro.CurPane().Buf
local cursor = buf:GetActiveCursor()
local substr = buf:Substr(start_loc, end_loc)
substr = utils.bytes_to_string(substr)
clear_kill_buffer()
insert_killed_chars(substr)
buf:Remove(start_loc, end_loc)
utils.next_tick(function()
local line = buf:Line(cursor.Y)
local length = utf8.RuneCount(line)
cursor.X = math.min(cursor.X, math.max(length - 1, 0))
move.update_virtual_cursor()
end)
end
-- d<mv> : Delete line region from current cursor to destination of motion <mv>.
local function delete_line_region(start_y, end_y)
if end_y < start_y then
-- start_y, end_y = end_y, start_y -- swap
start_y, end_y = utils.swap(start_y, end_y)
end
local cursor = micro.CurPane().Buf:GetActiveCursor()
cursor.X = 0
cursor.Y = start_y
delete_line(end_y - start_y + 1)
end
-- dw : Delete word.
local function delete_word(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
snapshot.update()
local cursor = micro.CurPane().Buf:GetActiveCursor()
local loc_start = buffer.Loc(cursor.X, cursor.Y)
move.by_word(num)
local loc_end = buffer.Loc(cursor.X, cursor.Y)
cursor.X = loc_start.X
cursor.Y = loc_start.Y
delete_region(loc_start, loc_end)
end
-- dW : Delete loose word.
local function delete_loose_word(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
snapshot.update()
local cursor = micro.CurPane().Buf:GetActiveCursor()
local loc_start = buffer.Loc(cursor.X, cursor.Y)
move.by_loose_word(num)
local loc_end = buffer.Loc(cursor.X, cursor.Y)
cursor.X = loc_start.X
cursor.Y = loc_start.Y
delete_region(loc_start, loc_end)
end
-- d$ D - Delete to end of current line.
local function delete_to_end()
mode.show()
snapshot.update()
local buf = micro.CurPane().Buf
local cursor = buf:GetActiveCursor()
local line = buf:Line(cursor.Y)
local length = utf8.RuneCount(line)
if length < 1 then
bell.ring("nothing to delete, line is empty")
return
end
clear_kill_buffer()
insert_killed_chars(line:sub(1 + cursor.X))
local start_loc = buffer.Loc(cursor.X, cursor.Y)
local end_loc = buffer.Loc(length, cursor.Y)
buf:Remove(start_loc, end_loc)
line = buf:Line(cursor.Y)
length = utf8.RuneCount(line)
cursor.X = math.min(cursor.X, math.max(length - 1, 0))
move.update_virtual_cursor()
end
--
-- Change / Substitute
--
-- cc : Change current line.
local function change_line(num, replay)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
snapshot.update()
delete_line(num)
insert.open_here(1, replay)
end
-- c<mv> : Change region from current cursor to destination of motion <mv>.
local function change_region(start_loc, end_loc, replay)
if not utils.is_locs_ordered(start_loc, end_loc) then
-- start_loc, end_loc = end_loc, start_loc -- swap
start_loc, end_loc = utils.swap(start_loc, end_loc)
end
snapshot.update()
local buf = micro.CurPane().Buf
local line = buf:Line(end_loc.Y)
local length = utf8.RuneCount(line)
local end_of_line = end_loc.X >= length
delete_region(start_loc, end_loc)
utils.next_tick(function()
local cursor = buf:GetActiveCursor()
cursor.X = start_loc.X
cursor.Y = start_loc.Y
if end_of_line then
insert.after(1, replay)
else
insert.before(1, replay)
end
end, 2)
end
-- c<mv> : Change line region from current cursor to destination of motion <mv>.
local function change_line_region(start_y, end_y, replay)
delete_line_region(start_y, end_y)
insert.open_here(1, replay)
end
-- cw : Change word.
local function change_word(num, replay)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
snapshot.update()
local cursor = micro.CurPane().Buf:GetActiveCursor()
local loc_start = buffer.Loc(cursor.X, cursor.Y)
move.by_word_for_change(num)
local loc_end = buffer.Loc(cursor.X, cursor.Y)
cursor.X = loc_start.X
cursor.Y = loc_start.Y
change_region(loc_start, loc_end, replay)
end
-- cW : Change loose word.
local function change_loose_word(num, replay)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
snapshot.update()
local cursor = micro.CurPane().Buf:GetActiveCursor()
local loc_start = buffer.Loc(cursor.X, cursor.Y)
move.by_loose_word_for_change(num)
local loc_end = buffer.Loc(cursor.X, cursor.Y)
cursor.X = loc_start.X
cursor.Y = loc_start.Y
change_region(loc_start, loc_end, replay)
end
-- C : Change to end of current line.
local function change_to_end(replay)
snapshot.update()
delete_to_end()
insert.after(1, replay)
end
-- s : Substitute one character under cursor.
local function subst(num, replay)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
snapshot.update()
insert.replace_mode()
local pane = micro.CurPane()
local buf = pane.Buf
local cursor = buf:GetActiveCursor()
local line = buf:Line(cursor.Y)
local length = utf8.RuneCount(line)
local n = math.min(num, length - cursor.X)
clear_kill_buffer()
insert_killed_chars(utils.utf8_sub(line, 1 + cursor.X, cursor.X + n))
local saved_x = cursor.X
local insert_after = cursor.X + num >= length
for _ = 1, n do
pane:Delete()
end
if replay then
local loc = buffer.Loc(cursor.X, cursor.Y)
insert.extend(loc, 1, replay)
else
mode.show()
line = buf:Line(cursor.Y)
length = utf8.RuneCount(line)
cursor.X = math.min(cursor.X + 1, length - 1)
utils.next_tick(function()
line = buf:Line(cursor.Y)
length = utf8.RuneCount(line)
if insert_after then
cursor.X = math.min(saved_x, math.max(length, 0))
else
cursor.X = math.min(saved_x, math.max(length - 1, 0))
end
move.update_virtual_cursor()
insert.before_replace(1, replay)
end)
end
end
-- S : Substtute current line (equals cc).
local function subst_line(num, replay)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
snapshot.update()
change_line(num, replay)
end
-------------
-- Exports --
-------------
local M = {}
-- internal use
M.clear_kill_buffer = clear_kill_buffer
--M.insert_killed_lines = insert_killed_lines
M.insert_killed_chars = insert_killed_chars
-- Copy (Yank)
M.copy_word = copy_word
M.copy_loose_word = copy_loose_word
M.copy_line = copy_line
M.copy_line_into_reg = copy_line_into_reg
M.copy_region = copy_region
M.copy_line_region = copy_line_region
M.copy_to_end = copy_to_end
-- Paste (Put)
M.paste = paste
M.paste_before = paste_before
M.paste_from_rag = paste_from_reg
-- Delete
M.delete = delete
M.delete_before = delete_before
M.delete_word = delete_word
M.delete_loose_word = delete_loose_word
M.delete_line = delete_line
M.delete_region = delete_region
M.delete_line_region = delete_line_region
M.delete_to_end = delete_to_end
-- Change / Substitute
M.change_word = change_word
M.change_loose_word = change_loose_word
M.change_line = change_line
M.change_region = change_region
M.change_line_region = change_line_region
M.change_to_end = change_to_end
M.subst = subst
M.subst_line = subst_line
return M