# HG changeset patch # User Bram Moolenaar # Date 1581804904 -3600 # Node ID 08f4dc2ba71697925fac98ee5bba029cf03b21f1 # Parent 7be3663e2f2b30084ac9b01a786bbe1802c36f22 patch 8.2.0260: several lines of code are duplicated Commit: https://github.com/vim/vim/commit/f4140488c72cad4dbf5449dba099cfa7de7bbb22 Author: Bram Moolenaar Date: Sat Feb 15 23:06:45 2020 +0100 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) diff --git a/src/option.c b/src/option.c --- a/src/option.c +++ b/src/option.c @@ -2498,6 +2498,61 @@ set_term_option_sctx_idx(char *name, int } #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. * 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; #if defined(FEAT_EVAL) - // Don't do this while starting up or recursively. - if (!starting && *get_vim_var_str(VV_OPTION_TYPE) == NUL) - { - char_u buf_old[2], buf_old_global[2], buf_new[2], buf_type[7]; - - 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(); - } + apply_optionset_autocmd(opt_idx, opt_flags, + (long)(old_value ? TRUE : FALSE), + (long)(old_global_value ? TRUE : FALSE), + (long)(value ? TRUE : FALSE), NULL); #endif comp_col(); // in case 'ruler' or 'showcmd' changed @@ -3666,42 +3686,8 @@ set_num_option( options[opt_idx].flags |= P_WAS_SET; #if defined(FEAT_EVAL) - // Don't do this while starting up, failure or recursively. - if (!starting && errmsg == NULL && *get_vim_var_str(VV_OPTION_TYPE) == NUL) - { - 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(); - } + apply_optionset_autocmd(opt_idx, opt_flags, old_value, old_global_value, + value, errmsg); #endif comp_col(); // in case 'columns' or 'ls' changed diff --git a/src/os_unix.c b/src/os_unix.c --- a/src/os_unix.c +++ b/src/os_unix.c @@ -4980,29 +4980,7 @@ mch_call_shell_fork( } } - // replace K_BS by and K_DEL by - 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; - } + term_replace_bs_del_keycode(ta_buf, ta_len, len); /* * For pipes: echo the typed characters. diff --git a/src/os_win32.c b/src/os_win32.c --- a/src/os_win32.c +++ b/src/os_win32.c @@ -4173,7 +4173,6 @@ mch_system_piped(char *cmd, int options) int ta_len = 0; // valid bytes in ta_buf[] DWORD i; - int c; int noread_cnt = 0; garray_T ga; int delay = 1; @@ -4312,29 +4311,7 @@ mch_system_piped(char *cmd, int options) } } - // replace K_BS by and K_DEL by - 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; - } + term_replace_bs_del_keycode(ta_buf, ta_len, len); /* * For pipes: echo the typed characters. For a pty this diff --git a/src/proto/term.pro b/src/proto/term.pro --- a/src/proto/term.pro +++ b/src/proto/term.pro @@ -77,4 +77,5 @@ void swap_tcap(void); guicolor_T gui_get_color_cmn(char_u *name); 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 term_replace_bs_del_keycode(char_u *ta_buf, int ta_len, int len); /* vim: set ft=c : */ diff --git a/src/quickfix.c b/src/quickfix.c --- a/src/quickfix.c +++ b/src/quickfix.c @@ -980,11 +980,11 @@ qf_parse_fmt_t(regmatch_T *rmp, int midx } /* - * Parse the match for '%+' format pattern. The whole matching line is included - * in the error string. Return the matched line in "fields->errmsg". + * Copy a non-error line into the error string. Return the matched line in + * "fields->errmsg". */ 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; @@ -996,7 +996,9 @@ qf_parse_fmt_plus(char_u *linebuf, int l fields->errmsg = p; fields->errmsglen = linelen + 1; } + // copy whole line to error message vim_strncpy(fields->errmsg, linebuf, linelen); + return QF_OK; } @@ -1180,7 +1182,7 @@ qf_parse_match( else if (i == 5) { 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 status = qf_parse_fmt_m(regmatch, midx, fields); } @@ -1307,23 +1309,11 @@ qf_parse_file_pfx( static int 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->lnum = 0; // don't jump to this line 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); } /* diff --git a/src/regexp.c b/src/regexp.c --- a/src/regexp.c +++ b/src/regexp.c @@ -2511,6 +2511,28 @@ reg_submatch_list(int no) } #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" static regengine_T bt_regengine = diff --git a/src/regexp_bt.c b/src/regexp_bt.c --- a/src/regexp_bt.c +++ b/src/regexp_bt.c @@ -4854,17 +4854,7 @@ bt_regexec_multi( proftime_T *tm, // timeout limit or NULL int *timed_out) // flag set on timeout or NULL { - 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; - + init_regexec_multi(rmp, win, buf, lnum); return bt_regexec_both(NULL, col, tm, timed_out); } diff --git a/src/regexp_nfa.c b/src/regexp_nfa.c --- a/src/regexp_nfa.c +++ b/src/regexp_nfa.c @@ -7409,17 +7409,7 @@ nfa_regexec_multi( proftime_T *tm, // timeout limit or NULL int *timed_out) // flag set on timeout or NULL { - 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; - + init_regexec_multi(rmp, win, buf, lnum); return nfa_regexec_both(NULL, col, tm, timed_out); } diff --git a/src/term.c b/src/term.c --- a/src/term.c +++ b/src/term.c @@ -6390,3 +6390,34 @@ cterm_color2rgb(int nr, char_u *r, char_ } #endif +/* + * Replace K_BS by and K_DEL by + */ + 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; + } +} diff --git a/src/version.c b/src/version.c --- a/src/version.c +++ b/src/version.c @@ -743,6 +743,8 @@ static char *(features[]) = static int included_patches[] = { /* Add new patch number below this line */ /**/ + 260, +/**/ 259, /**/ 258,