blob: 35e29fcdf916b20098b2f18df27b8a891e8f9d8f [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vim:set ts=8 sts=4 sw=4:
2 *
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 Moolenaar071d4272004-06-13 20:20:40 +000026static int diff_flags = DIFF_FILLER;
27
28#define LBUFLEN 50 /* length of line in diff file */
29
30static int diff_a_works = MAYBE; /* TRUE when "diff -a" works, FALSE when it
31 doesn't work, MAYBE when not checked yet */
Bram Moolenaar48e330a2016-02-23 14:53:34 +010032#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000033static int diff_bin_works = MAYBE; /* TRUE when "diff --binary" works, FALSE
34 when it doesn't work, MAYBE when not
35 checked yet */
36#endif
37
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010038static int diff_buf_idx(buf_T *buf);
39static int diff_buf_idx_tp(buf_T *buf, tabpage_T *tp);
40static void diff_mark_adjust_tp(tabpage_T *tp, int idx, linenr_T line1, linenr_T line2, long amount, long amount_after);
41static void diff_check_unchanged(tabpage_T *tp, diff_T *dp);
42static int diff_check_sanity(tabpage_T *tp, diff_T *dp);
43static void diff_redraw(int dofold);
44static int diff_write(buf_T *buf, char_u *fname);
45static void diff_file(char_u *tmp_orig, char_u *tmp_new, char_u *tmp_diff);
46static int diff_equal_entry(diff_T *dp, int idx1, int idx2);
47static int diff_cmp(char_u *s1, char_u *s2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000048#ifdef FEAT_FOLDING
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010049static void diff_fold_update(diff_T *dp, int skip_idx);
Bram Moolenaar071d4272004-06-13 20:20:40 +000050#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +010051static void diff_read(int idx_orig, int idx_new, char_u *fname);
52static void diff_copy_entry(diff_T *dprev, diff_T *dp, int idx_orig, int idx_new);
53static diff_T *diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
55#ifndef USE_CR
56# define tag_fgets vim_fgets
57#endif
58
59/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 * Called when deleting or unloading a buffer: No longer make a diff with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 */
62 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010063diff_buf_delete(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000064{
65 int i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000066 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000067
Bram Moolenaar29323592016-07-24 22:04:11 +020068 FOR_ALL_TABPAGES(tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000069 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000070 i = diff_buf_idx_tp(buf, tp);
71 if (i != DB_COUNT)
72 {
73 tp->tp_diffbuf[i] = NULL;
74 tp->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +000075 if (tp == curtab)
76 diff_redraw(TRUE);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000077 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000078 }
79}
80
81/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000082 * Check if the current buffer should be added to or removed from the list of
83 * diff buffers.
84 */
85 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010086diff_buf_adjust(win_T *win)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000087{
88 win_T *wp;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000089 int i;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000090
91 if (!win->w_p_diff)
92 {
93 /* When there is no window showing a diff for this buffer, remove
94 * it from the diffs. */
Bram Moolenaar29323592016-07-24 22:04:11 +020095 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000096 if (wp->w_buffer == win->w_buffer && wp->w_p_diff)
97 break;
98 if (wp == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000099 {
100 i = diff_buf_idx(win->w_buffer);
101 if (i != DB_COUNT)
102 {
103 curtab->tp_diffbuf[i] = NULL;
104 curtab->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000105 diff_redraw(TRUE);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000106 }
107 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000108 }
109 else
110 diff_buf_add(win->w_buffer);
111}
112
113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000114 * Add a buffer to make diffs for.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000115 * Call this when a new buffer is being edited in the current window where
116 * 'diff' is set.
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +0000117 * Marks the current buffer as being part of the diff and requiring updating.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000118 * This must be done before any autocmd, because a command may use info
119 * about the screen contents.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000120 */
121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100122diff_buf_add(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000123{
124 int i;
125
126 if (diff_buf_idx(buf) != DB_COUNT)
127 return; /* It's already there. */
128
129 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000130 if (curtab->tp_diffbuf[i] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000132 curtab->tp_diffbuf[i] = buf;
133 curtab->tp_diff_invalid = TRUE;
Bram Moolenaar5d55c0f2008-11-30 14:16:57 +0000134 diff_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000135 return;
136 }
137
Bram Moolenaar89eaa412016-07-31 14:17:27 +0200138 EMSGN(_("E96: Cannot diff more than %ld buffers"), DB_COUNT);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000139}
140
141/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000142 * Find buffer "buf" in the list of diff buffers for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000143 * Return its index or DB_COUNT if not found.
144 */
145 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100146diff_buf_idx(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147{
148 int idx;
149
150 for (idx = 0; idx < DB_COUNT; ++idx)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000151 if (curtab->tp_diffbuf[idx] == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 break;
153 return idx;
154}
155
156/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000157 * Find buffer "buf" in the list of diff buffers for tab page "tp".
158 * Return its index or DB_COUNT if not found.
159 */
160 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100161diff_buf_idx_tp(buf_T *buf, tabpage_T *tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000162{
163 int idx;
164
165 for (idx = 0; idx < DB_COUNT; ++idx)
166 if (tp->tp_diffbuf[idx] == buf)
167 break;
168 return idx;
169}
170
171/*
172 * Mark the diff info involving buffer "buf" as invalid, it will be updated
173 * when info is requested.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000174 */
175 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100176diff_invalidate(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000178 tabpage_T *tp;
179 int i;
180
Bram Moolenaar29323592016-07-24 22:04:11 +0200181 FOR_ALL_TABPAGES(tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000183 i = diff_buf_idx_tp(buf, tp);
184 if (i != DB_COUNT)
185 {
186 tp->tp_diff_invalid = TRUE;
187 if (tp == curtab)
188 diff_redraw(TRUE);
189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190 }
191}
192
193/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000194 * Called by mark_adjust(): update line numbers in "curbuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000195 */
196 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100197diff_mark_adjust(
198 linenr_T line1,
199 linenr_T line2,
200 long amount,
201 long amount_after)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000203 int idx;
204 tabpage_T *tp;
205
206 /* Handle all tab pages that use the current buffer in a diff. */
Bram Moolenaar29323592016-07-24 22:04:11 +0200207 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000208 {
209 idx = diff_buf_idx_tp(curbuf, tp);
210 if (idx != DB_COUNT)
211 diff_mark_adjust_tp(tp, idx, line1, line2, amount, amount_after);
212 }
213}
214
215/*
216 * Update line numbers in tab page "tp" for "curbuf" with index "idx".
217 * This attempts to update the changes as much as possible:
218 * When inserting/deleting lines outside of existing change blocks, create a
219 * new change block and update the line numbers in following blocks.
220 * When inserting/deleting lines in existing change blocks, update them.
221 */
222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100223diff_mark_adjust_tp(
224 tabpage_T *tp,
225 int idx,
226 linenr_T line1,
227 linenr_T line2,
228 long amount,
229 long amount_after)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000230{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231 diff_T *dp;
232 diff_T *dprev;
233 diff_T *dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 int i;
235 int inserted, deleted;
236 int n, off;
237 linenr_T last;
238 linenr_T lnum_deleted = line1; /* lnum of remaining deletion */
239 int check_unchanged;
240
Bram Moolenaar071d4272004-06-13 20:20:40 +0000241 if (line2 == MAXLNUM)
242 {
243 /* mark_adjust(99, MAXLNUM, 9, 0): insert lines */
244 inserted = amount;
245 deleted = 0;
246 }
247 else if (amount_after > 0)
248 {
249 /* mark_adjust(99, 98, MAXLNUM, 9): a change that inserts lines*/
250 inserted = amount_after;
251 deleted = 0;
252 }
253 else
254 {
255 /* mark_adjust(98, 99, MAXLNUM, -2): delete lines */
256 inserted = 0;
257 deleted = -amount_after;
258 }
259
260 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000261 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262 for (;;)
263 {
264 /* If the change is after the previous diff block and before the next
265 * diff block, thus not touching an existing change, create a new diff
266 * block. Don't do this when ex_diffgetput() is busy. */
267 if ((dp == NULL || dp->df_lnum[idx] - 1 > line2
268 || (line2 == MAXLNUM && dp->df_lnum[idx] > line1))
269 && (dprev == NULL
270 || dprev->df_lnum[idx] + dprev->df_count[idx] < line1)
271 && !diff_busy)
272 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000273 dnext = diff_alloc_new(tp, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274 if (dnext == NULL)
275 return;
276
277 dnext->df_lnum[idx] = line1;
278 dnext->df_count[idx] = inserted;
279 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000280 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000281 {
282 if (dprev == NULL)
283 dnext->df_lnum[i] = line1;
284 else
285 dnext->df_lnum[i] = line1
286 + (dprev->df_lnum[i] + dprev->df_count[i])
287 - (dprev->df_lnum[idx] + dprev->df_count[idx]);
288 dnext->df_count[i] = deleted;
289 }
290 }
291
292 /* if at end of the list, quit */
293 if (dp == NULL)
294 break;
295
296 /*
297 * Check for these situations:
298 * 1 2 3
299 * 1 2 3
300 * line1 2 3 4 5
301 * 2 3 4 5
302 * 2 3 4 5
303 * line2 2 3 4 5
304 * 3 5 6
305 * 3 5 6
306 */
307 /* compute last line of this change */
308 last = dp->df_lnum[idx] + dp->df_count[idx] - 1;
309
310 /* 1. change completely above line1: nothing to do */
311 if (last >= line1 - 1)
312 {
313 /* 6. change below line2: only adjust for amount_after; also when
314 * "deleted" became zero when deleted all lines between two diffs */
315 if (dp->df_lnum[idx] - (deleted + inserted != 0) > line2)
316 {
317 if (amount_after == 0)
318 break; /* nothing left to change */
319 dp->df_lnum[idx] += amount_after;
320 }
321 else
322 {
323 check_unchanged = FALSE;
324
325 /* 2. 3. 4. 5.: inserted/deleted lines touching this diff. */
326 if (deleted > 0)
327 {
328 if (dp->df_lnum[idx] >= line1)
329 {
330 off = dp->df_lnum[idx] - lnum_deleted;
331 if (last <= line2)
332 {
333 /* 4. delete all lines of diff */
334 if (dp->df_next != NULL
335 && dp->df_next->df_lnum[idx] - 1 <= line2)
336 {
337 /* delete continues in next diff, only do
338 * lines until that one */
339 n = dp->df_next->df_lnum[idx] - lnum_deleted;
340 deleted -= n;
341 n -= dp->df_count[idx];
342 lnum_deleted = dp->df_next->df_lnum[idx];
343 }
344 else
345 n = deleted - dp->df_count[idx];
346 dp->df_count[idx] = 0;
347 }
348 else
349 {
350 /* 5. delete lines at or just before top of diff */
351 n = off;
352 dp->df_count[idx] -= line2 - dp->df_lnum[idx] + 1;
353 check_unchanged = TRUE;
354 }
355 dp->df_lnum[idx] = line1;
356 }
357 else
358 {
359 off = 0;
360 if (last < line2)
361 {
362 /* 2. delete at end of of diff */
363 dp->df_count[idx] -= last - lnum_deleted + 1;
364 if (dp->df_next != NULL
365 && dp->df_next->df_lnum[idx] - 1 <= line2)
366 {
367 /* delete continues in next diff, only do
368 * lines until that one */
369 n = dp->df_next->df_lnum[idx] - 1 - last;
370 deleted -= dp->df_next->df_lnum[idx]
371 - lnum_deleted;
372 lnum_deleted = dp->df_next->df_lnum[idx];
373 }
374 else
375 n = line2 - last;
376 check_unchanged = TRUE;
377 }
378 else
379 {
380 /* 3. delete lines inside the diff */
381 n = 0;
382 dp->df_count[idx] -= deleted;
383 }
384 }
385
386 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000387 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000388 {
389 dp->df_lnum[i] -= off;
390 dp->df_count[i] += n;
391 }
392 }
393 else
394 {
395 if (dp->df_lnum[idx] <= line1)
396 {
397 /* inserted lines somewhere in this diff */
398 dp->df_count[idx] += inserted;
399 check_unchanged = TRUE;
400 }
401 else
402 /* inserted lines somewhere above this diff */
403 dp->df_lnum[idx] += inserted;
404 }
405
406 if (check_unchanged)
407 /* Check if inserted lines are equal, may reduce the
408 * size of the diff. TODO: also check for equal lines
409 * in the middle and perhaps split the block. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000410 diff_check_unchanged(tp, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000411 }
412 }
413
414 /* check if this block touches the previous one, may merge them. */
415 if (dprev != NULL && dprev->df_lnum[idx] + dprev->df_count[idx]
416 == dp->df_lnum[idx])
417 {
418 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000419 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000420 dprev->df_count[i] += dp->df_count[i];
421 dprev->df_next = dp->df_next;
422 vim_free(dp);
423 dp = dprev->df_next;
424 }
425 else
426 {
427 /* Advance to next entry. */
428 dprev = dp;
429 dp = dp->df_next;
430 }
431 }
432
433 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000434 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000435 while (dp != NULL)
436 {
437 /* All counts are zero, remove this entry. */
438 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000439 if (tp->tp_diffbuf[i] != NULL && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000440 break;
441 if (i == DB_COUNT)
442 {
443 dnext = dp->df_next;
444 vim_free(dp);
445 dp = dnext;
446 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000447 tp->tp_first_diff = dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 else
449 dprev->df_next = dnext;
450 }
451 else
452 {
453 /* Advance to next entry. */
454 dprev = dp;
455 dp = dp->df_next;
456 }
457
458 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000459
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000460 if (tp == curtab)
461 {
462 diff_redraw(TRUE);
463
464 /* Need to recompute the scroll binding, may remove or add filler
465 * lines (e.g., when adding lines above w_topline). But it's slow when
466 * making many changes, postpone until redrawing. */
467 diff_need_scrollbind = TRUE;
468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000469}
470
471/*
472 * Allocate a new diff block and link it between "dprev" and "dp".
473 */
474 static diff_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +0100475diff_alloc_new(tabpage_T *tp, diff_T *dprev, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000476{
477 diff_T *dnew;
478
479 dnew = (diff_T *)alloc((unsigned)sizeof(diff_T));
480 if (dnew != NULL)
481 {
482 dnew->df_next = dp;
483 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000484 tp->tp_first_diff = dnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000485 else
486 dprev->df_next = dnew;
487 }
488 return dnew;
489}
490
491/*
492 * Check if the diff block "dp" can be made smaller for lines at the start and
493 * end that are equal. Called after inserting lines.
494 * This may result in a change where all buffers have zero lines, the caller
495 * must take care of removing it.
496 */
497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100498diff_check_unchanged(tabpage_T *tp, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000499{
500 int i_org;
501 int i_new;
502 int off_org, off_new;
503 char_u *line_org;
504 int dir = FORWARD;
505
506 /* Find the first buffers, use it as the original, compare the other
507 * buffer lines against this one. */
508 for (i_org = 0; i_org < DB_COUNT; ++i_org)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000509 if (tp->tp_diffbuf[i_org] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000510 break;
511 if (i_org == DB_COUNT) /* safety check */
512 return;
513
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000514 if (diff_check_sanity(tp, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000515 return;
516
517 /* First check lines at the top, then at the bottom. */
518 off_org = 0;
519 off_new = 0;
520 for (;;)
521 {
522 /* Repeat until a line is found which is different or the number of
523 * lines has become zero. */
524 while (dp->df_count[i_org] > 0)
525 {
526 /* Copy the line, the next ml_get() will invalidate it. */
527 if (dir == BACKWARD)
528 off_org = dp->df_count[i_org] - 1;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000529 line_org = vim_strsave(ml_get_buf(tp->tp_diffbuf[i_org],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000530 dp->df_lnum[i_org] + off_org, FALSE));
531 if (line_org == NULL)
532 return;
533 for (i_new = i_org + 1; i_new < DB_COUNT; ++i_new)
534 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000535 if (tp->tp_diffbuf[i_new] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 continue;
537 if (dir == BACKWARD)
538 off_new = dp->df_count[i_new] - 1;
539 /* if other buffer doesn't have this line, it was inserted */
540 if (off_new < 0 || off_new >= dp->df_count[i_new])
541 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000542 if (diff_cmp(line_org, ml_get_buf(tp->tp_diffbuf[i_new],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000543 dp->df_lnum[i_new] + off_new, FALSE)) != 0)
544 break;
545 }
546 vim_free(line_org);
547
548 /* Stop when a line isn't equal in all diff buffers. */
549 if (i_new != DB_COUNT)
550 break;
551
552 /* Line matched in all buffers, remove it from the diff. */
553 for (i_new = i_org; i_new < DB_COUNT; ++i_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000554 if (tp->tp_diffbuf[i_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 {
556 if (dir == FORWARD)
557 ++dp->df_lnum[i_new];
558 --dp->df_count[i_new];
559 }
560 }
561 if (dir == BACKWARD)
562 break;
563 dir = BACKWARD;
564 }
565}
566
567/*
568 * Check if a diff block doesn't contain invalid line numbers.
569 * This can happen when the diff program returns invalid results.
570 */
571 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100572diff_check_sanity(tabpage_T *tp, diff_T *dp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573{
574 int i;
575
576 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000577 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 if (dp->df_lnum[i] + dp->df_count[i] - 1
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000579 > tp->tp_diffbuf[i]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 return FAIL;
581 return OK;
582}
583
584/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000585 * Mark all diff buffers in the current tab page for redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 */
587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100588diff_redraw(
589 int dofold) /* also recompute the folds */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590{
591 win_T *wp;
592 int n;
593
Bram Moolenaar29323592016-07-24 22:04:11 +0200594 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 if (wp->w_p_diff)
596 {
Bram Moolenaar60f83772006-03-12 21:52:47 +0000597 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598#ifdef FEAT_FOLDING
599 if (dofold && foldmethodIsDiff(wp))
600 foldUpdateAll(wp);
601#endif
602 /* A change may have made filler lines invalid, need to take care
603 * of that for other windows. */
Bram Moolenaara80888d2012-10-21 22:18:21 +0200604 n = diff_check(wp, wp->w_topline);
605 if ((wp != curwin && wp->w_topfill > 0) || n > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000606 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 if (wp->w_topfill > n)
608 wp->w_topfill = (n < 0 ? 0 : n);
Bram Moolenaara80888d2012-10-21 22:18:21 +0200609 else if (n > 0 && n > wp->w_topfill)
610 wp->w_topfill = n;
Bram Moolenaar846a2ff2014-05-28 11:35:37 +0200611 check_topfill(wp, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 }
613 }
614}
615
616/*
617 * Write buffer "buf" to file "name".
618 * Always use 'fileformat' set to "unix".
619 * Return FAIL for failure
620 */
621 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100622diff_write(buf_T *buf, char_u *fname)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623{
624 int r;
625 char_u *save_ff;
626
627 save_ff = buf->b_p_ff;
628 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX);
629 r = buf_write(buf, fname, NULL, (linenr_T)1, buf->b_ml.ml_line_count,
630 NULL, FALSE, FALSE, FALSE, TRUE);
631 free_string_option(buf->b_p_ff);
632 buf->b_p_ff = save_ff;
633 return r;
634}
635
636/*
637 * Completely update the diffs for the buffers involved.
638 * This uses the ordinary "diff" command.
639 * The buffers are written to a file, also for unmodified buffers (the file
640 * could have been produced by autocommands, e.g. the netrw plugin).
641 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000642 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100643ex_diffupdate(
Bram Moolenaarf1d25012016-03-03 12:22:53 +0100644 exarg_T *eap) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645{
646 buf_T *buf;
647 int idx_orig;
648 int idx_new;
649 char_u *tmp_orig;
650 char_u *tmp_new;
651 char_u *tmp_diff;
652 FILE *fd;
653 int ok;
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000654 int io_error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655
656 /* Delete all diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000657 diff_clear(curtab);
658 curtab->tp_diff_invalid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659
660 /* Use the first buffer as the original text. */
661 for (idx_orig = 0; idx_orig < DB_COUNT; ++idx_orig)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000662 if (curtab->tp_diffbuf[idx_orig] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000663 break;
664 if (idx_orig == DB_COUNT)
665 return;
666
667 /* Only need to do something when there is another buffer. */
668 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000669 if (curtab->tp_diffbuf[idx_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 break;
671 if (idx_new == DB_COUNT)
672 return;
673
674 /* We need three temp file names. */
Bram Moolenaare5c421c2015-03-31 13:33:08 +0200675 tmp_orig = vim_tempname('o', TRUE);
676 tmp_new = vim_tempname('n', TRUE);
677 tmp_diff = vim_tempname('d', TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000678 if (tmp_orig == NULL || tmp_new == NULL || tmp_diff == NULL)
679 goto theend;
680
681 /*
682 * Do a quick test if "diff" really works. Otherwise it looks like there
683 * are no differences. Can't use the return value, it's non-zero when
684 * there are differences.
685 * May try twice, first with "-a" and then without.
686 */
687 for (;;)
688 {
689 ok = FALSE;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000690 fd = mch_fopen((char *)tmp_orig, "w");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000691 if (fd == NULL)
692 io_error = TRUE;
693 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000695 if (fwrite("line1\n", (size_t)6, (size_t)1, fd) != 1)
696 io_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 fclose(fd);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000698 fd = mch_fopen((char *)tmp_new, "w");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000699 if (fd == NULL)
700 io_error = TRUE;
701 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000703 if (fwrite("line2\n", (size_t)6, (size_t)1, fd) != 1)
704 io_error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 fclose(fd);
706 diff_file(tmp_orig, tmp_new, tmp_diff);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000707 fd = mch_fopen((char *)tmp_diff, "r");
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000708 if (fd == NULL)
709 io_error = TRUE;
710 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 {
712 char_u linebuf[LBUFLEN];
713
714 for (;;)
715 {
716 /* There must be a line that contains "1c1". */
717 if (tag_fgets(linebuf, LBUFLEN, fd))
718 break;
719 if (STRNCMP(linebuf, "1c1", 3) == 0)
720 ok = TRUE;
721 }
722 fclose(fd);
723 }
724 mch_remove(tmp_diff);
725 mch_remove(tmp_new);
726 }
727 mch_remove(tmp_orig);
728 }
729
730#ifdef FEAT_EVAL
731 /* When using 'diffexpr' break here. */
732 if (*p_dex != NUL)
733 break;
734#endif
735
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100736#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 /* If the "-a" argument works, also check if "--binary" works. */
738 if (ok && diff_a_works == MAYBE && diff_bin_works == MAYBE)
739 {
740 diff_a_works = TRUE;
741 diff_bin_works = TRUE;
742 continue;
743 }
744 if (!ok && diff_a_works == TRUE && diff_bin_works == TRUE)
745 {
746 /* Tried --binary, but it failed. "-a" works though. */
747 diff_bin_works = FALSE;
748 ok = TRUE;
749 }
750#endif
751
752 /* If we checked if "-a" works already, break here. */
753 if (diff_a_works != MAYBE)
754 break;
755 diff_a_works = ok;
756
757 /* If "-a" works break here, otherwise retry without "-a". */
758 if (ok)
759 break;
760 }
761 if (!ok)
762 {
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000763 if (io_error)
764 EMSG(_("E810: Cannot read or write temp files"));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 EMSG(_("E97: Cannot create diffs"));
766 diff_a_works = MAYBE;
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100767#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 diff_bin_works = MAYBE;
769#endif
770 goto theend;
771 }
772
Bram Moolenaarbd1d5602012-05-18 18:47:17 +0200773 /* :diffupdate! */
774 if (eap != NULL && eap->forceit)
775 for (idx_new = idx_orig; idx_new < DB_COUNT; ++idx_new)
776 {
777 buf = curtab->tp_diffbuf[idx_new];
778 if (buf_valid(buf))
779 buf_check_timestamp(buf, FALSE);
780 }
781
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 /* Write the first buffer to a tempfile. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000783 buf = curtab->tp_diffbuf[idx_orig];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784 if (diff_write(buf, tmp_orig) == FAIL)
785 goto theend;
786
787 /* Make a difference between the first buffer and every other. */
788 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
789 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000790 buf = curtab->tp_diffbuf[idx_new];
Bram Moolenaar9dd33af2015-08-04 21:51:25 +0200791 if (buf == NULL || buf->b_ml.ml_mfp == NULL)
792 continue; /* skip buffer that isn't loaded */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000793 if (diff_write(buf, tmp_new) == FAIL)
794 continue;
795 diff_file(tmp_orig, tmp_new, tmp_diff);
796
797 /* Read the diff output and add each entry to the diff list. */
798 diff_read(idx_orig, idx_new, tmp_diff);
799 mch_remove(tmp_diff);
800 mch_remove(tmp_new);
801 }
802 mch_remove(tmp_orig);
803
Bram Moolenaar60a44dc2007-10-19 16:58:12 +0000804 /* force updating cursor position on screen */
805 curwin->w_valid_cursor.lnum = 0;
806
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 diff_redraw(TRUE);
808
809theend:
810 vim_free(tmp_orig);
811 vim_free(tmp_new);
812 vim_free(tmp_diff);
813}
814
815/*
816 * Make a diff between files "tmp_orig" and "tmp_new", results in "tmp_diff".
817 */
818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100819diff_file(
820 char_u *tmp_orig,
821 char_u *tmp_new,
822 char_u *tmp_diff)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823{
824 char_u *cmd;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000825 size_t len;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826
827#ifdef FEAT_EVAL
828 if (*p_dex != NUL)
829 /* Use 'diffexpr' to generate the diff file. */
830 eval_diff(tmp_orig, tmp_new, tmp_diff);
831 else
832#endif
833 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000834 len = STRLEN(tmp_orig) + STRLEN(tmp_new)
835 + STRLEN(tmp_diff) + STRLEN(p_srr) + 27;
836 cmd = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 if (cmd != NULL)
838 {
Bram Moolenaar95fb60a2005-03-08 22:29:20 +0000839 /* We don't want $DIFF_OPTIONS to get in the way. */
840 if (getenv("DIFF_OPTIONS"))
841 vim_setenv((char_u *)"DIFF_OPTIONS", (char_u *)"");
842
Bram Moolenaar071d4272004-06-13 20:20:40 +0000843 /* Build the diff command and execute it. Always use -a, binary
844 * differences are of no use. Ignore errors, diff returns
845 * non-zero when differences have been found. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000846 vim_snprintf((char *)cmd, len, "diff %s%s%s%s%s %s",
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 diff_a_works == FALSE ? "" : "-a ",
Bram Moolenaar48e330a2016-02-23 14:53:34 +0100848#if defined(MSWIN)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 diff_bin_works == TRUE ? "--binary " : "",
850#else
851 "",
852#endif
853 (diff_flags & DIFF_IWHITE) ? "-b " : "",
854 (diff_flags & DIFF_ICASE) ? "-i " : "",
855 tmp_orig, tmp_new);
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000856 append_redir(cmd, (int)len, p_srr, tmp_diff);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000857#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +0000858 block_autocmds(); /* Avoid ShellCmdPost stuff */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 (void)call_shell(cmd, SHELL_FILTER|SHELL_SILENT|SHELL_DOOUT);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000861#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +0000862 unblock_autocmds();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000863#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 vim_free(cmd);
865 }
866 }
867}
868
869/*
870 * Create a new version of a file from the current buffer and a diff file.
871 * The buffer is written to a file, also for unmodified buffers (the file
872 * could have been produced by autocommands, e.g. the netrw plugin).
873 */
874 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100875ex_diffpatch(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876{
877 char_u *tmp_orig; /* name of original temp file */
878 char_u *tmp_new; /* name of patched temp file */
879 char_u *buf = NULL;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000880 size_t buflen;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 win_T *old_curwin = curwin;
882 char_u *newname = NULL; /* name of patched file buffer */
883#ifdef UNIX
884 char_u dirbuf[MAXPATHL];
885 char_u *fullname = NULL;
886#endif
887#ifdef FEAT_BROWSE
888 char_u *browseFile = NULL;
889 int browse_flag = cmdmod.browse;
890#endif
Bram Moolenaar8767f522016-07-01 17:17:39 +0200891 stat_T st;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892
893#ifdef FEAT_BROWSE
894 if (cmdmod.browse)
895 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000896 browseFile = do_browse(0, (char_u *)_("Patch file"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000897 eap->arg, NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
898 if (browseFile == NULL)
899 return; /* operation cancelled */
900 eap->arg = browseFile;
901 cmdmod.browse = FALSE; /* don't let do_ecmd() browse again */
902 }
903#endif
904
905 /* We need two temp file names. */
Bram Moolenaare5c421c2015-03-31 13:33:08 +0200906 tmp_orig = vim_tempname('o', FALSE);
907 tmp_new = vim_tempname('n', FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 if (tmp_orig == NULL || tmp_new == NULL)
909 goto theend;
910
911 /* Write the current buffer to "tmp_orig". */
912 if (buf_write(curbuf, tmp_orig, NULL,
913 (linenr_T)1, curbuf->b_ml.ml_line_count,
914 NULL, FALSE, FALSE, FALSE, TRUE) == FAIL)
915 goto theend;
916
917#ifdef UNIX
918 /* Get the absolute path of the patchfile, changing directory below. */
919 fullname = FullName_save(eap->arg, FALSE);
920#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000921 buflen = STRLEN(tmp_orig) + (
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922# ifdef UNIX
923 fullname != NULL ? STRLEN(fullname) :
924# endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000925 STRLEN(eap->arg)) + STRLEN(tmp_new) + 16;
926 buf = alloc((unsigned)buflen);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927 if (buf == NULL)
928 goto theend;
929
930#ifdef UNIX
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +0000931 /* Temporarily chdir to /tmp, to avoid patching files in the current
Bram Moolenaar071d4272004-06-13 20:20:40 +0000932 * directory when the patch file contains more than one patch. When we
933 * have our own temp dir use that instead, it will be cleaned up when we
934 * exit (any .rej files created). Don't change directory if we can't
935 * return to the current. */
936 if (mch_dirname(dirbuf, MAXPATHL) != OK || mch_chdir((char *)dirbuf) != 0)
937 dirbuf[0] = NUL;
938 else
939 {
940# ifdef TEMPDIRNAMES
941 if (vim_tempdir != NULL)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000942 ignored = mch_chdir((char *)vim_tempdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 else
944# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +0000945 ignored = mch_chdir("/tmp");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000946 shorten_fnames(TRUE);
947 }
948#endif
949
950#ifdef FEAT_EVAL
951 if (*p_pex != NUL)
952 /* Use 'patchexpr' to generate the new file. */
953 eval_patch(tmp_orig,
954# ifdef UNIX
955 fullname != NULL ? fullname :
956# endif
957 eap->arg, tmp_new);
958 else
959#endif
960 {
961 /* Build the patch command and execute it. Ignore errors. Switch to
962 * cooked mode to allow the user to respond to prompts. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000963 vim_snprintf((char *)buf, buflen, "patch -o %s %s < \"%s\"",
964 tmp_new, tmp_orig,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000965# ifdef UNIX
966 fullname != NULL ? fullname :
967# endif
968 eap->arg);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000969#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +0000970 block_autocmds(); /* Avoid ShellCmdPost stuff */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 (void)call_shell(buf, SHELL_FILTER | SHELL_COOKED);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000973#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +0000974 unblock_autocmds();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000976 }
977
978#ifdef UNIX
979 if (dirbuf[0] != NUL)
980 {
981 if (mch_chdir((char *)dirbuf) != 0)
982 EMSG(_(e_prev_dir));
983 shorten_fnames(TRUE);
984 }
985#endif
986
987 /* patch probably has written over the screen */
988 redraw_later(CLEAR);
989
990 /* Delete any .orig or .rej file created. */
991 STRCPY(buf, tmp_new);
992 STRCAT(buf, ".orig");
993 mch_remove(buf);
994 STRCPY(buf, tmp_new);
995 STRCAT(buf, ".rej");
996 mch_remove(buf);
997
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +0000998 /* Only continue if the output file was created. */
999 if (mch_stat((char *)tmp_new, &st) < 0 || st.st_size == 0)
1000 EMSG(_("E816: Cannot read patch output"));
1001 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001003 if (curbuf->b_fname != NULL)
1004 {
1005 newname = vim_strnsave(curbuf->b_fname,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 (int)(STRLEN(curbuf->b_fname) + 4));
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001007 if (newname != NULL)
1008 STRCAT(newname, ".new");
1009 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010
1011#ifdef FEAT_GUI
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001012 need_mouse_correct = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013#endif
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001014 /* don't use a new tab page, each tab page has its own diffs */
1015 cmdmod.tab = 0;
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001016
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001017 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001019 /* Pretend it was a ":split fname" command */
1020 eap->cmdidx = CMD_split;
1021 eap->arg = tmp_new;
1022 do_exedit(eap, old_curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001024 /* check that split worked and editing tmp_new */
1025 if (curwin != old_curwin && win_valid(old_curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001026 {
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001027 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1028 diff_win_options(curwin, TRUE);
1029 diff_win_options(old_curwin, TRUE);
1030
1031 if (newname != NULL)
1032 {
1033 /* do a ":file filename.new" on the patched buffer */
1034 eap->arg = newname;
1035 ex_file(eap);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036
1037#ifdef FEAT_AUTOCMD
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001038 /* Do filetype detection with the new name. */
1039 if (au_has_group((char_u *)"filetypedetect"))
1040 do_cmdline_cmd((char_u *)":doau filetypedetect BufRead");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041#endif
Bram Moolenaar6ec0a6c2009-07-22 14:23:13 +00001042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043 }
1044 }
1045 }
1046
1047theend:
1048 if (tmp_orig != NULL)
1049 mch_remove(tmp_orig);
1050 vim_free(tmp_orig);
1051 if (tmp_new != NULL)
1052 mch_remove(tmp_new);
1053 vim_free(tmp_new);
1054 vim_free(newname);
1055 vim_free(buf);
1056#ifdef UNIX
1057 vim_free(fullname);
1058#endif
1059#ifdef FEAT_BROWSE
1060 vim_free(browseFile);
1061 cmdmod.browse = browse_flag;
1062#endif
1063}
1064
1065/*
1066 * Split the window and edit another file, setting options to show the diffs.
1067 */
1068 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001069ex_diffsplit(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070{
1071 win_T *old_curwin = curwin;
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001072 bufref_T old_curbuf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001074 set_bufref(&old_curbuf, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075#ifdef FEAT_GUI
1076 need_mouse_correct = TRUE;
1077#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001078 /* don't use a new tab page, each tab page has its own diffs */
1079 cmdmod.tab = 0;
1080
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001081 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 {
1083 /* Pretend it was a ":split fname" command */
1084 eap->cmdidx = CMD_split;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001085 curwin->w_p_diff = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 do_exedit(eap, old_curwin);
1087
1088 if (curwin != old_curwin) /* split must have worked */
1089 {
1090 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1091 diff_win_options(curwin, TRUE);
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001092 if (win_valid(old_curwin))
1093 {
1094 diff_win_options(old_curwin, TRUE);
1095
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001096 if (bufref_valid(&old_curbuf))
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001097 /* Move the cursor position to that of the old window. */
1098 curwin->w_cursor.lnum = diff_get_corresponding_line(
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001099 old_curbuf.br_buf,
Bram Moolenaarf29a82d2015-12-17 15:03:55 +01001100 old_curwin->w_cursor.lnum,
1101 curbuf,
1102 curwin->w_cursor.lnum);
1103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 }
1105 }
1106}
1107
1108/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001109 * Set options to show diffs for the current window.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001112ex_diffthis(exarg_T *eap UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113{
1114 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1115 diff_win_options(curwin, TRUE);
1116}
1117
1118/*
1119 * Set options in window "wp" for diff mode.
1120 */
1121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001122diff_win_options(
1123 win_T *wp,
1124 int addbuf) /* Add buffer to diff. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125{
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001126# ifdef FEAT_FOLDING
1127 win_T *old_curwin = curwin;
1128
1129 /* close the manually opened folds */
1130 curwin = wp;
1131 newFoldLevel();
1132 curwin = old_curwin;
1133# endif
1134
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001135 /* Use 'scrollbind' and 'cursorbind' when available */
1136#ifdef FEAT_SCROLLBIND
Bram Moolenaar43929962015-07-03 15:06:56 +02001137 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001138 wp->w_p_scb_save = wp->w_p_scb;
Bram Moolenaar3368ea22010-09-21 16:56:35 +02001139 wp->w_p_scb = TRUE;
1140#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02001141#ifdef FEAT_CURSORBIND
Bram Moolenaar43929962015-07-03 15:06:56 +02001142 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001143 wp->w_p_crb_save = wp->w_p_crb;
Bram Moolenaar860cae12010-06-05 23:22:07 +02001144 wp->w_p_crb = TRUE;
1145#endif
Bram Moolenaar43929962015-07-03 15:06:56 +02001146 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001147 wp->w_p_wrap_save = wp->w_p_wrap;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001148 wp->w_p_wrap = FALSE;
1149# ifdef FEAT_FOLDING
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001150 curwin = wp;
1151 curbuf = curwin->w_buffer;
Bram Moolenaar43929962015-07-03 15:06:56 +02001152 if (!wp->w_p_diff)
1153 {
1154 if (wp->w_p_diff_saved)
1155 free_string_option(wp->w_p_fdm_save);
Bram Moolenaara87aa802013-07-03 15:47:03 +02001156 wp->w_p_fdm_save = vim_strsave(wp->w_p_fdm);
Bram Moolenaar43929962015-07-03 15:06:56 +02001157 }
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001158 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"diff",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001159 OPT_LOCAL|OPT_FREE, 0);
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001160 curwin = old_curwin;
1161 curbuf = curwin->w_buffer;
Bram Moolenaar43929962015-07-03 15:06:56 +02001162 if (!wp->w_p_diff)
Bram Moolenaara87aa802013-07-03 15:47:03 +02001163 {
1164 wp->w_p_fdc_save = wp->w_p_fdc;
1165 wp->w_p_fen_save = wp->w_p_fen;
1166 wp->w_p_fdl_save = wp->w_p_fdl;
1167 }
Bram Moolenaarf4d7f942010-02-24 14:34:19 +01001168 wp->w_p_fdc = diff_foldcolumn;
1169 wp->w_p_fen = TRUE;
1170 wp->w_p_fdl = 0;
1171 foldUpdateAll(wp);
1172 /* make sure topline is not halfway a fold */
1173 changed_window_setting_win(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174# endif
1175#ifdef FEAT_SCROLLBIND
1176 if (vim_strchr(p_sbo, 'h') == NULL)
1177 do_cmdline_cmd((char_u *)"set sbo+=hor");
1178#endif
Bram Moolenaara87aa802013-07-03 15:47:03 +02001179 /* Saved the current values, to be restored in ex_diffoff(). */
1180 wp->w_p_diff_saved = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
Bram Moolenaar43929962015-07-03 15:06:56 +02001182 wp->w_p_diff = TRUE;
1183
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 if (addbuf)
1185 diff_buf_add(wp->w_buffer);
1186 redraw_win_later(wp, NOT_VALID);
1187}
1188
1189/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001190 * Set options not to show diffs. For the current window or all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001191 * Only in the current tab page.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001192 */
1193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001194ex_diffoff(exarg_T *eap)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001195{
1196 win_T *wp;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001197#ifdef FEAT_SCROLLBIND
1198 int diffwin = FALSE;
1199#endif
1200
Bram Moolenaar29323592016-07-24 22:04:11 +02001201 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001202 {
Bram Moolenaar00462ff2013-09-20 20:13:53 +02001203 if (eap->forceit ? wp->w_p_diff : wp == curwin)
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001204 {
Bram Moolenaar43929962015-07-03 15:06:56 +02001205 /* Set 'diff' off. If option values were saved in
1206 * diff_win_options(), restore the ones whose settings seem to have
1207 * been left over from diff mode. */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001208 wp->w_p_diff = FALSE;
Bram Moolenaara87aa802013-07-03 15:47:03 +02001209
Bram Moolenaara87aa802013-07-03 15:47:03 +02001210 if (wp->w_p_diff_saved)
1211 {
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001212
Bram Moolenaar43929962015-07-03 15:06:56 +02001213#ifdef FEAT_SCROLLBIND
1214 if (wp->w_p_scb)
1215 wp->w_p_scb = wp->w_p_scb_save;
1216#endif
1217#ifdef FEAT_CURSORBIND
1218 if (wp->w_p_crb)
1219 wp->w_p_crb = wp->w_p_crb_save;
1220#endif
1221 if (!wp->w_p_wrap)
1222 wp->w_p_wrap = wp->w_p_wrap_save;
1223#ifdef FEAT_FOLDING
1224 free_string_option(wp->w_p_fdm);
1225 wp->w_p_fdm = vim_strsave(wp->w_p_fdm_save);
1226
1227 if (wp->w_p_fdc == diff_foldcolumn)
1228 wp->w_p_fdc = wp->w_p_fdc_save;
1229 if (wp->w_p_fdl == 0)
1230 wp->w_p_fdl = wp->w_p_fdl_save;
1231
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001232 /* Only restore 'foldenable' when 'foldmethod' is not
1233 * "manual", otherwise we continue to show the diff folds. */
Bram Moolenaar43929962015-07-03 15:06:56 +02001234 if (wp->w_p_fen)
1235 wp->w_p_fen = foldmethodIsManual(wp) ? FALSE
1236 : wp->w_p_fen_save;
1237
1238 foldUpdateAll(wp);
Bram Moolenaar43929962015-07-03 15:06:56 +02001239#endif
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001240 }
Bram Moolenaare67d5462016-08-27 22:40:42 +02001241 /* remove filler lines */
1242 wp->w_topfill = 0;
1243
1244 /* make sure topline is not halfway a fold and cursor is
1245 * invalidated */
1246 changed_window_setting_win(wp);
Bram Moolenaar33ca6bf2013-07-17 13:43:39 +02001247
Bram Moolenaara87aa802013-07-03 15:47:03 +02001248 /* Note: 'sbo' is not restored, it's a global option. */
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001249 diff_buf_adjust(wp);
1250 }
1251#ifdef FEAT_SCROLLBIND
1252 diffwin |= wp->w_p_diff;
1253#endif
1254 }
1255
1256#ifdef FEAT_SCROLLBIND
1257 /* Remove "hor" from from 'scrollopt' if there are no diff windows left. */
1258 if (!diffwin && vim_strchr(p_sbo, 'h') != NULL)
1259 do_cmdline_cmd((char_u *)"set sbo-=hor");
1260#endif
1261}
1262
1263/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 * Read the diff output and add each entry to the diff list.
1265 */
1266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001267diff_read(
1268 int idx_orig, /* idx of original file */
1269 int idx_new, /* idx of new file */
1270 char_u *fname) /* name of diff output file */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271{
1272 FILE *fd;
1273 diff_T *dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001274 diff_T *dp = curtab->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 diff_T *dn, *dpl;
1276 long f1, l1, f2, l2;
1277 char_u linebuf[LBUFLEN]; /* only need to hold the diff line */
1278 int difftype;
1279 char_u *p;
1280 long off;
1281 int i;
1282 linenr_T lnum_orig, lnum_new;
1283 long count_orig, count_new;
1284 int notset = TRUE; /* block "*dp" not set yet */
1285
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001286 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 if (fd == NULL)
1288 {
1289 EMSG(_("E98: Cannot read diff output"));
1290 return;
1291 }
1292
1293 for (;;)
1294 {
1295 if (tag_fgets(linebuf, LBUFLEN, fd))
1296 break; /* end of file */
1297 if (!isdigit(*linebuf))
1298 continue; /* not the start of a diff block */
1299
1300 /* This line must be one of three formats:
1301 * {first}[,{last}]c{first}[,{last}]
1302 * {first}a{first}[,{last}]
1303 * {first}[,{last}]d{first}
1304 */
1305 p = linebuf;
1306 f1 = getdigits(&p);
1307 if (*p == ',')
1308 {
1309 ++p;
1310 l1 = getdigits(&p);
1311 }
1312 else
1313 l1 = f1;
1314 if (*p != 'a' && *p != 'c' && *p != 'd')
1315 continue; /* invalid diff format */
1316 difftype = *p++;
1317 f2 = getdigits(&p);
1318 if (*p == ',')
1319 {
1320 ++p;
1321 l2 = getdigits(&p);
1322 }
1323 else
1324 l2 = f2;
1325 if (l1 < f1 || l2 < f2)
1326 continue; /* invalid line range */
1327
1328 if (difftype == 'a')
1329 {
1330 lnum_orig = f1 + 1;
1331 count_orig = 0;
1332 }
1333 else
1334 {
1335 lnum_orig = f1;
1336 count_orig = l1 - f1 + 1;
1337 }
1338 if (difftype == 'd')
1339 {
1340 lnum_new = f2 + 1;
1341 count_new = 0;
1342 }
1343 else
1344 {
1345 lnum_new = f2;
1346 count_new = l2 - f2 + 1;
1347 }
1348
1349 /* Go over blocks before the change, for which orig and new are equal.
1350 * Copy blocks from orig to new. */
1351 while (dp != NULL
1352 && lnum_orig > dp->df_lnum[idx_orig] + dp->df_count[idx_orig])
1353 {
1354 if (notset)
1355 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1356 dprev = dp;
1357 dp = dp->df_next;
1358 notset = TRUE;
1359 }
1360
1361 if (dp != NULL
1362 && lnum_orig <= dp->df_lnum[idx_orig] + dp->df_count[idx_orig]
1363 && lnum_orig + count_orig >= dp->df_lnum[idx_orig])
1364 {
1365 /* New block overlaps with existing block(s).
1366 * First find last block that overlaps. */
1367 for (dpl = dp; dpl->df_next != NULL; dpl = dpl->df_next)
1368 if (lnum_orig + count_orig < dpl->df_next->df_lnum[idx_orig])
1369 break;
1370
1371 /* If the newly found block starts before the old one, set the
1372 * start back a number of lines. */
1373 off = dp->df_lnum[idx_orig] - lnum_orig;
1374 if (off > 0)
1375 {
1376 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001377 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 dp->df_lnum[i] -= off;
1379 dp->df_lnum[idx_new] = lnum_new;
1380 dp->df_count[idx_new] = count_new;
1381 }
1382 else if (notset)
1383 {
1384 /* new block inside existing one, adjust new block */
1385 dp->df_lnum[idx_new] = lnum_new + off;
1386 dp->df_count[idx_new] = count_new - off;
1387 }
1388 else
1389 /* second overlap of new block with existing block */
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001390 dp->df_count[idx_new] += count_new - count_orig
1391 + dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]
1392 - (dp->df_lnum[idx_orig] + dp->df_count[idx_orig]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393
1394 /* Adjust the size of the block to include all the lines to the
1395 * end of the existing block or the new diff, whatever ends last. */
1396 off = (lnum_orig + count_orig)
1397 - (dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]);
1398 if (off < 0)
1399 {
1400 /* new change ends in existing block, adjust the end if not
1401 * done already */
1402 if (notset)
1403 dp->df_count[idx_new] += -off;
1404 off = 0;
1405 }
Bram Moolenaard4b96bc2007-10-19 15:33:39 +00001406 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001407 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001408 dp->df_count[i] = dpl->df_lnum[i] + dpl->df_count[i]
1409 - dp->df_lnum[i] + off;
1410
1411 /* Delete the diff blocks that have been merged into one. */
1412 dn = dp->df_next;
1413 dp->df_next = dpl->df_next;
1414 while (dn != dp->df_next)
1415 {
1416 dpl = dn->df_next;
1417 vim_free(dn);
1418 dn = dpl;
1419 }
1420 }
1421 else
1422 {
1423 /* Allocate a new diffblock. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001424 dp = diff_alloc_new(curtab, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 if (dp == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001426 goto done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
1428 dp->df_lnum[idx_orig] = lnum_orig;
1429 dp->df_count[idx_orig] = count_orig;
1430 dp->df_lnum[idx_new] = lnum_new;
1431 dp->df_count[idx_new] = count_new;
1432
1433 /* Set values for other buffers, these must be equal to the
1434 * original buffer, otherwise there would have been a change
1435 * already. */
1436 for (i = idx_orig + 1; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001437 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 diff_copy_entry(dprev, dp, idx_orig, i);
1439 }
1440 notset = FALSE; /* "*dp" has been set */
1441 }
1442
1443 /* for remaining diff blocks orig and new are equal */
1444 while (dp != NULL)
1445 {
1446 if (notset)
1447 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1448 dprev = dp;
1449 dp = dp->df_next;
1450 notset = TRUE;
1451 }
1452
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001453done:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 fclose(fd);
1455}
1456
1457/*
1458 * Copy an entry at "dp" from "idx_orig" to "idx_new".
1459 */
1460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001461diff_copy_entry(
1462 diff_T *dprev,
1463 diff_T *dp,
1464 int idx_orig,
1465 int idx_new)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466{
1467 long off;
1468
1469 if (dprev == NULL)
1470 off = 0;
1471 else
1472 off = (dprev->df_lnum[idx_orig] + dprev->df_count[idx_orig])
1473 - (dprev->df_lnum[idx_new] + dprev->df_count[idx_new]);
1474 dp->df_lnum[idx_new] = dp->df_lnum[idx_orig] - off;
1475 dp->df_count[idx_new] = dp->df_count[idx_orig];
1476}
1477
1478/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001479 * Clear the list of diffblocks for tab page "tp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 */
Bram Moolenaarea408852005-06-25 22:49:46 +00001481 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001482diff_clear(tabpage_T *tp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483{
1484 diff_T *p, *next_p;
1485
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001486 for (p = tp->tp_first_diff; p != NULL; p = next_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487 {
1488 next_p = p->df_next;
1489 vim_free(p);
1490 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001491 tp->tp_first_diff = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492}
1493
1494/*
1495 * Check diff status for line "lnum" in buffer "buf":
1496 * Returns 0 for nothing special
1497 * Returns -1 for a line that should be highlighted as changed.
1498 * Returns -2 for a line that should be highlighted as added/deleted.
1499 * Returns > 0 for inserting that many filler lines above it (never happens
1500 * when 'diffopt' doesn't contain "filler").
1501 * This should only be used for windows where 'diff' is set.
1502 */
1503 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001504diff_check(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001506 int idx; /* index in tp_diffbuf[] for this buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 diff_T *dp;
1508 int maxcount;
1509 int i;
1510 buf_T *buf = wp->w_buffer;
1511 int cmp;
1512
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001513 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 ex_diffupdate(NULL); /* update after a big change */
1515
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001516 if (curtab->tp_first_diff == NULL || !wp->w_p_diff) /* no diffs at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 return 0;
1518
1519 /* safety check: "lnum" must be a buffer line */
1520 if (lnum < 1 || lnum > buf->b_ml.ml_line_count + 1)
1521 return 0;
1522
1523 idx = diff_buf_idx(buf);
1524 if (idx == DB_COUNT)
1525 return 0; /* no diffs for buffer "buf" */
1526
1527#ifdef FEAT_FOLDING
1528 /* A closed fold never has filler lines. */
1529 if (hasFoldingWin(wp, lnum, NULL, NULL, TRUE, NULL))
1530 return 0;
1531#endif
1532
1533 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001534 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001535 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
1536 break;
1537 if (dp == NULL || lnum < dp->df_lnum[idx])
1538 return 0;
1539
1540 if (lnum < dp->df_lnum[idx] + dp->df_count[idx])
1541 {
1542 int zero = FALSE;
1543
1544 /* Changed or inserted line. If the other buffers have a count of
1545 * zero, the lines were inserted. If the other buffers have the same
1546 * count, check if the lines are identical. */
1547 cmp = FALSE;
1548 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001549 if (i != idx && curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001550 {
1551 if (dp->df_count[i] == 0)
1552 zero = TRUE;
1553 else
1554 {
1555 if (dp->df_count[i] != dp->df_count[idx])
1556 return -1; /* nr of lines changed. */
1557 cmp = TRUE;
1558 }
1559 }
1560 if (cmp)
1561 {
1562 /* Compare all lines. If they are equal the lines were inserted
1563 * in some buffers, deleted in others, but not changed. */
1564 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar7c0a2f32016-07-10 22:11:16 +02001565 if (i != idx && curtab->tp_diffbuf[i] != NULL
1566 && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001567 if (!diff_equal_entry(dp, idx, i))
1568 return -1;
1569 }
1570 /* If there is no buffer with zero lines then there is no difference
1571 * any longer. Happens when making a change (or undo) that removes
1572 * the difference. Can't remove the entry here, we might be halfway
1573 * updating the window. Just report the text as unchanged. Other
1574 * windows might still show the change though. */
1575 if (zero == FALSE)
1576 return 0;
1577 return -2;
1578 }
1579
1580 /* If 'diffopt' doesn't contain "filler", return 0. */
1581 if (!(diff_flags & DIFF_FILLER))
1582 return 0;
1583
1584 /* Insert filler lines above the line just below the change. Will return
1585 * 0 when this buf had the max count. */
1586 maxcount = 0;
1587 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001588 if (curtab->tp_diffbuf[i] != NULL && dp->df_count[i] > maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 maxcount = dp->df_count[i];
1590 return maxcount - dp->df_count[idx];
1591}
1592
1593/*
1594 * Compare two entries in diff "*dp" and return TRUE if they are equal.
1595 */
1596 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001597diff_equal_entry(diff_T *dp, int idx1, int idx2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598{
1599 int i;
1600 char_u *line;
1601 int cmp;
1602
1603 if (dp->df_count[idx1] != dp->df_count[idx2])
1604 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001605 if (diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001606 return FALSE;
1607 for (i = 0; i < dp->df_count[idx1]; ++i)
1608 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001609 line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001610 dp->df_lnum[idx1] + i, FALSE));
1611 if (line == NULL)
1612 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001613 cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001614 dp->df_lnum[idx2] + i, FALSE));
1615 vim_free(line);
1616 if (cmp != 0)
1617 return FALSE;
1618 }
1619 return TRUE;
1620}
1621
1622/*
1623 * Compare strings "s1" and "s2" according to 'diffopt'.
1624 * Return non-zero when they are different.
1625 */
1626 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001627diff_cmp(char_u *s1, char_u *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628{
1629 char_u *p1, *p2;
1630#ifdef FEAT_MBYTE
1631 int l;
1632#endif
1633
1634 if ((diff_flags & (DIFF_ICASE | DIFF_IWHITE)) == 0)
1635 return STRCMP(s1, s2);
1636 if ((diff_flags & DIFF_ICASE) && !(diff_flags & DIFF_IWHITE))
1637 return MB_STRICMP(s1, s2);
1638
1639 /* Ignore white space changes and possibly ignore case. */
1640 p1 = s1;
1641 p2 = s2;
1642 while (*p1 != NUL && *p2 != NUL)
1643 {
1644 if (vim_iswhite(*p1) && vim_iswhite(*p2))
1645 {
1646 p1 = skipwhite(p1);
1647 p2 = skipwhite(p2);
1648 }
1649 else
1650 {
1651#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001652 l = (*mb_ptr2len)(p1);
1653 if (l != (*mb_ptr2len)(p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 break;
1655 if (l > 1)
1656 {
1657 if (STRNCMP(p1, p2, l) != 0
1658 && (!enc_utf8
1659 || !(diff_flags & DIFF_ICASE)
1660 || utf_fold(utf_ptr2char(p1))
1661 != utf_fold(utf_ptr2char(p2))))
1662 break;
1663 p1 += l;
1664 p2 += l;
1665 }
1666 else
1667#endif
1668 {
1669 if (*p1 != *p2 && (!(diff_flags & DIFF_ICASE)
1670 || TOLOWER_LOC(*p1) != TOLOWER_LOC(*p2)))
1671 break;
1672 ++p1;
1673 ++p2;
1674 }
1675 }
1676 }
1677
1678 /* Ignore trailing white space. */
1679 p1 = skipwhite(p1);
1680 p2 = skipwhite(p2);
1681 if (*p1 != NUL || *p2 != NUL)
1682 return 1;
1683 return 0;
1684}
1685
1686/*
1687 * Return the number of filler lines above "lnum".
1688 */
1689 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001690diff_check_fill(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691{
1692 int n;
1693
1694 /* be quick when there are no filler lines */
1695 if (!(diff_flags & DIFF_FILLER))
1696 return 0;
1697 n = diff_check(wp, lnum);
1698 if (n <= 0)
1699 return 0;
1700 return n;
1701}
1702
1703/*
1704 * Set the topline of "towin" to match the position in "fromwin", so that they
1705 * show the same diff'ed lines.
1706 */
1707 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001708diff_set_topline(win_T *fromwin, win_T *towin)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001709{
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001710 buf_T *frombuf = fromwin->w_buffer;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001711 linenr_T lnum = fromwin->w_topline;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001712 int fromidx;
1713 int toidx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714 diff_T *dp;
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001715 int max_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 int i;
1717
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001718 fromidx = diff_buf_idx(frombuf);
1719 if (fromidx == DB_COUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001720 return; /* safety check */
1721
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001722 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723 ex_diffupdate(NULL); /* update after a big change */
1724
1725 towin->w_topfill = 0;
1726
1727 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001728 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001729 if (lnum <= dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730 break;
1731 if (dp == NULL)
1732 {
1733 /* After last change, compute topline relative to end of file; no
1734 * filler lines. */
1735 towin->w_topline = towin->w_buffer->b_ml.ml_line_count
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001736 - (frombuf->b_ml.ml_line_count - lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 }
1738 else
1739 {
1740 /* Find index for "towin". */
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001741 toidx = diff_buf_idx(towin->w_buffer);
1742 if (toidx == DB_COUNT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743 return; /* safety check */
1744
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001745 towin->w_topline = lnum + (dp->df_lnum[toidx] - dp->df_lnum[fromidx]);
1746 if (lnum >= dp->df_lnum[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001747 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001748 /* Inside a change: compute filler lines. With three or more
1749 * buffers we need to know the largest count. */
1750 max_count = 0;
1751 for (i = 0; i < DB_COUNT; ++i)
1752 if (curtab->tp_diffbuf[i] != NULL
1753 && max_count < dp->df_count[i])
1754 max_count = dp->df_count[i];
1755
1756 if (dp->df_count[toidx] == dp->df_count[fromidx])
1757 {
1758 /* same number of lines: use same filler count */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001759 towin->w_topfill = fromwin->w_topfill;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 }
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001761 else if (dp->df_count[toidx] > dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001762 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001763 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00001764 {
Bram Moolenaarbb8f88b2008-01-18 16:40:00 +00001765 /* more lines in towin and fromwin doesn't show diff
1766 * lines, only filler lines */
1767 if (max_count - fromwin->w_topfill >= dp->df_count[toidx])
1768 {
1769 /* towin also only shows filler lines */
1770 towin->w_topline = dp->df_lnum[toidx]
1771 + dp->df_count[toidx];
1772 towin->w_topfill = fromwin->w_topfill;
1773 }
1774 else
1775 /* towin still has some diff lines to show */
1776 towin->w_topline = dp->df_lnum[toidx]
1777 + max_count - fromwin->w_topfill;
1778 }
1779 }
1780 else if (towin->w_topline >= dp->df_lnum[toidx]
1781 + dp->df_count[toidx])
1782 {
1783 /* less lines in towin and no diff lines to show: compute
1784 * filler lines */
1785 towin->w_topline = dp->df_lnum[toidx] + dp->df_count[toidx];
1786 if (diff_flags & DIFF_FILLER)
1787 {
1788 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
1789 /* fromwin is also out of diff lines */
1790 towin->w_topfill = fromwin->w_topfill;
1791 else
1792 /* fromwin has some diff lines */
1793 towin->w_topfill = dp->df_lnum[fromidx]
1794 + max_count - lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 }
1796 }
1797 }
1798 }
1799
1800 /* safety check (if diff info gets outdated strange things may happen) */
1801 towin->w_botfill = FALSE;
1802 if (towin->w_topline > towin->w_buffer->b_ml.ml_line_count)
1803 {
1804 towin->w_topline = towin->w_buffer->b_ml.ml_line_count;
1805 towin->w_botfill = TRUE;
1806 }
1807 if (towin->w_topline < 1)
1808 {
1809 towin->w_topline = 1;
1810 towin->w_topfill = 0;
1811 }
1812
1813 /* When w_topline changes need to recompute w_botline and cursor position */
1814 invalidate_botline_win(towin);
1815 changed_line_abv_curs_win(towin);
1816
1817 check_topfill(towin, FALSE);
1818#ifdef FEAT_FOLDING
1819 (void)hasFoldingWin(towin, towin->w_topline, &towin->w_topline,
1820 NULL, TRUE, NULL);
1821#endif
1822}
1823
1824/*
1825 * This is called when 'diffopt' is changed.
1826 */
1827 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001828diffopt_changed(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829{
1830 char_u *p;
1831 int diff_context_new = 6;
1832 int diff_flags_new = 0;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001833 int diff_foldcolumn_new = 2;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001834 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835
1836 p = p_dip;
1837 while (*p != NUL)
1838 {
1839 if (STRNCMP(p, "filler", 6) == 0)
1840 {
1841 p += 6;
1842 diff_flags_new |= DIFF_FILLER;
1843 }
1844 else if (STRNCMP(p, "context:", 8) == 0 && VIM_ISDIGIT(p[8]))
1845 {
1846 p += 8;
1847 diff_context_new = getdigits(&p);
1848 }
1849 else if (STRNCMP(p, "icase", 5) == 0)
1850 {
1851 p += 5;
1852 diff_flags_new |= DIFF_ICASE;
1853 }
1854 else if (STRNCMP(p, "iwhite", 6) == 0)
1855 {
1856 p += 6;
1857 diff_flags_new |= DIFF_IWHITE;
1858 }
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001859 else if (STRNCMP(p, "horizontal", 10) == 0)
1860 {
1861 p += 10;
1862 diff_flags_new |= DIFF_HORIZONTAL;
1863 }
1864 else if (STRNCMP(p, "vertical", 8) == 0)
1865 {
1866 p += 8;
1867 diff_flags_new |= DIFF_VERTICAL;
1868 }
1869 else if (STRNCMP(p, "foldcolumn:", 11) == 0 && VIM_ISDIGIT(p[11]))
1870 {
1871 p += 11;
1872 diff_foldcolumn_new = getdigits(&p);
1873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (*p != ',' && *p != NUL)
1875 return FAIL;
1876 if (*p == ',')
1877 ++p;
1878 }
1879
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001880 /* Can't have both "horizontal" and "vertical". */
1881 if ((diff_flags_new & DIFF_HORIZONTAL) && (diff_flags_new & DIFF_VERTICAL))
1882 return FAIL;
1883
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884 /* If "icase" or "iwhite" was added or removed, need to update the diff. */
1885 if (diff_flags != diff_flags_new)
Bram Moolenaar29323592016-07-24 22:04:11 +02001886 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001887 tp->tp_diff_invalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888
1889 diff_flags = diff_flags_new;
1890 diff_context = diff_context_new;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001891 diff_foldcolumn = diff_foldcolumn_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892
1893 diff_redraw(TRUE);
1894
1895 /* recompute the scroll binding with the new option value, may
1896 * remove or add filler lines */
1897 check_scrollbind((linenr_T)0, 0L);
1898
1899 return OK;
1900}
1901
1902/*
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001903 * Return TRUE if 'diffopt' contains "horizontal".
1904 */
1905 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001906diffopt_horizontal(void)
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001907{
1908 return (diff_flags & DIFF_HORIZONTAL) != 0;
1909}
1910
1911/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 * Find the difference within a changed line.
1913 * Returns TRUE if the line was added, no other buffer has it.
1914 */
1915 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001916diff_find_change(
1917 win_T *wp,
1918 linenr_T lnum,
1919 int *startp, /* first char of the change */
1920 int *endp) /* last char of the change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001921{
1922 char_u *line_org;
1923 char_u *line_new;
1924 int i;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001925 int si_org, si_new;
1926 int ei_org, ei_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 diff_T *dp;
1928 int idx;
1929 int off;
1930 int added = TRUE;
1931
1932 /* Make a copy of the line, the next ml_get() will invalidate it. */
1933 line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE));
1934 if (line_org == NULL)
1935 return FALSE;
1936
1937 idx = diff_buf_idx(wp->w_buffer);
1938 if (idx == DB_COUNT) /* cannot happen */
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00001939 {
1940 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00001942 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943
1944 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001945 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001946 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
1947 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001948 if (dp == NULL || diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00001949 {
1950 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00001952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953
1954 off = lnum - dp->df_lnum[idx];
1955
1956 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001957 if (curtab->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 {
1959 /* Skip lines that are not in the other change (filler lines). */
1960 if (off >= dp->df_count[i])
1961 continue;
1962 added = FALSE;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001963 line_new = ml_get_buf(curtab->tp_diffbuf[i],
1964 dp->df_lnum[i] + off, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965
1966 /* Search for start of difference */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001967 si_org = si_new = 0;
1968 while (line_org[si_org] != NUL)
1969 {
1970 if ((diff_flags & DIFF_IWHITE)
1971 && vim_iswhite(line_org[si_org])
1972 && vim_iswhite(line_new[si_new]))
1973 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001974 si_org = (int)(skipwhite(line_org + si_org) - line_org);
1975 si_new = (int)(skipwhite(line_new + si_new) - line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001976 }
1977 else
1978 {
1979 if (line_org[si_org] != line_new[si_new])
1980 break;
1981 ++si_org;
1982 ++si_new;
1983 }
1984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985#ifdef FEAT_MBYTE
1986 if (has_mbyte)
1987 {
1988 /* Move back to first byte of character in both lines (may
1989 * have "nn^" in line_org and "n^ in line_new). */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001990 si_org -= (*mb_head_off)(line_org, line_org + si_org);
1991 si_new -= (*mb_head_off)(line_new, line_new + si_new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 }
1993#endif
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001994 if (*startp > si_org)
1995 *startp = si_org;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996
1997 /* Search for end of difference, if any. */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001998 if (line_org[si_org] != NUL || line_new[si_new] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 {
2000 ei_org = (int)STRLEN(line_org);
2001 ei_new = (int)STRLEN(line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002002 while (ei_org >= *startp && ei_new >= si_new
2003 && ei_org >= 0 && ei_new >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002004 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002005 if ((diff_flags & DIFF_IWHITE)
2006 && vim_iswhite(line_org[ei_org])
2007 && vim_iswhite(line_new[ei_new]))
2008 {
2009 while (ei_org >= *startp
2010 && vim_iswhite(line_org[ei_org]))
2011 --ei_org;
2012 while (ei_new >= si_new
2013 && vim_iswhite(line_new[ei_new]))
2014 --ei_new;
2015 }
2016 else
2017 {
2018 if (line_org[ei_org] != line_new[ei_new])
2019 break;
2020 --ei_org;
2021 --ei_new;
2022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 }
2024 if (*endp < ei_org)
2025 *endp = ei_org;
2026 }
2027 }
2028
2029 vim_free(line_org);
2030 return added;
2031}
2032
2033#if defined(FEAT_FOLDING) || defined(PROTO)
2034/*
2035 * Return TRUE if line "lnum" is not close to a diff block, this line should
2036 * be in a fold.
2037 * Return FALSE if there are no diff blocks at all in this window.
2038 */
2039 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002040diff_infold(win_T *wp, linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002041{
2042 int i;
2043 int idx = -1;
2044 int other = FALSE;
2045 diff_T *dp;
2046
2047 /* Return if 'diff' isn't set. */
2048 if (!wp->w_p_diff)
2049 return FALSE;
2050
2051 for (i = 0; i < DB_COUNT; ++i)
2052 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002053 if (curtab->tp_diffbuf[i] == wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 idx = i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002055 else if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 other = TRUE;
2057 }
2058
2059 /* return here if there are no diffs in the window */
2060 if (idx == -1 || !other)
2061 return FALSE;
2062
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002063 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 ex_diffupdate(NULL); /* update after a big change */
2065
2066 /* Return if there are no diff blocks. All lines will be folded. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002067 if (curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 return TRUE;
2069
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002070 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 {
2072 /* If this change is below the line there can't be any further match. */
2073 if (dp->df_lnum[idx] - diff_context > lnum)
2074 break;
2075 /* If this change ends before the line we have a match. */
2076 if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum)
2077 return FALSE;
2078 }
2079 return TRUE;
2080}
2081#endif
2082
2083/*
2084 * "dp" and "do" commands.
2085 */
2086 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002087nv_diffgetput(int put, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002088{
2089 exarg_T ea;
Bram Moolenaar6a643652014-10-31 13:54:25 +01002090 char_u buf[30];
Bram Moolenaar071d4272004-06-13 20:20:40 +00002091
Bram Moolenaar6a643652014-10-31 13:54:25 +01002092 if (count == 0)
2093 ea.arg = (char_u *)"";
2094 else
2095 {
2096 vim_snprintf((char *)buf, 30, "%ld", count);
2097 ea.arg = buf;
2098 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002099 if (put)
2100 ea.cmdidx = CMD_diffput;
2101 else
2102 ea.cmdidx = CMD_diffget;
2103 ea.addr_count = 0;
2104 ea.line1 = curwin->w_cursor.lnum;
2105 ea.line2 = curwin->w_cursor.lnum;
2106 ex_diffgetput(&ea);
2107}
2108
2109/*
2110 * ":diffget"
2111 * ":diffput"
2112 */
2113 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002114ex_diffgetput(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115{
2116 linenr_T lnum;
2117 int count;
2118 linenr_T off = 0;
2119 diff_T *dp;
2120 diff_T *dprev;
2121 diff_T *dfree;
2122 int idx_cur;
2123 int idx_other;
2124 int idx_from;
2125 int idx_to;
2126 int i;
2127 int added;
2128 char_u *p;
2129 aco_save_T aco;
2130 buf_T *buf;
2131 int start_skip, end_skip;
2132 int new_count;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002133 int buf_empty;
Bram Moolenaar602eb742007-02-20 03:43:38 +00002134 int found_not_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135
2136 /* Find the current buffer in the list of diff buffers. */
2137 idx_cur = diff_buf_idx(curbuf);
2138 if (idx_cur == DB_COUNT)
2139 {
2140 EMSG(_("E99: Current buffer is not in diff mode"));
2141 return;
2142 }
2143
2144 if (*eap->arg == NUL)
2145 {
2146 /* No argument: Find the other buffer in the list of diff buffers. */
2147 for (idx_other = 0; idx_other < DB_COUNT; ++idx_other)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002148 if (curtab->tp_diffbuf[idx_other] != curbuf
Bram Moolenaar602eb742007-02-20 03:43:38 +00002149 && curtab->tp_diffbuf[idx_other] != NULL)
2150 {
2151 if (eap->cmdidx != CMD_diffput
2152 || curtab->tp_diffbuf[idx_other]->b_p_ma)
2153 break;
2154 found_not_ma = TRUE;
2155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 if (idx_other == DB_COUNT)
2157 {
Bram Moolenaar602eb742007-02-20 03:43:38 +00002158 if (found_not_ma)
2159 EMSG(_("E793: No other buffer in diff mode is modifiable"));
2160 else
2161 EMSG(_("E100: No other buffer in diff mode"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162 return;
2163 }
2164
2165 /* Check that there isn't a third buffer in the list */
2166 for (i = idx_other + 1; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002167 if (curtab->tp_diffbuf[i] != curbuf
2168 && curtab->tp_diffbuf[i] != NULL
2169 && (eap->cmdidx != CMD_diffput || curtab->tp_diffbuf[i]->b_p_ma))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 {
2171 EMSG(_("E101: More than two buffers in diff mode, don't know which one to use"));
2172 return;
2173 }
2174 }
2175 else
2176 {
2177 /* Buffer number or pattern given. Ignore trailing white space. */
2178 p = eap->arg + STRLEN(eap->arg);
2179 while (p > eap->arg && vim_iswhite(p[-1]))
2180 --p;
2181 for (i = 0; vim_isdigit(eap->arg[i]) && eap->arg + i < p; ++i)
2182 ;
2183 if (eap->arg + i == p) /* digits only */
2184 i = atol((char *)eap->arg);
2185 else
2186 {
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01002187 i = buflist_findpat(eap->arg, p, FALSE, TRUE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002188 if (i < 0)
2189 return; /* error message already given */
2190 }
2191 buf = buflist_findnr(i);
2192 if (buf == NULL)
2193 {
2194 EMSG2(_("E102: Can't find buffer \"%s\""), eap->arg);
2195 return;
2196 }
Bram Moolenaar5cc6a6e2009-01-22 19:48:55 +00002197 if (buf == curbuf)
2198 return; /* nothing to do */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 idx_other = diff_buf_idx(buf);
2200 if (idx_other == DB_COUNT)
2201 {
2202 EMSG2(_("E103: Buffer \"%s\" is not in diff mode"), eap->arg);
2203 return;
2204 }
2205 }
2206
2207 diff_busy = TRUE;
2208
2209 /* When no range given include the line above or below the cursor. */
2210 if (eap->addr_count == 0)
2211 {
2212 /* Make it possible that ":diffget" on the last line gets line below
2213 * the cursor line when there is no difference above the cursor. */
2214 if (eap->cmdidx == CMD_diffget
2215 && eap->line1 == curbuf->b_ml.ml_line_count
2216 && diff_check(curwin, eap->line1) == 0
2217 && (eap->line1 == 1 || diff_check(curwin, eap->line1 - 1) == 0))
2218 ++eap->line2;
2219 else if (eap->line1 > 0)
2220 --eap->line1;
2221 }
2222
2223 if (eap->cmdidx == CMD_diffget)
2224 {
2225 idx_from = idx_other;
2226 idx_to = idx_cur;
2227 }
2228 else
2229 {
2230 idx_from = idx_cur;
2231 idx_to = idx_other;
2232 /* Need to make the other buffer the current buffer to be able to make
2233 * changes in it. */
2234 /* set curwin/curbuf to buf and save a few things */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002235 aucmd_prepbuf(&aco, curtab->tp_diffbuf[idx_other]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 }
2237
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002238 /* May give the warning for a changed buffer here, which can trigger the
2239 * FileChangedRO autocommand, which may do nasty things and mess
2240 * everything up. */
2241 if (!curbuf->b_changed)
2242 {
2243 change_warning(0);
2244 if (diff_buf_idx(curbuf) != idx_to)
2245 {
2246 EMSG(_("E787: Buffer changed unexpectedly"));
2247 return;
2248 }
2249 }
2250
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002252 for (dp = curtab->tp_first_diff; dp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002253 {
2254 if (dp->df_lnum[idx_cur] > eap->line2 + off)
2255 break; /* past the range that was specified */
2256
2257 dfree = NULL;
2258 lnum = dp->df_lnum[idx_to];
2259 count = dp->df_count[idx_to];
2260 if (dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off
2261 && u_save(lnum - 1, lnum + count) != FAIL)
2262 {
2263 /* Inside the specified range and saving for undo worked. */
2264 start_skip = 0;
2265 end_skip = 0;
2266 if (eap->addr_count > 0)
2267 {
2268 /* A range was specified: check if lines need to be skipped. */
2269 start_skip = eap->line1 + off - dp->df_lnum[idx_cur];
2270 if (start_skip > 0)
2271 {
2272 /* range starts below start of current diff block */
2273 if (start_skip > count)
2274 {
2275 lnum += count;
2276 count = 0;
2277 }
2278 else
2279 {
2280 count -= start_skip;
2281 lnum += start_skip;
2282 }
2283 }
2284 else
2285 start_skip = 0;
2286
2287 end_skip = dp->df_lnum[idx_cur] + dp->df_count[idx_cur] - 1
2288 - (eap->line2 + off);
2289 if (end_skip > 0)
2290 {
2291 /* range ends above end of current/from diff block */
2292 if (idx_cur == idx_from) /* :diffput */
2293 {
2294 i = dp->df_count[idx_cur] - start_skip - end_skip;
2295 if (count > i)
2296 count = i;
2297 }
2298 else /* :diffget */
2299 {
2300 count -= end_skip;
2301 end_skip = dp->df_count[idx_from] - start_skip - count;
2302 if (end_skip < 0)
2303 end_skip = 0;
2304 }
2305 }
2306 else
2307 end_skip = 0;
2308 }
2309
Bram Moolenaare962c672014-10-15 12:56:49 +02002310 buf_empty = bufempty();
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 added = 0;
2312 for (i = 0; i < count; ++i)
2313 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00002314 /* remember deleting the last line of the buffer */
2315 buf_empty = curbuf->b_ml.ml_line_count == 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 ml_delete(lnum, FALSE);
2317 --added;
2318 }
2319 for (i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; ++i)
2320 {
2321 linenr_T nr;
2322
2323 nr = dp->df_lnum[idx_from] + start_skip + i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002324 if (nr > curtab->tp_diffbuf[idx_from]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002326 p = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx_from],
2327 nr, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 if (p != NULL)
2329 {
2330 ml_append(lnum + i - 1, p, 0, FALSE);
2331 vim_free(p);
2332 ++added;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002333 if (buf_empty && curbuf->b_ml.ml_line_count == 2)
2334 {
2335 /* Added the first line into an empty buffer, need to
2336 * delete the dummy empty line. */
2337 buf_empty = FALSE;
2338 ml_delete((linenr_T)2, FALSE);
2339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 }
2341 }
2342 new_count = dp->df_count[idx_to] + added;
2343 dp->df_count[idx_to] = new_count;
2344
2345 if (start_skip == 0 && end_skip == 0)
2346 {
2347 /* Check if there are any other buffers and if the diff is
2348 * equal in them. */
2349 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002350 if (curtab->tp_diffbuf[i] != NULL && i != idx_from
2351 && i != idx_to
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 && !diff_equal_entry(dp, idx_from, i))
2353 break;
2354 if (i == DB_COUNT)
2355 {
2356 /* delete the diff entry, the buffers are now equal here */
2357 dfree = dp;
2358 dp = dp->df_next;
2359 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002360 curtab->tp_first_diff = dp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 else
2362 dprev->df_next = dp;
2363 }
2364 }
2365
2366 /* Adjust marks. This will change the following entries! */
2367 if (added != 0)
2368 {
2369 mark_adjust(lnum, lnum + count - 1, (long)MAXLNUM, (long)added);
2370 if (curwin->w_cursor.lnum >= lnum)
2371 {
2372 /* Adjust the cursor position if it's in/after the changed
2373 * lines. */
2374 if (curwin->w_cursor.lnum >= lnum + count)
2375 curwin->w_cursor.lnum += added;
2376 else if (added < 0)
2377 curwin->w_cursor.lnum = lnum;
2378 }
2379 }
2380 changed_lines(lnum, 0, lnum + count, (long)added);
2381
2382 if (dfree != NULL)
2383 {
2384 /* Diff is deleted, update folds in other windows. */
2385#ifdef FEAT_FOLDING
2386 diff_fold_update(dfree, idx_to);
2387#endif
2388 vim_free(dfree);
2389 }
2390 else
2391 /* mark_adjust() may have changed the count in a wrong way */
2392 dp->df_count[idx_to] = new_count;
2393
2394 /* When changing the current buffer, keep track of line numbers */
2395 if (idx_cur == idx_to)
2396 off += added;
2397 }
2398
2399 /* If before the range or not deleted, go to next diff. */
2400 if (dfree == NULL)
2401 {
2402 dprev = dp;
2403 dp = dp->df_next;
2404 }
2405 }
2406
2407 /* restore curwin/curbuf and a few other things */
Bram Moolenaara9d52e32010-07-31 16:44:19 +02002408 if (eap->cmdidx != CMD_diffget)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 {
2410 /* Syncing undo only works for the current buffer, but we change
2411 * another buffer. Sync undo if the command was typed. This isn't
2412 * 100% right when ":diffput" is used in a function or mapping. */
2413 if (KeyTyped)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002414 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 aucmd_restbuf(&aco);
2416 }
2417
2418 diff_busy = FALSE;
2419
2420 /* Check that the cursor is on a valid character and update it's position.
2421 * When there were filler lines the topline has become invalid. */
2422 check_cursor();
2423 changed_line_abv_curs();
2424
2425 /* Also need to redraw the other buffers. */
2426 diff_redraw(FALSE);
2427}
2428
2429#ifdef FEAT_FOLDING
2430/*
2431 * Update folds for all diff buffers for entry "dp".
2432 * Skip buffer with index "skip_idx".
2433 * When there are no diffs, all folds are removed.
2434 */
2435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002436diff_fold_update(diff_T *dp, int skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437{
2438 int i;
2439 win_T *wp;
2440
Bram Moolenaar29323592016-07-24 22:04:11 +02002441 FOR_ALL_WINDOWS(wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002443 if (curtab->tp_diffbuf[i] == wp->w_buffer && i != skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 foldUpdate(wp, dp->df_lnum[i],
2445 dp->df_lnum[i] + dp->df_count[i]);
2446}
2447#endif
2448
2449/*
2450 * Return TRUE if buffer "buf" is in diff-mode.
2451 */
2452 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002453diff_mode_buf(buf_T *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002455 tabpage_T *tp;
2456
Bram Moolenaar29323592016-07-24 22:04:11 +02002457 FOR_ALL_TABPAGES(tp)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002458 if (diff_buf_idx_tp(buf, tp) != DB_COUNT)
2459 return TRUE;
2460 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461}
2462
2463/*
2464 * Move "count" times in direction "dir" to the next diff block.
2465 * Return FAIL if there isn't such a diff block.
2466 */
2467 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002468diff_move_to(int dir, long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469{
2470 int idx;
2471 linenr_T lnum = curwin->w_cursor.lnum;
2472 diff_T *dp;
2473
2474 idx = diff_buf_idx(curbuf);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002475 if (idx == DB_COUNT || curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 return FAIL;
2477
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002478 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 ex_diffupdate(NULL); /* update after a big change */
2480
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002481 if (curtab->tp_first_diff == NULL) /* no diffs today */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 return FAIL;
2483
2484 while (--count >= 0)
2485 {
2486 /* Check if already before first diff. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002487 if (dir == BACKWARD && lnum <= curtab->tp_first_diff->df_lnum[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 break;
2489
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002490 for (dp = curtab->tp_first_diff; ; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {
2492 if (dp == NULL)
2493 break;
2494 if ((dir == FORWARD && lnum < dp->df_lnum[idx])
2495 || (dir == BACKWARD
2496 && (dp->df_next == NULL
2497 || lnum <= dp->df_next->df_lnum[idx])))
2498 {
2499 lnum = dp->df_lnum[idx];
2500 break;
2501 }
2502 }
2503 }
2504
2505 /* don't end up past the end of the file */
2506 if (lnum > curbuf->b_ml.ml_line_count)
2507 lnum = curbuf->b_ml.ml_line_count;
2508
2509 /* When the cursor didn't move at all we fail. */
2510 if (lnum == curwin->w_cursor.lnum)
2511 return FAIL;
2512
2513 setpcmark();
2514 curwin->w_cursor.lnum = lnum;
2515 curwin->w_cursor.col = 0;
2516
2517 return OK;
2518}
2519
Bram Moolenaar860cae12010-06-05 23:22:07 +02002520 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002521diff_get_corresponding_line(
2522 buf_T *buf1,
2523 linenr_T lnum1,
2524 buf_T *buf2,
2525 linenr_T lnum3)
Bram Moolenaar860cae12010-06-05 23:22:07 +02002526{
2527 int idx1;
2528 int idx2;
2529 diff_T *dp;
2530 int baseline = 0;
2531 linenr_T lnum2;
2532
2533 idx1 = diff_buf_idx(buf1);
2534 idx2 = diff_buf_idx(buf2);
2535 if (idx1 == DB_COUNT || idx2 == DB_COUNT || curtab->tp_first_diff == NULL)
2536 return lnum1;
2537
2538 if (curtab->tp_diff_invalid)
2539 ex_diffupdate(NULL); /* update after a big change */
2540
2541 if (curtab->tp_first_diff == NULL) /* no diffs today */
2542 return lnum1;
2543
2544 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
2545 {
2546 if (dp->df_lnum[idx1] > lnum1)
2547 {
2548 lnum2 = lnum1 - baseline;
2549 /* don't end up past the end of the file */
2550 if (lnum2 > buf2->b_ml.ml_line_count)
2551 lnum2 = buf2->b_ml.ml_line_count;
2552
2553 return lnum2;
2554 }
2555 else if ((dp->df_lnum[idx1] + dp->df_count[idx1]) > lnum1)
2556 {
2557 /* Inside the diffblock */
2558 baseline = lnum1 - dp->df_lnum[idx1];
2559 if (baseline > dp->df_count[idx2])
2560 baseline = dp->df_count[idx2];
2561
2562 return dp->df_lnum[idx2] + baseline;
2563 }
2564 else if ( (dp->df_lnum[idx1] == lnum1)
2565 && (dp->df_count[idx1] == 0)
2566 && (dp->df_lnum[idx2] <= lnum3)
2567 && ((dp->df_lnum[idx2] + dp->df_count[idx2]) > lnum3))
2568 /*
2569 * Special case: if the cursor is just after a zero-count
2570 * block (i.e. all filler) and the target cursor is already
2571 * inside the corresponding block, leave the target cursor
2572 * unmoved. This makes repeated CTRL-W W operations work
2573 * as expected.
2574 */
2575 return lnum3;
2576 baseline = (dp->df_lnum[idx1] + dp->df_count[idx1])
2577 - (dp->df_lnum[idx2] + dp->df_count[idx2]);
2578 }
2579
2580 /* If we get here then the cursor is after the last diff */
2581 lnum2 = lnum1 - baseline;
2582 /* don't end up past the end of the file */
2583 if (lnum2 > buf2->b_ml.ml_line_count)
2584 lnum2 = buf2->b_ml.ml_line_count;
2585
2586 return lnum2;
2587}
Bram Moolenaar860cae12010-06-05 23:22:07 +02002588
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589#if defined(FEAT_FOLDING) || defined(PROTO)
2590/*
2591 * For line "lnum" in the current window find the equivalent lnum in window
2592 * "wp", compensating for inserted/deleted lines.
2593 */
2594 linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +01002595diff_lnum_win(linenr_T lnum, win_T *wp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596{
2597 diff_T *dp;
2598 int idx;
2599 int i;
2600 linenr_T n;
2601
2602 idx = diff_buf_idx(curbuf);
2603 if (idx == DB_COUNT) /* safety check */
2604 return (linenr_T)0;
2605
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002606 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 ex_diffupdate(NULL); /* update after a big change */
2608
2609 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002610 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
2612 break;
2613
2614 /* When after the last change, compute relative to the last line number. */
2615 if (dp == NULL)
2616 return wp->w_buffer->b_ml.ml_line_count
2617 - (curbuf->b_ml.ml_line_count - lnum);
2618
2619 /* Find index for "wp". */
2620 i = diff_buf_idx(wp->w_buffer);
2621 if (i == DB_COUNT) /* safety check */
2622 return (linenr_T)0;
2623
2624 n = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]);
2625 if (n > dp->df_lnum[i] + dp->df_count[i])
2626 n = dp->df_lnum[i] + dp->df_count[i];
2627 return n;
2628}
2629#endif
2630
2631#endif /* FEAT_DIFF */