zsh-workers
 help / color / mirror / code / Atom feed
* Re: PATCH: selective parameter completion
@ 2000-08-11  7:44 Sven Wischnowsky
  0 siblings, 0 replies; 3+ messages in thread
From: Sven Wischnowsky @ 2000-08-11  7:44 UTC (permalink / raw)
  To: zsh-workers


Oliver Kiddle wrote:

> I've implemented the change to _parameters which I mentioned yesterday
> and modified a few other completion functions to make use of it.

Great.

> In the
> process, it has raised a few questions:
> 
> In _arrays, _wanted is used but _parameters also calls _wanted so the
> description of what is to be completed - 'array' - is replaced by
> 'parameter'. _files manages to avoid this problem so how is it done and
> what should _parameters be doing here?

I think none of the functions calling _parameters should be calling
_wanted (when calling _parameters). For files, the tag comes with the
file-patterns style, so we'll have them for _parameters once we add a
parameter-patterns (or should it be parameter-types?) style. For other 
cases, _files (and _path_files) looks at the pattern to find out if it 
is completing directories.

For now, I think using only the parameter tag is fine (and adding a
special option, like the -/ for _files, to make it complete only, say, 
arrays, is probably not worth it). But _parameters should take care to 
make a description supplied by the calling function take precedence
over its own description -- just as _files does it. (For this and the
pattern stuff we could probably just copy some code from _files...)

One question that might be interesting is (already asked by you in your 
last mail for this), if there are cases where making the default
completion try different types of parameters one after another makes
sense (as in file completion). The only and obvious case I can think
of is when the calling function uses -g. I.e. first try it with that
pattern and the complete all parameters.

