Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Version 1.0: Initial upload

  • Loading branch information...
commit 748342ed76c8bdcaf86f685e2e10ae3a4ee6a269 0 parents
依云 lilydjwg authored committed

Showing 4 changed files with 168 additions and 0 deletions. Show diff stats Hide diff stats

  1. +18 0 README
  2. +45 0 plugin/fcitx.py
  3. +56 0 plugin/fcitx.vim
  4. +49 0 so/fcitx.vim
18 README
... ... @@ -0,0 +1,18 @@
  1 +This is a mirror of http://www.vim.org/scripts/script.php?script_id=3764
  2 +
  3 +(fcitx is a Chinese input method so no English description.)
  4 +
  5 +Fcitx 输入法自动切换
  6 +
  7 +在离开/重新进入插入模式时自动切换输入法状态,以便在普通模式下始终是英文输入模式,切换回插入模式时恢复离开时的输入法输入模式。状态为每个缓冲区单独保存,所以在中文文档和英文代码间交叉跳转编辑也得心应手!
  8 +
  9 +无需任何配置!
  10 +
  11 +要求:
  12 +fcitx 版本 3.6 以上,建议 fcitx 4.0 以上
  13 +
  14 +可选:
  15 +Python 3 或者 Python 支持以获得更快更好的效果。注意对于 vim 版本<7.3.288,如果同时编译了 Python 2 & 3 支持,因为此 vim 不能同时运行两个版本的 Python,而本脚本首先检查 Python3,所以会导致出错或者 Python 2 不可用。
  16 +
  17 +开发:
  18 +它是 https://github.com/lilydjwg/dotvim 的一部分
45 plugin/fcitx.py
... ... @@ -0,0 +1,45 @@
  1 +#!/usr/bin/env python3
  2 +# vim:fileencoding=utf-8
  3 +
  4 +import os
  5 +import vim
  6 +import socket
  7 +import struct
  8 +FCITX_STATUS = struct.pack('i', 0)
  9 +FCITX_OPEN = struct.pack('i', 1 | (1 << 16))
  10 +FCITX_CLOSE = struct.pack('i', 1)
  11 +INT_SIZE = struct.calcsize('i')
  12 +fcitxsocketfile = vim.eval('s:fcitxsocketfile')
  13 +
  14 +def fcitxtalk(command=None):
  15 + sock = socket.socket(socket.AF_UNIX)
  16 + try:
  17 + sock.connect(fcitxsocketfile)
  18 + except socket.error:
  19 + vim.command('echohl WarningMsg | echo "fcitx.vim: socket 连接出错" | echohl NONE')
  20 + return
  21 + try:
  22 + if not command:
  23 + sock.send(FCITX_STATUS)
  24 + return struct.unpack('i', sock.recv(INT_SIZE))[0]
  25 + elif command == 'c':
  26 + sock.send(FCITX_CLOSE)
  27 + elif command == 'o':
  28 + sock.send(FCITX_OPEN)
  29 + else:
  30 + raise ValueError('未知命令')
  31 + finally:
  32 + sock.close()
  33 +
  34 +def fcitx2en():
  35 + if fcitxtalk() == 2:
  36 + vim.command('let b:inputtoggle = 1')
  37 + fcitxtalk('c')
  38 +
  39 +def fcitx2zh():
  40 + if vim.eval('exists("b:inputtoggle")') == '1':
  41 + if vim.eval('b:inputtoggle') == '1':
  42 + fcitxtalk('o')
  43 + vim.command('let b:inputtoggle = 0')
  44 + else:
  45 + vim.command('let b:inputtoggle = 0')
