Skip to content

Commit f414048

Browse files
committed
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)
1 parent ebdf3c9 commit f414048

10 files changed

Lines changed: 128 additions & 161 deletions

File tree

src/option.c

Lines changed: 61 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -2498,6 +2498,61 @@ set_term_option_sctx_idx(char *name, int opt_idx)
24982498
}
24992499
#endif
25002500

2501+
#if defined(FEAT_EVAL)
2502+
/*
2503+
* Apply the OptionSet autocommand.
2504+
*/
2505+
static void
2506+
apply_optionset_autocmd(
2507+
int opt_idx,
2508+
long opt_flags,
2509+
long oldval,
2510+
long oldval_g,
2511+
long newval,
2512+
char *errmsg)
2513+
{
2514+
char_u buf_old[12], buf_old_global[12], buf_new[12], buf_type[12];
2515+
2516+
// Don't do this while starting up, failure or recursively.
2517+
if (starting || errmsg != NULL || *get_vim_var_str(VV_OPTION_TYPE) != NUL)
2518+
return;
2519+
2520+
vim_snprintf((char *)buf_old, sizeof(buf_old), "%ld", oldval);
2521+
vim_snprintf((char *)buf_old_global, sizeof(buf_old_global), "%ld",
2522+
oldval_g);
2523+
vim_snprintf((char *)buf_new, sizeof(buf_new), "%ld", newval);
2524+
vim_snprintf((char *)buf_type, sizeof(buf_type), "%s",
2525+
(opt_flags & OPT_LOCAL) ? "local" : "global");
2526+
set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
2527+
set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
2528+
set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
2529+
if (opt_flags & OPT_LOCAL)
2530+
{
2531+
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
2532+
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2533+
}
2534+
if (opt_flags & OPT_GLOBAL)
2535+
{
2536+
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
2537+
set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
2538+
}
2539+
if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
2540+
{
2541+
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
2542+
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2543+
set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
2544+
}
2545+
if (opt_flags & OPT_MODELINE)
2546+
{
2547+
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
2548+
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
2549+
}
2550+
apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
2551+
NULL, FALSE, NULL);
2552+
reset_v_option_vars();
2553+
}
2554+
#endif
2555+
25012556
/*
25022557
* Set the value of a boolean option, and take care of side effects.
25032558
* Returns NULL for success, or an error message for an error.
@@ -3071,45 +3126,10 @@ set_bool_option(
30713126
options[opt_idx].flags |= P_WAS_SET;
30723127

30733128
#if defined(FEAT_EVAL)
3074-
// Don't do this while starting up or recursively.
3075-
if (!starting && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
3076-
{
3077-
char_u buf_old[2], buf_old_global[2], buf_new[2], buf_type[7];
3078-
3079-
vim_snprintf((char *)buf_old, 2, "%d", old_value ? TRUE: FALSE);
3080-
vim_snprintf((char *)buf_old_global, 2, "%d",
3081-
old_global_value ? TRUE: FALSE);
3082-
vim_snprintf((char *)buf_new, 2, "%d", value ? TRUE: FALSE);
3083-
vim_snprintf((char *)buf_type, 7, "%s",
3084-
(opt_flags & OPT_LOCAL) ? "local" : "global");
3085-
set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3086-
set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3087-
set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3088-
if (opt_flags & OPT_LOCAL)
3089-
{
3090-
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3091-
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3092-
}
3093-
if (opt_flags & OPT_GLOBAL)
3094-
{
3095-
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3096-
set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3097-
}
3098-
if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3099-
{
3100-
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3101-
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3102-
set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3103-
}
3104-
if (opt_flags & OPT_MODELINE)
3105-
{
3106-
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3107-
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3108-
}
3109-
apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3110-
NULL, FALSE, NULL);
3111-
reset_v_option_vars();
3112-
}
3129+
apply_optionset_autocmd(opt_idx, opt_flags,
3130+
(long)(old_value ? TRUE : FALSE),
3131+
(long)(old_global_value ? TRUE : FALSE),
3132+
(long)(value ? TRUE : FALSE), NULL);
31133133
#endif
31143134

31153135
comp_col(); // in case 'ruler' or 'showcmd' changed
@@ -3666,42 +3686,8 @@ set_num_option(
36663686
options[opt_idx].flags |= P_WAS_SET;
36673687

36683688
#if defined(FEAT_EVAL)
3669-
// Don't do this while starting up, failure or recursively.
3670-
if (!starting && errmsg == NULL && *get_vim_var_str(VV_OPTION_TYPE) == NUL)
3671-
{
3672-
char_u buf_old[11], buf_old_global[11], buf_new[11], buf_type[7];
3673-
vim_snprintf((char *)buf_old, 10, "%ld", old_value);
3674-
vim_snprintf((char *)buf_old_global, 10, "%ld", old_global_value);
3675-
vim_snprintf((char *)buf_new, 10, "%ld", value);
3676-
vim_snprintf((char *)buf_type, 7, "%s", (opt_flags & OPT_LOCAL) ? "local" : "global");
3677-
set_vim_var_string(VV_OPTION_NEW, buf_new, -1);
3678-
set_vim_var_string(VV_OPTION_OLD, buf_old, -1);
3679-
set_vim_var_string(VV_OPTION_TYPE, buf_type, -1);
3680-
if (opt_flags & OPT_LOCAL)
3681-
{
3682-
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setlocal", -1);
3683-
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3684-
}
3685-
if (opt_flags & OPT_GLOBAL)
3686-
{
3687-
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"setglobal", -1);
3688-
set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old, -1);
3689-
}
3690-
if ((opt_flags & (OPT_LOCAL | OPT_GLOBAL)) == 0)
3691-
{
3692-
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"set", -1);
3693-
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3694-
set_vim_var_string(VV_OPTION_OLDGLOBAL, buf_old_global, -1);
3695-
}
3696-
if (opt_flags & OPT_MODELINE)
3697-
{
3698-
set_vim_var_string(VV_OPTION_COMMAND, (char_u *)"modeline", -1);
3699-
set_vim_var_string(VV_OPTION_OLDLOCAL, buf_old, -1);
3700-
}
3701-
apply_autocmds(EVENT_OPTIONSET, (char_u *)options[opt_idx].fullname,
3702-
NULL, FALSE, NULL);
3703-
reset_v_option_vars();
3704-
}
3689+
apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value,
3690+
value, errmsg);
37053691
#endif
37063692

37073693
comp_col(); // in case 'columns' or 'ls' changed

src/os_unix.c

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4980,29 +4980,7 @@ mch_call_shell_fork(
49804980
}
49814981
}
49824982

4983-
// replace K_BS by <BS> and K_DEL by <DEL>
4984-
for (i = ta_len; i < ta_len + len; ++i)
4985-
{
4986-
if (ta_buf[i] == CSI && len - i > 2)
4987-
{
4988-
c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4989-
if (c == K_DEL || c == K_KDEL || c == K_BS)
4990-
{
4991-
mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4992-
(size_t)(len - i - 2));
4993-
if (c == K_DEL || c == K_KDEL)
4994-
ta_buf[i] = DEL;
4995-
else
4996-
ta_buf[i] = Ctrl_H;
4997-
len -= 2;
4998-
}
4999-
}
5000-
else if (ta_buf[i] == '\r')
5001-
ta_buf[i] = '\n';
5002-
if (has_mbyte)
5003-
i += (*mb_ptr2len_len)(ta_buf + i,
5004-
ta_len + len - i) - 1;
5005-
}
4983+
term_replace_bs_del_keycode(ta_buf, ta_len, len);
50064984

50074985
/*
50084986
* For pipes: echo the typed characters.

src/os_win32.c

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4173,7 +4173,6 @@ mch_system_piped(char *cmd, int options)
41734173
int ta_len = 0; // valid bytes in ta_buf[]
41744174

41754175
DWORD i;
4176-
int c;
41774176
int noread_cnt = 0;
41784177
garray_T ga;
41794178
int delay = 1;
@@ -4312,29 +4311,7 @@ mch_system_piped(char *cmd, int options)
43124311
}
43134312
}
43144313

4315-
// replace K_BS by <BS> and K_DEL by <DEL>
4316-
for (i = ta_len; i < ta_len + len; ++i)
4317-
{
4318-
if (ta_buf[i] == CSI && len - i > 2)
4319-
{
4320-
c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
4321-
if (c == K_DEL || c == K_KDEL || c == K_BS)
4322-
{
4323-
mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
4324-
(size_t)(len - i - 2));
4325-
if (c == K_DEL || c == K_KDEL)
4326-
ta_buf[i] = DEL;
4327-
else
4328-
ta_buf[i] = Ctrl_H;
4329-
len -= 2;
4330-
}
4331-
}
4332-
else if (ta_buf[i] == '\r')
4333-
ta_buf[i] = '\n';
4334-
if (has_mbyte)
4335-
i += (*mb_ptr2len_len)(ta_buf + i,
4336-
ta_len + len - i) - 1;
4337-
}
4314+
term_replace_bs_del_keycode(ta_buf, ta_len, len);
43384315

43394316
/*
43404317
* For pipes: echo the typed characters. For a pty this

src/proto/term.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,4 +77,5 @@ void swap_tcap(void);
7777
guicolor_T gui_get_color_cmn(char_u *name);
7878
guicolor_T gui_get_rgb_color_cmn(int r, int g, int b);
7979
void cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx);
80+
void term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len);
8081
/* vim: set ft=c : */

