blob: 14063f9d89e3f315d9927b5cb198534217748162 [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
122 let g:IMactivatefunc_called = 0
123 let g:IMstatusfunc_called = 0
124 set iminsert=2
125
126 " Test for using a function()
127 set imactivatefunc=function('IMactivatefunc1')
128 set imstatusfunc=function('IMstatusfunc1')
129 normal! i
130
131 " Using a funcref variable to set 'completefunc'
132 let Fn1 = function('IMactivatefunc1')
133 let &imactivatefunc = string(Fn1)
134 let Fn2 = function('IMstatusfunc1')
135 let &imstatusfunc = string(Fn2)
136 normal! i
137 call assert_fails('let &imactivatefunc = Fn1', 'E729:')
138 call assert_fails('let &imstatusfunc = Fn2', 'E729:')
139
140 " Test for using a funcref()
141 set imactivatefunc=funcref('IMactivatefunc1')
142 set imstatusfunc=funcref('IMstatusfunc1')
143 normal! i
144
145 " Using a funcref variable to set 'imactivatefunc'
146 let Fn1 = funcref('IMactivatefunc1')
147 let &imactivatefunc = string(Fn1)
148 let Fn2 = funcref('IMstatusfunc1')
149 let &imstatusfunc = string(Fn2)
150 normal! i
151 call assert_fails('let &imactivatefunc = Fn1', 'E729:')
152 call assert_fails('let &imstatusfunc = Fn2', 'E729:')
153
154 " Test for using a lambda function
155 set imactivatefunc={a\ ->\ IMactivatefunc1(a)}
156 set imstatusfunc={\ ->\ IMstatusfunc1()}
157 normal! i
158
159 " Set 'imactivatefunc' and 'imstatusfunc' to a lambda expression
160 let &imactivatefunc = '{a -> IMactivatefunc1(a)}'
161 let &imstatusfunc = '{ -> IMstatusfunc1()}'
162 normal! i
163
164 " Set 'imactivatefunc' 'imstatusfunc' to a variable with a lambda expression
165 let Lambda1 = {a -> IMactivatefunc1(a)}
166 let Lambda2 = { -> IMstatusfunc1()}
167 let &imactivatefunc = string(Lambda1)
168 let &imstatusfunc = string(Lambda2)
169 normal! i
170 call assert_fails('let &imactivatefunc = Lambda1', 'E729:')
171 call assert_fails('let &imstatusfunc = Lambda2', 'E729:')
172
173 " Test for clearing the 'completefunc' option
174 set imactivatefunc='' imstatusfunc=''
175 set imactivatefunc& imstatusfunc&
176
177 call assert_fails("set imactivatefunc=function('abc')", "E700:")
178 call assert_fails("set imstatusfunc=function('abc')", "E700:")
179 call assert_fails("set imactivatefunc=funcref('abc')", "E700:")
180 call assert_fails("set imstatusfunc=funcref('abc')", "E700:")
181
182 call assert_equal(7, g:IMactivatefunc_called)
183 call assert_equal(14, g:IMstatusfunc_called)
184
185 " Vim9 tests
186 let lines =<< trim END
187 vim9script
188
189 # Test for using function()
190 def IMactivatefunc1(active: number): any
191 g:IMactivatefunc_called += 1
192 return 1
193 enddef
194 def IMstatusfunc1(): number
195 g:IMstatusfunc_called += 1
196 return 1
197 enddef
198 g:IMactivatefunc_called = 0
199 g:IMstatusfunc_called = 0
200 set iminsert=2
201 set imactivatefunc=function('IMactivatefunc1')
202 set imstatusfunc=function('IMstatusfunc1')
203 normal! i
204
205 # Test for using a lambda
206 &imactivatefunc = '(a) => IMactivatefunc1(a)'
207 &imstatusfunc = '() => IMstatusfunc1()'
208 normal! i
209
210 # Test for using a variable with a lambda expression
211 var Fn1: func = (active) => {
212 g:IMactivatefunc_called += 1
213 return 1
214 }
215 var Fn2: func = () => {
216 g:IMstatusfunc_called += 1
217 return 1
218 }
219 &imactivatefunc = string(Fn1)
220 &imstatusfunc = string(Fn2)
221 normal! i
222
223 assert_equal(3, g:IMactivatefunc_called)
224 assert_equal(6, g:IMstatusfunc_called)
225
226 set iminsert=0
227 set imactivatefunc=
228 set imstatusfunc=
229 END
230 call CheckScriptSuccess(lines)
231
232 " Using Vim9 lambda expression in legacy context should fail
233 set imactivatefunc=(a)\ =>\ IMactivatefunc1(a)
234 set imstatusfunc=IMstatusfunc1
235 call assert_fails('normal! i', 'E117:')
236 set imactivatefunc=IMactivatefunc1
237 set imstatusfunc=()\ =>\ IMstatusfunc1(a)
238 call assert_fails('normal! i', 'E117:')
239
240 " cleanup
241 delfunc IMactivatefunc1
242 delfunc IMstatusfunc1
243 set iminsert=0
244 set imactivatefunc=
245 set imstatusfunc=
246
247 %bw!
248endfunc
249
Bram Moolenaar0546d7d2020-03-01 16:53:09 +0100250" vim: shiftwidth=2 sts=2 expandtab