zsh-users
 help / color / mirror / code / Atom feed
* Re: matlab-history-beginning-search-backward
@ 2009-10-03 23:36 Guido van Steen
  2009-10-03 23:52 ` matlab-history-beginning-search-backward Guido van Steen
  0 siblings, 1 reply; 7+ messages in thread
From: Guido van Steen @ 2009-10-03 23:36 UTC (permalink / raw)
  To: zsh-users, Peter Stephenson

[-- Attachment #1: Type: text/plain, Size: 2279 bytes --]

Thank you Peter, 

I amended the widget slightly. With this version the searched pattern is shown again when you search-forward beyond the first match. As far as I can tell that is how history works in Matlab and Fish. 

Attached for users who also like this behavior. 

Best wishes, 

Guido 

--- On Fri, 10/2/09, Peter Stephenson <pws@csr.com> wrote:

> From: Peter Stephenson <pws@csr.com>
> Subject: Re: matlab-history-beginning-search-backward
> To: zsh-users@sunsite.dk
> Date: Friday, October 2, 2009, 10:48 AM
> Guido van Steen wrote:
> > * Which part of the code makes sure that the commands
> retrieved
> > from history are "shown" on the command line?
> 
> Assigning to HISTNO moves to the given line number in the
> history.  zsh
> remembers edits to history lines until you hit
> <return> on the line you
> actually want to execute; the next time you look at the
> history it's
> back the way it was.
> 
> > * Why does the cursor position matter, and how can one
> make sure
> > that the cursor moves to the right position before the
> widget is
> > invoked?
> 
> As far as the widget is concerned, the cursor position only
> matters at
> the start in order to find the string to the left which is
> used for the
> search.  If you execute the widget again (forward or
> backward) without
> any other command in between it will use the same search
> string (no
> matter where the cursor is).  If you move the cursor
> (or in fact execute
> any other command) and execute the widget again it will
> look again at
> the string to the left.
> 
> Some people like the cursor placed specially after the
> execution of
> history commands; this function doesn't have any support
> for that, so
> it's always left at the end.
> 
> -- 
> Peter Stephenson <pws@csr.com> 
>           Software Engineer
> Tel: +44 (0)1223 692070         
>          Cambridge Silicon
> Radio Limited
> Churchill House, Cambridge Business Park, Cowley Road,
> Cambridge, CB4 0WZ, UK
> 
> 
> Member of the CSR plc group of companies. CSR plc
> registered in England and Wales, registered number 4187346,
> registered office Churchill House, Cambridge Business Park,
> Cowley Road, Cambridge, CB4 0WZ, United Kingdom
>


      

[-- Attachment #2: history-substring-search-backward --]
[-- Type: application/octet-stream, Size: 1205 bytes --]

#!/usr/bin/env zsh 
# make sure vim that vim sees this file as a file with zsh syntax 

# history-substring-search-backward
# can also be used for the widget history-substring-search-forward.

emulate -L zsh
setopt extendedglob

zmodload -i zsh/parameter

if [[ $LASTWIDGET = history-substring-search-* ]]; then
	# here's one I prepared earlier
	search=$ZLE_HISTORY_SUBSTRING_MATCH
else
	# We need to quote metacharacters in the search string
	# since they are otherwise active in the reverse subscript.
	# We need to avoid quoting other characters since they aren't
	# and just stay quoted, rather annoyingly.
	search=${LBUFFER//(#m)[\][()\\*?#<>~^]/\\$MATCH}
	ZLE_HISTORY_SUBSTRING_MATCH=$search
fi

local -aU matches

matches=(${(kon)history[(R)*${search}*]})

# Filter out any match that's the same as the original.
# Note this isn't a pattern this time.

if [[ $WIDGET = *forward* ]]; then # arrow-down 
	eval "matches=(\${matches:#<-$HISTNO>})"
	if [[ ${#matches} == 0 ]]; then 
	    HISTNO=$HISTCMD
	else 
		HISTNO=${matches[1]}
	fi 
else # $WIDGET = *backward* arrow-up
	eval "matches=(\${matches:#<$HISTNO->})"
	if [[ ${#matches} == 0 ]]; then 
		return 1
	else 
		HISTNO=${matches[-1]}
	fi
fi



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

* Re: matlab-history-beginning-search-backward
  2009-10-03 23:36 matlab-history-beginning-search-backward Guido van Steen
@ 2009-10-03 23:52 ` Guido van Steen
  0 siblings, 0 replies; 7+ messages in thread
