blob: 918f17d4c9e5417599825f21a61a2073e1a6e2d8 [file] [log] [blame]
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +02001" Test for 'iminsert'
2
Christian Brabandteb380b92025-07-07 20:53:55 +02003import './util/vim9.vim' as v9
Bram Moolenaar6315a9a2017-11-25 15:20:02 +01004
5let s:imactivatefunc_called = 0
6let s:imstatusfunc_called = 0
Bram Moolenaara3a12462019-09-07 15:08:38 +02007let s:imstatus_active = 0
Bram Moolenaar6315a9a2017-11-25 15:20:02 +01008
9func IM_activatefunc(active)
10 let s:imactivatefunc_called = 1
Bram Moolenaar845e0ee2020-06-20 16:05:32 +020011 let s:imstatus_active = a:active
Bram Moolenaar6315a9a2017-11-25 15:20:02 +010012endfunc
13
14func IM_statusfunc()
15 let s:imstatusfunc_called = 1
Bram Moolenaara3a12462019-09-07 15:08:38 +020016 return s:imstatus_active
Bram Moolenaar6315a9a2017-11-25 15:20:02 +010017endfunc
18
19func Test_iminsert2()
Bram Moolenaara3a12462019-09-07 15:08:38 +020020 let s:imactivatefunc_called = 0
21 let s:imstatusfunc_called = 0
22
Bram Moolenaar6315a9a2017-11-25 15:20:02 +010023 set imactivatefunc=IM_activatefunc
24 set imstatusfunc=IM_statusfunc
25 set iminsert=2
26 normal! i
27 set iminsert=0
28 set imactivatefunc=
29 set imstatusfunc=
Bram Moolenaar2877d332017-11-26 14:56:16 +010030
Bram Moolenaarc19fd912020-07-02 20:59:05 +020031 let expected = (has('win32') && has('gui_running')) ? 0 : 1
Bram Moolenaar2877d332017-11-26 14:56:16 +010032 call assert_equal(expected, s:imactivatefunc_called)
33 call assert_equal(expected, s:imstatusfunc_called)
Bram Moolenaar6315a9a2017-11-25 15:20:02 +010034endfunc
Bram Moolenaara3a12462019-09-07 15:08:38 +020035
Bram Moolenaare2a3f362019-12-07 21:40:48 +010036func Test_getimstatus()
37 if has('win32')
38 CheckFeature multi_byte_ime
Bram Moolenaarbe7529e2020-08-13 21:05:39 +020039 else
Bram Moolenaare2a3f362019-12-07 21:40:48 +010040 CheckFeature xim
41 endif
Bram Moolenaarc19fd912020-07-02 20:59:05 +020042 if has('win32') && has('gui_running')
Bram Moolenaara3a12462019-09-07 15:08:38 +020043 set imactivatefunc=
44 set imstatusfunc=
45 else
46 set imactivatefunc=IM_activatefunc
47 set imstatusfunc=IM_statusfunc
48 let s:imstatus_active = 0
49 endif
50
51 new
52 set iminsert=2
53 call feedkeys("i\<C-R>=getimstatus()\<CR>\<ESC>", 'nx')
54 call assert_equal('1', getline(1))
55 set iminsert=0
56 call feedkeys("o\<C-R>=getimstatus()\<CR>\<ESC>", 'nx')
57 call assert_equal('0', getline(2))
58 bw!
59
60 set imactivatefunc=
61 set imstatusfunc=
62endfunc
Bram Moolenaar0546d7d2020-03-01 16:53:09 +010063
Millyc7e54ef2022-05-26 13:16:25 +010064func Test_imactivatefunc_imstatusfunc_callback_no_breaks_foldopen()
65 CheckScreendump
66
67 let lines =<< trim END
68 func IM_activatefunc(active)
69 endfunc
70 func IM_statusfunc()
71 return 0
72 endfunc
73 set imactivatefunc=IM_activatefunc
74 set imstatusfunc=IM_statusfunc
75 set foldmethod=marker
76 set foldopen=search
77 call setline(1, ['{{{', 'abc', '}}}'])
78 %foldclose
79 END
Bram Moolenaar7dd5a782022-09-29 21:01:57 +010080 call writefile(lines, 'Xscript', 'D')
Millyc7e54ef2022-05-26 13:16:25 +010081 let buf = RunVimInTerminal('-S Xscript', {})
Millyc7e54ef2022-05-26 13:16:25 +010082 call assert_notequal('abc', term_getline(buf, 2))
83 call term_sendkeys(buf, "/abc\n")
Bram Moolenaar37bb3b12022-06-21 17:40:47 +010084 call WaitForAssert({-> assert_equal('abc', term_getline(buf, 2))})
Millyc7e54ef2022-05-26 13:16:25 +010085
86 " clean up
87 call StopVimInTerminal(buf)
Millyc7e54ef2022-05-26 13:16:25 +010088endfunc
89
Bram Moolenaar1671f442020-03-10 07:48:13 +010090" Test for using an lmap in insert mode
91func Test_lmap_in_insert_mode()
92 new
93 call setline(1, 'abc')
94 lmap { w
95 set iminsert=1
96 call feedkeys('r{', 'xt')
97 call assert_equal('wbc', getline(1))
98 set iminsert=2
99 call feedkeys('$r{', 'xt')
100 call assert_equal('wb{', getline(1))
101 call setline(1, 'vim web')
102 set iminsert=1
103 call feedkeys('0f{', 'xt')
104 call assert_equal(5, col('.'))
105 set iminsert&
106 lunmap {
107 close!
108endfunc
109
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200110" Test for using CTRL-^ to toggle iminsert in insert mode
111func Test_iminsert_toggle()
112 CheckGui
113 if has('win32')
114 CheckFeature multi_byte_ime
Bram Moolenaarbe7529e2020-08-13 21:05:39 +0200115 else
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200116 CheckFeature xim
117 endif
118 if has('gui_running') && !has('win32')
Bram Moolenaar6d91bcb2020-08-12 18:50:36 +0200119 throw 'Skipped: works only in Win32 GUI version (for some reason)'
Bram Moolenaar845e0ee2020-06-20 16:05:32 +0200120 endif
121 new
122 let save_imdisable = &imdisable
123 let save_iminsert = &iminsert
124 set noimdisable
125 set iminsert=0
126 exe "normal i\<C-^>"
127 call assert_equal(2, &iminsert)
128 exe "normal i\<C-^>"
129 call assert_equal(0, &iminsert)
130 let &iminsert = save_iminsert
131 let &imdisable = save_imdisable
132 close!
133endfunc
134
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000135" Test for different ways of setting the 'imactivatefunc' and 'imstatusfunc'
136" options
137func Test_imactivatefunc_imstatusfunc_callback()
138 CheckNotMSWindows
139 func IMactivatefunc1(active)
140 let g:IMactivatefunc_called += 1
141 endfunc
142 func IMstatusfunc1()
143 let g:IMstatusfunc_called += 1
144 return 1
145 endfunc
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000146 set iminsert=2
147
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000148 let lines =<< trim END
149 LET g:IMactivatefunc_called = 0
150 LET g:IMstatusfunc_called = 0
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000151
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000152 #" Test for using a function name
153 LET &imactivatefunc = 'g:IMactivatefunc1'
154 LET &imstatusfunc = 'g:IMstatusfunc1'
155 normal! i
156
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000157 #" Test for using a function()
158 set imactivatefunc=function('g:IMactivatefunc1')
159 set imstatusfunc=function('g:IMstatusfunc1')
160 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000161
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000162 #" Using a funcref variable to set 'completefunc'
163 VAR Fn1 = function('g:IMactivatefunc1')
164 LET &imactivatefunc = Fn1
165 VAR Fn2 = function('g:IMstatusfunc1')
166 LET &imstatusfunc = Fn2
167 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000168
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000169 #" Using a string(funcref variable) to set 'completefunc'
170 LET &imactivatefunc = string(Fn1)
171 LET &imstatusfunc = string(Fn2)
172 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000173
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000174 #" Test for using a funcref()
175 set imactivatefunc=funcref('g:IMactivatefunc1')
176 set imstatusfunc=funcref('g:IMstatusfunc1')
177 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000178
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000179 #" Using a funcref variable to set 'imactivatefunc'
180 LET Fn1 = funcref('g:IMactivatefunc1')
181 LET &imactivatefunc = Fn1
182 LET Fn2 = funcref('g:IMstatusfunc1')
183 LET &imstatusfunc = Fn2
184 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000185
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000186 #" Using a string(funcref variable) to set 'imactivatefunc'
187 LET &imactivatefunc = string(Fn1)
188 LET &imstatusfunc = string(Fn2)
189 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000190
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000191 #" Test for using a lambda function
Bram Moolenaar62aec932022-01-29 21:45:34 +0000192 VAR optval = "LSTART a LMIDDLE g:IMactivatefunc1(a) LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000193 LET optval = substitute(optval, ' ', '\\ ', 'g')
194 exe "set imactivatefunc=" .. optval
Bram Moolenaar62aec932022-01-29 21:45:34 +0000195 LET optval = "LSTART LMIDDLE g:IMstatusfunc1() LEND"
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000196 LET optval = substitute(optval, ' ', '\\ ', 'g')
197 exe "set imstatusfunc=" .. optval
198 normal! i
Yegappan Lakshmanan64095532021-12-06 11:03:55 +0000199
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000200 #" Set 'imactivatefunc' and 'imstatusfunc' to a lambda expression
Bram Moolenaar62aec932022-01-29 21:45:34 +0000201 LET &imactivatefunc = LSTART a LMIDDLE g:IMactivatefunc1(a) LEND
202 LET &imstatusfunc = LSTART LMIDDLE g:IMstatusfunc1() LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000203 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000204
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000205 #" Set 'imactivatefunc' and 'imstatusfunc' to a string(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 Lakshmanan64095532021-12-06 11:03:55 +0000209
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000210 #" Set 'imactivatefunc' 'imstatusfunc' to a variable with a lambda
211 #" expression
Bram Moolenaar62aec932022-01-29 21:45:34 +0000212 VAR Lambda1 = LSTART a LMIDDLE g:IMactivatefunc1(a) LEND
213 VAR Lambda2 = LSTART LMIDDLE g:IMstatusfunc1() LEND
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000214 LET &imactivatefunc = Lambda1
215 LET &imstatusfunc = Lambda2
216 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000217
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000218 #" Set 'imactivatefunc' 'imstatusfunc' to a string(variable with a lambda
219 #" expression)
220 LET &imactivatefunc = string(Lambda1)
221 LET &imstatusfunc = string(Lambda2)
222 normal! i
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000223
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000224 #" Test for clearing the 'completefunc' option
225 set imactivatefunc='' imstatusfunc=''
226 set imactivatefunc& imstatusfunc&
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000227
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000228 set imactivatefunc=g:IMactivatefunc1
229 set imstatusfunc=g:IMstatusfunc1
230 call assert_fails("set imactivatefunc=function('abc')", "E700:")
231 call assert_fails("set imstatusfunc=function('abc')", "E700:")
232 call assert_fails("set imactivatefunc=funcref('abc')", "E700:")
233 call assert_fails("set imstatusfunc=funcref('abc')", "E700:")
234 call assert_fails("LET &imstatusfunc = function('abc')", "E700:")
235 call assert_fails("LET &imactivatefunc = function('abc')", "E700:")
236 normal! i
237
238 #" set 'imactivatefunc' and 'imstatusfunc' to a non-existing function
239 set imactivatefunc=IMactivatefunc1
240 set imstatusfunc=IMstatusfunc1
241 call assert_fails("set imactivatefunc=function('NonExistingFunc')", 'E700:')
242 call assert_fails("set imstatusfunc=function('NonExistingFunc')", 'E700:')
243 call assert_fails("LET &imactivatefunc = function('NonExistingFunc')", 'E700:')
244 call assert_fails("LET &imstatusfunc = function('NonExistingFunc')", 'E700:')
245 normal! i
246
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000247 call assert_equal(14, g:IMactivatefunc_called)
248 call assert_equal(28, g:IMstatusfunc_called)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000249 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000250 call v9.CheckLegacyAndVim9Success(lines)
Yegappan Lakshmanan6ae8fae2021-12-12 16:26:44 +0000251
252 " Using Vim9 lambda expression in legacy context should fail
253 set imactivatefunc=(a)\ =>\ IMactivatefunc1(a)
254 set imstatusfunc=IMstatusfunc1
255 call assert_fails('normal! i', 'E117:')
256 set imactivatefunc=IMactivatefunc1
257 set imstatusfunc=()\ =>\ IMstatusfunc1(a)
258 call assert_fails('normal! i', 'E117:')
259
260 " set 'imactivatefunc' and 'imstatusfunc' to a partial with dict. This used
261 " to cause a crash.
262 func SetIMFunc()
263 let params1 = {'activate': function('g:DictActivateFunc')}
264 let params2 = {'status': function('g:DictStatusFunc')}
265 let &imactivatefunc = params1.activate
266 let &imstatusfunc = params2.status
267 endfunc
268 func g:DictActivateFunc(_) dict
269 endfunc
270 func g:DictStatusFunc(_) dict
271 endfunc
272 call SetIMFunc()
273 new
274 call SetIMFunc()
275 bw
276 call test_garbagecollect_now()
277 new
278 set imactivatefunc=
279 set imstatusfunc=
280 wincmd w
281 set imactivatefunc=
282 set imstatusfunc=
283 :%bw!
284 delfunc g:DictActivateFunc
285 delfunc g:DictStatusFunc
286 delfunc SetIMFunc
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000287
288 " Vim9 tests
289 let lines =<< trim END
290 vim9script
291
292 # Test for using function()
293 def IMactivatefunc1(active: number): any
294 g:IMactivatefunc_called += 1
295 return 1
296 enddef
297 def IMstatusfunc1(): number
298 g:IMstatusfunc_called += 1
299 return 1
300 enddef
301 g:IMactivatefunc_called = 0
302 g:IMstatusfunc_called = 0
303 set iminsert=2
304 set imactivatefunc=function('IMactivatefunc1')
305 set imstatusfunc=function('IMstatusfunc1')
306 normal! i
307
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000308 set iminsert=0
309 set imactivatefunc=
310 set imstatusfunc=
311 END
Bram Moolenaar62aec932022-01-29 21:45:34 +0000312 call v9.CheckScriptSuccess(lines)
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000313
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000314 " cleanup
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000315 set iminsert=0
316 set imactivatefunc&
317 set imstatusfunc&
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000318 delfunc IMactivatefunc1
319 delfunc IMstatusfunc1
Yegappan Lakshmanan04ef1fb2021-12-12 20:08:05 +0000320 unlet g:IMactivatefunc_called g:IMstatusfunc_called
Yegappan Lakshmanan7645da52021-12-04 14:02:30 +0000321 %bw!
322endfunc
323
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100324" vim: shiftwidth=2 sts=2 expandtab