I am trying to write a pretty-printer Lua plugin for a LaTeX-like language. The idea is that if a user types \cup
in insert mode then presses <Space>
or <Tab>
or <CR>
, then my script will read the preceding string and transform it into ∪
. So far I have:
_G.rewrite_unicode = function(keystroke)
local r, c = unpack(vim.api.nvim_win_get_cursor(0))
return keystroke
end
vim.api.nvim_set_keymap('i', '<Space>', "v:lua.rewrite_unicode('<Space>')", { noremap = true, expr = true })
vim.api.nvim_set_keymap('i', '<Tab>', "v:lua.rewrite_unicode('<Tab>')", { noremap = true, expr = true })
vim.api.nvim_set_keymap('i', '<CR>', "v:lua.rewrite_unicode('<CR>')", { noremap = true, expr = true }
but I don't know how I can read the string preceding the cursor position in the buffer, then rewrite it. What APIs would I use for that?
:help inoreabbrev
). Snippets if you want to go more powerful than that.