mirror of
https://github.com/lilydjwg/fcitx.vim.git
synced 2025-12-27 02:24:57 +08:00
Version 1.0: Initial upload
This commit is contained in:
commit
748342ed76
4 changed files with 168 additions and 0 deletions
45
plugin/fcitx.py
Normal file
45
plugin/fcitx.py
Normal 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
56
plugin/fcitx.vim
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue