Skip to content

select.lua: add edit-config-file and edit-input-conf #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 2 commits 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
11 changes: 11 additions & 0 deletions DOCS/man/select.rst
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,17 @@ 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.

``open-docs``
Open mpv's online documentation in the browser.

``menu``
Show a menu with miscellaneous entries.

Expand Down
54 changes: 53 additions & 1 deletion player/lua/select.lua
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,55 @@ 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, "open-docs", function ()
system_open("https://mpv.io/manual/")
end)

mp.add_key_binding(nil, "menu", function ()
local sub_track_count = 0
local audio_track_count = 0
Expand Down Expand Up @@ -620,7 +669,10 @@ 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},
{"Online documentation", "script-binding select/open-docs", true},
}

local labels = {}
Expand All @@ -640,7 +692,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,
Expand Down