apl.lua nvim plugin

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值