summary refs log tree commit diff
path: root/Completion/Base
diff options
context:
space:
mode:
authorOliver Kiddle <opk@zsh.org>2014-08-14 00:00:39 +0200
committerOliver Kiddle <opk@zsh.org>2014-08-14 19:00:04 +0200
commit469ee6daef50d0736a38f646bd9eb659b7aad6d1 (patch)
treee77a3b919090d1d8433b4f06d6b41fcf6c8d387a /Completion/Base
parent5bcf00979fc5e01dd6fddff965aa871491f8b48b (diff)
downloadzsh-469ee6daef50d0736a38f646bd9eb659b7aad6d1.tar.gz
zsh-469ee6daef50d0736a38f646bd9eb659b7aad6d1.tar.xz
zsh-469ee6daef50d0736a38f646bd9eb659b7aad6d1.zip
32997: new utility function for separated lists
Diffstat (limited to 'Completion/Base')
-rw-r--r--Completion/Base/Utility/_sequence39
1 files changed, 39 insertions, 0 deletions
diff --git a/Completion/Base/Utility/_sequence b/Completion/Base/Utility/_sequence
new file mode 100644
index 000000000..391e5f78f
--- /dev/null
+++ b/Completion/Base/Utility/_sequence
@@ -0,0 +1,39 @@
+#autoload
+
+# a separated list where each component of the list uses the same
+# function.
+
+# -n num : number of items in list [default is unlimited]
+# -s sep : specify separator [defaults to comma]
+# -d     : duplicate values allowed
+
+local curcontext="$curcontext" nm="$compstate[nmatches]" pre nosep minus
+local -a sep num pref suf end uniq dedup
+
+zparseopts -D -a opts s:=sep n:=num p:=pref i:=pref P:=pref I:=suf S:=suf q=suf r:=suf R:=suf C:=cont d=uniq M: J: X: x:
+(( $#cont )) && curcontext="$curcontext%:*}:$cont[2]"
+(( $#sep )) || sep[2]=,
+
+if (( $+suf[(r)-S] )); then
+  end="${(q)suf[suf[(i)-S]+1]}"
+  (( $#end )) && compset -S ${end}\* && suf=() && nosep=1
+fi
+
+if (( ! $#uniq )); then
+  (( $+pref[(r)-P] )) && pre="${(q)pref[pref[(i)-P]+1]}"
+  typeset -T unique="${PREFIX#$pre}" uniq $sep[2]
+  dedup=( ${(q)uniq[1,-2]} )
+  unique="${SUFFIX}"
+  dedup+=( ${(q)uniq[2,-1]} )
+fi
+
+if (( ! $#num )) || (( num[2] > 1 )) && ! compset -P $(( num[2] - 1 )) \*$sep[2]; then
+  (( nosep )) || suf=( -S $sep[2] -r "$end[1]${sep[2][1]} \t\n\-" )
+  compset -S ${sep[2]}\* && suf=()
+  compset -P \*$sep[2] && pref=()
+else
+  pref=()
+fi
+
+(( minus = argv[(ib:2:)-] ))
+"${(@)argv[1,minus-1]}" "$opts[@]" -F dedup "$pref[@]" "$suf[@]" "${(@)argv[minus+1,-1]}"