【发布时间】:2023-01-30 02:19:37
【问题描述】:
使用 Lua 我想知道插入的字符(在插入模式下)是什么触发了 InsertCharPre/TextChangedI/... 事件。
我想编写一个简单的 Neovim 插件,如果我在这样的列表中按 enter 键,它会自动将新行添加到降价项目符号列表中。到目前为止,该插件能够检测给定行是否为项目符号列表行。现在,如果插入的字符是 <CR>,我想插入它,检查项目符号列表的当前缩进并在下一行的同一级别添加一个新元素。自动命令通过 InsertCharPre 事件触发,该事件是在按下某个键但在将其插入缓冲区之前创建的。
此代码正在为其创建自动命令:
api.nvim_create_autocmd("InsertCharPre", {
pattern = { "*.md" },
callback = create_new_bullet_list_entry,
group = mdGroup
})
这是我想继续检查最后输入的字符是否为 <CR> 的地方:
local create_new_bullet_list_entry = function(table)
local cur_line = api.nvim_get_current_line()
local is_bullet_list = is_line_bullet_list(cur_line)
if not is_bullet_list then
return
end
end
如何检查输入的字符触发了InsertCharPre 事件?
我检查了传递给回调函数的表,它不包含有关键入字符的任何信息:
{ ["id"] = 36,["file"] = /foo/bar,["match"] = /foo/bar,["group"] = 28,["buf"] = 1,["event"] = InsertCharPre,}
实现相同结果的其他方法也将非常有帮助!
【问题讨论】: