zsh-users
 help / color / mirror / code / Atom feed
* Completing possible elements of a comma-separated list
@ 2003-09-18 17:07 Malte Starostik
  2003-09-19 13:03 ` Oliver Kiddle
  0 siblings, 1 reply; 5+ messages in thread
From: Malte Starostik @ 2003-09-18 17:07 UTC (permalink / raw)
  To: zsh-users

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hi,

I'm trying to write a completion for slptool. One of its commands is
    slptool findattrs <service> [attr ids]
where service is an SLP service name or URL and attr ids is a comma-separated
list of attribute ids.
The following works fine for a single id, inserting a comma after it:

compadd -M 'm:{a-zA-Z}={A-Za-z}' -S, -q $(slptool findattrs $words[2] | \
    perl -pi -e 's#\(([^()]+)=[^()]*\),?#$1\n#g')

Now I'd like to continue completion with the same list of possible matches,
completing any id in the comma separated list independently of each other
against the list of available ids.

Example: (possible attribute ids: username password foo bar)
command typed so far:
    slptool findattrs example.acme u<TAB>
completes to (already works with the above):
    slptool findattrs example.acme username,
continuing:
    slptool findattrs example.acme username,p<TAB>
should complete to (this is what I'd like to achieve):
    slptool findattrs example.acme username,password,

Also, would be nice if
    slptool findattrs example.acme u,p<TAB>
could complete to
    slptool findattrs example.acme username,password,
as well.

Many thanks in advance,
- -Malte
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/aeZwVDF3RdLzx4cRAq/FAJ9D61VZhfqIxBVlvQeOwLPBBrfLfwCeK3PD
cNMbKjTLWbsO83sMiO8/tZI=
=qeuC
-----END PGP SIGNATURE-----


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

* Re: Completing possible elements of a comma-separated list
  2003-09-18 17:07 Completing possible elements of a comma-separated list Malte Starostik
@ 2003-09-19 13:03 ` Oliver Kiddle
  2003-09-19 16:31   ` Malte Starostik
  0 siblings, 1 reply; 5+ messages in thread
From: Oliver Kiddle @ 2003-09-19 13:03 UTC (permalink / raw)
  To: Malte Starostik; +Cc: zsh-users

Malte Starostik wrote:
> 
> I'm trying to write a completion for slptool. One of its commands is
>     slptool findattrs <service> [attr ids]

> Now I'd like to continue completion with the same list of possible matches,
> completing any id in the comma separated list independently of each other
> against the list of available ids.

Easiest way is probably to use:
  _values -s , 'attribute id' $(slptool findattrs ...)

Otherwise, you can use:

local -a suf
compset -P '*,'
compset -S ',*' || suf=( -S , )
compadd ... "$suf[@]" ...

That'll make it ignore anything before the cursor up the last comma
(and the same in reverse for after the cursor).

> Also, would be nice if
>     slptool findattrs example.acme u,p<TAB>
> could complete to
>     slptool findattrs example.acme username,password,
> as well.

That's harder. _sep_parts or _multi_parts might help you. Otherwise, you
need to start using compadd -O and it gets complicated.

Oliver


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

* Re: Completing possible elements of a comma-separated list
  2003-09-19 13:03 ` Oliver Kiddle
