awesomewm/wrapper/ibus.lua

103 lines
2.8 KiB
Lua

local ibus = {}
ibus.current_engine = ""
ibus.current_engine_index = nil
ibus.ibus_indicator_text = wibox.widget.textbox("??")
ibus.ibus_indicator_text.valign = "center"
ibus.ibus_indicator_text.align = "center"
ibus.ibus_indicator_text.font = "Hack NF 14"
ibus.xkb_indicator_text = wibox.widget.textbox("??")
ibus.xkb_indicator_text.valign = "center"
ibus.xkb_indicator_text.align = "center"
ibus.xkb_indicator_text.font = "Hack NF 10"
ibus.get = function(callback)
awful.spawn.easy_async("ibus engine", function(stdout, stderr, exitreason, exitcode)
-- Get current ibus engine, remove newlines from output.
ibus.current_engine = string.gsub(stdout, "\n", "")
ibus.current_engine_index = nil
-- Find the current engine's index in conf.ibus_language_list.
-- If it is not there, ibus.current_engine_index will be nil.
for k, v in pairs(conf.ibus_language_list) do
if (v["ibus_engine"] == ibus.current_engine) then
ibus.current_engine_index = k
end
end
if callback then
callback()
end
end)
end
ibus.update_indicator = function()
-- Update indicators
awful.spawn.easy_async("setxkbmap -query" , function(stdout, stderr, exitreason, exitcode)
ibus.ibus_indicator_text.markup = conf.ibus_language_list[ibus.current_engine_index]["indicator_code"]
ibus.xkb_indicator_text.markup = string.match(stdout, "layout:%s+(%w+)")
end)
end
ibus.set = function(language_index, callback)
-- engine is an index of the language list above
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 ~= ibus.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)
ibus.update_indicator()
ibus.get(callback)
end)
end)
else
awful.spawn.easy_async("ibus engine " .. engine, function(stdout, stderr, exitreason, exitcode)
ibus.update_indicator()
ibus.get(callback)
end)
end
end
ibus.next = function(callback)
if (ibus.current_engine_index == nil) or (ibus.current_engine_index == #conf.ibus_language_list) then
ibus.current_engine_index = 1
else
ibus.current_engine_index = ibus.current_engine_index + 1
end
ibus.set(ibus.current_engine_index, callback)
end
-- Update the language indicator
ibus.update_timer = gears.timer {
timeout = 2,
call_now = false,
autostart = true,
single_shot = false,
callback = function()
ibus.get(
function()
ibus.update_indicator()
end
)
end
}
return ibus