zsh-users
 help / color / mirror / code / Atom feed
* User-defined replacement for *-line-or-search in 3.1.4
       [not found]         ` <980609114223.ZM6613@candle.brasslantern.com>
@ 1998-06-09 20:23           ` Bart Schaefer
  1998-06-10  4:25             ` Bart Schaefer
  0 siblings, 1 reply; 2+ messages in thread
From: Bart Schaefer @ 1998-06-09 20:23 UTC (permalink / raw)
  To: zsh-users

My first real attempt at a zle widget.  It fudges some things, but it works
better than the corresponding builtins most of the time (which is too bad).

Hey, zsh-workers, read the comments, particularly about assignment to CURSOR.
Shouldn't $#LBUFFER == $CURSOR == $#moveorsearchpattern on first call to this
function?  If so, why is the +1 necessary?  (If I leave it out, the call to
history-beginning-search-*ward uses too short a prefix.)

--- 8< --- snip --- 8< ---

function uplineorsearch () {
    if [[ -n "$LBUFFER" ]]
    then
	# Check whether we need to restart the search.  This is crude, as
	# it assumes no history search begins with space (histignorespace)
	# and that a matching prefix of LBUFFER means repeat the search.
	if [[ -n "$RBUFFER" || "$LBUFFER" != ${moveorsearchpattern:-" "}* ]]
	then
	    # Set the search pattern to the first word or prefix.
	    moveorsearchpattern=(${=LBUFFER})
	    moveorsearchpattern=${moveorsearchpattern[1]}
	fi
	# The next test attempts to determine whether the search will
	# succeed or fail, so as not to move the cursor if it fails.
	# This may not be worth the effort ...
	if [[ -n "$(fc -lnm $moveorsearchpattern\* -99999 -1)" ]]
	then
	    # Ideally here, this would modify the buffer to insert the
	    # search pattern, then search, and restore the buffer in
	    # the event of failure.  Failure can't be detected, so do
	    # less-intrusive but less-accurate cursor-fiddling tricks.
	    # Why is +1 needed here, but not needed at $#BUFFER?
	    CURSOR=$[$#moveorsearchpattern+1]
	    zle history-beginning-search-backward
	    CURSOR=$#BUFFER
	else
	    zle history-search-backward		# Just to get a feep
	fi
    else
	unset moveorsearchpattern
	zle up-line-or-history
    fi
}

function downlineorsearch () {
    # See comments in uplineorsearch.
    if [[ -n "$LBUFFER" ]]
    then
	if [[ -n "$RBUFFER" || "$LBUFFER" != ${moveorsearchpattern:-" "}* ]]
	then
	    moveorsearchpattern=(${=LBUFFER})
	    moveorsearchpattern=${moveorsearchpattern[1]}
	fi
	if [[ -n "$(fc -lnm $moveorsearchpattern\* -99999 -1)" ]]
	then
	    CURSOR=$[$#moveorsearchpattern+1]
	    zle history-beginning-search-forward
	    CURSOR=$#BUFFER
	else
	    zle history-search-forward		# Just to get a feep
	fi
    else
	unset moveorsearchpattern
	zle down-line-or-history
    fi
}

zle -N up-line-or-search uplineorsearch
zle -N down-line-or-search downlineorsearch

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


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

* Re: User-defined replacement for *-line-or-search in 3.1.4
  1998-06-09 20:23           ` User-defined replacement for *-line-or-search in 3.1.4 Bart Schaefer
@ 1998-06-10  4:25             ` Bart Schaefer
  0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 1998-06-10  4:25 UTC (permalink / raw)
  To: zsh-users

On Jun 9,  1:23pm, I wrote:
} Subject: User-defined replacement for *-line-or-search in 3.1.4
}
} Shouldn't $#LBUFFER == $CURSOR == $#moveorsearchpattern on first call to this
} function?  If so, why is the +1 necessary?  (If I leave it out, the call to
} history-beginning-search-*ward uses too short a prefix.)
} 
} --- 8< --- snip --- 8< ---
} 
} 	    # Why is +1 needed here, but not needed at $#BUFFER?
} 	    CURSOR=$[$#moveorsearchpattern+1]
} 	    zle history-beginning-search-backward
} 	    CURSOR=$#BUFFER

I've just been fooling with this in the debugger, and I can no longer get
the behavior I was seeing where adding one to the pattern length was
necessary.  I don't know what strangeness I had going on, though I have
done a rebuild in the interim ... anyway, I recommend changing the first
assignment of CURSOR to

	CURSOR=$#moveorsearchpattern

in both of the functions I posted earlier today.

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


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

end of thread, other threads:[~1998-06-10  4:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <19980428143325.59341@gmp-fores1.uk.jpmorgan.com>
     [not found] ` <199804281408.PAA32237@taos.demon.co.uk>
     [not found]   ` <19980609141304.41665@gmp-fores1.uk.jpmorgan.com>
     [not found]     ` <980609082238.ZM5717@candle.brasslantern.com>
     [not found]       ` <vbu35utvzk.fsf@snake.isode.com>
     [not found]         ` <980609114223.ZM6613@candle.brasslantern.com>
1998-06-09 20:23           ` User-defined replacement for *-line-or-search in 3.1.4 Bart Schaefer
1998-06-10  4:25             ` Bart Schaefer

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