zsh-users
 help / color / mirror / code / Atom feed
From: "Bart Schaefer" <schaefer@candle.brasslantern.com>
To: zsh-users@sunsite.auc.dk
Subject: Re: is there a mix of history-search-backward and history-beginning-search-backward
Date: Sat, 11 Sep 1999 22:58:31 +0000	[thread overview]
Message-ID: <990911225831.ZM18771@candle.brasslantern.com> (raw)
In-Reply-To: <199909081527.RAA19661@paris.ifh.de>

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


  parent reply	other threads:[~1999-09-11 22:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-09-03 11:04 Andy Spiegl
1999-09-08 13:02 ` Andy Spiegl
1999-09-08 15:27   ` Peter Stephenson
1999-09-08 15:55     ` Andy Spiegl
1999-09-08 16:18       ` Oliver Kiddle
1999-09-08 16:58       ` unsubscribe tbabin@nortelnetworks.com Timothy Babin
1999-09-11 22:58     ` Bart Schaefer [this message]
1999-09-08 15:55   ` is there a mix of history-search-backward and history-beginning-search-backward Bart Schaefer

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=990911225831.ZM18771@candle.brasslantern.com \
    --to=schaefer@candle.brasslantern.com \
    --cc=zsh-users@sunsite.auc.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).