blob: 4fc59288c0fe564201d23d4ae6280e9348cb7cc6 [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
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200209endfunc
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100210
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000211func Test_screenpos_fold()
212 CheckFeature folding
213
214 enew!
215 call setline(1, range(10))
216 3,5fold
217 redraw
218 call assert_equal(2, screenpos(1, 2, 1).row)
219 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 3, 1))
zeertzjqba2d1912022-12-18 12:28:59 +0000220 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 4, 1))
221 call assert_equal(#{col: 1, row: 3, endcol: 1, curscol: 1}, screenpos(1, 5, 1))
222 setlocal number
223 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 3, 1))
224 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 4, 1))
225 call assert_equal(#{col: 5, row: 3, endcol: 5, curscol: 5}, screenpos(1, 5, 1))
Bram Moolenaar4556a2e2022-02-15 13:40:17 +0000226 call assert_equal(4, screenpos(1, 6, 1).row)
227 bwipe!
228endfunc
229
Bram Moolenaar1cb16c32022-12-05 22:26:44 +0000230func Test_screenpos_diff()
231 CheckFeature diff
232
233 enew!
234 call setline(1, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i'])
235 vnew
236 call setline(1, ['a', 'b', 'c', 'g', 'h', 'i'])
237 windo diffthis
238 wincmd w
239 call assert_equal(#{col: 3, row: 7, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
zeertzjq55daae32023-06-04 19:29:22 +0100240 call assert_equal(#{col: 3, row: 8, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
241 exe "normal! 3\<C-E>"
242 call assert_equal(#{col: 3, row: 4, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
243 call assert_equal(#{col: 3, row: 5, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
244 exe "normal! \<C-E>"
245 call assert_equal(#{col: 3, row: 3, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
246 call assert_equal(#{col: 3, row: 4, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
247 exe "normal! \<C-E>"
248 call assert_equal(#{col: 3, row: 2, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
249 call assert_equal(#{col: 3, row: 3, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
250 exe "normal! \<C-E>"
251 call assert_equal(#{col: 3, row: 1, endcol: 3, curscol: 3}, screenpos(0, 4, 1))
252 call assert_equal(#{col: 3, row: 2, endcol: 3, curscol: 3}, screenpos(0, 5, 1))
Bram Moolenaar1cb16c32022-12-05 22:26:44 +0000253
254 windo diffoff
255 bwipe!
256 bwipe!
257endfunc
258
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100259func Test_screenpos_number()
260 rightbelow new
261 rightbelow 73vsplit
262 call setline (1, repeat('x', 66))
263 setlocal number
264 redraw
265 let winid = win_getid()
266 let [winrow, wincol] = win_screenpos(winid)
267 let pos = screenpos(winid, 1, 66)
268 call assert_equal(winrow, pos.row)
269 call assert_equal(wincol + 66 + 3, pos.col)
Bram Moolenaar99d19432022-12-05 16:23:24 +0000270
271 call assert_fails('echo screenpos(0, 2, 1)', 'E966:')
272
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100273 close
274 bwipe!
275endfunc
Bram Moolenaar08f41572020-04-20 16:50:00 +0200276
Bram Moolenaar91458462021-01-13 20:08:38 +0100277" Save the visual start character position
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100278func SaveVisualStartCharPos()
279 call add(g:VisualStartPos, getcharpos('v'))
280 return ''
281endfunc
282
Bram Moolenaar91458462021-01-13 20:08:38 +0100283" Save the current cursor character position in insert mode
284func SaveInsertCurrentCharPos()
285 call add(g:InsertCurrentPos, getcharpos('.'))
286 return ''
287endfunc
288
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100289" Test for the getcharpos() function
290func Test_getcharpos()
291 call assert_fails('call getcharpos({})', 'E731:')
292 call assert_equal([0, 0, 0, 0], getcharpos(0))
293 new
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100294 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678', ' │ x'])
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100295
296 " Test for '.' and '$'
297 normal 1G
298 call assert_equal([0, 1, 1, 0], getcharpos('.'))
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100299 call assert_equal([0, 5, 1, 0], getcharpos('$'))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100300 normal 2G6l
301 call assert_equal([0, 2, 7, 0], getcharpos('.'))
302 normal 3G$
303 call assert_equal([0, 3, 1, 0], getcharpos('.'))
304 normal 4G$
305 call assert_equal([0, 4, 9, 0], getcharpos('.'))
306
307 " Test for a mark
308 normal 2G7lmmgg
309 call assert_equal([0, 2, 8, 0], getcharpos("'m"))
310 delmarks m
311 call assert_equal([0, 0, 0, 0], getcharpos("'m"))
312
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100313 " Check mark does not move
314 normal 5Gfxma
315 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
316 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
317 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
318
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100319 " Test for the visual start column
320 vnoremap <expr> <F3> SaveVisualStartCharPos()
321 let g:VisualStartPos = []
322 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100323 call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100324 call assert_equal([0, 2, 9, 0], getcharpos('v'))
325 let g:VisualStartPos = []
326 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100327 call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100328 let g:VisualStartPos = []
329 exe "normal 1Gv$\<F3>o\<F3>"
330 call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos)
331 vunmap <F3>
332
Bram Moolenaar91458462021-01-13 20:08:38 +0100333 " Test for getting the position in insert mode with the cursor after the
334 " last character in a line
335 inoremap <expr> <F3> SaveInsertCurrentCharPos()
336 let g:InsertCurrentPos = []
337 exe "normal 1GA\<F3>"
338 exe "normal 2GA\<F3>"
339 exe "normal 3GA\<F3>"
340 exe "normal 4GA\<F3>"
341 exe "normal 2G6li\<F3>"
342 call assert_equal([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0],
343 \ [0, 2, 7, 0]], g:InsertCurrentPos)
344 iunmap <F3>
345
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100346 %bw!
347endfunc
348
349" Test for the setcharpos() function
350func Test_setcharpos()
351 call assert_equal(-1, setcharpos('.', test_null_list()))
352 new
353 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
354 call setcharpos('.', [0, 1, 1, 0])
355 call assert_equal([1, 1], [line('.'), col('.')])
356 call setcharpos('.', [0, 2, 7, 0])
357 call assert_equal([2, 9], [line('.'), col('.')])
358 call setcharpos('.', [0, 3, 4, 0])
359 call assert_equal([3, 1], [line('.'), col('.')])
360 call setcharpos('.', [0, 3, 1, 0])
361 call assert_equal([3, 1], [line('.'), col('.')])
362 call setcharpos('.', [0, 4, 0, 0])
363 call assert_equal([4, 1], [line('.'), col('.')])
364 call setcharpos('.', [0, 4, 20, 0])
365 call assert_equal([4, 9], [line('.'), col('.')])
366
367 " Test for mark
368 delmarks m
369 call setcharpos("'m", [0, 2, 9, 0])
370 normal `m
371 call assert_equal([2, 11], [line('.'), col('.')])
Bram Moolenaar91458462021-01-13 20:08:38 +0100372 " unload the buffer and try to set the mark
373 let bnr = bufnr()
374 enew!
375 call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0]))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100376
377 %bw!
378 call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
379endfunc
380
381func SaveVisualStartCharCol()
382 call add(g:VisualStartCol, charcol('v'))
383 return ''
384endfunc
385
Bram Moolenaar91458462021-01-13 20:08:38 +0100386func SaveInsertCurrentCharCol()
387 call add(g:InsertCurrentCol, charcol('.'))
388 return ''
389endfunc
390
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100391" Test for the charcol() function
392func Test_charcol()
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +0000393 call assert_fails('call charcol({})', 'E1222:')
394 call assert_fails('call charcol(".", [])', 'E1210:')
395 call assert_fails('call charcol(0)', 'E1222:')
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100396 new
397 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
398
399 " Test for '.' and '$'
400 normal 1G
401 call assert_equal(1, charcol('.'))
402 call assert_equal(1, charcol('$'))
403 normal 2G6l
404 call assert_equal(7, charcol('.'))
405 call assert_equal(10, charcol('$'))
406 normal 3G$
407 call assert_equal(1, charcol('.'))
408 call assert_equal(2, charcol('$'))
409 normal 4G$
410 call assert_equal(9, charcol('.'))
411 call assert_equal(10, charcol('$'))
412
413 " Test for [lnum, '$']
414 call assert_equal(1, charcol([1, '$']))
415 call assert_equal(10, charcol([2, '$']))
416 call assert_equal(2, charcol([3, '$']))
417 call assert_equal(0, charcol([5, '$']))
418
419 " Test for a mark
420 normal 2G7lmmgg
421 call assert_equal(8, charcol("'m"))
422 delmarks m
423 call assert_equal(0, charcol("'m"))
424
425 " Test for the visual start column
426 vnoremap <expr> <F3> SaveVisualStartCharCol()
427 let g:VisualStartCol = []
428 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100429 call assert_equal([7, 10, 5], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100430 call assert_equal(9, charcol('v'))
431 let g:VisualStartCol = []
432 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100433 call assert_equal([1, 2], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100434 let g:VisualStartCol = []
435 exe "normal 1Gv$\<F3>o\<F3>"
436 call assert_equal([1, 1], g:VisualStartCol)
437 vunmap <F3>
438
Bram Moolenaar91458462021-01-13 20:08:38 +0100439 " Test for getting the column number in insert mode with the cursor after
440 " the last character in a line
441 inoremap <expr> <F3> SaveInsertCurrentCharCol()
442 let g:InsertCurrentCol = []
443 exe "normal 1GA\<F3>"
444 exe "normal 2GA\<F3>"
445 exe "normal 3GA\<F3>"
446 exe "normal 4GA\<F3>"
447 exe "normal 2G6li\<F3>"
448 call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol)
449 iunmap <F3>
450
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +0000451 " Test for getting the column number in another window.
452 let winid = win_getid()
453 new
454 call win_execute(winid, 'normal 1G')
455 call assert_equal(1, charcol('.', winid))
456 call assert_equal(1, charcol('$', winid))
457 call win_execute(winid, 'normal 2G6l')
458 call assert_equal(7, charcol('.', winid))
459 call assert_equal(10, charcol('$', winid))
460
461 " calling from another tab page also works
462 tabnew
463 call assert_equal(7, charcol('.', winid))
464 call assert_equal(10, charcol('$', winid))
465 tabclose
466
467 " unknown window ID
468 call assert_equal(0, charcol('.', 10001))
469
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100470 %bw!
471endfunc
472
Bram Moolenaar91458462021-01-13 20:08:38 +0100473func SaveInsertCursorCharPos()
474 call add(g:InsertCursorPos, getcursorcharpos('.'))
475 return ''
476endfunc
477
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100478" Test for getcursorcharpos()
479func Test_getcursorcharpos()
480 call assert_equal(getcursorcharpos(), getcursorcharpos(0))
481 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1))
482 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999))
483
484 new
485 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
486 normal 1G9l
487 call assert_equal([0, 1, 1, 0, 1], getcursorcharpos())
488 normal 2G9l
489 call assert_equal([0, 2, 9, 0, 14], getcursorcharpos())
490 normal 3G9l
491 call assert_equal([0, 3, 1, 0, 1], getcursorcharpos())
492 normal 4G9l
493 call assert_equal([0, 4, 9, 0, 9], getcursorcharpos())
494
Bram Moolenaar91458462021-01-13 20:08:38 +0100495 " Test for getting the cursor position in insert mode with the cursor after
496 " the last character in a line
497 inoremap <expr> <F3> SaveInsertCursorCharPos()
498 let g:InsertCursorPos = []
499 exe "normal 1GA\<F3>"
500 exe "normal 2GA\<F3>"
501 exe "normal 3GA\<F3>"
502 exe "normal 4GA\<F3>"
503 exe "normal 2G6li\<F3>"
504 call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2],
505 \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos)
506 iunmap <F3>
507
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100508 let winid = win_getid()
509 normal 2G5l
510 wincmd w
511 call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
512 %bw!
513endfunc
514
515" Test for setcursorcharpos()
516func Test_setcursorcharpos()
517 call assert_fails('call setcursorcharpos(test_null_list())', 'E474:')
518 call assert_fails('call setcursorcharpos([1])', 'E474:')
519 call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:')
520 new
521 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
522 normal G
523 call setcursorcharpos([1, 1])
524 call assert_equal([1, 1], [line('.'), col('.')])
Bram Moolenaar79f23442022-10-10 12:42:57 +0100525
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100526 call setcursorcharpos([2, 7, 0])
527 call assert_equal([2, 9], [line('.'), col('.')])
Bram Moolenaar79f23442022-10-10 12:42:57 +0100528 call setcursorcharpos([0, 7, 0])
529 call assert_equal([2, 9], [line('.'), col('.')])
530 call setcursorcharpos(0, 7, 0)
531 call assert_equal([2, 9], [line('.'), col('.')])
532
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100533 call setcursorcharpos(3, 4)
534 call assert_equal([3, 1], [line('.'), col('.')])
535 call setcursorcharpos([3, 1])
536 call assert_equal([3, 1], [line('.'), col('.')])
537 call setcursorcharpos([4, 0, 0, 0])
538 call assert_equal([4, 1], [line('.'), col('.')])
539 call setcursorcharpos([4, 20])
540 call assert_equal([4, 9], [line('.'), col('.')])
541 normal 1G
542 call setcursorcharpos([100, 100, 100, 100])
543 call assert_equal([4, 9], [line('.'), col('.')])
544 normal 1G
545 call setcursorcharpos('$', 1)
546 call assert_equal([4, 1], [line('.'), col('.')])
547
548 %bw!
549endfunc
550
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100551" Test for virtcol2col()
552func Test_virtcol2col()
553 new
554 call setline(1, ["a\tb\tc"])
555 call assert_equal(1, virtcol2col(0, 1, 1))
556 call assert_equal(2, virtcol2col(0, 1, 2))
557 call assert_equal(2, virtcol2col(0, 1, 8))
558 call assert_equal(3, virtcol2col(0, 1, 9))
559 call assert_equal(4, virtcol2col(0, 1, 10))
560 call assert_equal(4, virtcol2col(0, 1, 16))
561 call assert_equal(5, virtcol2col(0, 1, 17))
562 call assert_equal(-1, virtcol2col(10, 1, 1))
563 call assert_equal(-1, virtcol2col(0, 10, 1))
564 call assert_equal(-1, virtcol2col(0, -1, 1))
565 call assert_equal(-1, virtcol2col(0, 1, -1))
566 call assert_equal(5, virtcol2col(0, 1, 20))
Yegappan Lakshmananb209b862023-08-15 23:01:44 +0200567
568 " Multibyte character
569 call setline(1, ['a✅✅✅'])
570 call assert_equal(1, virtcol2col(0, 1, 1))
571 call assert_equal(2, virtcol2col(0, 1, 3))
572 call assert_equal(5, virtcol2col(0, 1, 5))
573 call assert_equal(8, virtcol2col(0, 1, 7))
574 call assert_equal(8, virtcol2col(0, 1, 8))
575
zeertzjq825cf812023-08-17 22:55:25 +0200576 let w = winwidth(0)
577 call setline(2, repeat('a', w + 2))
578 let win_nosbr = win_getid()
579 split
580 setlocal showbreak=!!
581 let win_sbr = win_getid()
582 call assert_equal(w, virtcol2col(win_nosbr, 2, w))
583 call assert_equal(w + 1, virtcol2col(win_nosbr, 2, w + 1))
584 call assert_equal(w + 2, virtcol2col(win_nosbr, 2, w + 2))
585 call assert_equal(w + 2, virtcol2col(win_nosbr, 2, w + 3))
586 call assert_equal(w, virtcol2col(win_sbr, 2, w))
587 call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 1))
588 call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 2))
589 call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 3))
590 call assert_equal(w + 2, virtcol2col(win_sbr, 2, w + 4))
591 call assert_equal(w + 2, virtcol2col(win_sbr, 2, w + 5))
592 close
593
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100594 call assert_fails('echo virtcol2col("0", 1, 20)', 'E1210:')
595 call assert_fails('echo virtcol2col(0, "1", 20)', 'E1210:')
596 call assert_fails('echo virtcol2col(0, 1, "1")', 'E1210:')
zeertzjq825cf812023-08-17 22:55:25 +0200597
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100598 bw!
599endfunc
600
Bram Moolenaar08f41572020-04-20 16:50:00 +0200601" vim: shiftwidth=2 sts=2 expandtab