From 7c1a4acdb24b2be82b9cb9bbfd3d7bc55bf17e70 Mon Sep 17 00:00:00 2001 From: Guido Cella Date: Sat, 26 Jul 2025 15:49:42 +0200 Subject: [PATCH] select.lua: add edit-config-file and edit-input-conf Add 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. --- DOCS/man/select.rst | 8 +++++++ player/lua/select.lua | 49 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/DOCS/man/select.rst b/DOCS/man/select.rst index 5f84e3c209..3fd45ca57c 100644 --- a/DOCS/man/select.rst +++ b/DOCS/man/select.rst @@ -127,6 +127,14 @@ Available script bindings are: print its value on the OSD, which is useful for long values that get clipped. +``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. + ``menu`` Show a menu with miscellaneous entries. diff --git a/player/lua/select.lua b/player/lua/select.lua index 22938e4303..559c02ab7f 100644 --- a/player/lua/select.lua +++ b/player/lua/select.lua @@ -633,6 +633,51 @@ mp.add_key_binding(nil, "show-properties", function () }) end) +local function system_open(path) + 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 = {"gio", "open", path} + end + + mp.commandv("run", unpack(args)) +end + +local function edit_config_file(filename) + if not mp.get_property_bool("config") then + show_warning("Editing config files with --no-config is not supported.") + return + end + + 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:close() + end + + system_open(path) +end + +mp.add_key_binding(nil, "edit-config-file", function () + edit_config_file("mpv.conf") +end) + +mp.add_key_binding(nil, "edit-input-conf", function () + edit_config_file("input.conf") +end) + mp.add_key_binding(nil, "menu", function () local sub_track_count = 0 local audio_track_count = 0 @@ -673,6 +718,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 select/edit-config-file", true}, + {"Edit key bindings", "script-binding select/edit-input-conf", true}, {"Help", "script-binding stats/display-page-4-toggle", true}, } @@ -693,7 +740,7 @@ mp.add_key_binding(nil, "menu", function () submit = function (i) mp.command(commands[i]) - if not commands[i]:find("^script%-binding select/") then + if not commands[i]:find("^script%-binding select/select") then input.terminate() end end,