zsh-workers
 help / color / mirror / code / Atom feed
* Re: help with _match, globcomplete etc.
@ 2000-02-15 16:08 Sven Wischnowsky
  2000-02-15 19:53 ` help with _match, globcomplete etc. (with a PATCH) Oliver Kiddle
  0 siblings, 1 reply; 8+ messages in thread
From: Sven Wischnowsky @ 2000-02-15 16:08 UTC (permalink / raw)
  To: zsh-workers


Oliver Kiddle wrote:

> ...
>
> so that ^X$ wouldn't also glob complete but it comes back with any glob
> characters quoted. It seems that expand-word always does this when
> noglob is set. Surely this isn't right? In the process, I also noticed
> that the quoting can go slightly wrong if there are opened but not
> closed braces: $code[ai]/{a,b<Ctrl-X,*> comes back with a quoted '[' in
> the middle.

Hm. It's the call to quotename() in zle_tricky.c:1649. I'm pretty sure 
this is intended to make things work when the words resulting from
parameter expansion or globbing contains characters that need to be
quoted.

Trying to fix that would be very hard... we would have to find out
which part comes from the expansion and which doesn't or maybe being
more careful with (un)tokenization()... so, no patch for this.

However, it also removed parts of the string in cases like the example --
brace expansions. That's due to the in-brace completion stuff. The
patch fixes this in the simplest way I could thnik of: keep a
unaltered copy of the word an let expansion work on that.

I couldn't reproduce your `[' problem, unless that was a typo and you
meant a quoted `{'. In this case it's the same as for the quoted
globbing characters.

Bye
 Sven

P.S.:   The difference between the not-fixed and the fixed part is
        that I didn't write the former and hence don't really feel
        responsible ;-)

P.P.S.: Yes, there are parts of zle_tricky.c I didn't write.

diff -ru ../z.old/Src/Zle/zle_tricky.c Src/Zle/zle_tricky.c
--- ../z.old/Src/Zle/zle_tricky.c	Tue Feb 15 13:21:40 2000
+++ Src/Zle/zle_tricky.c	Tue Feb 15 16:55:11 2000
@@ -108,6 +108,10 @@
 
 static char *qword;
 
+/* This holds the word we are working on without braces removed. */
+
+static char *origword;
+
 /* The quoted prefix/suffix and a flag saying if we want to add the
  * closing quote. */
 
@@ -685,17 +689,17 @@
 	    inwhat = IN_CMD;
 
 	if (lst == COMP_SPELL) {
-	    char *x, *q, *ox;
+	    char *w = dupstring(origword), *x, *q, *ox;
 
-	    for (q = s; *q; q++)
+	    for (q = w; *q; q++)
 		if (INULL(*q))
 		    *q = Nularg;
 	    cs = wb;
 	    foredel(we - wb);
 	    HEAPALLOC {
-		untokenize(x = ox = dupstring(s));
-		if (*s == Tilde || *s == Equals || *s == String)
-		    *x = *s;
+		untokenize(x = ox = dupstring(w));
+		if (*w == Tilde || *w == Equals || *w == String)
+		    *x = *w;
 		spckword(&x, 0, lincmd, 0);
 		ret = !strcmp(x, ox);
 	    } LASTALLOC;
@@ -708,7 +712,7 @@
 	    int ocs = cs, ne = noerrs;
 
 	    noerrs = 1;
-	    ret = doexpansion(s, lst, olst, lincmd);
+	    ret = doexpansion(origword, lst, olst, lincmd);
 	    lastambig = 0;
 	    noerrs = ne;
 
@@ -1329,6 +1333,9 @@
 		chuck(p--);
 	    }
 
+	zsfree(origword);
+	origword = ztrdup(s);
+
 	if (!isset(IGNOREBRACES)) {
 	    /* Try and deal with foo{xxx etc. */
 	    char *curs = s + (isset(COMPLETEINWORD) ? offs : strlen(s));
@@ -1353,8 +1360,8 @@
 			    break;
 			}
 			i += tp - p;
-			p = tp;
 			dp += tp - p;
+			p = tp;
 		    } else {
 			char *tp = p + 1;
 
@@ -1385,8 +1392,8 @@
 			    }
 			    tp--;
 			    i += tp - p;
-			    p = tp;
 			    dp += tp - p;
+			    p = tp;
 			}
 		    }
 		} else if (p < curs) {

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


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

* Re: help with _match, globcomplete etc. (with a PATCH)
  2000-02-15 16:08 help with _match, globcomplete etc Sven Wischnowsky
@ 2000-02-15 19:53 ` Oliver Kiddle
  2000-02-16  6:47   ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Oliver Kiddle @ 2000-02-15 19:53 UTC (permalink / raw)
  To: Zsh workers

Sven Wischnowsky wrote:

> I couldn't reproduce your `[' problem, unless that was a typo and you
> meant a quoted `{'. In this case it's the same as for the quoted
> globbing characters.

It wasn't a typo. It only occurs when the referenced associative array is set. I can reproduce it as follows:
zsh -f
bindkey -me
unsetopt glob
typeset -A code
code[ai]=foo
echo $code[ai]/{a,<tab>

and, in a separate message: 
> You can bind expand-word to ^X$. Expansion of parameter substitutions
> is a problem, because the completion (shell) code doesn't get the
> whole string. We only get the stuff after the $, so we can't really
> change such things.

I don't quite understand what you mean here. _expand seems to get the whole thing including the '$'. The main limiting factor on my configuration is that I can't do parameter expansion, command substitution and arithmetic expansion independantly. Maybe the e parameter expansion flag could allow options to select them. Also, I can't see a way of doing globbing, while preserving variable references.

In general, I don't like the substitution in _expand but the trouble with not having it enabled is that glob expansion will not work on lines with parameters. My solution is the following patch to _expand which adds the style subst-globs-only which if used with substitute and glob, only does expansion if globbing was able to do something meaningful. If you (Sven) are happy with this addition, I'll do a doc patch aswell.

and elsewhere:
> > Another thing which I would like to configure with expansion is when
> > there is only one match, I'd prefer if the space suffix was not added -
> > this is one of the things that makes the variable expansion annoying.
> > Ideally, the suffix would be as if normal completion was used so
> > directories would get a '/'.
> When speaking about variables: see above. When speaking about other
> expansions: _match does that and _expand could be made to do it
> (adding a loop that appends slashes to directory names).

It is only really when the globbing results in only one match that I am concerned about the suffix because my cursor is at the end of that match and I'm likely to go on adding more to it and doing more completion. I'll add a patch for this. Should I make it depend on a zstyle thing? Will it be needed for both of the compadds in _expand?

Out of interest, why does the check in _expand need to check against "$word"(|\(N\)) as opposed to just "$word": under what circumstances can the (N) find its way into $exp?

Thanks for your help

Oliver

--- Completion/Core/_expand.bak Tue Feb 15 19:25:20 2000
+++ Completion/Core/_expand     Tue Feb 15 19:26:24 2000
@@ -7,7 +7,7 @@
 # the expansions done produce no result or do not change the original
 # word from the line.
 
-local exp word="$PREFIX$SUFFIX" sort expr expl
+local exp word="$PREFIX$SUFFIX" sort expr expl subd
 local curcontext="${curcontext/:[^:]#:/:expand:}"
 
 # First, see if we should insert all *completions*.
@@ -34,17 +34,25 @@
 
 [[ -z "$exp" ]] && exp=("$word")
 
+subd="$exp"
+
 # Now try globbing.
 
 zstyle -s ":completion:${curcontext}:" glob expr &&
     [[ "${(e):-\$[$expr]}" -eq 1 ]] &&
     exp=( ${~exp}(N) )
-
+ 
 # If we don't have any expansions or only one and that is the same
 # as the original string, we let other completers run.
 
 [[ $#exp -eq 0 ||
    ( $#exp -eq 1 && "$exp[1]" = "$word"(|\(N\)) ) ]] && return 1
+
+# With subst-globs-only we bail out if there were no glob expansions,
+# regardless of any substitutions
+zstyle -s ":completion:${curcontext}:" subst-globs-only expr &&
+    [[ "${(e):-\$[$expr]}" -eq 1 ]] &&
+    [[ "$subd" = "$exp"(|\(N\)) ]] && return 1
 
 # Now add as matches whatever the user requested.


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

* Re: help with _match, globcomplete etc. (with a PATCH)
  2000-02-15 19:53 ` help with _match, globcomplete etc. (with a PATCH) Oliver Kiddle
@ 2000-02-16  6:47   ` Bart Schaefer
  2000-02-16 10:38     ` Oliver Kiddle
  0 siblings, 1 reply; 8+ messages in thread
From: Bart Schaefer @ 2000-02-16  6:47 UTC (permalink / raw)
  To: Zsh workers

On Feb 15,  7:53pm, Oliver Kiddle wrote:
} Subject: Re: help with _match, globcomplete etc. (with a PATCH)
}
} zsh -f
} bindkey -me
} unsetopt glob
} typeset -A code
} code[ai]=foo
} echo $code[ai]/{a,<tab>

All I get from that is a beep, before Sven's 9749, and

zagzig% echo foo/\{a, 
                      ^note trailing space added
with 9749 applied.

Of course that seems wrong, too.  I didn't want the brace to be quoted.

What output do you see?

} The main limiting factor on my configuration is that I can't do
} parameter expansion, command substitution and arithmetic expansion
} independantly.

