summaryrefslogtreecommitdiffstats
path: root/bash_completion
blob: d0c1edec8faeef3a23232345d7294ac9687015a0 (plain) (blame)
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
#-*- mode: shell-script;-*-

# Programmed completion for bash to use quilt
# Copyright 2003-2004 Martin Quinson <martin.quinson@tuxfamily.org>

# This file is part of the distribution of quilt, and is distributed under
# the same licence than quilt itself

if type quilt &> /dev/null ; then

if ! type _expand &> /dev/null ; then
    # This function expands tildes in pathnames
    #
    _expand()
    {
	[ "$cur" != "${cur%\\}" ] && cur="$cur"'\'

	# expand ~username type directory specifications
	if [[ "$cur" == \~*/* ]]; then
	    eval cur=$cur
	elif [[ "$cur" == \~* ]]; then
	    cur=${cur#\~}
	    COMPREPLY=( $( compgen -P '~' -u $cur ) )
	    return ${#COMPREPLY[@]}
	fi
    }
fi

if ! type _filedir &> /dev/null ; then
    # This function performs file and directory completion. It's better than
    # simply using 'compgen -f', because it honours spaces in filenames
    #
    _filedir()
    {
	local IFS=$'\t\n'

	_expand || return 0

	if [ "$1" = -d ]; then
	    COMPREPLY=( ${COMPREPLY[@]} $( compgen -d -- $cur ) )
	    return 0
	fi
	COMPREPLY=( ${COMPREPLY[@]} $( eval compgen -f -- \"$cur\" ) )
    }
fi

if ! type _longopt &> /dev/null ; then
    _longopt()
    {
	local cur opt

	cur=${COMP_WORDS[COMP_CWORD]}

	if [[ "$cur" == "--*=*" ]]; then
	    opt=${cur%%=*}
	    # cut backlash that gets inserted before '=' sign
	    opt=${opt%\\*}
	    cur=${cur#*=}
	    _filedir
	    COMPREPLY=( $( compgen -P "$opt=" -W '${COMPREPLY[@]}' -- $cur))
	    return 0
	fi

	if [[ "$cur" == "-*" ]]; then
	    COMPREPLY=( $( $1 --help 2>&1 | sed  -e '/--/!d' \
			    -e 's/.*\(--[-A-Za-z0-9]\+=\?\).*/\1/' | \
			   grep "^$cur" | sort -u ) )
	elif [[ "$1" == "@(mk|rm)dir" ]]; then
	    _filedir -d
	else
	    _filedir
	fi
    }
fi

_quilt_completion()
{
    local cur prev cmds command_matches

    COMPREPLY=()
    cur=${COMP_WORDS[COMP_CWORD]}
    prev=${COMP_WORDS[COMP_CWORD-1]}

    # quilt sub commands 
    cmds='add applied delete diff edit files fold fork graph grep  \
          import new next patches pop previous push refresh remove \
	  series setup snapshot top unapplied'

    # if no command were given, complete on commands
    if [[ $COMP_CWORD -eq 1 ]] ; then
	COMPREPLY=( $( compgen -W "$cmds -h" -- $cur ) )
	return 0
    fi
	
    # if we're completing for 'quilt -h', then just 
    # complete on any valid command
    if [ ${COMP_WORDS[1]} == -h ] ; then
	    COMPREPLY=( $( compgen -W "$cmds" -- $cur ) )
            return 0
    fi
    
    # Accept a partial command if it's unique, because quilt will accept it.
    command_matches=( $(compgen -W "$cmds" -- ${COMP_WORDS[1]}) )
    if [ ${#command_matches[@]} -ne 1 ] ; then
	return 0
    fi

    # Complete depending on options
    case ${command_matches[0]} in
	add)
	   case $prev in
	     -p)
 	        COMPREPLY=( $( compgen -W "$(quilt applied)" -- $cur ) )
		;;
	     *)
	        _filedir 
	        COMPREPLY=( ${COMPREPLY[@]:-} $( compgen -W "-p -h" -- $cur ) )
	        ;;
	   esac
	   ;;
	applied) 
	   COMPREPLY=( $( compgen -W "-n -h $(quilt applied)" -- $cur ) )
	   ;;
	delete) 
	   COMPREPLY=( $( compgen -W "-h $(quilt series)" -- $cur ) )
	   ;;
	diff) 
	   case $prev in
	     -p)
 	        COMPREPLY=( $( compgen -W "0 1" -- $cur ) )
		;;
	     -P|-c)
	     	COMPREPLY=( $( compgen -W "$(quilt applied)" -- $cur ) )
		;;
	     --diff|--snapshot)
	        COMREPLY=(  )
		;;
	     *)
	        _filedir 
	        COMPREPLY=( ${COMPREPLY[@]:-} $( compgen -W "-p -P -c -R -z -h --snapshot --diff --no-timestamps" -- $cur ) )
	        ;;
	   esac
	   ;;
	edit)
	   _filedir
	   COMPREPLY=( ${COMPREPLY[@]:-} $( compgen -W "-h" -- $cur ) )
	   ;;
	files)
	   COMPREPLY=( $( compgen -W "-v -h $(quilt series)" -- $cur ) )
	   ;;
	fold)
	   case $prev in
	     -p)
	        COMPREPLY=( $( compgen -W "0 1" -- $cur ) )
		;;
	     *)
	        COMPREPLY=( ${COMPREPLY[@]:-} $( compgen -W "-p" -- $cur ) )
	        ;;
	   esac
	   ;;
	fork)
	  COMPREPLY=( )
	  ;;
	graph)
	   COMPREPLY=( $( compgen -W "-h --all --reduce --lines --edge-labels $(quilt applied)" -- $cur ) )
	   ;;
	grep)
	   _longopt grep
	   COMPREPLY=( ${COMPREPLY[@]:-} $( compgen -W "-h" -- $cur ) )
	   ;;
	import)
	   case $prev in
	     -p)
 	        COMPREPLY=( $( compgen -W "0 1 2 3 4 5 6 7 8 9 10" -- $cur ) )
		;;
	     -n)
		;;
	     *)
	        _filedir 
	        COMPREPLY=( ${COMPREPLY[@]:-} $( compgen -W "-p -n -f -h" -- $cur ) )
	        ;;
	   esac
	   ;;
	new)
	   ;;
	next|previous)
	   COMPREPLY=( $( compgen -W "-n $(quilt series)" -- $cur ) )
	   ;;
	patches)
	   _filedir 
	   COMPREPLY=( ${COMPREPLY[@]:-} $( compgen -W "-v -n -h" -- $cur ) )
	   ;;
	pop)
	   COMPREPLY=( $( compgen -W "-a -f -R -q -v -h $(quilt applied)" -- $cur ) )
	   ;;
	push)
	   COMPREPLY=( $( compgen -W "-a -f -R -q -v -h --leave-rejects --interactive $(quilt unapplied)" -- $cur ) )
	   ;;
	refresh)
	   case $prev in
	     -p)
 	        COMPREPLY=( $( compgen -W "0 1" -- $cur ) )
		;;
	     *)
	     	COMPREPLY=( $( compgen -W "-p -f -h $(quilt applied)  --no-timestamps" -- $cur ) )
		;;
	   esac
	   ;;
	remove)
	   case $prev in
	     -p)
 	        COMPREPLY=( $( compgen -W "$(quilt applied)" -- $cur ) )
		;;
	     *)
	        _filedir 
	        COMPREPLY=( ${COMPREPLY[@]:-} $( compgen -W "-p -h" -- $cur ) )
		;;
	   esac
	   ;;
	 series)
	   COMPREPLY=( $( compgen -W "-n -v -h" -- $cur ) )
	   ;;
	 setup)
	   case $prev in
	     -d)
	     	_filedir -d
		;;
	     *)
	        _filedir 
	        COMPREPLY=( ${COMPREPLY[@]:-} $( compgen -W "-d -v -h" -- $cur ) )
		;;
	   esac
	   ;;
	 snapshot)
	   COMPREPLY=( $( compgen -W "-d -h" -- $cur ) )
	   ;;
	 top)
	   ;;
	 unapplied)
	   COMPREPLY=( $( compgen -W "-n -h $(quilt series)" -- $cur ) )
	   ;;
	 upgrade)
	   ;;
    esac
    return 0
}

[ ${BASH_VERSINFO[0]} > 2 -o \
  ${BASH_VERSINFO[0]} = 2 -a ${BASH_VERSINFO[1]} '>' 04 ] \
&& _quilt_complete_opt="-o filenames"
complete -F _quilt_completion $_quilt_complete_opt quilt
unset -v _quilt_complete_opt

fi