Skip to content

misc.lua: add this script #16129

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DOCS/interface-changes/misc.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add `--load-misc` option
16 changes: 16 additions & 0 deletions DOCS/man/misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
MISC
====

This script provides miscellaneous script bindings. It can be disabled using the
``--load-misc=no`` option.

Script bindings
---------------

``edit-config-file``
Open ``mpv.conf`` in the system text editor, creating it if it doesn't
already exist.

``edit-input-conf``
Open ``input.conf`` in the system text editor, creating it if it doesn't
already exist.
2 changes: 2 additions & 0 deletions DOCS/man/mpv.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1547,6 +1547,8 @@ works like in older mpv releases:

.. include:: positioning.rst

.. include:: misc.rst

.. include:: lua.rst

.. include:: javascript.rst
Expand Down
4 changes: 4 additions & 0 deletions DOCS/man/options.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1070,6 +1070,10 @@ Program Behavior
Enable the builtin script that provides various keybindings to pan videos
and images (default: yes).

``--load-misc=<yes|no>``
Enable the builtin script that provides miscellaneous script bindings
(default: yes).

``--player-operation-mode=<cplayer|pseudo-gui>``
For enabling "pseudo GUI mode", which means that the defaults for some
options are changed. This option should not normally be used directly, but
Expand Down
2 changes: 2 additions & 0 deletions options/options.c
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ static const m_option_t mp_opts[] = {
{"load-select", OPT_BOOL(lua_load_select), .flags = UPDATE_BUILTIN_SCRIPTS},
{"load-positioning", OPT_BOOL(lua_load_positioning), .flags = UPDATE_BUILTIN_SCRIPTS},
{"load-commands", OPT_BOOL(lua_load_commands), .flags = UPDATE_BUILTIN_SCRIPTS},
{"load-misc", OPT_BOOL(loa_load_misc), .flags = UPDATE_BUILTIN_SCRIPTS},
#endif

// ------------------------- stream options --------------------
Expand Down Expand Up @@ -990,6 +991,7 @@ static const struct MPOpts mp_default_opts = {
.lua_load_select = true,
.lua_load_positioning = true,
.lua_load_commands = true,
.loa_load_misc = true,
#endif
.auto_load_scripts = true,
.loop_times = 1,
Expand Down
1 change: 1 addition & 0 deletions options/options.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ typedef struct MPOpts {
bool lua_load_select;
bool lua_load_positioning;
bool lua_load_commands;
bool loa_load_misc;

bool auto_load_scripts;

Expand Down
2 changes: 1 addition & 1 deletion player/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -444,7 +444,7 @@ typedef struct MPContext {

struct mp_ipc_ctx *ipc_ctx;

int64_t builtin_script_ids[8];
int64_t builtin_script_ids[9];

mp_mutex abort_lock;

Expand Down
3 changes: 3 additions & 0 deletions player/lua.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ static const char * const builtin_lua_scripts[][2] = {
},
{"@commands.lua",
# include "player/lua/commands.lua.inc"
},
{"@misc.lua",
# include "player/lua/misc.lua.inc"
},
{0}
};
Expand Down
2 changes: 1 addition & 1 deletion player/lua/meson.build
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
lua_files = ['defaults.lua', 'assdraw.lua', 'options.lua', 'osc.lua',
'ytdl_hook.lua', 'stats.lua', 'console.lua', 'auto_profiles.lua',
'input.lua', 'fzy.lua', 'select.lua', 'positioning.lua',
'commands.lua']
'commands.lua', 'misc.lua']
foreach file: lua_files
lua_file = custom_target(file,
input: file,
Expand Down
82 changes: 82 additions & 0 deletions player/lua/misc.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
--[[
This file is part of mpv.

mpv is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

mpv is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with mpv. If not, see <http://www.gnu.org/licenses/>.
]]

local utils = require "mp.utils"

local default_config_file = [[# https://mpv.io/manual/master/#configuration-files
# https://mpv.io/manual/master/#options

]]

local default_input_conf = [[# https://mpv.io/manual/master/#command-interface

]]

local function show_error(message)
mp.msg.error(message)
if mp.get_property_native("vo-configured") then
mp.osd_message(message)
end
end

local function edit_config_file(filename, initial_contents)
local path = mp.find_config_file(filename)

if not path then
path = mp.command_native({"expand-path", "~~/" .. filename})
local file_handle, error_message = io.open(path, "w")

if not file_handle then
show_error(error_message)
return
end

file_handle:write(initial_contents)
file_handle:close()
end

local platform = mp.get_property("platform")
local args
if platform == "windows" then
args = {"rundll32", "url.dll,FileProtocolHandler", path}
elseif platform == "darwin" then
args = {"open", path}
else
args = {"xdg-open", path}
end

local result = mp.command_native({
name = "subprocess",
playback_only = false,
args = args,
})

if result.status < 0 then
show_error("Subprocess error: " .. result.error_string)
elseif result.status > 0 then
show_error(utils.to_string(args) .. " failed with code " ..
result.status)
end
end

mp.add_key_binding(nil, "edit-config-file", function ()
edit_config_file("mpv.conf", default_config_file)
end)

mp.add_key_binding(nil, "edit-input-conf", function ()
edit_config_file("input.conf", default_input_conf)
end)
2 changes: 2 additions & 0 deletions player/lua/select.lua
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ mp.add_key_binding(nil, "menu", function ()
{"Watch later", "script-binding select/select-watch-later", true},
{"Stats for nerds", "script-binding stats/display-page-1-toggle", true},
{"File info", "script-binding stats/display-page-5-toggle", mp.get_property("filename")},
{"Edit config file", "script-binding misc/edit-config-file", true},
{"Edit key bindings", "script-binding misc/edit-input-conf", true},
{"Help", "script-binding stats/display-page-4-toggle", true},
}

Expand Down
1 change: 1 addition & 0 deletions player/scripting.c
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ void mp_load_builtin_scripts(struct MPContext *mpctx)
load_builtin_script(mpctx, 5, mpctx->opts->lua_load_select, "@select.lua");
load_builtin_script(mpctx, 6, mpctx->opts->lua_load_positioning, "@positioning.lua");
load_builtin_script(mpctx, 7, mpctx->opts->lua_load_commands, "@commands.lua");
load_builtin_script(mpctx, 8, mpctx->opts->loa_load_misc, "@misc.lua");
}

bool mp_load_scripts(struct MPContext *mpctx)
Expand Down
Loading