zsh-users
 help / color / mirror / code / Atom feed
* I wonder if this can be improved a bit
@ 2001-12-31 19:24 Matt Armstrong
  2002-01-02 16:24 ` Oliver Kiddle
  0 siblings, 1 reply; 2+ messages in thread
From: Matt Armstrong @ 2001-12-31 19:24 UTC (permalink / raw)
  To: zsh-users

I'm implementing a completion system for Perforce's
(<URL:http://www.perforce.com)>) p4 command.  Like CVS, the p4 command
takes sub-commands (p4 add, p4 submit, etc.).

The p4 command prints out the list of available sub-commands with
"p4 -s help commands".  I have this bit of code that'll eventually go
into my _p4 completion system.  But I wonder if it can be improved.

    # This requires EXTENDED_GLOB to be set
    setopt extendedglob
    local a
    a=( ${(f)"$(p4 -s help commands 2>&1)"} )
    a=( ${(M)a:#info: 	*} )
    a=( ${a/(#b)info: 	#([a-z0-9]##) */$match[1]} )
    typeset -A _p4_cmds
    _p4_cmds=()
    for i in $a; do
        _p4_cmds[$a]=""
    done

So now _p4_cmds is an associative array of available sub commands.
Here I have annotated questions about this code.

    # This requires EXTENDED_GLOB to be set
    setopt extendedglob

Can I depend on the completion widgets setting EXTENDED_GLOB for me?

    local a
    a=( ${(f)"$(p4 -s help commands 2>&1)"} )

I will eventually change this to the more correct
"$(_call_program commands p4 -s help commands) ".

    a=( ${(M)a:#info: 	*} )

                     ^^^
The whitespace there is a space followed by a tab character.  Is there
any more readable way to include a tab character?  A \t escape sequence?

    a=( ${a/(#b)info: 	#([a-z0-9]##) */$match[1]} )
    typeset -A _p4_cmds
    for i in $a; do
        _p4_cmds[$a]=""
    done

Is there a simple way to turn an array (a b c) into (a "" b "" c "")?
Then I could assign _p4_cmds in one statement and dump the loop.

-- 
matt


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

* Re: I wonder if this can be improved a bit
  2001-12-31 19:24 I wonder if this can be improved a bit Matt Armstrong
@ 2002-01-02 16:24 ` Oliver Kiddle
  0 siblings, 0 replies; 2+ messages in thread
From: Oliver Kiddle @ 2002-01-02 16:24 UTC (permalink / raw)
  To: Matt Armstrong; +Cc: zsh-users

Matt Armstrong wrote:
> 
> I'm implementing a completion system for Perforce's

>     a=( ${(f)"$(p4 -s help commands 2>&1)"} )
>     a=( ${(M)a:#info:   *} )
>     a=( ${a/(#b)info:   #([a-z0-9]##) */$match[1]} )

you can nest those substitutions if you want, not that it makes
much difference.

>     typeset -A _p4_cmds
>     _p4_cmds=()
>     for i in $a; do
>         _p4_cmds[$a]=""

should that not be _p4_cmds[$i]=""

> So now _p4_cmds is an associative array of available sub commands.
> Here I have annotated questions about this code.
> 
>     # This requires EXTENDED_GLOB to be set
>     setopt extendedglob
> 
> Can I depend on the completion widgets setting EXTENDED_GLOB for me?

Yes. The _comp_options array contains the list of options set for
completion.

> 
>     local a
>     a=( ${(f)"$(p4 -s help commands 2>&1)"} )
> 
> I will eventually change this to the more correct
> "$(_call_program commands p4 -s help commands) ".

I'd use $words[1] instead of p4 so that it will work if p4 is not in
the path but is specified with its location.

>     a=( ${(M)a:#info:   *} )
> 
>                      ^^^
> The whitespace there is a space followed by a tab character.  Is there
> any more readable way to include a tab character?  A \t escape sequence?

$'\t'

In some circumstances, you may also be able to make use of [[:blank:]].

> Is there a simple way to turn an array (a b c) into (a "" b "" c "")?
> Then I could assign _p4_cmds in one statement and dump the loop.

See my -workers post on this question.

Oliver

This email has been scanned for all viruses by the MessageLabs SkyScan service. For more information on a pro-active anti-virus service working around the clock, around the globe visit http://www.messagelabs.com/


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

end of thread, other threads:[~2002-01-02 16:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-12-31 19:24 I wonder if this can be improved a bit Matt Armstrong
2002-01-02 16: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).