Hmm.  This would almost have to be done in C code; if you have more
than one of the three forms in the same "word", it'd get pretty messy
to dismantle it to expand only the appropriate substring(s).

} Also, I can't see a way of doing globbing, while preserving variable
} references.

This strikes me as nigh impossible in the general case.  What would you
expect to see if you invoked the "glob but preserve variables" expansion
in a case like:

	zsh% arr=('[sS]' Src/M ../)
	zsh% print -l ${~^arr}*

??  Or do I completely misunderstand what you're asking?

(I note that on that example expand-word produces a list of files but
the _expand completer (with substitute and glob styles set to 1) only
beeps, with or without 9752 applied.  Is that the right behavior?)

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


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

* Re: help with _match, globcomplete etc. (with a PATCH)
  2000-02-16  6:47   ` Bart Schaefer
@ 2000-02-16 10:38     ` Oliver Kiddle
  2000-02-16 11:52       ` Bart Schaefer
  0 siblings, 1 reply; 8+ messages in thread
From: Oliver Kiddle @ 2000-02-16 10:38 UTC (permalink / raw)
  To: Zsh workers

Bart Schaefer wrote:
> 
> On Feb 15,  7:53pm, Oliver Kiddle wrote:
> } Subject: Re: help with _match, globcomplete etc. (with a PATCH)
> } echo $code[ai]/{a,<tab>
>
> All I get from that is a beep, before Sven's 9749, and
> 
> zagzig% echo foo/\{a,
>                       ^note trailing space added
> with 9749 applied.
> Of course that seems wrong, too.  I didn't want the brace to be quoted.
> What output do you see?

Firstly, I meant <Ctrl-X,*> not <tab> above, as I said in the original
post but I think you probably used that. I see:

risc8% echo foo\[/\{a, 
also with a trailing space added.

> Hmm.  This would almost have to be done in C code; if you have more
> than one of the three forms in the same "word", it'd get pretty messy
> to dismantle it to expand only the appropriate substring(s).

Thats roughly what I imagined was the case after messing with _expand
more. It would probably never work too well with the various things I
had in mind for the scripts anyway.

> } Also, I can't see a way of doing globbing, while preserving variable
> } references.
> 
> This strikes me as nigh impossible in the general case.  What would you
> expect to see if you invoked the "glob but preserve variables" expansion
> in a case like:
> 
>         zsh% arr=('[sS]' Src/M ../)
>         zsh% print -l ${~^arr}*
> 
> ??  Or do I completely misunderstand what you're asking?

You're probably right, I was thinking of simple cases where the
variables used were single values not arrays. What I meant was that the
variable reference would remain in place of its expansion in the full
glob expansion. Anyway I take your point about why it isn't all that
simple.

The reason I wanted this is basically that I use a number of associative
arrays to point to different areas of the system I work on. The path to
most of these areas are stupidly long so I don't generally want my
variable references expanded on the command-line because I end up with a
mess spanning several lines with the things I want to look back at
buried deep amoung the long path names.

Normally for referencing directories, I'd be able to use the named
directory hash table - expand-or-complete never expanded tildes. In fact
the general handling of ~something in completion is exactly what I
wanted for my assocaitive array variables. This leads me to make a
suggestion which may or may not be easy but would solve my problems: in
the same way as a variable whose value is a directory can be refered to
with ~, could the same be made to work for associative arrays (and
arrays in general I suppose), allowing me to use stuff like ~code[ai]
and ~test[$area]?

Oliver Kiddle


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

* Re: help with _match, globcomplete etc. (with a PATCH)
  2000-02-16 10:38     ` Oliver Kiddle
