~ubuntu-branches/debian/sid/neovim/sid

« back to all changes in this revision

Viewing changes to src/nvim/diff.c

  • Committer: Package Import Robot
  • Author(s): James McCoy
  • Date: 2016-04-18 21:42:19 UTC
  • mfrom: (1.1.2)
  • Revision ID: package-import@ubuntu.com-20160418214219-1e6d4o1fwqarzk46
Tags: 0.1.3-1
* New upstream release.  (Closes: #820562)
* debian/control:
  + Remove unnecessary luarocks Build-Depends
  + Add libkvm-dev Build-Depends for kfreebsd-*
  + Add python(3)-neovim to Recommends.  (Closes: #812737)
  + Declare compiance with policy 3.9.8, no changes needed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
763
763
  // Make a difference between the first buffer and every other.
764
764
  for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new) {
765
765
    buf_T *buf = curtab->tp_diffbuf[idx_new];
766
 
    if (buf == NULL) {
767
 
      continue;
 
766
    if (buf == NULL || buf->b_ml.ml_mfp == NULL) {
 
767
      continue;  // skip buffer that isn't loaded
768
768
    }
769
769
 
770
770
    if (diff_write(buf, tmp_new) == FAIL) {
819
819
                 (diff_flags & DIFF_IWHITE) ? "-b " : "",
820
820
                 (diff_flags & DIFF_ICASE) ? "-i " : "",
821
821
                 tmp_orig, tmp_new);
822
 
    append_redir(cmd, (int)len, p_srr, tmp_diff);
823
 
    block_autocmds(); /* Avoid ShellCmdPost stuff */
 
822
    append_redir(cmd, len, p_srr, tmp_diff);
 
823
    block_autocmds();  // Avoid ShellCmdPost stuff
824
824
    (void)call_shell(
825
825
        cmd,
826
826
        kShellOptFilter | kShellOptSilent | kShellOptDoOut,
1057
1057
  newFoldLevel();
1058
1058
  curwin = old_curwin;
1059
1059
 
1060
 
  wp->w_p_diff = TRUE;
1061
 
 
1062
1060
  // Use 'scrollbind' and 'cursorbind' when available
1063
 
  if (!wp->w_p_diff_saved) {
 
1061
  if (!wp->w_p_diff) {
1064
1062
    wp->w_p_scb_save = wp->w_p_scb;
1065
1063
  }
1066
1064
  wp->w_p_scb = TRUE;
1067
1065
 
1068
 
  if (!wp->w_p_diff_saved) {
 
1066
  if (!wp->w_p_diff) {
1069
1067
    wp->w_p_crb_save = wp->w_p_crb;
1070
1068
  }
1071
1069
  wp->w_p_crb = TRUE;
1072
1070
 
1073
 
  if (!wp->w_p_diff_saved) {
 
1071
  if (!wp->w_p_diff) {
1074
1072
    wp->w_p_wrap_save = wp->w_p_wrap;
1075
1073
  }
1076
1074
  wp->w_p_wrap = FALSE;
1077
1075
  curwin = wp;
1078
1076
  curbuf = curwin->w_buffer;
1079
1077
 
1080
 
  if (!wp->w_p_diff_saved) {
 
1078
  if (!wp->w_p_diff) {
 
1079
    if (wp->w_p_diff_saved) {
 
1080
      free_string_option(wp->w_p_fdm_save);
 
1081
    }
1081
1082
    wp->w_p_fdm_save = vim_strsave(wp->w_p_fdm);
1082
1083
  }
1083
1084
  set_string_option_direct((char_u *)"fdm", -1, (char_u *)"diff",
1085
1086
  curwin = old_curwin;
1086
1087
  curbuf = curwin->w_buffer;
1087
1088
 
1088
 
  if (!wp->w_p_diff_saved) {
 
1089
  if (!wp->w_p_diff) {
1089
1090
    wp->w_p_fdc_save = wp->w_p_fdc;
1090
1091
    wp->w_p_fen_save = wp->w_p_fen;
1091
1092
    wp->w_p_fdl_save = wp->w_p_fdl;
1104
1105
  // Saved the current values, to be restored in ex_diffoff().
1105
1106
  wp->w_p_diff_saved = TRUE;
1106
1107
 
 
1108
  wp->w_p_diff = true;
 
1109
 
1107
1110
  if (addbuf) {
1108
1111
    diff_buf_add(wp->w_buffer);
1109
1112
  }
1116
1119
/// @param eap
1117
1120
void ex_diffoff(exarg_T *eap)
1118
1121
{
1119
 
  win_T *old_curwin = curwin;
1120
 
  int diffwin = FALSE;
 
1122
  int diffwin = false;
1121
1123
 
1122
1124
  FOR_ALL_WINDOWS_IN_TAB(wp, curtab) {
1123
1125
    if (eap->forceit ? wp->w_p_diff : (wp == curwin)) {
1124
 
      // Set 'diff', 'scrollbind' off and 'wrap' on. If option values
1125
 
      // were saved in diff_win_options() restore them.
1126
 
      wp->w_p_diff = FALSE;
1127
 
 
1128
 
      if (wp->w_p_scb) {
1129
 
        wp->w_p_scb = wp->w_p_diff_saved ? wp->w_p_scb_save : FALSE;
1130
 
      }
1131
 
 
1132
 
      if (wp->w_p_crb) {
1133
 
        wp->w_p_crb = wp->w_p_diff_saved ? wp->w_p_crb_save : FALSE;
1134
 
      }
1135
 
 
1136
 
      if (!wp->w_p_wrap) {
1137
 
        wp->w_p_wrap = wp->w_p_diff_saved ? wp->w_p_wrap_save : TRUE;
1138
 
      }
1139
 
      curwin = wp;
1140
 
      curbuf = curwin->w_buffer;
 
1126
      // Set 'diff' off. If option values were saved in
 
1127
      // diff_win_options(), restore the ones whose settings seem to have
 
1128
      // been left over from diff mode.
 
1129
      wp->w_p_diff = false;
1141
1130
 
1142
1131
      if (wp->w_p_diff_saved) {
 
1132
        if (wp->w_p_scb) {
 
1133
          wp->w_p_scb = wp->w_p_scb_save;
 
1134
        }
 
1135
 
 
1136
        if (wp->w_p_crb) {
 
1137
          wp->w_p_crb = wp->w_p_crb_save;
 
1138
        }
 
1139
 
 
1140
        if (!wp->w_p_wrap) {
 
1141
          wp->w_p_wrap = wp->w_p_wrap_save;
 
1142
        }
 
1143
 
1143
1144
        free_string_option(wp->w_p_fdm);
1144
 
        wp->w_p_fdm = wp->w_p_fdm_save;
1145
 
        wp->w_p_fdm_save = empty_option;
1146
 
      } else {
1147
 
        set_string_option_direct((char_u *)"fdm", -1,
1148
 
                                 (char_u *)"manual", OPT_LOCAL | OPT_FREE, 0);
1149
 
      }
1150
 
      curwin = old_curwin;
1151
 
      curbuf = curwin->w_buffer;
1152
 
 
1153
 
      if (wp->w_p_fdc == diff_foldcolumn) {
1154
 
        wp->w_p_fdc = wp->w_p_diff_saved ? wp->w_p_fdc_save : 0;
1155
 
      }
1156
 
 
1157
 
      if ((wp->w_p_fdl == 0)
1158
 
          && wp->w_p_diff_saved) {
1159
 
        wp->w_p_fdl = wp->w_p_fdl_save;
1160
 
      }
1161
 
 
1162
 
      if (wp->w_p_fen) {
 
1145
        wp->w_p_fdm = vim_strsave(wp->w_p_fdm_save);
 
1146
        if (wp->w_p_fdc == diff_foldcolumn) {
 
1147
          wp->w_p_fdc = wp->w_p_fdc_save;
 
1148
        }
 
1149
        if (wp->w_p_fdl == 0) {
 
1150
          wp->w_p_fdl = wp->w_p_fdl_save;
 
1151
        }
1163
1152
        // Only restore 'foldenable' when 'foldmethod' is not
1164
1153
        // "manual", otherwise we continue to show the diff folds.
1165
 
        if (foldmethodIsManual(wp) || !wp->w_p_diff_saved) {
1166
 
          wp->w_p_fen = FALSE;
1167
 
        } else {
1168
 
          wp->w_p_fen = wp->w_p_fen_save;
 
1154
        if (wp->w_p_fen) {
 
1155
          wp->w_p_fen = foldmethodIsManual(wp) ? false : wp->w_p_fen_save;
1169
1156
        }
 
1157
 
 
1158
        foldUpdateAll(wp);
 
1159
 
 
1160
        // make sure topline is not halfway through a fold
 
1161
        changed_window_setting_win(wp);
1170
1162
      }
1171
1163
 
1172
 
      foldUpdateAll(wp);
1173
 
 
1174
 
      // make sure topline is not halfway through a fold
1175
 
      changed_window_setting_win(wp);
1176
 
 
1177
1164
      // Note: 'sbo' is not restored, it's a global option.
1178
1165
      diff_buf_adjust(wp);
1179
 
 
1180
 
      wp->w_p_diff_saved = FALSE;
1181
1166
    }
1182
1167
    diffwin |= wp->w_p_diff;
1183
1168
  }
1546
1531
  return maxcount - dp->df_count[idx];
1547
1532
}
1548
1533
 
1549
 
/// Compare two entries in diff "*dp" and return TRUE if they are equal.
1550
 
///
1551
 
/// @param dp
1552
 
/// @param idx1 First entry in diff "*dp"
1553
 
/// @param idx2 Second entry in diff "*dp"
1554
 
///
1555
 
/// @return return TRUE if two entires are equal.
1556
 
static int diff_equal_entry(diff_T *dp, int idx1, int idx2)
 
1534
/// Compare two entries in diff "dp" and return true if they are equal.
 
1535
///
 
1536
/// @param  dp    diff
 
1537
/// @param  idx1  first entry in diff "dp"
 
1538
/// @param  idx2  second entry in diff "dp"
 
1539
///
 
1540
/// @return true if two entires are equal.
 
1541
static bool diff_equal_entry(diff_T *dp, int idx1, int idx2)
 
1542
  FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1)
1557
1543
{
1558
1544
  if (dp->df_count[idx1] != dp->df_count[idx2]) {
1559
 
    return FALSE;
 
1545
    return false;
1560
1546
  }
1561
1547
 
1562
1548
  if (diff_check_sanity(curtab, dp) == FAIL) {
1563
 
    return FALSE;
 
1549
    return false;
1564
1550
  }
1565
1551
 
1566
 
  int i;
1567
 
  for (i = 0; i < dp->df_count[idx1]; ++i) {
 
1552
  for (int i = 0; i < dp->df_count[idx1]; i++) {
1568
1553
    char_u *line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1],
1569
 
                                          dp->df_lnum[idx1] + i, FALSE));
 
1554
                                          dp->df_lnum[idx1] + i, false));
1570
1555
 
1571
1556
    int cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2],
1572
 
                                        dp->df_lnum[idx2] + i, FALSE));
 
1557
                                        dp->df_lnum[idx2] + i, false));
1573
1558
    xfree(line);
1574
1559
 
1575
1560
    if (cmp != 0) {
1576
 
      return FALSE;
 
1561
      return false;
1577
1562
    }
1578
1563
  }
1579
 
  return TRUE;
 
1564
  return true;
1580
1565
}
1581
1566
 
1582
1567
/// Compare strings "s1" and "s2" according to 'diffopt'.
1845
1830
  return OK;
1846
1831
}
1847
1832
 
1848
 
/// Return TRUE if 'diffopt' contains "horizontal".
1849
 
///
1850
 
/// @return TRUE if 'diffopt' contains "horizontal"
1851
 
int diffopt_horizontal(void)
 
1833
/// Check that "diffopt" contains "horizontal".
 
1834
bool diffopt_horizontal(void)
 
1835
  FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT
1852
1836
{
1853
1837
  return (diff_flags & DIFF_HORIZONTAL) != 0;
1854
1838
}
1855
1839
 
1856
1840
/// Find the difference within a changed line.
1857
1841
///
1858
 
/// @param startp first char of the change
1859
 
/// @param endp last char of the change
 
1842
/// @param  wp      window whose current buffer to check
 
1843
/// @param  lnum    line number to check within the buffer
 
1844
/// @param  startp  first char of the change
 
1845
/// @param  endp    last char of the change
1860
1846
///
1861
 
/// @returns TRUE if the line was added, no other buffer has it.
1862
 
int diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp)
 
