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

Re: is there a mix of history-search-backward and history-beginning-search-backward



On Sep 8,  5:27pm, Peter Stephenson wrote:
> Subject: Re: is there a mix of history-search-backward and history-beginni
>
> I might put these in the Functions/Zle directory (after Bart has found out
> whatever's wrong with them).

I don't see anything wrong with them except that there doesn't seem to be
any use of the "stat" local paramter.  With a tad more cleverness, though,
you can get away with only one function:

function history-search-end {
    integer ocursor=$CURSOR

    if [[ $LASTWIDGET = history-beginning-search-*-end ]]; then
      # Last widget called set $hbs_pos.
      CURSOR=$hbs_pos
    else
      hbs_pos=$CURSOR
    fi

    if zle .${WIDGET%-end}; then
      # success, go to end of line
      zle .end-of-line
    else
      # failure, restore position
      CURSOR=$ocursor
      return 1
    fi
}
zle -N history-beginning-search-backward-end history-search-end
zle -N history-beginning-search-forward-end history-search-end

While we're on the subject, I've been fooling with insert-and-predict.  The
version below seems to work pretty well, but the history-search-forward in
delete-backward-and-predict is less than ideal because of zsh's habit of
leaving edits behind in the history list until the next trashzle().  That
means that if you do some editing and then continue searching backwards,
when you search forward again you may find an edited history line, which
is probably not what you want.  Using ((-CURSOR)) rather than actually
deleting the character minimizes this effect, but assignment to LBUFFER in
either function can leave junk behind.  Any clever fixes for this?

I think this is now clean enough that you could actually run with predict
turned on most of the time.

predict-on() {
    zle -N self-insert insert-and-predict
    zle -N magic-space insert-and-predict
    zle -N backward-delete-char delete-backward-and-predict
}
predict-off() {
    zle -A .self-insert self-insert
    zle -A .magic-space magic-space
    zle -A .backward-delete-char backward-delete-char
}
insert-and-predict () {
  emulate -L zsh
  if [[ ${RBUFFER[1]} = ${KEYS[-1]} ]]
  then
    # same as what's typed, just move on
    ((++CURSOR))
  else
    LBUFFER="$LBUFFER$KEYS"
    if [[ $LASTWIDGET == (self-insert|magic-space|backward-delete-char) ]]
    then
      zle .history-beginning-search-backward || RBUFFER=""
    fi
  fi
  return 0
}
delete-backward-and-predict() {
  emulate -L zsh
  if [[ -n "$LBUFFER" ]]
  then
    # If the last widget was e.g. a motion, then probably the intent is
    # to actually edit the line, not change the search prefix.
    if [[ $LASTWIDGET == (self-insert|magic-space|backward-delete-char) ]]
    then
      ((--CURSOR))
      zle .history-beginning-search-forward || RBUFFER=""
      return 0
    else
      # Depending on preference, you might call "predict-off" here,
      # and also set up forward deletions to turn off prediction.
      LBUFFER="$LBUFFER[1,-2]"
    fi
  fi
}
zle -N insert-and-predict
zle -N predict-on
zle -N predict-off


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



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