diff options
author | Teddy Wing | 2023-03-18 17:05:06 +0100 |
---|---|---|
committer | Teddy Wing | 2023-03-19 19:52:34 +0100 |
commit | 3bc45fa6ac18ad54dd595054b6051df48d4e228c (patch) | |
tree | b0a376a3490bbf8b7d101a2bb4059545cf54f55b /bundle/insert-layout/plugin/insert_layout.vim | |
parent | 7a52016eaff478ce8cd55db1314e900f32bf2177 (diff) | |
download | dotvim-3bc45fa6ac18ad54dd595054b6051df48d4e228c.tar.bz2 |
insert-layout: Add commands to turn insert layout on and off
Add two new commands, `InsertLayout` and `InsertLayoutOff`, which allow
us to activate a different keyboard layout for insert mode and restore
the previous layout when leaving insert wode.
Works reasonably well, but needs a bit of cleaning up now.
Diffstat (limited to 'bundle/insert-layout/plugin/insert_layout.vim')
-rw-r--r-- | bundle/insert-layout/plugin/insert_layout.vim | 95 |
1 files changed, 89 insertions, 6 deletions
diff --git a/bundle/insert-layout/plugin/insert_layout.vim b/bundle/insert-layout/plugin/insert_layout.vim index f271264..5ca3d7b 100644 --- a/bundle/insert-layout/plugin/insert_layout.vim +++ b/bundle/insert-layout/plugin/insert_layout.vim @@ -1,7 +1,90 @@ -" 2023.03.09 +vim9script -augroup frinsert - autocmd! - autocmd InsertEnter * :call system('xkbswitch -se French-numerical') - autocmd InsertLeave * :call system('xkbswitch -se US') -augroup END +if exists('g:loaded_insert_layout') + finish +endif +g:loaded_insert_layout = 1 + +const layout_names = { + us: 'US', + qwazerty: 'QWAZERTY2', + fr: 'French-numerical', + dvorak: 'Dvorak', +} + +var normal_layout: string + +def IsLayoutSwitcherAvailable(): bool + return executable('xkbswitch') +enddef + +def CurrentInputLayout(): string + return system('xkbswitch -ge') +enddef + +def SetInputLayout(layout: string): void + system('xkbswitch -se ' .. layout) +enddef + +def OnInsertEnter(layout: string): void + normal_layout = CurrentInputLayout() + + SetInputLayout(layout) +enddef + +def OnInsertLeave(): void + SetInputLayout(normal_layout) +enddef + +def InsertLayoutOff(): void + # autocmd! insert_layout InsertEnter <buffer> + # autocmd! insert_layout InsertLeave <buffer> + + autocmd_delete([ + { + group: 'insert_layout', + event: 'InsertEnter', + bufnr: bufnr(), + }, + { + group: 'insert_layout', + event: 'InsertLeave', + bufnr: bufnr(), + }, + ]) +enddef + +def InsertLayoutOn(layout: string): void + # InsertLayoutOff() + + # autocmd insert_layout InsertEnter <buffer> OnInsertEnter(layout) + # autocmd insert_layout InsertLeave <buffer> OnInsertLeave() + autocmd_add([ + { + replace: true, + group: 'insert_layout', + event: 'InsertEnter', + bufnr: bufnr(), + cmd: 'OnInsertEnter("' .. layout_names[layout] .. '")', + }, + { + replace: true, + group: 'insert_layout', + event: 'InsertLeave', + bufnr: bufnr(), + cmd: 'OnInsertLeave()', + }, + ]) +enddef + +# TODO: Add layout completion +command! -nargs=1 InsertLayout InsertLayoutOn(<q-args>) +command! InsertLayoutOff InsertLayoutOff() + +# SetInputLayout(layout_names['fr']) + +# augroup frinsert +# autocmd! +# autocmd InsertEnter * :call system('xkbswitch -se French-numerical') +# autocmd InsertLeave * :call system('xkbswitch -se US') +# augroup END |