From 9f5c5abd425e8112a01ce2c1786e45f40998aea7 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 6 Dec 2022 06:51:17 +0800 Subject: [PATCH 1/6] vim-patch:8.2.3193: screenpos() is wrong when 'display' is "lastline" Problem: screenpos() is wrong when the last line is partially visible and 'display' is "lastline". Solution: Also compute the position for a partially visible line. (closes vim/vim#8599) https://github.com/vim/vim/commit/189663bdac1156237c49925f77bd197c1bdea12c Co-authored-by: Bram Moolenaar (cherry picked from commit 11d2704274ff817678e29f115ba1f074a52e519c) --- src/nvim/move.c | 8 +++--- src/nvim/testdir/test_cursor_func.vim | 40 +++++++++++++++++++-------- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/src/nvim/move.c b/src/nvim/move.c index 481746881b..a5faab3fdd 100644 --- a/src/nvim/move.c +++ b/src/nvim/move.c @@ -928,7 +928,7 @@ void textpos2screenpos(win_T *wp, pos_T *pos, int *rowp, int *scolp, int *ccolp, colnr_T coloff = 0; bool visible_row = false; - if (pos->lnum >= wp->w_topline && pos->lnum < wp->w_botline) { + if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline) { row = plines_m_win(wp, wp->w_topline, pos->lnum - 1) + 1; visible_row = true; } else if (pos->lnum < wp->w_topline) { @@ -962,15 +962,15 @@ void textpos2screenpos(win_T *wp, pos_T *pos, int *rowp, int *scolp, int *ccolp, col -= wp->w_leftcol; - if (col >= 0 && col < wp->w_width) { + if (col >= 0 && col < wp->w_width && row + rowoff <= wp->w_height) { coloff = col - scol + (local ? 0 : wp->w_wincol + wp->w_wincol_off) + 1; } else { + // character is left, right or below of the window scol = ccol = ecol = 0; - // character is left or right of the window if (local) { coloff = col < 0 ? -1 : wp->w_width_inner + 1; } else { - row = 0; + row = rowoff = 0; } } } diff --git a/src/nvim/testdir/test_cursor_func.vim b/src/nvim/testdir/test_cursor_func.vim index 2e625f2388..57e4b06ab3 100644 --- a/src/nvim/testdir/test_cursor_func.vim +++ b/src/nvim/testdir/test_cursor_func.vim @@ -82,25 +82,41 @@ func Test_screenpos() let winid = win_getid() let [winrow, wincol] = win_screenpos(winid) call assert_equal({'row': winrow, - \ 'col': wincol + 0, - \ 'curscol': wincol + 7, - \ 'endcol': wincol + 7}, winid->screenpos(1, 1)) + \ 'col': wincol + 0, + \ 'curscol': wincol + 7, + \ 'endcol': wincol + 7}, winid->screenpos(1, 1)) call assert_equal({'row': winrow, - \ 'col': wincol + 13, - \ 'curscol': wincol + 13, - \ 'endcol': wincol + 13}, winid->screenpos(1, 7)) + \ 'col': wincol + 13, + \ 'curscol': wincol + 13, + \ 'endcol': wincol + 13}, winid->screenpos(1, 7)) call assert_equal({'row': winrow + 2, - \ 'col': wincol + 1, - \ 'curscol': wincol + 1, - \ 'endcol': wincol + 1}, screenpos(winid, 2, 22)) + \ 'col': wincol + 1, + \ 'curscol': wincol + 1, + \ 'endcol': wincol + 1}, screenpos(winid, 2, 22)) setlocal number call assert_equal({'row': winrow + 3, - \ 'col': wincol + 9, - \ 'curscol': wincol + 9, - \ 'endcol': wincol + 9}, screenpos(winid, 2, 22)) + \ 'col': wincol + 9, + \ 'curscol': wincol + 9, + \ 'endcol': wincol + 9}, screenpos(winid, 2, 22)) + + let wininfo = getwininfo(winid)[0] + call setline(3, ['x']->repeat(wininfo.height)) + call setline(line('$') + 1, 'x'->repeat(wininfo.width * 3)) + setlocal nonumber display=lastline so=0 + exe "normal G\\" + redraw + call assert_equal({'row': winrow + wininfo.height - 1, + \ 'col': wincol + 7, + \ 'curscol': wincol + 7, + \ 'endcol': wincol + 7}, winid->screenpos(line('$'), 8)) + call assert_equal({'row': winrow - 1, 'col': 0, 'curscol': 0, 'endcol': 0}, + \ winid->screenpos(line('$'), 22)) + close call assert_equal({}, screenpos(999, 1, 1)) + bwipe! + set display& call assert_equal({'col': 1, 'row': 1, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1)) " nmenu WinBar.TEST : From bc4d11cd9da26e52ce7b0d9cbe3ba12eec6b8458 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 6 Dec 2022 07:26:00 +0800 Subject: [PATCH 2/6] fix(float): fix ml_get error with bufpos (cherry picked from commit e120a049f0e6594b317819bbc883d9040729bc93) --- src/nvim/move.c | 2 +- src/nvim/window.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nvim/move.c b/src/nvim/move.c index a5faab3fdd..28f3e0df12 100644 --- a/src/nvim/move.c +++ b/src/nvim/move.c @@ -940,7 +940,7 @@ void textpos2screenpos(win_T *wp, pos_T *pos, int *rowp, int *scolp, int *ccolp, bool existing_row = (pos->lnum > 0 && pos->lnum <= wp->w_buffer->b_ml.ml_line_count); - if ((local && existing_row) || visible_row) { + if ((local || visible_row) && existing_row) { colnr_T off; colnr_T col; int width; diff --git a/src/nvim/window.c b/src/nvim/window.c index f786895bea..ee513bcffe 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -827,7 +827,7 @@ void win_config_float(win_T *wp, FloatConfig fconfig) pos_T pos = { wp->w_float_config.bufpos.lnum + 1, wp->w_float_config.bufpos.col, 0 }; int trow, tcol, tcolc, tcole; - textpos2screenpos(wp, &pos, &trow, &tcol, &tcolc, &tcole, true); + textpos2screenpos(parent, &pos, &trow, &tcol, &tcolc, &tcole, true); row += trow - 1; col += tcol - 1; } From ebfe54617fad1e70ad053d925d5712ebf54cdb01 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 6 Dec 2022 07:26:41 +0800 Subject: [PATCH 3/6] vim-patch:8.2.4204: screenpos() has non-zero row for invisible text Problem: screenpos() has non-zero row for invisible text. Solution: Only add the window row when the text is visible. (closes vim/vim#9618) https://github.com/vim/vim/commit/7924a17791217d50be5a91989a9641bf68e7a735 Co-authored-by: Bram Moolenaar (cherry picked from commit 6f9cda0f0a9d2e38654e4a0afc4701a356efb862) --- src/nvim/move.c | 5 +++-- src/nvim/testdir/test_cursor_func.vim | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/nvim/move.c b/src/nvim/move.c index 28f3e0df12..f4257deaa9 100644 --- a/src/nvim/move.c +++ b/src/nvim/move.c @@ -931,7 +931,7 @@ void textpos2screenpos(win_T *wp, pos_T *pos, int *rowp, int *scolp, int *ccolp, if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline) { row = plines_m_win(wp, wp->w_topline, pos->lnum - 1) + 1; visible_row = true; - } else if (pos->lnum < wp->w_topline) { + } else if (!local || pos->lnum < wp->w_topline) { row = 0; } else { row = wp->w_height_inner; @@ -964,6 +964,7 @@ void textpos2screenpos(win_T *wp, pos_T *pos, int *rowp, int *scolp, int *ccolp, if (col >= 0 && col < wp->w_width && row + rowoff <= wp->w_height) { coloff = col - scol + (local ? 0 : wp->w_wincol + wp->w_wincol_off) + 1; + row += local ? 0 : wp->w_winrow + wp->w_winrow_off; } else { // character is left, right or below of the window scol = ccol = ecol = 0; @@ -974,7 +975,7 @@ void textpos2screenpos(win_T *wp, pos_T *pos, int *rowp, int *scolp, int *ccolp, } } } - *rowp = (local ? 0 : wp->w_winrow + wp->w_winrow_off) + row + rowoff; + *rowp = row + rowoff; *scolp = scol + coloff; *ccolp = ccol + coloff; *ecolp = ecol + coloff; diff --git a/src/nvim/testdir/test_cursor_func.vim b/src/nvim/testdir/test_cursor_func.vim index 57e4b06ab3..12a3306f0a 100644 --- a/src/nvim/testdir/test_cursor_func.vim +++ b/src/nvim/testdir/test_cursor_func.vim @@ -109,7 +109,7 @@ func Test_screenpos() \ 'col': wincol + 7, \ 'curscol': wincol + 7, \ 'endcol': wincol + 7}, winid->screenpos(line('$'), 8)) - call assert_equal({'row': winrow - 1, 'col': 0, 'curscol': 0, 'endcol': 0}, + call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0}, \ winid->screenpos(line('$'), 22)) close From e2afca9c767209ce07b00bb76599926f5d29f564 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 6 Dec 2022 07:41:33 +0800 Subject: [PATCH 4/6] vim-patch:8.2.4389: screenpos() does not handle a position in a closed fold Problem: screenpos() does not handle a position in a closed fold. Solution: Check if the position is inside a closed fold. (closes vim/vim#9778) https://github.com/vim/vim/commit/4556a2e8681c5c98fb4c7ca0a016924a69b4452a Co-authored-by: Bram Moolenaar (cherry picked from commit 10af0549df7ac4ea9907b34624228755b9752318) --- src/nvim/move.c | 10 ++++++++-- src/nvim/testdir/test_cursor_func.vim | 22 ++++++++++++++++++++-- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/nvim/move.c b/src/nvim/move.c index f4257deaa9..7a1c7dd3f2 100644 --- a/src/nvim/move.c +++ b/src/nvim/move.c @@ -927,9 +927,12 @@ void textpos2screenpos(win_T *wp, pos_T *pos, int *rowp, int *scolp, int *ccolp, int rowoff = 0; colnr_T coloff = 0; bool visible_row = false; + bool is_folded = false; if (pos->lnum >= wp->w_topline && pos->lnum <= wp->w_botline) { - row = plines_m_win(wp, wp->w_topline, pos->lnum - 1) + 1; + linenr_T lnum = pos->lnum; + is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, true, NULL); + row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1; visible_row = true; } else if (!local || pos->lnum < wp->w_topline) { row = 0; @@ -940,7 +943,10 @@ void textpos2screenpos(win_T *wp, pos_T *pos, int *rowp, int *scolp, int *ccolp, bool existing_row = (pos->lnum > 0 && pos->lnum <= wp->w_buffer->b_ml.ml_line_count); - if ((local || visible_row) && existing_row) { + if (is_folded) { + row += local ? 0 : wp->w_winrow + wp->w_winrow_off; + coloff = (local ? 0 : wp->w_wincol + wp->w_wincol_off) + 1; + } else if ((local || visible_row) && existing_row) { colnr_T off; colnr_T col; int width; diff --git a/src/nvim/testdir/test_cursor_func.vim b/src/nvim/testdir/test_cursor_func.vim index 12a3306f0a..1b39cff795 100644 --- a/src/nvim/testdir/test_cursor_func.vim +++ b/src/nvim/testdir/test_cursor_func.vim @@ -1,7 +1,10 @@ " Tests for cursor() and other functions that get/set the cursor position +source check.vim + func Test_wrong_arguments() call assert_fails('call cursor(1. 3)', 'E474:') + call assert_fails('call cursor(v:_null_list)', 'E474:') endfunc func Test_move_cursor() @@ -118,14 +121,29 @@ func Test_screenpos() bwipe! set display& - call assert_equal({'col': 1, 'row': 1, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1)) + call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1)) " nmenu WinBar.TEST : setlocal winbar=TEST - call assert_equal({'col': 1, 'row': 2, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1)) + call assert_equal(#{col: 1, row: 2, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1)) " nunmenu WinBar.TEST setlocal winbar& endfunc +func Test_screenpos_fold() + CheckFeature folding + + enew! + call setline(1, range(10)) + 3,5fold + redraw + call assert_equal(2, screenpos(1, 2, 1).row) + call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 3, 1)) + call assert_equal(3, screenpos(1, 4, 1).row) + call assert_equal(3, screenpos(1, 5, 1).row) + call assert_equal(4, screenpos(1, 6, 1).row) + bwipe! +endfunc + func Test_screenpos_number() rightbelow new rightbelow 73vsplit From 65c8851098d3581610ac3faa4406d91929a15e23 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 6 Dec 2022 07:52:00 +0800 Subject: [PATCH 5/6] vim-patch:9.0.1011: ml_get error when using screenpos() Problem: ml_get error when using screenpos(). Solution: Give an error for the line number. (closes vim/vim#11661) https://github.com/vim/vim/commit/99d19438cabaf13074229d9a32e3a4af9ce98744 Co-authored-by: Bram Moolenaar (cherry picked from commit 0909d987fe925483dc513ae330179339899cd0a5) --- src/nvim/globals.h | 2 ++ src/nvim/move.c | 4 ++++ src/nvim/testdir/test_cursor_func.vim | 3 +++ 3 files changed, 9 insertions(+) diff --git a/src/nvim/globals.h b/src/nvim/globals.h index 578ed478c6..d51c20a50e 100644 --- a/src/nvim/globals.h +++ b/src/nvim/globals.h @@ -1009,6 +1009,8 @@ EXTERN char e_highlight_group_name_invalid_char[] INIT(= N_("E5248: Invalid char EXTERN char e_highlight_group_name_too_long[] INIT(= N_("E1249: Highlight group name too long")); +EXTERN char e_invalid_line_number_nr[] INIT(= N_("E966: Invalid line number: %ld")); + EXTERN char e_undobang_cannot_redo_or_move_branch[] INIT(= N_("E5767: Cannot use :undo! to redo or move to a different undo branch")); diff --git a/src/nvim/move.c b/src/nvim/move.c index 7a1c7dd3f2..95cb99a998 100644 --- a/src/nvim/move.c +++ b/src/nvim/move.c @@ -1003,6 +1003,10 @@ void f_screenpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) .col = (colnr_T)tv_get_number(&argvars[2]) - 1, .coladd = 0 }; + if (pos.lnum > wp->w_buffer->b_ml.ml_line_count) { + semsg(_(e_invalid_line_number_nr), pos.lnum); + return; + } int row = 0; int scol = 0, ccol = 0, ecol = 0; textpos2screenpos(wp, &pos, &row, &scol, &ccol, &ecol, false); diff --git a/src/nvim/testdir/test_cursor_func.vim b/src/nvim/testdir/test_cursor_func.vim index 1b39cff795..4d98de81b0 100644 --- a/src/nvim/testdir/test_cursor_func.vim +++ b/src/nvim/testdir/test_cursor_func.vim @@ -155,6 +155,9 @@ func Test_screenpos_number() let pos = screenpos(winid, 1, 66) call assert_equal(winrow, pos.row) call assert_equal(wincol + 66 + 3, pos.col) + + call assert_fails('echo screenpos(0, 2, 1)', 'E966:') + close bwipe! endfunc From 78caeae92f9a1dde4da09f1f0211511f6b012236 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 6 Dec 2022 08:00:30 +0800 Subject: [PATCH 6/6] vim-patch:9.0.1016: screenpos() does not count filler lines for diff mode Problem: screenpos() does not count filler lines for diff mode. Solution: Add filler lines. (closes 11658) https://github.com/vim/vim/commit/1cb16c3a20a9d17df1a8dc3813ef64dc98e42637 Co-authored-by: Bram Moolenaar (cherry picked from commit 52b3e8bdef6896b53d3c4ee3fbc7d8ae5f480948) --- src/nvim/move.c | 2 ++ src/nvim/testdir/test_cursor_func.vim | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/nvim/move.c b/src/nvim/move.c index 95cb99a998..0d6d69d9e9 100644 --- a/src/nvim/move.c +++ b/src/nvim/move.c @@ -933,6 +933,8 @@ void textpos2screenpos(win_T *wp, pos_T *pos, int *rowp, int *scolp, int *ccolp, linenr_T lnum = pos->lnum; is_folded = hasFoldingWin(wp, lnum, &lnum, NULL, true, NULL); row = plines_m_win(wp, wp->w_topline, lnum - 1) + 1; + // Add filler lines above this buffer line. + row += win_get_fill(wp, lnum); visible_row = true; } else if (!local || pos->lnum < wp->w_topline) { row = 0; diff --git a/src/nvim/testdir/test_cursor_func.vim b/src/nvim/testdir/test_cursor_func.vim index 4d98de81b0..40adbe4dcb 100644 --- a/src/nvim/testdir/test_cursor_func.vim +++ b/src/nvim/testdir/test_cursor_func.vim @@ -144,6 +144,22 @@ func Test_screenpos_fold() bwipe! endfunc +func Test_screenpos_diff() + CheckFeature diff + + enew! + call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i']) + vnew + call setline(1, ['a', 'b', 'c', 'g', 'h', 'i']) + windo diffthis + wincmd w + call assert_equal(#{col: 3, row: 7, endcol: 3, curscol: 3}, screenpos(0, 4, 1)) + + windo diffoff + bwipe! + bwipe! +endfunc + func Test_screenpos_number() rightbelow new rightbelow 73vsplit