Version 1.0: Initial upload

This commit is contained in:
lilydjwg 2011-10-07 00:00:00 +00:00 committed by Able Scraper
commit 881a9db748
4 changed files with 168 additions and 0 deletions

18
README Normal file
View file

@ -0,0 +1,18 @@
This is a mirror of http://www.vim.org/scripts/script.php?script_id=3764
(fcitx is a Chinese input method so no English description.)
Fcitx 输入法自动切换
在离开/重新进入插入模式时自动切换输入法状态,以便在普通模式下始终是英文输入模式,切换回插入模式时恢复离开时的输入法输入模式。状态为每个缓冲区单独保存,所以在中文文档和英文代码间交叉跳转编辑也得心应手!
无需任何配置!
要求:
fcitx 版本 3.6 以上,建议 fcitx 4.0 以上
可选:
Python 3 或者 Python 支持以获得更快更好的效果。注意对于 vim 版本<7.3.288,如果同时编译了 Python 2 & 3 支持,因为此 vim 不能同时运行两个版本的 Python而本脚本首先检查 Python3所以会导致出错或者 Python 2 不可用。
开发:
它是 https://github.com/lilydjwg/dotvim 的一部分

45
plugin/fcitx.py Normal file
View file

@ -0,0 +1,45 @@
#!/usr/bin/env python3
# vim:fileencoding=utf-8
import os
import vim
import socket
import struct
FCITX_STATUS = struct.pack('i', 0)
FCITX_OPEN = struct.pack('i', 1 | (1 << 16))
FCITX_CLOSE = struct.pack('i', 1)
INT_SIZE = struct.calcsize('i')
fcitxsocketfile = vim.eval('s:fcitxsocketfile')
def fcitxtalk(command=None):
sock = socket.socket(socket.AF_UNIX)
try:
sock.connect(fcitxsocketfile)
except socket.error:
vim.command('echohl WarningMsg | echo "fcitx.vim: socket 连接出错" | echohl NONE')
return
try:
if not command:
sock.send(FCITX_STATUS)
return struct.unpack('i', sock.recv(INT_SIZE))[0]
elif command == 'c':
sock.send(FCITX_CLOSE)
elif command == 'o':
sock.send(FCITX_OPEN)
else:
raise ValueError('未知命令')
finally:
sock.close()
def fcitx2en():
if fcitxtalk() == 2:
vim.command('let b:inputtoggle = 1')
fcitxtalk('c')
def fcitx2zh():
if vim.eval('exists("b:inputtoggle")') == '1':
if vim.eval('b:inputtoggle') == '1':
fcitxtalk('o')
vim.command('let b:inputtoggle = 0')
else:
vim.command('let b:inputtoggle = 0')

56
plugin/fcitx.vim Normal file
View file

@ -0,0 +1,56 @@
scriptencoding utf-8
" fcitx.vim 记住插入模式小企鹅输入法的状态
" Author: lilydjwg
" Maintainer: lilydjwg
" ---------------------------------------------------------------------
" Load Once:
if &cp || exists("g:loaded_fcitx") || !exists('$DISPLAY') || exists('$SSH_TTY')
finish
endif
if has("python3")
let python3 = 1
elseif has("python")
let python3 = 0
else
echohl WarningMsg
echomsg "fcitx.vim: 没有 Python 支持,尝试使用旧版本。"
echohl None
runtime so/fcitx.vim
finish
endif
let s:keepcpo = &cpo
set cpo&vim
" this is quicker than expand()
let s:fcitxsocketfile = '/tmp/fcitx-socket-' . $DISPLAY
if !filewritable(s:fcitxsocketfile) "try again
if strridx(s:fcitxsocketfile, '.') > 0
let s:fcitxsocketfile = strpart(s:fcitxsocketfile, 0,
\ strridx(s:fcitxsocketfile, '.'))
else
let s:fcitxsocketfile = s:fcitxsocketfile . '.0'
if !filewritable(s:fcitxsocketfile)
echohl WarningMsg
echomsg "没有找到 fcitx 的 socket 文件fcitx.vim 没有载入。"
echohl None
finish
endif
endif
endif
let g:loaded_fcitx = 1
let pyfile = expand('<sfile>:r') . '.py'
if python3
exe 'py3file' pyfile
au InsertLeave * py3 fcitx2en()
au InsertEnter * py3 fcitx2zh()
else
exe 'pyfile' pyfile
au InsertLeave * py fcitx2en()
au InsertEnter * py fcitx2zh()
endif
" ---------------------------------------------------------------------
" Restoration And Modelines:
unlet python3
unlet pyfile
let &cpo=s:keepcpo
unlet s:keepcpo
" vim:fdm=expr:fde=getline(v\:lnum-1)=~'\\v"\\s*-{20,}'?'>1'\:1

49
so/fcitx.vim Normal file
View file

@ -0,0 +1,49 @@
" fcitx.vim 记住插入模式小企鹅输入法的状态
" Author: lilydjwg
" Maintainer: lilydjwg
" Last Change: 2010-11-18
" Note: 另有使用 Python3 接口的新版本
" ---------------------------------------------------------------------
" Load Once:
if (has("win32") || has("win95") || has("win64") || has("win16"))
" Windows 下不要载入
finish
endif
if !exists('$DISPLAY')
" 没有 X不要载入
finish
endif
if &cp || exists("g:loaded_fcitx") || !executable("fcitx-remote")
finish
endif
let s:keepcpo = &cpo
let g:loaded_fcitx = 1
set cpo&vim
" ---------------------------------------------------------------------
" Functions:
function Fcitx2en()
let inputstatus = system("fcitx-remote")
if inputstatus == 2
let b:inputtoggle = 1
call system("fcitx-remote -c")
endif
endfunction
function Fcitx2zh()
try
if b:inputtoggle == 1
call system("fcitx-remote -o")
let b:inputtoggle = 0
endif
catch /inputtoggle/
let b:inputtoggle = 0
endtry
endfunction
" ---------------------------------------------------------------------
" Autocmds:
au InsertLeave * call Fcitx2en()
au InsertEnter * call Fcitx2zh()
" ---------------------------------------------------------------------
" Restoration And Modelines:
let &cpo=s:keepcpo
unlet s:keepcpo
" vim:fdm=expr:fde=getline(v\:lnum-1)=~'\\v"\\s*-{20,}'?'>1'\:1