From 3e09fbdf82a181f1c0be1513fd50a17bf5b0a754 Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Tue, 11 Jun 2024 11:10:34 -0500 Subject: [PATCH 1/2] feat(highlight): add StatusLineTerm and StatusLineTermNC groups These highlight groups are used for the statusline in :terminal windows. By default they link to StatusLine and StatusLineNC (respectively), so there is no visual difference unless a colorscheme defines these groups separately. --- runtime/doc/syntax.txt | 5 +++ src/nvim/highlight.h | 2 ++ src/nvim/highlight_defs.h | 2 ++ src/nvim/highlight_group.c | 58 +++++++++++++++--------------- test/functional/ui/cursor_spec.lua | 2 +- 5 files changed, 40 insertions(+), 29 deletions(-) diff --git a/runtime/doc/syntax.txt b/runtime/doc/syntax.txt index 8e38827e87..1b981da2fc 100644 --- a/runtime/doc/syntax.txt +++ b/runtime/doc/syntax.txt @@ -5107,6 +5107,11 @@ SpellRare Word that is recognized by the spellchecker as one that is StatusLine Status line of current window. *hl-StatusLineNC* StatusLineNC Status lines of not-current windows. + *hl-StatusLineTerm* +StatusLineTerm Status line of |terminal| window. + *hl-StatusLineTermNC* +StatusLineTermNC + Status line of non-current |terminal| window. *hl-TabLine* TabLine Tab pages line, not active tab page label. *hl-TabLineFill* diff --git a/src/nvim/highlight.h b/src/nvim/highlight.h index cb3a84bcaf..558727fc51 100644 --- a/src/nvim/highlight.h +++ b/src/nvim/highlight.h @@ -78,6 +78,8 @@ EXTERN const char *hlf_names[] INIT( = { [HLF_CU] = "Cursor", [HLF_BTITLE] = "FloatTitle", [HLF_BFOOTER] = "FloatFooter", + [HLF_TS] = "StatusLineTerm", + [HLF_TSNC] = "StatusLineTermNC", }); EXTERN int highlight_attr[HLF_COUNT + 1]; // Highl. attr for each context. diff --git a/src/nvim/highlight_defs.h b/src/nvim/highlight_defs.h index 25ab9dc2d9..17e3db04da 100644 --- a/src/nvim/highlight_defs.h +++ b/src/nvim/highlight_defs.h @@ -125,6 +125,8 @@ typedef enum { HLF_CU, ///< Cursor HLF_BTITLE, ///< Float Border Title HLF_BFOOTER, ///< Float Border Footer + HLF_TS, ///< status line for terminal window + HLF_TSNC, ///< status line for non-current terminal window HLF_COUNT, ///< MUST be the last one } hlf_T; diff --git a/src/nvim/highlight_group.c b/src/nvim/highlight_group.c index b8381c5183..cc9e606d1d 100644 --- a/src/nvim/highlight_group.c +++ b/src/nvim/highlight_group.c @@ -151,34 +151,36 @@ static const char *highlight_init_both[] = { "lCursor guifg=bg guibg=fg", // UI - "default link CursorIM Cursor", - "default link CursorLineFold FoldColumn", - "default link CursorLineSign SignColumn", - "default link EndOfBuffer NonText", - "default link FloatBorder NormalFloat", - "default link FloatFooter FloatTitle", - "default link FloatTitle Title", - "default link FoldColumn SignColumn", - "default link IncSearch CurSearch", - "default link LineNrAbove LineNr", - "default link LineNrBelow LineNr", - "default link MsgSeparator StatusLine", - "default link MsgArea NONE", - "default link NormalNC NONE", - "default link PmenuExtra Pmenu", - "default link PmenuExtraSel PmenuSel", - "default link PmenuKind Pmenu", - "default link PmenuKindSel PmenuSel", - "default link PmenuSbar Pmenu", - "default link Substitute Search", - "default link TabLine StatusLineNC", - "default link TabLineFill TabLine", - "default link TermCursorNC NONE", - "default link VertSplit WinSeparator", - "default link VisualNOS Visual", - "default link Whitespace NonText", - "default link WildMenu PmenuSel", - "default link WinSeparator Normal", + "default link CursorIM Cursor", + "default link CursorLineFold FoldColumn", + "default link CursorLineSign SignColumn", + "default link EndOfBuffer NonText", + "default link FloatBorder NormalFloat", + "default link FloatFooter FloatTitle", + "default link FloatTitle Title", + "default link FoldColumn SignColumn", + "default link IncSearch CurSearch", + "default link LineNrAbove LineNr", + "default link LineNrBelow LineNr", + "default link MsgSeparator StatusLine", + "default link MsgArea NONE", + "default link NormalNC NONE", + "default link PmenuExtra Pmenu", + "default link PmenuExtraSel PmenuSel", + "default link PmenuKind Pmenu", + "default link PmenuKindSel PmenuSel", + "default link PmenuSbar Pmenu", + "default link Substitute Search", + "default link StatusLineTerm StatusLine", + "default link StatusLineTermNC StatusLineNC", + "default link TabLine StatusLineNC", + "default link TabLineFill TabLine", + "default link TermCursorNC NONE", + "default link VertSplit WinSeparator", + "default link VisualNOS Visual", + "default link Whitespace NonText", + "default link WildMenu PmenuSel", + "default link WinSeparator Normal", // Syntax "default link Character Constant", diff --git a/test/functional/ui/cursor_spec.lua b/test/functional/ui/cursor_spec.lua index 8bfceb8cce..f7eb8394bd 100644 --- a/test/functional/ui/cursor_spec.lua +++ b/test/functional/ui/cursor_spec.lua @@ -250,7 +250,7 @@ describe('ui/cursor', function() m.attr = { background = Screen.colors.DarkGray } end if m.id_lm then - m.id_lm = 69 + m.id_lm = 71 end end From d38912b59f97a4da0a2d0a24af226e6dd27e9b2c Mon Sep 17 00:00:00 2001 From: Gregory Anders Date: Tue, 11 Jun 2024 11:10:34 -0500 Subject: [PATCH 2/2] refactor(terminal): move :terminal defaults to _defaults.lua --- runtime/doc/news.txt | 2 ++ runtime/doc/syntax.txt | 2 +- runtime/doc/vim_diff.txt | 21 +++++++++---------- runtime/lua/vim/_defaults.lua | 20 ++++++++++++++++++ src/nvim/terminal.c | 8 ------- test/functional/terminal/buffer_spec.lua | 6 +++--- test/functional/terminal/mouse_spec.lua | 8 ++++--- test/functional/terminal/testutil.lua | 4 ++++ .../terminal/window_split_tab_spec.lua | 8 ++++--- 9 files changed, 50 insertions(+), 29 deletions(-) diff --git a/runtime/doc/news.txt b/runtime/doc/news.txt index 455b38b5fa..4addbf84c3 100644 --- a/runtime/doc/news.txt +++ b/runtime/doc/news.txt @@ -140,6 +140,8 @@ TERMINAL • The |terminal| now understands the OSC 52 escape sequence to write to the system clipboard (copy). Querying with OSC 52 (paste) is not supported. +• |hl-StatusLineTerm| and |hl-StatusLineTermNC| define highlights for the + status line in |terminal| windows. TREESITTER diff --git a/runtime/doc/syntax.txt b/runtime/doc/syntax.txt index 1b981da2fc..9fc415a158 100644 --- a/runtime/doc/syntax.txt +++ b/runtime/doc/syntax.txt @@ -5111,7 +5111,7 @@ StatusLineNC Status lines of not-current windows. StatusLineTerm Status line of |terminal| window. *hl-StatusLineTermNC* StatusLineTermNC - Status line of non-current |terminal| window. + Status line of non-current |terminal| windows. *hl-TabLine* TabLine Tab pages line, not active tab page label. *hl-TabLineFill* diff --git a/runtime/doc/vim_diff.txt b/runtime/doc/vim_diff.txt index ac20948f14..a6f08402f6 100644 --- a/runtime/doc/vim_diff.txt +++ b/runtime/doc/vim_diff.txt @@ -166,6 +166,14 @@ nvim_terminal: when 'background' is "light". While this may not reflect the actual foreground/background color, it permits 'background' to be retained for a nested Nvim instance running in the terminal emulator. +- TermOpen: Sets default options for |terminal| buffers: + - 'nomodifiable' + - 'undolevels' set to -1 + - 'textwidth' set to 0 + - 'nowrap' + - 'nolist' + - 'winhighlight' uses |hl-StatusLineTerm| and |hl-StatusLineTermNC| in + place of |hl-StatusLine| and |hl-StatusLineNC| nvim_cmdwin: - CmdwinEnter: Limits syntax sync to maxlines=1 in the |cmdwin|. @@ -538,6 +546,8 @@ Highlight groups: - Highlight groups names are allowed to contain `@` characters. - It is an error to define a highlight group with a name that doesn't match the regexp `[a-zA-Z0-9_.@-]*` (see |group-name|). +- |hl-StatusLineTerm| |hl-StatusLineTermNC| are implemented as 'winhighlight' + window-local highlights which are set by the default |TermOpen| handler. Macro (|recording|) behavior: - Replay of a macro recorded during :lmap produces the same actions as when it @@ -665,17 +675,6 @@ Events: - *SafeStateAgain* - *SigUSR1* Use |Signal| to detect `SIGUSR1` signal instead. -Highlight groups: -- *hl-StatusLineTerm* *hl-StatusLineTermNC* are unnecessary because Nvim - supports 'winhighlight' window-local highlights. For example, to mimic Vim's - StatusLineTerm: >vim - hi StatusLineTerm ctermfg=black ctermbg=green - hi StatusLineTermNC ctermfg=green - autocmd TermOpen,WinEnter * if &buftype=='terminal' - \|setlocal winhighlight=StatusLine:StatusLineTerm,StatusLineNC:StatusLineTermNC - \|else|setlocal winhighlight=|endif -< - Options: - *'aleph'* *'al'* - antialias diff --git a/runtime/lua/vim/_defaults.lua b/runtime/lua/vim/_defaults.lua index 01dcd4bf74..f417bda3fb 100644 --- a/runtime/lua/vim/_defaults.lua +++ b/runtime/lua/vim/_defaults.lua @@ -282,6 +282,26 @@ do end, }) + vim.api.nvim_create_autocmd('TermOpen', { + group = nvim_terminal_augroup, + desc = 'Default settings for :terminal buffers', + callback = function() + vim.bo.modifiable = false + vim.bo.undolevels = -1 + vim.bo.scrollback = vim.o.scrollback < 0 and 10000 or math.max(1, vim.o.scrollback) + vim.bo.textwidth = 0 + vim.wo.wrap = false + vim.wo.list = false + + -- This is gross. Proper list options support when? + local winhl = vim.o.winhighlight + if winhl ~= '' then + winhl = winhl .. ',' + end + vim.wo.winhighlight = winhl .. 'StatusLine:StatusLineTerm,StatusLineNC:StatusLineTermNC' + end, + }) + vim.api.nvim_create_autocmd('CmdwinEnter', { pattern = '[:>]', desc = 'Limit syntax sync to maxlines=1 in the command window', diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 818f8abbb5..000f750413 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -343,14 +343,6 @@ void terminal_open(Terminal **termpp, buf_T *buf, TerminalOptions opts) refresh_screen(term, buf); set_option_value(kOptBuftype, STATIC_CSTR_AS_OPTVAL("terminal"), OPT_LOCAL); - // Default settings for terminal buffers - buf->b_p_ma = false; // 'nomodifiable' - buf->b_p_ul = -1; // 'undolevels' - buf->b_p_scbk = // 'scrollback' (initialize local from global) - (p_scbk < 0) ? 10000 : MAX(1, p_scbk); - buf->b_p_tw = 0; // 'textwidth' - set_option_value(kOptWrap, BOOLEAN_OPTVAL(false), OPT_LOCAL); - set_option_value(kOptList, BOOLEAN_OPTVAL(false), OPT_LOCAL); if (buf->b_ffname != NULL) { buf_set_term_title(buf, buf->b_ffname, strlen(buf->b_ffname)); } diff --git a/test/functional/terminal/buffer_spec.lua b/test/functional/terminal/buffer_spec.lua index 96abd9f543..4ad0862986 100644 --- a/test/functional/terminal/buffer_spec.lua +++ b/test/functional/terminal/buffer_spec.lua @@ -196,10 +196,10 @@ describe(':terminal buffer', function() screen:expect([[ ab^c | {4:~ }| - {5:========== }| + {17:========== }| rows: 2, cols: 50 | {2: } | - {1:========== }| + {18:========== }| | ]]) @@ -340,7 +340,7 @@ describe(':terminal buffer', function() eq(termbuf, eval('g:termbuf')) end) - it('TermReqeust synchronization #27572', function() + it('TermRequest synchronization #27572', function() command('autocmd! nvim_terminal TermRequest') local term = exec_lua([[ _G.input = {} diff --git a/test/functional/terminal/mouse_spec.lua b/test/functional/terminal/mouse_spec.lua index ad98dfc6c3..476e2a5fe5 100644 --- a/test/functional/terminal/mouse_spec.lua +++ b/test/functional/terminal/mouse_spec.lua @@ -14,10 +14,12 @@ describe(':terminal mouse', function() before_each(function() clear() api.nvim_set_option_value('statusline', '==========', {}) - command('highlight StatusLine cterm=NONE') - command('highlight StatusLineNC cterm=NONE') - command('highlight VertSplit cterm=NONE') screen = tt.screen_setup() + command('highlight StatusLine NONE') + command('highlight StatusLineNC NONE') + command('highlight StatusLineTerm NONE') + command('highlight StatusLineTermNC NONE') + command('highlight VertSplit NONE') local lines = {} for i = 1, 30 do table.insert(lines, 'line' .. tostring(i)) diff --git a/test/functional/terminal/testutil.lua b/test/functional/terminal/testutil.lua index f3fc5d3f93..45c73b1dc6 100644 --- a/test/functional/terminal/testutil.lua +++ b/test/functional/terminal/testutil.lua @@ -92,6 +92,8 @@ local function screen_setup(extra_rows, command, cols, env, screen_opts) api.nvim_command('highlight TermCursor cterm=reverse') api.nvim_command('highlight TermCursorNC ctermbg=11') + api.nvim_command('highlight StatusLineTerm ctermbg=2 ctermfg=0') + api.nvim_command('highlight StatusLineTermNC ctermbg=2 ctermfg=8') local screen = Screen.new(cols, 7 + extra_rows) screen:set_default_attr_ids({ @@ -111,6 +113,8 @@ local function screen_setup(extra_rows, command, cols, env, screen_opts) [14] = { underline = true, reverse = true, bold = true }, [15] = { underline = true, foreground = 12 }, [16] = { background = 248, foreground = 0 }, -- Visual in :terminal session + [17] = { background = 2, foreground = 0 }, -- StatusLineTerm + [18] = { background = 2, foreground = 8 }, -- StatusLineTermNC }) screen:attach(screen_opts or { rgb = false }) diff --git a/test/functional/terminal/window_split_tab_spec.lua b/test/functional/terminal/window_split_tab_spec.lua index 04d2e0bca7..ccd6a5218b 100644 --- a/test/functional/terminal/window_split_tab_spec.lua +++ b/test/functional/terminal/window_split_tab_spec.lua @@ -22,10 +22,12 @@ describe(':terminal', function() -- set the statusline to a constant value because of variables like pid -- and current directory and to improve visibility of splits api.nvim_set_option_value('statusline', '==========', {}) - command('highlight StatusLine cterm=NONE') - command('highlight StatusLineNC cterm=NONE') - command('highlight VertSplit cterm=NONE') screen = tt.screen_setup(3) + command('highlight StatusLine NONE') + command('highlight StatusLineNC NONE') + command('highlight StatusLineTerm NONE') + command('highlight StatusLineTermNC NONE') + command('highlight VertSplit NONE') end) after_each(function()