blob: bded15e36a361f403f2eb4e5e2d482d75659997f [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
3func Test_wrong_arguments()
Bram Moolenaar37175402017-03-18 20:18:45 +01004 call assert_fails('call cursor(1. 3)', 'E474:')
Bram Moolenaar92b83cc2020-04-25 15:24:44 +02005 call assert_fails('call cursor(test_null_list())', 'E474:')
Bram Moolenaar5a46a582016-01-15 15:56:58 +01006endfunc
7
8func Test_move_cursor()
9 new
10 call setline(1, ['aaa', 'bbb', 'ccc', 'ddd'])
11
12 call cursor([1, 1, 0, 1])
13 call assert_equal([1, 1, 0, 1], getcurpos()[1:])
14 call cursor([4, 3, 0, 3])
15 call assert_equal([4, 3, 0, 3], getcurpos()[1:])
16
17 call cursor(2, 2)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010018 call assert_equal([2, 2, 0, 2], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010019 " line number zero keeps the line number
20 call cursor(0, 1)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010021 call assert_equal([2, 1, 0, 1], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010022 " col number zero keeps the column
23 call cursor(3, 0)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010024 call assert_equal([3, 1, 0, 1], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010025 " below last line goes to last line
Bram Moolenaar1a3a8912019-08-23 22:31:37 +020026 eval [9, 1]->cursor()
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010027 call assert_equal([4, 1, 0, 1], getcurpos()[1:])
Bram Moolenaar9ebcf232021-01-16 16:52:49 +010028 " pass string arguments
29 call cursor('3', '3')
30 call assert_equal([3, 3, 0, 3], getcurpos()[1:])
Bram Moolenaar5a46a582016-01-15 15:56:58 +010031
Bram Moolenaar17aca702019-05-16 22:24:55 +020032 call setline(1, ["\<TAB>"])
33 call cursor(1, 1, 1)
34 call assert_equal([1, 1, 1], getcurpos()[1:3])
35
Bram Moolenaar9a963372020-12-21 21:58:46 +010036 call assert_fails('call cursor(-1, -1)', 'E475:')
Bram Moolenaar17aca702019-05-16 22:24:55 +020037
Bram Moolenaar5a46a582016-01-15 15:56:58 +010038 quit!
39endfunc
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010040
41" Very short version of what matchparen does.
42function s:Highlight_Matching_Pair()
43 let save_cursor = getcurpos()
Bram Moolenaaraad222c2019-09-06 22:46:09 +020044 eval save_cursor->setpos('.')
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010045endfunc
46
47func Test_curswant_with_autocommand()
48 new
49 call setline(1, ['func()', '{', '}', '----'])
50 autocmd! CursorMovedI * call s:Highlight_Matching_Pair()
Bram Moolenaareb992cb2017-03-09 18:20:16 +010051 call test_override("char_avail", 1)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010052 exe "normal! 3Ga\<Down>X\<Esc>"
Bram Moolenaareb992cb2017-03-09 18:20:16 +010053 call test_override("char_avail", 0)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010054 call assert_equal('-X---', getline(4))
55 autocmd! CursorMovedI *
56 quit!
57endfunc
58
Bram Moolenaar177ab9e2019-01-15 21:12:57 +010059" Tests for behavior of curswant with cursorcolumn/line
60func Test_curswant_with_cursorcolumn()
61 new
62 call setline(1, ['01234567', ''])
63 exe "normal! ggf6j"
64 call assert_equal(6, winsaveview().curswant)
65 set cursorcolumn
66 call assert_equal(6, winsaveview().curswant)
67 quit!
68endfunc
69
70func Test_curswant_with_cursorline()
71 new
72 call setline(1, ['01234567', ''])
73 exe "normal! ggf6j"
74 call assert_equal(6, winsaveview().curswant)
75 set cursorline
76 call assert_equal(6, winsaveview().curswant)
77 quit!
78endfunc
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020079
80func Test_screenpos()
81 rightbelow new
82 rightbelow 20vsplit
83 call setline(1, ["\tsome text", "long wrapping line here", "next line"])
84 redraw
85 let winid = win_getid()
86 let [winrow, wincol] = win_screenpos(winid)
87 call assert_equal({'row': winrow,
88 \ 'col': wincol + 0,
89 \ 'curscol': wincol + 7,
Bram Moolenaarf92e58c2019-09-08 21:51:41 +020090 \ 'endcol': wincol + 7}, winid->screenpos(1, 1))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020091 call assert_equal({'row': winrow,
92 \ 'col': wincol + 13,
93 \ 'curscol': wincol + 13,
Bram Moolenaar196b4662019-09-06 21:34:30 +020094 \ 'endcol': wincol + 13}, winid->screenpos(1, 7))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +020095 call assert_equal({'row': winrow + 2,
96 \ 'col': wincol + 1,
97 \ 'curscol': wincol + 1,
98 \ 'endcol': wincol + 1}, screenpos(winid, 2, 22))
99 setlocal number
100 call assert_equal({'row': winrow + 3,
101 \ 'col': wincol + 9,
102 \ 'curscol': wincol + 9,
103 \ 'endcol': wincol + 9}, screenpos(winid, 2, 22))
104 close
Bram Moolenaarbdd2c292020-06-22 21:34:30 +0200105 call assert_equal({}, screenpos(999, 1, 1))
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200106 bwipe!
Bram Moolenaar8dd46e72020-12-17 21:35:29 +0100107
108 call assert_equal({'col': 1, 'row': 1, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1))
109 nmenu WinBar.TEST :
110 call assert_equal({'col': 1, 'row': 2, 'endcol': 1, 'curscol': 1}, screenpos(win_getid(), 1, 1))
111 nunmenu WinBar.TEST
Bram Moolenaarb3d17a22019-07-07 18:28:14 +0200112endfunc
Bram Moolenaar38ba4dc2019-10-27 21:39:09 +0100113
114func Test_screenpos_number()
115 rightbelow new
116 rightbelow 73vsplit
117 call setline (1, repeat('x', 66))
118 setlocal number
119 redraw
120 let winid = win_getid()
121 let [winrow, wincol] = win_screenpos(winid)
122 let pos = screenpos(winid, 1, 66)
123 call assert_equal(winrow, pos.row)
124 call assert_equal(wincol + 66 + 3, pos.col)
125 close
126 bwipe!
127endfunc
Bram Moolenaar08f41572020-04-20 16:50:00 +0200128
Bram Moolenaar91458462021-01-13 20:08:38 +0100129" Save the visual start character position
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100130func SaveVisualStartCharPos()
131 call add(g:VisualStartPos, getcharpos('v'))
132 return ''
133endfunc
134
Bram Moolenaar91458462021-01-13 20:08:38 +0100135" Save the current cursor character position in insert mode
136func SaveInsertCurrentCharPos()
137 call add(g:InsertCurrentPos, getcharpos('.'))
138 return ''
139endfunc
140
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100141" Test for the getcharpos() function
142func Test_getcharpos()
143 call assert_fails('call getcharpos({})', 'E731:')
144 call assert_equal([0, 0, 0, 0], getcharpos(0))
145 new
146 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
147
148 " Test for '.' and '$'
149 normal 1G
150 call assert_equal([0, 1, 1, 0], getcharpos('.'))
151 call assert_equal([0, 4, 1, 0], getcharpos('$'))
152 normal 2G6l
153 call assert_equal([0, 2, 7, 0], getcharpos('.'))
154 normal 3G$
155 call assert_equal([0, 3, 1, 0], getcharpos('.'))
156 normal 4G$
157 call assert_equal([0, 4, 9, 0], getcharpos('.'))
158
159 " Test for a mark
160 normal 2G7lmmgg
161 call assert_equal([0, 2, 8, 0], getcharpos("'m"))
162 delmarks m
163 call assert_equal([0, 0, 0, 0], getcharpos("'m"))
164
165 " Test for the visual start column
166 vnoremap <expr> <F3> SaveVisualStartCharPos()
167 let g:VisualStartPos = []
168 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100169 call assert_equal([[0, 2, 7, 0], [0, 2, 10, 0], [0, 2, 5, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100170 call assert_equal([0, 2, 9, 0], getcharpos('v'))
171 let g:VisualStartPos = []
172 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100173 call assert_equal([[0, 3, 1, 0], [0, 3, 2, 0]], g:VisualStartPos)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100174 let g:VisualStartPos = []
175 exe "normal 1Gv$\<F3>o\<F3>"
176 call assert_equal([[0, 1, 1, 0], [0, 1, 1, 0]], g:VisualStartPos)
177 vunmap <F3>
178
Bram Moolenaar91458462021-01-13 20:08:38 +0100179 " Test for getting the position in insert mode with the cursor after the
180 " last character in a line
181 inoremap <expr> <F3> SaveInsertCurrentCharPos()
182 let g:InsertCurrentPos = []
183 exe "normal 1GA\<F3>"
184 exe "normal 2GA\<F3>"
185 exe "normal 3GA\<F3>"
186 exe "normal 4GA\<F3>"
187 exe "normal 2G6li\<F3>"
188 call assert_equal([[0, 1, 1, 0], [0, 2, 10, 0], [0, 3, 2, 0], [0, 4, 10, 0],
189 \ [0, 2, 7, 0]], g:InsertCurrentPos)
190 iunmap <F3>
191
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100192 %bw!
193endfunc
194
195" Test for the setcharpos() function
196func Test_setcharpos()
197 call assert_equal(-1, setcharpos('.', test_null_list()))
198 new
199 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
200 call setcharpos('.', [0, 1, 1, 0])
201 call assert_equal([1, 1], [line('.'), col('.')])
202 call setcharpos('.', [0, 2, 7, 0])
203 call assert_equal([2, 9], [line('.'), col('.')])
204 call setcharpos('.', [0, 3, 4, 0])
205 call assert_equal([3, 1], [line('.'), col('.')])
206 call setcharpos('.', [0, 3, 1, 0])
207 call assert_equal([3, 1], [line('.'), col('.')])
208 call setcharpos('.', [0, 4, 0, 0])
209 call assert_equal([4, 1], [line('.'), col('.')])
210 call setcharpos('.', [0, 4, 20, 0])
211 call assert_equal([4, 9], [line('.'), col('.')])
212
213 " Test for mark
214 delmarks m
215 call setcharpos("'m", [0, 2, 9, 0])
216 normal `m
217 call assert_equal([2, 11], [line('.'), col('.')])
Bram Moolenaar91458462021-01-13 20:08:38 +0100218 " unload the buffer and try to set the mark
219 let bnr = bufnr()
220 enew!
221 call assert_equal(-1, setcharpos("'m", [bnr, 2, 2, 0]))
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100222
223 %bw!
224 call assert_equal(-1, setcharpos('.', [10, 3, 1, 0]))
225endfunc
226
227func SaveVisualStartCharCol()
228 call add(g:VisualStartCol, charcol('v'))
229 return ''
230endfunc
231
Bram Moolenaar91458462021-01-13 20:08:38 +0100232func SaveInsertCurrentCharCol()
233 call add(g:InsertCurrentCol, charcol('.'))
234 return ''
235endfunc
236
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100237" Test for the charcol() function
238func Test_charcol()
239 call assert_fails('call charcol({})', 'E731:')
240 call assert_equal(0, charcol(0))
241 new
242 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
243
244 " Test for '.' and '$'
245 normal 1G
246 call assert_equal(1, charcol('.'))
247 call assert_equal(1, charcol('$'))
248 normal 2G6l
249 call assert_equal(7, charcol('.'))
250 call assert_equal(10, charcol('$'))
251 normal 3G$
252 call assert_equal(1, charcol('.'))
253 call assert_equal(2, charcol('$'))
254 normal 4G$
255 call assert_equal(9, charcol('.'))
256 call assert_equal(10, charcol('$'))
257
258 " Test for [lnum, '$']
259 call assert_equal(1, charcol([1, '$']))
260 call assert_equal(10, charcol([2, '$']))
261 call assert_equal(2, charcol([3, '$']))
262 call assert_equal(0, charcol([5, '$']))
263
264 " Test for a mark
265 normal 2G7lmmgg
266 call assert_equal(8, charcol("'m"))
267 delmarks m
268 call assert_equal(0, charcol("'m"))
269
270 " Test for the visual start column
271 vnoremap <expr> <F3> SaveVisualStartCharCol()
272 let g:VisualStartCol = []
273 exe "normal 2G6lv$\<F3>ohh\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100274 call assert_equal([7, 10, 5], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100275 call assert_equal(9, charcol('v'))
276 let g:VisualStartCol = []
277 exe "normal 3Gv$\<F3>o\<F3>"
Bram Moolenaar91458462021-01-13 20:08:38 +0100278 call assert_equal([1, 2], g:VisualStartCol)
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100279 let g:VisualStartCol = []
280 exe "normal 1Gv$\<F3>o\<F3>"
281 call assert_equal([1, 1], g:VisualStartCol)
282 vunmap <F3>
283
Bram Moolenaar91458462021-01-13 20:08:38 +0100284 " Test for getting the column number in insert mode with the cursor after
285 " the last character in a line
286 inoremap <expr> <F3> SaveInsertCurrentCharCol()
287 let g:InsertCurrentCol = []
288 exe "normal 1GA\<F3>"
289 exe "normal 2GA\<F3>"
290 exe "normal 3GA\<F3>"
291 exe "normal 4GA\<F3>"
292 exe "normal 2G6li\<F3>"
293 call assert_equal([1, 10, 2, 10, 7], g:InsertCurrentCol)
294 iunmap <F3>
295
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100296 %bw!
297endfunc
298
Bram Moolenaar91458462021-01-13 20:08:38 +0100299func SaveInsertCursorCharPos()
300 call add(g:InsertCursorPos, getcursorcharpos('.'))
301 return ''
302endfunc
303
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100304" Test for getcursorcharpos()
305func Test_getcursorcharpos()
306 call assert_equal(getcursorcharpos(), getcursorcharpos(0))
307 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(-1))
308 call assert_equal([0, 0, 0, 0, 0], getcursorcharpos(1999))
309
310 new
311 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
312 normal 1G9l
313 call assert_equal([0, 1, 1, 0, 1], getcursorcharpos())
314 normal 2G9l
315 call assert_equal([0, 2, 9, 0, 14], getcursorcharpos())
316 normal 3G9l
317 call assert_equal([0, 3, 1, 0, 1], getcursorcharpos())
318 normal 4G9l
319 call assert_equal([0, 4, 9, 0, 9], getcursorcharpos())
320
Bram Moolenaar91458462021-01-13 20:08:38 +0100321 " Test for getting the cursor position in insert mode with the cursor after
322 " the last character in a line
323 inoremap <expr> <F3> SaveInsertCursorCharPos()
324 let g:InsertCursorPos = []
325 exe "normal 1GA\<F3>"
326 exe "normal 2GA\<F3>"
327 exe "normal 3GA\<F3>"
328 exe "normal 4GA\<F3>"
329 exe "normal 2G6li\<F3>"
330 call assert_equal([[0, 1, 1, 0, 1], [0, 2, 10, 0, 15], [0, 3, 2, 0, 2],
331 \ [0, 4, 10, 0, 10], [0, 2, 7, 0, 12]], g:InsertCursorPos)
332 iunmap <F3>
333
Bram Moolenaar6f02b002021-01-10 20:22:54 +0100334 let winid = win_getid()
335 normal 2G5l
336 wincmd w
337 call assert_equal([0, 2, 6, 0, 11], getcursorcharpos(winid))
338 %bw!
339endfunc
340
341" Test for setcursorcharpos()
342func Test_setcursorcharpos()
343 call assert_fails('call setcursorcharpos(test_null_list())', 'E474:')
344 call assert_fails('call setcursorcharpos([1])', 'E474:')
345 call assert_fails('call setcursorcharpos([1, 1, 1, 1, 1])', 'E474:')
346 new
347 call setline(1, ['', "01\tà4è678", 'Ⅵ', '012345678'])
348 normal G
349 call setcursorcharpos([1, 1])
350 call assert_equal([1, 1], [line('.'), col('.')])
351 call setcursorcharpos([2, 7, 0])
352 call assert_equal([2, 9], [line('.'), col('.')])
353 call setcursorcharpos(3, 4)
354 call assert_equal([3, 1], [line('.'), col('.')])
355 call setcursorcharpos([3, 1])
356 call assert_equal([3, 1], [line('.'), col('.')])
357 call setcursorcharpos([4, 0, 0, 0])
358 call assert_equal([4, 1], [line('.'), col('.')])
359 call setcursorcharpos([4, 20])
360 call assert_equal([4, 9], [line('.'), col('.')])
361 normal 1G
362 call setcursorcharpos([100, 100, 100, 100])
363 call assert_equal([4, 9], [line('.'), col('.')])
364 normal 1G
365 call setcursorcharpos('$', 1)
366 call assert_equal([4, 1], [line('.'), col('.')])
367
368 %bw!
369endfunc
370
Bram Moolenaar08f41572020-04-20 16:50:00 +0200371" vim: shiftwidth=2 sts=2 expandtab