From 83170a49865a3e271f918cde82201e794d029882 Mon Sep 17 00:00:00 2001 From: bfredl Date: Wed, 22 Jun 2022 17:39:30 +0200 Subject: [PATCH 1/2] refactor(map): simplify add_map params --- src/nvim/ex_getln.c | 4 ++-- src/nvim/mapping.c | 38 ++++++++++++++++++-------------------- src/nvim/normal.c | 2 +- 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 93758b9ec9..154086b82c 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -6645,8 +6645,8 @@ static int open_cmdwin(void) const int histtype = hist_char2type(cmdwin_type); if (histtype == HIST_CMD || histtype == HIST_DEBUG) { if (p_wc == TAB) { - add_map((char_u *)" ", MODE_INSERT, false); - add_map((char_u *)" a", MODE_NORMAL, false); + add_map("", "", MODE_INSERT, true); + add_map("", "a", MODE_NORMAL, true); } set_option_value("ft", 0L, "vim", OPT_LOCAL); } diff --git a/src/nvim/mapping.c b/src/nvim/mapping.c index d4f7450af4..ce313b4f46 100644 --- a/src/nvim/mapping.c +++ b/src/nvim/mapping.c @@ -2092,37 +2092,35 @@ void f_mapcheck(typval_T *argvars, typval_T *rettv, FunPtr fptr) void init_default_mappings(void) { - add_map((char_u *)"Y y$", MODE_NORMAL, true); + add_map("Y", "y$", MODE_NORMAL, false); // Use normal! to prevent inserting raw when using i_ // See https://github.com/neovim/neovim/issues/17473 - add_map((char_u *)" nohlsearchdiffupdatenormal! ", - MODE_NORMAL, true); - add_map((char_u *)" u", MODE_INSERT, true); - add_map((char_u *)" u", MODE_INSERT, true); - add_map((char_u *)"* y/\\\\V\"", MODE_VISUAL, true); - add_map((char_u *)"# y?\\\\V\"", MODE_VISUAL, true); + add_map("", "nohlsearchdiffupdatenormal! ", + MODE_NORMAL, false); + add_map("", "u", MODE_INSERT, false); + add_map("", "u", MODE_INSERT, false); + add_map("*", "y/\\\\V\"", MODE_VISUAL, false); + add_map("#", "y?\\\\V\"", MODE_VISUAL, false); } -/// Add a mapping. Unlike @ref do_map this copies the {map} argument, so +/// Add a mapping. Unlike @ref do_map this copies the string arguments, so /// static or read-only strings can be used. /// -/// @param map C-string containing the arguments of the map/abbrev command, -/// i.e. everything except the initial `:[X][nore]map`. +/// @param lhs C-string containing the lhs of the mapping +/// @param rhs C-string containing the rhs of the mapping /// @param mode Bitflags representing the mode in which to set the mapping. /// See @ref get_map_mode. -/// @param nore If true, make a non-recursive mapping. -void add_map(char_u *map, int mode, bool nore) +/// @param buffer If true, make a buffer-local mapping for curbuf +void add_map(char *lhs, char *rhs, int mode, bool buffer) { - char_u *s; - char *cpo_save = p_cpo; + MapArguments args = { 0 }; + set_maparg_lhs_rhs(lhs, strlen(lhs), rhs, strlen(rhs), LUA_NOREF, 0, &args); + args.buffer = buffer; - p_cpo = ""; // Allow <> notation - // Need to put string in allocated memory, because do_map() will modify it. - s = vim_strsave(map); - (void)do_map(nore ? 2 : 0, s, mode, false); - xfree(s); - p_cpo = cpo_save; + buf_do_map(2, &args, mode, false, curbuf); + xfree(args.rhs); + xfree(args.orig_rhs); } /// Any character has an equivalent 'langmap' character. This is used for diff --git a/src/nvim/normal.c b/src/nvim/normal.c index 9637fe85e2..1ca403965c 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -4282,7 +4282,7 @@ static void nv_ident(cmdarg_T *cap) // Start insert mode in terminal buffer restart_edit = 'i'; - add_map((char_u *)" bdelete!", MODE_TERMINAL, true); + add_map("", "bdelete!", MODE_TERMINAL, true); } } From 7ab2e12ebc9cdab71a4751550b9026db3a4bd0cf Mon Sep 17 00:00:00 2001 From: bfredl Date: Tue, 28 Jun 2022 20:38:34 +0200 Subject: [PATCH 2/2] refactor(aucmd): call define_autocmd() directly for default autocmds --- src/nvim/autocmd.c | 20 ++++++++++++-------- test/functional/core/startup_spec.lua | 6 +++--- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/src/nvim/autocmd.c b/src/nvim/autocmd.c index 7301e07a06..75666c600c 100644 --- a/src/nvim/autocmd.c +++ b/src/nvim/autocmd.c @@ -2707,14 +2707,21 @@ static void do_autocmd_focusgained(bool gained) recursive = false; } -// initialization +static void define_autocmd(event_T event, char *pat, char *group, bool once, bool nested, char *cmd) +{ + AucmdExecutable exec = AUCMD_EXECUTABLE_INIT; + exec.type = CALLABLE_EX; + exec.callable.cmd = cmd; // autocmd_register() makes a copy + int group_id = augroup_add(group); + autocmd_register(0, event, pat, (int)strlen(pat), group_id, once, nested, NULL, exec); +} +/// initialization of default autocmds void init_default_autocmds(void) { // open terminals when opening files that start with term:// #define PROTO "term://" - do_cmdline_cmd("augroup nvim_terminal"); - do_cmdline_cmd("autocmd BufReadCmd " PROTO "* ++nested " + define_autocmd(EVENT_BUFREADCMD, PROTO "*", "nvim_terminal", false, true, "if !exists('b:term_title')|call termopen(" // Capture the command string "matchstr(expand(\"\"), " @@ -2723,11 +2730,8 @@ void init_default_autocmds(void) "{'cwd': expand(get(matchlist(expand(\"\"), " "'\\c\\m" PROTO "\\(.\\{-}\\)//'), 1, ''))})" "|endif"); - do_cmdline_cmd("augroup END"); #undef PROTO - // limit syntax synchronization in the command window - do_cmdline_cmd("augroup nvim_cmdwin"); - do_cmdline_cmd("autocmd! CmdwinEnter [:>] syntax sync minlines=1 maxlines=1"); - do_cmdline_cmd("augroup END"); + define_autocmd(EVENT_CMDWINENTER, "[:>]", "nvim_cmdwin", false, false, + "syntax sync minlines=1 maxlines=1"); } diff --git a/test/functional/core/startup_spec.lua b/test/functional/core/startup_spec.lua index 919b7b3f9a..5ba80a3646 100644 --- a/test/functional/core/startup_spec.lua +++ b/test/functional/core/startup_spec.lua @@ -499,11 +499,11 @@ describe('sysinit', function() [[" -u NONE -i NONE --cmd "set noruler" -D')]]) screen:expect([[ ^ | - | Entering Debug mode. Type "cont" to continue. | - cmd: augroup nvim_terminal | + pre-vimrc command line | + cmd: set noruler | > | - <" -u NONE -i NONE --cmd "set noruler" -D 1,0-1 All| + <" -u NONE -i NONE --cmd "set noruler" -D 1,1 All| | ]]) command([[call chansend(g:id, "cont\n")]])