blob: 3acedc786d17828cb655cc72ba0d06d27c526760 [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 Moolenaarf9e3e092019-01-13 23:38:42 +0100176 semsg(_("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 {
Bram Moolenaar198fa062018-09-18 21:20:26 +0200298 // Will update diffs before redrawing. Set _invalid to update the
Bram Moolenaare3521d92018-09-16 14:10:31 +0200299 // diffs themselves, set _update to also update folds properly just
300 // before redrawing.
Bram Moolenaar5f57bdc2018-10-25 17:52:23 +0200301 // Do update marks here, it is needed for :%diffput.
Bram Moolenaare3521d92018-09-16 14:10:31 +0200302 tp->tp_diff_invalid = TRUE;
303 tp->tp_diff_update = TRUE;
Bram Moolenaare3521d92018-09-16 14:10:31 +0200304 }
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();
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100727 smsg(_("Not enough memory to use internal diff for buffer \"%s\""),
Bram Moolenaare828b762018-09-10 17:51:58 +0200728 buf->b_fname);
729 verbose_leave();
730 }
731 return FAIL;
732 }
733 din->din_mmfile.ptr = (char *)ptr;
734 din->din_mmfile.size = len;
735
736 len = 0;
737 for (lnum = 1; lnum <= buf->b_ml.ml_line_count; ++lnum)
738 {
739 for (s = ml_get_buf(buf, lnum, FALSE); *s != NUL; )
740 {
741 if (diff_flags & DIFF_ICASE)
742 {
743 int c;
744
745 // xdiff doesn't support ignoring case, fold-case the text.
746#ifdef FEAT_MBYTE
747 int orig_len;
748 char_u cbuf[MB_MAXBYTES + 1];
749
750 c = PTR2CHAR(s);
751 c = enc_utf8 ? utf_fold(c) : MB_TOLOWER(c);
752 orig_len = MB_PTR2LEN(s);
753 if (mb_char2bytes(c, cbuf) != orig_len)
754 // TODO: handle byte length difference
755 mch_memmove(ptr + len, s, orig_len);
756 else
757 mch_memmove(ptr + len, cbuf, orig_len);
758
759 s += orig_len;
760 len += orig_len;
761#else
762 c = *s++;
763 ptr[len++] = TOLOWER_LOC(c);
764#endif
765 }
766 else
767 ptr[len++] = *s++;
768 }
769 ptr[len++] = NL;
770 }
771 return OK;
772}
773
774/*
775 * Write buffer "buf" to file or memory buffer.
776 * Return FAIL for failure.
777 */
778 static int
779diff_write(buf_T *buf, diffin_T *din)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780{
781 int r;
782 char_u *save_ff;
783
Bram Moolenaare828b762018-09-10 17:51:58 +0200784 if (din->din_fname == NULL)
785 return diff_write_buffer(buf, din);
786
787 // Always use 'fileformat' set to "unix".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 save_ff = buf->b_p_ff;
789 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX);
Bram Moolenaare828b762018-09-10 17:51:58 +0200790 r = buf_write(buf, din->din_fname, NULL,
791 (linenr_T)1, buf->b_ml.ml_line_count,
792 NULL, FALSE, FALSE, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 free_string_option(buf->b_p_ff);
794 buf->b_p_ff = save_ff;
795 return r;
796}
797
798/*
Bram Moolenaare828b762018-09-10 17:51:58 +0200799 * Update the diffs for all buffers involved.
800 */
801 static void
802diff_try_update(
803 diffio_T *dio,
804 int idx_orig,
805 exarg_T *eap) // "eap" can be NULL
806{
807 buf_T *buf;
808 int idx_new;
809
810 if (dio->dio_internal)
811 {
812 ga_init2(&dio->dio_diff.dout_ga, sizeof(char *), 1000);
813 }
814 else
815 {
816 // We need three temp file names.
817 dio->dio_orig.din_fname = vim_tempname('o', TRUE);
818 dio->dio_new.din_fname = vim_tempname('n', TRUE);
819 dio->dio_diff.dout_fname = vim_tempname('d', TRUE);
820 if (dio->dio_orig.din_fname == NULL
821 || dio->dio_new.din_fname == NULL
822 || dio->dio_diff.dout_fname == NULL)
823 goto theend;
824 }
825
826 // Check external diff is actually working.
827 if (!dio->dio_internal && check_external_diff(dio) == FAIL)
828 goto theend;
829
830 // :diffupdate!
831 if (eap != NULL && eap->forceit)
832 for (idx_new = idx_orig; idx_new < DB_COUNT; ++idx_new)
833 {
834 buf = curtab->tp_diffbuf[idx_new];
835 if (buf_valid(buf))
836 buf_check_timestamp(buf, FALSE);
837 }
838
839 // Write the first buffer to a tempfile or mmfile_t.
840 buf = curtab->tp_diffbuf[idx_orig];
841 if (diff_write(buf, &dio->dio_orig) == FAIL)
842 goto theend;
843
844 // Make a difference between the first buffer and every other.
845 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
846 {
847 buf = curtab->tp_diffbuf[idx_new];
848 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
849 continue; // skip buffer that isn't loaded
850
851 // Write the other buffer and diff with the first one.
852 if (diff_write(buf, &dio->dio_new) == FAIL)
853 continue;
854 if (diff_file(dio) == FAIL)
855 continue;
856
857 // Read the diff output and add each entry to the diff list.
858 diff_read(idx_orig, idx_new, &dio->dio_diff);
859
860 clear_diffin(&dio->dio_new);
861 clear_diffout(&dio->dio_diff);
862 }
863 clear_diffin(&dio->dio_orig);
864
865theend:
866 vim_free(dio->dio_orig.din_fname);
867 vim_free(dio->dio_new.din_fname);
868 vim_free(dio->dio_diff.dout_fname);
869}
870
871/*
872 * Return TRUE if the options are set to use the internal diff library.
873 * Note that if the internal diff failed for one of the buffers, the external
874 * diff will be used anyway.
875 */
Bram Moolenaare3521d92018-09-16 14:10:31 +0200876 int
Bram Moolenaare828b762018-09-10 17:51:58 +0200877diff_internal(void)
878{
879 return (diff_flags & DIFF_INTERNAL) != 0 && *p_dex == NUL;
880}
881
882/*
883 * Return TRUE if the internal diff failed for one of the diff buffers.
884 */
885 static int
886diff_internal_failed(void)
887{
888 int idx;
889
890 // Only need to do something when there is another buffer.
891 for (idx = 0; idx < DB_COUNT; ++idx)
892 if (curtab->tp_diffbuf[idx] != NULL
893 && curtab->tp_diffbuf[idx]->b_diff_failed)
894 return TRUE;
895 return FALSE;
896}
897
898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 * Completely update the diffs for the buffers involved.
Bram Moolenaare3521d92018-09-16 14:10:31 +0200900 * When using the external "diff" command the buffers are written to a file,
901 * also for unmodified buffers (the file could have been produced by
902 * autocommands, e.g. the netrw plugin).
Bram Moolenaar071d4272004-06-13 20:20:40 +0000903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 void
Bram Moolenaare828b762018-09-10 17:51:58 +0200905ex_diffupdate(exarg_T *eap) // "eap" can be NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 int idx_orig;
908 int idx_new;
Bram Moolenaare828b762018-09-10 17:51:58 +0200909 diffio_T diffio;
Bram Moolenaar198fa062018-09-18 21:20:26 +0200910 int had_diffs = curtab->tp_first_diff != NULL;
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)
Bram Moolenaar198fa062018-09-18 21:20:26 +0200927 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000928
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)
Bram Moolenaar198fa062018-09-18 21:20:26 +0200934 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935
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
Bram Moolenaar198fa062018-09-18 21:20:26 +0200951theend:
952 // A redraw is needed if there were diffs and they were cleared, or there
953 // are diffs now, which means they got updated.
954 if (had_diffs || curtab->tp_first_diff != NULL)
955 {
956 diff_redraw(TRUE);
957 apply_autocmds(EVENT_DIFFUPDATED, NULL, NULL, FALSE, curbuf);
958 }
Bram Moolenaare828b762018-09-10 17:51:58 +0200959}
960
961/*
962 * Do a quick test if "diff" really works. Otherwise it looks like there
963 * are no differences. Can't use the return value, it's non-zero when
964 * there are differences.
965 */
966 static int
967check_external_diff(diffio_T *diffio)
968{
969 FILE *fd;
970 int ok;
971 int io_error = FALSE;
972
973 // May try twice, first with "-a" and then without.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000974 for (;;)
975 {
976 ok = FALSE;
Bram Moolenaare828b762018-09-10 17:51:58 +0200977 fd = mch_fopen((char *)diffio->dio_orig.din_fname, "w");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000978 if (fd == NULL)
979 io_error = TRUE;
980 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000981 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000982 if (fwrite("line1\n", (size_t)6, (size_t)1, fd) != 1)
983 io_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000984 fclose(fd);
Bram Moolenaare828b762018-09-10 17:51:58 +0200985 fd = mch_fopen((char *)diffio->dio_new.din_fname, "w");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000986 if (fd == NULL)
987 io_error = TRUE;
988 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000989 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000990 if (fwrite("line2\n", (size_t)6, (size_t)1, fd) != 1)
991 io_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 fclose(fd);
Bram Moolenaare828b762018-09-10 17:51:58 +0200993 fd = NULL;
994 if (diff_file(diffio) == OK)
995 fd = mch_fopen((char *)diffio->dio_diff.dout_fname, "r");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000996 if (fd == NULL)
997 io_error = TRUE;
998 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 {
1000 char_u linebuf[LBUFLEN];
1001
1002 for (;;)
1003 {
1004 /* There must be a line that contains "1c1". */
1005 if (tag_fgets(linebuf, LBUFLEN, fd))
1006 break;
1007 if (STRNCMP(linebuf, "1c1", 3) == 0)
1008 ok = TRUE;
1009 }
1010 fclose(fd);
1011 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001012 mch_remove(diffio->dio_diff.dout_fname);
1013 mch_remove(diffio->dio_new.din_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001014 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001015 mch_remove(diffio->dio_orig.din_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 }
1017
1018#ifdef FEAT_EVAL
1019 /* When using 'diffexpr' break here. */
1020 if (*p_dex != NUL)
1021 break;
1022#endif
1023
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001024#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025 /* If the "-a" argument works, also check if "--binary" works. */
1026 if (ok && diff_a_works == MAYBE && diff_bin_works == MAYBE)
1027 {
1028 diff_a_works = TRUE;
1029 diff_bin_works = TRUE;
1030 continue;
1031 }
1032 if (!ok && diff_a_works == TRUE && diff_bin_works == TRUE)
1033 {
1034 /* Tried --binary, but it failed. "-a" works though. */
1035 diff_bin_works = FALSE;
1036 ok = TRUE;
1037 }
1038#endif
1039
1040 /* If we checked if "-a" works already, break here. */
1041 if (diff_a_works != MAYBE)
1042 break;
1043 diff_a_works = ok;
1044
1045 /* If "-a" works break here, otherwise retry without "-a". */
1046 if (ok)
1047 break;
1048 }
1049 if (!ok)
1050 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +00001051 if (io_error)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001052 emsg(_("E810: Cannot read or write temp files"));
1053 emsg(_("E97: Cannot create diffs"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054 diff_a_works = MAYBE;
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001055#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 diff_bin_works = MAYBE;
1057#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001058 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001060 return OK;
1061}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062
Bram Moolenaare828b762018-09-10 17:51:58 +02001063/*
1064 * Invoke the xdiff function.
1065 */
1066 static int
1067diff_file_internal(diffio_T *diffio)
1068{
1069 xpparam_t param;
1070 xdemitconf_t emit_cfg;
1071 xdemitcb_t emit_cb;
Bram Moolenaarbd1d5602012-05-18 18:47:17 +02001072
Bram Moolenaare828b762018-09-10 17:51:58 +02001073 vim_memset(&param, 0, sizeof(param));
1074 vim_memset(&emit_cfg, 0, sizeof(emit_cfg));
1075 vim_memset(&emit_cb, 0, sizeof(emit_cb));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076
Bram Moolenaare828b762018-09-10 17:51:58 +02001077 param.flags = diff_algorithm;
1078
1079 if (diff_flags & DIFF_IWHITE)
1080 param.flags |= XDF_IGNORE_WHITESPACE_CHANGE;
Bram Moolenaar785fc652018-09-15 19:17:38 +02001081 if (diff_flags & DIFF_IWHITEALL)
1082 param.flags |= XDF_IGNORE_WHITESPACE;
1083 if (diff_flags & DIFF_IWHITEEOL)
1084 param.flags |= XDF_IGNORE_WHITESPACE_AT_EOL;
1085 if (diff_flags & DIFF_IBLANK)
1086 param.flags |= XDF_IGNORE_BLANK_LINES;
Bram Moolenaare828b762018-09-10 17:51:58 +02001087
1088 emit_cfg.ctxlen = 0; // don't need any diff_context here
1089 emit_cb.priv = &diffio->dio_diff;
1090 emit_cb.outf = xdiff_out;
1091 if (xdl_diff(&diffio->dio_orig.din_mmfile,
1092 &diffio->dio_new.din_mmfile,
1093 &param, &emit_cfg, &emit_cb) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001095 emsg(_("E960: Problem creating the internal diff"));
Bram Moolenaare828b762018-09-10 17:51:58 +02001096 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001098 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099}
1100
1101/*
1102 * Make a diff between files "tmp_orig" and "tmp_new", results in "tmp_diff".
Bram Moolenaare828b762018-09-10 17:51:58 +02001103 * return OK or FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 */
Bram Moolenaare828b762018-09-10 17:51:58 +02001105 static int
1106diff_file(diffio_T *dio)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001107{
1108 char_u *cmd;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001109 size_t len;
Bram Moolenaare828b762018-09-10 17:51:58 +02001110 char_u *tmp_orig = dio->dio_orig.din_fname;
1111 char_u *tmp_new = dio->dio_new.din_fname;
1112 char_u *tmp_diff = dio->dio_diff.dout_fname;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113
1114#ifdef FEAT_EVAL
1115 if (*p_dex != NUL)
Bram Moolenaare828b762018-09-10 17:51:58 +02001116 {
1117 // Use 'diffexpr' to generate the diff file.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118 eval_diff(tmp_orig, tmp_new, tmp_diff);
Bram Moolenaare828b762018-09-10 17:51:58 +02001119 return OK;
1120 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001121 else
1122#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001123 // Use xdiff for generating the diff.
1124 if (dio->dio_internal)
1125 {
1126 return diff_file_internal(dio);
1127 }
1128 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001130 len = STRLEN(tmp_orig) + STRLEN(tmp_new)
1131 + STRLEN(tmp_diff) + STRLEN(p_srr) + 27;
1132 cmd = alloc((unsigned)len);
Bram Moolenaare828b762018-09-10 17:51:58 +02001133 if (cmd == NULL)
1134 return FAIL;
Bram Moolenaar95fb60a2005-03-08 22:29:20 +00001135
Bram Moolenaare828b762018-09-10 17:51:58 +02001136 // We don't want $DIFF_OPTIONS to get in the way.
1137 if (getenv("DIFF_OPTIONS"))
1138 vim_setenv((char_u *)"DIFF_OPTIONS", (char_u *)"");
1139
1140 // Build the diff command and execute it. Always use -a, binary
1141 // differences are of no use. Ignore errors, diff returns
1142 // non-zero when differences have been found.
Bram Moolenaar785fc652018-09-15 19:17:38 +02001143 vim_snprintf((char *)cmd, len, "diff %s%s%s%s%s%s%s%s %s",
Bram Moolenaare828b762018-09-10 17:51:58 +02001144 diff_a_works == FALSE ? "" : "-a ",
Bram Moolenaar48e330a2016-02-23 14:53:34 +01001145#if defined(MSWIN)
Bram Moolenaare828b762018-09-10 17:51:58 +02001146 diff_bin_works == TRUE ? "--binary " : "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001147#else
Bram Moolenaare828b762018-09-10 17:51:58 +02001148 "",
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149#endif
Bram Moolenaare828b762018-09-10 17:51:58 +02001150 (diff_flags & DIFF_IWHITE) ? "-b " : "",
Bram Moolenaar785fc652018-09-15 19:17:38 +02001151 (diff_flags & DIFF_IWHITEALL) ? "-w " : "",
1152 (diff_flags & DIFF_IWHITEEOL) ? "-Z " : "",
1153 (diff_flags & DIFF_IBLANK) ? "-B " : "",
Bram Moolenaare828b762018-09-10 17:51:58 +02001154 (diff_flags & DIFF_ICASE) ? "-i " : "",
1155 tmp_orig, tmp_new);
1156 append_redir(cmd, (int)len, p_srr, tmp_diff);
1157 block_autocmds(); // avoid ShellCmdPost stuff
1158 (void)call_shell(cmd, SHELL_FILTER|SHELL_SILENT|SHELL_DOOUT);
1159 unblock_autocmds();
1160 vim_free(cmd);
1161 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 }
1163}
1164
1165/*
1166 * Create a new version of a file from the current buffer and a diff file.
1167 * The buffer is written to a file, also for unmodified buffers (the file
1168 * could have been produced by autocommands, e.g. the netrw plugin).
1169 */
1170 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001171ex_diffpatch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172{
1173 char_u *tmp_orig; /* name of original temp file */
1174 char_u *tmp_new; /* name of patched temp file */
1175 char_u *buf = NULL;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001176 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 win_T *old_curwin = curwin;
1178 char_u *newname = NULL; /* name of patched file buffer */
1179#ifdef UNIX
1180 char_u dirbuf[MAXPATHL];
1181 char_u *fullname = NULL;
1182#endif
1183#ifdef FEAT_BROWSE
1184 char_u *browseFile = NULL;
1185 int browse_flag = cmdmod.browse;
1186#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +02001187 stat_T st;
Bram Moolenaara95ab322017-03-11 19:21:53 +01001188 char_u *esc_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189
1190#ifdef FEAT_BROWSE
1191 if (cmdmod.browse)
1192 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +00001193 browseFile = do_browse(0, (char_u *)_("Patch file"),
Bram Moolenaarc36651b2018-04-29 12:22:56 +02001194 eap->arg, NULL, NULL,
1195 (char_u *)_(BROWSE_FILTER_ALL_FILES), NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 if (browseFile == NULL)
1197 return; /* operation cancelled */
1198 eap->arg = browseFile;
1199 cmdmod.browse = FALSE; /* don't let do_ecmd() browse again */
1200 }
1201#endif
1202
1203 /* We need two temp file names. */
Bram Moolenaare5c421c2015-03-31 13:33:08 +02001204 tmp_orig = vim_tempname('o', FALSE);
1205 tmp_new = vim_tempname('n', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 if (tmp_orig == NULL || tmp_new == NULL)
1207 goto theend;
1208
1209 /* Write the current buffer to "tmp_orig". */
1210 if (buf_write(curbuf, tmp_orig, NULL,
1211 (linenr_T)1, curbuf->b_ml.ml_line_count,
1212 NULL, FALSE, FALSE, FALSE, TRUE) == FAIL)
1213 goto theend;
1214
1215#ifdef UNIX
1216 /* Get the absolute path of the patchfile, changing directory below. */
1217 fullname = FullName_save(eap->arg, FALSE);
1218#endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001219 esc_name = vim_strsave_shellescape(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220# ifdef UNIX
Bram Moolenaara95ab322017-03-11 19:21:53 +01001221 fullname != NULL ? fullname :
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222# endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001223 eap->arg, TRUE, TRUE);
1224 if (esc_name == NULL)
1225 goto theend;
1226 buflen = STRLEN(tmp_orig) + STRLEN(esc_name) + STRLEN(tmp_new) + 16;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00001227 buf = alloc((unsigned)buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 if (buf == NULL)
1229 goto theend;
1230
1231#ifdef UNIX
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +00001232 /* Temporarily chdir to /tmp, to avoid patching files in the current
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 * directory when the patch file contains more than one patch. When we
1234 * have our own temp dir use that instead, it will be cleaned up when we
1235 * exit (any .rej files created). Don't change directory if we can't
1236 * return to the current. */
1237 if (mch_dirname(dirbuf, MAXPATHL) != OK || mch_chdir((char *)dirbuf) != 0)
1238 dirbuf[0] = NUL;
1239 else
1240 {
1241# ifdef TEMPDIRNAMES
1242 if (vim_tempdir != NULL)
Bram Moolenaar42335f52018-09-13 15:33:43 +02001243 vim_ignored = mch_chdir((char *)vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 else
1245# endif
Bram Moolenaar42335f52018-09-13 15:33:43 +02001246 vim_ignored = mch_chdir("/tmp");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 shorten_fnames(TRUE);
1248 }
1249#endif
1250
1251#ifdef FEAT_EVAL
1252 if (*p_pex != NUL)
1253 /* Use 'patchexpr' to generate the new file. */
1254 eval_patch(tmp_orig,
1255# ifdef UNIX
1256 fullname != NULL ? fullname :
1257# endif
1258 eap->arg, tmp_new);
1259 else
1260#endif
1261 {
1262 /* Build the patch command and execute it. Ignore errors. Switch to
1263 * cooked mode to allow the user to respond to prompts. */
Bram Moolenaara95ab322017-03-11 19:21:53 +01001264 vim_snprintf((char *)buf, buflen, "patch -o %s %s < %s",
1265 tmp_new, tmp_orig, esc_name);
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001266 block_autocmds(); /* Avoid ShellCmdPost stuff */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 (void)call_shell(buf, SHELL_FILTER | SHELL_COOKED);
Bram Moolenaar78ab3312007-09-29 12:16:41 +00001268 unblock_autocmds();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 }
1270
1271#ifdef UNIX
1272 if (dirbuf[0] != NUL)
1273 {
1274 if (mch_chdir((char *)dirbuf) != 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001275 emsg(_(e_prev_dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001276 shorten_fnames(TRUE);
1277 }
1278#endif
1279
1280 /* patch probably has written over the screen */
1281 redraw_later(CLEAR);
1282
1283 /* Delete any .orig or .rej file created. */
1284 STRCPY(buf, tmp_new);
1285 STRCAT(buf, ".orig");
1286 mch_remove(buf);
1287 STRCPY(buf, tmp_new);
1288 STRCAT(buf, ".rej");
1289 mch_remove(buf);
1290
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001291 /* Only continue if the output file was created. */
1292 if (mch_stat((char *)tmp_new, &st) < 0 || st.st_size == 0)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001293 emsg(_("E816: Cannot read patch output"));
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001294 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001296 if (curbuf->b_fname != NULL)
1297 {
1298 newname = vim_strnsave(curbuf->b_fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 (int)(STRLEN(curbuf->b_fname) + 4));
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001300 if (newname != NULL)
1301 STRCAT(newname, ".new");
1302 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303
1304#ifdef FEAT_GUI
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001305 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306#endif
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001307 /* don't use a new tab page, each tab page has its own diffs */
1308 cmdmod.tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001309
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001310 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001312 /* Pretend it was a ":split fname" command */
1313 eap->cmdidx = CMD_split;
1314 eap->arg = tmp_new;
1315 do_exedit(eap, old_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001317 /* check that split worked and editing tmp_new */
1318 if (curwin != old_curwin && win_valid(old_curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001320 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1321 diff_win_options(curwin, TRUE);
1322 diff_win_options(old_curwin, TRUE);
1323
1324 if (newname != NULL)
1325 {
1326 /* do a ":file filename.new" on the patched buffer */
1327 eap->arg = newname;
1328 ex_file(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001330 /* Do filetype detection with the new name. */
1331 if (au_has_group((char_u *)"filetypedetect"))
1332 do_cmdline_cmd((char_u *)":doau filetypedetect BufRead");
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 }
1335 }
1336 }
1337
1338theend:
1339 if (tmp_orig != NULL)
1340 mch_remove(tmp_orig);
1341 vim_free(tmp_orig);
1342 if (tmp_new != NULL)
1343 mch_remove(tmp_new);
1344 vim_free(tmp_new);
1345 vim_free(newname);
1346 vim_free(buf);
1347#ifdef UNIX
1348 vim_free(fullname);
1349#endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001350 vim_free(esc_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351#ifdef FEAT_BROWSE
1352 vim_free(browseFile);
1353 cmdmod.browse = browse_flag;
1354#endif
1355}
1356
1357/*
1358 * Split the window and edit another file, setting options to show the diffs.
1359 */
1360 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001361ex_diffsplit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362{
1363 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001364 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001366 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367#ifdef FEAT_GUI
1368 need_mouse_correct = TRUE;
1369#endif
Bram Moolenaar46328f92016-08-28 15:39:57 +02001370 /* Need to compute w_fraction when no redraw happened yet. */
1371 validate_cursor();
1372 set_fraction(curwin);
1373
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001374 /* don't use a new tab page, each tab page has its own diffs */
1375 cmdmod.tab = 0;
1376
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001377 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 {
1379 /* Pretend it was a ":split fname" command */
1380 eap->cmdidx = CMD_split;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001381 curwin->w_p_diff = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 do_exedit(eap, old_curwin);
1383
1384 if (curwin != old_curwin) /* split must have worked */
1385 {
1386 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1387 diff_win_options(curwin, TRUE);
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001388 if (win_valid(old_curwin))
1389 {
1390 diff_win_options(old_curwin, TRUE);
1391
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001392 if (bufref_valid(&old_curbuf))
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001393 /* Move the cursor position to that of the old window. */
1394 curwin->w_cursor.lnum = diff_get_corresponding_line(
Bram Moolenaar025e3e02016-10-18 14:50:18 +02001395 old_curbuf.br_buf, old_curwin->w_cursor.lnum);
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001396 }
Bram Moolenaar46328f92016-08-28 15:39:57 +02001397 /* Now that lines are folded scroll to show the cursor at the same
1398 * relative position. */
1399 scroll_to_fraction(curwin, curwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 }
1401 }
1402}
1403
1404/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001405 * Set options to show diffs for the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001408ex_diffthis(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409{
1410 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1411 diff_win_options(curwin, TRUE);
1412}
1413
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001414 static void
1415set_diff_option(win_T *wp, int value)
1416{
1417 win_T *old_curwin = curwin;
1418
1419 curwin = wp;
1420 curbuf = curwin->w_buffer;
1421 ++curbuf_lock;
1422 set_option_value((char_u *)"diff", (long)value, NULL, OPT_LOCAL);
1423 --curbuf_lock;
1424 curwin = old_curwin;
1425 curbuf = curwin->w_buffer;
1426}
1427
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428/*
1429 * Set options in window "wp" for diff mode.
1430 */
1431 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001432diff_win_options(
1433 win_T *wp,
1434 int addbuf) /* Add buffer to diff. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435{
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001436# ifdef FEAT_FOLDING
1437 win_T *old_curwin = curwin;
1438
1439 /* close the manually opened folds */
1440 curwin = wp;
1441 newFoldLevel();
1442 curwin = old_curwin;
1443# endif
1444
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001445 /* Use 'scrollbind' and 'cursorbind' when available */
Bram Moolenaar43929962015-07-03 15:06:56 +02001446 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001447 wp->w_p_scb_save = wp->w_p_scb;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001448 wp->w_p_scb = TRUE;
Bram Moolenaar43929962015-07-03 15:06:56 +02001449 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001450 wp->w_p_crb_save = wp->w_p_crb;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001451 wp->w_p_crb = TRUE;
Bram Moolenaar43929962015-07-03 15:06:56 +02001452 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001453 wp->w_p_wrap_save = wp->w_p_wrap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 wp->w_p_wrap = FALSE;
1455# ifdef FEAT_FOLDING
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001456 curwin = wp;
1457 curbuf = curwin->w_buffer;
Bram Moolenaar43929962015-07-03 15:06:56 +02001458 if (!wp->w_p_diff)
1459 {
1460 if (wp->w_p_diff_saved)
1461 free_string_option(wp->w_p_fdm_save);
Bram Moolenaara87aa802013-07-03 15:47:03 +02001462 wp->w_p_fdm_save = vim_strsave(wp->w_p_fdm);
Bram Moolenaar43929962015-07-03 15:06:56 +02001463 }
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001464 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"diff",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001465 OPT_LOCAL|OPT_FREE, 0);
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001466 curwin = old_curwin;
1467 curbuf = curwin->w_buffer;
Bram Moolenaar43929962015-07-03 15:06:56 +02001468 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001469 {
1470 wp->w_p_fdc_save = wp->w_p_fdc;
1471 wp->w_p_fen_save = wp->w_p_fen;
1472 wp->w_p_fdl_save = wp->w_p_fdl;
1473 }
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001474 wp->w_p_fdc = diff_foldcolumn;
1475 wp->w_p_fen = TRUE;
1476 wp->w_p_fdl = 0;
1477 foldUpdateAll(wp);
1478 /* make sure topline is not halfway a fold */
1479 changed_window_setting_win(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if (vim_strchr(p_sbo, 'h') == NULL)
1482 do_cmdline_cmd((char_u *)"set sbo+=hor");
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001483 /* Save the current values, to be restored in ex_diffoff(). */
Bram Moolenaara87aa802013-07-03 15:47:03 +02001484 wp->w_p_diff_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001486 set_diff_option(wp, TRUE);
Bram Moolenaar43929962015-07-03 15:06:56 +02001487
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488 if (addbuf)
1489 diff_buf_add(wp->w_buffer);
1490 redraw_win_later(wp, NOT_VALID);
1491}
1492
1493/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001494 * Set options not to show diffs. For the current window or all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001495 * Only in the current tab page.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001496 */
1497 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001498ex_diffoff(exarg_T *eap)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001499{
1500 win_T *wp;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001501 int diffwin = FALSE;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001502
Bram Moolenaar29323592016-07-24 22:04:11 +02001503 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001504 {
Bram Moolenaar00462ff2013-09-20 20:13:53 +02001505 if (eap->forceit ? wp->w_p_diff : wp == curwin)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001506 {
Bram Moolenaar43929962015-07-03 15:06:56 +02001507 /* Set 'diff' off. If option values were saved in
1508 * diff_win_options(), restore the ones whose settings seem to have
1509 * been left over from diff mode. */
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001510 set_diff_option(wp, FALSE);
Bram Moolenaara87aa802013-07-03 15:47:03 +02001511
Bram Moolenaara87aa802013-07-03 15:47:03 +02001512 if (wp->w_p_diff_saved)
1513 {
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001514
Bram Moolenaar43929962015-07-03 15:06:56 +02001515 if (wp->w_p_scb)
1516 wp->w_p_scb = wp->w_p_scb_save;
Bram Moolenaar43929962015-07-03 15:06:56 +02001517 if (wp->w_p_crb)
1518 wp->w_p_crb = wp->w_p_crb_save;
Bram Moolenaar43929962015-07-03 15:06:56 +02001519 if (!wp->w_p_wrap)
1520 wp->w_p_wrap = wp->w_p_wrap_save;
1521#ifdef FEAT_FOLDING
1522 free_string_option(wp->w_p_fdm);
Bram Moolenaar79a213d2017-05-16 13:15:18 +02001523 wp->w_p_fdm = vim_strsave(
1524 *wp->w_p_fdm_save ? wp->w_p_fdm_save : (char_u*)"manual");
Bram Moolenaar43929962015-07-03 15:06:56 +02001525
1526 if (wp->w_p_fdc == diff_foldcolumn)
1527 wp->w_p_fdc = wp->w_p_fdc_save;
1528 if (wp->w_p_fdl == 0)
1529 wp->w_p_fdl = wp->w_p_fdl_save;
1530
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001531 /* Only restore 'foldenable' when 'foldmethod' is not
1532 * "manual", otherwise we continue to show the diff folds. */
Bram Moolenaar43929962015-07-03 15:06:56 +02001533 if (wp->w_p_fen)
1534 wp->w_p_fen = foldmethodIsManual(wp) ? FALSE
1535 : wp->w_p_fen_save;
1536
1537 foldUpdateAll(wp);
Bram Moolenaar43929962015-07-03 15:06:56 +02001538#endif
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001539 }
Bram Moolenaare67d5462016-08-27 22:40:42 +02001540 /* remove filler lines */
1541 wp->w_topfill = 0;
1542
1543 /* make sure topline is not halfway a fold and cursor is
1544 * invalidated */
1545 changed_window_setting_win(wp);
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001546
Bram Moolenaara87aa802013-07-03 15:47:03 +02001547 /* Note: 'sbo' is not restored, it's a global option. */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001548 diff_buf_adjust(wp);
1549 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001550 diffwin |= wp->w_p_diff;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001551 }
1552
Bram Moolenaar25ea0542017-02-03 23:16:28 +01001553 /* Also remove hidden buffers from the list. */
1554 if (eap->forceit)
1555 diff_buf_clear();
1556
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001557 /* Remove "hor" from from 'scrollopt' if there are no diff windows left. */
1558 if (!diffwin && vim_strchr(p_sbo, 'h') != NULL)
1559 do_cmdline_cmd((char_u *)"set sbo-=hor");
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001560}
1561
1562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001563 * Read the diff output and add each entry to the diff list.
1564 */
1565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001566diff_read(
Bram Moolenaare828b762018-09-10 17:51:58 +02001567 int idx_orig, // idx of original file
1568 int idx_new, // idx of new file
1569 diffout_T *dout) // diff output
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570{
Bram Moolenaare828b762018-09-10 17:51:58 +02001571 FILE *fd = NULL;
1572 int line_idx = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001573 diff_T *dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001574 diff_T *dp = curtab->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001575 diff_T *dn, *dpl;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576 char_u linebuf[LBUFLEN]; /* only need to hold the diff line */
Bram Moolenaare828b762018-09-10 17:51:58 +02001577 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001578 long off;
1579 int i;
1580 linenr_T lnum_orig, lnum_new;
1581 long count_orig, count_new;
1582 int notset = TRUE; /* block "*dp" not set yet */
Bram Moolenaare828b762018-09-10 17:51:58 +02001583 enum {
1584 DIFF_ED,
1585 DIFF_UNIFIED,
1586 DIFF_NONE
1587 } diffstyle = DIFF_NONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001588
Bram Moolenaare828b762018-09-10 17:51:58 +02001589 if (dout->dout_fname == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001590 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001591 diffstyle = DIFF_UNIFIED;
1592 }
1593 else
1594 {
1595 fd = mch_fopen((char *)dout->dout_fname, "r");
1596 if (fd == NULL)
1597 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001598 emsg(_("E98: Cannot read diff output"));
Bram Moolenaare828b762018-09-10 17:51:58 +02001599 return;
1600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001601 }
1602
1603 for (;;)
1604 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001605 if (fd == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001607 if (line_idx >= dout->dout_ga.ga_len)
1608 break; // did last line
1609 line = ((char_u **)dout->dout_ga.ga_data)[line_idx++];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 }
1611 else
1612 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001613 if (tag_fgets(linebuf, LBUFLEN, fd))
1614 break; // end of file
1615 line = linebuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616 }
1617
Bram Moolenaare828b762018-09-10 17:51:58 +02001618 if (diffstyle == DIFF_NONE)
1619 {
1620 // Determine diff style.
1621 // ed like diff looks like this:
1622 // {first}[,{last}]c{first}[,{last}]
1623 // {first}a{first}[,{last}]
1624 // {first}[,{last}]d{first}
1625 //
1626 // unified diff looks like this:
1627 // --- file1 2018-03-20 13:23:35.783153140 +0100
1628 // +++ file2 2018-03-20 13:23:41.183156066 +0100
1629 // @@ -1,3 +1,5 @@
1630 if (isdigit(*line))
1631 diffstyle = DIFF_ED;
1632 else if ((STRNCMP(line, "@@ ", 3) == 0))
1633 diffstyle = DIFF_UNIFIED;
1634 else if ((STRNCMP(line, "--- ", 4) == 0)
1635 && (tag_fgets(linebuf, LBUFLEN, fd) == 0)
1636 && (STRNCMP(line, "+++ ", 4) == 0)
1637 && (tag_fgets(linebuf, LBUFLEN, fd) == 0)
1638 && (STRNCMP(line, "@@ ", 3) == 0))
1639 diffstyle = DIFF_UNIFIED;
Bram Moolenaar3b8defd2018-09-13 13:03:11 +02001640 else
1641 // Format not recognized yet, skip over this line. Cygwin diff
1642 // may put a warning at the start of the file.
1643 continue;
Bram Moolenaare828b762018-09-10 17:51:58 +02001644 }
1645
1646 if (diffstyle == DIFF_ED)
1647 {
1648 if (!isdigit(*line))
1649 continue; // not the start of a diff block
1650 if (parse_diff_ed(line, &lnum_orig, &count_orig,
1651 &lnum_new, &count_new) == FAIL)
1652 continue;
1653 }
1654 else if (diffstyle == DIFF_UNIFIED)
1655 {
1656 if (STRNCMP(line, "@@ ", 3) != 0)
1657 continue; // not the start of a diff block
1658 if (parse_diff_unified(line, &lnum_orig, &count_orig,
1659 &lnum_new, &count_new) == FAIL)
1660 continue;
1661 }
1662 else
1663 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01001664 emsg(_("E959: Invalid diff format."));
Bram Moolenaare828b762018-09-10 17:51:58 +02001665 break;
1666 }
1667
1668 // Go over blocks before the change, for which orig and new are equal.
1669 // Copy blocks from orig to new.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 while (dp != NULL
1671 && lnum_orig > dp->df_lnum[idx_orig] + dp->df_count[idx_orig])
1672 {
1673 if (notset)
1674 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1675 dprev = dp;
1676 dp = dp->df_next;
1677 notset = TRUE;
1678 }
1679
1680 if (dp != NULL
1681 && lnum_orig <= dp->df_lnum[idx_orig] + dp->df_count[idx_orig]
1682 && lnum_orig + count_orig >= dp->df_lnum[idx_orig])
1683 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001684 // New block overlaps with existing block(s).
1685 // First find last block that overlaps.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 for (dpl = dp; dpl->df_next != NULL; dpl = dpl->df_next)
1687 if (lnum_orig + count_orig < dpl->df_next->df_lnum[idx_orig])
1688 break;
1689
Bram Moolenaare828b762018-09-10 17:51:58 +02001690 // If the newly found block starts before the old one, set the
1691 // start back a number of lines.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692 off = dp->df_lnum[idx_orig] - lnum_orig;
1693 if (off > 0)
1694 {
1695 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001696 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001697 dp->df_lnum[i] -= off;
1698 dp->df_lnum[idx_new] = lnum_new;
1699 dp->df_count[idx_new] = count_new;
1700 }
1701 else if (notset)
1702 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001703 // new block inside existing one, adjust new block
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704 dp->df_lnum[idx_new] = lnum_new + off;
1705 dp->df_count[idx_new] = count_new - off;
1706 }
1707 else
Bram Moolenaare828b762018-09-10 17:51:58 +02001708 // second overlap of new block with existing block
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001709 dp->df_count[idx_new] += count_new - count_orig
1710 + dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]
1711 - (dp->df_lnum[idx_orig] + dp->df_count[idx_orig]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001712
Bram Moolenaare828b762018-09-10 17:51:58 +02001713 // Adjust the size of the block to include all the lines to the
1714 // end of the existing block or the new diff, whatever ends last.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001715 off = (lnum_orig + count_orig)
1716 - (dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]);
1717 if (off < 0)
1718 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001719 // new change ends in existing block, adjust the end if not
1720 // done already
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721 if (notset)
1722 dp->df_count[idx_new] += -off;
1723 off = 0;
1724 }
Bram Moolenaard4b96bc2007-10-19 15:33:39 +00001725 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001726 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727 dp->df_count[i] = dpl->df_lnum[i] + dpl->df_count[i]
1728 - dp->df_lnum[i] + off;
1729
Bram Moolenaare828b762018-09-10 17:51:58 +02001730 // Delete the diff blocks that have been merged into one.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001731 dn = dp->df_next;
1732 dp->df_next = dpl->df_next;
1733 while (dn != dp->df_next)
1734 {
1735 dpl = dn->df_next;
1736 vim_free(dn);
1737 dn = dpl;
1738 }
1739 }
1740 else
1741 {
Bram Moolenaare828b762018-09-10 17:51:58 +02001742 // Allocate a new diffblock.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001743 dp = diff_alloc_new(curtab, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001744 if (dp == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001745 goto done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001746
1747 dp->df_lnum[idx_orig] = lnum_orig;
1748 dp->df_count[idx_orig] = count_orig;
1749 dp->df_lnum[idx_new] = lnum_new;
1750 dp->df_count[idx_new] = count_new;
1751
Bram Moolenaare828b762018-09-10 17:51:58 +02001752 // Set values for other buffers, these must be equal to the
1753 // original buffer, otherwise there would have been a change
1754 // already.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 for (i = idx_orig + 1; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001756 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001757 diff_copy_entry(dprev, dp, idx_orig, i);
1758 }
Bram Moolenaare828b762018-09-10 17:51:58 +02001759 notset = FALSE; // "*dp" has been set
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
1761
Bram Moolenaare828b762018-09-10 17:51:58 +02001762 // for remaining diff blocks orig and new are equal
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 while (dp != NULL)
1764 {
1765 if (notset)
1766 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1767 dprev = dp;
1768 dp = dp->df_next;
1769 notset = TRUE;
1770 }
1771
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001772done:
Bram Moolenaare828b762018-09-10 17:51:58 +02001773 if (fd != NULL)
1774 fclose(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775}
1776
1777/*
1778 * Copy an entry at "dp" from "idx_orig" to "idx_new".
1779 */
1780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001781diff_copy_entry(
1782 diff_T *dprev,
1783 diff_T *dp,
1784 int idx_orig,
1785 int idx_new)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
1787 long off;
1788
1789 if (dprev == NULL)
1790 off = 0;
1791 else
1792 off = (dprev->df_lnum[idx_orig] + dprev->df_count[idx_orig])
1793 - (dprev->df_lnum[idx_new] + dprev->df_count[idx_new]);
1794 dp->df_lnum[idx_new] = dp->df_lnum[idx_orig] - off;
1795 dp->df_count[idx_new] = dp->df_count[idx_orig];
1796}
1797
1798/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001799 * Clear the list of diffblocks for tab page "tp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800 */
Bram Moolenaarea408852005-06-25 22:49:46 +00001801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001802diff_clear(tabpage_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803{
1804 diff_T *p, *next_p;
1805
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001806 for (p = tp->tp_first_diff; p != NULL; p = next_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001807 {
1808 next_p = p->df_next;
1809 vim_free(p);
1810 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001811 tp->tp_first_diff = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001812}
1813
1814/*
1815 * Check diff status for line "lnum" in buffer "buf":
1816 * Returns 0 for nothing special
1817 * Returns -1 for a line that should be highlighted as changed.
1818 * Returns -2 for a line that should be highlighted as added/deleted.
1819 * Returns > 0 for inserting that many filler lines above it (never happens
1820 * when 'diffopt' doesn't contain "filler").
1821 * This should only be used for windows where 'diff' is set.
1822 */
1823 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001824diff_check(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001826 int idx; /* index in tp_diffbuf[] for this buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 diff_T *dp;
1828 int maxcount;
1829 int i;
1830 buf_T *buf = wp->w_buffer;
1831 int cmp;
1832
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001833 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 ex_diffupdate(NULL); /* update after a big change */
1835
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001836 if (curtab->tp_first_diff == NULL || !wp->w_p_diff) /* no diffs at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 return 0;
1838
1839 /* safety check: "lnum" must be a buffer line */
1840 if (lnum < 1 || lnum > buf->b_ml.ml_line_count + 1)
1841 return 0;
1842
1843 idx = diff_buf_idx(buf);
1844 if (idx == DB_COUNT)
1845 return 0; /* no diffs for buffer "buf" */
1846
1847#ifdef FEAT_FOLDING
1848 /* A closed fold never has filler lines. */
1849 if (hasFoldingWin(wp, lnum, NULL, NULL, TRUE, NULL))
1850 return 0;
1851#endif
1852
1853 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001854 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
1856 break;
1857 if (dp == NULL || lnum < dp->df_lnum[idx])
1858 return 0;
1859
1860 if (lnum < dp->df_lnum[idx] + dp->df_count[idx])
1861 {
1862 int zero = FALSE;
1863
1864 /* Changed or inserted line. If the other buffers have a count of
1865 * zero, the lines were inserted. If the other buffers have the same
1866 * count, check if the lines are identical. */
1867 cmp = FALSE;
1868 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001869 if (i != idx && curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 {
1871 if (dp->df_count[i] == 0)
1872 zero = TRUE;
1873 else
1874 {
1875 if (dp->df_count[i] != dp->df_count[idx])
1876 return -1; /* nr of lines changed. */
1877 cmp = TRUE;
1878 }
1879 }
1880 if (cmp)
1881 {
1882 /* Compare all lines. If they are equal the lines were inserted
1883 * in some buffers, deleted in others, but not changed. */
1884 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001885 if (i != idx && curtab->tp_diffbuf[i] != NULL
1886 && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001887 if (!diff_equal_entry(dp, idx, i))
1888 return -1;
1889 }
1890 /* If there is no buffer with zero lines then there is no difference
1891 * any longer. Happens when making a change (or undo) that removes
1892 * the difference. Can't remove the entry here, we might be halfway
1893 * updating the window. Just report the text as unchanged. Other
1894 * windows might still show the change though. */
1895 if (zero == FALSE)
1896 return 0;
1897 return -2;
1898 }
1899
1900 /* If 'diffopt' doesn't contain "filler", return 0. */
1901 if (!(diff_flags & DIFF_FILLER))
1902 return 0;
1903
1904 /* Insert filler lines above the line just below the change. Will return
1905 * 0 when this buf had the max count. */
1906 maxcount = 0;
1907 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001908 if (curtab->tp_diffbuf[i] != NULL && dp->df_count[i] > maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 maxcount = dp->df_count[i];
1910 return maxcount - dp->df_count[idx];
1911}
1912
1913/*
1914 * Compare two entries in diff "*dp" and return TRUE if they are equal.
1915 */
1916 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001917diff_equal_entry(diff_T *dp, int idx1, int idx2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918{
1919 int i;
1920 char_u *line;
1921 int cmp;
1922
1923 if (dp->df_count[idx1] != dp->df_count[idx2])
1924 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001925 if (diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926 return FALSE;
1927 for (i = 0; i < dp->df_count[idx1]; ++i)
1928 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001929 line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 dp->df_lnum[idx1] + i, FALSE));
1931 if (line == NULL)
1932 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001933 cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 dp->df_lnum[idx2] + i, FALSE));
1935 vim_free(line);
1936 if (cmp != 0)
1937 return FALSE;
1938 }
1939 return TRUE;
1940}
1941
1942/*
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02001943 * Compare the characters at "p1" and "p2". If they are equal (possibly
1944 * ignoring case) return TRUE and set "len" to the number of bytes.
1945 */
1946 static int
1947diff_equal_char(char_u *p1, char_u *p2, int *len)
1948{
1949#ifdef FEAT_MBYTE
1950 int l = (*mb_ptr2len)(p1);
1951
1952 if (l != (*mb_ptr2len)(p2))
1953 return FALSE;
1954 if (l > 1)
1955 {
1956 if (STRNCMP(p1, p2, l) != 0
1957 && (!enc_utf8
1958 || !(diff_flags & DIFF_ICASE)
1959 || utf_fold(utf_ptr2char(p1))
1960 != utf_fold(utf_ptr2char(p2))))
1961 return FALSE;
1962 *len = l;
1963 }
1964 else
1965#endif
1966 {
1967 if ((*p1 != *p2)
1968 && (!(diff_flags & DIFF_ICASE)
1969 || TOLOWER_LOC(*p1) != TOLOWER_LOC(*p2)))
1970 return FALSE;
1971 *len = 1;
1972 }
1973 return TRUE;
1974}
1975
1976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001977 * Compare strings "s1" and "s2" according to 'diffopt'.
1978 * Return non-zero when they are different.
1979 */
1980 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001981diff_cmp(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001982{
1983 char_u *p1, *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985
Bram Moolenaar785fc652018-09-15 19:17:38 +02001986 if ((diff_flags & DIFF_IBLANK)
1987 && (*skipwhite(s1) == NUL || *skipwhite(s2) == NUL))
1988 return 0;
1989
1990 if ((diff_flags & (DIFF_ICASE | ALL_WHITE_DIFF)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 return STRCMP(s1, s2);
Bram Moolenaar785fc652018-09-15 19:17:38 +02001992 if ((diff_flags & DIFF_ICASE) && !(diff_flags & ALL_WHITE_DIFF))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 return MB_STRICMP(s1, s2);
1994
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 p1 = s1;
1996 p2 = s2;
Bram Moolenaar785fc652018-09-15 19:17:38 +02001997
1998 // Ignore white space changes and possibly ignore case.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 while (*p1 != NUL && *p2 != NUL)
2000 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002001 if (((diff_flags & DIFF_IWHITE)
2002 && VIM_ISWHITE(*p1) && VIM_ISWHITE(*p2))
2003 || ((diff_flags & DIFF_IWHITEALL)
2004 && (VIM_ISWHITE(*p1) || VIM_ISWHITE(*p2))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 {
2006 p1 = skipwhite(p1);
2007 p2 = skipwhite(p2);
2008 }
2009 else
2010 {
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002011 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 break;
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02002013 p1 += l;
2014 p2 += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 }
2016 }
2017
Bram Moolenaar785fc652018-09-15 19:17:38 +02002018 // Ignore trailing white space.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 p1 = skipwhite(p1);
2020 p2 = skipwhite(p2);
2021 if (*p1 != NUL || *p2 != NUL)
2022 return 1;
2023 return 0;
2024}
2025
2026/*
2027 * Return the number of filler lines above "lnum".
2028 */
2029 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002030diff_check_fill(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002031{
2032 int n;
2033
2034 /* be quick when there are no filler lines */
2035 if (!(diff_flags & DIFF_FILLER))
2036 return 0;
2037 n = diff_check(wp, lnum);
2038 if (n <= 0)
2039 return 0;
2040 return n;
2041}
2042
2043/*
2044 * Set the topline of "towin" to match the position in "fromwin", so that they
2045 * show the same diff'ed lines.
2046 */
2047 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002048diff_set_topline(win_T *fromwin, win_T *towin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049{
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002050 buf_T *frombuf = fromwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002051 linenr_T lnum = fromwin->w_topline;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002052 int fromidx;
2053 int toidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 diff_T *dp;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002055 int max_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 int i;
2057
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002058 fromidx = diff_buf_idx(frombuf);
2059 if (fromidx == DB_COUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002060 return; /* safety check */
2061
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002062 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 ex_diffupdate(NULL); /* update after a big change */
2064
2065 towin->w_topfill = 0;
2066
2067 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002068 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002069 if (lnum <= dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 break;
2071 if (dp == NULL)
2072 {
2073 /* After last change, compute topline relative to end of file; no
2074 * filler lines. */
2075 towin->w_topline = towin->w_buffer->b_ml.ml_line_count
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002076 - (frombuf->b_ml.ml_line_count - lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002077 }
2078 else
2079 {
2080 /* Find index for "towin". */
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002081 toidx = diff_buf_idx(towin->w_buffer);
2082 if (toidx == DB_COUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002083 return; /* safety check */
2084
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002085 towin->w_topline = lnum + (dp->df_lnum[toidx] - dp->df_lnum[fromidx]);
2086 if (lnum >= dp->df_lnum[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002087 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002088 /* Inside a change: compute filler lines. With three or more
2089 * buffers we need to know the largest count. */
2090 max_count = 0;
2091 for (i = 0; i < DB_COUNT; ++i)
2092 if (curtab->tp_diffbuf[i] != NULL
2093 && max_count < dp->df_count[i])
2094 max_count = dp->df_count[i];
2095
2096 if (dp->df_count[toidx] == dp->df_count[fromidx])
2097 {
2098 /* same number of lines: use same filler count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 towin->w_topfill = fromwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 }
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002101 else if (dp->df_count[toidx] > dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002102 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002103 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00002105 /* more lines in towin and fromwin doesn't show diff
2106 * lines, only filler lines */
2107 if (max_count - fromwin->w_topfill >= dp->df_count[toidx])
2108 {
2109 /* towin also only shows filler lines */
2110 towin->w_topline = dp->df_lnum[toidx]
2111 + dp->df_count[toidx];
2112 towin->w_topfill = fromwin->w_topfill;
2113 }
2114 else
2115 /* towin still has some diff lines to show */
2116 towin->w_topline = dp->df_lnum[toidx]
2117 + max_count - fromwin->w_topfill;
2118 }
2119 }
2120 else if (towin->w_topline >= dp->df_lnum[toidx]
2121 + dp->df_count[toidx])
2122 {
2123 /* less lines in towin and no diff lines to show: compute
2124 * filler lines */
2125 towin->w_topline = dp->df_lnum[toidx] + dp->df_count[toidx];
2126 if (diff_flags & DIFF_FILLER)
2127 {
2128 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
2129 /* fromwin is also out of diff lines */
2130 towin->w_topfill = fromwin->w_topfill;
2131 else
2132 /* fromwin has some diff lines */
2133 towin->w_topfill = dp->df_lnum[fromidx]
2134 + max_count - lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 }
2136 }
2137 }
2138 }
2139
2140 /* safety check (if diff info gets outdated strange things may happen) */
2141 towin->w_botfill = FALSE;
2142 if (towin->w_topline > towin->w_buffer->b_ml.ml_line_count)
2143 {
2144 towin->w_topline = towin->w_buffer->b_ml.ml_line_count;
2145 towin->w_botfill = TRUE;
2146 }
2147 if (towin->w_topline < 1)
2148 {
2149 towin->w_topline = 1;
2150 towin->w_topfill = 0;
2151 }
2152
2153 /* When w_topline changes need to recompute w_botline and cursor position */
2154 invalidate_botline_win(towin);
2155 changed_line_abv_curs_win(towin);
2156
2157 check_topfill(towin, FALSE);
2158#ifdef FEAT_FOLDING
2159 (void)hasFoldingWin(towin, towin->w_topline, &towin->w_topline,
2160 NULL, TRUE, NULL);
2161#endif
2162}
2163
2164/*
2165 * This is called when 'diffopt' is changed.
2166 */
2167 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002168diffopt_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169{
2170 char_u *p;
2171 int diff_context_new = 6;
2172 int diff_flags_new = 0;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002173 int diff_foldcolumn_new = 2;
Bram Moolenaare828b762018-09-10 17:51:58 +02002174 long diff_algorithm_new = 0;
Bram Moolenaarb6fc7282018-12-04 22:24:16 +01002175 long diff_indent_heuristic = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002176 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177
2178 p = p_dip;
2179 while (*p != NUL)
2180 {
2181 if (STRNCMP(p, "filler", 6) == 0)
2182 {
2183 p += 6;
2184 diff_flags_new |= DIFF_FILLER;
2185 }
2186 else if (STRNCMP(p, "context:", 8) == 0 && VIM_ISDIGIT(p[8]))
2187 {
2188 p += 8;
2189 diff_context_new = getdigits(&p);
2190 }
Bram Moolenaar785fc652018-09-15 19:17:38 +02002191 else if (STRNCMP(p, "iblank", 6) == 0)
2192 {
2193 p += 6;
2194 diff_flags_new |= DIFF_IBLANK;
2195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 else if (STRNCMP(p, "icase", 5) == 0)
2197 {
2198 p += 5;
2199 diff_flags_new |= DIFF_ICASE;
2200 }
Bram Moolenaar785fc652018-09-15 19:17:38 +02002201 else if (STRNCMP(p, "iwhiteall", 9) == 0)
2202 {
2203 p += 9;
2204 diff_flags_new |= DIFF_IWHITEALL;
2205 }
2206 else if (STRNCMP(p, "iwhiteeol", 9) == 0)
2207 {
2208 p += 9;
2209 diff_flags_new |= DIFF_IWHITEEOL;
2210 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 else if (STRNCMP(p, "iwhite", 6) == 0)
2212 {
2213 p += 6;
2214 diff_flags_new |= DIFF_IWHITE;
2215 }
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002216 else if (STRNCMP(p, "horizontal", 10) == 0)
2217 {
2218 p += 10;
2219 diff_flags_new |= DIFF_HORIZONTAL;
2220 }
2221 else if (STRNCMP(p, "vertical", 8) == 0)
2222 {
2223 p += 8;
2224 diff_flags_new |= DIFF_VERTICAL;
2225 }
2226 else if (STRNCMP(p, "foldcolumn:", 11) == 0 && VIM_ISDIGIT(p[11]))
2227 {
2228 p += 11;
2229 diff_foldcolumn_new = getdigits(&p);
2230 }
Bram Moolenaar97ce4192017-12-01 20:35:58 +01002231 else if (STRNCMP(p, "hiddenoff", 9) == 0)
2232 {
2233 p += 9;
2234 diff_flags_new |= DIFF_HIDDEN_OFF;
2235 }
Bram Moolenaare828b762018-09-10 17:51:58 +02002236 else if (STRNCMP(p, "indent-heuristic", 16) == 0)
2237 {
2238 p += 16;
Bram Moolenaarb6fc7282018-12-04 22:24:16 +01002239 diff_indent_heuristic = XDF_INDENT_HEURISTIC;
Bram Moolenaare828b762018-09-10 17:51:58 +02002240 }
2241 else if (STRNCMP(p, "internal", 8) == 0)
2242 {
2243 p += 8;
2244 diff_flags_new |= DIFF_INTERNAL;
2245 }
2246 else if (STRNCMP(p, "algorithm:", 10) == 0)
2247 {
2248 p += 10;
2249 if (STRNCMP(p, "myers", 5) == 0)
2250 {
2251 p += 5;
2252 diff_algorithm_new = 0;
2253 }
2254 else if (STRNCMP(p, "minimal", 7) == 0)
2255 {
2256 p += 7;
2257 diff_algorithm_new = XDF_NEED_MINIMAL;
2258 }
2259 else if (STRNCMP(p, "patience", 8) == 0)
2260 {
2261 p += 8;
2262 diff_algorithm_new = XDF_PATIENCE_DIFF;
2263 }
2264 else if (STRNCMP(p, "histogram", 9) == 0)
2265 {
2266 p += 9;
2267 diff_algorithm_new = XDF_HISTOGRAM_DIFF;
2268 }
Bram Moolenaard0721052018-11-05 21:21:33 +01002269 else
2270 return FAIL;
Bram Moolenaare828b762018-09-10 17:51:58 +02002271 }
2272
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 if (*p != ',' && *p != NUL)
2274 return FAIL;
2275 if (*p == ',')
2276 ++p;
2277 }
2278
Bram Moolenaarb6fc7282018-12-04 22:24:16 +01002279 diff_algorithm_new |= diff_indent_heuristic;
2280
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002281 /* Can't have both "horizontal" and "vertical". */
2282 if ((diff_flags_new & DIFF_HORIZONTAL) && (diff_flags_new & DIFF_VERTICAL))
2283 return FAIL;
2284
Bram Moolenaar198fa062018-09-18 21:20:26 +02002285 // If flags were added or removed, or the algorithm was changed, need to
2286 // update the diff.
Bram Moolenaare828b762018-09-10 17:51:58 +02002287 if (diff_flags != diff_flags_new || diff_algorithm != diff_algorithm_new)
Bram Moolenaar29323592016-07-24 22:04:11 +02002288 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002289 tp->tp_diff_invalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002290
2291 diff_flags = diff_flags_new;
2292 diff_context = diff_context_new;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002293 diff_foldcolumn = diff_foldcolumn_new;
Bram Moolenaare828b762018-09-10 17:51:58 +02002294 diff_algorithm = diff_algorithm_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295
2296 diff_redraw(TRUE);
2297
2298 /* recompute the scroll binding with the new option value, may
2299 * remove or add filler lines */
2300 check_scrollbind((linenr_T)0, 0L);
2301
2302 return OK;
2303}
2304
2305/*
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002306 * Return TRUE if 'diffopt' contains "horizontal".
2307 */
2308 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002309diffopt_horizontal(void)
Bram Moolenaarc4675a12006-03-15 22:50:30 +00002310{
2311 return (diff_flags & DIFF_HORIZONTAL) != 0;
2312}
2313
2314/*
Bram Moolenaar97ce4192017-12-01 20:35:58 +01002315 * Return TRUE if 'diffopt' contains "hiddenoff".
2316 */
2317 int
2318diffopt_hiddenoff(void)
2319{
2320 return (diff_flags & DIFF_HIDDEN_OFF) != 0;
2321}
2322
2323/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 * Find the difference within a changed line.
2325 * Returns TRUE if the line was added, no other buffer has it.
2326 */
2327 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002328diff_find_change(
2329 win_T *wp,
2330 linenr_T lnum,
2331 int *startp, /* first char of the change */
2332 int *endp) /* last char of the change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
2334 char_u *line_org;
2335 char_u *line_new;
2336 int i;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002337 int si_org, si_new;
2338 int ei_org, ei_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 diff_T *dp;
2340 int idx;
2341 int off;
2342 int added = TRUE;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002343 char_u *p1, *p2;
2344 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345
2346 /* Make a copy of the line, the next ml_get() will invalidate it. */
2347 line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE));
2348 if (line_org == NULL)
2349 return FALSE;
2350
2351 idx = diff_buf_idx(wp->w_buffer);
2352 if (idx == DB_COUNT) /* cannot happen */
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002353 {
2354 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002356 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
2358 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002359 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
2361 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002362 if (dp == NULL || diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002363 {
2364 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367
2368 off = lnum - dp->df_lnum[idx];
2369
2370 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002371 if (curtab->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 {
2373 /* Skip lines that are not in the other change (filler lines). */
2374 if (off >= dp->df_count[i])
2375 continue;
2376 added = FALSE;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002377 line_new = ml_get_buf(curtab->tp_diffbuf[i],
2378 dp->df_lnum[i] + off, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
2380 /* Search for start of difference */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002381 si_org = si_new = 0;
2382 while (line_org[si_org] != NUL)
2383 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002384 if (((diff_flags & DIFF_IWHITE)
2385 && VIM_ISWHITE(line_org[si_org])
2386 && VIM_ISWHITE(line_new[si_new]))
2387 || ((diff_flags & DIFF_IWHITEALL)
2388 && (VIM_ISWHITE(line_org[si_org])
2389 || VIM_ISWHITE(line_new[si_new]))))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002390 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002391 si_org = (int)(skipwhite(line_org + si_org) - line_org);
2392 si_new = (int)(skipwhite(line_new + si_new) - line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002393 }
2394 else
2395 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002396 if (!diff_equal_char(line_org + si_org, line_new + si_new,
2397 &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002398 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002399 si_org += l;
2400 si_new += l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002401 }
2402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002403#ifdef FEAT_MBYTE
2404 if (has_mbyte)
2405 {
2406 /* Move back to first byte of character in both lines (may
2407 * have "nn^" in line_org and "n^ in line_new). */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002408 si_org -= (*mb_head_off)(line_org, line_org + si_org);
2409 si_new -= (*mb_head_off)(line_new, line_new + si_new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
2411#endif
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002412 if (*startp > si_org)
2413 *startp = si_org;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414
2415 /* Search for end of difference, if any. */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002416 if (line_org[si_org] != NUL || line_new[si_new] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 {
2418 ei_org = (int)STRLEN(line_org);
2419 ei_new = (int)STRLEN(line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002420 while (ei_org >= *startp && ei_new >= si_new
2421 && ei_org >= 0 && ei_new >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 {
Bram Moolenaar785fc652018-09-15 19:17:38 +02002423 if (((diff_flags & DIFF_IWHITE)
2424 && VIM_ISWHITE(line_org[ei_org])
2425 && VIM_ISWHITE(line_new[ei_new]))
2426 || ((diff_flags & DIFF_IWHITEALL)
2427 && (VIM_ISWHITE(line_org[ei_org])
2428 || VIM_ISWHITE(line_new[ei_new]))))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002429 {
2430 while (ei_org >= *startp
Bram Moolenaar1c465442017-03-12 20:10:05 +01002431 && VIM_ISWHITE(line_org[ei_org]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002432 --ei_org;
2433 while (ei_new >= si_new
Bram Moolenaar1c465442017-03-12 20:10:05 +01002434 && VIM_ISWHITE(line_new[ei_new]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002435 --ei_new;
2436 }
2437 else
2438 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002439 p1 = line_org + ei_org;
2440 p2 = line_new + ei_new;
2441#ifdef FEAT_MBYTE
2442 p1 -= (*mb_head_off)(line_org, p1);
2443 p2 -= (*mb_head_off)(line_new, p2);
2444#endif
2445 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002446 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002447 ei_org -= l;
2448 ei_new -= l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 }
2451 if (*endp < ei_org)
2452 *endp = ei_org;
2453 }
2454 }
2455
2456 vim_free(line_org);
2457 return added;
2458}
2459
2460#if defined(FEAT_FOLDING) || defined(PROTO)
2461/*
2462 * Return TRUE if line "lnum" is not close to a diff block, this line should
2463 * be in a fold.
2464 * Return FALSE if there are no diff blocks at all in this window.
2465 */
2466 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002467diff_infold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468{
2469 int i;
2470 int idx = -1;
2471 int other = FALSE;
2472 diff_T *dp;
2473
2474 /* Return if 'diff' isn't set. */
2475 if (!wp->w_p_diff)
2476 return FALSE;
2477
2478 for (i = 0; i < DB_COUNT; ++i)
2479 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002480 if (curtab->tp_diffbuf[i] == wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 idx = i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002482 else if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 other = TRUE;
2484 }
2485
2486 /* return here if there are no diffs in the window */
2487 if (idx == -1 || !other)
2488 return FALSE;
2489
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002490 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 ex_diffupdate(NULL); /* update after a big change */
2492
2493 /* Return if there are no diff blocks. All lines will be folded. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002494 if (curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 return TRUE;
2496
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002497 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 {
2499 /* If this change is below the line there can't be any further match. */
2500 if (dp->df_lnum[idx] - diff_context > lnum)
2501 break;
2502 /* If this change ends before the line we have a match. */
2503 if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum)
2504 return FALSE;
2505 }
2506 return TRUE;
2507}
2508#endif
2509
2510/*
2511 * "dp" and "do" commands.
2512 */
2513 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002514nv_diffgetput(int put, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
2516 exarg_T ea;
Bram Moolenaar6a643652014-10-31 13:54:25 +01002517 char_u buf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
Bram Moolenaarf2732452018-06-03 14:47:35 +02002519#ifdef FEAT_JOB_CHANNEL
2520 if (bt_prompt(curbuf))
2521 {
2522 vim_beep(BO_OPER);
2523 return;
2524 }
2525#endif
Bram Moolenaar6a643652014-10-31 13:54:25 +01002526 if (count == 0)
2527 ea.arg = (char_u *)"";
2528 else
2529 {
2530 vim_snprintf((char *)buf, 30, "%ld", count);
2531 ea.arg = buf;
2532 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 if (put)
2534 ea.cmdidx = CMD_diffput;
2535 else
2536 ea.cmdidx = CMD_diffget;
2537 ea.addr_count = 0;
2538 ea.line1 = curwin->w_cursor.lnum;
2539 ea.line2 = curwin->w_cursor.lnum;
2540 ex_diffgetput(&ea);
2541}
2542
2543/*
2544 * ":diffget"
2545 * ":diffput"
2546 */
2547 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002548ex_diffgetput(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549{
2550 linenr_T lnum;
2551 int count;
2552 linenr_T off = 0;
2553 diff_T *dp;
2554 diff_T *dprev;
2555 diff_T *dfree;
2556 int idx_cur;
2557 int idx_other;
2558 int idx_from;
2559 int idx_to;
2560 int i;
2561 int added;
2562 char_u *p;
2563 aco_save_T aco;
2564 buf_T *buf;
2565 int start_skip, end_skip;
2566 int new_count;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002567 int buf_empty;
Bram Moolenaar602eb742007-02-20 03:43:38 +00002568 int found_not_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569
2570 /* Find the current buffer in the list of diff buffers. */
2571 idx_cur = diff_buf_idx(curbuf);
2572 if (idx_cur == DB_COUNT)
2573 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002574 emsg(_("E99: Current buffer is not in diff mode"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 return;
2576 }
2577
2578 if (*eap->arg == NUL)
2579 {
2580 /* No argument: Find the other buffer in the list of diff buffers. */
2581 for (idx_other = 0; idx_other < DB_COUNT; ++idx_other)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002582 if (curtab->tp_diffbuf[idx_other] != curbuf
Bram Moolenaar602eb742007-02-20 03:43:38 +00002583 && curtab->tp_diffbuf[idx_other] != NULL)
2584 {
2585 if (eap->cmdidx != CMD_diffput
2586 || curtab->tp_diffbuf[idx_other]->b_p_ma)
2587 break;
2588 found_not_ma = TRUE;
2589 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 if (idx_other == DB_COUNT)
2591 {
Bram Moolenaar602eb742007-02-20 03:43:38 +00002592 if (found_not_ma)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002593 emsg(_("E793: No other buffer in diff mode is modifiable"));
Bram Moolenaar602eb742007-02-20 03:43:38 +00002594 else
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002595 emsg(_("E100: No other buffer in diff mode"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 return;
2597 }
2598
2599 /* Check that there isn't a third buffer in the list */
2600 for (i = idx_other + 1; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002601 if (curtab->tp_diffbuf[i] != curbuf
2602 && curtab->tp_diffbuf[i] != NULL
2603 && (eap->cmdidx != CMD_diffput || curtab->tp_diffbuf[i]->b_p_ma))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002605 emsg(_("E101: More than two buffers in diff mode, don't know which one to use"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 return;
2607 }
2608 }
2609 else
2610 {
2611 /* Buffer number or pattern given. Ignore trailing white space. */
2612 p = eap->arg + STRLEN(eap->arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002613 while (p > eap->arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 --p;
2615 for (i = 0; vim_isdigit(eap->arg[i]) && eap->arg + i < p; ++i)
2616 ;
2617 if (eap->arg + i == p) /* digits only */
2618 i = atol((char *)eap->arg);
2619 else
2620 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002621 i = buflist_findpat(eap->arg, p, FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 if (i < 0)
2623 return; /* error message already given */
2624 }
2625 buf = buflist_findnr(i);
2626 if (buf == NULL)
2627 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002628 semsg(_("E102: Can't find buffer \"%s\""), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 return;
2630 }
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +00002631 if (buf == curbuf)
2632 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002633 idx_other = diff_buf_idx(buf);
2634 if (idx_other == DB_COUNT)
2635 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002636 semsg(_("E103: Buffer \"%s\" is not in diff mode"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 return;
2638 }
2639 }
2640
2641 diff_busy = TRUE;
2642
2643 /* When no range given include the line above or below the cursor. */
2644 if (eap->addr_count == 0)
2645 {
2646 /* Make it possible that ":diffget" on the last line gets line below
2647 * the cursor line when there is no difference above the cursor. */
2648 if (eap->cmdidx == CMD_diffget
2649 && eap->line1 == curbuf->b_ml.ml_line_count
2650 && diff_check(curwin, eap->line1) == 0
2651 && (eap->line1 == 1 || diff_check(curwin, eap->line1 - 1) == 0))
2652 ++eap->line2;
2653 else if (eap->line1 > 0)
2654 --eap->line1;
2655 }
2656
2657 if (eap->cmdidx == CMD_diffget)
2658 {
2659 idx_from = idx_other;
2660 idx_to = idx_cur;
2661 }
2662 else
2663 {
2664 idx_from = idx_cur;
2665 idx_to = idx_other;
2666 /* Need to make the other buffer the current buffer to be able to make
2667 * changes in it. */
2668 /* set curwin/curbuf to buf and save a few things */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002669 aucmd_prepbuf(&aco, curtab->tp_diffbuf[idx_other]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
2671
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002672 /* May give the warning for a changed buffer here, which can trigger the
2673 * FileChangedRO autocommand, which may do nasty things and mess
2674 * everything up. */
2675 if (!curbuf->b_changed)
2676 {
2677 change_warning(0);
2678 if (diff_buf_idx(curbuf) != idx_to)
2679 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01002680 emsg(_("E787: Buffer changed unexpectedly"));
Bram Moolenaard2b58c02018-09-16 18:10:48 +02002681 goto theend;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002682 }
2683 }
2684
Bram Moolenaar071d4272004-06-13 20:20:40 +00002685 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002686 for (dp = curtab->tp_first_diff; dp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 {
2688 if (dp->df_lnum[idx_cur] > eap->line2 + off)
2689 break; /* past the range that was specified */
2690
2691 dfree = NULL;
2692 lnum = dp->df_lnum[idx_to];
2693 count = dp->df_count[idx_to];
2694 if (dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off
2695 && u_save(lnum - 1, lnum + count) != FAIL)
2696 {
2697 /* Inside the specified range and saving for undo worked. */
2698 start_skip = 0;
2699 end_skip = 0;
2700 if (eap->addr_count > 0)
2701 {
2702 /* A range was specified: check if lines need to be skipped. */
2703 start_skip = eap->line1 + off - dp->df_lnum[idx_cur];
2704 if (start_skip > 0)
2705 {
2706 /* range starts below start of current diff block */
2707 if (start_skip > count)
2708 {
2709 lnum += count;
2710 count = 0;
2711 }
2712 else
2713 {
2714 count -= start_skip;
2715 lnum += start_skip;
2716 }
2717 }
2718 else
2719 start_skip = 0;
2720
2721 end_skip = dp->df_lnum[idx_cur] + dp->df_count[idx_cur] - 1
2722 - (eap->line2 + off);
2723 if (end_skip > 0)
2724 {
2725 /* range ends above end of current/from diff block */
2726 if (idx_cur == idx_from) /* :diffput */
2727 {
2728 i = dp->df_count[idx_cur] - start_skip - end_skip;
2729 if (count > i)
2730 count = i;
2731 }
2732 else /* :diffget */
2733 {
2734 count -= end_skip;
2735 end_skip = dp->df_count[idx_from] - start_skip - count;
2736 if (end_skip < 0)
2737 end_skip = 0;
2738 }
2739 }
2740 else
2741 end_skip = 0;
2742 }
2743
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002744 buf_empty = BUFEMPTY();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002745 added = 0;
2746 for (i = 0; i < count; ++i)
2747 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00002748 /* remember deleting the last line of the buffer */
2749 buf_empty = curbuf->b_ml.ml_line_count == 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 ml_delete(lnum, FALSE);
2751 --added;
2752 }
2753 for (i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; ++i)
2754 {
2755 linenr_T nr;
2756
2757 nr = dp->df_lnum[idx_from] + start_skip + i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002758 if (nr > curtab->tp_diffbuf[idx_from]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002760 p = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx_from],
2761 nr, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002762 if (p != NULL)
2763 {
2764 ml_append(lnum + i - 1, p, 0, FALSE);
2765 vim_free(p);
2766 ++added;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002767 if (buf_empty && curbuf->b_ml.ml_line_count == 2)
2768 {
2769 /* Added the first line into an empty buffer, need to
2770 * delete the dummy empty line. */
2771 buf_empty = FALSE;
2772 ml_delete((linenr_T)2, FALSE);
2773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 }
2775 }
2776 new_count = dp->df_count[idx_to] + added;
2777 dp->df_count[idx_to] = new_count;
2778
2779 if (start_skip == 0 && end_skip == 0)
2780 {
2781 /* Check if there are any other buffers and if the diff is
2782 * equal in them. */
2783 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002784 if (curtab->tp_diffbuf[i] != NULL && i != idx_from
2785 && i != idx_to
Bram Moolenaar071d4272004-06-13 20:20:40 +00002786 && !diff_equal_entry(dp, idx_from, i))
2787 break;
2788 if (i == DB_COUNT)
2789 {
2790 /* delete the diff entry, the buffers are now equal here */
2791 dfree = dp;
2792 dp = dp->df_next;
2793 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002794 curtab->tp_first_diff = dp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002795 else
2796 dprev->df_next = dp;
2797 }
2798 }
2799
2800 /* Adjust marks. This will change the following entries! */
2801 if (added != 0)
2802 {
2803 mark_adjust(lnum, lnum + count - 1, (long)MAXLNUM, (long)added);
2804 if (curwin->w_cursor.lnum >= lnum)
2805 {
2806 /* Adjust the cursor position if it's in/after the changed
2807 * lines. */
2808 if (curwin->w_cursor.lnum >= lnum + count)
2809 curwin->w_cursor.lnum += added;
2810 else if (added < 0)
2811 curwin->w_cursor.lnum = lnum;
2812 }
2813 }
2814 changed_lines(lnum, 0, lnum + count, (long)added);
2815
2816 if (dfree != NULL)
2817 {
2818 /* Diff is deleted, update folds in other windows. */
2819#ifdef FEAT_FOLDING
2820 diff_fold_update(dfree, idx_to);
2821#endif
2822 vim_free(dfree);
2823 }
2824 else
2825 /* mark_adjust() may have changed the count in a wrong way */
2826 dp->df_count[idx_to] = new_count;
2827
2828 /* When changing the current buffer, keep track of line numbers */
2829 if (idx_cur == idx_to)
2830 off += added;
2831 }
2832
2833 /* If before the range or not deleted, go to next diff. */
2834 if (dfree == NULL)
2835 {
2836 dprev = dp;
2837 dp = dp->df_next;
2838 }
2839 }
2840
2841 /* restore curwin/curbuf and a few other things */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02002842 if (eap->cmdidx != CMD_diffget)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843 {
2844 /* Syncing undo only works for the current buffer, but we change
2845 * another buffer. Sync undo if the command was typed. This isn't
2846 * 100% right when ":diffput" is used in a function or mapping. */
2847 if (KeyTyped)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002848 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 aucmd_restbuf(&aco);
2850 }
2851
Bram Moolenaard2b58c02018-09-16 18:10:48 +02002852theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 diff_busy = FALSE;
Bram Moolenaard2b58c02018-09-16 18:10:48 +02002854 if (diff_need_update)
Bram Moolenaard2b58c02018-09-16 18:10:48 +02002855 ex_diffupdate(NULL);
Bram Moolenaardf77cef2018-10-07 17:46:42 +02002856
Bram Moolenaar5f57bdc2018-10-25 17:52:23 +02002857 // Check that the cursor is on a valid character and update its
Bram Moolenaardf77cef2018-10-07 17:46:42 +02002858 // position. When there were filler lines the topline has become
2859 // invalid.
2860 check_cursor();
2861 changed_line_abv_curs();
2862
2863 if (diff_need_update)
2864 // redraw already done by ex_diffupdate()
2865 diff_need_update = FALSE;
Bram Moolenaar198fa062018-09-18 21:20:26 +02002866 else
2867 {
Bram Moolenaar198fa062018-09-18 21:20:26 +02002868 // Also need to redraw the other buffers.
2869 diff_redraw(FALSE);
2870 apply_autocmds(EVENT_DIFFUPDATED, NULL, NULL, FALSE, curbuf);
2871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872}
2873
2874#ifdef FEAT_FOLDING
2875/*
2876 * Update folds for all diff buffers for entry "dp".
2877 * Skip buffer with index "skip_idx".
2878 * When there are no diffs, all folds are removed.
2879 */
2880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002881diff_fold_update(diff_T *dp, int skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002882{
2883 int i;
2884 win_T *wp;
2885
Bram Moolenaar29323592016-07-24 22:04:11 +02002886 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002887 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002888 if (curtab->tp_diffbuf[i] == wp->w_buffer && i != skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002889 foldUpdate(wp, dp->df_lnum[i],
2890 dp->df_lnum[i] + dp->df_count[i]);
2891}
2892#endif
2893
2894/*
2895 * Return TRUE if buffer "buf" is in diff-mode.
2896 */
2897 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002898diff_mode_buf(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002900 tabpage_T *tp;
2901
Bram Moolenaar29323592016-07-24 22:04:11 +02002902 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002903 if (diff_buf_idx_tp(buf, tp) != DB_COUNT)
2904 return TRUE;
2905 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906}
2907
2908/*
2909 * Move "count" times in direction "dir" to the next diff block.
2910 * Return FAIL if there isn't such a diff block.
2911 */
2912 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002913diff_move_to(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002914{
2915 int idx;
2916 linenr_T lnum = curwin->w_cursor.lnum;
2917 diff_T *dp;
2918
2919 idx = diff_buf_idx(curbuf);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002920 if (idx == DB_COUNT || curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 return FAIL;
2922
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002923 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002924 ex_diffupdate(NULL); /* update after a big change */
2925
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002926 if (curtab->tp_first_diff == NULL) /* no diffs today */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 return FAIL;
2928
2929 while (--count >= 0)
2930 {
2931 /* Check if already before first diff. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002932 if (dir == BACKWARD && lnum <= curtab->tp_first_diff->df_lnum[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 break;
2934
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002935 for (dp = curtab->tp_first_diff; ; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002936 {
2937 if (dp == NULL)
2938 break;
2939 if ((dir == FORWARD && lnum < dp->df_lnum[idx])
2940 || (dir == BACKWARD
2941 && (dp->df_next == NULL
2942 || lnum <= dp->df_next->df_lnum[idx])))
2943 {
2944 lnum = dp->df_lnum[idx];
2945 break;
2946 }
2947 }
2948 }
2949
2950 /* don't end up past the end of the file */
2951 if (lnum > curbuf->b_ml.ml_line_count)
2952 lnum = curbuf->b_ml.ml_line_count;
2953
2954 /* When the cursor didn't move at all we fail. */
2955 if (lnum == curwin->w_cursor.lnum)
2956 return FAIL;
2957
2958 setpcmark();
2959 curwin->w_cursor.lnum = lnum;
2960 curwin->w_cursor.col = 0;
2961
2962 return OK;
2963}
2964
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002965/*
2966 * Return the line number in the current window that is closest to "lnum1" in
2967 * "buf1" in diff mode.
2968 */
2969 static linenr_T
2970diff_get_corresponding_line_int(
Bram Moolenaar7454a062016-01-30 15:14:10 +01002971 buf_T *buf1,
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002972 linenr_T lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002973{
2974 int idx1;
2975 int idx2;
2976 diff_T *dp;
2977 int baseline = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002978
2979 idx1 = diff_buf_idx(buf1);
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002980 idx2 = diff_buf_idx(curbuf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002981 if (idx1 == DB_COUNT || idx2 == DB_COUNT || curtab->tp_first_diff == NULL)
2982 return lnum1;
2983
2984 if (curtab->tp_diff_invalid)
2985 ex_diffupdate(NULL); /* update after a big change */
2986
2987 if (curtab->tp_first_diff == NULL) /* no diffs today */
2988 return lnum1;
2989
2990 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
2991 {
2992 if (dp->df_lnum[idx1] > lnum1)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002993 return lnum1 - baseline;
2994 if ((dp->df_lnum[idx1] + dp->df_count[idx1]) > lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002995 {
2996 /* Inside the diffblock */
2997 baseline = lnum1 - dp->df_lnum[idx1];
2998 if (baseline > dp->df_count[idx2])
2999 baseline = dp->df_count[idx2];
3000
3001 return dp->df_lnum[idx2] + baseline;
3002 }
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003003 if ( (dp->df_lnum[idx1] == lnum1)
3004 && (dp->df_count[idx1] == 0)
3005 && (dp->df_lnum[idx2] <= curwin->w_cursor.lnum)
3006 && ((dp->df_lnum[idx2] + dp->df_count[idx2])
3007 > curwin->w_cursor.lnum))
Bram Moolenaar860cae12010-06-05 23:22:07 +02003008 /*
3009 * Special case: if the cursor is just after a zero-count
3010 * block (i.e. all filler) and the target cursor is already
3011 * inside the corresponding block, leave the target cursor
3012 * unmoved. This makes repeated CTRL-W W operations work
3013 * as expected.
3014 */
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003015 return curwin->w_cursor.lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003016 baseline = (dp->df_lnum[idx1] + dp->df_count[idx1])
3017 - (dp->df_lnum[idx2] + dp->df_count[idx2]);
3018 }
3019
3020 /* If we get here then the cursor is after the last diff */
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003021 return lnum1 - baseline;
3022}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003023
Bram Moolenaar025e3e02016-10-18 14:50:18 +02003024/*
3025 * Return the line number in the current window that is closest to "lnum1" in
3026 * "buf1" in diff mode. Checks the line number to be valid.
3027 */
3028 linenr_T
3029diff_get_corresponding_line(buf_T *buf1, linenr_T lnum1)
3030{
3031 linenr_T lnum = diff_get_corresponding_line_int(buf1, lnum1);
3032
3033 /* don't end up past the end of the file */
3034 if (lnum > curbuf->b_ml.ml_line_count)
3035 return curbuf->b_ml.ml_line_count;
3036 return lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003037}
Bram Moolenaar860cae12010-06-05 23:22:07 +02003038
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039/*
3040 * For line "lnum" in the current window find the equivalent lnum in window
3041 * "wp", compensating for inserted/deleted lines.
3042 */
3043 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01003044diff_lnum_win(linenr_T lnum, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045{
3046 diff_T *dp;
3047 int idx;
3048 int i;
3049 linenr_T n;
3050
3051 idx = diff_buf_idx(curbuf);
3052 if (idx == DB_COUNT) /* safety check */
3053 return (linenr_T)0;
3054
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003055 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 ex_diffupdate(NULL); /* update after a big change */
3057
3058 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00003059 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
3061 break;
3062
3063 /* When after the last change, compute relative to the last line number. */
3064 if (dp == NULL)
3065 return wp->w_buffer->b_ml.ml_line_count
3066 - (curbuf->b_ml.ml_line_count - lnum);
3067
3068 /* Find index for "wp". */
3069 i = diff_buf_idx(wp->w_buffer);
3070 if (i == DB_COUNT) /* safety check */
3071 return (linenr_T)0;
3072
3073 n = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]);
3074 if (n > dp->df_lnum[i] + dp->df_count[i])
3075 n = dp->df_lnum[i] + dp->df_count[i];
3076 return n;
3077}
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078
Bram Moolenaare828b762018-09-10 17:51:58 +02003079/*
3080 * Handle an ED style diff line.
3081 * Return FAIL if the line does not contain diff info.
3082 */
3083 static int
3084parse_diff_ed(
3085 char_u *line,
3086 linenr_T *lnum_orig,
3087 long *count_orig,
3088 linenr_T *lnum_new,
3089 long *count_new)
3090{
3091 char_u *p;
3092 long f1, l1, f2, l2;
3093 int difftype;
3094
3095 // The line must be one of three formats:
3096 // change: {first}[,{last}]c{first}[,{last}]
3097 // append: {first}a{first}[,{last}]
3098 // delete: {first}[,{last}]d{first}
3099 p = line;
3100 f1 = getdigits(&p);
3101 if (*p == ',')
3102 {
3103 ++p;
3104 l1 = getdigits(&p);
3105 }
3106 else
3107 l1 = f1;
3108 if (*p != 'a' && *p != 'c' && *p != 'd')
3109 return FAIL; // invalid diff format
3110 difftype = *p++;
3111 f2 = getdigits(&p);
3112 if (*p == ',')
3113 {
3114 ++p;
3115 l2 = getdigits(&p);
3116 }
3117 else
3118 l2 = f2;
3119 if (l1 < f1 || l2 < f2)
3120 return FAIL;
3121
3122 if (difftype == 'a')
3123 {
3124 *lnum_orig = f1 + 1;
3125 *count_orig = 0;
3126 }
3127 else
3128 {
3129 *lnum_orig = f1;
3130 *count_orig = l1 - f1 + 1;
3131 }
3132 if (difftype == 'd')
3133 {
3134 *lnum_new = f2 + 1;
3135 *count_new = 0;
3136 }
3137 else
3138 {
3139 *lnum_new = f2;
3140 *count_new = l2 - f2 + 1;
3141 }
3142 return OK;
3143}
3144
3145/*
3146 * Parses unified diff with zero(!) context lines.
3147 * Return FAIL if there is no diff information in "line".
3148 */
3149 static int
3150parse_diff_unified(
3151 char_u *line,
3152 linenr_T *lnum_orig,
3153 long *count_orig,
3154 linenr_T *lnum_new,
3155 long *count_new)
3156{
3157 char_u *p;
3158 long oldline, oldcount, newline, newcount;
3159
3160 // Parse unified diff hunk header:
3161 // @@ -oldline,oldcount +newline,newcount @@
3162 p = line;
3163 if (*p++ == '@' && *p++ == '@' && *p++ == ' ' && *p++ == '-')
3164 {
3165 oldline = getdigits(&p);
3166 if (*p == ',')
3167 {
3168 ++p;
3169 oldcount = getdigits(&p);
3170 }
3171 else
3172 oldcount = 1;
3173 if (*p++ == ' ' && *p++ == '+')
3174 {
3175 newline = getdigits(&p);
3176 if (*p == ',')
3177 {
3178 ++p;
3179 newcount = getdigits(&p);
3180 }
3181 else
3182 newcount = 1;
3183 }
3184 else
3185 return FAIL; // invalid diff format
3186
3187 if (oldcount == 0)
3188 oldline += 1;
3189 if (newcount == 0)
3190 newline += 1;
3191 if (newline == 0)
3192 newline = 1;
3193
3194 *lnum_orig = oldline;
3195 *count_orig = oldcount;
3196 *lnum_new = newline;
3197 *count_new = newcount;
3198
3199 return OK;
3200 }
3201
3202 return FAIL;
3203}
3204
3205/*
3206 * Callback function for the xdl_diff() function.
3207 * Stores the diff output in a grow array.
3208 */
3209 static int
3210xdiff_out(void *priv, mmbuffer_t *mb, int nbuf)
3211{
3212 diffout_T *dout = (diffout_T *)priv;
Bram Moolenaare828b762018-09-10 17:51:58 +02003213 char_u *p;
3214
Bram Moolenaarf080d702018-10-31 22:57:26 +01003215 // The header line always comes by itself, text lines in at least two
3216 // parts. We drop the text part.
3217 if (nbuf > 1)
3218 return 0;
3219
3220 // sanity check
3221 if (STRNCMP(mb[0].ptr, "@@ ", 3) != 0)
3222 return 0;
3223
3224 if (ga_grow(&dout->dout_ga, 1) == FAIL)
3225 return -1;
3226 p = vim_strnsave((char_u *)mb[0].ptr, mb[0].size);
3227 if (p == NULL)
3228 return -1;
3229 ((char_u **)dout->dout_ga.ga_data)[dout->dout_ga.ga_len++] = p;
Bram Moolenaare828b762018-09-10 17:51:58 +02003230 return 0;
3231}
3232
Bram Moolenaar071d4272004-06-13 20:20:40 +00003233#endif /* FEAT_DIFF */