Initial commit
This commit is contained in:
142
home/apps/nvim/lua/scripter/init.lua
Normal file
142
home/apps/nvim/lua/scripter/init.lua
Normal file
@@ -0,0 +1,142 @@
|
||||
local presets = require("scripter.presets")
|
||||
|
||||
local M = {}
|
||||
|
||||
M.resolve = function(char)
|
||||
local check = string.lower(char)
|
||||
if check ~= char then
|
||||
M.upper = true
|
||||
end
|
||||
local next = M.current_script[check]
|
||||
if type(next) == "table" then
|
||||
if next.stop then
|
||||
local stopper = next.stopper
|
||||
if stopper then
|
||||
M.backlog = ""
|
||||
M.current_script = M.script
|
||||
local upper = M.upper
|
||||
M.upper = false
|
||||
return stopper[upper and 2 or 1]
|
||||
else
|
||||
M.current_script = M.script
|
||||
M.upper = false
|
||||
if M.consume then
|
||||
M.backlog = ""
|
||||
return ""
|
||||
else
|
||||
local backlog = M.backlog
|
||||
M.backlog = ""
|
||||
return backlog .. char
|
||||
end
|
||||
end
|
||||
else
|
||||
M.backlog = M.backlog .. char
|
||||
M.current_script = next
|
||||
return ""
|
||||
end
|
||||
end
|
||||
if type(next) == "nil" then
|
||||
local stopper = M.current_script.stopper
|
||||
if stopper then
|
||||
M.backlog = ""
|
||||
M.current_script = M.script
|
||||
local upper = M.upper
|
||||
M.upper = false
|
||||
return stopper[upper and 2 or 1] .. M.resolve(char)
|
||||
else
|
||||
M.current_script = M.script
|
||||
M.upper = false
|
||||
if M.consume then
|
||||
M.backlog = ""
|
||||
return ""
|
||||
else
|
||||
local backlog = M.backlog
|
||||
M.backlog = ""
|
||||
return backlog .. char
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
M.setup = function(opts)
|
||||
M.default = opts.default or presets.none
|
||||
M.script = M.default
|
||||
M.current_script = M.default
|
||||
M.consume = opts.consume or false
|
||||
M.backlog = ""
|
||||
M.mark_namespace = vim.api.nvim_create_namespace("Scripter")
|
||||
M.mark_id = nil
|
||||
M.highlight = opts.highlight or "@comment"
|
||||
M.temporary = false
|
||||
local group = vim.api.nvim_create_augroup("Scripter", {})
|
||||
M.group = group
|
||||
vim.api.nvim_create_autocmd({ "InsertCharPre" }, {
|
||||
group = group,
|
||||
callback = function(_)
|
||||
vim.v.char = M.resolve(vim.v.char)
|
||||
M.upadte_mark()
|
||||
end
|
||||
})
|
||||
vim.api.nvim_create_autocmd({ "InsertLeavePre" }, {
|
||||
group = group,
|
||||
callback = function(_)
|
||||
M.reset()
|
||||
end
|
||||
})
|
||||
end
|
||||
|
||||
M.disable = function()
|
||||
vim.api.nvim_buf_del_extmark(0, M.mark_namespace, M.mark_id)
|
||||
vim.api.nvim_del_augroup_by_id(M.group)
|
||||
M.group = nil
|
||||
end
|
||||
|
||||
M.upadte_mark = function()
|
||||
if M.mark_id ~= nil then
|
||||
vim.api.nvim_buf_del_extmark(0, M.mark_namespace, M.mark_id)
|
||||
M.mark_id = nil
|
||||
end
|
||||
if M.current_script.stopper ~= nil then
|
||||
local cursor = vim.api.nvim_win_get_cursor(0)
|
||||
M.mark_id = vim.api.nvim_buf_set_extmark(
|
||||
0,
|
||||
M.mark_namespace,
|
||||
cursor[1] - 1,
|
||||
cursor[2],
|
||||
{
|
||||
virt_text = { { M.current_script.stopper[M.upper and 2 or 1], M.highlight } },
|
||||
virt_text_pos = "overlay",
|
||||
}
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
M.reset = function()
|
||||
if M.current_script.stopper then
|
||||
vim.api.nvim_put({ M.current_script.stopper[M.upper and 2 or 1] }, "c", false, true)
|
||||
print(M.current_script.stopper)
|
||||
end
|
||||
if M.temporary then
|
||||
M.script = M.default
|
||||
M.temporary = false
|
||||
end
|
||||
M.current_script = M.script
|
||||
M.backlog = ""
|
||||
M.upper = false
|
||||
M.upadte_mark()
|
||||
end
|
||||
|
||||
M.set_script = function(script, temporary)
|
||||
M.script = script
|
||||
M.current_script = script
|
||||
M.temporary = temporary
|
||||
end
|
||||
|
||||
M.mapping = {}
|
||||
M.mapping.set_script = function(script, temporary)
|
||||
return function()
|
||||
M.set_script(script, temporary)
|
||||
end
|
||||
end
|
||||
|
||||
return M
|
||||
22
home/apps/nvim/lua/scripter/presets/auto.lua
Normal file
22
home/apps/nvim/lua/scripter/presets/auto.lua
Normal file
@@ -0,0 +1,22 @@
|
||||
local M = {}
|
||||
|
||||
M.autoscript = function(table)
|
||||
local output = {}
|
||||
for i = 1, #table, 3 do
|
||||
local path = table[i]
|
||||
local cmod = output
|
||||
for j = 1, string.len(path) do
|
||||
local dir = string.sub(path, j, j)
|
||||
if cmod[dir] == nil then
|
||||
cmod[dir] = { stop = string.len(path) == j }
|
||||
else
|
||||
cmod[dir].stop = false
|
||||
end
|
||||
cmod = cmod[dir]
|
||||
end
|
||||
cmod.stopper = { table[i + 1], table[i + 2] }
|
||||
end
|
||||
return output
|
||||
end
|
||||
|
||||
return M
|
||||
37
home/apps/nvim/lua/scripter/presets/cyrillic.lua
Normal file
37
home/apps/nvim/lua/scripter/presets/cyrillic.lua
Normal file
@@ -0,0 +1,37 @@
|
||||
local auto = require("scripter.presets.auto")
|
||||
|
||||
return auto.autoscript({
|
||||
"zh", "ж", "Ж",
|
||||
"ch", "ч", "Ч",
|
||||
"sh", "ш", "Ш",
|
||||
"sh'", "щ", "Щ",
|
||||
"yi", "ы", "Ы",
|
||||
"j", "ë", "Ë",
|
||||
"''", "ъ", "Ъ",
|
||||
"c", "ц", "Ц",
|
||||
"ye", "э", "Э",
|
||||
"yu", "ю", "Ю",
|
||||
"ya", "я", "Я",
|
||||
"a", "а", "А",
|
||||
"b", "б", "Б",
|
||||
"v", "в", "В",
|
||||
"g", "г", "Г",
|
||||
"d", "д", "Д",
|
||||
"e", "е", "Е",
|
||||
"z", "з", "З",
|
||||
"i", "и", "И",
|
||||
"y", "й", "Й",
|
||||
"k", "к", "К",
|
||||
"l", "л", "Л",
|
||||
"m", "м", "М",
|
||||
"n", "н", "Н",
|
||||
"o", "о", "О",
|
||||
"p", "п", "П",
|
||||
"r", "р", "Р",
|
||||
"s", "с", "С",
|
||||
"t", "т", "Т",
|
||||
"u", "у", "У",
|
||||
"f", "ф", "Ф",
|
||||
"h", "х", "Х",
|
||||
"'", "ь", "Ь",
|
||||
})
|
||||
6
home/apps/nvim/lua/scripter/presets/init.lua
Normal file
6
home/apps/nvim/lua/scripter/presets/init.lua
Normal file
@@ -0,0 +1,6 @@
|
||||
local M = {}
|
||||
|
||||
M.cyrillic = require("scripter.presets.cyrillic")
|
||||
M.none = require("scripter.presets.none")
|
||||
|
||||
return M
|
||||
3
home/apps/nvim/lua/scripter/presets/none.lua
Normal file
3
home/apps/nvim/lua/scripter/presets/none.lua
Normal file
@@ -0,0 +1,3 @@
|
||||
return {
|
||||
stop = true,
|
||||
}
|
||||
Reference in New Issue
Block a user