blob: a0e9db0a78514bd70e90c76f3d3608056772704f [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
82 call writefile(lines, 'Xscript')
83 let buf = RunVimInTerminal('-S Xscript', {})
84 call term_wait(buf)
85 call assert_notequal('abc', term_getline(buf, 2))
86 call term_sendkeys(buf, "/abc\n")
87 call term_wait(buf)
88 call assert_equal('abc', term_getline(buf, 2))
89
90 " clean up
91 call StopVimInTerminal(buf)
92 call delete('Xscript')
93endfunc
94
Bram Moolenaar1671f442020-03-10 07:48:13 +010095" Test for using an lmap in insert mode
96func Test_lmap_in_insert_mode()
97 new
98 call setline(1, 'abc')
99 lmap { w
100 set iminsert=1
101 call feedkeys('r{', 'xt')
102 call assert_equal('wbc', getline(1))
103 set iminsert=2
104 call feedkeys('$r{', 'xt')
105 call assert_equal('wb{', getline(1))
106 call setline(1, 'vim web')
107 set iminsert=1
108 call feedkeys('0f{', 'xt')
109 call assert_equal(5, col('.'))
110 set iminsert&
111 lunmap {
112 close!
113endfunc
114
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200115" Test for using CTRL-^ to toggle iminsert in insert mode
116func Test_iminsert_toggle()
117 CheckGui
118 if has('win32')
119 CheckFeature multi_byte_ime
Bram Moolenaarbe7529e2020-08-13 21:05:39 +0200120 else
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200121 CheckFeature xim
122 endif
123 if has('gui_running') && !has('win32')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200124 throw 'Skipped: works only in Win32 GUI version (for some reason)'
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200125 endif
126 new
127 let save_imdisable = &imdisable
128 let save_iminsert = &iminsert
129 set noimdisable
130 set iminsert=0
131 exe "normal i\<C-^>"
132 call assert_equal(2, &iminsert)
133 exe "normal i\<C-^>"
134 call assert_equal(0, &iminsert)
135 let &iminsert = save_iminsert
136 let &imdisable = save_imdisable
137 close!
138endfunc
139
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000140" Test for different ways of setting the 'imactivatefunc' and 'imstatusfunc'
141" options
142func Test_imactivatefunc_imstatusfunc_callback()
143 CheckNotMSWindows
144 func IMactivatefunc1(active)
145 let g:IMactivatefunc_called += 1
146 endfunc
147 func IMstatusfunc1()
148 let g:IMstatusfunc_called += 1
149 return 1
150 endfunc
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000151 set iminsert=2
152
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000153 let lines =<< trim END
154 LET g:IMactivatefunc_called = 0
155 LET g:IMstatusfunc_called = 0
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000156
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000157 #" Test for using a function name
158 LET &imactivatefunc = 'g:IMactivatefunc1'
159 LET &imstatusfunc = 'g:IMstatusfunc1'
160 normal! i
161
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000162 #" Test for using a function()
163 set imactivatefunc=function('g:IMactivatefunc1')
164 set imstatusfunc=function('g:IMstatusfunc1')
165 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000166
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000167 #" Using a funcref variable to set 'completefunc'
168 VAR Fn1 = function('g:IMactivatefunc1')
169 LET &imactivatefunc = Fn1
170 VAR Fn2 = function('g:IMstatusfunc1')
171 LET &imstatusfunc = Fn2
172 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000173
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000174 #" Using a string(funcref variable) to set 'completefunc'
175 LET &imactivatefunc = string(Fn1)
176 LET &imstatusfunc = string(Fn2)
177 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000178
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000179 #" Test for using a funcref()
180 set imactivatefunc=funcref('g:IMactivatefunc1')
181 set imstatusfunc=funcref('g:IMstatusfunc1')
182 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000183
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000184 #" Using a funcref variable to set 'imactivatefunc'
185 LET Fn1 = funcref('g:IMactivatefunc1')
186 LET &imactivatefunc = Fn1
187 LET Fn2 = funcref('g:IMstatusfunc1')
188 LET &imstatusfunc = Fn2
189 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000190
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000191 #" Using a string(funcref variable) to set 'imactivatefunc'
192 LET &imactivatefunc = string(Fn1)
193 LET &imstatusfunc = string(Fn2)
194 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000195
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000196 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +0000197 VAR optval = "LSTART a LMIDDLE g:IMactivatefunc1(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000198 LET optval = substitute(optval, ' ', '\\ ', 'g')
199 exe "set imactivatefunc=" .. optval
Bram Moolenaar62aec932022-01-29 21:45:34 +0000200 LET optval = "LSTART LMIDDLE g:IMstatusfunc1() LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000201 LET optval = substitute(optval, ' ', '\\ ', 'g')
202 exe "set imstatusfunc=" .. optval
203 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000204
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000205 #" Set 'imactivatefunc' and 'imstatusfunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +0000206 LET &imactivatefunc = LSTART a LMIDDLE g:IMactivatefunc1(a) LEND
207 LET &imstatusfunc = LSTART LMIDDLE g:IMstatusfunc1() LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000208 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000209
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000210 #" Set 'imactivatefunc' and 'imstatusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +0000211 LET &imactivatefunc = 'LSTART a LMIDDLE g:IMactivatefunc1(a) LEND'
212 LET &imstatusfunc = 'LSTART LMIDDLE g:IMstatusfunc1() LEND'
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000213 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000214
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000215 #" Set 'imactivatefunc' 'imstatusfunc' to a variable with a lambda
216 #" expression
Bram Moolenaar62aec932022-01-29 21:45:34 +0000217 VAR Lambda1 = LSTART a LMIDDLE g:IMactivatefunc1(a) LEND
218 VAR Lambda2 = LSTART LMIDDLE g:IMstatusfunc1() LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000219 LET &imactivatefunc = Lambda1
220 LET &imstatusfunc = Lambda2
221 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000222
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000223 #" Set 'imactivatefunc' 'imstatusfunc' to a string(variable with a lambda
224 #" expression)
225 LET &imactivatefunc = string(Lambda1)
226 LET &imstatusfunc = string(Lambda2)
227 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000228
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000229 #" Test for clearing the 'completefunc' option
230 set imactivatefunc='' imstatusfunc=''
231 set imactivatefunc& imstatusfunc&
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000232
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000233 set imactivatefunc=g:IMactivatefunc1
234 set imstatusfunc=g:IMstatusfunc1
235 call assert_fails("set imactivatefunc=function('abc')", "E700:")
236 call assert_fails("set imstatusfunc=function('abc')", "E700:")
237 call assert_fails("set imactivatefunc=funcref('abc')", "E700:")
238 call assert_fails("set imstatusfunc=funcref('abc')", "E700:")
239 call assert_fails("LET &imstatusfunc = function('abc')", "E700:")
240 call assert_fails("LET &imactivatefunc = function('abc')", "E700:")
241 normal! i
242
243 #" set 'imactivatefunc' and 'imstatusfunc' to a non-existing function
244 set imactivatefunc=IMactivatefunc1
245 set imstatusfunc=IMstatusfunc1
246 call assert_fails("set imactivatefunc=function('NonExistingFunc')", 'E700:')
247 call assert_fails("set imstatusfunc=function('NonExistingFunc')", 'E700:')
248 call assert_fails("LET &imactivatefunc = function('NonExistingFunc')", 'E700:')
249 call assert_fails("LET &imstatusfunc = function('NonExistingFunc')", 'E700:')
250 normal! i
251
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000252 call assert_equal(14, g:IMactivatefunc_called)
253 call assert_equal(28, g:IMstatusfunc_called)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000254 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000255 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000256
257 " Using Vim9 lambda expression in legacy context should fail
258 set imactivatefunc=(a)\ =>\ IMactivatefunc1(a)
259 set imstatusfunc=IMstatusfunc1
260 call assert_fails('normal! i', 'E117:')
261 set imactivatefunc=IMactivatefunc1
262 set imstatusfunc=()\ =>\ IMstatusfunc1(a)
263 call assert_fails('normal! i', 'E117:')
264
265 " set 'imactivatefunc' and 'imstatusfunc' to a partial with dict. This used
266 " to cause a crash.
267 func SetIMFunc()
268 let params1 = {'activate': function('g:DictActivateFunc')}
269 let params2 = {'status': function('g:DictStatusFunc')}
270 let &imactivatefunc = params1.activate
271 let &imstatusfunc = params2.status
272 endfunc
273 func g:DictActivateFunc(_) dict
274 endfunc
275 func g:DictStatusFunc(_) dict
276 endfunc
277 call SetIMFunc()
278 new
279 call SetIMFunc()
280 bw
281 call test_garbagecollect_now()
282 new
283 set imactivatefunc=
284 set imstatusfunc=
285 wincmd w
286 set imactivatefunc=
287 set imstatusfunc=
288 :%bw!
289 delfunc g:DictActivateFunc
290 delfunc g:DictStatusFunc
291 delfunc SetIMFunc
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000292
293 " Vim9 tests
294 let lines =<< trim END
295 vim9script
296
297 # Test for using function()
298 def IMactivatefunc1(active: number): any
299 g:IMactivatefunc_called += 1
300 return 1
301 enddef
302 def IMstatusfunc1(): number
303 g:IMstatusfunc_called += 1
304 return 1
305 enddef
306 g:IMactivatefunc_called = 0
307 g:IMstatusfunc_called = 0
308 set iminsert=2
309 set imactivatefunc=function('IMactivatefunc1')
310 set imstatusfunc=function('IMstatusfunc1')
311 normal! i
312
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000313 set iminsert=0
314 set imactivatefunc=
315 set imstatusfunc=
316 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000317 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000318
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000319 " cleanup
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000320 set iminsert=0
321 set imactivatefunc&
322 set imstatusfunc&
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000323 delfunc IMactivatefunc1
324 delfunc IMstatusfunc1
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000325 unlet g:IMactivatefunc_called g:IMstatusfunc_called
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000326 %bw!
327endfunc
328
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100329" vim: shiftwidth=2 sts=2 expandtab