Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

PATCH: completion utility function for lists



This adds a wrapper function that can be used for completing a separated
list where you want to use the same function for all items in the list.
We've already got an _list so I've called it _sequence. That name
perhaps implies that an ordering is significant which it isn't. I'm open
to better suggestions.

I didn't identify a vast number of uses for the function but I think it
is a simplification in those cases where it can be used. It handles
removing duplicates which we often didn't bother with. Similarly, it
also bothers to deal with $SUFFIX properly which some functions omit.

In some of the uses in the patch, you'll see that I'm using it to wrap
compadd (via _wanted).

_values does something similar but only for fixed values and with
generated values, you would need to be careful to quote colons.

Oliver

diff --git a/Completion/Base/Utility/_sequence b/Completion/Base/Utility/_sequence
new file mode 100644
index 0000000..391e5f7
--- /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]}"
diff --git a/Completion/Unix/Command/_mount b/Completion/Unix/Command/_mount
index 542154a..0428222 100644
--- a/Completion/Unix/Command/_mount
+++ b/Completion/Unix/Command/_mount
@@ -688,7 +688,7 @@ if [[ "$service" = mount ]]; then
       "($excl -r -w --rw)"{-w,--rw}'[mount read/write]'
       "($excl)-L+[mount partition with specified label]:label:->labels"
       "($excl)-U+[mount partition with specified uuid]:uuid"
-      "($excl -t --types)"{-t+,--types=}'[specify file system type]:file system type:->fslist'
+      "($excl -t --types)"{-t+,--types=}'[specify file system type]:file system type:_sequence -s , _file_systems'
       "($excl -O --test-opts)"{-O+,--test-opts=}'[with -a, restrict filesystems by options]:file system option:->fsopt'
       "($excl -a -O -o --options)"{-o+,--options=}'[specify file system options]:file system option:->fsopt'
       '(: -)'{-B,--bind}'[remount part of filesystem elsewhere]:old directory:_directories:new directory:_directories'
@@ -743,7 +743,7 @@ if [[ "$service" = mount ]]; then
       '-o[specify file system options]:file system option:->fsopt'
       '-p[print mounted file systems]'
       '-r[mount readonly]'
-      '-t[specify file system type]:file system type:->fslist'
+      '-t[specify file system type]:file system type:_sequence -s, _file_systems'
       '-u[change status of already mounted filesystem]'
       '-v[verbose mode]'
       '-w[mount read/write]'
@@ -817,7 +817,7 @@ else
 	'-A[unmount all mounted file systems except the root]'
 	'-f[force unmount]'
 	'-h[unmount all filesystems associated with host]:host:_hosts'
-	'-t[unmount all filesystems of specified type]:file system type:->fslist'
+	'-t[unmount all filesystems of specified type]:file system type:_sequence -s, _file_systems'
 	'-v[verbose mode]'
 	'*:dev or dir:->udevordir'
       )
@@ -846,11 +846,6 @@ else
 fi
 
 case "$state" in
-fslist)
-  compset -P '*,'
-  compset -S ',*' || suf=','
-  _file_systems -qS "$suf"
-;;
 fsopt)
   _tags options || return 1
 
diff --git a/Completion/Unix/Command/_nmap b/Completion/Unix/Command/_nmap
index f23937a..437e68b 100644
--- a/Completion/Unix/Command/_nmap
+++ b/Completion/Unix/Command/_nmap
@@ -22,7 +22,7 @@ _arguments -C \
   '-iR[scan random hosts]:num hosts' \
   '-p[specify ports to try]:port numbers' \
   '-F[scan only ports listed in services file]' \
-  '-D[perform decoy scan]:host list:->host-list' \
+  '-D[perform decoy scan]:host list:_sequence -s, _hosts' \
   '-S[specify source address]:address:_hosts' \
   '-e[specify interface to use]:network interface:_net_interfaces' \
   '-g[specify source port number]:port number' \
diff --git a/Completion/Unix/Command/_pgrep b/Completion/Unix/Command/_pgrep
index 3b180ab..95d0ea2 100644
--- a/Completion/Unix/Command/_pgrep
+++ b/Completion/Unix/Command/_pgrep
@@ -70,13 +70,9 @@ _arguments -s -w $arguments && ret=0
 
 case $state in
   (tty)
-    compset -P '*,'
-
-    local -a used ttys
-    used=(${(s:,:)IPREFIX})
-
+    local -a ttys
     ttys=( /dev/tty*(N) /dev/pts/*(N) )
-    _wanted tty expl 'terminal device' compadd -S ',' -q -F used ${ttys#/dev/}
+    _sequence -s , _wanted tty expl 'terminal device' compadd - ${ttys#/dev/}
     ;;
 
   (sid)
diff --git a/Completion/Unix/Command/_zip b/Completion/Unix/Command/_zip
index a6baa97..171daf0 100644
--- a/Completion/Unix/Command/_zip
+++ b/Completion/Unix/Command/_zip
@@ -104,11 +104,8 @@ fi
 
 case $state in
   suffixes)
-    compset -P '*:'
-    compset -S ':*' || suf=":."
     suffixes=( *.*(N:e) )
-    _wanted suffixes expl suffixes \
-	compadd -S "$suf" -r ": \t" .$^suffixes && return
+    _sequence -s : _wanted -x suffixes expl suffix compadd - .$^suffixes && return
   ;;
   files)
     if [[ $service = zip ]] && (( ! ${+opt_args[-d]} )); then
diff --git a/Doc/Zsh/compsys.yo b/Doc/Zsh/compsys.yo
index 8a9f47d..920b590 100644
--- a/Doc/Zsh/compsys.yo
+++ b/Doc/Zsh/compsys.yo
@@ -4741,6 +4741,19 @@ This function accepts the tt(compadd) options `tt(-V)', `tt(-J)',
 `tt(-r)', `tt(-R)', and `tt(-q)' and passes them on to the tt(compadd)
 builtin used to add the matches.
 )
+findex(_sequence)
+item(tt(_sequence) [ tt(-s) var(sep) ] [ tt(-n) var(max) ] [ tt(-d) ] var(function) [ tt(-) ] ...)(
+This function is a wrapper to other functions for completing items in a
+separated list. The same function is used to complete each item in the
+list. The separator is specified with the tt(-s) option. If tt(-s) is
+omitted it will use `tt(,)'. Duplicate values are not matched unless
+tt(-d) is specified. If there is a fixed or maximum number of items in
+the list, this can be specified with the tt(-n) option.
+
+Common tt(compadd) options are passed on to the function. It is possible
+to use tt(compadd) directly with tt(_sequence), though tt(_values) may
+be more appropriate in this situation.
+)
 findex(_setup)
 item(tt(_setup) var(tag) [ var(group) ])(
 This function sets up the special



Messages sorted by: Reverse Date, Date, Thread, Author