vim-patch:9.0.1208: code is indented more than necessary (#21846)

Problem:    Code is indented more than necessary.
Solution:   Use an early return where it makes sense. (Yegappan Lakshmanan,
            closes vim/vim#11819)

a41e221935

Cherry-pick check_text_or_curbuf_locked() from patch 9.0.0947.

Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
This commit is contained in:
zeertzjq
2023-01-17 08:09:51 +08:00
committed by GitHub
parent bbdded5cf7
commit f72cb97fa0
4 changed files with 502 additions and 421 deletions

View File

@@ -451,14 +451,36 @@ static int find_command(int cmdchar)
/// message, return true.
static bool check_text_locked(oparg_T *oap)
{
if (text_locked()) {
if (!text_locked()) {
return false;
}
if (oap != NULL) {
clearopbeep(oap);
}
text_locked_msg();
return true;
}
/// If text is locked, "curbuf->b_ro_locked" or "allbuf_lock" is set:
/// Give an error message, possibly beep and return true.
/// "oap" may be NULL.
static bool check_text_or_curbuf_locked(oparg_T *oap)
{
if (check_text_locked(oap)) {
return true;
}
if (!curbuf_locked()) {
return false;
}
if (oap != NULL) {
clearop(oap);
}
return true;
}
/// Normal state entry point. This is called on:
///
/// - Startup, In this case the function never returns.
@@ -1107,8 +1129,7 @@ static int normal_execute(VimState *state, int key)
goto finish;
}
if ((nv_cmds[s->idx].cmd_flags & NV_NCW)
&& (check_text_locked(&s->oa) || curbuf_locked())) {
if ((nv_cmds[s->idx].cmd_flags & NV_NCW) && check_text_or_curbuf_locked(&s->oa)) {
// this command is not allowed now
s->command_finished = true;
goto finish;
@@ -2191,7 +2212,10 @@ static void nv_addsub(cmdarg_T *cap)
/// CTRL-F, CTRL-B, etc: Scroll page up or down.
static void nv_page(cmdarg_T *cap)
{
if (!checkclearop(cap->oap)) {
if (checkclearop(cap->oap)) {
return;
}
if (mod_mask & MOD_MASK_CTRL) {
// <C-PageUp>: tab page back; <C-PageDown>: tab page forward
if (cap->arg == BACKWARD) {
@@ -2203,7 +2227,6 @@ static void nv_page(cmdarg_T *cap)
(void)onepage(cap->arg, cap->count1);
}
}
}
/// Implementation of "gd" and "gD" command.
///
@@ -2215,7 +2238,9 @@ static void nv_gd(oparg_T *oap, int nchar, int thisblock)
if ((len = find_ident_under_cursor(&ptr, FIND_IDENT)) == 0
|| !find_decl((char_u *)ptr, len, nchar == 'd', thisblock, SEARCH_START)) {
clearopbeep(oap);
} else {
return;
}
if ((fdo_flags & FDO_SEARCH) && KeyTyped && oap->op_type == OP_NOP) {
foldOpenCursor();
}
@@ -2224,7 +2249,6 @@ static void nv_gd(oparg_T *oap, int nchar, int thisblock)
clear_cmdline = true;
}
}
}
/// @return true if line[offset] is not inside a C-style comment or string,
/// false otherwise.
@@ -3195,7 +3219,9 @@ static void nv_colon(cmdarg_T *cap)
if (VIsual_active && !is_cmdkey && !is_lua) {
nv_operator(cap);
} else {
return;
}
if (cap->oap->op_type != OP_NOP) {
// Using ":" as a movement is charwise exclusive.
cap->oap->motion_type = kMTCharWise;
@@ -3234,7 +3260,6 @@ static void nv_colon(cmdarg_T *cap)
clearopbeep(cap->oap);
}
}
}
/// Handle CTRL-G command.
static void nv_ctrlg(cmdarg_T *cap)
@@ -3263,7 +3288,10 @@ static void nv_ctrlh(cmdarg_T *cap)
/// CTRL-L: clear screen and redraw.
static void nv_clear(cmdarg_T *cap)
{
if (!checkclearop(cap->oap)) {
if (checkclearop(cap->oap)) {
return;
}
// Clear all syntax states to force resyncing.
syn_stack_free_all(curwin->w_s);
FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
@@ -3271,7 +3299,6 @@ static void nv_clear(cmdarg_T *cap)
}
redraw_later(curwin, UPD_CLEAR);
}
}
/// CTRL-O: In Select mode: switch to Visual mode for one command.
/// Otherwise: Go to older pcmark.
@@ -3301,7 +3328,10 @@ static void nv_hat(cmdarg_T *cap)
/// "Z" commands.
static void nv_Zet(cmdarg_T *cap)
{
if (!checkclearopq(cap->oap)) {
if (checkclearopq(cap->oap)) {
return;
}
switch (cap->nchar) {
// "ZZ": equivalent to ":x".
case 'Z':
@@ -3317,7 +3347,6 @@ static void nv_Zet(cmdarg_T *cap)
clearopbeep(cap->oap);
}
}
}
/// Call nv_ident() as if "c1" was used, with "c2" as next character.
void do_nv_ident(int c1, int c2)
@@ -3846,7 +3875,9 @@ static void nv_up(cmdarg_T *cap)
// <S-Up> is page up
cap->arg = BACKWARD;
nv_page(cap);
} else {
return;
}
cap->oap->motion_type = kMTLineWise;
if (cursor_up(cap->count1, cap->oap->op_type == OP_NOP) == false) {
clearopbeep(cap->oap);
@@ -3854,7 +3885,6 @@ static void nv_up(cmdarg_T *cap)
beginline(BL_WHITE | BL_FIX);
}
}
}
/// Cursor down commands.
/// cap->arg is true for CR and "+": Move cursor to first non-blank.
@@ -3895,11 +3925,7 @@ static void nv_gotofile(cmdarg_T *cap)
char *ptr;
linenr_T lnum = -1;
if (check_text_locked(cap->oap)) {
return;
}
if (curbuf_locked()) {
clearop(cap->oap);
if (check_text_or_curbuf_locked(cap->oap)) {
return;
}
@@ -4058,7 +4084,9 @@ static void nv_csearch(cmdarg_T *cap)
cap->oap->motion_type = kMTCharWise;
if (IS_SPECIAL(cap->nchar) || searchc(cap, t_cmd) == false) {
clearopbeep(cap->oap);
} else {
return;
}
curwin->w_set_curswant = true;
// Include a Tab for "tx" and for "dfx".
if (gchar_cursor() == TAB && virtual_active() && cap->arg == FORWARD
@@ -4075,7 +4103,6 @@ static void nv_csearch(cmdarg_T *cap)
foldOpenCursor();
}
}
}
/// "[{", "[(", "]}" or "])": go to Nth unclosed '{', '(', '}' or ')'
/// "[#", "]#": go to start/end of Nth innermost #if..#endif construct.
@@ -4395,9 +4422,11 @@ static void nv_brace(cmdarg_T *cap)
cap->oap->inclusive = false;
curwin->w_set_curswant = true;
if (findsent(cap->arg, cap->count1) == false) {
if (findsent(cap->arg, cap->count1) == FAIL) {
clearopbeep(cap->oap);
} else {
return;
}
// Don't leave the cursor on the NUL past end of line.
adjust_cursor(cap->oap);
curwin->w_cursor.coladd = 0;
@@ -4405,17 +4434,18 @@ static void nv_brace(cmdarg_T *cap)
foldOpenCursor();
}
}
}
/// "m" command: Mark a position.
static void nv_mark(cmdarg_T *cap)
{
if (!checkclearop(cap->oap)) {
if (checkclearop(cap->oap)) {
return;
}
if (setmark(cap->nchar) == false) {
clearopbeep(cap->oap);
}
}
}
/// "{" and "}" commands.
/// cmd->arg is BACKWARD for "{" and FORWARD for "}".
@@ -4427,13 +4457,14 @@ static void nv_findpar(cmdarg_T *cap)
curwin->w_set_curswant = true;
if (!findpar(&cap->oap->inclusive, cap->arg, cap->count1, NUL, false)) {
clearopbeep(cap->oap);
} else {
return;
}
curwin->w_cursor.coladd = 0;
if ((fdo_flags & FDO_BLOCK) && KeyTyped && cap->oap->op_type == OP_NOP) {
foldOpenCursor();
}
}
}
/// "u" command: Undo or make lower case.
static void nv_undo(cmdarg_T *cap)
@@ -4452,7 +4483,10 @@ static void nv_undo(cmdarg_T *cap)
/// <Undo> command.
static void nv_kundo(cmdarg_T *cap)
{
if (!checkclearopq(cap->oap)) {
if (checkclearopq(cap->oap)) {
return;
}
if (bt_prompt(curbuf)) {
clearopbeep(cap->oap);
return;
@@ -4460,7 +4494,6 @@ static void nv_kundo(cmdarg_T *cap)
u_undo((int)cap->count1);
curwin->w_set_curswant = true;
}
}
/// Handle the "r" command.
static void nv_replace(cmdarg_T *cap)
@@ -4670,7 +4703,13 @@ static void nv_Replace(cmdarg_T *cap)
VIsual_mode_orig = VIsual_mode; // remember original area for gv
VIsual_mode = 'V';
nv_operator(cap);
} else if (!checkclearopq(cap->oap)) {
return;
}
if (checkclearopq(cap->oap)) {
return;
}
if (!MODIFIABLE(curbuf)) {
emsg(_(e_modifiable));
} else {
@@ -4680,7 +4719,6 @@ static void nv_Replace(cmdarg_T *cap)
invoke_edit(cap, false, cap->arg ? 'V' : 'R', false);
}
}
}
/// "gr".
static void nv_vreplace(cmdarg_T *cap)
@@ -4689,7 +4727,13 @@ static void nv_vreplace(cmdarg_T *cap)
cap->cmdchar = 'r';
cap->nchar = cap->extra_char;
nv_replace(cap); // Do same as "r" in Visual mode for now
} else if (!checkclearopq(cap->oap)) {
return;
}
if (checkclearopq(cap->oap)) {
return;
}
if (!MODIFIABLE(curbuf)) {
emsg(_(e_modifiable));
} else {
@@ -4704,7 +4748,6 @@ static void nv_vreplace(cmdarg_T *cap)
invoke_edit(cap, true, 'v', false);
}
}
}
/// Swap case for "~" command, when it does not work like an operator.
static void n_swapchar(cmdarg_T *cap)
@@ -4903,7 +4946,10 @@ static void nv_pcmark(cmdarg_T *cap)
MarkMoveRes move_res = 0; // Result from moving to the mark
const bool old_KeyTyped = KeyTyped; // getting file may reset it.
if (!checkclearopq(cap->oap)) {
if (checkclearopq(cap->oap)) {
return;
}
if (cap->cmdchar == TAB && mod_mask == MOD_MASK_CTRL) {
if (!goto_tabpage_lastused()) {
clearopbeep(cap->oap);
@@ -4940,7 +4986,6 @@ static void nv_pcmark(cmdarg_T *cap)
foldOpenCursor();
}
}
}
/// Handle '"' command.
static void nv_regname(cmdarg_T *cap)
@@ -5669,7 +5714,10 @@ static void nv_g_cmd(cmdarg_T *cap)
/// Handle "o" and "O" commands.
static void n_opencmd(cmdarg_T *cap)
{
if (!checkclearopq(cap->oap)) {
if (checkclearopq(cap->oap)) {
return;
}
if (cap->cmdchar == 'O') {
// Open above the first line of a folded sequence of lines
(void)hasFolding(curwin->w_cursor.lnum,
@@ -5693,12 +5741,14 @@ static void n_opencmd(cmdarg_T *cap)
invoke_edit(cap, false, cap->cmdchar, true);
}
}
}
/// "." command: redo last change.
static void nv_dot(cmdarg_T *cap)
{
if (!checkclearopq(cap->oap)) {
if (checkclearopq(cap->oap)) {
return;
}
// If "restart_edit" is true, the last but one command is repeated
// instead of the last command (inserting text). This is used for
// CTRL-O <.> in insert mode.
@@ -5706,7 +5756,6 @@ static void nv_dot(cmdarg_T *cap)
clearopbeep(cap->oap);
}
}
}
/// CTRL-R: undo undo or specify register in select mode
static void nv_redo_or_register(cmdarg_T *cap)
@@ -5728,11 +5777,13 @@ static void nv_redo_or_register(cmdarg_T *cap)
return;
}
if (!checkclearopq(cap->oap)) {
if (checkclearopq(cap->oap)) {
return;
}
u_redo((int)cap->count1);
curwin->w_set_curswant = true;
}
}
/// Handle "U" command.
static void nv_Undo(cmdarg_T *cap)
@@ -5743,11 +5794,16 @@ static void nv_Undo(cmdarg_T *cap)
cap->cmdchar = 'g';
cap->nchar = 'U';
nv_operator(cap);
} else if (!checkclearopq(cap->oap)) {
return;
}
if (checkclearopq(cap->oap)) {
return;
}
u_undoline();
curwin->w_set_curswant = true;
}
}
/// '~' command: If tilde is not an operator and Visual is off: swap case of a
/// single character.
@@ -6309,7 +6365,13 @@ static void nv_record(cmdarg_T *cap)
cap->cmdchar = 'g';
cap->nchar = 'q';
nv_operator(cap);
} else if (!checkclearop(cap->oap)) {
return;
}
if (checkclearop(cap->oap)) {
return;
}
if (cap->nchar == ':' || cap->nchar == '/' || cap->nchar == '?') {
stuffcharReadbuff(cap->nchar);
stuffcharReadbuff(K_CMDWIN);
@@ -6321,7 +6383,6 @@ static void nv_record(cmdarg_T *cap)
}
}
}
}
/// Handle the "@r" command.
static void nv_at(cmdarg_T *cap)
@@ -6360,7 +6421,13 @@ static void nv_join(cmdarg_T *cap)
{
if (VIsual_active) { // join the visual lines
nv_operator(cap);
} else if (!checkclearop(cap->oap)) {
return;
}
if (checkclearop(cap->oap)) {
return;
}
if (cap->count0 <= 1) {
cap->count0 = 2; // default for join is two lines!
}
@@ -6378,7 +6445,6 @@ static void nv_join(cmdarg_T *cap)
NUL, cap->cmdchar, NUL, NUL, cap->nchar);
do_join((size_t)cap->count0, cap->nchar == NUL, true, true, true);
}
}
/// "P", "gP", "p" and "gp" commands.
static void nv_put(cmdarg_T *cap)
@@ -6407,9 +6473,14 @@ static void nv_put_opt(cmdarg_T *cap, bool fix_indent)
} else {
clearopbeep(cap->oap);
}
} else if (bt_prompt(curbuf) && !prompt_curpos_editable()) {
return;
}
if (bt_prompt(curbuf) && !prompt_curpos_editable()) {
clearopbeep(cap->oap);
} else {
return;
}
if (fix_indent) {
dir = (cap->cmdchar == ']' && cap->nchar == 'p')
? FORWARD : BACKWARD;
@@ -6518,7 +6589,6 @@ static void nv_put_opt(cmdarg_T *cap, bool fix_indent)
}
auto_format(false, true);
}
}
/// "o" and "O" commands.
static void nv_open(cmdarg_T *cap)