From: Guido van Steen @ 2009-10-03 23:52 UTC (permalink / raw)
  To: zsh-users

I checked it one more time and I am totally wrong: Peter's orginal widget yields Fish's behavior. Anyway, I just like the attached widget a bit more. 


      


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

* Re: matlab-history-beginning-search-backward
  2009-10-01 20:58     ` matlab-history-beginning-search-backward Guido van Steen
@ 2009-10-02  8:48       ` Peter Stephenson
  0 siblings, 0 replies; 7+ messages in thread
From: Peter Stephenson @ 2009-10-02  8:48 UTC (permalink / raw)
  To: zsh-users

Guido van Steen wrote:
> * Which part of the code makes sure that the commands retrieved
> from history are "shown" on the command line?

Assigning to HISTNO moves to the given line number in the history.  zsh
remembers edits to history lines until you hit <return> on the line you
actually want to execute; the next time you look at the history it's
back the way it was.

> * Why does the cursor position matter, and how can one make sure
> that the cursor moves to the right position before the widget is
> invoked?

As far as the widget is concerned, the cursor position only matters at
the start in order to find the string to the left which is used for the
search.  If you execute the widget again (forward or backward) without
any other command in between it will use the same search string (no
matter where the cursor is).  If you move the cursor (or in fact execute
any other command) and execute the widget again it will look again at
the string to the left.

Some people like the cursor placed specially after the execution of
history commands; this function doesn't have any support for that, so
it's always left at the end.

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


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom


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

* Re: matlab-history-beginning-search-backward
  2009-10-01  8:38   ` matlab-history-beginning-search-backward Peter Stephenson
@ 2009-10-01 20:58     ` Guido van Steen
  2009-10-02  8:48       ` matlab-history-beginning-search-backward Peter Stephenson
  0 siblings, 1 reply; 7+ messages in thread
From: Guido van Steen @ 2009-10-01 20:58 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

Dear Peter, 

I am impressed by the code. It works well - apart from the 
cursor stuff. I tried to understand it, but I failed. 

I have two questions: 

* Which part of the code makes sure that the commands retrieved 
from history are "shown" on the command line? 

* Why does the cursor position matter, and how can one make sure 
that the cursor moves to the right position before the widget is 
invoked? 

Best wishes, 

Guido 

--- On Thu, 10/1/09, Peter Stephenson <pws@csr.com> wrote:

> From: Peter Stephenson <pws@csr.com>
> Subject: Re: matlab-history-beginning-search-backward
> To: zsh-users@sunsite.dk
> Date: Thursday, October 1, 2009, 10:38 AM
> Peter Stephenson wrote:
> > I stole a lot of it from
> > history-beginning-search-menu.
> 
> ... and I've just noticed because of that a couple of bits
> are nonsense ...
> 
> > local search=$LBUFFER MATCH MBEGIN MEND
> 
> ... this assignment is redundant ...
> 
> > # Filter out any match that's the same as the
> original.
> > # Note this isn't a pattern this time.
> > matches=(${matches:#${LBUFFER}})
> 
> ... more importantly, because in this case $matches is an
> array of
> numbers this doesn't do anything (except stop you from
> matching a
> history line whose number happens to match the string to
> the left of the
> cursor).  This filter would need to be moved up to
> where the matching is
> originally done.  It's minor, though.
> 
> -- 
> Peter Stephenson <pws@csr.com> 
>           Software Engineer
> Tel: +44 (0)1223 692070         
>          Cambridge Silicon
> Radio Limited
> Churchill House, Cambridge Business Park, Cowley Road,
> Cambridge, CB4 0WZ, UK
> 
> 
> 
> 
> Member of the CSR plc group of companies. CSR plc
> registered in England and Wales, registered number 4187346,
> registered office Churchill House, Cambridge Business Park,
> Cowley Road, Cambridge, CB4 0WZ, United Kingdom
> 





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

* Re: matlab-history-beginning-search-backward
  2009-09-30 19:53 ` matlab-history-beginning-search-backward Peter Stephenson
  2009-09-30 22:38   ` matlab-history-beginning-search-backward Guido van Steen
@ 2009-10-01  8:38   ` Peter Stephenson
  2009-10-01 20:58     ` matlab-history-beginning-search-backward Guido van Steen
  1 sibling, 1 reply; 7+ messages in thread
From: Peter Stephenson @ 2009-10-01  8:38 UTC (permalink / raw)
  To: zsh-users

Peter Stephenson wrote:
> I stole a lot of it from
> history-beginning-search-menu.

... and I've just noticed because of that a couple of bits are nonsense ...

> local search=$LBUFFER MATCH MBEGIN MEND

... this assignment is redundant ...

> # Filter out any match that's the same as the original.
> # Note this isn't a pattern this time.
> matches=(${matches:#${LBUFFER}})

... more importantly, because in this case $matches is an array of
numbers this doesn't do anything (except stop you from matching a
history line whose number happens to match the string to the left of the
cursor).  This filter would need to be moved up to where the matching is
originally done.  It's minor, though.

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




Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom


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

* Re: matlab-history-beginning-search-backward
  2009-09-30 19:53 ` matlab-history-beginning-search-backward Peter Stephenson
