awesomewm/modules/ibus/util.lua

95 lines
2.4 KiB
Lua
Raw Normal View History

local util = {}
util.current_engine = ""
util.current_engine_index = nil
util.current_xkbmap = ""
-- Update current_engine and current_engine index,
-- call callback() when done.
util.get = function(callback)
-- Get current ibus engine
awful.spawn.easy_async(
"ibus engine",
function(stdout, stderr, exitreason, exitcode)
util.current_engine = string.gsub(stdout, "\n", "")
util.current_engine_index = nil
-- Find the current engine's index in conf.ibus_language_list.
-- If it is not there, util.current_engine_index will be nil.
for k, v in pairs(conf.ibus_language_list) do
if (v["ibus_engine"] == util.current_engine) then
util.current_engine_index = k
end
end
-- Get current xkbmap
awful.spawn.easy_async(
"setxkbmap -query",
function(stdout, stderr, exitreason, exitcode)
util.current_xkbmap = string.match(stdout, "layout:%s+(%w+)")
-- When everything is done, run callback if it is given.
if callback then
callback()
end
end
)
end
)
end
-- Set engine to language_index.
-- calls util.get(callback) when done.
util.set = function(language_index, callback)
-- engine is an index of the language list above
local engine = conf.ibus_language_list[language_index]["ibus_engine"]
-- Get required engine, if one is given
local requires_engine
for k, v in pairs(conf.ibus_language_list) do
if (v["ibus_engine"] == engine) then
requires_engine = v["requires_engine"]
end
end
-- If a required xkb engine is given, but it is not active, switch to it before switching to the target
if (requires_engine ~= util.current_engine) and (requires_engine ~= nil) then
awful.spawn.easy_async(
"ibus engine " .. requires_engine,
function(stdout, stderr, exitreason, exitcode)
awful.spawn.easy_async(
"ibus engine " .. engine,
function(stdout, stderr, exitreason, exitcode)
util.get(callback)
end
)
end
)
else
awful.spawn.easy_async(
"ibus engine " .. engine,
function(stdout, stderr, exitreason, exitcode)
util.get(callback)
end
)
end
end
-- Calls util.set(callback) with next language in list.
util.next = function(callback)
if (util.current_engine_index == nil) or (util.current_engine_index == #conf.ibus_language_list) then
util.current_engine_index = 1
else
util.current_engine_index = util.current_engine_index + 1
end
util.set(util.current_engine_index, callback)
end
return util