patch 9.1.0945: ComplMatchIns highlight doesn't end after inserted text

Problem:  ComplMatchIns highlight doesn't end after inserted text.
Solution: Handle ComplMatchIns highlight more like search highlight.
          Fix off-by-one error. Handle deleting text properly.
          (zeertzjq)

closes: #16244

Signed-off-by: zeertzjq <zeertzjq@outlook.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/drawline.c b/src/drawline.c
index 18857ee..bc8e4d2 100644
--- a/src/drawline.c
+++ b/src/drawline.c
@@ -1172,7 +1172,8 @@
     int		vi_attr = 0;		// attributes for Visual and incsearch
 					// highlighting
     int		area_attr = 0;		// attributes desired by highlighting
-    int		search_attr = 0;	// attributes desired by 'hlsearch'
+    int		search_attr = 0;	// attributes desired by 'hlsearch' or
+					// ComplMatchIns
 #ifdef FEAT_SYN_HL
     int		vcol_save_attr = 0;	// saved attr for 'cursorcolumn'
     int		syntax_attr = 0;	// attributes desired by syntax
@@ -1213,7 +1214,7 @@
     int		word_end = 0;		// last byte with same spell_attr
     int		cur_checked_col = 0;	// checked column for current line
 #endif
-    int		extra_check = 0;	// has extra highlighting
+    int		extra_check = FALSE;	// has extra highlighting
     int		multi_attr = 0;		// attributes desired by multibyte
     int		mb_l = 1;		// multi-byte byte length
     int		mb_c = 0;		// decoded multi-byte character
@@ -1868,6 +1869,9 @@
     }
 #endif
 
+    if ((State & MODE_INSERT) && in_curline && ins_compl_active())
+	area_highlighting = TRUE;
+
 #ifdef FEAT_SYN_HL
     // Cursor line highlighting for 'cursorline' in the current window.
     if (wp->w_p_cul && lnum == wp->w_cursor.lnum)
@@ -2389,9 +2393,9 @@
 			|| (noinvcur && (colnr_T)wlv.vcol == wp->w_virtcol)))
 		*area_attr_p = 0;		// stop highlighting
 
-#ifdef FEAT_SEARCH_EXTRA
 	    if (wlv.n_extra == 0)
 	    {
+#ifdef FEAT_SEARCH_EXTRA
 		// Check for start/end of 'hlsearch' and other matches.
 		// After end, check for start/end of next match.
 		// When another match, have to check for start again.
@@ -2406,9 +2410,21 @@
 		// and bad things happen.
 		if (*ptr == NUL)
 		    has_match_conc = 0;
-	    }
+#else
+		search_attr = 0;
 #endif
 
+		// Check if ComplMatchIns highlight is needed.
+		if ((State & MODE_INSERT) && in_curline && ins_compl_active())
+		{
+		    int ins_match_attr =
+			ins_compl_col_range_attr((int)(ptr - line));
+		    if (ins_match_attr > 0)
+			search_attr =
+			    hl_combine_attr(search_attr, ins_match_attr);
+		}
+	    }
+
 #ifdef FEAT_DIFF
 	    if (wlv.diff_hlf != (hlf_T)0)
 	    {
@@ -2562,15 +2578,6 @@
 #endif
 	}
 
-	// Apply ComplMatchIns highlight if needed.
-	if (wlv.draw_state == WL_LINE
-		&& (State & MODE_INSERT) && in_curline && ins_compl_active())
-	{
-	    int ins_match_attr = ins_compl_col_range_attr((int)(ptr - line));
-	    if (ins_match_attr > 0)
-	        wlv.char_attr = hl_combine_attr(wlv.char_attr, ins_match_attr);
-	}
-
 	// combine attribute with 'wincolor'
 	if (wlv.win_attr != 0)
 	{
diff --git a/src/highlight.c b/src/highlight.c
index a4b2d48..628ceb2 100644
--- a/src/highlight.c
+++ b/src/highlight.c
@@ -262,7 +262,7 @@
     "default link PmenuMatchSel PmenuSel",
     "default link PmenuExtra Pmenu",
     "default link PmenuExtraSel PmenuSel",
-    "default link ComplMatchIns Normal",
+    CENT("ComplMatchIns cterm=NONE", "ComplMatchIns gui=NONE"),
     CENT("Normal cterm=NONE", "Normal gui=NONE"),
     NULL
 };
diff --git a/src/insexpand.c b/src/insexpand.c
index fd82b36..2d14904 100644
--- a/src/insexpand.c
+++ b/src/insexpand.c
@@ -912,7 +912,7 @@
     if (len == -1)
 	len = (int)STRLEN(p);
     ins_bytes_len(p, len);
