Commit Graph
134 Commits
Author SHA1 Message Date
nanahi dbd31fd9d3 DOCS/man: use _name key for mp.command_native 2026-04-06 18:33:20 +02:00
nanahi 80582efca5 input/cmd: use _name for command name in mpv_command_node
Some commands have named arguments named "name", which conflict with
the "name" entry used to specify command name in mpv_command_node, so
they cannot be used with mpv_command_node.

These commands include:
set, del, change-list, add, cycle, multiply, enable-section,
disable-section, define-section, script-binding, keybind, keypress,
keydown, keyup, apply-profile.

This makes mpv_command_node accept command name as key "_name", so
"name" can be used to specify command arguments. For compatibility,
if "_name" entry does not exist, then "name" is used as fallback.
2026-04-06 18:33:20 +02:00
Guido Cella 9465b30a40 DOCS: remove behavior changes before version 0.38
These are ancient and not useful anymore.

The version notices are kept under:
overlay-add: handled by #17520
end-file's file_error: the notice should be removed when updating error
field like suggested there
time-start: this should just be removed
~/.mpv/: needed to understand why this is used
--aid: needed to understand the following paragraphs
2026-03-30 15:09:38 +00:00
CogentRedTester 429fdcf21d mp.input: allow clients to override console.lua script-opts
This commit allows a table of script-opt overrides to be passed to
`mp.input.get()` and `mp.input.select()`, which console.lua will use
instead of the its own.

To minimise required changes, metatables are used to allow console.lua
to seamlessly fall back on the original script-opts if the client does
not provide an override.
To prevent exceptions, the incoming opts must be of the same type.
2026-03-07 12:31:50 +01:00
Guido Cella a9ac97a5b0 DOCS/man/lua: remove REPL reference
It is a leftover of the original repl.lua name. Follow up to 9551de6385.
2026-03-04 14:11:16 +01:00
Guido Cella 05fac7f21e DOCS/man/lua: document remaining input.select() fields
Document more shared fields with input.get().

I originally omitted them because they didn't seem useful for
input.select(), but now opened can be used to override keybindings, and
default_text = "'" can be used to default to exact search (which I do
for history in my build).

closed, default_text and cursor_position may be used to remember the
last input line for the next call.
2026-02-28 22:41:26 +01:00
CogentRedTester c72f2add25 mp.input: allow async completions
Previously, it was impossible to generate a list of completions for an
`input.get()` request asynchronously, as the values had to be returned
by the callback function.

This commit provides a way to send completions asynchronously by passing
a second argument to the complete callback; a function which can be
passed the return values to respond to the complete event.
2026-02-24 20:19:24 +01:00
CogentRedTester cf481faa72 DOCS/man/lua: update recommended use of the mp.input opened event
Now cites overriding keybinds as the most notable use of the `opened`
event.
2026-02-24 20:12:57 +01:00
Guido Cella 1f686769cd mp.input: deprecate log_error()
This was a mistake. It's too situational and it's not worth complicating
the API and every mp.input client with it. It made some sense before I
implemented input.select() because you could use input.get() to input a
choice and validate it, but in general for input.get() clients it's
better to show an error with input.set_log() instead of adding new lines
to the log everytime. This is what e.g.
https://framagit.org/Midgard/mpv-subber/-/blob/master/subber.lua does.

So this is only marginally useful for REPL scripts that keep adding
lines to the log, and the only such scripts are commands.lua and my
lua-repl.lua. commands.lua never used it and I already removed it from
lua-repl.lua. Cloning all scripts in the wiki reveals that **nobody** is
using log_error(), confirming that it is safe to deprecate.
2026-02-08 11:51:17 +01:00
verygoodlee 40d2947fa3 DOCS/man/lua: fix the arg of callback function for "mp.register_event" 2026-01-31 16:56:13 +01:00
Guido CellaandCogentRedTester cc1e654286 DOCS/man/lua: suggest keep_open for nested mp.input calls
Before 17551b2d43 keep_open was needed to make nested mp.input calls.
Even after that change it is still better to pass it to not show the
console quickly closing and reopening to the user, so recommend it.

Co-authored-by: CogentRedTester <cogent.redtester@outlook.com>
2026-01-23 10:15:04 +01:00
CogentRedTester 2aa727bc3d mp.input: send buffer ids with logs to avoid race conditions
This commit modifies the log methods in mp.input to always send the id
of the latest `input.get()` request with log entries.

Previously, the log methods applied to whichever input request happened
to be open when the log message was received. Even when scripts used
these methods correctly, there was the risk of sending a log to the
wrong log buffer if the active input request changed while the log
message was in transit; a race condition.

