" Test editing line in Ex mode (see :help Q and :help gQ). source check.vim source shared.vim " Helper function to test editing line in Q Ex mode func Ex_Q(cmd) " Is there a simpler way to test editing Ex line? call feedkeys("Q" \ .. "let s:test_ex =<< END\" \ .. a:cmd .. "\" \ .. "END\" \ .. "visual\", 'tx') return s:test_ex[0] endfunc " Helper function to test editing line in gQ Ex mode func Ex_gQ(cmd) call feedkeys("gQ" .. a:cmd .. "\\"\", 'tx') let ret = @:[1:] " Remove leading quote. call feedkeys("visual\", 'tx') return ret endfunc " Helper function to test editing line with both Q and gQ Ex mode. func Ex(cmd) return [Ex_Q(a:cmd), Ex_gQ(a:cmd)] endfunc " Test editing line in Ex mode (both Q and gQ) func Test_ex_mode() throw 'Skipped: Nvim only supports Vim Ex mode' let encoding_save = &encoding set sw=2 for e in ['utf8', 'latin1'] exe 'set encoding=' . e call assert_equal(['bar', 'bar'], Ex("foo bar\bar"), e) call assert_equal(["1\2", "1\2"], Ex("1\\2"), e) call assert_equal(["1\2\3", '213'], Ex("1\2\3"), e) call assert_equal(['0123', '2013'], Ex("01\2\3"), e) call assert_equal(['0123', '0213'], Ex("01\2\3"), e) call assert_equal(['01234', '0342'], Ex("012\\\3\4"), e) call assert_equal(["foo bar\", 'foo '], Ex("foo bar\"), e) call assert_equal(['foo', 'foo'], Ex("fooba\\"), e) call assert_equal(["foo\tbar", 'foobar'], Ex("foo\bar"), e) call assert_equal(["abbrev\t", 'abbreviate'], Ex("abbrev\"), e) call assert_equal([' 1', "1\\"], Ex("1\\"), e) call assert_equal([' 1', "1\\"], Ex("1\\\"), e) call assert_equal([' foo', ' foo'], Ex(" foo\"), e) call assert_equal(['foo', ' foo0'], Ex(" foo0\"), e) call assert_equal(['foo', ' foo^'], Ex(" foo^\"), e) call assert_equal(['foo', 'foo'], \ Ex("\\\\foo"), e) " default wildchar interferes with this test set wildchar= call assert_equal(["a\tb", "a\tb"], Ex("a\t\t\b"), e) call assert_equal(["\t mn", "\tm\n"], Ex("\tm\n"), e) set wildchar& endfor set sw& let &encoding = encoding_save endfunc " Test substitute confirmation prompt :%s/pat/str/c in Ex mode func Test_Ex_substitute() CheckRunVimInTerminal let buf = RunVimInTerminal('', {'rows': 6}) call term_sendkeys(buf, ":call setline(1, ['foo foo', 'foo foo', 'foo foo'])\") call term_sendkeys(buf, ":set number\") call term_sendkeys(buf, "gQ") call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000) call term_sendkeys(buf, "%s/foo/bar/gc\") call WaitForAssert({-> assert_match(' 1 foo foo', term_getline(buf, 5))}, \ 1000) call WaitForAssert({-> assert_match(' ^^^', term_getline(buf, 6))}, 1000) call term_sendkeys(buf, "N\") call term_wait(buf) call WaitForAssert({-> assert_match(' ^^^', term_getline(buf, 6))}, 1000) call term_sendkeys(buf, "n\") call WaitForAssert({-> assert_match(' ^^^', term_getline(buf, 6))}, \ 1000) call term_sendkeys(buf, "y\") call term_sendkeys(buf, "q\") call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000) " Pressing enter in ex mode should print the current line call term_sendkeys(buf, "\") call WaitForAssert({-> assert_match(' 3 foo foo', \ term_getline(buf, 5))}, 1000) call term_sendkeys(buf, ":vi\") call WaitForAssert({-> assert_match('foo bar', term_getline(buf, 1))}, 1000) call StopVimInTerminal(buf) endfunc " Test for displaying lines from an empty buffer in Ex mode func Test_Ex_emptybuf() new call assert_fails('call feedkeys("Q\", "xt")', 'E749:') call setline(1, "abc") call assert_fails('call feedkeys("Q\", "xt")', 'E501:') call assert_fails('call feedkeys("Q%d\", "xt")', 'E749:') close! endfunc " Test for the :open command func Test_open_command() throw 'Skipped: Nvim does not have :open' new call setline(1, ['foo foo', 'foo bar', 'foo baz']) call feedkeys("Qopen\j", 'xt') call assert_equal('foo bar', getline('.')) call feedkeys("Qopen /bar/\", 'xt') call assert_equal(5, col('.')) call assert_fails('call feedkeys("Qopen /baz/\", "xt")', 'E479:') close! endfunc func Test_open_command_flush_line() throw 'Skipped: Nvim does not have :open' " this was accessing freed memory: the regexp match uses a pointer to the " current line which becomes invalid when searching for the ') mark. new call setline(1, ['one', 'two. three']) s/one/ONE try open /\%')/ catch /E479/ endtry bwipe! endfunc " FIXME: this doesn't fail without the fix but hangs func Skip_Test_open_command_state() " Tricky script that failed because State was not set properly let lines =<< trim END !ls ƒ 0scìi so! Xsourced set t_û0=0 v/-/o END call writefile(lines, 'XopenScript', '') let sourced = ["!f\u0083\x02\z=0"] call writefile(sourced, 'Xsourced', 'b') CheckRunVimInTerminal 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}) sleep 3 call StopVimInTerminal(buf) endfunc " Test for :g/pat/visual to run vi commands in Ex mode " This used to hang Vim before 8.2.0274. func Test_Ex_global() new call setline(1, ['', 'foo', 'bar', 'foo', 'bar', 'foo']) call feedkeys("Q\g/bar/visual\$rxQ$ryQvisual\j", "xt") call assert_equal('bax', getline(3)) call assert_equal('bay', getline(5)) bwipe! endfunc " Test for pressing Ctrl-C in :append inside a loop in Ex mode " This used to hang Vim func Test_Ex_append_in_loop() CheckRunVimInTerminal let buf = RunVimInTerminal('', {'rows': 6}) call term_sendkeys(buf, "gQ") call term_sendkeys(buf, "for i in range(1)\") call term_sendkeys(buf, "append\") call WaitForAssert({-> assert_match(': append', term_getline(buf, 5))}, 1000) call term_sendkeys(buf, "\") " Wait for input to be flushed call term_wait(buf) call term_sendkeys(buf, "foo\") call WaitForAssert({-> assert_match('foo', term_getline(buf, 5))}, 1000) call term_sendkeys(buf, ".\") call WaitForAssert({-> assert_match('.', term_getline(buf, 5))}, 1000) call term_sendkeys(buf, "endfor\") call term_sendkeys(buf, "vi\") call WaitForAssert({-> assert_match('foo', term_getline(buf, 1))}, 1000) call StopVimInTerminal(buf) endfunc " In Ex-mode, a backslash escapes a newline func Test_Ex_escape_enter() call feedkeys("gQlet l = \"a\\\b\"\vi\", 'xt') call assert_equal("a\rb", l) endfunc " Test for :append! command in Ex mode func Test_Ex_append() throw 'Skipped: Nvim only supports Vim Ex mode' new call setline(1, "\t abc") call feedkeys("Qappend!\npqr\nxyz\n.\nvisual\n", 'xt') call assert_equal(["\t abc", "\t pqr", "\t xyz"], getline(1, '$')) close! endfunc " In Ex-mode, backslashes at the end of a command should be halved. func Test_Ex_echo_backslash() throw 'Skipped: Nvim only supports Vim Ex mode' " This test works only when the language is English CheckEnglish let bsl = '\\\\' let bsl2 = '\\\' call assert_fails('call feedkeys("Qecho " .. bsl .. "\nvisual\n", "xt")', \ 'E15: Invalid expression: "\\"') call assert_fails('call feedkeys("Qecho " .. bsl2 .. "\nm\nvisual\n", "xt")', \ "E15: Invalid expression: \"\\\nm\"") endfunc func Test_ex_mode_errors() " Not allowed to enter ex mode when text is locked au InsertCharPre normal! gQ let caught_e565 = 0 try call feedkeys("ix\", 'xt') catch /^Vim\%((\a\+)\)\=:E565/ " catch E565 let caught_e565 = 1 endtry call assert_equal(1, caught_e565) au! InsertCharPre new au CmdLineEnter * call ExEnterFunc() func ExEnterFunc() endfunc call feedkeys("gQvi\r", 'xt') au! CmdLineEnter delfunc ExEnterFunc au CmdlineEnter * : call feedkeys("gQecho 1\r", 'xt') au! CmdlineEnter quit endfunc func Test_ex_mode_count_overflow() " The multiplication causes an integer overflow CheckNotAsan " this used to cause a crash let lines =<< trim END call feedkeys("\gQ\") v9|9silent! vi|333333233333y32333333%O call writefile(['done'], 'Xdidexmode') qall! END call writefile(lines, 'Xexmodescript') call assert_equal(1, RunVim([], [], '-e -s -S Xexmodescript -c qa')) call assert_equal(['done'], readfile('Xdidexmode')) call delete('Xdidexmode') call delete('Xexmodescript') endfunc func Test_ex_mode_large_indent() new set ts=500 ai call setline(1, "\t") exe "normal gQi\." set ts=8 noai bwipe! endfunc " vim: shiftwidth=2 sts=2 expandtab