blob: bc12511aff33f01cf60214fda9872f4eb3bf9dae [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
3func Test_wrong_arguments()
Bram Moolenaar37175402017-03-18 20:18:45 +01004 call assert_fails('call cursor(1. 3)', 'E474:')
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005 call assert_fails('call cursor(test_null_list())', 'E474:')
Bram Moolenaar5a46a582016-01-15 15:56:58 +01006endfunc
7
8func 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 Moolenaar2ab375e2016-02-10 22:23:06 +010018 call assert_equal([2, 2, 0, 2], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010019 " line number zero keeps the line number
20 call cursor(0, 1)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010021 call assert_equal([2, 1, 0, 1], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010022 " col number zero keeps the column
23 call cursor(3, 0)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010024 call assert_equal([3, 1, 0, 1], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010025 " below last line goes to last line
Bram Moolenaar1a3a8912019-08-23 22:31:37 +020026 eval [9, 1]->cursor()
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010027 call assert_equal([4, 1, 0, 1], getcurpos()[1:])
Bram Moolenaar9ebcf232021-01-16 16:52:49 +010028 " pass string arguments
29 call cursor('3', '3')
30 call assert_equal([3, 3, 0, 3], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010031
Bram Moolenaar17aca702019-05-16 22:24:55 +020032 call setline(1, ["\<TAB>"])
33 call cursor(1, 1, 1)
34 call assert_equal([1, 1, 1], getcurpos()[1:3])
35
Bram Moolenaar9a963372020-12-21 21:58:46 +010036 call assert_fails('call cursor(-1, -1)', 'E475:')
Bram Moolenaar17aca702019-05-16 22:24:55 +020037
Bram Moolenaar5a46a582016-01-15 15:56:58 +010038 quit!
39endfunc
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010040
naohiro ono56200ee2022-01-01 14:59:44 +000041func 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!
51endfunc
52
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010053" Very short version of what matchparen does.
54function s:Highlight_Matching_Pair()
55 let save_cursor = getcurpos()
Bram Moolenaaraad222c2019-09-06 22:46:09 +020056 eval save_cursor->setpos('.')
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010057endfunc
58
59func Test_curswant_with_autocommand()
60 new
61 call setline(1, ['func()', '{', '}', '----'])
62 autocmd! CursorMovedI * call s:Highlight_Matching_Pair()
Bram Moolenaareb992cb2017-03-09 18:20:16 +010063 call test_override("char_avail", 1)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010064 exe "normal! 3Ga\<Down>X\<Esc>"
Bram Moolenaareb992cb2017-03-09 18:20:16 +010065 call test_override("char_avail", 0)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010066 call assert_equal('-X---', getline(4))
67 autocmd! CursorMovedI *
68 quit!
69endfunc
70
Bram Moolenaar177ab9e2019-01-15 21:12:57 +010071" Tests for behavior of curswant with cursorcolumn/line
72func 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!
80endfunc
81
82func 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!
90endfunc
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020091
92func 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 Moolenaarf92e58c2019-09-08 21:51:41 +0200102 \ 'endcol': wincol + 7}, winid->screenpos(1, 1))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200103 call assert_equal({'row': winrow,
104 \ 'col': wincol + 13,
105 \ 'curscol': wincol + 13,
Bram Moolenaar196b4662019-09-06 21:34:30 +0200106 \ 'endcol': wincol + 13}, winid->screenpos(1, 7))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200107 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 Moolenaar189663b2021-07-21 18:04:56 +0200116
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 Moolenaar7924a172022-01-24 16:15:15 +0000127 call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
Bram Moolenaar189663b2021-07-21 18:04:56 +0200128 \ winid->screenpos(line('$'), 22))
129
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200130 close
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200131 call assert_equal({}, screenpos(999, 1, 1))
Bram Moolenaar189663b2021-07-21 18:04:56 +0200132
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200133 bwipe!
Bram Moolenaar189663b2021-07-21 18:04:56 +0200134 set display&
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100135
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 Moolenaarb3d17a22019-07-07 18:28:14 +0200140endfunc
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100141
142func 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!
155endfunc
Bram Moolenaar08f41572020-04-20 16:50:00 +0200156
Bram Moolenaar91458462021-01-13 20:08:38 +0100157" Save the visual start character position
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100158func SaveVisualStartCharPos()
159 call add(g:VisualStartPos, getcharpos('v'))
160 return ''
161endfunc
162
Bram Moolenaar91458462021-01-13 20:08:38 +0100163" Save the current cursor character position in insert mode
164func SaveInsertCurrentCharPos()
165 call add(g:InsertCurrentPos, getcharpos('.'))
166 return ''
167endfunc
168
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100169" Test for the getcharpos() function
170func 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 Moolenaar91458462021-01-13 20:08:38 +0100197 call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100198 call assert_equal([0, 2, 9, 0], getcharpos('v'))
199 let g:VisualStartPos = []
200 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100201 call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100202 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 Moolenaar91458462021-01-13 20:08:38 +0100207 " 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 Moolenaar6f02b002021-01-10 20:22:54 +0100220 %bw!
221endfunc
222
223" Test for the setcharpos() function
224func 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 Moolenaar91458462021-01-13 20:08:38 +0100246 " 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 Moolenaar6f02b002021-01-10 20:22:54 +0100250
251 %bw!
252 call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
253endfunc
254
255func SaveVisualStartCharCol()
256 call add(g:VisualStartCol, charcol('v'))
257 return ''
258endfunc
259
Bram Moolenaar91458462021-01-13 20:08:38 +0100260func SaveInsertCurrentCharCol()
261 call add(g:InsertCurrentCol, charcol('.'))
262 return ''
263endfunc
264
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100265" Test for the charcol() function
266func 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 Moolenaar91458462021-01-13 20:08:38 +0100302 call assert_equal([7, 10, 5], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100303 call assert_equal(9, charcol('v'))
304 let g:VisualStartCol = []
305 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100306 call assert_equal([1, 2], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100307 let g:VisualStartCol = []
308 exe "normal 1Gv$\<F3>o\<F3>"
309 call assert_equal([1, 1], g:VisualStartCol)
310 vunmap <F3>
311
Bram Moolenaar91458462021-01-13 20:08:38 +0100312 " 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 Moolenaar6f02b002021-01-10 20:22:54 +0100324 %bw!
325endfunc
326
Bram Moolenaar91458462021-01-13 20:08:38 +0100327func SaveInsertCursorCharPos()
328 call add(g:InsertCursorPos, getcursorcharpos('.'))
329 return ''
330endfunc
331
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100332" Test for getcursorcharpos()
333func 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 Moolenaar91458462021-01-13 20:08:38 +0100349 " 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 Moolenaar6f02b002021-01-10 20:22:54 +0100362 let winid = win_getid()
363 normal 2G5l
364 wincmd w
365 call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
366 %bw!
367endfunc
368
369" Test for setcursorcharpos()
370func 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!
397endfunc
398
Bram Moolenaar08f41572020-04-20 16:50:00 +0200399" vim: shiftwidth=2 sts=2 expandtab