summaryrefslogtreecommitdiffstats
path: root/TOOLS/lua/autoload.lua
diff options
context:
space:
mode:
Diffstat (limited to 'TOOLS/lua/autoload.lua')
-rw-r--r--TOOLS/lua/autoload.lua229
1 files changed, 117 insertions, 112 deletions
diff --git a/TOOLS/lua/autoload.lua b/TOOLS/lua/autoload.lua
index 593d375431..dedfc64e85 100644
--- a/TOOLS/lua/autoload.lua
+++ b/TOOLS/lua/autoload.lua
@@ -1,5 +1,5 @@
-- This script automatically loads playlist entries before and after the
--- the currently played file. It does so by scanning the directory a file is
+-- currently played file. It does so by scanning the directory a file is
-- located in when starting playback. It sorts the directory entries
-- alphabetically, and adds entries before and after the current file to
-- the internal playlist. (It stops if it would add an already existing
@@ -32,14 +32,14 @@ ignore_patterns=^~,^bak-,%.bak$
--]]
-MAXENTRIES = 5000
-MAXDIRSTACK = 20
+local MAX_ENTRIES = 5000
+local MAX_DIR_STACK = 20
local msg = require 'mp.msg'
local options = require 'mp.options'
local utils = require 'mp.utils'
-o = {
+local o = {
disabled = false,
images = true,
videos = true,
@@ -52,37 +52,43 @@ o = {
directory_mode = "auto",
ignore_patterns = ""
}
-options.read_options(o, nil, function(list)
- split_option_exts(list.additional_video_exts, list.additional_audio_exts, list.additional_image_exts)
- if list.videos or list.additional_video_exts or
- list.audio or list.additional_audio_exts or
- list.images or list.additional_image_exts then
- create_extensions()
- end
- if list.directory_mode then
- validate_directory_mode()
- end
-end)
-function Set (t)
+local function Set(t)
local set = {}
for _, v in pairs(t) do set[v] = true end
return set
end
-function SetUnion (a,b)
+local EXTENSIONS_VIDEO_DEFAULT = Set {
+ '3g2', '3gp', 'avi', 'flv', 'm2ts', 'm4v', 'mj2', 'mkv', 'mov',
+ 'mp4', 'mpeg', 'mpg', 'ogv', 'rmvb', 'webm', 'wmv', 'y4m'
+}
+
+local EXTENSIONS_AUDIO_DEFAULT = Set {
+ 'aiff', 'ape', 'au', 'flac', 'm4a', 'mka', 'mp3', 'oga', 'ogg',
+ 'ogm', 'opus', 'wav', 'wma'
+}
+
+local EXTENSIONS_IMAGES_DEFAULT = Set {
+ 'avif', 'bmp', 'gif', 'j2k', 'jp2', 'jpeg', 'jpg', 'jxl', 'png',
+ 'svg', 'tga', 'tif', 'tiff', 'webp'
+}
+
+local EXTENSIONS, EXTENSIONS_VIDEO, EXTENSIONS_AUDIO, EXTENSIONS_IMAGES
+
+local function SetUnion(a, b)
for k in pairs(b) do a[k] = true end
return a
end
-- Returns first and last positions in string or past-to-end indices
-function FindOrPastTheEnd (string, pattern, start_at)
- local pos1, pos2 = string.find(string, pattern, start_at)
+local function FindOrPastTheEnd(string, pattern, start_at)
+ local pos1, pos2 = string:find(pattern, start_at)
return pos1 or #string + 1,
pos2 or #string + 1
end
-function Split (list)
+local function Split(list)
local set = {}
local item_pos = 1
@@ -117,34 +123,17 @@ function Split (list)
return set
end
-EXTENSIONS_VIDEO_DEFAULT = Set {
- '3g2', '3gp', 'avi', 'flv', 'm2ts', 'm4v', 'mj2', 'mkv', 'mov',
- 'mp4', 'mpeg', 'mpg', 'ogv', 'rmvb', 'webm', 'wmv', 'y4m'
-}
-
-EXTENSIONS_AUDIO_DEFAULT = Set {
- 'aiff', 'ape', 'au', 'flac', 'm4a', 'mka', 'mp3', 'oga', 'ogg',
- 'ogm', 'opus', 'wav', 'wma'
-}
-
-EXTENSIONS_IMAGES_DEFAULT = Set {
- 'avif', 'bmp', 'gif', 'j2k', 'jp2', 'jpeg', 'jpg', 'jxl', 'png',
- 'svg', 'tga', 'tif', 'tiff', 'webp'
-}
-
-function split_option_exts(video, audio, image)
+local function split_option_exts(video, audio, image)
if video then o.additional_video_exts = Split(o.additional_video_exts) end
if audio then o.additional_audio_exts = Split(o.additional_audio_exts) end
if image then o.additional_image_exts = Split(o.additional_image_exts) end
end
-split_option_exts(true, true, true)
-function split_patterns()
+local function split_patterns()
o.ignore_patterns = Split(o.ignore_patterns)
end
-split_patterns()
-function create_extensions()
+local function create_extensions()
EXTENSIONS = {}
EXTENSIONS_VIDEO = {}
EXTENSIONS_AUDIO = {}
@@ -162,16 +151,36 @@ function create_extensions()
SetUnion(EXTENSIONS, EXTENSIONS_IMAGES)
end
end
-create_extensions()
-function validate_directory_mode()
- if o.directory_mode ~= "recursive" and o.directory_mode ~= "lazy" and o.directory_mode ~= "ignore" then
+local function validate_directory_mode()
+ if o.directory_mode ~= "recursive" and o.directory_mode ~= "lazy"
+ and o.directory_mode ~= "ignore" then
o.directory_mode = nil
end
end
+
+options.read_options(o, nil, function(list)
+ split_option_exts(list.additional_video_exts, list.additional_audio_exts,
+ list.additional_image_exts)
+ if list.videos or list.additional_video_exts or
+ list.audio or list.additional_audio_exts or
+ list.images or list.additional_image_exts then
+ create_extensions()
+ end
+ if list.directory_mode then
+ validate_directory_mode()
+ end
+ if list.ignore_patterns then
+ split_patterns()
+ end
+end)
+
+split_option_exts(true, true, true)
+split_patterns()
+create_extensions()
validate_directory_mode()
-function add_files(files)
+local function add_files(files)
local oldcount = mp.get_property_number("playlist-count", 1)
for i = 1, #files do
mp.commandv("loadfile", files[i][1], "append")
@@ -179,44 +188,23 @@ function add_files(files)
end
end
-function get_extension(path)
- match = string.match(path, "%.([^%.]+)$" )
- if match == nil then
- return "nomatch"
- else
- return match
- end
+local function get_extension(path)
+ return path:match("%.([^%.]+)$") or "nomatch"
end
-function is_ignored(file)
- for pattern, _ in pairs(o.ignore_patterns) do
- if string.match(file, pattern) then
+local function is_ignored(file)
+ for pattern in pairs(o.ignore_patterns) do
+ if file:match(pattern) then
return true
end
end
-
return false
end
-table.filter = function(t, iter)
- for i = #t, 1, -1 do
- if not iter(t[i]) then
- table.remove(t, i)
- end
- end
-end
-
-table.append = function(t1, t2)
- local t1_size = #t1
- for i = 1, #t2 do
- t1[t1_size + i] = t2[i]
- end
-end
-
-- alphanum sorting for humans in Lua
-- http://notebook.kulchenko.com/algorithms/alphanumeric-natural-sorting-for-humans-in-lua
-function alphanumsort(filenames)
+local function alphanumsort(filenames)
local function padnum(n, d)
return #d > 0 and ("%03d%s%.12f"):format(#n, n, tonumber(d) / (10 ^ #d))
or ("%03d%s"):format(#n, n)
@@ -233,37 +221,45 @@ function alphanumsort(filenames)
return filenames
end
-local autoloaded = nil
+local autoloaded
local added_entries = {}
-local autoloaded_dir = nil
+local autoloaded_dir
-function scan_dir(path, current_file, dir_mode, separator, dir_depth, total_files, extensions)
- if dir_depth == MAXDIRSTACK then
+local function scan_dir(path, current_file, dir_mode, separator, dir_depth, total_files, extensions)
+ if dir_depth == MAX_DIR_STACK then
return
end
msg.trace("scanning: " .. path)
local files = utils.readdir(path, "files") or {}
local dirs = dir_mode ~= "ignore" and utils.readdir(path, "dirs") or {}
local prefix = path == "." and "" or path
- table.filter(files, function (v)
- -- The current file could be a hidden file, ignoring it doesn't load other
- -- files from the current directory.
+
+ local function filter(t, iter)
+ for i = #t, 1, -1 do
+ if not iter(t[i]) then
+ table.remove(t, i)
+ end
+ end
+ end
+
+ filter(files, function(v)
+ -- Always accept current file
local current = prefix .. v == current_file
- if o.ignore_hidden and not current and string.match(v, "^%.") then
+ if current then
+ return true
+ end
+ if o.ignore_hidden and v:match("^%.") then
return false
end
- if not current and is_ignored(v) then
+ if is_ignored(v) then
return false
end
local ext = get_extension(v)
- if ext == nil then
- return false
- end
- return extensions[string.lower(ext)]
+ return ext and extensions[ext:lower()]
end)
- table.filter(dirs, function(d)
- return not ((o.ignore_hidden and string.match(d, "^%.")))
+ filter(dirs, function(d)
+ return not (o.ignore_hidden and d:match("^%."))
end)
alphanumsort(files)
alphanumsort(dirs)
@@ -272,7 +268,14 @@ function scan_dir(path, current_file, dir_mode, separator, dir_depth, total_file
files[i] = prefix .. file
end
- table.append(total_files, files)
+ local function append(t1, t2)
+ local t1_size = #t1
+ for i = 1, #t2 do
+ t1[t1_size + i] = t2[i]
+ end
+ end
+
+ append(total_files, files)
if dir_mode == "recursive" then
for _, dir in ipairs(dirs) do
scan_dir(prefix .. dir .. separator, current_file, dir_mode,
@@ -282,11 +285,11 @@ function scan_dir(path, current_file, dir_mode, separator, dir_depth, total_file
for i, dir in ipairs(dirs) do
dirs[i] = prefix .. dir
end
- table.append(total_files, dirs)
+ append(total_files, dirs)
end
end
-function find_and_add_entries()
+local function find_and_add_entries()
local aborted = mp.get_property_native("playback-abort")
if aborted then
msg.debug("stopping: playback aborted")
@@ -305,32 +308,33 @@ function find_and_add_entries()
end
local pl_count = mp.get_property_number("playlist-count", 1)
- this_ext = get_extension(filename)
+ local this_ext = get_extension(filename)
-- check if this is a manually made playlist
- if (pl_count > 1 and autoloaded == nil) or
- (pl_count == 1 and EXTENSIONS[string.lower(this_ext)] == nil) then
+ if pl_count > 1 and autoloaded == nil then
msg.debug("stopping: manually made playlist")
return
- else
- if pl_count == 1 then
- autoloaded = true
- autoloaded_dir = dir
- added_entries = {}
- end
+ elseif pl_count == 1 then
+ autoloaded = true
+ autoloaded_dir = dir
+ added_entries = {}
end
- local extensions = {}
+ local extensions
if o.same_type then
- if EXTENSIONS_VIDEO[string.lower(this_ext)] ~= nil then
+ if EXTENSIONS_VIDEO[this_ext:lower()] then
extensions = EXTENSIONS_VIDEO
- elseif EXTENSIONS_AUDIO[string.lower(this_ext)] ~= nil then
+ elseif EXTENSIONS_AUDIO[this_ext:lower()] then
extensions = EXTENSIONS_AUDIO
- else
+ elseif EXTENSIONS_IMAGES[this_ext:lower()] then
extensions = EXTENSIONS_IMAGES
end
else
extensions = EXTENSIONS
end
+ if not extensions then
+ msg.debug("stopping: no matched extentions list")
+ return
+ end
local pl = mp.get_property_native("playlist", {})
local pl_current = mp.get_property_number("playlist-pos-1", 1)
@@ -338,11 +342,10 @@ function find_and_add_entries()
utils.to_string(pl)))
local files = {}
- do
- local dir_mode = o.directory_mode or mp.get_property("directory-mode", "lazy")
- local separator = mp.get_property_native("platform") == "windows" and "\\" or "/"
- scan_dir(autoloaded_dir, path, dir_mode, separator, 0, files, extensions)
- end
+ scan_dir(autoloaded_dir, path,
+ o.directory_mode or mp.get_property("directory-mode", "lazy"),
+ mp.get_property_native("platform") == "windows" and "\\" or "/",
+ 0, files, extensions)
if next(files) == nil then
msg.debug("no other files or directories in directory")
@@ -357,7 +360,8 @@ function find_and_add_entries()
break
end
end
- if current == nil then
+ if not current then
+ msg.debug("current file not found in directory")
return
end
msg.trace("current file position in files: "..current)
@@ -370,7 +374,7 @@ function find_and_add_entries()
local append = {[-1] = {}, [1] = {}}
for direction = -1, 1, 2 do -- 2 iterations, with direction = -1 and +1
- for i = 1, MAXENTRIES do
+ for i = 1, MAX_ENTRIES do
local pos = current + i * direction
local file = files[pos]
if file == nil or file[1] == "." then
@@ -390,12 +394,13 @@ function find_and_add_entries()
mp.commandv("loadfile", file, "append")
end
end
+ added_entries[file] = true
end
- added_entries[file] = true
end
if pl_count == 1 and direction == -1 and #append[-1] > 0 then
- for i = 1, #append[-1] do
- mp.commandv("loadfile", append[-1][i][1], "append")
+ local load = append[-1]
+ for i = 1, #load do
+ mp.commandv("loadfile", load[i][1], "append")
end
mp.commandv("playlist-move", 0, current)
end