refactor: click definition functions #20923

Need this part of `win_redr_custom()` in `drawline.c` for #20621.
Another refactor is pending in https://github.com/vim/vim/pull/11467
This commit is contained in:
luukvbaal 2022-11-07 04:10:09 +01:00 committed by GitHub
parent ce198102bd
commit 9f125371e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 48 additions and 40 deletions

View File

@ -598,6 +598,49 @@ void stl_clear_click_defs(StlClickDefinition *const click_defs, const long click
}
}
/// Allocate or resize the click definitions array if needed.
StlClickDefinition *stl_alloc_click_defs(StlClickDefinition *cdp, long width, size_t *size)
{
if (*size < (size_t)width) {
xfree(cdp);
*size = (size_t)width;
cdp = xcalloc(*size, sizeof(StlClickDefinition));
}
return cdp;
}
/// Fill the click definitions array if needed.
void stl_fill_click_defs(StlClickDefinition *click_defs, StlClickRecord *click_recs, char *buf,
int width, bool tabline)
{
if (click_defs == NULL) {
return;
}
int col = 0;
int len = 0;
StlClickDefinition cur_click_def = {
.type = kStlClickDisabled,
};
for (int i = 0; click_recs[i].start != NULL; i++) {
len += vim_strnsize(buf, (int)(click_recs[i].start - buf));
while (col < len) {
click_defs[col++] = cur_click_def;
}
buf = (char *)click_recs[i].start;
cur_click_def = click_recs[i].def;
if (!tabline && !(cur_click_def.type == kStlClickDisabled
|| cur_click_def.type == kStlClickFuncRun)) {
// window bar and status line only support click functions
cur_click_def.type = kStlClickDisabled;
}
}
while (col < width) {
click_defs[col++] = cur_click_def;
}
}
/// Set cursor to its position in the current window.
void setcursor(void)
{

View File

@ -470,26 +470,16 @@ void win_redr_custom(win_T *wp, bool draw_winbar, bool draw_ruler)
attr = (wp == curwin) ? win_hl_attr(wp, HLF_WBR) : win_hl_attr(wp, HLF_WBRNC);
maxwidth = wp->w_width_inner;
use_sandbox = was_set_insecurely(wp, "winbar", 0);
stl_clear_click_defs(wp->w_winbar_click_defs, (long)wp->w_winbar_click_defs_size);
// Allocate / resize the click definitions array for winbar if needed.
if (wp->w_winbar_height && wp->w_winbar_click_defs_size < (size_t)maxwidth) {
xfree(wp->w_winbar_click_defs);
wp->w_winbar_click_defs_size = (size_t)maxwidth;
wp->w_winbar_click_defs = xcalloc(wp->w_winbar_click_defs_size, sizeof(StlClickRecord));
}
wp->w_winbar_click_defs = stl_alloc_click_defs(wp->w_winbar_click_defs, maxwidth,
&wp->w_winbar_click_defs_size);
} else {
row = is_stl_global ? (Rows - (int)p_ch - 1) : W_ENDROW(wp);
fillchar = fillchar_status(&attr, wp);
maxwidth = is_stl_global ? Columns : wp->w_width;
stl_clear_click_defs(wp->w_status_click_defs, (long)wp->w_status_click_defs_size);
// Allocate / resize the click definitions array for statusline if needed.
if (wp->w_status_click_defs_size < (size_t)maxwidth) {
xfree(wp->w_status_click_defs);
wp->w_status_click_defs_size = (size_t)maxwidth;
wp->w_status_click_defs = xcalloc(wp->w_status_click_defs_size, sizeof(StlClickRecord));
}
wp->w_status_click_defs = stl_alloc_click_defs(wp->w_status_click_defs, maxwidth,
&wp->w_status_click_defs_size);
if (draw_ruler) {
stl = p_ruf;
@ -597,32 +587,7 @@ void win_redr_custom(win_T *wp, bool draw_winbar, bool draw_ruler)
: draw_winbar ? wp->w_winbar_click_defs
: wp->w_status_click_defs;
if (click_defs == NULL) {
goto theend;
}
col = 0;
len = 0;
p = buf;
StlClickDefinition cur_click_def = {
.type = kStlClickDisabled,
};
for (n = 0; tabtab[n].start != NULL; n++) {
len += vim_strnsize(p, (int)(tabtab[n].start - p));
while (col < len) {
click_defs[col++] = cur_click_def;
}
p = (char *)tabtab[n].start;
cur_click_def = tabtab[n].def;
if ((wp != NULL) && !(cur_click_def.type == kStlClickDisabled
|| cur_click_def.type == kStlClickFuncRun)) {
// window bar and status line only support click functions
cur_click_def.type = kStlClickDisabled;
}
}
while (col < maxwidth) {
click_defs[col++] = cur_click_def;
}
stl_fill_click_defs(click_defs, tabtab, buf, maxwidth, wp == NULL);
theend:
entered = false;