From 77135447e09903b45d1482da45869946212f7904 Mon Sep 17 00:00:00 2001 From: Felipe Oliveira Carvalho Date: Sat, 13 Dec 2014 10:55:51 -0300 Subject: Reduce indentation level by early returning or continuing loop Replace code like this ```c func() { if (cond) { ... ... ... } return ret; } ``` ```c for (...) { if (cond) { ... ... ... } } ``` with ```c func() { if (!cond) { return ret; } ... ... ... } ``` ```c for (...) { if (!cond) { continue; } ... ... ... } ``` --- src/nvim/buffer.c | 141 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 73 insertions(+), 68 deletions(-) (limited to 'src/nvim/buffer.c') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index ec10a826c7..b8dbb413ba 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -238,25 +238,27 @@ open_buffer ( } apply_autocmds_retval(EVENT_BUFENTER, NULL, NULL, FALSE, curbuf, &retval); - if (retval != FAIL) { - /* - * The autocommands may have changed the current buffer. Apply the - * modelines to the correct buffer, if it still exists and is loaded. - */ - if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL) { - aco_save_T aco; + if (retval == FAIL) { + return FAIL; + } - /* Go to the buffer that was opened. */ - aucmd_prepbuf(&aco, old_curbuf); - do_modelines(0); - curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED); + /* + * The autocommands may have changed the current buffer. Apply the + * modelines to the correct buffer, if it still exists and is loaded. + */ + if (buf_valid(old_curbuf) && old_curbuf->b_ml.ml_mfp != NULL) { + aco_save_T aco; - apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf, - &retval); + /* Go to the buffer that was opened. */ + aucmd_prepbuf(&aco, old_curbuf); + do_modelines(0); + curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED); - /* restore curwin/curbuf and a few other things */ - aucmd_restbuf(&aco); - } + apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf, + &retval); + + /* restore curwin/curbuf and a few other things */ + aucmd_restbuf(&aco); } return retval; @@ -4091,66 +4093,69 @@ chk_modeline ( prev = *s; } - if (*s) { - do /* skip over "ex:", "vi:" or "vim:" */ - ++s; - while (s[-1] != ':'); + if (!*s) { + return retval; + } - s = linecopy = vim_strsave(s); /* copy the line, it will change */ + do /* skip over "ex:", "vi:" or "vim:" */ + ++s; + while (s[-1] != ':'); - save_sourcing_lnum = sourcing_lnum; - save_sourcing_name = sourcing_name; - sourcing_lnum = lnum; /* prepare for emsg() */ - sourcing_name = (char_u *)"modelines"; + s = linecopy = vim_strsave(s); /* copy the line, it will change */ - end = FALSE; - while (end == FALSE) { - s = skipwhite(s); - if (*s == NUL) - break; + save_sourcing_lnum = sourcing_lnum; + save_sourcing_name = sourcing_name; + sourcing_lnum = lnum; /* prepare for emsg() */ + sourcing_name = (char_u *)"modelines"; - /* - * Find end of set command: ':' or end of line. - * Skip over "\:", replacing it with ":". - */ - for (e = s; *e != ':' && *e != NUL; ++e) - if (e[0] == '\\' && e[1] == ':') - STRMOVE(e, e + 1); - if (*e == NUL) - end = TRUE; + end = FALSE; + while (end == FALSE) { + s = skipwhite(s); + if (*s == NUL) + break; - /* - * If there is a "set" command, require a terminating ':' and - * ignore the stuff after the ':'. - * "vi:set opt opt opt: foo" -- foo not interpreted - * "vi:opt opt opt: foo" -- foo interpreted - * Accept "se" for compatibility with Elvis. - */ - if (STRNCMP(s, "set ", (size_t)4) == 0 - || STRNCMP(s, "se ", (size_t)3) == 0) { - if (*e != ':') /* no terminating ':'? */ - break; - end = TRUE; - s = vim_strchr(s, ' ') + 1; - } - *e = NUL; /* truncate the set command */ - - if (*s != NUL) { /* skip over an empty "::" */ - save_SID = current_SID; - current_SID = SID_MODELINE; - retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags); - current_SID = save_SID; - if (retval == FAIL) /* stop if error found */ - break; - } - s = e + 1; /* advance to next part */ + /* + * Find end of set command: ':' or end of line. + * Skip over "\:", replacing it with ":". + */ + for (e = s; *e != ':' && *e != NUL; ++e) + if (e[0] == '\\' && e[1] == ':') + STRMOVE(e, e + 1); + if (*e == NUL) + end = TRUE; + + /* + * If there is a "set" command, require a terminating ':' and + * ignore the stuff after the ':'. + * "vi:set opt opt opt: foo" -- foo not interpreted + * "vi:opt opt opt: foo" -- foo interpreted + * Accept "se" for compatibility with Elvis. + */ + if (STRNCMP(s, "set ", (size_t)4) == 0 + || STRNCMP(s, "se ", (size_t)3) == 0) { + if (*e != ':') /* no terminating ':'? */ + break; + end = TRUE; + s = vim_strchr(s, ' ') + 1; + } + *e = NUL; /* truncate the set command */ + + if (*s != NUL) { /* skip over an empty "::" */ + save_SID = current_SID; + current_SID = SID_MODELINE; + retval = do_set(s, OPT_MODELINE | OPT_LOCAL | flags); + current_SID = save_SID; + if (retval == FAIL) /* stop if error found */ + break; } + s = e + 1; /* advance to next part */ + } - sourcing_lnum = save_sourcing_lnum; - sourcing_name = save_sourcing_name; + sourcing_lnum = save_sourcing_lnum; + sourcing_name = save_sourcing_name; + + free(linecopy); - free(linecopy); - } return retval; } -- cgit From 0bc40e660c0a74776ace86ad5e393755523c3803 Mon Sep 17 00:00:00 2001 From: Felipe Oliveira Carvalho Date: Sat, 13 Dec 2014 10:56:17 -0300 Subject: Simple refatorings that didn't fit the pattern of the last commit --- src/nvim/buffer.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'src/nvim/buffer.c') diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index b8dbb413ba..f84e25cdfb 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -2105,14 +2105,10 @@ void get_winopts(buf_T *buf) */ pos_T *buflist_findfpos(buf_T *buf) { - wininfo_T *wip; static pos_T no_position = INIT_POS_T(1, 0, 0); - wip = find_wininfo(buf, FALSE); - if (wip != NULL) - return &(wip->wi_fpos); - else - return &no_position; + wininfo_T *wip = find_wininfo(buf, FALSE); + return (wip == NULL) ? &no_position : &(wip->wi_fpos); } /* -- cgit