/
bash-completion
executable file
·55 lines (49 loc) · 1.79 KB
/
bash-completion
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
#!/bin/bash
complete -F _dit dit
# arguments completion function
function _dit()
{
local args=( ${COMP_LINE:0:$COMP_POINT}::cursor:: )
unset args[0]
local cur="${args[-1]}"
cur="${cur:0:-10}"
# do not innsert trailing space
compopt -o nospace
if [ "${COMP_LINE:$(( $COMP_POINT - 1 ))}" = '=' ]; then
# try to fix issues related to use of = as a word separator (this is
# a readline setting, which we can't control per command completion)
cur=
fi
# obtain completion from the application
local compl="$(dit complete ${args[@]})"
local OLDIFS="$IFS"
IFS=$'\n'
COMPREPLY=( $( compgen -W "$compl" -- "$cur" ) )
IFS="$OLDIFS"
# try to properly escape completion results
# based on snipped from: http://stackoverflow.com/a/1146716/1535516
local escaped_single_qoute="'\''"
local i=0
for entry in "${COMPREPLY[@]}"; do
if [[ "${cur:0:1}" == "'" ]]; then
# started with single quote, escaping only other single quotes
# [']bla'bla"bla\bla bla --> [']bla'\''bla"bla\bla bla
COMPREPLY[$i]="${entry//\'/${escaped_single_qoute}}"
elif [[ "${cur:0:1}" == '"' ]]; then
# started with double quote, escaping all double quotes and all
# backslashes
# ["]bla'bla"bla\bla bla --> ["]bla'bla\"bla\\bla bla
entry="${entry//\\/\\\\}"
COMPREPLY[$i]="${entry//\"/\\\"}"
else
# no quotes in front, escaping _everything_
# [ ]bla'bla"bla\bla bla --> [ ]bla\'bla\"bla\\bla\ bla
entry="${entry//\\/\\\\}"
entry="${entry//\'/\'}"
entry="${entry//\"/\\\"}"
entry="${entry//|/\\|}"
COMPREPLY[$i]="${entry// /\\ }"
fi
(( i++ ))
done
}