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