summaryrefslogtreecommitdiffstats
path: root/src/testdir/test_ex_mode.vim
blob: ffde11e3e9e072b23c9884d2d6927f049d9a9978 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
" 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\<CR>"
        \    .. a:cmd .. "\<CR>"
        \    .. "END\<CR>"
        \    .. "visual\<CR>", '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 .. "\<C-b>\"\<CR>", 'tx')
  let ret = @:[1:] " Remove leading quote.
  call feedkeys("visual\<CR>", '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()
  let encoding_save = &encoding
  set sw=2

  for e in ['utf8', 'latin1']
    exe 'set encoding=' . e

    call assert_equal(['bar', 'bar'],             Ex("foo bar\<C-u>bar"), e)
    call assert_equal(["1\<C-u>2", "1\<C-u>2"],   Ex("1\<C-v>\<C-u>2"), e)
    call assert_equal(["1\<C-b>2\<C-e>3", '213'], Ex("1\<C-b>2\<C-e>3"), e)
    call assert_equal(['0123', '2013'],           Ex("01\<Home>2\<End>3"), e)
    call assert_equal(['0123', '0213'],           Ex("01\<Left>2\<Right>3"), e)
    call assert_equal(['01234', '0342'],          Ex("012\<Left>\<Left>\<Insert>3\<Insert>4"), e)
    call assert_equal(["foo bar\<C-w>", 'foo '],  Ex("foo bar\<C-w>"), e)
    call assert_equal(['foo', 'foo'],             Ex("fooba\<Del>\<Del>"), e)
    call assert_equal(["foo\tbar", 'foobar'],     Ex("foo\<Tab>bar"), e)
    call assert_equal(["abbrev\t", 'abbreviate'], Ex("abbrev\<Tab>"), e)
    call assert_equal(['    1', "1\<C-t>\<C-t>"], Ex("1\<C-t>\<C-t>"), e)
    call assert_equal(['  1', "1\<C-t>\<C-t>"],   Ex("1\<C-t>\<C-t>\<C-d>"), e)
    call assert_equal(['  foo', '    foo'],       Ex("    foo\<C-d>"), e)
    call assert_equal(['foo', '    foo0'],        Ex("    foo0\<C-d>"), e)
    call assert_equal(['foo', '    foo^'],        Ex("    foo^\<C-d>"), e)
    call assert_equal(['foo', 'foo'],
          \ Ex("\<BS>\<C-H>\<Del>\<kDel>foo"), e)
    " default wildchar <Tab> interferes with this test
    set wildchar=<c-e>
    call assert_equal(["a\tb", "a\tb"],           Ex("a\t\t\<C-H>b"), e)
    call assert_equal(["\t  mn", "\tm\<C-T>n"],        Ex("\tm\<C-T>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'])\<CR>")
  call term_sendkeys(buf, ":set number\<CR>")
  call term_sendkeys(buf, "gQ")
  call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000)

  call term_sendkeys(buf, "%s/foo/bar/gc\<CR>")
  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\<CR>")
  call WaitForAssert({-> assert_match('        ^^^', term_getline(buf, 6))},
        \ 1000)
  call term_sendkeys(buf, "y\<CR>")

  call term_sendkeys(buf, "q\<CR>")
  call WaitForAssert({-> assert_match(':', term_getline(buf, 6))}, 1000)

  " Pressing enter in ex mode should print the current line
  call term_sendkeys(buf, "\<CR>")
  call WaitForAssert({-> assert_match('  3 foo foo',
        \ term_getline(buf, 5))}, 1000)

  call term_sendkeys(buf, ":vi\<CR>")
  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\<CR>", "xt")', 'E749:')
  call setline(1, "abc")
  call assert_fails('call feedkeys("Q\<CR>", "xt")', 'E501:')
  call assert_fails('call feedkeys("Q%d\<CR>", "xt")', 'E749:')
  close!
endfunc

" Test for the :open command
func Test_open_command()
  new
  call setline(1, ['foo foo', 'foo bar', 'foo baz'])
  call feedkeys("Qopen\<CR>j", 'xt')
  call assert_equal('foo bar', getline('.'))
  call feedkeys("Qopen /bar/\<CR>", 'xt')
  call assert_equal(5, col('.'))
  call assert_fails('call feedkeys("Qopen /baz/\<CR>", "xt")', 'E479:')
  close!
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\<bs>g/bar/visual\<CR>$rxQ$ryQvisual\<CR>j", "xt")
  call assert_equal('bax', getline(3))
  call assert_equal('bay', getline(5))
  bwipe!
endfunc

" In Ex-mode, a backslash escapes a newline
func Test_Ex_escape_enter()
  call feedkeys("gQlet l = \"a\\\<kEnter>b\"\<cr>vi\<cr>", 'xt')
  call assert_equal("a\rb", l)
endfunc

" Test for :append! command in Ex mode
func Test_Ex_append()
  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()
  " 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 <buffer> normal! gQ<CR>
  let caught_e565 = 0
  try
    call feedkeys("ix\<esc>", '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
  quit
endfunc

func Test_ex_mode_with_global()
  CheckNotGui
  CheckFeature timers

  " This will get stuck in Normal mode after the failed "J", use a timer to
  " get going again.
  let lines =<< trim END
    call ch_logfile('logfile', 'w')
    pedit
    func FeedQ(id)
      call feedkeys('Q', 't')
    endfunc
    call timer_start(10, 'FeedQ')
    g/^/vi|HJ
    call writefile(['done'], 'Xdidexmode')
    qall!
  END
  call writefile(lines, 'Xexmodescript')
  call assert_equal(1, RunVim([], [], '-e -s -S Xexmodescript'))
  call assert_equal(['done'], readfile('Xdidexmode'))

  call delete('logfile')
  call delete('Xdidexmode')
  call delete('Xexmodescript')
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("\<Esc>Q\<CR>")
    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

" vim: shiftwidth=2 sts=2 expandtab