-
Notifications
You must be signed in to change notification settings - Fork 2
/
editor.lua
329 lines (286 loc) · 6.59 KB
/
editor.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
-- convention: c stands for cursor, s stands for string, f for filename
editor = {}
function editor:insert(c, s)
if c.column < lines[c.line]:len() then
lines[c.line] = lines[c.line]:sub(1, c.column-1)
.. s
.. lines[c.line]:sub(c.column)
c.column = c.column + s:len()
else
lines[c.line] = lines[c.line] .. s
c.column = c.column + s:len()
end
end
function editor:append(c, s)
lines[c.line] = lines[c.line] .. s
end
function editor:hello()
print("Hello world")
end
function editor:newline(c)
if c.column < lines[c.line]:len() then
table.insert(lines, c.line+1, "")
lines[c.line+1] = lines[c.line]:sub(c.column)
lines[c.line] = lines[c.line]:sub(1, c.column-1)
else
table.insert(lines, c.line+1, "")
end
c.line = c.line + 1
c.column = 1
end
function editor:delete_char(c)
-- this whole thing is a kludge
if c.column == 1 and #lines > 1 and c.line > 1 then
c.line = c.line - 1
local tmp = lines[c.line] .. lines[c.line + 1]
table.remove(lines, c.line + 1)
c.column = lines[c.line]:len()+1
lines[c.line] = tmp
return
end
if c.column > lines[c.line]:len() then
c.column = lines[c.line]:len()
end
if c.column < lines[c.line]:len() and c.column > 1 then
lines[c.line] = lines[c.line]:sub(1, c.column-2)
.. lines[c.line]:sub(c.column)
c.column = c.column - 1
elseif c.column == lines[c.line]:len() then
lines[c.line] = lines[c.line]:sub(1, lines[c.line]:len()-1)
c.column = lines[c.line]:len()+1
end
end
function editor:move_right(c)
if c.column <= lines[c.line]:len() and c.column >= 1 then
c.column = c.column + 1
end
end
function editor:move_left(c)
if c.column > 1 then
c.column = c.column - 1
end
end
function editor:move_down(c)
if c.line < #lines then
c.line = c.line + 1
if lines[c.line]:len() < c.column then
c.column = lines[c.line]:len()+1
end
if c.line > m.offset+m.maxlines then
m.offset = m.offset + 1
end
end
end
function editor:move_up(c)
if c.line > 1 then
c.line = c.line - 1
if lines[c.line]:len() < c.column then
c.column = lines[c.line]:len()+1
end
if c.line < m.offset then
m.offset = m.offset - 1
end
end
end
function editor:file_exists(f)
local file = io.open(f)
if file then
file:close()
if command_mode == true then
command_input = "File " .. f .. " exists."
end
return true
else
if command_mode == true then
command_input = "File " .. f .. " does not exist."
end
return false
end
end
function editor:open_file(f)
if f == "" or f == nil then
f = current_file
else
current_file = f
end
if f:sub(1, 1) == "~" then
f = editor:get_homedir() .. f:sub(2)
-- Have we been given just a filename?
elseif not (f:sub(1, 1) == "/") and
not (f:sub(2, 2) == ":" and (f:sub(3, 3) == "\\" or
f:sub(3, 3) == "/")) then
local cd = editor:current_dir()
if cd:find("/") then
f = cd .. "/" .. f
elseif cd:find("\\") then
f = cd .. "\\" .. f
end
end
if not editor:file_exists(f) then
if command_mode == true then
command_input = "File does not exist"
end
print("File does not exist")
return nil
end
local b = {}
for l in io.lines(f) do
table.insert(b, l)
end
lines = b
m.cursor.line = 1
m.cursor.column = 1
m.offset = 1
if command_mode == true then
command_input = "Opened " .. f
end
end
function editor:save_file(f)
if f == "" or f == nil then
f = current_file
else
current_file = f
end
if f:sub(1, 1) == "~" then
f = editor:get_homedir() .. f:sub(2)
-- Have we been given just a filename?
elseif not (f:sub(1, 1) == "/") and
not (f:sub(2, 2) == ":" and (f:sub(3, 3) == "\\" or
f:sub(3, 3) == "/")) then
local cd = editor:current_dir()
if cd:find("/") then
f = cd .. "/" .. f
elseif cd:find("\\") then
f = cd .. "\\" .. f
end
end
if not editor:file_exists(file) then
if command_mode == true then
command_input = "File does not exist"
end
print("File does not exist")
return nil
end
local file = io.open(f, "w")
for each, l in pairs(lines) do
file:write(l .. "\n")
end
file:close()
if command_mode == true then
command_input = "Wrote to " .. f
end
end
function editor:new_cursor(line, column)
return {line = line, column = column}
end
function editor:switch_cursor(c)
m.cursor = c
end
function editor:switch_mode(mode)
m = mode
if m.onload then
m:onload()
else
command_line = "Unable to load mode! onload() missing!"
end
end
function editor:close()
love.event.quit()
end
function editor:toggle_lines()
if m.show_line_num then
m.show_line_num = false
end
if not m.show_line_num then
m.show_line_num = true
end
end
function editor:background_colour(r, g, b)
m.col.back = {r=r, g=g, b=b}
end
-- editor:background_color = editor:background_colour
function editor:text_colour(r, g, b)
m.col.text = {r=r, g=g, b=b}
end
function editor:linenum_colour(r, g, b)
m.col.num = {r=r, g=g, b=b}
end
function editor:status_colour(r, g, b)
m.col.stat = {r=r, g=g, b=b}
end
function editor:command_colour(r, g, b)
m.col.comd = {r=r, g=g, b=b}
end
function editor:get_os()
return love.system.getOS()
end
function editor:get_homedir()
local t = editor:get_os()
if t == "Linux" or t == "OS X" then
return os.getenv("HOME")
elseif t == "Windows" then
return os.getenv("USERPROFILE")
end
end
function editor:change_dir(s)
if s:sub(1, 1) == "~" then
-- print(current_dir)
current_dir = editor:get_homedir() .. s:sub(2)
-- print(current_dir)
else
current_dir = s
end
end
function editor:current_dir()
if command_mode == true then
command_input = "Current dir: " .. current_dir
end
return current_dir
end
function editor:load_init_file()
local f = editor:get_homedir() .. "/.leinit"
if not f then
f = editor:get_homedir() .. "\\leinit~"
end
commands = ""
for l in io.lines(f) do
commands = commands .. l .. "\n"
end
editor:execute(commands)
end
function editor:execute(s)
if loadstring(s) == nil then
return false
else
loadstring(s)()
return true
end
end
function editor:interpret(s)
m:command_enter(s)
end
function editor:load_font(f, size)
if not editor:file_exists(f) then
print("Unable to load_font, file does not exist.")
if command_mode == true then
command_input = "Unable to load_font, file does not exist."
end
return nil
end
local font = love.graphics.newFont(f, size)
font:setFilter("nearest", "nearest", 2)
return font
end
function editor:set_font(font)
if font == nil then
print("Unable to set_font, font is nil")
if command_mode == true then
command_input = "Unable to set_font, font is nil."
end
return nil
end
-- Determine how many lines we can display per screen
local h = font:getHeight()
-- print(h)
love.graphics.setFont(font)
end