blob: 809694a68c21e7fce022a7ab6f9ed8f54668a120 [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 Moolenaar785fc652018-09-15 19:17:38 +020027#define DIFF_FILLER 0x001 // display filler lines
28#define DIFF_IBLANK 0x002 // ignore empty lines
29#define DIFF_ICASE 0x004 // ignore case
30#define DIFF_IWHITE 0x008 // ignore change in white space
31#define DIFF_IWHITEALL 0x010 // ignore all white space changes
32#define DIFF_IWHITEEOL 0x020 // ignore change in white space at EOL
33#define DIFF_HORIZONTAL 0x040 // horizontal splits
34#define DIFF_VERTICAL 0x080 // vertical splits
35#define DIFF_HIDDEN_OFF 0x100 // diffoff when hidden
36#define DIFF_INTERNAL 0x200 // use internal xdiff algorithm
37#define ALL_WHITE_DIFF (DIFF_IWHITE | DIFF_IWHITEALL | DIFF_IWHITEEOL)
Bram Moolenaar274cea32018-09-12 18:00:12 +020038static int diff_flags = DIFF_INTERNAL | DIFF_FILLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000039
Bram Moolenaare828b762018-09-10 17:51:58 +020040static long diff_algorithm = 0;
41
Bram Moolenaar071d4272004-06-13 20:20:40 +000042#define LBUFLEN 50 /* length of line in diff file */
43
44static int diff_a_works = MAYBE; /* TRUE when "diff -a" works, FALSE when it
45 doesn't work, MAYBE when not checked yet */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010046#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000047static int diff_bin_works = MAYBE; /* TRUE when "diff --binary" works, FALSE
48 when it doesn't work, MAYBE when not
49 checked yet */
50#endif
51
Bram Moolenaare828b762018-09-10 17:51:58 +020052// used for diff input
53typedef struct {
54 char_u *din_fname; // used for external diff
55 mmfile_t din_mmfile; // used for internal diff
56} diffin_T;
57
58// used for diff result
59typedef struct {
60 char_u *dout_fname; // used for external diff
61 garray_T dout_ga; // used for internal diff
62} diffout_T;
63
64// two diff inputs and one result
65typedef struct {
66 diffin_T dio_orig; // original file input
67 diffin_T dio_new; // new file input
68 diffout_T dio_diff; // diff result
69 int dio_internal; // using internal diff
70} diffio_T;
71
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010072static int diff_buf_idx(buf_T *buf);
73static int diff_buf_idx_tp(buf_T *buf, tabpage_T *tp);
74static void diff_mark_adjust_tp(tabpage_T *tp, int idx, linenr_T line1, linenr_T line2, long amount, long amount_after);
75static void diff_check_unchanged(tabpage_T *tp, diff_T *dp);
76static int diff_check_sanity(tabpage_T *tp, diff_T *dp);
77static void diff_redraw(int dofold);
Bram Moolenaare828b762018-09-10 17:51:58 +020078static int check_external_diff(diffio_T *diffio);
79static int diff_file(diffio_T *diffio);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010080static int diff_equal_entry(diff_T *dp, int idx1, int idx2);
81static int diff_cmp(char_u *s1, char_u *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000082#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010083static void diff_fold_update(diff_T *dp, int skip_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#endif
Bram Moolenaare828b762018-09-10 17:51:58 +020085static void diff_read(int idx_orig, int idx_new, diffout_T *fname);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010086static void diff_copy_entry(diff_T *dprev, diff_T *dp, int idx_orig, int idx_new);
87static diff_T *diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp);
Bram Moolenaare828b762018-09-10 17:51:58 +020088static int parse_diff_ed(char_u *line, linenr_T *lnum_orig, long *count_orig, linenr_T *lnum_new, long *count_new);
89static int parse_diff_unified(char_u *line, linenr_T *lnum_orig, long *count_orig, linenr_T *lnum_new, long *count_new);
90static int xdiff_out(void *priv, mmbuffer_t *mb, int nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000091
92#ifndef USE_CR
93# define tag_fgets vim_fgets
94#endif
95
96/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000097 * Called when deleting or unloading a buffer: No longer make a diff with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 */
99 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100100diff_buf_delete(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000101{
102 int i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000103 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104
Bram Moolenaar29323592016-07-24 22:04:11 +0200105 FOR_ALL_TABPAGES(tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000106 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000107 i = diff_buf_idx_tp(buf, tp);
108 if (i != DB_COUNT)
109 {
110 tp->tp_diffbuf[i] = NULL;
111 tp->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000112 if (tp == curtab)
113 diff_redraw(TRUE);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000114 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 }
116}
117
118/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000119 * Check if the current buffer should be added to or removed from the list of
120 * diff buffers.
121 */
122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100123diff_buf_adjust(win_T *win)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000124{
125 win_T *wp;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000126 int i;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000127
128 if (!win->w_p_diff)
129 {
130 /* When there is no window showing a diff for this buffer, remove
131 * it from the diffs. */
Bram Moolenaar29323592016-07-24 22:04:11 +0200132 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000133 if (wp->w_buffer == win->w_buffer && wp->w_p_diff)
134 break;
135 if (wp == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000136 {
137 i = diff_buf_idx(win->w_buffer);
138 if (i != DB_COUNT)
139 {
140 curtab->tp_diffbuf[i] = NULL;
141 curtab->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000142 diff_redraw(TRUE);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000143 }
144 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000145 }
146 else
147 diff_buf_add(win->w_buffer);
148}
149
150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000151 * Add a buffer to make diffs for.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000152 * Call this when a new buffer is being edited in the current window where
153 * 'diff' is set.
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +0000154 * Marks the current buffer as being part of the diff and requiring updating.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000155 * This must be done before any autocmd, because a command may use info
156 * about the screen contents.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000157 */
158 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100159diff_buf_add(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160{
161 int i;
162
163 if (diff_buf_idx(buf) != DB_COUNT)
164 return; /* It's already there. */
165
166 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000167 if (curtab->tp_diffbuf[i] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000169 curtab->tp_diffbuf[i] = buf;
170 curtab->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000171 diff_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000172 return;
173 }
174
Bram Moolenaar89eaa412016-07-31 14:17:27 +0200175 EMSGN(_("E96: Cannot diff more than %ld buffers"), DB_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176}
177
178/*
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100179 * Remove all buffers to make diffs for.
180 */
181 static void
182diff_buf_clear(void)
183{
184 int i;
185
186 for (i = 0; i < DB_COUNT; ++i)
187 if (curtab->tp_diffbuf[i] != NULL)
188 {
189 curtab->tp_diffbuf[i] = NULL;
190 curtab->tp_diff_invalid = TRUE;
191 diff_redraw(TRUE);
192 }
193}
194
195/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000196 * Find buffer "buf" in the list of diff buffers for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197 * Return its index or DB_COUNT if not found.
198 */
199 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100200diff_buf_idx(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201{
202 int idx;
203
204 for (idx = 0; idx < DB_COUNT; ++idx)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000205 if (curtab->tp_diffbuf[idx] == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 break;
207 return idx;
208}
209
210/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000211 * Find buffer "buf" in the list of diff buffers for tab page "tp".
212 * Return its index or DB_COUNT if not found.
213 */
214 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100215diff_buf_idx_tp(buf_T *buf, tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000216{
217 int idx;
218
219 for (idx = 0; idx < DB_COUNT; ++idx)
220 if (tp->tp_diffbuf[idx] == buf)
221 break;
222 return idx;
223}
224
225/*
226 * Mark the diff info involving buffer "buf" as invalid, it will be updated
227 * when info is requested.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228 */
229 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100230diff_invalidate(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000232 tabpage_T *tp;
233 int i;
234
Bram Moolenaar29323592016-07-24 22:04:11 +0200235 FOR_ALL_TABPAGES(tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000237 i = diff_buf_idx_tp(buf, tp);
238 if (i != DB_COUNT)
239 {
240 tp->tp_diff_invalid = TRUE;
241 if (tp == curtab)
242 diff_redraw(TRUE);
243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 }
245}
246
247/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000248 * Called by mark_adjust(): update line numbers in "curbuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 */
250 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100251diff_mark_adjust(
252 linenr_T line1,
253 linenr_T line2,
254 long amount,
255 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000256{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000257 int idx;
258 tabpage_T *tp;
259
260 /* Handle all tab pages that use the current buffer in a diff. */
Bram Moolenaar29323592016-07-24 22:04:11 +0200261 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000262 {
263 idx = diff_buf_idx_tp(curbuf, tp);
264 if (idx != DB_COUNT)
265 diff_mark_adjust_tp(tp, idx, line1, line2, amount, amount_after);
266 }
267}
268
269/*
270 * Update line numbers in tab page "tp" for "curbuf" with index "idx".
271 * This attempts to update the changes as much as possible:
272 * When inserting/deleting lines outside of existing change blocks, create a
273 * new change block and update the line numbers in following blocks.
274 * When inserting/deleting lines in existing change blocks, update them.
275 */
276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100277diff_mark_adjust_tp(
278 tabpage_T *tp,
279 int idx,
280 linenr_T line1,
281 linenr_T line2,
282 long amount,
283 long amount_after)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000284{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285 diff_T *dp;
286 diff_T *dprev;
287 diff_T *dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288 int i;
289 int inserted, deleted;
290 int n, off;
291 linenr_T last;
292 linenr_T lnum_deleted = line1; /* lnum of remaining deletion */
293 int check_unchanged;
294
Bram Moolenaare3521d92018-09-16 14:10:31 +0200295 if (diff_internal())
296 {
297 // Will udpate diffs before redrawing. Set _invalid to update the
298 // diffs themselves, set _update to also update folds properly just
299 // before redrawing.
300 tp->tp_diff_invalid = TRUE;
301 tp->tp_diff_update = TRUE;
302 return;
303 }
304
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305 if (line2 == MAXLNUM)
306 {
307 /* mark_adjust(99, MAXLNUM, 9, 0): insert lines */
308 inserted = amount;
309 deleted = 0;
310 }
311 else if (amount_after > 0)
312 {
313 /* mark_adjust(99, 98, MAXLNUM, 9): a change that inserts lines*/
314 inserted = amount_after;
315 deleted = 0;
316 }
317 else
318 {
319 /* mark_adjust(98, 99, MAXLNUM, -2): delete lines */
320 inserted = 0;
321 deleted = -amount_after;
322 }
323
324 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000325 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000326 for (;;)
327 {
328 /* If the change is after the previous diff block and before the next
329 * diff block, thus not touching an existing change, create a new diff
330 * block. Don't do this when ex_diffgetput() is busy. */
331 if ((dp == NULL || dp->df_lnum[idx] - 1 > line2
332 || (line2 == MAXLNUM && dp->df_lnum[idx] > line1))
333 && (dprev == NULL
334 || dprev->df_lnum[idx] + dprev->df_count[idx] < line1)
335 && !diff_busy)
336 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000337 dnext = diff_alloc_new(tp, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 if (dnext == NULL)
339 return;
340
341 dnext->df_lnum[idx] = line1;
342 dnext->df_count[idx] = inserted;
343 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000344 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345 {
346 if (dprev == NULL)
347 dnext->df_lnum[i] = line1;
348 else
349 dnext->df_lnum[i] = line1
350 + (dprev->df_lnum[i] + dprev->df_count[i])
351 - (dprev->df_lnum[idx] + dprev->df_count[idx]);
352 dnext->df_count[i] = deleted;
353 }
354 }
355
356 /* if at end of the list, quit */
357 if (dp == NULL)
358 break;
359
360 /*
361 * Check for these situations:
362 * 1 2 3
363 * 1 2 3
364 * line1 2 3 4 5
365 * 2 3 4 5
366 * 2 3 4 5
367 * line2 2 3 4 5
368 * 3 5 6
369 * 3 5 6
370 */
371 /* compute last line of this change */
372 last = dp->df_lnum[idx] + dp->df_count[idx] - 1;
373
374 /* 1. change completely above line1: nothing to do */
375 if (last >= line1 - 1)
376 {
377 /* 6. change below line2: only adjust for amount_after; also when
378 * "deleted" became zero when deleted all lines between two diffs */
379 if (dp->df_lnum[idx] - (deleted + inserted != 0) > line2)
380 {
381 if (amount_after == 0)
382 break; /* nothing left to change */
383 dp->df_lnum[idx] += amount_after;
384 }
385 else
386 {
387 check_unchanged = FALSE;
388
389 /* 2. 3. 4. 5.: inserted/deleted lines touching this diff. */
390 if (deleted > 0)
391 {
392 if (dp->df_lnum[idx] >= line1)
393 {
394 off = dp->df_lnum[idx] - lnum_deleted;
395 if (last <= line2)
396 {
397 /* 4. delete all lines of diff */
398 if (dp->df_next != NULL
399 && dp->df_next->df_lnum[idx] - 1 <= line2)
400 {
401 /* delete continues in next diff, only do
402 * lines until that one */
403 n = dp->df_next->df_lnum[idx] - lnum_deleted;
404 deleted -= n;
405 n -= dp->df_count[idx];
406 lnum_deleted = dp->df_next->df_lnum[idx];
407 }
408 else
409 n = deleted - dp->df_count[idx];
410 dp->df_count[idx] = 0;
411 }
412 else
413 {
414 /* 5. delete lines at or just before top of diff */
415 n = off;
416 dp->df_count[idx] -= line2 - dp->df_lnum[idx] + 1;
417 check_unchanged = TRUE;
418 }
419 dp->df_lnum[idx] = line1;
420 }
421 else
422 {
423 off = 0;
424 if (last < line2)
425 {
426 /* 2. delete at end of of diff */
427 dp->df_count[idx] -= last - lnum_deleted + 1;
428 if (dp->df_next != NULL
429 && dp->df_next->df_lnum[idx] - 1 <= line2)
430 {
431 /* delete continues in next diff, only do
432 * lines until that one */
433 n = dp->df_next->df_lnum[idx] - 1 - last;
434 deleted -= dp->df_next->df_lnum[idx]
435 - lnum_deleted;
436 lnum_deleted = dp->df_next->df_lnum[idx];
437 }
438 else
439 n = line2 - last;
440 check_unchanged = TRUE;
441 }
442 else
443 {
444 /* 3. delete lines inside the diff */
445 n = 0;
446 dp->df_count[idx] -= deleted;
447 }
448 }
449
450 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000451 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000452 {
453 dp->df_lnum[i] -= off;
454 dp->df_count[i] += n;
455 }
456 }
457 else
458 {
459 if (dp->df_lnum[idx] <= line1)
460 {
461 /* inserted lines somewhere in this diff */
462 dp->df_count[idx] += inserted;
463 check_unchanged = TRUE;
464 }
465 else
466 /* inserted lines somewhere above this diff */
467 dp->df_lnum[idx] += inserted;
468 }
469
470 if (check_unchanged)
471 /* Check if inserted lines are equal, may reduce the
472 * size of the diff. TODO: also check for equal lines
473 * in the middle and perhaps split the block. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000474 diff_check_unchanged(tp, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000475 }
476 }
477
478 /* check if this block touches the previous one, may merge them. */
479 if (dprev != NULL && dprev->df_lnum[idx] + dprev->df_count[idx]
480 == dp->df_lnum[idx])
481 {
482 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000483 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000484 dprev->df_count[i] += dp->df_count[i];
485 dprev->df_next = dp->df_next;
486 vim_free(dp);
487 dp = dprev->df_next;
488 }
489 else
490 {
491 /* Advance to next entry. */
492 dprev = dp;
493 dp = dp->df_next;
494 }
495 }
496
497 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000498 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499 while (dp != NULL)
500 {
501 /* All counts are zero, remove this entry. */
502 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000503 if (tp->tp_diffbuf[i] != NULL && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 break;
505 if (i == DB_COUNT)
506 {
507 dnext = dp->df_next;
508 vim_free(dp);
509 dp = dnext;
510 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000511 tp->tp_first_diff = dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000512 else
513 dprev->df_next = dnext;
514 }
515 else
516 {
517 /* Advance to next entry. */
518 dprev = dp;
519 dp = dp->df_next;
520 }
521
522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000524 if (tp == curtab)
525 {
526 diff_redraw(TRUE);
527
528 /* Need to recompute the scroll binding, may remove or add filler
529 * lines (e.g., when adding lines above w_topline). But it's slow when
530 * making many changes, postpone until redrawing. */
531 diff_need_scrollbind = TRUE;
532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533}
534
535/*
536 * Allocate a new diff block and link it between "dprev" and "dp".
537 */
538 static diff_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100539diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540{
541 diff_T *dnew;
542
543 dnew = (diff_T *)alloc((unsigned)sizeof(diff_T));
544 if (dnew != NULL)
545 {
546 dnew->df_next = dp;
547 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000548 tp->tp_first_diff = dnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 else
550 dprev->df_next = dnew;
551 }
552 return dnew;
553}
554
555/*
556 * Check if the diff block "dp" can be made smaller for lines at the start and
557 * end that are equal. Called after inserting lines.
558 * This may result in a change where all buffers have zero lines, the caller
559 * must take care of removing it.
560 */
561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100562diff_check_unchanged(tabpage_T *tp, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563{
564 int i_org;
565 int i_new;
566 int off_org, off_new;
567 char_u *line_org;
568 int dir = FORWARD;
569
570 /* Find the first buffers, use it as the original, compare the other
571 * buffer lines against this one. */
572 for (i_org = 0; i_org < DB_COUNT; ++i_org)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000573 if (tp->tp_diffbuf[i_org] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574 break;
575 if (i_org == DB_COUNT) /* safety check */
576 return;
577
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000578 if (diff_check_sanity(tp, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 return;
580
581 /* First check lines at the top, then at the bottom. */
582 off_org = 0;
583 off_new = 0;
584 for (;;)
585 {
586 /* Repeat until a line is found which is different or the number of
587 * lines has become zero. */
588 while (dp->df_count[i_org] > 0)
589 {
590 /* Copy the line, the next ml_get() will invalidate it. */
591 if (dir == BACKWARD)
592 off_org = dp->df_count[i_org] - 1;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000593 line_org = vim_strsave(ml_get_buf(tp->tp_diffbuf[i_org],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 dp->df_lnum[i_org] + off_org, FALSE));
595 if (line_org == NULL)
596 return;
597 for (i_new = i_org + 1; i_new < DB_COUNT; ++i_new)
598 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000599 if (tp->tp_diffbuf[i_new] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000600 continue;
601 if (dir == BACKWARD)
602 off_new = dp->df_count[i_new] - 1;
603 /* if other buffer doesn't have this line, it was inserted */
604 if (off_new < 0 || off_new >= dp->df_count[i_new])
605 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000606 if (diff_cmp(line_org, ml_get_buf(tp->tp_diffbuf[i_new],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 dp->df_lnum[i_new] + off_new, FALSE)) != 0)
608 break;
609 }
610 vim_free(line_org);
611
612 /* Stop when a line isn't equal in all diff buffers. */
613 if (i_new != DB_COUNT)
614 break;
615
616 /* Line matched in all buffers, remove it from the diff. */
617 for (i_new = i_org; i_new < DB_COUNT; ++i_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000618 if (tp->tp_diffbuf[i_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 {
620 if (dir == FORWARD)
621 ++dp->df_lnum[i_new];
622 --dp->df_count[i_new];
623 }
624 }
625 if (dir == BACKWARD)
626 break;
627 dir = BACKWARD;
628 }
629}
630
631/*
632 * Check if a diff block doesn't contain invalid line numbers.
633 * This can happen when the diff program returns invalid results.
634 */
635 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100636diff_check_sanity(tabpage_T *tp, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000637{
638 int i;
639
640 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000641 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 if (dp->df_lnum[i] + dp->df_count[i] - 1
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000643 > tp->tp_diffbuf[i]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 return FAIL;
645 return OK;
646}
647
648/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000649 * Mark all diff buffers in the current tab page for redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 */
651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100652diff_redraw(
Bram Moolenaare3521d92018-09-16 14:10:31 +0200653 int dofold) // also recompute the folds
Bram Moolenaar071d4272004-06-13 20:20:40 +0000654{
655 win_T *wp;
656 int n;
657
Bram Moolenaar29323592016-07-24 22:04:11 +0200658 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 if (wp->w_p_diff)
660 {
Bram Moolenaar60f83772006-03-12 21:52:47 +0000661 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000662#ifdef FEAT_FOLDING
663 if (dofold && foldmethodIsDiff(wp))
664 foldUpdateAll(wp);
665#endif
666 /* A change may have made filler lines invalid, need to take care
667 * of that for other windows. */
Bram Moolenaara80888d2012-10-21 22:18:21 +0200668 n = diff_check(wp, wp->w_topline);
669 if ((wp != curwin && wp->w_topfill > 0) || n > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 if (wp->w_topfill > n)
672 wp->w_topfill = (n < 0 ? 0 : n);
Bram Moolenaara80888d2012-10-21 22:18:21 +0200673 else if (n > 0 && n > wp->w_topfill)
674 wp->w_topfill = n;
Bram Moolenaar846a2ff2014-05-28 11:35:37 +0200675 check_topfill(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 }
677 }
678}
679
Bram Moolenaare828b762018-09-10 17:51:58 +0200680 static void
681clear_diffin(diffin_T *din)
682{
683 if (din->din_fname == NULL)
684 {
685 vim_free(din->din_mmfile.ptr);
686 din->din_mmfile.ptr = NULL;
687 }
688 else
689 mch_remove(din->din_fname);
690}
691
692 static void
693clear_diffout(diffout_T *dout)
694{
695 if (dout->dout_fname == NULL)
696 ga_clear_strings(&dout->dout_ga);
697 else
698 mch_remove(dout->dout_fname);
699}
700
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701/*
Bram Moolenaare828b762018-09-10 17:51:58 +0200702 * Write buffer "buf" to a memory buffer.
703 * Return FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000704 */
705 static int
Bram Moolenaare828b762018-09-10 17:51:58 +0200706diff_write_buffer(buf_T *buf, diffin_T *din)
707{
708 linenr_T lnum;
709 char_u *s;
710 long len = 0;
711 char_u *ptr;
712
713 // xdiff requires one big block of memory with all the text.
714 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar6e272ac2018-09-16 14:51:36 +0200715 len += (long)STRLEN(ml_get_buf(buf, lnum, FALSE)) + 1;
Bram Moolenaare828b762018-09-10 17:51:58 +0200716 ptr = lalloc(len, TRUE);
717 if (ptr == NULL)
718 {
719 // Allocating memory failed. This can happen, because we try to read
720 // the whole buffer text into memory. Set the failed flag, the diff
721 // will be retried with external diff. The flag is never reset.
722 buf->b_diff_failed = TRUE;
723 if (p_verbose > 0)
724 {
725 verbose_enter();
726 smsg((char_u *)
727 _("Not enough memory to use internal diff for buffer \"%s\""),
728 buf->b_fname);
729 verbose_leave();
730 }
731 return FAIL;
732 }
733 din->din_mmfile.ptr = (char *)ptr;
734 din->din_mmfile.size = len;
735
736 len = 0;
737 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
738 {
739 for (s = ml_get_buf(buf, lnum, FALSE); *s != NUL; )
740 {
741 if (diff_flags & DIFF_ICASE)
742 {
743 int c;
744
745 // xdiff doesn't support ignoring case, fold-case the text.
746#ifdef FEAT_MBYTE
747 int orig_len;
748 char_u cbuf[MB_MAXBYTES + 1];
749
750 c = PTR2CHAR(s);
751 c = enc_utf8 ? utf_fold(c) : MB_TOLOWER(c);
752 orig_len = MB_PTR2LEN(s);
753 if (mb_char2bytes(c, cbuf) != orig_len)
754 // TODO: handle byte length difference
755 mch_memmove(ptr + len, s, orig_len);
756 else
757 mch_memmove(ptr + len, cbuf, orig_len);
758
759 s += orig_len;
760 len += orig_len;
761#else
762 c = *s++;
763 ptr[len++] = TOLOWER_LOC(c);
764#endif
765 }
766 else
767 ptr[len++] = *s++;
768 }
769 ptr[len++] = NL;
770 }
771 return OK;
772}
773
774/*
775 * Write buffer "buf" to file or memory buffer.
776 * Return FAIL for failure.
777 */
778 static int
779diff_write(buf_T *buf, diffin_T *din)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780{
781 int r;
782 char_u *save_ff;
783
Bram Moolenaare828b762018-09-10 17:51:58 +0200784 if (din->din_fname == NULL)
785 return diff_write_buffer(buf, din);
786
787 // Always use 'fileformat' set to "unix".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 save_ff = buf->b_p_ff;
789 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX);
Bram Moolenaare828b762018-09-10 17:51:58 +0200790 r = buf_write(buf, din->din_fname, NULL,
791 (linenr_T)1, buf->b_ml.ml_line_count,
792 NULL, FALSE, FALSE, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 free_string_option(buf->b_p_ff);
794 buf->b_p_ff = save_ff;
795 return r;
796}
797
798/*
Bram Moolenaare828b762018-09-10 17:51:58 +0200799 * Update the diffs for all buffers involved.
800 */
801 static void
802diff_try_update(
803 diffio_T *dio,
804 int idx_orig,
805 exarg_T *eap) // "eap" can be NULL
806{
807 buf_T *buf;
808 int idx_new;
809
810 if (dio->dio_internal)
811 {
812 ga_init2(&dio->dio_diff.dout_ga, sizeof(char *), 1000);
813 }
814 else
815 {
816 // We need three temp file names.
817 dio->dio_orig.din_fname = vim_tempname('o', TRUE);
818 dio->dio_new.din_fname = vim_tempname('n', TRUE);
819 dio->dio_diff.dout_fname = vim_tempname('d', TRUE);
820 if (dio->dio_orig.din_fname == NULL
821 || dio->dio_new.din_fname == NULL
822 || dio->dio_diff.dout_fname == NULL)
823 goto theend;
824 }
825
826 // Check external diff is actually working.
827 if (!dio->dio_internal && check_external_diff(dio) == FAIL)
828 goto theend;
829
830 // :diffupdate!
831 if (eap != NULL && eap->forceit)
832 for (idx_new = idx_orig; idx_new < DB_COUNT; ++idx_new)
833 {
834 buf = curtab->tp_diffbuf[idx_new];
835 if (buf_valid(buf))
836 buf_check_timestamp(buf, FALSE);
837 }
838
839 // Write the first buffer to a tempfile or mmfile_t.
840 buf = curtab->tp_diffbuf[idx_orig];
841 if (diff_write(buf, &dio->dio_orig) == FAIL)
842 goto theend;
843
844 // Make a difference between the first buffer and every other.
845 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
846 {
847 buf = curtab->tp_diffbuf[idx_new];
848 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
849 continue; // skip buffer that isn't loaded
850
851 // Write the other buffer and diff with the first one.
852 if (diff_write(buf, &dio->dio_new) == FAIL)
853 continue;
854 if (diff_file(dio) == FAIL)
855 continue;
856
857 // Read the diff output and add each entry to the diff list.
858 diff_read(idx_orig, idx_new, &dio->dio_diff);
859
860 clear_diffin(&dio->dio_new);
861 clear_diffout(&dio->dio_diff);
862 }
863 clear_diffin(&dio->dio_orig);
864
865theend:
866 vim_free(dio->dio_orig.din_fname);
867 vim_free(dio->dio_new.din_fname);
868 vim_free(dio->dio_diff.dout_fname);
869}
870
871/*
872 * Return TRUE if the options are set to use the internal diff library.
873 * Note that if the internal diff failed for one of the buffers, the external
874 * diff will be used anyway.
875 */
Bram Moolenaare3521d92018-09-16 14:10:31 +0200876 int
Bram Moolenaare828b762018-09-10 17:51:58 +0200877diff_internal(void)
878{
879 return (diff_flags & DIFF_INTERNAL) != 0 && *p_dex == NUL;
880}
881
882/*
883 * Return TRUE if the internal diff failed for one of the diff buffers.
884 */
885 static int
886diff_internal_failed(void)
887{
888 int idx;
889
890 // Only need to do something when there is another buffer.
891 for (idx = 0; idx < DB_COUNT; ++idx)
892 if (curtab->tp_diffbuf[idx] != NULL
893 && curtab->tp_diffbuf[idx]->b_diff_failed)
894 return TRUE;
895 return FALSE;
896}
897
898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 * Completely update the diffs for the buffers involved.
Bram Moolenaare3521d92018-09-16 14:10:31 +0200900 * When using the external "diff" command the buffers are written to a file,
901 * also for unmodified buffers (the file could have been produced by
902 * autocommands, e.g. the netrw plugin).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 void
Bram Moolenaare828b762018-09-10 17:51:58 +0200905ex_diffupdate(exarg_T *eap) // "eap" can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 int idx_orig;
908 int idx_new;
Bram Moolenaare828b762018-09-10 17:51:58 +0200909 diffio_T diffio;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000910
Bram Moolenaare828b762018-09-10 17:51:58 +0200911 // Delete all diffblocks.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000912 diff_clear(curtab);
913 curtab->tp_diff_invalid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000914
Bram Moolenaare828b762018-09-10 17:51:58 +0200915 // Use the first buffer as the original text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 for (idx_orig = 0; idx_orig < DB_COUNT; ++idx_orig)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000917 if (curtab->tp_diffbuf[idx_orig] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000918 break;
919 if (idx_orig == DB_COUNT)
920 return;
921
Bram Moolenaare828b762018-09-10 17:51:58 +0200922 // Only need to do something when there is another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000924 if (curtab->tp_diffbuf[idx_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 break;
926 if (idx_new == DB_COUNT)
927 return;
928
Bram Moolenaare828b762018-09-10 17:51:58 +0200929 // Only use the internal method if it did not fail for one of the buffers.
930 vim_memset(&diffio, 0, sizeof(diffio));
931 diffio.dio_internal = diff_internal() && !diff_internal_failed();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932
Bram Moolenaare828b762018-09-10 17:51:58 +0200933 diff_try_update(&diffio, idx_orig, eap);
934 if (diffio.dio_internal && diff_internal_failed())
935 {
936 // Internal diff failed, use external diff instead.
937 vim_memset(&diffio, 0, sizeof(diffio));
938 diff_try_update(&diffio, idx_orig, eap);
939 }
940
941 // force updating cursor position on screen
942 curwin->w_valid_cursor.lnum = 0;
943
944 diff_redraw(TRUE);
Bram Moolenaare8fa05b2018-09-16 15:48:06 +0200945
946 apply_autocmds(EVENT_DIFFUPDATED, NULL, NULL, FALSE, curbuf);
Bram Moolenaare828b762018-09-10 17:51:58 +0200947}
948
949/*
950 * Do a quick test if "diff" really works. Otherwise it looks like there
951 * are no differences. Can't use the return value, it's non-zero when
952 * there are differences.
953 */
954 static int
955check_external_diff(diffio_T *diffio)
956{
957 FILE *fd;
958 int ok;
959 int io_error = FALSE;
960
961 // May try twice, first with "-a" and then without.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000962 for (;;)
963 {
964 ok = FALSE;
Bram Moolenaare828b762018-09-10 17:51:58 +0200965 fd = mch_fopen((char *)diffio->dio_orig.din_fname, "w");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000966 if (fd == NULL)
967 io_error = TRUE;
968 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000970 if (fwrite("line1\n", (size_t)6, (size_t)1, fd) != 1)
971 io_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 fclose(fd);
Bram Moolenaare828b762018-09-10 17:51:58 +0200973 fd = mch_fopen((char *)diffio->dio_new.din_fname, "w");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000974 if (fd == NULL)
975 io_error = TRUE;
976 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000977 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000978 if (fwrite("line2\n", (size_t)6, (size_t)1, fd) != 1)
979 io_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 fclose(fd);
Bram Moolenaare828b762018-09-10 17:51:58 +0200981 fd = NULL;
982 if (diff_file(diffio) == OK)
983 fd = mch_fopen((char *)diffio->dio_diff.dout_fname, "r");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000984 if (fd == NULL)
985 io_error = TRUE;
986 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 {
988 char_u linebuf[LBUFLEN];
989
990 for (;;)
991 {
992 /* There must be a line that contains "1c1". */
993 if (tag_fgets(linebuf, LBUFLEN, fd))
994 break;
995 if (STRNCMP(linebuf, "1c1", 3) == 0)
996 ok = TRUE;
997 }
998 fclose(fd);
999 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001000 mch_remove(diffio->dio_diff.dout_fname);
1001 mch_remove(diffio->dio_new.din_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001003 mch_remove(diffio->dio_orig.din_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001004 }
1005
1006#ifdef FEAT_EVAL
1007 /* When using 'diffexpr' break here. */
1008 if (*p_dex != NUL)
1009 break;
1010#endif
1011
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001012#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 /* If the "-a" argument works, also check if "--binary" works. */
1014 if (ok && diff_a_works == MAYBE && diff_bin_works == MAYBE)
1015 {
1016 diff_a_works = TRUE;
1017 diff_bin_works = TRUE;
1018 continue;
1019 }
1020 if (!ok && diff_a_works == TRUE && diff_bin_works == TRUE)
1021 {
1022 /* Tried --binary, but it failed. "-a" works though. */
1023 diff_bin_works = FALSE;
1024 ok = TRUE;
1025 }
1026#endif
1027
1028 /* If we checked if "-a" works already, break here. */
1029 if (diff_a_works != MAYBE)
1030 break;
1031 diff_a_works = ok;
1032
1033 /* If "-a" works break here, otherwise retry without "-a". */
1034 if (ok)
1035 break;
1036 }
1037 if (!ok)
1038 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001039 if (io_error)
1040 EMSG(_("E810: Cannot read or write temp files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041 EMSG(_("E97: Cannot create diffs"));
1042 diff_a_works = MAYBE;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001043#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 diff_bin_works = MAYBE;
1045#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001046 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001048 return OK;
1049}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050
Bram Moolenaare828b762018-09-10 17:51:58 +02001051/*
1052 * Invoke the xdiff function.
1053 */
1054 static int
1055diff_file_internal(diffio_T *diffio)
1056{
1057 xpparam_t param;
1058 xdemitconf_t emit_cfg;
1059 xdemitcb_t emit_cb;
Bram Moolenaarbd1d5602012-05-18 18:47:17 +02001060
Bram Moolenaare828b762018-09-10 17:51:58 +02001061 vim_memset(&param, 0, sizeof(param));
1062 vim_memset(&emit_cfg, 0, sizeof(emit_cfg));
1063 vim_memset(&emit_cb, 0, sizeof(emit_cb));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
Bram Moolenaare828b762018-09-10 17:51:58 +02001065 param.flags = diff_algorithm;
1066
1067 if (diff_flags & DIFF_IWHITE)
1068 param.flags |= XDF_IGNORE_WHITESPACE_CHANGE;
Bram Moolenaar785fc652018-09-15 19:17:38 +02001069 if (diff_flags & DIFF_IWHITEALL)
1070 param.flags |= XDF_IGNORE_WHITESPACE;
1071 if (diff_flags & DIFF_IWHITEEOL)
1072 param.flags |= XDF_IGNORE_WHITESPACE_AT_EOL;
1073 if (diff_flags & DIFF_IBLANK)
1074 param.flags |= XDF_IGNORE_BLANK_LINES;
Bram Moolenaare828b762018-09-10 17:51:58 +02001075
1076 emit_cfg.ctxlen = 0; // don't need any diff_context here
1077 emit_cb.priv = &diffio->dio_diff;
1078 emit_cb.outf = xdiff_out;
1079 if (xdl_diff(&diffio->dio_orig.din_mmfile,
1080 &diffio->dio_new.din_mmfile,
1081 &param, &emit_cfg, &emit_cb) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001083 EMSG(_("E960: Problem creating the internal diff"));
1084 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001086 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087}
1088
1089/*
1090 * Make a diff between files "tmp_orig" and "tmp_new", results in "tmp_diff".
Bram Moolenaare828b762018-09-10 17:51:58 +02001091 * return OK or FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 */
Bram Moolenaare828b762018-09-10 17:51:58 +02001093 static int
1094diff_file(diffio_T *dio)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095{
1096 char_u *cmd;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001097 size_t len;
Bram Moolenaare828b762018-09-10 17:51:58 +02001098 char_u *tmp_orig = dio->dio_orig.din_fname;
1099 char_u *tmp_new = dio->dio_new.din_fname;
1100 char_u *tmp_diff = dio->dio_diff.dout_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101
1102#ifdef FEAT_EVAL
1103 if (*p_dex != NUL)
Bram Moolenaare828b762018-09-10 17:51:58 +02001104 {
1105 // Use 'diffexpr' to generate the diff file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106 eval_diff(tmp_orig, tmp_new, tmp_diff);
Bram Moolenaare828b762018-09-10 17:51:58 +02001107 return OK;
1108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 else
1110#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001111 // Use xdiff for generating the diff.
1112 if (dio->dio_internal)
1113 {
1114 return diff_file_internal(dio);
1115 }
1116 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001118 len = STRLEN(tmp_orig) + STRLEN(tmp_new)
1119 + STRLEN(tmp_diff) + STRLEN(p_srr) + 27;
1120 cmd = alloc((unsigned)len);
Bram Moolenaare828b762018-09-10 17:51:58 +02001121 if (cmd == NULL)
1122 return FAIL;
Bram Moolenaar95fb60a2005-03-08 22:29:20 +00001123
Bram Moolenaare828b762018-09-10 17:51:58 +02001124 // We don't want $DIFF_OPTIONS to get in the way.
1125 if (getenv("DIFF_OPTIONS"))
1126 vim_setenv((char_u *)"DIFF_OPTIONS", (char_u *)"");
1127
1128 // Build the diff command and execute it. Always use -a, binary
1129 // differences are of no use. Ignore errors, diff returns
1130 // non-zero when differences have been found.
Bram Moolenaar785fc652018-09-15 19:17:38 +02001131 vim_snprintf((char *)cmd, len, "diff %s%s%s%s%s%s%s%s %s",
Bram Moolenaare828b762018-09-10 17:51:58 +02001132 diff_a_works == FALSE ? "" : "-a ",
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001133#if defined(MSWIN)
Bram Moolenaare828b762018-09-10 17:51:58 +02001134 diff_bin_works == TRUE ? "--binary " : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135#else
Bram Moolenaare828b762018-09-10 17:51:58 +02001136 "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001138 (diff_flags & DIFF_IWHITE) ? "-b " : "",
Bram Moolenaar785fc652018-09-15 19:17:38 +02001139 (diff_flags & DIFF_IWHITEALL) ? "-w " : "",
1140 (diff_flags & DIFF_IWHITEEOL) ? "-Z " : "",
1141 (diff_flags & DIFF_IBLANK) ? "-B " : "",
Bram Moolenaare828b762018-09-10 17:51:58 +02001142 (diff_flags & DIFF_ICASE) ? "-i " : "",
1143 tmp_orig, tmp_new);
1144 append_redir(cmd, (int)len, p_srr, tmp_diff);
1145 block_autocmds(); // avoid ShellCmdPost stuff
1146 (void)call_shell(cmd, SHELL_FILTER|SHELL_SILENT|SHELL_DOOUT);
1147 unblock_autocmds();
1148 vim_free(cmd);
1149 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 }
1151}
1152
1153/*
1154 * Create a new version of a file from the current buffer and a diff file.
1155 * The buffer is written to a file, also for unmodified buffers (the file
1156 * could have been produced by autocommands, e.g. the netrw plugin).
1157 */
1158 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001159ex_diffpatch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160{
1161 char_u *tmp_orig; /* name of original temp file */
1162 char_u *tmp_new; /* name of patched temp file */
1163 char_u *buf = NULL;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001164 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001165 win_T *old_curwin = curwin;
1166 char_u *newname = NULL; /* name of patched file buffer */
1167#ifdef UNIX
1168 char_u dirbuf[MAXPATHL];
1169 char_u *fullname = NULL;
1170#endif
1171#ifdef FEAT_BROWSE
1172 char_u *browseFile = NULL;
1173 int browse_flag = cmdmod.browse;
1174#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02001175 stat_T st;
Bram Moolenaara95ab322017-03-11 19:21:53 +01001176 char_u *esc_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177
1178#ifdef FEAT_BROWSE
1179 if (cmdmod.browse)
1180 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001181 browseFile = do_browse(0, (char_u *)_("Patch file"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02001182 eap->arg, NULL, NULL,
1183 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 if (browseFile == NULL)
1185 return; /* operation cancelled */
1186 eap->arg = browseFile;
1187 cmdmod.browse = FALSE; /* don't let do_ecmd() browse again */
1188 }
1189#endif
1190
1191 /* We need two temp file names. */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001192 tmp_orig = vim_tempname('o', FALSE);
1193 tmp_new = vim_tempname('n', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 if (tmp_orig == NULL || tmp_new == NULL)
1195 goto theend;
1196
1197 /* Write the current buffer to "tmp_orig". */
1198 if (buf_write(curbuf, tmp_orig, NULL,
1199 (linenr_T)1, curbuf->b_ml.ml_line_count,
1200 NULL, FALSE, FALSE, FALSE, TRUE) == FAIL)
1201 goto theend;
1202
1203#ifdef UNIX
1204 /* Get the absolute path of the patchfile, changing directory below. */
1205 fullname = FullName_save(eap->arg, FALSE);
1206#endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001207 esc_name = vim_strsave_shellescape(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208# ifdef UNIX
Bram Moolenaara95ab322017-03-11 19:21:53 +01001209 fullname != NULL ? fullname :
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210# endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001211 eap->arg, TRUE, TRUE);
1212 if (esc_name == NULL)
1213 goto theend;
1214 buflen = STRLEN(tmp_orig) + STRLEN(esc_name) + STRLEN(tmp_new) + 16;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001215 buf = alloc((unsigned)buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 if (buf == NULL)
1217 goto theend;
1218
1219#ifdef UNIX
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +00001220 /* Temporarily chdir to /tmp, to avoid patching files in the current
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 * directory when the patch file contains more than one patch. When we
1222 * have our own temp dir use that instead, it will be cleaned up when we
1223 * exit (any .rej files created). Don't change directory if we can't
1224 * return to the current. */
1225 if (mch_dirname(dirbuf, MAXPATHL) != OK || mch_chdir((char *)dirbuf) != 0)
1226 dirbuf[0] = NUL;
1227 else
1228 {
1229# ifdef TEMPDIRNAMES
1230 if (vim_tempdir != NULL)
Bram Moolenaar42335f52018-09-13 15:33:43 +02001231 vim_ignored = mch_chdir((char *)vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 else
1233# endif
Bram Moolenaar42335f52018-09-13 15:33:43 +02001234 vim_ignored = mch_chdir("/tmp");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 shorten_fnames(TRUE);
1236 }
1237#endif
1238
1239#ifdef FEAT_EVAL
1240 if (*p_pex != NUL)
1241 /* Use 'patchexpr' to generate the new file. */
1242 eval_patch(tmp_orig,
1243# ifdef UNIX
1244 fullname != NULL ? fullname :
1245# endif
1246 eap->arg, tmp_new);
1247 else
1248#endif
1249 {
1250 /* Build the patch command and execute it. Ignore errors. Switch to
1251 * cooked mode to allow the user to respond to prompts. */
Bram Moolenaara95ab322017-03-11 19:21:53 +01001252 vim_snprintf((char *)buf, buflen, "patch -o %s %s < %s",
1253 tmp_new, tmp_orig, esc_name);
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001254 block_autocmds(); /* Avoid ShellCmdPost stuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 (void)call_shell(buf, SHELL_FILTER | SHELL_COOKED);
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001256 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 }
1258
1259#ifdef UNIX
1260 if (dirbuf[0] != NUL)
1261 {
1262 if (mch_chdir((char *)dirbuf) != 0)
1263 EMSG(_(e_prev_dir));
1264 shorten_fnames(TRUE);
1265 }
1266#endif
1267
1268 /* patch probably has written over the screen */
1269 redraw_later(CLEAR);
1270
1271 /* Delete any .orig or .rej file created. */
1272 STRCPY(buf, tmp_new);
1273 STRCAT(buf, ".orig");
1274 mch_remove(buf);
1275 STRCPY(buf, tmp_new);
1276 STRCAT(buf, ".rej");
1277 mch_remove(buf);
1278
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001279 /* Only continue if the output file was created. */
1280 if (mch_stat((char *)tmp_new, &st) < 0 || st.st_size == 0)
1281 EMSG(_("E816: Cannot read patch output"));
1282 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001284 if (curbuf->b_fname != NULL)
1285 {
1286 newname = vim_strnsave(curbuf->b_fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 (int)(STRLEN(curbuf->b_fname) + 4));
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001288 if (newname != NULL)
1289 STRCAT(newname, ".new");
1290 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001291
1292#ifdef FEAT_GUI
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001293 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294#endif
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001295 /* don't use a new tab page, each tab page has its own diffs */
1296 cmdmod.tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001297
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001298 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001300 /* Pretend it was a ":split fname" command */
1301 eap->cmdidx = CMD_split;
1302 eap->arg = tmp_new;
1303 do_exedit(eap, old_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001305 /* check that split worked and editing tmp_new */
1306 if (curwin != old_curwin && win_valid(old_curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001308 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1309 diff_win_options(curwin, TRUE);
1310 diff_win_options(old_curwin, TRUE);
1311
1312 if (newname != NULL)
1313 {
1314 /* do a ":file filename.new" on the patched buffer */
1315 eap->arg = newname;
1316 ex_file(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001318 /* Do filetype detection with the new name. */
1319 if (au_has_group((char_u *)"filetypedetect"))
1320 do_cmdline_cmd((char_u *)":doau filetypedetect BufRead");
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001321 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 }
1323 }
1324 }
1325
1326theend:
1327 if (tmp_orig != NULL)
1328 mch_remove(tmp_orig);
1329 vim_free(tmp_orig);
1330 if (tmp_new != NULL)
1331 mch_remove(tmp_new);
1332 vim_free(tmp_new);
1333 vim_free(newname);
1334 vim_free(buf);
1335#ifdef UNIX
1336 vim_free(fullname);
1337#endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001338 vim_free(esc_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339#ifdef FEAT_BROWSE
1340 vim_free(browseFile);
1341 cmdmod.browse = browse_flag;
1342#endif
1343}
1344
1345/*
1346 * Split the window and edit another file, setting options to show the diffs.
1347 */
1348 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001349ex_diffsplit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350{
1351 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001352 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001354 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355#ifdef FEAT_GUI
1356 need_mouse_correct = TRUE;
1357#endif
Bram Moolenaar46328f92016-08-28 15:39:57 +02001358 /* Need to compute w_fraction when no redraw happened yet. */
1359 validate_cursor();
1360 set_fraction(curwin);
1361
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001362 /* don't use a new tab page, each tab page has its own diffs */
1363 cmdmod.tab = 0;
1364
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001365 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 {
1367 /* Pretend it was a ":split fname" command */
1368 eap->cmdidx = CMD_split;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001369 curwin->w_p_diff = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 do_exedit(eap, old_curwin);
1371
1372 if (curwin != old_curwin) /* split must have worked */
1373 {
1374 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1375 diff_win_options(curwin, TRUE);
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001376 if (win_valid(old_curwin))
1377 {
1378 diff_win_options(old_curwin, TRUE);
1379
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001380 if (bufref_valid(&old_curbuf))
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001381 /* Move the cursor position to that of the old window. */
1382 curwin->w_cursor.lnum = diff_get_corresponding_line(
Bram Moolenaar025e3e02016-10-18 14:50:18 +02001383 old_curbuf.br_buf, old_curwin->w_cursor.lnum);
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001384 }
Bram Moolenaar46328f92016-08-28 15:39:57 +02001385 /* Now that lines are folded scroll to show the cursor at the same
1386 * relative position. */
1387 scroll_to_fraction(curwin, curwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
1389 }
1390}
1391
1392/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001393 * Set options to show diffs for the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001396ex_diffthis(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397{
1398 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1399 diff_win_options(curwin, TRUE);
1400}
1401
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001402 static void
1403set_diff_option(win_T *wp, int value)
1404{
1405 win_T *old_curwin = curwin;
1406
1407 curwin = wp;
1408 curbuf = curwin->w_buffer;
1409 ++curbuf_lock;
1410 set_option_value((char_u *)"diff", (long)value, NULL, OPT_LOCAL);
1411 --curbuf_lock;
1412 curwin = old_curwin;
1413 curbuf = curwin->w_buffer;
1414}
1415
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416/*
1417 * Set options in window "wp" for diff mode.
1418 */
1419 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001420diff_win_options(
1421 win_T *wp,
1422 int addbuf) /* Add buffer to diff. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423{
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001424# ifdef FEAT_FOLDING
1425 win_T *old_curwin = curwin;
1426
1427 /* close the manually opened folds */
1428 curwin = wp;
1429 newFoldLevel();
1430 curwin = old_curwin;
1431# endif
1432
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001433 /* Use 'scrollbind' and 'cursorbind' when available */
Bram Moolenaar43929962015-07-03 15:06:56 +02001434 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001435 wp->w_p_scb_save = wp->w_p_scb;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001436 wp->w_p_scb = TRUE;
Bram Moolenaar43929962015-07-03 15:06:56 +02001437 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001438 wp->w_p_crb_save = wp->w_p_crb;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001439 wp->w_p_crb = TRUE;
Bram Moolenaar43929962015-07-03 15:06:56 +02001440 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001441 wp->w_p_wrap_save = wp->w_p_wrap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 wp->w_p_wrap = FALSE;
1443# ifdef FEAT_FOLDING
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001444 curwin = wp;
1445 curbuf = curwin->w_buffer;
Bram Moolenaar43929962015-07-03 15:06:56 +02001446 if (!wp->w_p_diff)
1447 {
1448 if (wp->w_p_diff_saved)
1449 free_string_option(wp->w_p_fdm_save);
Bram Moolenaara87aa802013-07-03 15:47:03 +02001450 wp->w_p_fdm_save = vim_strsave(wp->w_p_fdm);
Bram Moolenaar43929962015-07-03 15:06:56 +02001451 }
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001452 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"diff",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001453 OPT_LOCAL|OPT_FREE, 0);
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001454 curwin = old_curwin;
1455 curbuf = curwin->w_buffer;
Bram Moolenaar43929962015-07-03 15:06:56 +02001456 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001457 {
1458 wp->w_p_fdc_save = wp->w_p_fdc;
1459 wp->w_p_fen_save = wp->w_p_fen;
1460 wp->w_p_fdl_save = wp->w_p_fdl;
1461 }
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001462 wp->w_p_fdc = diff_foldcolumn;
1463 wp->w_p_fen = TRUE;
1464 wp->w_p_fdl = 0;
1465 foldUpdateAll(wp);
1466 /* make sure topline is not halfway a fold */
1467 changed_window_setting_win(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469 if (vim_strchr(p_sbo, 'h') == NULL)
1470 do_cmdline_cmd((char_u *)"set sbo+=hor");
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001471 /* Save the current values, to be restored in ex_diffoff(). */
Bram Moolenaara87aa802013-07-03 15:47:03 +02001472 wp->w_p_diff_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001474 set_diff_option(wp, TRUE);
Bram Moolenaar43929962015-07-03 15:06:56 +02001475
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if (addbuf)
1477 diff_buf_add(wp->w_buffer);
1478 redraw_win_later(wp, NOT_VALID);
1479}
1480
1481/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001482 * Set options not to show diffs. For the current window or all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001483 * Only in the current tab page.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001484 */
1485 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001486ex_diffoff(exarg_T *eap)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001487{
1488 win_T *wp;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001489 int diffwin = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001490
Bram Moolenaar29323592016-07-24 22:04:11 +02001491 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001492 {
Bram Moolenaar00462ff2013-09-20 20:13:53 +02001493 if (eap->forceit ? wp->w_p_diff : wp == curwin)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001494 {
Bram Moolenaar43929962015-07-03 15:06:56 +02001495 /* Set 'diff' off. If option values were saved in
1496 * diff_win_options(), restore the ones whose settings seem to have
1497 * been left over from diff mode. */
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001498 set_diff_option(wp, FALSE);
Bram Moolenaara87aa802013-07-03 15:47:03 +02001499
Bram Moolenaara87aa802013-07-03 15:47:03 +02001500 if (wp->w_p_diff_saved)
1501 {
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001502
Bram Moolenaar43929962015-07-03 15:06:56 +02001503 if (wp->w_p_scb)
1504 wp->w_p_scb = wp->w_p_scb_save;
Bram Moolenaar43929962015-07-03 15:06:56 +02001505 if (wp->w_p_crb)
1506 wp->w_p_crb = wp->w_p_crb_save;
Bram Moolenaar43929962015-07-03 15:06:56 +02001507 if (!wp->w_p_wrap)
1508 wp->w_p_wrap = wp->w_p_wrap_save;
1509#ifdef FEAT_FOLDING
1510 free_string_option(wp->w_p_fdm);
Bram Moolenaar79a213d2017-05-16 13:15:18 +02001511 wp->w_p_fdm = vim_strsave(
1512 *wp->w_p_fdm_save ? wp->w_p_fdm_save : (char_u*)"manual");
Bram Moolenaar43929962015-07-03 15:06:56 +02001513
1514 if (wp->w_p_fdc == diff_foldcolumn)
1515 wp->w_p_fdc = wp->w_p_fdc_save;
1516 if (wp->w_p_fdl == 0)
1517 wp->w_p_fdl = wp->w_p_fdl_save;
1518
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001519 /* Only restore 'foldenable' when 'foldmethod' is not
1520 * "manual", otherwise we continue to show the diff folds. */
Bram Moolenaar43929962015-07-03 15:06:56 +02001521 if (wp->w_p_fen)
1522 wp->w_p_fen = foldmethodIsManual(wp) ? FALSE
1523 : wp->w_p_fen_save;
1524
1525 foldUpdateAll(wp);
Bram Moolenaar43929962015-07-03 15:06:56 +02001526#endif
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001527 }
Bram Moolenaare67d5462016-08-27 22:40:42 +02001528 /* remove filler lines */
1529 wp->w_topfill = 0;
1530
1531 /* make sure topline is not halfway a fold and cursor is
1532 * invalidated */
1533 changed_window_setting_win(wp);
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001534
Bram Moolenaara87aa802013-07-03 15:47:03 +02001535 /* Note: 'sbo' is not restored, it's a global option. */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001536 diff_buf_adjust(wp);
1537 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001538 diffwin |= wp->w_p_diff;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001539 }
1540
Bram Moolenaar25ea0542017-02-03 23:16:28 +01001541 /* Also remove hidden buffers from the list. */
1542 if (eap->forceit)
1543 diff_buf_clear();
1544
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001545 /* Remove "hor" from from 'scrollopt' if there are no diff windows left. */
1546 if (!diffwin && vim_strchr(p_sbo, 'h') != NULL)
1547 do_cmdline_cmd((char_u *)"set sbo-=hor");
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001548}
1549
1550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 * Read the diff output and add each entry to the diff list.
1552 */
1553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001554diff_read(
Bram Moolenaare828b762018-09-10 17:51:58 +02001555 int idx_orig, // idx of original file
1556 int idx_new, // idx of new file
1557 diffout_T *dout) // diff output
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558{
Bram Moolenaare828b762018-09-10 17:51:58 +02001559 FILE *fd = NULL;
1560 int line_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 diff_T *dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001562 diff_T *dp = curtab->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 diff_T *dn, *dpl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564 char_u linebuf[LBUFLEN]; /* only need to hold the diff line */
Bram Moolenaare828b762018-09-10 17:51:58 +02001565 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001566 long off;
1567 int i;
1568 linenr_T lnum_orig, lnum_new;
1569 long count_orig, count_new;
1570 int notset = TRUE; /* block "*dp" not set yet */
Bram Moolenaare828b762018-09-10 17:51:58 +02001571 enum {
1572 DIFF_ED,
1573 DIFF_UNIFIED,
1574 DIFF_NONE
1575 } diffstyle = DIFF_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576
Bram Moolenaare828b762018-09-10 17:51:58 +02001577 if (dout->dout_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001579 diffstyle = DIFF_UNIFIED;
1580 }
1581 else
1582 {
1583 fd = mch_fopen((char *)dout->dout_fname, "r");
1584 if (fd == NULL)
1585 {
1586 EMSG(_("E98: Cannot read diff output"));
1587 return;
1588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 }
1590
1591 for (;;)
1592 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001593 if (fd == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001594 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001595 if (line_idx >= dout->dout_ga.ga_len)
1596 break; // did last line
1597 line = ((char_u **)dout->dout_ga.ga_data)[line_idx++];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 }
1599 else
1600 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001601 if (tag_fgets(linebuf, LBUFLEN, fd))
1602 break; // end of file
1603 line = linebuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001604 }
1605
Bram Moolenaare828b762018-09-10 17:51:58 +02001606 if (diffstyle == DIFF_NONE)
1607 {
1608 // Determine diff style.
1609 // ed like diff looks like this:
1610 // {first}[,{last}]c{first}[,{last}]
1611 // {first}a{first}[,{last}]
1612 // {first}[,{last}]d{first}
1613 //
1614 // unified diff looks like this:
1615 // --- file1 2018-03-20 13:23:35.783153140 +0100
1616 // +++ file2 2018-03-20 13:23:41.183156066 +0100
1617 // @@ -1,3 +1,5 @@
1618 if (isdigit(*line))
1619 diffstyle = DIFF_ED;
1620 else if ((STRNCMP(line, "@@ ", 3) == 0))
1621 diffstyle = DIFF_UNIFIED;
1622 else if ((STRNCMP(line, "--- ", 4) == 0)
1623 && (tag_fgets(linebuf, LBUFLEN, fd) == 0)
1624 && (STRNCMP(line, "+++ ", 4) == 0)
1625 && (tag_fgets(linebuf, LBUFLEN, fd) == 0)
1626 && (STRNCMP(line, "@@ ", 3) == 0))
1627 diffstyle = DIFF_UNIFIED;
Bram Moolenaar3b8defd2018-09-13 13:03:11 +02001628 else
1629 // Format not recognized yet, skip over this line. Cygwin diff
1630 // may put a warning at the start of the file.
1631 continue;
Bram Moolenaare828b762018-09-10 17:51:58 +02001632 }
1633
1634 if (diffstyle == DIFF_ED)
1635 {
1636 if (!isdigit(*line))
1637 continue; // not the start of a diff block
1638 if (parse_diff_ed(line, &lnum_orig, &count_orig,
1639 &lnum_new, &count_new) == FAIL)
1640 continue;
1641 }
1642 else if (diffstyle == DIFF_UNIFIED)
1643 {
1644 if (STRNCMP(line, "@@ ", 3) != 0)
1645 continue; // not the start of a diff block
1646 if (parse_diff_unified(line, &lnum_orig, &count_orig,
1647 &lnum_new, &count_new) == FAIL)
1648 continue;
1649 }
1650 else
1651 {
1652 EMSG(_("E959: Invalid diff format."));
1653 break;
1654 }
1655
1656 // Go over blocks before the change, for which orig and new are equal.
1657 // Copy blocks from orig to new.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 while (dp != NULL
1659 && lnum_orig > dp->df_lnum[idx_orig] + dp->df_count[idx_orig])
1660 {
1661 if (notset)
1662 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1663 dprev = dp;
1664 dp = dp->df_next;
1665 notset = TRUE;
1666 }
1667
1668 if (dp != NULL
1669 && lnum_orig <= dp->df_lnum[idx_orig] + dp->df_count[idx_orig]
1670 && lnum_orig + count_orig >= dp->df_lnum[idx_orig])
1671 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001672 // New block overlaps with existing block(s).
1673 // First find last block that overlaps.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 for (dpl = dp; dpl->df_next != NULL; dpl = dpl->df_next)
1675 if (lnum_orig + count_orig < dpl->df_next->df_lnum[idx_orig])
1676 break;
1677
Bram Moolenaare828b762018-09-10 17:51:58 +02001678 // If the newly found block starts before the old one, set the
1679 // start back a number of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 off = dp->df_lnum[idx_orig] - lnum_orig;
1681 if (off > 0)
1682 {
1683 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001684 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 dp->df_lnum[i] -= off;
1686 dp->df_lnum[idx_new] = lnum_new;
1687 dp->df_count[idx_new] = count_new;
1688 }
1689 else if (notset)
1690 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001691 // new block inside existing one, adjust new block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 dp->df_lnum[idx_new] = lnum_new + off;
1693 dp->df_count[idx_new] = count_new - off;
1694 }
1695 else
Bram Moolenaare828b762018-09-10 17:51:58 +02001696 // second overlap of new block with existing block
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001697 dp->df_count[idx_new] += count_new - count_orig
1698 + dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]
1699 - (dp->df_lnum[idx_orig] + dp->df_count[idx_orig]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700
Bram Moolenaare828b762018-09-10 17:51:58 +02001701 // Adjust the size of the block to include all the lines to the
1702 // end of the existing block or the new diff, whatever ends last.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703 off = (lnum_orig + count_orig)
1704 - (dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]);
1705 if (off < 0)
1706 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001707 // new change ends in existing block, adjust the end if not
1708 // done already
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709 if (notset)
1710 dp->df_count[idx_new] += -off;
1711 off = 0;
1712 }
Bram Moolenaard4b96bc2007-10-19 15:33:39 +00001713 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001714 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 dp->df_count[i] = dpl->df_lnum[i] + dpl->df_count[i]
1716 - dp->df_lnum[i] + off;
1717
Bram Moolenaare828b762018-09-10 17:51:58 +02001718 // Delete the diff blocks that have been merged into one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 dn = dp->df_next;
1720 dp->df_next = dpl->df_next;
1721 while (dn != dp->df_next)
1722 {
1723 dpl = dn->df_next;
1724 vim_free(dn);
1725 dn = dpl;
1726 }
1727 }
1728 else
1729 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001730 // Allocate a new diffblock.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001731 dp = diff_alloc_new(curtab, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732 if (dp == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001733 goto done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001734
1735 dp->df_lnum[idx_orig] = lnum_orig;
1736 dp->df_count[idx_orig] = count_orig;
1737 dp->df_lnum[idx_new] = lnum_new;
1738 dp->df_count[idx_new] = count_new;
1739
Bram Moolenaare828b762018-09-10 17:51:58 +02001740 // Set values for other buffers, these must be equal to the
1741 // original buffer, otherwise there would have been a change
1742 // already.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 for (i = idx_orig + 1; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001744 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 diff_copy_entry(dprev, dp, idx_orig, i);
1746 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001747 notset = FALSE; // "*dp" has been set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 }
1749
Bram Moolenaare828b762018-09-10 17:51:58 +02001750 // for remaining diff blocks orig and new are equal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001751 while (dp != NULL)
1752 {
1753 if (notset)
1754 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1755 dprev = dp;
1756 dp = dp->df_next;
1757 notset = TRUE;
1758 }
1759
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001760done:
Bram Moolenaare828b762018-09-10 17:51:58 +02001761 if (fd != NULL)
1762 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763}
1764
1765/*
1766 * Copy an entry at "dp" from "idx_orig" to "idx_new".
1767 */
1768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001769diff_copy_entry(
1770 diff_T *dprev,
1771 diff_T *dp,
1772 int idx_orig,
1773 int idx_new)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774{
1775 long off;
1776
1777 if (dprev == NULL)
1778 off = 0;
1779 else
1780 off = (dprev->df_lnum[idx_orig] + dprev->df_count[idx_orig])
1781 - (dprev->df_lnum[idx_new] + dprev->df_count[idx_new]);
1782 dp->df_lnum[idx_new] = dp->df_lnum[idx_orig] - off;
1783 dp->df_count[idx_new] = dp->df_count[idx_orig];
1784}
1785
1786/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001787 * Clear the list of diffblocks for tab page "tp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788 */
Bram Moolenaarea408852005-06-25 22:49:46 +00001789 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001790diff_clear(tabpage_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791{
1792 diff_T *p, *next_p;
1793
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001794 for (p = tp->tp_first_diff; p != NULL; p = next_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 {
1796 next_p = p->df_next;
1797 vim_free(p);
1798 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001799 tp->tp_first_diff = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800}
1801
1802/*
1803 * Check diff status for line "lnum" in buffer "buf":
1804 * Returns 0 for nothing special
1805 * Returns -1 for a line that should be highlighted as changed.
1806 * Returns -2 for a line that should be highlighted as added/deleted.
1807 * Returns > 0 for inserting that many filler lines above it (never happens
1808 * when 'diffopt' doesn't contain "filler").
1809 * This should only be used for windows where 'diff' is set.
1810 */
1811 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001812diff_check(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001814 int idx; /* index in tp_diffbuf[] for this buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 diff_T *dp;
1816 int maxcount;
1817 int i;
1818 buf_T *buf = wp->w_buffer;
1819 int cmp;
1820
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001821 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 ex_diffupdate(NULL); /* update after a big change */
1823
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001824 if (curtab->tp_first_diff == NULL || !wp->w_p_diff) /* no diffs at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 return 0;
1826
1827 /* safety check: "lnum" must be a buffer line */
1828 if (lnum < 1 || lnum > buf->b_ml.ml_line_count + 1)
1829 return 0;
1830
1831 idx = diff_buf_idx(buf);
1832 if (idx == DB_COUNT)
1833 return 0; /* no diffs for buffer "buf" */
1834
1835#ifdef FEAT_FOLDING
1836 /* A closed fold never has filler lines. */
1837 if (hasFoldingWin(wp, lnum, NULL, NULL, TRUE, NULL))
1838 return 0;
1839#endif
1840
1841 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001842 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
1844 break;
1845 if (dp == NULL || lnum < dp->df_lnum[idx])
1846 return 0;
1847
1848 if (lnum < dp->df_lnum[idx] + dp->df_count[idx])
1849 {
1850 int zero = FALSE;
1851
1852 /* Changed or inserted line. If the other buffers have a count of
1853 * zero, the lines were inserted. If the other buffers have the same
1854 * count, check if the lines are identical. */
1855 cmp = FALSE;
1856 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001857 if (i != idx && curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 {
1859 if (dp->df_count[i] == 0)
1860 zero = TRUE;
1861 else
1862 {
1863 if (dp->df_count[i] != dp->df_count[idx])
1864 return -1; /* nr of lines changed. */
1865 cmp = TRUE;
1866 }
1867 }
1868 if (cmp)
1869 {
1870 /* Compare all lines. If they are equal the lines were inserted
1871 * in some buffers, deleted in others, but not changed. */
1872 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001873 if (i != idx && curtab->tp_diffbuf[i] != NULL
1874 && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (!diff_equal_entry(dp, idx, i))
1876 return -1;
1877 }
1878 /* If there is no buffer with zero lines then there is no difference
1879 * any longer. Happens when making a change (or undo) that removes
1880 * the difference. Can't remove the entry here, we might be halfway
1881 * updating the window. Just report the text as unchanged. Other
1882 * windows might still show the change though. */
1883 if (zero == FALSE)
1884 return 0;
1885 return -2;
1886 }
1887
1888 /* If 'diffopt' doesn't contain "filler", return 0. */
1889 if (!(diff_flags & DIFF_FILLER))
1890 return 0;
1891
1892 /* Insert filler lines above the line just below the change. Will return
1893 * 0 when this buf had the max count. */
1894 maxcount = 0;
1895 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001896 if (curtab->tp_diffbuf[i] != NULL && dp->df_count[i] > maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 maxcount = dp->df_count[i];
1898 return maxcount - dp->df_count[idx];
1899}
1900
1901/*
1902 * Compare two entries in diff "*dp" and return TRUE if they are equal.
1903 */
1904 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001905diff_equal_entry(diff_T *dp, int idx1, int idx2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906{
1907 int i;
1908 char_u *line;
1909 int cmp;
1910
1911 if (dp->df_count[idx1] != dp->df_count[idx2])
1912 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001913 if (diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001914 return FALSE;
1915 for (i = 0; i < dp->df_count[idx1]; ++i)
1916 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001917 line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 dp->df_lnum[idx1] + i, FALSE));
1919 if (line == NULL)
1920 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001921 cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001922 dp->df_lnum[idx2] + i, FALSE));
1923 vim_free(line);
1924 if (cmp != 0)
1925 return FALSE;
1926 }
1927 return TRUE;
1928}
1929
1930/*
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02001931 * Compare the characters at "p1" and "p2". If they are equal (possibly
1932 * ignoring case) return TRUE and set "len" to the number of bytes.
1933 */
1934 static int
1935diff_equal_char(char_u *p1, char_u *p2, int *len)
1936{
1937#ifdef FEAT_MBYTE
1938 int l = (*mb_ptr2len)(p1);
1939
1940 if (l != (*mb_ptr2len)(p2))
1941 return FALSE;
1942 if (l > 1)
1943 {
1944 if (STRNCMP(p1, p2, l) != 0
1945 && (!enc_utf8
1946 || !(diff_flags & DIFF_ICASE)
1947 || utf_fold(utf_ptr2char(p1))
1948 != utf_fold(utf_ptr2char(p2))))
1949 return FALSE;
1950 *len = l;
1951 }
1952 else
1953#endif
1954 {
1955 if ((*p1 != *p2)
1956 && (!(diff_flags & DIFF_ICASE)
1957 || TOLOWER_LOC(*p1) != TOLOWER_LOC(*p2)))
1958 return FALSE;
1959 *len = 1;
1960 }
1961 return TRUE;
1962}
1963
1964/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 * Compare strings "s1" and "s2" according to 'diffopt'.
1966 * Return non-zero when they are different.
1967 */
1968 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001969diff_cmp(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970{
1971 char_u *p1, *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001973
Bram Moolenaar785fc652018-09-15 19:17:38 +02001974 if ((diff_flags & DIFF_IBLANK)
1975 && (*skipwhite(s1) == NUL || *skipwhite(s2) == NUL))
1976 return 0;
1977
1978 if ((diff_flags & (DIFF_ICASE | ALL_WHITE_DIFF)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 return STRCMP(s1, s2);
Bram Moolenaar785fc652018-09-15 19:17:38 +02001980 if ((diff_flags & DIFF_ICASE) && !(diff_flags & ALL_WHITE_DIFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 return MB_STRICMP(s1, s2);
1982
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 p1 = s1;
1984 p2 = s2;
Bram Moolenaar785fc652018-09-15 19:17:38 +02001985
1986 // Ignore white space changes and possibly ignore case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 while (*p1 != NUL && *p2 != NUL)
1988 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02001989 if (((diff_flags & DIFF_IWHITE)
1990 && VIM_ISWHITE(*p1) && VIM_ISWHITE(*p2))
1991 || ((diff_flags & DIFF_IWHITEALL)
1992 && (VIM_ISWHITE(*p1) || VIM_ISWHITE(*p2))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 {
1994 p1 = skipwhite(p1);
1995 p2 = skipwhite(p2);
1996 }
1997 else
1998 {
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02001999 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 break;
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002001 p1 += l;
2002 p2 += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002003 }
2004 }
2005
Bram Moolenaar785fc652018-09-15 19:17:38 +02002006 // Ignore trailing white space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 p1 = skipwhite(p1);
2008 p2 = skipwhite(p2);
2009 if (*p1 != NUL || *p2 != NUL)
2010 return 1;
2011 return 0;
2012}
2013
2014/*
2015 * Return the number of filler lines above "lnum".
2016 */
2017 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002018diff_check_fill(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019{
2020 int n;
2021
2022 /* be quick when there are no filler lines */
2023 if (!(diff_flags & DIFF_FILLER))
2024 return 0;
2025 n = diff_check(wp, lnum);
2026 if (n <= 0)
2027 return 0;
2028 return n;
2029}
2030
2031/*
2032 * Set the topline of "towin" to match the position in "fromwin", so that they
2033 * show the same diff'ed lines.
2034 */
2035 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002036diff_set_topline(win_T *fromwin, win_T *towin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037{
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002038 buf_T *frombuf = fromwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039 linenr_T lnum = fromwin->w_topline;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002040 int fromidx;
2041 int toidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 diff_T *dp;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002043 int max_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044 int i;
2045
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002046 fromidx = diff_buf_idx(frombuf);
2047 if (fromidx == DB_COUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 return; /* safety check */
2049
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002050 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 ex_diffupdate(NULL); /* update after a big change */
2052
2053 towin->w_topfill = 0;
2054
2055 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002056 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002057 if (lnum <= dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 break;
2059 if (dp == NULL)
2060 {
2061 /* After last change, compute topline relative to end of file; no
2062 * filler lines. */
2063 towin->w_topline = towin->w_buffer->b_ml.ml_line_count
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002064 - (frombuf->b_ml.ml_line_count - lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 }
2066 else
2067 {
2068 /* Find index for "towin". */
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002069 toidx = diff_buf_idx(towin->w_buffer);
2070 if (toidx == DB_COUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 return; /* safety check */
2072
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002073 towin->w_topline = lnum + (dp->df_lnum[toidx] - dp->df_lnum[fromidx]);
2074 if (lnum >= dp->df_lnum[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002076 /* Inside a change: compute filler lines. With three or more
2077 * buffers we need to know the largest count. */
2078 max_count = 0;
2079 for (i = 0; i < DB_COUNT; ++i)
2080 if (curtab->tp_diffbuf[i] != NULL
2081 && max_count < dp->df_count[i])
2082 max_count = dp->df_count[i];
2083
2084 if (dp->df_count[toidx] == dp->df_count[fromidx])
2085 {
2086 /* same number of lines: use same filler count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 towin->w_topfill = fromwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088 }
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002089 else if (dp->df_count[toidx] > dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002091 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002092 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002093 /* more lines in towin and fromwin doesn't show diff
2094 * lines, only filler lines */
2095 if (max_count - fromwin->w_topfill >= dp->df_count[toidx])
2096 {
2097 /* towin also only shows filler lines */
2098 towin->w_topline = dp->df_lnum[toidx]
2099 + dp->df_count[toidx];
2100 towin->w_topfill = fromwin->w_topfill;
2101 }
2102 else
2103 /* towin still has some diff lines to show */
2104 towin->w_topline = dp->df_lnum[toidx]
2105 + max_count - fromwin->w_topfill;
2106 }
2107 }
2108 else if (towin->w_topline >= dp->df_lnum[toidx]
2109 + dp->df_count[toidx])
2110 {
2111 /* less lines in towin and no diff lines to show: compute
2112 * filler lines */
2113 towin->w_topline = dp->df_lnum[toidx] + dp->df_count[toidx];
2114 if (diff_flags & DIFF_FILLER)
2115 {
2116 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
2117 /* fromwin is also out of diff lines */
2118 towin->w_topfill = fromwin->w_topfill;
2119 else
2120 /* fromwin has some diff lines */
2121 towin->w_topfill = dp->df_lnum[fromidx]
2122 + max_count - lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002123 }
2124 }
2125 }
2126 }
2127
2128 /* safety check (if diff info gets outdated strange things may happen) */
2129 towin->w_botfill = FALSE;
2130 if (towin->w_topline > towin->w_buffer->b_ml.ml_line_count)
2131 {
2132 towin->w_topline = towin->w_buffer->b_ml.ml_line_count;
2133 towin->w_botfill = TRUE;
2134 }
2135 if (towin->w_topline < 1)
2136 {
2137 towin->w_topline = 1;
2138 towin->w_topfill = 0;
2139 }
2140
2141 /* When w_topline changes need to recompute w_botline and cursor position */
2142 invalidate_botline_win(towin);
2143 changed_line_abv_curs_win(towin);
2144
2145 check_topfill(towin, FALSE);
2146#ifdef FEAT_FOLDING
2147 (void)hasFoldingWin(towin, towin->w_topline, &towin->w_topline,
2148 NULL, TRUE, NULL);
2149#endif
2150}
2151
2152/*
2153 * This is called when 'diffopt' is changed.
2154 */
2155 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002156diffopt_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157{
2158 char_u *p;
2159 int diff_context_new = 6;
2160 int diff_flags_new = 0;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002161 int diff_foldcolumn_new = 2;
Bram Moolenaare828b762018-09-10 17:51:58 +02002162 long diff_algorithm_new = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002163 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164
2165 p = p_dip;
2166 while (*p != NUL)
2167 {
2168 if (STRNCMP(p, "filler", 6) == 0)
2169 {
2170 p += 6;
2171 diff_flags_new |= DIFF_FILLER;
2172 }
2173 else if (STRNCMP(p, "context:", 8) == 0 && VIM_ISDIGIT(p[8]))
2174 {
2175 p += 8;
2176 diff_context_new = getdigits(&p);
2177 }
Bram Moolenaar785fc652018-09-15 19:17:38 +02002178 else if (STRNCMP(p, "iblank", 6) == 0)
2179 {
2180 p += 6;
2181 diff_flags_new |= DIFF_IBLANK;
2182 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 else if (STRNCMP(p, "icase", 5) == 0)
2184 {
2185 p += 5;
2186 diff_flags_new |= DIFF_ICASE;
2187 }
Bram Moolenaar785fc652018-09-15 19:17:38 +02002188 else if (STRNCMP(p, "iwhiteall", 9) == 0)
2189 {
2190 p += 9;
2191 diff_flags_new |= DIFF_IWHITEALL;
2192 }
2193 else if (STRNCMP(p, "iwhiteeol", 9) == 0)
2194 {
2195 p += 9;
2196 diff_flags_new |= DIFF_IWHITEEOL;
2197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 else if (STRNCMP(p, "iwhite", 6) == 0)
2199 {
2200 p += 6;
2201 diff_flags_new |= DIFF_IWHITE;
2202 }
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002203 else if (STRNCMP(p, "horizontal", 10) == 0)
2204 {
2205 p += 10;
2206 diff_flags_new |= DIFF_HORIZONTAL;
2207 }
2208 else if (STRNCMP(p, "vertical", 8) == 0)
2209 {
2210 p += 8;
2211 diff_flags_new |= DIFF_VERTICAL;
2212 }
2213 else if (STRNCMP(p, "foldcolumn:", 11) == 0 && VIM_ISDIGIT(p[11]))
2214 {
2215 p += 11;
2216 diff_foldcolumn_new = getdigits(&p);
2217 }
Bram Moolenaar97ce4192017-12-01 20:35:58 +01002218 else if (STRNCMP(p, "hiddenoff", 9) == 0)
2219 {
2220 p += 9;
2221 diff_flags_new |= DIFF_HIDDEN_OFF;
2222 }
Bram Moolenaare828b762018-09-10 17:51:58 +02002223 else if (STRNCMP(p, "indent-heuristic", 16) == 0)
2224 {
2225 p += 16;
2226 diff_algorithm_new |= XDF_INDENT_HEURISTIC;
2227 }
2228 else if (STRNCMP(p, "internal", 8) == 0)
2229 {
2230 p += 8;
2231 diff_flags_new |= DIFF_INTERNAL;
2232 }
2233 else if (STRNCMP(p, "algorithm:", 10) == 0)
2234 {
2235 p += 10;
2236 if (STRNCMP(p, "myers", 5) == 0)
2237 {
2238 p += 5;
2239 diff_algorithm_new = 0;
2240 }
2241 else if (STRNCMP(p, "minimal", 7) == 0)
2242 {
2243 p += 7;
2244 diff_algorithm_new = XDF_NEED_MINIMAL;
2245 }
2246 else if (STRNCMP(p, "patience", 8) == 0)
2247 {
2248 p += 8;
2249 diff_algorithm_new = XDF_PATIENCE_DIFF;
2250 }
2251 else if (STRNCMP(p, "histogram", 9) == 0)
2252 {
2253 p += 9;
2254 diff_algorithm_new = XDF_HISTOGRAM_DIFF;
2255 }
2256 }
2257
Bram Moolenaar071d4272004-06-13 20:20:40 +00002258 if (*p != ',' && *p != NUL)
2259 return FAIL;
2260 if (*p == ',')
2261 ++p;
2262 }
2263
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002264 /* Can't have both "horizontal" and "vertical". */
2265 if ((diff_flags_new & DIFF_HORIZONTAL) && (diff_flags_new & DIFF_VERTICAL))
2266 return FAIL;
2267
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 /* If "icase" or "iwhite" was added or removed, need to update the diff. */
Bram Moolenaare828b762018-09-10 17:51:58 +02002269 if (diff_flags != diff_flags_new || diff_algorithm != diff_algorithm_new)
Bram Moolenaar29323592016-07-24 22:04:11 +02002270 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002271 tp->tp_diff_invalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272
2273 diff_flags = diff_flags_new;
2274 diff_context = diff_context_new;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002275 diff_foldcolumn = diff_foldcolumn_new;
Bram Moolenaare828b762018-09-10 17:51:58 +02002276 diff_algorithm = diff_algorithm_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277
2278 diff_redraw(TRUE);
2279
2280 /* recompute the scroll binding with the new option value, may
2281 * remove or add filler lines */
2282 check_scrollbind((linenr_T)0, 0L);
2283
2284 return OK;
2285}
2286
2287/*
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002288 * Return TRUE if 'diffopt' contains "horizontal".
2289 */
2290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002291diffopt_horizontal(void)
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002292{
2293 return (diff_flags & DIFF_HORIZONTAL) != 0;
2294}
2295
2296/*
Bram Moolenaar97ce4192017-12-01 20:35:58 +01002297 * Return TRUE if 'diffopt' contains "hiddenoff".
2298 */
2299 int
2300diffopt_hiddenoff(void)
2301{
2302 return (diff_flags & DIFF_HIDDEN_OFF) != 0;
2303}
2304
2305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 * Find the difference within a changed line.
2307 * Returns TRUE if the line was added, no other buffer has it.
2308 */
2309 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002310diff_find_change(
2311 win_T *wp,
2312 linenr_T lnum,
2313 int *startp, /* first char of the change */
2314 int *endp) /* last char of the change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315{
2316 char_u *line_org;
2317 char_u *line_new;
2318 int i;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002319 int si_org, si_new;
2320 int ei_org, ei_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 diff_T *dp;
2322 int idx;
2323 int off;
2324 int added = TRUE;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002325 char_u *p1, *p2;
2326 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
2328 /* Make a copy of the line, the next ml_get() will invalidate it. */
2329 line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE));
2330 if (line_org == NULL)
2331 return FALSE;
2332
2333 idx = diff_buf_idx(wp->w_buffer);
2334 if (idx == DB_COUNT) /* cannot happen */
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002335 {
2336 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339
2340 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002341 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
2343 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002344 if (dp == NULL || diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002345 {
2346 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
2350 off = lnum - dp->df_lnum[idx];
2351
2352 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002353 if (curtab->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 {
2355 /* Skip lines that are not in the other change (filler lines). */
2356 if (off >= dp->df_count[i])
2357 continue;
2358 added = FALSE;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002359 line_new = ml_get_buf(curtab->tp_diffbuf[i],
2360 dp->df_lnum[i] + off, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
2362 /* Search for start of difference */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002363 si_org = si_new = 0;
2364 while (line_org[si_org] != NUL)
2365 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002366 if (((diff_flags & DIFF_IWHITE)
2367 && VIM_ISWHITE(line_org[si_org])
2368 && VIM_ISWHITE(line_new[si_new]))
2369 || ((diff_flags & DIFF_IWHITEALL)
2370 && (VIM_ISWHITE(line_org[si_org])
2371 || VIM_ISWHITE(line_new[si_new]))))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002372 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002373 si_org = (int)(skipwhite(line_org + si_org) - line_org);
2374 si_new = (int)(skipwhite(line_new + si_new) - line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002375 }
2376 else
2377 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002378 if (!diff_equal_char(line_org + si_org, line_new + si_new,
2379 &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002380 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002381 si_org += l;
2382 si_new += l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002383 }
2384 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385#ifdef FEAT_MBYTE
2386 if (has_mbyte)
2387 {
2388 /* Move back to first byte of character in both lines (may
2389 * have "nn^" in line_org and "n^ in line_new). */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002390 si_org -= (*mb_head_off)(line_org, line_org + si_org);
2391 si_new -= (*mb_head_off)(line_new, line_new + si_new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 }
2393#endif
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002394 if (*startp > si_org)
2395 *startp = si_org;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396
2397 /* Search for end of difference, if any. */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002398 if (line_org[si_org] != NUL || line_new[si_new] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 {
2400 ei_org = (int)STRLEN(line_org);
2401 ei_new = (int)STRLEN(line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002402 while (ei_org >= *startp && ei_new >= si_new
2403 && ei_org >= 0 && ei_new >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002405 if (((diff_flags & DIFF_IWHITE)
2406 && VIM_ISWHITE(line_org[ei_org])
2407 && VIM_ISWHITE(line_new[ei_new]))
2408 || ((diff_flags & DIFF_IWHITEALL)
2409 && (VIM_ISWHITE(line_org[ei_org])
2410 || VIM_ISWHITE(line_new[ei_new]))))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002411 {
2412 while (ei_org >= *startp
Bram Moolenaar1c465442017-03-12 20:10:05 +01002413 && VIM_ISWHITE(line_org[ei_org]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002414 --ei_org;
2415 while (ei_new >= si_new
Bram Moolenaar1c465442017-03-12 20:10:05 +01002416 && VIM_ISWHITE(line_new[ei_new]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002417 --ei_new;
2418 }
2419 else
2420 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002421 p1 = line_org + ei_org;
2422 p2 = line_new + ei_new;
2423#ifdef FEAT_MBYTE
2424 p1 -= (*mb_head_off)(line_org, p1);
2425 p2 -= (*mb_head_off)(line_new, p2);
2426#endif
2427 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002428 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002429 ei_org -= l;
2430 ei_new -= l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432 }
2433 if (*endp < ei_org)
2434 *endp = ei_org;
2435 }
2436 }
2437
2438 vim_free(line_org);
2439 return added;
2440}
2441
2442#if defined(FEAT_FOLDING) || defined(PROTO)
2443/*
2444 * Return TRUE if line "lnum" is not close to a diff block, this line should
2445 * be in a fold.
2446 * Return FALSE if there are no diff blocks at all in this window.
2447 */
2448 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002449diff_infold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450{
2451 int i;
2452 int idx = -1;
2453 int other = FALSE;
2454 diff_T *dp;
2455
2456 /* Return if 'diff' isn't set. */
2457 if (!wp->w_p_diff)
2458 return FALSE;
2459
2460 for (i = 0; i < DB_COUNT; ++i)
2461 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002462 if (curtab->tp_diffbuf[i] == wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 idx = i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002464 else if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 other = TRUE;
2466 }
2467
2468 /* return here if there are no diffs in the window */
2469 if (idx == -1 || !other)
2470 return FALSE;
2471
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002472 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 ex_diffupdate(NULL); /* update after a big change */
2474
2475 /* Return if there are no diff blocks. All lines will be folded. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002476 if (curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 return TRUE;
2478
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002479 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
2481 /* If this change is below the line there can't be any further match. */
2482 if (dp->df_lnum[idx] - diff_context > lnum)
2483 break;
2484 /* If this change ends before the line we have a match. */
2485 if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum)
2486 return FALSE;
2487 }
2488 return TRUE;
2489}
2490#endif
2491
2492/*
2493 * "dp" and "do" commands.
2494 */
2495 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002496nv_diffgetput(int put, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497{
2498 exarg_T ea;
Bram Moolenaar6a643652014-10-31 13:54:25 +01002499 char_u buf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500
Bram Moolenaarf2732452018-06-03 14:47:35 +02002501#ifdef FEAT_JOB_CHANNEL
2502 if (bt_prompt(curbuf))
2503 {
2504 vim_beep(BO_OPER);
2505 return;
2506 }
2507#endif
Bram Moolenaar6a643652014-10-31 13:54:25 +01002508 if (count == 0)
2509 ea.arg = (char_u *)"";
2510 else
2511 {
2512 vim_snprintf((char *)buf, 30, "%ld", count);
2513 ea.arg = buf;
2514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 if (put)
2516 ea.cmdidx = CMD_diffput;
2517 else
2518 ea.cmdidx = CMD_diffget;
2519 ea.addr_count = 0;
2520 ea.line1 = curwin->w_cursor.lnum;
2521 ea.line2 = curwin->w_cursor.lnum;
2522 ex_diffgetput(&ea);
2523}
2524
2525/*
2526 * ":diffget"
2527 * ":diffput"
2528 */
2529 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002530ex_diffgetput(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531{
2532 linenr_T lnum;
2533 int count;
2534 linenr_T off = 0;
2535 diff_T *dp;
2536 diff_T *dprev;
2537 diff_T *dfree;
2538 int idx_cur;
2539 int idx_other;
2540 int idx_from;
2541 int idx_to;
2542 int i;
2543 int added;
2544 char_u *p;
2545 aco_save_T aco;
2546 buf_T *buf;
2547 int start_skip, end_skip;
2548 int new_count;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002549 int buf_empty;
Bram Moolenaar602eb742007-02-20 03:43:38 +00002550 int found_not_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
2552 /* Find the current buffer in the list of diff buffers. */
2553 idx_cur = diff_buf_idx(curbuf);
2554 if (idx_cur == DB_COUNT)
2555 {
2556 EMSG(_("E99: Current buffer is not in diff mode"));
2557 return;
2558 }
2559
2560 if (*eap->arg == NUL)
2561 {
2562 /* No argument: Find the other buffer in the list of diff buffers. */
2563 for (idx_other = 0; idx_other < DB_COUNT; ++idx_other)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002564 if (curtab->tp_diffbuf[idx_other] != curbuf
Bram Moolenaar602eb742007-02-20 03:43:38 +00002565 && curtab->tp_diffbuf[idx_other] != NULL)
2566 {
2567 if (eap->cmdidx != CMD_diffput
2568 || curtab->tp_diffbuf[idx_other]->b_p_ma)
2569 break;
2570 found_not_ma = TRUE;
2571 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 if (idx_other == DB_COUNT)
2573 {
Bram Moolenaar602eb742007-02-20 03:43:38 +00002574 if (found_not_ma)
2575 EMSG(_("E793: No other buffer in diff mode is modifiable"));
2576 else
2577 EMSG(_("E100: No other buffer in diff mode"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 return;
2579 }
2580
2581 /* Check that there isn't a third buffer in the list */
2582 for (i = idx_other + 1; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002583 if (curtab->tp_diffbuf[i] != curbuf
2584 && curtab->tp_diffbuf[i] != NULL
2585 && (eap->cmdidx != CMD_diffput || curtab->tp_diffbuf[i]->b_p_ma))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 {
2587 EMSG(_("E101: More than two buffers in diff mode, don't know which one to use"));
2588 return;
2589 }
2590 }
2591 else
2592 {
2593 /* Buffer number or pattern given. Ignore trailing white space. */
2594 p = eap->arg + STRLEN(eap->arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002595 while (p > eap->arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 --p;
2597 for (i = 0; vim_isdigit(eap->arg[i]) && eap->arg + i < p; ++i)
2598 ;
2599 if (eap->arg + i == p) /* digits only */
2600 i = atol((char *)eap->arg);
2601 else
2602 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002603 i = buflist_findpat(eap->arg, p, FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 if (i < 0)
2605 return; /* error message already given */
2606 }
2607 buf = buflist_findnr(i);
2608 if (buf == NULL)
2609 {
2610 EMSG2(_("E102: Can't find buffer \"%s\""), eap->arg);
2611 return;
2612 }
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +00002613 if (buf == curbuf)
2614 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 idx_other = diff_buf_idx(buf);
2616 if (idx_other == DB_COUNT)
2617 {
2618 EMSG2(_("E103: Buffer \"%s\" is not in diff mode"), eap->arg);
2619 return;
2620 }
2621 }
2622
2623 diff_busy = TRUE;
2624
2625 /* When no range given include the line above or below the cursor. */
2626 if (eap->addr_count == 0)
2627 {
2628 /* Make it possible that ":diffget" on the last line gets line below
2629 * the cursor line when there is no difference above the cursor. */
2630 if (eap->cmdidx == CMD_diffget
2631 && eap->line1 == curbuf->b_ml.ml_line_count
2632 && diff_check(curwin, eap->line1) == 0
2633 && (eap->line1 == 1 || diff_check(curwin, eap->line1 - 1) == 0))
2634 ++eap->line2;
2635 else if (eap->line1 > 0)
2636 --eap->line1;
2637 }
2638
2639 if (eap->cmdidx == CMD_diffget)
2640 {
2641 idx_from = idx_other;
2642 idx_to = idx_cur;
2643 }
2644 else
2645 {
2646 idx_from = idx_cur;
2647 idx_to = idx_other;
2648 /* Need to make the other buffer the current buffer to be able to make
2649 * changes in it. */
2650 /* set curwin/curbuf to buf and save a few things */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002651 aucmd_prepbuf(&aco, curtab->tp_diffbuf[idx_other]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002652 }
2653
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002654 /* May give the warning for a changed buffer here, which can trigger the
2655 * FileChangedRO autocommand, which may do nasty things and mess
2656 * everything up. */
2657 if (!curbuf->b_changed)
2658 {
2659 change_warning(0);
2660 if (diff_buf_idx(curbuf) != idx_to)
2661 {
2662 EMSG(_("E787: Buffer changed unexpectedly"));
2663 return;
2664 }
2665 }
2666
Bram Moolenaar071d4272004-06-13 20:20:40 +00002667 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002668 for (dp = curtab->tp_first_diff; dp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669 {
2670 if (dp->df_lnum[idx_cur] > eap->line2 + off)
2671 break; /* past the range that was specified */
2672
2673 dfree = NULL;
2674 lnum = dp->df_lnum[idx_to];
2675 count = dp->df_count[idx_to];
2676 if (dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off
2677 && u_save(lnum - 1, lnum + count) != FAIL)
2678 {
2679 /* Inside the specified range and saving for undo worked. */
2680 start_skip = 0;
2681 end_skip = 0;
2682 if (eap->addr_count > 0)
2683 {
2684 /* A range was specified: check if lines need to be skipped. */
2685 start_skip = eap->line1 + off - dp->df_lnum[idx_cur];
2686 if (start_skip > 0)
2687 {
2688 /* range starts below start of current diff block */
2689 if (start_skip > count)
2690 {
2691 lnum += count;
2692 count = 0;
2693 }
2694 else
2695 {
2696 count -= start_skip;
2697 lnum += start_skip;
2698 }
2699 }
2700 else
2701 start_skip = 0;
2702
2703 end_skip = dp->df_lnum[idx_cur] + dp->df_count[idx_cur] - 1
2704 - (eap->line2 + off);
2705 if (end_skip > 0)
2706 {
2707 /* range ends above end of current/from diff block */
2708 if (idx_cur == idx_from) /* :diffput */
2709 {
2710 i = dp->df_count[idx_cur] - start_skip - end_skip;
2711 if (count > i)
2712 count = i;
2713 }
2714 else /* :diffget */
2715 {
2716 count -= end_skip;
2717 end_skip = dp->df_count[idx_from] - start_skip - count;
2718 if (end_skip < 0)
2719 end_skip = 0;
2720 }
2721 }
2722 else
2723 end_skip = 0;
2724 }
2725
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002726 buf_empty = BUFEMPTY();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 added = 0;
2728 for (i = 0; i < count; ++i)
2729 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00002730 /* remember deleting the last line of the buffer */
2731 buf_empty = curbuf->b_ml.ml_line_count == 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 ml_delete(lnum, FALSE);
2733 --added;
2734 }
2735 for (i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; ++i)
2736 {
2737 linenr_T nr;
2738
2739 nr = dp->df_lnum[idx_from] + start_skip + i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002740 if (nr > curtab->tp_diffbuf[idx_from]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002742 p = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx_from],
2743 nr, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002744 if (p != NULL)
2745 {
2746 ml_append(lnum + i - 1, p, 0, FALSE);
2747 vim_free(p);
2748 ++added;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002749 if (buf_empty && curbuf->b_ml.ml_line_count == 2)
2750 {
2751 /* Added the first line into an empty buffer, need to
2752 * delete the dummy empty line. */
2753 buf_empty = FALSE;
2754 ml_delete((linenr_T)2, FALSE);
2755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002756 }
2757 }
2758 new_count = dp->df_count[idx_to] + added;
2759 dp->df_count[idx_to] = new_count;
2760
2761 if (start_skip == 0 && end_skip == 0)
2762 {
2763 /* Check if there are any other buffers and if the diff is
2764 * equal in them. */
2765 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002766 if (curtab->tp_diffbuf[i] != NULL && i != idx_from
2767 && i != idx_to
Bram Moolenaar071d4272004-06-13 20:20:40 +00002768 && !diff_equal_entry(dp, idx_from, i))
2769 break;
2770 if (i == DB_COUNT)
2771 {
2772 /* delete the diff entry, the buffers are now equal here */
2773 dfree = dp;
2774 dp = dp->df_next;
2775 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002776 curtab->tp_first_diff = dp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002777 else
2778 dprev->df_next = dp;
2779 }
2780 }
2781
2782 /* Adjust marks. This will change the following entries! */
2783 if (added != 0)
2784 {
2785 mark_adjust(lnum, lnum + count - 1, (long)MAXLNUM, (long)added);
2786 if (curwin->w_cursor.lnum >= lnum)
2787 {
2788 /* Adjust the cursor position if it's in/after the changed
2789 * lines. */
2790 if (curwin->w_cursor.lnum >= lnum + count)
2791 curwin->w_cursor.lnum += added;
2792 else if (added < 0)
2793 curwin->w_cursor.lnum = lnum;
2794 }
2795 }
2796 changed_lines(lnum, 0, lnum + count, (long)added);
2797
2798 if (dfree != NULL)
2799 {
2800 /* Diff is deleted, update folds in other windows. */
2801#ifdef FEAT_FOLDING
2802 diff_fold_update(dfree, idx_to);
2803#endif
2804 vim_free(dfree);
2805 }
2806 else
2807 /* mark_adjust() may have changed the count in a wrong way */
2808 dp->df_count[idx_to] = new_count;
2809
2810 /* When changing the current buffer, keep track of line numbers */
2811 if (idx_cur == idx_to)
2812 off += added;
2813 }
2814
2815 /* If before the range or not deleted, go to next diff. */
2816 if (dfree == NULL)
2817 {
2818 dprev = dp;
2819 dp = dp->df_next;
2820 }
2821 }
2822
2823 /* restore curwin/curbuf and a few other things */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02002824 if (eap->cmdidx != CMD_diffget)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002825 {
2826 /* Syncing undo only works for the current buffer, but we change
2827 * another buffer. Sync undo if the command was typed. This isn't
2828 * 100% right when ":diffput" is used in a function or mapping. */
2829 if (KeyTyped)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002830 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002831 aucmd_restbuf(&aco);
2832 }
2833
2834 diff_busy = FALSE;
2835
2836 /* Check that the cursor is on a valid character and update it's position.
2837 * When there were filler lines the topline has become invalid. */
2838 check_cursor();
2839 changed_line_abv_curs();
2840
2841 /* Also need to redraw the other buffers. */
2842 diff_redraw(FALSE);
2843}
2844
2845#ifdef FEAT_FOLDING
2846/*
2847 * Update folds for all diff buffers for entry "dp".
2848 * Skip buffer with index "skip_idx".
2849 * When there are no diffs, all folds are removed.
2850 */
2851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002852diff_fold_update(diff_T *dp, int skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853{
2854 int i;
2855 win_T *wp;
2856
Bram Moolenaar29323592016-07-24 22:04:11 +02002857 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002859 if (curtab->tp_diffbuf[i] == wp->w_buffer && i != skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 foldUpdate(wp, dp->df_lnum[i],
2861 dp->df_lnum[i] + dp->df_count[i]);
2862}
2863#endif
2864
2865/*
2866 * Return TRUE if buffer "buf" is in diff-mode.
2867 */
2868 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002869diff_mode_buf(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002871 tabpage_T *tp;
2872
Bram Moolenaar29323592016-07-24 22:04:11 +02002873 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002874 if (diff_buf_idx_tp(buf, tp) != DB_COUNT)
2875 return TRUE;
2876 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877}
2878
2879/*
2880 * Move "count" times in direction "dir" to the next diff block.
2881 * Return FAIL if there isn't such a diff block.
2882 */
2883 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002884diff_move_to(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002885{
2886 int idx;
2887 linenr_T lnum = curwin->w_cursor.lnum;
2888 diff_T *dp;
2889
2890 idx = diff_buf_idx(curbuf);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002891 if (idx == DB_COUNT || curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 return FAIL;
2893
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002894 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895 ex_diffupdate(NULL); /* update after a big change */
2896
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002897 if (curtab->tp_first_diff == NULL) /* no diffs today */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898 return FAIL;
2899
2900 while (--count >= 0)
2901 {
2902 /* Check if already before first diff. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002903 if (dir == BACKWARD && lnum <= curtab->tp_first_diff->df_lnum[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002904 break;
2905
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002906 for (dp = curtab->tp_first_diff; ; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002907 {
2908 if (dp == NULL)
2909 break;
2910 if ((dir == FORWARD && lnum < dp->df_lnum[idx])
2911 || (dir == BACKWARD
2912 && (dp->df_next == NULL
2913 || lnum <= dp->df_next->df_lnum[idx])))
2914 {
2915 lnum = dp->df_lnum[idx];
2916 break;
2917 }
2918 }
2919 }
2920
2921 /* don't end up past the end of the file */
2922 if (lnum > curbuf->b_ml.ml_line_count)
2923 lnum = curbuf->b_ml.ml_line_count;
2924
2925 /* When the cursor didn't move at all we fail. */
2926 if (lnum == curwin->w_cursor.lnum)
2927 return FAIL;
2928
2929 setpcmark();
2930 curwin->w_cursor.lnum = lnum;
2931 curwin->w_cursor.col = 0;
2932
2933 return OK;
2934}
2935
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002936/*
2937 * Return the line number in the current window that is closest to "lnum1" in
2938 * "buf1" in diff mode.
2939 */
2940 static linenr_T
2941diff_get_corresponding_line_int(
Bram Moolenaar7454a062016-01-30 15:14:10 +01002942 buf_T *buf1,
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002943 linenr_T lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002944{
2945 int idx1;
2946 int idx2;
2947 diff_T *dp;
2948 int baseline = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002949
2950 idx1 = diff_buf_idx(buf1);
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002951 idx2 = diff_buf_idx(curbuf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002952 if (idx1 == DB_COUNT || idx2 == DB_COUNT || curtab->tp_first_diff == NULL)
2953 return lnum1;
2954
2955 if (curtab->tp_diff_invalid)
2956 ex_diffupdate(NULL); /* update after a big change */
2957
2958 if (curtab->tp_first_diff == NULL) /* no diffs today */
2959 return lnum1;
2960
2961 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
2962 {
2963 if (dp->df_lnum[idx1] > lnum1)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002964 return lnum1 - baseline;
2965 if ((dp->df_lnum[idx1] + dp->df_count[idx1]) > lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002966 {
2967 /* Inside the diffblock */
2968 baseline = lnum1 - dp->df_lnum[idx1];
2969 if (baseline > dp->df_count[idx2])
2970 baseline = dp->df_count[idx2];
2971
2972 return dp->df_lnum[idx2] + baseline;
2973 }
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002974 if ( (dp->df_lnum[idx1] == lnum1)
2975 && (dp->df_count[idx1] == 0)
2976 && (dp->df_lnum[idx2] <= curwin->w_cursor.lnum)
2977 && ((dp->df_lnum[idx2] + dp->df_count[idx2])
2978 > curwin->w_cursor.lnum))
Bram Moolenaar860cae12010-06-05 23:22:07 +02002979 /*
2980 * Special case: if the cursor is just after a zero-count
2981 * block (i.e. all filler) and the target cursor is already
2982 * inside the corresponding block, leave the target cursor
2983 * unmoved. This makes repeated CTRL-W W operations work
2984 * as expected.
2985 */
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002986 return curwin->w_cursor.lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002987 baseline = (dp->df_lnum[idx1] + dp->df_count[idx1])
2988 - (dp->df_lnum[idx2] + dp->df_count[idx2]);
2989 }
2990
2991 /* If we get here then the cursor is after the last diff */
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002992 return lnum1 - baseline;
2993}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002994
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002995/*
2996 * Return the line number in the current window that is closest to "lnum1" in
2997 * "buf1" in diff mode. Checks the line number to be valid.
2998 */
2999 linenr_T
3000diff_get_corresponding_line(buf_T *buf1, linenr_T lnum1)
3001{
3002 linenr_T lnum = diff_get_corresponding_line_int(buf1, lnum1);
3003
3004 /* don't end up past the end of the file */
3005 if (lnum > curbuf->b_ml.ml_line_count)
3006 return curbuf->b_ml.ml_line_count;
3007 return lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003008}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003009
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010/*
3011 * For line "lnum" in the current window find the equivalent lnum in window
3012 * "wp", compensating for inserted/deleted lines.
3013 */
3014 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003015diff_lnum_win(linenr_T lnum, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016{
3017 diff_T *dp;
3018 int idx;
3019 int i;
3020 linenr_T n;
3021
3022 idx = diff_buf_idx(curbuf);
3023 if (idx == DB_COUNT) /* safety check */
3024 return (linenr_T)0;
3025
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003026 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 ex_diffupdate(NULL); /* update after a big change */
3028
3029 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003030 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003031 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
3032 break;
3033
3034 /* When after the last change, compute relative to the last line number. */
3035 if (dp == NULL)
3036 return wp->w_buffer->b_ml.ml_line_count
3037 - (curbuf->b_ml.ml_line_count - lnum);
3038
3039 /* Find index for "wp". */
3040 i = diff_buf_idx(wp->w_buffer);
3041 if (i == DB_COUNT) /* safety check */
3042 return (linenr_T)0;
3043
3044 n = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]);
3045 if (n > dp->df_lnum[i] + dp->df_count[i])
3046 n = dp->df_lnum[i] + dp->df_count[i];
3047 return n;
3048}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049
Bram Moolenaare828b762018-09-10 17:51:58 +02003050/*
3051 * Handle an ED style diff line.
3052 * Return FAIL if the line does not contain diff info.
3053 */
3054 static int
3055parse_diff_ed(
3056 char_u *line,
3057 linenr_T *lnum_orig,
3058 long *count_orig,
3059 linenr_T *lnum_new,
3060 long *count_new)
3061{
3062 char_u *p;
3063 long f1, l1, f2, l2;
3064 int difftype;
3065
3066 // The line must be one of three formats:
3067 // change: {first}[,{last}]c{first}[,{last}]
3068 // append: {first}a{first}[,{last}]
3069 // delete: {first}[,{last}]d{first}
3070 p = line;
3071 f1 = getdigits(&p);
3072 if (*p == ',')
3073 {
3074 ++p;
3075 l1 = getdigits(&p);
3076 }
3077 else
3078 l1 = f1;
3079 if (*p != 'a' && *p != 'c' && *p != 'd')
3080 return FAIL; // invalid diff format
3081 difftype = *p++;
3082 f2 = getdigits(&p);
3083 if (*p == ',')
3084 {
3085 ++p;
3086 l2 = getdigits(&p);
3087 }
3088 else
3089 l2 = f2;
3090 if (l1 < f1 || l2 < f2)
3091 return FAIL;
3092
3093 if (difftype == 'a')
3094 {
3095 *lnum_orig = f1 + 1;
3096 *count_orig = 0;
3097 }
3098 else
3099 {
3100 *lnum_orig = f1;
3101 *count_orig = l1 - f1 + 1;
3102 }
3103 if (difftype == 'd')
3104 {
3105 *lnum_new = f2 + 1;
3106 *count_new = 0;
3107 }
3108 else
3109 {
3110 *lnum_new = f2;
3111 *count_new = l2 - f2 + 1;
3112 }
3113 return OK;
3114}
3115
3116/*
3117 * Parses unified diff with zero(!) context lines.
3118 * Return FAIL if there is no diff information in "line".
3119 */
3120 static int
3121parse_diff_unified(
3122 char_u *line,
3123 linenr_T *lnum_orig,
3124 long *count_orig,
3125 linenr_T *lnum_new,
3126 long *count_new)
3127{
3128 char_u *p;
3129 long oldline, oldcount, newline, newcount;
3130
3131 // Parse unified diff hunk header:
3132 // @@ -oldline,oldcount +newline,newcount @@
3133 p = line;
3134 if (*p++ == '@' && *p++ == '@' && *p++ == ' ' && *p++ == '-')
3135 {
3136 oldline = getdigits(&p);
3137 if (*p == ',')
3138 {
3139 ++p;
3140 oldcount = getdigits(&p);
3141 }
3142 else
3143 oldcount = 1;
3144 if (*p++ == ' ' && *p++ == '+')
3145 {
3146 newline = getdigits(&p);
3147 if (*p == ',')
3148 {
3149 ++p;
3150 newcount = getdigits(&p);
3151 }
3152 else
3153 newcount = 1;
3154 }
3155 else
3156 return FAIL; // invalid diff format
3157
3158 if (oldcount == 0)
3159 oldline += 1;
3160 if (newcount == 0)
3161 newline += 1;
3162 if (newline == 0)
3163 newline = 1;
3164
3165 *lnum_orig = oldline;
3166 *count_orig = oldcount;
3167 *lnum_new = newline;
3168 *count_new = newcount;
3169
3170 return OK;
3171 }
3172
3173 return FAIL;
3174}
3175
3176/*
3177 * Callback function for the xdl_diff() function.
3178 * Stores the diff output in a grow array.
3179 */
3180 static int
3181xdiff_out(void *priv, mmbuffer_t *mb, int nbuf)
3182{
3183 diffout_T *dout = (diffout_T *)priv;
3184 int i;
3185 char_u *p;
3186
3187 for (i = 0; i < nbuf; i++)
3188 {
3189 // We are only interested in the header lines, skip text lines.
3190 if (STRNCMP(mb[i].ptr, "@@ ", 3) != 0)
3191 continue;
3192 if (ga_grow(&dout->dout_ga, 1) == FAIL)
3193 return -1;
3194 p = vim_strnsave((char_u *)mb[i].ptr, mb[i].size);
3195 if (p == NULL)
3196 return -1;
3197 ((char_u **)dout->dout_ga.ga_data)[dout->dout_ga.ga_len++] = p;
3198 }
3199 return 0;
3200}
3201
Bram Moolenaar071d4272004-06-13 20:20:40 +00003202#endif /* FEAT_DIFF */