zsh-users
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-users@sunsite.dk
Subject: Re: doing regex on history
Date: Thu, 8 Jul 2004 00:40:40 -0700 (PDT)	[thread overview]
Message-ID: <Pine.LNX.4.60.0407072350390.13643@toltec.zanshin.com> (raw)
In-Reply-To: <1089253050.30333.5.camel@tp>

On Wed, 8 Jul 2004, damon wrote:

> I am after help to enable/use? regex on the history (using vi)

By "using vi" I assume you mean that you're using ZLE in vi emulation
mode, and that the vi-history-search-{forward,backward} widgets are
therefore of interest.

There's no direct support for regular expressions in history search,
in vi emulation or otherwise.  ZLE's vi emulation can be though of as
operating as if ":se nomagic" is always in effect.

If you have e.g. the PCRE library available and have built the zsh/pcre
module, you can write your own widgets that do the searching.  This gets
pretty tricky as there is no way to override the searching part of the
builtin vi-history-* widgets without also overriding the read-the-pattern
part, but fortunately there's a read-from-minibuffer function supplied in
the zsh distribution.  So you might do:

 autoload -U read-from-minibuffer

 vi-history-re-search-backward() {
   zmodload -i zsh/pcre || return 1
   local REPLY event
   read-from-minibuffer "?"
   [[ -z $REPLY ]] && return 0
   history -n -r 0 | while read event; do
     if [[ $event -pcre-match $REPLY ]]; then
       BUFFER=$event
       return 0
     fi
   done
   return 1
 }

If you don't have pcre, you can settle for glob patterns, which (if you
setopt EXTENDED_GLOB) can be regex-equivalent but do not use compatible
syntax.  In that case it's even easier:

 vi-history-glob-search-backward() {
   local event
   read-from-minibuffer '?'
   [[ -z $REPLY ]] && return 0
   if history -n -r -m \*$REPLY\* 0 | read event; then
     BUFFER=$event
     return 0
   fi
   return 1
 }

Left as excercises are to use the zsh/parameter module's $history hash 
instead of forking off "history -r 0" (and, can you find a bug in my 
example that would fixed by using the hash?), and then extending that to 
remember where you last found a match and to begin again from there the 
next time.

Oh, and for more fun, try doing it with an incremental search, which needs
a loop around "read-from-minibuffer -k 1" with a call to "zle -R", plus 
some special handling for backspace, return, etc.


      reply	other threads:[~2004-07-08  7:41 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-07-08  2:17 damon
2004-07-08  7:40 ` Bart Schaefer [this message]

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=Pine.LNX.4.60.0407072350390.13643@toltec.zanshin.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-users@sunsite.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).