Merge branch 'vs/prompt-avoid-unset-variable'
[gitweb.git] / xdiff / xemit.c
index 969100d99d4bfd7c86cf4464ebecc0564db1c1b7..49aa16ff78d8c0f10942e0f519543cc6befabb9d 100644 (file)
@@ -155,6 +155,18 @@ static long get_func_line(xdfenv_t *xe, xdemitconf_t const *xecfg,
        return -1;
 }
 
+static int is_empty_rec(xdfile_t *xdf, long ri)
+{
+       const char *rec;
+       long len = xdl_get_rec(xdf, ri, &rec);
+
+       while (len > 0 && XDL_ISSPACE(*rec)) {
+               rec++;
+               len--;
+       }
+       return !len;
+}
+
 int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
                  xdemitconf_t const *xecfg) {
        long s1, s2, e1, e2, lctx;
@@ -176,12 +188,18 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
                        /* Appended chunk? */
                        if (i1 >= xe->xdf1.nrec) {
                                char dummy[1];
+                               long i2 = xch->i2;
 
                                /*
                                 * We don't need additional context if
-                                * a whole function was added.
+                                * a whole function was added, possibly
+                                * starting with empty lines.
                                 */
-                               if (match_func_rec(&xe->xdf2, xecfg, xch->i2,
+                               while (i2 < xe->xdf2.nrec &&
+                                      is_empty_rec(&xe->xdf2, i2))
+                                       i2++;
+                               if (i2 < xe->xdf2.nrec &&
+                                   match_func_rec(&xe->xdf2, xecfg, i2,
                                                   dummy, sizeof(dummy)) >= 0)
                                        goto post_context_calculation;
 
@@ -213,6 +231,8 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
                        long fe1 = get_func_line(xe, xecfg, NULL,
                                                 xche->i1 + xche->chg1,
                                                 xe->xdf1.nrec);
+                       while (fe1 > 0 && is_empty_rec(&xe->xdf1, fe1 - 1))
+                               fe1--;
                        if (fe1 < 0)
                                fe1 = xe->xdf1.nrec;
                        if (fe1 > e1) {
@@ -226,7 +246,8 @@ int xdl_emit_diff(xdfenv_t *xe, xdchange_t *xscr, xdemitcb_t *ecb,
                         * its new end.
                         */
                        if (xche->next) {
-                               long l = xche->next->i1;
+                               long l = XDL_MIN(xche->next->i1,
+                                                xe->xdf1.nrec - 1);
                                if (l <= e1 ||
                                    get_func_line(xe, xecfg, NULL, l, e1) < 0) {
                                        xche = xche->next;