View File

@@ -1785,11 +1785,13 @@ setmarks:
/// Used for deletion.
static void mb_adjust_opend(oparg_T *oap)
{
if (oap->inclusive) {
if (!oap->inclusive) {
return;
}
char *p = ml_get(oap->end.lnum);
oap->end.col += utf_cp_tail_off(p, p + oap->end.col);
}
}
/// Put character 'c' at position 'lp'
static inline void pbyte(pos_T lp, int c)
@@ -3711,10 +3713,14 @@ void adjust_cursor_eol(void)
{
unsigned int cur_ve_flags = get_ve_flags();
if (curwin->w_cursor.col > 0
const bool adj_cursor = (curwin->w_cursor.col > 0
&& gchar_cursor() == NUL
&& (cur_ve_flags & VE_ONEMORE) == 0
&& !(restart_edit || (State & MODE_INSERT))) {
&& !(restart_edit || (State & MODE_INSERT)));
if (!adj_cursor) {
return;
}
// Put the cursor on the last character in the line.
dec_cursor();
@@ -3726,7 +3732,6 @@ void adjust_cursor_eol(void)
curwin->w_cursor.coladd = ecol - scol + 1;
}
}
}
/// @return true if lines starting with '#' should be left aligned.
int preprocs_left(void)
@@ -4205,12 +4210,14 @@ static bool reset_lbr(void)
/// Restore 'linebreak' and take care of side effects.
static void restore_lbr(bool lbr_saved)
{
if (!curwin->w_p_lbr && lbr_saved) {
if (curwin->w_p_lbr || !lbr_saved) {
return;
}
// changing 'linebreak' may require w_virtcol to be updated
curwin->w_p_lbr = true;
curwin->w_valid &= ~(VALID_WROW|VALID_WCOL|VALID_VIRTCOL);
}
}
/// prepare a few things for block mode yank/delete/tilde
///

