blob: 11fe24ee44f4ba7a5eb3a435a3c39325a7734fc4 [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
Millyc7e54ef2022-05-26 13:16:25 +010066func Test_imactivatefunc_imstatusfunc_callback_no_breaks_foldopen()
67 CheckScreendump
68
69 let lines =<< trim END
70 func IM_activatefunc(active)
71 endfunc
72 func IM_statusfunc()
73 return 0
74 endfunc
75 set imactivatefunc=IM_activatefunc
76 set imstatusfunc=IM_statusfunc
77 set foldmethod=marker
78 set foldopen=search
79 call setline(1, ['{{{', 'abc', '}}}'])
80 %foldclose
81 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +010082 call writefile(lines, 'Xscript', 'D')
Millyc7e54ef2022-05-26 13:16:25 +010083 let buf = RunVimInTerminal('-S Xscript', {})
Millyc7e54ef2022-05-26 13:16:25 +010084 call assert_notequal('abc', term_getline(buf, 2))
85 call term_sendkeys(buf, "/abc\n")
Bram Moolenaar37bb3b12022-06-21 17:40:47 +010086 call WaitForAssert({-> assert_equal('abc', term_getline(buf, 2))})
Millyc7e54ef2022-05-26 13:16:25 +010087
88 " clean up
89 call StopVimInTerminal(buf)
Millyc7e54ef2022-05-26 13:16:25 +010090endfunc
91
Bram Moolenaar1671f442020-03-10 07:48:13 +010092" Test for using an lmap in insert mode
93func Test_lmap_in_insert_mode()
94 new
95 call setline(1, 'abc')
96 lmap { w
97 set iminsert=1
98 call feedkeys('r{', 'xt')
99 call assert_equal('wbc', getline(1))
100 set iminsert=2
101 call feedkeys('$r{', 'xt')
102 call assert_equal('wb{', getline(1))
103 call setline(1, 'vim web')
104 set iminsert=1
105 call feedkeys('0f{', 'xt')
106 call assert_equal(5, col('.'))
107 set iminsert&
108 lunmap {
109 close!
110endfunc
111
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200112" Test for using CTRL-^ to toggle iminsert in insert mode
113func Test_iminsert_toggle()
114 CheckGui
115 if has('win32')
116 CheckFeature multi_byte_ime
Bram Moolenaarbe7529e2020-08-13 21:05:39 +0200117 else
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200118 CheckFeature xim
119 endif
120 if has('gui_running') && !has('win32')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200121 throw 'Skipped: works only in Win32 GUI version (for some reason)'
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200122 endif
123 new
124 let save_imdisable = &imdisable
125 let save_iminsert = &iminsert
126 set noimdisable
127 set iminsert=0
128 exe "normal i\<C-^>"
129 call assert_equal(2, &iminsert)
130 exe "normal i\<C-^>"
131 call assert_equal(0, &iminsert)
132 let &iminsert = save_iminsert
133 let &imdisable = save_imdisable
134 close!
135endfunc
136
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000137" Test for different ways of setting the 'imactivatefunc' and 'imstatusfunc'
138" options
139func Test_imactivatefunc_imstatusfunc_callback()
140 CheckNotMSWindows
141 func IMactivatefunc1(active)
142 let g:IMactivatefunc_called += 1
143 endfunc
144 func IMstatusfunc1()
145 let g:IMstatusfunc_called += 1
146 return 1
147 endfunc
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000148 set iminsert=2
149
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000150 let lines =<< trim END
151 LET g:IMactivatefunc_called = 0
152 LET g:IMstatusfunc_called = 0
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000153
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000154 #" Test for using a function name
155 LET &imactivatefunc = 'g:IMactivatefunc1'
156 LET &imstatusfunc = 'g:IMstatusfunc1'
157 normal! i
158
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000159 #" Test for using a function()
160 set imactivatefunc=function('g:IMactivatefunc1')
161 set imstatusfunc=function('g:IMstatusfunc1')
162 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000163
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000164 #" Using a funcref variable to set 'completefunc'
165 VAR Fn1 = function('g:IMactivatefunc1')
166 LET &imactivatefunc = Fn1
167 VAR Fn2 = function('g:IMstatusfunc1')
168 LET &imstatusfunc = Fn2
169 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000170
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000171 #" Using a string(funcref variable) to set 'completefunc'
172 LET &imactivatefunc = string(Fn1)
173 LET &imstatusfunc = string(Fn2)
174 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000175
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000176 #" Test for using a funcref()
177 set imactivatefunc=funcref('g:IMactivatefunc1')
178 set imstatusfunc=funcref('g:IMstatusfunc1')
179 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000180
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000181 #" Using a funcref variable to set 'imactivatefunc'
182 LET Fn1 = funcref('g:IMactivatefunc1')
183 LET &imactivatefunc = Fn1
184 LET Fn2 = funcref('g:IMstatusfunc1')
185 LET &imstatusfunc = Fn2
186 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000187
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000188 #" Using a string(funcref variable) to set 'imactivatefunc'
189 LET &imactivatefunc = string(Fn1)
190 LET &imstatusfunc = string(Fn2)
191 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000192
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000193 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +0000194 VAR optval = "LSTART a LMIDDLE g:IMactivatefunc1(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000195 LET optval = substitute(optval, ' ', '\\ ', 'g')
196 exe "set imactivatefunc=" .. optval
Bram Moolenaar62aec932022-01-29 21:45:34 +0000197 LET optval = "LSTART LMIDDLE g:IMstatusfunc1() LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000198 LET optval = substitute(optval, ' ', '\\ ', 'g')
199 exe "set imstatusfunc=" .. optval
200 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000201
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000202 #" Set 'imactivatefunc' and 'imstatusfunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +0000203 LET &imactivatefunc = LSTART a LMIDDLE g:IMactivatefunc1(a) LEND
204 LET &imstatusfunc = LSTART LMIDDLE g:IMstatusfunc1() LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000205 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000206
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000207 #" Set 'imactivatefunc' and 'imstatusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +0000208 LET &imactivatefunc = 'LSTART a LMIDDLE g:IMactivatefunc1(a) LEND'
209 LET &imstatusfunc = 'LSTART LMIDDLE g:IMstatusfunc1() LEND'
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000210 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000211
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000212 #" Set 'imactivatefunc' 'imstatusfunc' to a variable with a lambda
213 #" expression
Bram Moolenaar62aec932022-01-29 21:45:34 +0000214 VAR Lambda1 = LSTART a LMIDDLE g:IMactivatefunc1(a) LEND
215 VAR Lambda2 = LSTART LMIDDLE g:IMstatusfunc1() LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000216 LET &imactivatefunc = Lambda1
217 LET &imstatusfunc = Lambda2
218 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000219
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000220 #" Set 'imactivatefunc' 'imstatusfunc' to a string(variable with a lambda
221 #" expression)
222 LET &imactivatefunc = string(Lambda1)
223 LET &imstatusfunc = string(Lambda2)
224 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000225
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000226 #" Test for clearing the 'completefunc' option
227 set imactivatefunc='' imstatusfunc=''
228 set imactivatefunc& imstatusfunc&
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000229
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000230 set imactivatefunc=g:IMactivatefunc1
231 set imstatusfunc=g:IMstatusfunc1
232 call assert_fails("set imactivatefunc=function('abc')", "E700:")
233 call assert_fails("set imstatusfunc=function('abc')", "E700:")
234 call assert_fails("set imactivatefunc=funcref('abc')", "E700:")
235 call assert_fails("set imstatusfunc=funcref('abc')", "E700:")
236 call assert_fails("LET &imstatusfunc = function('abc')", "E700:")
237 call assert_fails("LET &imactivatefunc = function('abc')", "E700:")
238 normal! i
239
240 #" set 'imactivatefunc' and 'imstatusfunc' to a non-existing function
241 set imactivatefunc=IMactivatefunc1
242 set imstatusfunc=IMstatusfunc1
243 call assert_fails("set imactivatefunc=function('NonExistingFunc')", 'E700:')
244 call assert_fails("set imstatusfunc=function('NonExistingFunc')", 'E700:')
245 call assert_fails("LET &imactivatefunc = function('NonExistingFunc')", 'E700:')
246 call assert_fails("LET &imstatusfunc = function('NonExistingFunc')", 'E700:')
247 normal! i
248
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000249 call assert_equal(14, g:IMactivatefunc_called)
250 call assert_equal(28, g:IMstatusfunc_called)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000251 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000252 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000253
254 " Using Vim9 lambda expression in legacy context should fail
255 set imactivatefunc=(a)\ =>\ IMactivatefunc1(a)
256 set imstatusfunc=IMstatusfunc1
257 call assert_fails('normal! i', 'E117:')
258 set imactivatefunc=IMactivatefunc1
259 set imstatusfunc=()\ =>\ IMstatusfunc1(a)
260 call assert_fails('normal! i', 'E117:')
261
262 " set 'imactivatefunc' and 'imstatusfunc' to a partial with dict. This used
263 " to cause a crash.
264 func SetIMFunc()
265 let params1 = {'activate': function('g:DictActivateFunc')}
266 let params2 = {'status': function('g:DictStatusFunc')}
267 let &imactivatefunc = params1.activate
268 let &imstatusfunc = params2.status
269 endfunc
270 func g:DictActivateFunc(_) dict
271 endfunc
272 func g:DictStatusFunc(_) dict
273 endfunc
274 call SetIMFunc()
275 new
276 call SetIMFunc()
277 bw
278 call test_garbagecollect_now()
279 new
280 set imactivatefunc=
281 set imstatusfunc=
282 wincmd w
283 set imactivatefunc=
284 set imstatusfunc=
285 :%bw!
286 delfunc g:DictActivateFunc
287 delfunc g:DictStatusFunc
288 delfunc SetIMFunc
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000289
290 " Vim9 tests
291 let lines =<< trim END
292 vim9script
293
294 # Test for using function()
295 def IMactivatefunc1(active: number): any
296 g:IMactivatefunc_called += 1
297 return 1
298 enddef
299 def IMstatusfunc1(): number
300 g:IMstatusfunc_called += 1
301 return 1
302 enddef
303 g:IMactivatefunc_called = 0
304 g:IMstatusfunc_called = 0
305 set iminsert=2
306 set imactivatefunc=function('IMactivatefunc1')
307 set imstatusfunc=function('IMstatusfunc1')
308 normal! i
309
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000310 set iminsert=0
311 set imactivatefunc=
312 set imstatusfunc=
313 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000314 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000315
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000316 " cleanup
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000317 set iminsert=0
318 set imactivatefunc&
319 set imstatusfunc&
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000320 delfunc IMactivatefunc1
321 delfunc IMstatusfunc1
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000322 unlet g:IMactivatefunc_called g:IMstatusfunc_called
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000323 %bw!
324endfunc
325
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100326" vim: shiftwidth=2 sts=2 expandtab