diff options
author | Rui Abreu Ferreira <raf-ep@gmx.com> | 2014-07-06 01:43:40 +0100 |
---|---|---|
committer | Thiago de Arruda <tpadilha84@gmail.com> | 2014-07-07 12:18:36 -0300 |
commit | 67c5684e23ab7dc012979a058e6820fb7a6bc9ce (patch) | |
tree | 6f2fd428f8b0508c8bebb2d0a383eb0c19300935 /src/nvim/api/vim.c | |
parent | aad2a69c96c8e1435b52a960a1d241244e23abda (diff) | |
download | rneovim-67c5684e23ab7dc012979a058e6820fb7a6bc9ce.tar.gz rneovim-67c5684e23ab7dc012979a058e6820fb7a6bc9ce.tar.bz2 rneovim-67c5684e23ab7dc012979a058e6820fb7a6bc9ce.zip |
Add vim_feedkeys API function
- New API function to push data to the typeahead buffer - this should
equivalent to the vimscript feedkeys() function
- In Vim there was a --remote-send command to insert input into a
Vim server instance. Besides accepting key sequences it also
translated special keys such as <CR> or <Leader>, backslash notation
is ignored. This commit backports the original Vim handler for
--remote-send as a bool option for vim_feedkeys()
- vim-patch:0
Diffstat (limited to 'src/nvim/api/vim.c')
-rw-r--r-- | src/nvim/api/vim.c | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index fbeb42cf4b..0a5dfdf337 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -19,6 +19,8 @@ #include "nvim/message.h" #include "nvim/eval.h" #include "nvim/misc2.h" +#include "nvim/term.h" +#include "nvim/getchar.h" #define LINE_BUFFER_SIZE 4096 @@ -47,6 +49,60 @@ void vim_command(String str, Error *err) try_end(err); } +/// Pass input keys to Neovim +/// +/// @param keys to be typed +/// @param replace_tcodes If true replace special keys such as <CR> or <Leader> +/// for compatibility with Vim --remote-send expressions +/// @param remap If True remap keys +/// @param typed Handle keys as if typed; otherwise they are handled as +/// if coming from a mapping. This matters for undo, +/// opening folds, etc. +void vim_feedkeys(String keys, Boolean replace_tcodes, Boolean remap, + Boolean typed, Error *err) +{ + char *ptr = NULL; + char *cpo_save = (char *)p_cpo; + + if (replace_tcodes) { + // Set 'cpoptions' the way we want it. + // B set - backslashes are *not* treated specially + // k set - keycodes are *not* reverse-engineered + // < unset - <Key> sequences *are* interpreted + // The last but one parameter of replace_termcodes() is TRUE so that the + // <lt> sequence is recognised - needed for a real backslash. + p_cpo = (char_u *)"Bk"; + replace_termcodes((char_u *)keys.data, (char_u **)&ptr, false, true, true); + p_cpo = (char_u *)cpo_save; + } else { + ptr = keys.data; + } + + if (ptr == NULL) { + set_api_error("Failed to eval expression", err); + } else { + // Add the string to the input stream. + // Can't use add_to_input_buf() here, we now have K_SPECIAL bytes. + // + // First clear typed characters from the typeahead buffer, there could + // be half a mapping there. Then append to the existing string, so + // that multiple commands from a client are concatenated. + if (typebuf.tb_maplen < typebuf.tb_len) { + del_typebuf(typebuf.tb_len - typebuf.tb_maplen, typebuf.tb_maplen); + } + (void)ins_typebuf((char_u *)ptr, (remap ? REMAP_YES : REMAP_NONE), + typebuf.tb_len, !typed, false); + + // Let input_available() know we inserted text in the typeahead + // buffer. */ + typebuf_was_filled = true; + + if (replace_tcodes) { + free(ptr); + } + } +} + /// Evaluates the expression str using the vim internal expression /// evaluator (see |expression|). /// Dictionaries and lists are recursively expanded. |