blob: d523e7743a9e584eb4470465752c139b1fe3fb4a [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +000011 * diff.c: code for diff'ing two, three or four buffers.
Bram Moolenaare828b762018-09-10 17:51:58 +020012 *
13 * There are three ways to diff:
14 * - Shell out to an external diff program, using files.
15 * - Use the compiled-in xdiff library.
16 * - Let 'diffexpr' do the work, using files.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017 */
18
19#include "vim.h"
Bram Moolenaare828b762018-09-10 17:51:58 +020020#include "xdiff/xdiff.h"
Bram Moolenaar071d4272004-06-13 20:20:40 +000021
22#if defined(FEAT_DIFF) || defined(PROTO)
23
Bram Moolenaard2b58c02018-09-16 18:10:48 +020024static int diff_busy = FALSE; // using diff structs, don't change them
25static int diff_need_update = FALSE; // ex_diffupdate needs to be called
Bram Moolenaar071d4272004-06-13 20:20:40 +000026
27/* flags obtained from the 'diffopt' option */
Bram Moolenaar785fc652018-09-15 19:17:38 +020028#define DIFF_FILLER 0x001 // display filler lines
29#define DIFF_IBLANK 0x002 // ignore empty lines
30#define DIFF_ICASE 0x004 // ignore case
31#define DIFF_IWHITE 0x008 // ignore change in white space
32#define DIFF_IWHITEALL 0x010 // ignore all white space changes
33#define DIFF_IWHITEEOL 0x020 // ignore change in white space at EOL
34#define DIFF_HORIZONTAL 0x040 // horizontal splits
35#define DIFF_VERTICAL 0x080 // vertical splits
36#define DIFF_HIDDEN_OFF 0x100 // diffoff when hidden
37#define DIFF_INTERNAL 0x200 // use internal xdiff algorithm
38#define ALL_WHITE_DIFF (DIFF_IWHITE | DIFF_IWHITEALL | DIFF_IWHITEEOL)
Bram Moolenaar274cea32018-09-12 18:00:12 +020039static int diff_flags = DIFF_INTERNAL | DIFF_FILLER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000040
Bram Moolenaare828b762018-09-10 17:51:58 +020041static long diff_algorithm = 0;
42
Bram Moolenaar071d4272004-06-13 20:20:40 +000043#define LBUFLEN 50 /* length of line in diff file */
44
45static int diff_a_works = MAYBE; /* TRUE when "diff -a" works, FALSE when it
46 doesn't work, MAYBE when not checked yet */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010047#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000048static int diff_bin_works = MAYBE; /* TRUE when "diff --binary" works, FALSE
49 when it doesn't work, MAYBE when not
50 checked yet */
51#endif
52
Bram Moolenaare828b762018-09-10 17:51:58 +020053// used for diff input
54typedef struct {
55 char_u *din_fname; // used for external diff
56 mmfile_t din_mmfile; // used for internal diff
57} diffin_T;
58
59// used for diff result
60typedef struct {
61 char_u *dout_fname; // used for external diff
62 garray_T dout_ga; // used for internal diff
63} diffout_T;
64
65// two diff inputs and one result
66typedef struct {
67 diffin_T dio_orig; // original file input
68 diffin_T dio_new; // new file input
69 diffout_T dio_diff; // diff result
70 int dio_internal; // using internal diff
71} diffio_T;
72
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010073static int diff_buf_idx(buf_T *buf);
74static int diff_buf_idx_tp(buf_T *buf, tabpage_T *tp);
75static void diff_mark_adjust_tp(tabpage_T *tp, int idx, linenr_T line1, linenr_T line2, long amount, long amount_after);
76static void diff_check_unchanged(tabpage_T *tp, diff_T *dp);
77static int diff_check_sanity(tabpage_T *tp, diff_T *dp);
78static void diff_redraw(int dofold);
Bram Moolenaare828b762018-09-10 17:51:58 +020079static int check_external_diff(diffio_T *diffio);
80static int diff_file(diffio_T *diffio);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010081static int diff_equal_entry(diff_T *dp, int idx1, int idx2);
82static int diff_cmp(char_u *s1, char_u *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000083#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010084static void diff_fold_update(diff_T *dp, int skip_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000085#endif
Bram Moolenaare828b762018-09-10 17:51:58 +020086static void diff_read(int idx_orig, int idx_new, diffout_T *fname);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010087static void diff_copy_entry(diff_T *dprev, diff_T *dp, int idx_orig, int idx_new);
88static diff_T *diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp);
Bram Moolenaare828b762018-09-10 17:51:58 +020089static int parse_diff_ed(char_u *line, linenr_T *lnum_orig, long *count_orig, linenr_T *lnum_new, long *count_new);
90static int parse_diff_unified(char_u *line, linenr_T *lnum_orig, long *count_orig, linenr_T *lnum_new, long *count_new);
91static int xdiff_out(void *priv, mmbuffer_t *mb, int nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
93#ifndef USE_CR
94# define tag_fgets vim_fgets
95#endif
96
97/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000098 * Called when deleting or unloading a buffer: No longer make a diff with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000099 */
100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100101diff_buf_delete(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000102{
103 int i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000104 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000105
Bram Moolenaar29323592016-07-24 22:04:11 +0200106 FOR_ALL_TABPAGES(tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000107 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000108 i = diff_buf_idx_tp(buf, tp);
109 if (i != DB_COUNT)
110 {
111 tp->tp_diffbuf[i] = NULL;
112 tp->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000113 if (tp == curtab)
114 diff_redraw(TRUE);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000116 }
117}
118
119/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000120 * Check if the current buffer should be added to or removed from the list of
121 * diff buffers.
122 */
123 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100124diff_buf_adjust(win_T *win)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000125{
126 win_T *wp;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000127 int i;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000128
129 if (!win->w_p_diff)
130 {
131 /* When there is no window showing a diff for this buffer, remove
132 * it from the diffs. */
Bram Moolenaar29323592016-07-24 22:04:11 +0200133 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000134 if (wp->w_buffer == win->w_buffer && wp->w_p_diff)
135 break;
136 if (wp == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000137 {
138 i = diff_buf_idx(win->w_buffer);
139 if (i != DB_COUNT)
140 {
141 curtab->tp_diffbuf[i] = NULL;
142 curtab->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000143 diff_redraw(TRUE);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000144 }
145 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000146 }
147 else
148 diff_buf_add(win->w_buffer);
149}
150
151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 * Add a buffer to make diffs for.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000153 * Call this when a new buffer is being edited in the current window where
154 * 'diff' is set.
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +0000155 * Marks the current buffer as being part of the diff and requiring updating.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000156 * This must be done before any autocmd, because a command may use info
157 * about the screen contents.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158 */
159 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100160diff_buf_add(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161{
162 int i;
163
164 if (diff_buf_idx(buf) != DB_COUNT)
165 return; /* It's already there. */
166
167 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000168 if (curtab->tp_diffbuf[i] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000169 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000170 curtab->tp_diffbuf[i] = buf;
171 curtab->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000172 diff_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173 return;
174 }
175
Bram Moolenaar89eaa412016-07-31 14:17:27 +0200176 EMSGN(_("E96: Cannot diff more than %ld buffers"), DB_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177}
178
179/*
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100180 * Remove all buffers to make diffs for.
181 */
182 static void
183diff_buf_clear(void)
184{
185 int i;
186
187 for (i = 0; i < DB_COUNT; ++i)
188 if (curtab->tp_diffbuf[i] != NULL)
189 {
190 curtab->tp_diffbuf[i] = NULL;
191 curtab->tp_diff_invalid = TRUE;
192 diff_redraw(TRUE);
193 }
194}
195
196/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000197 * Find buffer "buf" in the list of diff buffers for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 * Return its index or DB_COUNT if not found.
199 */
200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100201diff_buf_idx(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202{
203 int idx;
204
205 for (idx = 0; idx < DB_COUNT; ++idx)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000206 if (curtab->tp_diffbuf[idx] == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207 break;
208 return idx;
209}
210
211/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000212 * Find buffer "buf" in the list of diff buffers for tab page "tp".
213 * Return its index or DB_COUNT if not found.
214 */
215 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100216diff_buf_idx_tp(buf_T *buf, tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000217{
218 int idx;
219
220 for (idx = 0; idx < DB_COUNT; ++idx)
221 if (tp->tp_diffbuf[idx] == buf)
222 break;
223 return idx;
224}
225
226/*
227 * Mark the diff info involving buffer "buf" as invalid, it will be updated
228 * when info is requested.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229 */
230 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100231diff_invalidate(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000233 tabpage_T *tp;
234 int i;
235
Bram Moolenaar29323592016-07-24 22:04:11 +0200236 FOR_ALL_TABPAGES(tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000238 i = diff_buf_idx_tp(buf, tp);
239 if (i != DB_COUNT)
240 {
241 tp->tp_diff_invalid = TRUE;
242 if (tp == curtab)
243 diff_redraw(TRUE);
244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000245 }
246}
247
248/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000249 * Called by mark_adjust(): update line numbers in "curbuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000250 */
251 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100252diff_mark_adjust(
253 linenr_T line1,
254 linenr_T line2,
255 long amount,
256 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000258 int idx;
259 tabpage_T *tp;
260
261 /* Handle all tab pages that use the current buffer in a diff. */
Bram Moolenaar29323592016-07-24 22:04:11 +0200262 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000263 {
264 idx = diff_buf_idx_tp(curbuf, tp);
265 if (idx != DB_COUNT)
266 diff_mark_adjust_tp(tp, idx, line1, line2, amount, amount_after);
267 }
268}
269
270/*
271 * Update line numbers in tab page "tp" for "curbuf" with index "idx".
272 * This attempts to update the changes as much as possible:
273 * When inserting/deleting lines outside of existing change blocks, create a
274 * new change block and update the line numbers in following blocks.
275 * When inserting/deleting lines in existing change blocks, update them.
276 */
277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100278diff_mark_adjust_tp(
279 tabpage_T *tp,
280 int idx,
281 linenr_T line1,
282 linenr_T line2,
283 long amount,
284 long amount_after)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000285{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286 diff_T *dp;
287 diff_T *dprev;
288 diff_T *dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000289 int i;
290 int inserted, deleted;
291 int n, off;
292 linenr_T last;
293 linenr_T lnum_deleted = line1; /* lnum of remaining deletion */
294 int check_unchanged;
295
Bram Moolenaare3521d92018-09-16 14:10:31 +0200296 if (diff_internal())
297 {
298 // Will udpate diffs before redrawing. Set _invalid to update the
299 // diffs themselves, set _update to also update folds properly just
300 // before redrawing.
301 tp->tp_diff_invalid = TRUE;
302 tp->tp_diff_update = TRUE;
303 return;
304 }
305
Bram Moolenaar071d4272004-06-13 20:20:40 +0000306 if (line2 == MAXLNUM)
307 {
308 /* mark_adjust(99, MAXLNUM, 9, 0): insert lines */
309 inserted = amount;
310 deleted = 0;
311 }
312 else if (amount_after > 0)
313 {
314 /* mark_adjust(99, 98, MAXLNUM, 9): a change that inserts lines*/
315 inserted = amount_after;
316 deleted = 0;
317 }
318 else
319 {
320 /* mark_adjust(98, 99, MAXLNUM, -2): delete lines */
321 inserted = 0;
322 deleted = -amount_after;
323 }
324
325 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000326 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 for (;;)
328 {
329 /* If the change is after the previous diff block and before the next
330 * diff block, thus not touching an existing change, create a new diff
331 * block. Don't do this when ex_diffgetput() is busy. */
332 if ((dp == NULL || dp->df_lnum[idx] - 1 > line2
333 || (line2 == MAXLNUM && dp->df_lnum[idx] > line1))
334 && (dprev == NULL
335 || dprev->df_lnum[idx] + dprev->df_count[idx] < line1)
336 && !diff_busy)
337 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000338 dnext = diff_alloc_new(tp, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 if (dnext == NULL)
340 return;
341
342 dnext->df_lnum[idx] = line1;
343 dnext->df_count[idx] = inserted;
344 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000345 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 {
347 if (dprev == NULL)
348 dnext->df_lnum[i] = line1;
349 else
350 dnext->df_lnum[i] = line1
351 + (dprev->df_lnum[i] + dprev->df_count[i])
352 - (dprev->df_lnum[idx] + dprev->df_count[idx]);
353 dnext->df_count[i] = deleted;
354 }
355 }
356
357 /* if at end of the list, quit */
358 if (dp == NULL)
359 break;
360
361 /*
362 * Check for these situations:
363 * 1 2 3
364 * 1 2 3
365 * line1 2 3 4 5
366 * 2 3 4 5
367 * 2 3 4 5
368 * line2 2 3 4 5
369 * 3 5 6
370 * 3 5 6
371 */
372 /* compute last line of this change */
373 last = dp->df_lnum[idx] + dp->df_count[idx] - 1;
374
375 /* 1. change completely above line1: nothing to do */
376 if (last >= line1 - 1)
377 {
378 /* 6. change below line2: only adjust for amount_after; also when
379 * "deleted" became zero when deleted all lines between two diffs */
380 if (dp->df_lnum[idx] - (deleted + inserted != 0) > line2)
381 {
382 if (amount_after == 0)
383 break; /* nothing left to change */
384 dp->df_lnum[idx] += amount_after;
385 }
386 else
387 {
388 check_unchanged = FALSE;
389
390 /* 2. 3. 4. 5.: inserted/deleted lines touching this diff. */
391 if (deleted > 0)
392 {
393 if (dp->df_lnum[idx] >= line1)
394 {
395 off = dp->df_lnum[idx] - lnum_deleted;
396 if (last <= line2)
397 {
398 /* 4. delete all lines of diff */
399 if (dp->df_next != NULL
400 && dp->df_next->df_lnum[idx] - 1 <= line2)
401 {
402 /* delete continues in next diff, only do
403 * lines until that one */
404 n = dp->df_next->df_lnum[idx] - lnum_deleted;
405 deleted -= n;
406 n -= dp->df_count[idx];
407 lnum_deleted = dp->df_next->df_lnum[idx];
408 }
409 else
410 n = deleted - dp->df_count[idx];
411 dp->df_count[idx] = 0;
412 }
413 else
414 {
415 /* 5. delete lines at or just before top of diff */
416 n = off;
417 dp->df_count[idx] -= line2 - dp->df_lnum[idx] + 1;
418 check_unchanged = TRUE;
419 }
420 dp->df_lnum[idx] = line1;
421 }
422 else
423 {
424 off = 0;
425 if (last < line2)
426 {
427 /* 2. delete at end of of diff */
428 dp->df_count[idx] -= last - lnum_deleted + 1;
429 if (dp->df_next != NULL
430 && dp->df_next->df_lnum[idx] - 1 <= line2)
431 {
432 /* delete continues in next diff, only do
433 * lines until that one */
434 n = dp->df_next->df_lnum[idx] - 1 - last;
435 deleted -= dp->df_next->df_lnum[idx]
436 - lnum_deleted;
437 lnum_deleted = dp->df_next->df_lnum[idx];
438 }
439 else
440 n = line2 - last;
441 check_unchanged = TRUE;
442 }
443 else
444 {
445 /* 3. delete lines inside the diff */
446 n = 0;
447 dp->df_count[idx] -= deleted;
448 }
449 }
450
451 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000452 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 {
454 dp->df_lnum[i] -= off;
455 dp->df_count[i] += n;
456 }
457 }
458 else
459 {
460 if (dp->df_lnum[idx] <= line1)
461 {
462 /* inserted lines somewhere in this diff */
463 dp->df_count[idx] += inserted;
464 check_unchanged = TRUE;
465 }
466 else
467 /* inserted lines somewhere above this diff */
468 dp->df_lnum[idx] += inserted;
469 }
470
471 if (check_unchanged)
472 /* Check if inserted lines are equal, may reduce the
473 * size of the diff. TODO: also check for equal lines
474 * in the middle and perhaps split the block. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000475 diff_check_unchanged(tp, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476 }
477 }
478
479 /* check if this block touches the previous one, may merge them. */
480 if (dprev != NULL && dprev->df_lnum[idx] + dprev->df_count[idx]
481 == dp->df_lnum[idx])
482 {
483 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000484 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 dprev->df_count[i] += dp->df_count[i];
486 dprev->df_next = dp->df_next;
487 vim_free(dp);
488 dp = dprev->df_next;
489 }
490 else
491 {
492 /* Advance to next entry. */
493 dprev = dp;
494 dp = dp->df_next;
495 }
496 }
497
498 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000499 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000500 while (dp != NULL)
501 {
502 /* All counts are zero, remove this entry. */
503 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000504 if (tp->tp_diffbuf[i] != NULL && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505 break;
506 if (i == DB_COUNT)
507 {
508 dnext = dp->df_next;
509 vim_free(dp);
510 dp = dnext;
511 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000512 tp->tp_first_diff = dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513 else
514 dprev->df_next = dnext;
515 }
516 else
517 {
518 /* Advance to next entry. */
519 dprev = dp;
520 dp = dp->df_next;
521 }
522
523 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000524
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000525 if (tp == curtab)
526 {
527 diff_redraw(TRUE);
528
529 /* Need to recompute the scroll binding, may remove or add filler
530 * lines (e.g., when adding lines above w_topline). But it's slow when
531 * making many changes, postpone until redrawing. */
532 diff_need_scrollbind = TRUE;
533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534}
535
536/*
537 * Allocate a new diff block and link it between "dprev" and "dp".
538 */
539 static diff_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100540diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541{
542 diff_T *dnew;
543
544 dnew = (diff_T *)alloc((unsigned)sizeof(diff_T));
545 if (dnew != NULL)
546 {
547 dnew->df_next = dp;
548 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000549 tp->tp_first_diff = dnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 else
551 dprev->df_next = dnew;
552 }
553 return dnew;
554}
555
556/*
557 * Check if the diff block "dp" can be made smaller for lines at the start and
558 * end that are equal. Called after inserting lines.
559 * This may result in a change where all buffers have zero lines, the caller
560 * must take care of removing it.
561 */
562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100563diff_check_unchanged(tabpage_T *tp, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000564{
565 int i_org;
566 int i_new;
567 int off_org, off_new;
568 char_u *line_org;
569 int dir = FORWARD;
570
571 /* Find the first buffers, use it as the original, compare the other
572 * buffer lines against this one. */
573 for (i_org = 0; i_org < DB_COUNT; ++i_org)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000574 if (tp->tp_diffbuf[i_org] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575 break;
576 if (i_org == DB_COUNT) /* safety check */
577 return;
578
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000579 if (diff_check_sanity(tp, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 return;
581
582 /* First check lines at the top, then at the bottom. */
583 off_org = 0;
584 off_new = 0;
585 for (;;)
586 {
587 /* Repeat until a line is found which is different or the number of
588 * lines has become zero. */
589 while (dp->df_count[i_org] > 0)
590 {
591 /* Copy the line, the next ml_get() will invalidate it. */
592 if (dir == BACKWARD)
593 off_org = dp->df_count[i_org] - 1;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000594 line_org = vim_strsave(ml_get_buf(tp->tp_diffbuf[i_org],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 dp->df_lnum[i_org] + off_org, FALSE));
596 if (line_org == NULL)
597 return;
598 for (i_new = i_org + 1; i_new < DB_COUNT; ++i_new)
599 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000600 if (tp->tp_diffbuf[i_new] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 continue;
602 if (dir == BACKWARD)
603 off_new = dp->df_count[i_new] - 1;
604 /* if other buffer doesn't have this line, it was inserted */
605 if (off_new < 0 || off_new >= dp->df_count[i_new])
606 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000607 if (diff_cmp(line_org, ml_get_buf(tp->tp_diffbuf[i_new],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 dp->df_lnum[i_new] + off_new, FALSE)) != 0)
609 break;
610 }
611 vim_free(line_org);
612
613 /* Stop when a line isn't equal in all diff buffers. */
614 if (i_new != DB_COUNT)
615 break;
616
617 /* Line matched in all buffers, remove it from the diff. */
618 for (i_new = i_org; i_new < DB_COUNT; ++i_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000619 if (tp->tp_diffbuf[i_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000620 {
621 if (dir == FORWARD)
622 ++dp->df_lnum[i_new];
623 --dp->df_count[i_new];
624 }
625 }
626 if (dir == BACKWARD)
627 break;
628 dir = BACKWARD;
629 }
630}
631
632/*
633 * Check if a diff block doesn't contain invalid line numbers.
634 * This can happen when the diff program returns invalid results.
635 */
636 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100637diff_check_sanity(tabpage_T *tp, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638{
639 int i;
640
641 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000642 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000643 if (dp->df_lnum[i] + dp->df_count[i] - 1
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000644 > tp->tp_diffbuf[i]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 return FAIL;
646 return OK;
647}
648
649/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000650 * Mark all diff buffers in the current tab page for redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 */
652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100653diff_redraw(
Bram Moolenaare3521d92018-09-16 14:10:31 +0200654 int dofold) // also recompute the folds
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655{
656 win_T *wp;
657 int n;
658
Bram Moolenaar29323592016-07-24 22:04:11 +0200659 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 if (wp->w_p_diff)
661 {
Bram Moolenaar60f83772006-03-12 21:52:47 +0000662 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663#ifdef FEAT_FOLDING
664 if (dofold && foldmethodIsDiff(wp))
665 foldUpdateAll(wp);
666#endif
667 /* A change may have made filler lines invalid, need to take care
668 * of that for other windows. */
Bram Moolenaara80888d2012-10-21 22:18:21 +0200669 n = diff_check(wp, wp->w_topline);
670 if ((wp != curwin && wp->w_topfill > 0) || n > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 if (wp->w_topfill > n)
673 wp->w_topfill = (n < 0 ? 0 : n);
Bram Moolenaara80888d2012-10-21 22:18:21 +0200674 else if (n > 0 && n > wp->w_topfill)
675 wp->w_topfill = n;
Bram Moolenaar846a2ff2014-05-28 11:35:37 +0200676 check_topfill(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 }
678 }
679}
680
Bram Moolenaare828b762018-09-10 17:51:58 +0200681 static void
682clear_diffin(diffin_T *din)
683{
684 if (din->din_fname == NULL)
685 {
686 vim_free(din->din_mmfile.ptr);
687 din->din_mmfile.ptr = NULL;
688 }
689 else
690 mch_remove(din->din_fname);
691}
692
693 static void
694clear_diffout(diffout_T *dout)
695{
696 if (dout->dout_fname == NULL)
697 ga_clear_strings(&dout->dout_ga);
698 else
699 mch_remove(dout->dout_fname);
700}
701
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702/*
Bram Moolenaare828b762018-09-10 17:51:58 +0200703 * Write buffer "buf" to a memory buffer.
704 * Return FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 */
706 static int
Bram Moolenaare828b762018-09-10 17:51:58 +0200707diff_write_buffer(buf_T *buf, diffin_T *din)
708{
709 linenr_T lnum;
710 char_u *s;
711 long len = 0;
712 char_u *ptr;
713
714 // xdiff requires one big block of memory with all the text.
715 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
Bram Moolenaar6e272ac2018-09-16 14:51:36 +0200716 len += (long)STRLEN(ml_get_buf(buf, lnum, FALSE)) + 1;
Bram Moolenaare828b762018-09-10 17:51:58 +0200717 ptr = lalloc(len, TRUE);
718 if (ptr == NULL)
719 {
720 // Allocating memory failed. This can happen, because we try to read
721 // the whole buffer text into memory. Set the failed flag, the diff
722 // will be retried with external diff. The flag is never reset.
723 buf->b_diff_failed = TRUE;
724 if (p_verbose > 0)
725 {
726 verbose_enter();
727 smsg((char_u *)
728 _("Not enough memory to use internal diff for buffer \"%s\""),
729 buf->b_fname);
730 verbose_leave();
731 }
732 return FAIL;
733 }
734 din->din_mmfile.ptr = (char *)ptr;
735 din->din_mmfile.size = len;
736
737 len = 0;
738 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
739 {
740 for (s = ml_get_buf(buf, lnum, FALSE); *s != NUL; )
741 {
742 if (diff_flags & DIFF_ICASE)
743 {
744 int c;
745
746 // xdiff doesn't support ignoring case, fold-case the text.
747#ifdef FEAT_MBYTE
748 int orig_len;
749 char_u cbuf[MB_MAXBYTES + 1];
750
751 c = PTR2CHAR(s);
752 c = enc_utf8 ? utf_fold(c) : MB_TOLOWER(c);
753 orig_len = MB_PTR2LEN(s);
754 if (mb_char2bytes(c, cbuf) != orig_len)
755 // TODO: handle byte length difference
756 mch_memmove(ptr + len, s, orig_len);
757 else
758 mch_memmove(ptr + len, cbuf, orig_len);
759
760 s += orig_len;
761 len += orig_len;
762#else
763 c = *s++;
764 ptr[len++] = TOLOWER_LOC(c);
765#endif
766 }
767 else
768 ptr[len++] = *s++;
769 }
770 ptr[len++] = NL;
771 }
772 return OK;
773}
774
775/*
776 * Write buffer "buf" to file or memory buffer.
777 * Return FAIL for failure.
778 */
779 static int
780diff_write(buf_T *buf, diffin_T *din)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781{
782 int r;
783 char_u *save_ff;
784
Bram Moolenaare828b762018-09-10 17:51:58 +0200785 if (din->din_fname == NULL)
786 return diff_write_buffer(buf, din);
787
788 // Always use 'fileformat' set to "unix".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 save_ff = buf->b_p_ff;
790 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX);
Bram Moolenaare828b762018-09-10 17:51:58 +0200791 r = buf_write(buf, din->din_fname, NULL,
792 (linenr_T)1, buf->b_ml.ml_line_count,
793 NULL, FALSE, FALSE, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 free_string_option(buf->b_p_ff);
795 buf->b_p_ff = save_ff;
796 return r;
797}
798
799/*
Bram Moolenaare828b762018-09-10 17:51:58 +0200800 * Update the diffs for all buffers involved.
801 */
802 static void
803diff_try_update(
804 diffio_T *dio,
805 int idx_orig,
806 exarg_T *eap) // "eap" can be NULL
807{
808 buf_T *buf;
809 int idx_new;
810
811 if (dio->dio_internal)
812 {
813 ga_init2(&dio->dio_diff.dout_ga, sizeof(char *), 1000);
814 }
815 else
816 {
817 // We need three temp file names.
818 dio->dio_orig.din_fname = vim_tempname('o', TRUE);
819 dio->dio_new.din_fname = vim_tempname('n', TRUE);
820 dio->dio_diff.dout_fname = vim_tempname('d', TRUE);
821 if (dio->dio_orig.din_fname == NULL
822 || dio->dio_new.din_fname == NULL
823 || dio->dio_diff.dout_fname == NULL)
824 goto theend;
825 }
826
827 // Check external diff is actually working.
828 if (!dio->dio_internal && check_external_diff(dio) == FAIL)
829 goto theend;
830
831 // :diffupdate!
832 if (eap != NULL && eap->forceit)
833 for (idx_new = idx_orig; idx_new < DB_COUNT; ++idx_new)
834 {
835 buf = curtab->tp_diffbuf[idx_new];
836 if (buf_valid(buf))
837 buf_check_timestamp(buf, FALSE);
838 }
839
840 // Write the first buffer to a tempfile or mmfile_t.
841 buf = curtab->tp_diffbuf[idx_orig];
842 if (diff_write(buf, &dio->dio_orig) == FAIL)
843 goto theend;
844
845 // Make a difference between the first buffer and every other.
846 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
847 {
848 buf = curtab->tp_diffbuf[idx_new];
849 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
850 continue; // skip buffer that isn't loaded
851
852 // Write the other buffer and diff with the first one.
853 if (diff_write(buf, &dio->dio_new) == FAIL)
854 continue;
855 if (diff_file(dio) == FAIL)
856 continue;
857
858 // Read the diff output and add each entry to the diff list.
859 diff_read(idx_orig, idx_new, &dio->dio_diff);
860
861 clear_diffin(&dio->dio_new);
862 clear_diffout(&dio->dio_diff);
863 }
864 clear_diffin(&dio->dio_orig);
865
866theend:
867 vim_free(dio->dio_orig.din_fname);
868 vim_free(dio->dio_new.din_fname);
869 vim_free(dio->dio_diff.dout_fname);
870}
871
872/*
873 * Return TRUE if the options are set to use the internal diff library.
874 * Note that if the internal diff failed for one of the buffers, the external
875 * diff will be used anyway.
876 */
Bram Moolenaare3521d92018-09-16 14:10:31 +0200877 int
Bram Moolenaare828b762018-09-10 17:51:58 +0200878diff_internal(void)
879{
880 return (diff_flags & DIFF_INTERNAL) != 0 && *p_dex == NUL;
881}
882
883/*
884 * Return TRUE if the internal diff failed for one of the diff buffers.
885 */
886 static int
887diff_internal_failed(void)
888{
889 int idx;
890
891 // Only need to do something when there is another buffer.
892 for (idx = 0; idx < DB_COUNT; ++idx)
893 if (curtab->tp_diffbuf[idx] != NULL
894 && curtab->tp_diffbuf[idx]->b_diff_failed)
895 return TRUE;
896 return FALSE;
897}
898
899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 * Completely update the diffs for the buffers involved.
Bram Moolenaare3521d92018-09-16 14:10:31 +0200901 * When using the external "diff" command the buffers are written to a file,
902 * also for unmodified buffers (the file could have been produced by
903 * autocommands, e.g. the netrw plugin).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905 void
Bram Moolenaare828b762018-09-10 17:51:58 +0200906ex_diffupdate(exarg_T *eap) // "eap" can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 int idx_orig;
909 int idx_new;
Bram Moolenaare828b762018-09-10 17:51:58 +0200910 diffio_T diffio;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
Bram Moolenaard2b58c02018-09-16 18:10:48 +0200912 if (diff_busy)
913 {
914 diff_need_update = TRUE;
915 return;
916 }
917
Bram Moolenaare828b762018-09-10 17:51:58 +0200918 // Delete all diffblocks.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000919 diff_clear(curtab);
920 curtab->tp_diff_invalid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921
Bram Moolenaare828b762018-09-10 17:51:58 +0200922 // Use the first buffer as the original text.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000923 for (idx_orig = 0; idx_orig < DB_COUNT; ++idx_orig)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000924 if (curtab->tp_diffbuf[idx_orig] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 break;
926 if (idx_orig == DB_COUNT)
927 return;
928
Bram Moolenaare828b762018-09-10 17:51:58 +0200929 // Only need to do something when there is another buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000930 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000931 if (curtab->tp_diffbuf[idx_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 break;
933 if (idx_new == DB_COUNT)
934 return;
935
Bram Moolenaare828b762018-09-10 17:51:58 +0200936 // Only use the internal method if it did not fail for one of the buffers.
937 vim_memset(&diffio, 0, sizeof(diffio));
938 diffio.dio_internal = diff_internal() && !diff_internal_failed();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939
Bram Moolenaare828b762018-09-10 17:51:58 +0200940 diff_try_update(&diffio, idx_orig, eap);
941 if (diffio.dio_internal && diff_internal_failed())
942 {
943 // Internal diff failed, use external diff instead.
944 vim_memset(&diffio, 0, sizeof(diffio));
945 diff_try_update(&diffio, idx_orig, eap);
946 }
947
948 // force updating cursor position on screen
949 curwin->w_valid_cursor.lnum = 0;
950
951 diff_redraw(TRUE);
Bram Moolenaare8fa05b2018-09-16 15:48:06 +0200952
953 apply_autocmds(EVENT_DIFFUPDATED, NULL, NULL, FALSE, curbuf);
Bram Moolenaare828b762018-09-10 17:51:58 +0200954}
955
956/*
957 * Do a quick test if "diff" really works. Otherwise it looks like there
958 * are no differences. Can't use the return value, it's non-zero when
959 * there are differences.
960 */
961 static int
962check_external_diff(diffio_T *diffio)
963{
964 FILE *fd;
965 int ok;
966 int io_error = FALSE;
967
968 // May try twice, first with "-a" and then without.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000969 for (;;)
970 {
971 ok = FALSE;
Bram Moolenaare828b762018-09-10 17:51:58 +0200972 fd = mch_fopen((char *)diffio->dio_orig.din_fname, "w");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000973 if (fd == NULL)
974 io_error = TRUE;
975 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000977 if (fwrite("line1\n", (size_t)6, (size_t)1, fd) != 1)
978 io_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 fclose(fd);
Bram Moolenaare828b762018-09-10 17:51:58 +0200980 fd = mch_fopen((char *)diffio->dio_new.din_fname, "w");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000981 if (fd == NULL)
982 io_error = TRUE;
983 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000985 if (fwrite("line2\n", (size_t)6, (size_t)1, fd) != 1)
986 io_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 fclose(fd);
Bram Moolenaare828b762018-09-10 17:51:58 +0200988 fd = NULL;
989 if (diff_file(diffio) == OK)
990 fd = mch_fopen((char *)diffio->dio_diff.dout_fname, "r");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000991 if (fd == NULL)
992 io_error = TRUE;
993 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 {
995 char_u linebuf[LBUFLEN];
996
997 for (;;)
998 {
999 /* There must be a line that contains "1c1". */
1000 if (tag_fgets(linebuf, LBUFLEN, fd))
1001 break;
1002 if (STRNCMP(linebuf, "1c1", 3) == 0)
1003 ok = TRUE;
1004 }
1005 fclose(fd);
1006 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001007 mch_remove(diffio->dio_diff.dout_fname);
1008 mch_remove(diffio->dio_new.din_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001010 mch_remove(diffio->dio_orig.din_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001011 }
1012
1013#ifdef FEAT_EVAL
1014 /* When using 'diffexpr' break here. */
1015 if (*p_dex != NUL)
1016 break;
1017#endif
1018
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001019#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 /* If the "-a" argument works, also check if "--binary" works. */
1021 if (ok && diff_a_works == MAYBE && diff_bin_works == MAYBE)
1022 {
1023 diff_a_works = TRUE;
1024 diff_bin_works = TRUE;
1025 continue;
1026 }
1027 if (!ok && diff_a_works == TRUE && diff_bin_works == TRUE)
1028 {
1029 /* Tried --binary, but it failed. "-a" works though. */
1030 diff_bin_works = FALSE;
1031 ok = TRUE;
1032 }
1033#endif
1034
1035 /* If we checked if "-a" works already, break here. */
1036 if (diff_a_works != MAYBE)
1037 break;
1038 diff_a_works = ok;
1039
1040 /* If "-a" works break here, otherwise retry without "-a". */
1041 if (ok)
1042 break;
1043 }
1044 if (!ok)
1045 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001046 if (io_error)
1047 EMSG(_("E810: Cannot read or write temp files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 EMSG(_("E97: Cannot create diffs"));
1049 diff_a_works = MAYBE;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001050#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 diff_bin_works = MAYBE;
1052#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001053 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001055 return OK;
1056}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057
Bram Moolenaare828b762018-09-10 17:51:58 +02001058/*
1059 * Invoke the xdiff function.
1060 */
1061 static int
1062diff_file_internal(diffio_T *diffio)
1063{
1064 xpparam_t param;
1065 xdemitconf_t emit_cfg;
1066 xdemitcb_t emit_cb;
Bram Moolenaarbd1d5602012-05-18 18:47:17 +02001067
Bram Moolenaare828b762018-09-10 17:51:58 +02001068 vim_memset(&param, 0, sizeof(param));
1069 vim_memset(&emit_cfg, 0, sizeof(emit_cfg));
1070 vim_memset(&emit_cb, 0, sizeof(emit_cb));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
Bram Moolenaare828b762018-09-10 17:51:58 +02001072 param.flags = diff_algorithm;
1073
1074 if (diff_flags & DIFF_IWHITE)
1075 param.flags |= XDF_IGNORE_WHITESPACE_CHANGE;
Bram Moolenaar785fc652018-09-15 19:17:38 +02001076 if (diff_flags & DIFF_IWHITEALL)
1077 param.flags |= XDF_IGNORE_WHITESPACE;
1078 if (diff_flags & DIFF_IWHITEEOL)
1079 param.flags |= XDF_IGNORE_WHITESPACE_AT_EOL;
1080 if (diff_flags & DIFF_IBLANK)
1081 param.flags |= XDF_IGNORE_BLANK_LINES;
Bram Moolenaare828b762018-09-10 17:51:58 +02001082
1083 emit_cfg.ctxlen = 0; // don't need any diff_context here
1084 emit_cb.priv = &diffio->dio_diff;
1085 emit_cb.outf = xdiff_out;
1086 if (xdl_diff(&diffio->dio_orig.din_mmfile,
1087 &diffio->dio_new.din_mmfile,
1088 &param, &emit_cfg, &emit_cb) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001090 EMSG(_("E960: Problem creating the internal diff"));
1091 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001093 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094}
1095
1096/*
1097 * Make a diff between files "tmp_orig" and "tmp_new", results in "tmp_diff".
Bram Moolenaare828b762018-09-10 17:51:58 +02001098 * return OK or FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 */
Bram Moolenaare828b762018-09-10 17:51:58 +02001100 static int
1101diff_file(diffio_T *dio)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102{
1103 char_u *cmd;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001104 size_t len;
Bram Moolenaare828b762018-09-10 17:51:58 +02001105 char_u *tmp_orig = dio->dio_orig.din_fname;
1106 char_u *tmp_new = dio->dio_new.din_fname;
1107 char_u *tmp_diff = dio->dio_diff.dout_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108
1109#ifdef FEAT_EVAL
1110 if (*p_dex != NUL)
Bram Moolenaare828b762018-09-10 17:51:58 +02001111 {
1112 // Use 'diffexpr' to generate the diff file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 eval_diff(tmp_orig, tmp_new, tmp_diff);
Bram Moolenaare828b762018-09-10 17:51:58 +02001114 return OK;
1115 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 else
1117#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001118 // Use xdiff for generating the diff.
1119 if (dio->dio_internal)
1120 {
1121 return diff_file_internal(dio);
1122 }
1123 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001125 len = STRLEN(tmp_orig) + STRLEN(tmp_new)
1126 + STRLEN(tmp_diff) + STRLEN(p_srr) + 27;
1127 cmd = alloc((unsigned)len);
Bram Moolenaare828b762018-09-10 17:51:58 +02001128 if (cmd == NULL)
1129 return FAIL;
Bram Moolenaar95fb60a2005-03-08 22:29:20 +00001130
Bram Moolenaare828b762018-09-10 17:51:58 +02001131 // We don't want $DIFF_OPTIONS to get in the way.
1132 if (getenv("DIFF_OPTIONS"))
1133 vim_setenv((char_u *)"DIFF_OPTIONS", (char_u *)"");
1134
1135 // Build the diff command and execute it. Always use -a, binary
1136 // differences are of no use. Ignore errors, diff returns
1137 // non-zero when differences have been found.
Bram Moolenaar785fc652018-09-15 19:17:38 +02001138 vim_snprintf((char *)cmd, len, "diff %s%s%s%s%s%s%s%s %s",
Bram Moolenaare828b762018-09-10 17:51:58 +02001139 diff_a_works == FALSE ? "" : "-a ",
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001140#if defined(MSWIN)
Bram Moolenaare828b762018-09-10 17:51:58 +02001141 diff_bin_works == TRUE ? "--binary " : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#else
Bram Moolenaare828b762018-09-10 17:51:58 +02001143 "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001145 (diff_flags & DIFF_IWHITE) ? "-b " : "",
Bram Moolenaar785fc652018-09-15 19:17:38 +02001146 (diff_flags & DIFF_IWHITEALL) ? "-w " : "",
1147 (diff_flags & DIFF_IWHITEEOL) ? "-Z " : "",
1148 (diff_flags & DIFF_IBLANK) ? "-B " : "",
Bram Moolenaare828b762018-09-10 17:51:58 +02001149 (diff_flags & DIFF_ICASE) ? "-i " : "",
1150 tmp_orig, tmp_new);
1151 append_redir(cmd, (int)len, p_srr, tmp_diff);
1152 block_autocmds(); // avoid ShellCmdPost stuff
1153 (void)call_shell(cmd, SHELL_FILTER|SHELL_SILENT|SHELL_DOOUT);
1154 unblock_autocmds();
1155 vim_free(cmd);
1156 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 }
1158}
1159
1160/*
1161 * Create a new version of a file from the current buffer and a diff file.
1162 * The buffer is written to a file, also for unmodified buffers (the file
1163 * could have been produced by autocommands, e.g. the netrw plugin).
1164 */
1165 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001166ex_diffpatch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167{
1168 char_u *tmp_orig; /* name of original temp file */
1169 char_u *tmp_new; /* name of patched temp file */
1170 char_u *buf = NULL;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001171 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 win_T *old_curwin = curwin;
1173 char_u *newname = NULL; /* name of patched file buffer */
1174#ifdef UNIX
1175 char_u dirbuf[MAXPATHL];
1176 char_u *fullname = NULL;
1177#endif
1178#ifdef FEAT_BROWSE
1179 char_u *browseFile = NULL;
1180 int browse_flag = cmdmod.browse;
1181#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02001182 stat_T st;
Bram Moolenaara95ab322017-03-11 19:21:53 +01001183 char_u *esc_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184
1185#ifdef FEAT_BROWSE
1186 if (cmdmod.browse)
1187 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001188 browseFile = do_browse(0, (char_u *)_("Patch file"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02001189 eap->arg, NULL, NULL,
1190 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 if (browseFile == NULL)
1192 return; /* operation cancelled */
1193 eap->arg = browseFile;
1194 cmdmod.browse = FALSE; /* don't let do_ecmd() browse again */
1195 }
1196#endif
1197
1198 /* We need two temp file names. */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001199 tmp_orig = vim_tempname('o', FALSE);
1200 tmp_new = vim_tempname('n', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 if (tmp_orig == NULL || tmp_new == NULL)
1202 goto theend;
1203
1204 /* Write the current buffer to "tmp_orig". */
1205 if (buf_write(curbuf, tmp_orig, NULL,
1206 (linenr_T)1, curbuf->b_ml.ml_line_count,
1207 NULL, FALSE, FALSE, FALSE, TRUE) == FAIL)
1208 goto theend;
1209
1210#ifdef UNIX
1211 /* Get the absolute path of the patchfile, changing directory below. */
1212 fullname = FullName_save(eap->arg, FALSE);
1213#endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001214 esc_name = vim_strsave_shellescape(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215# ifdef UNIX
Bram Moolenaara95ab322017-03-11 19:21:53 +01001216 fullname != NULL ? fullname :
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217# endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001218 eap->arg, TRUE, TRUE);
1219 if (esc_name == NULL)
1220 goto theend;
1221 buflen = STRLEN(tmp_orig) + STRLEN(esc_name) + STRLEN(tmp_new) + 16;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001222 buf = alloc((unsigned)buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 if (buf == NULL)
1224 goto theend;
1225
1226#ifdef UNIX
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +00001227 /* Temporarily chdir to /tmp, to avoid patching files in the current
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 * directory when the patch file contains more than one patch. When we
1229 * have our own temp dir use that instead, it will be cleaned up when we
1230 * exit (any .rej files created). Don't change directory if we can't
1231 * return to the current. */
1232 if (mch_dirname(dirbuf, MAXPATHL) != OK || mch_chdir((char *)dirbuf) != 0)
1233 dirbuf[0] = NUL;
1234 else
1235 {
1236# ifdef TEMPDIRNAMES
1237 if (vim_tempdir != NULL)
Bram Moolenaar42335f52018-09-13 15:33:43 +02001238 vim_ignored = mch_chdir((char *)vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 else
1240# endif
Bram Moolenaar42335f52018-09-13 15:33:43 +02001241 vim_ignored = mch_chdir("/tmp");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001242 shorten_fnames(TRUE);
1243 }
1244#endif
1245
1246#ifdef FEAT_EVAL
1247 if (*p_pex != NUL)
1248 /* Use 'patchexpr' to generate the new file. */
1249 eval_patch(tmp_orig,
1250# ifdef UNIX
1251 fullname != NULL ? fullname :
1252# endif
1253 eap->arg, tmp_new);
1254 else
1255#endif
1256 {
1257 /* Build the patch command and execute it. Ignore errors. Switch to
1258 * cooked mode to allow the user to respond to prompts. */
Bram Moolenaara95ab322017-03-11 19:21:53 +01001259 vim_snprintf((char *)buf, buflen, "patch -o %s %s < %s",
1260 tmp_new, tmp_orig, esc_name);
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001261 block_autocmds(); /* Avoid ShellCmdPost stuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001262 (void)call_shell(buf, SHELL_FILTER | SHELL_COOKED);
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001263 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 }
1265
1266#ifdef UNIX
1267 if (dirbuf[0] != NUL)
1268 {
1269 if (mch_chdir((char *)dirbuf) != 0)
1270 EMSG(_(e_prev_dir));
1271 shorten_fnames(TRUE);
1272 }
1273#endif
1274
1275 /* patch probably has written over the screen */
1276 redraw_later(CLEAR);
1277
1278 /* Delete any .orig or .rej file created. */
1279 STRCPY(buf, tmp_new);
1280 STRCAT(buf, ".orig");
1281 mch_remove(buf);
1282 STRCPY(buf, tmp_new);
1283 STRCAT(buf, ".rej");
1284 mch_remove(buf);
1285
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001286 /* Only continue if the output file was created. */
1287 if (mch_stat((char *)tmp_new, &st) < 0 || st.st_size == 0)
1288 EMSG(_("E816: Cannot read patch output"));
1289 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001291 if (curbuf->b_fname != NULL)
1292 {
1293 newname = vim_strnsave(curbuf->b_fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 (int)(STRLEN(curbuf->b_fname) + 4));
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001295 if (newname != NULL)
1296 STRCAT(newname, ".new");
1297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298
1299#ifdef FEAT_GUI
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001300 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301#endif
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001302 /* don't use a new tab page, each tab page has its own diffs */
1303 cmdmod.tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001304
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001305 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001307 /* Pretend it was a ":split fname" command */
1308 eap->cmdidx = CMD_split;
1309 eap->arg = tmp_new;
1310 do_exedit(eap, old_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001312 /* check that split worked and editing tmp_new */
1313 if (curwin != old_curwin && win_valid(old_curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001315 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1316 diff_win_options(curwin, TRUE);
1317 diff_win_options(old_curwin, TRUE);
1318
1319 if (newname != NULL)
1320 {
1321 /* do a ":file filename.new" on the patched buffer */
1322 eap->arg = newname;
1323 ex_file(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001325 /* Do filetype detection with the new name. */
1326 if (au_has_group((char_u *)"filetypedetect"))
1327 do_cmdline_cmd((char_u *)":doau filetypedetect BufRead");
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001328 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 }
1330 }
1331 }
1332
1333theend:
1334 if (tmp_orig != NULL)
1335 mch_remove(tmp_orig);
1336 vim_free(tmp_orig);
1337 if (tmp_new != NULL)
1338 mch_remove(tmp_new);
1339 vim_free(tmp_new);
1340 vim_free(newname);
1341 vim_free(buf);
1342#ifdef UNIX
1343 vim_free(fullname);
1344#endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001345 vim_free(esc_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346#ifdef FEAT_BROWSE
1347 vim_free(browseFile);
1348 cmdmod.browse = browse_flag;
1349#endif
1350}
1351
1352/*
1353 * Split the window and edit another file, setting options to show the diffs.
1354 */
1355 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001356ex_diffsplit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357{
1358 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001359 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001361 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362#ifdef FEAT_GUI
1363 need_mouse_correct = TRUE;
1364#endif
Bram Moolenaar46328f92016-08-28 15:39:57 +02001365 /* Need to compute w_fraction when no redraw happened yet. */
1366 validate_cursor();
1367 set_fraction(curwin);
1368
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001369 /* don't use a new tab page, each tab page has its own diffs */
1370 cmdmod.tab = 0;
1371
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001372 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 {
1374 /* Pretend it was a ":split fname" command */
1375 eap->cmdidx = CMD_split;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001376 curwin->w_p_diff = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 do_exedit(eap, old_curwin);
1378
1379 if (curwin != old_curwin) /* split must have worked */
1380 {
1381 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1382 diff_win_options(curwin, TRUE);
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001383 if (win_valid(old_curwin))
1384 {
1385 diff_win_options(old_curwin, TRUE);
1386
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001387 if (bufref_valid(&old_curbuf))
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001388 /* Move the cursor position to that of the old window. */
1389 curwin->w_cursor.lnum = diff_get_corresponding_line(
Bram Moolenaar025e3e02016-10-18 14:50:18 +02001390 old_curbuf.br_buf, old_curwin->w_cursor.lnum);
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001391 }
Bram Moolenaar46328f92016-08-28 15:39:57 +02001392 /* Now that lines are folded scroll to show the cursor at the same
1393 * relative position. */
1394 scroll_to_fraction(curwin, curwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396 }
1397}
1398
1399/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001400 * Set options to show diffs for the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001401 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001403ex_diffthis(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001404{
1405 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1406 diff_win_options(curwin, TRUE);
1407}
1408
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001409 static void
1410set_diff_option(win_T *wp, int value)
1411{
1412 win_T *old_curwin = curwin;
1413
1414 curwin = wp;
1415 curbuf = curwin->w_buffer;
1416 ++curbuf_lock;
1417 set_option_value((char_u *)"diff", (long)value, NULL, OPT_LOCAL);
1418 --curbuf_lock;
1419 curwin = old_curwin;
1420 curbuf = curwin->w_buffer;
1421}
1422
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423/*
1424 * Set options in window "wp" for diff mode.
1425 */
1426 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001427diff_win_options(
1428 win_T *wp,
1429 int addbuf) /* Add buffer to diff. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430{
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001431# ifdef FEAT_FOLDING
1432 win_T *old_curwin = curwin;
1433
1434 /* close the manually opened folds */
1435 curwin = wp;
1436 newFoldLevel();
1437 curwin = old_curwin;
1438# endif
1439
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001440 /* Use 'scrollbind' and 'cursorbind' when available */
Bram Moolenaar43929962015-07-03 15:06:56 +02001441 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001442 wp->w_p_scb_save = wp->w_p_scb;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001443 wp->w_p_scb = TRUE;
Bram Moolenaar43929962015-07-03 15:06:56 +02001444 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001445 wp->w_p_crb_save = wp->w_p_crb;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001446 wp->w_p_crb = TRUE;
Bram Moolenaar43929962015-07-03 15:06:56 +02001447 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001448 wp->w_p_wrap_save = wp->w_p_wrap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 wp->w_p_wrap = FALSE;
1450# ifdef FEAT_FOLDING
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001451 curwin = wp;
1452 curbuf = curwin->w_buffer;
Bram Moolenaar43929962015-07-03 15:06:56 +02001453 if (!wp->w_p_diff)
1454 {
1455 if (wp->w_p_diff_saved)
1456 free_string_option(wp->w_p_fdm_save);
Bram Moolenaara87aa802013-07-03 15:47:03 +02001457 wp->w_p_fdm_save = vim_strsave(wp->w_p_fdm);
Bram Moolenaar43929962015-07-03 15:06:56 +02001458 }
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001459 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"diff",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001460 OPT_LOCAL|OPT_FREE, 0);
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001461 curwin = old_curwin;
1462 curbuf = curwin->w_buffer;
Bram Moolenaar43929962015-07-03 15:06:56 +02001463 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001464 {
1465 wp->w_p_fdc_save = wp->w_p_fdc;
1466 wp->w_p_fen_save = wp->w_p_fen;
1467 wp->w_p_fdl_save = wp->w_p_fdl;
1468 }
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001469 wp->w_p_fdc = diff_foldcolumn;
1470 wp->w_p_fen = TRUE;
1471 wp->w_p_fdl = 0;
1472 foldUpdateAll(wp);
1473 /* make sure topline is not halfway a fold */
1474 changed_window_setting_win(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 if (vim_strchr(p_sbo, 'h') == NULL)
1477 do_cmdline_cmd((char_u *)"set sbo+=hor");
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001478 /* Save the current values, to be restored in ex_diffoff(). */
Bram Moolenaara87aa802013-07-03 15:47:03 +02001479 wp->w_p_diff_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001481 set_diff_option(wp, TRUE);
Bram Moolenaar43929962015-07-03 15:06:56 +02001482
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483 if (addbuf)
1484 diff_buf_add(wp->w_buffer);
1485 redraw_win_later(wp, NOT_VALID);
1486}
1487
1488/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001489 * Set options not to show diffs. For the current window or all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001490 * Only in the current tab page.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001491 */
1492 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001493ex_diffoff(exarg_T *eap)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001494{
1495 win_T *wp;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001496 int diffwin = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001497
Bram Moolenaar29323592016-07-24 22:04:11 +02001498 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001499 {
Bram Moolenaar00462ff2013-09-20 20:13:53 +02001500 if (eap->forceit ? wp->w_p_diff : wp == curwin)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001501 {
Bram Moolenaar43929962015-07-03 15:06:56 +02001502 /* Set 'diff' off. If option values were saved in
1503 * diff_win_options(), restore the ones whose settings seem to have
1504 * been left over from diff mode. */
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001505 set_diff_option(wp, FALSE);
Bram Moolenaara87aa802013-07-03 15:47:03 +02001506
Bram Moolenaara87aa802013-07-03 15:47:03 +02001507 if (wp->w_p_diff_saved)
1508 {
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001509
Bram Moolenaar43929962015-07-03 15:06:56 +02001510 if (wp->w_p_scb)
1511 wp->w_p_scb = wp->w_p_scb_save;
Bram Moolenaar43929962015-07-03 15:06:56 +02001512 if (wp->w_p_crb)
1513 wp->w_p_crb = wp->w_p_crb_save;
Bram Moolenaar43929962015-07-03 15:06:56 +02001514 if (!wp->w_p_wrap)
1515 wp->w_p_wrap = wp->w_p_wrap_save;
1516#ifdef FEAT_FOLDING
1517 free_string_option(wp->w_p_fdm);
Bram Moolenaar79a213d2017-05-16 13:15:18 +02001518 wp->w_p_fdm = vim_strsave(
1519 *wp->w_p_fdm_save ? wp->w_p_fdm_save : (char_u*)"manual");
Bram Moolenaar43929962015-07-03 15:06:56 +02001520
1521 if (wp->w_p_fdc == diff_foldcolumn)
1522 wp->w_p_fdc = wp->w_p_fdc_save;
1523 if (wp->w_p_fdl == 0)
1524 wp->w_p_fdl = wp->w_p_fdl_save;
1525
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001526 /* Only restore 'foldenable' when 'foldmethod' is not
1527 * "manual", otherwise we continue to show the diff folds. */
Bram Moolenaar43929962015-07-03 15:06:56 +02001528 if (wp->w_p_fen)
1529 wp->w_p_fen = foldmethodIsManual(wp) ? FALSE
1530 : wp->w_p_fen_save;
1531
1532 foldUpdateAll(wp);
Bram Moolenaar43929962015-07-03 15:06:56 +02001533#endif
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001534 }
Bram Moolenaare67d5462016-08-27 22:40:42 +02001535 /* remove filler lines */
1536 wp->w_topfill = 0;
1537
1538 /* make sure topline is not halfway a fold and cursor is
1539 * invalidated */
1540 changed_window_setting_win(wp);
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001541
Bram Moolenaara87aa802013-07-03 15:47:03 +02001542 /* Note: 'sbo' is not restored, it's a global option. */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001543 diff_buf_adjust(wp);
1544 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001545 diffwin |= wp->w_p_diff;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001546 }
1547
Bram Moolenaar25ea0542017-02-03 23:16:28 +01001548 /* Also remove hidden buffers from the list. */
1549 if (eap->forceit)
1550 diff_buf_clear();
1551
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001552 /* Remove "hor" from from 'scrollopt' if there are no diff windows left. */
1553 if (!diffwin && vim_strchr(p_sbo, 'h') != NULL)
1554 do_cmdline_cmd((char_u *)"set sbo-=hor");
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001555}
1556
1557/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558 * Read the diff output and add each entry to the diff list.
1559 */
1560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001561diff_read(
Bram Moolenaare828b762018-09-10 17:51:58 +02001562 int idx_orig, // idx of original file
1563 int idx_new, // idx of new file
1564 diffout_T *dout) // diff output
Bram Moolenaar071d4272004-06-13 20:20:40 +00001565{
Bram Moolenaare828b762018-09-10 17:51:58 +02001566 FILE *fd = NULL;
1567 int line_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 diff_T *dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001569 diff_T *dp = curtab->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570 diff_T *dn, *dpl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 char_u linebuf[LBUFLEN]; /* only need to hold the diff line */
Bram Moolenaare828b762018-09-10 17:51:58 +02001572 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 long off;
1574 int i;
1575 linenr_T lnum_orig, lnum_new;
1576 long count_orig, count_new;
1577 int notset = TRUE; /* block "*dp" not set yet */
Bram Moolenaare828b762018-09-10 17:51:58 +02001578 enum {
1579 DIFF_ED,
1580 DIFF_UNIFIED,
1581 DIFF_NONE
1582 } diffstyle = DIFF_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001583
Bram Moolenaare828b762018-09-10 17:51:58 +02001584 if (dout->dout_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001585 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001586 diffstyle = DIFF_UNIFIED;
1587 }
1588 else
1589 {
1590 fd = mch_fopen((char *)dout->dout_fname, "r");
1591 if (fd == NULL)
1592 {
1593 EMSG(_("E98: Cannot read diff output"));
1594 return;
1595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 }
1597
1598 for (;;)
1599 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001600 if (fd == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001602 if (line_idx >= dout->dout_ga.ga_len)
1603 break; // did last line
1604 line = ((char_u **)dout->dout_ga.ga_data)[line_idx++];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001605 }
1606 else
1607 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001608 if (tag_fgets(linebuf, LBUFLEN, fd))
1609 break; // end of file
1610 line = linebuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 }
1612
Bram Moolenaare828b762018-09-10 17:51:58 +02001613 if (diffstyle == DIFF_NONE)
1614 {
1615 // Determine diff style.
1616 // ed like diff looks like this:
1617 // {first}[,{last}]c{first}[,{last}]
1618 // {first}a{first}[,{last}]
1619 // {first}[,{last}]d{first}
1620 //
1621 // unified diff looks like this:
1622 // --- file1 2018-03-20 13:23:35.783153140 +0100
1623 // +++ file2 2018-03-20 13:23:41.183156066 +0100
1624 // @@ -1,3 +1,5 @@
1625 if (isdigit(*line))
1626 diffstyle = DIFF_ED;
1627 else if ((STRNCMP(line, "@@ ", 3) == 0))
1628 diffstyle = DIFF_UNIFIED;
1629 else if ((STRNCMP(line, "--- ", 4) == 0)
1630 && (tag_fgets(linebuf, LBUFLEN, fd) == 0)
1631 && (STRNCMP(line, "+++ ", 4) == 0)
1632 && (tag_fgets(linebuf, LBUFLEN, fd) == 0)
1633 && (STRNCMP(line, "@@ ", 3) == 0))
1634 diffstyle = DIFF_UNIFIED;
Bram Moolenaar3b8defd2018-09-13 13:03:11 +02001635 else
1636 // Format not recognized yet, skip over this line. Cygwin diff
1637 // may put a warning at the start of the file.
1638 continue;
Bram Moolenaare828b762018-09-10 17:51:58 +02001639 }
1640
1641 if (diffstyle == DIFF_ED)
1642 {
1643 if (!isdigit(*line))
1644 continue; // not the start of a diff block
1645 if (parse_diff_ed(line, &lnum_orig, &count_orig,
1646 &lnum_new, &count_new) == FAIL)
1647 continue;
1648 }
1649 else if (diffstyle == DIFF_UNIFIED)
1650 {
1651 if (STRNCMP(line, "@@ ", 3) != 0)
1652 continue; // not the start of a diff block
1653 if (parse_diff_unified(line, &lnum_orig, &count_orig,
1654 &lnum_new, &count_new) == FAIL)
1655 continue;
1656 }
1657 else
1658 {
1659 EMSG(_("E959: Invalid diff format."));
1660 break;
1661 }
1662
1663 // Go over blocks before the change, for which orig and new are equal.
1664 // Copy blocks from orig to new.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 while (dp != NULL
1666 && lnum_orig > dp->df_lnum[idx_orig] + dp->df_count[idx_orig])
1667 {
1668 if (notset)
1669 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1670 dprev = dp;
1671 dp = dp->df_next;
1672 notset = TRUE;
1673 }
1674
1675 if (dp != NULL
1676 && lnum_orig <= dp->df_lnum[idx_orig] + dp->df_count[idx_orig]
1677 && lnum_orig + count_orig >= dp->df_lnum[idx_orig])
1678 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001679 // New block overlaps with existing block(s).
1680 // First find last block that overlaps.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 for (dpl = dp; dpl->df_next != NULL; dpl = dpl->df_next)
1682 if (lnum_orig + count_orig < dpl->df_next->df_lnum[idx_orig])
1683 break;
1684
Bram Moolenaare828b762018-09-10 17:51:58 +02001685 // If the newly found block starts before the old one, set the
1686 // start back a number of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 off = dp->df_lnum[idx_orig] - lnum_orig;
1688 if (off > 0)
1689 {
1690 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001691 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 dp->df_lnum[i] -= off;
1693 dp->df_lnum[idx_new] = lnum_new;
1694 dp->df_count[idx_new] = count_new;
1695 }
1696 else if (notset)
1697 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001698 // new block inside existing one, adjust new block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 dp->df_lnum[idx_new] = lnum_new + off;
1700 dp->df_count[idx_new] = count_new - off;
1701 }
1702 else
Bram Moolenaare828b762018-09-10 17:51:58 +02001703 // second overlap of new block with existing block
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001704 dp->df_count[idx_new] += count_new - count_orig
1705 + dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]
1706 - (dp->df_lnum[idx_orig] + dp->df_count[idx_orig]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
Bram Moolenaare828b762018-09-10 17:51:58 +02001708 // Adjust the size of the block to include all the lines to the
1709 // end of the existing block or the new diff, whatever ends last.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710 off = (lnum_orig + count_orig)
1711 - (dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]);
1712 if (off < 0)
1713 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001714 // new change ends in existing block, adjust the end if not
1715 // done already
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 if (notset)
1717 dp->df_count[idx_new] += -off;
1718 off = 0;
1719 }
Bram Moolenaard4b96bc2007-10-19 15:33:39 +00001720 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001721 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722 dp->df_count[i] = dpl->df_lnum[i] + dpl->df_count[i]
1723 - dp->df_lnum[i] + off;
1724
Bram Moolenaare828b762018-09-10 17:51:58 +02001725 // Delete the diff blocks that have been merged into one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 dn = dp->df_next;
1727 dp->df_next = dpl->df_next;
1728 while (dn != dp->df_next)
1729 {
1730 dpl = dn->df_next;
1731 vim_free(dn);
1732 dn = dpl;
1733 }
1734 }
1735 else
1736 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001737 // Allocate a new diffblock.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001738 dp = diff_alloc_new(curtab, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001739 if (dp == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001740 goto done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741
1742 dp->df_lnum[idx_orig] = lnum_orig;
1743 dp->df_count[idx_orig] = count_orig;
1744 dp->df_lnum[idx_new] = lnum_new;
1745 dp->df_count[idx_new] = count_new;
1746
Bram Moolenaare828b762018-09-10 17:51:58 +02001747 // Set values for other buffers, these must be equal to the
1748 // original buffer, otherwise there would have been a change
1749 // already.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 for (i = idx_orig + 1; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001751 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752 diff_copy_entry(dprev, dp, idx_orig, i);
1753 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001754 notset = FALSE; // "*dp" has been set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 }
1756
Bram Moolenaare828b762018-09-10 17:51:58 +02001757 // for remaining diff blocks orig and new are equal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001758 while (dp != NULL)
1759 {
1760 if (notset)
1761 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1762 dprev = dp;
1763 dp = dp->df_next;
1764 notset = TRUE;
1765 }
1766
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001767done:
Bram Moolenaare828b762018-09-10 17:51:58 +02001768 if (fd != NULL)
1769 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770}
1771
1772/*
1773 * Copy an entry at "dp" from "idx_orig" to "idx_new".
1774 */
1775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001776diff_copy_entry(
1777 diff_T *dprev,
1778 diff_T *dp,
1779 int idx_orig,
1780 int idx_new)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781{
1782 long off;
1783
1784 if (dprev == NULL)
1785 off = 0;
1786 else
1787 off = (dprev->df_lnum[idx_orig] + dprev->df_count[idx_orig])
1788 - (dprev->df_lnum[idx_new] + dprev->df_count[idx_new]);
1789 dp->df_lnum[idx_new] = dp->df_lnum[idx_orig] - off;
1790 dp->df_count[idx_new] = dp->df_count[idx_orig];
1791}
1792
1793/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001794 * Clear the list of diffblocks for tab page "tp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 */
Bram Moolenaarea408852005-06-25 22:49:46 +00001796 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001797diff_clear(tabpage_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798{
1799 diff_T *p, *next_p;
1800
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001801 for (p = tp->tp_first_diff; p != NULL; p = next_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802 {
1803 next_p = p->df_next;
1804 vim_free(p);
1805 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001806 tp->tp_first_diff = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807}
1808
1809/*
1810 * Check diff status for line "lnum" in buffer "buf":
1811 * Returns 0 for nothing special
1812 * Returns -1 for a line that should be highlighted as changed.
1813 * Returns -2 for a line that should be highlighted as added/deleted.
1814 * Returns > 0 for inserting that many filler lines above it (never happens
1815 * when 'diffopt' doesn't contain "filler").
1816 * This should only be used for windows where 'diff' is set.
1817 */
1818 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001819diff_check(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001821 int idx; /* index in tp_diffbuf[] for this buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822 diff_T *dp;
1823 int maxcount;
1824 int i;
1825 buf_T *buf = wp->w_buffer;
1826 int cmp;
1827
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001828 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 ex_diffupdate(NULL); /* update after a big change */
1830
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001831 if (curtab->tp_first_diff == NULL || !wp->w_p_diff) /* no diffs at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 return 0;
1833
1834 /* safety check: "lnum" must be a buffer line */
1835 if (lnum < 1 || lnum > buf->b_ml.ml_line_count + 1)
1836 return 0;
1837
1838 idx = diff_buf_idx(buf);
1839 if (idx == DB_COUNT)
1840 return 0; /* no diffs for buffer "buf" */
1841
1842#ifdef FEAT_FOLDING
1843 /* A closed fold never has filler lines. */
1844 if (hasFoldingWin(wp, lnum, NULL, NULL, TRUE, NULL))
1845 return 0;
1846#endif
1847
1848 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001849 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
1851 break;
1852 if (dp == NULL || lnum < dp->df_lnum[idx])
1853 return 0;
1854
1855 if (lnum < dp->df_lnum[idx] + dp->df_count[idx])
1856 {
1857 int zero = FALSE;
1858
1859 /* Changed or inserted line. If the other buffers have a count of
1860 * zero, the lines were inserted. If the other buffers have the same
1861 * count, check if the lines are identical. */
1862 cmp = FALSE;
1863 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001864 if (i != idx && curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 {
1866 if (dp->df_count[i] == 0)
1867 zero = TRUE;
1868 else
1869 {
1870 if (dp->df_count[i] != dp->df_count[idx])
1871 return -1; /* nr of lines changed. */
1872 cmp = TRUE;
1873 }
1874 }
1875 if (cmp)
1876 {
1877 /* Compare all lines. If they are equal the lines were inserted
1878 * in some buffers, deleted in others, but not changed. */
1879 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001880 if (i != idx && curtab->tp_diffbuf[i] != NULL
1881 && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882 if (!diff_equal_entry(dp, idx, i))
1883 return -1;
1884 }
1885 /* If there is no buffer with zero lines then there is no difference
1886 * any longer. Happens when making a change (or undo) that removes
1887 * the difference. Can't remove the entry here, we might be halfway
1888 * updating the window. Just report the text as unchanged. Other
1889 * windows might still show the change though. */
1890 if (zero == FALSE)
1891 return 0;
1892 return -2;
1893 }
1894
1895 /* If 'diffopt' doesn't contain "filler", return 0. */
1896 if (!(diff_flags & DIFF_FILLER))
1897 return 0;
1898
1899 /* Insert filler lines above the line just below the change. Will return
1900 * 0 when this buf had the max count. */
1901 maxcount = 0;
1902 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001903 if (curtab->tp_diffbuf[i] != NULL && dp->df_count[i] > maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 maxcount = dp->df_count[i];
1905 return maxcount - dp->df_count[idx];
1906}
1907
1908/*
1909 * Compare two entries in diff "*dp" and return TRUE if they are equal.
1910 */
1911 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001912diff_equal_entry(diff_T *dp, int idx1, int idx2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913{
1914 int i;
1915 char_u *line;
1916 int cmp;
1917
1918 if (dp->df_count[idx1] != dp->df_count[idx2])
1919 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001920 if (diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921 return FALSE;
1922 for (i = 0; i < dp->df_count[idx1]; ++i)
1923 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001924 line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 dp->df_lnum[idx1] + i, FALSE));
1926 if (line == NULL)
1927 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001928 cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 dp->df_lnum[idx2] + i, FALSE));
1930 vim_free(line);
1931 if (cmp != 0)
1932 return FALSE;
1933 }
1934 return TRUE;
1935}
1936
1937/*
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02001938 * Compare the characters at "p1" and "p2". If they are equal (possibly
1939 * ignoring case) return TRUE and set "len" to the number of bytes.
1940 */
1941 static int
1942diff_equal_char(char_u *p1, char_u *p2, int *len)
1943{
1944#ifdef FEAT_MBYTE
1945 int l = (*mb_ptr2len)(p1);
1946
1947 if (l != (*mb_ptr2len)(p2))
1948 return FALSE;
1949 if (l > 1)
1950 {
1951 if (STRNCMP(p1, p2, l) != 0
1952 && (!enc_utf8
1953 || !(diff_flags & DIFF_ICASE)
1954 || utf_fold(utf_ptr2char(p1))
1955 != utf_fold(utf_ptr2char(p2))))
1956 return FALSE;
1957 *len = l;
1958 }
1959 else
1960#endif
1961 {
1962 if ((*p1 != *p2)
1963 && (!(diff_flags & DIFF_ICASE)
1964 || TOLOWER_LOC(*p1) != TOLOWER_LOC(*p2)))
1965 return FALSE;
1966 *len = 1;
1967 }
1968 return TRUE;
1969}
1970
1971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 * Compare strings "s1" and "s2" according to 'diffopt'.
1973 * Return non-zero when they are different.
1974 */
1975 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001976diff_cmp(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977{
1978 char_u *p1, *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980
Bram Moolenaar785fc652018-09-15 19:17:38 +02001981 if ((diff_flags & DIFF_IBLANK)
1982 && (*skipwhite(s1) == NUL || *skipwhite(s2) == NUL))
1983 return 0;
1984
1985 if ((diff_flags & (DIFF_ICASE | ALL_WHITE_DIFF)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 return STRCMP(s1, s2);
Bram Moolenaar785fc652018-09-15 19:17:38 +02001987 if ((diff_flags & DIFF_ICASE) && !(diff_flags & ALL_WHITE_DIFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001988 return MB_STRICMP(s1, s2);
1989
Bram Moolenaar071d4272004-06-13 20:20:40 +00001990 p1 = s1;
1991 p2 = s2;
Bram Moolenaar785fc652018-09-15 19:17:38 +02001992
1993 // Ignore white space changes and possibly ignore case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 while (*p1 != NUL && *p2 != NUL)
1995 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02001996 if (((diff_flags & DIFF_IWHITE)
1997 && VIM_ISWHITE(*p1) && VIM_ISWHITE(*p2))
1998 || ((diff_flags & DIFF_IWHITEALL)
1999 && (VIM_ISWHITE(*p1) || VIM_ISWHITE(*p2))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 {
2001 p1 = skipwhite(p1);
2002 p2 = skipwhite(p2);
2003 }
2004 else
2005 {
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002006 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002007 break;
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002008 p1 += l;
2009 p2 += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002010 }
2011 }
2012
Bram Moolenaar785fc652018-09-15 19:17:38 +02002013 // Ignore trailing white space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 p1 = skipwhite(p1);
2015 p2 = skipwhite(p2);
2016 if (*p1 != NUL || *p2 != NUL)
2017 return 1;
2018 return 0;
2019}
2020
2021/*
2022 * Return the number of filler lines above "lnum".
2023 */
2024 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002025diff_check_fill(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002026{
2027 int n;
2028
2029 /* be quick when there are no filler lines */
2030 if (!(diff_flags & DIFF_FILLER))
2031 return 0;
2032 n = diff_check(wp, lnum);
2033 if (n <= 0)
2034 return 0;
2035 return n;
2036}
2037
2038/*
2039 * Set the topline of "towin" to match the position in "fromwin", so that they
2040 * show the same diff'ed lines.
2041 */
2042 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002043diff_set_topline(win_T *fromwin, win_T *towin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002044{
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002045 buf_T *frombuf = fromwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002046 linenr_T lnum = fromwin->w_topline;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002047 int fromidx;
2048 int toidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 diff_T *dp;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002050 int max_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 int i;
2052
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002053 fromidx = diff_buf_idx(frombuf);
2054 if (fromidx == DB_COUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002055 return; /* safety check */
2056
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002057 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 ex_diffupdate(NULL); /* update after a big change */
2059
2060 towin->w_topfill = 0;
2061
2062 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002063 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002064 if (lnum <= dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 break;
2066 if (dp == NULL)
2067 {
2068 /* After last change, compute topline relative to end of file; no
2069 * filler lines. */
2070 towin->w_topline = towin->w_buffer->b_ml.ml_line_count
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002071 - (frombuf->b_ml.ml_line_count - lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002072 }
2073 else
2074 {
2075 /* Find index for "towin". */
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002076 toidx = diff_buf_idx(towin->w_buffer);
2077 if (toidx == DB_COUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 return; /* safety check */
2079
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002080 towin->w_topline = lnum + (dp->df_lnum[toidx] - dp->df_lnum[fromidx]);
2081 if (lnum >= dp->df_lnum[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002082 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002083 /* Inside a change: compute filler lines. With three or more
2084 * buffers we need to know the largest count. */
2085 max_count = 0;
2086 for (i = 0; i < DB_COUNT; ++i)
2087 if (curtab->tp_diffbuf[i] != NULL
2088 && max_count < dp->df_count[i])
2089 max_count = dp->df_count[i];
2090
2091 if (dp->df_count[toidx] == dp->df_count[fromidx])
2092 {
2093 /* same number of lines: use same filler count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 towin->w_topfill = fromwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 }
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002096 else if (dp->df_count[toidx] > dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002098 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002100 /* more lines in towin and fromwin doesn't show diff
2101 * lines, only filler lines */
2102 if (max_count - fromwin->w_topfill >= dp->df_count[toidx])
2103 {
2104 /* towin also only shows filler lines */
2105 towin->w_topline = dp->df_lnum[toidx]
2106 + dp->df_count[toidx];
2107 towin->w_topfill = fromwin->w_topfill;
2108 }
2109 else
2110 /* towin still has some diff lines to show */
2111 towin->w_topline = dp->df_lnum[toidx]
2112 + max_count - fromwin->w_topfill;
2113 }
2114 }
2115 else if (towin->w_topline >= dp->df_lnum[toidx]
2116 + dp->df_count[toidx])
2117 {
2118 /* less lines in towin and no diff lines to show: compute
2119 * filler lines */
2120 towin->w_topline = dp->df_lnum[toidx] + dp->df_count[toidx];
2121 if (diff_flags & DIFF_FILLER)
2122 {
2123 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
2124 /* fromwin is also out of diff lines */
2125 towin->w_topfill = fromwin->w_topfill;
2126 else
2127 /* fromwin has some diff lines */
2128 towin->w_topfill = dp->df_lnum[fromidx]
2129 + max_count - lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 }
2131 }
2132 }
2133 }
2134
2135 /* safety check (if diff info gets outdated strange things may happen) */
2136 towin->w_botfill = FALSE;
2137 if (towin->w_topline > towin->w_buffer->b_ml.ml_line_count)
2138 {
2139 towin->w_topline = towin->w_buffer->b_ml.ml_line_count;
2140 towin->w_botfill = TRUE;
2141 }
2142 if (towin->w_topline < 1)
2143 {
2144 towin->w_topline = 1;
2145 towin->w_topfill = 0;
2146 }
2147
2148 /* When w_topline changes need to recompute w_botline and cursor position */
2149 invalidate_botline_win(towin);
2150 changed_line_abv_curs_win(towin);
2151
2152 check_topfill(towin, FALSE);
2153#ifdef FEAT_FOLDING
2154 (void)hasFoldingWin(towin, towin->w_topline, &towin->w_topline,
2155 NULL, TRUE, NULL);
2156#endif
2157}
2158
2159/*
2160 * This is called when 'diffopt' is changed.
2161 */
2162 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002163diffopt_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164{
2165 char_u *p;
2166 int diff_context_new = 6;
2167 int diff_flags_new = 0;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002168 int diff_foldcolumn_new = 2;
Bram Moolenaare828b762018-09-10 17:51:58 +02002169 long diff_algorithm_new = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002170 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
2172 p = p_dip;
2173 while (*p != NUL)
2174 {
2175 if (STRNCMP(p, "filler", 6) == 0)
2176 {
2177 p += 6;
2178 diff_flags_new |= DIFF_FILLER;
2179 }
2180 else if (STRNCMP(p, "context:", 8) == 0 && VIM_ISDIGIT(p[8]))
2181 {
2182 p += 8;
2183 diff_context_new = getdigits(&p);
2184 }
Bram Moolenaar785fc652018-09-15 19:17:38 +02002185 else if (STRNCMP(p, "iblank", 6) == 0)
2186 {
2187 p += 6;
2188 diff_flags_new |= DIFF_IBLANK;
2189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 else if (STRNCMP(p, "icase", 5) == 0)
2191 {
2192 p += 5;
2193 diff_flags_new |= DIFF_ICASE;
2194 }
Bram Moolenaar785fc652018-09-15 19:17:38 +02002195 else if (STRNCMP(p, "iwhiteall", 9) == 0)
2196 {
2197 p += 9;
2198 diff_flags_new |= DIFF_IWHITEALL;
2199 }
2200 else if (STRNCMP(p, "iwhiteeol", 9) == 0)
2201 {
2202 p += 9;
2203 diff_flags_new |= DIFF_IWHITEEOL;
2204 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002205 else if (STRNCMP(p, "iwhite", 6) == 0)
2206 {
2207 p += 6;
2208 diff_flags_new |= DIFF_IWHITE;
2209 }
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002210 else if (STRNCMP(p, "horizontal", 10) == 0)
2211 {
2212 p += 10;
2213 diff_flags_new |= DIFF_HORIZONTAL;
2214 }
2215 else if (STRNCMP(p, "vertical", 8) == 0)
2216 {
2217 p += 8;
2218 diff_flags_new |= DIFF_VERTICAL;
2219 }
2220 else if (STRNCMP(p, "foldcolumn:", 11) == 0 && VIM_ISDIGIT(p[11]))
2221 {
2222 p += 11;
2223 diff_foldcolumn_new = getdigits(&p);
2224 }
Bram Moolenaar97ce4192017-12-01 20:35:58 +01002225 else if (STRNCMP(p, "hiddenoff", 9) == 0)
2226 {
2227 p += 9;
2228 diff_flags_new |= DIFF_HIDDEN_OFF;
2229 }
Bram Moolenaare828b762018-09-10 17:51:58 +02002230 else if (STRNCMP(p, "indent-heuristic", 16) == 0)
2231 {
2232 p += 16;
2233 diff_algorithm_new |= XDF_INDENT_HEURISTIC;
2234 }
2235 else if (STRNCMP(p, "internal", 8) == 0)
2236 {
2237 p += 8;
2238 diff_flags_new |= DIFF_INTERNAL;
2239 }
2240 else if (STRNCMP(p, "algorithm:", 10) == 0)
2241 {
2242 p += 10;
2243 if (STRNCMP(p, "myers", 5) == 0)
2244 {
2245 p += 5;
2246 diff_algorithm_new = 0;
2247 }
2248 else if (STRNCMP(p, "minimal", 7) == 0)
2249 {
2250 p += 7;
2251 diff_algorithm_new = XDF_NEED_MINIMAL;
2252 }
2253 else if (STRNCMP(p, "patience", 8) == 0)
2254 {
2255 p += 8;
2256 diff_algorithm_new = XDF_PATIENCE_DIFF;
2257 }
2258 else if (STRNCMP(p, "histogram", 9) == 0)
2259 {
2260 p += 9;
2261 diff_algorithm_new = XDF_HISTOGRAM_DIFF;
2262 }
2263 }
2264
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 if (*p != ',' && *p != NUL)
2266 return FAIL;
2267 if (*p == ',')
2268 ++p;
2269 }
2270
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002271 /* Can't have both "horizontal" and "vertical". */
2272 if ((diff_flags_new & DIFF_HORIZONTAL) && (diff_flags_new & DIFF_VERTICAL))
2273 return FAIL;
2274
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 /* If "icase" or "iwhite" was added or removed, need to update the diff. */
Bram Moolenaare828b762018-09-10 17:51:58 +02002276 if (diff_flags != diff_flags_new || diff_algorithm != diff_algorithm_new)
Bram Moolenaar29323592016-07-24 22:04:11 +02002277 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002278 tp->tp_diff_invalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279
2280 diff_flags = diff_flags_new;
2281 diff_context = diff_context_new;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002282 diff_foldcolumn = diff_foldcolumn_new;
Bram Moolenaare828b762018-09-10 17:51:58 +02002283 diff_algorithm = diff_algorithm_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284
2285 diff_redraw(TRUE);
2286
2287 /* recompute the scroll binding with the new option value, may
2288 * remove or add filler lines */
2289 check_scrollbind((linenr_T)0, 0L);
2290
2291 return OK;
2292}
2293
2294/*
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002295 * Return TRUE if 'diffopt' contains "horizontal".
2296 */
2297 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002298diffopt_horizontal(void)
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002299{
2300 return (diff_flags & DIFF_HORIZONTAL) != 0;
2301}
2302
2303/*
Bram Moolenaar97ce4192017-12-01 20:35:58 +01002304 * Return TRUE if 'diffopt' contains "hiddenoff".
2305 */
2306 int
2307diffopt_hiddenoff(void)
2308{
2309 return (diff_flags & DIFF_HIDDEN_OFF) != 0;
2310}
2311
2312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 * Find the difference within a changed line.
2314 * Returns TRUE if the line was added, no other buffer has it.
2315 */
2316 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002317diff_find_change(
2318 win_T *wp,
2319 linenr_T lnum,
2320 int *startp, /* first char of the change */
2321 int *endp) /* last char of the change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322{
2323 char_u *line_org;
2324 char_u *line_new;
2325 int i;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002326 int si_org, si_new;
2327 int ei_org, ei_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 diff_T *dp;
2329 int idx;
2330 int off;
2331 int added = TRUE;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002332 char_u *p1, *p2;
2333 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334
2335 /* Make a copy of the line, the next ml_get() will invalidate it. */
2336 line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE));
2337 if (line_org == NULL)
2338 return FALSE;
2339
2340 idx = diff_buf_idx(wp->w_buffer);
2341 if (idx == DB_COUNT) /* cannot happen */
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002342 {
2343 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346
2347 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002348 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
2350 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002351 if (dp == NULL || diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002352 {
2353 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
2357 off = lnum - dp->df_lnum[idx];
2358
2359 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002360 if (curtab->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
2362 /* Skip lines that are not in the other change (filler lines). */
2363 if (off >= dp->df_count[i])
2364 continue;
2365 added = FALSE;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002366 line_new = ml_get_buf(curtab->tp_diffbuf[i],
2367 dp->df_lnum[i] + off, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
2369 /* Search for start of difference */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002370 si_org = si_new = 0;
2371 while (line_org[si_org] != NUL)
2372 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002373 if (((diff_flags & DIFF_IWHITE)
2374 && VIM_ISWHITE(line_org[si_org])
2375 && VIM_ISWHITE(line_new[si_new]))
2376 || ((diff_flags & DIFF_IWHITEALL)
2377 && (VIM_ISWHITE(line_org[si_org])
2378 || VIM_ISWHITE(line_new[si_new]))))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002379 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002380 si_org = (int)(skipwhite(line_org + si_org) - line_org);
2381 si_new = (int)(skipwhite(line_new + si_new) - line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002382 }
2383 else
2384 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002385 if (!diff_equal_char(line_org + si_org, line_new + si_new,
2386 &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002387 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002388 si_org += l;
2389 si_new += l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002390 }
2391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392#ifdef FEAT_MBYTE
2393 if (has_mbyte)
2394 {
2395 /* Move back to first byte of character in both lines (may
2396 * have "nn^" in line_org and "n^ in line_new). */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002397 si_org -= (*mb_head_off)(line_org, line_org + si_org);
2398 si_new -= (*mb_head_off)(line_new, line_new + si_new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400#endif
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002401 if (*startp > si_org)
2402 *startp = si_org;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403
2404 /* Search for end of difference, if any. */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002405 if (line_org[si_org] != NUL || line_new[si_new] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002406 {
2407 ei_org = (int)STRLEN(line_org);
2408 ei_new = (int)STRLEN(line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002409 while (ei_org >= *startp && ei_new >= si_new
2410 && ei_org >= 0 && ei_new >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002412 if (((diff_flags & DIFF_IWHITE)
2413 && VIM_ISWHITE(line_org[ei_org])
2414 && VIM_ISWHITE(line_new[ei_new]))
2415 || ((diff_flags & DIFF_IWHITEALL)
2416 && (VIM_ISWHITE(line_org[ei_org])
2417 || VIM_ISWHITE(line_new[ei_new]))))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002418 {
2419 while (ei_org >= *startp
Bram Moolenaar1c465442017-03-12 20:10:05 +01002420 && VIM_ISWHITE(line_org[ei_org]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002421 --ei_org;
2422 while (ei_new >= si_new
Bram Moolenaar1c465442017-03-12 20:10:05 +01002423 && VIM_ISWHITE(line_new[ei_new]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002424 --ei_new;
2425 }
2426 else
2427 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002428 p1 = line_org + ei_org;
2429 p2 = line_new + ei_new;
2430#ifdef FEAT_MBYTE
2431 p1 -= (*mb_head_off)(line_org, p1);
2432 p2 -= (*mb_head_off)(line_new, p2);
2433#endif
2434 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002435 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002436 ei_org -= l;
2437 ei_new -= l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002438 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 }
2440 if (*endp < ei_org)
2441 *endp = ei_org;
2442 }
2443 }
2444
2445 vim_free(line_org);
2446 return added;
2447}
2448
2449#if defined(FEAT_FOLDING) || defined(PROTO)
2450/*
2451 * Return TRUE if line "lnum" is not close to a diff block, this line should
2452 * be in a fold.
2453 * Return FALSE if there are no diff blocks at all in this window.
2454 */
2455 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002456diff_infold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457{
2458 int i;
2459 int idx = -1;
2460 int other = FALSE;
2461 diff_T *dp;
2462
2463 /* Return if 'diff' isn't set. */
2464 if (!wp->w_p_diff)
2465 return FALSE;
2466
2467 for (i = 0; i < DB_COUNT; ++i)
2468 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002469 if (curtab->tp_diffbuf[i] == wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 idx = i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002471 else if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 other = TRUE;
2473 }
2474
2475 /* return here if there are no diffs in the window */
2476 if (idx == -1 || !other)
2477 return FALSE;
2478
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002479 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 ex_diffupdate(NULL); /* update after a big change */
2481
2482 /* Return if there are no diff blocks. All lines will be folded. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002483 if (curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 return TRUE;
2485
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002486 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 {
2488 /* If this change is below the line there can't be any further match. */
2489 if (dp->df_lnum[idx] - diff_context > lnum)
2490 break;
2491 /* If this change ends before the line we have a match. */
2492 if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum)
2493 return FALSE;
2494 }
2495 return TRUE;
2496}
2497#endif
2498
2499/*
2500 * "dp" and "do" commands.
2501 */
2502 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002503nv_diffgetput(int put, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504{
2505 exarg_T ea;
Bram Moolenaar6a643652014-10-31 13:54:25 +01002506 char_u buf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507
Bram Moolenaarf2732452018-06-03 14:47:35 +02002508#ifdef FEAT_JOB_CHANNEL
2509 if (bt_prompt(curbuf))
2510 {
2511 vim_beep(BO_OPER);
2512 return;
2513 }
2514#endif
Bram Moolenaar6a643652014-10-31 13:54:25 +01002515 if (count == 0)
2516 ea.arg = (char_u *)"";
2517 else
2518 {
2519 vim_snprintf((char *)buf, 30, "%ld", count);
2520 ea.arg = buf;
2521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if (put)
2523 ea.cmdidx = CMD_diffput;
2524 else
2525 ea.cmdidx = CMD_diffget;
2526 ea.addr_count = 0;
2527 ea.line1 = curwin->w_cursor.lnum;
2528 ea.line2 = curwin->w_cursor.lnum;
2529 ex_diffgetput(&ea);
2530}
2531
2532/*
2533 * ":diffget"
2534 * ":diffput"
2535 */
2536 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002537ex_diffgetput(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538{
2539 linenr_T lnum;
2540 int count;
2541 linenr_T off = 0;
2542 diff_T *dp;
2543 diff_T *dprev;
2544 diff_T *dfree;
2545 int idx_cur;
2546 int idx_other;
2547 int idx_from;
2548 int idx_to;
2549 int i;
2550 int added;
2551 char_u *p;
2552 aco_save_T aco;
2553 buf_T *buf;
2554 int start_skip, end_skip;
2555 int new_count;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002556 int buf_empty;
Bram Moolenaar602eb742007-02-20 03:43:38 +00002557 int found_not_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558
2559 /* Find the current buffer in the list of diff buffers. */
2560 idx_cur = diff_buf_idx(curbuf);
2561 if (idx_cur == DB_COUNT)
2562 {
2563 EMSG(_("E99: Current buffer is not in diff mode"));
2564 return;
2565 }
2566
2567 if (*eap->arg == NUL)
2568 {
2569 /* No argument: Find the other buffer in the list of diff buffers. */
2570 for (idx_other = 0; idx_other < DB_COUNT; ++idx_other)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002571 if (curtab->tp_diffbuf[idx_other] != curbuf
Bram Moolenaar602eb742007-02-20 03:43:38 +00002572 && curtab->tp_diffbuf[idx_other] != NULL)
2573 {
2574 if (eap->cmdidx != CMD_diffput
2575 || curtab->tp_diffbuf[idx_other]->b_p_ma)
2576 break;
2577 found_not_ma = TRUE;
2578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 if (idx_other == DB_COUNT)
2580 {
Bram Moolenaar602eb742007-02-20 03:43:38 +00002581 if (found_not_ma)
2582 EMSG(_("E793: No other buffer in diff mode is modifiable"));
2583 else
2584 EMSG(_("E100: No other buffer in diff mode"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 return;
2586 }
2587
2588 /* Check that there isn't a third buffer in the list */
2589 for (i = idx_other + 1; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002590 if (curtab->tp_diffbuf[i] != curbuf
2591 && curtab->tp_diffbuf[i] != NULL
2592 && (eap->cmdidx != CMD_diffput || curtab->tp_diffbuf[i]->b_p_ma))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 {
2594 EMSG(_("E101: More than two buffers in diff mode, don't know which one to use"));
2595 return;
2596 }
2597 }
2598 else
2599 {
2600 /* Buffer number or pattern given. Ignore trailing white space. */
2601 p = eap->arg + STRLEN(eap->arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002602 while (p > eap->arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 --p;
2604 for (i = 0; vim_isdigit(eap->arg[i]) && eap->arg + i < p; ++i)
2605 ;
2606 if (eap->arg + i == p) /* digits only */
2607 i = atol((char *)eap->arg);
2608 else
2609 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002610 i = buflist_findpat(eap->arg, p, FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 if (i < 0)
2612 return; /* error message already given */
2613 }
2614 buf = buflist_findnr(i);
2615 if (buf == NULL)
2616 {
2617 EMSG2(_("E102: Can't find buffer \"%s\""), eap->arg);
2618 return;
2619 }
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +00002620 if (buf == curbuf)
2621 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 idx_other = diff_buf_idx(buf);
2623 if (idx_other == DB_COUNT)
2624 {
2625 EMSG2(_("E103: Buffer \"%s\" is not in diff mode"), eap->arg);
2626 return;
2627 }
2628 }
2629
2630 diff_busy = TRUE;
2631
2632 /* When no range given include the line above or below the cursor. */
2633 if (eap->addr_count == 0)
2634 {
2635 /* Make it possible that ":diffget" on the last line gets line below
2636 * the cursor line when there is no difference above the cursor. */
2637 if (eap->cmdidx == CMD_diffget
2638 && eap->line1 == curbuf->b_ml.ml_line_count
2639 && diff_check(curwin, eap->line1) == 0
2640 && (eap->line1 == 1 || diff_check(curwin, eap->line1 - 1) == 0))
2641 ++eap->line2;
2642 else if (eap->line1 > 0)
2643 --eap->line1;
2644 }
2645
2646 if (eap->cmdidx == CMD_diffget)
2647 {
2648 idx_from = idx_other;
2649 idx_to = idx_cur;
2650 }
2651 else
2652 {
2653 idx_from = idx_cur;
2654 idx_to = idx_other;
2655 /* Need to make the other buffer the current buffer to be able to make
2656 * changes in it. */
2657 /* set curwin/curbuf to buf and save a few things */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002658 aucmd_prepbuf(&aco, curtab->tp_diffbuf[idx_other]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002659 }
2660
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002661 /* May give the warning for a changed buffer here, which can trigger the
2662 * FileChangedRO autocommand, which may do nasty things and mess
2663 * everything up. */
2664 if (!curbuf->b_changed)
2665 {
2666 change_warning(0);
2667 if (diff_buf_idx(curbuf) != idx_to)
2668 {
2669 EMSG(_("E787: Buffer changed unexpectedly"));
Bram Moolenaard2b58c02018-09-16 18:10:48 +02002670 goto theend;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002671 }
2672 }
2673
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002675 for (dp = curtab->tp_first_diff; dp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 {
2677 if (dp->df_lnum[idx_cur] > eap->line2 + off)
2678 break; /* past the range that was specified */
2679
2680 dfree = NULL;
2681 lnum = dp->df_lnum[idx_to];
2682 count = dp->df_count[idx_to];
2683 if (dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off
2684 && u_save(lnum - 1, lnum + count) != FAIL)
2685 {
2686 /* Inside the specified range and saving for undo worked. */
2687 start_skip = 0;
2688 end_skip = 0;
2689 if (eap->addr_count > 0)
2690 {
2691 /* A range was specified: check if lines need to be skipped. */
2692 start_skip = eap->line1 + off - dp->df_lnum[idx_cur];
2693 if (start_skip > 0)
2694 {
2695 /* range starts below start of current diff block */
2696 if (start_skip > count)
2697 {
2698 lnum += count;
2699 count = 0;
2700 }
2701 else
2702 {
2703 count -= start_skip;
2704 lnum += start_skip;
2705 }
2706 }
2707 else
2708 start_skip = 0;
2709
2710 end_skip = dp->df_lnum[idx_cur] + dp->df_count[idx_cur] - 1
2711 - (eap->line2 + off);
2712 if (end_skip > 0)
2713 {
2714 /* range ends above end of current/from diff block */
2715 if (idx_cur == idx_from) /* :diffput */
2716 {
2717 i = dp->df_count[idx_cur] - start_skip - end_skip;
2718 if (count > i)
2719 count = i;
2720 }
2721 else /* :diffget */
2722 {
2723 count -= end_skip;
2724 end_skip = dp->df_count[idx_from] - start_skip - count;
2725 if (end_skip < 0)
2726 end_skip = 0;
2727 }
2728 }
2729 else
2730 end_skip = 0;
2731 }
2732
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002733 buf_empty = BUFEMPTY();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 added = 0;
2735 for (i = 0; i < count; ++i)
2736 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00002737 /* remember deleting the last line of the buffer */
2738 buf_empty = curbuf->b_ml.ml_line_count == 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 ml_delete(lnum, FALSE);
2740 --added;
2741 }
2742 for (i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; ++i)
2743 {
2744 linenr_T nr;
2745
2746 nr = dp->df_lnum[idx_from] + start_skip + i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002747 if (nr > curtab->tp_diffbuf[idx_from]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002749 p = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx_from],
2750 nr, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 if (p != NULL)
2752 {
2753 ml_append(lnum + i - 1, p, 0, FALSE);
2754 vim_free(p);
2755 ++added;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002756 if (buf_empty && curbuf->b_ml.ml_line_count == 2)
2757 {
2758 /* Added the first line into an empty buffer, need to
2759 * delete the dummy empty line. */
2760 buf_empty = FALSE;
2761 ml_delete((linenr_T)2, FALSE);
2762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002763 }
2764 }
2765 new_count = dp->df_count[idx_to] + added;
2766 dp->df_count[idx_to] = new_count;
2767
2768 if (start_skip == 0 && end_skip == 0)
2769 {
2770 /* Check if there are any other buffers and if the diff is
2771 * equal in them. */
2772 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002773 if (curtab->tp_diffbuf[i] != NULL && i != idx_from
2774 && i != idx_to
Bram Moolenaar071d4272004-06-13 20:20:40 +00002775 && !diff_equal_entry(dp, idx_from, i))
2776 break;
2777 if (i == DB_COUNT)
2778 {
2779 /* delete the diff entry, the buffers are now equal here */
2780 dfree = dp;
2781 dp = dp->df_next;
2782 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002783 curtab->tp_first_diff = dp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002784 else
2785 dprev->df_next = dp;
2786 }
2787 }
2788
2789 /* Adjust marks. This will change the following entries! */
2790 if (added != 0)
2791 {
2792 mark_adjust(lnum, lnum + count - 1, (long)MAXLNUM, (long)added);
2793 if (curwin->w_cursor.lnum >= lnum)
2794 {
2795 /* Adjust the cursor position if it's in/after the changed
2796 * lines. */
2797 if (curwin->w_cursor.lnum >= lnum + count)
2798 curwin->w_cursor.lnum += added;
2799 else if (added < 0)
2800 curwin->w_cursor.lnum = lnum;
2801 }
2802 }
2803 changed_lines(lnum, 0, lnum + count, (long)added);
2804
2805 if (dfree != NULL)
2806 {
2807 /* Diff is deleted, update folds in other windows. */
2808#ifdef FEAT_FOLDING
2809 diff_fold_update(dfree, idx_to);
2810#endif
2811 vim_free(dfree);
2812 }
2813 else
2814 /* mark_adjust() may have changed the count in a wrong way */
2815 dp->df_count[idx_to] = new_count;
2816
2817 /* When changing the current buffer, keep track of line numbers */
2818 if (idx_cur == idx_to)
2819 off += added;
2820 }
2821
2822 /* If before the range or not deleted, go to next diff. */
2823 if (dfree == NULL)
2824 {
2825 dprev = dp;
2826 dp = dp->df_next;
2827 }
2828 }
2829
2830 /* restore curwin/curbuf and a few other things */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02002831 if (eap->cmdidx != CMD_diffget)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002832 {
2833 /* Syncing undo only works for the current buffer, but we change
2834 * another buffer. Sync undo if the command was typed. This isn't
2835 * 100% right when ":diffput" is used in a function or mapping. */
2836 if (KeyTyped)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002837 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002838 aucmd_restbuf(&aco);
2839 }
2840
Bram Moolenaard2b58c02018-09-16 18:10:48 +02002841theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842 diff_busy = FALSE;
Bram Moolenaard2b58c02018-09-16 18:10:48 +02002843 if (diff_need_update)
2844 {
2845 diff_need_update = FALSE;
2846 ex_diffupdate(NULL);
2847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848
2849 /* Check that the cursor is on a valid character and update it's position.
2850 * When there were filler lines the topline has become invalid. */
2851 check_cursor();
2852 changed_line_abv_curs();
2853
2854 /* Also need to redraw the other buffers. */
2855 diff_redraw(FALSE);
2856}
2857
2858#ifdef FEAT_FOLDING
2859/*
2860 * Update folds for all diff buffers for entry "dp".
2861 * Skip buffer with index "skip_idx".
2862 * When there are no diffs, all folds are removed.
2863 */
2864 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002865diff_fold_update(diff_T *dp, int skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
2867 int i;
2868 win_T *wp;
2869
Bram Moolenaar29323592016-07-24 22:04:11 +02002870 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002872 if (curtab->tp_diffbuf[i] == wp->w_buffer && i != skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 foldUpdate(wp, dp->df_lnum[i],
2874 dp->df_lnum[i] + dp->df_count[i]);
2875}
2876#endif
2877
2878/*
2879 * Return TRUE if buffer "buf" is in diff-mode.
2880 */
2881 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002882diff_mode_buf(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002884 tabpage_T *tp;
2885
Bram Moolenaar29323592016-07-24 22:04:11 +02002886 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002887 if (diff_buf_idx_tp(buf, tp) != DB_COUNT)
2888 return TRUE;
2889 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002890}
2891
2892/*
2893 * Move "count" times in direction "dir" to the next diff block.
2894 * Return FAIL if there isn't such a diff block.
2895 */
2896 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002897diff_move_to(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898{
2899 int idx;
2900 linenr_T lnum = curwin->w_cursor.lnum;
2901 diff_T *dp;
2902
2903 idx = diff_buf_idx(curbuf);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002904 if (idx == DB_COUNT || curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 return FAIL;
2906
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002907 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 ex_diffupdate(NULL); /* update after a big change */
2909
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002910 if (curtab->tp_first_diff == NULL) /* no diffs today */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 return FAIL;
2912
2913 while (--count >= 0)
2914 {
2915 /* Check if already before first diff. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002916 if (dir == BACKWARD && lnum <= curtab->tp_first_diff->df_lnum[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 break;
2918
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002919 for (dp = curtab->tp_first_diff; ; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 {
2921 if (dp == NULL)
2922 break;
2923 if ((dir == FORWARD && lnum < dp->df_lnum[idx])
2924 || (dir == BACKWARD
2925 && (dp->df_next == NULL
2926 || lnum <= dp->df_next->df_lnum[idx])))
2927 {
2928 lnum = dp->df_lnum[idx];
2929 break;
2930 }
2931 }
2932 }
2933
2934 /* don't end up past the end of the file */
2935 if (lnum > curbuf->b_ml.ml_line_count)
2936 lnum = curbuf->b_ml.ml_line_count;
2937
2938 /* When the cursor didn't move at all we fail. */
2939 if (lnum == curwin->w_cursor.lnum)
2940 return FAIL;
2941
2942 setpcmark();
2943 curwin->w_cursor.lnum = lnum;
2944 curwin->w_cursor.col = 0;
2945
2946 return OK;
2947}
2948
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002949/*
2950 * Return the line number in the current window that is closest to "lnum1" in
2951 * "buf1" in diff mode.
2952 */
2953 static linenr_T
2954diff_get_corresponding_line_int(
Bram Moolenaar7454a062016-01-30 15:14:10 +01002955 buf_T *buf1,
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002956 linenr_T lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002957{
2958 int idx1;
2959 int idx2;
2960 diff_T *dp;
2961 int baseline = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002962
2963 idx1 = diff_buf_idx(buf1);
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002964 idx2 = diff_buf_idx(curbuf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002965 if (idx1 == DB_COUNT || idx2 == DB_COUNT || curtab->tp_first_diff == NULL)
2966 return lnum1;
2967
2968 if (curtab->tp_diff_invalid)
2969 ex_diffupdate(NULL); /* update after a big change */
2970
2971 if (curtab->tp_first_diff == NULL) /* no diffs today */
2972 return lnum1;
2973
2974 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
2975 {
2976 if (dp->df_lnum[idx1] > lnum1)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002977 return lnum1 - baseline;
2978 if ((dp->df_lnum[idx1] + dp->df_count[idx1]) > lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002979 {
2980 /* Inside the diffblock */
2981 baseline = lnum1 - dp->df_lnum[idx1];
2982 if (baseline > dp->df_count[idx2])
2983 baseline = dp->df_count[idx2];
2984
2985 return dp->df_lnum[idx2] + baseline;
2986 }
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002987 if ( (dp->df_lnum[idx1] == lnum1)
2988 && (dp->df_count[idx1] == 0)
2989 && (dp->df_lnum[idx2] <= curwin->w_cursor.lnum)
2990 && ((dp->df_lnum[idx2] + dp->df_count[idx2])
2991 > curwin->w_cursor.lnum))
Bram Moolenaar860cae12010-06-05 23:22:07 +02002992 /*
2993 * Special case: if the cursor is just after a zero-count
2994 * block (i.e. all filler) and the target cursor is already
2995 * inside the corresponding block, leave the target cursor
2996 * unmoved. This makes repeated CTRL-W W operations work
2997 * as expected.
2998 */
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002999 return curwin->w_cursor.lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003000 baseline = (dp->df_lnum[idx1] + dp->df_count[idx1])
3001 - (dp->df_lnum[idx2] + dp->df_count[idx2]);
3002 }
3003
3004 /* If we get here then the cursor is after the last diff */
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003005 return lnum1 - baseline;
3006}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003007
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003008/*
3009 * Return the line number in the current window that is closest to "lnum1" in
3010 * "buf1" in diff mode. Checks the line number to be valid.
3011 */
3012 linenr_T
3013diff_get_corresponding_line(buf_T *buf1, linenr_T lnum1)
3014{
3015 linenr_T lnum = diff_get_corresponding_line_int(buf1, lnum1);
3016
3017 /* don't end up past the end of the file */
3018 if (lnum > curbuf->b_ml.ml_line_count)
3019 return curbuf->b_ml.ml_line_count;
3020 return lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003021}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003022
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023/*
3024 * For line "lnum" in the current window find the equivalent lnum in window
3025 * "wp", compensating for inserted/deleted lines.
3026 */
3027 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003028diff_lnum_win(linenr_T lnum, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029{
3030 diff_T *dp;
3031 int idx;
3032 int i;
3033 linenr_T n;
3034
3035 idx = diff_buf_idx(curbuf);
3036 if (idx == DB_COUNT) /* safety check */
3037 return (linenr_T)0;
3038
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003039 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 ex_diffupdate(NULL); /* update after a big change */
3041
3042 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003043 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
3045 break;
3046
3047 /* When after the last change, compute relative to the last line number. */
3048 if (dp == NULL)
3049 return wp->w_buffer->b_ml.ml_line_count
3050 - (curbuf->b_ml.ml_line_count - lnum);
3051
3052 /* Find index for "wp". */
3053 i = diff_buf_idx(wp->w_buffer);
3054 if (i == DB_COUNT) /* safety check */
3055 return (linenr_T)0;
3056
3057 n = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]);
3058 if (n > dp->df_lnum[i] + dp->df_count[i])
3059 n = dp->df_lnum[i] + dp->df_count[i];
3060 return n;
3061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062
Bram Moolenaare828b762018-09-10 17:51:58 +02003063/*
3064 * Handle an ED style diff line.
3065 * Return FAIL if the line does not contain diff info.
3066 */
3067 static int
3068parse_diff_ed(
3069 char_u *line,
3070 linenr_T *lnum_orig,
3071 long *count_orig,
3072 linenr_T *lnum_new,
3073 long *count_new)
3074{
3075 char_u *p;
3076 long f1, l1, f2, l2;
3077 int difftype;
3078
3079 // The line must be one of three formats:
3080 // change: {first}[,{last}]c{first}[,{last}]
3081 // append: {first}a{first}[,{last}]
3082 // delete: {first}[,{last}]d{first}
3083 p = line;
3084 f1 = getdigits(&p);
3085 if (*p == ',')
3086 {
3087 ++p;
3088 l1 = getdigits(&p);
3089 }
3090 else
3091 l1 = f1;
3092 if (*p != 'a' && *p != 'c' && *p != 'd')
3093 return FAIL; // invalid diff format
3094 difftype = *p++;
3095 f2 = getdigits(&p);
3096 if (*p == ',')
3097 {
3098 ++p;
3099 l2 = getdigits(&p);
3100 }
3101 else
3102 l2 = f2;
3103 if (l1 < f1 || l2 < f2)
3104 return FAIL;
3105
3106 if (difftype == 'a')
3107 {
3108 *lnum_orig = f1 + 1;
3109 *count_orig = 0;
3110 }
3111 else
3112 {
3113 *lnum_orig = f1;
3114 *count_orig = l1 - f1 + 1;
3115 }
3116 if (difftype == 'd')
3117 {
3118 *lnum_new = f2 + 1;
3119 *count_new = 0;
3120 }
3121 else
3122 {
3123 *lnum_new = f2;
3124 *count_new = l2 - f2 + 1;
3125 }
3126 return OK;
3127}
3128
3129/*
3130 * Parses unified diff with zero(!) context lines.
3131 * Return FAIL if there is no diff information in "line".
3132 */
3133 static int
3134parse_diff_unified(
3135 char_u *line,
3136 linenr_T *lnum_orig,
3137 long *count_orig,
3138 linenr_T *lnum_new,
3139 long *count_new)
3140{
3141 char_u *p;
3142 long oldline, oldcount, newline, newcount;
3143
3144 // Parse unified diff hunk header:
3145 // @@ -oldline,oldcount +newline,newcount @@
3146 p = line;
3147 if (*p++ == '@' && *p++ == '@' && *p++ == ' ' && *p++ == '-')
3148 {
3149 oldline = getdigits(&p);
3150 if (*p == ',')
3151 {
3152 ++p;
3153 oldcount = getdigits(&p);
3154 }
3155 else
3156 oldcount = 1;
3157 if (*p++ == ' ' && *p++ == '+')
3158 {
3159 newline = getdigits(&p);
3160 if (*p == ',')
3161 {
3162 ++p;
3163 newcount = getdigits(&p);
3164 }
3165 else
3166 newcount = 1;
3167 }
3168 else
3169 return FAIL; // invalid diff format
3170
3171 if (oldcount == 0)
3172 oldline += 1;
3173 if (newcount == 0)
3174 newline += 1;
3175 if (newline == 0)
3176 newline = 1;
3177
3178 *lnum_orig = oldline;
3179 *count_orig = oldcount;
3180 *lnum_new = newline;
3181 *count_new = newcount;
3182
3183 return OK;
3184 }
3185
3186 return FAIL;
3187}
3188
3189/*
3190 * Callback function for the xdl_diff() function.
3191 * Stores the diff output in a grow array.
3192 */
3193 static int
3194xdiff_out(void *priv, mmbuffer_t *mb, int nbuf)
3195{
3196 diffout_T *dout = (diffout_T *)priv;
3197 int i;
3198 char_u *p;
3199
3200 for (i = 0; i < nbuf; i++)
3201 {
3202 // We are only interested in the header lines, skip text lines.
3203 if (STRNCMP(mb[i].ptr, "@@ ", 3) != 0)
3204 continue;
3205 if (ga_grow(&dout->dout_ga, 1) == FAIL)
3206 return -1;
3207 p = vim_strnsave((char_u *)mb[i].ptr, mb[i].size);
3208 if (p == NULL)
3209 return -1;
3210 ((char_u **)dout->dout_ga.ga_data)[dout->dout_ga.ga_len++] = p;
3211 }
3212 return 0;
3213}
3214
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215#endif /* FEAT_DIFF */