vim-patch:partial:9.0.1196: code is indented more than necessary (#21796)

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

e857598896

Partial port as this depends on some previous eval and 'smoothscroll'
patches.

Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
This commit is contained in:
zeertzjq 2023-01-14 21:36:15 +08:00 committed by GitHub
parent d549734fb4
commit 2065ce877e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 173 additions and 142 deletions

View File

@ -326,10 +326,12 @@ void tv_list_free_list(list_T *const l)
void tv_list_free(list_T *const l) void tv_list_free(list_T *const l)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
{ {
if (!tv_in_free_unref_items) { if (tv_in_free_unref_items) {
tv_list_free_contents(l); return;
tv_list_free_list(l);
} }
tv_list_free_contents(l);
tv_list_free_list(l);
} }
/// Unreference a list /// Unreference a list

View File

@ -403,22 +403,24 @@ int name_to_mod_mask(int c)
int simplify_key(const int key, int *modifiers) int simplify_key(const int key, int *modifiers)
FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
{ {
if (*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT)) { if (!(*modifiers & (MOD_MASK_SHIFT | MOD_MASK_CTRL | MOD_MASK_ALT))) {
// TAB is a special case. return key;
if (key == TAB && (*modifiers & MOD_MASK_SHIFT)) { }
*modifiers &= ~MOD_MASK_SHIFT;
return K_S_TAB; // TAB is a special case.
} if (key == TAB && (*modifiers & MOD_MASK_SHIFT)) {
const int key0 = KEY2TERMCAP0(key); *modifiers &= ~MOD_MASK_SHIFT;
const int key1 = KEY2TERMCAP1(key); return K_S_TAB;
for (int i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE) { }
if (key0 == modifier_keys_table[i + 3] const int key0 = KEY2TERMCAP0(key);
&& key1 == modifier_keys_table[i + 4] const int key1 = KEY2TERMCAP1(key);
&& (*modifiers & modifier_keys_table[i])) { for (int i = 0; modifier_keys_table[i] != NUL; i += MOD_KEYS_ENTRY_SIZE) {
*modifiers &= ~modifier_keys_table[i]; if (key0 == modifier_keys_table[i + 3]
return TERMCAP2KEY(modifier_keys_table[i + 1], && key1 == modifier_keys_table[i + 4]
modifier_keys_table[i + 2]); && (*modifiers & modifier_keys_table[i])) {
} *modifiers &= ~modifier_keys_table[i];
return TERMCAP2KEY(modifier_keys_table[i + 1],
modifier_keys_table[i + 2]);
} }
} }
return key; return key;

View File

@ -317,23 +317,26 @@ static char **find_locales(void)
static void init_locales(void) static void init_locales(void)
{ {
# ifndef MSWIN # ifndef MSWIN
if (!did_init_locales) { if (did_init_locales) {
did_init_locales = true; return;
locales = find_locales();
} }
did_init_locales = true;
locales = find_locales();
# endif # endif
} }
# if defined(EXITFREE) # if defined(EXITFREE)
void free_locales(void) void free_locales(void)
{ {
int i; if (locales == NULL) {
if (locales != NULL) { return;
for (i = 0; locales[i] != NULL; i++) {
xfree(locales[i]);
}
XFREE_CLEAR(locales);
} }
for (int i = 0; locales[i] != NULL; i++) {
xfree(locales[i]);
}
XFREE_CLEAR(locales);
} }
# endif # endif

View File

@ -1828,17 +1828,19 @@ static void exe_pre_commands(mparm_T *parmp)
int cnt = parmp->n_pre_commands; int cnt = parmp->n_pre_commands;
int i; int i;
if (cnt > 0) { if (cnt <= 0) {
curwin->w_cursor.lnum = 0; // just in case.. return;
estack_push(ETYPE_ARGS, _("pre-vimrc command line"), 0);
current_sctx.sc_sid = SID_CMDARG;
for (i = 0; i < cnt; i++) {
do_cmdline_cmd(cmds[i]);
}
estack_pop();
current_sctx.sc_sid = 0;
TIME_MSG("--cmd commands");
} }
curwin->w_cursor.lnum = 0; // just in case..
estack_push(ETYPE_ARGS, _("pre-vimrc command line"), 0);
current_sctx.sc_sid = SID_CMDARG;
for (i = 0; i < cnt; i++) {
do_cmdline_cmd(cmds[i]);
}
estack_pop();
current_sctx.sc_sid = 0;
TIME_MSG("--cmd commands");
} }
// Execute "+", "-c" and "-S" arguments. // Execute "+", "-c" and "-S" arguments.
@ -2079,19 +2081,20 @@ static int execute_env(char *env)
FUNC_ATTR_NONNULL_ALL FUNC_ATTR_NONNULL_ALL
{ {
const char *initstr = os_getenv(env); const char *initstr = os_getenv(env);
if (initstr != NULL) { if (initstr == NULL) {
estack_push(ETYPE_ENV, env, 0); return FAIL;
const sctx_T save_current_sctx = current_sctx;
current_sctx.sc_sid = SID_ENV;
current_sctx.sc_seq = 0;
current_sctx.sc_lnum = 0;
do_cmdline_cmd((char *)initstr);
estack_pop();
current_sctx = save_current_sctx;
return OK;
} }
return FAIL;
estack_push(ETYPE_ENV, env, 0);
const sctx_T save_current_sctx = current_sctx;
current_sctx.sc_sid = SID_ENV;
current_sctx.sc_seq = 0;
current_sctx.sc_lnum = 0;
do_cmdline_cmd((char *)initstr);
estack_pop();
current_sctx = save_current_sctx;
return OK;
} }
/// Prints the following then exits: /// Prints the following then exits:

View File

@ -653,29 +653,31 @@ fmark_T *getnextmark(pos_T *startpos, int dir, int begin_line)
// until the mark is used to avoid a long startup delay. // until the mark is used to avoid a long startup delay.
static void fname2fnum(xfmark_T *fm) static void fname2fnum(xfmark_T *fm)
{ {
if (fm->fname != NULL) { if (fm->fname == NULL) {
// First expand "~/" in the file name to the home directory. return;
// Don't expand the whole name, it may contain other '~' chars. }
// First expand "~/" in the file name to the home directory.
// Don't expand the whole name, it may contain other '~' chars.
#ifdef BACKSLASH_IN_FILENAME #ifdef BACKSLASH_IN_FILENAME
if (fm->fname[0] == '~' && (fm->fname[1] == '/' || fm->fname[1] == '\\')) { if (fm->fname[0] == '~' && (fm->fname[1] == '/' || fm->fname[1] == '\\')) {
#else #else
if (fm->fname[0] == '~' && (fm->fname[1] == '/')) { if (fm->fname[0] == '~' && (fm->fname[1] == '/')) {
#endif #endif
expand_env("~/", NameBuff, MAXPATHL); expand_env("~/", NameBuff, MAXPATHL);
int len = (int)strlen(NameBuff); int len = (int)strlen(NameBuff);
xstrlcpy(NameBuff + len, fm->fname + 2, (size_t)(MAXPATHL - len)); xstrlcpy(NameBuff + len, fm->fname + 2, (size_t)(MAXPATHL - len));
} else { } else {
xstrlcpy(NameBuff, fm->fname, MAXPATHL); xstrlcpy(NameBuff, fm->fname, MAXPATHL);
}
// Try to shorten the file name.
os_dirname(IObuff, IOSIZE);
char *p = path_shorten_fname(NameBuff, IObuff);
// buflist_new() will call fmarks_check_names()
(void)buflist_new(NameBuff, p, (linenr_T)1, 0);
} }
// Try to shorten the file name.
os_dirname(IObuff, IOSIZE);
char *p = path_shorten_fname(NameBuff, IObuff);
// buflist_new() will call fmarks_check_names()
(void)buflist_new(NameBuff, p, (linenr_T)1, 0);
} }
// Check all file marks for a name that matches the file name in buf. // Check all file marks for a name that matches the file name in buf.

View File

@ -880,12 +880,14 @@ static int matchadd_dict_arg(typval_T *tv, const char **conceal_char, win_T **wi
*conceal_char = tv_get_string(&di->di_tv); *conceal_char = tv_get_string(&di->di_tv);
} }
if ((di = tv_dict_find(tv->vval.v_dict, S_LEN("window"))) != NULL) { if ((di = tv_dict_find(tv->vval.v_dict, S_LEN("window"))) == NULL) {
*win = find_win_by_nr_or_id(&di->di_tv); return OK;
if (*win == NULL) { }
emsg(_(e_invalwindow));
return FAIL; *win = find_win_by_nr_or_id(&di->di_tv);
} if (*win == NULL) {
emsg(_(e_invalwindow));
return FAIL;
} }
return OK; return OK;

View File

@ -767,11 +767,13 @@ void mf_set_fnames(memfile_T *mfp, char *fname)
/// Used before doing a :cd /// Used before doing a :cd
void mf_fullname(memfile_T *mfp) void mf_fullname(memfile_T *mfp)
{ {
if (mfp != NULL && mfp->mf_fname != NULL && mfp->mf_ffname != NULL) { if (mfp == NULL || mfp->mf_fname == NULL || mfp->mf_ffname == NULL) {
xfree(mfp->mf_fname); return;
mfp->mf_fname = mfp->mf_ffname;
mfp->mf_ffname = NULL;
} }
xfree(mfp->mf_fname);
mfp->mf_fname = mfp->mf_ffname;
mfp->mf_ffname = NULL;
} }
/// Return true if there are any translations pending for memfile. /// Return true if there are any translations pending for memfile.

View File

@ -1377,17 +1377,19 @@ char *make_percent_swname(const char *dir, const char *name)
{ {
char *d = NULL; char *d = NULL;
char *f = fix_fname(name != NULL ? name : ""); char *f = fix_fname(name != NULL ? name : "");
if (f != NULL) { if (f == NULL) {
char *s = xstrdup(f); return NULL;
for (d = s; *d != NUL; MB_PTR_ADV(d)) {
if (vim_ispathsep(*d)) {
*d = '%';
}
}
d = concat_fnames(dir, s, true);
xfree(s);
xfree(f);
} }
char *s = xstrdup(f);
for (d = s; *d != NUL; MB_PTR_ADV(d)) {
if (vim_ispathsep(*d)) {
*d = '%';
}
}
d = concat_fnames(dir, s, true);
xfree(s);
xfree(f);
return d; return d;
} }

View File

@ -1449,9 +1449,11 @@ void show_popupmenu(void)
} }
// Only show a popup when it is defined and has entries // Only show a popup when it is defined and has entries
if (menu != NULL && menu->children != NULL) { if (menu == NULL || menu->children == NULL) {
pum_show_popupmenu(menu); return;
} }
pum_show_popupmenu(menu);
} }
/// Execute "menu". Use by ":emenu" and the window toolbar. /// Execute "menu". Use by ":emenu" and the window toolbar.

View File

@ -606,50 +606,59 @@ void validate_virtcol(void)
void validate_virtcol_win(win_T *wp) void validate_virtcol_win(win_T *wp)
{ {
check_cursor_moved(wp); check_cursor_moved(wp);
if (!(wp->w_valid & VALID_VIRTCOL)) {
getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL); if (wp->w_valid & VALID_VIRTCOL) {
redraw_for_cursorcolumn(wp); return;
wp->w_valid |= VALID_VIRTCOL;
} }
getvvcol(wp, &wp->w_cursor, NULL, &(wp->w_virtcol), NULL);
redraw_for_cursorcolumn(wp);
wp->w_valid |= VALID_VIRTCOL;
} }
// Validate curwin->w_cline_height only. // Validate curwin->w_cline_height only.
void validate_cheight(void) void validate_cheight(void)
{ {
check_cursor_moved(curwin); check_cursor_moved(curwin);
if (!(curwin->w_valid & VALID_CHEIGHT)) {
curwin->w_cline_height = plines_win_full(curwin, curwin->w_cursor.lnum, if (curwin->w_valid & VALID_CHEIGHT) {
NULL, &curwin->w_cline_folded, return;
true);
curwin->w_valid |= VALID_CHEIGHT;
} }
curwin->w_cline_height = plines_win_full(curwin, curwin->w_cursor.lnum,
NULL, &curwin->w_cline_folded,
true);
curwin->w_valid |= VALID_CHEIGHT;
} }
// Validate w_wcol and w_virtcol only. // Validate w_wcol and w_virtcol only.
void validate_cursor_col(void) void validate_cursor_col(void)
{ {
validate_virtcol(); validate_virtcol();
if (!(curwin->w_valid & VALID_WCOL)) {
colnr_T col = curwin->w_virtcol;
colnr_T off = curwin_col_off();
col += off;
int width = curwin->w_width_inner - off + curwin_col_off2();
// long line wrapping, adjust curwin->w_wrow if (curwin->w_valid & VALID_WCOL) {
if (curwin->w_p_wrap && col >= (colnr_T)curwin->w_width_inner return;
&& width > 0) {
// use same formula as what is used in curs_columns()
col -= ((col - curwin->w_width_inner) / width + 1) * width;
}
if (col > (int)curwin->w_leftcol) {
col -= curwin->w_leftcol;
} else {
col = 0;
}
curwin->w_wcol = col;
curwin->w_valid |= VALID_WCOL;
} }
colnr_T col = curwin->w_virtcol;
colnr_T off = curwin_col_off();
col += off;
int width = curwin->w_width_inner - off + curwin_col_off2();
// long line wrapping, adjust curwin->w_wrow
if (curwin->w_p_wrap && col >= (colnr_T)curwin->w_width_inner
&& width > 0) {
// use same formula as what is used in curs_columns()
col -= ((col - curwin->w_width_inner) / width + 1) * width;
}
if (col > (int)curwin->w_leftcol) {
col -= curwin->w_leftcol;
} else {
col = 0;
}
curwin->w_wcol = col;
curwin->w_valid |= VALID_WCOL;
} }
// Compute offset of a window, occupied by absolute or relative line number, // Compute offset of a window, occupied by absolute or relative line number,

