update at 2025-11-04 19:58:03
This commit is contained in:
@@ -1,10 +1,9 @@
|
||||
" fcitx.vim 记住插入模式小企鹅输入法的状态
|
||||
" Author: lilydjwg
|
||||
" Maintainer: lilydjwg
|
||||
" Modified by: codefalling
|
||||
" Note: 另有使用 Python3 接口的新版本
|
||||
" 此修改版用于 OS X 下的 https://github.com/CodeFalling/fcitx-remote-for-osx
|
||||
" ---------------------------------------------------------------------
|
||||
" 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
|
||||
@@ -27,63 +26,100 @@ 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 Fcitx2en()
|
||||
let inputstatus = system("fcitx-remote")
|
||||
if inputstatus == 2
|
||||
let b:inputtoggle = 1
|
||||
let t = system("fcitx-remote -c")
|
||||
|
||||
" 离开 Insert 模式:保存当前输入法名称,然后切换到英文
|
||||
function! Fcitx2en()
|
||||
" 保存当前输入法名称
|
||||
let current_im = substitute(system("fcitx-remote -n 2>/dev/null"), '\n', '', 'g')
|
||||
let g:fcitx_last_insert_im_name = current_im
|
||||
|
||||
" 切换到英文输入法(使用后台任务并重定向所有输出)
|
||||
if has('job')
|
||||
call job_start(['sh', '-c', 'fcitx-remote -s ' . shellescape(g:fcitx_english_im) . ' >/dev/null 2>&1'])
|
||||
else
|
||||
call system("fcitx-remote -s " . shellescape(g:fcitx_english_im) . " >/dev/null 2>&1 &")
|
||||
endif
|
||||
endfunction
|
||||
function Fcitx2zh()
|
||||
try
|
||||
if b:inputtoggle == 1
|
||||
let t = system("fcitx-remote -o")
|
||||
let b:inputtoggle = 0
|
||||
endif
|
||||
catch /inputtoggle/
|
||||
let b:inputtoggle = 0
|
||||
endtry
|
||||
|
||||
" 进入 Insert 模式:根据保存的输入法名称恢复
|
||||
function! Fcitx2zh()
|
||||
" 如果上次保存的输入法名称为空,说明是第一次进入,保持英文
|
||||
if g:fcitx_last_insert_im_name == ''
|
||||
return
|
||||
endif
|
||||
|
||||
" 如果上次是英文输入法,保持英文(不需要做任何事)
|
||||
if g:fcitx_last_insert_im_name == g:fcitx_english_im
|
||||
return
|
||||
endif
|
||||
|
||||
" 上次是中文输入法,恢复到那个输入法(使用后台任务并重定向所有输出)
|
||||
if has('job')
|
||||
call job_start(['sh', '-c', 'fcitx-remote -s ' . shellescape(g:fcitx_last_insert_im_name) . ' >/dev/null 2>&1'])
|
||||
else
|
||||
call system("fcitx-remote -s " . shellescape(g:fcitx_last_insert_im_name) . " >/dev/null 2>&1 &")
|
||||
endif
|
||||
endfunction
|
||||
" ---------------------------------------------------------------------
|
||||
" Autocmds:
|
||||
function Fcitx2zhOnce()
|
||||
|
||||
" 进入 Insert 模式一次后的处理
|
||||
function! Fcitx2zhOnce()
|
||||
call Fcitx2zh()
|
||||
call UnBindAu()
|
||||
endfunction
|
||||
|
||||
function BindAu2zhOnce()
|
||||
function! BindAu2zhOnce()
|
||||
augroup Fcitx
|
||||
au InsertEnter * call Fcitx2zhOnce()
|
||||
augroup END
|
||||
endfunction
|
||||
|
||||
function BindAu()
|
||||
" 绑定自动命令
|
||||
function! BindAu()
|
||||
augroup Fcitx
|
||||
" 离开 Insert 模式:保存输入法状态并切换到英文
|
||||
au InsertLeave * call Fcitx2en()
|
||||
" 进入 Insert 模式:恢复上次的输入法状态
|
||||
au InsertEnter * call Fcitx2zh()
|
||||
" Vim 启动时:切换到英文
|
||||
au VimEnter * call Fcitx2en()
|
||||
augroup END
|
||||
endfunction
|
||||
|
||||
function UnBindAu()
|
||||
function! UnBindAu()
|
||||
au! Fcitx InsertLeave *
|
||||
au! Fcitx InsertEnter *
|
||||
endfunction
|
||||
|
||||
"call once when enter insert mode instead of vim startup
|
||||
" 延迟初始化:首次进入 Insert 模式时才绑定自动命令
|
||||
let g:called_bind = 0
|
||||
function EchoBind()
|
||||
function! EchoBind()
|
||||
if (g:called_bind==0)
|
||||
call BindAu()
|
||||
endif
|
||||
let g:called_bind =1
|
||||
let g:called_bind = 1
|
||||
endfunction
|
||||
|
||||
autocmd InsertEnter * call EchoBind()
|
||||
|
||||
"Called once right before you start selecting multiple cursors
|
||||
" 多光标支持:在选择多个光标前后的处理
|
||||
function! Multiple_cursors_before()
|
||||
call UnBindAu()
|
||||
call BindAu2zhOnce()
|
||||
@@ -95,9 +131,10 @@ function! Multiple_cursors_after()
|
||||
endfunction
|
||||
|
||||
" ---------------------------------------------------------------------
|
||||
" Restoration And Modelines:
|
||||
" Restoration And Modelines:
|
||||
let &cpo=s:keepcpo
|
||||
unlet s:keepcpo
|
||||
"vim:fdm=expr:fde=getline(v\:lnum-1)=~'\\v"\\s*-{20,}'?'>1'\:1
|
||||
|
||||
let g:fcitx_remote = 1
|
||||
|
||||
" vim:fdm=expr:fde=getline(v\:lnum-1)=~'\\v"\\s*-{20,}'?'>1'\:1
|
||||
|
||||
Reference in New Issue
Block a user