blob: 1f4cb6a65503f2908f5dc9c44a5b05294be77f1d [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for :global and :vglobal
2
Bram Moolenaar07188fc2020-06-05 20:03:16 +02003source check.vim
zeertzjq3cfae392022-07-26 17:48:13 +01004source term_util.vim
Bram Moolenaar3fcfa352017-03-29 19:20:41 +02005
6func Test_yank_put_clipboard()
7 new
8 call setline(1, ['a', 'b', 'c'])
9 set clipboard=unnamed
10 g/^/normal yyp
11 call assert_equal(['a', 'a', 'b', 'b', 'c', 'c'], getline(1, 6))
Bram Moolenaar515545e2020-03-22 14:08:59 +010012 set clipboard=unnamed,unnamedplus
13 call setline(1, ['a', 'b', 'c'])
14 g/^/normal yyp
15 call assert_equal(['a', 'a', 'b', 'b', 'c', 'c'], getline(1, 6))
Bram Moolenaar3fcfa352017-03-29 19:20:41 +020016 set clipboard&
17 bwipe!
18endfunc
Bram Moolenaarf84b1222017-06-10 14:29:52 +020019
Bram Moolenaar07188fc2020-06-05 20:03:16 +020020func Test_global_set_clipboard()
21 CheckFeature clipboard_working
22 new
23 set clipboard=unnamedplus
24 let @+='clipboard' | g/^/set cb= | let @" = 'unnamed' | put
25 call assert_equal(['','unnamed'], getline(1, '$'))
26 set clipboard&
27 bwipe!
28endfunc
29
Bram Moolenaarf84b1222017-06-10 14:29:52 +020030func Test_nested_global()
31 new
32 call setline(1, ['nothing', 'found', 'found bad', 'bad'])
Bram Moolenaare2e40752020-09-04 21:18:46 +020033 call assert_fails('g/found/3v/bad/s/^/++/', 'E147:')
Bram Moolenaarf84b1222017-06-10 14:29:52 +020034 g/found/v/bad/s/^/++/
35 call assert_equal(['nothing', '++found', 'found bad', 'bad'], getline(1, 4))
36 bwipe!
37endfunc
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010038
39func Test_global_error()
40 call assert_fails('g\\a', 'E10:')
41 call assert_fails('g', 'E148:')
Bram Moolenaar9b7bf9e2020-07-11 22:14:59 +020042 call assert_fails('g/\(/y', 'E54:')
Bram Moolenaar5d98dc22020-01-29 21:57:34 +010043endfunc
44
Bram Moolenaarea3db912020-02-02 15:32:13 +010045" Test for printing lines using :g with different search patterns
46func Test_global_print()
47 new
48 call setline(1, ['foo', 'bar', 'foo', 'foo'])
49 let @/ = 'foo'
50 let t = execute("g/")->trim()->split("\n")
51 call assert_equal(['foo', 'foo', 'foo'], t)
52
53 " Test for Vi compatible patterns
54 let @/ = 'bar'
55 let t = execute('g\/')->trim()->split("\n")
56 call assert_equal(['bar'], t)
57
58 normal gg
59 s/foo/foo/
60 let t = execute('g\&')->trim()->split("\n")
61 call assert_equal(['foo', 'foo', 'foo'], t)
62
63 let @/ = 'bar'
64 let t = execute('g?')->trim()->split("\n")
65 call assert_equal(['bar'], t)
66
67 " Test for the 'Pattern found in every line' message
68 let v:statusmsg = ''
69 v/foo\|bar/p
70 call assert_notequal('', v:statusmsg)
71
Bram Moolenaar24d9c052022-03-04 21:34:31 +000072 " In Vim9 script this is an error
73 let caught = 'no'
74 try
75 vim9cmd v/foo\|bar/p
76 catch /E538/
77 let caught = 'yes'
78 call assert_match('E538: Pattern found in every line: foo\|bar', v:exception)
79 endtry
80 call assert_equal('yes', caught)
81
82 " In Vim9 script not matching is an error
83 let caught = 'no'
84 try
85 vim9cmd g/foobarnotfound/p
86 catch /E486/
87 let caught = 'yes'
88 call assert_match('E486: Pattern not found: foobarnotfound', v:exception)
89 endtry
90 call assert_equal('yes', caught)
91
Bram Moolenaarea3db912020-02-02 15:32:13 +010092 close!
93endfunc
94
Bram Moolenaar818fc9a2020-02-21 17:54:45 +010095" Test for global command with newline character
96func Test_global_newline()
97 new
98 call setline(1, ['foo'])
99 exe "g/foo/s/f/h/\<NL>s/o$/w/"
100 call assert_equal('how', getline(1))
101 call setline(1, ["foo\<NL>bar"])
102 exe "g/foo/s/foo\\\<NL>bar/xyz/"
103 call assert_equal('xyz', getline(1))
104 close!
105endfunc
106
Bram Moolenaar419a40a2021-06-21 21:55:18 +0200107func Test_wrong_delimiter()
108 call assert_fails('g x^bxd', 'E146:')
109endfunc
110
zeertzjq3cfae392022-07-26 17:48:13 +0100111" Test for interrupting :global using Ctrl-C
112func Test_interrupt_global()
113 CheckRunVimInTerminal
Bram Moolenaar859ea4b2022-09-27 18:05:38 +0100114
zeertzjq3cfae392022-07-26 17:48:13 +0100115 let lines =<< trim END
116 cnoremap ; <Cmd>sleep 10<CR>
117 call setline(1, repeat(['foo'], 5))
118 END
119 call writefile(lines, 'Xtest_interrupt_global')
120 let buf = RunVimInTerminal('-S Xtest_interrupt_global', {'rows': 6})
121
122 call term_sendkeys(buf, ":g/foo/norm :\<C-V>;\<CR>")
123 " Wait for :sleep to start
Bram Moolenaar859ea4b2022-09-27 18:05:38 +0100124 call TermWait(buf, 100)
zeertzjq3cfae392022-07-26 17:48:13 +0100125 call term_sendkeys(buf, "\<C-C>")
126 call WaitForAssert({-> assert_match('Interrupted', term_getline(buf, 6))}, 1000)
127
128 " Also test in Ex mode
129 call term_sendkeys(buf, "gQg/foo/norm :\<C-V>;\<CR>")
130 " Wait for :sleep to start
Bram Moolenaar859ea4b2022-09-27 18:05:38 +0100131 call TermWait(buf, 100)
zeertzjq3cfae392022-07-26 17:48:13 +0100132 call term_sendkeys(buf, "\<C-C>")
133 call WaitForAssert({-> assert_match('Interrupted', term_getline(buf, 5))}, 1000)
134
135 call StopVimInTerminal(buf)
136 call delete('Xtest_interrupt_global')
137endfunc
138
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100139" vim: shiftwidth=2 sts=2 expandtab