Skip to content

Commit d39b909

Browse files
committed
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, also including links to the documentation.
1 parent 1b231b4 commit d39b909

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

DOCS/man/select.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,14 @@ Available script bindings are:
119119
print its value on the OSD, which is useful for long values that get
120120
clipped.
121121

122+
``edit-config-file``
123+
Open ``mpv.conf`` in the system text editor, creating it if it doesn't
124+
already exist.
125+
126+
``edit-input-conf``
127+
Open ``input.conf`` in the system text editor, creating it if it doesn't
128+
already exist.
129+
122130
``menu``
123131
Show a menu with miscellaneous entries.
124132

player/lua/select.lua

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ local options = {
2525

2626
require "mp.options".read_options(options, nil, function () end)
2727

28+
local default_config_file = [[# https://mpv.io/manual/master/#configuration-files
29+
# https://mpv.io/manual/master/#options
30+
31+
]]
32+
33+
local default_input_conf = [[# https://mpv.io/manual/master/#command-interface
34+
35+
]]
36+
2837
local function show_warning(message)
2938
mp.msg.warn(message)
3039
if mp.get_property_native("vo-configured") then
@@ -580,6 +589,54 @@ mp.add_key_binding(nil, "show-properties", function ()
580589
})
581590
end)
582591

592+
local function edit_config_file(filename, initial_contents)
593+
local path = mp.find_config_file(filename)
594+
595+
if not path then
596+
path = mp.command_native({"expand-path", "~~/" .. filename})
597+
local file_handle, error_message = io.open(path, "w")
598+
599+
if not file_handle then
600+
show_error(error_message)
601+
return
602+
end
603+
604+
file_handle:write(initial_contents)
605+
file_handle:close()
606+
end
607+
608+
local platform = mp.get_property("platform")
609+
local args
610+
if platform == "windows" then
611+
args = {"rundll32", "url.dll,FileProtocolHandler", path}
612+
elseif platform == "darwin" then
613+
args = {"open", path}
614+
else
615+
args = {"xdg-open", path}
616+
end
617+
618+
local result = mp.command_native({
619+
name = "subprocess",
620+
playback_only = false,
621+
args = args,
622+
})
623+
624+
if result.status < 0 then
625+
show_error("Subprocess error: " .. result.error_string)
626+
elseif result.status > 0 then
627+
show_error(utils.to_string(args) .. " failed with code " ..
628+
result.status)
629+
end
630+
end
631+
632+
mp.add_key_binding(nil, "edit-config-file", function ()
633+
edit_config_file("mpv.conf", default_config_file)
634+
end)
635+
636+
mp.add_key_binding(nil, "edit-input-conf", function ()
637+
edit_config_file("input.conf", default_input_conf)
638+
end)
639+
583640
mp.add_key_binding(nil, "menu", function ()
584641
local sub_track_count = 0
585642
local audio_track_count = 0
@@ -620,6 +677,8 @@ mp.add_key_binding(nil, "menu", function ()
620677
{"Watch later", "script-binding select/select-watch-later", true},
621678
{"Stats for nerds", "script-binding stats/display-page-1-toggle", true},
622679
{"File info", "script-binding stats/display-page-5-toggle", mp.get_property("filename")},
680+
{"Edit config file", "script-binding select/edit-config-file", true},
681+
{"Edit key bindings", "script-binding select/edit-input-conf", true},
623682
{"Help", "script-binding stats/display-page-4-toggle", true},
624683
}
625684

0 commit comments

Comments
 (0)