blob: e5ded5866c5364078f548a6e74afeffc51a09b40 [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
Bram Moolenaar62aec932022-01-29 21:45:34 +00005import './vim9.vim' as v9
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
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000122 set iminsert=2
123
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000124 let lines =<< trim END
125 LET g:IMactivatefunc_called = 0
126 LET g:IMstatusfunc_called = 0
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000127
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000128 #" Test for using a function name
129 LET &imactivatefunc = 'g:IMactivatefunc1'
130 LET &imstatusfunc = 'g:IMstatusfunc1'
131 normal! i
132
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000133 #" Test for using a function()
134 set imactivatefunc=function('g:IMactivatefunc1')
135 set imstatusfunc=function('g:IMstatusfunc1')
136 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000137
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000138 #" Using a funcref variable to set 'completefunc'
139 VAR Fn1 = function('g:IMactivatefunc1')
140 LET &imactivatefunc = Fn1
141 VAR Fn2 = function('g:IMstatusfunc1')
142 LET &imstatusfunc = Fn2
143 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000144
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000145 #" Using a string(funcref variable) to set 'completefunc'
146 LET &imactivatefunc = string(Fn1)
147 LET &imstatusfunc = string(Fn2)
148 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000149
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000150 #" Test for using a funcref()
151 set imactivatefunc=funcref('g:IMactivatefunc1')
152 set imstatusfunc=funcref('g:IMstatusfunc1')
153 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000154
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000155 #" Using a funcref variable to set 'imactivatefunc'
156 LET Fn1 = funcref('g:IMactivatefunc1')
157 LET &imactivatefunc = Fn1
158 LET Fn2 = funcref('g:IMstatusfunc1')
159 LET &imstatusfunc = Fn2
160 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000161
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000162 #" Using a string(funcref variable) to set 'imactivatefunc'
163 LET &imactivatefunc = string(Fn1)
164 LET &imstatusfunc = string(Fn2)
165 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000166
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000167 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +0000168 VAR optval = "LSTART a LMIDDLE g:IMactivatefunc1(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000169 LET optval = substitute(optval, ' ', '\\ ', 'g')
170 exe "set imactivatefunc=" .. optval
Bram Moolenaar62aec932022-01-29 21:45:34 +0000171 LET optval = "LSTART LMIDDLE g:IMstatusfunc1() LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000172 LET optval = substitute(optval, ' ', '\\ ', 'g')
173 exe "set imstatusfunc=" .. optval
174 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000175
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000176 #" Set 'imactivatefunc' and 'imstatusfunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +0000177 LET &imactivatefunc = LSTART a LMIDDLE g:IMactivatefunc1(a) LEND
178 LET &imstatusfunc = LSTART LMIDDLE g:IMstatusfunc1() LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000179 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000180
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000181 #" Set 'imactivatefunc' and 'imstatusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +0000182 LET &imactivatefunc = 'LSTART a LMIDDLE g:IMactivatefunc1(a) LEND'
183 LET &imstatusfunc = 'LSTART LMIDDLE g:IMstatusfunc1() LEND'
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000184 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000185
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000186 #" Set 'imactivatefunc' 'imstatusfunc' to a variable with a lambda
187 #" expression
Bram Moolenaar62aec932022-01-29 21:45:34 +0000188 VAR Lambda1 = LSTART a LMIDDLE g:IMactivatefunc1(a) LEND
189 VAR Lambda2 = LSTART LMIDDLE g:IMstatusfunc1() LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000190 LET &imactivatefunc = Lambda1
191 LET &imstatusfunc = Lambda2
192 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000193
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000194 #" Set 'imactivatefunc' 'imstatusfunc' to a string(variable with a lambda
195 #" expression)
196 LET &imactivatefunc = string(Lambda1)
197 LET &imstatusfunc = string(Lambda2)
198 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000199
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000200 #" Test for clearing the 'completefunc' option
201 set imactivatefunc='' imstatusfunc=''
202 set imactivatefunc& imstatusfunc&
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000203
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000204 set imactivatefunc=g:IMactivatefunc1
205 set imstatusfunc=g:IMstatusfunc1
206 call assert_fails("set imactivatefunc=function('abc')", "E700:")
207 call assert_fails("set imstatusfunc=function('abc')", "E700:")
208 call assert_fails("set imactivatefunc=funcref('abc')", "E700:")
209 call assert_fails("set imstatusfunc=funcref('abc')", "E700:")
210 call assert_fails("LET &imstatusfunc = function('abc')", "E700:")
211 call assert_fails("LET &imactivatefunc = function('abc')", "E700:")
212 normal! i
213
214 #" set 'imactivatefunc' and 'imstatusfunc' to a non-existing function
215 set imactivatefunc=IMactivatefunc1
216 set imstatusfunc=IMstatusfunc1
217 call assert_fails("set imactivatefunc=function('NonExistingFunc')", 'E700:')
218 call assert_fails("set imstatusfunc=function('NonExistingFunc')", 'E700:')
219 call assert_fails("LET &imactivatefunc = function('NonExistingFunc')", 'E700:')
220 call assert_fails("LET &imstatusfunc = function('NonExistingFunc')", 'E700:')
221 normal! i
222
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000223 call assert_equal(14, g:IMactivatefunc_called)
224 call assert_equal(28, g:IMstatusfunc_called)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000225 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000226 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000227
228 " Using Vim9 lambda expression in legacy context should fail
229 set imactivatefunc=(a)\ =>\ IMactivatefunc1(a)
230 set imstatusfunc=IMstatusfunc1
231 call assert_fails('normal! i', 'E117:')
232 set imactivatefunc=IMactivatefunc1
233 set imstatusfunc=()\ =>\ IMstatusfunc1(a)
234 call assert_fails('normal! i', 'E117:')
235
236 " set 'imactivatefunc' and 'imstatusfunc' to a partial with dict. This used
237 " to cause a crash.
238 func SetIMFunc()
239 let params1 = {'activate': function('g:DictActivateFunc')}
240 let params2 = {'status': function('g:DictStatusFunc')}
241 let &imactivatefunc = params1.activate
242 let &imstatusfunc = params2.status
243 endfunc
244 func g:DictActivateFunc(_) dict
245 endfunc
246 func g:DictStatusFunc(_) dict
247 endfunc
248 call SetIMFunc()
249 new
250 call SetIMFunc()
251 bw
252 call test_garbagecollect_now()
253 new
254 set imactivatefunc=
255 set imstatusfunc=
256 wincmd w
257 set imactivatefunc=
258 set imstatusfunc=
259 :%bw!
260 delfunc g:DictActivateFunc
261 delfunc g:DictStatusFunc
262 delfunc SetIMFunc
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000263
264 " Vim9 tests
265 let lines =<< trim END
266 vim9script
267
268 # Test for using function()
269 def IMactivatefunc1(active: number): any
270 g:IMactivatefunc_called += 1
271 return 1
272 enddef
273 def IMstatusfunc1(): number
274 g:IMstatusfunc_called += 1
275 return 1
276 enddef
277 g:IMactivatefunc_called = 0
278 g:IMstatusfunc_called = 0
279 set iminsert=2
280 set imactivatefunc=function('IMactivatefunc1')
281 set imstatusfunc=function('IMstatusfunc1')
282 normal! i
283
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000284 set iminsert=0
285 set imactivatefunc=
286 set imstatusfunc=
287 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000288 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000289
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000290 " cleanup
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000291 set iminsert=0
292 set imactivatefunc&
293 set imstatusfunc&
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000294 delfunc IMactivatefunc1
295 delfunc IMstatusfunc1
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000296 unlet g:IMactivatefunc_called g:IMstatusfunc_called
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000297 %bw!
298endfunc
299
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100300" vim: shiftwidth=2 sts=2 expandtab