xref: /vim-8.2.3635/src/testdir/test_mapping.vim (revision b1c9198a)
1" Tests for mappings and abbreviations
2
3if !has('multi_byte')
4  finish
5endif
6
7func Test_abbreviation()
8  " abbreviation with 0x80 should work
9  inoreab чкпр   vim
10  call feedkeys("Goчкпр \<Esc>", "xt")
11  call assert_equal('vim ', getline('$'))
12  iunab чкпр
13  set nomodified
14endfunc
15
16func Test_map_ctrl_c_insert()
17  " mapping of ctrl-c in Insert mode
18  set cpo-=< cpo-=k
19  inoremap <c-c> <ctrl-c>
20  cnoremap <c-c> dummy
21  cunmap <c-c>
22  call feedkeys("GoTEST2: CTRL-C |\<C-C>A|\<Esc>", "xt")
23  call assert_equal('TEST2: CTRL-C |<ctrl-c>A|', getline('$'))
24  unmap! <c-c>
25  set nomodified
26endfunc
27
28func Test_map_ctrl_c_visual()
29  " mapping of ctrl-c in Visual mode
30  vnoremap <c-c> :<C-u>$put ='vmap works'
31  call feedkeys("GV\<C-C>\<CR>", "xt")
32  call assert_equal('vmap works', getline('$'))
33  vunmap <c-c>
34  set nomodified
35endfunc
36
37func Test_map_langmap()
38  if !has('langmap')
39    return
40  endif
41
42  " check langmap applies in normal mode
43  set langmap=+- nolangremap
44  new
45  call setline(1, ['a', 'b', 'c'])
46  2
47  call assert_equal('b', getline('.'))
48  call feedkeys("+", "xt")
49  call assert_equal('a', getline('.'))
50
51  " check no remapping
52  map x +
53  2
54  call feedkeys("x", "xt")
55  call assert_equal('c', getline('.'))
56
57  " check with remapping
58  set langremap
59  2
60  call feedkeys("x", "xt")
61  call assert_equal('a', getline('.'))
62
63  unmap x
64  bwipe!
65
66  " 'langnoremap' follows 'langremap' and vise versa
67  set langremap
68  set langnoremap
69  call assert_equal(0, &langremap)
70  set langremap
71  call assert_equal(0, &langnoremap)
72  set nolangremap
73  call assert_equal(1, &langnoremap)
74
75  " check default values
76  set langnoremap&
77  call assert_equal(0, &langnoremap)
78  call assert_equal(1, &langremap)
79  set langremap&
80  call assert_equal(0, &langnoremap)
81  call assert_equal(1, &langremap)
82
83  " langmap should not apply in insert mode, 'langremap' doesn't matter
84  set langmap=+{ nolangremap
85  call feedkeys("Go+\<Esc>", "xt")
86  call assert_equal('+', getline('$'))
87  set langmap=+{ langremap
88  call feedkeys("Go+\<Esc>", "xt")
89  call assert_equal('+', getline('$'))
90
91  " langmap used for register name in insert mode.
92  call setreg('a', 'aaaa')
93  call setreg('b', 'bbbb')
94  call setreg('c', 'cccc')
95  set langmap=ab langremap
96  call feedkeys("Go\<C-R>a\<Esc>", "xt")
97  call assert_equal('bbbb', getline('$'))
98  call feedkeys("Go\<C-R>\<C-R>a\<Esc>", "xt")
99  call assert_equal('bbbb', getline('$'))
100  " mapping does not apply
101  imap c a
102  call feedkeys("Go\<C-R>c\<Esc>", "xt")
103  call assert_equal('cccc', getline('$'))
104  imap a c
105  call feedkeys("Go\<C-R>a\<Esc>", "xt")
106  call assert_equal('bbbb', getline('$'))
107
108  " langmap should not apply in Command-line mode
109  set langmap=+{ nolangremap
110  call feedkeys(":call append(line('$'), '+')\<CR>", "xt")
111  call assert_equal('+', getline('$'))
112
113  iunmap a
114  iunmap c
115  set nomodified
116endfunc
117
118func Test_map_feedkeys()
119  " issue #212 (feedkeys insert mapping at current position)
120  nnoremap . :call feedkeys(".", "in")<cr>
121  call setline('$', ['a b c d', 'a b c d'])
122  $-1
123  call feedkeys("0qqdw.ifoo\<Esc>qj0@q\<Esc>", "xt")
124  call assert_equal(['fooc d', 'fooc d'], getline(line('$') - 1, line('$')))
125  nunmap .
126  set nomodified
127endfunc
128
129func Test_map_cursor()
130  " <c-g>U<cursor> works only within a single line
131  imapclear
132  imap ( ()<c-g>U<left>
133  call feedkeys("G2o\<Esc>ki\<CR>Test1: text with a (here some more text\<Esc>k.", "xt")
134  call assert_equal('Test1: text with a (here some more text)', getline(line('$') - 2))
135  call assert_equal('Test1: text with a (here some more text)', getline(line('$') - 1))
136
137  " test undo
138  call feedkeys("G2o\<Esc>ki\<CR>Test2: text wit a (here some more text [und undo]\<C-G>u\<Esc>k.u", "xt")
139  call assert_equal('', getline(line('$') - 2))
140  call assert_equal('Test2: text wit a (here some more text [und undo])', getline(line('$') - 1))
141  set nomodified
142  imapclear
143endfunc
144
145" This isn't actually testing a mapping, but similar use of CTRL-G U as above.
146func Test_break_undo()
147  :set whichwrap=<,>,[,]
148  call feedkeys("G4o2k", "xt")
149  exe ":norm! iTest3: text with a (parenthesis here\<C-G>U\<Right>new line here\<esc>\<up>\<up>."
150  call assert_equal('new line here', getline(line('$') - 3))
151  call assert_equal('Test3: text with a (parenthesis here', getline(line('$') - 2))
152  call assert_equal('new line here', getline(line('$') - 1))
153  set nomodified
154endfunc
155
156func Test_map_meta_quotes()
157  imap <M-"> foo
158  call feedkeys("Go-\<M-\">-\<Esc>", "xt")
159  call assert_equal("-foo-", getline('$'))
160  set nomodified
161  iunmap <M-">
162endfunc
163
164func Test_abbr_after_line_join()
165  new
166  abbr foo bar
167  set backspace=indent,eol,start
168  exe "normal o\<BS>foo "
169  call assert_equal("bar ", getline(1))
170  bwipe!
171  unabbr foo
172  set backspace&
173endfunc
174
175func Test_map_timeout()
176  nnoremap aaaa :let got_aaaa = 1<CR>
177  nnoremap bb :let got_bb = 1<CR>
178  nmap b aaa
179  new
180  func ExitInsert(timer)
181    let g:line = getline(1)
182    call feedkeys("\<Esc>", "t")
183  endfunc
184  set timeout timeoutlen=200
185  call timer_start(300, 'ExitInsert')
186  " After the 'b' Vim waits for another character to see if it matches 'bb'.
187  " When it times out it is expanded to "aaa", but there is no wait for
188  " "aaaa".  Can't check that reliably though.
189  call feedkeys("b", "xt!")
190  call assert_equal("aa", g:line)
191  call assert_false(exists('got_aaa'))
192  call assert_false(exists('got_bb'))
193
194  bwipe!
195  nunmap aaaa
196  nunmap bb
197  nunmap b
198  set timeoutlen&
199  delfunc ExitInsert
200endfunc
201
202func Test_abbreviation_CR()
203  new
204  func Eatchar(pat)
205    let c = nr2char(getchar(0))
206    return (c =~ a:pat) ? '' : c
207  endfunc
208  iabbrev <buffer><silent> ~~7 <c-r>=repeat('~', 7)<CR><c-r>=Eatchar('\s')<cr>
209  call feedkeys("GA~~7 \<esc>", 'xt')
210  call assert_equal('~~~~~~~', getline('$'))
211  %d
212  call feedkeys("GA~~7\<cr>\<esc>", 'xt')
213  call assert_equal(['~~~~~~~', ''], getline(1,'$'))
214  delfunc Eatchar
215  bw!
216endfunc
217
218func Test_cabbr_visual_mode()
219  cabbr s su
220  call feedkeys(":s \<c-B>\"\<CR>", 'itx')
221  call assert_equal('"su ', getreg(':'))
222  call feedkeys(":'<,'>s \<c-B>\"\<CR>", 'itx')
223  let expected = '"'. "'<,'>su "
224  call assert_equal(expected, getreg(':'))
225  call feedkeys(":  '<,'>s \<c-B>\"\<CR>", 'itx')
226  let expected = '"  '. "'<,'>su "
227  call assert_equal(expected, getreg(':'))
228  call feedkeys(":'a,'bs \<c-B>\"\<CR>", 'itx')
229  let expected = '"'. "'a,'bsu "
230  call assert_equal(expected, getreg(':'))
231  cunabbr s
232endfunc
233