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