blob: 2bdda869e617490897f2ae4deaa7c683127ebd6b [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
Bram Moolenaar91458462021-01-13 20:08:38 +0100282" Save the visual start character position
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100283func SaveVisualStartCharPos()
284 call add(g:VisualStartPos, getcharpos('v'))
285 return ''
286endfunc
287
Bram Moolenaar91458462021-01-13 20:08:38 +0100288" Save the current cursor character position in insert mode
289func SaveInsertCurrentCharPos()
290 call add(g:InsertCurrentPos, getcharpos('.'))
291 return ''
292endfunc
293
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100294" Test for the getcharpos() function
295func Test_getcharpos()
296 call assert_fails('call getcharpos({})', 'E731:')
297 call assert_equal([0, 0, 0, 0], getcharpos(0))
298 new
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100299 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678', ' │ x'])
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100300
301 " Test for '.' and '$'
302 normal 1G
303 call assert_equal([0, 1, 1, 0], getcharpos('.'))
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100304 call assert_equal([0, 5, 1, 0], getcharpos('$'))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100305 normal 2G6l
306 call assert_equal([0, 2, 7, 0], getcharpos('.'))
307 normal 3G$
308 call assert_equal([0, 3, 1, 0], getcharpos('.'))
309 normal 4G$
310 call assert_equal([0, 4, 9, 0], getcharpos('.'))
311
312 " Test for a mark
313 normal 2G7lmmgg
314 call assert_equal([0, 2, 8, 0], getcharpos("'m"))
315 delmarks m
316 call assert_equal([0, 0, 0, 0], getcharpos("'m"))
317
Bram Moolenaar3caf1cc2022-04-11 13:05:16 +0100318 " Check mark does not move
319 normal 5Gfxma
320 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
321 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
322 call assert_equal([0, 5, 5, 0], getcharpos("'a"))
323
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100324 " Test for the visual start column
325 vnoremap <expr> <F3> SaveVisualStartCharPos()
326 let g:VisualStartPos = []
327 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100328 call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100329 call assert_equal([0, 2, 9, 0], getcharpos('v'))
330 let g:VisualStartPos = []
331 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100332 call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100333 let g:VisualStartPos = []
334 exe "normal 1Gv$\<F3>o\<F3>"
335 call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos)
336 vunmap <F3>
337
Bram Moolenaar91458462021-01-13 20:08:38 +0100338 " Test for getting the position in insert mode with the cursor after the
339 " last character in a line
340 inoremap <expr> <F3> SaveInsertCurrentCharPos()
341 let g:InsertCurrentPos = []
342 exe "normal 1GA\<F3>"
343 exe "normal 2GA\<F3>"
344 exe "normal 3GA\<F3>"
345 exe "normal 4GA\<F3>"
346 exe "normal 2G6li\<F3>"
347 call assert_equal([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0],
348 \ [0, 2, 7, 0]], g:InsertCurrentPos)
349 iunmap <F3>
350
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100351 %bw!
352endfunc
353
354" Test for the setcharpos() function
355func Test_setcharpos()
356 call assert_equal(-1, setcharpos('.', test_null_list()))
357 new
358 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
359 call setcharpos('.', [0, 1, 1, 0])
360 call assert_equal([1, 1], [line('.'), col('.')])
361 call setcharpos('.', [0, 2, 7, 0])
362 call assert_equal([2, 9], [line('.'), col('.')])
363 call setcharpos('.', [0, 3, 4, 0])
364 call assert_equal([3, 1], [line('.'), col('.')])
365 call setcharpos('.', [0, 3, 1, 0])
366 call assert_equal([3, 1], [line('.'), col('.')])
367 call setcharpos('.', [0, 4, 0, 0])
368 call assert_equal([4, 1], [line('.'), col('.')])
369 call setcharpos('.', [0, 4, 20, 0])
370 call assert_equal([4, 9], [line('.'), col('.')])
371
372 " Test for mark
373 delmarks m
374 call setcharpos("'m", [0, 2, 9, 0])
375 normal `m
376 call assert_equal([2, 11], [line('.'), col('.')])
Bram Moolenaar91458462021-01-13 20:08:38 +0100377 " unload the buffer and try to set the mark
378 let bnr = bufnr()
379 enew!
380 call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0]))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100381
382 %bw!
383 call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
384endfunc
385
386func SaveVisualStartCharCol()
387 call add(g:VisualStartCol, charcol('v'))
388 return ''
389endfunc
390
Bram Moolenaar91458462021-01-13 20:08:38 +0100391func SaveInsertCurrentCharCol()
392 call add(g:InsertCurrentCol, charcol('.'))
393 return ''
394endfunc
395
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100396" Test for the charcol() function
397func Test_charcol()
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +0000398 call assert_fails('call charcol({})', 'E1222:')
399 call assert_fails('call charcol(".", [])', 'E1210:')
400 call assert_fails('call charcol(0)', 'E1222:')
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100401 new
402 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
403
404 " Test for '.' and '$'
405 normal 1G
406 call assert_equal(1, charcol('.'))
407 call assert_equal(1, charcol('$'))
408 normal 2G6l
409 call assert_equal(7, charcol('.'))
410 call assert_equal(10, charcol('$'))
411 normal 3G$
412 call assert_equal(1, charcol('.'))
413 call assert_equal(2, charcol('$'))
414 normal 4G$
415 call assert_equal(9, charcol('.'))
416 call assert_equal(10, charcol('$'))
417
418 " Test for [lnum, '$']
419 call assert_equal(1, charcol([1, '$']))
420 call assert_equal(10, charcol([2, '$']))
421 call assert_equal(2, charcol([3, '$']))
422 call assert_equal(0, charcol([5, '$']))
423
424 " Test for a mark
425 normal 2G7lmmgg
426 call assert_equal(8, charcol("'m"))
427 delmarks m
428 call assert_equal(0, charcol("'m"))
429
430 " Test for the visual start column
431 vnoremap <expr> <F3> SaveVisualStartCharCol()
432 let g:VisualStartCol = []
433 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100434 call assert_equal([7, 10, 5], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100435 call assert_equal(9, charcol('v'))
436 let g:VisualStartCol = []
437 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100438 call assert_equal([1, 2], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100439 let g:VisualStartCol = []
440 exe "normal 1Gv$\<F3>o\<F3>"
441 call assert_equal([1, 1], g:VisualStartCol)
442 vunmap <F3>
443
Bram Moolenaar91458462021-01-13 20:08:38 +0100444 " Test for getting the column number in insert mode with the cursor after
445 " the last character in a line
446 inoremap <expr> <F3> SaveInsertCurrentCharCol()
447 let g:InsertCurrentCol = []
448 exe "normal 1GA\<F3>"
449 exe "normal 2GA\<F3>"
450 exe "normal 3GA\<F3>"
451 exe "normal 4GA\<F3>"
452 exe "normal 2G6li\<F3>"
453 call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol)
454 iunmap <F3>
455
Yegappan Lakshmanan4c8d2f02022-11-12 16:07:47 +0000456 " Test for getting the column number in another window.
457 let winid = win_getid()
458 new
459 call win_execute(winid, 'normal 1G')
460 call assert_equal(1, charcol('.', winid))
461 call assert_equal(1, charcol('$', winid))
462 call win_execute(winid, 'normal 2G6l')
463 call assert_equal(7, charcol('.', winid))
464 call assert_equal(10, charcol('$', winid))
465
466 " calling from another tab page also works
467 tabnew
468 call assert_equal(7, charcol('.', winid))
469 call assert_equal(10, charcol('$', winid))
470 tabclose
471
472 " unknown window ID
473 call assert_equal(0, charcol('.', 10001))
474
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100475 %bw!
476endfunc
477
Bram Moolenaar91458462021-01-13 20:08:38 +0100478func SaveInsertCursorCharPos()
479 call add(g:InsertCursorPos, getcursorcharpos('.'))
480 return ''
481endfunc
482
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100483" Test for getcursorcharpos()
484func Test_getcursorcharpos()
485 call assert_equal(getcursorcharpos(), getcursorcharpos(0))
486 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1))
487 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999))
488
489 new
490 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
491 normal 1G9l
492 call assert_equal([0, 1, 1, 0, 1], getcursorcharpos())
493 normal 2G9l
494 call assert_equal([0, 2, 9, 0, 14], getcursorcharpos())
495 normal 3G9l
496 call assert_equal([0, 3, 1, 0, 1], getcursorcharpos())
497 normal 4G9l
498 call assert_equal([0, 4, 9, 0, 9], getcursorcharpos())
499
Bram Moolenaar91458462021-01-13 20:08:38 +0100500 " Test for getting the cursor position in insert mode with the cursor after
501 " the last character in a line
502 inoremap <expr> <F3> SaveInsertCursorCharPos()
503 let g:InsertCursorPos = []
504 exe "normal 1GA\<F3>"
505 exe "normal 2GA\<F3>"
506 exe "normal 3GA\<F3>"
507 exe "normal 4GA\<F3>"
508 exe "normal 2G6li\<F3>"
509 call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2],
510 \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos)
511 iunmap <F3>
512
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100513 let winid = win_getid()
514 normal 2G5l
515 wincmd w
516 call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
517 %bw!
518endfunc
519
520" Test for setcursorcharpos()
521func Test_setcursorcharpos()
522 call assert_fails('call setcursorcharpos(test_null_list())', 'E474:')
523 call assert_fails('call setcursorcharpos([1])', 'E474:')
524 call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:')
525 new
526 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
527 normal G
528 call setcursorcharpos([1, 1])
529 call assert_equal([1, 1], [line('.'), col('.')])
Bram Moolenaar79f23442022-10-10 12:42:57 +0100530
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100531 call setcursorcharpos([2, 7, 0])
532 call assert_equal([2, 9], [line('.'), col('.')])
Bram Moolenaar79f23442022-10-10 12:42:57 +0100533 call setcursorcharpos([0, 7, 0])
534 call assert_equal([2, 9], [line('.'), col('.')])
535 call setcursorcharpos(0, 7, 0)
536 call assert_equal([2, 9], [line('.'), col('.')])
537
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100538 call setcursorcharpos(3, 4)
539 call assert_equal([3, 1], [line('.'), col('.')])
540 call setcursorcharpos([3, 1])
541 call assert_equal([3, 1], [line('.'), col('.')])
542 call setcursorcharpos([4, 0, 0, 0])
543 call assert_equal([4, 1], [line('.'), col('.')])
544 call setcursorcharpos([4, 20])
545 call assert_equal([4, 9], [line('.'), col('.')])
546 normal 1G
547 call setcursorcharpos([100, 100, 100, 100])
548 call assert_equal([4, 9], [line('.'), col('.')])
549 normal 1G
550 call setcursorcharpos('$', 1)
551 call assert_equal([4, 1], [line('.'), col('.')])
552
553 %bw!
554endfunc
555
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100556" Test for virtcol2col()
557func Test_virtcol2col()
558 new
559 call setline(1, ["a\tb\tc"])
560 call assert_equal(1, virtcol2col(0, 1, 1))
561 call assert_equal(2, virtcol2col(0, 1, 2))
562 call assert_equal(2, virtcol2col(0, 1, 8))
563 call assert_equal(3, virtcol2col(0, 1, 9))
564 call assert_equal(4, virtcol2col(0, 1, 10))
565 call assert_equal(4, virtcol2col(0, 1, 16))
566 call assert_equal(5, virtcol2col(0, 1, 17))
567 call assert_equal(-1, virtcol2col(10, 1, 1))
568 call assert_equal(-1, virtcol2col(0, 10, 1))
569 call assert_equal(-1, virtcol2col(0, -1, 1))
570 call assert_equal(-1, virtcol2col(0, 1, -1))
571 call assert_equal(5, virtcol2col(0, 1, 20))
Yegappan Lakshmananb209b862023-08-15 23:01:44 +0200572
573 " Multibyte character
574 call setline(1, ['a✅✅✅'])
575 call assert_equal(1, virtcol2col(0, 1, 1))
576 call assert_equal(2, virtcol2col(0, 1, 3))
577 call assert_equal(5, virtcol2col(0, 1, 5))
578 call assert_equal(8, virtcol2col(0, 1, 7))
579 call assert_equal(8, virtcol2col(0, 1, 8))
580
zeertzjqb583eda2023-10-14 11:32:28 +0200581 " These used to cause invalid memory access
582 call setline(1, '')
583 call assert_equal(0, virtcol2col(0, 1, 1))
584 call assert_equal(0, virtcol2col(0, 1, 2))
585
zeertzjq825cf812023-08-17 22:55:25 +0200586 let w = winwidth(0)
587 call setline(2, repeat('a', w + 2))
588 let win_nosbr = win_getid()
589 split
590 setlocal showbreak=!!
591 let win_sbr = win_getid()
592 call assert_equal(w, virtcol2col(win_nosbr, 2, w))
593 call assert_equal(w + 1, virtcol2col(win_nosbr, 2, w + 1))
594 call assert_equal(w + 2, virtcol2col(win_nosbr, 2, w + 2))
595 call assert_equal(w + 2, virtcol2col(win_nosbr, 2, w + 3))
596 call assert_equal(w, virtcol2col(win_sbr, 2, w))
597 call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 1))
598 call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 2))
599 call assert_equal(w + 1, virtcol2col(win_sbr, 2, w + 3))
600 call assert_equal(w + 2, virtcol2col(win_sbr, 2, w + 4))
601 call assert_equal(w + 2, virtcol2col(win_sbr, 2, w + 5))
602 close
603
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100604 call assert_fails('echo virtcol2col("0", 1, 20)', 'E1210:')
605 call assert_fails('echo virtcol2col(0, "1", 20)', 'E1210:')
606 call assert_fails('echo virtcol2col(0, 1, "1")', 'E1210:')
zeertzjq825cf812023-08-17 22:55:25 +0200607
Bram Moolenaar5a6ec102022-05-27 21:58:00 +0100608 bw!
609endfunc
610
Bram Moolenaar08f41572020-04-20 16:50:00 +0200611" vim: shiftwidth=2 sts=2 expandtab