local M = {}
local replacements = {
['<-'] = '←',
['xx'] = '×',
['/\\'] = '×',
[':-'] = '÷',
['*O'] = '⍟',
['[-'] = '⌹',
['-]'] = '⌹',
['OO'] = '○',
['77'] = '⌈',
['FF'] = '⌈',
['ll'] = '⌊',
['LL'] = '⌊',
['T_'] = '⌶',
['II'] = '⌶',
['|_'] = '⊥',
['TT'] = '⊤',
['-|'] = '⊣',
['|-'] = '⊢',
['=/'] = '≠',
['L-'] = '≠',
['<='] = '≤',
['<_'] = '≤',
['>='] = '≥',
['>_'] = '≥',
['=='] = '≡',
['=_'] = '≡',
['7='] = '≢',
['=-'] = '≢',
['Z-'] = '≢',
['vv'] = '∨',
['^^'] = '∧',
['^~'] = '⍲',
['v~'] = '⍱',
['^|'] = '↑',
['v|'] = '↓',
['(('] = '⊂',
['cc'] = '⊂',
['(_'] = '⊆',
['c_'] = '⊆',
['))'] = '⊃',
['[|'] = '⌷',
['|]'] = '⌷',
['A|'] = '⍋',
['V|'] = '⍒',
['ii'] = '⍳',
['i_'] = '⍸',
['ee'] = '∊',
['e_'] = '⍷',
['uu'] = '∪',
['UU'] = '∪',
['nn'] = '∩',
['/-'] = '⌿',
['\\-'] = '⍀',
[',-'] = '⍪',
['rr'] = '⍴',
['pp'] = '⍴',
['O|'] = '⌽',
['O-'] = '⊖',
['O\\'] = '⍉',
['::'] = '¨',
['""'] = '¨',
['~:'] = '⍨',
['~"'] = '⍨',
['*:'] = '⍣',
['*"'] = '⍣',
['oo'] = '∘',
['o:'] = '⍤',
['o"'] = '⍤',
['O:'] = '⍥',
['O"'] = '⍥',
['[\''] = '⍞',
['\']'] = '⍞',
['[]'] = '⎕',
['[:'] = '⍠',
[':]'] = '⍠',
['[='] = '⌸',
['=]'] = '⌸',
['[<'] = '⌺',
['>]'] = '⌺',
['o_'] = '⍎',
['oT'] = '⍕',
['o-'] = '⍕',
['<>'] = '⋄',
['^v'] = '⋄',
['on'] = '⍝',
['->'] = '→',
['aa'] = '⍺',
['ww'] = '⍵',
['VV'] = '∇',
['v-'] = '∇',
['--'] = '¯',
['0~'] = '⍬',
['AA'] = '∆',
['^-'] = '∆',
['A_'] = '⍙',
['^='] = '⍙',
['[?'] = '⍰',
['?]'] = '⍰',
[':V'] = '⍢',
['∇"'] = '⍢',
['||'] = '∥',
['ox'] = '¤',
[')_'] = '⊇',
['_)'] = '⊇',
['V~'] = '⍫',
['\'\''] = '`',
}
function M.apl_replace()
if not M.enabled then
return
end
local bufnr = vim.api.nvim_get_current_buf()
local line_count = vim.api.nvim_buf_line_count(bufnr)
local new_lines = {}
for i = 0, line_count - 1 do
local origcont = vim.api.nvim_buf_get_lines(bufnr, i, i + 1, false)[1]
for key, value in pairs(replacements) do
local eskey = key:gsub("([%.%?%*%+%-%[%]%(%)%$%^%$])", "%%%1")
origcont,_ = string.gsub(origcont, eskey, value)
end
table.insert(new_lines, origcont)
end
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, new_lines)
vim.api.nvim_command('write')
end
M.enabled = false
local function apl_replace2()
if not M.enabled then
return
end
local bufnr = vim.api.nvim_get_current_buf()
local cursor_pos = vim.api.nvim_win_get_cursor(0)
local line = vim.api.nvim_get_current_line()
local col = cursor_pos[2]
if col >= 2 then
local before_chars = line:sub(col - 1, col) -- 获取光标前两个字符
local new_chars = before_chars
for key, value in pairs(replacements) do
local eskey = key:gsub("([%.%?%*%+%-%[%]%(%)%$%^%$])", "%%%1")
new_chars = string.gsub(new_chars, eskey, value)
if new_chars ~= before_chars then
break
end
end
local new_line = line:sub(1, col - 2) .. new_chars .. line:sub(col+1)
vim.api.nvim_set_current_line(new_line) -- 更新当前行
vim.api.nvim_win_set_cursor(0, { cursor_pos[1], col+1 }) -- 移动光标
end
end
function M.toggle()
M.enabled = not M.enabled
if M.enabled then
print("插件已启用,按下 Tab 键将替换光标前两个字符。")
else
print("插件已禁用。")
end
end
vim.keymap.set('i', '<Tab>', function()
M.apl_replace2()
end, { noremap = true, silent = true })
vim.keymap.set('n', '<Tab>', function()
M.apl_replace2()
end, { noremap = true, silent = true })
vim.api.nvim_create_user_command('ToggleApl', function()
M.toggle()
end, {})
M.apl_replace2 = apl_replace2
return M
apl.lua nvim plugin
于 2024-08-31 17:37:43 首次发布