如何在python中更改iBus输入法? [英] How to change iBus input method in python?

查看:20
本文介绍了如何在python中更改iBus输入法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Vim 插件来设置 iBus 引擎和输入法.到目前为止,我可以使用以下代码更改引擎:

I am writing a Vim plugin to set iBus engines and input methods. So far I can change the engines with the code below:

function! im#setEngine(name)
python << EOF
try: 
  import ibus,vim
  bus = ibus.Bus()
  ic = ibus.InputContext(bus, bus.current_input_contxt())
  name = vim.eval("a:name")
  engines = bus.get_engines_by_names([name])
  size = len(engines)
  if size <= 0:
    print "Could not find engine %s"%name
  else:
    engine = engines[0]
    ic.set_engine(engine)
except Exception, e:
  print "Failed to connect to iBus"
  print e
EOF
endfunction

function! im#listEngines()

  let l:engines = []

python << EOF
try:
  import ibus,dbus,vim
  bus = ibus.Bus()
  names = []
  for engine in bus.list_engines():
    names.append(str(engine.name))
  vim.command("let l:engines = %s"% names)
except Exception, e:
  print "Failed to connect to iBus"
  print e
EOF
  return l:engines
endfunction

现在我还尝试为引擎设置输入法,但我无法找到如何执行此操作.到目前为止,iBus 文档缺乏详细信息.

Now I am trying to also set the input method for the engines but I am unable to find how to do this. The iBus documentation is lacking on details so far.

有人可以提供有关如何以编程方式 (Python) 更改 iBus 输入法的指针或示例吗?还有一种获取每个引擎支持的输入法列表的方法会很棒.

Does anyone can provide pointers or examples on how to programatically (Python) change the iBus input method? Also a way to get a list of supported input methods for each engine would be great.

====

从这一点来看,我将尝试提供有关我正在尝试解决的问题的更多背景信息.如果您不感兴趣,请跳过.

From this point I will try to provide more context on the problem I am trying to solve. Skip if you are not interested.

我实现了这个插件 vim-im 在进入 Vim 正常模式时禁用输入法.这很重要,因为如果 iBus 设置为非 ascii 输入法,则 Vim 正常模式将无法使用.如果你用vim写日文、中文、韩文等...你可能会理解这个问题.

I implemented this plugin vim-im to disable input methods when entering Vim normal mode. This is important because Vim normal mode is unusable if iBus is set to a non ascii input method. If you use vim to write in Japanese, Chinese, Korean, etc... you may understand the issue.

问题是,从 iBus 1.5 开始,我的插件依赖的启用/禁用方法已被弃用.所以我的插件适用于 Ubuntu <= 13.04 但不适用于 Debian Jessie,并且可能也不适用于未来的 Ubuntu 版本.

The problem is that since iBus 1.5 the enable/disable methods my plugin depends on are deprecated. So my plugin works in Ubuntu <= 13.04 but not in Debian Jessie and possibly won't work on future Ubuntu versions either.

我认为具有类似功能的唯一方法是定义一个默认的 iBus 引擎和输入法,并在每次 Vim 进入正常模式时将 iBus 更改为那些.

The only way I see to have similar functionality is to define a default iBus engine and input method and change iBus to those every time Vim enters in normal mode.

推荐答案

阅读 ibus 库代码我找到了一个可以接受的解决方案:

Reading the ibus library code I found an acceptable solution:

function! im#setInputMode(mode)
python << EOF
try:
  import ibus,dbus,vim
  bus = ibus.Bus()
  conn = bus.get_dbusconn().get_object(ibus.common.IBUS_SERVICE_IBUS, bus.current_input_contxt())
  ic = dbus.Interface(conn, dbus_interface=ibus.common.IBUS_IFACE_INPUT_CONTEXT)
  mode = vim.eval("a:mode")
  ic.PropertyActivate("InputMode." + mode, ibus.PROP_STATE_CHECKED)
except Exception, e:
  print "Failed to connect to iBus"
  print e
EOF
endfunction

这个方法允许我通过传递一个名字来改变 iBus 的输入法:

This method allows me to change the input method of iBus by passing it a name like:

call im#setInputMode("Hiragana")

不幸的是,输入法名称取决于所使用的引擎.例如,对于 mozc,我需要将其设置为Direct",而对于 anthy,我必须使用WideLatin"才能在 vim 正常模式下获得正确的输入.

Unfortunately the input method name depends on the engine being used. For example for mozc I need to set it to "Direct" while for anthy I have to use "WideLatin" in order get correct input in vim Normal Mode.

如果有人知道一种查询 iBus 引擎以获取支持的 InputMode 列表的方法,那就太好了.此外,查询引擎以获取当前设置的 InputMethod 的方法也会有所帮助.

If someone knows a way to query the iBus engines to get a list of supported InputMode would be great. Also a way to query the engine for the current set InputMethod would help too.

这篇关于如何在python中更改iBus输入法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