lyokha / vim-xkbswitch

vim plugin for automatic keyboard layout switching in insert mode
MIT License
481 stars 23 forks source link

[question] how to change input method in insert mode? #44

Closed Freed-Wu closed 4 years ago

Freed-Wu commented 4 years ago

sorry for my disturbance!

in insert mode, i press <c-o>:call system('g3kb-switch -s libpinyin')<CR> (libpinyin is an input method)

the input method don't change. but when i quit the insert mode, it change to libpinyin in normal mode...

i know SuperSpace can switch input method. but i want a method to change it in vim. image

thanks!

Freed-Wu commented 4 years ago

i try call libcall('/usr/local/lib/libg3kbswitch.so', 'Xkb_Switch_setXkbLayout', 'libpinyin'). the result is same as call system('g3kb-switch -s libpinyin')...

lyokha commented 4 years ago

Hi.

I wonder, why are you calling system/lib methods when you can simply switch layout by a system method/key? Or is this for a script?

I checked behaviour with xkb-switch and I believe that g3kb-switch must behave the same way. So, why this happens:

1) You are in Insert mode with, say, us keyboard layout, 2) You pressed <C-o> thus effectively switching to Nomal mode, the plugin saved previous layout in Insert mode as us, 3) You changed keyboard layout to libpinyin (I used ru) and press Enter, 4) Insert mode returns and brings back us,

at this step you may notice a fast blink of the language selector in the system tray: it fastly switched to libpinyin and back.

Then:

5) You exited from Insert mode and unexpectedly got libpinyin,

this is because the layout for Normal mode has been saved as well, and this was libpinyin when the Normal mode was abandoned.

To prevent this switch, you can put into the .vimrc line

let g:XkbSwitchNLayout = 'us'

Now keyboard layout in Normal mode will always be us, it seems to be a sane setting for all cases.

Freed-Wu commented 4 years ago

Now keyboard layout in Normal mode will always be us, it seems to be a sane setting for all cases.

thank for your answer! you are right.

I wonder, why are you calling system/lib methods when you can simply switch layout by a system method/key? Or is this for a script?

in truth, i want to take a try.

at this step you may notice a fast blink of the language selector in the system tray: it fastly switched to libpinyin and back.

why it fastly switch back? can i know the reason? thanks!

lyokha commented 4 years ago

Hmm, i told you why it happens, but did not answer the question how to switch layout in Insert mode... Probably, it is not possible with the current implementation. I will tell you if I get the answer.

lyokha commented 4 years ago

Hah, this is easily achieved if playing with the plugin internals, simply replace

<c-o>:call system('g3kb-switch -s libpinyin')<CR>

with

<c-o>:let b:xkb_ilayout='libpinyin'<CR>

Now the switch must work correctly, as you expect!

Freed-Wu commented 4 years ago

Thanks!