aboutsummaryrefslogtreecommitdiff
path: root/etc/completion/bash/guix
blob: a9386e7794de81a71595d48d60ac28c17340170e (about) (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
# GNU Guix --- Functional package management for GNU
# Copyright © 2015, 2016, 2017, 2018, 2019, 2020 Ludovic Courtès <ludo@gnu.org>
# Copyright © 2021 Tobias Geerinck-Rice <me@tobias.gr>
#
# 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
declare _guix_commands

_guix_complete_command ()
{
    local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
    if [ -z "$_guix_commands" ]
    then
	# Cache the list of commands to speed things up.
	_guix_commands="$(guix --help 2> /dev/null  \
                                     | grep '^    ' \
				     | sed '-es/^ *\([a-z-]\+\).*$/\1/g')"
    fi
    COMPREPLY=($(compgen -W "$_guix_commands" -- "$word_at_point"))
}

_guix_complete_subcommand ()
{
    local command="${COMP_WORDS[1]}"
    local subcommands="$(${COMP_WORDS[0]} $command --help 2> /dev/null \
                         | grep '^   [a-z]' \
                         | sed -e's/^ \+\([a-z-]\+\).*$/\1/g')"
    COMPREPLY=($(compgen -W "$subcommands" -- "${COMP_WORDS[$COMP_CWORD]}"))
}

_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 2> /dev/null \
                                    | 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" 2> /dev/null \
                      | cut -f1)"
    COMPREPLY=($(compgen -W "$packages" -- "$prefix"))
}

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

_guix_is_option ()
{
    case "$1" in
	-*)
	    true
	    ;;
	*)
	    false
	    ;;
    esac
}

_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_f ()
{
    [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-f" ] \
	|| { case "${COMP_WORDS[$COMP_CWORD]}" in
		 --file=*|--install-from-file=*) true;;
		 *)                              false;;
	     esac }
}

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

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

_guix_is_dash_m ()
{
    [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-m" ] \
	|| { case "${COMP_WORDS[$COMP_CWORD]}" in
		 --manifest=*) true;;
		 *)            false;;
	     esac }
}

_guix_is_dash_C ()
{
    [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-C" ] \
	|| { case "${COMP_WORDS[$COMP_CWORD]}" in
		 --channels=*)  true;;
		 *)             false;;
	     esac }
}

_guix_is_dash_p ()
{
    [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-p" ] \
	|| { case "${COMP_WORDS[$COMP_CWORD]}" in
		 --profile=*)   true;;
		 *)             false;;
	     esac }
}

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

_guix_complete_pid ()
{
    local pids="$(cd /proc; echo [0-9]*)"
    COMPREPLY=($(compgen -W "$pids" -- "$1"))
}

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

    # Find the innermost command at point, e.g. "build" in the case of
    # "guix time-machine OPTIONS -- build<Tab>" -- but "time-machine" if
    # point is moved before "build".
    local command_index=0
    local command
    local word_index=0
    local word
    local expect_command="true"
    while [[ $((++word_index)) -le COMP_CWORD ]]
    do
	word="${COMP_WORDS[$word_index]}"
	if $expect_command
	then
	    command_index=$word_index
	    command="$word"
	    expect_command="false"
	    continue
	fi
	if [[ "$word" = "--" ]]
	then
	    case "$command" in
		environment)
		    break
		    ;;
		time-machine)
		    expect_command="true"
		    ;;
	    esac
	fi
    done

    case $COMP_CWORD in
	$command_index)
	    _guix_complete_command
	    ;;
	*)
	    if [[ "$command" = "package" ]]
	    then
		if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p || _guix_is_dash_f
		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 [[ "$command" = "install" ]]
	    then
                if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p
                then
                    _guix_complete_file
		else
		    _guix_complete_available_package "$word_at_point"
		fi
	    elif [[ "$command" = "remove" ]]
	    then
                if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p
                then
                    _guix_complete_file
		else
		    _guix_complete_installed_package "$word_at_point"
		fi
	    elif [[ "$command" = "upgrade" ]]
	    then
                if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p
                then
                    _guix_complete_file
		else
		    _guix_complete_installed_package "$word_at_point"
		fi
            elif [[ "$command" = "build" ]]
            then
                if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_f
                then
                    _guix_complete_file
		else
		    _guix_complete_available_package "$word_at_point"
                fi
	    elif [[ "$command" = "environment" ]]
	    then
                if _guix_is_dash_L || _guix_is_dash_m || _guix_is_dash_p || _guix_is_dash_l
                then
                    _guix_complete_file
		elif _guix_is_option "$word_at_point"
		then
		    _guix_complete_option "$command_index" "$word_at_point"
		else
		    _guix_complete_available_package "$word_at_point"
		fi
	    elif [[ "$command" = "download" ]]
	    then
		 _guix_complete_file
	    elif [[ "$command" = "system" ]]
	    then
		case $COMP_CWORD in
		    2) _guix_complete_subcommand;;
		    *) _guix_complete_file;; # TODO: restrict to *.scm
		esac
            elif [[ "$command" = "pull" ]]
            then
                if _guix_is_dash_C || _guix_is_dash_p
                then
                    _guix_complete_file
                fi
            elif [[ "$command" = "time-machine" ]]
            then
                if _guix_is_dash_C
                then
                    _guix_complete_file
		else
		    _guix_complete_option "$command_index" "$word_at_point"
                fi
	    elif [[ "$command" = "container" ]]
	    then
		case $COMP_CWORD in
		    2) _guix_complete_subcommand;;
		    3) _guix_complete_pid "$word_at_point";;
		    *) _guix_complete_file;;
		esac
	    elif [[ "$command" = "import" ]]
	    then
		_guix_complete_subcommand
	    elif [[ "$command" = "hash" || "$command" = "gc" ]]
	    then
		_guix_complete_file
            elif [[ "$command" = "weather" ]]
            then
                if _guix_is_dash_m
                then
                    _guix_complete_file
                fi
	    else
		_guix_complete_available_package "$word_at_point"
	    fi
	    ;;
    esac

    if [[ -z "$COMPREPLY" && COMP_CWORD -gt command_index ]] &&
        _guix_is_option "$word_at_point"
    then
	_guix_complete_option "$command_index" "$word_at_point"
    fi
}

complete -F _guix_complete guix