View File

@@ -674,7 +674,10 @@ void set_helplang_default(const char *lang)
return;
}
int idx = findoption("hlg");
if (idx >= 0 && !(options[idx].flags & P_WAS_SET)) {
if (idx < 0 || (options[idx].flags & P_WAS_SET)) {
return;
}
if (options[idx].flags & P_ALLOCED) {
free_string_option(p_hlg);
}
@@ -691,7 +694,6 @@ void set_helplang_default(const char *lang)
p_hlg[2] = NUL;
options[idx].flags |= P_ALLOCED;
}
}
/// 'title' and 'icon' only default to true if they have not been set or reset
/// in .vimrc and we can read the old value.
@@ -5035,10 +5037,11 @@ bool option_was_set(const char *name)
void reset_option_was_set(const char *name)
{
const int idx = findoption(name);
if (idx >= 0) {
options[idx].flags &= ~P_WAS_SET;
if (idx < 0) {
return;
}
options[idx].flags &= ~P_WAS_SET;
}
/// fill_breakat_flags() -- called when 'breakat' changes value.

View File

@@ -167,9 +167,11 @@ void trigger_optionset_string(int opt_idx, int opt_flags, char *oldval, char *ol
char *oldval_g, char *newval)
{
// Don't do this recursively.
if (oldval != NULL
&& newval != NULL
&& *get_vim_var_str(VV_OPTION_TYPE) == NUL) {
if (oldval == NULL || newval == NULL
|| *get_vim_var_str(VV_OPTION_TYPE) != NUL) {
return;
}
char buf_type[7];
vim_snprintf(buf_type, ARRAY_SIZE(buf_type), "%s",
@@ -197,7 +199,6 @@ void trigger_optionset_string(int opt_idx, int opt_flags, char *oldval, char *ol
apply_autocmds(EVENT_OPTIONSET, get_option(opt_idx)->fullname, NULL, false, NULL);
reset_v_option_vars();
}
}
static char *illegal_char(char *errbuf, size_t errbuflen, int c)
{