summaryrefslogtreecommitdiff
blob: 64cf7df8abc32278fddd6ee2f3a0ebeec3ffd481 (plain)
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
107
108
109
110
111
# Copyright 1999-2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id: bashcomp,v 1.1 2005/04/06 14:15:21 ka0ttic Exp $
#
# Author: Aaron Walker <ka0ttic%gentoo.org>
#
# completion for bash-completion-config 

# echo all completions in BASHCOMP_DIR
complete_avail()
{
    local bashcomp BASHCOMP_DIR="/usr/share/bash-completion"
    for bashcomp in $BASHCOMP_DIR/* ; do
        echo -n "${bashcomp##*/} "
    done
}

# echo all installed completions in $1
complete_installed()
{
    local bashcomp
    for bashcomp in $1/* ; do
        echo -n "${bashcomp##*/} "
    done
}

# show completions for $1
complete_on()
{
    local cur="$1"

    case "$2" in
        install)
            COMPREPLY=($(compgen -f -X '*~' -W "$(complete_avail)" -- "${cur}"))
            ;;
        uninstall)
            if [ -z "$3" ] ; then
                COMPREPLY=($(compgen -X '*~' -W "$(complete_installed \
                    ${HOME}/.bash_completion.d)" -- "${cur}"))
            else
                COMPREPLY=($(compgen -X '*~' -W "$(complete_installed \
                /etc/bash_completion.d)" -- "${cur}"))
            fi
            ;;
    esac
}

_bash_completion_config()
{
    local cur prev opts i a=0
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts="-g --global -l --list -i --install -u --uninstall -h --help -d --debug -V --version -nc --nocolor"
    
    if [[ "${cur}" == -* || ${COMP_CWORD} -eq 1 ]] ; then
        COMPREPLY=($(compgen -W "${opts}" -- "${cur}"))
        return 0
    fi

    case "${prev}" in
        -nc|--nocolor|--nocolour)
            COMPREPLY=($(compgen -W "${opts/-nc --nocolor/}" -- "${cur}"))
            ;;
        -d|--debug)
            COMPREPLY=($(compgen -W "${opts/-d --debug/}" -- "${cur}"))
            ;;
        -g|--global)
            for i in ${COMP_WORDS[@]} ; do
                [[ "${i}" == "-i" || "${i}" == "--install" ||
                   "${i}" == "-u" || "${i}" == "--uninstall" ]] && a=1
                [[ "${i}" == "-nc" || "${i}" == "--nocolor" ||
                   "${i}" == "--nocolour" ]] && a=1
            done
            
            if [ ${a} -gt 0 ] ; then
                action=install
                complete_on "${cur}" "${action}"
            else
                COMPREPLY=($(compgen -W "-i --install -u --uninstall" -- "${cur}"))
            fi
            ;;
        -i|--install)
            action=install
            complete_on "${cur}" "${action}"
            ;;
        -u|--uninstall)
            action=uninstall

            for i in ${COMP_WORDS[@]} ; do
                if [[ "${i}" == "-g" || "${i}" == "--global" ]] ; then
                    global=1
                fi
            done

            if [ -z "${global}" ] ; then
                complete_on "${cur}" "${action}"
            else
                complete_on "${cur}" "${action}" glob
            fi
            ;;
        *)
            [ -n "${action}" ] && complete_on "${cur}" "${action}"
            ;;
    esac
    
    unset global
}
complete -F _bash_completion_config bash-completion-config

# vim: set ft=sh tw=80 sw=4 et :