From fbf2c414ad3409e8359ff744765e7486043bb4f7 Mon Sep 17 00:00:00 2001 From: Yilin Yang Date: Sun, 12 May 2019 11:44:48 +0200 Subject: API: nvim_set_keymap, nvim_del_keymap #9924 closes #9136 - Treat empty {rhs} like - getchar.c: Pull "repl. MapArg termcodes" into func The "preprocessing code" surrounding the replace_termcodes calls needs to invoke replace_termcodes, and also check if RHS is equal to "". To reduce code duplication, factor this out into a helper function. Also add an rhs_is_noop flag to MapArguments; buf_do_map_explicit expects an empty {rhs} string for "", but also needs to distinguish that from something like ":map lhs" where no {rhs} was provided. - getchar.c: Use allocated buffer for rhs in MapArgs Since the MAXMAPLEN limit does not apply to the RHS of a mapping (or else an RHS that calls a really long autoload function from a plugin would be incorrectly rejected as being too long), use an allocated buffer for RHS rather than a static buffer of length MAXMAPLEN + 1. - Mappings LHS and RHS can contain literal space characters, newlines, etc. - getchar.c: replace_termcodes in str_to_mapargs It makes sense to do this; str_to_mapargs is, intuitively, supposed to take a "raw" command string and parse it into a totally "do_map-ready" struct. - api/vim.c: Update lhs, rhs len after replace_termcodes Fixes a bug in which replace_termcodes changes the length of lhs or rhs, but the later search through the mappings/abbreviations hashtables still uses the old length value. This would cause the search to fail erroneously and throw 'E31: No such mapping' errors or 'E24: No such abbreviation' errors. - getchar: Create new map_arguments struct So that a string of map arguments can be parsed into a more useful, more portable data structure. - getchar.c: Add buf_do_map function Exactly the same as the old do_map, but replace the hardcoded references to the global `buf_T* curbuf` with a function parameter so that we can invoke it from nvim_buf_set_keymap. - Remove gettext calls in do_map error handling --- src/nvim/getchar.h | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'src/nvim/getchar.h') diff --git a/src/nvim/getchar.h b/src/nvim/getchar.h index 4f548d975a..a40ea7730a 100644 --- a/src/nvim/getchar.h +++ b/src/nvim/getchar.h @@ -5,6 +5,7 @@ #include "nvim/types.h" #include "nvim/buffer_defs.h" #include "nvim/ex_cmds_defs.h" +#include "nvim/vim.h" /// Values for "noremap" argument of ins_typebuf() /// @@ -23,6 +24,39 @@ typedef enum { FLUSH_INPUT // flush typebuf and inchar() input } flush_buffers_T; +/// All possible |:map-arguments| usable in a |:map| command. +/// +/// The argument has no effect on mappings and is excluded from this +/// struct declaration. |noremap| is included, since it behaves like a map +/// argument when used in a mapping. +/// +/// @see mapblock_T +struct map_arguments { + bool buffer; + bool expr; + bool noremap; + bool nowait; + bool script; + bool silent; + bool unique; + + /// The {lhs} of the mapping. + /// + /// vim limits this to MAXMAPLEN characters, allowing us to use a static + /// buffer. Setting lhs_len to a value larger than MAXMAPLEN can signal + /// that {lhs} was too long and truncated. + char_u lhs[MAXMAPLEN + 1]; + size_t lhs_len; + + char_u *rhs; /// The {rhs} of the mapping. + size_t rhs_len; + bool rhs_is_noop; /// True when the {orig_rhs} is . + + char_u *orig_rhs; /// The original text of the {rhs}. + size_t orig_rhs_len; +}; +typedef struct map_arguments MapArguments; + #define KEYLEN_PART_KEY -1 /* keylen value for incomplete key-code */ #define KEYLEN_PART_MAP -2 /* keylen value for incomplete mapping */ #define KEYLEN_REMOVED 9999 /* keylen value for removed sequence */ -- cgit