blob: c3c4725a1e528db19763679b1c508e6f38778465 [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for 'iminsert'
2
Bram Moolenaar6315a9a2017-11-25 15:20:02 +01003source view_util.vim
Bram Moolenaare2a3f362019-12-07 21:40:48 +01004source check.vim
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +00005source vim9.vim
Bram Moolenaar6315a9a2017-11-25 15:20:02 +01006
7let s:imactivatefunc_called = 0
8let s:imstatusfunc_called = 0
Bram Moolenaara3a12462019-09-07 15:08:38 +02009let s:imstatus_active = 0
Bram Moolenaar6315a9a2017-11-25 15:20:02 +010010
11func IM_activatefunc(active)
12 let s:imactivatefunc_called = 1
Bram Moolenaar845e0ee2020-06-20 16:05:32 +020013 let s:imstatus_active = a:active
Bram Moolenaar6315a9a2017-11-25 15:20:02 +010014endfunc
15
16func IM_statusfunc()
17 let s:imstatusfunc_called = 1
Bram Moolenaara3a12462019-09-07 15:08:38 +020018 return s:imstatus_active
Bram Moolenaar6315a9a2017-11-25 15:20:02 +010019endfunc
20
21func Test_iminsert2()
Bram Moolenaara3a12462019-09-07 15:08:38 +020022 let s:imactivatefunc_called = 0
23 let s:imstatusfunc_called = 0
24
Bram Moolenaar6315a9a2017-11-25 15:20:02 +010025 set imactivatefunc=IM_activatefunc
26 set imstatusfunc=IM_statusfunc
27 set iminsert=2
28 normal! i
29 set iminsert=0
30 set imactivatefunc=
31 set imstatusfunc=
Bram Moolenaar2877d332017-11-26 14:56:16 +010032
Bram Moolenaarc19fd912020-07-02 20:59:05 +020033 let expected = (has('win32') && has('gui_running')) ? 0 : 1
Bram Moolenaar2877d332017-11-26 14:56:16 +010034 call assert_equal(expected, s:imactivatefunc_called)
35 call assert_equal(expected, s:imstatusfunc_called)
Bram Moolenaar6315a9a2017-11-25 15:20:02 +010036endfunc
Bram Moolenaara3a12462019-09-07 15:08:38 +020037
Bram Moolenaare2a3f362019-12-07 21:40:48 +010038func Test_getimstatus()
39 if has('win32')
40 CheckFeature multi_byte_ime
Bram Moolenaarbe7529e2020-08-13 21:05:39 +020041 else
Bram Moolenaare2a3f362019-12-07 21:40:48 +010042 CheckFeature xim
43 endif
Bram Moolenaarc19fd912020-07-02 20:59:05 +020044 if has('win32') && has('gui_running')
Bram Moolenaara3a12462019-09-07 15:08:38 +020045 set imactivatefunc=
46 set imstatusfunc=
47 else
48 set imactivatefunc=IM_activatefunc
49 set imstatusfunc=IM_statusfunc
50 let s:imstatus_active = 0
51 endif
52
53 new
54 set iminsert=2
55 call feedkeys("i\<C-R>=getimstatus()\<CR>\<ESC>", 'nx')
56 call assert_equal('1', getline(1))
57 set iminsert=0
58 call feedkeys("o\<C-R>=getimstatus()\<CR>\<ESC>", 'nx')
59 call assert_equal('0', getline(2))
60 bw!
61
62 set imactivatefunc=
63 set imstatusfunc=
64endfunc
Bram Moolenaar0546d7d2020-03-01 16:53:09 +010065
Bram Moolenaar1671f442020-03-10 07:48:13 +010066" Test for using an lmap in insert mode
67func Test_lmap_in_insert_mode()
68 new
69 call setline(1, 'abc')
70 lmap { w
71 set iminsert=1
72 call feedkeys('r{', 'xt')
73 call assert_equal('wbc', getline(1))
74 set iminsert=2
75 call feedkeys('$r{', 'xt')
76 call assert_equal('wb{', getline(1))
77 call setline(1, 'vim web')
78 set iminsert=1
79 call feedkeys('0f{', 'xt')
80 call assert_equal(5, col('.'))
81 set iminsert&
82 lunmap {
83 close!
84endfunc
85
Bram Moolenaar845e0ee2020-06-20 16:05:32 +020086" Test for using CTRL-^ to toggle iminsert in insert mode
87func Test_iminsert_toggle()
88 CheckGui
89 if has('win32')
90 CheckFeature multi_byte_ime
Bram Moolenaarbe7529e2020-08-13 21:05:39 +020091 else
Bram Moolenaar845e0ee2020-06-20 16:05:32 +020092 CheckFeature xim
93 endif
94 if has('gui_running') && !has('win32')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +020095 throw 'Skipped: works only in Win32 GUI version (for some reason)'
Bram Moolenaar845e0ee2020-06-20 16:05:32 +020096 endif
97 new
98 let save_imdisable = &imdisable
99 let save_iminsert = &iminsert
100 set noimdisable
101 set iminsert=0
102 exe "normal i\<C-^>"
103 call assert_equal(2, &iminsert)
104 exe "normal i\<C-^>"
105 call assert_equal(0, &iminsert)
106 let &iminsert = save_iminsert
107 let &imdisable = save_imdisable
108 close!
109endfunc
110
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000111" Test for different ways of setting the 'imactivatefunc' and 'imstatusfunc'
112" options
113func Test_imactivatefunc_imstatusfunc_callback()
114 CheckNotMSWindows
115 func IMactivatefunc1(active)
116 let g:IMactivatefunc_called += 1
117 endfunc
118 func IMstatusfunc1()
119 let g:IMstatusfunc_called += 1
120 return 1
121 endfunc
122 let g:IMactivatefunc_called = 0
123 let g:IMstatusfunc_called = 0
124 set iminsert=2
125
126 " Test for using a function()
127 set imactivatefunc=function('IMactivatefunc1')
128 set imstatusfunc=function('IMstatusfunc1')
129 normal! i
130
131 " Using a funcref variable to set 'completefunc'
132 let Fn1 = function('IMactivatefunc1')
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000133 let &imactivatefunc = Fn1
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000134 let Fn2 = function('IMstatusfunc1')
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000135 let &imstatusfunc = Fn2
136 normal! i
137
138 " Using a string(funcref variable) to set 'completefunc'
139 let &imactivatefunc = string(Fn1)
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000140 let &imstatusfunc = string(Fn2)
141 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000142
143 " Test for using a funcref()
144 set imactivatefunc=funcref('IMactivatefunc1')
145 set imstatusfunc=funcref('IMstatusfunc1')
146 normal! i
147
148 " Using a funcref variable to set 'imactivatefunc'
149 let Fn1 = funcref('IMactivatefunc1')
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000150 let &imactivatefunc = Fn1
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000151 let Fn2 = funcref('IMstatusfunc1')
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000152 let &imstatusfunc = Fn2
153 normal! i
154
155 " Using a string(funcref variable) to set 'imactivatefunc'
156 let &imactivatefunc = string(Fn1)
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000157 let &imstatusfunc = string(Fn2)
158 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000159
160 " Test for using a lambda function
161 set imactivatefunc={a\ ->\ IMactivatefunc1(a)}
162 set imstatusfunc={\ ->\ IMstatusfunc1()}
163 normal! i
164
165 " Set 'imactivatefunc' and 'imstatusfunc' to a lambda expression
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000166 let &imactivatefunc = {a -> IMactivatefunc1(a)}
167 let &imstatusfunc = { -> IMstatusfunc1()}
168 normal! i
169
170 " Set 'imactivatefunc' and 'imstatusfunc' to a string(lambda expression)
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000171 let &imactivatefunc = '{a -> IMactivatefunc1(a)}'
172 let &imstatusfunc = '{ -> IMstatusfunc1()}'
173 normal! i
174
175 " Set 'imactivatefunc' 'imstatusfunc' to a variable with a lambda expression
176 let Lambda1 = {a -> IMactivatefunc1(a)}
177 let Lambda2 = { -> IMstatusfunc1()}
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000178 let &imactivatefunc = Lambda1
179 let &imstatusfunc = Lambda2
180 normal! i
181
182 " Set 'imactivatefunc' 'imstatusfunc' to a string(variable with a lambda
183 " expression)
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000184 let &imactivatefunc = string(Lambda1)
185 let &imstatusfunc = string(Lambda2)
186 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000187
188 " Test for clearing the 'completefunc' option
189 set imactivatefunc='' imstatusfunc=''
190 set imactivatefunc& imstatusfunc&
191
192 call assert_fails("set imactivatefunc=function('abc')", "E700:")
193 call assert_fails("set imstatusfunc=function('abc')", "E700:")
194 call assert_fails("set imactivatefunc=funcref('abc')", "E700:")
195 call assert_fails("set imstatusfunc=funcref('abc')", "E700:")
196
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000197 call assert_equal(11, g:IMactivatefunc_called)
198 call assert_equal(22, g:IMstatusfunc_called)
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000199
200 " Vim9 tests
201 let lines =<< trim END
202 vim9script
203
204 # Test for using function()
205 def IMactivatefunc1(active: number): any
206 g:IMactivatefunc_called += 1
207 return 1
208 enddef
209 def IMstatusfunc1(): number
210 g:IMstatusfunc_called += 1
211 return 1
212 enddef
213 g:IMactivatefunc_called = 0
214 g:IMstatusfunc_called = 0
215 set iminsert=2
216 set imactivatefunc=function('IMactivatefunc1')
217 set imstatusfunc=function('IMstatusfunc1')
218 normal! i
219
220 # Test for using a lambda
221 &imactivatefunc = '(a) => IMactivatefunc1(a)'
222 &imstatusfunc = '() => IMstatusfunc1()'
223 normal! i
224
225 # Test for using a variable with a lambda expression
226 var Fn1: func = (active) => {
227 g:IMactivatefunc_called += 1
228 return 1
229 }
230 var Fn2: func = () => {
231 g:IMstatusfunc_called += 1
232 return 1
233 }
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000234 &imactivatefunc = Fn1
235 &imstatusfunc = Fn2
236 normal! i
237
238 # Test for using a string(variable with a lambda expression)
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000239 &imactivatefunc = string(Fn1)
240 &imstatusfunc = string(Fn2)
241 normal! i
242
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000243 assert_equal(4, g:IMactivatefunc_called)
244 assert_equal(8, g:IMstatusfunc_called)
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000245
246 set iminsert=0
247 set imactivatefunc=
248 set imstatusfunc=
249 END
250 call CheckScriptSuccess(lines)
251
252 " Using Vim9 lambda expression in legacy context should fail
253 set imactivatefunc=(a)\ =>\ IMactivatefunc1(a)
254 set imstatusfunc=IMstatusfunc1
255 call assert_fails('normal! i', 'E117:')
256 set imactivatefunc=IMactivatefunc1
257 set imstatusfunc=()\ =>\ IMstatusfunc1(a)
258 call assert_fails('normal! i', 'E117:')
259
260 " cleanup
261 delfunc IMactivatefunc1
262 delfunc IMstatusfunc1
263 set iminsert=0
264 set imactivatefunc=
265 set imstatusfunc=
266
267 %bw!
268endfunc
269
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100270" vim: shiftwidth=2 sts=2 expandtab