Now the id of the latest `input.get()` request is sent alongside the log
messages, preventing data races between scripts while also preventing
those logs from being discarded.
2026-01-22 00:04:35 +01:00
CogentRedTester 5d469e6bf5 mp.input: remove race conditions when calling input.terminate()
This commit ensures that `input.terminate()` can only close input
requests made by the same script, and prevents any in-transit events
for old input requests from being processed.

Previously, there was no way to guarantee that the input request being
terminated was the one intended; the asynchronous nature of the API
meant that it was always possible (though unlikely) that another client
may have activated its own input request while the termination request
was in transit.
This commit removes the race condition between different scripts calling
`input.terminate()` by sending the script name alongside the termination
message.

In addition, when a script overwrites one of its own input requests,
there may be incoming events still in transit. Some of these events may
have a decent chance of calling `input.terminate()` if they are
processed (e.g., `submit`). This commit avoids this issue by only
processing `closed` requests once a new `input.get()` request is made.
2026-01-22 00:04:35 +01:00
sfan5 653e241ac3 DOCS/lua: fix mistyped function name 2025-08-22 08:38:02 +02:00
rkscv 5f8d5f8e8b DOCS/man/lua: fix spacing issues 2025-06-29 11:41:44 +02:00
Guido Cella 150b6d7b90 DOCS: remove references to the removed slave mode 2025-04-12 20:32:10 +02:00
Guido Cella 0b3cc3a167 commands.lua: split this script out of console.lua
It was suggested by CogentRedTester in
https://github.com/mpv-player/mpv/pull/10282#issuecomment-1858727729 and
https://github.com/mpv-player/mpv/pull/10282#issuecomment-1858809580 and
by avih that making running commands an mp.input client is a better
architecture.

A practical advantage is that completions are calculated in a different
thread, which prevents hanging when completing slow/network filesystems.

script-binding console/enable becomes script-binding commands/open,
though the console one is kept as an alias. I took the opportunity to
rename this because open makes more sense for a graphical modal, and it
is the word used in mp.input and user-data.

script-message-to console type becomes script-message-to commands type,
though the console one is kept as an alias. It is also changed to
automatically close on submit without having to append '; keypress ESC'
as you don't need to keep the console open after running prefilled
commands.

Also convert to double quotes like other scripts and rename some
inconsistent functions.
2025-03-08 19:09:23 +00:00
Guido Cella ce6a52d40a console.lua: add history_path field to mp.input.get
This allows saving the history to all mp.input.get clients.

Needed to split running commands out of console.lua.
2025-03-08 19:09:23 +00:00
Guido Cella 05438fc081 console.lua: add autoselect_completion field to mp.input.get
This selects the first completion when pressing Enter when one wasn't
already manually selected.

Needed to split running commands out of console.lua.
2025-03-08 19:09:23 +00:00
Guido Cella 3c175f0114 console.lua: let the complete callback return a character to append
Let the complete callback return the character to append to the selected
completion.

I didn't want to document this because it's specifically meant for
properties and unlikely to be needed by other scripts, but silently
interpreting the 3rd return value can screw you over if you do something
like

return candidates, code:find('[%w_]*$')

I was doing this in my lua-repl script, and find() returns the end
position as the 2rd argument, this becomes the 3rd return value and gets
appended to completions. So you have to do

return candidates, code:find('[%w_]*$'), nil

Needed to split running commands out of console.lua.
2025-03-08 19:09:23 +00:00
Guido Cella c7e62ee28c DOCS/man/lua: update complete docs referencing autocompletion 2025-03-08 19:09:23 +00:00
Guido Cella 996c098725 console.lua: add a keep_open flag to mp.input
With input.select this allows building nested submenus without the
flicker of console quickly closing and reopening, and also doing
multiple selections, to e.g. cycle through different values of a
property, or increase a property multiple times.

With input.get this is used to change the default behavior to closing by
default on submit. It was argued by avih that this is more useful, and
indeed Github code search shows that everybody is calling
input.terminate() unconditionally on submit, so the impact of the change
should be low, and restoring the old behavior is as easy as passing
keep_open = true, which is just ignored in older mpv versions.
2025-02-24 16:26:58 +01:00
Guido Cella 3fdc5338cc DOCS/man/select: add this file
Also update console's documentation.
2025-01-27 18:53:36 +01:00
nanahi 06670298b3 defaults.lua: support custom arg in mp.add_key_binding()
The information is available in the table.
2024-11-27 20:40:41 +01:00
nanahi e3e22de2ca defaults.lua: support scalable mp.add_key_binding()
For complex key bindings, the table now contains a new member
of the current key scale. mp.add_key_binding() now accepts the
scalable flag to make the binding scalable.
2024-11-27 20:40:41 +01:00
Guido Cella bb0b9f4cc8 defaults.lua: add an exit() function
Scripts can terminate execution by setting mp.keep_running = false. Add
an exit() function to wrap setting mp.keep_running and properly expose
this feature. It can be used e.g. by a thumbnail script to spawn workers
with load-script and then let them quit.

