Bram Moolenaar | 6f02b00 | 2021-01-10 20:22:54 +0100 | [diff] [blame] | 1 | " Tests for cursor() and other functions that get/set the cursor position |
Bram Moolenaar | 5a46a58 | 2016-01-15 15:56:58 +0100 | [diff] [blame] | 2 | |
| 3 | func Test_wrong_arguments() |
Bram Moolenaar | 3717540 | 2017-03-18 20:18:45 +0100 | [diff] [blame] | 4 | call assert_fails('call cursor(1. 3)', 'E474:') |
Bram Moolenaar | 92b83cc | 2020-04-25 15:24:44 +0200 | [diff] [blame] | 5 | call assert_fails('call cursor(test_null_list())', 'E474:') |
Bram Moolenaar | 5a46a58 | 2016-01-15 15:56:58 +0100 | [diff] [blame] | 6 | endfunc |
| 7 | |
| 8 | func Test_move_cursor() |
| 9 | new |
| 10 | call setline(1, ['aaa', 'bbb', 'ccc', 'ddd']) |
| 11 | |
| 12 | call cursor([1, 1, 0, 1]) |
| 13 | call assert_equal([1, 1, 0, 1], getcurpos()[1:]) |
| 14 | call cursor([4, 3, 0, 3]) |
| 15 | call assert_equal([4, 3, 0, 3], getcurpos()[1:]) |
| 16 | |
| 17 | call cursor(2, 2) |
Bram Moolenaar | 2ab375e | 2016-02-10 22:23:06 +0100 | [diff] [blame] | 18 | call assert_equal([2, 2, 0, 2], getcurpos()[1:]) |
Bram Moolenaar | 5a46a58 | 2016-01-15 15:56:58 +0100 | [diff] [blame] | 19 | " line number zero keeps the line number |
| 20 | call cursor(0, 1) |
Bram Moolenaar | 2ab375e | 2016-02-10 22:23:06 +0100 | [diff] [blame] | 21 | call assert_equal([2, 1, 0, 1], getcurpos()[1:]) |
Bram Moolenaar | 5a46a58 | 2016-01-15 15:56:58 +0100 | [diff] [blame] | 22 | " col number zero keeps the column |
| 23 | call cursor(3, 0) |
Bram Moolenaar | 2ab375e | 2016-02-10 22:23:06 +0100 | [diff] [blame] | 24 | call assert_equal([3, 1, 0, 1], getcurpos()[1:]) |
Bram Moolenaar | 5a46a58 | 2016-01-15 15:56:58 +0100 | [diff] [blame] | 25 | " below last line goes to last line |
Bram Moolenaar | 1a3a891 | 2019-08-23 22:31:37 +0200 | [diff] [blame] | 26 | eval [9, 1]->cursor() |
Bram Moolenaar | 2ab375e | 2016-02-10 22:23:06 +0100 | [diff] [blame] | 27 | call assert_equal([4, 1, 0, 1], getcurpos()[1:]) |
Bram Moolenaar | 9ebcf23 | 2021-01-16 16:52:49 +0100 | [diff] [blame] | 28 | " pass string arguments |
| 29 | call cursor('3', '3') |
| 30 | call assert_equal([3, 3, 0, 3], getcurpos()[1:]) |
Bram Moolenaar | 5a46a58 | 2016-01-15 15:56:58 +0100 | [diff] [blame] | 31 | |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 32 | call setline(1, ["\<TAB>"]) |
| 33 | call cursor(1, 1, 1) |
| 34 | call assert_equal([1, 1, 1], getcurpos()[1:3]) |
| 35 | |
Bram Moolenaar | 9a96337 | 2020-12-21 21:58:46 +0100 | [diff] [blame] | 36 | call assert_fails('call cursor(-1, -1)', 'E475:') |
Bram Moolenaar | 17aca70 | 2019-05-16 22:24:55 +0200 | [diff] [blame] | 37 | |
Bram Moolenaar | 5a46a58 | 2016-01-15 15:56:58 +0100 | [diff] [blame] | 38 | quit! |
| 39 | endfunc |
Bram Moolenaar | 2ab375e | 2016-02-10 22:23:06 +0100 | [diff] [blame] | 40 | |
naohiro ono | 56200ee | 2022-01-01 14:59:44 +0000 | [diff] [blame] | 41 | func Test_curswant_maxcol() |
| 42 | new |
| 43 | call setline(1, 'foo') |
| 44 | |
| 45 | " Test that after "$" command curswant is set to the same value as v:maxcol. |
| 46 | normal! 1G$ |
| 47 | call assert_equal(v:maxcol, getcurpos()[4]) |
| 48 | call assert_equal(v:maxcol, winsaveview().curswant) |
| 49 | |
| 50 | quit! |
| 51 | endfunc |
| 52 | |
Bram Moolenaar | 2ab375e | 2016-02-10 22:23:06 +0100 | [diff] [blame] | 53 | " Very short version of what matchparen does. |
| 54 | function s:Highlight_Matching_Pair() |
| 55 | let save_cursor = getcurpos() |
Bram Moolenaar | aad222c | 2019-09-06 22:46:09 +0200 | [diff] [blame] | 56 | eval save_cursor->setpos('.') |
Bram Moolenaar | 2ab375e | 2016-02-10 22:23:06 +0100 | [diff] [blame] | 57 | endfunc |
| 58 | |
| 59 | func Test_curswant_with_autocommand() |
| 60 | new |
| 61 | call setline(1, ['func()', '{', '}', '----']) |
| 62 | autocmd! CursorMovedI * call s:Highlight_Matching_Pair() |
Bram Moolenaar | eb992cb | 2017-03-09 18:20:16 +0100 | [diff] [blame] | 63 | call test_override("char_avail", 1) |
Bram Moolenaar | 2ab375e | 2016-02-10 22:23:06 +0100 | [diff] [blame] | 64 | exe "normal! 3Ga\<Down>X\<Esc>" |
Bram Moolenaar | eb992cb | 2017-03-09 18:20:16 +0100 | [diff] [blame] | 65 | call test_override("char_avail", 0) |
Bram Moolenaar | 2ab375e | 2016-02-10 22:23:06 +0100 | [diff] [blame] | 66 | call assert_equal('-X---', getline(4)) |
| 67 | autocmd! CursorMovedI * |
| 68 | quit! |
| 69 | endfunc |
| 70 | |
Bram Moolenaar | 177ab9e | 2019-01-15 21:12:57 +0100 | [diff] [blame] | 71 | " Tests for behavior of curswant with cursorcolumn/line |
| 72 | func Test_curswant_with_cursorcolumn() |
| 73 | new |
| 74 | call setline(1, ['01234567', '']) |
| 75 | exe "normal! ggf6j" |
| 76 | call assert_equal(6, winsaveview().curswant) |
| 77 | set cursorcolumn |
| 78 | call assert_equal(6, winsaveview().curswant) |
| 79 | quit! |
| 80 | endfunc |
| 81 | |
| 82 | func Test_curswant_with_cursorline() |
| 83 | new |
| 84 | call setline(1, ['01234567', '']) |
| 85 | exe "normal! ggf6j" |
| 86 | call assert_equal(6, winsaveview().curswant) |
| 87 | set cursorline |
| 88 | call assert_equal(6, winsaveview().curswant) |
| 89 | quit! |
| 90 | endfunc |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 91 | |
| 92 | func Test_screenpos() |
| 93 | rightbelow new |
| 94 | rightbelow 20vsplit |
| 95 | call setline(1, ["\tsome text", "long wrapping line here", "next line"]) |
| 96 | redraw |
| 97 | let winid = win_getid() |
| 98 | let [winrow, wincol] = win_screenpos(winid) |
| 99 | call assert_equal({'row': winrow, |
| 100 | \ 'col': wincol + 0, |
| 101 | \ 'curscol': wincol + 7, |
Bram Moolenaar | f92e58c | 2019-09-08 21:51:41 +0200 | [diff] [blame] | 102 | \ 'endcol': wincol + 7}, winid->screenpos(1, 1)) |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 103 | call assert_equal({'row': winrow, |
| 104 | \ 'col': wincol + 13, |
| 105 | \ 'curscol': wincol + 13, |
Bram Moolenaar | 196b466 | 2019-09-06 21:34:30 +0200 | [diff] [blame] | 106 | \ 'endcol': wincol + 13}, winid->screenpos(1, 7)) |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 107 | call assert_equal({'row': winrow + 2, |
| 108 | \ 'col': wincol + 1, |
| 109 | \ 'curscol': wincol + 1, |
| 110 | \ 'endcol': wincol + 1}, screenpos(winid, 2, 22)) |
| 111 | setlocal number |
| 112 | call assert_equal({'row': winrow + 3, |
| 113 | \ 'col': wincol + 9, |
| 114 | \ 'curscol': wincol + 9, |
| 115 | \ 'endcol': wincol + 9}, screenpos(winid, 2, 22)) |
Bram Moolenaar | 189663b | 2021-07-21 18:04:56 +0200 | [diff] [blame] | 116 | |
| 117 | let wininfo = getwininfo(winid)[0] |
| 118 | call setline(3, ['x']->repeat(wininfo.height)) |
| 119 | call setline(line('$') + 1, 'x'->repeat(wininfo.width * 3)) |
| 120 | setlocal nonumber display=lastline so=0 |
| 121 | exe "normal G\<C-Y>\<C-Y>" |
| 122 | redraw |
| 123 | call assert_equal({'row': winrow + wininfo.height - 1, |
| 124 | \ 'col': wincol + 7, |
| 125 | \ 'curscol': wincol + 7, |
| 126 | \ 'endcol': wincol + 7}, winid->screenpos(line('$'), 8)) |
Bram Moolenaar | 7924a17 | 2022-01-24 16:15:15 +0000 | [diff] [blame^] | 127 | call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0}, |
Bram Moolenaar | 189663b | 2021-07-21 18:04:56 +0200 | [diff] [blame] | 128 | \ winid->screenpos(line('$'), 22)) |
| 129 | |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 130 | close |
Bram Moolenaar | bdd2c29 | 2020-06-22 21:34:30 +0200 | [diff] [blame] | 131 | call assert_equal({}, screenpos(999, 1, 1)) |
Bram Moolenaar | 189663b | 2021-07-21 18:04:56 +0200 | [diff] [blame] | 132 | |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 133 | bwipe! |
Bram Moolenaar | 189663b | 2021-07-21 18:04:56 +0200 | [diff] [blame] | 134 | set display& |
Bram Moolenaar | 8dd46e7 | 2020-12-17 21:35:29 +0100 | [diff] [blame] | 135 | |
| 136 | call assert_equal({'col': 1, 'row': 1, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1)) |
| 137 | nmenu WinBar.TEST : |
| 138 | call assert_equal({'col': 1, 'row': 2, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1)) |
| 139 | nunmenu WinBar.TEST |
Bram Moolenaar | b3d17a2 | 2019-07-07 18:28:14 +0200 | [diff] [blame] | 140 | endfunc |
Bram Moolenaar | 38ba4dc | 2019-10-27 21:39:09 +0100 | [diff] [blame] | 141 | |
| 142 | func Test_screenpos_number() |
| 143 | rightbelow new |
| 144 | rightbelow 73vsplit |
| 145 | call setline (1, repeat('x', 66)) |
| 146 | setlocal number |
| 147 | redraw |
| 148 | let winid = win_getid() |
| 149 | let [winrow, wincol] = win_screenpos(winid) |
| 150 | let pos = screenpos(winid, 1, 66) |
| 151 | call assert_equal(winrow, pos.row) |
| 152 | call assert_equal(wincol + 66 + 3, pos.col) |
| 153 | close |
| 154 | bwipe! |
| 155 | endfunc |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 156 | |
Bram Moolenaar | 9145846 | 2021-01-13 20:08:38 +0100 | [diff] [blame] | 157 | " Save the visual start character position |
Bram Moolenaar | 6f02b00 | 2021-01-10 20:22:54 +0100 | [diff] [blame] | 158 | func SaveVisualStartCharPos() |
| 159 | call add(g:VisualStartPos, getcharpos('v')) |
| 160 | return '' |
| 161 | endfunc |
| 162 | |
Bram Moolenaar | 9145846 | 2021-01-13 20:08:38 +0100 | [diff] [blame] | 163 | " Save the current cursor character position in insert mode |
| 164 | func SaveInsertCurrentCharPos() |
| 165 | call add(g:InsertCurrentPos, getcharpos('.')) |
| 166 | return '' |
| 167 | endfunc |
| 168 | |
Bram Moolenaar | 6f02b00 | 2021-01-10 20:22:54 +0100 | [diff] [blame] | 169 | " Test for the getcharpos() function |
| 170 | func Test_getcharpos() |
| 171 | call assert_fails('call getcharpos({})', 'E731:') |
| 172 | call assert_equal([0, 0, 0, 0], getcharpos(0)) |
| 173 | new |
| 174 | call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678']) |
| 175 | |
| 176 | " Test for '.' and '$' |
| 177 | normal 1G |
| 178 | call assert_equal([0, 1, 1, 0], getcharpos('.')) |
| 179 | call assert_equal([0, 4, 1, 0], getcharpos('$')) |
| 180 | normal 2G6l |
| 181 | call assert_equal([0, 2, 7, 0], getcharpos('.')) |
| 182 | normal 3G$ |
| 183 | call assert_equal([0, 3, 1, 0], getcharpos('.')) |
| 184 | normal 4G$ |
| 185 | call assert_equal([0, 4, 9, 0], getcharpos('.')) |
| 186 | |
| 187 | " Test for a mark |
| 188 | normal 2G7lmmgg |
| 189 | call assert_equal([0, 2, 8, 0], getcharpos("'m")) |
| 190 | delmarks m |
| 191 | call assert_equal([0, 0, 0, 0], getcharpos("'m")) |
| 192 | |
| 193 | " Test for the visual start column |
| 194 | vnoremap <expr> <F3> SaveVisualStartCharPos() |
| 195 | let g:VisualStartPos = [] |
| 196 | exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>" |
Bram Moolenaar | 9145846 | 2021-01-13 20:08:38 +0100 | [diff] [blame] | 197 | call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos) |
Bram Moolenaar | 6f02b00 | 2021-01-10 20:22:54 +0100 | [diff] [blame] | 198 | call assert_equal([0, 2, 9, 0], getcharpos('v')) |
| 199 | let g:VisualStartPos = [] |
| 200 | exe "normal 3Gv$\<F3>o\<F3>" |
Bram Moolenaar | 9145846 | 2021-01-13 20:08:38 +0100 | [diff] [blame] | 201 | call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos) |
Bram Moolenaar | 6f02b00 | 2021-01-10 20:22:54 +0100 | [diff] [blame] | 202 | let g:VisualStartPos = [] |
| 203 | exe "normal 1Gv$\<F3>o\<F3>" |
| 204 | call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos) |
| 205 | vunmap <F3> |
| 206 | |
Bram Moolenaar | 9145846 | 2021-01-13 20:08:38 +0100 | [diff] [blame] | 207 | " Test for getting the position in insert mode with the cursor after the |
| 208 | " last character in a line |
| 209 | inoremap <expr> <F3> SaveInsertCurrentCharPos() |
| 210 | let g:InsertCurrentPos = [] |
| 211 | exe "normal 1GA\<F3>" |
| 212 | exe "normal 2GA\<F3>" |
| 213 | exe "normal 3GA\<F3>" |
| 214 | exe "normal 4GA\<F3>" |
| 215 | exe "normal 2G6li\<F3>" |
| 216 | call assert_equal([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0], |
| 217 | \ [0, 2, 7, 0]], g:InsertCurrentPos) |
| 218 | iunmap <F3> |
| 219 | |
Bram Moolenaar | 6f02b00 | 2021-01-10 20:22:54 +0100 | [diff] [blame] | 220 | %bw! |
| 221 | endfunc |
| 222 | |
| 223 | " Test for the setcharpos() function |
| 224 | func Test_setcharpos() |
| 225 | call assert_equal(-1, setcharpos('.', test_null_list())) |
| 226 | new |
| 227 | call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678']) |
| 228 | call setcharpos('.', [0, 1, 1, 0]) |
| 229 | call assert_equal([1, 1], [line('.'), col('.')]) |
| 230 | call setcharpos('.', [0, 2, 7, 0]) |
| 231 | call assert_equal([2, 9], [line('.'), col('.')]) |
| 232 | call setcharpos('.', [0, 3, 4, 0]) |
| 233 | call assert_equal([3, 1], [line('.'), col('.')]) |
| 234 | call setcharpos('.', [0, 3, 1, 0]) |
| 235 | call assert_equal([3, 1], [line('.'), col('.')]) |
| 236 | call setcharpos('.', [0, 4, 0, 0]) |
| 237 | call assert_equal([4, 1], [line('.'), col('.')]) |
| 238 | call setcharpos('.', [0, 4, 20, 0]) |
| 239 | call assert_equal([4, 9], [line('.'), col('.')]) |
| 240 | |
| 241 | " Test for mark |
| 242 | delmarks m |
| 243 | call setcharpos("'m", [0, 2, 9, 0]) |
| 244 | normal `m |
| 245 | call assert_equal([2, 11], [line('.'), col('.')]) |
Bram Moolenaar | 9145846 | 2021-01-13 20:08:38 +0100 | [diff] [blame] | 246 | " unload the buffer and try to set the mark |
| 247 | let bnr = bufnr() |
| 248 | enew! |
| 249 | call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0])) |
Bram Moolenaar | 6f02b00 | 2021-01-10 20:22:54 +0100 | [diff] [blame] | 250 | |
| 251 | %bw! |
| 252 | call assert_equal(-1, setcharpos('.', [10, 3, 1, 0])) |
| 253 | endfunc |
| 254 | |
| 255 | func SaveVisualStartCharCol() |
| 256 | call add(g:VisualStartCol, charcol('v')) |
| 257 | return '' |
| 258 | endfunc |
| 259 | |
Bram Moolenaar | 9145846 | 2021-01-13 20:08:38 +0100 | [diff] [blame] | 260 | func SaveInsertCurrentCharCol() |
| 261 | call add(g:InsertCurrentCol, charcol('.')) |
| 262 | return '' |
| 263 | endfunc |
| 264 | |
Bram Moolenaar | 6f02b00 | 2021-01-10 20:22:54 +0100 | [diff] [blame] | 265 | " Test for the charcol() function |
| 266 | func Test_charcol() |
| 267 | call assert_fails('call charcol({})', 'E731:') |
| 268 | call assert_equal(0, charcol(0)) |
| 269 | new |
| 270 | call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678']) |
| 271 | |
| 272 | " Test for '.' and '$' |
| 273 | normal 1G |
| 274 | call assert_equal(1, charcol('.')) |
| 275 | call assert_equal(1, charcol('$')) |
| 276 | normal 2G6l |
| 277 | call assert_equal(7, charcol('.')) |
| 278 | call assert_equal(10, charcol('$')) |
| 279 | normal 3G$ |
| 280 | call assert_equal(1, charcol('.')) |
| 281 | call assert_equal(2, charcol('$')) |
| 282 | normal 4G$ |
| 283 | call assert_equal(9, charcol('.')) |
| 284 | call assert_equal(10, charcol('$')) |
| 285 | |
| 286 | " Test for [lnum, '$'] |
| 287 | call assert_equal(1, charcol([1, '$'])) |
| 288 | call assert_equal(10, charcol([2, '$'])) |
| 289 | call assert_equal(2, charcol([3, '$'])) |
| 290 | call assert_equal(0, charcol([5, '$'])) |
| 291 | |
| 292 | " Test for a mark |
| 293 | normal 2G7lmmgg |
| 294 | call assert_equal(8, charcol("'m")) |
| 295 | delmarks m |
| 296 | call assert_equal(0, charcol("'m")) |
| 297 | |
| 298 | " Test for the visual start column |
| 299 | vnoremap <expr> <F3> SaveVisualStartCharCol() |
| 300 | let g:VisualStartCol = [] |
| 301 | exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>" |
Bram Moolenaar | 9145846 | 2021-01-13 20:08:38 +0100 | [diff] [blame] | 302 | call assert_equal([7, 10, 5], g:VisualStartCol) |
Bram Moolenaar | 6f02b00 | 2021-01-10 20:22:54 +0100 | [diff] [blame] | 303 | call assert_equal(9, charcol('v')) |
| 304 | let g:VisualStartCol = [] |
| 305 | exe "normal 3Gv$\<F3>o\<F3>" |
Bram Moolenaar | 9145846 | 2021-01-13 20:08:38 +0100 | [diff] [blame] | 306 | call assert_equal([1, 2], g:VisualStartCol) |
Bram Moolenaar | 6f02b00 | 2021-01-10 20:22:54 +0100 | [diff] [blame] | 307 | let g:VisualStartCol = [] |
| 308 | exe "normal 1Gv$\<F3>o\<F3>" |
| 309 | call assert_equal([1, 1], g:VisualStartCol) |
| 310 | vunmap <F3> |
| 311 | |
Bram Moolenaar | 9145846 | 2021-01-13 20:08:38 +0100 | [diff] [blame] | 312 | " Test for getting the column number in insert mode with the cursor after |
| 313 | " the last character in a line |
| 314 | inoremap <expr> <F3> SaveInsertCurrentCharCol() |
| 315 | let g:InsertCurrentCol = [] |
| 316 | exe "normal 1GA\<F3>" |
| 317 | exe "normal 2GA\<F3>" |
| 318 | exe "normal 3GA\<F3>" |
| 319 | exe "normal 4GA\<F3>" |
| 320 | exe "normal 2G6li\<F3>" |
| 321 | call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol) |
| 322 | iunmap <F3> |
| 323 | |
Bram Moolenaar | 6f02b00 | 2021-01-10 20:22:54 +0100 | [diff] [blame] | 324 | %bw! |
| 325 | endfunc |
| 326 | |
Bram Moolenaar | 9145846 | 2021-01-13 20:08:38 +0100 | [diff] [blame] | 327 | func SaveInsertCursorCharPos() |
| 328 | call add(g:InsertCursorPos, getcursorcharpos('.')) |
| 329 | return '' |
| 330 | endfunc |
| 331 | |
Bram Moolenaar | 6f02b00 | 2021-01-10 20:22:54 +0100 | [diff] [blame] | 332 | " Test for getcursorcharpos() |
| 333 | func Test_getcursorcharpos() |
| 334 | call assert_equal(getcursorcharpos(), getcursorcharpos(0)) |
| 335 | call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1)) |
| 336 | call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999)) |
| 337 | |
| 338 | new |
| 339 | call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678']) |
| 340 | normal 1G9l |
| 341 | call assert_equal([0, 1, 1, 0, 1], getcursorcharpos()) |
| 342 | normal 2G9l |
| 343 | call assert_equal([0, 2, 9, 0, 14], getcursorcharpos()) |
| 344 | normal 3G9l |
| 345 | call assert_equal([0, 3, 1, 0, 1], getcursorcharpos()) |
| 346 | normal 4G9l |
| 347 | call assert_equal([0, 4, 9, 0, 9], getcursorcharpos()) |
| 348 | |
Bram Moolenaar | 9145846 | 2021-01-13 20:08:38 +0100 | [diff] [blame] | 349 | " Test for getting the cursor position in insert mode with the cursor after |
| 350 | " the last character in a line |
| 351 | inoremap <expr> <F3> SaveInsertCursorCharPos() |
| 352 | let g:InsertCursorPos = [] |
| 353 | exe "normal 1GA\<F3>" |
| 354 | exe "normal 2GA\<F3>" |
| 355 | exe "normal 3GA\<F3>" |
| 356 | exe "normal 4GA\<F3>" |
| 357 | exe "normal 2G6li\<F3>" |
| 358 | call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2], |
| 359 | \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos) |
| 360 | iunmap <F3> |
| 361 | |
Bram Moolenaar | 6f02b00 | 2021-01-10 20:22:54 +0100 | [diff] [blame] | 362 | let winid = win_getid() |
| 363 | normal 2G5l |
| 364 | wincmd w |
| 365 | call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid)) |
| 366 | %bw! |
| 367 | endfunc |
| 368 | |
| 369 | " Test for setcursorcharpos() |
| 370 | func Test_setcursorcharpos() |
| 371 | call assert_fails('call setcursorcharpos(test_null_list())', 'E474:') |
| 372 | call assert_fails('call setcursorcharpos([1])', 'E474:') |
| 373 | call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:') |
| 374 | new |
| 375 | call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678']) |
| 376 | normal G |
| 377 | call setcursorcharpos([1, 1]) |
| 378 | call assert_equal([1, 1], [line('.'), col('.')]) |
| 379 | call setcursorcharpos([2, 7, 0]) |
| 380 | call assert_equal([2, 9], [line('.'), col('.')]) |
| 381 | call setcursorcharpos(3, 4) |
| 382 | call assert_equal([3, 1], [line('.'), col('.')]) |
| 383 | call setcursorcharpos([3, 1]) |
| 384 | call assert_equal([3, 1], [line('.'), col('.')]) |
| 385 | call setcursorcharpos([4, 0, 0, 0]) |
| 386 | call assert_equal([4, 1], [line('.'), col('.')]) |
| 387 | call setcursorcharpos([4, 20]) |
| 388 | call assert_equal([4, 9], [line('.'), col('.')]) |
| 389 | normal 1G |
| 390 | call setcursorcharpos([100, 100, 100, 100]) |
| 391 | call assert_equal([4, 9], [line('.'), col('.')]) |
| 392 | normal 1G |
| 393 | call setcursorcharpos('$', 1) |
| 394 | call assert_equal([4, 1], [line('.'), col('.')]) |
| 395 | |
| 396 | %bw! |
| 397 | endfunc |
| 398 | |
Bram Moolenaar | 08f4157 | 2020-04-20 16:50:00 +0200 | [diff] [blame] | 399 | " vim: shiftwidth=2 sts=2 expandtab |