Skip to content

Commit ccd4fdf

Browse files
custom-keybinds: add run-statement script-message
This is an extremely powerful script-message that allows you to basically embed small addons into custom-keybinds.
1 parent c9ec70c commit ccd4fdf

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

custom-keybinds.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ To avoid conflicts custom keybinds use the format: `file_browser/dynamic/custom/
173173
Expressions are used to evaluate Lua code into a string that can be used for commands.
174174
These behave similarly to those used for [`profile-cond`](https://mpv.io/manual/master/#conditional-auto-profiles)
175175
values. In an expression the `mp`, `mp.msg`, and `mp.utils` modules are available as `mp`, `msg`, and `utils` respectively.
176-
Additionally the file-browser [addon API](addons/addons.md#the-api) is available as `fb`.
176+
Additionally the file-browser [addon API](addons/addons.md#the-api) is available as `fb` and if [mpv-user-input](https://github.com/CogentRedTester/mpv-user-input)
177+
is installed then user-input API will be available in `input`.
177178

178179
This example only runs the keybind if the browser is in the Windows C drive or if
179180
the selected item is a matroska file:
@@ -284,6 +285,41 @@ This example replaces all `/` characters in the path with `\`
284285
}
285286
```
286287

288+
### `run-statement <statement...>`
289+
290+
Runs the following string a as a Lua statement. This is similar to an [expression](#expressions),
291+
but instead of the code evaluating to a value it must run a series of statements. Basically it allows
292+
for function bodies to be embedded into custom keybinds. All the same modules are available.
293+
If multiple strings are sent to the script-message then they will be concatenated together with newlines.
294+
295+
The following keybind will use [mpv-user-input](https://github.com/CogentRedTester/mpv-user-input) to
296+
rename items in file-browser:
297+
298+
```json
299+
{
300+
"key": "KP1",
301+
"command": ["script-message", "run-statement",
302+
"assert(input, 'install mpv-user-input!')",
303+
304+
"local line, err = input.get_user_input_co({",
305+
"id = 'rename-file',",
306+
"source = 'custom-keybind',",
307+
"request_text = 'rename file:',",
308+
"queueable = true,",
309+
"default_input = %N,",
310+
"cursor_pos = #(%N) - #fb.get_extension(%N, '')",
311+
"})",
312+
313+
"if not line then return end",
314+
"os.rename(%F, utils.join_path(%P, line))",
315+
316+
"fb.rescan()"
317+
],
318+
"parser": "file",
319+
"multiselect": true
320+
}
321+
```
322+
287323
## Examples
288324

289325
See [here](file-browser-keybinds.json).

file-browser.lua

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,10 @@ local o = {
126126
opt.read_options(o, 'file_browser')
127127
utils.shared_script_property_set("file_browser-open", "no")
128128

129+
package.path = mp.command_native({"expand-path", o.module_directory}).."/?.lua;"..package.path
130+
local success, input = pcall(require, "user-input-module")
131+
if not success then input = nil end
132+
129133

130134

131135
--------------------------------------------------------------------------------------------------------
@@ -161,8 +165,6 @@ local parse_state_API = {}
161165
local ass = mp.create_osd_overlay("ass-events")
162166
if not ass then return msg.error("Script requires minimum mpv version 0.31") end
163167

164-
package.path = mp.command_native({"expand-path", o.module_directory}).."/?.lua;"..package.path
165-
166168
local style = {
167169
global = o.alignment == 0 and "" or ([[{\an%d}]]):format(o.alignment),
168170

@@ -579,6 +581,7 @@ function API.evaluate_string(str, name)
579581
env.msg = API.redirect_table(msg)
580582
env.utils = API.redirect_table(utils)
581583
env.fb = API.redirect_table(API)
584+
env.input = input and API.redirect_table(input)
582585

583586
local chunk, err
584587
if setfenv then
@@ -2167,12 +2170,11 @@ local function scan_directory_json(directory, response_str)
21672170
mp.commandv("script-message", response_str, list or "", opts or "")
21682171
end
21692172

2170-
pcall(function()
2171-
local input = require "user-input-module"
2173+
if input then
21722174
mp.add_key_binding("Alt+o", "browse-directory/get-user-input", function()
21732175
input.get_user_input(browse_directory, {request_text = "open directory:"})
21742176
end)
2175-
end)
2177+
end
21762178

21772179

21782180

@@ -2228,6 +2230,14 @@ mp.register_script_message('evaluate-expressions', function(...)
22282230
end)
22292231
end)
22302232

2233+
--a helper function for custom-keybinds
2234+
--concatenates the command arguments with newlines and runs the
2235+
--string as a statement of code
2236+
mp.register_script_message('run-statement', function(...)
2237+
local statement = table.concat(table.pack(...), '\n')
2238+
API.coroutine.run(API.evaluate_string, statement)
2239+
end)
2240+
22312241
--allows keybinds/other scripts to auto-open specific directories
22322242
mp.register_script_message('browse-directory', browse_directory)
22332243

0 commit comments

Comments
 (0)