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
|
-- table_cmp - return true if table1 is the same as table2
table_cmp = function(table1, table2)
if type(table1) ~= 'table' or type(table2) ~= 'table' then
return false
end
if #table1 ~= #table2 then
return false
end
for i, item in pairs(table1) do
if item ~= table2[i] then
return false
end
end
return true
end
-- strcasecmp - compare strings case-insensitively
strcasecmp = function(str1, str2)
if type(str1) ~= 'string' or type(str2) ~= 'string' then
return false
end
str1 = string.lower(str1)
str2 = string.lower(str2)
return str1 == str2
end
-- Setline - Set lines starting at [startline] to [lines]
-- - [startline] can be '.' to set the current line
-- - [startline] can be '$' to set the last line in the buffer
-- - [lines] can be left nil in which case, set lines to empty
setline = function(win, startline, lines)
if startline == '.' then
startline = win.cursor.line
elseif startline == '$' then
startline = #win.file.lines
end
if type(lines) == 'string' then
lines = {lines}
end
for i, line in pairs(lines or {}) do
i = i + ((startline - 1) or 0)
win.file.lines[i] = line
end
end
-- Delete - Remove [number] lines starting at [startline]
-- - [number] can be left nil to remove single line
-- - [startline] can be '.' to delete the current line
-- - [startline] can be '$' to delete the last line in the buffer
-- - [startline] can be '%' to empty the buffer
delete = function(win, startline, number)
if startline == '.' then
startline = win.cursor.line
elseif startline == '$' then
startline = #win.file.lines
elseif startline == '%' then
startline = 1
number = #win.file.lines
end
-- Get position in file of startline
win.cursor:to(startline, 0)
startpos = win.cursor.pos
-- Get position in file of endline, or delete single line
number = number or 1
win.cursor:to(startline + number, 0)
endpos = win.cursor.pos
-- Delete lines
len = endpos - startpos
win.file:delete(startpos, len)
end
-- Getline - Return a table of [number] lines starting from [startline]
-- - [startline] can be '.' to return the current line
-- - [startline] can be '$' to return the last line in the buffer
-- - [startline] can be '%' to return the whole buffer
-- - [number] can be left nil, in which case return a string
-- containing line [startline]
getline = function(win, startline, number)
if startline == '.' then
startline = win.cursor.line
elseif startline == '$' or startline > #win.file.lines then
startline = #win.file.lines
elseif startline == '%' then
startline = 1
number = #win.file.lines
elseif startline <= 0 then
startline = 1
end
if number == nil then
return win.file.lines[startline]
else
lines = {}
for i = startline, startline + number - 1, 1 do
table.insert(lines, win.file.lines[i])
end
return lines
end
end
-- Append - Insert lines after given line number
-- - [line] can be either string or table
-- - [startline] can be '.' to append after the current line
-- - [startline] can be '$' to append after the last line in the buffer
append = function(win, startline, lines)
if startline == '.' then
win.cursor:to(win.cursor.line + 1, 0)
elseif startline == '$' or startline > #win.file.lines then
win.cursor:to(#win.file.lines + 1, 0)
else
win.cursor:to(startline + 1, 0)
end
pos = win.cursor.pos
if type(lines) == 'table' then
lines = table.concat(lines, "\n") .. "\n"
elseif type(lines) == 'string' then
lines = lines .. '\n'
end
win.file:insert(pos, lines)
end
|