> How does the implementation of _math differ from if compset -P and
> compset -S were used to move the patterns from PREFIX to IPREFIX and
> SUFFIX to ISUFFIX? It would be nice if spaces were treated more
> cleanly, for example:
> : $(( <tab>
> doesn't complete parameters but quotes the space.

Hm, it does complete parameters for me (and only integers and
floats). But the quoting is... urgh. Another thing is that _math
should not complete the special-character parameters (like !, $,
etc.) and that it should use parameter-expansion completion after a
`$' inside a math expression. The latter has to be fixed in C code,
I'll have a look. And the quoting has to be fixed in C-code, too, I
think, because the space is already reported in quoted form, which it
shouldn't when completing in a math expression.

> I have made _vars complete arrays and associations separately so that a
> '[' suffix can be added automatically. The trouble here is that it has
> to be quoted unless the glob option is not set otherwise a message
> about no matches being found will be printed when the command is run.
> Is it worth checking the state of the glob option before quoting this
> suffix. If so, should _precommand be doing a 'setopt localoptions
> noglob' when the noglob command is being completed to ensure the option
> is set during the completion of the command after it?

Except for command position, I think we should just always quote it
(using compquote to get it right without having to test if we are in
single or double quotes ourselves).

> Also, it would make sense to use _vars instead of _parameters when
> completing in command position but the square brackets aren't treated
> as a pattern there so how can I detect that it is being used in command
> postion: is [[ CURRENT = 1 ]] going to work?

Yes, it should.

> As far as I can tell, the existing code in _vars for completing
> association keys after an opening square-bracket is only useful when we
> have quoting because _subscript will do the job otherwise. I would add
> similar code for array indexes but it seems a bit pointless to be
> repeating code that we already have in _subscript. Calling _subscript
> doesn't seem to work so what would be a good way to avoid this
> repetition?

Just use _subscript, but make sure everything is set up correctly for
it. For example:

  compstate[parameter]=${PREFIX%%(|\\)\[*}

  IPREFIX=${PREFIX%%\[*}\[
  PREFIX=${PREFIX#*\[}

  _subscript

seems to work for the (admittedly simple) cases I tried.

Bye
 Sven


--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


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

* Re: PATCH: selective parameter completion
@ 2000-08-11 12:16 Sven Wischnowsky
  0 siblings, 0 replies; 3+ messages in thread
From: Sven Wischnowsky @ 2000-08-11 12:16 UTC (permalink / raw)
  To: zsh-workers


I wrote:

> Oliver Kiddle wrote:
> 
> ...
> 
> > How does the implementation of _math differ from if compset -P and
> > compset -S were used to move the patterns from PREFIX to IPREFIX and
> > SUFFIX to ISUFFIX? It would be nice if spaces were treated more
> > cleanly, for example:
> > : $(( <tab>
> > doesn't complete parameters but quotes the space.
> 
> Hm, it does complete parameters for me (and only integers and
> floats). But the quoting is... urgh. Another thing is that _math
> should not complete the special-character parameters (like !, $,
> etc.) and that it should use parameter-expansion completion after a
> `$' inside a math expression. The latter has to be fixed in C code,
> I'll have a look. And the quoting has to be fixed in C-code, too, I
> think, because the space is already reported in quoted form, which it
> shouldn't when completing in a math expression.

Ok, here's the patch. It avoids quoting the string when completing
inside a math expression and it makes it be tokenized so that
completion after a `$' is completed like every other parameter-
expansion. That hunk in _parameters keeps the special parameters like
`!' and `$' from being quoted.


And I forgot to answer the first part: of course using compset should
do the same. But why use it? Is it really easier to read? And I don't
think it's faster or something.


And yet another thing I forgot to point out: making the function
complete only integers and float parameters in math contexts really
only works in a perfect world. But as it is, we often use scalar
parameters to store numbers that are then used in math contexts. So we 
either need to check the value of the parameters (at least if it's a
scalar), or we *really* need the parameter-patterns (or -types) style
so that people can override the default of completing only integers
and floats.

Bye
 Sven

Index: Completion/Core/_parameters
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Core/_parameters,v
retrieving revision 1.3
diff -u -r1.3 _parameters
--- Completion/Core/_parameters	2000/08/10 21:22:25	1.3
+++ Completion/Core/_parameters	2000/08/11 12:08:05
@@ -12,4 +12,4 @@
 zparseopts -D -K -E g:=pattern
 
 _wanted parameters expl parameter compadd "$@" \
-    -k "parameters[(R)${pattern[2]}~*local*]"
+   -Q -k "parameters[(R)${pattern[2]}~*local*]"
Index: Src/Zle/compcore.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/compcore.c,v
retrieving revision 1.37
diff -u -r1.37 compcore.c
--- Src/Zle/compcore.c	2000/07/18 08:37:15	1.37
+++ Src/Zle/compcore.c	2000/08/11 12:08:06
@@ -664,7 +664,7 @@
 	zsfree(compprefix);
 	zsfree(compsuffix);
 	if (unset(COMPLETEINWORD)) {
-	    tmp = multiquote(s, 0);
+	    tmp = (linwhat == IN_MATH ? dupstring(s) : multiquote(s, 0));
 	    untokenize(tmp);
 	    compprefix = ztrdup(tmp);
 	    compsuffix = ztrdup("");
@@ -675,11 +675,11 @@
 
 	    sav = *ss;
 	    *ss = '\0';
-	    tmp = multiquote(s, 0);
+	    tmp = (linwhat == IN_MATH ? dupstring(s) : multiquote(s, 0));
 	    untokenize(tmp);
 	    compprefix = ztrdup(tmp);
 	    *ss = sav;
-	    ss = multiquote(ss, 0);
+	    ss = (linwhat == IN_MATH ? dupstring(ss) : multiquote(ss, 0));
 	    untokenize(ss);
 	    compsuffix = ztrdup(ss);
 	}
Index: Src/Zle/zle_tricky.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/Zle/zle_tricky.c,v
retrieving revision 1.18
diff -u -r1.18 zle_tricky.c
--- Src/Zle/zle_tricky.c	2000/06/29 06:59:00	1.18
+++ Src/Zle/zle_tricky.c	2000/08/11 12:08:06
@@ -1311,6 +1311,7 @@
 	    } else
 		insubscr = 1;
 	}
+	parse_subst_string(s);
     }
     /* This variable will hold the current word in quoted form. */
     qword = ztrdup(s);

--
Sven Wischnowsky                         wischnow@informatik.hu-berlin.de


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

* PATCH: selective parameter completion
@ 2000-08-10 20:06 Oliver Kiddle
  0 siblings, 0 replies; 3+ messages in thread
