blob: c37826cbc1cf13385c8232a889c4e556ab08dbec [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},
zeertzjqf0e68c02023-06-03 17:11:47 +0100130 \ winid->screenpos(line('$'), 22))
131
132 1split
133 normal G$
134 redraw
135 call assert_equal({'row': winrow + 0,
136 \ 'col': wincol + 20 - 1,
137 \ 'curscol': wincol + 20 - 1,
138 \ 'endcol': wincol + 20 - 1},
139 \ screenpos(win_getid(), line('.'), col('.')))
140
141 " w_skipcol should be subtracted
142 setlocal nowrap
143 normal 050zl$
144 call assert_equal({'row': winrow + 0,
145 \ 'col': wincol + 10 - 1,
146 \ 'curscol': wincol + 10 - 1,
147 \ 'endcol': wincol + 10 - 1},
148 \ screenpos(win_getid(), line('.'), col('.')))
149
150 " w_skipcol should only matter for the topline
151" FIXME: This fails because pline_m_win() does not take w_skipcol into
152" account. If it does, then other tests fail.
153" wincmd +
154" setlocal wrap smoothscroll
155" call setline(line('$') + 1, 'last line')
156" exe "normal \<C-E>G$"
157" redraw
158" call assert_equal({'row': winrow + 1,
159" \ 'col': wincol + 9 - 1,
160" \ 'curscol': wincol + 9 - 1,
161" \ 'endcol': wincol + 9 - 1},
162" \ screenpos(win_getid(), line('.'), col('.')))
163 close
Bram Moolenaar189663b2021-07-21 18:04:56 +0200164
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200165 close
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200166 call assert_equal({}, screenpos(999, 1, 1))
Bram Moolenaar189663b2021-07-21 18:04:56 +0200167
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200168 bwipe!
Bram Moolenaar189663b2021-07-21 18:04:56 +0200169 set display&
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100170
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000171 call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100172 nmenu WinBar.TEST :
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000173 call assert_equal(#{col: 1, row: 2, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100174 nunmenu WinBar.TEST
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200175endfunc
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100176
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000177func Test_screenpos_fold()
178 CheckFeature folding
179
180 enew!
181 call setline(1, range(10))
182 3,5fold
183 redraw
184 call assert_equal(2, screenpos(1, 2, 1).row)
185 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 3, 1))
zeertzjqba2d1912022-12-18 12:28:59 +0000186 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 4, 1))
187 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 5, 1))
188 setlocal number
189 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 3, 1))
190 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 4, 1))
191 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 5, 1))
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000192 call assert_equal(4, screenpos(1, 6, 1).row)
193 bwipe!
194endfunc
195
Bram Moolenaar1cb16c32022-12-05 22:26:44 +0000196func Test_screenpos_diff()
197 CheckFeature diff
198
199 enew!
200 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
201 vnew
202 call setline(1, ['a', 'b', 'c', 'g', 'h', 'i'])
203 windo diffthis
204 wincmd w
205 call assert_equal(#{col: 3, row: 7, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
206
207 windo diffoff
208 bwipe!
209 bwipe!
210endfunc
211
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100212func Test_screenpos_number()
213 rightbelow new
214 rightbelow 73vsplit
215 call setline (1, repeat('x', 66))
216 setlocal number
217 redraw
218 let winid = win_getid()
219 let [winrow, wincol] = win_screenpos(winid)
220 let pos = screenpos(winid, 1, 66)
221 call assert_equal(winrow, pos.row)
222 call assert_equal(wincol + 66 + 3, pos.col)
Bram Moolenaar99d19432022-12-05 16:23:24 +0000223
224 call assert_fails('echo screenpos(0, 2, 1)', 'E966:')
225
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100226 close
227 bwipe!
228endfunc
Bram Moolenaar08f41572020-04-20 16:50:00 +0200229
Bram Moolenaar91458462021-01-13 20:08:38 +0100230" Save the visual start character position
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100231func SaveVisualStartCharPos()
232 call add(g:VisualStartPos, getcharpos('v'))
233 return ''
234endfunc
235
Bram Moolenaar91458462021-01-13 20:08:38 +0100236" Save the current cursor character position in insert mode
237func SaveInsertCurrentCharPos()
238 call add(g:InsertCurrentPos, getcharpos('.'))
239 return ''
240endfunc
241
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100242" Test for the getcharpos() function
243func Test_getcharpos()
244 call assert_fails('call getcharpos({})', 'E731:')
245 call assert_equal([0, 0, 0, 0], getcharpos(0))
246 new
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100247 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678', ' │ x'])
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100248
249 " Test for '.' and '$'
250 normal 1G
251 call assert_equal([0, 1, 1, 0], getcharpos('.'))
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100252 call assert_equal([0, 5, 1, 0], getcharpos('$'))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100253 normal 2G6l
254 call assert_equal([0, 2, 7, 0], getcharpos('.'))
255 normal 3G$
256 call assert_equal([0, 3, 1, 0], getcharpos('.'))
257 normal 4G$
258 call assert_equal([0, 4, 9, 0], getcharpos('.'))
259
260 " Test for a mark
261 normal 2G7lmmgg
262 call assert_equal([0, 2, 8, 0], getcharpos("'m"))
263 delmarks m
264 call assert_equal([0, 0, 0, 0], getcharpos("'m"))
265
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100266 " Check mark does not move
267 normal 5Gfxma
268 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
269 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
270 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
271
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100272 " Test for the visual start column
273 vnoremap <expr> <F3> SaveVisualStartCharPos()
274 let g:VisualStartPos = []
275 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100276 call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100277 call assert_equal([0, 2, 9, 0], getcharpos('v'))
278 let g:VisualStartPos = []
279 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100280 call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100281 let g:VisualStartPos = []
282 exe "normal 1Gv$\<F3>o\<F3>"
283 call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos)
284 vunmap <F3>
285
Bram Moolenaar91458462021-01-13 20:08:38 +0100286 " Test for getting the position in insert mode with the cursor after the
287 " last character in a line
288 inoremap <expr> <F3> SaveInsertCurrentCharPos()
289 let g:InsertCurrentPos = []
290 exe "normal 1GA\<F3>"
291 exe "normal 2GA\<F3>"
292 exe "normal 3GA\<F3>"
293 exe "normal 4GA\<F3>"
294 exe "normal 2G6li\<F3>"
295 call assert_equal([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0],
296 \ [0, 2, 7, 0]], g:InsertCurrentPos)
297 iunmap <F3>
298
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100299 %bw!
300endfunc
301
302" Test for the setcharpos() function
303func Test_setcharpos()
304 call assert_equal(-1, setcharpos('.', test_null_list()))
305 new
306 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
307 call setcharpos('.', [0, 1, 1, 0])
308 call assert_equal([1, 1], [line('.'), col('.')])
309 call setcharpos('.', [0, 2, 7, 0])
310 call assert_equal([2, 9], [line('.'), col('.')])
311 call setcharpos('.', [0, 3, 4, 0])
312 call assert_equal([3, 1], [line('.'), col('.')])
313 call setcharpos('.', [0, 3, 1, 0])
314 call assert_equal([3, 1], [line('.'), col('.')])
315 call setcharpos('.', [0, 4, 0, 0])
316 call assert_equal([4, 1], [line('.'), col('.')])
317 call setcharpos('.', [0, 4, 20, 0])
318 call assert_equal([4, 9], [line('.'), col('.')])
319
320 " Test for mark
321 delmarks m
322 call setcharpos("'m", [0, 2, 9, 0])
323 normal `m
324 call assert_equal([2, 11], [line('.'), col('.')])
Bram Moolenaar91458462021-01-13 20:08:38 +0100325 " unload the buffer and try to set the mark
326 let bnr = bufnr()
327 enew!
328 call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0]))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100329
330 %bw!
331 call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
332endfunc
333
334func SaveVisualStartCharCol()
335 call add(g:VisualStartCol, charcol('v'))
336 return ''
337endfunc
338
Bram Moolenaar91458462021-01-13 20:08:38 +0100339func SaveInsertCurrentCharCol()
340 call add(g:InsertCurrentCol, charcol('.'))
341 return ''
342endfunc
343
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100344" Test for the charcol() function
345func Test_charcol()
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +0000346 call assert_fails('call charcol({})', 'E1222:')
347 call assert_fails('call charcol(".", [])', 'E1210:')
348 call assert_fails('call charcol(0)', 'E1222:')
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100349 new
350 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
351
352 " Test for '.' and '$'
353 normal 1G
354 call assert_equal(1, charcol('.'))
355 call assert_equal(1, charcol('$'))
356 normal 2G6l
357 call assert_equal(7, charcol('.'))
358 call assert_equal(10, charcol('$'))
359 normal 3G$
360 call assert_equal(1, charcol('.'))
361 call assert_equal(2, charcol('$'))
362 normal 4G$
363 call assert_equal(9, charcol('.'))
364 call assert_equal(10, charcol('$'))
365
366 " Test for [lnum, '$']
367 call assert_equal(1, charcol([1, '$']))
368 call assert_equal(10, charcol([2, '$']))
369 call assert_equal(2, charcol([3, '$']))
370 call assert_equal(0, charcol([5, '$']))
371
372 " Test for a mark
373 normal 2G7lmmgg
374 call assert_equal(8, charcol("'m"))
375 delmarks m
376 call assert_equal(0, charcol("'m"))
377
378 " Test for the visual start column
379 vnoremap <expr> <F3> SaveVisualStartCharCol()
380 let g:VisualStartCol = []
381 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100382 call assert_equal([7, 10, 5], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100383 call assert_equal(9, charcol('v'))
384 let g:VisualStartCol = []
385 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100386 call assert_equal([1, 2], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100387 let g:VisualStartCol = []
388 exe "normal 1Gv$\<F3>o\<F3>"
389 call assert_equal([1, 1], g:VisualStartCol)
390 vunmap <F3>
391
Bram Moolenaar91458462021-01-13 20:08:38 +0100392 " Test for getting the column number in insert mode with the cursor after
393 " the last character in a line
394 inoremap <expr> <F3> SaveInsertCurrentCharCol()
395 let g:InsertCurrentCol = []
396 exe "normal 1GA\<F3>"
397 exe "normal 2GA\<F3>"
398 exe "normal 3GA\<F3>"
399 exe "normal 4GA\<F3>"
400 exe "normal 2G6li\<F3>"
401 call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol)
402 iunmap <F3>
403
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +0000404 " Test for getting the column number in another window.
405 let winid = win_getid()
406 new
407 call win_execute(winid, 'normal 1G')
408 call assert_equal(1, charcol('.', winid))
409 call assert_equal(1, charcol('$', winid))
410 call win_execute(winid, 'normal 2G6l')
411 call assert_equal(7, charcol('.', winid))
412 call assert_equal(10, charcol('$', winid))
413
414 " calling from another tab page also works
415 tabnew
416 call assert_equal(7, charcol('.', winid))
417 call assert_equal(10, charcol('$', winid))
418 tabclose
419
420 " unknown window ID
421 call assert_equal(0, charcol('.', 10001))
422
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100423 %bw!
424endfunc
425
Bram Moolenaar91458462021-01-13 20:08:38 +0100426func SaveInsertCursorCharPos()
427 call add(g:InsertCursorPos, getcursorcharpos('.'))
428 return ''
429endfunc
430
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100431" Test for getcursorcharpos()
432func Test_getcursorcharpos()
433 call assert_equal(getcursorcharpos(), getcursorcharpos(0))
434 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1))
435 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999))
436
437 new
438 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
439 normal 1G9l
440 call assert_equal([0, 1, 1, 0, 1], getcursorcharpos())
441 normal 2G9l
442 call assert_equal([0, 2, 9, 0, 14], getcursorcharpos())
443 normal 3G9l
444 call assert_equal([0, 3, 1, 0, 1], getcursorcharpos())
445 normal 4G9l
446 call assert_equal([0, 4, 9, 0, 9], getcursorcharpos())
447
Bram Moolenaar91458462021-01-13 20:08:38 +0100448 " Test for getting the cursor position in insert mode with the cursor after
449 " the last character in a line
450 inoremap <expr> <F3> SaveInsertCursorCharPos()
451 let g:InsertCursorPos = []
452 exe "normal 1GA\<F3>"
453 exe "normal 2GA\<F3>"
454 exe "normal 3GA\<F3>"
455 exe "normal 4GA\<F3>"
456 exe "normal 2G6li\<F3>"
457 call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2],
458 \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos)
459 iunmap <F3>
460
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100461 let winid = win_getid()
462 normal 2G5l
463 wincmd w
464 call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
465 %bw!
466endfunc
467
468" Test for setcursorcharpos()
469func Test_setcursorcharpos()
470 call assert_fails('call setcursorcharpos(test_null_list())', 'E474:')
471 call assert_fails('call setcursorcharpos([1])', 'E474:')
472 call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:')
473 new
474 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
475 normal G
476 call setcursorcharpos([1, 1])
477 call assert_equal([1, 1], [line('.'), col('.')])
Bram Moolenaar79f23442022-10-10 12:42:57 +0100478
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100479 call setcursorcharpos([2, 7, 0])
480 call assert_equal([2, 9], [line('.'), col('.')])
Bram Moolenaar79f23442022-10-10 12:42:57 +0100481 call setcursorcharpos([0, 7, 0])
482 call assert_equal([2, 9], [line('.'), col('.')])
483 call setcursorcharpos(0, 7, 0)
484 call assert_equal([2, 9], [line('.'), col('.')])
485
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100486 call setcursorcharpos(3, 4)
487 call assert_equal([3, 1], [line('.'), col('.')])
488 call setcursorcharpos([3, 1])
489 call assert_equal([3, 1], [line('.'), col('.')])
490 call setcursorcharpos([4, 0, 0, 0])
491 call assert_equal([4, 1], [line('.'), col('.')])
492 call setcursorcharpos([4, 20])
493 call assert_equal([4, 9], [line('.'), col('.')])
494 normal 1G
495 call setcursorcharpos([100, 100, 100, 100])
496 call assert_equal([4, 9], [line('.'), col('.')])
497 normal 1G
498 call setcursorcharpos('$', 1)
499 call assert_equal([4, 1], [line('.'), col('.')])
500
501 %bw!
502endfunc
503
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100504" Test for virtcol2col()
505func Test_virtcol2col()
506 new
507 call setline(1, ["a\tb\tc"])
508 call assert_equal(1, virtcol2col(0, 1, 1))
509 call assert_equal(2, virtcol2col(0, 1, 2))
510 call assert_equal(2, virtcol2col(0, 1, 8))
511 call assert_equal(3, virtcol2col(0, 1, 9))
512 call assert_equal(4, virtcol2col(0, 1, 10))
513 call assert_equal(4, virtcol2col(0, 1, 16))
514 call assert_equal(5, virtcol2col(0, 1, 17))
515 call assert_equal(-1, virtcol2col(10, 1, 1))
516 call assert_equal(-1, virtcol2col(0, 10, 1))
517 call assert_equal(-1, virtcol2col(0, -1, 1))
518 call assert_equal(-1, virtcol2col(0, 1, -1))
519 call assert_equal(5, virtcol2col(0, 1, 20))
520 call assert_fails('echo virtcol2col("0", 1, 20)', 'E1210:')
521 call assert_fails('echo virtcol2col(0, "1", 20)', 'E1210:')
522 call assert_fails('echo virtcol2col(0, 1, "1")', 'E1210:')
523 bw!
524endfunc
525
Bram Moolenaar08f41572020-04-20 16:50:00 +0200526" vim: shiftwidth=2 sts=2 expandtab