aboutsummaryrefslogtreecommitdiff
blob: 7de410195ac3e506f12f44ca14dc31b975733c83 (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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
<?xml version="1.0"?>
<guide self="tasks-reference/completion/">
<chapter>
<title>Completion Files</title>
<body>

<p>
Since v2.05a, <c>bash</c> has offered intelligent programmable completion.  Writing
such completions for your own programs/things you maintain is relatively easy
provided you know bash already. See `bash-completion.eclass Reference`_ for how
to install completion files.
</p>

<section>
<title>Completion-Related Internal Bash Variables</title>
<body>

<p>
Most of these variables lose their special properties when unset, even if they
are subsequently reset.
</p>

<table>
  <tr>
    <th>
      Variable
    </th>
    <th>
      Purpose
    </th>
  </tr>
  <tr>
    <ti>
      <c>COMP_CWORD</c>
    </ti>
    <ti>
      An index into <c>${COMP_WORDS}</c> of the word containing the
      current cursor position.
    </ti>
  </tr>
  <tr>
    <ti>
      <c>COMP_LINE</c>
    </ti>
    <ti>
      The current command line.
    </ti>
  </tr>
  <tr>
    <ti>
      <c>COMP_POINT</c>
    </ti>
    <ti>
      The index of the current cursor position relative to the
      beginning of the current command.  If the current cursor
      position is at the end of the current command, the value
      of this variable is equal to <c>${#COMP_LINE}</c>.
    </ti>
  </tr>
  <tr>
    <ti>
      <c>COMP_WORDBREAKS</c>
    </ti>
    <ti>
      The set of characters that the Readline library treats as
      word separators when performing word completion.
    </ti>
  </tr>
  <tr>
    <ti>
      <c>COMP_WORDS</c>
    </ti>
    <ti>
      An array variable consisting of the individual words in
      the current command line, <c>${COMP_LINE}</c>.
    </ti>
  </tr>
  <tr>
    <ti>
      <c>COMPREPLY</c>
    </ti>
    <ti>
      An array variable from which bash reads the possible
      completions generated by a completion function.
    </ti>
  </tr>
</table>

</body>
</section>

<section>
<title>Completion-Related Bash Builtins</title>
<body>

<p>
See `bash-1`_ for a full description of these builtins and their options.
</p>

<table>
  <tr>
    <th>
      Builtin
    </th>
    <th>
      Usage
    </th>
  </tr>
  <tr>
    <ti>
      <c>compgen</c>
    </ti>
    <ti>
      <c>compgen [-abcdefgjksuv] [-o option] [-A action] [-G globpat]
      [-W wordlist] [-P prefix] [-S suffix] [-X filterpat]
      [-F function] [-C command] [word]</c>
      Display the possible completions depending on the options.
      Intended to be used from within a shell function generating
      possible completions. If the optional WORD argument is supplied,
      matches against WORD are generated.
    </ti>
  </tr>
  <tr>
    <ti>
      <c>complete</c>
    </ti>
    <ti>
      <c>complete [-abcdefgjksuv] [-pr] [-o option] [-A action]
      [-G globpat] [-W wordlist] [-P prefix] [-S suffix]
      [-X filterpat] [-F function] [-C command] [name ...]</c>
      For each NAME, specify how arguments are to be completed.
      If the -p option is supplied, or if no options are supplied,
      existing completion specifications are printed in a way that
      allows them to be reused as input.  The -r option removes a
      completion specification for each NAME, or, if no NAMEs are
      supplied, all completion specifications.
    </ti>
  </tr>
</table>

<p>
For extremely simple cases, a simple <c>complete</c> statement is all that is
needed.  For example, minimal <c>cd</c> completion (minimal as in no support for
<c>${CDPATH}</c>) would be as simple as:
</p>

<codesample lang="ebuild">
complete -o nospace -d cd
</codesample>

</body>
</section>

<section>
<title>Anatomy of a Completion Function</title>
<body>

<p>
Nearly all completion functions will start out the same way. For these cases,
the following can be used as a template for creating new completion functions:
</p>

<codesample lang="ebuild" numbering="lines">
_foo() {
    local cur prev opts
    COMPREPLY=()
    cur="${COMP_WORDS[COMP_CWORD]}"
    prev="${COMP_WORDS[COMP_CWORD-1]}"
    opts=""

    if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] ; then
        COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
    return 0
    fi

    case "${prev}" in
      # ...
    esac
}
complete -F _foo foo
</codesample>

<table>
  <tr>
    <th>
      Line
    </th>
    <th>
      Explanation
    </th>
  </tr>
  <tr>
    <ti>
      1
    </ti>
    <ti>
      The convention for completion function names is usually _NAME, where NAME
      is the name of the application/function you are writing the completion
      function for.  If NAME contains a '-', it should be replaced with a '_'.
      Hence bash-completion-config would be _bash_completion_config().  Failing
      to do so can cause weird bugs if bash is invoked in POSIX-mode while a
      function name containing a '-' is in the environment (POSIX sh does not
      allow a '-' in function names).
    </ti>
  </tr>
  <tr>
    <ti>
      3
    </ti>
    <ti>
      Resets the <c>${COMPREPLY}</c> array, as it may be set from a previous
      invocation.
    </ti>
  </tr>
  <tr>
    <ti>
      4
    </ti>
    <ti>
      Sets the local variable, <c>${cur}</c> to the current word of the command line.
      If the current command line, <c>${COMP_LINE}</c> is say 'foo --fil', <c>${cur}</c>
      would be equal to '--fil'.  If <c>${COMP_LINE}</c> is equal to 'foo --file '
      (note the space at the end), <c>${cur}</c> is null.
    </ti>
  </tr>
  <tr>
    <ti>
      5
    </ti>
    <ti>
      Sets the local variable, <c>${prev}</c> to the previous word of the command
      line.  <c>${prev}</c> is the word before <c>${cur}</c>.
    </ti>
  </tr>
  <tr>
    <ti>
      6
    </ti>
    <ti>
      Sets the local variable, <c>${opts}</c>.  In a real completion function, this
      variable would be set to all the options that <c>foo</c> recognizes.
    </ti>
  </tr>
  <tr>
    <ti>
      8
    </ti>
    <ti>
      Tests whether or not the current word is equivalent to -* (an option) or if
      we're completing on the first word (ie. <c>${COMP_CWORD}</c> == 1).
    </ti>
  </tr>
  <tr>
    <ti>
      9
    </ti>
    <ti>
      If the test returns true, show the available options, <c>${opts}</c>. The -W
      option to <c>compgen</c> tells bash to complete on the word list (string or
      something that evaluates to a string).  In the majority of cases, you'll
      pass <c>'-- ${cur}'</c> to <c>compgen</c> telling it to only return those
      completions that match <c>${cur}</c>.
    </ti>
  </tr>
  <tr>
    <ti>
      13
    </ti>
    <ti>
      Most of the time, you'll want to perform a certain action if <c>${prev}</c> is
      equal to a certain option.  For example, if <c>foo</c> has a --file option
      (and -f for short) that takes any kind file, you could do:
      <codesample lang="ebuild">
case "${prev}" in
    -f|--file)
        COMPREPLY=( $(compgen -f ? ${cur}) )
        ;;
