diff options
author | TJ DeVries <devries.timothyj@gmail.com> | 2020-08-14 10:03:17 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-08-14 10:03:17 -0400 |
commit | 3ccdbc570d856ee3ff1f64204e352a40b9030ac2 (patch) | |
tree | e73d198bef4dce52bfd990dc57ea419e8b1fc703 /src/nvim/lua/vim.lua | |
parent | aa48c1c724f7164485782a3a5a8ff7a94373f607 (diff) | |
download | rneovim-3ccdbc570d856ee3ff1f64204e352a40b9030ac2.tar.gz rneovim-3ccdbc570d856ee3ff1f64204e352a40b9030ac2.tar.bz2 rneovim-3ccdbc570d856ee3ff1f64204e352a40b9030ac2.zip |
lua: add vim.register_keystroke_callback (#12536)
* feat: Add vim.register_keystroke_callback
* fixup: Forgot to remove mention of old option
* fixup: Answer jamessan comments
* fixup: Answer norcalli comments
* fixup: portability
* Update runtime/doc/lua.txt
Co-authored-by: Ashkan Kiani <ashkan.k.kiani@gmail.com>
Diffstat (limited to 'src/nvim/lua/vim.lua')
-rw-r--r-- | src/nvim/lua/vim.lua | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/nvim/lua/vim.lua b/src/nvim/lua/vim.lua index 820b237c4f..bfa8b91208 100644 --- a/src/nvim/lua/vim.lua +++ b/src/nvim/lua/vim.lua @@ -489,4 +489,60 @@ function vim.defer_fn(fn, timeout) return timer end +local on_keystroke_callbacks = {} + +--- Register a lua {fn} with an {id} to be run after every keystroke. +--- +--@param fn function: Function to call. It should take one argument, which is a string. +--- The string will contain the literal keys typed. +--- See |i_CTRL-V| +--- +--- If {fn} is nil, it removes the callback for the associated {ns_id} +--@param ns_id number? Namespace ID. If not passed or 0, will generate and return a new +--- namespace ID from |nvim_create_namesapce()| +--- +--@return number Namespace ID associated with {fn} +--- +--@note {fn} will be automatically removed if an error occurs while calling. +--- This is to prevent the annoying situation of every keystroke erroring +--- while trying to remove a broken callback. +--@note {fn} will not be cleared from |nvim_buf_clear_namespace()| +--@note {fn} will receive the keystrokes after mappings have been evaluated +function vim.register_keystroke_callback(fn, ns_id) + vim.validate { + fn = { fn, 'c', true}, + ns_id = { ns_id, 'n', true } + } + + if ns_id == nil or ns_id == 0 then + ns_id = vim.api.nvim_create_namespace('') + end + + on_keystroke_callbacks[ns_id] = fn + return ns_id +end + +--- Function that executes the keystroke callbacks. +--@private +function vim._log_keystroke(char) + local failed_ns_ids = {} + local failed_messages = {} + for k, v in pairs(on_keystroke_callbacks) do + local ok, err_msg = pcall(v, char) + if not ok then + vim.register_keystroke_callback(nil, k) + + table.insert(failed_ns_ids, k) + table.insert(failed_messages, err_msg) + end + end + + if failed_ns_ids[1] then + error(string.format( + "Error executing 'on_keystroke' with ns_ids of '%s'\n With messages: %s", + table.concat(failed_ns_ids, ", "), + table.concat(failed_messages, "\n"))) + end +end + return module |