@ 2000-02-16 11:52       ` Bart Schaefer
  0 siblings, 0 replies; 8+ messages in thread
From: Bart Schaefer @ 2000-02-16 11:52 UTC (permalink / raw)
  To: Zsh workers

On Feb 16, 10:38am, Oliver Kiddle wrote:
} Subject: Re: help with _match, globcomplete etc. (with a PATCH)
}
} The reason I wanted this is basically that I use a number of associative
} arrays to point to different areas of the system I work on. [...]
} 
} [...] This leads me to make a suggestion which may or may not be easy
} but would solve my problems: in the same way as a variable whose
} value is a directory can be refered to with ~, could the same be made
} to work for associative arrays (and arrays in general I suppose),
} allowing me to use stuff like ~code[ai] and ~test[$area]?

Hrm.  You can already manually stuff things into the nameddir hash:

zagzig% unsetopt glob
zagzig% typeset -A xx 
zagzig% xx=($(print -l $path | cat -n))
zagzig% echo $xx[9]
/usr/etc
zagzig% for x in ${(k)xx}; hash -d xx[$x]=$xx[$x]
zagzig% cd ~xx[9]<TAB>
zagzig% cd ~xx[9]/

Well, would you look at that.  But if there I press return, I get:

cd: no such file or directory: ~xx[9]

That's because the [ and ] are tokenized most of the time, so they don't
match what has been added to the hash even if the typtab is modified to
treat them as IUSER.  It's probably an accident that the trailing slash
gets added that first time; you can't complete any further after it.

So it may be possible to make this work by using a more sophisticated
parse than simply scanning past all the IUSER characters, and then
throwing in a few calls to untokenize(); but I don't think I'm going to
attempt it.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com


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

* Re: help with _match, globcomplete etc.
@ 2000-02-15 11:34 Sven Wischnowsky
  0 siblings, 0 replies; 8+ messages in thread
From: Sven Wischnowsky @ 2000-02-15 11:34 UTC (permalink / raw)
  To: zsh-workers


Oliver Kiddle wrote:

> Sven Wischnowsky wrote:
> 
> > You can bind expand-word to ^X$.
> 
> True though I was mainly after not having variable expansion ever when I
> press tab.
> 
> Incidentally, I tried creating:
> expand-variables() {
>   setopt localoptions noglob
>   zle expand-word
> }
> zle -N expand-variables

Why use expand-word at all then? Modified from a widget that appeared
on the users list some time ago:

  expand-variables() {
    local skip="${(M)LBUFFER%%[ 	\;\|\{\}]##}"
    local word="${(M)${LBUFFER%${skip}}%%[^ 	\;\|\{\}]##}" exp

    exp=${(e)word}

    [[ -n "$exp" ]] && LBUFFER="${LBUFFER%${word}${skip}}${exp}${skip}"
  }
  zle -N expand-variables

Or some such.

> so that ^X$ wouldn't also glob complete but it comes back with any glob
> characters quoted. It seems that expand-word always does this when
> noglob is set. Surely this isn't right? In the process, I also noticed
> that the quoting can go slightly wrong if there are opened but not
> closed braces: $code[ai]/{a,b<Ctrl-X,*> comes back with a quoted '[' in
> the middle.

Maybe I'll have a look some time...

> > Have you had a look at the _expand completer? And the _expand_word
> 
> I have now but I can't get it to work. I've probably missed something
> but this won't work:
> 
> zsh -f
> autoload -U compinit
> compinit
> bindkey '^I' complete-word
> zstyle -d
> zstyle ':completion:*::::' completer _expand
> echo *<tab>
> 
> I was expecting the last line to glob expand (as if tab was still bound
> to expand-or-complete) but all I can get is a beep. I've tried changing
> the related styles without any effect.

Setting `zstyle ':completion:*:expand:*' glob 1' works for me.

