Skip to content

Instantly share code, notes, and snippets.

@dohyunkim
Last active November 30, 2025 21:17
Show Gist options
  • Select an option

  • Save dohyunkim/d8b18759916e32d3647afc568bb15194 to your computer and use it in GitHub Desktop.

Select an option

Save dohyunkim/d8b18759916e32d3647afc568bb15194 to your computer and use it in GitHub Desktop.
override regex... commands in krvtypeset.cls
--
-- Only for krvtypeset.cls 2025/11/02 version 0.66
--
-- written by dohyun kim
-- public domain
--
-- 프리앰블 말미에
-- \directlua{ require "krv_regex" }
-- 를 추가하고 컴파일한다.
--
local match_tf_id = luatexbase.new_luafunction"krv_regex_match"
token.set_lua("regex_match:nnTF", match_tf_id, "global", "protected")
lua.get_functions_table()[match_tf_id] = function()
local a1 = token.scan_argument(false)
local a2 = token.scan_argument(false)
a1 = a1:gsub("\\(%A)","%%%1")
if string.match(a1, a2) then
token.put_next(token.create"@firstoftwo")
else
token.put_next(token.create"@secondoftwo")
end
end
local match_f_id = luatexbase.new_luafunction"krv_regex_match2"
token.set_lua("regex_match:nnF", match_f_id, "global", "protected")
lua.get_functions_table()[match_f_id] = function()
local a1 = token.scan_argument(false)
local a2 = token.scan_argument(false)
a1 = a1:gsub("\\(%A)","%%%1")
if not string.match(a1, a2) then
token.put_next(token.create"@firstofone")
else
token.put_next(token.create"@gobble")
end
end
tex.runtoks(function()
tex.sprint[[\begingroup\newcatcodetable\mycctab\catcode`~=12 \savecatcodetable\mycctab\endgroup]]
end)
local mycctab = luatexbase.registernumber"mycctab"
local repl_1_id = luatexbase.new_luafunction"krv_regex_repl"
token.set_lua("regex_replace_once:nnN", repl_1_id, "global", "protected")
lua.get_functions_table()[repl_1_id] = function()
local patt = token.scan_argument(false)
local repl = token.scan_argument(false)
local cs = token.scan_csname()
local mac = token.get_macro(cs)
patt = patt:gsub("%.%+%?",".-")
:gsub("\\(%A)","%%%1")
:gsub("\\(%a)%s","%%%1")
:gsub("가%-힣","\x80-\xFF")
repl = repl:gsub("\\(%d+)","%%%1")
:gsub("\\c%s{(.-)}","\\csname %1\\endcsname ")
mac = mac:gsub(patt, repl, 1)
token.set_macro(mycctab, cs, mac)
end
local repl_all_id = luatexbase.new_luafunction"krv_regex_repl2"
token.set_lua("regex_replace_all:nnN", repl_all_id, "global", "protected")
lua.get_functions_table()[repl_all_id] = function()
local patt = token.scan_argument(false)
local repl = token.scan_argument(false)
local cs = token.scan_csname()
local mac = token.get_macro(cs)
patt = patt:gsub("%.%+%?",".-")
:gsub("\\(%A)","%%%1")
:gsub("\\(%a)%s","%%%1")
:gsub("%%\\cmd%%%{","\\cmd {")
repl = repl:gsub("\\(%d+)","%%%1")
:gsub("\\c%s{(.-)}","\\csname %1\\endcsname ")
:gsub("\\(%A)","%1")
local t = { } -- csname 안에 밑줄이 두 개 있으면 또다른 csname으로 바뀌는 문제를 피하기 위함
mac = mac:gsub("\\csname (.-)\\endcsname", function(c)
table.insert(t, c)
return ("\\csname %s\\endcsname"):format(#t) end)
:gsub(patt, repl)
:gsub("\\csname (%d+)\\endcsname", function(c)
return ("\\csname %s\\endcsname"):format(t[tonumber(c)]) end)
token.set_macro(mycctab, cs, mac)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment