-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalias_completion.bash
More file actions
97 lines (88 loc) · 5 KB
/
alias_completion.bash
File metadata and controls
97 lines (88 loc) · 5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# Automatically add completion for all aliases to commands having completion functions
# Fork of https://superuser.com/revisions/437508/21
function alias_completion {
local namespace="alias_completion"
# parse function based completion definitions, where capture group 2 => function and 3 => trigger
local compl_regex='complete( +[^ ]+)* -F ([^ ]+) ("[^"]+"|[^ ]+)'
# parse alias definitions, where capture group 1 => trigger, 2 => command, 3 => command arguments
local alias_regex="alias ([^=]+)='(\"[^\"]+\"|[^ ]+)(( +[^ ]+)*)'"
# build associative array of completion specs (trigger => full spec line)
local _compl_line
declare -A _comp_specs
while IFS= read -r _compl_line; do
if [[ $_compl_line =~ $compl_regex ]]; then
_comp_specs["${BASH_REMATCH[3]}"]="$_compl_line"
fi
done < <(complete -p 2>/dev/null)
(( ${#_comp_specs[@]} == 0 )) && return 0
# create temporary file for wrapper functions and completions
local tmp_file; tmp_file=$(mktemp 2>/dev/null || mktemp -t 'tmp') || return 1
local completion_loader; completion_loader="$(complete -p -D 2>/dev/null | sed -Ene 's/.* -F ([^ ]*).*/\1/p')"
# read in "<alias> '<aliased command>' '<command args>'" lines from defined aliases
local line; while read line; do
eval "local alias_tokens; alias_tokens=($line)" 2>/dev/null || continue # some alias arg patterns cause an eval parse error
local alias_name="${alias_tokens[0]}" alias_cmd="${alias_tokens[1]}" alias_args="${alias_tokens[2]# }"
# Skip aliases to pipes, boolean control structures and other command lists,
# by leveraging that bash errs when unquoted metacharacters appear in an array
# assignment (e.g. `x=(cmd1 | cmd2)` is a syntax error).
#
# We can't just `eval "x=($alias_args)"` because that would execute command
# substitutions ($(), ``, and even <(), >()). Instead we escape $, (, ), `
# and disable globbing (set -f), so eval parses without side effects.
#
# This also determines the word count for COMP_CWORD: each escaped $(...)
# becomes a single literal word (e.g. `\$\(g-main\)`), which is correct for
# quoted "$(cmd)" and for unquoted $(cmd) that expands to a single word.
# Unquoted $(cmd) that expands to multiple words at runtime will have an
# approximate count — a pre-existing limitation since the wrapper bakes in
# a fixed COMP_CWORD offset regardless.
#
# Limitation: if unquoted $() contains metacharacters (e.g. `$(cmd | pipe)`),
# escaping the parens exposes them to the top-level parser, falsely rejecting
# the alias. This is acceptable since such aliases have unknowable word counts.
local _safe="${alias_args//\$/\\\$}"
_safe="${_safe//\(/\\(}"
_safe="${_safe//\)/\\)}"
_safe="${_safe//\`/\\\`}"
local _had_noglob; [[ $- == *f* ]] && _had_noglob=1
set -o noglob
eval "local alias_arg_words; alias_arg_words=($_safe)" 2>/dev/null
local _eval_ok=$?
[[ -z $_had_noglob ]] && set +o noglob
(( _eval_ok != 0 )) && continue
# skip alias if there is no completion function triggered by the aliased command
if [[ ! -v _comp_specs[$alias_cmd] ]]; then
if [[ -n "$completion_loader" ]]; then
# force loading of completions for the aliased command
eval "$completion_loader $alias_cmd"
# 124 means completion loader was successful
[[ $? -eq 124 ]] || continue
_comp_specs["$alias_cmd"]="$(complete -p "$alias_cmd" 2>/dev/null)"
else
continue
fi
fi
local new_completion="${_comp_specs[$alias_cmd]}"
# create a wrapper inserting the alias arguments if any
if [[ -n $alias_args ]]; then
local compl_func="${new_completion/#* -F /}"; compl_func="${compl_func%% *}"
# avoid recursive call loops by ignoring our own functions
if [[ "${compl_func#_$namespace::}" == $compl_func ]]; then
local compl_wrapper="_${namespace}::${alias_name}"
echo "function $compl_wrapper {
(( COMP_CWORD += ${#alias_arg_words[@]} ))
COMP_WORDS=($alias_cmd $alias_args \${COMP_WORDS[@]:1})
(( COMP_POINT -= \${#COMP_LINE} ))
COMP_LINE=\${COMP_LINE/$alias_name/$alias_cmd $alias_args}
(( COMP_POINT += \${#COMP_LINE} ))
$compl_func
}" >> "$tmp_file"
new_completion="${new_completion/ -F $compl_func / -F $compl_wrapper }"
fi
fi
# replace completion trigger by alias
new_completion="${new_completion% *} $alias_name"
echo "$new_completion" >> "$tmp_file"
done < <(alias -p | sed -Ene "s/$alias_regex/\1 '\2' '\3'/p")
source "$tmp_file" && rm -f "$tmp_file"
}; alias_completion