diff options
author | Lewis Russell <lewis6991@gmail.com> | 2023-09-11 10:32:01 +0100 |
---|---|---|
committer | Lewis Russell <me@lewisr.dev> | 2023-09-11 11:40:15 +0100 |
commit | 676e1d490066a5289ba05df128c8710253fc1cf9 (patch) | |
tree | 75c25be5bc1d9c123c266a95266641dd87d3c317 | |
parent | 1d259724a994ac5cf2de82135ef3f4c644df5e9a (diff) | |
download | rneovim-676e1d490066a5289ba05df128c8710253fc1cf9.tar.gz rneovim-676e1d490066a5289ba05df128c8710253fc1cf9.tar.bz2 rneovim-676e1d490066a5289ba05df128c8710253fc1cf9.zip |
refactor(usercmd.c): reduce scope of locals
-rw-r--r-- | src/nvim/usercmd.c | 105 |
1 files changed, 41 insertions, 64 deletions
diff --git a/src/nvim/usercmd.c b/src/nvim/usercmd.c index e585818e75..7ccd13c3a7 100644 --- a/src/nvim/usercmd.c +++ b/src/nvim/usercmd.c @@ -130,23 +130,21 @@ static struct { char *find_ucmd(exarg_T *eap, char *p, int *full, expand_T *xp, int *complp) { int len = (int)(p - eap->cmd); - int j, k, matchlen = 0; - ucmd_T *uc; + int matchlen = 0; bool found = false; bool possible = false; - char *cp, *np; // Point into typed cmd and test name - garray_T *gap; bool amb_local = false; // Found ambiguous buffer-local command, // only full match global is accepted. // Look for buffer-local user commands first, then global ones. - gap = &prevwin_curwin()->w_buffer->b_ucmds; + garray_T *gap = &prevwin_curwin()->w_buffer->b_ucmds; while (true) { + int j; for (j = 0; j < gap->ga_len; j++) { - uc = USER_CMD_GA(gap, j); - cp = eap->cmd; - np = uc->uc_name; - k = 0; + ucmd_T *uc = USER_CMD_GA(gap, j); + char *cp = eap->cmd; + char *np = uc->uc_name; + int k = 0; while (k < len && *np != NUL && *cp++ == *np++) { k++; } @@ -447,17 +445,15 @@ int cmdcomplete_str_to_type(const char *complete_str) static void uc_list(char *name, size_t name_len) { - int i, j; bool found = false; - ucmd_T *cmd; - uint32_t a; // In cmdwin, the alternative buffer should be used. const garray_T *gap = &prevwin_curwin()->w_buffer->b_ucmds; while (true) { + int i; for (i = 0; i < gap->ga_len; i++) { - cmd = USER_CMD_GA(gap, i); - a = cmd->uc_argt; + ucmd_T *cmd = USER_CMD_GA(gap, i); + uint32_t a = cmd->uc_argt; // Skip commands which don't match the requested prefix and // commands filtered out. @@ -559,7 +555,7 @@ static void uc_list(char *name, size_t name_len) } while ((int64_t)len < 8 - over); // Address Type - for (j = 0; addr_type_complete[j].expand != ADDR_NONE; j++) { + for (int j = 0; addr_type_complete[j].expand != ADDR_NONE; j++) { if (addr_type_complete[j].expand != ADDR_LINES && addr_type_complete[j].expand == cmd->uc_addr_type) { int rc = snprintf(IObuff + len, IOSIZE - len, "%s", addr_type_complete[j].shortname); @@ -623,11 +619,11 @@ static void uc_list(char *name, size_t name_len) int parse_addr_type_arg(char *value, int vallen, cmd_addr_T *addr_type_arg) FUNC_ATTR_NONNULL_ALL { - int i, a, b; + int i; for (i = 0; addr_type_complete[i].expand != ADDR_NONE; i++) { - a = (int)strlen(addr_type_complete[i].name) == vallen; - b = strncmp(value, addr_type_complete[i].name, (size_t)vallen) == 0; + int a = (int)strlen(addr_type_complete[i].name) == vallen; + int b = strncmp(value, addr_type_complete[i].name, (size_t)vallen) == 0; if (a && b) { *addr_type_arg = addr_type_complete[i].expand; break; @@ -657,11 +653,10 @@ int parse_compl_arg(const char *value, int vallen, int *complp, uint32_t *argt, { const char *arg = NULL; size_t arglen = 0; - int i; int valend = vallen; // Look for any argument part - which is the part after any ',' - for (i = 0; i < vallen; i++) { + for (int i = 0; i < vallen; i++) { if (value[i] == ',') { arg = (char *)&value[i + 1]; arglen = (size_t)(vallen - i - 1); @@ -670,6 +665,7 @@ int parse_compl_arg(const char *value, int vallen, int *complp, uint32_t *argt, } } + int i; for (i = 0; i < (int)ARRAY_SIZE(command_complete); i++) { if (get_command_complete(i) == NULL) { continue; @@ -713,8 +709,6 @@ static int uc_scan_attr(char *attr, size_t len, uint32_t *argt, long *def, int * char **compl_arg, cmd_addr_T *addr_type_arg) FUNC_ATTR_NONNULL_ALL { - char *p; - if (len == 0) { emsg(_("E175: No attribute specified")); return FAIL; @@ -732,13 +726,12 @@ static int uc_scan_attr(char *attr, size_t len, uint32_t *argt, long *def, int * } else if (STRNICMP(attr, "bar", len) == 0) { *argt |= EX_TRLBAR; } else { - int i; char *val = NULL; size_t vallen = 0; size_t attrlen = len; // Look for the attribute name - which is the part before any '=' - for (i = 0; i < (int)len; i++) { + for (int i = 0; i < (int)len; i++) { if (attr[i] == '=') { val = &attr[i + 1]; vallen = len - (size_t)i - 1; @@ -772,7 +765,7 @@ wrong_nargs: if (vallen == 1 && *val == '%') { *argt |= EX_DFLALL; } else if (val != NULL) { - p = val; + char *p = val; if (*def >= 0) { two_count: emsg(_("E177: Count cannot be specified twice")); @@ -800,7 +793,7 @@ invalid_count: } if (val != NULL) { - p = val; + char *p = val; if (*def >= 0) { goto two_count; } @@ -878,7 +871,6 @@ int uc_add_command(char *name, size_t name_len, const char *rep, uint32_t argt, FUNC_ATTR_NONNULL_ARG(1, 3) { ucmd_T *cmd = NULL; - int i; int cmp = 1; char *rep_buf = NULL; garray_T *gap; @@ -899,12 +891,12 @@ int uc_add_command(char *name, size_t name_len, const char *rep, uint32_t argt, gap = &ucmds; } + int i; + // Search for the command in the already defined commands. for (i = 0; i < gap->ga_len; i++) { - size_t len; - cmd = USER_CMD_GA(gap, i); - len = strlen(cmd->uc_name); + size_t len = strlen(cmd->uc_name); cmp = strncmp(name, cmd->uc_name, name_len); if (cmp == 0) { if (name_len < len) { @@ -980,9 +972,7 @@ fail: /// ":command ..." void ex_command(exarg_T *eap) { - char *name; char *end; - char *p; uint32_t argt = 0; long def = -1; int flags = 0; @@ -990,9 +980,8 @@ void ex_command(exarg_T *eap) char *compl_arg = NULL; cmd_addr_T addr_type_arg = ADDR_NONE; int has_attr = (eap->arg[0] == '-'); - size_t name_len; - p = eap->arg; + char *p = eap->arg; // Check for attributes while (*p == '-') { @@ -1006,13 +995,13 @@ void ex_command(exarg_T *eap) } // Get the name (if any) and skip to the following argument. - name = p; + char *name = p; end = uc_validate_name(name); if (!end) { emsg(_("E182: Invalid command name")); goto theend; } - name_len = (size_t)(end - name); + size_t name_len = (size_t)(end - name); // If there is nothing after the name, and no attributes were specified, // we are listing commands @@ -1065,7 +1054,6 @@ void ex_delcommand(exarg_T *eap) int i = 0; ucmd_T *cmd = NULL; int res = -1; - garray_T *gap; const char *arg = eap->arg; bool buffer_only = false; @@ -1074,7 +1062,7 @@ void ex_delcommand(exarg_T *eap) arg = skipwhite(arg + 7); } - gap = &curbuf->b_ucmds; + garray_T *gap = &curbuf->b_ucmds; while (true) { for (i = 0; i < gap->ga_len; i++) { cmd = USER_CMD_GA(gap, i); @@ -1153,15 +1141,10 @@ bool uc_split_args_iter(const char *arg, size_t arglen, size_t *end, char *buf, static char *uc_split_args(const char *arg, char **args, const size_t *arglens, size_t argc, size_t *lenp) { - char *buf; - const char *p; - char *q; - int len; - // Precalculate length - len = 2; // Initial and final quotes + int len = 2; // Initial and final quotes if (args == NULL) { - p = arg; + const char *p = arg; while (*p) { if (p[0] == '\\' && p[1] == '\\') { @@ -1188,7 +1171,7 @@ static char *uc_split_args(const char *arg, char **args, const size_t *arglens, } } else { for (size_t i = 0; i < argc; i++) { - p = args[i]; + const char *p = args[i]; const char *arg_end = args[i] + arglens[i]; while (p < arg_end) { @@ -1209,13 +1192,13 @@ static char *uc_split_args(const char *arg, char **args, const size_t *arglens, } } - buf = xmalloc((size_t)len + 1); + char *buf = xmalloc((size_t)len + 1); - q = buf; + char *q = buf; *q++ = '"'; if (args == NULL) { - p = arg; + const char *p = arg; while (*p) { if (p[0] == '\\' && p[1] == '\\') { *q++ = '\\'; @@ -1242,7 +1225,7 @@ static char *uc_split_args(const char *arg, char **args, const size_t *arglens, } } else { for (size_t i = 0; i < argc; i++) { - p = args[i]; + const char *p = args[i]; const char *arg_end = args[i] + arglens[i]; while (p < arg_end) { @@ -1622,14 +1605,7 @@ static size_t uc_check_code(char *code, size_t len, char *buf, ucmd_T *cmd, exar int do_ucmd(exarg_T *eap, bool preview) { - char *buf; - char *p; - char *q; - - char *start; char *end = NULL; - char *ksp; - size_t len, totlen; size_t split_len = 0; char *split_buf = NULL; @@ -1654,18 +1630,19 @@ int do_ucmd(exarg_T *eap, bool preview) // Replace <> in the command by the arguments. // First round: "buf" is NULL, compute length, allocate "buf". // Second round: copy result into "buf". - buf = NULL; + char *buf = NULL; while (true) { - p = cmd->uc_rep; // source - q = buf; // destination - totlen = 0; + char *p = cmd->uc_rep; // source + char *q = buf; // destination + size_t totlen = 0; while (true) { - start = vim_strchr(p, '<'); + char *start = vim_strchr(p, '<'); if (start != NULL) { end = vim_strchr(start + 1, '>'); } if (buf != NULL) { + char *ksp; for (ksp = p; *ksp != NUL && (uint8_t)(*ksp) != K_SPECIAL; ksp++) {} if ((uint8_t)(*ksp) == K_SPECIAL && (start == NULL || ksp < start || end == NULL) @@ -1673,7 +1650,7 @@ int do_ucmd(exarg_T *eap, bool preview) // K_SPECIAL has been put in the buffer as K_SPECIAL // KS_SPECIAL KE_FILLER, like for mappings, but // do_cmdline() doesn't handle that, so convert it back. - len = (size_t)(ksp - p); + size_t len = (size_t)(ksp - p); if (len > 0) { memmove(q, p, len); q += len; @@ -1693,7 +1670,7 @@ int do_ucmd(exarg_T *eap, bool preview) end++; // Take everything up to the '<' - len = (size_t)(start - p); + size_t len = (size_t)(start - p); if (buf == NULL) { totlen += len; } else { |