Bram,
I noticed a thread in reddit¹ about how to avoid breaking the undo 
sequence in insert mode with <S-Left>. I thought <C-G>U should do it, 
but it didn't work, so I looked it up and noticed we need some special 
handling there as well.

Please see the attached patch. It adds a test as well. Furthermore it 
simplifies the function signature of the various ins_left() functions to 
not require the end_change parameter anymore. Since the variable 
dont_sync_undo is static to edit.c we can as well just determine the 
value inside the function.

¹) https://www.reddit.com/r/vim/comments/b5n6sq/custom_mapping_mix_of_cgn_and/

Best,
Christian
-- 
Man sagt leise: »ich empfehle mich Ihnen«, wenn man den Hut von weitem
zieht.
                -- Jean Paul

-- 
-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- 
You received this message because you are subscribed to the Google Groups 
"vim_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.
From 90395aac68a60d9c704ba3806ac359d5075037c3 Mon Sep 17 00:00:00 2001
From: Christian Brabandt <[email protected]>
Date: Tue, 26 Mar 2019 20:05:11 +0100
Subject: [PATCH] Make <C-G>U work with <S-Left>/<S-Right>

---
 src/edit.c                   | 32 ++++++++++++++++++++------------
 src/testdir/test_mapping.vim | 23 +++++++++++++++++++++++
 2 files changed, 43 insertions(+), 12 deletions(-)

diff --git a/src/edit.c b/src/edit.c
index b15b4b10b..b1328d9c8 100644
--- a/src/edit.c
+++ b/src/edit.c
@@ -236,11 +236,11 @@ static void ins_mousescroll(int dir);
 #if defined(FEAT_GUI_TABLINE) || defined(PROTO)
 static void ins_tabline(int c);
 #endif
-static void ins_left(int end_change);
+static void ins_left(void);
 static void ins_home(int c);
 static void ins_end(int c);
 static void ins_s_left(void);
-static void ins_right(int end_change);
+static void ins_right(void);
 static void ins_s_right(void);
 static void ins_up(int startcol);
 static void ins_pageup(void);
@@ -1284,7 +1284,7 @@ doESCkey:
 	    if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
 		ins_s_left();
 	    else
-		ins_left(dont_sync_undo == FALSE);
+		ins_left();
 	    break;
 
 	case K_S_LEFT:	/* <S-Left> */
@@ -1296,7 +1296,7 @@ doESCkey:
 	    if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
 		ins_s_right();
 	    else
-		ins_right(dont_sync_undo == FALSE);
+		ins_right();
 	    break;
 
 	case K_S_RIGHT:	/* <S-Right> */
@@ -9296,10 +9296,10 @@ ins_horscroll(void)
 #endif
 
     static void
-ins_left(
-    int	    end_change) /* end undoable change */
+ins_left()
 {
     pos_T	tpos;
+    int end_change = dont_sync_undo == FALSE; // end undoable change
 
 #ifdef FEAT_FOLDING
     if ((fdo_flags & FDO_HOR) && KeyTyped)
@@ -9383,8 +9383,9 @@ ins_end(int c)
 }
 
     static void
-ins_s_left(void)
+ins_s_left()
 {
+    int end_change = dont_sync_undo == FALSE; // end undoable change
 #ifdef FEAT_FOLDING
     if ((fdo_flags & FDO_HOR) && KeyTyped)
 	foldOpenCursor();
@@ -9392,18 +9393,21 @@ ins_s_left(void)
     undisplay_dollar();
     if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
     {
-	start_arrow(&curwin->w_cursor);
+	start_arrow_with_change(&curwin->w_cursor, end_change);
+	if (!end_change)
+	    AppendCharToRedobuff(K_S_LEFT);
 	(void)bck_word(1L, FALSE, FALSE);
 	curwin->w_set_curswant = TRUE;
     }
     else
 	vim_beep(BO_CRSR);
+    dont_sync_undo = FALSE;
 }
 
     static void
-ins_right(
-    int	    end_change) /* end undoable change */
+ins_right()
 {
+    int end_change = dont_sync_undo == FALSE; // end undoable change
 #ifdef FEAT_FOLDING
     if ((fdo_flags & FDO_HOR) && KeyTyped)
 	foldOpenCursor();
@@ -9447,8 +9451,9 @@ ins_right(
 }
 
     static void
-ins_s_right(void)
+ins_s_right()
 {
+    int end_change = dont_sync_undo == FALSE; // end undoable change
 #ifdef FEAT_FOLDING
     if ((fdo_flags & FDO_HOR) && KeyTyped)
 	foldOpenCursor();
@@ -9457,12 +9462,15 @@ ins_s_right(void)
     if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
 	    || gchar_cursor() != NUL)
     {
-	start_arrow(&curwin->w_cursor);
+	start_arrow_with_change(&curwin->w_cursor, end_change);
+	if (!end_change)
+	    AppendCharToRedobuff(K_S_RIGHT);
 	(void)fwd_word(1L, FALSE, 0);
 	curwin->w_set_curswant = TRUE;
     }
     else
 	vim_beep(BO_CRSR);
+    dont_sync_undo = FALSE;
 }
 
     static void
diff --git a/src/testdir/test_mapping.vim b/src/testdir/test_mapping.vim
index c454fc01c..20914ed0b 100644
--- a/src/testdir/test_mapping.vim
+++ b/src/testdir/test_mapping.vim
@@ -140,6 +140,29 @@ func Test_map_cursor()
   imapclear
 endfunc
 
+func Test_map_cursor_ctrl_gU()
+  " <c-g>U<cursor> works only within a single line
+  nnoremap c<* *Ncgn<C-r>"<C-G>U<S-Left>
+  call setline(1, ['foo', 'foobar', '', 'foo'])
+  call cursor(1,2)
+  call feedkeys("c<*PREFIX\<esc>.", 'xt')
+  call assert_equal(['PREFIXfoo', 'foobar', '', 'PREFIXfoo'], getline(1,'$'))
+  " break undo manually
+  set ul=1000
+  exe ":norm! uu"
+  call assert_equal(['foo', 'foobar', '', 'foo'], getline(1,'$'))
+
+  " Test that it does not work if the cursor moves to the previous line
+  " 2 times <S-Left> move to the previous line
+  nnoremap c<* *Ncgn<C-r>"<C-G>U<S-Left><C-G>U<S-Left>
+  call setline(1, ['', ' foo', 'foobar', '', 'foo'])
+  call cursor(2,3)
+  call feedkeys("c<*PREFIX\<esc>.", 'xt')
+  call assert_equal(['PREFIXPREFIX', ' foo', 'foobar', '', 'foo'], getline(1,'$'))
+  nmapclear
+endfunc
+
+
 " This isn't actually testing a mapping, but similar use of CTRL-G U as above.
 func Test_break_undo()
   :set whichwrap=<,>,[,]
-- 
2.20.1

Raspunde prin e-mail lui