Skip to content

Commit 7260550

Browse files
committed
misc.lua: add this script
Add a script with script bindings to edit mpv.conf and input.conf, and add them to the menu. These are useful as shortcuts, but the main motivation is that new users often ask why they can't find mpv.conf and input.conf, so this creates them if they don't exist, also including links to the documentation. Other future script bindings that don't fit anywhere else can also be added to misc.lua.
1 parent 1b231b4 commit 7260550

File tree

12 files changed

+116
-2
lines changed

12 files changed

+116
-2
lines changed

DOCS/interface-changes/misc.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add `--load-misc` option

DOCS/man/misc.rst

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
MISC
2+
====
3+
4+
This script provides miscellaneous script bindings. It can be disabled using the
5+
``--load-misc=no`` option.
6+
7+
Script bindings
8+
---------------
9+
10+
``edit-config-file``
11+
Open ``mpv.conf`` in the system text editor, creating it if it doesn't
12+
already exist.
13+
14+
``edit-input-conf``
15+
Open ``input.conf`` in the system text editor, creating it if it doesn't
16+
already exist.

DOCS/man/mpv.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1547,6 +1547,8 @@ works like in older mpv releases:
15471547

15481548
.. include:: positioning.rst
15491549

1550+
.. include:: misc.rst
1551+
15501552
.. include:: lua.rst
15511553

15521554
.. include:: javascript.rst

DOCS/man/options.rst

+4
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,10 @@ Program Behavior
10701070
Enable the builtin script that provides various keybindings to pan videos
10711071
and images (default: yes).
10721072

1073+
``--load-misc=<yes|no>``
1074+
Enable the builtin script that provides miscellaneous script bindings
1075+
(default: yes).
1076+
10731077
``--player-operation-mode=<cplayer|pseudo-gui>``
10741078
For enabling "pseudo GUI mode", which means that the defaults for some
10751079
options are changed. This option should not normally be used directly, but

options/options.c

