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"
|
2022-06-03 01:20:54 +02:00
|
|
|
local style = require "colors.default"
|
2019-12-28 12:16:32 +01:00
|
|
|
local command
|
|
|
|
local keymap
|
2022-03-06 06:59:22 +01:00
|
|
|
local dirwatch
|
2022-10-16 01:58:51 +02:00
|
|
|
local ime
|
2019-12-28 12:16:32 +01:00
|
|
|
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()
|
2023-01-30 17:10:11 +01:00
|
|
|
local ok, t = pcall(dofile, USERDIR .. PATHSEP .. "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()
|
2023-01-30 17:10:11 +01:00
|
|
|
local fp = io.open(USERDIR .. PATHSEP .. "session.lua", "w")
|
2020-12-31 11:25:12 +01:00
|
|
|
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-30 23:57:23 +01:00
|
|
|
|
|
|
|
local function reload_customizations()
|
2022-06-05 04:19:31 +02:00
|
|
|
local user_error = not core.load_user_directory()
|
|
|
|
local project_error = not core.load_project_module()
|
|
|
|
if user_error or project_error then
|
2022-06-28 04:21:43 +02:00
|
|
|
-- Use core.add_thread to delay opening the LogView, as opening
|
|
|
|
-- it directly here disturbs the normal save operations.
|
|
|
|
core.add_thread(function()
|
|
|
|
local LogView = require "core.logview"
|
|
|
|
local rn = core.root_view.root_node
|
|
|
|
for _,v in pairs(core.root_view.root_node:get_children()) do
|
|
|
|
if v:is(LogView) then
|
|
|
|
rn:get_node_for_view(v):set_active_view(v)
|
|
|
|
return
|
|
|
|
end
|
2022-06-05 04:19:31 +02:00
|
|
|
end
|
2022-06-28 04:21:43 +02:00
|
|
|
command.perform("core:open-log")
|
|
|
|
end)
|
2022-06-05 04:19:31 +02:00
|
|
|
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-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 "..
|
2022-03-06 06:59:22 +01:00
|
|
|
"usage.md at https://github.com/lite-xl/lite-xl."
|
|
|
|
if core.status_view then
|
|
|
|
core.status_view:show_message("!", style.accent, message)
|
|
|
|
end
|
2021-07-12 18:21:27 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-03-09 01:30:25 +01:00
|
|
|
-- bisects the sorted file list to get to things in ln(n)
|
|
|
|
local function file_bisect(files, is_superior, start_idx, end_idx)
|
|
|
|
local inf, sup = start_idx or 1, end_idx or #files
|
2021-07-12 18:21:27 +02:00
|
|
|
while sup - inf > 8 do
|
|
|
|
local curr = math.floor((inf + sup) / 2)
|
2022-03-09 01:30:25 +01:00
|
|
|
if is_superior(files[curr]) then
|
2021-07-12 18:21:27 +02:00
|
|
|
sup = curr - 1
|
|
|
|
else
|
|
|
|
inf = curr
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2021-07-12 18:21:27 +02:00
|
|
|
end
|
2022-03-09 01:30:25 +01:00
|
|
|
while inf <= sup and not is_superior(files[inf]) do
|
2021-07-12 18:21:27 +02:00
|
|
|
inf = inf + 1
|
2021-11-26 13:45:13 +01:00
|
|
|
end
|
2022-03-09 01:30:25 +01:00
|
|
|
return inf
|
2021-07-12 18:21:27 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
|
2022-03-09 01:30:25 +01:00
|
|
|
local function file_search(files, info)
|
|
|
|
local idx = file_bisect(files, function(file)
|
2022-03-09 07:15:01 +01:00
|
|
|
return system.path_compare(info.filename, info.type, file.filename, file.type)
|
2022-03-09 01:30:25 +01:00
|
|
|
end)
|
|
|
|
if idx > 1 and files[idx-1].filename == info.filename then
|
|
|
|
return idx - 1, true
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2022-03-09 01:30:25 +01:00
|
|
|
return idx, false
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-07-12 18:21:27 +02:00
|
|
|
local function files_info_equal(a, b)
|
2022-03-09 01:30:25 +01:00
|
|
|
return (a == nil and b == nil) or (a and b and a.filename == b.filename and a.type == b.type)
|
2021-07-12 18:21:27 +02:00
|
|
|
end
|
|
|
|
|
2021-06-17 19:07:32 +02:00
|
|
|
|
2022-03-09 01:30:25 +01:00
|
|
|
local function project_subdir_bounds(dir, filename, start_index)
|
|
|
|
local found = true
|
|
|
|
if not start_index then
|
|
|
|
start_index, found = file_search(dir.files, { type = "dir", filename = filename })
|
2021-07-12 18:21:27 +02:00
|
|
|
end
|
2022-03-09 01:30:25 +01:00
|
|
|
if found then
|
|
|
|
local end_index = file_bisect(dir.files, function(file)
|
|
|
|
return not common.path_belongs_to(file.filename, filename)
|
|
|
|
end, start_index + 1)
|
|
|
|
return start_index, end_index - start_index, dir.files[start_index]
|
2021-05-25 23:25:56 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-01-10 16:26:39 +01:00
|
|
|
|
2022-03-09 01:30:25 +01:00
|
|
|
-- Should be called on any directory that registers a change, or on a directory we open if we're over the file limit.
|
|
|
|
-- Uses relative paths at the project root (i.e. target = "", target = "first-level-directory", target = "first-level-directory/second-level-directory")
|
|
|
|
local function refresh_directory(topdir, target)
|
|
|
|
local directory_start_idx, directory_end_idx = 1, #topdir.files
|
|
|
|
if target and target ~= "" then
|
|
|
|
directory_start_idx, directory_end_idx = project_subdir_bounds(topdir, target)
|
|
|
|
directory_end_idx = directory_start_idx + directory_end_idx - 1
|
|
|
|
directory_start_idx = directory_start_idx + 1
|
2021-07-12 18:21:27 +02:00
|
|
|
end
|
2021-05-25 23:25:56 +02:00
|
|
|
|
2022-03-09 01:30:25 +01:00
|
|
|
local files = dirwatch.get_directory_files(topdir, topdir.name, (target or ""), {}, 0, function() return false end)
|
|
|
|
local change = false
|
2022-03-09 07:15:01 +01:00
|
|
|
|
2022-03-09 01:30:25 +01:00
|
|
|
-- If this file doesn't exist, we should be calling this on our parent directory, assume we'll do that.
|
|
|
|
-- Unwatch just in case.
|
2022-03-09 07:15:01 +01:00
|
|
|
if files == nil then
|
2022-03-09 01:30:25 +01:00
|
|
|
topdir.watch:unwatch(topdir.name .. PATHSEP .. (target or ""))
|
2021-07-12 18:21:27 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2022-03-09 01:30:25 +01:00
|
|
|
local new_idx, old_idx = 1, directory_start_idx
|
|
|
|
local new_directories = {}
|
|
|
|
-- Run through each sorted list and compare them. If we find a new entry, insert it and flag as new. If we're missing an entry
|
|
|
|
-- remove it and delete the entry from the list.
|
2022-03-09 07:15:01 +01:00
|
|
|
while old_idx <= directory_end_idx or new_idx <= #files do
|
2022-03-09 01:30:25 +01:00
|
|
|
local old_info, new_info = topdir.files[old_idx], files[new_idx]
|
|
|
|
if not files_info_equal(new_info, old_info) then
|
|
|
|
change = true
|
|
|
|
-- If we're a new file, and we exist *before* the other file in the list, then add to the list.
|
|
|
|
if not old_info or (new_info and system.path_compare(new_info.filename, new_info.type, old_info.filename, old_info.type)) then
|
|
|
|
table.insert(topdir.files, old_idx, new_info)
|
|
|
|
old_idx, new_idx = old_idx + 1, new_idx + 1
|
|
|
|
if new_info.type == "dir" then
|
|
|
|
table.insert(new_directories, new_info)
|
|
|
|
end
|
|
|
|
directory_end_idx = directory_end_idx + 1
|
|
|
|
else
|
2022-03-09 07:15:01 +01:00
|
|
|
-- If it's not there, remove the entry from the list as being out of order.
|
2022-03-09 01:30:25 +01:00
|
|
|
table.remove(topdir.files, old_idx)
|
|
|
|
if old_info.type == "dir" then
|
|
|
|
topdir.watch:unwatch(topdir.name .. PATHSEP .. old_info.filename)
|
2021-05-25 23:25:56 +02:00
|
|
|
end
|
2022-03-09 01:30:25 +01:00
|
|
|
directory_end_idx = directory_end_idx - 1
|
2021-10-21 23:57:17 +02:00
|
|
|
end
|
2022-03-09 01:30:25 +01:00
|
|
|
else
|
|
|
|
-- If this file is a directory, determine in ln(n) the size of the directory, and skip every file in it.
|
|
|
|
local size = old_info and old_info.type == "dir" and select(2, project_subdir_bounds(topdir, old_info.filename, old_idx)) or 1
|
|
|
|
old_idx, new_idx = old_idx + size, new_idx + 1
|
2021-05-25 23:25:56 +02:00
|
|
|
end
|
2021-10-21 23:57:17 +02:00
|
|
|
end
|
2022-03-09 07:15:01 +01:00
|
|
|
for i, v in ipairs(new_directories) do
|
2022-03-09 01:30:25 +01:00
|
|
|
topdir.watch:watch(topdir.name .. PATHSEP .. v.filename)
|
|
|
|
if not topdir.files_limit or core.project_subdir_is_shown(topdir, v.filename) then
|
|
|
|
refresh_directory(topdir, v.filename)
|
2021-10-21 23:57:17 +02:00
|
|
|
end
|
|
|
|
end
|
2022-03-09 01:30:25 +01:00
|
|
|
if change then
|
|
|
|
core.redraw = true
|
|
|
|
topdir.is_dirty = true
|
2021-05-25 23:25:56 +02:00
|
|
|
end
|
2022-03-09 01:30:25 +01:00
|
|
|
return change
|
2021-05-25 23:25:56 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2022-03-06 06:59:22 +01: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)
|
2021-05-25 23:25:56 +02:00
|
|
|
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)
|
2022-03-06 06:59:22 +01:00
|
|
|
local topdir = {
|
2021-10-21 23:57:17 +02:00
|
|
|
name = path,
|
|
|
|
item = {filename = common.basename(path), type = "dir", topdir = true},
|
|
|
|
files_limit = false,
|
|
|
|
is_dirty = true,
|
|
|
|
shown_subdir = {},
|
2022-03-06 06:59:22 +01:00
|
|
|
watch_thread = nil,
|
|
|
|
watch = dirwatch.new()
|
2021-10-21 23:57:17 +02:00
|
|
|
}
|
2022-03-07 00:34:12 +01:00
|
|
|
table.insert(core.project_directories, topdir)
|
|
|
|
|
2022-03-06 06:59:22 +01:00
|
|
|
local fstype = PLATFORM == "Linux" and system.get_fs_type(topdir.name) or "unknown"
|
|
|
|
topdir.force_scans = (fstype == "nfs" or fstype == "fuse")
|
|
|
|
local t, complete, entries_count = dirwatch.get_directory_files(topdir, topdir.name, "", {}, 0, timed_max_files_pred)
|
|
|
|
topdir.files = t
|
|
|
|
if not complete then
|
|
|
|
topdir.slow_filesystem = not complete and (entries_count <= config.max_project_files)
|
|
|
|
topdir.files_limit = true
|
|
|
|
show_max_files_warning(topdir)
|
2022-03-09 01:30:25 +01:00
|
|
|
refresh_directory(topdir)
|
2022-03-06 06:59:22 +01:00
|
|
|
else
|
|
|
|
for i,v in ipairs(t) do
|
|
|
|
if v.type == "dir" then topdir.watch:watch(path .. PATHSEP .. v.filename) end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
topdir.watch:watch(topdir.name)
|
|
|
|
-- each top level directory gets a watch thread. if the project is small, or
|
|
|
|
-- if the ablity to use directory watches hasn't been compromised in some way
|
2022-03-07 00:34:12 +01:00
|
|
|
-- either through error, or amount of files, then this should be incredibly
|
2022-03-06 06:59:22 +01:00
|
|
|
-- quick; essentially one syscall per check. Otherwise, this may take a bit of
|
|
|
|
-- time; the watch will yield in this coroutine after 0.01 second, for 0.1 seconds.
|
|
|
|
topdir.watch_thread = core.add_thread(function()
|
|
|
|
while true do
|
2022-04-24 19:40:58 +02:00
|
|
|
local changed = topdir.watch:check(function(target)
|
2022-03-09 07:15:01 +01:00
|
|
|
if target == topdir.name then return refresh_directory(topdir) end
|
2022-03-06 06:59:22 +01:00
|
|
|
local dirpath = target:sub(#topdir.name + 2)
|
|
|
|
local abs_dirpath = topdir.name .. PATHSEP .. dirpath
|
|
|
|
if dirpath then
|
|
|
|
-- check if the directory is in the project files list, if not exit.
|
|
|
|
local dir_index, dir_match = file_search(topdir.files, {filename = dirpath, type = "dir"})
|
|
|
|
if not dir_match or not core.project_subdir_is_shown(topdir, topdir.files[dir_index].filename) then return end
|
|
|
|
end
|
2022-03-09 01:30:25 +01:00
|
|
|
return refresh_directory(topdir, dirpath)
|
2022-03-06 06:59:22 +01:00
|
|
|
end, 0.01, 0.01)
|
2022-10-11 19:22:44 +02:00
|
|
|
-- properly exit coroutine if project not open anymore to clear dir watch
|
|
|
|
local project_dir_open = false
|
|
|
|
for _, prj in ipairs(core.project_directories) do
|
|
|
|
if topdir == prj then
|
|
|
|
project_dir_open = true
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if project_dir_open then
|
2023-03-28 18:57:47 +02:00
|
|
|
coroutine.yield(changed and 0 or 0.05)
|
2022-10-11 19:22:44 +02:00
|
|
|
else
|
|
|
|
return
|
|
|
|
end
|
2022-03-06 06:59:22 +01:00
|
|
|
end
|
|
|
|
end)
|
2022-03-07 00:34:12 +01:00
|
|
|
|
2021-10-21 23:57:17 +02:00
|
|
|
if path == core.project_dir then
|
2022-03-06 06:59:22 +01:00
|
|
|
core.project_files = topdir.files
|
2021-10-21 23:57:17 +02:00
|
|
|
end
|
|
|
|
core.redraw = true
|
2022-03-06 06:59:22 +01:00
|
|
|
return topdir
|
2021-10-21 23:57:17 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-12-28 12:32:25 +01:00
|
|
|
-- The function below is needed to reload the project directories
|
|
|
|
-- when the project's module changes.
|
2022-05-02 19:55:25 +02:00
|
|
|
function core.rescan_project_directories()
|
2021-12-28 12:32:25 +01:00
|
|
|
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.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.
|
|
|
|
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
|
2021-07-12 18:21:27 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
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")
|
2022-03-06 06:59:22 +01:00
|
|
|
dir.shown_subdir[filename] = expanded
|
2022-10-13 06:37:52 +02:00
|
|
|
if expanded then
|
|
|
|
dir.watch:watch(dir.name .. PATHSEP .. filename)
|
|
|
|
else
|
|
|
|
dir.watch:unwatch(dir.name .. PATHSEP .. filename)
|
|
|
|
end
|
2022-03-09 01:30:25 +01:00
|
|
|
return refresh_directory(dir, filename)
|
2021-07-12 18:21:27 +02:00
|
|
|
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)
|
2022-03-06 06:59:22 +01:00
|
|
|
elseif not common.match_pattern(common.basename(info.filename), config.ignore_files) then
|
2021-07-12 18:21:27 +02:00
|
|
|
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
|
|
|
|
|
|
|
|
|
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
|
|
|
|
2022-12-28 00:40:22 +01:00
|
|
|
-- pass 'true' for second parameter to overwrite an existing binding
|
|
|
|
-- keymap.add({ ["ctrl+pageup"] = "root:switch-to-previous-tab" }, true)
|
|
|
|
-- keymap.add({ ["ctrl+pagedown"] = "root:switch-to-next-tab" }, true)
|
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
|
|
|
--
|
2022-04-18 21:14:50 +02:00
|
|
|
-- DATADIR is the location of the installed Lite XL Lua code, default color
|
|
|
|
-- schemes and fonts.
|
|
|
|
-- USERDIR is the location of the Lite XL configuration directory.
|
|
|
|
--
|
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:
|
|
|
|
--
|
2022-08-20 22:15:08 +02:00
|
|
|
-- {antialiasing="grayscale", hinting="full", bold=true, italic=true, underline=true, smoothing=true, strikethrough=true}
|
2020-12-11 14:49:03 +01:00
|
|
|
--
|
|
|
|
-- possible values are:
|
|
|
|
-- antialiasing: grayscale, subpixel
|
|
|
|
-- hinting: none, slight, full
|
2022-07-15 12:05:59 +02:00
|
|
|
-- bold: true, false
|
|
|
|
-- italic: true, false
|
|
|
|
-- underline: true, false
|
2022-08-20 22:15:08 +02:00
|
|
|
-- smoothing: true, false
|
|
|
|
-- strikethrough: true, false
|
2021-01-13 14:50:24 +01:00
|
|
|
|
|
|
|
------------------------------ Plugins ----------------------------------------
|
|
|
|
|
2023-03-29 01:57:24 +02:00
|
|
|
-- disable plugin loading setting config entries:
|
2021-01-13 14:50:24 +01:00
|
|
|
|
2023-03-29 01:57:24 +02:00
|
|
|
-- disable plugin detectindent, otherwise it is enabled by default:
|
2021-07-20 21:09:14 +02:00
|
|
|
-- config.plugins.detectindent = false
|
2022-10-20 04:23:01 +02:00
|
|
|
|
2022-12-28 00:56:30 +01:00
|
|
|
---------------------------- Miscellaneous -------------------------------------
|
2022-10-20 04:23:01 +02:00
|
|
|
|
|
|
|
-- modify list of files to ignore when indexing the project:
|
|
|
|
-- config.ignore_files = {
|
|
|
|
-- -- folders
|
|
|
|
-- "^%.svn/", "^%.git/", "^%.hg/", "^CVS/", "^%.Trash/", "^%.Trash%-.*/",
|
|
|
|
-- "^node_modules/", "^%.cache/", "^__pycache__/",
|
|
|
|
-- -- files
|
|
|
|
-- "%.pyc$", "%.pyo$", "%.exe$", "%.dll$", "%.obj$", "%.o$",
|
|
|
|
-- "%.a$", "%.lib$", "%.so$", "%.dylib$", "%.ncb$", "%.sdf$",
|
|
|
|
-- "%.suo$", "%.pdb$", "%.idb$", "%.class$", "%.psd$", "%.db$",
|
|
|
|
-- "^desktop%.ini$", "^%.DS_Store$", "^%.directory$",
|
|
|
|
-- }
|
|
|
|
|
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".
|
|
|
|
|
2022-12-30 12:52:47 +01:00
|
|
|
-- You may activate some plugins on a per-project basis to override the user's settings.
|
2022-01-04 00:25:14 +01:00
|
|
|
-- 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
|
2023-01-30 17:10:11 +01:00
|
|
|
local init_filename = USERDIR .. PATHSEP .. "init.lua"
|
2020-12-10 17:56:53 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-05-02 19:55:25 +02:00
|
|
|
function core.configure_borderless_window()
|
2021-12-28 12:32:25 +01:00
|
|
|
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()
|
2022-05-02 19:55:25 +02:00
|
|
|
core.rescan_project_directories()
|
|
|
|
core.configure_borderless_window()
|
2021-05-31 09:41:37 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-10-10 03:43:23 +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)
|
2022-06-01 03:48:47 +02:00
|
|
|
if common.is_absolute_path(filename) then
|
2021-10-10 03:43:23 +02:00
|
|
|
return common.normalize_path(filename)
|
|
|
|
elseif not core.project_dir then
|
|
|
|
local cwd = system.absolute_path(".")
|
|
|
|
return cwd .. PATHSEP .. common.normalize_path(filename)
|
|
|
|
else
|
|
|
|
return core.project_dir .. PATHSEP .. filename
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
function core.init()
|
2023-02-02 01:28:21 +01:00
|
|
|
core.log_items = {}
|
|
|
|
core.log_quiet("Lite XL version %s - mod-version %s", VERSION, MOD_VERSION_STRING)
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
command = require "core.command"
|
|
|
|
keymap = require "core.keymap"
|
2022-03-06 06:59:22 +01:00
|
|
|
dirwatch = require "core.dirwatch"
|
2022-10-16 01:58:51 +02:00
|
|
|
ime = require "core.ime"
|
2019-12-28 12:16:32 +01:00
|
|
|
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 = {}
|
|
|
|
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 {}
|
2021-10-10 03:43:23 +02:00
|
|
|
if 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
|
2021-10-10 03:43:23 +02:00
|
|
|
local file_abs = core.project_absolute_path(arg_filename)
|
|
|
|
if file_abs then
|
|
|
|
table.insert(files, file_abs)
|
|
|
|
project_dir = file_abs:match("^(.+)[/\\].+$")
|
|
|
|
end
|
2021-09-07 15:44:15 +02:00
|
|
|
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.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-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
|
|
|
|
2022-03-07 02:21:00 +01:00
|
|
|
-- We load core views before plugins that may need them.
|
2022-06-08 02:09:48 +02:00
|
|
|
---@type core.rootview
|
2019-12-28 12:16:32 +01:00
|
|
|
core.root_view = RootView()
|
2022-06-08 02:09:48 +02:00
|
|
|
---@type core.commandview
|
2019-12-28 12:16:32 +01:00
|
|
|
core.command_view = CommandView()
|
2022-06-08 02:09:48 +02:00
|
|
|
---@type core.statusview
|
2019-12-28 12:16:32 +01:00
|
|
|
core.status_view = StatusView()
|
2022-06-08 02:09:48 +02:00
|
|
|
---@type core.nagview
|
2021-03-13 12:57:52 +01:00
|
|
|
core.nag_view = NagView()
|
2022-06-08 02:09:48 +02:00
|
|
|
---@type core.titleview
|
2021-04-10 19:35:57 +02:00
|
|
|
core.title_view = TitleView()
|
2019-12-28 12:16:32 +01:00
|
|
|
|
2022-03-07 02:21:00 +01:00
|
|
|
-- Some plugins (eg: console) require the nodes to be initialized to defaults
|
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
|
|
|
|
2022-03-07 02:21:00 +01:00
|
|
|
-- Load defaiult commands first so plugins can override them
|
2019-12-28 12:16:32 +01:00
|
|
|
command.add_defaults()
|
2022-03-07 02:21:00 +01:00
|
|
|
|
|
|
|
-- Load user module, plugins and project module
|
2022-03-06 06:59:22 +01:00
|
|
|
local got_user_error, got_project_error = not core.load_user_directory()
|
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)
|
2022-03-06 21:03:54 +01:00
|
|
|
-- We prevent set_project_dir below to effectively add and scan the directory because the
|
2021-12-28 10:59:01 +01:00
|
|
|
-- 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
|
2022-03-06 21:03:54 +01:00
|
|
|
got_project_error = not core.load_project_module()
|
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(".")
|
2022-03-07 00:34:12 +01:00
|
|
|
if not core.set_project_dir(project_dir_abs, function()
|
2022-03-06 06:59:22 +01:00
|
|
|
got_project_error = not core.load_project_module()
|
|
|
|
end) 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
|
|
|
|
2022-03-07 02:21:00 +01:00
|
|
|
-- Load core plugins after user ones to let the user override them
|
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
|
2022-02-05 14:35:38 +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
|
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)
|
2019-12-28 12:16:32 +01:00
|
|
|
|
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-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
|
|
|
|
2022-05-02 19:55:25 +02:00
|
|
|
core.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 = {
|
2023-02-06 18:38:00 +01:00
|
|
|
{ text = "Exit", default_no = true },
|
|
|
|
{ text = "Continue", default_yes = true }
|
2021-04-07 09:42:19 +02:00
|
|
|
}
|
|
|
|
local msg = {}
|
|
|
|
for _, entry in pairs(plugins_refuse_list) do
|
|
|
|
if #entry.plugins > 0 then
|
2023-02-02 01:28:21 +01:00
|
|
|
local msg_list = {}
|
|
|
|
for _, p in pairs(entry.plugins) do
|
|
|
|
table.insert(msg_list, string.format("%s[%s]", p.file, p.version_string))
|
|
|
|
end
|
|
|
|
msg[#msg + 1] = string.format("Plugins from directory \"%s\":\n%s", common.home_encode(entry.dir), table.concat(msg_list, "\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(
|
2023-02-02 01:28:21 +01:00
|
|
|
"Some plugins are not loaded due to version mismatch. Expected version %s.\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.",
|
2023-02-02 01:28:21 +01:00
|
|
|
MOD_VERSION_STRING, 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 = {
|
2023-02-06 18:38:00 +01:00
|
|
|
{ text = "Yes", default_yes = true },
|
|
|
|
{ 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
|
|
|
|
2022-03-29 04:36:49 +02:00
|
|
|
function core.delete_temp_files(dir)
|
|
|
|
dir = type(dir) == "string" and common.normalize_path(dir) or USERDIR
|
2022-04-28 01:54:43 +02:00
|
|
|
for _, filename in ipairs(system.list_dir(dir) or {}) do
|
2020-05-24 18:34:23 +02:00
|
|
|
if filename:find(temp_file_prefix, 1, true) == 1 then
|
2022-03-29 04:36:49 +02:00
|
|
|
os.remove(dir .. PATHSEP .. filename)
|
2020-05-24 18:34:23 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-03-29 04:36:49 +02:00
|
|
|
function core.temp_filename(ext, dir)
|
|
|
|
dir = type(dir) == "string" and common.normalize_path(dir) or USERDIR
|
2020-05-27 12:38:42 +02:00
|
|
|
temp_file_counter = temp_file_counter + 1
|
2022-03-29 04:36:49 +02:00
|
|
|
return dir .. 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
|
2022-03-29 04:36:49 +02:00
|
|
|
core.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
|
|
|
|
|
|
|
|
|
2023-02-02 01:28:21 +01:00
|
|
|
local mod_version_regex =
|
|
|
|
regex.compile([[--.*mod-version:(\d+)(?:\.(\d+))?(?:\.(\d+))?(?:$|\s)]])
|
2022-05-06 00:02:00 +02:00
|
|
|
local function get_plugin_details(filename)
|
2021-04-25 12:35:00 +02:00
|
|
|
local info = system.get_file_info(filename)
|
|
|
|
if info ~= nil and info.type == "dir" then
|
2023-01-30 17:10:11 +01:00
|
|
|
filename = filename .. PATHSEP .. "init.lua"
|
2021-04-25 19:14:36 +02:00
|
|
|
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
|
2022-05-06 00:02:00 +02:00
|
|
|
local priority = false
|
2021-04-06 17:50:46 +02:00
|
|
|
local version_match = false
|
2023-02-02 01:28:21 +01:00
|
|
|
local major, minor, patch
|
|
|
|
|
2021-04-06 17:50:46 +02:00
|
|
|
for line in f:lines() do
|
2022-05-06 00:02:00 +02:00
|
|
|
if not version_match then
|
2023-02-02 01:28:21 +01:00
|
|
|
local _major, _minor, _patch = mod_version_regex:match(line)
|
|
|
|
if _major then
|
|
|
|
_major = tonumber(_major) or 0
|
|
|
|
_minor = tonumber(_minor) or 0
|
|
|
|
_patch = tonumber(_patch) or 0
|
|
|
|
major, minor, patch = _major, _minor, _patch
|
|
|
|
|
|
|
|
version_match = major == MOD_VERSION_MAJOR
|
|
|
|
if version_match then
|
|
|
|
version_match = minor <= MOD_VERSION_MINOR
|
|
|
|
end
|
|
|
|
if version_match then
|
|
|
|
version_match = patch <= MOD_VERSION_PATCH
|
|
|
|
end
|
2022-05-06 00:02:00 +02:00
|
|
|
end
|
2021-05-05 22:38:29 +02:00
|
|
|
end
|
2023-02-02 01:28:21 +01:00
|
|
|
|
2022-05-06 00:02:00 +02:00
|
|
|
if not priority then
|
|
|
|
priority = line:match('%-%-.*%f[%a]priority%s*:%s*(%d+)')
|
|
|
|
if priority then priority = tonumber(priority) end
|
2021-05-05 22:38:29 +02:00
|
|
|
end
|
2023-02-02 01:28:21 +01:00
|
|
|
|
2022-05-06 00:02:00 +02:00
|
|
|
if version_match then
|
2021-05-05 22:38:29 +02:00
|
|
|
break
|
|
|
|
end
|
2021-04-06 17:50:46 +02:00
|
|
|
end
|
|
|
|
f:close()
|
2022-05-06 00:02:00 +02:00
|
|
|
return true, {
|
|
|
|
version_match = version_match,
|
2023-02-02 01:28:21 +01:00
|
|
|
version = major and {major, minor, patch} or {},
|
2022-05-06 00:02:00 +02:00
|
|
|
priority = priority or 100
|
|
|
|
}
|
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
|
2023-01-30 17:10:11 +01:00
|
|
|
local plugin_dir = root_dir .. PATHSEP .. "plugins"
|
2021-07-20 20:39:50 +02:00
|
|
|
for _, filename in ipairs(system.list_dir(plugin_dir) or {}) do
|
2022-05-06 00:02:00 +02:00
|
|
|
if not files[filename] then
|
|
|
|
table.insert(
|
|
|
|
ordered, {file = filename}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
-- user plugins will always replace system plugins
|
|
|
|
files[filename] = plugin_dir
|
2021-07-20 20:39:50 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-05-06 00:02:00 +02:00
|
|
|
for _, plugin in ipairs(ordered) do
|
|
|
|
local dir = files[plugin.file]
|
|
|
|
local name = plugin.file:match("(.-)%.lua$") or plugin.file
|
2023-01-30 17:10:11 +01:00
|
|
|
local is_lua_file, details = get_plugin_details(dir .. PATHSEP .. plugin.file)
|
2022-05-06 00:02:00 +02:00
|
|
|
|
|
|
|
plugin.valid = is_lua_file
|
|
|
|
plugin.name = name
|
|
|
|
plugin.dir = dir
|
|
|
|
plugin.priority = details and details.priority or 100
|
|
|
|
plugin.version_match = details and details.version_match or false
|
2023-02-02 01:28:21 +01:00
|
|
|
plugin.version = details and details.version or {}
|
|
|
|
plugin.version_string = #plugin.version > 0 and table.concat(plugin.version, ".") or "unknown"
|
2022-05-06 00:02:00 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
-- sort by priority or name for plugins that have same priority
|
|
|
|
table.sort(ordered, function(a, b)
|
|
|
|
if a.priority ~= b.priority then
|
|
|
|
return a.priority < b.priority
|
|
|
|
end
|
|
|
|
return a.name < b.name
|
|
|
|
end)
|
2021-07-20 20:39:50 +02:00
|
|
|
|
2022-04-30 22:23:05 +02:00
|
|
|
local load_start = system.get_time()
|
2022-05-06 00:02:00 +02:00
|
|
|
for _, plugin in ipairs(ordered) do
|
|
|
|
if plugin.valid then
|
|
|
|
if not config.skip_plugins_version and not plugin.version_match then
|
|
|
|
core.log_quiet(
|
2023-02-02 01:28:21 +01:00
|
|
|
"Version mismatch for plugin %q[%s] from %s",
|
2022-05-06 00:02:00 +02:00
|
|
|
plugin.name,
|
2023-02-02 01:28:21 +01:00
|
|
|
plugin.version_string,
|
2022-05-06 00:02:00 +02:00
|
|
|
plugin.dir
|
|
|
|
)
|
|
|
|
local rlist = plugin.dir:find(USERDIR, 1, true) == 1
|
|
|
|
and 'userdir' or 'datadir'
|
|
|
|
local list = refused_list[rlist].plugins
|
2023-02-02 01:28:21 +01:00
|
|
|
table.insert(list, plugin)
|
2022-05-06 00:02:00 +02:00
|
|
|
elseif config.plugins[plugin.name] ~= false then
|
2022-04-29 03:41:55 +02:00
|
|
|
local start = system.get_time()
|
2023-01-19 16:14:45 +01:00
|
|
|
local ok, loaded_plugin = core.try(require, "plugins." .. plugin.name)
|
2022-05-06 00:02:00 +02:00
|
|
|
if ok then
|
2023-02-02 01:28:21 +01:00
|
|
|
local plugin_version = ""
|
|
|
|
if plugin.version_string ~= MOD_VERSION_STRING then
|
|
|
|
plugin_version = "["..plugin.version_string.."]"
|
|
|
|
end
|
2022-05-06 00:02:00 +02:00
|
|
|
core.log_quiet(
|
2023-02-02 01:28:21 +01:00
|
|
|
"Loaded plugin %q%s from %s in %.1fms",
|
2022-05-06 00:02:00 +02:00
|
|
|
plugin.name,
|
2023-02-02 01:28:21 +01:00
|
|
|
plugin_version,
|
2022-05-06 00:02:00 +02:00
|
|
|
plugin.dir,
|
|
|
|
(system.get_time() - start) * 1000
|
|
|
|
)
|
|
|
|
end
|
2021-08-25 23:45:18 +02:00
|
|
|
if not ok then
|
|
|
|
no_errors = false
|
2023-01-19 16:14:45 +01:00
|
|
|
elseif config.plugins[plugin.name].onload then
|
|
|
|
core.try(config.plugins[plugin.name].onload, loaded_plugin)
|
2021-08-25 23:45:18 +02:00
|
|
|
end
|
2020-12-06 11:23:33 +01:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
2022-05-06 00:02:00 +02:00
|
|
|
core.log_quiet(
|
|
|
|
"Loaded all plugins in %.1fms",
|
|
|
|
(system.get_time() - load_start) * 1000
|
|
|
|
)
|
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")
|
2022-10-16 01:58:51 +02:00
|
|
|
-- Reset the IME even if the focus didn't change
|
|
|
|
ime.stop()
|
2020-05-19 13:58:41 +02:00
|
|
|
if view ~= core.active_view then
|
2023-04-07 18:42:46 +02:00
|
|
|
system.text_input(view:supports_text_input())
|
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
|
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
|
2022-05-31 02:35:56 +02:00
|
|
|
function core.custom_log(level, show, backtrace, 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(),
|
2022-05-31 02:26:18 +02:00
|
|
|
at = at,
|
2022-11-07 17:48:09 +01:00
|
|
|
info = backtrace and debug.traceback("", 2):gsub("\t", "")
|
2021-12-18 03:51:44 +01:00
|
|
|
}
|
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(...)
|
2022-05-31 02:35:56 +02:00
|
|
|
return core.custom_log("INFO", true, false, ...)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
function core.log_quiet(...)
|
2022-05-31 02:35:56 +02:00
|
|
|
return core.custom_log("INFO", false, false, ...)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
2022-05-31 02:26:42 +02:00
|
|
|
function core.warn(...)
|
2022-05-31 02:35:56 +02:00
|
|
|
return core.custom_log("WARN", true, true, ...)
|
2022-05-31 02:26:42 +02:00
|
|
|
end
|
2019-12-28 12:16:32 +01:00
|
|
|
|
|
|
|
function core.error(...)
|
2022-05-31 02:35:56 +02:00
|
|
|
return core.custom_log("ERROR", true, 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)
|
2022-11-07 17:48:09 +01:00
|
|
|
item.info = debug.traceback("", 2):gsub("\t", "")
|
2019-12-28 12:16:32 +01:00
|
|
|
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(...)
|
2022-10-16 01:58:51 +02:00
|
|
|
elseif type == "textediting" then
|
|
|
|
ime.on_text_editing(...)
|
2019-12-28 12:16:32 +01:00
|
|
|
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(...)
|
2022-04-29 03:50:34 +02:00
|
|
|
elseif type == "mouseleft" then
|
|
|
|
core.root_view:on_mouse_left()
|
2019-12-28 12:16:32 +01:00
|
|
|
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
|
2023-04-07 19:15:50 +02:00
|
|
|
elseif type == "touchpressed" then
|
|
|
|
core.root_view:on_touch_pressed(...)
|
|
|
|
elseif type == "touchreleased" then
|
|
|
|
core.root_view:on_touch_released(...)
|
|
|
|
elseif type == "touchmoved" then
|
|
|
|
core.root_view:on_touch_moved(...)
|
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
|
2022-02-15 00:45:59 +01:00
|
|
|
if not core.root_view:on_file_dropped(...) then
|
|
|
|
local filename, mx, my = ...
|
|
|
|
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
|
2020-05-23 12:31:08 +02:00
|
|
|
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()
|
2022-04-27 00:29:05 +02:00
|
|
|
if doc_filename ~= "---" then return doc_filename end
|
|
|
|
return ""
|
2021-02-18 00:26:19 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
|
2021-04-10 19:35:57 +02:00
|
|
|
function core.compose_window_title(title)
|
2022-04-27 00:29:05 +02:00
|
|
|
return (title == "" or title == nil) 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)
|
2023-04-07 18:58:56 +02:00
|
|
|
elseif type == "enteringforeground" then
|
|
|
|
-- to break our frame refresh in two if we get entering/entered at the same time.
|
|
|
|
-- required to avoid flashing and refresh issues on mobile
|
|
|
|
core.redraw = true
|
|
|
|
break
|
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)
|
2022-04-27 00:29:05 +02:00
|
|
|
if current_title ~= nil and 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
|
2023-03-28 18:57:47 +02:00
|
|
|
local minimal_time_to_wake = math.huge
|
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
|
2023-03-28 20:17:30 +02:00
|
|
|
else
|
2023-03-29 00:33:29 +02:00
|
|
|
wait = wait or (1/30)
|
2019-12-28 12:16:32 +01:00
|
|
|
thread.wake = system.get_time() + wait
|
2023-03-28 18:57:47 +02:00
|
|
|
minimal_time_to_wake = math.min(minimal_time_to_wake, wait)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2023-03-28 18:57:47 +02:00
|
|
|
else
|
|
|
|
minimal_time_to_wake = math.min(minimal_time_to_wake, thread.wake - system.get_time())
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- stop running threads if we're about to hit the end of frame
|
|
|
|
if system.get_time() - core.frame_start > max_time then
|
2023-03-28 18:57:47 +02:00
|
|
|
coroutine.yield(0)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-03-28 18:57:47 +02:00
|
|
|
coroutine.yield(minimal_time_to_wake)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
|
|
|
|
function core.run()
|
2023-03-28 18:57:47 +02:00
|
|
|
local next_step
|
2019-12-28 12:16:32 +01:00
|
|
|
while true do
|
|
|
|
core.frame_start = system.get_time()
|
2023-03-28 18:57:47 +02:00
|
|
|
local time_to_wake = run_threads()
|
|
|
|
local did_redraw = false
|
|
|
|
if not next_step or system.get_time() >= next_step then
|
|
|
|
did_redraw = core.step()
|
|
|
|
next_step = nil
|
|
|
|
end
|
2021-08-27 00:17:43 +02:00
|
|
|
if core.restart_request or core.quit_request then break end
|
2023-03-28 18:57:47 +02:00
|
|
|
|
|
|
|
if not did_redraw then
|
|
|
|
if system.window_has_focus() then
|
|
|
|
local now = system.get_time()
|
|
|
|
if not next_step then -- compute the time until the next blink
|
|
|
|
local t = now - core.blink_start
|
2021-04-17 23:57:04 +02:00
|
|
|
local h = config.blink_period / 2
|
|
|
|
local dt = math.ceil(t / h) * h - t
|
2023-03-28 18:57:47 +02:00
|
|
|
local cursor_time_to_wake = dt + 1 / config.fps
|
|
|
|
next_step = now + cursor_time_to_wake
|
2020-11-21 16:36:13 +01:00
|
|
|
end
|
2023-03-28 18:57:47 +02:00
|
|
|
if time_to_wake > 0 and system.wait_event(math.min(next_step - now, time_to_wake)) then
|
|
|
|
next_step = nil -- if we've recevied an event, perform a step
|
|
|
|
end
|
|
|
|
else
|
|
|
|
system.wait_event()
|
|
|
|
next_step = nil -- perform a step when we're not in focus if get we an event
|
2020-11-16 14:48:15 +01:00
|
|
|
end
|
2023-03-28 18:57:47 +02:00
|
|
|
else -- if we redrew, then make sure we only draw at most FPS/sec
|
|
|
|
local now = system.get_time()
|
|
|
|
local elapsed = now - core.frame_start
|
|
|
|
local next_frame = math.max(0, 1 / config.fps - elapsed)
|
|
|
|
next_step = next_step or (now + next_frame)
|
|
|
|
system.sleep(math.min(next_frame, time_to_wake))
|
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
|
2023-01-30 17:10:11 +01:00
|
|
|
local fp = io.open(USERDIR .. PATHSEP .. "error.txt", "wb")
|
2019-12-28 12:16:32 +01:00
|
|
|
fp:write("Error: " .. tostring(err) .. "\n")
|
2022-11-07 17:48:09 +01:00
|
|
|
fp:write(debug.traceback("", 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
|
|
|
|
|
|
|
|
|
2023-05-20 19:54:58 +02:00
|
|
|
local alerted_deprecations = {}
|
|
|
|
---Show deprecation notice once per `kind`.
|
|
|
|
---
|
|
|
|
---@param kind string
|
|
|
|
function core.deprecation_log(kind)
|
|
|
|
if alerted_deprecations[kind] then return end
|
|
|
|
alerted_deprecations[kind] = true
|
|
|
|
core.warn("Used deprecated functionality [%s]. Check if your plugins are up to date.", kind)
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
return core
|
2022-02-05 14:35:38 +01:00
|
|
|
|