blob: d079f48f9253f4c4007d5b7651d11117a96de928 [file] [log] [blame]
Bram Moolenaar3324d0a2018-03-06 19:51:13 +01001" Tests for bracketed paste and other forms of pasting.
Bram Moolenaar076e5022017-01-24 18:58:30 +01002
Bram Moolenaar832615b2019-03-23 13:30:22 +01003" Bracketed paste only works with "xterm". Not in GUI or Windows console.
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02004source check.vim
zeertzjq68a573c2022-04-28 14:10:01 +01005source term_util.vim
Bram Moolenaar8c5a2782019-08-07 23:07:07 +02006CheckNotMSWindows
7CheckNotGui
8
Bram Moolenaar076e5022017-01-24 18:58:30 +01009set term=xterm
10
Bram Moolenaara9034722018-03-13 15:43:46 +010011source shared.vim
12
Bram Moolenaar076e5022017-01-24 18:58:30 +010013func Test_paste_normal_mode()
14 new
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010015 " In first column text is inserted
Bram Moolenaar076e5022017-01-24 18:58:30 +010016 call setline(1, ['a', 'b', 'c'])
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010017 call cursor(2, 1)
Bram Moolenaar076e5022017-01-24 18:58:30 +010018 call feedkeys("\<Esc>[200~foo\<CR>bar\<Esc>[201~", 'xt')
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010019 call assert_equal('foo', getline(2))
20 call assert_equal('barb', getline(3))
Bram Moolenaar076e5022017-01-24 18:58:30 +010021 call assert_equal('c', getline(4))
22
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010023 " When repeating text is appended
Bram Moolenaar076e5022017-01-24 18:58:30 +010024 normal .
25 call assert_equal('barfoo', getline(3))
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010026 call assert_equal('barb', getline(4))
Bram Moolenaar076e5022017-01-24 18:58:30 +010027 call assert_equal('c', getline(5))
28 bwipe!
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010029
30 " In second column text is appended
31 call setline(1, ['a', 'bbb', 'c'])
32 call cursor(2, 2)
33 call feedkeys("\<Esc>[200~foo\<CR>bar\<Esc>[201~", 'xt')
34 call assert_equal('bbfoo', getline(2))
35 call assert_equal('barb', getline(3))
36 call assert_equal('c', getline(4))
37
38 " In last column text is appended
39 call setline(1, ['a', 'bbb', 'c'])
40 call cursor(2, 3)
41 call feedkeys("\<Esc>[200~foo\<CR>bar\<Esc>[201~", 'xt')
42 call assert_equal('bbbfoo', getline(2))
43 call assert_equal('bar', getline(3))
44 call assert_equal('c', getline(4))
Bram Moolenaar076e5022017-01-24 18:58:30 +010045endfunc
46
47func Test_paste_insert_mode()
48 new
49 call setline(1, ['a', 'b', 'c'])
50 2
51 call feedkeys("i\<Esc>[200~foo\<CR>bar\<Esc>[201~ done\<Esc>", 'xt')
52 call assert_equal('foo', getline(2))
53 call assert_equal('bar doneb', getline(3))
54 call assert_equal('c', getline(4))
55
56 normal .
57 call assert_equal('bar donfoo', getline(3))
58 call assert_equal('bar doneeb', getline(4))
59 call assert_equal('c', getline(5))
Bram Moolenaar9e817c82017-01-25 21:36:17 +010060
61 set ai et tw=10
62 call setline(1, ['a', ' b', 'c'])
63 2
64 call feedkeys("A\<Esc>[200~foo\<CR> bar bar bar\<Esc>[201~\<Esc>", 'xt')
65 call assert_equal(' bfoo', getline(2))
66 call assert_equal(' bar bar bar', getline(3))
67 call assert_equal('c', getline(4))
68
69 set ai& et& tw=0
Bram Moolenaar076e5022017-01-24 18:58:30 +010070 bwipe!
71endfunc
72
Bram Moolenaar3324d0a2018-03-06 19:51:13 +010073func Test_paste_clipboard()
Bram Moolenaar52992fe2019-08-12 14:20:33 +020074 CheckFeature clipboard_working
75
Bram Moolenaar3324d0a2018-03-06 19:51:13 +010076 let @+ = "nasty\<Esc>:!ls\<CR>command"
77 new
78 exe "normal i\<C-R>+\<Esc>"
79 call assert_equal("nasty\<Esc>:!ls\<CR>command", getline(1))
80 bwipe!
81endfunc
82
Bram Moolenaar845e0ee2020-06-20 16:05:32 +020083" bracketed paste in command line
Bram Moolenaar076e5022017-01-24 18:58:30 +010084func Test_paste_cmdline()
85 call feedkeys(":a\<Esc>[200~foo\<CR>bar\<Esc>[201~b\<Home>\"\<CR>", 'xt')
86 call assert_equal("\"afoo\<CR>barb", getreg(':'))
87endfunc
Bram Moolenaara1891842017-02-04 21:34:31 +010088
Bram Moolenaar845e0ee2020-06-20 16:05:32 +020089" bracketed paste in Ex-mode
90func Test_paste_ex_mode()
91 unlet! foo
92 call feedkeys("Qlet foo=\"\<Esc>[200~foo\<CR>bar\<Esc>[201~\"\<CR>vi\<CR>", 'xt')
93 call assert_equal("foo\rbar", foo)
Bram Moolenaar806d0372022-01-25 20:45:16 +000094
95 " pasting more than 40 bytes
96 exe "norm Q\<PasteStart>0000000000000000000000000000000000000000000000000000000000000000000000\<C-C>"
Bram Moolenaar845e0ee2020-06-20 16:05:32 +020097endfunc
98
99func Test_paste_onechar()
100 new
101 let @f='abc'
102 call feedkeys("i\<C-R>\<Esc>[200~foo\<CR>bar\<Esc>[201~", 'xt')
103 call assert_equal("abc", getline(1))
104 close!
105endfunc
106
Bram Moolenaara1891842017-02-04 21:34:31 +0100107func Test_paste_visual_mode()
108 new
109 call setline(1, 'here are some words')
110 call feedkeys("0fsve\<Esc>[200~more\<Esc>[201~", 'xt')
111 call assert_equal('here are more words', getline(1))
112 call assert_equal('some', getreg('-'))
zeertzjq7a732522022-03-14 20:46:41 +0000113 normal! u
114 call assert_equal('here are some words', getline(1))
115 exe "normal! \<C-R>"
116 call assert_equal('here are more words', getline(1))
Bram Moolenaara1891842017-02-04 21:34:31 +0100117
118 " include last char in the line
119 call feedkeys("0fwve\<Esc>[200~noises\<Esc>[201~", 'xt')
120 call assert_equal('here are more noises', getline(1))
121 call assert_equal('words', getreg('-'))
zeertzjq7a732522022-03-14 20:46:41 +0000122 normal! u
123 call assert_equal('here are more words', getline(1))
124 exe "normal! \<C-R>"
125 call assert_equal('here are more noises', getline(1))
Bram Moolenaara1891842017-02-04 21:34:31 +0100126
127 " exclude last char in the line
128 call setline(1, 'some words!')
129 call feedkeys("0fwve\<Esc>[200~noises\<Esc>[201~", 'xt')
130 call assert_equal('some noises!', getline(1))
131 call assert_equal('words', getreg('-'))
zeertzjq7a732522022-03-14 20:46:41 +0000132 normal! u
133 call assert_equal('some words!', getline(1))
134 exe "normal! \<C-R>"
135 call assert_equal('some noises!', getline(1))
Bram Moolenaara1891842017-02-04 21:34:31 +0100136
137 " multi-line selection
138 call setline(1, ['some words', 'and more'])
139 call feedkeys("0fwvj0fd\<Esc>[200~letters\<Esc>[201~", 'xt')
140 call assert_equal('some letters more', getline(1))
141 call assert_equal("words\nand", getreg('1'))
zeertzjq7a732522022-03-14 20:46:41 +0000142 normal! u
143 call assert_equal(['some words', 'and more'], getline(1, 2))
144 exe "normal! \<C-R>"
145 call assert_equal('some letters more', getline(1))
146
147 " linewise non-last line, cursor at start of line
148 call setline(1, ['some words', 'and more'])
149 call feedkeys("0V\<Esc>[200~letters\<Esc>[201~", 'xt')
150 call assert_equal('lettersand more', getline(1))
151 call assert_equal("some words\n", getreg('1'))
152 normal! u
153 call assert_equal(['some words', 'and more'], getline(1, 2))
154 exe "normal! \<C-R>"
155 call assert_equal('lettersand more', getline(1))
156
157 " linewise non-last line, cursor in the middle of line
158 call setline(1, ['some words', 'and more'])
159 call feedkeys("0fwV\<Esc>[200~letters\<Esc>[201~", 'xt')
160 call assert_equal('lettersand more', getline(1))
161 call assert_equal("some words\n", getreg('1'))
162 normal! u
163 call assert_equal(['some words', 'and more'], getline(1, 2))
164 exe "normal! \<C-R>"
165 call assert_equal('lettersand more', getline(1))
166
167 " linewise last line
168 call setline(1, ['some words', 'and more'])
169 call feedkeys("j0V\<Esc>[200~letters\<Esc>[201~", 'xt')
170 call assert_equal(['some words', 'letters'], getline(1, 2))
171 call assert_equal("and more\n", getreg('1'))
172 normal! u
173 call assert_equal(['some words', 'and more'], getline(1, 2))
174 exe "normal! \<C-R>"
175 call assert_equal(['some words', 'letters'], getline(1, 2))
Bram Moolenaara1891842017-02-04 21:34:31 +0100176
177 bwipe!
178endfunc
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200179
180func CheckCopyPaste()
181 call setline(1, ['copy this', ''])
182 normal 1G0"*y$
183 normal j"*p
184 call assert_equal('copy this', getline(2))
185endfunc
186
187func Test_xrestore()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200188 CheckFeature xterm_clipboard
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100189 let g:test_is_flaky = 1
190
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200191 let display = $DISPLAY
192 new
193 call CheckCopyPaste()
194
195 xrestore
196 call CheckCopyPaste()
197
198 exe "xrestore " .. display
199 call CheckCopyPaste()
200
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200201 bwipe!
202endfunc
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200203
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +0100204" Test for 'pastetoggle'
205func Test_pastetoggle()
206 new
207 set pastetoggle=<F4>
208 set nopaste
209 call feedkeys("iHello\<F4>", 'xt')
210 call assert_true(&paste)
211 call feedkeys("i\<F4>", 'xt')
212 call assert_false(&paste)
213 call assert_equal('Hello', getline(1))
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200214 " command-line completion for 'pastetoggle' value
215 call feedkeys(":set pastetoggle=\<Tab>\<C-B>\"\<CR>", 'xt')
216 call assert_equal('"set pastetoggle=<F4>', @:)
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +0100217 set pastetoggle&
218 bwipe!
219endfunc
220
zeertzjq68a573c2022-04-28 14:10:01 +0100221func Test_pastetoggle_timeout_no_typed_after_mapped()
222 CheckRunVimInTerminal
223
224 let lines =<< trim END
225 set pastetoggle=abc
226 set ttimeoutlen=10000
227 imap d a
228 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100229 call writefile(lines, 'Xpastetoggle_no_typed_after_mapped.vim', 'D')
zeertzjq68a573c2022-04-28 14:10:01 +0100230 let buf = RunVimInTerminal('-S Xpastetoggle_no_typed_after_mapped.vim', #{rows: 8})
231 call TermWait(buf)
232 call term_sendkeys(buf, ":call feedkeys('id', 't')\<CR>")
233 call term_wait(buf, 200)
234 call term_sendkeys(buf, 'bc')
235 " 'ttimeoutlen' should NOT apply
236 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 8))})
237
238 call StopVimInTerminal(buf)
zeertzjq68a573c2022-04-28 14:10:01 +0100239endfunc
240
241func Test_pastetoggle_timeout_typed_after_mapped()
242 CheckRunVimInTerminal
243
244 let lines =<< trim END
245 set pastetoggle=abc
246 set ttimeoutlen=10000
247 imap d a
248 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100249 call writefile(lines, 'Xpastetoggle_typed_after_mapped.vim', 'D')
zeertzjq68a573c2022-04-28 14:10:01 +0100250 let buf = RunVimInTerminal('-S Xpastetoggle_typed_after_mapped.vim', #{rows: 8})
251 call TermWait(buf)
252 call term_sendkeys(buf, ":call feedkeys('idb', 't')\<CR>")
253 call term_wait(buf, 200)
254 call term_sendkeys(buf, 'c')
255 " 'ttimeoutlen' should apply
256 call WaitForAssert({-> assert_match('^-- INSERT (paste) --', term_getline(buf, 8))})
257
258 call StopVimInTerminal(buf)
zeertzjq68a573c2022-04-28 14:10:01 +0100259endfunc
260
261func Test_pastetoggle_timeout_typed_after_noremap()
262 CheckRunVimInTerminal
263
264 let lines =<< trim END
265 set pastetoggle=abc
266 set ttimeoutlen=10000
267 inoremap d a
268 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100269 call writefile(lines, 'Xpastetoggle_typed_after_noremap.vim', 'D')
zeertzjq68a573c2022-04-28 14:10:01 +0100270 let buf = RunVimInTerminal('-S Xpastetoggle_typed_after_noremap.vim', #{rows: 8})
271 call TermWait(buf)
272 call term_sendkeys(buf, ":call feedkeys('idb', 't')\<CR>")
273 call term_wait(buf, 200)
274 call term_sendkeys(buf, 'c')
275 " 'ttimeoutlen' should apply
276 call WaitForAssert({-> assert_match('^-- INSERT (paste) --', term_getline(buf, 8))})
277
278 call StopVimInTerminal(buf)
zeertzjq68a573c2022-04-28 14:10:01 +0100279endfunc
280
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200281" Test for restoring option values when 'paste' is disabled
282func Test_paste_opt_restore()
283 set autoindent expandtab ruler showmatch
284 if has('rightleft')
285 set revins hkmap
286 endif
287 set smarttab softtabstop=3 textwidth=27 wrapmargin=12
288 if has('vartabs')
289 set varsofttabstop=10,20
290 endif
291
292 " enabling 'paste' should reset the above options
293 set paste
294 call assert_false(&autoindent)
295 call assert_false(&expandtab)
296 if has('rightleft')
297 call assert_false(&revins)
298 call assert_false(&hkmap)
299 endif
300 call assert_false(&ruler)
301 call assert_false(&showmatch)
302 call assert_false(&smarttab)
303 call assert_equal(0, &softtabstop)
304 call assert_equal(0, &textwidth)
305 call assert_equal(0, &wrapmargin)
306 if has('vartabs')
307 call assert_equal('', &varsofttabstop)
308 endif
309
310 " disabling 'paste' should restore the option values
311 set nopaste
312 call assert_true(&autoindent)
313 call assert_true(&expandtab)
314 if has('rightleft')
315 call assert_true(&revins)
316 call assert_true(&hkmap)
317 endif
318 call assert_true(&ruler)
319 call assert_true(&showmatch)
320 call assert_true(&smarttab)
321 call assert_equal(3, &softtabstop)
322 call assert_equal(27, &textwidth)
323 call assert_equal(12, &wrapmargin)
324 if has('vartabs')
325 call assert_equal('10,20', &varsofttabstop)
326 endif
327
328 set autoindent& expandtab& ruler& showmatch&
329 if has('rightleft')
330 set revins& hkmap&
331 endif
332 set smarttab& softtabstop& textwidth& wrapmargin&
333 if has('vartabs')
334 set varsofttabstop&
335 endif
336endfunc
337
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200338" vim: shiftwidth=2 sts=2 expandtab