blob: 1c26f6d35ef29114e2a77360bbe5969e9d64955a [file] [log] [blame]
Bram Moolenaar6f02b002021-01-10 20:22:54 +01001" Tests for cursor() and other functions that get/set the cursor position
Bram Moolenaar5a46a582016-01-15 15:56:58 +01002
Bram Moolenaar4556a2e2022-02-15 13:40:17 +00003source check.vim
4
Bram Moolenaar5a46a582016-01-15 15:56:58 +01005func Test_wrong_arguments()
Bram Moolenaar37175402017-03-18 20:18:45 +01006 call assert_fails('call cursor(1. 3)', 'E474:')
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02007 call assert_fails('call cursor(test_null_list())', 'E474:')
Bram Moolenaar5a46a582016-01-15 15:56:58 +01008endfunc
9
10func Test_move_cursor()
11 new
12 call setline(1, ['aaa', 'bbb', 'ccc', 'ddd'])
13
14 call cursor([1, 1, 0, 1])
15 call assert_equal([1, 1, 0, 1], getcurpos()[1:])
16 call cursor([4, 3, 0, 3])
17 call assert_equal([4, 3, 0, 3], getcurpos()[1:])
18
19 call cursor(2, 2)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010020 call assert_equal([2, 2, 0, 2], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010021 " line number zero keeps the line number
22 call cursor(0, 1)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010023 call assert_equal([2, 1, 0, 1], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010024 " col number zero keeps the column
25 call cursor(3, 0)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010026 call assert_equal([3, 1, 0, 1], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010027 " below last line goes to last line
Bram Moolenaar1a3a8912019-08-23 22:31:37 +020028 eval [9, 1]->cursor()
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010029 call assert_equal([4, 1, 0, 1], getcurpos()[1:])
Bram Moolenaar9ebcf232021-01-16 16:52:49 +010030 " pass string arguments
31 call cursor('3', '3')
32 call assert_equal([3, 3, 0, 3], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010033
Bram Moolenaar17aca702019-05-16 22:24:55 +020034 call setline(1, ["\<TAB>"])
35 call cursor(1, 1, 1)
36 call assert_equal([1, 1, 1], getcurpos()[1:3])
37
Bram Moolenaar9a963372020-12-21 21:58:46 +010038 call assert_fails('call cursor(-1, -1)', 'E475:')
Bram Moolenaar17aca702019-05-16 22:24:55 +020039
Bram Moolenaar5a46a582016-01-15 15:56:58 +010040 quit!
41endfunc
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010042
naohiro ono56200ee2022-01-01 14:59:44 +000043func Test_curswant_maxcol()
44 new
45 call setline(1, 'foo')
46
47 " Test that after "$" command curswant is set to the same value as v:maxcol.
48 normal! 1G$
49 call assert_equal(v:maxcol, getcurpos()[4])
50 call assert_equal(v:maxcol, winsaveview().curswant)
51
52 quit!
53endfunc
54
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010055" Very short version of what matchparen does.
56function s:Highlight_Matching_Pair()
57 let save_cursor = getcurpos()
Bram Moolenaaraad222c2019-09-06 22:46:09 +020058 eval save_cursor->setpos('.')
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010059endfunc
60
61func Test_curswant_with_autocommand()
62 new
63 call setline(1, ['func()', '{', '}', '----'])
64 autocmd! CursorMovedI * call s:Highlight_Matching_Pair()
Bram Moolenaareb992cb2017-03-09 18:20:16 +010065 call test_override("char_avail", 1)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010066 exe "normal! 3Ga\<Down>X\<Esc>"
Bram Moolenaareb992cb2017-03-09 18:20:16 +010067 call test_override("char_avail", 0)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010068 call assert_equal('-X---', getline(4))
69 autocmd! CursorMovedI *
70 quit!
71endfunc
72
Bram Moolenaar177ab9e2019-01-15 21:12:57 +010073" Tests for behavior of curswant with cursorcolumn/line
74func Test_curswant_with_cursorcolumn()
75 new
76 call setline(1, ['01234567', ''])
77 exe "normal! ggf6j"
78 call assert_equal(6, winsaveview().curswant)
79 set cursorcolumn
80 call assert_equal(6, winsaveview().curswant)
81 quit!
82endfunc
83
84func Test_curswant_with_cursorline()
85 new
86 call setline(1, ['01234567', ''])
87 exe "normal! ggf6j"
88 call assert_equal(6, winsaveview().curswant)
89 set cursorline
90 call assert_equal(6, winsaveview().curswant)
91 quit!
92endfunc
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020093
94func Test_screenpos()
95 rightbelow new
96 rightbelow 20vsplit
97 call setline(1, ["\tsome text", "long wrapping line here", "next line"])
98 redraw
99 let winid = win_getid()
100 let [winrow, wincol] = win_screenpos(winid)
101 call assert_equal({'row': winrow,
102 \ 'col': wincol + 0,
103 \ 'curscol': wincol + 7,
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200104 \ 'endcol': wincol + 7}, winid->screenpos(1, 1))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200105 call assert_equal({'row': winrow,
106 \ 'col': wincol + 13,
107 \ 'curscol': wincol + 13,
Bram Moolenaar196b4662019-09-06 21:34:30 +0200108 \ 'endcol': wincol + 13}, winid->screenpos(1, 7))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200109 call assert_equal({'row': winrow + 2,
110 \ 'col': wincol + 1,
111 \ 'curscol': wincol + 1,
112 \ 'endcol': wincol + 1}, screenpos(winid, 2, 22))
113 setlocal number
114 call assert_equal({'row': winrow + 3,
115 \ 'col': wincol + 9,
116 \ 'curscol': wincol + 9,
117 \ 'endcol': wincol + 9}, screenpos(winid, 2, 22))
Bram Moolenaar189663b2021-07-21 18:04:56 +0200118
119 let wininfo = getwininfo(winid)[0]
120 call setline(3, ['x']->repeat(wininfo.height))
121 call setline(line('$') + 1, 'x'->repeat(wininfo.width * 3))
122 setlocal nonumber display=lastline so=0
123 exe "normal G\<C-Y>\<C-Y>"
124 redraw
125 call assert_equal({'row': winrow + wininfo.height - 1,
126 \ 'col': wincol + 7,
127 \ 'curscol': wincol + 7,
128 \ 'endcol': wincol + 7}, winid->screenpos(line('$'), 8))
Bram Moolenaar7924a172022-01-24 16:15:15 +0000129 call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
Bram Moolenaar189663b2021-07-21 18:04:56 +0200130 \ winid->screenpos(line('$'), 22))
131
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200132 close
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200133 call assert_equal({}, screenpos(999, 1, 1))
Bram Moolenaar189663b2021-07-21 18:04:56 +0200134
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200135 bwipe!
Bram Moolenaar189663b2021-07-21 18:04:56 +0200136 set display&
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100137
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000138 call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100139 nmenu WinBar.TEST :
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000140 call assert_equal(#{col: 1, row: 2, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100141 nunmenu WinBar.TEST
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200142endfunc
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100143
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000144func Test_screenpos_fold()
145 CheckFeature folding
146
147 enew!
148 call setline(1, range(10))
149 3,5fold
150 redraw
151 call assert_equal(2, screenpos(1, 2, 1).row)
152 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 3, 1))
153 call assert_equal(3, screenpos(1, 4, 1).row)
154 call assert_equal(3, screenpos(1, 5, 1).row)
155 call assert_equal(4, screenpos(1, 6, 1).row)
156 bwipe!
157endfunc
158
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100159func Test_screenpos_number()
160 rightbelow new
161 rightbelow 73vsplit
162 call setline (1, repeat('x', 66))
163 setlocal number
164 redraw
165 let winid = win_getid()
166 let [winrow, wincol] = win_screenpos(winid)
167 let pos = screenpos(winid, 1, 66)
168 call assert_equal(winrow, pos.row)
169 call assert_equal(wincol + 66 + 3, pos.col)
170 close
171 bwipe!
172endfunc
Bram Moolenaar08f41572020-04-20 16:50:00 +0200173
Bram Moolenaar91458462021-01-13 20:08:38 +0100174" Save the visual start character position
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100175func SaveVisualStartCharPos()
176 call add(g:VisualStartPos, getcharpos('v'))
177 return ''
178endfunc
179
Bram Moolenaar91458462021-01-13 20:08:38 +0100180" Save the current cursor character position in insert mode
181func SaveInsertCurrentCharPos()
182 call add(g:InsertCurrentPos, getcharpos('.'))
183 return ''
184endfunc
185
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100186" Test for the getcharpos() function
187func Test_getcharpos()
188 call assert_fails('call getcharpos({})', 'E731:')
189 call assert_equal([0, 0, 0, 0], getcharpos(0))
190 new
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100191 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678', ' │ x'])
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100192
193 " Test for '.' and '$'
194 normal 1G
195 call assert_equal([0, 1, 1, 0], getcharpos('.'))
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100196 call assert_equal([0, 5, 1, 0], getcharpos('$'))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100197 normal 2G6l
198 call assert_equal([0, 2, 7, 0], getcharpos('.'))
199 normal 3G$
200 call assert_equal([0, 3, 1, 0], getcharpos('.'))
201 normal 4G$
202 call assert_equal([0, 4, 9, 0], getcharpos('.'))
203
204 " Test for a mark
205 normal 2G7lmmgg
206 call assert_equal([0, 2, 8, 0], getcharpos("'m"))
207 delmarks m
208 call assert_equal([0, 0, 0, 0], getcharpos("'m"))
209
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100210 " Check mark does not move
211 normal 5Gfxma
212 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
213 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
214 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
215
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100216 " Test for the visual start column
217 vnoremap <expr> <F3> SaveVisualStartCharPos()
218 let g:VisualStartPos = []
219 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100220 call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100221 call assert_equal([0, 2, 9, 0], getcharpos('v'))
222 let g:VisualStartPos = []
223 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100224 call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100225 let g:VisualStartPos = []
226 exe "normal 1Gv$\<F3>o\<F3>"
227 call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos)
228 vunmap <F3>
229
Bram Moolenaar91458462021-01-13 20:08:38 +0100230 " Test for getting the position in insert mode with the cursor after the
231 " last character in a line
232 inoremap <expr> <F3> SaveInsertCurrentCharPos()
233 let g:InsertCurrentPos = []
234 exe "normal 1GA\<F3>"
235 exe "normal 2GA\<F3>"
236 exe "normal 3GA\<F3>"
237 exe "normal 4GA\<F3>"
238 exe "normal 2G6li\<F3>"
239 call assert_equal([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0],
240 \ [0, 2, 7, 0]], g:InsertCurrentPos)
241 iunmap <F3>
242
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100243 %bw!
244endfunc
245
246" Test for the setcharpos() function
247func Test_setcharpos()
248 call assert_equal(-1, setcharpos('.', test_null_list()))
249 new
250 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
251 call setcharpos('.', [0, 1, 1, 0])
252 call assert_equal([1, 1], [line('.'), col('.')])
253 call setcharpos('.', [0, 2, 7, 0])
254 call assert_equal([2, 9], [line('.'), col('.')])
255 call setcharpos('.', [0, 3, 4, 0])
256 call assert_equal([3, 1], [line('.'), col('.')])
257 call setcharpos('.', [0, 3, 1, 0])
258 call assert_equal([3, 1], [line('.'), col('.')])
259 call setcharpos('.', [0, 4, 0, 0])
260 call assert_equal([4, 1], [line('.'), col('.')])
261 call setcharpos('.', [0, 4, 20, 0])
262 call assert_equal([4, 9], [line('.'), col('.')])
263
264 " Test for mark
265 delmarks m
266 call setcharpos("'m", [0, 2, 9, 0])
267 normal `m
268 call assert_equal([2, 11], [line('.'), col('.')])
Bram Moolenaar91458462021-01-13 20:08:38 +0100269 " unload the buffer and try to set the mark
270 let bnr = bufnr()
271 enew!
272 call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0]))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100273
274 %bw!
275 call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
276endfunc
277
278func SaveVisualStartCharCol()
279 call add(g:VisualStartCol, charcol('v'))
280 return ''
281endfunc
282
Bram Moolenaar91458462021-01-13 20:08:38 +0100283func SaveInsertCurrentCharCol()
284 call add(g:InsertCurrentCol, charcol('.'))
285 return ''
286endfunc
287
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100288" Test for the charcol() function
289func Test_charcol()
290 call assert_fails('call charcol({})', 'E731:')
291 call assert_equal(0, charcol(0))
292 new
293 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
294
295 " Test for '.' and '$'
296 normal 1G
297 call assert_equal(1, charcol('.'))
298 call assert_equal(1, charcol('$'))
299 normal 2G6l
300 call assert_equal(7, charcol('.'))
301 call assert_equal(10, charcol('$'))
302 normal 3G$
303 call assert_equal(1, charcol('.'))
304 call assert_equal(2, charcol('$'))
305 normal 4G$
306 call assert_equal(9, charcol('.'))
307 call assert_equal(10, charcol('$'))
308
309 " Test for [lnum, '$']
310 call assert_equal(1, charcol([1, '$']))
311 call assert_equal(10, charcol([2, '$']))
312 call assert_equal(2, charcol([3, '$']))
313 call assert_equal(0, charcol([5, '$']))
314
315 " Test for a mark
316 normal 2G7lmmgg
317 call assert_equal(8, charcol("'m"))
318 delmarks m
319 call assert_equal(0, charcol("'m"))
320
321 " Test for the visual start column
322 vnoremap <expr> <F3> SaveVisualStartCharCol()
323 let g:VisualStartCol = []
324 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100325 call assert_equal([7, 10, 5], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100326 call assert_equal(9, charcol('v'))
327 let g:VisualStartCol = []
328 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100329 call assert_equal([1, 2], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100330 let g:VisualStartCol = []
331 exe "normal 1Gv$\<F3>o\<F3>"
332 call assert_equal([1, 1], g:VisualStartCol)
333 vunmap <F3>
334
Bram Moolenaar91458462021-01-13 20:08:38 +0100335 " Test for getting the column number in insert mode with the cursor after
336 " the last character in a line
337 inoremap <expr> <F3> SaveInsertCurrentCharCol()
338 let g:InsertCurrentCol = []
339 exe "normal 1GA\<F3>"
340 exe "normal 2GA\<F3>"
341 exe "normal 3GA\<F3>"
342 exe "normal 4GA\<F3>"
343 exe "normal 2G6li\<F3>"
344 call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol)
345 iunmap <F3>
346
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100347 %bw!
348endfunc
349
Bram Moolenaar91458462021-01-13 20:08:38 +0100350func SaveInsertCursorCharPos()
351 call add(g:InsertCursorPos, getcursorcharpos('.'))
352 return ''
353endfunc
354
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100355" Test for getcursorcharpos()
356func Test_getcursorcharpos()
357 call assert_equal(getcursorcharpos(), getcursorcharpos(0))
358 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1))
359 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999))
360
361 new
362 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
363 normal 1G9l
364 call assert_equal([0, 1, 1, 0, 1], getcursorcharpos())
365 normal 2G9l
366 call assert_equal([0, 2, 9, 0, 14], getcursorcharpos())
367 normal 3G9l
368 call assert_equal([0, 3, 1, 0, 1], getcursorcharpos())
369 normal 4G9l
370 call assert_equal([0, 4, 9, 0, 9], getcursorcharpos())
371
Bram Moolenaar91458462021-01-13 20:08:38 +0100372 " Test for getting the cursor position in insert mode with the cursor after
373 " the last character in a line
374 inoremap <expr> <F3> SaveInsertCursorCharPos()
375 let g:InsertCursorPos = []
376 exe "normal 1GA\<F3>"
377 exe "normal 2GA\<F3>"
378 exe "normal 3GA\<F3>"
379 exe "normal 4GA\<F3>"
380 exe "normal 2G6li\<F3>"
381 call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2],
382 \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos)
383 iunmap <F3>
384
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100385 let winid = win_getid()
386 normal 2G5l
387 wincmd w
388 call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
389 %bw!
390endfunc
391
392" Test for setcursorcharpos()
393func Test_setcursorcharpos()
394 call assert_fails('call setcursorcharpos(test_null_list())', 'E474:')
395 call assert_fails('call setcursorcharpos([1])', 'E474:')
396 call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:')
397 new
398 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
399 normal G
400 call setcursorcharpos([1, 1])
401 call assert_equal([1, 1], [line('.'), col('.')])
402 call setcursorcharpos([2, 7, 0])
403 call assert_equal([2, 9], [line('.'), col('.')])
404 call setcursorcharpos(3, 4)
405 call assert_equal([3, 1], [line('.'), col('.')])
406 call setcursorcharpos([3, 1])
407 call assert_equal([3, 1], [line('.'), col('.')])
408 call setcursorcharpos([4, 0, 0, 0])
409 call assert_equal([4, 1], [line('.'), col('.')])
410 call setcursorcharpos([4, 20])
411 call assert_equal([4, 9], [line('.'), col('.')])
412 normal 1G
413 call setcursorcharpos([100, 100, 100, 100])
414 call assert_equal([4, 9], [line('.'), col('.')])
415 normal 1G
416 call setcursorcharpos('$', 1)
417 call assert_equal([4, 1], [line('.'), col('.')])
418
419 %bw!
420endfunc
421
Bram Moolenaar08f41572020-04-20 16:50:00 +0200422" vim: shiftwidth=2 sts=2 expandtab