From d1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0 Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Fri, 19 Jul 2024 11:00:13 +0100 Subject: refactor: collapse statements in single assignments Problem: Variables are often assigned multiple places in common patterns. Solution: Replace these common patterns with different patterns that reduce the number of assignments. Use `MAX` and `MIN`: ```c if (x < y) { x = y; } // --> x = MAX(x, y); ``` ```c if (x > y) { x = y; } // --> x = MIN(x, y); ``` Use ternary: ```c int a; if (cond) { a = b; } els { a = c; } // --> int a = cond ? b : c; ``` --- src/nvim/arglist.c | 7 +-- src/nvim/buffer.c | 104 +++++++++++------------------------- src/nvim/bufwrite.c | 26 +++------ src/nvim/change.c | 16 ++---- src/nvim/cmdexpand.c | 21 ++------ src/nvim/cursor.c | 21 ++------ src/nvim/diff.c | 41 ++++----------- src/nvim/drawline.c | 8 +-- src/nvim/drawscreen.c | 122 +++++++++++-------------------------------- src/nvim/edit.c | 31 +++-------- src/nvim/ex_cmds.c | 62 +++++++--------------- src/nvim/ex_docmd.c | 43 +++++---------- src/nvim/ex_getln.c | 57 ++++++-------------- src/nvim/fileio.c | 15 ++---- src/nvim/fold.c | 130 +++++++++++---------------------------------- src/nvim/garray.c | 8 +-- src/nvim/getchar.c | 23 ++------ src/nvim/hashtab.c | 5 +- src/nvim/memline.c | 21 +++----- src/nvim/message.c | 14 ++--- src/nvim/mouse.c | 19 ++----- src/nvim/move.c | 51 +++++------------- src/nvim/normal.c | 50 +++++------------- src/nvim/ops.c | 110 +++++++++------------------------------ src/nvim/quickfix.c | 119 +++++++++++------------------------------- src/nvim/search.c | 62 +++++++--------------- src/nvim/sha256.c | 17 +++--- src/nvim/spellsuggest.c | 72 +++++++------------------ src/nvim/statusline.c | 35 +++---------- src/nvim/tag.c | 136 +++++++++++++++--------------------------------- src/nvim/terminal.c | 5 +- src/nvim/textformat.c | 23 ++------ src/nvim/textobject.c | 6 +-- src/nvim/undo.c | 34 ++++-------- src/nvim/usercmd.c | 4 +- src/nvim/window.c | 115 ++++++++++------------------------------ 36 files changed, 419 insertions(+), 1214 deletions(-) (limited to 'src/nvim') diff --git a/src/nvim/arglist.c b/src/nvim/arglist.c index 700bfc1655..e3a2db75e5 100644 --- a/src/nvim/arglist.c +++ b/src/nvim/arglist.c @@ -346,12 +346,7 @@ static void alist_add_list(int count, char **files, int after, bool will_edit) int old_argcount = ARGCOUNT; ga_grow(&ALIST(curwin)->al_ga, count); if (check_arglist_locked() != FAIL) { - if (after < 0) { - after = 0; - } - if (after > ARGCOUNT) { - after = ARGCOUNT; - } + after = MIN(MAX(after, 0), ARGCOUNT); if (after < ARGCOUNT) { memmove(&(ARGLIST[after + count]), &(ARGLIST[after]), (size_t)(ARGCOUNT - after) * sizeof(aentry_T)); diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index d1b7c553c3..2fc684b412 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -1262,17 +1262,8 @@ static int do_buffer_ext(int action, int start, int dir, int count, int flags) if (bp == NULL) { bp = buf; } - if (dir == FORWARD) { - buf = buf->b_next; - if (buf == NULL) { - buf = firstbuf; - } - } else { - buf = buf->b_prev; - if (buf == NULL) { - buf = lastbuf; - } - } + buf = dir == FORWARD ? (buf->b_next != NULL ? buf->b_next : firstbuf) + : (buf->b_prev != NULL ? buf->b_prev : lastbuf); // Don't count unlisted buffers. // Avoid non-help buffers if the starting point was a non-help buffer and // vice-versa. @@ -1505,11 +1496,7 @@ static int do_buffer_ext(int action, int start, int dir, int count, int flags) bp = buf; } } - if (forward) { - buf = buf->b_next; - } else { - buf = buf->b_prev; - } + buf = forward ? buf->b_next : buf->b_prev; } } if (buf == NULL) { // No loaded buffer, use unloaded one @@ -1524,11 +1511,7 @@ static int do_buffer_ext(int action, int start, int dir, int count, int flags) } } if (buf == NULL) { // Still no buffer, just take one - if (curbuf->b_next != NULL) { - buf = curbuf->b_next; - } else { - buf = curbuf->b_prev; - } + buf = curbuf->b_next != NULL ? curbuf->b_next : curbuf->b_prev; if (bt_quickfix(buf)) { buf = NULL; } @@ -1679,11 +1662,7 @@ void set_curbuf(buf_T *buf, int action, bool update_jumplist) } // If the buffer is not valid but curwin->w_buffer is NULL we must // enter some buffer. Using the last one is hopefully OK. - if (!valid) { - enter_buffer(lastbuf); - } else { - enter_buffer(buf); - } + enter_buffer(valid ? buf : lastbuf); if (old_tw != curbuf->b_p_tw) { check_colorcolumn(curwin); } @@ -2135,7 +2114,6 @@ int buflist_getfile(int n, linenr_T lnum, int options, int forceit) { win_T *wp = NULL; fmark_T *fm = NULL; - colnr_T col; buf_T *buf = buflist_findnr(n); if (buf == NULL) { @@ -2156,6 +2134,7 @@ int buflist_getfile(int n, linenr_T lnum, int options, int forceit) return FAIL; } + colnr_T col; bool restore_view = false; // altfpos may be changed by getfile(), get it now if (lnum == 0) { @@ -2291,11 +2270,7 @@ int buflist_findpat(const char *pattern, const char *pattern_end, bool unlisted, int match = -1; if (pattern_end == pattern + 1 && (*pattern == '%' || *pattern == '#')) { - if (*pattern == '%') { - match = curbuf->b_fnum; - } else { - match = curwin->w_alt_fnum; - } + match = *pattern == '%' ? curbuf->b_fnum : curwin->w_alt_fnum; buf_T *found_buf = buflist_findnr(match); if (diffmode && !(found_buf && diff_mode_buf(found_buf))) { match = -1; @@ -2906,9 +2881,7 @@ void buflist_list(exarg_T *eap) changed_char, NameBuff); - if (len > IOSIZE - 20) { - len = IOSIZE - 20; - } + len = MIN(len, IOSIZE - 20); // put "line 999" in column 40 or after the file name int i = 40 - vim_strsize(IObuff); @@ -3212,8 +3185,6 @@ static bool buf_same_file_id(buf_T *buf, FileID *file_id) /// @param fullname when non-zero print full path void fileinfo(int fullname, int shorthelp, bool dont_truncate) { - char *name; - int n; char *p; char *buffer = xmalloc(IOSIZE); @@ -3229,11 +3200,9 @@ void fileinfo(int fullname, int shorthelp, bool dont_truncate) if (buf_spname(curbuf) != NULL) { xstrlcpy(p, buf_spname(curbuf), (size_t)(IOSIZE - (p - buffer))); } else { - if (!fullname && curbuf->b_fname != NULL) { - name = curbuf->b_fname; - } else { - name = curbuf->b_ffname; - } + char *name = (!fullname && curbuf->b_fname != NULL) + ? curbuf->b_fname + : curbuf->b_ffname; home_replace(shorthelp ? curbuf : NULL, name, p, (size_t)(IOSIZE - (p - buffer)), true); } @@ -3254,6 +3223,7 @@ void fileinfo(int fullname, int shorthelp, bool dont_truncate) || (curbuf->b_flags & BF_WRITE_MASK) || curbuf->b_p_ro) ? " " : ""); + int n; // With 32 bit longs and more than 21,474,836 lines multiplying by 100 // causes an overflow, thus for large numbers divide instead. if (curwin->w_cursor.lnum > 1000000) { @@ -3342,10 +3312,7 @@ void maketitle(void) if (p_title) { if (p_titlelen > 0) { - maxlen = (int)(p_titlelen * Columns / 100); - if (maxlen < 10) { - maxlen = 10; - } + maxlen = MAX((int)(p_titlelen * Columns / 100), 10); } if (*p_titlestring != NUL) { @@ -3461,12 +3428,9 @@ void maketitle(void) icon_str = p_iconstring; } } else { - char *buf_p; - if (buf_spname(curbuf) != NULL) { - buf_p = buf_spname(curbuf); - } else { // use file name only in icon - buf_p = path_tail(curbuf->b_ffname); - } + char *buf_p = buf_spname(curbuf) != NULL + ? buf_spname(curbuf) + : path_tail(curbuf->b_ffname); // use file name only in icon *icon_str = NUL; // Truncate name at 100 bytes. int len = (int)strlen(buf_p); @@ -3631,23 +3595,18 @@ bool bt_prompt(buf_T *buf) /// Open a window for a number of buffers. void ex_buffer_all(exarg_T *eap) { - win_T *wp, *wpnext; + win_T *wpnext; int split_ret = OK; int open_wins = 0; - linenr_T count; // Maximum number of windows to open. - int all; // When true also load inactive buffers. int had_tab = cmdmod.cmod_tab; - if (eap->addr_count == 0) { // make as many windows as possible - count = 9999; - } else { - count = eap->line2; // make as many windows as specified - } - if (eap->cmdidx == CMD_unhide || eap->cmdidx == CMD_sunhide) { - all = false; - } else { - all = true; - } + // Maximum number of windows to open. + linenr_T count = eap->addr_count == 0 + ? 9999 // make as many windows as possible + : eap->line2; // make as many windows as specified + + // When true also load inactive buffers. + int all = eap->cmdidx != CMD_unhide && eap->cmdidx != CMD_sunhide; // Stop Visual mode, the cursor and "VIsual" may very well be invalid after // switching to another buffer. @@ -3663,7 +3622,7 @@ void ex_buffer_all(exarg_T *eap) while (true) { tabpage_T *tpnext = curtab->tp_next; // Try to close floating windows first - for (wp = lastwin->w_floating ? lastwin : firstwin; wp != NULL; wp = wpnext) { + for (win_T *wp = lastwin->w_floating ? lastwin : firstwin; wp != NULL; wp = wpnext) { wpnext = wp->w_floating ? wp->w_prev->w_floating ? wp->w_prev : firstwin : (wp->w_next == NULL || wp->w_next->w_floating) ? NULL : wp->w_next; @@ -3712,13 +3671,12 @@ void ex_buffer_all(exarg_T *eap) continue; } + win_T *wp; if (had_tab != 0) { // With the ":tab" modifier don't move the window. - if (buf->b_nwindows > 0) { - wp = lastwin; // buffer has a window, skip it - } else { - wp = NULL; - } + wp = buf->b_nwindows > 0 + ? lastwin // buffer has a window, skip it + : NULL; } else { // Check if this buffer already has a window for (wp = firstwin; wp != NULL; wp = wp->w_next) { @@ -3794,7 +3752,7 @@ void ex_buffer_all(exarg_T *eap) autocmd_no_leave--; // Close superfluous windows. - for (wp = lastwin; open_wins > count;) { + for (win_T *wp = lastwin; open_wins > count;) { bool r = (buf_hide(wp->w_buffer) || !bufIsChanged(wp->w_buffer) || autowrite(wp->w_buffer, false) == OK) && !is_aucmd_win(wp); if (!win_valid(wp)) { @@ -3862,7 +3820,6 @@ static int chk_modeline(linenr_T lnum, int flags) { char *s; char *e; - char *linecopy; // local copy of any modeline found intmax_t vers; int retval = OK; @@ -3907,6 +3864,7 @@ static int chk_modeline(linenr_T lnum, int flags) s++; } while (s[-1] != ':'); + char *linecopy; // local copy of any modeline found s = linecopy = xstrdup(s); // copy the line, it will change // prepare for emsg() diff --git a/src/nvim/bufwrite.c b/src/nvim/bufwrite.c index 9f6eb8c9e8..fa0a55e7b6 100644 --- a/src/nvim/bufwrite.c +++ b/src/nvim/bufwrite.c @@ -261,11 +261,8 @@ static int buf_write_convert(struct bw_info *ip, char **bufp, int *lenp) ip->bw_restlen += *lenp; break; } - if (n > 1) { - c = (unsigned)utf_ptr2char((char *)ip->bw_rest); - } else { - c = ip->bw_rest[0]; - } + c = (n > 1) ? (unsigned)utf_ptr2char((char *)ip->bw_rest) + : ip->bw_rest[0]; if (n >= ip->bw_restlen) { n -= ip->bw_restlen; ip->bw_restlen = 0; @@ -289,11 +286,8 @@ static int buf_write_convert(struct bw_info *ip, char **bufp, int *lenp) (size_t)ip->bw_restlen); break; } - if (n > 1) { - c = (unsigned)utf_ptr2char(*bufp + wlen); - } else { - c = (uint8_t)(*bufp)[wlen]; - } + c = n > 1 ? (unsigned)utf_ptr2char(*bufp + wlen) + : (uint8_t)(*bufp)[wlen]; } if (ucs2bytes(c, &p, flags) && !ip->bw_conv_error) { @@ -876,9 +870,7 @@ static int buf_write_make_backup(char *fname, bool append, FileInfo *file_info_o // Change one character, just before the extension. // char *wp = *backupp + strlen(*backupp) - 1 - strlen(backup_ext); - if (wp < *backupp) { // empty file name ??? - wp = *backupp; - } + wp = MAX(wp, *backupp); // empty file name ??? *wp = 'z'; while (*wp > 'a' && os_fileinfo(*backupp, &file_info_new)) { (*wp)--; @@ -993,9 +985,7 @@ nobackup: // Change one character, just before the extension. if (!p_bk && os_path_exists(*backupp)) { p = *backupp + strlen(*backupp) - 1 - strlen(backup_ext); - if (p < *backupp) { // empty file name ??? - p = *backupp; - } + p = MAX(p, *backupp); // empty file name ??? *p = 'z'; while (*p > 'a' && os_path_exists(*backupp)) { (*p)--; @@ -1255,9 +1245,7 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en status_redraw_all(); // redraw status lines later } - if (end > buf->b_ml.ml_line_count) { - end = buf->b_ml.ml_line_count; - } + end = MIN(end, buf->b_ml.ml_line_count); if (buf->b_ml.ml_flags & ML_EMPTY) { start = end + 1; } diff --git a/src/nvim/change.c b/src/nvim/change.c index 428a9187c8..6e9fab5a9b 100644 --- a/src/nvim/change.c +++ b/src/nvim/change.c @@ -523,19 +523,13 @@ void changed_lines_redraw_buf(buf_T *buf, linenr_T lnum, linenr_T lnume, linenr_ { if (buf->b_mod_set) { // find the maximum area that must be redisplayed - if (lnum < buf->b_mod_top) { - buf->b_mod_top = lnum; - } + buf->b_mod_top = MIN(buf->b_mod_top, lnum); if (lnum < buf->b_mod_bot) { // adjust old bot position for xtra lines buf->b_mod_bot += xtra; - if (buf->b_mod_bot < lnum) { - buf->b_mod_bot = lnum; - } - } - if (lnume + xtra > buf->b_mod_bot) { - buf->b_mod_bot = lnume + xtra; + buf->b_mod_bot = MAX(buf->b_mod_bot, lnum); } + buf->b_mod_bot = MAX(buf->b_mod_bot, lnume + xtra); buf->b_mod_xlines += xtra; } else { // set the area that must be redisplayed @@ -2262,9 +2256,7 @@ int get_last_leader_offset(char *line, char **flags) for (int off = (len2 > i ? i : len2); off > 0 && off + len1 > len2;) { off--; if (!strncmp(string + off, com_leader, (size_t)(len2 - off))) { - if (i - off < lower_check_bound) { - lower_check_bound = i - off; - } + lower_check_bound = MIN(lower_check_bound, i - off); } } } diff --git a/src/nvim/cmdexpand.c b/src/nvim/cmdexpand.c index e1a282cb0b..402a891099 100644 --- a/src/nvim/cmdexpand.c +++ b/src/nvim/cmdexpand.c @@ -670,10 +670,7 @@ static char *get_next_or_prev_match(int mode, expand_T *xp) ht -= 2; } findex -= ht; - if (findex < 0) { - // few entries left, select the first entry - findex = 0; - } + findex = MAX(findex, 0); // few entries left, select the first entry } } else if (mode == WILD_PAGEDOWN) { if (findex == xp->xp_numfiles - 1) { @@ -701,18 +698,10 @@ static char *get_next_or_prev_match(int mode, expand_T *xp) // When wrapping around, return the original string, set findex to -1. if (findex < 0) { - if (xp->xp_orig == NULL) { - findex = xp->xp_numfiles - 1; - } else { - findex = -1; - } + findex = xp->xp_orig == NULL ? xp->xp_numfiles - 1 : -1; } if (findex >= xp->xp_numfiles) { - if (xp->xp_orig == NULL) { - findex = 0; - } else { - findex = -1; - } + findex = xp->xp_orig == NULL ? 0 : -1; } if (compl_match_array) { compl_selected = findex; @@ -1112,9 +1101,7 @@ int showmatches(expand_T *xp, bool wildmenu) } else { j = vim_strsize(SHOW_MATCH(i)); } - if (j > maxlen) { - maxlen = j; - } + maxlen = MAX(maxlen, j); } if (xp->xp_context == EXPAND_TAGS_LISTFILES) { diff --git a/src/nvim/cursor.c b/src/nvim/cursor.c index 2e9e68843a..35afca2fe9 100644 --- a/src/nvim/cursor.c +++ b/src/nvim/cursor.c @@ -216,12 +216,7 @@ static int coladvance2(win_T *wp, pos_T *pos, bool addspaces, bool finetune, col } } - if (idx < 0) { - pos->col = 0; - } else { - pos->col = idx; - } - + pos->col = MAX(idx, 0); pos->coladd = 0; if (finetune) { @@ -310,15 +305,9 @@ linenr_T get_cursor_rel_lnum(win_T *wp, linenr_T lnum) /// This allows for the col to be on the NUL byte. void check_pos(buf_T *buf, pos_T *pos) { - if (pos->lnum > buf->b_ml.ml_line_count) { - pos->lnum = buf->b_ml.ml_line_count; - } - + pos->lnum = MIN(pos->lnum, buf->b_ml.ml_line_count); if (pos->col > 0) { - colnr_T len = ml_get_buf_len(buf, pos->lnum); - if (pos->col > len) { - pos->col = len; - } + pos->col = MIN(pos->col, ml_get_buf_len(buf, pos->lnum)); } } @@ -385,9 +374,7 @@ void check_cursor_col(win_T *win) int cs, ce; getvcol(win, &win->w_cursor, &cs, NULL, &ce); - if (win->w_cursor.coladd > ce - cs) { - win->w_cursor.coladd = ce - cs; - } + win->w_cursor.coladd = MIN(win->w_cursor.coladd, ce - cs); } } else { // avoid weird number when there is a miscalculation or overflow diff --git a/src/nvim/diff.c b/src/nvim/diff.c index 45bcb94a1f..0b9bdb6181 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -694,7 +694,7 @@ void diff_redraw(bool dofold) if (((wp != curwin) && (wp->w_topfill > 0)) || (n > 0)) { if (wp->w_topfill > n) { - wp->w_topfill = (n < 0 ? 0 : n); + wp->w_topfill = MAX(n, 0); } else if ((n > 0) && (n > wp->w_topfill)) { wp->w_topfill = n; if (wp == curwin) { @@ -2679,9 +2679,7 @@ bool diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp) si_org -= utf_head_off(line_org, line_org + si_org); si_new -= utf_head_off(line_new, line_new + si_new); - if (*startp > si_org) { - *startp = si_org; - } + *startp = MIN(*startp, si_org); // Search for end of difference, if any. if ((line_org[si_org] != NUL) || (line_new[si_new] != NUL)) { @@ -2721,9 +2719,7 @@ bool diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp) } } - if (*endp < ei_org) { - *endp = ei_org; - } + *endp = MAX(*endp, ei_org); } } } @@ -3051,19 +3047,11 @@ static void diffgetput(const int addr_count, const int idx_cur, const int idx_fr // range ends above end of current/from diff block if (idx_cur == idx_from) { // :diffput - int i = dp->df_count[idx_cur] - start_skip - end_skip; - - if (count > i) { - count = i; - } + count = MIN(count, dp->df_count[idx_cur] - start_skip - end_skip); } else { // :diffget count -= end_skip; - end_skip = dp->df_count[idx_from] - start_skip - count; - - if (end_skip < 0) { - end_skip = 0; - } + end_skip = MAX(dp->df_count[idx_from] - start_skip - count, 0); } } else { end_skip = 0; @@ -3249,9 +3237,7 @@ int diff_move_to(int dir, int count) } // don't end up past the end of the file - if (lnum > curbuf->b_ml.ml_line_count) { - lnum = curbuf->b_ml.ml_line_count; - } + lnum = MIN(lnum, curbuf->b_ml.ml_line_count); // When the cursor didn't move at all we fail. if (lnum == curwin->w_cursor.lnum) { @@ -3297,10 +3283,7 @@ static linenr_T diff_get_corresponding_line_int(buf_T *buf1, linenr_T lnum1) if ((dp->df_lnum[idx1] + dp->df_count[idx1]) > lnum1) { // Inside the diffblock baseline = lnum1 - dp->df_lnum[idx1]; - - if (baseline > dp->df_count[idx2]) { - baseline = dp->df_count[idx2]; - } + baseline = MIN(baseline, dp->df_count[idx2]); return dp->df_lnum[idx2] + baseline; } @@ -3335,10 +3318,7 @@ linenr_T diff_get_corresponding_line(buf_T *buf1, linenr_T lnum1) linenr_T lnum = diff_get_corresponding_line_int(buf1, lnum1); // don't end up past the end of the file - if (lnum > curbuf->b_ml.ml_line_count) { - return curbuf->b_ml.ml_line_count; - } - return lnum; + return MIN(lnum, curbuf->b_ml.ml_line_count); } /// For line "lnum" in the current window find the equivalent lnum in window @@ -3381,10 +3361,7 @@ linenr_T diff_lnum_win(linenr_T lnum, win_T *wp) } linenr_T n = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]); - if (n > dp->df_lnum[i] + dp->df_count[i]) { - n = dp->df_lnum[i] + dp->df_count[i]; - } - return n; + return MIN(n, dp->df_lnum[i] + dp->df_count[i]); } /// Handle an ED style diff line. diff --git a/src/nvim/drawline.c b/src/nvim/drawline.c index 14b3b3e693..47d84e6539 100644 --- a/src/nvim/drawline.c +++ b/src/nvim/drawline.c @@ -886,9 +886,7 @@ static int get_rightmost_vcol(win_T *wp, const int *color_cols) if (color_cols) { // determine rightmost colorcolumn to possibly draw for (int i = 0; color_cols[i] >= 0; i++) { - if (ret < color_cols[i]) { - ret = color_cols[i]; - } + ret = MAX(ret, color_cols[i]); } } @@ -2560,9 +2558,7 @@ int win_line(win_T *wp, linenr_T lnum, int startrow, int endrow, int col_rows, s // Highlight 'cursorcolumn' & 'colorcolumn' past end of the line. // check if line ends before left margin - if (wlv.vcol < start_col + wlv.col - win_col_off(wp)) { - wlv.vcol = start_col + wlv.col - win_col_off(wp); - } + wlv.vcol = MAX(wlv.vcol, start_col + wlv.col - win_col_off(wp)); // Get rid of the boguscols now, we want to draw until the right // edge for 'cursorcolumn'. wlv.col -= wlv.boguscols; diff --git a/src/nvim/drawscreen.c b/src/nvim/drawscreen.c index 770f8da6cf..425651b60e 100644 --- a/src/nvim/drawscreen.c +++ b/src/nvim/drawscreen.c @@ -395,18 +395,9 @@ void screen_resize(int width, int height) void check_screensize(void) { // Limit Rows and Columns to avoid an overflow in Rows * Columns. - if (Rows < min_rows()) { - // need room for one window and command line - Rows = min_rows(); - } else if (Rows > 1000) { - Rows = 1000; - } - - if (Columns < MIN_COLUMNS) { - Columns = MIN_COLUMNS; - } else if (Columns > 10000) { - Columns = 10000; - } + // need room for one window and command line + Rows = MIN(MAX(Rows, min_rows()), 1000); + Columns = MIN(MAX(Columns, MIN_COLUMNS), 10000); } /// Return true if redrawing should currently be done. @@ -996,12 +987,9 @@ int showmode(void) } if (edit_submode_extra != NULL) { msg_puts_attr(" ", attr); // Add a space in between. - int sub_attr; - if (edit_submode_highl < HLF_COUNT) { - sub_attr = win_hl_attr(curwin, (int)edit_submode_highl); - } else { - sub_attr = attr; - } + int sub_attr = edit_submode_highl < HLF_COUNT + ? win_hl_attr(curwin, (int)edit_submode_highl) + : attr; msg_puts_attr(edit_submode_extra, sub_attr); } } @@ -1564,9 +1552,7 @@ static void win_update(win_T *wp) // in a pattern match. if (syntax_present(wp)) { mod_top -= buf->b_s.b_syn_sync_linebreaks; - if (mod_top < 1) { - mod_top = 1; - } + mod_top = MAX(mod_top, 1); } } if (mod_bot == 0 || mod_bot < buf->b_mod_bot) { @@ -1635,17 +1621,13 @@ static void win_update(win_T *wp) } hasFolding(wp, mod_top, &mod_top, NULL); - if (mod_top > lnumt) { - mod_top = lnumt; - } + mod_top = MIN(mod_top, lnumt); // Now do the same for the bottom line (one above mod_bot). mod_bot--; hasFolding(wp, mod_bot, NULL, &mod_bot); mod_bot++; - if (mod_bot < lnumb) { - mod_bot = lnumb; - } + mod_bot = MAX(mod_bot, lnumb); } // When a change starts above w_topline and the end is below @@ -1865,18 +1847,8 @@ static void win_update(win_T *wp) to = curwin->w_cursor.lnum; } // redraw more when the cursor moved as well - if (wp->w_old_cursor_lnum < from) { - from = wp->w_old_cursor_lnum; - } - if (wp->w_old_cursor_lnum > to) { - to = wp->w_old_cursor_lnum; - } - if (wp->w_old_visual_lnum < from) { - from = wp->w_old_visual_lnum; - } - if (wp->w_old_visual_lnum > to) { - to = wp->w_old_visual_lnum; - } + from = MIN(MIN(from, wp->w_old_cursor_lnum), wp->w_old_visual_lnum); + to = MAX(MAX(to, wp->w_old_cursor_lnum), wp->w_old_visual_lnum); } else { // Find the line numbers that need to be updated: The lines // between the old cursor position and the current cursor @@ -1898,15 +1870,8 @@ static void win_update(win_T *wp) && wp->w_old_visual_lnum != 0) { from = wp->w_old_visual_lnum; } - if (wp->w_old_visual_lnum > to) { - to = wp->w_old_visual_lnum; - } - if (VIsual.lnum < from) { - from = VIsual.lnum; - } - if (VIsual.lnum > to) { - to = VIsual.lnum; - } + to = MAX(MAX(to, wp->w_old_visual_lnum), VIsual.lnum); + from = MIN(from, VIsual.lnum); } } @@ -1941,9 +1906,7 @@ static void win_update(win_T *wp) pos.col = (colnr_T)strlen(ml_get_buf(wp->w_buffer, pos.lnum)); getvvcol(wp, &pos, NULL, NULL, &t); - if (toc < t) { - toc = t; - } + toc = MAX(toc, t); } toc++; } else { @@ -1953,12 +1916,8 @@ static void win_update(win_T *wp) if (fromc != wp->w_old_cursor_fcol || toc != wp->w_old_cursor_lcol) { - if (from > VIsual.lnum) { - from = VIsual.lnum; - } - if (to < VIsual.lnum) { - to = VIsual.lnum; - } + from = MIN(from, VIsual.lnum); + to = MAX(to, VIsual.lnum); } wp->w_old_cursor_fcol = fromc; wp->w_old_cursor_lcol = toc; @@ -1975,19 +1934,13 @@ static void win_update(win_T *wp) } // There is no need to update lines above the top of the window. - if (from < wp->w_topline) { - from = wp->w_topline; - } + from = MAX(from, wp->w_topline); // If we know the value of w_botline, use it to restrict the update to // the lines that are visible in the window. if (wp->w_valid & VALID_BOTLINE) { - if (from >= wp->w_botline) { - from = wp->w_botline - 1; - } - if (to >= wp->w_botline) { - to = wp->w_botline - 1; - } + from = MIN(from, wp->w_botline - 1); + to = MIN(to, wp->w_botline - 1); } // Find the minimal part to be updated. @@ -2175,11 +2128,9 @@ static void win_update(win_T *wp) if (hasFolding(wp, l, NULL, &l)) { new_rows++; } else if (l == wp->w_topline) { - int n = plines_win_nofill(wp, l, false) + wp->w_topfill; - n -= adjust_plines_for_skipcol(wp); - if (n > wp->w_height_inner) { - n = wp->w_height_inner; - } + int n = plines_win_nofill(wp, l, false) + wp->w_topfill + - adjust_plines_for_skipcol(wp); + n = MIN(n, wp->w_height_inner); new_rows += n; } else { new_rows += plines_win(wp, l, true); @@ -2244,16 +2195,12 @@ static void win_update(win_T *wp) x += wp->w_lines[j++].wl_size; i++; } - if (bot_start > x) { - bot_start = x; - } + bot_start = MIN(bot_start, x); } else { // j > i // move entries in w_lines[] downwards j -= i; wp->w_lines_valid += (linenr_T)j; - if (wp->w_lines_valid > wp->w_grid.rows) { - wp->w_lines_valid = wp->w_grid.rows; - } + wp->w_lines_valid = MIN(wp->w_lines_valid, wp->w_grid.rows); for (i = wp->w_lines_valid; i - j >= idx; i--) { wp->w_lines[i] = wp->w_lines[i - j]; } @@ -2385,9 +2332,7 @@ redr_statuscol: wp->w_last_cursor_lnum_rnu = wp->w_p_rnu ? wp->w_cursor.lnum : 0; - if (idx > wp->w_lines_valid) { - wp->w_lines_valid = idx; - } + wp->w_lines_valid = MAX(wp->w_lines_valid, idx); // Let the syntax stuff know we stop parsing here. if (syntax_last_parsed != 0 && syntax_present(wp)) { @@ -2602,10 +2547,7 @@ int compute_foldcolumn(win_T *wp, int col) int wmw = wp == curwin && p_wmw == 0 ? 1 : (int)p_wmw; int wwidth = wp->w_grid.cols; - if (fdc > wwidth - (col + wmw)) { - fdc = wwidth - (col + wmw); - } - return fdc; + return MIN(fdc, wwidth - (col + wmw)); } /// Return the width of the 'number' and 'relativenumber' column. @@ -2642,9 +2584,7 @@ int number_width(win_T *wp) } while (lnum > 0); // 'numberwidth' gives the minimal width plus one - if (n < wp->w_p_nuw - 1) { - n = (int)wp->w_p_nuw - 1; - } + n = MAX(n, (int)wp->w_p_nuw - 1); // If 'signcolumn' is set to 'number' and there is a sign to display, then // the minimal width for the number column is 2. @@ -2669,9 +2609,7 @@ void redraw_later(win_T *wp, int type) if (type >= UPD_NOT_VALID) { wp->w_lines_valid = 0; } - if (must_redraw < type) { // must_redraw is the maximum of all windows - must_redraw = type; - } + must_redraw = MAX(must_redraw, type); // must_redraw is the maximum of all windows } } @@ -2689,8 +2627,8 @@ void redraw_all_later(int type) /// or it is currently not allowed. void set_must_redraw(int type) { - if (!redraw_not_allowed && must_redraw < type) { - must_redraw = type; + if (!redraw_not_allowed) { + must_redraw = MAX(must_redraw, type); } } diff --git a/src/nvim/edit.c b/src/nvim/edit.c index 5c88cb59e1..00ce38c4b1 100644 --- a/src/nvim/edit.c +++ b/src/nvim/edit.c @@ -271,11 +271,7 @@ static void insert_enter(InsertState *s) if (restart_edit != 0 && stuff_empty()) { // After a paste we consider text typed to be part of the insert for // the pasted text. You can backspace over the pasted text too. - if (where_paste_started.lnum) { - arrow_used = false; - } else { - arrow_used = true; - } + arrow_used = where_paste_started.lnum == 0; restart_edit = 0; // If the cursor was after the end-of-line before the CTRL-O and it is @@ -1541,9 +1537,7 @@ static void init_prompt(int cmdchar_todo) if (cmdchar_todo == 'A') { coladvance(curwin, MAXCOL); } - if (curwin->w_cursor.col < (colnr_T)strlen(prompt)) { - curwin->w_cursor.col = (colnr_T)strlen(prompt); - } + curwin->w_cursor.col = MAX(curwin->w_cursor.col, (colnr_T)strlen(prompt)); // Make sure the cursor is in a valid position. check_cursor(curwin); } @@ -1578,7 +1572,7 @@ void edit_unputchar(void) /// text. Only works when cursor is in the line that changes. void display_dollar(colnr_T col_arg) { - colnr_T col = col_arg < 0 ? 0 : col_arg; + colnr_T col = MAX(col_arg, 0); if (!redrawing()) { return; @@ -1736,12 +1730,7 @@ void change_indent(int type, int amount, int round, int replaced, bool call_chan } curwin->w_p_list = save_p_list; - - if (new_cursor_col <= 0) { - curwin->w_cursor.col = 0; - } else { - curwin->w_cursor.col = (colnr_T)new_cursor_col; - } + curwin->w_cursor.col = MAX(0, (colnr_T)new_cursor_col); curwin->w_set_curswant = true; changed_cline_bef_curs(curwin); @@ -2607,9 +2596,7 @@ void cursor_up_inner(win_T *wp, linenr_T n) hasFolding(wp, lnum, &lnum, NULL); } } - if (lnum < 1) { - lnum = 1; - } + lnum = MAX(lnum, 1); } else { lnum -= n; } @@ -2659,9 +2646,7 @@ void cursor_down_inner(win_T *wp, int n) break; } } - if (lnum > line_count) { - lnum = line_count; - } + lnum = MIN(lnum, line_count); } else { lnum += (linenr_T)n; } @@ -4726,9 +4711,7 @@ static void ins_try_si(int c) } // Adjust ai_col, the char at this position can be deleted. - if (ai_col > curwin->w_cursor.col) { - ai_col = curwin->w_cursor.col; - } + ai_col = MIN(ai_col, curwin->w_cursor.col); } // Get the value that w_virtcol would have when 'list' is off. diff --git a/src/nvim/ex_cmds.c b/src/nvim/ex_cmds.c index e4e9075afa..5cc9a8c106 100644 --- a/src/nvim/ex_cmds.c +++ b/src/nvim/ex_cmds.c @@ -311,9 +311,7 @@ void ex_align(exarg_T *eap) } } } - if (new_indent < 0) { - new_indent = 0; - } + new_indent = MAX(new_indent, 0); set_indent(new_indent, 0); // set indent } changed_lines(curbuf, eap->line1, 0, eap->line2 + 1, 0, true); @@ -543,9 +541,7 @@ void ex_sort(exarg_T *eap) for (linenr_T lnum = eap->line1; lnum <= eap->line2; lnum++) { char *s = ml_get(lnum); int len = ml_get_len(lnum); - if (maxlen < len) { - maxlen = len; - } + maxlen = MAX(maxlen, len); colnr_T start_col = 0; colnr_T end_col = len; @@ -705,11 +701,6 @@ sortend: /// @return FAIL for failure, OK otherwise int do_move(linenr_T line1, linenr_T line2, linenr_T dest) { - linenr_T l; - linenr_T extra; // Num lines added before line1 - linenr_T num_lines; // Num lines moved - linenr_T last_line; // Last line in file after adding new text - if (dest >= line1 && dest < line2) { emsg(_("E134: Cannot move a range of lines into itself")); return FAIL; @@ -720,11 +711,9 @@ int do_move(linenr_T line1, linenr_T line2, linenr_T dest) if (dest == line1 - 1 || dest == line2) { // Move the cursor as if lines were moved (see below) to be backwards // compatible. - if (dest >= line1) { - curwin->w_cursor.lnum = dest; - } else { - curwin->w_cursor.lnum = dest + (line2 - line1) + 1; - } + curwin->w_cursor.lnum = dest >= line1 + ? dest + : dest + (line2 - line1) + 1; return OK; } @@ -733,13 +722,16 @@ int do_move(linenr_T line1, linenr_T line2, linenr_T dest) bcount_t extent_byte = end_byte - start_byte; bcount_t dest_byte = ml_find_line_or_offset(curbuf, dest + 1, NULL, true); - num_lines = line2 - line1 + 1; + linenr_T num_lines = line2 - line1 + 1; // Num lines moved // First we copy the old text to its new location -- webb // Also copy the flag that ":global" command uses. if (u_save(dest, dest + 1) == FAIL) { return FAIL; } + + linenr_T l; + linenr_T extra; // Num lines added before line1 for (extra = 0, l = line1; l <= line2; l++) { char *str = xstrnsave(ml_get(l + extra), (size_t)ml_get_len(l + extra)); ml_append(dest + l - line1, str, 0, false); @@ -762,7 +754,7 @@ int do_move(linenr_T line1, linenr_T line2, linenr_T dest) // // And Finally we adjust the marks we put at the end of the file back to // their final destination at the new text position -- webb - last_line = curbuf->b_ml.ml_line_count; + linenr_T last_line = curbuf->b_ml.ml_line_count; // Last line in file after adding new text mark_adjust_nofold(line1, line2, last_line - line2, 0, kExtmarkNOOP); disable_fold_update++; @@ -838,9 +830,7 @@ int do_move(linenr_T line1, linenr_T line2, linenr_T dest) if (line1 < dest) { dest += num_lines + 1; last_line = curbuf->b_ml.ml_line_count; - if (dest > last_line + 1) { - dest = last_line + 1; - } + dest = MIN(dest, last_line + 1); changed_lines(curbuf, line1, 0, dest, 0, false); } else { changed_lines(curbuf, dest + 1, 0, line1 + num_lines, 0, false); @@ -2931,9 +2921,7 @@ void ex_z(exarg_T *eap) } else { bigness = curwin->w_height_inner - 3; } - if (bigness < 1) { - bigness = 1; - } + bigness = MAX(bigness, 1); char *x = eap->arg; char *kind = x; @@ -3006,19 +2994,9 @@ void ex_z(exarg_T *eap) break; } - if (start < 1) { - start = 1; - } - - if (end > curbuf->b_ml.ml_line_count) { - end = curbuf->b_ml.ml_line_count; - } - - if (curs > curbuf->b_ml.ml_line_count) { - curs = curbuf->b_ml.ml_line_count; - } else if (curs < 1) { - curs = 1; - } + start = MAX(start, 1); + end = MIN(end, curbuf->b_ml.ml_line_count); + curs = MIN(MAX(curs, 1), curbuf->b_ml.ml_line_count); for (linenr_T i = start; i <= end; i++) { if (minus && i == lnum) { @@ -3459,9 +3437,7 @@ static int do_sub(exarg_T *eap, const proftime_T timeout, const int cmdpreview_n } eap->line1 = eap->line2; eap->line2 += (linenr_T)i - 1; - if (eap->line2 > curbuf->b_ml.ml_line_count) { - eap->line2 = curbuf->b_ml.ml_line_count; - } + eap->line2 = MIN(eap->line2, curbuf->b_ml.ml_line_count); } // check for trailing command or garbage @@ -3725,10 +3701,8 @@ static int do_sub(exarg_T *eap, const proftime_T timeout, const int cmdpreview_n print_line_no_prefix(lnum, subflags.do_number, subflags.do_list); getvcol(curwin, &curwin->w_cursor, &sc, NULL, NULL); - curwin->w_cursor.col = regmatch.endpos[0].col - 1; - if (curwin->w_cursor.col < 0) { - curwin->w_cursor.col = 0; - } + curwin->w_cursor.col = MAX(regmatch.endpos[0].col - 1, 0); + getvcol(curwin, &curwin->w_cursor, NULL, NULL, &ec); curwin->w_cursor.col = regmatch.startpos[0].col; if (subflags.do_number || curwin->w_p_nu) { diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index b252ba2b20..2defd580fc 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -1079,7 +1079,6 @@ void *getline_cookie(LineGetter fgetline, void *cookie) /// @return the buffer number. static int compute_buffer_local_count(cmd_addr_T addr_type, linenr_T lnum, int offset) { - buf_T *nextbuf; int count = offset; buf_T *buf = firstbuf; @@ -1088,7 +1087,7 @@ static int compute_buffer_local_count(cmd_addr_T addr_type, linenr_T lnum, int o } while (count != 0) { count += (count < 0) ? 1 : -1; - nextbuf = (offset < 0) ? buf->b_prev : buf->b_next; + buf_T *nextbuf = (offset < 0) ? buf->b_prev : buf->b_next; if (nextbuf == NULL) { break; } @@ -1107,7 +1106,7 @@ static int compute_buffer_local_count(cmd_addr_T addr_type, linenr_T lnum, int o // we might have gone too far, last buffer is not loaded if (addr_type == ADDR_LOADED_BUFFERS) { while (buf->b_ml.ml_mfp == NULL) { - nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next; + buf_T *nextbuf = (offset >= 0) ? buf->b_prev : buf->b_next; if (nextbuf == NULL) { break; } @@ -1696,9 +1695,7 @@ static int execute_cmd0(int *retv, exarg_T *eap, const char **errormsg, bool pre // ":silent! try" was used, it should only apply to :try itself. if (eap->cmdidx == CMD_try && cmdmod.cmod_did_esilent > 0) { emsg_silent -= cmdmod.cmod_did_esilent; - if (emsg_silent < 0) { - emsg_silent = 0; - } + emsg_silent = MAX(emsg_silent, 0); cmdmod.cmod_did_esilent = 0; } @@ -2444,9 +2441,7 @@ static char *ex_range_without_command(exarg_T *eap) ex_print(eap); } } else if (eap->addr_count != 0) { - if (eap->line2 > curbuf->b_ml.ml_line_count) { - eap->line2 = curbuf->b_ml.ml_line_count; - } + eap->line2 = MIN(eap->line2, curbuf->b_ml.ml_line_count); if (eap->line2 < 0) { errormsg = _(e_invrange); @@ -2785,9 +2780,7 @@ void undo_cmdmod(cmdmod_T *cmod) msg_silent = cmod->cmod_save_msg_silent - 1; } emsg_silent -= cmod->cmod_did_esilent; - if (emsg_silent < 0) { - emsg_silent = 0; - } + emsg_silent = MAX(emsg_silent, 0); // Restore msg_scroll, it's set by file I/O commands, even when no // message is actually displayed. msg_scroll = cmod->cmod_save_msg_scroll; @@ -3520,11 +3513,7 @@ static linenr_T get_address(exarg_T *eap, char **ptr, cmd_addr_T addr_type, bool // This makes sure we never match in the current // line, and can match anywhere in the // next/previous line. - if (c == '/' && curwin->w_cursor.lnum > 0) { - curwin->w_cursor.col = MAXCOL; - } else { - curwin->w_cursor.col = 0; - } + curwin->w_cursor.col = (c == '/' && curwin->w_cursor.lnum > 0) ? MAXCOL : 0; searchcmdlen = 0; flags = silent ? 0 : SEARCH_HIS | SEARCH_MSG; if (!do_search(NULL, c, c, cmd, strlen(cmd), 1, flags, NULL)) { @@ -4793,11 +4782,9 @@ static void ex_cquit(exarg_T *eap) int before_quit_all(exarg_T *eap) { if (cmdwin_type != 0) { - if (eap->forceit) { - cmdwin_result = K_XF1; // open_cmdwin() takes care of this - } else { - cmdwin_result = K_XF2; - } + cmdwin_result = eap->forceit + ? K_XF1 // open_cmdwin() takes care of this + : K_XF2; return FAIL; } @@ -5594,7 +5581,6 @@ static void ex_swapname(exarg_T *eap) static void ex_syncbind(exarg_T *eap) { linenr_T topline; - int y; linenr_T old_linenr = curwin->w_cursor.lnum; setpcmark(); @@ -5604,15 +5590,10 @@ static void ex_syncbind(exarg_T *eap) topline = curwin->w_topline; FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { if (wp->w_p_scb && wp->w_buffer) { - y = wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value(curwin); - if (topline > y) { - topline = y; - } + topline = MIN(topline, wp->w_buffer->b_ml.ml_line_count - get_scrolloff_value(curwin)); } } - if (topline < 1) { - topline = 1; - } + topline = MAX(topline, 1); } else { topline = 1; } @@ -5620,7 +5601,7 @@ static void ex_syncbind(exarg_T *eap) // Set all scrollbind windows to the same topline. FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { if (wp->w_p_scb) { - y = topline - wp->w_topline; + int y = topline - wp->w_topline; if (y > 0) { scrollup(wp, y, true); } else { diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index e3c7008d3b..c8ca6f0e4e 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -390,13 +390,8 @@ static bool do_incsearch_highlighting(int firstc, int *search_delim, incsearch_s parse_cmd_address(&ea, &dummy, true); if (ea.addr_count > 0) { // Allow for reverse match. - if (ea.line2 < ea.line1) { - search_first_line = ea.line2; - search_last_line = ea.line1; - } else { - search_first_line = ea.line1; - search_last_line = ea.line2; - } + search_first_line = MIN(ea.line1, ea.line1); + search_last_line = MAX(ea.line2, ea.line1); } else if (cmd[0] == 's' && cmd[1] != 'o') { // :s defaults to the current line search_first_line = curwin->w_cursor.lnum; @@ -1034,11 +1029,7 @@ static int command_line_handle_ctrl_bsl(CommandLineState *s) // Restore the cursor or use the position set with // set_cmdline_pos(). - if (new_cmdpos > ccline.cmdlen) { - ccline.cmdpos = ccline.cmdlen; - } else { - ccline.cmdpos = new_cmdpos; - } + ccline.cmdpos = MIN(ccline.cmdlen, new_cmdpos); KeyTyped = false; // Don't do p_wc completion. redrawcmd(); @@ -1658,11 +1649,7 @@ static int command_line_insert_reg(CommandLineState *s) KeyTyped = false; // Don't do p_wc completion. if (new_cmdpos >= 0) { // set_cmdline_pos() was used - if (new_cmdpos > ccline.cmdlen) { - ccline.cmdpos = ccline.cmdlen; - } else { - ccline.cmdpos = new_cmdpos; - } + ccline.cmdpos = MIN(ccline.cmdlen, new_cmdpos); } } new_cmdpos = save_new_cmdpos; @@ -3546,10 +3533,6 @@ void unputcmdline(void) // called afterwards. void put_on_cmdline(const char *str, int len, bool redraw) { - int i; - int m; - int c; - if (len < 0) { len = (int)strlen(str); } @@ -3563,7 +3546,8 @@ void put_on_cmdline(const char *str, int len, bool redraw) ccline.cmdlen += len; } else { // Count nr of characters in the new string. - m = 0; + int m = 0; + int i; for (i = 0; i < len; i += utfc_ptr2len(str + i)) { m++; } @@ -3587,8 +3571,8 @@ void put_on_cmdline(const char *str, int len, bool redraw) { // When the inserted text starts with a composing character, // backup to the character before it. There could be two of them. - i = 0; - c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); + int i = 0; + int c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); while (ccline.cmdpos > 0 && utf_iscomposing(c)) { i = utf_head_off(ccline.cmdbuff, ccline.cmdbuff + ccline.cmdpos - 1) + 1; ccline.cmdpos -= i; @@ -3619,7 +3603,7 @@ void put_on_cmdline(const char *str, int len, bool redraw) if (redraw && !cmd_silent) { msg_no_more = true; - i = cmdline_row; + int i = cmdline_row; cursorcmd(); draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); // Avoid clearing the rest of the line too often. @@ -3628,6 +3612,7 @@ void put_on_cmdline(const char *str, int len, bool redraw) } msg_no_more = false; } + int m; if (KeyTyped) { m = Columns * Rows; if (m < 0) { // overflow, Columns or Rows at weird value @@ -3636,8 +3621,8 @@ void put_on_cmdline(const char *str, int len, bool redraw) } else { m = MAXCOL; } - for (i = 0; i < len; i++) { - c = cmdline_charsize(ccline.cmdpos); + for (int i = 0; i < len; i++) { + int c = cmdline_charsize(ccline.cmdpos); // count ">" for a double-wide char that doesn't fit. correct_screencol(ccline.cmdpos, c, &ccline.cmdspos); // Stop cursor at the end of the screen, but do increment the @@ -3647,9 +3632,7 @@ void put_on_cmdline(const char *str, int len, bool redraw) ccline.cmdspos += c; } c = utfc_ptr2len(ccline.cmdbuff + ccline.cmdpos) - 1; - if (c > len - i - 1) { - c = len - i - 1; - } + c = MIN(c, len - i - 1); ccline.cmdpos += c; i += c; ccline.cmdpos++; @@ -3886,17 +3869,13 @@ void cursorcmd(void) } if (ui_has(kUICmdline)) { - if (ccline.redraw_state < kCmdRedrawPos) { - ccline.redraw_state = kCmdRedrawPos; - } + ccline.redraw_state = MAX(ccline.redraw_state, kCmdRedrawPos); return; } msg_row = cmdline_row + (ccline.cmdspos / Columns); msg_col = ccline.cmdspos % Columns; - if (msg_row >= Rows) { - msg_row = Rows - 1; - } + msg_row = MIN(msg_row, Rows - 1); msg_cursor_goto(msg_row, msg_col); } @@ -4192,11 +4171,7 @@ static int set_cmdline_pos(int pos) // The position is not set directly but after CTRL-\ e or CTRL-R = has // changed the command line. - if (pos < 0) { - new_cmdpos = 0; - } else { - new_cmdpos = pos; - } + new_cmdpos = MAX(0, pos); return 0; } diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 3078f00fbb..6fc1d0717a 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -888,10 +888,7 @@ retry: // Use buffer >= 64K. Add linerest to double the size if the // line gets very long, to avoid a lot of copying. But don't // read more than 1 Mbyte at a time, so we can be interrupted. - size = 0x10000 + linerest; - if (size > 0x100000) { - size = 0x100000; - } + size = MIN(0x10000 + linerest, 0x100000); } // Protect against the argument of lalloc() going negative. @@ -2800,9 +2797,7 @@ int check_timestamps(int focus) bufref_T bufref; set_bufref(&bufref, buf); const int n = buf_check_timestamp(buf); - if (didit < n) { - didit = n; - } + didit = MAX(didit, n); if (n > 0 && !bufref_valid(&bufref)) { // Autocommands have removed the buffer, start at the first one again. buf = firstbuf; @@ -3192,11 +3187,7 @@ void buf_reload(buf_T *buf, int orig_mode, bool reload_options) // Restore the topline and cursor position and check it (lines may // have been removed). - if (old_topline > curbuf->b_ml.ml_line_count) { - curwin->w_topline = curbuf->b_ml.ml_line_count; - } else { - curwin->w_topline = old_topline; - } + curwin->w_topline = MIN(old_topline, curbuf->b_ml.ml_line_count); curwin->w_cursor = old_cursor; check_cursor(curwin); update_topline(curwin); diff --git a/src/nvim/fold.c b/src/nvim/fold.c index 78fe33cc78..c8e6a25dbc 100644 --- a/src/nvim/fold.c +++ b/src/nvim/fold.c @@ -248,9 +248,7 @@ bool hasFoldingWin(win_T *const win, const linenr_T lnum, linenr_T *const firstp return false; } - if (last > win->w_buffer->b_ml.ml_line_count) { - last = win->w_buffer->b_ml.ml_line_count; - } + last = MIN(last, win->w_buffer->b_ml.ml_line_count); if (lastp != NULL) { *lastp = last; } @@ -618,15 +616,11 @@ void foldCreate(win_T *wp, pos_T start, pos_T end) ga_grow(&fold_ga, cont); // If the first fold starts before the new fold, let the new fold // start there. Otherwise the existing fold would change. - if (start_rel.lnum > fp->fd_top) { - start_rel.lnum = fp->fd_top; - } + start_rel.lnum = MIN(start_rel.lnum, fp->fd_top); // When last contained fold isn't completely contained, adjust end // of new fold. - if (end_rel.lnum < fp[cont - 1].fd_top + fp[cont - 1].fd_len - 1) { - end_rel.lnum = fp[cont - 1].fd_top + fp[cont - 1].fd_len - 1; - } + end_rel.lnum = MAX(end_rel.lnum, fp[cont - 1].fd_top + fp[cont - 1].fd_len - 1); // Move contained folds to inside new fold memmove(fold_ga.ga_data, fp, sizeof(fold_T) * (size_t)cont); fold_ga.ga_len += cont; @@ -722,12 +716,8 @@ void deleteFold(win_T *const wp, const linenr_T start, const linenr_T end, const (int)(found_fp - (fold_T *)found_ga->ga_data), recursive); } else { - if (first_lnum > found_fp->fd_top + found_off) { - first_lnum = found_fp->fd_top + found_off; - } - if (last_lnum < lnum) { - last_lnum = lnum; - } + first_lnum = MIN(first_lnum, found_fp->fd_top + found_off); + last_lnum = MAX(last_lnum, lnum); if (!did_one) { parseMarker(wp); } @@ -788,14 +778,10 @@ void foldUpdate(win_T *wp, linenr_T top, linenr_T bot) } if (wp->w_folds.ga_len > 0) { - linenr_T maybe_small_start = top; - linenr_T maybe_small_end = bot; - // Mark all folds from top to bot (or bot to top) as maybe-small. - if (top > bot) { - maybe_small_start = bot; - maybe_small_end = top; - } + linenr_T maybe_small_start = MIN(top, bot); + linenr_T maybe_small_end = MAX(top, bot); + fold_T *fp; foldFind(&wp->w_folds, maybe_small_start, &fp); while (fp < (fold_T *)wp->w_folds.ga_data + wp->w_folds.ga_len @@ -1225,11 +1211,7 @@ static linenr_T setManualFoldWin(win_T *wp, linenr_T lnum, bool opening, bool re // Change from level-dependent folding to manual. if (use_level || fp->fd_flags == FD_LEVEL) { use_level = true; - if (level >= wp->w_p_fdl) { - fp->fd_flags = FD_CLOSED; - } else { - fp->fd_flags = FD_OPEN; - } + fp->fd_flags = level >= wp->w_p_fdl ? FD_CLOSED : FD_OPEN; fp2 = (fold_T *)fp->fd_nested.ga_data; for (int j = 0; j < fp->fd_nested.ga_len; j++) { fp2[j].fd_flags = FD_LEVEL; @@ -1378,15 +1360,11 @@ static void foldMarkAdjustRecurse(win_T *wp, garray_T *gap, linenr_T line1, line return; } - linenr_T top; - // In Insert mode an inserted line at the top of a fold is considered part // of the fold, otherwise it isn't. - if ((State & MODE_INSERT) && amount == 1 && line2 == MAXLNUM) { - top = line1 + 1; - } else { - top = line1; - } + linenr_T top = ((State & MODE_INSERT) && amount == 1 && line2 == MAXLNUM) + ? line1 + 1 + : line1; // Find the fold containing or just below "line1". fold_T *fp; @@ -1480,9 +1458,7 @@ static int getDeepestNestingRecurse(garray_T *gap) fold_T *fp = (fold_T *)gap->ga_data; for (int i = 0; i < gap->ga_len; i++) { int level = getDeepestNestingRecurse(&fp[i].fd_nested) + 1; - if (level > maxlevel) { - maxlevel = level; - } + maxlevel = MAX(maxlevel, level); } return maxlevel; @@ -1598,7 +1574,6 @@ static void foldCreateMarkers(win_T *wp, pos_T start, pos_T end) static void foldAddMarker(buf_T *buf, pos_T pos, const char *marker, size_t markerlen) { char *cms = buf->b_p_cms; - char *newline; char *p = strstr(buf->b_p_cms, "%s"); bool line_is_comment = false; linenr_T lnum = pos.lnum; @@ -1614,7 +1589,7 @@ static void foldAddMarker(buf_T *buf, pos_T pos, const char *marker, size_t mark // Check if the line ends with an unclosed comment skip_comment(line, false, false, &line_is_comment); - newline = xmalloc(line_len + markerlen + strlen(cms) + 1); + char *newline = xmalloc(line_len + markerlen + strlen(cms) + 1); STRCPY(newline, line); // Append the marker to the end of the line if (p == NULL || line_is_comment) { @@ -1737,10 +1712,7 @@ char *get_foldtext(win_T *wp, linenr_T lnum, linenr_T lnume, foldinfo_T foldinfo // Set "v:folddashes" to a string of "level" dashes. // Set "v:foldlevel" to "level". - int level = foldinfo.fi_level; - if (level > (int)sizeof(dashes) - 1) { - level = (int)sizeof(dashes) - 1; - } + int level = MIN(foldinfo.fi_level, (int)sizeof(dashes) - 1); memset(dashes, '-', (size_t)level); dashes[level] = NUL; set_vim_var_string(VV_FOLDDASHES, dashes, -1); @@ -1937,9 +1909,7 @@ static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot) // When deleting lines at the end of the buffer "top" can be past the end // of the buffer. - if (top > wp->w_buffer->b_ml.ml_line_count) { - top = wp->w_buffer->b_ml.ml_line_count; - } + top = MIN(top, wp->w_buffer->b_ml.ml_line_count); fline_T fline; @@ -2047,9 +2017,7 @@ static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot) if (fpn != NULL && current_fdl == fline.lvl) { linenr_T fold_end_lnum = fold_start_lnum + fpn->fd_len; - if (fold_end_lnum > bot) { - bot = fold_end_lnum; - } + bot = MAX(bot, fold_end_lnum); } } @@ -2127,9 +2095,7 @@ static void foldUpdateIEMS(win_T *const wp, linenr_T top, linenr_T bot) if (wp->w_redraw_top == 0 || wp->w_redraw_top > top) { wp->w_redraw_top = top; } - if (wp->w_redraw_bot < end) { - wp->w_redraw_bot = end; - } + wp->w_redraw_bot = MAX(wp->w_redraw_bot, end); } invalid_top = 0; @@ -2205,10 +2171,7 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level, // and after the first line of the fold, set the level to zero to // force the fold to end. Do the same when had_end is set: Previous // line was marked as end of a fold. - lvl = flp->lvl; - if (lvl > MAX_LEVEL) { - lvl = MAX_LEVEL; - } + lvl = MIN(flp->lvl, MAX_LEVEL); if (flp->lnum > firstlnum && (level > lvl - flp->start || level >= flp->had_end)) { lvl = 0; @@ -2263,12 +2226,7 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level, while (!got_int) { // set concat to 1 if it's allowed to concatenate this fold // with a previous one that touches it. - int concat; - if (flp->start != 0 || flp->had_end <= MAX_LEVEL) { - concat = 0; - } else { - concat = 1; - } + int concat = (flp->start != 0 || flp->had_end <= MAX_LEVEL) ? 0 : 1; // Find an existing fold to re-use. Preferably one that // includes startlnum, otherwise one that ends just before @@ -2424,9 +2382,7 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level, if (lvl > level && fp != NULL) { // There is a nested fold, handle it recursively. // At least do one line (can happen when finish is true). - if (bot < flp->lnum) { - bot = flp->lnum; - } + bot = MAX(bot, flp->lnum); // Line numbers in the nested fold are relative to the start of // this fold. @@ -2556,9 +2512,7 @@ static linenr_T foldUpdateIEMSRecurse(garray_T *const gap, const int level, // Need to redraw the lines we inspected, which might be further down than // was asked for. - if (bot < flp->lnum - 1) { - bot = flp->lnum - 1; - } + bot = MAX(bot, flp->lnum - 1); return bot; } @@ -2898,17 +2852,11 @@ static void foldlevelIndent(fline_T *flp) // depends on surrounding lines if (*s == NUL || vim_strchr(flp->wp->w_p_fdi, (uint8_t)(*s)) != NULL) { // first and last line can't be undefined, use level 0 - if (lnum == 1 || lnum == buf->b_ml.ml_line_count) { - flp->lvl = 0; - } else { - flp->lvl = -1; - } + flp->lvl = (lnum == 1 || lnum == buf->b_ml.ml_line_count) ? 0 : -1; } else { flp->lvl = get_indent_buf(buf, lnum) / get_sw_value(buf); } - if (flp->lvl > flp->wp->w_p_fdn) { - flp->lvl = (int)MAX(0, flp->wp->w_p_fdn); - } + flp->lvl = MIN(flp->lvl, (int)MAX(0, flp->wp->w_p_fdn)); } // foldlevelDiff() {{{2 @@ -2916,11 +2864,7 @@ static void foldlevelIndent(fline_T *flp) /// Doesn't use any caching. static void foldlevelDiff(fline_T *flp) { - if (diff_infold(flp->wp, flp->lnum + flp->off)) { - flp->lvl = 1; - } else { - flp->lvl = 0; - } + flp->lvl = (diff_infold(flp->wp, flp->lnum + flp->off)) ? 1 : 0; } // foldlevelExpr() {{{2 @@ -3067,11 +3011,7 @@ static void foldlevelMarker(fline_T *flp) if (n > 0) { flp->lvl = n; flp->lvl_next = n; - if (n <= start_lvl) { - flp->start = 1; - } else { - flp->start = n - start_lvl; - } + flp->start = MAX(n - start_lvl, 1); } } else { flp->lvl++; @@ -3088,9 +3028,7 @@ static void foldlevelMarker(fline_T *flp) flp->lvl = n; flp->lvl_next = n - 1; // never start a fold with an end marker - if (flp->lvl_next > start_lvl) { - flp->lvl_next = start_lvl; - } + flp->lvl_next = MIN(flp->lvl_next, start_lvl); } } else { flp->lvl_next--; @@ -3101,9 +3039,7 @@ static void foldlevelMarker(fline_T *flp) } // The level can't go negative, must be missing a start marker. - if (flp->lvl_next < 0) { - flp->lvl_next = 0; - } + flp->lvl_next = MAX(flp->lvl_next, 0); } // foldlevelSyntax() {{{2 @@ -3244,11 +3180,7 @@ static void foldclosed_both(typval_T *argvars, typval_T *rettv, bool end) linenr_T first; linenr_T last; if (hasFoldingWin(curwin, lnum, &first, &last, false, NULL)) { - if (end) { - rettv->vval.v_number = (varnumber_T)last; - } else { - rettv->vval.v_number = (varnumber_T)first; - } + rettv->vval.v_number = (varnumber_T)(end ? last : first); return; } } @@ -3336,9 +3268,7 @@ void f_foldtextresult(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) entered = true; linenr_T lnum = tv_get_lnum(argvars); // Treat illegal types and illegal string values for {lnum} the same. - if (lnum < 0) { - lnum = 0; - } + lnum = MAX(lnum, 0); foldinfo_T info = fold_info(curwin, lnum); if (info.fi_lines > 0) { diff --git a/src/nvim/garray.c b/src/nvim/garray.c index f87a196361..4f8ba30522 100644 --- a/src/nvim/garray.c +++ b/src/nvim/garray.c @@ -78,16 +78,12 @@ void ga_grow(garray_T *gap, int n) } // the garray grows by at least growsize - if (n < gap->ga_growsize) { - n = gap->ga_growsize; - } + n = MAX(n, gap->ga_growsize); // A linear growth is very inefficient when the array grows big. This // is a compromise between allocating memory that won't be used and too // many copy operations. A factor of 1.5 seems reasonable. - if (n < gap->ga_len / 2) { - n = gap->ga_len / 2; - } + n = MAX(n, gap->ga_len / 2); int new_maxlen = gap->ga_len + n; diff --git a/src/nvim/getchar.c b/src/nvim/getchar.c index 6e441757dc..5c8b3ebe46 100644 --- a/src/nvim/getchar.c +++ b/src/nvim/getchar.c @@ -268,17 +268,12 @@ static void add_buff(buffheader_T *const buf, const char *const s, ptrdiff_t sle } buf->bh_index = 0; - size_t len; if (buf->bh_space >= (size_t)slen) { - len = strlen(buf->bh_curr->b_str); + size_t len = strlen(buf->bh_curr->b_str); xmemcpyz(buf->bh_curr->b_str + len, s, (size_t)slen); buf->bh_space -= (size_t)slen; } else { - if (slen < MINIMAL_SIZE) { - len = MINIMAL_SIZE; - } else { - len = (size_t)slen; - } + size_t len = MAX(MINIMAL_SIZE, (size_t)slen); buffblock_T *p = xmalloc(offsetof(buffblock_T, b_str) + len + 1); buf->bh_space = len - (size_t)slen; xmemcpyz(p->b_str, s, (size_t)slen); @@ -2238,9 +2233,7 @@ static int handle_mapping(int *keylenp, const bool *timedout, int *mapdepth) } } else { // No match; may have to check for termcode at next character. - if (max_mlen < mlen) { - max_mlen = mlen; - } + max_mlen = MAX(max_mlen, mlen); } } } @@ -2372,9 +2365,7 @@ static int handle_mapping(int *keylenp, const bool *timedout, int *mapdepth) if (State & MODE_CMDLINE) { // redraw the command below the error msg_didout = true; - if (msg_row < cmdline_row) { - msg_row = cmdline_row; - } + msg_row = MAX(msg_row, cmdline_row); redrawcmd(); } } else if (State & (MODE_NORMAL | MODE_INSERT)) { @@ -2738,11 +2729,7 @@ static int vgetorpeek(bool advance) // For the cmdline window: Alternate between ESC and // CTRL-C: ESC for most situations and CTRL-C to close the // cmdline window. - if ((State & MODE_CMDLINE) || (cmdwin_type > 0 && tc == ESC)) { - c = Ctrl_C; - } else { - c = ESC; - } + c = ((State & MODE_CMDLINE) || (cmdwin_type > 0 && tc == ESC)) ? Ctrl_C : ESC; tc = c; // set a flag to indicate this wasn't a normal char diff --git a/src/nvim/hashtab.c b/src/nvim/hashtab.c index a8a998a361..95df5e5383 100644 --- a/src/nvim/hashtab.c +++ b/src/nvim/hashtab.c @@ -316,10 +316,7 @@ static void hash_may_resize(hashtab_T *ht, size_t minitems) } } else { // Use specified size. - if (minitems < ht->ht_used) { - // just in case... - minitems = ht->ht_used; - } + minitems = MAX(minitems, ht->ht_used); // array is up to 2/3 full minsize = minitems * 3 / 2; } diff --git a/src/nvim/memline.c b/src/nvim/memline.c index 8af80fce5d..eac62aeda2 100644 --- a/src/nvim/memline.c +++ b/src/nvim/memline.c @@ -905,12 +905,9 @@ void ml_recover(bool checkext) msg_end(); goto theend; } - off_T size; - if ((size = vim_lseek(mfp->mf_fd, 0, SEEK_END)) <= 0) { - mfp->mf_blocknr_max = 0; // no file or empty file - } else { - mfp->mf_blocknr_max = size / mfp->mf_page_size; - } + off_T size = vim_lseek(mfp->mf_fd, 0, SEEK_END); + // 0 means no file or empty file + mfp->mf_blocknr_max = size <= 0 ? 0 : size /