patch 9.0.2022: getmousepos() returns wrong index for TAB char
Problem: When clicking in the middle of a TAB, getmousepos() returns
the column of the next char instead of the TAB.
Solution: Break out of the loop when the vcol to find is inside current
char. Fix invalid memory access when calling virtcol2col() on
an empty line.
closes: #13321
Signed-off-by: Christian Brabandt <cb@256bit.org>
Co-authored-by: zeertzjq <zeertzjq@outlook.com>
diff --git a/src/move.c b/src/move.c
index 42878e4..72490d2 100644
--- a/src/move.c
+++ b/src/move.c
@@ -1547,14 +1547,17 @@
static int
virtcol2col(win_T *wp, linenr_T lnum, int vcol)
{
- int offset = vcol2col(wp, lnum, vcol);
+ int offset = vcol2col(wp, lnum, vcol - 1);
char_u *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
char_u *p = line + offset;
- // For a multibyte character, need to return the column number of the first
- // byte.
- MB_PTR_BACK(line, p);
-
+ if (*p == NUL)
+ {
+ if (p == line) // empty line
+ return 0;
+ // Move to the first byte of the last char.
+ MB_PTR_BACK(line, p);
+ }
return (int)(p - line + 1);
}