blob: 0f72c3cf80cd733a8bf08a0a65045cfa9d616cf9 [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
Rob Pillinge86190e2022-12-23 19:06:04 +000095func Test_global_empty_pattern()
96 " populate history
97 silent g/hello/
98
99 redir @a
100 g//
101 redir END
102
103 call assert_match('Pattern not found: hello', @a)
104 " ^~~~~ this was previously empty
105endfunc
106
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100107" Test for global command with newline character
108func Test_global_newline()
109 new
110 call setline(1, ['foo'])
111 exe "g/foo/s/f/h/\<NL>s/o$/w/"
112 call assert_equal('how', getline(1))
113 call setline(1, ["foo\<NL>bar"])
114 exe "g/foo/s/foo\\\<NL>bar/xyz/"
115 call assert_equal('xyz', getline(1))
116 close!
117endfunc
118
zeertzjq30741372024-05-24 07:37:36 +0200119" Test :g with ? as delimiter.
120func Test_global_question_delimiter()
121 new
122 call setline(1, ['aaaaa', 'b?bbb', 'ccccc', 'ddd?d', 'eeeee'])
123 g?\??delete
124 call assert_equal(['aaaaa', 'ccccc', 'eeeee'], getline(1, '$'))
125 bwipe!
126endfunc
127
128func Test_global_wrong_delimiter()
Bram Moolenaar419a40a2021-06-21 21:55:18 +0200129 call assert_fails('g x^bxd', 'E146:')
130endfunc
131
zeertzjq3cfae392022-07-26 17:48:13 +0100132" Test for interrupting :global using Ctrl-C
133func Test_interrupt_global()
134 CheckRunVimInTerminal
Bram Moolenaar859ea4b2022-09-27 18:05:38 +0100135
zeertzjq3cfae392022-07-26 17:48:13 +0100136 let lines =<< trim END
137 cnoremap ; <Cmd>sleep 10<CR>
138 call setline(1, repeat(['foo'], 5))
139 END
Bram Moolenaar572a4432022-09-28 21:07:03 +0100140 call writefile(lines, 'Xtest_interrupt_global', 'D')
zeertzjq3cfae392022-07-26 17:48:13 +0100141 let buf = RunVimInTerminal('-S Xtest_interrupt_global', {'rows': 6})
142
143 call term_sendkeys(buf, ":g/foo/norm :\<C-V>;\<CR>")
144 " Wait for :sleep to start
Bram Moolenaar859ea4b2022-09-27 18:05:38 +0100145 call TermWait(buf, 100)
zeertzjq3cfae392022-07-26 17:48:13 +0100146 call term_sendkeys(buf, "\<C-C>")
147 call WaitForAssert({-> assert_match('Interrupted', term_getline(buf, 6))}, 1000)
148
149 " Also test in Ex mode
150 call term_sendkeys(buf, "gQg/foo/norm :\<C-V>;\<CR>")
151 " Wait for :sleep to start
Bram Moolenaar859ea4b2022-09-27 18:05:38 +0100152 call TermWait(buf, 100)
zeertzjq3cfae392022-07-26 17:48:13 +0100153 call term_sendkeys(buf, "\<C-C>")
154 call WaitForAssert({-> assert_match('Interrupted', term_getline(buf, 5))}, 1000)
155
156 call StopVimInTerminal(buf)
zeertzjq3cfae392022-07-26 17:48:13 +0100157endfunc
158
Bram Moolenaar5d98dc22020-01-29 21:57:34 +0100159" vim: shiftwidth=2 sts=2 expandtab