Complete Alias
Complete Alias
/bin/bash
## ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
## automagical shell alias completion;
## ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
## ============================================================================
## Copyright (C) 2016-2021 Cyker Way
##
## This program is free software: you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by the Free
## Software Foundation, either version 3 of the License, or (at your option)
## any later version.
##
## This program is distributed in the hope that it will be useful, but WITHOUT
## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
## more details.
##
## You should have received a copy of the GNU General Public License along with
## this program. If not, see <http://www.gnu.org/licenses/>.
## ============================================================================
## ============================================================================
## # environment variables
##
## these are envars read by this script; users are advised to set these envars
## before sourcing this script to customize its behavior, even though some may
## still work if set after sourcing this script; these envar names must follow
## this naming convention: all letters uppercase, no leading underscore, words
## separated by one underscore;
## ============================================================================
## bool: true iff auto unmask alias commands; set it to false if auto unmask
## feels too slow, or custom unmask is necessary to make an unusual behavior;
COMPAL_AUTO_UNMASK="${COMPAL_AUTO_UNMASK:-0}"
## ============================================================================
## # variables
## ============================================================================
## a set of raw vanilla completions, keyed by cspec; these raw cspecs will be
## parsed and loaded into `_vanilla_cspecs` on use; we need this lazy loading
## because parsing all cspecs on sourcing incurs a large performance overhead;
##
## vanilla completions are alias-free and fetched before `_complete_alias` is
## set as the completion function for alias commands; the way we enforce this
## partial order is to init this array on source; the sourcing happens before
## `complete -F _complete_alias ...` for obvious reasons;
##
## this is made a set, not an array, to avoid duplication when this script is
## sourced repeatedly; each sourcing overwrites previous ones on duplication;
##
## example:
##
## _raw_vanilla_cspecs["complete -F _longopt tee"]=""
## _raw_vanilla_cspecs["complete -c type"]=""
## _raw_vanilla_cspecs["complete -a unalias"]=""
## ...
##
declare -A __compal__raw_vanilla_cspecs
## ============================================================================
## # functions
## ============================================================================
## debug `_split_cmd_line`;
##
## this function is very easy to use; just call it with a string argument in an
## interactive shell and look at the result; some interesting string arguments:
##
## - (fail) `&> /dev/null ping`
## - (fail) `2> /dev/null ping`
## - (fail) `2>&1 > /dev/null ping`
## - (fail) `> /dev/null ping`
## - (work) `&>/dev/null ping`
## - (work) `2>&1 >/dev/null ping`
## - (work) `2>&1 ping`
## - (work) `2>/dev/null ping`
## - (work) `>/dev/null ping`
## - (work) `FOO=foo true && BAR=bar ping`
## - (work) `echo & echo & ping`
## - (work) `echo ; echo ; ping`
## - (work) `echo | echo | ping`
## - (work) `ping &> /dev/null`
## - (work) `ping &>/dev/null`
## - (work) `ping 2> /dev/null`
## - (work) `ping 2>&1 > /dev/null`
## - (work) `ping 2>&1 >/dev/null`
## - (work) `ping 2>&1`
## - (work) `ping 2>/dev/null`
## - (work) `ping > /dev/null`
## - (work) `ping >/dev/null`
##
## these failed examples are not an emergency because you can easily find their
## equivalents in those working ones; and we will check for emergency on failed
## examples added in the future;
##
## $1
## : command line string;
__compal__debug_split_cmd_line() {
## command line string;
local str="$1"
__compal__split_cmd_line "$str"
for x in "${__compal__retval[@]}"; do
echo "'$x'"
done
}
## alloc a temp stack to track open and close chars when splitting;
local sta=()
## examine each char of `str`; test branches are ordered; this order has
## two importances: first is to respect substring relationship (eg: `&&`
## must be tested before `&`); second is to test in optimistic order for
## speeding up the testing; the first importance is compulsory and takes
## precedence;
local i=0 j=0
for (( ; j < ${#str}; j++ )); do
if (( ${#sta[@]} == 0 )); then
if [[ "${str:j:1}" =~ [_a-zA-Z0-9] ]]; then
:
elif [[ $' \t\n' == *"${str:j:1}"* ]]; then
if (( i < j )); then
if (( $found_redass == 1 )); then
if (( $check_redass == 0 )); then
words+=( "${str:i:j-i}" )
fi
found_redass=0
else
## no redass in current word; stop checking;
check_redass=0
words+=( "${str:i:j-i}" )
fi
fi
(( i = j + 1 ))
elif [[ ":" == *"${str:j:1}"* ]]; then
if (( i < j )); then
if (( $found_redass == 1 )); then
if (( $check_redass == 0 )); then
words+=( "${str:i:j-i}" )
fi
found_redass=0
else
## no redass in current word; stop checking;
check_redass=0
words+=( "${str:i:j-i}" )
fi
fi
words+=( "${str:j:1}" )
(( i = j + 1 ))
elif [[ '$(' == "${str:j:2}" ]]; then
sta+=( ')' )
(( j++ ))
elif [[ '`' == "${str:j:1}" ]]; then
sta+=( '`' )
elif [[ '(' == "${str:j:1}" ]]; then
sta+=( ')' )
elif [[ '{' == "${str:j:1}" ]]; then
sta+=( '}' )
elif [[ '"' == "${str:j:1}" ]]; then
sta+=( '"' )
elif [[ "'" == "${str:j:1}" ]]; then
sta+=( "'" )
elif [[ '\' == "${str:j:1}" ]]; then
(( j++ ))
elif [[ '&>' == "${str:j:2}" ]]; then
found_redass=1
(( j++ ))
elif [[ '>&' == "${str:j:2}" ]]; then
found_redass=1
(( j++ ))
elif [[ "><=" == *"${str:j:1}"* ]]; then
found_redass=1
elif [[ '&&' == "${str:j:2}" ]]; then
words=()
check_redass=1
(( i = j + 2 ))
elif [[ '||' == "${str:j:2}" ]]; then
words=()
check_redass=1
(( i = j + 2 ))
elif [[ '&' == "${str:j:1}" ]]; then
words=()
check_redass=1
(( i = j + 1 ))
elif [[ '|' == "${str:j:1}" ]]; then
words=()
check_redass=1
(( i = j + 1 ))
elif [[ ';' == "${str:j:1}" ]]; then
words=()
check_redass=1
(( i = j + 1 ))
fi
elif [[ "${sta[-1]}" == ')' ]]; then
if [[ ')' == "${str:j:1}" ]]; then
unset sta[-1]
elif [[ '$(' == "${str:j:2}" ]]; then
sta+=( ')' )
(( j++ ))
elif [[ '`' == "${str:j:1}" ]]; then
sta+=( '`' )
elif [[ '(' == "${str:j:1}" ]]; then
sta+=( ')' )
elif [[ '{' == "${str:j:1}" ]]; then
sta+=( '}' )
elif [[ '"' == "${str:j:1}" ]]; then
sta+=( '"' )
elif [[ "'" == "${str:j:1}" ]]; then
sta+=( "'" )
elif [[ '\' == "${str:j:1}" ]]; then
(( j++ ))
fi
elif [[ "${sta[-1]}" == '}' ]]; then
if [[ '}' == "${str:j:1}" ]]; then
unset sta[-1]
elif [[ '$(' == "${str:j:2}" ]]; then
sta+=( ')' )
(( j++ ))
elif [[ '`' == "${str:j:1}" ]]; then
sta+=( '`' )
elif [[ '(' == "${str:j:1}" ]]; then
sta+=( ')' )
elif [[ '{' == "${str:j:1}" ]]; then
sta+=( '}' )
elif [[ '"' == "${str:j:1}" ]]; then
sta+=( '"' )
elif [[ "'" == "${str:j:1}" ]]; then
sta+=( "'" )
elif [[ '\' == "${str:j:1}" ]]; then
(( j++ ))
fi
elif [[ "${sta[-1]}" == '`' ]]; then
if [[ '`' == "${str:j:1}" ]]; then
unset sta[-1]
elif [[ '$(' == "${str:j:2}" ]]; then
sta+=( ')' )
(( j++ ))
elif [[ '(' == "${str:j:1}" ]]; then
sta+=( ')' )
elif [[ '{' == "${str:j:1}" ]]; then
sta+=( '}' )
elif [[ '"' == "${str:j:1}" ]]; then
sta+=( '"' )
elif [[ "'" == "${str:j:1}" ]]; then
sta+=( "'" )
elif [[ '\' == "${str:j:1}" ]]; then
(( j++ ))
fi
elif [[ "${sta[-1]}" == "'" ]]; then
if [[ "'" == "${str:j:1}" ]]; then
unset sta[-1]
fi
elif [[ "${sta[-1]}" == '"' ]]; then
if [[ '"' == "${str:j:1}" ]]; then
unset sta[-1]
elif [[ '$(' == "${str:j:2}" ]]; then
sta+=( ')' )
(( j++ ))
elif [[ '`' == "${str:j:1}" ]]; then
sta+=( '`' )
elif [[ '\$' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\`' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\"' == "${str:j:2}" ]]; then
(( j++ ))
elif [[ '\\' == "${str:j:2}" ]]; then
(( j++ ))
fi
fi
done
## return value;
__compal__retval=( "${words[@]}" )
}
## return value;
__compal__retval=$(( ${#words0[@]} - 1 + diff0 + diff1 ))
fi
}
case "$cmd" in
bind)
complete -A binding "$cmd"
;;
help)
complete -A helptopic "$cmd"
;;
set)
complete -A setopt "$cmd"
;;
shopt)
complete -A shopt "$cmd"
;;
bg)
complete -A stopped -P '"%' -S '"' "$cmd"
;;
service)
complete -F _service "$cmd"
;;
unalias)
complete -a "$cmd"
;;
builtin)
complete -b "$cmd"
;;
command|type|which)
complete -c "$cmd"
;;
fg|jobs|disown)
complete -j -P '"%' -S '"' "$cmd"
;;
groups|slay|w|sux)
complete -u "$cmd"
;;
readonly|unset)
complete -v "$cmd"
;;
traceroute|traceroute6|tracepath|tracepath6|fping|fping6|telnet|rsh|\
rlogin|ftp|dig|mtr|ssh-installkeys|showmount)
complete -F _known_hosts "$cmd"
;;
aoss|command|do|else|eval|exec|ltrace|nice|nohup|padsp|then|time|\
tsocks|vsound|xargs)
complete -F _command "$cmd"
;;
fakeroot|gksu|gksudo|kdesudo|really)
complete -F _root_command "$cmd"
;;
a2ps|awk|base64|bash|bc|bison|cat|chroot|colordiff|cp|csplit|cut|date|\
df|diff|dir|du|enscript|env|expand|fmt|fold|gperf|grep|grub|head|\
irb|ld|ldd|less|ln|ls|m4|md5sum|mkdir|mkfifo|mknod|mv|netstat|nl|\
nm|objcopy|objdump|od|paste|pr|ptx|readelf|rm|rmdir|sed|seq|\
sha{,1,224,256,384,512}sum|shar|sort|split|strip|sum|tac|tail|tee|\
texindex|touch|tr|uname|unexpand|uniq|units|vdir|wc|who)
complete -F _longopt "$cmd"
;;
*)
_completion_loader "$cmd"
;;
esac
}
## unmask alias:
__compal__unmask_alias "$cmd"
## do actual completion;
__compal__delegate
## remask alias:
__compal__remask_alias "$cmd"
}
## save vanilla completions; run this function when this script is sourced;
## this ensures vanilla completions of alias commands are fetched and saved
## before they are overwritten by `complete -F _complete_alias`;
##
## this function saves raw cspecs and does not parse them; for other useful
## comments about parsing and running cspecs see function `_run_cspec_args`;
##
## running this function on source is mandatory only when using auto unmask;
## when using manual unmask, it is safe to skip this function on source;
__compal__save_vanilla_cspecs() {
## get default cspec;
local def_cspec; def_cspec="$(complete -p -D 2>/dev/null)"
## `complete -p` prints cspec for one command per line; so we can loop;
while IFS= read -r cspec; do
## we expand aliases only for the original command line (ie: the command
## line on which user pressed `<tab>`); unfortunately, we may not have a
## chance to see the original command line, and we have no way to ensure
## that; we take an approximation: we expand aliases only in the outmost
## call of this function, which implies only on the first occasion of an
## alias command; we can ensure this condition using a refcnt and expand
## aliases iff the refcnt is equal to 0; this approximation always works
## correctly when the 1st word on the original command line is an alias;
##
## this approximation may fail when the 1st word on the original command
## line is not an alias; an example that expects files but gets ip addrs:
##
## $ unalias sudo
## $ complete -r sudo
## $ alias ls='ping'
## $ complete -F _complete_alias ls
## $ sudo ls <tab>
## {ip}
## {ip}
## {ip}
## ...
##
if (( __compal__refcnt == 0 )); then
## expand aliases;
__compal__expand_alias 0 "${#COMP_WORDS[@]}" "$ignore" 0
fi
## increase refcnt;
(( __compal__refcnt++ ))
## decrease refcnt;
(( __compal__refcnt-- ))
}
## this is the function to be set with `complete -F`; this function expects
## alias commands, but can also handle non-alias commands in rare occasions;
##
## as a standard completion function, this function can take 3 arguments as
## described in `man bash`; they are currently not being used, though;
##
## $1
## : the name of the command whose arguments are being completed;
## $2
## : the word being completed;
## $3
## : the word preceding the word being completed on the current command line;
_complete_alias() {
## get command;
local cmd="${COMP_WORDS[0]}"
## complete command;
if ! alias "$cmd" &>/dev/null; then
__compal__complete_non_alias "$@"
else
__compal__complete_alias "$@"
fi
}
## main function;
__compal__main() {
if (( "$COMPAL_AUTO_UNMASK" == 1 )); then
## save vanilla completions;
__compal__save_vanilla_cspecs
fi
}
## ============================================================================
## # script
## ============================================================================
## ============================================================================
## # complete user-defined aliases
## ============================================================================
## to complete all aliases, run this line after all aliases have been defined;
complete -F _complete_alias "${!BASH_ALIASES[@]}"
# complete -F _complete_alias ta