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

Re: smart-insert-last-word bug in zsh 5.0.7



On Jun 13, 11:23am, Bart Schaefer wrote:
}
} After further testing, the change number doesn't quite work because it's
} not updated soon enough -- the value saved at the end is never the same
} as the new value on re-entry.  Also, executing "zle undo" doesn't alter
} UNDO_CHANGE_NO.  So probably some test of both is necessary.

Alas, this is still not sufficient.  The ugly bit is here:

} +if (( _ilw_cursor != CURSOR ))
} +then
} +    _ilw_cursor=$CURSOR
} +
} +    # UNDO_CHANGE_NO increments after we return, but why by two?
} +    _ilw_changeno=$((UNDO_CHANGE_NO+2))
} +fi

If two consecutive last words have the same number of characters,
_ilw_cursor == CURSOR and then _ilw_changeno is not updated.  But if
_ilw_changeno is updated unconditionally, then it becomes wrong if
UNDO_CHANGE_NO is not incremented, which might happen if two
consecutive last words are the same string (a replacement of a string
by itself is not treated as an undo-able change).

The answer seems to be to force an undo point with split-undo.


diff --git a/Functions/Zle/smart-insert-last-word b/Functions/Zle/smart-insert-last-word
index 27b0849..67adea7 100644
--- a/Functions/Zle/smart-insert-last-word
+++ b/Functions/Zle/smart-insert-last-word
@@ -47,13 +47,15 @@ setopt extendedglob nohistignoredups
 zle auto-suffix-retain
 
 # Not strictly necessary:
-# (($+_ilw_hist)) || integer -g _ilw_hist _ilw_count _ilw_cursor _ilw_lcursor
+# (($+_ilw_hist)) || integer -g _ilw_hist _ilw_count _ilw_cursor _ilw_lcursor _ilw_changeno
 
 integer cursor=$CURSOR lcursor=$CURSOR
 local lastcmd pattern numeric=$NUMERIC
 
 # Save state for repeated calls
-if (( HISTNO == _ilw_hist && cursor == _ilw_cursor )); then
+if (( HISTNO == _ilw_hist && cursor == _ilw_cursor &&
+      UNDO_CHANGE_NO == _ilw_changeno ))
+then
     NUMERIC=$[_ilw_count+1]
     lcursor=$_ilw_lcursor
 else
@@ -61,7 +63,8 @@ else
     _ilw_lcursor=$lcursor
 fi
 # Handle the up to three arguments of .insert-last-word
-if (( $+1 )); then
+if (( $+1 ))
+then
     if (( $+3 )); then
 	((NUMERIC = -($1)))
     else
@@ -117,3 +120,6 @@ fi
 
 LBUFFER[lcursor+1,cursor+1]=$lastcmd[-NUMERIC]
 _ilw_cursor=$CURSOR
+
+# This is necessary to update UNDO_CHANGE_NO immediately
+zle split-undo && _ilw_changeno=$UNDO_CHANGE_NO



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