patch 9.1.0494: Wrong matched text highlighted in pum with 'rightleft'

Problem:  Wrong matched text highlighted in pum with 'rightleft'.
Solution: Match using the original text instead of the reversed text.
          (zeertzjq)

closes: #15020

Signed-off-by: zeertzjq <zeertzjq@outlook.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/popupmenu.c b/src/popupmenu.c
index 79686c6..8a4b68f 100644
--- a/src/popupmenu.c
+++ b/src/popupmenu.c
@@ -421,46 +421,43 @@
 }
 
 /*
- * displays text on the popup menu with specific attributes.
+ * Computes attributes of text on the popup menu.
+ * Returns attributes for every cell, or NULL if all attributes are the same.
  */
-    static void
-pum_screen_put_with_attr(int row, int col, char_u *text, int textlen, hlf_T hlf)
+    static int *
+pum_compute_text_attrs(char_u *text, hlf_T hlf)
 {
     int		i;
     int		leader_len;
-    int		char_len;
-    int		cells;
+    int		char_cells;
     int		new_attr;
-    char_u	*rt_leader = NULL;
-    char_u	*match_leader = NULL;
     char_u	*ptr = text;
+    int		cell_idx = 0;
     garray_T	*ga = NULL;
+    int		*attrs = NULL;
     char_u	*leader = ins_compl_leader();
     int		in_fuzzy = (get_cot_flags() & COT_FUZZY) != 0;
+    int		matched_start = FALSE;
+    int_u	char_pos = 0;
 
     if (leader == NULL || *leader == NUL || (hlf != HLF_PSI && hlf != HLF_PNI)
 	    || (highlight_attr[HLF_PMSI] == highlight_attr[HLF_PSI]
 		&& highlight_attr[HLF_PMNI] == highlight_attr[HLF_PNI]))
-    {
-	screen_puts_len(text, textlen, row, col, highlight_attr[hlf]);
-	return;
-    }
+	return NULL;
 
-#ifdef FEAT_RIGHTLEFT
-    if (pum_rl)
-	rt_leader = reverse_text(leader);
-#endif
-    match_leader = rt_leader != NULL ? rt_leader : leader;
-    leader_len = (int)STRLEN(match_leader);
+    attrs = ALLOC_MULT(int, vim_strsize(text));
+    if (attrs == NULL)
+	return NULL;
+
+    leader_len = (int)STRLEN(leader);
 
     if (in_fuzzy)
-	ga = fuzzy_match_str_with_pos(text, match_leader);
+	ga = fuzzy_match_str_with_pos(text, leader);
+    else
+	matched_start = STRNCMP(text, leader, leader_len) == 0;
 
-    // Render text with proper attributes
-    while (*ptr != NUL && ptr < text + textlen)
+    while (*ptr != NUL)
     {
-	char_len = mb_ptr2len(ptr);
-	cells = mb_ptr2cells(ptr);
 	new_attr = highlight_attr[hlf];
 
 	if (ga != NULL)
@@ -468,15 +465,7 @@
 	    // Handle fuzzy matching
 	    for (i = 0; i < ga->ga_len; i++)
 	    {
-		int_u *match_pos = ((int_u *)ga->ga_data) + i;
-		int_u actual_char_pos = 0;
-		char_u *temp_ptr = text;
-		while (temp_ptr < ptr)
-		{
-		    temp_ptr += mb_ptr2len(temp_ptr);
-		    actual_char_pos++;
-		}
-		if (actual_char_pos == match_pos[0])
+		if (char_pos == ((int_u *)ga->ga_data)[i])
 		{
 		    new_attr = highlight_attr[hlf == HLF_PSI
 							? HLF_PMSI : HLF_PMNI];
@@ -484,13 +473,16 @@
 		}
 	    }
 	}
-	else if (!in_fuzzy && (ptr - text < leader_len)
-			     && (STRNCMP(text, match_leader, leader_len) == 0))
+	else if (matched_start && ptr < text + leader_len)
 	    new_attr = highlight_attr[hlf == HLF_PSI ? HLF_PMSI : HLF_PMNI];
 
-	screen_puts_len(ptr, char_len, row, col, new_attr);
-	col += cells;
-	ptr += char_len;
+	char_cells = mb_ptr2cells(ptr);
+	for (i = 0; i < char_cells; i++)
+	    attrs[cell_idx + i] = new_attr;
+	cell_idx += char_cells;
+
+	MB_PTR_ADV(ptr);
+	char_pos++;
     }
 
     if (ga != NULL)
