diff options
Diffstat (limited to 'src/nvim/diff.c')
-rw-r--r-- | src/nvim/diff.c | 249 |
1 files changed, 118 insertions, 131 deletions
diff --git a/src/nvim/diff.c b/src/nvim/diff.c index ce79158050..4826e70727 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -29,7 +29,6 @@ #include "nvim/path.h" #include "nvim/screen.h" #include "nvim/strings.h" -#include "nvim/tempfile.h" #include "nvim/undo.h" #include "nvim/window.h" #include "nvim/os/os.h" @@ -658,9 +657,9 @@ void ex_diffupdate(exarg_T *eap) } // We need three temp file names. - char_u *tmp_orig = vim_tempname(); - char_u *tmp_new = vim_tempname(); - char_u *tmp_diff = vim_tempname(); + char *tmp_orig = (char *) vim_tempname(); + char *tmp_new = (char *) vim_tempname(); + char *tmp_diff = (char *) vim_tempname(); if ((tmp_orig == NULL) || (tmp_new == NULL) || (tmp_diff == NULL)) { goto theend; @@ -670,11 +669,11 @@ void ex_diffupdate(exarg_T *eap) // are no differences. Can't use the return value, it's non-zero when // there are differences. // May try twice, first with "-a" and then without. - int io_error = FALSE; - int ok = FALSE; + int io_error = false; + bool ok = false; for (;;) { - ok = FALSE; - FILE *fd = mch_fopen((char *)tmp_orig, "w"); + ok = false; + FILE *fd = mch_fopen(tmp_orig, "w"); if (fd == NULL) { io_error = TRUE; @@ -683,7 +682,7 @@ void ex_diffupdate(exarg_T *eap) io_error = TRUE; } fclose(fd); - fd = mch_fopen((char *)tmp_new, "w"); + fd = mch_fopen(tmp_new, "w"); if (fd == NULL) { io_error = TRUE; @@ -693,7 +692,7 @@ void ex_diffupdate(exarg_T *eap) } fclose(fd); diff_file(tmp_orig, tmp_new, tmp_diff); - fd = mch_fopen((char *)tmp_diff, "r"); + fd = mch_fopen(tmp_diff, "r"); if (fd == NULL) { io_error = TRUE; @@ -712,10 +711,10 @@ void ex_diffupdate(exarg_T *eap) } fclose(fd); } - os_remove((char *)tmp_diff); - os_remove((char *)tmp_new); + os_remove(tmp_diff); + os_remove(tmp_new); } - os_remove((char *)tmp_orig); + os_remove(tmp_orig); } // When using 'diffexpr' break here. @@ -756,28 +755,28 @@ void ex_diffupdate(exarg_T *eap) // Write the first buffer to a tempfile. buf_T *buf = curtab->tp_diffbuf[idx_orig]; - if (diff_write(buf, tmp_orig) == FAIL) { + if (diff_write(buf, (char_u *) tmp_orig) == FAIL) { goto theend; } // Make a difference between the first buffer and every other. for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new) { buf_T *buf = curtab->tp_diffbuf[idx_new]; - if (buf == NULL) { - continue; + if (buf == NULL || buf->b_ml.ml_mfp == NULL) { + continue; // skip buffer that isn't loaded } - if (diff_write(buf, tmp_new) == FAIL) { + if (diff_write(buf, (char_u *) tmp_new) == FAIL) { continue; } diff_file(tmp_orig, tmp_new, tmp_diff); // Read the diff output and add each entry to the diff list. - diff_read(idx_orig, idx_new, tmp_diff); - os_remove((char *)tmp_diff); - os_remove((char *)tmp_new); + diff_read(idx_orig, idx_new, (char_u *) tmp_diff); + os_remove(tmp_diff); + os_remove(tmp_new); } - os_remove((char *)tmp_orig); + os_remove(tmp_orig); // force updating cursor position on screen curwin->w_valid_cursor.lnum = 0; @@ -795,15 +794,16 @@ theend: /// @param tmp_orig /// @param tmp_new /// @param tmp_diff -static void diff_file(char_u *tmp_orig, char_u *tmp_new, char_u *tmp_diff) +static void diff_file(const char *const tmp_orig, const char *const tmp_new, + const char *const tmp_diff) { if (*p_dex != NUL) { // Use 'diffexpr' to generate the diff file. eval_diff(tmp_orig, tmp_new, tmp_diff); } else { - size_t len = STRLEN(tmp_orig) + STRLEN(tmp_new) + STRLEN(tmp_diff) - + STRLEN(p_srr) + 27; - char_u *cmd = xmalloc(len); + const size_t len = (strlen(tmp_orig) + strlen(tmp_new) + strlen(tmp_diff) + + STRLEN(p_srr) + 27); + char *const cmd = xmalloc(len); /* We don't want $DIFF_OPTIONS to get in the way. */ if (os_getenv("DIFF_OPTIONS")) { @@ -813,19 +813,17 @@ static void diff_file(char_u *tmp_orig, char_u *tmp_new, char_u *tmp_diff) /* Build the diff command and execute it. Always use -a, binary * differences are of no use. Ignore errors, diff returns * non-zero when differences have been found. */ - vim_snprintf((char *)cmd, len, "diff %s%s%s%s%s %s", - diff_a_works == FALSE ? "" : "-a ", + vim_snprintf(cmd, len, "diff %s%s%s%s%s %s", + diff_a_works ? "-a " : "", "", (diff_flags & DIFF_IWHITE) ? "-b " : "", (diff_flags & DIFF_ICASE) ? "-i " : "", tmp_orig, tmp_new); - append_redir(cmd, (int)len, p_srr, tmp_diff); - block_autocmds(); /* Avoid ShellCmdPost stuff */ - (void)call_shell( - cmd, - kShellOptFilter | kShellOptSilent | kShellOptDoOut, - NULL - ); + append_redir(cmd, len, (char *) p_srr, tmp_diff); + block_autocmds(); // Avoid ShellCmdPost stuff + (void)call_shell((char_u *) cmd, + kShellOptFilter | kShellOptSilent | kShellOptDoOut, + NULL); unblock_autocmds(); xfree(cmd); } @@ -902,9 +900,11 @@ void ex_diffpatch(exarg_T *eap) if (*p_pex != NUL) { // Use 'patchexpr' to generate the new file. #ifdef UNIX - eval_patch(tmp_orig, fullname != NULL ? fullname : eap->arg, tmp_new); + eval_patch((char *) tmp_orig, + (char *) (fullname != NULL ? fullname : eap->arg), + (char *) tmp_new); #else - eval_patch(tmp_orig, eap->arg, tmp_new); + eval_patch((char *) tmp_orig, (char *) eap->arg, (char *) tmp_new); #endif // ifdef UNIX } else { // Build the patch command and execute it. Ignore errors. Switch to @@ -1057,27 +1057,28 @@ void diff_win_options(win_T *wp, int addbuf) newFoldLevel(); curwin = old_curwin; - wp->w_p_diff = TRUE; - // Use 'scrollbind' and 'cursorbind' when available - if (!wp->w_p_diff_saved) { + if (!wp->w_p_diff) { wp->w_p_scb_save = wp->w_p_scb; } wp->w_p_scb = TRUE; - if (!wp->w_p_diff_saved) { + if (!wp->w_p_diff) { wp->w_p_crb_save = wp->w_p_crb; } wp->w_p_crb = TRUE; - if (!wp->w_p_diff_saved) { + if (!wp->w_p_diff) { wp->w_p_wrap_save = wp->w_p_wrap; } wp->w_p_wrap = FALSE; curwin = wp; curbuf = curwin->w_buffer; - if (!wp->w_p_diff_saved) { + if (!wp->w_p_diff) { + if (wp->w_p_diff_saved) { + free_string_option(wp->w_p_fdm_save); + } wp->w_p_fdm_save = vim_strsave(wp->w_p_fdm); } set_string_option_direct((char_u *)"fdm", -1, (char_u *)"diff", @@ -1085,7 +1086,7 @@ void diff_win_options(win_T *wp, int addbuf) curwin = old_curwin; curbuf = curwin->w_buffer; - if (!wp->w_p_diff_saved) { + if (!wp->w_p_diff) { wp->w_p_fdc_save = wp->w_p_fdc; wp->w_p_fen_save = wp->w_p_fen; wp->w_p_fdl_save = wp->w_p_fdl; @@ -1104,6 +1105,8 @@ void diff_win_options(win_T *wp, int addbuf) // Saved the current values, to be restored in ex_diffoff(). wp->w_p_diff_saved = TRUE; + wp->w_p_diff = true; + if (addbuf) { diff_buf_add(wp->w_buffer); } @@ -1116,68 +1119,50 @@ void diff_win_options(win_T *wp, int addbuf) /// @param eap void ex_diffoff(exarg_T *eap) { - win_T *old_curwin = curwin; - int diffwin = FALSE; + int diffwin = false; FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { if (eap->forceit ? wp->w_p_diff : (wp == curwin)) { - // Set 'diff', 'scrollbind' off and 'wrap' on. If option values - // were saved in diff_win_options() restore them. - wp->w_p_diff = FALSE; - - if (wp->w_p_scb) { - wp->w_p_scb = wp->w_p_diff_saved ? wp->w_p_scb_save : FALSE; - } - - if (wp->w_p_crb) { - wp->w_p_crb = wp->w_p_diff_saved ? wp->w_p_crb_save : FALSE; - } - - if (!wp->w_p_wrap) { - wp->w_p_wrap = wp->w_p_diff_saved ? wp->w_p_wrap_save : TRUE; - } - curwin = wp; - curbuf = curwin->w_buffer; + // Set 'diff' off. If option values were saved in + // diff_win_options(), restore the ones whose settings seem to have + // been left over from diff mode. + wp->w_p_diff = false; if (wp->w_p_diff_saved) { - free_string_option(wp->w_p_fdm); - wp->w_p_fdm = wp->w_p_fdm_save; - wp->w_p_fdm_save = empty_option; - } else { - set_string_option_direct((char_u *)"fdm", -1, - (char_u *)"manual", OPT_LOCAL | OPT_FREE, 0); - } - curwin = old_curwin; - curbuf = curwin->w_buffer; + if (wp->w_p_scb) { + wp->w_p_scb = wp->w_p_scb_save; + } - if (wp->w_p_fdc == diff_foldcolumn) { - wp->w_p_fdc = wp->w_p_diff_saved ? wp->w_p_fdc_save : 0; - } + if (wp->w_p_crb) { + wp->w_p_crb = wp->w_p_crb_save; + } - if ((wp->w_p_fdl == 0) - && wp->w_p_diff_saved) { - wp->w_p_fdl = wp->w_p_fdl_save; - } + if (!wp->w_p_wrap) { + wp->w_p_wrap = wp->w_p_wrap_save; + } - if (wp->w_p_fen) { + free_string_option(wp->w_p_fdm); + wp->w_p_fdm = vim_strsave(wp->w_p_fdm_save); + if (wp->w_p_fdc == diff_foldcolumn) { + wp->w_p_fdc = wp->w_p_fdc_save; + } + if (wp->w_p_fdl == 0) { + wp->w_p_fdl = wp->w_p_fdl_save; + } // Only restore 'foldenable' when 'foldmethod' is not // "manual", otherwise we continue to show the diff folds. - if (foldmethodIsManual(wp) || !wp->w_p_diff_saved) { - wp->w_p_fen = FALSE; - } else { - wp->w_p_fen = wp->w_p_fen_save; + if (wp->w_p_fen) { + wp->w_p_fen = foldmethodIsManual(wp) ? false : wp->w_p_fen_save; } - } - foldUpdateAll(wp); + foldUpdateAll(wp); - // make sure topline is not halfway through a fold - changed_window_setting_win(wp); + // make sure topline is not halfway through a fold + changed_window_setting_win(wp); + } // Note: 'sbo' is not restored, it's a global option. diff_buf_adjust(wp); - - wp->w_p_diff_saved = FALSE; } diffwin |= wp->w_p_diff; } @@ -1546,37 +1531,37 @@ int diff_check(win_T *wp, linenr_T lnum) return maxcount - dp->df_count[idx]; } -/// Compare two entries in diff "*dp" and return TRUE if they are equal. +/// Compare two entries in diff "dp" and return true if they are equal. /// -/// @param dp -/// @param idx1 First entry in diff "*dp" -/// @param idx2 Second entry in diff "*dp" +/// @param dp diff +/// @param idx1 first entry in diff "dp" +/// @param idx2 second entry in diff "dp" /// -/// @return return TRUE if two entires are equal. -static int diff_equal_entry(diff_T *dp, int idx1, int idx2) +/// @return true if two entires are equal. +static bool diff_equal_entry(diff_T *dp, int idx1, int idx2) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1) { if (dp->df_count[idx1] != dp->df_count[idx2]) { - return FALSE; + return false; } if (diff_check_sanity(curtab, dp) == FAIL) { - return FALSE; + return false; } - int i; - for (i = 0; i < dp->df_count[idx1]; ++i) { + for (int i = 0; i < dp->df_count[idx1]; i++) { char_u *line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1], - dp->df_lnum[idx1] + i, FALSE)); + dp->df_lnum[idx1] + i, false)); int cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2], - dp->df_lnum[idx2] + i, FALSE)); + dp->df_lnum[idx2] + i, false)); xfree(line); if (cmp != 0) { - return FALSE; + return false; } } - return TRUE; + return true; } /// Compare strings "s1" and "s2" according to 'diffopt'. @@ -1845,28 +1830,30 @@ int diffopt_changed(void) return OK; } -/// Return TRUE if 'diffopt' contains "horizontal". -/// -/// @return TRUE if 'diffopt' contains "horizontal" -int diffopt_horizontal(void) +/// Check that "diffopt" contains "horizontal". +bool diffopt_horizontal(void) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { return (diff_flags & DIFF_HORIZONTAL) != 0; } /// Find the difference within a changed line. /// -/// @param startp first char of the change -/// @param endp last char of the change +/// @param wp window whose current buffer to check +/// @param lnum line number to check within the buffer +/// @param startp first char of the change +/// @param endp last char of the change /// -/// @returns TRUE if the line was added, no other buffer has it. -int diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp) +/// @return true if the line was added, no other buffer has it. +bool diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp) + FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL { char_u *line_new; int si_org; int si_new; int ei_org; int ei_new; - int added = TRUE; + bool added = true; // Make a copy of the line, the next ml_get() will invalidate it. char_u *line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE)); @@ -1875,7 +1862,7 @@ int diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp) if (idx == DB_COUNT) { // cannot happen xfree(line_org); - return FALSE; + return false; } // search for a change that includes "lnum" in the list of diffblocks. @@ -1888,7 +1875,7 @@ int diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp) if ((dp == NULL) || (diff_check_sanity(curtab, dp) == FAIL)) { xfree(line_org); - return FALSE; + return false; } int off = lnum - dp->df_lnum[idx]; @@ -1899,7 +1886,7 @@ int diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp) if (off >= dp->df_count[i]) { continue; } - added = FALSE; + added = false; line_new = ml_get_buf(curtab->tp_diffbuf[i], dp->df_lnum[i] + off, FALSE); @@ -1971,21 +1958,22 @@ int diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp) return added; } -/// Return TRUE if line "lnum" is not close to a diff block, this line should +/// Check that line "lnum" is not close to a diff block, this line should /// be in a fold. /// -/// @param wp -/// @param lnum +/// @param wp window containing the buffer to check +/// @param lnum line number to check within the buffer /// -/// @return FALSE if there are no diff blocks at all in this window. -int diff_infold(win_T *wp, linenr_T lnum) +/// @return false if there are no diff blocks at all in this window. +bool diff_infold(win_T *wp, linenr_T lnum) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1) { - int other = FALSE; + bool other = false; diff_T *dp; // Return if 'diff' isn't set. if (!wp->w_p_diff) { - return FALSE; + return false; } int idx = -1; @@ -1994,13 +1982,13 @@ int diff_infold(win_T *wp, linenr_T lnum) if (curtab->tp_diffbuf[i] == wp->w_buffer) { idx = i; } else if (curtab->tp_diffbuf[i] != NULL) { - other = TRUE; + other = true; } } // return here if there are no diffs in the window if ((idx == -1) || !other) { - return FALSE; + return false; } if (curtab->tp_diff_invalid) { @@ -2010,7 +1998,7 @@ int diff_infold(win_T *wp, linenr_T lnum) // Return if there are no diff blocks. All lines will be folded. if (curtab->tp_first_diff == NULL) { - return TRUE; + return true; } for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next) { @@ -2021,10 +2009,10 @@ int diff_infold(win_T *wp, linenr_T lnum) // If this change ends before the line we have a match. if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum) { - return FALSE; + return false; } } - return TRUE; + return true; } /// "dp" and "do" commands. @@ -2387,12 +2375,11 @@ static void diff_fold_update(diff_T *dp, int skip_idx) } } -/// Checks if the buffer is in diff-mode. -/// -/// @param buf The buffer to check. +/// Checks that the buffer is in diff-mode. /// -/// @return TRUE if buffer "buf" is in diff-mode. +/// @param buf buffer to check. bool diff_mode_buf(buf_T *buf) + FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1) { FOR_ALL_TABS(tp) { if (diff_buf_idx_tp(buf, tp) != DB_COUNT) { |