]> code.delx.au - gnu-emacs/blobdiff - src/xdisp.c
Merge branch 'master' into xwidget
[gnu-emacs] / src / xdisp.c
index e6bbd859fe75f7f4d65109a11123097c5bbbad1d..01d598fe2c2d2f5281b192cd8d652bacedf93cfb 100644 (file)
@@ -1,6 +1,6 @@
 /* Display generation from window structure and buffer text.
 
-Copyright (C) 1985-1988, 1993-1995, 1997-2014 Free Software Foundation,
+Copyright (C) 1985-1988, 1993-1995, 1997-2015 Free Software Foundation,
 Inc.
 
 This file is part of GNU Emacs.
@@ -318,58 +318,18 @@ along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.  */
 #include TERM_HEADER
 #endif /* HAVE_WINDOW_SYSTEM */
 
+#ifdef HAVE_XWIDGETS
+#include "xwidget.h"
+#endif
 #ifndef FRAME_X_OUTPUT
 #define FRAME_X_OUTPUT(f) ((f)->output_data.x)
 #endif
 
 #define INFINITY 10000000
 
-Lisp_Object Qoverriding_local_map, Qoverriding_terminal_local_map;
-Lisp_Object Qwindow_scroll_functions;
-static Lisp_Object Qwindow_text_change_functions;
-static Lisp_Object Qredisplay_end_trigger_functions;
-Lisp_Object Qinhibit_point_motion_hooks;
-static Lisp_Object QCeval, QCpropertize;
-Lisp_Object QCfile, QCdata;
-static Lisp_Object Qfontified;
-static Lisp_Object Qgrow_only;
-static Lisp_Object Qinhibit_eval_during_redisplay;
-static Lisp_Object Qbuffer_position, Qposition, Qobject;
-static Lisp_Object Qright_to_left, Qleft_to_right;
-
-/* Cursor shapes.  */
-Lisp_Object Qbar, Qhbar, Qbox, Qhollow;
-
-/* Pointer shapes.  */
-static Lisp_Object Qarrow, Qhand;
-Lisp_Object Qtext;
-
 /* Holds the list (error).  */
 static Lisp_Object list_of_error;
 
-Lisp_Object Qfontification_functions;
-
-static Lisp_Object Qwrap_prefix;
-static Lisp_Object Qline_prefix;
-static Lisp_Object Qredisplay_internal;
-
-/* Non-nil means don't actually do any redisplay.  */
-
-Lisp_Object Qinhibit_redisplay;
-
-/* Names of text properties relevant for redisplay.  */
-
-Lisp_Object Qdisplay;
-
-Lisp_Object Qspace, QCalign_to;
-static Lisp_Object QCrelative_width, QCrelative_height;
-Lisp_Object Qleft_margin, Qright_margin;
-static Lisp_Object Qspace_width, Qraise;
-static Lisp_Object Qslice;
-Lisp_Object Qcenter;
-static Lisp_Object Qmargin, Qpointer;
-static Lisp_Object Qline_height;
-
 #ifdef HAVE_WINDOW_SYSTEM
 
 /* Test if overflow newline into fringe.  Called with iterator IT
@@ -403,31 +363,6 @@ static Lisp_Object Qline_height;
           && (*BYTE_POS_ADDR (IT_BYTEPOS (*it)) == ' '                 \
               || *BYTE_POS_ADDR (IT_BYTEPOS (*it)) == '\t'))))         \
 
-/* Name of the face used to highlight trailing whitespace.  */
-
-static Lisp_Object Qtrailing_whitespace;
-
-/* Name and number of the face used to highlight escape glyphs.  */
-
-static Lisp_Object Qescape_glyph;
-
-/* Name and number of the face used to highlight non-breaking spaces.  */
-
-static Lisp_Object Qnobreak_space;
-
-/* The symbol `image' which is the car of the lists used to represent
-   images in Lisp.  Also a tool bar style.  */
-
-Lisp_Object Qimage;
-
-/* The image map types.  */
-Lisp_Object QCmap;
-static Lisp_Object QCpointer;
-static Lisp_Object Qrect, Qcircle, Qpoly;
-
-/* Tool bar styles */
-Lisp_Object Qboth, Qboth_horiz, Qtext_image_horiz;
-
 /* Non-zero means print newline to stdout before next mini-buffer
    message.  */
 
@@ -477,21 +412,6 @@ static struct text_pos this_line_min_pos;
 
 static struct buffer *this_line_buffer;
 
-
-/* Values of those variables at last redisplay are stored as
-   properties on `overlay-arrow-position' symbol.  However, if
-   Voverlay_arrow_position is a marker, last-arrow-position is its
-   numerical position.  */
-
-static Lisp_Object Qlast_arrow_position, Qlast_arrow_string;
-
-/* Alternative overlay-arrow-string and overlay-arrow-bitmap
-   properties on a symbol in overlay-arrow-variable-list.  */
-
-static Lisp_Object Qoverlay_arrow_string, Qoverlay_arrow_bitmap;
-
-Lisp_Object Qmenu_bar_update_hook;
-
 /* Nonzero if an overlay arrow has been displayed in this window.  */
 
 static bool overlay_arrow_seen;
@@ -567,11 +487,6 @@ static bool display_last_displayed_message_p;
 
 static bool message_buf_print;
 
-/* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable.  */
-
-static Lisp_Object Qinhibit_menubar_update;
-static Lisp_Object Qmessage_truncate_lines;
-
 /* Set to 1 in clear_message to make redisplay_internal aware
    of an emptied echo area.  */
 
@@ -691,8 +606,6 @@ int trace_move;
 #define TRACE_MOVE(x)  (void) 0
 #endif
 
-static Lisp_Object Qauto_hscroll_mode;
-
 /* Buffer being redisplayed -- for redisplay_window_error.  */
 
 static struct buffer *displayed_buffer;
@@ -712,8 +625,8 @@ enum prop_handled
 
 struct props
 {
-  /* The name of the property.  */
-  Lisp_Object *name;
+  /* The symbol index of the name of the property.  */
+  short name;
 
   /* A unique index for the property.  */
   enum prop_idx idx;
@@ -734,14 +647,14 @@ static enum prop_handled handle_fontified_prop (struct it *);
 
 static struct props it_props[] =
 {
-  {&Qfontified,                FONTIFIED_PROP_IDX,     handle_fontified_prop},
+  {SYMBOL_INDEX (Qfontified),  FONTIFIED_PROP_IDX,     handle_fontified_prop},
   /* Handle `face' before `display' because some sub-properties of
      `display' need to know the face.  */
-  {&Qface,             FACE_PROP_IDX,          handle_face_prop},
-  {&Qdisplay,          DISPLAY_PROP_IDX,       handle_display_prop},
-  {&Qinvisible,                INVISIBLE_PROP_IDX,     handle_invisible_prop},
-  {&Qcomposition,      COMPOSITION_PROP_IDX,   handle_composition_prop},
-  {NULL,               0,                      NULL}
+  {SYMBOL_INDEX (Qface),       FACE_PROP_IDX,          handle_face_prop},
+  {SYMBOL_INDEX (Qdisplay),    DISPLAY_PROP_IDX,       handle_display_prop},
+  {SYMBOL_INDEX (Qinvisible),  INVISIBLE_PROP_IDX,     handle_invisible_prop},
+  {SYMBOL_INDEX (Qcomposition),        COMPOSITION_PROP_IDX, handle_composition_prop},
+  {0,                          0,                      NULL}
 };
 
 /* Value is the position described by X.  If X is a marker, value is
@@ -796,9 +709,6 @@ static struct glyph_slice null_glyph_slice = { 0, 0, 0, 0 };
 
 bool redisplaying_p;
 
-static Lisp_Object Qinhibit_free_realized_faces;
-static Lisp_Object Qmode_line_default_help_echo;
-
 /* If a string, XTread_socket generates an event to display that string.
    (The display is done in read_char.)  */
 
@@ -824,15 +734,6 @@ static struct atimer *hourglass_atimer;
 
 #endif /* HAVE_WINDOW_SYSTEM */
 
-/* Name of the face used to display glyphless characters.  */
-static Lisp_Object Qglyphless_char;
-
-/* Symbol for the purpose of Vglyphless_char_display.  */
-static Lisp_Object Qglyphless_char_display;
-
-/* Method symbols for Vglyphless_char_display.  */
-static Lisp_Object Qhex_code, Qempty_box, Qthin_space, Qzero_width;
-
 /* Default number of seconds to wait before displaying an hourglass
    cursor.  */
 #define DEFAULT_HOURGLASS_DELAY 1
@@ -944,6 +845,9 @@ static int next_element_from_c_string (struct it *);
 static int next_element_from_buffer (struct it *);
 static int next_element_from_composition (struct it *);
 static int next_element_from_image (struct it *);
+#ifdef HAVE_XWIDGETS
+static int next_element_from_xwidget(struct it *);
+#endif
 static int next_element_from_stretch (struct it *);
 static void load_overlay_strings (struct it *, ptrdiff_t);
 static int init_from_display_pos (struct it *, struct window *,
@@ -1406,6 +1310,7 @@ pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
   struct text_pos top;
   int visible_p = 0;
   struct buffer *old_buffer = NULL;
+  bool r2l = false;
 
   if (FRAME_INITIAL_P (XFRAME (WINDOW_FRAME (w))))
     return visible_p;
@@ -1691,6 +1596,8 @@ pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
          *rowh = max (0, (min (bottom_y, it.last_visible_y)
                           - max (top_y, window_top_y)));
          *vpos = it.vpos;
+         if (it.bidi_it.paragraph_dir == R2L)
+           r2l = true;
        }
     }
   else
