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 |
Yegappan Lakshmanan | 7645da5 | 2021-12-04 14:02:30 +0000 | [diff] [blame] | 5 | source vim9.vim |
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 |
| 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! |
| 248 | endfunc |
| 249 | |
Bram Moolenaar | 0546d7d | 2020-03-01 16:53:09 +0100 | [diff] [blame] | 250 | " vim: shiftwidth=2 sts=2 expandtab |