Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * tabpanel.c: |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | #if defined(FEAT_TABPANEL) || defined(PROTO) |
| 17 | |
| 18 | static void do_by_tplmode(int tplmode, int col_start, int col_end, |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 19 | int *pcurtab_row, int *ptabpagenr); |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 20 | |
| 21 | // set pcurtab_row. don't redraw tabpanel. |
| 22 | #define TPLMODE_GET_CURTAB_ROW 0 |
| 23 | // set ptabpagenr. don't redraw tabpanel. |
| 24 | #define TPLMODE_GET_TABPAGENR 1 |
| 25 | // redraw tabpanel. |
| 26 | #define TPLMODE_REDRAW 2 |
| 27 | |
| 28 | #define TPL_FILLCHAR ' ' |
| 29 | |
| 30 | #define VERT_LEN 1 |
| 31 | |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 32 | // tpl_align's values |
| 33 | #define ALIGN_LEFT 0 |
| 34 | #define ALIGN_RIGHT 1 |
| 35 | |
| 36 | static char_u *opt_name = (char_u *)"tabpanel"; |
| 37 | static int opt_scope = OPT_LOCAL; |
| 38 | static int tpl_align = ALIGN_LEFT; |
| 39 | static int tpl_columns = 20; |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 40 | static int tpl_is_vert = FALSE; |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 41 | |
| 42 | typedef struct { |
| 43 | win_T *wp; |
| 44 | win_T *cwp; |
| 45 | char_u *user_defined; |
| 46 | int maxrow; |
| 47 | int offsetrow; |
| 48 | int *prow; |
| 49 | int *pcol; |
| 50 | int attr; |
| 51 | int col_start; |
| 52 | int col_end; |
| 53 | } tabpanel_T; |
| 54 | |
| 55 | int |
| 56 | tabpanelopt_changed(void) |
| 57 | { |
| 58 | char_u *p; |
| 59 | int new_align = ALIGN_LEFT; |
| 60 | int new_columns = 20; |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 61 | int new_is_vert = FALSE; |
Naruhiko Nishino | 2a1e253 | 2025-05-17 16:19:24 +0200 | [diff] [blame] | 62 | int do_equal = 0; |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 63 | |
| 64 | p = p_tplo; |
| 65 | while (*p != NUL) |
| 66 | { |
Hirohito Higashi | 598bbb1 | 2025-05-22 22:41:05 +0200 | [diff] [blame] | 67 | if (STRNCMP(p, "align:", 6) == 0) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 68 | { |
Hirohito Higashi | 598bbb1 | 2025-05-22 22:41:05 +0200 | [diff] [blame] | 69 | p += 6; |
| 70 | if (STRNCMP(p, "left", 4) == 0) |
| 71 | { |
| 72 | p += 4; |
| 73 | new_align = ALIGN_LEFT; |
| 74 | } |
| 75 | else if (STRNCMP(p, "right", 5) == 0) |
| 76 | { |
| 77 | p += 5; |
| 78 | new_align = ALIGN_RIGHT; |
| 79 | } |
| 80 | else |
| 81 | return FAIL; |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 82 | } |
| 83 | else if (STRNCMP(p, "columns:", 8) == 0 && VIM_ISDIGIT(p[8])) |
| 84 | { |
| 85 | p += 8; |
| 86 | new_columns = getdigits(&p); |
| 87 | } |
| 88 | else if (STRNCMP(p, "vert", 4) == 0) |
| 89 | { |
| 90 | p += 4; |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 91 | new_is_vert = TRUE; |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | if (*p != ',' && *p != NUL) |
| 95 | return FAIL; |
| 96 | if (*p == ',') |
| 97 | ++p; |
| 98 | } |
| 99 | |
Naruhiko Nishino | 2a1e253 | 2025-05-17 16:19:24 +0200 | [diff] [blame] | 100 | // Whether all the windows are automatically made the same size |
| 101 | // when tabpanel size is changed. |
| 102 | do_equal = p_ea && tpl_columns != new_columns; |
| 103 | |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 104 | tpl_align = new_align; |
| 105 | tpl_columns = new_columns; |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 106 | tpl_is_vert = new_is_vert; |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 107 | |
Naruhiko Nishino | 2a1e253 | 2025-05-17 16:19:24 +0200 | [diff] [blame] | 108 | shell_new_columns(); |
| 109 | redraw_tabpanel = TRUE; |
| 110 | |
| 111 | if (do_equal) |
| 112 | win_equal(curwin, FALSE, 0); |
| 113 | |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 114 | return OK; |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Return the width of tabpanel. |
| 119 | */ |
| 120 | int |
| 121 | tabpanel_width(void) |
| 122 | { |
| 123 | if (msg_scrolled != 0) |
| 124 | return 0; |
| 125 | |
| 126 | switch (p_stpl) |
| 127 | { |
| 128 | case 0: |
| 129 | return 0; |
| 130 | case 1: |
| 131 | if (first_tabpage->tp_next == NULL) |
| 132 | return 0; |
| 133 | } |
| 134 | if (Columns < tpl_columns) |
| 135 | return 0; |
| 136 | else |
| 137 | return tpl_columns; |
| 138 | } |
| 139 | |
| 140 | /* |
| 141 | * Return the offset of a window considering the width of tabpanel. |
| 142 | */ |
| 143 | int |
| 144 | tabpanel_leftcol(win_T *wp) |
| 145 | { |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 146 | if (cmdline_pum_active() || (wp != NULL && WIN_IS_POPUP(wp))) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 147 | return 0; |
| 148 | else |
| 149 | return tpl_align == ALIGN_RIGHT ? 0 : tabpanel_width(); |
| 150 | } |
| 151 | |
| 152 | /* |
| 153 | * draw the tabpanel. |
| 154 | */ |
| 155 | void |
| 156 | draw_tabpanel(void) |
| 157 | { |
| 158 | int saved_KeyTyped = KeyTyped; |
| 159 | int saved_got_int = got_int; |
| 160 | int maxwidth = tabpanel_width(); |
| 161 | int vs_attr = HL_ATTR(HLF_C); |
| 162 | int curtab_row = 0; |
| 163 | #ifndef MSWIN |
| 164 | int row = 0; |
| 165 | int off = 0; |
| 166 | #endif |
| 167 | int vsrow = 0; |
| 168 | int is_right = tpl_align == ALIGN_RIGHT; |
| 169 | |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 170 | if (maxwidth == 0) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 171 | return; |
| 172 | |
| 173 | #ifndef MSWIN |
| 174 | // We need this section only for the Vim running on WSL. |
| 175 | for (row = 0; row < cmdline_row; row++) |
| 176 | { |
| 177 | if (is_right) |
| 178 | off = LineOffset[row] + Columns - maxwidth; |
| 179 | else |
| 180 | off = LineOffset[row]; |
| 181 | |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 182 | vim_memset(ScreenLines + off, ' ', (size_t)maxwidth * sizeof(schar_T)); |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 183 | if (enc_utf8) |
| 184 | vim_memset(ScreenLinesUC + off, -1, |
| 185 | (size_t)maxwidth * sizeof(u8char_T)); |
| 186 | } |
| 187 | #endif |
| 188 | |
| 189 | // Reset got_int to avoid build_stl_str_hl() isn't evaluted. |
| 190 | got_int = FALSE; |
| 191 | |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 192 | if (tpl_is_vert) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 193 | { |
| 194 | if (is_right) |
| 195 | { |
| 196 | // draw main contents in tabpanel |
| 197 | do_by_tplmode(TPLMODE_GET_CURTAB_ROW, VERT_LEN, |
| 198 | maxwidth - VERT_LEN, &curtab_row, NULL); |
| 199 | do_by_tplmode(TPLMODE_REDRAW, VERT_LEN, maxwidth, &curtab_row, |
| 200 | NULL); |
| 201 | // clear for multi-byte vert separater |
| 202 | screen_fill(0, cmdline_row, COLUMNS_WITHOUT_TPL(), |
| 203 | COLUMNS_WITHOUT_TPL() + VERT_LEN, |
| 204 | TPL_FILLCHAR, TPL_FILLCHAR, vs_attr); |
| 205 | // draw vert separater in tabpanel |
| 206 | for (vsrow = 0; vsrow < cmdline_row; vsrow++) |
| 207 | screen_putchar(curwin->w_fill_chars.tpl_vert, vsrow, |
| 208 | COLUMNS_WITHOUT_TPL(), vs_attr); |
| 209 | } |
| 210 | else |
| 211 | { |
| 212 | // draw main contents in tabpanel |
| 213 | do_by_tplmode(TPLMODE_GET_CURTAB_ROW, 0, maxwidth - VERT_LEN, |
| 214 | &curtab_row, NULL); |
| 215 | do_by_tplmode(TPLMODE_REDRAW, 0, maxwidth - VERT_LEN, |
| 216 | &curtab_row, NULL); |
| 217 | // clear for multi-byte vert separater |
| 218 | screen_fill(0, cmdline_row, maxwidth - VERT_LEN, |
| 219 | maxwidth, TPL_FILLCHAR, TPL_FILLCHAR, vs_attr); |
| 220 | // draw vert separater in tabpanel |
| 221 | for (vsrow = 0; vsrow < cmdline_row; vsrow++) |
| 222 | screen_putchar(curwin->w_fill_chars.tpl_vert, vsrow, |
| 223 | maxwidth - VERT_LEN, vs_attr); |
| 224 | } |
| 225 | } |
| 226 | else |
| 227 | { |
| 228 | do_by_tplmode(TPLMODE_GET_CURTAB_ROW, 0, maxwidth, &curtab_row, NULL); |
| 229 | do_by_tplmode(TPLMODE_REDRAW, 0, maxwidth, &curtab_row, NULL); |
| 230 | } |
| 231 | |
| 232 | got_int |= saved_got_int; |
| 233 | |
| 234 | // A user function may reset KeyTyped, restore it. |
| 235 | KeyTyped = saved_KeyTyped; |
| 236 | |
| 237 | redraw_tabpanel = FALSE; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Return tabpagenr when clicking and dragging in tabpanel. |
| 242 | */ |
| 243 | int |
| 244 | get_tabpagenr_on_tabpanel(void) |
| 245 | { |
| 246 | int maxwidth = tabpanel_width(); |
| 247 | int curtab_row = 0; |
| 248 | int tabpagenr = 0; |
| 249 | |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 250 | if (maxwidth == 0) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 251 | return -1; |
| 252 | |
| 253 | do_by_tplmode(TPLMODE_GET_CURTAB_ROW, 0, maxwidth, &curtab_row, NULL); |
| 254 | do_by_tplmode(TPLMODE_GET_TABPAGENR, 0, maxwidth, &curtab_row, |
| 255 | &tabpagenr); |
| 256 | |
| 257 | return tabpagenr; |
| 258 | } |
| 259 | |
| 260 | /* |
| 261 | * Fill tailing area between {start_row} and {end_row - 1}. |
| 262 | */ |
| 263 | static void |
| 264 | screen_fill_tailing_area( |
| 265 | int tplmode, |
| 266 | int row_start, |
| 267 | int row_end, |
| 268 | int col_start, |
| 269 | int col_end, |
| 270 | int attr) |
| 271 | { |
| 272 | int is_right = tpl_align == ALIGN_RIGHT; |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 273 | if (tplmode == TPLMODE_REDRAW) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 274 | screen_fill(row_start, row_end, |
| 275 | (is_right ? COLUMNS_WITHOUT_TPL() : 0) + col_start, |
| 276 | (is_right ? COLUMNS_WITHOUT_TPL() : 0) + col_end, |
| 277 | TPL_FILLCHAR, TPL_FILLCHAR, attr); |
| 278 | } |
| 279 | |
| 280 | /* |
| 281 | * screen_puts_len() for tabpanel. |
| 282 | */ |
| 283 | static void |
| 284 | screen_puts_len_for_tabpanel( |
| 285 | int tplmode, |
| 286 | char_u *p, |
| 287 | int len, |
| 288 | int attr, |
| 289 | tabpanel_T *pargs) |
| 290 | { |
| 291 | int j, k; |
| 292 | int chlen; |
| 293 | int chcells; |
| 294 | char_u buf[IOSIZE]; |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 295 | char_u *temp; |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 296 | |
| 297 | for (j = 0; j < len;) |
| 298 | { |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 299 | if (tplmode != TPLMODE_GET_CURTAB_ROW |
| 300 | && pargs->maxrow <= *pargs->prow - pargs->offsetrow) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 301 | break; |
| 302 | |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 303 | if (p[j] == '\n' || p[j] == '\r') |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 304 | { |
| 305 | // fill the tailing area of current row. |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 306 | if (*pargs->prow - pargs->offsetrow >= 0 |
| 307 | && *pargs->prow - pargs->offsetrow < pargs->maxrow) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 308 | screen_fill_tailing_area(tplmode, |
| 309 | *pargs->prow - pargs->offsetrow, |
| 310 | *pargs->prow - pargs->offsetrow + 1, |
| 311 | *pargs->pcol, pargs->col_end, attr); |
| 312 | (*pargs->prow)++; |
| 313 | *pargs->pcol = pargs->col_start; |
| 314 | j++; |
| 315 | } |
| 316 | else |
| 317 | { |
| 318 | if (has_mbyte) |
| 319 | chlen = (*mb_ptr2len)(p + j); |
| 320 | else |
| 321 | chlen = (int)STRLEN(p + j); |
| 322 | |
| 323 | for (k = 0; k < chlen; k++) |
| 324 | buf[k] = p[j + k]; |
| 325 | buf[chlen] = NUL; |
| 326 | j += chlen; |
| 327 | |
| 328 | // Make all characters printable. |
| 329 | temp = transstr(buf); |
| 330 | if (temp != NULL) |
| 331 | { |
| 332 | vim_strncpy(buf, temp, sizeof(buf) - 1); |
| 333 | vim_free(temp); |
| 334 | } |
| 335 | |
| 336 | if (has_mbyte) |
| 337 | chcells = (*mb_ptr2cells)(buf); |
| 338 | else |
| 339 | chcells = 1; |
| 340 | |
| 341 | if (pargs->col_end < (*pargs->pcol) + chcells) |
| 342 | { |
| 343 | // fill the tailing area of current row. |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 344 | if (*pargs->prow - pargs->offsetrow >= 0 |
| 345 | && *pargs->prow - pargs->offsetrow < pargs->maxrow) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 346 | screen_fill_tailing_area(tplmode, |
| 347 | *pargs->prow - pargs->offsetrow, |
| 348 | *pargs->prow - pargs->offsetrow + 1, |
| 349 | *pargs->pcol, pargs->col_end, attr); |
| 350 | *pargs->pcol = pargs->col_end; |
| 351 | |
| 352 | if (pargs->col_end < chcells) |
| 353 | break; |
| 354 | } |
| 355 | |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 356 | if (*pargs->pcol + chcells <= pargs->col_end) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 357 | { |
| 358 | int off = (tpl_align == ALIGN_RIGHT) |
| 359 | ? COLUMNS_WITHOUT_TPL() |
| 360 | : 0; |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 361 | if (TPLMODE_REDRAW == tplmode |
| 362 | && (*pargs->prow - pargs->offsetrow >= 0 |
| 363 | && *pargs->prow - pargs->offsetrow < pargs->maxrow)) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 364 | screen_puts(buf, *pargs->prow - pargs->offsetrow, |
| 365 | *pargs->pcol + off, attr); |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 366 | *pargs->pcol += chcells; |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | /* |
| 373 | * default tabpanel drawing behavior if 'tabpanel' option is empty. |
| 374 | */ |
| 375 | static void |
| 376 | draw_tabpanel_default(int tplmode, tabpanel_T *pargs) |
| 377 | { |
| 378 | int modified; |
| 379 | int wincount; |
| 380 | int len = 0; |
| 381 | char_u buf[2] = { NUL, NUL }; |
| 382 | |
| 383 | modified = FALSE; |
| 384 | for (wincount = 0; pargs->wp != NULL; |
| 385 | pargs->wp = pargs->wp->w_next, ++wincount) |
| 386 | if (bufIsChanged(pargs->wp->w_buffer)) |
| 387 | modified = TRUE; |
| 388 | |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 389 | if (modified || wincount > 1) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 390 | { |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 391 | if (wincount > 1) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 392 | { |
| 393 | vim_snprintf((char *)NameBuff, MAXPATHL, "%d", wincount); |
| 394 | len = (int)STRLEN(NameBuff); |
| 395 | screen_puts_len_for_tabpanel(tplmode, NameBuff, len, |
| 396 | #if defined(FEAT_SYN_HL) |
| 397 | hl_combine_attr(pargs->attr, HL_ATTR(HLF_T)), |
| 398 | #else |
| 399 | pargs->attr, |
| 400 | #endif |
| 401 | pargs); |
| 402 | } |
| 403 | if (modified) |
| 404 | { |
| 405 | buf[0] = '+'; |
| 406 | screen_puts_len_for_tabpanel(tplmode, buf, 1, pargs->attr, pargs); |
| 407 | } |
| 408 | |
| 409 | buf[0] = TPL_FILLCHAR; |
| 410 | screen_puts_len_for_tabpanel(tplmode, buf, 1, pargs->attr, pargs); |
| 411 | } |
| 412 | |
| 413 | get_trans_bufname(pargs->cwp->w_buffer); |
| 414 | shorten_dir(NameBuff); |
| 415 | len = (int)STRLEN(NameBuff); |
| 416 | screen_puts_len_for_tabpanel(tplmode, NameBuff, len, pargs->attr, pargs); |
| 417 | |
| 418 | // fill the tailing area of current row. |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 419 | if (*pargs->prow - pargs->offsetrow >= 0 |
| 420 | && *pargs->prow - pargs->offsetrow < pargs->maxrow) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 421 | screen_fill_tailing_area(tplmode, *pargs->prow - pargs->offsetrow, |
| 422 | *pargs->prow - pargs->offsetrow + 1, |
| 423 | *pargs->pcol, pargs->col_end, pargs->attr); |
| 424 | *pargs->pcol = pargs->col_end; |
| 425 | } |
| 426 | |
| 427 | /* |
| 428 | * default tabpanel drawing behavior if 'tabpanel' option is NOT empty. |
| 429 | */ |
| 430 | static void |
| 431 | draw_tabpanel_userdefined(int tplmode, tabpanel_T *pargs) |
| 432 | { |
| 433 | char_u *p; |
| 434 | int p_crb_save; |
| 435 | char_u buf[IOSIZE]; |
| 436 | stl_hlrec_T *hltab; |
| 437 | stl_hlrec_T *tabtab; |
| 438 | int curattr; |
| 439 | int n; |
| 440 | |
| 441 | // Temporarily reset 'cursorbind', we don't want a side effect from moving |
| 442 | // the cursor away and back. |
| 443 | p_crb_save = pargs->cwp->w_p_crb; |
| 444 | pargs->cwp->w_p_crb = FALSE; |
| 445 | |
| 446 | // Make a copy, because the statusline may include a function call that |
| 447 | // might change the option value and free the memory. |
| 448 | p = vim_strsave(pargs->user_defined); |
| 449 | |
| 450 | build_stl_str_hl(pargs->cwp, buf, sizeof(buf), |
| 451 | p, opt_name, opt_scope, |
| 452 | TPL_FILLCHAR, pargs->col_end - pargs->col_start, &hltab, &tabtab); |
| 453 | |
| 454 | vim_free(p); |
| 455 | pargs->cwp->w_p_crb = p_crb_save; |
| 456 | |
| 457 | curattr = pargs->attr; |
| 458 | p = buf; |
| 459 | for (n = 0; hltab[n].start != NULL; n++) |
| 460 | { |
| 461 | screen_puts_len_for_tabpanel(tplmode, p, (int)(hltab[n].start - p), |
| 462 | curattr, pargs); |
| 463 | p = hltab[n].start; |
| 464 | if (hltab[n].userhl == 0) |
| 465 | curattr = pargs->attr; |
| 466 | else if (hltab[n].userhl < 0) |
| 467 | curattr = syn_id2attr(-hltab[n].userhl); |
| 468 | #ifdef FEAT_TERMINAL |
| 469 | else if (pargs->wp != NULL && pargs->wp != curwin |
| 470 | && bt_terminal(pargs->wp->w_buffer) |
| 471 | && pargs->wp->w_status_height != 0) |
| 472 | curattr = highlight_stltermnc[hltab[n].userhl - 1]; |
| 473 | else if (pargs->wp != NULL && bt_terminal(pargs->wp->w_buffer) |
| 474 | && pargs->wp->w_status_height != 0) |
| 475 | curattr = highlight_stlterm[hltab[n].userhl - 1]; |
| 476 | #endif |
| 477 | else if (pargs->wp != NULL && pargs->wp != curwin |
| 478 | && pargs->wp->w_status_height != 0) |
| 479 | curattr = highlight_stlnc[hltab[n].userhl - 1]; |
| 480 | else |
| 481 | curattr = highlight_user[hltab[n].userhl - 1]; |
| 482 | } |
| 483 | screen_puts_len_for_tabpanel(tplmode, p, (int)STRLEN(p), curattr, pargs); |
| 484 | |
| 485 | // fill the tailing area of current row. |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 486 | if (*pargs->prow - pargs->offsetrow >= 0 |
| 487 | && *pargs->prow - pargs->offsetrow < pargs->maxrow) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 488 | screen_fill_tailing_area(tplmode, *pargs->prow - pargs->offsetrow, |
| 489 | *pargs->prow - pargs->offsetrow + 1, *pargs->pcol, |
| 490 | pargs->col_end, curattr); |
| 491 | *pargs->pcol = pargs->col_end; |
| 492 | } |
| 493 | |
| 494 | static char_u * |
| 495 | starts_with_percent_and_bang(tabpanel_T *pargs) |
| 496 | { |
| 497 | int len = 0; |
| 498 | char_u *usefmt = p_tpl; |
| 499 | |
| 500 | if (usefmt == NULL) |
| 501 | return NULL; |
| 502 | |
| 503 | len = (int)STRLEN(usefmt); |
| 504 | |
| 505 | if (len == 0) |
| 506 | return NULL; |
| 507 | |
| 508 | #ifdef FEAT_EVAL |
| 509 | // if "fmt" was set insecurely it needs to be evaluated in the sandbox |
| 510 | int use_sandbox = was_set_insecurely(opt_name, opt_scope); |
| 511 | |
| 512 | // When the format starts with "%!" then evaluate it as an expression and |
| 513 | // use the result as the actual format string. |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 514 | if (len > 1 && usefmt[0] == '%' && usefmt[1] == '!') |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 515 | { |
| 516 | typval_T tv; |
| 517 | char_u *p = NULL; |
| 518 | |
| 519 | tv.v_type = VAR_NUMBER; |
| 520 | tv.vval.v_number = pargs->cwp->w_id; |
| 521 | set_var((char_u *)"g:tabpanel_winid", &tv, FALSE); |
| 522 | |
| 523 | p = eval_to_string_safe(usefmt + 2, use_sandbox, FALSE, FALSE); |
| 524 | if (p != NULL) |
| 525 | usefmt = p; |
| 526 | |
| 527 | do_unlet((char_u *)"g:tabpanel_winid", TRUE); |
| 528 | } |
| 529 | #endif |
| 530 | |
| 531 | return usefmt; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * do something by tplmode for drawing tabpanel. |
| 536 | */ |
| 537 | static void |
| 538 | do_by_tplmode( |
| 539 | int tplmode, |
| 540 | int col_start, |
| 541 | int col_end, |
| 542 | int *pcurtab_row, |
| 543 | int *ptabpagenr) |
| 544 | { |
| 545 | int attr_tplf = HL_ATTR(HLF_TPLF); |
| 546 | int attr_tpls = HL_ATTR(HLF_TPLS); |
| 547 | int attr_tpl = HL_ATTR(HLF_TPL); |
| 548 | int col = col_start; |
| 549 | int row = 0; |
| 550 | tabpage_T *tp = NULL; |
| 551 | typval_T v; |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 552 | tabpanel_T args; |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 553 | |
| 554 | args.maxrow = cmdline_row; |
| 555 | args.offsetrow = 0; |
| 556 | args.col_start = col_start; |
| 557 | args.col_end = col_end; |
| 558 | |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 559 | if (tplmode != TPLMODE_GET_CURTAB_ROW && args.maxrow > 0) |
| 560 | while (args.offsetrow + args.maxrow <= *pcurtab_row) |
| 561 | args.offsetrow += args.maxrow; |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 562 | |
| 563 | tp = first_tabpage; |
| 564 | |
| 565 | for (row = 0; tp != NULL; row++) |
| 566 | { |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 567 | if (tplmode != TPLMODE_GET_CURTAB_ROW |
| 568 | && args.maxrow <= row - args.offsetrow) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 569 | break; |
| 570 | |
| 571 | col = col_start; |
| 572 | |
| 573 | v.v_type = VAR_NUMBER; |
| 574 | v.vval.v_number = tabpage_index(tp); |
| 575 | set_var((char_u *)"g:actual_curtabpage", &v, TRUE); |
| 576 | |
| 577 | if (tp->tp_topframe == topframe) |
| 578 | { |
| 579 | args.attr = attr_tpls; |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 580 | if (tplmode == TPLMODE_GET_CURTAB_ROW) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 581 | { |
| 582 | *pcurtab_row = row; |
| 583 | break; |
| 584 | } |
| 585 | } |
| 586 | else |
| 587 | args.attr = attr_tpl; |
| 588 | |
| 589 | if (tp == curtab) |
| 590 | { |
| 591 | args.cwp = curwin; |
| 592 | args.wp = firstwin; |
| 593 | } |
| 594 | else |
| 595 | { |
| 596 | args.cwp = tp->tp_curwin; |
| 597 | args.wp = tp->tp_firstwin; |
| 598 | } |
| 599 | |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 600 | char_u *usefmt = starts_with_percent_and_bang(&args); |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 601 | if (usefmt != NULL) |
| 602 | { |
| 603 | char_u buf[IOSIZE]; |
| 604 | char_u *p = usefmt; |
| 605 | size_t i = 0; |
| 606 | |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 607 | while (p[i] != NUL) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 608 | { |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 609 | while (p[i] == '\n' || p[i] == '\r') |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 610 | { |
| 611 | // fill the tailing area of current row. |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 612 | if (row - args.offsetrow >= 0 |
| 613 | && row - args.offsetrow < args.maxrow) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 614 | screen_fill_tailing_area(tplmode, |
| 615 | row - args.offsetrow, |
| 616 | row - args.offsetrow + 1, |
| 617 | col, args.col_end, args.attr); |
| 618 | row++; |
| 619 | col = col_start; |
| 620 | p++; |
| 621 | } |
| 622 | |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 623 | while (p[i] != '\n' && p[i] != '\r' && (p[i] != NUL)) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 624 | { |
| 625 | if (i + 1 >= sizeof(buf)) |
| 626 | break; |
| 627 | buf[i] = p[i]; |
| 628 | i++; |
| 629 | } |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 630 | buf[i] = NUL; |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 631 | |
| 632 | args.user_defined = buf; |
| 633 | args.prow = &row; |
| 634 | args.pcol = &col; |
| 635 | draw_tabpanel_userdefined(tplmode, &args); |
| 636 | |
| 637 | p += i; |
| 638 | i = 0; |
| 639 | } |
| 640 | if (usefmt != p_tpl) |
| 641 | VIM_CLEAR(usefmt); |
| 642 | } |
| 643 | else |
| 644 | { |
| 645 | args.user_defined = NULL; |
| 646 | args.prow = &row; |
| 647 | args.pcol = &col; |
| 648 | draw_tabpanel_default(tplmode, &args); |
| 649 | } |
| 650 | |
| 651 | do_unlet((char_u *)"g:actual_curtabpage", TRUE); |
| 652 | |
| 653 | tp = tp->tp_next; |
| 654 | |
Hirohito Higashi | c659e4a | 2025-05-16 19:34:34 +0200 | [diff] [blame] | 655 | if ((tplmode == TPLMODE_GET_TABPAGENR) |
Naruhiko Nishino | be5bd4d | 2025-05-14 21:20:28 +0200 | [diff] [blame] | 656 | && (mouse_row <= (row - args.offsetrow))) |
| 657 | { |
| 658 | *ptabpagenr = v.vval.v_number; |
| 659 | break; |
| 660 | } |
| 661 | } |
| 662 | |
| 663 | // fill the area of TabPanelFill. |
| 664 | screen_fill_tailing_area(tplmode, row - args.offsetrow, args.maxrow, |
| 665 | args.col_start, args.col_end, attr_tplf); |
| 666 | } |
| 667 | |
| 668 | #endif // FEAT_TABPANEL |