From: Oliver Kiddle @ 2000-08-10 20:06 UTC (permalink / raw)
  To: Zsh workers

I've implemented the change to _parameters which I mentioned yesterday
and modified a few other completion functions to make use of it. In the
process, it has raised a few questions:

In _arrays, _wanted is used but _parameters also calls _wanted so the
description of what is to be completed - 'array' - is replaced by
'parameter'. _files manages to avoid this problem so how is it done and
what should _parameters be doing here?

How does the implementation of _math differ from if compset -P and
compset -S were used to move the patterns from PREFIX to IPREFIX and
SUFFIX to ISUFFIX? It would be nice if spaces were treated more
cleanly, for example:
: $(( <tab>
doesn't complete parameters but quotes the space.

I have made _vars complete arrays and associations separately so that a
'[' suffix can be added automatically. The trouble here is that it has
to be quoted unless the glob option is not set otherwise a message
about no matches being found will be printed when the command is run.
Is it worth checking the state of the glob option before quoting this
suffix. If so, should _precommand be doing a 'setopt localoptions
noglob' when the noglob command is being completed to ensure the option
is set during the completion of the command after it?

Also, it would make sense to use _vars instead of _parameters when
completing in command position but the square brackets aren't treated
as a pattern there so how can I detect that it is being used in command
postion: is [[ CURRENT = 1 ]] going to work?

As far as I can tell, the existing code in _vars for completing
association keys after an opening square-bracket is only useful when we
have quoting because _subscript will do the job otherwise. I would add
similar code for array indexes but it seems a bit pointless to be
repeating code that we already have in _subscript. Calling _subscript
doesn't seem to work so what would be a good way to avoid this
repetition?

Oliver

Index: Completion/Base/_math
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Base/_math,v
retrieving revision 1.2
diff -u -r1.2 _math
--- Completion/Base/_math	2000/05/09 17:16:08	1.2
+++ Completion/Base/_math	2000/08/10 21:01:00
@@ -9,4 +9,4 @@
   SUFFIX="${SUFFIX%%[^a-zA-Z0-9_]*}"
 fi
 
-_parameters
+_parameters -g '(integer|float)*'
Index: Completion/Builtins/_arrays
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Builtins/_arrays,v
retrieving revision 1.2
diff -u -r1.2 _arrays
--- Completion/Builtins/_arrays	2000/06/22 08:42:36	1.2
+++ Completion/Builtins/_arrays	2000/08/10 21:01:00
@@ -2,4 +2,4 @@
 
 local expl
 
-_wanted arrays expl array compadd -k "parameters[(R)*array*~*local*]"
+_wanted arrays expl array _parameters "$@" -g '*array*'
Index: Completion/Builtins/_vars
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Builtins/_vars,v
retrieving revision 1.4
diff -u -r1.4 _vars
--- Completion/Builtins/_vars	2000/08/09 21:20:41	1.4
+++ Completion/Builtins/_vars	2000/08/10 21:01:00
@@ -1,9 +1,10 @@
 #compdef getopts unset vared
 
 # This will handle completion of keys of associative arrays, e.g. at
-# `vared foo[<TAB>'.  However, in this version the [ must be added
-# by hand.
+# `vared foo[<TAB>'.
 
+local ret=1
+
 if [[ $PREFIX = *\[* ]]; then
   local var=${PREFIX%%\[*}
   local elt="${PREFIX#*\]}${SUFFIX%\]}"
@@ -11,7 +12,7 @@
 
   compset -p $(( ${#var} + 1 ))
   if ! compset -S \]; then
-    addclose=(-S ']')
+    addclose=(-S "${${QIPREFIX:+]}:-\]}")
   fi
   if [[ ${(tP)var} = assoc* ]]; then
     local expl
@@ -20,5 +21,13 @@
         compadd $addclose -k "$var"
   fi
 else
-  _parameters "$@"
+  _parameters -g '^a*' "$@" && ret=0
+  
+  if compset -S '\[*'; then
+    set - -S "" "$@"
+  else
+    set - -qS"${${QIPREFIX:+[}:-\[}" "$@"
+  fi
+  _parameters -g 'a*' "$@" && ret=0
+  return ret
 fi
Index: Completion/Builtins/_zpty
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Builtins/_zpty,v
retrieving revision 1.8
diff -u -r1.8 _zpty
--- Completion/Builtins/_zpty	2000/05/31 09:38:26	1.8
+++ Completion/Builtins/_zpty	2000/08/10 21:01:00
@@ -10,7 +10,7 @@
   '(-r -w -L -e -b)-d[delete command]:*:name:->name' \
   '(-r -L -e -b -d)-w[send string to command]:name:->name:*:strings to write' \
   '(: -r -w -e -b -d)-L[list defined commands as calls]' \
-  '(: -w -L -e -b -d)-r[read string from command]:name:->name:param: _parameters:pattern:' \
+  '(: -w -L -e -b -d)-r[read string from command]:name:->name:param: _vars:pattern:' \
   '(-r -w -L -d):zpty command name:' \
   '(-r -w -L -d):cmd: _command_names -e' \
   '(-r -w -L -d)*::args:_precommand' && return 0
@@ -21,7 +21,7 @@
 #   - read \
 #     '-r[read string from command]' \
 #     ':name:->name' \
-#     ':param: _parameters' \
+#     ':param: _vars' \
 #     ':pattern:' \
 #   - write \
 #     '-w[send string to command]' \
Index: Completion/Commands/_bash_completions
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Commands/_bash_completions,v
retrieving revision 1.5
diff -u -r1.5 _bash_completions
--- Completion/Commands/_bash_completions	2000/08/03 13:35:43	1.5
+++ Completion/Commands/_bash_completions	2000/08/10 21:01:00
@@ -34,7 +34,7 @@
   '!') _main_complete _command_names
        ;;
   '$') _main_complete - parameters _wanted parameters expl 'exported parameters' \
-                                       compadd -k 'parameters[(R)*export*]'
+                                       _parameters -g '*export*'
        ;;
   '@') _main_complete _hosts
        ;;
Index: Completion/Core/_parameters
===================================================================
RCS file: /cvsroot/zsh/zsh/Completion/Core/_parameters,v
retrieving revision 1.2
diff -u -r1.2 _parameters
--- Completion/Core/_parameters	2000/06/22 08:42:36	1.2
+++ Completion/Core/_parameters	2000/08/10 21:01:00
@@ -3,6 +3,13 @@
 # This should be used to complete parameter names if you need some of the
 # extra options of compadd. It completes only non-local parameters.
 
-local expl
+# If you specify a -g option with a pattern, the pattern will be used to
+# restrict the type of parameters matched.
 
-_wanted parameters expl parameter compadd "$@" -k 'parameters[(R)^*local*]'
+local expl pattern
+
+pattern=(-g \*)
+zparseopts -D -K -E g:=pattern
+
+_wanted parameters expl parameter compadd "$@" \
+    -k "parameters[(R)${pattern[2]}~*local*]"
Index: Doc/Zsh/compsys.yo
===================================================================
RCS file: /cvsroot/zsh/zsh/Doc/Zsh/compsys.yo,v
retrieving revision 1.90
diff -u -r1.90 compsys.yo
--- Doc/Zsh/compsys.yo	2000/08/02 13:45:52	1.90
+++ Doc/Zsh/compsys.yo	2000/08/10 21:01:27
@@ -3470,8 +3470,12 @@
 )
 findex(_parameters)
 item(tt(_parameters))(
-This should be used to complete parameter names.  All arguments are
-passed unchanged to the tt(compadd) builtin.
+This should be used to complete parameter names.  tt(_parameters) can
+take a tt(-g var(pattern)) option which specifies that only parameters
+whose type matches the var(pattern) should be completed.  Strings of
+the same form as those returned by the tt(t) parameter expansion flag
+are used here when matching the type.  All other arguments are passed
+unchanged to the tt(compadd) builtin.
 )
 findex(_path_files)
 item(tt(_path_files))(


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

end of thread, other threads:[~2000-08-11 12:17 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-08-11  7:44 PATCH: selective parameter completion Sven Wischnowsky
  -- strict thread matches above, loose matches on Subject: below --
2000-08-11 12:16 Sven Wischnowsky
2000-08-10 20:06 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).