blob: 9028d20c153f89418187d61b308a004faa36a790 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vim:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * diff.c: code for diff'ing two or three buffers.
12 */
13
14#include "vim.h"
15
16#if defined(FEAT_DIFF) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018static int diff_busy = FALSE; /* ex_diffgetput() is busy */
19
20/* flags obtained from the 'diffopt' option */
21#define DIFF_FILLER 1 /* display filler lines */
22#define DIFF_ICASE 2 /* ignore case */
23#define DIFF_IWHITE 4 /* ignore change in white space */
Bram Moolenaarc4675a12006-03-15 22:50:30 +000024#define DIFF_HORIZONTAL 8 /* horizontal splits */
25#define DIFF_VERTICAL 16 /* vertical splits */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026static int diff_flags = DIFF_FILLER;
27
28#define LBUFLEN 50 /* length of line in diff file */
29
30static 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)
33static int diff_bin_works = MAYBE; /* TRUE when "diff --binary" works, FALSE
34 when it doesn't work, MAYBE when not
35 checked yet */
36#endif
37
38static int diff_buf_idx __ARGS((buf_T *buf));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000039static int diff_buf_idx_tp __ARGS((buf_T *buf, tabpage_T *tp));
40static void diff_mark_adjust_tp __ARGS((tabpage_T *tp, int idx, linenr_T line1, linenr_T line2, long amount, long amount_after));
41static void diff_check_unchanged __ARGS((tabpage_T *tp, diff_T *dp));
42static int diff_check_sanity __ARGS((tabpage_T *tp, diff_T *dp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000043static void diff_redraw __ARGS((int dofold));
44static int diff_write __ARGS((buf_T *buf, char_u *fname));
45static void diff_file __ARGS((char_u *tmp_orig, char_u *tmp_new, char_u *tmp_diff));
Bram Moolenaar071d4272004-06-13 20:20:40 +000046static int diff_equal_entry __ARGS((diff_T *dp, int idx1, int idx2));
47static int diff_cmp __ARGS((char_u *s1, char_u *s2));
48#ifdef FEAT_FOLDING
49static void diff_fold_update __ARGS((diff_T *dp, int skip_idx));
50#endif
51static void diff_read __ARGS((int idx_orig, int idx_new, char_u *fname));
52static void diff_copy_entry __ARGS((diff_T *dprev, diff_T *dp, int idx_orig, int idx_new));
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000053static diff_T *diff_alloc_new __ARGS((tabpage_T *tp, diff_T *dprev, diff_T *dp));
Bram Moolenaar071d4272004-06-13 20:20:40 +000054
55#ifndef USE_CR
56# define tag_fgets vim_fgets
57#endif
58
59/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000060 * Called when deleting or unloading a buffer: No longer make a diff with it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000061 */
62 void
63diff_buf_delete(buf)
64 buf_T *buf;
65{
66 int i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000067 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000069 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +000070 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000071 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 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000077 }
78}
79
80/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000081 * Check if the current buffer should be added to or removed from the list of
82 * diff buffers.
83 */
84 void
85diff_buf_adjust(win)
86 win_T *win;
87{
88 win_T *wp;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000089 int i;
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000090
91 if (!win->w_p_diff)
92 {
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)
97 break;
98 if (wp == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +000099 {
100 i = diff_buf_idx(win->w_buffer);
101 if (i != DB_COUNT)
102 {
103 curtab->tp_diffbuf[i] = NULL;
104 curtab->tp_diff_invalid = TRUE;
105 }
106 }
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +0000107 }
108 else
109 diff_buf_add(win->w_buffer);
110}
111
112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +0000113 * Add a buffer to make diffs for.
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000114 * Call this when a new buffer is being edited in the current window where
115 * 'diff' is set.
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.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000119 */
120 void
121diff_buf_add(buf)
122 buf_T *buf;
123{
124 int i;
125
126 if (diff_buf_idx(buf) != DB_COUNT)
127 return; /* It's already there. */
128
129 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000130 if (curtab->tp_diffbuf[i] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000131 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000132 curtab->tp_diffbuf[i] = buf;
133 curtab->tp_diff_invalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000134 return;
135 }
136
137 EMSGN(_("E96: Can not diff more than %ld buffers"), DB_COUNT);
138}
139
140/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000141 * Find buffer "buf" in the list of diff buffers for the current tab page.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000142 * Return its index or DB_COUNT if not found.
143 */
144 static int
145diff_buf_idx(buf)
146 buf_T *buf;
147{
148 int idx;
149
150 for (idx = 0; idx < DB_COUNT; ++idx)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000151 if (curtab->tp_diffbuf[idx] == buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000152 break;
153 return idx;
154}
155
156/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000157 * Find buffer "buf" in the list of diff buffers for tab page "tp".
158 * Return its index or DB_COUNT if not found.
159 */
160 static int
161diff_buf_idx_tp(buf, tp)
162 buf_T *buf;
163 tabpage_T *tp;
164{
165 int idx;
166
167 for (idx = 0; idx < DB_COUNT; ++idx)
168 if (tp->tp_diffbuf[idx] == buf)
169 break;
170 return idx;
171}
172
173/*
174 * Mark the diff info involving buffer "buf" as invalid, it will be updated
175 * when info is requested.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000176 */
177 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000178diff_invalidate(buf)
179 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000181 tabpage_T *tp;
182 int i;
183
184 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000186 i = diff_buf_idx_tp(buf, tp);
187 if (i != DB_COUNT)
188 {
189 tp->tp_diff_invalid = TRUE;
190 if (tp == curtab)
191 diff_redraw(TRUE);
192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193 }
194}
195
196/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000197 * Called by mark_adjust(): update line numbers in "curbuf".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198 */
199 void
200diff_mark_adjust(line1, line2, amount, amount_after)
201 linenr_T line1;
202 linenr_T line2;
203 long amount;
204 long amount_after;
205{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000206 int idx;
207 tabpage_T *tp;
208
209 /* Handle all tab pages that use the current buffer in a diff. */
210 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
211 {
212 idx = diff_buf_idx_tp(curbuf, tp);
213 if (idx != DB_COUNT)
214 diff_mark_adjust_tp(tp, idx, line1, line2, amount, amount_after);
215 }
216}
217
218/*
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.
224 */
225 static void
226diff_mark_adjust_tp(tp, idx, line1, line2, amount, amount_after)
227 tabpage_T *tp;
228 int idx;
229 linenr_T line1;
230 linenr_T line2;
231 long amount;
232 long amount_after;
233{
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234 diff_T *dp;
235 diff_T *dprev;
236 diff_T *dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 int i;
238 int inserted, deleted;
239 int n, off;
240 linenr_T last;
241 linenr_T lnum_deleted = line1; /* lnum of remaining deletion */
242 int check_unchanged;
243
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244 if (line2 == MAXLNUM)
245 {
246 /* mark_adjust(99, MAXLNUM, 9, 0): insert lines */
247 inserted = amount;
248 deleted = 0;
249 }
250 else if (amount_after > 0)
251 {
252 /* mark_adjust(99, 98, MAXLNUM, 9): a change that inserts lines*/
253 inserted = amount_after;
254 deleted = 0;
255 }
256 else
257 {
258 /* mark_adjust(98, 99, MAXLNUM, -2): delete lines */
259 inserted = 0;
260 deleted = -amount_after;
261 }
262
263 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000264 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265 for (;;)
266 {
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))
272 && (dprev == NULL
273 || dprev->df_lnum[idx] + dprev->df_count[idx] < line1)
274 && !diff_busy)
275 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000276 dnext = diff_alloc_new(tp, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277 if (dnext == NULL)
278 return;
279
280 dnext->df_lnum[idx] = line1;
281 dnext->df_count[idx] = inserted;
282 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000283 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284 {
285 if (dprev == NULL)
286 dnext->df_lnum[i] = line1;
287 else
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;
292 }
293 }
294
295 /* if at end of the list, quit */
296 if (dp == NULL)
297 break;
298
299 /*
300 * Check for these situations:
301 * 1 2 3
302 * 1 2 3
303 * line1 2 3 4 5
304 * 2 3 4 5
305 * 2 3 4 5
306 * line2 2 3 4 5
307 * 3 5 6
308 * 3 5 6
309 */
310 /* compute last line of this change */
311 last = dp->df_lnum[idx] + dp->df_count[idx] - 1;
312
313 /* 1. change completely above line1: nothing to do */
314 if (last >= line1 - 1)
315 {
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)
319 {
320 if (amount_after == 0)
321 break; /* nothing left to change */
322 dp->df_lnum[idx] += amount_after;
323 }
324 else
325 {
326 check_unchanged = FALSE;
327
328 /* 2. 3. 4. 5.: inserted/deleted lines touching this diff. */
329 if (deleted > 0)
330 {
331 if (dp->df_lnum[idx] >= line1)
332 {
333 off = dp->df_lnum[idx] - lnum_deleted;
334 if (last <= line2)
335 {
336 /* 4. delete all lines of diff */
337 if (dp->df_next != NULL
338 && dp->df_next->df_lnum[idx] - 1 <= line2)
339 {
340 /* delete continues in next diff, only do
341 * lines until that one */
342 n = dp->df_next->df_lnum[idx] - lnum_deleted;
343 deleted -= n;
344 n -= dp->df_count[idx];
345 lnum_deleted = dp->df_next->df_lnum[idx];
346 }
347 else
348 n = deleted - dp->df_count[idx];
349 dp->df_count[idx] = 0;
350 }
351 else
352 {
353 /* 5. delete lines at or just before top of diff */
354 n = off;
355 dp->df_count[idx] -= line2 - dp->df_lnum[idx] + 1;
356 check_unchanged = TRUE;
357 }
358 dp->df_lnum[idx] = line1;
359 }
360 else
361 {
362 off = 0;
363 if (last < line2)
364 {
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)
369 {
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]
374 - lnum_deleted;
375 lnum_deleted = dp->df_next->df_lnum[idx];
376 }
377 else
378 n = line2 - last;
379 check_unchanged = TRUE;
380 }
381 else
382 {
383 /* 3. delete lines inside the diff */
384 n = 0;
385 dp->df_count[idx] -= deleted;
386 }
387 }
388
389 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000390 if (tp->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 {
392 dp->df_lnum[i] -= off;
393 dp->df_count[i] += n;
394 }
395 }
396 else
397 {
398 if (dp->df_lnum[idx] <= line1)
399 {
400 /* inserted lines somewhere in this diff */
401 dp->df_count[idx] += inserted;
402 check_unchanged = TRUE;
403 }
404 else
405 /* inserted lines somewhere above this diff */
406 dp->df_lnum[idx] += inserted;
407 }
408
409 if (check_unchanged)
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. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000413 diff_check_unchanged(tp, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000414 }
415 }
416
417 /* check if this block touches the previous one, may merge them. */
418 if (dprev != NULL && dprev->df_lnum[idx] + dprev->df_count[idx]
419 == dp->df_lnum[idx])
420 {
421 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000422 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000423 dprev->df_count[i] += dp->df_count[i];
424 dprev->df_next = dp->df_next;
425 vim_free(dp);
426 dp = dprev->df_next;
427 }
428 else
429 {
430 /* Advance to next entry. */
431 dprev = dp;
432 dp = dp->df_next;
433 }
434 }
435
436 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000437 dp = tp->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000438 while (dp != NULL)
439 {
440 /* All counts are zero, remove this entry. */
441 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000442 if (tp->tp_diffbuf[i] != NULL && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000443 break;
444 if (i == DB_COUNT)
445 {
446 dnext = dp->df_next;
447 vim_free(dp);
448 dp = dnext;
449 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000450 tp->tp_first_diff = dnext;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000451 else
452 dprev->df_next = dnext;
453 }
454 else
455 {
456 /* Advance to next entry. */
457 dprev = dp;
458 dp = dp->df_next;
459 }
460
461 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000463 if (tp == curtab)
464 {
465 diff_redraw(TRUE);
466
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;
471 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472}
473
474/*
475 * Allocate a new diff block and link it between "dprev" and "dp".
476 */
477 static diff_T *
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000478diff_alloc_new(tp, dprev, dp)
479 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480 diff_T *dprev;
481 diff_T *dp;
482{
483 diff_T *dnew;
484
485 dnew = (diff_T *)alloc((unsigned)sizeof(diff_T));
486 if (dnew != NULL)
487 {
488 dnew->df_next = dp;
489 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000490 tp->tp_first_diff = dnew;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000491 else
492 dprev->df_next = dnew;
493 }
494 return dnew;
495}
496
497/*
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.
502 */
503 static void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000504diff_check_unchanged(tp, dp)
505 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000506 diff_T *dp;
507{
508 int i_org;
509 int i_new;
510 int off_org, off_new;
511 char_u *line_org;
512 int dir = FORWARD;
513
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)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000517 if (tp->tp_diffbuf[i_org] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 break;
519 if (i_org == DB_COUNT) /* safety check */
520 return;
521
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000522 if (diff_check_sanity(tp, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000523 return;
524
525 /* First check lines at the top, then at the bottom. */
526 off_org = 0;
527 off_new = 0;
528 for (;;)
529 {
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)
533 {
534 /* Copy the line, the next ml_get() will invalidate it. */
535 if (dir == BACKWARD)
536 off_org = dp->df_count[i_org] - 1;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000537 line_org = vim_strsave(ml_get_buf(tp->tp_diffbuf[i_org],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000538 dp->df_lnum[i_org] + off_org, FALSE));
539 if (line_org == NULL)
540 return;
541 for (i_new = i_org + 1; i_new < DB_COUNT; ++i_new)
542 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000543 if (tp->tp_diffbuf[i_new] == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000544 continue;
545 if (dir == BACKWARD)
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])
549 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000550 if (diff_cmp(line_org, ml_get_buf(tp->tp_diffbuf[i_new],
Bram Moolenaar071d4272004-06-13 20:20:40 +0000551 dp->df_lnum[i_new] + off_new, FALSE)) != 0)
552 break;
553 }
554 vim_free(line_org);
555
556 /* Stop when a line isn't equal in all diff buffers. */
557 if (i_new != DB_COUNT)
558 break;
559
560 /* Line matched in all buffers, remove it from the diff. */
561 for (i_new = i_org; i_new < DB_COUNT; ++i_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000562 if (tp->tp_diffbuf[i_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 {
564 if (dir == FORWARD)
565 ++dp->df_lnum[i_new];
566 --dp->df_count[i_new];
567 }
568 }
569 if (dir == BACKWARD)
570 break;
571 dir = BACKWARD;
572 }
573}
574
575/*
576 * Check if a diff block doesn't contain invalid line numbers.
577 * This can happen when the diff program returns invalid results.
578 */
579 static int
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000580diff_check_sanity(tp, dp)
581 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 diff_T *dp;
583{
584 int i;
585
586 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000587 if (tp->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 if (dp->df_lnum[i] + dp->df_count[i] - 1
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000589 > tp->tp_diffbuf[i]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000590 return FAIL;
591 return OK;
592}
593
594/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000595 * Mark all diff buffers in the current tab page for redraw.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000596 */
597 static void
598diff_redraw(dofold)
599 int dofold; /* also recompute the folds */
600{
601 win_T *wp;
602 int n;
603
604 for (wp = firstwin; wp != NULL; wp = wp->w_next)
605 if (wp->w_p_diff)
606 {
Bram Moolenaar60f83772006-03-12 21:52:47 +0000607 redraw_win_later(wp, SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608#ifdef FEAT_FOLDING
609 if (dofold && foldmethodIsDiff(wp))
610 foldUpdateAll(wp);
611#endif
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)
615 {
616 n = diff_check(wp, wp->w_topline);
617 if (wp->w_topfill > n)
618 wp->w_topfill = (n < 0 ? 0 : n);
619 }
620 }
621}
622
623/*
624 * Write buffer "buf" to file "name".
625 * Always use 'fileformat' set to "unix".
626 * Return FAIL for failure
627 */
628 static int
629diff_write(buf, fname)
630 buf_T *buf;
631 char_u *fname;
632{
633 int r;
634 char_u *save_ff;
635
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;
642 return r;
643}
644
645/*
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).
650 */
651/*ARGSUSED*/
652 void
653ex_diffupdate(eap)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000654 exarg_T *eap; /* can be NULL, it's not used */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000655{
656 buf_T *buf;
657 int idx_orig;
658 int idx_new;
659 char_u *tmp_orig;
660 char_u *tmp_new;
661 char_u *tmp_diff;
662 FILE *fd;
663 int ok;
664
665 /* Delete all diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000666 diff_clear(curtab);
667 curtab->tp_diff_invalid = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668
669 /* Use the first buffer as the original text. */
670 for (idx_orig = 0; idx_orig < DB_COUNT; ++idx_orig)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000671 if (curtab->tp_diffbuf[idx_orig] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000672 break;
673 if (idx_orig == DB_COUNT)
674 return;
675
676 /* Only need to do something when there is another buffer. */
677 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000678 if (curtab->tp_diffbuf[idx_new] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 break;
680 if (idx_new == DB_COUNT)
681 return;
682
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)
688 goto theend;
689
690 /*
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.
695 */
696 for (;;)
697 {
698 ok = FALSE;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000699 fd = mch_fopen((char *)tmp_orig, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000700 if (fd != NULL)
701 {
702 fwrite("line1\n", (size_t)6, (size_t)1, fd);
703 fclose(fd);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000704 fd = mch_fopen((char *)tmp_new, "w");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 if (fd != NULL)
706 {
707 fwrite("line2\n", (size_t)6, (size_t)1, fd);
708 fclose(fd);
709 diff_file(tmp_orig, tmp_new, tmp_diff);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +0000710 fd = mch_fopen((char *)tmp_diff, "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 if (fd != NULL)
712 {
713 char_u linebuf[LBUFLEN];
714
715 for (;;)
716 {
717 /* There must be a line that contains "1c1". */
718 if (tag_fgets(linebuf, LBUFLEN, fd))
719 break;
720 if (STRNCMP(linebuf, "1c1", 3) == 0)
721 ok = TRUE;
722 }
723 fclose(fd);
724 }
725 mch_remove(tmp_diff);
726 mch_remove(tmp_new);
727 }
728 mch_remove(tmp_orig);
729 }
730
731#ifdef FEAT_EVAL
732 /* When using 'diffexpr' break here. */
733 if (*p_dex != NUL)
734 break;
735#endif
736
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)
740 {
741 diff_a_works = TRUE;
742 diff_bin_works = TRUE;
743 continue;
744 }
745 if (!ok && diff_a_works == TRUE && diff_bin_works == TRUE)
746 {
747 /* Tried --binary, but it failed. "-a" works though. */
748 diff_bin_works = FALSE;
749 ok = TRUE;
750 }
751#endif
752
753 /* If we checked if "-a" works already, break here. */
754 if (diff_a_works != MAYBE)
755 break;
756 diff_a_works = ok;
757
758 /* If "-a" works break here, otherwise retry without "-a". */
759 if (ok)
760 break;
761 }
762 if (!ok)
763 {
764 EMSG(_("E97: Cannot create diffs"));
765 diff_a_works = MAYBE;
766#if defined(MSWIN) || defined(MSDOS)
767 diff_bin_works = MAYBE;
768#endif
769 goto theend;
770 }
771
772 /* Write the first buffer to a tempfile. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000773 buf = curtab->tp_diffbuf[idx_orig];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774 if (diff_write(buf, tmp_orig) == FAIL)
775 goto theend;
776
777 /* Make a difference between the first buffer and every other. */
778 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
779 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +0000780 buf = curtab->tp_diffbuf[idx_new];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000781 if (buf == NULL)
782 continue;
783 if (diff_write(buf, tmp_new) == FAIL)
784 continue;
785 diff_file(tmp_orig, tmp_new, tmp_diff);
786
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);
790 mch_remove(tmp_new);
791 }
792 mch_remove(tmp_orig);
793
Bram Moolenaar60a44dc2007-10-19 16:58:12 +0000794 /* force updating cursor position on screen */
795 curwin->w_valid_cursor.lnum = 0;
796
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 diff_redraw(TRUE);
798
799theend:
800 vim_free(tmp_orig);
801 vim_free(tmp_new);
802 vim_free(tmp_diff);
803}
804
805/*
806 * Make a diff between files "tmp_orig" and "tmp_new", results in "tmp_diff".
807 */
808 static void
809diff_file(tmp_orig, tmp_new, tmp_diff)
810 char_u *tmp_orig;
811 char_u *tmp_new;
812 char_u *tmp_diff;
813{
814 char_u *cmd;
815
816#ifdef FEAT_EVAL
817 if (*p_dex != NUL)
818 /* Use 'diffexpr' to generate the diff file. */
819 eval_diff(tmp_orig, tmp_new, tmp_diff);
820 else
821#endif
822 {
823 cmd = alloc((unsigned)(STRLEN(tmp_orig) + STRLEN(tmp_new)
824 + STRLEN(tmp_diff) + STRLEN(p_srr) + 27));
825 if (cmd != NULL)
826 {
Bram Moolenaar95fb60a2005-03-08 22:29:20 +0000827 /* We don't want $DIFF_OPTIONS to get in the way. */
828 if (getenv("DIFF_OPTIONS"))
829 vim_setenv((char_u *)"DIFF_OPTIONS", (char_u *)"");
830
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 /* Build the diff command and execute it. Always use -a, binary
832 * differences are of no use. Ignore errors, diff returns
833 * non-zero when differences have been found. */
834 sprintf((char *)cmd, "diff %s%s%s%s%s %s",
835 diff_a_works == FALSE ? "" : "-a ",
836#if defined(MSWIN) || defined(MSDOS)
837 diff_bin_works == TRUE ? "--binary " : "",
838#else
839 "",
840#endif
841 (diff_flags & DIFF_IWHITE) ? "-b " : "",
842 (diff_flags & DIFF_ICASE) ? "-i " : "",
843 tmp_orig, tmp_new);
844 append_redir(cmd, p_srr, tmp_diff);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000845#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +0000846 block_autocmds(); /* Avoid ShellCmdPost stuff */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000848 (void)call_shell(cmd, SHELL_FILTER|SHELL_SILENT|SHELL_DOOUT);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000849#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +0000850 unblock_autocmds();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 vim_free(cmd);
853 }
854 }
855}
856
857/*
858 * Create a new version of a file from the current buffer and a diff file.
859 * The buffer is written to a file, also for unmodified buffers (the file
860 * could have been produced by autocommands, e.g. the netrw plugin).
861 */
862 void
863ex_diffpatch(eap)
864 exarg_T *eap;
865{
866 char_u *tmp_orig; /* name of original temp file */
867 char_u *tmp_new; /* name of patched temp file */
868 char_u *buf = NULL;
869 win_T *old_curwin = curwin;
870 char_u *newname = NULL; /* name of patched file buffer */
871#ifdef UNIX
872 char_u dirbuf[MAXPATHL];
873 char_u *fullname = NULL;
874#endif
875#ifdef FEAT_BROWSE
876 char_u *browseFile = NULL;
877 int browse_flag = cmdmod.browse;
878#endif
879
880#ifdef FEAT_BROWSE
881 if (cmdmod.browse)
882 {
Bram Moolenaar7171abe2004-10-11 10:06:20 +0000883 browseFile = do_browse(0, (char_u *)_("Patch file"),
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 eap->arg, NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
885 if (browseFile == NULL)
886 return; /* operation cancelled */
887 eap->arg = browseFile;
888 cmdmod.browse = FALSE; /* don't let do_ecmd() browse again */
889 }
890#endif
891
892 /* We need two temp file names. */
893 tmp_orig = vim_tempname('o');
894 tmp_new = vim_tempname('n');
895 if (tmp_orig == NULL || tmp_new == NULL)
896 goto theend;
897
898 /* Write the current buffer to "tmp_orig". */
899 if (buf_write(curbuf, tmp_orig, NULL,
900 (linenr_T)1, curbuf->b_ml.ml_line_count,
901 NULL, FALSE, FALSE, FALSE, TRUE) == FAIL)
902 goto theend;
903
904#ifdef UNIX
905 /* Get the absolute path of the patchfile, changing directory below. */
906 fullname = FullName_save(eap->arg, FALSE);
907#endif
908 buf = alloc((unsigned)(STRLEN(tmp_orig) + (
909# ifdef UNIX
910 fullname != NULL ? STRLEN(fullname) :
911# endif
912 STRLEN(eap->arg)) + STRLEN(tmp_new) + 16));
913 if (buf == NULL)
914 goto theend;
915
916#ifdef UNIX
917 /* Temporaraly chdir to /tmp, to avoid patching files in the current
918 * directory when the patch file contains more than one patch. When we
919 * have our own temp dir use that instead, it will be cleaned up when we
920 * exit (any .rej files created). Don't change directory if we can't
921 * return to the current. */
922 if (mch_dirname(dirbuf, MAXPATHL) != OK || mch_chdir((char *)dirbuf) != 0)
923 dirbuf[0] = NUL;
924 else
925 {
926# ifdef TEMPDIRNAMES
927 if (vim_tempdir != NULL)
928 mch_chdir((char *)vim_tempdir);
929 else
930# endif
931 mch_chdir("/tmp");
932 shorten_fnames(TRUE);
933 }
934#endif
935
936#ifdef FEAT_EVAL
937 if (*p_pex != NUL)
938 /* Use 'patchexpr' to generate the new file. */
939 eval_patch(tmp_orig,
940# ifdef UNIX
941 fullname != NULL ? fullname :
942# endif
943 eap->arg, tmp_new);
944 else
945#endif
946 {
947 /* Build the patch command and execute it. Ignore errors. Switch to
948 * cooked mode to allow the user to respond to prompts. */
949 sprintf((char *)buf, "patch -o %s %s < \"%s\"", tmp_new, tmp_orig,
950# ifdef UNIX
951 fullname != NULL ? fullname :
952# endif
953 eap->arg);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000954#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +0000955 block_autocmds(); /* Avoid ShellCmdPost stuff */
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000957 (void)call_shell(buf, SHELL_FILTER | SHELL_COOKED);
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000958#ifdef FEAT_AUTOCMD
Bram Moolenaar78ab3312007-09-29 12:16:41 +0000959 unblock_autocmds();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +0000960#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 }
962
963#ifdef UNIX
964 if (dirbuf[0] != NUL)
965 {
966 if (mch_chdir((char *)dirbuf) != 0)
967 EMSG(_(e_prev_dir));
968 shorten_fnames(TRUE);
969 }
970#endif
971
972 /* patch probably has written over the screen */
973 redraw_later(CLEAR);
974
975 /* Delete any .orig or .rej file created. */
976 STRCPY(buf, tmp_new);
977 STRCAT(buf, ".orig");
978 mch_remove(buf);
979 STRCPY(buf, tmp_new);
980 STRCAT(buf, ".rej");
981 mch_remove(buf);
982
983 if (curbuf->b_fname != NULL)
984 {
985 newname = vim_strnsave(curbuf->b_fname,
986 (int)(STRLEN(curbuf->b_fname) + 4));
987 if (newname != NULL)
988 STRCAT(newname, ".new");
989 }
990
991#ifdef FEAT_GUI
992 need_mouse_correct = TRUE;
993#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +0000994 /* don't use a new tab page, each tab page has its own diffs */
995 cmdmod.tab = 0;
996
Bram Moolenaarc4675a12006-03-15 22:50:30 +0000997 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 {
999 /* Pretend it was a ":split fname" command */
1000 eap->cmdidx = CMD_split;
1001 eap->arg = tmp_new;
1002 do_exedit(eap, old_curwin);
1003
1004 if (curwin != old_curwin) /* split must have worked */
1005 {
1006 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1007 diff_win_options(curwin, TRUE);
1008 diff_win_options(old_curwin, TRUE);
1009
1010 if (newname != NULL)
1011 {
1012 /* do a ":file filename.new" on the patched buffer */
1013 eap->arg = newname;
1014 ex_file(eap);
1015
1016#ifdef FEAT_AUTOCMD
1017 /* Do filetype detection with the new name. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001018 if (au_has_group((char_u *)"filetypedetect"))
1019 do_cmdline_cmd((char_u *)":doau filetypedetect BufRead");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020#endif
1021 }
1022 }
1023 }
1024
1025theend:
1026 if (tmp_orig != NULL)
1027 mch_remove(tmp_orig);
1028 vim_free(tmp_orig);
1029 if (tmp_new != NULL)
1030 mch_remove(tmp_new);
1031 vim_free(tmp_new);
1032 vim_free(newname);
1033 vim_free(buf);
1034#ifdef UNIX
1035 vim_free(fullname);
1036#endif
1037#ifdef FEAT_BROWSE
1038 vim_free(browseFile);
1039 cmdmod.browse = browse_flag;
1040#endif
1041}
1042
1043/*
1044 * Split the window and edit another file, setting options to show the diffs.
1045 */
1046 void
1047ex_diffsplit(eap)
1048 exarg_T *eap;
1049{
1050 win_T *old_curwin = curwin;
1051
1052#ifdef FEAT_GUI
1053 need_mouse_correct = TRUE;
1054#endif
Bram Moolenaar80a94a52006-02-23 21:26:58 +00001055 /* don't use a new tab page, each tab page has its own diffs */
1056 cmdmod.tab = 0;
1057
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001058 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 {
1060 /* Pretend it was a ":split fname" command */
1061 eap->cmdidx = CMD_split;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001062 curwin->w_p_diff = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 do_exedit(eap, old_curwin);
1064
1065 if (curwin != old_curwin) /* split must have worked */
1066 {
1067 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1068 diff_win_options(curwin, TRUE);
1069 diff_win_options(old_curwin, TRUE);
1070 }
1071 }
1072}
1073
1074/*
1075 * Set options to show difs for the current window.
1076 */
1077/*ARGSUSED*/
1078 void
1079ex_diffthis(eap)
1080 exarg_T *eap;
1081{
1082 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
1083 diff_win_options(curwin, TRUE);
1084}
1085
1086/*
1087 * Set options in window "wp" for diff mode.
1088 */
1089 void
1090diff_win_options(wp, addbuf)
1091 win_T *wp;
1092 int addbuf; /* Add buffer to diff. */
1093{
1094 wp->w_p_diff = TRUE;
1095 wp->w_p_scb = TRUE;
1096 wp->w_p_wrap = FALSE;
1097# ifdef FEAT_FOLDING
1098 {
1099 win_T *old_curwin = curwin;
1100
1101 curwin = wp;
1102 curbuf = curwin->w_buffer;
1103 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"diff",
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001104 OPT_LOCAL|OPT_FREE, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 curwin = old_curwin;
1106 curbuf = curwin->w_buffer;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001107 wp->w_p_fdc = diff_foldcolumn;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 wp->w_p_fen = TRUE;
1109 wp->w_p_fdl = 0;
1110 foldUpdateAll(wp);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001111 /* make sure topline is not halfway a fold */
1112 changed_window_setting_win(wp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 }
1114# endif
1115#ifdef FEAT_SCROLLBIND
1116 if (vim_strchr(p_sbo, 'h') == NULL)
1117 do_cmdline_cmd((char_u *)"set sbo+=hor");
1118#endif
1119
1120 if (addbuf)
1121 diff_buf_add(wp->w_buffer);
1122 redraw_win_later(wp, NOT_VALID);
1123}
1124
1125/*
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001126 * Set options not to show diffs. For the current window or all windows.
Bram Moolenaarf740b292006-02-16 22:11:02 +00001127 * Only in the current tab page.
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001128 */
1129 void
1130ex_diffoff(eap)
1131 exarg_T *eap;
1132{
1133 win_T *wp;
1134 win_T *old_curwin = curwin;
1135#ifdef FEAT_SCROLLBIND
1136 int diffwin = FALSE;
1137#endif
1138
1139 for (wp = firstwin; wp != NULL; wp = wp->w_next)
1140 {
1141 if (wp == curwin || eap->forceit)
1142 {
1143 /* Set 'diff', 'scrollbind' off and 'wrap' on. */
1144 wp->w_p_diff = FALSE;
1145 wp->w_p_scb = FALSE;
1146 wp->w_p_wrap = TRUE;
1147#ifdef FEAT_FOLDING
1148 curwin = wp;
1149 curbuf = curwin->w_buffer;
1150 set_string_option_direct((char_u *)"fdm", -1,
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00001151 (char_u *)"manual", OPT_LOCAL|OPT_FREE, 0);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +00001152 curwin = old_curwin;
1153 curbuf = curwin->w_buffer;
1154 wp->w_p_fdc = 0;
1155 wp->w_p_fen = FALSE;
1156 wp->w_p_fdl = 0;
1157 foldUpdateAll(wp);
1158 /* make sure topline is not halfway a fold */
1159 changed_window_setting_win(wp);
1160#endif
1161 diff_buf_adjust(wp);
1162 }
1163#ifdef FEAT_SCROLLBIND
1164 diffwin |= wp->w_p_diff;
1165#endif
1166 }
1167
1168#ifdef FEAT_SCROLLBIND
1169 /* Remove "hor" from from 'scrollopt' if there are no diff windows left. */
1170 if (!diffwin && vim_strchr(p_sbo, 'h') != NULL)
1171 do_cmdline_cmd((char_u *)"set sbo-=hor");
1172#endif
1173}
1174
1175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 * Read the diff output and add each entry to the diff list.
1177 */
1178 static void
1179diff_read(idx_orig, idx_new, fname)
1180 int idx_orig; /* idx of original file */
1181 int idx_new; /* idx of new file */
1182 char_u *fname; /* name of diff output file */
1183{
1184 FILE *fd;
1185 diff_T *dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001186 diff_T *dp = curtab->tp_first_diff;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187 diff_T *dn, *dpl;
1188 long f1, l1, f2, l2;
1189 char_u linebuf[LBUFLEN]; /* only need to hold the diff line */
1190 int difftype;
1191 char_u *p;
1192 long off;
1193 int i;
1194 linenr_T lnum_orig, lnum_new;
1195 long count_orig, count_new;
1196 int notset = TRUE; /* block "*dp" not set yet */
1197
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00001198 fd = mch_fopen((char *)fname, "r");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 if (fd == NULL)
1200 {
1201 EMSG(_("E98: Cannot read diff output"));
1202 return;
1203 }
1204
1205 for (;;)
1206 {
1207 if (tag_fgets(linebuf, LBUFLEN, fd))
1208 break; /* end of file */
1209 if (!isdigit(*linebuf))
1210 continue; /* not the start of a diff block */
1211
1212 /* This line must be one of three formats:
1213 * {first}[,{last}]c{first}[,{last}]
1214 * {first}a{first}[,{last}]
1215 * {first}[,{last}]d{first}
1216 */
1217 p = linebuf;
1218 f1 = getdigits(&p);
1219 if (*p == ',')
1220 {
1221 ++p;
1222 l1 = getdigits(&p);
1223 }
1224 else
1225 l1 = f1;
1226 if (*p != 'a' && *p != 'c' && *p != 'd')
1227 continue; /* invalid diff format */
1228 difftype = *p++;
1229 f2 = getdigits(&p);
1230 if (*p == ',')
1231 {
1232 ++p;
1233 l2 = getdigits(&p);
1234 }
1235 else
1236 l2 = f2;
1237 if (l1 < f1 || l2 < f2)
1238 continue; /* invalid line range */
1239
1240 if (difftype == 'a')
1241 {
1242 lnum_orig = f1 + 1;
1243 count_orig = 0;
1244 }
1245 else
1246 {
1247 lnum_orig = f1;
1248 count_orig = l1 - f1 + 1;
1249 }
1250 if (difftype == 'd')
1251 {
1252 lnum_new = f2 + 1;
1253 count_new = 0;
1254 }
1255 else
1256 {
1257 lnum_new = f2;
1258 count_new = l2 - f2 + 1;
1259 }
1260
1261 /* Go over blocks before the change, for which orig and new are equal.
1262 * Copy blocks from orig to new. */
1263 while (dp != NULL
1264 && lnum_orig > dp->df_lnum[idx_orig] + dp->df_count[idx_orig])
1265 {
1266 if (notset)
1267 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1268 dprev = dp;
1269 dp = dp->df_next;
1270 notset = TRUE;
1271 }
1272
1273 if (dp != NULL
1274 && lnum_orig <= dp->df_lnum[idx_orig] + dp->df_count[idx_orig]
1275 && lnum_orig + count_orig >= dp->df_lnum[idx_orig])
1276 {
1277 /* New block overlaps with existing block(s).
1278 * First find last block that overlaps. */
1279 for (dpl = dp; dpl->df_next != NULL; dpl = dpl->df_next)
1280 if (lnum_orig + count_orig < dpl->df_next->df_lnum[idx_orig])
1281 break;
1282
1283 /* If the newly found block starts before the old one, set the
1284 * start back a number of lines. */
1285 off = dp->df_lnum[idx_orig] - lnum_orig;
1286 if (off > 0)
1287 {
1288 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001289 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 dp->df_lnum[i] -= off;
1291 dp->df_lnum[idx_new] = lnum_new;
1292 dp->df_count[idx_new] = count_new;
1293 }
1294 else if (notset)
1295 {
1296 /* new block inside existing one, adjust new block */
1297 dp->df_lnum[idx_new] = lnum_new + off;
1298 dp->df_count[idx_new] = count_new - off;
1299 }
1300 else
1301 /* second overlap of new block with existing block */
1302 dp->df_count[idx_new] += count_new - count_orig;
1303
1304 /* Adjust the size of the block to include all the lines to the
1305 * end of the existing block or the new diff, whatever ends last. */
1306 off = (lnum_orig + count_orig)
1307 - (dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]);
1308 if (off < 0)
1309 {
1310 /* new change ends in existing block, adjust the end if not
1311 * done already */
1312 if (notset)
1313 dp->df_count[idx_new] += -off;
1314 off = 0;
1315 }
Bram Moolenaard4b96bc2007-10-19 15:33:39 +00001316 for (i = idx_orig; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001317 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 dp->df_count[i] = dpl->df_lnum[i] + dpl->df_count[i]
1319 - dp->df_lnum[i] + off;
1320
1321 /* Delete the diff blocks that have been merged into one. */
1322 dn = dp->df_next;
1323 dp->df_next = dpl->df_next;
1324 while (dn != dp->df_next)
1325 {
1326 dpl = dn->df_next;
1327 vim_free(dn);
1328 dn = dpl;
1329 }
1330 }
1331 else
1332 {
1333 /* Allocate a new diffblock. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001334 dp = diff_alloc_new(curtab, dprev, dp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001335 if (dp == NULL)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001336 goto done;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337
1338 dp->df_lnum[idx_orig] = lnum_orig;
1339 dp->df_count[idx_orig] = count_orig;
1340 dp->df_lnum[idx_new] = lnum_new;
1341 dp->df_count[idx_new] = count_new;
1342
1343 /* Set values for other buffers, these must be equal to the
1344 * original buffer, otherwise there would have been a change
1345 * already. */
1346 for (i = idx_orig + 1; i < idx_new; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001347 if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 diff_copy_entry(dprev, dp, idx_orig, i);
1349 }
1350 notset = FALSE; /* "*dp" has been set */
1351 }
1352
1353 /* for remaining diff blocks orig and new are equal */
1354 while (dp != NULL)
1355 {
1356 if (notset)
1357 diff_copy_entry(dprev, dp, idx_orig, idx_new);
1358 dprev = dp;
1359 dp = dp->df_next;
1360 notset = TRUE;
1361 }
1362
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001363done:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001364 fclose(fd);
1365}
1366
1367/*
1368 * Copy an entry at "dp" from "idx_orig" to "idx_new".
1369 */
1370 static void
1371diff_copy_entry(dprev, dp, idx_orig, idx_new)
1372 diff_T *dprev;
1373 diff_T *dp;
1374 int idx_orig;
1375 int idx_new;
1376{
1377 long off;
1378
1379 if (dprev == NULL)
1380 off = 0;
1381 else
1382 off = (dprev->df_lnum[idx_orig] + dprev->df_count[idx_orig])
1383 - (dprev->df_lnum[idx_new] + dprev->df_count[idx_new]);
1384 dp->df_lnum[idx_new] = dp->df_lnum[idx_orig] - off;
1385 dp->df_count[idx_new] = dp->df_count[idx_orig];
1386}
1387
1388/*
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001389 * Clear the list of diffblocks for tab page "tp".
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 */
Bram Moolenaarea408852005-06-25 22:49:46 +00001391 void
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001392diff_clear(tp)
1393 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001394{
1395 diff_T *p, *next_p;
1396
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001397 for (p = tp->tp_first_diff; p != NULL; p = next_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 {
1399 next_p = p->df_next;
1400 vim_free(p);
1401 }
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001402 tp->tp_first_diff = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403}
1404
1405/*
1406 * Check diff status for line "lnum" in buffer "buf":
1407 * Returns 0 for nothing special
1408 * Returns -1 for a line that should be highlighted as changed.
1409 * Returns -2 for a line that should be highlighted as added/deleted.
1410 * Returns > 0 for inserting that many filler lines above it (never happens
1411 * when 'diffopt' doesn't contain "filler").
1412 * This should only be used for windows where 'diff' is set.
1413 */
1414 int
1415diff_check(wp, lnum)
1416 win_T *wp;
1417 linenr_T lnum;
1418{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001419 int idx; /* index in tp_diffbuf[] for this buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 diff_T *dp;
1421 int maxcount;
1422 int i;
1423 buf_T *buf = wp->w_buffer;
1424 int cmp;
1425
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001426 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427 ex_diffupdate(NULL); /* update after a big change */
1428
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001429 if (curtab->tp_first_diff == NULL || !wp->w_p_diff) /* no diffs at all */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 return 0;
1431
1432 /* safety check: "lnum" must be a buffer line */
1433 if (lnum < 1 || lnum > buf->b_ml.ml_line_count + 1)
1434 return 0;
1435
1436 idx = diff_buf_idx(buf);
1437 if (idx == DB_COUNT)
1438 return 0; /* no diffs for buffer "buf" */
1439
1440#ifdef FEAT_FOLDING
1441 /* A closed fold never has filler lines. */
1442 if (hasFoldingWin(wp, lnum, NULL, NULL, TRUE, NULL))
1443 return 0;
1444#endif
1445
1446 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001447 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001448 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
1449 break;
1450 if (dp == NULL || lnum < dp->df_lnum[idx])
1451 return 0;
1452
1453 if (lnum < dp->df_lnum[idx] + dp->df_count[idx])
1454 {
1455 int zero = FALSE;
1456
1457 /* Changed or inserted line. If the other buffers have a count of
1458 * zero, the lines were inserted. If the other buffers have the same
1459 * count, check if the lines are identical. */
1460 cmp = FALSE;
1461 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001462 if (i != idx && curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 {
1464 if (dp->df_count[i] == 0)
1465 zero = TRUE;
1466 else
1467 {
1468 if (dp->df_count[i] != dp->df_count[idx])
1469 return -1; /* nr of lines changed. */
1470 cmp = TRUE;
1471 }
1472 }
1473 if (cmp)
1474 {
1475 /* Compare all lines. If they are equal the lines were inserted
1476 * in some buffers, deleted in others, but not changed. */
1477 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001478 if (i != idx && curtab->tp_diffbuf[i] != NULL && dp->df_count[i] != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 if (!diff_equal_entry(dp, idx, i))
1480 return -1;
1481 }
1482 /* If there is no buffer with zero lines then there is no difference
1483 * any longer. Happens when making a change (or undo) that removes
1484 * the difference. Can't remove the entry here, we might be halfway
1485 * updating the window. Just report the text as unchanged. Other
1486 * windows might still show the change though. */
1487 if (zero == FALSE)
1488 return 0;
1489 return -2;
1490 }
1491
1492 /* If 'diffopt' doesn't contain "filler", return 0. */
1493 if (!(diff_flags & DIFF_FILLER))
1494 return 0;
1495
1496 /* Insert filler lines above the line just below the change. Will return
1497 * 0 when this buf had the max count. */
1498 maxcount = 0;
1499 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001500 if (curtab->tp_diffbuf[i] != NULL && dp->df_count[i] > maxcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501 maxcount = dp->df_count[i];
1502 return maxcount - dp->df_count[idx];
1503}
1504
1505/*
1506 * Compare two entries in diff "*dp" and return TRUE if they are equal.
1507 */
1508 static int
1509diff_equal_entry(dp, idx1, idx2)
1510 diff_T *dp;
1511 int idx1;
1512 int idx2;
1513{
1514 int i;
1515 char_u *line;
1516 int cmp;
1517
1518 if (dp->df_count[idx1] != dp->df_count[idx2])
1519 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001520 if (diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 return FALSE;
1522 for (i = 0; i < dp->df_count[idx1]; ++i)
1523 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001524 line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 dp->df_lnum[idx1] + i, FALSE));
1526 if (line == NULL)
1527 return FALSE;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001528 cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2],
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529 dp->df_lnum[idx2] + i, FALSE));
1530 vim_free(line);
1531 if (cmp != 0)
1532 return FALSE;
1533 }
1534 return TRUE;
1535}
1536
1537/*
1538 * Compare strings "s1" and "s2" according to 'diffopt'.
1539 * Return non-zero when they are different.
1540 */
1541 static int
1542diff_cmp(s1, s2)
1543 char_u *s1;
1544 char_u *s2;
1545{
1546 char_u *p1, *p2;
1547#ifdef FEAT_MBYTE
1548 int l;
1549#endif
1550
1551 if ((diff_flags & (DIFF_ICASE | DIFF_IWHITE)) == 0)
1552 return STRCMP(s1, s2);
1553 if ((diff_flags & DIFF_ICASE) && !(diff_flags & DIFF_IWHITE))
1554 return MB_STRICMP(s1, s2);
1555
1556 /* Ignore white space changes and possibly ignore case. */
1557 p1 = s1;
1558 p2 = s2;
1559 while (*p1 != NUL && *p2 != NUL)
1560 {
1561 if (vim_iswhite(*p1) && vim_iswhite(*p2))
1562 {
1563 p1 = skipwhite(p1);
1564 p2 = skipwhite(p2);
1565 }
1566 else
1567 {
1568#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001569 l = (*mb_ptr2len)(p1);
1570 if (l != (*mb_ptr2len)(p2))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571 break;
1572 if (l > 1)
1573 {
1574 if (STRNCMP(p1, p2, l) != 0
1575 && (!enc_utf8
1576 || !(diff_flags & DIFF_ICASE)
1577 || utf_fold(utf_ptr2char(p1))
1578 != utf_fold(utf_ptr2char(p2))))
1579 break;
1580 p1 += l;
1581 p2 += l;
1582 }
1583 else
1584#endif
1585 {
1586 if (*p1 != *p2 && (!(diff_flags & DIFF_ICASE)
1587 || TOLOWER_LOC(*p1) != TOLOWER_LOC(*p2)))
1588 break;
1589 ++p1;
1590 ++p2;
1591 }
1592 }
1593 }
1594
1595 /* Ignore trailing white space. */
1596 p1 = skipwhite(p1);
1597 p2 = skipwhite(p2);
1598 if (*p1 != NUL || *p2 != NUL)
1599 return 1;
1600 return 0;
1601}
1602
1603/*
1604 * Return the number of filler lines above "lnum".
1605 */
1606 int
1607diff_check_fill(wp, lnum)
1608 win_T *wp;
1609 linenr_T lnum;
1610{
1611 int n;
1612
1613 /* be quick when there are no filler lines */
1614 if (!(diff_flags & DIFF_FILLER))
1615 return 0;
1616 n = diff_check(wp, lnum);
1617 if (n <= 0)
1618 return 0;
1619 return n;
1620}
1621
1622/*
1623 * Set the topline of "towin" to match the position in "fromwin", so that they
1624 * show the same diff'ed lines.
1625 */
1626 void
1627diff_set_topline(fromwin, towin)
1628 win_T *fromwin;
1629 win_T *towin;
1630{
1631 buf_T *buf = fromwin->w_buffer;
1632 linenr_T lnum = fromwin->w_topline;
1633 int idx;
1634 diff_T *dp;
1635 int i;
1636
1637 idx = diff_buf_idx(buf);
1638 if (idx == DB_COUNT)
1639 return; /* safety check */
1640
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001641 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642 ex_diffupdate(NULL); /* update after a big change */
1643
1644 towin->w_topfill = 0;
1645
1646 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001647 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
1649 break;
1650 if (dp == NULL)
1651 {
1652 /* After last change, compute topline relative to end of file; no
1653 * filler lines. */
1654 towin->w_topline = towin->w_buffer->b_ml.ml_line_count
1655 - (buf->b_ml.ml_line_count - lnum);
1656 }
1657 else
1658 {
1659 /* Find index for "towin". */
1660 i = diff_buf_idx(towin->w_buffer);
1661 if (i == DB_COUNT)
1662 return; /* safety check */
1663
1664 towin->w_topline = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]);
1665 if (lnum >= dp->df_lnum[idx])
1666 {
1667 /* Inside a change: compute filler lines. */
1668 if (dp->df_count[i] == dp->df_count[idx])
1669 towin->w_topfill = fromwin->w_topfill;
1670 else if (dp->df_count[i] > dp->df_count[idx])
1671 {
1672 if (lnum == dp->df_lnum[idx] + dp->df_count[idx])
1673 towin->w_topline = dp->df_lnum[i] + dp->df_count[i]
1674 - fromwin->w_topfill;
1675 }
1676 else
1677 {
1678 if (towin->w_topline >= dp->df_lnum[i] + dp->df_count[i])
1679 {
1680 if (diff_flags & DIFF_FILLER)
1681 towin->w_topfill = dp->df_lnum[idx]
1682 + dp->df_count[idx] - lnum;
1683 towin->w_topline = dp->df_lnum[i] + dp->df_count[i];
1684 }
1685 }
1686 }
1687 }
1688
1689 /* safety check (if diff info gets outdated strange things may happen) */
1690 towin->w_botfill = FALSE;
1691 if (towin->w_topline > towin->w_buffer->b_ml.ml_line_count)
1692 {
1693 towin->w_topline = towin->w_buffer->b_ml.ml_line_count;
1694 towin->w_botfill = TRUE;
1695 }
1696 if (towin->w_topline < 1)
1697 {
1698 towin->w_topline = 1;
1699 towin->w_topfill = 0;
1700 }
1701
1702 /* When w_topline changes need to recompute w_botline and cursor position */
1703 invalidate_botline_win(towin);
1704 changed_line_abv_curs_win(towin);
1705
1706 check_topfill(towin, FALSE);
1707#ifdef FEAT_FOLDING
1708 (void)hasFoldingWin(towin, towin->w_topline, &towin->w_topline,
1709 NULL, TRUE, NULL);
1710#endif
1711}
1712
1713/*
1714 * This is called when 'diffopt' is changed.
1715 */
1716 int
1717diffopt_changed()
1718{
1719 char_u *p;
1720 int diff_context_new = 6;
1721 int diff_flags_new = 0;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001722 int diff_foldcolumn_new = 2;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001723 tabpage_T *tp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001724
1725 p = p_dip;
1726 while (*p != NUL)
1727 {
1728 if (STRNCMP(p, "filler", 6) == 0)
1729 {
1730 p += 6;
1731 diff_flags_new |= DIFF_FILLER;
1732 }
1733 else if (STRNCMP(p, "context:", 8) == 0 && VIM_ISDIGIT(p[8]))
1734 {
1735 p += 8;
1736 diff_context_new = getdigits(&p);
1737 }
1738 else if (STRNCMP(p, "icase", 5) == 0)
1739 {
1740 p += 5;
1741 diff_flags_new |= DIFF_ICASE;
1742 }
1743 else if (STRNCMP(p, "iwhite", 6) == 0)
1744 {
1745 p += 6;
1746 diff_flags_new |= DIFF_IWHITE;
1747 }
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001748 else if (STRNCMP(p, "horizontal", 10) == 0)
1749 {
1750 p += 10;
1751 diff_flags_new |= DIFF_HORIZONTAL;
1752 }
1753 else if (STRNCMP(p, "vertical", 8) == 0)
1754 {
1755 p += 8;
1756 diff_flags_new |= DIFF_VERTICAL;
1757 }
1758 else if (STRNCMP(p, "foldcolumn:", 11) == 0 && VIM_ISDIGIT(p[11]))
1759 {
1760 p += 11;
1761 diff_foldcolumn_new = getdigits(&p);
1762 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001763 if (*p != ',' && *p != NUL)
1764 return FAIL;
1765 if (*p == ',')
1766 ++p;
1767 }
1768
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001769 /* Can't have both "horizontal" and "vertical". */
1770 if ((diff_flags_new & DIFF_HORIZONTAL) && (diff_flags_new & DIFF_VERTICAL))
1771 return FAIL;
1772
Bram Moolenaar071d4272004-06-13 20:20:40 +00001773 /* If "icase" or "iwhite" was added or removed, need to update the diff. */
1774 if (diff_flags != diff_flags_new)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001775 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
1776 tp->tp_diff_invalid = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001777
1778 diff_flags = diff_flags_new;
1779 diff_context = diff_context_new;
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001780 diff_foldcolumn = diff_foldcolumn_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781
1782 diff_redraw(TRUE);
1783
1784 /* recompute the scroll binding with the new option value, may
1785 * remove or add filler lines */
1786 check_scrollbind((linenr_T)0, 0L);
1787
1788 return OK;
1789}
1790
1791/*
Bram Moolenaarc4675a12006-03-15 22:50:30 +00001792 * Return TRUE if 'diffopt' contains "horizontal".
1793 */
1794 int
1795diffopt_horizontal()
1796{
1797 return (diff_flags & DIFF_HORIZONTAL) != 0;
1798}
1799
1800/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801 * Find the difference within a changed line.
1802 * Returns TRUE if the line was added, no other buffer has it.
1803 */
1804 int
1805diff_find_change(wp, lnum, startp, endp)
1806 win_T *wp;
1807 linenr_T lnum;
1808 int *startp; /* first char of the change */
1809 int *endp; /* last char of the change */
1810{
1811 char_u *line_org;
1812 char_u *line_new;
1813 int i;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001814 int si_org, si_new;
1815 int ei_org, ei_new;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 diff_T *dp;
1817 int idx;
1818 int off;
1819 int added = TRUE;
1820
1821 /* Make a copy of the line, the next ml_get() will invalidate it. */
1822 line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE));
1823 if (line_org == NULL)
1824 return FALSE;
1825
1826 idx = diff_buf_idx(wp->w_buffer);
1827 if (idx == DB_COUNT) /* cannot happen */
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00001828 {
1829 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00001831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832
1833 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001834 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
1836 break;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001837 if (dp == NULL || diff_check_sanity(curtab, dp) == FAIL)
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00001838 {
1839 vim_free(line_org);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 return FALSE;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00001841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
1843 off = lnum - dp->df_lnum[idx];
1844
1845 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001846 if (curtab->tp_diffbuf[i] != NULL && i != idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 {
1848 /* Skip lines that are not in the other change (filler lines). */
1849 if (off >= dp->df_count[i])
1850 continue;
1851 added = FALSE;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001852 line_new = ml_get_buf(curtab->tp_diffbuf[i],
1853 dp->df_lnum[i] + off, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
1855 /* Search for start of difference */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001856 si_org = si_new = 0;
1857 while (line_org[si_org] != NUL)
1858 {
1859 if ((diff_flags & DIFF_IWHITE)
1860 && vim_iswhite(line_org[si_org])
1861 && vim_iswhite(line_new[si_new]))
1862 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001863 si_org = (int)(skipwhite(line_org + si_org) - line_org);
1864 si_new = (int)(skipwhite(line_new + si_new) - line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001865 }
1866 else
1867 {
1868 if (line_org[si_org] != line_new[si_new])
1869 break;
1870 ++si_org;
1871 ++si_new;
1872 }
1873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874#ifdef FEAT_MBYTE
1875 if (has_mbyte)
1876 {
1877 /* Move back to first byte of character in both lines (may
1878 * have "nn^" in line_org and "n^ in line_new). */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001879 si_org -= (*mb_head_off)(line_org, line_org + si_org);
1880 si_new -= (*mb_head_off)(line_new, line_new + si_new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881 }
1882#endif
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001883 if (*startp > si_org)
1884 *startp = si_org;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
1886 /* Search for end of difference, if any. */
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001887 if (line_org[si_org] != NUL || line_new[si_new] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888 {
1889 ei_org = (int)STRLEN(line_org);
1890 ei_new = (int)STRLEN(line_new);
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001891 while (ei_org >= *startp && ei_new >= si_new
1892 && ei_org >= 0 && ei_new >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001894 if ((diff_flags & DIFF_IWHITE)
1895 && vim_iswhite(line_org[ei_org])
1896 && vim_iswhite(line_new[ei_new]))
1897 {
1898 while (ei_org >= *startp
1899 && vim_iswhite(line_org[ei_org]))
1900 --ei_org;
1901 while (ei_new >= si_new
1902 && vim_iswhite(line_new[ei_new]))
1903 --ei_new;
1904 }
1905 else
1906 {
1907 if (line_org[ei_org] != line_new[ei_new])
1908 break;
1909 --ei_org;
1910 --ei_new;
1911 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 }
1913 if (*endp < ei_org)
1914 *endp = ei_org;
1915 }
1916 }
1917
1918 vim_free(line_org);
1919 return added;
1920}
1921
1922#if defined(FEAT_FOLDING) || defined(PROTO)
1923/*
1924 * Return TRUE if line "lnum" is not close to a diff block, this line should
1925 * be in a fold.
1926 * Return FALSE if there are no diff blocks at all in this window.
1927 */
1928 int
1929diff_infold(wp, lnum)
1930 win_T *wp;
1931 linenr_T lnum;
1932{
1933 int i;
1934 int idx = -1;
1935 int other = FALSE;
1936 diff_T *dp;
1937
1938 /* Return if 'diff' isn't set. */
1939 if (!wp->w_p_diff)
1940 return FALSE;
1941
1942 for (i = 0; i < DB_COUNT; ++i)
1943 {
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001944 if (curtab->tp_diffbuf[i] == wp->w_buffer)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 idx = i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001946 else if (curtab->tp_diffbuf[i] != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 other = TRUE;
1948 }
1949
1950 /* return here if there are no diffs in the window */
1951 if (idx == -1 || !other)
1952 return FALSE;
1953
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001954 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 ex_diffupdate(NULL); /* update after a big change */
1956
1957 /* Return if there are no diff blocks. All lines will be folded. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001958 if (curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 return TRUE;
1960
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00001961 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 {
1963 /* If this change is below the line there can't be any further match. */
1964 if (dp->df_lnum[idx] - diff_context > lnum)
1965 break;
1966 /* If this change ends before the line we have a match. */
1967 if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum)
1968 return FALSE;
1969 }
1970 return TRUE;
1971}
1972#endif
1973
1974/*
1975 * "dp" and "do" commands.
1976 */
1977 void
1978nv_diffgetput(put)
1979 int put;
1980{
1981 exarg_T ea;
1982
1983 ea.arg = (char_u *)"";
1984 if (put)
1985 ea.cmdidx = CMD_diffput;
1986 else
1987 ea.cmdidx = CMD_diffget;
1988 ea.addr_count = 0;
1989 ea.line1 = curwin->w_cursor.lnum;
1990 ea.line2 = curwin->w_cursor.lnum;
1991 ex_diffgetput(&ea);
1992}
1993
1994/*
1995 * ":diffget"
1996 * ":diffput"
1997 */
1998 void
1999ex_diffgetput(eap)
2000 exarg_T *eap;
2001{
2002 linenr_T lnum;
2003 int count;
2004 linenr_T off = 0;
2005 diff_T *dp;
2006 diff_T *dprev;
2007 diff_T *dfree;
2008 int idx_cur;
2009 int idx_other;
2010 int idx_from;
2011 int idx_to;
2012 int i;
2013 int added;
2014 char_u *p;
2015 aco_save_T aco;
2016 buf_T *buf;
2017 int start_skip, end_skip;
2018 int new_count;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002019 int buf_empty;
Bram Moolenaar602eb742007-02-20 03:43:38 +00002020 int found_not_ma = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002021
2022 /* Find the current buffer in the list of diff buffers. */
2023 idx_cur = diff_buf_idx(curbuf);
2024 if (idx_cur == DB_COUNT)
2025 {
2026 EMSG(_("E99: Current buffer is not in diff mode"));
2027 return;
2028 }
2029
2030 if (*eap->arg == NUL)
2031 {
2032 /* No argument: Find the other buffer in the list of diff buffers. */
2033 for (idx_other = 0; idx_other < DB_COUNT; ++idx_other)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002034 if (curtab->tp_diffbuf[idx_other] != curbuf
Bram Moolenaar602eb742007-02-20 03:43:38 +00002035 && curtab->tp_diffbuf[idx_other] != NULL)
2036 {
2037 if (eap->cmdidx != CMD_diffput
2038 || curtab->tp_diffbuf[idx_other]->b_p_ma)
2039 break;
2040 found_not_ma = TRUE;
2041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002042 if (idx_other == DB_COUNT)
2043 {
Bram Moolenaar602eb742007-02-20 03:43:38 +00002044 if (found_not_ma)
2045 EMSG(_("E793: No other buffer in diff mode is modifiable"));
2046 else
2047 EMSG(_("E100: No other buffer in diff mode"));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 return;
2049 }
2050
2051 /* Check that there isn't a third buffer in the list */
2052 for (i = idx_other + 1; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002053 if (curtab->tp_diffbuf[i] != curbuf
2054 && curtab->tp_diffbuf[i] != NULL
2055 && (eap->cmdidx != CMD_diffput || curtab->tp_diffbuf[i]->b_p_ma))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002056 {
2057 EMSG(_("E101: More than two buffers in diff mode, don't know which one to use"));
2058 return;
2059 }
2060 }
2061 else
2062 {
2063 /* Buffer number or pattern given. Ignore trailing white space. */
2064 p = eap->arg + STRLEN(eap->arg);
2065 while (p > eap->arg && vim_iswhite(p[-1]))
2066 --p;
2067 for (i = 0; vim_isdigit(eap->arg[i]) && eap->arg + i < p; ++i)
2068 ;
2069 if (eap->arg + i == p) /* digits only */
2070 i = atol((char *)eap->arg);
2071 else
2072 {
2073 i = buflist_findpat(eap->arg, p, FALSE, TRUE);
2074 if (i < 0)
2075 return; /* error message already given */
2076 }
2077 buf = buflist_findnr(i);
2078 if (buf == NULL)
2079 {
2080 EMSG2(_("E102: Can't find buffer \"%s\""), eap->arg);
2081 return;
2082 }
2083 idx_other = diff_buf_idx(buf);
2084 if (idx_other == DB_COUNT)
2085 {
2086 EMSG2(_("E103: Buffer \"%s\" is not in diff mode"), eap->arg);
2087 return;
2088 }
2089 }
2090
2091 diff_busy = TRUE;
2092
2093 /* When no range given include the line above or below the cursor. */
2094 if (eap->addr_count == 0)
2095 {
2096 /* Make it possible that ":diffget" on the last line gets line below
2097 * the cursor line when there is no difference above the cursor. */
2098 if (eap->cmdidx == CMD_diffget
2099 && eap->line1 == curbuf->b_ml.ml_line_count
2100 && diff_check(curwin, eap->line1) == 0
2101 && (eap->line1 == 1 || diff_check(curwin, eap->line1 - 1) == 0))
2102 ++eap->line2;
2103 else if (eap->line1 > 0)
2104 --eap->line1;
2105 }
2106
2107 if (eap->cmdidx == CMD_diffget)
2108 {
2109 idx_from = idx_other;
2110 idx_to = idx_cur;
2111 }
2112 else
2113 {
2114 idx_from = idx_cur;
2115 idx_to = idx_other;
2116 /* Need to make the other buffer the current buffer to be able to make
2117 * changes in it. */
2118 /* set curwin/curbuf to buf and save a few things */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002119 aucmd_prepbuf(&aco, curtab->tp_diffbuf[idx_other]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 }
2121
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002122 /* May give the warning for a changed buffer here, which can trigger the
2123 * FileChangedRO autocommand, which may do nasty things and mess
2124 * everything up. */
2125 if (!curbuf->b_changed)
2126 {
2127 change_warning(0);
2128 if (diff_buf_idx(curbuf) != idx_to)
2129 {
2130 EMSG(_("E787: Buffer changed unexpectedly"));
2131 return;
2132 }
2133 }
2134
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 dprev = NULL;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002136 for (dp = curtab->tp_first_diff; dp != NULL; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 {
2138 if (dp->df_lnum[idx_cur] > eap->line2 + off)
2139 break; /* past the range that was specified */
2140
2141 dfree = NULL;
2142 lnum = dp->df_lnum[idx_to];
2143 count = dp->df_count[idx_to];
2144 if (dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off
2145 && u_save(lnum - 1, lnum + count) != FAIL)
2146 {
2147 /* Inside the specified range and saving for undo worked. */
2148 start_skip = 0;
2149 end_skip = 0;
2150 if (eap->addr_count > 0)
2151 {
2152 /* A range was specified: check if lines need to be skipped. */
2153 start_skip = eap->line1 + off - dp->df_lnum[idx_cur];
2154 if (start_skip > 0)
2155 {
2156 /* range starts below start of current diff block */
2157 if (start_skip > count)
2158 {
2159 lnum += count;
2160 count = 0;
2161 }
2162 else
2163 {
2164 count -= start_skip;
2165 lnum += start_skip;
2166 }
2167 }
2168 else
2169 start_skip = 0;
2170
2171 end_skip = dp->df_lnum[idx_cur] + dp->df_count[idx_cur] - 1
2172 - (eap->line2 + off);
2173 if (end_skip > 0)
2174 {
2175 /* range ends above end of current/from diff block */
2176 if (idx_cur == idx_from) /* :diffput */
2177 {
2178 i = dp->df_count[idx_cur] - start_skip - end_skip;
2179 if (count > i)
2180 count = i;
2181 }
2182 else /* :diffget */
2183 {
2184 count -= end_skip;
2185 end_skip = dp->df_count[idx_from] - start_skip - count;
2186 if (end_skip < 0)
2187 end_skip = 0;
2188 }
2189 }
2190 else
2191 end_skip = 0;
2192 }
2193
Bram Moolenaar280f1262006-01-30 00:14:18 +00002194 buf_empty = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 added = 0;
2196 for (i = 0; i < count; ++i)
2197 {
Bram Moolenaar280f1262006-01-30 00:14:18 +00002198 /* remember deleting the last line of the buffer */
2199 buf_empty = curbuf->b_ml.ml_line_count == 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002200 ml_delete(lnum, FALSE);
2201 --added;
2202 }
2203 for (i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; ++i)
2204 {
2205 linenr_T nr;
2206
2207 nr = dp->df_lnum[idx_from] + start_skip + i;
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002208 if (nr > curtab->tp_diffbuf[idx_from]->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002210 p = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx_from],
2211 nr, FALSE));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 if (p != NULL)
2213 {
2214 ml_append(lnum + i - 1, p, 0, FALSE);
2215 vim_free(p);
2216 ++added;
Bram Moolenaar280f1262006-01-30 00:14:18 +00002217 if (buf_empty && curbuf->b_ml.ml_line_count == 2)
2218 {
2219 /* Added the first line into an empty buffer, need to
2220 * delete the dummy empty line. */
2221 buf_empty = FALSE;
2222 ml_delete((linenr_T)2, FALSE);
2223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 }
2225 }
2226 new_count = dp->df_count[idx_to] + added;
2227 dp->df_count[idx_to] = new_count;
2228
2229 if (start_skip == 0 && end_skip == 0)
2230 {
2231 /* Check if there are any other buffers and if the diff is
2232 * equal in them. */
2233 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002234 if (curtab->tp_diffbuf[i] != NULL && i != idx_from
2235 && i != idx_to
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 && !diff_equal_entry(dp, idx_from, i))
2237 break;
2238 if (i == DB_COUNT)
2239 {
2240 /* delete the diff entry, the buffers are now equal here */
2241 dfree = dp;
2242 dp = dp->df_next;
2243 if (dprev == NULL)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002244 curtab->tp_first_diff = dp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 else
2246 dprev->df_next = dp;
2247 }
2248 }
2249
2250 /* Adjust marks. This will change the following entries! */
2251 if (added != 0)
2252 {
2253 mark_adjust(lnum, lnum + count - 1, (long)MAXLNUM, (long)added);
2254 if (curwin->w_cursor.lnum >= lnum)
2255 {
2256 /* Adjust the cursor position if it's in/after the changed
2257 * lines. */
2258 if (curwin->w_cursor.lnum >= lnum + count)
2259 curwin->w_cursor.lnum += added;
2260 else if (added < 0)
2261 curwin->w_cursor.lnum = lnum;
2262 }
2263 }
2264 changed_lines(lnum, 0, lnum + count, (long)added);
2265
2266 if (dfree != NULL)
2267 {
2268 /* Diff is deleted, update folds in other windows. */
2269#ifdef FEAT_FOLDING
2270 diff_fold_update(dfree, idx_to);
2271#endif
2272 vim_free(dfree);
2273 }
2274 else
2275 /* mark_adjust() may have changed the count in a wrong way */
2276 dp->df_count[idx_to] = new_count;
2277
2278 /* When changing the current buffer, keep track of line numbers */
2279 if (idx_cur == idx_to)
2280 off += added;
2281 }
2282
2283 /* If before the range or not deleted, go to next diff. */
2284 if (dfree == NULL)
2285 {
2286 dprev = dp;
2287 dp = dp->df_next;
2288 }
2289 }
2290
2291 /* restore curwin/curbuf and a few other things */
2292 if (idx_other == idx_to)
2293 {
2294 /* Syncing undo only works for the current buffer, but we change
2295 * another buffer. Sync undo if the command was typed. This isn't
2296 * 100% right when ":diffput" is used in a function or mapping. */
2297 if (KeyTyped)
Bram Moolenaar779b74b2006-04-10 14:55:34 +00002298 u_sync(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299 aucmd_restbuf(&aco);
2300 }
2301
2302 diff_busy = FALSE;
2303
2304 /* Check that the cursor is on a valid character and update it's position.
2305 * When there were filler lines the topline has become invalid. */
2306 check_cursor();
2307 changed_line_abv_curs();
2308
2309 /* Also need to redraw the other buffers. */
2310 diff_redraw(FALSE);
2311}
2312
2313#ifdef FEAT_FOLDING
2314/*
2315 * Update folds for all diff buffers for entry "dp".
2316 * Skip buffer with index "skip_idx".
2317 * When there are no diffs, all folds are removed.
2318 */
2319 static void
2320diff_fold_update(dp, skip_idx)
2321 diff_T *dp;
2322 int skip_idx;
2323{
2324 int i;
2325 win_T *wp;
2326
2327 for (wp = firstwin; wp != NULL; wp = wp->w_next)
2328 for (i = 0; i < DB_COUNT; ++i)
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002329 if (curtab->tp_diffbuf[i] == wp->w_buffer && i != skip_idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 foldUpdate(wp, dp->df_lnum[i],
2331 dp->df_lnum[i] + dp->df_count[i]);
2332}
2333#endif
2334
2335/*
2336 * Return TRUE if buffer "buf" is in diff-mode.
2337 */
2338 int
2339diff_mode_buf(buf)
2340 buf_T *buf;
2341{
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002342 tabpage_T *tp;
2343
2344 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
2345 if (diff_buf_idx_tp(buf, tp) != DB_COUNT)
2346 return TRUE;
2347 return FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348}
2349
2350/*
2351 * Move "count" times in direction "dir" to the next diff block.
2352 * Return FAIL if there isn't such a diff block.
2353 */
2354 int
2355diff_move_to(dir, count)
2356 int dir;
2357 long count;
2358{
2359 int idx;
2360 linenr_T lnum = curwin->w_cursor.lnum;
2361 diff_T *dp;
2362
2363 idx = diff_buf_idx(curbuf);
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002364 if (idx == DB_COUNT || curtab->tp_first_diff == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 return FAIL;
2366
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002367 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 ex_diffupdate(NULL); /* update after a big change */
2369
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002370 if (curtab->tp_first_diff == NULL) /* no diffs today */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 return FAIL;
2372
2373 while (--count >= 0)
2374 {
2375 /* Check if already before first diff. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002376 if (dir == BACKWARD && lnum <= curtab->tp_first_diff->df_lnum[idx])
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 break;
2378
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002379 for (dp = curtab->tp_first_diff; ; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 {
2381 if (dp == NULL)
2382 break;
2383 if ((dir == FORWARD && lnum < dp->df_lnum[idx])
2384 || (dir == BACKWARD
2385 && (dp->df_next == NULL
2386 || lnum <= dp->df_next->df_lnum[idx])))
2387 {
2388 lnum = dp->df_lnum[idx];
2389 break;
2390 }
2391 }
2392 }
2393
2394 /* don't end up past the end of the file */
2395 if (lnum > curbuf->b_ml.ml_line_count)
2396 lnum = curbuf->b_ml.ml_line_count;
2397
2398 /* When the cursor didn't move at all we fail. */
2399 if (lnum == curwin->w_cursor.lnum)
2400 return FAIL;
2401
2402 setpcmark();
2403 curwin->w_cursor.lnum = lnum;
2404 curwin->w_cursor.col = 0;
2405
2406 return OK;
2407}
2408
2409#if defined(FEAT_FOLDING) || defined(PROTO)
2410/*
2411 * For line "lnum" in the current window find the equivalent lnum in window
2412 * "wp", compensating for inserted/deleted lines.
2413 */
2414 linenr_T
2415diff_lnum_win(lnum, wp)
2416 linenr_T lnum;
2417 win_T *wp;
2418{
2419 diff_T *dp;
2420 int idx;
2421 int i;
2422 linenr_T n;
2423
2424 idx = diff_buf_idx(curbuf);
2425 if (idx == DB_COUNT) /* safety check */
2426 return (linenr_T)0;
2427
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002428 if (curtab->tp_diff_invalid)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 ex_diffupdate(NULL); /* update after a big change */
2430
2431 /* search for a change that includes "lnum" in the list of diffblocks. */
Bram Moolenaar49d7bf12006-02-17 21:45:41 +00002432 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
2434 break;
2435
2436 /* When after the last change, compute relative to the last line number. */
2437 if (dp == NULL)
2438 return wp->w_buffer->b_ml.ml_line_count
2439 - (curbuf->b_ml.ml_line_count - lnum);
2440
2441 /* Find index for "wp". */
2442 i = diff_buf_idx(wp->w_buffer);
2443 if (i == DB_COUNT) /* safety check */
2444 return (linenr_T)0;
2445
2446 n = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]);
2447 if (n > dp->df_lnum[i] + dp->df_count[i])
2448 n = dp->df_lnum[i] + dp->df_count[i];
2449 return n;
2450}
2451#endif
2452
2453#endif /* FEAT_DIFF */