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

Patch to add HIST_REDUCE_BLANKS



Here's a more modern version of my patch to remove superfluous blanks
from the history lines (and never any significant blanks, like those
in quotes).  The previous version used the option HIST_STRIP_SPACES,
but I thought that the name HIST_REDUCE_BLANKS would be better so
that what is used in this patch.  This is relative to zsh-3.1.0-test3.

..wayne..
---8<------8<------8<------8<---cut here--->8------>8------>8------>8---
Index: Src/globals.h
@@ -740,6 +740,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
@@ -636,6 +636,32 @@
     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 */
 
 /**/
@@ -706,6 +732,9 @@
     curhistent->ftim = 0L;
     curhistent->flags = 0;
     if (save) {
+	/* strip superfluous blanks, if desired */
+	if (isset(HISTREDUCEBLANKS))
+	    histreduceblanks();
 	if (save == 2) {
 	    /* Don't duplicate history entry, but use the current rather than
 	     * the previous one, in case minor changes were made to it.
Index: Src/zsh.h
@@ -1107,6 +1107,7 @@
     HISTIGNOREDUPS,
     HISTIGNORESPACE,
     HISTNOSTORE,
+    HISTREDUCEBLANKS,
     HISTVERIFY,
     HUP,
     IGNOREBRACES,
---8<------8<------8<------8<---cut here--->8------>8------>8------>8---



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