Implement user's config as init file and add directory into package.path

Create the user's config init file if lite user's config directory does
not exists.

No longer use the awkward package.searchers but instead add user's
config dir at the end of package path.
This commit is contained in:
Francesco Abbate 2020-12-02 16:03:31 +01:00
parent 3589d7d3c0
commit a337f893d9
7 changed files with 84 additions and 29 deletions

View File

@ -86,6 +86,61 @@ local function project_scan_thread()
end
-- create a directory using mkdir but may need to create the parent
-- directories as well.
local function create_user_directory()
local dirname_create = USERDIR
local basedir
local subdirs = {}
while dirname_create and dirname_create ~= "" do
local success_mkdir = system.mkdir(dirname_create)
if success_mkdir then break end
dirname_create, basedir = dirname_create:match("(.*)[/\\](.+)$")
if basedir then
subdirs[#subdirs + 1] = basedir
end
end
for _, dirname in ipairs(subdirs) do
dirname_create = dirname_create .. '/' .. dirname
local success_mkdir = system.mkdir(dirname_create)
if not success_mkdir then error("cannot create directory: \"" .. dirname_create .. "\"") end
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
local keymap = require "core.keymap"
local config = require "core.config"
local style = require "core.style"
-- light theme:
-- require "colors.summer"
-- key binding:
-- keymap.add { ["ctrl+escape"] = "core:quit" }
]])
init_file:close()
end
local function load_user_directory()
local init_filename = USERDIR .. "/init.lua"
local info = system.get_file_info(USERDIR)
if not info then
create_user_directory()
write_user_init_file(init_filename)
end
return dofile(init_filename)
end
function core.init()
command = require "core.command"
keymap = require "core.keymap"
@ -126,7 +181,7 @@ function core.init()
core.add_thread(project_scan_thread)
command.add_defaults()
local got_plugin_error = not core.load_plugins()
local got_user_error = not core.try(require, "user")
local got_user_error = not core.try(load_user_directory)
local got_project_error = not core.load_project_module()
for _, filename in ipairs(files) do

View File

@ -6,7 +6,7 @@ local config = require "core.config"
local style = require "core.style"
-- light theme:
-- require "user.colors.summer"
-- require "colors.summer"
-- key binding:
-- keymap.add { ["ctrl+escape"] = "core:quit" }

View File

@ -20,9 +20,9 @@ if host_machine.system() == 'windows'
endif
lite_include = include_directories('src')
install_subdir('data/core', install_dir : 'share/lite-xl')
install_subdir('data/fonts', install_dir : 'share/lite-xl')
install_subdir('data/plugins', install_dir : 'share/lite-xl')
foreach data_module : ['core', 'fonts', 'plugins', 'colors']
install_subdir('data' / data_module , install_dir : 'share/lite-xl')
endforeach
lite_link_args = []
if get_option('buildtype') == 'release'

View File

@ -8,6 +8,7 @@
#include "api.h"
#include "rencache.h"
#ifdef _WIN32
#include <direct.h>
#include <windows.h>
#endif
@ -314,6 +315,25 @@ static int f_get_file_info(lua_State *L) {
}
static int f_mkdir(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
#ifdef _WIN32
int err = _mkdir(path);
#else
int err = mkdir(path, S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH);
#endif
if (err < 0) {
lua_pushboolean(L, 0);
lua_pushstring(L, strerror(errno));
return 2;
}
lua_pushboolean(L, 1);
return 1;
}
static int f_get_clipboard(lua_State *L) {
char *text = SDL_GetClipboardText();
if (!text) { return 0; }
@ -397,6 +417,7 @@ static const luaL_Reg lib[] = {
{ "window_has_focus", f_window_has_focus },
{ "show_confirm_dialog", f_show_confirm_dialog },
{ "chdir", f_chdir },
{ "mkdir", f_mkdir },
{ "list_dir", f_list_dir },
{ "absolute_path", f_absolute_path },
{ "get_file_info", f_get_file_info },

View File

@ -125,34 +125,13 @@ int main(int argc, char **argv) {
" EXEDIR = EXEFILE:match(\"^(.+)[/\\\\].*$\")\n"
#ifdef LITE_XL_DATA_USE_EXEDIR
" DATADIR = EXEDIR .. '/data'\n"
" USERDIR = EXEDIR .. '/user'\n"
#else
" local prefix = EXEDIR:match(\"^(.+)[/\\\\]bin$\")\n"
" DATADIR = prefix and (prefix .. '/share/lite-xl') or (EXEDIR .. '/data')\n"
" USERDIR = os.getenv(\"HOME\") .. '/.config/lite-xl'\n"
"table.insert(package.searchers, 1, function(module_name)\n"
" if module_name == 'user' or module_name:match('^user%.') then\n"
" return function(x_module_name)\n"
" local filename, err\n"
" if x_module_name == 'user' then\n"
" filename = USERDIR..'/init.lua'\n"
" else\n"
" local modname = x_module_name:gsub('^user%.', '')\n"
" filename, err = package.searchpath(modname, USERDIR..'/?.lua;'..USERDIR..'/?/init.lua')\n"
" end\n"
" if filename then\n"
" local module_f, module_err = loadfile(filename)\n"
" if module_f then\n"
" return module_f()\n"
" else\n"
" return nil, module_err\n"
" end\n"
" else\n"
" return nil, err\n"
" end\n"
" end\n"
" end\n"
"end)\n"
" package.path = package.path .. ';' .. DATADIR .. '/?.lua'\n"
" package.path = package.path .. ';' .. DATADIR .. '/?/init.lua'\n"
#endif
" package.path = DATADIR .. '/?.lua;' .. package.path\n"
" package.path = DATADIR .. '/?/init.lua;' .. package.path\n"