From 818456470c0ce0d463b0be82e9c35eb77cc65f19 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 23 Jan 2022 05:58:32 +0800 Subject: fix(input): put modifiers back into typeahead buffer when needed --- src/nvim/terminal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 70a5c7aa08..a2d855244c 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -1299,7 +1299,7 @@ static bool send_mouse_event(Terminal *term, int c) } end: - ins_char_typebuf(c); + ins_char_typebuf(c, mod_mask); return true; } -- cgit From 7813b48645bf2af11c2d18f4e4154a74d4dad662 Mon Sep 17 00:00:00 2001 From: Andreas Schneider Date: Tue, 8 Feb 2022 10:50:17 +0100 Subject: feat(term): use vterm_output_set_callback() --- src/nvim/terminal.c | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index a2d855244c..1c26e46a21 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -172,6 +172,11 @@ void terminal_teardown(void) pmap_init(ptr_t, &invalidated_terminals); } +static void term_output_callback(const char *s, size_t len, void *user_data) +{ + terminal_send((Terminal *)user_data, (char *)s, len); +} + // public API {{{ Terminal *terminal_open(buf_T *buf, TerminalOptions opts) @@ -195,6 +200,7 @@ Terminal *terminal_open(buf_T *buf, TerminalOptions opts) vterm_screen_set_callbacks(rv->vts, &vterm_screen_callbacks, rv); vterm_screen_set_damage_merge(rv->vts, VTERM_DAMAGE_SCROLL); vterm_screen_reset(rv->vts, 1); + vterm_output_set_callback(rv->vt, term_output_callback, rv); // force a initial refresh of the screen to ensure the buffer will always // have as many lines as screen rows when refresh_scrollback is called rv->invalid_start = 0; @@ -636,7 +642,6 @@ void terminal_paste(long count, char_u **y_array, size_t y_size) return; } vterm_keyboard_start_paste(curbuf->terminal->vt); - terminal_flush_output(curbuf->terminal); size_t buff_len = STRLEN(y_array[0]); char_u *buff = xmalloc(buff_len); for (int i = 0; i < count; i++) { // -V756 @@ -667,14 +672,6 @@ void terminal_paste(long count, char_u **y_array, size_t y_size) } xfree(buff); vterm_keyboard_end_paste(curbuf->terminal->vt); - terminal_flush_output(curbuf->terminal); -} - -void terminal_flush_output(Terminal *term) -{ - size_t len = vterm_output_read(term->vt, term->textbuf, - sizeof(term->textbuf)); - terminal_send(term, term->textbuf, len); } void terminal_send_key(Terminal *term, int c) @@ -693,8 +690,6 @@ void terminal_send_key(Terminal *term, int c) } else { vterm_keyboard_unichar(term->vt, (uint32_t)c, mod); } - - terminal_flush_output(term); } void terminal_receive(Terminal *term, char *data, size_t len) @@ -1265,9 +1260,6 @@ static bool send_mouse_event(Terminal *term, int c) } mouse_action(term, button, row, col - offset, pressed, 0); - size_t len = vterm_output_read(term->vt, term->textbuf, - sizeof(term->textbuf)); - terminal_send(term, term->textbuf, len); return false; } -- cgit From 5051510ade5f171c1239906c8638e804356186fe Mon Sep 17 00:00:00 2001 From: erw7 Date: Fri, 12 Nov 2021 00:07:03 +0900 Subject: fix(channel): fix channel consistency - Fix the problem that chanclose() does not work for channel created by nvim_open_term(). - Fix the problem that the loopback channel is not released. - Fix the error message when sending raw data to the loopback channel. --- src/nvim/terminal.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 1c26e46a21..a76a806b80 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -317,10 +317,14 @@ void terminal_close(Terminal *term, int status) term->opts.close_cb(term->opts.data); } } else if (!only_destroy) { - // This was called by channel_process_exit_cb() not in process_teardown(). + // Associated channel has been closed and the editor is not exiting. // Do not call the close callback now. Wait for the user to press a key. char msg[sizeof("\r\n[Process exited ]") + NUMBUFLEN]; - snprintf(msg, sizeof msg, "\r\n[Process exited %d]", status); + if (((Channel *)term->opts.data)->streamtype == kChannelStreamInternal) { + snprintf(msg, sizeof msg, "\r\n[Terminal closed]"); + } else { + snprintf(msg, sizeof msg, "\r\n[Process exited %d]", status); + } terminal_receive(term, msg, strlen(msg)); } -- cgit From d238b8f6003d34cae7f65ff7585b48a2cd9449fb Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Thu, 17 Mar 2022 06:21:24 +0100 Subject: chore: fix typos (#17670) Co-authored-by: zeertzjq --- src/nvim/terminal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index a76a806b80..5189705a36 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -1288,7 +1288,7 @@ static bool send_mouse_event(Terminal *term, int c) return mouse_win == curwin; } - // ignore left release action if it was not proccessed above + // ignore left release action if it was not processed above // to prevent leaving Terminal mode after entering to it using a mouse if (c == K_LEFTRELEASE && mouse_win->w_buffer->terminal == term) { return false; -- cgit From 00effff56944d5b59440dcdb5e3496d49a76d3e2 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Fri, 18 Mar 2022 04:47:08 +0000 Subject: vim-patch:8.1.1693: syntax coloring and highlighting is in one big file (#17721) Problem: Syntax coloring and highlighting is in one big file. Solution: Move the highlighting to a separate file. (Yegappan Lakshmanan, closes vim/vim#4674) https://github.com/vim/vim/commit/f9cc9f209ede9f15959e4c2351e970477c139614 Name the new file highlight_group.c instead. Co-authored-by: zeertzjq --- src/nvim/terminal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 5189705a36..5c8789ec37 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -55,6 +55,7 @@ #include "nvim/fileio.h" #include "nvim/getchar.h" #include "nvim/highlight.h" +#include "nvim/highlight_group.h" #include "nvim/keymap.h" #include "nvim/log.h" #include "nvim/macros.h" @@ -70,7 +71,6 @@ #include "nvim/os/input.h" #include "nvim/screen.h" #include "nvim/state.h" -#include "nvim/syntax.h" #include "nvim/terminal.h" #include "nvim/ui.h" #include "nvim/vim.h" -- cgit From 263a7fde35f2341f526a536690122b927300021a Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Sun, 10 Apr 2022 07:20:35 +0800 Subject: vim-patch:8.2.4723: the ModeChanged autocmd event is inefficient Problem: The ModeChanged autocmd event is inefficient. Solution: Avoid allocating memory. (closes vim/vim#10134) Rename trigger_modechanged() to may_trigger_modechanged(). https://github.com/vim/vim/commit/2bf52dd065495cbf28e28792f2c2d50d44546d9f Make v:event readonly for ModeChanged. --- src/nvim/terminal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 5c8789ec37..f4baa92f5b 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -422,7 +422,7 @@ void terminal_enter(void) curwin->w_redr_status = true; // For mode() in statusline. #8323 ui_busy_start(); apply_autocmds(EVENT_TERMENTER, NULL, NULL, false, curbuf); - trigger_modechanged(); + may_trigger_modechanged(); s->state.execute = terminal_execute; s->state.check = terminal_check; -- cgit From 9da0023a666e83e6b9f777871553177473bfa9ce Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 11 Apr 2022 09:54:59 +0800 Subject: feat(keymap): add F38-F63 keys (#17893) --- src/nvim/terminal.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index f4baa92f5b..59e0d4da6c 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -1205,6 +1205,58 @@ static VTermKey convert_key(int key, VTermModifier *statep) return VTERM_KEY_FUNCTION(36); case K_F37: return VTERM_KEY_FUNCTION(37); + case K_F38: + return VTERM_KEY_FUNCTION(38); + case K_F39: + return VTERM_KEY_FUNCTION(39); + case K_F40: + return VTERM_KEY_FUNCTION(40); + case K_F41: + return VTERM_KEY_FUNCTION(41); + case K_F42: + return VTERM_KEY_FUNCTION(42); + case K_F43: + return VTERM_KEY_FUNCTION(43); + case K_F44: + return VTERM_KEY_FUNCTION(44); + case K_F45: + return VTERM_KEY_FUNCTION(45); + case K_F46: + return VTERM_KEY_FUNCTION(46); + case K_F47: + return VTERM_KEY_FUNCTION(47); + case K_F48: + return VTERM_KEY_FUNCTION(48); + case K_F49: + return VTERM_KEY_FUNCTION(49); + case K_F50: + return VTERM_KEY_FUNCTION(50); + case K_F51: + return VTERM_KEY_FUNCTION(51); + case K_F52: + return VTERM_KEY_FUNCTION(52); + case K_F53: + return VTERM_KEY_FUNCTION(53); + case K_F54: + return VTERM_KEY_FUNCTION(54); + case K_F55: + return VTERM_KEY_FUNCTION(55); + case K_F56: + return VTERM_KEY_FUNCTION(56); + case K_F57: + return VTERM_KEY_FUNCTION(57); + case K_F58: + return VTERM_KEY_FUNCTION(58); + case K_F59: + return VTERM_KEY_FUNCTION(59); + case K_F60: + return VTERM_KEY_FUNCTION(60); + case K_F61: + return VTERM_KEY_FUNCTION(61); + case K_F62: + return VTERM_KEY_FUNCTION(62); + case K_F63: + return VTERM_KEY_FUNCTION(63); default: return VTERM_KEY_NONE; -- cgit From f89ca7194f484108529cbb93ef61ce45a9a277b5 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 11 Apr 2022 11:32:15 +0800 Subject: fix(events): make v:event readonly in more events (#18070) This makes v:event readonly in these four events: - ChanInfo - ChanOpen - RecordingLeave - TermClose --- src/nvim/terminal.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 59e0d4da6c..fd870361c7 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -336,6 +336,7 @@ void terminal_close(Terminal *term, int status) save_v_event_T save_v_event; dict_T *dict = get_v_event(&save_v_event); tv_dict_add_nr(dict, S_LEN("status"), status); + tv_dict_set_keys_readonly(dict); apply_autocmds(EVENT_TERMCLOSE, NULL, NULL, false, buf); restore_v_event(dict, &save_v_event); } -- cgit From 66747f18ded775e2c0b6bac73cee18a3752086af Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Mon, 25 Apr 2022 21:40:46 +0800 Subject: vim-patch:8.2.0839: dropping modifier when putting a character back in typeahead Problem: Dropping modifier when putting a character back in typeahead. Solution: Add modifier to ins_char_typebuf(). (closes vim/vim#6158) https://github.com/vim/vim/commit/b42c0d54279b1fdb79652db0c84171e213458809 Vim's test doesn't seem to work properly as the hit-enter prompt seems to be delayed. Add a Lua screen test. --- src/nvim/terminal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index fd870361c7..5f536c0b7a 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -1348,7 +1348,7 @@ static bool send_mouse_event(Terminal *term, int c) } end: - ins_char_typebuf(c, mod_mask); + ins_char_typebuf(vgetc_char, vgetc_mod_mask); return true; } -- cgit From eef8de4df0247157e57f306062b1b86e01a41454 Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Fri, 29 Apr 2022 13:53:42 +0200 Subject: refactor(uncrustify): change rules to better align with the style guide Add space around arithmetic operators '+' and '-'. Remove space between back-to-back parentheses, i.e. ')(' vs. ') ('. Remove space between '((' or '))' of control statements. Add space between ')' and '{' of control statements. Remove space between function name and '(' on function declaration. Collapse empty blocks between '{' and '}'. Remove newline at the end of the file. Remove newline between 'enum' and '{'. Remove newline between '}' and ')' in a function invocation. Remove newline between '}' and 'while' of 'do' statement. --- src/nvim/terminal.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 5f536c0b7a..b07c3786c3 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -744,8 +744,8 @@ void terminal_get_line_attributes(Terminal *term, win_T *wp, int linenr, int *te int vt_fg_idx = ((!fg_default && fg_indexed) ? cell.fg.indexed.idx + 1 : 0); int vt_bg_idx = ((!bg_default && bg_indexed) ? cell.bg.indexed.idx + 1 : 0); - bool fg_set = vt_fg_idx && vt_fg_idx <= 16 && term->color_set[vt_fg_idx-1]; - bool bg_set = vt_bg_idx && vt_bg_idx <= 16 && term->color_set[vt_bg_idx-1]; + bool fg_set = vt_fg_idx && vt_fg_idx <= 16 && term->color_set[vt_fg_idx - 1]; + bool bg_set = vt_bg_idx && vt_bg_idx <= 16 && term->color_set[vt_bg_idx - 1]; int hl_attrs = (cell.attrs.bold ? HL_BOLD : 0) | (cell.attrs.italic ? HL_ITALIC : 0) -- cgit From 4fb48c5654d9ffbffbdcdd80d0498b1ea3c8e68b Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Tue, 3 May 2022 15:08:35 +0200 Subject: feat(server): set $NVIM, unset $NVIM_LISTEN_ADDRESS #11009 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PROBLEM ------------------------------------------------------------------------ $NVIM_LISTEN_ADDRESS has conflicting purposes as both a parameter ("the current process should listen on this address") and a descriptor ("the current process is a child of this address"). This contradiction means the presence of NVIM_LISTEN_ADDRESS is ambiguous, so child Nvim always tries to listen on its _parent's_ socket. This is the cause of lots of "Failed to start server" spam in our test/CI logs: WARN 2022-04-30… server_start:154: Failed to start server: address already in use: \\.\pipe\nvim-4480-0 WARN 2022-04-30… server_start:154: Failed to start server: address already in use: \\.\pipe\nvim-2168-0 SOLUTION ------------------------------------------------------------------------ 1. Set $NVIM to the parent v:servername, *only* in child processes. - Now the correct way to detect a "parent" Nvim is to check for $NVIM. 2. Do NOT set $NVIM_LISTEN_ADDRESS in child processes. 3. On startup if $NVIM_LISTEN_ADDRESS exists, unset it immediately after server init. 4. Open a channel to parent automatically, expose it as v:parent. Fixes #3118 Fixes #6764 Fixes #9336 Ref https://github.com/neovim/neovim/pull/8247#issuecomment-380275696 Ref #8696 --- src/nvim/terminal.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index b07c3786c3..225aa0100e 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -179,6 +179,13 @@ static void term_output_callback(const char *s, size_t len, void *user_data) // public API {{{ +/// Initializes terminal properties, and triggers TermOpen. +/// +/// The PTY process (TerminalOptions.data) was already started by termopen(), +/// via ex_terminal() or the term:// BufReadCmd. +/// +/// @param buf Buffer used for presentation of the terminal. +/// @param opts PTY process channel, various terminal properties and callbacks. Terminal *terminal_open(buf_T *buf, TerminalOptions opts) { // Create a new terminal instance and configure it @@ -374,6 +381,7 @@ void terminal_check_size(Terminal *term) invalidate_terminal(term, -1, -1); } +/// Implements TERM_FOCUS mode. :help Terminal-mode void terminal_enter(void) { buf_T *buf = curbuf; @@ -502,6 +510,7 @@ static int terminal_check(VimState *state) return 1; } +/// Processes one char of terminal-mode input. static int terminal_execute(VimState *state, int key) { TerminalState *s = (TerminalState *)state; @@ -1448,7 +1457,8 @@ static void refresh_terminal(Terminal *term) long ml_added = buf->b_ml.ml_line_count - ml_before; adjust_topline(term, buf, ml_added); } -// Calls refresh_terminal() on all invalidated_terminals. + +/// Calls refresh_terminal() on all invalidated_terminals. static void refresh_timer_cb(TimeWatcher *watcher, void *data) { refresh_pending = false; -- cgit From 9a671e6a24243a5ff2879599d91ab5aec8b4e77d Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Wed, 4 May 2022 18:27:22 +0200 Subject: refactor: replace char_u variables and functions with char Work on https://github.com/neovim/neovim/issues/459 --- src/nvim/terminal.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 225aa0100e..385b55e545 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -1533,7 +1533,7 @@ static void refresh_scrollback(Terminal *term, buf_T *buf) int row_offset = term->sb_pending; while (term->sb_pending > 0 && buf->b_ml.ml_line_count < height) { fetch_row(term, term->sb_pending - row_offset - 1, width); - ml_append(0, (uint8_t *)term->textbuf, 0, false); + ml_append(0, term->textbuf, 0, false); appended_lines(0, 1); term->sb_pending--; } @@ -1551,7 +1551,7 @@ static void refresh_scrollback(Terminal *term, buf_T *buf) } fetch_row(term, -term->sb_pending - row_offset, width); int buf_index = (int)buf->b_ml.ml_line_count - height; - ml_append(buf_index, (uint8_t *)term->textbuf, 0, false); + ml_append(buf_index, term->textbuf, 0, false); appended_lines(buf_index, 1); term->sb_pending--; } @@ -1591,10 +1591,10 @@ static void refresh_screen(Terminal *term, buf_T *buf) fetch_row(term, r, width); if (linenr <= buf->b_ml.ml_line_count) { - ml_replace(linenr, (uint8_t *)term->textbuf, true); + ml_replace(linenr, term->textbuf, true); changed++; } else { - ml_append(linenr - 1, (uint8_t *)term->textbuf, 0, false); + ml_append(linenr - 1, term->textbuf, 0, false); added++; } } -- cgit From 2a378e6e82cececb12339f2df51ffe107039d867 Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Wed, 4 May 2022 22:35:50 +0200 Subject: refactor: replace char_u variables and functions with char Work on https://github.com/neovim/neovim/issues/459 --- src/nvim/terminal.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 385b55e545..1f763c3e1e 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -673,8 +673,8 @@ void terminal_paste(long count, char_u **y_array, size_t y_size) char_u *dst = buff; char_u *src = y_array[j]; while (*src != '\0') { - len = (size_t)utf_ptr2len(src); - int c = utf_ptr2char(src); + len = (size_t)utf_ptr2len((char *)src); + int c = utf_ptr2char((char *)src); if (!is_filter_char(c)) { memcpy(dst, src, len); dst += len; -- cgit From e31b32a293f6ba8708499a176234f8be1df6a145 Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Thu, 5 May 2022 13:36:14 +0200 Subject: refactor: replace char_u variables and functions with char Work on https://github.com/neovim/neovim/issues/459 --- src/nvim/terminal.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 1f763c3e1e..142da8f184 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -1377,8 +1377,7 @@ static void fetch_row(Terminal *term, int row, int end_col) int cell_len = 0; if (cell.chars[0]) { for (int i = 0; cell.chars[i]; i++) { - cell_len += utf_char2bytes((int)cell.chars[i], - (uint8_t *)ptr + cell_len); + cell_len += utf_char2bytes((int)cell.chars[i], ptr + cell_len); } } else { *ptr = ' '; -- cgit From 9aa5647e686e5420e5b9b51828ec7d55631f98ed Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 10 May 2022 07:58:58 +0800 Subject: vim-patch:8.2.4911: the mode #defines are not clearly named (#18499) Problem: The mode #defines are not clearly named. Solution: Prepend MODE_. Renumber them to put the mapped modes first. https://github.com/vim/vim/commit/249591057b4840785c50e41dd850efb8a8faf435 A hunk from the patch depends on patch 8.2.4861, which hasn't been ported yet, but that should be easy to notice. --- src/nvim/terminal.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 142da8f184..f7d33de4fe 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -381,7 +381,7 @@ void terminal_check_size(Terminal *term) invalidate_terminal(term, -1, -1); } -/// Implements TERM_FOCUS mode. :help Terminal-mode +/// Implements MODE_TERMINAL state. :help Terminal-mode void terminal_enter(void) { buf_T *buf = curbuf; @@ -398,8 +398,8 @@ void terminal_enter(void) int save_state = State; s->save_rd = RedrawingDisabled; - State = TERM_FOCUS; - mapped_ctrl_c |= TERM_FOCUS; // Always map CTRL-C to avoid interrupt. + State = MODE_TERMINAL; + mapped_ctrl_c |= MODE_TERMINAL; // Always map CTRL-C to avoid interrupt. RedrawingDisabled = false; // Disable these options in terminal-mode. They are nonsense because cursor is @@ -1637,7 +1637,7 @@ static int linenr_to_row(Terminal *term, int linenr) static bool is_focused(Terminal *term) { - return State & TERM_FOCUS && curbuf->terminal == term; + return State & MODE_TERMINAL && curbuf->terminal == term; } static char *get_config_string(char *key) -- cgit From 3a91adabda43376638e0edc80f54181258c98dea Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Thu, 12 May 2022 20:19:29 +0800 Subject: refactor: rename keymap.{c,h} to keycodes.{c,h} (#18535) Most code in keymap.h is for keycode definitions, while most code in keymap.c is for the parsing and conversion of keycodes. The name "keymap" may also make people think these two files are for mappings, while in fact keycodes are used even when no mappings are involved, so "keycodes" should be a better file name than "keymap". --- src/nvim/terminal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index f7d33de4fe..9ea847d9fb 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -56,7 +56,7 @@ #include "nvim/getchar.h" #include "nvim/highlight.h" #include "nvim/highlight_group.h" -#include "nvim/keymap.h" +#include "nvim/keycodes.h" #include "nvim/log.h" #include "nvim/macros.h" #include "nvim/main.h" -- cgit From 963cfa7020a0e27be7223e43f6b5ee270d2ee7ec Mon Sep 17 00:00:00 2001 From: Clément Bœsch Date: Thu, 12 May 2022 14:53:08 +0200 Subject: fix(terminal): invalid pointer comparison #18453 At the moment of comparison, the pointer save_curwin can be invalid (as suggested by the comment) because it has been free'd. Worst, the new curwin could have been re-allocated to that same pointer, altering the execution flow unpredictably. While there are many other potential similar cases to fix in the codebase, the presented scenario is not hypothetical and does happen in practice (while spawning new windows from fzf for instance). There are numerous other instances of curwin comparisons in the codebase, and they may need further investigation. closes #16941 --- src/nvim/terminal.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 9ea847d9fb..2d3102707c 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -404,7 +404,7 @@ void terminal_enter(void) // Disable these options in terminal-mode. They are nonsense because cursor is // placed at end of buffer to "follow" output. #11072 - win_T *save_curwin = curwin; + handle_T save_curwin = curwin->handle; bool save_w_p_cul = curwin->w_p_cul; char_u *save_w_p_culopt = NULL; char_u save_w_p_culopt_flags = curwin->w_p_culopt_flags; @@ -442,7 +442,7 @@ void terminal_enter(void) RedrawingDisabled = s->save_rd; apply_autocmds(EVENT_TERMLEAVE, NULL, NULL, false, curbuf); - if (save_curwin == curwin) { // save_curwin may be invalid (window closed)! + if (save_curwin == curwin->handle) { // Else: window was closed. curwin->w_p_cul = save_w_p_cul; if (save_w_p_culopt) { xfree(curwin->w_p_culopt); -- cgit From f49699737c9b24e1af52719974cf3bc770539ef9 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 17 May 2022 21:09:28 +0800 Subject: fix(terminal): do not trim whitespace that is actually in the terminal (#16423) --- src/nvim/terminal.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 2d3102707c..c8f70d4afd 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -1374,26 +1374,21 @@ static void fetch_row(Terminal *term, int row, int end_col) while (col < end_col) { VTermScreenCell cell; fetch_cell(term, row, col, &cell); - int cell_len = 0; if (cell.chars[0]) { + int cell_len = 0; for (int i = 0; cell.chars[i]; i++) { cell_len += utf_char2bytes((int)cell.chars[i], ptr + cell_len); } - } else { - *ptr = ' '; - cell_len = 1; - } - char c = *ptr; - ptr += cell_len; - if (c != ' ') { - // only increase the line length if the last character is not whitespace + ptr += cell_len; line_len = (size_t)(ptr - term->textbuf); + } else { + *ptr++ = ' '; } col += cell.width; } - // trim trailing whitespace - term->textbuf[line_len] = 0; + // end of line + term->textbuf[line_len] = NUL; } static bool fetch_cell(Terminal *term, int row, int col, VTermScreenCell *cell) -- cgit From 9fec6dc9a25b5cf9c9a444ac2bd0728e8af3229e Mon Sep 17 00:00:00 2001 From: dundargoc <33953936+dundargoc@users.noreply.github.com> Date: Wed, 25 May 2022 20:31:14 +0200 Subject: refactor(uncrustify): set maximum number of consecutive newlines to 2 (#18695) --- src/nvim/terminal.c | 3 --- 1 file changed, 3 deletions(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index c8f70d4afd..e4262c2ca9 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -359,7 +359,6 @@ void terminal_check_size(Terminal *term) vterm_get_size(term->vt, &curheight, &curwidth); uint16_t width = 0, height = 0; - FOR_ALL_TAB_WINDOWS(tp, wp) { if (wp->w_buffer && wp->w_buffer->terminal == term) { const uint16_t win_width = @@ -722,7 +721,6 @@ static int get_rgb(VTermState *state, VTermColor color) return RGB_(color.rgb.red, color.rgb.green, color.rgb.blue); } - void terminal_get_line_attributes(Terminal *term, win_T *wp, int linenr, int *term_attrs) { int height, width; @@ -1364,7 +1362,6 @@ end: // }}} // terminal buffer refresh & misc {{{ - static void fetch_row(Terminal *term, int row, int end_col) { int col = 0; -- cgit From 916d848049979e7b3974fbf4b6fdf2cf58ae98c3 Mon Sep 17 00:00:00 2001 From: Javier Lopez Date: Wed, 8 Jun 2022 19:46:57 -0500 Subject: fix(terminal): scrollback delete lines immediately #18832 * on_scrollback_option_changed renamed to adjust_scrollback. The function name did not correspond to what it was doing. It is called unconditionally in every refresh of the terminal unrelated if the scrollback option was changed. * new on_scrollback_option_changed function, which calls refresh_terminal, which then calls adjust_scrollback * terminal_check_size is not the appropriate function to call when the option is changed since it only conditionally adjusts the scrollback. Use the new on_scrollback_option_changed fixes #15477 fixes #11811 --- src/nvim/terminal.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index e4262c2ca9..c5b3bbcb01 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -233,6 +233,8 @@ Terminal *terminal_open(buf_T *buf, TerminalOptions opts) RESET_BINDING(curwin); // Reset cursor in current window. curwin->w_cursor = (pos_T){ .lnum = 1, .col = 0, .coladd = 0 }; + // Initialize to check if the scrollback buffer has been allocated inside a TermOpen autocmd + rv->sb_buffer = NULL; // Apply TermOpen autocmds _before_ configuring the scrollback buffer. apply_autocmds(EVENT_TERMOPEN, NULL, NULL, false, buf); // Local 'scrollback' _after_ autocmds. @@ -1481,8 +1483,16 @@ static void refresh_size(Terminal *term, buf_T *buf) term->opts.resize_cb((uint16_t)width, (uint16_t)height, term->opts.data); } -/// Adjusts scrollback storage after 'scrollback' option changed. -static void on_scrollback_option_changed(Terminal *term, buf_T *buf) +void on_scrollback_option_changed(Terminal *term) +{ + // Scrollback buffer may not exist yet, e.g. if 'scrollback' is set in a TermOpen autocmd. + if (term->sb_buffer != NULL) { + refresh_terminal(term); + } +} + +/// Adjusts scrollback storage and the terminal buffer scrollback lines +static void adjust_scrollback(Terminal *term, buf_T *buf) { if (buf->b_p_scbk < 1) { // Local 'scrollback' was set to -1. buf->b_p_scbk = SB_MAX; @@ -1554,7 +1564,7 @@ static void refresh_scrollback(Terminal *term, buf_T *buf) deleted_lines(buf->b_ml.ml_line_count, 1); } - on_scrollback_option_changed(term, buf); + adjust_scrollback(term, buf); } // Refresh the screen (visible part of the buffer when the terminal is -- cgit From a732c253b71f89702285d5ec6fd7803045f6add9 Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Sat, 7 May 2022 12:53:37 +0200 Subject: refactor: change type of linenr_T from long to int32_t The size of long varies depending on architecture, in contrast to the MAXLNUM constant which sets the maximum allowable number of lines to 2^32-1. This discrepancy may lead to hard to detect bugs, for example https://github.com/neovim/neovim/issues/18454. Setting linenr_T to a fix maximum size of 2^32-1 will prevent this type of errors in the future. Also change the variables `amount` and `amount_after` to be linenr_T since they're referring to "the line number difference" between two texts. --- src/nvim/terminal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index c5b3bbcb01..a0adf482e4 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -1511,7 +1511,7 @@ static void adjust_scrollback(Terminal *term, buf_T *buf) term->sb_current--; xfree(term->sb_buffer[term->sb_current]); } - deleted_lines(1, (long)diff); + deleted_lines(1, (linenr_T)diff); } // Resize the scrollback storage. -- cgit From ff6b8f54359037790b300cb06a025f84f11d829a Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sat, 18 Jun 2022 18:53:12 +0200 Subject: fix(terminal): coverity USE_AFTER_FREE #18978 Problem: Coverity reports use after free: *** CID 352784: Memory - illegal accesses (USE_AFTER_FREE) /src/nvim/buffer.c: 1508 in set_curbuf() 1502 if (old_tw != curbuf->b_p_tw) { 1503 check_colorcolumn(curwin); 1504 } 1505 } 1506 1507 if (bufref_valid(&prevbufref) && prevbuf->terminal != NULL) { >>> CID 352784: Memory - illegal accesses (USE_AFTER_FREE) >>> Calling "terminal_check_size" dereferences freed pointer "prevbuf->terminal". 1508 terminal_check_size(prevbuf->terminal); 1509 } 1510 } 1511 1512 /// Enter a new current buffer. 1513 /// Old curbuf must have been abandoned already! This also means "curbuf" may Solution: Change terminal_destroy and terminal_close to set caller storage to NULL, similar to XFREE_CLEAR. This aligns with the pattern found already in: terminal_destroy e897ccad3eb1e term_delayed_free 3e59c1e20d605 --- src/nvim/terminal.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index a0adf482e4..b9fdddf235 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -275,8 +275,12 @@ Terminal *terminal_open(buf_T *buf, TerminalOptions opts) return rv; } -void terminal_close(Terminal *term, int status) +/// Closes the Terminal buffer. +/// +/// May call terminal_destroy, which sets caller storage to NULL. +void terminal_close(Terminal **termpp, int status) { + Terminal *term = *termpp; if (term->destroy) { return; } @@ -285,7 +289,7 @@ void terminal_close(Terminal *term, int status) if (entered_free_all_mem) { // If called from close_buffer() inside free_all_mem(), the main loop has // already been freed, so it is not safe to call the close callback here. - terminal_destroy(term); + terminal_destroy(termpp); return; } #endif @@ -586,8 +590,11 @@ static int terminal_execute(VimState *state, int key) return 1; } -void terminal_destroy(Terminal *term) +/// Frees the given Terminal structure and sets the caller storage to NULL (in the spirit of +/// XFREE_CLEAR). +void terminal_destroy(Terminal **termpp) { + Terminal *term = *termpp; buf_T *buf = handle_get_buffer(term->buf_handle); if (buf) { term->buf_handle = 0; @@ -608,6 +615,7 @@ void terminal_destroy(Terminal *term) xfree(term->sb_buffer); vterm_free(term->vt); xfree(term); + *termpp = NULL; // coverity[dead-store] } } -- cgit From 374e0b6678b21105bd5a26265e483cc4d9dbcaad Mon Sep 17 00:00:00 2001 From: bfredl Date: Tue, 21 Jun 2022 12:29:49 +0200 Subject: perf(highlight): don't allocate duplicates for color names --- src/nvim/terminal.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index b9fdddf235..85517a71a4 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -257,7 +257,8 @@ Terminal *terminal_open(buf_T *buf, TerminalOptions opts) snprintf(var, sizeof(var), "terminal_color_%d", i); char *name = get_config_string(var); if (name) { - color_val = name_to_color(name); + int dummy; + color_val = name_to_color(name, &dummy); xfree(name); if (color_val != -1) { -- cgit From 3b8804571c565a91c9ce729bb487c7ba21b659e0 Mon Sep 17 00:00:00 2001 From: Dundar Goc Date: Tue, 28 Jun 2022 13:03:09 +0200 Subject: refactor: replace char_u Work on https://github.com/neovim/neovim/issues/459 --- src/nvim/terminal.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/nvim/terminal.c') diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 85517a71a4..be49048aec 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -228,7 +228,7 @@ Terminal *terminal_open(buf_T *buf, TerminalOptions opts) set_option_value("wrap", false, NULL, OPT_LOCAL); set_option_value("list", false, NULL, OPT_LOCAL); if (buf->b_ffname != NULL) { - buf_set_term_title(buf, (char *)buf->b_ffname); + buf_set_term_title(buf, buf->b_ffname); } RESET_BINDING(curwin); // Reset cursor in current window. -- cgit