blob: 7170835d4b7d767807685e651ae5057635d33481 [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/*
11 * diff.c: code for diff'ing two or three buffers.
12 */
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 */
24static int diff_flags = DIFF_FILLER;
25
26#define LBUFLEN 50 /* length of line in diff file */
27
28static int diff_a_works = MAYBE; /* TRUE when "diff -a" works, FALSE when it
29 doesn't work, MAYBE when not checked yet */
30#if defined(MSWIN) || defined(MSDOS)
31static int diff_bin_works = MAYBE; /* TRUE when "diff --binary" works, FALSE
32 when it doesn't work, MAYBE when not
33 checked yet */
34#endif
35
36static int diff_buf_idx __ARGS((buf_T *buf));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000037static int diff_buf_idx_tp __ARGS((buf_T *buf, tabpage_T *tp));
38static void diff_mark_adjust_tp __ARGS((tabpage_T *tp, int idx, linenr_T line1, linenr_T line2, long amount, long amount_after));
39static void diff_check_unchanged __ARGS((tabpage_T *tp, diff_T *dp));
40static int diff_check_sanity __ARGS((tabpage_T *tp, diff_T *dp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000041static void diff_redraw __ARGS((int dofold));
42static int diff_write __ARGS((buf_T *buf, char_u *fname));
43static void diff_file __ARGS((char_u *tmp_orig, char_u *tmp_new, char_u *tmp_diff));
Bram Moolenaar071d4272004-06-13 20:20:40 +000044static int diff_equal_entry __ARGS((diff_T *dp, int idx1, int idx2));
45static int diff_cmp __ARGS((char_u *s1, char_u *s2));
46#ifdef FEAT_FOLDING
47static void diff_fold_update __ARGS((diff_T *dp, int skip_idx));
48#endif
49static void diff_read __ARGS((int idx_orig, int idx_new, char_u *fname));
50static void diff_copy_entry __ARGS((diff_T *dprev, diff_T *dp, int idx_orig, int idx_new));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000051static diff_T *diff_alloc_new __ARGS((tabpage_T *tp, diff_T *dprev, diff_T *dp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000052
53#ifndef USE_CR
54# define tag_fgets vim_fgets
55#endif
56
57/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000058 * Called when deleting or unloading a buffer: No longer make a diff with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000059 */
60 void
61diff_buf_delete(buf)
62 buf_T *buf;
63{
64 int i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000065 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000066
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000067 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000068 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000069 i = diff_buf_idx_tp(buf, tp);
70 if (i != DB_COUNT)
71 {
72 tp->tp_diffbuf[i] = NULL;
73 tp->tp_diff_invalid = TRUE;
74 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000075 }
76}
77
78/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000079 * Check if the current buffer should be added to or removed from the list of
80 * diff buffers.
81 */
82 void
83diff_buf_adjust(win)
84 win_T *win;
85{
86 win_T *wp;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000087 int i;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000088
89 if (!win->w_p_diff)
90 {
91 /* When there is no window showing a diff for this buffer, remove
92 * it from the diffs. */
93 for (wp = firstwin; wp != NULL; wp = wp->w_next)
94 if (wp->w_buffer == win->w_buffer && wp->w_p_diff)
95 break;
96 if (wp == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000097 {
98 i = diff_buf_idx(win->w_buffer);
99 if (i != DB_COUNT)
100 {
101 curtab->tp_diffbuf[i] = NULL;
102 curtab->tp_diff_invalid = TRUE;
103 }
104 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000105 }
106 else
107 diff_buf_add(win->w_buffer);
108}
109
110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000111 * Add a buffer to make diffs for.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000112 * Call this when a new buffer is being edited in the current window where
113 * 'diff' is set.
114 * Marks the current buffer as being part of the diff and requireing updating.
115 * This must be done before any autocmd, because a command may use info
116 * about the screen contents.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000117 */
118 void
119diff_buf_add(buf)
120 buf_T *buf;
121{
122 int i;
123
124 if (diff_buf_idx(buf) != DB_COUNT)
125 return; /* It's already there. */
126
127 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000128 if (curtab->tp_diffbuf[i] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000130 curtab->tp_diffbuf[i] = buf;
131 curtab->tp_diff_invalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132 return;
133 }
134
135 EMSGN(_("E96: Can not diff more than %ld buffers"), DB_COUNT);
136}
137
138/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000139 * Find buffer "buf" in the list of diff buffers for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 * Return its index or DB_COUNT if not found.
141 */
142 static int
143diff_buf_idx(buf)
144 buf_T *buf;
145{
146 int idx;
147
148 for (idx = 0; idx < DB_COUNT; ++idx)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000149 if (curtab->tp_diffbuf[idx] == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150 break;
151 return idx;
152}
153
154/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000155 * Find buffer "buf" in the list of diff buffers for tab page "tp".
156 * Return its index or DB_COUNT if not found.
157 */
158 static int
159diff_buf_idx_tp(buf, tp)
160 buf_T *buf;
161 tabpage_T *tp;
162{
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 Moolenaar49d7bf12006-02-17 21:45:41 +0000176diff_invalidate(buf)
177 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000178{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000179 tabpage_T *tp;
180 int i;
181
182 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000183 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000184 i = diff_buf_idx_tp(buf, tp);
185 if (i != DB_COUNT)
186 {
187 tp->tp_diff_invalid = TRUE;
188 if (tp == curtab)
189 diff_redraw(TRUE);
190 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191 }
192}
193
194/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000195 * Called by mark_adjust(): update line numbers in "curbuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196 */
197 void
198diff_mark_adjust(line1, line2, amount, amount_after)
199 linenr_T line1;
200 linenr_T line2;
201 long amount;
202 long amount_after;
203{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000204 int idx;
205 tabpage_T *tp;
206
207 /* Handle all tab pages that use the current buffer in a diff. */
208 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
209 {
210 idx = diff_buf_idx_tp(curbuf, tp);
211 if (idx != DB_COUNT)
212 diff_mark_adjust_tp(tp, idx, line1, line2, amount, amount_after);
213 }
214}
215
216/*
217 * Update line numbers in tab page "tp" for "curbuf" with index "idx".
218 * This attempts to update the changes as much as possible:
219 * When inserting/deleting lines outside of existing change blocks, create a
220 * new change block and update the line numbers in following blocks.
221 * When inserting/deleting lines in existing change blocks, update them.
222 */
223 static void
224diff_mark_adjust_tp(tp, idx, line1, line2, amount, amount_after)
225 tabpage_T *tp;
226 int idx;
227 linenr_T line1;
228 linenr_T line2;
229 long amount;
230 long amount_after;
231{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232 diff_T *dp;
233 diff_T *dprev;
234 diff_T *dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235 int i;
236 int inserted, deleted;
237 int n, off;
238 linenr_T last;
239 linenr_T lnum_deleted = line1; /* lnum of remaining deletion */
240 int check_unchanged;
241
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242 if (line2 == MAXLNUM)
243 {
244 /* mark_adjust(99, MAXLNUM, 9, 0): insert lines */
245 inserted = amount;
246 deleted = 0;
247 }
248 else if (amount_after > 0)
249 {
250 /* mark_adjust(99, 98, MAXLNUM, 9): a change that inserts lines*/
251 inserted = amount_after;
252 deleted = 0;
253 }
254 else
255 {
256 /* mark_adjust(98, 99, MAXLNUM, -2): delete lines */
257 inserted = 0;
258 deleted = -amount_after;
259 }
260
261 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000262 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263 for (;;)
264 {
265 /* If the change is after the previous diff block and before the next
266 * diff block, thus not touching an existing change, create a new diff
267 * block. Don't do this when ex_diffgetput() is busy. */
268 if ((dp == NULL || dp->df_lnum[idx] - 1 > line2
269 || (line2 == MAXLNUM && dp->df_lnum[idx] > line1))
270 && (dprev == NULL
271 || dprev->df_lnum[idx] + dprev->df_count[idx] < line1)
272 && !diff_busy)
273 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000274 dnext = diff_alloc_new(tp, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275 if (dnext == NULL)
276 return;
277
278 dnext->df_lnum[idx] = line1;
279 dnext->df_count[idx] = inserted;
280 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000281 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282 {
283 if (dprev == NULL)
284 dnext->df_lnum[i] = line1;
285 else
286 dnext->df_lnum[i] = line1
287 + (dprev->df_lnum[i] + dprev->df_count[i])
288 - (dprev->df_lnum[idx] + dprev->df_count[idx]);
289 dnext->df_count[i] = deleted;
290 }
291 }
292
293 /* if at end of the list, quit */
294 if (dp == NULL)
295 break;
296
297 /*
298 * Check for these situations:
299 * 1 2 3
300 * 1 2 3
301 * line1 2 3 4 5
302 * 2 3 4 5
303 * 2 3 4 5
304 * line2 2 3 4 5
305 * 3 5 6
306 * 3 5 6
307 */
308 /* compute last line of this change */
309 last = dp->df_lnum[idx] + dp->df_count[idx] - 1;
310
311 /* 1. change completely above line1: nothing to do */
312 if (last >= line1 - 1)
313 {
314 /* 6. change below line2: only adjust for amount_after; also when
315 * "deleted" became zero when deleted all lines between two diffs */
316 if (dp->df_lnum[idx] - (deleted + inserted != 0) > line2)
317 {
318 if (amount_after == 0)
319 break; /* nothing left to change */
320 dp->df_lnum[idx] += amount_after;
321 }
322 else
323 {
324 check_unchanged = FALSE;
325
326 /* 2. 3. 4. 5.: inserted/deleted lines touching this diff. */
327 if (deleted > 0)
328 {
329 if (dp->df_lnum[idx] >= line1)
330 {
331 off = dp->df_lnum[idx] - lnum_deleted;
332 if (last <= line2)
333 {
334 /* 4. delete all lines of diff */
335 if (dp->df_next != NULL
336 && dp->df_next->df_lnum[idx] - 1 <= line2)
337 {
338 /* delete continues in next diff, only do
339 * lines until that one */
340 n = dp->df_next->df_lnum[idx] - lnum_deleted;
341 deleted -= n;
342 n -= dp->df_count[idx];
343 lnum_deleted = dp->df_next->df_lnum[idx];
344 }
345 else
346 n = deleted - dp->df_count[idx];
347 dp->df_count[idx] = 0;
348 }
349 else
350 {
351 /* 5. delete lines at or just before top of diff */
352 n = off;
353 dp->df_count[idx] -= line2 - dp->df_lnum[idx] + 1;
354 check_unchanged = TRUE;
355 }
356 dp->df_lnum[idx] = line1;
357 }
358 else
359 {
360 off = 0;
361 if (last < line2)
362 {
363 /* 2. delete at end of of diff */
364 dp->df_count[idx] -= last - lnum_deleted + 1;
365 if (dp->df_next != NULL
366 && dp->df_next->df_lnum[idx] - 1 <= line2)
367 {
368 /* delete continues in next diff, only do
369 * lines until that one */
370 n = dp->df_next->df_lnum[idx] - 1 - last;
371 deleted -= dp->df_next->df_lnum[idx]
372 - lnum_deleted;
373 lnum_deleted = dp->df_next->df_lnum[idx];
374 }
375 else
376 n = line2 - last;
377 check_unchanged = TRUE;
378 }
379 else
380 {
381 /* 3. delete lines inside the diff */
382 n = 0;
383 dp->df_count[idx] -= deleted;
384 }
385 }
386
387 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000388 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 {
390 dp->df_lnum[i] -= off;
391 dp->df_count[i] += n;
392 }
393 }
394 else
395 {
396 if (dp->df_lnum[idx] <= line1)
397 {
398 /* inserted lines somewhere in this diff */
399 dp->df_count[idx] += inserted;
400 check_unchanged = TRUE;
401 }
402 else
403 /* inserted lines somewhere above this diff */
404 dp->df_lnum[idx] += inserted;
405 }
406
407 if (check_unchanged)
408 /* Check if inserted lines are equal, may reduce the
409 * size of the diff. TODO: also check for equal lines
410 * in the middle and perhaps split the block. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000411 diff_check_unchanged(tp, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000412 }
413 }
414
415 /* check if this block touches the previous one, may merge them. */
416 if (dprev != NULL && dprev->df_lnum[idx] + dprev->df_count[idx]
417 == dp->df_lnum[idx])
418 {
419 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000420 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421 dprev->df_count[i] += dp->df_count[i];
422 dprev->df_next = dp->df_next;
423 vim_free(dp);
424 dp = dprev->df_next;
425 }
426 else
427 {
428 /* Advance to next entry. */
429 dprev = dp;
430 dp = dp->df_next;
431 }
432 }
433
434 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000435 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 while (dp != NULL)
437 {
438 /* All counts are zero, remove this entry. */
439 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000440 if (tp->tp_diffbuf[i] != NULL && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000441 break;
442 if (i == DB_COUNT)
443 {
444 dnext = dp->df_next;
445 vim_free(dp);
446 dp = dnext;
447 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000448 tp->tp_first_diff = dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000449 else
450 dprev->df_next = dnext;
451 }
452 else
453 {
454 /* Advance to next entry. */
455 dprev = dp;
456 dp = dp->df_next;
457 }
458
459 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000460
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000461 if (tp == curtab)
462 {
463 diff_redraw(TRUE);
464
465 /* Need to recompute the scroll binding, may remove or add filler
466 * lines (e.g., when adding lines above w_topline). But it's slow when
467 * making many changes, postpone until redrawing. */
468 diff_need_scrollbind = TRUE;
469 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470}
471
472/*
473 * Allocate a new diff block and link it between "dprev" and "dp".
474 */
475 static diff_T *
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000476diff_alloc_new(tp, dprev, dp)
477 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000478 diff_T *dprev;
479 diff_T *dp;
480{
481 diff_T *dnew;
482
483 dnew = (diff_T *)alloc((unsigned)sizeof(diff_T));
484 if (dnew != NULL)
485 {
486 dnew->df_next = dp;
487 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000488 tp->tp_first_diff = dnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000489 else
490 dprev->df_next = dnew;
491 }
492 return dnew;
493}
494
495/*
496 * Check if the diff block "dp" can be made smaller for lines at the start and
497 * end that are equal. Called after inserting lines.
498 * This may result in a change where all buffers have zero lines, the caller
499 * must take care of removing it.
500 */
501 static void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000502diff_check_unchanged(tp, dp)
503 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000504 diff_T *dp;
505{
506 int i_org;
507 int i_new;
508 int off_org, off_new;
509 char_u *line_org;
510 int dir = FORWARD;
511
512 /* Find the first buffers, use it as the original, compare the other
513 * buffer lines against this one. */
514 for (i_org = 0; i_org < DB_COUNT; ++i_org)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000515 if (tp->tp_diffbuf[i_org] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 break;
517 if (i_org == DB_COUNT) /* safety check */
518 return;
519
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000520 if (diff_check_sanity(tp, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000521 return;
522
523 /* First check lines at the top, then at the bottom. */
524 off_org = 0;
525 off_new = 0;
526 for (;;)
527 {
528 /* Repeat until a line is found which is different or the number of
529 * lines has become zero. */
530 while (dp->df_count[i_org] > 0)
531 {
532 /* Copy the line, the next ml_get() will invalidate it. */
533 if (dir == BACKWARD)
534 off_org = dp->df_count[i_org] - 1;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000535 line_org = vim_strsave(ml_get_buf(tp->tp_diffbuf[i_org],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000536 dp->df_lnum[i_org] + off_org, FALSE));
537 if (line_org == NULL)
538 return;
539 for (i_new = i_org + 1; i_new < DB_COUNT; ++i_new)
540 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000541 if (tp->tp_diffbuf[i_new] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000542 continue;
543 if (dir == BACKWARD)
544 off_new = dp->df_count[i_new] - 1;
545 /* if other buffer doesn't have this line, it was inserted */
546 if (off_new < 0 || off_new >= dp->df_count[i_new])
547 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000548 if (diff_cmp(line_org, ml_get_buf(tp->tp_diffbuf[i_new],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 dp->df_lnum[i_new] + off_new, FALSE)) != 0)
550 break;
551 }
552 vim_free(line_org);
553
554 /* Stop when a line isn't equal in all diff buffers. */
555 if (i_new != DB_COUNT)
556 break;
557
558 /* Line matched in all buffers, remove it from the diff. */
559 for (i_new = i_org; i_new < DB_COUNT; ++i_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000560 if (tp->tp_diffbuf[i_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 {
562 if (dir == FORWARD)
563 ++dp->df_lnum[i_new];
564 --dp->df_count[i_new];
565 }
566 }
567 if (dir == BACKWARD)
568 break;
569 dir = BACKWARD;
570 }
571}
572
573/*
574 * Check if a diff block doesn't contain invalid line numbers.
575 * This can happen when the diff program returns invalid results.
576 */
577 static int
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000578diff_check_sanity(tp, dp)
579 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 diff_T *dp;
581{
582 int i;
583
584 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000585 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 if (dp->df_lnum[i] + dp->df_count[i] - 1
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000587 > tp->tp_diffbuf[i]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 return FAIL;
589 return OK;
590}
591
592/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000593 * Mark all diff buffers in the current tab page for redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000594 */
595 static void
596diff_redraw(dofold)
597 int dofold; /* also recompute the folds */
598{
599 win_T *wp;
600 int n;
601
602 for (wp = firstwin; wp != NULL; wp = wp->w_next)
603 if (wp->w_p_diff)
604 {
605 redraw_win_later(wp, NOT_VALID);
606#ifdef FEAT_FOLDING
607 if (dofold && foldmethodIsDiff(wp))
608 foldUpdateAll(wp);
609#endif
610 /* A change may have made filler lines invalid, need to take care
611 * of that for other windows. */
612 if (wp != curwin && wp->w_topfill > 0)
613 {
614 n = diff_check(wp, wp->w_topline);
615 if (wp->w_topfill > n)
616 wp->w_topfill = (n < 0 ? 0 : n);
617 }
618 }
619}
620
621/*
622 * Write buffer "buf" to file "name".
623 * Always use 'fileformat' set to "unix".
624 * Return FAIL for failure
625 */
626 static int
627diff_write(buf, fname)
628 buf_T *buf;
629 char_u *fname;
630{
631 int r;
632 char_u *save_ff;
633
634 save_ff = buf->b_p_ff;
635 buf->b_p_ff = vim_strsave((char_u *)FF_UNIX);
636 r = buf_write(buf, fname, NULL, (linenr_T)1, buf->b_ml.ml_line_count,
637 NULL, FALSE, FALSE, FALSE, TRUE);
638 free_string_option(buf->b_p_ff);
639 buf->b_p_ff = save_ff;
640 return r;
641}
642
643/*
644 * Completely update the diffs for the buffers involved.
645 * This uses the ordinary "diff" command.
646 * The buffers are written to a file, also for unmodified buffers (the file
647 * could have been produced by autocommands, e.g. the netrw plugin).
648 */
649/*ARGSUSED*/
650 void
651ex_diffupdate(eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000652 exarg_T *eap; /* can be NULL, it's not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000653{
654 buf_T *buf;
655 int idx_orig;
656 int idx_new;
657 char_u *tmp_orig;
658 char_u *tmp_new;
659 char_u *tmp_diff;
660 FILE *fd;
661 int ok;
662
663 /* Delete all diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000664 diff_clear(curtab);
665 curtab->tp_diff_invalid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666
667 /* Use the first buffer as the original text. */
668 for (idx_orig = 0; idx_orig < DB_COUNT; ++idx_orig)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000669 if (curtab->tp_diffbuf[idx_orig] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000670 break;
671 if (idx_orig == DB_COUNT)
672 return;
673
674 /* Only need to do something when there is another buffer. */
675 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000676 if (curtab->tp_diffbuf[idx_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000677 break;
678 if (idx_new == DB_COUNT)
679 return;
680
681 /* We need three temp file names. */
682 tmp_orig = vim_tempname('o');
683 tmp_new = vim_tempname('n');
684 tmp_diff = vim_tempname('d');
685 if (tmp_orig == NULL || tmp_new == NULL || tmp_diff == NULL)
686 goto theend;
687
688 /*
689 * Do a quick test if "diff" really works. Otherwise it looks like there
690 * are no differences. Can't use the return value, it's non-zero when
691 * there are differences.
692 * May try twice, first with "-a" and then without.
693 */
694 for (;;)
695 {
696 ok = FALSE;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000697 fd = mch_fopen((char *)tmp_orig, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 if (fd != NULL)
699 {
700 fwrite("line1\n", (size_t)6, (size_t)1, fd);
701 fclose(fd);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000702 fd = mch_fopen((char *)tmp_new, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 if (fd != NULL)
704 {
705 fwrite("line2\n", (size_t)6, (size_t)1, fd);
706 fclose(fd);
707 diff_file(tmp_orig, tmp_new, tmp_diff);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000708 fd = mch_fopen((char *)tmp_diff, "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 if (fd != NULL)
710 {
711 char_u linebuf[LBUFLEN];
712
713 for (;;)
714 {
715 /* There must be a line that contains "1c1". */
716 if (tag_fgets(linebuf, LBUFLEN, fd))
717 break;
718 if (STRNCMP(linebuf, "1c1", 3) == 0)
719 ok = TRUE;
720 }
721 fclose(fd);
722 }
723 mch_remove(tmp_diff);
724 mch_remove(tmp_new);
725 }
726 mch_remove(tmp_orig);
727 }
728
729#ifdef FEAT_EVAL
730 /* When using 'diffexpr' break here. */
731 if (*p_dex != NUL)
732 break;
733#endif
734
735#if defined(MSWIN) || defined(MSDOS)
736 /* If the "-a" argument works, also check if "--binary" works. */
737 if (ok && diff_a_works == MAYBE && diff_bin_works == MAYBE)
738 {
739 diff_a_works = TRUE;
740 diff_bin_works = TRUE;
741 continue;
742 }
743 if (!ok && diff_a_works == TRUE && diff_bin_works == TRUE)
744 {
745 /* Tried --binary, but it failed. "-a" works though. */
746 diff_bin_works = FALSE;
747 ok = TRUE;
748 }
749#endif
750
751 /* If we checked if "-a" works already, break here. */
752 if (diff_a_works != MAYBE)
753 break;
754 diff_a_works = ok;
755
756 /* If "-a" works break here, otherwise retry without "-a". */
757 if (ok)
758 break;
759 }
760 if (!ok)
761 {
762 EMSG(_("E97: Cannot create diffs"));
763 diff_a_works = MAYBE;
764#if defined(MSWIN) || defined(MSDOS)
765 diff_bin_works = MAYBE;
766#endif
767 goto theend;
768 }
769
770 /* Write the first buffer to a tempfile. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000771 buf = curtab->tp_diffbuf[idx_orig];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (diff_write(buf, tmp_orig) == FAIL)
773 goto theend;
774
775 /* Make a difference between the first buffer and every other. */
776 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
777 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000778 buf = curtab->tp_diffbuf[idx_new];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000779 if (buf == NULL)
780 continue;
781 if (diff_write(buf, tmp_new) == FAIL)
782 continue;
783 diff_file(tmp_orig, tmp_new, tmp_diff);
784
785 /* Read the diff output and add each entry to the diff list. */
786 diff_read(idx_orig, idx_new, tmp_diff);
787 mch_remove(tmp_diff);
788 mch_remove(tmp_new);
789 }
790 mch_remove(tmp_orig);
791
792 diff_redraw(TRUE);
793
794theend:
795 vim_free(tmp_orig);
796 vim_free(tmp_new);
797 vim_free(tmp_diff);
798}
799
800/*
801 * Make a diff between files "tmp_orig" and "tmp_new", results in "tmp_diff".
802 */
803 static void
804diff_file(tmp_orig, tmp_new, tmp_diff)
805 char_u *tmp_orig;
806 char_u *tmp_new;
807 char_u *tmp_diff;
808{
809 char_u *cmd;
810
811#ifdef FEAT_EVAL
812 if (*p_dex != NUL)
813 /* Use 'diffexpr' to generate the diff file. */
814 eval_diff(tmp_orig, tmp_new, tmp_diff);
815 else
816#endif
817 {
818 cmd = alloc((unsigned)(STRLEN(tmp_orig) + STRLEN(tmp_new)
819 + STRLEN(tmp_diff) + STRLEN(p_srr) + 27));
820 if (cmd != NULL)
821 {
Bram Moolenaar95fb60a2005-03-08 22:29:20 +0000822 /* We don't want $DIFF_OPTIONS to get in the way. */
823 if (getenv("DIFF_OPTIONS"))
824 vim_setenv((char_u *)"DIFF_OPTIONS", (char_u *)"");
825
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 /* Build the diff command and execute it. Always use -a, binary
827 * differences are of no use. Ignore errors, diff returns
828 * non-zero when differences have been found. */
829 sprintf((char *)cmd, "diff %s%s%s%s%s %s",
830 diff_a_works == FALSE ? "" : "-a ",
831#if defined(MSWIN) || defined(MSDOS)
832 diff_bin_works == TRUE ? "--binary " : "",
833#else
834 "",
835#endif
836 (diff_flags & DIFF_IWHITE) ? "-b " : "",
837 (diff_flags & DIFF_ICASE) ? "-i " : "",
838 tmp_orig, tmp_new);
839 append_redir(cmd, p_srr, tmp_diff);
840 (void)call_shell(cmd, SHELL_FILTER|SHELL_SILENT|SHELL_DOOUT);
841 vim_free(cmd);
842 }
843 }
844}
845
846/*
847 * Create a new version of a file from the current buffer and a diff file.
848 * The buffer is written to a file, also for unmodified buffers (the file
849 * could have been produced by autocommands, e.g. the netrw plugin).
850 */
851 void
852ex_diffpatch(eap)
853 exarg_T *eap;
854{
855 char_u *tmp_orig; /* name of original temp file */
856 char_u *tmp_new; /* name of patched temp file */
857 char_u *buf = NULL;
858 win_T *old_curwin = curwin;
859 char_u *newname = NULL; /* name of patched file buffer */
860#ifdef UNIX
861 char_u dirbuf[MAXPATHL];
862 char_u *fullname = NULL;
863#endif
864#ifdef FEAT_BROWSE
865 char_u *browseFile = NULL;
866 int browse_flag = cmdmod.browse;
867#endif
868
869#ifdef FEAT_BROWSE
870 if (cmdmod.browse)
871 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000872 browseFile = do_browse(0, (char_u *)_("Patch file"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000873 eap->arg, NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
874 if (browseFile == NULL)
875 return; /* operation cancelled */
876 eap->arg = browseFile;
877 cmdmod.browse = FALSE; /* don't let do_ecmd() browse again */
878 }
879#endif
880
881 /* We need two temp file names. */
882 tmp_orig = vim_tempname('o');
883 tmp_new = vim_tempname('n');
884 if (tmp_orig == NULL || tmp_new == NULL)
885 goto theend;
886
887 /* Write the current buffer to "tmp_orig". */
888 if (buf_write(curbuf, tmp_orig, NULL,
889 (linenr_T)1, curbuf->b_ml.ml_line_count,
890 NULL, FALSE, FALSE, FALSE, TRUE) == FAIL)
891 goto theend;
892
893#ifdef UNIX
894 /* Get the absolute path of the patchfile, changing directory below. */
895 fullname = FullName_save(eap->arg, FALSE);
896#endif
897 buf = alloc((unsigned)(STRLEN(tmp_orig) + (
898# ifdef UNIX
899 fullname != NULL ? STRLEN(fullname) :
900# endif
901 STRLEN(eap->arg)) + STRLEN(tmp_new) + 16));
902 if (buf == NULL)
903 goto theend;
904
905#ifdef UNIX
906 /* Temporaraly chdir to /tmp, to avoid patching files in the current
907 * directory when the patch file contains more than one patch. When we
908 * have our own temp dir use that instead, it will be cleaned up when we
909 * exit (any .rej files created). Don't change directory if we can't
910 * return to the current. */
911 if (mch_dirname(dirbuf, MAXPATHL) != OK || mch_chdir((char *)dirbuf) != 0)
912 dirbuf[0] = NUL;
913 else
914 {
915# ifdef TEMPDIRNAMES
916 if (vim_tempdir != NULL)
917 mch_chdir((char *)vim_tempdir);
918 else
919# endif
920 mch_chdir("/tmp");
921 shorten_fnames(TRUE);
922 }
923#endif
924
925#ifdef FEAT_EVAL
926 if (*p_pex != NUL)
927 /* Use 'patchexpr' to generate the new file. */
928 eval_patch(tmp_orig,
929# ifdef UNIX
930 fullname != NULL ? fullname :
931# endif
932 eap->arg, tmp_new);
933 else
934#endif
935 {
936 /* Build the patch command and execute it. Ignore errors. Switch to
937 * cooked mode to allow the user to respond to prompts. */
938 sprintf((char *)buf, "patch -o %s %s < \"%s\"", tmp_new, tmp_orig,
939# ifdef UNIX
940 fullname != NULL ? fullname :
941# endif
942 eap->arg);
943 (void)call_shell(buf, SHELL_FILTER | SHELL_COOKED);
944 }
945
946#ifdef UNIX
947 if (dirbuf[0] != NUL)
948 {
949 if (mch_chdir((char *)dirbuf) != 0)
950 EMSG(_(e_prev_dir));
951 shorten_fnames(TRUE);
952 }
953#endif
954
955 /* patch probably has written over the screen */
956 redraw_later(CLEAR);
957
958 /* Delete any .orig or .rej file created. */
959 STRCPY(buf, tmp_new);
960 STRCAT(buf, ".orig");
961 mch_remove(buf);
962 STRCPY(buf, tmp_new);
963 STRCAT(buf, ".rej");
964 mch_remove(buf);
965
966 if (curbuf->b_fname != NULL)
967 {
968 newname = vim_strnsave(curbuf->b_fname,
969 (int)(STRLEN(curbuf->b_fname) + 4));
970 if (newname != NULL)
971 STRCAT(newname, ".new");
972 }
973
974#ifdef FEAT_GUI
975 need_mouse_correct = TRUE;
976#endif
977 if (win_split(0, 0) != FAIL)
978 {
979 /* Pretend it was a ":split fname" command */
980 eap->cmdidx = CMD_split;
981 eap->arg = tmp_new;
982 do_exedit(eap, old_curwin);
983
984 if (curwin != old_curwin) /* split must have worked */
985 {
986 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
987 diff_win_options(curwin, TRUE);
988 diff_win_options(old_curwin, TRUE);
989
990 if (newname != NULL)
991 {
992 /* do a ":file filename.new" on the patched buffer */
993 eap->arg = newname;
994 ex_file(eap);
995
996#ifdef FEAT_AUTOCMD
997 /* Do filetype detection with the new name. */
998 do_cmdline_cmd((char_u *)":doau filetypedetect BufRead");
999#endif
1000 }
1001 }
1002 }
1003
1004theend:
1005 if (tmp_orig != NULL)
1006 mch_remove(tmp_orig);
1007 vim_free(tmp_orig);
1008 if (tmp_new != NULL)
1009 mch_remove(tmp_new);
1010 vim_free(tmp_new);
1011 vim_free(newname);
1012 vim_free(buf);
1013#ifdef UNIX
1014 vim_free(fullname);
1015#endif
1016#ifdef FEAT_BROWSE
1017 vim_free(browseFile);
1018 cmdmod.browse = browse_flag;
1019#endif
1020}
1021
1022/*
1023 * Split the window and edit another file, setting options to show the diffs.
1024 */
1025 void
1026ex_diffsplit(eap)
1027 exarg_T *eap;
1028{
1029 win_T *old_curwin = curwin;
1030
1031#ifdef FEAT_GUI
1032 need_mouse_correct = TRUE;
1033#endif
1034 if (win_split(0, 0) != FAIL)
1035 {
1036 /* Pretend it was a ":split fname" command */
1037 eap->cmdidx = CMD_split;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001038 curwin->w_p_diff = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 do_exedit(eap, old_curwin);
1040
1041 if (curwin != old_curwin) /* split must have worked */
1042 {
1043 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1044 diff_win_options(curwin, TRUE);
1045 diff_win_options(old_curwin, TRUE);
1046 }
1047 }
1048}
1049
1050/*
1051 * Set options to show difs for the current window.
1052 */
1053/*ARGSUSED*/
1054 void
1055ex_diffthis(eap)
1056 exarg_T *eap;
1057{
1058 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1059 diff_win_options(curwin, TRUE);
1060}
1061
1062/*
1063 * Set options in window "wp" for diff mode.
1064 */
1065 void
1066diff_win_options(wp, addbuf)
1067 win_T *wp;
1068 int addbuf; /* Add buffer to diff. */
1069{
1070 wp->w_p_diff = TRUE;
1071 wp->w_p_scb = TRUE;
1072 wp->w_p_wrap = FALSE;
1073# ifdef FEAT_FOLDING
1074 {
1075 win_T *old_curwin = curwin;
1076
1077 curwin = wp;
1078 curbuf = curwin->w_buffer;
1079 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"diff",
1080 OPT_LOCAL|OPT_FREE);
1081 curwin = old_curwin;
1082 curbuf = curwin->w_buffer;
1083 wp->w_p_fdc = 2;
1084 wp->w_p_fen = TRUE;
1085 wp->w_p_fdl = 0;
1086 foldUpdateAll(wp);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001087 /* make sure topline is not halfway a fold */
1088 changed_window_setting_win(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 }
1090# endif
1091#ifdef FEAT_SCROLLBIND
1092 if (vim_strchr(p_sbo, 'h') == NULL)
1093 do_cmdline_cmd((char_u *)"set sbo+=hor");
1094#endif
1095
1096 if (addbuf)
1097 diff_buf_add(wp->w_buffer);
1098 redraw_win_later(wp, NOT_VALID);
1099}
1100
1101/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001102 * Set options not to show diffs. For the current window or all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001103 * Only in the current tab page.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001104 */
1105 void
1106ex_diffoff(eap)
1107 exarg_T *eap;
1108{
1109 win_T *wp;
1110 win_T *old_curwin = curwin;
1111#ifdef FEAT_SCROLLBIND
1112 int diffwin = FALSE;
1113#endif
1114
1115 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1116 {
1117 if (wp == curwin || eap->forceit)
1118 {
1119 /* Set 'diff', 'scrollbind' off and 'wrap' on. */
1120 wp->w_p_diff = FALSE;
1121 wp->w_p_scb = FALSE;
1122 wp->w_p_wrap = TRUE;
1123#ifdef FEAT_FOLDING
1124 curwin = wp;
1125 curbuf = curwin->w_buffer;
1126 set_string_option_direct((char_u *)"fdm", -1,
1127 (char_u *)"manual", OPT_LOCAL|OPT_FREE);
1128 curwin = old_curwin;
1129 curbuf = curwin->w_buffer;
1130 wp->w_p_fdc = 0;
1131 wp->w_p_fen = FALSE;
1132 wp->w_p_fdl = 0;
1133 foldUpdateAll(wp);
1134 /* make sure topline is not halfway a fold */
1135 changed_window_setting_win(wp);
1136#endif
1137 diff_buf_adjust(wp);
1138 }
1139#ifdef FEAT_SCROLLBIND
1140 diffwin |= wp->w_p_diff;
1141#endif
1142 }
1143
1144#ifdef FEAT_SCROLLBIND
1145 /* Remove "hor" from from 'scrollopt' if there are no diff windows left. */
1146 if (!diffwin && vim_strchr(p_sbo, 'h') != NULL)
1147 do_cmdline_cmd((char_u *)"set sbo-=hor");
1148#endif
1149}
1150
1151/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 * Read the diff output and add each entry to the diff list.
1153 */
1154 static void
1155diff_read(idx_orig, idx_new, fname)
1156 int idx_orig; /* idx of original file */
1157 int idx_new; /* idx of new file */
1158 char_u *fname; /* name of diff output file */
1159{
1160 FILE *fd;
1161 diff_T *dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001162 diff_T *dp = curtab->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 diff_T *dn, *dpl;
1164 long f1, l1, f2, l2;
1165 char_u linebuf[LBUFLEN]; /* only need to hold the diff line */
1166 int difftype;
1167 char_u *p;
1168 long off;
1169 int i;
1170 linenr_T lnum_orig, lnum_new;
1171 long count_orig, count_new;
1172 int notset = TRUE; /* block "*dp" not set yet */
1173
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001174 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 if (fd == NULL)
1176 {
1177 EMSG(_("E98: Cannot read diff output"));
1178 return;
1179 }
1180
1181 for (;;)
1182 {
1183 if (tag_fgets(linebuf, LBUFLEN, fd))
1184 break; /* end of file */
1185 if (!isdigit(*linebuf))
1186 continue; /* not the start of a diff block */
1187
1188 /* This line must be one of three formats:
1189 * {first}[,{last}]c{first}[,{last}]
1190 * {first}a{first}[,{last}]
1191 * {first}[,{last}]d{first}
1192 */
1193 p = linebuf;
1194 f1 = getdigits(&p);
1195 if (*p == ',')
1196 {
1197 ++p;
1198 l1 = getdigits(&p);
1199 }
1200 else
1201 l1 = f1;
1202 if (*p != 'a' && *p != 'c' && *p != 'd')
1203 continue; /* invalid diff format */
1204 difftype = *p++;
1205 f2 = getdigits(&p);
1206 if (*p == ',')
1207 {
1208 ++p;
1209 l2 = getdigits(&p);
1210 }
1211 else
1212 l2 = f2;
1213 if (l1 < f1 || l2 < f2)
1214 continue; /* invalid line range */
1215
1216 if (difftype == 'a')
1217 {
1218 lnum_orig = f1 + 1;
1219 count_orig = 0;
1220 }
1221 else
1222 {
1223 lnum_orig = f1;
1224 count_orig = l1 - f1 + 1;
1225 }
1226 if (difftype == 'd')
1227 {
1228 lnum_new = f2 + 1;
1229 count_new = 0;
1230 }
1231 else
1232 {
1233 lnum_new = f2;
1234 count_new = l2 - f2 + 1;
1235 }
1236
1237 /* Go over blocks before the change, for which orig and new are equal.
1238 * Copy blocks from orig to new. */
1239 while (dp != NULL
1240 && lnum_orig > dp->df_lnum[idx_orig] + dp->df_count[idx_orig])
1241 {
1242 if (notset)
1243 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1244 dprev = dp;
1245 dp = dp->df_next;
1246 notset = TRUE;
1247 }
1248
1249 if (dp != NULL
1250 && lnum_orig <= dp->df_lnum[idx_orig] + dp->df_count[idx_orig]
1251 && lnum_orig + count_orig >= dp->df_lnum[idx_orig])
1252 {
1253 /* New block overlaps with existing block(s).
1254 * First find last block that overlaps. */
1255 for (dpl = dp; dpl->df_next != NULL; dpl = dpl->df_next)
1256 if (lnum_orig + count_orig < dpl->df_next->df_lnum[idx_orig])
1257 break;
1258
1259 /* If the newly found block starts before the old one, set the
1260 * start back a number of lines. */
1261 off = dp->df_lnum[idx_orig] - lnum_orig;
1262 if (off > 0)
1263 {
1264 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001265 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 dp->df_lnum[i] -= off;
1267 dp->df_lnum[idx_new] = lnum_new;
1268 dp->df_count[idx_new] = count_new;
1269 }
1270 else if (notset)
1271 {
1272 /* new block inside existing one, adjust new block */
1273 dp->df_lnum[idx_new] = lnum_new + off;
1274 dp->df_count[idx_new] = count_new - off;
1275 }
1276 else
1277 /* second overlap of new block with existing block */
1278 dp->df_count[idx_new] += count_new - count_orig;
1279
1280 /* Adjust the size of the block to include all the lines to the
1281 * end of the existing block or the new diff, whatever ends last. */
1282 off = (lnum_orig + count_orig)
1283 - (dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]);
1284 if (off < 0)
1285 {
1286 /* new change ends in existing block, adjust the end if not
1287 * done already */
1288 if (notset)
1289 dp->df_count[idx_new] += -off;
1290 off = 0;
1291 }
1292 for (i = idx_orig; i < idx_new + !notset; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001293 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 dp->df_count[i] = dpl->df_lnum[i] + dpl->df_count[i]
1295 - dp->df_lnum[i] + off;
1296
1297 /* Delete the diff blocks that have been merged into one. */
1298 dn = dp->df_next;
1299 dp->df_next = dpl->df_next;
1300 while (dn != dp->df_next)
1301 {
1302 dpl = dn->df_next;
1303 vim_free(dn);
1304 dn = dpl;
1305 }
1306 }
1307 else
1308 {
1309 /* Allocate a new diffblock. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001310 dp = diff_alloc_new(curtab, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 if (dp == NULL)
1312 return;
1313
1314 dp->df_lnum[idx_orig] = lnum_orig;
1315 dp->df_count[idx_orig] = count_orig;
1316 dp->df_lnum[idx_new] = lnum_new;
1317 dp->df_count[idx_new] = count_new;
1318
1319 /* Set values for other buffers, these must be equal to the
1320 * original buffer, otherwise there would have been a change
1321 * already. */
1322 for (i = idx_orig + 1; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001323 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 diff_copy_entry(dprev, dp, idx_orig, i);
1325 }
1326 notset = FALSE; /* "*dp" has been set */
1327 }
1328
1329 /* for remaining diff blocks orig and new are equal */
1330 while (dp != NULL)
1331 {
1332 if (notset)
1333 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1334 dprev = dp;
1335 dp = dp->df_next;
1336 notset = TRUE;
1337 }
1338
1339 fclose(fd);
1340}
1341
1342/*
1343 * Copy an entry at "dp" from "idx_orig" to "idx_new".
1344 */
1345 static void
1346diff_copy_entry(dprev, dp, idx_orig, idx_new)
1347 diff_T *dprev;
1348 diff_T *dp;
1349 int idx_orig;
1350 int idx_new;
1351{
1352 long off;
1353
1354 if (dprev == NULL)
1355 off = 0;
1356 else
1357 off = (dprev->df_lnum[idx_orig] + dprev->df_count[idx_orig])
1358 - (dprev->df_lnum[idx_new] + dprev->df_count[idx_new]);
1359 dp->df_lnum[idx_new] = dp->df_lnum[idx_orig] - off;
1360 dp->df_count[idx_new] = dp->df_count[idx_orig];
1361}
1362
1363/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001364 * Clear the list of diffblocks for tab page "tp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 */
Bram Moolenaarea408852005-06-25 22:49:46 +00001366 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001367diff_clear(tp)
1368 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369{
1370 diff_T *p, *next_p;
1371
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001372 for (p = tp->tp_first_diff; p != NULL; p = next_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 {
1374 next_p = p->df_next;
1375 vim_free(p);
1376 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001377 tp->tp_first_diff = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378}
1379
1380/*
1381 * Check diff status for line "lnum" in buffer "buf":
1382 * Returns 0 for nothing special
1383 * Returns -1 for a line that should be highlighted as changed.
1384 * Returns -2 for a line that should be highlighted as added/deleted.
1385 * Returns > 0 for inserting that many filler lines above it (never happens
1386 * when 'diffopt' doesn't contain "filler").
1387 * This should only be used for windows where 'diff' is set.
1388 */
1389 int
1390diff_check(wp, lnum)
1391 win_T *wp;
1392 linenr_T lnum;
1393{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001394 int idx; /* index in tp_diffbuf[] for this buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 diff_T *dp;
1396 int maxcount;
1397 int i;
1398 buf_T *buf = wp->w_buffer;
1399 int cmp;
1400
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001401 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 ex_diffupdate(NULL); /* update after a big change */
1403
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001404 if (curtab->tp_first_diff == NULL || !wp->w_p_diff) /* no diffs at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 return 0;
1406
1407 /* safety check: "lnum" must be a buffer line */
1408 if (lnum < 1 || lnum > buf->b_ml.ml_line_count + 1)
1409 return 0;
1410
1411 idx = diff_buf_idx(buf);
1412 if (idx == DB_COUNT)
1413 return 0; /* no diffs for buffer "buf" */
1414
1415#ifdef FEAT_FOLDING
1416 /* A closed fold never has filler lines. */
1417 if (hasFoldingWin(wp, lnum, NULL, NULL, TRUE, NULL))
1418 return 0;
1419#endif
1420
1421 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001422 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
1424 break;
1425 if (dp == NULL || lnum < dp->df_lnum[idx])
1426 return 0;
1427
1428 if (lnum < dp->df_lnum[idx] + dp->df_count[idx])
1429 {
1430 int zero = FALSE;
1431
1432 /* Changed or inserted line. If the other buffers have a count of
1433 * zero, the lines were inserted. If the other buffers have the same
1434 * count, check if the lines are identical. */
1435 cmp = FALSE;
1436 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001437 if (i != idx && curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 {
1439 if (dp->df_count[i] == 0)
1440 zero = TRUE;
1441 else
1442 {
1443 if (dp->df_count[i] != dp->df_count[idx])
1444 return -1; /* nr of lines changed. */
1445 cmp = TRUE;
1446 }
1447 }
1448 if (cmp)
1449 {
1450 /* Compare all lines. If they are equal the lines were inserted
1451 * in some buffers, deleted in others, but not changed. */
1452 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001453 if (i != idx && curtab->tp_diffbuf[i] != NULL && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 if (!diff_equal_entry(dp, idx, i))
1455 return -1;
1456 }
1457 /* If there is no buffer with zero lines then there is no difference
1458 * any longer. Happens when making a change (or undo) that removes
1459 * the difference. Can't remove the entry here, we might be halfway
1460 * updating the window. Just report the text as unchanged. Other
1461 * windows might still show the change though. */
1462 if (zero == FALSE)
1463 return 0;
1464 return -2;
1465 }
1466
1467 /* If 'diffopt' doesn't contain "filler", return 0. */
1468 if (!(diff_flags & DIFF_FILLER))
1469 return 0;
1470
1471 /* Insert filler lines above the line just below the change. Will return
1472 * 0 when this buf had the max count. */
1473 maxcount = 0;
1474 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001475 if (curtab->tp_diffbuf[i] != NULL && dp->df_count[i] > maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 maxcount = dp->df_count[i];
1477 return maxcount - dp->df_count[idx];
1478}
1479
1480/*
1481 * Compare two entries in diff "*dp" and return TRUE if they are equal.
1482 */
1483 static int
1484diff_equal_entry(dp, idx1, idx2)
1485 diff_T *dp;
1486 int idx1;
1487 int idx2;
1488{
1489 int i;
1490 char_u *line;
1491 int cmp;
1492
1493 if (dp->df_count[idx1] != dp->df_count[idx2])
1494 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001495 if (diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496 return FALSE;
1497 for (i = 0; i < dp->df_count[idx1]; ++i)
1498 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001499 line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 dp->df_lnum[idx1] + i, FALSE));
1501 if (line == NULL)
1502 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001503 cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 dp->df_lnum[idx2] + i, FALSE));
1505 vim_free(line);
1506 if (cmp != 0)
1507 return FALSE;
1508 }
1509 return TRUE;
1510}
1511
1512/*
1513 * Compare strings "s1" and "s2" according to 'diffopt'.
1514 * Return non-zero when they are different.
1515 */
1516 static int
1517diff_cmp(s1, s2)
1518 char_u *s1;
1519 char_u *s2;
1520{
1521 char_u *p1, *p2;
1522#ifdef FEAT_MBYTE
1523 int l;
1524#endif
1525
1526 if ((diff_flags & (DIFF_ICASE | DIFF_IWHITE)) == 0)
1527 return STRCMP(s1, s2);
1528 if ((diff_flags & DIFF_ICASE) && !(diff_flags & DIFF_IWHITE))
1529 return MB_STRICMP(s1, s2);
1530
1531 /* Ignore white space changes and possibly ignore case. */
1532 p1 = s1;
1533 p2 = s2;
1534 while (*p1 != NUL && *p2 != NUL)
1535 {
1536 if (vim_iswhite(*p1) && vim_iswhite(*p2))
1537 {
1538 p1 = skipwhite(p1);
1539 p2 = skipwhite(p2);
1540 }
1541 else
1542 {
1543#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001544 l = (*mb_ptr2len)(p1);
1545 if (l != (*mb_ptr2len)(p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001546 break;
1547 if (l > 1)
1548 {
1549 if (STRNCMP(p1, p2, l) != 0
1550 && (!enc_utf8
1551 || !(diff_flags & DIFF_ICASE)
1552 || utf_fold(utf_ptr2char(p1))
1553 != utf_fold(utf_ptr2char(p2))))
1554 break;
1555 p1 += l;
1556 p2 += l;
1557 }
1558 else
1559#endif
1560 {
1561 if (*p1 != *p2 && (!(diff_flags & DIFF_ICASE)
1562 || TOLOWER_LOC(*p1) != TOLOWER_LOC(*p2)))
1563 break;
1564 ++p1;
1565 ++p2;
1566 }
1567 }
1568 }
1569
1570 /* Ignore trailing white space. */
1571 p1 = skipwhite(p1);
1572 p2 = skipwhite(p2);
1573 if (*p1 != NUL || *p2 != NUL)
1574 return 1;
1575 return 0;
1576}
1577
1578/*
1579 * Return the number of filler lines above "lnum".
1580 */
1581 int
1582diff_check_fill(wp, lnum)
1583 win_T *wp;
1584 linenr_T lnum;
1585{
1586 int n;
1587
1588 /* be quick when there are no filler lines */
1589 if (!(diff_flags & DIFF_FILLER))
1590 return 0;
1591 n = diff_check(wp, lnum);
1592 if (n <= 0)
1593 return 0;
1594 return n;
1595}
1596
1597/*
1598 * Set the topline of "towin" to match the position in "fromwin", so that they
1599 * show the same diff'ed lines.
1600 */
1601 void
1602diff_set_topline(fromwin, towin)
1603 win_T *fromwin;
1604 win_T *towin;
1605{
1606 buf_T *buf = fromwin->w_buffer;
1607 linenr_T lnum = fromwin->w_topline;
1608 int idx;
1609 diff_T *dp;
1610 int i;
1611
1612 idx = diff_buf_idx(buf);
1613 if (idx == DB_COUNT)
1614 return; /* safety check */
1615
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001616 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001617 ex_diffupdate(NULL); /* update after a big change */
1618
1619 towin->w_topfill = 0;
1620
1621 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001622 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
1624 break;
1625 if (dp == NULL)
1626 {
1627 /* After last change, compute topline relative to end of file; no
1628 * filler lines. */
1629 towin->w_topline = towin->w_buffer->b_ml.ml_line_count
1630 - (buf->b_ml.ml_line_count - lnum);
1631 }
1632 else
1633 {
1634 /* Find index for "towin". */
1635 i = diff_buf_idx(towin->w_buffer);
1636 if (i == DB_COUNT)
1637 return; /* safety check */
1638
1639 towin->w_topline = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]);
1640 if (lnum >= dp->df_lnum[idx])
1641 {
1642 /* Inside a change: compute filler lines. */
1643 if (dp->df_count[i] == dp->df_count[idx])
1644 towin->w_topfill = fromwin->w_topfill;
1645 else if (dp->df_count[i] > dp->df_count[idx])
1646 {
1647 if (lnum == dp->df_lnum[idx] + dp->df_count[idx])
1648 towin->w_topline = dp->df_lnum[i] + dp->df_count[i]
1649 - fromwin->w_topfill;
1650 }
1651 else
1652 {
1653 if (towin->w_topline >= dp->df_lnum[i] + dp->df_count[i])
1654 {
1655 if (diff_flags & DIFF_FILLER)
1656 towin->w_topfill = dp->df_lnum[idx]
1657 + dp->df_count[idx] - lnum;
1658 towin->w_topline = dp->df_lnum[i] + dp->df_count[i];
1659 }
1660 }
1661 }
1662 }
1663
1664 /* safety check (if diff info gets outdated strange things may happen) */
1665 towin->w_botfill = FALSE;
1666 if (towin->w_topline > towin->w_buffer->b_ml.ml_line_count)
1667 {
1668 towin->w_topline = towin->w_buffer->b_ml.ml_line_count;
1669 towin->w_botfill = TRUE;
1670 }
1671 if (towin->w_topline < 1)
1672 {
1673 towin->w_topline = 1;
1674 towin->w_topfill = 0;
1675 }
1676
1677 /* When w_topline changes need to recompute w_botline and cursor position */
1678 invalidate_botline_win(towin);
1679 changed_line_abv_curs_win(towin);
1680
1681 check_topfill(towin, FALSE);
1682#ifdef FEAT_FOLDING
1683 (void)hasFoldingWin(towin, towin->w_topline, &towin->w_topline,
1684 NULL, TRUE, NULL);
1685#endif
1686}
1687
1688/*
1689 * This is called when 'diffopt' is changed.
1690 */
1691 int
1692diffopt_changed()
1693{
1694 char_u *p;
1695 int diff_context_new = 6;
1696 int diff_flags_new = 0;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001697 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698
1699 p = p_dip;
1700 while (*p != NUL)
1701 {
1702 if (STRNCMP(p, "filler", 6) == 0)
1703 {
1704 p += 6;
1705 diff_flags_new |= DIFF_FILLER;
1706 }
1707 else if (STRNCMP(p, "context:", 8) == 0 && VIM_ISDIGIT(p[8]))
1708 {
1709 p += 8;
1710 diff_context_new = getdigits(&p);
1711 }
1712 else if (STRNCMP(p, "icase", 5) == 0)
1713 {
1714 p += 5;
1715 diff_flags_new |= DIFF_ICASE;
1716 }
1717 else if (STRNCMP(p, "iwhite", 6) == 0)
1718 {
1719 p += 6;
1720 diff_flags_new |= DIFF_IWHITE;
1721 }
1722 if (*p != ',' && *p != NUL)
1723 return FAIL;
1724 if (*p == ',')
1725 ++p;
1726 }
1727
1728 /* If "icase" or "iwhite" was added or removed, need to update the diff. */
1729 if (diff_flags != diff_flags_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001730 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
1731 tp->tp_diff_invalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001732
1733 diff_flags = diff_flags_new;
1734 diff_context = diff_context_new;
1735
1736 diff_redraw(TRUE);
1737
1738 /* recompute the scroll binding with the new option value, may
1739 * remove or add filler lines */
1740 check_scrollbind((linenr_T)0, 0L);
1741
1742 return OK;
1743}
1744
1745/*
1746 * Find the difference within a changed line.
1747 * Returns TRUE if the line was added, no other buffer has it.
1748 */
1749 int
1750diff_find_change(wp, lnum, startp, endp)
1751 win_T *wp;
1752 linenr_T lnum;
1753 int *startp; /* first char of the change */
1754 int *endp; /* last char of the change */
1755{
1756 char_u *line_org;
1757 char_u *line_new;
1758 int i;
1759 int si, ei_org, ei_new;
1760 diff_T *dp;
1761 int idx;
1762 int off;
1763 int added = TRUE;
1764
1765 /* Make a copy of the line, the next ml_get() will invalidate it. */
1766 line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE));
1767 if (line_org == NULL)
1768 return FALSE;
1769
1770 idx = diff_buf_idx(wp->w_buffer);
1771 if (idx == DB_COUNT) /* cannot happen */
1772 return FALSE;
1773
1774 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001775 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001776 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
1777 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001778 if (dp == NULL || diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779 return FALSE;
1780
1781 off = lnum - dp->df_lnum[idx];
1782
1783 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001784 if (curtab->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 {
1786 /* Skip lines that are not in the other change (filler lines). */
1787 if (off >= dp->df_count[i])
1788 continue;
1789 added = FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001790 line_new = ml_get_buf(curtab->tp_diffbuf[i], dp->df_lnum[i] + off, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791
1792 /* Search for start of difference */
1793 for (si = 0; line_org[si] != NUL && line_org[si] == line_new[si]; )
1794 ++si;
1795#ifdef FEAT_MBYTE
1796 if (has_mbyte)
1797 {
1798 /* Move back to first byte of character in both lines (may
1799 * have "nn^" in line_org and "n^ in line_new). */
1800 si -= (*mb_head_off)(line_org, line_org + si);
1801 si -= (*mb_head_off)(line_new, line_new + si);
1802 }
1803#endif
1804 if (*startp > si)
1805 *startp = si;
1806
1807 /* Search for end of difference, if any. */
1808 if (line_org[si] != NUL || line_new[si] != NUL)
1809 {
1810 ei_org = (int)STRLEN(line_org);
1811 ei_new = (int)STRLEN(line_new);
1812 while (ei_org >= *startp && ei_new >= *startp
1813 && ei_org >= 0 && ei_new >= 0
1814 && line_org[ei_org] == line_new[ei_new])
1815 {
1816 --ei_org;
1817 --ei_new;
1818 }
1819 if (*endp < ei_org)
1820 *endp = ei_org;
1821 }
1822 }
1823
1824 vim_free(line_org);
1825 return added;
1826}
1827
1828#if defined(FEAT_FOLDING) || defined(PROTO)
1829/*
1830 * Return TRUE if line "lnum" is not close to a diff block, this line should
1831 * be in a fold.
1832 * Return FALSE if there are no diff blocks at all in this window.
1833 */
1834 int
1835diff_infold(wp, lnum)
1836 win_T *wp;
1837 linenr_T lnum;
1838{
1839 int i;
1840 int idx = -1;
1841 int other = FALSE;
1842 diff_T *dp;
1843
1844 /* Return if 'diff' isn't set. */
1845 if (!wp->w_p_diff)
1846 return FALSE;
1847
1848 for (i = 0; i < DB_COUNT; ++i)
1849 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001850 if (curtab->tp_diffbuf[i] == wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 idx = i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001852 else if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 other = TRUE;
1854 }
1855
1856 /* return here if there are no diffs in the window */
1857 if (idx == -1 || !other)
1858 return FALSE;
1859
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001860 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 ex_diffupdate(NULL); /* update after a big change */
1862
1863 /* Return if there are no diff blocks. All lines will be folded. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001864 if (curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 return TRUE;
1866
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001867 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 {
1869 /* If this change is below the line there can't be any further match. */
1870 if (dp->df_lnum[idx] - diff_context > lnum)
1871 break;
1872 /* If this change ends before the line we have a match. */
1873 if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum)
1874 return FALSE;
1875 }
1876 return TRUE;
1877}
1878#endif
1879
1880/*
1881 * "dp" and "do" commands.
1882 */
1883 void
1884nv_diffgetput(put)
1885 int put;
1886{
1887 exarg_T ea;
1888
1889 ea.arg = (char_u *)"";
1890 if (put)
1891 ea.cmdidx = CMD_diffput;
1892 else
1893 ea.cmdidx = CMD_diffget;
1894 ea.addr_count = 0;
1895 ea.line1 = curwin->w_cursor.lnum;
1896 ea.line2 = curwin->w_cursor.lnum;
1897 ex_diffgetput(&ea);
1898}
1899
1900/*
1901 * ":diffget"
1902 * ":diffput"
1903 */
1904 void
1905ex_diffgetput(eap)
1906 exarg_T *eap;
1907{
1908 linenr_T lnum;
1909 int count;
1910 linenr_T off = 0;
1911 diff_T *dp;
1912 diff_T *dprev;
1913 diff_T *dfree;
1914 int idx_cur;
1915 int idx_other;
1916 int idx_from;
1917 int idx_to;
1918 int i;
1919 int added;
1920 char_u *p;
1921 aco_save_T aco;
1922 buf_T *buf;
1923 int start_skip, end_skip;
1924 int new_count;
Bram Moolenaar280f1262006-01-30 00:14:18 +00001925 int buf_empty;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001926
1927 /* Find the current buffer in the list of diff buffers. */
1928 idx_cur = diff_buf_idx(curbuf);
1929 if (idx_cur == DB_COUNT)
1930 {
1931 EMSG(_("E99: Current buffer is not in diff mode"));
1932 return;
1933 }
1934
1935 if (*eap->arg == NUL)
1936 {
1937 /* No argument: Find the other buffer in the list of diff buffers. */
1938 for (idx_other = 0; idx_other < DB_COUNT; ++idx_other)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001939 if (curtab->tp_diffbuf[idx_other] != curbuf
1940 && curtab->tp_diffbuf[idx_other] != NULL
Bram Moolenaar1e015462005-09-25 22:16:38 +00001941 && (eap->cmdidx != CMD_diffput
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001942 || curtab->tp_diffbuf[idx_other]->b_p_ma))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 break;
1944 if (idx_other == DB_COUNT)
1945 {
1946 EMSG(_("E100: No other buffer in diff mode"));
1947 return;
1948 }
1949
1950 /* Check that there isn't a third buffer in the list */
1951 for (i = idx_other + 1; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001952 if (curtab->tp_diffbuf[i] != curbuf
1953 && curtab->tp_diffbuf[i] != NULL
1954 && (eap->cmdidx != CMD_diffput || curtab->tp_diffbuf[i]->b_p_ma))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 {
1956 EMSG(_("E101: More than two buffers in diff mode, don't know which one to use"));
1957 return;
1958 }
1959 }
1960 else
1961 {
1962 /* Buffer number or pattern given. Ignore trailing white space. */
1963 p = eap->arg + STRLEN(eap->arg);
1964 while (p > eap->arg && vim_iswhite(p[-1]))
1965 --p;
1966 for (i = 0; vim_isdigit(eap->arg[i]) && eap->arg + i < p; ++i)
1967 ;
1968 if (eap->arg + i == p) /* digits only */
1969 i = atol((char *)eap->arg);
1970 else
1971 {
1972 i = buflist_findpat(eap->arg, p, FALSE, TRUE);
1973 if (i < 0)
1974 return; /* error message already given */
1975 }
1976 buf = buflist_findnr(i);
1977 if (buf == NULL)
1978 {
1979 EMSG2(_("E102: Can't find buffer \"%s\""), eap->arg);
1980 return;
1981 }
1982 idx_other = diff_buf_idx(buf);
1983 if (idx_other == DB_COUNT)
1984 {
1985 EMSG2(_("E103: Buffer \"%s\" is not in diff mode"), eap->arg);
1986 return;
1987 }
1988 }
1989
1990 diff_busy = TRUE;
1991
1992 /* When no range given include the line above or below the cursor. */
1993 if (eap->addr_count == 0)
1994 {
1995 /* Make it possible that ":diffget" on the last line gets line below
1996 * the cursor line when there is no difference above the cursor. */
1997 if (eap->cmdidx == CMD_diffget
1998 && eap->line1 == curbuf->b_ml.ml_line_count
1999 && diff_check(curwin, eap->line1) == 0
2000 && (eap->line1 == 1 || diff_check(curwin, eap->line1 - 1) == 0))
2001 ++eap->line2;
2002 else if (eap->line1 > 0)
2003 --eap->line1;
2004 }
2005
2006 if (eap->cmdidx == CMD_diffget)
2007 {
2008 idx_from = idx_other;
2009 idx_to = idx_cur;
2010 }
2011 else
2012 {
2013 idx_from = idx_cur;
2014 idx_to = idx_other;
2015 /* Need to make the other buffer the current buffer to be able to make
2016 * changes in it. */
2017 /* set curwin/curbuf to buf and save a few things */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002018 aucmd_prepbuf(&aco, curtab->tp_diffbuf[idx_other]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002019 }
2020
2021 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002022 for (dp = curtab->tp_first_diff; dp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002023 {
2024 if (dp->df_lnum[idx_cur] > eap->line2 + off)
2025 break; /* past the range that was specified */
2026
2027 dfree = NULL;
2028 lnum = dp->df_lnum[idx_to];
2029 count = dp->df_count[idx_to];
2030 if (dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off
2031 && u_save(lnum - 1, lnum + count) != FAIL)
2032 {
2033 /* Inside the specified range and saving for undo worked. */
2034 start_skip = 0;
2035 end_skip = 0;
2036 if (eap->addr_count > 0)
2037 {
2038 /* A range was specified: check if lines need to be skipped. */
2039 start_skip = eap->line1 + off - dp->df_lnum[idx_cur];
2040 if (start_skip > 0)
2041 {
2042 /* range starts below start of current diff block */
2043 if (start_skip > count)
2044 {
2045 lnum += count;
2046 count = 0;
2047 }
2048 else
2049 {
2050 count -= start_skip;
2051 lnum += start_skip;
2052 }
2053 }
2054 else
2055 start_skip = 0;
2056
2057 end_skip = dp->df_lnum[idx_cur] + dp->df_count[idx_cur] - 1
2058 - (eap->line2 + off);
2059 if (end_skip > 0)
2060 {
2061 /* range ends above end of current/from diff block */
2062 if (idx_cur == idx_from) /* :diffput */
2063 {
2064 i = dp->df_count[idx_cur] - start_skip - end_skip;
2065 if (count > i)
2066 count = i;
2067 }
2068 else /* :diffget */
2069 {
2070 count -= end_skip;
2071 end_skip = dp->df_count[idx_from] - start_skip - count;
2072 if (end_skip < 0)
2073 end_skip = 0;
2074 }
2075 }
2076 else
2077 end_skip = 0;
2078 }
2079
Bram Moolenaar280f1262006-01-30 00:14:18 +00002080 buf_empty = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002081 added = 0;
2082 for (i = 0; i < count; ++i)
2083 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00002084 /* remember deleting the last line of the buffer */
2085 buf_empty = curbuf->b_ml.ml_line_count == 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002086 ml_delete(lnum, FALSE);
2087 --added;
2088 }
2089 for (i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; ++i)
2090 {
2091 linenr_T nr;
2092
2093 nr = dp->df_lnum[idx_from] + start_skip + i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002094 if (nr > curtab->tp_diffbuf[idx_from]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002095 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002096 p = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx_from], nr, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002097 if (p != NULL)
2098 {
2099 ml_append(lnum + i - 1, p, 0, FALSE);
2100 vim_free(p);
2101 ++added;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002102 if (buf_empty && curbuf->b_ml.ml_line_count == 2)
2103 {
2104 /* Added the first line into an empty buffer, need to
2105 * delete the dummy empty line. */
2106 buf_empty = FALSE;
2107 ml_delete((linenr_T)2, FALSE);
2108 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 }
2110 }
2111 new_count = dp->df_count[idx_to] + added;
2112 dp->df_count[idx_to] = new_count;
2113
2114 if (start_skip == 0 && end_skip == 0)
2115 {
2116 /* Check if there are any other buffers and if the diff is
2117 * equal in them. */
2118 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002119 if (curtab->tp_diffbuf[i] != NULL && i != idx_from && i != idx_to
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 && !diff_equal_entry(dp, idx_from, i))
2121 break;
2122 if (i == DB_COUNT)
2123 {
2124 /* delete the diff entry, the buffers are now equal here */
2125 dfree = dp;
2126 dp = dp->df_next;
2127 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002128 curtab->tp_first_diff = dp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 else
2130 dprev->df_next = dp;
2131 }
2132 }
2133
2134 /* Adjust marks. This will change the following entries! */
2135 if (added != 0)
2136 {
2137 mark_adjust(lnum, lnum + count - 1, (long)MAXLNUM, (long)added);
2138 if (curwin->w_cursor.lnum >= lnum)
2139 {
2140 /* Adjust the cursor position if it's in/after the changed
2141 * lines. */
2142 if (curwin->w_cursor.lnum >= lnum + count)
2143 curwin->w_cursor.lnum += added;
2144 else if (added < 0)
2145 curwin->w_cursor.lnum = lnum;
2146 }
2147 }
2148 changed_lines(lnum, 0, lnum + count, (long)added);
2149
2150 if (dfree != NULL)
2151 {
2152 /* Diff is deleted, update folds in other windows. */
2153#ifdef FEAT_FOLDING
2154 diff_fold_update(dfree, idx_to);
2155#endif
2156 vim_free(dfree);
2157 }
2158 else
2159 /* mark_adjust() may have changed the count in a wrong way */
2160 dp->df_count[idx_to] = new_count;
2161
2162 /* When changing the current buffer, keep track of line numbers */
2163 if (idx_cur == idx_to)
2164 off += added;
2165 }
2166
2167 /* If before the range or not deleted, go to next diff. */
2168 if (dfree == NULL)
2169 {
2170 dprev = dp;
2171 dp = dp->df_next;
2172 }
2173 }
2174
2175 /* restore curwin/curbuf and a few other things */
2176 if (idx_other == idx_to)
2177 {
2178 /* Syncing undo only works for the current buffer, but we change
2179 * another buffer. Sync undo if the command was typed. This isn't
2180 * 100% right when ":diffput" is used in a function or mapping. */
2181 if (KeyTyped)
2182 u_sync();
2183 aucmd_restbuf(&aco);
2184 }
2185
2186 diff_busy = FALSE;
2187
2188 /* Check that the cursor is on a valid character and update it's position.
2189 * When there were filler lines the topline has become invalid. */
2190 check_cursor();
2191 changed_line_abv_curs();
2192
2193 /* Also need to redraw the other buffers. */
2194 diff_redraw(FALSE);
2195}
2196
2197#ifdef FEAT_FOLDING
2198/*
2199 * Update folds for all diff buffers for entry "dp".
2200 * Skip buffer with index "skip_idx".
2201 * When there are no diffs, all folds are removed.
2202 */
2203 static void
2204diff_fold_update(dp, skip_idx)
2205 diff_T *dp;
2206 int skip_idx;
2207{
2208 int i;
2209 win_T *wp;
2210
2211 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2212 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002213 if (curtab->tp_diffbuf[i] == wp->w_buffer && i != skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002214 foldUpdate(wp, dp->df_lnum[i],
2215 dp->df_lnum[i] + dp->df_count[i]);
2216}
2217#endif
2218
2219/*
2220 * Return TRUE if buffer "buf" is in diff-mode.
2221 */
2222 int
2223diff_mode_buf(buf)
2224 buf_T *buf;
2225{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002226 tabpage_T *tp;
2227
2228 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
2229 if (diff_buf_idx_tp(buf, tp) != DB_COUNT)
2230 return TRUE;
2231 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232}
2233
2234/*
2235 * Move "count" times in direction "dir" to the next diff block.
2236 * Return FAIL if there isn't such a diff block.
2237 */
2238 int
2239diff_move_to(dir, count)
2240 int dir;
2241 long count;
2242{
2243 int idx;
2244 linenr_T lnum = curwin->w_cursor.lnum;
2245 diff_T *dp;
2246
2247 idx = diff_buf_idx(curbuf);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002248 if (idx == DB_COUNT || curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 return FAIL;
2250
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002251 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252 ex_diffupdate(NULL); /* update after a big change */
2253
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002254 if (curtab->tp_first_diff == NULL) /* no diffs today */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 return FAIL;
2256
2257 while (--count >= 0)
2258 {
2259 /* Check if already before first diff. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002260 if (dir == BACKWARD && lnum <= curtab->tp_first_diff->df_lnum[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002261 break;
2262
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002263 for (dp = curtab->tp_first_diff; ; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002264 {
2265 if (dp == NULL)
2266 break;
2267 if ((dir == FORWARD && lnum < dp->df_lnum[idx])
2268 || (dir == BACKWARD
2269 && (dp->df_next == NULL
2270 || lnum <= dp->df_next->df_lnum[idx])))
2271 {
2272 lnum = dp->df_lnum[idx];
2273 break;
2274 }
2275 }
2276 }
2277
2278 /* don't end up past the end of the file */
2279 if (lnum > curbuf->b_ml.ml_line_count)
2280 lnum = curbuf->b_ml.ml_line_count;
2281
2282 /* When the cursor didn't move at all we fail. */
2283 if (lnum == curwin->w_cursor.lnum)
2284 return FAIL;
2285
2286 setpcmark();
2287 curwin->w_cursor.lnum = lnum;
2288 curwin->w_cursor.col = 0;
2289
2290 return OK;
2291}
2292
2293#if defined(FEAT_FOLDING) || defined(PROTO)
2294/*
2295 * For line "lnum" in the current window find the equivalent lnum in window
2296 * "wp", compensating for inserted/deleted lines.
2297 */
2298 linenr_T
2299diff_lnum_win(lnum, wp)
2300 linenr_T lnum;
2301 win_T *wp;
2302{
2303 diff_T *dp;
2304 int idx;
2305 int i;
2306 linenr_T n;
2307
2308 idx = diff_buf_idx(curbuf);
2309 if (idx == DB_COUNT) /* safety check */
2310 return (linenr_T)0;
2311
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002312 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 ex_diffupdate(NULL); /* update after a big change */
2314
2315 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002316 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
2318 break;
2319
2320 /* When after the last change, compute relative to the last line number. */
2321 if (dp == NULL)
2322 return wp->w_buffer->b_ml.ml_line_count
2323 - (curbuf->b_ml.ml_line_count - lnum);
2324
2325 /* Find index for "wp". */
2326 i = diff_buf_idx(wp->w_buffer);
2327 if (i == DB_COUNT) /* safety check */
2328 return (linenr_T)0;
2329
2330 n = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]);
2331 if (n > dp->df_lnum[i] + dp->df_count[i])
2332 n = dp->df_lnum[i] + dp->df_count[i];
2333 return n;
2334}
2335#endif
2336
2337#endif /* FEAT_DIFF */