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

Re: PATCH: narrow-to-region (was Re: This widget implementation feels a bit clunky)



Bart wrote:
> } It'd also be good for the function to use the new undo limit features.
> 
> I was going to send a follow-up message about undo-ing and whether a call
> to split-undo is needed before (and/or after) the recursive-edit.

It is needed immediately after you've setup PREDISPLAY and emptied
BUFFER.

I would have been fairly sure of that answer but my own attempts to make
it work had failed so I went back to see again first. I now realise that
the problem I had is elsewhere. I've attached a very rudimentary patch to
narrow-to-region.

My main use of narrow-to-region is to retrieve history lines into the
middle of the current line. And it is just this situation that is
breaking it. When doing:
  zle undo $_ntr_changeno
it is falling foul of the first line of unapplychange which is:
  if(ch->hist != histline) {
and not undoing enough changes.

This is a consequence of the fix in 10328. If I comment out the two
lines of undo() which do else break; it appears to work fine and the
problem that 10328 targetted doesn't seem to come back. But it is hard
to know how that was failing at the time.

> Related, should -S / -R implicitly set undo points?

I've never used those two options and am not sure I quite understand
what they're for. At least not without digging in the archives. With the
patch below, it is fairly obviously broken for them because
_ntr_changeno is set in a different block from the one which does zle
undo to revert to it.

Oliver

diff --git a/Functions/Zle/narrow-to-region b/Functions/Zle/narrow-to-region
index 293f89b..ae4addd 100644
--- a/Functions/Zle/narrow-to-region
+++ b/Functions/Zle/narrow-to-region
@@ -29,8 +29,9 @@
 emulate -L zsh
 setopt extendedglob
 
-local _ntr_lbuf_return _ntr_rbuf_return
+local _ntr_new_lbuf _ntr_newrbuf _ntr_lbuf_return _ntr_rbuf_return
 local _ntr_predisplay=$PREDISPLAY _ntr_postdisplay=$POSTDISPLAY
+integer _ntr_savelim=$UNDO_LIMIT_NO _ntr_changeno
 integer _ntr_start _ntr_end _ntr_swap _ntr_cursor=$CURSOR _ntr_mark=$MARK
 integer _ntr_stat
 
@@ -98,6 +99,7 @@ if [[ -n $_ntr_save || -z $_ntr_restore ]]; then
   then
     _ntr_posttext=$_ntr_rbuffer
   fi
+  _ntr_changeno=$UNDO_CHANGE_NO
   PREDISPLAY="$_ntr_predisplay$_ntr_pretext"
   POSTDISPLAY="$_ntr_posttext$_ntr_postdisplay"
 
@@ -110,16 +112,25 @@ if [[ -n $_ntr_save || -z $_ntr_restore ]]; then
   BUFFER=${BUFFER[_ntr_start+1,_ntr_end-1]}
   CURSOR=$_ntr_cursor
   MARK=$_ntr_mark
+  zle split-undo
 fi
 
 if [[ -z $_ntr_save && -z $_ntr_restore ]]; then
-  zle recursive-edit
-  _ntr_stat=$?
-
-  [[ -n $_ntr_lbuf_return ]] &&
-    builtin typeset -g ${_ntr_lbuf_return}="${LBUFFER}"
-  [[ -n $_ntr_rbuf_return ]] &&
-    builtin typeset -g ${_ntr_rbuf_return}="${RBUFFER}"
+  {
+    UNDO_LIMIT_NO=$UNDO_CHANGE_NO
+    zle recursive-edit
+    _ntr_stat=$?
+    _ntr_newlbuf="$LBUFFER"
+    _ntr_newrbuf="$RBUFFER"
+
+    [[ -n $_ntr_lbuf_return ]] &&
+      builtin typeset -g ${_ntr_lbuf_return}="${LBUFFER}"
+    [[ -n $_ntr_rbuf_return ]] &&
+      builtin typeset -g ${_ntr_rbuf_return}="${RBUFFER}"
+  } always {
+    zle undo $_ntr_changeno
+    UNDO_LIMIT_NO=_ntr_savelim
+  }
 fi
 
 if [[ -n $_ntr_restore || -z $_ntr_save ]]; then
@@ -135,8 +146,8 @@ if [[ -n $_ntr_restore || -z $_ntr_save ]]; then
 
   PREDISPLAY=$_ntr_predisplay
   POSTDISPLAY=$_ntr_postdisplay
-  LBUFFER="$_ntr_lbuffer$BUFFER"
-  RBUFFER="$_ntr_rbuffer"
+  LBUFFER="$_ntr_lbuffer$_ntr_newlbuf"
+  RBUFFER="$_ntr_newrbuf$_ntr_rbuffer"
   MARK=${#_ntr_lbuffer}
 fi
 



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