(U+6215): Apply new conventions for glyph granularity.
[chise/xemacs-chise.git.1] / src / gutter.c
index ae568f7..fe6301a 100644 (file)
@@ -50,6 +50,9 @@ Lisp_Object Qgutter_size;
 Lisp_Object Qgutter_visible;
 Lisp_Object Qdefault_gutter_position_changed_hook;
 
+static void
+update_gutter_geometry (struct frame *f, enum gutter_pos pos);
+
 #define SET_GUTTER_WAS_VISIBLE_FLAG(frame, pos, flag)  \
   do {                                                 \
     switch (pos)                                       \
@@ -67,7 +70,7 @@ Lisp_Object Qdefault_gutter_position_changed_hook;
        (frame)->right_gutter_was_visible = flag;       \
        break;                                          \
       default:                                         \
-       abort ();                                       \
+       ABORT ();                                       \
       }                                                        \
   } while (0)
 
@@ -84,7 +87,8 @@ static int gutter_was_visible (struct frame* frame, enum gutter_pos pos)
     case RIGHT_GUTTER:
       return frame->right_gutter_was_visible;
     default:
-      abort ();
+      ABORT ();
+       return 0;       /* To keep the compiler happy */
     }
 }
 
@@ -179,7 +183,7 @@ get_gutter_coords (struct frame *f, enum gutter_pos pos, int *x, int *y,
     case TOP_GUTTER:
       *x = FRAME_LEFT_BORDER_END (f);
       *y = FRAME_TOP_BORDER_END (f);
-      *width = FRAME_RIGHT_BORDER_START (f) 
+      *width = FRAME_RIGHT_BORDER_START (f)
        - FRAME_LEFT_BORDER_END (f);
       *height = FRAME_TOP_GUTTER_BOUNDS (f);
       break;
@@ -187,7 +191,7 @@ get_gutter_coords (struct frame *f, enum gutter_pos pos, int *x, int *y,
     case BOTTOM_GUTTER:
       *x = FRAME_LEFT_BORDER_END (f);
       *y = WINDOW_BOTTOM (bot);
-      *width = FRAME_RIGHT_BORDER_START (f) 
+      *width = FRAME_RIGHT_BORDER_START (f)
        - FRAME_LEFT_BORDER_END (f);
       *height = FRAME_BOTTOM_GUTTER_BOUNDS (f);
       break;
@@ -199,7 +203,7 @@ get_gutter_coords (struct frame *f, enum gutter_pos pos, int *x, int *y,
       *height = WINDOW_BOTTOM (bot)
        - (FRAME_TOP_BORDER_END (f) + FRAME_TOP_GUTTER_BOUNDS (f));
       break;
-      
+
     case RIGHT_GUTTER:
       *x = FRAME_RIGHT_BORDER_START (f)
        - FRAME_RIGHT_GUTTER_BOUNDS (f);
@@ -210,26 +214,61 @@ get_gutter_coords (struct frame *f, enum gutter_pos pos, int *x, int *y,
       break;
 
     default:
-      abort ();
+      ABORT ();
     }
 }
 
+/*
+ display_boxes_in_gutter_p
+
+ Determine whether the required display_glyph_area is completely
+ inside the gutter. -1 means the display_box is not in the gutter. 1
+ means the display_box and the display_glyph_area are in the
+ window. 0 means the display_box is in the gutter but the
+ display_glyph_area is not. */
+int display_boxes_in_gutter_p (struct frame *f, struct display_box* db,
+                              struct display_glyph_area* dga)
+{
+  enum gutter_pos pos;
+  GUTTER_POS_LOOP (pos)
+    {
+      if (FRAME_GUTTER_VISIBLE (f, pos))
+       {
+         int x, y, width, height;
+         get_gutter_coords (f, pos, &x, &y, &width, &height);
+         if (db->xpos + dga->xoffset >= x
+             &&
+             db->ypos + dga->yoffset >= y
+             &&
+             db->xpos + dga->xoffset + dga->width <= x + width
+             &&
+             db->ypos + dga->yoffset + dga->height <= y + height)
+           return 1;
+         else if (db->xpos >= x && db->ypos >= y
+                  && db->xpos + db->width <= x + width
+                  && db->ypos + db->height <= y + height)
+           return 0;
+       }
+    }
+  return -1;
+}
+
 /* Convert the gutter specifier into something we can actually
    display. */
-static Lisp_Object construct_window_gutter_spec (struct window* w, 
+static Lisp_Object construct_window_gutter_spec (struct window* w,
                                                 enum gutter_pos pos)
 {
   Lisp_Object rest, *args;
   int nargs = 0;
   Lisp_Object gutter = RAW_WINDOW_GUTTER (w, pos);
-  
+
   if (STRINGP (gutter) || NILP (gutter))
     return gutter;
 
   GET_LIST_LENGTH (gutter, nargs);
   args = alloca_array (Lisp_Object, nargs >> 1);
   nargs = 0;
-  
+
   for (rest = gutter; !NILP (rest); rest = XCDR (XCDR (rest)))
     {
       /* We only put things in the real gutter that are declared to be
@@ -241,10 +280,100 @@ static Lisp_Object construct_window_gutter_spec (struct window* w,
          args [nargs++] = XCAR (XCDR (rest));
        }
     }
-  
+
   return Fconcat (nargs, args);
 }
 
+/* Sizing gutters is a pain so we try and help the user by determining
+   what height will accommodate all lines. This is useless on left and
+   right gutters as we always have a maximal number of lines. */
+static int
+calculate_gutter_size_from_display_lines (enum gutter_pos pos,
+                                         display_line_dynarr* ddla)
+{
+  int size = 0;
+  struct display_line *dl;
+
+  /* For top and bottom the calculation is easy. */
+  if (pos == TOP_GUTTER || pos == BOTTOM_GUTTER)
+    {
+      /* grab coordinates of last line  */
+      if (Dynarr_length (ddla))
+       {
+         dl = Dynarr_atp (ddla, Dynarr_length (ddla) - 1);
+         size = (dl->ypos + dl->descent - dl->clip) 
+           - (Dynarr_atp (ddla, 0)->ypos - Dynarr_atp (ddla, 0)->ascent);
+       }
+    }
+  /* For left and right we have to do some maths. */
+  else
+    {
+      int start_pos = 0, end_pos = 0, line;
+      for (line = 0; line < Dynarr_length (ddla); line++)
+       {
+         int block;
+         dl = Dynarr_atp (ddla, line);
+
+         for (block = 0; block < Dynarr_largest (dl->display_blocks); block++)
+           {
+             struct display_block *db = Dynarr_atp (dl->display_blocks, block);
+
+             if (db->type == TEXT)
+               {
+                 start_pos = min (db->start_pos, start_pos);
+                 end_pos = max (db->end_pos, end_pos);
+               }
+           }
+       }
+      size = end_pos - start_pos;
+    }
+
+  return size;
+}
+
+static Lisp_Object
+calculate_gutter_size (struct window *w, enum gutter_pos pos)
+{
+  struct frame* f = XFRAME (WINDOW_FRAME (w));
+  int count;
+  display_line_dynarr* ddla;
+  Lisp_Object ret = Qnil;
+
+  /* degenerate case */
+  if (NILP (RAW_WINDOW_GUTTER (w, pos))
+      ||
+      !FRAME_VISIBLE_P (f)
+      ||
+      NILP (w->buffer))
+    return Qnil;
+
+  /* Redisplay code that we use relies on GC not happening. Make it
+     so. */
+  count = specpdl_depth ();
+  record_unwind_protect (restore_gc_inhibit,
+                        make_int (gc_currently_forbidden));
+  gc_currently_forbidden = 1;
+
+  ddla = Dynarr_new (display_line);
+  /* generate some display lines */
+  generate_displayable_area (w, WINDOW_GUTTER (w, pos),
+                            FRAME_LEFT_BORDER_END (f),
+                            FRAME_TOP_BORDER_END (f),
+                            FRAME_RIGHT_BORDER_START (f)
+                            - FRAME_LEFT_BORDER_END (f),
+                            FRAME_BOTTOM_BORDER_START (f)
+                            - FRAME_TOP_BORDER_END (f),
+                            ddla, 0, 0);
+
+  /* Let GC happen again. */
+  unbind_to (count, Qnil);
+
+  ret = make_int (calculate_gutter_size_from_display_lines (pos, ddla));
+  free_display_lines (ddla);
+
+  return ret;
+}
+
 static void
 output_gutter (struct frame *f, enum gutter_pos pos, int force)
 {
@@ -256,7 +385,7 @@ output_gutter (struct frame *f, enum gutter_pos pos, int force)
   int line, border_width;
   face_index findex;
   display_line_dynarr* ddla, *cdla;
-  struct display_line *dl;
+  struct display_line *dl = 0;
   int cdla_len;
 
   if (!WINDOW_LIVE_P (w))
@@ -280,7 +409,7 @@ output_gutter (struct frame *f, enum gutter_pos pos, int force)
   /* generate some display lines */
   generate_displayable_area (w, WINDOW_GUTTER (w, pos),
                             x + border_width, y + border_width,
-                            width - 2 * border_width, 
+                            width - 2 * border_width,
                             height - 2 * border_width, ddla, 0, findex);
 
   /* We only output the gutter if we think something of significance
@@ -295,7 +424,8 @@ output_gutter (struct frame *f, enum gutter_pos pos, int force)
       (f->extents_changed && w->gutter_extent_modiff[pos]))
     {
 #ifdef DEBUG_GUTTERS
-      printf ("gutter redisplay triggered by %s\n", force ? "force" : 
+      printf ("gutter redisplay [%dx%d@%d+%d] triggered by %s,\n", 
+             width, height, x, y, force ? "force" :
              f->faces_changed ? "f->faces_changed" :
              f->frame_changed ? "f->frame_changed" :
              f->gutter_changed ? "f->gutter_changed" :
@@ -313,22 +443,53 @@ output_gutter (struct frame *f, enum gutter_pos pos, int force)
        {
          output_display_line (w, cdla, ddla, line, -1, -1);
        }
-      
+
       /* If the number of display lines has shrunk, adjust. */
       if (cdla_len > Dynarr_length (ddla))
        {
          Dynarr_length (cdla) = Dynarr_length (ddla);
        }
-      
+
       /* grab coordinates of last line and blank after it. */
-      dl = Dynarr_atp (ddla, Dynarr_length (ddla) - 1);
-      ypos = dl->ypos + dl->descent - dl->clip;
+      if (Dynarr_length (ddla) > 0)
+       {
+         dl = Dynarr_atp (ddla, Dynarr_length (ddla) - 1);
+         ypos = dl->ypos + dl->descent - dl->clip;
+       }
+      else
+       ypos = y;
+
       redisplay_clear_region (window, findex, x + border_width , ypos,
                              width - 2 * border_width, height - (ypos - y) - border_width);
+      /* If, for some reason, we have more to display than we have
+         room for, and we are allowed to resize the gutter, then make
+         sure this happens before the next time we try and
+         output. This can happen when face font sizes change. */
+      if (dl && EQ (w->gutter_size[pos], Qautodetect) 
+         && (dl->clip > 0 ||
+             calculate_gutter_size_from_display_lines (pos, ddla) > 
+             WINDOW_GUTTER_SIZE_INTERNAL (w, pos)))
+       {
+         /* #### Ideally we would just mark the specifier as dirty
+         and everything else would "just work". Unfortunately we have
+         two problems with this. One is that the specifier cache
+         won't be recalculated unless the specifier code thinks the
+         cached value has actually changed, even though we have
+         marked the specifier as dirty. Additionally, although doing
+         this results in a gutter size change, we never seem to get
+         back into redisplay so that the frame size can be updated. I
+         think this is because we are already in redisplay and later
+         on the frame will be marked as clean. Thus we also have to
+         force a pending recalculation of the frame size.  */
+         w->gutter_size[pos] = Qnil;
+         Fset_specifier_dirty_flag (Vgutter_size[pos]);
+         update_gutter_geometry (f, pos);
+       }
+
       /* bevel the gutter area if so desired */
       if (border_width != 0)
        {
-         MAYBE_DEVMETH (d, bevel_area, 
+         MAYBE_DEVMETH (d, bevel_area,
                         (w, findex, x, y, width, height, border_width,
                          EDGE_ALL, EDGE_BEVEL_OUT));
        }
@@ -346,63 +507,6 @@ output_gutter (struct frame *f, enum gutter_pos pos, int force)
   w->gutter_extent_modiff [pos] = 0;
 }
 
-/* Sizing gutters is a pain so we try and help the user by detemining
-   what height will accommodate all lines. This is useless on left and
-   right gutters as we always have a maximal number of lines. */
-static Lisp_Object
-calculate_gutter_size (struct window *w, enum gutter_pos pos)
-{
-  struct frame* f = XFRAME (WINDOW_FRAME (w));
-  int ypos, count;
-  display_line_dynarr* ddla;
-  struct display_line *dl;
-
-  /* we cannot autodetect gutter sizes for the left and right as there
-     is no reasonable metric to use */
-  assert (pos == TOP_GUTTER || pos == BOTTOM_GUTTER);
-  /* degenerate case */
-  if (NILP (RAW_WINDOW_GUTTER (w, pos))
-      ||
-      !FRAME_VISIBLE_P (f)
-      ||
-      NILP (w->buffer))
-    return Qnil;
-
-  /* Redisplay code that we use relies on GC not happening. Make it
-     so. */
-  count = specpdl_depth ();
-  record_unwind_protect (restore_gc_inhibit,
-                        make_int (gc_currently_forbidden));
-  gc_currently_forbidden = 1;
-
-  ddla = Dynarr_new (display_line);
-  /* generate some display lines */
-  generate_displayable_area (w, WINDOW_GUTTER (w, pos),
-                            FRAME_LEFT_BORDER_END (f),
-                            0,
-                            FRAME_RIGHT_BORDER_START (f)
-                            - FRAME_LEFT_BORDER_END (f),
-                            200,
-                            ddla, 0, 0);
-
-  /* Let GC happen again. */
-  unbind_to (count, Qnil);
-
-  /* grab coordinates of last line  */
-  if (Dynarr_length (ddla))
-    {
-      dl = Dynarr_atp (ddla, Dynarr_length (ddla) - 1);
-      ypos = dl->ypos + dl->descent - dl->clip;
-      free_display_lines (ddla);
-      return make_int (ypos);
-    }
-  else
-    {
-      free_display_lines (ddla);
-      return Qnil;
-    }
-}
-
 static void
 clear_gutter (struct frame *f, enum gutter_pos pos)
 {
@@ -440,7 +544,7 @@ mark_gutters (struct frame* f)
 
 /* This is called by extent_changed_for_redisplay, so that redisplay
    knows exactly what extents have changed. */
-void 
+void
 gutter_extent_signal_changed_region_maybe (Lisp_Object obj,
                                           Bufpos start, Bufpos end)
 {
@@ -455,7 +559,7 @@ gutter_extent_signal_changed_region_maybe (Lisp_Object obj,
       enum gutter_pos pos;
       Lisp_Object window = FRAME_LAST_NONMINIBUF_WINDOW (f);
       struct window* w = XWINDOW (window);
-      
+
       GUTTER_POS_LOOP (pos)
        {
          if (EQ (WINDOW_GUTTER (w, pos), obj))
@@ -468,10 +572,32 @@ gutter_extent_signal_changed_region_maybe (Lisp_Object obj,
 
 /* We have to change the gutter geometry separately to the gutter
    update since it needs to occur outside of redisplay proper. */
+static void
+update_gutter_geometry (struct frame *f, enum gutter_pos pos)
+{
+  /* If the gutter geometry has changed then re-layout the
+     frame. If we are in display there is almost no point in doing
+     anything else since the frame size changes will be delayed
+     until we are out of redisplay proper. */
+  if (FRAME_GUTTER_BOUNDS (f, pos) != f->current_gutter_bounds[pos])
+    {
+      int width, height;
+      pixel_to_char_size (f, FRAME_PIXWIDTH (f), FRAME_PIXHEIGHT (f),
+                         &width, &height);
+      change_frame_size (f, height, width, 0);
+      MARK_FRAME_LAYOUT_CHANGED (f);
+    }
+
+  /* Mark sizes as up-to-date. */
+  f->current_gutter_bounds[pos] = FRAME_GUTTER_BOUNDS (f, pos);
+}
+
 void
 update_frame_gutter_geometry (struct frame *f)
 {
-  if (f->gutter_changed || f->windows_structure_changed)
+  if (f->gutter_changed 
+      || f->frame_layout_changed 
+      || f->windows_structure_changed)
     {
       enum gutter_pos pos;
 
@@ -481,20 +607,7 @@ update_frame_gutter_geometry (struct frame *f)
          until we are out of redisplay proper. */
       GUTTER_POS_LOOP (pos)
        {
-         if (FRAME_GUTTER_BOUNDS (f, pos) != f->current_gutter_bounds[pos])
-           {
-             int width, height;
-             pixel_to_char_size (f, FRAME_PIXWIDTH (f), FRAME_PIXHEIGHT (f),
-                                 &width, &height);
-             change_frame_size (f, height, width, 0);
-             break;
-           }
-       }
-
-      GUTTER_POS_LOOP (pos)
-       {
-         /* Mark sizes as up-to-date. */
-         f->current_gutter_bounds[pos] = FRAME_GUTTER_BOUNDS (f, pos);
+         update_gutter_geometry (f, pos);
        }
     }
 }
@@ -506,10 +619,10 @@ update_frame_gutters (struct frame *f)
       f->gutter_changed || f->glyphs_changed ||
       f->size_changed || f->subwindows_changed ||
       f->windows_changed || f->windows_structure_changed ||
-      f->extents_changed)
+      f->extents_changed || f->frame_layout_changed)
     {
       enum gutter_pos pos;
-      
+
       /* We don't actually care about these when outputting the gutter
          so locally disable them. */
       int local_clip_changed = f->clip_changed;
@@ -557,6 +670,10 @@ redraw_exposed_gutter (struct frame *f, enum gutter_pos pos, int x, int y,
   if (((x + width) < g_x) || (x > (g_x + g_width)))
     return;
 
+#ifdef DEBUG_WIDGETS
+  printf ("redrawing gutter after expose %d+%d, %dx%d\n",
+         x, y, width, height);
+#endif
   /* #### optimize this - redrawing the whole gutter for every expose
      is very expensive. We reset the current display lines because if
      they're being exposed they are no longer current. */
@@ -573,11 +690,16 @@ redraw_exposed_gutters (struct frame *f, int x, int y, int width,
                        int height)
 {
   enum gutter_pos pos;
+
+  /* We have to be "in display" when we output the gutter - make it
+     so. */
+  hold_frame_size_changes ();
   GUTTER_POS_LOOP (pos)
     {
       if (FRAME_GUTTER_VISIBLE (f, pos))
        redraw_exposed_gutter (f, pos, x, y, width, height);
     }
+  unhold_one_frame_size_changes (f);
 }
 
 void
@@ -639,21 +761,15 @@ See `default-gutter-position'.
                              list1 (Fcons (Qnil, Qzero)));
       set_specifier_fallback (Vgutter_border_width[new],
                              Vdefault_gutter_border_width);
-      /* We don't realy want the left and right gutters to default to
-         visible. */
-      set_specifier_fallback (Vgutter_visible_p[cur],
-                             cur == TOP_GUTTER || cur == BOTTOM_GUTTER ?
-                             list1 (Fcons (Qnil, Qt))
-                             : list1 (Fcons (Qnil, Qnil)));
-      set_specifier_fallback (Vgutter_visible_p[new],
-                             Vdefault_gutter_visible_p);
+      set_specifier_fallback (Vgutter_visible_p[cur], list1 (Fcons (Qnil, Qt)));
+      set_specifier_fallback (Vgutter_visible_p[new], Vdefault_gutter_visible_p);
 
       Vdefault_gutter_position = position;
       unhold_frame_size_changes ();
     }
 
   run_hook (Qdefault_gutter_position_changed_hook);
-  
+
   return position;
 }
 
@@ -722,7 +838,7 @@ gutter_validate (Lisp_Object instantiator)
 {
   if (NILP (instantiator))
     return;
-  
+
   /* Must be a string or a plist. */
   if (!STRINGP (instantiator) && NILP (Fvalid_plist_p (instantiator)))
       signal_simple_error ("Gutter spec must be string, plist or nil", instantiator);
@@ -730,7 +846,7 @@ gutter_validate (Lisp_Object instantiator)
   if (!STRINGP (instantiator))
     {
       Lisp_Object rest;
-      
+
       for (rest = instantiator; !NILP (rest); rest = XCDR (XCDR (rest)))
        {
          if (!SYMBOLP (XCAR (rest))
@@ -742,14 +858,9 @@ gutter_validate (Lisp_Object instantiator)
 
 DEFUN ("gutter-specifier-p", Fgutter_specifier_p, 1, 1, 0, /*
 Return non-nil if OBJECT is a gutter specifier.
-Gutter specifiers are used to specify the format of a gutter.
-The values of the variables `default-gutter', `top-gutter',
-`left-gutter', `right-gutter', and `bottom-gutter' are always
-gutter specifiers.
-
-Valid gutter instantiators are called "gutter descriptors" and are
-either strings or property-lists of strings.  See `default-gutter' for
-a description of the exact format.
+
+See `make-gutter-specifier' for a description of possible gutter
+instantiators.
 */
        (object))
 {
@@ -774,7 +885,7 @@ gutter_specs_changed (Lisp_Object specifier, struct window *w,
 {
   w->real_gutter[pos] = construct_window_gutter_spec (w, pos);
   w->real_gutter_size[pos] = w->gutter_size[pos];
-  
+
   if (EQ (w->real_gutter_size[pos], Qautodetect)
       && !NILP (w->gutter_visible_p[pos]))
     {
@@ -835,7 +946,7 @@ gutter_geometry_changed_in_window (Lisp_Object specifier, struct window *w,
          w->real_gutter_size [pos] = calculate_gutter_size (w, pos);
        }
     }
-  
+
   MARK_GUTTER_CHANGED;
   MARK_MODELINE_CHANGED;
   MARK_WINDOWS_CHANGED (w);
@@ -885,15 +996,8 @@ gutter_size_validate (Lisp_Object instantiator)
 DEFUN ("gutter-size-specifier-p", Fgutter_size_specifier_p, 1, 1, 0, /*
 Return non-nil if OBJECT is a gutter-size specifier.
 
-Gutter-size specifiers are used to specify the size of a gutter.  The
-values of the variables `default-gutter-size', `top-gutter-size',
-`left-gutter-size', `right-gutter-size', and `bottom-gutter-size' are
-always gutter-size specifiers.
-
-Valid gutter-size instantiators are either integers or the special
-symbol 'autodetect. If a gutter-size is set to 'autodetect them the
-size of the gutter will be adjusted to just accomodate the gutters
-contents. 'autodetect only works for top and bottom gutters.
+See `make-gutter-size-specifier' for a description of possible gutter-size
+instantiators.
 */
        (object))
 {
@@ -911,7 +1015,7 @@ gutter_visible_validate (Lisp_Object instantiator)
     return;
 
   if (!NILP (instantiator) && !EQ (instantiator, Qt) && !CONSP (instantiator))
-    signal_simple_error ("Gutter visibility must be a boolean or list of symbols", 
+    signal_simple_error ("Gutter visibility must be a boolean or list of symbols",
                         instantiator);
 
   if (CONSP (instantiator))
@@ -921,7 +1025,7 @@ gutter_visible_validate (Lisp_Object instantiator)
       EXTERNAL_LIST_LOOP (rest, instantiator)
        {
          if (!SYMBOLP (XCAR (rest)))
-             signal_simple_error ("Gutter visibility must be a boolean or list of symbols", 
+             signal_simple_error ("Gutter visibility must be a boolean or list of symbols",
                                   instantiator);
        }
     }
@@ -930,17 +1034,8 @@ gutter_visible_validate (Lisp_Object instantiator)
 DEFUN ("gutter-visible-specifier-p", Fgutter_visible_specifier_p, 1, 1, 0, /*
 Return non-nil if OBJECT is a gutter-visible specifier.
 
-Gutter-visible specifiers are used to specify the visibility of a
-gutter.  The values of the variables `default-gutter-visible-p',
-`top-gutter-visible-p', `left-gutter-visible-p',
-`right-gutter-visible-p', and `bottom-gutter-visible-p' are always
-gutter-visible specifiers.
-
-Valid gutter-visible instantiators are t, nil or a list of symbols.
-If a gutter-visible instantiator is set to a list of symbols, and the
-correspondong gutter specification is a property-list strings, then
-elements of the gutter specification will only be visible if the
-corresponding symbol occurs in the gutter-visible instantiator.
+See `make-gutter-visible-specifier' for a description of possible
+gutter-visible instantiators.
 */
        (object))
 {
@@ -962,11 +1057,13 @@ Ensure that all gutters are correctly showing their gutter specifier.
       DEVICE_FRAME_LOOP (frmcons, d)
        {
          struct frame *f = XFRAME (XCAR (frmcons));
-         
+
+         MAYBE_DEVMETH (d, frame_output_begin, (f));
+
          /* Sequence is quite important here. We not only want to
           redisplay the gutter area but we also want to flush any
           frame size changes out so that the gutter redisplay happens
-          in a kosha environment. 
+          in a kosha environment.
 
           This is not only so that things look right but so that
           glyph redisplay optimization kicks in, by default display
@@ -988,13 +1085,10 @@ Ensure that all gutters are correctly showing their gutter specifier.
              update_frame_gutters (f);
              unhold_one_frame_size_changes (f);
            }
-       }
-      /* We now call the output_end routine for tty frames.  We delay
-        doing so in order to avoid cursor flicker.  So much for 100%
-        encapsulation. */
-      if (DEVICE_TTY_P (d))
-       DEVMETH (d, output_end, (d));
-      
+
+         MAYBE_DEVMETH (d, frame_output_end, (f));
+      }
+
       d->gutter_changed = 0;
     }
 
@@ -1047,8 +1141,8 @@ syms_of_gutter (void)
 
   defsymbol (&Qgutter_size, "gutter-size");
   defsymbol (&Qgutter_visible, "gutter-visible");
-  defsymbol (&Qdefault_gutter_position_changed_hook, 
-            "default-gutter-position-changed");
+  defsymbol (&Qdefault_gutter_position_changed_hook,
+            "default-gutter-position-changed-hook");
 }
 
 void
@@ -1133,7 +1227,7 @@ before being displayed.  */ );
   set_specifier_caching (Vdefault_gutter,
                         offsetof (struct window, default_gutter),
                         default_gutter_specs_changed,
-                        0, 0);
+                        0, 0, 1);
 
   DEFVAR_SPECIFIER ("top-gutter",
                    &Vgutter[TOP_GUTTER] /*
@@ -1145,7 +1239,7 @@ See `default-gutter' for a description of a valid gutter instantiator.
   set_specifier_caching (Vgutter[TOP_GUTTER],
                         offsetof (struct window, gutter[TOP_GUTTER]),
                         top_gutter_specs_changed,
-                        0, 0);
+                        0, 0, 1);
 
   DEFVAR_SPECIFIER ("bottom-gutter",
                    &Vgutter[BOTTOM_GUTTER] /*
@@ -1162,7 +1256,7 @@ displayed even if you provide a value for `bottom-gutter'.
   set_specifier_caching (Vgutter[BOTTOM_GUTTER],
                         offsetof (struct window, gutter[BOTTOM_GUTTER]),
                         bottom_gutter_specs_changed,
-                        0, 0);
+                        0, 0, 1);
 
   DEFVAR_SPECIFIER ("left-gutter",
                    &Vgutter[LEFT_GUTTER] /*
@@ -1179,7 +1273,7 @@ displayed even if you provide a value for `left-gutter'.
   set_specifier_caching (Vgutter[LEFT_GUTTER],
                         offsetof (struct window, gutter[LEFT_GUTTER]),
                         left_gutter_specs_changed,
-                        0, 0);
+                        0, 0, 1);
 
   DEFVAR_SPECIFIER ("right-gutter",
                    &Vgutter[RIGHT_GUTTER] /*
@@ -1196,7 +1290,7 @@ displayed even if you provide a value for `right-gutter'.
   set_specifier_caching (Vgutter[RIGHT_GUTTER],
                         offsetof (struct window, gutter[RIGHT_GUTTER]),
                         right_gutter_specs_changed,
-                        0, 0);
+                        0, 0, 1);
 
   /* initially, top inherits from default; this can be
      changed with `set-default-gutter-position'. */
@@ -1236,7 +1330,7 @@ is the default.
   set_specifier_caching (Vdefault_gutter_height,
                         offsetof (struct window, default_gutter_height),
                         default_gutter_size_changed_in_window,
-                        0, 0);
+                        0, 0, 1);
 
   DEFVAR_SPECIFIER ("default-gutter-width", &Vdefault_gutter_width /*
 *Width of the default gutter, if it's oriented vertically.
@@ -1244,11 +1338,11 @@ This is a specifier; use `set-specifier' to change it.
 
 See `default-gutter-height' for more information.
 */ );
-  Vdefault_gutter_width = Fmake_specifier (Qnatnum);
+  Vdefault_gutter_width = Fmake_specifier (Qgutter_size);
   set_specifier_caching (Vdefault_gutter_width,
                         offsetof (struct window, default_gutter_width),
                         default_gutter_size_changed_in_window,
-                        0, 0);
+                        0, 0, 1);
 
   DEFVAR_SPECIFIER ("top-gutter-height",
                    &Vgutter_size[TOP_GUTTER] /*
@@ -1260,7 +1354,7 @@ See `default-gutter-height' for more information.
   Vgutter_size[TOP_GUTTER] = Fmake_specifier (Qgutter_size);
   set_specifier_caching (Vgutter_size[TOP_GUTTER],
                         offsetof (struct window, gutter_size[TOP_GUTTER]),
-                        gutter_geometry_changed_in_window, 0, 0);
+                        gutter_geometry_changed_in_window, 0, 0, 1);
 
   DEFVAR_SPECIFIER ("bottom-gutter-height",
                    &Vgutter_size[BOTTOM_GUTTER] /*
@@ -1272,7 +1366,7 @@ See `default-gutter-height' for more information.
   Vgutter_size[BOTTOM_GUTTER] = Fmake_specifier (Qgutter_size);
   set_specifier_caching (Vgutter_size[BOTTOM_GUTTER],
                         offsetof (struct window, gutter_size[BOTTOM_GUTTER]),
-                        gutter_geometry_changed_in_window, 0, 0);
+                        gutter_geometry_changed_in_window, 0, 0, 1);
 
   DEFVAR_SPECIFIER ("left-gutter-width",
                    &Vgutter_size[LEFT_GUTTER] /*
@@ -1281,10 +1375,10 @@ This is a specifier; use `set-specifier' to change it.
 
 See `default-gutter-height' for more information.
 */ );
-  Vgutter_size[LEFT_GUTTER] = Fmake_specifier (Qnatnum);
+  Vgutter_size[LEFT_GUTTER] = Fmake_specifier (Qgutter_size);
   set_specifier_caching (Vgutter_size[LEFT_GUTTER],
                         offsetof (struct window, gutter_size[LEFT_GUTTER]),
-                        gutter_geometry_changed_in_window, 0, 0);
+                        gutter_geometry_changed_in_window, 0, 0, 1);
 
   DEFVAR_SPECIFIER ("right-gutter-width",
                    &Vgutter_size[RIGHT_GUTTER] /*
@@ -1293,15 +1387,18 @@ This is a specifier; use `set-specifier' to change it.
 
 See `default-gutter-height' for more information.
 */ );
-  Vgutter_size[RIGHT_GUTTER] = Fmake_specifier (Qnatnum);
+  Vgutter_size[RIGHT_GUTTER] = Fmake_specifier (Qgutter_size);
   set_specifier_caching (Vgutter_size[RIGHT_GUTTER],
                         offsetof (struct window, gutter_size[RIGHT_GUTTER]),
-                        gutter_geometry_changed_in_window, 0, 0);
+                        gutter_geometry_changed_in_window, 0, 0, 1);
 
   fb = Qnil;
 #ifdef HAVE_TTY
   fb = Fcons (Fcons (list1 (Qtty), Qautodetect), fb);
 #endif
+#ifdef HAVE_GTK
+  fb = Fcons (Fcons (list1 (Qgtk), Qautodetect), fb);
+#endif
 #ifdef HAVE_X_WINDOWS
   fb = Fcons (Fcons (list1 (Qx), Qautodetect), fb);
 #endif
@@ -1314,15 +1411,17 @@ See `default-gutter-height' for more information.
 
   fb = Qnil;
 #ifdef HAVE_TTY
-  fb = Fcons (Fcons (list1 (Qtty), Qzero), fb);
+  fb = Fcons (Fcons (list1 (Qtty), Qautodetect), fb);
 #endif
 #ifdef HAVE_X_WINDOWS
-  fb = Fcons (Fcons (list1 (Qx), make_int (DEFAULT_GUTTER_WIDTH)), fb);
+  fb = Fcons (Fcons (list1 (Qx), Qautodetect), fb);
+#endif
+#ifdef HAVE_GTK
+  fb = Fcons (Fcons (list1 (Qgtk), Qautodetect), fb);
 #endif
 #ifdef HAVE_MS_WINDOWS
-  fb = Fcons (Fcons (list1 (Qmsprinter), Qzero), fb);
-  fb = Fcons (Fcons (list1 (Qmswindows), 
-                    make_int (DEFAULT_GUTTER_WIDTH)), fb);
+  fb = Fcons (Fcons (list1 (Qmsprinter), Qautodetect), fb);
+  fb = Fcons (Fcons (list1 (Qmswindows), Qautodetect), fb);
 #endif
   if (!NILP (fb))
     set_specifier_fallback (Vdefault_gutter_width, fb);
@@ -1351,7 +1450,7 @@ instead.
   set_specifier_caching (Vdefault_gutter_border_width,
                         offsetof (struct window, default_gutter_border_width),
                         default_gutter_border_width_changed_in_window,
-                        0, 0);
+                        0, 0, 0);
 
   DEFVAR_SPECIFIER ("top-gutter-border-width",
                    &Vgutter_border_width[TOP_GUTTER] /*
@@ -1364,7 +1463,7 @@ See `default-gutter-height' for more information.
   set_specifier_caching (Vgutter_border_width[TOP_GUTTER],
                         offsetof (struct window,
                                   gutter_border_width[TOP_GUTTER]),
-                        gutter_geometry_changed_in_window, 0, 0);
+                        gutter_geometry_changed_in_window, 0, 0, 0);
 
   DEFVAR_SPECIFIER ("bottom-gutter-border-width",
                    &Vgutter_border_width[BOTTOM_GUTTER] /*
@@ -1377,7 +1476,7 @@ See `default-gutter-height' for more information.
   set_specifier_caching (Vgutter_border_width[BOTTOM_GUTTER],
                         offsetof (struct window,
                                   gutter_border_width[BOTTOM_GUTTER]),
-                        gutter_geometry_changed_in_window, 0, 0);
+                        gutter_geometry_changed_in_window, 0, 0, 0);
 
   DEFVAR_SPECIFIER ("left-gutter-border-width",
                    &Vgutter_border_width[LEFT_GUTTER] /*
@@ -1390,7 +1489,7 @@ See `default-gutter-height' for more information.
   set_specifier_caching (Vgutter_border_width[LEFT_GUTTER],
                         offsetof (struct window,
                                   gutter_border_width[LEFT_GUTTER]),
-                        gutter_geometry_changed_in_window, 0, 0);
+                        gutter_geometry_changed_in_window, 0, 0, 0);
 
   DEFVAR_SPECIFIER ("right-gutter-border-width",
                    &Vgutter_border_width[RIGHT_GUTTER] /*
@@ -1403,7 +1502,7 @@ See `default-gutter-height' for more information.
   set_specifier_caching (Vgutter_border_width[RIGHT_GUTTER],
                         offsetof (struct window,
                                   gutter_border_width[RIGHT_GUTTER]),
-                        gutter_geometry_changed_in_window, 0, 0);
+                        gutter_geometry_changed_in_window, 0, 0, 0);
 
   fb = Qnil;
 #ifdef HAVE_TTY
@@ -1445,7 +1544,7 @@ visibility specifiers have a fallback value of true.
                         offsetof (struct window,
                                   default_gutter_visible_p),
                         default_gutter_visible_p_changed_in_window,
-                        0, 0);
+                        0, 0, 0);
 
   DEFVAR_SPECIFIER ("top-gutter-visible-p",
                    &Vgutter_visible_p[TOP_GUTTER] /*
@@ -1458,7 +1557,7 @@ See `default-gutter-visible-p' for more information.
   set_specifier_caching (Vgutter_visible_p[TOP_GUTTER],
                         offsetof (struct window,
                                   gutter_visible_p[TOP_GUTTER]),
-                        top_gutter_specs_changed, 0, 0);
+                        top_gutter_specs_changed, 0, 0, 0);
 
   DEFVAR_SPECIFIER ("bottom-gutter-visible-p",
                    &Vgutter_visible_p[BOTTOM_GUTTER] /*
@@ -1471,7 +1570,7 @@ See `default-gutter-visible-p' for more information.
   set_specifier_caching (Vgutter_visible_p[BOTTOM_GUTTER],
                         offsetof (struct window,
                                   gutter_visible_p[BOTTOM_GUTTER]),
-                        bottom_gutter_specs_changed, 0, 0);
+                        bottom_gutter_specs_changed, 0, 0, 0);
 
   DEFVAR_SPECIFIER ("left-gutter-visible-p",
                    &Vgutter_visible_p[LEFT_GUTTER] /*
@@ -1484,7 +1583,7 @@ See `default-gutter-visible-p' for more information.
   set_specifier_caching (Vgutter_visible_p[LEFT_GUTTER],
                         offsetof (struct window,
                                   gutter_visible_p[LEFT_GUTTER]),
-                        left_gutter_specs_changed, 0, 0);
+                        left_gutter_specs_changed, 0, 0, 0);
 
   DEFVAR_SPECIFIER ("right-gutter-visible-p",
                    &Vgutter_visible_p[RIGHT_GUTTER] /*
@@ -1497,7 +1596,7 @@ See `default-gutter-visible-p' for more information.
   set_specifier_caching (Vgutter_visible_p[RIGHT_GUTTER],
                         offsetof (struct window,
                                   gutter_visible_p[RIGHT_GUTTER]),
-                        right_gutter_specs_changed, 0, 0);
+                        right_gutter_specs_changed, 0, 0, 0);
 
   /* initially, top inherits from default; this can be
      changed with `set-default-gutter-position'. */