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

hist_strip_spaces (was Re: histignoredups done properly)



Peter Stephenson writes:
> This fixes histignoredups so that only lines which are really
> different are stored; insignificant changes in whitespace are not
> treated as differences.

Nice change.  I was working on something similar over the weekend:  the
removal of insignificant spaces from history lines.  Since I think my
change complements your patch, I've based this diff on previously
applying your histignoredups patch.

Since it may be that not everyone will want this, I made it depend on a
new option, HIST_STRIP_SPACES.  I haven't modified the documentation yet,
however.

Note that since the histstrip() routine may change curhistent->text, I
had to change the cleanup code that frees chline.  The new code has the
additional benefit of being much clearer in what it is trying to do (it
took me a while to figure out what the old code was for).

..wayne..
---8<------8<------8<------8<---cut here--->8------>8------>8------>8---
Index: Src/globals.h
@@ -720,6 +720,7 @@
     {"histignoredups", 		'h',  0,    0},
     {"histignorespace", 	'g',  0,    0},
     {"histnostore", 		0,    0,    0},
+    {"histstripspaces",		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
@@ -648,6 +648,36 @@
     return 0;
 }
 
+/**/
+void
+histstrip(Histent he)
+{
+    int i, len;
+    int limit = he->nwords*2;
+    char *str, *s;
+
+    for (i = 0, len = he->nwords-1; i < limit; i += 2)
+	len += he->words[i+1] - he->words[i];
+    if (len == he->words[limit-1])
+	return;
+
+    str = zalloc(len+1);
+
+    for (i = 0, s = str; i < limit; i += 2) {
+	len = he->words[i+1] - he->words[i];
+	memcpy(s, he->text + he->words[i], len);
+	he->words[i] = s - str;
+	he->words[i+1] = he->words[i] + len;
+	s += len;
+	*s++ = ' ';
+    }
+    s[-1] = '\0';
+
+    if (he->text != chline)
+	zsfree(he->text);
+    he->text = str;
+}
+
 /* say we're done using the history mechanism */
 
 /**/
@@ -694,7 +724,7 @@
 		chwordpos -= 2;
 	    he = gethistent(curhist - 1);
 	    if (isset(HISTIGNOREDUPS) && he->text && !histcmp(he))
-		save = 2;
+		save = isset(HISTSTRIPSPACES)? 0 : 2;
 	}
     }
     if (flag & (HISTFLAG_DONE | HISTFLAG_RECALL)) {
@@ -737,16 +767,13 @@
 	    memcpy(curhistent->words, chwords,
 		   curhistent->nwords*2*sizeof(short));
 	}
+	if (isset(HISTSTRIPSPACES))
+	    histstrip(curhistent);
     } else
 	remhist();
-    if (chline && !curhistent->text)
-	zfree(chline, hlinesz);
-    if (curhistent->text) {
-	char *s = ztrdup(curhistent->text);
-
-	zfree(curhistent->text, hlinesz);
-	curhistent->text = s;
-    }
+    if (curhistent->text == chline)
+	curhistent->text = ztrdup(chline);
+    zfree(chline, hlinesz);
     zfree(chwords, chwordlen*sizeof(short));
     chline = NULL;
     return !(flag & HISTFLAG_NOEXEC || errflag);
Index: Src/zsh.h
@@ -1102,6 +1102,7 @@
     HISTIGNOREDUPS,
     HISTIGNORESPACE,
     HISTNOSTORE,
+    HISTSTRIPSPACES,
     HISTVERIFY,
     HUP,
     IGNOREBRACES,
---8<------8<------8<------8<---cut here--->8------>8------>8------>8---



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