wp->w_has_scrollbar = wp->w_want_scrollbar
&& (wp->w_topline > 1 || lnum <= wp->w_buffer->b_ml.ml_line_count);
+#ifdef FEAT_TERMINAL
+ if (wp->w_buffer->b_term != NULL)
+ // Terminal window never has a scrollbar, adjusts to window height.
+ wp->w_has_scrollbar = FALSE;
+#endif
if (wp->w_has_scrollbar)
{
++right_extra;
semsg(_(e_nobufnr), argvars[0].vval.v_number);
return NULL;
}
-#ifdef FEAT_TERMINAL
- if (buf->b_term != NULL)
- {
- emsg(_("E278: Cannot put a terminal buffer in a popup window"));
- return NULL;
- }
-#endif
}
else if (!(argvars[0].v_type == VAR_STRING
&& argvars[0].vval.v_string != NULL)
&& !(argvars[0].v_type == VAR_LIST
&& argvars[0].vval.v_list != NULL))
{
- emsg(_(e_listreq));
+ emsg(_("E450: buffer number, text or a list required"));
return NULL;
}
if (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL)
redraw_all_later(NOT_VALID);
popup_mask_refresh = TRUE;
+ // When running a terminal in the popup it becomes the current window.
+ if (buf->b_term != NULL)
+ win_enter(wp, FALSE);
+
return wp;
}
{
int id = wp->w_id;
+ if (wp == curwin && curbuf->b_term != NULL)
+ {
+ // Closing popup window with a terminal: put focus back on the previous
+ // window.
+ win_enter(prevwin, FALSE);
+ }
+
// Just in case a check higher up is missing.
if (wp == curwin && ERROR_IF_POPUP_WINDOW)
return;
popup_close(id);
}
- static void
+ void
popup_close_with_retval(win_T *wp, int retval)
{
typval_T res;
int
error_if_popup_window()
{
- if (WIN_IS_POPUP(curwin))
+ // win_execute() may set "curwin" to a popup window temporarily, but many
+ // commands are disallowed then. When a terminal runs in the popup most
+ // things are allowed.
+ if (WIN_IS_POPUP(curwin) && curbuf->b_term == NULL)
{
emsg(_("E994: Not allowed in a popup window"));
return TRUE;
return FALSE;
}
+ int
+error_if_term_popup_window()
+{
+ if (WIN_IS_POPUP(curwin) && curbuf->b_term != NULL)
+ {
+ emsg(_("E899: Not allowed for a terminal in a popup window"));
+ return TRUE;
+ }
+ return FALSE;
+}
+
/*
* Reset all the "handled_flag" flags in global popup windows and popup windows
* in the current tab page.
int state;
int was_must_redraw = must_redraw;
+ // Popup window with terminal always gets focus.
+ if (popup_is_popup(curwin) && curbuf->b_term != NULL)
+ return FALSE;
+
if (recursive)
return FALSE;
recursive = TRUE;
wp->w_winrow -= top_off;
wp->w_wincol -= left_extra;
+ // cursor position matters in terminal
+ wp->w_wrow += top_off;
+ wp->w_wcol += left_extra;
total_width = popup_width(wp);
total_height = popup_height(wp);
term_send_mouse(VTerm *vterm, int button, int pressed)
{
VTermModifier mod = VTERM_MOD_NONE;
+ int row = mouse_row - W_WINROW(curwin);
+ int col = mouse_col - curwin->w_wincol;
- vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
- mouse_col - curwin->w_wincol, mod);
+#ifdef FEAT_PROP_POPUP
+ if (popup_is_popup(curwin))
+ {
+ row -= popup_top_extra(curwin);
+ col -= popup_left_extra(curwin);
+ }
+#endif
+ vterm_mouse_move(vterm, row, col, mod);
if (button != 0)
vterm_mouse_button(vterm, button, pressed, mod);
return TRUE;
case K_MOUSEDOWN:
case K_MOUSELEFT:
case K_MOUSERIGHT:
- if (mouse_row < W_WINROW(curwin)
- || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
- || mouse_col < curwin->w_wincol
- || mouse_col >= W_ENDCOL(curwin)
- || dragging_outside)
{
- // click or scroll outside the current window or on status line
- // or vertical separator
- if (typed)
+ int row = mouse_row;
+ int col = mouse_col;
+
+#ifdef FEAT_PROP_POPUP
+ if (popup_is_popup(curwin))
+ {
+ row -= popup_top_extra(curwin);
+ col -= popup_left_extra(curwin);
+ }
+#endif
+ if (row < W_WINROW(curwin)
+ || row >= (W_WINROW(curwin) + curwin->w_height)
+ || col < curwin->w_wincol
+ || col >= W_ENDCOL(curwin)
+ || dragging_outside)
{
- stuffcharReadbuff(c);
- mouse_was_outside = TRUE;
+ // click or scroll outside the current window or on status
+ // line or vertical separator
+ if (typed)
+ {
+ stuffcharReadbuff(c);
+ mouse_was_outside = TRUE;
+ }
+ return FAIL;
}
- return FAIL;
}
}
if (typed)
}
static void
-position_cursor(win_T *wp, VTermPos *pos)
+position_cursor(win_T *wp, VTermPos *pos, int add_off UNUSED)
{
wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
+#ifdef FEAT_PROP_POPUP
+ if (add_off && popup_is_popup(curwin))
+ {
+ wp->w_wrow += popup_top_extra(curwin);
+ wp->w_wcol += popup_left_extra(curwin);
+ }
+#endif
wp->w_valid |= (VALID_WCOL|VALID_WROW);
}
if (termwinkey == Ctrl_W)
termwinkey = 0;
}
- position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
+ position_cursor(curwin, &curbuf->b_term->tl_cursor_pos, TRUE);
may_set_cursor_props(curbuf->b_term);
while (blocking || vpeekc_nomap() != NUL)
* Convert the attributes of a vterm cell into an attribute index.
*/
static int
-cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
+cell2attr(
+ win_T *wp,
+ VTermScreenCellAttrs cellattrs,
+ VTermColor cellfg,
+ VTermColor cellbg)
{
int attr = vtermAttr2hl(cellattrs);
int fg = color2index(&cellfg, TRUE, &bold);
int bg = color2index(&cellbg, FALSE, &bold);
- // Use the "Terminal" highlighting for the default colors.
+ // Use the 'wincolor' or "Terminal" highlighting for the default
+ // colors.
if ((fg == 0 || bg == 0) && t_colors >= 16)
{
- if (fg == 0 && term_default_cterm_fg >= 0)
- fg = term_default_cterm_fg + 1;
- if (bg == 0 && term_default_cterm_bg >= 0)
- bg = term_default_cterm_bg + 1;
+ int wincolor_fg = -1;
+ int wincolor_bg = -1;
+
+ if (wp != NULL && *wp->w_p_wcr != NUL)
+ {
+ int id = syn_name2id(curwin->w_p_wcr);
+
+ // Get the 'wincolor' group colors.
+ if (id > 0)
+ syn_id2cterm_bg(id, &wincolor_fg, &wincolor_bg);
+ }
+ if (fg == 0)
+ {
+ if (wincolor_fg >= 0)
+ fg = wincolor_fg + 1;
+ else if (term_default_cterm_fg >= 0)
+ fg = term_default_cterm_fg + 1;
+ }
+ if (bg == 0)
+ {
+ if (wincolor_bg >= 0)
+ bg = wincolor_bg + 1;
+ else if (term_default_cterm_bg >= 0)
+ bg = term_default_cterm_bg + 1;
+ }
}
// with 8 colors set the bold attribute to get a bright foreground
VTermScreenCellAttrs attr;
int clear_attr;
- // Set the color to clear lines with.
- vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
- &fg, &bg);
vim_memset(&attr, 0, sizeof(attr));
- clear_attr = cell2attr(attr, fg, bg);
FOR_ALL_WINDOWS(wp)
{
if (wp->w_buffer == term->tl_buffer)
+ {
+ // Set the color to clear lines with.
+ vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
+ &fg, &bg);
+ clear_attr = cell2attr(wp, attr, fg, bg);
win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
+ }
}
}
FOR_ALL_WINDOWS(wp)
{
if (wp->w_buffer == term->tl_buffer)
- position_cursor(wp, &pos);
+ position_cursor(wp, &pos, FALSE);
}
if (term->tl_buffer == curbuf && !term->tl_normal_mode)
{
{
aco_save_T aco;
int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
+#ifdef FEAT_PROP_POPUP
+ win_T *pwin = NULL;
+ // If this was a terminal in a popup window, go back to the
+ // previous window.
+ if (popup_is_popup(curwin) && curbuf == term->tl_buffer)
+ {
+ pwin = curwin;
+ if (win_valid(prevwin))
+ win_enter(prevwin, FALSE);
+ }
+ else
+#endif
// If this is the last normal window: exit Vim.
if (term->tl_buffer->b_nwindows > 0 && only_one_window())
{
if (do_set_w_closing)
curwin->w_closing = FALSE;
aucmd_restbuf(&aco);
+#ifdef FEAT_PROP_POPUP
+ if (pwin != NULL)
+ popup_close_with_retval(pwin, 0);
+#endif
return TRUE;
}
if (term->tl_finish == TL_FINISH_OPEN
* Advances "pos" to past the last column.
*/
static void
-term_line2screenline(VTermScreen *screen, VTermPos *pos, int max_col)
+term_line2screenline(
+ win_T *wp,
+ VTermScreen *screen,
+ VTermPos *pos,
+ int max_col)
{
int off = screen_get_current_line_off();
else
ScreenLines[off] = c;
}
- ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
+ ScreenAttrs[off] = cell2attr(wp, cell.attrs, cell.fg, cell.bg);
++pos->col;
++off;
{
int max_col = MIN(Columns, term->tl_cols);
- term_line2screenline(screen, &pos, max_col);
+ term_line2screenline(NULL, screen, &pos, max_col);
}
else
pos.col = 0;
newrows = 99999;
newcols = 99999;
- FOR_ALL_WINDOWS(twp)
+ for (twp = firstwin; ; twp = twp->w_next)
{
+ // Always use curwin, it may be a popup window.
+ win_T *wwp = twp == NULL ? curwin : twp;
+
// When more than one window shows the same terminal, use the
// smallest size.
- if (twp->w_buffer == term->tl_buffer)
+ if (wwp->w_buffer == term->tl_buffer)
{
- newrows = MIN(newrows, twp->w_height);
- newcols = MIN(newcols, twp->w_width);
+ newrows = MIN(newrows, wwp->w_height);
+ newcols = MIN(newcols, wwp->w_width);
}
+ if (twp == NULL)
+ break;
}
if (newrows == 99999 || newcols == 99999)
return; // safety exit
// The cursor may have been moved when resizing.
vterm_state_get_cursorpos(state, &pos);
- position_cursor(wp, &pos);
+ position_cursor(wp, &pos, FALSE);
for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
&& pos.row < wp->w_height; ++pos.row)
{
int max_col = MIN(wp->w_width, term->tl_cols);
- term_line2screenline(screen, &pos, max_col);
+ term_line2screenline(wp, screen, &pos, max_col);
}
else
pos.col = 0;
#ifdef FEAT_MENU
+ winbar_height(wp)
#endif
- , wp->w_wincol, pos.col, wp->w_width, 0);
+ , wp->w_wincol, pos.col, wp->w_width,
+#ifdef FEAT_PROP_POPUP
+ popup_is_popup(wp) ? SLF_POPUP :
+#endif
+ 0);
}
term->tl_dirty_row_start = MAX_ROW;
term->tl_dirty_row_end = 0;
* Use a negative "col" to get the filler background color.
*/
int
-term_get_attr(buf_T *buf, linenr_T lnum, int col)
+term_get_attr(win_T *wp, linenr_T lnum, int col)
{
+ buf_T *buf = wp->w_buffer;
term_T *term = buf->b_term;
sb_line_T *line;
cellattr_T *cellattr;
else
cellattr = line->sb_cells + col;
}
- return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
+ return cell2attr(wp, cellattr->attrs, cellattr->fg, cellattr->bg);
}
/*
* Initialize term->tl_default_color from the environment.
*/
static void
-init_default_colors(term_T *term)
+init_default_colors(term_T *term, win_T *wp)
{
VTermColor *fg, *bg;
int fgval, bgval;
bg->red = bg->green = bg->blue = bgval;
fg->ansi_index = bg->ansi_index = VTERM_ANSI_INDEX_DEFAULT;
- // The "Terminal" highlight group overrules the defaults.
- id = syn_name2id((char_u *)"Terminal");
+ // The 'wincolor' or "Terminal" highlight group overrules the defaults.
+ if (wp != NULL && *wp->w_p_wcr != NUL)
+ id = syn_name2id(wp->w_p_wcr);
+ else
+ id = syn_name2id((char_u *)"Terminal");
// Use the actual color for the GUI and when 'termguicolors' is set.
#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
// TODO: depends on 'encoding'.
vterm_set_utf8(vterm, 1);
- init_default_colors(term);
+ init_default_colors(term, NULL);
vterm_state_set_default_colors(
state,
return OK;
}
+/*
+ * Called when 'wincolor' was set.
+ */
+ void
+term_update_colors(void)
+{
+ term_T *term = curwin->w_buffer->b_term;
+
+ init_default_colors(term, curwin);
+ vterm_state_set_default_colors(
+ vterm_obtain_state(term->tl_vterm),
+ &term->tl_default_color.fg,
+ &term->tl_default_color.bg);
+}
+
/*
* Return the text to show for the buffer name and status.
*/
VTermPos cursor_pos1;
VTermPos cursor_pos2;
- init_default_colors(term);
+ init_default_colors(term, NULL);
rettv->vval.v_number = buf->b_fnum;
bg.red, bg.green, bg.blue);
dict_add_string(dcell, "bg", rgb);
- dict_add_number(dcell, "attr", cell2attr(attrs, fg, bg));
+ dict_add_number(dcell, "attr", cell2attr(NULL, attrs, fg, bg));
dict_add_number(dcell, "width", width);
++pos.col;