@ 2003-09-19 16:31   ` Malte Starostik
  2003-09-20 16:35     ` Oliver Kiddle
  2003-09-21  3:38     ` Hello Zsh users jayjwa
  0 siblings, 2 replies; 5+ messages in thread
From: Malte Starostik @ 2003-09-19 16:31 UTC (permalink / raw)
  To: zsh-users; +Cc: Oliver Kiddle

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Friday 19 September 2003 15:03, Oliver Kiddle wrote:
> Malte Starostik wrote:
> > I'm trying to write a completion for slptool. One of its commands is
> >     slptool findattrs <service> [attr ids]
> >
> > Now I'd like to continue completion with the same list of possible
> > matches, completing any id in the comma separated list independently of
> > each other against the list of available ids.
>
> Easiest way is probably to use:
>   _values -s , 'attribute id' $(slptool findattrs ...)
>
> Otherwise, you can use:
>
> local -a suf
> compset -P '*,'
> compset -S ',*' || suf=( -S , )
> compadd ... "$suf[@]" ...
>
> That'll make it ignore anything before the cursor up the last comma
> (and the same in reverse for after the cursor).

Thanks, these both work, the latter a little better as it accepts -M to get 
case-insensitivity.

> > Also, would be nice if
> >     slptool findattrs example.acme u,p<TAB>
> > could complete to
> >     slptool findattrs example.acme username,password,
> > as well.
>
> That's harder. _sep_parts or _multi_parts might help you. Otherwise, you
> need to start using compadd -O and it gets complicated.

for _multi_parts I'd need to add all possible permutations I gather? Anyway, 
this Works For Me (TM):

    local -a attrs
    local parts
    attrs=($(slptool findattrs $words[2] | \
               perl -pi -e 's#\(([^()]+)=[^()]*\),?#$1\n#g'))
    parts="_sep_parts -M 'm:{a-zA-Z}={A-Za-z}'"
    repeat $[${#attrs}*5] do
        parts="$parts attrs ,";
    done
    eval $parts

*5 is an arbitrary value that allows for listing attributes multiple times. 
Not that useful but allowed and it look like the list can be continued almost 
indefinately :-)
I only wonder why it seems to ignore -M ...

Thanks alot,
- -Malte
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQE/ay+CVDF3RdLzx4cRAi1hAJ4zddBRSA23EXXCPQZhc7IhzEKooACgwg2T
rGMDXOsX/KKZivHzFz2gRDs=
=1ACe
-----END PGP SIGNATURE-----


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

* Re: Completing possible elements of a comma-separated list
  2003-09-19 16:31   ` Malte Starostik
@ 2003-09-20 16:35     ` Oliver Kiddle
  2003-09-21  3:38     ` Hello Zsh users jayjwa
  1 sibling, 0 replies; 5+ messages in thread
From: Oliver Kiddle @ 2003-09-20 16:35 UTC (permalink / raw)
  To: Malte Starostik; +Cc: zsh-users

Malte Starostik wrote:
> 
> Thanks, these both work, the latter a little better as it accepts -M to get
> case-insensitivity.

With _values, you can use:

  args=( -M 'm:{a-zA-Z}={A-Za-z}' )
and then
  _values -O args -s , ...