56 plugin/fcitx.vim
... ... @@ -0,0 +1,56 @@
  1 +scriptencoding utf-8
  2 +" fcitx.vim 记住插入模式小企鹅输入法的状态
  3 +" Author: lilydjwg
  4 +" Maintainer: lilydjwg
  5 +" ---------------------------------------------------------------------
  6 +" Load Once:
  7 +if &cp || exists("g:loaded_fcitx") || !exists('$DISPLAY') || exists('$SSH_TTY')
  8 + finish
  9 +endif
  10 +if has("python3")
  11 + let python3 = 1
  12 +elseif has("python")
  13 + let python3 = 0
  14 +else
  15 + echohl WarningMsg
  16 + echomsg "fcitx.vim: 没有 Python 支持,尝试使用旧版本。"
  17 + echohl None
  18 + runtime so/fcitx.vim
  19 + finish
  20 +endif
  21 +let s:keepcpo = &cpo
  22 +set cpo&vim
  23 +" this is quicker than expand()
  24 +let s:fcitxsocketfile = '/tmp/fcitx-socket-' . $DISPLAY
  25 +if !filewritable(s:fcitxsocketfile) "try again
  26 + if strridx(s:fcitxsocketfile, '.') > 0
  27 + let s:fcitxsocketfile = strpart(s:fcitxsocketfile, 0,
  28 + \ strridx(s:fcitxsocketfile, '.'))
  29 + else
  30 + let s:fcitxsocketfile = s:fcitxsocketfile . '.0'
  31 + if !filewritable(s:fcitxsocketfile)
  32 + echohl WarningMsg
  33 + echomsg "没有找到 fcitx 的 socket 文件,fcitx.vim 没有载入。"
  34 + echohl None
  35 + finish
  36 + endif
  37 + endif
  38 +endif
  39 +let g:loaded_fcitx = 1
  40 +let pyfile = expand('<sfile>:r') . '.py'
  41 +if python3
  42 + exe 'py3file' pyfile
  43 + au InsertLeave * py3 fcitx2en()
  44 + au InsertEnter * py3 fcitx2zh()
  45 +else
  46 + exe 'pyfile' pyfile
  47 + au InsertLeave * py fcitx2en()
  48 + au InsertEnter * py fcitx2zh()
  49 +endif
  50 +" ---------------------------------------------------------------------
  51 +" Restoration And Modelines:
  52 +unlet python3
  53 +unlet pyfile
  54 +let &cpo=s:keepcpo
  55 +unlet s:keepcpo
  56 +" vim:fdm=expr:fde=getline(v\:lnum-1)=~'\\v"\\s*-{20,}'?'>1'\:1
49 so/fcitx.vim
... ... @@ -0,0 +1,49 @@
  1 +" fcitx.vim 记住插入模式小企鹅输入法的状态
  2 +" Author: lilydjwg
  3 +" Maintainer: lilydjwg
  4 +" Last Change: 2010-11-18
  5 +" Note: 另有使用 Python3 接口的新版本
  6 +" ---------------------------------------------------------------------
  7 +" Load Once:
  8 +if (has("win32") || has("win95") || has("win64") || has("win16"))
  9 + " Windows 下不要载入
  10 + finish
  11 +endif
  12 +if !exists('$DISPLAY')
  13 + " 没有 X,不要载入
  14 + finish
  15 +endif
  16 +if &cp || exists("g:loaded_fcitx") || !executable("fcitx-remote")
  17 + finish
  18 +endif
  19 +let s:keepcpo = &cpo
  20 +let g:loaded_fcitx = 1
  21 +set cpo&vim
  22 +" ---------------------------------------------------------------------
  23 +" Functions:
  24 +function Fcitx2en()
  25 + let inputstatus = system("fcitx-remote")
  26 + if inputstatus == 2
  27 + let b:inputtoggle = 1
  28 + call system("fcitx-remote -c")
  29 + endif
  30 +endfunction
  31 +function Fcitx2zh()
  32 + try
  33 + if b:inputtoggle == 1
  34 + call system("fcitx-remote -o")
  35 + let b:inputtoggle = 0
  36 + endif
  37 + catch /inputtoggle/
  38 + let b:inputtoggle = 0
  39 + endtry
  40 +endfunction
  41 +" ---------------------------------------------------------------------
  42 +" Autocmds:
  43 +au InsertLeave * call Fcitx2en()
  44 +au InsertEnter * call Fcitx2zh()
  45 +" ---------------------------------------------------------------------
  46 +" Restoration And Modelines:
  47 +let &cpo=s:keepcpo
  48 +unlet s:keepcpo
  49 +" vim:fdm=expr:fde=getline(v\:lnum-1)=~'\\v"\\s*-{20,}'?'>1'\:1

0 comments on commit 748342e

Please sign in to comment.
Something went wrong with that request. Please try again.