blob: 8766f97d9dc1781316072d7d9003262ba05fc01f [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
zeertzjqf0e68c02023-06-03 17:11:47 +0100133
zeertzjq55daae32023-06-04 19:29:22 +0100134 " w_leftcol should be subtracted
zeertzjqf0e68c02023-06-03 17:11:47 +0100135 setlocal nowrap
zeertzjqbfe377b2023-08-17 22:58:53 +0200136 normal G050zl$
137 redraw
zeertzjqf0e68c02023-06-03 17:11:47 +0100138 call assert_equal({'row': winrow + 0,
139 \ 'col': wincol + 10 - 1,
140 \ 'curscol': wincol + 10 - 1,
141 \ 'endcol': wincol + 10 - 1},
142 \ screenpos(win_getid(), line('.'), col('.')))
143
zeertzjqbfe377b2023-08-17 22:58:53 +0200144 " w_skipcol should be taken into account
145 setlocal wrap
146 normal $
147 redraw
148 call assert_equal({'row': winrow + 0,
149 \ 'col': wincol + 20 - 1,
150 \ 'curscol': wincol + 20 - 1,
151 \ 'endcol': wincol + 20 - 1},
152 \ screenpos(win_getid(), line('.'), col('.')))
153 call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
154 \ screenpos(win_getid(), line('.'), col('.') - 20))
155 setlocal number
156 redraw
157 call assert_equal({'row': winrow + 0,
158 \ 'col': wincol + 16 - 1,
159 \ 'curscol': wincol + 16 - 1,
160 \ 'endcol': wincol + 16 - 1},
161 \ screenpos(win_getid(), line('.'), col('.')))
162 call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
163 \ screenpos(win_getid(), line('.'), col('.') - 16))
164 set cpoptions+=n
165 redraw
166 call assert_equal({'row': winrow + 0,
167 \ 'col': wincol + 4 - 1,
168 \ 'curscol': wincol + 4 - 1,
169 \ 'endcol': wincol + 4 - 1},
170 \ screenpos(win_getid(), line('.'), col('.')))
171 call assert_equal({'row': 0, 'col': 0, 'curscol': 0, 'endcol': 0},
172 \ screenpos(win_getid(), line('.'), col('.') - 4))
173
174 wincmd +
175 call setline(line('$') + 1, 'last line')
176 setlocal smoothscroll
177 normal G$
178 redraw
179 call assert_equal({'row': winrow + 1,
180 \ 'col': wincol + 4 + 9 - 1,
181 \ 'curscol': wincol + 4 + 9 - 1,
182 \ 'endcol': wincol + 4 + 9 - 1},
183 \ screenpos(win_getid(), line('.'), col('.')))
184 set cpoptions-=n
185 redraw
186 call assert_equal({'row': winrow + 1,
187 \ 'col': wincol + 4 + 9 - 1,
188 \ 'curscol': wincol + 4 + 9 - 1,
189 \ 'endcol': wincol + 4 + 9 - 1},
190 \ screenpos(win_getid(), line('.'), col('.')))
191 setlocal nonumber
192 redraw
193 call assert_equal({'row': winrow + 1,
194 \ 'col': wincol + 9 - 1,
195 \ 'curscol': wincol + 9 - 1,
196 \ 'endcol': wincol + 9 - 1},
197 \ screenpos(win_getid(), line('.'), col('.')))
Bram Moolenaar189663b2021-07-21 18:04:56 +0200198
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200199 close
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200200 call assert_equal({}, screenpos(999, 1, 1))
Bram Moolenaar189663b2021-07-21 18:04:56 +0200201
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200202 bwipe!
Bram Moolenaar189663b2021-07-21 18:04:56 +0200203 set display&
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100204
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000205 call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100206 nmenu WinBar.TEST :
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000207 call assert_equal(#{col: 1, row: 2, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100208 nunmenu WinBar.TEST
zeertzjqec54af42023-12-12 16:43:44 +0100209 call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
210
211 call assert_equal(#{col: 0, row: 0, endcol: 0, curscol: 0}, screenpos(0, 0, 1))
212 call assert_equal(#{col: 0, row: 0, endcol: 0, curscol: 0}, screenpos(0, -1, 1))
213 call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(0, 1, -v:maxcol))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200214endfunc
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100215
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000216func Test_screenpos_fold()
217 CheckFeature folding
218
219 enew!
220 call setline(1, range(10))
221 3,5fold
222 redraw
223 call assert_equal(2, screenpos(1, 2, 1).row)
224 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 3, 1))
zeertzjqba2d1912022-12-18 12:28:59 +0000225 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 4, 1))
226 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 5, 1))
227 setlocal number
228 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 3, 1))
229 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 4, 1))
230 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 5, 1))
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000231 call assert_equal(4, screenpos(1, 6, 1).row)
232 bwipe!
233endfunc
234
Bram Moolenaar1cb16c32022-12-05 22:26:44 +0000235func Test_screenpos_diff()
236 CheckFeature diff
237
238 enew!
239 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
240 vnew
241 call setline(1, ['a', 'b', 'c', 'g', 'h', 'i'])
242 windo diffthis
243 wincmd w
244 call assert_equal(#{col: 3, row: 7, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
zeertzjq55daae32023-06-04 19:29:22 +0100245 call assert_equal(#{col: 3, row: 8, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
246 exe "normal! 3\<C-E>"
247 call assert_equal(#{col: 3, row: 4, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
248 call assert_equal(#{col: 3, row: 5, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
249 exe "normal! \<C-E>"
250 call assert_equal(#{col: 3, row: 3, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
251 call assert_equal(#{col: 3, row: 4, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
252 exe "normal! \<C-E>"
253 call assert_equal(#{col: 3, row: 2, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
254 call assert_equal(#{col: 3, row: 3, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
255 exe "normal! \<C-E>"
256 call assert_equal(#{col: 3, row: 1, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
257 call assert_equal(#{col: 3, row: 2, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
Bram Moolenaar1cb16c32022-12-05 22:26:44 +0000258
259 windo diffoff
260 bwipe!
261 bwipe!
262endfunc
263
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100264func Test_screenpos_number()
265 rightbelow new
266 rightbelow 73vsplit
267 call setline (1, repeat('x', 66))
268 setlocal number
269 redraw
270 let winid = win_getid()
271 let [winrow, wincol] = win_screenpos(winid)
272 let pos = screenpos(winid, 1, 66)
273 call assert_equal(winrow, pos.row)
274 call assert_equal(wincol + 66 + 3, pos.col)
Bram Moolenaar99d19432022-12-05 16:23:24 +0000275
276 call assert_fails('echo screenpos(0, 2, 1)', 'E966:')
277
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100278 close
279 bwipe!
280endfunc
Bram Moolenaar08f41572020-04-20 16:50:00 +0200281
Christian Brabandtb065a102024-10-05 17:30:22 +0200282func Test_screenpos_edit_newfile()
283 new
284 20vsp
285 setl nowrap
286 call setline(1, 'abcdefghijklmnopqrstuvwxyz')
287 call cursor(1, 10)
288 norm! 5zl
289 call assert_equal(#{col: 5, row: 1, endcol: 5, curscol: 5}, screenpos(win_getid(), 1, 10))
290 enew!
291 call assert_equal(1, &l:wrap)
292 call assert_equal(#{col: 1, row: 1, endcol: 1, curscol: 1}, screenpos(win_getid(), 1, 1))
293
294 bwipe!
295endfunc
296
Bram Moolenaar91458462021-01-13 20:08:38 +0100297" Save the visual start character position
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100298func SaveVisualStartCharPos()
299 call add(g:VisualStartPos, getcharpos('v'))
300 return ''
301endfunc
302
Bram Moolenaar91458462021-01-13 20:08:38 +0100303" Save the current cursor character position in insert mode
304func SaveInsertCurrentCharPos()
305 call add(g:InsertCurrentPos, getcharpos('.'))
306 return ''
307endfunc
308
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100309" Test for the getcharpos() function
310func Test_getcharpos()
311 call assert_fails('call getcharpos({})', 'E731:')
312 call assert_equal([0, 0, 0, 0], getcharpos(0))
313 new
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100314 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678', ' │ x'])
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100315
316 " Test for '.' and '$'
317 normal 1G
318 call assert_equal([0, 1, 1, 0], getcharpos('.'))
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100319 call assert_equal([0, 5, 1, 0], getcharpos('$'))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100320 normal 2G6l
321 call assert_equal([0, 2, 7, 0], getcharpos('.'))
322 normal 3G$
323 call assert_equal([0, 3, 1, 0], getcharpos('.'))
324 normal 4G$
325 call assert_equal([0, 4, 9, 0], getcharpos('.'))
326
327 " Test for a mark
328 normal 2G7lmmgg
329 call assert_equal([0, 2, 8, 0], getcharpos("'m"))
330 delmarks m
331 call assert_equal([0, 0, 0, 0], getcharpos("'m"))
332
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100333 " Check mark does not move
334 normal 5Gfxma
335 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
336 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
337 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
338
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100339 " Test for the visual start column
340 vnoremap <expr> <F3> SaveVisualStartCharPos()
341 let g:VisualStartPos = []
342 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100343 call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100344 call assert_equal([0, 2, 9, 0], getcharpos('v'))
345 let g:VisualStartPos = []
346 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100347 call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100348 let g:VisualStartPos = []
349 exe "normal 1Gv$\<F3>o\<F3>"
350 call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos)
351 vunmap <F3>
352
Bram Moolenaar91458462021-01-13 20:08:38 +0100353 " Test for getting the position in insert mode with the cursor after the
354 " last character in a line
355 inoremap <expr> <F3> SaveInsertCurrentCharPos()
356 let g:InsertCurrentPos = []
357 exe "normal 1GA\<F3>"
358 exe "normal 2GA\<F3>"
359 exe "normal 3GA\<F3>"
360 exe "normal 4GA\<F3>"
361 exe "normal 2G6li\<F3>"
362 call assert_equal([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0],
363 \ [0, 2, 7, 0]], g:InsertCurrentPos)
364 iunmap <F3>
365
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100366 %bw!
367endfunc
368
369" Test for the setcharpos() function
370func Test_setcharpos()
371 call assert_equal(-1, setcharpos('.', test_null_list()))
372 new
373 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
374 call setcharpos('.', [0, 1, 1, 0])
375 call assert_equal([1, 1], [line('.'), col('.')])
376 call setcharpos('.', [0, 2, 7, 0])
377 call assert_equal([2, 9], [line('.'), col('.')])
378 call setcharpos('.', [0, 3, 4, 0])
379 call assert_equal([3, 1], [line('.'), col('.')])
380 call setcharpos('.', [0, 3, 1, 0])
381 call assert_equal([3, 1], [line('.'), col('.')])
382 call setcharpos('.', [0, 4, 0, 0])
383 call assert_equal([4, 1], [line('.'), col('.')])
384 call setcharpos('.', [0, 4, 20, 0])
385 call assert_equal([4, 9], [line('.'), col('.')])
386
387 " Test for mark
388 delmarks m
389 call setcharpos("'m", [0, 2, 9, 0])
390 normal `m
391 call assert_equal([2, 11], [line('.'), col('.')])
Bram Moolenaar91458462021-01-13 20:08:38 +0100392 " unload the buffer and try to set the mark
393 let bnr = bufnr()
394 enew!
395 call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0]))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100396
397 %bw!
398 call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
399endfunc
400
401func SaveVisualStartCharCol()
402 call add(g:VisualStartCol, charcol('v'))
403 return ''
404endfunc
405
Bram Moolenaar91458462021-01-13 20:08:38 +0100406func SaveInsertCurrentCharCol()
407 call add(g:InsertCurrentCol, charcol('.'))
408 return ''
409endfunc
410
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100411" Test for the charcol() function
412func Test_charcol()
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +0000413 call assert_fails('call charcol({})', 'E1222:')
414 call assert_fails('call charcol(".", [])', 'E1210:')
415 call assert_fails('call charcol(0)', 'E1222:')
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100416 new
417 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
418
419 " Test for '.' and '$'
420 normal 1G
421 call assert_equal(1, charcol('.'))
422 call assert_equal(1, charcol('$'))
423 normal 2G6l
424 call assert_equal(7, charcol('.'))
425 call assert_equal(10, charcol('$'))
426 normal 3G$
427 call assert_equal(1, charcol('.'))
428 call assert_equal(2, charcol('$'))
429 normal 4G$
430 call assert_equal(9, charcol('.'))
431 call assert_equal(10, charcol('$'))
432
433 " Test for [lnum, '$']
434 call assert_equal(1, charcol([1, '$']))
435 call assert_equal(10, charcol([2, '$']))
436 call assert_equal(2, charcol([3, '$']))
437 call assert_equal(0, charcol([5, '$']))
438
439 " Test for a mark
440 normal 2G7lmmgg
441 call assert_equal(8, charcol("'m"))
442 delmarks m
443 call assert_equal(0, charcol("'m"))
444
445 " Test for the visual start column
446 vnoremap <expr> <F3> SaveVisualStartCharCol()
447 let g:VisualStartCol = []
448 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100449 call assert_equal([7, 10, 5], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100450 call assert_equal(9, charcol('v'))
451 let g:VisualStartCol = []
452 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100453 call assert_equal([1, 2], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100454 let g:VisualStartCol = []
455 exe "normal 1Gv$\<F3>o\<F3>"
456 call assert_equal([1, 1], g:VisualStartCol)
457 vunmap <F3>
458
Bram Moolenaar91458462021-01-13 20:08:38 +0100459 " Test for getting the column number in insert mode with the cursor after
460 " the last character in a line
461 inoremap <expr> <F3> SaveInsertCurrentCharCol()
462 let g:InsertCurrentCol = []
463 exe "normal 1GA\<F3>"
464 exe "normal 2GA\<F3>"
465 exe "normal 3GA\<F3>"
466 exe "normal 4GA\<F3>"
467 exe "normal 2G6li\<F3>"
468 call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol)
469 iunmap <F3>
470
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +0000471 " Test for getting the column number in another window.
472 let winid = win_getid()
473 new
474 call win_execute(winid, 'normal 1G')
475 call assert_equal(1, charcol('.', winid))
476 call assert_equal(1, charcol('$', winid))
477 call win_execute(winid, 'normal 2G6l')
478 call assert_equal(7, charcol('.', winid))
479 call assert_equal(10, charcol('$', winid))
480
481 " calling from another tab page also works
482 tabnew
483 call assert_equal(7, charcol('.', winid))
484 call assert_equal(10, charcol('$', winid))
485 tabclose
486
487 " unknown window ID
488 call assert_equal(0, charcol('.', 10001))
489
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100490 %bw!
491endfunc
492
Bram Moolenaar91458462021-01-13 20:08:38 +0100493func SaveInsertCursorCharPos()
494 call add(g:InsertCursorPos, getcursorcharpos('.'))
495 return ''
496endfunc
497
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100498" Test for getcursorcharpos()
499func Test_getcursorcharpos()
500 call assert_equal(getcursorcharpos(), getcursorcharpos(0))
501 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1))
502 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999))
503
504 new
505 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
506 normal 1G9l
507 call assert_equal([0, 1, 1, 0, 1], getcursorcharpos())
508 normal 2G9l
509 call assert_equal([0, 2, 9, 0, 14], getcursorcharpos())
510 normal 3G9l
511 call assert_equal([0, 3, 1, 0, 1], getcursorcharpos())
512 normal 4G9l
513 call assert_equal([0, 4, 9, 0, 9], getcursorcharpos())
514
Bram Moolenaar91458462021-01-13 20:08:38 +0100515 " Test for getting the cursor position in insert mode with the cursor after
516 " the last character in a line
517 inoremap <expr> <F3> SaveInsertCursorCharPos()
518 let g:InsertCursorPos = []
519 exe "normal 1GA\<F3>"
520 exe "normal 2GA\<F3>"
521 exe "normal 3GA\<F3>"
522 exe "normal 4GA\<F3>"
523 exe "normal 2G6li\<F3>"
524 call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2],
525 \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos)
526 iunmap <F3>
527
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100528 let winid = win_getid()
529 normal 2G5l
530 wincmd w
531 call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
532 %bw!
533endfunc
534
535" Test for setcursorcharpos()
536func Test_setcursorcharpos()
537 call assert_fails('call setcursorcharpos(test_null_list())', 'E474:')
538 call assert_fails('call setcursorcharpos([1])', 'E474:')
539 call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:')
540 new
541 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
542 normal G
543 call setcursorcharpos([1, 1])
544 call assert_equal([1, 1], [line('.'), col('.')])
Bram Moolenaar79f23442022-10-10 12:42:57 +0100545
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100546 call setcursorcharpos([2, 7, 0])
547 call assert_equal([2, 9], [line('.'), col('.')])
Bram Moolenaar79f23442022-10-10 12:42:57 +0100548 call setcursorcharpos([0, 7, 0])
549 call assert_equal([2, 9], [line('.'), col('.')])
550 call setcursorcharpos(0, 7, 0)
551 call assert_equal([2, 9], [line('.'), col('.')])
552
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100553 call setcursorcharpos(3, 4)
554 call assert_equal([3, 1], [line('.'), col('.')])
555 call setcursorcharpos([3, 1])
556 call assert_equal([3, 1], [line('.'), col('.')])
557 call setcursorcharpos([4, 0, 0, 0])
558 call assert_equal([4, 1], [line('.'), col('.')])
559 call setcursorcharpos([4, 20])
560 call assert_equal([4, 9], [line('.'), col('.')])
561 normal 1G
562 call setcursorcharpos([100, 100, 100, 100])
563 call assert_equal([4, 9], [line('.'), col('.')])
564 normal 1G
565 call setcursorcharpos('$', 1)
566 call assert_equal([4, 1], [line('.'), col('.')])
567
568 %bw!
569endfunc
570
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100571" Test for virtcol2col()
572func Test_virtcol2col()
573 new
574 call setline(1, ["a\tb\tc"])
575 call assert_equal(1, virtcol2col(0, 1, 1))
576 call assert_equal(2, virtcol2col(0, 1, 2))
577 call assert_equal(2, virtcol2col(0, 1, 8))
578 call assert_equal(3, virtcol2col(0, 1, 9))
579 call assert_equal(4, virtcol2col(0, 1, 10))
580 call assert_equal(4, virtcol2col(0, 1, 16))
581 call assert_equal(5, virtcol2col(0, 1, 17))
582 call assert_equal(-1, virtcol2col(10, 1, 1))
583 call assert_equal(-1, virtcol2col(0, 10, 1))
584 call assert_equal(-1, virtcol2col(0, -1, 1))
585 call assert_equal(-1, virtcol2col(0, 1, -1))
586 call assert_equal(5, virtcol2col(0, 1, 20))
Yegappan Lakshmananb209b862023-08-15 23:01:44 +0200587
588 " Multibyte character
589 call setline(1, ['a✅✅✅'])
590 call assert_equal(1, virtcol2col(0, 1, 1))
591 call assert_equal(2, virtcol2col(0, 1, 3))
592 call assert_equal(5, virtcol2col(0, 1, 5))
593 call assert_equal(8, virtcol2col(0, 1, 7))
594 call assert_equal(8, virtcol2col(0, 1, 8))
595
zeertzjqb583eda2023-10-14 11:32:28 +0200596 " These used to cause invalid memory access
597 call setline(1, '')
598 call assert_equal(0, virtcol2col(0, 1, 1))
599 call assert_equal(0, virtcol2col(0, 1, 2))
600
zeertzjq825cf812023-08-17 22:55:25 +0200601 let w = winwidth(0)
602 call setline(2, repeat('a', w + 2))
603 let win_nosbr = win_getid()
604 split
605 setlocal showbreak=!!
606 let win_sbr = win_getid()
607 call assert_equal(w, virtcol2col(win_nosbr, 2, w))
608 call assert_equal(w + 1, virtcol2col(win_nosbr, 2, w + 1))
609 call assert_equal(w + 2, virtcol2col(win_nosbr, 2, w + 2))
610 call assert_equal(w + 2, virtcol2col(win_nosbr, 2, w + 3))
611 call assert_equal(w, virtcol2col(win_sbr, 2, w))
612 call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 1))
613 call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 2))
614 call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 3))
615 call assert_equal(w + 2, virtcol2col(win_sbr, 2, w + 4))
616 call assert_equal(w + 2, virtcol2col(win_sbr, 2, w + 5))
617 close
618
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100619 call assert_fails('echo virtcol2col("0", 1, 20)', 'E1210:')
620 call assert_fails('echo virtcol2col(0, "1", 20)', 'E1210:')
621 call assert_fails('echo virtcol2col(0, 1, "1")', 'E1210:')
zeertzjq825cf812023-08-17 22:55:25 +0200622
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100623 bw!
624endfunc
625
Bram Moolenaar08f41572020-04-20 16:50:00 +0200626" vim: shiftwidth=2 sts=2 expandtab