summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOtto Modinos <ottomodinos@gmail.com>2014-09-24 21:17:49 +0300
committerwm4 <wm4@nowhere>2014-09-28 20:23:38 +0200
commitcbfb6de6675f4cd741dc9a136f8b0ea59bf7ebb7 (patch)
tree08aab47f908069587788ac0ce1f580b15cfe721f
parent34fc0720f763b67c2643c40408f8bdc80d288fef (diff)
downloadmpv-cbfb6de6675f4cd741dc9a136f8b0ea59bf7ebb7.tar.bz2
mpv-cbfb6de6675f4cd741dc9a136f8b0ea59bf7ebb7.tar.xz
lua: add mpv/lua directories to the lua path
Signed-off-by: wm4 <wm4@nowhere>
-rw-r--r--DOCS/man/lua.rst11
-rw-r--r--player/lua.c25
2 files changed, 33 insertions, 3 deletions
diff --git a/DOCS/man/lua.rst b/DOCS/man/lua.rst
index 147563a964..3b11085503 100644
--- a/DOCS/man/lua.rst
+++ b/DOCS/man/lua.rst
@@ -2,9 +2,14 @@ LUA SCRIPTING
=============
mpv can load Lua scripts. Scripts in ``~/.mpv/lua/`` will be loaded on program
-start, or if passed to ``--lua``. mpv provides the built-in module ``mp``, which
-provides functions to send commands to the mpv core and to retrieve information
-about playback state, user settings, file information, and so on.
+start, or if passed to ``--lua``. mpv appends ``~/.mpv/lua`` to the end of
+lua's path so you can import them too. Since it's added to the end, don't name
+scripts you want to import the same as lua libraries because they will be
+overshadowed by them.
+
+mpv provides the built-in module ``mp``, which provides functions to send
+commands to the mpv core and to retrieve information about playback state, user
+settings, file information, and so on.
These scripts can be used to control mpv in a similar way to slave mode.
Technically, the Lua code uses the client API internally.
diff --git a/player/lua.c b/player/lua.c
index 06d61f4c83..8aa6b594fb 100644
--- a/player/lua.c
+++ b/player/lua.c
@@ -215,6 +215,28 @@ static int load_scripts(lua_State *L)
return 0;
}
+static void set_path(lua_State *L)
+{
+ void *tmp = talloc_new(NULL);
+
+ lua_getglobal(L, "package"); // package
+ lua_getfield(L, -1, "path"); // package path
+ const char *path = lua_tostring(L, -1);
+
+ char *newpath = talloc_strdup(tmp, path ? path : "");
+ char **luadir = mp_find_all_config_files(tmp, get_mpctx(L)->global, "lua");
+ for (int i = 0; luadir && luadir[i]; i++) {
+ newpath = talloc_asprintf_append(newpath, ";%s",
+ mp_path_join(tmp, bstr0(luadir[i]), bstr0("?.lua")));
+ }
+
+ lua_pushstring(L, newpath); // package path newpath
+ lua_setfield(L, -3, "path"); // package path
+ lua_pop(L, 2); // -
+
+ talloc_free(tmp);
+}
+
static int run_lua(lua_State *L)
{
struct script_ctx *ctx = lua_touserdata(L, -1);
@@ -268,6 +290,9 @@ static int run_lua(lua_State *L)
assert(lua_gettop(L) == 0);
+ set_path(L);
+ assert(lua_gettop(L) == 0);
+
// run this under an error handler that can do backtraces
lua_pushcfunction(L, error_handler); // errf
lua_pushcfunction(L, load_scripts); // errf fn