Skip to content

Commit

Permalink
enhance(lua): pin功能的freestyle加词
Browse files Browse the repository at this point in the history
  • Loading branch information
pfeiwu committed Nov 16, 2024
1 parent a8a5b35 commit d269a06
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 8 deletions.
65 changes: 58 additions & 7 deletions lua/moran_pin.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
5 changes: 4 additions & 1 deletion moran.schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,17 @@ moran:
# 置頂詞提示符
indicator: "📌"
# 「萬靈藥」加詞功能
# 通過輸入 「編碼A」// 「編碼B」 實現造詞功能,編碼A是這個新詞的新編碼 編碼B是目前可以打出該詞的編碼
# 通過輸入 「編碼A」//「編碼B」 實現造詞功能,編碼A是這個新詞的新編碼 編碼B是目前可以打出該詞的編碼
# 例如 輸入 tnfb 不能得到「頭腦風暴」這一候選, 此時我們可以輸入 tnfb//tbnkfgbk 按下空格 讓「頭腦風暴」候選上屏,上屏的詞會被加入用戶詞庫中,之後我們可以用 tnfb 來得到「頭腦風暴」這一候選了
# 造出的詞與置頂詞是等同的
panacea:
# 加詞狀態提示
prompt: "〔加詞〕"
# 加詞中綴引導符
infix: "//"
# 自由加詞模式開關,開啓此模式後,輸入「編碼A」//並空格上屏後會進入加詞狀態,此時可以連續地、不受束縛地輸入,再次按下//空格退出加詞狀態,期間輸入的內容將會被記錄爲「編碼A」的新置頂詞
# 此模式適合造一些混合詞,如「A4紙」、「KOS-MOS」等
freestyle: false

# 默認啓用語言模型
__include: moran:/octagram/enable_for_sentence
Expand Down

0 comments on commit d269a06

Please sign in to comment.