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