Lines 1-29
Link Here
|
1 |
# Copyright 1999-2005 Gentoo Foundation |
1 |
# Completion for eselect [<global options>] <module> <action> [<options>] |
|
|
2 |
# Author: Wilke Schwiedop <w.schwiedop@web.de> |
2 |
# Distributed under the terms of the GNU General Public License v2 |
3 |
# Distributed under the terms of the GNU General Public License v2 |
3 |
# $Id$ |
|
|
4 |
|
5 |
# bash command-line completion for eselect |
6 |
# Author: Aaron Walker <ka0ttic@gentoo.org> |
7 |
|
4 |
|
8 |
_eselect() { |
5 |
_eselect() { |
9 |
local cur prev sedcmd possibles |
6 |
local curr=${COMP_WORDS[COMP_CWORD]} possibles="" module_index=1 |
10 |
COMPREPLY=() |
7 |
|
11 |
cur="${COMP_WORDS[COMP_CWORD]}" |
8 |
# Unset default completion |
12 |
prev="${COMP_WORDS[COMP_CWORD-1]}" |
9 |
COMPREPLY=() |
13 |
sedcmd='sed -n -e s/^[[:space:]]\+.*1m\([^[:space:]][[:alnum:]-]\+\).*[[:space:]]\+.*$/\1/p' |
10 |
|
14 |
|
11 |
# Unfortunatly we can't source eselect to get to the variables directly, so this is a bit more complicated |
15 |
if [[ ${COMP_CWORD} -eq 1 ]] || [[ -n "${prev}" && ${prev} == -* ]] |
12 |
local ESELECT_DATA_PATH ESELECT_MODULES_PATH |
16 |
then |
|
|
17 |
possibles="$(eselect help | ${sedcmd})" |
18 |
elif eselect ${prev} usage &>/dev/null ; then |
19 |
possibles=$(eselect ${prev} usage | ${sedcmd}) |
20 |
fi |
21 |
|
13 |
|
22 |
[[ -n "${possibles}" ]] && \ |
14 |
# Obtain ESELECT_DATA_PATH and ESELECT_MODULES_PATH . See eselect for more info |
23 |
COMPREPLY=( $(compgen -W "${possibles}" -- ${cur}) ) |
15 |
ESELECT_DATA_PATH=$(sed -n -e 's/^ESELECT_DATA_PATH="\(.\+\)"$/\1/p' $(which eselect)) |
|
|
16 |
ESELECT_MODULES_PATH=( "${HOME}/.eselect/modules" "${ESELECT_DATA_PATH}/modules" ) |
17 |
|
18 |
# Hard-coded Actions. |
19 |
# Note: list-modules is hardcoded aswell, but not used in the modules. list-modules is added only when needed. |
20 |
ESELECT_KNOWN_ACTIONS="help usage version" |
21 |
# Hard-coded Options |
22 |
# TODO These could be read from ESELECT_KNOWN_OPTIONS |
23 |
ESELECT_KNOWN_OPTIONS="--no-colour" |
24 |
|
25 |
# If any word in ${COMP_WORDS[@]} is a default-action, we don't need further completion |
26 |
for i in ${COMP_WORDS[@]} ; do |
27 |
[[ $i == @(${ESELECT_KNOWN_ACTIONS//[[:space:]]/"|"}|list-modules) ]] && return 0 |
28 |
done |
29 |
|
30 |
# Skip all global-options |
31 |
while [[ ${COMP_WORDS[module_index]} == -* ]] ; do |
32 |
((++module_index)) |
33 |
done |
34 |
|
35 |
# If this is true we're looking for a module name or an eselect-option. In short: "eselect <wearehere>" |
36 |
# Since we're skipping the global options, this may be < 0 if we have nothing but options in COMP_WORDS. |
37 |
if [[ $((COMP_CWORD-module_index)) -le 0 ]] ; then |
38 |
local path file |
39 |
for path in "${ESELECT_MODULES_PATH[@]}" ; do |
40 |
[[ -d ${path} ]] || continue |
41 |
for file in ${path}/*.eselect ; do |
42 |
[[ -f ${file} ]] || continue |
43 |
file=${file##*/} # strip path |
44 |
file=${file%%.eselect} # strip extension |
45 |
possibles+=" ${file}" |
46 |
done |
47 |
done |
48 |
|
49 |
possibles+=" list-modules ${ESELECT_KNOWN_ACTIONS}" # Include default actions and list-modules |
50 |
possibles+=" ${ESELECT_KNOWN_OPTIONS}" # Include default options |
51 |
|
52 |
# We already have a module, so we're looking for an action or even and option |
53 |
# In short: "eselect somemodule <wearehere> <orevenhere>" |
54 |
else |
55 |
local modname=${COMP_WORDS[module_index]} modpath="" modfile="" |
56 |
for modpath in "${ESELECT_MODULES_PATH[@]}" ; do |
57 |
modfile="${modpath}/${modname}.eselect" |
58 |
[[ -f ${modfile} ]] && break |
59 |
done |
60 |
[[ -f ${modfile} ]] || return 1 # 404 File not found, so theres no way we get the info we need |
61 |
|
62 |
|
63 |
# We're looking for an action; In short: "eselect somemodule <wearehere>" |
64 |
if [[ $((COMP_CWORD-module_index)) -eq 1 ]] ; then |
65 |
possibles+=" ${ESELECT_KNOWN_ACTIONS}" # Include default actions |
66 |
|
67 |
# module-actions are simple bash functions in the module-file. |
68 |
# They follow the pattern "do_${action}()" and "do_${action} ()" respectively |
69 |
possibles+=" " |
70 |
possibles+=$(sed -n -e 's/^do_\([^ _\(]\+\).*/\1/p' ${modfile}) |
71 |
|
72 |
# We're looking for an option. |
73 |
# In short: "eselect somemodule someaction <wearehere>" |
74 |
else |
75 |
local action=${COMP_WORDS[module_index+1]} |
76 |
|
77 |
possibles+=" " |
78 |
possibles+=$( |
79 |
source ${modfile} &> /dev/null |
80 |
# functions describing options follow the pattern "describe_${action}_options()" |
81 |
if [[ $(type -t "describe_${action}_options" ) == "function" ]] ; then |
82 |
# we're only interested in --options-like-this |
83 |
# TODO The regexp could be improved to match only single "-" between letters |
84 |
echo $(describe_${action}_options | sed -n -e 's/^.*--\([[:alpha:]-]\+\).*/--\1/p') |
85 |
fi |
86 |
) |
87 |
fi |
88 |
fi |
89 |
|
90 |
# If we already completed something, remove already used words from completion. |
91 |
if [[ ${COMP_CWORD} -ge 2 ]] ; then |
92 |
# Our delete candidates are ${COMP_WORDS[@] without the first ("eselect") and last word ("$curr") |
93 |
local delete_candidates="${COMP_WORDS[@]:1:${COMP_CWORD}-1}" |
94 |
# Convert delete candidates to regexp |
95 |
delete_candidates=${delete_candidates//[[:space:]]/"|"} |
96 |
# Delete candidates begone! |
97 |
possibles=${possibles//@($delete_candidates)/""} |
98 |
# When removing a word, the delimiting spaces remain. Remove them aswell |
99 |
possibles=${possibles//" "/" "} |
100 |
fi |
24 |
|
101 |
|
25 |
return 0 |
102 |
COMPREPLY=( $(compgen -W "${possibles}" -- ${curr}) ) |
|
|
103 |
|
104 |
return 0 |
26 |
} |
105 |
} |
|
|
106 |
|
27 |
complete -F _eselect eselect |
107 |
complete -F _eselect eselect |
28 |
|
108 |
|
29 |
# vim: set ft=sh tw=80 sw=4 et : |
|
|