robin/laufey
git clone https://git.robinwobin.dev/robin/laufey
40740f49f8
master
1" Tests for the substitute (:s) command 2 3source shared.vim 4source check.vim 5source screendump.vim 6 7" NOTE: This needs to be the first test to be 8" run in the file, since it depends on 9" that the previous substitution atom 10" was not yet set. 11" 12" recursive call of :s and sub-replace special 13" (did cause heap-use-after free in < v9.0.2121) 14func Test_aaaa_substitute_expr_recursive_special () 15func R () 16" FIXME: leaving out the 'n' flag leaks memory, why? 17 %s /./\='.'/gn 18endfunc 19new Xfoobar_UAF 20put ='abcdef' 21let bufnr = bufnr ( '%' ) 22try 23silent ! : s /./~\=R()/0 24"call assert_fails(':s/./~\=R()/0', 'E939:') 25let @/= '.' 26 ~g 27catch /^Vim\%((\a\+)\)\=:E565:/ 28endtry 29delfunc R 30exe bufnr .. "bw!" 31endfunc 32 33func Test_multiline_subst () 34enew ! 35call append ( 0 , [ "1 aa" , 36 \"bb" , 37 \"cc" , 38 \"2 dd" , 39 \"ee" , 40 \"3 ef" , 41 \"gh" , 42 \"4 ij" , 43 \"5 a8" , 44 \"8b c9" , 45 \"9d" , 46 \"6 e7" , 47 \"77f" , 48 \"xxxxx" ]) 49 501 51" test if replacing a line break works with a back reference 52/ ^1 / , / ^2 / s /\n\(.\)/ \1/ 53" test if inserting a line break works with a back reference 54/ ^3 / , / ^4 / s /\(.\)$/\r\1/ 55" test if replacing a line break with another line break works 56/ ^5 / , / ^6 / s /\(\_d\{3}\)/x\1x/ 57call assert_equal ( '1 aa bb cc 2 dd ee' , getline ( 1 )) 58call assert_equal ( '3 e' , getline ( 2 )) 59call assert_equal ( 'f' , getline ( 3 )) 60call assert_equal ( 'g' , getline ( 4 )) 61call assert_equal ( 'h' , getline ( 5 )) 62call assert_equal ( '4 i' , getline ( 6 )) 63call assert_equal ( 'j' , getline ( 7 )) 64call assert_equal ( '5 ax8' , getline ( 8 )) 65call assert_equal ( '8xb cx9' , getline ( 9 )) 66call assert_equal ( '9xd' , getline ( 10 )) 67call assert_equal ( '6 ex7' , getline ( 11 )) 68call assert_equal ( '7x7f' , getline ( 12 )) 69call assert_equal ( 'xxxxx' , getline ( 13 )) 70enew ! 71endfunc 72 73func Test_substitute_variants () 74" Validate that all the 2-/3-letter variants which embed the flags into the 75" command name actually work. 76enew ! 77let ln = 'Testing string' 78let variants = [ 79 \{ 'cmd' : ':s/Test/test/c' , 'exp' : 'testing string' , 'prompt' : 'y' }, 80 \{ 'cmd' : ':s/foo/bar/ce' , 'exp' : ln }, 81 \{ 'cmd' : ':s/t/r/cg' , 'exp' : 'Tesring srring' , 'prompt' : 'a' }, 82 \{ 'cmd' : ':s/t/r/ci' , 'exp' : 'resting string' , 'prompt' : 'y' }, 83 \{ 'cmd' : ':s/t/r/cI' , 'exp' : 'Tesring string' , 'prompt' : 'y' }, 84 \{ 'cmd' : ':s/t/r/c' , 'exp' : 'Testing string' , 'prompt' : 'n' }, 85 \{ 'cmd' : ':s/t/r/cn' , 'exp' : ln }, 86 \{ 'cmd' : ':s/t/r/cp' , 'exp' : 'Tesring string' , 'prompt' : 'y' }, 87 \{ 'cmd' : ':s/t/r/cl' , 'exp' : 'Tesring string' , 'prompt' : 'y' }, 88 \{ 'cmd' : ':s/t/r/gc' , 'exp' : 'Tesring srring' , 'prompt' : 'a' }, 89 \{ 'cmd' : ':s/i/I/gc' , 'exp' : 'TestIng string' , 'prompt' : 'l' }, 90 \{ 'cmd' : ':s/foo/bar/ge' , 'exp' : ln }, 91 \{ 'cmd' : ':s/t/r/g' , 'exp' : 'Tesring srring' }, 92 \{ 'cmd' : ':s/t/r/gi' , 'exp' : 'resring srring' }, 93 \{ 'cmd' : ':s/t/r/gI' , 'exp' : 'Tesring srring' }, 94 \{ 'cmd' : ':s/t/r/gn' , 'exp' : ln }, 95 \{ 'cmd' : ':s/t/r/gp' , 'exp' : 'Tesring srring' }, 96 \{ 'cmd' : ':s/t/r/gl' , 'exp' : 'Tesring srring' }, 97 \{ 'cmd' : ':s//r/gr' , 'exp' : 'Testr strr' }, 98 \{ 'cmd' : ':s/t/r/ic' , 'exp' : 'resting string' , 'prompt' : 'y' }, 99 \{ 'cmd' : ':s/foo/bar/ie' , 'exp' : ln }, 100 \{ 'cmd' : ':s/t/r/i' , 'exp' : 'resting string' }, 101 \{ 'cmd' : ':s/t/r/iI' , 'exp' : 'Tesring string' }, 102 \{ 'cmd' : ':s/t/r/in' , 'exp' : ln }, 103 \{ 'cmd' : ':s/t/r/ip' , 'exp' : 'resting string' }, 104 \{ 'cmd' : ':s//r/ir' , 'exp' : 'Testr string' }, 105 \{ 'cmd' : ':s/t/r/Ic' , 'exp' : 'Tesring string' , 'prompt' : 'y' }, 106 \{ 'cmd' : ':s/foo/bar/Ie' , 'exp' : ln }, 107 \{ 'cmd' : ':s/t/r/Ig' , 'exp' : 'Tesring srring' }, 108 \{ 'cmd' : ':s/t/r/Ii' , 'exp' : 'resting string' }, 109 \{ 'cmd' : ':s/t/r/I' , 'exp' : 'Tesring string' }, 110 \{ 'cmd' : ':s/t/r/Ip' , 'exp' : 'Tesring string' }, 111 \{ 'cmd' : ':s/t/r/Il' , 'exp' : 'Tesring string' }, 112 \{ 'cmd' : ':s//r/Ir' , 'exp' : 'Testr string' }, 113 \{ 'cmd' : ':s//r/rc' , 'exp' : 'Testr string' , 'prompt' : 'y' }, 114 \{ 'cmd' : ':s//r/rg' , 'exp' : 'Testr strr' }, 115 \{ 'cmd' : ':s//r/ri' , 'exp' : 'Testr string' }, 116 \{ 'cmd' : ':s//r/rI' , 'exp' : 'Testr string' }, 117 \{ 'cmd' : ':s//r/rn' , 'exp' : 'Testing string' }, 118 \{ 'cmd' : ':s//r/rp' , 'exp' : 'Testr string' }, 119 \{ 'cmd' : ':s//r/rl' , 'exp' : 'Testr string' }, 120 \{ 'cmd' : ':s//r/r' , 'exp' : 'Testr string' }, 121 \{ 'cmd' : ':s/i/I/gc' , 'exp' : 'Testing string' , 'prompt' : 'q' }, 122 \] 123 124for var in variants 125for run in [ 1 , 2 ] 126let cmd = var . cmd 127if run == 2 && cmd =~ "/.*/.*/." 128" Change :s/from/to/{flags} to :s{flags} 129let cmd = substitute ( cmd , '/.*/' , '' , '' ) 130endif 131call setline ( 1 , [ ln ]) 132let msg = printf ( 'using "%s"' , cmd ) 133let @/= 'ing' 134let v: errmsg = '' 135call feedkeys ( cmd . "\<CR>" . get ( var , 'prompt' , '' ), 'ntx' ) 136" No error should exist (matters for testing e flag) 137call assert_equal ( '' , v: errmsg , msg ) 138call assert_equal ( var . exp , getline ( '.' ), msg ) 139endfor 140endfor 141endfunc 142 143" Test the l, p, # flags. 144func Test_substitute_flags_lp () 145new 146call setline ( 1 , "abc\tdef\<C-h>ghi" ) 147 148let a = execute ( 's/a/a/p' ) 149call assert_equal ( "\nabc def^Hghi" , a ) 150 151let a = execute ( 's/a/a/l' ) 152call assert_equal ( "\nabc^Idef^Hghi$" , a ) 153 154let a = execute ( 's/a/a/#' ) 155call assert_equal ( "\n 1 abc def^Hghi" , a ) 156 157let a = execute ( 's/a/a/p#' ) 158call assert_equal ( "\n 1 abc def^Hghi" , a ) 159 160let a = execute ( 's/a/a/l#' ) 161call assert_equal ( "\n 1 abc^Idef^Hghi$" , a ) 162 163let a = execute ( 's/a/a/' ) 164call assert_equal ( "" , a ) 165 166bwipe ! 167endfunc 168 169func Test_substitute_repeat () 170" This caused an invalid memory access. 171split Xfile 172s /^/x 173call feedkeys ( "Qsc\<CR>y" , 'tx' ) 174bwipe ! 175endfunc 176 177" Test :s with ? as delimiter. 178func Test_substitute_question_delimiter () 179new 180call setline ( 1 , '??:??' ) 181 %s ?\?\??!!?g 182call assert_equal ( '!!:!!' , getline ( 1 )) 183bwipe ! 184endfunc 185 186" Test %s/\n// which is implemented as a special case to use a 187" more efficient join rather than doing a regular substitution. 188func Test_substitute_join () 189new 190 191call setline ( 1 , [ "foo\tbar" , "bar\<C-H>foo" ]) 192let a = execute ( '%s/\n//' ) 193call assert_equal ( "" , a ) 194call assert_equal ([ "foo\tbarbar\<C-H>foo" ], getline ( 1 , '$' )) 195call assert_equal ( '\n' , histget ( "search" , -1 )) 196 197call setline ( 1 , [ "foo\tbar" , "bar\<C-H>foo" ]) 198let a = execute ( '%s/\n//g' ) 199call assert_equal ( "" , a ) 200call assert_equal ([ "foo\tbarbar\<C-H>foo" ], getline ( 1 , '$' )) 201call assert_equal ( '\n' , histget ( "search" , -1 )) 202 203call setline ( 1 , [ "foo\tbar" , "bar\<C-H>foo" ]) 204let a = execute ( '%s/\n//p' ) 205call assert_equal ( "\nfoo barbar^Hfoo" , a ) 206call assert_equal ([ "foo\tbarbar\<C-H>foo" ], getline ( 1 , '$' )) 207call assert_equal ( '\n' , histget ( "search" , -1 )) 208 209call setline ( 1 , [ "foo\tbar" , "bar\<C-H>foo" ]) 210let a = execute ( '%s/\n//l' ) 211call assert_equal ( "\nfoo^Ibarbar^Hfoo$" , a ) 212call assert_equal ([ "foo\tbarbar\<C-H>foo" ], getline ( 1 , '$' )) 213call assert_equal ( '\n' , histget ( "search" , -1 )) 214 215call setline ( 1 , [ "foo\tbar" , "bar\<C-H>foo" ]) 216let a = execute ( '%s/\n//#' ) 217call assert_equal ( "\n 1 foo barbar^Hfoo" , a ) 218call assert_equal ([ "foo\tbarbar\<C-H>foo" ], getline ( 1 , '$' )) 219call assert_equal ( '\n' , histget ( "search" , -1 )) 220 221call setline ( 1 , [ 'foo' , 'bar' , 'baz' , 'qux' ]) 222call execute ( '1,2s/\n//' ) 223call assert_equal ([ 'foobarbaz' , 'qux' ], getline ( 1 , '$' )) 224 225bwipe ! 226endfunc 227 228func Test_substitute_count () 229new 230call setline ( 1 , [ 'foo foo' , 'foo foo' , 'foo foo' , 'foo foo' , 'foo foo' ]) 2312 232 233s /foo/bar/3 234call assert_equal ([ 'foo foo' , 'bar foo' , 'bar foo' , 'bar foo' , 'foo foo' ], 235 \getline ( 1 , '$' )) 236 237call assert_fails ( 's/foo/bar/0' , 'E939:' ) 238 239call setline ( 1 , [ 'foo foo' , 'foo foo' , 'foo foo' , 'foo foo' , 'foo foo' ]) 2402 , 4 s /foo/bar/ 10 241call assert_equal ([ 'foo foo' , 'foo foo' , 'foo foo' , 'bar foo' , 'bar foo' ], 242 \getline ( 1 , '$' )) 243 244call assert_fails ( 's/./b/2147483647' , 'E1510:' ) 245bwipe ! 246endfunc 247 248" Test substitute 'n' flag (report number of matches, do not substitute). 249func Test_substitute_flag_n () 250new 251let lines = [ 'foo foo' , 'foo foo' , 'foo foo' , 'foo foo' , 'foo foo' ] 252call setline ( 1 , lines ) 253 254call assert_equal ( "\n3 matches on 3 lines" , execute ( '2,4s/foo/bar/n' )) 255call assert_equal ( "\n6 matches on 3 lines" , execute ( '2,4s/foo/bar/gn' )) 256 257" c flag (confirm) should be ignored when using n flag. 258call assert_equal ( "\n3 matches on 3 lines" , execute ( '2,4s/foo/bar/nc' )) 259 260" No substitution should have been done. 261call assert_equal ( lines , getline ( 1 , '$' )) 262 263 %delete _ 264call setline ( 1 , [ 'A' , 'Bar' , 'Baz' ]) 265call assert_equal ( "\n1 match on 1 line" , execute ( 's/\nB\@=//gn' )) 266 267bwipe ! 268endfunc 269 270func Test_substitute_errors () 271new 272call setline ( 1 , 'foobar' ) 273 274call assert_fails ( 's/FOO/bar/' , 'E486:' ) 275call assert_fails ( 's/foo/bar/@' , 'E488:' ) 276call assert_fails ( 's/\(/bar/' , 'E54:' ) 277call assert_fails ( 's afooabara' , 'E146:' ) 278call assert_fails ( 's\\a' , 'E10:' ) 279 280setl nomodifiable 281call assert_fails ( 's/foo/bar/' , 'E21:' ) 282 283call assert_fails ( "let s=substitute([], 'a', 'A', 'g')" , 'E730:' ) 284call assert_fails ( "let s=substitute('abcda', [], 'A', 'g')" , 'E730:' ) 285call assert_fails ( "let s=substitute('abcda', 'a', [], 'g')" , 'E730:' ) 286call assert_fails ( "let s=substitute('abcda', 'a', 'A', [])" , 'E730:' ) 287call assert_fails ( "let s=substitute('abc', '\\%(', 'A', 'g')" , 'E53:' ) 288 289bwipe ! 290endfunc 291 292" Test for *sub-replace-special* and *sub-replace-expression* on substitute(). 293func Test_sub_replace_1 () 294" Run the tests with 'magic' on 295set magic 296set cpo & 297call assert_equal ( 'AA' , substitute ( 'A' , 'A' , '&&' , '' )) 298call assert_equal ( '&' , substitute ( 'B' , 'B' , '\&' , '' )) 299call assert_equal ( 'C123456789987654321' , substitute ( 'C123456789' , 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)' , '\0\9\8\7\6\5\4\3\2\1' , '' )) 300call assert_equal ( 'd' , substitute ( 'D' , 'D' , 'd' , '' )) 301call assert_equal ( '~' , substitute ( 'E' , 'E' , '~' , '' )) 302call assert_equal ( '~' , substitute ( 'F' , 'F' , '\~' , '' )) 303call assert_equal ( 'Gg' , substitute ( 'G' , 'G' , '\ugg' , '' )) 304call assert_equal ( 'Hh' , substitute ( 'H' , 'H' , '\Uh\Eh' , '' )) 305call assert_equal ( 'iI' , substitute ( 'I' , 'I' , '\lII' , '' )) 306call assert_equal ( 'jJ' , substitute ( 'J' , 'J' , '\LJ\EJ' , '' )) 307call assert_equal ( 'Kk' , substitute ( 'K' , 'K' , '\Uk\ek' , '' )) 308call assert_equal ( "l\<C-V>\<C-M>l" , 309 \substitute ( 'lLl' , 'L' , "\<C-V>\<C-M>" , '' )) 310call assert_equal ( "m\<C-M>m" , substitute ( 'mMm' , 'M' , '\r' , '' )) 311call assert_equal ( "n\<C-V>\<C-M>n" , 312 \substitute ( 'nNn' , 'N' , "\\\<C-V>\<C-M>" , '' )) 313call assert_equal ( "o\no" , substitute ( 'oOo' , 'O' , '\n' , '' )) 314call assert_equal ( "p\<C-H>p" , substitute ( 'pPp' , 'P' , '\b' , '' )) 315call assert_equal ( "q\tq" , substitute ( 'qQq' , 'Q' , '\t' , '' )) 316call assert_equal ( 'r\r' , substitute ( 'rRr' , 'R' , '\\' , '' )) 317call assert_equal ( 'scs' , substitute ( 'sSs' , 'S' , '\c' , '' )) 318call assert_equal ( "u\nu" , substitute ( 'uUu' , 'U' , "\n" , '' )) 319call assert_equal ( "v\<C-H>v" , substitute ( 'vVv' , 'V' , "\b" , '' )) 320call assert_equal ( "w\\w" , substitute ( 'wWw' , 'W' , "\\" , '' )) 321call assert_equal ( "x\<C-M>x" , substitute ( 'xXx' , 'X' , "\r" , '' )) 322call assert_equal ( "YyyY" , substitute ( 'Y' , 'Y' , '\L\uyYy\l\EY' , '' )) 323call assert_equal ( "zZZz" , substitute ( 'Z' , 'Z' , '\U\lZzZ\u\Ez' , '' )) 324" \v or \V after $ 325call assert_equal ( 'abxx' , substitute ( 'abcd' , 'xy$\v|cd$' , 'xx' , '' )) 326call assert_equal ( 'abxx' , substitute ( 'abcd' , 'xy$\V\|cd\$' , 'xx' , '' )) 327endfunc 328 329func Test_sub_replace_2 () 330" Run the tests with 'magic' off 331set nomagic 332set cpo & 333call assert_equal ( 'AA' , substitute ( 'A' , 'A' , '&&' , '' )) 334call assert_equal ( '&' , substitute ( 'B' , 'B' , '\&' , '' )) 335call assert_equal ( 'C123456789987654321' , substitute ( 'C123456789' , 'C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)' , '\0\9\8\7\6\5\4\3\2\1' , '' )) 336call assert_equal ( 'd' , substitute ( 'D' , 'D' , 'd' , '' )) 337call assert_equal ( '~' , substitute ( 'E' , 'E' , '~' , '' )) 338call assert_equal ( '~' , substitute ( 'F' , 'F' , '\~' , '' )) 339call assert_equal ( 'Gg' , substitute ( 'G' , 'G' , '\ugg' , '' )) 340call assert_equal ( 'Hh' , substitute ( 'H' , 'H' , '\Uh\Eh' , '' )) 341call assert_equal ( 'iI' , substitute ( 'I' , 'I' , '\lII' , '' )) 342call assert_equal ( 'jJ' , substitute ( 'J' , 'J' , '\LJ\EJ' , '' )) 343call assert_equal ( 'Kk' , substitute ( 'K' , 'K' , '\Uk\ek' , '' )) 344call assert_equal ( "l\<C-V>\<C-M>l" , 345 \substitute ( 'lLl' , 'L' , "\<C-V>\<C-M>" , '' )) 346call assert_equal ( "m\<C-M>m" , substitute ( 'mMm' , 'M' , '\r' , '' )) 347call assert_equal ( "n\<C-V>\<C-M>n" , 348 \substitute ( 'nNn' , 'N' , "\\\<C-V>\<C-M>" , '' )) 349call assert_equal ( "o\no" , substitute ( 'oOo' , 'O' , '\n' , '' )) 350call assert_equal ( "p\<C-H>p" , substitute ( 'pPp' , 'P' , '\b' , '' )) 351call assert_equal ( "q\tq" , substitute ( 'qQq' , 'Q' , '\t' , '' )) 352call assert_equal ( 'r\r' , substitute ( 'rRr' , 'R' , '\\' , '' )) 353call assert_equal ( 'scs' , substitute ( 'sSs' , 'S' , '\c' , '' )) 354call assert_equal ( "t\<C-M>t" , substitute ( 'tTt' , 'T' , "\r" , '' )) 355call assert_equal ( "u\nu" , substitute ( 'uUu' , 'U' , "\n" , '' )) 356call assert_equal ( "v\<C-H>v" , substitute ( 'vVv' , 'V' , "\b" , '' )) 357call assert_equal ( 'w\w' , substitute ( 'wWw' , 'W' , "\\" , '' )) 358call assert_equal ( 'XxxX' , substitute ( 'X' , 'X' , '\L\uxXx\l\EX' , '' )) 359call assert_equal ( 'yYYy' , substitute ( 'Y' , 'Y' , '\U\lYyY\u\Ey' , '' )) 360endfunc 361 362func Test_sub_replace_3 () 363set magic & 364set cpo & 365call assert_equal ( 'a\a' , substitute ( 'aAa' , 'A' , '\="\\"' , '' )) 366call assert_equal ( 'b\\b' , substitute ( 'bBb' , 'B' , '\="\\\\"' , '' )) 367call assert_equal ( "c\rc" , substitute ( 'cCc' , 'C' , "\\=\"\r\"" , '' )) 368call assert_equal ( "d\\\rd" , substitute ( 'dDd' , 'D' , "\\=\"\\\\\r\"" , '' )) 369call assert_equal ( "e\\\\\re" , substitute ( 'eEe' , 'E' , "\\=\"\\\\\\\\\r\"" , '' )) 370call assert_equal ( 'f\rf' , substitute ( 'fFf' , 'F' , '\="\\r"' , '' )) 371call assert_equal ( 'j\nj' , substitute ( 'jJj' , 'J' , '\="\\n"' , '' )) 372call assert_equal ( "k\<C-M>k" , substitute ( 'kKk' , 'K' , '\="\r"' , '' )) 373call assert_equal ( "l\nl" , substitute ( 'lLl' , 'L' , '\="\n"' , '' )) 374endfunc 375 376" Test for submatch() on substitute(). 377func Test_sub_replace_4 () 378set magic & 379set cpo & 380call assert_equal ( 'a\a' , substitute ( 'aAa' , 'A' , 381 \'\=substitute(submatch(0), ".", "\\", "")' , '' )) 382call assert_equal ( 'b\b' , substitute ( 'bBb' , 'B' , 383 \'\=substitute(submatch(0), ".", "\\\\", "")' , '' )) 384call assert_equal ( "c\<C-V>\<C-M>c" , substitute ( 'cCc' , 'C' , '\=substitute(submatch(0), ".", "\<C-V>\<C-M>", "")' , '' )) 385call assert_equal ( "d\<C-V>\<C-M>d" , substitute ( 'dDd' , 'D' , '\=substitute(submatch(0), ".", "\\\<C-V>\<C-M>", "")' , '' )) 386call assert_equal ( "e\\\<C-V>\<C-M>e" , substitute ( 'eEe' , 'E' , '\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-M>", "")' , '' )) 387call assert_equal ( "f\<C-M>f" , substitute ( 'fFf' , 'F' , '\=substitute(submatch(0), ".", "\\r", "")' , '' )) 388call assert_equal ( "j\nj" , substitute ( 'jJj' , 'J' , '\=substitute(submatch(0), ".", "\\n", "")' , '' )) 389call assert_equal ( "k\rk" , substitute ( 'kKk' , 'K' , '\=substitute(submatch(0), ".", "\r", "")' , '' )) 390call assert_equal ( "l\nl" , substitute ( 'lLl' , 'L' , '\=substitute(submatch(0), ".", "\n", "")' , '' )) 391endfunc 392 393func Test_sub_replace_5 () 394set magic & 395set cpo & 396call assert_equal ( 'A123456789987654321' , substitute ( 'A123456789' , 397 \'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)' , 398 \'\=submatch(0) . submatch(9) . submatch(8) . ' . 399 \'submatch(7) . submatch(6) . submatch(5) . ' . 400 \'submatch(4) . submatch(3) . submatch(2) . submatch(1)' , 401 \'' )) 402call assert_equal ( "[['A123456789'], ['9'], ['8'], ['7'], ['6'], " . 403 \"['5'], ['4'], ['3'], ['2'], ['1']]" , 404 \substitute ( 'A123456789' , 405 \'A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)' , 406 \'\=string([submatch(0, 1), submatch(9, 1), ' . 407 \'submatch(8, 1), 7->submatch(1), submatch(6, 1), ' . 408 \'submatch(5, 1), submatch(4, 1), submatch(3, 1), ' . 409 \'submatch(2, 1), submatch(1, 1)])' , 410 \'' )) 411endfunc 412 413func Test_sub_replace_6 () 414set magic & 415" Nvim: no "/" flag in 'cpoptions'. 416" set cpo+=/ 417call assert_equal ( 'a' , substitute ( 'A' , 'A' , 'a' , '' )) 418call assert_equal ( '%' , substitute ( 'B' , 'B' , '%' , '' )) 419set cpo -= / 420call assert_equal ( 'c' , substitute ( 'C' , 'C' , 'c' , '' )) 421call assert_equal ( '%' , substitute ( 'D' , 'D' , '%' , '' )) 422endfunc 423 424func Test_sub_replace_7 () 425set magic & 426set cpo & 427call assert_equal ( 'AA' , substitute ( 'AA' , 'A.' , '\=submatch(0)' , '' )) 428call assert_equal ( "B\nB" , substitute ( "B\nB" , 'B.' , '\=submatch(0)' , '' )) 429call assert_equal ( "['B\n']B" , substitute ( "B\nB" , 'B.' , '\=string(submatch(0, 1))' , '' )) 430call assert_equal ( '-abab' , substitute ( '-bb' , '\zeb' , 'a' , 'g' )) 431call assert_equal ( 'c-cbcbc' , substitute ( '-bb' , '\ze' , 'c' , 'g' )) 432endfunc 433 434" Test for *:s%* on :substitute. 435func Test_sub_replace_8 () 436new 437set magic & 438set cpo & 439$ put =',,X' 440s /\(^\|,\)\ze\(,\|X\)/\1N/g 441call assert_equal ( 'N,,NX' , getline ( "$" )) 442$ put =',,Y' 443let cmd = ':s/\(^\|,\)\ze\(,\|Y\)/\1N/gc' 444call feedkeys ( cmd . "\<CR>a" , "xt" ) 445call assert_equal ( 'N,,NY' , getline ( "$" )) 446: $ put =',,Z' 447let cmd = ':s/\(^\|,\)\ze\(,\|Z\)/\1N/gc' 448call feedkeys ( cmd . "\<CR>yy" , "xt" ) 449call assert_equal ( 'N,,NZ' , getline ( "$" )) 450enew ! |close 451endfunc 452 453func Test_sub_replace_9 () 454new 455set magic & 456set cpo & 457$ put ='xxx' 458call feedkeys ( ":s/x/X/gc\<CR>yyq" , "xt" ) 459call assert_equal ( 'XXx' , getline ( "$" )) 460enew ! |close 461endfunc 462 463func Test_sub_replace_10 () 464set magic & 465set cpo & 466call assert_equal ( 'a1a2a3a' , substitute ( '123' , '\zs' , 'a' , 'g' )) 467call assert_equal ( 'aaa' , substitute ( '123' , '\zs.' , 'a' , 'g' )) 468call assert_equal ( '1a2a3a' , substitute ( '123' , '.\zs' , 'a' , 'g' )) 469call assert_equal ( 'a1a2a3a' , substitute ( '123' , '\ze' , 'a' , 'g' )) 470call assert_equal ( 'a1a2a3' , substitute ( '123' , '\ze.' , 'a' , 'g' )) 471call assert_equal ( 'aaa' , substitute ( '123' , '.\ze' , 'a' , 'g' )) 472call assert_equal ( 'aa2a3a' , substitute ( '123' , '1\|\ze' , 'a' , 'g' )) 473call assert_equal ( '1aaa' , substitute ( '123' , '1\zs\|[23]' , 'a' , 'g' )) 474endfunc 475 476func SubReplacer ( text , submatches ) 477return a: text .. a: submatches [ 0 ] .. a: text 478endfunc 479func SubReplacerVar ( text , ...) 480return a: text .. a: 1 [ 0 ] .. a: text 481endfunc 482func SubReplacer20 ( t1 , t2 , t3 , t4 , t5 , t6 , t7 , t8 , t9 , t10 , t11 , t12 , t13 , t14 , t15 , t16 , t17 , t18 , t19 , submatches ) 483return a: t3 .. a: submatches [ 0 ] .. a: t11 484endfunc 485 486func Test_substitute_partial () 487call assert_equal ( '1foo2foo3' , substitute ( '123' , '2' , function ( 'SubReplacer' , [ 'foo' ]), 'g' )) 488call assert_equal ( '1foo2foo3' , substitute ( '123' , '2' , function ( 'SubReplacerVar' , [ 'foo' ]), 'g' )) 489 490" 19 arguments plus one is just OK 491let Replacer = function ( 'SubReplacer20' , repeat ([ 'foo' ], 19 )) 492call assert_equal ( '1foo2foo3' , substitute ( '123' , '2' , Replacer , 'g' )) 493 494" 20 arguments plus one is too many 495let Replacer = function ( 'SubReplacer20' , repeat ([ 'foo' ], 20 )) 496call assert_fails ( "call substitute('123', '2', Replacer, 'g')" , 'E118:' ) 497endfunc 498 499func Test_substitute_float () 500CheckFeature float 501 502call assert_equal ( 'number 1.23' , substitute ( 'number ' , '$' , { ->1.23 }, '' )) 503" vim9 assert_equal('number 1.23', substitute('number ', '$', () => 1.23, '')) 504endfunc 505 506" Tests for *sub-replace-special* and *sub-replace-expression* on :substitute. 507 508" Execute a list of :substitute command tests 509func Run_SubCmd_Tests ( tests ) 510enew ! 511for t in a: tests 512let start = line ( '.' ) + 1 513let end = start + len ( t [ 2 ]) - 1 514exe "normal o" . t [ 0 ] 515call cursor ( start , 1 ) 516exe t [ 1 ] 517call assert_equal ( t [ 2 ], getline ( start , end ), t [ 1 ]) 518endfor 519enew ! 520endfunc 521 522func Test_sub_cmd_1 () 523set magic 524set cpo & 525 526" List entry format: [input, cmd, output] 527let tests = [[ 'A' , 's/A/&&/' , [ 'AA' ]], 528 \[ 'B' , 's/B/\&/' , [ '&' ]], 529 \[ 'C123456789' , 's/C\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/' , [ 'C123456789987654321' ]], 530 \[ 'D' , 's/D/d/' , [ 'd' ]], 531 \[ 'E' , 's/E/~/' , [ 'd' ]], 532 \[ 'F' , 's/F/\~/' , [ '~' ]], 533 \[ 'G' , 's/G/\ugg/' , [ 'Gg' ]], 534 \[ 'H' , 's/H/\Uh\Eh/' , [ 'Hh' ]], 535 \[ 'I' , 's/I/\lII/' , [ 'iI' ]], 536 \[ 'J' , 's/J/\LJ\EJ/' , [ 'jJ' ]], 537 \[ 'K' , 's/K/\Uk\ek/' , [ 'Kk' ]], 538 \[ 'lLl' , "s/L/\<C-V>\<C-M>/" , [ "l\<C-V>" , 'l' ]], 539 \[ 'mMm' , 's/M/\r/' , [ 'm' , 'm' ]], 540 \[ 'nNn' , "s/N/\\\<C-V>\<C-M>/" , [ "n\<C-V>" , 'n' ]], 541 \[ 'oOo' , 's/O/\n/' , [ "o\no" ]], 542 \[ 'pPp' , 's/P/\b/' , [ "p\<C-H>p" ]], 543 \[ 'qQq' , 's/Q/\t/' , [ "q\tq" ]], 544 \[ 'rRr' , 's/R/\\/' , [ 'r\r' ]], 545 \[ 'sSs' , 's/S/\c/' , [ 'scs' ]], 546 \[ 'tTt' , "s/T/\<C-V>\<C-J>/" , [ "t\<C-V>\<C-J>t" ]], 547 \[ 'U' , 's/U/\L\uuUu\l\EU/' , [ 'UuuU' ]], 548 \[ 'V' , 's/V/\U\lVvV\u\Ev/' , [ 'vVVv' ]], 549 \[ '\' , 's/\\/\\\\/' , [ '\\' ]] 550 \] 551call Run_SubCmd_Tests ( tests ) 552endfunc 553 554func Test_sub_cmd_2 () 555set nomagic 556set cpo & 557 558" List entry format: [input, cmd, output] 559let tests = [[ 'A' , 's/A/&&/' , [ '&&' ]], 560 \[ 'B' , 's/B/\&/' , [ 'B' ]], 561 \[ 'C123456789' , 's/\mC\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\0\9\8\7\6\5\4\3\2\1/' , [ 'C123456789987654321' ]], 562 \[ 'D' , 's/D/d/' , [ 'd' ]], 563 \[ 'E' , 's/E/~/' , [ '~' ]], 564 \[ 'F' , 's/F/\~/' , [ '~' ]], 565 \[ 'G' , 's/G/\ugg/' , [ 'Gg' ]], 566 \[ 'H' , 's/H/\Uh\Eh/' , [ 'Hh' ]], 567 \[ 'I' , 's/I/\lII/' , [ 'iI' ]], 568 \[ 'J' , 's/J/\LJ\EJ/' , [ 'jJ' ]], 569 \[ 'K' , 's/K/\Uk\ek/' , [ 'Kk' ]], 570 \[ 'lLl' , "s/L/\<C-V>\<C-M>/" , [ "l\<C-V>" , 'l' ]], 571 \[ 'mMm' , 's/M/\r/' , [ 'm' , 'm' ]], 572 \[ 'nNn' , "s/N/\\\<C-V>\<C-M>/" , [ "n\<C-V>" , 'n' ]], 573 \[ 'oOo' , 's/O/\n/' , [ "o\no" ]], 574 \[ 'pPp' , 's/P/\b/' , [ "p\<C-H>p" ]], 575 \[ 'qQq' , 's/Q/\t/' , [ "q\tq" ]], 576 \[ 'rRr' , 's/R/\\/' , [ 'r\r' ]], 577 \[ 'sSs' , 's/S/\c/' , [ 'scs' ]], 578 \[ 'tTt' , "s/T/\<C-V>\<C-J>/" , [ "t\<C-V>\<C-J>t" ]], 579 \[ 'U' , 's/U/\L\uuUu\l\EU/' , [ 'UuuU' ]], 580 \[ 'V' , 's/V/\U\lVvV\u\Ev/' , [ 'vVVv' ]], 581 \[ '\' , 's/\\/\\\\/' , [ '\\' ]] 582 \] 583call Run_SubCmd_Tests ( tests ) 584endfunc 585 586func Test_sub_cmd_3 () 587set nomagic 588set cpo & 589 590" List entry format: [input, cmd, output] 591let tests = [[ 'aAa' , "s/A/\\='\\'/" , [ 'a\a' ]], 592 \[ 'bBb' , "s/B/\\='\\\\'/" , [ 'b\\b' ]], 593 \[ 'cCc' , "s/C/\\='\<C-V>\<C-M>'/" , [ "c\<C-V>" , 'c' ]], 594 \[ 'dDd' , "s/D/\\='\\\<C-V>\<C-M>'/" , [ "d\\\<C-V>" , 'd' ]], 595 \[ 'eEe' , "s/E/\\='\\\\\<C-V>\<C-M>'/" , [ "e\\\\\<C-V>" , 'e' ]], 596 \[ 'fFf' , "s/F/\\='\r'/" , [ 'f' , 'f' ]], 597 \[ 'gGg' , "s/G/\\='\<C-V>\<C-J>'/" , [ "g\<C-V>" , 'g' ]], 598 \[ 'hHh' , "s/H/\\='\\\<C-V>\<C-J>'/" , [ "h\\\<C-V>" , 'h' ]], 599 \[ 'iIi' , "s/I/\\='\\\\\<C-V>\<C-J>'/" , [ "i\\\\\<C-V>" , 'i' ]], 600 \[ 'jJj' , "s/J/\\='\n'/" , [ 'j' , 'j' ]], 601 \[ 'kKk' , 's/K/\="\r"/' , [ 'k' , 'k' ]], 602 \[ 'lLl' , 's/L/\="\n"/' , [ 'l' , 'l' ]] 603 \] 604call Run_SubCmd_Tests ( tests ) 605endfunc 606 607" Test for submatch() on :substitute. 608func Test_sub_cmd_4 () 609set magic & 610set cpo & 611 612" List entry format: [input, cmd, output] 613let tests = [ [ 'aAa' , "s/A/\\=substitute(submatch(0), '.', '\\', '')/" , 614 \[ 'a\a' ]], 615 \[ 'bBb' , "s/B/\\=substitute(submatch(0), '.', '\\', '')/" , 616 \[ 'b\b' ]], 617 \[ 'cCc' , "s/C/\\=substitute(submatch(0), '.', '\<C-V>\<C-M>', '')/" , 618 \[ "c\<C-V>" , 'c' ]], 619 \[ 'dDd' , "s/D/\\=substitute(submatch(0), '.', '\\\<C-V>\<C-M>', '')/" , 620 \[ "d\<C-V>" , 'd' ]], 621 \[ 'eEe' , "s/E/\\=substitute(submatch(0), '.', '\\\\\<C-V>\<C-M>', '')/" , 622 \[ "e\\\<C-V>" , 'e' ]], 623 \[ 'fFf' , "s/F/\\=substitute(submatch(0), '.', '\\r', '')/" , 624 \[ 'f' , 'f' ]], 625 \[ 'gGg' , 's/G/\=substitute(submatch(0), ".", "\<C-V>\<C-J>", "")/' , 626 \[ "g\<C-V>" , 'g' ]], 627 \[ 'hHh' , 's/H/\=substitute(submatch(0), ".", "\\\<C-V>\<C-J>", "")/' , 628 \[ "h\<C-V>" , 'h' ]], 629 \[ 'iIi' , 's/I/\=substitute(submatch(0), ".", "\\\\\<C-V>\<C-J>", "")/' , 630 \[ "i\\\<C-V>" , 'i' ]], 631 \[ 'jJj' , "s/J/\\=substitute(submatch(0), '.', '\\n', '')/" , 632 \[ 'j' , 'j' ]], 633 \[ 'kKk' , "s/K/\\=substitute(submatch(0), '.', '\\r', '')/" , 634 \[ 'k' , 'k' ]], 635 \[ 'lLl' , "s/L/\\=substitute(submatch(0), '.', '\\n', '')/" , 636 \[ 'l' , 'l' ]], 637 \] 638call Run_SubCmd_Tests ( tests ) 639endfunc 640 641func Test_sub_cmd_5 () 642set magic & 643set cpo & 644 645" List entry format: [input, cmd, output] 646let tests = [ [ 'A123456789' , 's/A\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\=submatch(0) . submatch(9) . submatch(8) . submatch(7) . submatch(6) . submatch(5) . submatch(4) . submatch(3) . submatch(2) . submatch(1)/' , [ 'A123456789987654321' ]], 647 \[ 'B123456789' , 's/B\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)/\=string([submatch(0, 1), submatch(9, 1), submatch(8, 1), submatch(7, 1), submatch(6, 1), submatch(5, 1), submatch(4, 1), submatch(3, 1), submatch(2, 1), submatch(1, 1)])/' , [ "[['B123456789'], ['9'], ['8'], ['7'], ['6'], ['5'], ['4'], ['3'], ['2'], ['1']]" ]], 648 \] 649call Run_SubCmd_Tests ( tests ) 650endfunc 651 652" Test for *:s%* on :substitute. 653func Test_sub_cmd_6 () 654set magic & 655" Nvim: no "/" flag in 'cpoptions'. 656" set cpo+=/ 657 658" List entry format: [input, cmd, output] 659let tests = [ [ 'A' , 's/A/a/' , [ 'a' ]], 660 \[ 'B' , 's/B/%/' , [ 'a' ]], 661 \] 662" call Run_SubCmd_Tests(tests) 663 664set cpo -= / 665let tests = [ [ 'C' , 's/C/c/' , [ 'c' ]], 666 \[ 'D' , 's/D/%/' , [ '%' ]], 667 \] 668call Run_SubCmd_Tests ( tests ) 669 670set cpo & 671endfunc 672 673" Test for :s replacing \n with line break. 674func Test_sub_cmd_7 () 675set magic & 676set cpo & 677 678" List entry format: [input, cmd, output] 679let tests = [ [ "A\<C-V>\<C-M>A" , 's/A./\=submatch(0)/' , [ 'A' , 'A' ]], 680 \[ "B\<C-V>\<C-J>B" , 's/B./\=submatch(0)/' , [ 'B' , 'B' ]], 681 \[ "C\<C-V>\<C-J>C" , 's/C./\=strtrans(string(submatch(0, 1)))/' , [ strtrans ( "['C\<C-J>']C" )]], 682 \[ "D\<C-V>\<C-J>\nD" , 's/D.\nD/\=strtrans(string(submatch(0, 1)))/' , [ strtrans ( "['D\<C-J>', 'D']" )]], 683 \[ "E\<C-V>\<C-J>\n\<C-V>\<C-J>\n\<C-V>\<C-J>\n\<C-V>\<C-J>\n\<C-V>\<C-J>E" , 's/E\_.\{-}E/\=strtrans(string(submatch(0, 1)))/' , [ strtrans ( "['E\<C-J>', '\<C-J>', '\<C-J>', '\<C-J>', '\<C-J>E']" )]], 684 \] 685call Run_SubCmd_Tests ( tests ) 686 687exe "normal oQ\nQ\<Esc>k" 688call assert_fails ( 's/Q[^\n]Q/\=submatch(0)."foobar"/' , 'E486' ) 689enew ! 690endfunc 691 692func TitleString () 693let check = 'foo' =~ 'bar' 694return "" 695endfunc 696 697func Test_sub_cmd_8 () 698set titlestring = %{TitleString()} 699 700enew ! 701call append ( 0 , [ '' , 'test_one' , 'test_two' ]) 702call cursor ( 1 , 1 ) 703/ ^test_one / s /. * /\= "foo\nbar"/ 704call assert_equal ( 'foo' , getline ( 2 )) 705call assert_equal ( 'bar' , getline ( 3 )) 706call feedkeys ( ':/^test_two/s/.*/\="foo\nbar"/c' , "t" ) 707call feedkeys ( "\<CR>y" , "xt" ) 708call assert_equal ( 'foo' , getline ( 4 )) 709call assert_equal ( 'bar' , getline ( 5 )) 710 711enew ! 712set titlestring & 713endfunc 714 715func Test_sub_cmd_9 () 716new 717let input = [ '1 aaa' , '2 aaa' , '3 aaa' ] 718call setline ( 1 , input ) 719func Foo () 720return submatch ( 0 ) 721endfunc 722 %s /aaa/\=Foo()/gn 723call assert_equal ( input , getline ( 1 , '$' )) 724call assert_equal ( 1 , & modifiable ) 725 726delfunc Foo 727bw ! 728endfunc 729 730func Test_sub_highlight_zero_match () 731CheckScreendump 732CheckRunVimInTerminal 733 734let lines =<< trim END 735call setline(1, ['one', 'two', 'three']) 736END 737call writefile ( lines , 'XscriptSubHighlight' , 'D' ) 738let buf = RunVimInTerminal ( '-S XscriptSubHighlight' , #{ rows : 8 , cols : 60 }) 739call term_sendkeys ( buf , ":%s/^/ /c\<CR>" ) 740call VerifyScreenDump ( buf , 'Test_sub_highlight_zer_match_1' , {}) 741 742call term_sendkeys ( buf , "\<Esc>" ) 743call StopVimInTerminal ( buf ) 744endfunc 745 746func Test_nocatch_sub_failure_handling () 747" normal error results in all replacements 748func Foo () 749foobar 750endfunc 751new 752call setline ( 1 , [ '1 aaa' , '2 aaa' , '3 aaa' ]) 753" need silent! to avoid a delay when entering Insert mode 754silent ! %s /aaa/\=Foo()/g 755call assert_equal ([ '1 0' , '2 0' , '3 0' ], getline ( 1 , 3 )) 756 757" Throw without try-catch causes abort after the first line. 758" We cannot test this, since it would stop executing the test script. 759 760" try/catch does not result in any changes 761func ! Foo () 762throw 'error' 763endfunc 764call setline ( 1 , [ '1 aaa' , '2 aaa' , '3 aaa' ]) 765let error_caught = 0 766try 767 %s /aaa/\=Foo()/g 768catch 769let error_caught = 1 770endtry 771call assert_equal ( 1 , error_caught ) 772call assert_equal ([ '1 aaa' , '2 aaa' , '3 aaa' ], getline ( 1 , 3 )) 773 774" Same, but using "n" flag so that "sandbox" gets set 775call setline ( 1 , [ '1 aaa' , '2 aaa' , '3 aaa' ]) 776let error_caught = 0 777try 778 %s /aaa/\=Foo()/gn 779catch 780let error_caught = 1 781endtry 782call assert_equal ( 1 , error_caught ) 783call assert_equal ([ '1 aaa' , '2 aaa' , '3 aaa' ], getline ( 1 , 3 )) 784 785delfunc Foo 786bwipe ! 787endfunc 788 789" Test ":s/pat/sub/" with different ~s in sub. 790func Test_replace_with_tilde () 791new 792" Set the last replace string to empty 793s /^$// 794call append ( 0 , [ '- Bug in "vPPPP" on this text:' ]) 795normal gg 796s /u/~u~/ 797call assert_equal ( '- Bug in "vPPPP" on this text:' , getline ( 1 )) 798s /i/~u~/ 799call assert_equal ( '- Bug uuun "vPPPP" on this text:' , getline ( 1 )) 800s /o/~~~/ 801call assert_equal ( '- Bug uuun "vPPPP" uuuuuuuuun this text:' , getline ( 1 )) 802bw ! 803endfunc 804 805func Test_replace_keeppatterns () 806new 807a 808foobar 809 810substitute foo asdf foo 811 812one two 813. 814 815normal gg 816/ ^substitute 817s /foo/bar/ 818call assert_equal ( 'foo' , @/) 819call assert_equal ( 'substitute bar asdf foo' , getline ( '.' )) 820 821/ ^substitute 822keeppatterns s/asdf/xyz/ 823call assert_equal ( '^substitute' , @/) 824call assert_equal ( 'substitute bar xyz foo' , getline ( '.' )) 825 826/ ^substitute 827 & 828call assert_equal ( '^substitute' , @/) 829call assert_equal ( 'substitute bar xyz bar' , getline ( '.' )) 830 831exe "normal /bar /e\<CR>" 832call assert_equal ( 15 , col ( '.' )) 833normal - 834keeppatterns /xyz 835call assert_equal ( 'bar ' , @/) 836call assert_equal ( 'substitute bar xyz bar' , getline ( '.' )) 837exe "normal 0dn" 838call assert_equal ( 'xyz bar' , getline ( '.' )) 839 840bw ! 841endfunc 842 843func Test_sub_beyond_end () 844new 845call setline ( 1 , '#' ) 846let @/= '^#\n\zs' 847s ///e 848call assert_equal ( '#' , getline ( 1 )) 849bwipe ! 850endfunc 851 852" Test for repeating last substitution using :~ and :&r 853func Test_repeat_last_sub () 854new 855call setline ( 1 , [ 'blue green yellow orange white' ]) 856s /blue/red/ 857let @/= 'yellow' 858 ~ 859let @/= 'white' 860: &r 861let @/= 'green' 862s //gray 863call assert_equal ( 'red gray red orange red' , getline ( 1 )) 864bw ! 865endfunc 866 867" Test for Vi compatible substitution: 868" \/{string}/, \?{string}? and \&{string}& 869func Test_sub_vi_compatibility () 870new 871call setline ( 1 , [ 'blue green yellow orange blue' ]) 872let @/= 'orange' 873s \/white/ 874let @/= 'blue' 875s \?amber? 876let @/= 'white' 877s \&green& 878call assert_equal ( 'amber green yellow white green' , getline ( 1 )) 879bw ! 880endfunc 881 882" Test for substitute with the new text longer than the original text 883func Test_sub_expand_text () 884new 885call setline ( 1 , 'abcabcabcabcabcabcabcabc' ) 886s /b/\=repeat('B', 10)/g 887call assert_equal ( repeat ( 'aBBBBBBBBBBc' , 8 ), getline ( 1 )) 888bw ! 889endfunc 890 891" Test for command failures when the last substitute pattern is not set. 892func Test_sub_with_no_last_pat () 893let lines =<< trim [SCRIPT] 894call assert_fails('~', 'E33:') 895call assert_fails('s//abc/g', 'E35:') 896call assert_fails('s\/bar', 'E35:') 897call assert_fails('s\&bar&', 'E33:') 898call writefile(v:errors, 'Xresult') 899qall! 900[SCRIPT] 901call writefile ( lines , 'Xscript' , 'D' ) 902if RunVim ([], [], '--clean -S Xscript' ) 903call assert_equal ([], readfile ( 'Xresult' )) 904endif 905 906let lines =<< trim [SCRIPT] 907set cpo+=/ 908call assert_fails('s/abc/%/', 'E33:') 909call writefile(v:errors, 'Xresult') 910qall! 911[SCRIPT] 912" Nvim: no "/" flag in 'cpoptions'. 913" call writefile(lines, 'Xscript') 914" if RunVim([], [], '--clean -S Xscript') 915" call assert_equal([], readfile('Xresult')) 916" endif 917 918call delete ( 'Xresult' ) 919endfunc 920 921func Test_substitute () 922call assert_equal ( 'a1a2a3a' , substitute ( '123' , '\zs' , 'a' , 'g' )) 923" Substitute with special keys 924call assert_equal ( "a\<End>c" , substitute ( 'abc' , "a.c" , "a\<End>c" , '' )) 925endfunc 926 927func Test_substitute_expr () 928let g: val = 'XXX' 929call assert_equal ( 'XXX' , substitute ( 'yyy' , 'y*' , '\=g:val' , '' )) 930call assert_equal ( 'XXX' , substitute ( 'yyy' , 'y*' , { ->g: val }, '' )) 931call assert_equal ( "-\u1b \uf2-" , substitute ( "-%1b %f2-" , '%\(\x\x\)' , 932 \'\=nr2char("0x" . submatch(1))' , 'g' )) 933call assert_equal ( "-\u1b \uf2-" , substitute ( "-%1b %f2-" , '%\(\x\x\)' , 934 \{ ->nr2char ( "0x" . submatch ( 1 ))}, 'g' )) 935 936call assert_equal ( '231' , substitute ( '123' , '\(.\)\(.\)\(.\)' , 937 \{ ->submatch ( 2 ) . submatch ( 3 ) . submatch ( 1 )}, '' )) 938 939func Recurse () 940return substitute ( 'yyy' , 'y\(.\)y' , { ->submatch ( 1 )}, '' ) 941endfunc 942" recursive call works 943call assert_equal ( '-y-x-' , substitute ( 'xxx' , 'x\(.\)x' , { ->'-' . Recurse () . '-' . submatch ( 1 ) . '-' }, '' )) 944 945call assert_fails ( "let s=submatch([])" , 'E745:' ) 946call assert_fails ( "let s=submatch(2, [])" , 'E745:' ) 947endfunc 948 949func Test_invalid_submatch () 950" This was causing invalid memory access in Vim-7.4.2232 and older 951call assert_fails ( "call substitute('x', '.', {-> submatch(10)}, '')" , 'E935:' ) 952call assert_fails ( 'eval submatch(-1)' , 'E935:' ) 953call assert_equal ( '' , submatch ( 0 )) 954call assert_equal ( '' , submatch ( 1 )) 955call assert_equal ([], submatch ( 0 , 1 )) 956call assert_equal ([], submatch ( 1 , 1 )) 957endfunc 958 959func Test_submatch_list_concatenate () 960let pat = 'A\(.\)' 961let Rep = { ->string ([ submatch ( 0 , 1 )] + [[ submatch ( 1 )]])} 962call substitute ( 'A1' , pat , Rep , '' ) ->assert_equal ( "[['A1'], ['1']]" ) 963endfunc 964 965func Test_substitute_expr_arg () 966call assert_equal ( '123456789-123456789=' , substitute ( '123456789' , 967 \'\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)\(.\)' , 968 \{ m ->m [ 0 ] . '-' . m [ 1 ] . m [ 2 ] . m [ 3 ] . m [ 4 ] . m [ 5 ] . m [ 6 ] . m [ 7 ] . m [ 8 ] . m [ 9 ] . '=' }, '' )) 969 970call assert_equal ( '123456-123456=789' , substitute ( '123456789' , 971 \'\(.\)\(.\)\(.\)\(a*\)\(n*\)\(.\)\(.\)\(.\)\(x*\)' , 972 \{ m ->m [ 0 ] . '-' . m [ 1 ] . m [ 2 ] . m [ 3 ] . m [ 4 ] . m [ 5 ] . m [ 6 ] . m [ 7 ] . m [ 8 ] . m [ 9 ] . '=' }, '' )) 973 974call assert_equal ( '123456789-123456789x=' , substitute ( '123456789' , 975 \'\(.\)\(.\)\(.*\)' , 976 \{ m ->m [ 0 ] . '-' . m [ 1 ] . m [ 2 ] . m [ 3 ] . 'x' . m [ 4 ] . m [ 5 ] . m [ 6 ] . m [ 7 ] . m [ 8 ] . m [ 9 ] . '=' }, '' )) 977 978call assert_fails ( "call substitute('xxx', '.', {m -> string(add(m, 'x'))}, '')" , 'E742:' ) 979call assert_fails ( "call substitute('xxx', '.', {m -> string(insert(m, 'x'))}, '')" , 'E742:' ) 980call assert_fails ( "call substitute('xxx', '.', {m -> string(extend(m, ['x']))}, '')" , 'E742:' ) 981call assert_fails ( "call substitute('xxx', '.', {m -> string(remove(m, 1))}, '')" , 'E742:' ) 982endfunc 983 984" Test for using a function to supply the substitute string 985func Test_substitute_using_func () 986func Xfunc () 987return '1234' 988endfunc 989call assert_equal ( 'a1234f' , substitute ( 'abcdef' , 'b..e' , 990 \function ( "Xfunc" ), '' )) 991delfunc Xfunc 992endfunc 993 994" Test for using submatch() with a multiline match 995func Test_substitute_multiline_submatch () 996new 997call setline ( 1 , [ 'line1' , 'line2' , 'line3' , 'line4' ]) 998 %s /^line1\(\_.\+\)line4$/\=submatch(1)/ 999call assert_equal ([ '' , 'line2' , 'line3' , '' ], getline ( 1 , '$' )) 1000bw ! 1001endfunc 1002 1003func Test_substitute_skipped_range () 1004new 1005if 0 1006/ 1 / 5 /2/2/\n 1007endif 1008call assert_equal ([ 0 , 1 , 1 , 0 , 1 ], getcurpos ()) 1009bwipe ! 1010endfunc 1011 1012" Test using the 'gdefault' option (when on, flag 'g' is default on). 1013func Test_substitute_gdefault () 1014new 1015 1016" First check without 'gdefault' 1017call setline ( 1 , 'foo bar foo' ) 1018s /foo/FOO/ 1019call assert_equal ( 'FOO bar foo' , getline ( 1 )) 1020call setline ( 1 , 'foo bar foo' ) 1021s /foo/FOO/g 1022call assert_equal ( 'FOO bar FOO' , getline ( 1 )) 1023call setline ( 1 , 'foo bar foo' ) 1024s /foo/FOO/gg 1025call assert_equal ( 'FOO bar foo' , getline ( 1 )) 1026 1027" Then check with 'gdefault' 1028set gdefault 1029call setline ( 1 , 'foo bar foo' ) 1030s /foo/FOO/ 1031call assert_equal ( 'FOO bar FOO' , getline ( 1 )) 1032call setline ( 1 , 'foo bar foo' ) 1033s /foo/FOO/g 1034call assert_equal ( 'FOO bar foo' , getline ( 1 )) 1035call setline ( 1 , 'foo bar foo' ) 1036s /foo/FOO/gg 1037call assert_equal ( 'FOO bar FOO' , getline ( 1 )) 1038 1039" Setting 'compatible' should reset 'gdefault' 1040call assert_equal ( 1 , & gdefault ) 1041" set compatible 1042set nogdefault 1043call assert_equal ( 0 , & gdefault ) 1044set nocompatible 1045call assert_equal ( 0 , & gdefault ) 1046 1047bw ! 1048endfunc 1049 1050" This was using "old_sub" after it was freed. 1051func Test_using_old_sub () 1052" set compatible maxfuncdepth=10 1053set maxfuncdepth = 10 1054new 1055call setline ( 1 , 'some text.' ) 1056func Repl () 1057 ~ 1058s / 1059endfunc 1060silent ! s /\%')/\=Repl() 1061 1062delfunc Repl 1063bwipe ! 1064set nocompatible 1065endfunc 1066 1067" This was switching windows in between computing the length and using it. 1068func Test_sub_change_window () 1069silent ! lfile 1070sil ! norm o0000000000000000000000000000000000000000000000000000 1071func Repl () 1072lopen 1073endfunc 1074silent ! s /\%')/\=Repl() 1075bwipe ! 1076bwipe ! 1077delfunc Repl 1078endfunc 1079 1080" This was undoign a change in between computing the length and using it. 1081func Do_Test_sub_undo_change () 1082new 1083norm o0000000000000000000000000000000000000000000000000000 1084silent ! s /\%')/\=Repl() 1085bwipe ! 1086endfunc 1087 1088func Test_sub_undo_change () 1089func Repl () 1090silent ! norm g- 1091endfunc 1092call Do_Test_sub_undo_change () 1093 1094func ! Repl () 1095silent earlier 1096endfunc 1097call Do_Test_sub_undo_change () 1098 1099delfunc Repl 1100endfunc 1101 1102" This was opening a command line window from the expression 1103func Test_sub_open_cmdline_win () 1104" the error only happens in a very specific setup, run a new Vim instance to 1105" get a clean starting point. 1106let lines =<< trim [SCRIPT] 1107set vb t_vb= 1108norm o0000000000000000000000000000000000000000000000000000 1109func Replace() 1110norm q/ 1111endfunc 1112s/\%')/\=Replace() 1113redir >Xresult 1114messages 1115redir END 1116qall! 1117[SCRIPT] 1118call writefile ( lines , 'Xscript' , 'D' ) 1119if RunVim ([], [], '-u NONE -S Xscript' ) 1120call assert_match ( 'E565: Not allowed to change text or change window' , 1121 \readfile ( 'Xresult' ) ->join ( 'XX' )) 1122endif 1123 1124call delete ( 'Xresult' ) 1125endfunc 1126 1127" This was editing a script file from the expression 1128func Test_sub_edit_scriptfile () 1129new 1130norm o0000000000000000000000000000000000000000000000000000 1131func EditScript () 1132silent ! scr ! Xfile 1133endfunc 1134s /\%')/\=EditScript() 1135 1136delfunc EditScript 1137bwipe ! 1138endfunc 1139 1140" This was editing another file from the expression. 1141func Test_sub_expr_goto_other_file () 1142call writefile ([ '' ], 'Xfileone' , 'D' ) 1143enew ! 1144call setline ( 1 , [ 'a' , 'b' , 'c' , 'd' , 1145 \'Xfileone zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz' ]) 1146 1147func g: SplitGotoFile () 1148exe "sil! norm 0\<C-W>gf" 1149return '' 1150endfunc 1151 1152$ 1153s /\%')/\=g:SplitGotoFile() 1154 1155delfunc g:SplitGotoFile 1156bwipe ! 1157endfunc 1158 1159func Test_recursive_expr_substitute () 1160" this was reading invalid memory 1161let lines =<< trim END 1162func Repl(g, n) 1163s 1164r%:s000 1165endfunc 1166next 0 1167let caught = 0 1168s/\%')/\=Repl(0, 0) 1169qall! 1170END 1171call writefile ( lines , 'XexprSubst' , 'D' ) 1172call RunVim ([], [], '--clean -S XexprSubst' ) 1173endfunc 1174 1175" Test for the 2-letter and 3-letter :substitute commands 1176func Test_substitute_short_cmd () 1177new 1178call setline ( 1 , [ 'one' , 'one one one' ]) 1179s /one/two 1180call cursor ( 2 , 1 ) 1181 1182" :sc 1183call feedkeys ( ":sc\<CR>y" , 'xt' ) 1184call assert_equal ( 'two one one' , getline ( 2 )) 1185 1186" :scg 1187call setline ( 2 , 'one one one' ) 1188call feedkeys ( ":scg\<CR>nyq" , 'xt' ) 1189call assert_equal ( 'one two one' , getline ( 2 )) 1190 1191" :sci 1192call setline ( 2 , 'ONE One onE' ) 1193call feedkeys ( ":sci\<CR>y" , 'xt' ) 1194call assert_equal ( 'two One onE' , getline ( 2 )) 1195 1196" :scI 1197set ignorecase 1198call setline ( 2 , 'ONE One one' ) 1199call feedkeys ( ":scI\<CR>y" , 'xt' ) 1200call assert_equal ( 'ONE One two' , getline ( 2 )) 1201set ignorecase & 1202 1203" :scn 1204call setline ( 2 , 'one one one' ) 1205let t = execute ( 'scn' ) ->split ( "\n" ) 1206call assert_equal ([ '1 match on 1 line' ], t ) 1207call assert_equal ( 'one one one' , getline ( 2 )) 1208 1209" :scp 1210call setline ( 2 , "\tone one one" ) 1211redir => output 1212call feedkeys ( ":scp\<CR>y" , 'xt' ) 1213redir END 1214call assert_equal ( ' two one one' , output ->split ( "\n" )[ -1 ]) 1215call assert_equal ( "\ttwo one one" , getline ( 2 )) 1216 1217" :scl 1218call setline ( 2 , "\tone one one" ) 1219redir => output 1220call feedkeys ( ":scl\<CR>y" , 'xt' ) 1221redir END 1222call assert_equal ( "^Itwo one one$" , output ->split ( "\n" )[ -1 ]) 1223call assert_equal ( "\ttwo one one" , getline ( 2 )) 1224 1225" :sgc 1226call setline ( 2 , 'one one one one one' ) 1227call feedkeys ( ":sgc\<CR>nyyq" , 'xt' ) 1228call assert_equal ( 'one two two one one' , getline ( 2 )) 1229 1230" :sg 1231call setline ( 2 , 'one one one' ) 1232sg 1233call assert_equal ( 'two two two' , getline ( 2 )) 1234 1235" :sgi 1236call setline ( 2 , 'ONE One onE' ) 1237sgi 1238call assert_equal ( 'two two two' , getline ( 2 )) 1239 1240" :sgI 1241set ignorecase 1242call setline ( 2 , 'ONE One one' ) 1243sgI 1244call assert_equal ( 'ONE One two' , getline ( 2 )) 1245set ignorecase & 1246 1247" :sgn 1248call setline ( 2 , 'one one one' ) 1249let t = execute ( 'sgn' ) ->split ( "\n" ) 1250call assert_equal ([ '3 matches on 1 line' ], t ) 1251call assert_equal ( 'one one one' , getline ( 2 )) 1252 1253" :sgp 1254call setline ( 2 , "\tone one one" ) 1255redir => output 1256sgp 1257redir END 1258call assert_equal ( ' two two two' , output ->split ( "\n" )[ -1 ]) 1259call assert_equal ( "\ttwo two two" , getline ( 2 )) 1260 1261" :sgl 1262call setline ( 2 , "\tone one one" ) 1263redir => output 1264sgl 1265redir END 1266call assert_equal ( "^Itwo two two$" , output ->split ( "\n" )[ -1 ]) 1267call assert_equal ( "\ttwo two two" , getline ( 2 )) 1268 1269" :sgr 1270call setline ( 2 , "one one one" ) 1271call cursor ( 2 , 1 ) 1272s /abc/xyz/e 1273let @/= 'one' 1274sgr 1275call assert_equal ( 'xyz xyz xyz' , getline ( 2 )) 1276 1277" :sic 1278call cursor ( 1 , 1 ) 1279s /one/two/e 1280call setline ( 2 , "ONE One one" ) 1281call cursor ( 2 , 1 ) 1282call feedkeys ( ":sic\<CR>y" , 'xt' ) 1283call assert_equal ( 'two One one' , getline ( 2 )) 1284 1285" :si 1286call setline ( 2 , "ONE One one" ) 1287si 1288call assert_equal ( 'two One one' , getline ( 2 )) 1289 1290" :siI 1291call setline ( 2 , "ONE One one" ) 1292siI 1293call assert_equal ( 'ONE One two' , getline ( 2 )) 1294 1295" :sin 1296call setline ( 2 , 'ONE One onE' ) 1297let t = execute ( 'sin' ) ->split ( "\n" ) 1298call assert_equal ([ '1 match on 1 line' ], t ) 1299call assert_equal ( 'ONE One onE' , getline ( 2 )) 1300 1301" :sip 1302call setline ( 2 , "\tONE One onE" ) 1303redir => output 1304sip 1305redir END 1306call assert_equal ( ' two One onE' , output ->split ( "\n" )[ -1 ]) 1307call assert_equal ( "\ttwo One onE" , getline ( 2 )) 1308 1309" :sir 1310call setline ( 2 , "ONE One onE" ) 1311call cursor ( 2 , 1 ) 1312s /abc/xyz/e 1313let @/= 'one' 1314sir 1315call assert_equal ( 'xyz One onE' , getline ( 2 )) 1316 1317" :sIc 1318call cursor ( 1 , 1 ) 1319s /one/two/e 1320call setline ( 2 , "ONE One one" ) 1321call cursor ( 2 , 1 ) 1322call feedkeys ( ":sIc\<CR>y" , 'xt' ) 1323call assert_equal ( 'ONE One two' , getline ( 2 )) 1324 1325" :sIg 1326call setline ( 2 , "ONE one onE one" ) 1327sIg 1328call assert_equal ( 'ONE two onE two' , getline ( 2 )) 1329 1330" :sIi 1331call setline ( 2 , "ONE One one" ) 1332sIi 1333call assert_equal ( 'two One one' , getline ( 2 )) 1334 1335" :sI 1336call setline ( 2 , "ONE One one" ) 1337sI 1338call assert_equal ( 'ONE One two' , getline ( 2 )) 1339 1340" :sIn 1341call setline ( 2 , 'ONE One one' ) 1342let t = execute ( 'sIn' ) ->split ( "\n" ) 1343call assert_equal ([ '1 match on 1 line' ], t ) 1344call assert_equal ( 'ONE One one' , getline ( 2 )) 1345 1346" :sIp 1347call setline ( 2 , "\tONE One one" ) 1348redir => output 1349sIp 1350redir END 1351call assert_equal ( ' ONE One two' , output ->split ( "\n" )[ -1 ]) 1352call assert_equal ( "\tONE One two" , getline ( 2 )) 1353 1354" :sIl 1355call setline ( 2 , "\tONE onE one" ) 1356redir => output 1357sIl 1358redir END 1359call assert_equal ( "^IONE onE two$" , output ->split ( "\n" )[ -1 ]) 1360call assert_equal ( "\tONE onE two" , getline ( 2 )) 1361 1362" :sIr 1363call setline ( 2 , "ONE one onE" ) 1364call cursor ( 2 , 1 ) 1365s /abc/xyz/e 1366let @/= 'one' 1367sIr 1368call assert_equal ( 'ONE xyz onE' , getline ( 2 )) 1369 1370" :src 1371call setline ( 2 , "ONE one one" ) 1372call cursor ( 2 , 1 ) 1373s /abc/xyz/e 1374let @/= 'one' 1375call feedkeys ( ":src\<CR>y" , 'xt' ) 1376call assert_equal ( 'ONE xyz one' , getline ( 2 )) 1377 1378" :srg 1379call setline ( 2 , "one one one" ) 1380call cursor ( 2 , 1 ) 1381s /abc/xyz/e 1382let @/= 'one' 1383srg 1384call assert_equal ( 'xyz xyz xyz' , getline ( 2 )) 1385 1386" :sri 1387call setline ( 2 , "ONE one onE" ) 1388call cursor ( 2 , 1 ) 1389s /abc/xyz/e 1390let @/= 'one' 1391sri 1392call assert_equal ( 'xyz one onE' , getline ( 2 )) 1393 1394" :srI 1395call setline ( 2 , "ONE one onE" ) 1396call cursor ( 2 , 1 ) 1397s /abc/xyz/e 1398let @/= 'one' 1399srI 1400call assert_equal ( 'ONE xyz onE' , getline ( 2 )) 1401 1402" :srn 1403call setline ( 2 , "ONE one onE" ) 1404call cursor ( 2 , 1 ) 1405s /abc/xyz/e 1406let @/= 'one' 1407let t = execute ( 'srn' ) ->split ( "\n" ) 1408call assert_equal ([ '1 match on 1 line' ], t ) 1409call assert_equal ( 'ONE one onE' , getline ( 2 )) 1410 1411" :srp 1412call setline ( 2 , "\tONE one onE" ) 1413call cursor ( 2 , 1 ) 1414s /abc/xyz/e 1415let @/= 'one' 1416redir => output 1417srp 1418redir END 1419call assert_equal ( ' ONE xyz onE' , output ->split ( "\n" )[ -1 ]) 1420call assert_equal ( "\tONE xyz onE" , getline ( 2 )) 1421 1422" :srl 1423call setline ( 2 , "\tONE one onE" ) 1424call cursor ( 2 , 1 ) 1425s /abc/xyz/e 1426let @/= 'one' 1427redir => output 1428srl 1429redir END 1430call assert_equal ( "^IONE xyz onE$" , output ->split ( "\n" )[ -1 ]) 1431call assert_equal ( "\tONE xyz onE" , getline ( 2 )) 1432 1433" :sr 1434call setline ( 2 , "ONE one onE" ) 1435call cursor ( 2 , 1 ) 1436s /abc/xyz/e 1437let @/= 'one' 1438sr 1439call assert_equal ( 'ONE xyz onE' , getline ( 2 )) 1440 1441" :sce 1442s /abc/xyz/e 1443call assert_fails ( "sc" , 'E486:' ) 1444sce 1445" :sge 1446call assert_fails ( "sg" , 'E486:' ) 1447sge 1448" :sie 1449call assert_fails ( "si" , 'E486:' ) 1450sie 1451" :sIe 1452call assert_fails ( "sI" , 'E486:' ) 1453sIe 1454 1455bw ! 1456endfunc 1457 1458" Check handling expanding "~" resulting in extremely long text. 1459" FIXME: disabled, it takes too long to run on CI 1460"func Test_substitute_tilde_too_long() 1461" enew! 1462" 1463" s/.*/ixxx 1464" s//~~~~~~~~~AAAAAAA@( 1465" 1466" " Either fails with "out of memory" or "text too long". 1467" " This can take a long time. 1468" call assert_fails('sil! norm &&&&&&&&&', ['E1240:\|E342:']) 1469" 1470" bwipe! 1471"endfunc 1472 1473" This should be done last to reveal a memory leak when vim_regsub_both() is 1474" called to evaluate an expression but it is not used in a second call. 1475func Test_z_substitute_expr_leak () 1476func SubExpr () 1477 ~n 1478endfunc 1479silent ! s /\%')/\=SubExpr() 1480delfunc SubExpr 1481endfunc 1482 1483func Test_substitute_expr_switch_win () 1484func R () 1485wincmd x 1486return 'XXXX' 1487endfunc 1488new Xfoobar 1489let bufnr = bufnr ( '%' ) 1490put ='abcdef' 1491silent ! s /\%')/\=R() 1492call assert_fails ( ':%s/./\=R()/g' , 'E565:' ) 1493delfunc R 1494exe bufnr .. "bw!" 1495endfunc 1496 1497" recursive call of :s using test-replace special 1498func Test_substitute_expr_recursive () 1499func Q () 1500 %s /./\='foobar'/gn 1501return "foobar" 1502endfunc 1503func R () 1504 %s /./\=Q()/g 1505endfunc 1506new Xfoobar_UAF 1507let bufnr = bufnr ( '%' ) 1508put ='abcdef' 1509silent ! s /./\=R()/g 1510call assert_fails ( ':%s/./\=R()/g' , 'E565:' ) 1511delfunc R 1512delfunc Q 1513exe bufnr .. "bw!" 1514endfunc 1515 1516" Test for changing 'cpo' in a substitute expression 1517func Test_substitute_expr_cpo () 1518func XSubExpr () 1519set cpo = 1520return 'x' 1521endfunc 1522 1523let save_cpo = & cpo 1524call assert_equal ( 'xxx' , substitute ( 'abc' , '.' , '\=XSubExpr()' , 'g' )) 1525call assert_equal ( save_cpo , & cpo ) 1526 1527delfunc XSubExpr 1528endfunc 1529 1530" vim: shiftwidth=2 sts=2 expandtab