It is a bit irritating that you have to switch on at least one style
to make _expand do anything...


Bye
 Sven


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


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

* Re: help with _match, globcomplete etc.
  2000-02-14 13:03 help with _match, globcomplete etc. (with PATCH) Sven Wischnowsky
@ 2000-02-15 11:09 ` Oliver Kiddle
  0 siblings, 0 replies; 8+ messages in thread
From: Oliver Kiddle @ 2000-02-15 11:09 UTC (permalink / raw)
  To: Zsh workers

Sven Wischnowsky wrote:

> You can bind expand-word to ^X$.

True though I was mainly after not having variable expansion ever when I
press tab.

Incidentally, I tried creating:
expand-variables() {
  setopt localoptions noglob
  zle expand-word
}
zle -N expand-variables

so that ^X$ wouldn't also glob complete but it comes back with any glob
characters quoted. It seems that expand-word always does this when
noglob is set. Surely this isn't right? In the process, I also noticed
that the quoting can go slightly wrong if there are opened but not
closed braces: $code[ai]/{a,b<Ctrl-X,*> comes back with a quoted '[' in
the middle.

> Have you had a look at the _expand completer? And the _expand_word

I have now but I can't get it to work. I've probably missed something
but this won't work:

zsh -f
autoload -U compinit
compinit
bindkey '^I' complete-word
zstyle -d
zstyle ':completion:*::::' completer _expand
echo *<tab>

I was expecting the last line to glob expand (as if tab was still bound
to expand-or-complete) but all I can get is a beep. I've tried changing
the related styles without any effect.

Thanks

Oliver


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

* help with _match, globcomplete etc.
  2000-02-08 14:28 Problem with completion after a variable with globcomplete Sven Wischnowsky
@ 2000-02-14 12:23 ` Oliver Kiddle
  0 siblings, 0 replies; 8+ messages in thread
