Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

User-defined replacement for *-line-or-search in 3.1.4



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



Messages sorted by: Reverse Date, Date, Thread, Author