zsh-workers
 help / color / mirror / code / Atom feed
From: Wayne Davison <wayne@clari.net>
To: "Bart Schaefer" <schaefer@brasslantern.com>
Cc: zsh-workers@math.gatech.edu
Subject: PATCH: even better history-search-{for,back}ward for 3.1.4
Date: Wed, 10 Jun 1998 01:51:44 -0700	[thread overview]
Message-ID: <199806100851.BAA05622@bebop.clari.net> (raw)
In-Reply-To: schaefer's message of Tue, 09 Jun 1998 20:58:02 -0700. <980609205803.ZM8774@candle.brasslantern.com>

"Bart Schaefer" writes:
> Unfortunately, this doesn't work right.

Yeah, that code didn't save enough state.  I've appended a new
patch that does a better job.

> You can do a little better if you also save `hl' and reset the
> search if `histline' differs from the last time through, but
> that's still not quite right.

Cool -- I had the same idea, plus I also decided to save cs and the
search prefix, which allows the code to do a much better job at
figuring out when to start a new search.  It is still possible to
fool it into continuing the last search when the old code would have
started a new one, but it does behave much more like the code in
3.0.5 now.

A known deficiency is that if you use uplineorsearch and pass
through a multi-line history entry, the code will start a new
search, which may not be what you want (if it was searching for
a prefix and not a command name).  This should be easy enough
to fix, though.

> zsh% ech<M-p>
> zsh% echo this is a test<C-p>
> zsh% fc -R historytest<M-p>

I find it humorous that my old code did the right thing in this case
but for the wrong reason.  The new code will still search for "fc ",
but because it properly decided to start a new search.

This patch assumes that you've removed the previous one.

..wayne..

---8<------8<------8<------8<---cut here--->8------>8------>8------>8---
Index: zle_hist.c
@@ -369,55 +369,69 @@
 	feep();
 }
 
+static int histpos, srch_hl, srch_cs = -1;
+static char *srch_str;
+
 /**/
 void
 historysearchbackward(void)
 {
-    int histpos, histmpos, hl = histline;
+    int hl = histline;
     char *s;
 
-    for (histpos = histmpos = 0; histpos < ll && !iblank(line[histpos]);
-	histpos++, histmpos++)
-	if(imeta(line[histpos]))
-	    histmpos++;
+    if (hl == curhist || hl != srch_hl || cs != srch_cs || mark != 0
+     || memcmp(srch_str, line, histpos) != 0) {
+	zfree(srch_str, histpos);
+	for (histpos = 0; histpos < ll && !iblank(line[histpos]); histpos++) ;
+	if (histpos < ll)
+	    histpos++;
+	srch_str = zalloc(histpos);
+	memcpy(srch_str, line, histpos);
+    }
     for (;;) {
 	hl--;
 	if (!(s = zle_get_event(hl))) {
 	    feep();
 	    return;
 	}
-	if (metadiffer(s, (char *) line, histpos) < 0 &&
-	    iblank(s[histmpos] == Meta ? s[histmpos+1]^32 : s[histmpos]) &&
-	    metadiffer(s, (char *) line, ll))
+	if (metadiffer(s, srch_str, histpos) < 0 &&
+	    metadiffer(s, srch_str, ll))
 	    break;
     }
     zle_goto_hist(hl);
+    srch_hl = hl;
+    srch_cs = cs;
 }
 
 /**/
 void
 historysearchforward(void)
 {
-    int histpos, histmpos, hl = histline;
+    int hl = histline;
     char *s;
 
-    for (histpos = histmpos = 0; histpos < ll && !iblank(line[histpos]);
-	histpos++, histmpos++)
-	if(imeta(line[histpos]))
-	    histmpos++;
+    if (hl == curhist || hl != srch_hl || cs != srch_cs || mark != 0
+     || memcmp(srch_str, line, histpos) != 0) {
+	zfree(srch_str, histpos);
+	for (histpos = 0; histpos < ll && !iblank(line[histpos]); histpos++) ;
+	if (histpos < ll)
+	    histpos++;
+	srch_str = zalloc(histpos);
+	memcpy(srch_str, line, histpos);
+    }
     for (;;) {
 	hl++;
 	if (!(s = zle_get_event(hl))) {
 	    feep();
 	    return;
 	}
-	if (metadiffer(s, (char *) line, histpos) < (histline == curhist) &&
-	    (!s[histmpos] ||
-	     iblank(s[histmpos] == Meta ? s[histmpos+1]^32 : s[histmpos])) &&
-	    metadiffer(s, (char *) line, ll))
+	if (metadiffer(s, srch_str, histpos) < (hl == curhist) &&
+	    metadiffer(s, srch_str, ll))
 	    break;
     }
     zle_goto_hist(hl);
+    srch_hl = hl;
+    srch_cs = cs;
 }
 
 /**/
---8<------8<------8<------8<---cut here--->8------>8------>8------>8---


  reply	other threads:[~1998-06-10  8:57 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1998-06-09 22:42 PATCH: revamped " Wayne Davison
1998-06-10  3:58 ` Bart Schaefer
1998-06-10  8:51   ` Wayne Davison [this message]
1998-06-10  9:23     ` PATCH: even better " Bart Schaefer
1998-06-10  9:33       ` Wayne Davison
1998-06-13  4:06     ` Bart Schaefer
1998-06-15 16:49       ` Wayne Davison

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=199806100851.BAA05622@bebop.clari.net \
    --to=wayne@clari.net \
    --cc=schaefer@brasslantern.com \
    --cc=zsh-workers@math.gatech.edu \
    /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).