commands.lua: add quotes to file completion - #18336
Conversation
Automatically inserting quotes in file completion problematic, because when completing directories you want the cursor before the final quote rather than after in order to further complete the files within, and there is no system to set a cursor position before the end of the completion. However we can add quotes automatically without this issue when: - completing files in the current directory - we can add only the first quote when completing directories in the current directory - we can add the final quote after files if the user typed a quote at the beginning of the path, since there's nothing remaining to complete after regular files This adds single quotes because they are easier to type. Filenames containing both spaces and single quotes are not supported.
|
The quote issue can be avoided entirely by auto escaping special characters instead. There won't be quote character at the end that interferes further completion this way. |
|
How? mpv interprets |
|
Console can preprocess the input and make that one argument. Similar to what shells do. |
I don't think this is feasible. there may be additional arguments after the file path, which can cause ambiguity. e.g.
the linux shell style escaping |
I don't think this is too difficult, we should be able to detect when a space is not escaped and detect that a new argument will start afterwards. Something like this: local function submit(text)
-- Find each argument in the command string.
-- Only end the argument with a space not preceded by a `\`.
local escaped_command = string.gsub(text..' ', '([^%s].-[^\\])%s', function(arg)
-- Do nothing if the argument is already quoted.
if arg:find([[^".*"$]]) or arg:find([[^'.*'$]]) or arg:find([[^`(.).*%1`]]) then
return nil
end
return '"' .. arg:gsub('\\ ', ' ') .. '"'
end)
mp.command(escaped_command)
endThe only problem is that the command syntax currently allows backslash literals (and, I think, pretty much every character outside of newlines?) outside quotes, meaning that someone can currently write
We could use another escape character I suppose, or we could return forward slashes for completions even on windows, though that may cause issues if people are copying and pasting paths. We could also probably code |
|
Escaping is not so simple since you can also do |


Automatically inserting quotes in file completion problematic, because when completing directories you want the cursor before the final quote rather than after in order to further complete the files within, and there is no system to set a cursor position before the end of the completion.
However we can add quotes automatically without this issue when:
This adds single quotes because they are easier to type. Filenames containing both spaces and single quotes are not supported.