blob: 3c72e12bf85c86790b172f35cf0537ee660d4c87 [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +000011 * diff.c: code for diff'ing two, three or four buffers.
Bram Moolenaare828b762018-09-10 17:51:58 +020012 *
13 * There are three ways to diff:
14 * - Shell out to an external diff program, using files.
15 * - Use the compiled-in xdiff library.
16 * - Let 'diffexpr' do the work, using files.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017 */
18
19#include "vim.h"
Bram Moolenaare828b762018-09-10 17:51:58 +020020#include "xdiff/xdiff.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000021
22#if defined(FEAT_DIFF) || defined(PROTO)
23
Bram Moolenaard2b58c02018-09-16 18:10:48 +020024static int diff_busy = FALSE; // using diff structs, don't change them
25static int diff_need_update = FALSE; // ex_diffupdate needs to be called
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
27/* flags obtained from the 'diffopt' option */
Bram Moolenaar785fc652018-09-15 19:17:38 +020028#define DIFF_FILLER 0x001 // display filler lines
29#define DIFF_IBLANK 0x002 // ignore empty lines
30#define DIFF_ICASE 0x004 // ignore case
31#define DIFF_IWHITE 0x008 // ignore change in white space
32#define DIFF_IWHITEALL 0x010 // ignore all white space changes
33#define DIFF_IWHITEEOL 0x020 // ignore change in white space at EOL
34#define DIFF_HORIZONTAL 0x040 // horizontal splits
35#define DIFF_VERTICAL 0x080 // vertical splits
36#define DIFF_HIDDEN_OFF 0x100 // diffoff when hidden
37#define DIFF_INTERNAL 0x200 // use internal xdiff algorithm
38#define ALL_WHITE_DIFF (DIFF_IWHITE | DIFF_IWHITEALL | DIFF_IWHITEEOL)
Bram Moolenaar274cea32018-09-12 18:00:12 +020039static int diff_flags = DIFF_INTERNAL | DIFF_FILLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
Bram Moolenaare828b762018-09-10 17:51:58 +020041static long diff_algorithm = 0;
42
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#define LBUFLEN 50 /* length of line in diff file */
44
45static int diff_a_works = MAYBE; /* TRUE when "diff -a" works, FALSE when it
46 doesn't work, MAYBE when not checked yet */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010047#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static int diff_bin_works = MAYBE; /* TRUE when "diff --binary" works, FALSE
49 when it doesn't work, MAYBE when not
50 checked yet */
51#endif
52
Bram Moolenaare828b762018-09-10 17:51:58 +020053// used for diff input
54typedef struct {
55 char_u *din_fname; // used for external diff
56 mmfile_t din_mmfile; // used for internal diff
57} diffin_T;
58
59// used for diff result
60typedef struct {
61 char_u *dout_fname; // used for external diff
62 garray_T dout_ga; // used for internal diff
63} diffout_T;
64
65// two diff inputs and one result
66typedef struct {
67 diffin_T dio_orig; // original file input
68 diffin_T dio_new; // new file input
69 diffout_T dio_diff; // diff result
70 int dio_internal; // using internal diff
71} diffio_T;
72
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010073static int diff_buf_idx(buf_T *buf);
74static int diff_buf_idx_tp(buf_T *buf, tabpage_T *tp);
75static void diff_mark_adjust_tp(tabpage_T *tp, int idx, linenr_T line1, linenr_T line2, long amount, long amount_after);
76static void diff_check_unchanged(tabpage_T *tp, diff_T *dp);
77static int diff_check_sanity(tabpage_T *tp, diff_T *dp);
78static void diff_redraw(int dofold);
Bram Moolenaare828b762018-09-10 17:51:58 +020079static int check_external_diff(diffio_T *diffio);
80static int diff_file(diffio_T *diffio);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010081static int diff_equal_entry(diff_T *dp, int idx1, int idx2);
82static int diff_cmp(char_u *s1, char_u *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000083#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static void diff_fold_update(diff_T *dp, int skip_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000085#endif
Bram Moolenaare828b762018-09-10 17:51:58 +020086static void diff_read(int idx_orig, int idx_new, diffout_T *fname);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static void diff_copy_entry(diff_T *dprev, diff_T *dp, int idx_orig, int idx_new);
88static diff_T *diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp);
Bram Moolenaare828b762018-09-10 17:51:58 +020089static int parse_diff_ed(char_u *line, linenr_T *lnum_orig, long *count_orig, linenr_T *lnum_new, long *count_new);
90static int parse_diff_unified(char_u *line, linenr_T *lnum_orig, long *count_orig, linenr_T *lnum_new, long *count_new);
91static int xdiff_out(void *priv, mmbuffer_t *mb, int nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
Bram Moolenaar071d4272004-06-13 20:20:40 +000093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 * Called when deleting or unloading a buffer: No longer make a diff with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 */
96 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010097diff_buf_delete(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000098{
99 int i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000100 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101
Bram Moolenaar29323592016-07-24 22:04:11 +0200102 FOR_ALL_TABPAGES(tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000103 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000104 i = diff_buf_idx_tp(buf, tp);
105 if (i != DB_COUNT)
106 {
107 tp->tp_diffbuf[i] = NULL;
108 tp->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000109 if (tp == curtab)
110 diff_redraw(TRUE);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000112 }
113}
114
115/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000116 * Check if the current buffer should be added to or removed from the list of
117 * diff buffers.
118 */
119 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100120diff_buf_adjust(win_T *win)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000121{
122 win_T *wp;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000123 int i;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000124
125 if (!win->w_p_diff)
126 {
127 /* When there is no window showing a diff for this buffer, remove
128 * it from the diffs. */
Bram Moolenaar29323592016-07-24 22:04:11 +0200129 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000130 if (wp->w_buffer == win->w_buffer && wp->w_p_diff)
131 break;
132 if (wp == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000133 {
134 i = diff_buf_idx(win->w_buffer);
135 if (i != DB_COUNT)
136 {
137 curtab->tp_diffbuf[i] = NULL;
138 curtab->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000139 diff_redraw(TRUE);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000140 }
141 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000142 }
143 else
144 diff_buf_add(win->w_buffer);
145}
146
147/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000148 * Add a buffer to make diffs for.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000149 * Call this when a new buffer is being edited in the current window where
150 * 'diff' is set.
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +0000151 * Marks the current buffer as being part of the diff and requiring updating.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000152 * This must be done before any autocmd, because a command may use info
153 * about the screen contents.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000154 */
155 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100156diff_buf_add(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157{
158 int i;
159
160 if (diff_buf_idx(buf) != DB_COUNT)
161 return; /* It's already there. */
162
163 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000164 if (curtab->tp_diffbuf[i] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000166 curtab->tp_diffbuf[i] = buf;
167 curtab->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000168 diff_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 return;
170 }
171
Bram Moolenaarb5443cc2019-01-15 20:19:40 +0100172 semsg(_("E96: Cannot diff more than %d buffers"), DB_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173}
174
175/*
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100176 * Remove all buffers to make diffs for.
177 */
178 static void
179diff_buf_clear(void)
180{
181 int i;
182
183 for (i = 0; i < DB_COUNT; ++i)
184 if (curtab->tp_diffbuf[i] != NULL)
185 {
186 curtab->tp_diffbuf[i] = NULL;
187 curtab->tp_diff_invalid = TRUE;
188 diff_redraw(TRUE);
189 }
190}
191
192/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000193 * Find buffer "buf" in the list of diff buffers for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194 * Return its index or DB_COUNT if not found.
195 */
196 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100197diff_buf_idx(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198{
199 int idx;
200
201 for (idx = 0; idx < DB_COUNT; ++idx)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000202 if (curtab->tp_diffbuf[idx] == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203 break;
204 return idx;
205}
206
207/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000208 * Find buffer "buf" in the list of diff buffers for tab page "tp".
209 * Return its index or DB_COUNT if not found.
210 */
211 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100212diff_buf_idx_tp(buf_T *buf, tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000213{
214 int idx;
215
216 for (idx = 0; idx < DB_COUNT; ++idx)
217 if (tp->tp_diffbuf[idx] == buf)
218 break;
219 return idx;
220}
221
222/*
223 * Mark the diff info involving buffer "buf" as invalid, it will be updated
224 * when info is requested.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000225 */
226 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100227diff_invalidate(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000229 tabpage_T *tp;
230 int i;
231
Bram Moolenaar29323592016-07-24 22:04:11 +0200232 FOR_ALL_TABPAGES(tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000234 i = diff_buf_idx_tp(buf, tp);
235 if (i != DB_COUNT)
236 {
237 tp->tp_diff_invalid = TRUE;
238 if (tp == curtab)
239 diff_redraw(TRUE);
240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 }
242}
243
244/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000245 * Called by mark_adjust(): update line numbers in "curbuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000246 */
247 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100248diff_mark_adjust(
249 linenr_T line1,
250 linenr_T line2,
251 long amount,
252 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000254 int idx;
255 tabpage_T *tp;
256
257 /* Handle all tab pages that use the current buffer in a diff. */
Bram Moolenaar29323592016-07-24 22:04:11 +0200258 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000259 {
260 idx = diff_buf_idx_tp(curbuf, tp);
261 if (idx != DB_COUNT)
262 diff_mark_adjust_tp(tp, idx, line1, line2, amount, amount_after);
263 }
264}
265
266/*
267 * Update line numbers in tab page "tp" for "curbuf" with index "idx".
268 * This attempts to update the changes as much as possible:
269 * When inserting/deleting lines outside of existing change blocks, create a
270 * new change block and update the line numbers in following blocks.
271 * When inserting/deleting lines in existing change blocks, update them.
272 */
273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100274diff_mark_adjust_tp(
275 tabpage_T *tp,
276 int idx,
277 linenr_T line1,
278 linenr_T line2,
279 long amount,
280 long amount_after)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000281{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 diff_T *dp;
283 diff_T *dprev;
284 diff_T *dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 int i;
286 int inserted, deleted;
287 int n, off;
288 linenr_T last;
289 linenr_T lnum_deleted = line1; /* lnum of remaining deletion */
290 int check_unchanged;
291
Bram Moolenaare3521d92018-09-16 14:10:31 +0200292 if (diff_internal())
293 {
Bram Moolenaar198fa062018-09-18 21:20:26 +0200294 // Will update diffs before redrawing. Set _invalid to update the
Bram Moolenaare3521d92018-09-16 14:10:31 +0200295 // diffs themselves, set _update to also update folds properly just
296 // before redrawing.
Bram Moolenaar5f57bdc2018-10-25 17:52:23 +0200297 // Do update marks here, it is needed for :%diffput.
Bram Moolenaare3521d92018-09-16 14:10:31 +0200298 tp->tp_diff_invalid = TRUE;
299 tp->tp_diff_update = TRUE;
Bram Moolenaare3521d92018-09-16 14:10:31 +0200300 }
301
Bram Moolenaar071d4272004-06-13 20:20:40 +0000302 if (line2 == MAXLNUM)
303 {
304 /* mark_adjust(99, MAXLNUM, 9, 0): insert lines */
305 inserted = amount;
306 deleted = 0;
307 }
308 else if (amount_after > 0)
309 {
310 /* mark_adjust(99, 98, MAXLNUM, 9): a change that inserts lines*/
311 inserted = amount_after;
312 deleted = 0;
313 }
314 else
315 {
316 /* mark_adjust(98, 99, MAXLNUM, -2): delete lines */
317 inserted = 0;
318 deleted = -amount_after;
319 }
320
321 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000322 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 for (;;)
324 {
325 /* If the change is after the previous diff block and before the next
326 * diff block, thus not touching an existing change, create a new diff
327 * block. Don't do this when ex_diffgetput() is busy. */
328 if ((dp == NULL || dp->df_lnum[idx] - 1 > line2
329 || (line2 == MAXLNUM && dp->df_lnum[idx] > line1))
330 && (dprev == NULL
331 || dprev->df_lnum[idx] + dprev->df_count[idx] < line1)
332 && !diff_busy)
333 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000334 dnext = diff_alloc_new(tp, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 if (dnext == NULL)
336 return;
337
338 dnext->df_lnum[idx] = line1;
339 dnext->df_count[idx] = inserted;
340 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000341 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342 {
343 if (dprev == NULL)
344 dnext->df_lnum[i] = line1;
345 else
346 dnext->df_lnum[i] = line1
347 + (dprev->df_lnum[i] + dprev->df_count[i])
348 - (dprev->df_lnum[idx] + dprev->df_count[idx]);
349 dnext->df_count[i] = deleted;
350 }
351 }
352
353 /* if at end of the list, quit */
354 if (dp == NULL)
355 break;
356
357 /*
358 * Check for these situations:
359 * 1 2 3
360 * 1 2 3
361 * line1 2 3 4 5
362 * 2 3 4 5
363 * 2 3 4 5
364 * line2 2 3 4 5
365 * 3 5 6
366 * 3 5 6
367 */
368 /* compute last line of this change */
369 last = dp->df_lnum[idx] + dp->df_count[idx] - 1;
370
371 /* 1. change completely above line1: nothing to do */
372 if (last >= line1 - 1)
373 {
374 /* 6. change below line2: only adjust for amount_after; also when
375 * "deleted" became zero when deleted all lines between two diffs */
376 if (dp->df_lnum[idx] - (deleted + inserted != 0) > line2)
377 {
378 if (amount_after == 0)
379 break; /* nothing left to change */
380 dp->df_lnum[idx] += amount_after;
381 }
382 else
383 {
384 check_unchanged = FALSE;
385
386 /* 2. 3. 4. 5.: inserted/deleted lines touching this diff. */
387 if (deleted > 0)
388 {
389 if (dp->df_lnum[idx] >= line1)
390 {
391 off = dp->df_lnum[idx] - lnum_deleted;
392 if (last <= line2)
393 {
394 /* 4. delete all lines of diff */
395 if (dp->df_next != NULL
396 && dp->df_next->df_lnum[idx] - 1 <= line2)
397 {
398 /* delete continues in next diff, only do
399 * lines until that one */
400 n = dp->df_next->df_lnum[idx] - lnum_deleted;
401 deleted -= n;
402 n -= dp->df_count[idx];
403 lnum_deleted = dp->df_next->df_lnum[idx];
404 }
405 else
406 n = deleted - dp->df_count[idx];
407 dp->df_count[idx] = 0;
408 }
409 else
410 {
411 /* 5. delete lines at or just before top of diff */
412 n = off;
413 dp->df_count[idx] -= line2 - dp->df_lnum[idx] + 1;
414 check_unchanged = TRUE;
415 }
416 dp->df_lnum[idx] = line1;
417 }
418 else
419 {
420 off = 0;
421 if (last < line2)
422 {
423 /* 2. delete at end of of diff */
424 dp->df_count[idx] -= last - lnum_deleted + 1;
425 if (dp->df_next != NULL
426 && dp->df_next->df_lnum[idx] - 1 <= line2)
427 {
428 /* delete continues in next diff, only do
429 * lines until that one */
430 n = dp->df_next->df_lnum[idx] - 1 - last;
431 deleted -= dp->df_next->df_lnum[idx]
432 - lnum_deleted;
433 lnum_deleted = dp->df_next->df_lnum[idx];
434 }
435 else
436 n = line2 - last;
437 check_unchanged = TRUE;
438 }
439 else
440 {
441 /* 3. delete lines inside the diff */
442 n = 0;
443 dp->df_count[idx] -= deleted;
444 }
445 }
446
447 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000448 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 {
450 dp->df_lnum[i] -= off;
451 dp->df_count[i] += n;
452 }
453 }
454 else
455 {
456 if (dp->df_lnum[idx] <= line1)
457 {
458 /* inserted lines somewhere in this diff */
459 dp->df_count[idx] += inserted;
460 check_unchanged = TRUE;
461 }
462 else
463 /* inserted lines somewhere above this diff */
464 dp->df_lnum[idx] += inserted;
465 }
466
467 if (check_unchanged)
468 /* Check if inserted lines are equal, may reduce the
469 * size of the diff. TODO: also check for equal lines
470 * in the middle and perhaps split the block. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000471 diff_check_unchanged(tp, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472 }
473 }
474
475 /* check if this block touches the previous one, may merge them. */
476 if (dprev != NULL && dprev->df_lnum[idx] + dprev->df_count[idx]
477 == dp->df_lnum[idx])
478 {
479 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000480 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481 dprev->df_count[i] += dp->df_count[i];
482 dprev->df_next = dp->df_next;
483 vim_free(dp);
484 dp = dprev->df_next;
485 }
486 else
487 {
488 /* Advance to next entry. */
489 dprev = dp;
490 dp = dp->df_next;
491 }
492 }
493
494 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000495 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000496 while (dp != NULL)
497 {
498 /* All counts are zero, remove this entry. */
499 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000500 if (tp->tp_diffbuf[i] != NULL && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000501 break;
502 if (i == DB_COUNT)
503 {
504 dnext = dp->df_next;
505 vim_free(dp);
506 dp = dnext;
507 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000508 tp->tp_first_diff = dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509 else
510 dprev->df_next = dnext;
511 }
512 else
513 {
514 /* Advance to next entry. */
515 dprev = dp;
516 dp = dp->df_next;
517 }
518
519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000521 if (tp == curtab)
522 {
523 diff_redraw(TRUE);
524
525 /* Need to recompute the scroll binding, may remove or add filler
526 * lines (e.g., when adding lines above w_topline). But it's slow when
527 * making many changes, postpone until redrawing. */
528 diff_need_scrollbind = TRUE;
529 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530}
531
532/*
533 * Allocate a new diff block and link it between "dprev" and "dp".
534 */
535 static diff_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100536diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000537{
538 diff_T *dnew;
539
Bram Moolenaar964b3742019-05-24 18:54:09 +0200540 dnew = (diff_T *)alloc(sizeof(diff_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 if (dnew != NULL)
542 {
543 dnew->df_next = dp;
544 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000545 tp->tp_first_diff = dnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000546 else
547 dprev->df_next = dnew;
548 }
549 return dnew;
550}
551
552/*
553 * Check if the diff block "dp" can be made smaller for lines at the start and
554 * end that are equal. Called after inserting lines.
555 * This may result in a change where all buffers have zero lines, the caller
556 * must take care of removing it.
557 */
558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100559diff_check_unchanged(tabpage_T *tp, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560{
561 int i_org;
562 int i_new;
563 int off_org, off_new;
564 char_u *line_org;
565 int dir = FORWARD;
566
567 /* Find the first buffers, use it as the original, compare the other
568 * buffer lines against this one. */
569 for (i_org = 0; i_org < DB_COUNT; ++i_org)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000570 if (tp->tp_diffbuf[i_org] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 break;
572 if (i_org == DB_COUNT) /* safety check */
573 return;
574
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000575 if (diff_check_sanity(tp, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 return;
577
578 /* First check lines at the top, then at the bottom. */
579 off_org = 0;
580 off_new = 0;
581 for (;;)
582 {
583 /* Repeat until a line is found which is different or the number of
584 * lines has become zero. */
585 while (dp->df_count[i_org] > 0)
586 {
587 /* Copy the line, the next ml_get() will invalidate it. */
588 if (dir == BACKWARD)
589 off_org = dp->df_count[i_org] - 1;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000590 line_org = vim_strsave(ml_get_buf(tp->tp_diffbuf[i_org],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591 dp->df_lnum[i_org] + off_org, FALSE));
592 if (line_org == NULL)
593 return;
594 for (i_new = i_org + 1; i_new < DB_COUNT; ++i_new)
595 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000596 if (tp->tp_diffbuf[i_new] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 continue;
598 if (dir == BACKWARD)
599 off_new = dp->df_count[i_new] - 1;
600 /* if other buffer doesn't have this line, it was inserted */
601 if (off_new < 0 || off_new >= dp->df_count[i_new])
602 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000603 if (diff_cmp(line_org, ml_get_buf(tp->tp_diffbuf[i_new],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 dp->df_lnum[i_new] + off_new, FALSE)) != 0)
605 break;
606 }
607 vim_free(line_org);
608
609 /* Stop when a line isn't equal in all diff buffers. */
610 if (i_new != DB_COUNT)
611 break;
612
613 /* Line matched in all buffers, remove it from the diff. */
614 for (i_new = i_org; i_new < DB_COUNT; ++i_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000615 if (tp->tp_diffbuf[i_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 {
617 if (dir == FORWARD)
618 ++dp->df_lnum[i_new];
619 --dp->df_count[i_new];
620 }
621 }
622 if (dir == BACKWARD)
623 break;
624 dir = BACKWARD;
625 }
626}
627
628/*
629 * Check if a diff block doesn't contain invalid line numbers.
630 * This can happen when the diff program returns invalid results.
631 */
632 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100633diff_check_sanity(tabpage_T *tp, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634{
635 int i;
636
637 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000638 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 if (dp->df_lnum[i] + dp->df_count[i] - 1
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000640 > tp->tp_diffbuf[i]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 return FAIL;
642 return OK;
643}
644
645/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000646 * Mark all diff buffers in the current tab page for redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 */
648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100649diff_redraw(
Bram Moolenaare3521d92018-09-16 14:10:31 +0200650 int dofold) // also recompute the folds
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651{
652 win_T *wp;
653 int n;
654
Bram Moolenaar29323592016-07-24 22:04:11 +0200655 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 if (wp->w_p_diff)
657 {
Bram Moolenaar60f83772006-03-12 21:52:47 +0000658 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659#ifdef FEAT_FOLDING
660 if (dofold && foldmethodIsDiff(wp))
661 foldUpdateAll(wp);
662#endif
663 /* A change may have made filler lines invalid, need to take care
664 * of that for other windows. */
Bram Moolenaara80888d2012-10-21 22:18:21 +0200665 n = diff_check(wp, wp->w_topline);
666 if ((wp != curwin && wp->w_topfill > 0) || n > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 if (wp->w_topfill > n)
669 wp->w_topfill = (n < 0 ? 0 : n);
Bram Moolenaara80888d2012-10-21 22:18:21 +0200670 else if (n > 0 && n > wp->w_topfill)
671 wp->w_topfill = n;
Bram Moolenaar846a2ff2014-05-28 11:35:37 +0200672 check_topfill(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673 }
674 }
675}
676
Bram Moolenaare828b762018-09-10 17:51:58 +0200677 static void
678clear_diffin(diffin_T *din)
679{
680 if (din->din_fname == NULL)
681 {
682 vim_free(din->din_mmfile.ptr);
683 din->din_mmfile.ptr = NULL;
684 }
685 else
686 mch_remove(din->din_fname);
687}
688
689 static void
690clear_diffout(diffout_T *dout)
691{
692 if (dout->dout_fname == NULL)
693 ga_clear_strings(&dout->dout_ga);
694 else
695 mch_remove(dout->dout_fname);
696}
697
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698/*
Bram Moolenaare828b762018-09-10 17:51:58 +0200699 * Write buffer "buf" to a memory buffer.
700 * Return FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701 */
702 static int
Bram Moolenaare828b762018-09-10 17:51:58 +0200703diff_write_buffer(buf_T *buf, diffin_T *din)
704{
705 linenr_T lnum;
706 char_u *s;
707 long len = 0;
708 char_u *ptr;
709
710 // xdiff requires one big block of memory with all the text.
711 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar6e272ac2018-09-16 14:51:36 +0200712 len += (long)STRLEN(ml_get_buf(buf, lnum, FALSE)) + 1;
Bram Moolenaare828b762018-09-10 17:51:58 +0200713 ptr = lalloc(len, TRUE);
714 if (ptr == NULL)
715 {
716 // Allocating memory failed. This can happen, because we try to read
717 // the whole buffer text into memory. Set the failed flag, the diff
718 // will be retried with external diff. The flag is never reset.
719 buf->b_diff_failed = TRUE;
720 if (p_verbose > 0)
721 {
722 verbose_enter();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100723 smsg(_("Not enough memory to use internal diff for buffer \"%s\""),
Bram Moolenaare828b762018-09-10 17:51:58 +0200724 buf->b_fname);
725 verbose_leave();
726 }
727 return FAIL;
728 }
729 din->din_mmfile.ptr = (char *)ptr;
730 din->din_mmfile.size = len;
731
732 len = 0;
733 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
734 {
735 for (s = ml_get_buf(buf, lnum, FALSE); *s != NUL; )
736 {
737 if (diff_flags & DIFF_ICASE)
738 {
739 int c;
Bram Moolenaare828b762018-09-10 17:51:58 +0200740 int orig_len;
741 char_u cbuf[MB_MAXBYTES + 1];
742
Bram Moolenaar13505972019-01-24 15:04:48 +0100743 // xdiff doesn't support ignoring case, fold-case the text.
Bram Moolenaare828b762018-09-10 17:51:58 +0200744 c = PTR2CHAR(s);
745 c = enc_utf8 ? utf_fold(c) : MB_TOLOWER(c);
746 orig_len = MB_PTR2LEN(s);
747 if (mb_char2bytes(c, cbuf) != orig_len)
748 // TODO: handle byte length difference
749 mch_memmove(ptr + len, s, orig_len);
750 else
751 mch_memmove(ptr + len, cbuf, orig_len);
752
753 s += orig_len;
754 len += orig_len;
Bram Moolenaare828b762018-09-10 17:51:58 +0200755 }
756 else
757 ptr[len++] = *s++;
758 }
759 ptr[len++] = NL;
760 }
761 return OK;
762}
763
764/*
765 * Write buffer "buf" to file or memory buffer.
766 * Return FAIL for failure.
767 */
768 static int
769diff_write(buf_T *buf, diffin_T *din)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000770{
771 int r;
772 char_u *save_ff;
773
Bram Moolenaare828b762018-09-10 17:51:58 +0200774 if (din->din_fname == NULL)
775 return diff_write_buffer(buf, din);
776
777 // Always use 'fileformat' set to "unix".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 save_ff = buf->b_p_ff;
779 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX);
Bram Moolenaare828b762018-09-10 17:51:58 +0200780 r = buf_write(buf, din->din_fname, NULL,
781 (linenr_T)1, buf->b_ml.ml_line_count,
782 NULL, FALSE, FALSE, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 free_string_option(buf->b_p_ff);
784 buf->b_p_ff = save_ff;
785 return r;
786}
787
788/*
Bram Moolenaare828b762018-09-10 17:51:58 +0200789 * Update the diffs for all buffers involved.
790 */
791 static void
792diff_try_update(
793 diffio_T *dio,
794 int idx_orig,
795 exarg_T *eap) // "eap" can be NULL
796{
797 buf_T *buf;
798 int idx_new;
799
800 if (dio->dio_internal)
801 {
802 ga_init2(&dio->dio_diff.dout_ga, sizeof(char *), 1000);
803 }
804 else
805 {
806 // We need three temp file names.
807 dio->dio_orig.din_fname = vim_tempname('o', TRUE);
808 dio->dio_new.din_fname = vim_tempname('n', TRUE);
809 dio->dio_diff.dout_fname = vim_tempname('d', TRUE);
810 if (dio->dio_orig.din_fname == NULL
811 || dio->dio_new.din_fname == NULL
812 || dio->dio_diff.dout_fname == NULL)
813 goto theend;
814 }
815
816 // Check external diff is actually working.
817 if (!dio->dio_internal && check_external_diff(dio) == FAIL)
818 goto theend;
819
820 // :diffupdate!
821 if (eap != NULL && eap->forceit)
822 for (idx_new = idx_orig; idx_new < DB_COUNT; ++idx_new)
823 {
824 buf = curtab->tp_diffbuf[idx_new];
825 if (buf_valid(buf))
826 buf_check_timestamp(buf, FALSE);
827 }
828
829 // Write the first buffer to a tempfile or mmfile_t.
830 buf = curtab->tp_diffbuf[idx_orig];
831 if (diff_write(buf, &dio->dio_orig) == FAIL)
832 goto theend;
833
834 // Make a difference between the first buffer and every other.
835 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
836 {
837 buf = curtab->tp_diffbuf[idx_new];
838 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
839 continue; // skip buffer that isn't loaded
840
841 // Write the other buffer and diff with the first one.
842 if (diff_write(buf, &dio->dio_new) == FAIL)
843 continue;
844 if (diff_file(dio) == FAIL)
845 continue;
846
847 // Read the diff output and add each entry to the diff list.
848 diff_read(idx_orig, idx_new, &dio->dio_diff);
849
850 clear_diffin(&dio->dio_new);
851 clear_diffout(&dio->dio_diff);
852 }
853 clear_diffin(&dio->dio_orig);
854
855theend:
856 vim_free(dio->dio_orig.din_fname);
857 vim_free(dio->dio_new.din_fname);
858 vim_free(dio->dio_diff.dout_fname);
859}
860
861/*
862 * Return TRUE if the options are set to use the internal diff library.
863 * Note that if the internal diff failed for one of the buffers, the external
864 * diff will be used anyway.
865 */
Bram Moolenaare3521d92018-09-16 14:10:31 +0200866 int
Bram Moolenaare828b762018-09-10 17:51:58 +0200867diff_internal(void)
868{
Bram Moolenaar975880b2019-03-03 14:42:11 +0100869 return (diff_flags & DIFF_INTERNAL) != 0
870#ifdef FEAT_EVAL
871 && *p_dex == NUL
872#endif
873 ;
Bram Moolenaare828b762018-09-10 17:51:58 +0200874}
875
876/*
877 * Return TRUE if the internal diff failed for one of the diff buffers.
878 */
879 static int
880diff_internal_failed(void)
881{
882 int idx;
883
884 // Only need to do something when there is another buffer.
885 for (idx = 0; idx < DB_COUNT; ++idx)
886 if (curtab->tp_diffbuf[idx] != NULL
887 && curtab->tp_diffbuf[idx]->b_diff_failed)
888 return TRUE;
889 return FALSE;
890}
891
892/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 * Completely update the diffs for the buffers involved.
Bram Moolenaare3521d92018-09-16 14:10:31 +0200894 * When using the external "diff" command the buffers are written to a file,
895 * also for unmodified buffers (the file could have been produced by
896 * autocommands, e.g. the netrw plugin).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 void
Bram Moolenaare828b762018-09-10 17:51:58 +0200899ex_diffupdate(exarg_T *eap) // "eap" can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 int idx_orig;
902 int idx_new;
Bram Moolenaare828b762018-09-10 17:51:58 +0200903 diffio_T diffio;
Bram Moolenaar198fa062018-09-18 21:20:26 +0200904 int had_diffs = curtab->tp_first_diff != NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905
Bram Moolenaard2b58c02018-09-16 18:10:48 +0200906 if (diff_busy)
907 {
908 diff_need_update = TRUE;
909 return;
910 }
911
Bram Moolenaare828b762018-09-10 17:51:58 +0200912 // Delete all diffblocks.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000913 diff_clear(curtab);
914 curtab->tp_diff_invalid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000915
Bram Moolenaare828b762018-09-10 17:51:58 +0200916 // Use the first buffer as the original text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000917 for (idx_orig = 0; idx_orig < DB_COUNT; ++idx_orig)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000918 if (curtab->tp_diffbuf[idx_orig] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000919 break;
920 if (idx_orig == DB_COUNT)
Bram Moolenaar198fa062018-09-18 21:20:26 +0200921 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922
Bram Moolenaare828b762018-09-10 17:51:58 +0200923 // Only need to do something when there is another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000924 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000925 if (curtab->tp_diffbuf[idx_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926 break;
927 if (idx_new == DB_COUNT)
Bram Moolenaar198fa062018-09-18 21:20:26 +0200928 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929
Bram Moolenaare828b762018-09-10 17:51:58 +0200930 // Only use the internal method if it did not fail for one of the buffers.
931 vim_memset(&diffio, 0, sizeof(diffio));
932 diffio.dio_internal = diff_internal() && !diff_internal_failed();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933
Bram Moolenaare828b762018-09-10 17:51:58 +0200934 diff_try_update(&diffio, idx_orig, eap);
935 if (diffio.dio_internal && diff_internal_failed())
936 {
937 // Internal diff failed, use external diff instead.
938 vim_memset(&diffio, 0, sizeof(diffio));
939 diff_try_update(&diffio, idx_orig, eap);
940 }
941
942 // force updating cursor position on screen
943 curwin->w_valid_cursor.lnum = 0;
944
Bram Moolenaar198fa062018-09-18 21:20:26 +0200945theend:
946 // A redraw is needed if there were diffs and they were cleared, or there
947 // are diffs now, which means they got updated.
948 if (had_diffs || curtab->tp_first_diff != NULL)
949 {
950 diff_redraw(TRUE);
951 apply_autocmds(EVENT_DIFFUPDATED, NULL, NULL, FALSE, curbuf);
952 }
Bram Moolenaare828b762018-09-10 17:51:58 +0200953}
954
955/*
956 * Do a quick test if "diff" really works. Otherwise it looks like there
957 * are no differences. Can't use the return value, it's non-zero when
958 * there are differences.
959 */
960 static int
961check_external_diff(diffio_T *diffio)
962{
963 FILE *fd;
964 int ok;
965 int io_error = FALSE;
966
967 // May try twice, first with "-a" and then without.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 for (;;)
969 {
970 ok = FALSE;
Bram Moolenaare828b762018-09-10 17:51:58 +0200971 fd = mch_fopen((char *)diffio->dio_orig.din_fname, "w");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000972 if (fd == NULL)
973 io_error = TRUE;
974 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000976 if (fwrite("line1\n", (size_t)6, (size_t)1, fd) != 1)
977 io_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 fclose(fd);
Bram Moolenaare828b762018-09-10 17:51:58 +0200979 fd = mch_fopen((char *)diffio->dio_new.din_fname, "w");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000980 if (fd == NULL)
981 io_error = TRUE;
982 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000984 if (fwrite("line2\n", (size_t)6, (size_t)1, fd) != 1)
985 io_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 fclose(fd);
Bram Moolenaare828b762018-09-10 17:51:58 +0200987 fd = NULL;
988 if (diff_file(diffio) == OK)
989 fd = mch_fopen((char *)diffio->dio_diff.dout_fname, "r");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000990 if (fd == NULL)
991 io_error = TRUE;
992 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 {
994 char_u linebuf[LBUFLEN];
995
996 for (;;)
997 {
998 /* There must be a line that contains "1c1". */
Bram Moolenaar00590742019-02-15 21:06:09 +0100999 if (vim_fgets(linebuf, LBUFLEN, fd))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 break;
1001 if (STRNCMP(linebuf, "1c1", 3) == 0)
1002 ok = TRUE;
1003 }
1004 fclose(fd);
1005 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001006 mch_remove(diffio->dio_diff.dout_fname);
1007 mch_remove(diffio->dio_new.din_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001008 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001009 mch_remove(diffio->dio_orig.din_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 }
1011
1012#ifdef FEAT_EVAL
1013 /* When using 'diffexpr' break here. */
1014 if (*p_dex != NUL)
1015 break;
1016#endif
1017
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001018#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 /* If the "-a" argument works, also check if "--binary" works. */
1020 if (ok && diff_a_works == MAYBE && diff_bin_works == MAYBE)
1021 {
1022 diff_a_works = TRUE;
1023 diff_bin_works = TRUE;
1024 continue;
1025 }
1026 if (!ok && diff_a_works == TRUE && diff_bin_works == TRUE)
1027 {
1028 /* Tried --binary, but it failed. "-a" works though. */
1029 diff_bin_works = FALSE;
1030 ok = TRUE;
1031 }
1032#endif
1033
1034 /* If we checked if "-a" works already, break here. */
1035 if (diff_a_works != MAYBE)
1036 break;
1037 diff_a_works = ok;
1038
1039 /* If "-a" works break here, otherwise retry without "-a". */
1040 if (ok)
1041 break;
1042 }
1043 if (!ok)
1044 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001045 if (io_error)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001046 emsg(_("E810: Cannot read or write temp files"));
1047 emsg(_("E97: Cannot create diffs"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 diff_a_works = MAYBE;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001049#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 diff_bin_works = MAYBE;
1051#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001052 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001054 return OK;
1055}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056
Bram Moolenaare828b762018-09-10 17:51:58 +02001057/*
1058 * Invoke the xdiff function.
1059 */
1060 static int
1061diff_file_internal(diffio_T *diffio)
1062{
1063 xpparam_t param;
1064 xdemitconf_t emit_cfg;
1065 xdemitcb_t emit_cb;
Bram Moolenaarbd1d5602012-05-18 18:47:17 +02001066
Bram Moolenaare828b762018-09-10 17:51:58 +02001067 vim_memset(&param, 0, sizeof(param));
1068 vim_memset(&emit_cfg, 0, sizeof(emit_cfg));
1069 vim_memset(&emit_cb, 0, sizeof(emit_cb));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070
Bram Moolenaare828b762018-09-10 17:51:58 +02001071 param.flags = diff_algorithm;
1072
1073 if (diff_flags & DIFF_IWHITE)
1074 param.flags |= XDF_IGNORE_WHITESPACE_CHANGE;
Bram Moolenaar785fc652018-09-15 19:17:38 +02001075 if (diff_flags & DIFF_IWHITEALL)
1076 param.flags |= XDF_IGNORE_WHITESPACE;
1077 if (diff_flags & DIFF_IWHITEEOL)
1078 param.flags |= XDF_IGNORE_WHITESPACE_AT_EOL;
1079 if (diff_flags & DIFF_IBLANK)
1080 param.flags |= XDF_IGNORE_BLANK_LINES;
Bram Moolenaare828b762018-09-10 17:51:58 +02001081
1082 emit_cfg.ctxlen = 0; // don't need any diff_context here
1083 emit_cb.priv = &diffio->dio_diff;
1084 emit_cb.outf = xdiff_out;
1085 if (xdl_diff(&diffio->dio_orig.din_mmfile,
1086 &diffio->dio_new.din_mmfile,
1087 &param, &emit_cfg, &emit_cb) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001089 emsg(_("E960: Problem creating the internal diff"));
Bram Moolenaare828b762018-09-10 17:51:58 +02001090 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001092 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093}
1094
1095/*
1096 * Make a diff between files "tmp_orig" and "tmp_new", results in "tmp_diff".
Bram Moolenaare828b762018-09-10 17:51:58 +02001097 * return OK or FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 */
Bram Moolenaare828b762018-09-10 17:51:58 +02001099 static int
1100diff_file(diffio_T *dio)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101{
1102 char_u *cmd;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001103 size_t len;
Bram Moolenaare828b762018-09-10 17:51:58 +02001104 char_u *tmp_orig = dio->dio_orig.din_fname;
1105 char_u *tmp_new = dio->dio_new.din_fname;
1106 char_u *tmp_diff = dio->dio_diff.dout_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107
1108#ifdef FEAT_EVAL
1109 if (*p_dex != NUL)
Bram Moolenaare828b762018-09-10 17:51:58 +02001110 {
1111 // Use 'diffexpr' to generate the diff file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 eval_diff(tmp_orig, tmp_new, tmp_diff);
Bram Moolenaare828b762018-09-10 17:51:58 +02001113 return OK;
1114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115 else
1116#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001117 // Use xdiff for generating the diff.
1118 if (dio->dio_internal)
1119 {
1120 return diff_file_internal(dio);
1121 }
1122 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001124 len = STRLEN(tmp_orig) + STRLEN(tmp_new)
1125 + STRLEN(tmp_diff) + STRLEN(p_srr) + 27;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001126 cmd = alloc(len);
Bram Moolenaare828b762018-09-10 17:51:58 +02001127 if (cmd == NULL)
1128 return FAIL;
Bram Moolenaar95fb60a2005-03-08 22:29:20 +00001129
Bram Moolenaare828b762018-09-10 17:51:58 +02001130 // We don't want $DIFF_OPTIONS to get in the way.
1131 if (getenv("DIFF_OPTIONS"))
1132 vim_setenv((char_u *)"DIFF_OPTIONS", (char_u *)"");
1133
1134 // Build the diff command and execute it. Always use -a, binary
1135 // differences are of no use. Ignore errors, diff returns
1136 // non-zero when differences have been found.
Bram Moolenaar785fc652018-09-15 19:17:38 +02001137 vim_snprintf((char *)cmd, len, "diff %s%s%s%s%s%s%s%s %s",
Bram Moolenaare828b762018-09-10 17:51:58 +02001138 diff_a_works == FALSE ? "" : "-a ",
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001139#if defined(MSWIN)
Bram Moolenaare828b762018-09-10 17:51:58 +02001140 diff_bin_works == TRUE ? "--binary " : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001141#else
Bram Moolenaare828b762018-09-10 17:51:58 +02001142 "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001143#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001144 (diff_flags & DIFF_IWHITE) ? "-b " : "",
Bram Moolenaar785fc652018-09-15 19:17:38 +02001145 (diff_flags & DIFF_IWHITEALL) ? "-w " : "",
1146 (diff_flags & DIFF_IWHITEEOL) ? "-Z " : "",
1147 (diff_flags & DIFF_IBLANK) ? "-B " : "",
Bram Moolenaare828b762018-09-10 17:51:58 +02001148 (diff_flags & DIFF_ICASE) ? "-i " : "",
1149 tmp_orig, tmp_new);
1150 append_redir(cmd, (int)len, p_srr, tmp_diff);
1151 block_autocmds(); // avoid ShellCmdPost stuff
1152 (void)call_shell(cmd, SHELL_FILTER|SHELL_SILENT|SHELL_DOOUT);
1153 unblock_autocmds();
1154 vim_free(cmd);
1155 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 }
1157}
1158
1159/*
1160 * Create a new version of a file from the current buffer and a diff file.
1161 * The buffer is written to a file, also for unmodified buffers (the file
1162 * could have been produced by autocommands, e.g. the netrw plugin).
1163 */
1164 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001165ex_diffpatch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166{
1167 char_u *tmp_orig; /* name of original temp file */
1168 char_u *tmp_new; /* name of patched temp file */
1169 char_u *buf = NULL;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001170 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 win_T *old_curwin = curwin;
1172 char_u *newname = NULL; /* name of patched file buffer */
1173#ifdef UNIX
1174 char_u dirbuf[MAXPATHL];
1175 char_u *fullname = NULL;
1176#endif
1177#ifdef FEAT_BROWSE
1178 char_u *browseFile = NULL;
1179 int browse_flag = cmdmod.browse;
1180#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02001181 stat_T st;
Bram Moolenaara95ab322017-03-11 19:21:53 +01001182 char_u *esc_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
1184#ifdef FEAT_BROWSE
1185 if (cmdmod.browse)
1186 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001187 browseFile = do_browse(0, (char_u *)_("Patch file"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02001188 eap->arg, NULL, NULL,
1189 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 if (browseFile == NULL)
1191 return; /* operation cancelled */
1192 eap->arg = browseFile;
1193 cmdmod.browse = FALSE; /* don't let do_ecmd() browse again */
1194 }
1195#endif
1196
1197 /* We need two temp file names. */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001198 tmp_orig = vim_tempname('o', FALSE);
1199 tmp_new = vim_tempname('n', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 if (tmp_orig == NULL || tmp_new == NULL)
1201 goto theend;
1202
1203 /* Write the current buffer to "tmp_orig". */
1204 if (buf_write(curbuf, tmp_orig, NULL,
1205 (linenr_T)1, curbuf->b_ml.ml_line_count,
1206 NULL, FALSE, FALSE, FALSE, TRUE) == FAIL)
1207 goto theend;
1208
1209#ifdef UNIX
1210 /* Get the absolute path of the patchfile, changing directory below. */
1211 fullname = FullName_save(eap->arg, FALSE);
1212#endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001213 esc_name = vim_strsave_shellescape(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214# ifdef UNIX
Bram Moolenaara95ab322017-03-11 19:21:53 +01001215 fullname != NULL ? fullname :
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216# endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001217 eap->arg, TRUE, TRUE);
1218 if (esc_name == NULL)
1219 goto theend;
1220 buflen = STRLEN(tmp_orig) + STRLEN(esc_name) + STRLEN(tmp_new) + 16;
Bram Moolenaar964b3742019-05-24 18:54:09 +02001221 buf = alloc(buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 if (buf == NULL)
1223 goto theend;
1224
1225#ifdef UNIX
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +00001226 /* Temporarily chdir to /tmp, to avoid patching files in the current
Bram Moolenaar071d4272004-06-13 20:20:40 +00001227 * directory when the patch file contains more than one patch. When we
1228 * have our own temp dir use that instead, it will be cleaned up when we
1229 * exit (any .rej files created). Don't change directory if we can't
1230 * return to the current. */
1231 if (mch_dirname(dirbuf, MAXPATHL) != OK || mch_chdir((char *)dirbuf) != 0)
1232 dirbuf[0] = NUL;
1233 else
1234 {
1235# ifdef TEMPDIRNAMES
1236 if (vim_tempdir != NULL)
Bram Moolenaar42335f52018-09-13 15:33:43 +02001237 vim_ignored = mch_chdir((char *)vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 else
1239# endif
Bram Moolenaar42335f52018-09-13 15:33:43 +02001240 vim_ignored = mch_chdir("/tmp");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 shorten_fnames(TRUE);
1242 }
1243#endif
1244
1245#ifdef FEAT_EVAL
1246 if (*p_pex != NUL)
1247 /* Use 'patchexpr' to generate the new file. */
1248 eval_patch(tmp_orig,
1249# ifdef UNIX
1250 fullname != NULL ? fullname :
1251# endif
1252 eap->arg, tmp_new);
1253 else
1254#endif
1255 {
1256 /* Build the patch command and execute it. Ignore errors. Switch to
1257 * cooked mode to allow the user to respond to prompts. */
Bram Moolenaara95ab322017-03-11 19:21:53 +01001258 vim_snprintf((char *)buf, buflen, "patch -o %s %s < %s",
1259 tmp_new, tmp_orig, esc_name);
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001260 block_autocmds(); /* Avoid ShellCmdPost stuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 (void)call_shell(buf, SHELL_FILTER | SHELL_COOKED);
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001262 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 }
1264
1265#ifdef UNIX
1266 if (dirbuf[0] != NUL)
1267 {
1268 if (mch_chdir((char *)dirbuf) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001269 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 shorten_fnames(TRUE);
1271 }
1272#endif
1273
1274 /* patch probably has written over the screen */
1275 redraw_later(CLEAR);
1276
1277 /* Delete any .orig or .rej file created. */
1278 STRCPY(buf, tmp_new);
1279 STRCAT(buf, ".orig");
1280 mch_remove(buf);
1281 STRCPY(buf, tmp_new);
1282 STRCAT(buf, ".rej");
1283 mch_remove(buf);
1284
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001285 /* Only continue if the output file was created. */
1286 if (mch_stat((char *)tmp_new, &st) < 0 || st.st_size == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001287 emsg(_("E816: Cannot read patch output"));
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001288 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001290 if (curbuf->b_fname != NULL)
1291 {
1292 newname = vim_strnsave(curbuf->b_fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 (int)(STRLEN(curbuf->b_fname) + 4));
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001294 if (newname != NULL)
1295 STRCAT(newname, ".new");
1296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297
1298#ifdef FEAT_GUI
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001299 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300#endif
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001301 /* don't use a new tab page, each tab page has its own diffs */
1302 cmdmod.tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001303
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001304 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001306 /* Pretend it was a ":split fname" command */
1307 eap->cmdidx = CMD_split;
1308 eap->arg = tmp_new;
1309 do_exedit(eap, old_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001310
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001311 /* check that split worked and editing tmp_new */
1312 if (curwin != old_curwin && win_valid(old_curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001314 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1315 diff_win_options(curwin, TRUE);
1316 diff_win_options(old_curwin, TRUE);
1317
1318 if (newname != NULL)
1319 {
1320 /* do a ":file filename.new" on the patched buffer */
1321 eap->arg = newname;
1322 ex_file(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001324 /* Do filetype detection with the new name. */
1325 if (au_has_group((char_u *)"filetypedetect"))
1326 do_cmdline_cmd((char_u *)":doau filetypedetect BufRead");
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001327 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329 }
1330 }
1331
1332theend:
1333 if (tmp_orig != NULL)
1334 mch_remove(tmp_orig);
1335 vim_free(tmp_orig);
1336 if (tmp_new != NULL)
1337 mch_remove(tmp_new);
1338 vim_free(tmp_new);
1339 vim_free(newname);
1340 vim_free(buf);
1341#ifdef UNIX
1342 vim_free(fullname);
1343#endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001344 vim_free(esc_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345#ifdef FEAT_BROWSE
1346 vim_free(browseFile);
1347 cmdmod.browse = browse_flag;
1348#endif
1349}
1350
1351/*
1352 * Split the window and edit another file, setting options to show the diffs.
1353 */
1354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001355ex_diffsplit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356{
1357 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001358 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001360 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361#ifdef FEAT_GUI
1362 need_mouse_correct = TRUE;
1363#endif
Bram Moolenaar46328f92016-08-28 15:39:57 +02001364 /* Need to compute w_fraction when no redraw happened yet. */
1365 validate_cursor();
1366 set_fraction(curwin);
1367
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001368 /* don't use a new tab page, each tab page has its own diffs */
1369 cmdmod.tab = 0;
1370
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001371 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 {
1373 /* Pretend it was a ":split fname" command */
1374 eap->cmdidx = CMD_split;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001375 curwin->w_p_diff = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 do_exedit(eap, old_curwin);
1377
1378 if (curwin != old_curwin) /* split must have worked */
1379 {
1380 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1381 diff_win_options(curwin, TRUE);
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001382 if (win_valid(old_curwin))
1383 {
1384 diff_win_options(old_curwin, TRUE);
1385
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001386 if (bufref_valid(&old_curbuf))
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001387 /* Move the cursor position to that of the old window. */
1388 curwin->w_cursor.lnum = diff_get_corresponding_line(
Bram Moolenaar025e3e02016-10-18 14:50:18 +02001389 old_curbuf.br_buf, old_curwin->w_cursor.lnum);
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001390 }
Bram Moolenaar46328f92016-08-28 15:39:57 +02001391 /* Now that lines are folded scroll to show the cursor at the same
1392 * relative position. */
1393 scroll_to_fraction(curwin, curwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 }
1395 }
1396}
1397
1398/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001399 * Set options to show diffs for the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001402ex_diffthis(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403{
1404 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1405 diff_win_options(curwin, TRUE);
1406}
1407
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001408 static void
1409set_diff_option(win_T *wp, int value)
1410{
1411 win_T *old_curwin = curwin;
1412
1413 curwin = wp;
1414 curbuf = curwin->w_buffer;
1415 ++curbuf_lock;
1416 set_option_value((char_u *)"diff", (long)value, NULL, OPT_LOCAL);
1417 --curbuf_lock;
1418 curwin = old_curwin;
1419 curbuf = curwin->w_buffer;
1420}
1421
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422/*
1423 * Set options in window "wp" for diff mode.
1424 */
1425 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001426diff_win_options(
1427 win_T *wp,
1428 int addbuf) /* Add buffer to diff. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429{
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001430# ifdef FEAT_FOLDING
1431 win_T *old_curwin = curwin;
1432
1433 /* close the manually opened folds */
1434 curwin = wp;
1435 newFoldLevel();
1436 curwin = old_curwin;
1437# endif
1438
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001439 /* Use 'scrollbind' and 'cursorbind' when available */
Bram Moolenaar43929962015-07-03 15:06:56 +02001440 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001441 wp->w_p_scb_save = wp->w_p_scb;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001442 wp->w_p_scb = TRUE;
Bram Moolenaar43929962015-07-03 15:06:56 +02001443 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001444 wp->w_p_crb_save = wp->w_p_crb;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001445 wp->w_p_crb = TRUE;
Bram Moolenaar43929962015-07-03 15:06:56 +02001446 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001447 wp->w_p_wrap_save = wp->w_p_wrap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 wp->w_p_wrap = FALSE;
1449# ifdef FEAT_FOLDING
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001450 curwin = wp;
1451 curbuf = curwin->w_buffer;
Bram Moolenaar43929962015-07-03 15:06:56 +02001452 if (!wp->w_p_diff)
1453 {
1454 if (wp->w_p_diff_saved)
1455 free_string_option(wp->w_p_fdm_save);
Bram Moolenaara87aa802013-07-03 15:47:03 +02001456 wp->w_p_fdm_save = vim_strsave(wp->w_p_fdm);
Bram Moolenaar43929962015-07-03 15:06:56 +02001457 }
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001458 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"diff",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001459 OPT_LOCAL|OPT_FREE, 0);
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001460 curwin = old_curwin;
1461 curbuf = curwin->w_buffer;
Bram Moolenaar43929962015-07-03 15:06:56 +02001462 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001463 {
1464 wp->w_p_fdc_save = wp->w_p_fdc;
1465 wp->w_p_fen_save = wp->w_p_fen;
1466 wp->w_p_fdl_save = wp->w_p_fdl;
1467 }
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001468 wp->w_p_fdc = diff_foldcolumn;
1469 wp->w_p_fen = TRUE;
1470 wp->w_p_fdl = 0;
1471 foldUpdateAll(wp);
1472 /* make sure topline is not halfway a fold */
1473 changed_window_setting_win(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 if (vim_strchr(p_sbo, 'h') == NULL)
1476 do_cmdline_cmd((char_u *)"set sbo+=hor");
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001477 /* Save the current values, to be restored in ex_diffoff(). */
Bram Moolenaara87aa802013-07-03 15:47:03 +02001478 wp->w_p_diff_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001480 set_diff_option(wp, TRUE);
Bram Moolenaar43929962015-07-03 15:06:56 +02001481
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 if (addbuf)
1483 diff_buf_add(wp->w_buffer);
1484 redraw_win_later(wp, NOT_VALID);
1485}
1486
1487/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001488 * Set options not to show diffs. For the current window or all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001489 * Only in the current tab page.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001490 */
1491 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001492ex_diffoff(exarg_T *eap)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001493{
1494 win_T *wp;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001495 int diffwin = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001496
Bram Moolenaar29323592016-07-24 22:04:11 +02001497 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001498 {
Bram Moolenaar00462ff2013-09-20 20:13:53 +02001499 if (eap->forceit ? wp->w_p_diff : wp == curwin)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001500 {
Bram Moolenaar43929962015-07-03 15:06:56 +02001501 /* Set 'diff' off. If option values were saved in
1502 * diff_win_options(), restore the ones whose settings seem to have
1503 * been left over from diff mode. */
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001504 set_diff_option(wp, FALSE);
Bram Moolenaara87aa802013-07-03 15:47:03 +02001505
Bram Moolenaara87aa802013-07-03 15:47:03 +02001506 if (wp->w_p_diff_saved)
1507 {
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001508
Bram Moolenaar43929962015-07-03 15:06:56 +02001509 if (wp->w_p_scb)
1510 wp->w_p_scb = wp->w_p_scb_save;
Bram Moolenaar43929962015-07-03 15:06:56 +02001511 if (wp->w_p_crb)
1512 wp->w_p_crb = wp->w_p_crb_save;
Bram Moolenaar43929962015-07-03 15:06:56 +02001513 if (!wp->w_p_wrap)
1514 wp->w_p_wrap = wp->w_p_wrap_save;
1515#ifdef FEAT_FOLDING
1516 free_string_option(wp->w_p_fdm);
Bram Moolenaar79a213d2017-05-16 13:15:18 +02001517 wp->w_p_fdm = vim_strsave(
1518 *wp->w_p_fdm_save ? wp->w_p_fdm_save : (char_u*)"manual");
Bram Moolenaar43929962015-07-03 15:06:56 +02001519
1520 if (wp->w_p_fdc == diff_foldcolumn)
1521 wp->w_p_fdc = wp->w_p_fdc_save;
1522 if (wp->w_p_fdl == 0)
1523 wp->w_p_fdl = wp->w_p_fdl_save;
1524
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001525 /* Only restore 'foldenable' when 'foldmethod' is not
1526 * "manual", otherwise we continue to show the diff folds. */
Bram Moolenaar43929962015-07-03 15:06:56 +02001527 if (wp->w_p_fen)
1528 wp->w_p_fen = foldmethodIsManual(wp) ? FALSE
1529 : wp->w_p_fen_save;
1530
1531 foldUpdateAll(wp);
Bram Moolenaar43929962015-07-03 15:06:56 +02001532#endif
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001533 }
Bram Moolenaare67d5462016-08-27 22:40:42 +02001534 /* remove filler lines */
1535 wp->w_topfill = 0;
1536
1537 /* make sure topline is not halfway a fold and cursor is
1538 * invalidated */
1539 changed_window_setting_win(wp);
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001540
Bram Moolenaara87aa802013-07-03 15:47:03 +02001541 /* Note: 'sbo' is not restored, it's a global option. */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001542 diff_buf_adjust(wp);
1543 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001544 diffwin |= wp->w_p_diff;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001545 }
1546
Bram Moolenaar25ea0542017-02-03 23:16:28 +01001547 /* Also remove hidden buffers from the list. */
1548 if (eap->forceit)
1549 diff_buf_clear();
1550
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001551 /* Remove "hor" from from 'scrollopt' if there are no diff windows left. */
1552 if (!diffwin && vim_strchr(p_sbo, 'h') != NULL)
1553 do_cmdline_cmd((char_u *)"set sbo-=hor");
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001554}
1555
1556/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001557 * Read the diff output and add each entry to the diff list.
1558 */
1559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001560diff_read(
Bram Moolenaare828b762018-09-10 17:51:58 +02001561 int idx_orig, // idx of original file
1562 int idx_new, // idx of new file
1563 diffout_T *dout) // diff output
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
Bram Moolenaare828b762018-09-10 17:51:58 +02001565 FILE *fd = NULL;
1566 int line_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 diff_T *dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001568 diff_T *dp = curtab->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 diff_T *dn, *dpl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 char_u linebuf[LBUFLEN]; /* only need to hold the diff line */
Bram Moolenaare828b762018-09-10 17:51:58 +02001571 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001572 long off;
1573 int i;
1574 linenr_T lnum_orig, lnum_new;
1575 long count_orig, count_new;
1576 int notset = TRUE; /* block "*dp" not set yet */
Bram Moolenaare828b762018-09-10 17:51:58 +02001577 enum {
1578 DIFF_ED,
1579 DIFF_UNIFIED,
1580 DIFF_NONE
1581 } diffstyle = DIFF_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582
Bram Moolenaare828b762018-09-10 17:51:58 +02001583 if (dout->dout_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001584 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001585 diffstyle = DIFF_UNIFIED;
1586 }
1587 else
1588 {
1589 fd = mch_fopen((char *)dout->dout_fname, "r");
1590 if (fd == NULL)
1591 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001592 emsg(_("E98: Cannot read diff output"));
Bram Moolenaare828b762018-09-10 17:51:58 +02001593 return;
1594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001595 }
1596
1597 for (;;)
1598 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001599 if (fd == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001600 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001601 if (line_idx >= dout->dout_ga.ga_len)
1602 break; // did last line
1603 line = ((char_u **)dout->dout_ga.ga_data)[line_idx++];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 }
1605 else
1606 {
Bram Moolenaar00590742019-02-15 21:06:09 +01001607 if (vim_fgets(linebuf, LBUFLEN, fd))
Bram Moolenaare828b762018-09-10 17:51:58 +02001608 break; // end of file
1609 line = linebuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
1611
Bram Moolenaare828b762018-09-10 17:51:58 +02001612 if (diffstyle == DIFF_NONE)
1613 {
1614 // Determine diff style.
1615 // ed like diff looks like this:
1616 // {first}[,{last}]c{first}[,{last}]
1617 // {first}a{first}[,{last}]
1618 // {first}[,{last}]d{first}
1619 //
1620 // unified diff looks like this:
1621 // --- file1 2018-03-20 13:23:35.783153140 +0100
1622 // +++ file2 2018-03-20 13:23:41.183156066 +0100
1623 // @@ -1,3 +1,5 @@
1624 if (isdigit(*line))
1625 diffstyle = DIFF_ED;
1626 else if ((STRNCMP(line, "@@ ", 3) == 0))
1627 diffstyle = DIFF_UNIFIED;
1628 else if ((STRNCMP(line, "--- ", 4) == 0)
Bram Moolenaar00590742019-02-15 21:06:09 +01001629 && (vim_fgets(linebuf, LBUFLEN, fd) == 0)
Bram Moolenaare828b762018-09-10 17:51:58 +02001630 && (STRNCMP(line, "+++ ", 4) == 0)
Bram Moolenaar00590742019-02-15 21:06:09 +01001631 && (vim_fgets(linebuf, LBUFLEN, fd) == 0)
Bram Moolenaare828b762018-09-10 17:51:58 +02001632 && (STRNCMP(line, "@@ ", 3) == 0))
1633 diffstyle = DIFF_UNIFIED;
Bram Moolenaar3b8defd2018-09-13 13:03:11 +02001634 else
1635 // Format not recognized yet, skip over this line. Cygwin diff
1636 // may put a warning at the start of the file.
1637 continue;
Bram Moolenaare828b762018-09-10 17:51:58 +02001638 }
1639
1640 if (diffstyle == DIFF_ED)
1641 {
1642 if (!isdigit(*line))
1643 continue; // not the start of a diff block
1644 if (parse_diff_ed(line, &lnum_orig, &count_orig,
1645 &lnum_new, &count_new) == FAIL)
1646 continue;
1647 }
1648 else if (diffstyle == DIFF_UNIFIED)
1649 {
1650 if (STRNCMP(line, "@@ ", 3) != 0)
1651 continue; // not the start of a diff block
1652 if (parse_diff_unified(line, &lnum_orig, &count_orig,
1653 &lnum_new, &count_new) == FAIL)
1654 continue;
1655 }
1656 else
1657 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001658 emsg(_("E959: Invalid diff format."));
Bram Moolenaare828b762018-09-10 17:51:58 +02001659 break;
1660 }
1661
1662 // Go over blocks before the change, for which orig and new are equal.
1663 // Copy blocks from orig to new.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 while (dp != NULL
1665 && lnum_orig > dp->df_lnum[idx_orig] + dp->df_count[idx_orig])
1666 {
1667 if (notset)
1668 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1669 dprev = dp;
1670 dp = dp->df_next;
1671 notset = TRUE;
1672 }
1673
1674 if (dp != NULL
1675 && lnum_orig <= dp->df_lnum[idx_orig] + dp->df_count[idx_orig]
1676 && lnum_orig + count_orig >= dp->df_lnum[idx_orig])
1677 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001678 // New block overlaps with existing block(s).
1679 // First find last block that overlaps.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 for (dpl = dp; dpl->df_next != NULL; dpl = dpl->df_next)
1681 if (lnum_orig + count_orig < dpl->df_next->df_lnum[idx_orig])
1682 break;
1683
Bram Moolenaare828b762018-09-10 17:51:58 +02001684 // If the newly found block starts before the old one, set the
1685 // start back a number of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 off = dp->df_lnum[idx_orig] - lnum_orig;
1687 if (off > 0)
1688 {
1689 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001690 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 dp->df_lnum[i] -= off;
1692 dp->df_lnum[idx_new] = lnum_new;
1693 dp->df_count[idx_new] = count_new;
1694 }
1695 else if (notset)
1696 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001697 // new block inside existing one, adjust new block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698 dp->df_lnum[idx_new] = lnum_new + off;
1699 dp->df_count[idx_new] = count_new - off;
1700 }
1701 else
Bram Moolenaare828b762018-09-10 17:51:58 +02001702 // second overlap of new block with existing block
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001703 dp->df_count[idx_new] += count_new - count_orig
1704 + dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]
1705 - (dp->df_lnum[idx_orig] + dp->df_count[idx_orig]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706
Bram Moolenaare828b762018-09-10 17:51:58 +02001707 // Adjust the size of the block to include all the lines to the
1708 // end of the existing block or the new diff, whatever ends last.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 off = (lnum_orig + count_orig)
1710 - (dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]);
1711 if (off < 0)
1712 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001713 // new change ends in existing block, adjust the end if not
1714 // done already
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 if (notset)
1716 dp->df_count[idx_new] += -off;
1717 off = 0;
1718 }
Bram Moolenaard4b96bc2007-10-19 15:33:39 +00001719 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001720 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 dp->df_count[i] = dpl->df_lnum[i] + dpl->df_count[i]
1722 - dp->df_lnum[i] + off;
1723
Bram Moolenaare828b762018-09-10 17:51:58 +02001724 // Delete the diff blocks that have been merged into one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001725 dn = dp->df_next;
1726 dp->df_next = dpl->df_next;
1727 while (dn != dp->df_next)
1728 {
1729 dpl = dn->df_next;
1730 vim_free(dn);
1731 dn = dpl;
1732 }
1733 }
1734 else
1735 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001736 // Allocate a new diffblock.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001737 dp = diff_alloc_new(curtab, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 if (dp == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001739 goto done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001740
1741 dp->df_lnum[idx_orig] = lnum_orig;
1742 dp->df_count[idx_orig] = count_orig;
1743 dp->df_lnum[idx_new] = lnum_new;
1744 dp->df_count[idx_new] = count_new;
1745
Bram Moolenaare828b762018-09-10 17:51:58 +02001746 // Set values for other buffers, these must be equal to the
1747 // original buffer, otherwise there would have been a change
1748 // already.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 for (i = idx_orig + 1; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001750 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 diff_copy_entry(dprev, dp, idx_orig, i);
1752 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001753 notset = FALSE; // "*dp" has been set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001754 }
1755
Bram Moolenaare828b762018-09-10 17:51:58 +02001756 // for remaining diff blocks orig and new are equal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 while (dp != NULL)
1758 {
1759 if (notset)
1760 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1761 dprev = dp;
1762 dp = dp->df_next;
1763 notset = TRUE;
1764 }
1765
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001766done:
Bram Moolenaare828b762018-09-10 17:51:58 +02001767 if (fd != NULL)
1768 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001769}
1770
1771/*
1772 * Copy an entry at "dp" from "idx_orig" to "idx_new".
1773 */
1774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001775diff_copy_entry(
1776 diff_T *dprev,
1777 diff_T *dp,
1778 int idx_orig,
1779 int idx_new)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
1781 long off;
1782
1783 if (dprev == NULL)
1784 off = 0;
1785 else
1786 off = (dprev->df_lnum[idx_orig] + dprev->df_count[idx_orig])
1787 - (dprev->df_lnum[idx_new] + dprev->df_count[idx_new]);
1788 dp->df_lnum[idx_new] = dp->df_lnum[idx_orig] - off;
1789 dp->df_count[idx_new] = dp->df_count[idx_orig];
1790}
1791
1792/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001793 * Clear the list of diffblocks for tab page "tp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 */
Bram Moolenaarea408852005-06-25 22:49:46 +00001795 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001796diff_clear(tabpage_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797{
1798 diff_T *p, *next_p;
1799
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001800 for (p = tp->tp_first_diff; p != NULL; p = next_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
1802 next_p = p->df_next;
1803 vim_free(p);
1804 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001805 tp->tp_first_diff = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806}
1807
1808/*
1809 * Check diff status for line "lnum" in buffer "buf":
1810 * Returns 0 for nothing special
1811 * Returns -1 for a line that should be highlighted as changed.
1812 * Returns -2 for a line that should be highlighted as added/deleted.
1813 * Returns > 0 for inserting that many filler lines above it (never happens
1814 * when 'diffopt' doesn't contain "filler").
1815 * This should only be used for windows where 'diff' is set.
1816 */
1817 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001818diff_check(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001819{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001820 int idx; /* index in tp_diffbuf[] for this buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 diff_T *dp;
1822 int maxcount;
1823 int i;
1824 buf_T *buf = wp->w_buffer;
1825 int cmp;
1826
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001827 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001828 ex_diffupdate(NULL); /* update after a big change */
1829
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001830 if (curtab->tp_first_diff == NULL || !wp->w_p_diff) /* no diffs at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001831 return 0;
1832
1833 /* safety check: "lnum" must be a buffer line */
1834 if (lnum < 1 || lnum > buf->b_ml.ml_line_count + 1)
1835 return 0;
1836
1837 idx = diff_buf_idx(buf);
1838 if (idx == DB_COUNT)
1839 return 0; /* no diffs for buffer "buf" */
1840
1841#ifdef FEAT_FOLDING
1842 /* A closed fold never has filler lines. */
1843 if (hasFoldingWin(wp, lnum, NULL, NULL, TRUE, NULL))
1844 return 0;
1845#endif
1846
1847 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001848 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
1850 break;
1851 if (dp == NULL || lnum < dp->df_lnum[idx])
1852 return 0;
1853
1854 if (lnum < dp->df_lnum[idx] + dp->df_count[idx])
1855 {
1856 int zero = FALSE;
1857
1858 /* Changed or inserted line. If the other buffers have a count of
1859 * zero, the lines were inserted. If the other buffers have the same
1860 * count, check if the lines are identical. */
1861 cmp = FALSE;
1862 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001863 if (i != idx && curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 {
1865 if (dp->df_count[i] == 0)
1866 zero = TRUE;
1867 else
1868 {
1869 if (dp->df_count[i] != dp->df_count[idx])
1870 return -1; /* nr of lines changed. */
1871 cmp = TRUE;
1872 }
1873 }
1874 if (cmp)
1875 {
1876 /* Compare all lines. If they are equal the lines were inserted
1877 * in some buffers, deleted in others, but not changed. */
1878 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001879 if (i != idx && curtab->tp_diffbuf[i] != NULL
1880 && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 if (!diff_equal_entry(dp, idx, i))
1882 return -1;
1883 }
1884 /* If there is no buffer with zero lines then there is no difference
1885 * any longer. Happens when making a change (or undo) that removes
1886 * the difference. Can't remove the entry here, we might be halfway
1887 * updating the window. Just report the text as unchanged. Other
1888 * windows might still show the change though. */
1889 if (zero == FALSE)
1890 return 0;
1891 return -2;
1892 }
1893
1894 /* If 'diffopt' doesn't contain "filler", return 0. */
1895 if (!(diff_flags & DIFF_FILLER))
1896 return 0;
1897
1898 /* Insert filler lines above the line just below the change. Will return
1899 * 0 when this buf had the max count. */
1900 maxcount = 0;
1901 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001902 if (curtab->tp_diffbuf[i] != NULL && dp->df_count[i] > maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 maxcount = dp->df_count[i];
1904 return maxcount - dp->df_count[idx];
1905}
1906
1907/*
1908 * Compare two entries in diff "*dp" and return TRUE if they are equal.
1909 */
1910 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001911diff_equal_entry(diff_T *dp, int idx1, int idx2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912{
1913 int i;
1914 char_u *line;
1915 int cmp;
1916
1917 if (dp->df_count[idx1] != dp->df_count[idx2])
1918 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001919 if (diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 return FALSE;
1921 for (i = 0; i < dp->df_count[idx1]; ++i)
1922 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001923 line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001924 dp->df_lnum[idx1] + i, FALSE));
1925 if (line == NULL)
1926 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001927 cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 dp->df_lnum[idx2] + i, FALSE));
1929 vim_free(line);
1930 if (cmp != 0)
1931 return FALSE;
1932 }
1933 return TRUE;
1934}
1935
1936/*
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02001937 * Compare the characters at "p1" and "p2". If they are equal (possibly
1938 * ignoring case) return TRUE and set "len" to the number of bytes.
1939 */
1940 static int
1941diff_equal_char(char_u *p1, char_u *p2, int *len)
1942{
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02001943 int l = (*mb_ptr2len)(p1);
1944
1945 if (l != (*mb_ptr2len)(p2))
1946 return FALSE;
1947 if (l > 1)
1948 {
1949 if (STRNCMP(p1, p2, l) != 0
1950 && (!enc_utf8
1951 || !(diff_flags & DIFF_ICASE)
1952 || utf_fold(utf_ptr2char(p1))
1953 != utf_fold(utf_ptr2char(p2))))
1954 return FALSE;
1955 *len = l;
1956 }
1957 else
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02001958 {
1959 if ((*p1 != *p2)
1960 && (!(diff_flags & DIFF_ICASE)
1961 || TOLOWER_LOC(*p1) != TOLOWER_LOC(*p2)))
1962 return FALSE;
1963 *len = 1;
1964 }
1965 return TRUE;
1966}
1967
1968/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 * Compare strings "s1" and "s2" according to 'diffopt'.
1970 * Return non-zero when they are different.
1971 */
1972 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001973diff_cmp(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974{
1975 char_u *p1, *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977
Bram Moolenaar785fc652018-09-15 19:17:38 +02001978 if ((diff_flags & DIFF_IBLANK)
1979 && (*skipwhite(s1) == NUL || *skipwhite(s2) == NUL))
1980 return 0;
1981
1982 if ((diff_flags & (DIFF_ICASE | ALL_WHITE_DIFF)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 return STRCMP(s1, s2);
Bram Moolenaar785fc652018-09-15 19:17:38 +02001984 if ((diff_flags & DIFF_ICASE) && !(diff_flags & ALL_WHITE_DIFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 return MB_STRICMP(s1, s2);
1986
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 p1 = s1;
1988 p2 = s2;
Bram Moolenaar785fc652018-09-15 19:17:38 +02001989
1990 // Ignore white space changes and possibly ignore case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 while (*p1 != NUL && *p2 != NUL)
1992 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02001993 if (((diff_flags & DIFF_IWHITE)
1994 && VIM_ISWHITE(*p1) && VIM_ISWHITE(*p2))
1995 || ((diff_flags & DIFF_IWHITEALL)
1996 && (VIM_ISWHITE(*p1) || VIM_ISWHITE(*p2))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 {
1998 p1 = skipwhite(p1);
1999 p2 = skipwhite(p2);
2000 }
2001 else
2002 {
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002003 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 break;
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002005 p1 += l;
2006 p2 += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 }
2008 }
2009
Bram Moolenaar785fc652018-09-15 19:17:38 +02002010 // Ignore trailing white space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 p1 = skipwhite(p1);
2012 p2 = skipwhite(p2);
2013 if (*p1 != NUL || *p2 != NUL)
2014 return 1;
2015 return 0;
2016}
2017
2018/*
2019 * Return the number of filler lines above "lnum".
2020 */
2021 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002022diff_check_fill(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023{
2024 int n;
2025
2026 /* be quick when there are no filler lines */
2027 if (!(diff_flags & DIFF_FILLER))
2028 return 0;
2029 n = diff_check(wp, lnum);
2030 if (n <= 0)
2031 return 0;
2032 return n;
2033}
2034
2035/*
2036 * Set the topline of "towin" to match the position in "fromwin", so that they
2037 * show the same diff'ed lines.
2038 */
2039 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002040diff_set_topline(win_T *fromwin, win_T *towin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041{
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002042 buf_T *frombuf = fromwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002043 linenr_T lnum = fromwin->w_topline;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002044 int fromidx;
2045 int toidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 diff_T *dp;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002047 int max_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 int i;
2049
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002050 fromidx = diff_buf_idx(frombuf);
2051 if (fromidx == DB_COUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002052 return; /* safety check */
2053
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002054 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 ex_diffupdate(NULL); /* update after a big change */
2056
2057 towin->w_topfill = 0;
2058
2059 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002060 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002061 if (lnum <= dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002062 break;
2063 if (dp == NULL)
2064 {
2065 /* After last change, compute topline relative to end of file; no
2066 * filler lines. */
2067 towin->w_topline = towin->w_buffer->b_ml.ml_line_count
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002068 - (frombuf->b_ml.ml_line_count - lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 }
2070 else
2071 {
2072 /* Find index for "towin". */
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002073 toidx = diff_buf_idx(towin->w_buffer);
2074 if (toidx == DB_COUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 return; /* safety check */
2076
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002077 towin->w_topline = lnum + (dp->df_lnum[toidx] - dp->df_lnum[fromidx]);
2078 if (lnum >= dp->df_lnum[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002080 /* Inside a change: compute filler lines. With three or more
2081 * buffers we need to know the largest count. */
2082 max_count = 0;
2083 for (i = 0; i < DB_COUNT; ++i)
2084 if (curtab->tp_diffbuf[i] != NULL
2085 && max_count < dp->df_count[i])
2086 max_count = dp->df_count[i];
2087
2088 if (dp->df_count[toidx] == dp->df_count[fromidx])
2089 {
2090 /* same number of lines: use same filler count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091 towin->w_topfill = fromwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 }
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002093 else if (dp->df_count[toidx] > dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002095 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002097 /* more lines in towin and fromwin doesn't show diff
2098 * lines, only filler lines */
2099 if (max_count - fromwin->w_topfill >= dp->df_count[toidx])
2100 {
2101 /* towin also only shows filler lines */
2102 towin->w_topline = dp->df_lnum[toidx]
2103 + dp->df_count[toidx];
2104 towin->w_topfill = fromwin->w_topfill;
2105 }
2106 else
2107 /* towin still has some diff lines to show */
2108 towin->w_topline = dp->df_lnum[toidx]
2109 + max_count - fromwin->w_topfill;
2110 }
2111 }
2112 else if (towin->w_topline >= dp->df_lnum[toidx]
2113 + dp->df_count[toidx])
2114 {
2115 /* less lines in towin and no diff lines to show: compute
2116 * filler lines */
2117 towin->w_topline = dp->df_lnum[toidx] + dp->df_count[toidx];
2118 if (diff_flags & DIFF_FILLER)
2119 {
2120 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
2121 /* fromwin is also out of diff lines */
2122 towin->w_topfill = fromwin->w_topfill;
2123 else
2124 /* fromwin has some diff lines */
2125 towin->w_topfill = dp->df_lnum[fromidx]
2126 + max_count - lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 }
2128 }
2129 }
2130 }
2131
2132 /* safety check (if diff info gets outdated strange things may happen) */
2133 towin->w_botfill = FALSE;
2134 if (towin->w_topline > towin->w_buffer->b_ml.ml_line_count)
2135 {
2136 towin->w_topline = towin->w_buffer->b_ml.ml_line_count;
2137 towin->w_botfill = TRUE;
2138 }
2139 if (towin->w_topline < 1)
2140 {
2141 towin->w_topline = 1;
2142 towin->w_topfill = 0;
2143 }
2144
2145 /* When w_topline changes need to recompute w_botline and cursor position */
2146 invalidate_botline_win(towin);
2147 changed_line_abv_curs_win(towin);
2148
2149 check_topfill(towin, FALSE);
2150#ifdef FEAT_FOLDING
2151 (void)hasFoldingWin(towin, towin->w_topline, &towin->w_topline,
2152 NULL, TRUE, NULL);
2153#endif
2154}
2155
2156/*
2157 * This is called when 'diffopt' is changed.
2158 */
2159 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002160diffopt_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002161{
2162 char_u *p;
2163 int diff_context_new = 6;
2164 int diff_flags_new = 0;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002165 int diff_foldcolumn_new = 2;
Bram Moolenaare828b762018-09-10 17:51:58 +02002166 long diff_algorithm_new = 0;
Bram Moolenaarb6fc7282018-12-04 22:24:16 +01002167 long diff_indent_heuristic = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002168 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169
2170 p = p_dip;
2171 while (*p != NUL)
2172 {
2173 if (STRNCMP(p, "filler", 6) == 0)
2174 {
2175 p += 6;
2176 diff_flags_new |= DIFF_FILLER;
2177 }
2178 else if (STRNCMP(p, "context:", 8) == 0 && VIM_ISDIGIT(p[8]))
2179 {
2180 p += 8;
2181 diff_context_new = getdigits(&p);
2182 }
Bram Moolenaar785fc652018-09-15 19:17:38 +02002183 else if (STRNCMP(p, "iblank", 6) == 0)
2184 {
2185 p += 6;
2186 diff_flags_new |= DIFF_IBLANK;
2187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 else if (STRNCMP(p, "icase", 5) == 0)
2189 {
2190 p += 5;
2191 diff_flags_new |= DIFF_ICASE;
2192 }
Bram Moolenaar785fc652018-09-15 19:17:38 +02002193 else if (STRNCMP(p, "iwhiteall", 9) == 0)
2194 {
2195 p += 9;
2196 diff_flags_new |= DIFF_IWHITEALL;
2197 }
2198 else if (STRNCMP(p, "iwhiteeol", 9) == 0)
2199 {
2200 p += 9;
2201 diff_flags_new |= DIFF_IWHITEEOL;
2202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 else if (STRNCMP(p, "iwhite", 6) == 0)
2204 {
2205 p += 6;
2206 diff_flags_new |= DIFF_IWHITE;
2207 }
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002208 else if (STRNCMP(p, "horizontal", 10) == 0)
2209 {
2210 p += 10;
2211 diff_flags_new |= DIFF_HORIZONTAL;
2212 }
2213 else if (STRNCMP(p, "vertical", 8) == 0)
2214 {
2215 p += 8;
2216 diff_flags_new |= DIFF_VERTICAL;
2217 }
2218 else if (STRNCMP(p, "foldcolumn:", 11) == 0 && VIM_ISDIGIT(p[11]))
2219 {
2220 p += 11;
2221 diff_foldcolumn_new = getdigits(&p);
2222 }
Bram Moolenaar97ce4192017-12-01 20:35:58 +01002223 else if (STRNCMP(p, "hiddenoff", 9) == 0)
2224 {
2225 p += 9;
2226 diff_flags_new |= DIFF_HIDDEN_OFF;
2227 }
Bram Moolenaare828b762018-09-10 17:51:58 +02002228 else if (STRNCMP(p, "indent-heuristic", 16) == 0)
2229 {
2230 p += 16;
Bram Moolenaarb6fc7282018-12-04 22:24:16 +01002231 diff_indent_heuristic = XDF_INDENT_HEURISTIC;
Bram Moolenaare828b762018-09-10 17:51:58 +02002232 }
2233 else if (STRNCMP(p, "internal", 8) == 0)
2234 {
2235 p += 8;
2236 diff_flags_new |= DIFF_INTERNAL;
2237 }
2238 else if (STRNCMP(p, "algorithm:", 10) == 0)
2239 {
2240 p += 10;
2241 if (STRNCMP(p, "myers", 5) == 0)
2242 {
2243 p += 5;
2244 diff_algorithm_new = 0;
2245 }
2246 else if (STRNCMP(p, "minimal", 7) == 0)
2247 {
2248 p += 7;
2249 diff_algorithm_new = XDF_NEED_MINIMAL;
2250 }
2251 else if (STRNCMP(p, "patience", 8) == 0)
2252 {
2253 p += 8;
2254 diff_algorithm_new = XDF_PATIENCE_DIFF;
2255 }
2256 else if (STRNCMP(p, "histogram", 9) == 0)
2257 {
2258 p += 9;
2259 diff_algorithm_new = XDF_HISTOGRAM_DIFF;
2260 }
Bram Moolenaard0721052018-11-05 21:21:33 +01002261 else
2262 return FAIL;
Bram Moolenaare828b762018-09-10 17:51:58 +02002263 }
2264
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 if (*p != ',' && *p != NUL)
2266 return FAIL;
2267 if (*p == ',')
2268 ++p;
2269 }
2270
Bram Moolenaarb6fc7282018-12-04 22:24:16 +01002271 diff_algorithm_new |= diff_indent_heuristic;
2272
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002273 /* Can't have both "horizontal" and "vertical". */
2274 if ((diff_flags_new & DIFF_HORIZONTAL) && (diff_flags_new & DIFF_VERTICAL))
2275 return FAIL;
2276
Bram Moolenaar198fa062018-09-18 21:20:26 +02002277 // If flags were added or removed, or the algorithm was changed, need to
2278 // update the diff.
Bram Moolenaare828b762018-09-10 17:51:58 +02002279 if (diff_flags != diff_flags_new || diff_algorithm != diff_algorithm_new)
Bram Moolenaar29323592016-07-24 22:04:11 +02002280 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002281 tp->tp_diff_invalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282
2283 diff_flags = diff_flags_new;
Bram Moolenaarb9ddda62019-02-19 23:00:50 +01002284 diff_context = diff_context_new == 0 ? 1 : diff_context_new;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002285 diff_foldcolumn = diff_foldcolumn_new;
Bram Moolenaare828b762018-09-10 17:51:58 +02002286 diff_algorithm = diff_algorithm_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287
2288 diff_redraw(TRUE);
2289
2290 /* recompute the scroll binding with the new option value, may
2291 * remove or add filler lines */
2292 check_scrollbind((linenr_T)0, 0L);
2293
2294 return OK;
2295}
2296
2297/*
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002298 * Return TRUE if 'diffopt' contains "horizontal".
2299 */
2300 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002301diffopt_horizontal(void)
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002302{
2303 return (diff_flags & DIFF_HORIZONTAL) != 0;
2304}
2305
2306/*
Bram Moolenaar97ce4192017-12-01 20:35:58 +01002307 * Return TRUE if 'diffopt' contains "hiddenoff".
2308 */
2309 int
2310diffopt_hiddenoff(void)
2311{
2312 return (diff_flags & DIFF_HIDDEN_OFF) != 0;
2313}
2314
2315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 * Find the difference within a changed line.
2317 * Returns TRUE if the line was added, no other buffer has it.
2318 */
2319 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002320diff_find_change(
2321 win_T *wp,
2322 linenr_T lnum,
2323 int *startp, /* first char of the change */
2324 int *endp) /* last char of the change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325{
2326 char_u *line_org;
2327 char_u *line_new;
2328 int i;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002329 int si_org, si_new;
2330 int ei_org, ei_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 diff_T *dp;
2332 int idx;
2333 int off;
2334 int added = TRUE;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002335 char_u *p1, *p2;
2336 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337
2338 /* Make a copy of the line, the next ml_get() will invalidate it. */
2339 line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE));
2340 if (line_org == NULL)
2341 return FALSE;
2342
2343 idx = diff_buf_idx(wp->w_buffer);
2344 if (idx == DB_COUNT) /* cannot happen */
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002345 {
2346 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
2350 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002351 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
2353 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002354 if (dp == NULL || diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002355 {
2356 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359
2360 off = lnum - dp->df_lnum[idx];
2361
2362 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002363 if (curtab->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
2365 /* Skip lines that are not in the other change (filler lines). */
2366 if (off >= dp->df_count[i])
2367 continue;
2368 added = FALSE;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002369 line_new = ml_get_buf(curtab->tp_diffbuf[i],
2370 dp->df_lnum[i] + off, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371
2372 /* Search for start of difference */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002373 si_org = si_new = 0;
2374 while (line_org[si_org] != NUL)
2375 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002376 if (((diff_flags & DIFF_IWHITE)
2377 && VIM_ISWHITE(line_org[si_org])
2378 && VIM_ISWHITE(line_new[si_new]))
2379 || ((diff_flags & DIFF_IWHITEALL)
2380 && (VIM_ISWHITE(line_org[si_org])
2381 || VIM_ISWHITE(line_new[si_new]))))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002382 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002383 si_org = (int)(skipwhite(line_org + si_org) - line_org);
2384 si_new = (int)(skipwhite(line_new + si_new) - line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002385 }
2386 else
2387 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002388 if (!diff_equal_char(line_org + si_org, line_new + si_new,
2389 &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002390 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002391 si_org += l;
2392 si_new += l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002393 }
2394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 if (has_mbyte)
2396 {
2397 /* Move back to first byte of character in both lines (may
2398 * have "nn^" in line_org and "n^ in line_new). */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002399 si_org -= (*mb_head_off)(line_org, line_org + si_org);
2400 si_new -= (*mb_head_off)(line_new, line_new + si_new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 }
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002402 if (*startp > si_org)
2403 *startp = si_org;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404
2405 /* Search for end of difference, if any. */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002406 if (line_org[si_org] != NUL || line_new[si_new] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
2408 ei_org = (int)STRLEN(line_org);
2409 ei_new = (int)STRLEN(line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002410 while (ei_org >= *startp && ei_new >= si_new
2411 && ei_org >= 0 && ei_new >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002412 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002413 if (((diff_flags & DIFF_IWHITE)
2414 && VIM_ISWHITE(line_org[ei_org])
2415 && VIM_ISWHITE(line_new[ei_new]))
2416 || ((diff_flags & DIFF_IWHITEALL)
2417 && (VIM_ISWHITE(line_org[ei_org])
2418 || VIM_ISWHITE(line_new[ei_new]))))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002419 {
2420 while (ei_org >= *startp
Bram Moolenaar1c465442017-03-12 20:10:05 +01002421 && VIM_ISWHITE(line_org[ei_org]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002422 --ei_org;
2423 while (ei_new >= si_new
Bram Moolenaar1c465442017-03-12 20:10:05 +01002424 && VIM_ISWHITE(line_new[ei_new]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002425 --ei_new;
2426 }
2427 else
2428 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002429 p1 = line_org + ei_org;
2430 p2 = line_new + ei_new;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002431 p1 -= (*mb_head_off)(line_org, p1);
2432 p2 -= (*mb_head_off)(line_new, p2);
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002433 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002434 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002435 ei_org -= l;
2436 ei_new -= l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 }
2439 if (*endp < ei_org)
2440 *endp = ei_org;
2441 }
2442 }
2443
2444 vim_free(line_org);
2445 return added;
2446}
2447
2448#if defined(FEAT_FOLDING) || defined(PROTO)
2449/*
2450 * Return TRUE if line "lnum" is not close to a diff block, this line should
2451 * be in a fold.
2452 * Return FALSE if there are no diff blocks at all in this window.
2453 */
2454 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002455diff_infold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
2457 int i;
2458 int idx = -1;
2459 int other = FALSE;
2460 diff_T *dp;
2461
2462 /* Return if 'diff' isn't set. */
2463 if (!wp->w_p_diff)
2464 return FALSE;
2465
2466 for (i = 0; i < DB_COUNT; ++i)
2467 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002468 if (curtab->tp_diffbuf[i] == wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 idx = i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002470 else if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 other = TRUE;
2472 }
2473
2474 /* return here if there are no diffs in the window */
2475 if (idx == -1 || !other)
2476 return FALSE;
2477
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002478 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 ex_diffupdate(NULL); /* update after a big change */
2480
2481 /* Return if there are no diff blocks. All lines will be folded. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002482 if (curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 return TRUE;
2484
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002485 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
2487 /* If this change is below the line there can't be any further match. */
2488 if (dp->df_lnum[idx] - diff_context > lnum)
2489 break;
2490 /* If this change ends before the line we have a match. */
2491 if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum)
2492 return FALSE;
2493 }
2494 return TRUE;
2495}
2496#endif
2497
2498/*
2499 * "dp" and "do" commands.
2500 */
2501 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002502nv_diffgetput(int put, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503{
2504 exarg_T ea;
Bram Moolenaar6a643652014-10-31 13:54:25 +01002505 char_u buf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506
Bram Moolenaarf2732452018-06-03 14:47:35 +02002507#ifdef FEAT_JOB_CHANNEL
2508 if (bt_prompt(curbuf))
2509 {
2510 vim_beep(BO_OPER);
2511 return;
2512 }
2513#endif
Bram Moolenaar6a643652014-10-31 13:54:25 +01002514 if (count == 0)
2515 ea.arg = (char_u *)"";
2516 else
2517 {
2518 vim_snprintf((char *)buf, 30, "%ld", count);
2519 ea.arg = buf;
2520 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 if (put)
2522 ea.cmdidx = CMD_diffput;
2523 else
2524 ea.cmdidx = CMD_diffget;
2525 ea.addr_count = 0;
2526 ea.line1 = curwin->w_cursor.lnum;
2527 ea.line2 = curwin->w_cursor.lnum;
2528 ex_diffgetput(&ea);
2529}
2530
2531/*
2532 * ":diffget"
2533 * ":diffput"
2534 */
2535 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002536ex_diffgetput(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537{
2538 linenr_T lnum;
2539 int count;
2540 linenr_T off = 0;
2541 diff_T *dp;
2542 diff_T *dprev;
2543 diff_T *dfree;
2544 int idx_cur;
2545 int idx_other;
2546 int idx_from;
2547 int idx_to;
2548 int i;
2549 int added;
2550 char_u *p;
2551 aco_save_T aco;
2552 buf_T *buf;
2553 int start_skip, end_skip;
2554 int new_count;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002555 int buf_empty;
Bram Moolenaar602eb742007-02-20 03:43:38 +00002556 int found_not_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557
2558 /* Find the current buffer in the list of diff buffers. */
2559 idx_cur = diff_buf_idx(curbuf);
2560 if (idx_cur == DB_COUNT)
2561 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002562 emsg(_("E99: Current buffer is not in diff mode"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 return;
2564 }
2565
2566 if (*eap->arg == NUL)
2567 {
2568 /* No argument: Find the other buffer in the list of diff buffers. */
2569 for (idx_other = 0; idx_other < DB_COUNT; ++idx_other)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002570 if (curtab->tp_diffbuf[idx_other] != curbuf
Bram Moolenaar602eb742007-02-20 03:43:38 +00002571 && curtab->tp_diffbuf[idx_other] != NULL)
2572 {
2573 if (eap->cmdidx != CMD_diffput
2574 || curtab->tp_diffbuf[idx_other]->b_p_ma)
2575 break;
2576 found_not_ma = TRUE;
2577 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 if (idx_other == DB_COUNT)
2579 {
Bram Moolenaar602eb742007-02-20 03:43:38 +00002580 if (found_not_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002581 emsg(_("E793: No other buffer in diff mode is modifiable"));
Bram Moolenaar602eb742007-02-20 03:43:38 +00002582 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002583 emsg(_("E100: No other buffer in diff mode"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584 return;
2585 }
2586
2587 /* Check that there isn't a third buffer in the list */
2588 for (i = idx_other + 1; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002589 if (curtab->tp_diffbuf[i] != curbuf
2590 && curtab->tp_diffbuf[i] != NULL
2591 && (eap->cmdidx != CMD_diffput || curtab->tp_diffbuf[i]->b_p_ma))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002593 emsg(_("E101: More than two buffers in diff mode, don't know which one to use"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 return;
2595 }
2596 }
2597 else
2598 {
2599 /* Buffer number or pattern given. Ignore trailing white space. */
2600 p = eap->arg + STRLEN(eap->arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002601 while (p > eap->arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 --p;
2603 for (i = 0; vim_isdigit(eap->arg[i]) && eap->arg + i < p; ++i)
2604 ;
2605 if (eap->arg + i == p) /* digits only */
2606 i = atol((char *)eap->arg);
2607 else
2608 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002609 i = buflist_findpat(eap->arg, p, FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610 if (i < 0)
2611 return; /* error message already given */
2612 }
2613 buf = buflist_findnr(i);
2614 if (buf == NULL)
2615 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002616 semsg(_("E102: Can't find buffer \"%s\""), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 return;
2618 }
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +00002619 if (buf == curbuf)
2620 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 idx_other = diff_buf_idx(buf);
2622 if (idx_other == DB_COUNT)
2623 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002624 semsg(_("E103: Buffer \"%s\" is not in diff mode"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002625 return;
2626 }
2627 }
2628
2629 diff_busy = TRUE;
2630
2631 /* When no range given include the line above or below the cursor. */
2632 if (eap->addr_count == 0)
2633 {
2634 /* Make it possible that ":diffget" on the last line gets line below
2635 * the cursor line when there is no difference above the cursor. */
2636 if (eap->cmdidx == CMD_diffget
2637 && eap->line1 == curbuf->b_ml.ml_line_count
2638 && diff_check(curwin, eap->line1) == 0
2639 && (eap->line1 == 1 || diff_check(curwin, eap->line1 - 1) == 0))
2640 ++eap->line2;
2641 else if (eap->line1 > 0)
2642 --eap->line1;
2643 }
2644
2645 if (eap->cmdidx == CMD_diffget)
2646 {
2647 idx_from = idx_other;
2648 idx_to = idx_cur;
2649 }
2650 else
2651 {
2652 idx_from = idx_cur;
2653 idx_to = idx_other;
2654 /* Need to make the other buffer the current buffer to be able to make
2655 * changes in it. */
2656 /* set curwin/curbuf to buf and save a few things */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002657 aucmd_prepbuf(&aco, curtab->tp_diffbuf[idx_other]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002658 }
2659
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002660 /* May give the warning for a changed buffer here, which can trigger the
2661 * FileChangedRO autocommand, which may do nasty things and mess
2662 * everything up. */
2663 if (!curbuf->b_changed)
2664 {
2665 change_warning(0);
2666 if (diff_buf_idx(curbuf) != idx_to)
2667 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002668 emsg(_("E787: Buffer changed unexpectedly"));
Bram Moolenaard2b58c02018-09-16 18:10:48 +02002669 goto theend;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002670 }
2671 }
2672
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002674 for (dp = curtab->tp_first_diff; dp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002675 {
2676 if (dp->df_lnum[idx_cur] > eap->line2 + off)
2677 break; /* past the range that was specified */
2678
2679 dfree = NULL;
2680 lnum = dp->df_lnum[idx_to];
2681 count = dp->df_count[idx_to];
2682 if (dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off
2683 && u_save(lnum - 1, lnum + count) != FAIL)
2684 {
2685 /* Inside the specified range and saving for undo worked. */
2686 start_skip = 0;
2687 end_skip = 0;
2688 if (eap->addr_count > 0)
2689 {
2690 /* A range was specified: check if lines need to be skipped. */
2691 start_skip = eap->line1 + off - dp->df_lnum[idx_cur];
2692 if (start_skip > 0)
2693 {
2694 /* range starts below start of current diff block */
2695 if (start_skip > count)
2696 {
2697 lnum += count;
2698 count = 0;
2699 }
2700 else
2701 {
2702 count -= start_skip;
2703 lnum += start_skip;
2704 }
2705 }
2706 else
2707 start_skip = 0;
2708
2709 end_skip = dp->df_lnum[idx_cur] + dp->df_count[idx_cur] - 1
2710 - (eap->line2 + off);
2711 if (end_skip > 0)
2712 {
2713 /* range ends above end of current/from diff block */
2714 if (idx_cur == idx_from) /* :diffput */
2715 {
2716 i = dp->df_count[idx_cur] - start_skip - end_skip;
2717 if (count > i)
2718 count = i;
2719 }
2720 else /* :diffget */
2721 {
2722 count -= end_skip;
2723 end_skip = dp->df_count[idx_from] - start_skip - count;
2724 if (end_skip < 0)
2725 end_skip = 0;
2726 }
2727 }
2728 else
2729 end_skip = 0;
2730 }
2731
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002732 buf_empty = BUFEMPTY();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 added = 0;
2734 for (i = 0; i < count; ++i)
2735 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00002736 /* remember deleting the last line of the buffer */
2737 buf_empty = curbuf->b_ml.ml_line_count == 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 ml_delete(lnum, FALSE);
2739 --added;
2740 }
2741 for (i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; ++i)
2742 {
2743 linenr_T nr;
2744
2745 nr = dp->df_lnum[idx_from] + start_skip + i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002746 if (nr > curtab->tp_diffbuf[idx_from]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002748 p = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx_from],
2749 nr, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 if (p != NULL)
2751 {
2752 ml_append(lnum + i - 1, p, 0, FALSE);
2753 vim_free(p);
2754 ++added;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002755 if (buf_empty && curbuf->b_ml.ml_line_count == 2)
2756 {
2757 /* Added the first line into an empty buffer, need to
2758 * delete the dummy empty line. */
2759 buf_empty = FALSE;
2760 ml_delete((linenr_T)2, FALSE);
2761 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 }
2763 }
2764 new_count = dp->df_count[idx_to] + added;
2765 dp->df_count[idx_to] = new_count;
2766
2767 if (start_skip == 0 && end_skip == 0)
2768 {
2769 /* Check if there are any other buffers and if the diff is
2770 * equal in them. */
2771 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002772 if (curtab->tp_diffbuf[i] != NULL && i != idx_from
2773 && i != idx_to
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 && !diff_equal_entry(dp, idx_from, i))
2775 break;
2776 if (i == DB_COUNT)
2777 {
2778 /* delete the diff entry, the buffers are now equal here */
2779 dfree = dp;
2780 dp = dp->df_next;
2781 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002782 curtab->tp_first_diff = dp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 else
2784 dprev->df_next = dp;
2785 }
2786 }
2787
2788 /* Adjust marks. This will change the following entries! */
2789 if (added != 0)
2790 {
2791 mark_adjust(lnum, lnum + count - 1, (long)MAXLNUM, (long)added);
2792 if (curwin->w_cursor.lnum >= lnum)
2793 {
2794 /* Adjust the cursor position if it's in/after the changed
2795 * lines. */
2796 if (curwin->w_cursor.lnum >= lnum + count)
2797 curwin->w_cursor.lnum += added;
2798 else if (added < 0)
2799 curwin->w_cursor.lnum = lnum;
2800 }
2801 }
2802 changed_lines(lnum, 0, lnum + count, (long)added);
2803
2804 if (dfree != NULL)
2805 {
2806 /* Diff is deleted, update folds in other windows. */
2807#ifdef FEAT_FOLDING
2808 diff_fold_update(dfree, idx_to);
2809#endif
2810 vim_free(dfree);
2811 }
2812 else
2813 /* mark_adjust() may have changed the count in a wrong way */
2814 dp->df_count[idx_to] = new_count;
2815
2816 /* When changing the current buffer, keep track of line numbers */
2817 if (idx_cur == idx_to)
2818 off += added;
2819 }
2820
2821 /* If before the range or not deleted, go to next diff. */
2822 if (dfree == NULL)
2823 {
2824 dprev = dp;
2825 dp = dp->df_next;
2826 }
2827 }
2828
2829 /* restore curwin/curbuf and a few other things */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02002830 if (eap->cmdidx != CMD_diffget)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 {
2832 /* Syncing undo only works for the current buffer, but we change
2833 * another buffer. Sync undo if the command was typed. This isn't
2834 * 100% right when ":diffput" is used in a function or mapping. */
2835 if (KeyTyped)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002836 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 aucmd_restbuf(&aco);
2838 }
2839
Bram Moolenaard2b58c02018-09-16 18:10:48 +02002840theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002841 diff_busy = FALSE;
Bram Moolenaard2b58c02018-09-16 18:10:48 +02002842 if (diff_need_update)
Bram Moolenaard2b58c02018-09-16 18:10:48 +02002843 ex_diffupdate(NULL);
Bram Moolenaardf77cef2018-10-07 17:46:42 +02002844
Bram Moolenaar5f57bdc2018-10-25 17:52:23 +02002845 // Check that the cursor is on a valid character and update its
Bram Moolenaardf77cef2018-10-07 17:46:42 +02002846 // position. When there were filler lines the topline has become
2847 // invalid.
2848 check_cursor();
2849 changed_line_abv_curs();
2850
2851 if (diff_need_update)
2852 // redraw already done by ex_diffupdate()
2853 diff_need_update = FALSE;
Bram Moolenaar198fa062018-09-18 21:20:26 +02002854 else
2855 {
Bram Moolenaar198fa062018-09-18 21:20:26 +02002856 // Also need to redraw the other buffers.
2857 diff_redraw(FALSE);
2858 apply_autocmds(EVENT_DIFFUPDATED, NULL, NULL, FALSE, curbuf);
2859 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860}
2861
2862#ifdef FEAT_FOLDING
2863/*
2864 * Update folds for all diff buffers for entry "dp".
2865 * Skip buffer with index "skip_idx".
2866 * When there are no diffs, all folds are removed.
2867 */
2868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002869diff_fold_update(diff_T *dp, int skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870{
2871 int i;
2872 win_T *wp;
2873
Bram Moolenaar29323592016-07-24 22:04:11 +02002874 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002876 if (curtab->tp_diffbuf[i] == wp->w_buffer && i != skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 foldUpdate(wp, dp->df_lnum[i],
2878 dp->df_lnum[i] + dp->df_count[i]);
2879}
2880#endif
2881
2882/*
2883 * Return TRUE if buffer "buf" is in diff-mode.
2884 */
2885 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002886diff_mode_buf(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002888 tabpage_T *tp;
2889
Bram Moolenaar29323592016-07-24 22:04:11 +02002890 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002891 if (diff_buf_idx_tp(buf, tp) != DB_COUNT)
2892 return TRUE;
2893 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002894}
2895
2896/*
2897 * Move "count" times in direction "dir" to the next diff block.
2898 * Return FAIL if there isn't such a diff block.
2899 */
2900 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002901diff_move_to(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002902{
2903 int idx;
2904 linenr_T lnum = curwin->w_cursor.lnum;
2905 diff_T *dp;
2906
2907 idx = diff_buf_idx(curbuf);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002908 if (idx == DB_COUNT || curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002909 return FAIL;
2910
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002911 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 ex_diffupdate(NULL); /* update after a big change */
2913
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002914 if (curtab->tp_first_diff == NULL) /* no diffs today */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 return FAIL;
2916
2917 while (--count >= 0)
2918 {
2919 /* Check if already before first diff. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002920 if (dir == BACKWARD && lnum <= curtab->tp_first_diff->df_lnum[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 break;
2922
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002923 for (dp = curtab->tp_first_diff; ; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 {
2925 if (dp == NULL)
2926 break;
2927 if ((dir == FORWARD && lnum < dp->df_lnum[idx])
2928 || (dir == BACKWARD
2929 && (dp->df_next == NULL
2930 || lnum <= dp->df_next->df_lnum[idx])))
2931 {
2932 lnum = dp->df_lnum[idx];
2933 break;
2934 }
2935 }
2936 }
2937
2938 /* don't end up past the end of the file */
2939 if (lnum > curbuf->b_ml.ml_line_count)
2940 lnum = curbuf->b_ml.ml_line_count;
2941
2942 /* When the cursor didn't move at all we fail. */
2943 if (lnum == curwin->w_cursor.lnum)
2944 return FAIL;
2945
2946 setpcmark();
2947 curwin->w_cursor.lnum = lnum;
2948 curwin->w_cursor.col = 0;
2949
2950 return OK;
2951}
2952
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002953/*
2954 * Return the line number in the current window that is closest to "lnum1" in
2955 * "buf1" in diff mode.
2956 */
2957 static linenr_T
2958diff_get_corresponding_line_int(
Bram Moolenaar7454a062016-01-30 15:14:10 +01002959 buf_T *buf1,
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002960 linenr_T lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002961{
2962 int idx1;
2963 int idx2;
2964 diff_T *dp;
2965 int baseline = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002966
2967 idx1 = diff_buf_idx(buf1);
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002968 idx2 = diff_buf_idx(curbuf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002969 if (idx1 == DB_COUNT || idx2 == DB_COUNT || curtab->tp_first_diff == NULL)
2970 return lnum1;
2971
2972 if (curtab->tp_diff_invalid)
2973 ex_diffupdate(NULL); /* update after a big change */
2974
2975 if (curtab->tp_first_diff == NULL) /* no diffs today */
2976 return lnum1;
2977
2978 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
2979 {
2980 if (dp->df_lnum[idx1] > lnum1)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002981 return lnum1 - baseline;
2982 if ((dp->df_lnum[idx1] + dp->df_count[idx1]) > lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002983 {
2984 /* Inside the diffblock */
2985 baseline = lnum1 - dp->df_lnum[idx1];
2986 if (baseline > dp->df_count[idx2])
2987 baseline = dp->df_count[idx2];
2988
2989 return dp->df_lnum[idx2] + baseline;
2990 }
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002991 if ( (dp->df_lnum[idx1] == lnum1)
2992 && (dp->df_count[idx1] == 0)
2993 && (dp->df_lnum[idx2] <= curwin->w_cursor.lnum)
2994 && ((dp->df_lnum[idx2] + dp->df_count[idx2])
2995 > curwin->w_cursor.lnum))
Bram Moolenaar860cae12010-06-05 23:22:07 +02002996 /*
2997 * Special case: if the cursor is just after a zero-count
2998 * block (i.e. all filler) and the target cursor is already
2999 * inside the corresponding block, leave the target cursor
3000 * unmoved. This makes repeated CTRL-W W operations work
3001 * as expected.
3002 */
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003003 return curwin->w_cursor.lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003004 baseline = (dp->df_lnum[idx1] + dp->df_count[idx1])
3005 - (dp->df_lnum[idx2] + dp->df_count[idx2]);
3006 }
3007
3008 /* If we get here then the cursor is after the last diff */
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003009 return lnum1 - baseline;
3010}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003011
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003012/*
3013 * Return the line number in the current window that is closest to "lnum1" in
3014 * "buf1" in diff mode. Checks the line number to be valid.
3015 */
3016 linenr_T
3017diff_get_corresponding_line(buf_T *buf1, linenr_T lnum1)
3018{
3019 linenr_T lnum = diff_get_corresponding_line_int(buf1, lnum1);
3020
3021 /* don't end up past the end of the file */
3022 if (lnum > curbuf->b_ml.ml_line_count)
3023 return curbuf->b_ml.ml_line_count;
3024 return lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003025}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003026
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027/*
3028 * For line "lnum" in the current window find the equivalent lnum in window
3029 * "wp", compensating for inserted/deleted lines.
3030 */
3031 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003032diff_lnum_win(linenr_T lnum, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033{
3034 diff_T *dp;
3035 int idx;
3036 int i;
3037 linenr_T n;
3038
3039 idx = diff_buf_idx(curbuf);
3040 if (idx == DB_COUNT) /* safety check */
3041 return (linenr_T)0;
3042
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003043 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 ex_diffupdate(NULL); /* update after a big change */
3045
3046 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003047 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003048 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
3049 break;
3050
3051 /* When after the last change, compute relative to the last line number. */
3052 if (dp == NULL)
3053 return wp->w_buffer->b_ml.ml_line_count
3054 - (curbuf->b_ml.ml_line_count - lnum);
3055
3056 /* Find index for "wp". */
3057 i = diff_buf_idx(wp->w_buffer);
3058 if (i == DB_COUNT) /* safety check */
3059 return (linenr_T)0;
3060
3061 n = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]);
3062 if (n > dp->df_lnum[i] + dp->df_count[i])
3063 n = dp->df_lnum[i] + dp->df_count[i];
3064 return n;
3065}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066
Bram Moolenaare828b762018-09-10 17:51:58 +02003067/*
3068 * Handle an ED style diff line.
3069 * Return FAIL if the line does not contain diff info.
3070 */
3071 static int
3072parse_diff_ed(
3073 char_u *line,
3074 linenr_T *lnum_orig,
3075 long *count_orig,
3076 linenr_T *lnum_new,
3077 long *count_new)
3078{
3079 char_u *p;
3080 long f1, l1, f2, l2;
3081 int difftype;
3082
3083 // The line must be one of three formats:
3084 // change: {first}[,{last}]c{first}[,{last}]
3085 // append: {first}a{first}[,{last}]
3086 // delete: {first}[,{last}]d{first}
3087 p = line;
3088 f1 = getdigits(&p);
3089 if (*p == ',')
3090 {
3091 ++p;
3092 l1 = getdigits(&p);
3093 }
3094 else
3095 l1 = f1;
3096 if (*p != 'a' && *p != 'c' && *p != 'd')
3097 return FAIL; // invalid diff format
3098 difftype = *p++;
3099 f2 = getdigits(&p);
3100 if (*p == ',')
3101 {
3102 ++p;
3103 l2 = getdigits(&p);
3104 }
3105 else
3106 l2 = f2;
3107 if (l1 < f1 || l2 < f2)
3108 return FAIL;
3109
3110 if (difftype == 'a')
3111 {
3112 *lnum_orig = f1 + 1;
3113 *count_orig = 0;
3114 }
3115 else
3116 {
3117 *lnum_orig = f1;
3118 *count_orig = l1 - f1 + 1;
3119 }
3120 if (difftype == 'd')
3121 {
3122 *lnum_new = f2 + 1;
3123 *count_new = 0;
3124 }
3125 else
3126 {
3127 *lnum_new = f2;
3128 *count_new = l2 - f2 + 1;
3129 }
3130 return OK;
3131}
3132
3133/*
3134 * Parses unified diff with zero(!) context lines.
3135 * Return FAIL if there is no diff information in "line".
3136 */
3137 static int
3138parse_diff_unified(
3139 char_u *line,
3140 linenr_T *lnum_orig,
3141 long *count_orig,
3142 linenr_T *lnum_new,
3143 long *count_new)
3144{
3145 char_u *p;
3146 long oldline, oldcount, newline, newcount;
3147
3148 // Parse unified diff hunk header:
3149 // @@ -oldline,oldcount +newline,newcount @@
3150 p = line;
3151 if (*p++ == '@' && *p++ == '@' && *p++ == ' ' && *p++ == '-')
3152 {
3153 oldline = getdigits(&p);
3154 if (*p == ',')
3155 {
3156 ++p;
3157 oldcount = getdigits(&p);
3158 }
3159 else
3160 oldcount = 1;
3161 if (*p++ == ' ' && *p++ == '+')
3162 {
3163 newline = getdigits(&p);
3164 if (*p == ',')
3165 {
3166 ++p;
3167 newcount = getdigits(&p);
3168 }
3169 else
3170 newcount = 1;
3171 }
3172 else
3173 return FAIL; // invalid diff format
3174
3175 if (oldcount == 0)
3176 oldline += 1;
3177 if (newcount == 0)
3178 newline += 1;
3179 if (newline == 0)
3180 newline = 1;
3181
3182 *lnum_orig = oldline;
3183 *count_orig = oldcount;
3184 *lnum_new = newline;
3185 *count_new = newcount;
3186
3187 return OK;
3188 }
3189
3190 return FAIL;
3191}
3192
3193/*
3194 * Callback function for the xdl_diff() function.
3195 * Stores the diff output in a grow array.
3196 */
3197 static int
3198xdiff_out(void *priv, mmbuffer_t *mb, int nbuf)
3199{
3200 diffout_T *dout = (diffout_T *)priv;
Bram Moolenaare828b762018-09-10 17:51:58 +02003201 char_u *p;
3202
Bram Moolenaarf080d702018-10-31 22:57:26 +01003203 // The header line always comes by itself, text lines in at least two
3204 // parts. We drop the text part.
3205 if (nbuf > 1)
3206 return 0;
3207
3208 // sanity check
3209 if (STRNCMP(mb[0].ptr, "@@ ", 3) != 0)
3210 return 0;
3211
3212 if (ga_grow(&dout->dout_ga, 1) == FAIL)
3213 return -1;
3214 p = vim_strnsave((char_u *)mb[0].ptr, mb[0].size);
3215 if (p == NULL)
3216 return -1;
3217 ((char_u **)dout->dout_ga.ga_data)[dout->dout_ga.ga_len++] = p;
Bram Moolenaare828b762018-09-10 17:51:58 +02003218 return 0;
3219}
3220
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221#endif /* FEAT_DIFF */