How to change iBus input method in python?

假如想象 提交于 2019-12-23 12:32:16

问题


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

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.

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.

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.

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.

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.


回答1:


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

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

call im#setInputMode("Hiragana")

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.

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.



来源:https://stackoverflow.com/questions/22031653/how-to-change-ibus-input-method-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!