blob: a1ec15d7e252daf2c4f5d4336191160362f58d64 [file] [log] [blame]
Bram Moolenaar59cb0412019-12-18 22:26:31 +01001" Test editing line in Ex mode (see :help Q and :help gQ).
2
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01003source check.vim
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +01004source shared.vim
Bram Moolenaare20b9ec2020-02-03 21:40:04 +01005
Bram Moolenaar59cb0412019-12-18 22:26:31 +01006" Helper function to test editing line in Q Ex mode
7func Ex_Q(cmd)
8 " Is there a simpler way to test editing Ex line?
9 call feedkeys("Q"
10 \ .. "let s:test_ex =<< END\<CR>"
11 \ .. a:cmd .. "\<CR>"
12 \ .. "END\<CR>"
13 \ .. "visual\<CR>", 'tx')
14 return s:test_ex[0]
15endfunc
16
17" Helper function to test editing line in gQ Ex mode
18func Ex_gQ(cmd)
19 call feedkeys("gQ" .. a:cmd .. "\<C-b>\"\<CR>", 'tx')
20 let ret = @:[1:] " Remove leading quote.
21 call feedkeys("visual\<CR>", 'tx')
22 return ret
23endfunc
24
25" Helper function to test editing line with both Q and gQ Ex mode.
26func Ex(cmd)
27 return [Ex_Q(a:cmd), Ex_gQ(a:cmd)]
28endfunc
29
30" Test editing line in Ex mode (both Q and gQ)
31func Test_ex_mode()
32 let encoding_save = &encoding
33 set sw=2
34
35 for e in ['utf8', 'latin1']
36 exe 'set encoding=' . e
37
38 call assert_equal(['bar', 'bar'], Ex("foo bar\<C-u>bar"), e)
39 call assert_equal(["1\<C-u>2", "1\<C-u>2"], Ex("1\<C-v>\<C-u>2"), e)
40 call assert_equal(["1\<C-b>2\<C-e>3", '213'], Ex("1\<C-b>2\<C-e>3"), e)
41 call assert_equal(['0123', '2013'], Ex("01\<Home>2\<End>3"), e)
42 call assert_equal(['0123', '0213'], Ex("01\<Left>2\<Right>3"), e)
43 call assert_equal(['01234', '0342'], Ex("012\<Left>\<Left>\<Insert>3\<Insert>4"), e)
44 call assert_equal(["foo bar\<C-w>", 'foo '], Ex("foo bar\<C-w>"), e)
45 call assert_equal(['foo', 'foo'], Ex("fooba\<Del>\<Del>"), e)
46 call assert_equal(["foo\tbar", 'foobar'], Ex("foo\<Tab>bar"), e)
47 call assert_equal(["abbrev\t", 'abbreviate'], Ex("abbrev\<Tab>"), e)
48 call assert_equal([' 1', "1\<C-t>\<C-t>"], Ex("1\<C-t>\<C-t>"), e)
49 call assert_equal([' 1', "1\<C-t>\<C-t>"], Ex("1\<C-t>\<C-t>\<C-d>"), e)
50 call assert_equal([' foo', ' foo'], Ex(" foo\<C-d>"), e)
51 call assert_equal(['foo', ' foo0'], Ex(" foo0\<C-d>"), e)
52 call assert_equal(['foo', ' foo^'], Ex(" foo^\<C-d>"), e)
Bram Moolenaar0546d7d2020-03-01 16:53:09 +010053 call assert_equal(['foo', 'foo'],
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +010054 \ Ex("\<BS>\<C-H>\<Del>\<kDel>foo"), e)
55 " default wildchar <Tab> interferes with this test
56 set wildchar=<c-e>
57 call assert_equal(["a\tb", "a\tb"], Ex("a\t\t\<C-H>b"), e)
Bram Moolenaarbd7206e2020-03-06 20:36:04 +010058 call assert_equal(["\t mn", "\tm\<C-T>n"], Ex("\tm\<C-T>n"), e)
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +010059 set wildchar&
Bram Moolenaar59cb0412019-12-18 22:26:31 +010060 endfor
61
62 set sw&
63 let &encoding = encoding_save
64endfunc
Bram Moolenaare20b9ec2020-02-03 21:40:04 +010065
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +010066" Test substitute confirmation prompt :%s/pat/str/c in Ex mode
Bram Moolenaare20b9ec2020-02-03 21:40:04 +010067func Test_Ex_substitute()
68 CheckRunVimInTerminal
69 let buf = RunVimInTerminal('', {'rows': 6})
70
zeertzjq7d664bf2024-07-14 10:22:54 +020071 call term_sendkeys(buf, ":call setline(1, repeat(['foo foo'], 4))\<CR>")
Bram Moolenaare20b9ec2020-02-03 21:40:04 +010072 call term_sendkeys(buf, ":set number\<CR>")
73 call term_sendkeys(buf, "gQ")
74 call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000)
75
76 call term_sendkeys(buf, "%s/foo/bar/gc\<CR>")
77 call WaitForAssert({-> assert_match(' 1 foo foo', term_getline(buf, 5))},
78 \ 1000)
79 call WaitForAssert({-> assert_match(' ^^^', term_getline(buf, 6))}, 1000)
Bram Moolenaare5b0b982021-04-07 19:42:57 +020080 call term_sendkeys(buf, "N\<CR>")
81 call term_wait(buf)
82 call WaitForAssert({-> assert_match(' ^^^', term_getline(buf, 6))}, 1000)
Bram Moolenaare20b9ec2020-02-03 21:40:04 +010083 call term_sendkeys(buf, "n\<CR>")
84 call WaitForAssert({-> assert_match(' ^^^', term_getline(buf, 6))},
85 \ 1000)
86 call term_sendkeys(buf, "y\<CR>")
87
88 call term_sendkeys(buf, "q\<CR>")
89 call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000)
90
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +010091 " Pressing enter in ex mode should print the current line
92 call term_sendkeys(buf, "\<CR>")
zeertzjq7d664bf2024-07-14 10:22:54 +020093 call WaitForAssert({-> assert_match(' 3 foo foo', term_getline(buf, 5))}, 1000)
94 call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000)
95
96 " The printed line should overwrite the colon
97 call term_sendkeys(buf, "\<CR>")
98 call WaitForAssert({-> assert_match(' 3 foo foo', term_getline(buf, 4))}, 1000)
99 call WaitForAssert({-> assert_match(' 4 foo foo', term_getline(buf, 5))}, 1000)
100 call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000)
Bram Moolenaarbc2b71d2020-02-17 21:33:30 +0100101
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100102 call term_sendkeys(buf, ":vi\<CR>")
103 call WaitForAssert({-> assert_match('foo bar', term_getline(buf, 1))}, 1000)
104
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100105 call StopVimInTerminal(buf)
106endfunc
107
Bram Moolenaar9f6277b2020-02-11 22:04:02 +0100108" Test for displaying lines from an empty buffer in Ex mode
109func Test_Ex_emptybuf()
110 new
111 call assert_fails('call feedkeys("Q\<CR>", "xt")', 'E749:')
112 call setline(1, "abc")
113 call assert_fails('call feedkeys("Q\<CR>", "xt")', 'E501:')
114 call assert_fails('call feedkeys("Q%d\<CR>", "xt")', 'E749:')
115 close!
116endfunc
117
118" Test for the :open command
119func Test_open_command()
120 new
121 call setline(1, ['foo foo', 'foo bar', 'foo baz'])
122 call feedkeys("Qopen\<CR>j", 'xt')
123 call assert_equal('foo bar', getline('.'))
124 call feedkeys("Qopen /bar/\<CR>", 'xt')
125 call assert_equal(5, col('.'))
126 call assert_fails('call feedkeys("Qopen /baz/\<CR>", "xt")', 'E479:')
127 close!
128endfunc
129
Bram Moolenaare031fe92021-12-05 12:06:24 +0000130func Test_open_command_flush_line()
131 " this was accessing freed memory: the regexp match uses a pointer to the
132 " current line which becomes invalid when searching for the ') mark.
133 new
134 call setline(1, ['one', 'two. three'])
135 s/one/ONE
136 try
137 open /\%')/
138 catch /E479/
139 endtry
140 bwipe!
141endfunc
142
Bram Moolenaar7ac50232023-03-07 21:05:04 +0000143" FIXME: this doesn't fail without the fix but hangs
144func Skip_Test_open_command_state()
145 " Tricky script that failed because State was not set properly
146 let lines =<< trim END
147 !ls ƒ
148 0scìi
149 so! Xsourced
150 set t_û0=0
151 v/-/o
152 END
153 call writefile(lines, 'XopenScript', '')
154
155 let sourced = ["!f\u0083\x02\<Esc>z=0"]
156 call writefile(sourced, 'Xsourced', 'b')
157
158 CheckRunVimInTerminal
159 let buf = RunVimInTerminal('-u NONE -i NONE -n -m -X -Z -e -s -S XopenScript -c qa!', #{rows: 6, wait_for_ruler: 0, no_clean: 1})
160 sleep 3
161
162 call StopVimInTerminal(buf)
163endfunc
164
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100165" Test for :g/pat/visual to run vi commands in Ex mode
166" This used to hang Vim before 8.2.0274.
167func Test_Ex_global()
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +0100168 new
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100169 call setline(1, ['', 'foo', 'bar', 'foo', 'bar', 'foo'])
Bram Moolenaar578fe942020-02-27 21:32:51 +0100170 call feedkeys("Q\<bs>g/bar/visual\<CR>$rxQ$ryQvisual\<CR>j", "xt")
Bram Moolenaar818fc9a2020-02-21 17:54:45 +0100171 call assert_equal('bax', getline(3))
172 call assert_equal('bay', getline(5))
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +0100173 bwipe!
Mohamed Akramf3daa452024-07-06 17:12:09 +0200174
175 new
176 call setline(1, ['foo', 'bar'])
177 call feedkeys("Qg/./i\\\na\\\n.\\\na\\\nb\\\n.", "xt")
178 call assert_equal(['a', 'b', 'foo', 'a', 'b', 'bar'], getline(1, '$'))
179 bwipe!
180endfunc
181
182func Test_Ex_shell()
183 CheckUnix
184
185 new
186 call feedkeys("Qr !echo foo\\\necho bar\n", 'xt')
187 call assert_equal(['', 'foo', 'bar'], getline(1, '$'))
188 bwipe!
189
190 new
191 call feedkeys("Qr !echo foo\\\\\nbar\n", 'xt')
192 call assert_equal(['', 'foobar'], getline(1, '$'))
193 bwipe!
194
195 new
196 call feedkeys("Qr !echo foo\\ \\\necho bar\n", 'xt')
197 call assert_equal(['', 'foo ', 'bar'], getline(1, '$'))
198 bwipe!
199
200 new
201 call setline(1, ['bar', 'baz'])
202 call feedkeys("Qg/./!echo \\\ns/b/c/", "xt")
203 call assert_equal(['car', 'caz'], getline(1, '$'))
204 bwipe!
Bram Moolenaar9e2bcb52020-02-18 21:33:00 +0100205endfunc
206
zeertzjqf754fe62022-07-14 17:06:12 +0100207" Test for pressing Ctrl-C in :append inside a loop in Ex mode
208" This used to hang Vim
209func Test_Ex_append_in_loop()
210 CheckRunVimInTerminal
211 let buf = RunVimInTerminal('', {'rows': 6})
212
213 call term_sendkeys(buf, "gQ")
214 call term_sendkeys(buf, "for i in range(1)\<CR>")
215 call term_sendkeys(buf, "append\<CR>")
216 call WaitForAssert({-> assert_match(': append', term_getline(buf, 5))}, 1000)
217 call term_sendkeys(buf, "\<C-C>")
zeertzjq3cfae392022-07-26 17:48:13 +0100218 " Wait for input to be flushed
zeertzjqf754fe62022-07-14 17:06:12 +0100219 call term_wait(buf)
220 call term_sendkeys(buf, "foo\<CR>")
221 call WaitForAssert({-> assert_match('foo', term_getline(buf, 5))}, 1000)
222 call term_sendkeys(buf, ".\<CR>")
223 call WaitForAssert({-> assert_match('.', term_getline(buf, 5))}, 1000)
224 call term_sendkeys(buf, "endfor\<CR>")
225 call term_sendkeys(buf, "vi\<CR>")
226 call WaitForAssert({-> assert_match('foo', term_getline(buf, 1))}, 1000)
227
228 call StopVimInTerminal(buf)
229endfunc
230
Bram Moolenaar578fe942020-02-27 21:32:51 +0100231" In Ex-mode, a backslash escapes a newline
232func Test_Ex_escape_enter()
233 call feedkeys("gQlet l = \"a\\\<kEnter>b\"\<cr>vi\<cr>", 'xt')
234 call assert_equal("a\rb", l)
235endfunc
236
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100237" Test for :append! command in Ex mode
238func Test_Ex_append()
239 new
240 call setline(1, "\t abc")
241 call feedkeys("Qappend!\npqr\nxyz\n.\nvisual\n", 'xt')
242 call assert_equal(["\t abc", "\t pqr", "\t xyz"], getline(1, '$'))
243 close!
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100244
Mohamed Akramf3daa452024-07-06 17:12:09 +0200245 new
246 call feedkeys("Qappend\na\\\n.", 'xt')
247 call assert_equal(['a\'], getline(1, '$'))
248 close!
Bram Moolenaar91ffc8a2020-03-02 20:54:22 +0100249endfunc
250
Bram Moolenaar1671f442020-03-10 07:48:13 +0100251func Test_ex_mode_errors()
252 " Not allowed to enter ex mode when text is locked
253 au InsertCharPre <buffer> normal! gQ<CR>
Bram Moolenaarff06f282020-04-21 22:01:14 +0200254 let caught_e565 = 0
Bram Moolenaar1671f442020-03-10 07:48:13 +0100255 try
256 call feedkeys("ix\<esc>", 'xt')
Bram Moolenaarff06f282020-04-21 22:01:14 +0200257 catch /^Vim\%((\a\+)\)\=:E565/ " catch E565
258 let caught_e565 = 1
Bram Moolenaar1671f442020-03-10 07:48:13 +0100259 endtry
Bram Moolenaarff06f282020-04-21 22:01:14 +0200260 call assert_equal(1, caught_e565)
Bram Moolenaar1671f442020-03-10 07:48:13 +0100261 au! InsertCharPre
Bram Moolenaar158ea172020-06-18 17:28:39 +0200262
263 new
264 au CmdLineEnter * call ExEnterFunc()
265 func ExEnterFunc()
266
267 endfunc
268 call feedkeys("gQvi\r", 'xt')
269
270 au! CmdLineEnter
271 delfunc ExEnterFunc
Christian Brabandt590aae32023-06-25 22:34:22 +0100272
273 au CmdlineEnter * :
274 call feedkeys("gQecho 1\r", 'xt')
275
276 au! CmdlineEnter
277
Bram Moolenaar158ea172020-06-18 17:28:39 +0200278 quit
Bram Moolenaar1671f442020-03-10 07:48:13 +0100279endfunc
280
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100281func Test_ex_mode_with_global()
Bram Moolenaar399db042020-11-01 22:31:08 +0100282 CheckNotGui
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100283 CheckFeature timers
284
285 " This will get stuck in Normal mode after the failed "J", use a timer to
286 " get going again.
287 let lines =<< trim END
288 call ch_logfile('logfile', 'w')
289 pedit
290 func FeedQ(id)
291 call feedkeys('Q', 't')
292 endfunc
293 call timer_start(10, 'FeedQ')
294 g/^/vi|HJ
295 call writefile(['done'], 'Xdidexmode')
296 qall!
297 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100298 call writefile(lines, 'Xexmodescript', 'D')
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100299 call assert_equal(1, RunVim([], [], '-e -s -S Xexmodescript'))
300 call assert_equal(['done'], readfile('Xdidexmode'))
301
Bram Moolenaarf637bce2020-11-23 18:14:56 +0100302 call delete('logfile')
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100303 call delete('Xdidexmode')
Bram Moolenaar3b6d57f2020-11-01 21:56:40 +0100304endfunc
305
Bram Moolenaar1d859e22021-01-28 17:24:58 +0100306func Test_ex_mode_count_overflow()
Bram Moolenaar97202d92021-01-28 18:34:35 +0100307 " The multiplication causes an integer overflow
308 CheckNotAsan
309
Bram Moolenaar1d859e22021-01-28 17:24:58 +0100310 " this used to cause a crash
311 let lines =<< trim END
312 call feedkeys("\<Esc>Q\<CR>")
313 v9|9silent! vi|333333233333y32333333%O
314 call writefile(['done'], 'Xdidexmode')
315 qall!
316 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100317 call writefile(lines, 'Xexmodescript', 'D')
Bram Moolenaar1d859e22021-01-28 17:24:58 +0100318 call assert_equal(1, RunVim([], [], '-e -s -S Xexmodescript -c qa'))
319 call assert_equal(['done'], readfile('Xdidexmode'))
320
321 call delete('Xdidexmode')
Bram Moolenaar1d859e22021-01-28 17:24:58 +0100322endfunc
323
Bram Moolenaar85b67472022-01-25 11:55:02 +0000324func Test_ex_mode_large_indent()
325 new
326 set ts=500 ai
327 call setline(1, "\t")
328 exe "normal gQi\<CR>."
329 set ts=8 noai
330 bwipe!
331endfunc
332
Bram Moolenaarf50808e2022-04-16 18:52:17 +0100333" This was accessing illegal memory when using "+" for eap->cmd.
334func Test_empty_command_visual_mode()
335 let lines =<< trim END
336 r<sfile>
337 0norm0V:
338 :qall!
339 END
Bram Moolenaar5c645a22022-09-21 22:00:03 +0100340 call writefile(lines, 'Xexmodescript', 'D')
Bram Moolenaarf50808e2022-04-16 18:52:17 +0100341 call assert_equal(1, RunVim([], [], '-u NONE -e -s -S Xexmodescript'))
342
Bram Moolenaar217ea512022-06-14 17:13:59 +0100343 " This may cause a dialog to be displayed for an empty command, ignore it.
344 call delete('guidialogfile')
Bram Moolenaarf50808e2022-04-16 18:52:17 +0100345endfunc
346
Mohamed Akramf3daa452024-07-06 17:12:09 +0200347" Test using backslash in ex-mode
348func Test_backslash_multiline()
349 new
350 call setline(1, 'enum')
351 call feedkeys('Qg/enum/i\ \ .', "xt")
352 call assert_equal(["", "enum"], getline(1, 2))
353endfunc
Bram Moolenaar85b67472022-01-25 11:55:02 +0000354
Christian Brabandt248efab2024-07-07 19:44:16 +0200355" Test using backslash in ex-mode after patch 9.1.0535
356func Test_backslash_multiline2()
357 new
358 call feedkeys('Qa X \\ Y .', "xt")
359 call assert_equal(['X \\', "Y"], getline(1, 2))
360endfunc
361
Mohamed Akramc25a7082024-07-12 20:17:55 +0200362" Testing implicit print command
363func Test_implicit_print()
364 new
365 call setline(1, ['one', 'two', 'three'])
366 call feedkeys('Q:let a=execute(":1,2")', 'xt')
367 call feedkeys('Q:let b=execute(":3")', 'xt')
368 call assert_equal('one two', a->split('\n')->join(' '))
369 call assert_equal('three', b->split('\n')->join(' '))
370 bw!
371endfunc
372
Mohamed Akram8c446da2024-07-13 18:49:55 +0200373" Test inserting text after the trailing bar
374func Test_insert_after_trailing_bar()
375 new
376 call feedkeys("Qi|\nfoo\n.\na|bar\nbar\n.\nc|baz\n.", "xt")
377 call assert_equal(['', 'foo', 'bar', 'baz'], getline(1, '$'))
378 bwipe!
379endfunc
380
Bram Moolenaare20b9ec2020-02-03 21:40:04 +0100381" vim: shiftwidth=2 sts=2 expandtab