Also support fcitx5-remote to toggle IME state (#27)

* Also support fcitx5-remote to toggle IME state

* Fix types and wording
This commit is contained in:
oxalica 2021-08-20 16:31:07 +08:00 committed by GitHub
parent 4042bbb29c
commit 5c7b8e5833
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 78 additions and 15 deletions

View file

@ -5,27 +5,66 @@ scriptencoding utf-8
" URL: https://www.vim.org/scripts/script.php?script_id=3764
" ---------------------------------------------------------------------
" Load Once:
if &cp || exists("g:loaded_fcitx") || !exists('$DISPLAY') || !has('python3')
if &cp || exists("g:loaded_fcitx") || !exists('$DISPLAY')
finish
endif
let s:keepcpo = &cpo
set cpo&vim
let g:loaded_fcitx = 1
try " abort on fail
exe 'py3file' expand('<sfile>:r') . '.py'
if py3eval('fcitx_loaded')
if exists('##InsertLeavePre')
au InsertLeavePre * py3 fcitx2en()
else
au InsertLeave * py3 fcitx2en()
" If g:fcitx5_remote is set (to the path to `fcitx5-remove`), use it to toggle IME state.
if exists("g:fcitx5_remote")
function Fcitx2en()
let inputstatus = trim(system(g:fcitx5_remote))
if inputstatus == '2'
let b:inputtoggle = 1
call system(g:fcitx5_remote . ' -c')
endif
au InsertEnter * py3 fcitx2zh()
au CmdlineEnter [/\?] py3 fcitx2zh()
au CmdlineLeave [/\?] py3 fcitx2en()
endfunction
function Fcitx2zh()
try
if b:inputtoggle == 1
call system(g:fcitx5_remote . ' -o')
let b:inputtoggle = 0
endif
catch /inputtoggle/
let b:inputtoggle = 0
endtry
endfunction
let g:loaded_fcitx = 1
" Otherwise, if python3 is available, use python and dbus to toggle IME state.
elseif has('python3')
try " abort on fail
exe 'py3file' expand('<sfile>:r') . '.py'
if py3eval('fcitx_loaded')
function Fcitx2en()
py3 fcitx2en()
endfunction
function Fcitx2zh()
py3 fcitx2zh()
endfunction
let g:loaded_fcitx = 1
endif
endtry
endif
" Register autocmd if successfully loaded.
if exists("g:loaded_fcitx")
if exists('##InsertLeavePre')
au InsertLeavePre * call Fcitx2en()
else
au InsertLeave * call Fcitx2en()
endif
endtry
au InsertEnter * call Fcitx2zh()
au CmdlineEnter [/\?] call Fcitx2zh()
au CmdlineLeave [/\?] call Fcitx2en()
endif
" ---------------------------------------------------------------------
" Restoration And Modelines:
let &cpo=s:keepcpo
unlet s:keepcpo
" vim: sw=2 :