It is not added to the mp namespace as mp.exit because that would make
it look like it terminates mpv.

This mirrors the exit() function which already exists in js.

The note in javascript.rst about having to remove key bindings before
exit is not kept because they are actually removed automatically since
bf385e1140 (though it was accurate when the JS backend was developed
before upstreaming it).
2024-11-02 14:03:02 +02:00
Guido Cella 4def28ad0d DOCS/man/input: note that properties can be unavailable on init
It comes up often in IRC and issues that users don't understand why the
path property is initially unavailable, so link the section that
mentions it from the Properties section, and expand on how to get these
properties.
2024-11-02 01:41:11 +01:00
rkscv 8cd69e6de8 DOCS/man/lua: correct example syntax 2024-10-29 12:26:04 +02:00
nanahi eeb8e6e451 defaults.lua: handle canceled key bindings
There is a subtle behavior difference for built-in/input.conf key bindings
and key bindings registered by scripts: when a key binding is canceled
(e.g. a mouse button is bound to a command, is pressed down, and then
another key is pressed which is bound to another command), the command is
not invoked if the binding is built-in/input.conf, but is invoked if it's
registered by scripts, because it's handled with a different mechanism,
which gives no way for scripts to detect this.

Fix this by using the newly available canceled flag to detect this.
If a key binding is canceled, the callback is now not invoked unless
the key binding is registered with the complex option. In this situation,
the callback is invoked with the canceled state available so that scripts
can detect and handle this situation.
2024-06-06 23:38:39 +03:00
Guido Cella 367a6b561a console.lua: close when pressing enter with input.select
When you select an item, due to the submit handler being called
asynchronously, the default item list is redrawn before the console
closes, which is jarring. Fix this by always closing the console as soon
as enter is pressed, as keeping it open is unlikely to be useful with a
fuzzy selector (unlike with input.get() where it can be used e.g. to
implement a Lua REPL). If desired we can later add a close_on_submit
flag defaulting to true.

Also fix a crash when pressing enter without any match.
2024-05-12 23:13:48 +02:00
Guido Cella 2354d876da scripting: add mp.input.select()
This allows scripts to make the user choose from a list of entries by
typing part of their text and/or by navigating them with keybindings,
like dmenu or fzf.

Closes #13964.
2024-05-07 22:34:22 +02:00
gaesa d8f4749c34 DOCS/lua: clarify repeatable and complex for add_key_binding
The current documentation lacks clarity regarding the interaction
between the `repeatable` and `complex` options. Through an analysis
of the source code (`player/lua/defaults.lua` and
`player/js/defaults.js`), it was observed that the `repeatable` option
is only meaningful when the `complex` option is not enabled.
Additionally, the `complex` option in the existing documentation is
confusing, actually `fn` can be called on key repeat when `complex` is
`true` and `repeatable` is not `true`.

To address these issues, the documentation for the `repeatable` option
was updated to specify that it only applies when the `complex` option is
not set to `true`. Furthermore, the description of the `complex` and
`event` were revised to acknowledge the occurrence of key repeat events.
2024-02-15 16:43:46 +00:00
Guido Cella de0849404b scripting: don't observe properties with type nil
mp.observe_property('foo', nil, ...) calls the handler at least 2 times
on each playlist change even when the property doesn't change. This is
dangerous because if you haven't read observe_property's documentation
in a long time this is easy to forget, and you can end up using it for
handlers that are computationally expensive or that cause unintended
side effects.

Therefore, this commit discourages its use more explicitly in the
documentation, and replaces its usages in scripts.

For console.lua, observing focused with type nil leads to calling
mp.osd_message('') when changing file while playing in the terminal with
the console disabled. I don't notice issues from this, but it's safer to
avoid it.

For playlist and track-list this doesn't really matter since they
trigger multiple changes on each new file anyway, but changing it can
avoid encouraging people to imitate the code.