-    compl_ins_end_col = curwin->w_cursor.col - 1;
+    compl_ins_end_col = curwin->w_cursor.col;
 }
 
 /*
@@ -2437,7 +2437,7 @@
 	    int	    compl_len = get_compl_len();
 
 	    if ((int)plen > compl_len)
-		ins_compl_insert_bytes(p + compl_len, (int)(plen - compl_len));
+		ins_compl_insert_bytes(p + compl_len, (int)plen - compl_len);
 	}
 	retval = TRUE;
     }
@@ -4264,6 +4264,7 @@
 	if (stop_arrow() == FAIL)
 	    return;
 	backspace_until_column(col);
+	compl_ins_end_col = curwin->w_cursor.col;
     }
 
     // TODO: is this sufficient for redrawing?  Redrawing everything causes
diff --git a/src/testdir/dumps/Test_pum_matchins_07.dump b/src/testdir/dumps/Test_pum_matchins_07.dump
new file mode 100644
index 0000000..bed5b51
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_matchins_07.dump
@@ -0,0 +1,20 @@
+|α+0&#ffffff0|β|γ| |f+0#ff404010&|o@1> +0#0000000&|f|o@1| @63
+|~+0#4040ff13&| @1| +0#0000001#e0e0e08|f|o@1| @11| +0#4040ff13#ffffff0@55
+|~| @1| +0#0000001#ffd7ff255|b|a|r| @11| +0#4040ff13#ffffff0@55
+|~| @1| +0#0000001#ffd7ff255|你*&|好| +&@10| +0#4040ff13#ffffff0@55
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|-+2#0000000&@1| |O|m|n|i| |c|o|m|p|l|e|t|i|o|n| |(|^|O|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |1| |o|f| |3| +0#0000000&@34
diff --git a/src/testdir/dumps/Test_pum_matchins_08.dump b/src/testdir/dumps/Test_pum_matchins_08.dump
new file mode 100644
index 0000000..9f97b01
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_matchins_08.dump
@@ -0,0 +1,20 @@
+|α+0&#ffffff0|β|γ| > |f|o@1| @66
+|~+0#4040ff13&| @1| +0#0000001#ffd7ff255|f|o@1| @11| +0#4040ff13#ffffff0@55
+|~| @1| +0#0000001#ffd7ff255|b|a|r| @11| +0#4040ff13#ffffff0@55
+|~| @1| +0#0000001#ffd7ff255|你*&|好| +&@10| +0#4040ff13#ffffff0@55
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|-+2#0000000&@1| |O|m|n|i| |c|o|m|p|l|e|t|i|o|n| |(|^|O|^|N|^|P|)| |B+0#e000002&|a|c|k| |a|t| |o|r|i|g|i|n|a|l| +0#0000000&@30
diff --git a/src/testdir/dumps/Test_pum_matchins_09.dump b/src/testdir/dumps/Test_pum_matchins_09.dump
new file mode 100644
index 0000000..9efdfcc
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_matchins_09.dump
@@ -0,0 +1,20 @@
+|α+0&#ffffff0|β|γ| |你*0#ff404010&|好> +0#0000000&|f|o@1| @62
+|~+0#4040ff13&| @1| +0#0000001#ffd7ff255|f|o@1| @11| +0#4040ff13#ffffff0@55
+|~| @1| +0#0000001#ffd7ff255|b|a|r| @11| +0#4040ff13#ffffff0@55
+|~| @1| +0#0000001#e0e0e08|你*&|好| +&@10| +0#4040ff13#ffffff0@55
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|-+2#0000000&@1| |O|m|n|i| |c|o|m|p|l|e|t|i|o|n| |(|^|O|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |3| |o|f| |3| +0#0000000&@34
diff --git a/src/testdir/dumps/Test_pum_matchins_10.dump b/src/testdir/dumps/Test_pum_matchins_10.dump
new file mode 100644
index 0000000..867f935
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_matchins_10.dump
@@ -0,0 +1,20 @@
+|α+0&#ffffff0|β|γ| |你*&|好> +&|f|o@1| @62
+|~+0#4040ff13&| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|-+2#0000000&@1| |I|N|S|E|R|T| |-@1| +0&&@44|1|,|1|4|-|9| @7|A|l@1| 
diff --git a/src/testdir/dumps/Test_pum_matchins_combine_01.dump b/src/testdir/dumps/Test_pum_matchins_combine_01.dump
new file mode 100644
index 0000000..2fe08fa
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_matchins_combine_01.dump
@@ -0,0 +1,20 @@
+|a+8&#40ff4011@2| |f|o@1> |b@2| @63
+|~+0#4040ff13#4040ff13| @1| +0#0000001#e0e0e08|f|o@1| @11| +0#4040ff13#4040ff13@55
+|~| @1| +0#0000001#ffd7ff255|b|a|r| @11| +0#4040ff13#4040ff13@55
+|~| @1| +0#0000001#ffd7ff255|你*&|好| +&@10| +0#4040ff13#4040ff13@55
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|-+2#0000000&@1| |O|m|n|i| |c|o|m|p|l|e|t|i|o|n| |(|^|O|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |1| |o|f| |3| +0#0000000&@34
diff --git a/src/testdir/dumps/Test_pum_matchins_combine_02.dump b/src/testdir/dumps/Test_pum_matchins_combine_02.dump
new file mode 100644
index 0000000..2a54238
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_matchins_combine_02.dump
@@ -0,0 +1,20 @@
+|a+8&#40ff4011@2| > |b@2| @66
+|~+0#4040ff13#4040ff13| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|-+2#0000000&@1| |I|N|S|E|R|T| |-@1| +0&&@44|1|,|5| @10|A|l@1| 
diff --git a/src/testdir/dumps/Test_pum_matchins_combine_03.dump b/src/testdir/dumps/Test_pum_matchins_combine_03.dump
new file mode 100644
index 0000000..a8c5c66
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_matchins_combine_03.dump
@@ -0,0 +1,20 @@
+|a+8&#40ff4011@2| |f+8#ffff4012#ff404010|o@1> +8#0000000#40ff4011|b@2| @63
+|~+0#4040ff13#4040ff13| @1| +0#0000001#e0e0e08|f|o@1| @11| +0#4040ff13#4040ff13@55
+|~| @1| +0#0000001#ffd7ff255|b|a|r| @11| +0#4040ff13#4040ff13@55
+|~| @1| +0#0000001#ffd7ff255|你*&|好| +&@10| +0#4040ff13#4040ff13@55
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|-+2#0000000&@1| |O|m|n|i| |c|o|m|p|l|e|t|i|o|n| |(|^|O|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |1| |o|f| |3| +0#0000000&@34
diff --git a/src/testdir/dumps/Test_pum_matchins_combine_04.dump b/src/testdir/dumps/Test_pum_matchins_combine_04.dump
new file mode 100644
index 0000000..667e1c9
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_matchins_combine_04.dump
@@ -0,0 +1,20 @@
+|a+8&#40ff4011@2| > |b@2| @66
+|~+0#4040ff13#4040ff13| @1| +0#0000001#ffd7ff255|f|o@1| @11| +0#4040ff13#4040ff13@55
+|~| @1| +0#0000001#ffd7ff255|b|a|r| @11| +0#4040ff13#4040ff13@55
+|~| @1| +0#0000001#ffd7ff255|你*&|好| +&@10| +0#4040ff13#4040ff13@55
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|-+2#0000000&@1| |O|m|n|i| |c|o|m|p|l|e|t|i|o|n| |(|^|O|^|N|^|P|)| |B+0#e000002&|a|c|k| |a|t| |o|r|i|g|i|n|a|l| +0#0000000&@30
diff --git a/src/testdir/dumps/Test_pum_matchins_combine_05.dump b/src/testdir/dumps/Test_pum_matchins_combine_05.dump
new file mode 100644
index 0000000..d241123
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_matchins_combine_05.dump
@@ -0,0 +1,20 @@
+|a+8&#40ff4011@2| |你*8#ffff4012#ff404010|好> +8#0000000#40ff4011|b@2| @62
+|~+0#4040ff13#4040ff13| @1| +0#0000001#ffd7ff255|f|o@1| @11| +0#4040ff13#4040ff13@55
+|~| @1| +0#0000001#ffd7ff255|b|a|r| @11| +0#4040ff13#4040ff13@55
+|~| @1| +0#0000001#e0e0e08|你*&|好| +&@10| +0#4040ff13#4040ff13@55
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|-+2#0000000&@1| |O|m|n|i| |c|o|m|p|l|e|t|i|o|n| |(|^|O|^|N|^|P|)| |m+0#00e0003&|a|t|c|h| |3| |o|f| |3| +0#0000000&@34
diff --git a/src/testdir/dumps/Test_pum_matchins_combine_06.dump b/src/testdir/dumps/Test_pum_matchins_combine_06.dump
new file mode 100644
index 0000000..2a54238
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_matchins_combine_06.dump
@@ -0,0 +1,20 @@
+|a+8&#40ff4011@2| > |b@2| @66
+|~+0#4040ff13#4040ff13| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|~| @73
+|-+2#0000000&@1| |I|N|S|E|R|T| |-@1| +0&&@44|1|,|5| @10|A|l@1| 
diff --git a/src/testdir/test_indent.vim b/src/testdir/test_indent.vim
index 0cead5f..0f0f9da 100644
--- a/src/testdir/test_indent.vim
+++ b/src/testdir/test_indent.vim
@@ -305,7 +305,7 @@
 
 " Test that mouse shape is restored to Normal mode after using "gq" when
 " 'indentexpr' executes :normal.
-func Test_indent_norm_with_gq()
+func Test_mouse_shape_indent_norm_with_gq()
   CheckFeature mouseshape
   CheckCanRunGui
 
diff --git a/src/testdir/test_popup.vim b/src/testdir/test_popup.vim
index 9ea5445..8f053fa 100644
--- a/src/testdir/test_popup.vim
+++ b/src/testdir/test_popup.vim
@@ -1746,13 +1746,67 @@
   call TermWait(buf)
   call term_sendkeys(buf, "Sαβγ \<C-X>\<C-O>\<C-Y>")
   call VerifyScreenDump(buf, 'Test_pum_matchins_04', {})
-  call term_sendkeys(buf, "\<C-E>\<Esc>")
+  call term_sendkeys(buf, "\<Esc>")
 
   " restore after cancel completion
   call TermWait(buf)
   call term_sendkeys(buf, "Sαβγ \<C-X>\<C-O>\<Space>")
   call VerifyScreenDump(buf, 'Test_pum_matchins_05', {})
-  call term_sendkeys(buf, "\<C-E>\<Esc>")
+  call term_sendkeys(buf, "\<Esc>")
+
+  " text after the inserted text shouldn't be highlighted
+  call TermWait(buf)
+  call term_sendkeys(buf, "0ea \<C-X>\<C-O>")
+  call VerifyScreenDump(buf, 'Test_pum_matchins_07', {})
+  call term_sendkeys(buf, "\<C-P>")
+  call VerifyScreenDump(buf, 'Test_pum_matchins_08', {})
+  call term_sendkeys(buf, "\<C-P>")
+  call VerifyScreenDump(buf, 'Test_pum_matchins_09', {})
+  call term_sendkeys(buf, "\<C-Y>")
+  call VerifyScreenDump(buf, 'Test_pum_matchins_10', {})
+  call term_sendkeys(buf, "\<Esc>")
+
+  call StopVimInTerminal(buf)
+endfunc
+
+func Test_pum_matchins_highlight_combine()
+  CheckScreendump
+  let lines =<< trim END
+    func Omni_test(findstart, base)
+      if a:findstart
+        return col(".")
+      endif
+      return [#{word: "foo"}, #{word: "bar"}, #{word: "你好"}]
+    endfunc
+    set omnifunc=Omni_test
+    hi Normal ctermbg=blue
+    hi CursorLine cterm=underline ctermbg=green
+    set cursorline
+    call setline(1, 'aaa bbb')
+  END
+  call writefile(lines, 'Xscript', 'D')
+  let buf = RunVimInTerminal('-S Xscript', {})
+
+  " when ComplMatchIns is not set, CursorLine applies normally
+  call term_sendkeys(buf, "0ea \<C-X>\<C-O>")
+  call VerifyScreenDump(buf, 'Test_pum_matchins_combine_01', {})
+  call term_sendkeys(buf, "\<C-E>")
+  call VerifyScreenDump(buf, 'Test_pum_matchins_combine_02', {})
+  call term_sendkeys(buf, "\<BS>\<Esc>")
+
+  " when ComplMatchIns is set, it is applied over CursorLine
+  call TermWait(buf)
+  call term_sendkeys(buf, ":hi ComplMatchIns ctermbg=red ctermfg=yellow\<CR>")
+  call TermWait(buf)
+  call term_sendkeys(buf, "0ea \<C-X>\<C-O>")
+  call VerifyScreenDump(buf, 'Test_pum_matchins_combine_03', {})
+  call term_sendkeys(buf, "\<C-P>")
+  call VerifyScreenDump(buf, 'Test_pum_matchins_combine_04', {})
+  call term_sendkeys(buf, "\<C-P>")
+  call VerifyScreenDump(buf, 'Test_pum_matchins_combine_05', {})
+  call term_sendkeys(buf, "\<C-E>")
+  call VerifyScreenDump(buf, 'Test_pum_matchins_combine_06', {})
+  call term_sendkeys(buf, "\<Esc>")
 
   call StopVimInTerminal(buf)
 endfunc
diff --git a/src/version.c b/src/version.c
index ebaca6e..f18772d 100644
--- a/src/version.c
+++ b/src/version.c
@@ -705,6 +705,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    945,
+/**/
     944,
 /**/
     943,