Compare commits

..

49 commits
1.0 ... fcitx5

Author SHA1 Message Date
lilydjwg
28d5bcc86d add another function FcitxCurrentIMwithRime to show RIME info too
see https://github.com/lilydjwg/fcitx.vim/issues/33#issuecomment-2002346047
2024-04-21 16:21:34 +08:00
lilydjwg
451d277bbd don't execute when executing a macro 2023-08-03 23:51:05 +08:00
lilydjwg
92247352c6 fallback to cmd if Python's dbus module isn't available
closes #36.
2023-02-02 15:55:50 +08:00
lilydjwg
dc95d0301b fix not returning IM name when it's not rime 2022-11-15 22:06:01 +08:00
lilydjwg
eb62569b3b if current im is rime, return rime's schema name 2022-11-15 20:47:21 +08:00
lilydjwg
d79d816e24 add a function for current IM name
for showing info e.g. on the status line. closes #32.
2022-11-15 16:20:41 +08:00
lilydjwg
7f06ccfa8a fix reconnecting not using the RIME interface 2022-11-15 16:18:44 +08:00
lilydjwg
fe899f6aec update README about fcitx5_rime 2022-11-09 14:59:45 +08:00
lilydjwg
e962387307 add support for fcitx5-rime
https://github.com/fcitx/fcitx5-rime/issues/30#issuecomment-957001110
2022-11-09 12:32:49 +08:00
依云
c689083ffb
Merge pull request #29 from T2hhbmEK/wayland
add WAYLAND_DISPLAY
2021-10-26 18:25:01 +08:00
T2hhbmEK
84a183b46d add WAYLAND_DISPLAY 2021-10-26 17:55:02 +08:00
lilydjwg
3554b279a0 update README to make two modes' benefit clear 2021-08-20 16:32:24 +08:00
oxalica
5c7b8e5833
Also support fcitx5-remote to toggle IME state (#27)
* Also support fcitx5-remote to toggle IME state

* Fix types and wording
2021-08-20 16:31:07 +08:00
lilydjwg
4042bbb29c add LICENSE 2021-08-15 22:21:32 +08:00
依云
fcef347398
Merge pull request #24 from zeertzjq/patch-1
feat: support backward search mode
2021-06-25 16:01:20 +08:00
zeertzjq
bc068d0556
feat: support backward search mode 2021-06-25 15:54:03 +08:00
lilydjwg
92ef27262d make it possible to silent the warning on dbus errors
fixes #22.
2021-05-24 12:43:22 +08:00
依云
4c6ec0e384
Merge pull request #21 from lokiiart/patch-1
为rime用户节省一些时间吧。
2021-05-12 19:16:55 +08:00
lokiiart
2b86740c87
为rime用户节省一些时间吧。 2021-05-12 17:30:57 +08:00
依云
07b608571d
Merge pull request #20 from ouuan/fcitx5
feat: support search mode
2021-04-18 16:36:27 +08:00
Yufan You
bbca697f3c
feat: support search mode 2021-04-18 13:01:57 +08:00
lilydjwg
c038116610 support for earlier Vim that doesn't have InsertLeavePre 2021-02-08 14:29:37 +08:00
lilydjwg
1efd21b9aa README: update for the fcitx5-server branch 2021-02-05 16:34:07 +08:00
lilydjwg
695ee2b958 fix error handling from python to vim 2020-12-29 19:46:06 +08:00
lilydjwg
d619809504 better error handling 2020-12-23 15:40:21 +08:00
lilydjwg
056f8c580d reconnect dbus on error (maybe fcitx5 just was restarted) 2020-12-22 01:02:43 +08:00
lilydjwg
0d95b09914 README: fix lists being combined as one 2020-12-21 19:46:15 +08:00
lilydjwg
0dd5692687 initial fcitx5 version 2020-12-20 19:52:05 +08:00
lilydjwg
99a27b4323 remove modeline 2020-05-12 20:41:58 +08:00
lilydjwg
6267a6c073 bump version 2019-05-22 13:02:48 +08:00
lilydjwg
cd5b278dc4 README: better formatted 2018-08-29 16:09:41 +08:00
lilydjwg
bab3fd3afe GitHub renders plain text files too small, switching to markdown
closes #7.
2018-08-29 16:04:20 +08:00
依云
49983f1c49
Merge pull request #6 from meijieru/fcitx5
[RDY] fcitx5 vimscript
2018-05-31 18:51:01 +08:00
meijieru
b6fc70ffba fcitx5 vimscript 2018-05-31 18:45:22 +08:00
依云
83519f4ed3 Merge pull request #1 from gou4shi1/master
sockets in Python 2 don't support "with". use contextlib.closing instead.
2017-05-29 18:19:21 +08:00
lilydjwg
528f0f998f update link protocol 2017-05-24 10:54:35 +08:00
gou4shi1
5b33e919d9 there is no _EXIT_ in socket in python2 2017-05-15 19:07:15 +08:00
lilydjwg
3a69ec3ff6 fcitx socket connection 2017-04-18 10:18:46 +08:00
lilydjwg
e70e870b97 improved error message and connection handling 2017-04-15 12:45:13 +08:00
lilydjwg
be04ddd01c update README 2016-09-23 13:10:48 +08:00
lilydjwg
b31a3c7a08 minor update for Mac OS X 2015-11-03 19:00:52 +08:00
lilydjwg
cf7e2ab555 add timeout for socket operations 2013-11-14 11:49:24 +08:00
lilydjwg
c77e732eac update README 2013-11-05 11:09:45 +08:00
lilydjwg
8a4b2203f5 support abstract sockets 2013-11-05 11:03:45 +08:00
lilydjwg
dc5455d2f2 update from vim-scripts mirror 2013-11-05 11:02:56 +08:00
lilydjwg
9c757c161d Version 1.2.1: forgot to add the pure vimscript version: so/fcitx.vim 2012-10-07 17:43:04 -05:00
lilydjwg
6b07b1e728 Version 1.2
support to specify fcitx socket file by environment variable $FCITX_SOCKET
2012-08-08 01:24:44 -05:00
lilydjwg
4904d73230 Version 1.1.1: forgot to compress the tarball. -
lilydjwg
f83971b7f4 Version 1.1
* remove warning about not using Python interface
* messages translate into English as fcitx supports more and more languages
-
6 changed files with 253 additions and 140 deletions

21
LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2011-2021 lilydjwg <lilydjwg@gmail.com>, et al.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

18
README
View file

@ -1,18 +0,0 @@
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 的一部分

72
README.md Normal file
View file

@ -0,0 +1,72 @@
Keep and restore fcitx state for each buffer separately when leaving/re-entering insert mode or search mode. Like always typing English in normal mode, but Chinese in insert mode.
D-Bus only works with the same user so this won't work with `sudo vim`. See the `fcitx5-server` branch for an experimental implementation that supports `sudo vim`.
By default, it use python3 and D-Bus to toggle IME state.
If you set `g:fcitx5_remote` to the executable path of `fcitx5-remote` **BEFORE** loading the plugin, it will use `fcitx5-remote` instead of python and D-Bus. In this case, python3 support is optional.
Usually `fcitx5-remote` mode is way faster to start up since Python script need quite some time for the initial load if you don't use any other plugins that load Python. The Python version will be faster while switching.
Base requirements:
* fcitx 5
Requirements for Python mode (`g:fcitx5_remote` is not set):
* Vim with Python 3 compiled in
* The python-dbus package
Requirements for `fcitx5-remote` mode (`g:fcitx5_remote` is set):
* fcitx5-remote
If you are using `fcitx5-rime` (which has its own state), `let g:fcitx5_rime = 1` in your `.vimrc`.
The `FcitxCurrentIM()` function can be used to get current IM's name.
Links:
* [git repo](https://github.com/lilydjwg/fcitx.vim)
* [www.vim.org](https://www.vim.org/scripts/script.php?script_id=3764)
Warning:
1. If you use Vim in terminal, to avoid the Esc delay, please set `'ttimeoutlen'` to 100 or some other value. And check screen's `maptimeout` or tmux's `escape-time` option if you use it too.
----
在离开或重新进入插入模式或搜索模式时自动记录和恢复每个缓冲区各自的输入法状态,以便在普通模式下始终是英文输入模式,切换回插入模式时恢复离开前的输入法输入模式。
D-Bus 只在同一用户时有效,所以使用 `sudo vim` 时本代码就失效了。在 `fcitx5-server` 分支有一个实验性的版本支持 `sudo vim` 的用法。
本插件默认会使用 Python 3 并通过 D-Bus 来切换输入法状态。
但如果你在加载插件**之前**设置了 `g:fcitx5_remote` 为你已安装的 `fcitx5-remote` 可执行文件的路径,那么本插件会使用它来切换输入法状态;此模式下本插件并不需要 Python。
如果你没有其他使用 Python 的 Vim 插件,本插件的 Python 模式初始化可能会显著拖慢启动时间;而 `fcitx5-remote` 模式则没有这个问题。Python 模式会在切换时更快。
基本要求:
* fcitx 5
使用 Python 模式的要求(未设置 `g:fcitx5_remote`
* 带有 Python 3 支持的 Vim
* python-dbus 包
使用 `fcitx5-remote` 模式的要求(需设置 `g:fcitx5_remote`
* fcitx5-remote
如果使用 `fcitx5-rime`(它自己有输入状态),在 `.vimrc` 中设置 `let g:fcitx5_rime = 1`
`FcitxCurrentIM()` 函数可以用于获取当前输入法的名字。
链接:
* [git 仓库](https://github.com/lilydjwg/fcitx.vim)
* [www.vim.org](https://www.vim.org/scripts/script.php?script_id=3764)
注意事项:
1. 终端下请设置 Vim `'ttimeoutlen'` 选项为较小值如100否则退出插入模式时会有较严重的延迟。同样会造成延迟的还有 screen 的 `maptimeout` 选项以及 tmux 的 `escape-time` 选项。
2. 请在 fcitx5-configtool 中确认英语是第一个输入法中文是第二个输入法rime 用户可能需要设置 `g:fcitx5_rime = 1`

View file

@ -1,45 +1,103 @@
#!/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')
import functools
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)
import dbus
class FcitxComm:
def __init__(self):
self.bus = bus = dbus.SessionBus()
obj = bus.get_object('org.fcitx.Fcitx5', '/controller')
self.fcitx = dbus.Interface(obj, dbus_interface='org.fcitx.Fcitx.Controller1')
self._rime = None
def status(self):
return self.fcitx.State() == 2
def activate(self):
self.fcitx.Activate()
def deactivate(self):
self.fcitx.Deactivate()
def current(self):
im = self.fcitx.CurrentInputMethod()
if im == 'rime':
return self._get_rime().GetCurrentSchema()
else:
raise ValueError('未知命令')
finally:
sock.close()
return im
def current_and_rime(self):
im = self.fcitx.CurrentInputMethod()
if im == 'rime':
return 'rime:' + self._get_rime().GetCurrentSchema()
else:
return im
def _get_rime(self):
if self._rime is None:
obj = self.bus.get_object('org.fcitx.Fcitx5', '/rime')
self._rime = dbus.Interface(obj, dbus_interface='org.fcitx.Fcitx.Rime1')
return self._rime
class FcitxRimeComm:
def __init__(self):
bus = dbus.SessionBus()
obj = bus.get_object('org.fcitx.Fcitx5', '/rime')
self.fcitx = dbus.Interface(obj, dbus_interface='org.fcitx.Fcitx.Rime1')
def status(self):
return self.fcitx.IsAsciiMode()
def activate(self):
self.fcitx.SetAsciiMode(False)
def deactivate(self):
self.fcitx.SetAsciiMode(True)
def current(self):
return self.fcitx.GetCurrentSchema()
try:
if vim.eval('get(g:, "fcitx5_rime")') == '1':
FcitxComm = FcitxRimeComm
Fcitx = FcitxComm()
fcitx_loaded = True
except dbus.exceptions.DBusException as e:
if not vim.vars.get('silent_unsupported'):
vim.command('echohl WarningMsg | echom "fcitx.vim not loaded: %s" | echohl NONE' % e)
fcitx_loaded = False
def may_reconnect(func):
@functools.wraps(func)
def wrapped():
global Fcitx
for _ in range(2):
try:
return func()
except Exception as e:
vim.command('echohl WarningMsg | echom "fcitx.vim: %s: %s" | echohl NONE' % (type(e).__name__, e))
Fcitx = FcitxComm()
return wrapped
@may_reconnect
def fcitx2en():
if fcitxtalk() == 2:
if Fcitx.status():
vim.command('let b:inputtoggle = 1')
fcitxtalk('c')
Fcitx.deactivate()
@may_reconnect
def fcitx2zh():
if vim.eval('exists("b:inputtoggle")') == '1':
if vim.eval('b:inputtoggle') == '1':
fcitxtalk('o')
Fcitx.activate()
vim.command('let b:inputtoggle = 0')
else:
vim.command('let b:inputtoggle = 0')
@may_reconnect
def fcitx_current_im():
return Fcitx.current()
@may_reconnect
def fcitx_current_im_and_rime():
return Fcitx.current_and_rime()

View file

@ -1,56 +1,85 @@
scriptencoding utf-8
" fcitx.vim 记住插入模式小企鹅输入法的状态
" fcitx.vim remember fcitx's input state for each buffer
" Author: lilydjwg
" Maintainer: lilydjwg
" 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')
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
if &cp || exists("g:loaded_fcitx") || (!exists('$DISPLAY') && !exists('$WAYLAND_DISPLAY'))
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
function s:setup_cmd()
function Fcitx2en()
let inputstatus = trim(system(g:fcitx5_remote))
if inputstatus == '2'
let b:inputtoggle = 1
call system(g:fcitx5_remote . ' -c')
endif
endfunction
function Fcitx2zh()
try
if b:inputtoggle == 1
call system(g:fcitx5_remote . ' -o')
let b:inputtoggle = 0
endif
catch /inputtoggle/
let b:inputtoggle = 0
endtry
endfunction
let g:loaded_fcitx = 1
endfunction
" If g:fcitx5_remote is set (to the path to `fcitx5-remove`), use it to toggle IME state.
if exists("g:fcitx5_remote")
call s:setup_cmd()
" Otherwise, if python3 is available, use python and dbus to toggle IME state.
elseif has('python3')
try " abort on fail
exe 'py3file' expand('<sfile>:r') . '.py'
if py3eval('fcitx_loaded')
function Fcitx2en()
py3 fcitx2en()
endfunction
function Fcitx2zh()
py3 fcitx2zh()
endfunction
function FcitxCurrentIM()
return py3eval('fcitx_current_im()')
endfunction
function FcitxCurrentIMwithRime()
return py3eval('fcitx_current_im_and_rime()')
endfunction
let g:loaded_fcitx = 1
endif
catch
if executable('fcitx5-remote')
let g:fcitx5_remote = 'fcitx5-remote'
call s:setup_cmd()
endif
endtry
endif
" Register autocmd if successfully loaded.
if exists("g:loaded_fcitx")
if exists('##InsertLeavePre')
au InsertLeavePre * if reg_executing() == "" | call Fcitx2en() | endif
else
au InsertLeave * if reg_executing() == "" | call Fcitx2en() | endif
endif
au InsertEnter * if reg_executing() == "" | call Fcitx2zh() | endif
au CmdlineEnter [/\?] if reg_executing() == "" | call Fcitx2zh() | endif
au CmdlineLeave [/\?] if reg_executing() == "" | call Fcitx2en() | 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
" vim: sw=2 :

View file

@ -1,49 +0,0 @@
" 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