2021-05-27 13:46:51 +02:00
|
|
|
-- this file is used by lite-xl to setup the Lua environment when starting
|
2021-08-09 19:32:53 +02:00
|
|
|
VERSION = "@PROJECT_VERSION@"
|
2023-02-02 01:28:21 +01:00
|
|
|
MOD_VERSION_MAJOR = 3
|
|
|
|
MOD_VERSION_MINOR = 0
|
|
|
|
MOD_VERSION_PATCH = 0
|
|
|
|
MOD_VERSION_STRING = string.format("%d.%d.%d", MOD_VERSION_MAJOR, MOD_VERSION_MINOR, MOD_VERSION_PATCH)
|
2021-02-24 16:29:39 +01:00
|
|
|
|
2023-09-30 16:48:38 +02:00
|
|
|
SCALE = tonumber(os.getenv("LITE_SCALE") or os.getenv("GDK_SCALE") or os.getenv("QT_SCALE_FACTOR")) or 1
|
2021-02-24 16:29:39 +01:00
|
|
|
PATHSEP = package.config:sub(1, 1)
|
|
|
|
|
2021-04-18 17:51:31 +02:00
|
|
|
EXEDIR = EXEFILE:match("^(.+)[/\\][^/\\]+$")
|
|
|
|
if MACOS_RESOURCES then
|
|
|
|
DATADIR = MACOS_RESOURCES
|
|
|
|
else
|
2023-02-16 02:48:09 +01:00
|
|
|
local prefix = os.getenv('LITE_PREFIX') or EXEDIR:match("^(.+)[/\\]bin$")
|
2022-10-08 20:48:30 +02:00
|
|
|
DATADIR = prefix and (prefix .. PATHSEP .. 'share' .. PATHSEP .. 'lite-xl') or (EXEDIR .. PATHSEP .. 'data')
|
2021-04-18 17:51:31 +02:00
|
|
|
end
|
2022-10-08 20:48:30 +02:00
|
|
|
USERDIR = (system.get_file_info(EXEDIR .. PATHSEP .. 'user') and (EXEDIR .. PATHSEP .. 'user'))
|
|
|
|
or os.getenv("LITE_USERDIR")
|
|
|
|
or ((os.getenv("XDG_CONFIG_HOME") and os.getenv("XDG_CONFIG_HOME") .. PATHSEP .. "lite-xl"))
|
|
|
|
or (HOME and (HOME .. PATHSEP .. '.config' .. PATHSEP .. 'lite-xl'))
|
2021-02-24 16:29:39 +01:00
|
|
|
|
2022-08-03 17:13:26 +02:00
|
|
|
package.path = DATADIR .. '/?.lua;'
|
2021-02-24 16:29:39 +01:00
|
|
|
package.path = DATADIR .. '/?/init.lua;' .. package.path
|
2021-03-03 12:20:22 +01:00
|
|
|
package.path = USERDIR .. '/?.lua;' .. package.path
|
|
|
|
package.path = USERDIR .. '/?/init.lua;' .. package.path
|
2021-11-17 01:37:37 +01:00
|
|
|
|
2023-07-20 03:06:18 +02:00
|
|
|
local suffix = PLATFORM == "Windows" and 'dll' or 'so'
|
2022-09-25 23:08:21 +02:00
|
|
|
package.cpath =
|
2022-09-25 23:13:07 +02:00
|
|
|
USERDIR .. '/?.' .. ARCH .. "." .. suffix .. ";" ..
|
|
|
|
USERDIR .. '/?/init.' .. ARCH .. "." .. suffix .. ";" ..
|
|
|
|
USERDIR .. '/?.' .. suffix .. ";" ..
|
|
|
|
USERDIR .. '/?/init.' .. suffix .. ";" ..
|
|
|
|
DATADIR .. '/?.' .. ARCH .. "." .. suffix .. ";" ..
|
|
|
|
DATADIR .. '/?/init.' .. ARCH .. "." .. suffix .. ";" ..
|
|
|
|
DATADIR .. '/?.' .. suffix .. ";" ..
|
|
|
|
DATADIR .. '/?/init.' .. suffix .. ";"
|
|
|
|
|
2021-09-21 05:42:39 +02:00
|
|
|
package.native_plugins = {}
|
2021-09-22 23:25:16 +02:00
|
|
|
package.searchers = { package.searchers[1], package.searchers[2], function(modname)
|
2023-04-26 02:29:57 +02:00
|
|
|
local path, err = package.searchpath(modname, package.cpath)
|
|
|
|
if not path then return err end
|
2021-09-21 05:33:12 +02:00
|
|
|
return system.load_native_plugin, path
|
2021-09-22 23:24:22 +02:00
|
|
|
end }
|
2021-11-17 01:42:08 +01:00
|
|
|
|
2021-11-17 01:37:37 +01:00
|
|
|
table.pack = table.pack or pack or function(...) return {...} end
|
|
|
|
table.unpack = table.unpack or unpack
|
2022-01-12 00:07:53 +01:00
|
|
|
|
2024-10-22 18:31:45 +02:00
|
|
|
local lua_require = require
|
|
|
|
local require_stack = { "" }
|
|
|
|
---Loads the given module, returns any value returned by the searcher (`true` when `nil`).
|
|
|
|
---Besides that value, also returns as a second result the loader data returned by the searcher,
|
|
|
|
---which indicates how `require` found the module.
|
|
|
|
---(For instance, if the module came from a file, this loader data is the file path.)
|
|
|
|
---
|
|
|
|
---This is a variant that also supports relative imports.
|
|
|
|
---
|
|
|
|
---For example `require ".b"` will require `b` in the same path of the current
|
|
|
|
---file.
|
|
|
|
---This also supports multiple levels traversal. For example `require "...b"`
|
|
|
|
---will require `b` from two levels above the current one.
|
|
|
|
---This method has a few caveats: it uses the last `require` call to get the
|
|
|
|
---current "path", so this only works if the relative `require` is called inside
|
|
|
|
---its parent `require`.
|
|
|
|
---Calling a relative `require` in a function called outside the parent
|
|
|
|
---`require`, will result in the wrong "path" being used.
|
|
|
|
---
|
|
|
|
---It's possible to save the current "path" with `get_current_require_path`
|
|
|
|
---called inside the parent `require`, and use its return value to populate
|
|
|
|
---future requires.
|
|
|
|
---@see get_current_require_path
|
|
|
|
---@param modname string
|
|
|
|
---@return unknown
|
|
|
|
---@return unknown loaderdata
|
|
|
|
function require(modname, ...)
|
|
|
|
if modname then
|
|
|
|
local level, rel_path = string.match(modname, "^(%.*)(.*)")
|
|
|
|
level = #(level or "")
|
|
|
|
if level > 0 then
|
|
|
|
if #require_stack == 0 then
|
|
|
|
return error("Require stack underflowed.")
|
|
|
|
else
|
|
|
|
local base_path = require_stack[#require_stack]
|
|
|
|
while level > 1 do
|
|
|
|
base_path = string.match(base_path, "^(.*)%.") or ""
|
|
|
|
level = level - 1
|
|
|
|
end
|
|
|
|
modname = base_path
|
|
|
|
if #base_path > 0 then
|
|
|
|
modname = modname .. "."
|
|
|
|
end
|
|
|
|
modname = modname .. rel_path
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
table.insert(require_stack, modname)
|
|
|
|
local ok, result, loaderdata = pcall(lua_require, modname, ...)
|
|
|
|
table.remove(require_stack)
|
|
|
|
|
|
|
|
if not ok then
|
|
|
|
return error(result)
|
|
|
|
end
|
|
|
|
return result, loaderdata
|
|
|
|
end
|
|
|
|
|
|
|
|
---Returns the current `require` path.
|
|
|
|
---@see require for details and caveats
|
|
|
|
---@return string
|
|
|
|
function get_current_require_path()
|
|
|
|
return require_stack[#require_stack]
|
|
|
|
end
|
|
|
|
|
2022-01-12 00:07:53 +01:00
|
|
|
bit32 = bit32 or require "core.bit"
|
2022-04-20 23:00:48 +02:00
|
|
|
|
2022-04-26 15:42:02 +02:00
|
|
|
require "core.utf8string"
|
2024-06-22 18:43:05 +02:00
|
|
|
require "core.process"
|
2022-04-26 15:42:02 +02:00
|
|
|
|
2022-04-20 23:00:48 +02:00
|
|
|
-- Because AppImages change the working directory before running the executable,
|
|
|
|
-- we need to change it back to the original one.
|
|
|
|
-- https://github.com/AppImage/AppImageKit/issues/172
|
|
|
|
-- https://github.com/AppImage/AppImageKit/pull/191
|
|
|
|
local appimage_owd = os.getenv("OWD")
|
|
|
|
if os.getenv("APPIMAGE") and appimage_owd then
|
|
|
|
system.chdir(appimage_owd)
|
|
|
|
end
|