fish-shell/share/completions/commandline.fish
Johannes Altmanninger 368017905e 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.
2024-01-27 09:28:06 +01:00

29 lines
1.9 KiB
Fish

complete -c commandline -s h -l help -d "Display help and exit"
complete -c commandline -s a -l append -d "Add text to the end of the selected area"
complete -c commandline -s i -l insert -d "Add text at cursor"
complete -c commandline -s r -l replace -d "Replace selected part"
complete -c commandline -s j -l current-job -d "Select job under cursor"
complete -c commandline -s p -l current-process -d "Select process under cursor"
complete -c commandline -s s -l current-selection -d "Select current selection"
complete -c commandline -s t -l current-token -d "Select token under cursor"
complete -c commandline -s b -l current-buffer -d "Select entire command line (default)"
complete -c commandline -s c -l cut-at-cursor -d "Only return that part of the command line before the cursor"
complete -c commandline -s f -l function -d "Inject readline functions to reader"
complete -c commandline -s x -l tokens-expanded -d "Print a list of expanded tokens"
complete -c commandline -l tokens-raw -d "Print a list of raw tokens"
complete -c commandline -s I -l input -d "Specify command to operate on"
complete -c commandline -s C -l cursor -d "Set/get cursor position, not buffer contents"
complete -c commandline -s B -l selection-start -d "Get current selection starting position"
complete -c commandline -s E -l selection-end -d "Get current selection ending position"
complete -c commandline -s L -l line -d "Print the line that the cursor is on"
complete -c commandline -s S -l search-mode -d "Return true if performing a history search"
complete -c commandline -s P -l paging-mode -d "Return true if showing pager content"
complete -c commandline -l paging-full-mode -d "Return true if pager is showing all content"
complete -c commandline -l is-valid -d "Return true if the command line is syntactically valid and complete"
complete -c commandline -n '__fish_contains_opt -s f function' -a '(bind --function-names)' -d 'Function name' -x