@@ -1720,6 +1627,8 @@ pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
                           - max (it2.current_y,
                                  WINDOW_HEADER_LINE_HEIGHT (w))));
          *vpos = it2.vpos;
+         if (it2.bidi_it.paragraph_dir == R2L)
+           r2l = true;
        }
       else
        bidi_unshelve_cache (it2data, 1);
@@ -1729,10 +1638,20 @@ pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
   if (old_buffer)
     set_buffer_internal_1 (old_buffer);
 
-  if (visible_p && w->hscroll > 0)
-    *x -=
-      window_hscroll_limited (w, WINDOW_XFRAME (w))
-      * WINDOW_FRAME_COLUMN_WIDTH (w);
+  if (visible_p)
+    {
+      if (w->hscroll > 0)
+       *x -=
+         window_hscroll_limited (w, WINDOW_XFRAME (w))
+         * WINDOW_FRAME_COLUMN_WIDTH (w);
+      /* For lines in an R2L paragraph, we need to mirror the X pixel
+         coordinate wrt the text area.  For the reasons, see the
+         commentary in buffer_posn_from_coords and the explanation of
+         the geometry used by the move_it_* functions at the end of
+         the large commentary near the beginning of this file.  */
+      if (r2l)
+       *x = window_box_width (w, TEXT_AREA) - *x - 1;
+    }
 
 #if 0
   /* Debugging code.  */
@@ -2681,8 +2600,6 @@ safe__call1 (bool inhibit_quit, Lisp_Object fn, ...)
   return retval;
 }
 
-static Lisp_Object Qeval;
-
 Lisp_Object
 safe_eval (Lisp_Object sexpr)
 {
@@ -2813,9 +2730,9 @@ init_iterator (struct it *it, struct window *w,
      free realized faces now because they depend on face definitions
      that might have changed.  Don't free faces while there might be
      desired matrices pending which reference these faces.  */
-  if (face_change_count && !inhibit_free_realized_faces)
+  if (face_change && !inhibit_free_realized_faces)
     {
-      face_change_count = 0;
+      face_change = false;
       free_all_realized_faces (Qnil);
     }
 
@@ -2835,17 +2752,14 @@ init_iterator (struct it *it, struct window *w,
        row = MATRIX_HEADER_LINE_ROW (w->desired_matrix);
     }
 
-  /* Clear IT.  */
-  memset (it, 0, sizeof *it);
+  /* Clear IT, and set it->object and other IT's Lisp objects to Qnil.
+     Other parts of redisplay rely on that.  */
+  memclear (it, sizeof *it);
   it->current.overlay_string_index = -1;
   it->current.dpvec_index = -1;
   it->base_face_id = remapped_base_face_id;
-  it->string = Qnil;
   IT_STRING_CHARPOS (*it) = IT_STRING_BYTEPOS (*it) = -1;
   it->paragraph_embedding = L2R;
-  it->bidi_it.string.lstring = Qnil;
-  it->bidi_it.string.s = NULL;
-  it->bidi_it.string.bufpos = 0;
   it->bidi_it.w = w;
 
   /* The window in which we iterate over current_buffer:  */
@@ -2866,7 +2780,6 @@ init_iterator (struct it *it, struct window *w,
                                  * FRAME_LINE_HEIGHT (it->f));
       else if (it->f->extra_line_spacing > 0)
        it->extra_line_spacing = it->f->extra_line_spacing;
-      it->max_extra_line_spacing = 0;
     }
 
   /* If realized faces have been removed, e.g. because of face
@@ -2878,10 +2791,6 @@ init_iterator (struct it *it, struct window *w,
   if (FRAME_FACE_CACHE (it->f)->used == 0)
     recompute_basic_faces (it->f);
 
-  /* Current value of the `slice', `space-width', and 'height' properties.  */
-  it->slice.x = it->slice.y = it->slice.width = it->slice.height = Qnil;
-  it->space_width = Qnil;
-  it->font_height = Qnil;
   it->override_ascent = -1;
 
   /* Are control characters displayed as `^C'?  */
@@ -2919,21 +2828,19 @@ init_iterator (struct it *it, struct window *w,
   it->tab_width = SANE_TAB_WIDTH (current_buffer);
 
   /* Are lines in the display truncated?  */
-  if (base_face_id != DEFAULT_FACE_ID
-      || it->w->hscroll
-      || (! WINDOW_FULL_WIDTH_P (it->w)
-         && ((!NILP (Vtruncate_partial_width_windows)
-              && !INTEGERP (Vtruncate_partial_width_windows))
-             || (INTEGERP (Vtruncate_partial_width_windows)
-                 /* PXW: Shall we do something about this?  */
-                 && (WINDOW_TOTAL_COLS (it->w)
-                     < XINT (Vtruncate_partial_width_windows))))))
+  if (TRUNCATE != 0)
     it->line_wrap = TRUNCATE;
-  else if (NILP (BVAR (current_buffer, truncate_lines)))
+  if (base_face_id == DEFAULT_FACE_ID
+      && !it->w->hscroll
+      && (WINDOW_FULL_WIDTH_P (it->w)
+         || NILP (Vtruncate_partial_width_windows)
+         || (INTEGERP (Vtruncate_partial_width_windows)
+             /* PXW: Shall we do something about this?  */
+             && (XINT (Vtruncate_partial_width_windows)
+                 <= WINDOW_TOTAL_COLS (it->w))))
+      && NILP (BVAR (current_buffer, truncate_lines)))
     it->line_wrap = NILP (BVAR (current_buffer, word_wrap))
       ? WINDOW_WRAP : WORD_WRAP;
-  else
-    it->line_wrap = TRUNCATE;
 
   /* Get dimensions of truncation and continuation glyphs.  These are
      displayed as fringe bitmaps under X, but we need them for such
@@ -3605,7 +3512,8 @@ compute_stop_pos (struct it *it)
 
       /* Get properties here.  */
       for (p = it_props; p->handler; ++p)
-       values_here[p->idx] = textget (iv->plist, *p->name);
+       values_here[p->idx] = textget (iv->plist,
+                                      builtin_lisp_symbol (p->name));
 
       /* Look for an interval following iv that has different
         properties.  */
@@ -3617,9 +3525,8 @@ compute_stop_pos (struct it *it)
        {
          for (p = it_props; p->handler; ++p)
            {
-             Lisp_Object new_value;
-
-             new_value = textget (next_iv->plist, *p->name);
+             Lisp_Object new_value = textget (next_iv->plist,
+                                              builtin_lisp_symbol (p->name));
              if (!EQ (values_here[p->idx], new_value))
                break;
            }
@@ -3976,7 +3883,7 @@ handle_face_prop (struct it *it)
                                   &next_stop,
                                   (IT_CHARPOS (*it)
                                    + TEXT_PROP_DISTANCE_LIMIT),
-                                  0, it->base_face_id);
+                                  false, it->base_face_id);
 
       /* Is this a start of a run of characters with box face?
         Caveat: this can be called for a freshly initialized
@@ -4052,7 +3959,7 @@ handle_face_prop (struct it *it)
                                       &next_stop,
                                       (IT_CHARPOS (*it)
                                        + TEXT_PROP_DISTANCE_LIMIT),
-                                      0,
+                                      false,
                                       from_overlay);
        }
       else
@@ -4087,7 +3994,7 @@ handle_face_prop (struct it *it)
                                             IT_STRING_CHARPOS (*it),
                                             bufpos,
                                             &next_stop,
-                                            base_face_id, 0);
+                                            base_face_id, false);
 
       /* Is this a start of a run of characters with box?  Caveat:
         this can be called for a freshly allocated iterator; face_id
@@ -4229,7 +4136,7 @@ face_before_or_after_it_pos (struct it *it, int before_p)
                                         charpos,
                                         bufpos,
                                         &next_check_charpos,
-                                        base_face_id, 0);
+                                        base_face_id, false);
 
       /* Correct the face for charsets different from ASCII.  Do it
         for the multibyte case only.  The face returned above is
@@ -4318,7 +4225,7 @@ face_before_or_after_it_pos (struct it *it, int before_p)
       face_id = face_at_buffer_position (it->w,
                                         CHARPOS (pos),
                                         &next_check_charpos,
-                                        limit, 0, -1);
+                                        limit, false, -1);
 
       /* Correct the face for charsets different from ASCII.  Do it
         for the multibyte case only.  The face returned above is
@@ -4777,6 +4684,9 @@ handle_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
   if (CONSP (spec)
       /* Simple specifications.  */
       && !EQ (XCAR (spec), Qimage)
+#ifdef HAVE_XWIDGETS
+      && !EQ (XCAR (spec), Qxwidget)
+#endif
       && !EQ (XCAR (spec), Qspace)
       && !EQ (XCAR (spec), Qwhen)
       && !EQ (XCAR (spec), Qslice)
@@ -5223,7 +5133,12 @@ handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
              || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
                 && valid_image_p (value))
 #endif /* not HAVE_WINDOW_SYSTEM */