esac</codesample>
    </ti>
  </tr>
  <tr>
    <ti>
      17
    </ti>
    <ti>
      Tells bash to use the <c>_foo</c> function to generate any completions for
      the <c>foo</c> application/function.
    </ti>
  </tr>
</table>

</body>
</section>

<section>
<title>Real-World Example</title>
<body>

<p>
For this document, I will take you through a real-world example and write a
real completion function for <c>revdep-rebuild</c> (might even be available in
<c>gentoo-bashcomp</c> by the time you read this :]).
</p>

<codesample lang="ebuild" numbering="lines">
_revdep_rebuild() {
	local cur prev opts
	COMPREPLY=()
	cur="${COMP_WORDS[COMP_CWORD]}"
	prev="${COMP_WORDS[COMP_CWORD-1]}"
	opts="-X --package-names --soname --soname-regexp -q --quiet"

	if [[ ${cur} == -* || ${COMP_CWORD} -eq 1 ]] || \
	   [[ ${prev} == @(-q|--quiet) ]] ; then
		COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
		return 0
	fi
 
	case "${prev}" in
		-X|--package-names)
			_pkgname -I ${cur}
			;;
		--soname)
			local sonames=$(for x in /lib/*.so?(.)* /usr/lib*/*.so\?(.)* ; do \
						echo ${x##*/} ; \
						done)
			COMPREPLY=( $(compgen -W "${sonames}" -- ${cur}) )
			;;
		--soname-regexp)
			COMPREPLY=()
			;;
		*)
			if [[ ${COMP_LINE} == *" "@(-X|--package-names)* ]] ; then
				_pkgname -I ${cur}
				COMPREPLY=(${COMPREPLY[@]} $(compgen -W "${opts}"))
			else
				COMPREPLY=($(compgen -W "${opts} -- ${cur}"))
			fi
		;;
	esac
}
complete -F _revdep_rebuild revdep-rebuild
</codesample>

<p>
Lines 1-12 are pretty much the same as in the previous section.
</p>

<table>
  <tr>
    <th>
      Line
    </th>
    <th>
      Explanation
    </th>
  </tr>
  <tr>
    <ti>
      15
    </ti>
    <ti>
      If <c>${prev}</c> is equal to -X|--package-names, call _pkgname (a function
      defined by <c>gentoo-bashcomp</c> that completes on package names - it sets
      <c>${COMPREPLY}</c>, so we don't worry about that here).
    </ti>
  </tr>
  <tr>
    <ti>
      18
    </ti>
    <ti>
      If <c>${prev}</c> is equal to --soname, generate a list of all shared libs
      in /lib and /usr/lib*.  Pass that list to <c>compgen</c> to generate a list
      of possible completions that match <c>${cur}</c>.
    </ti>
  </tr>
  <tr>
    <ti>
      24
    </ti>
    <ti>
      Obviously we cannot complete on any regexp's so if <c>${prev}</c> is equal
      to --soname-regexp, do nothing.
    </ti>
  </tr>
  <tr>
    <ti>
      27
    </ti>
    <ti>
      For anything else (any options not specified in the case statement above
      OR any argument to one of the options specified in the case statement)
      perform the tests.  Since --package-names can take multiple package
      names, we want to continue to complete on package names until another
      recognized option is encountered (ie. is <c>${prev}</c>).
    </ti>
  </tr>
  <tr>
    <ti>
      30
    </ti>
    <ti>
      Since _pkgname sets <c>${COMPREPLY}</c> and we want to add to that list,
      we have to use the COMPREPLY=(${COMPREPLY[@] ... ) construct.
    </ti>
  </tr>
  <tr>
    <ti>
      37
    </ti>
    <ti>
      Tell bash to use _revdep_rebuild to generate all possible completions
      for revdep-rebuild.
    </ti>
  </tr>
</table>

</body>
</section>

</body>
</chapter>
</guide>