From 7f7261ea8b9c2f52cd05511a6edfc349a09fd607 Mon Sep 17 00:00:00 2001 From: amorphobia Date: Wed, 3 Mar 2021 12:39:08 +0800 Subject: [PATCH 1/5] Add SwitcherReg --- src/types.cc | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/types.cc b/src/types.cc index 8ca6005d..8ff44ad6 100644 --- a/src/types.cc +++ b/src/types.cc @@ -9,6 +9,7 @@ #include #include #include +#include #include "lua_gears.h" #include "lib/lua_templates.h" @@ -816,7 +817,7 @@ namespace LogReg { } } -namespace RimeApiReg{ +namespace RimeApiReg { string get_rime_version() { RimeApi* rime = rime_get_api(); return string(rime->get_version()); @@ -852,6 +853,38 @@ namespace RimeApiReg{ } } +namespace SwitcherReg { + typedef Switcher T; + + an make(const Engine *engine) { + return New(engine); + } + + static const luaL_Reg funcs[] = { + { "Switcher", WRAP(make) }, + { NULL, NULL }, + }; + + static const luaL_Reg methods[] = { + { "select_next_schema", WRAPMEM(T::SelectNextSchema) }, + { "refresh_menu", WRAPMEM(T::RefreshMenu) }, + { "activate", WRAPMEM(T::Activate) }, + { "deactivate", WRAPMEM(T::Deactivate) }, + { NULL, NULL }, + }; + + static const luaL_Reg vars_get[] = { + { "is_auto_save", WRAPMEM(T::IsAutoSave) }, + { "attached_engine", WRAPMEM(T::attached_engine) }, + { "user_config", WRAPMEM(T::user_config) }, + { "active", WRAPMEM(T::active) }, + { NULL, NULL }, + }; + + static const luaL_Reg vars_set[] = { + { NULL, NULL }, + }; +} //--- Lua #define EXPORT(ns, L) \ From 5f7e55bca9c782a501cbff738efc947ae62a41b4 Mon Sep 17 00:00:00 2001 From: amorphobia Date: Wed, 3 Mar 2021 13:54:07 +0800 Subject: [PATCH 2/5] Include ticket.h --- src/types.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/types.cc b/src/types.cc index 8ff44ad6..6ec1be0d 100644 --- a/src/types.cc +++ b/src/types.cc @@ -10,6 +10,7 @@ #include #include #include +#include #include "lua_gears.h" #include "lib/lua_templates.h" From baed948f62b63eb9a1cfb7d7d50e997333400f72 Mon Sep 17 00:00:00 2001 From: amorphobia Date: Wed, 3 Mar 2021 14:21:10 +0800 Subject: [PATCH 3/5] Remove const for engine --- src/types.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.cc b/src/types.cc index 6ec1be0d..bc358ff5 100644 --- a/src/types.cc +++ b/src/types.cc @@ -857,7 +857,7 @@ namespace RimeApiReg { namespace SwitcherReg { typedef Switcher T; - an make(const Engine *engine) { + an make(Engine *engine) { return New(engine); } From 2fb7d323f02971f8d4a03d16cfea9b7f0e287f26 Mon Sep 17 00:00:00 2001 From: amorphobia Date: Wed, 3 Mar 2021 15:04:29 +0800 Subject: [PATCH 4/5] Export SwitcherReg --- src/types.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/types.cc b/src/types.cc index bc358ff5..874dc508 100644 --- a/src/types.cc +++ b/src/types.cc @@ -10,7 +10,6 @@ #include #include #include -#include #include "lua_gears.h" #include "lib/lua_templates.h" @@ -868,6 +867,7 @@ namespace SwitcherReg { static const luaL_Reg methods[] = { { "select_next_schema", WRAPMEM(T::SelectNextSchema) }, + { "is_auto_save", WRAPMEM(T::IsAutoSave) }, { "refresh_menu", WRAPMEM(T::RefreshMenu) }, { "activate", WRAPMEM(T::Activate) }, { "deactivate", WRAPMEM(T::Deactivate) }, @@ -875,7 +875,6 @@ namespace SwitcherReg { }; static const luaL_Reg vars_get[] = { - { "is_auto_save", WRAPMEM(T::IsAutoSave) }, { "attached_engine", WRAPMEM(T::attached_engine) }, { "user_config", WRAPMEM(T::user_config) }, { "active", WRAPMEM(T::active) }, @@ -932,6 +931,7 @@ void types_init(lua_State *L) { EXPORT(PropertyUpdateNotifierReg, L); EXPORT(KeyEventNotifierReg, L); EXPORT(ConnectionReg, L); + EXPORT(SwitcherReg, L); LogReg::init(L); RimeApiReg::init(L); } From b81f65feaa8fdd785361e8a4373e7aa9325eb713 Mon Sep 17 00:00:00 2001 From: amorphobia Date: Fri, 5 Mar 2021 11:12:32 +0800 Subject: [PATCH 5/5] Add a sample script for Switcher --- sample/lua/switch.lua | 84 +++++++++++++++++++++++++++++++++++++++++++ sample/rime.lua | 6 ++++ 2 files changed, 90 insertions(+) create mode 100644 sample/lua/switch.lua diff --git a/sample/lua/switch.lua b/sample/lua/switch.lua new file mode 100644 index 00000000..d9d9b84d --- /dev/null +++ b/sample/lua/switch.lua @@ -0,0 +1,84 @@ +--[[ +switch_processor: 通过选择自定义的候选项来切换开关(以简繁切换和下一方案为例) + +`Switcher` 适用于: +1. 不方便或不倾向用 key_binder 处理的情况 +2. 自定义开关的读取(本例未体现) + +须将 lua_processor@switch_processor 放在 engine/processors 里,并位于默认 selector 之前 + +为更好的使用本例,可以添加置顶的自定义词组,如 +〔简〕 simp +〔繁〕 simp +〔下一方案〕 next +--]] + +-- 帮助函数,返回被选中的候选的索引 +local function select_index(key, env) + local ch = key.keycode + local index = -1 + local select_keys = env.engine.schema.select_keys + if select_keys ~= nil and select_keys ~= "" and not key.ctrl() and ch >= 0x20 and ch < 0x7f then + local pos = string.find(select_keys, string.char(ch)) + if pos ~= nil then index = pos end + elseif ch >= 0x30 and ch <= 0x39 then + index = (ch - 0x30 + 9) % 10 + elseif ch >= 0xffb0 and ch < 0xffb9 then + index = (ch - 0xffb0 + 9) % 10 + elseif ch == 0x20 then + index = 0 + end + return index +end + +-- 切换开关函数 +local function apply_switch(env, keyword, target_state) + local ctx = env.engine.context + local swt = env.switcher + local conf = swt.user_config + ctx:set_option(keyword, target_state) + -- 如果设置了自动保存,则需相应的配置 + if swt:is_auto_save(keyword) and conf ~= nil then + conf:set_bool("var/option/" .. keyword, target_state) + end +end + +local kRejected = 0 +local kAccepted = 1 +local kNoop = 2 + +local function selector(key, env) + if env.switcher == nil then return kNoop end + if key:release() or key:alt() then return kNoop end + local idx = select_index(key,env) + if idx < 0 then return kNoop end + local ctx = env.engine.context + if ctx.input == "simp" then -- 当输入为 "simp" 时响应选择 + local state = nil + if idx == 0 then + state = true + elseif idx == 1 then + state = false + end + if state ~= nil then + apply_switch(env, "simplification", state) + ctx:clear() -- 切换完成后清空,避免上屏 + return kAccepted + end + elseif ctx.input == "next" and idx == 0 then + env.switcher:select_next_schema() + ctx:clear() + return kAccepted + end + + return kNoop +end + +-- 初始化 switcher +local function init(env) + -- 若当前 librime-lua 版本未集成 Switcher 则无事发生 + if Switcher == nil then return end + env.switcher = Switcher(env.engine) +end + +return { init = init, func = selector } diff --git a/sample/rime.lua b/sample/rime.lua index d5a6f822..56fc04e5 100644 --- a/sample/rime.lua +++ b/sample/rime.lua @@ -67,3 +67,9 @@ single_char_filter = require("single_char") -- reverse_lookup_filter: 依地球拼音为候选项加上带调拼音的注释 -- 详见 `lua/reverse.lua` reverse_lookup_filter = require("reverse") + +-- III. processors: + +-- switch_processor: 通过选择自定义的候选项来切换开关(以简繁切换和下一方案为例) +-- 详见 `lua/switch.lua` +switch_processor = require("switch")