1847
/// @return true if the line was added, no other buffer has it.
 
1848
bool diff_find_change(win_T *wp, linenr_T lnum, int *startp, int *endp)
 
1849
  FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ALL
1863
1850
{
1864
1851
  char_u *line_new;
1865
1852
  int si_org;
1866
1853
  int si_new;
1867
1854
  int ei_org;
1868
1855
  int ei_new;
1869
 
  int added = TRUE;
 
1856
  bool added = true;
1870
1857
 
1871
1858
  // Make a copy of the line, the next ml_get() will invalidate it.
1872
1859
  char_u *line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE));
1875
1862
  if (idx == DB_COUNT) {
1876
1863
    // cannot happen
1877
1864
    xfree(line_org);
1878
 
    return FALSE;
 
1865
    return false;
1879
1866
  }
1880
1867
 
1881
1868
  // search for a change that includes "lnum" in the list of diffblocks.
1888
1875
 
1889
1876
  if ((dp == NULL) || (diff_check_sanity(curtab, dp) == FAIL)) {
1890
1877
    xfree(line_org);
1891
 
    return FALSE;
 
1878
    return false;
1892
1879
  }
1893
1880
 
1894
1881
  int off = lnum - dp->df_lnum[idx];
1899
1886
      if (off >= dp->df_count[i]) {
1900
1887
        continue;
1901
1888
      }
