blob: b943059162d8020b7eea36fcd9461e052665a17f [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
41" Very short version of what matchparen does.
42function s:Highlight_Matching_Pair()
43 let save_cursor = getcurpos()
Bram Moolenaaraad222c2019-09-06 22:46:09 +020044 eval save_cursor->setpos('.')
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010045endfunc
46
47func Test_curswant_with_autocommand()
48 new
49 call setline(1, ['func()', '{', '}', '----'])
50 autocmd! CursorMovedI * call s:Highlight_Matching_Pair()
Bram Moolenaareb992cb2017-03-09 18:20:16 +010051 call test_override("char_avail", 1)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010052 exe "normal! 3Ga\<Down>X\<Esc>"
Bram Moolenaareb992cb2017-03-09 18:20:16 +010053 call test_override("char_avail", 0)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010054 call assert_equal('-X---', getline(4))
55 autocmd! CursorMovedI *
56 quit!
57endfunc
58
Bram Moolenaar177ab9e2019-01-15 21:12:57 +010059" Tests for behavior of curswant with cursorcolumn/line
60func Test_curswant_with_cursorcolumn()
61 new
62 call setline(1, ['01234567', ''])
63 exe "normal! ggf6j"
64 call assert_equal(6, winsaveview().curswant)
65 set cursorcolumn
66 call assert_equal(6, winsaveview().curswant)
67 quit!
68endfunc
69
70func Test_curswant_with_cursorline()
71 new
72 call setline(1, ['01234567', ''])
73 exe "normal! ggf6j"
74 call assert_equal(6, winsaveview().curswant)
75 set cursorline
76 call assert_equal(6, winsaveview().curswant)
77 quit!
78endfunc
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020079
80func Test_screenpos()
81 rightbelow new
82 rightbelow 20vsplit
83 call setline(1, ["\tsome text", "long wrapping line here", "next line"])
84 redraw
85 let winid = win_getid()
86 let [winrow, wincol] = win_screenpos(winid)
87 call assert_equal({'row': winrow,
88 \ 'col': wincol + 0,
89 \ 'curscol': wincol + 7,
Bram Moolenaarf92e58c2019-09-08 21:51:41 +020090 \ 'endcol': wincol + 7}, winid->screenpos(1, 1))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020091 call assert_equal({'row': winrow,
92 \ 'col': wincol + 13,
93 \ 'curscol': wincol + 13,
Bram Moolenaar196b4662019-09-06 21:34:30 +020094 \ 'endcol': wincol + 13}, winid->screenpos(1, 7))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020095 call assert_equal({'row': winrow + 2,
96 \ 'col': wincol + 1,
97 \ 'curscol': wincol + 1,
98 \ 'endcol': wincol + 1}, screenpos(winid, 2, 22))
99 setlocal number
100 call assert_equal({'row': winrow + 3,
101 \ 'col': wincol + 9,
102 \ 'curscol': wincol + 9,
103 \ 'endcol': wincol + 9}, screenpos(winid, 2, 22))
Bram Moolenaar189663b2021-07-21 18:04:56 +0200104
105 let wininfo = getwininfo(winid)[0]
106 call setline(3, ['x']->repeat(wininfo.height))
107 call setline(line('$') + 1, 'x'->repeat(wininfo.width * 3))
108 setlocal nonumber display=lastline so=0
109 exe "normal G\<C-Y>\<C-Y>"
110 redraw
111 call assert_equal({'row': winrow + wininfo.height - 1,
112 \ 'col': wincol + 7,
113 \ 'curscol': wincol + 7,
114 \ 'endcol': wincol + 7}, winid->screenpos(line('$'), 8))
115 call assert_equal({'row': winrow - 1, 'col': 0, 'curscol': 0, 'endcol': 0},
116 \ winid->screenpos(line('$'), 22))
117
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200118 close
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200119 call assert_equal({}, screenpos(999, 1, 1))
Bram Moolenaar189663b2021-07-21 18:04:56 +0200120
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200121 bwipe!
Bram Moolenaar189663b2021-07-21 18:04:56 +0200122 set display&
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100123
124 call assert_equal({'col': 1, 'row': 1, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1))
125 nmenu WinBar.TEST :
126 call assert_equal({'col': 1, 'row': 2, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1))
127 nunmenu WinBar.TEST
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200128endfunc
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100129
130func Test_screenpos_number()
131 rightbelow new
132 rightbelow 73vsplit
133 call setline (1, repeat('x', 66))
134 setlocal number
135 redraw
136 let winid = win_getid()
137 let [winrow, wincol] = win_screenpos(winid)
138 let pos = screenpos(winid, 1, 66)
139 call assert_equal(winrow, pos.row)
140 call assert_equal(wincol + 66 + 3, pos.col)
141 close
142 bwipe!
143endfunc
Bram Moolenaar08f41572020-04-20 16:50:00 +0200144
Bram Moolenaar91458462021-01-13 20:08:38 +0100145" Save the visual start character position
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100146func SaveVisualStartCharPos()
147 call add(g:VisualStartPos, getcharpos('v'))
148 return ''
149endfunc
150
Bram Moolenaar91458462021-01-13 20:08:38 +0100151" Save the current cursor character position in insert mode
152func SaveInsertCurrentCharPos()
153 call add(g:InsertCurrentPos, getcharpos('.'))
154 return ''
155endfunc
156
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100157" Test for the getcharpos() function
158func Test_getcharpos()
159 call assert_fails('call getcharpos({})', 'E731:')
160 call assert_equal([0, 0, 0, 0], getcharpos(0))
161 new
162 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
163
164 " Test for '.' and '$'
165 normal 1G
166 call assert_equal([0, 1, 1, 0], getcharpos('.'))
167 call assert_equal([0, 4, 1, 0], getcharpos('$'))
168 normal 2G6l
169 call assert_equal([0, 2, 7, 0], getcharpos('.'))
170 normal 3G$
171 call assert_equal([0, 3, 1, 0], getcharpos('.'))
172 normal 4G$
173 call assert_equal([0, 4, 9, 0], getcharpos('.'))
174
175 " Test for a mark
176 normal 2G7lmmgg
177 call assert_equal([0, 2, 8, 0], getcharpos("'m"))
178 delmarks m
179 call assert_equal([0, 0, 0, 0], getcharpos("'m"))
180
181 " Test for the visual start column
182 vnoremap <expr> <F3> SaveVisualStartCharPos()
183 let g:VisualStartPos = []
184 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100185 call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100186 call assert_equal([0, 2, 9, 0], getcharpos('v'))
187 let g:VisualStartPos = []
188 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100189 call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100190 let g:VisualStartPos = []
191 exe "normal 1Gv$\<F3>o\<F3>"
192 call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos)
193 vunmap <F3>
194
Bram Moolenaar91458462021-01-13 20:08:38 +0100195 " Test for getting the position in insert mode with the cursor after the
196 " last character in a line
197 inoremap <expr> <F3> SaveInsertCurrentCharPos()
198 let g:InsertCurrentPos = []
199 exe "normal 1GA\<F3>"
200 exe "normal 2GA\<F3>"
201 exe "normal 3GA\<F3>"
202 exe "normal 4GA\<F3>"
203 exe "normal 2G6li\<F3>"
204 call assert_equal([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0],
205 \ [0, 2, 7, 0]], g:InsertCurrentPos)
206 iunmap <F3>
207
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100208 %bw!
209endfunc
210
211" Test for the setcharpos() function
212func Test_setcharpos()
213 call assert_equal(-1, setcharpos('.', test_null_list()))
214 new
215 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
216 call setcharpos('.', [0, 1, 1, 0])
217 call assert_equal([1, 1], [line('.'), col('.')])
218 call setcharpos('.', [0, 2, 7, 0])
219 call assert_equal([2, 9], [line('.'), col('.')])
220 call setcharpos('.', [0, 3, 4, 0])
221 call assert_equal([3, 1], [line('.'), col('.')])
222 call setcharpos('.', [0, 3, 1, 0])
223 call assert_equal([3, 1], [line('.'), col('.')])
224 call setcharpos('.', [0, 4, 0, 0])
225 call assert_equal([4, 1], [line('.'), col('.')])
226 call setcharpos('.', [0, 4, 20, 0])
227 call assert_equal([4, 9], [line('.'), col('.')])
228
229 " Test for mark
230 delmarks m
231 call setcharpos("'m", [0, 2, 9, 0])
232 normal `m
233 call assert_equal([2, 11], [line('.'), col('.')])
Bram Moolenaar91458462021-01-13 20:08:38 +0100234 " unload the buffer and try to set the mark
235 let bnr = bufnr()
236 enew!
237 call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0]))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100238
239 %bw!
240 call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
241endfunc
242
243func SaveVisualStartCharCol()
244 call add(g:VisualStartCol, charcol('v'))
245 return ''
246endfunc
247
Bram Moolenaar91458462021-01-13 20:08:38 +0100248func SaveInsertCurrentCharCol()
249 call add(g:InsertCurrentCol, charcol('.'))
250 return ''
251endfunc
252
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100253" Test for the charcol() function
254func Test_charcol()
255 call assert_fails('call charcol({})', 'E731:')
256 call assert_equal(0, charcol(0))
257 new
258 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
259
260 " Test for '.' and '$'
261 normal 1G
262 call assert_equal(1, charcol('.'))
263 call assert_equal(1, charcol('$'))
264 normal 2G6l
265 call assert_equal(7, charcol('.'))
266 call assert_equal(10, charcol('$'))
267 normal 3G$
268 call assert_equal(1, charcol('.'))
269 call assert_equal(2, charcol('$'))
270 normal 4G$
271 call assert_equal(9, charcol('.'))
272 call assert_equal(10, charcol('$'))
273
274 " Test for [lnum, '$']
275 call assert_equal(1, charcol([1, '$']))
276 call assert_equal(10, charcol([2, '$']))
277 call assert_equal(2, charcol([3, '$']))
278 call assert_equal(0, charcol([5, '$']))
279
280 " Test for a mark
281 normal 2G7lmmgg
282 call assert_equal(8, charcol("'m"))
283 delmarks m
284 call assert_equal(0, charcol("'m"))
285
286 " Test for the visual start column
287 vnoremap <expr> <F3> SaveVisualStartCharCol()
288 let g:VisualStartCol = []
289 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100290 call assert_equal([7, 10, 5], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100291 call assert_equal(9, charcol('v'))
292 let g:VisualStartCol = []
293 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100294 call assert_equal([1, 2], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100295 let g:VisualStartCol = []
296 exe "normal 1Gv$\<F3>o\<F3>"
297 call assert_equal([1, 1], g:VisualStartCol)
298 vunmap <F3>
299
Bram Moolenaar91458462021-01-13 20:08:38 +0100300 " Test for getting the column number in insert mode with the cursor after
301 " the last character in a line
302 inoremap <expr> <F3> SaveInsertCurrentCharCol()
303 let g:InsertCurrentCol = []
304 exe "normal 1GA\<F3>"
305 exe "normal 2GA\<F3>"
306 exe "normal 3GA\<F3>"
307 exe "normal 4GA\<F3>"
308 exe "normal 2G6li\<F3>"
309 call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol)
310 iunmap <F3>
311
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100312 %bw!
313endfunc
314
Bram Moolenaar91458462021-01-13 20:08:38 +0100315func SaveInsertCursorCharPos()
316 call add(g:InsertCursorPos, getcursorcharpos('.'))
317 return ''
318endfunc
319
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100320" Test for getcursorcharpos()
321func Test_getcursorcharpos()
322 call assert_equal(getcursorcharpos(), getcursorcharpos(0))
323 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1))
324 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999))
325
326 new
327 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
328 normal 1G9l
329 call assert_equal([0, 1, 1, 0, 1], getcursorcharpos())
330 normal 2G9l
331 call assert_equal([0, 2, 9, 0, 14], getcursorcharpos())
332 normal 3G9l
333 call assert_equal([0, 3, 1, 0, 1], getcursorcharpos())
334 normal 4G9l
335 call assert_equal([0, 4, 9, 0, 9], getcursorcharpos())
336
Bram Moolenaar91458462021-01-13 20:08:38 +0100337 " Test for getting the cursor position in insert mode with the cursor after
338 " the last character in a line
339 inoremap <expr> <F3> SaveInsertCursorCharPos()
340 let g:InsertCursorPos = []
341 exe "normal 1GA\<F3>"
342 exe "normal 2GA\<F3>"
343 exe "normal 3GA\<F3>"
344 exe "normal 4GA\<F3>"
345 exe "normal 2G6li\<F3>"
346 call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2],
347 \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos)
348 iunmap <F3>
349
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100350 let winid = win_getid()
351 normal 2G5l
352 wincmd w
353 call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
354 %bw!
355endfunc
356
357" Test for setcursorcharpos()
358func Test_setcursorcharpos()
359 call assert_fails('call setcursorcharpos(test_null_list())', 'E474:')
360 call assert_fails('call setcursorcharpos([1])', 'E474:')
361 call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:')
362 new
363 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
364 normal G
365 call setcursorcharpos([1, 1])
366 call assert_equal([1, 1], [line('.'), col('.')])
367 call setcursorcharpos([2, 7, 0])
368 call assert_equal([2, 9], [line('.'), col('.')])
369 call setcursorcharpos(3, 4)
370 call assert_equal([3, 1], [line('.'), col('.')])
371 call setcursorcharpos([3, 1])
372 call assert_equal([3, 1], [line('.'), col('.')])
373 call setcursorcharpos([4, 0, 0, 0])
374 call assert_equal([4, 1], [line('.'), col('.')])
375 call setcursorcharpos([4, 20])
376 call assert_equal([4, 9], [line('.'), col('.')])
377 normal 1G
378 call setcursorcharpos([100, 100, 100, 100])
379 call assert_equal([4, 9], [line('.'), col('.')])
380 normal 1G
381 call setcursorcharpos('$', 1)
382 call assert_equal([4, 1], [line('.'), col('.')])
383
384 %bw!
385endfunc
386
Bram Moolenaar08f41572020-04-20 16:50:00 +0200387" vim: shiftwidth=2 sts=2 expandtab