blob: bd87e3be7eeb0b88a9008998f2542a4e2d8b0926 [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 +02004CheckNotMSWindows
5CheckNotGui
6
Bram Moolenaar076e5022017-01-24 18:58:30 +01007set term=xterm
8
9func Test_paste_normal_mode()
10 new
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010011 " In first column text is inserted
Bram Moolenaar076e5022017-01-24 18:58:30 +010012 call setline(1, ['a', 'b', 'c'])
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010013 call cursor(2, 1)
Bram Moolenaar076e5022017-01-24 18:58:30 +010014 call feedkeys("\<Esc>[200~foo\<CR>bar\<Esc>[201~", 'xt')
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010015 call assert_equal('foo', getline(2))
16 call assert_equal('barb', getline(3))
Bram Moolenaar076e5022017-01-24 18:58:30 +010017 call assert_equal('c', getline(4))
18
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010019 " When repeating text is appended
Bram Moolenaar076e5022017-01-24 18:58:30 +010020 normal .
21 call assert_equal('barfoo', getline(3))
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010022 call assert_equal('barb', getline(4))
Bram Moolenaar076e5022017-01-24 18:58:30 +010023 call assert_equal('c', getline(5))
24 bwipe!
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010025
26 " In second column text is appended
27 call setline(1, ['a', 'bbb', 'c'])
28 call cursor(2, 2)
29 call feedkeys("\<Esc>[200~foo\<CR>bar\<Esc>[201~", 'xt')
30 call assert_equal('bbfoo', getline(2))
31 call assert_equal('barb', getline(3))
32 call assert_equal('c', getline(4))
33
34 " In last column text is appended
35 call setline(1, ['a', 'bbb', 'c'])
36 call cursor(2, 3)
37 call feedkeys("\<Esc>[200~foo\<CR>bar\<Esc>[201~", 'xt')
38 call assert_equal('bbbfoo', getline(2))
39 call assert_equal('bar', getline(3))
40 call assert_equal('c', getline(4))
Bram Moolenaar076e5022017-01-24 18:58:30 +010041endfunc
42
43func Test_paste_insert_mode()
44 new
45 call setline(1, ['a', 'b', 'c'])
46 2
47 call feedkeys("i\<Esc>[200~foo\<CR>bar\<Esc>[201~ done\<Esc>", 'xt')
48 call assert_equal('foo', getline(2))
49 call assert_equal('bar doneb', getline(3))
50 call assert_equal('c', getline(4))
51
52 normal .
53 call assert_equal('bar donfoo', getline(3))
54 call assert_equal('bar doneeb', getline(4))
55 call assert_equal('c', getline(5))
Bram Moolenaar9e817c82017-01-25 21:36:17 +010056
57 set ai et tw=10
58 call setline(1, ['a', ' b', 'c'])
59 2
60 call feedkeys("A\<Esc>[200~foo\<CR> bar bar bar\<Esc>[201~\<Esc>", 'xt')
61 call assert_equal(' bfoo', getline(2))
62 call assert_equal(' bar bar bar', getline(3))
63 call assert_equal('c', getline(4))
64
65 set ai& et& tw=0
Bram Moolenaar076e5022017-01-24 18:58:30 +010066 bwipe!
67endfunc
68
Bram Moolenaar3324d0a2018-03-06 19:51:13 +010069func Test_paste_clipboard()
Bram Moolenaar52992fe2019-08-12 14:20:33 +020070 CheckFeature clipboard_working
71
Bram Moolenaar3324d0a2018-03-06 19:51:13 +010072 let @+ = "nasty\<Esc>:!ls\<CR>command"
73 new
74 exe "normal i\<C-R>+\<Esc>"
75 call assert_equal("nasty\<Esc>:!ls\<CR>command", getline(1))
76 bwipe!
77endfunc
78
Bram Moolenaar845e0ee2020-06-20 16:05:32 +020079" bracketed paste in command line
Bram Moolenaar076e5022017-01-24 18:58:30 +010080func Test_paste_cmdline()
81 call feedkeys(":a\<Esc>[200~foo\<CR>bar\<Esc>[201~b\<Home>\"\<CR>", 'xt')
82 call assert_equal("\"afoo\<CR>barb", getreg(':'))
83endfunc
Bram Moolenaara1891842017-02-04 21:34:31 +010084
Bram Moolenaar845e0ee2020-06-20 16:05:32 +020085" bracketed paste in Ex-mode
86func Test_paste_ex_mode()
87 unlet! foo
88 call feedkeys("Qlet foo=\"\<Esc>[200~foo\<CR>bar\<Esc>[201~\"\<CR>vi\<CR>", 'xt')
89 call assert_equal("foo\rbar", foo)
Bram Moolenaar806d0372022-01-25 20:45:16 +000090
91 " pasting more than 40 bytes
Mohamed Akramc25a7082024-07-12 20:17:55 +020092 exe "norm Q\<PasteStart>s/.*/0000000000000000000000000000000000000000000000000000000000000000/\<C-C>"
Bram Moolenaar845e0ee2020-06-20 16:05:32 +020093endfunc
94
95func Test_paste_onechar()
96 new
97 let @f='abc'
98 call feedkeys("i\<C-R>\<Esc>[200~foo\<CR>bar\<Esc>[201~", 'xt')
99 call assert_equal("abc", getline(1))
100 close!
101endfunc
102
Bram Moolenaara1891842017-02-04 21:34:31 +0100103func Test_paste_visual_mode()
104 new
105 call setline(1, 'here are some words')
106 call feedkeys("0fsve\<Esc>[200~more\<Esc>[201~", 'xt')
107 call assert_equal('here are more words', getline(1))
108 call assert_equal('some', getreg('-'))
zeertzjq7a732522022-03-14 20:46:41 +0000109 normal! u
110 call assert_equal('here are some words', getline(1))
111 exe "normal! \<C-R>"
112 call assert_equal('here are more words', getline(1))
Bram Moolenaara1891842017-02-04 21:34:31 +0100113
114 " include last char in the line
115 call feedkeys("0fwve\<Esc>[200~noises\<Esc>[201~", 'xt')
116 call assert_equal('here are more noises', getline(1))
117 call assert_equal('words', getreg('-'))
zeertzjq7a732522022-03-14 20:46:41 +0000118 normal! u
119 call assert_equal('here are more words', getline(1))
120 exe "normal! \<C-R>"
121 call assert_equal('here are more noises', getline(1))
Bram Moolenaara1891842017-02-04 21:34:31 +0100122
123 " exclude last char in the line
124 call setline(1, 'some words!')
125 call feedkeys("0fwve\<Esc>[200~noises\<Esc>[201~", 'xt')
126 call assert_equal('some noises!', getline(1))
127 call assert_equal('words', getreg('-'))
zeertzjq7a732522022-03-14 20:46:41 +0000128 normal! u
129 call assert_equal('some words!', getline(1))
130 exe "normal! \<C-R>"
131 call assert_equal('some noises!', getline(1))
Bram Moolenaara1891842017-02-04 21:34:31 +0100132
133 " multi-line selection
134 call setline(1, ['some words', 'and more'])
135 call feedkeys("0fwvj0fd\<Esc>[200~letters\<Esc>[201~", 'xt')
136 call assert_equal('some letters more', getline(1))
137 call assert_equal("words\nand", getreg('1'))
zeertzjq7a732522022-03-14 20:46:41 +0000138 normal! u
139 call assert_equal(['some words', 'and more'], getline(1, 2))
140 exe "normal! \<C-R>"
141 call assert_equal('some letters more', getline(1))
142
143 " linewise non-last line, cursor at start of line
144 call setline(1, ['some words', 'and more'])
145 call feedkeys("0V\<Esc>[200~letters\<Esc>[201~", 'xt')
146 call assert_equal('lettersand more', getline(1))
147 call assert_equal("some words\n", getreg('1'))
148 normal! u
149 call assert_equal(['some words', 'and more'], getline(1, 2))
150 exe "normal! \<C-R>"
151 call assert_equal('lettersand more', getline(1))
152
153 " linewise non-last line, cursor in the middle of line
154 call setline(1, ['some words', 'and more'])
155 call feedkeys("0fwV\<Esc>[200~letters\<Esc>[201~", 'xt')
156 call assert_equal('lettersand more', getline(1))
157 call assert_equal("some words\n", getreg('1'))
158 normal! u
159 call assert_equal(['some words', 'and more'], getline(1, 2))
160 exe "normal! \<C-R>"
161 call assert_equal('lettersand more', getline(1))
162
163 " linewise last line
164 call setline(1, ['some words', 'and more'])
165 call feedkeys("j0V\<Esc>[200~letters\<Esc>[201~", 'xt')
166 call assert_equal(['some words', 'letters'], getline(1, 2))
167 call assert_equal("and more\n", getreg('1'))
168 normal! u
169 call assert_equal(['some words', 'and more'], getline(1, 2))
170 exe "normal! \<C-R>"
171 call assert_equal(['some words', 'letters'], getline(1, 2))
Bram Moolenaara1891842017-02-04 21:34:31 +0100172
173 bwipe!
174endfunc
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200175
176func CheckCopyPaste()
177 call setline(1, ['copy this', ''])
178 normal 1G0"*y$
179 normal j"*p
180 call assert_equal('copy this', getline(2))
181endfunc
182
183func Test_xrestore()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200184 CheckFeature xterm_clipboard
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100185 let g:test_is_flaky = 1
186
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200187 let display = $DISPLAY
188 new
189 call CheckCopyPaste()
190
191 xrestore
192 call CheckCopyPaste()
193
194 exe "xrestore " .. display
195 call CheckCopyPaste()
196
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200197 bwipe!
198endfunc
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200199
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +0100200" Test for 'pastetoggle'
201func Test_pastetoggle()
202 new
203 set pastetoggle=<F4>
204 set nopaste
205 call feedkeys("iHello\<F4>", 'xt')
206 call assert_true(&paste)
207 call feedkeys("i\<F4>", 'xt')
208 call assert_false(&paste)
209 call assert_equal('Hello', getline(1))
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200210 " command-line completion for 'pastetoggle' value
211 call feedkeys(":set pastetoggle=\<Tab>\<C-B>\"\<CR>", 'xt')
212 call assert_equal('"set pastetoggle=<F4>', @:)
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +0100213 set pastetoggle&
214 bwipe!
215endfunc
216
zeertzjq68a573c2022-04-28 14:10:01 +0100217func Test_pastetoggle_timeout_no_typed_after_mapped()
218 CheckRunVimInTerminal
219
220 let lines =<< trim END
221 set pastetoggle=abc
222 set ttimeoutlen=10000
223 imap d a
224 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100225 call writefile(lines, 'Xpastetoggle_no_typed_after_mapped.vim', 'D')
zeertzjq68a573c2022-04-28 14:10:01 +0100226 let buf = RunVimInTerminal('-S Xpastetoggle_no_typed_after_mapped.vim', #{rows: 8})
227 call TermWait(buf)
228 call term_sendkeys(buf, ":call feedkeys('id', 't')\<CR>")
229 call term_wait(buf, 200)
230 call term_sendkeys(buf, 'bc')
231 " 'ttimeoutlen' should NOT apply
232 call WaitForAssert({-> assert_match('^-- INSERT --', term_getline(buf, 8))})
233
234 call StopVimInTerminal(buf)
zeertzjq68a573c2022-04-28 14:10:01 +0100235endfunc
236
237func Test_pastetoggle_timeout_typed_after_mapped()
238 CheckRunVimInTerminal
239
240 let lines =<< trim END
241 set pastetoggle=abc
242 set ttimeoutlen=10000
243 imap d a
244 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100245 call writefile(lines, 'Xpastetoggle_typed_after_mapped.vim', 'D')
zeertzjq68a573c2022-04-28 14:10:01 +0100246 let buf = RunVimInTerminal('-S Xpastetoggle_typed_after_mapped.vim', #{rows: 8})
247 call TermWait(buf)
248 call term_sendkeys(buf, ":call feedkeys('idb', 't')\<CR>")
249 call term_wait(buf, 200)
250 call term_sendkeys(buf, 'c')
251 " 'ttimeoutlen' should apply
252 call WaitForAssert({-> assert_match('^-- INSERT (paste) --', term_getline(buf, 8))})
253
254 call StopVimInTerminal(buf)
zeertzjq68a573c2022-04-28 14:10:01 +0100255endfunc
256
257func Test_pastetoggle_timeout_typed_after_noremap()
258 CheckRunVimInTerminal
259
260 let lines =<< trim END
261 set pastetoggle=abc
262 set ttimeoutlen=10000
263 inoremap d a
264 END
Bram Moolenaar145d1fd2022-09-30 21:57:11 +0100265 call writefile(lines, 'Xpastetoggle_typed_after_noremap.vim', 'D')
zeertzjq68a573c2022-04-28 14:10:01 +0100266 let buf = RunVimInTerminal('-S Xpastetoggle_typed_after_noremap.vim', #{rows: 8})
267 call TermWait(buf)
268 call term_sendkeys(buf, ":call feedkeys('idb', 't')\<CR>")
269 call term_wait(buf, 200)
270 call term_sendkeys(buf, 'c')
271 " 'ttimeoutlen' should apply
272 call WaitForAssert({-> assert_match('^-- INSERT (paste) --', term_getline(buf, 8))})
273
274 call StopVimInTerminal(buf)
zeertzjq68a573c2022-04-28 14:10:01 +0100275endfunc
276
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200277" Test for restoring option values when 'paste' is disabled
278func Test_paste_opt_restore()
279 set autoindent expandtab ruler showmatch
280 if has('rightleft')
281 set revins hkmap
282 endif
283 set smarttab softtabstop=3 textwidth=27 wrapmargin=12
284 if has('vartabs')
285 set varsofttabstop=10,20
286 endif
287
288 " enabling 'paste' should reset the above options
289 set paste
290 call assert_false(&autoindent)
291 call assert_false(&expandtab)
292 if has('rightleft')
293 call assert_false(&revins)
294 call assert_false(&hkmap)
295 endif
296 call assert_false(&ruler)
297 call assert_false(&showmatch)
298 call assert_false(&smarttab)
299 call assert_equal(0, &softtabstop)
300 call assert_equal(0, &textwidth)
301 call assert_equal(0, &wrapmargin)
302 if has('vartabs')
303 call assert_equal('', &varsofttabstop)
304 endif
305
306 " disabling 'paste' should restore the option values
307 set nopaste
308 call assert_true(&autoindent)
309 call assert_true(&expandtab)
310 if has('rightleft')
311 call assert_true(&revins)
312 call assert_true(&hkmap)
313 endif
314 call assert_true(&ruler)
315 call assert_true(&showmatch)
316 call assert_true(&smarttab)
317 call assert_equal(3, &softtabstop)
318 call assert_equal(27, &textwidth)
319 call assert_equal(12, &wrapmargin)
320 if has('vartabs')
321 call assert_equal('10,20', &varsofttabstop)
322 endif
323
324 set autoindent& expandtab& ruler& showmatch&
325 if has('rightleft')
326 set revins& hkmap&
327 endif
328 set smarttab& softtabstop& textwidth& wrapmargin&
329 if has('vartabs')
330 set varsofttabstop&
331 endif
332endfunc
333
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200334" vim: shiftwidth=2 sts=2 expandtab