View File

@ -292,34 +292,36 @@ void vim_beep(unsigned val)
{ {
called_vim_beep = true; called_vim_beep = true;
if (emsg_silent == 0 && !in_assert_fails) { if (emsg_silent != 0 || in_assert_fails) {
if (!((bo_flags & val) || (bo_flags & BO_ALL))) { return;
static int beeps = 0; }
static uint64_t start_time = 0;
// Only beep up to three times per half a second, if (!((bo_flags & val) || (bo_flags & BO_ALL))) {
// otherwise a sequence of beeps would freeze Vim. static int beeps = 0;
if (start_time == 0 || os_hrtime() - start_time > 500000000U) { static uint64_t start_time = 0;
beeps = 0;
start_time = os_hrtime(); // Only beep up to three times per half a second,
} // otherwise a sequence of beeps would freeze Vim.
beeps++; if (start_time == 0 || os_hrtime() - start_time > 500000000U) {
if (beeps <= 3) { beeps = 0;
if (p_vb) { start_time = os_hrtime();
ui_call_visual_bell(); }
} else { beeps++;
ui_call_bell(); if (beeps <= 3) {
} if (p_vb) {
ui_call_visual_bell();
} else {
ui_call_bell();
} }
} }
}
// When 'debug' contains "beep" produce a message. If we are sourcing // When 'debug' contains "beep" produce a message. If we are sourcing
// a script or executing a function give the user a hint where the beep // a script or executing a function give the user a hint where the beep
// comes from. // comes from.
if (vim_strchr(p_debug, 'e') != NULL) { if (vim_strchr(p_debug, 'e') != NULL) {
msg_source(HL_ATTR(HLF_W)); msg_source(HL_ATTR(HLF_W));
msg_attr(_("Beep!"), HL_ATTR(HLF_W)); msg_attr(_("Beep!"), HL_ATTR(HLF_W));
}
} }
} }