-             || (CONSP (value) && EQ (XCAR (value), Qspace)));
+             || (CONSP (value) && EQ (XCAR (value), Qspace))
+#ifdef HAVE_XWIDGETS
+             || ((it ? FRAME_WINDOW_P (it->f) : frame_window_p)
+                && valid_xwidget_spec_p(value))
+#endif
+             );
 
   if (valid_p && !display_replaced_p)
     {
@@ -5298,6 +5213,18 @@ handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
          *position = it->position = start_pos;
          retval = 1 + (it->area == TEXT_AREA);
        }
+#ifdef HAVE_XWIDGETS
+      else if (valid_xwidget_spec_p(value))
+       {
+          it->what = IT_XWIDGET;
+          it->method = GET_FROM_XWIDGET;
+          it->position = start_pos;
+         it->object = NILP (object) ? it->w->contents : object;
+         *position = start_pos;
+
+          it->xwidget = lookup_xwidget(value);
+       }
+#endif
 #ifdef HAVE_WINDOW_SYSTEM
       else
        {
@@ -6034,6 +5961,11 @@ push_it (struct it *it, struct text_pos *position)
     case GET_FROM_STRETCH:
       p->u.stretch.object = it->object;
       break;
+#ifdef HAVE_XWIDGETS
+    case GET_FROM_XWIDGET:
+      p->u.xwidget.object = it->object;
+      break;
+#endif
     }
   p->position = position ? *position : it->position;
   p->current = it->current;
@@ -6127,6 +6059,11 @@ pop_it (struct it *it)
       it->object = p->u.image.object;
       it->slice = p->u.image.slice;
       break;
+#ifdef HAVE_XWIDGETS
+    case GET_FROM_XWIDGET:
+      it->object = p->u.xwidget.object;
+      break;
+#endif
     case GET_FROM_STRETCH:
       it->object = p->u.stretch.object;
       break;
@@ -6789,6 +6726,9 @@ static int (* get_next_element[NUM_IT_METHODS]) (struct it *it) =
   next_element_from_c_string,
   next_element_from_image,
   next_element_from_stretch
+#ifdef HAVE_XWIDGETS
+  ,next_element_from_xwidget
+#endif
 };
 
 #define GET_NEXT_DISPLAY_ELEMENT(it) (*get_next_element[(it)->method]) (it)
@@ -7264,7 +7204,7 @@ get_next_display_element (struct it *it)
                    {
                      next_face_id = face_at_buffer_position
                        (it->w, CHARPOS (pos), &ignore,
-                        CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, 0, -1);
+                        CHARPOS (pos) + TEXT_PROP_DISTANCE_LIMIT, false, -1);
                      it->end_of_box_run_p
                        = (FACE_FROM_ID (it->f, next_face_id)->box
                           == FACE_NO_BOX);
@@ -7634,6 +7574,10 @@ set_iterator_to_next (struct it *it, int reseat_p)
 
     case GET_FROM_IMAGE:
     case GET_FROM_STRETCH:
+#ifdef HAVE_XWIDGETS
+    case GET_FROM_XWIDGET:
+#endif
+
       /* The position etc with which we have to proceed are on
         the stack.  The position may be at the end of a string,
          if the `display' property takes up the whole string.  */
@@ -8023,7 +7967,7 @@ next_element_from_c_string (struct it *it)
   eassert (!it->bidi_p || it->s == it->bidi_it.string.s);
   it->what = IT_CHARACTER;
   BYTEPOS (it->position) = CHARPOS (it->position) = 0;
-  it->object = Qnil;
+  it->object = make_number (0);
 
   /* With bidi reordering, the character to display might not be the
      character at IT_CHARPOS.  BIDI_IT.FIRST_ELT non-zero means that
@@ -8096,6 +8040,16 @@ next_element_from_image (struct it *it)
   return 1;
 }
 
+#ifdef HAVE_XWIDGETS
+/* im not sure about this FIXME JAVE*/
+static int
+next_element_from_xwidget (struct it *it)
+{
+  it->what = IT_XWIDGET;
+  return 1;
+}
+#endif
+
 
 /* Fill iterator IT with next display element from a stretch glyph
    property.  IT->object is the value of the text property.  Value is
@@ -8385,22 +8339,18 @@ next_element_from_buffer (struct it *it)
 static void
 run_redisplay_end_trigger_hook (struct it *it)
 {
-  Lisp_Object args[3];
-
   /* IT->glyph_row should be non-null, i.e. we should be actually
      displaying something, or otherwise we should not run the hook.  */
   eassert (it->glyph_row);
 
-  /* Set up hook arguments.  */
-  args[0] = Qredisplay_end_trigger_functions;
-  args[1] = it->window;
-  XSETINT (args[2], it->redisplay_end_trigger_charpos);
+  ptrdiff_t charpos = it->redisplay_end_trigger_charpos;
   it->redisplay_end_trigger_charpos = 0;
 
   /* Since we are *trying* to run these functions, don't try to run
      them again, even if they get an error.  */
   wset_redisplay_end_trigger (it->w, Qnil);
-  Frun_hook_with_args (3, args);
+  CALLN (Frun_hook_with_args, Qredisplay_end_trigger_functions, it->window,
+        make_number (charpos));
 
   /* Notice if it changed the face of the character we are on.  */
   handle_face_prop (it);
