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
|
2021-04-10 19:35:57 +02:00
|
|
|
local TitleView
|
2019-12-28 12:16:32 +01:00
|
|
|
local CommandView
|
2021-03-13 12:57:52 +01:00
|
|
|
local NagView
|
2021-02-15 22:38:29 +01:00
|
|
|
local DocView
|
2019-12-28 12:16:32 +01:00
|
|
|
local Doc
|
|
|
|
|
|
|
|
local core = {}
|
|
|
|
|
2020-12-31 11:25:12 +01:00
|
|
|
local function load_session()
|
|
|
|
local ok, t = pcall(dofile, USERDIR .. "/session.lua")
|
|
|
|
if ok then
|
|
|
|
return t.recents, t.window
|
|
|
|
end
|
|
|
|
return {}
|
2020-12-08 16:57:24 +01:00
|
|
|
end
|
|
|
|
|
2020-12-31 11:25:12 +01:00
|
|
|
|
|
|
|
local function save_session()
|
|
|
|
local fp = io.open(USERDIR .. "/session.lua", "w")
|
|
|
|
if fp then
|
|
|
|
fp:write("return {recents=", common.serialize(core.recent_projects),
|
|
|
|
", window=", common.serialize(table.pack(system.get_window_size())),
|
|
|
|
"}\n")
|
|
|
|
fp:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-02-19 00:14:34 +01:00
|
|
|
local function update_recents_project(action, dir_path_abs)
|
2021-05-10 13:08:39 +02:00
|
|
|
local dirname = common.normalize_path(dir_path_abs)
|
2020-12-08 16:57:24 +01:00
|
|
|
if not dirname then return end
|
|
|
|
local recents = core.recent_projects
|
|
|
|
local n = #recents
|
|
|
|
for i = 1, n do
|
2020-12-08 18:49:58 +01:00
|
|
|
if dirname == recents[i] then
|
|
|
|
table.remove(recents, i)
|
|
|
|
break
|
|
|
|
end
|
2020-12-08 16:57:24 +01:00
|
|
|
end
|
2021-02-19 00:14:34 +01:00
|
|
|
if action == "add" then
|
|
|
|
table.insert(recents, 1, dirname)
|
|
|
|
end
|
2020-12-08 16:57:24 +01:00
|
|
|
end
|
|
|
|
|
2020-12-27 11:32:52 +01:00
|
|
|
|
2021-05-14 10:59:42 +02:00
|
|
|
local function cleanup_recent_projects()
|
|
|
|
local recents = core.recent_projects
|
|
|
|
local i = 1
|
|
|
|
while i <= #recents do
|
|
|
|
local info = system.get_file_info(recents[i])
|
|
|
|
if not info or info.type ~= "dir" then
|
|
|
|
table.remove(recents, i)
|
|
|
|
else
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-30 14:31:36 +01:00
|
|
|
function core.reschedule_project_scan()
|
|
|
|
if core.project_scan_thread_id then
|
|
|
|
core.threads[core.project_scan_thread_id].wake = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-02-03 07:39:35 +01:00
|
|
|
function core.open_folder_project(dir_path_abs)
|
2021-03-01 14:57:28 +01:00
|
|
|
core.root_view:close_all_docviews()
|
|
|
|
core.project_entries = {}
|
|
|
|
core.add_project_directory(dir_path_abs)
|
|
|
|
update_recents_project("add", dir_path_abs)
|
2020-12-08 16:57:24 +01:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2020-12-28 10:26:27 +01:00
|
|
|
|
|
|
|
local function strip_leading_path(filename)
|
|
|
|
return filename:sub(2)
|
|
|
|
end
|
|
|
|
|
2021-01-10 12:56:15 +01:00
|
|
|
local function strip_trailing_slash(filename)
|
|
|
|
if filename:match("[^:][/\\]$") then
|
|
|
|
return filename:sub(1, -2)
|
|
|
|
end
|
|
|
|
return filename
|
|
|
|
end
|
2020-12-28 10:26:27 +01:00
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
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
|
|
|
|
|
2020-12-28 10:26:27 +01:00
|
|
|
-- "root" will by an absolute path without trailing '/'
|
|
|
|
-- "path" will be a path starting with '/' and without trailing '/'
|
|
|
|
-- or the empty string.
|
|
|
|
-- It will identifies a sub-path within "root.
|
|
|
|
-- The current path location will therefore always be: root .. path.
|
|
|
|
-- When recursing "root" will always be the same, only "path" will change.
|
|
|
|
-- Returns a list of file "items". In eash item the "filename" will be the
|
|
|
|
-- complete file path relative to "root" *without* the trailing '/'.
|
2020-12-27 09:47:58 +01:00
|
|
|
local function get_files(root, path, t)
|
2020-12-26 11:30:20 +01:00
|
|
|
coroutine.yield()
|
2019-12-28 12:16:32 +01:00
|
|
|
t = t or {}
|
|
|
|
local size_limit = config.file_size_limit * 10e5
|
2020-12-27 09:47:58 +01:00
|
|
|
local all = system.list_dir(root .. path) or {}
|
2019-12-28 12:16:32 +01:00
|
|
|
local dirs, files = {}, {}
|
|
|
|
|
2020-11-16 14:45:45 +01:00
|
|
|
local entries_count = 0
|
|
|
|
local max_entries = config.max_project_files
|
2019-12-28 12:16:32 +01:00
|
|
|
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-12-26 23:39:27 +01:00
|
|
|
local file = path .. PATHSEP .. file
|
2020-12-27 09:47:58 +01:00
|
|
|
local info = system.get_file_info(root .. file)
|
2019-12-28 12:16:32 +01:00
|
|
|
if info and info.size < size_limit then
|
2020-12-28 10:26:27 +01:00
|
|
|
info.filename = strip_leading_path(file)
|
2020-05-06 18:03:10 +02:00
|
|
|
table.insert(info.type == "dir" and dirs or files, info)
|
2020-11-16 14:45:45 +01:00
|
|
|
entries_count = entries_count + 1
|
|
|
|
if entries_count > max_entries then break end
|
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)
|
2020-11-16 14:45:45 +01:00
|
|
|
if entries_count <= max_entries then
|
2020-12-28 10:26:27 +01:00
|
|
|
local subdir_t, subdir_count = get_files(root, PATHSEP .. f.filename, t)
|
2020-11-16 14:45:45 +01:00
|
|
|
entries_count = entries_count + subdir_count
|
|
|
|
end
|
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
|
|
|
|
|
2020-11-16 14:45:45 +01:00
|
|
|
return t, entries_count
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
while true do
|
|
|
|
-- get project files and replace previous table if the new table is
|
|
|
|
-- different
|
2021-03-01 14:57:28 +01:00
|
|
|
for i = 1, #core.project_entries do
|
|
|
|
local dir = core.project_entries[i]
|
|
|
|
if dir.item.type == 'dir' then
|
|
|
|
local t, entries_count = get_files(dir.name, "")
|
|
|
|
if diff_files(dir.files, t) then
|
|
|
|
if entries_count > config.max_project_files then
|
|
|
|
core.status_view:show_message("!", style.accent,
|
|
|
|
"Too many files in project directory: stopping reading at "..
|
|
|
|
config.max_project_files.." files according to config.max_project_files. "..
|
|
|
|
"Either tweak this variable, or ignore certain files/directories by "..
|
|
|
|
"using the config.ignore_files variable in your user plugin or "..
|
|
|
|
"project config.")
|
|
|
|
end
|
|
|
|
dir.files = t
|
|
|
|
core.redraw = true
|
2020-12-26 21:22:57 +01:00
|
|
|
end
|
2020-12-30 14:53:50 +01:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- wait for next scan
|
2020-12-30 14:31:36 +01:00
|
|
|
coroutine.yield(config.project_scan_rate)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-27 11:32:52 +01:00
|
|
|
local function project_files_iter(state)
|
2021-03-01 14:57:28 +01:00
|
|
|
local dir = core.project_entries[state.dir_index]
|
2020-12-27 11:32:52 +01:00
|
|
|
state.file_index = state.file_index + 1
|
|
|
|
while dir and state.file_index > #dir.files do
|
|
|
|
state.dir_index = state.dir_index + 1
|
|
|
|
state.file_index = 1
|
2021-03-01 14:57:28 +01:00
|
|
|
dir = core.project_entries[state.dir_index]
|
2020-12-27 11:32:52 +01:00
|
|
|
end
|
|
|
|
if not dir then return end
|
|
|
|
return dir.name, dir.files[state.file_index]
|
|
|
|
end
|
|
|
|
|
2020-12-27 18:28:21 +01:00
|
|
|
|
|
|
|
function core.get_project_files()
|
2020-12-27 11:32:52 +01:00
|
|
|
local state = { dir_index = 1, file_index = 0 }
|
|
|
|
return project_files_iter, state
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-27 18:28:21 +01:00
|
|
|
function core.project_files_number()
|
|
|
|
local n = 0
|
2021-03-01 14:57:28 +01:00
|
|
|
for i = 1, #core.project_entries do
|
|
|
|
n = n + #core.project_entries[i].files
|
2020-12-27 18:28:21 +01:00
|
|
|
end
|
|
|
|
return n
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-02 16:03:31 +01:00
|
|
|
-- create a directory using mkdir but may need to create the parent
|
|
|
|
-- directories as well.
|
|
|
|
local function create_user_directory()
|
|
|
|
local dirname_create = USERDIR
|
|
|
|
local basedir
|
|
|
|
local subdirs = {}
|
|
|
|
while dirname_create and dirname_create ~= "" do
|
|
|
|
local success_mkdir = system.mkdir(dirname_create)
|
|
|
|
if success_mkdir then break end
|
|
|
|
dirname_create, basedir = dirname_create:match("(.*)[/\\](.+)$")
|
|
|
|
if basedir then
|
|
|
|
subdirs[#subdirs + 1] = basedir
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for _, dirname in ipairs(subdirs) do
|
|
|
|
dirname_create = dirname_create .. '/' .. dirname
|
2020-12-06 11:23:33 +01:00
|
|
|
if not system.mkdir(dirname_create) then
|
|
|
|
error("cannot create directory: \"" .. dirname_create .. "\"")
|
|
|
|
end
|
|
|
|
end
|
2021-02-19 16:10:02 +01:00
|
|
|
for _, modname in ipairs {'plugins', 'colors', 'fonts'} do
|
2020-12-06 11:23:33 +01:00
|
|
|
local subdirname = dirname_create .. '/' .. modname
|
|
|
|
if not system.mkdir(subdirname) then
|
|
|
|
error("cannot create directory: \"" .. subdirname .. "\"")
|
|
|
|
end
|
2020-12-02 16:03:31 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function write_user_init_file(init_filename)
|
|
|
|
local init_file = io.open(init_filename, "w")
|
|
|
|
if not init_file then error("cannot create file: \"" .. init_filename .. "\"") end
|
|
|
|
init_file:write([[
|
|
|
|
-- put user settings here
|
|
|
|
-- this module will be loaded after everything else when the application starts
|
2020-12-11 14:49:03 +01:00
|
|
|
-- it will be automatically reloaded when saved
|
2020-12-02 16:03:31 +01:00
|
|
|
|
2020-12-13 13:04:06 +01:00
|
|
|
local core = require "core"
|
2020-12-02 16:03:31 +01:00
|
|
|
local keymap = require "core.keymap"
|
|
|
|
local config = require "core.config"
|
|
|
|
local style = require "core.style"
|
|
|
|
|
2021-01-13 14:50:24 +01:00
|
|
|
------------------------------ Themes ----------------------------------------
|
|
|
|
|
2020-12-02 16:03:31 +01:00
|
|
|
-- light theme:
|
2020-12-13 13:04:06 +01:00
|
|
|
-- core.reload_module("colors.summer")
|
2020-12-02 16:03:31 +01:00
|
|
|
|
2021-01-13 14:50:24 +01:00
|
|
|
--------------------------- Key bindings -------------------------------------
|
|
|
|
|
2020-12-02 16:03:31 +01:00
|
|
|
-- key binding:
|
|
|
|
-- keymap.add { ["ctrl+escape"] = "core:quit" }
|
2020-12-10 17:56:53 +01:00
|
|
|
|
2021-01-13 14:50:24 +01:00
|
|
|
|
|
|
|
------------------------------- Fonts ----------------------------------------
|
|
|
|
|
2020-12-10 17:56:53 +01:00
|
|
|
-- customize fonts:
|
2021-03-01 09:48:12 +01:00
|
|
|
-- style.font = renderer.font.load(DATADIR .. "/fonts/font.ttf", 13 * SCALE)
|
2021-02-19 16:23:19 +01:00
|
|
|
-- style.code_font = renderer.font.load(DATADIR .. "/fonts/monospace.ttf", 12 * SCALE)
|
2020-12-10 17:56:53 +01:00
|
|
|
--
|
2021-01-13 14:50:24 +01:00
|
|
|
-- font names used by lite:
|
2021-02-27 12:13:51 +01:00
|
|
|
-- style.font : user interface
|
|
|
|
-- style.big_font : big text in welcome screen
|
|
|
|
-- style.icon_font : icons
|
|
|
|
-- style.icon_big_font : toolbar icons
|
|
|
|
-- style.code_font : code
|
2020-12-11 14:49:03 +01:00
|
|
|
--
|
|
|
|
-- the function to load the font accept a 3rd optional argument like:
|
|
|
|
--
|
|
|
|
-- {antialiasing="grayscale", hinting="full"}
|
|
|
|
--
|
|
|
|
-- possible values are:
|
|
|
|
-- antialiasing: grayscale, subpixel
|
|
|
|
-- hinting: none, slight, full
|
2021-01-13 14:50:24 +01:00
|
|
|
|
|
|
|
------------------------------ Plugins ----------------------------------------
|
|
|
|
|
|
|
|
-- enable or disable plugin loading setting config entries:
|
|
|
|
|
|
|
|
-- enable trimwhitespace, otherwise it is disable by default:
|
|
|
|
-- config.trimwhitespace = true
|
|
|
|
--
|
|
|
|
-- disable detectindent, otherwise it is enabled by default
|
|
|
|
-- config.detectindent = false
|
2020-12-02 16:03:31 +01:00
|
|
|
]])
|
|
|
|
init_file:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
2020-12-10 17:56:53 +01:00
|
|
|
function core.load_user_directory()
|
|
|
|
return core.try(function()
|
|
|
|
local stat_dir = system.get_file_info(USERDIR)
|
|
|
|
if not stat_dir then
|
|
|
|
create_user_directory()
|
|
|
|
end
|
|
|
|
local init_filename = USERDIR .. "/init.lua"
|
|
|
|
local stat_file = system.get_file_info(init_filename)
|
|
|
|
if not stat_file then
|
|
|
|
write_user_init_file(init_filename)
|
|
|
|
end
|
|
|
|
dofile(init_filename)
|
|
|
|
end)
|
2020-12-02 16:03:31 +01:00
|
|
|
end
|
|
|
|
|
2021-03-01 14:57:28 +01:00
|
|
|
function core.add_project_file(path)
|
2021-05-13 12:07:56 +02:00
|
|
|
path = common.normalize_path(path)
|
2021-05-14 10:59:42 +02:00
|
|
|
local entry = {
|
2021-03-01 14:57:28 +01:00
|
|
|
name = path,
|
|
|
|
item = {filename = common.basename(path), type = "file", topdir = true},
|
|
|
|
files = {path}
|
2021-05-14 10:59:42 +02:00
|
|
|
}
|
|
|
|
table.insert(core.project_entries, entry)
|
2021-03-01 14:57:28 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-27 11:32:52 +01:00
|
|
|
function core.add_project_directory(path)
|
2020-12-28 10:26:27 +01:00
|
|
|
-- top directories has a file-like "item" but the item.filename
|
|
|
|
-- will be simply the name of the directory, without its path.
|
|
|
|
-- The field item.topdir will identify it as a top level directory.
|
2021-05-10 13:08:39 +02:00
|
|
|
path = common.normalize_path(path)
|
2021-05-14 10:59:42 +02:00
|
|
|
local entry = {
|
2020-12-27 11:32:52 +01:00
|
|
|
name = path,
|
2021-01-10 12:56:15 +01:00
|
|
|
item = {filename = common.basename(path), type = "dir", topdir = true},
|
2020-12-27 11:32:52 +01:00
|
|
|
files = {}
|
2021-05-14 10:59:42 +02:00
|
|
|
}
|
|
|
|
table.insert(core.project_entries, entry)
|
2020-12-27 11:32:52 +01:00
|
|
|
end
|
2020-12-02 16:03:31 +01:00
|
|
|
|
2020-12-28 15:51:16 +01:00
|
|
|
|
2021-03-01 14:57:28 +01:00
|
|
|
function core.remove_project_entry(path)
|
2021-01-03 12:02:07 +01:00
|
|
|
-- skip the fist directory because it is the project's directory
|
2021-03-01 14:57:28 +01:00
|
|
|
for i = 2, #core.project_entries do
|
|
|
|
local dir = core.project_entries[i]
|
2020-12-28 15:51:16 +01:00
|
|
|
if dir.name == path then
|
2021-03-01 14:57:28 +01:00
|
|
|
table.remove(core.project_entries, i)
|
2020-12-28 15:51:16 +01:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-03-31 17:33:35 +02:00
|
|
|
local function whitespace_replacements()
|
|
|
|
local r = renderer.replacements.new()
|
|
|
|
r:add(" ", "·")
|
|
|
|
r:add("\t", "»")
|
|
|
|
return r
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function core.init()
|
|
|
|
command = require "core.command"
|
|
|
|
keymap = require "core.keymap"
|
|
|
|
RootView = require "core.rootview"
|
|
|
|
StatusView = require "core.statusview"
|
2021-04-10 19:35:57 +02:00
|
|
|
TitleView = require "core.titleview"
|
2019-12-28 12:16:32 +01:00
|
|
|
CommandView = require "core.commandview"
|
2021-03-13 12:57:52 +01:00
|
|
|
NagView = require "core.nagview"
|
2021-02-16 19:12:56 +01:00
|
|
|
DocView = require "core.docview"
|
2019-12-28 12:16:32 +01:00
|
|
|
Doc = require "core.doc"
|
|
|
|
|
2021-02-18 09:02:57 +01:00
|
|
|
if PATHSEP == '\\' then
|
|
|
|
USERDIR = common.normalize_path(USERDIR)
|
|
|
|
DATADIR = common.normalize_path(DATADIR)
|
|
|
|
EXEDIR = common.normalize_path(EXEDIR)
|
|
|
|
end
|
|
|
|
|
2020-12-31 11:25:12 +01:00
|
|
|
do
|
|
|
|
local recent_projects, window_position = load_session()
|
|
|
|
if window_position then
|
|
|
|
system.set_window_size(table.unpack(window_position))
|
|
|
|
end
|
|
|
|
core.recent_projects = recent_projects
|
|
|
|
end
|
2021-05-14 10:59:42 +02:00
|
|
|
cleanup_recent_projects()
|
|
|
|
|
|
|
|
core.log_items = {}
|
|
|
|
core.docs = {}
|
|
|
|
core.project_entries = {}
|
2020-12-08 16:57:24 +01:00
|
|
|
|
2021-05-14 10:59:42 +02:00
|
|
|
local init_files = {}
|
|
|
|
local delayed_errors = {}
|
2020-05-26 11:17:36 +02:00
|
|
|
for i = 2, #ARGS do
|
2021-05-14 10:59:42 +02:00
|
|
|
local filename = strip_trailing_slash(ARGS[i])
|
|
|
|
local info = system.get_file_info(filename)
|
|
|
|
if info and info.type == "file" then
|
|
|
|
filename = system.absolute_path(filename)
|
|
|
|
if filename then
|
|
|
|
core.add_project_file(filename)
|
|
|
|
table.insert(init_files, filename)
|
2021-01-10 11:50:15 +01:00
|
|
|
end
|
2021-05-14 10:59:42 +02:00
|
|
|
elseif info and info.type == "dir" then
|
|
|
|
filename = system.absolute_path(filename)
|
|
|
|
if filename then
|
|
|
|
core.add_project_directory(filename)
|
|
|
|
update_recents_project("add", filename)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local error_msg = string.format("error: invalid file or directory \"%s\"", ARGS[i])
|
|
|
|
table.insert(delayed_errors, error_msg)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
local init_message
|
|
|
|
if #core.project_entries == 0 then
|
|
|
|
if #core.recent_projects > 0 then
|
|
|
|
init_message = string.format("Opening project from directory \"%s\"", core.recent_projects[1])
|
|
|
|
core.add_project_directory(core.recent_projects[1])
|
2021-02-19 00:14:34 +01:00
|
|
|
else
|
2021-05-14 10:59:42 +02:00
|
|
|
init_message = string.format("Empty project: use the \"Core: Open File\" command to open a file or a directory.")
|
2020-05-26 11:17:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-14 10:59:42 +02:00
|
|
|
core.threads = setmetatable({}, { __mode = "k" })
|
2019-12-28 12:16:32 +01:00
|
|
|
core.frame_start = 0
|
|
|
|
core.clip_rect_stack = {{ 0,0,0,0 }}
|
2021-04-12 13:31:32 +02:00
|
|
|
core.window_mode = "normal"
|
2021-04-17 23:57:04 +02:00
|
|
|
core.blink_start = system.get_time()
|
|
|
|
core.blink_timer = core.blink_start
|
2020-05-24 09:04:47 +02:00
|
|
|
core.redraw = true
|
2020-11-16 23:52:39 +01:00
|
|
|
core.visited_files = {}
|
2020-12-10 12:44:01 +01:00
|
|
|
core.restart_request = false
|
2021-03-31 17:33:35 +02:00
|
|
|
core.replacements = whitespace_replacements()
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
core.root_view = RootView()
|
|
|
|
core.command_view = CommandView()
|
|
|
|
core.status_view = StatusView()
|
2021-03-13 12:57:52 +01:00
|
|
|
core.nag_view = NagView()
|
2021-04-10 19:35:57 +02:00
|
|
|
core.title_view = TitleView()
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2021-02-11 16:12:21 +01:00
|
|
|
local cur_node = core.root_view.root_node
|
|
|
|
cur_node.is_primary_node = true
|
2021-04-10 19:35:57 +02:00
|
|
|
cur_node:split("up", core.title_view, {y = true})
|
|
|
|
cur_node = cur_node.b
|
2021-03-13 12:57:52 +01:00
|
|
|
cur_node:split("up", core.nag_view, {y = true})
|
|
|
|
cur_node = cur_node.b
|
2021-02-15 00:36:39 +01:00
|
|
|
cur_node = cur_node:split("down", core.command_view, {y = true})
|
|
|
|
cur_node = cur_node:split("down", core.status_view, {y = true})
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2020-12-09 15:41:25 +01:00
|
|
|
core.project_scan_thread_id = core.add_thread(project_scan_thread)
|
2019-12-28 12:16:32 +01:00
|
|
|
command.add_defaults()
|
2020-12-08 18:49:58 +01:00
|
|
|
|
2021-05-14 10:59:42 +02:00
|
|
|
for _, project_entry in ipairs(core.project_entries) do
|
|
|
|
if project_entry.item.type == "dir" then
|
|
|
|
core.log_quiet("Setting working directory to \"%s\"", project_entry.name)
|
|
|
|
system.chdir(project_entry.name)
|
|
|
|
core.working_dir = project_entry.name
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if not core.working_dir then
|
|
|
|
core.working_dir = system.absolute_path(".")
|
2020-12-08 18:49:58 +01:00
|
|
|
end
|
2021-05-14 10:59:42 +02:00
|
|
|
|
|
|
|
if init_message then
|
|
|
|
core.log(init_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
local got_user_error = not core.load_user_directory()
|
|
|
|
local plugins_success, plugins_refuse_list = core.load_plugins()
|
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
|
|
|
|
2021-05-14 10:59:42 +02:00
|
|
|
for _, filename in ipairs(init_files) do
|
2020-05-26 11:17:36 +02:00
|
|
|
core.root_view:open_doc(core.open_doc(filename))
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2021-05-14 10:59:42 +02:00
|
|
|
for _, error_msg in ipairs(delayed_errors) do
|
|
|
|
core.error(error_msg)
|
2021-02-19 09:29:46 +01:00
|
|
|
end
|
|
|
|
|
2021-04-07 09:42:19 +02:00
|
|
|
if not plugins_success or got_user_error or got_project_error then
|
2019-12-28 12:16:32 +01:00
|
|
|
command.perform("core:open-log")
|
|
|
|
end
|
2021-04-07 09:42:19 +02:00
|
|
|
|
2021-04-12 19:05:30 +02:00
|
|
|
system.set_window_bordered(not config.borderless)
|
|
|
|
core.title_view:configure_hit_test(config.borderless)
|
|
|
|
core.title_view.visible = config.borderless
|
|
|
|
|
2021-04-07 09:42:19 +02:00
|
|
|
if #plugins_refuse_list.userdir.plugins > 0 or #plugins_refuse_list.datadir.plugins > 0 then
|
|
|
|
local opt = {
|
|
|
|
{ font = style.font, text = "Exit", default_no = true },
|
|
|
|
{ font = style.font, text = "Continue" , default_yes = true }
|
|
|
|
}
|
|
|
|
local msg = {}
|
|
|
|
for _, entry in pairs(plugins_refuse_list) do
|
|
|
|
if #entry.plugins > 0 then
|
2021-04-07 23:46:45 +02:00
|
|
|
msg[#msg + 1] = string.format("Plugins from directory \"%s\":\n%s", common.home_encode(entry.dir), table.concat(entry.plugins, "\n"))
|
2021-04-07 09:42:19 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
core.nag_view:show(
|
|
|
|
"Refused Plugins",
|
2021-04-07 23:46:45 +02:00
|
|
|
string.format(
|
|
|
|
"Some plugins are not loaded due to version mismatch.\n\n%s.\n\n" ..
|
|
|
|
"Please download a recent version from https://github.com/franko/lite-plugins.",
|
|
|
|
table.concat(msg, ".\n\n")),
|
2021-04-07 09:42:19 +02:00
|
|
|
opt, function(item)
|
|
|
|
if item.text == "Exit" then os.exit(1) end
|
|
|
|
end)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-03-13 12:57:52 +01:00
|
|
|
function core.confirm_close_all(close_fn, ...)
|
2019-12-28 12:16:32 +01:00
|
|
|
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
|
2021-03-13 12:57:52 +01:00
|
|
|
local args = {...}
|
|
|
|
local opt = {
|
2021-04-04 16:11:47 +02:00
|
|
|
{ font = style.font, text = "Yes", default_yes = true },
|
|
|
|
{ font = style.font, text = "No" , default_no = true }
|
2021-03-13 12:57:52 +01:00
|
|
|
}
|
|
|
|
core.nag_view:show("Unsaved Changes", text, opt, function(item)
|
|
|
|
if item.text == "Yes" then close_fn(table.unpack(args)) end
|
|
|
|
end)
|
|
|
|
else
|
|
|
|
close_fn(...)
|
2020-12-06 00:57:27 +01:00
|
|
|
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
|
2021-01-22 02:10:24 +01:00
|
|
|
return USERDIR .. PATHSEP .. temp_file_prefix
|
2020-05-27 12:38:42 +02:00
|
|
|
.. string.format("%06x", temp_file_counter) .. (ext or "")
|
2020-05-24 18:34:23 +02:00
|
|
|
end
|
|
|
|
|
2020-12-30 14:31:36 +01:00
|
|
|
-- override to perform an operation before quitting or entering the
|
|
|
|
-- current project
|
|
|
|
do
|
|
|
|
local do_nothing = function() end
|
|
|
|
core.on_quit_project = do_nothing
|
|
|
|
core.on_enter_project = do_nothing
|
2020-12-14 13:57:24 +01:00
|
|
|
end
|
2020-05-24 18:34:23 +02:00
|
|
|
|
2021-02-17 20:10:39 +01:00
|
|
|
|
|
|
|
core.doc_save_hooks = {}
|
|
|
|
function core.add_save_hook(fn)
|
|
|
|
core.doc_save_hooks[#core.doc_save_hooks + 1] = fn
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.on_doc_save(filename)
|
|
|
|
for _, hook in ipairs(core.doc_save_hooks) do
|
|
|
|
hook(filename)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-14 13:57:24 +01:00
|
|
|
local function quit_with_function(quit_fn, force)
|
2019-12-28 12:16:32 +01:00
|
|
|
if force then
|
2020-05-24 18:34:23 +02:00
|
|
|
delete_temp_files()
|
2020-12-30 14:31:36 +01:00
|
|
|
core.on_quit_project()
|
2020-12-31 11:25:12 +01:00
|
|
|
save_session()
|
2020-12-14 13:57:24 +01:00
|
|
|
quit_fn()
|
|
|
|
else
|
2021-03-13 12:57:52 +01:00
|
|
|
core.confirm_close_all(quit_with_function, quit_fn, true)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-14 13:57:24 +01:00
|
|
|
function core.quit(force)
|
|
|
|
quit_with_function(os.exit, force)
|
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2020-12-10 12:44:01 +01:00
|
|
|
function core.restart()
|
2020-12-14 13:57:24 +01:00
|
|
|
quit_with_function(function() core.restart_request = true end)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-04-06 17:50:46 +02:00
|
|
|
local function check_plugin_version(filename)
|
2021-04-25 12:35:00 +02:00
|
|
|
local info = system.get_file_info(filename)
|
|
|
|
if info ~= nil and info.type == "dir" then
|
2021-04-25 19:14:36 +02:00
|
|
|
filename = filename .. "/init.lua"
|
|
|
|
info = system.get_file_info(filename)
|
|
|
|
if not info then return true end
|
2021-04-25 12:35:00 +02:00
|
|
|
end
|
2021-04-06 17:50:46 +02:00
|
|
|
local f = io.open(filename, "r")
|
|
|
|
if not f then return false end
|
|
|
|
local version_match = false
|
|
|
|
for line in f:lines() do
|
2021-05-05 22:38:29 +02:00
|
|
|
local mod_version = line:match('%-%-.*%f[%a]mod%-version%s*:%s*(%d+)')
|
|
|
|
if mod_version then
|
|
|
|
version_match = (mod_version == MOD_VERSION)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
-- The following pattern is used for backward compatibility only
|
|
|
|
-- Future versions will look only at the mod-version tag.
|
|
|
|
local version = line:match('%-%-%s*lite%-xl%s*(%d+%.%d+)$')
|
|
|
|
if version then
|
|
|
|
-- we consider the version tag 1.16 equivalent to mod-version:1
|
|
|
|
version_match = (version == '1.16' and MOD_VERSION == "1")
|
|
|
|
break
|
|
|
|
end
|
2021-04-06 17:50:46 +02:00
|
|
|
end
|
|
|
|
f:close()
|
|
|
|
return version_match
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function core.load_plugins()
|
|
|
|
local no_errors = true
|
2021-04-07 09:42:19 +02:00
|
|
|
local refused_list = {
|
|
|
|
userdir = {dir = USERDIR, plugins = {}},
|
|
|
|
datadir = {dir = DATADIR, plugins = {}},
|
|
|
|
}
|
2021-03-03 12:20:22 +01:00
|
|
|
for _, root_dir in ipairs {USERDIR, DATADIR} do
|
2021-03-20 16:34:00 +01:00
|
|
|
local plugin_dir = root_dir .. "/plugins"
|
|
|
|
local files = system.list_dir(plugin_dir)
|
2021-03-05 07:22:56 +01:00
|
|
|
for _, filename in ipairs(files or {}) do
|
2021-04-25 12:35:00 +02:00
|
|
|
local basename = filename:match("(.-)%.lua$") or filename
|
|
|
|
local version_match = check_plugin_version(plugin_dir .. '/' .. filename)
|
|
|
|
if not version_match then
|
|
|
|
core.log_quiet("Version mismatch for plugin %q from %s", basename, plugin_dir)
|
|
|
|
local ls = refused_list[root_dir == USERDIR and 'userdir' or 'datadir'].plugins
|
|
|
|
ls[#ls + 1] = filename
|
|
|
|
end
|
|
|
|
if version_match and config[basename] ~= false then
|
|
|
|
local modname = "plugins." .. basename
|
|
|
|
local ok = core.try(require, modname)
|
|
|
|
if ok then core.log_quiet("Loaded plugin %q from %s", basename, plugin_dir) end
|
|
|
|
if not ok then
|
|
|
|
no_errors = false
|
2021-01-13 14:50:24 +01:00
|
|
|
end
|
2020-12-06 11:23:33 +01:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
2021-04-07 09:42:19 +02:00
|
|
|
return no_errors, refused_list
|
2019-12-28 12:16:32 +01:00
|
|
|
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-11-16 23:52:39 +01:00
|
|
|
function core.set_visited(filename)
|
2021-05-13 14:57:55 +02:00
|
|
|
filename = common.home_encode(filename)
|
2020-11-16 23:52:39 +01:00
|
|
|
for i = 1, #core.visited_files do
|
|
|
|
if core.visited_files[i] == filename then
|
|
|
|
table.remove(core.visited_files, i)
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
table.insert(core.visited_files, 1, filename)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-05-19 13:58:41 +02:00
|
|
|
function core.set_active_view(view)
|
|
|
|
assert(view, "Tried to set active view to nil")
|
2021-03-13 12:57:52 +01:00
|
|
|
if core.active_view and core.active_view.force_focus then return end
|
2020-05-19 13:58:41 +02:00
|
|
|
if view ~= core.active_view then
|
2020-11-16 23:52:39 +01:00
|
|
|
if view.doc and view.doc.filename then
|
|
|
|
core.set_visited(view.doc.filename)
|
|
|
|
end
|
2020-05-19 13:58:41 +02:00
|
|
|
core.last_active_view = core.active_view
|
|
|
|
core.active_view = view
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-04-12 19:05:30 +02:00
|
|
|
function core.show_title_bar(show)
|
|
|
|
core.title_view.visible = show
|
|
|
|
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 }
|
2020-12-09 15:41:25 +01:00
|
|
|
return key
|
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
|
|
|
|
|
2021-05-10 15:08:56 +02:00
|
|
|
-- The function below works like system.absolute_path except it
|
|
|
|
-- doesn't fail if the file does not exist. We consider that the
|
2021-05-13 14:57:55 +02:00
|
|
|
-- current dir is core.working_dir so relative filename are considered
|
|
|
|
-- to be in core.working_dir.
|
2021-05-10 15:08:56 +02:00
|
|
|
-- Please note that .. or . in the filename are not taken into account.
|
|
|
|
-- This function should get only filenames normalized using
|
|
|
|
-- common.normalize_path function.
|
2021-05-13 12:07:56 +02:00
|
|
|
function core.working_dir_absolute_path(filename)
|
2021-05-13 14:57:55 +02:00
|
|
|
if filename:match('^%a:\\') or filename:find('/', 1, true) == 1 then
|
2021-05-10 15:08:56 +02:00
|
|
|
return filename
|
|
|
|
else
|
2021-05-13 12:07:56 +02:00
|
|
|
return core.working_dir .. PATHSEP .. filename
|
2021-05-10 15:08:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-05-13 12:07:56 +02:00
|
|
|
function core.normalize_to_working_dir(filename)
|
|
|
|
filename = common.normalize_path(filename)
|
|
|
|
return core.working_dir_absolute_path(filename)
|
|
|
|
end
|
|
|
|
|
2021-05-10 15:08:56 +02:00
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function core.open_doc(filename)
|
2021-05-10 15:08:56 +02:00
|
|
|
local new_file = not filename or not system.get_file_info(filename)
|
2019-12-28 12:16:32 +01:00
|
|
|
if filename then
|
2021-05-10 15:08:56 +02:00
|
|
|
-- normalize filename and set absolute filename then
|
2019-12-28 12:16:32 +01:00
|
|
|
-- try to find existing doc for filename
|
2021-05-13 12:07:56 +02:00
|
|
|
filename = core.normalize_to_working_dir(filename)
|
2019-12-28 12:16:32 +01:00
|
|
|
for _, doc in ipairs(core.docs) do
|
2021-05-13 12:07:56 +02:00
|
|
|
if doc.filename and filename == doc.filename then
|
2019-12-28 12:16:32 +01:00
|
|
|
return doc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- no existing doc for filename; create new
|
2021-05-13 12:07:56 +02:00
|
|
|
local doc = Doc(filename, new_file)
|
2019-12-28 12:16:32 +01:00
|
|
|
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(...)
|
2021-04-12 13:31:32 +02:00
|
|
|
elseif type == "resized" then
|
|
|
|
core.window_mode = system.get_window_mode()
|
|
|
|
elseif type == "minimized" or type == "maximized" or type == "restored" then
|
|
|
|
core.window_mode = type == "restored" and "normal" or type
|
2019-12-28 12:16:32 +01:00
|
|
|
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
|
2020-11-21 16:36:13 +01:00
|
|
|
elseif type == "focuslost" then
|
|
|
|
core.root_view:on_focus_lost(...)
|
2019-12-28 12:16:32 +01:00
|
|
|
elseif type == "quit" then
|
|
|
|
core.quit()
|
|
|
|
end
|
|
|
|
return did_keymap
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-02-18 00:26:19 +01:00
|
|
|
local function get_title_filename(view)
|
|
|
|
local doc_filename = view.get_filename and view:get_filename() or view:get_name()
|
|
|
|
return (doc_filename ~= "---") and doc_filename or ""
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-04-10 19:35:57 +02:00
|
|
|
function core.compose_window_title(title)
|
2021-02-28 18:55:05 +01:00
|
|
|
return title == "" and "Lite XL" or title .. " - Lite XL"
|
2021-02-18 00:26:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function core.step()
|
|
|
|
-- handle events
|
|
|
|
local did_keymap = false
|
|
|
|
local mouse_moved = false
|
|
|
|
local mouse = { x = 0, y = 0, dx = 0, dy = 0 }
|
2021-03-03 12:20:22 +01:00
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
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
|
2021-02-18 00:26:19 +01:00
|
|
|
local current_title = get_title_filename(core.active_view)
|
|
|
|
if current_title ~= core.window_title then
|
2021-04-10 19:35:57 +02:00
|
|
|
system.set_window_title(core.compose_window_title(current_title))
|
2021-02-18 00:26:19 +01:00
|
|
|
core.window_title = current_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
|
2020-06-16 14:54:08 +02:00
|
|
|
local need_more_work = false
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
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
|
2020-06-16 14:54:08 +02:00
|
|
|
else
|
|
|
|
need_more_work = true
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- stop running threads if we're about to hit the end of frame
|
|
|
|
if system.get_time() - core.frame_start > max_time then
|
2020-06-16 14:54:08 +02:00
|
|
|
coroutine.yield(true)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-06-16 14:54:08 +02:00
|
|
|
if not need_more_work then coroutine.yield(false) end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
|
|
function core.run()
|
2020-11-16 14:48:15 +01:00
|
|
|
local idle_iterations = 0
|
2019-12-28 12:16:32 +01:00
|
|
|
while true do
|
|
|
|
core.frame_start = system.get_time()
|
2020-05-24 14:31:49 +02:00
|
|
|
local did_redraw = core.step()
|
2020-06-16 14:54:08 +02:00
|
|
|
local need_more_work = run_threads()
|
2020-12-10 12:44:01 +01:00
|
|
|
if core.restart_request then break end
|
2020-06-16 14:54:08 +02:00
|
|
|
if not did_redraw and not need_more_work then
|
2020-11-16 14:48:15 +01:00
|
|
|
idle_iterations = idle_iterations + 1
|
|
|
|
-- do not wait of events at idle_iterations = 1 to give a chance at core.step to run
|
|
|
|
-- and set "redraw" flag.
|
|
|
|
if idle_iterations > 1 then
|
2020-11-21 16:36:13 +01:00
|
|
|
if system.window_has_focus() then
|
|
|
|
-- keep running even with no events to make the cursor blinks
|
2021-04-17 23:57:04 +02:00
|
|
|
local t = system.get_time() - core.blink_start
|
|
|
|
local h = config.blink_period / 2
|
|
|
|
local dt = math.ceil(t / h) * h - t
|
|
|
|
system.wait_event(dt + 1 / config.fps)
|
2020-11-21 16:36:13 +01:00
|
|
|
else
|
|
|
|
system.wait_event()
|
|
|
|
end
|
2020-11-16 14:48:15 +01:00
|
|
|
end
|
|
|
|
else
|
|
|
|
idle_iterations = 0
|
2020-12-09 15:41:25 +01:00
|
|
|
local elapsed = system.get_time() - core.frame_start
|
2021-04-17 23:57:04 +02:00
|
|
|
system.sleep(math.max(0, 1 / config.fps - elapsed))
|
2020-05-24 14:31:49 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-04-17 23:57:04 +02:00
|
|
|
function core.blink_reset()
|
|
|
|
core.blink_start = system.get_time()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function core.on_error(err)
|
|
|
|
-- write error to file
|
2021-03-05 23:46:41 +01:00
|
|
|
local fp = io.open(USERDIR .. "/error.txt", "wb")
|
2019-12-28 12:16:32 +01:00
|
|
|
fp:write("Error: " .. tostring(err) .. "\n")
|
2021-03-07 09:48:44 +01:00
|
|
|
fp:write(debug.traceback(nil, 4) .. "\n")
|
2019-12-28 12:16:32 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2021-02-17 20:10:39 +01:00
|
|
|
core.add_save_hook(function(filename)
|
|
|
|
local doc = core.active_view.doc
|
2021-05-13 12:07:56 +02:00
|
|
|
if doc and doc:is(Doc) and doc.filename == USERDIR .. PATHSEP .. "init.lua" then
|
2021-02-17 20:10:39 +01:00
|
|
|
core.reload_module("core.style")
|
|
|
|
core.load_user_directory()
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
2021-02-18 09:02:57 +01:00
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
return core
|