@@ -260,135 +260,6 @@ _upvars()
260
260
done
261
261
}
262
262
263
- # Filter the array elements with the specified condition.
264
- # @param $1 Array name (that is not "value" or other internal variable names)
265
- # @param $2 When none of the options -EFG are specified, this is used as the
266
- # command that tests the array element. The command is supposed to exit with
267
- # status 0 when the element should be preserved, and 1 when the element
268
- # should be removed. The other exit status will cancel the array filtering.
269
- # If this is an existing function name, the function is called with the value
270
- # of the array element. Otherwise, this shall be the shell command that
271
- # tests the array-element value stored in the shell variable "value".
272
- #
273
- # Options:
274
- # -E $2 is interpreted as a POSIX extended regular expression.
275
- # The default anchoring is `-m` (see below).
276
- # -F $2 is interpreted as a fixed string. The default anchoring
277
- # is `-m` (see below).
278
- # -G $2 is interpreted as a glob pattern. The default anchoring
279
- # is `-x` (see below).
280
- #
281
- # -p Combined with -EFG, it performs the prefix matching.
282
- # -s Combined with -EFG, it performs the suffix matching.
283
- # -m Combined with -EFG, it performs the middle matching.
284
- # -x Combined with -EFG, it performs the exact matching.
285
- #
286
- # -r Revert the condition, i.e., remove elements that satisfy
287
- # the original condition.
288
- # -C Array compaction is not performed.
289
- #
290
- # @return 2 with a wrong usage, 1 when any elements are removed, 0 when the set
291
- # of array elements are unchanged. [ Note: the compaction will be performed
292
- # (without the option -C) even when the set of array elements are
293
- # unchanged. ]
294
- _comp_array_filter ()
295
- {
296
- local _comp_local_flags=' ' _comp_local_pattype=' ' _comp_local_anchoring=' '
297
- local OPTIND=1 OPTARG=' ' OPTERR=0 _comp_local_opt=' '
298
- while getopts ' EFGpsmxrC' _comp_local_opt " $@ " ; do
299
- case $_comp_local_opt in
300
- [EFG]) _comp_local_pattype=$_comp_local_opt ;;
301
- [psmx]) _comp_local_anchoring=$_comp_local_opt ;;
302
- [rC]) _comp_local_flags=$_comp_local_opt$_comp_local_flags ;;
303
- * )
304
- printf ' bash_completion: %s: %s\n' " $FUNCNAME " ' usage error' >&2
305
- printf ' usage: %s %s\n' " $FUNCNAME " " [-EFGpsmxrC] ARRAY_NAME CONDITION" >&2
306
- return 2
307
- ;;
308
- esac
309
- done
310
-
311
- shift $(( OPTIND - 1 ))
312
- if (( $# != 2 )) ; then
313
- printf ' bash_completion: %s: %s\n' " $FUNCNAME " " unexpected number of arguments: $# " >&2
314
- printf ' usage: %s %s\n' " $FUNCNAME " " [-EFGpsmxrC] ARRAY_NAME CONDITION" >&2
315
- return 2
316
- elif [[ $1 != [a-zA-Z_]* ([a-zA-Z_0-9]) ]]; then
317
- printf ' bash_completion: %s: %s\n' " $FUNCNAME " " invalid array name '$1 '." >&2
318
- return 2
319
- elif [[ $1 == @ (_comp_local_* | OPTIND| OPTARG| OPTERR) ]]; then
320
- printf ' bash_completion: %s: %s\n' " $FUNCNAME " " array name '$1 ' is reserved for internal uses" >&2
321
- return 2
322
- elif [[ ! $_comp_local_pattype && $1 == value ]]; then
323
- printf ' bash_completion: %s: %s\n' " $FUNCNAME " " array name '$1 ' cannot be used for the predicate" >&2
324
- return 2
325
- fi
326
- # When the array is empty:
327
- eval " ((\$ {#$1 [@]}))" || return 0
328
-
329
- local _comp_local_predicate=' ' _comp_local_pattern=$2
330
- case $_comp_local_pattype in
331
- E)
332
- case $_comp_local_anchoring in
333
- p) _comp_local_predicate=' [[ $_comp_local_value =~ ^($_comp_local_pattern) ]]' ;;
334
- s) _comp_local_predicate=' [[ $_comp_local_value =~ ($_comp_local_pattern)$ ]]' ;;
335
- x) _comp_local_predicate=' [[ $_comp_local_value =~ ^($_comp_local_pattern)$ ]]' ;;
336
- * ) _comp_local_predicate=' [[ $_comp_local_value =~ $_comp_local_pattern ]]' ;;
337
- esac
338
- ;;
339
- F)
340
- case $_comp_local_anchoring in
341
- p) _comp_local_predicate=' [[ $_comp_local_value == "$_comp_local_pattern"* ]]' ;;
342
- s) _comp_local_predicate=' [[ $_comp_local_value == *"$_comp_local_pattern" ]]' ;;
343
- x) _comp_local_predicate=' [[ $_comp_local_value == "$_comp_local_pattern" ]]' ;;
344
- * ) _comp_local_predicate=' [[ $_comp_local_value == *"$_comp_local_pattern"* ]]' ;;
345
- esac
346
- ;;
347
- G)
348
- case $_comp_local_anchoring in
349
- p) _comp_local_predicate=' [[ $_comp_local_value == $_comp_local_pattern* ]]' ;;
350
- s) _comp_local_predicate=' [[ $_comp_local_value == *$_comp_local_pattern ]]' ;;
351
- m) _comp_local_predicate=' [[ $_comp_local_value == *$_comp_local_pattern* ]]' ;;
352
- * ) _comp_local_predicate=' [[ $_comp_local_value == $_comp_local_pattern ]]' ;;
353
- esac
354
- ;;
355
- * )
356
- if declare -F " $2 " & > /dev/null; then
357
- _comp_local_predicate=" $2 \"\$ _comp_local_value\" "
358
- else
359
- _comp_local_predicate=" local value=\$ _comp_local_value; $2 "
360
- fi
361
- ;;
362
- esac
363
-
364
- local _comp_local_unset=' ' _comp_local_expected_status=0
365
- [[ $_comp_local_flags == * r* ]] && _comp_local_expected_status=1
366
-
367
- local _comp_local_indices _comp_local_index _comp_local_value
368
- eval " _comp_local_indices=(\"\$ {!$1 [@]}\" )"
369
- for _comp_local_index in " ${_comp_local_indices[@]} " ; do
370
- eval " _comp_local_value=\$ {$1 [\$ _comp_local_index]}; $_comp_local_predicate "
371
- case $? in
372
- " $_comp_local_expected_status " ) continue ;;
373
- [01])
374
- unset -v " $1 [\$ _comp_local_index]"
375
- _comp_local_unset=1
376
- ;;
377
- * )
378
- printf ' bash_completion: %s: %s\n' " $FUNCNAME " \
379
- " the filter condition broken '${_comp_local_pattype: +-$_comp_local_pattype } $2 '" >&2
380
- return 2
381
- ;;
382
- esac
383
- done
384
-
385
- # Compaction of the sparse array
386
- [[ $_comp_local_flags == * C* ]] ||
387
- eval " ((\$ {#$1 [@]})) && $1 =(\"\$ {$1 [@]}\" )"
388
-
389
- [[ ! $_comp_local_unset ]]
390
- }
391
-
392
263
# Reassemble command line words, excluding specified characters from the
393
264
# list of word completion separators (COMP_WORDBREAKS).
394
265
# @param $1 chars Characters out of $COMP_WORDBREAKS which should
0 commit comments