blob: 2afe3e885ece8ab9673944c0c36bf97c2e7954af [file] [log] [blame]
Bram Moolenaardf7df592020-06-14 13:50:55 +02001" Test for the various 'cpoptions' (cpo) flags
Bram Moolenaarc9630d22020-06-13 13:20:48 +02002
Bram Moolenaarc9630d22020-06-13 13:20:48 +02003" Test for the 'a' flag in 'cpo'. Reading a file should set the alternate
4" file name.
5func Test_cpo_a()
6 let save_cpo = &cpo
Bram Moolenaar45bbaef2022-09-08 16:39:22 +01007 call writefile(['one'], 'XfileCpoA', 'D')
Bram Moolenaarc9630d22020-06-13 13:20:48 +02008 " Wipe out all the buffers, so that the alternate file is empty
9 edit Xfoo | %bw
10 set cpo-=a
11 new
Bram Moolenaara85e4db2022-08-28 17:44:20 +010012 read XfileCpoA
Bram Moolenaarc9630d22020-06-13 13:20:48 +020013 call assert_equal('', @#)
14 %d
15 set cpo+=a
Bram Moolenaara85e4db2022-08-28 17:44:20 +010016 read XfileCpoA
17 call assert_equal('XfileCpoA', @#)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +020018 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +020019 let &cpo = save_cpo
20endfunc
21
22" Test for the 'A' flag in 'cpo'. Writing a file should set the alternate
23" file name.
24func Test_cpo_A()
25 let save_cpo = &cpo
26 " Wipe out all the buffers, so that the alternate file is empty
27 edit Xfoo | %bw
28 set cpo-=A
Bram Moolenaar61abe7d2022-08-30 21:46:08 +010029 new XcpoAfile1
30 write XcpoAfile2
Bram Moolenaarc9630d22020-06-13 13:20:48 +020031 call assert_equal('', @#)
32 %bw
Bram Moolenaar61abe7d2022-08-30 21:46:08 +010033 call delete('XcpoAfile2')
34 new XcpoAfile1
Bram Moolenaarc9630d22020-06-13 13:20:48 +020035 set cpo+=A
Bram Moolenaar61abe7d2022-08-30 21:46:08 +010036 write XcpoAfile2
37 call assert_equal('XcpoAfile2', @#)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +020038 bw!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +010039 call delete('XcpoAfile2')
Bram Moolenaarc9630d22020-06-13 13:20:48 +020040 let &cpo = save_cpo
41endfunc
42
43" Test for the 'b' flag in 'cpo'. "\|" at the end of a map command is
44" recognized as the end of the map.
45func Test_cpo_b()
46 let save_cpo = &cpo
47 set cpo+=b
48 nnoremap <F5> :pwd\<CR>\|let i = 1
49 call assert_equal(':pwd\<CR>\', maparg('<F5>'))
50 nunmap <F5>
51 exe "nnoremap <F5> :pwd\<C-V>|let i = 1"
52 call assert_equal(':pwd|let i = 1', maparg('<F5>'))
53 nunmap <F5>
54 set cpo-=b
55 nnoremap <F5> :pwd\<CR>\|let i = 1
56 call assert_equal(':pwd\<CR>|let i = 1', maparg('<F5>'))
57 let &cpo = save_cpo
58 nunmap <F5>
59endfunc
60
Bram Moolenaardf7df592020-06-14 13:50:55 +020061" Test for the 'B' flag in 'cpo'. A backslash in mappings, abbreviations, user
62" commands and menu commands has no special meaning.
63func Test_cpo_B()
64 let save_cpo = &cpo
65 new
zeertzjqc3a26c62023-02-17 16:40:20 +000066 imap <buffer> x<Bslash>k Test
Bram Moolenaardf7df592020-06-14 13:50:55 +020067 set cpo-=B
68 iabbr <buffer> abc ab\<BS>d
69 exe "normal iabc "
70 call assert_equal('ab<BS>d ', getline(1))
zeertzjqc3a26c62023-02-17 16:40:20 +000071 call feedkeys(":imap <buffer> x\<C-A>\<C-B>\"\<CR>", 'tx')
72 call assert_equal('"imap <buffer> x\\k', @:)
Bram Moolenaardf7df592020-06-14 13:50:55 +020073 %d
74 set cpo+=B
75 iabbr <buffer> abc ab\<BS>d
76 exe "normal iabc "
77 call assert_equal('abd ', getline(1))
zeertzjqc3a26c62023-02-17 16:40:20 +000078 call feedkeys(":imap <buffer> x\<C-A>\<C-B>\"\<CR>", 'tx')
79 call assert_equal('"imap <buffer> x\k', @:)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +020080 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +020081 let &cpo = save_cpo
82endfunc
83
Bram Moolenaarc9630d22020-06-13 13:20:48 +020084" Test for the 'c' flag in 'cpo'.
85func Test_cpo_c()
86 let save_cpo = &cpo
87 set cpo+=c
88 new
89 call setline(1, ' abababababab')
90 exe "normal gg/abab\<CR>"
91 call assert_equal(3, searchcount().total)
92 set cpo-=c
93 exe "normal gg/abab\<CR>"
94 call assert_equal(5, searchcount().total)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +020095 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +020096 let &cpo = save_cpo
97endfunc
98
99" Test for the 'C' flag in 'cpo' (line continuation)
100func Test_cpo_C()
101 let save_cpo = &cpo
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100102 call writefile(['let l = [', '\ 1,', '\ 2]'], 'XfileCpoC', 'D')
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200103 set cpo-=C
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100104 source XfileCpoC
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200105 call assert_equal([1, 2], g:l)
106 set cpo+=C
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100107 call assert_fails('source XfileCpoC', ['E697:', 'E10:'])
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200108 let &cpo = save_cpo
109endfunc
110
111" Test for the 'd' flag in 'cpo' (tags relative to the current file)
112func Test_cpo_d()
113 let save_cpo = &cpo
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100114 call mkdir('XdirCpoD', 'R')
115 call writefile(["one\tXfile1\t/^one$/"], 'tags', 'D')
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100116 call writefile(["two\tXfile2\t/^two$/"], 'XdirCpoD/tags')
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200117 set tags=./tags
118 set cpo-=d
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100119 edit XdirCpoD/Xfile
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200120 call assert_equal('two', taglist('.*')[0].name)
121 set cpo+=d
122 call assert_equal('one', taglist('.*')[0].name)
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100123
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200124 %bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200125 set tags&
126 let &cpo = save_cpo
127endfunc
128
129" Test for the 'D' flag in 'cpo' (digraph after a r, f or t)
130func Test_cpo_D()
131 CheckFeature digraphs
132 let save_cpo = &cpo
133 new
134 set cpo-=D
135 call setline(1, 'abcdefgh|')
136 exe "norm! 1gg0f\<c-k>!!"
137 call assert_equal(9, col('.'))
138 set cpo+=D
139 exe "norm! 1gg0f\<c-k>!!"
140 call assert_equal(1, col('.'))
141 set cpo-=D
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200142 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200143 let &cpo = save_cpo
144endfunc
145
146" Test for the 'e' flag in 'cpo'
147func Test_cpo_e()
148 let save_cpo = &cpo
149 let @a='let i = 45'
150 set cpo+=e
151 call feedkeys(":@a\<CR>", 'xt')
152 call assert_equal(45, i)
153 set cpo-=e
154 call feedkeys(":@a\<CR>6\<CR>", 'xt')
155 call assert_equal(456, i)
156 let &cpo = save_cpo
157endfunc
158
159" Test for the 'E' flag in 'cpo' with yank, change, delete, etc. operators
160func Test_cpo_E()
161 new
162 call setline(1, '')
163 set cpo+=E
164 " yank an empty line
165 call assert_beeps('normal "ayl')
166 " change an empty line
167 call assert_beeps('normal lcTa')
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200168 call assert_beeps('normal 0c0')
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200169 " delete an empty line
170 call assert_beeps('normal D')
171 call assert_beeps('normal dl')
172 call assert_equal('', getline(1))
173 " change case of an empty line
174 call assert_beeps('normal gul')
175 call assert_beeps('normal gUl')
176 " replace a character
177 call assert_beeps('normal vrx')
178 " increment and decrement
179 call assert_beeps('exe "normal v\<C-A>"')
180 call assert_beeps('exe "normal v\<C-X>"')
181 set cpo-=E
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200182 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200183endfunc
184
185" Test for the 'f' flag in 'cpo' (read in an empty buffer sets the file name)
186func Test_cpo_f()
187 let save_cpo = &cpo
188 new
189 set cpo-=f
190 read test_cpoptions.vim
191 call assert_equal('', @%)
192 %d
193 set cpo+=f
194 read test_cpoptions.vim
195 call assert_equal('test_cpoptions.vim', @%)
zeertzjqc3a26c62023-02-17 16:40:20 +0000196
197 bwipe!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200198 let &cpo = save_cpo
199endfunc
200
201" Test for the 'F' flag in 'cpo' (write in an empty buffer sets the file name)
202func Test_cpo_F()
203 let save_cpo = &cpo
204 new
205 set cpo-=F
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100206 write XfileCpoF
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200207 call assert_equal('', @%)
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100208 call delete('XfileCpoF')
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200209 set cpo+=F
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100210 write XfileCpoF
211 call assert_equal('XfileCpoF', @%)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200212 bw!
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100213 call delete('XfileCpoF')
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200214 let &cpo = save_cpo
215endfunc
216
217" Test for the 'g' flag in 'cpo' (jump to line 1 when re-editing a file)
218func Test_cpo_g()
219 let save_cpo = &cpo
220 new test_cpoptions.vim
221 set cpo-=g
222 normal 20G
223 edit
224 call assert_equal(20, line('.'))
225 set cpo+=g
226 edit
227 call assert_equal(1, line('.'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200228 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200229 let &cpo = save_cpo
230endfunc
231
232" Test for inserting text in a line with only spaces ('H' flag in 'cpoptions')
233func Test_cpo_H()
234 let save_cpo = &cpo
235 new
236 set cpo-=H
237 call setline(1, ' ')
238 normal! Ia
239 call assert_equal(' a', getline(1))
240 set cpo+=H
241 call setline(1, ' ')
242 normal! Ia
243 call assert_equal(' a ', getline(1))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200244 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200245 let &cpo = save_cpo
246endfunc
247
Bram Moolenaardf7df592020-06-14 13:50:55 +0200248" TODO: Add a test for the 'i' flag in 'cpo'
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200249" Interrupting the reading of a file will leave it modified.
Bram Moolenaardf7df592020-06-14 13:50:55 +0200250
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200251" Test for the 'I' flag in 'cpo' (deleting autoindent when using arrow keys)
252func Test_cpo_I()
253 let save_cpo = &cpo
254 new
255 setlocal autoindent
256 set cpo+=I
257 exe "normal i one\<CR>\<Up>"
258 call assert_equal(' ', getline(2))
259 set cpo-=I
260 %d
261 exe "normal i one\<CR>\<Up>"
262 call assert_equal('', getline(2))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200263 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200264 let &cpo = save_cpo
265endfunc
266
Bram Moolenaardf7df592020-06-14 13:50:55 +0200267" Test for the 'j' flag in 'cpo' is in the test_join.vim file.
268
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200269" Test for the 'J' flag in 'cpo' (two spaces after a sentence)
270func Test_cpo_J()
271 let save_cpo = &cpo
272 new
273 set cpo-=J
274 call setline(1, 'one. two! three? four."'' five.)]')
275 normal 0
276 for colnr in [6, 12, 19, 28, 34]
277 normal )
278 call assert_equal(colnr, col('.'))
279 endfor
280 for colnr in [28, 19, 12, 6, 1]
281 normal (
282 call assert_equal(colnr, col('.'))
283 endfor
284 set cpo+=J
285 normal 0
286 for colnr in [12, 28, 34]
287 normal )
288 call assert_equal(colnr, col('.'))
289 endfor
290 for colnr in [28, 12, 1]
291 normal (
292 call assert_equal(colnr, col('.'))
293 endfor
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200294 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200295 let &cpo = save_cpo
296endfunc
297
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200298" TODO: Add a test for the 'k' flag in 'cpo'.
299" Disable the recognition of raw key codes in mappings, abbreviations, and the
300" "to" part of menu commands.
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200301
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200302" TODO: Add a test for the 'K' flag in 'cpo'.
303" Don't wait for a key code to complete when it is halfway a mapping.
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200304
305" Test for the 'l' flag in 'cpo' (backslash in a [] range)
306func Test_cpo_l()
307 let save_cpo = &cpo
308 new
309 call setline(1, ['', "a\tc" .. '\t'])
310 set cpo-=l
311 exe 'normal gg/[\t]' .. "\<CR>"
312 call assert_equal([2, 8], [col('.'), virtcol('.')])
313 set cpo+=l
314 exe 'normal gg/[\t]' .. "\<CR>"
315 call assert_equal([4, 10], [col('.'), virtcol('.')])
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200316 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200317 let &cpo = save_cpo
318endfunc
319
320" Test for inserting tab in virtual replace mode ('L' flag in 'cpoptions')
321func Test_cpo_L()
322 let save_cpo = &cpo
323 new
324 set cpo-=L
325 call setline(1, 'abcdefghijklmnopqr')
326 exe "normal 0gR\<Tab>"
327 call assert_equal("\<Tab>ijklmnopqr", getline(1))
328 set cpo+=L
329 set list
330 call setline(1, 'abcdefghijklmnopqr')
331 exe "normal 0gR\<Tab>"
332 call assert_equal("\<Tab>cdefghijklmnopqr", getline(1))
333 set nolist
334 call setline(1, 'abcdefghijklmnopqr')
335 exe "normal 0gR\<Tab>"
336 call assert_equal("\<Tab>ijklmnopqr", getline(1))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200337 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200338 let &cpo = save_cpo
339endfunc
340
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200341" TODO: Add a test for the 'm' flag in 'cpo'.
342" When included, a showmatch will always wait half a second. When not
343" included, a showmatch will wait half a second or until a character is typed.
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200344
345" Test for the 'M' flag in 'cpo' (% with escape parenthesis)
346func Test_cpo_M()
347 let save_cpo = &cpo
348 new
349 call setline(1, ['( \( )', '\( ( \)'])
350
351 set cpo-=M
352 call cursor(1, 1)
353 normal %
354 call assert_equal(6, col('.'))
355 call cursor(1, 4)
356 call assert_beeps('normal %')
357 call cursor(2, 2)
358 normal %
359 call assert_equal(7, col('.'))
360 call cursor(2, 4)
361 call assert_beeps('normal %')
362
363 set cpo+=M
364 call cursor(1, 4)
365 normal %
366 call assert_equal(6, col('.'))
367 call cursor(1, 1)
368 call assert_beeps('normal %')
369 call cursor(2, 4)
370 normal %
371 call assert_equal(7, col('.'))
372 call cursor(2, 1)
373 call assert_beeps('normal %')
374
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200375 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200376 let &cpo = save_cpo
377endfunc
378
379" Test for the 'n' flag in 'cpo' (using number column for wrapped lines)
380func Test_cpo_n()
381 let save_cpo = &cpo
382 new
383 call setline(1, repeat('a', &columns))
384 setlocal number
385 set cpo-=n
386 redraw!
387 call assert_equal(' aaaa', Screenline(2))
388 set cpo+=n
389 redraw!
390 call assert_equal('aaaa', Screenline(2))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200391 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200392 let &cpo = save_cpo
393endfunc
394
395" Test for the 'o' flag in 'cpo' (line offset to search command)
396func Test_cpo_o()
397 let save_cpo = &cpo
398 new
399 call setline(1, ['', 'one', 'two', 'three', 'one', 'two', 'three'])
400 set cpo-=o
401 exe "normal /one/+2\<CR>"
402 normal n
403 call assert_equal(7, line('.'))
404 set cpo+=o
405 exe "normal /one/+2\<CR>"
406 normal n
407 call assert_equal(5, line('.'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200408 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200409 let &cpo = save_cpo
410endfunc
411
412" Test for the 'O' flag in 'cpo' (overwriting an existing file)
413func Test_cpo_O()
414 let save_cpo = &cpo
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100415 new XfileCpoO
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200416 call setline(1, 'one')
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100417 call writefile(['two'], 'XfileCpoO', 'D')
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200418 set cpo-=O
419 call assert_fails('write', 'E13:')
420 set cpo+=O
421 write
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100422 call assert_equal(['one'], readfile('XfileCpoO'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200423 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200424 let &cpo = save_cpo
425endfunc
426
Bram Moolenaard26c5802022-10-13 12:30:08 +0100427" Test for the 'p' flag in 'cpo' is in the test_lispindent.vim file.
Bram Moolenaardf7df592020-06-14 13:50:55 +0200428
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200429" Test for the 'P' flag in 'cpo' (appending to a file sets the current file
430" name)
431func Test_cpo_P()
432 let save_cpo = &cpo
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100433 call writefile([], 'XfileCpoP', 'D')
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200434 new
435 call setline(1, 'one')
436 set cpo+=F
437 set cpo-=P
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100438 write >> XfileCpoP
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200439 call assert_equal('', @%)
440 set cpo+=P
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100441 write >> XfileCpoP
442 call assert_equal('XfileCpoP', @%)
zeertzjqc3a26c62023-02-17 16:40:20 +0000443
444 bwipe!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200445 let &cpo = save_cpo
446endfunc
447
448" Test for the 'q' flag in 'cpo' (joining multiple lines)
449func Test_cpo_q()
450 let save_cpo = &cpo
451 new
452 call setline(1, ['one', 'two', 'three', 'four', 'five'])
453 set cpo-=q
454 normal gg4J
455 call assert_equal(14, col('.'))
456 %d
457 call setline(1, ['one', 'two', 'three', 'four', 'five'])
458 set cpo+=q
459 normal gg4J
460 call assert_equal(4, col('.'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200461 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200462 let &cpo = save_cpo
463endfunc
464
465" Test for the 'r' flag in 'cpo' (redo command with a search motion)
466func Test_cpo_r()
467 let save_cpo = &cpo
468 new
469 call setline(1, repeat(['one two three four'], 2))
470 set cpo-=r
471 exe "normal ggc/two\<CR>abc "
472 let @/ = 'three'
473 normal 2G.
474 call assert_equal('abc two three four', getline(2))
475 %d
476 call setline(1, repeat(['one two three four'], 2))
477 set cpo+=r
478 exe "normal ggc/two\<CR>abc "
479 let @/ = 'three'
480 normal 2G.
481 call assert_equal('abc three four', getline(2))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200482 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200483 let &cpo = save_cpo
484endfunc
485
486" Test for the 'R' flag in 'cpo' (clear marks after a filter command)
487func Test_cpo_R()
488 CheckUnix
489 let save_cpo = &cpo
490 new
491 call setline(1, ['three', 'one', 'two'])
492 set cpo-=R
493 3mark r
494 %!sort
495 call assert_equal(3, line("'r"))
496 %d
497 call setline(1, ['three', 'one', 'two'])
498 set cpo+=R
499 3mark r
500 %!sort
501 call assert_equal(0, line("'r"))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200502 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200503 let &cpo = save_cpo
504endfunc
505
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200506" TODO: Add a test for the 's' flag in 'cpo'.
507" Set buffer options when entering the buffer for the first time. If not
508" present the options are set when the buffer is created.
Bram Moolenaardf7df592020-06-14 13:50:55 +0200509
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200510" Test for the 'S' flag in 'cpo' (copying buffer options)
511func Test_cpo_S()
512 let save_cpo = &cpo
513 new Xfile1
514 set noautoindent
515 new Xfile2
516 set cpo-=S
517 set autoindent
518 wincmd p
519 call assert_equal(0, &autoindent)
520 wincmd p
521 call assert_equal(1, &autoindent)
522 set cpo+=S
523 wincmd p
524 call assert_equal(1, &autoindent)
525 set noautoindent
526 wincmd p
527 call assert_equal(0, &autoindent)
528 wincmd t
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200529 bw!
530 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200531 let &cpo = save_cpo
532endfunc
533
Bram Moolenaardf7df592020-06-14 13:50:55 +0200534" Test for the 't' flag in 'cpo' is in the test_tagjump.vim file.
535
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200536" Test for the 'u' flag in 'cpo' (Vi-compatible undo)
537func Test_cpo_u()
538 let save_cpo = &cpo
539 new
540 set cpo-=u
541 exe "normal iabc\<C-G>udef\<C-G>ughi"
542 normal uu
543 call assert_equal('abc', getline(1))
544 %d
545 set cpo+=u
546 exe "normal iabc\<C-G>udef\<C-G>ughi"
547 normal uu
548 call assert_equal('abcdefghi', getline(1))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200549 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200550 let &cpo = save_cpo
551endfunc
552
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200553" TODO: Add a test for the 'v' flag in 'cpo'.
554" Backspaced characters remain visible on the screen in Insert mode.
Bram Moolenaardf7df592020-06-14 13:50:55 +0200555
556" Test for the 'w' flag in 'cpo' ('cw' on a blank character changes only one
557" character)
558func Test_cpo_w()
559 let save_cpo = &cpo
560 new
561 set cpo+=w
562 call setline(1, 'here are some words')
563 norm! 1gg0elcwZZZ
564 call assert_equal('hereZZZ are some words', getline('.'))
565 norm! 1gg2elcWYYY
566 call assert_equal('hereZZZ areYYY some words', getline('.'))
567 set cpo-=w
568 call setline(1, 'here are some words')
569 norm! 1gg0elcwZZZ
570 call assert_equal('hereZZZare some words', getline('.'))
571 norm! 1gg2elcWYYY
572 call assert_equal('hereZZZare someYYYwords', getline('.'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200573 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200574 let &cpo = save_cpo
575endfunc
576
577" Test for the 'W' flag in 'cpo' is in the test_writefile.vim file
578
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200579" Test for the 'x' flag in 'cpo' (Esc on command-line executes command)
580func Test_cpo_x()
581 let save_cpo = &cpo
582 set cpo-=x
583 let i = 1
584 call feedkeys(":let i=10\<Esc>", 'xt')
585 call assert_equal(1, i)
586 set cpo+=x
587 call feedkeys(":let i=10\<Esc>", 'xt')
588 call assert_equal(10, i)
589 let &cpo = save_cpo
590endfunc
591
592" Test for the 'X' flag in 'cpo' ('R' with a count)
593func Test_cpo_X()
594 let save_cpo = &cpo
595 new
596 call setline(1, 'aaaaaa')
597 set cpo-=X
598 normal gg4Rx
599 call assert_equal('xxxxaa', getline(1))
600 normal ggRy
601 normal 4.
602 call assert_equal('yyyyaa', getline(1))
603 call setline(1, 'aaaaaa')
604 set cpo+=X
605 normal gg4Rx
606 call assert_equal('xxxxaaaaa', getline(1))
607 normal ggRy
608 normal 4.
609 call assert_equal('yyyyxxxaaaaa', getline(1))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200610 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200611 let &cpo = save_cpo
612endfunc
613
614" Test for the 'y' flag in 'cpo' (repeating a yank command)
615func Test_cpo_y()
616 let save_cpo = &cpo
617 new
618 call setline(1, ['one', 'two'])
619 set cpo-=y
620 normal ggyy
621 normal 2G.
622 call assert_equal("one\n", @")
623 %d
624 call setline(1, ['one', 'two'])
625 set cpo+=y
626 normal ggyy
627 normal 2G.
628 call assert_equal("two\n", @")
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200629 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200630 let &cpo = save_cpo
631endfunc
632
633" Test for the 'Z' flag in 'cpo' (write! resets 'readonly')
634func Test_cpo_Z()
635 let save_cpo = &cpo
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100636 call writefile([], 'XfileCpoZ', 'D')
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100637 new XfileCpoZ
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200638 setlocal readonly
639 set cpo-=Z
640 write!
641 call assert_equal(0, &readonly)
642 set cpo+=Z
643 setlocal readonly
644 write!
645 call assert_equal(1, &readonly)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200646 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200647 let &cpo = save_cpo
648endfunc
649
Bram Moolenaardf7df592020-06-14 13:50:55 +0200650" Test for the '!' flag in 'cpo' is in the test_normal.vim file
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200651
652" Test for displaying dollar when changing text ('$' flag in 'cpoptions')
653func Test_cpo_dollar()
654 new
655 let g:Line = ''
656 func SaveFirstLine()
657 let g:Line = Screenline(1)
658 return ''
659 endfunc
660 inoremap <expr> <buffer> <F2> SaveFirstLine()
661 call test_override('redraw_flag', 1)
662 set cpo+=$
663 call setline(1, 'one two three')
664 redraw!
665 exe "normal c2w\<F2>vim"
666 call assert_equal('one tw$ three', g:Line)
667 call assert_equal('vim three', getline(1))
668 set cpo-=$
669 call test_override('ALL', 0)
670 delfunc SaveFirstLine
671 %bw!
672endfunc
673
Bram Moolenaardf7df592020-06-14 13:50:55 +0200674" Test for the '%' flag in 'cpo' (parenthesis matching inside strings)
675func Test_cpo_percent()
676 let save_cpo = &cpo
677 new
678 call setline(1, ' if (strcmp("ab)cd(", s))')
679 set cpo-=%
680 normal 8|%
681 call assert_equal(28, col('.'))
682 normal 15|%
683 call assert_equal(27, col('.'))
684 normal 27|%
685 call assert_equal(15, col('.'))
686 call assert_beeps("normal 19|%")
687 call assert_beeps("normal 22|%")
688 set cpo+=%
689 normal 8|%
690 call assert_equal(28, col('.'))
691 normal 15|%
692 call assert_equal(19, col('.'))
693 normal 27|%
694 call assert_equal(22, col('.'))
695 normal 19|%
696 call assert_equal(15, col('.'))
697 normal 22|%
698 call assert_equal(27, col('.'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200699 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200700 let &cpo = save_cpo
701endfunc
702
703" Test for cursor movement with '-' in 'cpoptions'
704func Test_cpo_minus()
705 new
706 call setline(1, ['foo', 'bar', 'baz'])
707 let save_cpo = &cpo
708 set cpo+=-
709 call assert_beeps('normal 10j')
710 call assert_equal(1, line('.'))
711 normal G
712 call assert_beeps('normal 10k')
713 call assert_equal(3, line('.'))
714 call assert_fails(10, 'E16:')
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200715 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200716 let &cpo = save_cpo
717endfunc
718
719" Test for the '+' flag in 'cpo' ('write file' command resets the 'modified'
720" flag)
721func Test_cpo_plus()
722 let save_cpo = &cpo
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100723 call writefile([], 'XfileCpoPlus', 'D')
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100724 new XfileCpoPlus
Bram Moolenaardf7df592020-06-14 13:50:55 +0200725 call setline(1, 'foo')
726 write X1
727 call assert_equal(1, &modified)
728 set cpo+=+
729 write X2
730 call assert_equal(0, &modified)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200731 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200732 call delete('X1')
733 call delete('X2')
734 let &cpo = save_cpo
735endfunc
736
737" Test for the '*' flag in 'cpo' (':*' is same as ':@')
738func Test_cpo_star()
739 let save_cpo = &cpo
740 let x = 0
741 new
742 set cpo-=*
743 let @a = 'let x += 1'
744 call assert_fails('*a', 'E20:')
745 set cpo+=*
746 *a
747 call assert_equal(1, x)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200748 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200749 let &cpo = save_cpo
750endfunc
751
752" Test for the '<' flag in 'cpo' is in the test_mapping.vim file
753
754" Test for the '>' flag in 'cpo' (use a new line when appending to a register)
755func Test_cpo_gt()
756 let save_cpo = &cpo
757 new
758 call setline(1, 'one two')
759 set cpo-=>
760 let @r = ''
761 normal gg"Rye
762 normal "Rye
763 call assert_equal("oneone", @r)
764 set cpo+=>
765 let @r = ''
766 normal gg"Rye
767 normal "Rye
768 call assert_equal("\none\none", @r)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200769 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200770 let &cpo = save_cpo
771endfunc
772
773" Test for the ';' flag in 'cpo'
774" Test for t,f,F,T movement commands and 'cpo-;' setting
775func Test_cpo_semicolon()
776 let save_cpo = &cpo
777 new
778 call append(0, ["aaa two three four", " zzz", "yyy ",
779 \ "bbb yee yoo four", "ccc two three four",
780 \ "ddd yee yoo four"])
781 set cpo-=;
782 1
783 normal! 0tt;D
784 2
785 normal! 0fz;D
786 3
787 normal! $Fy;D
788 4
789 normal! $Ty;D
790 set cpo+=;
791 5
792 normal! 0tt;;D
793 6
794 normal! $Ty;;D
795
796 call assert_equal('aaa two', getline(1))
797 call assert_equal(' z', getline(2))
798 call assert_equal('y', getline(3))
799 call assert_equal('bbb y', getline(4))
800 call assert_equal('ccc', getline(5))
801 call assert_equal('ddd yee y', getline(6))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200802 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200803 let &cpo = save_cpo
804endfunc
805
806" Test for the '#' flag in 'cpo' (count before 'D', 'o' and 'O' operators)
807func Test_cpo_hash()
808 let save_cpo = &cpo
809 new
810 set cpo-=#
811 call setline(1, ['one', 'two', 'three'])
812 normal gg2D
813 call assert_equal(['three'], getline(1, '$'))
814 normal gg2ofour
815 call assert_equal(['three', 'four', 'four'], getline(1, '$'))
816 normal gg2Otwo
817 call assert_equal(['two', 'two', 'three', 'four', 'four'], getline(1, '$'))
818 %d
819 set cpo+=#
820 call setline(1, ['one', 'two', 'three'])
821 normal gg2D
822 call assert_equal(['', 'two', 'three'], getline(1, '$'))
823 normal gg2oone
824 call assert_equal(['', 'one', 'two', 'three'], getline(1, '$'))
825 normal gg2Ozero
826 call assert_equal(['zero', '', 'one', 'two', 'three'], getline(1, '$'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200827 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200828 let &cpo = save_cpo
829endfunc
830
831" Test for the '&' flag in 'cpo'. The swap file is kept when a buffer is still
832" loaded and ':preserve' is used.
833func Test_cpo_ampersand()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100834 call writefile(['one'], 'XfileCpoAmp', 'D')
Bram Moolenaardf7df592020-06-14 13:50:55 +0200835 let after =<< trim [CODE]
836 set cpo+=&
837 preserve
838 qall
839 [CODE]
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100840 if RunVim([], after, 'XfileCpoAmp')
841 call assert_equal(1, filereadable('.XfileCpoAmp.swp'))
842 call delete('.XfileCpoAmp.swp')
Bram Moolenaardf7df592020-06-14 13:50:55 +0200843 endif
Bram Moolenaardf7df592020-06-14 13:50:55 +0200844endfunc
845
846" Test for the '\' flag in 'cpo' (backslash in a [] range in a search pattern)
847func Test_cpo_backslash()
848 let save_cpo = &cpo
849 new
850 call setline(1, ['', " \\-string"])
851 set cpo-=\
852 exe 'normal gg/[ \-]' .. "\<CR>n"
853 call assert_equal(3, col('.'))
854 set cpo+=\
855 exe 'normal gg/[ \-]' .. "\<CR>n"
856 call assert_equal(2, col('.'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200857 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200858 let &cpo = save_cpo
859endfunc
860
861" Test for the '/' flag in 'cpo' is in the test_substitute.vim file
862
863" Test for the '{' flag in 'cpo' (the "{" and "}" commands stop at a {
864" character at the start of a line)
865func Test_cpo_brace()
866 let save_cpo = &cpo
867 new
868 call setline(1, ['', '{', ' int i;', '}', ''])
869 set cpo-={
870 normal gg}
871 call assert_equal(5, line('.'))
872 normal G{
873 call assert_equal(1, line('.'))
874 set cpo+={
875 normal gg}
876 call assert_equal(2, line('.'))
877 normal G{
878 call assert_equal(2, line('.'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200879 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200880 let &cpo = save_cpo
881endfunc
882
883" Test for the '.' flag in 'cpo' (:cd command fails if the current buffer is
884" modified)
885func Test_cpo_dot()
886 let save_cpo = &cpo
887 new Xfoo
888 call setline(1, 'foo')
889 let save_dir = getcwd()
890 set cpo+=.
891
892 " :cd should fail when buffer is modified and 'cpo' contains dot.
893 call assert_fails('cd ..', 'E747:')
894 call assert_equal(save_dir, getcwd())
895
896 " :cd with exclamation mark should succeed.
897 cd! ..
898 call assert_notequal(save_dir, getcwd())
899
900 " :cd should succeed when buffer has been written.
901 w!
902 exe 'cd ' .. fnameescape(save_dir)
903 call assert_equal(save_dir, getcwd())
904
905 call delete('Xfoo')
906 set cpo&
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200907 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200908 let &cpo = save_cpo
909endfunc
910
Christian Brabandt22105fd2024-07-15 20:51:11 +0200911" Test for the 'z' flag in 'cpo' (make cw and dw work similar and avoid
912" inconsistencies, see :h cpo-z)
913func Test_cpo_z()
914 let save_cpo = &cpo
915 new
916 " Test 1: dw behaves differently from cw
917 call setline(1, ['foo bar baz', 'one two three'])
918 call cursor(1, 1)
919 " dw does not delete the whitespace after the word
920 norm! wcwanother
921 set cpo-=z
922 " dw deletes the whitespace after the word
923 call cursor(2, 1)
924 norm! wcwfour
925 call assert_equal(['foo another baz', 'one fourthree'], getline(1, '$'))
926 " Test 2: d{motion} becomes linewise :h d-special
927 %d
928 call setline(1, ['one ', ' bar', ' e ', 'zwei'])
929 call cursor(2, 1)
930 set cpo+=z
931 " delete operation becomes linewise
932 call feedkeys("fbd/e\\zs\<cr>", 'tnx')
933 call assert_equal(['one ', 'zwei'], getline(1, '$'))
934 %d
935 call setline(1, ['one ', ' bar', ' e ', 'zwei'])
936 call cursor(2, 1)
937 call feedkeys("fbd2w", 'tnx')
938 call assert_equal(['one ', 'zwei'], getline(1, '$'))
939
940 " delete operation does not become line wise
941 set cpo-=z
942 call setline(1, ['one ', ' bar', ' e ', 'zwei'])
943 call cursor(2, 1)
944 call feedkeys("fbd/e\\zs\<cr>", 'tnx')
945 call assert_equal(['one ', ' ', 'zwei'], getline(1, '$')) " codestyle: ignore
946 %d
947 call setline(1, ['one ', ' bar', ' e ', 'zwei'])
948 call cursor(2, 1)
949 call feedkeys("fbd2w", 'tnx')
950 call assert_equal(['one ', ' ', 'zwei'], getline(1, '$'))
951
952 " clean up
953 bw!
954 let &cpo = save_cpo
955endfunc
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200956" vim: shiftwidth=2 sts=2 expandtab