7
|
1 /* vim:set ts=8 sts=4 sw=4:
|
|
2 *
|
|
3 * VIM - Vi IMproved by Bram Moolenaar
|
|
4 *
|
|
5 * Do ":help uganda" in Vim to read copying and usage conditions.
|
|
6 * Do ":help credits" in Vim to see a list of people who contributed.
|
|
7 * See README.txt for an overview of the Vim source code.
|
|
8 */
|
|
9
|
|
10 /*
|
|
11 * diff.c: code for diff'ing two or three buffers.
|
|
12 */
|
|
13
|
|
14 #include "vim.h"
|
|
15
|
|
16 #if defined(FEAT_DIFF) || defined(PROTO)
|
|
17
|
|
18 static int diff_busy = FALSE; /* ex_diffgetput() is busy */
|
|
19
|
|
20 /* flags obtained from the 'diffopt' option */
|
|
21 #define DIFF_FILLER 1 /* display filler lines */
|
|
22 #define DIFF_ICASE 2 /* ignore case */
|
|
23 #define DIFF_IWHITE 4 /* ignore change in white space */
|
764
|
24 #define DIFF_HORIZONTAL 8 /* horizontal splits */
|
|
25 #define DIFF_VERTICAL 16 /* vertical splits */
|
7
|
26 static int diff_flags = DIFF_FILLER;
|
|
27
|
|
28 #define LBUFLEN 50 /* length of line in diff file */
|
|
29
|
|
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
|
|
35 checked yet */
|
|
36 #endif
|
|
37
|
|
38 static int diff_buf_idx __ARGS((buf_T *buf));
|
672
|
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));
|
7
|
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));
|
|
48 #ifdef FEAT_FOLDING
|
|
49 static void diff_fold_update __ARGS((diff_T *dp, int skip_idx));
|
|
50 #endif
|
|
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));
|
672
|
53 static diff_T *diff_alloc_new __ARGS((tabpage_T *tp, diff_T *dprev, diff_T *dp));
|
7
|
54
|
|
55 #ifndef USE_CR
|
|
56 # define tag_fgets vim_fgets
|
|
57 #endif
|
|
58
|
|
59 /*
|
|
60 * Called when deleting or unloading a buffer: No longer make a diff with it.
|
|
61 */
|
|
62 void
|
|
63 diff_buf_delete(buf)
|
|
64 buf_T *buf;
|
|
65 {
|
|
66 int i;
|
672
|
67 tabpage_T *tp;
|
7
|
68
|
672
|
69 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
|
7
|
70 {
|
672
|
71 i = diff_buf_idx_tp(buf, tp);
|
|
72 if (i != DB_COUNT)
|
|
73 {
|
|
74 tp->tp_diffbuf[i] = NULL;
|
|
75 tp->tp_diff_invalid = TRUE;
|
|
76 }
|
7
|
77 }
|
|
78 }
|
|
79
|
|
80 /*
|
16
|
81 * Check if the current buffer should be added to or removed from the list of
|
|
82 * diff buffers.
|
|
83 */
|
|
84 void
|
|
85 diff_buf_adjust(win)
|
|
86 win_T *win;
|
|
87 {
|
|
88 win_T *wp;
|
672
|
89 int i;
|
16
|
90
|
|
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)
|
672
|
99 {
|
|
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 }
|
16
|
107 }
|
|
108 else
|
|
109 diff_buf_add(win->w_buffer);
|
|
110 }
|
|
111
|
|
112 /*
|
7
|
113 * Add a buffer to make diffs for.
|
672
|
114 * 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.
|
7
|
119 */
|
|
120 void
|
|
121 diff_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)
|
672
|
130 if (curtab->tp_diffbuf[i] == NULL)
|
7
|
131 {
|
672
|
132 curtab->tp_diffbuf[i] = buf;
|
|
133 curtab->tp_diff_invalid = TRUE;
|
7
|
134 return;
|
|
135 }
|
|
136
|
|
137 EMSGN(_("E96: Can not diff more than %ld buffers"), DB_COUNT);
|
|
138 }
|
|
139
|
|
140 /*
|
672
|
141 * Find buffer "buf" in the list of diff buffers for the current tab page.
|
7
|
142 * Return its index or DB_COUNT if not found.
|
|
143 */
|
|
144 static int
|
|
145 diff_buf_idx(buf)
|
|
146 buf_T *buf;
|
|
147 {
|
|
148 int idx;
|
|
149
|
|
150 for (idx = 0; idx < DB_COUNT; ++idx)
|
672
|
151 if (curtab->tp_diffbuf[idx] == buf)
|
|
152 break;
|
|
153 return idx;
|
|
154 }
|
|
155
|
|
156 /*
|
|
157 * 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
|
|
161 diff_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)
|
7
|
169 break;
|
|
170 return idx;
|
|
171 }
|
|
172
|
|
173 /*
|
672
|
174 * Mark the diff info involving buffer "buf" as invalid, it will be updated
|
|
175 * when info is requested.
|
7
|
176 */
|
|
177 void
|
672
|
178 diff_invalidate(buf)
|
|
179 buf_T *buf;
|
7
|
180 {
|
672
|
181 tabpage_T *tp;
|
|
182 int i;
|
|
183
|
|
184 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
|
7
|
185 {
|
672
|
186 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 }
|
7
|
193 }
|
|
194 }
|
|
195
|
|
196 /*
|
672
|
197 * Called by mark_adjust(): update line numbers in "curbuf".
|
7
|
198 */
|
|
199 void
|
|
200 diff_mark_adjust(line1, line2, amount, amount_after)
|
|
201 linenr_T line1;
|
|
202 linenr_T line2;
|
|
203 long amount;
|
|
204 long amount_after;
|
|
205 {
|
672
|
206 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
|
|
226 diff_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 {
|
7
|
234 diff_T *dp;
|
|
235 diff_T *dprev;
|
|
236 diff_T *dnext;
|
|
237 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
|
|
244 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;
|
672
|
264 dp = tp->tp_first_diff;
|
7
|
265 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 {
|
672
|
276 dnext = diff_alloc_new(tp, dprev, dp);
|
7
|
277 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)
|
672
|
283 if (tp->tp_diffbuf[i] != NULL && i != idx)
|
7
|
284 {
|
|
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)
|
672
|
390 if (tp->tp_diffbuf[i] != NULL && i != idx)
|
7
|
391 {
|
|
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. */
|
672
|
413 diff_check_unchanged(tp, dp);
|
7
|
414 }
|
|
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)
|
672
|
422 if (tp->tp_diffbuf[i] != NULL)
|
7
|
423 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;
|
672
|
437 dp = tp->tp_first_diff;
|
7
|
438 while (dp != NULL)
|
|
439 {
|
|
440 /* All counts are zero, remove this entry. */
|
|
441 for (i = 0; i < DB_COUNT; ++i)
|
672
|
442 if (tp->tp_diffbuf[i] != NULL && dp->df_count[i] != 0)
|
7
|
443 break;
|
|
444 if (i == DB_COUNT)
|
|
445 {
|
|
446 dnext = dp->df_next;
|
|
447 vim_free(dp);
|
|
448 dp = dnext;
|
|
449 if (dprev == NULL)
|
672
|
450 tp->tp_first_diff = dnext;
|
7
|
451 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 }
|
672
|
462
|
|
463 if (tp == curtab)
|
|
464 {
|
|
465 diff_redraw(TRUE);
|
7
|
466
|
672
|
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 }
|
7
|
472 }
|
|
473
|
|
474 /*
|
|
475 * Allocate a new diff block and link it between "dprev" and "dp".
|
|
476 */
|
|
477 static diff_T *
|
672
|
478 diff_alloc_new(tp, dprev, dp)
|
|
479 tabpage_T *tp;
|
7
|
480 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)
|
672
|
490 tp->tp_first_diff = dnew;
|
7
|
491 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
|
672
|
504 diff_check_unchanged(tp, dp)
|
|
505 tabpage_T *tp;
|
7
|
506 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)
|
672
|
517 if (tp->tp_diffbuf[i_org] != NULL)
|
7
|
518 break;
|
|
519 if (i_org == DB_COUNT) /* safety check */
|
|
520 return;
|
|
521
|
672
|
522 if (diff_check_sanity(tp, dp) == FAIL)
|
7
|
523 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;
|
672
|
537 line_org = vim_strsave(ml_get_buf(tp->tp_diffbuf[i_org],
|
7
|
538 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 {
|
672
|
543 if (tp->tp_diffbuf[i_new] == NULL)
|
7
|
544 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;
|
672
|
550 if (diff_cmp(line_org, ml_get_buf(tp->tp_diffbuf[i_new],
|
7
|
551 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)
|
672
|
562 if (tp->tp_diffbuf[i_new] != NULL)
|
7
|
563 {
|
|
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
|
672
|
580 diff_check_sanity(tp, dp)
|
|
581 tabpage_T *tp;
|
7
|
582 diff_T *dp;
|
|
583 {
|
|
584 int i;
|
|
585
|
|
586 for (i = 0; i < DB_COUNT; ++i)
|
672
|
587 if (tp->tp_diffbuf[i] != NULL)
|
7
|
588 if (dp->df_lnum[i] + dp->df_count[i] - 1
|
672
|
589 > tp->tp_diffbuf[i]->b_ml.ml_line_count)
|
7
|
590 return FAIL;
|
|
591 return OK;
|
|
592 }
|
|
593
|
|
594 /*
|
672
|
595 * Mark all diff buffers in the current tab page for redraw.
|
7
|
596 */
|
|
597 static void
|
|
598 diff_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 {
|
738
|
607 redraw_win_later(wp, SOME_VALID);
|
7
|
608 #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
|
|
629 diff_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
|
|
653 ex_diffupdate(eap)
|
672
|
654 exarg_T *eap; /* can be NULL, it's not used */
|
7
|
655 {
|
|
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;
|
1757
|
664 int io_error = FALSE;
|
7
|
665
|
|
666 /* Delete all diffblocks. */
|
672
|
667 diff_clear(curtab);
|
|
668 curtab->tp_diff_invalid = FALSE;
|
7
|
669
|
|
670 /* Use the first buffer as the original text. */
|
|
671 for (idx_orig = 0; idx_orig < DB_COUNT; ++idx_orig)
|
672
|
672 if (curtab->tp_diffbuf[idx_orig] != NULL)
|
7
|
673 break;
|
|
674 if (idx_orig == DB_COUNT)
|
|
675 return;
|
|
676
|
|
677 /* Only need to do something when there is another buffer. */
|
|
678 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
|
672
|
679 if (curtab->tp_diffbuf[idx_new] != NULL)
|
7
|
680 break;
|
|
681 if (idx_new == DB_COUNT)
|
|
682 return;
|
|
683
|
|
684 /* We need three temp file names. */
|
|
685 tmp_orig = vim_tempname('o');
|
|
686 tmp_new = vim_tempname('n');
|
|
687 tmp_diff = vim_tempname('d');
|
|
688 if (tmp_orig == NULL || tmp_new == NULL || tmp_diff == NULL)
|
|
689 goto theend;
|
|
690
|
|
691 /*
|
|
692 * Do a quick test if "diff" really works. Otherwise it looks like there
|
|
693 * are no differences. Can't use the return value, it's non-zero when
|
|
694 * there are differences.
|
|
695 * May try twice, first with "-a" and then without.
|
|
696 */
|
|
697 for (;;)
|
|
698 {
|
|
699 ok = FALSE;
|
531
|
700 fd = mch_fopen((char *)tmp_orig, "w");
|
1757
|
701 if (fd == NULL)
|
|
702 io_error = TRUE;
|
|
703 else
|
7
|
704 {
|
1757
|
705 if (fwrite("line1\n", (size_t)6, (size_t)1, fd) != 1)
|
|
706 io_error = TRUE;
|
7
|
707 fclose(fd);
|
531
|
708 fd = mch_fopen((char *)tmp_new, "w");
|
1757
|
709 if (fd == NULL)
|
|
710 io_error = TRUE;
|
|
711 else
|
7
|
712 {
|
1757
|
713 if (fwrite("line2\n", (size_t)6, (size_t)1, fd) != 1)
|
|
714 io_error = TRUE;
|
7
|
715 fclose(fd);
|
|
716 diff_file(tmp_orig, tmp_new, tmp_diff);
|
531
|
717 fd = mch_fopen((char *)tmp_diff, "r");
|
1757
|
718 if (fd == NULL)
|
|
719 io_error = TRUE;
|
|
720 else
|
7
|
721 {
|
|
722 char_u linebuf[LBUFLEN];
|
|
723
|
|
724 for (;;)
|
|
725 {
|
|
726 /* There must be a line that contains "1c1". */
|
|
727 if (tag_fgets(linebuf, LBUFLEN, fd))
|
|
728 break;
|
|
729 if (STRNCMP(linebuf, "1c1", 3) == 0)
|
|
730 ok = TRUE;
|
|
731 }
|
|
732 fclose(fd);
|
|
733 }
|
|
734 mch_remove(tmp_diff);
|
|
735 mch_remove(tmp_new);
|
|
736 }
|
|
737 mch_remove(tmp_orig);
|
|
738 }
|
|
739
|
|
740 #ifdef FEAT_EVAL
|
|
741 /* When using 'diffexpr' break here. */
|
|
742 if (*p_dex != NUL)
|
|
743 break;
|
|
744 #endif
|
|
745
|
|
746 #if defined(MSWIN) || defined(MSDOS)
|
|
747 /* If the "-a" argument works, also check if "--binary" works. */
|
|
748 if (ok && diff_a_works == MAYBE && diff_bin_works == MAYBE)
|
|
749 {
|
|
750 diff_a_works = TRUE;
|
|
751 diff_bin_works = TRUE;
|
|
752 continue;
|
|
753 }
|
|
754 if (!ok && diff_a_works == TRUE && diff_bin_works == TRUE)
|
|
755 {
|
|
756 /* Tried --binary, but it failed. "-a" works though. */
|
|
757 diff_bin_works = FALSE;
|
|
758 ok = TRUE;
|
|
759 }
|
|
760 #endif
|
|
761
|
|
762 /* If we checked if "-a" works already, break here. */
|
|
763 if (diff_a_works != MAYBE)
|
|
764 break;
|
|
765 diff_a_works = ok;
|
|
766
|
|
767 /* If "-a" works break here, otherwise retry without "-a". */
|
|
768 if (ok)
|
|
769 break;
|
|
770 }
|
|
771 if (!ok)
|
|
772 {
|
1757
|
773 if (io_error)
|
|
774 EMSG(_("E810: Cannot read or write temp files"));
|
7
|
775 EMSG(_("E97: Cannot create diffs"));
|
|
776 diff_a_works = MAYBE;
|
|
777 #if defined(MSWIN) || defined(MSDOS)
|
|
778 diff_bin_works = MAYBE;
|
|
779 #endif
|
|
780 goto theend;
|
|
781 }
|
|
782
|
|
783 /* Write the first buffer to a tempfile. */
|
672
|
784 buf = curtab->tp_diffbuf[idx_orig];
|
7
|
785 if (diff_write(buf, tmp_orig) == FAIL)
|
|
786 goto theend;
|
|
787
|
|
788 /* Make a difference between the first buffer and every other. */
|
|
789 for (idx_new = idx_orig + 1; idx_new < DB_COUNT; ++idx_new)
|
|
790 {
|
672
|
791 buf = curtab->tp_diffbuf[idx_new];
|
7
|
792 if (buf == NULL)
|
|
793 continue;
|
|
794 if (diff_write(buf, tmp_new) == FAIL)
|
|
795 continue;
|
|
796 diff_file(tmp_orig, tmp_new, tmp_diff);
|
|
797
|
|
798 /* Read the diff output and add each entry to the diff list. */
|
|
799 diff_read(idx_orig, idx_new, tmp_diff);
|
|
800 mch_remove(tmp_diff);
|
|
801 mch_remove(tmp_new);
|
|
802 }
|
|
803 mch_remove(tmp_orig);
|
|
804
|
1429
|
805 /* force updating cursor position on screen */
|
|
806 curwin->w_valid_cursor.lnum = 0;
|
|
807
|
7
|
808 diff_redraw(TRUE);
|
|
809
|
|
810 theend:
|
|
811 vim_free(tmp_orig);
|
|
812 vim_free(tmp_new);
|
|
813 vim_free(tmp_diff);
|
|
814 }
|
|
815
|
|
816 /*
|
|
817 * Make a diff between files "tmp_orig" and "tmp_new", results in "tmp_diff".
|
|
818 */
|
|
819 static void
|
|
820 diff_file(tmp_orig, tmp_new, tmp_diff)
|
|
821 char_u *tmp_orig;
|
|
822 char_u *tmp_new;
|
|
823 char_u *tmp_diff;
|
|
824 {
|
|
825 char_u *cmd;
|
|
826
|
|
827 #ifdef FEAT_EVAL
|
|
828 if (*p_dex != NUL)
|
|
829 /* Use 'diffexpr' to generate the diff file. */
|
|
830 eval_diff(tmp_orig, tmp_new, tmp_diff);
|
|
831 else
|
|
832 #endif
|
|
833 {
|
|
834 cmd = alloc((unsigned)(STRLEN(tmp_orig) + STRLEN(tmp_new)
|
|
835 + STRLEN(tmp_diff) + STRLEN(p_srr) + 27));
|
|
836 if (cmd != NULL)
|
|
837 {
|
193
|
838 /* We don't want $DIFF_OPTIONS to get in the way. */
|
|
839 if (getenv("DIFF_OPTIONS"))
|
|
840 vim_setenv((char_u *)"DIFF_OPTIONS", (char_u *)"");
|
|
841
|
7
|
842 /* Build the diff command and execute it. Always use -a, binary
|
|
843 * differences are of no use. Ignore errors, diff returns
|
|
844 * non-zero when differences have been found. */
|
|
845 sprintf((char *)cmd, "diff %s%s%s%s%s %s",
|
|
846 diff_a_works == FALSE ? "" : "-a ",
|
|
847 #if defined(MSWIN) || defined(MSDOS)
|
|
848 diff_bin_works == TRUE ? "--binary " : "",
|
|
849 #else
|
|
850 "",
|
|
851 #endif
|
|
852 (diff_flags & DIFF_IWHITE) ? "-b " : "",
|
|
853 (diff_flags & DIFF_ICASE) ? "-i " : "",
|
|
854 tmp_orig, tmp_new);
|
|
855 append_redir(cmd, p_srr, tmp_diff);
|
828
|
856 #ifdef FEAT_AUTOCMD
|
1410
|
857 block_autocmds(); /* Avoid ShellCmdPost stuff */
|
828
|
858 #endif
|
7
|
859 (void)call_shell(cmd, SHELL_FILTER|SHELL_SILENT|SHELL_DOOUT);
|
828
|
860 #ifdef FEAT_AUTOCMD
|
1410
|
861 unblock_autocmds();
|
828
|
862 #endif
|
7
|
863 vim_free(cmd);
|
|
864 }
|
|
865 }
|
|
866 }
|
|
867
|
|
868 /*
|
|
869 * Create a new version of a file from the current buffer and a diff file.
|
|
870 * The buffer is written to a file, also for unmodified buffers (the file
|
|
871 * could have been produced by autocommands, e.g. the netrw plugin).
|
|
872 */
|
|
873 void
|
|
874 ex_diffpatch(eap)
|
|
875 exarg_T *eap;
|
|
876 {
|
|
877 char_u *tmp_orig; /* name of original temp file */
|
|
878 char_u *tmp_new; /* name of patched temp file */
|
|
879 char_u *buf = NULL;
|
|
880 win_T *old_curwin = curwin;
|
|
881 char_u *newname = NULL; /* name of patched file buffer */
|
|
882 #ifdef UNIX
|
|
883 char_u dirbuf[MAXPATHL];
|
|
884 char_u *fullname = NULL;
|
|
885 #endif
|
|
886 #ifdef FEAT_BROWSE
|
|
887 char_u *browseFile = NULL;
|
|
888 int browse_flag = cmdmod.browse;
|
|
889 #endif
|
|
890
|
|
891 #ifdef FEAT_BROWSE
|
|
892 if (cmdmod.browse)
|
|
893 {
|
28
|
894 browseFile = do_browse(0, (char_u *)_("Patch file"),
|
7
|
895 eap->arg, NULL, NULL, BROWSE_FILTER_ALL_FILES, NULL);
|
|
896 if (browseFile == NULL)
|
|
897 return; /* operation cancelled */
|
|
898 eap->arg = browseFile;
|
|
899 cmdmod.browse = FALSE; /* don't let do_ecmd() browse again */
|
|
900 }
|
|
901 #endif
|
|
902
|
|
903 /* We need two temp file names. */
|
|
904 tmp_orig = vim_tempname('o');
|
|
905 tmp_new = vim_tempname('n');
|
|
906 if (tmp_orig == NULL || tmp_new == NULL)
|
|
907 goto theend;
|
|
908
|
|
909 /* Write the current buffer to "tmp_orig". */
|
|
910 if (buf_write(curbuf, tmp_orig, NULL,
|
|
911 (linenr_T)1, curbuf->b_ml.ml_line_count,
|
|
912 NULL, FALSE, FALSE, FALSE, TRUE) == FAIL)
|
|
913 goto theend;
|
|
914
|
|
915 #ifdef UNIX
|
|
916 /* Get the absolute path of the patchfile, changing directory below. */
|
|
917 fullname = FullName_save(eap->arg, FALSE);
|
|
918 #endif
|
|
919 buf = alloc((unsigned)(STRLEN(tmp_orig) + (
|
|
920 # ifdef UNIX
|
|
921 fullname != NULL ? STRLEN(fullname) :
|
|
922 # endif
|
|
923 STRLEN(eap->arg)) + STRLEN(tmp_new) + 16));
|
|
924 if (buf == NULL)
|
|
925 goto theend;
|
|
926
|
|
927 #ifdef UNIX
|
|
928 /* Temporaraly chdir to /tmp, to avoid patching files in the current
|
|
929 * directory when the patch file contains more than one patch. When we
|
|
930 * have our own temp dir use that instead, it will be cleaned up when we
|
|
931 * exit (any .rej files created). Don't change directory if we can't
|
|
932 * return to the current. */
|
|
933 if (mch_dirname(dirbuf, MAXPATHL) != OK || mch_chdir((char *)dirbuf) != 0)
|
|
934 dirbuf[0] = NUL;
|
|
935 else
|
|
936 {
|
|
937 # ifdef TEMPDIRNAMES
|
|
938 if (vim_tempdir != NULL)
|
1757
|
939 ignored = mch_chdir((char *)vim_tempdir);
|
7
|
940 else
|
|
941 # endif
|
1757
|
942 ignored = mch_chdir("/tmp");
|
7
|
943 shorten_fnames(TRUE);
|
|
944 }
|
|
945 #endif
|
|
946
|
|
947 #ifdef FEAT_EVAL
|
|
948 if (*p_pex != NUL)
|
|
949 /* Use 'patchexpr' to generate the new file. */
|
|
950 eval_patch(tmp_orig,
|
|
951 # ifdef UNIX
|
|
952 fullname != NULL ? fullname :
|
|
953 # endif
|
|
954 eap->arg, tmp_new);
|
|
955 else
|
|
956 #endif
|
|
957 {
|
|
958 /* Build the patch command and execute it. Ignore errors. Switch to
|
|
959 * cooked mode to allow the user to respond to prompts. */
|
|
960 sprintf((char *)buf, "patch -o %s %s < \"%s\"", tmp_new, tmp_orig,
|
|
961 # ifdef UNIX
|
|
962 fullname != NULL ? fullname :
|
|
963 # endif
|
|
964 eap->arg);
|
828
|
965 #ifdef FEAT_AUTOCMD
|
1410
|
966 block_autocmds(); /* Avoid ShellCmdPost stuff */
|
828
|
967 #endif
|
7
|
968 (void)call_shell(buf, SHELL_FILTER | SHELL_COOKED);
|
828
|
969 #ifdef FEAT_AUTOCMD
|
1410
|
970 unblock_autocmds();
|
828
|
971 #endif
|
7
|
972 }
|
|
973
|
|
974 #ifdef UNIX
|
|
975 if (dirbuf[0] != NUL)
|
|
976 {
|
|
977 if (mch_chdir((char *)dirbuf) != 0)
|
|
978 EMSG(_(e_prev_dir));
|
|
979 shorten_fnames(TRUE);
|
|
980 }
|
|
981 #endif
|
|
982
|
|
983 /* patch probably has written over the screen */
|
|
984 redraw_later(CLEAR);
|
|
985
|
|
986 /* Delete any .orig or .rej file created. */
|
|
987 STRCPY(buf, tmp_new);
|
|
988 STRCAT(buf, ".orig");
|
|
989 mch_remove(buf);
|
|
990 STRCPY(buf, tmp_new);
|
|
991 STRCAT(buf, ".rej");
|
|
992 mch_remove(buf);
|
|
993
|
|
994 if (curbuf->b_fname != NULL)
|
|
995 {
|
|
996 newname = vim_strnsave(curbuf->b_fname,
|
|
997 (int)(STRLEN(curbuf->b_fname) + 4));
|
|
998 if (newname != NULL)
|
|
999 STRCAT(newname, ".new");
|
|
1000 }
|
|
1001
|
|
1002 #ifdef FEAT_GUI
|
|
1003 need_mouse_correct = TRUE;
|
|
1004 #endif
|
682
|
1005 /* don't use a new tab page, each tab page has its own diffs */
|
|
1006 cmdmod.tab = 0;
|
|
1007
|
764
|
1008 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
|
7
|
1009 {
|
|
1010 /* Pretend it was a ":split fname" command */
|
|
1011 eap->cmdidx = CMD_split;
|
|
1012 eap->arg = tmp_new;
|
|
1013 do_exedit(eap, old_curwin);
|
|
1014
|
|
1015 if (curwin != old_curwin) /* split must have worked */
|
|
1016 {
|
|
1017 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
|
|
1018 diff_win_options(curwin, TRUE);
|
|
1019 diff_win_options(old_curwin, TRUE);
|
|
1020
|
|
1021 if (newname != NULL)
|
|
1022 {
|
|
1023 /* do a ":file filename.new" on the patched buffer */
|
|
1024 eap->arg = newname;
|
|
1025 ex_file(eap);
|
|
1026
|
|
1027 #ifdef FEAT_AUTOCMD
|
|
1028 /* Do filetype detection with the new name. */
|
819
|
1029 if (au_has_group((char_u *)"filetypedetect"))
|
|
1030 do_cmdline_cmd((char_u *)":doau filetypedetect BufRead");
|
7
|
1031 #endif
|
|
1032 }
|
|
1033 }
|
|
1034 }
|
|
1035
|
|
1036 theend:
|
|
1037 if (tmp_orig != NULL)
|
|
1038 mch_remove(tmp_orig);
|
|
1039 vim_free(tmp_orig);
|
|
1040 if (tmp_new != NULL)
|
|
1041 mch_remove(tmp_new);
|
|
1042 vim_free(tmp_new);
|
|
1043 vim_free(newname);
|
|
1044 vim_free(buf);
|
|
1045 #ifdef UNIX
|
|
1046 vim_free(fullname);
|
|
1047 #endif
|
|
1048 #ifdef FEAT_BROWSE
|
|
1049 vim_free(browseFile);
|
|
1050 cmdmod.browse = browse_flag;
|
|
1051 #endif
|
|
1052 }
|
|
1053
|
|
1054 /*
|
|
1055 * Split the window and edit another file, setting options to show the diffs.
|
|
1056 */
|
|
1057 void
|
|
1058 ex_diffsplit(eap)
|
|
1059 exarg_T *eap;
|
|
1060 {
|
|
1061 win_T *old_curwin = curwin;
|
|
1062
|
|
1063 #ifdef FEAT_GUI
|
|
1064 need_mouse_correct = TRUE;
|
|
1065 #endif
|
682
|
1066 /* don't use a new tab page, each tab page has its own diffs */
|
|
1067 cmdmod.tab = 0;
|
|
1068
|
764
|
1069 if (win_split(0, (diff_flags & DIFF_VERTICAL) ? WSP_VERT : 0) != FAIL)
|
7
|
1070 {
|
|
1071 /* Pretend it was a ":split fname" command */
|
|
1072 eap->cmdidx = CMD_split;
|
449
|
1073 curwin->w_p_diff = TRUE;
|
7
|
1074 do_exedit(eap, old_curwin);
|
|
1075
|
|
1076 if (curwin != old_curwin) /* split must have worked */
|
|
1077 {
|
|
1078 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
|
|
1079 diff_win_options(curwin, TRUE);
|
|
1080 diff_win_options(old_curwin, TRUE);
|
|
1081 }
|
|
1082 }
|
|
1083 }
|
|
1084
|
|
1085 /*
|
|
1086 * Set options to show difs for the current window.
|
|
1087 */
|
|
1088 /*ARGSUSED*/
|
|
1089 void
|
|
1090 ex_diffthis(eap)
|
|
1091 exarg_T *eap;
|
|
1092 {
|
|
1093 /* Set 'diff', 'scrollbind' on and 'wrap' off. */
|
|
1094 diff_win_options(curwin, TRUE);
|
|
1095 }
|
|
1096
|
|
1097 /*
|
|
1098 * Set options in window "wp" for diff mode.
|
|
1099 */
|
|
1100 void
|
|
1101 diff_win_options(wp, addbuf)
|
|
1102 win_T *wp;
|
|
1103 int addbuf; /* Add buffer to diff. */
|
|
1104 {
|
|
1105 wp->w_p_diff = TRUE;
|
|
1106 wp->w_p_scb = TRUE;
|
|
1107 wp->w_p_wrap = FALSE;
|
|
1108 # ifdef FEAT_FOLDING
|
|
1109 {
|
|
1110 win_T *old_curwin = curwin;
|
|
1111
|
|
1112 curwin = wp;
|
|
1113 curbuf = curwin->w_buffer;
|
|
1114 set_string_option_direct((char_u *)"fdm", -1, (char_u *)"diff",
|
694
|
1115 OPT_LOCAL|OPT_FREE, 0);
|
7
|
1116 curwin = old_curwin;
|
|
1117 curbuf = curwin->w_buffer;
|
764
|
1118 wp->w_p_fdc = diff_foldcolumn;
|
7
|
1119 wp->w_p_fen = TRUE;
|
|
1120 wp->w_p_fdl = 0;
|
|
1121 foldUpdateAll(wp);
|
16
|
1122 /* make sure topline is not halfway a fold */
|
|
1123 changed_window_setting_win(wp);
|
7
|
1124 }
|
|
1125 # endif
|
|
1126 #ifdef FEAT_SCROLLBIND
|
|
1127 if (vim_strchr(p_sbo, 'h') == NULL)
|
|
1128 do_cmdline_cmd((char_u *)"set sbo+=hor");
|
|
1129 #endif
|
|
1130
|
|
1131 if (addbuf)
|
|
1132 diff_buf_add(wp->w_buffer);
|
|
1133 redraw_win_later(wp, NOT_VALID);
|
|
1134 }
|
|
1135
|
|
1136 /*
|
16
|
1137 * Set options not to show diffs. For the current window or all windows.
|
671
|
1138 * Only in the current tab page.
|
16
|
1139 */
|
|
1140 void
|
|
1141 ex_diffoff(eap)
|
|
1142 exarg_T *eap;
|
|
1143 {
|
|
1144 win_T *wp;
|
|
1145 win_T *old_curwin = curwin;
|
|
1146 #ifdef FEAT_SCROLLBIND
|
|
1147 int diffwin = FALSE;
|
|
1148 #endif
|
|
1149
|
|
1150 for (wp = firstwin; wp != NULL; wp = wp->w_next)
|
|
1151 {
|
|
1152 if (wp == curwin || eap->forceit)
|
|
1153 {
|
|
1154 /* Set 'diff', 'scrollbind' off and 'wrap' on. */
|
|
1155 wp->w_p_diff = FALSE;
|
|
1156 wp->w_p_scb = FALSE;
|
|
1157 wp->w_p_wrap = TRUE;
|
|
1158 #ifdef FEAT_FOLDING
|
|
1159 curwin = wp;
|
|
1160 curbuf = curwin->w_buffer;
|
|
1161 set_string_option_direct((char_u *)"fdm", -1,
|
694
|
1162 (char_u *)"manual", OPT_LOCAL|OPT_FREE, 0);
|
16
|
1163 curwin = old_curwin;
|
|
1164 curbuf = curwin->w_buffer;
|
|
1165 wp->w_p_fdc = 0;
|
|
1166 wp->w_p_fen = FALSE;
|
|
1167 wp->w_p_fdl = 0;
|
|
1168 foldUpdateAll(wp);
|
|
1169 /* make sure topline is not halfway a fold */
|
|
1170 changed_window_setting_win(wp);
|
|
1171 #endif
|
|
1172 diff_buf_adjust(wp);
|
|
1173 }
|
|
1174 #ifdef FEAT_SCROLLBIND
|
|
1175 diffwin |= wp->w_p_diff;
|
|
1176 #endif
|
|
1177 }
|
|
1178
|
|
1179 #ifdef FEAT_SCROLLBIND
|
|
1180 /* Remove "hor" from from 'scrollopt' if there are no diff windows left. */
|
|
1181 if (!diffwin && vim_strchr(p_sbo, 'h') != NULL)
|
|
1182 do_cmdline_cmd((char_u *)"set sbo-=hor");
|
|
1183 #endif
|
|
1184 }
|
|
1185
|
|
1186 /*
|
7
|
1187 * Read the diff output and add each entry to the diff list.
|
|
1188 */
|
|
1189 static void
|
|
1190 diff_read(idx_orig, idx_new, fname)
|
|
1191 int idx_orig; /* idx of original file */
|
|
1192 int idx_new; /* idx of new file */
|
|
1193 char_u *fname; /* name of diff output file */
|
|
1194 {
|
|
1195 FILE *fd;
|
|
1196 diff_T *dprev = NULL;
|
672
|
1197 diff_T *dp = curtab->tp_first_diff;
|
7
|
1198 diff_T *dn, *dpl;
|
|
1199 long f1, l1, f2, l2;
|
|
1200 char_u linebuf[LBUFLEN]; /* only need to hold the diff line */
|
|
1201 int difftype;
|
|
1202 char_u *p;
|
|
1203 long off;
|
|
1204 int i;
|
|
1205 linenr_T lnum_orig, lnum_new;
|
|
1206 long count_orig, count_new;
|
|
1207 int notset = TRUE; /* block "*dp" not set yet */
|
|
1208
|
531
|
1209 fd = mch_fopen((char *)fname, "r");
|
7
|
1210 if (fd == NULL)
|
|
1211 {
|
|
1212 EMSG(_("E98: Cannot read diff output"));
|
|
1213 return;
|
|
1214 }
|
|
1215
|
|
1216 for (;;)
|
|
1217 {
|
|
1218 if (tag_fgets(linebuf, LBUFLEN, fd))
|
|
1219 break; /* end of file */
|
|
1220 if (!isdigit(*linebuf))
|
|
1221 continue; /* not the start of a diff block */
|
|
1222
|
|
1223 /* This line must be one of three formats:
|
|
1224 * {first}[,{last}]c{first}[,{last}]
|
|
1225 * {first}a{first}[,{last}]
|
|
1226 * {first}[,{last}]d{first}
|
|
1227 */
|
|
1228 p = linebuf;
|
|
1229 f1 = getdigits(&p);
|
|
1230 if (*p == ',')
|
|
1231 {
|
|
1232 ++p;
|
|
1233 l1 = getdigits(&p);
|
|
1234 }
|
|
1235 else
|
|
1236 l1 = f1;
|
|
1237 if (*p != 'a' && *p != 'c' && *p != 'd')
|
|
1238 continue; /* invalid diff format */
|
|
1239 difftype = *p++;
|
|
1240 f2 = getdigits(&p);
|
|
1241 if (*p == ',')
|
|
1242 {
|
|
1243 ++p;
|
|
1244 l2 = getdigits(&p);
|
|
1245 }
|
|
1246 else
|
|
1247 l2 = f2;
|
|
1248 if (l1 < f1 || l2 < f2)
|
|
1249 continue; /* invalid line range */
|
|
1250
|
|
1251 if (difftype == 'a')
|
|
1252 {
|
|
1253 lnum_orig = f1 + 1;
|
|
1254 count_orig = 0;
|
|
1255 }
|
|
1256 else
|
|
1257 {
|
|
1258 lnum_orig = f1;
|
|
1259 count_orig = l1 - f1 + 1;
|
|
1260 }
|
|
1261 if (difftype == 'd')
|
|
1262 {
|
|
1263 lnum_new = f2 + 1;
|
|
1264 count_new = 0;
|
|
1265 }
|
|
1266 else
|
|
1267 {
|
|
1268 lnum_new = f2;
|
|
1269 count_new = l2 - f2 + 1;
|
|
1270 }
|
|
1271
|
|
1272 /* Go over blocks before the change, for which orig and new are equal.
|
|
1273 * Copy blocks from orig to new. */
|
|
1274 while (dp != NULL
|
|
1275 && lnum_orig > dp->df_lnum[idx_orig] + dp->df_count[idx_orig])
|
|
1276 {
|
|
1277 if (notset)
|
|
1278 diff_copy_entry(dprev, dp, idx_orig, idx_new);
|
|
1279 dprev = dp;
|
|
1280 dp = dp->df_next;
|
|
1281 notset = TRUE;
|
|
1282 }
|
|
1283
|
|
1284 if (dp != NULL
|
|
1285 && lnum_orig <= dp->df_lnum[idx_orig] + dp->df_count[idx_orig]
|
|
1286 && lnum_orig + count_orig >= dp->df_lnum[idx_orig])
|
|
1287 {
|
|
1288 /* New block overlaps with existing block(s).
|
|
1289 * First find last block that overlaps. */
|
|
1290 for (dpl = dp; dpl->df_next != NULL; dpl = dpl->df_next)
|
|
1291 if (lnum_orig + count_orig < dpl->df_next->df_lnum[idx_orig])
|
|
1292 break;
|
|
1293
|
|
1294 /* If the newly found block starts before the old one, set the
|
|
1295 * start back a number of lines. */
|
|
1296 off = dp->df_lnum[idx_orig] - lnum_orig;
|
|
1297 if (off > 0)
|
|
1298 {
|
|
1299 for (i = idx_orig; i < idx_new; ++i)
|
672
|
1300 if (curtab->tp_diffbuf[i] != NULL)
|
7
|
1301 dp->df_lnum[i] -= off;
|
|
1302 dp->df_lnum[idx_new] = lnum_new;
|
|
1303 dp->df_count[idx_new] = count_new;
|
|
1304 }
|
|
1305 else if (notset)
|
|
1306 {
|
|
1307 /* new block inside existing one, adjust new block */
|
|
1308 dp->df_lnum[idx_new] = lnum_new + off;
|
|
1309 dp->df_count[idx_new] = count_new - off;
|
|
1310 }
|
|
1311 else
|
|
1312 /* second overlap of new block with existing block */
|
1519
|
1313 dp->df_count[idx_new] += count_new - count_orig
|
|
1314 + dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]
|
|
1315 - (dp->df_lnum[idx_orig] + dp->df_count[idx_orig]);
|
7
|
1316
|
|
1317 /* Adjust the size of the block to include all the lines to the
|
|
1318 * end of the existing block or the new diff, whatever ends last. */
|
|
1319 off = (lnum_orig + count_orig)
|
|
1320 - (dpl->df_lnum[idx_orig] + dpl->df_count[idx_orig]);
|
|
1321 if (off < 0)
|
|
1322 {
|
|
1323 /* new change ends in existing block, adjust the end if not
|
|
1324 * done already */
|
|
1325 if (notset)
|
|
1326 dp->df_count[idx_new] += -off;
|
|
1327 off = 0;
|
|
1328 }
|
1428
|
1329 for (i = idx_orig; i < idx_new; ++i)
|
672
|
1330 if (curtab->tp_diffbuf[i] != NULL)
|
7
|
1331 dp->df_count[i] = dpl->df_lnum[i] + dpl->df_count[i]
|
|
1332 - dp->df_lnum[i] + off;
|
|
1333
|
|
1334 /* Delete the diff blocks that have been merged into one. */
|
|
1335 dn = dp->df_next;
|
|
1336 dp->df_next = dpl->df_next;
|
|
1337 while (dn != dp->df_next)
|
|
1338 {
|
|
1339 dpl = dn->df_next;
|
|
1340 vim_free(dn);
|
|
1341 dn = dpl;
|
|
1342 }
|
|
1343 }
|
|
1344 else
|
|
1345 {
|
|
1346 /* Allocate a new diffblock. */
|
672
|
1347 dp = diff_alloc_new(curtab, dprev, dp);
|
7
|
1348 if (dp == NULL)
|
840
|
1349 goto done;
|
7
|
1350
|
|
1351 dp->df_lnum[idx_orig] = lnum_orig;
|
|
1352 dp->df_count[idx_orig] = count_orig;
|
|
1353 dp->df_lnum[idx_new] = lnum_new;
|
|
1354 dp->df_count[idx_new] = count_new;
|
|
1355
|
|
1356 /* Set values for other buffers, these must be equal to the
|
|
1357 * original buffer, otherwise there would have been a change
|
|
1358 * already. */
|
|
1359 for (i = idx_orig + 1; i < idx_new; ++i)
|
672
|
1360 if (curtab->tp_diffbuf[i] != NULL)
|
7
|
1361 diff_copy_entry(dprev, dp, idx_orig, i);
|
|
1362 }
|
|
1363 notset = FALSE; /* "*dp" has been set */
|
|
1364 }
|
|
1365
|
|
1366 /* for remaining diff blocks orig and new are equal */
|
|
1367 while (dp != NULL)
|
|
1368 {
|
|
1369 if (notset)
|
|
1370 diff_copy_entry(dprev, dp, idx_orig, idx_new);
|
|
1371 dprev = dp;
|
|
1372 dp = dp->df_next;
|
|
1373 notset = TRUE;
|
|
1374 }
|
|
1375
|
840
|
1376 done:
|
7
|
1377 fclose(fd);
|
|
1378 }
|
|
1379
|
|
1380 /*
|
|
1381 * Copy an entry at "dp" from "idx_orig" to "idx_new".
|
|
1382 */
|
|
1383 static void
|
|
1384 diff_copy_entry(dprev, dp, idx_orig, idx_new)
|
|
1385 diff_T *dprev;
|
|
1386 diff_T *dp;
|
|
1387 int idx_orig;
|
|
1388 int idx_new;
|
|
1389 {
|
|
1390 long off;
|
|
1391
|
|
1392 if (dprev == NULL)
|
|
1393 off = 0;
|
|
1394 else
|
|
1395 off = (dprev->df_lnum[idx_orig] + dprev->df_count[idx_orig])
|
|
1396 - (dprev->df_lnum[idx_new] + dprev->df_count[idx_new]);
|
|
1397 dp->df_lnum[idx_new] = dp->df_lnum[idx_orig] - off;
|
|
1398 dp->df_count[idx_new] = dp->df_count[idx_orig];
|
|
1399 }
|
|
1400
|
|
1401 /*
|
672
|
1402 * Clear the list of diffblocks for tab page "tp".
|
7
|
1403 */
|
358
|
1404 void
|
672
|
1405 diff_clear(tp)
|
|
1406 tabpage_T *tp;
|
7
|
1407 {
|
|
1408 diff_T *p, *next_p;
|
|
1409
|
672
|
1410 for (p = tp->tp_first_diff; p != NULL; p = next_p)
|
7
|
1411 {
|
|
1412 next_p = p->df_next;
|
|
1413 vim_free(p);
|
|
1414 }
|
672
|
1415 tp->tp_first_diff = NULL;
|
7
|
1416 }
|
|
1417
|
|
1418 /*
|
|
1419 * Check diff status for line "lnum" in buffer "buf":
|
|
1420 * Returns 0 for nothing special
|
|
1421 * Returns -1 for a line that should be highlighted as changed.
|
|
1422 * Returns -2 for a line that should be highlighted as added/deleted.
|
|
1423 * Returns > 0 for inserting that many filler lines above it (never happens
|
|
1424 * when 'diffopt' doesn't contain "filler").
|
|
1425 * This should only be used for windows where 'diff' is set.
|
|
1426 */
|
|
1427 int
|
|
1428 diff_check(wp, lnum)
|
|
1429 win_T *wp;
|
|
1430 linenr_T lnum;
|
|
1431 {
|
672
|
1432 int idx; /* index in tp_diffbuf[] for this buffer */
|
7
|
1433 diff_T *dp;
|
|
1434 int maxcount;
|
|
1435 int i;
|
|
1436 buf_T *buf = wp->w_buffer;
|
|
1437 int cmp;
|
|
1438
|
672
|
1439 if (curtab->tp_diff_invalid)
|
7
|
1440 ex_diffupdate(NULL); /* update after a big change */
|
|
1441
|
672
|
1442 if (curtab->tp_first_diff == NULL || !wp->w_p_diff) /* no diffs at all */
|
7
|
1443 return 0;
|
|
1444
|
|
1445 /* safety check: "lnum" must be a buffer line */
|
|
1446 if (lnum < 1 || lnum > buf->b_ml.ml_line_count + 1)
|
|
1447 return 0;
|
|
1448
|
|
1449 idx = diff_buf_idx(buf);
|
|
1450 if (idx == DB_COUNT)
|
|
1451 return 0; /* no diffs for buffer "buf" */
|
|
1452
|
|
1453 #ifdef FEAT_FOLDING
|
|
1454 /* A closed fold never has filler lines. */
|
|
1455 if (hasFoldingWin(wp, lnum, NULL, NULL, TRUE, NULL))
|
|
1456 return 0;
|
|
1457 #endif
|
|
1458
|
|
1459 /* search for a change that includes "lnum" in the list of diffblocks. */
|
672
|
1460 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
|
7
|
1461 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
|
|
1462 break;
|
|
1463 if (dp == NULL || lnum < dp->df_lnum[idx])
|
|
1464 return 0;
|
|
1465
|
|
1466 if (lnum < dp->df_lnum[idx] + dp->df_count[idx])
|
|
1467 {
|
|
1468 int zero = FALSE;
|
|
1469
|
|
1470 /* Changed or inserted line. If the other buffers have a count of
|
|
1471 * zero, the lines were inserted. If the other buffers have the same
|
|
1472 * count, check if the lines are identical. */
|
|
1473 cmp = FALSE;
|
|
1474 for (i = 0; i < DB_COUNT; ++i)
|
672
|
1475 if (i != idx && curtab->tp_diffbuf[i] != NULL)
|
7
|
1476 {
|
|
1477 if (dp->df_count[i] == 0)
|
|
1478 zero = TRUE;
|
|
1479 else
|
|
1480 {
|
|
1481 if (dp->df_count[i] != dp->df_count[idx])
|
|
1482 return -1; /* nr of lines changed. */
|
|
1483 cmp = TRUE;
|
|
1484 }
|
|
1485 }
|
|
1486 if (cmp)
|
|
1487 {
|
|
1488 /* Compare all lines. If they are equal the lines were inserted
|
|
1489 * in some buffers, deleted in others, but not changed. */
|
|
1490 for (i = 0; i < DB_COUNT; ++i)
|
672
|
1491 if (i != idx && curtab->tp_diffbuf[i] != NULL && dp->df_count[i] != 0)
|
7
|
1492 if (!diff_equal_entry(dp, idx, i))
|
|
1493 return -1;
|
|
1494 }
|
|
1495 /* If there is no buffer with zero lines then there is no difference
|
|
1496 * any longer. Happens when making a change (or undo) that removes
|
|
1497 * the difference. Can't remove the entry here, we might be halfway
|
|
1498 * updating the window. Just report the text as unchanged. Other
|
|
1499 * windows might still show the change though. */
|
|
1500 if (zero == FALSE)
|
|
1501 return 0;
|
|
1502 return -2;
|
|
1503 }
|
|
1504
|
|
1505 /* If 'diffopt' doesn't contain "filler", return 0. */
|
|
1506 if (!(diff_flags & DIFF_FILLER))
|
|
1507 return 0;
|
|
1508
|
|
1509 /* Insert filler lines above the line just below the change. Will return
|
|
1510 * 0 when this buf had the max count. */
|
|
1511 maxcount = 0;
|
|
1512 for (i = 0; i < DB_COUNT; ++i)
|
672
|
1513 if (curtab->tp_diffbuf[i] != NULL && dp->df_count[i] > maxcount)
|
7
|
1514 maxcount = dp->df_count[i];
|
|
1515 return maxcount - dp->df_count[idx];
|
|
1516 }
|
|
1517
|
|
1518 /*
|
|
1519 * Compare two entries in diff "*dp" and return TRUE if they are equal.
|
|
1520 */
|
|
1521 static int
|
|
1522 diff_equal_entry(dp, idx1, idx2)
|
|
1523 diff_T *dp;
|
|
1524 int idx1;
|
|
1525 int idx2;
|
|
1526 {
|
|
1527 int i;
|
|
1528 char_u *line;
|
|
1529 int cmp;
|
|
1530
|
|
1531 if (dp->df_count[idx1] != dp->df_count[idx2])
|
|
1532 return FALSE;
|
672
|
1533 if (diff_check_sanity(curtab, dp) == FAIL)
|
7
|
1534 return FALSE;
|
|
1535 for (i = 0; i < dp->df_count[idx1]; ++i)
|
|
1536 {
|
672
|
1537 line = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx1],
|
7
|
1538 dp->df_lnum[idx1] + i, FALSE));
|
|
1539 if (line == NULL)
|
|
1540 return FALSE;
|
672
|
1541 cmp = diff_cmp(line, ml_get_buf(curtab->tp_diffbuf[idx2],
|
7
|
1542 dp->df_lnum[idx2] + i, FALSE));
|
|
1543 vim_free(line);
|
|
1544 if (cmp != 0)
|
|
1545 return FALSE;
|
|
1546 }
|
|
1547 return TRUE;
|
|
1548 }
|
|
1549
|
|
1550 /*
|
|
1551 * Compare strings "s1" and "s2" according to 'diffopt'.
|
|
1552 * Return non-zero when they are different.
|
|
1553 */
|
|
1554 static int
|
|
1555 diff_cmp(s1, s2)
|
|
1556 char_u *s1;
|
|
1557 char_u *s2;
|
|
1558 {
|
|
1559 char_u *p1, *p2;
|
|
1560 #ifdef FEAT_MBYTE
|
|
1561 int l;
|
|
1562 #endif
|
|
1563
|
|
1564 if ((diff_flags & (DIFF_ICASE | DIFF_IWHITE)) == 0)
|
|
1565 return STRCMP(s1, s2);
|
|
1566 if ((diff_flags & DIFF_ICASE) && !(diff_flags & DIFF_IWHITE))
|
|
1567 return MB_STRICMP(s1, s2);
|
|
1568
|
|
1569 /* Ignore white space changes and possibly ignore case. */
|
|
1570 p1 = s1;
|
|
1571 p2 = s2;
|
|
1572 while (*p1 != NUL && *p2 != NUL)
|
|
1573 {
|
|
1574 if (vim_iswhite(*p1) && vim_iswhite(*p2))
|
|
1575 {
|
|
1576 p1 = skipwhite(p1);
|
|
1577 p2 = skipwhite(p2);
|
|
1578 }
|
|
1579 else
|
|
1580 {
|
|
1581 #ifdef FEAT_MBYTE
|
474
|
1582 l = (*mb_ptr2len)(p1);
|
|
1583 if (l != (*mb_ptr2len)(p2))
|
7
|
1584 break;
|
|
1585 if (l > 1)
|
|
1586 {
|
|
1587 if (STRNCMP(p1, p2, l) != 0
|
|
1588 && (!enc_utf8
|
|
1589 || !(diff_flags & DIFF_ICASE)
|
|
1590 || utf_fold(utf_ptr2char(p1))
|
|
1591 != utf_fold(utf_ptr2char(p2))))
|
|
1592 break;
|
|
1593 p1 += l;
|
|
1594 p2 += l;
|
|
1595 }
|
|
1596 else
|
|
1597 #endif
|
|
1598 {
|
|
1599 if (*p1 != *p2 && (!(diff_flags & DIFF_ICASE)
|
|
1600 || TOLOWER_LOC(*p1) != TOLOWER_LOC(*p2)))
|
|
1601 break;
|
|
1602 ++p1;
|
|
1603 ++p2;
|
|
1604 }
|
|
1605 }
|
|
1606 }
|
|
1607
|
|
1608 /* Ignore trailing white space. */
|
|
1609 p1 = skipwhite(p1);
|
|
1610 p2 = skipwhite(p2);
|
|
1611 if (*p1 != NUL || *p2 != NUL)
|
|
1612 return 1;
|
|
1613 return 0;
|
|
1614 }
|
|
1615
|
|
1616 /*
|
|
1617 * Return the number of filler lines above "lnum".
|
|
1618 */
|
|
1619 int
|
|
1620 diff_check_fill(wp, lnum)
|
|
1621 win_T *wp;
|
|
1622 linenr_T lnum;
|
|
1623 {
|
|
1624 int n;
|
|
1625
|
|
1626 /* be quick when there are no filler lines */
|
|
1627 if (!(diff_flags & DIFF_FILLER))
|
|
1628 return 0;
|
|
1629 n = diff_check(wp, lnum);
|
|
1630 if (n <= 0)
|
|
1631 return 0;
|
|
1632 return n;
|
|
1633 }
|
|
1634
|
|
1635 /*
|
|
1636 * Set the topline of "towin" to match the position in "fromwin", so that they
|
|
1637 * show the same diff'ed lines.
|
|
1638 */
|
|
1639 void
|
|
1640 diff_set_topline(fromwin, towin)
|
|
1641 win_T *fromwin;
|
|
1642 win_T *towin;
|
|
1643 {
|
1519
|
1644 buf_T *frombuf = fromwin->w_buffer;
|
7
|
1645 linenr_T lnum = fromwin->w_topline;
|
1519
|
1646 int fromidx;
|
|
1647 int toidx;
|
7
|
1648 diff_T *dp;
|
1519
|
1649 int max_count;
|
7
|
1650 int i;
|
|
1651
|
1519
|
1652 fromidx = diff_buf_idx(frombuf);
|
|
1653 if (fromidx == DB_COUNT)
|
7
|
1654 return; /* safety check */
|
|
1655
|
672
|
1656 if (curtab->tp_diff_invalid)
|
7
|
1657 ex_diffupdate(NULL); /* update after a big change */
|
|
1658
|
|
1659 towin->w_topfill = 0;
|
|
1660
|
|
1661 /* search for a change that includes "lnum" in the list of diffblocks. */
|
672
|
1662 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
|
1519
|
1663 if (lnum <= dp->df_lnum[fromidx] + dp->df_count[fromidx])
|
7
|
1664 break;
|
|
1665 if (dp == NULL)
|
|
1666 {
|
|
1667 /* After last change, compute topline relative to end of file; no
|
|
1668 * filler lines. */
|
|
1669 towin->w_topline = towin->w_buffer->b_ml.ml_line_count
|
1519
|
1670 - (frombuf->b_ml.ml_line_count - lnum);
|
7
|
1671 }
|
|
1672 else
|
|
1673 {
|
|
1674 /* Find index for "towin". */
|
1519
|
1675 toidx = diff_buf_idx(towin->w_buffer);
|
|
1676 if (toidx == DB_COUNT)
|
7
|
1677 return; /* safety check */
|
|
1678
|
1519
|
1679 towin->w_topline = lnum + (dp->df_lnum[toidx] - dp->df_lnum[fromidx]);
|
|
1680 if (lnum >= dp->df_lnum[fromidx])
|
7
|
1681 {
|
1519
|
1682 /* Inside a change: compute filler lines. With three or more
|
|
1683 * buffers we need to know the largest count. */
|
|
1684 max_count = 0;
|
|
1685 for (i = 0; i < DB_COUNT; ++i)
|
|
1686 if (curtab->tp_diffbuf[i] != NULL
|
|
1687 && max_count < dp->df_count[i])
|
|
1688 max_count = dp->df_count[i];
|
|
1689
|
|
1690 if (dp->df_count[toidx] == dp->df_count[fromidx])
|
|
1691 {
|
|
1692 /* same number of lines: use same filler count */
|
7
|
1693 towin->w_topfill = fromwin->w_topfill;
|
1519
|
1694 }
|
|
1695 else if (dp->df_count[toidx] > dp->df_count[fromidx])
|
7
|
1696 {
|
1519
|
1697 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
|
|
1698 {
|
|
1699 /* more lines in towin and fromwin doesn't show diff
|
|
1700 * lines, only filler lines */
|
|
1701 if (max_count - fromwin->w_topfill >= dp->df_count[toidx])
|
|
1702 {
|
|
1703 /* towin also only shows filler lines */
|
|
1704 towin->w_topline = dp->df_lnum[toidx]
|
|
1705 + dp->df_count[toidx];
|
|
1706 towin->w_topfill = fromwin->w_topfill;
|
|
1707 }
|
|
1708 else
|
|
1709 /* towin still has some diff lines to show */
|
|
1710 towin->w_topline = dp->df_lnum[toidx]
|
|
1711 + max_count - fromwin->w_topfill;
|
|
1712 }
|
7
|
1713 }
|
1519
|
1714 else if (towin->w_topline >= dp->df_lnum[toidx]
|
|
1715 + dp->df_count[toidx])
|
7
|
1716 {
|
1519
|
1717 /* less lines in towin and no diff lines to show: compute
|
|
1718 * filler lines */
|
|
1719 towin->w_topline = dp->df_lnum[toidx] + dp->df_count[toidx];
|
|
1720 if (diff_flags & DIFF_FILLER)
|
7
|
1721 {
|
1519
|
1722 if (lnum == dp->df_lnum[fromidx] + dp->df_count[fromidx])
|
|
1723 /* fromwin is also out of diff lines */
|
|
1724 towin->w_topfill = fromwin->w_topfill;
|
|
1725 else
|
|
1726 /* fromwin has some diff lines */
|
|
1727 towin->w_topfill = dp->df_lnum[fromidx]
|
|
1728 + max_count - lnum;
|
7
|
1729 }
|
|
1730 }
|
|
1731 }
|
|
1732 }
|
|
1733
|
|
1734 /* safety check (if diff info gets outdated strange things may happen) */
|
|
1735 towin->w_botfill = FALSE;
|
|
1736 if (towin->w_topline > towin->w_buffer->b_ml.ml_line_count)
|
|
1737 {
|
|
1738 towin->w_topline = towin->w_buffer->b_ml.ml_line_count;
|
|
1739 towin->w_botfill = TRUE;
|
|
1740 }
|
|
1741 if (towin->w_topline < 1)
|
|
1742 {
|
|
1743 towin->w_topline = 1;
|
|
1744 towin->w_topfill = 0;
|
|
1745 }
|
|
1746
|
|
1747 /* When w_topline changes need to recompute w_botline and cursor position */
|
|
1748 invalidate_botline_win(towin);
|
|
1749 changed_line_abv_curs_win(towin);
|
|
1750
|
|
1751 check_topfill(towin, FALSE);
|
|
1752 #ifdef FEAT_FOLDING
|
|
1753 (void)hasFoldingWin(towin, towin->w_topline, &towin->w_topline,
|
|
1754 NULL, TRUE, NULL);
|
|
1755 #endif
|
|
1756 }
|
|
1757
|
|
1758 /*
|
|
1759 * This is called when 'diffopt' is changed.
|
|
1760 */
|
|
1761 int
|
|
1762 diffopt_changed()
|
|
1763 {
|
|
1764 char_u *p;
|
|
1765 int diff_context_new = 6;
|
|
1766 int diff_flags_new = 0;
|
764
|
1767 int diff_foldcolumn_new = 2;
|
672
|
1768 tabpage_T *tp;
|
7
|
1769
|
|
1770 p = p_dip;
|
|
1771 while (*p != NUL)
|
|
1772 {
|
|
1773 if (STRNCMP(p, "filler", 6) == 0)
|
|
1774 {
|
|
1775 p += 6;
|
|
1776 diff_flags_new |= DIFF_FILLER;
|
|
1777 }
|
|
1778 else if (STRNCMP(p, "context:", 8) == 0 && VIM_ISDIGIT(p[8]))
|
|
1779 {
|
|
1780 p += 8;
|
|
1781 diff_context_new = getdigits(&p);
|
|
1782 }
|
|
1783 else if (STRNCMP(p, "icase", 5) == 0)
|
|
1784 {
|
|
1785 p += 5;
|
|
1786 diff_flags_new |= DIFF_ICASE;
|
|
1787 }
|
|
1788 else if (STRNCMP(p, "iwhite", 6) == 0)
|
|
1789 {
|
|
1790 p += 6;
|
|
1791 diff_flags_new |= DIFF_IWHITE;
|
|
1792 }
|
764
|
1793 else if (STRNCMP(p, "horizontal", 10) == 0)
|
|
1794 {
|
|
1795 p += 10;
|
|
1796 diff_flags_new |= DIFF_HORIZONTAL;
|
|
1797 }
|
|
1798 else if (STRNCMP(p, "vertical", 8) == 0)
|
|
1799 {
|
|
1800 p += 8;
|
|
1801 diff_flags_new |= DIFF_VERTICAL;
|
|
1802 }
|
|
1803 else if (STRNCMP(p, "foldcolumn:", 11) == 0 && VIM_ISDIGIT(p[11]))
|
|
1804 {
|
|
1805 p += 11;
|
|
1806 diff_foldcolumn_new = getdigits(&p);
|
|
1807 }
|
7
|
1808 if (*p != ',' && *p != NUL)
|
|
1809 return FAIL;
|
|
1810 if (*p == ',')
|
|
1811 ++p;
|
|
1812 }
|
|
1813
|
764
|
1814 /* Can't have both "horizontal" and "vertical". */
|
|
1815 if ((diff_flags_new & DIFF_HORIZONTAL) && (diff_flags_new & DIFF_VERTICAL))
|
|
1816 return FAIL;
|
|
1817
|
7
|
1818 /* If "icase" or "iwhite" was added or removed, need to update the diff. */
|
|
1819 if (diff_flags != diff_flags_new)
|
672
|
1820 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
|
|
1821 tp->tp_diff_invalid = TRUE;
|
7
|
1822
|
|
1823 diff_flags = diff_flags_new;
|
|
1824 diff_context = diff_context_new;
|
764
|
1825 diff_foldcolumn = diff_foldcolumn_new;
|
7
|
1826
|
|
1827 diff_redraw(TRUE);
|
|
1828
|
|
1829 /* recompute the scroll binding with the new option value, may
|
|
1830 * remove or add filler lines */
|
|
1831 check_scrollbind((linenr_T)0, 0L);
|
|
1832
|
|
1833 return OK;
|
|
1834 }
|
|
1835
|
|
1836 /*
|
764
|
1837 * Return TRUE if 'diffopt' contains "horizontal".
|
|
1838 */
|
|
1839 int
|
|
1840 diffopt_horizontal()
|
|
1841 {
|
|
1842 return (diff_flags & DIFF_HORIZONTAL) != 0;
|
|
1843 }
|
|
1844
|
|
1845 /*
|
7
|
1846 * Find the difference within a changed line.
|
|
1847 * Returns TRUE if the line was added, no other buffer has it.
|
|
1848 */
|
|
1849 int
|
|
1850 diff_find_change(wp, lnum, startp, endp)
|
|
1851 win_T *wp;
|
|
1852 linenr_T lnum;
|
|
1853 int *startp; /* first char of the change */
|
|
1854 int *endp; /* last char of the change */
|
|
1855 {
|
|
1856 char_u *line_org;
|
|
1857 char_u *line_new;
|
|
1858 int i;
|
829
|
1859 int si_org, si_new;
|
|
1860 int ei_org, ei_new;
|
7
|
1861 diff_T *dp;
|
|
1862 int idx;
|
|
1863 int off;
|
|
1864 int added = TRUE;
|
|
1865
|
|
1866 /* Make a copy of the line, the next ml_get() will invalidate it. */
|
|
1867 line_org = vim_strsave(ml_get_buf(wp->w_buffer, lnum, FALSE));
|
|
1868 if (line_org == NULL)
|
|
1869 return FALSE;
|
|
1870
|
|
1871 idx = diff_buf_idx(wp->w_buffer);
|
|
1872 if (idx == DB_COUNT) /* cannot happen */
|
1074
|
1873 {
|
|
1874 vim_free(line_org);
|
7
|
1875 return FALSE;
|
1074
|
1876 }
|
7
|
1877
|
|
1878 /* search for a change that includes "lnum" in the list of diffblocks. */
|
672
|
1879 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
|
7
|
1880 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
|
|
1881 break;
|
672
|
1882 if (dp == NULL || diff_check_sanity(curtab, dp) == FAIL)
|
1074
|
1883 {
|
|
1884 vim_free(line_org);
|
7
|
1885 return FALSE;
|
1074
|
1886 }
|
7
|
1887
|
|
1888 off = lnum - dp->df_lnum[idx];
|
|
1889
|
|
1890 for (i = 0; i < DB_COUNT; ++i)
|
672
|
1891 if (curtab->tp_diffbuf[i] != NULL && i != idx)
|
7
|
1892 {
|
|
1893 /* Skip lines that are not in the other change (filler lines). */
|
|
1894 if (off >= dp->df_count[i])
|
|
1895 continue;
|
|
1896 added = FALSE;
|
829
|
1897 line_new = ml_get_buf(curtab->tp_diffbuf[i],
|
|
1898 dp->df_lnum[i] + off, FALSE);
|
7
|
1899
|
|
1900 /* Search for start of difference */
|
829
|
1901 si_org = si_new = 0;
|
|
1902 while (line_org[si_org] != NUL)
|
|
1903 {
|
|
1904 if ((diff_flags & DIFF_IWHITE)
|
|
1905 && vim_iswhite(line_org[si_org])
|
|
1906 && vim_iswhite(line_new[si_new]))
|
|
1907 {
|
835
|
1908 si_org = (int)(skipwhite(line_org + si_org) - line_org);
|
|
1909 si_new = (int)(skipwhite(line_new + si_new) - line_new);
|
829
|
1910 }
|
|
1911 else
|
|
1912 {
|
|
1913 if (line_org[si_org] != line_new[si_new])
|
|
1914 break;
|
|
1915 ++si_org;
|
|
1916 ++si_new;
|
|
1917 }
|
|
1918 }
|
7
|
1919 #ifdef FEAT_MBYTE
|
|
1920 if (has_mbyte)
|
|
1921 {
|
|
1922 /* Move back to first byte of character in both lines (may
|
|
1923 * have "nn^" in line_org and "n^ in line_new). */
|
829
|
1924 si_org -= (*mb_head_off)(line_org, line_org + si_org);
|
|
1925 si_new -= (*mb_head_off)(line_new, line_new + si_new);
|
7
|
1926 }
|
|
1927 #endif
|
829
|
1928 if (*startp > si_org)
|
|
1929 *startp = si_org;
|
7
|
1930
|
|
1931 /* Search for end of difference, if any. */
|
829
|
1932 if (line_org[si_org] != NUL || line_new[si_new] != NUL)
|
7
|
1933 {
|
|
1934 ei_org = (int)STRLEN(line_org);
|
|
1935 ei_new = (int)STRLEN(line_new);
|
829
|
1936 while (ei_org >= *startp && ei_new >= si_new
|
|
1937 && ei_org >= 0 && ei_new >= 0)
|
7
|
1938 {
|
829
|
1939 if ((diff_flags & DIFF_IWHITE)
|
|
1940 && vim_iswhite(line_org[ei_org])
|
|
1941 && vim_iswhite(line_new[ei_new]))
|
|
1942 {
|
|
1943 while (ei_org >= *startp
|
|
1944 && vim_iswhite(line_org[ei_org]))
|
|
1945 --ei_org;
|
|
1946 while (ei_new >= si_new
|
|
1947 && vim_iswhite(line_new[ei_new]))
|
|
1948 --ei_new;
|
|
1949 }
|
|
1950 else
|
|
1951 {
|
|
1952 if (line_org[ei_org] != line_new[ei_new])
|
|
1953 break;
|
|
1954 --ei_org;
|
|
1955 --ei_new;
|
|
1956 }
|
7
|
1957 }
|
|
1958 if (*endp < ei_org)
|
|
1959 *endp = ei_org;
|
|
1960 }
|
|
1961 }
|
|
1962
|
|
1963 vim_free(line_org);
|
|
1964 return added;
|
|
1965 }
|
|
1966
|
|
1967 #if defined(FEAT_FOLDING) || defined(PROTO)
|
|
1968 /*
|
|
1969 * Return TRUE if line "lnum" is not close to a diff block, this line should
|
|
1970 * be in a fold.
|
|
1971 * Return FALSE if there are no diff blocks at all in this window.
|
|
1972 */
|
|
1973 int
|
|
1974 diff_infold(wp, lnum)
|
|
1975 win_T *wp;
|
|
1976 linenr_T lnum;
|
|
1977 {
|
|
1978 int i;
|
|
1979 int idx = -1;
|
|
1980 int other = FALSE;
|
|
1981 diff_T *dp;
|
|
1982
|
|
1983 /* Return if 'diff' isn't set. */
|
|
1984 if (!wp->w_p_diff)
|
|
1985 return FALSE;
|
|
1986
|
|
1987 for (i = 0; i < DB_COUNT; ++i)
|
|
1988 {
|
672
|
1989 if (curtab->tp_diffbuf[i] == wp->w_buffer)
|
7
|
1990 idx = i;
|
672
|
1991 else if (curtab->tp_diffbuf[i] != NULL)
|
7
|
1992 other = TRUE;
|
|
1993 }
|
|
1994
|
|
1995 /* return here if there are no diffs in the window */
|
|
1996 if (idx == -1 || !other)
|
|
1997 return FALSE;
|
|
1998
|
672
|
1999 if (curtab->tp_diff_invalid)
|
7
|
2000 ex_diffupdate(NULL); /* update after a big change */
|
|
2001
|
|
2002 /* Return if there are no diff blocks. All lines will be folded. */
|
672
|
2003 if (curtab->tp_first_diff == NULL)
|
7
|
2004 return TRUE;
|
|
2005
|
672
|
2006 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
|
7
|
2007 {
|
|
2008 /* If this change is below the line there can't be any further match. */
|
|
2009 if (dp->df_lnum[idx] - diff_context > lnum)
|
|
2010 break;
|
|
2011 /* If this change ends before the line we have a match. */
|
|
2012 if (dp->df_lnum[idx] + dp->df_count[idx] + diff_context > lnum)
|
|
2013 return FALSE;
|
|
2014 }
|
|
2015 return TRUE;
|
|
2016 }
|
|
2017 #endif
|
|
2018
|
|
2019 /*
|
|
2020 * "dp" and "do" commands.
|
|
2021 */
|
|
2022 void
|
|
2023 nv_diffgetput(put)
|
|
2024 int put;
|
|
2025 {
|
|
2026 exarg_T ea;
|
|
2027
|
|
2028 ea.arg = (char_u *)"";
|
|
2029 if (put)
|
|
2030 ea.cmdidx = CMD_diffput;
|
|
2031 else
|
|
2032 ea.cmdidx = CMD_diffget;
|
|
2033 ea.addr_count = 0;
|
|
2034 ea.line1 = curwin->w_cursor.lnum;
|
|
2035 ea.line2 = curwin->w_cursor.lnum;
|
|
2036 ex_diffgetput(&ea);
|
|
2037 }
|
|
2038
|
|
2039 /*
|
|
2040 * ":diffget"
|
|
2041 * ":diffput"
|
|
2042 */
|
|
2043 void
|
|
2044 ex_diffgetput(eap)
|
|
2045 exarg_T *eap;
|
|
2046 {
|
|
2047 linenr_T lnum;
|
|
2048 int count;
|
|
2049 linenr_T off = 0;
|
|
2050 diff_T *dp;
|
|
2051 diff_T *dprev;
|
|
2052 diff_T *dfree;
|
|
2053 int idx_cur;
|
|
2054 int idx_other;
|
|
2055 int idx_from;
|
|
2056 int idx_to;
|
|
2057 int i;
|
|
2058 int added;
|
|
2059 char_u *p;
|
|
2060 aco_save_T aco;
|
|
2061 buf_T *buf;
|
|
2062 int start_skip, end_skip;
|
|
2063 int new_count;
|
648
|
2064 int buf_empty;
|
1075
|
2065 int found_not_ma = FALSE;
|
7
|
2066
|
|
2067 /* Find the current buffer in the list of diff buffers. */
|
|
2068 idx_cur = diff_buf_idx(curbuf);
|
|
2069 if (idx_cur == DB_COUNT)
|
|
2070 {
|
|
2071 EMSG(_("E99: Current buffer is not in diff mode"));
|
|
2072 return;
|
|
2073 }
|
|
2074
|
|
2075 if (*eap->arg == NUL)
|
|
2076 {
|
|
2077 /* No argument: Find the other buffer in the list of diff buffers. */
|
|
2078 for (idx_other = 0; idx_other < DB_COUNT; ++idx_other)
|
672
|
2079 if (curtab->tp_diffbuf[idx_other] != curbuf
|
1075
|
2080 && curtab->tp_diffbuf[idx_other] != NULL)
|
|
2081 {
|
|
2082 if (eap->cmdidx != CMD_diffput
|
|
2083 || curtab->tp_diffbuf[idx_other]->b_p_ma)
|
|
2084 break;
|
|
2085 found_not_ma = TRUE;
|
|
2086 }
|
7
|
2087 if (idx_other == DB_COUNT)
|
|
2088 {
|
1075
|
2089 if (found_not_ma)
|
|
2090 EMSG(_("E793: No other buffer in diff mode is modifiable"));
|
|
2091 else
|
|
2092 EMSG(_("E100: No other buffer in diff mode"));
|
7
|
2093 return;
|
|
2094 }
|
|
2095
|
|
2096 /* Check that there isn't a third buffer in the list */
|
|
2097 for (i = idx_other + 1; i < DB_COUNT; ++i)
|
672
|
2098 if (curtab->tp_diffbuf[i] != curbuf
|
|
2099 && curtab->tp_diffbuf[i] != NULL
|
|
2100 && (eap->cmdidx != CMD_diffput || curtab->tp_diffbuf[i]->b_p_ma))
|
7
|
2101 {
|
|
2102 EMSG(_("E101: More than two buffers in diff mode, don't know which one to use"));
|
|
2103 return;
|
|
2104 }
|
|
2105 }
|
|
2106 else
|
|
2107 {
|
|
2108 /* Buffer number or pattern given. Ignore trailing white space. */
|
|
2109 p = eap->arg + STRLEN(eap->arg);
|
|
2110 while (p > eap->arg && vim_iswhite(p[-1]))
|
|
2111 --p;
|
|
2112 for (i = 0; vim_isdigit(eap->arg[i]) && eap->arg + i < p; ++i)
|
|
2113 ;
|
|
2114 if (eap->arg + i == p) /* digits only */
|
|
2115 i = atol((char *)eap->arg);
|
|
2116 else
|
|
2117 {
|
|
2118 i = buflist_findpat(eap->arg, p, FALSE, TRUE);
|
|
2119 if (i < 0)
|
|
2120 return; /* error message already given */
|
|
2121 }
|
|
2122 buf = buflist_findnr(i);
|
|
2123 if (buf == NULL)
|
|
2124 {
|
|
2125 EMSG2(_("E102: Can't find buffer \"%s\""), eap->arg);
|
|
2126 return;
|
|
2127 }
|
|
2128 idx_other = diff_buf_idx(buf);
|
|
2129 if (idx_other == DB_COUNT)
|
|
2130 {
|
|
2131 EMSG2(_("E103: Buffer \"%s\" is not in diff mode"), eap->arg);
|
|
2132 return;
|
|
2133 }
|
|
2134 }
|
|
2135
|
|
2136 diff_busy = TRUE;
|
|
2137
|
|
2138 /* When no range given include the line above or below the cursor. */
|
|
2139 if (eap->addr_count == 0)
|
|
2140 {
|
|
2141 /* Make it possible that ":diffget" on the last line gets line below
|
|
2142 * the cursor line when there is no difference above the cursor. */
|
|
2143 if (eap->cmdidx == CMD_diffget
|
|
2144 && eap->line1 == curbuf->b_ml.ml_line_count
|
|
2145 && diff_check(curwin, eap->line1) == 0
|
|
2146 && (eap->line1 == 1 || diff_check(curwin, eap->line1 - 1) == 0))
|
|
2147 ++eap->line2;
|
|
2148 else if (eap->line1 > 0)
|
|
2149 --eap->line1;
|
|
2150 }
|
|
2151
|
|
2152 if (eap->cmdidx == CMD_diffget)
|
|
2153 {
|
|
2154 idx_from = idx_other;
|
|
2155 idx_to = idx_cur;
|
|
2156 }
|
|
2157 else
|
|
2158 {
|
|
2159 idx_from = idx_cur;
|
|
2160 idx_to = idx_other;
|
|
2161 /* Need to make the other buffer the current buffer to be able to make
|
|
2162 * changes in it. */
|
|
2163 /* set curwin/curbuf to buf and save a few things */
|
672
|
2164 aucmd_prepbuf(&aco, curtab->tp_diffbuf[idx_other]);
|
7
|
2165 }
|
|
2166
|
819
|
2167 /* May give the warning for a changed buffer here, which can trigger the
|
|
2168 * FileChangedRO autocommand, which may do nasty things and mess
|
|
2169 * everything up. */
|
|
2170 if (!curbuf->b_changed)
|
|
2171 {
|
|
2172 change_warning(0);
|
|
2173 if (diff_buf_idx(curbuf) != idx_to)
|
|
2174 {
|
|
2175 EMSG(_("E787: Buffer changed unexpectedly"));
|
|
2176 return;
|
|
2177 }
|
|
2178 }
|
|
2179
|
7
|
2180 dprev = NULL;
|
672
|
2181 for (dp = curtab->tp_first_diff; dp != NULL; )
|
7
|
2182 {
|
|
2183 if (dp->df_lnum[idx_cur] > eap->line2 + off)
|
|
2184 break; /* past the range that was specified */
|
|
2185
|
|
2186 dfree = NULL;
|
|
2187 lnum = dp->df_lnum[idx_to];
|
|
2188 count = dp->df_count[idx_to];
|
|
2189 if (dp->df_lnum[idx_cur] + dp->df_count[idx_cur] > eap->line1 + off
|
|
2190 && u_save(lnum - 1, lnum + count) != FAIL)
|
|
2191 {
|
|
2192 /* Inside the specified range and saving for undo worked. */
|
|
2193 start_skip = 0;
|
|
2194 end_skip = 0;
|
|
2195 if (eap->addr_count > 0)
|
|
2196 {
|
|
2197 /* A range was specified: check if lines need to be skipped. */
|
|
2198 start_skip = eap->line1 + off - dp->df_lnum[idx_cur];
|
|
2199 if (start_skip > 0)
|
|
2200 {
|
|
2201 /* range starts below start of current diff block */
|
|
2202 if (start_skip > count)
|
|
2203 {
|
|
2204 lnum += count;
|
|
2205 count = 0;
|
|
2206 }
|
|
2207 else
|
|
2208 {
|
|
2209 count -= start_skip;
|
|
2210 lnum += start_skip;
|
|
2211 }
|
|
2212 }
|
|
2213 else
|
|
2214 start_skip = 0;
|
|
2215
|
|
2216 end_skip = dp->df_lnum[idx_cur] + dp->df_count[idx_cur] - 1
|
|
2217 - (eap->line2 + off);
|
|
2218 if (end_skip > 0)
|
|
2219 {
|
|
2220 /* range ends above end of current/from diff block */
|
|
2221 if (idx_cur == idx_from) /* :diffput */
|
|
2222 {
|
|
2223 i = dp->df_count[idx_cur] - start_skip - end_skip;
|
|
2224 if (count > i)
|
|
2225 count = i;
|
|
2226 }
|
|
2227 else /* :diffget */
|
|
2228 {
|
|
2229 count -= end_skip;
|
|
2230 end_skip = dp->df_count[idx_from] - start_skip - count;
|
|
2231 if (end_skip < 0)
|
|
2232 end_skip = 0;
|
|
2233 }
|
|
2234 }
|
|
2235 else
|
|
2236 end_skip = 0;
|
|
2237 }
|
|
2238
|
648
|
2239 buf_empty = FALSE;
|
7
|
2240 added = 0;
|
|
2241 for (i = 0; i < count; ++i)
|
|
2242 {
|
648
|
2243 /* remember deleting the last line of the buffer */
|
|
2244 buf_empty = curbuf->b_ml.ml_line_count == 1;
|
7
|
2245 ml_delete(lnum, FALSE);
|
|
2246 --added;
|
|
2247 }
|
|
2248 for (i = 0; i < dp->df_count[idx_from] - start_skip - end_skip; ++i)
|
|
2249 {
|
|
2250 linenr_T nr;
|
|
2251
|
|
2252 nr = dp->df_lnum[idx_from] + start_skip + i;
|
672
|
2253 if (nr > curtab->tp_diffbuf[idx_from]->b_ml.ml_line_count)
|
7
|
2254 break;
|
819
|
2255 p = vim_strsave(ml_get_buf(curtab->tp_diffbuf[idx_from],
|
|
2256 nr, FALSE));
|
7
|
2257 if (p != NULL)
|
|
2258 {
|
|
2259 ml_append(lnum + i - 1, p, 0, FALSE);
|
|
2260 vim_free(p);
|
|
2261 ++added;
|
648
|
2262 if (buf_empty && curbuf->b_ml.ml_line_count == 2)
|
|
2263 {
|
|
2264 /* Added the first line into an empty buffer, need to
|
|
2265 * delete the dummy empty line. */
|
|
2266 buf_empty = FALSE;
|
|
2267 ml_delete((linenr_T)2, FALSE);
|
|
2268 }
|
7
|
2269 }
|
|
2270 }
|
|
2271 new_count = dp->df_count[idx_to] + added;
|
|
2272 dp->df_count[idx_to] = new_count;
|
|
2273
|
|
2274 if (start_skip == 0 && end_skip == 0)
|
|
2275 {
|
|
2276 /* Check if there are any other buffers and if the diff is
|
|
2277 * equal in them. */
|
|
2278 for (i = 0; i < DB_COUNT; ++i)
|
819
|
2279 if (curtab->tp_diffbuf[i] != NULL && i != idx_from
|
|
2280 && i != idx_to
|
7
|
2281 && !diff_equal_entry(dp, idx_from, i))
|
|
2282 break;
|
|
2283 if (i == DB_COUNT)
|
|
2284 {
|
|
2285 /* delete the diff entry, the buffers are now equal here */
|
|
2286 dfree = dp;
|
|
2287 dp = dp->df_next;
|
|
2288 if (dprev == NULL)
|
672
|
2289 curtab->tp_first_diff = dp;
|
7
|
2290 else
|
|
2291 dprev->df_next = dp;
|
|
2292 }
|
|
2293 }
|
|
2294
|
|
2295 /* Adjust marks. This will change the following entries! */
|
|
2296 if (added != 0)
|
|
2297 {
|
|
2298 mark_adjust(lnum, lnum + count - 1, (long)MAXLNUM, (long)added);
|
|
2299 if (curwin->w_cursor.lnum >= lnum)
|
|
2300 {
|
|
2301 /* Adjust the cursor position if it's in/after the changed
|
|
2302 * lines. */
|
|
2303 if (curwin->w_cursor.lnum >= lnum + count)
|
|
2304 curwin->w_cursor.lnum += added;
|
|
2305 else if (added < 0)
|
|
2306 curwin->w_cursor.lnum = lnum;
|
|
2307 }
|
|
2308 }
|
|
2309 changed_lines(lnum, 0, lnum + count, (long)added);
|
|
2310
|
|
2311 if (dfree != NULL)
|
|
2312 {
|
|
2313 /* Diff is deleted, update folds in other windows. */
|
|
2314 #ifdef FEAT_FOLDING
|
|
2315 diff_fold_update(dfree, idx_to);
|
|
2316 #endif
|
|
2317 vim_free(dfree);
|
|
2318 }
|
|
2319 else
|
|
2320 /* mark_adjust() may have changed the count in a wrong way */
|
|
2321 dp->df_count[idx_to] = new_count;
|
|
2322
|
|
2323 /* When changing the current buffer, keep track of line numbers */
|
|
2324 if (idx_cur == idx_to)
|
|
2325 off += added;
|
|
2326 }
|
|
2327
|
|
2328 /* If before the range or not deleted, go to next diff. */
|
|
2329 if (dfree == NULL)
|
|
2330 {
|
|
2331 dprev = dp;
|
|
2332 dp = dp->df_next;
|
|
2333 }
|
|
2334 }
|
|
2335
|
|
2336 /* restore curwin/curbuf and a few other things */
|
|
2337 if (idx_other == idx_to)
|
|
2338 {
|
|
2339 /* Syncing undo only works for the current buffer, but we change
|
|
2340 * another buffer. Sync undo if the command was typed. This isn't
|
|
2341 * 100% right when ":diffput" is used in a function or mapping. */
|
|
2342 if (KeyTyped)
|
825
|
2343 u_sync(FALSE);
|
7
|
2344 aucmd_restbuf(&aco);
|
|
2345 }
|
|
2346
|
|
2347 diff_busy = FALSE;
|
|
2348
|
|
2349 /* Check that the cursor is on a valid character and update it's position.
|
|
2350 * When there were filler lines the topline has become invalid. */
|
|
2351 check_cursor();
|
|
2352 changed_line_abv_curs();
|
|
2353
|
|
2354 /* Also need to redraw the other buffers. */
|
|
2355 diff_redraw(FALSE);
|
|
2356 }
|
|
2357
|
|
2358 #ifdef FEAT_FOLDING
|
|
2359 /*
|
|
2360 * Update folds for all diff buffers for entry "dp".
|
|
2361 * Skip buffer with index "skip_idx".
|
|
2362 * When there are no diffs, all folds are removed.
|
|
2363 */
|
|
2364 static void
|
|
2365 diff_fold_update(dp, skip_idx)
|
|
2366 diff_T *dp;
|
|
2367 int skip_idx;
|
|
2368 {
|
|
2369 int i;
|
|
2370 win_T *wp;
|
|
2371
|
|
2372 for (wp = firstwin; wp != NULL; wp = wp->w_next)
|
|
2373 for (i = 0; i < DB_COUNT; ++i)
|
672
|
2374 if (curtab->tp_diffbuf[i] == wp->w_buffer && i != skip_idx)
|
7
|
2375 foldUpdate(wp, dp->df_lnum[i],
|
|
2376 dp->df_lnum[i] + dp->df_count[i]);
|
|
2377 }
|
|
2378 #endif
|
|
2379
|
|
2380 /*
|
|
2381 * Return TRUE if buffer "buf" is in diff-mode.
|
|
2382 */
|
|
2383 int
|
|
2384 diff_mode_buf(buf)
|
|
2385 buf_T *buf;
|
|
2386 {
|
672
|
2387 tabpage_T *tp;
|
|
2388
|
|
2389 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
|
|
2390 if (diff_buf_idx_tp(buf, tp) != DB_COUNT)
|
|
2391 return TRUE;
|
|
2392 return FALSE;
|
7
|
2393 }
|
|
2394
|
|
2395 /*
|
|
2396 * Move "count" times in direction "dir" to the next diff block.
|
|
2397 * Return FAIL if there isn't such a diff block.
|
|
2398 */
|
|
2399 int
|
|
2400 diff_move_to(dir, count)
|
|
2401 int dir;
|
|
2402 long count;
|
|
2403 {
|
|
2404 int idx;
|
|
2405 linenr_T lnum = curwin->w_cursor.lnum;
|
|
2406 diff_T *dp;
|
|
2407
|
|
2408 idx = diff_buf_idx(curbuf);
|
672
|
2409 if (idx == DB_COUNT || curtab->tp_first_diff == NULL)
|
7
|
2410 return FAIL;
|
|
2411
|
672
|
2412 if (curtab->tp_diff_invalid)
|
7
|
2413 ex_diffupdate(NULL); /* update after a big change */
|
|
2414
|
672
|
2415 if (curtab->tp_first_diff == NULL) /* no diffs today */
|
7
|
2416 return FAIL;
|
|
2417
|
|
2418 while (--count >= 0)
|
|
2419 {
|
|
2420 /* Check if already before first diff. */
|
672
|
2421 if (dir == BACKWARD && lnum <= curtab->tp_first_diff->df_lnum[idx])
|
7
|
2422 break;
|
|
2423
|
672
|
2424 for (dp = curtab->tp_first_diff; ; dp = dp->df_next)
|
7
|
2425 {
|
|
2426 if (dp == NULL)
|
|
2427 break;
|
|
2428 if ((dir == FORWARD && lnum < dp->df_lnum[idx])
|
|
2429 || (dir == BACKWARD
|
|
2430 && (dp->df_next == NULL
|
|
2431 || lnum <= dp->df_next->df_lnum[idx])))
|
|
2432 {
|
|
2433 lnum = dp->df_lnum[idx];
|
|
2434 break;
|
|
2435 }
|
|
2436 }
|
|
2437 }
|
|
2438
|
|
2439 /* don't end up past the end of the file */
|
|
2440 if (lnum > curbuf->b_ml.ml_line_count)
|
|
2441 lnum = curbuf->b_ml.ml_line_count;
|
|
2442
|
|
2443 /* When the cursor didn't move at all we fail. */
|
|
2444 if (lnum == curwin->w_cursor.lnum)
|
|
2445 return FAIL;
|
|
2446
|
|
2447 setpcmark();
|
|
2448 curwin->w_cursor.lnum = lnum;
|
|
2449 curwin->w_cursor.col = 0;
|
|
2450
|
|
2451 return OK;
|
|
2452 }
|
|
2453
|
|
2454 #if defined(FEAT_FOLDING) || defined(PROTO)
|
|
2455 /*
|
|
2456 * For line "lnum" in the current window find the equivalent lnum in window
|
|
2457 * "wp", compensating for inserted/deleted lines.
|
|
2458 */
|
|
2459 linenr_T
|
|
2460 diff_lnum_win(lnum, wp)
|
|
2461 linenr_T lnum;
|
|
2462 win_T *wp;
|
|
2463 {
|
|
2464 diff_T *dp;
|
|
2465 int idx;
|
|
2466 int i;
|
|
2467 linenr_T n;
|
|
2468
|
|
2469 idx = diff_buf_idx(curbuf);
|
|
2470 if (idx == DB_COUNT) /* safety check */
|
|
2471 return (linenr_T)0;
|
|
2472
|
672
|
2473 if (curtab->tp_diff_invalid)
|
7
|
2474 ex_diffupdate(NULL); /* update after a big change */
|
|
2475
|
|
2476 /* search for a change that includes "lnum" in the list of diffblocks. */
|
672
|
2477 for (dp = curtab->tp_first_diff; dp != NULL; dp = dp->df_next)
|
7
|
2478 if (lnum <= dp->df_lnum[idx] + dp->df_count[idx])
|
|
2479 break;
|
|
2480
|
|
2481 /* When after the last change, compute relative to the last line number. */
|
|
2482 if (dp == NULL)
|
|
2483 return wp->w_buffer->b_ml.ml_line_count
|
|
2484 - (curbuf->b_ml.ml_line_count - lnum);
|
|
2485
|
|
2486 /* Find index for "wp". */
|
|
2487 i = diff_buf_idx(wp->w_buffer);
|
|
2488 if (i == DB_COUNT) /* safety check */
|
|
2489 return (linenr_T)0;
|
|
2490
|
|
2491 n = lnum + (dp->df_lnum[i] - dp->df_lnum[idx]);
|
|
2492 if (n > dp->df_lnum[i] + dp->df_count[i])
|
|
2493 n = dp->df_lnum[i] + dp->df_count[i];
|
|
2494 return n;
|
|
2495 }
|
|
2496 #endif
|
|
2497
|
|
2498 #endif /* FEAT_DIFF */
|