diff options
author | shadmansaleh <13149513+shadmansaleh@users.noreply.github.com> | 2021-12-30 07:10:45 +0600 |
---|---|---|
committer | shadmansaleh <13149513+shadmansaleh@users.noreply.github.com> | 2022-01-01 00:26:58 +0600 |
commit | b411f436d3e2e8a902dbf879d00fc5ed0fc436d3 (patch) | |
tree | 0780d3d7eed40aff1e94347922dc7d5b0ca23107 /src/nvim/eval/funcs.c | |
parent | 5c1b8b77c59d06f80368784f0eac92d6a331a313 (diff) | |
download | rneovim-b411f436d3e2e8a902dbf879d00fc5ed0fc436d3.tar.gz rneovim-b411f436d3e2e8a902dbf879d00fc5ed0fc436d3.tar.bz2 rneovim-b411f436d3e2e8a902dbf879d00fc5ed0fc436d3.zip |
feat(api): add support for lua function & description in keymap
Behavioral changes:
1. Added support for lua function in keymaps in
--------------------------------------------
- nvim_set_keymap
Can set lua function as keymap rhs like following:
```lua
vim.api.nvim_{buf_}set_keymap('n', '<leader>lr', '', {callback = vim.lsp.buf.references})
```
Note: lua function can only be set from lua . If api function being
called from viml or over rpc this option isn't available.
- nvim_{buf_}get_keymap
When called from lua, lua function is returned is `callback` key .
But in other cases callback contains number of the function ref.
- :umap, nvim_del_keymap & nvim_buf_del_keymap clears lua keymaps correctly.
- :map commands for displaing rhs .
For lua keymaps rhs is displayed as <Lua function ref_no>
Note: lua keymap cannot be set through viml command / functions.
- mapargs()
When dict is false it returns string in `<Lua function ref_no>`
format (same format as :map commands).
When dict is true it returns ref_no number in `callback` key.
- mapcheck()
returns string in `<Lua function ref_no>` format (same format as :map commands).
2. Added support for keymap description
---------------------------------------
- nvim_{buf_}set_keymap: added `desc` option in opts table .
```lua
vim.api.nvim_set_keymap('n', '<leader>w', '<cmd>w<cr>', {desc='Save current file'})
```
- nvim_{buf_}get_keymap: contains `desc` in returned list.
- commands like `:nmap <leader>w` will show description in a new line below rhs.
- `maparg()` return dict contains `desc`.
Diffstat (limited to 'src/nvim/eval/funcs.c')
-rw-r--r-- | src/nvim/eval/funcs.c | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/nvim/eval/funcs.c b/src/nvim/eval/funcs.c index 32026282cf..4cccecb23a 100644 --- a/src/nvim/eval/funcs.c +++ b/src/nvim/eval/funcs.c @@ -5980,6 +5980,7 @@ static void get_maparg(typval_T *argvars, typval_T *rettv, int exact) { char_u *keys_buf = NULL; char_u *rhs; + LuaRef rhs_lua; int mode; int abbr = FALSE; int get_dict = FALSE; @@ -6016,7 +6017,7 @@ static void get_maparg(typval_T *argvars, typval_T *rettv, int exact) keys = replace_termcodes(keys, STRLEN(keys), &keys_buf, true, true, true, CPO_TO_CPO_FLAGS); - rhs = check_map(keys, mode, exact, false, abbr, &mp, &buffer_local); + rhs = check_map(keys, mode, exact, false, abbr, &mp, &buffer_local, &rhs_lua); xfree(keys_buf); if (!get_dict) { @@ -6027,10 +6028,15 @@ static void get_maparg(typval_T *argvars, typval_T *rettv, int exact) } else { rettv->vval.v_string = (char_u *)str2special_save((char *)rhs, false, false); } + } else if (rhs_lua != LUA_NOREF) { + size_t msglen = 100; + char *msg = (char *)xmalloc(msglen); + snprintf(msg, msglen, "<Lua function %d>", mp->m_luaref); + rettv->vval.v_string = (char_u *)msg; } } else { tv_dict_alloc_ret(rettv); - if (rhs != NULL) { + if (mp != NULL && (rhs != NULL || rhs_lua != LUA_NOREF)) { // Return a dictionary. mapblock_fill_dict(rettv->vval.v_dict, mp, buffer_local, true); } |