zsh-users
 help / color / mirror / code / Atom feed
* non-greedy matching?
@ 2001-03-21 14:23 Adam Spiers
  2001-03-21 15:14 ` Oliver Kiddle
  2001-03-21 15:47 ` Oliver Kiddle
  0 siblings, 2 replies; 5+ messages in thread
From: Adam Spiers @ 2001-03-21 14:23 UTC (permalink / raw)
  To: zsh users mailing list

Sorry, I seem to be coming out with a lot of dumb questions recently.

In my quest to improve my zsh startup time, I noticed to my horror
that when I initially coded prompt_adam2_setup, I made it invoke perl
no less than three times.  Eliminating one of them is fairly easy;
however the other two invocations are:

  ..... | perl -pe 's/%{.*?%}//g'

They are done in order to strip control characters from a prompt so
that its display width can be determined.  At first I thought that it
would surely be easy to avoid this, but I still haven't come up with a
quick replacement, since neither zsh nor sed seem to be able to do
non-greedy matching.

Any ideas?  The only alternatives I've come up with are:

  foo=$(print -P "$prompt_string")
  bar="${foo//^[[[0-9;]##m/}"

the string and then strip out ANSI escape sequences, or maybe to use
the (^(...)) extended glob somehow, but I haven't managed to get the
latter working.

It would be nice if zsh could do non-greedy though.


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

* Re: non-greedy matching?
  2001-03-21 14:23 non-greedy matching? Adam Spiers
@ 2001-03-21 15:14 ` Oliver Kiddle
  2001-03-21 16:01   ` Bart Schaefer
  2001-03-21 15:47 ` Oliver Kiddle
  1 sibling, 1 reply; 5+ messages in thread
From: Oliver Kiddle @ 2001-03-21 15:14 UTC (permalink / raw)
  To: Adam Spiers, zsh users mailing list

--- Adam Spiers <adam@spiers.net> wrote:

> however the other two invocations are:
> 
>   ..... | perl -pe 's/%{.*?%}//g'

Does this do what you want:
... | sed 's/%{[^}]*%}//g'

It works here unless for some reason you have a closing curly bracket
in your escape sequence - assuming I've not missed something else. It
is fairly common to do [^..]* instead of .* when avoiding the effects
of greedy matching. What does the ? achieve in your perl regex?

It's a pity the col program isn't more powerful.

> They are done in order to strip control characters from a prompt so
> that its display width can be determined.  At first I thought that it
> would surely be easy to avoid this, but I still haven't come up with

I haven't looked at your prompt function but you might also consider
checking if you can get what you want with the ternary expression in
the prompt expansion as it can check how many characters have been
printed on the current line.

Oliver

____________________________________________________________
Do You Yahoo!?
Get your free @yahoo.co.uk address at http://mail.yahoo.co.uk
or your free @yahoo.ie address at http://mail.yahoo.ie


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

* Re: non-greedy matching?
  2001-03-21 14:23 non-greedy matching? Adam Spiers
  2001-03-21 15:14 ` Oliver Kiddle
@ 2001-03-21 15:47 ` Oliver Kiddle
  1 sibling, 0 replies; 5+ messages in thread
From: Oliver Kiddle @ 2001-03-21 15:47 UTC (permalink / raw)
  To: Adam Spiers, zsh users mailing list

--- Adam Spiers <adam@spiers.net> wrote: 
> Any ideas?  The only alternatives I've come up with are:
> 
>   foo=$(print -P "$prompt_string")
>   bar="${foo//^[[[0-9;]##m/}"

I should have thought about this before replying the first time but I
didn't until after Sven's e-mail. If you have extendedglob set, you can
do:
${(%%)${PS1//%\{(^*%\}*)%\}/}}
Doing the replace before the prompt expansion allows this to match the
%{...%} instead of the escape sequence. If you have any parameters
which when substituted include escape sequences, you may need to use
(e) before PS1 to expand these first. I trust this more than the sed
because it can do (^*%\}*) which matches anything which doesn't contain
%} to force the non-greedy behaviour.

Oliver

____________________________________________________________
Do You Yahoo!?
Get your free @yahoo.co.uk address at http://mail.yahoo.co.uk
or your free @yahoo.ie address at http://mail.yahoo.ie


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

* Re: non-greedy matching?
  2001-03-21 15:14 ` Oliver Kiddle
@ 2001-03-21 16:01   ` Bart Schaefer
  0 siblings, 0 replies; 5+ messages in thread
From: Bart Schaefer @ 2001-03-21 16:01 UTC (permalink / raw)
  To: zsh users mailing list

On Mar 21,  2:23pm, Adam Spiers wrote:
} Subject: non-greedy matching?
}
}   ..... | perl -pe 's/%{.*?%}//g'
} 
} They are done in order to strip control characters from a prompt so
} that its display width can be determined.

I had to do something similar in prompt_bart_setup, and ended up with
this:

	${#${(%%f)${PS1//[%]\{[^%]#%\}/}}[1]}

The (f) and [1] are because of the multi-line PS1, where it computes the
length only of the first line.  The above works as long as you don't
have other prompt escapes inside the %{ %}.  Hmm.  Perhaps this:

	${#${(%%)${prompt_line_1a//[%]\{([^%]|%[^\}])#%\}/}}}

Looking at prompt_adam2_precmd, it does exactly what prompt_bart_precmd
does: compute a padding width by subtracting from $COLUMNS.  With the
above expression you can get rid of prompt_line_1[ab]_no_color entirely:

	prompt_padding_size=$(( COLUMNS
		- ${#${(%%)${prompt_line_1a//[%]\{([^%]|%[^\}])#%\}/}}}
		- ${#${(%%)${prompt_line_1b//[%]\{([^%]|%[^\}])#%\}/}}} ))

Which also, as Sven mentioned, gets rid of $(print -P ...).

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

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


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

* Re: non-greedy matching?
@ 2001-03-21 14:57 Sven Wischnowsky
  0 siblings, 0 replies; 5+ messages in thread
From: Sven Wischnowsky @ 2001-03-21 14:57 UTC (permalink / raw)
  To: zsh-users


Adam Spiers wrote:

> ...
> 
> Any ideas? 

No (I'm still worried about what I wrote in the other mail to
-workers), but:

> The only alternatives I've come up with are:
> 
>   foo=$(print -P "$prompt_string")

No need for that extra sub-shell there, use ${(%):-$prompt_string} or
${(%%):-...} depending on what you want...


Bye
 Sven


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


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

end of thread, other threads:[~2001-03-21 16:03 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-21 14:23 non-greedy matching? Adam Spiers
2001-03-21 15:14 ` Oliver Kiddle
2001-03-21 16:01   ` Bart Schaefer
2001-03-21 15:47 ` Oliver Kiddle
2001-03-21 14:57 Sven Wischnowsky

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