blob: b0444d5e677417c92b0b7564cfb74deddfe96ab0 [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
zeertzjq55daae32023-06-04 19:29:22 +0100135 " w_skipcol should be subtracted
zeertzjqf0e68c02023-06-03 17:11:47 +0100136 call assert_equal({'row': winrow + 0,
137 \ 'col': wincol + 20 - 1,
138 \ 'curscol': wincol + 20 - 1,
139 \ 'endcol': wincol + 20 - 1},
140 \ screenpos(win_getid(), line('.'), col('.')))
141
zeertzjq55daae32023-06-04 19:29:22 +0100142 " w_leftcol should be subtracted
zeertzjqf0e68c02023-06-03 17:11:47 +0100143 setlocal nowrap
144 normal 050zl$
145 call assert_equal({'row': winrow + 0,
146 \ 'col': wincol + 10 - 1,
147 \ 'curscol': wincol + 10 - 1,
148 \ 'endcol': wincol + 10 - 1},
149 \ screenpos(win_getid(), line('.'), col('.')))
150
151 " w_skipcol should only matter for the topline
152" FIXME: This fails because pline_m_win() does not take w_skipcol into
153" account. If it does, then other tests fail.
154" wincmd +
155" setlocal wrap smoothscroll
156" call setline(line('$') + 1, 'last line')
157" exe "normal \<C-E>G$"
158" redraw
159" call assert_equal({'row': winrow + 1,
160" \ 'col': wincol + 9 - 1,
161" \ 'curscol': wincol + 9 - 1,
162" \ 'endcol': wincol + 9 - 1},
163" \ screenpos(win_getid(), line('.'), col('.')))
164 close
Bram Moolenaar189663b2021-07-21 18:04:56 +0200165
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200166 close
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200167 call assert_equal({}, screenpos(999, 1, 1))
Bram Moolenaar189663b2021-07-21 18:04:56 +0200168
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200169 bwipe!
Bram Moolenaar189663b2021-07-21 18:04:56 +0200170 set display&
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100171
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000172 call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100173 nmenu WinBar.TEST :
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000174 call assert_equal(#{col: 1, row: 2, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100175 nunmenu WinBar.TEST
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200176endfunc
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100177
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000178func Test_screenpos_fold()
179 CheckFeature folding
180
181 enew!
182 call setline(1, range(10))
183 3,5fold
184 redraw
185 call assert_equal(2, screenpos(1, 2, 1).row)
186 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 3, 1))
zeertzjqba2d1912022-12-18 12:28:59 +0000187 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 4, 1))
188 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 5, 1))
189 setlocal number
190 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 3, 1))
191 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 4, 1))
192 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 5, 1))
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000193 call assert_equal(4, screenpos(1, 6, 1).row)
194 bwipe!
195endfunc
196
Bram Moolenaar1cb16c32022-12-05 22:26:44 +0000197func Test_screenpos_diff()
198 CheckFeature diff
199
200 enew!
201 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
202 vnew
203 call setline(1, ['a', 'b', 'c', 'g', 'h', 'i'])
204 windo diffthis
205 wincmd w
206 call assert_equal(#{col: 3, row: 7, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
zeertzjq55daae32023-06-04 19:29:22 +0100207 call assert_equal(#{col: 3, row: 8, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
208 exe "normal! 3\<C-E>"
209 call assert_equal(#{col: 3, row: 4, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
210 call assert_equal(#{col: 3, row: 5, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
211 exe "normal! \<C-E>"
212 call assert_equal(#{col: 3, row: 3, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
213 call assert_equal(#{col: 3, row: 4, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
214 exe "normal! \<C-E>"
215 call assert_equal(#{col: 3, row: 2, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
216 call assert_equal(#{col: 3, row: 3, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
217 exe "normal! \<C-E>"
218 call assert_equal(#{col: 3, row: 1, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
219 call assert_equal(#{col: 3, row: 2, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
Bram Moolenaar1cb16c32022-12-05 22:26:44 +0000220
221 windo diffoff
222 bwipe!
223 bwipe!
224endfunc
225
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100226func Test_screenpos_number()
227 rightbelow new
228 rightbelow 73vsplit
229 call setline (1, repeat('x', 66))
230 setlocal number
231 redraw
232 let winid = win_getid()
233 let [winrow, wincol] = win_screenpos(winid)
234 let pos = screenpos(winid, 1, 66)
235 call assert_equal(winrow, pos.row)
236 call assert_equal(wincol + 66 + 3, pos.col)
Bram Moolenaar99d19432022-12-05 16:23:24 +0000237
238 call assert_fails('echo screenpos(0, 2, 1)', 'E966:')
239
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100240 close
241 bwipe!
242endfunc
Bram Moolenaar08f41572020-04-20 16:50:00 +0200243
Bram Moolenaar91458462021-01-13 20:08:38 +0100244" Save the visual start character position
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100245func SaveVisualStartCharPos()
246 call add(g:VisualStartPos, getcharpos('v'))
247 return ''
248endfunc
249
Bram Moolenaar91458462021-01-13 20:08:38 +0100250" Save the current cursor character position in insert mode
251func SaveInsertCurrentCharPos()
252 call add(g:InsertCurrentPos, getcharpos('.'))
253 return ''
254endfunc
255
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100256" Test for the getcharpos() function
257func Test_getcharpos()
258 call assert_fails('call getcharpos({})', 'E731:')
259 call assert_equal([0, 0, 0, 0], getcharpos(0))
260 new
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100261 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678', ' │ x'])
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100262
263 " Test for '.' and '$'
264 normal 1G
265 call assert_equal([0, 1, 1, 0], getcharpos('.'))
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100266 call assert_equal([0, 5, 1, 0], getcharpos('$'))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100267 normal 2G6l
268 call assert_equal([0, 2, 7, 0], getcharpos('.'))
269 normal 3G$
270 call assert_equal([0, 3, 1, 0], getcharpos('.'))
271 normal 4G$
272 call assert_equal([0, 4, 9, 0], getcharpos('.'))
273
274 " Test for a mark
275 normal 2G7lmmgg
276 call assert_equal([0, 2, 8, 0], getcharpos("'m"))
277 delmarks m
278 call assert_equal([0, 0, 0, 0], getcharpos("'m"))
279
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100280 " Check mark does not move
281 normal 5Gfxma
282 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
283 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
284 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
285
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100286 " Test for the visual start column
287 vnoremap <expr> <F3> SaveVisualStartCharPos()
288 let g:VisualStartPos = []
289 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100290 call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100291 call assert_equal([0, 2, 9, 0], getcharpos('v'))
292 let g:VisualStartPos = []
293 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100294 call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100295 let g:VisualStartPos = []
296 exe "normal 1Gv$\<F3>o\<F3>"
297 call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos)
298 vunmap <F3>
299
Bram Moolenaar91458462021-01-13 20:08:38 +0100300 " Test for getting the position in insert mode with the cursor after the
301 " last character in a line
302 inoremap <expr> <F3> SaveInsertCurrentCharPos()
303 let g:InsertCurrentPos = []
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([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0],
310 \ [0, 2, 7, 0]], g:InsertCurrentPos)
311 iunmap <F3>
312
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100313 %bw!
314endfunc
315
316" Test for the setcharpos() function
317func Test_setcharpos()
318 call assert_equal(-1, setcharpos('.', test_null_list()))
319 new
320 call setline(1, ['', "01\tà4è678", '', '012345678'])
321 call setcharpos('.', [0, 1, 1, 0])
322 call assert_equal([1, 1], [line('.'), col('.')])
323 call setcharpos('.', [0, 2, 7, 0])
324 call assert_equal([2, 9], [line('.'), col('.')])
325 call setcharpos('.', [0, 3, 4, 0])
326 call assert_equal([3, 1], [line('.'), col('.')])
327 call setcharpos('.', [0, 3, 1, 0])
328 call assert_equal([3, 1], [line('.'), col('.')])
329 call setcharpos('.', [0, 4, 0, 0])
330 call assert_equal([4, 1], [line('.'), col('.')])
331 call setcharpos('.', [0, 4, 20, 0])
332 call assert_equal([4, 9], [line('.'), col('.')])
333
334 " Test for mark
335 delmarks m
336 call setcharpos("'m", [0, 2, 9, 0])
337 normal `m
338 call assert_equal([2, 11], [line('.'), col('.')])
Bram Moolenaar91458462021-01-13 20:08:38 +0100339 " unload the buffer and try to set the mark
340 let bnr = bufnr()
341 enew!
342 call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0]))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100343
344 %bw!
345 call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
346endfunc
347
348func SaveVisualStartCharCol()
349 call add(g:VisualStartCol, charcol('v'))
350 return ''
351endfunc
352
Bram Moolenaar91458462021-01-13 20:08:38 +0100353func SaveInsertCurrentCharCol()
354 call add(g:InsertCurrentCol, charcol('.'))
355 return ''
356endfunc
357
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100358" Test for the charcol() function
359func Test_charcol()
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +0000360 call assert_fails('call charcol({})', 'E1222:')
361 call assert_fails('call charcol(".", [])', 'E1210:')
362 call assert_fails('call charcol(0)', 'E1222:')
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100363 new
364 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
365
366 " Test for '.' and '$'
367 normal 1G
368 call assert_equal(1, charcol('.'))
369 call assert_equal(1, charcol('$'))
370 normal 2G6l
371 call assert_equal(7, charcol('.'))
372 call assert_equal(10, charcol('$'))
373 normal 3G$
374 call assert_equal(1, charcol('.'))
375 call assert_equal(2, charcol('$'))
376 normal 4G$
377 call assert_equal(9, charcol('.'))
378 call assert_equal(10, charcol('$'))
379
380 " Test for [lnum, '$']
381 call assert_equal(1, charcol([1, '$']))
382 call assert_equal(10, charcol([2, '$']))
383 call assert_equal(2, charcol([3, '$']))
384 call assert_equal(0, charcol([5, '$']))
385
386 " Test for a mark
387 normal 2G7lmmgg
388 call assert_equal(8, charcol("'m"))
389 delmarks m
390 call assert_equal(0, charcol("'m"))
391
392 " Test for the visual start column
393 vnoremap <expr> <F3> SaveVisualStartCharCol()
394 let g:VisualStartCol = []
395 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100396 call assert_equal([7, 10, 5], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100397 call assert_equal(9, charcol('v'))
398 let g:VisualStartCol = []
399 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100400 call assert_equal([1, 2], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100401 let g:VisualStartCol = []
402 exe "normal 1Gv$\<F3>o\<F3>"
403 call assert_equal([1, 1], g:VisualStartCol)
404 vunmap <F3>
405
Bram Moolenaar91458462021-01-13 20:08:38 +0100406 " Test for getting the column number in insert mode with the cursor after
407 " the last character in a line
408 inoremap <expr> <F3> SaveInsertCurrentCharCol()
409 let g:InsertCurrentCol = []
410 exe "normal 1GA\<F3>"
411 exe "normal 2GA\<F3>"
412 exe "normal 3GA\<F3>"
413 exe "normal 4GA\<F3>"
414 exe "normal 2G6li\<F3>"
415 call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol)
416 iunmap <F3>
417
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +0000418 " Test for getting the column number in another window.
419 let winid = win_getid()
420 new
421 call win_execute(winid, 'normal 1G')
422 call assert_equal(1, charcol('.', winid))
423 call assert_equal(1, charcol('$', winid))
424 call win_execute(winid, 'normal 2G6l')
425 call assert_equal(7, charcol('.', winid))
426 call assert_equal(10, charcol('$', winid))
427
428 " calling from another tab page also works
429 tabnew
430 call assert_equal(7, charcol('.', winid))
431 call assert_equal(10, charcol('$', winid))
432 tabclose
433
434 " unknown window ID
435 call assert_equal(0, charcol('.', 10001))
436
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100437 %bw!
438endfunc
439
Bram Moolenaar91458462021-01-13 20:08:38 +0100440func SaveInsertCursorCharPos()
441 call add(g:InsertCursorPos, getcursorcharpos('.'))
442 return ''
443endfunc
444
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100445" Test for getcursorcharpos()
446func Test_getcursorcharpos()
447 call assert_equal(getcursorcharpos(), getcursorcharpos(0))
448 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1))
449 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999))
450
451 new
452 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
453 normal 1G9l
454 call assert_equal([0, 1, 1, 0, 1], getcursorcharpos())
455 normal 2G9l
456 call assert_equal([0, 2, 9, 0, 14], getcursorcharpos())
457 normal 3G9l
458 call assert_equal([0, 3, 1, 0, 1], getcursorcharpos())
459 normal 4G9l
460 call assert_equal([0, 4, 9, 0, 9], getcursorcharpos())
461
Bram Moolenaar91458462021-01-13 20:08:38 +0100462 " Test for getting the cursor position in insert mode with the cursor after
463 " the last character in a line
464 inoremap <expr> <F3> SaveInsertCursorCharPos()
465 let g:InsertCursorPos = []
466 exe "normal 1GA\<F3>"
467 exe "normal 2GA\<F3>"
468 exe "normal 3GA\<F3>"
469 exe "normal 4GA\<F3>"
470 exe "normal 2G6li\<F3>"
471 call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2],
472 \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos)
473 iunmap <F3>
474
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100475 let winid = win_getid()
476 normal 2G5l
477 wincmd w
478 call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
479 %bw!
480endfunc
481
482" Test for setcursorcharpos()
483func Test_setcursorcharpos()
484 call assert_fails('call setcursorcharpos(test_null_list())', 'E474:')
485 call assert_fails('call setcursorcharpos([1])', 'E474:')
486 call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:')
487 new
488 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
489 normal G
490 call setcursorcharpos([1, 1])
491 call assert_equal([1, 1], [line('.'), col('.')])
Bram Moolenaar79f23442022-10-10 12:42:57 +0100492
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100493 call setcursorcharpos([2, 7, 0])
494 call assert_equal([2, 9], [line('.'), col('.')])
Bram Moolenaar79f23442022-10-10 12:42:57 +0100495 call setcursorcharpos([0, 7, 0])
496 call assert_equal([2, 9], [line('.'), col('.')])
497 call setcursorcharpos(0, 7, 0)
498 call assert_equal([2, 9], [line('.'), col('.')])
499
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100500 call setcursorcharpos(3, 4)
501 call assert_equal([3, 1], [line('.'), col('.')])
502 call setcursorcharpos([3, 1])
503 call assert_equal([3, 1], [line('.'), col('.')])
504 call setcursorcharpos([4, 0, 0, 0])
505 call assert_equal([4, 1], [line('.'), col('.')])
506 call setcursorcharpos([4, 20])
507 call assert_equal([4, 9], [line('.'), col('.')])
508 normal 1G
509 call setcursorcharpos([100, 100, 100, 100])
510 call assert_equal([4, 9], [line('.'), col('.')])
511 normal 1G
512 call setcursorcharpos('$', 1)
513 call assert_equal([4, 1], [line('.'), col('.')])
514
515 %bw!
516endfunc
517
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100518" Test for virtcol2col()
519func Test_virtcol2col()
520 new
521 call setline(1, ["a\tb\tc"])
522 call assert_equal(1, virtcol2col(0, 1, 1))
523 call assert_equal(2, virtcol2col(0, 1, 2))
524 call assert_equal(2, virtcol2col(0, 1, 8))
525 call assert_equal(3, virtcol2col(0, 1, 9))
526 call assert_equal(4, virtcol2col(0, 1, 10))
527 call assert_equal(4, virtcol2col(0, 1, 16))
528 call assert_equal(5, virtcol2col(0, 1, 17))
529 call assert_equal(-1, virtcol2col(10, 1, 1))
530 call assert_equal(-1, virtcol2col(0, 10, 1))
531 call assert_equal(-1, virtcol2col(0, -1, 1))
532 call assert_equal(-1, virtcol2col(0, 1, -1))
533 call assert_equal(5, virtcol2col(0, 1, 20))
Yegappan Lakshmananb209b862023-08-15 23:01:44 +0200534
535 " Multibyte character
536 call setline(1, ['a✅✅✅'])
537 call assert_equal(1, virtcol2col(0, 1, 1))
538 call assert_equal(2, virtcol2col(0, 1, 3))
539 call assert_equal(5, virtcol2col(0, 1, 5))
540 call assert_equal(8, virtcol2col(0, 1, 7))
541 call assert_equal(8, virtcol2col(0, 1, 8))
542
zeertzjq825cf812023-08-17 22:55:25 +0200543 let w = winwidth(0)
544 call setline(2, repeat('a', w + 2))
545 let win_nosbr = win_getid()
546 split
547 setlocal showbreak=!!
548 let win_sbr = win_getid()
549 call assert_equal(w, virtcol2col(win_nosbr, 2, w))
550 call assert_equal(w + 1, virtcol2col(win_nosbr, 2, w + 1))
551 call assert_equal(w + 2, virtcol2col(win_nosbr, 2, w + 2))
552 call assert_equal(w + 2, virtcol2col(win_nosbr, 2, w + 3))
553 call assert_equal(w, virtcol2col(win_sbr, 2, w))
554 call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 1))
555 call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 2))
556 call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 3))
557 call assert_equal(w + 2, virtcol2col(win_sbr, 2, w + 4))
558 call assert_equal(w + 2, virtcol2col(win_sbr, 2, w + 5))
559 close
560
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100561 call assert_fails('echo virtcol2col("0", 1, 20)', 'E1210:')
562 call assert_fails('echo virtcol2col(0, "1", 20)', 'E1210:')
563 call assert_fails('echo virtcol2col(0, 1, "1")', 'E1210:')
zeertzjq825cf812023-08-17 22:55:25 +0200564
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100565 bw!
566endfunc
567
Bram Moolenaar08f41572020-04-20 16:50:00 +0200568" vim: shiftwidth=2 sts=2 expandtab