Skip to content

Commit f5a94d5

Browse files
zeertzjqchrisbra
authored andcommitted
patch 9.0.2032: cannot get mouse click pos for tab or virt text
Problem: Cannot accurately get mouse clicking position when clicking on a TAB or with virtual text. Solution: Add a "coladd" field to getmousepos() result. closes: #13335 Signed-off-by: Christian Brabandt <[email protected]> Co-authored-by: zeertzjq <[email protected]>
1 parent 3c81f47 commit f5a94d5

File tree

8 files changed

+51
-14
lines changed

8 files changed

+51
-14
lines changed

runtime/doc/builtin.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3928,6 +3928,8 @@ getmousepos() *getmousepos()*
39283928
wincol column inside "winid"
39293929
line text line inside "winid"
39303930
column text column inside "winid"
3931+
coladd offset (in screen columns) from the
3932+
start of the clicked char
39313933
All numbers are 1-based.
39323934

39333935
If not over a window, e.g. when in the command line, then only

src/beval.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ find_word_under_cursor(
7474
}
7575
}
7676

77-
col = vcol2col(wp, lnum, col);
77+
col = vcol2col(wp, lnum, col, NULL);
7878
scol = col;
7979

8080
if (VIsual_active

src/mouse.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,7 @@ get_fpos_of_mouse(pos_T *mpos)
172172
if (mouse_comp_pos(curwin, &row, &col, &mpos->lnum, NULL))
173173
return IN_STATUS_LINE; // past bottom
174174

175-
mpos->col = vcol2col(wp, mpos->lnum, col);
176-
177-
mpos->coladd = 0;
175+
mpos->col = vcol2col(wp, mpos->lnum, col, &mpos->coladd);
178176
return IN_BUFFER;
179177
}
180178
#endif
@@ -3204,7 +3202,7 @@ mouse_find_win(int *rowp, int *colp, mouse_find_T popup UNUSED)
32043202
* The first column is zero.
32053203
*/
32063204
int
3207-
vcol2col(win_T *wp, linenr_T lnum, int vcol)
3205+
vcol2col(win_T *wp, linenr_T lnum, int vcol, colnr_T *coladdp)
32083206
{
32093207
char_u *line;
32103208
chartabsize_T cts;
@@ -3222,6 +3220,8 @@ vcol2col(win_T *wp, linenr_T lnum, int vcol)
32223220
}
32233221
clear_chartabsize_arg(&cts);
32243222

3223+
if (coladdp != NULL)
3224+
*coladdp = vcol - cts.cts_vcol;
32253225
return (int)(cts.cts_ptr - line);
32263226
}
32273227
#endif
@@ -3242,6 +3242,7 @@ f_getmousepos(typval_T *argvars UNUSED, typval_T *rettv)
32423242
varnumber_T wincol = 0;
32433243
linenr_T lnum = 0;
32443244
varnumber_T column = 0;
3245+
colnr_T coladd = 0;
32453246

32463247
if (rettv_dict_alloc(rettv) == FAIL)
32473248
return;
@@ -3275,7 +3276,7 @@ f_getmousepos(typval_T *argvars UNUSED, typval_T *rettv)
32753276
if (row >= 0 && row < wp->w_height && col >= 0 && col < wp->w_width)
32763277
{
32773278
(void)mouse_comp_pos(wp, &row, &col, &lnum, NULL);
3278-
col = vcol2col(wp, lnum, col);
3279+
col = vcol2col(wp, lnum, col, &coladd);
32793280
column = col + 1;
32803281
}
32813282
}
@@ -3285,5 +3286,6 @@ f_getmousepos(typval_T *argvars UNUSED, typval_T *rettv)
32853286
dict_add_number(d, "wincol", wincol);
32863287
dict_add_number(d, "line", (varnumber_T)lnum);
32873288
dict_add_number(d, "column", column);
3289+
dict_add_number(d, "coladd", coladd);
32883290
}
32893291
#endif

