Skip to content

Commit e6a116c

Browse files
committed
extract function
1 parent 034384d commit e6a116c

1 file changed

Lines changed: 20 additions & 23 deletions

File tree

mpvacious/helpers.lua

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -425,35 +425,32 @@ function this.find_mpv_script_opts_directory()
425425
return utils.join_path(this.find_mpv_config_directory(), "script-opts")
426426
end
427427

428+
local function searchpath_lua51_fallback(name, path)
429+
--- mpv may embed Lua 5.1/LuaJIT, whe-re package.searchpath is unavailable.
430+
--- This affects common builds such as Homebrew mpv on macOS, which links
431+
--- against a Lua 5.1-compatible runtime.
432+
local module_file = name:gsub("%.", "/")
433+
434+
for template in path:gmatch("[^;]+") do
435+
local candidate = template:gsub("%?", module_file)
436+
local file = io.open(candidate, "r")
437+
438+
if file ~= nil then
439+
file:close()
440+
return candidate
441+
end
442+
end
443+
444+
return nil
445+
end
446+
428447
function this.maybe_require(module_name)
429448
--- Example: ~/.config/mpv/scripts/mpvacious_custom_subtitle_filter/custom_subtitle_filter.lua
430449

431450
-- Make path to directory ~/.config/mpv/scripts/mpvacious_custom_subtitle_filter
432451
local external_scripts_path = utils.join_path(this.find_mpv_scripts_dir(), "mpvacious_" .. module_name)
433452
local search_template = external_scripts_path .. "/?.lua;"
434-
local searchpath = package.searchpath
435-
436-
-- mpv may embed Lua 5.1/LuaJIT, where package.searchpath is unavailable.
437-
-- This affects common builds such as Homebrew mpv on macOS, which links
438-
-- against a Lua 5.1-compatible runtime.
439-
if searchpath == nil then
440-
searchpath = function(name, path)
441-
local module_file = name:gsub("%.", "/")
442-
443-
for template in path:gmatch("[^;]+") do
444-
local candidate = template:gsub("%?", module_file)
445-
local file = io.open(candidate, "r")
446-
447-
if file ~= nil then
448-
file:close()
449-
return candidate
450-
end
451-
end
452-
453-
return nil
454-
end
455-
end
456-
453+
local searchpath = package.searchpath or searchpath_lua51_fallback
457454
local module_path = searchpath(module_name, search_template)
458455

459456
if not module_path then

0 commit comments

Comments
 (0)