0
0
mirror of https://github.com/vim/vim.git synced 2025-09-30 04:44:14 -04:00

patch 8.1.0772: the sign_define_by_name() function is too long

Problem:    The sign_define_by_name() function is too long.
Solution:   Split it into smaller functions. (Yegappan Lakshmanan,
            closes #3819)
This commit is contained in:
Bram Moolenaar
2019-01-18 22:01:42 +01:00
parent 147e7d0cab
commit 0314236aab
2 changed files with 140 additions and 105 deletions

View File

@@ -727,22 +727,12 @@ sign_find(char_u *name, sign_T **sp_prev)
}
/*
* Define a new sign or update an existing sign
* Allocate a new sign
*/
int
sign_define_by_name(
char_u *name,
char_u *icon,
char_u *linehl,
char_u *text,
char_u *texthl)
static sign_T *
alloc_new_sign(char_u *name)
{
sign_T *sp_prev;
sign_T *sp;
sp = sign_find(name, &sp_prev);
if (sp == NULL)
{
sign_T *lp;
int start = next_sign_typenr;
@@ -750,7 +740,7 @@ sign_define_by_name(
sp = (sign_T *)alloc_clear_id((unsigned)sizeof(sign_T),
aid_sign_define_by_name);
if (sp == NULL)
return FAIL;
return NULL;
// Check that next_sign_typenr is not already being used.
// This only happens after wrapping around. Hopefully
@@ -766,7 +756,7 @@ sign_define_by_name(
{
vim_free(sp);
emsg(_("E612: Too many signs defined"));
return FAIL;
return NULL;
}
lp = first_sign; // start all over
continue;
@@ -782,19 +772,18 @@ sign_define_by_name(
if (sp->sn_name == NULL) // out of memory
{
vim_free(sp);
return FAIL;
return NULL;
}
// add the new sign to the list of signs
if (sp_prev == NULL)
first_sign = sp;
else
sp_prev->sn_next = sp;
}
return sp;
}
// set values for a defined sign.
if (icon != NULL)
{
/*
* Initialize the icon information for a new sign
*/
static void
sign_define_init_icon(sign_T *sp, char_u *icon)
{
vim_free(sp->sn_icon);
sp->sn_icon = vim_strsave(icon);
backslash_halve(sp->sn_icon);
@@ -807,26 +796,31 @@ sign_define_by_name(
sp->sn_image = gui_mch_register_sign(sp->sn_icon);
}
# endif
}
}
if (text != NULL)
{
/*
* Initialize the text for a new sign
*/
static int
sign_define_init_text(sign_T *sp, char_u *text)
{
char_u *s;
char_u *endp;
int cells;
int len;
endp = text + (int)STRLEN(text);
// Remove backslashes so that it is possible to use a space.
for (s = text; s + 1 < endp; ++s)
if (*s == '\\')
{
// Remove a backslash, so that it is possible
// to use a space.
STRMOVE(s, s + 1);
--endp;
}
# ifdef FEAT_MBYTE
// Count cells and check for non-printable chars
# ifdef FEAT_MBYTE
if (has_mbyte)
{
cells = 0;
@@ -845,7 +839,8 @@ sign_define_by_name(
break;
cells = (int)(s - text);
}
// Currently must be one or two display cells
// Currently sign text must be one or two display cells
if (s != endp || cells < 1 || cells > 2)
{
semsg(_("E239: Invalid sign text: %s"), text);
@@ -858,10 +853,48 @@ sign_define_by_name(
len = (int)(endp - text + ((cells == 1) ? 1 : 0));
sp->sn_text = vim_strnsave(text, len);
// For single character sign text, pad with a space.
if (sp->sn_text != NULL && cells == 1)
STRCPY(sp->sn_text + len - 1, " ");
return OK;
}
/*
* Define a new sign or update an existing sign
*/
int
sign_define_by_name(
char_u *name,
char_u *icon,
char_u *linehl,
char_u *text,
char_u *texthl)
{
sign_T *sp_prev;
sign_T *sp;
sp = sign_find(name, &sp_prev);
if (sp == NULL)
{
sp = alloc_new_sign(name);
if (sp == NULL)
return FAIL;
// add the new sign to the list of signs
if (sp_prev == NULL)
first_sign = sp;
else
sp_prev->sn_next = sp;
}
// set values for a defined sign.
if (icon != NULL)
sign_define_init_icon(sp, icon);
if (text != NULL && (sign_define_init_text(sp, text) == FAIL))
return FAIL;
if (linehl != NULL)
sp->sn_line_hl = syn_check_group(linehl, (int)STRLEN(linehl));

View File

@@ -791,6 +791,8 @@ static char *(features[]) =
static int included_patches[] =
{ /* Add new patch number below this line */
/**/
772,
/**/
771,
/**/