0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

patch 8.2.0260: several lines of code are duplicated

Problem:    Several lines of code are duplicated.
Solution:   Move duplicated code to a function. (Yegappan Lakshmanan,
            closes #5330)
This commit is contained in:
Bram Moolenaar 2020-02-15 23:06:45 +01:00
parent ebdf3c964a
commit f4140488c7
10 changed files with 128 additions and 161 deletions

View File

@ -2498,6 +2498,61 @@ set_term_option_sctx_idx(char *name, int opt_idx)
} }
#endif #endif
#if defined(FEAT_EVAL)
/*
* Apply the OptionSet autocommand.
*/
static void
apply_optionset_autocmd(
int opt_idx,
long opt_flags,
long oldval,
long oldval_g,
long newval,
char *errmsg)
{
char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
// Don't do this while starting up, failure or recursively.
if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
return;
vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
oldval_g);
vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
(opt_flags & OPT_LOCAL) ? "local" : "global");
set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
if (opt_flags & OPT_LOCAL)
{
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
}
if (opt_flags & OPT_GLOBAL)
{
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
}
if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
{
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
}
if (opt_flags & OPT_MODELINE)
{
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
}
apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
NULL, FALSE, NULL);
reset_v_option_vars();
}
#endif
/* /*
* Set the value of a boolean option, and take care of side effects. * Set the value of a boolean option, and take care of side effects.
* Returns NULL for success, or an error message for an error. * Returns NULL for success, or an error message for an error.
@ -3071,45 +3126,10 @@ set_bool_option(
options[opt_idx].flags |= P_WAS_SET; options[opt_idx].flags |= P_WAS_SET;
#if defined(FEAT_EVAL) #if defined(FEAT_EVAL)
// Don't do this while starting up or recursively. apply_optionset_autocmd(opt_idx, opt_flags,
if (!starting && *get_vim_var_str(VV_OPTION_TYPE) == NUL) (long)(old_value ? TRUE : FALSE),
{ (long)(old_global_value ? TRUE : FALSE),
char_u buf_old[2], buf_old_global[2], buf_new[2], buf_type[7]; (long)(value ? TRUE : FALSE), NULL);
vim_snprintf((char *)buf_old, 2, "%d", old_value ? TRUE: FALSE);
vim_snprintf((char *)buf_old_global, 2, "%d",
old_global_value ? TRUE: FALSE);
vim_snprintf((char *)buf_new, 2, "%d", value ? TRUE: FALSE);
vim_snprintf((char *)buf_type, 7, "%s",
(opt_flags & OPT_LOCAL) ? "local" : "global");
set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
if (opt_flags & OPT_LOCAL)
{
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
}
if (opt_flags & OPT_GLOBAL)
{
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
}
if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
{
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
}
if (opt_flags & OPT_MODELINE)
{
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
}
apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
NULL, FALSE, NULL);
reset_v_option_vars();
}
#endif #endif
comp_col(); // in case 'ruler' or 'showcmd' changed comp_col(); // in case 'ruler' or 'showcmd' changed
@ -3666,42 +3686,8 @@ set_num_option(
options[opt_idx].flags |= P_WAS_SET; options[opt_idx].flags |= P_WAS_SET;
#if defined(FEAT_EVAL) #if defined(FEAT_EVAL)
// Don't do this while starting up, failure or recursively. apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
if (!starting && errmsg == NULL && *get_vim_var_str(VV_OPTION_TYPE) == NUL) value, errmsg);
{
char_u buf_old[11], buf_old_global[11], buf_new[11], buf_type[7];
vim_snprintf((char *)buf_old, 10, "%ld", old_value);
vim_snprintf((char *)buf_old_global, 10, "%ld", old_global_value);
vim_snprintf((char *)buf_new, 10, "%ld", value);
vim_snprintf((char *)buf_type, 7, "%s", (opt_flags & OPT_LOCAL) ? "local" : "global");
set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
if (opt_flags & OPT_LOCAL)
{
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
}
if (opt_flags & OPT_GLOBAL)
{
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
}
if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
{
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
}
if (opt_flags & OPT_MODELINE)
{
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
}
apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
NULL, FALSE, NULL);
reset_v_option_vars();
}
#endif #endif
comp_col(); // in case 'columns' or 'ls' changed comp_col(); // in case 'columns' or 'ls' changed

View File

@ -4980,29 +4980,7 @@ mch_call_shell_fork(
} }
} }
// replace K_BS by <BS> and K_DEL by <DEL> term_replace_bs_del_keycode(ta_buf, ta_len, len);
for (i = ta_len; i < ta_len + len; ++i)
{
if (ta_buf[i] == CSI && len - i > 2)
{
c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
if (c == K_DEL || c == K_KDEL || c == K_BS)
{
mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
(size_t)(len - i - 2));
if (c == K_DEL || c == K_KDEL)
ta_buf[i] = DEL;
else
ta_buf[i] = Ctrl_H;
len -= 2;
}
}
else if (ta_buf[i] == '\r')
ta_buf[i] = '\n';
if (has_mbyte)
i += (*mb_ptr2len_len)(ta_buf + i,
ta_len + len - i) - 1;
}
/* /*
* For pipes: echo the typed characters. * For pipes: echo the typed characters.

View File

@ -4173,7 +4173,6 @@ mch_system_piped(char *cmd, int options)
int ta_len = 0; // valid bytes in ta_buf[] int ta_len = 0; // valid bytes in ta_buf[]
DWORD i; DWORD i;
int c;
int noread_cnt = 0; int noread_cnt = 0;
garray_T ga; garray_T ga;
int delay = 1; int delay = 1;
@ -4312,29 +4311,7 @@ mch_system_piped(char *cmd, int options)
} }
} }
// replace K_BS by <BS> and K_DEL by <DEL> term_replace_bs_del_keycode(ta_buf, ta_len, len);
for (i = ta_len; i < ta_len + len; ++i)
{
if (ta_buf[i] == CSI && len - i > 2)
{
c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
if (c == K_DEL || c == K_KDEL || c == K_BS)
{
mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
(size_t)(len - i - 2));
if (c == K_DEL || c == K_KDEL)
ta_buf[i] = DEL;
else
ta_buf[i] = Ctrl_H;
len -= 2;
}
}
else if (ta_buf[i] == '\r')
ta_buf[i] = '\n';
if (has_mbyte)
i += (*mb_ptr2len_len)(ta_buf + i,
ta_len + len - i) - 1;
}
/* /*
* For pipes: echo the typed characters. For a pty this * For pipes: echo the typed characters. For a pty this

View File

@ -77,4 +77,5 @@ void swap_tcap(void);
guicolor_T gui_get_color_cmn(char_u *name); guicolor_T gui_get_color_cmn(char_u *name);
guicolor_T gui_get_rgb_color_cmn(int r, int g, int b); guicolor_T gui_get_rgb_color_cmn(int r, int g, int b);
void cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx); void cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx);
void term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len);
/* vim: set ft=c : */ /* vim: set ft=c : */

