zsh-workers
 help / color / mirror / code / Atom feed
* PATCH: completion utility function for lists
@ 2014-08-13 21:59 Oliver Kiddle
  2014-08-15  4:54 ` Bart Schaefer
  0 siblings, 1 reply; 4+ messages in thread
From: Oliver Kiddle @ 2014-08-13 21:59 UTC (permalink / raw)
  To: Zsh workers

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: PATCH: completion utility function for lists
  2014-08-13 21:59 PATCH: completion utility function for lists Oliver Kiddle
@ 2014-08-15  4:54 ` Bart Schaefer
  2014-08-15  8:42   ` Peter Stephenson
  2014-08-15 10:24   ` Oliver Kiddle
  0 siblings, 2 replies; 4+ messages in thread
From: Bart Schaefer @ 2014-08-15  4:54 UTC (permalink / raw)
  To: Zsh workers

Preliminary question:  I noticed these changes got committed/pushed.  Are
we rushing this in before the release for some reason, or will the release
be made off an older tag (in which case we'll need to branch (?) to put
in any patch for the "cd /" warning issue)?

On Aug 13, 11:59pm, Oliver Kiddle wrote:
} Subject: 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 was going to suggest _delimited, but I see it's been pushed as is.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: PATCH: completion utility function for lists
  2014-08-15  4:54 ` Bart Schaefer
@ 2014-08-15  8:42   ` Peter Stephenson
  2014-08-15 10:24   ` Oliver Kiddle
  1 sibling, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2014-08-15  8:42 UTC (permalink / raw)
  To: Zsh workers

On Thu, 14 Aug 2014 21:54:18 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:
> Preliminary question:  I noticed these changes got committed/pushed.  Are
> we rushing this in before the release for some reason, or will the release
> be made off an older tag (in which case we'll need to branch (?) to put
> in any patch for the "cd /" warning issue)?

I'm not branching.  I tend not to worry about new functions
since they shouldn't break anything, in theory.  I see it's
going to have knock-on effects on completion functions, so ideally ought
to wait, but it's not worth doing anything special now.

pws


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: PATCH: completion utility function for lists
  2014-08-15  4:54 ` Bart Schaefer
  2014-08-15  8:42   ` Peter Stephenson
@ 2014-08-15 10:24   ` Oliver Kiddle
  1 sibling, 0 replies; 4+ messages in thread
From: Oliver Kiddle @ 2014-08-15 10:24 UTC (permalink / raw)
  To: Zsh workers

On Friday, 15 August 2014, 6:55, Bart Schaefer <schaefer@brasslantern.com> wrote:

> Preliminary question:  I noticed these changes got committed/pushed.  Are
> we rushing this in before the release for some reason, or will the release
> be made off an older tag (in which case we'll need to branch (?) to put
> in any patch for the "cd /" warning issue)?




Sorry, it perhaps wasn't wise. I'm away myself for a few weeks and wanted to get it done before leaving. I've got a big mess of half-finished completion functions and having cleaned up a few wanted them out of the way. 

> I was going to suggest _delimited, but I see it's been pushed as is.

Possibly, though not sure it is much better. There's already an _delimiters which is unrelated. It also wouldn't be quite accurate if it gains a -w option like _values. I also considered _repeated.

Oliver



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2014-08-15 10:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-08-13 21:59 PATCH: completion utility function for lists Oliver Kiddle
2014-08-15  4:54 ` Bart Schaefer
2014-08-15  8:42   ` Peter Stephenson
2014-08-15 10:24   ` Oliver Kiddle

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).