1# Bash auto-completion script written for lv2info and lv2jack.
2# Could be adapted to any other program that takes an
3# LV2 plugin URI as parameter.
4
5# Updated for Lilv by David Robillard <d@drobilla.net> on 2012-01-08.
6# Written by Lars Luthman <lars.luthman@gmail.com> on 2009-10-12.
7# No copyright claimed for this script. Do what you want with it.
8
9# For some reason Bash splits the command line not only at whitespace
10# but also at ':' signs before putting the parts into COMP_WORDS.
11# Since ':' is used in all URIs, which are what we want to complete,
12# we have to put the URI back together before we can complete it
13# and then cut off the parts we prepended from the completions.
14# It probably breaks in some special cases but for most common uses
15# it should work fine.
16
17function _lv2info() {
18    local uri cur opts w wn raw_reply len type
19    opts=`lv2ls | xargs -n1 echo -n " "`
20
21    # This is the last "word", as split by Bash.
22    cur="${COMP_WORDS[COMP_CWORD]}"
23    w="$cur"
24
25    # Add the previous word while it or this one is a word break character
26    for i in `seq $(( $COMP_CWORD - 1 )) -1 1`; do
27	wn="${COMP_WORDS[i]}"
28	if expr "$COMP_WORDBREAKS" : ".*$wn" > /dev/null; then
29	    if expr "$COMP_WORDBREAKS" : ".*$w" > /dev/null; then
30		break
31	    fi
32	fi
33	w="$wn"
34	uri="$w$uri"
35    done
36
37    # Check the length of the words we prepend
38    len=${#uri}
39    uri="$uri$cur"
40    raw_reply="$(compgen -W "${opts}" -- ${uri})"
41
42    # If we are listing alternatives, just print the full URIs.
43    type=`echo $COMP_TYPE | awk '{ printf "%c", $1 }'`
44    if expr "?!@%" : ".*$type" > /dev/null; then
45	COMPREPLY=( $raw_reply )
46	return 0
47    fi
48
49    # Otherwise, strip the prepended words from all completion suggestions.
50    COMPREPLY=()
51    for i in $raw_reply; do
52	COMPREPLY=( ${COMPREPLY[@]} ${i:len} )
53    done
54}
55
56complete -F _lv2info lv2info
57
58# And the same for lv2jack.
59complete -F _lv2info lv2jack
60