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