blob: 2f5b38c7b329bf44286da05d9f516c68d98d274c [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 Moolenaar071d4272004-06-13 20:20:40 +000012 */
13
14#include "vim.h"
15
16#if defined(FEAT_DIFF) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018static int diff_busy = FALSE; /* ex_diffgetput() is busy */
19
20/* flags obtained from the 'diffopt' option */
21#define DIFF_FILLER 1 /* display filler lines */
22#define DIFF_ICASE 2 /* ignore case */
23#define DIFF_IWHITE 4 /* ignore change in white space */
Bram Moolenaarc4675a12006-03-15 22:50:30 +000024#define DIFF_HORIZONTAL 8 /* horizontal splits */
25#define DIFF_VERTICAL 16 /* vertical splits */
Bram Moolenaar97ce4192017-12-01 20:35:58 +010026#define DIFF_HIDDEN_OFF 32 /* diffoff when hidden */
Bram Moolenaar071d4272004-06-13 20:20:40 +000027static int diff_flags = DIFF_FILLER;
28
29#define LBUFLEN 50 /* length of line in diff file */
30
31static int diff_a_works = MAYBE; /* TRUE when "diff -a" works, FALSE when it
32 doesn't work, MAYBE when not checked yet */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010033#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000034static int diff_bin_works = MAYBE; /* TRUE when "diff --binary" works, FALSE
35 when it doesn't work, MAYBE when not
36 checked yet */
37#endif
38
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010039static int diff_buf_idx(buf_T *buf);
40static int diff_buf_idx_tp(buf_T *buf, tabpage_T *tp);
41static void diff_mark_adjust_tp(tabpage_T *tp, int idx, linenr_T line1, linenr_T line2, long amount, long amount_after);
42static void diff_check_unchanged(tabpage_T *tp, diff_T *dp);
43static int diff_check_sanity(tabpage_T *tp, diff_T *dp);
44static void diff_redraw(int dofold);
45static int diff_write(buf_T *buf, char_u *fname);
46static void diff_file(char_u *tmp_orig, char_u *tmp_new, char_u *tmp_diff);
47static int diff_equal_entry(diff_T *dp, int idx1, int idx2);
48static int diff_cmp(char_u *s1, char_u *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000049#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010050static void diff_fold_update(diff_T *dp, int skip_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000051#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010052static void diff_read(int idx_orig, int idx_new, char_u *fname);
53static void diff_copy_entry(diff_T *dprev, diff_T *dp, int idx_orig, int idx_new);
54static diff_T *diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000055
56#ifndef USE_CR
57# define tag_fgets vim_fgets
58#endif
59
60/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 * Called when deleting or unloading a buffer: No longer make a diff with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000062 */
63 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010064diff_buf_delete(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000065{
66 int i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000067 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaar29323592016-07-24 22:04:11 +020069 FOR_ALL_TABPAGES(tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000070 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000071 i = diff_buf_idx_tp(buf, tp);
72 if (i != DB_COUNT)
73 {
74 tp->tp_diffbuf[i] = NULL;
75 tp->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +000076 if (tp == curtab)
77 diff_redraw(TRUE);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000079 }
80}
81
82/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000083 * Check if the current buffer should be added to or removed from the list of
84 * diff buffers.
85 */
86 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010087diff_buf_adjust(win_T *win)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000088{
89 win_T *wp;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000090 int i;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000091
92 if (!win->w_p_diff)
93 {
94 /* When there is no window showing a diff for this buffer, remove
95 * it from the diffs. */
Bram Moolenaar29323592016-07-24 22:04:11 +020096 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000097 if (wp->w_buffer == win->w_buffer && wp->w_p_diff)
98 break;
99 if (wp == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000100 {
101 i = diff_buf_idx(win->w_buffer);
102 if (i != DB_COUNT)
103 {
104 curtab->tp_diffbuf[i] = NULL;
105 curtab->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000106 diff_redraw(TRUE);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000107 }
108 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000109 }
110 else
111 diff_buf_add(win->w_buffer);
112}
113
114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000115 * Add a buffer to make diffs for.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000116 * Call this when a new buffer is being edited in the current window where
117 * 'diff' is set.
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +0000118 * Marks the current buffer as being part of the diff and requiring updating.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000119 * This must be done before any autocmd, because a command may use info
120 * about the screen contents.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000121 */
122 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100123diff_buf_add(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000124{
125 int i;
126
127 if (diff_buf_idx(buf) != DB_COUNT)
128 return; /* It's already there. */
129
130 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000131 if (curtab->tp_diffbuf[i] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000133 curtab->tp_diffbuf[i] = buf;
134 curtab->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000135 diff_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000136 return;
137 }
138
Bram Moolenaar89eaa412016-07-31 14:17:27 +0200139 EMSGN(_("E96: Cannot diff more than %ld buffers"), DB_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140}
141
142/*
Bram Moolenaar25ea0542017-02-03 23:16:28 +0100143 * Remove all buffers to make diffs for.
144 */
145 static void
146diff_buf_clear(void)
147{
148 int i;
149
150 for (i = 0; i < DB_COUNT; ++i)
151 if (curtab->tp_diffbuf[i] != NULL)
152 {
153 curtab->tp_diffbuf[i] = NULL;
154 curtab->tp_diff_invalid = TRUE;
155 diff_redraw(TRUE);
156 }
157}
158
159/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000160 * Find buffer "buf" in the list of diff buffers for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000161 * Return its index or DB_COUNT if not found.
162 */
163 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100164diff_buf_idx(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000165{
166 int idx;
167
168 for (idx = 0; idx < DB_COUNT; ++idx)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000169 if (curtab->tp_diffbuf[idx] == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000170 break;
171 return idx;
172}
173
174/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000175 * Find buffer "buf" in the list of diff buffers for tab page "tp".
176 * Return its index or DB_COUNT if not found.
177 */
178 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100179diff_buf_idx_tp(buf_T *buf, tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000180{
181 int idx;
182
183 for (idx = 0; idx < DB_COUNT; ++idx)
184 if (tp->tp_diffbuf[idx] == buf)
185 break;
186 return idx;
187}
188
189/*
190 * Mark the diff info involving buffer "buf" as invalid, it will be updated
191 * when info is requested.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 */
193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100194diff_invalidate(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000196 tabpage_T *tp;
197 int i;
198
Bram Moolenaar29323592016-07-24 22:04:11 +0200199 FOR_ALL_TABPAGES(tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000200 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000201 i = diff_buf_idx_tp(buf, tp);
202 if (i != DB_COUNT)
203 {
204 tp->tp_diff_invalid = TRUE;
205 if (tp == curtab)
206 diff_redraw(TRUE);
207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208 }
209}
210
211/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000212 * Called by mark_adjust(): update line numbers in "curbuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213 */
214 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100215diff_mark_adjust(
216 linenr_T line1,
217 linenr_T line2,
218 long amount,
219 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000221 int idx;
222 tabpage_T *tp;
223
224 /* Handle all tab pages that use the current buffer in a diff. */
Bram Moolenaar29323592016-07-24 22:04:11 +0200225 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000226 {
227 idx = diff_buf_idx_tp(curbuf, tp);
228 if (idx != DB_COUNT)
229 diff_mark_adjust_tp(tp, idx, line1, line2, amount, amount_after);
230 }
231}
232
233/*
234 * Update line numbers in tab page "tp" for "curbuf" with index "idx".
235 * This attempts to update the changes as much as possible:
236 * When inserting/deleting lines outside of existing change blocks, create a
237 * new change block and update the line numbers in following blocks.
238 * When inserting/deleting lines in existing change blocks, update them.
239 */
240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100241diff_mark_adjust_tp(
242 tabpage_T *tp,
243 int idx,
244 linenr_T line1,
245 linenr_T line2,
246 long amount,
247 long amount_after)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000248{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 diff_T *dp;
250 diff_T *dprev;
251 diff_T *dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252 int i;
253 int inserted, deleted;
254 int n, off;
255 linenr_T last;
256 linenr_T lnum_deleted = line1; /* lnum of remaining deletion */
257 int check_unchanged;
258
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 if (line2 == MAXLNUM)
260 {
261 /* mark_adjust(99, MAXLNUM, 9, 0): insert lines */
262 inserted = amount;
263 deleted = 0;
264 }
265 else if (amount_after > 0)
266 {
267 /* mark_adjust(99, 98, MAXLNUM, 9): a change that inserts lines*/
268 inserted = amount_after;
269 deleted = 0;
270 }
271 else
272 {
273 /* mark_adjust(98, 99, MAXLNUM, -2): delete lines */
274 inserted = 0;
275 deleted = -amount_after;
276 }
277
278 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000279 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280 for (;;)
281 {
282 /* If the change is after the previous diff block and before the next
283 * diff block, thus not touching an existing change, create a new diff
284 * block. Don't do this when ex_diffgetput() is busy. */
285 if ((dp == NULL || dp->df_lnum[idx] - 1 > line2
286 || (line2 == MAXLNUM && dp->df_lnum[idx] > line1))
287 && (dprev == NULL
288 || dprev->df_lnum[idx] + dprev->df_count[idx] < line1)
289 && !diff_busy)
290 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000291 dnext = diff_alloc_new(tp, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292 if (dnext == NULL)
293 return;
294
295 dnext->df_lnum[idx] = line1;
296 dnext->df_count[idx] = inserted;
297 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000298 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000299 {
300 if (dprev == NULL)
301 dnext->df_lnum[i] = line1;
302 else
303 dnext->df_lnum[i] = line1
304 + (dprev->df_lnum[i] + dprev->df_count[i])
305 - (dprev->df_lnum[idx] + dprev->df_count[idx]);
306 dnext->df_count[i] = deleted;
307 }
308 }
309
310 /* if at end of the list, quit */
311 if (dp == NULL)
312 break;
313
314 /*
315 * Check for these situations:
316 * 1 2 3
317 * 1 2 3
318 * line1 2 3 4 5
319 * 2 3 4 5
320 * 2 3 4 5
321 * line2 2 3 4 5
322 * 3 5 6
323 * 3 5 6
324 */
325 /* compute last line of this change */
326 last = dp->df_lnum[idx] + dp->df_count[idx] - 1;
327
328 /* 1. change completely above line1: nothing to do */
329 if (last >= line1 - 1)
330 {
331 /* 6. change below line2: only adjust for amount_after; also when
332 * "deleted" became zero when deleted all lines between two diffs */
333 if (dp->df_lnum[idx] - (deleted + inserted != 0) > line2)
334 {
335 if (amount_after == 0)
336 break; /* nothing left to change */
337 dp->df_lnum[idx] += amount_after;
338 }
339 else
340 {
341 check_unchanged = FALSE;
342
343 /* 2. 3. 4. 5.: inserted/deleted lines touching this diff. */
344 if (deleted > 0)
345 {
346 if (dp->df_lnum[idx] >= line1)
347 {
348 off = dp->df_lnum[idx] - lnum_deleted;
349 if (last <= line2)
350 {
351 /* 4. delete all lines of diff */
352 if (dp->df_next != NULL
353 && dp->df_next->df_lnum[idx] - 1 <= line2)
354 {
355 /* delete continues in next diff, only do
356 * lines until that one */
357 n = dp->df_next->df_lnum[idx] - lnum_deleted;
358 deleted -= n;
359 n -= dp->df_count[idx];
360 lnum_deleted = dp->df_next->df_lnum[idx];
361 }
362 else
363 n = deleted - dp->df_count[idx];
364 dp->df_count[idx] = 0;
365 }
366 else
367 {
368 /* 5. delete lines at or just before top of diff */
369 n = off;
370 dp->df_count[idx] -= line2 - dp->df_lnum[idx] + 1;
371 check_unchanged = TRUE;
372 }
373 dp->df_lnum[idx] = line1;
374 }
375 else
376 {
377 off = 0;
378 if (last < line2)
379 {
380 /* 2. delete at end of of diff */
381 dp->df_count[idx] -= last - lnum_deleted + 1;
382 if (dp->df_next != NULL
383 && dp->df_next->df_lnum[idx] - 1 <= line2)
384 {
385 /* delete continues in next diff, only do
386 * lines until that one */
387 n = dp->df_next->df_lnum[idx] - 1 - last;
388 deleted -= dp->df_next->df_lnum[idx]
389 - lnum_deleted;
390 lnum_deleted = dp->df_next->df_lnum[idx];
391 }
392 else
393 n = line2 - last;
394 check_unchanged = TRUE;
395 }
396 else
397 {
398 /* 3. delete lines inside the diff */
399 n = 0;
400 dp->df_count[idx] -= deleted;
401 }
402 }
403
404 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000405 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000406 {
407 dp->df_lnum[i] -= off;
408 dp->df_count[i] += n;
409 }
410 }
411 else
412 {
413 if (dp->df_lnum[idx] <= line1)
414 {
415 /* inserted lines somewhere in this diff */
416 dp->df_count[idx] += inserted;
417 check_unchanged = TRUE;
418 }
419 else
420 /* inserted lines somewhere above this diff */
421 dp->df_lnum[idx] += inserted;
422 }
423
424 if (check_unchanged)
425 /* Check if inserted lines are equal, may reduce the
426 * size of the diff. TODO: also check for equal lines
427 * in the middle and perhaps split the block. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000428 diff_check_unchanged(tp, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000429 }
430 }
431
432 /* check if this block touches the previous one, may merge them. */
433 if (dprev != NULL && dprev->df_lnum[idx] + dprev->df_count[idx]
434 == dp->df_lnum[idx])
435 {
436 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000437 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 dprev->df_count[i] += dp->df_count[i];
439 dprev->df_next = dp->df_next;
440 vim_free(dp);
441 dp = dprev->df_next;
442 }
443 else
444 {
445 /* Advance to next entry. */
446 dprev = dp;
447 dp = dp->df_next;
448 }
449 }
450
451 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000452 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453 while (dp != NULL)
454 {
455 /* All counts are zero, remove this entry. */
456 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000457 if (tp->tp_diffbuf[i] != NULL && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000458 break;
459 if (i == DB_COUNT)
460 {
461 dnext = dp->df_next;
462 vim_free(dp);
463 dp = dnext;
464 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000465 tp->tp_first_diff = dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466 else
467 dprev->df_next = dnext;
468 }
469 else
470 {
471 /* Advance to next entry. */
472 dprev = dp;
473 dp = dp->df_next;
474 }
475
476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000477
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000478 if (tp == curtab)
479 {
480 diff_redraw(TRUE);
481
482 /* Need to recompute the scroll binding, may remove or add filler
483 * lines (e.g., when adding lines above w_topline). But it's slow when
484 * making many changes, postpone until redrawing. */
485 diff_need_scrollbind = TRUE;
486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000487}
488
489/*
490 * Allocate a new diff block and link it between "dprev" and "dp".
491 */
492 static diff_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100493diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000494{
495 diff_T *dnew;
496
497 dnew = (diff_T *)alloc((unsigned)sizeof(diff_T));
498 if (dnew != NULL)
499 {
500 dnew->df_next = dp;
501 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000502 tp->tp_first_diff = dnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503 else
504 dprev->df_next = dnew;
505 }
506 return dnew;
507}
508
509/*
510 * Check if the diff block "dp" can be made smaller for lines at the start and
511 * end that are equal. Called after inserting lines.
512 * This may result in a change where all buffers have zero lines, the caller
513 * must take care of removing it.
514 */
515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100516diff_check_unchanged(tabpage_T *tp, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517{
518 int i_org;
519 int i_new;
520 int off_org, off_new;
521 char_u *line_org;
522 int dir = FORWARD;
523
524 /* Find the first buffers, use it as the original, compare the other
525 * buffer lines against this one. */
526 for (i_org = 0; i_org < DB_COUNT; ++i_org)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000527 if (tp->tp_diffbuf[i_org] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000528 break;
529 if (i_org == DB_COUNT) /* safety check */
530 return;
531
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000532 if (diff_check_sanity(tp, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 return;
534
535 /* First check lines at the top, then at the bottom. */
536 off_org = 0;
537 off_new = 0;
538 for (;;)
539 {
540 /* Repeat until a line is found which is different or the number of
541 * lines has become zero. */
542 while (dp->df_count[i_org] > 0)
543 {
544 /* Copy the line, the next ml_get() will invalidate it. */
545 if (dir == BACKWARD)
546 off_org = dp->df_count[i_org] - 1;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000547 line_org = vim_strsave(ml_get_buf(tp->tp_diffbuf[i_org],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 dp->df_lnum[i_org] + off_org, FALSE));
549 if (line_org == NULL)
550 return;
551 for (i_new = i_org + 1; i_new < DB_COUNT; ++i_new)
552 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000553 if (tp->tp_diffbuf[i_new] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554 continue;
555 if (dir == BACKWARD)
556 off_new = dp->df_count[i_new] - 1;
557 /* if other buffer doesn't have this line, it was inserted */
558 if (off_new < 0 || off_new >= dp->df_count[i_new])
559 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000560 if (diff_cmp(line_org, ml_get_buf(tp->tp_diffbuf[i_new],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 dp->df_lnum[i_new] + off_new, FALSE)) != 0)
562 break;
563 }
564 vim_free(line_org);
565
566 /* Stop when a line isn't equal in all diff buffers. */
567 if (i_new != DB_COUNT)
568 break;
569
570 /* Line matched in all buffers, remove it from the diff. */
571 for (i_new = i_org; i_new < DB_COUNT; ++i_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000572 if (tp->tp_diffbuf[i_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 {
574 if (dir == FORWARD)
575 ++dp->df_lnum[i_new];
576 --dp->df_count[i_new];
577 }
578 }
579 if (dir == BACKWARD)
580 break;
581 dir = BACKWARD;
582 }
583}
584
585/*
586 * Check if a diff block doesn't contain invalid line numbers.
587 * This can happen when the diff program returns invalid results.
588 */
589 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100590diff_check_sanity(tabpage_T *tp, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000591{
592 int i;
593
594 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000595 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 if (dp->df_lnum[i] + dp->df_count[i] - 1
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000597 > tp->tp_diffbuf[i]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 return FAIL;
599 return OK;
600}
601
602/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000603 * Mark all diff buffers in the current tab page for redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000604 */
605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100606diff_redraw(
607 int dofold) /* also recompute the folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608{
609 win_T *wp;
610 int n;
611
Bram Moolenaar29323592016-07-24 22:04:11 +0200612 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 if (wp->w_p_diff)
614 {
Bram Moolenaar60f83772006-03-12 21:52:47 +0000615 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616#ifdef FEAT_FOLDING
617 if (dofold && foldmethodIsDiff(wp))
618 foldUpdateAll(wp);
619#endif
620 /* A change may have made filler lines invalid, need to take care
621 * of that for other windows. */
Bram Moolenaara80888d2012-10-21 22:18:21 +0200622 n = diff_check(wp, wp->w_topline);
623 if ((wp != curwin && wp->w_topfill > 0) || n > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 if (wp->w_topfill > n)
626 wp->w_topfill = (n < 0 ? 0 : n);
Bram Moolenaara80888d2012-10-21 22:18:21 +0200627 else if (n > 0 && n > wp->w_topfill)
628 wp->w_topfill = n;
Bram Moolenaar846a2ff2014-05-28 11:35:37 +0200629 check_topfill(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 }
631 }
632}
633
634/*
635 * Write buffer "buf" to file "name".
636 * Always use 'fileformat' set to "unix".
637 * Return FAIL for failure
638 */
639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100640diff_write(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641{
642 int r;
643 char_u *save_ff;
644
645 save_ff = buf->b_p_ff;
646 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX);
647 r = buf_write(buf, fname, NULL, (linenr_T)1, buf->b_ml.ml_line_count,
648 NULL, FALSE, FALSE, FALSE, TRUE);
649 free_string_option(buf->b_p_ff);
650 buf->b_p_ff = save_ff;
651 return r;
652}
653
654/*
655 * Completely update the diffs for the buffers involved.
656 * This uses the ordinary "diff" command.
657 * The buffers are written to a file, also for unmodified buffers (the file
658 * could have been produced by autocommands, e.g. the netrw plugin).
659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100661ex_diffupdate(
Bram Moolenaarf1d25012016-03-03 12:22:53 +0100662 exarg_T *eap) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663{
664 buf_T *buf;
665 int idx_orig;
666 int idx_new;
667 char_u *tmp_orig;
668 char_u *tmp_new;
669 char_u *tmp_diff;
670 FILE *fd;
671 int ok;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000672 int io_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000673
674 /* Delete all diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000675 diff_clear(curtab);
676 curtab->tp_diff_invalid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677
678 /* Use the first buffer as the original text. */
679 for (idx_orig = 0; idx_orig < DB_COUNT; ++idx_orig)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000680 if (curtab->tp_diffbuf[idx_orig] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 break;
682 if (idx_orig == DB_COUNT)
683 return;
684
685 /* Only need to do something when there is another buffer. */
686 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000687 if (curtab->tp_diffbuf[idx_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 break;
689 if (idx_new == DB_COUNT)
690 return;
691
692 /* We need three temp file names. */
Bram Moolenaare5c421c2015-03-31 13:33:08 +0200693 tmp_orig = vim_tempname('o', TRUE);
694 tmp_new = vim_tempname('n', TRUE);
695 tmp_diff = vim_tempname('d', TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000696 if (tmp_orig == NULL || tmp_new == NULL || tmp_diff == NULL)
697 goto theend;
698
699 /*
700 * Do a quick test if "diff" really works. Otherwise it looks like there
701 * are no differences. Can't use the return value, it's non-zero when
702 * there are differences.
703 * May try twice, first with "-a" and then without.
704 */
705 for (;;)
706 {
707 ok = FALSE;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000708 fd = mch_fopen((char *)tmp_orig, "w");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000709 if (fd == NULL)
710 io_error = TRUE;
711 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000713 if (fwrite("line1\n", (size_t)6, (size_t)1, fd) != 1)
714 io_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 fclose(fd);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000716 fd = mch_fopen((char *)tmp_new, "w");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000717 if (fd == NULL)
718 io_error = TRUE;
719 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000721 if (fwrite("line2\n", (size_t)6, (size_t)1, fd) != 1)
722 io_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 fclose(fd);
724 diff_file(tmp_orig, tmp_new, tmp_diff);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000725 fd = mch_fopen((char *)tmp_diff, "r");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000726 if (fd == NULL)
727 io_error = TRUE;
728 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729 {
730 char_u linebuf[LBUFLEN];
731
732 for (;;)
733 {
734 /* There must be a line that contains "1c1". */
735 if (tag_fgets(linebuf, LBUFLEN, fd))
736 break;
737 if (STRNCMP(linebuf, "1c1", 3) == 0)
738 ok = TRUE;
739 }
740 fclose(fd);
741 }
742 mch_remove(tmp_diff);
743 mch_remove(tmp_new);
744 }
745 mch_remove(tmp_orig);
746 }
747
748#ifdef FEAT_EVAL
749 /* When using 'diffexpr' break here. */
750 if (*p_dex != NUL)
751 break;
752#endif
753
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100754#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 /* If the "-a" argument works, also check if "--binary" works. */
756 if (ok && diff_a_works == MAYBE && diff_bin_works == MAYBE)
757 {
758 diff_a_works = TRUE;
759 diff_bin_works = TRUE;
760 continue;
761 }
762 if (!ok && diff_a_works == TRUE && diff_bin_works == TRUE)
763 {
764 /* Tried --binary, but it failed. "-a" works though. */
765 diff_bin_works = FALSE;
766 ok = TRUE;
767 }
768#endif
769
770 /* If we checked if "-a" works already, break here. */
771 if (diff_a_works != MAYBE)
772 break;
773 diff_a_works = ok;
774
775 /* If "-a" works break here, otherwise retry without "-a". */
776 if (ok)
777 break;
778 }
779 if (!ok)
780 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000781 if (io_error)
782 EMSG(_("E810: Cannot read or write temp files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 EMSG(_("E97: Cannot create diffs"));
784 diff_a_works = MAYBE;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100785#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 diff_bin_works = MAYBE;
787#endif
788 goto theend;
789 }
790
Bram Moolenaarbd1d5602012-05-18 18:47:17 +0200791 /* :diffupdate! */
792 if (eap != NULL && eap->forceit)
793 for (idx_new = idx_orig; idx_new < DB_COUNT; ++idx_new)
794 {
795 buf = curtab->tp_diffbuf[idx_new];
796 if (buf_valid(buf))
797 buf_check_timestamp(buf, FALSE);
798 }
799
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 /* Write the first buffer to a tempfile. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000801 buf = curtab->tp_diffbuf[idx_orig];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 if (diff_write(buf, tmp_orig) == FAIL)
803 goto theend;
804
805 /* Make a difference between the first buffer and every other. */
806 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
807 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000808 buf = curtab->tp_diffbuf[idx_new];
Bram Moolenaar9dd33af2015-08-04 21:51:25 +0200809 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
810 continue; /* skip buffer that isn't loaded */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 if (diff_write(buf, tmp_new) == FAIL)
812 continue;
813 diff_file(tmp_orig, tmp_new, tmp_diff);
814
815 /* Read the diff output and add each entry to the diff list. */
816 diff_read(idx_orig, idx_new, tmp_diff);
817 mch_remove(tmp_diff);
818 mch_remove(tmp_new);
819 }
820 mch_remove(tmp_orig);
821
Bram Moolenaar60a44dc2007-10-19 16:58:12 +0000822 /* force updating cursor position on screen */
823 curwin->w_valid_cursor.lnum = 0;
824
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 diff_redraw(TRUE);
826
827theend:
828 vim_free(tmp_orig);
829 vim_free(tmp_new);
830 vim_free(tmp_diff);
831}
832
833/*
834 * Make a diff between files "tmp_orig" and "tmp_new", results in "tmp_diff".
835 */
836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100837diff_file(
838 char_u *tmp_orig,
839 char_u *tmp_new,
840 char_u *tmp_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000841{
842 char_u *cmd;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000843 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000844
845#ifdef FEAT_EVAL
846 if (*p_dex != NUL)
847 /* Use 'diffexpr' to generate the diff file. */
848 eval_diff(tmp_orig, tmp_new, tmp_diff);
849 else
850#endif
851 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000852 len = STRLEN(tmp_orig) + STRLEN(tmp_new)
853 + STRLEN(tmp_diff) + STRLEN(p_srr) + 27;
854 cmd = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 if (cmd != NULL)
856 {
Bram Moolenaar95fb60a2005-03-08 22:29:20 +0000857 /* We don't want $DIFF_OPTIONS to get in the way. */
858 if (getenv("DIFF_OPTIONS"))
859 vim_setenv((char_u *)"DIFF_OPTIONS", (char_u *)"");
860
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 /* Build the diff command and execute it. Always use -a, binary
862 * differences are of no use. Ignore errors, diff returns
863 * non-zero when differences have been found. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000864 vim_snprintf((char *)cmd, len, "diff %s%s%s%s%s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 diff_a_works == FALSE ? "" : "-a ",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100866#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 diff_bin_works == TRUE ? "--binary " : "",
868#else
869 "",
870#endif
871 (diff_flags & DIFF_IWHITE) ? "-b " : "",
872 (diff_flags & DIFF_ICASE) ? "-i " : "",
873 tmp_orig, tmp_new);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000874 append_redir(cmd, (int)len, p_srr, tmp_diff);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000875#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +0000876 block_autocmds(); /* Avoid ShellCmdPost stuff */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000877#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 (void)call_shell(cmd, SHELL_FILTER|SHELL_SILENT|SHELL_DOOUT);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000879#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +0000880 unblock_autocmds();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 vim_free(cmd);
883 }
884 }
885}
886
887/*
888 * Create a new version of a file from the current buffer and a diff file.
889 * The buffer is written to a file, also for unmodified buffers (the file
890 * could have been produced by autocommands, e.g. the netrw plugin).
891 */
892 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100893ex_diffpatch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894{
895 char_u *tmp_orig; /* name of original temp file */
896 char_u *tmp_new; /* name of patched temp file */
897 char_u *buf = NULL;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000898 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 win_T *old_curwin = curwin;
900 char_u *newname = NULL; /* name of patched file buffer */
901#ifdef UNIX
902 char_u dirbuf[MAXPATHL];
903 char_u *fullname = NULL;
904#endif
905#ifdef FEAT_BROWSE
906 char_u *browseFile = NULL;
907 int browse_flag = cmdmod.browse;
908#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200909 stat_T st;
Bram Moolenaara95ab322017-03-11 19:21:53 +0100910 char_u *esc_name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000911
912#ifdef FEAT_BROWSE
913 if (cmdmod.browse)
914 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000915 browseFile = do_browse(0, (char_u *)_("Patch file"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000916 eap->arg, NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
917 if (browseFile == NULL)
918 return; /* operation cancelled */
919 eap->arg = browseFile;
920 cmdmod.browse = FALSE; /* don't let do_ecmd() browse again */
921 }
922#endif
923
924 /* We need two temp file names. */
Bram Moolenaare5c421c2015-03-31 13:33:08 +0200925 tmp_orig = vim_tempname('o', FALSE);
926 tmp_new = vim_tempname('n', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 if (tmp_orig == NULL || tmp_new == NULL)
928 goto theend;
929
930 /* Write the current buffer to "tmp_orig". */
931 if (buf_write(curbuf, tmp_orig, NULL,
932 (linenr_T)1, curbuf->b_ml.ml_line_count,
933 NULL, FALSE, FALSE, FALSE, TRUE) == FAIL)
934 goto theend;
935
936#ifdef UNIX
937 /* Get the absolute path of the patchfile, changing directory below. */
938 fullname = FullName_save(eap->arg, FALSE);
939#endif
Bram Moolenaara95ab322017-03-11 19:21:53 +0100940 esc_name = vim_strsave_shellescape(
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941# ifdef UNIX
Bram Moolenaara95ab322017-03-11 19:21:53 +0100942 fullname != NULL ? fullname :
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943# endif
Bram Moolenaara95ab322017-03-11 19:21:53 +0100944 eap->arg, TRUE, TRUE);
945 if (esc_name == NULL)
946 goto theend;
947 buflen = STRLEN(tmp_orig) + STRLEN(esc_name) + STRLEN(tmp_new) + 16;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000948 buf = alloc((unsigned)buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 if (buf == NULL)
950 goto theend;
951
952#ifdef UNIX
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +0000953 /* Temporarily chdir to /tmp, to avoid patching files in the current
Bram Moolenaar071d4272004-06-13 20:20:40 +0000954 * directory when the patch file contains more than one patch. When we
955 * have our own temp dir use that instead, it will be cleaned up when we
956 * exit (any .rej files created). Don't change directory if we can't
957 * return to the current. */
958 if (mch_dirname(dirbuf, MAXPATHL) != OK || mch_chdir((char *)dirbuf) != 0)
959 dirbuf[0] = NUL;
960 else
961 {
962# ifdef TEMPDIRNAMES
963 if (vim_tempdir != NULL)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000964 ignored = mch_chdir((char *)vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965 else
966# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000967 ignored = mch_chdir("/tmp");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 shorten_fnames(TRUE);
969 }
970#endif
971
972#ifdef FEAT_EVAL
973 if (*p_pex != NUL)
974 /* Use 'patchexpr' to generate the new file. */
975 eval_patch(tmp_orig,
976# ifdef UNIX
977 fullname != NULL ? fullname :
978# endif
979 eap->arg, tmp_new);
980 else
981#endif
982 {
983 /* Build the patch command and execute it. Ignore errors. Switch to
984 * cooked mode to allow the user to respond to prompts. */
Bram Moolenaara95ab322017-03-11 19:21:53 +0100985 vim_snprintf((char *)buf, buflen, "patch -o %s %s < %s",
986 tmp_new, tmp_orig, esc_name);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000987#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +0000988 block_autocmds(); /* Avoid ShellCmdPost stuff */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 (void)call_shell(buf, SHELL_FILTER | SHELL_COOKED);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000991#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +0000992 unblock_autocmds();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000994 }
995
996#ifdef UNIX
997 if (dirbuf[0] != NUL)
998 {
999 if (mch_chdir((char *)dirbuf) != 0)
1000 EMSG(_(e_prev_dir));
1001 shorten_fnames(TRUE);
1002 }
1003#endif
1004
1005 /* patch probably has written over the screen */
1006 redraw_later(CLEAR);
1007
1008 /* Delete any .orig or .rej file created. */
1009 STRCPY(buf, tmp_new);
1010 STRCAT(buf, ".orig");
1011 mch_remove(buf);
1012 STRCPY(buf, tmp_new);
1013 STRCAT(buf, ".rej");
1014 mch_remove(buf);
1015
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001016 /* Only continue if the output file was created. */
1017 if (mch_stat((char *)tmp_new, &st) < 0 || st.st_size == 0)
1018 EMSG(_("E816: Cannot read patch output"));
1019 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001021 if (curbuf->b_fname != NULL)
1022 {
1023 newname = vim_strnsave(curbuf->b_fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 (int)(STRLEN(curbuf->b_fname) + 4));
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001025 if (newname != NULL)
1026 STRCAT(newname, ".new");
1027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028
1029#ifdef FEAT_GUI
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001030 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031#endif
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001032 /* don't use a new tab page, each tab page has its own diffs */
1033 cmdmod.tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001034
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001035 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001037 /* Pretend it was a ":split fname" command */
1038 eap->cmdidx = CMD_split;
1039 eap->arg = tmp_new;
1040 do_exedit(eap, old_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001042 /* check that split worked and editing tmp_new */
1043 if (curwin != old_curwin && win_valid(old_curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001045 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1046 diff_win_options(curwin, TRUE);
1047 diff_win_options(old_curwin, TRUE);
1048
1049 if (newname != NULL)
1050 {
1051 /* do a ":file filename.new" on the patched buffer */
1052 eap->arg = newname;
1053 ex_file(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054
1055#ifdef FEAT_AUTOCMD
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001056 /* Do filetype detection with the new name. */
1057 if (au_has_group((char_u *)"filetypedetect"))
1058 do_cmdline_cmd((char_u *)":doau filetypedetect BufRead");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059#endif
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001060 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061 }
1062 }
1063 }
1064
1065theend:
1066 if (tmp_orig != NULL)
1067 mch_remove(tmp_orig);
1068 vim_free(tmp_orig);
1069 if (tmp_new != NULL)
1070 mch_remove(tmp_new);
1071 vim_free(tmp_new);
1072 vim_free(newname);
1073 vim_free(buf);
1074#ifdef UNIX
1075 vim_free(fullname);
1076#endif
Bram Moolenaara95ab322017-03-11 19:21:53 +01001077 vim_free(esc_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078#ifdef FEAT_BROWSE
1079 vim_free(browseFile);
1080 cmdmod.browse = browse_flag;
1081#endif
1082}
1083
1084/*
1085 * Split the window and edit another file, setting options to show the diffs.
1086 */
1087 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001088ex_diffsplit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089{
1090 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001091 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001093 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094#ifdef FEAT_GUI
1095 need_mouse_correct = TRUE;
1096#endif
Bram Moolenaar46328f92016-08-28 15:39:57 +02001097 /* Need to compute w_fraction when no redraw happened yet. */
1098 validate_cursor();
1099 set_fraction(curwin);
1100
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001101 /* don't use a new tab page, each tab page has its own diffs */
1102 cmdmod.tab = 0;
1103
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001104 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 {
1106 /* Pretend it was a ":split fname" command */
1107 eap->cmdidx = CMD_split;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 curwin->w_p_diff = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 do_exedit(eap, old_curwin);
1110
1111 if (curwin != old_curwin) /* split must have worked */
1112 {
1113 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1114 diff_win_options(curwin, TRUE);
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001115 if (win_valid(old_curwin))
1116 {
1117 diff_win_options(old_curwin, TRUE);
1118
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001119 if (bufref_valid(&old_curbuf))
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001120 /* Move the cursor position to that of the old window. */
1121 curwin->w_cursor.lnum = diff_get_corresponding_line(
Bram Moolenaar025e3e02016-10-18 14:50:18 +02001122 old_curbuf.br_buf, old_curwin->w_cursor.lnum);
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001123 }
Bram Moolenaar46328f92016-08-28 15:39:57 +02001124 /* Now that lines are folded scroll to show the cursor at the same
1125 * relative position. */
1126 scroll_to_fraction(curwin, curwin->w_height);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 }
1128 }
1129}
1130
1131/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001132 * Set options to show diffs for the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001135ex_diffthis(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136{
1137 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1138 diff_win_options(curwin, TRUE);
1139}
1140
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001141 static void
1142set_diff_option(win_T *wp, int value)
1143{
1144 win_T *old_curwin = curwin;
1145
1146 curwin = wp;
1147 curbuf = curwin->w_buffer;
1148 ++curbuf_lock;
1149 set_option_value((char_u *)"diff", (long)value, NULL, OPT_LOCAL);
1150 --curbuf_lock;
1151 curwin = old_curwin;
1152 curbuf = curwin->w_buffer;
1153}
1154
Bram Moolenaar071d4272004-06-13 20:20:40 +00001155/*
1156 * Set options in window "wp" for diff mode.
1157 */
1158 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001159diff_win_options(
1160 win_T *wp,
1161 int addbuf) /* Add buffer to diff. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162{
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001163# ifdef FEAT_FOLDING
1164 win_T *old_curwin = curwin;
1165
1166 /* close the manually opened folds */
1167 curwin = wp;
1168 newFoldLevel();
1169 curwin = old_curwin;
1170# endif
1171
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001172 /* Use 'scrollbind' and 'cursorbind' when available */
1173#ifdef FEAT_SCROLLBIND
Bram Moolenaar43929962015-07-03 15:06:56 +02001174 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001175 wp->w_p_scb_save = wp->w_p_scb;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001176 wp->w_p_scb = TRUE;
1177#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02001178#ifdef FEAT_CURSORBIND
Bram Moolenaar43929962015-07-03 15:06:56 +02001179 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001180 wp->w_p_crb_save = wp->w_p_crb;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001181 wp->w_p_crb = TRUE;
1182#endif
Bram Moolenaar43929962015-07-03 15:06:56 +02001183 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001184 wp->w_p_wrap_save = wp->w_p_wrap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 wp->w_p_wrap = FALSE;
1186# ifdef FEAT_FOLDING
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001187 curwin = wp;
1188 curbuf = curwin->w_buffer;
Bram Moolenaar43929962015-07-03 15:06:56 +02001189 if (!wp->w_p_diff)
1190 {
1191 if (wp->w_p_diff_saved)
1192 free_string_option(wp->w_p_fdm_save);
Bram Moolenaara87aa802013-07-03 15:47:03 +02001193 wp->w_p_fdm_save = vim_strsave(wp->w_p_fdm);
Bram Moolenaar43929962015-07-03 15:06:56 +02001194 }
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001195 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"diff",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001196 OPT_LOCAL|OPT_FREE, 0);
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001197 curwin = old_curwin;
1198 curbuf = curwin->w_buffer;
Bram Moolenaar43929962015-07-03 15:06:56 +02001199 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001200 {
1201 wp->w_p_fdc_save = wp->w_p_fdc;
1202 wp->w_p_fen_save = wp->w_p_fen;
1203 wp->w_p_fdl_save = wp->w_p_fdl;
1204 }
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001205 wp->w_p_fdc = diff_foldcolumn;
1206 wp->w_p_fen = TRUE;
1207 wp->w_p_fdl = 0;
1208 foldUpdateAll(wp);
1209 /* make sure topline is not halfway a fold */
1210 changed_window_setting_win(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211# endif
1212#ifdef FEAT_SCROLLBIND
1213 if (vim_strchr(p_sbo, 'h') == NULL)
1214 do_cmdline_cmd((char_u *)"set sbo+=hor");
1215#endif
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001216 /* Save the current values, to be restored in ex_diffoff(). */
Bram Moolenaara87aa802013-07-03 15:47:03 +02001217 wp->w_p_diff_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001219 set_diff_option(wp, TRUE);
Bram Moolenaar43929962015-07-03 15:06:56 +02001220
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 if (addbuf)
1222 diff_buf_add(wp->w_buffer);
1223 redraw_win_later(wp, NOT_VALID);
1224}
1225
1226/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001227 * Set options not to show diffs. For the current window or all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001228 * Only in the current tab page.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001229 */
1230 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001231ex_diffoff(exarg_T *eap)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001232{
1233 win_T *wp;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001234#ifdef FEAT_SCROLLBIND
1235 int diffwin = FALSE;
1236#endif
1237
Bram Moolenaar29323592016-07-24 22:04:11 +02001238 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001239 {
Bram Moolenaar00462ff2013-09-20 20:13:53 +02001240 if (eap->forceit ? wp->w_p_diff : wp == curwin)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001241 {
Bram Moolenaar43929962015-07-03 15:06:56 +02001242 /* Set 'diff' off. If option values were saved in
1243 * diff_win_options(), restore the ones whose settings seem to have
1244 * been left over from diff mode. */
Bram Moolenaar04f62f82017-07-19 18:18:39 +02001245 set_diff_option(wp, FALSE);
Bram Moolenaara87aa802013-07-03 15:47:03 +02001246
Bram Moolenaara87aa802013-07-03 15:47:03 +02001247 if (wp->w_p_diff_saved)
1248 {
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001249
Bram Moolenaar43929962015-07-03 15:06:56 +02001250#ifdef FEAT_SCROLLBIND
1251 if (wp->w_p_scb)
1252 wp->w_p_scb = wp->w_p_scb_save;
1253#endif
1254#ifdef FEAT_CURSORBIND
1255 if (wp->w_p_crb)
1256 wp->w_p_crb = wp->w_p_crb_save;
1257#endif
1258 if (!wp->w_p_wrap)
1259 wp->w_p_wrap = wp->w_p_wrap_save;
1260#ifdef FEAT_FOLDING
1261 free_string_option(wp->w_p_fdm);
Bram Moolenaar79a213d2017-05-16 13:15:18 +02001262 wp->w_p_fdm = vim_strsave(
1263 *wp->w_p_fdm_save ? wp->w_p_fdm_save : (char_u*)"manual");
Bram Moolenaar43929962015-07-03 15:06:56 +02001264
1265 if (wp->w_p_fdc == diff_foldcolumn)
1266 wp->w_p_fdc = wp->w_p_fdc_save;
1267 if (wp->w_p_fdl == 0)
1268 wp->w_p_fdl = wp->w_p_fdl_save;
1269
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001270 /* Only restore 'foldenable' when 'foldmethod' is not
1271 * "manual", otherwise we continue to show the diff folds. */
Bram Moolenaar43929962015-07-03 15:06:56 +02001272 if (wp->w_p_fen)
1273 wp->w_p_fen = foldmethodIsManual(wp) ? FALSE
1274 : wp->w_p_fen_save;
1275
1276 foldUpdateAll(wp);
Bram Moolenaar43929962015-07-03 15:06:56 +02001277#endif
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001278 }
Bram Moolenaare67d5462016-08-27 22:40:42 +02001279 /* remove filler lines */
1280 wp->w_topfill = 0;
1281
1282 /* make sure topline is not halfway a fold and cursor is
1283 * invalidated */
1284 changed_window_setting_win(wp);
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001285
Bram Moolenaara87aa802013-07-03 15:47:03 +02001286 /* Note: 'sbo' is not restored, it's a global option. */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001287 diff_buf_adjust(wp);
1288 }
1289#ifdef FEAT_SCROLLBIND
1290 diffwin |= wp->w_p_diff;
1291#endif
1292 }
1293
Bram Moolenaar25ea0542017-02-03 23:16:28 +01001294 /* Also remove hidden buffers from the list. */
1295 if (eap->forceit)
1296 diff_buf_clear();
1297
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001298#ifdef FEAT_SCROLLBIND
1299 /* Remove "hor" from from 'scrollopt' if there are no diff windows left. */
1300 if (!diffwin && vim_strchr(p_sbo, 'h') != NULL)
1301 do_cmdline_cmd((char_u *)"set sbo-=hor");
1302#endif
1303}
1304
1305/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 * Read the diff output and add each entry to the diff list.
1307 */
1308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001309diff_read(
1310 int idx_orig, /* idx of original file */
1311 int idx_new, /* idx of new file */
1312 char_u *fname) /* name of diff output file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
1314 FILE *fd;
1315 diff_T *dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001316 diff_T *dp = curtab->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 diff_T *dn, *dpl;
1318 long f1, l1, f2, l2;
1319 char_u linebuf[LBUFLEN]; /* only need to hold the diff line */
1320 int difftype;
1321 char_u *p;
1322 long off;
1323 int i;
1324 linenr_T lnum_orig, lnum_new;
1325 long count_orig, count_new;
1326 int notset = TRUE; /* block "*dp" not set yet */
1327
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001328 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 if (fd == NULL)
1330 {
1331 EMSG(_("E98: Cannot read diff output"));
1332 return;
1333 }
1334
1335 for (;;)
1336 {
1337 if (tag_fgets(linebuf, LBUFLEN, fd))
1338 break; /* end of file */
1339 if (!isdigit(*linebuf))
1340 continue; /* not the start of a diff block */
1341
1342 /* This line must be one of three formats:
1343 * {first}[,{last}]c{first}[,{last}]
1344 * {first}a{first}[,{last}]
1345 * {first}[,{last}]d{first}
1346 */
1347 p = linebuf;
1348 f1 = getdigits(&p);
1349 if (*p == ',')
1350 {
1351 ++p;
1352 l1 = getdigits(&p);
1353 }
1354 else
1355 l1 = f1;
1356 if (*p != 'a' && *p != 'c' && *p != 'd')
1357 continue; /* invalid diff format */
1358 difftype = *p++;
1359 f2 = getdigits(&p);
1360 if (*p == ',')
1361 {
1362 ++p;
1363 l2 = getdigits(&p);
1364 }
1365 else
1366 l2 = f2;
1367 if (l1 < f1 || l2 < f2)
1368 continue; /* invalid line range */
1369
1370 if (difftype == 'a')
1371 {
1372 lnum_orig = f1 + 1;
1373 count_orig = 0;
1374 }
1375 else
1376 {
1377 lnum_orig = f1;
1378 count_orig = l1 - f1 + 1;
1379 }
1380 if (difftype == 'd')
1381 {
1382 lnum_new = f2 + 1;
1383 count_new = 0;
1384 }
1385 else
1386 {
1387 lnum_new = f2;
1388 count_new = l2 - f2 + 1;
1389 }
1390
1391 /* Go over blocks before the change, for which orig and new are equal.
1392 * Copy blocks from orig to new. */
1393 while (dp != NULL
1394 && lnum_orig > dp->df_lnum[idx_orig] + dp->df_count[idx_orig])
1395 {
1396 if (notset)
1397 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1398 dprev = dp;
1399 dp = dp->df_next;
1400 notset = TRUE;
1401 }
1402
1403 if (dp != NULL
1404 && lnum_orig <= dp->df_lnum[idx_orig] + dp->df_count[idx_orig]
1405 && lnum_orig + count_orig >= dp->df_lnum[idx_orig])
1406 {
1407 /* New block overlaps with existing block(s).
1408 * First find last block that overlaps. */
1409 for (dpl = dp; dpl->df_next != NULL; dpl = dpl->df_next)
1410 if (lnum_orig + count_orig < dpl->df_next->df_lnum[idx_orig])
1411 break;
1412
1413 /* If the newly found block starts before the old one, set the
1414 * start back a number of lines. */
1415 off = dp->df_lnum[idx_orig] - lnum_orig;
1416 if (off > 0)
1417 {
1418 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001419 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 dp->df_lnum[i] -= off;
1421 dp->df_lnum[idx_new] = lnum_new;
1422 dp->df_count[idx_new] = count_new;
1423 }
1424 else if (notset)
1425 {
1426 /* new block inside existing one, adjust new block */
1427 dp->df_lnum[idx_new] = lnum_new + off;
1428 dp->df_count[idx_new] = count_new - off;
1429 }
1430 else
1431 /* second overlap of new block with existing block */
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001432 dp->df_count[idx_new] += count_new - count_orig
1433 + dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]
1434 - (dp->df_lnum[idx_orig] + dp->df_count[idx_orig]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435
1436 /* Adjust the size of the block to include all the lines to the
1437 * end of the existing block or the new diff, whatever ends last. */
1438 off = (lnum_orig + count_orig)
1439 - (dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]);
1440 if (off < 0)
1441 {
1442 /* new change ends in existing block, adjust the end if not
1443 * done already */
1444 if (notset)
1445 dp->df_count[idx_new] += -off;
1446 off = 0;
1447 }
Bram Moolenaard4b96bc2007-10-19 15:33:39 +00001448 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001449 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 dp->df_count[i] = dpl->df_lnum[i] + dpl->df_count[i]
1451 - dp->df_lnum[i] + off;
1452
1453 /* Delete the diff blocks that have been merged into one. */
1454 dn = dp->df_next;
1455 dp->df_next = dpl->df_next;
1456 while (dn != dp->df_next)
1457 {
1458 dpl = dn->df_next;
1459 vim_free(dn);
1460 dn = dpl;
1461 }
1462 }
1463 else
1464 {
1465 /* Allocate a new diffblock. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001466 dp = diff_alloc_new(curtab, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 if (dp == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001468 goto done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469
1470 dp->df_lnum[idx_orig] = lnum_orig;
1471 dp->df_count[idx_orig] = count_orig;
1472 dp->df_lnum[idx_new] = lnum_new;
1473 dp->df_count[idx_new] = count_new;
1474
1475 /* Set values for other buffers, these must be equal to the
1476 * original buffer, otherwise there would have been a change
1477 * already. */
1478 for (i = idx_orig + 1; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001479 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 diff_copy_entry(dprev, dp, idx_orig, i);
1481 }
1482 notset = FALSE; /* "*dp" has been set */
1483 }
1484
1485 /* for remaining diff blocks orig and new are equal */
1486 while (dp != NULL)
1487 {
1488 if (notset)
1489 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1490 dprev = dp;
1491 dp = dp->df_next;
1492 notset = TRUE;
1493 }
1494
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001495done:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 fclose(fd);
1497}
1498
1499/*
1500 * Copy an entry at "dp" from "idx_orig" to "idx_new".
1501 */
1502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001503diff_copy_entry(
1504 diff_T *dprev,
1505 diff_T *dp,
1506 int idx_orig,
1507 int idx_new)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001508{
1509 long off;
1510
1511 if (dprev == NULL)
1512 off = 0;
1513 else
1514 off = (dprev->df_lnum[idx_orig] + dprev->df_count[idx_orig])
1515 - (dprev->df_lnum[idx_new] + dprev->df_count[idx_new]);
1516 dp->df_lnum[idx_new] = dp->df_lnum[idx_orig] - off;
1517 dp->df_count[idx_new] = dp->df_count[idx_orig];
1518}
1519
1520/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001521 * Clear the list of diffblocks for tab page "tp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001522 */
Bram Moolenaarea408852005-06-25 22:49:46 +00001523 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524diff_clear(tabpage_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525{
1526 diff_T *p, *next_p;
1527
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001528 for (p = tp->tp_first_diff; p != NULL; p = next_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 {
1530 next_p = p->df_next;
1531 vim_free(p);
1532 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001533 tp->tp_first_diff = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001534}
1535
1536/*
1537 * Check diff status for line "lnum" in buffer "buf":
1538 * Returns 0 for nothing special
1539 * Returns -1 for a line that should be highlighted as changed.
1540 * Returns -2 for a line that should be highlighted as added/deleted.
1541 * Returns > 0 for inserting that many filler lines above it (never happens
1542 * when 'diffopt' doesn't contain "filler").
1543 * This should only be used for windows where 'diff' is set.
1544 */
1545 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001546diff_check(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001547{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001548 int idx; /* index in tp_diffbuf[] for this buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001549 diff_T *dp;
1550 int maxcount;
1551 int i;
1552 buf_T *buf = wp->w_buffer;
1553 int cmp;
1554
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001555 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001556 ex_diffupdate(NULL); /* update after a big change */
1557
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001558 if (curtab->tp_first_diff == NULL || !wp->w_p_diff) /* no diffs at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 return 0;
1560
1561 /* safety check: "lnum" must be a buffer line */
1562 if (lnum < 1 || lnum > buf->b_ml.ml_line_count + 1)
1563 return 0;
1564
1565 idx = diff_buf_idx(buf);
1566 if (idx == DB_COUNT)
1567 return 0; /* no diffs for buffer "buf" */
1568
1569#ifdef FEAT_FOLDING
1570 /* A closed fold never has filler lines. */
1571 if (hasFoldingWin(wp, lnum, NULL, NULL, TRUE, NULL))
1572 return 0;
1573#endif
1574
1575 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001576 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001577 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
1578 break;
1579 if (dp == NULL || lnum < dp->df_lnum[idx])
1580 return 0;
1581
1582 if (lnum < dp->df_lnum[idx] + dp->df_count[idx])
1583 {
1584 int zero = FALSE;
1585
1586 /* Changed or inserted line. If the other buffers have a count of
1587 * zero, the lines were inserted. If the other buffers have the same
1588 * count, check if the lines are identical. */
1589 cmp = FALSE;
1590 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001591 if (i != idx && curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592 {
1593 if (dp->df_count[i] == 0)
1594 zero = TRUE;
1595 else
1596 {
1597 if (dp->df_count[i] != dp->df_count[idx])
1598 return -1; /* nr of lines changed. */
1599 cmp = TRUE;
1600 }
1601 }
1602 if (cmp)
1603 {
1604 /* Compare all lines. If they are equal the lines were inserted
1605 * in some buffers, deleted in others, but not changed. */
1606 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001607 if (i != idx && curtab->tp_diffbuf[i] != NULL
1608 && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001609 if (!diff_equal_entry(dp, idx, i))
1610 return -1;
1611 }
1612 /* If there is no buffer with zero lines then there is no difference
1613 * any longer. Happens when making a change (or undo) that removes
1614 * the difference. Can't remove the entry here, we might be halfway
1615 * updating the window. Just report the text as unchanged. Other
1616 * windows might still show the change though. */
1617 if (zero == FALSE)
1618 return 0;
1619 return -2;
1620 }
1621
1622 /* If 'diffopt' doesn't contain "filler", return 0. */
1623 if (!(diff_flags & DIFF_FILLER))
1624 return 0;
1625
1626 /* Insert filler lines above the line just below the change. Will return
1627 * 0 when this buf had the max count. */
1628 maxcount = 0;
1629 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001630 if (curtab->tp_diffbuf[i] != NULL && dp->df_count[i] > maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 maxcount = dp->df_count[i];
1632 return maxcount - dp->df_count[idx];
1633}
1634
1635/*
1636 * Compare two entries in diff "*dp" and return TRUE if they are equal.
1637 */
1638 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001639diff_equal_entry(diff_T *dp, int idx1, int idx2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640{
1641 int i;
1642 char_u *line;
1643 int cmp;
1644
1645 if (dp->df_count[idx1] != dp->df_count[idx2])
1646 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001647 if (diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 return FALSE;
1649 for (i = 0; i < dp->df_count[idx1]; ++i)
1650 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001651 line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652 dp->df_lnum[idx1] + i, FALSE));
1653 if (line == NULL)
1654 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001655 cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 dp->df_lnum[idx2] + i, FALSE));
1657 vim_free(line);
1658 if (cmp != 0)
1659 return FALSE;
1660 }
1661 return TRUE;
1662}
1663
1664/*
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02001665 * Compare the characters at "p1" and "p2". If they are equal (possibly
1666 * ignoring case) return TRUE and set "len" to the number of bytes.
1667 */
1668 static int
1669diff_equal_char(char_u *p1, char_u *p2, int *len)
1670{
1671#ifdef FEAT_MBYTE
1672 int l = (*mb_ptr2len)(p1);
1673
1674 if (l != (*mb_ptr2len)(p2))
1675 return FALSE;
1676 if (l > 1)
1677 {
1678 if (STRNCMP(p1, p2, l) != 0
1679 && (!enc_utf8
1680 || !(diff_flags & DIFF_ICASE)
1681 || utf_fold(utf_ptr2char(p1))
1682 != utf_fold(utf_ptr2char(p2))))
1683 return FALSE;
1684 *len = l;
1685 }
1686 else
1687#endif
1688 {
1689 if ((*p1 != *p2)
1690 && (!(diff_flags & DIFF_ICASE)
1691 || TOLOWER_LOC(*p1) != TOLOWER_LOC(*p2)))
1692 return FALSE;
1693 *len = 1;
1694 }
1695 return TRUE;
1696}
1697
1698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699 * Compare strings "s1" and "s2" according to 'diffopt'.
1700 * Return non-zero when they are different.
1701 */
1702 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001703diff_cmp(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704{
1705 char_u *p1, *p2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001706 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707
1708 if ((diff_flags & (DIFF_ICASE | DIFF_IWHITE)) == 0)
1709 return STRCMP(s1, s2);
1710 if ((diff_flags & DIFF_ICASE) && !(diff_flags & DIFF_IWHITE))
1711 return MB_STRICMP(s1, s2);
1712
1713 /* Ignore white space changes and possibly ignore case. */
1714 p1 = s1;
1715 p2 = s2;
1716 while (*p1 != NUL && *p2 != NUL)
1717 {
Bram Moolenaar1c465442017-03-12 20:10:05 +01001718 if (VIM_ISWHITE(*p1) && VIM_ISWHITE(*p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 {
1720 p1 = skipwhite(p1);
1721 p2 = skipwhite(p2);
1722 }
1723 else
1724 {
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02001725 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726 break;
Bram Moolenaarae96b8d2017-09-03 15:04:21 +02001727 p1 += l;
1728 p2 += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001729 }
1730 }
1731
1732 /* Ignore trailing white space. */
1733 p1 = skipwhite(p1);
1734 p2 = skipwhite(p2);
1735 if (*p1 != NUL || *p2 != NUL)
1736 return 1;
1737 return 0;
1738}
1739
1740/*
1741 * Return the number of filler lines above "lnum".
1742 */
1743 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001744diff_check_fill(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745{
1746 int n;
1747
1748 /* be quick when there are no filler lines */
1749 if (!(diff_flags & DIFF_FILLER))
1750 return 0;
1751 n = diff_check(wp, lnum);
1752 if (n <= 0)
1753 return 0;
1754 return n;
1755}
1756
1757/*
1758 * Set the topline of "towin" to match the position in "fromwin", so that they
1759 * show the same diff'ed lines.
1760 */
1761 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001762diff_set_topline(win_T *fromwin, win_T *towin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763{
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001764 buf_T *frombuf = fromwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001765 linenr_T lnum = fromwin->w_topline;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001766 int fromidx;
1767 int toidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001768 diff_T *dp;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001769 int max_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001770 int i;
1771
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001772 fromidx = diff_buf_idx(frombuf);
1773 if (fromidx == DB_COUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001774 return; /* safety check */
1775
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001776 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777 ex_diffupdate(NULL); /* update after a big change */
1778
1779 towin->w_topfill = 0;
1780
1781 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001782 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001783 if (lnum <= dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 break;
1785 if (dp == NULL)
1786 {
1787 /* After last change, compute topline relative to end of file; no
1788 * filler lines. */
1789 towin->w_topline = towin->w_buffer->b_ml.ml_line_count
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001790 - (frombuf->b_ml.ml_line_count - lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 }
1792 else
1793 {
1794 /* Find index for "towin". */
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001795 toidx = diff_buf_idx(towin->w_buffer);
1796 if (toidx == DB_COUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797 return; /* safety check */
1798
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001799 towin->w_topline = lnum + (dp->df_lnum[toidx] - dp->df_lnum[fromidx]);
1800 if (lnum >= dp->df_lnum[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001802 /* Inside a change: compute filler lines. With three or more
1803 * buffers we need to know the largest count. */
1804 max_count = 0;
1805 for (i = 0; i < DB_COUNT; ++i)
1806 if (curtab->tp_diffbuf[i] != NULL
1807 && max_count < dp->df_count[i])
1808 max_count = dp->df_count[i];
1809
1810 if (dp->df_count[toidx] == dp->df_count[fromidx])
1811 {
1812 /* same number of lines: use same filler count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 towin->w_topfill = fromwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814 }
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001815 else if (dp->df_count[toidx] > dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001817 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001819 /* more lines in towin and fromwin doesn't show diff
1820 * lines, only filler lines */
1821 if (max_count - fromwin->w_topfill >= dp->df_count[toidx])
1822 {
1823 /* towin also only shows filler lines */
1824 towin->w_topline = dp->df_lnum[toidx]
1825 + dp->df_count[toidx];
1826 towin->w_topfill = fromwin->w_topfill;
1827 }
1828 else
1829 /* towin still has some diff lines to show */
1830 towin->w_topline = dp->df_lnum[toidx]
1831 + max_count - fromwin->w_topfill;
1832 }
1833 }
1834 else if (towin->w_topline >= dp->df_lnum[toidx]
1835 + dp->df_count[toidx])
1836 {
1837 /* less lines in towin and no diff lines to show: compute
1838 * filler lines */
1839 towin->w_topline = dp->df_lnum[toidx] + dp->df_count[toidx];
1840 if (diff_flags & DIFF_FILLER)
1841 {
1842 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
1843 /* fromwin is also out of diff lines */
1844 towin->w_topfill = fromwin->w_topfill;
1845 else
1846 /* fromwin has some diff lines */
1847 towin->w_topfill = dp->df_lnum[fromidx]
1848 + max_count - lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 }
1850 }
1851 }
1852 }
1853
1854 /* safety check (if diff info gets outdated strange things may happen) */
1855 towin->w_botfill = FALSE;
1856 if (towin->w_topline > towin->w_buffer->b_ml.ml_line_count)
1857 {
1858 towin->w_topline = towin->w_buffer->b_ml.ml_line_count;
1859 towin->w_botfill = TRUE;
1860 }
1861 if (towin->w_topline < 1)
1862 {
1863 towin->w_topline = 1;
1864 towin->w_topfill = 0;
1865 }
1866
1867 /* When w_topline changes need to recompute w_botline and cursor position */
1868 invalidate_botline_win(towin);
1869 changed_line_abv_curs_win(towin);
1870
1871 check_topfill(towin, FALSE);
1872#ifdef FEAT_FOLDING
1873 (void)hasFoldingWin(towin, towin->w_topline, &towin->w_topline,
1874 NULL, TRUE, NULL);
1875#endif
1876}
1877
1878/*
1879 * This is called when 'diffopt' is changed.
1880 */
1881 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001882diffopt_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883{
1884 char_u *p;
1885 int diff_context_new = 6;
1886 int diff_flags_new = 0;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001887 int diff_foldcolumn_new = 2;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001888 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889
1890 p = p_dip;
1891 while (*p != NUL)
1892 {
1893 if (STRNCMP(p, "filler", 6) == 0)
1894 {
1895 p += 6;
1896 diff_flags_new |= DIFF_FILLER;
1897 }
1898 else if (STRNCMP(p, "context:", 8) == 0 && VIM_ISDIGIT(p[8]))
1899 {
1900 p += 8;
1901 diff_context_new = getdigits(&p);
1902 }
1903 else if (STRNCMP(p, "icase", 5) == 0)
1904 {
1905 p += 5;
1906 diff_flags_new |= DIFF_ICASE;
1907 }
1908 else if (STRNCMP(p, "iwhite", 6) == 0)
1909 {
1910 p += 6;
1911 diff_flags_new |= DIFF_IWHITE;
1912 }
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001913 else if (STRNCMP(p, "horizontal", 10) == 0)
1914 {
1915 p += 10;
1916 diff_flags_new |= DIFF_HORIZONTAL;
1917 }
1918 else if (STRNCMP(p, "vertical", 8) == 0)
1919 {
1920 p += 8;
1921 diff_flags_new |= DIFF_VERTICAL;
1922 }
1923 else if (STRNCMP(p, "foldcolumn:", 11) == 0 && VIM_ISDIGIT(p[11]))
1924 {
1925 p += 11;
1926 diff_foldcolumn_new = getdigits(&p);
1927 }
Bram Moolenaar97ce4192017-12-01 20:35:58 +01001928 else if (STRNCMP(p, "hiddenoff", 9) == 0)
1929 {
1930 p += 9;
1931 diff_flags_new |= DIFF_HIDDEN_OFF;
1932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 if (*p != ',' && *p != NUL)
1934 return FAIL;
1935 if (*p == ',')
1936 ++p;
1937 }
1938
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001939 /* Can't have both "horizontal" and "vertical". */
1940 if ((diff_flags_new & DIFF_HORIZONTAL) && (diff_flags_new & DIFF_VERTICAL))
1941 return FAIL;
1942
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 /* If "icase" or "iwhite" was added or removed, need to update the diff. */
1944 if (diff_flags != diff_flags_new)
Bram Moolenaar29323592016-07-24 22:04:11 +02001945 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001946 tp->tp_diff_invalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947
1948 diff_flags = diff_flags_new;
1949 diff_context = diff_context_new;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001950 diff_foldcolumn = diff_foldcolumn_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951
1952 diff_redraw(TRUE);
1953
1954 /* recompute the scroll binding with the new option value, may
1955 * remove or add filler lines */
1956 check_scrollbind((linenr_T)0, 0L);
1957
1958 return OK;
1959}
1960
1961/*
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001962 * Return TRUE if 'diffopt' contains "horizontal".
1963 */
1964 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001965diffopt_horizontal(void)
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001966{
1967 return (diff_flags & DIFF_HORIZONTAL) != 0;
1968}
1969
1970/*
Bram Moolenaar97ce4192017-12-01 20:35:58 +01001971 * Return TRUE if 'diffopt' contains "hiddenoff".
1972 */
1973 int
1974diffopt_hiddenoff(void)
1975{
1976 return (diff_flags & DIFF_HIDDEN_OFF) != 0;
1977}
1978
1979/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 * Find the difference within a changed line.
1981 * Returns TRUE if the line was added, no other buffer has it.
1982 */
1983 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001984diff_find_change(
1985 win_T *wp,
1986 linenr_T lnum,
1987 int *startp, /* first char of the change */
1988 int *endp) /* last char of the change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989{
1990 char_u *line_org;
1991 char_u *line_new;
1992 int i;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001993 int si_org, si_new;
1994 int ei_org, ei_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 diff_T *dp;
1996 int idx;
1997 int off;
1998 int added = TRUE;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02001999 char_u *p1, *p2;
2000 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001
2002 /* Make a copy of the line, the next ml_get() will invalidate it. */
2003 line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE));
2004 if (line_org == NULL)
2005 return FALSE;
2006
2007 idx = diff_buf_idx(wp->w_buffer);
2008 if (idx == DB_COUNT) /* cannot happen */
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002009 {
2010 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002011 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002012 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002013
2014 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002015 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002016 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
2017 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002018 if (dp == NULL || diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002019 {
2020 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023
2024 off = lnum - dp->df_lnum[idx];
2025
2026 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002027 if (curtab->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 {
2029 /* Skip lines that are not in the other change (filler lines). */
2030 if (off >= dp->df_count[i])
2031 continue;
2032 added = FALSE;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002033 line_new = ml_get_buf(curtab->tp_diffbuf[i],
2034 dp->df_lnum[i] + off, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002035
2036 /* Search for start of difference */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002037 si_org = si_new = 0;
2038 while (line_org[si_org] != NUL)
2039 {
2040 if ((diff_flags & DIFF_IWHITE)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002041 && VIM_ISWHITE(line_org[si_org])
2042 && VIM_ISWHITE(line_new[si_new]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002043 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002044 si_org = (int)(skipwhite(line_org + si_org) - line_org);
2045 si_new = (int)(skipwhite(line_new + si_new) - line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002046 }
2047 else
2048 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002049 if (!diff_equal_char(line_org + si_org, line_new + si_new,
2050 &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002051 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002052 si_org += l;
2053 si_new += l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002054 }
2055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056#ifdef FEAT_MBYTE
2057 if (has_mbyte)
2058 {
2059 /* Move back to first byte of character in both lines (may
2060 * have "nn^" in line_org and "n^ in line_new). */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002061 si_org -= (*mb_head_off)(line_org, line_org + si_org);
2062 si_new -= (*mb_head_off)(line_new, line_new + si_new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 }
2064#endif
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002065 if (*startp > si_org)
2066 *startp = si_org;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067
2068 /* Search for end of difference, if any. */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002069 if (line_org[si_org] != NUL || line_new[si_new] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 {
2071 ei_org = (int)STRLEN(line_org);
2072 ei_new = (int)STRLEN(line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002073 while (ei_org >= *startp && ei_new >= si_new
2074 && ei_org >= 0 && ei_new >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002076 if ((diff_flags & DIFF_IWHITE)
Bram Moolenaar1c465442017-03-12 20:10:05 +01002077 && VIM_ISWHITE(line_org[ei_org])
2078 && VIM_ISWHITE(line_new[ei_new]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002079 {
2080 while (ei_org >= *startp
Bram Moolenaar1c465442017-03-12 20:10:05 +01002081 && VIM_ISWHITE(line_org[ei_org]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002082 --ei_org;
2083 while (ei_new >= si_new
Bram Moolenaar1c465442017-03-12 20:10:05 +01002084 && VIM_ISWHITE(line_new[ei_new]))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002085 --ei_new;
2086 }
2087 else
2088 {
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002089 p1 = line_org + ei_org;
2090 p2 = line_new + ei_new;
2091#ifdef FEAT_MBYTE
2092 p1 -= (*mb_head_off)(line_org, p1);
2093 p2 -= (*mb_head_off)(line_new, p2);
2094#endif
2095 if (!diff_equal_char(p1, p2, &l))
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002096 break;
Bram Moolenaarda22b8c2017-09-02 18:01:50 +02002097 ei_org -= l;
2098 ei_new -= l;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002099 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002100 }
2101 if (*endp < ei_org)
2102 *endp = ei_org;
2103 }
2104 }
2105
2106 vim_free(line_org);
2107 return added;
2108}
2109
2110#if defined(FEAT_FOLDING) || defined(PROTO)
2111/*
2112 * Return TRUE if line "lnum" is not close to a diff block, this line should
2113 * be in a fold.
2114 * Return FALSE if there are no diff blocks at all in this window.
2115 */
2116 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002117diff_infold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118{
2119 int i;
2120 int idx = -1;
2121 int other = FALSE;
2122 diff_T *dp;
2123
2124 /* Return if 'diff' isn't set. */
2125 if (!wp->w_p_diff)
2126 return FALSE;
2127
2128 for (i = 0; i < DB_COUNT; ++i)
2129 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002130 if (curtab->tp_diffbuf[i] == wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002131 idx = i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002132 else if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002133 other = TRUE;
2134 }
2135
2136 /* return here if there are no diffs in the window */
2137 if (idx == -1 || !other)
2138 return FALSE;
2139
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002140 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 ex_diffupdate(NULL); /* update after a big change */
2142
2143 /* Return if there are no diff blocks. All lines will be folded. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002144 if (curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 return TRUE;
2146
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002147 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 {
2149 /* If this change is below the line there can't be any further match. */
2150 if (dp->df_lnum[idx] - diff_context > lnum)
2151 break;
2152 /* If this change ends before the line we have a match. */
2153 if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum)
2154 return FALSE;
2155 }
2156 return TRUE;
2157}
2158#endif
2159
2160/*
2161 * "dp" and "do" commands.
2162 */
2163 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002164nv_diffgetput(int put, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165{
2166 exarg_T ea;
Bram Moolenaar6a643652014-10-31 13:54:25 +01002167 char_u buf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168
Bram Moolenaar6a643652014-10-31 13:54:25 +01002169 if (count == 0)
2170 ea.arg = (char_u *)"";
2171 else
2172 {
2173 vim_snprintf((char *)buf, 30, "%ld", count);
2174 ea.arg = buf;
2175 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 if (put)
2177 ea.cmdidx = CMD_diffput;
2178 else
2179 ea.cmdidx = CMD_diffget;
2180 ea.addr_count = 0;
2181 ea.line1 = curwin->w_cursor.lnum;
2182 ea.line2 = curwin->w_cursor.lnum;
2183 ex_diffgetput(&ea);
2184}
2185
2186/*
2187 * ":diffget"
2188 * ":diffput"
2189 */
2190 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002191ex_diffgetput(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192{
2193 linenr_T lnum;
2194 int count;
2195 linenr_T off = 0;
2196 diff_T *dp;
2197 diff_T *dprev;
2198 diff_T *dfree;
2199 int idx_cur;
2200 int idx_other;
2201 int idx_from;
2202 int idx_to;
2203 int i;
2204 int added;
2205 char_u *p;
2206 aco_save_T aco;
2207 buf_T *buf;
2208 int start_skip, end_skip;
2209 int new_count;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002210 int buf_empty;
Bram Moolenaar602eb742007-02-20 03:43:38 +00002211 int found_not_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212
2213 /* Find the current buffer in the list of diff buffers. */
2214 idx_cur = diff_buf_idx(curbuf);
2215 if (idx_cur == DB_COUNT)
2216 {
2217 EMSG(_("E99: Current buffer is not in diff mode"));
2218 return;
2219 }
2220
2221 if (*eap->arg == NUL)
2222 {
2223 /* No argument: Find the other buffer in the list of diff buffers. */
2224 for (idx_other = 0; idx_other < DB_COUNT; ++idx_other)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002225 if (curtab->tp_diffbuf[idx_other] != curbuf
Bram Moolenaar602eb742007-02-20 03:43:38 +00002226 && curtab->tp_diffbuf[idx_other] != NULL)
2227 {
2228 if (eap->cmdidx != CMD_diffput
2229 || curtab->tp_diffbuf[idx_other]->b_p_ma)
2230 break;
2231 found_not_ma = TRUE;
2232 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 if (idx_other == DB_COUNT)
2234 {
Bram Moolenaar602eb742007-02-20 03:43:38 +00002235 if (found_not_ma)
2236 EMSG(_("E793: No other buffer in diff mode is modifiable"));
2237 else
2238 EMSG(_("E100: No other buffer in diff mode"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 return;
2240 }
2241
2242 /* Check that there isn't a third buffer in the list */
2243 for (i = idx_other + 1; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002244 if (curtab->tp_diffbuf[i] != curbuf
2245 && curtab->tp_diffbuf[i] != NULL
2246 && (eap->cmdidx != CMD_diffput || curtab->tp_diffbuf[i]->b_p_ma))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 {
2248 EMSG(_("E101: More than two buffers in diff mode, don't know which one to use"));
2249 return;
2250 }
2251 }
2252 else
2253 {
2254 /* Buffer number or pattern given. Ignore trailing white space. */
2255 p = eap->arg + STRLEN(eap->arg);
Bram Moolenaar1c465442017-03-12 20:10:05 +01002256 while (p > eap->arg && VIM_ISWHITE(p[-1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 --p;
2258 for (i = 0; vim_isdigit(eap->arg[i]) && eap->arg + i < p; ++i)
2259 ;
2260 if (eap->arg + i == p) /* digits only */
2261 i = atol((char *)eap->arg);
2262 else
2263 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002264 i = buflist_findpat(eap->arg, p, FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 if (i < 0)
2266 return; /* error message already given */
2267 }
2268 buf = buflist_findnr(i);
2269 if (buf == NULL)
2270 {
2271 EMSG2(_("E102: Can't find buffer \"%s\""), eap->arg);
2272 return;
2273 }
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +00002274 if (buf == curbuf)
2275 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002276 idx_other = diff_buf_idx(buf);
2277 if (idx_other == DB_COUNT)
2278 {
2279 EMSG2(_("E103: Buffer \"%s\" is not in diff mode"), eap->arg);
2280 return;
2281 }
2282 }
2283
2284 diff_busy = TRUE;
2285
2286 /* When no range given include the line above or below the cursor. */
2287 if (eap->addr_count == 0)
2288 {
2289 /* Make it possible that ":diffget" on the last line gets line below
2290 * the cursor line when there is no difference above the cursor. */
2291 if (eap->cmdidx == CMD_diffget
2292 && eap->line1 == curbuf->b_ml.ml_line_count
2293 && diff_check(curwin, eap->line1) == 0
2294 && (eap->line1 == 1 || diff_check(curwin, eap->line1 - 1) == 0))
2295 ++eap->line2;
2296 else if (eap->line1 > 0)
2297 --eap->line1;
2298 }
2299
2300 if (eap->cmdidx == CMD_diffget)
2301 {
2302 idx_from = idx_other;
2303 idx_to = idx_cur;
2304 }
2305 else
2306 {
2307 idx_from = idx_cur;
2308 idx_to = idx_other;
2309 /* Need to make the other buffer the current buffer to be able to make
2310 * changes in it. */
2311 /* set curwin/curbuf to buf and save a few things */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002312 aucmd_prepbuf(&aco, curtab->tp_diffbuf[idx_other]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 }
2314
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002315 /* May give the warning for a changed buffer here, which can trigger the
2316 * FileChangedRO autocommand, which may do nasty things and mess
2317 * everything up. */
2318 if (!curbuf->b_changed)
2319 {
2320 change_warning(0);
2321 if (diff_buf_idx(curbuf) != idx_to)
2322 {
2323 EMSG(_("E787: Buffer changed unexpectedly"));
2324 return;
2325 }
2326 }
2327
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002329 for (dp = curtab->tp_first_diff; dp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 {
2331 if (dp->df_lnum[idx_cur] > eap->line2 + off)
2332 break; /* past the range that was specified */
2333
2334 dfree = NULL;
2335 lnum = dp->df_lnum[idx_to];
2336 count = dp->df_count[idx_to];
2337 if (dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off
2338 && u_save(lnum - 1, lnum + count) != FAIL)
2339 {
2340 /* Inside the specified range and saving for undo worked. */
2341 start_skip = 0;
2342 end_skip = 0;
2343 if (eap->addr_count > 0)
2344 {
2345 /* A range was specified: check if lines need to be skipped. */
2346 start_skip = eap->line1 + off - dp->df_lnum[idx_cur];
2347 if (start_skip > 0)
2348 {
2349 /* range starts below start of current diff block */
2350 if (start_skip > count)
2351 {
2352 lnum += count;
2353 count = 0;
2354 }
2355 else
2356 {
2357 count -= start_skip;
2358 lnum += start_skip;
2359 }
2360 }
2361 else
2362 start_skip = 0;
2363
2364 end_skip = dp->df_lnum[idx_cur] + dp->df_count[idx_cur] - 1
2365 - (eap->line2 + off);
2366 if (end_skip > 0)
2367 {
2368 /* range ends above end of current/from diff block */
2369 if (idx_cur == idx_from) /* :diffput */
2370 {
2371 i = dp->df_count[idx_cur] - start_skip - end_skip;
2372 if (count > i)
2373 count = i;
2374 }
2375 else /* :diffget */
2376 {
2377 count -= end_skip;
2378 end_skip = dp->df_count[idx_from] - start_skip - count;
2379 if (end_skip < 0)
2380 end_skip = 0;
2381 }
2382 }
2383 else
2384 end_skip = 0;
2385 }
2386
Bram Moolenaarb5aedf32017-03-12 18:23:53 +01002387 buf_empty = BUFEMPTY();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 added = 0;
2389 for (i = 0; i < count; ++i)
2390 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00002391 /* remember deleting the last line of the buffer */
2392 buf_empty = curbuf->b_ml.ml_line_count == 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 ml_delete(lnum, FALSE);
2394 --added;
2395 }
2396 for (i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; ++i)
2397 {
2398 linenr_T nr;
2399
2400 nr = dp->df_lnum[idx_from] + start_skip + i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002401 if (nr > curtab->tp_diffbuf[idx_from]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002403 p = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx_from],
2404 nr, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 if (p != NULL)
2406 {
2407 ml_append(lnum + i - 1, p, 0, FALSE);
2408 vim_free(p);
2409 ++added;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002410 if (buf_empty && curbuf->b_ml.ml_line_count == 2)
2411 {
2412 /* Added the first line into an empty buffer, need to
2413 * delete the dummy empty line. */
2414 buf_empty = FALSE;
2415 ml_delete((linenr_T)2, FALSE);
2416 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 }
2418 }
2419 new_count = dp->df_count[idx_to] + added;
2420 dp->df_count[idx_to] = new_count;
2421
2422 if (start_skip == 0 && end_skip == 0)
2423 {
2424 /* Check if there are any other buffers and if the diff is
2425 * equal in them. */
2426 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002427 if (curtab->tp_diffbuf[i] != NULL && i != idx_from
2428 && i != idx_to
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 && !diff_equal_entry(dp, idx_from, i))
2430 break;
2431 if (i == DB_COUNT)
2432 {
2433 /* delete the diff entry, the buffers are now equal here */
2434 dfree = dp;
2435 dp = dp->df_next;
2436 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002437 curtab->tp_first_diff = dp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438 else
2439 dprev->df_next = dp;
2440 }
2441 }
2442
2443 /* Adjust marks. This will change the following entries! */
2444 if (added != 0)
2445 {
2446 mark_adjust(lnum, lnum + count - 1, (long)MAXLNUM, (long)added);
2447 if (curwin->w_cursor.lnum >= lnum)
2448 {
2449 /* Adjust the cursor position if it's in/after the changed
2450 * lines. */
2451 if (curwin->w_cursor.lnum >= lnum + count)
2452 curwin->w_cursor.lnum += added;
2453 else if (added < 0)
2454 curwin->w_cursor.lnum = lnum;
2455 }
2456 }
2457 changed_lines(lnum, 0, lnum + count, (long)added);
2458
2459 if (dfree != NULL)
2460 {
2461 /* Diff is deleted, update folds in other windows. */
2462#ifdef FEAT_FOLDING
2463 diff_fold_update(dfree, idx_to);
2464#endif
2465 vim_free(dfree);
2466 }
2467 else
2468 /* mark_adjust() may have changed the count in a wrong way */
2469 dp->df_count[idx_to] = new_count;
2470
2471 /* When changing the current buffer, keep track of line numbers */
2472 if (idx_cur == idx_to)
2473 off += added;
2474 }
2475
2476 /* If before the range or not deleted, go to next diff. */
2477 if (dfree == NULL)
2478 {
2479 dprev = dp;
2480 dp = dp->df_next;
2481 }
2482 }
2483
2484 /* restore curwin/curbuf and a few other things */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02002485 if (eap->cmdidx != CMD_diffget)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 {
2487 /* Syncing undo only works for the current buffer, but we change
2488 * another buffer. Sync undo if the command was typed. This isn't
2489 * 100% right when ":diffput" is used in a function or mapping. */
2490 if (KeyTyped)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002491 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492 aucmd_restbuf(&aco);
2493 }
2494
2495 diff_busy = FALSE;
2496
2497 /* Check that the cursor is on a valid character and update it's position.
2498 * When there were filler lines the topline has become invalid. */
2499 check_cursor();
2500 changed_line_abv_curs();
2501
2502 /* Also need to redraw the other buffers. */
2503 diff_redraw(FALSE);
2504}
2505
2506#ifdef FEAT_FOLDING
2507/*
2508 * Update folds for all diff buffers for entry "dp".
2509 * Skip buffer with index "skip_idx".
2510 * When there are no diffs, all folds are removed.
2511 */
2512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002513diff_fold_update(diff_T *dp, int skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514{
2515 int i;
2516 win_T *wp;
2517
Bram Moolenaar29323592016-07-24 22:04:11 +02002518 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002520 if (curtab->tp_diffbuf[i] == wp->w_buffer && i != skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 foldUpdate(wp, dp->df_lnum[i],
2522 dp->df_lnum[i] + dp->df_count[i]);
2523}
2524#endif
2525
2526/*
2527 * Return TRUE if buffer "buf" is in diff-mode.
2528 */
2529 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002530diff_mode_buf(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002532 tabpage_T *tp;
2533
Bram Moolenaar29323592016-07-24 22:04:11 +02002534 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002535 if (diff_buf_idx_tp(buf, tp) != DB_COUNT)
2536 return TRUE;
2537 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538}
2539
2540/*
2541 * Move "count" times in direction "dir" to the next diff block.
2542 * Return FAIL if there isn't such a diff block.
2543 */
2544 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002545diff_move_to(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
2547 int idx;
2548 linenr_T lnum = curwin->w_cursor.lnum;
2549 diff_T *dp;
2550
2551 idx = diff_buf_idx(curbuf);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002552 if (idx == DB_COUNT || curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 return FAIL;
2554
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002555 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 ex_diffupdate(NULL); /* update after a big change */
2557
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002558 if (curtab->tp_first_diff == NULL) /* no diffs today */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 return FAIL;
2560
2561 while (--count >= 0)
2562 {
2563 /* Check if already before first diff. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002564 if (dir == BACKWARD && lnum <= curtab->tp_first_diff->df_lnum[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 break;
2566
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002567 for (dp = curtab->tp_first_diff; ; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 {
2569 if (dp == NULL)
2570 break;
2571 if ((dir == FORWARD && lnum < dp->df_lnum[idx])
2572 || (dir == BACKWARD
2573 && (dp->df_next == NULL
2574 || lnum <= dp->df_next->df_lnum[idx])))
2575 {
2576 lnum = dp->df_lnum[idx];
2577 break;
2578 }
2579 }
2580 }
2581
2582 /* don't end up past the end of the file */
2583 if (lnum > curbuf->b_ml.ml_line_count)
2584 lnum = curbuf->b_ml.ml_line_count;
2585
2586 /* When the cursor didn't move at all we fail. */
2587 if (lnum == curwin->w_cursor.lnum)
2588 return FAIL;
2589
2590 setpcmark();
2591 curwin->w_cursor.lnum = lnum;
2592 curwin->w_cursor.col = 0;
2593
2594 return OK;
2595}
2596
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002597/*
2598 * Return the line number in the current window that is closest to "lnum1" in
2599 * "buf1" in diff mode.
2600 */
2601 static linenr_T
2602diff_get_corresponding_line_int(
Bram Moolenaar7454a062016-01-30 15:14:10 +01002603 buf_T *buf1,
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002604 linenr_T lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002605{
2606 int idx1;
2607 int idx2;
2608 diff_T *dp;
2609 int baseline = 0;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002610
2611 idx1 = diff_buf_idx(buf1);
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002612 idx2 = diff_buf_idx(curbuf);
Bram Moolenaar860cae12010-06-05 23:22:07 +02002613 if (idx1 == DB_COUNT || idx2 == DB_COUNT || curtab->tp_first_diff == NULL)
2614 return lnum1;
2615
2616 if (curtab->tp_diff_invalid)
2617 ex_diffupdate(NULL); /* update after a big change */
2618
2619 if (curtab->tp_first_diff == NULL) /* no diffs today */
2620 return lnum1;
2621
2622 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
2623 {
2624 if (dp->df_lnum[idx1] > lnum1)
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002625 return lnum1 - baseline;
2626 if ((dp->df_lnum[idx1] + dp->df_count[idx1]) > lnum1)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002627 {
2628 /* Inside the diffblock */
2629 baseline = lnum1 - dp->df_lnum[idx1];
2630 if (baseline > dp->df_count[idx2])
2631 baseline = dp->df_count[idx2];
2632
2633 return dp->df_lnum[idx2] + baseline;
2634 }
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002635 if ( (dp->df_lnum[idx1] == lnum1)
2636 && (dp->df_count[idx1] == 0)
2637 && (dp->df_lnum[idx2] <= curwin->w_cursor.lnum)
2638 && ((dp->df_lnum[idx2] + dp->df_count[idx2])
2639 > curwin->w_cursor.lnum))
Bram Moolenaar860cae12010-06-05 23:22:07 +02002640 /*
2641 * Special case: if the cursor is just after a zero-count
2642 * block (i.e. all filler) and the target cursor is already
2643 * inside the corresponding block, leave the target cursor
2644 * unmoved. This makes repeated CTRL-W W operations work
2645 * as expected.
2646 */
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002647 return curwin->w_cursor.lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002648 baseline = (dp->df_lnum[idx1] + dp->df_count[idx1])
2649 - (dp->df_lnum[idx2] + dp->df_count[idx2]);
2650 }
2651
2652 /* If we get here then the cursor is after the last diff */
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002653 return lnum1 - baseline;
2654}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002655
Bram Moolenaar025e3e02016-10-18 14:50:18 +02002656/*
2657 * Return the line number in the current window that is closest to "lnum1" in
2658 * "buf1" in diff mode. Checks the line number to be valid.
2659 */
2660 linenr_T
2661diff_get_corresponding_line(buf_T *buf1, linenr_T lnum1)
2662{
2663 linenr_T lnum = diff_get_corresponding_line_int(buf1, lnum1);
2664
2665 /* don't end up past the end of the file */
2666 if (lnum > curbuf->b_ml.ml_line_count)
2667 return curbuf->b_ml.ml_line_count;
2668 return lnum;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002669}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002670
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671/*
2672 * For line "lnum" in the current window find the equivalent lnum in window
2673 * "wp", compensating for inserted/deleted lines.
2674 */
2675 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002676diff_lnum_win(linenr_T lnum, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677{
2678 diff_T *dp;
2679 int idx;
2680 int i;
2681 linenr_T n;
2682
2683 idx = diff_buf_idx(curbuf);
2684 if (idx == DB_COUNT) /* safety check */
2685 return (linenr_T)0;
2686
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002687 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 ex_diffupdate(NULL); /* update after a big change */
2689
2690 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002691 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
2693 break;
2694
2695 /* When after the last change, compute relative to the last line number. */
2696 if (dp == NULL)
2697 return wp->w_buffer->b_ml.ml_line_count
2698 - (curbuf->b_ml.ml_line_count - lnum);
2699
2700 /* Find index for "wp". */
2701 i = diff_buf_idx(wp->w_buffer);
2702 if (i == DB_COUNT) /* safety check */
2703 return (linenr_T)0;
2704
2705 n = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]);
2706 if (n > dp->df_lnum[i] + dp->df_count[i])
2707 n = dp->df_lnum[i] + dp->df_count[i];
2708 return n;
2709}
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710
2711#endif /* FEAT_DIFF */