Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 1 | " Test for 'iminsert' |
| 2 | |
Bram Moolenaar | 6315a9a | 2017-11-25 15:20:02 +0100 | [diff] [blame] | 3 | source view_util.vim |
Bram Moolenaar | e2a3f36 | 2019-12-07 21:40:48 +0100 | [diff] [blame] | 4 | source check.vim |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 5 | import './vim9.vim' as v9 |
Bram Moolenaar | 6315a9a | 2017-11-25 15:20:02 +0100 | [diff] [blame] | 6 | |
| 7 | let s:imactivatefunc_called = 0 |
| 8 | let s:imstatusfunc_called = 0 |
Bram Moolenaar | a3a1246 | 2019-09-07 15:08:38 +0200 | [diff] [blame] | 9 | let s:imstatus_active = 0 |
Bram Moolenaar | 6315a9a | 2017-11-25 15:20:02 +0100 | [diff] [blame] | 10 | |
| 11 | func IM_activatefunc(active) |
| 12 | let s:imactivatefunc_called = 1 |
Bram Moolenaar | 845e0ee | 2020-06-20 16:05:32 +0200 | [diff] [blame] | 13 | let s:imstatus_active = a:active |
Bram Moolenaar | 6315a9a | 2017-11-25 15:20:02 +0100 | [diff] [blame] | 14 | endfunc |
| 15 | |
| 16 | func IM_statusfunc() |
| 17 | let s:imstatusfunc_called = 1 |
Bram Moolenaar | a3a1246 | 2019-09-07 15:08:38 +0200 | [diff] [blame] | 18 | return s:imstatus_active |
Bram Moolenaar | 6315a9a | 2017-11-25 15:20:02 +0100 | [diff] [blame] | 19 | endfunc |
| 20 | |
| 21 | func Test_iminsert2() |
Bram Moolenaar | a3a1246 | 2019-09-07 15:08:38 +0200 | [diff] [blame] | 22 | let s:imactivatefunc_called = 0 |
| 23 | let s:imstatusfunc_called = 0 |
| 24 | |
Bram Moolenaar | 6315a9a | 2017-11-25 15:20:02 +0100 | [diff] [blame] | 25 | 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 Moolenaar | 2877d33 | 2017-11-26 14:56:16 +0100 | [diff] [blame] | 32 | |
Bram Moolenaar | c19fd91 | 2020-07-02 20:59:05 +0200 | [diff] [blame] | 33 | let expected = (has('win32') && has('gui_running')) ? 0 : 1 |
Bram Moolenaar | 2877d33 | 2017-11-26 14:56:16 +0100 | [diff] [blame] | 34 | call assert_equal(expected, s:imactivatefunc_called) |
| 35 | call assert_equal(expected, s:imstatusfunc_called) |
Bram Moolenaar | 6315a9a | 2017-11-25 15:20:02 +0100 | [diff] [blame] | 36 | endfunc |
Bram Moolenaar | a3a1246 | 2019-09-07 15:08:38 +0200 | [diff] [blame] | 37 | |
Bram Moolenaar | e2a3f36 | 2019-12-07 21:40:48 +0100 | [diff] [blame] | 38 | func Test_getimstatus() |
| 39 | if has('win32') |
| 40 | CheckFeature multi_byte_ime |
Bram Moolenaar | be7529e | 2020-08-13 21:05:39 +0200 | [diff] [blame] | 41 | else |
Bram Moolenaar | e2a3f36 | 2019-12-07 21:40:48 +0100 | [diff] [blame] | 42 | CheckFeature xim |
| 43 | endif |
Bram Moolenaar | c19fd91 | 2020-07-02 20:59:05 +0200 | [diff] [blame] | 44 | if has('win32') && has('gui_running') |
Bram Moolenaar | a3a1246 | 2019-09-07 15:08:38 +0200 | [diff] [blame] | 45 | 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= |
| 64 | endfunc |
Bram Moolenaar | 0546d7d | 2020-03-01 16:53:09 +0100 | [diff] [blame] | 65 | |
Bram Moolenaar | 1671f44 | 2020-03-10 07:48:13 +0100 | [diff] [blame] | 66 | " Test for using an lmap in insert mode |
| 67 | func 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! |
| 84 | endfunc |
| 85 | |
Bram Moolenaar | 845e0ee | 2020-06-20 16:05:32 +0200 | [diff] [blame] | 86 | " Test for using CTRL-^ to toggle iminsert in insert mode |
| 87 | func Test_iminsert_toggle() |
| 88 | CheckGui |
| 89 | if has('win32') |
| 90 | CheckFeature multi_byte_ime |
Bram Moolenaar | be7529e | 2020-08-13 21:05:39 +0200 | [diff] [blame] | 91 | else |
Bram Moolenaar | 845e0ee | 2020-06-20 16:05:32 +0200 | [diff] [blame] | 92 | CheckFeature xim |
| 93 | endif |
| 94 | if has('gui_running') && !has('win32') |
Bram Moolenaar | 6d91bcb | 2020-08-12 18:50:36 +0200 | [diff] [blame] | 95 | throw 'Skipped: works only in Win32 GUI version (for some reason)' |
Bram Moolenaar | 845e0ee | 2020-06-20 16:05:32 +0200 | [diff] [blame] | 96 | 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! |
| 109 | endfunc |
| 110 | |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 111 | " Test for different ways of setting the 'imactivatefunc' and 'imstatusfunc' |
| 112 | " options |
| 113 | func 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 |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 122 | set iminsert=2 |
| 123 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 124 | let lines =<< trim END |
| 125 | LET g:IMactivatefunc_called = 0 |
| 126 | LET g:IMstatusfunc_called = 0 |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 127 | |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 128 | #" Test for using a function name |
| 129 | LET &imactivatefunc = 'g:IMactivatefunc1' |
| 130 | LET &imstatusfunc = 'g:IMstatusfunc1' |
| 131 | normal! i |
| 132 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 133 | #" Test for using a function() |
| 134 | set imactivatefunc=function('g:IMactivatefunc1') |
| 135 | set imstatusfunc=function('g:IMstatusfunc1') |
| 136 | normal! i |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 137 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 138 | #" Using a funcref variable to set 'completefunc' |
| 139 | VAR Fn1 = function('g:IMactivatefunc1') |
| 140 | LET &imactivatefunc = Fn1 |
| 141 | VAR Fn2 = function('g:IMstatusfunc1') |
| 142 | LET &imstatusfunc = Fn2 |
| 143 | normal! i |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 144 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 145 | #" Using a string(funcref variable) to set 'completefunc' |
| 146 | LET &imactivatefunc = string(Fn1) |
| 147 | LET &imstatusfunc = string(Fn2) |
| 148 | normal! i |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 149 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 150 | #" Test for using a funcref() |
| 151 | set imactivatefunc=funcref('g:IMactivatefunc1') |
| 152 | set imstatusfunc=funcref('g:IMstatusfunc1') |
| 153 | normal! i |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 154 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 155 | #" Using a funcref variable to set 'imactivatefunc' |
| 156 | LET Fn1 = funcref('g:IMactivatefunc1') |
| 157 | LET &imactivatefunc = Fn1 |
| 158 | LET Fn2 = funcref('g:IMstatusfunc1') |
| 159 | LET &imstatusfunc = Fn2 |
| 160 | normal! i |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 161 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 162 | #" Using a string(funcref variable) to set 'imactivatefunc' |
| 163 | LET &imactivatefunc = string(Fn1) |
| 164 | LET &imstatusfunc = string(Fn2) |
| 165 | normal! i |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 166 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 167 | #" Test for using a lambda function |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 168 | VAR optval = "LSTART a LMIDDLE g:IMactivatefunc1(a) LEND" |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 169 | LET optval = substitute(optval, ' ', '\\ ', 'g') |
| 170 | exe "set imactivatefunc=" .. optval |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 171 | LET optval = "LSTART LMIDDLE g:IMstatusfunc1() LEND" |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 172 | LET optval = substitute(optval, ' ', '\\ ', 'g') |
| 173 | exe "set imstatusfunc=" .. optval |
| 174 | normal! i |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 175 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 176 | #" Set 'imactivatefunc' and 'imstatusfunc' to a lambda expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 177 | LET &imactivatefunc = LSTART a LMIDDLE g:IMactivatefunc1(a) LEND |
| 178 | LET &imstatusfunc = LSTART LMIDDLE g:IMstatusfunc1() LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 179 | normal! i |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 180 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 181 | #" Set 'imactivatefunc' and 'imstatusfunc' to a string(lambda expression) |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 182 | LET &imactivatefunc = 'LSTART a LMIDDLE g:IMactivatefunc1(a) LEND' |
| 183 | LET &imstatusfunc = 'LSTART LMIDDLE g:IMstatusfunc1() LEND' |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 184 | normal! i |
Yegappan Lakshmanan | 6409553 | 2021-12-06 11:03:55 +0000 | [diff] [blame] | 185 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 186 | #" Set 'imactivatefunc' 'imstatusfunc' to a variable with a lambda |
| 187 | #" expression |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 188 | VAR Lambda1 = LSTART a LMIDDLE g:IMactivatefunc1(a) LEND |
| 189 | VAR Lambda2 = LSTART LMIDDLE g:IMstatusfunc1() LEND |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 190 | LET &imactivatefunc = Lambda1 |
| 191 | LET &imstatusfunc = Lambda2 |
| 192 | normal! i |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 193 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 194 | #" Set 'imactivatefunc' 'imstatusfunc' to a string(variable with a lambda |
| 195 | #" expression) |
| 196 | LET &imactivatefunc = string(Lambda1) |
| 197 | LET &imstatusfunc = string(Lambda2) |
| 198 | normal! i |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 199 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 200 | #" Test for clearing the 'completefunc' option |
| 201 | set imactivatefunc='' imstatusfunc='' |
| 202 | set imactivatefunc& imstatusfunc& |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 203 | |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 204 | set imactivatefunc=g:IMactivatefunc1 |
| 205 | set imstatusfunc=g:IMstatusfunc1 |
| 206 | call assert_fails("set imactivatefunc=function('abc')", "E700:") |
| 207 | call assert_fails("set imstatusfunc=function('abc')", "E700:") |
| 208 | call assert_fails("set imactivatefunc=funcref('abc')", "E700:") |
| 209 | call assert_fails("set imstatusfunc=funcref('abc')", "E700:") |
| 210 | call assert_fails("LET &imstatusfunc = function('abc')", "E700:") |
| 211 | call assert_fails("LET &imactivatefunc = function('abc')", "E700:") |
| 212 | normal! i |
| 213 | |
| 214 | #" set 'imactivatefunc' and 'imstatusfunc' to a non-existing function |
| 215 | set imactivatefunc=IMactivatefunc1 |
| 216 | set imstatusfunc=IMstatusfunc1 |
| 217 | call assert_fails("set imactivatefunc=function('NonExistingFunc')", 'E700:') |
| 218 | call assert_fails("set imstatusfunc=function('NonExistingFunc')", 'E700:') |
| 219 | call assert_fails("LET &imactivatefunc = function('NonExistingFunc')", 'E700:') |
| 220 | call assert_fails("LET &imstatusfunc = function('NonExistingFunc')", 'E700:') |
| 221 | normal! i |
| 222 | |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 223 | call assert_equal(14, g:IMactivatefunc_called) |
| 224 | call assert_equal(28, g:IMstatusfunc_called) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 225 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 226 | call v9.CheckLegacyAndVim9Success(lines) |
Yegappan Lakshmanan | 6ae8fae | 2021-12-12 16:26:44 +0000 | [diff] [blame] | 227 | |
| 228 | " Using Vim9 lambda expression in legacy context should fail |
| 229 | set imactivatefunc=(a)\ =>\ IMactivatefunc1(a) |
| 230 | set imstatusfunc=IMstatusfunc1 |
| 231 | call assert_fails('normal! i', 'E117:') |
| 232 | set imactivatefunc=IMactivatefunc1 |
| 233 | set imstatusfunc=()\ =>\ IMstatusfunc1(a) |
| 234 | call assert_fails('normal! i', 'E117:') |
| 235 | |
| 236 | " set 'imactivatefunc' and 'imstatusfunc' to a partial with dict. This used |
| 237 | " to cause a crash. |
| 238 | func SetIMFunc() |
| 239 | let params1 = {'activate': function('g:DictActivateFunc')} |
| 240 | let params2 = {'status': function('g:DictStatusFunc')} |
| 241 | let &imactivatefunc = params1.activate |
| 242 | let &imstatusfunc = params2.status |
| 243 | endfunc |
| 244 | func g:DictActivateFunc(_) dict |
| 245 | endfunc |
| 246 | func g:DictStatusFunc(_) dict |
| 247 | endfunc |
| 248 | call SetIMFunc() |
| 249 | new |
| 250 | call SetIMFunc() |
| 251 | bw |
| 252 | call test_garbagecollect_now() |
| 253 | new |
| 254 | set imactivatefunc= |
| 255 | set imstatusfunc= |
| 256 | wincmd w |
| 257 | set imactivatefunc= |
| 258 | set imstatusfunc= |
| 259 | :%bw! |
| 260 | delfunc g:DictActivateFunc |
| 261 | delfunc g:DictStatusFunc |
| 262 | delfunc SetIMFunc |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 263 | |
| 264 | " Vim9 tests |
| 265 | let lines =<< trim END |
| 266 | vim9script |
| 267 | |
| 268 | # Test for using function() |
| 269 | def IMactivatefunc1(active: number): any |
| 270 | g:IMactivatefunc_called += 1 |
| 271 | return 1 |
| 272 | enddef |
| 273 | def IMstatusfunc1(): number |
| 274 | g:IMstatusfunc_called += 1 |
| 275 | return 1 |
| 276 | enddef |
| 277 | g:IMactivatefunc_called = 0 |
| 278 | g:IMstatusfunc_called = 0 |
| 279 | set iminsert=2 |
| 280 | set imactivatefunc=function('IMactivatefunc1') |
| 281 | set imstatusfunc=function('IMstatusfunc1') |
| 282 | normal! i |
| 283 | |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 284 | set iminsert=0 |
| 285 | set imactivatefunc= |
| 286 | set imstatusfunc= |
| 287 | END |
Bram Moolenaar | 62aec93 | 2022-01-29 21:45:34 +0000 | [diff] [blame] | 288 | call v9.CheckScriptSuccess(lines) |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 289 | |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 290 | " cleanup |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 291 | set iminsert=0 |
| 292 | set imactivatefunc& |
| 293 | set imstatusfunc& |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 294 | delfunc IMactivatefunc1 |
| 295 | delfunc IMstatusfunc1 |
Yegappan Lakshmanan | 04ef1fb | 2021-12-12 20:08:05 +0000 | [diff] [blame] | 296 | unlet g:IMactivatefunc_called g:IMstatusfunc_called |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 297 | %bw! |
| 298 | endfunc |
| 299 | |
Bram Moolenaar | 0546d7d | 2020-03-01 16:53:09 +0100 | [diff] [blame] | 300 | " vim: shiftwidth=2 sts=2 expandtab |