diff options
author | Teddy Wing | 2023-03-19 05:10:06 +0100 |
---|---|---|
committer | Teddy Wing | 2023-03-19 19:52:34 +0100 |
commit | 744e5df5cbf66e6f7fcab7920de2eee12e3456cd (patch) | |
tree | e210a18abf8e33c2d1913d7c327a434441ccdac5 /bundle | |
parent | f82897261ad36a95b7227b3aab0a8301edf72d39 (diff) | |
download | dotvim-744e5df5cbf66e6f7fcab7920de2eee12e3456cd.tar.bz2 |
insert-layout: Move functions to autoload
Clean up the plugin file and move the bulk of the work to autoload.
Diffstat (limited to 'bundle')
-rw-r--r-- | bundle/insert-layout/autoload/insert_layout.vim | 75 | ||||
-rw-r--r-- | bundle/insert-layout/plugin/insert_layout.vim | 79 |
2 files changed, 80 insertions, 74 deletions
diff --git a/bundle/insert-layout/autoload/insert_layout.vim b/bundle/insert-layout/autoload/insert_layout.vim new file mode 100644 index 0000000..e9f53b4 --- /dev/null +++ b/bundle/insert-layout/autoload/insert_layout.vim @@ -0,0 +1,75 @@ +vim9script + +const layout_names = { + us: 'US', + qwazerty: 'QWAZERTY2', + fr: 'French-numerical', + dvorak: 'Dvorak', +} + +var normal_layout: string + +def IsLayoutSwitcherAvailable(): number + 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 + +export def InsertLayoutOff(): void + autocmd_delete([ + { + group: 'insert_layout', + event: 'InsertEnter', + bufnr: bufnr(), + }, + { + group: 'insert_layout', + event: 'InsertLeave', + bufnr: bufnr(), + }, + ]) +enddef + +export def InsertLayoutOn(layout: string): void + if !IsLayoutSwitcherAvailable() + echoerr "'xkbswitch' command not found" + return + endif + + 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 + +export def Complete(arg_lead: string, cmd_line: string, cursor_pos: number): string + return join(keys(layout_names), "\n") +enddef diff --git a/bundle/insert-layout/plugin/insert_layout.vim b/bundle/insert-layout/plugin/insert_layout.vim index b417fea..62c27f5 100644 --- a/bundle/insert-layout/plugin/insert_layout.vim +++ b/bundle/insert-layout/plugin/insert_layout.vim @@ -5,79 +5,10 @@ if exists('g:loaded_insert_layout') endif g:loaded_insert_layout = 1 -const layout_names = { - us: 'US', - qwazerty: 'QWAZERTY2', - fr: 'French-numerical', - dvorak: 'Dvorak', -} -var normal_layout: string +import autoload '../autoload/insert_layout.vim' -def IsLayoutSwitcherAvailable(): number - 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_delete([ - { - group: 'insert_layout', - event: 'InsertEnter', - bufnr: bufnr(), - }, - { - group: 'insert_layout', - event: 'InsertLeave', - bufnr: bufnr(), - }, - ]) -enddef - -def InsertLayoutOn(layout: string): void - if !IsLayoutSwitcherAvailable() - echoerr "'xkbswitch' command not found" - return - endif - - 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 - -def Complete(arg_lead: string, cmd_line: string, cursor_pos: number): string - return join(keys(layout_names), "\n") -enddef - -command! -nargs=1 -complete=custom,Complete InsertLayout InsertLayoutOn(<q-args>) -command! InsertLayoutOff InsertLayoutOff() +command! -nargs=1 -complete=custom,insert_layout.Complete + \ InsertLayout + \ insert_layout.InsertLayoutOn(<q-args>) +command! InsertLayoutOff insert_layout.InsertLayoutOff() |