home *** CD-ROM | disk | FTP | other *** search
/ vim.ftp.fu-berlin.de / 2015-02-03.vim.ftp.fu-berlin.de.tar / vim.ftp.fu-berlin.de / unix / vim-6.2.tar.bz2 / vim-6.2.tar / vim62 / src / diff.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-27  |  49.2 KB  |  2,074 lines

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