|
| 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) |
0 commit comments