src/move.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1547,7 +1547,7 @@ f_screenpos(typval_T *argvars UNUSED, typval_T *rettv)
15471547
static int
15481548
virtcol2col(win_T *wp, linenr_T lnum, int vcol)
15491549
{
1550-
int offset = vcol2col(wp, lnum, vcol - 1);
1550+
int offset = vcol2col(wp, lnum, vcol - 1, NULL);
15511551
char_u *line = ml_get_buf(wp->w_buffer, lnum, FALSE);
15521552
char_u *p = line + offset;
15531553

src/proto/mouse.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ void reset_held_button(void);
2121
int check_termcode_mouse(char_u *tp, int *slen, char_u *key_name, char_u *modifiers_start, int idx, int *modifiers);
2222
int mouse_comp_pos(win_T *win, int *rowp, int *colp, linenr_T *lnump, int *plines_cache);
2323
win_T *mouse_find_win(int *rowp, int *colp, mouse_find_T popup);
24-
int vcol2col(win_T *wp, linenr_T lnum, int vcol);
24+
int vcol2col(win_T *wp, linenr_T lnum, int vcol, colnr_T *coladdp);
2525
void f_getmousepos(typval_T *argvars, typval_T *rettv);
2626
/* vim: set ft=c : */

src/testdir/test_functions.vim

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3365,6 +3365,7 @@ func Test_getmousepos()
33653365
\ wincol: 1,
33663366
\ line: 1,
33673367
\ column: 1,
3368+
\ coladd: 0,
33683369
\ }, getmousepos())
33693370
call test_setmouse(1, 2)
33703371
call assert_equal(#{
@@ -3375,6 +3376,7 @@ func Test_getmousepos()
33753376
\ wincol: 2,
33763377
\ line: 1,
33773378
\ column: 1,
3379+
\ coladd: 1,
33783380
\ }, getmousepos())
33793381
call test_setmouse(1, 8)
33803382
call assert_equal(#{
@@ -3385,6 +3387,7 @@ func Test_getmousepos()
33853387
\ wincol: 8,
33863388
\ line: 1,
33873389
\ column: 1,
3390+
\ coladd: 7,
33883391
\ }, getmousepos())
33893392
call test_setmouse(1, 9)
33903393
call assert_equal(#{
@@ -3395,6 +3398,7 @@ func Test_getmousepos()
33953398
\ wincol: 9,
33963399
\ line: 1,
33973400
\ column: 2,
3401+
\ coladd: 0,
33983402
\ }, getmousepos())
33993403
call test_setmouse(1, 12)
34003404
call assert_equal(#{
@@ -3405,6 +3409,7 @@ func Test_getmousepos()
34053409
\ wincol: 12,
34063410
\ line: 1,
34073411
\ column: 2,
3412+
\ coladd: 3,
34083413
\ }, getmousepos())
34093414
call test_setmouse(1, 25)
34103415
call assert_equal(#{
@@ -3415,6 +3420,29 @@ func Test_getmousepos()
34153420
\ wincol: 25,
34163421
\ line: 1,
34173422
\ column: 4,
3423+
\ coladd: 0,
3424+
\ }, getmousepos())
3425+
call test_setmouse(1, 28)
3426+
call assert_equal(#{
3427+
\ screenrow: 1,
3428+
\ screencol: 28,
3429+
\ winid: win_getid(),
3430+
\ winrow: 1,
3431+
\ wincol: 28,
3432+
\ line: 1,
3433+
\ column: 7,
3434+
\ coladd: 0,
3435+
\ }, getmousepos())
3436+
call test_setmouse(1, 29)
3437+
call assert_equal(#{
3438+
\ screenrow: 1,
3439+
\ screencol: 29,
3440+
\ winid: win_getid(),
3441+
\ winrow: 1,
3442+
\ wincol: 29,
3443+
\ line: 1,
3444+
\ column: 8,
3445+
\ coladd: 0,
34183446
\ }, getmousepos())
34193447
call test_setmouse(1, 50)
34203448
call assert_equal(#{
@@ -3425,6 +3453,7 @@ func Test_getmousepos()
34253453
\ wincol: 50,
34263454
\ line: 1,
34273455
\ column: 8,
3456+
\ coladd: 21,
34283457
\ }, getmousepos())
34293458

