lua: add mp.get_script_directory() function

And add some clarifications/suggestions to the manpage.
This commit is contained in:
wm4 2020-02-04 20:40:16 +01:00
parent 6a83187b06
commit 65cd9efa85
2 changed files with 27 additions and 5 deletions

View File

@ -55,12 +55,18 @@ that uses the ``.foo`` file extension.
mpv also appends the top level directory of the script to the start of Lua's
package path so you can import scripts from there too. Be aware that this will
shadow Lua libraries that use the same package path.
shadow Lua libraries that use the same package path. (Single file scripts do not
include mpv specific directory the Lua package path. This was silently changed
in mpv 0.32.0.)
On the other hand, if the script is a single file (directly located in
``~/.config/mpv/scripts/`` and not as sub-directory), the Lua package path
does not include any mpv specific directories. (This was silently changed in
mpv 0.32.0.)
Using a script directory is the recommended way to package a script that
consists of multiple source files, or requires other files (you can use
``mp.get_script_directory()`` to get the location and e.g. load data files).
Making a script a git repository, basically a repository which contains a
``main.lua``` file in the root directory, makes scripts easily updateable
(without the dangers of auto-updates). Another suggestion is to use git
submodules to share common files or libraries.
Details on the script initialization and lifecycle
--------------------------------------------------
@ -492,6 +498,11 @@ The ``mp`` module is preloaded, although it can be loaded manually with
The script ``/path/to/fooscript.lua`` becomes ``fooscript``.
``mp.get_script_directory()``
Return the directory if this is a script packaged as directory (see
`Script location`_ for a description). Return nothing if this is a single
file script.
``mp.osd_message(text [,duration])``
Show an OSD message on the screen. ``duration`` is in seconds, and is
optional (uses ``--osd-duration`` by default).

View File

@ -455,6 +455,16 @@ static int script_find_config_file(lua_State *L)
return 1;
}
static int script_get_script_directory(lua_State *L)
{
struct script_ctx *ctx = get_ctx(L);
if (ctx->path) {
lua_pushstring(L, ctx->path);
return 1;
}
return 0;
}
static int script_suspend(lua_State *L)
{
struct script_ctx *ctx = get_ctx(L);
@ -1228,6 +1238,7 @@ static const struct fn_entry main_fns[] = {
FN_ENTRY(wait_event),
FN_ENTRY(request_event),
FN_ENTRY(find_config_file),
FN_ENTRY(get_script_directory),
FN_ENTRY(command),
FN_ENTRY(commandv),
FN_ENTRY(command_native),