From 469ee6daef50d0736a38f646bd9eb659b7aad6d1 Mon Sep 17 00:00:00 2001 From: Oliver Kiddle Date: Thu, 14 Aug 2014 00:00:39 +0200 Subject: 32997: new utility function for separated lists --- ChangeLog | 7 +++++++ Completion/Base/Utility/_sequence | 39 +++++++++++++++++++++++++++++++++++++++ Completion/Unix/Command/_mount | 11 +++-------- Completion/Unix/Command/_nmap | 2 +- Completion/Unix/Command/_pgrep | 8 ++------ Completion/Unix/Command/_zip | 5 +---- Doc/Zsh/compsys.yo | 13 +++++++++++++ 7 files changed, 66 insertions(+), 19 deletions(-) create mode 100644 Completion/Base/Utility/_sequence diff --git a/ChangeLog b/ChangeLog index 9ab2d4970..6bd84c07d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2014-08-14 Oliver Kiddle + + * 32997: Completion/Base/Utility/_sequence, Doc/Zsh/compsys.yo, + Completion/Unix/Command/_mount, Completion/Unix/Command/_nmap, + Completion/Unix/Command/_pgrep, Completion/Unix/Command/_zip: + add completion utility function for lists + 2014-08-14 Peter Stephenson * 33002: Doc/Zsh/tcpsys.yo, Functions/TCP/tcp_expect: add option 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]}" diff --git a/Completion/Unix/Command/_mount b/Completion/Unix/Command/_mount index 542154a58..04282225f 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 f23937abc..437e68b7d 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 3b180ab2d..95d0ea284 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 a6baa9757..171daf01e 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 8a9f47db1..920b5903d 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 -- cgit 1.4.1