blob: 1fd6b5fc4c6f39f2c6eb6cc1408d4386f7030030 [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()
Drew Vogelea67ba72025-05-07 22:05:17 +020095 if has('gui_running')
96 set lines=25
97 set columns=78
98 endif
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020099 rightbelow new
100 rightbelow 20vsplit
101 call setline(1, ["\tsome text", "long wrapping line here", "next line"])
102 redraw
103 let winid = win_getid()
104 let [winrow, wincol] = win_screenpos(winid)
105 call assert_equal({'row': winrow,
106 \ 'col': wincol + 0,
107 \ 'curscol': wincol + 7,
Bram Moolenaarf92e58c2019-09-08 21:51:41 +0200108 \ 'endcol': wincol + 7}, winid->screenpos(1, 1))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200109 call assert_equal({'row': winrow,
110 \ 'col': wincol + 13,
111 \ 'curscol': wincol + 13,
Bram Moolenaar196b4662019-09-06 21:34:30 +0200112 \ 'endcol': wincol + 13}, winid->screenpos(1, 7))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200113 call assert_equal({'row': winrow + 2,
114 \ 'col': wincol + 1,
115 \ 'curscol': wincol + 1,
116 \ 'endcol': wincol + 1}, screenpos(winid, 2, 22))
117 setlocal number
118 call assert_equal({'row': winrow + 3,
119 \ 'col': wincol + 9,
120 \ 'curscol': wincol + 9,
121 \ 'endcol': wincol + 9}, screenpos(winid, 2, 22))
Bram Moolenaar189663b2021-07-21 18:04:56 +0200122
123 let wininfo = getwininfo(winid)[0]
124 call setline(3, ['x']->repeat(wininfo.height))
125 call setline(line('$') + 1, 'x'->repeat(wininfo.width * 3))
126 setlocal nonumber display=lastline so=0
127 exe "normal G\<C-Y>\<C-Y>"
128 redraw
129 call assert_equal({'row': winrow + wininfo.height - 1,
130 \ 'col': wincol + 7,
131 \ 'curscol': wincol + 7,
132 \ 'endcol': wincol + 7}, winid->screenpos(line('$'), 8))
Bram Moolenaar7924a172022-01-24 16:15:15 +0000133 call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
zeertzjqf0e68c02023-06-03 17:11:47 +0100134 \ winid->screenpos(line('$'), 22))
135
136 1split
zeertzjqf0e68c02023-06-03 17:11:47 +0100137
zeertzjq55daae32023-06-04 19:29:22 +0100138 " w_leftcol should be subtracted
zeertzjqf0e68c02023-06-03 17:11:47 +0100139 setlocal nowrap
zeertzjqbfe377b2023-08-17 22:58:53 +0200140 normal G050zl$
141 redraw
zeertzjqf0e68c02023-06-03 17:11:47 +0100142 call assert_equal({'row': winrow + 0,
143 \ 'col': wincol + 10 - 1,
144 \ 'curscol': wincol + 10 - 1,
145 \ 'endcol': wincol + 10 - 1},
146 \ screenpos(win_getid(), line('.'), col('.')))
147
zeertzjqbfe377b2023-08-17 22:58:53 +0200148 " w_skipcol should be taken into account
149 setlocal wrap
150 normal $
151 redraw
152 call assert_equal({'row': winrow + 0,
153 \ 'col': wincol + 20 - 1,
154 \ 'curscol': wincol + 20 - 1,
155 \ 'endcol': wincol + 20 - 1},
156 \ screenpos(win_getid(), line('.'), col('.')))
157 call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
158 \ screenpos(win_getid(), line('.'), col('.') - 20))
159 setlocal number
160 redraw
161 call assert_equal({'row': winrow + 0,
162 \ 'col': wincol + 16 - 1,
163 \ 'curscol': wincol + 16 - 1,
164 \ 'endcol': wincol + 16 - 1},
165 \ screenpos(win_getid(), line('.'), col('.')))
166 call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
167 \ screenpos(win_getid(), line('.'), col('.') - 16))
168 set cpoptions+=n
169 redraw
170 call assert_equal({'row': winrow + 0,
171 \ 'col': wincol + 4 - 1,
172 \ 'curscol': wincol + 4 - 1,
173 \ 'endcol': wincol + 4 - 1},
174 \ screenpos(win_getid(), line('.'), col('.')))
175 call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
176 \ screenpos(win_getid(), line('.'), col('.') - 4))
177
178 wincmd +
179 call setline(line('$') + 1, 'last line')
180 setlocal smoothscroll
181 normal G$
182 redraw
183 call assert_equal({'row': winrow + 1,
184 \ 'col': wincol + 4 + 9 - 1,
185 \ 'curscol': wincol + 4 + 9 - 1,
186 \ 'endcol': wincol + 4 + 9 - 1},
187 \ screenpos(win_getid(), line('.'), col('.')))
188 set cpoptions-=n
189 redraw
190 call assert_equal({'row': winrow + 1,
191 \ 'col': wincol + 4 + 9 - 1,
192 \ 'curscol': wincol + 4 + 9 - 1,
193 \ 'endcol': wincol + 4 + 9 - 1},
194 \ screenpos(win_getid(), line('.'), col('.')))
195 setlocal nonumber
196 redraw
197 call assert_equal({'row': winrow + 1,
198 \ 'col': wincol + 9 - 1,
199 \ 'curscol': wincol + 9 - 1,
200 \ 'endcol': wincol + 9 - 1},
201 \ screenpos(win_getid(), line('.'), col('.')))
Bram Moolenaar189663b2021-07-21 18:04:56 +0200202
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200203 close
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200204 call assert_equal({}, screenpos(999, 1, 1))
Bram Moolenaar189663b2021-07-21 18:04:56 +0200205
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200206 bwipe!
Bram Moolenaar189663b2021-07-21 18:04:56 +0200207 set display&
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100208
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000209 call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100210 nmenu WinBar.TEST :
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000211 call assert_equal(#{col: 1, row: 2, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100212 nunmenu WinBar.TEST
zeertzjqec54af42023-12-12 16:43:44 +0100213 call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
214
215 call assert_equal(#{col: 0, row: 0, endcol: 0, curscol: 0}, screenpos(0, 0, 1))
216 call assert_equal(#{col: 0, row: 0, endcol: 0, curscol: 0}, screenpos(0, -1, 1))
217 call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(0, 1, -v:maxcol))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200218endfunc
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100219
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000220func Test_screenpos_fold()
221 CheckFeature folding
222
223 enew!
224 call setline(1, range(10))
225 3,5fold
226 redraw
227 call assert_equal(2, screenpos(1, 2, 1).row)
228 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 3, 1))
zeertzjqba2d1912022-12-18 12:28:59 +0000229 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 4, 1))
230 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 5, 1))
231 setlocal number
232 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 3, 1))
233 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 4, 1))
234 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 5, 1))
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000235 call assert_equal(4, screenpos(1, 6, 1).row)
236 bwipe!
237endfunc
238
Bram Moolenaar1cb16c32022-12-05 22:26:44 +0000239func Test_screenpos_diff()
240 CheckFeature diff
241
242 enew!
243 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
244 vnew
245 call setline(1, ['a', 'b', 'c', 'g', 'h', 'i'])
246 windo diffthis
247 wincmd w
248 call assert_equal(#{col: 3, row: 7, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
zeertzjq55daae32023-06-04 19:29:22 +0100249 call assert_equal(#{col: 3, row: 8, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
250 exe "normal! 3\<C-E>"
251 call assert_equal(#{col: 3, row: 4, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
252 call assert_equal(#{col: 3, row: 5, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
253 exe "normal! \<C-E>"
254 call assert_equal(#{col: 3, row: 3, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
255 call assert_equal(#{col: 3, row: 4, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
256 exe "normal! \<C-E>"
257 call assert_equal(#{col: 3, row: 2, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
258 call assert_equal(#{col: 3, row: 3, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
259 exe "normal! \<C-E>"
260 call assert_equal(#{col: 3, row: 1, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
261 call assert_equal(#{col: 3, row: 2, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
Bram Moolenaar1cb16c32022-12-05 22:26:44 +0000262
263 windo diffoff
264 bwipe!
265 bwipe!
266endfunc
267
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100268func Test_screenpos_number()
269 rightbelow new
270 rightbelow 73vsplit
271 call setline (1, repeat('x', 66))
272 setlocal number
273 redraw
274 let winid = win_getid()
275 let [winrow, wincol] = win_screenpos(winid)
276 let pos = screenpos(winid, 1, 66)
277 call assert_equal(winrow, pos.row)
278 call assert_equal(wincol + 66 + 3, pos.col)
Bram Moolenaar99d19432022-12-05 16:23:24 +0000279
280 call assert_fails('echo screenpos(0, 2, 1)', 'E966:')
281
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100282 close
283 bwipe!
284endfunc
Bram Moolenaar08f41572020-04-20 16:50:00 +0200285
Christian Brabandtb065a102024-10-05 17:30:22 +0200286func Test_screenpos_edit_newfile()
287 new
288 20vsp
289 setl nowrap
290 call setline(1, 'abcdefghijklmnopqrstuvwxyz')
291 call cursor(1, 10)
292 norm! 5zl
293 call assert_equal(#{col: 5, row: 1, endcol: 5, curscol: 5}, screenpos(win_getid(), 1, 10))
294 enew!
295 call assert_equal(1, &l:wrap)
296 call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
297
298 bwipe!
299endfunc
300
Bram Moolenaar91458462021-01-13 20:08:38 +0100301" Save the visual start character position
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100302func SaveVisualStartCharPos()
303 call add(g:VisualStartPos, getcharpos('v'))
304 return ''
305endfunc
306
Bram Moolenaar91458462021-01-13 20:08:38 +0100307" Save the current cursor character position in insert mode
308func SaveInsertCurrentCharPos()
309 call add(g:InsertCurrentPos, getcharpos('.'))
310 return ''
311endfunc
312
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100313" Test for the getcharpos() function
314func Test_getcharpos()
315 call assert_fails('call getcharpos({})', 'E731:')
316 call assert_equal([0, 0, 0, 0], getcharpos(0))
317 new
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100318 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678', ' │ x'])
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100319
320 " Test for '.' and '$'
321 normal 1G
322 call assert_equal([0, 1, 1, 0], getcharpos('.'))
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100323 call assert_equal([0, 5, 1, 0], getcharpos('$'))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100324 normal 2G6l
325 call assert_equal([0, 2, 7, 0], getcharpos('.'))
326 normal 3G$
327 call assert_equal([0, 3, 1, 0], getcharpos('.'))
328 normal 4G$
329 call assert_equal([0, 4, 9, 0], getcharpos('.'))
330
331 " Test for a mark
332 normal 2G7lmmgg
333 call assert_equal([0, 2, 8, 0], getcharpos("'m"))
334 delmarks m
335 call assert_equal([0, 0, 0, 0], getcharpos("'m"))
336
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100337 " Check mark does not move
338 normal 5Gfxma
339 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
340 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
341 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
342
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100343 " Test for the visual start column
344 vnoremap <expr> <F3> SaveVisualStartCharPos()
345 let g:VisualStartPos = []
346 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100347 call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100348 call assert_equal([0, 2, 9, 0], getcharpos('v'))
349 let g:VisualStartPos = []
350 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100351 call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100352 let g:VisualStartPos = []
353 exe "normal 1Gv$\<F3>o\<F3>"
354 call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos)
355 vunmap <F3>
356
Bram Moolenaar91458462021-01-13 20:08:38 +0100357 " Test for getting the position in insert mode with the cursor after the
358 " last character in a line
359 inoremap <expr> <F3> SaveInsertCurrentCharPos()
360 let g:InsertCurrentPos = []
361 exe "normal 1GA\<F3>"
362 exe "normal 2GA\<F3>"
363 exe "normal 3GA\<F3>"
364 exe "normal 4GA\<F3>"
365 exe "normal 2G6li\<F3>"
366 call assert_equal([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0],
367 \ [0, 2, 7, 0]], g:InsertCurrentPos)
368 iunmap <F3>
369
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100370 %bw!
371endfunc
372
373" Test for the setcharpos() function
374func Test_setcharpos()
375 call assert_equal(-1, setcharpos('.', test_null_list()))
376 new
377 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
378 call setcharpos('.', [0, 1, 1, 0])
379 call assert_equal([1, 1], [line('.'), col('.')])
380 call setcharpos('.', [0, 2, 7, 0])
381 call assert_equal([2, 9], [line('.'), col('.')])
382 call setcharpos('.', [0, 3, 4, 0])
383 call assert_equal([3, 1], [line('.'), col('.')])
384 call setcharpos('.', [0, 3, 1, 0])
385 call assert_equal([3, 1], [line('.'), col('.')])
386 call setcharpos('.', [0, 4, 0, 0])
387 call assert_equal([4, 1], [line('.'), col('.')])
388 call setcharpos('.', [0, 4, 20, 0])
389 call assert_equal([4, 9], [line('.'), col('.')])
390
391 " Test for mark
392 delmarks m
393 call setcharpos("'m", [0, 2, 9, 0])
394 normal `m
395 call assert_equal([2, 11], [line('.'), col('.')])
Bram Moolenaar91458462021-01-13 20:08:38 +0100396 " unload the buffer and try to set the mark
397 let bnr = bufnr()
398 enew!
399 call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0]))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100400
401 %bw!
402 call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
403endfunc
404
405func SaveVisualStartCharCol()
406 call add(g:VisualStartCol, charcol('v'))
407 return ''
408endfunc
409
Bram Moolenaar91458462021-01-13 20:08:38 +0100410func SaveInsertCurrentCharCol()
411 call add(g:InsertCurrentCol, charcol('.'))
412 return ''
413endfunc
414
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100415" Test for the charcol() function
416func Test_charcol()
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +0000417 call assert_fails('call charcol({})', 'E1222:')
418 call assert_fails('call charcol(".", [])', 'E1210:')
419 call assert_fails('call charcol(0)', 'E1222:')
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100420 new
421 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
422
423 " Test for '.' and '$'
424 normal 1G
425 call assert_equal(1, charcol('.'))
426 call assert_equal(1, charcol('$'))
427 normal 2G6l
428 call assert_equal(7, charcol('.'))
429 call assert_equal(10, charcol('$'))
430 normal 3G$
431 call assert_equal(1, charcol('.'))
432 call assert_equal(2, charcol('$'))
433 normal 4G$
434 call assert_equal(9, charcol('.'))
435 call assert_equal(10, charcol('$'))
436
437 " Test for [lnum, '$']
438 call assert_equal(1, charcol([1, '$']))
439 call assert_equal(10, charcol([2, '$']))
440 call assert_equal(2, charcol([3, '$']))
441 call assert_equal(0, charcol([5, '$']))
442
443 " Test for a mark
444 normal 2G7lmmgg
445 call assert_equal(8, charcol("'m"))
446 delmarks m
447 call assert_equal(0, charcol("'m"))
448
449 " Test for the visual start column
450 vnoremap <expr> <F3> SaveVisualStartCharCol()
451 let g:VisualStartCol = []
452 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100453 call assert_equal([7, 10, 5], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100454 call assert_equal(9, charcol('v'))
455 let g:VisualStartCol = []
456 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100457 call assert_equal([1, 2], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100458 let g:VisualStartCol = []
459 exe "normal 1Gv$\<F3>o\<F3>"
460 call assert_equal([1, 1], g:VisualStartCol)
461 vunmap <F3>
462
Bram Moolenaar91458462021-01-13 20:08:38 +0100463 " Test for getting the column number in insert mode with the cursor after
464 " the last character in a line
465 inoremap <expr> <F3> SaveInsertCurrentCharCol()
466 let g:InsertCurrentCol = []
467 exe "normal 1GA\<F3>"
468 exe "normal 2GA\<F3>"
469 exe "normal 3GA\<F3>"
470 exe "normal 4GA\<F3>"
471 exe "normal 2G6li\<F3>"
472 call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol)
473 iunmap <F3>
474
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +0000475 " Test for getting the column number in another window.
476 let winid = win_getid()
477 new
478 call win_execute(winid, 'normal 1G')
479 call assert_equal(1, charcol('.', winid))
480 call assert_equal(1, charcol('$', winid))
481 call win_execute(winid, 'normal 2G6l')
482 call assert_equal(7, charcol('.', winid))
483 call assert_equal(10, charcol('$', winid))
484
485 " calling from another tab page also works
486 tabnew
487 call assert_equal(7, charcol('.', winid))
488 call assert_equal(10, charcol('$', winid))
489 tabclose
490
491 " unknown window ID
492 call assert_equal(0, charcol('.', 10001))
493
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100494 %bw!
495endfunc
496
Bram Moolenaar91458462021-01-13 20:08:38 +0100497func SaveInsertCursorCharPos()
498 call add(g:InsertCursorPos, getcursorcharpos('.'))
499 return ''
500endfunc
501
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100502" Test for getcursorcharpos()
503func Test_getcursorcharpos()
504 call assert_equal(getcursorcharpos(), getcursorcharpos(0))
505 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1))
506 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999))
507
508 new
509 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
510 normal 1G9l
511 call assert_equal([0, 1, 1, 0, 1], getcursorcharpos())
512 normal 2G9l
513 call assert_equal([0, 2, 9, 0, 14], getcursorcharpos())
514 normal 3G9l
515 call assert_equal([0, 3, 1, 0, 1], getcursorcharpos())
516 normal 4G9l
517 call assert_equal([0, 4, 9, 0, 9], getcursorcharpos())
518
Bram Moolenaar91458462021-01-13 20:08:38 +0100519 " Test for getting the cursor position in insert mode with the cursor after
520 " the last character in a line
521 inoremap <expr> <F3> SaveInsertCursorCharPos()
522 let g:InsertCursorPos = []
523 exe "normal 1GA\<F3>"
524 exe "normal 2GA\<F3>"
525 exe "normal 3GA\<F3>"
526 exe "normal 4GA\<F3>"
527 exe "normal 2G6li\<F3>"
528 call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2],
529 \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos)
530 iunmap <F3>
531
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100532 let winid = win_getid()
533 normal 2G5l
534 wincmd w
535 call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
536 %bw!
537endfunc
538
539" Test for setcursorcharpos()
540func Test_setcursorcharpos()
541 call assert_fails('call setcursorcharpos(test_null_list())', 'E474:')
542 call assert_fails('call setcursorcharpos([1])', 'E474:')
543 call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:')
544 new
545 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
546 normal G
547 call setcursorcharpos([1, 1])
548 call assert_equal([1, 1], [line('.'), col('.')])
Bram Moolenaar79f23442022-10-10 12:42:57 +0100549
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100550 call setcursorcharpos([2, 7, 0])
551 call assert_equal([2, 9], [line('.'), col('.')])
Bram Moolenaar79f23442022-10-10 12:42:57 +0100552 call setcursorcharpos([0, 7, 0])
553 call assert_equal([2, 9], [line('.'), col('.')])
554 call setcursorcharpos(0, 7, 0)
555 call assert_equal([2, 9], [line('.'), col('.')])
556
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100557 call setcursorcharpos(3, 4)
558 call assert_equal([3, 1], [line('.'), col('.')])
559 call setcursorcharpos([3, 1])
560 call assert_equal([3, 1], [line('.'), col('.')])
561 call setcursorcharpos([4, 0, 0, 0])
562 call assert_equal([4, 1], [line('.'), col('.')])
563 call setcursorcharpos([4, 20])
564 call assert_equal([4, 9], [line('.'), col('.')])
565 normal 1G
566 call setcursorcharpos([100, 100, 100, 100])
567 call assert_equal([4, 9], [line('.'), col('.')])
568 normal 1G
569 call setcursorcharpos('$', 1)
570 call assert_equal([4, 1], [line('.'), col('.')])
571
572 %bw!
573endfunc
574
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100575" Test for virtcol2col()
576func Test_virtcol2col()
577 new
578 call setline(1, ["a\tb\tc"])
579 call assert_equal(1, virtcol2col(0, 1, 1))
580 call assert_equal(2, virtcol2col(0, 1, 2))
581 call assert_equal(2, virtcol2col(0, 1, 8))
582 call assert_equal(3, virtcol2col(0, 1, 9))
583 call assert_equal(4, virtcol2col(0, 1, 10))
584 call assert_equal(4, virtcol2col(0, 1, 16))
585 call assert_equal(5, virtcol2col(0, 1, 17))
586 call assert_equal(-1, virtcol2col(10, 1, 1))
587 call assert_equal(-1, virtcol2col(0, 10, 1))
588 call assert_equal(-1, virtcol2col(0, -1, 1))
589 call assert_equal(-1, virtcol2col(0, 1, -1))
590 call assert_equal(5, virtcol2col(0, 1, 20))
Yegappan Lakshmananb209b862023-08-15 23:01:44 +0200591
592 " Multibyte character
593 call setline(1, ['a✅✅✅'])
594 call assert_equal(1, virtcol2col(0, 1, 1))
595 call assert_equal(2, virtcol2col(0, 1, 3))
596 call assert_equal(5, virtcol2col(0, 1, 5))
597 call assert_equal(8, virtcol2col(0, 1, 7))
598 call assert_equal(8, virtcol2col(0, 1, 8))
599
zeertzjqb583eda2023-10-14 11:32:28 +0200600 " These used to cause invalid memory access
601 call setline(1, '')
602 call assert_equal(0, virtcol2col(0, 1, 1))
603 call assert_equal(0, virtcol2col(0, 1, 2))
604
zeertzjq825cf812023-08-17 22:55:25 +0200605 let w = winwidth(0)
606 call setline(2, repeat('a', w + 2))
607 let win_nosbr = win_getid()
608 split
609 setlocal showbreak=!!
610 let win_sbr = win_getid()
611 call assert_equal(w, virtcol2col(win_nosbr, 2, w))
612 call assert_equal(w + 1, virtcol2col(win_nosbr, 2, w + 1))
613 call assert_equal(w + 2, virtcol2col(win_nosbr, 2, w + 2))
614 call assert_equal(w + 2, virtcol2col(win_nosbr, 2, w + 3))
615 call assert_equal(w, virtcol2col(win_sbr, 2, w))
616 call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 1))
617 call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 2))
618 call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 3))
619 call assert_equal(w + 2, virtcol2col(win_sbr, 2, w + 4))
620 call assert_equal(w + 2, virtcol2col(win_sbr, 2, w + 5))
621 close
622
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100623 call assert_fails('echo virtcol2col("0", 1, 20)', 'E1210:')
624 call assert_fails('echo virtcol2col(0, "1", 20)', 'E1210:')
625 call assert_fails('echo virtcol2col(0, 1, "1")', 'E1210:')
zeertzjq825cf812023-08-17 22:55:25 +0200626
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100627 bw!
628endfunc
629
Bram Moolenaar08f41572020-04-20 16:50:00 +0200630" vim: shiftwidth=2 sts=2 expandtab