-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathedit.lua
More file actions
240 lines (203 loc) · 4.96 KB
/
edit.lua
File metadata and controls
240 lines (203 loc) · 4.96 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
-- Editing 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")
-- r : Replace single character under cursor.
local function replace(num, letter)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
if utf8.RuneCount(letter) ~= 1 then
bell.program_error("1 ~= utif8.len(letter) == " .. #letter)
return
end
local buf = micro.CurPane().Buf
local cursor = buf:GetActiveCursor()
local line = buf:Line(cursor.Y)
local length = utf8.RuneCount(line)
if cursor.X + num > length then
bell.ring("line end exceeded")
return
end
mode.show()
snapshot.update()
local start_loc = buffer.Loc(cursor.X, cursor.Y)
local end_loc = buffer.Loc(cursor.X + num, cursor.Y)
buf:Remove(start_loc, end_loc)
local chars = {}
if letter == "\n" then
table.insert(chars, letter)
else
for _ = 1, num do
table.insert(chars, letter)
end
end
mode.insert()
buf:Insert(start_loc, table.concat(chars))
mode.command()
if letter == "\n" then
cursor.Y = end_loc.Y
cursor.X = 0
else
cursor.X = end_loc.X - 1
end
move.update_virtual_cursor()
end
-- J : Join current line with next line.
local function join(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 last_line_index = utils.last_line_index(buf)
if cursor.Y >= last_line_index then
bell.vi_info("no lines to join below")
return
end
local n = num
if n > 1 then
n = n - 1
end
for _ = 1, n do
if cursor.Y >= last_line_index then
break
end
local line = buf:Line(cursor.Y)
local length = utf8.RuneCount(line)
cursor.X = length
local next_line = buf:Line(cursor.Y + 1)
local loc = buffer.Loc(cursor.X, cursor.Y)
local _, body = next_line:match("^(%s*)(.*)$")
if #body > 0 then
buf:Insert(loc, " " .. body)
end
cursor.Y = cursor.Y + 1
pane:DeleteLine()
cursor.Y = cursor.Y - 1
utils.next_tick(function()
cursor.Y = loc.Y
line = buf:Line(cursor.Y)
if length < 1 or #next_line < 1 then
local current_length = utf8.RuneCount(line)
cursor.X = math.max(current_length - 1, 0)
else
cursor.X = loc.X
end
move.update_virtual_cursor()
end)
end
end
-- internal use
--
local function indent_lines_internal(num, right)
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 last_line_index = utils.last_line_index(buf)
if cursor.Y + num - 1 > last_line_index then
bell.ring("there are not " .. num .. " lines below, only " .. last_line_index - cursor.Y + 1)
return
end
mode.insert()
local saved_x = cursor.X
local saved_y = cursor.Y
for i = 1, num do
if right then
micro.CurPane():IndentLine()
else
micro.CurPane():OutdentLine()
end
if num > 1 then
if i == 1 then
saved_x = cursor.X
saved_y = cursor.Y
end
cursor.X = 0
cursor.Y = cursor.Y + 1
end
end
if num > 1 then
cursor.X = saved_x
cursor.Y = saved_y
end
mode.command()
end
-- >> : Indent current line.
local function indent(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
indent_lines_internal(num, true)
end
-- << : Outdent current line.
local function outdent(num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
indent_lines_internal(num, false)
end
-- internal use
--
local function indent_region_internal(start_loc, end_loc, num, right)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
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 n = end_loc.Y - start_loc.Y + 1
indent_lines_internal(num * n, right)
end
-- > <mv> : Indent region from current cursor to destination of motion <mv>.
local function indent_region(start_loc, end_loc, num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
indent_region_internal(start_loc, end_loc, num, true)
end
-- < <mv> : Outdent region from current cursor to destination of motion <mv>.
local function outdent_region(start_loc, end_loc, num)
if num < 1 then
bell.program_error("1 > num == " .. num)
return
end
indent_region_internal(start_loc, end_loc, num, false)
end
-------------
-- Exports --
-------------
local M = {}
M.replace = replace
M.join = join
M.indent = indent
M.outdent = outdent
M.indent_region = indent_region
M.outdent_region = outdent_region
return M