From: Oliver Kiddle @ 2000-02-14 12:23 UTC (permalink / raw)
  To: zsh-workers

Sven Wischnowsky wrote:

> (there is almost no real reason to use
> GLOB_COMPLETE nowadays, the _match completer should be better -- and
> configurable).

I've just been trying to fathom out what is going on with respect to
when zsh expands, menu completes expansions and doesn't expand. It all
is a little bit complicated, especially as the behaviour is controlled
by a mix of options, styles and the choice of widget that tab is bound
to.

Part of the trouble is the way zsh's shell expansion works. If you use
expand-or-complete, I can't see any way of customising what is and isn't
expanded - variables, globs and history are, tildes are not. The
behaviour which I would like is that variables are not expanded unless I
specifically use a widget bound to '^X$' - like tcsh's expand-variables
widget. Would it be possible for the shell expansion used in completion
to be controlled by a style?

The reason I still use glob_complete is that it allows some action when
completing glob patterns (menu completion though I'd prefer expansion in
the case of files) while not expanding variables.

I can however see that the _match completer is better, mainly in that I
can choose menu completion where only one argument is required and
expansion where many are required.

Another thing which I would like to configure with expansion is when
there is only one match, I'd prefer if the space suffix was not added -
this is one of the things that makes the variable expansion annoying.
Ideally, the suffix would be as if normal completion was used so
directories would get a '/'.

Oliver Kiddle

PS. Sorry about sending the wrapped patch before - I forgot to disable
wrapping in my mailer. I can resend it if that would be useful to
anyone.


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

end of thread, other threads:[~2000-02-16 11:52 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2000-02-15 16:08 help with _match, globcomplete etc Sven Wischnowsky
2000-02-15 19:53 ` help with _match, globcomplete etc. (with a PATCH) Oliver Kiddle
2000-02-16  6:47   ` Bart Schaefer
2000-02-16 10:38     ` Oliver Kiddle
2000-02-16 11:52       ` Bart Schaefer
  -- strict thread matches above, loose matches on Subject: below --
2000-02-15 11:34 help with _match, globcomplete etc Sven Wischnowsky
2000-02-14 13:03 help with _match, globcomplete etc. (with PATCH) Sven Wischnowsky
2000-02-15 11:09 ` help with _match, globcomplete etc Oliver Kiddle
2000-02-08 14:28 Problem with completion after a variable with globcomplete Sven Wischnowsky
2000-02-14 12:23 ` help with _match, globcomplete etc 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).