aboutsummaryrefslogtreecommitdiff
path: root/etc/completion/bash/guix
blob: 807a0b2e1f024887ba861069c0aa0796009722eb (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# GNU Guix --- Functional package management for GNU
# Copyright © 2015 Ludovic Courtès <ludo@gnu.org>
#
# This file is part of GNU Guix.
#
# GNU Guix is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or (at
# your option) any later version.
#
# GNU Guix is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GNU Guix.  If not, see <http://www.gnu.org/licenses/>.

# Bash completion for Guix commands.

declare _guix_available_packages

_guix_complete_available_package ()
{
    local prefix="$1"
    if [ -z "$_guix_available_packages" ]
    then
	# Cache the complete list because it rarely changes and makes
	# completion much faster.
	_guix_available_packages="$(${COMP_WORDS[0]} package -A | cut -f1)"
    fi
    COMPREPLY=($(compgen -W "$_guix_available_packages" -- "$prefix"))
}

_guix_complete_installed_package ()
{
    # Here we do not cache the list of installed packages because that
    # may change over time and the list is relatively small anyway.
    local prefix="$1"
    local packages="$(${COMP_WORDS[0]} package -I "^$prefix" | cut -f1)"
    COMPREPLY=($(compgen -W "$packages" -- "$prefix"))
}

_guix_complete_option ()
{
    local options="$(${COMP_WORDS[0]} ${COMP_WORDS[1]} --help \
                            | grep '^  \+-' \
                            | sed -e's/^.*--\([a-zA-Z0-9_-]\+\)\(=\?\).*/--\1\2/g' )"
    compopt -o nospace
    COMPREPLY=($(compgen -W "$options" -- "${COMP_WORDS[$word_count - 1]}"))
}

_guix_is_command ()
{
    local word
    local result="false"
    for word in ${COMP_WORDS[*]}
    do
	if [ "$word" = "$1" ]
	then
	    result=true
	    break
	fi
    done
    $result
}

_guix_is_removing ()
{
    local word
    local result="false"
    for word in ${COMP_WORDS[*]}
    do
	case "$word" in
	    --remove|--remove=*|-r)
		result=true
		break
		;;
	esac
    done
    $result
}

_guix_is_dash_L ()
{
    [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-L" ] \
	|| { case "${COMP_WORDS[$COMP_CWORD]}" in
		 --load-path=*) true;;
		 *)             false;;
	     esac }
}

_guix_complete_file ()
{
    # Let Readline complete file names.
    compopt -o default
    COMPREPLY=()
}

declare _guix_subcommands

_guix_complete ()
{
    local word_count=${#COMP_WORDS[*]}
    local word_at_point="${COMP_WORDS[$COMP_CWORD]}"

    if [ "$COMP_CWORD" -gt 1 ]
    then
	case "$word_at_point" in
	    -*)
		_guix_complete_option "$word_at_point"
		return
		;;
	esac
    fi

    case $COMP_CWORD in
	1)
	    if [ -z "$_guix_subcommands" ]
	    then
		# Cache the list of subcommands to speed things up.
		_guix_subcommands="$(guix --help | grep '^  ' | cut -c 2-)"
	    fi
	    COMPREPLY=($(compgen -W "$_guix_subcommands" -- "$word_at_point"))
	    ;;
	*)
	    if _guix_is_command "package"
	    then
		if _guix_is_dash_L
		then
		    _guix_complete_file
		elif _guix_is_removing
		then
		    _guix_complete_installed_package "$word_at_point"
		else
		    _guix_complete_available_package "$word_at_point"
		fi
	    elif _guix_is_command "system"
	    then
		_guix_complete_file # TODO: complete sub-commands
	    elif _guix_is_command "hash"
	    then
		 _guix_complete_file
	    elif _guix_is_command "import" # TODO: complete sub-commands
	    then
		 _guix_complete_file
	    else
		_guix_complete_available_package "$word_at_point"
	    fi
	    ;;
    esac
}

complete -F _guix_complete guix