Repository navigation
Expand file tree
/
Copy pathcompletion_bash.bash
More file actions
78 lines (77 loc) · 3.01 KB
/
Copy pathcompletion_bash.bash
File metadata and controls
78 lines (77 loc) · 3.01 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
_@FUNCTION@_quote_context() {
# COMP_POINT is a byte offset, independently of the interactive locale.
# Only inspect syntax; never evaluate the command line or provider values.
local LC_ALL=C line="${COMP_LINE:-}" char next i
quote=
for ((i=0; i<${COMP_POINT:-0}; i++)); do
char="${line:i:1}"
next="${line:i+1:1}"
if [[ "$char" == \\ && "$quote" != "'" ]] &&
[[ -z "$quote" || "$quote" == ansi || "$next" == \\ || "$next" == '"' ||
"$next" == '$' || "$next" == '`' || "$next" == $'\n' ]]; then
((i++))
elif [[ -n "$quote" && ( "$char" == "$quote" || ( "$quote" == ansi && "$char" == "'" ) ) ]]; then
quote=
elif [[ -z "$quote" && "$char" == '$' && "$next" == "'" ]]; then
quote=ansi
((i++))
elif [[ -z "$quote" && ( "$char" == "'" || "$char" == '"' ) ]]; then
quote="$char"
fi
done
}
_@FUNCTION@() {
local command="$1" field mode path candidate quote i
local -a response=()
while IFS= read -r -d '' field; do
response+=("$field")
done < <("$command" @BASHREQUEST@ 1 bash "${COMP_POINT:-0}" "${COMP_LINE:-}" "${2:-}" 2>/dev/null)
COMPREPLY=()
((${#response[@]} >= 7)) && [[ "${response[0]}" == 1 ]] || return 0
[[ "${response[2]}" == 1 ]] && compopt -o nospace
[[ "${response[3]}" == 1 ]] && compopt -o nosort
for ((i=7; i<${#response[@]}; i++)); do
COMPREPLY+=("${response[i]}")
done
mode="${response[1]}"
if [[ "$mode" != 0 ]]; then
local action=-f
[[ "$mode" == 2 ]] && action=-d
while IFS= read -r path; do
candidate="${response[4]}$path"
candidate="${candidate#"${response[6]}"}"
if [[ -d "$path" ]]; then
candidate="${candidate%/}/"
compopt -o nospace
fi
COMPREPLY+=("$candidate")
done < <(compgen "$action" -- "${response[5]}")
fi
# Readline's filename quoting can leave leading $ and backticks active.
# Supply literal shell syntax ourselves, preserving an unfinished quote.
_@FUNCTION@_quote_context
# The request lexer supports simple quotes, not ANSI-C escape decoding.
# Do not insert single-quote syntax inside an active $'...' expression.
if [[ "$quote" == ansi ]]; then
COMPREPLY=()
return 0
fi
for i in "${!COMPREPLY[@]}"; do
candidate="${COMPREPLY[i]}"
case "$quote" in
"'") candidate="'${candidate//\'/\'\\\'\'}'" ;;
'"')
candidate="${candidate//\\/\\\\}"
candidate="${candidate//\$/\\\$}"
candidate="${candidate//\`/\\\`}"
candidate="${candidate//\"/\\\"}"
candidate="${candidate//\!/\"\'\!\'\"}"
candidate="\"$candidate\""
;;
*) printf -v candidate '%q' "$candidate" ;;
esac
COMPREPLY[i]="$candidate"
done
return 0
}
complete -o noquote -F _@FUNCTION@ -- @BASHCOMMAND@