1902
 
      added = FALSE;
 
1889
      added = false;
1903
1890
      line_new = ml_get_buf(curtab->tp_diffbuf[i],
1904
1891
                            dp->df_lnum[i] + off, FALSE);
1905
1892
 
1971
1958
  return added;
1972
1959
}
1973
1960
 
1974
 
/// Return TRUE if line "lnum" is not close to a diff block, this line should
 
1961
/// Check that line "lnum" is not close to a diff block, this line should
1975
1962
/// be in a fold.
1976
1963
///
1977
 
/// @param wp
1978
 
/// @param lnum
 
1964
/// @param  wp    window containing the buffer to check
 
1965
/// @param  lnum  line number to check within the buffer
1979
1966
///
1980
 
/// @return FALSE if there are no diff blocks at all in this window.
1981
 
int diff_infold(win_T *wp, linenr_T lnum)
 
1967
/// @return false if there are no diff blocks at all in this window.
 
1968
bool diff_infold(win_T *wp, linenr_T lnum)
 
1969
  FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1)
1982
1970
{
1983
 
  int other = FALSE;
 
1971
  bool other = false;
1984
1972
  diff_T *dp;
1985
1973
 
1986
1974
  // Return if 'diff' isn't set.
1987
1975
  if (!wp->w_p_diff) {
1988
 
    return FALSE;
 
1976
    return false;
1989
1977
  }
1990
1978
 
1991
1979
  int idx = -1;
1994
1982
    if (curtab->tp_diffbuf[i] == wp->w_buffer) {
1995
1983
      idx = i;
1996
1984
    } else if (curtab->tp_diffbuf[i] != NULL) {
1997
 
      other = TRUE;
 
1985
      other = true;
1998
1986
    }
1999
1987
  }
