2019-12-28 12:16:32 +01:00
|
|
|
require "core.strict"
|
2021-06-02 21:27:00 +02:00
|
|
|
require "core.regex"
|
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")
|
2021-09-07 22:31:36 +02:00
|
|
|
return ok and t or {}
|
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())),
|
2021-05-27 22:01:25 +02:00
|
|
|
", window_mode=", common.serialize(system.get_window_mode()),
|
2021-09-07 22:31:36 +02:00
|
|
|
", previous_find=", common.serialize(core.previous_find),
|
|
|
|
", previous_replace=", common.serialize(core.previous_replace),
|
2020-12-31 11:25:12 +01:00
|
|
|
"}\n")
|
|
|
|
fp:close()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-02-19 00:14:34 +01:00
|
|
|
local function update_recents_project(action, dir_path_abs)
|
2021-10-12 16:08:06 +02:00
|
|
|
local dirname = common.normalize_volume(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-01-10 11:50:15 +01:00
|
|
|
function core.set_project_dir(new_dir, change_project_fn)
|
|
|
|
local chdir_ok = pcall(system.chdir, new_dir)
|
|
|
|
if chdir_ok then
|
|
|
|
if change_project_fn then change_project_fn() end
|
2021-10-12 16:08:06 +02:00
|
|
|
core.project_dir = common.normalize_volume(new_dir)
|
2021-01-10 11:50:15 +01:00
|
|
|
core.project_directories = {}
|
|
|
|
end
|
2021-12-30 23:57:23 +01:00
|
|
|
return chdir_ok
|
2020-12-27 11:32:52 +01:00
|
|
|
end
|
|
|
|
|
2021-12-20 14:42:48 +01:00
|
|
|
function core.close_current_project()
|
|
|
|
-- When using system.unwatch_dir we need to pass the watch_id provided by dmon.
|
|
|
|
-- In reality when unwatching a directory the dmon library shifts the other watch_id
|
|
|
|
-- values so the actual watch_id changes. To workaround this problem we assume the
|
|
|
|
-- first watch_id is always 1 and the watch_id are continguous and we unwatch the
|
|
|
|
-- first watch_id repeateadly up to the number of watch_ids.
|
|
|
|
local watch_id_max = 0
|
|
|
|
for _, project_dir in ipairs(core.project_directories) do
|
|
|
|
if project_dir.watch_id and project_dir.watch_id > watch_id_max then
|
|
|
|
watch_id_max = project_dir.watch_id
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for i = 1, watch_id_max do
|
|
|
|
system.unwatch_dir(1)
|
|
|
|
end
|
|
|
|
end
|
2020-12-27 11:32:52 +01:00
|
|
|
|
2021-12-30 23:57:23 +01:00
|
|
|
|
|
|
|
local function reload_customizations()
|
2022-01-10 09:54:47 +01:00
|
|
|
-- The logic is:
|
|
|
|
-- - the core.style and config modules are reloaded with the purpose of applying
|
|
|
|
-- the new user's and project's module configs
|
|
|
|
-- - inside the core.config the existing fields in config.plugins are preserved
|
|
|
|
-- because they are reserved to plugins configuration and plugins are already
|
|
|
|
-- loaded.
|
|
|
|
-- - plugins are not reloaded or unloaded
|
2021-12-31 00:20:52 +01:00
|
|
|
local plugins_save = {}
|
|
|
|
for k, v in pairs(config.plugins) do
|
|
|
|
plugins_save[k] = v
|
|
|
|
end
|
2022-01-10 09:54:47 +01:00
|
|
|
core.reload_module("core.style")
|
2021-12-31 00:20:52 +01:00
|
|
|
core.reload_module("core.config")
|
2022-01-10 09:54:47 +01:00
|
|
|
core.load_user_directory()
|
|
|
|
core.load_project_module()
|
2021-12-31 00:20:52 +01:00
|
|
|
for k, v in pairs(plugins_save) do
|
|
|
|
config.plugins[k] = v
|
|
|
|
end
|
2020-12-27 11:32:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-02-03 07:39:35 +01:00
|
|
|
function core.open_folder_project(dir_path_abs)
|
2021-02-05 15:36:48 +01:00
|
|
|
if core.set_project_dir(dir_path_abs, core.on_quit_project) then
|
2021-01-10 11:50:15 +01:00
|
|
|
core.root_view:close_all_docviews()
|
2021-12-30 23:57:23 +01:00
|
|
|
reload_customizations()
|
2021-02-19 00:14:34 +01:00
|
|
|
update_recents_project("add", dir_path_abs)
|
2021-12-30 23:57:23 +01:00
|
|
|
core.add_project_directory(dir_path_abs)
|
2021-02-03 07:39:35 +01:00
|
|
|
core.on_enter_project(dir_path_abs)
|
2021-01-10 11:50:15 +01:00
|
|
|
end
|
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
|
|
|
|
2021-05-25 23:25:56 +02:00
|
|
|
local function compare_file(a, b)
|
|
|
|
return a.filename < b.filename
|
|
|
|
end
|
|
|
|
|
2021-07-12 18:21:27 +02:00
|
|
|
|
2022-01-03 23:43:12 +01:00
|
|
|
-- inspect config.ignore_files patterns and prepare ready to use entries.
|
2022-01-03 17:49:15 +01:00
|
|
|
local function compile_ignore_files()
|
|
|
|
local ipatterns = config.ignore_files
|
|
|
|
local compiled = {}
|
|
|
|
-- config.ignore_files could be a simple string...
|
|
|
|
if type(ipatterns) ~= "table" then ipatterns = {ipatterns} end
|
|
|
|
for i, pattern in ipairs(ipatterns) do
|
2022-01-25 14:14:56 +01:00
|
|
|
-- we ignore malformed pattern that raise an error
|
|
|
|
if pcall(string.match, "a", pattern) then
|
|
|
|
table.insert(compiled, {
|
|
|
|
use_path = pattern:match("/[^/$]"), -- contains a slash but not at the end
|
|
|
|
-- An '/' or '/$' at the end means we want to match a directory.
|
|
|
|
match_dir = pattern:match(".+/%$?$"), -- to be used as a boolen value
|
|
|
|
pattern = pattern -- get the actual pattern
|
|
|
|
})
|
|
|
|
end
|
2022-01-03 17:49:15 +01:00
|
|
|
end
|
|
|
|
return compiled
|
|
|
|
end
|
2022-01-03 15:05:57 +01:00
|
|
|
|
|
|
|
|
2022-01-03 17:49:15 +01:00
|
|
|
local function fileinfo_pass_filter(info, ignore_compiled)
|
2022-01-03 15:05:57 +01:00
|
|
|
if info.size >= config.file_size_limit * 1e6 then return false end
|
2021-12-30 22:24:43 +01:00
|
|
|
local basename = common.basename(info.filename)
|
2022-01-03 15:05:57 +01:00
|
|
|
-- replace '\' with '/' for Windows where PATHSEP = '\'
|
|
|
|
local fullname = "/" .. info.filename:gsub("\\", "/")
|
2022-01-03 17:49:15 +01:00
|
|
|
for _, compiled in ipairs(ignore_compiled) do
|
2022-01-13 16:43:37 +01:00
|
|
|
local test = compiled.use_path and fullname or basename
|
|
|
|
if compiled.match_dir then
|
|
|
|
if info.type == "dir" and string.match(test .. "/", compiled.pattern) then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if string.match(test, compiled.pattern) then
|
|
|
|
return false
|
|
|
|
end
|
2022-01-03 15:05:57 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
return true
|
2021-12-30 22:24:43 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-07-12 18:21:27 +02:00
|
|
|
-- compute a file's info entry completed with "filename" to be used
|
|
|
|
-- in project scan or falsy if it shouldn't appear in the list.
|
2022-01-03 17:49:15 +01:00
|
|
|
local function get_project_file_info(root, file, ignore_compiled)
|
2021-07-12 18:21:27 +02:00
|
|
|
local info = system.get_file_info(root .. file)
|
2022-01-05 23:32:26 +01:00
|
|
|
-- info can be not nil but info.type may be nil if is neither a file neither
|
|
|
|
-- a directory, for example for /dev/* entries on linux.
|
|
|
|
if info and info.type then
|
2021-07-12 18:21:27 +02:00
|
|
|
info.filename = strip_leading_path(file)
|
2022-01-03 17:49:15 +01:00
|
|
|
return fileinfo_pass_filter(info, ignore_compiled) and info
|
2021-07-12 18:21:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-10-21 11:07:37 +02:00
|
|
|
-- Predicate function to inhibit directory recursion in get_directory_files
|
|
|
|
-- based on a time limit and the number of files.
|
|
|
|
local function timed_max_files_pred(dir, filename, entries_count, t_elapsed)
|
|
|
|
local n_limit = entries_count <= config.max_project_files
|
|
|
|
local t_limit = t_elapsed < 20 / config.fps
|
|
|
|
return n_limit and t_limit and core.project_subdir_is_shown(dir, filename)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-05-25 23:25:56 +02: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 '/'.
|
2022-01-03 17:49:15 +01:00
|
|
|
local function get_directory_files(dir, root, path, t, ignore_compiled, entries_count, recurse_pred, begin_hook)
|
2021-05-25 23:25:56 +02:00
|
|
|
if begin_hook then begin_hook() end
|
2022-01-03 17:49:15 +01:00
|
|
|
ignore_compiled = ignore_compiled or compile_ignore_files()
|
2021-10-21 11:07:37 +02:00
|
|
|
local t0 = system.get_time()
|
2021-05-25 23:25:56 +02:00
|
|
|
local all = system.list_dir(root .. path) or {}
|
2021-10-21 11:07:37 +02:00
|
|
|
local t_elapsed = system.get_time() - t0
|
2021-05-25 23:25:56 +02:00
|
|
|
local dirs, files = {}, {}
|
|
|
|
|
|
|
|
for _, file in ipairs(all) do
|
2022-01-03 17:49:15 +01:00
|
|
|
local info = get_project_file_info(root, path .. PATHSEP .. file, ignore_compiled)
|
2021-07-12 18:21:27 +02:00
|
|
|
if info then
|
|
|
|
table.insert(info.type == "dir" and dirs or files, info)
|
|
|
|
entries_count = entries_count + 1
|
2021-05-25 23:25:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-10-21 11:07:37 +02:00
|
|
|
local recurse_complete = true
|
2021-05-25 23:25:56 +02:00
|
|
|
table.sort(dirs, compare_file)
|
|
|
|
for _, f in ipairs(dirs) do
|
|
|
|
table.insert(t, f)
|
2021-10-21 11:07:37 +02:00
|
|
|
if recurse_pred(dir, f.filename, entries_count, t_elapsed) then
|
2022-01-03 17:49:15 +01:00
|
|
|
local _, complete, n = get_directory_files(dir, root, PATHSEP .. f.filename, t, ignore_compiled, entries_count, recurse_pred, begin_hook)
|
2021-10-21 11:07:37 +02:00
|
|
|
recurse_complete = recurse_complete and complete
|
|
|
|
entries_count = n
|
|
|
|
else
|
|
|
|
recurse_complete = false
|
2021-05-25 23:25:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
table.sort(files, compare_file)
|
|
|
|
for _, f in ipairs(files) do
|
|
|
|
table.insert(t, f)
|
|
|
|
end
|
|
|
|
|
2021-10-21 11:07:37 +02:00
|
|
|
return t, recurse_complete, entries_count
|
2021-05-25 23:25:56 +02:00
|
|
|
end
|
|
|
|
|
2021-07-12 18:21:27 +02:00
|
|
|
|
|
|
|
function core.project_subdir_set_show(dir, filename, show)
|
2021-12-29 08:19:44 +01:00
|
|
|
if dir.files_limit and not dir.force_rescan then
|
2021-07-12 18:21:27 +02:00
|
|
|
local fullpath = dir.name .. PATHSEP .. filename
|
2022-01-06 00:10:14 +01:00
|
|
|
if not (show and system.watch_dir_add or system.watch_dir_rm)(dir.watch_id, fullpath) then
|
|
|
|
return false
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
2022-01-06 00:10:14 +01:00
|
|
|
dir.shown_subdir[filename] = show
|
|
|
|
return true
|
2021-07-12 18:21:27 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2021-07-12 18:21:27 +02:00
|
|
|
|
|
|
|
function core.project_subdir_is_shown(dir, filename)
|
|
|
|
return not dir.files_limit or dir.shown_subdir[filename]
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-10-21 11:07:37 +02:00
|
|
|
local function show_max_files_warning(dir)
|
|
|
|
local message = dir.slow_filesystem and
|
|
|
|
"Filesystem is too slow: project files will not be indexed." or
|
2021-07-12 18:21:27 +02:00
|
|
|
"Too many files in project directory: stopped reading at "..
|
|
|
|
config.max_project_files.." files. For more information see "..
|
2021-12-19 18:13:19 +01:00
|
|
|
"usage.md at github.com/lite-xl/lite-xl."
|
2021-10-21 11:07:37 +02:00
|
|
|
core.status_view:show_message("!", style.accent, message)
|
2021-07-12 18:21:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function file_search(files, info)
|
|
|
|
local filename, type = info.filename, info.type
|
|
|
|
local inf, sup = 1, #files
|
|
|
|
while sup - inf > 8 do
|
|
|
|
local curr = math.floor((inf + sup) / 2)
|
|
|
|
if system.path_compare(filename, type, files[curr].filename, files[curr].type) then
|
|
|
|
sup = curr - 1
|
|
|
|
else
|
|
|
|
inf = curr
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2021-07-12 18:21:27 +02:00
|
|
|
end
|
2021-11-26 13:45:13 +01:00
|
|
|
while inf <= sup and not system.path_compare(filename, type, files[inf].filename, files[inf].type) do
|
2021-07-12 18:21:27 +02:00
|
|
|
if files[inf].filename == filename then
|
|
|
|
return inf, true
|
|
|
|
end
|
|
|
|
inf = inf + 1
|
2021-11-26 13:45:13 +01:00
|
|
|
end
|
2021-07-12 18:21:27 +02:00
|
|
|
return inf, false
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
|
2021-07-12 18:21:27 +02:00
|
|
|
local function files_info_equal(a, b)
|
|
|
|
return a.filename == b.filename and a.type == b.type
|
|
|
|
end
|
|
|
|
|
|
|
|
-- for "a" inclusive from i1 + 1 and i1 + n
|
|
|
|
local function files_list_match(a, i1, n, b)
|
|
|
|
if n ~= #b then return false end
|
|
|
|
for i = 1, n do
|
|
|
|
if not files_info_equal(a[i1 + i], b[i]) then
|
|
|
|
return false
|
2021-06-17 19:07:32 +02:00
|
|
|
end
|
|
|
|
end
|
2021-07-12 18:21:27 +02:00
|
|
|
return true
|
2021-06-17 19:07:32 +02:00
|
|
|
end
|
|
|
|
|
2021-07-12 18:21:27 +02:00
|
|
|
-- arguments like for files_list_match
|
2022-01-10 16:26:39 +01:00
|
|
|
local function files_list_replace(as, i1, n, bs, hook)
|
2021-07-12 18:21:27 +02:00
|
|
|
local m = #bs
|
|
|
|
local i, j = 1, 1
|
|
|
|
while i <= m or i <= n do
|
|
|
|
local a, b = as[i1 + i], bs[j]
|
|
|
|
if i > n or (j <= m and not files_info_equal(a, b) and
|
|
|
|
not system.path_compare(a.filename, a.type, b.filename, b.type))
|
|
|
|
then
|
|
|
|
table.insert(as, i1 + i, b)
|
|
|
|
i, j, n = i + 1, j + 1, n + 1
|
2022-01-10 16:26:39 +01:00
|
|
|
if hook and hook.insert then hook.insert(b) end
|
2021-07-12 18:21:27 +02:00
|
|
|
elseif j > m or system.path_compare(a.filename, a.type, b.filename, b.type) then
|
2022-01-10 16:26:39 +01:00
|
|
|
if hook and hook.remove then hook.remove(as[i1 + i]) end
|
2021-07-12 18:21:27 +02:00
|
|
|
table.remove(as, i1 + i)
|
|
|
|
n = n - 1
|
|
|
|
else
|
|
|
|
i, j = i + 1, j + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-06-17 19:07:32 +02:00
|
|
|
|
2022-01-10 16:26:39 +01:00
|
|
|
|
|
|
|
local function project_scan_add_entry(dir, fileinfo)
|
|
|
|
assert(not dir.force_rescan, "should be used only when force_rescan is false")
|
|
|
|
local index, match = file_search(dir.files, fileinfo)
|
|
|
|
if not match then
|
|
|
|
table.insert(dir.files, index, fileinfo)
|
|
|
|
if fileinfo.type == "dir" and not dir.files_limit then
|
|
|
|
-- ASSUMPTION: dir.force_rescan is FALSE
|
|
|
|
system.watch_dir_add(dir.watch_id, dir.name .. PATHSEP .. fileinfo.filename)
|
|
|
|
if fileinfo.symlink then
|
|
|
|
local new_files = get_directory_files(dir, dir.name, PATHSEP .. fileinfo.filename, {}, nil, 0, core.project_subdir_is_shown)
|
|
|
|
files_list_replace(dir.files, index, 0, new_files, {insert = function(info)
|
|
|
|
if info.type == "dir" then
|
|
|
|
system.watch_dir_add(dir.watch_id, dir.name .. PATHSEP .. info.filename)
|
|
|
|
end
|
|
|
|
end})
|
|
|
|
end
|
|
|
|
end
|
|
|
|
dir.is_dirty = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-07-12 18:21:27 +02:00
|
|
|
local function project_subdir_bounds(dir, filename)
|
|
|
|
local index, n = 0, #dir.files
|
|
|
|
for i, file in ipairs(dir.files) do
|
|
|
|
local file = dir.files[i]
|
|
|
|
if file.filename == filename then
|
|
|
|
index, n = i, #dir.files - i
|
|
|
|
for j = 1, #dir.files - i do
|
|
|
|
if not common.path_belongs_to(dir.files[i + j].filename, filename) then
|
|
|
|
n = j - 1
|
|
|
|
break
|
2021-05-25 23:25:56 +02:00
|
|
|
end
|
|
|
|
end
|
2021-07-12 18:21:27 +02:00
|
|
|
return index, n, file
|
2021-05-25 23:25:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-07-12 18:21:27 +02:00
|
|
|
local function rescan_project_subdir(dir, filename_rooted)
|
2022-01-03 17:49:15 +01:00
|
|
|
local new_files = get_directory_files(dir, dir.name, filename_rooted, {}, nil, 0, core.project_subdir_is_shown, coroutine.yield)
|
2021-07-12 18:21:27 +02:00
|
|
|
local index, n = 0, #dir.files
|
|
|
|
if filename_rooted ~= "" then
|
|
|
|
local filename = strip_leading_path(filename_rooted)
|
|
|
|
index, n = project_subdir_bounds(dir, filename)
|
|
|
|
end
|
2021-05-25 23:25:56 +02:00
|
|
|
|
2021-07-12 18:21:27 +02:00
|
|
|
if not files_list_match(dir.files, index, n, new_files) then
|
2021-12-29 08:19:44 +01:00
|
|
|
-- Since we are modifying the list of files we may add new directories and
|
|
|
|
-- when dir.files_limit is false we need to add a watch for each subdirectory.
|
|
|
|
-- We are therefore passing a insert hook function to the purpose of adding
|
|
|
|
-- a watch.
|
|
|
|
-- Note that the hook shold almost never be called, it happens only if
|
|
|
|
-- we missed some directory creation event from the directory monitoring which
|
|
|
|
-- almost never happens. With inotify is at least theoretically possible.
|
|
|
|
local need_subdir_watches = not dir.files_limit and not dir.force_rescan
|
2022-01-10 16:26:39 +01:00
|
|
|
files_list_replace(dir.files, index, n, new_files, need_subdir_watches and {insert = function(fileinfo)
|
2021-12-29 08:19:44 +01:00
|
|
|
if fileinfo.type == "dir" then
|
|
|
|
system.watch_dir_add(dir.watch_id, dir.name .. PATHSEP .. fileinfo.filename)
|
|
|
|
end
|
2022-01-10 16:26:39 +01:00
|
|
|
end})
|
2021-07-12 18:21:27 +02:00
|
|
|
dir.is_dirty = true
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-10-21 23:57:17 +02:00
|
|
|
local function add_dir_scan_thread(dir)
|
|
|
|
core.add_thread(function()
|
|
|
|
while true do
|
|
|
|
local has_changes = rescan_project_subdir(dir, "")
|
|
|
|
if has_changes then
|
|
|
|
core.redraw = true -- we run without an event, from a thread
|
|
|
|
end
|
|
|
|
coroutine.yield(5)
|
2021-05-25 23:25:56 +02:00
|
|
|
end
|
2022-01-08 17:15:09 +01:00
|
|
|
end, dir)
|
2021-10-21 23:57:17 +02:00
|
|
|
end
|
|
|
|
|
2021-12-29 08:19:44 +01:00
|
|
|
|
|
|
|
local function folder_add_subdirs_watch(dir)
|
|
|
|
for _, fileinfo in ipairs(dir.files) do
|
|
|
|
if fileinfo.type == "dir" then
|
|
|
|
system.watch_dir_add(dir.watch_id, dir.name .. PATHSEP .. fileinfo.filename)
|
|
|
|
end
|
|
|
|
end
|
2021-10-21 23:57:17 +02:00
|
|
|
end
|
|
|
|
|
2021-12-29 08:19:44 +01:00
|
|
|
|
2021-10-21 23:57:17 +02:00
|
|
|
-- Populate a project folder top directory by scanning the filesystem.
|
|
|
|
local function scan_project_folder(index)
|
|
|
|
local dir = core.project_directories[index]
|
2021-12-29 08:19:44 +01:00
|
|
|
local fstype = system.get_fs_type(dir.name)
|
|
|
|
dir.force_rescan = (fstype == "nfs" or fstype == "fuse")
|
|
|
|
if not dir.force_rescan then
|
2022-01-08 10:55:43 +01:00
|
|
|
local watch_err
|
|
|
|
dir.watch_id, watch_err = system.watch_dir(dir.name)
|
|
|
|
if not dir.watch_id then
|
|
|
|
core.log("Watch directory %s: %s", dir.name, watch_err)
|
|
|
|
dir.force_rescan = true
|
|
|
|
end
|
2021-10-21 23:57:17 +02:00
|
|
|
end
|
2022-01-03 17:49:15 +01:00
|
|
|
local t, complete, entries_count = get_directory_files(dir, dir.name, "", {}, nil, 0, timed_max_files_pred)
|
2021-12-29 08:19:44 +01:00
|
|
|
-- If dir.files_limit is set to TRUE it means that:
|
|
|
|
-- * we will not read recursively all the project files and we don't index them
|
|
|
|
-- * we read only the files for the subdirectories that are opened/expanded in the
|
|
|
|
-- TreeView
|
|
|
|
-- * we add a subdirectory watch only to the directories that are opened/expanded
|
|
|
|
-- * we set the values in the shown_subdir table
|
|
|
|
--
|
|
|
|
-- If dir.files_limit is set to FALSE it means that:
|
|
|
|
-- * we will read recursively all the project files and we index them
|
|
|
|
-- * all the list of project files is always complete and kept updated when
|
|
|
|
-- changes happen on the disk
|
|
|
|
-- * all the subdirectories at any depth must have a watch using system.watch_dir_add
|
|
|
|
-- * we DO NOT set the values in the shown_subdir table
|
|
|
|
--
|
|
|
|
-- * If force_rescan is set to TRUE no watch are used in any case.
|
2021-10-21 23:57:17 +02:00
|
|
|
if not complete then
|
|
|
|
dir.slow_filesystem = not complete and (entries_count <= config.max_project_files)
|
|
|
|
dir.files_limit = true
|
|
|
|
if core.status_view then -- May be not yet initialized.
|
|
|
|
show_max_files_warning(dir)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
dir.files = t
|
|
|
|
if dir.force_rescan then
|
|
|
|
add_dir_scan_thread(dir)
|
|
|
|
else
|
2021-12-29 08:19:44 +01:00
|
|
|
if not dir.files_limit then
|
|
|
|
folder_add_subdirs_watch(dir)
|
|
|
|
end
|
2021-10-21 23:57:17 +02:00
|
|
|
core.dir_rescan_add_job(dir, ".")
|
2021-05-25 23:25:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-10-21 23:57:17 +02:00
|
|
|
function core.add_project_directory(path)
|
|
|
|
-- 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.
|
|
|
|
path = common.normalize_volume(path)
|
|
|
|
local dir = {
|
|
|
|
name = path,
|
|
|
|
item = {filename = common.basename(path), type = "dir", topdir = true},
|
|
|
|
files_limit = false,
|
|
|
|
is_dirty = true,
|
|
|
|
shown_subdir = {},
|
|
|
|
}
|
|
|
|
table.insert(core.project_directories, dir)
|
|
|
|
scan_project_folder(#core.project_directories)
|
|
|
|
if path == core.project_dir then
|
|
|
|
core.project_files = dir.files
|
|
|
|
end
|
|
|
|
core.redraw = true
|
2021-12-28 12:32:25 +01:00
|
|
|
return dir
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- The function below is needed to reload the project directories
|
|
|
|
-- when the project's module changes.
|
|
|
|
local function rescan_project_directories()
|
|
|
|
local save_project_dirs = {}
|
|
|
|
local n = #core.project_directories
|
|
|
|
for i = 1, n do
|
|
|
|
local dir = core.project_directories[i]
|
|
|
|
save_project_dirs[i] = {name = dir.name, shown_subdir = dir.shown_subdir}
|
|
|
|
end
|
|
|
|
core.close_current_project() -- ensure we unwatch directories
|
|
|
|
core.project_directories = {}
|
|
|
|
for i = 1, n do -- add again the directories in the project
|
|
|
|
local dir = core.add_project_directory(save_project_dirs[i].name)
|
2021-12-29 08:19:44 +01:00
|
|
|
if dir.files_limit then
|
2022-01-06 18:00:15 +01:00
|
|
|
-- We need to sort the list of shown subdirectories so that higher level
|
|
|
|
-- directories are populated first. We use the function system.path_compare
|
|
|
|
-- because it order the entries in the appropriate order.
|
|
|
|
-- TODO: we may consider storing the table shown_subdir as a sorted table
|
|
|
|
-- since the beginning.
|
|
|
|
local subdir_list = {}
|
|
|
|
for subdir in pairs(save_project_dirs[i].shown_subdir) do
|
|
|
|
table.insert(subdir_list, subdir)
|
|
|
|
end
|
|
|
|
table.sort(subdir_list, function(a, b) return system.path_compare(a, "dir", b, "dir") end)
|
|
|
|
for _, subdir in ipairs(subdir_list) do
|
|
|
|
local show = save_project_dirs[i].shown_subdir[subdir]
|
2021-12-29 08:19:44 +01:00
|
|
|
for j = 1, #dir.files do
|
|
|
|
if dir.files[j].filename == subdir then
|
|
|
|
-- The instructions below match when happens in TreeView:on_mouse_pressed.
|
|
|
|
-- We perform the operations only once iff the subdir is in dir.files.
|
2022-01-06 18:00:15 +01:00
|
|
|
-- In theory set_show below may fail and return false but is it is listed
|
|
|
|
-- there it means it succeeded before so we are optimistically assume it
|
|
|
|
-- will not fail for the sake of simplicity.
|
2021-12-29 08:19:44 +01:00
|
|
|
core.project_subdir_set_show(dir, subdir, show)
|
2022-01-06 18:00:15 +01:00
|
|
|
core.update_project_subdir(dir, subdir, show)
|
2021-12-29 08:19:44 +01:00
|
|
|
break
|
|
|
|
end
|
2021-12-28 12:32:25 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-10-21 23:57:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-12-28 14:32:39 +01:00
|
|
|
function core.project_dir_by_name(name)
|
|
|
|
for i = 1, #core.project_directories do
|
|
|
|
if core.project_directories[i].name == name then
|
|
|
|
return core.project_directories[i]
|
|
|
|
end
|
|
|
|
end
|
2021-10-21 23:57:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-07-12 18:21:27 +02:00
|
|
|
function core.update_project_subdir(dir, filename, expanded)
|
2021-12-29 08:19:44 +01:00
|
|
|
assert(dir.files_limit, "function should be called only when directory is in files limit mode")
|
2021-07-12 18:21:27 +02:00
|
|
|
local index, n, file = project_subdir_bounds(dir, filename)
|
|
|
|
if index then
|
2022-01-03 17:49:15 +01:00
|
|
|
local new_files = expanded and get_directory_files(dir, dir.name, PATHSEP .. filename, {}, nil, 0, core.project_subdir_is_shown) or {}
|
2021-12-29 08:19:44 +01:00
|
|
|
-- ASSUMPTION: core.update_project_subdir is called only when dir.files_limit is true
|
|
|
|
-- NOTE: we may add new directories below but we do not need to call
|
|
|
|
-- system.watch_dir_add because the current function is called only
|
|
|
|
-- in dir.files_limit mode and in this latter case we don't need to
|
|
|
|
-- add watch to new, unfolded, subdirectories.
|
2021-07-12 18:21:27 +02:00
|
|
|
files_list_replace(dir.files, index, n, new_files)
|
|
|
|
dir.is_dirty = true
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- Find files and directories recursively reading from the filesystem.
|
|
|
|
-- Filter files and yields file's directory and info table. This latter
|
|
|
|
-- is filled to be like required by project directories "files" list.
|
|
|
|
local function find_files_rec(root, path)
|
2021-05-25 23:25:56 +02:00
|
|
|
local all = system.list_dir(root .. path) or {}
|
|
|
|
for _, file in ipairs(all) do
|
2021-07-12 18:21:27 +02:00
|
|
|
local file = path .. PATHSEP .. file
|
|
|
|
local info = system.get_file_info(root .. file)
|
|
|
|
if info then
|
|
|
|
info.filename = strip_leading_path(file)
|
|
|
|
if info.type == "file" then
|
|
|
|
coroutine.yield(root, info)
|
|
|
|
else
|
|
|
|
find_files_rec(root, PATHSEP .. info.filename)
|
2021-05-25 23:25:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-07-12 18:21:27 +02:00
|
|
|
-- Iterator function to list all project files
|
2020-12-27 11:32:52 +01:00
|
|
|
local function project_files_iter(state)
|
|
|
|
local dir = core.project_directories[state.dir_index]
|
2021-07-12 18:21:27 +02:00
|
|
|
if state.co then
|
|
|
|
-- We have a coroutine to fetch for files, use the coroutine.
|
|
|
|
-- Used for directories that exceeds the files nuumber limit.
|
|
|
|
local ok, name, file = coroutine.resume(state.co, dir.name, "")
|
|
|
|
if ok and name then
|
|
|
|
return name, file
|
|
|
|
else
|
|
|
|
-- The coroutine terminated, increment file/dir counter to scan
|
|
|
|
-- next project directory.
|
|
|
|
state.co = false
|
|
|
|
state.file_index = 1
|
|
|
|
state.dir_index = state.dir_index + 1
|
|
|
|
dir = core.project_directories[state.dir_index]
|
|
|
|
end
|
|
|
|
else
|
|
|
|
-- Increase file/dir counter
|
|
|
|
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
|
|
|
|
dir = core.project_directories[state.dir_index]
|
|
|
|
end
|
2020-12-27 11:32:52 +01:00
|
|
|
end
|
|
|
|
if not dir then return end
|
2021-07-12 18:21:27 +02:00
|
|
|
if dir.files_limit then
|
|
|
|
-- The current project directory is files limited: create a couroutine
|
|
|
|
-- to read files from the filesystem.
|
|
|
|
state.co = coroutine.create(find_files_rec)
|
|
|
|
return project_files_iter(state)
|
|
|
|
end
|
2020-12-27 11:32:52 +01:00
|
|
|
return dir.name, dir.files[state.file_index]
|
|
|
|
end
|
|
|
|
|
2020-12-27 18:28:21 +01:00
|
|
|
|
|
|
|
function core.get_project_files()
|
2021-07-12 18:21:27 +02:00
|
|
|
local state = { dir_index = 1, file_index = 0 }
|
|
|
|
return project_files_iter, state
|
2020-12-27 11:32:52 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2020-12-27 18:28:21 +01:00
|
|
|
function core.project_files_number()
|
2021-07-12 18:21:27 +02:00
|
|
|
local n = 0
|
|
|
|
for i = 1, #core.project_directories do
|
|
|
|
if core.project_directories[i].files_limit then return end
|
|
|
|
n = n + #core.project_directories[i].files
|
|
|
|
end
|
|
|
|
return n
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function project_dir_by_watch_id(watch_id)
|
|
|
|
for i = 1, #core.project_directories do
|
|
|
|
if core.project_directories[i].watch_id == watch_id then
|
|
|
|
return core.project_directories[i]
|
2021-05-25 23:25:56 +02:00
|
|
|
end
|
2021-07-12 18:21:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function project_scan_remove_file(dir, filepath)
|
|
|
|
local fileinfo = { filename = filepath }
|
|
|
|
for _, filetype in ipairs {"dir", "file"} do
|
|
|
|
fileinfo.type = filetype
|
|
|
|
local index, match = file_search(dir.files, fileinfo)
|
|
|
|
if match then
|
2022-01-10 16:26:39 +01:00
|
|
|
if filetype == "dir" then
|
|
|
|
-- If the directory is a symlink it may get deleted and we will
|
|
|
|
-- never get dirmonitor events for the removal the files it contains.
|
|
|
|
-- We proceed to remove all the files that belong to the directory.
|
|
|
|
local _, n_subdir = project_subdir_bounds(dir, filepath)
|
|
|
|
files_list_replace(dir.files, index, n_subdir, {}, {
|
|
|
|
remove= function(fileinfo)
|
|
|
|
if fileinfo.type == "dir" then
|
|
|
|
system.watch_dir_rm(dir.watch_id, dir.name .. PATHSEP .. filepath)
|
|
|
|
end
|
|
|
|
end})
|
|
|
|
if dir.files_limit then
|
|
|
|
dir.shown_subdir[filepath] = nil
|
|
|
|
end
|
2021-12-29 08:19:44 +01:00
|
|
|
end
|
2021-07-12 18:21:27 +02:00
|
|
|
table.remove(dir.files, index)
|
|
|
|
dir.is_dirty = true
|
|
|
|
return
|
2021-05-25 23:25:56 +02:00
|
|
|
end
|
2021-07-12 18:21:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function project_scan_add_file(dir, filepath)
|
2022-01-03 17:49:15 +01:00
|
|
|
local ignore = compile_ignore_files()
|
|
|
|
local fileinfo = get_project_file_info(dir.name, PATHSEP .. filepath, ignore)
|
2021-07-12 18:21:27 +02:00
|
|
|
if fileinfo then
|
2022-01-03 23:43:12 +01:00
|
|
|
-- on Windows and MacOS we can get events from directories we are not following:
|
|
|
|
-- check if each parent directories pass the ignore_files rules.
|
2022-01-03 18:50:43 +01:00
|
|
|
repeat
|
|
|
|
filepath = common.dirname(filepath)
|
2022-01-03 17:49:15 +01:00
|
|
|
local parent_info = filepath and get_project_file_info(dir.name, PATHSEP .. filepath, ignore)
|
2022-01-03 18:50:43 +01:00
|
|
|
if filepath and not parent_info then
|
2022-01-03 23:43:12 +01:00
|
|
|
return -- parent directory does match ignore_files rules: stop there
|
2022-01-03 18:50:43 +01:00
|
|
|
end
|
|
|
|
until not parent_info
|
2021-07-12 18:21:27 +02:00
|
|
|
project_scan_add_entry(dir, fileinfo)
|
2020-12-27 18:28:21 +01:00
|
|
|
end
|
|
|
|
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()
|
2021-06-13 19:50:42 +02:00
|
|
|
local success, err = common.mkdirp(USERDIR)
|
|
|
|
if not success then
|
|
|
|
error("cannot create directory \"" .. USERDIR .. "\": " .. err)
|
2020-12-06 11:23:33 +01:00
|
|
|
end
|
2021-02-19 16:10:02 +01:00
|
|
|
for _, modname in ipairs {'plugins', 'colors', 'fonts'} do
|
2021-06-13 19:50:42 +02:00
|
|
|
local subdirname = USERDIR .. PATHSEP .. modname
|
2020-12-06 11:23:33 +01:00
|
|
|
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-09-07 15:11:20 +02:00
|
|
|
-- style.font = renderer.font.load(DATADIR .. "/fonts/FiraSans-Regular.ttf", 14 * SCALE)
|
|
|
|
-- style.code_font = renderer.font.load(DATADIR .. "/fonts/JetBrainsMono-Regular.ttf", 14 * 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:
|
|
|
|
|
2021-07-20 21:09:14 +02:00
|
|
|
-- enable plugins.trimwhitespace, otherwise it is disable by default:
|
|
|
|
-- config.plugins.trimwhitespace = true
|
2021-01-13 14:50:24 +01:00
|
|
|
--
|
|
|
|
-- disable detectindent, otherwise it is enabled by default
|
2021-07-20 21:09:14 +02:00
|
|
|
-- config.plugins.detectindent = false
|
2020-12-02 16:03:31 +01:00
|
|
|
]])
|
|
|
|
init_file:close()
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-01-04 00:25:14 +01:00
|
|
|
function core.write_init_project_module(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 project's module settings here.
|
|
|
|
-- This module will be loaded when opening a project, after the user module
|
|
|
|
-- configuration.
|
|
|
|
-- It will be automatically reloaded when saved.
|
|
|
|
|
|
|
|
local config = require "core.config"
|
|
|
|
|
|
|
|
-- you can add some patterns to ignore files within the project
|
|
|
|
-- config.ignore_files = {"^%.", <some-patterns>}
|
|
|
|
|
|
|
|
-- Patterns are normally applied to the file's or directory's name, without
|
2022-01-13 16:43:37 +01:00
|
|
|
-- its path. See below about how to apply filters on a path.
|
2022-01-04 00:25:14 +01:00
|
|
|
--
|
|
|
|
-- Here some examples:
|
|
|
|
--
|
|
|
|
-- "^%." match any file of directory whose basename begins with a dot.
|
|
|
|
--
|
2022-01-13 16:43:37 +01:00
|
|
|
-- When there is an '/' or a '/$' at the end the pattern it will only match
|
|
|
|
-- directories. When using such a pattern a final '/' will be added to the name
|
|
|
|
-- of any directory entry before checking if it matches.
|
2022-01-04 00:25:14 +01:00
|
|
|
--
|
2022-01-13 16:43:37 +01:00
|
|
|
-- "^%.git/" matches any directory named ".git" anywhere in the project.
|
2022-01-04 00:25:14 +01:00
|
|
|
--
|
2022-01-13 16:43:37 +01:00
|
|
|
-- If a "/" appears anywhere in the pattern except if it appears at the end or
|
|
|
|
-- is immediately followed by a '$' then the pattern will be applied to the full
|
|
|
|
-- path of the file or directory. An initial "/" will be prepended to the file's
|
|
|
|
-- or directory's path to indicate the project's root.
|
2022-01-04 00:25:14 +01:00
|
|
|
--
|
2022-01-13 16:43:37 +01:00
|
|
|
-- "^/node_modules/" will match a directory named "node_modules" at the project's root.
|
|
|
|
-- "^/build.*/" match any top level directory whose name begins with "build"
|
2022-01-04 00:25:14 +01:00
|
|
|
-- "^/subprojects/.+/" match any directory inside a top-level folder named "subprojects".
|
|
|
|
|
|
|
|
-- You may activate some plugins on a pre-project base to override the user's settings.
|
|
|
|
-- config.plugins.trimwitespace = true
|
|
|
|
]])
|
|
|
|
init_file:close()
|
|
|
|
end
|
|
|
|
|
2020-12-02 16:03:31 +01:00
|
|
|
|
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
|
|
|
|
|
2020-12-28 15:51:16 +01:00
|
|
|
|
|
|
|
function core.remove_project_directory(path)
|
2021-01-03 12:02:07 +01:00
|
|
|
-- skip the fist directory because it is the project's directory
|
|
|
|
for i = 2, #core.project_directories do
|
|
|
|
local dir = core.project_directories[i]
|
2020-12-28 15:51:16 +01:00
|
|
|
if dir.name == path then
|
|
|
|
table.remove(core.project_directories, i)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-12-28 12:32:25 +01:00
|
|
|
local function configure_borderless_window()
|
|
|
|
system.set_window_bordered(not config.borderless)
|
|
|
|
core.title_view:configure_hit_test(config.borderless)
|
|
|
|
core.title_view.visible = config.borderless
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
local function add_config_files_hooks()
|
2021-05-31 09:41:37 +02:00
|
|
|
-- auto-realod style when user's module is saved by overriding Doc:Save()
|
|
|
|
local doc_save = Doc.save
|
|
|
|
local user_filename = system.absolute_path(USERDIR .. PATHSEP .. "init.lua")
|
2021-06-02 19:09:55 +02:00
|
|
|
function Doc:save(filename, abs_filename)
|
2022-01-24 09:31:40 +01:00
|
|
|
local module_filename = system.absolute_path(".lite_project.lua")
|
2021-06-02 19:09:55 +02:00
|
|
|
doc_save(self, filename, abs_filename)
|
2022-01-24 09:31:40 +01:00
|
|
|
if self.abs_filename == user_filename or self.abs_filename == module_filename then
|
2021-12-28 12:32:25 +01:00
|
|
|
reload_customizations()
|
2021-12-30 23:57:23 +01:00
|
|
|
rescan_project_directories()
|
|
|
|
configure_borderless_window()
|
2021-05-31 09:41:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
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
|
2021-10-12 16:08:06 +02:00
|
|
|
USERDIR = common.normalize_volume(USERDIR)
|
|
|
|
DATADIR = common.normalize_volume(DATADIR)
|
|
|
|
EXEDIR = common.normalize_volume(EXEDIR)
|
2021-02-18 09:02:57 +01:00
|
|
|
end
|
|
|
|
|
2020-12-31 11:25:12 +01:00
|
|
|
do
|
2021-09-07 22:31:36 +02:00
|
|
|
local session = load_session()
|
|
|
|
if session.window_mode == "normal" then
|
|
|
|
system.set_window_size(table.unpack(session.window))
|
|
|
|
elseif session.window_mode == "maximized" then
|
2021-05-27 22:01:25 +02:00
|
|
|
system.set_window_mode("maximized")
|
2020-12-31 11:25:12 +01:00
|
|
|
end
|
2021-09-07 22:31:36 +02:00
|
|
|
core.recent_projects = session.recents or {}
|
|
|
|
core.previous_find = session.previous_find or {}
|
|
|
|
core.previous_replace = session.previous_replace or {}
|
2020-12-31 11:25:12 +01:00
|
|
|
end
|
2020-12-08 16:57:24 +01:00
|
|
|
|
2020-12-08 18:49:58 +01:00
|
|
|
local project_dir = core.recent_projects[1] or "."
|
2021-02-19 00:14:34 +01:00
|
|
|
local project_dir_explicit = false
|
2020-05-26 11:17:36 +02:00
|
|
|
local files = {}
|
2021-02-19 09:29:46 +01:00
|
|
|
local delayed_error
|
2020-05-26 11:17:36 +02:00
|
|
|
for i = 2, #ARGS do
|
2021-01-10 12:56:15 +01:00
|
|
|
local arg_filename = strip_trailing_slash(ARGS[i])
|
|
|
|
local info = system.get_file_info(arg_filename) or {}
|
2020-05-26 11:17:36 +02:00
|
|
|
if info.type == "file" then
|
2021-01-10 12:56:15 +01:00
|
|
|
local file_abs = system.absolute_path(arg_filename)
|
2021-01-10 11:50:15 +01:00
|
|
|
if file_abs then
|
|
|
|
table.insert(files, file_abs)
|
|
|
|
project_dir = file_abs:match("^(.+)[/\\].+$")
|
|
|
|
end
|
2020-05-26 11:17:36 +02:00
|
|
|
elseif info.type == "dir" then
|
2021-01-10 12:56:15 +01:00
|
|
|
project_dir = arg_filename
|
2021-02-19 00:14:34 +01:00
|
|
|
project_dir_explicit = true
|
|
|
|
else
|
2021-09-07 15:44:15 +02:00
|
|
|
-- on macOS we can get an argument like "-psn_0_52353" that we just ignore.
|
|
|
|
if not ARGS[i]:match("^-psn") then
|
|
|
|
delayed_error = string.format("error: invalid file or directory %q", ARGS[i])
|
|
|
|
end
|
2020-05-26 11:17:36 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
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 = {}
|
2021-12-08 13:34:10 +01:00
|
|
|
core.cursor_clipboard = {}
|
|
|
|
core.cursor_clipboard_whole_line = {}
|
2021-04-12 13:31:32 +02:00
|
|
|
core.window_mode = "normal"
|
2019-12-28 12:16:32 +01:00
|
|
|
core.threads = setmetatable({}, { __mode = "k" })
|
2021-04-17 23:57:04 +02:00
|
|
|
core.blink_start = system.get_time()
|
|
|
|
core.blink_timer = core.blink_start
|
2020-12-28 10:26:27 +01:00
|
|
|
|
2021-01-10 11:50:15 +01:00
|
|
|
local project_dir_abs = system.absolute_path(project_dir)
|
2021-12-28 10:59:01 +01:00
|
|
|
-- We prevent set_project_dir below to effectively add and scan the directory becaese tha
|
|
|
|
-- project module and its ignore files is not yet loaded.
|
2021-02-19 00:14:34 +01:00
|
|
|
local set_project_ok = project_dir_abs and core.set_project_dir(project_dir_abs)
|
2021-01-10 11:50:15 +01:00
|
|
|
if set_project_ok then
|
2021-02-19 00:14:34 +01:00
|
|
|
if project_dir_explicit then
|
|
|
|
update_recents_project("add", project_dir_abs)
|
|
|
|
end
|
2021-01-10 11:50:15 +01:00
|
|
|
else
|
2021-02-19 00:14:34 +01:00
|
|
|
if not project_dir_explicit then
|
|
|
|
update_recents_project("remove", project_dir)
|
|
|
|
end
|
2021-01-10 11:50:15 +01:00
|
|
|
project_dir_abs = system.absolute_path(".")
|
|
|
|
if not core.set_project_dir(project_dir_abs) then
|
2021-03-05 10:33:50 +01:00
|
|
|
system.show_fatal_error("Lite XL internal error", "cannot set project directory to cwd")
|
2021-01-10 11:50:15 +01:00
|
|
|
os.exit(1)
|
|
|
|
end
|
|
|
|
end
|
2020-12-28 10:26:27 +01:00
|
|
|
|
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-08-27 00:17:43 +02:00
|
|
|
core.quit_request = false
|
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
|
|
|
|
|
|
|
command.add_defaults()
|
2020-12-10 17:56:53 +01:00
|
|
|
local got_user_error = not core.load_user_directory()
|
2021-04-07 09:42:19 +02:00
|
|
|
local plugins_success, plugins_refuse_list = core.load_plugins()
|
2020-12-08 18:49:58 +01:00
|
|
|
|
|
|
|
do
|
2021-01-10 11:50:15 +01:00
|
|
|
local pdir, pname = project_dir_abs:match("(.*)[/\\\\](.*)")
|
2021-05-01 19:19:44 +02:00
|
|
|
core.log("Opening project %q from directory %s", pname, pdir)
|
2020-12-08 18:49:58 +01:00
|
|
|
end
|
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-12-28 10:59:01 +01:00
|
|
|
-- We add the project directory now because the project's module is loaded.
|
|
|
|
core.add_project_directory(project_dir_abs)
|
|
|
|
|
2021-07-12 18:21:27 +02:00
|
|
|
-- We assume we have just a single project directory here. Now that StatusView
|
|
|
|
-- is there show max files warning if needed.
|
|
|
|
if core.project_directories[1].files_limit then
|
2021-10-21 11:07:37 +02:00
|
|
|
show_max_files_warning(core.project_directories[1])
|
2021-07-12 18:21:27 +02:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
|
2021-02-19 09:29:46 +01:00
|
|
|
if delayed_error then
|
|
|
|
core.error(delayed_error)
|
|
|
|
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-12-28 12:32:25 +01:00
|
|
|
configure_borderless_window()
|
2021-04-12 19:05:30 +02:00
|
|
|
|
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" ..
|
2021-12-19 18:13:19 +01:00
|
|
|
"Please download a recent version from https://github.com/lite-xl/lite-xl-plugins.",
|
2021-04-07 23:46:45 +02:00
|
|
|
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
|
2021-05-31 09:41:37 +02:00
|
|
|
|
2021-12-28 12:32:25 +01:00
|
|
|
add_config_files_hooks()
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-08-11 05:14:40 +02:00
|
|
|
function core.confirm_close_docs(docs, close_fn, ...)
|
2019-12-28 12:16:32 +01:00
|
|
|
local dirty_count = 0
|
|
|
|
local dirty_name
|
2021-08-11 05:14:40 +02:00
|
|
|
for _, doc in ipairs(docs or core.docs) do
|
2019-12-28 12:16:32 +01:00
|
|
|
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
|
|
|
|
|
2022-01-08 18:59:15 +01:00
|
|
|
local temp_uid = math.floor(system.get_time() * 1000) % 0xffffffff
|
|
|
|
local temp_file_prefix = string.format(".lite_temp_%08x", tonumber(temp_uid))
|
2020-05-27 12:38:42 +02:00
|
|
|
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
|
|
|
|
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-08-11 05:14:40 +02:00
|
|
|
core.confirm_close_docs(core.docs, 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)
|
2021-08-27 00:17:43 +02:00
|
|
|
quit_with_function(function() core.quit_request = true end, force)
|
2020-12-14 13:57:24 +01:00
|
|
|
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)
|
2021-04-25 12:35:00 +02:00
|
|
|
end
|
2021-07-27 23:16:33 +02:00
|
|
|
if not info or not filename:match("%.lua$") then return false 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
|
2021-08-17 15:46:21 +02:00
|
|
|
-- we consider the version tag 2.0 equivalent to mod-version:2
|
|
|
|
version_match = (version == '2.0' and MOD_VERSION == "2")
|
2021-05-05 22:38:29 +02:00
|
|
|
break
|
|
|
|
end
|
2021-04-06 17:50:46 +02:00
|
|
|
end
|
|
|
|
f:close()
|
2021-07-27 23:16:33 +02:00
|
|
|
return true, version_match
|
2021-04-06 17:50:46 +02:00
|
|
|
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-11-07 19:14:48 +01:00
|
|
|
local files, ordered = {}, {}
|
2021-07-20 20:39:50 +02:00
|
|
|
for _, root_dir in ipairs {DATADIR, USERDIR} do
|
2021-03-20 16:34:00 +01:00
|
|
|
local plugin_dir = root_dir .. "/plugins"
|
2021-07-20 20:39:50 +02:00
|
|
|
for _, filename in ipairs(system.list_dir(plugin_dir) or {}) do
|
2021-11-07 19:14:48 +01:00
|
|
|
if not files[filename] then table.insert(ordered, filename) end
|
2021-07-20 20:39:50 +02:00
|
|
|
files[filename] = plugin_dir -- user plugins will always replace system plugins
|
|
|
|
end
|
|
|
|
end
|
2021-11-07 19:14:48 +01:00
|
|
|
table.sort(ordered)
|
2021-07-20 20:39:50 +02:00
|
|
|
|
2021-11-14 21:40:23 +01:00
|
|
|
for _, filename in ipairs(ordered) do
|
2021-11-07 19:14:48 +01:00
|
|
|
local plugin_dir, basename = files[filename], filename:match("(.-)%.lua$") or filename
|
2021-08-25 23:45:18 +02:00
|
|
|
local is_lua_file, version_match = check_plugin_version(plugin_dir .. '/' .. filename)
|
|
|
|
if is_lua_file then
|
|
|
|
if not version_match then
|
|
|
|
core.log_quiet("Version mismatch for plugin %q from %s", basename, plugin_dir)
|
2021-08-29 02:23:47 +02:00
|
|
|
local list = refused_list[plugin_dir:find(USERDIR, 1, true) == 1 and 'userdir' or 'datadir'].plugins
|
2021-08-25 23:45:18 +02:00
|
|
|
table.insert(list, filename)
|
|
|
|
end
|
|
|
|
if version_match and config.plugins[basename] ~= false then
|
|
|
|
local ok = core.try(require, "plugins." .. basename)
|
|
|
|
if ok then core.log_quiet("Loaded plugin %q from %s", basename, plugin_dir) end
|
|
|
|
if not ok then
|
|
|
|
no_errors = false
|
|
|
|
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)
|
|
|
|
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")
|
|
|
|
if view ~= core.active_view then
|
2021-08-28 00:21:29 +02:00
|
|
|
if core.active_view and core.active_view.force_focus then
|
|
|
|
core.next_active_view = view
|
|
|
|
return
|
|
|
|
end
|
|
|
|
core.next_active_view = nil
|
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
|
|
|
|
|
|
|
|
|
2022-01-28 15:53:30 +01:00
|
|
|
function core.add_thread(f, weak_ref, ...)
|
2019-12-28 12:16:32 +01:00
|
|
|
local key = weak_ref or #core.threads + 1
|
2022-01-28 15:53:30 +01:00
|
|
|
local args = {...}
|
2022-01-28 22:30:19 +01:00
|
|
|
local fn = function() return core.try(f, table.unpack(args)) end
|
2020-04-22 00:23:08 +02:00
|
|
|
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-01 19:18:39 +02:00
|
|
|
function core.normalize_to_project_dir(filename)
|
|
|
|
filename = common.normalize_path(filename)
|
|
|
|
if common.path_belongs_to(filename, core.project_dir) then
|
|
|
|
filename = common.relative_path(core.project_dir, filename)
|
|
|
|
end
|
|
|
|
return filename
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-07-20 20:39:50 +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
|
|
|
|
-- current dir is core.project_dir so relative filename are considered
|
|
|
|
-- to be in core.project_dir.
|
|
|
|
-- Please note that .. or . in the filename are not taken into account.
|
|
|
|
-- This function should get only filenames normalized using
|
|
|
|
-- common.normalize_path function.
|
|
|
|
function core.project_absolute_path(filename)
|
2021-09-02 18:58:24 +02:00
|
|
|
if filename:match('^%a:\\') or filename:find('/', 1, true) == 1 then
|
2021-07-20 20:39:50 +02:00
|
|
|
return filename
|
|
|
|
else
|
|
|
|
return core.project_dir .. PATHSEP .. filename
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function core.open_doc(filename)
|
2021-07-20 20:39:50 +02:00
|
|
|
local new_file = not filename or not system.get_file_info(filename)
|
|
|
|
local abs_filename
|
2019-12-28 12:16:32 +01:00
|
|
|
if filename then
|
2021-07-20 20:39:50 +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-07-20 20:39:50 +02:00
|
|
|
filename = core.normalize_to_project_dir(filename)
|
|
|
|
abs_filename = core.project_absolute_path(filename)
|
2019-12-28 12:16:32 +01:00
|
|
|
for _, doc in ipairs(core.docs) do
|
2021-05-01 19:18:39 +02:00
|
|
|
if doc.abs_filename and abs_filename == doc.abs_filename then
|
2019-12-28 12:16:32 +01:00
|
|
|
return doc
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- no existing doc for filename; create new
|
2021-07-20 20:39:50 +02:00
|
|
|
local doc = Doc(filename, abs_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
|
|
|
|
|
|
|
|
|
2021-12-18 03:51:44 +01:00
|
|
|
local function log(level, show, fmt, ...)
|
2020-05-06 01:06:27 +02:00
|
|
|
local text = string.format(fmt, ...)
|
2021-12-18 03:51:44 +01:00
|
|
|
if show then
|
|
|
|
local s = style.log[level]
|
|
|
|
core.status_view:show_message(s.icon, s.color, text)
|
2019-12-28 12:16:32 +01:00
|
|
|
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)
|
2021-12-18 03:51:44 +01:00
|
|
|
local item = {
|
|
|
|
level = level,
|
|
|
|
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(...)
|
2021-12-18 03:51:44 +01:00
|
|
|
return log("INFO", true, ...)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.log_quiet(...)
|
2021-12-18 03:51:44 +01:00
|
|
|
return log("INFO", false, ...)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.error(...)
|
2021-12-18 03:51:44 +01:00
|
|
|
return log("ERROR", true, ...)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-08-22 13:32:45 +02:00
|
|
|
function core.get_log(i)
|
|
|
|
if i == nil then
|
|
|
|
local r = {}
|
|
|
|
for _, item in ipairs(core.log_items) do
|
|
|
|
table.insert(r, core.get_log(item))
|
|
|
|
end
|
|
|
|
return table.concat(r, "\n")
|
|
|
|
end
|
|
|
|
local item = type(i) == "number" and core.log_items[i] or i
|
2021-12-18 03:51:44 +01:00
|
|
|
local text = string.format("%s [%s] %s at %s", os.date(nil, item.time), item.level, item.text, item.at)
|
2021-08-22 13:32:45 +02:00
|
|
|
if item.info then
|
|
|
|
text = string.format("%s\n%s\n", text, item.info)
|
|
|
|
end
|
|
|
|
return text
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
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
|
|
|
|
|
2021-07-12 18:21:27 +02:00
|
|
|
local scheduled_rescan = {}
|
|
|
|
|
|
|
|
function core.has_pending_rescan()
|
|
|
|
for _ in pairs(scheduled_rescan) do
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.dir_rescan_add_job(dir, filepath)
|
|
|
|
local dirpath = filepath:match("^(.+)[/\\].+$")
|
|
|
|
local dirpath_rooted = dirpath and PATHSEP .. dirpath or ""
|
|
|
|
local abs_dirpath = dir.name .. dirpath_rooted
|
|
|
|
if dirpath then
|
|
|
|
-- check if the directory is in the project files list, if not exit
|
|
|
|
local dir_index, dir_match = file_search(dir.files, {filename = dirpath, type = "dir"})
|
|
|
|
-- Note that is dir_match is false dir_index greaten than the last valid index.
|
|
|
|
-- We use dir_index to index dir.files below only if dir_match is true.
|
|
|
|
if not dir_match or not core.project_subdir_is_shown(dir, dir.files[dir_index].filename) then return end
|
|
|
|
end
|
|
|
|
local new_time = system.get_time() + 1
|
|
|
|
|
|
|
|
-- evaluate new rescan request versus existing rescan
|
|
|
|
local remove_list = {}
|
|
|
|
for _, rescan in pairs(scheduled_rescan) do
|
|
|
|
if abs_dirpath == rescan.abs_path or common.path_belongs_to(abs_dirpath, rescan.abs_path) then
|
|
|
|
-- abs_dirpath is a subpath of a scan already ongoing: skip
|
|
|
|
rescan.time_limit = new_time
|
|
|
|
return
|
|
|
|
elseif common.path_belongs_to(rescan.abs_path, abs_dirpath) then
|
|
|
|
-- abs_dirpath already cover this rescan: add to the list of rescan to be removed
|
|
|
|
table.insert(remove_list, rescan.abs_path)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
for _, key_path in ipairs(remove_list) do
|
|
|
|
scheduled_rescan[key_path] = nil
|
|
|
|
end
|
|
|
|
|
|
|
|
scheduled_rescan[abs_dirpath] = {dir = dir, path = dirpath_rooted, abs_path = abs_dirpath, time_limit = new_time}
|
|
|
|
core.add_thread(function()
|
|
|
|
while true do
|
|
|
|
local rescan = scheduled_rescan[abs_dirpath]
|
|
|
|
if not rescan then return end
|
|
|
|
if system.get_time() > rescan.time_limit then
|
|
|
|
local has_changes = rescan_project_subdir(rescan.dir, rescan.path)
|
|
|
|
if has_changes then
|
|
|
|
core.redraw = true -- we run without an event, from a thread
|
|
|
|
rescan.time_limit = new_time
|
|
|
|
else
|
|
|
|
scheduled_rescan[rescan.abs_path] = nil
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
coroutine.yield(0.2)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-10-08 23:15:25 +02:00
|
|
|
-- no-op but can be overrided by plugins
|
2021-12-22 23:43:56 +01:00
|
|
|
function core.on_dirmonitor_modify(dir, filepath) end
|
|
|
|
function core.on_dirmonitor_delete(dir, filepath) end
|
2021-10-08 23:13:50 +02:00
|
|
|
|
|
|
|
|
2021-07-12 18:21:27 +02:00
|
|
|
function core.on_dir_change(watch_id, action, filepath)
|
|
|
|
local dir = project_dir_by_watch_id(watch_id)
|
|
|
|
if not dir then return end
|
|
|
|
core.dir_rescan_add_job(dir, filepath)
|
|
|
|
if action == "delete" then
|
|
|
|
project_scan_remove_file(dir, filepath)
|
2021-12-20 11:03:49 +01:00
|
|
|
core.on_dirmonitor_delete(dir, filepath)
|
2021-07-12 18:21:27 +02:00
|
|
|
elseif action == "create" then
|
|
|
|
project_scan_add_file(dir, filepath)
|
2021-10-08 23:13:50 +02:00
|
|
|
core.on_dirmonitor_modify(dir, filepath);
|
|
|
|
elseif action == "modify" then
|
|
|
|
core.on_dirmonitor_modify(dir, filepath);
|
2021-07-12 18:21:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
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
|
2021-10-06 01:42:04 +02:00
|
|
|
if not core.root_view:on_mouse_pressed(...) then
|
|
|
|
did_keymap = keymap.on_mouse_pressed(...)
|
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
elseif type == "mousereleased" then
|
|
|
|
core.root_view:on_mouse_released(...)
|
|
|
|
elseif type == "mousewheel" then
|
2021-11-07 20:31:15 +01:00
|
|
|
if not core.root_view:on_mouse_wheel(...) then
|
|
|
|
did_keymap = keymap.on_mouse_wheel(...)
|
|
|
|
end
|
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(...)
|
2021-07-12 18:21:27 +02:00
|
|
|
elseif type == "dirchange" then
|
|
|
|
core.on_dir_change(...)
|
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
|
|
|
|
|
|
|
|
for type, a,b,c,d in system.poll_event do
|
2021-06-09 23:37:03 +02:00
|
|
|
if type == "textinput" and did_keymap then
|
2019-12-28 12:16:32 +01:00
|
|
|
did_keymap = false
|
2021-06-09 23:37:03 +02:00
|
|
|
elseif type == "mousemoved" then
|
|
|
|
core.try(core.on_event, type, a, b, c, d)
|
2019-12-28 12:16:32 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
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)
|
2021-06-20 06:13:07 +02:00
|
|
|
doc:on_close()
|
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()
|
2021-07-12 18:21:27 +02:00
|
|
|
local need_more_work = run_threads() or core.has_pending_rescan()
|
2021-08-27 00:17:43 +02:00
|
|
|
if core.restart_request or core.quit_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
|
|
|
|
|
|
|
|
|
2021-06-03 22:49:37 +02:00
|
|
|
function core.request_cursor(value)
|
|
|
|
core.cursor_change_req = value
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
return core
|