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

怎样实现中英文之间自动加空格

Open H4M5TER opened this issue 2 years ago • 5 comments
trafficstars

考虑以下几种方式

  1. 在中英混输上屏时,自动在中英文之间加空格
  2. 在键入任意字符时记录并和上一个字符比对

有哪些接口可能有帮助?

H4M5TER avatar Apr 04 '23 22:04 H4M5TER

在engine/filters 最後加上一個 lua_filter 檢查前面commit 的字的最後一個 字元不是空白 時 pre_space = " " 候選字是英文時把 候選字 加上空白

local F={}
function F.init(env)
   env.size=20
   env.history={}
   
   env.notifier= env.engine.context.commit_notifier:connect( function(ctx)
      for i=env.size, #env.history do
           env.history[i] = nil
       end
       table.insert( env.history, 1, ctx:get_selected_candidate().text)
   end)
end
function F.fini(env)
   env.notifier:disconnect()
end
function F.tag_match(seg, env)
   -- return  env.engine.context:get_option('auto_space")  -- 可以加上開關
   return true
end


function F.func(inp,env)
  local pre_space = ""
  if env.history[1] and not env.history[1]:find(" $") then
    pre_space= " "
  end

   for cand in inp:iter() do
       if #cand.text == utf8.len(cand.txt) then -- 英文候選加空白
           cand.text = pre_space .. cand.text .. " "
        end
        yield( cand )
   end
end

return F

shewer avatar Apr 07 '23 04:04 shewer

非常感谢!

我在 schema 里最后加上 lua_filter@pangu_filterrime.lua 里加上 local pangu_filter = require("pangu") 新建一个 lua 文件夹,在里面新建 pangu.lua 然后把上面的内容贴进去 重新部署之后遇到问题:任意输入都没有候选词

H4M5TER avatar Apr 27 '23 20:04 H4M5TER

是否有可能给 select_notifier 传入一个函数,在其中修改即将上屏的候选词?我没翻到相关的接口

H4M5TER avatar Apr 27 '23 20:04 H4M5TER

可以耶, 測試狀態,但是 大部份 candidate text 是改不了的, 除了 SimpleCandidate

在notifier func 中,加入 engine:commit_text( "--") 會 commit cand 後 commit -- ( 在commit_history 中會增加一個 record type: "raw" text: "--"

你可參考 librime rime/candidate.h librime-lua src/typess.cc CandidateReg set_xxxxx 會檢查 Candidate 型別

shewer avatar Apr 28 '23 01:04 shewer

然而中英文之间不应该有空格

groverlynn avatar Dec 27 '23 18:12 groverlynn