_values will have the advantage that the same value will not be
completed twice in the list. Though you can do that manually with
something like:  attrs=( ${attrs:#(${~words[CURRENT]/,/|})} )

> for _multi_parts I'd need to add all possible permutations I gather? Anyway,

Yes. Not ideal if there are more than a few.

> this Works For Me (TM):
> 
>     local -a attrs
>     local parts
>     attrs=($(slptool findattrs $words[2] | \
>                perl -pi -e 's#\(([^()]+)=[^()]*\),?#$1\n#g'))
>     parts="_sep_parts -M 'm:{a-zA-Z}={A-Za-z}'"
>     repeat $[${#attrs}*5] do
>         parts="$parts attrs ,";
>     done
>     eval $parts
> 
> *5 is an arbitrary value that allows for listing attributes multiple times.

Surely $#attrs*5 will be far higher than necessary. You could instead
count the number of commas in the current word and add one to it:
  $(( 1 + ${#words[CURRENT]//[^,]/} ))

> I only wonder why it seems to ignore -M ...

Looks to me like _sep_parts is broken. I should first point out that
_sep_parts isn't used anywhere in the distributed completion functions
so it has probably had little real testing. It is also quite old.

_sep_parts tries to use both of compadd's -U and -M options together
yet they are in effect mutually exclusive: -U renders -M irrelevant. I
would have thought it should be using the matcher earlier when it does
compadd -O.

I was initially a bit mystified by this: why does _sep_parts go to all
the trouble of building an r: matcher for the suffix only to use
compadd -U thus making it irrelevant? However, looking at the history
of _sep_parts (or _comp_parts as it used to be called), it seems that
the original implementation predated the -O and -A options to compadd,
didn't use -U and will have needed the matcher where it was.

Would be good if you can try the patch though as you might spot a
problem (probably better to send any reply about it to zsh-workers
though).

Oliver

--- /usr/local/share/zsh/4.1.1-dev-1/functions/Completion/Base/_sep_parts	2003-09-10 21:01:11.000000000 +0200
+++ /home/opk/.zfunc/_sep_parts	2003-09-20 18:21:55.000000000 +0200
@@ -17,7 +17,7 @@
 # This function understands the `-J group', `-V group', and
 # `-X explanation' options.
 
-local str arr sep test testarr tmparr prefix suffixes matchers autosuffix
+local str arr sep test testarr tmparr prefix suffixes autosuffix
 local matchflags opt group expl nm=$compstate[nmatches] opre osuf opts matcher
 
 # Get the options.
@@ -25,12 +25,6 @@
 zparseopts -D -a opts \
     'J+:=group' 'V+:=group' P: F: S: r: R: q 1 2 n 'X+:=expl' 'M+:=matcher'
 
-if (( $#matcher )); then
-  matcher="${matcher[2]}"
-else
-  matcher=''
-fi
-
 # Get the string from the line.
 
 opre="$PREFIX"
@@ -58,9 +52,9 @@
   # Get the matching array elements.
 
   PREFIX="${str%%(|\\)${sep}*}"
-  builtin compadd -O testarr -a "$arr"
+  builtin compadd -O testarr "$matcher[@]" -a "$arr"
   [[ $#testarr -eq 0 && -n "$_comp_correct" ]] &&
-    compadd -O testarr -a "$arr"
+    compadd -O testarr "$matcher[@]" -a "$arr"
 
   # If there are no matches we give up. If there is more than one
   # match, this is the part we will complete.
@@ -88,9 +82,9 @@
   # No more separators, build the matches.
 
   PREFIX="$str"
-  builtin compadd -O testarr -a "$arr"
+  builtin compadd -O testarr "$matcher[@]" -a "$arr"
   [[ $#testarr -eq 0 && -n "$_comp_correct" ]] &&
-    compadd -O testarr -a "$arr"
+    compadd -O testarr "$matcher[@]" -a "$arr"
 fi
 
 [[ $#testarr -eq 0 || ${#testarr[1]} -eq 0 ]] && return 1
@@ -98,7 +92,6 @@
 # Now we build the suffixes to give to the completion code.
 
 shift
-matchers=()
 suffixes=("")
 autosuffix=()
 
@@ -125,17 +118,12 @@
     arr=tmparr
   fi
 
-  builtin compadd -O tmparr -a "$arr"
+  builtin compadd -O tmparr "$matcher[@]" -a "$arr"
   [[ $#tmparr -eq 0 && -n "$_comp_correct" ]] &&
-    compadd -O tmparr - "$arr"
+    compadd -O tmparr "$matcher[@]" - "$arr"
 
   suffixes=("${(@)^suffixes[@]}${(q)1}${(@)^tmparr}")
 
-  # We want the completion code to generate the most specific suffix
-  # for us, so we collect matching specifications that allow partial
-  # word matching before the separators on the fly.
-
-  matchers=("$matchers[@]" "r:|${1:q}=*")
   shift 2
 done
 
@@ -144,17 +132,12 @@
 
 (( $# )) && autosuffix=(-qS "${(q)1}")
 
-# If we have collected matching specifications, we build an array
-# from it that can be used as arguments to `compadd'.
-
-[[ $#matchers+$#matcher -gt 0 ]] && matchers=(-M "$matchers $matcher")
-
 # Add the matches for each of the suffixes.
 
 PREFIX="$pre"
 SUFFIX="$suf"
 for i in "$suffixes[@]"; do
-  compadd -U "$group[@]" "$expl[@]" "$matchers[@]" "$autosuffix[@]" "$opts[@]" \
+  compadd -U "$group[@]" "$expl[@]" "$autosuffix[@]" "$opts[@]" \
           -i "$IPREFIX" -I "$ISUFFIX" -p "$prefix" -s "$i" -a testarr
 done
 


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

* Hello Zsh users
  2003-09-19 16:31   ` Malte Starostik
  2003-09-20 16:35     ` Oliver Kiddle
@ 2003-09-21  3:38     ` jayjwa
  1 sibling, 0 replies; 5+ messages in thread
From: jayjwa @ 2003-09-21  3:38 UTC (permalink / raw)
  To: zsh-users


Hi, I just joined the list. I started using zsh recently after switching
over from bash. I look forward to reading about my new favorite shell.


--------------nonoffensive sig.v1.2RC1------------------------
- jayjwa            4 Spammers: mailto: listme@listme.dsbl.org
The New Atr2.          PGP/GPG Keys onsite

==Atr2.Ath.Cx: Linux Tough, Powered by Slackware.=============




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

end of thread, other threads:[~2003-09-21  8:17 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-09-18 17:07 Completing possible elements of a comma-separated list Malte Starostik
2003-09-19 13:03 ` Oliver Kiddle
2003-09-19 16:31   ` Malte Starostik
2003-09-20 16:35     ` Oliver Kiddle
2003-09-21  3:38     ` Hello Zsh users jayjwa

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).