diff options
author | Bram Moolenaar <Bram@vim.org> | 2020-02-15 23:06:45 +0100 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2020-02-15 23:06:45 +0100 |
commit | f4140488c72cad4dbf5449dba099cfa7de7bbb22 (patch) | |
tree | ef69c7720d31e0871cd01c41d447a5440e9d8e9c | |
parent | ebdf3c964a901fc00c9009689f7cfda478342c51 (diff) | |
download | vim-git-f4140488c72cad4dbf5449dba099cfa7de7bbb22.tar.gz |
patch 8.2.0260: several lines of code are duplicatedv8.2.0260
Problem: Several lines of code are duplicated.
Solution: Move duplicated code to a function. (Yegappan Lakshmanan,
closes #5330)
-rw-r--r-- | src/option.c | 136 | ||||
-rw-r--r-- | src/os_unix.c | 24 | ||||
-rw-r--r-- | src/os_win32.c | 25 | ||||
-rw-r--r-- | src/proto/term.pro | 1 | ||||
-rw-r--r-- | src/quickfix.c | 24 | ||||
-rw-r--r-- | src/regexp.c | 22 | ||||
-rw-r--r-- | src/regexp_bt.c | 12 | ||||
-rw-r--r-- | src/regexp_nfa.c | 12 | ||||
-rw-r--r-- | src/term.c | 31 | ||||
-rw-r--r-- | src/version.c | 2 |
10 files changed, 128 insertions, 161 deletions
diff --git a/src/option.c b/src/option.c index f02dd26a9..39ea7cbb4 100644 --- a/src/option.c +++ b/src/option.c @@ -2498,6 +2498,61 @@ set_term_option_sctx_idx(char *name, int opt_idx) } #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 index 15d88f9e1..654480d9e 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -4980,29 +4980,7 @@ mch_call_shell_fork( } } - // replace K_BS by <BS> and K_DEL by <DEL> - 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 index f394979d6..df63fb7f3 100644 --- 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 <BS> and K_DEL by <DEL> - 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 index 4b9ee9ca2..2091cbfee 100644 --- 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 index 00457e256..f1df11121 100644 --- a/src/quickfix.c +++ b/src/quickfix.c @@ -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 - * 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 linelen, qffields_T *fields) 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 index ef3896c0a..4e21d44cc 100644 --- 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 index 78be2b5f8..0ed371136 100644 --- 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 index a288ea47c..67d4af2f6 100644 --- 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 index b942b0f4b..d269eb83c 100644 --- a/src/term.c +++ b/src/term.c @@ -6390,3 +6390,34 @@ cterm_color2rgb(int nr, char_u *r, char_u *g, char_u *b, char_u *ansi_idx) } #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; + } +} diff --git a/src/version.c b/src/version.c index 7dacf84a3..a77a7c8ca 100644 --- 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, |