Skip to content

Commit fd5526b

Browse files
committed
fix(_comp_array_filter): move to a separate file
1 parent 38d539c commit fd5526b

File tree

2 files changed

+130
-129
lines changed

2 files changed

+130
-129
lines changed

bash_completion

Lines changed: 0 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -260,135 +260,6 @@ _upvars()
260260
done
261261
}
262262

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-
392263
# Reassemble command line words, excluding specified characters from the
393264
# list of word completion separators (COMP_WORDBREAKS).
394265
# @param $1 chars Characters out of $COMP_WORDBREAKS which should

completions/ARRAY

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
# Utility xfunc functions for array manipulations -*- shell-script -*-
2+
3+
# Filter the array elements with the specified condition.
4+
# @param $1 Array name (that is not "value" or other internal variable names)
5+
# @param $2 When none of the options -EFG are specified, this is used as the
6+
# command that tests the array element. The command is supposed to exit with
7+
# status 0 when the element should be preserved, and 1 when the element
8+
# should be removed. The other exit status will cancel the array filtering.
9+
# If this is an existing function name, the function is called with the value
10+
# of the array element. Otherwise, this shall be the shell command that
11+
# tests the array-element value stored in the shell variable "value".
12+
#
13+
# Options:
14+
# -E $2 is interpreted as a POSIX extended regular expression.
15+
# The default anchoring is `-m` (see below).
16+
# -F $2 is interpreted as a fixed string. The default anchoring
17+
# is `-m` (see below).
18+
# -G $2 is interpreted as a glob pattern. The default anchoring
19+
# is `-x` (see below).
20+
#
21+
# -p Combined with -EFG, it performs the prefix matching.
22+
# -s Combined with -EFG, it performs the suffix matching.
23+
# -m Combined with -EFG, it performs the middle matching.
24+
# -x Combined with -EFG, it performs the exact matching.
25+
#
26+
# -r Revert the condition, i.e., remove elements that satisfy
27+
# the original condition.
28+
# -C Array compaction is not performed.
29+
#
30+
# @return 2 with a wrong usage, 1 when any elements are removed, 0 when the set
31+
# of array elements are unchanged. [ Note: the compaction will be performed
32+
# (without the option -C) even when the set of array elements are
33+
# unchanged. ]
34+
_comp_xfunc_ARRAY_filter()
35+
{
36+
local _comp_local_flags='' _comp_local_pattype='' _comp_local_anchoring=''
37+
local OPTIND=1 OPTARG='' OPTERR=0 _comp_local_opt=''
38+
while getopts 'EFGpsmxrC' _comp_local_opt "$@"; do
39+
case $_comp_local_opt in
40+
[EFG]) _comp_local_pattype=$_comp_local_opt ;;
41+
[psmx]) _comp_local_anchoring=$_comp_local_opt ;;
42+
[rC]) _comp_local_flags=$_comp_local_opt$_comp_local_flags ;;
43+
*)
44+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" 'usage error' >&2
45+
printf 'usage: %s %s\n' "$FUNCNAME" "[-EFGpsmxrC] ARRAY_NAME CONDITION" >&2
46+
return 2
47+
;;
48+
esac
49+
done
50+
51+
shift $((OPTIND - 1))
52+
if (($# != 2)); then
53+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "unexpected number of arguments: $#" >&2
54+
printf 'usage: %s %s\n' "$FUNCNAME" "[-EFGpsmxrC] ARRAY_NAME CONDITION" >&2
55+
return 2
56+
elif [[ $1 != [a-zA-Z_]*([a-zA-Z_0-9]) ]]; then
57+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "invalid array name '$1'." >&2
58+
return 2
59+
elif [[ $1 == @(_comp_local_*|OPTIND|OPTARG|OPTERR) ]]; then
60+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "array name '$1' is reserved for internal uses" >&2
61+
return 2
62+
elif [[ ! $_comp_local_pattype && $1 == value ]]; then
63+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" "array name '$1' cannot be used for the predicate" >&2
64+
return 2
65+
fi
66+
# When the array is empty:
67+
eval "((\${#$1[@]}))" || return 0
68+
69+
local _comp_local_predicate='' _comp_local_pattern=$2
70+
case $_comp_local_pattype in
71+
E)
72+
case $_comp_local_anchoring in
73+
p) _comp_local_predicate='[[ $_comp_local_value =~ ^($_comp_local_pattern) ]]' ;;
74+
s) _comp_local_predicate='[[ $_comp_local_value =~ ($_comp_local_pattern)$ ]]' ;;
75+
x) _comp_local_predicate='[[ $_comp_local_value =~ ^($_comp_local_pattern)$ ]]' ;;
76+
*) _comp_local_predicate='[[ $_comp_local_value =~ $_comp_local_pattern ]]' ;;
77+
esac
78+
;;
79+
F)
80+
case $_comp_local_anchoring in
81+
p) _comp_local_predicate='[[ $_comp_local_value == "$_comp_local_pattern"* ]]' ;;
82+
s) _comp_local_predicate='[[ $_comp_local_value == *"$_comp_local_pattern" ]]' ;;
83+
x) _comp_local_predicate='[[ $_comp_local_value == "$_comp_local_pattern" ]]' ;;
84+
*) _comp_local_predicate='[[ $_comp_local_value == *"$_comp_local_pattern"* ]]' ;;
85+
esac
86+
;;
87+
G)
88+
case $_comp_local_anchoring in
89+
p) _comp_local_predicate='[[ $_comp_local_value == $_comp_local_pattern* ]]' ;;
90+
s) _comp_local_predicate='[[ $_comp_local_value == *$_comp_local_pattern ]]' ;;
91+
m) _comp_local_predicate='[[ $_comp_local_value == *$_comp_local_pattern* ]]' ;;
92+
*) _comp_local_predicate='[[ $_comp_local_value == $_comp_local_pattern ]]' ;;
93+
esac
94+
;;
95+
*)
96+
if declare -F "$2" &>/dev/null; then
97+
_comp_local_predicate="$2 \"\$_comp_local_value\""
98+
else
99+
_comp_local_predicate="local value=\$_comp_local_value; $2"
100+
fi
101+
;;
102+
esac
103+
104+
local _comp_local_unset='' _comp_local_expected_status=0
105+
[[ $_comp_local_flags == *r* ]] && _comp_local_expected_status=1
106+
107+
local _comp_local_indices _comp_local_index _comp_local_value
108+
eval "_comp_local_indices=(\"\${!$1[@]}\")"
109+
for _comp_local_index in "${_comp_local_indices[@]}"; do
110+
eval "_comp_local_value=\${$1[\$_comp_local_index]}; $_comp_local_predicate"
111+
case $? in
112+
"$_comp_local_expected_status") continue ;;
113+
[01])
114+
unset -v "$1[\$_comp_local_index]"
115+
_comp_local_unset=1
116+
;;
117+
*)
118+
printf 'bash_completion: %s: %s\n' "$FUNCNAME" \
119+
"the filter condition broken '${_comp_local_pattype:+-$_comp_local_pattype }$2'" >&2
120+
return 2
121+
;;
122+
esac
123+
done
124+
125+
# Compaction of the sparse array
126+
[[ $_comp_local_flags == *C* ]] ||
127+
eval "((\${#$1[@]})) && $1=(\"\${$1[@]}\")"
128+
129+
[[ ! $_comp_local_unset ]]
130+
}

0 commit comments

Comments
 (0)