From 0376874c326c85b5e155ad0bed24f5dfb8707608 Mon Sep 17 00:00:00 2001 From: Tommy Allen Date: Wed, 24 Jan 2018 19:19:22 -0500 Subject: mouse.c: Fix mouse click on lines with multibyte text fixes #5341, #5801 --- src/nvim/mouse.c | 84 ++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 51 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/nvim/mouse.c b/src/nvim/mouse.c index d908a022f1..e4dc47efde 100644 --- a/src/nvim/mouse.c +++ b/src/nvim/mouse.c @@ -609,7 +609,7 @@ bool mouse_scroll_horiz(int dir) } // Adjust the clicked column position if there are concealed characters -// before the current column. But only when it's absolutely necessary. +// before the current column. static int mouse_adjust_click(win_T *wp, int row, int col) { if (!(wp->w_p_cole > 0 && curbuf->b_p_smc > 0 @@ -617,64 +617,82 @@ static int mouse_adjust_click(win_T *wp, int row, int col) return col; } - int end = (colnr_T)STRLEN(ml_get(wp->w_cursor.lnum)); - int vend = getviscol2(end, 0); + char_u *line = ml_get(wp->w_cursor.lnum); + char_u *ptr = line; + char_u *ptr_end = line; + char_u *ptr_row_offset = line; - if (col >= vend) { - return col; + int offset = wp->w_leftcol; + if (row > 0) { + offset += row * (wp->w_width - win_col_off(wp) - win_col_off2(wp) - + wp->w_leftcol + wp->w_skipcol); } - int i = wp->w_leftcol; + if (offset) { + // Skip everything up to an offset since nvim takes care of displaying the + // correct portion of the line when horizontally scrolling. + // When 'wrap' is enabled, only the row (of the wrapped line) needs to be + // checked for concealed characters. + while (offset--) { + ptr += utf_ptr2len(ptr); + } + ptr_row_offset = ptr; + } - if (row > 0) { - i += row * (wp->w_width - win_col_off(wp) - win_col_off2(wp) - - wp->w_leftcol) + wp->w_skipcol; + for (int i = 0; i < col; i++) { + ptr_end += utf_ptr2len(ptr_end); } - int start_col = i; int matchid; - int last_matchid; - int bcol = end - (vend - col); - - while (i < bcol) { - matchid = syn_get_concealed_id(wp, wp->w_cursor.lnum, i); - + int prev_matchid; + int len = 0; + int prev_len = 0; + +#define incr() col++; ptr_end += utf_ptr2len(ptr_end) +#define decr() col--; ptr_end -= utf_ptr2len(ptr_end) + + while (ptr < ptr_end) { + prev_len = len; + len = utf_ptr2len(ptr); + matchid = syn_get_concealed_id(wp, wp->w_cursor.lnum, + (colnr_T)(ptr - line)); if (matchid != 0) { if (wp->w_p_cole == 3) { - bcol++; + incr(); } else { - if (row > 0 && i == start_col) { + if (row > 0 && ptr == ptr_row_offset) { // Check if the current concealed character is actually part of // the previous wrapped row's conceal group. - last_matchid = syn_get_concealed_id(wp, wp->w_cursor.lnum, - i - 1); - if (last_matchid == matchid) { - bcol++; + prev_matchid = syn_get_concealed_id(wp, wp->w_cursor.lnum, + (colnr_T)((ptr - line) + - prev_len)); + if (prev_matchid == matchid) { + incr(); } } else if (wp->w_p_cole == 1 || (wp->w_p_cole == 2 && (lcs_conceal != NUL || syn_get_sub_char() != NUL))) { // At least one placeholder character will be displayed. - bcol--; + decr(); } - last_matchid = matchid; - - // Adjust for concealed text that spans more than one character. + prev_matchid = matchid; do { - i++; - bcol++; - matchid = syn_get_concealed_id(wp, wp->w_cursor.lnum, i); - } while (last_matchid == matchid); + incr(); + ptr += len; + prev_len = len; + len = utf_ptr2len(ptr); + matchid = syn_get_concealed_id(wp, wp->w_cursor.lnum, + (colnr_T)(ptr - line)); + } while (prev_matchid == matchid); continue; } } - i++; + ptr += len; } - return getviscol2(bcol, 0); + return col; } - -- cgit From d2ff5d5bb0807be17211b5e3e86da16038d23537 Mon Sep 17 00:00:00 2001 From: Tommy Allen Date: Thu, 25 Jan 2018 10:28:14 -0500 Subject: Use utfc_ptr2len instead of utf_ptr2len --- src/nvim/mouse.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/nvim/mouse.c b/src/nvim/mouse.c index e4dc47efde..ea3505aec1 100644 --- a/src/nvim/mouse.c +++ b/src/nvim/mouse.c @@ -634,13 +634,13 @@ static int mouse_adjust_click(win_T *wp, int row, int col) // When 'wrap' is enabled, only the row (of the wrapped line) needs to be // checked for concealed characters. while (offset--) { - ptr += utf_ptr2len(ptr); + ptr += utfc_ptr2len(ptr); } ptr_row_offset = ptr; } for (int i = 0; i < col; i++) { - ptr_end += utf_ptr2len(ptr_end); + ptr_end += utfc_ptr2len(ptr_end); } int matchid; @@ -648,12 +648,12 @@ static int mouse_adjust_click(win_T *wp, int row, int col) int len = 0; int prev_len = 0; -#define incr() col++; ptr_end += utf_ptr2len(ptr_end) -#define decr() col--; ptr_end -= utf_ptr2len(ptr_end) +#define incr() col++; ptr_end += utfc_ptr2len(ptr_end) +#define decr() col--; ptr_end -= utfc_ptr2len(ptr_end) while (ptr < ptr_end) { prev_len = len; - len = utf_ptr2len(ptr); + len = utfc_ptr2len(ptr); matchid = syn_get_concealed_id(wp, wp->w_cursor.lnum, (colnr_T)(ptr - line)); if (matchid != 0) { @@ -682,7 +682,7 @@ static int mouse_adjust_click(win_T *wp, int row, int col) incr(); ptr += len; prev_len = len; - len = utf_ptr2len(ptr); + len = utfc_ptr2len(ptr); matchid = syn_get_concealed_id(wp, wp->w_cursor.lnum, (colnr_T)(ptr - line)); } while (prev_matchid == matchid); -- cgit From 60d035311945c3cf7b87cc3838091f7cd97944f1 Mon Sep 17 00:00:00 2001 From: Tommy Allen Date: Thu, 25 Jan 2018 10:33:44 -0500 Subject: Don't move cursor to the left if on the first column of wrapped line ref: #7887 --- src/nvim/mouse.c | 20 ++++---------------- 1 file changed, 4 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/nvim/mouse.c b/src/nvim/mouse.c index ea3505aec1..d2b33b03d7 100644 --- a/src/nvim/mouse.c +++ b/src/nvim/mouse.c @@ -646,13 +646,11 @@ static int mouse_adjust_click(win_T *wp, int row, int col) int matchid; int prev_matchid; int len = 0; - int prev_len = 0; #define incr() col++; ptr_end += utfc_ptr2len(ptr_end) #define decr() col--; ptr_end -= utfc_ptr2len(ptr_end) while (ptr < ptr_end) { - prev_len = len; len = utfc_ptr2len(ptr); matchid = syn_get_concealed_id(wp, wp->w_cursor.lnum, (colnr_T)(ptr - line)); @@ -660,19 +658,10 @@ static int mouse_adjust_click(win_T *wp, int row, int col) if (wp->w_p_cole == 3) { incr(); } else { - if (row > 0 && ptr == ptr_row_offset) { - // Check if the current concealed character is actually part of - // the previous wrapped row's conceal group. - prev_matchid = syn_get_concealed_id(wp, wp->w_cursor.lnum, - (colnr_T)((ptr - line) - - prev_len)); - if (prev_matchid == matchid) { - incr(); - } - } else if (wp->w_p_cole == 1 - || (wp->w_p_cole == 2 - && (lcs_conceal != NUL - || syn_get_sub_char() != NUL))) { + if (!(row > 0 && ptr == ptr_row_offset) + && (wp->w_p_cole == 1 || (wp->w_p_cole == 2 + && (lcs_conceal != NUL + || syn_get_sub_char() != NUL)))) { // At least one placeholder character will be displayed. decr(); } @@ -681,7 +670,6 @@ static int mouse_adjust_click(win_T *wp, int row, int col) do { incr(); ptr += len; - prev_len = len; len = utfc_ptr2len(ptr); matchid = syn_get_concealed_id(wp, wp->w_cursor.lnum, (colnr_T)(ptr - line)); -- cgit From caf87f597febb90728288865d8df48c35c4a78e3 Mon Sep 17 00:00:00 2001 From: Tommy Allen Date: Fri, 26 Jan 2018 01:39:20 -0500 Subject: Adjust cursor according to character display widths --- src/nvim/mouse.c | 68 ++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 49 insertions(+), 19 deletions(-) (limited to 'src') diff --git a/src/nvim/mouse.c b/src/nvim/mouse.c index d2b33b03d7..9bba165299 100644 --- a/src/nvim/mouse.c +++ b/src/nvim/mouse.c @@ -608,8 +608,7 @@ bool mouse_scroll_horiz(int dir) return leftcol_changed(); } -// Adjust the clicked column position if there are concealed characters -// before the current column. +/// Adjusts the clicked column position when 'conceallevel' > 0 static int mouse_adjust_click(win_T *wp, int row, int col) { if (!(wp->w_p_cole > 0 && curbuf->b_p_smc > 0 @@ -617,43 +616,76 @@ static int mouse_adjust_click(win_T *wp, int row, int col) return col; } - char_u *line = ml_get(wp->w_cursor.lnum); + // `col` is the position within the current line that is highlighted by the + // cursor without consideration for concealed characters. The current line is + // scanned *up to* `col`, nudging it left or right when concealed characters + // are encountered. + // + // chartabsize() is used to keep track of the virtual column position relative + // to the line's bytes. For example: if col == 9 and the line starts with a + // tab that's 8 columns wide, we would want the cursor to be highlighting the + // second byte, not the ninth. + + linenr_T lnum = wp->w_cursor.lnum; + char_u *line = ml_get(lnum); char_u *ptr = line; char_u *ptr_end = line; - char_u *ptr_row_offset = line; + char_u *ptr_row_offset = line; // Where we begin adjusting `ptr_end` + // Find the offset where scanning should begin. int offset = wp->w_leftcol; if (row > 0) { offset += row * (wp->w_width - win_col_off(wp) - win_col_off2(wp) - wp->w_leftcol + wp->w_skipcol); } + int vcol; + if (offset) { // Skip everything up to an offset since nvim takes care of displaying the // correct portion of the line when horizontally scrolling. // When 'wrap' is enabled, only the row (of the wrapped line) needs to be // checked for concealed characters. - while (offset--) { + vcol = 0; + while (vcol < offset && *ptr != NUL) { + vcol += chartabsize(ptr, vcol); ptr += utfc_ptr2len(ptr); } + ptr_row_offset = ptr; } - for (int i = 0; i < col; i++) { + // Align `ptr_end` with `col` + vcol = offset; + ptr_end = ptr_row_offset; + while (vcol < col && *ptr_end != NUL) { + vcol += chartabsize(ptr_end, vcol); ptr_end += utfc_ptr2len(ptr_end); } int matchid; int prev_matchid; - int len = 0; - -#define incr() col++; ptr_end += utfc_ptr2len(ptr_end) -#define decr() col--; ptr_end -= utfc_ptr2len(ptr_end) + int nudge = 0; + int cwidth = 0; + + vcol = offset; + +#define incr() col++; nudge++; ptr_end += utfc_ptr2len(ptr_end) +#define decr() col--; nudge--; ptr_end -= utfc_ptr2len(ptr_end) + + while (ptr < ptr_end && *ptr != NUL) { + cwidth = chartabsize(ptr, vcol); + vcol += cwidth; + if (cwidth > 1 && *ptr == '\t' && nudge > 0) { + // A tab will "absorb" any previous adjustments. + cwidth = MIN(cwidth, nudge); + while (cwidth > 0) { + decr(); + cwidth--; + } + } - while (ptr < ptr_end) { - len = utfc_ptr2len(ptr); - matchid = syn_get_concealed_id(wp, wp->w_cursor.lnum, - (colnr_T)(ptr - line)); + matchid = syn_get_concealed_id(wp, lnum, (colnr_T)(ptr - line)); if (matchid != 0) { if (wp->w_p_cole == 3) { incr(); @@ -669,17 +701,15 @@ static int mouse_adjust_click(win_T *wp, int row, int col) prev_matchid = matchid; do { incr(); - ptr += len; - len = utfc_ptr2len(ptr); - matchid = syn_get_concealed_id(wp, wp->w_cursor.lnum, - (colnr_T)(ptr - line)); + ptr += utfc_ptr2len(ptr); + matchid = syn_get_concealed_id(wp, lnum, (colnr_T)(ptr - line)); } while (prev_matchid == matchid); continue; } } - ptr += len; + ptr += utfc_ptr2len(ptr); } return col; -- cgit From be7990bb49d73c56a4aba6c7ce9509d116b77348 Mon Sep 17 00:00:00 2001 From: Tommy Allen Date: Fri, 26 Jan 2018 16:10:16 -0500 Subject: Use one variable for tracking adjustment instead of two --- src/nvim/mouse.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/nvim/mouse.c b/src/nvim/mouse.c index 9bba165299..1e1fbe7a4d 100644 --- a/src/nvim/mouse.c +++ b/src/nvim/mouse.c @@ -670,8 +670,8 @@ static int mouse_adjust_click(win_T *wp, int row, int col) vcol = offset; -#define incr() col++; nudge++; ptr_end += utfc_ptr2len(ptr_end) -#define decr() col--; nudge--; ptr_end -= utfc_ptr2len(ptr_end) +#define incr() nudge++; ptr_end += utfc_ptr2len(ptr_end) +#define decr() nudge--; ptr_end -= utfc_ptr2len(ptr_end) while (ptr < ptr_end && *ptr != NUL) { cwidth = chartabsize(ptr, vcol); @@ -712,5 +712,5 @@ static int mouse_adjust_click(win_T *wp, int row, int col) ptr += utfc_ptr2len(ptr); } - return col; + return col + nudge; } -- cgit From 2eb8dc40acc82231dcb8a6bfd1900a1640a2118a Mon Sep 17 00:00:00 2001 From: Tommy Allen Date: Sat, 27 Jan 2018 00:20:18 -0500 Subject: Safer loop for skipping consecutive concealed chars --- src/nvim/mouse.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/nvim/mouse.c b/src/nvim/mouse.c index 1e1fbe7a4d..6f636f643a 100644 --- a/src/nvim/mouse.c +++ b/src/nvim/mouse.c @@ -699,11 +699,12 @@ static int mouse_adjust_click(win_T *wp, int row, int col) } prev_matchid = matchid; - do { + + while (prev_matchid == matchid && *ptr != NUL) { incr(); ptr += utfc_ptr2len(ptr); matchid = syn_get_concealed_id(wp, lnum, (colnr_T)(ptr - line)); - } while (prev_matchid == matchid); + } continue; } -- cgit