@ 2009-09-30 22:38   ` Guido van Steen
  2009-10-01  8:38   ` matlab-history-beginning-search-backward Peter Stephenson
  1 sibling, 0 replies; 7+ messages in thread
From: Guido van Steen @ 2009-09-30 22:38 UTC (permalink / raw)
  To: Peter Stephenson; +Cc: zsh-users

Hi Peter, 

Awesome! This works exactly like what I had in mind. As an exercise for myself I may try to add some cursor handling. 

Thank you very, very much!!! 

Guido 

--- On Wed, 9/30/09, Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:

> From: Peter Stephenson <p.w.stephenson@ntlworld.com>
> Subject: Re: matlab-history-beginning-search-backward
> To: zsh-users@sunsite.dk
> Date: Wednesday, September 30, 2009, 9:53 PM
> On Wed, 30 Sep 2009 09:52:10 -0700
> (PDT)
> Guido van Steen <gvsteen@yahoo.com>
> wrote:
> > I use the "history-beginning-search-backward" widget
> quite
> > frequently. It searches for commands in the command
> history that START
> > WITH "LBUFFER". 
> >
> > I like this behaviour. It is simple and it keeps my
> attention fixed on
> > one line. What I don't like about the widget is that
> ONLY searches for
> > commands that start with "LBUFFER". Instead I would
> prefer a similar
> > widget that searches for commands in the command
> history, in such a way
> > that "LBUFFER" is a SUBSTRING of these commands.
> 
> (I moved this to zsh-users.)
> 
> This seemed to work.  I stole a lot of it from
> history-beginning-search-menu.  It implements forward
> and backward
> searches, so you can do something like
> 
> zle -N history-substring-search-forward
> history-substring-search-backward
> zle -N history-substring-search-backward
> bindkey '\ep' history-substring-search-backward
> bindkey '\en' history-substring-search-forward
> 
> At this point, it is traditional for people to say it's
> absolutely
> crucial that the cursor be posititioned etc. etc...
> 
> pws
> 
> 
> # history-substring-search-backward
> # can also be used for the widget
> history-substring-search-forward.
> 
> emulate -L zsh
> setopt extendedglob
> 
> zmodload -i zsh/parameter
> 
> local search=$LBUFFER MATCH MBEGIN MEND
> 
> typeset -g ZLE_HISTORY_SUBSTRING_MATCH
> 
> if [[ $LASTWIDGET = history-substring-search-* ]]; then
>   # here's one I prepared earlier
>   search=$ZLE_HISTORY_SUBSTRING_MATCH
> else
>   # We need to quote metacharacters in the search
> string
>   # since they are otherwise active in the reverse
> subscript.
>   # We need to avoid quoting other characters since
> they aren't
>   # and just stay quoted, rather annoyingly.
>  
> search=${LBUFFER//(#m)[\][()\\*?#<>~^]/\\$MATCH}
>   ZLE_HISTORY_SUBSTRING_MATCH=$search
> fi
> 
> local -aU matches
> matches=(${(kon)history[(R)*${search}*]})
> 
> # Filter out any match that's the same as the original.
> # Note this isn't a pattern this time.
> matches=(${matches:#${LBUFFER}})
> 
> (( ${#matches} )) || return 1
> 
> if [[ $WIDGET = *forward* ]]; then
>   eval "matches=(\${matches:#<-$HISTNO>})"
>   (( ${#matches} )) || return 1
>   HISTNO=${matches[1]}
> else
>   eval "matches=(\${matches:#<$HISTNO->})"
>   (( ${#matches} )) || return 1
>   HISTNO=${matches[-1]}
> fi
> 


     


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

* Re: matlab-history-beginning-search-backward
       [not found] <140222.25175.qm@web65614.mail.ac4.yahoo.com>
@ 2009-09-30 19:53 ` Peter Stephenson
  2009-09-30 22:38   ` matlab-history-beginning-search-backward Guido van Steen
  2009-10-01  8:38   ` matlab-history-beginning-search-backward Peter Stephenson
  0 siblings, 2 replies; 7+ messages in thread
From: Peter Stephenson @ 2009-09-30 19:53 UTC (permalink / raw)
  To: zsh-users

On Wed, 30 Sep 2009 09:52:10 -0700 (PDT)
Guido van Steen <gvsteen@yahoo.com> wrote:
> I use the "history-beginning-search-backward" widget quite
> frequently. It searches for commands in the command history that START
> WITH "LBUFFER". 
>
> I like this behaviour. It is simple and it keeps my attention fixed on
> one line. What I don't like about the widget is that ONLY searches for
> commands that start with "LBUFFER". Instead I would prefer a similar
> widget that searches for commands in the command history, in such a way
> that "LBUFFER" is a SUBSTRING of these commands.

(I moved this to zsh-users.)

This seemed to work.  I stole a lot of it from
history-beginning-search-menu.  It implements forward and backward
searches, so you can do something like

zle -N history-substring-search-forward history-substring-search-backward
zle -N history-substring-search-backward
bindkey '\ep' history-substring-search-backward
bindkey '\en' history-substring-search-forward

At this point, it is traditional for people to say it's absolutely
crucial that the cursor be posititioned etc. etc...

pws


# history-substring-search-backward
# can also be used for the widget history-substring-search-forward.

emulate -L zsh
setopt extendedglob

zmodload -i zsh/parameter

local search=$LBUFFER MATCH MBEGIN MEND

typeset -g ZLE_HISTORY_SUBSTRING_MATCH

if [[ $LASTWIDGET = history-substring-search-* ]]; then
  # here's one I prepared earlier
  search=$ZLE_HISTORY_SUBSTRING_MATCH
else
  # We need to quote metacharacters in the search string
  # since they are otherwise active in the reverse subscript.
  # We need to avoid quoting other characters since they aren't
  # and just stay quoted, rather annoyingly.
  search=${LBUFFER//(#m)[\][()\\*?#<>~^]/\\$MATCH}
  ZLE_HISTORY_SUBSTRING_MATCH=$search
fi

local -aU matches
matches=(${(kon)history[(R)*${search}*]})

# Filter out any match that's the same as the original.
# Note this isn't a pattern this time.
matches=(${matches:#${LBUFFER}})

(( ${#matches} )) || return 1

if [[ $WIDGET = *forward* ]]; then
  eval "matches=(\${matches:#<-$HISTNO>})"
  (( ${#matches} )) || return 1
  HISTNO=${matches[1]}
else
  eval "matches=(\${matches:#<$HISTNO->})"
  (( ${#matches} )) || return 1
  HISTNO=${matches[-1]}
fi


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

end of thread, other threads:[~2009-10-03 23:53 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-10-03 23:36 matlab-history-beginning-search-backward Guido van Steen
2009-10-03 23:52 ` matlab-history-beginning-search-backward Guido van Steen
     [not found] <140222.25175.qm@web65614.mail.ac4.yahoo.com>
2009-09-30 19:53 ` matlab-history-beginning-search-backward Peter Stephenson
2009-09-30 22:38   ` matlab-history-beginning-search-backward Guido van Steen
2009-10-01  8:38   ` matlab-history-beginning-search-backward Peter Stephenson
2009-10-01 20:58     ` matlab-history-beginning-search-backward Guido van Steen
2009-10-02  8:48       ` matlab-history-beginning-search-backward 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).