aboutsummaryrefslogtreecommitdiff
path: root/src/diff.c
diff options
context:
space:
mode:
authorThiago de Arruda <tpadilha84@gmail.com>2014-01-31 10:39:15 -0300
committerThiago de Arruda <tpadilha84@gmail.com>2014-01-31 10:39:15 -0300
commit72cf89bce8e4230dbc161dc5606f48ef9884ba70 (patch)
tree3ee06dbd8c57e291a8713289670ae1cb51397d44 /src/diff.c
downloadrneovim-72cf89bce8e4230dbc161dc5606f48ef9884ba70.tar.gz
rneovim-72cf89bce8e4230dbc161dc5606f48ef9884ba70.tar.bz2
rneovim-72cf89bce8e4230dbc161dc5606f48ef9884ba70.zip
Import vim from changeset v5628:c9cad40b4181
- Cleanup source tree, leaving only files necessary for compilation/testing - Process files through unifdef to remove tons of FEAT_* macros - Process files through uncrustify to normalize source code formatting. - Port the build system to cmake
Diffstat (limited to 'src/diff.c')
-rw-r--r--src/diff.c2296
1 files changed, 2296 insertions, 0 deletions
diff --git a/src/diff.c b/src/diff.c
new file mode 100644
index 0000000000..ad31128575
--- /dev/null
+++ b/src/diff.c
@@ -0,0 +1,2296 @@
+/* vim:set ts=8 sts=4 sw=4:
+ *
+ * VIM - Vi IMproved by Bram Moolenaar
+ *
+ * Do ":help uganda" in Vim to read copying and usage conditions.
+ * Do ":help credits" in Vim to see a list of people who contributed.
+ * See README.txt for an overview of the Vim source code.
+ */
+
+/*
+ * diff.c: code for diff'ing two, three or four buffers.
+ */
+
+#include "vim.h"
+
+
+static int diff_busy = FALSE; /* ex_diffgetput() is busy */
+
+/* flags obtained from the 'diffopt' option */
+#define DIFF_FILLER 1 /* display filler lines */
+#define DIFF_ICASE 2 /* ignore case */
+#define DIFF_IWHITE 4 /* ignore change in white space */
+#define DIFF_HORIZONTAL 8 /* horizontal splits */
+#define DIFF_VERTICAL 16 /* vertical splits */
+static int diff_flags = DIFF_FILLER;
+
+#define LBUFLEN 50 /* length of line in diff file */
+
+static int diff_a_works = MAYBE; /* TRUE when "diff -a" works, FALSE when it
+ doesn't work, MAYBE when not checked yet */
+
+static int diff_buf_idx __ARGS((buf_T *buf));
+static int diff_buf_idx_tp __ARGS((buf_T *buf, tabpage_T *tp));
+static void diff_mark_adjust_tp __ARGS((tabpage_T *tp, int idx, linenr_T line1,
+ linenr_T line2, long amount,
+ long amount_after));
+static void diff_check_unchanged __ARGS((tabpage_T *tp, diff_T *dp));
+static int diff_check_sanity __ARGS((tabpage_T *tp, diff_T *dp));
+static void diff_redraw __ARGS((int dofold));
+static int diff_write __ARGS((buf_T *buf, char_u *fname));
+static void diff_file __ARGS((char_u *tmp_orig, char_u *tmp_new,
+ char_u *tmp_diff));
+static int diff_equal_entry __ARGS((diff_T *dp, int idx1, int idx2));
+static int diff_cmp __ARGS((char_u *s1, char_u *s2));
+static void diff_fold_update __ARGS((diff_T *dp, int skip_idx));
+static void diff_read __ARGS((int idx_orig, int idx_new, char_u *fname));
+static void diff_copy_entry __ARGS((diff_T *dprev, diff_T *dp, int idx_orig,
+ int idx_new));
+static diff_T *diff_alloc_new __ARGS((tabpage_T *tp, diff_T *dprev, diff_T *dp));
+
+#ifndef USE_CR
+# define tag_fgets vim_fgets
+#endif
+
+/*
+ * Called when deleting or unloading a buffer: No longer make a diff with it.
+ */
+void diff_buf_delete(buf)
+buf_T *buf;
+{
+ int i;
+ tabpage_T *tp;
+
+ for (tp = first_tabpage; tp != NULL; tp = tp->tp_next) {
+ i = diff_buf_idx_tp(buf, tp);
+ if (i != DB_COUNT) {
+ tp->tp_diffbuf[i] = NULL;
+ tp->tp_diff_invalid = TRUE;
+ if (tp == curtab)
+ diff_redraw(TRUE);
+ }
+ }
+}
+
+/*
+ * Check if the current buffer should be added to or removed from the list of
+ * diff buffers.
+ */
+void diff_buf_adjust(win)
+win_T *win;
+{
+ win_T *wp;
+ int i;
+
+ if (!win->w_p_diff) {
+ /* When there is no window showing a diff for this buffer, remove
+ * it from the diffs. */
+ for (wp = firstwin; wp != NULL; wp = wp->w_next)
+ if (wp->w_buffer == win->w_buffer && wp->w_p_diff)
+ break;
+ if (wp == NULL) {
+ i = diff_buf_idx(win->w_buffer);
+ if (i != DB_COUNT) {
+ curtab->tp_diffbuf[i] = NULL;
+ curtab->tp_diff_invalid = TRUE;
+ diff_redraw(TRUE);
+ }
+ }
+ } else
+ diff_buf_add(win->w_buffer);
+}
+
+/*
+ * Add a buffer to make diffs for.
+ * Call this when a new buffer is being edited in the current window where
+ * 'diff' is set.
+ * Marks the current buffer as being part of the diff and requiring updating.
+ * This must be done before any autocmd, because a command may use info
+ * about the screen contents.
+ */
+void diff_buf_add(buf)
+buf_T *buf;
+{
+ int i;
+
+ if (diff_buf_idx(buf) != DB_COUNT)
+ return; /* It's already there. */
+
+ for (i = 0; i < DB_COUNT; ++i)
+ if (curtab->tp_diffbuf[i] == NULL) {
+ curtab->tp_diffbuf[i] = buf;
+ curtab->tp_diff_invalid = TRUE;
+ diff_redraw(TRUE);
+ return;
+ }
+
+ EMSGN(_("E96: Can not diff more than %ld buffers"), DB_COUNT);
+}
+
+/*
+ * Find buffer "buf" in the list of diff buffers for the current tab page.
+ * Return its index or DB_COUNT if not found.
+ */
+static int diff_buf_idx(buf)
+buf_T *buf;
+{
+ int idx;
+
+ for (idx = 0; idx < DB_COUNT; ++idx)
+ if (curtab->tp_diffbuf[idx] == buf)
+ break;
+ return idx;
+}
+
+/*
+ * Find buffer "buf" in the list of diff buffers for tab page "tp".
+ * Return its index or DB_COUNT if not found.
+ */
+static int diff_buf_idx_tp(buf, tp)
+buf_T *buf;
+tabpage_T *tp;
+{
+ int idx;
+
+ for (idx = 0; idx < DB_COUNT; ++idx)
+ if (tp->tp_diffbuf[idx] == buf)
+ break;
+ return idx;
+}
+
+/*
+ * Mark the diff info involving buffer "buf" as invalid, it will be updated
+ * when info is requested.
+ */
+void diff_invalidate(buf)
+buf_T *buf;
+{
+ tabpage_T *tp;
+ int i;
+
+ for (tp = first_tabpage; tp != NULL; tp = tp->tp_next) {
+ i = diff_buf_idx_tp(buf, tp);
+ if (i != DB_COUNT) {
+ tp->tp_diff_invalid = TRUE;
+ if (tp == curtab)
+ diff_redraw(TRUE);
+ }
+ }
+}
+
+/*
+ * Called by mark_adjust(): update line numbers in "curbuf".
+ */
+void diff_mark_adjust(line1, line2, amount, amount_after)
+linenr_T line1;
+linenr_T line2;
+long amount;
+long amount_after;
+{
+ int idx;
+ tabpage_T *tp;
+
+ /* Handle all tab pages that use the current buffer in a diff. */
+ for (tp = first_tabpage; tp != NULL; tp = tp->tp_next) {
+ idx = diff_buf_idx_tp(curbuf, tp);
+ if (idx != DB_COUNT)
+ diff_mark_adjust_tp(tp, idx, line1, line2, amount, amount_after);
+ }
+}
+
+/*
+ * Update line numbers in tab page "tp" for "curbuf" with index "idx".
+ * This attempts to update the changes as much as possible:
+ * When inserting/deleting lines outside of existing change blocks, create a
+ * new change block and update the line numbers in following blocks.
+ * When inserting/deleting lines in existing change blocks, update them.
+ */
+static void diff_mark_adjust_tp(tp, idx, line1, line2, amount, amount_after)
+tabpage_T *tp;
+int idx;
+linenr_T line1;
+linenr_T line2;
+long amount;
+long amount_after;
+{
+ diff_T *dp;
+ diff_T *dprev;
+ diff_T *dnext;
+ int i;
+ int inserted, deleted;
+ int n, off;
+ linenr_T last;
+ linenr_T lnum_deleted = line1; /* lnum of remaining deletion */
+ int check_unchanged;
+
+ if (line2 == MAXLNUM) {
+ /* mark_adjust(99, MAXLNUM, 9, 0): insert lines */
+ inserted = amount;
+ deleted = 0;
+ } else if (amount_after > 0) {
+ /* mark_adjust(99, 98, MAXLNUM, 9): a change that inserts lines*/
+ inserted = amount_after;
+ deleted = 0;
+ } else {
+ /* mark_adjust(98, 99, MAXLNUM, -2): delete lines */
+ inserted = 0;
+ deleted = -amount_after;
+ }
+
+ dprev = NULL;
+ dp = tp->tp_first_diff;
+ for (;; ) {
+ /* If the change is after the previous diff block and before the next
+ * diff block, thus not touching an existing change, create a new diff
+ * block. Don't do this when ex_diffgetput() is busy. */
+ if ((dp == NULL || dp->df_lnum[idx] - 1 > line2
+ || (line2 == MAXLNUM && dp->df_lnum[idx] > line1))
+ && (dprev == NULL
+ || dprev->df_lnum[idx] + dprev->df_count[idx] < line1)
+ && !diff_busy) {
+ dnext = diff_alloc_new(tp, dprev, dp);
+ if (dnext == NULL)
+ return;
+
+ dnext->df_lnum[idx] = line1;
+ dnext->df_count[idx] = inserted;
+ for (i = 0; i < DB_COUNT; ++i)
+ if (tp->tp_diffbuf[i] != NULL && i != idx) {
+ if (dprev == NULL)
+ dnext->df_lnum[i] = line1;
+ else
+ dnext->df_lnum[i] = line1
+ + (dprev->df_lnum[i] + dprev->df_count[i])
+ - (dprev->df_lnum[idx] + dprev->df_count[idx]);
+ dnext->df_count[i] = deleted;
+ }
+ }
+
+ /* if at end of the list, quit */
+ if (dp == NULL)
+ break;
+
+ /*
+ * Check for these situations:
+ * 1 2 3
+ * 1 2 3
+ * line1 2 3 4 5
+ * 2 3 4 5
+ * 2 3 4 5
+ * line2 2 3 4 5
+ * 3 5 6
+ * 3 5 6
+ */
+ /* compute last line of this change */
+ last = dp->df_lnum[idx] + dp->df_count[idx] - 1;
+
+ /* 1. change completely above line1: nothing to do */
+ if (last >= line1 - 1) {
+ /* 6. change below line2: only adjust for amount_after; also when
+ * "deleted" became zero when deleted all lines between two diffs */
+ if (dp->df_lnum[idx] - (deleted + inserted != 0) > line2) {
+ if (amount_after == 0)
+ break; /* nothing left to change */
+ dp->df_lnum[idx] += amount_after;
+ } else {
+ check_unchanged = FALSE;
+
+ /* 2. 3. 4. 5.: inserted/deleted lines touching this diff. */
+ if (deleted > 0) {
+ if (dp->df_lnum[idx] >= line1) {
+ off = dp->df_lnum[idx] - lnum_deleted;
+ if (last <= line2) {
+ /* 4. delete all lines of diff */
+ if (dp->df_next != NULL
+ && dp->df_next->df_lnum[idx] - 1 <= line2) {
+ /* delete continues in next diff, only do
+ * lines until that one */
+ n = dp->df_next->df_lnum[idx] - lnum_deleted;
+ deleted -= n;
+ n -= dp->df_count[idx];
+ lnum_deleted = dp->df_next->df_lnum[idx];
+ } else
+ n = deleted - dp->df_count[idx];
+ dp->df_count[idx] = 0;
+ } else {
+ /* 5. delete lines at or just before top of diff */
+ n = off;
+ dp->df_count[idx] -= line2 - dp->df_lnum[idx] + 1;
+ check_unchanged = TRUE;
+ }
+ dp->df_lnum[idx] = line1;
+ } else {
+ off = 0;
+ if (last < line2) {
+ /* 2. delete at end of of diff */
+ dp->df_count[idx] -= last - lnum_deleted + 1;
+ if (dp->df_next != NULL
+ && dp->df_next->df_lnum[idx] - 1 <= line2) {
+ /* delete continues in next diff, only do
+ * lines until that one */
+ n = dp->df_next->df_lnum[idx] - 1 - last;
+ deleted -= dp->df_next->df_lnum[idx]
+ - lnum_deleted;
+ lnum_deleted = dp->df_next->df_lnum[idx];
+ } else
+ n = line2 - last;
+ check_unchanged = TRUE;
+ } else {
+ /* 3. delete lines inside the diff */
+ n = 0;
+ dp->df_count[idx] -= deleted;
+ }
+ }
+
+ for (i = 0; i < DB_COUNT; ++i)
+ if (tp->tp_diffbuf[i] != NULL && i != idx) {
+ dp->df_lnum[i] -= off;
+ dp->df_count[i] += n;
+ }
+ } else {
+ if (dp->df_lnum[idx] <= line1) {
+ /* inserted lines somewhere in this diff */
+ dp->df_count[idx] += inserted;
+ check_unchanged = TRUE;
+ } else
+ /* inserted lines somewhere above this diff */
+ dp->df_lnum[idx] += inserted;
+ }
+
+ if (check_unchanged)
+ /* Check if inserted lines are equal, may reduce the
+ * size of the diff. TODO: also check for equal lines
+ * in the middle and perhaps split the block. */
+ diff_check_unchanged(tp, dp);
+ }
+ }
+
+ /* check if this block touches the previous one, may merge them. */
+ if (dprev != NULL && dprev->df_lnum[idx] + dprev->df_count[idx]
+ == dp->df_lnum[idx]) {
+ for (i = 0; i < DB_COUNT; ++i)
+ if (tp->tp_diffbuf[i] != NULL)
+ dprev->df_count[i] += dp->df_count[i];
+ dprev->df_next = dp->df_next;
+ vim_free(dp);
+ dp = dprev->df_next;
+ } else {
+ /* Advance to next entry. */
+ dprev = dp;
+ dp = dp->df_next;
+ }
+ }
+
+ dprev = NULL;
+ dp = tp->tp_first_diff;
+ while (dp != NULL) {
+ /* All counts are zero, remove this entry. */
+ for (i = 0; i < DB_COUNT; ++i)
+ if (tp->tp_diffbuf[i] != NULL && dp->df_count[i] != 0)
+ break;
+ if (i == DB_COUNT) {
+ dnext = dp->df_next;
+ vim_free(dp);
+ dp = dnext;
+ if (dprev == NULL)
+ tp->tp_first_diff = dnext;
+ else
+ dprev->df_next = dnext;
+ } else {
+ /* Advance to next entry. */
+ dprev = dp;
+ dp = dp->df_next;
+ }
+
+ }
+
+ if (tp == curtab) {
+ diff_redraw(TRUE);
+
+ /* Need to recompute the scroll binding, may remove or add filler
+ * lines (e.g., when adding lines above w_topline). But it's slow when
+ * making many changes, postpone until redrawing. */
+ diff_need_scrollbind = TRUE;
+ }
+}
+
+/*
+ * Allocate a new diff block and link it between "dprev" and "dp".
+ */
+static diff_T * diff_alloc_new(tp, dprev, dp)
+tabpage_T *tp;
+diff_T *dprev;
+diff_T *dp;
+{
+ diff_T *dnew;
+
+ dnew = (diff_T *)alloc((unsigned)sizeof(diff_T));
+ if (dnew != NULL) {
+ dnew->df_next = dp;
+ if (dprev == NULL)
+ tp->tp_first_diff = dnew;
+ else
+ dprev->df_next = dnew;
+ }
+ return dnew;
+}
+
+/*
+ * Check if the diff block "dp" can be made smaller for lines at the start and
+ * end that are equal. Called after inserting lines.
+ * This may result in a change where all buffers have zero lines, the caller
+ * must take care of removing it.
+ */
+static void diff_check_unchanged(tp, dp)
+tabpage_T *tp;
+diff_T *dp;
+{
+ int i_org;
+ int i_new;
+ int off_org, off_new;
+ char_u *line_org;
+ int dir = FORWARD;
+
+ /* Find the first buffers, use it as the original, compare the other
+ * buffer lines against this one. */
+ for (i_org = 0; i_org < DB_COUNT; ++i_org)
+ if (tp->tp_diffbuf[i_org] != NULL)
+ break;
+ if (i_org == DB_COUNT) /* safety check */
+ return;
+
+ if (diff_check_sanity(tp, dp) == FAIL)
+ return;
+
+ /* First check lines at the top, then at the bottom. */
+ off_org = 0;
+ off_new = 0;
+ for (;; ) {
+ /* Repeat until a line is found which is different or the number of
+ * lines has become zero. */
+ while (dp->df_count[i_org] > 0) {
+ /* Copy the line, the next ml_get() will invalidate it. */
+ if (dir == BACKWARD)
+ off_org = dp->df_count[i_org] - 1;
+ line_org = vim_strsave(ml_get_buf(tp->tp_diffbuf[i_org],
+ dp->df_lnum[i_org] + off_org, FALSE));
+ if (line_org == NULL)
+ return;
+ for (i_new = i_org + 1; i_new < DB_COUNT; ++i_new) {
+ if (tp->tp_diffbuf[i_new] == NULL)
+ continue;
+ if (dir == BACKWARD)
+ off_new = dp->df_count[i_new] - 1;
+ /* if other buffer doesn't have this line, it was inserted */
+ if (off_new < 0 || off_new >= dp->df_count[i_new])
+ break;
+ if (diff_cmp(line_org, ml_get_buf(tp->tp_diffbuf[i_new],
+ dp->df_lnum[i_new] + off_new, FALSE)) != 0)
+ break;
+ }
+ vim_free(line_org);
+
+ /* Stop when a line isn't equal in all diff buffers. */
+ if (i_new != DB_COUNT)
+ break;
+
+ /* Line matched in all buffers, remove it from the diff. */
+ for (i_new = i_org; i_new < DB_COUNT; ++i_new)
+ if (tp->tp_diffbuf[i_new] != NULL) {
+ if (dir == FORWARD)
+ ++dp->df_lnum[i_new];
+ --dp->df_count[i_new];
+ }
+ }
+ if (dir == BACKWARD)
+ break;
+ dir = BACKWARD;
+ }
+}
+
+/*
+ * Check if a diff block doesn't contain invalid line numbers.
+ * This can happen when the diff program returns invalid results.
+ */
+static int diff_check_sanity(tp, dp)
+tabpage_T *tp;
+diff_T *dp;
+{
+ int i;
+
+ for (i = 0; i < DB_COUNT; ++i)
+ if (tp->tp_diffbuf[i] != NULL)
+ if (dp->df_lnum[i] + dp->df_count[i] - 1
+ > tp->tp_diffbuf[i]->b_ml.ml_line_count)
+ return FAIL;
+ return OK;
+}
+
+/*
+ * Mark all diff buffers in the current tab page for redraw.
+ */
+static void diff_redraw(dofold)
+int dofold; /* also recompute the folds */
+{
+ win_T *wp;
+ int n;
+
+ for (wp = firstwin; wp != NULL; wp = wp->w_next)
+ if (wp->w_p_diff) {
+ redraw_win_later(wp, SOME_VALID);
+ if (dofold && foldmethodIsDiff(wp))
+ foldUpdateAll(wp);
+ /* A change may have made filler lines invalid, need to take care
+ * of that for other windows. */
+ n = diff_check(wp, wp->w_topline);
+ if ((wp != curwin && wp->w_topfill > 0) || n > 0) {
+ if (wp->w_topfill > n)
+ wp->w_topfill = (n < 0 ? 0 : n);
+ else if (n > 0 && n > wp->w_topfill)
+ wp->w_topfill = n;
+ }
+ }
+}
+
+/*
+ * Write buffer "buf" to file "name".
+ * Always use 'fileformat' set to "unix".
+ * Return FAIL for failure
+ */
+static int diff_write(buf, fname)
+buf_T *buf;
+char_u *fname;
+{
+ int r;
+ char_u *save_ff;
+
+ save_ff = buf->b_p_ff;
+ buf->b_p_ff = vim_strsave((char_u *)FF_UNIX);
+ r = buf_write(buf, fname, NULL, (linenr_T)1, buf->b_ml.ml_line_count,
+ NULL, FALSE, FALSE, FALSE, TRUE);
+ free_string_option(buf->b_p_ff);
+ buf->b_p_ff = save_ff;
+ return r;
+}
+
+/*
+ * Completely update the diffs for the buffers involved.
+ * This uses the ordinary "diff" command.
+ * The buffers are written to a file, also for unmodified buffers (the file
+ * could have been produced by autocommands, e.g. the netrw plugin).
+ */
+void ex_diffupdate(eap)
+exarg_T *eap UNUSED; /* can be NULL */
+{
+ buf_T *buf;
+ int idx_orig;
+ int idx_new;
+ char_u *tmp_orig;
+ char_u *tmp_new;
+ char_u *tmp_diff;
+ FILE *fd;
+ int ok;
+ int io_error = FALSE;
+
+ /* Delete all diffblocks. */
+ diff_clear(curtab);
+ curtab->tp_diff_invalid = FALSE;
+
+ /* Use the first buffer as the original text. */
+ for (idx_orig = 0; idx_orig < DB_COUNT; ++idx_orig)
+ if (curtab->tp_diffbuf[idx_orig] != NULL)
+ break;
+ if (idx_orig == DB_COUNT)
+ return;
+
+ /* Only need to do something when there is another buffer. */
+ for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
+ if (curtab->tp_diffbuf[idx_new] != NULL)
+ break;
+ if (idx_new == DB_COUNT)
+ return;
+
+ /* We need three temp file names. */
+ tmp_orig = vim_tempname('o');
+ tmp_new = vim_tempname('n');
+ tmp_diff = vim_tempname('d');
+ if (tmp_orig == NULL || tmp_new == NULL || tmp_diff == NULL)
+ goto theend;
+
+ /*
+ * Do a quick test if "diff" really works. Otherwise it looks like there
+ * 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.
+ */
+ for (;; ) {
+ ok = FALSE;
+ fd = mch_fopen((char *)tmp_orig, "w");
+ if (fd == NULL)
+ io_error = TRUE;
+ else {
+ if (fwrite("line1\n", (size_t)6, (size_t)1, fd) != 1)
+ io_error = TRUE;
+ fclose(fd);
+ fd = mch_fopen((char *)tmp_new, "w");
+ if (fd == NULL)
+ io_error = TRUE;
+ else {
+ if (fwrite("line2\n", (size_t)6, (size_t)1, fd) != 1)
+ io_error = TRUE;
+ fclose(fd);
+ diff_file(tmp_orig, tmp_new, tmp_diff);
+ fd = mch_fopen((char *)tmp_diff, "r");
+ if (fd == NULL)
+ io_error = TRUE;
+ else {
+ char_u linebuf[LBUFLEN];
+
+ for (;; ) {
+ /* There must be a line that contains "1c1". */
+ if (tag_fgets(linebuf, LBUFLEN, fd))
+ break;
+ if (STRNCMP(linebuf, "1c1", 3) == 0)
+ ok = TRUE;
+ }
+ fclose(fd);
+ }
+ mch_remove(tmp_diff);
+ mch_remove(tmp_new);
+ }
+ mch_remove(tmp_orig);
+ }
+
+ /* When using 'diffexpr' break here. */
+ if (*p_dex != NUL)
+ break;
+
+
+ /* If we checked if "-a" works already, break here. */
+ if (diff_a_works != MAYBE)
+ break;
+ diff_a_works = ok;
+
+ /* If "-a" works break here, otherwise retry without "-a". */
+ if (ok)
+ break;
+ }
+ if (!ok) {
+ if (io_error)
+ EMSG(_("E810: Cannot read or write temp files"));
+ EMSG(_("E97: Cannot create diffs"));
+ diff_a_works = MAYBE;
+ goto theend;
+ }
+
+ /* :diffupdate! */
+ if (eap != NULL && eap->forceit)
+ for (idx_new = idx_orig; idx_new < DB_COUNT; ++idx_new) {
+ buf = curtab->tp_diffbuf[idx_new];
+ if (buf_valid(buf))
+ buf_check_timestamp(buf, FALSE);
+ }
+
+ /* Write the first buffer to a tempfile. */
+ buf = curtab->tp_diffbuf[idx_orig];
+ if (diff_write(buf, 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 = curtab->tp_diffbuf[idx_new];
+ if (buf == NULL)
+ continue;
+ if (diff_write(buf, 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);
+ mch_remove(tmp_diff);
+ mch_remove(tmp_new);
+ }
+ mch_remove(tmp_orig);
+
+ /* force updating cursor position on screen */
+ curwin->w_valid_cursor.lnum = 0;
+
+ diff_redraw(TRUE);
+
+theend:
+ vim_free(tmp_orig);
+ vim_free(tmp_new);
+ vim_free(tmp_diff);
+}
+
+/*
+ * Make a diff between files "tmp_orig" and "tmp_new", results in "tmp_diff".
+ */
+static void diff_file(tmp_orig, tmp_new, tmp_diff)
+char_u *tmp_orig;
+char_u *tmp_new;
+char_u *tmp_diff;
+{
+ char_u *cmd;
+ size_t len;
+
+ if (*p_dex != NUL)
+ /* Use 'diffexpr' to generate the diff file. */
+ eval_diff(tmp_orig, tmp_new, tmp_diff);
+ else {
+ len = STRLEN(tmp_orig) + STRLEN(tmp_new)
+ + STRLEN(tmp_diff) + STRLEN(p_srr) + 27;
+ cmd = alloc((unsigned)len);
+ if (cmd != NULL) {
+ /* We don't want $DIFF_OPTIONS to get in the way. */
+ if (getenv("DIFF_OPTIONS"))
+ vim_setenv((char_u *)"DIFF_OPTIONS", (char_u *)"");
+
+ /* 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 ",
+ "",
+ (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, SHELL_FILTER|SHELL_SILENT|SHELL_DOOUT);
+ unblock_autocmds();
+ vim_free(cmd);
+ }
+ }
+}
+
+/*
+ * Create a new version of a file from the current buffer and a diff file.
+ * The buffer is written to a file, also for unmodified buffers (the file
+ * could have been produced by autocommands, e.g. the netrw plugin).
+ */
+void ex_diffpatch(eap)
+exarg_T *eap;
+{
+ char_u *tmp_orig; /* name of original temp file */
+ char_u *tmp_new; /* name of patched temp file */
+ char_u *buf = NULL;
+ size_t buflen;
+ win_T *old_curwin = curwin;
+ char_u *newname = NULL; /* name of patched file buffer */
+#ifdef UNIX
+ char_u dirbuf[MAXPATHL];
+ char_u *fullname = NULL;
+#endif
+ struct stat st;
+
+
+ /* We need two temp file names. */
+ tmp_orig = vim_tempname('o');
+ tmp_new = vim_tempname('n');
+ if (tmp_orig == NULL || tmp_new == NULL)
+ goto theend;
+
+ /* Write the current buffer to "tmp_orig". */
+ if (buf_write(curbuf, tmp_orig, NULL,
+ (linenr_T)1, curbuf->b_ml.ml_line_count,
+ NULL, FALSE, FALSE, FALSE, TRUE) == FAIL)
+ goto theend;
+
+#ifdef UNIX
+ /* Get the absolute path of the patchfile, changing directory below. */
+ fullname = FullName_save(eap->arg, FALSE);
+#endif
+ buflen = STRLEN(tmp_orig) + (
+# ifdef UNIX
+ fullname != NULL ? STRLEN(fullname) :
+# endif
+ STRLEN(eap->arg)) + STRLEN(tmp_new) + 16;
+ buf = alloc((unsigned)buflen);
+ if (buf == NULL)
+ goto theend;
+
+#ifdef UNIX
+ /* Temporarily chdir to /tmp, to avoid patching files in the current
+ * directory when the patch file contains more than one patch. When we
+ * have our own temp dir use that instead, it will be cleaned up when we
+ * exit (any .rej files created). Don't change directory if we can't
+ * return to the current. */
+ if (mch_dirname(dirbuf, MAXPATHL) != OK || mch_chdir((char *)dirbuf) != 0)
+ dirbuf[0] = NUL;
+ else {
+# ifdef TEMPDIRNAMES
+ if (vim_tempdir != NULL)
+ ignored = mch_chdir((char *)vim_tempdir);
+ else
+# endif
+ ignored = mch_chdir("/tmp");
+ shorten_fnames(TRUE);
+ }
+#endif
+
+ if (*p_pex != NUL)
+ /* Use 'patchexpr' to generate the new file. */
+ eval_patch(tmp_orig,
+# ifdef UNIX
+ fullname != NULL ? fullname :
+# endif
+ eap->arg, tmp_new);
+ else {
+ /* Build the patch command and execute it. Ignore errors. Switch to
+ * cooked mode to allow the user to respond to prompts. */
+ vim_snprintf((char *)buf, buflen, "patch -o %s %s < \"%s\"",
+ tmp_new, tmp_orig,
+# ifdef UNIX
+ fullname != NULL ? fullname :
+# endif
+ eap->arg);
+ block_autocmds(); /* Avoid ShellCmdPost stuff */
+ (void)call_shell(buf, SHELL_FILTER | SHELL_COOKED);
+ unblock_autocmds();
+ }
+
+#ifdef UNIX
+ if (dirbuf[0] != NUL) {
+ if (mch_chdir((char *)dirbuf) != 0)
+ EMSG(_(e_prev_dir));
+ shorten_fnames(TRUE);
+ }
+#endif
+
+ /* patch probably has written over the screen */
+ redraw_later(CLEAR);
+
+ /* Delete any .orig or .rej file created. */
+ STRCPY(buf, tmp_new);
+ STRCAT(buf, ".orig");
+ mch_remove(buf);
+ STRCPY(buf, tmp_new);
+ STRCAT(buf, ".rej");
+ mch_remove(buf);
+
+ /* Only continue if the output file was created. */
+ if (mch_stat((char *)tmp_new, &st) < 0 || st.st_size == 0)
+ EMSG(_("E816: Cannot read patch output"));
+ else {
+ if (curbuf->b_fname != NULL) {
+ newname = vim_strnsave(curbuf->b_fname,
+ (int)(STRLEN(curbuf->b_fname) + 4));
+ if (newname != NULL)
+ STRCAT(newname, ".new");
+ }
+
+ /* don't use a new tab page, each tab page has its own diffs */
+ cmdmod.tab = 0;
+
+ if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL) {
+ /* Pretend it was a ":split fname" command */
+ eap->cmdidx = CMD_split;
+ eap->arg = tmp_new;
+ do_exedit(eap, old_curwin);
+
+ /* check that split worked and editing tmp_new */
+ if (curwin != old_curwin && win_valid(old_curwin)) {
+ /* Set 'diff', 'scrollbind' on and 'wrap' off. */
+ diff_win_options(curwin, TRUE);
+ diff_win_options(old_curwin, TRUE);
+
+ if (newname != NULL) {
+ /* do a ":file filename.new" on the patched buffer */
+ eap->arg = newname;
+ ex_file(eap);
+
+ /* Do filetype detection with the new name. */
+ if (au_has_group((char_u *)"filetypedetect"))
+ do_cmdline_cmd((char_u *)":doau filetypedetect BufRead");
+ }
+ }
+ }
+ }
+
+theend:
+ if (tmp_orig != NULL)
+ mch_remove(tmp_orig);
+ vim_free(tmp_orig);
+ if (tmp_new != NULL)
+ mch_remove(tmp_new);
+ vim_free(tmp_new);
+ vim_free(newname);
+ vim_free(buf);
+#ifdef UNIX
+ vim_free(fullname);
+#endif
+}
+
+/*
+ * Split the window and edit another file, setting options to show the diffs.
+ */
+void ex_diffsplit(eap)
+exarg_T *eap;
+{
+ win_T *old_curwin = curwin;
+
+ /* don't use a new tab page, each tab page has its own diffs */
+ cmdmod.tab = 0;
+
+ if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL) {
+ /* Pretend it was a ":split fname" command */
+ eap->cmdidx = CMD_split;
+ curwin->w_p_diff = TRUE;
+ do_exedit(eap, old_curwin);
+
+ if (curwin != old_curwin) { /* split must have worked */
+ /* Set 'diff', 'scrollbind' on and 'wrap' off. */
+ diff_win_options(curwin, TRUE);
+ diff_win_options(old_curwin, TRUE);
+ }
+ }
+}
+
+/*
+ * Set options to show diffs for the current window.
+ */
+void ex_diffthis(eap)
+exarg_T *eap UNUSED;
+{
+ /* Set 'diff', 'scrollbind' on and 'wrap' off. */
+ diff_win_options(curwin, TRUE);
+}
+
+/*
+ * Set options in window "wp" for diff mode.
+ */
+void diff_win_options(wp, addbuf)
+win_T *wp;
+int addbuf; /* Add buffer to diff. */
+{
+ win_T *old_curwin = curwin;
+
+ /* close the manually opened folds */
+ curwin = wp;
+ newFoldLevel();
+ curwin = old_curwin;
+
+ wp->w_p_diff = TRUE;
+
+ /* Use 'scrollbind' and 'cursorbind' when available */
+ if (!wp->w_p_diff_saved)
+ wp->w_p_scb_save = wp->w_p_scb;
+ wp->w_p_scb = TRUE;
+ if (!wp->w_p_diff_saved)
+ wp->w_p_crb_save = wp->w_p_crb;
+ wp->w_p_crb = TRUE;
+ if (!wp->w_p_diff_saved)
+ 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)
+ wp->w_p_fdm_save = vim_strsave(wp->w_p_fdm);
+ set_string_option_direct((char_u *)"fdm", -1, (char_u *)"diff",
+ OPT_LOCAL|OPT_FREE, 0);
+ curwin = old_curwin;
+ curbuf = curwin->w_buffer;
+ if (!wp->w_p_diff_saved) {
+ 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;
+ }
+ wp->w_p_fdc = diff_foldcolumn;
+ wp->w_p_fen = TRUE;
+ wp->w_p_fdl = 0;
+ foldUpdateAll(wp);
+ /* make sure topline is not halfway a fold */
+ changed_window_setting_win(wp);
+ if (vim_strchr(p_sbo, 'h') == NULL)
+ do_cmdline_cmd((char_u *)"set sbo+=hor");
+ /* Saved the current values, to be restored in ex_diffoff(). */
+ wp->w_p_diff_saved = TRUE;
+
+ if (addbuf)
+ diff_buf_add(wp->w_buffer);
+ redraw_win_later(wp, NOT_VALID);
+}
+
+/*
+ * Set options not to show diffs. For the current window or all windows.
+ * Only in the current tab page.
+ */
+void ex_diffoff(eap)
+exarg_T *eap;
+{
+ win_T *wp;
+ win_T *old_curwin = curwin;
+ int diffwin = FALSE;
+
+ for (wp = firstwin; wp != NULL; wp = wp->w_next) {
+ 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;
+ 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_fdc == diff_foldcolumn)
+ wp->w_p_fdc = wp->w_p_diff_saved ? wp->w_p_fdc_save : 0;
+ if (wp->w_p_fdl == 0 && wp->w_p_diff_saved)
+ wp->w_p_fdl = wp->w_p_fdl_save;
+
+ if (wp->w_p_fen) {
+ /* 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;
+ }
+
+ foldUpdateAll(wp);
+ /* make sure topline is not halfway 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;
+ }
+
+ /* Remove "hor" from from 'scrollopt' if there are no diff windows left. */
+ if (!diffwin && vim_strchr(p_sbo, 'h') != NULL)
+ do_cmdline_cmd((char_u *)"set sbo-=hor");
+}
+
+/*
+ * Read the diff output and add each entry to the diff list.
+ */
+static void diff_read(idx_orig, idx_new, fname)
+int idx_orig; /* idx of original file */
+int idx_new; /* idx of new file */
+char_u *fname; /* name of diff output file */
+{
+ FILE *fd;
+ diff_T *dprev = NULL;
+ di