@@ -9366,6 +9316,7 @@ move_it_to (struct it *it, ptrdiff_t to_charpos, int to_x, int to_y, int to_vpos
       && it->current_x == it->last_visible_x - 1
       && it->c != '\n'
       && it->c != '\t'
+      && it->w->window_end_valid
       && it->vpos < it->w->window_end_vpos)
     {
       it->continuation_lines_width += it->current_x;
@@ -9899,7 +9850,6 @@ include the height of both, if present, in the return value.  */)
 void
 add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
 {
-  Lisp_Object args[3];
   Lisp_Object msg, fmt;
   char *buffer;
   ptrdiff_t len;
@@ -9909,10 +9859,8 @@ add_to_log (const char *format, Lisp_Object arg1, Lisp_Object arg2)
   fmt = msg = Qnil;
   GCPRO4 (fmt, msg, arg1, arg2);
 
-  args[0] = fmt = build_string (format);
-  args[1] = arg1;
-  args[2] = arg2;
-  msg = Fformat (3, args);
+  fmt = build_string (format);
+  msg = CALLN (Fformat, fmt, arg1, arg2);
 
   len = SBYTES (msg) + 1;
   buffer = SAFE_ALLOCA (len);
@@ -10327,15 +10275,13 @@ message_with_string (const char *m, Lisp_Object string, int log)
         initialized yet, just toss it.  */
       if (f->glyphs_initialized_p)
        {
-         Lisp_Object args[2], msg;
          struct gcpro gcpro1, gcpro2;
 
-         args[0] = build_string (m);
-         args[1] = msg = string;
-         GCPRO2 (args[0], msg);
-         gcpro1.nvars = 2;
+         Lisp_Object fmt = build_string (m);
+         Lisp_Object msg = string;
+         GCPRO2 (fmt, msg);
 
-         msg = Fformat (2, args);
+         msg = CALLN (Fformat, fmt, msg);
 
          if (log)
            message3 (msg);
@@ -12441,6 +12387,7 @@ redisplay_tool_bar (struct frame *f)
       if (new_height != WINDOW_PIXEL_HEIGHT (w))
        {
          x_change_tool_bar_height (f, new_height);
+         frame_default_tool_bar_height = new_height;
          /* Always do that now.  */
          clear_glyph_matrix (w->desired_matrix);
          f->fonts_changed = 1;
@@ -12535,6 +12482,7 @@ redisplay_tool_bar (struct frame *f)
          if (change_height_p)
            {
              x_change_tool_bar_height (f, new_height);
+             frame_default_tool_bar_height = new_height;
              clear_glyph_matrix (w->desired_matrix);
              f->n_tool_bar_rows = nrows;
              f->fonts_changed = 1;
@@ -13463,7 +13411,7 @@ redisplay_internal (void)
   specbind (Qinhibit_free_realized_faces, Qnil);
 
   /* Record this function, so it appears on the profiler's backtraces.  */
-  record_in_backtrace (Qredisplay_internal, &Qnil, 0);
+  record_in_backtrace (Qredisplay_internal, 0, 0);
 
   FOR_EACH_FRAME (tail, frame)
     XFRAME (frame)->already_hscrolled_p = 0;
@@ -13478,10 +13426,10 @@ redisplay_internal (void)
   last_glyphless_glyph_frame = NULL;
   last_glyphless_glyph_face_id = (1 << FACE_ID_BITS);
 
-  /* If face_change_count is non-zero, init_iterator will free all
-     realized faces, which includes the faces referenced from current
-     matrices.  So, we can't reuse current matrices in this case.  */
-  if (face_change_count)
+  /* If face_change, init_iterator will free all realized faces, which
+     includes the faces referenced from current matrices.  So, we
+     can't reuse current matrices in this case.  */
+  if (face_change)
     windows_or_buffers_changed = 47;
 
   if ((FRAME_TERMCAP_P (sf) || FRAME_MSDOS_P (sf))
@@ -13864,6 +13812,17 @@ redisplay_internal (void)
 
        retry_frame:
 
+#if defined (HAVE_WINDOW_SYSTEM) && !defined (USE_GTK) && !defined (HAVE_NS)
+         /* Redisplay internal tool bar if this is the first time so we
+            can adjust the frame height right now, if necessary.  */
+         if (!f->tool_bar_redisplayed_once)
+           {
+             if (redisplay_tool_bar (f))
+               adjust_frame_glyphs (f);
+             f->tool_bar_redisplayed_once = true;
+           }
+#endif
+
          if (FRAME_WINDOW_P (f) || FRAME_TERMCAP_P (f) || f == sf)
            {
              bool gcscrollbars
@@ -14370,14 +14329,14 @@ set_cursor_from_row (struct window *w, struct glyph_row *row,
       if (!row->reversed_p)
        {
          while (glyph < end
-                && INTEGERP (glyph->object)
+                && NILP (glyph->object)
                 && glyph->charpos < 0)
            {
              x += glyph->pixel_width;
              ++glyph;
            }
          while (end > glyph
-                && INTEGERP ((end - 1)->object)
+                && NILP ((end - 1)->object)
                 /* CHARPOS is zero for blanks and stretch glyphs
                    inserted by extend_face_to_end_of_line.  */
                 && (end - 1)->charpos <= 0)
@@ -14395,20 +14354,20 @@ set_cursor_from_row (struct window *w, struct glyph_row *row,
          glyph += row->used[TEXT_AREA] - 1;
 
          while (glyph > end + 1
-                && INTEGERP (glyph->object)
+                && NILP (glyph->object)
                 && glyph->charpos < 0)
            {
              --glyph;
              x -= glyph->pixel_width;
            }
-         if (INTEGERP (glyph->object) && glyph->charpos < 0)
+         if (NILP (glyph->object) && glyph->charpos < 0)
            --glyph;
          /* By default, in reversed rows we put the cursor on the
             rightmost (first in the reading order) glyph.  */
          for (g = end + 1; g < glyph; g++)
            x += g->pixel_width;
          while (end < glyph
-                && INTEGERP ((end + 1)->object)
+                && NILP ((end + 1)->object)
                 && (end + 1)->charpos <= 0)
            ++end;
          glyph_before = glyph + 1;
@@ -14439,7 +14398,7 @@ set_cursor_from_row (struct window *w, struct glyph_row *row,
     while (/* not marched to end of glyph row */
           glyph < end
           /* glyph was not inserted by redisplay for internal purposes */
-          && !INTEGERP (glyph->object))
+          && !NILP (glyph->object))
       {
        if (BUFFERP (glyph->object))
          {
@@ -14527,7 +14486,7 @@ set_cursor_from_row (struct window *w, struct glyph_row *row,
        ++glyph;
       }
   else if (glyph > end)        /* row is reversed */
-    while (!INTEGERP (glyph->object))
+    while (!NILP (glyph->object))
       {
        if (BUFFERP (glyph->object))
          {
@@ -14604,16 +14563,16 @@ set_cursor_from_row (struct window *w, struct glyph_row *row,
        && BUFFERP (glyph->object) && glyph->charpos == pt_old)
       && !(bpos_max <= pt_old && pt_old <= bpos_covered))
     {
-      /* An empty line has a single glyph whose OBJECT is zero and
+      /* An empty line has a single glyph whose OBJECT is nil and
         whose CHARPOS is the position of a newline on that line.
         Note that on a TTY, there are more glyphs after that, which
         were produced by extend_face_to_end_of_line, but their
         CHARPOS is zero or negative.  */
       int empty_line_p =
        (row->reversed_p ? glyph > glyphs_end : glyph < glyphs_end)
-       && INTEGERP (glyph->object) && glyph->charpos > 0
+       && NILP (glyph->object) && glyph->charpos > 0
        /* On a TTY, continued and truncated rows also have a glyph at
-          their end whose OBJECT is zero and whose CHARPOS is
+          their end whose OBJECT is nil and whose CHARPOS is
           positive (the continuation and truncation glyphs), but such
           rows are obviously not "empty".  */
        && !(row->continued_p || row->truncated_on_right_p);
@@ -14890,7 +14849,7 @@ set_cursor_from_row (struct window *w, struct glyph_row *row,
                          && string_from_text_prop)
                      /* this candidate is from newline and its
                         position is not an exact match */
-                     || (INTEGERP (glyph->object)
+                     || (NILP (glyph->object)
                          && glyph->charpos != pt_old)))))
        return 0;
       /* If this candidate gives an exact match, use that.  */
@@ -14899,7 +14858,7 @@ set_cursor_from_row (struct window *w, struct glyph_row *row,
               terminating newline of a line, and point is on that
               newline, it wins because it's an exact match.  */
            || (!row->continued_p
-               && INTEGERP (glyph->object)
+               && NILP (glyph->object)
                && glyph->charpos == 0
                && pt_old == MATRIX_ROW_END_CHARPOS (row) - 1))
          /* Otherwise, keep the candidate that comes from a row
@@ -15478,7 +15437,8 @@ try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_ste
   /* Likewise there was a check whether window_end_vpos is nil or larger
      than the window.  Now window_end_vpos is int and so never nil, but
      let's leave eassert to check whether it fits in the window.  */
-  eassert (w->window_end_vpos < w->current_matrix->nrows);
+  eassert (!w->window_end_valid
+          || w->window_end_vpos < w->current_matrix->nrows);
 
   /* Handle case where text has not changed, only point, and it has
      not moved off the frame.  */
@@ -15742,7 +15702,7 @@ try_cursor_movement (Lisp_Object window, struct text_pos startp, int *scroll_ste
 
                          exact_match_p =
                            (BUFFERP (g->object) && g->charpos == PT)
-                           || (INTEGERP (g->object)
+                           || (NILP (g->object)
                                && (g->charpos == PT
                                    || (g->charpos == 0 && endpos - 1 == PT)));
                        }
@@ -17096,6 +17056,13 @@ try_window_reusing_current_matrix (struct window *w)
     return 0;
 #endif
 
+#ifdef HAVE_XWIDGETS_xxx
+ //currently this is needed to detect xwidget movement reliably. or probably not.
+  printf("try_window_reusing_current_matrix\n");
+    return 0;
+#endif
+
+
   if (/* This function doesn't handle terminal frames.  */
       !FRAME_WINDOW_P (f)
       /* Don't try to reuse the display if windows have been split
@@ -18218,6 +18185,21 @@ try_window_id (struct window *w)
   if (f->fonts_changed)
     return -1;
 
+  /* The redisplay iterations in display_line above could have
+     triggered font-lock, which could have done something that
+     invalidates IT->w window's end-point information, on which we
+     rely below.  E.g., one package, which will remain unnamed, used
+     to install a font-lock-fontify-region-function that called
+     bury-buffer, whose side effect is to switch the buffer displayed
+     by IT->w, and that predictably resets IT->w's window_end_valid
+     flag, which we already tested at the entry to this function.
+     Amply punish such packages/modes by giving up on this
+     optimization in those cases.  */
+  if (!w->window_end_valid)
+    {
+      clear_glyph_matrix (w->desired_matrix);
+      return -1;
+    }
 
   /* Compute differences in buffer positions, y-positions etc.  for
      lines reused at the bottom of the window.  Compute what we can
@@ -18596,7 +18578,7 @@ dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
                ? 'B'
                : (STRINGP (glyph->object)
                   ? 'S'
-                  : (INTEGERP (glyph->object)
+                  : (NILP (glyph->object)
                      ? '0'
                      : '-'))),
               glyph->pixel_width,
@@ -18619,7 +18601,7 @@ dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
                ? 'B'
                : (STRINGP (glyph->object)
                   ? 'S'
-                  : (INTEGERP (glyph->object)
+                  : (NILP (glyph->object)
                      ? '0'
                      : '-'))),
               glyph->pixel_width,
@@ -18640,7 +18622,7 @@ dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
                ? 'B'
                : (STRINGP (glyph->object)
                   ? 'S'
-                  : (INTEGERP (glyph->object)
+                  : (NILP (glyph->object)
                      ? '0'
                      : '-'))),
               glyph->pixel_width,
@@ -18661,7 +18643,7 @@ dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
                ? 'B'
                : (STRINGP (glyph->object)
                   ? 'S'
-                  : (INTEGERP (glyph->object)
+                  : (NILP (glyph->object)
                      ? '0'
                      : '-'))),
               glyph->pixel_width,
@@ -18675,6 +18657,28 @@ dump_glyph (struct glyph_row *row, struct glyph *glyph, int area)
               glyph->left_box_line_p,
               glyph->right_box_line_p);
     }
+#ifdef HAVE_XWIDGETS
+  else if (glyph->type == XWIDGET_GLYPH)
+    {
+      fprintf (stderr,
+              "  %5d %4c %6d %c %3d 0x%05x %c %4d %1.1d%1.1d\n",
+              glyph - row->glyphs[TEXT_AREA],
+              'X',
+              glyph->charpos,
+              (BUFFERP (glyph->object)
+               ? 'B'
+               : (STRINGP (glyph->object)
+                  ? 'S'
+                  : '-')),
+              glyph->pixel_width,
+              glyph->u.xwidget,
+              '.',
+              glyph->face_id,
+              glyph->left_box_line_p,
+              glyph->right_box_line_p);
+
+    }
+#endif
 }
 
 
@@ -18761,7 +18765,7 @@ dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
              struct glyph *glyph = row->glyphs[area] + i;
              if (i == row->used[area] - 1
                  && area == TEXT_AREA
-                 && INTEGERP (glyph->object)
+                 && NILP (glyph->object)
                  && glyph->type == CHAR_GLYPH
                  && glyph->u.ch == ' ')
                {
@@ -18892,7 +18896,7 @@ usage: (trace-to-stderr STRING &rest OBJECTS)  */)
   (ptrdiff_t nargs, Lisp_Object *args)
 {
   Lisp_Object s = Fformat (nargs, args);
-  fprintf (stderr, "%s", SDATA (s));
+  fwrite (SDATA (s), 1, SBYTES (s), stderr);
   return Qnil;
 }
 
@@ -18991,7 +18995,7 @@ insert_left_trunc_glyphs (struct it *it)
   truncate_it.area = TEXT_AREA;
   truncate_it.glyph_row->used[TEXT_AREA] = 0;
   CHARPOS (truncate_it.position) = BYTEPOS (truncate_it.position) = -1;
-  truncate_it.object = make_number (0);
+  truncate_it.object = Qnil;
   produce_special_glyphs (&truncate_it, IT_TRUNCATION);
 
   /* Overwrite glyphs from IT with truncation glyphs.  */
@@ -19274,7 +19278,7 @@ append_space_for_newline (struct it *it, int default_face_p)
 
          it->what = IT_CHARACTER;
          memset (&it->position, 0, sizeof it->position);
-         it->object = make_number (0);
+         it->object = Qnil;
          it->c = it->char_to_display = ' ';
          it->len = 1;
 
@@ -19466,7 +19470,7 @@ extend_face_to_end_of_line (struct it *it)
              else
                it->face_id = face->id;
              it->start_of_box_run_p = 0;
-             append_stretch_glyph (it, make_number (0), stretch_width,
+             append_stretch_glyph (it, Qnil, stretch_width,
                                    it->ascent + it->descent, stretch_ascent);
              it->position = saved_pos;
              it->avoid_cursor_p = saved_avoid_cursor;
@@ -19496,7 +19500,7 @@ extend_face_to_end_of_line (struct it *it)
 
       it->what = IT_CHARACTER;
       memset (&it->position, 0, sizeof it->position);
-      it->object = make_number (0);
+      it->object = Qnil;
       it->c = it->char_to_display = ' ';
       it->len = 1;
 
@@ -19625,14 +19629,14 @@ highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
        {
          while (glyph >= start
                 && glyph->type == CHAR_GLYPH
-                && INTEGERP (glyph->object))
+                && NILP (glyph->object))
            --glyph;
        }
       else
        {
          while (glyph <= start
                 && glyph->type == CHAR_GLYPH
-                && INTEGERP (glyph->object))
+                && NILP (glyph->object))
            ++glyph;
        }
 
@@ -19646,7 +19650,7 @@ highlight_trailing_whitespace (struct frame *f, struct glyph_row *row)
                  && glyph->u.ch == ' '))
          && trailing_whitespace_p (glyph->charpos))
        {
-         int face_id = lookup_named_face (f, Qtrailing_whitespace, 0);
+         int face_id = lookup_named_face (f, Qtrailing_whitespace, false);
          if (face_id < 0)
            return;
 
@@ -19995,10 +19999,9 @@ find_row_edges (struct it *it, struct glyph_row *row,
            {
              start = r1->glyphs[TEXT_AREA];
              end = start + r1->used[TEXT_AREA];
-             /* Glyphs inserted by redisplay have an integer (zero)
-                as their object.  */
+             /* Glyphs inserted by redisplay have nil as their object.  */
              while (end > start
-                    && INTEGERP ((end - 1)->object)
+                    && NILP ((end - 1)->object)
                     && (end - 1)->charpos <= 0)
                --end;
              if (end > start)
@@ -20019,7 +20022,7 @@ find_row_edges (struct it *it, struct glyph_row *row,
              end = r1->glyphs[TEXT_AREA] - 1;
              start = end + r1->used[TEXT_AREA];
              while (end < start
-                    && INTEGERP ((end + 1)->object)
+                    && NILP ((end + 1)->object)
                     && (end + 1)->charpos <= 0)
                ++end;
              if (end < start)
@@ -21172,7 +21175,7 @@ Value is the new character position of point.  */)
 
 #define ROW_GLYPH_NEWLINE_P(ROW,GLYPH)         \
   (!(ROW)->continued_p                         \
-   && INTEGERP ((GLYPH)->object)               \
+   && NILP ((GLYPH)->object)                   \
    && (GLYPH)->type == CHAR_GLYPH              \
    && (GLYPH)->u.ch == ' '                     \
    && (GLYPH)->charpos >= 0                    \
@@ -21214,7 +21217,7 @@ Value is the new character position of point.  */)
              w->cursor.vpos = -1;
              return make_number (PT);
            }
-         else if (!INTEGERP (g->object) && !EQ (g->object, gpt->object))
+         else if (!NILP (g->object) && !EQ (g->object, gpt->object))
            {
              ptrdiff_t new_pos;
 
@@ -21251,7 +21254,7 @@ Value is the new character position of point.  */)
              return make_number (PT);
            }
        }
-      if (g == e || INTEGERP (g->object))
+      if (g == e || NILP (g->object))
        {
          if (row->truncated_on_left_p || row->truncated_on_right_p)
            goto simulate_display;
@@ -21284,7 +21287,7 @@ Value is the new character position of point.  */)
                         EOB also has one glyph, but its charpos is -1.  */
                      || (row->ends_at_zv_p
                          && !row->reversed_p
-                         && INTEGERP (g->object)
+                         && NILP (g->object)
                          && g->type == CHAR_GLYPH
                          && g->u.ch == ' '))
                    {
@@ -21322,7 +21325,7 @@ Value is the new character position of point.  */)
                      || g->type == STRETCH_GLYPH
                      || (row->ends_at_zv_p
                          && row->reversed_p
-                         && INTEGERP (g->object)
+                         && NILP (g->object)
                          && g->type == CHAR_GLYPH
                          && g->u.ch == ' '))
                    {
@@ -21686,13 +21689,13 @@ Emacs UBA implementation, in particular with the test suite.  */)
          /* Skip over glyphs at the start of the row that was
             generated by redisplay for its own needs.  */
          while (g < e
-                && INTEGERP (g->object)
+                && NILP (g->object)
                 && g->charpos < 0)
            g++;
          g1 = g;
 
          /* Count the "interesting" glyphs in this row.  */
-         for (nglyphs = 0; g < e && !INTEGERP (g->object); g++)
+         for (nglyphs = 0; g < e && !NILP (g->object); g++)
            nglyphs++;
 
          /* Create and fill the array.  */
@@ -21705,11 +21708,11 @@ Emacs UBA implementation, in particular with the test suite.  */)
          g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
          e = row->glyphs[TEXT_AREA] - 1;
          while (g > e
-                && INTEGERP (g->object)
+                && NILP (g->object)
                 && g->charpos < 0)
            g--;
          g1 = g;
-         for (nglyphs = 0; g > e && !INTEGERP (g->object); g--)
+         for (nglyphs = 0; g > e && !NILP (g->object); g--)
            nglyphs++;
          levels = make_uninit_vector (nglyphs);
          for (i = 0; g1 > g; i++, g1--)
@@ -23657,7 +23660,7 @@ display_string (const char *string, Lisp_Object lisp_string, Lisp_Object face_st
 
       it->face_id
        = face_at_string_position (it->w, face_string, face_string_pos,
-                                  0, &endptr, it->base_face_id, 0);
+                                  0, &endptr, it->base_face_id, false);
       face = FACE_FROM_ID (it->f, it->face_id);
       it->face_box_p = face->box != FACE_NO_BOX;
     }
@@ -24093,6 +24096,13 @@ calc_pixel_width_or_height (double *res, struct it *it, Lisp_Object prop,
 
              return OK_PIXELS (width_p ? img->width : img->height);
            }
+#ifdef HAVE_XWIDGETS
+         if (FRAME_WINDOW_P (it->f) && valid_xwidget_spec_p (prop))
+           {
+              //TODO dont return dummy size
+              return OK_PIXELS (width_p ? 100 : 100);
+            }
+#endif
 #endif
          if (EQ (car, Qplus) || EQ (car, Qminus))
            {
@@ -24597,6 +24607,18 @@ fill_image_glyph_string (struct glyph_string *s)
 }
 
 
+#ifdef HAVE_XWIDGETS
+static void
+fill_xwidget_glyph_string (struct glyph_string *s)
+{
+  eassert (s->first_glyph->type == XWIDGET_GLYPH);
+  s->face = FACE_FROM_ID (s->f, s->first_glyph->face_id);
+  s->font = s->face->font;
+  s->width = s->first_glyph->pixel_width;
+  s->ybase += s->first_glyph->voffset;
+  s->xwidget = s->first_glyph->u.xwidget;
+}
+#endif
 /* Fill glyph string S from a sequence of stretch glyphs.
 
    START is the index of the first glyph to consider,
@@ -24932,6 +24954,20 @@ compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
        }                                                               \
      while (0)
 
+#ifdef HAVE_XWIDGETS
+#define BUILD_XWIDGET_GLYPH_STRING(START, END, HEAD, TAIL, HL, X, LAST_X) \
+     do                                                                        \
+       { \
+        s = (struct glyph_string *) alloca (sizeof *s);                \
+        INIT_GLYPH_STRING (s, NULL, w, row, area, START, HL);          \
+        fill_xwidget_glyph_string (s);                                 \
+        append_glyph_string (&HEAD, &TAIL, s);                         \
+        ++START;                                                       \
+         s->x = (X);                                                   \
+       }                                                               \
+     while (0)
+#endif
+
 
 /* Add a glyph string for a sequence of character glyphs to the list
    of strings between HEAD and TAIL.  START is the index of the first
@@ -25053,7 +25089,7 @@ compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
    to allocate glyph strings (because draw_glyphs can be called
    asynchronously).  */
 
-#define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X)     \
+#define BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X)   \
   do                                                                   \
     {                                                                  \
       HEAD = TAIL = NULL;                                              \
@@ -25084,8 +25120,15 @@ compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
            case IMAGE_GLYPH:                                           \
              BUILD_IMAGE_GLYPH_STRING (START, END, HEAD, TAIL,         \
                                        HL, X, LAST_X);                 \
-             break;                                                    \
-                                                                       \
+             break;
+
+#define BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X)  \
+            case XWIDGET_GLYPH:                                         \
+              BUILD_XWIDGET_GLYPH_STRING (START, END, HEAD, TAIL,       \
+                                          HL, X, LAST_X);               \
+              break;
+
+#define BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)   \
            case GLYPHLESS_GLYPH:                                       \
              BUILD_GLYPHLESS_GLYPH_STRING (START, END, HEAD, TAIL,     \
                                            HL, X, LAST_X);             \
@@ -25104,6 +25147,18 @@ compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
     } while (0)
 
 
+#ifdef HAVE_XWIDGETS
+#define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X)     \
+BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X)   \
+BUILD_GLYPH_STRINGS_XW(START, END, HEAD, TAIL, HL, X, LAST_X)  \
+BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
+#else
+#define BUILD_GLYPH_STRINGS(START, END, HEAD, TAIL, HL, X, LAST_X)     \
+BUILD_GLYPH_STRINGS_1(START, END, HEAD, TAIL, HL, X, LAST_X)   \
+BUILD_GLYPH_STRINGS_2(START, END, HEAD, TAIL, HL, X, LAST_X)
+#endif
+
+
 /* Draw glyphs between START and END in AREA of ROW on window W,
    starting at x-position X.  X is relative to AREA in W.  HL is a
    face-override with the following meaning:
@@ -25690,6 +25745,15 @@ produce_image_glyph (struct it *it)
       enum glyph_row_area area = it->area;
 
       glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
+      if (it->glyph_row->reversed_p)
+       {
+         struct glyph *g;
+
+         /* Make room for the new glyph.  */
+         for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
+           g[1] = *g;
+         glyph = it->glyph_row->glyphs[it->area];
+       }
       if (glyph < it->glyph_row->glyphs[area + 1])
        {
          glyph->charpos = CHARPOS (it->position);
@@ -25733,6 +25797,113 @@ produce_image_glyph (struct it *it)
     }
 }
 