34303459
" If the mouse is positioned past the last buffer line, "line" and "column"
@@ -3438,6 +3467,7 @@ func Test_getmousepos()
34383467
\ wincol: 25,
34393468
\ line: 1,
34403469
\ column: 4,
3470+
\ coladd: 0,
34413471
\ }, getmousepos())
34423472
call test_setmouse(2, 50)
34433473
call assert_equal(#{
@@ -3448,6 +3478,7 @@ func Test_getmousepos()
34483478
\ wincol: 50,
34493479
\ line: 1,
34503480
\ column: 8,
3481+
\ coladd: 21,
34513482
\ }, getmousepos())
34523483
bwipe!
34533484
endfunc

src/testdir/test_popupwin.vim

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2671,21 +2671,21 @@ func Test_popupwin_filter_mouse()
26712671
eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
26722672
\ screenrow: a:row, screencol: a:col,
26732673
\ winid: win_getid(), winrow: a:row, wincol: a:col,
2674-
\ line: a:row, column: a:col,
2674+
\ line: a:row, column: a:col, coladd: 0,
26752675
\ }})
26762676
endfunc
26772677
func AddItemInPopupBorder(tests, winid, row, col)
26782678
eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
26792679
\ screenrow: a:row, screencol: a:col,
26802680
\ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2681-
\ line: 0, column: 0,
2681+
\ line: 0, column: 0, coladd: 0,
26822682
\ }})
26832683
endfunc
2684-
func AddItemInPopupText(tests, winid, row, col, textline, textcol)
2684+
func AddItemInPopupText(tests, winid, row, col, textline, textcol, coladd = 0)
26852685
eval a:tests->add(#{clickrow: a:row, clickcol: a:col, result: #{
26862686
\ screenrow: a:row, screencol: a:col,
26872687
\ winid: a:winid, winrow: a:row - 1, wincol: a:col - 3,
2688-
\ line: a:textline, column: a:textcol,
2688+
\ line: a:textline, column: a:textcol, coladd: a:coladd,
26892689
\ }})
26902690
endfunc
26912691

@@ -2717,7 +2717,7 @@ func Test_popupwin_filter_mouse()
27172717
call AddItemInPopupText(tests, winid, 4, 6, 1, 1)
27182718
call AddItemInPopupText(tests, winid, 4, 10, 1, 5)
27192719
call AddItemInPopupText(tests, winid, 4, 11, 1, 6)
2720-
call AddItemInPopupText(tests, winid, 4, 17, 1, 6)
2720+
call AddItemInPopupText(tests, winid, 4, 17, 1, 6, 6)
27212721
" text "long line th"
27222722
call AddItemInPopupText(tests, winid, 5, 6, 2, 1)
27232723
call AddItemInPopupText(tests, winid, 5, 10, 2, 5)
@@ -2730,7 +2730,7 @@ func Test_popupwin_filter_mouse()
27302730
call AddItemInPopupText(tests, winid, 7, 6, 3, 1)
27312731
call AddItemInPopupText(tests, winid, 7, 10, 3, 5)
27322732
call AddItemInPopupText(tests, winid, 7, 11, 3, 6)
2733-
call AddItemInPopupText(tests, winid, 7, 17, 3, 6)
2733+
call AddItemInPopupText(tests, winid, 7, 17, 3, 6, 6)
27342734

27352735
for item in tests
27362736
call test_setmouse(item.clickrow, item.clickcol)

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,8 @@ static char *(features[]) =
704704

705705
static int included_patches[] =
706706
{ /* Add new patch number below this line */
707+
/**/
708+
2032,
707709
/**/
708710
2031,
709711
/**/

0 commit comments

Comments
 (0)