Skip to content

Commit

Permalink
custom-keybinds: add run-statement script-message
Browse files Browse the repository at this point in the history
This is an extremely powerful script-message that allows you to
basically embed small addons into custom-keybinds.
  • Loading branch information
CogentRedTester committed Oct 30, 2022
1 parent c9ec70c commit ccd4fdf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 6 deletions.
38 changes: 37 additions & 1 deletion custom-keybinds.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ To avoid conflicts custom keybinds use the format: `file_browser/dynamic/custom/
Expressions are used to evaluate Lua code into a string that can be used for commands.
These behave similarly to those used for [`profile-cond`](https://mpv.io/manual/master/#conditional-auto-profiles)
values. In an expression the `mp`, `mp.msg`, and `mp.utils` modules are available as `mp`, `msg`, and `utils` respectively.
Additionally the file-browser [addon API](addons/addons.md#the-api) is available as `fb`.
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)
is installed then user-input API will be available in `input`.

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

### `run-statement <statement...>`

Runs the following string a as a Lua statement. This is similar to an [expression](#expressions),
but instead of the code evaluating to a value it must run a series of statements. Basically it allows
for function bodies to be embedded into custom keybinds. All the same modules are available.
If multiple strings are sent to the script-message then they will be concatenated together with newlines.

The following keybind will use [mpv-user-input](https://github.com/CogentRedTester/mpv-user-input) to
rename items in file-browser:

```json
{
"key": "KP1",
"command": ["script-message", "run-statement",
"assert(input, 'install mpv-user-input!')",

"local line, err = input.get_user_input_co({",
"id = 'rename-file',",
"source = 'custom-keybind',",
"request_text = 'rename file:',",
"queueable = true,",
"default_input = %N,",
"cursor_pos = #(%N) - #fb.get_extension(%N, '')",
"})",

"if not line then return end",
"os.rename(%F, utils.join_path(%P, line))",

"fb.rescan()"
],
"parser": "file",
"multiselect": true
}
```

## Examples

See [here](file-browser-keybinds.json).
20 changes: 15 additions & 5 deletions file-browser.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,10 @@ local o = {
opt.read_options(o, 'file_browser')
utils.shared_script_property_set("file_browser-open", "no")

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



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

package.path = mp.command_native({"expand-path", o.module_directory}).."/?.lua;"..package.path

local style = {
global = o.alignment == 0 and "" or ([[{\an%d}]]):format(o.alignment),

Expand Down Expand Up @@ -579,6 +581,7 @@ function API.evaluate_string(str, name)
env.msg = API.redirect_table(msg)
env.utils = API.redirect_table(utils)
env.fb = API.redirect_table(API)
env.input = input and API.redirect_table(input)

local chunk, err
if setfenv then
Expand Down Expand Up @@ -2167,12 +2170,11 @@ local function scan_directory_json(directory, response_str)
mp.commandv("script-message", response_str, list or "", opts or "")
end

pcall(function()
local input = require "user-input-module"
if input then
mp.add_key_binding("Alt+o", "browse-directory/get-user-input", function()
input.get_user_input(browse_directory, {request_text = "open directory:"})
end)
end)
end



Expand Down Expand Up @@ -2228,6 +2230,14 @@ mp.register_script_message('evaluate-expressions', function(...)
end)
end)

--a helper function for custom-keybinds
--concatenates the command arguments with newlines and runs the
--string as a statement of code
mp.register_script_message('run-statement', function(...)
local statement = table.concat(table.pack(...), '\n')
API.coroutine.run(API.evaluate_string, statement)
end)

--allows keybinds/other scripts to auto-open specific directories
mp.register_script_message('browse-directory', browse_directory)

Expand Down

0 comments on commit ccd4fdf

Please sign in to comment.