builtin commandline: -x for expanded tokens, supplanting -o

Issue #10194 reports Cobra completions do

    set -l args (commandline -opc)
    eval $args[1] __complete $args[2..] (commandline -ct | string escape)

The intent behind "eval" is to expand variables and tildes in "$args".
Fair enough. Several of our own completions do the same, see the next commit.

The problem with "commandline -o" + "eval" is that the former already
removes quotes that are  relevant for "eval". This becomes a problem if $args
contains quoted () or {}, for example this command will wrongly execute a
command substituion:

    git --work-tree='(launch-missiles)' <TAB>

It is possible to escape the string the tokens before running eval, but
then there will be no expansion of variables etc.  The problem is that
"commandline -o" only unescapes tokens so they end up in a weird state
somewhere in-between what the user typed and the expanded version.

Remove the need for "eval" by introducing "commandline -x" which expands
things like variables and braces. This enables custom completion scripts to
be aware of shell variables without eval, see the added test for completions
to "make -C $var/some/dir ".

This means that essentially all third party scripts should migrate from
"commandline -o" to "commandline -x". For example

    set -l tokens
    if commandline -x >/dev/null 2>&1
        set tokens (commandline -xpc)
    else
        set tokens (commandline -opc)
    end

Since this is mainly used for completions, the expansion skips command
substitutions.  They are passed through as-is (instead of cancelling or
expanding to nothing) to make custom completion scripts work reasonably well
in the common case. Of course there are cases where we would want to expand
command substitutions here, so I'm not sure.
This commit is contained in:
Johannes Altmanninger
2024-01-06 08:45:33 +01:00
parent 1b9e5258b5
commit 368017905e
6 changed files with 165 additions and 40 deletions

View File

@@ -74,13 +74,20 @@ The following options change the way ``commandline`` prints the current commandl
**-c** or **--cut-at-cursor**
Only print selection up until the current cursor position.
If combined with ``--tokenize``, this will print up until the last completed token - excluding the token the cursor is in.
If combined with ``--expand-tokens``, this will print up until the last completed token - excluding the token the cursor is in.
This is typically what you would want for instance in completions.
To get both, use both ``commandline --cut-at-cursor --tokenize; commandline --cut-at-cursor --current-token``,
or ``commandline -co; commandline -ct`` for short.
To get both, use both ``commandline --cut-at-cursor --expand-tokens; commandline --cut-at-cursor --current-token``,
or ``commandline -cx; commandline -ct`` for short.
**-o** or **--tokenize**
Tokenize the selection and print one string-type token per line.
**-x** or **tokens-expanded**
Perform argument expansion on the selection and print one argument per line.
Command substituions are not expanded but forwarded as-is.
**tokens-raw**
Print arguments in the selection as they appear on the command line, one per line.
**-o** or **tokenize**
Deprecated, do not use.
If ``commandline`` is called during a call to complete a given string using ``complete -C STRING``, ``commandline`` will consider the specified string to be the current contents of the command line.
@@ -128,7 +135,7 @@ The most common use for something like completions is
::
set -l tokens (commandline -opc)
set -l tokens (commandline -xpc)
which gives the current *process* (what is being completed), tokenized into separate entries, up to but excluding the currently being completed token