From 4d07253a485819b3a9fd923d263e722ea2109c12 Mon Sep 17 00:00:00 2001
From: Bram Moolenaar <Bram@vim.org>
Date: Thu, 25 Nov 2021 19:31:15 +0000
Subject: [PATCH] patch 8.2.3677: after a put the '] mark is on the last byte

Problem:    After a put the '] mark is on the last byte of a multi-byte
            character.
Solution:   Move it to the first byte. (closes #9047)
---
 src/register.c           | 18 +++++++++++++++---
 src/testdir/test_put.vim | 13 +++++++++++++
 2 files changed, 28 insertions(+), 3 deletions(-)

diff --git a/src/register.c b/src/register.c
index d5eb011..49f4079 100644
--- a/src/register.c
+++ b/src/register.c
@@ -1479,6 +1479,7 @@ do_put(
     long	cnt;
     pos_T	orig_start = curbuf->b_op_start;
     pos_T	orig_end = curbuf->b_op_end;
+    int		first_byte_off = 0;
 
 #ifdef FEAT_CLIPBOARD
     // Adjust register name for "unnamed" in 'clipboard'.
@@ -1936,6 +1937,10 @@ do_put(
 		    }
 		    STRMOVE(ptr, oldp + col);
 		    ml_replace(lnum, newp, FALSE);
+
+		    // compute the byte offset for the last character
+		    first_byte_off = mb_head_off(newp, ptr - 1);
+
 		    // Place cursor on last putted char.
 		    if (lnum == curwin->w_cursor.lnum)
 		    {
@@ -1951,10 +1956,15 @@ do_put(
 	    if (VIsual_active) // reset lnum to the last visual line
 		lnum--;
 
+	    // put '] at the first byte of the last character
 	    curbuf->b_op_end = curwin->w_cursor;
+	    curbuf->b_op_end.col -= first_byte_off;
+
 	    // For "CTRL-O p" in Insert mode, put cursor after last char
 	    if (totlen && (restart_edit != 0 || (flags & PUT_CURSEND)))
 		++curwin->w_cursor.col;
+	    else
+		curwin->w_cursor.col -= first_byte_off;
 	    changed_bytes(lnum, col);
 	}
 	else
@@ -2061,12 +2071,14 @@ error:
 		changed_lines(curbuf->b_op_start.lnum, 0,
 					   curbuf->b_op_start.lnum, nr_lines);
 
-	    // put '] mark at last inserted character
+	    // Put the '] mark on the first byte of the last inserted character.
+	    // Correct the length for change in indent.
 	    curbuf->b_op_end.lnum = lnum;
-	    // correct length for change in indent
 	    col = (colnr_T)STRLEN(y_array[y_size - 1]) - lendiff;
 	    if (col > 1)
-		curbuf->b_op_end.col = col - 1;
+		curbuf->b_op_end.col = col - 1
+				- mb_head_off(y_array[y_size - 1],
+						y_array[y_size - 1] + col - 1);
 	    else
 		curbuf->b_op_end.col = 0;
 
diff --git a/src/testdir/test_put.vim b/src/testdir/test_put.vim
index 42bb7e6..07f6387 100644
--- a/src/testdir/test_put.vim
+++ b/src/testdir/test_put.vim
@@ -130,3 +130,16 @@ func Test_very_larg_count()
   bwipe!
 endfunc
 
+func Test_multibyte_op_end_mark()
+  new
+  call setline(1, 'тест')
+  normal viwdp
+  call assert_equal([0, 1, 7, 0], getpos("'>"))
+  call assert_equal([0, 1, 7, 0], getpos("']"))
+
+  normal Vyp
+  call assert_equal([0, 1, 2147483647, 0], getpos("'>"))
+  call assert_equal([0, 2, 7, 0], getpos("']"))
+  bwipe!
+endfunc
+
-- 
1.8.3.1

