2019-12-28 12:16:32 +01:00
|
|
|
require "core.strict"
|
2020-05-17 14:31:04 +02:00
|
|
|
local common = require "core.common"
|
2019-12-28 12:16:32 +01:00
|
|
|
local config = require "core.config"
|
|
|
|
local style = require "core.style"
|
|
|
|
local command
|
|
|
|
local keymap
|
|
|
|
local RootView
|
|
|
|
local StatusView
|
|
|
|
local CommandView
|
|
|
|
local Doc
|
|
|
|
|
|
|
|
local core = {}
|
|
|
|
|
|
|
|
|
|
|
|
local function project_scan_thread()
|
|
|
|
local function diff_files(a, b)
|
|
|
|
if #a ~= #b then return true end
|
|
|
|
for i, v in ipairs(a) do
|
2020-05-06 18:03:10 +02:00
|
|
|
if b[i].filename ~= v.filename
|
|
|
|
or b[i].modified ~= v.modified then
|
2019-12-28 12:16:32 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-06 18:03:10 +02:00
|
|
|
local function compare_file(a, b)
|
|
|
|
return a.filename < b.filename
|
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
local function get_files(path, t)
|
|
|
|
coroutine.yield()
|
|
|
|
t = t or {}
|
|
|
|
local size_limit = config.file_size_limit * 10e5
|
2020-04-22 00:41:44 +02:00
|
|
|
local all = system.list_dir(path) or {}
|
2019-12-28 12:16:32 +01:00
|
|
|
local dirs, files = {}, {}
|
|
|
|
|
|
|
|
for _, file in ipairs(all) do
|
2020-05-17 14:31:04 +02:00
|
|
|
if not common.match_pattern(file, config.ignore_files) then
|
2020-05-17 17:59:19 +02:00
|
|
|
local file = (path ~= "." and path .. PATHSEP or "") .. file
|
2019-12-28 12:16:32 +01:00
|
|
|
local info = system.get_file_info(file)
|
|
|
|
if info and info.size < size_limit then
|
2020-05-06 18:03:10 +02:00
|
|
|
info.filename = file
|
|
|
|
table.insert(info.type == "dir" and dirs or files, info)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-06 18:03:10 +02:00
|
|
|
table.sort(dirs, compare_file)
|
|
|
|
for _, f in ipairs(dirs) do
|
|
|
|
table.insert(t, f)
|
|
|
|
get_files(f.filename, t)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2020-05-06 18:03:10 +02:00
|
|
|
table.sort(files, compare_file)
|
|
|
|
for _, f in ipairs(files) do
|
|
|
|
table.insert(t, f)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
|
|
|
while true do
|
|
|
|
-- get project files and replace previous table if the new table is
|
|
|
|
-- different
|
2020-05-17 17:59:19 +02:00
|
|
|
local t = get_files(".")
|
2019-12-28 12:16:32 +01:00
|
|
|
if diff_files(core.project_files, t) then
|
|
|
|
core.project_files = t
|
|
|
|
core.redraw = true
|
|
|
|
end
|
|
|
|
|
|
|
|
-- wait for next scan
|
|
|
|
coroutine.yield(config.project_scan_rate)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.init()
|
|
|
|
command = require "core.command"
|
|
|
|
keymap = require "core.keymap"
|
|
|
|
RootView = require "core.rootview"
|
|
|
|
StatusView = require "core.statusview"
|
|
|
|
CommandView = require "core.commandview"
|
|
|
|
Doc = require "core.doc"
|
|
|
|
|
2020-06-13 09:54:29 +02:00
|
|
|
local project_dir = EXEDIR
|
2020-05-26 11:17:36 +02:00
|
|
|
local files = {}
|
|
|
|
for i = 2, #ARGS do
|
|
|
|
local info = system.get_file_info(ARGS[i]) or {}
|
|
|
|
if info.type == "file" then
|
|
|
|
table.insert(files, system.absolute_path(ARGS[i]))
|
|
|
|
elseif info.type == "dir" then
|
|
|
|
project_dir = ARGS[i]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
system.chdir(project_dir)
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
core.frame_start = 0
|
|
|
|
core.clip_rect_stack = {{ 0,0,0,0 }}
|
|
|
|
core.log_items = {}
|
|
|
|
core.docs = {}
|
|
|
|
core.threads = setmetatable({}, { __mode = "k" })
|
|
|
|
core.project_files = {}
|
2020-05-24 09:04:47 +02:00
|
|
|
core.redraw = true
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
core.root_view = RootView()
|
|
|
|
core.command_view = CommandView()
|
|
|
|
core.status_view = StatusView()
|
|
|
|
|
|
|
|
core.root_view.root_node:split("down", core.command_view, true)
|
|
|
|
core.root_view.root_node.b:split("down", core.status_view, true)
|
|
|
|
|
|
|
|
core.add_thread(project_scan_thread)
|
|
|
|
command.add_defaults()
|
|
|
|
local got_plugin_error = not core.load_plugins()
|
|
|
|
local got_user_error = not core.try(require, "user")
|
2020-05-06 15:46:56 +02:00
|
|
|
local got_project_error = not core.load_project_module()
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2020-05-26 11:17:36 +02:00
|
|
|
for _, filename in ipairs(files) do
|
|
|
|
core.root_view:open_doc(core.open_doc(filename))
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2020-05-06 14:29:35 +02:00
|
|
|
if got_plugin_error or got_user_error or got_project_error then
|
2019-12-28 12:16:32 +01:00
|
|
|
command.perform("core:open-log")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-05-27 12:38:42 +02:00
|
|
|
local temp_uid = (system.get_time() * 1000) % 0xffffffff
|
|
|
|
local temp_file_prefix = string.format(".lite_temp_%08x", temp_uid)
|
|
|
|
local temp_file_counter = 0
|
2020-05-24 18:34:23 +02:00
|
|
|
|
|
|
|
local function delete_temp_files()
|
|
|
|
for _, filename in ipairs(system.list_dir(EXEDIR)) do
|
|
|
|
if filename:find(temp_file_prefix, 1, true) == 1 then
|
|
|
|
os.remove(EXEDIR .. PATHSEP .. filename)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function core.temp_filename(ext)
|
2020-05-27 12:38:42 +02:00
|
|
|
temp_file_counter = temp_file_counter + 1
|
|
|
|
return EXEDIR .. PATHSEP .. temp_file_prefix
|
|
|
|
.. string.format("%06x", temp_file_counter) .. (ext or "")
|
2020-05-24 18:34:23 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function core.quit(force)
|
|
|
|
if force then
|
2020-05-24 18:34:23 +02:00
|
|
|
delete_temp_files()
|
2019-12-28 12:16:32 +01:00
|
|
|
os.exit()
|
|
|
|
end
|
|
|
|
local dirty_count = 0
|
|
|
|
local dirty_name
|
|
|
|
for _, doc in ipairs(core.docs) do
|
|
|
|
if doc:is_dirty() then
|
|
|
|
dirty_count = dirty_count + 1
|
|
|
|
dirty_name = doc:get_name()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if dirty_count > 0 then
|
|
|
|
local text
|
|
|
|
if dirty_count == 1 then
|
2020-04-25 14:26:55 +02:00
|
|
|
text = string.format("\"%s\" has unsaved changes. Quit anyway?", dirty_name)
|
2019-12-28 12:16:32 +01:00
|
|
|
else
|
|
|
|
text = string.format("%d docs have unsaved changes. Quit anyway?", dirty_count)
|
|
|
|
end
|
|
|
|
local confirm = system.show_confirm_dialog("Unsaved Changes", text)
|
|
|
|
if not confirm then return end
|
|
|
|
end
|
|
|
|
core.quit(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.load_plugins()
|
|
|
|
local no_errors = true
|
2020-04-25 10:57:35 +02:00
|
|
|
local files = system.list_dir(EXEDIR .. "/data/plugins")
|
2019-12-28 12:16:32 +01:00
|
|
|
for _, filename in ipairs(files) do
|
|
|
|
local modname = "plugins." .. filename:gsub(".lua$", "")
|
|
|
|
local ok = core.try(require, modname)
|
|
|
|
if ok then
|
|
|
|
core.log_quiet("Loaded plugin %q", modname)
|
|
|
|
else
|
|
|
|
no_errors = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return no_errors
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-05-06 15:46:56 +02:00
|
|
|
function core.load_project_module()
|
2020-05-17 17:59:19 +02:00
|
|
|
local filename = ".lite_project.lua"
|
2020-05-06 14:29:35 +02:00
|
|
|
if system.get_file_info(filename) then
|
|
|
|
return core.try(function()
|
|
|
|
local fn, err = loadfile(filename)
|
2020-05-06 15:46:56 +02:00
|
|
|
if not fn then error("Error when loading project module:\n\t" .. err) end
|
2020-05-06 14:29:35 +02:00
|
|
|
fn()
|
2020-05-07 14:41:39 +02:00
|
|
|
core.log_quiet("Loaded project module")
|
2020-05-06 14:29:35 +02:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function core.reload_module(name)
|
|
|
|
local old = package.loaded[name]
|
|
|
|
package.loaded[name] = nil
|
|
|
|
local new = require(name)
|
|
|
|
if type(old) == "table" then
|
|
|
|
for k, v in pairs(new) do old[k] = v end
|
|
|
|
package.loaded[name] = old
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-05-19 13:58:41 +02:00
|
|
|
function core.set_active_view(view)
|
|
|
|
assert(view, "Tried to set active view to nil")
|
|
|
|
if view ~= core.active_view then
|
|
|
|
core.last_active_view = core.active_view
|
|
|
|
core.active_view = view
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function core.add_thread(f, weak_ref)
|
|
|
|
local key = weak_ref or #core.threads + 1
|
2020-04-22 00:23:08 +02:00
|
|
|
local fn = function() return core.try(f) end
|
|
|
|
core.threads[key] = { cr = coroutine.create(fn), wake = 0 }
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.push_clip_rect(x, y, w, h)
|
|
|
|
local x2, y2, w2, h2 = table.unpack(core.clip_rect_stack[#core.clip_rect_stack])
|
|
|
|
local r, b, r2, b2 = x+w, y+h, x2+w2, y2+h2
|
|
|
|
x, y = math.max(x, x2), math.max(y, y2)
|
|
|
|
b, r = math.min(b, b2), math.min(r, r2)
|
|
|
|
w, h = r-x, b-y
|
|
|
|
table.insert(core.clip_rect_stack, { x, y, w, h })
|
|
|
|
renderer.set_clip_rect(x, y, w, h)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.pop_clip_rect()
|
|
|
|
table.remove(core.clip_rect_stack)
|
|
|
|
local x, y, w, h = table.unpack(core.clip_rect_stack[#core.clip_rect_stack])
|
|
|
|
renderer.set_clip_rect(x, y, w, h)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.open_doc(filename)
|
|
|
|
if filename then
|
|
|
|
-- try to find existing doc for filename
|
|
|
|
local abs_filename = system.absolute_path(filename)
|
|
|
|
for _, doc in ipairs(core.docs) do
|
|
|
|
if doc.filename
|
|
|
|
and abs_filename == system.absolute_path(doc.filename) then
|
|
|
|
return doc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- no existing doc for filename; create new
|
|
|
|
local doc = Doc(filename)
|
|
|
|
table.insert(core.docs, doc)
|
2020-04-25 14:26:55 +02:00
|
|
|
core.log_quiet(filename and "Opened doc \"%s\"" or "Opened new doc", filename)
|
2019-12-28 12:16:32 +01:00
|
|
|
return doc
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.get_views_referencing_doc(doc)
|
|
|
|
local res = {}
|
|
|
|
local views = core.root_view.root_node:get_children()
|
|
|
|
for _, view in ipairs(views) do
|
|
|
|
if view.doc == doc then table.insert(res, view) end
|
|
|
|
end
|
|
|
|
return res
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function log(icon, icon_color, fmt, ...)
|
2020-05-06 01:06:27 +02:00
|
|
|
local text = string.format(fmt, ...)
|
2019-12-28 12:16:32 +01:00
|
|
|
if icon then
|
|
|
|
core.status_view:show_message(icon, icon_color, text)
|
|
|
|
end
|
|
|
|
|
2020-04-22 01:02:35 +02:00
|
|
|
local info = debug.getinfo(2, "Sl")
|
|
|
|
local at = string.format("%s:%d", info.short_src, info.currentline)
|
|
|
|
local item = { text = text, time = os.time(), at = at }
|
2019-12-28 12:16:32 +01:00
|
|
|
table.insert(core.log_items, item)
|
|
|
|
if #core.log_items > config.max_log_items then
|
|
|
|
table.remove(core.log_items, 1)
|
|
|
|
end
|
|
|
|
return item
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.log(...)
|
|
|
|
return log("i", style.text, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.log_quiet(...)
|
|
|
|
return log(nil, nil, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.error(...)
|
|
|
|
return log("!", style.accent, ...)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.try(fn, ...)
|
|
|
|
local err
|
|
|
|
local ok, res = xpcall(fn, function(msg)
|
2020-04-26 22:40:25 +02:00
|
|
|
local item = core.error("%s", msg)
|
2019-12-28 12:16:32 +01:00
|
|
|
item.info = debug.traceback(nil, 2):gsub("\t", "")
|
|
|
|
err = msg
|
|
|
|
end, ...)
|
|
|
|
if ok then
|
|
|
|
return true, res
|
|
|
|
end
|
|
|
|
return false, err
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.on_event(type, ...)
|
|
|
|
local did_keymap = false
|
|
|
|
if type == "textinput" then
|
|
|
|
core.root_view:on_text_input(...)
|
|
|
|
elseif type == "keypressed" then
|
|
|
|
did_keymap = keymap.on_key_pressed(...)
|
|
|
|
elseif type == "keyreleased" then
|
|
|
|
keymap.on_key_released(...)
|
|
|
|
elseif type == "mousemoved" then
|
|
|
|
core.root_view:on_mouse_moved(...)
|
|
|
|
elseif type == "mousepressed" then
|
|
|
|
core.root_view:on_mouse_pressed(...)
|
|
|
|
elseif type == "mousereleased" then
|
|
|
|
core.root_view:on_mouse_released(...)
|
|
|
|
elseif type == "mousewheel" then
|
|
|
|
core.root_view:on_mouse_wheel(...)
|
|
|
|
elseif type == "filedropped" then
|
2020-05-20 11:33:08 +02:00
|
|
|
local filename, mx, my = ...
|
2020-05-23 12:31:08 +02:00
|
|
|
local info = system.get_file_info(filename)
|
|
|
|
if info and info.type == "dir" then
|
|
|
|
system.exec(string.format("%q %q", EXEFILE, filename))
|
|
|
|
else
|
|
|
|
local ok, doc = core.try(core.open_doc, filename)
|
|
|
|
if ok then
|
|
|
|
local node = core.root_view.root_node:get_child_overlapping_point(mx, my)
|
|
|
|
node:set_active_view(node.active_view)
|
|
|
|
core.root_view:open_doc(doc)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
elseif type == "quit" then
|
|
|
|
core.quit()
|
|
|
|
end
|
|
|
|
return did_keymap
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.step()
|
|
|
|
-- handle events
|
|
|
|
local did_keymap = false
|
|
|
|
local mouse_moved = false
|
|
|
|
local mouse = { x = 0, y = 0, dx = 0, dy = 0 }
|
|
|
|
|
|
|
|
for type, a,b,c,d in system.poll_event do
|
|
|
|
if type == "mousemoved" then
|
|
|
|
mouse_moved = true
|
|
|
|
mouse.x, mouse.y = a, b
|
|
|
|
mouse.dx, mouse.dy = mouse.dx + c, mouse.dy + d
|
|
|
|
elseif type == "textinput" and did_keymap then
|
|
|
|
did_keymap = false
|
|
|
|
else
|
2020-05-09 09:38:51 +02:00
|
|
|
local _, res = core.try(core.on_event, type, a, b, c, d)
|
|
|
|
did_keymap = res or did_keymap
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2020-05-08 21:44:53 +02:00
|
|
|
core.redraw = true
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
if mouse_moved then
|
2020-05-09 09:38:51 +02:00
|
|
|
core.try(core.on_event, "mousemoved", mouse.x, mouse.y, mouse.dx, mouse.dy)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
local width, height = renderer.get_size()
|
|
|
|
|
|
|
|
-- update
|
|
|
|
core.root_view.size.x, core.root_view.size.y = width, height
|
|
|
|
core.root_view:update()
|
2020-05-24 14:31:49 +02:00
|
|
|
if not core.redraw then return false end
|
2019-12-28 12:16:32 +01:00
|
|
|
core.redraw = false
|
|
|
|
|
|
|
|
-- close unreferenced docs
|
|
|
|
for i = #core.docs, 1, -1 do
|
|
|
|
local doc = core.docs[i]
|
|
|
|
if #core.get_views_referencing_doc(doc) == 0 then
|
|
|
|
table.remove(core.docs, i)
|
2020-04-25 14:26:55 +02:00
|
|
|
core.log_quiet("Closed doc \"%s\"", doc:get_name())
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- update window title
|
|
|
|
local name = core.active_view:get_name()
|
2020-05-30 09:53:48 +02:00
|
|
|
local title = (name ~= "---") and (name .. " - lite") or "lite"
|
|
|
|
if title ~= core.window_title then
|
|
|
|
system.set_window_title(title)
|
|
|
|
core.window_title = title
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- draw
|
|
|
|
renderer.begin_frame()
|
|
|
|
core.clip_rect_stack[1] = { 0, 0, width, height }
|
|
|
|
renderer.set_clip_rect(table.unpack(core.clip_rect_stack[1]))
|
|
|
|
core.root_view:draw()
|
|
|
|
renderer.end_frame()
|
2020-05-24 14:31:49 +02:00
|
|
|
return true
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local run_threads = coroutine.wrap(function()
|
|
|
|
while true do
|
|
|
|
local max_time = 1 / config.fps - 0.004
|
|
|
|
local ran_any_threads = false
|
|
|
|
|
|
|
|
for k, thread in pairs(core.threads) do
|
|
|
|
-- run thread
|
|
|
|
if thread.wake < system.get_time() then
|
|
|
|
local _, wait = assert(coroutine.resume(thread.cr))
|
|
|
|
if coroutine.status(thread.cr) == "dead" then
|
|
|
|
if type(k) == "number" then
|
|
|
|
table.remove(core.threads, k)
|
|
|
|
else
|
|
|
|
core.threads[k] = nil
|
|
|
|
end
|
|
|
|
elseif wait then
|
|
|
|
thread.wake = system.get_time() + wait
|
|
|
|
end
|
|
|
|
ran_any_threads = true
|
|
|
|
end
|
|
|
|
|
|
|
|
-- stop running threads if we're about to hit the end of frame
|
|
|
|
if system.get_time() - core.frame_start > max_time then
|
|
|
|
coroutine.yield()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
if not ran_any_threads then coroutine.yield() end
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
|
|
function core.run()
|
|
|
|
while true do
|
|
|
|
core.frame_start = system.get_time()
|
2020-05-24 14:31:49 +02:00
|
|
|
local did_redraw = core.step()
|
2019-12-28 12:16:32 +01:00
|
|
|
run_threads()
|
2020-05-24 14:31:49 +02:00
|
|
|
if not did_redraw and not system.window_has_focus() then
|
|
|
|
system.wait_event(0.25)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
local elapsed = system.get_time() - core.frame_start
|
|
|
|
system.sleep(math.max(0, 1 / config.fps - elapsed))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.on_error(err)
|
|
|
|
-- write error to file
|
2020-04-25 10:57:35 +02:00
|
|
|
local fp = io.open(EXEDIR .. "/error.txt", "wb")
|
2019-12-28 12:16:32 +01:00
|
|
|
fp:write("Error: " .. tostring(err) .. "\n")
|
|
|
|
fp:write(debug.traceback(nil, 4))
|
|
|
|
fp:close()
|
|
|
|
-- save copy of all unsaved documents
|
|
|
|
for _, doc in ipairs(core.docs) do
|
|
|
|
if doc:is_dirty() and doc.filename then
|
|
|
|
doc:save(doc.filename .. "~")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
return core
|