blob: 5b8d8a0e3e2dba9642b16e4e6be34c36fa9af6fc [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
5CheckNotMSWindows
6CheckNotGui
7
Bram Moolenaar076e5022017-01-24 18:58:30 +01008set term=xterm
9
Bram Moolenaara9034722018-03-13 15:43:46 +010010source shared.vim
11
Bram Moolenaar076e5022017-01-24 18:58:30 +010012func Test_paste_normal_mode()
13 new
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010014 " In first column text is inserted
Bram Moolenaar076e5022017-01-24 18:58:30 +010015 call setline(1, ['a', 'b', 'c'])
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010016 call cursor(2, 1)
Bram Moolenaar076e5022017-01-24 18:58:30 +010017 call feedkeys("\<Esc>[200~foo\<CR>bar\<Esc>[201~", 'xt')
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010018 call assert_equal('foo', getline(2))
19 call assert_equal('barb', getline(3))
Bram Moolenaar076e5022017-01-24 18:58:30 +010020 call assert_equal('c', getline(4))
21
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010022 " When repeating text is appended
Bram Moolenaar076e5022017-01-24 18:58:30 +010023 normal .
24 call assert_equal('barfoo', getline(3))
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010025 call assert_equal('barb', getline(4))
Bram Moolenaar076e5022017-01-24 18:58:30 +010026 call assert_equal('c', getline(5))
27 bwipe!
Bram Moolenaarfd8983b2017-02-02 22:21:29 +010028
29 " In second column text is appended
30 call setline(1, ['a', 'bbb', 'c'])
31 call cursor(2, 2)
32 call feedkeys("\<Esc>[200~foo\<CR>bar\<Esc>[201~", 'xt')
33 call assert_equal('bbfoo', getline(2))
34 call assert_equal('barb', getline(3))
35 call assert_equal('c', getline(4))
36
37 " In last column text is appended
38 call setline(1, ['a', 'bbb', 'c'])
39 call cursor(2, 3)
40 call feedkeys("\<Esc>[200~foo\<CR>bar\<Esc>[201~", 'xt')
41 call assert_equal('bbbfoo', getline(2))
42 call assert_equal('bar', getline(3))
43 call assert_equal('c', getline(4))
Bram Moolenaar076e5022017-01-24 18:58:30 +010044endfunc
45
46func Test_paste_insert_mode()
47 new
48 call setline(1, ['a', 'b', 'c'])
49 2
50 call feedkeys("i\<Esc>[200~foo\<CR>bar\<Esc>[201~ done\<Esc>", 'xt')
51 call assert_equal('foo', getline(2))
52 call assert_equal('bar doneb', getline(3))
53 call assert_equal('c', getline(4))
54
55 normal .
56 call assert_equal('bar donfoo', getline(3))
57 call assert_equal('bar doneeb', getline(4))
58 call assert_equal('c', getline(5))
Bram Moolenaar9e817c82017-01-25 21:36:17 +010059
60 set ai et tw=10
61 call setline(1, ['a', ' b', 'c'])
62 2
63 call feedkeys("A\<Esc>[200~foo\<CR> bar bar bar\<Esc>[201~\<Esc>", 'xt')
64 call assert_equal(' bfoo', getline(2))
65 call assert_equal(' bar bar bar', getline(3))
66 call assert_equal('c', getline(4))
67
68 set ai& et& tw=0
Bram Moolenaar076e5022017-01-24 18:58:30 +010069 bwipe!
70endfunc
71
Bram Moolenaar3324d0a2018-03-06 19:51:13 +010072func Test_paste_clipboard()
Bram Moolenaar52992fe2019-08-12 14:20:33 +020073 CheckFeature clipboard_working
74
Bram Moolenaar3324d0a2018-03-06 19:51:13 +010075 let @+ = "nasty\<Esc>:!ls\<CR>command"
76 new
77 exe "normal i\<C-R>+\<Esc>"
78 call assert_equal("nasty\<Esc>:!ls\<CR>command", getline(1))
79 bwipe!
80endfunc
81
Bram Moolenaar845e0ee2020-06-20 16:05:32 +020082" bracketed paste in command line
Bram Moolenaar076e5022017-01-24 18:58:30 +010083func Test_paste_cmdline()
84 call feedkeys(":a\<Esc>[200~foo\<CR>bar\<Esc>[201~b\<Home>\"\<CR>", 'xt')
85 call assert_equal("\"afoo\<CR>barb", getreg(':'))
86endfunc
Bram Moolenaara1891842017-02-04 21:34:31 +010087
Bram Moolenaar845e0ee2020-06-20 16:05:32 +020088" bracketed paste in Ex-mode
89func Test_paste_ex_mode()
90 unlet! foo
91 call feedkeys("Qlet foo=\"\<Esc>[200~foo\<CR>bar\<Esc>[201~\"\<CR>vi\<CR>", 'xt')
92 call assert_equal("foo\rbar", foo)
Bram Moolenaar806d0372022-01-25 20:45:16 +000093
94 " pasting more than 40 bytes
95 exe "norm Q\<PasteStart>0000000000000000000000000000000000000000000000000000000000000000000000\<C-C>"
Bram Moolenaar845e0ee2020-06-20 16:05:32 +020096endfunc
97
98func Test_paste_onechar()
99 new
100 let @f='abc'
101 call feedkeys("i\<C-R>\<Esc>[200~foo\<CR>bar\<Esc>[201~", 'xt')
102 call assert_equal("abc", getline(1))
103 close!
104endfunc
105
Bram Moolenaara1891842017-02-04 21:34:31 +0100106func Test_paste_visual_mode()
107 new
108 call setline(1, 'here are some words')
109 call feedkeys("0fsve\<Esc>[200~more\<Esc>[201~", 'xt')
110 call assert_equal('here are more words', getline(1))
111 call assert_equal('some', getreg('-'))
112
113 " include last char in the line
114 call feedkeys("0fwve\<Esc>[200~noises\<Esc>[201~", 'xt')
115 call assert_equal('here are more noises', getline(1))
116 call assert_equal('words', getreg('-'))
117
118 " exclude last char in the line
119 call setline(1, 'some words!')
120 call feedkeys("0fwve\<Esc>[200~noises\<Esc>[201~", 'xt')
121 call assert_equal('some noises!', getline(1))
122 call assert_equal('words', getreg('-'))
123
124 " multi-line selection
125 call setline(1, ['some words', 'and more'])
126 call feedkeys("0fwvj0fd\<Esc>[200~letters\<Esc>[201~", 'xt')
127 call assert_equal('some letters more', getline(1))
128 call assert_equal("words\nand", getreg('1'))
129
130 bwipe!
131endfunc
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200132
133func CheckCopyPaste()
134 call setline(1, ['copy this', ''])
135 normal 1G0"*y$
136 normal j"*p
137 call assert_equal('copy this', getline(2))
138endfunc
139
140func Test_xrestore()
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200141 CheckFeature xterm_clipboard
Bram Moolenaarf08b0eb2021-10-16 13:00:14 +0100142 let g:test_is_flaky = 1
143
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200144 let display = $DISPLAY
145 new
146 call CheckCopyPaste()
147
148 xrestore
149 call CheckCopyPaste()
150
151 exe "xrestore " .. display
152 call CheckCopyPaste()
153
Bram Moolenaard4aa83a2019-05-09 18:59:31 +0200154 bwipe!
155endfunc
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200156
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +0100157" Test for 'pastetoggle'
158func Test_pastetoggle()
159 new
160 set pastetoggle=<F4>
161 set nopaste
162 call feedkeys("iHello\<F4>", 'xt')
163 call assert_true(&paste)
164 call feedkeys("i\<F4>", 'xt')
165 call assert_false(&paste)
166 call assert_equal('Hello', getline(1))
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200167 " command-line completion for 'pastetoggle' value
168 call feedkeys(":set pastetoggle=\<Tab>\<C-B>\"\<CR>", 'xt')
169 call assert_equal('"set pastetoggle=<F4>', @:)
Bram Moolenaarf4fcedc2021-03-15 18:36:20 +0100170 set pastetoggle&
171 bwipe!
172endfunc
173
Yegappan Lakshmanan2d6d7182021-06-13 21:52:48 +0200174" Test for restoring option values when 'paste' is disabled
175func Test_paste_opt_restore()
176 set autoindent expandtab ruler showmatch
177 if has('rightleft')
178 set revins hkmap
179 endif
180 set smarttab softtabstop=3 textwidth=27 wrapmargin=12
181 if has('vartabs')
182 set varsofttabstop=10,20
183 endif
184
185 " enabling 'paste' should reset the above options
186 set paste
187 call assert_false(&autoindent)
188 call assert_false(&expandtab)
189 if has('rightleft')
190 call assert_false(&revins)
191 call assert_false(&hkmap)
192 endif
193 call assert_false(&ruler)
194 call assert_false(&showmatch)
195 call assert_false(&smarttab)
196 call assert_equal(0, &softtabstop)
197 call assert_equal(0, &textwidth)
198 call assert_equal(0, &wrapmargin)
199 if has('vartabs')
200 call assert_equal('', &varsofttabstop)
201 endif
202
203 " disabling 'paste' should restore the option values
204 set nopaste
205 call assert_true(&autoindent)
206 call assert_true(&expandtab)
207 if has('rightleft')
208 call assert_true(&revins)
209 call assert_true(&hkmap)
210 endif
211 call assert_true(&ruler)
212 call assert_true(&showmatch)
213 call assert_true(&smarttab)
214 call assert_equal(3, &softtabstop)
215 call assert_equal(27, &textwidth)
216 call assert_equal(12, &wrapmargin)
217 if has('vartabs')
218 call assert_equal('10,20', &varsofttabstop)
219 endif
220
221 set autoindent& expandtab& ruler& showmatch&
222 if has('rightleft')
223 set revins& hkmap&
224 endif
225 set smarttab& softtabstop& textwidth& wrapmargin&
226 if has('vartabs')
227 set varsofttabstop&
228 endif
229endfunc
230
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200231" vim: shiftwidth=2 sts=2 expandtab