blob: 9f3ede3379f2fc3673a45474fefd3d51086b4c30 [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', {})
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)
90 call delete('Xscript')
91endfunc
92
Bram Moolenaar1671f442020-03-10 07:48:13 +010093" Test for using an lmap in insert mode
94func Test_lmap_in_insert_mode()
95 new
96 call setline(1, 'abc')
97 lmap { w
98 set iminsert=1
99 call feedkeys('r{', 'xt')
100 call assert_equal('wbc', getline(1))
101 set iminsert=2
102 call feedkeys('$r{', 'xt')
103 call assert_equal('wb{', getline(1))
104 call setline(1, 'vim web')
105 set iminsert=1
106 call feedkeys('0f{', 'xt')
107 call assert_equal(5, col('.'))
108 set iminsert&
109 lunmap {
110 close!
111endfunc
112
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200113" Test for using CTRL-^ to toggle iminsert in insert mode
114func Test_iminsert_toggle()
115 CheckGui
116 if has('win32')
117 CheckFeature multi_byte_ime
Bram Moolenaarbe7529e2020-08-13 21:05:39 +0200118 else
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200119 CheckFeature xim
120 endif
121 if has('gui_running') && !has('win32')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200122 throw 'Skipped: works only in Win32 GUI version (for some reason)'
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200123 endif
124 new
125 let save_imdisable = &imdisable
126 let save_iminsert = &iminsert
127 set noimdisable
128 set iminsert=0
129 exe "normal i\<C-^>"
130 call assert_equal(2, &iminsert)
131 exe "normal i\<C-^>"
132 call assert_equal(0, &iminsert)
133 let &iminsert = save_iminsert
134 let &imdisable = save_imdisable
135 close!
136endfunc
137
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000138" Test for different ways of setting the 'imactivatefunc' and 'imstatusfunc'
139" options
140func Test_imactivatefunc_imstatusfunc_callback()
141 CheckNotMSWindows
142 func IMactivatefunc1(active)
143 let g:IMactivatefunc_called += 1
144 endfunc
145 func IMstatusfunc1()
146 let g:IMstatusfunc_called += 1
147 return 1
148 endfunc
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000149 set iminsert=2
150
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000151 let lines =<< trim END
152 LET g:IMactivatefunc_called = 0
153 LET g:IMstatusfunc_called = 0
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000154
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000155 #" Test for using a function name
156 LET &imactivatefunc = 'g:IMactivatefunc1'
157 LET &imstatusfunc = 'g:IMstatusfunc1'
158 normal! i
159
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000160 #" Test for using a function()
161 set imactivatefunc=function('g:IMactivatefunc1')
162 set imstatusfunc=function('g:IMstatusfunc1')
163 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000164
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000165 #" Using a funcref variable to set 'completefunc'
166 VAR Fn1 = function('g:IMactivatefunc1')
167 LET &imactivatefunc = Fn1
168 VAR Fn2 = function('g:IMstatusfunc1')
169 LET &imstatusfunc = Fn2
170 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000171
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000172 #" Using a string(funcref variable) to set 'completefunc'
173 LET &imactivatefunc = string(Fn1)
174 LET &imstatusfunc = string(Fn2)
175 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000176
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000177 #" Test for using a funcref()
178 set imactivatefunc=funcref('g:IMactivatefunc1')
179 set imstatusfunc=funcref('g:IMstatusfunc1')
180 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000181
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000182 #" Using a funcref variable to set 'imactivatefunc'
183 LET Fn1 = funcref('g:IMactivatefunc1')
184 LET &imactivatefunc = Fn1
185 LET Fn2 = funcref('g:IMstatusfunc1')
186 LET &imstatusfunc = Fn2
187 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000188
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000189 #" Using a string(funcref variable) to set 'imactivatefunc'
190 LET &imactivatefunc = string(Fn1)
191 LET &imstatusfunc = string(Fn2)
192 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000193
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000194 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +0000195 VAR optval = "LSTART a LMIDDLE g:IMactivatefunc1(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000196 LET optval = substitute(optval, ' ', '\\ ', 'g')
197 exe "set imactivatefunc=" .. optval
Bram Moolenaar62aec932022-01-29 21:45:34 +0000198 LET optval = "LSTART LMIDDLE g:IMstatusfunc1() LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000199 LET optval = substitute(optval, ' ', '\\ ', 'g')
200 exe "set imstatusfunc=" .. optval
201 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000202
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000203 #" Set 'imactivatefunc' and 'imstatusfunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +0000204 LET &imactivatefunc = LSTART a LMIDDLE g:IMactivatefunc1(a) LEND
205 LET &imstatusfunc = LSTART LMIDDLE g:IMstatusfunc1() LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000206 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000207
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000208 #" Set 'imactivatefunc' and 'imstatusfunc' to a string(lambda expression)
Bram Moolenaar62aec932022-01-29 21:45:34 +0000209 LET &imactivatefunc = 'LSTART a LMIDDLE g:IMactivatefunc1(a) LEND'
210 LET &imstatusfunc = 'LSTART LMIDDLE g:IMstatusfunc1() LEND'
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000211 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000212
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000213 #" Set 'imactivatefunc' 'imstatusfunc' to a variable with a lambda
214 #" expression
Bram Moolenaar62aec932022-01-29 21:45:34 +0000215 VAR Lambda1 = LSTART a LMIDDLE g:IMactivatefunc1(a) LEND
216 VAR Lambda2 = LSTART LMIDDLE g:IMstatusfunc1() LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000217 LET &imactivatefunc = Lambda1
218 LET &imstatusfunc = Lambda2
219 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000220
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000221 #" Set 'imactivatefunc' 'imstatusfunc' to a string(variable with a lambda
222 #" expression)
223 LET &imactivatefunc = string(Lambda1)
224 LET &imstatusfunc = string(Lambda2)
225 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000226
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000227 #" Test for clearing the 'completefunc' option
228 set imactivatefunc='' imstatusfunc=''
229 set imactivatefunc& imstatusfunc&
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000230
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000231 set imactivatefunc=g:IMactivatefunc1
232 set imstatusfunc=g:IMstatusfunc1
233 call assert_fails("set imactivatefunc=function('abc')", "E700:")
234 call assert_fails("set imstatusfunc=function('abc')", "E700:")
235 call assert_fails("set imactivatefunc=funcref('abc')", "E700:")
236 call assert_fails("set imstatusfunc=funcref('abc')", "E700:")
237 call assert_fails("LET &imstatusfunc = function('abc')", "E700:")
238 call assert_fails("LET &imactivatefunc = function('abc')", "E700:")
239 normal! i
240
241 #" set 'imactivatefunc' and 'imstatusfunc' to a non-existing function
242 set imactivatefunc=IMactivatefunc1
243 set imstatusfunc=IMstatusfunc1
244 call assert_fails("set imactivatefunc=function('NonExistingFunc')", 'E700:')
245 call assert_fails("set imstatusfunc=function('NonExistingFunc')", 'E700:')
246 call assert_fails("LET &imactivatefunc = function('NonExistingFunc')", 'E700:')
247 call assert_fails("LET &imstatusfunc = function('NonExistingFunc')", 'E700:')
248 normal! i
249
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000250 call assert_equal(14, g:IMactivatefunc_called)
251 call assert_equal(28, g:IMstatusfunc_called)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000252 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000253 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000254
255 " Using Vim9 lambda expression in legacy context should fail
256 set imactivatefunc=(a)\ =>\ IMactivatefunc1(a)
257 set imstatusfunc=IMstatusfunc1
258 call assert_fails('normal! i', 'E117:')
259 set imactivatefunc=IMactivatefunc1
260 set imstatusfunc=()\ =>\ IMstatusfunc1(a)
261 call assert_fails('normal! i', 'E117:')
262
263 " set 'imactivatefunc' and 'imstatusfunc' to a partial with dict. This used
264 " to cause a crash.
265 func SetIMFunc()
266 let params1 = {'activate': function('g:DictActivateFunc')}
267 let params2 = {'status': function('g:DictStatusFunc')}
268 let &imactivatefunc = params1.activate
269 let &imstatusfunc = params2.status
270 endfunc
271 func g:DictActivateFunc(_) dict
272 endfunc
273 func g:DictStatusFunc(_) dict
274 endfunc
275 call SetIMFunc()
276 new
277 call SetIMFunc()
278 bw
279 call test_garbagecollect_now()
280 new
281 set imactivatefunc=
282 set imstatusfunc=
283 wincmd w
284 set imactivatefunc=
285 set imstatusfunc=
286 :%bw!
287 delfunc g:DictActivateFunc
288 delfunc g:DictStatusFunc
289 delfunc SetIMFunc
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000290
291 " Vim9 tests
292 let lines =<< trim END
293 vim9script
294
295 # Test for using function()
296 def IMactivatefunc1(active: number): any
297 g:IMactivatefunc_called += 1
298 return 1
299 enddef
300 def IMstatusfunc1(): number
301 g:IMstatusfunc_called += 1
302 return 1
303 enddef
304 g:IMactivatefunc_called = 0
305 g:IMstatusfunc_called = 0
306 set iminsert=2
307 set imactivatefunc=function('IMactivatefunc1')
308 set imstatusfunc=function('IMstatusfunc1')
309 normal! i
310
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000311 set iminsert=0
312 set imactivatefunc=
313 set imstatusfunc=
314 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000315 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000316
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000317 " cleanup
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000318 set iminsert=0
319 set imactivatefunc&
320 set imstatusfunc&
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000321 delfunc IMactivatefunc1
322 delfunc IMstatusfunc1
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000323 unlet g:IMactivatefunc_called g:IMstatusfunc_called
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000324 %bw!
325endfunc
326
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100327" vim: shiftwidth=2 sts=2 expandtab