@@ -498,8 +490,40 @@
 	ga_clear(ga);
 	vim_free(ga);
     }
-    if (rt_leader)
-	vim_free(rt_leader);
+    return attrs;
+}
+
+/*
+ * Displays text on the popup menu with specific attributes.
+ */
+    static void
+pum_screen_puts_with_attrs(
+    int		row,
+    int		col,
+    int		cells UNUSED,
+    char_u	*text,
+    int		textlen,
+    int		*attrs)
+{
+    int		col_start = col;
+    char_u	*ptr = text;
+    int		char_len;
+    int		attr;
+
+    // Render text with proper attributes
+    while (*ptr != NUL && ptr < text + textlen)
+    {
+	char_len = mb_ptr2len(ptr);
+#ifdef FEAT_RIGHTLEFT
+	if (pum_rl)
+	    attr = attrs[col_start + cells - col - 1];
+	else
+#endif
+	    attr = attrs[col - col_start];
+        screen_puts_len(ptr, char_len, row, col, attr);
+	col += mb_ptr2cells(ptr);
+	ptr += char_len;
+    }
 }
 
 /*
@@ -616,6 +640,7 @@
 			// Display the text that fits or comes before a Tab.
 			// First convert it to printable characters.
 			char_u	*st;
+			int	*attrs;
 			int	saved = *p;
 
 			if (saved != NUL)
@@ -623,6 +648,9 @@
 			st = transstr(s);
 			if (saved != NUL)
 			    *p = saved;
+
+			attrs = pum_compute_text_attrs(st, hlf);
+
 #ifdef FEAT_RIGHTLEFT
 			if (pum_rl)
 			{
@@ -633,19 +661,19 @@
 				if (rt != NULL)
 				{
 				    char_u	*rt_start = rt;
-				    int		size;
+				    int		cells;
 
-				    size = vim_strsize(rt);
-				    if (size > pum_width)
+				    cells = vim_strsize(rt);
+				    if (cells > pum_width)
 				    {
 					do
 					{
-					    size -= has_mbyte
-						    ? (*mb_ptr2cells)(rt) : 1;
+					    cells -= has_mbyte
+						     ? (*mb_ptr2cells)(rt) : 1;
 					    MB_PTR_ADV(rt);
-					} while (size > pum_width);
+					} while (cells > pum_width);
 
-					if (size < pum_width)
+					if (cells < pum_width)
 					{
 					    // Most left character requires
 					    // 2-cells but only 1 cell is
@@ -653,10 +681,18 @@
 					    // '<' on the left of the pum
 					    // item
 					    *(--rt) = '<';
-					    size++;
+					    cells++;
 					}
 				    }
-				    pum_screen_put_with_attr(row, col - size + 1, rt, (int)STRLEN(rt), hlf);
+
+				    if (attrs == NULL)
+					screen_puts_len(rt, (int)STRLEN(rt),
+						   row, col - cells + 1, attr);
+				    else
+					pum_screen_puts_with_attrs(row,
+						    col - cells + 1, cells, rt,
+						       (int)STRLEN(rt), attrs);
+
 				    vim_free(rt_start);
 				}
 				vim_free(st);
@@ -684,12 +720,20 @@
 				    else
 					--cells;
 				}
-				pum_screen_put_with_attr(row, col, st, size, hlf);
+
+				if (attrs == NULL)
+				    screen_puts_len(st, size, row, col, attr);
+				else
+				    pum_screen_puts_with_attrs(row, col, cells,
+							      st, size, attrs);
+
 				vim_free(st);
 			    }
 			    col += width;
 			}
 
+			vim_free(attrs);
+
 			if (*p != TAB)
 			    break;
 
diff --git a/src/testdir/dumps/Test_pum_highlights_03.dump b/src/testdir/dumps/Test_pum_highlights_03.dump
index 094660a..77d87d4 100644
--- a/src/testdir/dumps/Test_pum_highlights_03.dump
+++ b/src/testdir/dumps/Test_pum_highlights_03.dump
@@ -1,5 +1,6 @@
 |f+0&#ffffff0|o> @72
-|f+0#00e0e07#ffd7ff255|o|o+0#0000001#e0e0e08| @4|f|o@1|k|i|n|d| | +0#4040ff13#ffffff0@58
+|f+0#00e0e07#e0e0e08|o|o+0#0000001&| @4|f|o@1|k|i|n|d| | +0#4040ff13#ffffff0@58
+|f+0#0000e05#ffd7ff255|o|o+0#0000001&|f|o@1| @1|f|o@1|k|i|n|d| | +0#4040ff13#ffffff0@58
 |f+0#0000e05#ffd7ff255|o|o+0#0000001&|b|a|r| @1|f|o@1|k|i|n|d| | +0#4040ff13#ffffff0@58
 |f+0#0000e05#ffd7ff255|o|o+0#0000001&|B|a|z| @1|f|o@1|k|i|n|d| | +0#4040ff13#ffffff0@58
 |f+0#0000e05#ffd7ff255|o|o+0#0000001&|b|a|l|a| |f|o@1|k|i|n|d| | +0#4040ff13#ffffff0@58
@@ -16,5 +17,4 @@
 |~| @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| |8| +0#0000000&@34
+|-+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| |9| +0#0000000&@34
diff --git a/src/testdir/dumps/Test_pum_highlights_04.dump b/src/testdir/dumps/Test_pum_highlights_04.dump
index 2204dbd..486a59e 100644
--- a/src/testdir/dumps/Test_pum_highlights_04.dump
+++ b/src/testdir/dumps/Test_pum_highlights_04.dump
@@ -1,5 +1,5 @@
 |你*0&#ffffff0> +&@72
-|你*0#00e0e07#ffd7ff255|好*0#0000001#e0e0e08| +&@10| +0#4040ff13#ffffff0@59
+|你*0#00e0e07#e0e0e08|好*0#0000001&| +&@10| +0#4040ff13#ffffff0@59
 |你*0#0000e05#ffd7ff255|好*0#0000001&|吗| +&@8| +0#4040ff13#ffffff0@59
 |你*0#0000e05#ffd7ff255|不*0#0000001&|好|吗| +&@6| +0#4040ff13#ffffff0@59
 |你*0#0000e05#ffd7ff255|可*0#0000001&|好|吗| +&@6| +0#4040ff13#ffffff0@59
@@ -17,4 +17,4 @@
 |~| @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| |8| +0#0000000&@34
+|-+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| |9| +0#0000000&@34
diff --git a/src/testdir/dumps/Test_pum_highlights_05.dump b/src/testdir/dumps/Test_pum_highlights_05.dump
index b67c249..a2ce2cf 100644
--- a/src/testdir/dumps/Test_pum_highlights_05.dump
+++ b/src/testdir/dumps/Test_pum_highlights_05.dump
@@ -1,5 +1,5 @@
 |你*0&#ffffff0|吗> +&@70
-|你*0#00e0e07#ffd7ff255|好*0#0000001#e0e0e08|吗*0#00e0e07#ffd7ff255| +0#0000001#e0e0e08@8| +0#4040ff13#ffffff0@59
+|你*0#00e0e07#e0e0e08|好*0#0000001&|吗*0#00e0e07&| +0#0000001&@8| +0#4040ff13#ffffff0@59
 |你*0#0000e05#ffd7ff255|不*0#0000001&|好|吗*0#0000e05&| +0#0000001&@6| +0#4040ff13#ffffff0@59
 |你*0#0000e05#ffd7ff255|可*0#0000001&|好|吗*0#0000e05&| +0#0000001&@6| +0#4040ff13#ffffff0@59
 |~| @73
@@ -17,4 +17,4 @@
 |~| @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| |8| +0#0000000&@34
+|-+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| |9| +0#0000000&@34
diff --git a/src/testdir/dumps/Test_pum_highlights_06.dump b/src/testdir/dumps/Test_pum_highlights_06.dump
index 8aeb5ae..3eb1524 100644
--- a/src/testdir/dumps/Test_pum_highlights_06.dump
+++ b/src/testdir/dumps/Test_pum_highlights_06.dump
@@ -1,5 +1,6 @@
-| +0&#ffffff0@70|o>f|o|f
-| +0#4040ff13&@58| +0#0000001#e0e0e08|d|n|i|k|o@1|f| @4|o|o+0#00e0e07#ffd7ff255|f
+| +0&#ffffff0@71> |o|f
+| +0#4040ff13&@58| +0#0000001#e0e0e08|d|n|i|k|o@1|f| @4|o|o+0#00e0e07&|f
+| +0#4040ff13#ffffff0@58| +0#0000001#ffd7ff255|d|n|i|k|o@1|f| @1|o@1|f|o|o+0#0000e05&|f
 | +0#4040ff13#ffffff0@58| +0#0000001#ffd7ff255|d|n|i|k|o@1|f| @1|r|a|b|o|o+0#0000e05&|f
 | +0#4040ff13#ffffff0@58| +0#0000001#ffd7ff255|d|n|i|k|o@1|f| @1|z|a|B|o|o+0#0000e05&|f
 | +0#4040ff13#ffffff0@58| +0#0000001#ffd7ff255|d|n|i|k|o@1|f| |a|l|a|b|o|o+0#0000e05&|f
@@ -16,5 +17,4 @@
 | @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| |8| +0#0000000&@34
+|-+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| |9| +0#0000000&@34
diff --git a/src/testdir/dumps/Test_pum_highlights_06a.dump b/src/testdir/dumps/Test_pum_highlights_06a.dump
new file mode 100644
index 0000000..5d51f11
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_highlights_06a.dump
@@ -0,0 +1,20 @@
+| +0&#ffffff0@71> |你*&
+| +0#4040ff13&@59| +0#0000001#e0e0e08@10|好*&|你*0#00e0e07&
+| +0#4040ff13#ffffff0@59| +0#0000001#ffd7ff255@8|吗*&|好|你*0#0000e05&
+| +0#4040ff13#ffffff0@59| +0#0000001#ffd7ff255@6|吗*&|好|不|你*0#0000e05&
+| +0#4040ff13#ffffff0@59| +0#0000001#ffd7ff255@6|吗*&|好|可|你*0#0000e05&
+| +0#4040ff13#ffffff0@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| |9| +0#0000000&@34
diff --git a/src/testdir/dumps/Test_pum_highlights_06b.dump b/src/testdir/dumps/Test_pum_highlights_06b.dump
new file mode 100644
index 0000000..41314ab
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_highlights_06b.dump
@@ -0,0 +1,20 @@
+| +0&#ffffff0@69> |吗*&|你
+| +0#4040ff13&@59| +0#0000001#e0e0e08@8|吗*0#00e0e07&|好*0#0000001&|你*0#00e0e07&
+| +0#4040ff13#ffffff0@59| +0#0000001#ffd7ff255@6|吗*0#0000e05&|好*0#0000001&|不|你*0#0000e05&
+| +0#4040ff13#ffffff0@59| +0#0000001#ffd7ff255@6|吗*0#0000e05&|好*0#0000001&|可|你*0#0000e05&
+| +0#4040ff13#ffffff0@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| |9| +0#0000000&@34
diff --git a/src/testdir/dumps/Test_pum_highlights_07.dump b/src/testdir/dumps/Test_pum_highlights_07.dump
index 094660a..77d87d4 100644
--- a/src/testdir/dumps/Test_pum_highlights_07.dump
+++ b/src/testdir/dumps/Test_pum_highlights_07.dump
@@ -1,5 +1,6 @@
 |f+0&#ffffff0|o> @72
-|f+0#00e0e07#ffd7ff255|o|o+0#0000001#e0e0e08| @4|f|o@1|k|i|n|d| | +0#4040ff13#ffffff0@58
+|f+0#00e0e07#e0e0e08|o|o+0#0000001&| @4|f|o@1|k|i|n|d| | +0#4040ff13#ffffff0@58
+|f+0#0000e05#ffd7ff255|o|o+0#0000001&|f|o@1| @1|f|o@1|k|i|n|d| | +0#4040ff13#ffffff0@58
 |f+0#0000e05#ffd7ff255|o|o+0#0000001&|b|a|r| @1|f|o@1|k|i|n|d| | +0#4040ff13#ffffff0@58
 |f+0#0000e05#ffd7ff255|o|o+0#0000001&|B|a|z| @1|f|o@1|k|i|n|d| | +0#4040ff13#ffffff0@58
 |f+0#0000e05#ffd7ff255|o|o+0#0000001&|b|a|l|a| |f|o@1|k|i|n|d| | +0#4040ff13#ffffff0@58
@@ -16,5 +17,4 @@
 |~| @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| |8| +0#0000000&@34
+|-+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| |9| +0#0000000&@34
diff --git a/src/testdir/dumps/Test_pum_highlights_08.dump b/src/testdir/dumps/Test_pum_highlights_08.dump
new file mode 100644
index 0000000..3eb1524
--- /dev/null
+++ b/src/testdir/dumps/Test_pum_highlights_08.dump
@@ -0,0 +1,20 @@
+| +0&#ffffff0@71> |o|f
+| +0#4040ff13&@58| +0#0000001#e0e0e08|d|n|i|k|o@1|f| @4|o|o+0#00e0e07&|f
+| +0#4040ff13#ffffff0@58| +0#0000001#ffd7ff255|d|n|i|k|o@1|f| @1|o@1|f|o|o+0#0000e05&|f
+| +0#4040ff13#ffffff0@58| +0#0000001#ffd7ff255|d|n|i|k|o@1|f| @1|r|a|b|o|o+0#0000e05&|f
+| +0#4040ff13#ffffff0@58| +0#0000001#ffd7ff255|d|n|i|k|o@1|f| @1|z|a|B|o|o+0#0000e05&|f
+| +0#4040ff13#ffffff0@58| +0#0000001#ffd7ff255|d|n|i|k|o@1|f| |a|l|a|b|o|o+0#0000e05&|f
+| +0#4040ff13#ffffff0@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| |9| +0#0000000&@34
diff --git a/src/testdir/test_popup.vim b/src/testdir/test_popup.vim
index b9c6b92..d1fa5a9 100644
--- a/src/testdir/test_popup.vim
+++ b/src/testdir/test_popup.vim
@@ -1390,6 +1390,7 @@
       return {
             \ 'words': [
             \ { 'word': 'foo', 'kind': 'fookind' },
+            \ { 'word': 'foofoo', 'kind': 'fookind' },
             \ { 'word': 'foobar', 'kind': 'fookind' },
             \ { 'word': 'fooBaz', 'kind': 'fookind' },
             \ { 'word': 'foobala', 'kind': 'fookind' },
@@ -1401,7 +1402,7 @@
     endfunc
     set omnifunc=Omni_test
     set completeopt=menu,noinsert,fuzzy
-    hi PmenuMatchSel  ctermfg=6 ctermbg=225
+    hi PmenuMatchSel  ctermfg=6 ctermbg=7
     hi PmenuMatch     ctermfg=4 ctermbg=225
   END
   call writefile(lines, 'Xscript', 'D')
@@ -1412,7 +1413,7 @@
   call term_sendkeys(buf, "fo")
   call TermWait(buf, 50)
   call VerifyScreenDump(buf, 'Test_pum_highlights_03', {})
-  call term_sendkeys(buf, "\<ESC>S\<C-x>\<C-O>")
+  call term_sendkeys(buf, "\<Esc>S\<C-X>\<C-O>")
   call TermWait(buf, 50)
   call term_sendkeys(buf, "你")
   call TermWait(buf, 50)
@@ -1420,28 +1421,48 @@
   call term_sendkeys(buf, "吗")
   call TermWait(buf, 50)
   call VerifyScreenDump(buf, 'Test_pum_highlights_05', {})
+  call term_sendkeys(buf, "\<C-E>\<Esc>")
 
   if has('rightleft')
-    call term_sendkeys(buf, "\<C-E>\<ESC>u:set rightleft\<CR>")
+    call term_sendkeys(buf, ":set rightleft\<CR>")
     call TermWait(buf, 50)
-    call term_sendkeys(buf, "i\<C-X>\<C-O>")
+    call term_sendkeys(buf, "S\<C-X>\<C-O>")
     call TermWait(buf, 50)
     call term_sendkeys(buf, "fo")
     call TermWait(buf, 50)
     call VerifyScreenDump(buf, 'Test_pum_highlights_06', {})
-    call term_sendkeys(buf, "\<C-E>\<ESC>u:set norightleft\<CR>")
+    call term_sendkeys(buf, "\<Esc>S\<C-X>\<C-O>")
+    call TermWait(buf, 50)
+    call term_sendkeys(buf, "你")
+    call VerifyScreenDump(buf, 'Test_pum_highlights_06a', {})
+    call term_sendkeys(buf, "吗")
+    call VerifyScreenDump(buf, 'Test_pum_highlights_06b', {})
+    call term_sendkeys(buf, "\<C-E>\<Esc>")
+    call term_sendkeys(buf, ":set norightleft\<CR>")
     call TermWait(buf)
   endif
 
   call term_sendkeys(buf, ":set completeopt-=fuzzy\<CR>")
   call TermWait(buf)
-  call term_sendkeys(buf, "\<ESC>S\<C-x>\<C-O>")
+  call term_sendkeys(buf, "S\<C-X>\<C-O>")
   call TermWait(buf, 50)
   call term_sendkeys(buf, "fo")
   call TermWait(buf, 50)
   call VerifyScreenDump(buf, 'Test_pum_highlights_07', {})
+  call term_sendkeys(buf, "\<C-E>\<Esc>")
 
-  call term_sendkeys(buf, "\<C-E>\<Esc>u")
+  if has('rightleft')
+    call term_sendkeys(buf, ":set rightleft\<CR>")
+    call TermWait(buf, 50)
+    call term_sendkeys(buf, "S\<C-X>\<C-O>")
+    call TermWait(buf, 50)
+    call term_sendkeys(buf, "fo")
+    call TermWait(buf, 50)
+    call VerifyScreenDump(buf, 'Test_pum_highlights_08', {})
+    call term_sendkeys(buf, "\<C-E>\<Esc>")
+    call term_sendkeys(buf, ":set norightleft\<CR>")
+  endif
+
   call TermWait(buf)
   call StopVimInTerminal(buf)
 endfunc
diff --git a/src/version.c b/src/version.c
index 3817c9b..0ca2bf2 100644
--- a/src/version.c
+++ b/src/version.c
@@ -705,6 +705,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    494,
+/**/
     493,
 /**/
     492,