blob: 5aaa743fadb896679eb07d8067fa5939066bc1cd [file] [log] [blame]
Bram Moolenaar2d1a2482016-08-14 15:32:11 +02001" Tests for mappings and abbreviations
2
Bram Moolenaar2d1a2482016-08-14 15:32:11 +02003func Test_abbreviation()
4 " abbreviation with 0x80 should work
5 inoreab чкпр vim
6 call feedkeys("Goчкпр \<Esc>", "xt")
7 call assert_equal('vim ', getline('$'))
8 iunab чкпр
9 set nomodified
10endfunc
11
12func Test_map_ctrl_c_insert()
13 " mapping of ctrl-c in Insert mode
14 set cpo-=< cpo-=k
15 inoremap <c-c> <ctrl-c>
16 cnoremap <c-c> dummy
17 cunmap <c-c>
18 call feedkeys("GoTEST2: CTRL-C |\<C-C>A|\<Esc>", "xt")
19 call assert_equal('TEST2: CTRL-C |<ctrl-c>A|', getline('$'))
20 unmap! <c-c>
21 set nomodified
22endfunc
23
24func Test_map_ctrl_c_visual()
25 " mapping of ctrl-c in Visual mode
26 vnoremap <c-c> :<C-u>$put ='vmap works'
27 call feedkeys("GV\<C-C>\<CR>", "xt")
28 call assert_equal('vmap works', getline('$'))
29 vunmap <c-c>
30 set nomodified
31endfunc
32
33func Test_map_langmap()
Bram Moolenaar920694c2016-08-21 17:45:02 +020034 if !has('langmap')
35 return
36 endif
37
38 " check langmap applies in normal mode
39 set langmap=+- nolangremap
40 new
41 call setline(1, ['a', 'b', 'c'])
42 2
43 call assert_equal('b', getline('.'))
44 call feedkeys("+", "xt")
45 call assert_equal('a', getline('.'))
46
47 " check no remapping
48 map x +
49 2
50 call feedkeys("x", "xt")
51 call assert_equal('c', getline('.'))
52
53 " check with remapping
54 set langremap
55 2
56 call feedkeys("x", "xt")
57 call assert_equal('a', getline('.'))
58
59 unmap x
60 bwipe!
61
62 " 'langnoremap' follows 'langremap' and vise versa
63 set langremap
64 set langnoremap
65 call assert_equal(0, &langremap)
66 set langremap
67 call assert_equal(0, &langnoremap)
68 set nolangremap
69 call assert_equal(1, &langnoremap)
70
Bram Moolenaarda9ce2c2016-09-02 19:34:10 +020071 " check default values
72 set langnoremap&
73 call assert_equal(0, &langnoremap)
74 call assert_equal(1, &langremap)
75 set langremap&
76 call assert_equal(0, &langnoremap)
77 call assert_equal(1, &langremap)
78
Bram Moolenaar920694c2016-08-21 17:45:02 +020079 " langmap should not apply in insert mode, 'langremap' doesn't matter
80 set langmap=+{ nolangremap
81 call feedkeys("Go+\<Esc>", "xt")
82 call assert_equal('+', getline('$'))
83 set langmap=+{ langremap
Bram Moolenaar2d1a2482016-08-14 15:32:11 +020084 call feedkeys("Go+\<Esc>", "xt")
85 call assert_equal('+', getline('$'))
86
Bram Moolenaar920694c2016-08-21 17:45:02 +020087 " langmap used for register name in insert mode.
88 call setreg('a', 'aaaa')
89 call setreg('b', 'bbbb')
90 call setreg('c', 'cccc')
91 set langmap=ab langremap
92 call feedkeys("Go\<C-R>a\<Esc>", "xt")
93 call assert_equal('bbbb', getline('$'))
94 call feedkeys("Go\<C-R>\<C-R>a\<Esc>", "xt")
95 call assert_equal('bbbb', getline('$'))
96 " mapping does not apply
97 imap c a
98 call feedkeys("Go\<C-R>c\<Esc>", "xt")
99 call assert_equal('cccc', getline('$'))
100 imap a c
101 call feedkeys("Go\<C-R>a\<Esc>", "xt")
102 call assert_equal('bbbb', getline('$'))
103
104 " langmap should not apply in Command-line mode
105 set langmap=+{ nolangremap
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200106 call feedkeys(":call append(line('$'), '+')\<CR>", "xt")
107 call assert_equal('+', getline('$'))
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200108
Bram Moolenaare90858d2017-02-01 17:24:34 +0100109 iunmap a
110 iunmap c
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200111 set nomodified
112endfunc
113
114func Test_map_feedkeys()
115 " issue #212 (feedkeys insert mapping at current position)
116 nnoremap . :call feedkeys(".", "in")<cr>
117 call setline('$', ['a b c d', 'a b c d'])
118 $-1
119 call feedkeys("0qqdw.ifoo\<Esc>qj0@q\<Esc>", "xt")
120 call assert_equal(['fooc d', 'fooc d'], getline(line('$') - 1, line('$')))
Bram Moolenaare90858d2017-02-01 17:24:34 +0100121 nunmap .
Bram Moolenaar2d1a2482016-08-14 15:32:11 +0200122 set nomodified
123endfunc
124
125func Test_map_cursor()
126 " <c-g>U<cursor> works only within a single line
127 imapclear
128 imap ( ()<c-g>U<left>
129 call feedkeys("G2o\<Esc>ki\<CR>Test1: text with a (here some more text\<Esc>k.", "xt")
130 call assert_equal('Test1: text with a (here some more text)', getline(line('$') - 2))
131 call assert_equal('Test1: text with a (here some more text)', getline(line('$') - 1))
132
133 " test undo
134 call feedkeys("G2o\<Esc>ki\<CR>Test2: text wit a (here some more text [und undo]\<C-G>u\<Esc>k.u", "xt")
135 call assert_equal('', getline(line('$') - 2))
136 call assert_equal('Test2: text wit a (here some more text [und undo])', getline(line('$') - 1))
137 set nomodified
138 imapclear
139endfunc
140
141" This isn't actually testing a mapping, but similar use of CTRL-G U as above.
142func Test_break_undo()
143 :set whichwrap=<,>,[,]
144 call feedkeys("G4o2k", "xt")
145 exe ":norm! iTest3: text with a (parenthesis here\<C-G>U\<Right>new line here\<esc>\<up>\<up>."
146 call assert_equal('new line here', getline(line('$') - 3))
147 call assert_equal('Test3: text with a (parenthesis here', getline(line('$') - 2))
148 call assert_equal('new line here', getline(line('$') - 1))
149 set nomodified
150endfunc
Bram Moolenaar35a4cfa2016-08-14 16:07:48 +0200151
152func Test_map_meta_quotes()
153 imap <M-"> foo
154 call feedkeys("Go-\<M-\">-\<Esc>", "xt")
155 call assert_equal("-foo-", getline('$'))
156 set nomodified
157 iunmap <M-">
158endfunc
Bram Moolenaar878c2632017-04-01 15:15:52 +0200159
160func Test_abbr_after_line_join()
161 new
162 abbr foo bar
163 set backspace=indent,eol,start
164 exe "normal o\<BS>foo "
165 call assert_equal("bar ", getline(1))
166 bwipe!
167 unabbr foo
168 set backspace&
169endfunc
Bram Moolenaarb7637c42017-04-23 18:49:36 +0200170
171func Test_map_timeout()
172 nnoremap aaaa :let got_aaaa = 1<CR>
173 nnoremap bb :let got_bb = 1<CR>
174 nmap b aaa
175 new
176 func ExitInsert(timer)
177 let g:line = getline(1)
178 call feedkeys("\<Esc>", "t")
179 endfunc
180 set timeout timeoutlen=200
181 call timer_start(300, 'ExitInsert')
182 " After the 'b' Vim waits for another character to see if it matches 'bb'.
183 " When it times out it is expanded to "aaa", but there is no wait for
184 " "aaaa". Can't check that reliably though.
185 call feedkeys("b", "xt!")
186 call assert_equal("aa", g:line)
187 call assert_false(exists('got_aaa'))
188 call assert_false(exists('got_bb'))
189
190 bwipe!
191 nunmap aaaa
192 nunmap bb
193 nunmap b
194 set timeoutlen&
195 delfunc ExitInsert
196endfunc
Bram Moolenaarc3c3e692018-04-26 22:30:33 +0200197
198func Test_abbreviation_CR()
199 new
200 func Eatchar(pat)
201 let c = nr2char(getchar(0))
202 return (c =~ a:pat) ? '' : c
203 endfunc
204 iabbrev <buffer><silent> ~~7 <c-r>=repeat('~', 7)<CR><c-r>=Eatchar('\s')<cr>
205 call feedkeys("GA~~7 \<esc>", 'xt')
206 call assert_equal('~~~~~~~', getline('$'))
207 %d
208 call feedkeys("GA~~7\<cr>\<esc>", 'xt')
209 call assert_equal(['~~~~~~~', ''], getline(1,'$'))
210 delfunc Eatchar
211 bw!
212endfunc
Bram Moolenaar5e3423d2018-05-13 18:36:27 +0200213
214func Test_cabbr_visual_mode()
215 cabbr s su
216 call feedkeys(":s \<c-B>\"\<CR>", 'itx')
217 call assert_equal('"su ', getreg(':'))
218 call feedkeys(":'<,'>s \<c-B>\"\<CR>", 'itx')
219 let expected = '"'. "'<,'>su "
220 call assert_equal(expected, getreg(':'))
221 call feedkeys(": '<,'>s \<c-B>\"\<CR>", 'itx')
222 let expected = '" '. "'<,'>su "
223 call assert_equal(expected, getreg(':'))
224 call feedkeys(":'a,'bs \<c-B>\"\<CR>", 'itx')
225 let expected = '"'. "'a,'bsu "
226 call assert_equal(expected, getreg(':'))
227 cunabbr s
228endfunc
Bram Moolenaar5976f8f2018-12-27 23:44:44 +0100229
230func Test_motionforce_omap()
231 func GetCommand()
232 let g:m=mode(1)
233 let [g:lnum1, g:col1] = searchpos('-', 'Wb')
234 if g:lnum1 == 0
235 return "\<Esc>"
236 endif
237 let [g:lnum2, g:col2] = searchpos('-', 'W')
238 if g:lnum2 == 0
239 return "\<Esc>"
240 endif
241 return ":call Select()\<CR>"
242 endfunc
243 func Select()
244 call cursor([g:lnum1, g:col1])
245 exe "normal! 1 ". (strlen(g:m) == 2 ? 'v' : g:m[2])
246 call cursor([g:lnum2, g:col2])
247 execute "normal! \<BS>"
248 endfunc
249 new
250 onoremap <buffer><expr> i- GetCommand()
251 " 1) default omap mapping
252 %d_
253 call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
254 call cursor(2, 1)
255 norm di-
256 call assert_equal('no', g:m)
257 call assert_equal(['aaa -- eee'], getline(1, '$'))
258 " 2) forced characterwise operation
259 %d_
260 call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
261 call cursor(2, 1)
262 norm dvi-
263 call assert_equal('nov', g:m)
264 call assert_equal(['aaa -- eee'], getline(1, '$'))
265 " 3) forced linewise operation
266 %d_
267 call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
268 call cursor(2, 1)
269 norm dVi-
270 call assert_equal('noV', g:m)
271 call assert_equal([''], getline(1, '$'))
272 " 4) forced blockwise operation
273 %d_
274 call setline(1, ['aaa - bbb', 'x', 'ddd - eee'])
275 call cursor(2, 1)
276 exe "norm d\<C-V>i-"
277 call assert_equal("no\<C-V>", g:m)
278 call assert_equal(['aaabbb', 'x', 'dddeee'], getline(1, '$'))
279 bwipe!
280 delfunc Select
281 delfunc GetCommand
282endfunc