librime-lua icon indicating copy to clipboard operation
librime-lua copied to clipboard

如何拦截即将上屏的字符串并修改他?

Open hoofcushion opened this issue 1 year ago • 4 comments

如题

hoofcushion avatar Oct 29 '24 07:10 hoofcushion

可以用 select_notifyer or commit_notifier(但要註冊在 engine 前) 上屏 有 Context::Commit() & Select() 在 processor: selector xxxx_editor 中實現 你可以 在這兩個前 加入入 lua_processor 檢查條件 直接使用 engine:commit_text(string)

但我覺的用 lua_filter 去修改 candidate 比較簡單 , 熱鍵 設定 option or recognizer 定義 tag

local F ={}
function F.init(env)
    env.tag= "t1"
end
function F.tags_match(seg,env)
    return env.env.engine.context:get_option(xxx) or seg.tags[env.tag] 
end

function F.fini(env)
end
function F.func(inp, env)
    for cand in inp:iter() do 
        if  ... then
           ....
          yield( replace_cand)
      else
         yield(cand)
      end
end


return F

shewer avatar Oct 29 '24 14:10 shewer

如何在 engine 前注册 commit notifier?或 select notifier? 如何拦截消息让 engine 接收不到?如果用 lua filter 的话,会将候选项的显示效果造成影响。

hoofcushion avatar Oct 31 '24 08:10 hoofcushion

你可以先查一下 wiki / script 說明 context.commit_notifier:connect( func[, group: size_t])
context.commit_notifirer:connect( func) -- 最後 context.commit_notifirer:connect( func, 1) -- 2 context.commit_notifirer:connect( func, 0) -- 1

還有 已經排入候選的candidate 是無法 替換的 ,所以在 engine call OnCommit() 還是取用 原來的candidate.text , 除非是 SimpleCandidate 類 才可以更改text

shewer avatar Oct 31 '24 11:10 shewer

發送 select_notifier 是 selector 送出來的 發送 commit_notifier 是 editor_xxxx 送出來的 要欄下 要在這兩個processor 前加入 lua_processor 由你自己處理 可以看一下 librime source code https://github.com/rime/librime/blob/b74f5fa0b7377ba6103df7b6e757fbf84d36cf1f/src/rime/gear/selector.cc#L118

shewer avatar Oct 31 '24 14:10 shewer