This repository has been archived by the owner on Dec 11, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpadre
executable file
·106 lines (103 loc) · 3.97 KB
/
padre
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
98
99
100
101
102
103
104
105
106
# bash completion for padre
# Put this in your bash_completion directory or add
# 'source path/to/dir/padre' to your bashrc and
# running shells.
# If sourcing from the current dir, use `source ./padre'
# NOT 'source padre', which sources the program padre, not this file.
##PERL6_COMMON>>
# Prepending with _perl6_ so there are no conflicts when sourcing multiple files
_perl6_match() { # Succeeds if argument is repeated. Must be an EXACT MATCH.
#usage, _perl6_match 'str to match' a list of str #returns fale/1;
#usage, _perl6_match 'matched str' a 'matched str' was found #returns true/0;
repcur="$1"
shift
for val in "$@"; do
[[ "${repcur}" == "${val}" ]] && return 0
done
return 1
}
_perl6_assoc() { # Removes associated arguments. usage, from _perl6_rem: _perl6_assoc $i -h --help
asscur=$1; shift
akv=($@)
for (( k=0; k<$((${#akv[@]}-1)); k+=2)); do
v=$(($k+1))
[[ "$asscur" == "${akv[$k]}" ]] && {
COMPREPLY=" ${COMPREPLY[@]} "
COMPREPLY=(${COMPREPLY/ ${akv[$v]%% *} / })
}
[[ "$asscur" == "${akv[$v]}" ]] && {
COMPREPLY=" ${COMPREPLY[@]} "
COMPREPLY=(${COMPREPLY/ ${akv[$k]%% *} / })
}
done
printf "%s\n" "${COMPREPLY[@]}"
}
_perl6_seper() { # Prints the arguments that are deliminated by 'cnt' number of 'sep's. ie:
# _perl6_seper 1 '--' -a -1 -- -2 -b -- c 3 ; #yeilds -2 -b; _perl6_seper 0 yeilds -a -1
cnt=$1; shift
sep=$1; shift
args=("$@")
idx=0
zarg=0
for ((iter=0; iter<=$cnt; iter++)); do
zarg=1
while [[ "$idx" -lt "${#args[@]}" ]]; do
zarg=0
arg="${args[$idx]}"; ((idx++))
[[ "$arg" == "$sep" ]] && {
[ "$iter" -eq "$cnt" ] && return 0
break
}
[[ "$iter" == "$cnt" ]] && printf "%s\n" "${arg}" # Im not sure how this will handle spaces
done
[ "$zarg" -ne "0" ] && return 1
iterend=$iter
done
[ "$iterend" -eq "$cnt" ] && return 0 # may chose to do 3 way in future
return 1
}
# NOTE: This slows things down!
# Taken from gentoo's adaption of Ian Macdonald's bash_completion.
# (Adapted from bash_completion by Ian Macdonald <ian@caliban.org>)
# This removes any options from the list of completions that have
# already been specified on the command line.
_perl6_rem() { # removes arguments previously used.
# _perl6_rem repeatable words -- associated pairs -- words we shouldn't add spaces after
rep_words=($(_perl6_seper '0' '--' "$@")) # These words can be repeated
assoc_words=($(_perl6_seper '1' '--' "$@")) # These words are paired. If we have seen one, we will not sugest either.
nospace_words=($(_perl6_seper '2' '--' "$@")) # these words should not have a space appended to them (eg. --output= )
# NOTE i wonder why he didnt use a for loop? (would IFS need to be changed?)
COMPREPLY=($(echo "${COMP_WORDS[@]}" | \
(while read -d ' ' i; do
[[ -z ${i} ]] && continue # next word if null.
_perl6_match "${i}" "${rep_words[@]}" && continue # next if word may repeat
COMPREPLY=($(_perl6_assoc "${i}" "${assoc_words[@]}")) # Remove word if its pair has been typed
COMPREPLY=" ${COMPREPLY[@]} " # flatten array with spaces on either side, so we can grep on word boundaries of first and last word.
COMPREPLY=("${COMPREPLY/ ${i%% *} / }") # remove word from list of completions
done
echo ${COMPREPLY[@]})))
# If only one match was found, and that match is a nospace_word, tell bash not to add a space after the completion.
[[ "${#COMPREPLY[@]}" -eq '1' ]] && [[ "${#nospace_words[@]}" -ge '1' ]] && {
_perl6_match "$COMPREPLY" "${nospace_words[@]}" && compopt -o nospace
}
return 0
}
#_BC_dbg() { # used for debuging
# fifo_pipe="${HOME}/tmp/pipe/perl6cmdcomp.fifo"
# [[ -p "$fifo_pipe" ]] && echo "$@" > "$fifo_pipe"
#}
##<<PERL6_COMMON
_padre() {
local cur prev words cword
_init_completion || return
longargs="--help --reset --version --desktop"
unlong="--home= --session= --actionqueue= --locale="
case "$cur" in
-*)
COMPREPLY=( $( compgen -W '$longargs $unlong' -- "$cur" ) )
_perl6_rem $repargs '--' $assocargs -- $unlong
return 0
;;
esac
}
complete -F _padre -o default padre