1 /* vim:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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.
11 * diff.c: code for diff'ing two or three buffers.
16 #if defined(FEAT_DIFF) || defined(PROTO)
18 static int diff_busy
= FALSE
; /* ex_diffgetput() is busy */
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 static int diff_flags
= DIFF_FILLER
;
28 #define LBUFLEN 50 /* length of line in diff file */
30 static int diff_a_works
= MAYBE
; /* TRUE when "diff -a" works, FALSE when it
31 doesn't work, MAYBE when not checked yet */
32 #if defined(MSWIN) || defined(MSDOS)
33 static int diff_bin_works
= MAYBE
; /* TRUE when "diff --binary" works, FALSE
34 when it doesn't work, MAYBE when not
38 static int diff_buf_idx
__ARGS((buf_T
*buf
));
39 static int diff_buf_idx_tp
__ARGS((buf_T
*buf
, tabpage_T
*tp
));
40 static void diff_mark_adjust_tp
__ARGS((tabpage_T
*tp
, int idx
, linenr_T line1
, linenr_T line2
, long amount
, long amount_after
));
41 static void diff_check_unchanged
__ARGS((tabpage_T
*tp
, diff_T
*dp
));
42 static int diff_check_sanity
__ARGS((tabpage_T
*tp
, diff_T
*dp
));
43 static void diff_redraw
__ARGS((int dofold
));
44 static int diff_write
__ARGS((buf_T
*buf
, char_u
*fname
));
45 static void diff_file
__ARGS((char_u
*tmp_orig
, char_u
*tmp_new
, char_u
*tmp_diff
));
46 static int diff_equal_entry
__ARGS((diff_T
*dp
, int idx1
, int idx2
));
47 static int diff_cmp
__ARGS((char_u
*s1
, char_u
*s2
));
49 static void diff_fold_update
__ARGS((diff_T
*dp
, int skip_idx
));
51 static void diff_read
__ARGS((int idx_orig
, int idx_new
, char_u
*fname
));
52 static void diff_copy_entry
__ARGS((diff_T
*dprev
, diff_T
*dp
, int idx_orig
, int idx_new
));
53 static diff_T
*diff_alloc_new
__ARGS((tabpage_T
*tp
, diff_T
*dprev
, diff_T
*dp
));
56 # define tag_fgets vim_fgets
60 * Called when deleting or unloading a buffer: No longer make a diff with it.
69 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
71 i
= diff_buf_idx_tp(buf
, tp
);
74 tp
->tp_diffbuf
[i
] = NULL
;
75 tp
->tp_diff_invalid
= TRUE
;
81 * Check if the current buffer should be added to or removed from the list of
93 /* When there is no window showing a diff for this buffer, remove
94 * it from the diffs. */
95 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
96 if (wp
->w_buffer
== win
->w_buffer
&& wp
->w_p_diff
)
100 i
= diff_buf_idx(win
->w_buffer
);
103 curtab
->tp_diffbuf
[i
] = NULL
;
104 curtab
->tp_diff_invalid
= TRUE
;
109 diff_buf_add(win
->w_buffer
);
113 * Add a buffer to make diffs for.
114 * Call this when a new buffer is being edited in the current window where
116 * Marks the current buffer as being part of the diff and requireing updating.
117 * This must be done before any autocmd, because a command may use info
118 * about the screen contents.
126 if (diff_buf_idx(buf
) != DB_COUNT
)
127 return; /* It's already there. */
129 for (i
= 0; i
< DB_COUNT
; ++i
)
130 if (curtab
->tp_diffbuf
[i
] == NULL
)
132 curtab
->tp_diffbuf
[i
] = buf
;
133 curtab
->tp_diff_invalid
= TRUE
;
137 EMSGN(_("E96: Can not diff more than %ld buffers"), DB_COUNT
);
141 * Find buffer "buf" in the list of diff buffers for the current tab page.
142 * Return its index or DB_COUNT if not found.
150 for (idx
= 0; idx
< DB_COUNT
; ++idx
)
151 if (curtab
->tp_diffbuf
[idx
] == buf
)
157 * Find buffer "buf" in the list of diff buffers for tab page "tp".
158 * Return its index or DB_COUNT if not found.
161 diff_buf_idx_tp(buf
, tp
)
167 for (idx
= 0; idx
< DB_COUNT
; ++idx
)
168 if (tp
->tp_diffbuf
[idx
] == buf
)
174 * Mark the diff info involving buffer "buf" as invalid, it will be updated
175 * when info is requested.
184 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
186 i
= diff_buf_idx_tp(buf
, tp
);
189 tp
->tp_diff_invalid
= TRUE
;
197 * Called by mark_adjust(): update line numbers in "curbuf".
200 diff_mark_adjust(line1
, line2
, amount
, amount_after
)
209 /* Handle all tab pages that use the current buffer in a diff. */
210 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
212 idx
= diff_buf_idx_tp(curbuf
, tp
);
214 diff_mark_adjust_tp(tp
, idx
, line1
, line2
, amount
, amount_after
);
219 * Update line numbers in tab page "tp" for "curbuf" with index "idx".
220 * This attempts to update the changes as much as possible:
221 * When inserting/deleting lines outside of existing change blocks, create a
222 * new change block and update the line numbers in following blocks.
223 * When inserting/deleting lines in existing change blocks, update them.
226 diff_mark_adjust_tp(tp
, idx
, line1
, line2
, amount
, amount_after
)
238 int inserted
, deleted
;
241 linenr_T lnum_deleted
= line1
; /* lnum of remaining deletion */
244 if (line2
== MAXLNUM
)
246 /* mark_adjust(99, MAXLNUM, 9, 0): insert lines */
250 else if (amount_after
> 0)
252 /* mark_adjust(99, 98, MAXLNUM, 9): a change that inserts lines*/
253 inserted
= amount_after
;
258 /* mark_adjust(98, 99, MAXLNUM, -2): delete lines */
260 deleted
= -amount_after
;
264 dp
= tp
->tp_first_diff
;
267 /* If the change is after the previous diff block and before the next
268 * diff block, thus not touching an existing change, create a new diff
269 * block. Don't do this when ex_diffgetput() is busy. */
270 if ((dp
== NULL
|| dp
->df_lnum
[idx
] - 1 > line2
271 || (line2
== MAXLNUM
&& dp
->df_lnum
[idx
] > line1
))
273 || dprev
->df_lnum
[idx
] + dprev
->df_count
[idx
] < line1
)
276 dnext
= diff_alloc_new(tp
, dprev
, dp
);
280 dnext
->df_lnum
[idx
] = line1
;
281 dnext
->df_count
[idx
] = inserted
;
282 for (i
= 0; i
< DB_COUNT
; ++i
)
283 if (tp
->tp_diffbuf
[i
] != NULL
&& i
!= idx
)
286 dnext
->df_lnum
[i
] = line1
;
288 dnext
->df_lnum
[i
] = line1
289 + (dprev
->df_lnum
[i
] + dprev
->df_count
[i
])
290 - (dprev
->df_lnum
[idx
] + dprev
->df_count
[idx
]);
291 dnext
->df_count
[i
] = deleted
;
295 /* if at end of the list, quit */
300 * Check for these situations:
310 /* compute last line of this change */
311 last
= dp
->df_lnum
[idx
] + dp
->df_count
[idx
] - 1;
313 /* 1. change completely above line1: nothing to do */
314 if (last
>= line1
- 1)
316 /* 6. change below line2: only adjust for amount_after; also when
317 * "deleted" became zero when deleted all lines between two diffs */
318 if (dp
->df_lnum
[idx
] - (deleted
+ inserted
!= 0) > line2
)
320 if (amount_after
== 0)
321 break; /* nothing left to change */
322 dp
->df_lnum
[idx
] += amount_after
;
326 check_unchanged
= FALSE
;
328 /* 2. 3. 4. 5.: inserted/deleted lines touching this diff. */
331 if (dp
->df_lnum
[idx
] >= line1
)
333 off
= dp
->df_lnum
[idx
] - lnum_deleted
;
336 /* 4. delete all lines of diff */
337 if (dp
->df_next
!= NULL
338 && dp
->df_next
->df_lnum
[idx
] - 1 <= line2
)
340 /* delete continues in next diff, only do
341 * lines until that one */
342 n
= dp
->df_next
->df_lnum
[idx
] - lnum_deleted
;
344 n
-= dp
->df_count
[idx
];
345 lnum_deleted
= dp
->df_next
->df_lnum
[idx
];
348 n
= deleted
- dp
->df_count
[idx
];
349 dp
->df_count
[idx
] = 0;
353 /* 5. delete lines at or just before top of diff */
355 dp
->df_count
[idx
] -= line2
- dp
->df_lnum
[idx
] + 1;
356 check_unchanged
= TRUE
;
358 dp
->df_lnum
[idx
] = line1
;
365 /* 2. delete at end of of diff */
366 dp
->df_count
[idx
] -= last
- lnum_deleted
+ 1;
367 if (dp
->df_next
!= NULL
368 && dp
->df_next
->df_lnum
[idx
] - 1 <= line2
)
370 /* delete continues in next diff, only do
371 * lines until that one */
372 n
= dp
->df_next
->df_lnum
[idx
] - 1 - last
;
373 deleted
-= dp
->df_next
->df_lnum
[idx
]
375 lnum_deleted
= dp
->df_next
->df_lnum
[idx
];
379 check_unchanged
= TRUE
;
383 /* 3. delete lines inside the diff */
385 dp
->df_count
[idx
] -= deleted
;
389 for (i
= 0; i
< DB_COUNT
; ++i
)
390 if (tp
->tp_diffbuf
[i
] != NULL
&& i
!= idx
)
392 dp
->df_lnum
[i
] -= off
;
393 dp
->df_count
[i
] += n
;
398 if (dp
->df_lnum
[idx
] <= line1
)
400 /* inserted lines somewhere in this diff */
401 dp
->df_count
[idx
] += inserted
;
402 check_unchanged
= TRUE
;
405 /* inserted lines somewhere above this diff */
406 dp
->df_lnum
[idx
] += inserted
;
410 /* Check if inserted lines are equal, may reduce the
411 * size of the diff. TODO: also check for equal lines
412 * in the middle and perhaps split the block. */
413 diff_check_unchanged(tp
, dp
);
417 /* check if this block touches the previous one, may merge them. */
418 if (dprev
!= NULL
&& dprev
->df_lnum
[idx
] + dprev
->df_count
[idx
]
421 for (i
= 0; i
< DB_COUNT
; ++i
)
422 if (tp
->tp_diffbuf
[i
] != NULL
)
423 dprev
->df_count
[i
] += dp
->df_count
[i
];
424 dprev
->df_next
= dp
->df_next
;
430 /* Advance to next entry. */
437 dp
= tp
->tp_first_diff
;
440 /* All counts are zero, remove this entry. */
441 for (i
= 0; i
< DB_COUNT
; ++i
)
442 if (tp
->tp_diffbuf
[i
] != NULL
&& dp
->df_count
[i
] != 0)
450 tp
->tp_first_diff
= dnext
;
452 dprev
->df_next
= dnext
;
456 /* Advance to next entry. */
467 /* Need to recompute the scroll binding, may remove or add filler
468 * lines (e.g., when adding lines above w_topline). But it's slow when
469 * making many changes, postpone until redrawing. */
470 diff_need_scrollbind
= TRUE
;
475 * Allocate a new diff block and link it between "dprev" and "dp".
478 diff_alloc_new(tp
, dprev
, dp
)
485 dnew
= (diff_T
*)alloc((unsigned)sizeof(diff_T
));
490 tp
->tp_first_diff
= dnew
;
492 dprev
->df_next
= dnew
;
498 * Check if the diff block "dp" can be made smaller for lines at the start and
499 * end that are equal. Called after inserting lines.
500 * This may result in a change where all buffers have zero lines, the caller
501 * must take care of removing it.
504 diff_check_unchanged(tp
, dp
)
510 int off_org
, off_new
;
514 /* Find the first buffers, use it as the original, compare the other
515 * buffer lines against this one. */
516 for (i_org
= 0; i_org
< DB_COUNT
; ++i_org
)
517 if (tp
->tp_diffbuf
[i_org
] != NULL
)
519 if (i_org
== DB_COUNT
) /* safety check */
522 if (diff_check_sanity(tp
, dp
) == FAIL
)
525 /* First check lines at the top, then at the bottom. */
530 /* Repeat until a line is found which is different or the number of
531 * lines has become zero. */
532 while (dp
->df_count
[i_org
] > 0)
534 /* Copy the line, the next ml_get() will invalidate it. */
536 off_org
= dp
->df_count
[i_org
] - 1;
537 line_org
= vim_strsave(ml_get_buf(tp
->tp_diffbuf
[i_org
],
538 dp
->df_lnum
[i_org
] + off_org
, FALSE
));
539 if (line_org
== NULL
)
541 for (i_new
= i_org
+ 1; i_new
< DB_COUNT
; ++i_new
)
543 if (tp
->tp_diffbuf
[i_new
] == NULL
)
546 off_new
= dp
->df_count
[i_new
] - 1;
547 /* if other buffer doesn't have this line, it was inserted */
548 if (off_new
< 0 || off_new
>= dp
->df_count
[i_new
])
550 if (diff_cmp(line_org
, ml_get_buf(tp
->tp_diffbuf
[i_new
],
551 dp
->df_lnum
[i_new
] + off_new
, FALSE
)) != 0)
556 /* Stop when a line isn't equal in all diff buffers. */
557 if (i_new
!= DB_COUNT
)
560 /* Line matched in all buffers, remove it from the diff. */
561 for (i_new
= i_org
; i_new
< DB_COUNT
; ++i_new
)
562 if (tp
->tp_diffbuf
[i_new
] != NULL
)
565 ++dp
->df_lnum
[i_new
];
566 --dp
->df_count
[i_new
];
576 * Check if a diff block doesn't contain invalid line numbers.
577 * This can happen when the diff program returns invalid results.
580 diff_check_sanity(tp
, dp
)
586 for (i
= 0; i
< DB_COUNT
; ++i
)
587 if (tp
->tp_diffbuf
[i
] != NULL
)
588 if (dp
->df_lnum
[i
] + dp
->df_count
[i
] - 1
589 > tp
->tp_diffbuf
[i
]->b_ml
.ml_line_count
)
595 * Mark all diff buffers in the current tab page for redraw.
599 int dofold
; /* also recompute the folds */
604 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
607 redraw_win_later(wp
, SOME_VALID
);
609 if (dofold
&& foldmethodIsDiff(wp
))
612 /* A change may have made filler lines invalid, need to take care
613 * of that for other windows. */
614 if (wp
!= curwin
&& wp
->w_topfill
> 0)
616 n
= diff_check(wp
, wp
->w_topline
);
617 if (wp
->w_topfill
> n
)
618 wp
->w_topfill
= (n
< 0 ? 0 : n
);
624 * Write buffer "buf" to file "name".
625 * Always use 'fileformat' set to "unix".
626 * Return FAIL for failure
629 diff_write(buf
, fname
)
636 save_ff
= buf
->b_p_ff
;
637 buf
->b_p_ff
= vim_strsave((char_u
*)FF_UNIX
);
638 r
= buf_write(buf
, fname
, NULL
, (linenr_T
)1, buf
->b_ml
.ml_line_count
,
639 NULL
, FALSE
, FALSE
, FALSE
, TRUE
);
640 free_string_option(buf
->b_p_ff
);
641 buf
->b_p_ff
= save_ff
;
646 * Completely update the diffs for the buffers involved.
647 * This uses the ordinary "diff" command.
648 * The buffers are written to a file, also for unmodified buffers (the file
649 * could have been produced by autocommands, e.g. the netrw plugin).
654 exarg_T
*eap
; /* can be NULL, it's not used */
665 /* Delete all diffblocks. */
667 curtab
->tp_diff_invalid
= FALSE
;
669 /* Use the first buffer as the original text. */
670 for (idx_orig
= 0; idx_orig
< DB_COUNT
; ++idx_orig
)
671 if (curtab
->tp_diffbuf
[idx_orig
] != NULL
)
673 if (idx_orig
== DB_COUNT
)
676 /* Only need to do something when there is another buffer. */
677 for (idx_new
= idx_orig
+ 1; idx_new
< DB_COUNT
; ++idx_new
)
678 if (curtab
->tp_diffbuf
[idx_new
] != NULL
)
680 if (idx_new
== DB_COUNT
)
683 /* We need three temp file names. */
684 tmp_orig
= vim_tempname('o');
685 tmp_new
= vim_tempname('n');
686 tmp_diff
= vim_tempname('d');
687 if (tmp_orig
== NULL
|| tmp_new
== NULL
|| tmp_diff
== NULL
)
691 * Do a quick test if "diff" really works. Otherwise it looks like there
692 * are no differences. Can't use the return value, it's non-zero when
693 * there are differences.
694 * May try twice, first with "-a" and then without.
699 fd
= mch_fopen((char *)tmp_orig
, "w");
702 fwrite("line1\n", (size_t)6, (size_t)1, fd
);
704 fd
= mch_fopen((char *)tmp_new
, "w");
707 fwrite("line2\n", (size_t)6, (size_t)1, fd
);
709 diff_file(tmp_orig
, tmp_new
, tmp_diff
);
710 fd
= mch_fopen((char *)tmp_diff
, "r");
713 char_u linebuf
[LBUFLEN
];
717 /* There must be a line that contains "1c1". */
718 if (tag_fgets(linebuf
, LBUFLEN
, fd
))
720 if (STRNCMP(linebuf
, "1c1", 3) == 0)
725 mch_remove(tmp_diff
);
728 mch_remove(tmp_orig
);
732 /* When using 'diffexpr' break here. */
737 #if defined(MSWIN) || defined(MSDOS)
738 /* If the "-a" argument works, also check if "--binary" works. */
739 if (ok
&& diff_a_works
== MAYBE
&& diff_bin_works
== MAYBE
)
742 diff_bin_works
= TRUE
;
745 if (!ok
&& diff_a_works
== TRUE
&& diff_bin_works
== TRUE
)
747 /* Tried --binary, but it failed. "-a" works though. */
748 diff_bin_works
= FALSE
;
753 /* If we checked if "-a" works already, break here. */
754 if (diff_a_works
!= MAYBE
)
758 /* If "-a" works break here, otherwise retry without "-a". */
764 EMSG(_("E97: Cannot create diffs"));
765 diff_a_works
= MAYBE
;
766 #if defined(MSWIN) || defined(MSDOS)
767 diff_bin_works
= MAYBE
;
772 /* Write the first buffer to a tempfile. */
773 buf
= curtab
->tp_diffbuf
[idx_orig
];
774 if (diff_write(buf
, tmp_orig
) == FAIL
)
777 /* Make a difference between the first buffer and every other. */
778 for (idx_new
= idx_orig
+ 1; idx_new
< DB_COUNT
; ++idx_new
)
780 buf
= curtab
->tp_diffbuf
[idx_new
];
783 if (diff_write(buf
, tmp_new
) == FAIL
)
785 diff_file(tmp_orig
, tmp_new
, tmp_diff
);
787 /* Read the diff output and add each entry to the diff list. */
788 diff_read(idx_orig
, idx_new
, tmp_diff
);
789 mch_remove(tmp_diff
);
792 mch_remove(tmp_orig
);
803 * Make a diff between files "tmp_orig" and "tmp_new", results in "tmp_diff".
806 diff_file(tmp_orig
, tmp_new
, tmp_diff
)
815 /* Use 'diffexpr' to generate the diff file. */
816 eval_diff(tmp_orig
, tmp_new
, tmp_diff
);
820 cmd
= alloc((unsigned)(STRLEN(tmp_orig
) + STRLEN(tmp_new
)
821 + STRLEN(tmp_diff
) + STRLEN(p_srr
) + 27));
824 /* We don't want $DIFF_OPTIONS to get in the way. */
825 if (getenv("DIFF_OPTIONS"))
826 vim_setenv((char_u
*)"DIFF_OPTIONS", (char_u
*)"");
828 /* Build the diff command and execute it. Always use -a, binary
829 * differences are of no use. Ignore errors, diff returns
830 * non-zero when differences have been found. */
831 sprintf((char *)cmd
, "diff %s%s%s%s%s %s",
832 diff_a_works
== FALSE
? "" : "-a ",
833 #if defined(MSWIN) || defined(MSDOS)
834 diff_bin_works
== TRUE
? "--binary " : "",
838 (diff_flags
& DIFF_IWHITE
) ? "-b " : "",
839 (diff_flags
& DIFF_ICASE
) ? "-i " : "",
841 append_redir(cmd
, p_srr
, tmp_diff
);
843 ++autocmd_block
; /* Avoid ShellCmdPost stuff */
845 (void)call_shell(cmd
, SHELL_FILTER
|SHELL_SILENT
|SHELL_DOOUT
);
855 * Create a new version of a file from the current buffer and a diff file.
856 * The buffer is written to a file, also for unmodified buffers (the file
857 * could have been produced by autocommands, e.g. the netrw plugin).
863 char_u
*tmp_orig
; /* name of original temp file */
864 char_u
*tmp_new
; /* name of patched temp file */
866 win_T
*old_curwin
= curwin
;
867 char_u
*newname
= NULL
; /* name of patched file buffer */
869 char_u dirbuf
[MAXPATHL
];
870 char_u
*fullname
= NULL
;
873 char_u
*browseFile
= NULL
;
874 int browse_flag
= cmdmod
.browse
;
880 browseFile
= do_browse(0, (char_u
*)_("Patch file"),
881 eap
->arg
, NULL
, NULL
, BROWSE_FILTER_ALL_FILES
, NULL
);
882 if (browseFile
== NULL
)
883 return; /* operation cancelled */
884 eap
->arg
= browseFile
;
885 cmdmod
.browse
= FALSE
; /* don't let do_ecmd() browse again */
889 /* We need two temp file names. */
890 tmp_orig
= vim_tempname('o');
891 tmp_new
= vim_tempname('n');
892 if (tmp_orig
== NULL
|| tmp_new
== NULL
)
895 /* Write the current buffer to "tmp_orig". */
896 if (buf_write(curbuf
, tmp_orig
, NULL
,
897 (linenr_T
)1, curbuf
->b_ml
.ml_line_count
,
898 NULL
, FALSE
, FALSE
, FALSE
, TRUE
) == FAIL
)
902 /* Get the absolute path of the patchfile, changing directory below. */
903 fullname
= FullName_save(eap
->arg
, FALSE
);
905 buf
= alloc((unsigned)(STRLEN(tmp_orig
) + (
907 fullname
!= NULL
? STRLEN(fullname
) :
909 STRLEN(eap
->arg
)) + STRLEN(tmp_new
) + 16));
914 /* Temporaraly chdir to /tmp, to avoid patching files in the current
915 * directory when the patch file contains more than one patch. When we
916 * have our own temp dir use that instead, it will be cleaned up when we
917 * exit (any .rej files created). Don't change directory if we can't
918 * return to the current. */
919 if (mch_dirname(dirbuf
, MAXPATHL
) != OK
|| mch_chdir((char *)dirbuf
) != 0)
924 if (vim_tempdir
!= NULL
)
925 mch_chdir((char *)vim_tempdir
);
929 shorten_fnames(TRUE
);
935 /* Use 'patchexpr' to generate the new file. */
938 fullname
!= NULL
? fullname
:
944 /* Build the patch command and execute it. Ignore errors. Switch to
945 * cooked mode to allow the user to respond to prompts. */
946 sprintf((char *)buf
, "patch -o %s %s < \"%s\"", tmp_new
, tmp_orig
,
948 fullname
!= NULL
? fullname
:
952 ++autocmd_block
; /* Avoid ShellCmdPost stuff */
954 (void)call_shell(buf
, SHELL_FILTER
| SHELL_COOKED
);
961 if (dirbuf
[0] != NUL
)
963 if (mch_chdir((char *)dirbuf
) != 0)
965 shorten_fnames(TRUE
);
969 /* patch probably has written over the screen */
972 /* Delete any .orig or .rej file created. */
973 STRCPY(buf
, tmp_new
);
974 STRCAT(buf
, ".orig");
976 STRCPY(buf
, tmp_new
);
980 if (curbuf
->b_fname
!= NULL
)
982 newname
= vim_strnsave(curbuf
->b_fname
,
983 (int)(STRLEN(curbuf
->b_fname
) + 4));
985 STRCAT(newname
, ".new");
989 need_mouse_correct
= TRUE
;
991 /* don't use a new tab page, each tab page has its own diffs */
994 if (win_split(0, (diff_flags
& DIFF_VERTICAL
) ? WSP_VERT
: 0) != FAIL
)
996 /* Pretend it was a ":split fname" command */
997 eap
->cmdidx
= CMD_split
;
999 do_exedit(eap
, old_curwin
);
1001 if (curwin
!= old_curwin
) /* split must have worked */
1003 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1004 diff_win_options(curwin
, TRUE
);
1005 diff_win_options(old_curwin
, TRUE
);
1007 if (newname
!= NULL
)
1009 /* do a ":file filename.new" on the patched buffer */
1014 /* Do filetype detection with the new name. */
1015 if (au_has_group((char_u
*)"filetypedetect"))
1016 do_cmdline_cmd((char_u
*)":doau filetypedetect BufRead");
1023 if (tmp_orig
!= NULL
)
1024 mch_remove(tmp_orig
);
1026 if (tmp_new
!= NULL
)
1027 mch_remove(tmp_new
);
1035 vim_free(browseFile
);
1036 cmdmod
.browse
= browse_flag
;
1041 * Split the window and edit another file, setting options to show the diffs.
1047 win_T
*old_curwin
= curwin
;
1050 need_mouse_correct
= TRUE
;
1052 /* don't use a new tab page, each tab page has its own diffs */
1055 if (win_split(0, (diff_flags
& DIFF_VERTICAL
) ? WSP_VERT
: 0) != FAIL
)
1057 /* Pretend it was a ":split fname" command */
1058 eap
->cmdidx
= CMD_split
;
1059 curwin
->w_p_diff
= TRUE
;
1060 do_exedit(eap
, old_curwin
);
1062 if (curwin
!= old_curwin
) /* split must have worked */
1064 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1065 diff_win_options(curwin
, TRUE
);
1066 diff_win_options(old_curwin
, TRUE
);
1072 * Set options to show difs for the current window.
1079 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1080 diff_win_options(curwin
, TRUE
);
1084 * Set options in window "wp" for diff mode.
1087 diff_win_options(wp
, addbuf
)
1089 int addbuf
; /* Add buffer to diff. */
1091 wp
->w_p_diff
= TRUE
;
1093 wp
->w_p_wrap
= FALSE
;
1094 # ifdef FEAT_FOLDING
1096 win_T
*old_curwin
= curwin
;
1099 curbuf
= curwin
->w_buffer
;
1100 set_string_option_direct((char_u
*)"fdm", -1, (char_u
*)"diff",
1101 OPT_LOCAL
|OPT_FREE
, 0);
1102 curwin
= old_curwin
;
1103 curbuf
= curwin
->w_buffer
;
1104 wp
->w_p_fdc
= diff_foldcolumn
;
1108 /* make sure topline is not halfway a fold */
1109 changed_window_setting_win(wp
);
1112 #ifdef FEAT_SCROLLBIND
1113 if (vim_strchr(p_sbo
, 'h') == NULL
)
1114 do_cmdline_cmd((char_u
*)"set sbo+=hor");
1118 diff_buf_add(wp
->w_buffer
);
1119 redraw_win_later(wp
, NOT_VALID
);
1123 * Set options not to show diffs. For the current window or all windows.
1124 * Only in the current tab page.
1131 win_T
*old_curwin
= curwin
;
1132 #ifdef FEAT_SCROLLBIND
1133 int diffwin
= FALSE
;
1136 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
1138 if (wp
== curwin
|| eap
->forceit
)
1140 /* Set 'diff', 'scrollbind' off and 'wrap' on. */
1141 wp
->w_p_diff
= FALSE
;
1142 wp
->w_p_scb
= FALSE
;
1143 wp
->w_p_wrap
= TRUE
;
1146 curbuf
= curwin
->w_buffer
;
1147 set_string_option_direct((char_u
*)"fdm", -1,
1148 (char_u
*)"manual", OPT_LOCAL
|OPT_FREE
, 0);
1149 curwin
= old_curwin
;
1150 curbuf
= curwin
->w_buffer
;
1152 wp
->w_p_fen
= FALSE
;
1155 /* make sure topline is not halfway a fold */
1156 changed_window_setting_win(wp
);
1158 diff_buf_adjust(wp
);
1160 #ifdef FEAT_SCROLLBIND
1161 diffwin
|= wp
->w_p_diff
;
1165 #ifdef FEAT_SCROLLBIND
1166 /* Remove "hor" from from 'scrollopt' if there are no diff windows left. */
1167 if (!diffwin
&& vim_strchr(p_sbo
, 'h') != NULL
)
1168 do_cmdline_cmd((char_u
*)"set sbo-=hor");
1173 * Read the diff output and add each entry to the diff list.
1176 diff_read(idx_orig
, idx_new
, fname
)
1177 int idx_orig
; /* idx of original file */
1178 int idx_new
; /* idx of new file */
1179 char_u
*fname
; /* name of diff output file */
1182 diff_T
*dprev
= NULL
;
1183 diff_T
*dp
= curtab
->tp_first_diff
;
1185 long f1
, l1
, f2
, l2
;
1186 char_u linebuf
[LBUFLEN
]; /* only need to hold the diff line */
1191 linenr_T lnum_orig
, lnum_new
;
1192 long count_orig
, count_new
;
1193 int notset
= TRUE
; /* block "*dp" not set yet */
1195 fd
= mch_fopen((char *)fname
, "r");
1198 EMSG(_("E98: Cannot read diff output"));
1204 if (tag_fgets(linebuf
, LBUFLEN
, fd
))
1205 break; /* end of file */
1206 if (!isdigit(*linebuf
))
1207 continue; /* not the start of a diff block */
1209 /* This line must be one of three formats:
1210 * {first}[,{last}]c{first}[,{last}]
1211 * {first}a{first}[,{last}]
1212 * {first}[,{last}]d{first}
1223 if (*p
!= 'a' && *p
!= 'c' && *p
!= 'd')
1224 continue; /* invalid diff format */
1234 if (l1
< f1
|| l2
< f2
)
1235 continue; /* invalid line range */
1237 if (difftype
== 'a')
1245 count_orig
= l1
- f1
+ 1;
1247 if (difftype
== 'd')
1255 count_new
= l2
- f2
+ 1;
1258 /* Go over blocks before the change, for which orig and new are equal.
1259 * Copy blocks from orig to new. */
1261 && lnum_orig
> dp
->df_lnum
[idx_orig
] + dp
->df_count
[idx_orig
])
1264 diff_copy_entry(dprev
, dp
, idx_orig
, idx_new
);
1271 && lnum_orig
<= dp
->df_lnum
[idx_orig
] + dp
->df_count
[idx_orig
]
1272 && lnum_orig
+ count_orig
>= dp
->df_lnum
[idx_orig
])
1274 /* New block overlaps with existing block(s).
1275 * First find last block that overlaps. */
1276 for (dpl
= dp
; dpl
->df_next
!= NULL
; dpl
= dpl
->df_next
)
1277 if (lnum_orig
+ count_orig
< dpl
->df_next
->df_lnum
[idx_orig
])
1280 /* If the newly found block starts before the old one, set the
1281 * start back a number of lines. */
1282 off
= dp
->df_lnum
[idx_orig
] - lnum_orig
;
1285 for (i
= idx_orig
; i
< idx_new
; ++i
)
1286 if (curtab
->tp_diffbuf
[i
] != NULL
)
1287 dp
->df_lnum
[i
] -= off
;
1288 dp
->df_lnum
[idx_new
] = lnum_new
;
1289 dp
->df_count
[idx_new
] = count_new
;
1293 /* new block inside existing one, adjust new block */
1294 dp
->df_lnum
[idx_new
] = lnum_new
+ off
;
1295 dp
->df_count
[idx_new
] = count_new
- off
;
1298 /* second overlap of new block with existing block */
1299 dp
->df_count
[idx_new
] += count_new
- count_orig
;
1301 /* Adjust the size of the block to include all the lines to the
1302 * end of the existing block or the new diff, whatever ends last. */
1303 off
= (lnum_orig
+ count_orig
)
1304 - (dpl
->df_lnum
[idx_orig
] + dpl
->df_count
[idx_orig
]);
1307 /* new change ends in existing block, adjust the end if not
1310 dp
->df_count
[idx_new
] += -off
;
1313 for (i
= idx_orig
; i
< idx_new
+ !notset
; ++i
)
1314 if (curtab
->tp_diffbuf
[i
] != NULL
)
1315 dp
->df_count
[i
] = dpl
->df_lnum
[i
] + dpl
->df_count
[i
]
1316 - dp
->df_lnum
[i
] + off
;
1318 /* Delete the diff blocks that have been merged into one. */
1320 dp
->df_next
= dpl
->df_next
;
1321 while (dn
!= dp
->df_next
)
1330 /* Allocate a new diffblock. */
1331 dp
= diff_alloc_new(curtab
, dprev
, dp
);
1335 dp
->df_lnum
[idx_orig
] = lnum_orig
;
1336 dp
->df_count
[idx_orig
] = count_orig
;
1337 dp
->df_lnum
[idx_new
] = lnum_new
;
1338 dp
->df_count
[idx_new
] = count_new
;
1340 /* Set values for other buffers, these must be equal to the
1341 * original buffer, otherwise there would have been a change
1343 for (i
= idx_orig
+ 1; i
< idx_new
; ++i
)
1344 if (curtab
->tp_diffbuf
[i
] != NULL
)
1345 diff_copy_entry(dprev
, dp
, idx_orig
, i
);
1347 notset
= FALSE
; /* "*dp" has been set */
1350 /* for remaining diff blocks orig and new are equal */
1354 diff_copy_entry(dprev
, dp
, idx_orig
, idx_new
);
1365 * Copy an entry at "dp" from "idx_orig" to "idx_new".
1368 diff_copy_entry(dprev
, dp
, idx_orig
, idx_new
)
1379 off
= (dprev
->df_lnum
[idx_orig
] + dprev
->df_count
[idx_orig
])
1380 - (dprev
->df_lnum
[idx_new
] + dprev
->df_count
[idx_new
]);
1381 dp
->df_lnum
[idx_new
] = dp
->df_lnum
[idx_orig
] - off
;
1382 dp
->df_count
[idx_new
] = dp
->df_count
[idx_orig
];
1386 * Clear the list of diffblocks for tab page "tp".
1394 for (p
= tp
->tp_first_diff
; p
!= NULL
; p
= next_p
)
1396 next_p
= p
->df_next
;
1399 tp
->tp_first_diff
= NULL
;
1403 * Check diff status for line "lnum" in buffer "buf":
1404 * Returns 0 for nothing special
1405 * Returns -1 for a line that should be highlighted as changed.
1406 * Returns -2 for a line that should be highlighted as added/deleted.
1407 * Returns > 0 for inserting that many filler lines above it (never happens
1408 * when 'diffopt' doesn't contain "filler").
1409 * This should only be used for windows where 'diff' is set.
1412 diff_check(wp
, lnum
)
1416 int idx
; /* index in tp_diffbuf[] for this buffer */
1420 buf_T
*buf
= wp
->w_buffer
;
1423 if (curtab
->tp_diff_invalid
)
1424 ex_diffupdate(NULL
); /* update after a big change */
1426 if (curtab
->tp_first_diff
== NULL
|| !wp
->w_p_diff
) /* no diffs at all */
1429 /* safety check: "lnum" must be a buffer line */
1430 if (lnum
< 1 || lnum
> buf
->b_ml
.ml_line_count
+ 1)
1433 idx
= diff_buf_idx(buf
);
1434 if (idx
== DB_COUNT
)
1435 return 0; /* no diffs for buffer "buf" */
1438 /* A closed fold never has filler lines. */
1439 if (hasFoldingWin(wp
, lnum
, NULL
, NULL
, TRUE
, NULL
))
1443 /* search for a change that includes "lnum" in the list of diffblocks. */
1444 for (dp
= curtab
->tp_first_diff
; dp
!= NULL
; dp
= dp
->df_next
)
1445 if (lnum
<= dp
->df_lnum
[idx
] + dp
->df_count
[idx
])
1447 if (dp
== NULL
|| lnum
< dp
->df_lnum
[idx
])
1450 if (lnum
< dp
->df_lnum
[idx
] + dp
->df_count
[idx
])
1454 /* Changed or inserted line. If the other buffers have a count of
1455 * zero, the lines were inserted. If the other buffers have the same
1456 * count, check if the lines are identical. */
1458 for (i
= 0; i
< DB_COUNT
; ++i
)
1459 if (i
!= idx
&& curtab
->tp_diffbuf
[i
] != NULL
)
1461 if (dp
->df_count
[i
] == 0)
1465 if (dp
->df_count
[i
] != dp
->df_count
[idx
])
1466 return -1; /* nr of lines changed. */
1472 /* Compare all lines. If they are equal the lines were inserted
1473 * in some buffers, deleted in others, but not changed. */
1474 for (i
= 0; i
< DB_COUNT
; ++i
)
1475 if (i
!= idx
&& curtab
->tp_diffbuf
[i
] != NULL
&& dp
->df_count
[i
] != 0)
1476 if (!diff_equal_entry(dp
, idx
, i
))
1479 /* If there is no buffer with zero lines then there is no difference
1480 * any longer. Happens when making a change (or undo) that removes
1481 * the difference. Can't remove the entry here, we might be halfway
1482 * updating the window. Just report the text as unchanged. Other
1483 * windows might still show the change though. */
1489 /* If 'diffopt' doesn't contain "filler", return 0. */
1490 if (!(diff_flags
& DIFF_FILLER
))
1493 /* Insert filler lines above the line just below the change. Will return
1494 * 0 when this buf had the max count. */
1496 for (i
= 0; i
< DB_COUNT
; ++i
)
1497 if (curtab
->tp_diffbuf
[i
] != NULL
&& dp
->df_count
[i
] > maxcount
)
1498 maxcount
= dp
->df_count
[i
];
1499 return maxcount
- dp
->df_count
[idx
];
1503 * Compare two entries in diff "*dp" and return TRUE if they are equal.
1506 diff_equal_entry(dp
, idx1
, idx2
)
1515 if (dp
->df_count
[idx1
] != dp
->df_count
[idx2
])
1517 if (diff_check_sanity(curtab
, dp
) == FAIL
)
1519 for (i
= 0; i
< dp
->df_count
[idx1
]; ++i
)
1521 line
= vim_strsave(ml_get_buf(curtab
->tp_diffbuf
[idx1
],
1522 dp
->df_lnum
[idx1
] + i
, FALSE
));
1525 cmp
= diff_cmp(line
, ml_get_buf(curtab
->tp_diffbuf
[idx2
],
1526 dp
->df_lnum
[idx2
] + i
, FALSE
));
1535 * Compare strings "s1" and "s2" according to 'diffopt'.
1536 * Return non-zero when they are different.
1548 if ((diff_flags
& (DIFF_ICASE
| DIFF_IWHITE
)) == 0)
1549 return STRCMP(s1
, s2
);
1550 if ((diff_flags
& DIFF_ICASE
) && !(diff_flags
& DIFF_IWHITE
))
1551 return MB_STRICMP(s1
, s2
);
1553 /* Ignore white space changes and possibly ignore case. */
1556 while (*p1
!= NUL
&& *p2
!= NUL
)
1558 if (vim_iswhite(*p1
) && vim_iswhite(*p2
))
1566 l
= (*mb_ptr2len
)(p1
);
1567 if (l
!= (*mb_ptr2len
)(p2
))
1571 if (STRNCMP(p1
, p2
, l
) != 0
1573 || !(diff_flags
& DIFF_ICASE
)
1574 || utf_fold(utf_ptr2char(p1
))
1575 != utf_fold(utf_ptr2char(p2
))))
1583 if (*p1
!= *p2
&& (!(diff_flags
& DIFF_ICASE
)
1584 || TOLOWER_LOC(*p1
) != TOLOWER_LOC(*p2
)))
1592 /* Ignore trailing white space. */
1595 if (*p1
!= NUL
|| *p2
!= NUL
)
1601 * Return the number of filler lines above "lnum".
1604 diff_check_fill(wp
, lnum
)
1610 /* be quick when there are no filler lines */
1611 if (!(diff_flags
& DIFF_FILLER
))
1613 n
= diff_check(wp
, lnum
);
1620 * Set the topline of "towin" to match the position in "fromwin", so that they
1621 * show the same diff'ed lines.
1624 diff_set_topline(fromwin
, towin
)
1628 buf_T
*buf
= fromwin
->w_buffer
;
1629 linenr_T lnum
= fromwin
->w_topline
;
1634 idx
= diff_buf_idx(buf
);
1635 if (idx
== DB_COUNT
)
1636 return; /* safety check */
1638 if (curtab
->tp_diff_invalid
)
1639 ex_diffupdate(NULL
); /* update after a big change */
1641 towin
->w_topfill
= 0;
1643 /* search for a change that includes "lnum" in the list of diffblocks. */
1644 for (dp
= curtab
->tp_first_diff
; dp
!= NULL
; dp
= dp
->df_next
)
1645 if (lnum
<= dp
->df_lnum
[idx
] + dp
->df_count
[idx
])
1649 /* After last change, compute topline relative to end of file; no
1651 towin
->w_topline
= towin
->w_buffer
->b_ml
.ml_line_count
1652 - (buf
->b_ml
.ml_line_count
- lnum
);
1656 /* Find index for "towin". */
1657 i
= diff_buf_idx(towin
->w_buffer
);
1659 return; /* safety check */
1661 towin
->w_topline
= lnum
+ (dp
->df_lnum
[i
] - dp
->df_lnum
[idx
]);
1662 if (lnum
>= dp
->df_lnum
[idx
])
1664 /* Inside a change: compute filler lines. */
1665 if (dp
->df_count
[i
] == dp
->df_count
[idx
])
1666 towin
->w_topfill
= fromwin
->w_topfill
;
1667 else if (dp
->df_count
[i
] > dp
->df_count
[idx
])
1669 if (lnum
== dp
->df_lnum
[idx
] + dp
->df_count
[idx
])
1670 towin
->w_topline
= dp
->df_lnum
[i
] + dp
->df_count
[i
]
1671 - fromwin
->w_topfill
;
1675 if (towin
->w_topline
>= dp
->df_lnum
[i
] + dp
->df_count
[i
])
1677 if (diff_flags
& DIFF_FILLER
)
1678 towin
->w_topfill
= dp
->df_lnum
[idx
]
1679 + dp
->df_count
[idx
] - lnum
;
1680 towin
->w_topline
= dp
->df_lnum
[i
] + dp
->df_count
[i
];
1686 /* safety check (if diff info gets outdated strange things may happen) */
1687 towin
->w_botfill
= FALSE
;
1688 if (towin
->w_topline
> towin
->w_buffer
->b_ml
.ml_line_count
)
1690 towin
->w_topline
= towin
->w_buffer
->b_ml
.ml_line_count
;
1691 towin
->w_botfill
= TRUE
;
1693 if (towin
->w_topline
< 1)
1695 towin
->w_topline
= 1;
1696 towin
->w_topfill
= 0;
1699 /* When w_topline changes need to recompute w_botline and cursor position */
1700 invalidate_botline_win(towin
);
1701 changed_line_abv_curs_win(towin
);
1703 check_topfill(towin
, FALSE
);
1705 (void)hasFoldingWin(towin
, towin
->w_topline
, &towin
->w_topline
,
1711 * This is called when 'diffopt' is changed.
1717 int diff_context_new
= 6;
1718 int diff_flags_new
= 0;
1719 int diff_foldcolumn_new
= 2;
1725 if (STRNCMP(p
, "filler", 6) == 0)
1728 diff_flags_new
|= DIFF_FILLER
;
1730 else if (STRNCMP(p
, "context:", 8) == 0 && VIM_ISDIGIT(p
[8]))
1733 diff_context_new
= getdigits(&p
);
1735 else if (STRNCMP(p
, "icase", 5) == 0)
1738 diff_flags_new
|= DIFF_ICASE
;
1740 else if (STRNCMP(p
, "iwhite", 6) == 0)
1743 diff_flags_new
|= DIFF_IWHITE
;
1745 else if (STRNCMP(p
, "horizontal", 10) == 0)
1748 diff_flags_new
|= DIFF_HORIZONTAL
;
1750 else if (STRNCMP(p
, "vertical", 8) == 0)
1753 diff_flags_new
|= DIFF_VERTICAL
;
1755 else if (STRNCMP(p
, "foldcolumn:", 11) == 0 && VIM_ISDIGIT(p
[11]))
1758 diff_foldcolumn_new
= getdigits(&p
);
1760 if (*p
!= ',' && *p
!= NUL
)
1766 /* Can't have both "horizontal" and "vertical". */
1767 if ((diff_flags_new
& DIFF_HORIZONTAL
) && (diff_flags_new
& DIFF_VERTICAL
))
1770 /* If "icase" or "iwhite" was added or removed, need to update the diff. */
1771 if (diff_flags
!= diff_flags_new
)
1772 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
1773 tp
->tp_diff_invalid
= TRUE
;
1775 diff_flags
= diff_flags_new
;
1776 diff_context
= diff_context_new
;
1777 diff_foldcolumn
= diff_foldcolumn_new
;
1781 /* recompute the scroll binding with the new option value, may
1782 * remove or add filler lines */
1783 check_scrollbind((linenr_T
)0, 0L);
1789 * Return TRUE if 'diffopt' contains "horizontal".
1792 diffopt_horizontal()
1794 return (diff_flags
& DIFF_HORIZONTAL
) != 0;
1798 * Find the difference within a changed line.
1799 * Returns TRUE if the line was added, no other buffer has it.
1802 diff_find_change(wp
, lnum
, startp
, endp
)
1805 int *startp
; /* first char of the change */
1806 int *endp
; /* last char of the change */
1818 /* Make a copy of the line, the next ml_get() will invalidate it. */
1819 line_org
= vim_strsave(ml_get_buf(wp
->w_buffer
, lnum
, FALSE
));
1820 if (line_org
== NULL
)
1823 idx
= diff_buf_idx(wp
->w_buffer
);
1824 if (idx
== DB_COUNT
) /* cannot happen */
1830 /* search for a change that includes "lnum" in the list of diffblocks. */
1831 for (dp
= curtab
->tp_first_diff
; dp
!= NULL
; dp
= dp
->df_next
)
1832 if (lnum
<= dp
->df_lnum
[idx
] + dp
->df_count
[idx
])
1834 if (dp
== NULL
|| diff_check_sanity(curtab
, dp
) == FAIL
)
1840 off
= lnum
- dp
->df_lnum
[idx
];
1842 for (i
= 0; i
< DB_COUNT
; ++i
)
1843 if (curtab
->tp_diffbuf
[i
] != NULL
&& i
!= idx
)
1845 /* Skip lines that are not in the other change (filler lines). */
1846 if (off
>= dp
->df_count
[i
])
1849 line_new
= ml_get_buf(curtab
->tp_diffbuf
[i
],
1850 dp
->df_lnum
[i
] + off
, FALSE
);
1852 /* Search for start of difference */
1853 si_org
= si_new
= 0;
1854 while (line_org
[si_org
] != NUL
)
1856 if ((diff_flags
& DIFF_IWHITE
)
1857 && vim_iswhite(line_org
[si_org
])
1858 && vim_iswhite(line_new
[si_new
]))
1860 si_org
= (int)(skipwhite(line_org
+ si_org
) - line_org
);
1861 si_new
= (int)(skipwhite(line_new
+ si_new
) - line_new
);
1865 if (line_org
[si_org
] != line_new
[si_new
])
1874 /* Move back to first byte of character in both lines (may
1875 * have "nn^" in line_org and "n^ in line_new). */
1876 si_org
-= (*mb_head_off
)(line_org
, line_org
+ si_org
);
1877 si_new
-= (*mb_head_off
)(line_new
, line_new
+ si_new
);
1880 if (*startp
> si_org
)
1883 /* Search for end of difference, if any. */
1884 if (line_org
[si_org
] != NUL
|| line_new
[si_new
] != NUL
)
1886 ei_org
= (int)STRLEN(line_org
);
1887 ei_new
= (int)STRLEN(line_new
);
1888 while (ei_org
>= *startp
&& ei_new
>= si_new
1889 && ei_org
>= 0 && ei_new
>= 0)
1891 if ((diff_flags
& DIFF_IWHITE
)
1892 && vim_iswhite(line_org
[ei_org
])
1893 && vim_iswhite(line_new
[ei_new
]))
1895 while (ei_org
>= *startp
1896 && vim_iswhite(line_org
[ei_org
]))
1898 while (ei_new
>= si_new
1899 && vim_iswhite(line_new
[ei_new
]))
1904 if (line_org
[ei_org
] != line_new
[ei_new
])
1919 #if defined(FEAT_FOLDING) || defined(PROTO)
1921 * Return TRUE if line "lnum" is not close to a diff block, this line should
1923 * Return FALSE if there are no diff blocks at all in this window.
1926 diff_infold(wp
, lnum
)
1935 /* Return if 'diff' isn't set. */
1939 for (i
= 0; i
< DB_COUNT
; ++i
)
1941 if (curtab
->tp_diffbuf
[i
] == wp
->w_buffer
)
1943 else if (curtab
->tp_diffbuf
[i
] != NULL
)
1947 /* return here if there are no diffs in the window */
1948 if (idx
== -1 || !other
)
1951 if (curtab
->tp_diff_invalid
)
1952 ex_diffupdate(NULL
); /* update after a big change */
1954 /* Return if there are no diff blocks. All lines will be folded. */
1955 if (curtab
->tp_first_diff
== NULL
)
1958 for (dp
= curtab
->tp_first_diff
; dp
!= NULL
; dp
= dp
->df_next
)
1960 /* If this change is below the line there can't be any further match. */
1961 if (dp
->df_lnum
[idx
] - diff_context
> lnum
)
1963 /* If this change ends before the line we have a match. */
1964 if (dp
->df_lnum
[idx
] + dp
->df_count
[idx
] + diff_context
> lnum
)
1972 * "dp" and "do" commands.
1980 ea
.arg
= (char_u
*)"";
1982 ea
.cmdidx
= CMD_diffput
;
1984 ea
.cmdidx
= CMD_diffget
;
1986 ea
.line1
= curwin
->w_cursor
.lnum
;
1987 ea
.line2
= curwin
->w_cursor
.lnum
;
2014 int start_skip
, end_skip
;
2018 /* Find the current buffer in the list of diff buffers. */
2019 idx_cur
= diff_buf_idx(curbuf
);
2020 if (idx_cur
== DB_COUNT
)
2022 EMSG(_("E99: Current buffer is not in diff mode"));
2026 if (*eap
->arg
== NUL
)
2028 /* No argument: Find the other buffer in the list of diff buffers. */
2029 for (idx_other
= 0; idx_other
< DB_COUNT
; ++idx_other
)
2030 if (curtab
->tp_diffbuf
[idx_other
] != curbuf
2031 && curtab
->tp_diffbuf
[idx_other
] != NULL
2032 && (eap
->cmdidx
!= CMD_diffput
2033 || curtab
->tp_diffbuf
[idx_other
]->b_p_ma
))
2035 if (idx_other
== DB_COUNT
)
2037 EMSG(_("E100: No other buffer in diff mode"));
2041 /* Check that there isn't a third buffer in the list */
2042 for (i
= idx_other
+ 1; i
< DB_COUNT
; ++i
)
2043 if (curtab
->tp_diffbuf
[i
] != curbuf
2044 && curtab
->tp_diffbuf
[i
] != NULL
2045 && (eap
->cmdidx
!= CMD_diffput
|| curtab
->tp_diffbuf
[i
]->b_p_ma
))
2047 EMSG(_("E101: More than two buffers in diff mode, don't know which one to use"));
2053 /* Buffer number or pattern given. Ignore trailing white space. */
2054 p
= eap
->arg
+ STRLEN(eap
->arg
);
2055 while (p
> eap
->arg
&& vim_iswhite(p
[-1]))
2057 for (i
= 0; vim_isdigit(eap
->arg
[i
]) && eap
->arg
+ i
< p
; ++i
)
2059 if (eap
->arg
+ i
== p
) /* digits only */
2060 i
= atol((char *)eap
->arg
);
2063 i
= buflist_findpat(eap
->arg
, p
, FALSE
, TRUE
);
2065 return; /* error message already given */
2067 buf
= buflist_findnr(i
);
2070 EMSG2(_("E102: Can't find buffer \"%s\""), eap
->arg
);
2073 idx_other
= diff_buf_idx(buf
);
2074 if (idx_other
== DB_COUNT
)
2076 EMSG2(_("E103: Buffer \"%s\" is not in diff mode"), eap
->arg
);
2083 /* When no range given include the line above or below the cursor. */
2084 if (eap
->addr_count
== 0)
2086 /* Make it possible that ":diffget" on the last line gets line below
2087 * the cursor line when there is no difference above the cursor. */
2088 if (eap
->cmdidx
== CMD_diffget
2089 && eap
->line1
== curbuf
->b_ml
.ml_line_count
2090 && diff_check(curwin
, eap
->line1
) == 0
2091 && (eap
->line1
== 1 || diff_check(curwin
, eap
->line1
- 1) == 0))
2093 else if (eap
->line1
> 0)
2097 if (eap
->cmdidx
== CMD_diffget
)
2099 idx_from
= idx_other
;
2106 /* Need to make the other buffer the current buffer to be able to make
2108 /* set curwin/curbuf to buf and save a few things */
2109 aucmd_prepbuf(&aco
, curtab
->tp_diffbuf
[idx_other
]);
2112 /* May give the warning for a changed buffer here, which can trigger the
2113 * FileChangedRO autocommand, which may do nasty things and mess
2115 if (!curbuf
->b_changed
)
2118 if (diff_buf_idx(curbuf
) != idx_to
)
2120 EMSG(_("E787: Buffer changed unexpectedly"));
2126 for (dp
= curtab
->tp_first_diff
; dp
!= NULL
; )
2128 if (dp
->df_lnum
[idx_cur
] > eap
->line2
+ off
)
2129 break; /* past the range that was specified */
2132 lnum
= dp
->df_lnum
[idx_to
];
2133 count
= dp
->df_count
[idx_to
];
2134 if (dp
->df_lnum
[idx_cur
] + dp
->df_count
[idx_cur
] > eap
->line1
+ off
2135 && u_save(lnum
- 1, lnum
+ count
) != FAIL
)
2137 /* Inside the specified range and saving for undo worked. */
2140 if (eap
->addr_count
> 0)
2142 /* A range was specified: check if lines need to be skipped. */
2143 start_skip
= eap
->line1
+ off
- dp
->df_lnum
[idx_cur
];
2146 /* range starts below start of current diff block */
2147 if (start_skip
> count
)
2154 count
-= start_skip
;
2161 end_skip
= dp
->df_lnum
[idx_cur
] + dp
->df_count
[idx_cur
] - 1
2162 - (eap
->line2
+ off
);
2165 /* range ends above end of current/from diff block */
2166 if (idx_cur
== idx_from
) /* :diffput */
2168 i
= dp
->df_count
[idx_cur
] - start_skip
- end_skip
;
2175 end_skip
= dp
->df_count
[idx_from
] - start_skip
- count
;
2186 for (i
= 0; i
< count
; ++i
)
2188 /* remember deleting the last line of the buffer */
2189 buf_empty
= curbuf
->b_ml
.ml_line_count
== 1;
2190 ml_delete(lnum
, FALSE
);
2193 for (i
= 0; i
< dp
->df_count
[idx_from
] - start_skip
- end_skip
; ++i
)
2197 nr
= dp
->df_lnum
[idx_from
] + start_skip
+ i
;
2198 if (nr
> curtab
->tp_diffbuf
[idx_from
]->b_ml
.ml_line_count
)
2200 p
= vim_strsave(ml_get_buf(curtab
->tp_diffbuf
[idx_from
],
2204 ml_append(lnum
+ i
- 1, p
, 0, FALSE
);
2207 if (buf_empty
&& curbuf
->b_ml
.ml_line_count
== 2)
2209 /* Added the first line into an empty buffer, need to
2210 * delete the dummy empty line. */
2212 ml_delete((linenr_T
)2, FALSE
);
2216 new_count
= dp
->df_count
[idx_to
] + added
;
2217 dp
->df_count
[idx_to
] = new_count
;
2219 if (start_skip
== 0 && end_skip
== 0)
2221 /* Check if there are any other buffers and if the diff is
2223 for (i
= 0; i
< DB_COUNT
; ++i
)
2224 if (curtab
->tp_diffbuf
[i
] != NULL
&& i
!= idx_from
2226 && !diff_equal_entry(dp
, idx_from
, i
))
2230 /* delete the diff entry, the buffers are now equal here */
2234 curtab
->tp_first_diff
= dp
;
2236 dprev
->df_next
= dp
;
2240 /* Adjust marks. This will change the following entries! */
2243 mark_adjust(lnum
, lnum
+ count
- 1, (long)MAXLNUM
, (long)added
);
2244 if (curwin
->w_cursor
.lnum
>= lnum
)
2246 /* Adjust the cursor position if it's in/after the changed
2248 if (curwin
->w_cursor
.lnum
>= lnum
+ count
)
2249 curwin
->w_cursor
.lnum
+= added
;
2251 curwin
->w_cursor
.lnum
= lnum
;
2254 changed_lines(lnum
, 0, lnum
+ count
, (long)added
);
2258 /* Diff is deleted, update folds in other windows. */
2260 diff_fold_update(dfree
, idx_to
);
2265 /* mark_adjust() may have changed the count in a wrong way */
2266 dp
->df_count
[idx_to
] = new_count
;
2268 /* When changing the current buffer, keep track of line numbers */
2269 if (idx_cur
== idx_to
)
2273 /* If before the range or not deleted, go to next diff. */
2281 /* restore curwin/curbuf and a few other things */
2282 if (idx_other
== idx_to
)
2284 /* Syncing undo only works for the current buffer, but we change
2285 * another buffer. Sync undo if the command was typed. This isn't
2286 * 100% right when ":diffput" is used in a function or mapping. */
2289 aucmd_restbuf(&aco
);
2294 /* Check that the cursor is on a valid character and update it's position.
2295 * When there were filler lines the topline has become invalid. */
2297 changed_line_abv_curs();
2299 /* Also need to redraw the other buffers. */
2305 * Update folds for all diff buffers for entry "dp".
2306 * Skip buffer with index "skip_idx".
2307 * When there are no diffs, all folds are removed.
2310 diff_fold_update(dp
, skip_idx
)
2317 for (wp
= firstwin
; wp
!= NULL
; wp
= wp
->w_next
)
2318 for (i
= 0; i
< DB_COUNT
; ++i
)
2319 if (curtab
->tp_diffbuf
[i
] == wp
->w_buffer
&& i
!= skip_idx
)
2320 foldUpdate(wp
, dp
->df_lnum
[i
],
2321 dp
->df_lnum
[i
] + dp
->df_count
[i
]);
2326 * Return TRUE if buffer "buf" is in diff-mode.
2334 for (tp
= first_tabpage
; tp
!= NULL
; tp
= tp
->tp_next
)
2335 if (diff_buf_idx_tp(buf
, tp
) != DB_COUNT
)
2341 * Move "count" times in direction "dir" to the next diff block.
2342 * Return FAIL if there isn't such a diff block.
2345 diff_move_to(dir
, count
)
2350 linenr_T lnum
= curwin
->w_cursor
.lnum
;
2353 idx
= diff_buf_idx(curbuf
);
2354 if (idx
== DB_COUNT
|| curtab
->tp_first_diff
== NULL
)
2357 if (curtab
->tp_diff_invalid
)
2358 ex_diffupdate(NULL
); /* update after a big change */
2360 if (curtab
->tp_first_diff
== NULL
) /* no diffs today */
2363 while (--count
>= 0)
2365 /* Check if already before first diff. */
2366 if (dir
== BACKWARD
&& lnum
<= curtab
->tp_first_diff
->df_lnum
[idx
])
2369 for (dp
= curtab
->tp_first_diff
; ; dp
= dp
->df_next
)
2373 if ((dir
== FORWARD
&& lnum
< dp
->df_lnum
[idx
])
2375 && (dp
->df_next
== NULL
2376 || lnum
<= dp
->df_next
->df_lnum
[idx
])))
2378 lnum
= dp
->df_lnum
[idx
];
2384 /* don't end up past the end of the file */
2385 if (lnum
> curbuf
->b_ml
.ml_line_count
)
2386 lnum
= curbuf
->b_ml
.ml_line_count
;
2388 /* When the cursor didn't move at all we fail. */
2389 if (lnum
== curwin
->w_cursor
.lnum
)
2393 curwin
->w_cursor
.lnum
= lnum
;
2394 curwin
->w_cursor
.col
= 0;
2399 #if defined(FEAT_FOLDING) || defined(PROTO)
2401 * For line "lnum" in the current window find the equivalent lnum in window
2402 * "wp", compensating for inserted/deleted lines.
2405 diff_lnum_win(lnum
, wp
)
2414 idx
= diff_buf_idx(curbuf
);
2415 if (idx
== DB_COUNT
) /* safety check */
2418 if (curtab
->tp_diff_invalid
)
2419 ex_diffupdate(NULL
); /* update after a big change */
2421 /* search for a change that includes "lnum" in the list of diffblocks. */
2422 for (dp
= curtab
->tp_first_diff
; dp
!= NULL
; dp
= dp
->df_next
)
2423 if (lnum
<= dp
->df_lnum
[idx
] + dp
->df_count
[idx
])
2426 /* When after the last change, compute relative to the last line number. */
2428 return wp
->w_buffer
->b_ml
.ml_line_count
2429 - (curbuf
->b_ml
.ml_line_count
- lnum
);
2431 /* Find index for "wp". */
2432 i
= diff_buf_idx(wp
->w_buffer
);
2433 if (i
== DB_COUNT
) /* safety check */
2436 n
= lnum
+ (dp
->df_lnum
[i
] - dp
->df_lnum
[idx
]);
2437 if (n
> dp
->df_lnum
[i
] + dp
->df_count
[i
])
2438 n
= dp
->df_lnum
[i
] + dp
->df_count
[i
];
2443 #endif /* FEAT_DIFF */