From 5b33e919d98cb0b77c49f2860a3cc9a5c9a4de80 Mon Sep 17 00:00:00 2001 From: gou4shi1 Date: Mon, 15 May 2017 19:07:15 +0800 Subject: [PATCH 01/30] there is no _EXIT_ in socket in python2 --- plugin/fcitx.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin/fcitx.py b/plugin/fcitx.py index 57cf94a..59d8159 100644 --- a/plugin/fcitx.py +++ b/plugin/fcitx.py @@ -4,6 +4,7 @@ import os import vim import socket import struct +import contextlib fcitxsocketfile = vim.eval('s:fcitxsocketfile') @@ -48,7 +49,7 @@ class FcitxComm(object): if not self._connect(): return - with self.sock: + with contextlib.closing(self.sock): try: return func(*args, **kwargs) except (socket.error, socket.timeout, struct.error) as e: From b6fc70ffba65f356e0a981dbec3b51cf471ea2c6 Mon Sep 17 00:00:00 2001 From: meijieru Date: Thu, 31 May 2018 18:45:22 +0800 Subject: [PATCH 02/30] fcitx5 vimscript --- plugin/fcitx.vim | 8 ++++++++ so/fcitx.vim | 8 ++++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/plugin/fcitx.vim b/plugin/fcitx.vim index b0c7ab5..882a2f7 100644 --- a/plugin/fcitx.vim +++ b/plugin/fcitx.vim @@ -10,6 +10,14 @@ if &cp || exists("g:loaded_fcitx") || ( \ && !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") diff --git a/so/fcitx.vim b/so/fcitx.vim index 953010f..45a85a8 100644 --- a/so/fcitx.vim +++ b/so/fcitx.vim @@ -11,7 +11,7 @@ endif if !(exists('$DISPLAY') || has('gui_macvim')) || exists('$SSH_TTY') finish endif -if &cp || exists("g:loaded_fcitx") || !executable("fcitx-remote") +if &cp || exists("g:loaded_fcitx") || !executable(g:fcitx_remote) finish endif let s:keepcpo = &cpo @@ -20,16 +20,16 @@ set cpo&vim " --------------------------------------------------------------------- " Functions: function Fcitx2en() - let inputstatus = system("fcitx-remote") + let inputstatus = system(g:fcitx_remote) if inputstatus == 2 let b:inputtoggle = 1 - call system("fcitx-remote -c") + call system(g:fcitx_remote . ' -c') endif endfunction function Fcitx2zh() try if b:inputtoggle == 1 - call system("fcitx-remote -o") + call system(g:fcitx_remote . ' -o') let b:inputtoggle = 0 endif catch /inputtoggle/ From bab3fd3afe82c052cae396e53fe026b71f69e186 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Wed, 29 Aug 2018 16:03:30 +0800 Subject: [PATCH 03/30] GitHub renders plain text files too small, switching to markdown closes #7. --- README => README.md | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename README => README.md (100%) diff --git a/README b/README.md similarity index 100% rename from README rename to README.md From cd5b278dc4972f95365e8a4badaad499446c2fd1 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Wed, 29 Aug 2018 16:09:41 +0800 Subject: [PATCH 04/30] README: better formatted --- README.md | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 94ab1ce..5f8f551 100644 --- a/README.md +++ b/README.md @@ -2,30 +2,32 @@ Keep and restore fcitx state for each buffer separately when leaving/re-entering Requires: fcitx 3.6 or later, 4.0 or later will be better. -Settings: environment variable $FCITX_SOCKET specifies a socket to connect instead of figuring out itself. This can be an abstract socket address starting with "@" from version 1.2.4 on. +Settings: environment variable `$FCITX_SOCKET` specifies a socket to connect instead of figuring out itself. This can be an abstract socket address starting with `@` from version 1.2.4 on. -Git repo: https://github.com/lilydjwg/fcitx.vim -www.vim.org: http://www.vim.org/scripts/script.php?script_id=3764 +* [git repo](https://github.com/lilydjwg/fcitx.vim) +* [www.vim.org](https://www.vim.org/scripts/script.php?script_id=3764) Warning: -1, It will be faster and better with Python (3 or 2) enabled Vim. But some old version Vim enabled both Python 2 & 3 may have some issues. -2, 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. -For Mac OS X users, you can use a "fcitx-remote" here https://github.com/CodeFalling/fcitx-remote-for-osx , together with the VimL version (the so/fcitx.vim file). +1. It will be faster and better with Python (3 or 2) enabled Vim. But some old version Vim enabled both Python 2 & 3 may have some issues. +2. 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. + +For Mac OS X users, you can use a "fcitx-remote" [here](https://github.com/CodeFalling/fcitx-remote-for-osx), together with the VimL version (the `so/fcitx.vim` file). 在离开或重新进入插入模式时自动记录和恢复每个缓冲区各自的输入法状态,以便在普通模式下始终是英文输入模式,切换回插入模式时恢复离开前的输入法输入模式。 要求: fcitx 版本 3.6 以上,建议 fcitx 4.0 以上。 -配置:环境变量 $FCITX_SOCKET 指定要连接的套接字路径,而非默认的。自版本 1.2.4 起,此变量若以 "@" 字符开头,则被认为是抽象套接字地址。 +配置:环境变量 `$FCITX_SOCKET` 指定要连接的套接字路径,而非默认的。自版本 1.2.4 起,此变量若以 `@` 字符开头,则被认为是抽象套接字地址。 -Git 仓库: https://github.com/lilydjwg/fcitx.vim -www.vim.org: http://www.vim.org/scripts/script.php?script_id=3764 +* [git 仓库](https://github.com/lilydjwg/fcitx.vim) +* [www.vim.org](https://www.vim.org/scripts/script.php?script_id=3764) 注意事项: + 1. Vim 如有 Python 3或2 支持可以获得更快更好的效果。但对于较旧的 Vim 版本,如果同时编译了 Python 2 & 3 支持,因为此 Vim 不能同时运行两个版本的 Python,而本脚本首先检查 Python 3,所以会导致出错或者 Python 2 不可用。 -2. 终端下请设置 Vim 'ttimeoutlen' 选项为较小值(如100),否则退出插入模式时会有较严重的延迟。同样会造成延迟的还有 screen 的 maptimeout 选项以及 tmux 的 escape-time 选项。 +2. 终端下请设置 Vim `'ttimeoutlen'` 选项为较小值(如100),否则退出插入模式时会有较严重的延迟。同样会造成延迟的还有 screen 的 `maptimeout` 选项以及 tmux 的 `escape-time` 选项。 -如果你想跨主机使用 fcitx.vim,请参考此文: https://blog.lilydjwg.me/2012/7/27/using-fcitx-remote-interface-remotely-via-socat.34729.html +如果你想跨主机使用 fcitx.vim,请参考此文:[fcitx-remote 接口通过 socat 跨主机使用](https://blog.lilydjwg.me/2012/7/27/using-fcitx-remote-interface-remotely-via-socat.34729.html)。 -Mac OS X 用户可以使用此项目 https://github.com/CodeFalling/fcitx-remote-for-osx 提供的 fcitx-remote 命令,配合本软件的 VimL 版(so/fcitx.vim 文件)来使用。 +Mac OS X 用户可以使用[此项目](https://github.com/CodeFalling/fcitx-remote-for-osx)提供的 fcitx-remote 命令,配合本软件的 VimL 版(`so/fcitx.vim` 文件)来使用。 From 6267a6c073a59e474c4f91abcbd03cc9f518d5a1 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Wed, 22 May 2019 13:02:48 +0800 Subject: [PATCH 05/30] bump version --- plugin/fcitx.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/fcitx.vim b/plugin/fcitx.vim index 882a2f7..673cc8f 100644 --- a/plugin/fcitx.vim +++ b/plugin/fcitx.vim @@ -1,8 +1,8 @@ scriptencoding utf-8 " fcitx.vim remember fcitx's input state for each buffer " Author: lilydjwg -" Version: 1.2.5 -" URL: http://www.vim.org/scripts/script.php?script_id=3764 +" Version: 1.2.6 +" URL: https://www.vim.org/scripts/script.php?script_id=3764 " --------------------------------------------------------------------- " Load Once: if &cp || exists("g:loaded_fcitx") || ( From 99a27b43238149e7ac09441c588de7aedf722ec5 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Tue, 12 May 2020 20:41:58 +0800 Subject: [PATCH 06/30] remove modeline --- plugin/fcitx.vim | 1 - 1 file changed, 1 deletion(-) diff --git a/plugin/fcitx.vim b/plugin/fcitx.vim index 673cc8f..258e2f2 100644 --- a/plugin/fcitx.vim +++ b/plugin/fcitx.vim @@ -65,4 +65,3 @@ unlet python3 unlet pyfile let &cpo=s:keepcpo unlet s:keepcpo -" vim:fdm=expr:fde=getline(v\:lnum-1)=~'\\v"\\s*-{20,}'?'>1'\:1 From 0dd5692687433ef07d2b7fb56257fa4e890a430c Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Sun, 20 Dec 2020 19:52:05 +0800 Subject: [PATCH 07/30] initial fcitx5 version --- README.md | 24 ++++++++---------- plugin/fcitx.py | 65 ++++++++---------------------------------------- plugin/fcitx.vim | 58 +++++------------------------------------- so/fcitx.vim | 47 ---------------------------------- 4 files changed, 26 insertions(+), 168 deletions(-) delete mode 100644 so/fcitx.vim diff --git a/README.md b/README.md index 5f8f551..9fd6897 100644 --- a/README.md +++ b/README.md @@ -1,33 +1,29 @@ Keep and restore fcitx state for each buffer separately when leaving/re-entering insert mode. Like always typing English in normal mode, but Chinese in insert mode. -Requires: fcitx 3.6 or later, 4.0 or later will be better. +Requires: -Settings: environment variable `$FCITX_SOCKET` specifies a socket to connect instead of figuring out itself. This can be an abstract socket address starting with `@` from version 1.2.4 on. +* fcitx 5 +* Vim with Python 3 compiled in +* The python-dbus package * [git repo](https://github.com/lilydjwg/fcitx.vim) * [www.vim.org](https://www.vim.org/scripts/script.php?script_id=3764) Warning: -1. It will be faster and better with Python (3 or 2) enabled Vim. But some old version Vim enabled both Python 2 & 3 may have some issues. -2. 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. - -For Mac OS X users, you can use a "fcitx-remote" [here](https://github.com/CodeFalling/fcitx-remote-for-osx), together with the VimL version (the `so/fcitx.vim` file). +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. 在离开或重新进入插入模式时自动记录和恢复每个缓冲区各自的输入法状态,以便在普通模式下始终是英文输入模式,切换回插入模式时恢复离开前的输入法输入模式。 -要求: fcitx 版本 3.6 以上,建议 fcitx 4.0 以上。 +要求: -配置:环境变量 `$FCITX_SOCKET` 指定要连接的套接字路径,而非默认的。自版本 1.2.4 起,此变量若以 `@` 字符开头,则被认为是抽象套接字地址。 +* fcitx 5 +* 带有 Python 3 支持的 Vim +* python-dbus 包 * [git 仓库](https://github.com/lilydjwg/fcitx.vim) * [www.vim.org](https://www.vim.org/scripts/script.php?script_id=3764) 注意事项: -1. Vim 如有 Python 3或2 支持可以获得更快更好的效果。但对于较旧的 Vim 版本,如果同时编译了 Python 2 & 3 支持,因为此 Vim 不能同时运行两个版本的 Python,而本脚本首先检查 Python 3,所以会导致出错或者 Python 2 不可用。 -2. 终端下请设置 Vim `'ttimeoutlen'` 选项为较小值(如100),否则退出插入模式时会有较严重的延迟。同样会造成延迟的还有 screen 的 `maptimeout` 选项以及 tmux 的 `escape-time` 选项。 - -如果你想跨主机使用 fcitx.vim,请参考此文:[fcitx-remote 接口通过 socat 跨主机使用](https://blog.lilydjwg.me/2012/7/27/using-fcitx-remote-interface-remotely-via-socat.34729.html)。 - -Mac OS X 用户可以使用[此项目](https://github.com/CodeFalling/fcitx-remote-for-osx)提供的 fcitx-remote 命令,配合本软件的 VimL 版(`so/fcitx.vim` 文件)来使用。 +1. 终端下请设置 Vim `'ttimeoutlen'` 选项为较小值(如100),否则退出插入模式时会有较严重的延迟。同样会造成延迟的还有 screen 的 `maptimeout` 选项以及 tmux 的 `escape-time` 选项。 diff --git a/plugin/fcitx.py b/plugin/fcitx.py index 59d8159..3e02377 100644 --- a/plugin/fcitx.py +++ b/plugin/fcitx.py @@ -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(): diff --git a/plugin/fcitx.vim b/plugin/fcitx.vim index 258e2f2..3d43647 100644 --- a/plugin/fcitx.vim +++ b/plugin/fcitx.vim @@ -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(':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(':r') . '.py' +au InsertLeave * py3 fcitx2en() +au InsertEnter * py3 fcitx2zh() " --------------------------------------------------------------------- " Restoration And Modelines: -unlet python3 -unlet pyfile let &cpo=s:keepcpo unlet s:keepcpo diff --git a/so/fcitx.vim b/so/fcitx.vim deleted file mode 100644 index 45a85a8..0000000 --- a/so/fcitx.vim +++ /dev/null @@ -1,47 +0,0 @@ -" fcitx.vim 记住插入模式小企鹅输入法的状态 -" Author: lilydjwg -" Maintainer: lilydjwg -" Note: 另有使用 Python3 接口的新版本 -" --------------------------------------------------------------------- -" Load Once: -if (has("win32") || has("win95") || has("win64") || has("win16")) - " Windows 下不要载入 - finish -endif -if !(exists('$DISPLAY') || has('gui_macvim')) || exists('$SSH_TTY') - finish -endif -if &cp || exists("g:loaded_fcitx") || !executable(g:fcitx_remote) - finish -endif -let s:keepcpo = &cpo -let g:loaded_fcitx = 1 -set cpo&vim -" --------------------------------------------------------------------- -" Functions: -function Fcitx2en() - let inputstatus = system(g:fcitx_remote) - if inputstatus == 2 - let b:inputtoggle = 1 - call system(g:fcitx_remote . ' -c') - endif -endfunction -function Fcitx2zh() - try - if b:inputtoggle == 1 - call system(g: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 From 0d95b099142d7fa0268ca285895c5b95c35588f2 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Mon, 21 Dec 2020 19:46:15 +0800 Subject: [PATCH 08/30] README: fix lists being combined as one --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index 9fd6897..ca54cd5 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ Requires: * Vim with Python 3 compiled in * The python-dbus package +Links: + * [git repo](https://github.com/lilydjwg/fcitx.vim) * [www.vim.org](https://www.vim.org/scripts/script.php?script_id=3764) @@ -21,6 +23,8 @@ Warning: * 带有 Python 3 支持的 Vim * python-dbus 包 +链接: + * [git 仓库](https://github.com/lilydjwg/fcitx.vim) * [www.vim.org](https://www.vim.org/scripts/script.php?script_id=3764) From 056f8c580d56e85ccffbbc31e4d632c1898957a8 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Tue, 22 Dec 2020 01:02:43 +0800 Subject: [PATCH 09/30] reconnect dbus on error (maybe fcitx5 just was restarted) --- plugin/fcitx.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/plugin/fcitx.py b/plugin/fcitx.py index 3e02377..01d1689 100644 --- a/plugin/fcitx.py +++ b/plugin/fcitx.py @@ -1,4 +1,5 @@ import vim +import functools import dbus @@ -19,11 +20,25 @@ class FcitxComm(): Fcitx = FcitxComm() +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 Fcitx.status(): vim.command('let b:inputtoggle = 1') Fcitx.deactivate() +@may_reconnect def fcitx2zh(): if vim.eval('exists("b:inputtoggle")') == '1': if vim.eval('b:inputtoggle') == '1': From d6198095041e7967a409915583a2f200bfe49010 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Wed, 23 Dec 2020 15:40:21 +0800 Subject: [PATCH 10/30] better error handling --- plugin/fcitx.py | 5 ++++- plugin/fcitx.vim | 8 +++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/plugin/fcitx.py b/plugin/fcitx.py index 01d1689..3dc9ef5 100644 --- a/plugin/fcitx.py +++ b/plugin/fcitx.py @@ -18,7 +18,10 @@ class FcitxComm(): def deactivate(self): self.fcitx.Deactivate() -Fcitx = FcitxComm() +try: + Fcitx = FcitxComm() +except dbus.exceptions.DBusException as e: + vim.command('echohl WarningMsg | echom "fcitx.vim not loaded: %s" | echohl NONE' % e) def may_reconnect(func): @functools.wraps(func) diff --git a/plugin/fcitx.vim b/plugin/fcitx.vim index 3d43647..6a414e9 100644 --- a/plugin/fcitx.vim +++ b/plugin/fcitx.vim @@ -12,9 +12,11 @@ let s:keepcpo = &cpo set cpo&vim let g:loaded_fcitx = 1 -exe 'py3file' expand(':r') . '.py' -au InsertLeave * py3 fcitx2en() -au InsertEnter * py3 fcitx2zh() +try " abort on fail + exe 'py3file' expand(':r') . '.py' + au InsertLeave * py3 fcitx2en() + au InsertEnter * py3 fcitx2zh() +endtry " --------------------------------------------------------------------- " Restoration And Modelines: let &cpo=s:keepcpo From 695ee2b958c65eab812e6b45fad7337073d12963 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Tue, 29 Dec 2020 19:46:06 +0800 Subject: [PATCH 11/30] fix error handling from python to vim --- plugin/fcitx.py | 2 ++ plugin/fcitx.vim | 6 ++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/plugin/fcitx.py b/plugin/fcitx.py index 3dc9ef5..9456653 100644 --- a/plugin/fcitx.py +++ b/plugin/fcitx.py @@ -20,8 +20,10 @@ class FcitxComm(): try: Fcitx = FcitxComm() + fcitx_loaded = True except dbus.exceptions.DBusException as e: vim.command('echohl WarningMsg | echom "fcitx.vim not loaded: %s" | echohl NONE' % e) + fcitx_loaded = False def may_reconnect(func): @functools.wraps(func) diff --git a/plugin/fcitx.vim b/plugin/fcitx.vim index 6a414e9..779ba13 100644 --- a/plugin/fcitx.vim +++ b/plugin/fcitx.vim @@ -14,8 +14,10 @@ let g:loaded_fcitx = 1 try " abort on fail exe 'py3file' expand(':r') . '.py' - au InsertLeave * py3 fcitx2en() - au InsertEnter * py3 fcitx2zh() + if py3eval('fcitx_loaded') + au InsertLeavePre * py3 fcitx2en() + au InsertEnter * py3 fcitx2zh() + endif endtry " --------------------------------------------------------------------- " Restoration And Modelines: From 1efd21b9aade7693ed9731e464597bebec28d5ec Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Fri, 5 Feb 2021 16:34:07 +0800 Subject: [PATCH 12/30] README: update for the fcitx5-server branch --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index ca54cd5..2792397 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,7 @@ Keep and restore fcitx state for each buffer separately when leaving/re-entering insert 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`. + Requires: * fcitx 5 @@ -17,6 +19,8 @@ Warning: 在离开或重新进入插入模式时自动记录和恢复每个缓冲区各自的输入法状态,以便在普通模式下始终是英文输入模式,切换回插入模式时恢复离开前的输入法输入模式。 +D-Bus 只在同一用户时有效,所以使用 `sudo vim` 时本代码就失效了。在 `fcitx5-server` 分支有一个实验性的版本支持 `sudo vim` 的用法。 + 要求: * fcitx 5 From c0381166106e7659a9cb9433e077c369cc370eb8 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Mon, 8 Feb 2021 14:29:37 +0800 Subject: [PATCH 13/30] support for earlier Vim that doesn't have InsertLeavePre --- plugin/fcitx.vim | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugin/fcitx.vim b/plugin/fcitx.vim index 779ba13..e63e5cb 100644 --- a/plugin/fcitx.vim +++ b/plugin/fcitx.vim @@ -15,7 +15,11 @@ let g:loaded_fcitx = 1 try " abort on fail exe 'py3file' expand(':r') . '.py' if py3eval('fcitx_loaded') - au InsertLeavePre * py3 fcitx2en() + if exists('##InsertLeavePre') + au InsertLeavePre * py3 fcitx2en() + else + au InsertLeave * py3 fcitx2en() + endif au InsertEnter * py3 fcitx2zh() endif endtry From bbca697f3cd98a4a6a1c3f9c90ef3ed70532f55f Mon Sep 17 00:00:00 2001 From: Yufan You Date: Sun, 18 Apr 2021 12:56:50 +0800 Subject: [PATCH 14/30] feat: support search mode --- README.md | 4 ++-- plugin/fcitx.vim | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2792397..b34437a 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -Keep and restore fcitx state for each buffer separately when leaving/re-entering insert mode. Like always typing English in normal mode, but Chinese in insert mode. +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`. @@ -17,7 +17,7 @@ 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` 的用法。 diff --git a/plugin/fcitx.vim b/plugin/fcitx.vim index e63e5cb..acd4407 100644 --- a/plugin/fcitx.vim +++ b/plugin/fcitx.vim @@ -21,6 +21,8 @@ try " abort on fail au InsertLeave * py3 fcitx2en() endif au InsertEnter * py3 fcitx2zh() + au CmdlineEnter / py3 fcitx2zh() + au CmdlineLeave / py3 fcitx2en() endif endtry " --------------------------------------------------------------------- From 2b86740c87b514efbdca020421f3b5d1b8bf3f41 Mon Sep 17 00:00:00 2001 From: lokiiart Date: Wed, 12 May 2021 17:30:57 +0800 Subject: [PATCH 15/30] =?UTF-8?q?=E4=B8=BArime=E7=94=A8=E6=88=B7=E8=8A=82?= =?UTF-8?q?=E7=9C=81=E4=B8=80=E4=BA=9B=E6=97=B6=E9=97=B4=E5=90=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index b34437a..3101743 100644 --- a/README.md +++ b/README.md @@ -35,3 +35,4 @@ D-Bus 只在同一用户时有效,所以使用 `sudo vim` 时本代码就失 注意事项: 1. 终端下请设置 Vim `'ttimeoutlen'` 选项为较小值(如100),否则退出插入模式时会有较严重的延迟。同样会造成延迟的还有 screen 的 `maptimeout` 选项以及 tmux 的 `escape-time` 选项。 +2. 请在fcitx5-configtool中确认英语是第一个输入法,中文是第二个输入法,rime用户请注意在fcitx5中一定要有两个输入法。 From 92ef27262d8f5d2887e755d0327b70c184b0068f Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Mon, 24 May 2021 12:42:38 +0800 Subject: [PATCH 16/30] make it possible to silent the warning on dbus errors fixes #22. --- plugin/fcitx.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plugin/fcitx.py b/plugin/fcitx.py index 9456653..901d439 100644 --- a/plugin/fcitx.py +++ b/plugin/fcitx.py @@ -22,7 +22,8 @@ try: Fcitx = FcitxComm() fcitx_loaded = True except dbus.exceptions.DBusException as e: - vim.command('echohl WarningMsg | echom "fcitx.vim not loaded: %s" | echohl NONE' % 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): From bc068d055659b83cfabb962dbf304a3b1536fc9c Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 25 Jun 2021 15:54:03 +0800 Subject: [PATCH 17/30] feat: support backward search mode --- plugin/fcitx.vim | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/fcitx.vim b/plugin/fcitx.vim index acd4407..d1eab03 100644 --- a/plugin/fcitx.vim +++ b/plugin/fcitx.vim @@ -21,8 +21,8 @@ try " abort on fail au InsertLeave * py3 fcitx2en() endif au InsertEnter * py3 fcitx2zh() - au CmdlineEnter / py3 fcitx2zh() - au CmdlineLeave / py3 fcitx2en() + au CmdlineEnter [/\?] py3 fcitx2zh() + au CmdlineLeave [/\?] py3 fcitx2en() endif endtry " --------------------------------------------------------------------- From 4042bbb29c50a592062563f5042951abec696702 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Sun, 15 Aug 2021 22:21:15 +0800 Subject: [PATCH 18/30] add LICENSE --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d0c2d03 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2011-2021 lilydjwg , 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. From 5c7b8e5833efb64360e3fcc60b2366fb7f726c35 Mon Sep 17 00:00:00 2001 From: oxalica Date: Fri, 20 Aug 2021 16:31:07 +0800 Subject: [PATCH 19/30] Also support fcitx5-remote to toggle IME state (#27) * Also support fcitx5-remote to toggle IME state * Fix types and wording --- README.md | 28 +++++++++++++++++++-- plugin/fcitx.vim | 65 ++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 78 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 3101743..1c2ae92 100644 --- a/README.md +++ b/README.md @@ -2,12 +2,24 @@ Keep and restore fcitx state for each buffer separately when leaving/re-entering 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`. -Requires: +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 since Python script need quite some time for the initial load if you don't use any other plugins that load Python. + +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 + Links: * [git repo](https://github.com/lilydjwg/fcitx.vim) @@ -21,12 +33,24 @@ Warning: D-Bus 只在同一用户时有效,所以使用 `sudo vim` 时本代码就失效了。在 `fcitx5-server` 分支有一个实验性的版本支持 `sudo vim` 的用法。 -要求: +本插件默认会使用 Python 3 并通过 D-Bus 来切换输入法状态。 +但如果你在加载插件**之前**设置了 `g:fcitx5_remote` 为你已安装的 `fcitx5-remote` 可执行文件的路径,那么本插件会使用它来切换输入法状态;此模式下本插件并不需要 Python 。 + +如果你没有其他使用 Python 的 Vim 插件,本插件的 Python 模式初始化可能会显著拖慢启动时间;而 `fcitx5-remote` 模式则没有这个问题。 + +基本要求: * fcitx 5 + +使用 Python 模式的要求(未设置 `g:fcitx5_remote`): + * 带有 Python 3 支持的 Vim * python-dbus 包 +使用 `fcitx5-remote` 模式的要求(需设置 `g:fcitx5_remote`): + +* fcitx5-remote + 链接: * [git 仓库](https://github.com/lilydjwg/fcitx.vim) diff --git a/plugin/fcitx.vim b/plugin/fcitx.vim index d1eab03..9b4d47c 100644 --- a/plugin/fcitx.vim +++ b/plugin/fcitx.vim @@ -5,27 +5,66 @@ scriptencoding utf-8 " URL: https://www.vim.org/scripts/script.php?script_id=3764 " --------------------------------------------------------------------- " Load Once: -if &cp || exists("g:loaded_fcitx") || !exists('$DISPLAY') || !has('python3') +if &cp || exists("g:loaded_fcitx") || !exists('$DISPLAY') finish endif let s:keepcpo = &cpo set cpo&vim -let g:loaded_fcitx = 1 -try " abort on fail - exe 'py3file' expand(':r') . '.py' - if py3eval('fcitx_loaded') - if exists('##InsertLeavePre') - au InsertLeavePre * py3 fcitx2en() - else - au InsertLeave * py3 fcitx2en() +" If g:fcitx5_remote is set (to the path to `fcitx5-remove`), use it to toggle IME state. +if exists("g:fcitx5_remote") + function Fcitx2en() + let inputstatus = trim(system(g:fcitx5_remote)) + if inputstatus == '2' + let b:inputtoggle = 1 + call system(g:fcitx5_remote . ' -c') endif - au InsertEnter * py3 fcitx2zh() - au CmdlineEnter [/\?] py3 fcitx2zh() - au CmdlineLeave [/\?] py3 fcitx2en() + 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 + +" Otherwise, if python3 is available, use python and dbus to toggle IME state. +elseif has('python3') + try " abort on fail + exe 'py3file' expand(':r') . '.py' + if py3eval('fcitx_loaded') + function Fcitx2en() + py3 fcitx2en() + endfunction + function Fcitx2zh() + py3 fcitx2zh() + endfunction + + let g:loaded_fcitx = 1 + endif + endtry +endif + +" Register autocmd if successfully loaded. +if exists("g:loaded_fcitx") + if exists('##InsertLeavePre') + au InsertLeavePre * call Fcitx2en() + else + au InsertLeave * call Fcitx2en() endif -endtry + au InsertEnter * call Fcitx2zh() + au CmdlineEnter [/\?] call Fcitx2zh() + au CmdlineLeave [/\?] call Fcitx2en() +endif + " --------------------------------------------------------------------- " Restoration And Modelines: let &cpo=s:keepcpo unlet s:keepcpo + +" vim: sw=2 : From 3554b279a09f0edf31129ad162613e5954097fd0 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Fri, 20 Aug 2021 16:32:24 +0800 Subject: [PATCH 20/30] update README to make two modes' benefit clear --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1c2ae92..7f19a25 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ D-Bus only works with the same user so this won't work with `sudo vim`. See the 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 since Python script need quite some time for the initial load if you don't use any other plugins that load Python. +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: @@ -36,7 +36,7 @@ D-Bus 只在同一用户时有效,所以使用 `sudo vim` 时本代码就失 本插件默认会使用 Python 3 并通过 D-Bus 来切换输入法状态。 但如果你在加载插件**之前**设置了 `g:fcitx5_remote` 为你已安装的 `fcitx5-remote` 可执行文件的路径,那么本插件会使用它来切换输入法状态;此模式下本插件并不需要 Python 。 -如果你没有其他使用 Python 的 Vim 插件,本插件的 Python 模式初始化可能会显著拖慢启动时间;而 `fcitx5-remote` 模式则没有这个问题。 +如果你没有其他使用 Python 的 Vim 插件,本插件的 Python 模式初始化可能会显著拖慢启动时间;而 `fcitx5-remote` 模式则没有这个问题。Python 模式会在切换时更快。 基本要求: From 84a183b46dabf639cf40c7684fe4b11ceb6a3437 Mon Sep 17 00:00:00 2001 From: T2hhbmEK Date: Tue, 26 Oct 2021 17:55:02 +0800 Subject: [PATCH 21/30] add WAYLAND_DISPLAY --- plugin/fcitx.vim | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/fcitx.vim b/plugin/fcitx.vim index 9b4d47c..992217d 100644 --- a/plugin/fcitx.vim +++ b/plugin/fcitx.vim @@ -5,7 +5,7 @@ scriptencoding utf-8 " URL: https://www.vim.org/scripts/script.php?script_id=3764 " --------------------------------------------------------------------- " Load Once: -if &cp || exists("g:loaded_fcitx") || !exists('$DISPLAY') +if &cp || exists("g:loaded_fcitx") || (!exists('$DISPLAY') && !exists('$WAYLAND_DISPLAY')) finish endif let s:keepcpo = &cpo From e96238730791df1eccf9541b7cb7693ef4274896 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Wed, 9 Nov 2022 12:32:49 +0800 Subject: [PATCH 22/30] add support for fcitx5-rime https://github.com/fcitx/fcitx5-rime/issues/30#issuecomment-957001110 --- plugin/fcitx.py | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/plugin/fcitx.py b/plugin/fcitx.py index 901d439..8ef82d4 100644 --- a/plugin/fcitx.py +++ b/plugin/fcitx.py @@ -18,8 +18,26 @@ class FcitxComm(): def deactivate(self): self.fcitx.Deactivate() +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) + try: - Fcitx = FcitxComm() + if vim.eval('get(g:, "fcitx5_rime")') == '1': + Fcitx = FcitxRimeComm() + else: + Fcitx = FcitxComm() fcitx_loaded = True except dbus.exceptions.DBusException as e: if not vim.vars.get('silent_unsupported'): From fe899f6aece54d962cd75686124ce4abc3e67ec2 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Wed, 9 Nov 2022 14:59:45 +0800 Subject: [PATCH 23/30] update README about fcitx5_rime --- README.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7f19a25..4d95843 100644 --- a/README.md +++ b/README.md @@ -20,6 +20,8 @@ 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`. + Links: * [git repo](https://github.com/lilydjwg/fcitx.vim) @@ -29,12 +31,14 @@ 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 。 +但如果你在加载插件**之前**设置了 `g:fcitx5_remote` 为你已安装的 `fcitx5-remote` 可执行文件的路径,那么本插件会使用它来切换输入法状态;此模式下本插件并不需要 Python。 如果你没有其他使用 Python 的 Vim 插件,本插件的 Python 模式初始化可能会显著拖慢启动时间;而 `fcitx5-remote` 模式则没有这个问题。Python 模式会在切换时更快。 @@ -51,6 +55,8 @@ D-Bus 只在同一用户时有效,所以使用 `sudo vim` 时本代码就失 * fcitx5-remote +如果使用 `fcitx5-rime`(它自己有输入状态),在 `.vimrc` 中设置 `let g:fcitx5_rime = 1`。 + 链接: * [git 仓库](https://github.com/lilydjwg/fcitx.vim) @@ -59,4 +65,4 @@ D-Bus 只在同一用户时有效,所以使用 `sudo vim` 时本代码就失 注意事项: 1. 终端下请设置 Vim `'ttimeoutlen'` 选项为较小值(如100),否则退出插入模式时会有较严重的延迟。同样会造成延迟的还有 screen 的 `maptimeout` 选项以及 tmux 的 `escape-time` 选项。 -2. 请在fcitx5-configtool中确认英语是第一个输入法,中文是第二个输入法,rime用户请注意在fcitx5中一定要有两个输入法。 +2. 请在 fcitx5-configtool 中确认英语是第一个输入法,中文是第二个输入法,rime 用户可能需要设置 `g:fcitx5_rime = 1`。 From 7f06ccfa8aa6ee0c1fbcf4658fa1ffc3c9fd3093 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Tue, 15 Nov 2022 16:18:44 +0800 Subject: [PATCH 24/30] fix reconnecting not using the RIME interface --- plugin/fcitx.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/plugin/fcitx.py b/plugin/fcitx.py index 8ef82d4..fd04a68 100644 --- a/plugin/fcitx.py +++ b/plugin/fcitx.py @@ -35,9 +35,8 @@ class FcitxRimeComm(): try: if vim.eval('get(g:, "fcitx5_rime")') == '1': - Fcitx = FcitxRimeComm() - else: - Fcitx = FcitxComm() + FcitxComm = FcitxRimeComm + Fcitx = FcitxComm() fcitx_loaded = True except dbus.exceptions.DBusException as e: if not vim.vars.get('silent_unsupported'): From d79d816e24db97443160563df5256f8431f6660c Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Tue, 15 Nov 2022 16:19:00 +0800 Subject: [PATCH 25/30] add a function for current IM name for showing info e.g. on the status line. closes #32. --- README.md | 4 ++++ plugin/fcitx.py | 10 ++++++++++ plugin/fcitx.vim | 3 +++ 3 files changed, 17 insertions(+) diff --git a/README.md b/README.md index 4d95843..6ce27b8 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,8 @@ Requirements for `fcitx5-remote` mode (`g:fcitx5_remote` is set): 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) @@ -57,6 +59,8 @@ D-Bus 只在同一用户时有效,所以使用 `sudo vim` 时本代码就失 如果使用 `fcitx5-rime`(它自己有输入状态),在 `.vimrc` 中设置 `let g:fcitx5_rime = 1`。 +`FcitxCurrentIM()` 函数可以用于获取当前输入法的名字。 + 链接: * [git 仓库](https://github.com/lilydjwg/fcitx.vim) diff --git a/plugin/fcitx.py b/plugin/fcitx.py index fd04a68..b6f3083 100644 --- a/plugin/fcitx.py +++ b/plugin/fcitx.py @@ -18,6 +18,9 @@ class FcitxComm(): def deactivate(self): self.fcitx.Deactivate() + def current(self): + return self.fcitx.CurrentInputMethod() + class FcitxRimeComm(): def __init__(self): bus = dbus.SessionBus() @@ -33,6 +36,9 @@ class FcitxRimeComm(): 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 @@ -69,3 +75,7 @@ def fcitx2zh(): vim.command('let b:inputtoggle = 0') else: vim.command('let b:inputtoggle = 0') + +@may_reconnect +def fcitx_current_im(): + return Fcitx.current() diff --git a/plugin/fcitx.vim b/plugin/fcitx.vim index 992217d..b326c85 100644 --- a/plugin/fcitx.vim +++ b/plugin/fcitx.vim @@ -44,6 +44,9 @@ elseif has('python3') function Fcitx2zh() py3 fcitx2zh() endfunction + function FcitxCurrentIM() + return py3eval('fcitx_current_im()') + endfunction let g:loaded_fcitx = 1 endif From eb62569b3bc72795cdea918f5dda71dc8fd8ef28 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Tue, 15 Nov 2022 20:47:21 +0800 Subject: [PATCH 26/30] if current im is rime, return rime's schema name --- plugin/fcitx.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/plugin/fcitx.py b/plugin/fcitx.py index b6f3083..937f691 100644 --- a/plugin/fcitx.py +++ b/plugin/fcitx.py @@ -3,11 +3,12 @@ import functools import dbus -class FcitxComm(): +class FcitxComm: def __init__(self): - bus = dbus.SessionBus() + 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 @@ -19,9 +20,17 @@ class FcitxComm(): self.fcitx.Deactivate() def current(self): - return self.fcitx.CurrentInputMethod() + im = self.fcitx.CurrentInputMethod() + if im == 'rime': + return self._get_rime().GetCurrentSchema() -class FcitxRimeComm(): + 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') From dc95d0301b61a7db1f1badfe743fb627ea6e06d1 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Tue, 15 Nov 2022 22:06:01 +0800 Subject: [PATCH 27/30] fix not returning IM name when it's not rime --- plugin/fcitx.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/plugin/fcitx.py b/plugin/fcitx.py index 937f691..ccee097 100644 --- a/plugin/fcitx.py +++ b/plugin/fcitx.py @@ -23,6 +23,8 @@ class FcitxComm: im = self.fcitx.CurrentInputMethod() if im == 'rime': return self._get_rime().GetCurrentSchema() + else: + return im def _get_rime(self): if self._rime is None: From 92247352c6f05c8deebea2f50d8e842bfd91c294 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Thu, 2 Feb 2023 15:54:47 +0800 Subject: [PATCH 28/30] fallback to cmd if Python's dbus module isn't available closes #36. --- plugin/fcitx.vim | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/plugin/fcitx.vim b/plugin/fcitx.vim index b326c85..c50c1fc 100644 --- a/plugin/fcitx.vim +++ b/plugin/fcitx.vim @@ -11,8 +11,7 @@ endif let s:keepcpo = &cpo set cpo&vim -" If g:fcitx5_remote is set (to the path to `fcitx5-remove`), use it to toggle IME state. -if exists("g:fcitx5_remote") +function s:setup_cmd() function Fcitx2en() let inputstatus = trim(system(g:fcitx5_remote)) if inputstatus == '2' @@ -32,6 +31,11 @@ if exists("g:fcitx5_remote") 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') @@ -50,6 +54,11 @@ elseif has('python3') let g:loaded_fcitx = 1 endif + catch + if executable('fcitx5-remote') + let g:fcitx5_remote = 'fcitx5-remote' + call s:setup_cmd() + endif endtry endif From 451d277bbd003d57da939fa129541dba5c63c154 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Thu, 3 Aug 2023 23:51:05 +0800 Subject: [PATCH 29/30] don't execute when executing a macro --- plugin/fcitx.vim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/plugin/fcitx.vim b/plugin/fcitx.vim index c50c1fc..1cb837e 100644 --- a/plugin/fcitx.vim +++ b/plugin/fcitx.vim @@ -65,13 +65,13 @@ endif " Register autocmd if successfully loaded. if exists("g:loaded_fcitx") if exists('##InsertLeavePre') - au InsertLeavePre * call Fcitx2en() + au InsertLeavePre * if reg_executing() == "" | call Fcitx2en() | endif else - au InsertLeave * call Fcitx2en() + au InsertLeave * if reg_executing() == "" | call Fcitx2en() | endif endif - au InsertEnter * call Fcitx2zh() - au CmdlineEnter [/\?] call Fcitx2zh() - au CmdlineLeave [/\?] call Fcitx2en() + 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 " --------------------------------------------------------------------- From 28d5bcc86d01a75911cf9c165d16840b7f0d4f98 Mon Sep 17 00:00:00 2001 From: lilydjwg Date: Sun, 21 Apr 2024 16:21:34 +0800 Subject: [PATCH 30/30] add another function FcitxCurrentIMwithRime to show RIME info too see https://github.com/lilydjwg/fcitx.vim/issues/33#issuecomment-2002346047 --- plugin/fcitx.py | 11 +++++++++++ plugin/fcitx.vim | 3 +++ 2 files changed, 14 insertions(+) diff --git a/plugin/fcitx.py b/plugin/fcitx.py index ccee097..3cbd642 100644 --- a/plugin/fcitx.py +++ b/plugin/fcitx.py @@ -26,6 +26,13 @@ class FcitxComm: else: 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') @@ -90,3 +97,7 @@ def fcitx2zh(): @may_reconnect def fcitx_current_im(): return Fcitx.current() + +@may_reconnect +def fcitx_current_im_and_rime(): + return Fcitx.current_and_rime() diff --git a/plugin/fcitx.vim b/plugin/fcitx.vim index 1cb837e..ee85606 100644 --- a/plugin/fcitx.vim +++ b/plugin/fcitx.vim @@ -51,6 +51,9 @@ elseif has('python3') function FcitxCurrentIM() return py3eval('fcitx_current_im()') endfunction + function FcitxCurrentIMwithRime() + return py3eval('fcitx_current_im_and_rime()') + endfunction let g:loaded_fcitx = 1 endif