Skip to content

refactor(_command_offset): save Bash's $2 passed to completion functions #814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
68 changes: 44 additions & 24 deletions bash_completion
Original file line number Diff line number Diff line change
Expand Up @@ -971,7 +971,7 @@ _comp_delimited()
# The word to be completed is expected to contain the entire assignment,
# including the variable name and the "=". Some known variables are completed
# with colon separated values; for those to work, colon should not have been
# used to split words. See related parameters to _init_completion.
# used to split words. See related parameters to _comp_initialize.
#
# @param $1 variable assignment to be completed
# @return True (0) if variable value completion was attempted,
Expand Down Expand Up @@ -1034,13 +1034,28 @@ _comp_variable_assignments()
# -o XSPEC Passed to _filedir as first arg for other output redirections
# -i XSPEC Passed to _filedir as first arg for stdin redirections
# -s Split long options with _split_longopt, implies -n =
# @param $1...$3 args Original arguments specified to the completion function.
# The first argument $1 is command name. The second
# argument $2 is the string before the cursor in the
# current word. The third argument $3 is the previous
# word.
# @var[out] cur Reconstructed current word
# @var[out] prev Reconstructed previous word
# @var[out] words Reconstructed words
# @var[out] cword Current word index in `words`
# @var[out] comp_args Original arguments specified to the completion function
# are saved in this array, if the arguments $1...$3 is
# specified.
# @var[out,opt] split When "-s" is specified, `true/false` is set depending on
# whether the split happened.
# @return True (0) if completion needs further processing,
# False (> 0) no further processing is necessary.
#
_init_completion()
_comp_initialize()
{
local exclude="" flag outx errx inx OPTIND=1
local exclude="" outx errx inx

local flag OPTIND=1 OPTARG='' OPTERR=0
while getopts "n:e:o:i:s" flag "$@"; do
case $flag in
n) exclude+=$OPTARG ;;
Expand All @@ -1057,6 +1072,8 @@ _init_completion()
;;
esac
done
shift "$((OPTIND - 1))"
(($#)) && comp_args=("$@")

COMPREPLY=()
local redir='@(?(+([0-9])|{[a-zA-Z_]*([a-zA-Z_0-9])})@(>?([>|&])|<?([>&])|<<?([-<]))|&>?(>))'
Expand Down Expand Up @@ -1108,6 +1125,7 @@ _init_completion()

return 0
}
_comp_deprecate_func _init_completion _comp_initialize

# Helper function for _parse_help and _parse_usage.
# @return True (0) if an option was found, False (> 0) otherwise
Expand Down Expand Up @@ -1605,8 +1623,8 @@ _services()
#
_service()
{
local cur prev words cword
_init_completion || return
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

# don't complete past 2nd token
((cword > 2)) && return
Expand Down Expand Up @@ -1895,8 +1913,8 @@ _bashcomp_try_faketty()
#
_user_at_host()
{
local cur prev words cword
_init_completion -n : || return
local cur prev words cword comp_args
_comp_initialize -n : -- "$@" || return

if [[ $cur == *@* ]]; then
_known_hosts_real "$cur"
Expand All @@ -1911,8 +1929,8 @@ shopt -u hostcomplete && complete -F _user_at_host talk ytalk finger
# `_known_hosts_real' instead.
_known_hosts()
{
local cur prev words cword
_init_completion -n : || return
local cur prev words cword comp_args
_comp_initialize -n : -- "$@" || return

# NOTE: Using `_known_hosts' as a helper function and passing options
# to `_known_hosts' is deprecated: Use `_known_hosts_real' instead.
Expand Down Expand Up @@ -2193,8 +2211,8 @@ complete -F _known_hosts traceroute traceroute6 \
#
_cd()
{
local cur prev words cword
_init_completion || return
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

if [[ $cur == -* ]]; then
COMPREPLY=($(compgen -W '$(_parse_help help "$1")' -- "$cur"))
Expand Down Expand Up @@ -2301,24 +2319,26 @@ _comp_command_offset()

local retry_count=0
while true; do # loop for the retry request by status 124
local args original_cur=${comp_args[1]-$cur}
if ((${#COMP_WORDS[@]} >= 2)); then
args=("$cmd" "$original_cur" "${COMP_WORDS[-2]}")
else
args=("$cmd" "$original_cur")
fi

if [[ ! $cspec ]]; then
if ((${#COMPREPLY[@]} == 0)); then
# XXX will probably never happen as long as completion loader loads
# *something* for every command thrown at it ($cspec != empty)
_minimal
_minimal "${args[@]}"
fi
elif [[ $cspec == *' -F '* ]]; then
# complete -F <function>

# get function name
local func=${cspec#* -F }
func=${func%% *}

if ((${#COMP_WORDS[@]} >= 2)); then
$func "$cmd" "${COMP_WORDS[-1]}" "${COMP_WORDS[-2]}"
else
$func "$cmd" "${COMP_WORDS[-1]}"
fi
$func "${args[@]}"

# restart completion (once) if function exited with 124
if (($? == 124 && retry_count++ == 0)); then
Expand Down Expand Up @@ -2395,8 +2415,8 @@ _complete_as_root()

_longopt()
{
local cur prev words cword split
_init_completion -s || return
local cur prev words cword split comp_args
_comp_initialize -s -- "$@" || return

case "${prev,,}" in
--help | --usage | --version)
Expand Down Expand Up @@ -2454,8 +2474,8 @@ declare -Ag _xspecs

_filedir_xspec()
{
local cur prev words cword
_init_completion || return
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

_tilde "$cur" || return

Expand Down Expand Up @@ -2576,8 +2596,8 @@ unset -f _install_xspec
# Minimal completion to use as fallback in _completion_loader.
_minimal()
{
local cur prev words cword split
_init_completion -s || return
local cur prev words cword split comp_args
_comp_initialize -s -- "$@" || return
$split && return
_filedir
}
Expand Down
4 changes: 2 additions & 2 deletions completions/2to3
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

_2to3()
{
local cur prev words cword split
_init_completion -s || return
local cur prev words cword split comp_args
_comp_initialize -s -- "$@" || return

case $prev in
-h | --help | --add-suffix)
Expand Down
6 changes: 3 additions & 3 deletions completions/7z
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

_7z()
{
local cur prev words cword
_init_completion -n = || return
local cur prev words cword comp_args
_comp_initialize -n = -- "$@" || return

if ((cword == 1)); then
COMPREPLY=($(compgen -W 'a b d e h i l rn t u x' -- "$cur"))
Expand Down Expand Up @@ -99,7 +99,7 @@ _7z()
local args
_count_args "="
if ((args == 2)); then
_filedir_xspec unzip
_filedir_xspec unzip "${@:2}"
# TODO: parsing 7z i output?
# - how to figure out if the format is input or output?
# - find string Formats:, read until next empty line
Expand Down
4 changes: 2 additions & 2 deletions completions/_adb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ _adb_command_usage()

_adb()
{
local cur prev words cword
_init_completion || return
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

case $prev in
-s | -p | --algo | --key | --iv)
Expand Down
4 changes: 2 additions & 2 deletions completions/_cal
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

_cal()
{
local cur prev words cword
_init_completion || return
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

case $prev in
-m)
Expand Down
4 changes: 2 additions & 2 deletions completions/_chsh
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

_chsh()
{
local cur prev words cword
_init_completion || return
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

local word chroot
for word in "${words[@]}"; do
Expand Down
4 changes: 2 additions & 2 deletions completions/_dmesg
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ _dmesg()
{
[[ $OSTYPE == *solaris* ]] && return # no args there

local cur prev words cword
_init_completion || return
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

case $prev in
-h | --help | -V | --version | -s | --buffer-size | -M | -N)
Expand Down
4 changes: 2 additions & 2 deletions completions/_eject
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

_eject()
{
local cur prev words cword
_init_completion || return
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

case $prev in
-h | --help | -V | --version | -c | --changerslot | -x | --cdspeed)
Expand Down
4 changes: 2 additions & 2 deletions completions/_hexdump
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

_hexdump()
{
local cur prev words cword
_init_completion || return
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

case $prev in
-V | -e | -n | -s)
Expand Down
4 changes: 2 additions & 2 deletions completions/_hwclock
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

_hwclock()
{
local cur prev words cword
_init_completion || return
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

case $prev in
-h | --help | -V | --version | --date | --epoch)
Expand Down
4 changes: 2 additions & 2 deletions completions/_ionice
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

_ionice()
{
local cur prev words cword
_init_completion || return
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

local offset=0 i
for ((i = 1; i <= cword; i++)); do
Expand Down
4 changes: 2 additions & 2 deletions completions/_look
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

_look()
{
local cur prev words cword
_init_completion || return
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

if ((cword == 1)); then
COMPREPLY=($(compgen -W '$(look "$cur" 2>/dev/null)' -- "$cur"))
Expand Down
4 changes: 2 additions & 2 deletions completions/_mock
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

_mock()
{
local cur prev words cword split
_init_completion -s || return
local cur prev words cword split comp_args
_comp_initialize -s -- "$@" || return

local plugins='tmpfs root_cache yum_cache bind_mount ccache'
local cfgdir=/etc/mock count=0 i
Expand Down
4 changes: 2 additions & 2 deletions completions/_modules
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ _module_avail()
# A completion function for the module alias
_module()
{
local cur prev words cword
_init_completion || return
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

if ((cword == 1)); then
# First parameter on line -- we expect it to be a mode selection
Expand Down
4 changes: 2 additions & 2 deletions completions/_mount
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ fi
#
_mount()
{
local cur prev words cword
_init_completion -n : || return
local cur prev words cword comp_args
_comp_initialize -n : -- "$@" || return

local sm host

Expand Down
4 changes: 2 additions & 2 deletions completions/_mount.linux
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

_mount()
{
local cur prev words cword
_init_completion -n =: || return
local cur prev words cword comp_args
_comp_initialize -n =: -- "$@" || return

local split=false
case "$prev" in
Expand Down
4 changes: 2 additions & 2 deletions completions/_newgrp
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

_newgrp()
{
local cur prev words cword
_init_completion || return
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

if [[ $cur == "-" ]]; then
COMPREPLY=(-)
Expand Down
4 changes: 2 additions & 2 deletions completions/_nmcli
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ _nmcli_ab_bssid()

_nmcli()
{
local cur prev words cword
_init_completion || return
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

case $prev in
-m | --mode)
Expand Down
4 changes: 2 additions & 2 deletions completions/_op
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ _op_command_options()

_op()
{
local cur prev words cword split
_init_completion -s || return
local cur prev words cword split comp_args
_comp_initialize -s -- "$@" || return

local command i
for ((i = 1; i < cword; i++)); do
Expand Down
4 changes: 2 additions & 2 deletions completions/_renice
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

_renice()
{
local cur prev words cword
_init_completion || return
local cur prev words cword comp_args
_comp_initialize -- "$@" || return

local command=$1 curopt i=0

Expand Down
4 changes: 2 additions & 2 deletions completions/_repomanage
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

_repomanage()
{
local cur prev words cword split
_init_completion -s || return
local cur prev words cword split comp_args
_comp_initialize -s -- "$@" || return

[[ $prev == -@([hk]|-help|-keep) ]] && return

Expand Down
Loading