zsh-workers
 help / color / mirror / code / Atom feed
* hist_strip_spaces for 3.0.3?
@ 1997-02-03  6:17 Bart Schaefer
  1997-02-03  7:38 ` Wayne Davison
  0 siblings, 1 reply; 2+ messages in thread
From: Bart Schaefer @ 1997-02-03  6:17 UTC (permalink / raw)
  To: zsh-workers

Has anyone redone the hist_strip_spaces patch against 3.0.3-test4?  I liked
the way it modifies hist_ignore_dups to save only the most recent of the
duplicate entries (rather than saving only the oldest) because it makes the
timestamps from `history -d` so much more sensible.

The history code has been shuffled around enough that histcmp() from the
original patch no longer works, and I don't know when I'll have time to
re-learn the history mechanism well enough to fix it.

-- 
Bart Schaefer                             Brass Lantern Enterprises
http://www.well.com/user/barts            http://www.nbn.com/people/lantern


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: hist_strip_spaces for 3.0.3?
  1997-02-03  6:17 hist_strip_spaces for 3.0.3? Bart Schaefer
@ 1997-02-03  7:38 ` Wayne Davison
  0 siblings, 0 replies; 2+ messages in thread
From: Wayne Davison @ 1997-02-03  7:38 UTC (permalink / raw)
  To: schaefer; +Cc: zsh-workers

"Bart Schaefer" writes:
> Has anyone redone the hist_strip_spaces patch against 3.0.3-test4?

The following diff appears to work fine for me (though it goes by the
name hist_reduce_blanks these days).

..wayne..
---8<------8<------8<------8<---cut here--->8------>8------>8------>8---
Index: Src/globals.h
@@ -731,6 +731,7 @@
     {"histignoredups", 		'h',  0,    0},
     {"histignorespace", 	'g',  0,    0},
     {"histnostore", 		0,    0,    0},
+    {"histreduceblanks",	0,    0,    0},
     {"histverify", 		0,    0,    0},
     {"hup", 			0,    0,    OPT_EMULATE|OPT_ZSH},
     {"ignorebraces", 		'I',  0,    OPT_EMULATE|OPT_SH},
Index: Src/hist.c
@@ -604,6 +604,53 @@
 	histactive = HA_NOINC;
 }
 
+/* compare current line with history entry using only text in words */
+
+/**/
+int
+histcmp(Histent he)
+{
+    int kword, lword;
+    int nwords = chwordpos/2;
+
+    if (nwords != he->nwords)
+	return 1;
+
+    for (kword = 0; kword < 2*nwords; kword += 2)
+	if ((lword = chwords[kword+1]-chwords[kword])
+	    != he->words[kword+1]-he->words[kword] ||
+	    memcmp(he->text+he->words[kword], chline+chwords[kword], lword))
+	    return 1;
+
+    return 0;
+}
+
+/**/
+void
+histreduceblanks(void)
+{
+    int i, len, pos, needblank;
+
+    for (i = 0, len = 0; i < chwordpos; i += 2) {
+	len += chwords[i+1] - chwords[i]
+	     + (i > 0 && chwords[i] > chwords[i-1]);
+    }
+    if (chline[len] == '\0')
+	return;
+
+    for (i = 0, pos = 0; i < chwordpos; i += 2) {
+	len = chwords[i+1] - chwords[i];
+	needblank = (i < chwordpos-2 && chwords[i+2] > chwords[i+1]);
+	if (pos != chwords[i]) {
+	    memcpy(chline + pos, chline + chwords[i], len + needblank);
+	    chwords[i] = pos;
+	    chwords[i+1] = chwords[i] + len;
+	}
+	pos += len + needblank;
+    }
+    chline[pos] = '\0';
+}
+
 /* say we're done using the history mechanism */
 
 /**/
@@ -611,7 +658,6 @@
 hend(void)
 {
     int flag, save = 1;
-    Histent he;
 
     DPUTS(!chline, "BUG: chline is NULL in hend()");
     if (histactive & (HA_NOSTORE|HA_NOINC)) {
@@ -634,9 +680,7 @@
 		*--hptr = '\0';
 	    } else
 		save = 0;
-	he = gethistent(curhist - 1);
 	if (!*chline || !strcmp(chline, "\n") ||
-	    (isset(HISTIGNOREDUPS) && he->text && !strcmp(he->text, chline)) ||
 	    (isset(HISTIGNORESPACE) && spaceflag))
 	    save = 0;
     }
@@ -658,15 +702,7 @@
 	    zsfree(ptr);
     }
     if (save) {
-	Histent curhistent = gethistent(curhist);
-	zsfree(curhistent->text);
-	if (curhistent->nwords)
-	    zfree(curhistent->words, curhistent->nwords*2*sizeof(short));
-
-	curhistent->text = ztrdup(chline);
-	curhistent->stim = time(NULL);
-	curhistent->ftim = 0L;
-	curhistent->flags = 0;
+	Histent he;
 #ifdef DEBUG
 	/* debugging only */
 	if (chwordpos%2) {
@@ -677,11 +713,37 @@
 	/* get rid of pesky \n which we've already nulled out */
 	if (!chline[chwords[chwordpos-2]])
 	    chwordpos -= 2;
-	if ((curhistent->nwords = chwordpos/2)) {
-	    curhistent->words =
-		(short *)zalloc(curhistent->nwords*2*sizeof(short));
-	    memcpy(curhistent->words, chwords,
-		   curhistent->nwords*2*sizeof(short));
+	/* strip superfluous blanks, if desired */
+	if (isset(HISTREDUCEBLANKS))
+	    histreduceblanks();
+	if (isset(HISTIGNOREDUPS) && (he = gethistent(curhist - 1))
+	 && he->text && histcmp(he) == 0) {
+	    /* Don't duplicate history entry, but use the current rather than
+	     * the previous one, in case minor changes were made to it.
+	     */
+	    zsfree(he->text);
+	    he->text = ztrdup(chline);
+	    if (chwordpos)
+		memcpy(he->words, chwords, chwordpos * sizeof(short));
+	    he->stim = time(NULL);	/* set start time */
+	    he->ftim = 0;
+	    curhist--;
+	}
+	else {
+	    Histent curhistent = gethistent(curhist);
+	    zsfree(curhistent->text);
+	    if (curhistent->nwords)
+		zfree(curhistent->words, curhistent->nwords*2*sizeof(short));
+
+	    curhistent->text = ztrdup(chline);
+	    curhistent->stim = time(NULL);
+	    curhistent->ftim = 0L;
+	    curhistent->flags = 0;
+
+	    if ((curhistent->nwords = chwordpos/2)) {
+		curhistent->words = (short *)zalloc(chwordpos * sizeof(short));
+		memcpy(curhistent->words, chwords, chwordpos * sizeof(short));
+	    }
 	}
     } else
 	curhist--;
Index: Src/zsh.h
@@ -1097,6 +1097,7 @@
     HISTIGNOREDUPS,
     HISTIGNORESPACE,
     HISTNOSTORE,
+    HISTREDUCEBLANKS,
     HISTVERIFY,
     HUP,
     IGNOREBRACES,
---8<------8<------8<------8<---cut here--->8------>8------>8------>8---


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1997-02-03  7:53 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-02-03  6:17 hist_strip_spaces for 3.0.3? Bart Schaefer
1997-02-03  7:38 ` Wayne Davison

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).