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