blob: 1f7d4afa097ecc2f920058e70a917e4bb7a10716 [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
3source check.vim
Bram Moolenaardf7df592020-06-14 13:50:55 +02004source shared.vim
Bram Moolenaarc9630d22020-06-13 13:20:48 +02005source view_util.vim
6
7" Test for the 'a' flag in 'cpo'. Reading a file should set the alternate
8" file name.
9func Test_cpo_a()
10 let save_cpo = &cpo
Bram Moolenaar45bbaef2022-09-08 16:39:22 +010011 call writefile(['one'], 'XfileCpoA', 'D')
Bram Moolenaarc9630d22020-06-13 13:20:48 +020012 " Wipe out all the buffers, so that the alternate file is empty
13 edit Xfoo | %bw
14 set cpo-=a
15 new
Bram Moolenaara85e4db2022-08-28 17:44:20 +010016 read XfileCpoA
Bram Moolenaarc9630d22020-06-13 13:20:48 +020017 call assert_equal('', @#)
18 %d
19 set cpo+=a
Bram Moolenaara85e4db2022-08-28 17:44:20 +010020 read XfileCpoA
21 call assert_equal('XfileCpoA', @#)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +020022 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +020023 let &cpo = save_cpo
24endfunc
25
26" Test for the 'A' flag in 'cpo'. Writing a file should set the alternate
27" file name.
28func Test_cpo_A()
29 let save_cpo = &cpo
30 " Wipe out all the buffers, so that the alternate file is empty
31 edit Xfoo | %bw
32 set cpo-=A
Bram Moolenaar61abe7d2022-08-30 21:46:08 +010033 new XcpoAfile1
34 write XcpoAfile2
Bram Moolenaarc9630d22020-06-13 13:20:48 +020035 call assert_equal('', @#)
36 %bw
Bram Moolenaar61abe7d2022-08-30 21:46:08 +010037 call delete('XcpoAfile2')
38 new XcpoAfile1
Bram Moolenaarc9630d22020-06-13 13:20:48 +020039 set cpo+=A
Bram Moolenaar61abe7d2022-08-30 21:46:08 +010040 write XcpoAfile2
41 call assert_equal('XcpoAfile2', @#)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +020042 bw!
Bram Moolenaar61abe7d2022-08-30 21:46:08 +010043 call delete('XcpoAfile2')
Bram Moolenaarc9630d22020-06-13 13:20:48 +020044 let &cpo = save_cpo
45endfunc
46
47" Test for the 'b' flag in 'cpo'. "\|" at the end of a map command is
48" recognized as the end of the map.
49func Test_cpo_b()
50 let save_cpo = &cpo
51 set cpo+=b
52 nnoremap <F5> :pwd\<CR>\|let i = 1
53 call assert_equal(':pwd\<CR>\', maparg('<F5>'))
54 nunmap <F5>
55 exe "nnoremap <F5> :pwd\<C-V>|let i = 1"
56 call assert_equal(':pwd|let i = 1', maparg('<F5>'))
57 nunmap <F5>
58 set cpo-=b
59 nnoremap <F5> :pwd\<CR>\|let i = 1
60 call assert_equal(':pwd\<CR>|let i = 1', maparg('<F5>'))
61 let &cpo = save_cpo
62 nunmap <F5>
63endfunc
64
Bram Moolenaardf7df592020-06-14 13:50:55 +020065" Test for the 'B' flag in 'cpo'. A backslash in mappings, abbreviations, user
66" commands and menu commands has no special meaning.
67func Test_cpo_B()
68 let save_cpo = &cpo
69 new
zeertzjqc3a26c62023-02-17 16:40:20 +000070 imap <buffer> x<Bslash>k Test
Bram Moolenaardf7df592020-06-14 13:50:55 +020071 set cpo-=B
72 iabbr <buffer> abc ab\<BS>d
73 exe "normal iabc "
74 call assert_equal('ab<BS>d ', getline(1))
zeertzjqc3a26c62023-02-17 16:40:20 +000075 call feedkeys(":imap <buffer> x\<C-A>\<C-B>\"\<CR>", 'tx')
76 call assert_equal('"imap <buffer> x\\k', @:)
Bram Moolenaardf7df592020-06-14 13:50:55 +020077 %d
78 set cpo+=B
79 iabbr <buffer> abc ab\<BS>d
80 exe "normal iabc "
81 call assert_equal('abd ', getline(1))
zeertzjqc3a26c62023-02-17 16:40:20 +000082 call feedkeys(":imap <buffer> x\<C-A>\<C-B>\"\<CR>", 'tx')
83 call assert_equal('"imap <buffer> x\k', @:)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +020084 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +020085 let &cpo = save_cpo
86endfunc
87
Bram Moolenaarc9630d22020-06-13 13:20:48 +020088" Test for the 'c' flag in 'cpo'.
89func Test_cpo_c()
90 let save_cpo = &cpo
91 set cpo+=c
92 new
93 call setline(1, ' abababababab')
94 exe "normal gg/abab\<CR>"
95 call assert_equal(3, searchcount().total)
96 set cpo-=c
97 exe "normal gg/abab\<CR>"
98 call assert_equal(5, searchcount().total)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +020099 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200100 let &cpo = save_cpo
101endfunc
102
103" Test for the 'C' flag in 'cpo' (line continuation)
104func Test_cpo_C()
105 let save_cpo = &cpo
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100106 call writefile(['let l = [', '\ 1,', '\ 2]'], 'XfileCpoC', 'D')
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200107 set cpo-=C
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100108 source XfileCpoC
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200109 call assert_equal([1, 2], g:l)
110 set cpo+=C
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100111 call assert_fails('source XfileCpoC', ['E697:', 'E10:'])
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200112 let &cpo = save_cpo
113endfunc
114
115" Test for the 'd' flag in 'cpo' (tags relative to the current file)
116func Test_cpo_d()
117 let save_cpo = &cpo
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100118 call mkdir('XdirCpoD', 'R')
119 call writefile(["one\tXfile1\t/^one$/"], 'tags', 'D')
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100120 call writefile(["two\tXfile2\t/^two$/"], 'XdirCpoD/tags')
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200121 set tags=./tags
122 set cpo-=d
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100123 edit XdirCpoD/Xfile
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200124 call assert_equal('two', taglist('.*')[0].name)
125 set cpo+=d
126 call assert_equal('one', taglist('.*')[0].name)
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100127
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200128 %bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200129 set tags&
130 let &cpo = save_cpo
131endfunc
132
133" Test for the 'D' flag in 'cpo' (digraph after a r, f or t)
134func Test_cpo_D()
135 CheckFeature digraphs
136 let save_cpo = &cpo
137 new
138 set cpo-=D
139 call setline(1, 'abcdefgh|')
140 exe "norm! 1gg0f\<c-k>!!"
141 call assert_equal(9, col('.'))
142 set cpo+=D
143 exe "norm! 1gg0f\<c-k>!!"
144 call assert_equal(1, col('.'))
145 set cpo-=D
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200146 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200147 let &cpo = save_cpo
148endfunc
149
150" Test for the 'e' flag in 'cpo'
151func Test_cpo_e()
152 let save_cpo = &cpo
153 let @a='let i = 45'
154 set cpo+=e
155 call feedkeys(":@a\<CR>", 'xt')
156 call assert_equal(45, i)
157 set cpo-=e
158 call feedkeys(":@a\<CR>6\<CR>", 'xt')
159 call assert_equal(456, i)
160 let &cpo = save_cpo
161endfunc
162
163" Test for the 'E' flag in 'cpo' with yank, change, delete, etc. operators
164func Test_cpo_E()
165 new
166 call setline(1, '')
167 set cpo+=E
168 " yank an empty line
169 call assert_beeps('normal "ayl')
170 " change an empty line
171 call assert_beeps('normal lcTa')
Bram Moolenaar3e72dca2021-05-29 16:30:12 +0200172 call assert_beeps('normal 0c0')
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200173 " delete an empty line
174 call assert_beeps('normal D')
175 call assert_beeps('normal dl')
176 call assert_equal('', getline(1))
177 " change case of an empty line
178 call assert_beeps('normal gul')
179 call assert_beeps('normal gUl')
180 " replace a character
181 call assert_beeps('normal vrx')
182 " increment and decrement
183 call assert_beeps('exe "normal v\<C-A>"')
184 call assert_beeps('exe "normal v\<C-X>"')
185 set cpo-=E
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200186 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200187endfunc
188
189" Test for the 'f' flag in 'cpo' (read in an empty buffer sets the file name)
190func Test_cpo_f()
191 let save_cpo = &cpo
192 new
193 set cpo-=f
194 read test_cpoptions.vim
195 call assert_equal('', @%)
196 %d
197 set cpo+=f
198 read test_cpoptions.vim
199 call assert_equal('test_cpoptions.vim', @%)
zeertzjqc3a26c62023-02-17 16:40:20 +0000200
201 bwipe!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200202 let &cpo = save_cpo
203endfunc
204
205" Test for the 'F' flag in 'cpo' (write in an empty buffer sets the file name)
206func Test_cpo_F()
207 let save_cpo = &cpo
208 new
209 set cpo-=F
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100210 write XfileCpoF
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200211 call assert_equal('', @%)
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100212 call delete('XfileCpoF')
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200213 set cpo+=F
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100214 write XfileCpoF
215 call assert_equal('XfileCpoF', @%)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200216 bw!
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100217 call delete('XfileCpoF')
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200218 let &cpo = save_cpo
219endfunc
220
221" Test for the 'g' flag in 'cpo' (jump to line 1 when re-editing a file)
222func Test_cpo_g()
223 let save_cpo = &cpo
224 new test_cpoptions.vim
225 set cpo-=g
226 normal 20G
227 edit
228 call assert_equal(20, line('.'))
229 set cpo+=g
230 edit
231 call assert_equal(1, line('.'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200232 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200233 let &cpo = save_cpo
234endfunc
235
236" Test for inserting text in a line with only spaces ('H' flag in 'cpoptions')
237func Test_cpo_H()
238 let save_cpo = &cpo
239 new
240 set cpo-=H
241 call setline(1, ' ')
242 normal! Ia
243 call assert_equal(' a', getline(1))
244 set cpo+=H
245 call setline(1, ' ')
246 normal! Ia
247 call assert_equal(' a ', getline(1))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200248 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200249 let &cpo = save_cpo
250endfunc
251
Bram Moolenaardf7df592020-06-14 13:50:55 +0200252" TODO: Add a test for the 'i' flag in 'cpo'
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200253" Interrupting the reading of a file will leave it modified.
Bram Moolenaardf7df592020-06-14 13:50:55 +0200254
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200255" Test for the 'I' flag in 'cpo' (deleting autoindent when using arrow keys)
256func Test_cpo_I()
257 let save_cpo = &cpo
258 new
259 setlocal autoindent
260 set cpo+=I
261 exe "normal i one\<CR>\<Up>"
262 call assert_equal(' ', getline(2))
263 set cpo-=I
264 %d
265 exe "normal i one\<CR>\<Up>"
266 call assert_equal('', getline(2))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200267 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200268 let &cpo = save_cpo
269endfunc
270
Bram Moolenaardf7df592020-06-14 13:50:55 +0200271" Test for the 'j' flag in 'cpo' is in the test_join.vim file.
272
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200273" Test for the 'J' flag in 'cpo' (two spaces after a sentence)
274func Test_cpo_J()
275 let save_cpo = &cpo
276 new
277 set cpo-=J
278 call setline(1, 'one. two! three? four."'' five.)]')
279 normal 0
280 for colnr in [6, 12, 19, 28, 34]
281 normal )
282 call assert_equal(colnr, col('.'))
283 endfor
284 for colnr in [28, 19, 12, 6, 1]
285 normal (
286 call assert_equal(colnr, col('.'))
287 endfor
288 set cpo+=J
289 normal 0
290 for colnr in [12, 28, 34]
291 normal )
292 call assert_equal(colnr, col('.'))
293 endfor
294 for colnr in [28, 12, 1]
295 normal (
296 call assert_equal(colnr, col('.'))
297 endfor
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200298 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200299 let &cpo = save_cpo
300endfunc
301
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200302" TODO: Add a test for the 'k' flag in 'cpo'.
303" Disable the recognition of raw key codes in mappings, abbreviations, and the
304" "to" part of menu commands.
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200305
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200306" TODO: Add a test for the 'K' flag in 'cpo'.
307" Don't wait for a key code to complete when it is halfway a mapping.
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200308
309" Test for the 'l' flag in 'cpo' (backslash in a [] range)
310func Test_cpo_l()
311 let save_cpo = &cpo
312 new
313 call setline(1, ['', "a\tc" .. '\t'])
314 set cpo-=l
315 exe 'normal gg/[\t]' .. "\<CR>"
316 call assert_equal([2, 8], [col('.'), virtcol('.')])
317 set cpo+=l
318 exe 'normal gg/[\t]' .. "\<CR>"
319 call assert_equal([4, 10], [col('.'), virtcol('.')])
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200320 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200321 let &cpo = save_cpo
322endfunc
323
324" Test for inserting tab in virtual replace mode ('L' flag in 'cpoptions')
325func Test_cpo_L()
326 let save_cpo = &cpo
327 new
328 set cpo-=L
329 call setline(1, 'abcdefghijklmnopqr')
330 exe "normal 0gR\<Tab>"
331 call assert_equal("\<Tab>ijklmnopqr", getline(1))
332 set cpo+=L
333 set list
334 call setline(1, 'abcdefghijklmnopqr')
335 exe "normal 0gR\<Tab>"
336 call assert_equal("\<Tab>cdefghijklmnopqr", getline(1))
337 set nolist
338 call setline(1, 'abcdefghijklmnopqr')
339 exe "normal 0gR\<Tab>"
340 call assert_equal("\<Tab>ijklmnopqr", getline(1))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200341 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200342 let &cpo = save_cpo
343endfunc
344
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200345" TODO: Add a test for the 'm' flag in 'cpo'.
346" When included, a showmatch will always wait half a second. When not
347" included, a showmatch will wait half a second or until a character is typed.
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200348
349" Test for the 'M' flag in 'cpo' (% with escape parenthesis)
350func Test_cpo_M()
351 let save_cpo = &cpo
352 new
353 call setline(1, ['( \( )', '\( ( \)'])
354
355 set cpo-=M
356 call cursor(1, 1)
357 normal %
358 call assert_equal(6, col('.'))
359 call cursor(1, 4)
360 call assert_beeps('normal %')
361 call cursor(2, 2)
362 normal %
363 call assert_equal(7, col('.'))
364 call cursor(2, 4)
365 call assert_beeps('normal %')
366
367 set cpo+=M
368 call cursor(1, 4)
369 normal %
370 call assert_equal(6, col('.'))
371 call cursor(1, 1)
372 call assert_beeps('normal %')
373 call cursor(2, 4)
374 normal %
375 call assert_equal(7, col('.'))
376 call cursor(2, 1)
377 call assert_beeps('normal %')
378
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200379 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200380 let &cpo = save_cpo
381endfunc
382
383" Test for the 'n' flag in 'cpo' (using number column for wrapped lines)
384func Test_cpo_n()
385 let save_cpo = &cpo
386 new
387 call setline(1, repeat('a', &columns))
388 setlocal number
389 set cpo-=n
390 redraw!
391 call assert_equal(' aaaa', Screenline(2))
392 set cpo+=n
393 redraw!
394 call assert_equal('aaaa', Screenline(2))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200395 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200396 let &cpo = save_cpo
397endfunc
398
399" Test for the 'o' flag in 'cpo' (line offset to search command)
400func Test_cpo_o()
401 let save_cpo = &cpo
402 new
403 call setline(1, ['', 'one', 'two', 'three', 'one', 'two', 'three'])
404 set cpo-=o
405 exe "normal /one/+2\<CR>"
406 normal n
407 call assert_equal(7, line('.'))
408 set cpo+=o
409 exe "normal /one/+2\<CR>"
410 normal n
411 call assert_equal(5, line('.'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200412 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200413 let &cpo = save_cpo
414endfunc
415
416" Test for the 'O' flag in 'cpo' (overwriting an existing file)
417func Test_cpo_O()
418 let save_cpo = &cpo
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100419 new XfileCpoO
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200420 call setline(1, 'one')
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100421 call writefile(['two'], 'XfileCpoO', 'D')
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200422 set cpo-=O
423 call assert_fails('write', 'E13:')
424 set cpo+=O
425 write
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100426 call assert_equal(['one'], readfile('XfileCpoO'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200427 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200428 let &cpo = save_cpo
429endfunc
430
Bram Moolenaard26c5802022-10-13 12:30:08 +0100431" Test for the 'p' flag in 'cpo' is in the test_lispindent.vim file.
Bram Moolenaardf7df592020-06-14 13:50:55 +0200432
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200433" Test for the 'P' flag in 'cpo' (appending to a file sets the current file
434" name)
435func Test_cpo_P()
436 let save_cpo = &cpo
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100437 call writefile([], 'XfileCpoP', 'D')
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200438 new
439 call setline(1, 'one')
440 set cpo+=F
441 set cpo-=P
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100442 write >> XfileCpoP
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200443 call assert_equal('', @%)
444 set cpo+=P
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100445 write >> XfileCpoP
446 call assert_equal('XfileCpoP', @%)
zeertzjqc3a26c62023-02-17 16:40:20 +0000447
448 bwipe!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200449 let &cpo = save_cpo
450endfunc
451
452" Test for the 'q' flag in 'cpo' (joining multiple lines)
453func Test_cpo_q()
454 let save_cpo = &cpo
455 new
456 call setline(1, ['one', 'two', 'three', 'four', 'five'])
457 set cpo-=q
458 normal gg4J
459 call assert_equal(14, col('.'))
460 %d
461 call setline(1, ['one', 'two', 'three', 'four', 'five'])
462 set cpo+=q
463 normal gg4J
464 call assert_equal(4, col('.'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200465 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200466 let &cpo = save_cpo
467endfunc
468
469" Test for the 'r' flag in 'cpo' (redo command with a search motion)
470func Test_cpo_r()
471 let save_cpo = &cpo
472 new
473 call setline(1, repeat(['one two three four'], 2))
474 set cpo-=r
475 exe "normal ggc/two\<CR>abc "
476 let @/ = 'three'
477 normal 2G.
478 call assert_equal('abc two three four', getline(2))
479 %d
480 call setline(1, repeat(['one two three four'], 2))
481 set cpo+=r
482 exe "normal ggc/two\<CR>abc "
483 let @/ = 'three'
484 normal 2G.
485 call assert_equal('abc three four', getline(2))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200486 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200487 let &cpo = save_cpo
488endfunc
489
490" Test for the 'R' flag in 'cpo' (clear marks after a filter command)
491func Test_cpo_R()
492 CheckUnix
493 let save_cpo = &cpo
494 new
495 call setline(1, ['three', 'one', 'two'])
496 set cpo-=R
497 3mark r
498 %!sort
499 call assert_equal(3, line("'r"))
500 %d
501 call setline(1, ['three', 'one', 'two'])
502 set cpo+=R
503 3mark r
504 %!sort
505 call assert_equal(0, line("'r"))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200506 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200507 let &cpo = save_cpo
508endfunc
509
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200510" TODO: Add a test for the 's' flag in 'cpo'.
511" Set buffer options when entering the buffer for the first time. If not
512" present the options are set when the buffer is created.
Bram Moolenaardf7df592020-06-14 13:50:55 +0200513
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200514" Test for the 'S' flag in 'cpo' (copying buffer options)
515func Test_cpo_S()
516 let save_cpo = &cpo
517 new Xfile1
518 set noautoindent
519 new Xfile2
520 set cpo-=S
521 set autoindent
522 wincmd p
523 call assert_equal(0, &autoindent)
524 wincmd p
525 call assert_equal(1, &autoindent)
526 set cpo+=S
527 wincmd p
528 call assert_equal(1, &autoindent)
529 set noautoindent
530 wincmd p
531 call assert_equal(0, &autoindent)
532 wincmd t
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200533 bw!
534 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200535 let &cpo = save_cpo
536endfunc
537
Bram Moolenaardf7df592020-06-14 13:50:55 +0200538" Test for the 't' flag in 'cpo' is in the test_tagjump.vim file.
539
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200540" Test for the 'u' flag in 'cpo' (Vi-compatible undo)
541func Test_cpo_u()
542 let save_cpo = &cpo
543 new
544 set cpo-=u
545 exe "normal iabc\<C-G>udef\<C-G>ughi"
546 normal uu
547 call assert_equal('abc', getline(1))
548 %d
549 set cpo+=u
550 exe "normal iabc\<C-G>udef\<C-G>ughi"
551 normal uu
552 call assert_equal('abcdefghi', getline(1))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200553 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200554 let &cpo = save_cpo
555endfunc
556
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200557" TODO: Add a test for the 'v' flag in 'cpo'.
558" Backspaced characters remain visible on the screen in Insert mode.
Bram Moolenaardf7df592020-06-14 13:50:55 +0200559
560" Test for the 'w' flag in 'cpo' ('cw' on a blank character changes only one
561" character)
562func Test_cpo_w()
563 let save_cpo = &cpo
564 new
565 set cpo+=w
566 call setline(1, 'here are some words')
567 norm! 1gg0elcwZZZ
568 call assert_equal('hereZZZ are some words', getline('.'))
569 norm! 1gg2elcWYYY
570 call assert_equal('hereZZZ areYYY some words', getline('.'))
571 set cpo-=w
572 call setline(1, 'here are some words')
573 norm! 1gg0elcwZZZ
574 call assert_equal('hereZZZare some words', getline('.'))
575 norm! 1gg2elcWYYY
576 call assert_equal('hereZZZare someYYYwords', getline('.'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200577 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200578 let &cpo = save_cpo
579endfunc
580
581" Test for the 'W' flag in 'cpo' is in the test_writefile.vim file
582
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200583" Test for the 'x' flag in 'cpo' (Esc on command-line executes command)
584func Test_cpo_x()
585 let save_cpo = &cpo
586 set cpo-=x
587 let i = 1
588 call feedkeys(":let i=10\<Esc>", 'xt')
589 call assert_equal(1, i)
590 set cpo+=x
591 call feedkeys(":let i=10\<Esc>", 'xt')
592 call assert_equal(10, i)
593 let &cpo = save_cpo
594endfunc
595
596" Test for the 'X' flag in 'cpo' ('R' with a count)
597func Test_cpo_X()
598 let save_cpo = &cpo
599 new
600 call setline(1, 'aaaaaa')
601 set cpo-=X
602 normal gg4Rx
603 call assert_equal('xxxxaa', getline(1))
604 normal ggRy
605 normal 4.
606 call assert_equal('yyyyaa', getline(1))
607 call setline(1, 'aaaaaa')
608 set cpo+=X
609 normal gg4Rx
610 call assert_equal('xxxxaaaaa', getline(1))
611 normal ggRy
612 normal 4.
613 call assert_equal('yyyyxxxaaaaa', getline(1))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200614 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200615 let &cpo = save_cpo
616endfunc
617
618" Test for the 'y' flag in 'cpo' (repeating a yank command)
619func Test_cpo_y()
620 let save_cpo = &cpo
621 new
622 call setline(1, ['one', 'two'])
623 set cpo-=y
624 normal ggyy
625 normal 2G.
626 call assert_equal("one\n", @")
627 %d
628 call setline(1, ['one', 'two'])
629 set cpo+=y
630 normal ggyy
631 normal 2G.
632 call assert_equal("two\n", @")
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200633 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200634 let &cpo = save_cpo
635endfunc
636
637" Test for the 'Z' flag in 'cpo' (write! resets 'readonly')
638func Test_cpo_Z()
639 let save_cpo = &cpo
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100640 call writefile([], 'XfileCpoZ', 'D')
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100641 new XfileCpoZ
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200642 setlocal readonly
643 set cpo-=Z
644 write!
645 call assert_equal(0, &readonly)
646 set cpo+=Z
647 setlocal readonly
648 write!
649 call assert_equal(1, &readonly)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200650 bw!
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200651 let &cpo = save_cpo
652endfunc
653
Bram Moolenaardf7df592020-06-14 13:50:55 +0200654" Test for the '!' flag in 'cpo' is in the test_normal.vim file
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200655
656" Test for displaying dollar when changing text ('$' flag in 'cpoptions')
657func Test_cpo_dollar()
658 new
659 let g:Line = ''
660 func SaveFirstLine()
661 let g:Line = Screenline(1)
662 return ''
663 endfunc
664 inoremap <expr> <buffer> <F2> SaveFirstLine()
665 call test_override('redraw_flag', 1)
666 set cpo+=$
667 call setline(1, 'one two three')
668 redraw!
669 exe "normal c2w\<F2>vim"
670 call assert_equal('one tw$ three', g:Line)
671 call assert_equal('vim three', getline(1))
672 set cpo-=$
673 call test_override('ALL', 0)
674 delfunc SaveFirstLine
675 %bw!
676endfunc
677
Bram Moolenaardf7df592020-06-14 13:50:55 +0200678" Test for the '%' flag in 'cpo' (parenthesis matching inside strings)
679func Test_cpo_percent()
680 let save_cpo = &cpo
681 new
682 call setline(1, ' if (strcmp("ab)cd(", s))')
683 set cpo-=%
684 normal 8|%
685 call assert_equal(28, col('.'))
686 normal 15|%
687 call assert_equal(27, col('.'))
688 normal 27|%
689 call assert_equal(15, col('.'))
690 call assert_beeps("normal 19|%")
691 call assert_beeps("normal 22|%")
692 set cpo+=%
693 normal 8|%
694 call assert_equal(28, col('.'))
695 normal 15|%
696 call assert_equal(19, col('.'))
697 normal 27|%
698 call assert_equal(22, col('.'))
699 normal 19|%
700 call assert_equal(15, col('.'))
701 normal 22|%
702 call assert_equal(27, col('.'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200703 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200704 let &cpo = save_cpo
705endfunc
706
707" Test for cursor movement with '-' in 'cpoptions'
708func Test_cpo_minus()
709 new
710 call setline(1, ['foo', 'bar', 'baz'])
711 let save_cpo = &cpo
712 set cpo+=-
713 call assert_beeps('normal 10j')
714 call assert_equal(1, line('.'))
715 normal G
716 call assert_beeps('normal 10k')
717 call assert_equal(3, line('.'))
718 call assert_fails(10, 'E16:')
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200719 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200720 let &cpo = save_cpo
721endfunc
722
723" Test for the '+' flag in 'cpo' ('write file' command resets the 'modified'
724" flag)
725func Test_cpo_plus()
726 let save_cpo = &cpo
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100727 call writefile([], 'XfileCpoPlus', 'D')
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100728 new XfileCpoPlus
Bram Moolenaardf7df592020-06-14 13:50:55 +0200729 call setline(1, 'foo')
730 write X1
731 call assert_equal(1, &modified)
732 set cpo+=+
733 write X2
734 call assert_equal(0, &modified)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200735 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200736 call delete('X1')
737 call delete('X2')
738 let &cpo = save_cpo
739endfunc
740
741" Test for the '*' flag in 'cpo' (':*' is same as ':@')
742func Test_cpo_star()
743 let save_cpo = &cpo
744 let x = 0
745 new
746 set cpo-=*
747 let @a = 'let x += 1'
748 call assert_fails('*a', 'E20:')
749 set cpo+=*
750 *a
751 call assert_equal(1, x)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200752 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200753 let &cpo = save_cpo
754endfunc
755
756" Test for the '<' flag in 'cpo' is in the test_mapping.vim file
757
758" Test for the '>' flag in 'cpo' (use a new line when appending to a register)
759func Test_cpo_gt()
760 let save_cpo = &cpo
761 new
762 call setline(1, 'one two')
763 set cpo-=>
764 let @r = ''
765 normal gg"Rye
766 normal "Rye
767 call assert_equal("oneone", @r)
768 set cpo+=>
769 let @r = ''
770 normal gg"Rye
771 normal "Rye
772 call assert_equal("\none\none", @r)
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200773 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200774 let &cpo = save_cpo
775endfunc
776
777" Test for the ';' flag in 'cpo'
778" Test for t,f,F,T movement commands and 'cpo-;' setting
779func Test_cpo_semicolon()
780 let save_cpo = &cpo
781 new
782 call append(0, ["aaa two three four", " zzz", "yyy ",
783 \ "bbb yee yoo four", "ccc two three four",
784 \ "ddd yee yoo four"])
785 set cpo-=;
786 1
787 normal! 0tt;D
788 2
789 normal! 0fz;D
790 3
791 normal! $Fy;D
792 4
793 normal! $Ty;D
794 set cpo+=;
795 5
796 normal! 0tt;;D
797 6
798 normal! $Ty;;D
799
800 call assert_equal('aaa two', getline(1))
801 call assert_equal(' z', getline(2))
802 call assert_equal('y', getline(3))
803 call assert_equal('bbb y', getline(4))
804 call assert_equal('ccc', getline(5))
805 call assert_equal('ddd yee y', getline(6))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200806 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200807 let &cpo = save_cpo
808endfunc
809
810" Test for the '#' flag in 'cpo' (count before 'D', 'o' and 'O' operators)
811func Test_cpo_hash()
812 let save_cpo = &cpo
813 new
814 set cpo-=#
815 call setline(1, ['one', 'two', 'three'])
816 normal gg2D
817 call assert_equal(['three'], getline(1, '$'))
818 normal gg2ofour
819 call assert_equal(['three', 'four', 'four'], getline(1, '$'))
820 normal gg2Otwo
821 call assert_equal(['two', 'two', 'three', 'four', 'four'], getline(1, '$'))
822 %d
823 set cpo+=#
824 call setline(1, ['one', 'two', 'three'])
825 normal gg2D
826 call assert_equal(['', 'two', 'three'], getline(1, '$'))
827 normal gg2oone
828 call assert_equal(['', 'one', 'two', 'three'], getline(1, '$'))
829 normal gg2Ozero
830 call assert_equal(['zero', '', 'one', 'two', 'three'], getline(1, '$'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200831 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200832 let &cpo = save_cpo
833endfunc
834
835" Test for the '&' flag in 'cpo'. The swap file is kept when a buffer is still
836" loaded and ':preserve' is used.
837func Test_cpo_ampersand()
Bram Moolenaar45bbaef2022-09-08 16:39:22 +0100838 call writefile(['one'], 'XfileCpoAmp', 'D')
Bram Moolenaardf7df592020-06-14 13:50:55 +0200839 let after =<< trim [CODE]
840 set cpo+=&
841 preserve
842 qall
843 [CODE]
Bram Moolenaara85e4db2022-08-28 17:44:20 +0100844 if RunVim([], after, 'XfileCpoAmp')
845 call assert_equal(1, filereadable('.XfileCpoAmp.swp'))
846 call delete('.XfileCpoAmp.swp')
Bram Moolenaardf7df592020-06-14 13:50:55 +0200847 endif
Bram Moolenaardf7df592020-06-14 13:50:55 +0200848endfunc
849
850" Test for the '\' flag in 'cpo' (backslash in a [] range in a search pattern)
851func Test_cpo_backslash()
852 let save_cpo = &cpo
853 new
854 call setline(1, ['', " \\-string"])
855 set cpo-=\
856 exe 'normal gg/[ \-]' .. "\<CR>n"
857 call assert_equal(3, col('.'))
858 set cpo+=\
859 exe 'normal gg/[ \-]' .. "\<CR>n"
860 call assert_equal(2, col('.'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200861 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200862 let &cpo = save_cpo
863endfunc
864
865" Test for the '/' flag in 'cpo' is in the test_substitute.vim file
866
867" Test for the '{' flag in 'cpo' (the "{" and "}" commands stop at a {
868" character at the start of a line)
869func Test_cpo_brace()
870 let save_cpo = &cpo
871 new
872 call setline(1, ['', '{', ' int i;', '}', ''])
873 set cpo-={
874 normal gg}
875 call assert_equal(5, line('.'))
876 normal G{
877 call assert_equal(1, line('.'))
878 set cpo+={
879 normal gg}
880 call assert_equal(2, line('.'))
881 normal G{
882 call assert_equal(2, line('.'))
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200883 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200884 let &cpo = save_cpo
885endfunc
886
887" Test for the '.' flag in 'cpo' (:cd command fails if the current buffer is
888" modified)
889func Test_cpo_dot()
890 let save_cpo = &cpo
891 new Xfoo
892 call setline(1, 'foo')
893 let save_dir = getcwd()
894 set cpo+=.
895
896 " :cd should fail when buffer is modified and 'cpo' contains dot.
897 call assert_fails('cd ..', 'E747:')
898 call assert_equal(save_dir, getcwd())
899
900 " :cd with exclamation mark should succeed.
901 cd! ..
902 call assert_notequal(save_dir, getcwd())
903
904 " :cd should succeed when buffer has been written.
905 w!
906 exe 'cd ' .. fnameescape(save_dir)
907 call assert_equal(save_dir, getcwd())
908
909 call delete('Xfoo')
910 set cpo&
Christian Brabandtbb5d27d2024-07-14 16:03:41 +0200911 bw!
Bram Moolenaardf7df592020-06-14 13:50:55 +0200912 let &cpo = save_cpo
913endfunc
914
Bram Moolenaarc9630d22020-06-13 13:20:48 +0200915" vim: shiftwidth=2 sts=2 expandtab