View File

@ -980,11 +980,11 @@ qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
} }
/* /*
* Parse the match for '%+' format pattern. The whole matching line is included * Copy a non-error line into the error string. Return the matched line in
* in the error string. Return the matched line in "fields->errmsg". * "fields->errmsg".
*/ */
static int static int
qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields) copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
{ {
char_u *p; char_u *p;
@ -996,7 +996,9 @@ qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
fields->errmsg = p; fields->errmsg = p;
fields->errmsglen = linelen + 1; fields->errmsglen = linelen + 1;
} }
// copy whole line to error message
vim_strncpy(fields->errmsg, linebuf, linelen); vim_strncpy(fields->errmsg, linebuf, linelen);
return QF_OK; return QF_OK;
} }
@ -1180,7 +1182,7 @@ qf_parse_match(
else if (i == 5) else if (i == 5)
{ {
if (fmt_ptr->flags == '+' && !qf_multiscan) // %+ if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
status = qf_parse_fmt_plus(linebuf, linelen, fields); status = copy_nonerror_line(linebuf, linelen, fields);
else if (midx > 0) // %m else if (midx > 0) // %m
status = qf_parse_fmt_m(regmatch, midx, fields); status = qf_parse_fmt_m(regmatch, midx, fields);
} }
@ -1307,23 +1309,11 @@ qf_parse_file_pfx(
static int static int
qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields) qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
{ {
char_u *p;
fields->namebuf[0] = NUL; // no match found, remove file name fields->namebuf[0] = NUL; // no match found, remove file name
fields->lnum = 0; // don't jump to this line fields->lnum = 0; // don't jump to this line
fields->valid = FALSE; fields->valid = FALSE;
if (linelen >= fields->errmsglen)
{
// linelen + null terminator
if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
return QF_NOMEM;
fields->errmsg = p;
fields->errmsglen = linelen + 1;
}
// copy whole line to error message
vim_strncpy(fields->errmsg, linebuf, linelen);
return QF_OK; return copy_nonerror_line(linebuf, linelen, fields);
} }
/* /*

View File

@ -2511,6 +2511,28 @@ reg_submatch_list(int no)
} }
#endif #endif
/*
* Initialize the values used for matching against multiple lines
*/
static void
init_regexec_multi(
regmmatch_T *rmp,
win_T *win, // window in which to search or NULL
buf_T *buf, // buffer in which to search
linenr_T lnum) // nr of line to start looking for match
{
rex.reg_match = NULL;
rex.reg_mmatch = rmp;
rex.reg_buf = buf;
rex.reg_win = win;
rex.reg_firstlnum = lnum;
rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
rex.reg_line_lbr = FALSE;
rex.reg_ic = rmp->rmm_ic;
rex.reg_icombine = FALSE;
rex.reg_maxcol = rmp->rmm_maxcol;
}
#include "regexp_bt.c" #include "regexp_bt.c"
static regengine_T bt_regengine = static regengine_T bt_regengine =

View File

@ -4854,17 +4854,7 @@ bt_regexec_multi(
proftime_T *tm, // timeout limit or NULL proftime_T *tm, // timeout limit or NULL
int *timed_out) // flag set on timeout or NULL int *timed_out) // flag set on timeout or NULL
{ {
rex.reg_match = NULL; init_regexec_multi(rmp, win, buf, lnum);
rex.reg_mmatch = rmp;
rex.reg_buf = buf;
rex.reg_win = win;
rex.reg_firstlnum = lnum;
rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
rex.reg_line_lbr = FALSE;
rex.reg_ic = rmp->rmm_ic;
rex.reg_icombine = FALSE;
rex.reg_maxcol = rmp->rmm_maxcol;
return bt_regexec_both(NULL, col, tm, timed_out); return bt_regexec_both(NULL, col, tm, timed_out);
} }

View File

@ -7409,17 +7409,7 @@ nfa_regexec_multi(
proftime_T *tm, // timeout limit or NULL proftime_T *tm, // timeout limit or NULL
int *timed_out) // flag set on timeout or NULL int *timed_out) // flag set on timeout or NULL
{ {
rex.reg_match = NULL; init_regexec_multi(rmp, win, buf, lnum);
rex.reg_mmatch = rmp;
rex.reg_buf = buf;
rex.reg_win = win;
rex.reg_firstlnum = lnum;
rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
rex.reg_line_lbr = FALSE;
rex.reg_ic = rmp->rmm_ic;
rex.reg_icombine = FALSE;
rex.reg_maxcol = rmp->rmm_maxcol;
return nfa_regexec_both(NULL, col, tm, timed_out); return nfa_regexec_both(NULL, col, tm, timed_out);
} }

View File

@ -6390,3 +6390,34 @@ cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
} }
#endif #endif
/*
* Replace K_BS by <BS> and K_DEL by <DEL>
*/
void
term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len)
{
int i;
int c;
for (i = ta_len; i < ta_len + len; ++i)
{
if (ta_buf[i] == CSI && len - i > 2)
{
c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
if (c == K_DEL || c == K_KDEL || c == K_BS)
{
mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
(size_t)(len - i - 2));
if (c == K_DEL || c == K_KDEL)
ta_buf[i] = DEL;
else
ta_buf[i] = Ctrl_H;
len -= 2;
}
}
else if (ta_buf[i] == '\r')
ta_buf[i] = '\n';
if (has_mbyte)
i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
}
}

View File

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