2000
1988
 
2001
1989
  // return here if there are no diffs in the window
2002
1990
  if ((idx == -1) || !other) {
2003
 
    return FALSE;
 
1991
    return false;
2004
1992
  }
2005
1993
 
2006
1994
  if (curtab->tp_diff_invalid) {
2010
1998
 
2011
1999
  // Return if there are no diff blocks.  All lines will be folded.
2012
2000
  if (curtab->tp_first_diff == NULL) {
2013
 
    return TRUE;
 
2001
    return true;
2014
2002
  }
2015
2003
 
2016
2004
  for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next) {
2021
2009
 
2022
2010
    // If this change ends before the line we have a match.
2023
2011
    if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum) {
2024
 
      return FALSE;
 
2012
      return false;
2025
2013
    }
2026
2014
  }
2027
 
  return TRUE;
 
2015
  return true;
2028
2016
}
2029
2017
 
2030
2018
/// "dp" and "do" commands.
2387
2375
  }
2388
2376
}
2389
2377
 
2390
 
/// Checks if the buffer is in diff-mode.
2391
 
///
2392
 
/// @param buf The buffer to check.
2393
 
///
2394
 
/// @return TRUE if buffer "buf" is in diff-mode.
 
2378
/// Checks that the buffer is in diff-mode.
 
2379
///
 
2380
/// @param  buf  buffer to check.
2395
2381
bool diff_mode_buf(buf_T *buf)
 
2382
  FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FUNC_ATTR_NONNULL_ARG(1)
2396
2383
{
2397
2384
  FOR_ALL_TABS(tp) {
2398
2385
    if (diff_buf_idx_tp(buf, tp) != DB_COUNT) {