From d269a06cf01413476658f3325ea07250522225ef Mon Sep 17 00:00:00 2001 From: Pengfei Wu Date: Sat, 16 Nov 2024 13:31:16 +0800 Subject: [PATCH] =?UTF-8?q?enhance(lua):=20pin=E5=8A=9F=E8=83=BD=E7=9A=84f?= =?UTF-8?q?reestyle=E5=8A=A0=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lua/moran_pin.lua | 65 ++++++++++++++++++++++++++++++++++++++++++----- moran.schema.yaml | 5 +++- 2 files changed, 62 insertions(+), 8 deletions(-) diff --git a/lua/moran_pin.lua b/lua/moran_pin.lua index 10ef06e..a3b05a3 100644 --- a/lua/moran_pin.lua +++ b/lua/moran_pin.lua @@ -332,24 +332,68 @@ local panacea_translator = {} function panacea_translator.init(env) env.infix = env.engine.schema.config:get_string("moran/pin/panacea/infix") or '//' - env.enscaped_infix = string.gsub(env.infix, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1") + env.escaped_infix = string.gsub(env.infix, "([%^%$%(%)%%%.%[%]%*%+%-%?])", "%%%1") env.prompt = env.engine.schema.config:get_string("moran/pin/panacea/prompt") or "〔加詞〕" env.indicator = env.engine.schema.config:get_string("moran/pin/indicator") or "📌" + env.freestyle = env.engine.schema.config:get_bool("moran/pin/panacea/freestyle") or false + + env.freestyle_state = false + env.freestyle_text = "" + env.freestyle_code = "" + user_db.acquire() - local pattern = string.format("(.+)%s(.+)", env.enscaped_infix) + local pattern = string.format("(.+)%s(.+)", env.escaped_infix) local function on_commit(ctx) local commit_text = ctx:get_commit_text() + local selected_cand = ctx:get_selected_candidate() + if selected_cand ~= nil then + if env.freestyle and selected_cand:get_genuine().type == "pin_tip" then + if env.freestyle_state then + if env.freestyle_code and env.freestyle_code ~= "" and env.freestyle_text and env.freestyle_text ~= "" then + user_db.toggle_pin_status(env.freestyle_code, env.freestyle_text) + env.freestyle_code = "" + env.freestyle_text = "" + end + else + if string.sub(ctx.input, - #env.infix) == env.infix then + env.freestyle_code = string.sub(ctx.input, 1, #ctx.input - #env.infix) + end + + if env.freestyle_code == "" then + return + end + end + env.freestyle_state = not env.freestyle_state + return + end + end + if env.freestyle_state then + env.freestyle_text = env.freestyle_text .. commit_text + return + end + + local code, original_code = ctx.input:match(pattern) - if original_code and code and commit_text then + if original_code and original_code ~= "" and + code and code ~= "" and + commit_text and commit_text ~= "" then user_db.toggle_pin_status(code, commit_text) end end local function on_update_or_select(ctx) + if env.freestyle_state then + local segment = ctx.composition:back() + if segment ~= nil then + segment.prompt = env.prompt .. " | " .. env.freestyle_text .. " | " .. env.freestyle_code + end + return + end + local code, original_code = ctx.input:match(pattern) if original_code and code then local segment = ctx.composition:back() - segment.prompt = env.prompt .." | " .. code + segment.prompt = env.prompt .. " | " .. code end end @@ -366,14 +410,21 @@ function panacea_translator.fini(env) end function panacea_translator.func(input, seg, env) - local pattern = "[a-z]+"..env.enscaped_infix + local pattern = "[a-z]*" .. env.escaped_infix local match = input:match(pattern) if match then - local tip_cand = Candidate("pin_tip", 0, #match, "", "➕"..env.indicator) + local comment = "➕" .. env.indicator + if env.freestyle then + if env.freestyle_state then + comment = "完成加詞" .. comment + else + comment = "開始加詞" .. comment + end + end + local tip_cand = Candidate("pin_tip", 0, #match, "", comment) tip_cand.quality = math.huge yield(tip_cand) - return end end diff --git a/moran.schema.yaml b/moran.schema.yaml index d08585f..c52305a 100644 --- a/moran.schema.yaml +++ b/moran.schema.yaml @@ -388,7 +388,7 @@ moran: # 置頂詞提示符 indicator: "📌" # 「萬靈藥」加詞功能 - # 通過輸入 「編碼A」// 「編碼B」 實現造詞功能,編碼A是這個新詞的新編碼 編碼B是目前可以打出該詞的編碼 + # 通過輸入 「編碼A」//「編碼B」 實現造詞功能,編碼A是這個新詞的新編碼 編碼B是目前可以打出該詞的編碼 # 例如 輸入 tnfb 不能得到「頭腦風暴」這一候選, 此時我們可以輸入 tnfb//tbnkfgbk 按下空格 讓「頭腦風暴」候選上屏,上屏的詞會被加入用戶詞庫中,之後我們可以用 tnfb 來得到「頭腦風暴」這一候選了 # 造出的詞與置頂詞是等同的 panacea: @@ -396,6 +396,9 @@ moran: prompt: "〔加詞〕" # 加詞中綴引導符 infix: "//" + # 自由加詞模式開關,開啓此模式後,輸入「編碼A」//並空格上屏後會進入加詞狀態,此時可以連續地、不受束縛地輸入,再次按下//空格退出加詞狀態,期間輸入的內容將會被記錄爲「編碼A」的新置頂詞 + # 此模式適合造一些混合詞,如「A4紙」、「KOS-MOS」等 + freestyle: false # 默認啓用語言模型 __include: moran:/octagram/enable_for_sentence