blob: 860fb07ad316224e0f5cd23971758e0b8680194e [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
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 Lakshmanan6ae8fae2021-12-12 16:26:44 +0000128 #" Test for using a function()
129 set imactivatefunc=function('g:IMactivatefunc1')
130 set imstatusfunc=function('g:IMstatusfunc1')
131 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000132
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000133 #" Using a funcref variable to set 'completefunc'
134 VAR Fn1 = function('g:IMactivatefunc1')
135 LET &imactivatefunc = Fn1
136 VAR Fn2 = function('g:IMstatusfunc1')
137 LET &imstatusfunc = Fn2
138 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000139
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000140 #" Using a string(funcref variable) to set 'completefunc'
141 LET &imactivatefunc = string(Fn1)
142 LET &imstatusfunc = string(Fn2)
143 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000144
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000145 #" Test for using a funcref()
146 set imactivatefunc=funcref('g:IMactivatefunc1')
147 set imstatusfunc=funcref('g:IMstatusfunc1')
148 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000149
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000150 #" Using a funcref variable to set 'imactivatefunc'
151 LET Fn1 = funcref('g:IMactivatefunc1')
152 LET &imactivatefunc = Fn1
153 LET Fn2 = funcref('g:IMstatusfunc1')
154 LET &imstatusfunc = Fn2
155 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000156
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000157 #" Using a string(funcref variable) to set 'imactivatefunc'
158 LET &imactivatefunc = string(Fn1)
159 LET &imstatusfunc = string(Fn2)
160 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000161
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000162 #" Test for using a lambda function
163 VAR optval = "LSTART a LMIDDLE IMactivatefunc1(a) LEND"
164 LET optval = substitute(optval, ' ', '\\ ', 'g')
165 exe "set imactivatefunc=" .. optval
166 LET optval = "LSTART LMIDDLE IMstatusfunc1() LEND"
167 LET optval = substitute(optval, ' ', '\\ ', 'g')
168 exe "set imstatusfunc=" .. optval
169 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000170
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000171 #" Set 'imactivatefunc' and 'imstatusfunc' to a lambda expression
172 LET &imactivatefunc = LSTART a LMIDDLE IMactivatefunc1(a) LEND
173 LET &imstatusfunc = LSTART LMIDDLE IMstatusfunc1() LEND
174 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000175
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000176 #" Set 'imactivatefunc' and 'imstatusfunc' to a string(lambda expression)
177 LET &imactivatefunc = 'LSTART a LMIDDLE IMactivatefunc1(a) LEND'
178 LET &imstatusfunc = 'LSTART LMIDDLE IMstatusfunc1() LEND'
179 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000180
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000181 #" Set 'imactivatefunc' 'imstatusfunc' to a variable with a lambda
182 #" expression
183 VAR Lambda1 = LSTART a LMIDDLE IMactivatefunc1(a) LEND
184 VAR Lambda2 = LSTART LMIDDLE IMstatusfunc1() LEND
185 LET &imactivatefunc = Lambda1
186 LET &imstatusfunc = Lambda2
187 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000188
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000189 #" Set 'imactivatefunc' 'imstatusfunc' to a string(variable with a lambda
190 #" expression)
191 LET &imactivatefunc = string(Lambda1)
192 LET &imstatusfunc = string(Lambda2)
193 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000194
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000195 #" Test for clearing the 'completefunc' option
196 set imactivatefunc='' imstatusfunc=''
197 set imactivatefunc& imstatusfunc&
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000198
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000199 set imactivatefunc=g:IMactivatefunc1
200 set imstatusfunc=g:IMstatusfunc1
201 call assert_fails("set imactivatefunc=function('abc')", "E700:")
202 call assert_fails("set imstatusfunc=function('abc')", "E700:")
203 call assert_fails("set imactivatefunc=funcref('abc')", "E700:")
204 call assert_fails("set imstatusfunc=funcref('abc')", "E700:")
205 call assert_fails("LET &imstatusfunc = function('abc')", "E700:")
206 call assert_fails("LET &imactivatefunc = function('abc')", "E700:")
207 normal! i
208
209 #" set 'imactivatefunc' and 'imstatusfunc' to a non-existing function
210 set imactivatefunc=IMactivatefunc1
211 set imstatusfunc=IMstatusfunc1
212 call assert_fails("set imactivatefunc=function('NonExistingFunc')", 'E700:')
213 call assert_fails("set imstatusfunc=function('NonExistingFunc')", 'E700:')
214 call assert_fails("LET &imactivatefunc = function('NonExistingFunc')", 'E700:')
215 call assert_fails("LET &imstatusfunc = function('NonExistingFunc')", 'E700:')
216 normal! i
217
218 call assert_equal(13, g:IMactivatefunc_called)
219 call assert_equal(26, g:IMstatusfunc_called)
220 END
221 call CheckLegacyAndVim9Success(lines)
222
223 " Using Vim9 lambda expression in legacy context should fail
224 set imactivatefunc=(a)\ =>\ IMactivatefunc1(a)
225 set imstatusfunc=IMstatusfunc1
226 call assert_fails('normal! i', 'E117:')
227 set imactivatefunc=IMactivatefunc1
228 set imstatusfunc=()\ =>\ IMstatusfunc1(a)
229 call assert_fails('normal! i', 'E117:')
230
231 " set 'imactivatefunc' and 'imstatusfunc' to a partial with dict. This used
232 " to cause a crash.
233 func SetIMFunc()
234 let params1 = {'activate': function('g:DictActivateFunc')}
235 let params2 = {'status': function('g:DictStatusFunc')}
236 let &imactivatefunc = params1.activate
237 let &imstatusfunc = params2.status
238 endfunc
239 func g:DictActivateFunc(_) dict
240 endfunc
241 func g:DictStatusFunc(_) dict
242 endfunc
243 call SetIMFunc()
244 new
245 call SetIMFunc()
246 bw
247 call test_garbagecollect_now()
248 new
249 set imactivatefunc=
250 set imstatusfunc=
251 wincmd w
252 set imactivatefunc=
253 set imstatusfunc=
254 :%bw!
255 delfunc g:DictActivateFunc
256 delfunc g:DictStatusFunc
257 delfunc SetIMFunc
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000258
259 " Vim9 tests
260 let lines =<< trim END
261 vim9script
262
263 # Test for using function()
264 def IMactivatefunc1(active: number): any
265 g:IMactivatefunc_called += 1
266 return 1
267 enddef
268 def IMstatusfunc1(): number
269 g:IMstatusfunc_called += 1
270 return 1
271 enddef
272 g:IMactivatefunc_called = 0
273 g:IMstatusfunc_called = 0
274 set iminsert=2
275 set imactivatefunc=function('IMactivatefunc1')
276 set imstatusfunc=function('IMstatusfunc1')
277 normal! i
278
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000279 set iminsert=0
280 set imactivatefunc=
281 set imstatusfunc=
282 END
283 call CheckScriptSuccess(lines)
284
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000285 " cleanup
286 delfunc IMactivatefunc1
287 delfunc IMstatusfunc1
288 set iminsert=0
289 set imactivatefunc=
290 set imstatusfunc=
291
292 %bw!
293endfunc
294
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100295" vim: shiftwidth=2 sts=2 expandtab