refactor(cmd): format do_one_cmd()

- Comment style
- Minimise scope of locals
This commit is contained in:
Lewis Russell 2022-07-27 14:11:51 +01:00
parent 0a049c322f
commit 1f847edc63

View File

@ -1724,16 +1724,11 @@ end:
static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter fgetline, static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter fgetline,
void *cookie) void *cookie)
{ {
char *p;
linenr_T lnum;
char *errormsg = NULL; // error message char *errormsg = NULL; // error message
char *after_modifier = NULL;
exarg_T ea;
cmdmod_T save_cmdmod;
const int save_reg_executing = reg_executing; const int save_reg_executing = reg_executing;
const bool save_pending_end_reg_executing = pending_end_reg_executing; const bool save_pending_end_reg_executing = pending_end_reg_executing;
char *cmd;
exarg_T ea;
memset(&ea, 0, sizeof(ea)); memset(&ea, 0, sizeof(ea));
ea.line1 = 1; ea.line1 = 1;
ea.line2 = 1; ea.line2 = 1;
@ -1749,11 +1744,9 @@ static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter
--quitmore; --quitmore;
} }
/* // Reset browse, confirm, etc.. They are restored when returning, for
* Reset browse, confirm, etc.. They are restored when returning, for // recursive calls.
* recursive calls. cmdmod_T save_cmdmod = cmdmod;
*/
save_cmdmod = cmdmod;
// "#!anything" is handled like a comment. // "#!anything" is handled like a comment.
if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!') { if ((*cmdlinep)[0] == '#' && (*cmdlinep)[1] == '!') {
@ -1775,7 +1768,7 @@ static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter
} }
apply_cmdmod(&cmdmod); apply_cmdmod(&cmdmod);
after_modifier = ea.cmd; char *after_modifier = ea.cmd;
ea.skip = (did_emsg ea.skip = (did_emsg
|| got_int || got_int
@ -1786,12 +1779,12 @@ static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter
// 3. Skip over the range to find the command. Let "p" point to after it. // 3. Skip over the range to find the command. Let "p" point to after it.
// //
// We need the command to know what kind of range it uses. // We need the command to know what kind of range it uses.
cmd = ea.cmd; char *cmd = ea.cmd;
ea.cmd = skip_range(ea.cmd, NULL); ea.cmd = skip_range(ea.cmd, NULL);
if (*ea.cmd == '*') { if (*ea.cmd == '*') {
ea.cmd = skipwhite(ea.cmd + 1); ea.cmd = skipwhite(ea.cmd + 1);
} }
p = find_ex_command(&ea, NULL); char *p = find_ex_command(&ea, NULL);
// Count this line for profiling if skip is TRUE. // Count this line for profiling if skip is TRUE.
if (do_profiling == PROF_YES if (do_profiling == PROF_YES
@ -1855,19 +1848,13 @@ static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter
goto doend; goto doend;
} }
/* // 5. Parse the command.
* 5. Parse the command.
*/
/* // Skip ':' and any white space
* Skip ':' and any white space
*/
ea.cmd = skip_colon_white(ea.cmd, true); ea.cmd = skip_colon_white(ea.cmd, true);
/* // If we got a line, but no command, then go to the line.
* If we got a line, but no command, then go to the line. // If we find a '|' or '\n' we set ea.nextcmd.
* If we find a '|' or '\n' we set ea.nextcmd.
*/
if (*ea.cmd == NUL || *ea.cmd == '"' if (*ea.cmd == NUL || *ea.cmd == '"'
|| (ea.nextcmd = (char *)check_nextcmd((char_u *)ea.cmd)) != NULL) { || (ea.nextcmd = (char *)check_nextcmd((char_u *)ea.cmd)) != NULL) {
// strange vi behaviour: // strange vi behaviour:
@ -2012,10 +1999,8 @@ static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter
goto doend; goto doend;
} }
/* // Don't complain about the range if it is not used
* Don't complain about the range if it is not used // (could happen if line_count is accidentally set to 0).
* (could happen if line_count is accidentally set to 0).
*/
if (!ea.skip && !ni && (ea.argt & EX_RANGE)) { if (!ea.skip && !ni && (ea.argt & EX_RANGE)) {
// If the range is backwards, ask for confirmation and, if given, swap // If the range is backwards, ask for confirmation and, if given, swap
// ea.line1 & ea.line2 so it's forwards again. // ea.line1 & ea.line2 so it's forwards again.
@ -2030,7 +2015,7 @@ static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter
goto doend; goto doend;
} }
} }
lnum = ea.line1; linenr_T lnum = ea.line1;
ea.line1 = ea.line2; ea.line1 = ea.line2;
ea.line2 = lnum; ea.line2 = lnum;
} }
@ -2054,19 +2039,15 @@ static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter
(void)hasFolding(ea.line2, NULL, &ea.line2); (void)hasFolding(ea.line2, NULL, &ea.line2);
} }
/* // For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg'
* For the ":make" and ":grep" commands we insert the 'makeprg'/'grepprg' // option here, so things like % get expanded.
* option here, so things like % get expanded.
*/
p = replace_makeprg(&ea, p, cmdlinep); p = replace_makeprg(&ea, p, cmdlinep);
if (p == NULL) { if (p == NULL) {
goto doend; goto doend;
} }
/* // Skip to start of argument.
* Skip to start of argument. // Don't do this for the ":!" command, because ":!! -l" needs the space.
* Don't do this for the ":!" command, because ":!! -l" needs the space.
*/
if (ea.cmdidx == CMD_bang) { if (ea.cmdidx == CMD_bang) {
ea.arg = p; ea.arg = p;
} else { } else {
@ -2078,10 +2059,8 @@ static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter
goto doend; goto doend;
} }
/* // Check for "++opt=val" argument.
* Check for "++opt=val" argument. // Must be first, allow ":w ++enc=utf8 !cmd"
* Must be first, allow ":w ++enc=utf8 !cmd"
*/
if (ea.argt & EX_ARGOPT) { if (ea.argt & EX_ARGOPT) {
while (ea.arg[0] == '+' && ea.arg[1] == '+') { while (ea.arg[0] == '+' && ea.arg[1] == '+') {
if (getargopt(&ea) == FAIL && !ni) { if (getargopt(&ea) == FAIL && !ni) {
@ -2124,18 +2103,14 @@ static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter
ea.arg = skipwhite(ea.arg); ea.arg = skipwhite(ea.arg);
} }
/* // Check for "+command" argument, before checking for next command.
* Check for "+command" argument, before checking for next command. // Don't do this for ":read !cmd" and ":write !cmd".
* Don't do this for ":read !cmd" and ":write !cmd".
*/
if ((ea.argt & EX_CMDARG) && !ea.usefilter) { if ((ea.argt & EX_CMDARG) && !ea.usefilter) {
ea.do_ecmd_cmd = getargcmd(&ea.arg); ea.do_ecmd_cmd = getargcmd(&ea.arg);
} }
/* // Check for '|' to separate commands and '"' to start comments.
* Check for '|' to separate commands and '"' to start comments. // Don't do this for ":read !cmd" and ":write !cmd".
* Don't do this for ":read !cmd" and ":write !cmd".
*/
if ((ea.argt & EX_TRLBAR) && !ea.usefilter) { if ((ea.argt & EX_TRLBAR) && !ea.usefilter) {
separate_nextcmd(&ea); separate_nextcmd(&ea);
} else if (ea.cmdidx == CMD_bang } else if (ea.cmdidx == CMD_bang
@ -2146,18 +2121,18 @@ static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter
// Check for <newline> to end a shell command. // Check for <newline> to end a shell command.
// Also do this for ":read !cmd", ":write !cmd" and ":global". // Also do this for ":read !cmd", ":write !cmd" and ":global".
// Any others? // Any others?
for (p = ea.arg; *p; p++) { for (char *s = ea.arg; *s; s++) {
// Remove one backslash before a newline, so that it's possible to // Remove one backslash before a newline, so that it's possible to
// pass a newline to the shell and also a newline that is preceded // pass a newline to the shell and also a newline that is preceded
// with a backslash. This makes it impossible to end a shell // with a backslash. This makes it impossible to end a shell
// command in a backslash, but that doesn't appear useful. // command in a backslash, but that doesn't appear useful.
// Halving the number of backslashes is incompatible with previous // Halving the number of backslashes is incompatible with previous
// versions. // versions.
if (*p == '\\' && p[1] == '\n') { if (*s == '\\' && s[1] == '\n') {
STRMOVE(p, p + 1); STRMOVE(s, s + 1);
} else if (*p == '\n') { } else if (*s == '\n') {
ea.nextcmd = p + 1; ea.nextcmd = s + 1;
*p = NUL; *s = NUL;
break; break;
} }
} }
@ -2173,9 +2148,7 @@ static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter
goto doend; goto doend;
} }
/* // Check for flags: 'l', 'p' and '#'.
* Check for flags: 'l', 'p' and '#'.
*/
if (ea.argt & EX_FLAGS) { if (ea.argt & EX_FLAGS) {
get_flags(&ea); get_flags(&ea);
} }
@ -2191,12 +2164,10 @@ static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter
goto doend; goto doend;
} }
/* // Skip the command when it's not going to be executed.
* Skip the command when it's not going to be executed. // The commands like :if, :endif, etc. always need to be executed.
* The commands like :if, :endif, etc. always need to be executed. // Also make an exception for commands that handle a trailing command
* Also make an exception for commands that handle a trailing command // themselves.
* themselves.
*/
if (ea.skip) { if (ea.skip) {
switch (ea.cmdidx) { switch (ea.cmdidx) {
// commands that need evaluation // commands that need evaluation
@ -2295,17 +2266,13 @@ static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter
} }
} }
/* // Accept buffer name. Cannot be used at the same time with a buffer
* Accept buffer name. Cannot be used at the same time with a buffer // number. Don't do this for a user command.
* number. Don't do this for a user command.
*/
if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0 if ((ea.argt & EX_BUFNAME) && *ea.arg != NUL && ea.addr_count == 0
&& !IS_USER_CMDIDX(ea.cmdidx)) { && !IS_USER_CMDIDX(ea.cmdidx)) {
/* // :bdelete, :bwipeout and :bunload take several arguments, separated
* :bdelete, :bwipeout and :bunload take several arguments, separated // by spaces: find next space (skipping over escaped characters).
* by spaces: find next space (skipping over escaped characters). // The others take one argument: ignore trailing spaces.
* The others take one argument: ignore trailing spaces.
*/
if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout if (ea.cmdidx == CMD_bdelete || ea.cmdidx == CMD_bwipeout
|| ea.cmdidx == CMD_bunload) { || ea.cmdidx == CMD_bunload) {
p = skiptowhite_esc(ea.arg); p = skiptowhite_esc(ea.arg);
@ -2336,14 +2303,10 @@ static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter
// 7. Execute the command. // 7. Execute the command.
if (IS_USER_CMDIDX(ea.cmdidx)) { if (IS_USER_CMDIDX(ea.cmdidx)) {
/* // Execute a user-defined command.
* Execute a user-defined command.
*/
do_ucmd(&ea, false); do_ucmd(&ea, false);
} else { } else {
/* // Call the function to execute the command.
* Call the function to execute the command.
*/
ea.errmsg = NULL; ea.errmsg = NULL;
(cmdnames[ea.cmdidx].cmd_func)(&ea); (cmdnames[ea.cmdidx].cmd_func)(&ea);
if (ea.errmsg != NULL) { if (ea.errmsg != NULL) {
@ -2351,13 +2314,11 @@ static char *do_one_cmd(char **cmdlinep, int flags, cstack_T *cstack, LineGetter
} }
} }
/* // If the command just executed called do_cmdline(), any throw or ":return"
* If the command just executed called do_cmdline(), any throw or ":return" // or ":finish" encountered there must also check the cstack of the still
* or ":finish" encountered there must also check the cstack of the still // active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught
* active do_cmdline() that called this do_one_cmd(). Rethrow an uncaught // exception, or reanimate a returned function or finished script file and
* exception, or reanimate a returned function or finished script file and // return or finish it again.
* return or finish it again.
*/
if (need_rethrow) { if (need_rethrow) {
do_throw(cstack); do_throw(cstack);
} else if (check_cstack) { } else if (check_cstack) {