xref: /vim-8.2.3635/src/testdir/test_mapping.vim (revision bb76f24a)
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  set nomodified
114endfunc
115
116func Test_map_feedkeys()
117  " issue #212 (feedkeys insert mapping at current position)
118  nnoremap . :call feedkeys(".", "in")<cr>
119  call setline('$', ['a b c d', 'a b c d'])
120  $-1
121  call feedkeys("0qqdw.ifoo\<Esc>qj0@q\<Esc>", "xt")
122  call assert_equal(['fooc d', 'fooc d'], getline(line('$') - 1, line('$')))
123  unmap .
124  set nomodified
125endfunc
126
127func Test_map_cursor()
128  " <c-g>U<cursor> works only within a single line
129  imapclear
130  imap ( ()<c-g>U<left>
131  call feedkeys("G2o\<Esc>ki\<CR>Test1: text with a (here some more text\<Esc>k.", "xt")
132  call assert_equal('Test1: text with a (here some more text)', getline(line('$') - 2))
133  call assert_equal('Test1: text with a (here some more text)', getline(line('$') - 1))
134
135  " test undo
136  call feedkeys("G2o\<Esc>ki\<CR>Test2: text wit a (here some more text [und undo]\<C-G>u\<Esc>k.u", "xt")
137  call assert_equal('', getline(line('$') - 2))
138  call assert_equal('Test2: text wit a (here some more text [und undo])', getline(line('$') - 1))
139  set nomodified
140  imapclear
141endfunc
142
143" This isn't actually testing a mapping, but similar use of CTRL-G U as above.
144func Test_break_undo()
145  :set whichwrap=<,>,[,]
146  call feedkeys("G4o2k", "xt")
147  exe ":norm! iTest3: text with a (parenthesis here\<C-G>U\<Right>new line here\<esc>\<up>\<up>."
148  call assert_equal('new line here', getline(line('$') - 3))
149  call assert_equal('Test3: text with a (parenthesis here', getline(line('$') - 2))
150  call assert_equal('new line here', getline(line('$') - 1))
151  set nomodified
152endfunc
153
154func Test_map_meta_quotes()
155  imap <M-"> foo
156  call feedkeys("Go-\<M-\">-\<Esc>", "xt")
157  call assert_equal("-foo-", getline('$'))
158  set nomodified
159  iunmap <M-">
160endfunc
161