Patch 8.2.4555

10 views
Skip to first unread message

Bram Moolenaar

unread,
Mar 13, 2022, 8:08:37 AM3/13/22
to vim...@googlegroups.com

Patch 8.2.4555
Problem: getmousepos() returns the wrong column. (Ernie Rael)
Solution: Limit to the text size, not the number of bytes.
Files: src/mouse.c, src/testdir/test_functions.vim


*** ../vim-8.2.4554/src/mouse.c 2021-11-29 17:37:38.063265208 +0000
--- src/mouse.c 2022-03-13 11:54:16.919648342 +0000
***************
*** 3099,3115 ****
col -= left_off;
if (row >= 0 && row < wp->w_height && col >= 0 && col < wp->w_width)
{
- char_u *p;
int count;

mouse_comp_pos(wp, &row, &col, &line, NULL);

! // limit to text length plus one
! p = ml_get_buf(wp->w_buffer, line, FALSE);
! count = (int)STRLEN(p);
if (col > count)
col = count;
-
column = col + 1;
}
}
--- 3099,3112 ----
col -= left_off;
if (row >= 0 && row < wp->w_height && col >= 0 && col < wp->w_width)
{
int count;

mouse_comp_pos(wp, &row, &col, &line, NULL);

! // limit to text size plus one
! count = linetabsize(ml_get_buf(wp->w_buffer, line, FALSE));
if (col > count)
col = count;
column = col + 1;
}
}
*** ../vim-8.2.4554/src/testdir/test_functions.vim 2022-02-20 19:05:06.359955597 +0000
--- src/testdir/test_functions.vim 2022-03-13 12:02:00.249657121 +0000
***************
*** 2750,2755 ****
--- 2750,2781 ----
call assert_equal([0, 0, 0, 0, 0], getcurpos(1999))
endfunc

+ func Test_getmousepos()
+ enew!
+ call setline(1, "\t\t\t1234")
+ call test_setmouse(1, 25)
+ call assert_equal(#{
+ \ screenrow: 1,
+ \ screencol: 25,
+ \ winid: win_getid(),
+ \ winrow: 1,
+ \ wincol: 25,
+ \ line: 1,
+ \ column: 25,
+ \ }, getmousepos())
+ call test_setmouse(1, 50)
+ call assert_equal(#{
+ \ screenrow: 1,
+ \ screencol: 50,
+ \ winid: win_getid(),
+ \ winrow: 1,
+ \ wincol: 50,
+ \ line: 1,
+ \ column: 29,
+ \ }, getmousepos())
+ bwipe!
+ endfunc
+
" Test for glob()
func Test_glob()
call assert_equal('', glob(test_null_string()))
*** ../vim-8.2.4554/src/version.c 2022-03-12 21:28:18.532257720 +0000
--- src/version.c 2022-03-13 12:00:39.170223010 +0000
***************
*** 752,753 ****
--- 752,755 ----
{ /* Add new patch number below this line */
+ /**/
+ 4555,
/**/

--
hundred-and-one symptoms of being an internet addict:
245. You use Real Audio to listen to a radio station from a distant
city rather than turn on your stereo system.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Ernie Rael

unread,
Mar 13, 2022, 10:43:21 AM3/13/22
to vim...@googlegroups.com
The problem persists: Included patches: 1-4557.

The problem seems to be that tabs are not taken into account when
calculating column.

With the line, like listmode, starts with 3 tabs
^I^I^Ix
click on the x and getmousepos return column 25
but with the cursor on 'x' "getpos('.')" returns 4


My goal is to intercept a mouse click, if it is in a special area, want
to do something special, otherwise set the cursor position to where
there there was a click, like default behavior.

def Click()
    var d = getmousepos()
    echo d['column']
    win_gotoid(d['winid'])
    cursor(d['line'], d['column'])
enddef

nnoremap <LeftMouse> :MyClick<CR>
command! -nargs=0 MyClick Click()


-ernie

Bram Moolenaar

unread,
Mar 13, 2022, 11:51:57 AM3/13/22
to vim...@googlegroups.com, Ernie Rael

Ernie Rael wrote:

> The problem persists: Included patches: 1-4557.
>
> The problem seems to be that tabs are not taken into account when
> calculating column.
>
> With the line, like listmode, starts with 3 tabs
> ^I^I^Ix
> click on the x and getmousepos return column 25
> but with the cursor on 'x' "getpos('.')" returns 4
>
>
> My goal is to intercept a mouse click, if it is in a special area, want
> to do something special, otherwise set the cursor position to where
> there there was a click, like default behavior.
>
> def Click()
>     var d = getmousepos()
>     echo d['column']
>     win_gotoid(d['winid'])
>     cursor(d['line'], d['column'])
> enddef
>
> nnoremap <LeftMouse> :MyClick<CR>
> command! -nargs=0 MyClick Click()

Hmm, reading the help text for getmousepos() again, it looks like the
intention for "column" was to actually get the byte index of where the
mouse is. But it currently is set to the screen column. And then
it was limited to the number of bytes, which doesn't work. I fixed that
last problem, but that goes against what the help says.

I can change it to return the byte index, which will make it work for
you. It is not backwards compatible, but considering that it was not
returning the correct value, I doubt that someone depended on it.

--
hundred-and-one symptoms of being an internet addict:
250. You've given up the search for the "perfect woman" and instead,
sit in front of the PC until you're just too tired to care.

Ernie Rael

unread,
Mar 13, 2022, 1:07:55 PM3/13/22
to vim...@googlegroups.com
Works ok now.

There was probably use of column when screencol or wincol was intended,
so there might be issues...

>

Reply all
Reply to author
Forward
0 new messages