One usage of none in stats.lua is kept because according to b9084dfd47
it is a hack to replicate the deprecated tick event.
2024-01-20 16:09:34 +00:00
Guido Cella dfecc9f083 console.lua: style log lines in the terminal
When running the console in the terminal, style log lines with the same
escape sequences as msg.c.

mp.input can also specify terminal escape sequences, e.g. a script to
select a playlist entry can invert the color of the selection.

Also add a missing newline to help's error message.
2024-01-14 23:26:07 +01:00
Guido Cella 871f7a152a scripting: add mp.input
This lets scripts get textual input from the user using console.lua.
2024-01-13 02:53:08 +00:00
Mike Will 5ac37500c5 defaults.lua: add a disabled parameter to timer constructors
Added to the functions `mp.add_timeout` and `mp.add_periodic_timer`.

If the `disabled` argument is set to `true` or a truthy value, the
timer will wait to be manually started with a call to its `resume()`
method.
2023-10-11 21:04:13 +00:00
Guido Cella 2016e902e1 DOCS/lua: update read_options example
This still shows the old way of calling read_options even though it was
deprecated 8 years in ago 327b091909.
2023-10-10 18:38:15 +00:00
Christoph Heinrich e9690727a0 DOCS/lua: remove superfluous parameter
This seems to have been a leftover from copying the entry below it.
2023-09-30 15:17:10 +00:00
Guido Cella be92223157 DOCS/lua.rst: fix typo 2023-09-09 09:09:52 -04:00
Ameer Taweel 892ca9f5d9 DOCS/lua: fix typo 2023-03-21 15:49:42 +02:00
Avi Halachmi (:avih) 86093fcae7 lua/js: remove user-data helpers
This reverts:
  3fb4140c lua/defaults: add user_data helpers
  68a20e7a javascript/defaults: add user_data helpers
  00510379 lua/js: fix user_data_del util function

As well as the lua/js parts of:
  3ec2a098 docs: document new user-data property

user-data and its sub-properties can be set/get/observed/deleted
via the standard properties interface, so there's no need for
additional helpers specific to user-data, which only added maintenance
burden.
2023-01-29 01:52:31 +02:00
rcombs 0f3a041796 doc: correct spelling of user-data/user_data JS/lua helpers 2023-01-28 14:55:24 -06:00
rcombs 3ec2a0988a docs: document new user-data property 2023-01-28 14:37:24 -06:00
rcombs 51c6784df7 lua: add mp.del_property() 2023-01-28 14:20:20 -06:00
CogentRedTester 099ae86717 lua: command_native_async: always callback a-sync
Previously if the raw command_native_async returned an error then the
callback function was run directly. This meant that script writers
potentially had to account for both synchronous and asynchronous logic
in the callback, which can happen even with seemingly 'safe' commands
if the mpv event queue is full.

This was at odds with the Javascript implementation of
the function, which always runs the callback asynchronously.

Now the mp.add_timeout function is used to run the callback
asynchronously on error, replicating the Javascript implementation.

This provides consistency for developers in how the callback is handled
in Lua, and increases consistency between the Lua and Javascript APIs.
2022-06-22 14:44:12 +03:00
sfan5 dfcd561ba9 lua: remove mp.suspend, resume and resume_all
These functions were deprecated in v0.21.0 and no-ops
since v0.23.0. The matching client API functions were
removed in the previous commit.
2021-12-15 12:29:10 +01:00
a1346054 88460b6f86 DOCS: fix spelling 2021-08-24 20:44:52 +03:00
LaserEyess 9aa5cd6f45 DOCS: convert 3rd party http links to https
Discovered with:

find . -type f \( -name '*.md' -o -name '*.rst' \)  -exec grep -n 'http://' {} +

All real, i.e. non-example, links found were moved to https. There are
some dead links and websites with no https available which were not
converted.
2021-08-17 13:42:21 +00:00
Avi Halachmi (:avih) b6aedaa726 DOCS/lua.rst: fix docs for utils.file_info
The ctime member on Windows uses FILE_BASIC_INFO.ChangeTime, which is
pretty much the same as st_ctime on POSIX.

See https://devblogs.microsoft.com/oldnewthing/20100709-00/?p=13463 :

  ... The Last­Write­Time covers writes to the file’s data stream
  (which you accomplish via the Write­File function). On the other
  hand, the Change­Time also includes changes to the file metadata,
  such as changing its file attributes ...

Fixes #8801
2021-05-03 13:25:21 +03:00
Dudemanguy 7df9ebda6c DOCS: clarify how client/script names work
This isn't really clearly stated anywhere and could understandably lead
to some confusion.
2021-04-29 15:56:11 -05:00