diff options
author | Lewis Russell <lewis6991@gmail.com> | 2024-07-19 11:00:13 +0100 |
---|---|---|
committer | Lewis Russell <me@lewisr.dev> | 2024-07-30 22:43:29 +0100 |
commit | d1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0 (patch) | |
tree | ca5a4a60fca9e814d0a3937cc4ff21ed9e037520 | |
parent | 1b5a394ffd4bb638ec9cbbb5e4d12d11225389cf (diff) | |
download | rneovim-d1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0.tar.gz rneovim-d1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0.tar.bz2 rneovim-d1bd3d643e5846eee7343ba1a12bdcbbc8cee7b0.zip |
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;
```
36 files changed, 419 insertions, 1214 deletions
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 / mfp->mf_page_size; mfp->mf_infile_count = mfp->mf_blocknr_max; // need to reallocate the memory used to store the data @@ -1898,9 +1895,7 @@ errorret: buf->b_ml.ml_line_lnum = lnum; return questions; } - if (lnum <= 0) { // pretend line 0 is line 1 - lnum = 1; - } + lnum = MAX(lnum, 1); // pretend line 0 is line 1 if (buf->b_ml.ml_mfp == NULL) { // there are no lines buf->b_ml.ml_line_len = 1; @@ -2111,12 +2106,8 @@ static int ml_append_int(buf_T *buf, linenr_T lnum, char *line, colnr_T len, boo if (line_count > db_idx + 1) { // if there are following lines // Offset is the start of the previous line. // This will become the character just after the new line. - int offset; - if (db_idx < 0) { - offset = (int)dp->db_txt_end; - } else { - offset = ((dp->db_index[db_idx]) & DB_INDEX_MASK); - } + int offset = db_idx < 0 ? (int)dp->db_txt_end + : (int)((dp->db_index[db_idx]) & DB_INDEX_MASK); memmove((char *)dp + dp->db_txt_start, (char *)dp + dp->db_txt_start + len, (size_t)offset - (dp->db_txt_start + (size_t)len)); diff --git a/src/nvim/message.c b/src/nvim/message.c index 73712489ad..53e5511a5a 100644 --- a/src/nvim/message.c +++ b/src/nvim/message.c @@ -1384,11 +1384,7 @@ void msgmore(int n) return; } - if (n > 0) { - pn = n; - } else { - pn = -n; - } + pn = abs(n); if (pn > p_report) { if (n > 0) { @@ -1426,9 +1422,7 @@ void msg_start(void) { bool did_return = false; - if (msg_row < cmdline_row) { - msg_row = cmdline_row; - } + msg_row = MAX(msg_row, cmdline_row); if (!msg_silent) { XFREE_CLEAR(keep_msg); // don't display old message now @@ -3382,9 +3376,7 @@ void msg_advance(int col) } return; } - if (col >= Columns) { // not enough room - col = Columns - 1; - } + col = MIN(col, Columns - 1); // not enough room while (msg_col < col) { msg_putchar(' '); } diff --git a/src/nvim/mouse.c b/src/nvim/mouse.c index 6bd40003ea..5e2f2f0320 100644 --- a/src/nvim/mouse.c +++ b/src/nvim/mouse.c @@ -1050,9 +1050,7 @@ void do_mousescroll(cmdarg_T *cap) // Horizontal scrolling int step = shift_or_ctrl ? curwin->w_width_inner : (int)p_mousescroll_hor; colnr_T leftcol = curwin->w_leftcol + (cap->arg == MSCR_RIGHT ? -step : +step); - if (leftcol < 0) { - leftcol = 0; - } + leftcol = MAX(leftcol, 0); do_mousescroll_horiz(leftcol); } } @@ -1619,11 +1617,8 @@ bool mouse_comp_pos(win_T *win, int *rowp, int *colp, linenr_T *lnump) while (row > 0) { // Don't include filler lines in "count" if (win_may_fill(win)) { - if (lnum == win->w_topline) { - row -= win->w_topfill; - } else { - row -= win_get_fill(win, lnum); - } + row -= lnum == win->w_topline ? win->w_topfill + : win_get_fill(win, lnum); count = plines_win_nofill(win, lnum, false); } else { count = plines_win(win, lnum, false); @@ -1664,9 +1659,7 @@ bool mouse_comp_pos(win_T *win, int *rowp, int *colp, linenr_T *lnump) if (!retval) { // Compute the column without wrapping. int off = win_col_off(win) - win_col_off2(win); - if (col < off) { - col = off; - } + col = MAX(col, off); col += row * (win->w_width_inner - off); // Add skip column for the topline. @@ -1681,9 +1674,7 @@ bool mouse_comp_pos(win_T *win, int *rowp, int *colp, linenr_T *lnump) // skip line number and fold column in front of the line col -= win_col_off(win); - if (col <= 0) { - col = 0; - } + col = MAX(col, 0); *colp = col; *rowp = row; diff --git a/src/nvim/move.c b/src/nvim/move.c index 8c206b3e8d..6324466dcc 100644 --- a/src/nvim/move.c +++ b/src/nvim/move.c @@ -894,9 +894,7 @@ void curs_columns(win_T *wp, int may_scroll) new_leftcol = wp->w_leftcol + diff; } } - if (new_leftcol < 0) { - new_leftcol = 0; - } + new_leftcol = MAX(new_leftcol, 0); if (new_leftcol != (int)wp->w_leftcol) { wp->w_leftcol = new_leftcol; win_check_anchored_floats(wp); @@ -969,11 +967,8 @@ void curs_columns(win_T *wp, int may_scroll) if (n > plines - wp->w_height_inner + 1) { n = plines - wp->w_height_inner + 1; } - if (n > 0) { - wp->w_skipcol = width1 + (n - 1) * width2; - } else { - wp->w_skipcol = 0; - } + wp->w_skipcol = n > 0 ? width1 + (n - 1) * width2 + : 0; } else if (extra == 1) { // less than 'scrolloff' lines above, decrease skipcol assert(so <= INT_MAX); @@ -990,9 +985,7 @@ void curs_columns(win_T *wp, int may_scroll) while (endcol > wp->w_virtcol) { endcol -= width2; } - if (endcol > wp->w_skipcol) { - wp->w_skipcol = endcol; - } + wp->w_skipcol = MAX(wp->w_skipcol, endcol); } // adjust w_wrow for the changed w_skipcol @@ -1129,9 +1122,7 @@ void f_screenpos(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) semsg(_(e_invalid_line_number_nr), pos.lnum); return; } - if (pos.col < 0) { - pos.col = 0; - } + pos.col = MAX(pos.col, 0); int row = 0; int scol = 0; int ccol = 0; @@ -1423,9 +1414,7 @@ bool scrolldown(win_T *wp, linenr_T line_count, int byfold) foldAdjustCursor(wp); coladvance(wp, wp->w_curswant); } - if (wp->w_cursor.lnum < wp->w_topline) { - wp->w_cursor.lnum = wp->w_topline; - } + wp->w_cursor.lnum = MAX(wp->w_cursor.lnum, wp->w_topline); return moved; } @@ -1506,12 +1495,8 @@ bool scrollup(win_T *wp, linenr_T line_count, bool byfold) wp->w_botline += line_count; // approximate w_botline } - if (wp->w_topline > wp->w_buffer->b_ml.ml_line_count) { - wp->w_topline = wp->w_buffer->b_ml.ml_line_count; - } - if (wp->w_botline > wp->w_buffer->b_ml.ml_line_count + 1) { - wp->w_botline = wp->w_buffer->b_ml.ml_line_count + 1; - } + wp->w_topline = MIN(wp->w_topline, wp->w_buffer->b_ml.ml_line_count); + wp->w_botline = MIN(wp->w_botline, wp->w_buffer->b_ml.ml_line_count + 1); check_topfill(wp, false); @@ -1623,9 +1608,7 @@ void check_topfill(win_T *wp, bool down) wp->w_topfill = 0; } else { wp->w_topfill = wp->w_height_inner - n; - if (wp->w_topfill < 0) { - wp->w_topfill = 0; - } + wp->w_topfill = MAX(wp->w_topfill, 0); } } } @@ -1849,15 +1832,11 @@ void scroll_cursor_top(win_T *wp, int min_scroll, int always) if (new_topline < wp->w_topline || always) { wp->w_topline = new_topline; } - if (wp->w_topline > wp->w_cursor.lnum) { - wp->w_topline = wp->w_cursor.lnum; - } + wp->w_topline = MIN(wp->w_topline, wp->w_cursor.lnum); wp->w_topfill = win_get_fill(wp, wp->w_topline); if (wp->w_topfill > 0 && extra > off) { wp->w_topfill -= extra - off; - if (wp->w_topfill < 0) { - wp->w_topfill = 0; - } + wp->w_topfill = MAX(wp->w_topfill, 0); } check_topfill(wp, false); if (wp->w_topline != old_topline) { @@ -2276,18 +2255,14 @@ void cursor_correct(win_T *wp) if (wp->w_topline == 1) { above_wanted = 0; int max_off = wp->w_height_inner / 2; - if (below_wanted > max_off) { - below_wanted = max_off; - } + below_wanted = MIN(below_wanted, max_off); } validate_botline(wp); if (wp->w_botline == wp->w_buffer->b_ml.ml_line_count + 1 && mouse_dragging == 0) { below_wanted = 0; int max_off = (wp->w_height_inner - 1) / 2; - if (above_wanted > max_off) { - above_wanted = max_off; - } + above_wanted = MIN(above_wanted, max_off); } // If there are sufficient file-lines above and below the cursor, we can diff --git a/src/nvim/normal.c b/src/nvim/normal.c index ee67748af9..88b8ccbb85 100644 --- a/src/nvim/normal.c +++ b/src/nvim/normal.c @@ -1685,9 +1685,7 @@ size_t find_ident_at_pos(win_T *wp, linenr_T lnum, colnr_T startcol, char **text // If we don't want just any old text, or we've found an // identifier, stop searching. - if (this_class > 2) { - this_class = 2; - } + this_class = MIN(this_class, 2); if (!(find_type & FIND_STRING) || this_class == 2) { break; } @@ -2011,9 +2009,7 @@ static void del_from_showcmd(int len) } int old_len = (int)strlen(showcmd_buf); - if (len > old_len) { - len = old_len; - } + len = MIN(len, old_len); showcmd_buf[old_len - len] = NUL; if (!char_avail()) { @@ -2515,9 +2511,7 @@ bool nv_screengo(oparg_T *oap, int dir, int dist) } else { n = width1; } - if (curwin->w_curswant >= n) { - curwin->w_curswant = n - 1; - } + curwin->w_curswant = MIN(curwin->w_curswant, n - 1); } while (dist--) { @@ -2776,11 +2770,7 @@ static void nv_zet(cmdarg_T *cap) if (cap->count0 == 0) { // No count given: put cursor at the line below screen validate_botline(curwin); // make sure w_botline is valid - if (curwin->w_botline > curbuf->b_ml.ml_line_count) { - curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; - } else { - curwin->w_cursor.lnum = curwin->w_botline; - } + curwin->w_cursor.lnum = MIN(curwin->w_botline, curbuf->b_ml.ml_line_count); } FALLTHROUGH; case NL: @@ -3049,9 +3039,7 @@ static void nv_zet(cmdarg_T *cap) case 'm': if (curwin->w_p_fdl > 0) { curwin->w_p_fdl -= cap->count1; - if (curwin->w_p_fdl < 0) { - curwin->w_p_fdl = 0; - } + curwin->w_p_fdl = MAX(curwin->w_p_fdl, 0); } old_fdl = -1; // force an update curwin->w_p_fen = true; @@ -3069,9 +3057,7 @@ static void nv_zet(cmdarg_T *cap) curwin->w_p_fdl += cap->count1; { int d = getDeepestNesting(curwin); - if (curwin->w_p_fdl >= d) { - curwin->w_p_fdl = d; - } + curwin->w_p_fdl = MIN(curwin->w_p_fdl, d); } break; @@ -3662,10 +3648,7 @@ static void nv_scroll(cmdarg_T *cap) n = lnum - curwin->w_topline; } } - curwin->w_cursor.lnum = curwin->w_topline + n; - if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) { - curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; - } + curwin->w_cursor.lnum = MIN(curwin->w_topline + n, curbuf->b_ml.ml_line_count); } // Correct for 'so', except when an operator is pending. @@ -4344,12 +4327,8 @@ static void nv_percent(cmdarg_T *cap) curwin->w_cursor.lnum = (curbuf->b_ml.ml_line_count * cap->count0 + 99) / 100; } - if (curwin->w_cursor.lnum < 1) { - curwin->w_cursor.lnum = 1; - } - if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) { - curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; - } + curwin->w_cursor.lnum = MIN(MAX(curwin->w_cursor.lnum, 1), curbuf->b_ml.ml_line_count); + beginline(BL_SOL | BL_FIX); } } else { // "%" : go to matching paren @@ -6088,11 +6067,7 @@ static void nv_goto(cmdarg_T *cap) if (cap->count0 != 0) { lnum = cap->count0; } - if (lnum < 1) { - lnum = 1; - } else if (lnum > curbuf->b_ml.ml_line_count) { - lnum = curbuf->b_ml.ml_line_count; - } + lnum = MIN(MAX(lnum, 1), curbuf->b_ml.ml_line_count); curwin->w_cursor.lnum = lnum; beginline(BL_SOL | BL_FIX); if ((fdo_flags & FDO_JUMP) && KeyTyped && cap->oap->op_type == OP_NOP) { @@ -6422,9 +6397,8 @@ static void nv_join(cmdarg_T *cap) return; } - if (cap->count0 <= 1) { - cap->count0 = 2; // default for join is two lines! - } + cap->count0 = MAX(cap->count0, 2); // default for join is two lines! + if (curwin->w_cursor.lnum + cap->count0 - 1 > curbuf->b_ml.ml_line_count) { // can't join when on the last line diff --git a/src/nvim/ops.c b/src/nvim/ops.c index f2f8708235..a71317d75d 100644 --- a/src/nvim/ops.c +++ b/src/nvim/ops.c @@ -246,12 +246,7 @@ void op_shift(oparg_T *oap, bool curs_top, int amount) foldOpenCursor(); if (oap->line_count > p_report) { - char *op; - if (oap->op_type == OP_RSHIFT) { - op = ">"; - } else { - op = "<"; - } + char *op = oap->op_type == OP_RSHIFT ? ">" : "<"; char *msg_line_single = NGETTEXT("%" PRId64 " line %sed %d time", "%" PRId64 " line %sed %d times", amount); @@ -295,20 +290,14 @@ void shift_line(bool left, bool round, int amount, int call_changed_bytes) amount--; } if (left) { - i -= amount; - if (i < 0) { - i = 0; - } + i = MAX(i - amount, 0); } else { i += amount; } count = i * sw_val; } else { // original vi indent if (left) { - count -= sw_val * amount; - if (count < 0) { - count = 0; - } + count = MAX(count - sw_val * amount, 0); } else { count += sw_val * amount; } @@ -445,9 +434,7 @@ static void shift_block(oparg_T *oap, int amount) const colnr_T block_space_width = non_white_col - oap->start_vcol; // We will shift by "total" or "block_space_width", whichever is less. - const colnr_T shift_amount = block_space_width < total - ? block_space_width - : total; + const colnr_T shift_amount = MIN(block_space_width, total); // The column to which we will shift the text. const colnr_T destination_col = non_white_col - shift_amount; @@ -561,9 +548,7 @@ static void block_insert(oparg_T *oap, const char *s, size_t slen, bool b_insert // avoid copying part of a multi-byte character offset -= utf_head_off(oldp, oldp + offset); } - if (spaces < 0) { // can happen when the cursor was moved - spaces = 0; - } + spaces = MAX(spaces, 0); // can happen when the cursor was moved assert(count >= 0); // Make sure the allocated size matches what is actually copied below. @@ -2369,9 +2354,7 @@ void op_insert(oparg_T *oap, int count1) } } } - if (add > len) { - add = len; // short line, point to the NUL - } + add = MIN(add, len); // short line, point to the NUL firstline += add; len -= add; int ins_len = len - pre_textlen - offset; @@ -3041,9 +3024,7 @@ void do_put(int regname, yankreg_T *reg, int dir, int count, int flags) if (y_type == kMTBlockWise) { lnum = curwin->w_cursor.lnum + (linenr_T)y_size + 1; - if (lnum > curbuf->b_ml.ml_line_count) { - lnum = curbuf->b_ml.ml_line_count + 1; - } + lnum = MIN(lnum, curbuf->b_ml.ml_line_count + 1); if (u_save(curwin->w_cursor.lnum - 1, lnum) == FAIL) { goto end; } @@ -3204,9 +3185,7 @@ void do_put(int regname, yankreg_T *reg, int dir, int count, int flags) spaces -= win_charsize(cstype, 0, ci.ptr, ci.chr.value, &csarg).width; ci = utfc_next(ci); } - if (spaces < 0) { - spaces = 0; - } + spaces = MAX(spaces, 0); } // Insert the new text. @@ -3271,10 +3250,7 @@ void do_put(int regname, yankreg_T *reg, int dir, int count, int flags) // adjust '] mark curbuf->b_op_end.lnum = curwin->w_cursor.lnum - 1; - curbuf->b_op_end.col = bd.textcol + (colnr_T)totlen - 1; - if (curbuf->b_op_end.col < 0) { - curbuf->b_op_end.col = 0; - } + curbuf->b_op_end.col = MAX(bd.textcol + (colnr_T)totlen - 1, 0); curbuf->b_op_end.coladd = 0; if (flags & PUT_CURSEND) { curwin->w_cursor = curbuf->b_op_end; @@ -3282,9 +3258,7 @@ void do_put(int regname, yankreg_T *reg, int dir, int count, int flags) // in Insert mode we might be after the NUL, correct for that colnr_T len = get_cursor_line_len(); - if (curwin->w_cursor.col > len) { - curwin->w_cursor.col = len; - } + curwin->w_cursor.col = MIN(curwin->w_cursor.col, len); } else { curwin->w_cursor.lnum = lnum; } @@ -3317,10 +3291,7 @@ void do_put(int regname, yankreg_T *reg, int dir, int count, int flags) int first_byte_off = 0; if (VIsual_active) { - end_lnum = curbuf->b_visual.vi_end.lnum; - if (end_lnum < curbuf->b_visual.vi_start.lnum) { - end_lnum = curbuf->b_visual.vi_start.lnum; - } + end_lnum = MAX(curbuf->b_visual.vi_end.lnum, curbuf->b_visual.vi_start.lnum); if (end_lnum > start_lnum) { // "col" is valid for the first line, in following lines // the virtual column needs to be used. Matters for @@ -4259,10 +4230,7 @@ void charwise_block_prep(pos_T start, pos_T end, struct block_def *bdp, linenr_T if (ce != cs && start.coladd > 0) { // Part of a tab selected -- but don't double-count it. bdp->start_char_vcols = ce - cs + 1; - bdp->startspaces = bdp->start_char_vcols - start.coladd; - if (bdp->startspaces < 0) { - bdp->startspaces = 0; - } + bdp->startspaces = MAX(bdp->start_char_vcols - start.coladd, 0); startcol++; } } @@ -4362,9 +4330,7 @@ void op_addsub(oparg_T *oap, linenr_T Prenum1, bool g_cmd) } if (pos.lnum == oap->end.lnum) { length = ml_get_len(oap->end.lnum); - if (oap->end.col >= length) { - oap->end.col = length - 1; - } + oap->end.col = MIN(oap->end.col, length - 1); length = oap->end.col - pos.col + 1; } } @@ -4553,21 +4519,13 @@ bool do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1) // decrement or increment alphabetic character if (op_type == OP_NR_SUB) { if (CHAR_ORD(firstdigit) < Prenum1) { - if (isupper(firstdigit)) { - firstdigit = 'A'; - } else { - firstdigit = 'a'; - } + firstdigit = isupper(firstdigit) ? 'A' : 'a'; } else { firstdigit -= (int)Prenum1; } } else { if (26 - CHAR_ORD(firstdigit) - 1 < Prenum1) { - if (isupper(firstdigit)) { - firstdigit = 'Z'; - } else { - firstdigit = 'z'; - } + firstdigit = isupper(firstdigit) ? 'Z' : 'z'; } else { firstdigit += (int)Prenum1; } @@ -4678,11 +4636,7 @@ bool do_addsub(int op_type, pos_T *pos, int length, linenr_T Prenum1) } while (todel-- > 0) { if (c < 0x100 && isalpha(c)) { - if (isupper(c)) { - hexupper = true; - } else { - hexupper = false; - } + hexupper = isupper(c); } // del_char() will mark line needing displaying del_char(false); @@ -5158,9 +5112,7 @@ static void str_to_reg(yankreg_T *y_ptr, MotionType yank_type, const char *str, for (char **ss = (char **)str; *ss != NULL; ss++, lnum++) { size_t ss_len = strlen(*ss); pp[lnum] = xmemdupz(*ss, ss_len); - if (ss_len > maxlen) { - maxlen = ss_len; - } + maxlen = MAX(maxlen, ss_len); } } else { size_t line_len; @@ -5169,9 +5121,7 @@ static void str_to_reg(yankreg_T *y_ptr, MotionType yank_type, const char *str, start += line_len + 1, lnum++) { assert(end - start >= 0); line_len = (size_t)((char *)xmemscan(start, '\n', (size_t)(end - start)) - start); - if (line_len > maxlen) { - maxlen = line_len; - } + maxlen = MAX(maxlen, line_len); // When appending, copy the previous line and free it after. size_t extra = append ? strlen(pp[--lnum]) : 0; @@ -5661,9 +5611,7 @@ static void get_op_vcol(oparg_T *oap, colnr_T redo_VIsual_vcol, bool initial) if (!redo_VIsual_busy) { getvvcol(curwin, &(oap->end), &start, NULL, &end); - if (start < oap->start_vcol) { - oap->start_vcol = start; - } + oap->start_vcol = MIN(oap->start_vcol, start); if (end > oap->end_vcol) { if (initial && *p_sel == 'e' && start >= 1 @@ -5682,9 +5630,7 @@ static void get_op_vcol(oparg_T *oap, colnr_T redo_VIsual_vcol, bool initial) for (curwin->w_cursor.lnum = oap->start.lnum; curwin->w_cursor.lnum <= oap->end.lnum; curwin->w_cursor.lnum++) { getvvcol(curwin, &curwin->w_cursor, NULL, NULL, &end); - if (end > oap->end_vcol) { - oap->end_vcol = end; - } + oap->end_vcol = MAX(oap->end_vcol, end); } } else if (redo_VIsual_busy) { oap->end_vcol = oap->start_vcol + redo_VIsual_vcol - 1; @@ -5818,9 +5764,7 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) // redo_VIsual.rv_line_count and redo_VIsual.rv_vcol. oap->start = curwin->w_cursor; curwin->w_cursor.lnum += redo_VIsual.rv_line_count - 1; - if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count) { - curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; - } + curwin->w_cursor.lnum = MIN(curwin->w_cursor.lnum, curbuf->b_ml.ml_line_count); VIsual_mode = redo_VIsual.rv_mode; if (redo_VIsual.rv_vcol == MAXCOL || VIsual_mode == 'v') { if (VIsual_mode == 'v') { @@ -6108,9 +6052,7 @@ void do_pending_operator(cmdarg_T *cap, int old_col, bool gui_yank) case OP_JOIN_NS: case OP_JOIN: - if (oap->line_count < 2) { - oap->line_count = 2; - } + oap->line_count = MAX(oap->line_count, 2); if (curwin->w_cursor.lnum + oap->line_count - 1 > curbuf->b_ml.ml_line_count) { beep_flush(); @@ -6509,9 +6451,7 @@ void finish_yankreg_from_object(yankreg_T *reg, bool clipboard_adjust) size_t maxlen = 0; for (size_t i = 0; i < reg->y_size; i++) { size_t rowlen = strlen(reg->y_array[i]); - if (rowlen > maxlen) { - maxlen = rowlen; - } + maxlen = MAX(maxlen, rowlen); } assert(maxlen <= INT_MAX); reg->y_width = MAX(reg->y_width, (int)maxlen - 1); @@ -6615,9 +6555,7 @@ static bool get_clipboard(int name, yankreg_T **target, bool quiet) size_t maxlen = 0; for (size_t i = 0; i < reg->y_size; i++) { size_t rowlen = strlen(reg->y_array[i]); - if (rowlen > maxlen) { - maxlen = rowlen; - } + maxlen = MAX(maxlen, rowlen); } assert(maxlen <= INT_MAX); reg->y_width = (int)maxlen - 1; diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index 21662264bc..a549e12adc 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -382,11 +382,7 @@ static int qf_init_process_nextline(qf_list_T *qfl, efm_T *fmt_first, qfstate_T int qf_init(win_T *wp, const char *restrict efile, char *restrict errorformat, int newlist, const char *restrict qf_title, char *restrict enc) { - qf_info_T *qi = &ql_info; - - if (wp != NULL) { - qi = ll_get_or_alloc_list(wp); - } + qf_info_T *qi = wp == NULL ? &ql_info : ll_get_or_alloc_list(wp); return qf_init_ext(qi, qi->qf_curlist, efile, curbuf, NULL, errorformat, newlist, 0, 0, qf_title, enc); @@ -834,8 +830,7 @@ retry: break; } - state->growbufsiz = (2 * state->growbufsiz < LINE_MAXLEN) - ? 2 * state->growbufsiz : LINE_MAXLEN; + state->growbufsiz = MIN(2 * state->growbufsiz, LINE_MAXLEN); state->growbuf = xrealloc(state->growbuf, state->growbufsiz); } @@ -871,8 +866,7 @@ retry: xfree(state->growbuf); state->linebuf = line; state->growbuf = line; - state->growbufsiz = state->linelen < LINE_MAXLEN - ? state->linelen : LINE_MAXLEN; + state->growbufsiz = MIN(state->linelen, LINE_MAXLEN); } } } @@ -1152,14 +1146,10 @@ static int qf_init_ext(qf_info_T *qi, int qf_idx, const char *restrict efile, bu } } - char *efm; - // Use the local value of 'errorformat' if it's set. - if (errorformat == p_efm && tv == NULL && buf && *buf->b_p_efm != NUL) { - efm = buf->b_p_efm; - } else { - efm = errorformat; - } + char *efm = (errorformat == p_efm && tv == NULL && buf && *buf->b_p_efm != NUL) + ? buf->b_p_efm + : errorformat; // If the errorformat didn't change between calls, then reuse the previously // parsed values. @@ -1491,9 +1481,7 @@ static int qf_parse_fmt_s(regmatch_T *rmp, int midx, qffields_T *fields) return QF_FAIL; } size_t len = (size_t)(rmp->endp[midx] - rmp->startp[midx]); - if (len > CMDBUFFSIZE - 5) { - len = CMDBUFFSIZE - 5; - } + len = MIN(len, CMDBUFFSIZE - 5); STRCPY(fields->pattern, "^\\V"); xstrlcat(fields->pattern, rmp->startp[midx], len + 4); fields->pattern[len + 3] = '\\'; @@ -1511,9 +1499,7 @@ static int qf_parse_fmt_o(regmatch_T *rmp, int midx, qffields_T *fields) } size_t len = (size_t)(rmp->endp[midx] - rmp->startp[midx]); size_t dsize = strlen(fields->module) + len + 1; - if (dsize > CMDBUFFSIZE) { - dsize = CMDBUFFSIZE; - } + dsize = MIN(dsize, CMDBUFFSIZE); xstrlcat(fields->module, rmp->startp[midx], dsize); return QF_OK; } @@ -2527,13 +2513,7 @@ static void win_set_loclist(win_T *wp, qf_info_T *qi) /// window. static int jump_to_help_window(qf_info_T *qi, bool newwin, bool *opened_window) { - win_T *wp = NULL; - - if (cmdmod.cmod_tab != 0 || newwin) { - wp = NULL; - } else { - wp = qf_find_help_win(); - } + win_T *wp = (cmdmod.cmod_tab != 0 || newwin) ? NULL : qf_find_help_win(); if (wp != NULL && wp->w_buffer->b_nwindows > 0) { win_enter(wp, true); @@ -2882,9 +2862,7 @@ static void qf_jump_goto_line(linenr_T qf_lnum, int qf_col, char qf_viscol, char // Go to line with error, unless qf_lnum is 0. linenr_T i = qf_lnum; if (i > 0) { - if (i > curbuf->b_ml.ml_line_count) { - i = curbuf->b_ml.ml_line_count; - } + i = MIN(i, curbuf->b_ml.ml_line_count); curwin->w_cursor.lnum = i; } if (qf_col > 0) { @@ -3898,13 +3876,8 @@ static bool qf_win_pos_update(qf_info_T *qi, int old_qf_index) if (win != NULL && qf_index <= win->w_buffer->b_ml.ml_line_count && old_qf_index != qf_index) { - if (qf_index > old_qf_index) { - win->w_redraw_top = old_qf_index; - win->w_redraw_bot = qf_index; - } else { - win->w_redraw_top = qf_index; - win->w_redraw_bot = old_qf_index; - } + win->w_redraw_top = MIN(old_qf_index, qf_index); + win->w_redraw_bot = MAX(old_qf_index, qf_index); qf_win_goto(win, qf_index); } return win != NULL; @@ -4220,11 +4193,7 @@ static void qf_fill_buffer(qf_list_T *qfl, buf_T *buf, qfline_T *old_last, int q qfp = qfl->qf_start; lnum = 0; } else { - if (old_last->qf_next != NULL) { - qfp = old_last->qf_next; - } else { - qfp = old_last; - } + qfp = old_last->qf_next != NULL ? old_last->qf_next : old_last; lnum = buf->b_ml.ml_line_count; } @@ -4451,15 +4420,11 @@ void ex_make(exarg_T *eap) incr_quickfix_busy(); - char *errorformat = p_efm; - bool newlist = true; + char *errorformat = (eap->cmdidx != CMD_make && eap->cmdidx != CMD_lmake) + ? p_gefm + : p_efm; - if (eap->cmdidx != CMD_make && eap->cmdidx != CMD_lmake) { - errorformat = p_gefm; - } - if (eap->cmdidx == CMD_grepadd || eap->cmdidx == CMD_lgrepadd) { - newlist = false; - } + bool newlist = eap->cmdidx != CMD_grepadd && eap->cmdidx != CMD_lgrepadd; int res = qf_init(wp, fname, errorformat, newlist, qf_cmdtitle(*eap->cmdlinep), enc); @@ -5323,9 +5288,7 @@ static bool vgr_match_buflines(qf_list_T *qfl, char *fname, buf_T *buf, char *sp { bool found_match = false; size_t pat_len = strlen(spat); - if (pat_len > MAX_FUZZY_MATCHES) { - pat_len = MAX_FUZZY_MATCHES; - } + pat_len = MIN(pat_len, MAX_FUZZY_MATCHES); for (linenr_T lnum = 1; lnum <= buf->b_ml.ml_line_count && *tomatch > 0; lnum++) { colnr_T col = 0; @@ -5467,12 +5430,7 @@ static int vgr_process_args(exarg_T *eap, vgr_args_T *args) args->regmatch.regprog = NULL; args->qf_title = xstrdup(qf_cmdtitle(*eap->cmdlinep)); - - if (eap->addr_count > 0) { - args->tomatch = eap->line2; - } else { - args->tomatch = MAXLNUM; - } + args->tomatch = eap->addr_count > 0 ? eap->line2 : MAXLNUM; // Get the search pattern: either white-separated or enclosed in // char *p = skip_vimgrep_pat(eap->arg, &args->spat, &args->flags); @@ -6702,9 +6660,7 @@ static int qf_setprop_curidx(qf_info_T *qi, qf_list_T *qfl, const dictitem_T *di if (newidx < 1) { // sanity check return FAIL; } - if (newidx > qfl->qf_count) { - newidx = qfl->qf_count; - } + newidx = MIN(newidx, qfl->qf_count); const int old_qfidx = qfl->qf_index; qfline_T *const qf_ptr = get_nth_entry(qfl, newidx, &newidx); if (qf_ptr == NULL) { @@ -6901,31 +6857,17 @@ static bool mark_quickfix_ctx(qf_info_T *qi, int copyID) /// "in use". So that garbage collection doesn't free the context. bool set_ref_in_quickfix(int copyID) { - bool abort = mark_quickfix_ctx(&ql_info, copyID); - if (abort) { - return abort; - } - - abort = mark_quickfix_user_data(&ql_info, copyID); - if (abort) { - return abort; - } - - abort = set_ref_in_callback(&qftf_cb, copyID, NULL, NULL); - if (abort) { - return abort; + if (mark_quickfix_ctx(&ql_info, copyID) + || mark_quickfix_user_data(&ql_info, copyID) + || set_ref_in_callback(&qftf_cb, copyID, NULL, NULL)) { + return true; } FOR_ALL_TAB_WINDOWS(tp, win) { if (win->w_llist != NULL) { - abort = mark_quickfix_ctx(win->w_llist, copyID); - if (abort) { - return abort; - } - - abort = mark_quickfix_user_data(win->w_llist, copyID); - if (abort) { - return abort; + if (mark_quickfix_ctx(win->w_llist, copyID) + || mark_quickfix_user_data(win->w_llist, copyID)) { + return true; } } @@ -6933,14 +6875,13 @@ bool set_ref_in_quickfix(int copyID) // In a location list window and none of the other windows is // referring to this location list. Mark the location list // context as still in use. - abort = mark_quickfix_ctx(win->w_llist_ref, copyID); - if (abort) { - return abort; + if (mark_quickfix_ctx(win->w_llist_ref, copyID)) { + return true; } } } - return abort; + return false; } /// Return the autocmd name for the :cbuffer Ex commands diff --git a/src/nvim/search.c b/src/nvim/search.c index 4d817b2418..57d151cf75 100644 --- a/src/nvim/search.c +++ b/src/nvim/search.c @@ -947,11 +947,9 @@ int searchit(win_T *win, buf_T *buf, pos_T *pos, pos_T *end_pos, Direction dir, // This message is also remembered in keep_msg for when the screen // is redrawn. The keep_msg is cleared whenever another message is // written. - if (dir == BACKWARD) { // start second loop at the other end - lnum = buf->b_ml.ml_line_count; - } else { - lnum = 1; - } + lnum = dir == BACKWARD // start second loop at the other end + ? buf->b_ml.ml_line_count + : 1; if (!shortmess(SHM_SEARCH) && shortmess(SHM_SEARCHCOUNT) && (options & SEARCH_MSG)) { @@ -1053,7 +1051,6 @@ static int first_submatch(regmmatch_T *rp) int do_search(oparg_T *oap, int dirc, int search_delim, char *pat, size_t patlen, int count, int options, searchit_arg_T *sia) { - pos_T pos; // position of the last match char *searchstr; size_t searchstrlen; int retval; // Return value @@ -1078,7 +1075,8 @@ int do_search(oparg_T *oap, int dirc, int search_delim, char *pat, size_t patlen // (there is no "if ()" around this because gcc wants them initialized) SearchOffset old_off = spats[0].off; - pos = curwin->w_cursor; // start searching at the cursor position + pos_T pos = curwin->w_cursor; // Position of the last match. + // Start searching at the cursor position. // Find out the direction of the search. if (dirc == 0) { @@ -1088,11 +1086,7 @@ int do_search(oparg_T *oap, int dirc, int search_delim, char *pat, size_t patlen set_vv_searchforward(); } if (options & SEARCH_REV) { - if (dirc == '/') { - dirc = '?'; - } else { - dirc = '/'; - } + dirc = dirc == '/' ? '?' : '/'; } // If the cursor is in a closed fold, don't find another match in the same @@ -1564,11 +1558,9 @@ int searchc(cmdarg_T *cap, bool t_cmd) if (*lastc == NUL && lastc_bytelen <= 1) { return FAIL; } - if (dir) { // repeat in opposite direction - dir = -lastcdir; - } else { - dir = lastcdir; - } + dir = dir // repeat in opposite direction + ? -lastcdir + : lastcdir; t_cmd = last_t_cmd; c = *lastc; // For multi-byte re-use last lastc_bytes[] and lastc_bytelen. @@ -1581,11 +1573,7 @@ int searchc(cmdarg_T *cap, bool t_cmd) } } - if (dir == BACKWARD) { - cap->oap->inclusive = false; - } else { - cap->oap->inclusive = true; - } + cap->oap->inclusive = dir != BACKWARD; char *p = get_cursor_line_ptr(); int col = curwin->w_cursor.col; @@ -2432,17 +2420,13 @@ int current_search(int count, bool forward) dec_cursor(); } - pos_T end_pos; // end position of the pattern match - pos_T orig_pos; // position of the cursor at beginning - pos_T pos; // position after the pattern - int result; // result of various function calls - // When searching forward and the cursor is at the start of the Visual // area, skip the first search backward, otherwise it doesn't move. const bool skip_first_backward = forward && VIsual_active && lt(curwin->w_cursor, VIsual); - orig_pos = pos = curwin->w_cursor; + pos_T pos = curwin->w_cursor; // position after the pattern + pos_T orig_pos = curwin->w_cursor; // position of the cursor at beginning if (VIsual_active) { // Searching further will extend the match. if (forward) { @@ -2459,6 +2443,9 @@ int current_search(int count, bool forward) return FAIL; // pattern not found } + pos_T end_pos; // end position of the pattern match + int result; // result of various function calls + // The trick is to first search backwards and then search forward again, // so that a match at the current cursor position will be correctly // captured. When "forward" is false do it the other way around. @@ -3238,9 +3225,8 @@ static int fuzzy_match_item_compare(const void *const s1, const void *const s2) if (v1 == v2) { return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1; - } else { - return v1 > v2 ? -1 : 1; } + return v1 > v2 ? -1 : 1; } /// Fuzzy search the string "str" in a list of "items" and return the matching @@ -3514,9 +3500,8 @@ static int fuzzy_match_func_compare(const void *const s1, const void *const s2) } if (v1 == v2) { return idx1 == idx2 ? 0 : idx1 > idx2 ? 1 : -1; - } else { - return v1 > v2 ? -1 : 1; } + return v1 > v2 ? -1 : 1; } /// Sort fuzzy matches of function names by score. @@ -3704,13 +3689,8 @@ void find_pattern_in_path(char *ptr, Direction dir, size_t len, bool whole, bool int old_files = max_path_depth; int depth = depth_displayed = -1; - linenr_T lnum = start_lnum; - if (end_lnum > curbuf->b_ml.ml_line_count) { - end_lnum = curbuf->b_ml.ml_line_count; - } - if (lnum > end_lnum) { // do at least one line - lnum = end_lnum; - } + end_lnum = MIN(end_lnum, curbuf->b_ml.ml_line_count); + linenr_T lnum = MIN(start_lnum, end_lnum); // do at least one line char *line = get_line_and_copy(lnum, file_line); while (true) { @@ -4159,9 +4139,7 @@ exit_matched: depth--; curr_fname = (depth == -1) ? curbuf->b_fname : files[depth].name; - if (depth < depth_displayed) { - depth_displayed = depth; - } + depth_displayed = MIN(depth_displayed, depth); } if (depth >= 0) { // we could read the line files[depth].lnum++; diff --git a/src/nvim/sha256.c b/src/nvim/sha256.c index 5e0aac3f69..f892d93236 100644 --- a/src/nvim/sha256.c +++ b/src/nvim/sha256.c @@ -223,18 +223,15 @@ static uint8_t sha256_padding[SHA256_BUFFER_SIZE] = { void sha256_finish(context_sha256_T *ctx, uint8_t digest[SHA256_SUM_SIZE]) { - uint32_t last, padn; - uint32_t high, low; - uint8_t msglen[8]; - - high = (ctx->total[0] >> 29) | (ctx->total[1] << 3); - low = (ctx->total[0] << 3); + uint32_t high = (ctx->total[0] >> 29) | (ctx->total[1] << 3); + uint32_t low = (ctx->total[0] << 3); + uint8_t msglen[8]; PUT_UINT32(high, msglen, 0); PUT_UINT32(low, msglen, 4); - last = ctx->total[0] & 0x3F; - padn = (last < 56) ? (56 - last) : (120 - last); + uint32_t last = ctx->total[0] & 0x3F; + uint32_t padn = (last < 56) ? (56 - last) : (120 - last); sha256_update(ctx, sha256_padding, padn); sha256_update(ctx, msglen, 8); @@ -263,18 +260,18 @@ void sha256_finish(context_sha256_T *ctx, uint8_t digest[SHA256_SUM_SIZE]) const char *sha256_bytes(const uint8_t *restrict buf, size_t buf_len, const uint8_t *restrict salt, size_t salt_len) { - uint8_t sha256sum[SHA256_SUM_SIZE]; static char hexit[SHA256_BUFFER_SIZE + 1]; // buf size + NULL - context_sha256_T ctx; sha256_self_test(); + context_sha256_T ctx; sha256_start(&ctx); sha256_update(&ctx, buf, buf_len); if (salt != NULL) { sha256_update(&ctx, salt, salt_len); } + uint8_t sha256sum[SHA256_SUM_SIZE]; sha256_finish(&ctx, sha256sum); for (size_t j = 0; j < SHA256_SUM_SIZE; j++) { diff --git a/src/nvim/spellsuggest.c b/src/nvim/spellsuggest.c index ed1ba972d5..d6053a533e 100644 --- a/src/nvim/spellsuggest.c +++ b/src/nvim/spellsuggest.c @@ -276,7 +276,6 @@ static bool can_be_compound(trystate_T *sp, slang_T *slang, uint8_t *compflags, static int score_wordcount_adj(slang_T *slang, int score, char *word, bool split) { int bonus; - int newscore; hashitem_T *hi = hash_find(&slang->sl_wordcount, word); if (HASHITEM_EMPTY(hi)) { @@ -291,11 +290,8 @@ static int score_wordcount_adj(slang_T *slang, int score, char *word, bool split } else { bonus = SCORE_COMMON3; } - if (split) { - newscore = score - bonus / 2; - } else { - newscore = score - bonus; - } + int newscore = split ? score - bonus / 2 + : score - bonus; if (newscore < 0) { return 0; } @@ -449,7 +445,6 @@ void spell_suggest(int count) suginfo_T sug; suggest_T *stp; bool mouse_used; - int limit; int selected = count; int badlen = 0; int msg_scroll_save = msg_scroll; @@ -481,9 +476,7 @@ void spell_suggest(int count) badlen++; end_visual_mode(); // make sure we don't include the NUL at the end of the line - if (badlen > get_cursor_line_len() - curwin->w_cursor.col) { - badlen = get_cursor_line_len() - curwin->w_cursor.col; - } + badlen = MIN(badlen, get_cursor_line_len() - curwin->w_cursor.col); // Find the start of the badly spelled word. } else if (spell_move_to(curwin, FORWARD, SMT_ALL, true, NULL) == 0 || curwin->w_cursor.col > prev_cursor.col) { @@ -519,11 +512,7 @@ void spell_suggest(int count) // Get the list of suggestions. Limit to 'lines' - 2 or the number in // 'spellsuggest', whatever is smaller. - if (sps_limit > Rows - 2) { - limit = Rows - 2; - } else { - limit = sps_limit; - } + int limit = MIN(sps_limit, Rows - 2); spell_find_suggest(line + curwin->w_cursor.col, badlen, &sug, limit, true, need_cap, true); @@ -731,10 +720,7 @@ static void spell_find_suggest(char *badptr, int badlen, suginfo_T *su, int maxc } su->su_maxcount = maxcount; su->su_maxscore = SCORE_MAXINIT; - - if (su->su_badlen >= MAXWLEN) { - su->su_badlen = MAXWLEN - 1; // just in case - } + su->su_badlen = MIN(su->su_badlen, MAXWLEN - 1); // just in case xmemcpyz(su->su_badword, su->su_badptr, (size_t)su->su_badlen); spell_casefold(curwin, su->su_badptr, su->su_badlen, su->su_fbadword, MAXWLEN); @@ -1145,7 +1131,6 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char *fword, bool soun idx_T *idxs, *fidxs, *pidxs; int c, c2, c3; int n = 0; - garray_T *gap; idx_T arridx; int fl = 0; int tl; @@ -1736,11 +1721,8 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char *fword, bool soun // Done all bytes at this node, do next state. When still at // already changed bytes skip the other tricks. PROF_STORE(sp->ts_state) - if (sp->ts_fidx >= sp->ts_fidxtry) { - sp->ts_state = STATE_DEL; - } else { - sp->ts_state = STATE_FINAL; - } + sp->ts_state = sp->ts_fidx >= sp->ts_fidxtry ? STATE_DEL + : STATE_FINAL; } else { arridx += sp->ts_curi++; c = byts[arridx]; @@ -2254,11 +2236,8 @@ static void suggest_trie_walk(suginfo_T *su, langp_T *lp, char *fword, bool soun // valid. p = fword + sp->ts_fidx; - if (soundfold) { - gap = &slang->sl_repsal; - } else { - gap = &lp->lp_replang->sl_rep; - } + garray_T *gap = soundfold ? &slang->sl_repsal + : &lp->lp_replang->sl_rep; while (sp->ts_curi < gap->ga_len) { fromto_T *ftp = (fromto_T *)gap->ga_data + sp->ts_curi++; if (*ftp->ft_from != *p) { @@ -3126,11 +3105,9 @@ static void add_suggestion(suginfo_T *su, garray_T *gap, const char *goodword, i // the best suggestions. if (gap->ga_len > SUG_MAX_COUNT(su)) { if (maxsf) { - su->su_sfmaxscore = cleanup_suggestions(gap, - su->su_sfmaxscore, SUG_CLEAN_COUNT(su)); + su->su_sfmaxscore = cleanup_suggestions(gap, su->su_sfmaxscore, SUG_CLEAN_COUNT(su)); } else { - su->su_maxscore = cleanup_suggestions(gap, - su->su_maxscore, SUG_CLEAN_COUNT(su)); + su->su_maxscore = cleanup_suggestions(gap, su->su_maxscore, SUG_CLEAN_COUNT(su)); } } } @@ -3276,8 +3253,6 @@ static int soundalike_score(char *goodstart, char *badstart) { char *goodsound = goodstart; char *badsound = badstart; - char *pl, *ps; - char *pl2, *ps2; int score = 0; // Adding/inserting "*" at the start (word starts with vowel) shouldn't be @@ -3318,13 +3293,10 @@ static int soundalike_score(char *goodstart, char *badstart) return SCORE_MAXMAX; } - if (n > 0) { - pl = goodsound; // goodsound is longest - ps = badsound; - } else { - pl = badsound; // badsound is longest - ps = goodsound; - } + // n > 0 : goodsound is longest + // n <= 0 : badsound is longest + char *pl = n > 0 ? goodsound : badsound; + char *ps = n > 0 ? badsound : goodsound; // Skip over the identical part. while (*pl == *ps && *pl != NUL) { @@ -3332,6 +3304,8 @@ static int soundalike_score(char *goodstart, char *badstart) ps++; } + char *pl2, *ps2; + switch (n) { case -2: case 2: @@ -3552,19 +3526,13 @@ static int spell_edit_score(slang_T *slang, const char *badword, const char *goo int pgc = wgoodword[j - 2]; if (bc == pgc && pbc == gc) { int t = SCORE_SWAP + CNT(i - 2, j - 2); - if (t < CNT(i, j)) { - CNT(i, j) = t; - } + CNT(i, j) = MIN(CNT(i, j), t); } } int t = SCORE_DEL + CNT(i - 1, j); - if (t < CNT(i, j)) { - CNT(i, j) = t; - } + CNT(i, j) = MIN(CNT(i, j), t); t = SCORE_INS + CNT(i, j - 1); - if (t < CNT(i, j)) { - CNT(i, j) = t; - } + CNT(i, j) = MIN(CNT(i, j), t); } } } diff --git a/src/nvim/statusline.c b/src/nvim/statusline.c index ad7e4587a9..e94e33eebc 100644 --- a/src/nvim/statusline.c +++ b/src/nvim/statusline.c @@ -123,10 +123,7 @@ void win_redr_status(win_T *wp) // len += (int)strlen(p + len); // dead assignment } - int this_ru_col = ru_col - (Columns - stl_width); - if (this_ru_col < (stl_width + 1) / 2) { - this_ru_col = (stl_width + 1) / 2; - } + int this_ru_col = MAX(ru_col - (Columns - stl_width), (stl_width + 1) / 2); if (this_ru_col <= 1) { p = "<"; // No room for file name! len = 1; @@ -374,10 +371,7 @@ static void win_redr_custom(win_T *wp, bool draw_winbar, bool draw_ruler) stl = p_ruf; } } - col = ru_col - (Columns - maxwidth); - if (col < (maxwidth + 1) / 2) { - col = (maxwidth + 1) / 2; - } + col = MAX(ru_col - (Columns - maxwidth), (maxwidth + 1) / 2); maxwidth -= col; if (!in_status_line) { grid = &msg_grid_adj; @@ -574,15 +568,9 @@ void win_redr_ruler(win_T *wp) if (wp->w_status_height == 0 && !is_stl_global) { // can't use last char of screen o++; } - int this_ru_col = ru_col - (Columns - width); - if (this_ru_col < 0) { - this_ru_col = 0; - } // Never use more than half the window/screen width, leave the other half // for the filename. - if (this_ru_col < (width + 1) / 2) { - this_ru_col = (width + 1) / 2; - } + int this_ru_col = MAX(ru_col - (Columns - width), (width + 1) / 2); if (this_ru_col + o < width) { // Need at least 3 chars left for get_rel_pos() + NUL. while (this_ru_col + o < width && RULER_BUF_LEN > i + 4) { @@ -726,7 +714,6 @@ void draw_tabline(void) win_redr_custom(NULL, false, false); } else { int tabcount = 0; - int tabwidth = 0; int col = 0; win_T *cwp; int wincount; @@ -735,13 +722,7 @@ void draw_tabline(void) tabcount++; } - if (tabcount > 0) { - tabwidth = (Columns - 1 + tabcount / 2) / tabcount; - } - - if (tabwidth < 6) { - tabwidth = 6; - } + int tabwidth = MAX(tabcount > 0 ? (Columns - 1 + tabcount / 2) / tabcount : 0, 6); int attr = attr_nosel; tabcount = 0; @@ -810,9 +791,7 @@ void draw_tabline(void) len -= ptr2cells(p); MB_PTR_ADV(p); } - if (len > Columns - col - 1) { - len = Columns - col - 1; - } + len = MIN(len, Columns - col - 1); grid_line_puts(col, p, -1, attr); col += len; @@ -1193,9 +1172,7 @@ int build_stl_str_hl(win_T *wp, char *out, size_t outlen, char *fmt, OptIndex op // If the item was partially or completely truncated, set its // start to the start of the group - if (stl_items[idx].start < t) { - stl_items[idx].start = t; - } + stl_items[idx].start = MAX(stl_items[idx].start, t); } // If the group is shorter than the minimum width, add padding characters. } else if (abs(stl_items[stl_groupitems[groupdepth]].minwid) > group_len) { diff --git a/src/nvim/tag.c b/src/nvim/tag.c index cc7ff070c2..01200e43e0 100644 --- a/src/nvim/tag.c +++ b/src/nvim/tag.c @@ -586,11 +586,7 @@ void do_tag(char *tag, int type, int count, int forceit, bool verbose) || type == DT_LTAG) { cur_match = MAXCOL - 1; } - if (type == DT_TAG) { - max_num_matches = MAXCOL; - } else { - max_num_matches = cur_match + 1; - } + max_num_matches = type == DT_TAG ? MAXCOL : cur_match + 1; // when the argument starts with '/', use it as a regexp if (!no_regexp && *name == '/') { @@ -600,12 +596,8 @@ void do_tag(char *tag, int type, int count, int forceit, bool verbose) flags = TAG_NOIC; } - if (verbose) { - flags |= TAG_VERBOSE; - } - if (!use_tfu) { - flags |= TAG_NO_TAGFUNC; - } + flags |= verbose ? TAG_VERBOSE : 0; + flags |= !use_tfu ? TAG_NO_TAGFUNC : 0; if (find_tags(name, &new_num_matches, &new_matches, flags, max_num_matches, buf_ffname) == OK @@ -815,10 +807,7 @@ static void print_tag_list(bool new_tag, bool use_tagstack, int num_matches, cha // Assume that the first match indicates how long the tags can // be, and align the file names to that. parse_match(matches[0], &tagp); - int taglen = (int)(tagp.tagname_end - tagp.tagname + 2); - if (taglen < 18) { - taglen = 18; - } + int taglen = MAX((int)(tagp.tagname_end - tagp.tagname + 2), 18); if (taglen > Columns - 25) { taglen = MAXCOL; } @@ -999,10 +988,7 @@ static int add_llist_tags(char *tag, int num_matches, char **matches) parse_match(matches[i], &tagp); // Save the tag name - int len = (int)(tagp.tagname_end - tagp.tagname); - if (len > 128) { - len = 128; - } + int len = MIN((int)(tagp.tagname_end - tagp.tagname), 128); xmemcpyz(tag_name, tagp.tagname, (size_t)len); tag_name[len] = NUL; @@ -1063,10 +1049,7 @@ static int add_llist_tags(char *tag, int num_matches, char **matches) strcat(cmd, "\\V"); len += 2; - int cmd_len = (int)(cmd_end - cmd_start + 1); - if (cmd_len > (CMDBUFFSIZE - 5)) { - cmd_len = CMDBUFFSIZE - 5; - } + int cmd_len = MIN((int)(cmd_end - cmd_start + 1), CMDBUFFSIZE - 5); snprintf(cmd + len, (size_t)(CMDBUFFSIZE + 1 - len), "%.*s", cmd_len, cmd_start); len += cmd_len; @@ -1820,11 +1803,9 @@ static tagmatch_status_T findtags_parse_line(findtags_state_T *st, tagptrs_T *ta } else if (st->state == TS_STEP_FORWARD) { assert(cmplen >= 0); if (mb_strnicmp(tagpp->tagname, st->orgpat->head, (size_t)cmplen) != 0) { - if ((off_T)vim_ftell(st->fp) > sinfo_p->match_offset) { - return TAG_MATCH_STOP; // past last match - } else { - return TAG_MATCH_NEXT; // before first match - } + return ((off_T)vim_ftell(st->fp) > sinfo_p->match_offset) + ? TAG_MATCH_STOP // past last match + : TAG_MATCH_NEXT; // before first match } } else { // skip this match if it can't match @@ -1847,11 +1828,9 @@ static tagmatch_status_T findtags_parse_line(findtags_state_T *st, tagptrs_T *ta status = parse_tag_line(st->lbuf, tagpp); } - if (status == FAIL) { - return TAG_MATCH_FAIL; - } - - return TAG_MATCH_SUCCESS; + return status == FAIL + ? TAG_MATCH_FAIL + : TAG_MATCH_SUCCESS; } /// Initialize the structure used for tag matching. @@ -1944,32 +1923,22 @@ static void findtags_add_match(findtags_state_T *st, tagptrs_T *tagpp, findtags_ char *buf_ffname, hash_T *hash) { const bool name_only = (st->flags & TAG_NAMES); - int mtt; size_t len = 0; size_t mfp_size = 0; - bool is_current; // file name matches - bool is_static; // current tag line is static char *mfp; // Decide in which array to store this match. - is_current = test_for_current(tagpp->fname, tagpp->fname_end, - st->tag_fname, buf_ffname); - is_static = test_for_static(tagpp); + bool is_current = test_for_current(tagpp->fname, tagpp->fname_end, + st->tag_fname, buf_ffname); + + // current tag line is static + bool is_static = test_for_static(tagpp); // Decide in which of the sixteen tables to store this match. - if (is_static) { - if (is_current) { - mtt = MT_ST_CUR; - } else { - mtt = MT_ST_OTH; - } - } else { - if (is_current) { - mtt = MT_GL_CUR; - } else { - mtt = MT_GL_OTH; - } - } + int mtt = is_static + ? is_current ? MT_ST_CUR : MT_ST_OTH + : is_current ? MT_GL_CUR : MT_GL_OTH; + if (st->orgpat->regmatch.rm_ic && !margs->match_no_ic) { mtt += MT_IC_OFF; } @@ -2237,13 +2206,11 @@ static void findtags_in_file(findtags_state_T *st, int flags, char *buf_ffname) static int findtags_copy_matches(findtags_state_T *st, char ***matchesp) { const bool name_only = (st->flags & TAG_NAMES); - char **matches; - if (st->match_count > 0) { - matches = xmalloc((size_t)st->match_count * sizeof(char *)); - } else { - matches = NULL; - } + char **matches = st->match_count > 0 + ? xmalloc((size_t)st->match_count * sizeof(char *)) + : NULL; + st->match_count = 0; for (int mtt = 0; mtt < MT_COUNT; mtt++) { for (int i = 0; i < st->ga_match[mtt].ga_len; i++) { @@ -2956,13 +2923,13 @@ static int jumpto_tag(const char *lbuf_arg, int forceit, bool keep_help) p_ic = false; // don't ignore case now p_scs = false; linenr_T save_lnum = curwin->w_cursor.lnum; - if (tagp.tagline > 0) { - // start search before line from "line:" field - curwin->w_cursor.lnum = tagp.tagline - 1; - } else { - // start search before first line - curwin->w_cursor.lnum = 0; - } + + curwin->w_cursor.lnum = tagp.tagline > 0 + // start search before line from "line:" field + ? tagp.tagline - 1 + // start search before first line + : 0; + if (do_search(NULL, pbuf[0], pbuf[0], pbuf + 1, pbuflen - 1, 1, search_options, NULL)) { retval = OK; @@ -3197,18 +3164,12 @@ void tagstack_clear_entry(taggy_T *item) /// @param tagnames expand tag names int expand_tags(bool tagnames, char *pat, int *num_file, char ***file) { - int extra_flag; size_t name_buf_size = 100; - tagptrs_T t_p; int ret; char *name_buf = xmalloc(name_buf_size); - if (tagnames) { - extra_flag = TAG_NAMES; - } else { - extra_flag = 0; - } + int extra_flag = tagnames ? TAG_NAMES : 0; if (pat[0] == '/') { ret = find_tags(pat + 1, num_file, file, TAG_REGEXP | extra_flag | TAG_VERBOSE | TAG_NO_TAGFUNC, @@ -3222,10 +3183,9 @@ int expand_tags(bool tagnames, char *pat, int *num_file, char ***file) // Reorganize the tags for display and matching as strings of: // "<tagname>\0<kind>\0<filename>\0" for (int i = 0; i < *num_file; i++) { - size_t len; - + tagptrs_T t_p; parse_match((*file)[i], &t_p); - len = (size_t)(t_p.tagname_end - t_p.tagname); + size_t len = (size_t)(t_p.tagname_end - t_p.tagname); if (len > name_buf_size - 3) { name_buf_size = len + 3; char *buf = xrealloc(name_buf, name_buf_size); @@ -3254,8 +3214,6 @@ int expand_tags(bool tagnames, char *pat, int *num_file, char ***file) static int add_tag_field(dict_T *dict, const char *field_name, const char *start, const char *end) FUNC_ATTR_NONNULL_ARG(1, 2) { - int len = 0; - // Check that the field name doesn't exist yet. if (tv_dict_find(dict, field_name, -1) != NULL) { if (p_verbose > 0) { @@ -3265,6 +3223,7 @@ static int add_tag_field(dict_T *dict, const char *field_name, const char *start } return FAIL; } + int len = 0; char *buf = xmalloc(MAXPATHL); if (start != NULL) { if (end == NULL) { @@ -3273,10 +3232,7 @@ static int add_tag_field(dict_T *dict, const char *field_name, const char *start end--; } } - len = (int)(end - start); - if (len > MAXPATHL - 1) { - len = MAXPATHL - 1; - } + len = MIN((int)(end - start), MAXPATHL - 1); xmemcpyz(buf, start, (size_t)len); } buf[len] = NUL; @@ -3455,9 +3411,7 @@ static void tagstack_push_item(win_T *wp, char *tagname, int cur_fnum, int cur_m tagstack[idx].tagname = tagname; tagstack[idx].cur_fnum = cur_fnum; tagstack[idx].cur_match = cur_match; - if (tagstack[idx].cur_match < 0) { - tagstack[idx].cur_match = 0; - } + tagstack[idx].cur_match = MAX(tagstack[idx].cur_match, 0); tagstack[idx].fmark.mark = mark; tagstack[idx].fmark.fnum = fnum; tagstack[idx].user_data = user_data; @@ -3507,12 +3461,8 @@ static void tagstack_push_items(win_T *wp, list_T *l) static void tagstack_set_curidx(win_T *wp, int curidx) { wp->w_tagstackidx = curidx; - if (wp->w_tagstackidx < 0) { // sanity check - wp->w_tagstackidx = 0; - } - if (wp->w_tagstackidx > wp->w_tagstacklen) { - wp->w_tagstackidx = wp->w_tagstacklen; - } + // sanity check + wp->w_tagstackidx = MIN(MAX(wp->w_tagstackidx, 0), wp->w_tagstacklen); } // Set the tag stack entries of the specified window. @@ -3523,15 +3473,15 @@ static void tagstack_set_curidx(win_T *wp, int curidx) int set_tagstack(win_T *wp, const dict_T *d, int action) FUNC_ATTR_NONNULL_ARG(1) { - dictitem_T *di; - list_T *l = NULL; - // not allowed to alter the tag stack entries from inside tagfunc if (tfu_in_use) { emsg(_(e_cannot_modify_tag_stack_within_tagfunc)); return FAIL; } + dictitem_T *di; + list_T *l = NULL; + if ((di = tv_dict_find(d, "items", -1)) != NULL) { if (di->di_tv.v_type != VAR_LIST) { emsg(_(e_listreq)); diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index b16777be8a..507a9e3a2a 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -1191,10 +1191,7 @@ static int term_sb_pop(int cols, VTermScreenCell *cells, void *data) memmove(term->sb_buffer, term->sb_buffer + 1, sizeof(term->sb_buffer[0]) * (term->sb_current)); - size_t cols_to_copy = (size_t)cols; - if (cols_to_copy > sbrow->cols) { - cols_to_copy = sbrow->cols; - } + size_t cols_to_copy = MIN((size_t)cols, sbrow->cols); // copy to vterm state memcpy(cells, sbrow->cells, sizeof(cells[0]) * cols_to_copy); diff --git a/src/nvim/textformat.c b/src/nvim/textformat.c index 1722bcc968..13607da043 100644 --- a/src/nvim/textformat.c +++ b/src/nvim/textformat.c @@ -348,9 +348,7 @@ void internal_format(int textwidth, int second_indent, int flags, bool format_on inc_cursor(); } startcol -= curwin->w_cursor.col; - if (startcol < 0) { - startcol = 0; - } + startcol = MAX(startcol, 0); if (State & VREPLACE_FLAG) { // In MODE_VREPLACE state, we will backspace over the text to be @@ -433,9 +431,7 @@ void internal_format(int textwidth, int second_indent, int flags, bool format_on // may have added or removed indent. curwin->w_cursor.col += startcol; colnr_T len = get_cursor_line_len(); - if (curwin->w_cursor.col > len) { - curwin->w_cursor.col = len; - } + curwin->w_cursor.col = MIN(curwin->w_cursor.col, len); } haveto_redraw = true; @@ -758,14 +754,9 @@ int comp_textwidth(bool ff) textwidth -= 8; } } - if (textwidth < 0) { - textwidth = 0; - } + textwidth = MAX(textwidth, 0); if (ff && textwidth == 0) { - textwidth = curwin->w_width_inner - 1; - if (textwidth > 79) { - textwidth = 79; - } + textwidth = MIN(curwin->w_width_inner - 1, 79); } return textwidth; } @@ -1105,11 +1096,7 @@ void format_lines(linenr_T line_count, bool avoid_fex) } first_par_line = false; // If the line is getting long, format it next time - if (get_cursor_line_len() > max_len) { - force_format = true; - } else { - force_format = false; - } + force_format = get_cursor_line_len() > max_len; } } line_breakcheck(); diff --git a/src/nvim/textobject.c b/src/nvim/textobject.c index ffc5bc2cb2..45978765bf 100644 --- a/src/nvim/textobject.c +++ b/src/nvim/textobject.c @@ -1269,11 +1269,7 @@ int current_par(oparg_T *oap, int count, bool include, int type) // When visual area is more than one line: extend it. if (VIsual_active && start_lnum != VIsual.lnum) { extend: - if (start_lnum < VIsual.lnum) { - dir = BACKWARD; - } else { - dir = FORWARD; - } + dir = start_lnum < VIsual.lnum ? BACKWARD : FORWARD; for (int i = count; --i >= 0;) { if (start_lnum == (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count)) { diff --git a/src/nvim/undo.c b/src/nvim/undo.c index ed5c9a508c..c403e94184 100644 --- a/src/nvim/undo.c +++ b/src/nvim/undo.c @@ -1132,17 +1132,11 @@ static void serialize_pos(bufinfo_T *bi, pos_T pos) static void unserialize_pos(bufinfo_T *bi, pos_T *pos) { pos->lnum = undo_read_4c(bi); - if (pos->lnum < 0) { - pos->lnum = 0; - } + pos->lnum = MAX(pos->lnum, 0); pos->col = undo_read_4c(bi); - if (pos->col < 0) { - pos->col = 0; - } + pos->col = MAX(pos->col, 0); pos->coladd = undo_read_4c(bi); - if (pos->coladd < 0) { - pos->coladd = 0; - } + pos->coladd = MAX(pos->coladd, 0); } /// Serializes "info". @@ -1209,14 +1203,12 @@ void u_write_undo(const char *const name, const bool forceit, buf_T *const buf, // Strip any sticky and executable bits. perm = perm & 0666; - int fd; - // If the undo file already exists, verify that it actually is an undo // file, and delete it. if (os_path_exists(file_name)) { if (name == NULL || !forceit) { // Check we can read it and it's an undo file. - fd = os_open(file_name, O_RDONLY, 0); + int fd = os_open(file_name, O_RDONLY, 0); if (fd < 0) { if (name != NULL || p_verbose > 0) { if (name == NULL) { @@ -1261,7 +1253,7 @@ void u_write_undo(const char *const name, const bool forceit, buf_T *const buf, goto theend; } - fd = os_open(file_name, O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); + int fd = os_open(file_name, O_CREAT|O_WRONLY|O_EXCL|O_NOFOLLOW, perm); if (fd < 0) { semsg(_(e_not_open), file_name); goto theend; @@ -2001,9 +1993,7 @@ void undo_time(int step, bool sec, bool file, bool absolute) target = curbuf->b_u_seq_cur + step; } if (step < 0) { - if (target < 0) { - target = 0; - } + target = MAX(target, 0); closest = -1; } else { if (dosec) { @@ -2396,9 +2386,7 @@ static void u_undoredo(bool undo, bool do_buf_event) } // Set the '[ mark. - if (top + 1 < curbuf->b_op_start.lnum) { - curbuf->b_op_start.lnum = top + 1; - } + curbuf->b_op_start.lnum = MIN(curbuf->b_op_start.lnum, top + 1); // Set the '] mark. if (newsize == 0 && top + 1 > curbuf->b_op_end.lnum) { curbuf->b_op_end.lnum = top + 1; @@ -2419,12 +2407,8 @@ static void u_undoredo(bool undo, bool do_buf_event) } // Ensure the '[ and '] marks are within bounds. - if (curbuf->b_op_start.lnum > curbuf->b_ml.ml_line_count) { - curbuf->b_op_start.lnum = curbuf->b_ml.ml_line_count; - } - if (curbuf->b_op_end.lnum > curbuf->b_ml.ml_line_count) { - curbuf->b_op_end.lnum = curbuf->b_ml.ml_line_count; - } + curbuf->b_op_start.lnum = MIN(curbuf->b_op_start.lnum, curbuf->b_ml.ml_line_count); + curbuf->b_op_end.lnum = MIN(curbuf->b_op_end.lnum, curbuf->b_ml.ml_line_count); // Adjust Extmarks if (undo) { diff --git a/src/nvim/usercmd.c b/src/nvim/usercmd.c index cb084edcf0..6bff66dca2 100644 --- a/src/nvim/usercmd.c +++ b/src/nvim/usercmd.c @@ -805,9 +805,7 @@ invalid_count: } } - if (*def < 0) { - *def = 0; - } + *def = MAX(*def, 0); } else if (STRNICMP(attr, "complete", attrlen) == 0) { if (val == NULL) { semsg(_(e_argument_required_for_str), "-complete"); diff --git a/src/nvim/window.c b/src/nvim/window.c index bc0d8e6d7b..b770f0ccab 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -1112,12 +1112,7 @@ win_T *win_split_ins(int size, int flags, win_T *new_wp, int dir, frame_T *to_fl if (new_size == 0) { new_size = oldwin->w_width / 2; } - if (new_size > available - minwidth - 1) { - new_size = available - minwidth - 1; - } - if (new_size < wmw1) { - new_size = wmw1; - } + new_size = MAX(MIN(new_size, available - minwidth - 1), wmw1); // if it doesn't fit in the current window, need win_equal() if (oldwin->w_width - new_size - 1 < p_wmw) { @@ -1198,12 +1193,7 @@ win_T *win_split_ins(int size, int flags, win_T *new_wp, int dir, frame_T *to_fl new_size = oldwin_height / 2; } - if (new_size > available - minheight - STATUS_HEIGHT) { - new_size = available - minheight - STATUS_HEIGHT; - } - if (new_size < wmh1) { - new_size = wmh1; - } + new_size = MAX(MIN(new_size, available - minheight - STATUS_HEIGHT), wmh1); // if it doesn't fit in the current window, need win_equal() if (oldwin_height - new_size - STATUS_HEIGHT < p_wmh) { @@ -1730,12 +1720,8 @@ int make_windows(int count, bool vertical) - (p_wh - p_wmh)) / ((int)p_wmh + STATUS_HEIGHT + global_winbar_height()); } - if (maxcount < 2) { - maxcount = 2; - } - if (count > maxcount) { - count = maxcount; - } + maxcount = MAX(maxcount, 2); + count = MIN(count, maxcount); // add status line now, otherwise first window will be too big if (count > 1) { @@ -2189,9 +2175,7 @@ static void win_equal_rec(win_T *next_curwin, bool current, frame_T *topfr, int if (frame_has_win(fr, next_curwin)) { room += (int)p_wiw - (int)p_wmw; next_curwin_size = 0; - if (new_size < p_wiw) { - new_size = (int)p_wiw; - } + new_size = MAX(new_size, (int)p_wiw); } else { // These windows don't use up room. totwincount -= (n + (fr->fr_next == NULL ? extra_sep : 0)) / ((int)p_wmw + 1); @@ -2254,9 +2238,7 @@ static void win_equal_rec(win_T *next_curwin, bool current, frame_T *topfr, int } if (hnc) { // add next_curwin size next_curwin_size -= (int)p_wiw - (m - n); - if (next_curwin_size < 0) { - next_curwin_size = 0; - } + next_curwin_size = MAX(next_curwin_size, 0); new_size += next_curwin_size; room -= new_size - next_curwin_size; } else { @@ -2319,9 +2301,7 @@ static void win_equal_rec(win_T *next_curwin, bool current, frame_T *topfr, int if (frame_has_win(fr, next_curwin)) { room += (int)p_wh - (int)p_wmh; next_curwin_size = 0; - if (new_size < p_wh) { - new_size = (int)p_wh; - } + new_size = MAX(new_size, (int)p_wh); } else { // These windows don't use up room. totwincount -= get_maximum_wincount(fr, (n + (fr->fr_next == NULL ? extra_sep : 0))); @@ -3931,9 +3911,7 @@ static int frame_minwidth(frame_T *topfrp, win_T *next_curwin) frame_T *frp; FOR_ALL_FRAMES(frp, topfrp->fr_child) { int n = frame_minwidth(frp, next_curwin); - if (n > m) { - m = n; - } + m = MAX(m, n); } } else { // Add up the minimal widths for all frames in this row. @@ -4266,9 +4244,7 @@ int make_tabpages(int maxcount) int count = maxcount; // Limit to 'tabpagemax' tabs. - if (count > p_tpm) { - count = (int)p_tpm; - } + count = MIN(count, (int)p_tpm); // Don't execute autocommands while creating the tab pages. Must do that // when putting the buffers in the windows. @@ -5428,14 +5404,10 @@ void win_new_screensize(void) /// This only does the current tab page, others must be done when made active. void win_new_screen_rows(void) { - int h = (int)ROWS_AVAIL; - if (firstwin == NULL) { // not initialized yet return; } - if (h < frame_minheight(topframe, NULL)) { - h = frame_minheight(topframe, NULL); - } + int h = MAX((int)ROWS_AVAIL, frame_minheight(topframe, NULL)); // First try setting the heights of windows with 'winfixheight'. If // that doesn't result in the right height, forget about that option. @@ -5932,9 +5904,7 @@ static void frame_setheight(frame_T *curfrp, int height) // Row of frames: Also need to resize frames left and right of this // one. First check for the minimal height of these. int h = frame_minheight(curfrp->fr_parent, NULL); - if (height < h) { - height = h; - } + height = MAX(height, h); frame_setheight(curfrp->fr_parent, height); } else { // Column of frames: try to change only frames in this column. @@ -5969,9 +5939,7 @@ static void frame_setheight(frame_T *curfrp, int height) win_T *wp = lastwin_nofloating(); room_cmdline = Rows - (int)p_ch - global_stl_height() - (wp->w_winrow + wp->w_height + wp->w_hsep_height + wp->w_status_height); - if (room_cmdline < 0) { - room_cmdline = 0; - } + room_cmdline = MAX(room_cmdline, 0); } if (height <= room + room_cmdline) { @@ -6003,9 +5971,7 @@ static void frame_setheight(frame_T *curfrp, int height) if (take > 0 && room_cmdline > 0) { // use lines from cmdline first - if (take < room_cmdline) { - room_cmdline = take; - } + room_cmdline = MIN(room_cmdline, take), take -= room_cmdline; topframe->fr_height += room_cmdline; } @@ -6067,12 +6033,7 @@ void win_setwidth_win(int width, win_T *wp) // Always keep current window at least one column wide, even when // 'winminwidth' is zero. if (wp == curwin) { - if (width < p_wmw) { - width = (int)p_wmw; - } - if (width == 0) { - width = 1; - } + width = MAX(MAX(width, (int)p_wmw), 1); } else if (width < 0) { width = 0; } @@ -6110,9 +6071,7 @@ static void frame_setwidth(frame_T *curfrp, int width) // Column of frames: Also need to resize frames above and below of // this one. First check for the minimal width of these. int w = frame_minwidth(curfrp->fr_parent, NULL); - if (width < w) { - width = w; - } + width = MAX(width, w); frame_setwidth(curfrp->fr_parent, width); } else { // Row of frames: try to change only frames in this row. @@ -6309,9 +6268,7 @@ void win_drag_status_line(win_T *dragwin, int offset) } else if (!p_ch_was_zero) { room--; } - if (room < 0) { - room = 0; - } + room = MAX(room, 0); // sum up the room of frames below of the current one FOR_ALL_FRAMES(fr, curfr->fr_next) { room += fr->fr_height - frame_minheight(fr, NULL); @@ -6319,9 +6276,8 @@ void win_drag_status_line(win_T *dragwin, int offset) fr = curfr; // put fr at window that grows } - if (room < offset) { // Not enough room - offset = room; // Move as far as we can - } + // If not enough room then move as far as we can + offset = MIN(offset, room); if (offset <= 0) { return; } @@ -6423,10 +6379,8 @@ void win_drag_vsep_line(win_T *dragwin, int offset) fr = curfr; // put fr at window that grows } - // Not enough room - if (room < offset) { - offset = room; // Move as far as we can - } + // If not enough room thn move as far as we can + offset = MIN(offset, room); // No room at all, quit. if (offset <= 0) { @@ -6597,9 +6551,7 @@ void win_new_height(win_T *wp, int height) { // Don't want a negative height. Happens when splitting a tiny window. // Will equalize heights soon to fix it. - if (height < 0) { - height = 0; - } + height = MAX(height, 0); if (wp->w_height == height) { return; // nothing to do } @@ -6625,9 +6577,8 @@ void scroll_to_fraction(win_T *wp, int prev_height) // Find a value for w_topline that shows the cursor at the same // relative position in the window as before (more or less). linenr_T lnum = wp->w_cursor.lnum; - if (lnum < 1) { // can happen when starting up - lnum = 1; - } + // can happen when starting up + lnum = MAX(lnum, 1); wp->w_wrow = (wp->w_fraction * height - 1) / FRACTION_MULT; int line_size = plines_win_col(wp, lnum, wp->w_cursor.col) - 1; int sline = wp->w_wrow - line_size; @@ -6843,9 +6794,7 @@ void command_height(void) break; } int h = frp->fr_height - frame_minheight(frp, NULL); - if (h > p_ch - old_p_ch) { - h = (int)p_ch - old_p_ch; - } + h = MIN(h, (int)p_ch - old_p_ch); old_p_ch += h; frame_add_height(frp, -h); frp = frp->fr_prev; @@ -6863,9 +6812,7 @@ void command_height(void) return; } - if (msg_row < cmdline_row) { - msg_row = cmdline_row; - } + msg_row = MAX(msg_row, cmdline_row); redraw_cmdline = true; } frame_add_height(frp, (int)(old_p_ch - p_ch)); @@ -7251,9 +7198,7 @@ int min_rows(void) int total = 0; FOR_ALL_TABS(tp) { int n = frame_minheight(tp->tp_topframe, NULL); - if (total < n) { - total = n; - } + total = MAX(total, n); } total += tabline_height() + global_stl_height(); if (p_ch > 0) { @@ -7548,13 +7493,7 @@ static int int_cmp(const void *pa, const void *pb) { const int a = *(const int *)pa; const int b = *(const int *)pb; - if (a > b) { - return 1; - } - if (a < b) { - return -1; - } - return 0; + return a == b ? 0 : a < b ? -1 : 1; } /// Handle setting 'colorcolumn' or 'textwidth' in window "wp". |