summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLudovic Courtès <ludo@gnu.org>2015-04-09 23:44:14 +0200
committerLudovic Courtès <ludo@gnu.org>2015-04-09 23:44:14 +0200
commitee3e157dec3512778502f20538c581f5e28d63fe (patch)
treef1089216e0920f4c603ca38a2417c9133eac38c1
parentc833ab556f811da6d1d9e467cb85b36e5d006cc7 (diff)
Add Bash completion file.
* etc/completion/bash/guix: New file. * Makefile.am (dist_bashcompletion_DATA): New variable. * configure.ac: Add --with-bash-completion-dir.
-rw-r--r--Makefile.am3
-rw-r--r--configure.ac7
-rw-r--r--etc/completion/bash/guix139
3 files changed, 149 insertions, 0 deletions
diff --git a/Makefile.am b/Makefile.am
index d54e281163a..51884b92211 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -240,6 +240,9 @@ tests/guix-gc.log: \
240# Public key used to sign substitutes from hydra.gnu.org. 240# Public key used to sign substitutes from hydra.gnu.org.
241dist_pkgdata_DATA = hydra.gnu.org.pub 241dist_pkgdata_DATA = hydra.gnu.org.pub
242 242
243# Bash completion file.
244dist_bashcompletion_DATA = etc/completion/bash/guix
245
243EXTRA_DIST = \ 246EXTRA_DIST = \
244 HACKING \ 247 HACKING \
245 ROADMAP \ 248 ROADMAP \
diff --git a/configure.ac b/configure.ac
index 6f261cdb630..2227c71c1b0 100644
--- a/configure.ac
+++ b/configure.ac
@@ -34,6 +34,13 @@ AC_ARG_WITH(store-dir,
34 [storedir="/gnu/store"]) 34 [storedir="/gnu/store"])
35AC_SUBST(storedir) 35AC_SUBST(storedir)
36 36
37AC_ARG_WITH([bash-completion-dir],
38 AC_HELP_STRING([--with-bash-completion-dir=DIR],
39 [name of the Bash completion directory]),
40 [bashcompletiondir="$withval"],
41 [bashcompletiondir='${sysconfdir}/bash_completion.d'])
42AC_SUBST([bashcompletiondir])
43
37dnl Better be verbose. 44dnl Better be verbose.
38AC_MSG_CHECKING([for the store directory]) 45AC_MSG_CHECKING([for the store directory])
39AC_MSG_RESULT([$storedir]) 46AC_MSG_RESULT([$storedir])
diff --git a/etc/completion/bash/guix b/etc/completion/bash/guix
new file mode 100644
index 00000000000..2a3fa1201e1
--- /dev/null
+++ b/etc/completion/bash/guix
@@ -0,0 +1,139 @@
1# GNU Guix --- Functional package management for GNU
2# Copyright © 2015 Ludovic Courtès <ludo@gnu.org>
3#
4# This file is part of GNU Guix.
5#
6# GNU Guix is free software; you can redistribute it and/or modify it
7# under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 3 of the License, or (at
9# your option) any later version.
10#
11# GNU Guix is distributed in the hope that it will be useful, but
12# WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
18
19# Bash completion for Guix commands.
20
21_guix_complete_available_package ()
22{
23 local prefix="$1"
24 local packages="$(${COMP_WORDS[0]} package -A "^$prefix" | cut -f1)"
25 COMPREPLY=($(compgen -W "$packages" -- "$prefix"))
26}
27
28_guix_complete_installed_package ()
29{
30 local prefix="$1"
31 local packages="$(${COMP_WORDS[0]} package -I "^$prefix" | cut -f1)"
32 COMPREPLY=($(compgen -W "$packages" -- "$prefix"))
33}
34
35_guix_complete_option ()
36{
37 local options="$(${COMP_WORDS[0]} ${COMP_WORDS[1]} --help \
38 | grep '^ -' \
39 | sed -e's/^.*--\([a-zA-Z0-9_-]\+\)\(=\?\).*/--\1\2/g' )"
40 compopt -o nospace
41 COMPREPLY=($(compgen -W "$options" -- "${COMP_WORDS[$word_count - 1]}"))
42}
43
44_guix_is_command ()
45{
46 local word
47 local result="false"
48 for word in ${COMP_WORDS[*]}
49 do
50 if [ "$word" = "$1" ]
51 then
52 result=true
53 break
54 fi
55 done
56 $result
57}
58
59_guix_is_removing ()
60{
61 local word
62 local result="false"
63 for word in ${COMP_WORDS[*]}
64 do
65 case "$word" in
66 --remove|--remove=*|-r)
67 result=true
68 break
69 ;;
70 esac
71 done
72 $result
73}
74
75_guix_is_dash_L ()
76{
77 [ "${COMP_WORDS[$COMP_CWORD - 1]}" = "-L" ] \
78 || { case "${COMP_WORDS[$COMP_CWORD]}" in
79 --load-path=*) true;;
80 *) false;;
81 esac }
82}
83
84_guix_complete_file ()
85{
86 # Let Readline complete file names.
87 compopt -o default
88 COMPREPLY=()
89}
90
91_guix_complete ()
92{
93 local word_count=${#COMP_WORDS[*]}
94 local word_at_point="${COMP_WORDS[$COMP_CWORD]}"
95
96 if [ "$COMP_CWORD" -gt 1 ]
97 then
98 case "$word_at_point" in
99 -*)
100 _guix_complete_option "$word_at_point"
101 return
102 ;;
103 esac
104 fi
105
106 case $COMP_CWORD in
107 1)
108 local subcommands="$(guix --help | grep '^ ' | cut -c 2-)"
109 COMPREPLY=($(compgen -W "$subcommands" -- "$word_at_point"))
110 ;;
111 *)
112 if _guix_is_command "package"
113 then
114 if _guix_is_dash_L
115 then
116 _guix_complete_file
117 elif _guix_is_removing
118 then
119 _guix_complete_installed_package "$word_at_point"
120 else
121 _guix_complete_available_package "$word_at_point"
122 fi
123 elif _guix_is_command "system"
124 then
125 _guix_complete_file # TODO: complete sub-commands
126 elif _guix_is_command "hash"
127 then
128 _guix_complete_file
129 elif _guix_is_command "import" # TODO: complete sub-commands
130 then
131 _guix_complete_file
132 else
133 _guix_complete_available_package "$word_at_point"
134 fi
135 ;;
136 esac
137}
138
139complete -F _guix_complete guix