+2
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,7 @@ static const m_option_t mp_opts[] = {
556556
{"load-select", OPT_BOOL(lua_load_select), .flags = UPDATE_BUILTIN_SCRIPTS},
557557
{"load-positioning", OPT_BOOL(lua_load_positioning), .flags = UPDATE_BUILTIN_SCRIPTS},
558558
{"load-commands", OPT_BOOL(lua_load_commands), .flags = UPDATE_BUILTIN_SCRIPTS},
559+
{"load-misc", OPT_BOOL(loa_load_misc), .flags = UPDATE_BUILTIN_SCRIPTS},
559560
#endif
560561

561562
// ------------------------- stream options --------------------
@@ -990,6 +991,7 @@ static const struct MPOpts mp_default_opts = {
990991
.lua_load_select = true,
991992
.lua_load_positioning = true,
992993
.lua_load_commands = true,
994+
.loa_load_misc = true,
993995
#endif
994996
.auto_load_scripts = true,
995997
.loop_times = 1,

options/options.h

+1
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ typedef struct MPOpts {
176176
bool lua_load_select;
177177
bool lua_load_positioning;
178178
bool lua_load_commands;
179+
bool loa_load_misc;
179180

180181
bool auto_load_scripts;
181182

player/core.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ typedef struct MPContext {
444444

445445
struct mp_ipc_ctx *ipc_ctx;
446446

447-
int64_t builtin_script_ids[8];
447+
int64_t builtin_script_ids[9];
448448

449449
mp_mutex abort_lock;
450450

player/lua.c

+3
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,9 @@ static const char * const builtin_lua_scripts[][2] = {
9090
},
9191
{"@commands.lua",
9292
# include "player/lua/commands.lua.inc"
93+
},
94+
{"@misc.lua",
95+
# include "player/lua/misc.lua.inc"
9396
},
9497
{0}
9598
};

player/lua/meson.build

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
lua_files = ['defaults.lua', 'assdraw.lua', 'options.lua', 'osc.lua',
22
'ytdl_hook.lua', 'stats.lua', 'console.lua', 'auto_profiles.lua',
33
'input.lua', 'fzy.lua', 'select.lua', 'positioning.lua',
4-
'commands.lua']
4+
'commands.lua', 'misc.lua']
55
foreach file: lua_files
66
lua_file = custom_target(file,
77
input: file,

player/lua/misc.lua

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
--[[
2+
This file is part of mpv.
3+
4+
mpv is free software; you can redistribute it and/or
5+
modify it under the terms of the GNU Lesser General Public
6+
License as published by the Free Software Foundation; either
7+
version 2.1 of the License, or (at your option) any later version.
8+
9+
mpv is distributed in the hope that it will be useful,
10+
but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
GNU Lesser General Public License for more details.
13+
14+
You should have received a copy of the GNU Lesser General Public
15+
License along with mpv. If not, see <http://www.gnu.org/licenses/>.
16+
]]
17+
18+
local utils = require "mp.utils"
19+
20+
local default_config_file = [[# https://mpv.io/manual/master/#configuration-files
21+
# https://mpv.io/manual/master/#options
22+
23+
]]
24+
25+
local default_input_conf = [[# https://mpv.io/manual/master/#command-interface
26+
27+
]]
28+
29+
local function show_error(message)
30+
mp.msg.error(message)
31+
if mp.get_property_native("vo-configured") then
32+
mp.osd_message(message)
33+
end
34+
end
35+
36+
local function edit_config_file(filename, initial_contents)
37+
local path = mp.find_config_file(filename)
38+
39+
if not path then
40+
path = mp.command_native({"expand-path", "~~/" .. filename})
41+
local file_handle, error_message = io.open(path, "w")
42+
43+
if not file_handle then
44+
show_error(error_message)
45+
return
46+
end
47+
48+
file_handle:write(initial_contents)
49+
file_handle:close()
50+
end
51+
52+
local platform = mp.get_property("platform")
53+
local args
54+
if platform == "windows" then
55+
args = {"rundll32", "url.dll,FileProtocolHandler", path}
56+
elseif platform == "darwin" then
57+
args = {"open", path}
58+
else
59+
args = {"xdg-open", path}
60+
end
61+
62+
local result = mp.command_native({
63+
name = "subprocess",
64+
playback_only = false,
65+
args = args,
66+
})
67+
68+
if result.status < 0 then
69+
show_error("Subprocess error: " .. result.error_string)
70+
elseif result.status > 0 then
71+
show_error(utils.to_string(args) .. " failed with code " ..
72+
result.status)
73+
end
74+
end
75+
76+
mp.add_key_binding(nil, "edit-config-file", function ()
77+
edit_config_file("mpv.conf", default_config_file)
78+
end)
79+
80+
mp.add_key_binding(nil, "edit-input-conf", function ()
81+
edit_config_file("input.conf", default_input_conf)
82+
end)

player/lua/select.lua

+2
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,8 @@ mp.add_key_binding(nil, "menu", function ()
620620
{"Watch later", "script-binding select/select-watch-later", true},
621621
{"Stats for nerds", "script-binding stats/display-page-1-toggle", true},
622622
{"File info", "script-binding stats/display-page-5-toggle", mp.get_property("filename")},
623+
{"Edit config file", "script-binding misc/edit-config-file", true},
624+
{"Edit key bindings", "script-binding misc/edit-input-conf", true},
623625
{"Help", "script-binding stats/display-page-4-toggle", true},
624626
}
625627

player/scripting.c

+1
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ void mp_load_builtin_scripts(struct MPContext *mpctx)
269269
load_builtin_script(mpctx, 5, mpctx->opts->lua_load_select, "@select.lua");
270270
load_builtin_script(mpctx, 6, mpctx->opts->lua_load_positioning, "@positioning.lua");
271271
load_builtin_script(mpctx, 7, mpctx->opts->lua_load_commands, "@commands.lua");
272+
load_builtin_script(mpctx, 8, mpctx->opts->loa_load_misc, "@misc.lua");
272273
}
273274

274275
bool mp_load_scripts(struct MPContext *mpctx)

0 commit comments

Comments
 (0)