zsh-users
 help / color / mirror / code / Atom feed
* Variable expansion problem
@ 2007-09-25  7:39 John Cooper
  2007-09-25  9:01 ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: John Cooper @ 2007-09-25  7:39 UTC (permalink / raw)
  To: zsh-users; +Cc: John Cooper

I'd like to have a mechanism such that I can type $FOO<TAB> and have it expand to the following which I can then manually edit further:
"C:\Program Files (x86)\Citrix\Web Interface\5.0.0\sitemgr.exe" -c "WIDest=1:/Citrix/AccessPlatform,Config=Local,XMLService=beanstalk:80"

I've tried:
PF="C:\Program Files (x86)"
FOO="$PF\Citrix\Web Interface\5.0.0\sitemgr.exe -c \"WIDest=1:/Citrix/AccessPlatform,Config=Local,XMLService=beanstalk:80\""

.. but this expands the value into a single escaped string:
$ C:\\Program\ Files\ \(x86\)\\Citrix\\Web\ Interface\\5.0.0\\sitemgr.exe\ -c\ \"WIDest=1:/Citrix/AccessPlatform,Config=Local,XMLService=beanstalk:80\"

If I try `setopt shwordsplit' (which I don't really want), the spaces in the expanded path are no longer quoted causing the cmd to fail:
C:\\Program Files \(x86\)\\Citrix\\Web Interface\\5.0.0\\sitemgr.exe -c \"WIDest=1:/Citrix/AccessPlatform,Config=Local,XMLService=beanstalk:80\" 
zsh: command not found: C:\Program

I also don't want the double-quotes to be quoted in the expansion.


Any ideas how to do this?

Thanks,

    --- John 


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

* Re: Variable expansion problem
  2007-09-25  7:39 Variable expansion problem John Cooper
@ 2007-09-25  9:01 ` Peter Stephenson
  2007-09-25 11:08   ` John Cooper
  0 siblings, 1 reply; 4+ messages in thread
From: Peter Stephenson @ 2007-09-25  9:01 UTC (permalink / raw)
  To: zsh-users

"John Cooper" wrote:
> I'd like to have a mechanism such that I can type $FOO<TAB> and have
> it expand to the following which I can then manually edit further:

So you need a literal expansion mechanism for parameters; the usual
mechanism does all forms of expansion and quotes the result.  Of course
there are other possible ways of achieving the effect, such as aliases
and _expand_alias (after the recent fix), but this answers the question
directly.

Rather than fiddle with the _expand completer, which is already trying
too hard to be all things to all users, it's probably better to have a
different one (call it _expand_literal_param, or something):


#autoload

# Expand a parameter literally (no quotation).  Only activates if the entire
# word matches a parameter name.

local val="$IPREFIX$PREFIX$SUFFIX$ISUFFIX" expl
[[ $val = \$[[:IDENT:]]## ]] || return 1

val=$val[2,-1]
(( ${(P)+val} )) || return 1

_wanted parameters expl "literal parameter expansion" compadd -UQ "${(P)val}"


Note I've deliberately limited this so it only kicks in if the entire
word matches $<paramname>.  Put that in a function and put
_expand_literal_param in your list of completers immediately before the
existing _expand.  My list became:

zstyle ':completion:*' completer _oldlist _expand_literal_param _expand \
  _complete _ignored _approximate

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


.


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

* RE: Variable expansion problem
  2007-09-25  9:01 ` Peter Stephenson
@ 2007-09-25 11:08   ` John Cooper
  2007-09-25 11:29     ` Peter Stephenson
  0 siblings, 1 reply; 4+ messages in thread
From: John Cooper @ 2007-09-25 11:08 UTC (permalink / raw)
  To: Peter Stephenson, zsh-users; +Cc: John Cooper

Thanks for the reply, although it doesn't seem to be working for me. I did the following:

