summaryrefslogtreecommitdiffstats
path: root/contrib/bash_completion.sh
blob: f4979427bb87457374cfc3a4ef94d13df72a5f2f (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
#-*- mode: shell-script;-*-
#
# bash completion support for crmsh.
#
# Copyright (C) 2013 Kristoffer Gronlund <kgronlund@suse.com>
# Conceptually based on gitcompletion (http://gitweb.hawaga.org.uk/).
# Distributed under the GNU General Public License, version 2.0.
#
# To use these routines:
#
#    1) Copy this file to somewhere (e.g. ~/.crm-completion.sh).
#    2) Add the following line to your .bashrc/.zshrc:
#        source ~/.crm-completion.sh

shopt -s extglob

# The following function is based on code from:
#
#   bash_completion - programmable completion functions for bash 3.2+
#
#   Copyright © 2006-2008, Ian Macdonald <ian@caliban.org>
#             © 2009-2010, Bash Completion Maintainers
#                     <bash-completion-devel@lists.alioth.debian.org>
#
#   This program 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 2, or (at your option)
#   any later version.
#
#   This program 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 this program; if not, write to the Free Software Foundation,
#   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#   The latest version of this software can be obtained here:
#
#   http://bash-completion.alioth.debian.org/
#
#   RELEASE: 2.x

# This function can be used to access a tokenized list of words
# on the command line:
#
#       __git_reassemble_comp_words_by_ref '=:'
#       if test "${words_[cword_-1]}" = -w
#       then
#               ...
#       fi
#
# The argument should be a collection of characters from the list of
# word completion separators (COMP_WORDBREAKS) to treat as ordinary
# characters.
#
# This is roughly equivalent to going back in time and setting
# COMP_WORDBREAKS to exclude those characters.  The intent is to
# make option types like --date=<type> and <rev>:<path> easy to
# recognize by treating each shell word as a single token.
#
# It is best not to set COMP_WORDBREAKS directly because the value is
# shared with other completion scripts.  By the time the completion
# function gets called, COMP_WORDS has already been populated so local
# changes to COMP_WORDBREAKS have no effect.
#
# Output: words_, cword_, cur_.

__crm_reassemble_comp_words_by_ref()
{
        local exclude i j first
        # Which word separators to exclude?
        exclude="${1//[^$COMP_WORDBREAKS]}"
        cword_=$COMP_CWORD
        if [ -z "$exclude" ]; then
                words_=("${COMP_WORDS[@]}")
                return
        fi
        # List of word completion separators has shrunk;
        # re-assemble words to complete.
        for ((i=0, j=0; i < ${#COMP_WORDS[@]}; i++, j++)); do
                # Append each nonempty word consisting of just
                # word separator characters to the current word.
                first=t
                while
                        [ $i -gt 0 ] &&
                        [ -n "${COMP_WORDS[$i]}" ] &&
                        # word consists of excluded word separators
                        [ "${COMP_WORDS[$i]//[^$exclude]}" = "${COMP_WORDS[$i]}" ]
                do
                        # Attach to the previous token,
                        # unless the previous token is the command name.
                        if [ $j -ge 2 ] && [ -n "$first" ]; then
                                ((j--))
                        fi
                        first=
                        words_[$j]=${words_[j]}${COMP_WORDS[i]}
                        if [ $i = $COMP_CWORD ]; then
                                cword_=$j
                        fi
                        if (($i < ${#COMP_WORDS[@]} - 1)); then
                                ((i++))
                        else
                                # Done.
                                return
                        fi
                done
                words_[$j]=${words_[j]}${COMP_WORDS[i]}
                if [ $i = $COMP_CWORD ]; then
                        cword_=$j
                fi
        done
}

if ! type _get_comp_words_by_ref >/dev/null 2>&1; then
_get_comp_words_by_ref ()
{
        local exclude cur_ words_ cword_
        if [ "$1" = "-n" ]; then
                exclude=$2
                shift 2
        fi
        __crm_reassemble_comp_words_by_ref "$exclude"
        cur_=${words_[cword_]}
        while [ $# -gt 0 ]; do
                case "$1" in
                cur)
                        cur=$cur_
                        ;;
                prev)
                        prev=${words_[$cword_-1]}
                        ;;
                words)
                        words=("${words_[@]}")
                        ;;
                cword)
                        cword=$cword_
                        ;;
                esac
                shift
        done
}
fi

__crmcompadd ()
{
	local i=0
	for x in $1; do
		if [[ "$x" == "$3"* ]]; then
                    if [[ "$x" =~ .*(=|:)$ ]];then
                        if [[ "$x" =~ ^id=$ ]];then
                            :
                        else
			    COMPREPLY[i++]="$2$x"
                        fi
                    else
			COMPREPLY[i++]="$2$x$4"
                    fi
		fi
	done
}

# Generates completion reply, appending a space to possible completion words,
# if necessary.
# It accepts 1 to 4 arguments:
# 1: List of possible completion words.
# 2: A prefix to be added to each possible completion word (optional).
# 3: Generate possible completion matches for this word (optional).
# 4: A suffix to be appended to each possible completion word (optional).
__crmcomp ()
{
	local cur_="${3-$cur}"

	case "$cur_" in
	--*=)
		;;
	*)
		local c i=0 IFS=$' \t\n'
		for c in $1; do
			c="$c${4-}"
			if [[ $c == "$cur_"* ]]; then
				case $c in
				--*=*|*.) ;;
				*) c="$c " ;;
				esac
				COMPREPLY[i++]="${2-}$c"
			fi
		done
		;;
	esac
}

# Generates completion reply from newline-separated possible completion words
# by appending a space to all of them.
# It accepts 1 to 4 arguments:
# 1: List of possible completion words, separated by a single newline.
# 2: A prefix to be added to each possible completion word (optional).
# 3: Generate possible completion matches for this word (optional).
# 4: A suffix to be appended to each possible completion word instead of
#    the default space (optional).  If specified but empty, nothing is
#    appended.
__crmcomp_nl ()
{
	local IFS=$'\n'
	__crmcompadd "$1" "${2-}" "${3-$cur}" "${4- }"
}

__crm_compgen ()
{
	local cur_="$cur" cmd="${words[1]}"
	local pfx=""

	case "$cur_" in
	*:*)
		case "$COMP_WORDBREAKS" in
		*:*) : great ;;
		*)   pfx="${cur_%%:*}:" ;;
		esac
		cur_="${cur_##*:}"
		;;
	esac

    __crmcomp_nl "$(2>/dev/null crm --compgen "${COMP_POINT}" "${COMP_LINE}")" "$pfx" "$cur_"
}

_crm() {
    local cur words cword prev

	_get_comp_words_by_ref -n =: cur words cword prev

    for ((i=1; $i<=$cword; i++)); do
        if [[ ${words[i]} != -* ]]; then
            if [[ ${words[i-1]} != @(-f|--file|-H|--history|-D|--display|-X|-c|--cib) ]]; then
                arg="${words[i]}"
                argi=$i
                break
            fi
        fi
    done

    case $prev in
        -f|--file|-H|--history|-D|--display|-X|-c|--cib)
            # use default completion
            return
            ;;
    esac

    if [[ "$cur" == -* ]]; then
		__crmcomp '-w -h -d -F -R -f --file -H --history -D --display -X -c --cib'
        return 0
    fi

	__crm_compgen
} &&
complete -o bashdefault -o default -o nospace -F _crm crm || complete -o default -o nospace -F _crm crm