+#ifdef HAVE_XWIDGETS
+static void
+produce_xwidget_glyph (struct it *it)
+{
+  struct xwidget* xw;
+  struct face *face;
+  int glyph_ascent, crop;
+  eassert (it->what == IT_XWIDGET);
+
+  face = FACE_FROM_ID (it->f, it->face_id);
+  eassert (face);
+  /* Make sure X resources of the face is loaded.  */
+  prepare_face_for_display (it->f, face);
+
+  xw = it->xwidget;
+  it->ascent = it->phys_ascent = glyph_ascent = xw->height/2;
+  it->descent = xw->height/2;
+  it->phys_descent = it->descent;
+  it->pixel_width = xw->width;
+  /* It's quite possible for images to have an ascent greater than
+     their height, so don't get confused in that case.  */
+  if (it->descent < 0)
+    it->descent = 0;
+
+  it->nglyphs = 1;
+
+  if (face->box != FACE_NO_BOX)
+    {
+      if (face->box_line_width > 0)
+       {
+           it->ascent += face->box_line_width;
+           it->descent += face->box_line_width;
+       }
+
+      if (it->start_of_box_run_p)
+       it->pixel_width += eabs (face->box_line_width);
+      it->pixel_width += eabs (face->box_line_width);
+    }
+
+  take_vertical_position_into_account (it);
+
+  /* Automatically crop wide image glyphs at right edge so we can
+     draw the cursor on same display row.  */
+  if ((crop = it->pixel_width - (it->last_visible_x - it->current_x), crop > 0)
+      && (it->hpos == 0 || it->pixel_width > it->last_visible_x / 4))
+    {
+      it->pixel_width -= crop;
+    }
+
+  if (it->glyph_row)
+    {
+      struct glyph *glyph;
+      enum glyph_row_area area = it->area;
+
+      glyph = it->glyph_row->glyphs[area] + it->glyph_row->used[area];
+      if (it->glyph_row->reversed_p)
+       {
+         struct glyph *g;
+
+         /* Make room for the new glyph.  */
+         for (g = glyph - 1; g >= it->glyph_row->glyphs[it->area]; g--)
+           g[1] = *g;
+         glyph = it->glyph_row->glyphs[it->area];
+       }
+      if (glyph < it->glyph_row->glyphs[area + 1])
+       {
+         glyph->charpos = CHARPOS (it->position);
+         glyph->object = it->object;
+         glyph->pixel_width = it->pixel_width;
+         glyph->ascent = glyph_ascent;
+         glyph->descent = it->descent;
+         glyph->voffset = it->voffset;
+         glyph->type = XWIDGET_GLYPH;
+         glyph->avoid_cursor_p = it->avoid_cursor_p;
+         glyph->multibyte_p = it->multibyte_p;
+         if (it->glyph_row->reversed_p && area == TEXT_AREA)
+           {
+             /* In R2L rows, the left and the right box edges need to be
+                drawn in reverse direction.  */
+             glyph->right_box_line_p = it->start_of_box_run_p;
+             glyph->left_box_line_p = it->end_of_box_run_p;
+           }
+         else
+           {
+             glyph->left_box_line_p = it->start_of_box_run_p;
+             glyph->right_box_line_p = it->end_of_box_run_p;
+           }
+          glyph->overlaps_vertically_p = 0;
+          glyph->padding_p = 0;
+         glyph->glyph_not_available_p = 0;
+         glyph->face_id = it->face_id;
+          glyph->u.xwidget = it->xwidget;
+          //assert_valid_xwidget_id(glyph->u.xwidget_id,"produce_xwidget_glyph");
+         glyph->font_type = FONT_TYPE_UNKNOWN;
+         if (it->bidi_p)
+           {
+             glyph->resolved_level = it->bidi_it.resolved_level;
+             eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
+             glyph->bidi_type = it->bidi_it.type;
+           }
+         ++it->glyph_row->used[area];
+       }
+      else
+       IT_EXPAND_MATRIX_WIDTH (it, area);
+    }
+}
+#endif
 
 /* Append a stretch glyph to IT->glyph_row.  OBJECT is the source
    of the glyph, WIDTH and HEIGHT are the width and height of the
@@ -26034,7 +26205,7 @@ produce_special_glyphs (struct it *it, enum display_element_type what)
   GLYPH glyph;
 
   temp_it = *it;
-  temp_it.object = make_number (0);
+  temp_it.object = Qnil;
   memset (&temp_it.current, 0, sizeof temp_it.current);
 
   if (what == IT_CONTINUATION)
@@ -26097,7 +26268,7 @@ produce_special_glyphs (struct it *it, enum display_element_type what)
            (((temp_it.ascent + temp_it.descent)
              * FONT_BASE (font)) / FONT_HEIGHT (font));
 
-         append_stretch_glyph (&temp_it, make_number (0), stretch_width,
+         append_stretch_glyph (&temp_it, Qnil, stretch_width,
                                temp_it.ascent + temp_it.descent,
                                stretch_ascent);
        }
@@ -26163,7 +26334,7 @@ calc_line_height_property (struct it *it, Lisp_Object val, struct font *font,
       int face_id;
       struct face *face;
 
-      face_id = lookup_named_face (it->f, face_name, 0);
+      face_id = lookup_named_face (it->f, face_name, false);
       if (face_id < 0)
        return make_number (-1);
 
@@ -27073,6 +27244,10 @@ x_produce_glyphs (struct it *it)
     produce_image_glyph (it);
   else if (it->what == IT_STRETCH)
     produce_stretch_glyph (it);
+#ifdef HAVE_XWIDGETS
+  else if (it->what == IT_XWIDGET)
+    produce_xwidget_glyph (it);
+#endif
 
  done:
   /* Accumulate dimensions.  Note: can't assume that it->descent > 0
@@ -27442,6 +27617,12 @@ get_window_cursor_type (struct window *w, struct glyph *glyph, int *width,
   /* Use normal cursor if not blinked off.  */
   if (!w->cursor_off_p)
     {
+
+#ifdef HAVE_XWIDGETS
+      if (glyph != NULL && glyph->type == XWIDGET_GLYPH){
+        return NO_CURSOR;
+      }
+#endif
       if (glyph != NULL && glyph->type == IMAGE_GLYPH)
        {
          if (cursor_type == FILLED_BOX_CURSOR)
@@ -28272,7 +28453,7 @@ rows_from_pos_range (struct window *w,
 
          while (g < e)
            {
-             if (((BUFFERP (g->object) || INTEGERP (g->object))
+             if (((BUFFERP (g->object) || NILP (g->object))
                   && start_charpos <= g->charpos && g->charpos < end_charpos)
                  /* A glyph that comes from DISP_STRING is by
                     definition to be highlighted.  */
@@ -28327,7 +28508,7 @@ rows_from_pos_range (struct window *w,
 
          while (g < e)
            {
-             if (((BUFFERP (g->object) || INTEGERP (g->object))
+             if (((BUFFERP (g->object) || NILP (g->object))
                   && ((start_charpos <= g->charpos && g->charpos < end_charpos)
                       /* If the buffer position of the first glyph in
                          the row is equal to END_CHARPOS, it means
@@ -28409,7 +28590,7 @@ mouse_face_from_buffer_pos (Lisp_Object window,
        {
          struct glyph *beg = prev->glyphs[TEXT_AREA];
          glyph = beg + prev->used[TEXT_AREA];
-         while (--glyph >= beg && INTEGERP (glyph->object));
+         while (--glyph >= beg && NILP (glyph->object));
          if (glyph < beg
              || !(EQ (glyph->object, before_string)
                   || EQ (glyph->object, disp_string)))
@@ -28473,7 +28654,7 @@ mouse_face_from_buffer_pos (Lisp_Object window,
       /* Skip truncation glyphs at the start of the glyph row.  */
       if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
        for (; glyph < end
-              && INTEGERP (glyph->object)
+              && NILP (glyph->object)
               && glyph->charpos < 0;
             ++glyph)
          x += glyph->pixel_width;
@@ -28482,7 +28663,7 @@ mouse_face_from_buffer_pos (Lisp_Object window,
         or DISP_STRING, and the first glyph from buffer whose
         position is between START_CHARPOS and END_CHARPOS.  */
       for (; glyph < end
-            && !INTEGERP (glyph->object)
+            && !NILP (glyph->object)
             && !EQ (glyph->object, disp_string)
             && !(BUFFERP (glyph->object)
                  && (glyph->charpos >= start_charpos
@@ -28524,7 +28705,7 @@ mouse_face_from_buffer_pos (Lisp_Object window,
       /* Skip truncation glyphs at the start of the glyph row.  */
       if (MATRIX_ROW_DISPLAYS_TEXT_P (r1))
        for (; glyph > end
-              && INTEGERP (glyph->object)
+              && NILP (glyph->object)
               && glyph->charpos < 0;
             --glyph)
          ;
@@ -28533,7 +28714,7 @@ mouse_face_from_buffer_pos (Lisp_Object window,
         or DISP_STRING, and the first glyph from buffer whose
         position is between START_CHARPOS and END_CHARPOS.  */
       for (; glyph > end
-            && !INTEGERP (glyph->object)
+            && !NILP (glyph->object)
             && !EQ (glyph->object, disp_string)
             && !(BUFFERP (glyph->object)
                  && (glyph->charpos >= start_charpos
@@ -28590,7 +28771,7 @@ mouse_face_from_buffer_pos (Lisp_Object window,
         row, and also blanks and stretch glyphs inserted by
         extend_face_to_end_of_line.  */
       while (end > glyph
-            && INTEGERP ((end - 1)->object))
+            && NILP ((end - 1)->object))
        --end;
       /* Scan the rest of the glyph row from the end, looking for the
         first glyph that comes from BEFORE_STRING, AFTER_STRING, or
@@ -28598,7 +28779,7 @@ mouse_face_from_buffer_pos (Lisp_Object window,
         and END_CHARPOS */
       for (--end;
             end > glyph
-            && !INTEGERP (end->object)
+            && !NILP (end->object)
             && !EQ (end->object, disp_string)
             && !(BUFFERP (end->object)
                  && (end->charpos >= start_charpos
@@ -28636,7 +28817,7 @@ mouse_face_from_buffer_pos (Lisp_Object window,
       x = r2->x;
       end++;
       while (end < glyph
-            && INTEGERP (end->object))
+            && NILP (end->object))
        {
          x += end->pixel_width;
          ++end;
@@ -28647,7 +28828,7 @@ mouse_face_from_buffer_pos (Lisp_Object window,
         and END_CHARPOS */
       for ( ;
             end < glyph
-            && !INTEGERP (end->object)
+            && !NILP (end->object)
             && !EQ (end->object, disp_string)
             && !(BUFFERP (end->object)
                  && (end->charpos >= start_charpos
@@ -29379,7 +29560,7 @@ note_mode_line_or_margin_highlight (Lisp_Object window, int x, int y,
                                                                charpos,
                                                                0, &ignore,
                                                                glyph->face_id,
-                                                               1);
+                                                               true);
          show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
 
          if (NILP (pointer))
@@ -29579,12 +29760,12 @@ note_mouse_highlight (struct frame *f, int x, int y)
       if (glyph == NULL
          || area != TEXT_AREA
          || !MATRIX_ROW_DISPLAYS_TEXT_P (MATRIX_ROW (w->current_matrix, vpos))
-         /* Glyph's OBJECT is an integer for glyphs inserted by the
+         /* Glyph's OBJECT is nil for glyphs inserted by the
             display engine for its internal purposes, like truncation
             and continuation glyphs and blanks beyond the end of
             line's text on text terminals.  If we are over such a
             glyph, we are not over any text.  */
-         || INTEGERP (glyph->object)
+         || NILP (glyph->object)
          /* R2L rows have a stretch glyph at their front, which
             stands for no text, whereas L2R rows have no glyphs at
             all beyond the end of text.  Treat such stretch glyphs
@@ -29707,7 +29888,7 @@ note_mouse_highlight (struct frame *f, int x, int y)
              hlinfo->mouse_face_window = window;
              hlinfo->mouse_face_face_id
                = face_at_string_position (w, object, pos, 0, &ignore,
-                                          glyph->face_id, 1);
+                                          glyph->face_id, true);
              show_mouse_face (hlinfo, DRAW_MOUSE_FACE);
              cursor = No_Cursor;
            }
@@ -30556,7 +30737,9 @@ syms_of_xdisp (void)
   Vmessage_stack = Qnil;
   staticpro (&Vmessage_stack);
 
+  /* Non-nil means don't actually do any redisplay.  */
   DEFSYM (Qinhibit_redisplay, "inhibit-redisplay");
+
   DEFSYM (Qredisplay_internal, "redisplay_internal (C function)");
 
   message_dolog_marker1 = Fmake_marker ();
@@ -30595,6 +30778,8 @@ syms_of_xdisp (void)
   DEFSYM (Qinhibit_point_motion_hooks, "inhibit-point-motion-hooks");
   DEFSYM (Qeval, "eval");
   DEFSYM (QCdata, ":data");
+
+  /* Names of text properties relevant for redisplay.  */
   DEFSYM (Qdisplay, "display");
   DEFSYM (Qspace_width, "space-width");
   DEFSYM (Qraise, "raise");
@@ -30614,40 +30799,69 @@ syms_of_xdisp (void)
   DEFSYM (QCfile, ":file");
   DEFSYM (Qfontified, "fontified");
   DEFSYM (Qfontification_functions, "fontification-functions");
+
+  /* Name of the face used to highlight trailing whitespace.  */
   DEFSYM (Qtrailing_whitespace, "trailing-whitespace");
+
+  /* Name and number of the face used to highlight escape glyphs.  */
   DEFSYM (Qescape_glyph, "escape-glyph");
+
+  /* Name and number of the face used to highlight non-breaking spaces.  */
   DEFSYM (Qnobreak_space, "nobreak-space");
+
+  /* The symbol 'image' which is the car of the lists used to represent
+     images in Lisp.  Also a tool bar style.  */
   DEFSYM (Qimage, "image");
+
+  /* Tool bar styles.  */
   DEFSYM (Qtext, "text");
   DEFSYM (Qboth, "both");
   DEFSYM (Qboth_horiz, "both-horiz");
   DEFSYM (Qtext_image_horiz, "text-image-horiz");
+
+  /* The image map types.  */
   DEFSYM (QCmap, ":map");
   DEFSYM (QCpointer, ":pointer");
   DEFSYM (Qrect, "rect");
   DEFSYM (Qcircle, "circle");
   DEFSYM (Qpoly, "poly");
+
+  /* The symbol `inhibit-menubar-update' and its DEFVAR_BOOL variable.  */
+  DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
   DEFSYM (Qmessage_truncate_lines, "message-truncate-lines");
+
   DEFSYM (Qgrow_only, "grow-only");
-  DEFSYM (Qinhibit_menubar_update, "inhibit-menubar-update");
   DEFSYM (Qinhibit_eval_during_redisplay, "inhibit-eval-during-redisplay");
   DEFSYM (Qposition, "position");
   DEFSYM (Qbuffer_position, "buffer-position");
   DEFSYM (Qobject, "object");
+
+  /* Cursor shapes.  */
   DEFSYM (Qbar, "bar");
   DEFSYM (Qhbar, "hbar");
   DEFSYM (Qbox, "box");
   DEFSYM (Qhollow, "hollow");
+
+  /* Pointer shapes.  */
   DEFSYM (Qhand, "hand");
   DEFSYM (Qarrow, "arrow");
+  /* also Qtext */
+
   DEFSYM (Qinhibit_free_realized_faces, "inhibit-free-realized-faces");
 
   list_of_error = list1 (list2 (intern_c_string ("error"),
                                intern_c_string ("void-variable")));
   staticpro (&list_of_error);
 
+  /* Values of those variables at last redisplay are stored as
+     properties on 'overlay-arrow-position' symbol.  However, if
+     Voverlay_arrow_position is a marker, last-arrow-position is its
+     numerical position.  */
   DEFSYM (Qlast_arrow_position, "last-arrow-position");
   DEFSYM (Qlast_arrow_string, "last-arrow-string");
+
+  /* Alternative overlay-arrow-string and overlay-arrow-bitmap
+     properties on a symbol in overlay-arrow-variable-list.  */
   DEFSYM (Qoverlay_arrow_string, "overlay-arrow-string");
   DEFSYM (Qoverlay_arrow_bitmap, "overlay-arrow-bitmap");
 
@@ -31147,7 +31361,10 @@ cursor shapes.  */);
   hourglass_shown_p = 0;
 #endif /* HAVE_WINDOW_SYSTEM */
 
+  /* Name of the face used to display glyphless characters.  */
   DEFSYM (Qglyphless_char, "glyphless-char");
+
+  /* Method symbols for Vglyphless_char_display.  */
   DEFSYM (Qhex_code, "hex-code");
   DEFSYM (Qempty_box, "empty-box");
   DEFSYM (Qthin_space, "thin-space");
@@ -31160,6 +31377,7 @@ be redisplayed.  This set can be nil (meaning, only the selected window),
 or t (meaning all windows).  */);
   Vpre_redisplay_function = intern ("ignore");
 
+  /* Symbol for the purpose of Vglyphless_char_display.  */
   DEFSYM (Qglyphless_char_display, "glyphless-char-display");
   Fput (Qglyphless_char_display, Qchar_table_extra_slots, make_number (1));