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
|
|
|
|
|
2020-12-19 23:53:29 +01:00
|
|
|
local function home_encode_list(paths)
|
2020-12-20 00:31:49 +01:00
|
|
|
local t = {}
|
|
|
|
for i = 1, #paths do
|
|
|
|
t[i] = common.home_encode(paths[i])
|
2020-12-19 16:31:42 +01:00
|
|
|
end
|
2020-12-20 00:31:49 +01:00
|
|
|
return t
|
2020-12-19 16:31:42 +01:00
|
|
|
end
|
|
|
|
|
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)
|
2020-12-19 23:53:29 +01:00
|
|
|
return home_encode_list(text == "" and core.recent_projects or common.dir_path_suggest(text))
|
2020-12-19 16:31:42 +01:00
|
|
|
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
|
2020-04-07 19:48:57 +02:00
|
|
|
system.set_window_mode(fullscreen and "fullscreen" or "normal")
|
2020-03-25 23:44:59 +01:00
|
|
|
end,
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
["core:reload-module"] = function()
|
|
|
|
core.command_view:enter("Reload Module", function(text, item)
|
|
|
|
local text = item and item.text or text
|
|
|
|
core.reload_module(text)
|
|
|
|
core.log("Reloaded module %q", text)
|
|
|
|
end, function(text)
|
|
|
|
local items = {}
|
|
|
|
for name in pairs(package.loaded) do
|
|
|
|
table.insert(items, name)
|
|
|
|
end
|
|
|
|
return common.fuzzy_match(items, text)
|
|
|
|
end)
|
|
|
|
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()
|
|
|
|
core.command_view:enter("Do Command", function(text, item)
|
|
|
|
if item then
|
|
|
|
command.perform(item.command)
|
|
|
|
end
|
|
|
|
end, 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
|
|
|
|
end)
|
|
|
|
end,
|
|
|
|
|
2020-05-30 10:11:42 +02:00
|
|
|
["core:find-file"] = function()
|
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
|
2020-05-06 15:46:56 +02:00
|
|
|
core.command_view:enter("Open File From Project", function(text, item)
|
2020-12-27 11:32:52 +01:00
|
|
|
core.root_view:open_doc(core.open_doc(common.home_expand(text)))
|
2019-12-28 12:16:32 +01:00
|
|
|
end, function(text)
|
2020-11-16 23:52:39 +01:00
|
|
|
if text == "" then
|
|
|
|
local recent_files = {}
|
|
|
|
for i = 2, #core.visited_files do
|
|
|
|
table.insert(recent_files, core.visited_files[i])
|
|
|
|
end
|
|
|
|
table.insert(recent_files, core.visited_files[1])
|
|
|
|
local other_files = common.fuzzy_match(files, "")
|
|
|
|
for i = 1, #other_files do
|
|
|
|
table.insert(recent_files, other_files[i])
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
2020-11-16 23:52:39 +01:00
|
|
|
return recent_files
|
|
|
|
else
|
|
|
|
return common.fuzzy_match(files, text)
|
2019-12-28 12:16:32 +01:00
|
|
|
end
|
|
|
|
end)
|
|
|
|
end,
|
|
|
|
|
2020-05-06 15:46:56 +02:00
|
|
|
["core:new-doc"] = function()
|
|
|
|
core.root_view:open_doc(core.open_doc())
|
|
|
|
end,
|
|
|
|
|
2019-12-28 12:16:32 +01:00
|
|
|
["core:open-file"] = function()
|
|
|
|
core.command_view:enter("Open File", function(text)
|
2020-12-20 00:31:49 +01:00
|
|
|
core.root_view:open_doc(core.open_doc(common.home_expand(text)))
|
|
|
|
end, function (text)
|
|
|
|
return home_encode_list(common.path_suggest(common.home_expand(text)))
|
|
|
|
end)
|
2019-12-28 12:16:32 +01:00
|
|
|
end,
|
|
|
|
|
|
|
|
["core:open-log"] = function()
|
|
|
|
local node = core.root_view:get_active_node()
|
|
|
|
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
|
|
|
|
local doc_save = user_module_doc.save
|
|
|
|
user_module_doc.save = function(self)
|
|
|
|
doc_save(self)
|
|
|
|
core.reload_module("core.style")
|
|
|
|
core.load_user_directory()
|
|
|
|
end
|
|
|
|
core.root_view:open_doc(user_module_doc)
|
2020-05-06 15:46:56 +02:00
|
|
|
end,
|
|
|
|
|
|
|
|
["core:open-project-module"] = function()
|
2020-05-17 17:59:19 +02:00
|
|
|
local filename = ".lite_project.lua"
|
2020-05-06 15:46:56 +02:00
|
|
|
if system.get_file_info(filename) then
|
|
|
|
core.root_view:open_doc(core.open_doc(filename))
|
|
|
|
else
|
|
|
|
local doc = core.open_doc()
|
|
|
|
core.root_view:open_doc(doc)
|
|
|
|
doc:save(filename)
|
|
|
|
end
|
|
|
|
end,
|
2020-12-08 16:57:24 +01:00
|
|
|
|
2020-12-10 17:56:53 +01:00
|
|
|
["core:change-project-folder"] = function()
|
|
|
|
core.command_view:enter("Change Project Folder", function(text)
|
2020-12-19 23:53:29 +01:00
|
|
|
text = common.home_expand(text)
|
2020-12-08 16:57:24 +01:00
|
|
|
local path_stat = system.get_file_info(text)
|
|
|
|
if not path_stat or path_stat.type ~= 'dir' then
|
|
|
|
core.error("Cannot open folder %q", text)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
if core.confirm_close_all() then
|
|
|
|
core.open_folder_project(text)
|
|
|
|
end
|
2020-12-19 16:31:42 +01:00
|
|
|
end, suggest_directory)
|
2020-12-08 16:57:24 +01:00
|
|
|
end,
|
2020-12-10 17:56:53 +01:00
|
|
|
|
|
|
|
["core:open-project-folder"] = function()
|
|
|
|
core.command_view:enter("Open Project", function(text)
|
2020-12-19 23:53:29 +01:00
|
|
|
text = common.home_expand(text)
|
2020-12-10 17:56:53 +01:00
|
|
|
local path_stat = system.get_file_info(text)
|
|
|
|
if not path_stat or path_stat.type ~= 'dir' then
|
|
|
|
core.error("Cannot open folder %q", text)
|
|
|
|
return
|
|
|
|
end
|
|
|
|
system.exec(string.format("%q %q", EXEFILE, text))
|
2020-12-19 16:31:42 +01:00
|
|
|
end, suggest_directory)
|
2020-12-10 17:56:53 +01:00
|
|
|
end,
|
2020-12-27 11:32:52 +01:00
|
|
|
|
|
|
|
["core:add-directory"] = function()
|
2020-12-28 15:51:16 +01:00
|
|
|
core.command_view:enter("Add Directory", function(text)
|
2020-12-27 11:32:52 +01:00
|
|
|
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))
|
2020-12-27 18:33:03 +01:00
|
|
|
-- TODO: add the name of directory to prioritize
|
2020-12-27 11:32:52 +01:00
|
|
|
core.request_project_scan()
|
|
|
|
end, suggest_directory)
|
|
|
|
end,
|
|
|
|
|
2020-12-28 15:51:16 +01:00
|
|
|
["core:remove-directory"] = function()
|
|
|
|
local dir_list = {}
|
|
|
|
local n = #core.project_directories
|
|
|
|
for i = n, 1, -1 do
|
|
|
|
dir_list[n - i + 1] = core.project_directories[i].name
|
|
|
|
end
|
|
|
|
core.command_view:enter("Remove Directory", function(text)
|
|
|
|
if not core.remove_project_directory(text) then
|
|
|
|
core.error("The project has no directory %q", text)
|
|
|
|
end
|
|
|
|
end, function(text)
|
|
|
|
text = common.home_expand(text)
|
|
|
|
return home_encode_list(common.dir_list_suggest(text, dir_list))
|
|
|
|
end)
|
|
|
|
end,
|
2019-12-28 12:16:32 +01:00
|
|
|
})
|