]> granicus.if.org Git - vim/commitdiff
patch 8.2.0260: several lines of code are duplicated v8.2.0260
authorBram Moolenaar <Bram@vim.org>
Sat, 15 Feb 2020 22:06:45 +0000 (23:06 +0100)
committerBram Moolenaar <Bram@vim.org>
Sat, 15 Feb 2020 22:06:45 +0000 (23:06 +0100)
Problem:    Several lines of code are duplicated.
Solution:   Move duplicated code to a function. (Yegappan Lakshmanan,
            closes #5330)

src/option.c
src/os_unix.c
src/os_win32.c
src/proto/term.pro
src/quickfix.c
src/regexp.c
src/regexp_bt.c
src/regexp_nfa.c
src/term.c
src/version.c

index f02dd26a96bb2c0f0b5adcff9369704ae628ea24..39ea7cbb4d024aab8bd4e394a85e6cd4b77f9652 100644 (file)
@@ -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
index 15d88f9e15a3cf697d07366f5158b937e04cb355..654480d9ed02d3a7ce8fec901b908fde2518ae9d 100644 (file)
@@ -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.
index f394979d60a834777f4a59545a34c12bc51ea7e7..df63fb7f3ecec1bbe4182489cf289ea2599b927a 100644 (file)
@@ -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
index 4b9ee9ca24aeaa23cfd8d0b0ffae43005a5fb218..2091cbfee8ce2d046a5a5e9edaddaa510bdc34ba 100644 (file)
@@ -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 : */
index 00457e256d3681aa74943d726b98c167edd59130..f1df11121222b3c0c53a76ab70a9bfa53ea2afab 100644 (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
- * 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);
 }
 
 /*
index ef3896c0a4d7a97ae35bfd29454196345da617d4..4e21d44ccf061a2be1465dd6a5967bec4b593f4e 100644 (file)
@@ -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 =
index 78be2b5f8dfc2074bf0a4244f233ce4fec14d2c2..0ed37113632a48552a5d7cba26587143bce03cd0 100644 (file)
@@ -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);
 }
 
index a288ea47c84036ee769ae08c0c7f9d23087dab5c..67d4af2f6177ec74375f1d7ec6c8ac2932edabde 100644 (file)
@@ -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);
 }
 
index b942b0f4b92342010bc48b3eb71886bda00792b3..d269eb83c04ea694f8a2bb4adede866d9e344491 100644 (file)
@@ -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;
+    }
+}
index 7dacf84a38b6040e8c01f9256b9b0e11fbbec526..a77a7c8caeba39e42e0992c08002708b31fae2df 100644 (file)
@@ -742,6 +742,8 @@ static char *(features[]) =
 
 static int included_patches[] =
 {   /* Add new patch number below this line */
+/**/
+    260,
 /**/
     259,
 /**/