blob: 4c0792baae079c36a77f987c23a06b4a4412fab8 [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 Moolenaar071d4272004-06-13 20:20:40 +000024static int diff_busy = FALSE; /* ex_diffgetput() is busy */
25
26/* flags obtained from the 'diffopt' option */
Bram Moolenaare828b762018-09-10 17:51:58 +020027#define DIFF_FILLER 1 // display filler lines
28#define DIFF_ICASE 2 // ignore case
29#define DIFF_IWHITE 4 // ignore change in white space
30#define DIFF_HORIZONTAL 8 // horizontal splits
31#define DIFF_VERTICAL 16 // vertical splits
32#define DIFF_HIDDEN_OFF 32 // diffoff when hidden
33#define DIFF_INTERNAL 64 // use internal xdiff algorithm
Bram Moolenaar071d4272004-06-13 20:20:40 +000034static int diff_flags = DIFF_FILLER;
35
Bram Moolenaare828b762018-09-10 17:51:58 +020036static long diff_algorithm = 0;
37
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#define LBUFLEN 50 /* length of line in diff file */
39
40static int diff_a_works = MAYBE; /* TRUE when "diff -a" works, FALSE when it
41 doesn't work, MAYBE when not checked yet */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010042#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000043static int diff_bin_works = MAYBE; /* TRUE when "diff --binary" works, FALSE
44 when it doesn't work, MAYBE when not
45 checked yet */
46#endif
47
Bram Moolenaare828b762018-09-10 17:51:58 +020048// used for diff input
49typedef struct {
50 char_u *din_fname; // used for external diff
51 mmfile_t din_mmfile; // used for internal diff
52} diffin_T;
53
54// used for diff result
55typedef struct {
56 char_u *dout_fname; // used for external diff
57 garray_T dout_ga; // used for internal diff
58} diffout_T;
59
60// two diff inputs and one result
61typedef struct {
62 diffin_T dio_orig; // original file input
63 diffin_T dio_new; // new file input
64 diffout_T dio_diff; // diff result
65 int dio_internal; // using internal diff
66} diffio_T;
67
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010068static int diff_buf_idx(buf_T *buf);
69static int diff_buf_idx_tp(buf_T *buf, tabpage_T *tp);
70static void diff_mark_adjust_tp(tabpage_T *tp, int idx, linenr_T line1, linenr_T line2, long amount, long amount_after);
71static void diff_check_unchanged(tabpage_T *tp, diff_T *dp);
72static int diff_check_sanity(tabpage_T *tp, diff_T *dp);
73static void diff_redraw(int dofold);
Bram Moolenaare828b762018-09-10 17:51:58 +020074static int check_external_diff(diffio_T *diffio);
75static int diff_file(diffio_T *diffio);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010076static int diff_equal_entry(diff_T *dp, int idx1, int idx2);
77static int diff_cmp(char_u *s1, char_u *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000078#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010079static void diff_fold_update(diff_T *dp, int skip_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#endif
Bram Moolenaare828b762018-09-10 17:51:58 +020081static void diff_read(int idx_orig, int idx_new, diffout_T *fname);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010082static void diff_copy_entry(diff_T *dprev, diff_T *dp, int idx_orig, int idx_new);
83static diff_T *diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp);
Bram Moolenaare828b762018-09-10 17:51:58 +020084static int parse_diff_ed(char_u *line, linenr_T *lnum_orig, long *count_orig, linenr_T *lnum_new, long *count_new);
85static int parse_diff_unified(char_u *line, linenr_T *lnum_orig, long *count_orig, linenr_T *lnum_new, long *count_new);
86static int xdiff_out(void *priv, mmbuffer_t *mb, int nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000087
88#ifndef USE_CR
89# define tag_fgets vim_fgets
90#endif
91
92/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000093 * Called when deleting or unloading a buffer: No longer make a diff with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000094 */
95 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010096diff_buf_delete(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000097{
98 int i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000099 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000100
Bram Moolenaar29323592016-07-24 22:04:11 +0200101 FOR_ALL_TABPAGES(tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000103 i = diff_buf_idx_tp(buf, tp);
104 if (i != DB_COUNT)
105 {
106 tp->tp_diffbuf[i] = NULL;
107 tp->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000108 if (tp == curtab)
109 diff_redraw(TRUE);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 }
112}
113
114/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000115 * Check if the current buffer should be added to or removed from the list of
116 * diff buffers.
117 */
118 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100119diff_buf_adjust(win_T *win)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000120{
121 win_T *wp;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000122 int i;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000123
124 if (!win->w_p_diff)
125 {
126 /* When there is no window showing a diff for this buffer, remove
127 * it from the diffs. */
Bram Moolenaar29323592016-07-24 22:04:11 +0200128 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000129 if (wp->w_buffer == win->w_buffer && wp->w_p_diff)
130 break;
131 if (wp == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000132 {
133 i = diff_buf_idx(win->w_buffer);
134 if (i != DB_COUNT)
135 {
136 curtab->tp_diffbuf[i] = NULL;
137 curtab->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000138 diff_redraw(TRUE);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000139 }
140 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000141 }
142 else
143 diff_buf_add(win->w_buffer);
144}
145
146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147 * Add a buffer to make diffs for.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000148 * Call this when a new buffer is being edited in the current window where
149 * 'diff' is set.
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +0000150 * Marks the current buffer as being part of the diff and requiring updating.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000151 * This must be done before any autocmd, because a command may use info
152 * about the screen contents.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153 */
154 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100155diff_buf_add(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000156{
157 int i;
158
159 if (diff_buf_idx(buf) != DB_COUNT)
160 return; /* It's already there. */
161
162 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000163 if (curtab->tp_diffbuf[i] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000165 curtab->tp_diffbuf[i] = buf;
166 curtab->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000167 diff_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 return;
169 }
170
Bram Moolenaar89eaa412016-07-31 14:17:27 +0200171 EMSGN(_("E96: Cannot diff more than %ld buffers"), DB_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172}
173
174/*
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100175 * Remove all buffers to make diffs for.
176 */
177 static void
178diff_buf_clear(void)
179{
180 int i;
181
182 for (i = 0; i < DB_COUNT; ++i)
183 if (curtab->tp_diffbuf[i] != NULL)
184 {
185 curtab->tp_diffbuf[i] = NULL;
186 curtab->tp_diff_invalid = TRUE;
187 diff_redraw(TRUE);
188 }
189}
190
191/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000192 * Find buffer "buf" in the list of diff buffers for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 * Return its index or DB_COUNT if not found.
194 */
195 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100196diff_buf_idx(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197{
198 int idx;
199
200 for (idx = 0; idx < DB_COUNT; ++idx)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000201 if (curtab->tp_diffbuf[idx] == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202 break;
203 return idx;
204}
205
206/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000207 * Find buffer "buf" in the list of diff buffers for tab page "tp".
208 * Return its index or DB_COUNT if not found.
209 */
210 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100211diff_buf_idx_tp(buf_T *buf, tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000212{
213 int idx;
214
215 for (idx = 0; idx < DB_COUNT; ++idx)
216 if (tp->tp_diffbuf[idx] == buf)
217 break;
218 return idx;
219}
220
221/*
222 * Mark the diff info involving buffer "buf" as invalid, it will be updated
223 * when info is requested.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224 */
225 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100226diff_invalidate(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000227{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000228 tabpage_T *tp;
229 int i;
230
Bram Moolenaar29323592016-07-24 22:04:11 +0200231 FOR_ALL_TABPAGES(tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000233 i = diff_buf_idx_tp(buf, tp);
234 if (i != DB_COUNT)
235 {
236 tp->tp_diff_invalid = TRUE;
237 if (tp == curtab)
238 diff_redraw(TRUE);
239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240 }
241}
242
243/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000244 * Called by mark_adjust(): update line numbers in "curbuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 */
246 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100247diff_mark_adjust(
248 linenr_T line1,
249 linenr_T line2,
250 long amount,
251 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000253 int idx;
254 tabpage_T *tp;
255
256 /* Handle all tab pages that use the current buffer in a diff. */
Bram Moolenaar29323592016-07-24 22:04:11 +0200257 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000258 {
259 idx = diff_buf_idx_tp(curbuf, tp);
260 if (idx != DB_COUNT)
261 diff_mark_adjust_tp(tp, idx, line1, line2, amount, amount_after);
262 }
263}
264
265/*
266 * Update line numbers in tab page "tp" for "curbuf" with index "idx".
267 * This attempts to update the changes as much as possible:
268 * When inserting/deleting lines outside of existing change blocks, create a
269 * new change block and update the line numbers in following blocks.
270 * When inserting/deleting lines in existing change blocks, update them.
271 */
272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100273diff_mark_adjust_tp(
274 tabpage_T *tp,
275 int idx,
276 linenr_T line1,
277 linenr_T line2,
278 long amount,
279 long amount_after)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000280{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 diff_T *dp;
282 diff_T *dprev;
283 diff_T *dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 int i;
285 int inserted, deleted;
286 int n, off;
287 linenr_T last;
288 linenr_T lnum_deleted = line1; /* lnum of remaining deletion */
289 int check_unchanged;
290
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 if (line2 == MAXLNUM)
292 {
293 /* mark_adjust(99, MAXLNUM, 9, 0): insert lines */
294 inserted = amount;
295 deleted = 0;
296 }
297 else if (amount_after > 0)
298 {
299 /* mark_adjust(99, 98, MAXLNUM, 9): a change that inserts lines*/
300 inserted = amount_after;
301 deleted = 0;
302 }
303 else
304 {
305 /* mark_adjust(98, 99, MAXLNUM, -2): delete lines */
306 inserted = 0;
307 deleted = -amount_after;
308 }
309
310 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000311 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000312 for (;;)
313 {
314 /* If the change is after the previous diff block and before the next
315 * diff block, thus not touching an existing change, create a new diff
316 * block. Don't do this when ex_diffgetput() is busy. */
317 if ((dp == NULL || dp->df_lnum[idx] - 1 > line2
318 || (line2 == MAXLNUM && dp->df_lnum[idx] > line1))
319 && (dprev == NULL
320 || dprev->df_lnum[idx] + dprev->df_count[idx] < line1)
321 && !diff_busy)
322 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000323 dnext = diff_alloc_new(tp, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 if (dnext == NULL)
325 return;
326
327 dnext->df_lnum[idx] = line1;
328 dnext->df_count[idx] = inserted;
329 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000330 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 {
332 if (dprev == NULL)
333 dnext->df_lnum[i] = line1;
334 else
335 dnext->df_lnum[i] = line1
336 + (dprev->df_lnum[i] + dprev->df_count[i])
337 - (dprev->df_lnum[idx] + dprev->df_count[idx]);
338 dnext->df_count[i] = deleted;
339 }
340 }
341
342 /* if at end of the list, quit */
343 if (dp == NULL)
344 break;
345
346 /*
347 * Check for these situations:
348 * 1 2 3
349 * 1 2 3
350 * line1 2 3 4 5
351 * 2 3 4 5
352 * 2 3 4 5
353 * line2 2 3 4 5
354 * 3 5 6
355 * 3 5 6
356 */
357 /* compute last line of this change */
358 last = dp->df_lnum[idx] + dp->df_count[idx] - 1;
359
360 /* 1. change completely above line1: nothing to do */
361 if (last >= line1 - 1)
362 {
363 /* 6. change below line2: only adjust for amount_after; also when
364 * "deleted" became zero when deleted all lines between two diffs */
365 if (dp->df_lnum[idx] - (deleted + inserted != 0) > line2)
366 {
367 if (amount_after == 0)
368 break; /* nothing left to change */
369 dp->df_lnum[idx] += amount_after;
370 }
371 else
372 {
373 check_unchanged = FALSE;
374
375 /* 2. 3. 4. 5.: inserted/deleted lines touching this diff. */
376 if (deleted > 0)
377 {
378 if (dp->df_lnum[idx] >= line1)
379 {
380 off = dp->df_lnum[idx] - lnum_deleted;
381 if (last <= line2)
382 {
383 /* 4. delete all lines of diff */
384 if (dp->df_next != NULL
385 && dp->df_next->df_lnum[idx] - 1 <= line2)
386 {
387 /* delete continues in next diff, only do
388 * lines until that one */
389 n = dp->df_next->df_lnum[idx] - lnum_deleted;
390 deleted -= n;
391 n -= dp->df_count[idx];
392 lnum_deleted = dp->df_next->df_lnum[idx];
393 }
394 else
395 n = deleted - dp->df_count[idx];
396 dp->df_count[idx] = 0;
397 }
398 else
399 {
400 /* 5. delete lines at or just before top of diff */
401 n = off;
402 dp->df_count[idx] -= line2 - dp->df_lnum[idx] + 1;
403 check_unchanged = TRUE;
404 }
405 dp->df_lnum[idx] = line1;
406 }
407 else
408 {
409 off = 0;
410 if (last < line2)
411 {
412 /* 2. delete at end of of diff */
413 dp->df_count[idx] -= last - lnum_deleted + 1;
414 if (dp->df_next != NULL
415 && dp->df_next->df_lnum[idx] - 1 <= line2)
416 {
417 /* delete continues in next diff, only do
418 * lines until that one */
419 n = dp->df_next->df_lnum[idx] - 1 - last;
420 deleted -= dp->df_next->df_lnum[idx]
421 - lnum_deleted;
422 lnum_deleted = dp->df_next->df_lnum[idx];
423 }
424 else
425 n = line2 - last;
426 check_unchanged = TRUE;
427 }
428 else
429 {
430 /* 3. delete lines inside the diff */
431 n = 0;
432 dp->df_count[idx] -= deleted;
433 }
434 }
435
436 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000437 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 {
439 dp->df_lnum[i] -= off;
440 dp->df_count[i] += n;
441 }
442 }
443 else
444 {
445 if (dp->df_lnum[idx] <= line1)
446 {
447 /* inserted lines somewhere in this diff */
448 dp->df_count[idx] += inserted;
449 check_unchanged = TRUE;
450 }
451 else
452 /* inserted lines somewhere above this diff */
453 dp->df_lnum[idx] += inserted;
454 }
455
456 if (check_unchanged)
457 /* Check if inserted lines are equal, may reduce the
458 * size of the diff. TODO: also check for equal lines
459 * in the middle and perhaps split the block. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000460 diff_check_unchanged(tp, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000461 }
462 }
463
464 /* check if this block touches the previous one, may merge them. */
465 if (dprev != NULL && dprev->df_lnum[idx] + dprev->df_count[idx]
466 == dp->df_lnum[idx])
467 {
468 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000469 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 dprev->df_count[i] += dp->df_count[i];
471 dprev->df_next = dp->df_next;
472 vim_free(dp);
473 dp = dprev->df_next;
474 }
475 else
476 {
477 /* Advance to next entry. */
478 dprev = dp;
479 dp = dp->df_next;
480 }
481 }
482
483 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000484 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 while (dp != NULL)
486 {
487 /* All counts are zero, remove this entry. */
488 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000489 if (tp->tp_diffbuf[i] != NULL && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000490 break;
491 if (i == DB_COUNT)
492 {
493 dnext = dp->df_next;
494 vim_free(dp);
495 dp = dnext;
496 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000497 tp->tp_first_diff = dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 else
499 dprev->df_next = dnext;
500 }
501 else
502 {
503 /* Advance to next entry. */
504 dprev = dp;
505 dp = dp->df_next;
506 }
507
508 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000509
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000510 if (tp == curtab)
511 {
512 diff_redraw(TRUE);
513
514 /* Need to recompute the scroll binding, may remove or add filler
515 * lines (e.g., when adding lines above w_topline). But it's slow when
516 * making many changes, postpone until redrawing. */
517 diff_need_scrollbind = TRUE;
518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519}
520
521/*
522 * Allocate a new diff block and link it between "dprev" and "dp".
523 */
524 static diff_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100525diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526{
527 diff_T *dnew;
528
529 dnew = (diff_T *)alloc((unsigned)sizeof(diff_T));
530 if (dnew != NULL)
531 {
532 dnew->df_next = dp;
533 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000534 tp->tp_first_diff = dnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 else
536 dprev->df_next = dnew;
537 }
538 return dnew;
539}
540
541/*
542 * Check if the diff block "dp" can be made smaller for lines at the start and
543 * end that are equal. Called after inserting lines.
544 * This may result in a change where all buffers have zero lines, the caller
545 * must take care of removing it.
546 */
547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100548diff_check_unchanged(tabpage_T *tp, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549{
550 int i_org;
551 int i_new;
552 int off_org, off_new;
553 char_u *line_org;
554 int dir = FORWARD;
555
556 /* Find the first buffers, use it as the original, compare the other
557 * buffer lines against this one. */
558 for (i_org = 0; i_org < DB_COUNT; ++i_org)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000559 if (tp->tp_diffbuf[i_org] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560 break;
561 if (i_org == DB_COUNT) /* safety check */
562 return;
563
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000564 if (diff_check_sanity(tp, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 return;
566
567 /* First check lines at the top, then at the bottom. */
568 off_org = 0;
569 off_new = 0;
570 for (;;)
571 {
572 /* Repeat until a line is found which is different or the number of
573 * lines has become zero. */
574 while (dp->df_count[i_org] > 0)
575 {
576 /* Copy the line, the next ml_get() will invalidate it. */
577 if (dir == BACKWARD)
578 off_org = dp->df_count[i_org] - 1;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000579 line_org = vim_strsave(ml_get_buf(tp->tp_diffbuf[i_org],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 dp->df_lnum[i_org] + off_org, FALSE));
581 if (line_org == NULL)
582 return;
583 for (i_new = i_org + 1; i_new < DB_COUNT; ++i_new)
584 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000585 if (tp->tp_diffbuf[i_new] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 continue;
587 if (dir == BACKWARD)
588 off_new = dp->df_count[i_new] - 1;
589 /* if other buffer doesn't have this line, it was inserted */
590 if (off_new < 0 || off_new >= dp->df_count[i_new])
591 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000592 if (diff_cmp(line_org, ml_get_buf(tp->tp_diffbuf[i_new],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 dp->df_lnum[i_new] + off_new, FALSE)) != 0)
594 break;
595 }
596 vim_free(line_org);
597
598 /* Stop when a line isn't equal in all diff buffers. */
599 if (i_new != DB_COUNT)
600 break;
601
602 /* Line matched in all buffers, remove it from the diff. */
603 for (i_new = i_org; i_new < DB_COUNT; ++i_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000604 if (tp->tp_diffbuf[i_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 {
606 if (dir == FORWARD)
607 ++dp->df_lnum[i_new];
608 --dp->df_count[i_new];
609 }
610 }
611 if (dir == BACKWARD)
612 break;
613 dir = BACKWARD;
614 }
615}
616
617/*
618 * Check if a diff block doesn't contain invalid line numbers.
619 * This can happen when the diff program returns invalid results.
620 */
621 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100622diff_check_sanity(tabpage_T *tp, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623{
624 int i;
625
626 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000627 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 if (dp->df_lnum[i] + dp->df_count[i] - 1
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000629 > tp->tp_diffbuf[i]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 return FAIL;
631 return OK;
632}
633
634/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000635 * Mark all diff buffers in the current tab page for redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000636 */
637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100638diff_redraw(
639 int dofold) /* also recompute the folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000640{
641 win_T *wp;
642 int n;
643
Bram Moolenaar29323592016-07-24 22:04:11 +0200644 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 if (wp->w_p_diff)
646 {
Bram Moolenaar60f83772006-03-12 21:52:47 +0000647 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000648#ifdef FEAT_FOLDING
649 if (dofold && foldmethodIsDiff(wp))
650 foldUpdateAll(wp);
651#endif
652 /* A change may have made filler lines invalid, need to take care
653 * of that for other windows. */
Bram Moolenaara80888d2012-10-21 22:18:21 +0200654 n = diff_check(wp, wp->w_topline);
655 if ((wp != curwin && wp->w_topfill > 0) || n > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000657 if (wp->w_topfill > n)
658 wp->w_topfill = (n < 0 ? 0 : n);
Bram Moolenaara80888d2012-10-21 22:18:21 +0200659 else if (n > 0 && n > wp->w_topfill)
660 wp->w_topfill = n;
Bram Moolenaar846a2ff2014-05-28 11:35:37 +0200661 check_topfill(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662 }
663 }
664}
665
Bram Moolenaare828b762018-09-10 17:51:58 +0200666 static void
667clear_diffin(diffin_T *din)
668{
669 if (din->din_fname == NULL)
670 {
671 vim_free(din->din_mmfile.ptr);
672 din->din_mmfile.ptr = NULL;
673 }
674 else
675 mch_remove(din->din_fname);
676}
677
678 static void
679clear_diffout(diffout_T *dout)
680{
681 if (dout->dout_fname == NULL)
682 ga_clear_strings(&dout->dout_ga);
683 else
684 mch_remove(dout->dout_fname);
685}
686
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687/*
Bram Moolenaare828b762018-09-10 17:51:58 +0200688 * Write buffer "buf" to a memory buffer.
689 * Return FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 */
691 static int
Bram Moolenaare828b762018-09-10 17:51:58 +0200692diff_write_buffer(buf_T *buf, diffin_T *din)
693{
694 linenr_T lnum;
695 char_u *s;
696 long len = 0;
697 char_u *ptr;
698
699 // xdiff requires one big block of memory with all the text.
700 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
701 len += STRLEN(ml_get_buf(buf, lnum, FALSE)) + 1;
702 ptr = lalloc(len, TRUE);
703 if (ptr == NULL)
704 {
705 // Allocating memory failed. This can happen, because we try to read
706 // the whole buffer text into memory. Set the failed flag, the diff
707 // will be retried with external diff. The flag is never reset.
708 buf->b_diff_failed = TRUE;
709 if (p_verbose > 0)
710 {
711 verbose_enter();
712 smsg((char_u *)
713 _("Not enough memory to use internal diff for buffer \"%s\""),
714 buf->b_fname);
715 verbose_leave();
716 }
717 return FAIL;
718 }
719 din->din_mmfile.ptr = (char *)ptr;
720 din->din_mmfile.size = len;
721
722 len = 0;
723 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
724 {
725 for (s = ml_get_buf(buf, lnum, FALSE); *s != NUL; )
726 {
727 if (diff_flags & DIFF_ICASE)
728 {
729 int c;
730
731 // xdiff doesn't support ignoring case, fold-case the text.
732#ifdef FEAT_MBYTE
733 int orig_len;
734 char_u cbuf[MB_MAXBYTES + 1];
735
736 c = PTR2CHAR(s);
737 c = enc_utf8 ? utf_fold(c) : MB_TOLOWER(c);
738 orig_len = MB_PTR2LEN(s);
739 if (mb_char2bytes(c, cbuf) != orig_len)
740 // TODO: handle byte length difference
741 mch_memmove(ptr + len, s, orig_len);
742 else
743 mch_memmove(ptr + len, cbuf, orig_len);
744
745 s += orig_len;
746 len += orig_len;
747#else
748 c = *s++;
749 ptr[len++] = TOLOWER_LOC(c);
750#endif
751 }
752 else
753 ptr[len++] = *s++;
754 }
755 ptr[len++] = NL;
756 }
757 return OK;
758}
759
760/*
761 * Write buffer "buf" to file or memory buffer.
762 * Return FAIL for failure.
763 */
764 static int
765diff_write(buf_T *buf, diffin_T *din)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766{
767 int r;
768 char_u *save_ff;
769
Bram Moolenaare828b762018-09-10 17:51:58 +0200770 if (din->din_fname == NULL)
771 return diff_write_buffer(buf, din);
772
773 // Always use 'fileformat' set to "unix".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 save_ff = buf->b_p_ff;
775 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX);
Bram Moolenaare828b762018-09-10 17:51:58 +0200776 r = buf_write(buf, din->din_fname, NULL,
777 (linenr_T)1, buf->b_ml.ml_line_count,
778 NULL, FALSE, FALSE, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 free_string_option(buf->b_p_ff);
780 buf->b_p_ff = save_ff;
781 return r;
782}
783
784/*
Bram Moolenaare828b762018-09-10 17:51:58 +0200785 * Update the diffs for all buffers involved.
786 */
787 static void
788diff_try_update(
789 diffio_T *dio,
790 int idx_orig,
791 exarg_T *eap) // "eap" can be NULL
792{
793 buf_T *buf;
794 int idx_new;
795
796 if (dio->dio_internal)
797 {
798 ga_init2(&dio->dio_diff.dout_ga, sizeof(char *), 1000);
799 }
800 else
801 {
802 // We need three temp file names.
803 dio->dio_orig.din_fname = vim_tempname('o', TRUE);
804 dio->dio_new.din_fname = vim_tempname('n', TRUE);
805 dio->dio_diff.dout_fname = vim_tempname('d', TRUE);
806 if (dio->dio_orig.din_fname == NULL
807 || dio->dio_new.din_fname == NULL
808 || dio->dio_diff.dout_fname == NULL)
809 goto theend;
810 }
811
812 // Check external diff is actually working.
813 if (!dio->dio_internal && check_external_diff(dio) == FAIL)
814 goto theend;
815
816 // :diffupdate!
817 if (eap != NULL && eap->forceit)
818 for (idx_new = idx_orig; idx_new < DB_COUNT; ++idx_new)
819 {
820 buf = curtab->tp_diffbuf[idx_new];
821 if (buf_valid(buf))
822 buf_check_timestamp(buf, FALSE);
823 }
824
825 // Write the first buffer to a tempfile or mmfile_t.
826 buf = curtab->tp_diffbuf[idx_orig];
827 if (diff_write(buf, &dio->dio_orig) == FAIL)
828 goto theend;
829
830 // Make a difference between the first buffer and every other.
831 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
832 {
833 buf = curtab->tp_diffbuf[idx_new];
834 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
835 continue; // skip buffer that isn't loaded
836
837 // Write the other buffer and diff with the first one.
838 if (diff_write(buf, &dio->dio_new) == FAIL)
839 continue;
840 if (diff_file(dio) == FAIL)
841 continue;
842
843 // Read the diff output and add each entry to the diff list.
844 diff_read(idx_orig, idx_new, &dio->dio_diff);
845
846 clear_diffin(&dio->dio_new);
847 clear_diffout(&dio->dio_diff);
848 }
849 clear_diffin(&dio->dio_orig);
850
851theend:
852 vim_free(dio->dio_orig.din_fname);
853 vim_free(dio->dio_new.din_fname);
854 vim_free(dio->dio_diff.dout_fname);
855}
856
857/*
858 * Return TRUE if the options are set to use the internal diff library.
859 * Note that if the internal diff failed for one of the buffers, the external
860 * diff will be used anyway.
861 */
862 static int
863diff_internal(void)
864{
865 return (diff_flags & DIFF_INTERNAL) != 0 && *p_dex == NUL;
866}
867
868/*
869 * Return TRUE if the internal diff failed for one of the diff buffers.
870 */
871 static int
872diff_internal_failed(void)
873{
874 int idx;
875
876 // Only need to do something when there is another buffer.
877 for (idx = 0; idx < DB_COUNT; ++idx)
878 if (curtab->tp_diffbuf[idx] != NULL
879 && curtab->tp_diffbuf[idx]->b_diff_failed)
880 return TRUE;
881 return FALSE;
882}
883
884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 * Completely update the diffs for the buffers involved.
886 * This uses the ordinary "diff" command.
887 * The buffers are written to a file, also for unmodified buffers (the file
888 * could have been produced by autocommands, e.g. the netrw plugin).
889 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 void
Bram Moolenaare828b762018-09-10 17:51:58 +0200891ex_diffupdate(exarg_T *eap) // "eap" can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 int idx_orig;
894 int idx_new;
Bram Moolenaare828b762018-09-10 17:51:58 +0200895 diffio_T diffio;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896
Bram Moolenaare828b762018-09-10 17:51:58 +0200897 // Delete all diffblocks.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000898 diff_clear(curtab);
899 curtab->tp_diff_invalid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900
Bram Moolenaare828b762018-09-10 17:51:58 +0200901 // Use the first buffer as the original text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 for (idx_orig = 0; idx_orig < DB_COUNT; ++idx_orig)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000903 if (curtab->tp_diffbuf[idx_orig] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 break;
905 if (idx_orig == DB_COUNT)
906 return;
907
Bram Moolenaare828b762018-09-10 17:51:58 +0200908 // Only need to do something when there is another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000909 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000910 if (curtab->tp_diffbuf[idx_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911 break;
912 if (idx_new == DB_COUNT)
913 return;
914
Bram Moolenaare828b762018-09-10 17:51:58 +0200915 // Only use the internal method if it did not fail for one of the buffers.
916 vim_memset(&diffio, 0, sizeof(diffio));
917 diffio.dio_internal = diff_internal() && !diff_internal_failed();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918
Bram Moolenaare828b762018-09-10 17:51:58 +0200919 diff_try_update(&diffio, idx_orig, eap);
920 if (diffio.dio_internal && diff_internal_failed())
921 {
922 // Internal diff failed, use external diff instead.
923 vim_memset(&diffio, 0, sizeof(diffio));
924 diff_try_update(&diffio, idx_orig, eap);
925 }
926
927 // force updating cursor position on screen
928 curwin->w_valid_cursor.lnum = 0;
929
930 diff_redraw(TRUE);
931}
932
933/*
934 * Do a quick test if "diff" really works. Otherwise it looks like there
935 * are no differences. Can't use the return value, it's non-zero when
936 * there are differences.
937 */
938 static int
939check_external_diff(diffio_T *diffio)
940{
941 FILE *fd;
942 int ok;
943 int io_error = FALSE;
944
945 // May try twice, first with "-a" and then without.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 for (;;)
947 {
948 ok = FALSE;
Bram Moolenaare828b762018-09-10 17:51:58 +0200949 fd = mch_fopen((char *)diffio->dio_orig.din_fname, "w");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000950 if (fd == NULL)
951 io_error = TRUE;
952 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000954 if (fwrite("line1\n", (size_t)6, (size_t)1, fd) != 1)
955 io_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 fclose(fd);
Bram Moolenaare828b762018-09-10 17:51:58 +0200957 fd = mch_fopen((char *)diffio->dio_new.din_fname, "w");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000958 if (fd == NULL)
959 io_error = TRUE;
960 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000962 if (fwrite("line2\n", (size_t)6, (size_t)1, fd) != 1)
963 io_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000964 fclose(fd);
Bram Moolenaare828b762018-09-10 17:51:58 +0200965 fd = NULL;
966 if (diff_file(diffio) == OK)
967 fd = mch_fopen((char *)diffio->dio_diff.dout_fname, "r");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000968 if (fd == NULL)
969 io_error = TRUE;
970 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 {
972 char_u linebuf[LBUFLEN];
973
974 for (;;)
975 {
976 /* There must be a line that contains "1c1". */
977 if (tag_fgets(linebuf, LBUFLEN, fd))
978 break;
979 if (STRNCMP(linebuf, "1c1", 3) == 0)
980 ok = TRUE;
981 }
982 fclose(fd);
983 }
Bram Moolenaare828b762018-09-10 17:51:58 +0200984 mch_remove(diffio->dio_diff.dout_fname);
985 mch_remove(diffio->dio_new.din_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 }
Bram Moolenaare828b762018-09-10 17:51:58 +0200987 mch_remove(diffio->dio_orig.din_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000988 }
989
990#ifdef FEAT_EVAL
991 /* When using 'diffexpr' break here. */
992 if (*p_dex != NUL)
993 break;
994#endif
995
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100996#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 /* If the "-a" argument works, also check if "--binary" works. */
998 if (ok && diff_a_works == MAYBE && diff_bin_works == MAYBE)
999 {
1000 diff_a_works = TRUE;
1001 diff_bin_works = TRUE;
1002 continue;
1003 }
1004 if (!ok && diff_a_works == TRUE && diff_bin_works == TRUE)
1005 {
1006 /* Tried --binary, but it failed. "-a" works though. */
1007 diff_bin_works = FALSE;
1008 ok = TRUE;
1009 }
1010#endif
1011
1012 /* If we checked if "-a" works already, break here. */
1013 if (diff_a_works != MAYBE)
1014 break;
1015 diff_a_works = ok;
1016
1017 /* If "-a" works break here, otherwise retry without "-a". */
1018 if (ok)
1019 break;
1020 }
1021 if (!ok)
1022 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001023 if (io_error)
1024 EMSG(_("E810: Cannot read or write temp files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 EMSG(_("E97: Cannot create diffs"));
1026 diff_a_works = MAYBE;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001027#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 diff_bin_works = MAYBE;
1029#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001030 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001032 return OK;
1033}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034
Bram Moolenaare828b762018-09-10 17:51:58 +02001035/*
1036 * Invoke the xdiff function.
1037 */
1038 static int
1039diff_file_internal(diffio_T *diffio)
1040{
1041 xpparam_t param;
1042 xdemitconf_t emit_cfg;
1043 xdemitcb_t emit_cb;
Bram Moolenaarbd1d5602012-05-18 18:47:17 +02001044
Bram Moolenaare828b762018-09-10 17:51:58 +02001045 vim_memset(&param, 0, sizeof(param));
1046 vim_memset(&emit_cfg, 0, sizeof(emit_cfg));
1047 vim_memset(&emit_cb, 0, sizeof(emit_cb));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048
Bram Moolenaare828b762018-09-10 17:51:58 +02001049 param.flags = diff_algorithm;
1050
1051 if (diff_flags & DIFF_IWHITE)
1052 param.flags |= XDF_IGNORE_WHITESPACE_CHANGE;
1053
1054 emit_cfg.ctxlen = 0; // don't need any diff_context here
1055 emit_cb.priv = &diffio->dio_diff;
1056 emit_cb.outf = xdiff_out;
1057 if (xdl_diff(&diffio->dio_orig.din_mmfile,
1058 &diffio->dio_new.din_mmfile,
1059 &param, &emit_cfg, &emit_cb) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001061 EMSG(_("E960: Problem creating the internal diff"));
1062 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001064 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065}
1066
1067/*
1068 * Make a diff between files "tmp_orig" and "tmp_new", results in "tmp_diff".
Bram Moolenaare828b762018-09-10 17:51:58 +02001069 * return OK or FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 */
Bram Moolenaare828b762018-09-10 17:51:58 +02001071 static int
1072diff_file(diffio_T *dio)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073{
1074 char_u *cmd;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001075 size_t len;
Bram Moolenaare828b762018-09-10 17:51:58 +02001076 char_u *tmp_orig = dio->dio_orig.din_fname;
1077 char_u *tmp_new = dio->dio_new.din_fname;
1078 char_u *tmp_diff = dio->dio_diff.dout_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079
1080#ifdef FEAT_EVAL
1081 if (*p_dex != NUL)
Bram Moolenaare828b762018-09-10 17:51:58 +02001082 {
1083 // Use 'diffexpr' to generate the diff file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 eval_diff(tmp_orig, tmp_new, tmp_diff);
Bram Moolenaare828b762018-09-10 17:51:58 +02001085 return OK;
1086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 else
1088#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001089 // Use xdiff for generating the diff.
1090 if (dio->dio_internal)
1091 {
1092 return diff_file_internal(dio);
1093 }
1094 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001096 len = STRLEN(tmp_orig) + STRLEN(tmp_new)
1097 + STRLEN(tmp_diff) + STRLEN(p_srr) + 27;
1098 cmd = alloc((unsigned)len);
Bram Moolenaare828b762018-09-10 17:51:58 +02001099 if (cmd == NULL)
1100 return FAIL;
Bram Moolenaar95fb60a2005-03-08 22:29:20 +00001101
Bram Moolenaare828b762018-09-10 17:51:58 +02001102 // We don't want $DIFF_OPTIONS to get in the way.
1103 if (getenv("DIFF_OPTIONS"))
1104 vim_setenv((char_u *)"DIFF_OPTIONS", (char_u *)"");
1105
1106 // Build the diff command and execute it. Always use -a, binary
1107 // differences are of no use. Ignore errors, diff returns
1108 // non-zero when differences have been found.
1109 vim_snprintf((char *)cmd, len, "diff %s%s%s%s%s %s",
1110 diff_a_works == FALSE ? "" : "-a ",
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001111#if defined(MSWIN)
Bram Moolenaare828b762018-09-10 17:51:58 +02001112 diff_bin_works == TRUE ? "--binary " : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113#else
Bram Moolenaare828b762018-09-10 17:51:58 +02001114 "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001115#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001116 (diff_flags & DIFF_IWHITE) ? "-b " : "",
1117 (diff_flags & DIFF_ICASE) ? "-i " : "",
1118 tmp_orig, tmp_new);
1119 append_redir(cmd, (int)len, p_srr, tmp_diff);
1120 block_autocmds(); // avoid ShellCmdPost stuff
1121 (void)call_shell(cmd, SHELL_FILTER|SHELL_SILENT|SHELL_DOOUT);
1122 unblock_autocmds();
1123 vim_free(cmd);
1124 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 }
1126}
1127
1128/*
1129 * Create a new version of a file from the current buffer and a diff file.
1130 * The buffer is written to a file, also for unmodified buffers (the file
1131 * could have been produced by autocommands, e.g. the netrw plugin).
1132 */
1133 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001134ex_diffpatch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135{
1136 char_u *tmp_orig; /* name of original temp file */
1137 char_u *tmp_new; /* name of patched temp file */
1138 char_u *buf = NULL;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001139 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 win_T *old_curwin = curwin;
1141 char_u *newname = NULL; /* name of patched file buffer */
1142#ifdef UNIX
1143 char_u dirbuf[MAXPATHL];
1144 char_u *fullname = NULL;
1145#endif
1146#ifdef FEAT_BROWSE
1147 char_u *browseFile = NULL;
1148 int browse_flag = cmdmod.browse;
1149#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02001150 stat_T st;
Bram Moolenaara95ab322017-03-11 19:21:53 +01001151 char_u *esc_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152
1153#ifdef FEAT_BROWSE
1154 if (cmdmod.browse)
1155 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001156 browseFile = do_browse(0, (char_u *)_("Patch file"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02001157 eap->arg, NULL, NULL,
1158 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 if (browseFile == NULL)
1160 return; /* operation cancelled */
1161 eap->arg = browseFile;
1162 cmdmod.browse = FALSE; /* don't let do_ecmd() browse again */
1163 }
1164#endif
1165
1166 /* We need two temp file names. */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001167 tmp_orig = vim_tempname('o', FALSE);
1168 tmp_new = vim_tempname('n', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 if (tmp_orig == NULL || tmp_new == NULL)
1170 goto theend;
1171
1172 /* Write the current buffer to "tmp_orig". */
1173 if (buf_write(curbuf, tmp_orig, NULL,
1174 (linenr_T)1, curbuf->b_ml.ml_line_count,
1175 NULL, FALSE, FALSE, FALSE, TRUE) == FAIL)
1176 goto theend;
1177
1178#ifdef UNIX
1179 /* Get the absolute path of the patchfile, changing directory below. */
1180 fullname = FullName_save(eap->arg, FALSE);
1181#endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001182 esc_name = vim_strsave_shellescape(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183# ifdef UNIX
Bram Moolenaara95ab322017-03-11 19:21:53 +01001184 fullname != NULL ? fullname :
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185# endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001186 eap->arg, TRUE, TRUE);
1187 if (esc_name == NULL)
1188 goto theend;
1189 buflen = STRLEN(tmp_orig) + STRLEN(esc_name) + STRLEN(tmp_new) + 16;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001190 buf = alloc((unsigned)buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (buf == NULL)
1192 goto theend;
1193
1194#ifdef UNIX
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +00001195 /* Temporarily chdir to /tmp, to avoid patching files in the current
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 * directory when the patch file contains more than one patch. When we
1197 * have our own temp dir use that instead, it will be cleaned up when we
1198 * exit (any .rej files created). Don't change directory if we can't
1199 * return to the current. */
1200 if (mch_dirname(dirbuf, MAXPATHL) != OK || mch_chdir((char *)dirbuf) != 0)
1201 dirbuf[0] = NUL;
1202 else
1203 {
1204# ifdef TEMPDIRNAMES
1205 if (vim_tempdir != NULL)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001206 ignored = mch_chdir((char *)vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 else
1208# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001209 ignored = mch_chdir("/tmp");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 shorten_fnames(TRUE);
1211 }
1212#endif
1213
1214#ifdef FEAT_EVAL
1215 if (*p_pex != NUL)
1216 /* Use 'patchexpr' to generate the new file. */
1217 eval_patch(tmp_orig,
1218# ifdef UNIX
1219 fullname != NULL ? fullname :
1220# endif
1221 eap->arg, tmp_new);
1222 else
1223#endif
1224 {
1225 /* Build the patch command and execute it. Ignore errors. Switch to
1226 * cooked mode to allow the user to respond to prompts. */
Bram Moolenaara95ab322017-03-11 19:21:53 +01001227 vim_snprintf((char *)buf, buflen, "patch -o %s %s < %s",
1228 tmp_new, tmp_orig, esc_name);
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001229 block_autocmds(); /* Avoid ShellCmdPost stuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 (void)call_shell(buf, SHELL_FILTER | SHELL_COOKED);
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001231 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 }
1233
1234#ifdef UNIX
1235 if (dirbuf[0] != NUL)
1236 {
1237 if (mch_chdir((char *)dirbuf) != 0)
1238 EMSG(_(e_prev_dir));
1239 shorten_fnames(TRUE);
1240 }
1241#endif
1242
1243 /* patch probably has written over the screen */
1244 redraw_later(CLEAR);
1245
1246 /* Delete any .orig or .rej file created. */
1247 STRCPY(buf, tmp_new);
1248 STRCAT(buf, ".orig");
1249 mch_remove(buf);
1250 STRCPY(buf, tmp_new);
1251 STRCAT(buf, ".rej");
1252 mch_remove(buf);
1253
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001254 /* Only continue if the output file was created. */
1255 if (mch_stat((char *)tmp_new, &st) < 0 || st.st_size == 0)
1256 EMSG(_("E816: Cannot read patch output"));
1257 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001259 if (curbuf->b_fname != NULL)
1260 {
1261 newname = vim_strnsave(curbuf->b_fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 (int)(STRLEN(curbuf->b_fname) + 4));
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001263 if (newname != NULL)
1264 STRCAT(newname, ".new");
1265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266
1267#ifdef FEAT_GUI
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001268 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269#endif
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001270 /* don't use a new tab page, each tab page has its own diffs */
1271 cmdmod.tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001272
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001273 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001275 /* Pretend it was a ":split fname" command */
1276 eap->cmdidx = CMD_split;
1277 eap->arg = tmp_new;
1278 do_exedit(eap, old_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001280 /* check that split worked and editing tmp_new */
1281 if (curwin != old_curwin && win_valid(old_curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001283 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1284 diff_win_options(curwin, TRUE);
1285 diff_win_options(old_curwin, TRUE);
1286
1287 if (newname != NULL)
1288 {
1289 /* do a ":file filename.new" on the patched buffer */
1290 eap->arg = newname;
1291 ex_file(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001293 /* Do filetype detection with the new name. */
1294 if (au_has_group((char_u *)"filetypedetect"))
1295 do_cmdline_cmd((char_u *)":doau filetypedetect BufRead");
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 }
1298 }
1299 }
1300
1301theend:
1302 if (tmp_orig != NULL)
1303 mch_remove(tmp_orig);
1304 vim_free(tmp_orig);
1305 if (tmp_new != NULL)
1306 mch_remove(tmp_new);
1307 vim_free(tmp_new);
1308 vim_free(newname);
1309 vim_free(buf);
1310#ifdef UNIX
1311 vim_free(fullname);
1312#endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001313 vim_free(esc_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314#ifdef FEAT_BROWSE
1315 vim_free(browseFile);
1316 cmdmod.browse = browse_flag;
1317#endif
1318}
1319
1320/*
1321 * Split the window and edit another file, setting options to show the diffs.
1322 */
1323 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001324ex_diffsplit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325{
1326 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001327 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001329 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001330#ifdef FEAT_GUI
1331 need_mouse_correct = TRUE;
1332#endif
Bram Moolenaar46328f92016-08-28 15:39:57 +02001333 /* Need to compute w_fraction when no redraw happened yet. */
1334 validate_cursor();
1335 set_fraction(curwin);
1336
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001337 /* don't use a new tab page, each tab page has its own diffs */
1338 cmdmod.tab = 0;
1339
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001340 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341 {
1342 /* Pretend it was a ":split fname" command */
1343 eap->cmdidx = CMD_split;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001344 curwin->w_p_diff = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 do_exedit(eap, old_curwin);
1346
1347 if (curwin != old_curwin) /* split must have worked */
1348 {
1349 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1350 diff_win_options(curwin, TRUE);
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001351 if (win_valid(old_curwin))
1352 {
1353 diff_win_options(old_curwin, TRUE);
1354
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001355 if (bufref_valid(&old_curbuf))
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001356 /* Move the cursor position to that of the old window. */
1357 curwin->w_cursor.lnum = diff_get_corresponding_line(
Bram Moolenaar025e3e02016-10-18 14:50:18 +02001358 old_curbuf.br_buf, old_curwin->w_cursor.lnum);
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001359 }
Bram Moolenaar46328f92016-08-28 15:39:57 +02001360 /* Now that lines are folded scroll to show the cursor at the same
1361 * relative position. */
1362 scroll_to_fraction(curwin, curwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 }
1364 }
1365}
1366
1367/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001368 * Set options to show diffs for the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001371ex_diffthis(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372{
1373 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1374 diff_win_options(curwin, TRUE);
1375}
1376
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001377 static void
1378set_diff_option(win_T *wp, int value)
1379{
1380 win_T *old_curwin = curwin;
1381
1382 curwin = wp;
1383 curbuf = curwin->w_buffer;
1384 ++curbuf_lock;
1385 set_option_value((char_u *)"diff", (long)value, NULL, OPT_LOCAL);
1386 --curbuf_lock;
1387 curwin = old_curwin;
1388 curbuf = curwin->w_buffer;
1389}
1390
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391/*
1392 * Set options in window "wp" for diff mode.
1393 */
1394 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001395diff_win_options(
1396 win_T *wp,
1397 int addbuf) /* Add buffer to diff. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398{
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001399# ifdef FEAT_FOLDING
1400 win_T *old_curwin = curwin;
1401
1402 /* close the manually opened folds */
1403 curwin = wp;
1404 newFoldLevel();
1405 curwin = old_curwin;
1406# endif
1407
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001408 /* Use 'scrollbind' and 'cursorbind' when available */
Bram Moolenaar43929962015-07-03 15:06:56 +02001409 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001410 wp->w_p_scb_save = wp->w_p_scb;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001411 wp->w_p_scb = TRUE;
Bram Moolenaar43929962015-07-03 15:06:56 +02001412 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001413 wp->w_p_crb_save = wp->w_p_crb;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001414 wp->w_p_crb = TRUE;
Bram Moolenaar43929962015-07-03 15:06:56 +02001415 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001416 wp->w_p_wrap_save = wp->w_p_wrap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 wp->w_p_wrap = FALSE;
1418# ifdef FEAT_FOLDING
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001419 curwin = wp;
1420 curbuf = curwin->w_buffer;
Bram Moolenaar43929962015-07-03 15:06:56 +02001421 if (!wp->w_p_diff)
1422 {
1423 if (wp->w_p_diff_saved)
1424 free_string_option(wp->w_p_fdm_save);
Bram Moolenaara87aa802013-07-03 15:47:03 +02001425 wp->w_p_fdm_save = vim_strsave(wp->w_p_fdm);
Bram Moolenaar43929962015-07-03 15:06:56 +02001426 }
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001427 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"diff",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001428 OPT_LOCAL|OPT_FREE, 0);
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001429 curwin = old_curwin;
1430 curbuf = curwin->w_buffer;
Bram Moolenaar43929962015-07-03 15:06:56 +02001431 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001432 {
1433 wp->w_p_fdc_save = wp->w_p_fdc;
1434 wp->w_p_fen_save = wp->w_p_fen;
1435 wp->w_p_fdl_save = wp->w_p_fdl;
1436 }
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001437 wp->w_p_fdc = diff_foldcolumn;
1438 wp->w_p_fen = TRUE;
1439 wp->w_p_fdl = 0;
1440 foldUpdateAll(wp);
1441 /* make sure topline is not halfway a fold */
1442 changed_window_setting_win(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001443# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001444 if (vim_strchr(p_sbo, 'h') == NULL)
1445 do_cmdline_cmd((char_u *)"set sbo+=hor");
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001446 /* Save the current values, to be restored in ex_diffoff(). */
Bram Moolenaara87aa802013-07-03 15:47:03 +02001447 wp->w_p_diff_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001449 set_diff_option(wp, TRUE);
Bram Moolenaar43929962015-07-03 15:06:56 +02001450
Bram Moolenaar071d4272004-06-13 20:20:40 +00001451 if (addbuf)
1452 diff_buf_add(wp->w_buffer);
1453 redraw_win_later(wp, NOT_VALID);
1454}
1455
1456/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001457 * Set options not to show diffs. For the current window or all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001458 * Only in the current tab page.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001459 */
1460 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001461ex_diffoff(exarg_T *eap)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001462{
1463 win_T *wp;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001464 int diffwin = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001465
Bram Moolenaar29323592016-07-24 22:04:11 +02001466 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001467 {
Bram Moolenaar00462ff2013-09-20 20:13:53 +02001468 if (eap->forceit ? wp->w_p_diff : wp == curwin)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001469 {
Bram Moolenaar43929962015-07-03 15:06:56 +02001470 /* Set 'diff' off. If option values were saved in
1471 * diff_win_options(), restore the ones whose settings seem to have
1472 * been left over from diff mode. */
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001473 set_diff_option(wp, FALSE);
Bram Moolenaara87aa802013-07-03 15:47:03 +02001474
Bram Moolenaara87aa802013-07-03 15:47:03 +02001475 if (wp->w_p_diff_saved)
1476 {
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001477
Bram Moolenaar43929962015-07-03 15:06:56 +02001478 if (wp->w_p_scb)
1479 wp->w_p_scb = wp->w_p_scb_save;
Bram Moolenaar43929962015-07-03 15:06:56 +02001480 if (wp->w_p_crb)
1481 wp->w_p_crb = wp->w_p_crb_save;
Bram Moolenaar43929962015-07-03 15:06:56 +02001482 if (!wp->w_p_wrap)
1483 wp->w_p_wrap = wp->w_p_wrap_save;
1484#ifdef FEAT_FOLDING
1485 free_string_option(wp->w_p_fdm);
Bram Moolenaar79a213d2017-05-16 13:15:18 +02001486 wp->w_p_fdm = vim_strsave(
1487 *wp->w_p_fdm_save ? wp->w_p_fdm_save : (char_u*)"manual");
Bram Moolenaar43929962015-07-03 15:06:56 +02001488
1489 if (wp->w_p_fdc == diff_foldcolumn)
1490 wp->w_p_fdc = wp->w_p_fdc_save;
1491 if (wp->w_p_fdl == 0)
1492 wp->w_p_fdl = wp->w_p_fdl_save;
1493
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001494 /* Only restore 'foldenable' when 'foldmethod' is not
1495 * "manual", otherwise we continue to show the diff folds. */
Bram Moolenaar43929962015-07-03 15:06:56 +02001496 if (wp->w_p_fen)
1497 wp->w_p_fen = foldmethodIsManual(wp) ? FALSE
1498 : wp->w_p_fen_save;
1499
1500 foldUpdateAll(wp);
Bram Moolenaar43929962015-07-03 15:06:56 +02001501#endif
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001502 }
Bram Moolenaare67d5462016-08-27 22:40:42 +02001503 /* remove filler lines */
1504 wp->w_topfill = 0;
1505
1506 /* make sure topline is not halfway a fold and cursor is
1507 * invalidated */
1508 changed_window_setting_win(wp);
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001509
Bram Moolenaara87aa802013-07-03 15:47:03 +02001510 /* Note: 'sbo' is not restored, it's a global option. */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001511 diff_buf_adjust(wp);
1512 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001513 diffwin |= wp->w_p_diff;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001514 }
1515
Bram Moolenaar25ea0542017-02-03 23:16:28 +01001516 /* Also remove hidden buffers from the list. */
1517 if (eap->forceit)
1518 diff_buf_clear();
1519
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001520 /* Remove "hor" from from 'scrollopt' if there are no diff windows left. */
1521 if (!diffwin && vim_strchr(p_sbo, 'h') != NULL)
1522 do_cmdline_cmd((char_u *)"set sbo-=hor");
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001523}
1524
1525/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 * Read the diff output and add each entry to the diff list.
1527 */
1528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001529diff_read(
Bram Moolenaare828b762018-09-10 17:51:58 +02001530 int idx_orig, // idx of original file
1531 int idx_new, // idx of new file
1532 diffout_T *dout) // diff output
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533{
Bram Moolenaare828b762018-09-10 17:51:58 +02001534 FILE *fd = NULL;
1535 int line_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 diff_T *dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001537 diff_T *dp = curtab->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 diff_T *dn, *dpl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001539 char_u linebuf[LBUFLEN]; /* only need to hold the diff line */
Bram Moolenaare828b762018-09-10 17:51:58 +02001540 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001541 long off;
1542 int i;
1543 linenr_T lnum_orig, lnum_new;
1544 long count_orig, count_new;
1545 int notset = TRUE; /* block "*dp" not set yet */
Bram Moolenaare828b762018-09-10 17:51:58 +02001546 enum {
1547 DIFF_ED,
1548 DIFF_UNIFIED,
1549 DIFF_NONE
1550 } diffstyle = DIFF_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551
Bram Moolenaare828b762018-09-10 17:51:58 +02001552 if (dout->dout_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001553 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001554 diffstyle = DIFF_UNIFIED;
1555 }
1556 else
1557 {
1558 fd = mch_fopen((char *)dout->dout_fname, "r");
1559 if (fd == NULL)
1560 {
1561 EMSG(_("E98: Cannot read diff output"));
1562 return;
1563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 }
1565
1566 for (;;)
1567 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001568 if (fd == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001569 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001570 if (line_idx >= dout->dout_ga.ga_len)
1571 break; // did last line
1572 line = ((char_u **)dout->dout_ga.ga_data)[line_idx++];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 }
1574 else
1575 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001576 if (tag_fgets(linebuf, LBUFLEN, fd))
1577 break; // end of file
1578 line = linebuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001579 }
1580
Bram Moolenaare828b762018-09-10 17:51:58 +02001581 if (diffstyle == DIFF_NONE)
1582 {
1583 // Determine diff style.
1584 // ed like diff looks like this:
1585 // {first}[,{last}]c{first}[,{last}]
1586 // {first}a{first}[,{last}]
1587 // {first}[,{last}]d{first}
1588 //
1589 // unified diff looks like this:
1590 // --- file1 2018-03-20 13:23:35.783153140 +0100
1591 // +++ file2 2018-03-20 13:23:41.183156066 +0100
1592 // @@ -1,3 +1,5 @@
1593 if (isdigit(*line))
1594 diffstyle = DIFF_ED;
1595 else if ((STRNCMP(line, "@@ ", 3) == 0))
1596 diffstyle = DIFF_UNIFIED;
1597 else if ((STRNCMP(line, "--- ", 4) == 0)
1598 && (tag_fgets(linebuf, LBUFLEN, fd) == 0)
1599 && (STRNCMP(line, "+++ ", 4) == 0)
1600 && (tag_fgets(linebuf, LBUFLEN, fd) == 0)
1601 && (STRNCMP(line, "@@ ", 3) == 0))
1602 diffstyle = DIFF_UNIFIED;
1603 }
1604
1605 if (diffstyle == DIFF_ED)
1606 {
1607 if (!isdigit(*line))
1608 continue; // not the start of a diff block
1609 if (parse_diff_ed(line, &lnum_orig, &count_orig,
1610 &lnum_new, &count_new) == FAIL)
1611 continue;
1612 }
1613 else if (diffstyle == DIFF_UNIFIED)
1614 {
1615 if (STRNCMP(line, "@@ ", 3) != 0)
1616 continue; // not the start of a diff block
1617 if (parse_diff_unified(line, &lnum_orig, &count_orig,
1618 &lnum_new, &count_new) == FAIL)
1619 continue;
1620 }
1621 else
1622 {
1623 EMSG(_("E959: Invalid diff format."));
1624 break;
1625 }
1626
1627 // Go over blocks before the change, for which orig and new are equal.
1628 // Copy blocks from orig to new.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 while (dp != NULL
1630 && lnum_orig > dp->df_lnum[idx_orig] + dp->df_count[idx_orig])
1631 {
1632 if (notset)
1633 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1634 dprev = dp;
1635 dp = dp->df_next;
1636 notset = TRUE;
1637 }
1638
1639 if (dp != NULL
1640 && lnum_orig <= dp->df_lnum[idx_orig] + dp->df_count[idx_orig]
1641 && lnum_orig + count_orig >= dp->df_lnum[idx_orig])
1642 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001643 // New block overlaps with existing block(s).
1644 // First find last block that overlaps.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 for (dpl = dp; dpl->df_next != NULL; dpl = dpl->df_next)
1646 if (lnum_orig + count_orig < dpl->df_next->df_lnum[idx_orig])
1647 break;
1648
Bram Moolenaare828b762018-09-10 17:51:58 +02001649 // If the newly found block starts before the old one, set the
1650 // start back a number of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 off = dp->df_lnum[idx_orig] - lnum_orig;
1652 if (off > 0)
1653 {
1654 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001655 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 dp->df_lnum[i] -= off;
1657 dp->df_lnum[idx_new] = lnum_new;
1658 dp->df_count[idx_new] = count_new;
1659 }
1660 else if (notset)
1661 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001662 // new block inside existing one, adjust new block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001663 dp->df_lnum[idx_new] = lnum_new + off;
1664 dp->df_count[idx_new] = count_new - off;
1665 }
1666 else
Bram Moolenaare828b762018-09-10 17:51:58 +02001667 // second overlap of new block with existing block
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001668 dp->df_count[idx_new] += count_new - count_orig
1669 + dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]
1670 - (dp->df_lnum[idx_orig] + dp->df_count[idx_orig]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671
Bram Moolenaare828b762018-09-10 17:51:58 +02001672 // Adjust the size of the block to include all the lines to the
1673 // end of the existing block or the new diff, whatever ends last.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 off = (lnum_orig + count_orig)
1675 - (dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]);
1676 if (off < 0)
1677 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001678 // new change ends in existing block, adjust the end if not
1679 // done already
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (notset)
1681 dp->df_count[idx_new] += -off;
1682 off = 0;
1683 }
Bram Moolenaard4b96bc2007-10-19 15:33:39 +00001684 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001685 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 dp->df_count[i] = dpl->df_lnum[i] + dpl->df_count[i]
1687 - dp->df_lnum[i] + off;
1688
Bram Moolenaare828b762018-09-10 17:51:58 +02001689 // Delete the diff blocks that have been merged into one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690 dn = dp->df_next;
1691 dp->df_next = dpl->df_next;
1692 while (dn != dp->df_next)
1693 {
1694 dpl = dn->df_next;
1695 vim_free(dn);
1696 dn = dpl;
1697 }
1698 }
1699 else
1700 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001701 // Allocate a new diffblock.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001702 dp = diff_alloc_new(curtab, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 if (dp == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001704 goto done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001705
1706 dp->df_lnum[idx_orig] = lnum_orig;
1707 dp->df_count[idx_orig] = count_orig;
1708 dp->df_lnum[idx_new] = lnum_new;
1709 dp->df_count[idx_new] = count_new;
1710
Bram Moolenaare828b762018-09-10 17:51:58 +02001711 // Set values for other buffers, these must be equal to the
1712 // original buffer, otherwise there would have been a change
1713 // already.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 for (i = idx_orig + 1; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001715 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 diff_copy_entry(dprev, dp, idx_orig, i);
1717 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001718 notset = FALSE; // "*dp" has been set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 }
1720
Bram Moolenaare828b762018-09-10 17:51:58 +02001721 // for remaining diff blocks orig and new are equal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 while (dp != NULL)
1723 {
1724 if (notset)
1725 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1726 dprev = dp;
1727 dp = dp->df_next;
1728 notset = TRUE;
1729 }
1730
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001731done:
Bram Moolenaare828b762018-09-10 17:51:58 +02001732 if (fd != NULL)
1733 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734}
1735
1736/*
1737 * Copy an entry at "dp" from "idx_orig" to "idx_new".
1738 */
1739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001740diff_copy_entry(
1741 diff_T *dprev,
1742 diff_T *dp,
1743 int idx_orig,
1744 int idx_new)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745{
1746 long off;
1747
1748 if (dprev == NULL)
1749 off = 0;
1750 else
1751 off = (dprev->df_lnum[idx_orig] + dprev->df_count[idx_orig])
1752 - (dprev->df_lnum[idx_new] + dprev->df_count[idx_new]);
1753 dp->df_lnum[idx_new] = dp->df_lnum[idx_orig] - off;
1754 dp->df_count[idx_new] = dp->df_count[idx_orig];
1755}
1756
1757/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001758 * Clear the list of diffblocks for tab page "tp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 */
Bram Moolenaarea408852005-06-25 22:49:46 +00001760 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001761diff_clear(tabpage_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762{
1763 diff_T *p, *next_p;
1764
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001765 for (p = tp->tp_first_diff; p != NULL; p = next_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001766 {
1767 next_p = p->df_next;
1768 vim_free(p);
1769 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001770 tp->tp_first_diff = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001771}
1772
1773/*
1774 * Check diff status for line "lnum" in buffer "buf":
1775 * Returns 0 for nothing special
1776 * Returns -1 for a line that should be highlighted as changed.
1777 * Returns -2 for a line that should be highlighted as added/deleted.
1778 * Returns > 0 for inserting that many filler lines above it (never happens
1779 * when 'diffopt' doesn't contain "filler").
1780 * This should only be used for windows where 'diff' is set.
1781 */
1782 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001783diff_check(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001785 int idx; /* index in tp_diffbuf[] for this buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 diff_T *dp;
1787 int maxcount;
1788 int i;
1789 buf_T *buf = wp->w_buffer;
1790 int cmp;
1791
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001792 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 ex_diffupdate(NULL); /* update after a big change */
1794
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001795 if (curtab->tp_first_diff == NULL || !wp->w_p_diff) /* no diffs at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 return 0;
1797
1798 /* safety check: "lnum" must be a buffer line */
1799 if (lnum < 1 || lnum > buf->b_ml.ml_line_count + 1)
1800 return 0;
1801
1802 idx = diff_buf_idx(buf);
1803 if (idx == DB_COUNT)
1804 return 0; /* no diffs for buffer "buf" */
1805
1806#ifdef FEAT_FOLDING
1807 /* A closed fold never has filler lines. */
1808 if (hasFoldingWin(wp, lnum, NULL, NULL, TRUE, NULL))
1809 return 0;
1810#endif
1811
1812 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001813 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
1815 break;
1816 if (dp == NULL || lnum < dp->df_lnum[idx])
1817 return 0;
1818
1819 if (lnum < dp->df_lnum[idx] + dp->df_count[idx])
1820 {
1821 int zero = FALSE;
1822
1823 /* Changed or inserted line. If the other buffers have a count of
1824 * zero, the lines were inserted. If the other buffers have the same
1825 * count, check if the lines are identical. */
1826 cmp = FALSE;
1827 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001828 if (i != idx && curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 {
1830 if (dp->df_count[i] == 0)
1831 zero = TRUE;
1832 else
1833 {
1834 if (dp->df_count[i] != dp->df_count[idx])
1835 return -1; /* nr of lines changed. */
1836 cmp = TRUE;
1837 }
1838 }
1839 if (cmp)
1840 {
1841 /* Compare all lines. If they are equal the lines were inserted
1842 * in some buffers, deleted in others, but not changed. */
1843 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001844 if (i != idx && curtab->tp_diffbuf[i] != NULL
1845 && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 if (!diff_equal_entry(dp, idx, i))
1847 return -1;
1848 }
1849 /* If there is no buffer with zero lines then there is no difference
1850 * any longer. Happens when making a change (or undo) that removes
1851 * the difference. Can't remove the entry here, we might be halfway
1852 * updating the window. Just report the text as unchanged. Other
1853 * windows might still show the change though. */
1854 if (zero == FALSE)
1855 return 0;
1856 return -2;
1857 }
1858
1859 /* If 'diffopt' doesn't contain "filler", return 0. */
1860 if (!(diff_flags & DIFF_FILLER))
1861 return 0;
1862
1863 /* Insert filler lines above the line just below the change. Will return
1864 * 0 when this buf had the max count. */
1865 maxcount = 0;
1866 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001867 if (curtab->tp_diffbuf[i] != NULL && dp->df_count[i] > maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 maxcount = dp->df_count[i];
1869 return maxcount - dp->df_count[idx];
1870}
1871
1872/*
1873 * Compare two entries in diff "*dp" and return TRUE if they are equal.
1874 */
1875 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001876diff_equal_entry(diff_T *dp, int idx1, int idx2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877{
1878 int i;
1879 char_u *line;
1880 int cmp;
1881
1882 if (dp->df_count[idx1] != dp->df_count[idx2])
1883 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001884 if (diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 return FALSE;
1886 for (i = 0; i < dp->df_count[idx1]; ++i)
1887 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001888 line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 dp->df_lnum[idx1] + i, FALSE));
1890 if (line == NULL)
1891 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001892 cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 dp->df_lnum[idx2] + i, FALSE));
1894 vim_free(line);
1895 if (cmp != 0)
1896 return FALSE;
1897 }
1898 return TRUE;
1899}
1900
1901/*
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02001902 * Compare the characters at "p1" and "p2". If they are equal (possibly
1903 * ignoring case) return TRUE and set "len" to the number of bytes.
1904 */
1905 static int
1906diff_equal_char(char_u *p1, char_u *p2, int *len)
1907{
1908#ifdef FEAT_MBYTE
1909 int l = (*mb_ptr2len)(p1);
1910
1911 if (l != (*mb_ptr2len)(p2))
1912 return FALSE;
1913 if (l > 1)
1914 {
1915 if (STRNCMP(p1, p2, l) != 0
1916 && (!enc_utf8
1917 || !(diff_flags & DIFF_ICASE)
1918 || utf_fold(utf_ptr2char(p1))
1919 != utf_fold(utf_ptr2char(p2))))
1920 return FALSE;
1921 *len = l;
1922 }
1923 else
1924#endif
1925 {
1926 if ((*p1 != *p2)
1927 && (!(diff_flags & DIFF_ICASE)
1928 || TOLOWER_LOC(*p1) != TOLOWER_LOC(*p2)))
1929 return FALSE;
1930 *len = 1;
1931 }
1932 return TRUE;
1933}
1934
1935/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 * Compare strings "s1" and "s2" according to 'diffopt'.
1937 * Return non-zero when they are different.
1938 */
1939 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001940diff_cmp(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941{
1942 char_u *p1, *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944
1945 if ((diff_flags & (DIFF_ICASE | DIFF_IWHITE)) == 0)
1946 return STRCMP(s1, s2);
1947 if ((diff_flags & DIFF_ICASE) && !(diff_flags & DIFF_IWHITE))
1948 return MB_STRICMP(s1, s2);
1949
1950 /* Ignore white space changes and possibly ignore case. */
1951 p1 = s1;
1952 p2 = s2;
1953 while (*p1 != NUL && *p2 != NUL)
1954 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001955 if (VIM_ISWHITE(*p1) && VIM_ISWHITE(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
1957 p1 = skipwhite(p1);
1958 p2 = skipwhite(p2);
1959 }
1960 else
1961 {
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02001962 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 break;
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02001964 p1 += l;
1965 p2 += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 }
1967 }
1968
1969 /* Ignore trailing white space. */
1970 p1 = skipwhite(p1);
1971 p2 = skipwhite(p2);
1972 if (*p1 != NUL || *p2 != NUL)
1973 return 1;
1974 return 0;
1975}
1976
1977/*
1978 * Return the number of filler lines above "lnum".
1979 */
1980 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001981diff_check_fill(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982{
1983 int n;
1984
1985 /* be quick when there are no filler lines */
1986 if (!(diff_flags & DIFF_FILLER))
1987 return 0;
1988 n = diff_check(wp, lnum);
1989 if (n <= 0)
1990 return 0;
1991 return n;
1992}
1993
1994/*
1995 * Set the topline of "towin" to match the position in "fromwin", so that they
1996 * show the same diff'ed lines.
1997 */
1998 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001999diff_set_topline(win_T *fromwin, win_T *towin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000{
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002001 buf_T *frombuf = fromwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002002 linenr_T lnum = fromwin->w_topline;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002003 int fromidx;
2004 int toidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 diff_T *dp;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002006 int max_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 int i;
2008
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002009 fromidx = diff_buf_idx(frombuf);
2010 if (fromidx == DB_COUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 return; /* safety check */
2012
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002013 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 ex_diffupdate(NULL); /* update after a big change */
2015
2016 towin->w_topfill = 0;
2017
2018 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002019 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002020 if (lnum <= dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 break;
2022 if (dp == NULL)
2023 {
2024 /* After last change, compute topline relative to end of file; no
2025 * filler lines. */
2026 towin->w_topline = towin->w_buffer->b_ml.ml_line_count
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002027 - (frombuf->b_ml.ml_line_count - lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
2029 else
2030 {
2031 /* Find index for "towin". */
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002032 toidx = diff_buf_idx(towin->w_buffer);
2033 if (toidx == DB_COUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 return; /* safety check */
2035
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002036 towin->w_topline = lnum + (dp->df_lnum[toidx] - dp->df_lnum[fromidx]);
2037 if (lnum >= dp->df_lnum[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002038 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002039 /* Inside a change: compute filler lines. With three or more
2040 * buffers we need to know the largest count. */
2041 max_count = 0;
2042 for (i = 0; i < DB_COUNT; ++i)
2043 if (curtab->tp_diffbuf[i] != NULL
2044 && max_count < dp->df_count[i])
2045 max_count = dp->df_count[i];
2046
2047 if (dp->df_count[toidx] == dp->df_count[fromidx])
2048 {
2049 /* same number of lines: use same filler count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 towin->w_topfill = fromwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 }
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002052 else if (dp->df_count[toidx] > dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002053 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002054 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002056 /* more lines in towin and fromwin doesn't show diff
2057 * lines, only filler lines */
2058 if (max_count - fromwin->w_topfill >= dp->df_count[toidx])
2059 {
2060 /* towin also only shows filler lines */
2061 towin->w_topline = dp->df_lnum[toidx]
2062 + dp->df_count[toidx];
2063 towin->w_topfill = fromwin->w_topfill;
2064 }
2065 else
2066 /* towin still has some diff lines to show */
2067 towin->w_topline = dp->df_lnum[toidx]
2068 + max_count - fromwin->w_topfill;
2069 }
2070 }
2071 else if (towin->w_topline >= dp->df_lnum[toidx]
2072 + dp->df_count[toidx])
2073 {
2074 /* less lines in towin and no diff lines to show: compute
2075 * filler lines */
2076 towin->w_topline = dp->df_lnum[toidx] + dp->df_count[toidx];
2077 if (diff_flags & DIFF_FILLER)
2078 {
2079 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
2080 /* fromwin is also out of diff lines */
2081 towin->w_topfill = fromwin->w_topfill;
2082 else
2083 /* fromwin has some diff lines */
2084 towin->w_topfill = dp->df_lnum[fromidx]
2085 + max_count - lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 }
2087 }
2088 }
2089 }
2090
2091 /* safety check (if diff info gets outdated strange things may happen) */
2092 towin->w_botfill = FALSE;
2093 if (towin->w_topline > towin->w_buffer->b_ml.ml_line_count)
2094 {
2095 towin->w_topline = towin->w_buffer->b_ml.ml_line_count;
2096 towin->w_botfill = TRUE;
2097 }
2098 if (towin->w_topline < 1)
2099 {
2100 towin->w_topline = 1;
2101 towin->w_topfill = 0;
2102 }
2103
2104 /* When w_topline changes need to recompute w_botline and cursor position */
2105 invalidate_botline_win(towin);
2106 changed_line_abv_curs_win(towin);
2107
2108 check_topfill(towin, FALSE);
2109#ifdef FEAT_FOLDING
2110 (void)hasFoldingWin(towin, towin->w_topline, &towin->w_topline,
2111 NULL, TRUE, NULL);
2112#endif
2113}
2114
2115/*
2116 * This is called when 'diffopt' is changed.
2117 */
2118 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002119diffopt_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120{
2121 char_u *p;
2122 int diff_context_new = 6;
2123 int diff_flags_new = 0;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002124 int diff_foldcolumn_new = 2;
Bram Moolenaare828b762018-09-10 17:51:58 +02002125 long diff_algorithm_new = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002126 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127
2128 p = p_dip;
2129 while (*p != NUL)
2130 {
2131 if (STRNCMP(p, "filler", 6) == 0)
2132 {
2133 p += 6;
2134 diff_flags_new |= DIFF_FILLER;
2135 }
2136 else if (STRNCMP(p, "context:", 8) == 0 && VIM_ISDIGIT(p[8]))
2137 {
2138 p += 8;
2139 diff_context_new = getdigits(&p);
2140 }
2141 else if (STRNCMP(p, "icase", 5) == 0)
2142 {
2143 p += 5;
2144 diff_flags_new |= DIFF_ICASE;
2145 }
2146 else if (STRNCMP(p, "iwhite", 6) == 0)
2147 {
2148 p += 6;
2149 diff_flags_new |= DIFF_IWHITE;
2150 }
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002151 else if (STRNCMP(p, "horizontal", 10) == 0)
2152 {
2153 p += 10;
2154 diff_flags_new |= DIFF_HORIZONTAL;
2155 }
2156 else if (STRNCMP(p, "vertical", 8) == 0)
2157 {
2158 p += 8;
2159 diff_flags_new |= DIFF_VERTICAL;
2160 }
2161 else if (STRNCMP(p, "foldcolumn:", 11) == 0 && VIM_ISDIGIT(p[11]))
2162 {
2163 p += 11;
2164 diff_foldcolumn_new = getdigits(&p);
2165 }
Bram Moolenaar97ce4192017-12-01 20:35:58 +01002166 else if (STRNCMP(p, "hiddenoff", 9) == 0)
2167 {
2168 p += 9;
2169 diff_flags_new |= DIFF_HIDDEN_OFF;
2170 }
Bram Moolenaare828b762018-09-10 17:51:58 +02002171 else if (STRNCMP(p, "indent-heuristic", 16) == 0)
2172 {
2173 p += 16;
2174 diff_algorithm_new |= XDF_INDENT_HEURISTIC;
2175 }
2176 else if (STRNCMP(p, "internal", 8) == 0)
2177 {
2178 p += 8;
2179 diff_flags_new |= DIFF_INTERNAL;
2180 }
2181 else if (STRNCMP(p, "algorithm:", 10) == 0)
2182 {
2183 p += 10;
2184 if (STRNCMP(p, "myers", 5) == 0)
2185 {
2186 p += 5;
2187 diff_algorithm_new = 0;
2188 }
2189 else if (STRNCMP(p, "minimal", 7) == 0)
2190 {
2191 p += 7;
2192 diff_algorithm_new = XDF_NEED_MINIMAL;
2193 }
2194 else if (STRNCMP(p, "patience", 8) == 0)
2195 {
2196 p += 8;
2197 diff_algorithm_new = XDF_PATIENCE_DIFF;
2198 }
2199 else if (STRNCMP(p, "histogram", 9) == 0)
2200 {
2201 p += 9;
2202 diff_algorithm_new = XDF_HISTOGRAM_DIFF;
2203 }
2204 }
2205
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 if (*p != ',' && *p != NUL)
2207 return FAIL;
2208 if (*p == ',')
2209 ++p;
2210 }
2211
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002212 /* Can't have both "horizontal" and "vertical". */
2213 if ((diff_flags_new & DIFF_HORIZONTAL) && (diff_flags_new & DIFF_VERTICAL))
2214 return FAIL;
2215
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 /* If "icase" or "iwhite" was added or removed, need to update the diff. */
Bram Moolenaare828b762018-09-10 17:51:58 +02002217 if (diff_flags != diff_flags_new || diff_algorithm != diff_algorithm_new)
Bram Moolenaar29323592016-07-24 22:04:11 +02002218 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002219 tp->tp_diff_invalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220
2221 diff_flags = diff_flags_new;
2222 diff_context = diff_context_new;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002223 diff_foldcolumn = diff_foldcolumn_new;
Bram Moolenaare828b762018-09-10 17:51:58 +02002224 diff_algorithm = diff_algorithm_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225
2226 diff_redraw(TRUE);
2227
2228 /* recompute the scroll binding with the new option value, may
2229 * remove or add filler lines */
2230 check_scrollbind((linenr_T)0, 0L);
2231
2232 return OK;
2233}
2234
2235/*
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002236 * Return TRUE if 'diffopt' contains "horizontal".
2237 */
2238 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239diffopt_horizontal(void)
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002240{
2241 return (diff_flags & DIFF_HORIZONTAL) != 0;
2242}
2243
2244/*
Bram Moolenaar97ce4192017-12-01 20:35:58 +01002245 * Return TRUE if 'diffopt' contains "hiddenoff".
2246 */
2247 int
2248diffopt_hiddenoff(void)
2249{
2250 return (diff_flags & DIFF_HIDDEN_OFF) != 0;
2251}
2252
2253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 * Find the difference within a changed line.
2255 * Returns TRUE if the line was added, no other buffer has it.
2256 */
2257 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002258diff_find_change(
2259 win_T *wp,
2260 linenr_T lnum,
2261 int *startp, /* first char of the change */
2262 int *endp) /* last char of the change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263{
2264 char_u *line_org;
2265 char_u *line_new;
2266 int i;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002267 int si_org, si_new;
2268 int ei_org, ei_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002269 diff_T *dp;
2270 int idx;
2271 int off;
2272 int added = TRUE;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002273 char_u *p1, *p2;
2274 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275
2276 /* Make a copy of the line, the next ml_get() will invalidate it. */
2277 line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE));
2278 if (line_org == NULL)
2279 return FALSE;
2280
2281 idx = diff_buf_idx(wp->w_buffer);
2282 if (idx == DB_COUNT) /* cannot happen */
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002283 {
2284 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287
2288 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002289 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
2291 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002292 if (dp == NULL || diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002293 {
2294 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
2298 off = lnum - dp->df_lnum[idx];
2299
2300 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002301 if (curtab->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
2303 /* Skip lines that are not in the other change (filler lines). */
2304 if (off >= dp->df_count[i])
2305 continue;
2306 added = FALSE;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002307 line_new = ml_get_buf(curtab->tp_diffbuf[i],
2308 dp->df_lnum[i] + off, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
2310 /* Search for start of difference */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002311 si_org = si_new = 0;
2312 while (line_org[si_org] != NUL)
2313 {
2314 if ((diff_flags & DIFF_IWHITE)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002315 && VIM_ISWHITE(line_org[si_org])
2316 && VIM_ISWHITE(line_new[si_new]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002317 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002318 si_org = (int)(skipwhite(line_org + si_org) - line_org);
2319 si_new = (int)(skipwhite(line_new + si_new) - line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002320 }
2321 else
2322 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002323 if (!diff_equal_char(line_org + si_org, line_new + si_new,
2324 &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002325 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002326 si_org += l;
2327 si_new += l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002328 }
2329 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330#ifdef FEAT_MBYTE
2331 if (has_mbyte)
2332 {
2333 /* Move back to first byte of character in both lines (may
2334 * have "nn^" in line_org and "n^ in line_new). */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002335 si_org -= (*mb_head_off)(line_org, line_org + si_org);
2336 si_new -= (*mb_head_off)(line_new, line_new + si_new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 }
2338#endif
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002339 if (*startp > si_org)
2340 *startp = si_org;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341
2342 /* Search for end of difference, if any. */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002343 if (line_org[si_org] != NUL || line_new[si_new] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
2345 ei_org = (int)STRLEN(line_org);
2346 ei_new = (int)STRLEN(line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002347 while (ei_org >= *startp && ei_new >= si_new
2348 && ei_org >= 0 && ei_new >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002350 if ((diff_flags & DIFF_IWHITE)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002351 && VIM_ISWHITE(line_org[ei_org])
2352 && VIM_ISWHITE(line_new[ei_new]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002353 {
2354 while (ei_org >= *startp
Bram Moolenaar1c465442017-03-12 20:10:05 +01002355 && VIM_ISWHITE(line_org[ei_org]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002356 --ei_org;
2357 while (ei_new >= si_new
Bram Moolenaar1c465442017-03-12 20:10:05 +01002358 && VIM_ISWHITE(line_new[ei_new]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002359 --ei_new;
2360 }
2361 else
2362 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002363 p1 = line_org + ei_org;
2364 p2 = line_new + ei_new;
2365#ifdef FEAT_MBYTE
2366 p1 -= (*mb_head_off)(line_org, p1);
2367 p2 -= (*mb_head_off)(line_new, p2);
2368#endif
2369 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002370 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002371 ei_org -= l;
2372 ei_new -= l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 }
2375 if (*endp < ei_org)
2376 *endp = ei_org;
2377 }
2378 }
2379
2380 vim_free(line_org);
2381 return added;
2382}
2383
2384#if defined(FEAT_FOLDING) || defined(PROTO)
2385/*
2386 * Return TRUE if line "lnum" is not close to a diff block, this line should
2387 * be in a fold.
2388 * Return FALSE if there are no diff blocks at all in this window.
2389 */
2390 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002391diff_infold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392{
2393 int i;
2394 int idx = -1;
2395 int other = FALSE;
2396 diff_T *dp;
2397
2398 /* Return if 'diff' isn't set. */
2399 if (!wp->w_p_diff)
2400 return FALSE;
2401
2402 for (i = 0; i < DB_COUNT; ++i)
2403 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002404 if (curtab->tp_diffbuf[i] == wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 idx = i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002406 else if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 other = TRUE;
2408 }
2409
2410 /* return here if there are no diffs in the window */
2411 if (idx == -1 || !other)
2412 return FALSE;
2413
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002414 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 ex_diffupdate(NULL); /* update after a big change */
2416
2417 /* Return if there are no diff blocks. All lines will be folded. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002418 if (curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 return TRUE;
2420
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002421 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
2423 /* If this change is below the line there can't be any further match. */
2424 if (dp->df_lnum[idx] - diff_context > lnum)
2425 break;
2426 /* If this change ends before the line we have a match. */
2427 if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum)
2428 return FALSE;
2429 }
2430 return TRUE;
2431}
2432#endif
2433
2434/*
2435 * "dp" and "do" commands.
2436 */
2437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002438nv_diffgetput(int put, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439{
2440 exarg_T ea;
Bram Moolenaar6a643652014-10-31 13:54:25 +01002441 char_u buf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442
Bram Moolenaarf2732452018-06-03 14:47:35 +02002443#ifdef FEAT_JOB_CHANNEL
2444 if (bt_prompt(curbuf))
2445 {
2446 vim_beep(BO_OPER);
2447 return;
2448 }
2449#endif
Bram Moolenaar6a643652014-10-31 13:54:25 +01002450 if (count == 0)
2451 ea.arg = (char_u *)"";
2452 else
2453 {
2454 vim_snprintf((char *)buf, 30, "%ld", count);
2455 ea.arg = buf;
2456 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 if (put)
2458 ea.cmdidx = CMD_diffput;
2459 else
2460 ea.cmdidx = CMD_diffget;
2461 ea.addr_count = 0;
2462 ea.line1 = curwin->w_cursor.lnum;
2463 ea.line2 = curwin->w_cursor.lnum;
2464 ex_diffgetput(&ea);
2465}
2466
2467/*
2468 * ":diffget"
2469 * ":diffput"
2470 */
2471 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002472ex_diffgetput(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473{
2474 linenr_T lnum;
2475 int count;
2476 linenr_T off = 0;
2477 diff_T *dp;
2478 diff_T *dprev;
2479 diff_T *dfree;
2480 int idx_cur;
2481 int idx_other;
2482 int idx_from;
2483 int idx_to;
2484 int i;
2485 int added;
2486 char_u *p;
2487 aco_save_T aco;
2488 buf_T *buf;
2489 int start_skip, end_skip;
2490 int new_count;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002491 int buf_empty;
Bram Moolenaar602eb742007-02-20 03:43:38 +00002492 int found_not_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493
2494 /* Find the current buffer in the list of diff buffers. */
2495 idx_cur = diff_buf_idx(curbuf);
2496 if (idx_cur == DB_COUNT)
2497 {
2498 EMSG(_("E99: Current buffer is not in diff mode"));
2499 return;
2500 }
2501
2502 if (*eap->arg == NUL)
2503 {
2504 /* No argument: Find the other buffer in the list of diff buffers. */
2505 for (idx_other = 0; idx_other < DB_COUNT; ++idx_other)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002506 if (curtab->tp_diffbuf[idx_other] != curbuf
Bram Moolenaar602eb742007-02-20 03:43:38 +00002507 && curtab->tp_diffbuf[idx_other] != NULL)
2508 {
2509 if (eap->cmdidx != CMD_diffput
2510 || curtab->tp_diffbuf[idx_other]->b_p_ma)
2511 break;
2512 found_not_ma = TRUE;
2513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 if (idx_other == DB_COUNT)
2515 {
Bram Moolenaar602eb742007-02-20 03:43:38 +00002516 if (found_not_ma)
2517 EMSG(_("E793: No other buffer in diff mode is modifiable"));
2518 else
2519 EMSG(_("E100: No other buffer in diff mode"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 return;
2521 }
2522
2523 /* Check that there isn't a third buffer in the list */
2524 for (i = idx_other + 1; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002525 if (curtab->tp_diffbuf[i] != curbuf
2526 && curtab->tp_diffbuf[i] != NULL
2527 && (eap->cmdidx != CMD_diffput || curtab->tp_diffbuf[i]->b_p_ma))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
2529 EMSG(_("E101: More than two buffers in diff mode, don't know which one to use"));
2530 return;
2531 }
2532 }
2533 else
2534 {
2535 /* Buffer number or pattern given. Ignore trailing white space. */
2536 p = eap->arg + STRLEN(eap->arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002537 while (p > eap->arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 --p;
2539 for (i = 0; vim_isdigit(eap->arg[i]) && eap->arg + i < p; ++i)
2540 ;
2541 if (eap->arg + i == p) /* digits only */
2542 i = atol((char *)eap->arg);
2543 else
2544 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002545 i = buflist_findpat(eap->arg, p, FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 if (i < 0)
2547 return; /* error message already given */
2548 }
2549 buf = buflist_findnr(i);
2550 if (buf == NULL)
2551 {
2552 EMSG2(_("E102: Can't find buffer \"%s\""), eap->arg);
2553 return;
2554 }
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +00002555 if (buf == curbuf)
2556 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 idx_other = diff_buf_idx(buf);
2558 if (idx_other == DB_COUNT)
2559 {
2560 EMSG2(_("E103: Buffer \"%s\" is not in diff mode"), eap->arg);
2561 return;
2562 }
2563 }
2564
2565 diff_busy = TRUE;
2566
2567 /* When no range given include the line above or below the cursor. */
2568 if (eap->addr_count == 0)
2569 {
2570 /* Make it possible that ":diffget" on the last line gets line below
2571 * the cursor line when there is no difference above the cursor. */
2572 if (eap->cmdidx == CMD_diffget
2573 && eap->line1 == curbuf->b_ml.ml_line_count
2574 && diff_check(curwin, eap->line1) == 0
2575 && (eap->line1 == 1 || diff_check(curwin, eap->line1 - 1) == 0))
2576 ++eap->line2;
2577 else if (eap->line1 > 0)
2578 --eap->line1;
2579 }
2580
2581 if (eap->cmdidx == CMD_diffget)
2582 {
2583 idx_from = idx_other;
2584 idx_to = idx_cur;
2585 }
2586 else
2587 {
2588 idx_from = idx_cur;
2589 idx_to = idx_other;
2590 /* Need to make the other buffer the current buffer to be able to make
2591 * changes in it. */
2592 /* set curwin/curbuf to buf and save a few things */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002593 aucmd_prepbuf(&aco, curtab->tp_diffbuf[idx_other]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
2595
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002596 /* May give the warning for a changed buffer here, which can trigger the
2597 * FileChangedRO autocommand, which may do nasty things and mess
2598 * everything up. */
2599 if (!curbuf->b_changed)
2600 {
2601 change_warning(0);
2602 if (diff_buf_idx(curbuf) != idx_to)
2603 {
2604 EMSG(_("E787: Buffer changed unexpectedly"));
2605 return;
2606 }
2607 }
2608
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002610 for (dp = curtab->tp_first_diff; dp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 {
2612 if (dp->df_lnum[idx_cur] > eap->line2 + off)
2613 break; /* past the range that was specified */
2614
2615 dfree = NULL;
2616 lnum = dp->df_lnum[idx_to];
2617 count = dp->df_count[idx_to];
2618 if (dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off
2619 && u_save(lnum - 1, lnum + count) != FAIL)
2620 {
2621 /* Inside the specified range and saving for undo worked. */
2622 start_skip = 0;
2623 end_skip = 0;
2624 if (eap->addr_count > 0)
2625 {
2626 /* A range was specified: check if lines need to be skipped. */
2627 start_skip = eap->line1 + off - dp->df_lnum[idx_cur];
2628 if (start_skip > 0)
2629 {
2630 /* range starts below start of current diff block */
2631 if (start_skip > count)
2632 {
2633 lnum += count;
2634 count = 0;
2635 }
2636 else
2637 {
2638 count -= start_skip;
2639 lnum += start_skip;
2640 }
2641 }
2642 else
2643 start_skip = 0;
2644
2645 end_skip = dp->df_lnum[idx_cur] + dp->df_count[idx_cur] - 1
2646 - (eap->line2 + off);
2647 if (end_skip > 0)
2648 {
2649 /* range ends above end of current/from diff block */
2650 if (idx_cur == idx_from) /* :diffput */
2651 {
2652 i = dp->df_count[idx_cur] - start_skip - end_skip;
2653 if (count > i)
2654 count = i;
2655 }
2656 else /* :diffget */
2657 {
2658 count -= end_skip;
2659 end_skip = dp->df_count[idx_from] - start_skip - count;
2660 if (end_skip < 0)
2661 end_skip = 0;
2662 }
2663 }
2664 else
2665 end_skip = 0;
2666 }
2667
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002668 buf_empty = BUFEMPTY();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 added = 0;
2670 for (i = 0; i < count; ++i)
2671 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00002672 /* remember deleting the last line of the buffer */
2673 buf_empty = curbuf->b_ml.ml_line_count == 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 ml_delete(lnum, FALSE);
2675 --added;
2676 }
2677 for (i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; ++i)
2678 {
2679 linenr_T nr;
2680
2681 nr = dp->df_lnum[idx_from] + start_skip + i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002682 if (nr > curtab->tp_diffbuf[idx_from]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683 break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002684 p = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx_from],
2685 nr, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 if (p != NULL)
2687 {
2688 ml_append(lnum + i - 1, p, 0, FALSE);
2689 vim_free(p);
2690 ++added;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002691 if (buf_empty && curbuf->b_ml.ml_line_count == 2)
2692 {
2693 /* Added the first line into an empty buffer, need to
2694 * delete the dummy empty line. */
2695 buf_empty = FALSE;
2696 ml_delete((linenr_T)2, FALSE);
2697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 }
2699 }
2700 new_count = dp->df_count[idx_to] + added;
2701 dp->df_count[idx_to] = new_count;
2702
2703 if (start_skip == 0 && end_skip == 0)
2704 {
2705 /* Check if there are any other buffers and if the diff is
2706 * equal in them. */
2707 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002708 if (curtab->tp_diffbuf[i] != NULL && i != idx_from
2709 && i != idx_to
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 && !diff_equal_entry(dp, idx_from, i))
2711 break;
2712 if (i == DB_COUNT)
2713 {
2714 /* delete the diff entry, the buffers are now equal here */
2715 dfree = dp;
2716 dp = dp->df_next;
2717 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002718 curtab->tp_first_diff = dp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 else
2720 dprev->df_next = dp;
2721 }
2722 }
2723
2724 /* Adjust marks. This will change the following entries! */
2725 if (added != 0)
2726 {
2727 mark_adjust(lnum, lnum + count - 1, (long)MAXLNUM, (long)added);
2728 if (curwin->w_cursor.lnum >= lnum)
2729 {
2730 /* Adjust the cursor position if it's in/after the changed
2731 * lines. */
2732 if (curwin->w_cursor.lnum >= lnum + count)
2733 curwin->w_cursor.lnum += added;
2734 else if (added < 0)
2735 curwin->w_cursor.lnum = lnum;
2736 }
2737 }
2738 changed_lines(lnum, 0, lnum + count, (long)added);
2739
2740 if (dfree != NULL)
2741 {
2742 /* Diff is deleted, update folds in other windows. */
2743#ifdef FEAT_FOLDING
2744 diff_fold_update(dfree, idx_to);
2745#endif
2746 vim_free(dfree);
2747 }
2748 else
2749 /* mark_adjust() may have changed the count in a wrong way */
2750 dp->df_count[idx_to] = new_count;
2751
2752 /* When changing the current buffer, keep track of line numbers */
2753 if (idx_cur == idx_to)
2754 off += added;
2755 }
2756
2757 /* If before the range or not deleted, go to next diff. */
2758 if (dfree == NULL)
2759 {
2760 dprev = dp;
2761 dp = dp->df_next;
2762 }
2763 }
2764
2765 /* restore curwin/curbuf and a few other things */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02002766 if (eap->cmdidx != CMD_diffget)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002767 {
2768 /* Syncing undo only works for the current buffer, but we change
2769 * another buffer. Sync undo if the command was typed. This isn't
2770 * 100% right when ":diffput" is used in a function or mapping. */
2771 if (KeyTyped)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002772 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002773 aucmd_restbuf(&aco);
2774 }
2775
2776 diff_busy = FALSE;
2777
2778 /* Check that the cursor is on a valid character and update it's position.
2779 * When there were filler lines the topline has become invalid. */
2780 check_cursor();
2781 changed_line_abv_curs();
2782
2783 /* Also need to redraw the other buffers. */
2784 diff_redraw(FALSE);
2785}
2786
2787#ifdef FEAT_FOLDING
2788/*
2789 * Update folds for all diff buffers for entry "dp".
2790 * Skip buffer with index "skip_idx".
2791 * When there are no diffs, all folds are removed.
2792 */
2793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002794diff_fold_update(diff_T *dp, int skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795{
2796 int i;
2797 win_T *wp;
2798
Bram Moolenaar29323592016-07-24 22:04:11 +02002799 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002800 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002801 if (curtab->tp_diffbuf[i] == wp->w_buffer && i != skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002802 foldUpdate(wp, dp->df_lnum[i],
2803 dp->df_lnum[i] + dp->df_count[i]);
2804}
2805#endif
2806
2807/*
2808 * Return TRUE if buffer "buf" is in diff-mode.
2809 */
2810 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002811diff_mode_buf(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002812{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002813 tabpage_T *tp;
2814
Bram Moolenaar29323592016-07-24 22:04:11 +02002815 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002816 if (diff_buf_idx_tp(buf, tp) != DB_COUNT)
2817 return TRUE;
2818 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002819}
2820
2821/*
2822 * Move "count" times in direction "dir" to the next diff block.
2823 * Return FAIL if there isn't such a diff block.
2824 */
2825 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002826diff_move_to(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002827{
2828 int idx;
2829 linenr_T lnum = curwin->w_cursor.lnum;
2830 diff_T *dp;
2831
2832 idx = diff_buf_idx(curbuf);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002833 if (idx == DB_COUNT || curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002834 return FAIL;
2835
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002836 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002837 ex_diffupdate(NULL); /* update after a big change */
2838
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002839 if (curtab->tp_first_diff == NULL) /* no diffs today */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002840 return FAIL;
2841
2842 while (--count >= 0)
2843 {
2844 /* Check if already before first diff. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002845 if (dir == BACKWARD && lnum <= curtab->tp_first_diff->df_lnum[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846 break;
2847
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002848 for (dp = curtab->tp_first_diff; ; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 {
2850 if (dp == NULL)
2851 break;
2852 if ((dir == FORWARD && lnum < dp->df_lnum[idx])
2853 || (dir == BACKWARD
2854 && (dp->df_next == NULL
2855 || lnum <= dp->df_next->df_lnum[idx])))
2856 {
2857 lnum = dp->df_lnum[idx];
2858 break;
2859 }
2860 }
2861 }
2862
2863 /* don't end up past the end of the file */
2864 if (lnum > curbuf->b_ml.ml_line_count)
2865 lnum = curbuf->b_ml.ml_line_count;
2866
2867 /* When the cursor didn't move at all we fail. */
2868 if (lnum == curwin->w_cursor.lnum)
2869 return FAIL;
2870
2871 setpcmark();
2872 curwin->w_cursor.lnum = lnum;
2873 curwin->w_cursor.col = 0;
2874
2875 return OK;
2876}
2877
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002878/*
2879 * Return the line number in the current window that is closest to "lnum1" in
2880 * "buf1" in diff mode.
2881 */
2882 static linenr_T
2883diff_get_corresponding_line_int(
Bram Moolenaar7454a062016-01-30 15:14:10 +01002884 buf_T *buf1,
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002885 linenr_T lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002886{
2887 int idx1;
2888 int idx2;
2889 diff_T *dp;
2890 int baseline = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002891
2892 idx1 = diff_buf_idx(buf1);
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002893 idx2 = diff_buf_idx(curbuf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002894 if (idx1 == DB_COUNT || idx2 == DB_COUNT || curtab->tp_first_diff == NULL)
2895 return lnum1;
2896
2897 if (curtab->tp_diff_invalid)
2898 ex_diffupdate(NULL); /* update after a big change */
2899
2900 if (curtab->tp_first_diff == NULL) /* no diffs today */
2901 return lnum1;
2902
2903 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
2904 {
2905 if (dp->df_lnum[idx1] > lnum1)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002906 return lnum1 - baseline;
2907 if ((dp->df_lnum[idx1] + dp->df_count[idx1]) > lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002908 {
2909 /* Inside the diffblock */
2910 baseline = lnum1 - dp->df_lnum[idx1];
2911 if (baseline > dp->df_count[idx2])
2912 baseline = dp->df_count[idx2];
2913
2914 return dp->df_lnum[idx2] + baseline;
2915 }
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002916 if ( (dp->df_lnum[idx1] == lnum1)
2917 && (dp->df_count[idx1] == 0)
2918 && (dp->df_lnum[idx2] <= curwin->w_cursor.lnum)
2919 && ((dp->df_lnum[idx2] + dp->df_count[idx2])
2920 > curwin->w_cursor.lnum))
Bram Moolenaar860cae12010-06-05 23:22:07 +02002921 /*
2922 * Special case: if the cursor is just after a zero-count
2923 * block (i.e. all filler) and the target cursor is already
2924 * inside the corresponding block, leave the target cursor
2925 * unmoved. This makes repeated CTRL-W W operations work
2926 * as expected.
2927 */
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002928 return curwin->w_cursor.lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002929 baseline = (dp->df_lnum[idx1] + dp->df_count[idx1])
2930 - (dp->df_lnum[idx2] + dp->df_count[idx2]);
2931 }
2932
2933 /* If we get here then the cursor is after the last diff */
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002934 return lnum1 - baseline;
2935}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002936
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002937/*
2938 * Return the line number in the current window that is closest to "lnum1" in
2939 * "buf1" in diff mode. Checks the line number to be valid.
2940 */
2941 linenr_T
2942diff_get_corresponding_line(buf_T *buf1, linenr_T lnum1)
2943{
2944 linenr_T lnum = diff_get_corresponding_line_int(buf1, lnum1);
2945
2946 /* don't end up past the end of the file */
2947 if (lnum > curbuf->b_ml.ml_line_count)
2948 return curbuf->b_ml.ml_line_count;
2949 return lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002950}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002951
Bram Moolenaar071d4272004-06-13 20:20:40 +00002952/*
2953 * For line "lnum" in the current window find the equivalent lnum in window
2954 * "wp", compensating for inserted/deleted lines.
2955 */
2956 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002957diff_lnum_win(linenr_T lnum, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002958{
2959 diff_T *dp;
2960 int idx;
2961 int i;
2962 linenr_T n;
2963
2964 idx = diff_buf_idx(curbuf);
2965 if (idx == DB_COUNT) /* safety check */
2966 return (linenr_T)0;
2967
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002968 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 ex_diffupdate(NULL); /* update after a big change */
2970
2971 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002972 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002973 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
2974 break;
2975
2976 /* When after the last change, compute relative to the last line number. */
2977 if (dp == NULL)
2978 return wp->w_buffer->b_ml.ml_line_count
2979 - (curbuf->b_ml.ml_line_count - lnum);
2980
2981 /* Find index for "wp". */
2982 i = diff_buf_idx(wp->w_buffer);
2983 if (i == DB_COUNT) /* safety check */
2984 return (linenr_T)0;
2985
2986 n = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]);
2987 if (n > dp->df_lnum[i] + dp->df_count[i])
2988 n = dp->df_lnum[i] + dp->df_count[i];
2989 return n;
2990}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991
Bram Moolenaare828b762018-09-10 17:51:58 +02002992/*
2993 * Handle an ED style diff line.
2994 * Return FAIL if the line does not contain diff info.
2995 */
2996 static int
2997parse_diff_ed(
2998 char_u *line,
2999 linenr_T *lnum_orig,
3000 long *count_orig,
3001 linenr_T *lnum_new,
3002 long *count_new)
3003{
3004 char_u *p;
3005 long f1, l1, f2, l2;
3006 int difftype;
3007
3008 // The line must be one of three formats:
3009 // change: {first}[,{last}]c{first}[,{last}]
3010 // append: {first}a{first}[,{last}]
3011 // delete: {first}[,{last}]d{first}
3012 p = line;
3013 f1 = getdigits(&p);
3014 if (*p == ',')
3015 {
3016 ++p;
3017 l1 = getdigits(&p);
3018 }
3019 else
3020 l1 = f1;
3021 if (*p != 'a' && *p != 'c' && *p != 'd')
3022 return FAIL; // invalid diff format
3023 difftype = *p++;
3024 f2 = getdigits(&p);
3025 if (*p == ',')
3026 {
3027 ++p;
3028 l2 = getdigits(&p);
3029 }
3030 else
3031 l2 = f2;
3032 if (l1 < f1 || l2 < f2)
3033 return FAIL;
3034
3035 if (difftype == 'a')
3036 {
3037 *lnum_orig = f1 + 1;
3038 *count_orig = 0;
3039 }
3040 else
3041 {
3042 *lnum_orig = f1;
3043 *count_orig = l1 - f1 + 1;
3044 }
3045 if (difftype == 'd')
3046 {
3047 *lnum_new = f2 + 1;
3048 *count_new = 0;
3049 }
3050 else
3051 {
3052 *lnum_new = f2;
3053 *count_new = l2 - f2 + 1;
3054 }
3055 return OK;
3056}
3057
3058/*
3059 * Parses unified diff with zero(!) context lines.
3060 * Return FAIL if there is no diff information in "line".
3061 */
3062 static int
3063parse_diff_unified(
3064 char_u *line,
3065 linenr_T *lnum_orig,
3066 long *count_orig,
3067 linenr_T *lnum_new,
3068 long *count_new)
3069{
3070 char_u *p;
3071 long oldline, oldcount, newline, newcount;
3072
3073 // Parse unified diff hunk header:
3074 // @@ -oldline,oldcount +newline,newcount @@
3075 p = line;
3076 if (*p++ == '@' && *p++ == '@' && *p++ == ' ' && *p++ == '-')
3077 {
3078 oldline = getdigits(&p);
3079 if (*p == ',')
3080 {
3081 ++p;
3082 oldcount = getdigits(&p);
3083 }
3084 else
3085 oldcount = 1;
3086 if (*p++ == ' ' && *p++ == '+')
3087 {
3088 newline = getdigits(&p);
3089 if (*p == ',')
3090 {
3091 ++p;
3092 newcount = getdigits(&p);
3093 }
3094 else
3095 newcount = 1;
3096 }
3097 else
3098 return FAIL; // invalid diff format
3099
3100 if (oldcount == 0)
3101 oldline += 1;
3102 if (newcount == 0)
3103 newline += 1;
3104 if (newline == 0)
3105 newline = 1;
3106
3107 *lnum_orig = oldline;
3108 *count_orig = oldcount;
3109 *lnum_new = newline;
3110 *count_new = newcount;
3111
3112 return OK;
3113 }
3114
3115 return FAIL;
3116}
3117
3118/*
3119 * Callback function for the xdl_diff() function.
3120 * Stores the diff output in a grow array.
3121 */
3122 static int
3123xdiff_out(void *priv, mmbuffer_t *mb, int nbuf)
3124{
3125 diffout_T *dout = (diffout_T *)priv;
3126 int i;
3127 char_u *p;
3128
3129 for (i = 0; i < nbuf; i++)
3130 {
3131 // We are only interested in the header lines, skip text lines.
3132 if (STRNCMP(mb[i].ptr, "@@ ", 3) != 0)
3133 continue;
3134 if (ga_grow(&dout->dout_ga, 1) == FAIL)
3135 return -1;
3136 p = vim_strnsave((char_u *)mb[i].ptr, mb[i].size);
3137 if (p == NULL)
3138 return -1;
3139 ((char_u **)dout->dout_ga.ga_data)[dout->dout_ga.ga_len++] = p;
3140 }
3141 return 0;
3142}
3143
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144#endif /* FEAT_DIFF */