" fcitx-osx.vim - 智能记忆输入法状态 " Author: Gavin Chan " Modified by: codefalling, enhanced with smart IM state memory " Version: 2.0.0 " Description: 记住退出 Insert 模式时的输入法状态,下次进入时自动恢复 " --------------------------------------------------------------------- " Load Once: if exists('g:fcitx_remote') finish endif if &ttimeoutlen <= 0 || &ttimeoutlen > 50 set ttimeoutlen=50 endif if (has("win32") || has("win95") || has("win64") || has("win16")) " Windows 下不要载入 finish endif if exists('$SSH_TTY') finish endif if !executable("fcitx-remote") finish endif let s:keepcpo = &cpo let g:loaded_fcitx = 1 set cpo&vim " --------------------------------------------------------------------- " 全局变量:记住上次 Insert 模式的输入法名称 let g:fcitx_last_insert_im_name = '' " 英文和中文输入法的 ID(可以通过 fcitx-remote -n 获取) if !exists('g:fcitx_english_im') let g:fcitx_english_im = 'com.apple.keylayout.ABC' endif if !exists('g:fcitx_chinese_im') let g:fcitx_chinese_im = 'auto-detect' " 将自动检测 endif " --------------------------------------------------------------------- " Functions: function! FcitxCurrentIM() abort return substitute(system('fcitx-remote -n 2>/dev/null'), '[\r\n]', '', 'g') endfunction function! FcitxSwitch(im) abort silent! call system('fcitx-remote -s ' . shellescape(a:im) . ' >/dev/null 2>&1') endfunction function! FcitxStart() abort call FcitxSwitch(g:fcitx_english_im) endfunction " 离开 Insert 模式:保存当前输入法名称,然后切换到英文 function! Fcitx2en() abort let current_im = FcitxCurrentIM() if current_im != '' let g:fcitx_last_insert_im_name = current_im endif call FcitxSwitch(g:fcitx_english_im) endfunction " 进入 Insert 模式:根据保存的输入法名称恢复 function! Fcitx2zh() abort if g:fcitx_last_insert_im_name == '' || g:fcitx_last_insert_im_name == g:fcitx_english_im return endif call FcitxSwitch(g:fcitx_last_insert_im_name) endfunction " --------------------------------------------------------------------- " Autocmds: " 绑定自动命令 function! BindAu() augroup Fcitx autocmd! " 离开 Insert 模式:保存输入法状态并切换到英文 autocmd InsertLeave * call Fcitx2en() " 进入 Insert 模式:恢复上次的输入法状态 autocmd InsertEnter * call Fcitx2zh() " Vim 启动后:切换到英文 autocmd VimEnter * call timer_start(100, {-> FcitxStart()}) augroup END endfunction function! UnBindAu() augroup Fcitx autocmd! augroup END endfunction call BindAu() " 多光标支持:在选择多个光标前后的处理 function! Multiple_cursors_before() call UnBindAu() endfunction function! Multiple_cursors_after() call Fcitx2en() call BindAu() endfunction " --------------------------------------------------------------------- " Restoration And Modelines: let &cpo=s:keepcpo unlet s:keepcpo let g:fcitx_remote = 1 " vim:fdm=expr:fde=getline(v\:lnum-1)=~'\\v"\\s*-{20,}'?'>1'\:1