hchunhui / librime-lua

Extending RIME with Lua scripts
BSD 3-Clause "New" or "Revised" License
359 stars 45 forks source link

请教 commit_history:latest_text() 方法的问题:该方法未能获取上一次输入由 ⇧Shift 引导的上屏符号 #303

Closed mirtlecn closed 10 months ago

mirtlecn commented 10 months ago

如下脚本:将指定字符后的指定符号切换为 ascii_mode,不清楚哪里出了问题

-- https://github.com/rime/librime/issues/712#issuecomment-1722225697
-- - lua_processor@*auto_ascii_punct
-- auto_ascii_punct:
--  # - /last_commmit_key/auto_ascii_key/
--  - /%d/,-*+//
--  - /%a/:_-./
--  = /(/)/
--  # - /%a/%a/
--  - /</<>/

local P = {}

-- .....

function P.func(key, env)
    local context = env.engine.context
    -- ...
    if key.keycode < 0x7f and key.keycode > 0x20 then
        local latest_text = context.commit_history:latest_text()

        if not latest_text or not P.list then
            return 2
        end

        log.error('last_key: ' .. context.commit_history:latest_text() .. " ---------------------")

        local ascii_str = string.char(key.keycode)

        for k, v in pairs(P.list) do
            if latest_text:match("[" .. k .. "]$") and ascii_str:match("[" .. v .. "]") then
                return 0
            end
        end
    end
    return 2
end
return P

实际运用中,

看 context.commit_history:latest_text() 的输出发现,由 Shift键 引导的符号(<{()都未能正常获取

输入 ()))) 发现:

E20240123 18:49:37.563741 10008 types.cc:1342] last_key: ( ---------------------
E20240123 18:49:37.678844 10008 types.cc:1342] last_key: ( ---------------------
E20240123 18:49:38.103999 10008 types.cc:1342] last_key: ( ---------------------
E20240123 18:49:38.180725 10008 types.cc:1342] last_key: ( ---------------------
E20240123 18:49:38.290984 10008 types.cc:1342] last_key: ( ---------------------
E20240123 18:49:38.392171 10008 types.cc:1342] last_key: ( ---------------------
E20240123 18:49:38.480557 10008 types.cc:1342] last_key: ( ---------------------
E20240123 18:49:38.576220 10008 types.cc:1342] last_key: ( ---------------------
E20240123 18:49:38.647603 10008 types.cc:1342] last_key: ( ---------------------
E20240123 18:49:38.743587 10008 types.cc:1342] last_key: ( ---------------------
E20240123 18:49:39.301573 10008 types.cc:1342] last_key: ( ---------------------
E20240123 18:49:39.413802 10008 types.cc:1342] last_key: ( ---------------------

输入 []] 是

E20240123 18:50:21.927165 10008 types.cc:1342] last_key: [ ---------------------
E20240123 18:50:23.618816 10008 types.cc:1342] last_key: [ ---------------------
E20240123 18:50:23.771934 10008 types.cc:1342] last_key: ] ---------------------
E20240123 18:50:24.176414 10008 types.cc:1342] last_key: ] ---------------------
E20240123 18:50:25.149842 10008 types.cc:1342] last_key: ] ---------------------
E20240123 18:50:25.816538 10008 types.cc:1342] last_key: ] ---------------------
E20240123 18:50:26.487324 10008 types.cc:1342] last_key: ] ---------------------
E20240123 18:50:26.808023 10008 types.cc:1342] last_key: ] ---------------------

临时的解决方案是:用 commit_text() 方法,然后 return 1