xref: /vim-8.2.3635/src/testdir/test_mapping.vim (revision cb03397a)
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  " langmap should not apply in insert mode, 'langremap' doesn't matter
76  set langmap=+{ nolangremap
77  call feedkeys("Go+\<Esc>", "xt")
78  call assert_equal('+', getline('$'))
79  set langmap=+{ langremap
80  call feedkeys("Go+\<Esc>", "xt")
81  call assert_equal('+', getline('$'))
82
83  " langmap used for register name in insert mode.
84  call setreg('a', 'aaaa')
85  call setreg('b', 'bbbb')
86  call setreg('c', 'cccc')
87  set langmap=ab langremap
88  call feedkeys("Go\<C-R>a\<Esc>", "xt")
89  call assert_equal('bbbb', getline('$'))
90  call feedkeys("Go\<C-R>\<C-R>a\<Esc>", "xt")
91  call assert_equal('bbbb', getline('$'))
92  " mapping does not apply
93  imap c a
94  call feedkeys("Go\<C-R>c\<Esc>", "xt")
95  call assert_equal('cccc', getline('$'))
96  imap a c
97  call feedkeys("Go\<C-R>a\<Esc>", "xt")
98  call assert_equal('bbbb', getline('$'))
99
100  " langmap should not apply in Command-line mode
101  set langmap=+{ nolangremap
102  call feedkeys(":call append(line('$'), '+')\<CR>", "xt")
103  call assert_equal('+', getline('$'))
104
105  set nomodified
106endfunc
107
108func Test_map_feedkeys()
109  " issue #212 (feedkeys insert mapping at current position)
110  nnoremap . :call feedkeys(".", "in")<cr>
111  call setline('$', ['a b c d', 'a b c d'])
112  $-1
113  call feedkeys("0qqdw.ifoo\<Esc>qj0@q\<Esc>", "xt")
114  call assert_equal(['fooc d', 'fooc d'], getline(line('$') - 1, line('$')))
115  unmap .
116  set nomodified
117endfunc
118
119func Test_map_cursor()
120  " <c-g>U<cursor> works only within a single line
121  imapclear
122  imap ( ()<c-g>U<left>
123  call feedkeys("G2o\<Esc>ki\<CR>Test1: text with a (here some more text\<Esc>k.", "xt")
124  call assert_equal('Test1: text with a (here some more text)', getline(line('$') - 2))
125  call assert_equal('Test1: text with a (here some more text)', getline(line('$') - 1))
126
127  " test undo
128  call feedkeys("G2o\<Esc>ki\<CR>Test2: text wit a (here some more text [und undo]\<C-G>u\<Esc>k.u", "xt")
129  call assert_equal('', getline(line('$') - 2))
130  call assert_equal('Test2: text wit a (here some more text [und undo])', getline(line('$') - 1))
131  set nomodified
132  imapclear
133endfunc
134
135" This isn't actually testing a mapping, but similar use of CTRL-G U as above.
136func Test_break_undo()
137  :set whichwrap=<,>,[,]
138  call feedkeys("G4o2k", "xt")
139  exe ":norm! iTest3: text with a (parenthesis here\<C-G>U\<Right>new line here\<esc>\<up>\<up>."
140  call assert_equal('new line here', getline(line('$') - 3))
141  call assert_equal('Test3: text with a (parenthesis here', getline(line('$') - 2))
142  call assert_equal('new line here', getline(line('$') - 1))
143  set nomodified
144endfunc
145
146func Test_map_meta_quotes()
147  imap <M-"> foo
148  call feedkeys("Go-\<M-\">-\<Esc>", "xt")
149  call assert_equal("-foo-", getline('$'))
150  set nomodified
151  iunmap <M-">
152endfunc
153