2019-12-28 12:16:32 +01:00
|
|
|
local core = require "core"
|
|
|
|
local common = require "core.common"
|
|
|
|
local command = require "core.command"
|
|
|
|
local keymap = require "core.keymap"
|
|
|
|
local LogView = require "core.logview"
|
|
|
|
|
|
|
|
|
2020-03-25 23:44:59 +01:00
|
|
|
local fullscreen = false
|
2021-11-09 22:21:45 +01:00
|
|
|
local restore_title_view = false
|
2020-03-25 23:44:59 +01:00
|
|
|
|
2020-12-19 23:53:29 +01:00
|
|
|
local function suggest_directory(text)
|
2020-12-20 00:31:49 +01:00
|
|
|
text = common.home_expand(text)
|
2022-01-05 23:21:47 +01:00
|
|
|
local basedir = common.dirname(core.project_dir)
|
|
|
|
return common.home_encode_list((basedir and text == basedir .. PATHSEP or text == "") and
|
|
|
|
core.recent_projects or common.dir_path_suggest(text))
|
2020-12-19 16:31:42 +01:00
|
|
|
end
|
|
|
|
|
2022-01-09 20:22:39 +01:00
|
|
|
local function check_directory_path(path)
|
|
|
|
local abs_path = system.absolute_path(path)
|
|
|
|
local info = abs_path and system.get_file_info(abs_path)
|
|
|
|
if not info or info.type ~= 'dir' then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
return abs_path
|
|
|
|
end
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
command.add(nil, {
|
|
|
|
["core:quit"] = function()
|
|
|
|
core.quit()
|
|
|
|
end,
|
|
|
|
|
2020-12-10 12:44:01 +01:00
|
|
|
["core:restart"] = function()
|
|
|
|
core.restart()
|
|
|
|
end,
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
["core:force-quit"] = function()
|
|
|
|
core.quit(true)
|
|
|
|
end,
|
|
|
|
|
2020-03-25 23:44:59 +01:00
|
|
|
["core:toggle-fullscreen"] = function()
|
|
|
|
fullscreen = not fullscreen
|
2021-11-09 22:21:45 +01:00
|
|
|
if fullscreen then
|
|
|
|
restore_title_view = core.title_view.visible
|
|
|
|
end
|
2020-04-07 19:48:57 +02:00
|
|
|
system.set_window_mode(fullscreen and "fullscreen" or "normal")
|
2021-11-09 22:21:45 +01:00
|
|
|
core.show_title_bar(not fullscreen and restore_title_view)
|
|
|
|
core.title_view:configure_hit_test(not fullscreen and restore_title_view)
|
2020-03-25 23:44:59 +01:00
|
|
|
end,
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
["core:reload-module"] = function()
|
2022-05-30 22:06:47 +02:00
|
|
|
core.command_view:enter("Reload Module", {
|
|
|
|
submit = function(text, item)
|
|
|
|
local text = item and item.text or text
|
|
|
|
core.reload_module(text)
|
|
|
|
core.log("Reloaded module %q", text)
|
|
|
|
end,
|
|
|
|
suggest = function(text)
|
|
|
|
local items = {}
|
|
|
|
for name in pairs(package.loaded) do
|
|
|
|
table.insert(items, name)
|
|
|
|
end
|
|
|
|
return common.fuzzy_match(items, text)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
})
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2020-05-30 10:11:42 +02:00
|
|
|
["core:find-command"] = function()
|
2019-12-28 12:16:32 +01:00
|
|
|
local commands = command.get_all_valid()
|
2022-05-30 22:06:47 +02:00
|
|
|
core.command_view:enter("Do Command", {
|
|
|
|
submit = function(text, item)
|
|
|
|
if item then
|
|
|
|
command.perform(item.command)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
suggest = function(text)
|
|
|
|
local res = common.fuzzy_match(commands, text)
|
|
|
|
for i, name in ipairs(res) do
|
|
|
|
res[i] = {
|
|
|
|
text = command.prettify_name(name),
|
|
|
|
info = keymap.get_binding(name),
|
|
|
|
command = name,
|
|
|
|
}
|
|
|
|
end
|
|
|
|
return res
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
})
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2020-05-30 10:11:42 +02:00
|
|
|
["core:find-file"] = function()
|
2021-07-12 18:21:27 +02:00
|
|
|
if not core.project_files_number() then
|
|
|
|
return command.perform "core:open-file"
|
2021-05-25 23:25:56 +02:00
|
|
|
end
|
2020-11-16 23:52:39 +01:00
|
|
|
local files = {}
|
2020-12-27 18:28:21 +01:00
|
|
|
for dir, item in core.get_project_files() do
|
2020-11-16 23:52:39 +01:00
|
|
|
if item.type == "file" then
|
2020-12-28 10:26:27 +01:00
|
|
|
local path = (dir == core.project_dir and "" or dir .. PATHSEP)
|
|
|
|
table.insert(files, common.home_encode(path .. item.filename))
|
2020-11-16 23:52:39 +01:00
|
|
|
end
|
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
core.command_view:enter("Open File From Project", {
|
|
|
|
submit = function(text, item)
|
|
|
|
text = item and item.text or text
|
|
|
|
core.root_view:open_doc(core.open_doc(common.home_expand(text)))
|
|
|
|
end,
|
|
|
|
suggest = function(text)
|
|
|
|
return common.fuzzy_match_with_recents(files, core.visited_files, text)
|
|
|
|
end
|
|
|
|
})
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
2020-05-06 15:46:56 +02:00
|
|
|
["core:new-doc"] = function()
|
|
|
|
core.root_view:open_doc(core.open_doc())
|
|
|
|
end,
|
|
|
|
|
2021-10-10 03:21:49 +02:00
|
|
|
["core:new-named-doc"] = function()
|
2022-05-30 22:06:47 +02:00
|
|
|
core.command_view:enter("File name", {
|
|
|
|
submit = function(text)
|
2021-10-10 03:21:49 +02:00
|
|
|
core.root_view:open_doc(core.open_doc(text))
|
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
})
|
2021-10-10 03:21:49 +02:00
|
|
|
end,
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
["core:open-file"] = function()
|
2021-04-25 20:58:56 +02:00
|
|
|
local view = core.active_view
|
2022-06-01 19:34:46 +02:00
|
|
|
local text
|
2021-05-01 17:33:12 +02:00
|
|
|
if view.doc and view.doc.abs_filename then
|
2021-05-17 09:29:51 +02:00
|
|
|
local dirname, filename = view.doc.abs_filename:match("(.*)[/\\](.+)$")
|
2021-05-24 15:57:02 +02:00
|
|
|
if dirname then
|
|
|
|
dirname = core.normalize_to_project_dir(dirname)
|
2022-06-01 19:34:46 +02:00
|
|
|
text = dirname == core.project_dir and "" or common.home_encode(dirname) .. PATHSEP
|
2021-05-24 15:57:02 +02:00
|
|
|
end
|
2021-04-25 20:58:56 +02:00
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
core.command_view:enter("Open File", {
|
2022-06-01 19:34:46 +02:00
|
|
|
text = text,
|
2022-05-30 22:06:47 +02:00
|
|
|
submit = function(text)
|
|
|
|
local filename = system.absolute_path(common.home_expand(text))
|
|
|
|
core.root_view:open_doc(core.open_doc(filename))
|
|
|
|
end,
|
|
|
|
suggest = function (text)
|
|
|
|
return common.home_encode_list(common.path_suggest(common.home_expand(text)))
|
|
|
|
end,
|
|
|
|
validate = function(text)
|
|
|
|
local filename = common.home_expand(text)
|
|
|
|
local path_stat, err = system.get_file_info(filename)
|
|
|
|
if err then
|
|
|
|
if err:find("No such file", 1, true) then
|
|
|
|
-- check if the containing directory exists
|
|
|
|
local dirname = common.dirname(filename)
|
|
|
|
local dir_stat = dirname and system.get_file_info(dirname)
|
|
|
|
if not dirname or (dir_stat and dir_stat.type == 'dir') then
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
core.error("Cannot open file %s: %s", text, err)
|
|
|
|
elseif path_stat.type == 'dir' then
|
|
|
|
core.error("Cannot open %s, is a folder", text)
|
|
|
|
else
|
2021-07-20 20:39:50 +02:00
|
|
|
return true
|
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
end,
|
|
|
|
})
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
["core:open-log"] = function()
|
2022-05-04 02:58:34 +02:00
|
|
|
local node = core.root_view:get_active_node_default()
|
2019-12-28 12:16:32 +01:00
|
|
|
node:add_view(LogView())
|
|
|
|
end,
|
2020-05-06 15:46:56 +02:00
|
|
|
|
|
|
|
["core:open-user-module"] = function()
|
2020-12-10 17:56:53 +01:00
|
|
|
local user_module_doc = core.open_doc(USERDIR .. "/init.lua")
|
|
|
|
if not user_module_doc then return end
|
|
|
|
core.root_view:open_doc(user_module_doc)
|
2020-05-06 15:46:56 +02:00
|
|
|
end,
|
|
|
|
|
|
|
|
["core:open-project-module"] = function()
|
2022-01-04 00:25:14 +01:00
|
|
|
if not system.get_file_info(".lite_project.lua") then
|
|
|
|
core.try(core.write_init_project_module, ".lite_project.lua")
|
|
|
|
end
|
2021-12-30 12:11:01 +01:00
|
|
|
local doc = core.open_doc(".lite_project.lua")
|
|
|
|
core.root_view:open_doc(doc)
|
|
|
|
doc:save()
|
2020-05-06 15:46:56 +02:00
|
|
|
end,
|
2020-12-08 16:57:24 +01:00
|
|
|
|
2020-12-10 17:56:53 +01:00
|
|
|
["core:change-project-folder"] = function()
|
2021-08-27 15:22:09 +02:00
|
|
|
local dirname = common.dirname(core.project_dir)
|
2022-06-01 19:34:46 +02:00
|
|
|
local text
|
2021-09-07 14:21:45 +02:00
|
|
|
if dirname then
|
2022-06-01 19:34:46 +02:00
|
|
|
text = common.home_encode(dirname) .. PATHSEP
|
2021-09-07 14:21:45 +02:00
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
core.command_view:enter("Change Project Folder", {
|
2022-06-01 19:34:46 +02:00
|
|
|
text = text,
|
2022-05-30 22:06:47 +02:00
|
|
|
submit = function(text)
|
|
|
|
local path = common.home_expand(text)
|
|
|
|
local abs_path = check_directory_path(path)
|
|
|
|
if not abs_path then
|
|
|
|
core.error("Cannot open directory %q", path)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if abs_path == core.project_dir then return end
|
|
|
|
core.confirm_close_docs(core.docs, function(dirpath)
|
|
|
|
core.open_folder_project(dirpath)
|
|
|
|
end, abs_path)
|
|
|
|
end,
|
|
|
|
suggest = suggest_directory
|
|
|
|
})
|
2020-12-08 16:57:24 +01:00
|
|
|
end,
|
2020-12-10 17:56:53 +01:00
|
|
|
|
|
|
|
["core:open-project-folder"] = function()
|
2021-09-07 15:23:41 +02:00
|
|
|
local dirname = common.dirname(core.project_dir)
|
2022-06-01 19:34:46 +02:00
|
|
|
local text
|
2021-09-07 15:23:41 +02:00
|
|
|
if dirname then
|
2022-06-01 19:34:46 +02:00
|
|
|
text = common.home_encode(dirname) .. PATHSEP
|
2021-09-07 15:23:41 +02:00
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
core.command_view:enter("Open Project", {
|
2022-06-01 19:34:46 +02:00
|
|
|
text = text,
|
2022-05-30 22:06:47 +02:00
|
|
|
submit = function(text)
|
|
|
|
local path = common.home_expand(text)
|
|
|
|
local abs_path = check_directory_path(path)
|
|
|
|
if not abs_path then
|
|
|
|
core.error("Cannot open directory %q", path)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if abs_path == core.project_dir then
|
|
|
|
core.error("Directory %q is currently opened", abs_path)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
system.exec(string.format("%q %q", EXEFILE, abs_path))
|
|
|
|
end,
|
|
|
|
suggest = suggest_directory
|
|
|
|
})
|
2020-12-10 17:56:53 +01:00
|
|
|
end,
|
2020-12-27 11:32:52 +01:00
|
|
|
|
|
|
|
["core:add-directory"] = function()
|
2022-05-30 22:06:47 +02:00
|
|
|
core.command_view:enter("Add Directory", {
|
|
|
|
submit = function(text)
|
|
|
|
text = common.home_expand(text)
|
|
|
|
local path_stat, err = system.get_file_info(text)
|
|
|
|
if not path_stat then
|
|
|
|
core.error("cannot open %q: %s", text, err)
|
|
|
|
return
|
|
|
|
elseif path_stat.type ~= 'dir' then
|
|
|
|
core.error("%q is not a directory", text)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
core.add_project_directory(system.absolute_path(text))
|
|
|
|
end,
|
|
|
|
suggest = suggest_directory
|
|
|
|
})
|
2020-12-27 11:32:52 +01:00
|
|
|
end,
|
|
|
|
|
2020-12-28 15:51:16 +01:00
|
|
|
["core:remove-directory"] = function()
|
|
|
|
local dir_list = {}
|
|
|
|
local n = #core.project_directories
|
2021-01-03 12:02:07 +01:00
|
|
|
for i = n, 2, -1 do
|
2020-12-28 15:51:16 +01:00
|
|
|
dir_list[n - i + 1] = core.project_directories[i].name
|
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
core.command_view:enter("Remove Directory", {
|
|
|
|
submit = function(text, item)
|
|
|
|
text = common.home_expand(item and item.text or text)
|
|
|
|
if not core.remove_project_directory(text) then
|
|
|
|
core.error("No directory %q to be removed", text)
|
|
|
|
end
|
|
|
|
end,
|
|
|
|
suggest = function(text)
|
|
|
|
text = common.home_expand(text)
|
|
|
|
return common.home_encode_list(common.dir_list_suggest(text, dir_list))
|
2020-12-28 15:51:16 +01:00
|
|
|
end
|
2022-05-30 22:06:47 +02:00
|
|
|
})
|
2020-12-28 15:51:16 +01:00
|
|
|
end,
|
2019-12-28 12:16:32 +01:00
|
|
|
})
|