From 5b6477be45c54ebac4dce6bda51028542167fd1f Mon Sep 17 00:00:00 2001 From: luukvbaal Date: Tue, 28 May 2024 11:43:56 +0200 Subject: fix(ui): flush ext_cmdline events before doing cmdpreview #27950 Problem: Unable to update the screen for external cmdline during cmdpreview. Solution: Flush the cmdline UI before cmdpreview state. --- src/nvim/ex_getln.c | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'src/nvim/ex_getln.c') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index f18dc0f747..cc2608433d 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -2532,6 +2532,10 @@ static bool cmdpreview_may_show(CommandLineState *s) goto end; } + // Flush now: external cmdline may itself wish to update the screen which is + // currently disallowed during cmdpreview(no longer needed in case that changes). + cmdline_ui_flush(); + // Swap invalid command range if needed if ((ea.argt & EX_RANGE) && ea.line1 > ea.line2) { linenr_T lnum = ea.line1; -- cgit From bb6190bec5f18c1f9e2c1d29ef1f7cf7912ea625 Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sat, 1 Jun 2024 08:19:41 -0700 Subject: refactor: move shared messages to errors.h #26214 --- src/nvim/ex_getln.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/nvim/ex_getln.c') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index cc2608433d..4323a9d221 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -28,6 +28,7 @@ #include "nvim/digraph.h" #include "nvim/drawscreen.h" #include "nvim/edit.h" +#include "nvim/errors.h" #include "nvim/eval.h" #include "nvim/eval/typval.h" #include "nvim/eval/vars.h" -- cgit From b66106a46c5c6180c7f80852a8c822b400e73100 Mon Sep 17 00:00:00 2001 From: luukvbaal Date: Tue, 4 Jun 2024 15:09:12 +0200 Subject: fix(ui): superfluous showmode / excessive grid_cursor_goto #29089 Problem: Unsetting global variables earlier in #28578 to avoid recursiveness, caused superfluous or even unlimited showmode(). Solution: Partly revert #28578 so that the globals are unset at the end of showmode(), and avoid recursiveness for ext UI by adding a recursive function guard to each generated UI call that may call a Lua callback. --- src/nvim/ex_getln.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src/nvim/ex_getln.c') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 4323a9d221..cef1868fc8 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -3455,11 +3455,9 @@ void cmdline_screen_cleared(void) /// called by ui_flush, do what redraws necessary to keep cmdline updated. void cmdline_ui_flush(void) { - static bool flushing = false; - if (!ui_has(kUICmdline) || flushing) { + if (!ui_has(kUICmdline)) { return; } - flushing = true; int level = ccline.level; CmdlineInfo *line = &ccline; while (level > 0 && line) { @@ -3474,7 +3472,6 @@ void cmdline_ui_flush(void) } line = line->prev_ccline; } - flushing = false; } // Put a character on the command line. Shifts the following text to the -- cgit From c37695a5d5f2e8914fff86f3581bed70b4c85d3c Mon Sep 17 00:00:00 2001 From: James <89495599+IAKOBVS@users.noreply.github.com> Date: Tue, 11 Jun 2024 22:40:24 +0700 Subject: refactor: use S_LEN(s) instead of s, n (#29219) --- src/nvim/ex_getln.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/nvim/ex_getln.c') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index cef1868fc8..74e6e3422e 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -2799,13 +2799,13 @@ int check_opt_wim(void) if (p[i] != NUL && p[i] != ',' && p[i] != ':') { return FAIL; } - if (i == 7 && strncmp(p, "longest", 7) == 0) { + if (i == 7 && strncmp(p, S_LEN("longest")) == 0) { new_wim_flags[idx] |= WIM_LONGEST; - } else if (i == 4 && strncmp(p, "full", 4) == 0) { + } else if (i == 4 && strncmp(p, S_LEN("full")) == 0) { new_wim_flags[idx] |= WIM_FULL; - } else if (i == 4 && strncmp(p, "list", 4) == 0) { + } else if (i == 4 && strncmp(p, S_LEN("list")) == 0) { new_wim_flags[idx] |= WIM_LIST; - } else if (i == 8 && strncmp(p, "lastused", 8) == 0) { + } else if (i == 8 && strncmp(p, S_LEN("lastused")) == 0) { new_wim_flags[idx] |= WIM_BUFLASTUSED; } else { return FAIL; -- cgit From 43d8435cf84468e776e0a6a98d05ae7bd62583b7 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Thu, 13 Jun 2024 22:20:06 +0100 Subject: revert: "refactor: use S_LEN macro" (#29319) revert: "refactor: use S_LEN(s) instead of s, n (#29219)" This reverts commit c37695a5d5f2e8914fff86f3581bed70b4c85d3c. --- src/nvim/ex_getln.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/nvim/ex_getln.c') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 74e6e3422e..cef1868fc8 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -2799,13 +2799,13 @@ int check_opt_wim(void) if (p[i] != NUL && p[i] != ',' && p[i] != ':') { return FAIL; } - if (i == 7 && strncmp(p, S_LEN("longest")) == 0) { + if (i == 7 && strncmp(p, "longest", 7) == 0) { new_wim_flags[idx] |= WIM_LONGEST; - } else if (i == 4 && strncmp(p, S_LEN("full")) == 0) { + } else if (i == 4 && strncmp(p, "full", 4) == 0) { new_wim_flags[idx] |= WIM_FULL; - } else if (i == 4 && strncmp(p, S_LEN("list")) == 0) { + } else if (i == 4 && strncmp(p, "list", 4) == 0) { new_wim_flags[idx] |= WIM_LIST; - } else if (i == 8 && strncmp(p, S_LEN("lastused")) == 0) { + } else if (i == 8 && strncmp(p, "lastused", 8) == 0) { new_wim_flags[idx] |= WIM_BUFLASTUSED; } else { return FAIL; -- cgit From 4e8ec4900eb9fdc2a864e65d3de73c51e963abd0 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 19 Jun 2024 05:47:25 +0800 Subject: vim-patch:9.1.0498: getcmdcompltype() interferes with cmdline completion (#29397) Problem: getcmdcompltype() interferes with cmdline completion. Solution: Don't set expand context when it's already set. (zeertzjq) closes: vim/vim#15036 https://github.com/vim/vim/commit/a821b609f9bb9daef032fe1cb8fb95995822e367 --- src/nvim/ex_getln.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'src/nvim/ex_getln.c') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index cef1868fc8..588f0aea13 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -4072,18 +4072,22 @@ static char *get_cmdline_completion(void) return NULL; } - set_expand_context(p->xpc); - if (p->xpc->xp_context == EXPAND_UNSUCCESSFUL) { + int xp_context = p->xpc->xp_context; + if (xp_context == EXPAND_NOTHING) { + set_expand_context(p->xpc); + xp_context = p->xpc->xp_context; + p->xpc->xp_context = EXPAND_NOTHING; + } + if (xp_context == EXPAND_UNSUCCESSFUL) { return NULL; } - char *cmd_compl = get_user_cmd_complete(p->xpc, p->xpc->xp_context); + char *cmd_compl = get_user_cmd_complete(NULL, xp_context); if (cmd_compl == NULL) { return NULL; } - if (p->xpc->xp_context == EXPAND_USER_LIST - || p->xpc->xp_context == EXPAND_USER_DEFINED) { + if (xp_context == EXPAND_USER_LIST || xp_context == EXPAND_USER_DEFINED) { size_t buflen = strlen(cmd_compl) + strlen(p->xpc->xp_arg) + 2; char *buffer = xmalloc(buflen); snprintf(buffer, buflen, "%s,%s", cmd_compl, p->xpc->xp_arg); -- cgit From 86ea42ce265a5a9df2843b04e8036268593825b9 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 21 Jun 2024 06:29:13 +0800 Subject: vim-patch:9.1.0507: hard to detect cursor movement in the command line Problem: hard to detect cursor movement in the command line Solution: Add the CursorMovedC autocommand (Shougo Matsushita) closes: vim/vim#15040 https://github.com/vim/vim/commit/d09521476f41dd8dbddb25b7acd0b299f9bf94d3 Co-authored-by: Shougo Matsushita --- src/nvim/ex_getln.c | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) (limited to 'src/nvim/ex_getln.c') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 588f0aea13..8e0378dd68 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -133,7 +133,8 @@ typedef struct { int did_wild_list; // did wild_list() recently int wim_index; // index in wim_flags[] int save_msg_scroll; - int save_State; // remember State when called + int save_State; // remember State when called + int save_cmdspos; char *save_p_icm; int some_key_typed; // one of the keys was typed // mouse drag and release events are ignored, unless they are @@ -223,6 +224,12 @@ static int cmdpreview_ns = 0; static const char e_active_window_or_buffer_changed_or_deleted[] = N_("E199: Active window or buffer changed or deleted"); +static void trigger_cmd_autocmd(int typechar, event_T evt) +{ + char typestr[2] = { (char)typechar, NUL }; + apply_autocmds(evt, typestr, typestr, false, curbuf); +} + static void save_viewstate(win_T *wp, viewstate_T *vs) FUNC_ATTR_NONNULL_ALL { @@ -684,6 +691,7 @@ static uint8_t *command_line_enter(int firstc, int count, int indent, bool clear .indent = indent, .save_msg_scroll = msg_scroll, .save_State = State, + .save_cmdspos = ccline.cmdspos, .ignore_drag_release = true, }; CommandLineState *s = &state; @@ -2175,6 +2183,11 @@ static int command_line_handle_key(CommandLineState *s) static int command_line_not_changed(CommandLineState *s) { + // Trigger CursorMovedC autocommands. + if (ccline.cmdspos != s->save_cmdspos) { + trigger_cmd_autocmd(get_cmdline_type(), EVENT_CURSORMOVEDC); + } + // Incremental searches for "/" and "?": // Enter command_line_not_changed() when a character has been read but the // command line did not change. Then we only search and redraw if something @@ -4177,6 +4190,10 @@ static int set_cmdline_pos(int pos) } else { new_cmdpos = pos; } + + // Trigger CursorMovedC autocommands. + trigger_cmd_autocmd(get_cmdline_type(), EVENT_CURSORMOVEDC); + return 0; } @@ -4303,7 +4320,6 @@ static int open_cmdwin(void) win_T *old_curwin = curwin; int i; garray_T winsizes; - char typestr[2]; int save_restart_edit = restart_edit; int save_State = State; bool save_exmode = exmode_active; @@ -4446,9 +4462,7 @@ static int open_cmdwin(void) cmdwin_result = 0; // Trigger CmdwinEnter autocommands. - typestr[0] = (char)cmdwin_type; - typestr[1] = NUL; - apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, false, curbuf); + trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER); if (restart_edit != 0) { // autocmd with ":startinsert" stuffcharReadbuff(K_NOP); } @@ -4466,7 +4480,7 @@ static int open_cmdwin(void) const bool save_KeyTyped = KeyTyped; // Trigger CmdwinLeave autocommands. - apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, false, curbuf); + trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE); // Restore KeyTyped in case it is modified by autocommands KeyTyped = save_KeyTyped; -- cgit From f45403db19d888266d1cc04756bfef20a26f9f7f Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Fri, 21 Jun 2024 14:11:05 +0800 Subject: vim-patch:9.1.0511: CursorMovedC triggered wrongly with setcmdpos() Problem: CursorMovedC triggered wrongly with setcmdpos() (after v9.1.0507) Solution: Remove the premature triggering. Also don't trigger when cursor didn't move. (zeertzjq) closes: vim/vim#15064 https://github.com/vim/vim/commit/bc6f96708e3678dbb27ec4192d87cf94a15d4e9a --- src/nvim/ex_getln.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) (limited to 'src/nvim/ex_getln.c') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 8e0378dd68..2e63a139c3 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -134,7 +134,7 @@ typedef struct { int wim_index; // index in wim_flags[] int save_msg_scroll; int save_State; // remember State when called - int save_cmdspos; + int prev_cmdpos; char *save_p_icm; int some_key_typed; // one of the keys was typed // mouse drag and release events are ignored, unless they are @@ -691,7 +691,7 @@ static uint8_t *command_line_enter(int firstc, int count, int indent, bool clear .indent = indent, .save_msg_scroll = msg_scroll, .save_State = State, - .save_cmdspos = ccline.cmdspos, + .prev_cmdpos = -1, .ignore_drag_release = true, }; CommandLineState *s = &state; @@ -2184,10 +2184,10 @@ static int command_line_handle_key(CommandLineState *s) static int command_line_not_changed(CommandLineState *s) { // Trigger CursorMovedC autocommands. - if (ccline.cmdspos != s->save_cmdspos) { + if (ccline.cmdpos != s->prev_cmdpos) { trigger_cmd_autocmd(get_cmdline_type(), EVENT_CURSORMOVEDC); + s->prev_cmdpos = ccline.cmdpos; } - // Incremental searches for "/" and "?": // Enter command_line_not_changed() when a character has been read but the // command line did not change. Then we only search and redraw if something @@ -2662,6 +2662,7 @@ static void do_autocmd_cmdlinechanged(int firstc) static int command_line_changed(CommandLineState *s) { + s->prev_cmdpos = ccline.cmdpos; // Trigger CmdlineChanged autocommands. do_autocmd_cmdlinechanged(s->firstc > 0 ? s->firstc : '-'); @@ -4191,9 +4192,6 @@ static int set_cmdline_pos(int pos) new_cmdpos = pos; } - // Trigger CursorMovedC autocommands. - trigger_cmd_autocmd(get_cmdline_type(), EVENT_CURSORMOVEDC); - return 0; } -- cgit From 76b91106fc836ae634cdf8425d121d843d14d70a Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 8 Jul 2024 06:55:21 +0800 Subject: vim-patch:9.1.0543: Behavior of CursorMovedC is strange (#29608) Problem: Behavior of CursorMovedC is strange. Solution: Also trigger when the cmdline has changed. (zeertzjq) fixes: vim/vim#15069 closes: vim/vim#15071 https://github.com/vim/vim/commit/8145620a958dbb5c82cf8f8a37556ee1ea501c6d --- src/nvim/ex_getln.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'src/nvim/ex_getln.c') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 2e63a139c3..e3c7008d3b 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -2188,6 +2188,7 @@ static int command_line_not_changed(CommandLineState *s) trigger_cmd_autocmd(get_cmdline_type(), EVENT_CURSORMOVEDC); s->prev_cmdpos = ccline.cmdpos; } + // Incremental searches for "/" and "?": // Enter command_line_not_changed() when a character has been read but the // command line did not change. Then we only search and redraw if something @@ -2662,10 +2663,15 @@ static void do_autocmd_cmdlinechanged(int firstc) static int command_line_changed(CommandLineState *s) { - s->prev_cmdpos = ccline.cmdpos; // Trigger CmdlineChanged autocommands. do_autocmd_cmdlinechanged(s->firstc > 0 ? s->firstc : '-'); + // Trigger CursorMovedC autocommands. + if (ccline.cmdpos != s->prev_cmdpos) { + trigger_cmd_autocmd(get_cmdline_type(), EVENT_CURSORMOVEDC); + s->prev_cmdpos = ccline.cmdpos; + } + const bool prev_cmdpreview = cmdpreview; if (s->firstc == ':' && current_sctx.sc_sid == 0 // only if interactive -- cgit From d1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Fri, 19 Jul 2024 11:00:13 +0100 Subject: refactor: collapse statements in single assignments Problem: Variables are often assigned multiple places in common patterns. Solution: Replace these common patterns with different patterns that reduce the number of assignments. Use `MAX` and `MIN`: ```c if (x < y) { x = y; } // --> x = MAX(x, y); ``` ```c if (x > y) { x = y; } // --> x = MIN(x, y); ``` Use ternary: ```c int a; if (cond) { a = b; } els { a = c; } // --> int a = cond ? b : c; ``` --- src/nvim/ex_getln.c | 57 +++++++++++++++-------------------------------------- 1 file changed, 16 insertions(+), 41 deletions(-) (limited to 'src/nvim/ex_getln.c') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index e3c7008d3b..c8ca6f0e4e 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -390,13 +390,8 @@ static bool do_incsearch_highlighting(int firstc, int *search_delim, incsearch_s parse_cmd_address(&ea, &dummy, true); if (ea.addr_count > 0) { // Allow for reverse match. - if (ea.line2 < ea.line1) { - search_first_line = ea.line2; - search_last_line = ea.line1; - } else { - search_first_line = ea.line1; - search_last_line = ea.line2; - } + search_first_line = MIN(ea.line1, ea.line1); + search_last_line = MAX(ea.line2, ea.line1); } else if (cmd[0] == 's' && cmd[1] != 'o') { // :s defaults to the current line search_first_line = curwin->w_cursor.lnum; @@ -1034,11 +1029,7 @@ static int command_line_handle_ctrl_bsl(CommandLineState *s) // Restore the cursor or use the position set with // set_cmdline_pos(). - if (new_cmdpos > ccline.cmdlen) { - ccline.cmdpos = ccline.cmdlen; - } else { - ccline.cmdpos = new_cmdpos; - } + ccline.cmdpos = MIN(ccline.cmdlen, new_cmdpos); KeyTyped = false; // Don't do p_wc completion. redrawcmd(); @@ -1658,11 +1649,7 @@ static int command_line_insert_reg(CommandLineState *s) KeyTyped = false; // Don't do p_wc completion. if (new_cmdpos >= 0) { // set_cmdline_pos() was used - if (new_cmdpos > ccline.cmdlen) { - ccline.cmdpos = ccline.cmdlen; - } else { - ccline.cmdpos = new_cmdpos; - } + ccline.cmdpos = MIN(ccline.cmdlen, new_cmdpos); } } new_cmdpos = save_new_cmdpos; @@ -3546,10 +3533,6 @@ void unputcmdline(void) // called afterwards. void put_on_cmdline(const char *str, int len, bool redraw) { - int i; - int m; - int c; - if (len < 0) { len = (int)strlen(str); } @@ -3563,7 +3546,8 @@ void put_on_cmdline(const char *str, int len, bool redraw) ccline.cmdlen += len; } else { // Count nr of characters in the new string. - m = 0; + int m = 0; + int i; for (i = 0; i < len; i += utfc_ptr2len(str + i)) { m++; } @@ -3587,8 +3571,8 @@ void put_on_cmdline(const char *str, int len, bool redraw) { // When the inserted text starts with a composing character, // backup to the character before it. There could be two of them. - i = 0; - c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); + int i = 0; + int c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); while (ccline.cmdpos > 0 && utf_iscomposing(c)) { i = utf_head_off(ccline.cmdbuff, ccline.cmdbuff + ccline.cmdpos - 1) + 1; ccline.cmdpos -= i; @@ -3619,7 +3603,7 @@ void put_on_cmdline(const char *str, int len, bool redraw) if (redraw && !cmd_silent) { msg_no_more = true; - i = cmdline_row; + int i = cmdline_row; cursorcmd(); draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); // Avoid clearing the rest of the line too often. @@ -3628,6 +3612,7 @@ void put_on_cmdline(const char *str, int len, bool redraw) } msg_no_more = false; } + int m; if (KeyTyped) { m = Columns * Rows; if (m < 0) { // overflow, Columns or Rows at weird value @@ -3636,8 +3621,8 @@ void put_on_cmdline(const char *str, int len, bool redraw) } else { m = MAXCOL; } - for (i = 0; i < len; i++) { - c = cmdline_charsize(ccline.cmdpos); + for (int i = 0; i < len; i++) { + int c = cmdline_charsize(ccline.cmdpos); // count ">" for a double-wide char that doesn't fit. correct_screencol(ccline.cmdpos, c, &ccline.cmdspos); // Stop cursor at the end of the screen, but do increment the @@ -3647,9 +3632,7 @@ void put_on_cmdline(const char *str, int len, bool redraw) ccline.cmdspos += c; } c = utfc_ptr2len(ccline.cmdbuff + ccline.cmdpos) - 1; - if (c > len - i - 1) { - c = len - i - 1; - } + c = MIN(c, len - i - 1); ccline.cmdpos += c; i += c; ccline.cmdpos++; @@ -3886,17 +3869,13 @@ void cursorcmd(void) } if (ui_has(kUICmdline)) { - if (ccline.redraw_state < kCmdRedrawPos) { - ccline.redraw_state = kCmdRedrawPos; - } + ccline.redraw_state = MAX(ccline.redraw_state, kCmdRedrawPos); return; } msg_row = cmdline_row + (ccline.cmdspos / Columns); msg_col = ccline.cmdspos % Columns; - if (msg_row >= Rows) { - msg_row = Rows - 1; - } + msg_row = MIN(msg_row, Rows - 1); msg_cursor_goto(msg_row, msg_col); } @@ -4192,11 +4171,7 @@ static int set_cmdline_pos(int pos) // The position is not set directly but after CTRL-\ e or CTRL-R = has // changed the command line. - if (pos < 0) { - new_cmdpos = 0; - } else { - new_cmdpos = pos; - } + new_cmdpos = MAX(0, pos); return 0; } -- cgit From 383f6934720a203d093c762cbd5362092110f35f Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 3 Aug 2024 07:42:52 +0800 Subject: refactor: move some functions out of eval.c (#29964) - common_function() has always been in evalfunc.c in Vim - return_register() has always been in evalfunc.c in Vim - get_user_input() was moved to ex_getln.c in Vim 8.1.1957 - tv_get_lnum_buf() was moved to typval.c in Vim 8.2.0847 --- src/nvim/ex_getln.c | 146 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 138 insertions(+), 8 deletions(-) (limited to 'src/nvim/ex_getln.c') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index c8ca6f0e4e..8a34e03d91 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -267,6 +267,18 @@ static void init_incsearch_state(incsearch_state_T *s) save_viewstate(curwin, &s->old_viewstate); } +static void set_search_match(pos_T *t) +{ + // First move cursor to end of match, then to the start. This + // moves the whole match onto the screen when 'nowrap' is set. + t->lnum += search_match_lines; + t->col = search_match_endcol; + if (t->lnum > curbuf->b_ml.ml_line_count) { + t->lnum = curbuf->b_ml.ml_line_count; + coladvance(curwin, MAXCOL); + } +} + // Return true when 'incsearch' highlighting is to be done. // Sets search_first_line and search_last_line to the address range. static bool do_incsearch_highlighting(int firstc, int *search_delim, incsearch_state_T *s, @@ -4622,14 +4634,132 @@ char *script_get(exarg_T *const eap, size_t *const lenp) return (char *)ga.ga_data; } -static void set_search_match(pos_T *t) +/// This function is used by f_input() and f_inputdialog() functions. The third +/// argument to f_input() specifies the type of completion to use at the +/// prompt. The third argument to f_inputdialog() specifies the value to return +/// when the user cancels the prompt. +void get_user_input(const typval_T *const argvars, typval_T *const rettv, const bool inputdialog, + const bool secret) + FUNC_ATTR_NONNULL_ALL { - // First move cursor to end of match, then to the start. This - // moves the whole match onto the screen when 'nowrap' is set. - t->lnum += search_match_lines; - t->col = search_match_endcol; - if (t->lnum > curbuf->b_ml.ml_line_count) { - t->lnum = curbuf->b_ml.ml_line_count; - coladvance(curwin, MAXCOL); + rettv->v_type = VAR_STRING; + rettv->vval.v_string = NULL; + + const char *prompt; + const char *defstr = ""; + typval_T *cancelreturn = NULL; + typval_T cancelreturn_strarg2 = TV_INITIAL_VALUE; + const char *xp_name = NULL; + Callback input_callback = { .type = kCallbackNone }; + char prompt_buf[NUMBUFLEN]; + char defstr_buf[NUMBUFLEN]; + char cancelreturn_buf[NUMBUFLEN]; + char xp_name_buf[NUMBUFLEN]; + char def[1] = { 0 }; + if (argvars[0].v_type == VAR_DICT) { + if (argvars[1].v_type != VAR_UNKNOWN) { + emsg(_("E5050: {opts} must be the only argument")); + return; + } + dict_T *const dict = argvars[0].vval.v_dict; + prompt = tv_dict_get_string_buf_chk(dict, S_LEN("prompt"), prompt_buf, ""); + if (prompt == NULL) { + return; + } + defstr = tv_dict_get_string_buf_chk(dict, S_LEN("default"), defstr_buf, ""); + if (defstr == NULL) { + return; + } + dictitem_T *cancelreturn_di = tv_dict_find(dict, S_LEN("cancelreturn")); + if (cancelreturn_di != NULL) { + cancelreturn = &cancelreturn_di->di_tv; + } + xp_name = tv_dict_get_string_buf_chk(dict, S_LEN("completion"), + xp_name_buf, def); + if (xp_name == NULL) { // error + return; + } + if (xp_name == def) { // default to NULL + xp_name = NULL; + } + if (!tv_dict_get_callback(dict, S_LEN("highlight"), &input_callback)) { + return; + } + } else { + prompt = tv_get_string_buf_chk(&argvars[0], prompt_buf); + if (prompt == NULL) { + return; + } + if (argvars[1].v_type != VAR_UNKNOWN) { + defstr = tv_get_string_buf_chk(&argvars[1], defstr_buf); + if (defstr == NULL) { + return; + } + if (argvars[2].v_type != VAR_UNKNOWN) { + const char *const strarg2 = tv_get_string_buf_chk(&argvars[2], cancelreturn_buf); + if (strarg2 == NULL) { + return; + } + if (inputdialog) { + cancelreturn_strarg2.v_type = VAR_STRING; + cancelreturn_strarg2.vval.v_string = (char *)strarg2; + cancelreturn = &cancelreturn_strarg2; + } else { + xp_name = strarg2; + } + } + } + } + + int xp_type = EXPAND_NOTHING; + char *xp_arg = NULL; + if (xp_name != NULL) { + // input() with a third argument: completion + const int xp_namelen = (int)strlen(xp_name); + + uint32_t argt = 0; + if (parse_compl_arg(xp_name, xp_namelen, &xp_type, + &argt, &xp_arg) == FAIL) { + return; + } + } + + const bool cmd_silent_save = cmd_silent; + + cmd_silent = false; // Want to see the prompt. + // Only the part of the message after the last NL is considered as + // prompt for the command line, unlsess cmdline is externalized + const char *p = prompt; + if (!ui_has(kUICmdline)) { + const char *lastnl = strrchr(prompt, '\n'); + if (lastnl != NULL) { + p = lastnl + 1; + msg_start(); + msg_clr_eos(); + msg_puts_len(prompt, p - prompt, get_echo_attr()); + msg_didout = false; + msg_starthere(); + } } + cmdline_row = msg_row; + + stuffReadbuffSpec(defstr); + + const int save_ex_normal_busy = ex_normal_busy; + ex_normal_busy = 0; + rettv->vval.v_string = getcmdline_prompt(secret ? NUL : '@', p, get_echo_attr(), + xp_type, xp_arg, input_callback); + ex_normal_busy = save_ex_normal_busy; + callback_free(&input_callback); + + if (rettv->vval.v_string == NULL && cancelreturn != NULL) { + tv_copy(cancelreturn, rettv); + } + + xfree(xp_arg); + + // Since the user typed this, no need to wait for return. + need_wait_return = false; + msg_didout = false; + cmd_silent = cmd_silent_save; } -- cgit From cfdf68a7acde16597fbd896674af68c42361102c Mon Sep 17 00:00:00 2001 From: bfredl Date: Thu, 8 Aug 2024 10:42:08 +0200 Subject: feat(mbyte): support extended grapheme clusters including more emoji Use the grapheme break algorithm from utf8proc to support grapheme clusters from recent unicode versions. Handle variant selector VS16 turning some codepoints into double-width emoji. This means we need to use ptr2cells rather than char2cells when possible. --- src/nvim/ex_getln.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src/nvim/ex_getln.c') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 8a34e03d91..722a857f03 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -2118,7 +2118,7 @@ static int command_line_handle_key(CommandLineState *s) s->do_abbr = false; // don't do abbreviation now ccline.special_char = NUL; // may need to remove ^ when composing char was typed - if (utf_iscomposing(s->c) && !cmd_silent) { + if (utf_iscomposing_first(s->c) && !cmd_silent) { if (ui_has(kUICmdline)) { // TODO(bfredl): why not make unputcmdline also work with true? unputcmdline(); @@ -3585,7 +3585,9 @@ void put_on_cmdline(const char *str, int len, bool redraw) // backup to the character before it. There could be two of them. int i = 0; int c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); - while (ccline.cmdpos > 0 && utf_iscomposing(c)) { + // TODO(bfredl): this can be corrected/simplified as utf_head_off implements the + // correct grapheme cluster breaks + while (ccline.cmdpos > 0 && utf_iscomposing_legacy(c)) { i = utf_head_off(ccline.cmdbuff, ccline.cmdbuff + ccline.cmdpos - 1) + 1; ccline.cmdpos -= i; len += i; -- cgit From d8cd15e8d67d76a327b5ffa75d54a23dc05b58c9 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sat, 21 Sep 2024 18:26:44 +0800 Subject: vim-patch:9.1.0739: [security]: use-after-free in ex_getln.c (#30448) Problem: [security]: use-after-free in ex_getln.c Solution: free pointer p a bit later (John Marriott) closes: vim/vim#15712 https://github.com/vim/vim/commit/a6de28755ec3fcc86d1ed0b744f1b410a8e9702d Co-authored-by: John Marriott --- src/nvim/ex_getln.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/nvim/ex_getln.c') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 722a857f03..6a57b3ab78 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -3028,7 +3028,6 @@ void realloc_cmdbuff(int len) // there, thus copy up to the NUL and add a NUL. memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen); ccline.cmdbuff[ccline.cmdlen] = NUL; - xfree(p); if (ccline.xpc != NULL && ccline.xpc->xp_pattern != NULL @@ -3042,6 +3041,8 @@ void realloc_cmdbuff(int len) ccline.xpc->xp_pattern = ccline.cmdbuff + i; } } + + xfree(p); } enum { MAX_CB_ERRORS = 1, }; -- cgit From c2fb1fc700db28cb554be9da8e79443b5d3a5fe9 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 24 Sep 2024 06:51:02 +0800 Subject: vim-patch:9.1.0741: No way to get prompt for input()/confirm() Problem: No way to get prompt for input()/confirm() Solution: add getcmdprompt() function (Shougo Matsushita) (Shougo Matsushita) closes: vim/vim#15667 https://github.com/vim/vim/commit/6908428560a0d6ae27bf7af6fcb6dc362e31926c Co-authored-by: Shougo Matsushita --- src/nvim/ex_getln.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'src/nvim/ex_getln.c') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 6a57b3ab78..7eb4fe67a1 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -4132,6 +4132,28 @@ void f_getcmdpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) rettv->vval.v_number = p != NULL ? p->cmdpos + 1 : 0; } +static char current_prompt[CMDBUFFSIZE + 1] = ""; + +/// Get current command line prompt. +static char *get_prompt(void) +{ + return current_prompt; +} + +/// Set current command line prompt. +void set_prompt(const char *str) +{ + xstrlcpy(current_prompt, str, sizeof(current_prompt)); +} + +/// "getcmdprompt()" function +void f_getcmdprompt(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) +{ + CmdlineInfo *p = get_ccline_ptr(); + rettv->v_type = VAR_STRING; + rettv->vval.v_string = p != NULL ? xstrdup(get_prompt()) : NULL; +} + /// "getcmdscreenpos()" function void f_getcmdscreenpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { @@ -4730,6 +4752,8 @@ void get_user_input(const typval_T *const argvars, typval_T *const rettv, const const bool cmd_silent_save = cmd_silent; cmd_silent = false; // Want to see the prompt. + set_prompt(prompt); + // Only the part of the message after the last NL is considered as // prompt for the command line, unlsess cmdline is externalized const char *p = prompt; -- cgit From 65b6cd1b3a43024f6c2d9d6f31b8ae3a69dabb56 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 26 Sep 2024 22:15:54 +0800 Subject: vim-patch:9.1.0742: getcmdprompt() implementation can be improved Problem: getcmdprompt() implementation can be improved Solution: Improve and simplify it (h-east) closes: vim/vim#15743 https://github.com/vim/vim/commit/25876a6cdd439054d0b3f920ccca0a435481de15 Co-authored-by: h-east --- src/nvim/ex_getln.c | 19 ++----------------- 1 file changed, 2 insertions(+), 17 deletions(-) (limited to 'src/nvim/ex_getln.c') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 7eb4fe67a1..69fcdac095 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -4132,26 +4132,13 @@ void f_getcmdpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) rettv->vval.v_number = p != NULL ? p->cmdpos + 1 : 0; } -static char current_prompt[CMDBUFFSIZE + 1] = ""; - -/// Get current command line prompt. -static char *get_prompt(void) -{ - return current_prompt; -} - -/// Set current command line prompt. -void set_prompt(const char *str) -{ - xstrlcpy(current_prompt, str, sizeof(current_prompt)); -} - /// "getcmdprompt()" function void f_getcmdprompt(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) { CmdlineInfo *p = get_ccline_ptr(); rettv->v_type = VAR_STRING; - rettv->vval.v_string = p != NULL ? xstrdup(get_prompt()) : NULL; + rettv->vval.v_string = p != NULL && p->cmdprompt != NULL + ? xstrdup(p->cmdprompt) : NULL; } /// "getcmdscreenpos()" function @@ -4752,8 +4739,6 @@ void get_user_input(const typval_T *const argvars, typval_T *const rettv, const const bool cmd_silent_save = cmd_silent; cmd_silent = false; // Want to see the prompt. - set_prompt(prompt); - // Only the part of the message after the last NL is considered as // prompt for the command line, unlsess cmdline is externalized const char *p = prompt; -- cgit From 2168d772b864fd05109fb4299e409d4bdc1df39d Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Wed, 2 Oct 2024 07:39:28 +0800 Subject: vim-patch:9.1.0752: can set 'cedit' to an invalid value (#30616) Problem: can set cedit to an invalid value Solution: Check that the value is a valid key name (Milly) closes: vim/vim#15778 https://github.com/vim/vim/commit/25732435c56d762abb260499680939bd8882c708 Co-authored-by: Milly --- src/nvim/ex_getln.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/ex_getln.c') diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 69fcdac095..7d87e609ca 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -4302,7 +4302,7 @@ const char *did_set_cedit(optset_T *args) cedit_key = -1; } else { int n = string_to_key(p_cedit); - if (vim_isprintc(n)) { + if (n == 0 || vim_isprintc(n)) { return e_invarg; } cedit_key = n; -- cgit