src/quickfix.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -980,11 +980,11 @@ qf_parse_fmt_t(regmatch_T *rmp, int midx, qffields_T *fields)
980980
}
981981

982982
/*
983-
* Parse the match for '%+' format pattern. The whole matching line is included
984-
* in the error string. Return the matched line in "fields->errmsg".
983+
* Copy a non-error line into the error string. Return the matched line in
984+
* "fields->errmsg".
985985
*/
986986
static int
987-
qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
987+
copy_nonerror_line(char_u *linebuf, int linelen, qffields_T *fields)
988988
{
989989
char_u *p;
990990

@@ -996,7 +996,9 @@ qf_parse_fmt_plus(char_u *linebuf, int linelen, qffields_T *fields)
996996
fields->errmsg = p;
997997
fields->errmsglen = linelen + 1;
998998
}
999+
// copy whole line to error message
9991000
vim_strncpy(fields->errmsg, linebuf, linelen);
1001+
10001002
return QF_OK;
10011003
}
10021004

@@ -1180,7 +1182,7 @@ qf_parse_match(
11801182
else if (i == 5)
11811183
{
11821184
if (fmt_ptr->flags == '+' && !qf_multiscan) // %+
1183-
status = qf_parse_fmt_plus(linebuf, linelen, fields);
1185+
status = copy_nonerror_line(linebuf, linelen, fields);
11841186
else if (midx > 0) // %m
11851187
status = qf_parse_fmt_m(regmatch, midx, fields);
11861188
}
@@ -1307,23 +1309,11 @@ qf_parse_file_pfx(
13071309
static int
13081310
qf_parse_line_nomatch(char_u *linebuf, int linelen, qffields_T *fields)
13091311
{
1310-
char_u *p;
1311-
13121312
fields->namebuf[0] = NUL; // no match found, remove file name
13131313
fields->lnum = 0; // don't jump to this line
13141314
fields->valid = FALSE;
1315-
if (linelen >= fields->errmsglen)
1316-
{
1317-
// linelen + null terminator
1318-
if ((p = vim_realloc(fields->errmsg, linelen + 1)) == NULL)
1319-
return QF_NOMEM;
1320-
fields->errmsg = p;
1321-
fields->errmsglen = linelen + 1;
1322-
}
1323-
// copy whole line to error message
1324-
vim_strncpy(fields->errmsg, linebuf, linelen);
13251315

1326-
return QF_OK;
1316+
return copy_nonerror_line(linebuf, linelen, fields);
13271317
}
13281318

13291319
/*

src/regexp.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2511,6 +2511,28 @@ reg_submatch_list(int no)
25112511
}
25122512
#endif
25132513

2514+
/*
2515+
* Initialize the values used for matching against multiple lines
2516+
*/
2517+
static void
2518+
init_regexec_multi(
2519+
regmmatch_T *rmp,
2520+
win_T *win, // window in which to search or NULL
2521+
buf_T *buf, // buffer in which to search
2522+
linenr_T lnum) // nr of line to start looking for match
2523+
{
2524+
rex.reg_match = NULL;
2525+
rex.reg_mmatch = rmp;
2526+
rex.reg_buf = buf;
2527+
rex.reg_win = win;
2528+
rex.reg_firstlnum = lnum;
2529+
rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
2530+
rex.reg_line_lbr = FALSE;
2531+
rex.reg_ic = rmp->rmm_ic;
2532+
rex.reg_icombine = FALSE;
2533+
rex.reg_maxcol = rmp->rmm_maxcol;
2534+
}
2535+
25142536
#include "regexp_bt.c"
25152537

25162538
static regengine_T bt_regengine =

src/regexp_bt.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4854,17 +4854,7 @@ bt_regexec_multi(
48544854
proftime_T *tm, // timeout limit or NULL
48554855
int *timed_out) // flag set on timeout or NULL
48564856
{
4857-
rex.reg_match = NULL;
4858-
rex.reg_mmatch = rmp;
4859-
rex.reg_buf = buf;
4860-
rex.reg_win = win;
4861-
rex.reg_firstlnum = lnum;
4862-
rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
4863-
rex.reg_line_lbr = FALSE;
4864-
rex.reg_ic = rmp->rmm_ic;
4865-
rex.reg_icombine = FALSE;
4866-
rex.reg_maxcol = rmp->rmm_maxcol;
4867-
4857+
init_regexec_multi(rmp, win, buf, lnum);
48684858
return bt_regexec_both(NULL, col, tm, timed_out);
48694859
}
48704860

src/regexp_nfa.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7409,17 +7409,7 @@ nfa_regexec_multi(
74097409
proftime_T *tm, // timeout limit or NULL
74107410
int *timed_out) // flag set on timeout or NULL
74117411
{
7412-
rex.reg_match = NULL;
7413-
rex.reg_mmatch = rmp;
7414-
rex.reg_buf = buf;
7415-
rex.reg_win = win;
7416-
rex.reg_firstlnum = lnum;
7417-
rex.reg_maxline = rex.reg_buf->b_ml.ml_line_count - lnum;
7418-
rex.reg_line_lbr = FALSE;
7419-
rex.reg_ic = rmp->rmm_ic;
7420-
rex.reg_icombine = FALSE;
7421-
rex.reg_maxcol = rmp->rmm_maxcol;
7422-
7412+
init_regexec_multi(rmp, win, buf, lnum);
74237413
return nfa_regexec_both(NULL, col, tm, timed_out);
74247414
}
74257415

src/term.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6390,3 +6390,34 @@ cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx)
63906390
}
63916391
#endif
63926392

6393+
/*
6394+
* Replace K_BS by <BS> and K_DEL by <DEL>
6395+
*/
6396+
void
6397+
term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len)
6398+
{
6399+
int i;
6400+
int c;
6401+
6402+
for (i = ta_len; i < ta_len + len; ++i)
6403+
{
6404+
if (ta_buf[i] == CSI && len - i > 2)
6405+
{
6406+
c = TERMCAP2KEY(ta_buf[i + 1], ta_buf[i + 2]);
6407+
if (c == K_DEL || c == K_KDEL || c == K_BS)
6408+
{
6409+
mch_memmove(ta_buf + i + 1, ta_buf + i + 3,
6410+
(size_t)(len - i - 2));
6411+
if (c == K_DEL || c == K_KDEL)
6412+
ta_buf[i] = DEL;
6413+
else
6414+
ta_buf[i] = Ctrl_H;
6415+
len -= 2;
6416+
}
6417+
}
6418+
else if (ta_buf[i] == '\r')
6419+
ta_buf[i] = '\n';
6420+
if (has_mbyte)
6421+
i += (*mb_ptr2len_len)(ta_buf + i, ta_len + len - i) - 1;
6422+
}
6423+
}

0 commit comments

Comments
 (0)