initial fcitx5 version

This commit is contained in:
lilydjwg 2020-12-20 19:52:05 +08:00
parent 99a27b4323
commit 0dd5692687
4 changed files with 26 additions and 168 deletions

View file

@ -1,68 +1,23 @@
# vim:fileencoding=utf-8
import os
import vim
import socket
import struct
import contextlib
fcitxsocketfile = vim.eval('s:fcitxsocketfile')
import dbus
class FcitxComm(object):
STATUS = struct.pack('i', 0)
ACTIVATE = struct.pack('i', 1 | (1 << 16))
DEACTIVATE = struct.pack('i', 1)
INT_SIZE = struct.calcsize('i')
def __init__(self, socketfile):
if socketfile[0] == '@': # abstract socket
socketfile = '\x00' + socketfile[1:]
self.socketfile = socketfile
self.sock = None
class FcitxComm():
def __init__(self):
bus = dbus.SessionBus()
obj = bus.get_object('org.fcitx.Fcitx5', '/controller')
self.fcitx = dbus.Interface(obj, dbus_interface='org.fcitx.Fcitx.Controller1')
def status(self):
return self._with_socket(self._status) == 2
return self.fcitx.State() == 2
def activate(self):
self._with_socket(self._command, self.ACTIVATE)
self.fcitx.Activate()
def deactivate(self):
self._with_socket(self._command, self.DEACTIVATE)
self.fcitx.Deactivate()
def _error(self, e):
estr = str(e).replace('"', r'\"')
file = self.socketfile.replace('"', r'\"')
vim.command('echohl WarningMsg | echo "fcitx.vim: socket %s error: %s" | echohl NONE' % (file, estr))
def _connect(self):
self.sock = sock = socket.socket(socket.AF_UNIX)
sock.settimeout(0.5)
try:
sock.connect(self.socketfile)
return True
except (socket.error, socket.timeout) as e:
self._error(e)
return False
def _with_socket(self, func, *args, **kwargs):
# fcitx doesn't support connection reuse
if not self._connect():
return
with contextlib.closing(self.sock):
try:
return func(*args, **kwargs)
except (socket.error, socket.timeout, struct.error) as e:
self._error(e)
def _status(self):
self.sock.send(self.STATUS)
return struct.unpack('i', self.sock.recv(self.INT_SIZE))[0]
def _command(self, cmd):
self.sock.send(cmd)
Fcitx = FcitxComm(fcitxsocketfile)
Fcitx = FcitxComm()
def fcitx2en():
if Fcitx.status():

View file

@ -1,67 +1,21 @@
scriptencoding utf-8
" fcitx.vim remember fcitx's input state for each buffer
" Author: lilydjwg
" Version: 1.2.6
" Version: 2.0a
" URL: https://www.vim.org/scripts/script.php?script_id=3764
" ---------------------------------------------------------------------
" Load Once:
if &cp || exists("g:loaded_fcitx") || (
\ (!exists('$DISPLAY') || exists('$SSH_TTY') || has('gui_macvim'))
\ && !exists('$FCITX_SOCKET'))
finish
endif
if executable('fcitx5-remote')
" currently python version does not support fcitx5
let g:fcitx_remote = 'fcitx5-remote'
runtime so/fcitx.vim
finish
else
let g:fcitx_remote = 'fcitx-remote'
endif
if has("python3")
let python3 = 1
elseif has("python")
let python3 = 0
else
runtime so/fcitx.vim
if &cp || exists("g:loaded_fcitx") || !exists('$DISPLAY') || !has('python3')
finish
endif
let s:keepcpo = &cpo
set cpo&vim
" this is quicker than expand()
if exists('$FCITX_SOCKET')
let s:fcitxsocketfile = $FCITX_SOCKET
else
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 "socket file of fcitx not found, fcitx.vim not loaded."
echohl None
finish
endif
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
exe 'py3file' expand('<sfile>:r') . '.py'
au InsertLeave * py3 fcitx2en()
au InsertEnter * py3 fcitx2zh()
" ---------------------------------------------------------------------
" Restoration And Modelines:
unlet python3
unlet pyfile
let &cpo=s:keepcpo
unlet s:keepcpo