1. Put your code snippet into a file named _expand_literal_param in a directory on my FPATH.
2. Added _expand_literal_param to my list of completers (which doesn't have an entry for _expand); my list is now: zstyle ':completion:*' completer _expand_literal_param _complete _correct _approximate
3. Set the following variable: IDENT="$PF\Citrix\Web Interface\5.0.0\sitemgr.exe -arg foo"
4. Typed $IDENT<TAB>, which expanded to:
C:\\Program\ Files\ \(x86\)\\Citrix\\Web\ Interface\\5.0.0\\sitemgr.exe\ -arg\ foo

(the space between the 2 args is still quoted.). I suspect I'm doing something wrong!

I don't really understand the code, and I'm not sure what you mean by "only kicks in if the entire word matches $<paramname>.", as there is no <paramname> in the code - did you mean it kicks in when the entire param name matches "$IDENT"? Btw, what is the "P" referring to in ${(P)+val}?


I managed to get the effect working by defining the following alias (backslashes were awkward so I used `cygpath' to convert to unix-style paths):
  PFu=$(cygpath -u $PF)
  alias sss="\\\"$PFu/Citrix/Web Interface/5.0.0/sitemgr.exe\\\" -arg foo"

It seemed unwieldy to invoke it by typing the following (I use Emacs key mappings)
  sssMeta-X _expa<TAB>a<TAB><RET>

.. so I've added _expand_alias to my list of completers (just before _complete), so sss now expands as I'd like when I type sss<TAB>. This is OK but now if I type d<TAB> it expands my alias for "d" rather than completing all commands starting with "d".

I think what I really want is to be able to type
  Prompt> $SITEMGR -c <TAB>
.. and have it complete to:
  Prompt> $SITEMGR -c "WIDest=1:/Citrix/AccessPlatform,Config=Local,XMLService=beanstalk:80"

    --- John. 

-----Original Message-----
From: Peter Stephenson [mailto:pws@csr.com] 
Sent: 25 September 2007 10:01
To: zsh-users@sunsite.dk
Subject: Re: Variable expansion problem

"John Cooper" wrote:
> I'd like to have a mechanism such that I can type $FOO<TAB> and have
> it expand to the following which I can then manually edit further:

So you need a literal expansion mechanism for parameters; the usual
mechanism does all forms of expansion and quotes the result.  Of course
there are other possible ways of achieving the effect, such as aliases
and _expand_alias (after the recent fix), but this answers the question
directly.

Rather than fiddle with the _expand completer, which is already trying
too hard to be all things to all users, it's probably better to have a
different one (call it _expand_literal_param, or something):


#autoload

# Expand a parameter literally (no quotation).  Only activates if the entire
# word matches a parameter name.

local val="$IPREFIX$PREFIX$SUFFIX$ISUFFIX" expl
[[ $val = \$[[:IDENT:]]## ]] || return 1

val=$val[2,-1]
(( ${(P)+val} )) || return 1

_wanted parameters expl "literal parameter expansion" compadd -UQ "${(P)val}"


Note I've deliberately limited this so it only kicks in if the entire
word matches $<paramname>.  Put that in a function and put
_expand_literal_param in your list of completers immediately before the
existing _expand.  My list became:

zstyle ':completion:*' completer _oldlist _expand_literal_param _expand \
  _complete _ignored _approximate

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


.


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

* Re: Variable expansion problem
  2007-09-25 11:08   ` John Cooper
@ 2007-09-25 11:29     ` Peter Stephenson
  0 siblings, 0 replies; 4+ messages in thread
From: Peter Stephenson @ 2007-09-25 11:29 UTC (permalink / raw)
  To: zsh-users; +Cc: John Cooper

On Tue, 25 Sep 2007 12:08:20 +0100
"John Cooper" <john.cooper@eu.citrix.com> wrote:
> Thanks for the reply, although it doesn't seem to be working for me.
> (the space between the 2 args is still quoted.). I suspect I'm doing
> something wrong!

Is <TAB> still bound to expand-or-complete?  (If you don't have _expand in
your completers it probably is.)  In that case, rebind it:

bindkey '\t' complete-word

You might want to add that _expand to your completers to restore expansion
to (almost) how it worked before.

Alternatively, you could bind the new function (defined as a completion
widget) to a different key

> I don't really understand the code, and I'm not sure what you mean by
> "only kicks in if the entire word matches $<paramname>.", as there is no
> <paramname> in the code - did you mean it kicks in when the entire param
> name matches "$IDENT"?

<paramname> just meant any parameter name.

> Btw, what is the "P" referring to in ${(P)+val}? 

That causes $val to be treated as the name of another parameter.

> I think what I really want is to be able to type
>   Prompt> $SITEMGR -c <TAB>
> .. and have it complete to:
>   Prompt> $SITEMGR -c ...

You'd need to define a completion for the expansion of $SITEMGR;
a completion for the basename of the command would be enough.

-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


.


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

end of thread, other threads:[~2007-09-25 11:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2007-09-25  7:39 Variable expansion problem John Cooper
2007-09-25  9:01 ` Peter Stephenson
2007-09-25 11:08   ` John Cooper
2007-09-25 11:29     ` Peter Stephenson

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