Bram Moolenaar | 59cb041 | 2019-12-18 22:26:31 +0100 | [diff] [blame] | 1 | " Test editing line in Ex mode (see :help Q and :help gQ). |
| 2 | |
Bram Moolenaar | e20b9ec | 2020-02-03 21:40:04 +0100 | [diff] [blame] | 3 | source check.vim |
Bram Moolenaar | 3b6d57f | 2020-11-01 21:56:40 +0100 | [diff] [blame] | 4 | source shared.vim |
Bram Moolenaar | e20b9ec | 2020-02-03 21:40:04 +0100 | [diff] [blame] | 5 | |
Bram Moolenaar | 59cb041 | 2019-12-18 22:26:31 +0100 | [diff] [blame] | 6 | " Helper function to test editing line in Q Ex mode |
| 7 | func 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] |
| 15 | endfunc |
| 16 | |
| 17 | " Helper function to test editing line in gQ Ex mode |
| 18 | func 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 |
| 23 | endfunc |
| 24 | |
| 25 | " Helper function to test editing line with both Q and gQ Ex mode. |
| 26 | func Ex(cmd) |
| 27 | return [Ex_Q(a:cmd), Ex_gQ(a:cmd)] |
| 28 | endfunc |
| 29 | |
| 30 | " Test editing line in Ex mode (both Q and gQ) |
| 31 | func 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 Moolenaar | 0546d7d | 2020-03-01 16:53:09 +0100 | [diff] [blame] | 53 | call assert_equal(['foo', 'foo'], |
Bram Moolenaar | 91ffc8a | 2020-03-02 20:54:22 +0100 | [diff] [blame] | 54 | \ 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 Moolenaar | bd7206e | 2020-03-06 20:36:04 +0100 | [diff] [blame] | 58 | call assert_equal(["\t mn", "\tm\<C-T>n"], Ex("\tm\<C-T>n"), e) |
Bram Moolenaar | 91ffc8a | 2020-03-02 20:54:22 +0100 | [diff] [blame] | 59 | set wildchar& |
Bram Moolenaar | 59cb041 | 2019-12-18 22:26:31 +0100 | [diff] [blame] | 60 | endfor |
| 61 | |
| 62 | set sw& |
| 63 | let &encoding = encoding_save |
| 64 | endfunc |
Bram Moolenaar | e20b9ec | 2020-02-03 21:40:04 +0100 | [diff] [blame] | 65 | |
Bram Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 66 | " Test substitute confirmation prompt :%s/pat/str/c in Ex mode |
Bram Moolenaar | e20b9ec | 2020-02-03 21:40:04 +0100 | [diff] [blame] | 67 | func Test_Ex_substitute() |
| 68 | CheckRunVimInTerminal |
| 69 | let buf = RunVimInTerminal('', {'rows': 6}) |
| 70 | |
| 71 | call term_sendkeys(buf, ":call setline(1, ['foo foo', 'foo foo', 'foo foo'])\<CR>") |
| 72 | 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 Moolenaar | e5b0b98 | 2021-04-07 19:42:57 +0200 | [diff] [blame] | 80 | call term_sendkeys(buf, "N\<CR>") |
| 81 | call term_wait(buf) |
| 82 | call WaitForAssert({-> assert_match(' ^^^', term_getline(buf, 6))}, 1000) |
Bram Moolenaar | e20b9ec | 2020-02-03 21:40:04 +0100 | [diff] [blame] | 83 | 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 Moolenaar | bc2b71d | 2020-02-17 21:33:30 +0100 | [diff] [blame] | 91 | " Pressing enter in ex mode should print the current line |
| 92 | call term_sendkeys(buf, "\<CR>") |
| 93 | call WaitForAssert({-> assert_match(' 3 foo foo', |
| 94 | \ term_getline(buf, 5))}, 1000) |
| 95 | |
Bram Moolenaar | e20b9ec | 2020-02-03 21:40:04 +0100 | [diff] [blame] | 96 | call term_sendkeys(buf, ":vi\<CR>") |
| 97 | call WaitForAssert({-> assert_match('foo bar', term_getline(buf, 1))}, 1000) |
| 98 | |
Bram Moolenaar | e20b9ec | 2020-02-03 21:40:04 +0100 | [diff] [blame] | 99 | call StopVimInTerminal(buf) |
| 100 | endfunc |
| 101 | |
Bram Moolenaar | 9f6277b | 2020-02-11 22:04:02 +0100 | [diff] [blame] | 102 | " Test for displaying lines from an empty buffer in Ex mode |
| 103 | func Test_Ex_emptybuf() |
| 104 | new |
| 105 | call assert_fails('call feedkeys("Q\<CR>", "xt")', 'E749:') |
| 106 | call setline(1, "abc") |
| 107 | call assert_fails('call feedkeys("Q\<CR>", "xt")', 'E501:') |
| 108 | call assert_fails('call feedkeys("Q%d\<CR>", "xt")', 'E749:') |
| 109 | close! |
| 110 | endfunc |
| 111 | |
| 112 | " Test for the :open command |
| 113 | func Test_open_command() |
| 114 | new |
| 115 | call setline(1, ['foo foo', 'foo bar', 'foo baz']) |
| 116 | call feedkeys("Qopen\<CR>j", 'xt') |
| 117 | call assert_equal('foo bar', getline('.')) |
| 118 | call feedkeys("Qopen /bar/\<CR>", 'xt') |
| 119 | call assert_equal(5, col('.')) |
| 120 | call assert_fails('call feedkeys("Qopen /baz/\<CR>", "xt")', 'E479:') |
| 121 | close! |
| 122 | endfunc |
| 123 | |
Bram Moolenaar | e031fe9 | 2021-12-05 12:06:24 +0000 | [diff] [blame] | 124 | func Test_open_command_flush_line() |
| 125 | " this was accessing freed memory: the regexp match uses a pointer to the |
| 126 | " current line which becomes invalid when searching for the ') mark. |
| 127 | new |
| 128 | call setline(1, ['one', 'two. three']) |
| 129 | s/one/ONE |
| 130 | try |
| 131 | open /\%')/ |
| 132 | catch /E479/ |
| 133 | endtry |
| 134 | bwipe! |
| 135 | endfunc |
| 136 | |
Bram Moolenaar | 818fc9a | 2020-02-21 17:54:45 +0100 | [diff] [blame] | 137 | " Test for :g/pat/visual to run vi commands in Ex mode |
| 138 | " This used to hang Vim before 8.2.0274. |
| 139 | func Test_Ex_global() |
Bram Moolenaar | 9e2bcb5 | 2020-02-18 21:33:00 +0100 | [diff] [blame] | 140 | new |
Bram Moolenaar | 818fc9a | 2020-02-21 17:54:45 +0100 | [diff] [blame] | 141 | call setline(1, ['', 'foo', 'bar', 'foo', 'bar', 'foo']) |
Bram Moolenaar | 578fe94 | 2020-02-27 21:32:51 +0100 | [diff] [blame] | 142 | call feedkeys("Q\<bs>g/bar/visual\<CR>$rxQ$ryQvisual\<CR>j", "xt") |
Bram Moolenaar | 818fc9a | 2020-02-21 17:54:45 +0100 | [diff] [blame] | 143 | call assert_equal('bax', getline(3)) |
| 144 | call assert_equal('bay', getline(5)) |
Bram Moolenaar | 9e2bcb5 | 2020-02-18 21:33:00 +0100 | [diff] [blame] | 145 | bwipe! |
| 146 | endfunc |
| 147 | |
zeertzjq | f754fe6 | 2022-07-14 17:06:12 +0100 | [diff] [blame] | 148 | " Test for pressing Ctrl-C in :append inside a loop in Ex mode |
| 149 | " This used to hang Vim |
| 150 | func Test_Ex_append_in_loop() |
| 151 | CheckRunVimInTerminal |
| 152 | let buf = RunVimInTerminal('', {'rows': 6}) |
| 153 | |
| 154 | call term_sendkeys(buf, "gQ") |
| 155 | call term_sendkeys(buf, "for i in range(1)\<CR>") |
| 156 | call term_sendkeys(buf, "append\<CR>") |
| 157 | call WaitForAssert({-> assert_match(': append', term_getline(buf, 5))}, 1000) |
| 158 | call term_sendkeys(buf, "\<C-C>") |
| 159 | call term_wait(buf) |
| 160 | call term_sendkeys(buf, "foo\<CR>") |
| 161 | call WaitForAssert({-> assert_match('foo', term_getline(buf, 5))}, 1000) |
| 162 | call term_sendkeys(buf, ".\<CR>") |
| 163 | call WaitForAssert({-> assert_match('.', term_getline(buf, 5))}, 1000) |
| 164 | call term_sendkeys(buf, "endfor\<CR>") |
| 165 | call term_sendkeys(buf, "vi\<CR>") |
| 166 | call WaitForAssert({-> assert_match('foo', term_getline(buf, 1))}, 1000) |
| 167 | |
| 168 | call StopVimInTerminal(buf) |
| 169 | endfunc |
| 170 | |
Bram Moolenaar | 578fe94 | 2020-02-27 21:32:51 +0100 | [diff] [blame] | 171 | " In Ex-mode, a backslash escapes a newline |
| 172 | func Test_Ex_escape_enter() |
| 173 | call feedkeys("gQlet l = \"a\\\<kEnter>b\"\<cr>vi\<cr>", 'xt') |
| 174 | call assert_equal("a\rb", l) |
| 175 | endfunc |
| 176 | |
Bram Moolenaar | 0546d7d | 2020-03-01 16:53:09 +0100 | [diff] [blame] | 177 | " Test for :append! command in Ex mode |
| 178 | func Test_Ex_append() |
| 179 | new |
| 180 | call setline(1, "\t abc") |
| 181 | call feedkeys("Qappend!\npqr\nxyz\n.\nvisual\n", 'xt') |
| 182 | call assert_equal(["\t abc", "\t pqr", "\t xyz"], getline(1, '$')) |
| 183 | close! |
| 184 | endfunc |
| 185 | |
Bram Moolenaar | 91ffc8a | 2020-03-02 20:54:22 +0100 | [diff] [blame] | 186 | " In Ex-mode, backslashes at the end of a command should be halved. |
| 187 | func Test_Ex_echo_backslash() |
| 188 | " This test works only when the language is English |
Bram Moolenaar | cde0ff3 | 2020-04-04 14:00:39 +0200 | [diff] [blame] | 189 | CheckEnglish |
Bram Moolenaar | 91ffc8a | 2020-03-02 20:54:22 +0100 | [diff] [blame] | 190 | let bsl = '\\\\' |
| 191 | let bsl2 = '\\\' |
| 192 | call assert_fails('call feedkeys("Qecho " .. bsl .. "\nvisual\n", "xt")', |
Bram Moolenaar | 6b02b38 | 2021-05-16 16:19:37 +0200 | [diff] [blame] | 193 | \ 'E15: Invalid expression: "\\"') |
Bram Moolenaar | 91ffc8a | 2020-03-02 20:54:22 +0100 | [diff] [blame] | 194 | call assert_fails('call feedkeys("Qecho " .. bsl2 .. "\nm\nvisual\n", "xt")', |
Bram Moolenaar | 6b02b38 | 2021-05-16 16:19:37 +0200 | [diff] [blame] | 195 | \ "E15: Invalid expression: \"\\\nm\"") |
Bram Moolenaar | 91ffc8a | 2020-03-02 20:54:22 +0100 | [diff] [blame] | 196 | endfunc |
| 197 | |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 198 | func Test_ex_mode_errors() |
| 199 | " Not allowed to enter ex mode when text is locked |
| 200 | au InsertCharPre <buffer> normal! gQ<CR> |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 201 | let caught_e565 = 0 |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 202 | try |
| 203 | call feedkeys("ix\<esc>", 'xt') |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 204 | catch /^Vim\%((\a\+)\)\=:E565/ " catch E565 |
| 205 | let caught_e565 = 1 |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 206 | endtry |
Bram Moolenaar | ff06f28 | 2020-04-21 22:01:14 +0200 | [diff] [blame] | 207 | call assert_equal(1, caught_e565) |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 208 | au! InsertCharPre |
Bram Moolenaar | 158ea17 | 2020-06-18 17:28:39 +0200 | [diff] [blame] | 209 | |
| 210 | new |
| 211 | au CmdLineEnter * call ExEnterFunc() |
| 212 | func ExEnterFunc() |
| 213 | |
| 214 | endfunc |
| 215 | call feedkeys("gQvi\r", 'xt') |
| 216 | |
| 217 | au! CmdLineEnter |
| 218 | delfunc ExEnterFunc |
| 219 | quit |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 220 | endfunc |
| 221 | |
Bram Moolenaar | 3b6d57f | 2020-11-01 21:56:40 +0100 | [diff] [blame] | 222 | func Test_ex_mode_with_global() |
Bram Moolenaar | 399db04 | 2020-11-01 22:31:08 +0100 | [diff] [blame] | 223 | CheckNotGui |
Bram Moolenaar | 3b6d57f | 2020-11-01 21:56:40 +0100 | [diff] [blame] | 224 | CheckFeature timers |
| 225 | |
| 226 | " This will get stuck in Normal mode after the failed "J", use a timer to |
| 227 | " get going again. |
| 228 | let lines =<< trim END |
| 229 | call ch_logfile('logfile', 'w') |
| 230 | pedit |
| 231 | func FeedQ(id) |
| 232 | call feedkeys('Q', 't') |
| 233 | endfunc |
| 234 | call timer_start(10, 'FeedQ') |
| 235 | g/^/vi|HJ |
| 236 | call writefile(['done'], 'Xdidexmode') |
| 237 | qall! |
| 238 | END |
| 239 | call writefile(lines, 'Xexmodescript') |
| 240 | call assert_equal(1, RunVim([], [], '-e -s -S Xexmodescript')) |
| 241 | call assert_equal(['done'], readfile('Xdidexmode')) |
| 242 | |
Bram Moolenaar | f637bce | 2020-11-23 18:14:56 +0100 | [diff] [blame] | 243 | call delete('logfile') |
Bram Moolenaar | 3b6d57f | 2020-11-01 21:56:40 +0100 | [diff] [blame] | 244 | call delete('Xdidexmode') |
| 245 | call delete('Xexmodescript') |
| 246 | endfunc |
| 247 | |
Bram Moolenaar | 1d859e2 | 2021-01-28 17:24:58 +0100 | [diff] [blame] | 248 | func Test_ex_mode_count_overflow() |
Bram Moolenaar | 97202d9 | 2021-01-28 18:34:35 +0100 | [diff] [blame] | 249 | " The multiplication causes an integer overflow |
| 250 | CheckNotAsan |
| 251 | |
Bram Moolenaar | 1d859e2 | 2021-01-28 17:24:58 +0100 | [diff] [blame] | 252 | " this used to cause a crash |
| 253 | let lines =<< trim END |
| 254 | call feedkeys("\<Esc>Q\<CR>") |
| 255 | v9|9silent! vi|333333233333y32333333%O |
| 256 | call writefile(['done'], 'Xdidexmode') |
| 257 | qall! |
| 258 | END |
| 259 | call writefile(lines, 'Xexmodescript') |
| 260 | call assert_equal(1, RunVim([], [], '-e -s -S Xexmodescript -c qa')) |
| 261 | call assert_equal(['done'], readfile('Xdidexmode')) |
| 262 | |
| 263 | call delete('Xdidexmode') |
| 264 | call delete('Xexmodescript') |
| 265 | endfunc |
| 266 | |
Bram Moolenaar | 85b6747 | 2022-01-25 11:55:02 +0000 | [diff] [blame] | 267 | func Test_ex_mode_large_indent() |
| 268 | new |
| 269 | set ts=500 ai |
| 270 | call setline(1, "\t") |
| 271 | exe "normal gQi\<CR>." |
| 272 | set ts=8 noai |
| 273 | bwipe! |
| 274 | endfunc |
| 275 | |
Bram Moolenaar | f50808e | 2022-04-16 18:52:17 +0100 | [diff] [blame] | 276 | " This was accessing illegal memory when using "+" for eap->cmd. |
| 277 | func Test_empty_command_visual_mode() |
| 278 | let lines =<< trim END |
| 279 | r<sfile> |
| 280 | 0norm0V: |
| 281 | :qall! |
| 282 | END |
| 283 | call writefile(lines, 'Xexmodescript') |
| 284 | call assert_equal(1, RunVim([], [], '-u NONE -e -s -S Xexmodescript')) |
| 285 | |
| 286 | call delete('Xexmodescript') |
Bram Moolenaar | 217ea51 | 2022-06-14 17:13:59 +0100 | [diff] [blame] | 287 | |
| 288 | " This may cause a dialog to be displayed for an empty command, ignore it. |
| 289 | call delete('guidialogfile') |
Bram Moolenaar | f50808e | 2022-04-16 18:52:17 +0100 | [diff] [blame] | 290 | endfunc |
| 291 | |
Bram Moolenaar | 85b6747 | 2022-01-25 11:55:02 +0000 | [diff] [blame] | 292 | |
Bram Moolenaar | e20b9ec | 2020-02-03 21:40:04 +0100 | [diff] [blame] | 293 | " vim: shiftwidth=2 sts=2 expandtab |