]> code.delx.au - gnu-emacs/blobdiff - src/xdisp.c
Merge branch 'master' into xwidget
[gnu-emacs] / src / xdisp.c
index 7a00e29a1917c8d3aa20e1cff5983fd27faaedb3..707418218f010de1a0262292e807f58f5961f062 100644 (file)
@@ -351,7 +351,7 @@ Lisp_Object Qtext;
 /* Holds the list (error).  */
 static Lisp_Object list_of_error;
 
-static Lisp_Object Qfontification_functions;
+Lisp_Object Qfontification_functions;
 
 static Lisp_Object Qwrap_prefix;
 static Lisp_Object Qline_prefix;
@@ -909,7 +909,7 @@ static void iterate_out_of_display_property (struct it *);
 static void pop_it (struct it *);
 static void sync_frame_with_window_matrix_rows (struct window *);
 static void redisplay_internal (void);
-static int echo_area_display (int);
+static bool echo_area_display (bool);
 static void redisplay_windows (Lisp_Object);
 static void redisplay_window (Lisp_Object, bool);
 static Lisp_Object redisplay_window_error (Lisp_Object);
@@ -1151,7 +1151,7 @@ window_box_left_offset (struct window *w, enum glyph_row_area area)
    area AREA of window W.  ANY_AREA means return the right edge of the
    whole window, to the left of the right fringe of W.  */
 
-int
+static int
 window_box_right_offset (struct window *w, enum glyph_row_area area)
 {
   /* Don't return more than the window's pixel width.  */
@@ -1462,15 +1462,19 @@ pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
         glyph.  */
       int top_x = it.current_x;
       int top_y = it.current_y;
-      /* Calling line_bottom_y may change it.method, it.position, etc.  */
-      enum it_method it_method = it.method;
-      int bottom_y = (last_height = 0, line_bottom_y (&it));
       int window_top_y = WINDOW_HEADER_LINE_HEIGHT (w);
+      int bottom_y;
+      struct it save_it;
+      void *save_it_data = NULL;
 
+      /* Calling line_bottom_y may change it.method, it.position, etc.  */
+      SAVE_IT (save_it, it, save_it_data);
+      last_height = 0;
+      bottom_y = line_bottom_y (&it);
       if (top_y < window_top_y)
        visible_p = bottom_y > window_top_y;
       else if (top_y < it.last_visible_y)
-       visible_p = true;
+       visible_p = 1;
       if (bottom_y >= it.last_visible_y
          && it.bidi_p && it.bidi_it.scan_dir == -1
          && IT_CHARPOS (it) < charpos)
@@ -1483,7 +1487,6 @@ pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
             move_it_to again with a slightly larger vertical limit,
             and see if it actually moved vertically; if it did, we
             didn't really reach CHARPOS, which is beyond window end.  */
-         struct it save_it = it;
          /* Why 10? because we don't know how many canonical lines
             will the height of the next line(s) be.  So we guess.  */
          int ten_more_lines = 10 * default_line_pixel_height (w);
@@ -1493,11 +1496,11 @@ pos_visible_p (struct window *w, ptrdiff_t charpos, int *x, int *y,
          if (it.current_y > top_y)
            visible_p = 0;
 
-         it = save_it;
        }
+      RESTORE_IT (&it, &save_it, save_it_data);
       if (visible_p)
        {
-         if (it_method == GET_FROM_DISPLAY_VECTOR)
+         if (it.method == GET_FROM_DISPLAY_VECTOR)
            {
              /* We stopped on the last glyph of a display vector.
                 Try and recompute.  Hack alert!  */
@@ -2181,7 +2184,10 @@ get_glyph_string_clip_rects (struct glyph_string *s, NativeRectangle *rects, int
 
       if (s->x > r.x)
        {
-         r.width -= s->x - r.x;
+         if (r.width >= s->x - r.x)
+           r.width -= s->x - r.x;
+         else  /* R2L hscrolled row with cursor outside text area */
+           r.width = 0;
          r.x = s->x;
        }
       r.width = min (r.width, glyph->pixel_width);
@@ -2303,10 +2309,7 @@ get_phys_cursor_geometry (struct window *w, struct glyph_row *row,
      glyph, and `x-stretch-block-cursor' is nil, don't draw a
      rectangle as wide as the glyph, but use a canonical character
      width instead.  */
-  wd = glyph->pixel_width - 1;
-#if defined (HAVE_NTGUI) || defined (HAVE_NS)
-  wd++; /* Why? */
-#endif
+  wd = glyph->pixel_width;
 
   x = w->phys_cursor.x;
   if (x < 0)
@@ -2542,7 +2545,7 @@ remember_mouse_glyph (struct frame *f, int gx, int gy, NativeRectangle *rect)
       gy = 0;
       /* The bottom divider prevails. */
       height = WINDOW_PIXEL_HEIGHT (w) - WINDOW_BOTTOM_DIVIDER_WIDTH (w);
-      goto add_edge;;
+      goto add_edge;
 
     case ON_BOTTOM_DIVIDER:
       gx = 0;
@@ -2630,15 +2633,14 @@ safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
     {
       ptrdiff_t i;
       ptrdiff_t count = SPECPDL_INDEX ();
-      struct gcpro gcpro1;
-      Lisp_Object *args = alloca (nargs * word_size);
+      Lisp_Object *args;
+      USE_SAFE_ALLOCA;
+      SAFE_ALLOCA_LISP (args, nargs);
 
       args[0] = func;
       for (i = 1; i < nargs; i++)
        args[i] = va_arg (ap, Lisp_Object);
 
-      GCPRO1 (args[0]);
-      gcpro1.nvars = nargs;
       specbind (Qinhibit_redisplay, Qt);
       if (inhibit_quit)
        specbind (Qinhibit_quit, Qt);
@@ -2646,7 +2648,7 @@ safe__call (bool inhibit_quit, ptrdiff_t nargs, Lisp_Object func, va_list ap)
         so there is no possibility of wanting to redisplay.  */
       val = internal_condition_case_n (Ffuncall, nargs, args, Qt,
                                       safe_eval_handler);
-      UNGCPRO;
+      SAFE_FREE ();
       val = unbind_to (count, val);
     }
 
@@ -2978,10 +2980,6 @@ init_iterator (struct it *it, struct window *w,
   it->glyph_row = row;
   it->area = TEXT_AREA;
 
-  /* Forget any previous info about this row being reversed.  */
-  if (it->glyph_row)
-    it->glyph_row->reversed_p = 0;
-
   /* Get the dimensions of the display area.  The display area
      consists of the visible window area plus a horizontally scrolled
      part to the left of the window.  All x-values are relative to the
@@ -3001,12 +2999,8 @@ init_iterator (struct it *it, struct window *w,
 
       /* If we truncate lines, leave room for the truncation glyph(s) at
         the right margin.  Otherwise, leave room for the continuation
-        glyph(s).  Done only if the window has no fringes.  Since we
-        don't know at this point whether there will be any R2L lines in
-        the window, we reserve space for truncation/continuation glyphs
-        even if only one of the fringes is absent.  */
-      if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0
-         || (it->bidi_p && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
+        glyph(s).  Done only if the window has no right fringe.  */
+      if (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
        {
          if (it->line_wrap == TRUNCATE)
            it->last_visible_x -= it->truncation_pixel_width;
@@ -3071,6 +3065,19 @@ init_iterator (struct it *it, struct window *w,
         iterator.  */
       if (it->bidi_p)
        {
+         /*  Since we don't know at this point whether there will be
+             any R2L lines in the window, we reserve space for
+             truncation/continuation glyphs even if only the left
+             fringe is absent.  */
+         if (base_face_id == DEFAULT_FACE_ID
+             && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0
+             && WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
+           {
+             if (it->line_wrap == TRUNCATE)
+               it->last_visible_x -= it->truncation_pixel_width;
+             else
+               it->last_visible_x -= it->continuation_pixel_width;
+           }
          /* Note the paragraph direction that this buffer wants to
             use.  */
          if (EQ (BVAR (current_buffer, bidi_paragraph_direction),
@@ -3424,6 +3431,48 @@ handle_stop (struct it *it)
   if (it->selective_display_ellipsis_p)
     it->saved_face_id = it->face_id;
 
+  /* Here's the description of the semantics of, and the logic behind,
+     the various HANDLED_* statuses:
+
+     HANDLED_NORMALLY means the handler did its job, and the loop
+     should proceed to calling the next handler in order.
+
+     HANDLED_RECOMPUTE_PROPS means the handler caused a significant
+     change in the properties and overlays at current position, so the
+     loop should be restarted, to re-invoke the handlers that were
+     already called.  This happens when fontification-functions were
+     called by handle_fontified_prop, and actually fontified
+     something.  Another case where HANDLED_RECOMPUTE_PROPS is
+     returned is when we discover overlay strings that need to be
+     displayed right away.  The loop below will continue for as long
+     as the status is HANDLED_RECOMPUTE_PROPS.
+
+     HANDLED_RETURN means return immediately to the caller, to
+     continue iteration without calling any further handlers.  This is
+     used when we need to act on some property right away, for example
+     when we need to display the ellipsis or a replacing display
+     property, such as display string or image.
+
+     HANDLED_OVERLAY_STRING_CONSUMED means an overlay string was just
+     consumed, and the handler switched to the next overlay string.
+     This signals the loop below to refrain from looking for more
+     overlays before all the overlay strings of the current overlay
+     are processed.
+
+     Some of the handlers called by the loop push the iterator state
+     onto the stack (see 'push_it'), and arrange for the iteration to
+     continue with another object, such as an image, a display string,
+     or an overlay string.  In most such cases, it->stop_charpos is
+     set to the first character of the string, so that when the
+     iteration resumes, this function will immediately be called
+     again, to examine the properties at the beginning of the string.
+
+     When a display or overlay string is exhausted, the iterator state
+     is popped (see 'pop_it'), and iteration continues with the
+     previous object.  Again, in many such cases this function is
+     called again to find the next position where properties might
+     change.  */
+
   do
     {
       handled = HANDLED_NORMALLY;
@@ -3625,6 +3674,7 @@ next_overlay_change (ptrdiff_t pos)
   ptrdiff_t i, noverlays;
   ptrdiff_t endpos;
   Lisp_Object *overlays;
+  USE_SAFE_ALLOCA;
 
   /* Get all overlays at the given position.  */
   GET_OVERLAYS_AT (pos, overlays, noverlays, &endpos, 1);
@@ -3641,6 +3691,7 @@ next_overlay_change (ptrdiff_t pos)
       endpos = min (endpos, oendpos);
     }
 
+  SAFE_FREE ();
   return endpos;
 }
 
@@ -5065,6 +5116,11 @@ handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
                  iterate_out_of_display_property (it);
                  *position = it->position;
                }
+             /* If we were to display this fringe bitmap,
+                next_element_from_image would have reset this flag.
+                Do the same, to avoid affecting overlays that
+                follow.  */
+             it->ignore_overlay_strings_at_pos_p = 0;
              return 1;
            }
        }
@@ -5084,12 +5140,15 @@ handle_single_display_spec (struct it *it, Lisp_Object spec, Lisp_Object object,
              iterate_out_of_display_property (it);
              *position = it->position;
            }
+         if (it)
+           /* Reset this flag like next_element_from_image would.  */
+           it->ignore_overlay_strings_at_pos_p = 0;
          return 1;
        }
 
       if (it)
        {
-         int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);;
+         int face_id = lookup_basic_face (it->f, DEFAULT_FACE_ID);
 
          if (CONSP (XCDR (XCDR (spec))))
            {
@@ -5721,10 +5780,11 @@ load_overlay_strings (struct it *it, ptrdiff_t charpos)
   Lisp_Object overlay, window, str, invisible;
   struct Lisp_Overlay *ov;
   ptrdiff_t start, end;
-  ptrdiff_t size = 20;
   ptrdiff_t n = 0, i, j;
   int invis_p;
-  struct overlay_entry *entries = alloca (size * sizeof *entries);
+  struct overlay_entry entriesbuf[20];
+  ptrdiff_t size = ARRAYELTS (entriesbuf);
+  struct overlay_entry *entries = entriesbuf;
   USE_SAFE_ALLOCA;
 
   if (charpos <= 0)
@@ -6920,7 +6980,8 @@ get_next_display_element (struct it *it)
         is R..."  */
       /* FIXME: Do we need an exception for characters from display
         tables?  */
-      if (it->bidi_p && it->bidi_it.type == STRONG_R)
+      if (it->bidi_p && it->bidi_it.type == STRONG_R
+         && !inhibit_bidi_mirroring)
        it->c = bidi_mirror_char (it->c);
       /* Map via display table or translate control characters.
         IT->c, IT->len etc. have been set to the next character by
@@ -8332,6 +8393,10 @@ next_element_from_buffer (struct it *it)
       unsigned char *p;
       ptrdiff_t stop;
 
+      /* We moved to the next buffer position, so any info about
+        previously seen overlays is no longer valid.  */
+      it->ignore_overlay_strings_at_pos_p = 0;
+
       /* Maybe run the redisplay end trigger hook.  Performance note:
         This doesn't seem to cost measurable time.  */
       if (it->redisplay_end_trigger_charpos
@@ -8785,12 +8850,7 @@ move_it_in_display_line_to (struct it *it,
                         doesn't fit on the line, e.g. a wide image.  */
                      it->hpos == 0
                      || (new_x == it->last_visible_x
-                         && FRAME_WINDOW_P (it->f)
-                         /* When word-wrap is ON and we have a valid
-                            wrap point, we don't allow the last glyph
-                            to "just barely fit" on the line.  */
-                         && (it->line_wrap != WORD_WRAP
-                             || wrap_it.sp < 0)))
+                         && FRAME_WINDOW_P (it->f)))
                    {
                      ++it->hpos;
                      it->current_x = new_x;
@@ -8857,7 +8917,8 @@ move_it_in_display_line_to (struct it *it,
                                }
                              if (ITERATOR_AT_END_OF_LINE_P (it)
                                  && (it->line_wrap != WORD_WRAP
-                                     || wrap_it.sp < 0))
+                                     || wrap_it.sp < 0
+                                     || IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)))
                                {
                                  result = MOVE_NEWLINE_OR_CR;
                                  break;
@@ -9753,6 +9814,7 @@ move_it_by_lines (struct it *it, ptrdiff_t dvpos)
              back_to_previous_visible_line_start (it);
              it->vpos--;
            }
+         reseat_1 (it, it->current.pos, 1);
        }
       else
        RESTORE_IT (it, it, it2data);
@@ -10206,9 +10268,9 @@ message3 (Lisp_Object m)
     {
       ptrdiff_t nbytes = SBYTES (m);
       bool multibyte = STRING_MULTIBYTE (m);
+      char *buffer;
       USE_SAFE_ALLOCA;
-      char *buffer = SAFE_ALLOCA (nbytes);
-      memcpy (buffer, SDATA (m), nbytes);
+      SAFE_ALLOCA_STRING (buffer, m);
       message_dolog (buffer, nbytes, 1, multibyte);
       SAFE_FREE ();
     }
@@ -10269,9 +10331,9 @@ message3_nolog (Lisp_Object m)
       else
        clear_message (true, true);
 
-      do_pending_window_change (0);
-      echo_area_display (1);
-      do_pending_window_change (0);
+      do_pending_window_change (false);
+      echo_area_display (true);
+      do_pending_window_change (false);
       if (FRAME_TERMINAL (f)->frame_up_to_date_hook)
        (*FRAME_TERMINAL (f)->frame_up_to_date_hook) (f);
     }
@@ -10410,11 +10472,13 @@ vmessage (const char *m, va_list ap)
            {
              ptrdiff_t len;
              ptrdiff_t maxsize = FRAME_MESSAGE_BUF_SIZE (f);
-             char *message_buf = alloca (maxsize + 1);
+             USE_SAFE_ALLOCA;
+             char *message_buf = SAFE_ALLOCA (maxsize + 1);
 
              len = doprnt (message_buf, maxsize, m, 0, ap);
 
              message3 (make_string (message_buf, len));
+             SAFE_FREE ();
            }
          else
            message1 (0);
@@ -11240,13 +11304,13 @@ clear_garbaged_frames (void)
    is non-zero update selected_frame.  Value is non-zero if the
    mini-windows height has been changed.  */
 
-static int
-echo_area_display (int update_frame_p)
+static bool
+echo_area_display (bool update_frame_p)
 {
   Lisp_Object mini_window;
   struct window *w;
   struct frame *f;
-  int window_height_changed_p = 0;
+  bool window_height_changed_p = false;
   struct frame *sf = SELECTED_FRAME ();
 
   mini_window = FRAME_MINIBUF_WINDOW (sf);
@@ -11309,11 +11373,11 @@ echo_area_display (int update_frame_p)
              /* Window configuration is the same as before.
                 Can do with a display update of the echo area,
                 unless we displayed some mode lines.  */
-             update_single_window (w, 1);
+             update_single_window (w);
              flush_frame (f);
            }
          else
-           update_frame (f, 1, 1);
+           update_frame (f, true, true);
 
          /* If cursor is in the echo area, make sure that the next
             redisplay displays the minibuffer, so that the cursor will
@@ -11784,12 +11848,6 @@ prepare_menu_bars (void)
          menu_bar_hooks_run = update_menu_bar (f, 0, menu_bar_hooks_run);
 #ifdef HAVE_WINDOW_SYSTEM
          update_tool_bar (f, 0);
-#endif
-#ifdef HAVE_NS
-          if (windows_or_buffers_changed
-             && FRAME_NS_P (f))
-            ns_set_doc_edited
-             (f, Fbuffer_modified_p (XWINDOW (f->selected_window)->contents));
 #endif
          UNGCPRO;
        }
@@ -12046,11 +12104,11 @@ static void
 build_desired_tool_bar_string (struct frame *f)
 {
   int i, size, size_needed;
-  struct gcpro gcpro1, gcpro2, gcpro3;
-  Lisp_Object image, plist, props;
+  struct gcpro gcpro1, gcpro2;
+  Lisp_Object image, plist;
 
-  image = plist = props = Qnil;
-  GCPRO3 (image, plist, props);
+  image = plist = Qnil;
+  GCPRO2 (image, plist);
 
   /* Prepare F->desired_tool_bar_string.  If we can reuse it, do so.
      Otherwise, make a new string.  */
@@ -12069,9 +12127,12 @@ build_desired_tool_bar_string (struct frame *f)
       (f, Fmake_string (make_number (size_needed), make_number (' ')));
   else
     {
-      props = list4 (Qdisplay, Qnil, Qmenu_item, Qnil);
+      AUTO_LIST4 (props, Qdisplay, Qnil, Qmenu_item, Qnil);
+      struct gcpro gcpro1;
+      GCPRO1 (props);
       Fremove_text_properties (make_number (0), make_number (size),
                               props, f->desired_tool_bar_string);
+      UNGCPRO;
     }
 
   /* Put a `display' property on the string for the images to display,
@@ -12182,8 +12243,10 @@ build_desired_tool_bar_string (struct frame *f)
         the start of this item's properties in the tool-bar items
         vector.  */
       image = Fcons (Qimage, plist);
-      props = list4 (Qdisplay, image,
-                    Qmenu_item, make_number (i * TOOL_BAR_ITEM_NSLOTS));
+      AUTO_LIST4 (props, Qdisplay, image, Qmenu_item,
+                 make_number (i * TOOL_BAR_ITEM_NSLOTS));
+      struct gcpro gcpro1;
+      GCPRO1 (props);
 
       /* Let the last image hide all remaining spaces in the tool bar
          string.  The string can be longer than needed when we reuse a
@@ -12194,6 +12257,7 @@ build_desired_tool_bar_string (struct frame *f)
        end = i + 1;
       Fadd_text_properties (make_number (i), make_number (end),
                            props, f->desired_tool_bar_string);
+      UNGCPRO;
 #undef PROP
     }
 
@@ -12350,6 +12414,7 @@ tool_bar_height (struct frame *f, int *n_rows, bool pixelwise)
   /* Initialize an iterator for iteration over
      F->desired_tool_bar_string in the tool-bar window of frame F.  */
   init_iterator (&it, w, -1, -1, temp_row, TOOL_BAR_FACE_ID);
+  temp_row->reversed_p = false;
   it.first_visible_x = 0;
   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
   reseat_to_string (&it, NULL, f->desired_tool_bar_string, 0, 0, 0, -1);
@@ -12434,6 +12499,7 @@ redisplay_tool_bar (struct frame *f)
   it.first_visible_x = 0;
   it.last_visible_x = WINDOW_PIXEL_WIDTH (w);
   row = it.glyph_row;
+  row->reversed_p = false;
 
   /* Build a string that represents the contents of the tool-bar.  */
   build_desired_tool_bar_string (f);
@@ -13425,7 +13491,7 @@ redisplay_internal (void)
   struct window *w = XWINDOW (selected_window);
   struct window *sw;
   struct frame *fr;
-  int pending;
+  bool pending;
   bool must_finish = 0, match_p;
   struct text_pos tlbufpos, tlendpos;
   int number_of_visible_frames;
@@ -13485,7 +13551,7 @@ redisplay_internal (void)
   /* Remember the currently selected window.  */
   sw = w;
 
-  pending = 0;
+  pending = false;
   last_escape_glyph_frame = NULL;
   last_escape_glyph_face_id = (1 << FACE_ID_BITS);
   last_glyphless_glyph_frame = NULL;
@@ -13537,7 +13603,7 @@ redisplay_internal (void)
     }
 
   /* Notice any pending interrupt request to change frame size.  */
-  do_pending_window_change (1);
+  do_pending_window_change (true);
 
   /* do_pending_window_change could change the selected_window due to
      frame resizing which makes the selected window too small.  */
@@ -13563,6 +13629,12 @@ redisplay_internal (void)
 
       if (mode_line_update_needed (w))
        w->update_mode_line = 1;
+
+      /* If reconsider_clip_changes above decided that the narrowing
+        in the current buffer changed, make sure all other windows
+        showing that buffer will be redisplayed.  */
+      if (current_buffer->clip_changed)
+       bset_update_mode_line (current_buffer);
     }
 
   /* Normally the message* functions will have already displayed and
@@ -13578,7 +13650,7 @@ redisplay_internal (void)
             echo-area doesn't show through.  */
          && !MINI_WINDOW_P (XWINDOW (selected_window))))
     {
-      int window_height_changed_p = echo_area_display (0);
+      int window_height_changed_p = echo_area_display (false);
 
       if (message_cleared_p)
        update_miniwindow_p = true;
@@ -13793,7 +13865,7 @@ redisplay_internal (void)
        {
          if (!must_finish)
            {
-             do_pending_window_change (1);
+             do_pending_window_change (true);
              /* If selected_window changed, redisplay again.  */
              if (WINDOWP (selected_window)
                  && (w = XWINDOW (selected_window)) != sw)
@@ -13910,14 +13982,14 @@ redisplay_internal (void)
                  if (f->fonts_changed)
                    {
                      adjust_frame_glyphs (f);
-                     f->fonts_changed = 0;
+                     f->fonts_changed = false;
                      goto retry_frame;
                    }
 
                  /* See if we have to hscroll.  */
                  if (!f->already_hscrolled_p)
                    {
-                     f->already_hscrolled_p = 1;
+                     f->already_hscrolled_p = true;
                      if (hscroll_windows (f->root_window))
                        goto retry_frame;
                    }
@@ -13929,9 +14001,9 @@ redisplay_internal (void)
                    unrequest_sigio ();
                  STOP_POLLING;
 
-                 pending |= update_frame (f, 0, 0);
-                 f->cursor_type_changed = 0;
-                 f->updated_p = 1;
+                 pending |= update_frame (f, false, false);
+                 f->cursor_type_changed = false;
+                 f->updated_p = true;
                }
            }
        }
@@ -13992,8 +14064,8 @@ redisplay_internal (void)
            goto retry;
 
          XWINDOW (selected_window)->must_be_updated_p = true;
-         pending = update_frame (sf, 0, 0);
-         sf->cursor_type_changed = 0;
+         pending = update_frame (sf, false, false);
+         sf->cursor_type_changed = false;
        }
 
       /* We may have called echo_area_display at the top of this
@@ -14007,8 +14079,8 @@ redisplay_internal (void)
       if (mini_frame != sf && FRAME_WINDOW_P (mini_frame))
        {
          XWINDOW (mini_window)->must_be_updated_p = true;
-         pending |= update_frame (mini_frame, 0, 0);
-         mini_frame->cursor_type_changed = 0;
+         pending |= update_frame (mini_frame, false, false);
+         mini_frame->cursor_type_changed = false;
          if (!pending && hscroll_windows (mini_window))
            goto retry;
        }
@@ -14085,7 +14157,7 @@ redisplay_internal (void)
     }
 
   /* Change frame size now if a change is pending.  */
-  do_pending_window_change (1);
+  do_pending_window_change (true);
 
   /* If we just did a pending size change, or have additional
      visible frames, or selected_window changed, redisplay again.  */
@@ -14101,7 +14173,7 @@ redisplay_internal (void)
 
   if (clear_face_cache_count > CLEAR_FACE_CACHE_COUNT)
     {
-      clear_face_cache (0);
+      clear_face_cache (false);
       clear_face_cache_count = 0;
     }
 
@@ -14114,6 +14186,9 @@ redisplay_internal (void)
 #endif /* HAVE_WINDOW_SYSTEM */
 
  end_of_redisplay:
+#ifdef HAVE_NS
+  ns_set_doc_edited ();
+#endif
   if (interrupt_input && interrupts_deferred)
     request_sigio ();
 
@@ -14142,9 +14217,9 @@ redisplay_preserve_echo_area (int from_where)
     {
       /* We have a previously displayed message, but no current
         message.  Redisplay the previous message.  */
-      display_last_displayed_message_p = 1;
+      display_last_displayed_message_p = true;
       redisplay_internal ();
-      display_last_displayed_message_p = 0;
+      display_last_displayed_message_p = false;
     }
   else
     redisplay_internal ();
@@ -14993,6 +15068,10 @@ run_window_scroll_functions (Lisp_Object window, struct text_pos startp)
    If FORCE_P is non-zero, return 0 even if partial visible cursor row
    is higher than window.
 
+   If CURRENT_MATRIX_P is non-zero, use the information from the
+   window's current glyph matrix; otherwise use the desired glyph
+   matrix.
+
    A value of 0 means the caller should do scrolling
    as if point had gone off the screen.  */
 
@@ -15188,8 +15267,8 @@ try_scrolling (Lisp_Object window, int just_this_one_p,
                 non-zero margins, because scroll_up_aggressively
                 means put point that fraction of window height
                 _from_the_bottom_margin_.  */
-             if (aggressive_scroll + 2*this_scroll_margin > height)
-               aggressive_scroll = height - 2*this_scroll_margin;
+             if (aggressive_scroll + 2 * this_scroll_margin > height)
+               aggressive_scroll = height - 2 * this_scroll_margin;
              amount_to_scroll = dy + aggressive_scroll;
            }
        }
@@ -15282,8 +15361,8 @@ try_scrolling (Lisp_Object window, int just_this_one_p,
          start_display (&it, w, startp);
 
          if (arg_scroll_conservatively)
-           amount_to_scroll = max (dy, frame_line_height *
-                                   max (scroll_step, temp_scroll_step));
+           amount_to_scroll = max (dy, frame_line_height
+                                   max (scroll_step, temp_scroll_step));
          else if (scroll_step || temp_scroll_step)
            amount_to_scroll = scroll_max;
          else
@@ -15300,8 +15379,8 @@ try_scrolling (Lisp_Object window, int just_this_one_p,
                     bottom of the window, if the value of
                     scroll_down_aggressively happens to be too
                     large.  */
-                 if (aggressive_scroll + 2*this_scroll_margin > height)
-                   aggressive_scroll = height - 2*this_scroll_margin;
+                 if (aggressive_scroll + 2 * this_scroll_margin > height)
+                   aggressive_scroll = height - 2 * this_scroll_margin;
                  amount_to_scroll = dy + aggressive_scroll;
                }
            }
@@ -15842,7 +15921,7 @@ set_vertical_scroll_bar (struct window *w)
 void
 set_horizontal_scroll_bar (struct window *w)
 {
-  int start, end, whole, box_width;
+  int start, end, whole, portion;
 
   if (!MINI_WINDOW_P (w)
       || (w == XWINDOW (minibuf_window)
@@ -15869,27 +15948,38 @@ set_horizontal_scroll_bar (struct window *w)
                          MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y); */
 
       start = w->hscroll * FRAME_COLUMN_WIDTH (WINDOW_XFRAME (w));
-      box_width = window_box_width (w, TEXT_AREA);
-      end = start + box_width;
+      end = start + window_box_width (w, TEXT_AREA);
+      portion = end - start;
+      /* After enlarging a horizontally scrolled window such that it
+        gets at least as wide as the text it contains, make sure that
+        the thumb doesn't fill the entire scroll bar so we can still
+        drag it back to see the entire text.  */
+      whole = max (whole, end);
 
-      /* The following is needed to ensure that if after maximizing a
-        window we get hscroll > 0, we can still drag the thumb to the
-        left.  */
-      whole = max (whole, w->hscroll + box_width);
-      whole = max (whole, end - start);
+      if (it.bidi_p)
+       {
+         Lisp_Object pdir;
+
+         pdir = Fcurrent_bidi_paragraph_direction (Qnil);
+         if (EQ (pdir, Qright_to_left))
+           {
+             start = whole - end;
+             end = start + portion;
+           }
+       }
 
       if (old_buffer)
        set_buffer_internal (old_buffer);
     }
   else
-    start = end = whole = 0;
+    start = end = whole = portion = 0;
 
   w->hscroll_whole = whole;
 
   /* Indicate what this scroll bar ought to be displaying now.  */
   if (FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
     (*FRAME_TERMINAL (XFRAME (w->frame))->set_horizontal_scroll_bar_hook)
-      (w, end - start, whole, start);
+      (w, portion, whole, start);
 }
 
 
@@ -16155,26 +16245,48 @@ redisplay_window (Lisp_Object window, bool just_this_one_p)
 
   /* If someone specified a new starting point but did not insist,
      check whether it can be used.  */
-  if (w->optional_new_start
+  if ((w->optional_new_start || window_frozen_p (w))
       && CHARPOS (startp) >= BEGV
       && CHARPOS (startp) <= ZV)
     {
+      ptrdiff_t it_charpos;
+
       w->optional_new_start = 0;
       start_display (&it, w, startp);
       move_it_to (&it, PT, 0, it.last_visible_y, -1,
                  MOVE_TO_POS | MOVE_TO_X | MOVE_TO_Y);
-      if (IT_CHARPOS (it) == PT)
-       w->force_start = 1;
-      /* IT may overshoot PT if text at PT is invisible.  */
-      else if (IT_CHARPOS (it) > PT && CHARPOS (startp) <= PT)
-       w->force_start = 1;
+      /* Record IT's position now, since line_bottom_y might change
+        that.  */
+      it_charpos = IT_CHARPOS (it);
+      /* Make sure we set the force_start flag only if the cursor row
+        will be fully visible.  Otherwise, the code under force_start
+        label below will try to move point back into view, which is
+        not what the code which sets optional_new_start wants.  */
+      if ((it.current_y == 0 || line_bottom_y (&it) < it.last_visible_y)
+         && !w->force_start)
+       {
+         if (it_charpos == PT)
+           w->force_start = 1;
+         /* IT may overshoot PT if text at PT is invisible.  */
+         else if (it_charpos > PT && CHARPOS (startp) <= PT)
+           w->force_start = 1;
+#ifdef GLYPH_DEBUG
+         if (w->force_start)
+           {
+             if (window_frozen_p (w))
+               debug_method_add (w, "set force_start from frozen window start");
+             else
+               debug_method_add (w, "set force_start from optional_new_start");
+           }
+#endif
+       }
     }
 
  force_start:
 
   /* Handle case where place to start displaying has been specified,
      unless the specified location is outside the accessible range.  */
-  if (w->force_start || window_frozen_p (w))
+  if (w->force_start)
     {
       /* We set this later on if we have to adjust point.  */
       int new_vpos = -1;
@@ -16219,7 +16331,7 @@ redisplay_window (Lisp_Object window, bool just_this_one_p)
          goto need_larger_matrices;
        }
 
-      if (w->cursor.vpos < 0 && !window_frozen_p (w))
+      if (w->cursor.vpos < 0)
        {
          /* If point does not appear, try to move point so it does
             appear. The desired matrix has been built above, so we
@@ -16301,16 +16413,29 @@ redisplay_window (Lisp_Object window, bool just_this_one_p)
 
          set_cursor_from_row (w, row, w->desired_matrix, 0, 0, 0, 0);
 
-         /* If we are highlighting the region, then we just changed
-            the region, so redisplay to show it.  */
-         /* FIXME: We need to (re)run pre-redisplay-function!  */
-         /* if (markpos_of_region () >= 0)
+         /* Re-run pre-redisplay-function so it can update the region
+            according to the new position of point.  */
+         /* Other than the cursor, w's redisplay is done so we can set its
+            redisplay to false.  Also the buffer's redisplay can be set to
+            false, since propagate_buffer_redisplay should have already
+            propagated its info to `w' anyway.  */
+         w->redisplay = false;
+         XBUFFER (w->contents)->text->redisplay = false;
+         safe__call1 (true, Vpre_redisplay_function, Fcons (window, Qnil));
+
+         if (w->redisplay || XBUFFER (w->contents)->text->redisplay)
            {
+             /* pre-redisplay-function made changes (e.g. move the region)
+                that require another round of redisplay.  */
              clear_glyph_matrix (w->desired_matrix);
              if (!try_window (window, startp, 0))
                goto need_larger_matrices;
            }
-         */
+       }
+      if (w->cursor.vpos < 0 || !cursor_row_fully_visible_p (w, 0, 0))
+       {
+         clear_glyph_matrix (w->desired_matrix);
+         goto try_to_scroll;
        }
 
 #ifdef GLYPH_DEBUG
@@ -16376,7 +16501,8 @@ redisplay_window (Lisp_Object window, bool just_this_one_p)
               || CHARPOS (startp) == BEGV
               || !window_outdated (w)))
     {
-      int d1, d2, d3, d4, d5, d6;
+      int d1, d2, d5, d6;
+      int rtop, rbot;
 
       /* If first window line is a continuation line, and window start
         is inside the modified region, but the first change is before
@@ -16401,14 +16527,20 @@ redisplay_window (Lisp_Object window, bool just_this_one_p)
          && compute_window_start_on_continuation_line (w)
          /* It doesn't make sense to force the window start like we
             do at label force_start if it is already known that point
-            will not be visible in the resulting window, because
+            will not be fully visible in the resulting window, because
             doing so will move point from its correct position
             instead of scrolling the window to bring point into view.
             See bug#9324.  */
-         && pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &d6))
+         && pos_visible_p (w, PT, &d1, &d2, &rtop, &rbot, &d5, &d6)
+         /* A very tall row could need more than the window height,
+            in which case we accept that it is partially visible.  */
+         && (rtop != 0) == (rbot != 0))
        {
          w->force_start = 1;
          SET_TEXT_POS_FROM_MARKER (startp, w->start);
+#ifdef GLYPH_DEBUG
+         debug_method_add (w, "recomputed window start in continuation line");
+#endif
          goto force_start;
        }
 
@@ -16519,8 +16651,8 @@ redisplay_window (Lisp_Object window, bool just_this_one_p)
     {
       int window_total_lines
        = WINDOW_TOTAL_LINES (w) * FRAME_LINE_HEIGHT (f) / frame_line_height;
-      int margin =
-       scroll_margin > 0
+      int margin
+       scroll_margin > 0
        ? min (scroll_margin, window_total_lines / 4)
        : 0;
       ptrdiff_t margin_pos = CHARPOS (startp);
@@ -16950,6 +17082,7 @@ try_window (Lisp_Object window, struct text_pos pos, int flags)
 
   /* Initialize iterator and info to start at POS.  */
   start_display (&it, w, pos);
+  it.glyph_row->reversed_p = false;
 
 
   
@@ -17147,6 +17280,7 @@ try_window_reusing_current_matrix (struct window *w)
              && it.current.dpvec_index < 0)
            break;
 
+         it.glyph_row->reversed_p = false;
          if (display_line (&it))
            last_text_row = it.glyph_row - 1;
 
@@ -18174,6 +18308,11 @@ try_window_id (struct window *w)
   w->cursor.vpos = -1;
   last_text_row = NULL;
   overlay_arrow_seen = 0;
+  if (it.current_y < it.last_visible_y
+      && !f->fonts_changed
+      && (first_unchanged_at_end_row == NULL
+         || IT_CHARPOS (it) < stop_pos))
+    it.glyph_row->reversed_p = false;
   while (it.current_y < it.last_visible_y
         && !f->fonts_changed
         && (first_unchanged_at_end_row == NULL
@@ -18741,10 +18880,10 @@ dump_glyph_row (struct glyph_row *row, int vpos, int glyphs)
   else if (glyphs == 1)
     {
       int area;
+      char s[SHRT_MAX + 4];
 
       for (area = LEFT_MARGIN_AREA; area < LAST_AREA; ++area)
        {
-         char *s = alloca (row->used[area] + 4);
          int i;
 
          for (i = 0; i < row->used[area]; ++i)
@@ -18779,7 +18918,10 @@ DEFUN ("dump-glyph-matrix", Fdump_glyph_matrix,
        doc: /* Dump the current matrix of the selected window to stderr.
 Shows contents of glyph row structures.  With non-nil
 parameter GLYPHS, dump glyphs as well.  If GLYPHS is 1 show
-glyphs in short form, otherwise show glyphs in long form.  */)
+glyphs in short form, otherwise show glyphs in long form.
+
+Interactively, no argument means show glyphs in short form;
+with numeric argument, its value is passed as the GLYPHS flag.  */)
   (Lisp_Object glyphs)
 {
   struct window *w = XWINDOW (selected_window);
@@ -18797,11 +18939,16 @@ glyphs in short form, otherwise show glyphs in long form.  */)
 
 
 DEFUN ("dump-frame-glyph-matrix", Fdump_frame_glyph_matrix,
-       Sdump_frame_glyph_matrix, 0, 0, "", doc: /* */)
+       Sdump_frame_glyph_matrix, 0, 0, "", doc: /* Dump the current glyph matrix of the selected frame to stderr.
+Only text-mode frames have frame glyph matrices.  */)
   (void)
 {
   struct frame *f = XFRAME (selected_frame);
-  dump_glyph_matrix (f->current_matrix, 1);
+
+  if (f->current_matrix)
+    dump_glyph_matrix (f->current_matrix, 1);
+  else
+    fprintf (stderr, "*** This frame doesn't have a frame glyph matrix ***\n");
   return Qnil;
 }
 
@@ -18897,7 +19044,7 @@ get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
   struct buffer *buffer = XBUFFER (w->contents);
   struct buffer *old = current_buffer;
   const unsigned char *arrow_string = SDATA (overlay_arrow_string);
-  int arrow_len = SCHARS (overlay_arrow_string);
+  ptrdiff_t arrow_len = SCHARS (overlay_arrow_string);
   const unsigned char *arrow_end = arrow_string + arrow_len;
   const unsigned char *p;
   struct it it;
@@ -18906,6 +19053,7 @@ get_overlay_arrow_glyph_row (struct window *w, Lisp_Object overlay_arrow_string)
 
   set_buffer_temp (buffer);
   init_iterator (&it, w, -1, -1, &scratch_glyph_row, DEFAULT_FACE_ID);
+  scratch_glyph_row.reversed_p = false;
   it.glyph_row->used[TEXT_AREA] = 0;
   SET_TEXT_POS (it.position, 0, 0);
 
@@ -19417,7 +19565,18 @@ extend_face_to_end_of_line (struct it *it)
 
          for (row_width = 0, g = row_start; g < row_end; g++)
            row_width += g->pixel_width;
-         stretch_width = window_box_width (it->w, TEXT_AREA) - row_width;
+
+         /* FIXME: There are various minor display glitches in R2L
+            rows when only one of the fringes is missing.  The
+            strange condition below produces the least bad effect.  */
+         if ((WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0)
+             == (WINDOW_RIGHT_FRINGE_WIDTH (it->w) == 0)
+             || WINDOW_RIGHT_FRINGE_WIDTH (it->w) != 0)
+           stretch_width = window_box_width (it->w, TEXT_AREA);
+         else
+           stretch_width = it->last_visible_x - it->first_visible_x;
+         stretch_width -= row_width;
+
          if (stretch_width > 0)
            {
              stretch_ascent =
@@ -19444,6 +19603,12 @@ extend_face_to_end_of_line (struct it *it)
              it->face_id = saved_face_id;
              it->start_of_box_run_p = saved_box_start;
            }
+         /* If stretch_width comes out negative, it means that the
+            last glyph is only partially visible.  In R2L rows, we
+            want the leftmost glyph to be partially visible, so we
+            need to give the row the corresponding left offset.  */
+         if (stretch_width < 0)
+           it->glyph_row->x = stretch_width;
        }
 #endif /* HAVE_WINDOW_SYSTEM */
     }
@@ -20070,6 +20235,7 @@ display_line (struct it *it)
   int cvpos;
   ptrdiff_t min_pos = ZV + 1, max_pos = 0;
   ptrdiff_t min_bpos IF_LINT (= 0), max_bpos IF_LINT (= 0);
+  bool pending_handle_line_prefix = false;
 
   /* We always start displaying at hpos zero even if hscrolled.  */
   eassert (it->hpos == 0 && it->current_x == 0);
@@ -20130,13 +20296,23 @@ display_line (struct it *it)
       min_pos = CHARPOS (this_line_min_pos);
       min_bpos = BYTEPOS (this_line_min_pos);
     }
-  else
+  else if (it->area == TEXT_AREA)
     {
-      /* We only do this when not calling `move_it_in_display_line_to'
-        above, because move_it_in_display_line_to calls
-        handle_line_prefix itself.  */
+      /* We only do this when not calling move_it_in_display_line_to
+        above, because that function calls itself handle_line_prefix.  */
       handle_line_prefix (it);
     }
+  else
+    {
+      /* Line-prefix and wrap-prefix are always displayed in the text
+        area.  But if this is the first call to display_line after
+        init_iterator, the iterator might have been set up to write
+        into a marginal area, e.g. if the line begins with some
+        display property that writes to the margins.  So we need to
+        wait with the call to handle_line_prefix until whatever
+        writes to the margin has done its job.  */
+      pending_handle_line_prefix = true;
+    }
 
   /* Get the initial row height.  This is either the height of the
      text hscrolled, if there is any, or zero.  */
@@ -20268,6 +20444,14 @@ display_line (struct it *it)
          row->extra_line_spacing = max (row->extra_line_spacing,
                                         it->max_extra_line_spacing);
          set_iterator_to_next (it, 1);
+         /* If we didn't handle the line/wrap prefix above, and the
+            call to set_iterator_to_next just switched to TEXT_AREA,
+            process the prefix now.  */
+         if (it->area == TEXT_AREA && pending_handle_line_prefix)
+           {
+             pending_handle_line_prefix = false;
+             handle_line_prefix (it);
+           }
          continue;
        }
 
@@ -20297,7 +20481,12 @@ display_line (struct it *it)
                                  it->max_phys_ascent + it->max_phys_descent);
          row->extra_line_spacing = max (row->extra_line_spacing,
                                         it->max_extra_line_spacing);
-         if (it->current_x - it->pixel_width < it->first_visible_x)
+         if (it->current_x - it->pixel_width < it->first_visible_x
+             /* In R2L rows, we arrange in extend_face_to_end_of_line
+                to add a right offset to the line, by a suitable
+                change to the stretch glyph that is the leftmost
+                glyph of the line.  */
+             && !row->reversed_p)
            row->x = x - it->first_visible_x;
          /* Record the maximum and minimum buffer positions seen so
             far in glyphs that will be displayed by this row.  */
@@ -20311,7 +20500,13 @@ display_line (struct it *it)
 
          for (i = 0; i < nglyphs; ++i, x = new_x)
            {
-             glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
+             /* Identify the glyphs added by the last call to
+                PRODUCE_GLYPHS.  In R2L rows, they are prepended to
+                the previous glyphs.  */
+             if (!row->reversed_p)
+               glyph = row->glyphs[TEXT_AREA] + n_glyphs_before + i;
+             else
+               glyph = row->glyphs[TEXT_AREA] + nglyphs - 1 - i;
              new_x = x + glyph->pixel_width;
 
              if (/* Lines are continued.  */
@@ -20346,7 +20541,8 @@ display_line (struct it *it)
                        {
                          /* If line-wrap is on, check if a previous
                             wrap point was found.  */
-                         if (wrap_row_used > 0
+                         if (!IT_OVERFLOW_NEWLINE_INTO_FRINGE (it)
+                             && wrap_row_used > 0
                              /* Even if there is a previous wrap
                                 point, continue the line here as
                                 usual, if (i) the previous character
@@ -20376,6 +20572,18 @@ display_line (struct it *it)
                                  row->continued_p = 0;
                                  row->exact_window_width_line_p = 1;
                                }
+                             /* If line-wrap is on, check if a
+                                previous wrap point was found.  */
+                             else if (wrap_row_used > 0
+                                      /* Even if there is a previous wrap
+                                         point, continue the line here as
+                                         usual, if (i) the previous character
+                                         was a space or tab AND (ii) the
+                                         current character is not.  */
+                                      && (!may_wrap
+                                          || IT_DISPLAYING_WHITESPACE (it)))
+                               goto back_to_wrap;
+
                            }
                        }
                      else if (it->bidi_p)
@@ -20511,10 +20719,28 @@ display_line (struct it *it)
                  if (it->bidi_p)
                    RECORD_MAX_MIN_POS (it);
 
-                 if (x < it->first_visible_x)
+                 if (x < it->first_visible_x && !row->reversed_p)
                    /* Glyph is partially visible, i.e. row starts at
-                      negative X position.  */
+                      negative X position.  Don't do that in R2L
+                      rows, where we arrange to add a right offset to
+                      the line in extend_face_to_end_of_line, by a
+                      suitable change to the stretch glyph that is
+                      the leftmost glyph of the line.  */
                    row->x = x - it->first_visible_x;
+                 /* When the last glyph of an R2L row only fits
+                    partially on the line, we need to set row->x to a
+                    negative offset, so that the leftmost glyph is
+                    the one that is partially visible.  But if we are
+                    going to produce the truncation glyph, this will
+                    be taken care of in produce_special_glyphs.  */
+                 if (row->reversed_p
+                     && new_x > it->last_visible_x
+                     && !(it->line_wrap == TRUNCATE
+                          && WINDOW_LEFT_FRINGE_WIDTH (it->w) == 0))
+                   {
+                     eassert (FRAME_WINDOW_P (it->f));
+                     row->x = it->last_visible_x - new_x;
+                   }
                }
              else
                {
@@ -20588,7 +20814,10 @@ display_line (struct it *it)
                  that they are cropped at the right edge of the
                  window, so an image glyph will always end exactly at
                  last_visible_x, even if there's no right fringe.  */
-              && (WINDOW_RIGHT_FRINGE_WIDTH (it->w) || it->what == IT_IMAGE))
+              && ((row->reversed_p
+                   ? WINDOW_LEFT_FRINGE_WIDTH (it->w)
+                   : WINDOW_RIGHT_FRINGE_WIDTH (it->w))
+                  || it->what == IT_IMAGE))
              ? (it->current_x >= it->last_visible_x)
              : (it->current_x > it->last_visible_x)))
        {
@@ -20665,10 +20894,15 @@ display_line (struct it *it)
          row->truncated_on_right_p = 1;
          it->continuation_lines_width = 0;
          reseat_at_next_visible_line_start (it, 0);
-         if (IT_BYTEPOS (*it) <= BEG_BYTE)
-           row->ends_at_zv_p = true;
+         /* We insist below that IT's position be at ZV because in
+            bidi-reordered lines the character at visible line start
+            might not be the character that follows the newline in
+            the logical order.  */
+         if (IT_BYTEPOS (*it) > BEG_BYTE)
+           row->ends_at_zv_p =
+             IT_BYTEPOS (*it) >= ZV_BYTE && FETCH_BYTE (ZV_BYTE - 1) != '\n';
          else
-           row->ends_at_zv_p = FETCH_BYTE (IT_BYTEPOS (*it) - 1) != '\n';
+           row->ends_at_zv_p = false;
          break;
        }
     }
@@ -20871,7 +21105,8 @@ See also `bidi-paragraph-direction'.  */)
         the previous non-empty line.  */
       if (pos >= ZV && pos > BEGV)
        DEC_BOTH (pos, bytepos);
-      if (fast_looking_at (build_string ("[\f\t ]*\n"),
+      AUTO_STRING (trailing_white_space, "[\f\t ]*\n");
+      if (fast_looking_at (trailing_white_space,
                           pos, bytepos, ZV, ZV_BYTE, Qnil) > 0)
        {
          while ((c = FETCH_BYTE (bytepos)) == '\n'
@@ -20913,6 +21148,143 @@ See also `bidi-paragraph-direction'.  */)
     }
 }
 
+DEFUN ("bidi-find-overridden-directionality",
+       Fbidi_find_overridden_directionality,
+       Sbidi_find_overridden_directionality, 2, 3, 0,
+       doc: /* Return position between FROM and TO where directionality was overridden.
+
+This function returns the first character position in the specified
+region of OBJECT where there is a character whose `bidi-class' property
+is `L', but which was forced to display as `R' by a directional
+override, and likewise with characters whose `bidi-class' is `R'
+or `AL' that were forced to display as `L'.
+
+If no such character is found, the function returns nil.
+
+OBJECT is a Lisp string or buffer to search for overridden
+directionality, and defaults to the current buffer if nil or omitted.
+OBJECT can also be a window, in which case the function will search
+the buffer displayed in that window.  Passing the window instead of
+a buffer is preferable when the buffer is displayed in some window,
+because this function will then be able to correctly account for
+window-specific overlays, which can affect the results.
+
+Strong directional characters `L', `R', and `AL' can have their
+intrinsic directionality overridden by directional override
+control characters RLO \(u+202e) and LRO \(u+202d).  See the
+function `get-char-code-property' for a way to inquire about
+the `bidi-class' property of a character.  */)
+  (Lisp_Object from, Lisp_Object to, Lisp_Object object)
+{
+  struct buffer *buf = current_buffer;
+  struct buffer *old = buf;
+  struct window *w = NULL;
+  bool frame_window_p = FRAME_WINDOW_P (SELECTED_FRAME ());
+  struct bidi_it itb;
+  ptrdiff_t from_pos, to_pos, from_bpos;
+  void *itb_data;
+
+  if (!NILP (object))
+    {
+      if (BUFFERP (object))
+       buf = XBUFFER (object);
+      else if (WINDOWP (object))
+       {
+         w = decode_live_window (object);
+         buf = XBUFFER (w->contents);
+         frame_window_p = FRAME_WINDOW_P (XFRAME (w->frame));
+       }
+      else
+       CHECK_STRING (object);
+    }
+
+  if (STRINGP (object))
+    {
+      /* Characters in unibyte strings are always treated by bidi.c as
+        strong LTR.  */
+      if (!STRING_MULTIBYTE (object)
+         /* When we are loading loadup.el, the character property
+            tables needed for bidi iteration are not yet
+            available.  */
+         || !NILP (Vpurify_flag))
+       return Qnil;
+
+      validate_subarray (object, from, to, SCHARS (object), &from_pos, &to_pos);
+      if (from_pos >= SCHARS (object))
+       return Qnil;
+
+      /* Set up the bidi iterator.  */
+      itb_data = bidi_shelve_cache ();
+      itb.paragraph_dir = NEUTRAL_DIR;
+      itb.string.lstring = object;
+      itb.string.s = NULL;
+      itb.string.schars = SCHARS (object);
+      itb.string.bufpos = 0;
+      itb.string.from_disp_str = 0;
+      itb.string.unibyte = 0;
+      itb.w = w;
+      bidi_init_it (0, 0, frame_window_p, &itb);
+    }
+  else
+    {
+      /* Nothing this fancy can happen in unibyte buffers, or in a
+        buffer that disabled reordering, or if FROM is at EOB.  */
+      if (NILP (BVAR (buf, bidi_display_reordering))
+         || NILP (BVAR (buf, enable_multibyte_characters))
+         /* When we are loading loadup.el, the character property
+            tables needed for bidi iteration are not yet
+            available.  */
+         || !NILP (Vpurify_flag))
+       return Qnil;
+
+      set_buffer_temp (buf);
+      validate_region (&from, &to);
+      from_pos = XINT (from);
+      to_pos = XINT (to);
+      if (from_pos >= ZV)
+       return Qnil;
+
+      /* Set up the bidi iterator.  */
+      itb_data = bidi_shelve_cache ();
+      from_bpos = CHAR_TO_BYTE (from_pos);
+      if (from_pos == BEGV)
+       {
+         itb.charpos = BEGV;
+         itb.bytepos = BEGV_BYTE;
+       }
+      else if (FETCH_CHAR (from_bpos - 1) == '\n')
+       {
+         itb.charpos = from_pos;
+         itb.bytepos = from_bpos;
+       }
+      else
+       itb.charpos = find_newline_no_quit (from_pos, CHAR_TO_BYTE (from_pos),
+                                           -1, &itb.bytepos);
+      itb.paragraph_dir = NEUTRAL_DIR;
+      itb.string.s = NULL;
+      itb.string.lstring = Qnil;
+      itb.string.bufpos = 0;
+      itb.string.from_disp_str = 0;
+      itb.string.unibyte = 0;
+      itb.w = w;
+      bidi_init_it (itb.charpos, itb.bytepos, frame_window_p, &itb);
+    }
+
+  ptrdiff_t found;
+  do {
+    /* For the purposes of this function, the actual base direction of
+       the paragraph doesn't matter, so just set it to L2R.  */
+    bidi_paragraph_init (L2R, &itb, 0);
+    while ((found = bidi_find_first_overridden (&itb)) < from_pos)
+      ;
+  } while (found == ZV && itb.ch == '\n' && itb.charpos < to_pos);
+
+  bidi_unshelve_cache (itb_data, 0);
+  set_buffer_temp (old);
+
+  return (from_pos <= found && found < to_pos) ? make_number (found) : Qnil;
+}
+
 DEFUN ("move-point-visually", Fmove_point_visually,
        Smove_point_visually, 1, 1, 0,
        doc: /* Move point in the visual order in the specified DIRECTION.
@@ -20982,7 +21354,7 @@ Value is the new character position of point.  */)
                  if ((gpt->resolved_level - row->reversed_p) % 2 == 0)
                    new_pos += (row->reversed_p ? -dir : dir);
                  else
-                   new_pos -= (row->reversed_p ? -dir : dir);;
+                   new_pos -= (row->reversed_p ? -dir : dir);
                }
              else if (BUFFERP (g->object))
                new_pos = g->charpos;
@@ -21372,6 +21744,114 @@ Value is the new character position of point.  */)
 #undef ROW_GLYPH_NEWLINE_P
 }
 
+DEFUN ("bidi-resolved-levels", Fbidi_resolved_levels,
+       Sbidi_resolved_levels, 0, 1, 0,
+       doc: /* Return the resolved bidirectional levels of characters at VPOS.
+
+The resolved levels are produced by the Emacs bidi reordering engine
+that implements the UBA, the Unicode Bidirectional Algorithm.  Please
+read the Unicode Standard Annex 9 (UAX#9) for background information
+about these levels.
+
+VPOS is the zero-based number of the current window's screen line
+for which to produce the resolved levels.  If VPOS is nil or omitted,
+it defaults to the screen line of point.  If the window displays a
+header line, VPOS of zero will report on the header line, and first
+line of text in the window will have VPOS of 1.
+
+Value is an array of resolved levels, indexed by glyph number.
+Glyphs are numbered from zero starting from the beginning of the
+screen line, i.e. the left edge of the window for left-to-right lines
+and from the right edge for right-to-left lines.  The resolved levels
+are produced only for the window's text area; text in display margins
+is not included.
+
+If the selected window's display is not up-to-date, or if the specified
+screen line does not display text, this function returns nil.  It is
+highly recommended to bind this function to some simple key, like F8,
+in order to avoid these problems.
+
+This function exists mainly for testing the correctness of the
+Emacs UBA implementation, in particular with the test suite.  */)
+  (Lisp_Object vpos)
+{
+  struct window *w = XWINDOW (selected_window);
+  struct buffer *b = XBUFFER (w->contents);
+  int nrow;
+  struct glyph_row *row;
+
+  if (NILP (vpos))
+    {
+      int d1, d2, d3, d4, d5;
+
+      pos_visible_p (w, PT, &d1, &d2, &d3, &d4, &d5, &nrow);
+    }
+  else
+    {
+      CHECK_NUMBER_COERCE_MARKER (vpos);
+      nrow = XINT (vpos);
+    }
+
+  /* We require up-to-date glyph matrix for this window.  */
+  if (w->window_end_valid
+      && !windows_or_buffers_changed
+      && b
+      && !b->clip_changed
+      && !b->prevent_redisplay_optimizations_p
+      && !window_outdated (w)
+      && nrow >= 0
+      && nrow < w->current_matrix->nrows
+      && (row = MATRIX_ROW (w->current_matrix, nrow))->enabled_p
+      && MATRIX_ROW_DISPLAYS_TEXT_P (row))
+    {
+      struct glyph *g, *e, *g1;
+      int nglyphs, i;
+      Lisp_Object levels;
+
+      if (!row->reversed_p)    /* Left-to-right glyph row.  */
+       {
+         g = g1 = row->glyphs[TEXT_AREA];
+         e = g + row->used[TEXT_AREA];
+
+         /* Skip over glyphs at the start of the row that was
+            generated by redisplay for its own needs.  */
+         while (g < e
+                && INTEGERP (g->object)
+                && g->charpos < 0)
+           g++;
+         g1 = g;
+
+         /* Count the "interesting" glyphs in this row.  */
+         for (nglyphs = 0; g < e && !INTEGERP (g->object); g++)
+           nglyphs++;
+
+         /* Create and fill the array.  */
+         levels = make_uninit_vector (nglyphs);
+         for (i = 0; g1 < g; i++, g1++)
+           ASET (levels, i, make_number (g1->resolved_level));
+       }
+      else                     /* Right-to-left glyph row.  */
+       {
+         g = row->glyphs[TEXT_AREA] + row->used[TEXT_AREA] - 1;
+         e = row->glyphs[TEXT_AREA] - 1;
+         while (g > e
+                && INTEGERP (g->object)
+                && g->charpos < 0)
+           g--;
+         g1 = g;
+         for (nglyphs = 0; g > e && !INTEGERP (g->object); g--)
+           nglyphs++;
+         levels = make_uninit_vector (nglyphs);
+         for (i = 0; g1 > g; i++, g1--)
+           ASET (levels, i, make_number (g1->resolved_level));
+       }
+      return levels;
+    }
+  else
+    return Qnil;
+}
+
+
 \f
 /***********************************************************************
                               Menu Bar
@@ -21450,6 +21930,7 @@ display_menu_bar (struct window *w)
       clear_glyph_row (row);
       row->enabled_p = true;
       row->full_width_p = 1;
+      row->reversed_p = false;
     }
 
   /* Display all items of the menu bar.  */
@@ -22683,10 +23164,8 @@ decode_mode_spec_coding (Lisp_Object coding_system, register char *buf, int eol_
        }
       else if (CHARACTERP (eoltype))
        {
-         unsigned char *tmp = alloca (MAX_MULTIBYTE_LENGTH);
          int c = XFASTINT (eoltype);
-         eol_str_len = CHAR_STRING (c, tmp);
-         eol_str = tmp;
+         return buf + CHAR_STRING (c, (unsigned char *) buf);
        }
       else
        {
@@ -22822,7 +23301,7 @@ decode_mode_spec (struct window *w, register int c, int field_width,
        }
 
     case 'e':
-#ifndef SYSTEM_MALLOC
+#if !defined SYSTEM_MALLOC && !defined HYBRID_MALLOC
       {
        if (NILP (Vmemory_full))
          return "";
@@ -24637,7 +25116,7 @@ compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
         face_id = (row)->glyphs[area][START].face_id;                     \
                                                                           \
         s = alloca (sizeof *s);                                           \
-        char2b = alloca ((END - START) * sizeof *char2b);                 \
+        SAFE_NALLOCA (char2b, 1, (END) - (START));                        \
         INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);           \
         append_glyph_string (&HEAD, &TAIL, s);                            \
         s->x = (X);                                                       \
@@ -24665,7 +25144,7 @@ compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
     struct glyph_string *first_s = NULL;                                   \
     int n;                                                                 \
                                                                            \
-    char2b = alloca (cmp->glyph_len * sizeof *char2b);                     \
+    SAFE_NALLOCA (char2b, 1, cmp->glyph_len);                              \
                                                                            \
     /* Make glyph_strings for each glyph sequence that is drawable by      \
        the same face, and append them to HEAD/TAIL.  */                            \
@@ -24700,7 +25179,7 @@ compute_overhangs_and_x (struct glyph_string *s, int x, int backward_p)
     gstring = (composition_gstring_from_id                               \
               ((row)->glyphs[area][START].u.cmp.id));                    \
     s = alloca (sizeof *s);                                              \
-    char2b = alloca (LGSTRING_GLYPH_LEN (gstring) * sizeof *char2b);     \
+    SAFE_NALLOCA (char2b, 1, LGSTRING_GLYPH_LEN (gstring));              \
     INIT_GLYPH_STRING (s, char2b, w, row, area, START, HL);              \
     append_glyph_string (&(HEAD), &(TAIL), s);                           \
     s->x = (X);                                                                  \
@@ -24871,6 +25350,7 @@ draw_glyphs (struct window *w, int x, struct glyph_row *row,
      BUILD_GLYPH_STRINGS will modify its start parameter.  That's
      the reason we use a separate variable `i'.  */
   i = start;
+  USE_SAFE_ALLOCA;
   BUILD_GLYPH_STRINGS (i, end, head, tail, hl, x, last_x);
   if (tail)
     x_reached = tail->x + tail->background_width;
@@ -25070,6 +25550,7 @@ draw_glyphs (struct window *w, int x, struct glyph_row *row,
 
   RELEASE_HDC (hdc, f);
 
+  SAFE_FREE ();
   return x_reached;
 }
 
@@ -25155,8 +25636,7 @@ append_glyph (struct it *it)
       if (it->bidi_p)
        {
          glyph->resolved_level = it->bidi_it.resolved_level;
-         if ((it->bidi_it.type & 7) != it->bidi_it.type)
-           emacs_abort ();
+         eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
          glyph->bidi_type = it->bidi_it.type;
        }
       else
@@ -25239,8 +25719,7 @@ append_composite_glyph (struct it *it)
       if (it->bidi_p)
        {
          glyph->resolved_level = it->bidi_it.resolved_level;
-         if ((it->bidi_it.type & 7) != it->bidi_it.type)
-           emacs_abort ();
+         eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
          glyph->bidi_type = it->bidi_it.type;
        }
       ++it->glyph_row->used[area];
@@ -25428,8 +25907,7 @@ produce_image_glyph (struct it *it)
          if (it->bidi_p)
            {
              glyph->resolved_level = it->bidi_it.resolved_level;
-             if ((it->bidi_it.type & 7) != it->bidi_it.type)
-               emacs_abort ();
+             eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
              glyph->bidi_type = it->bidi_it.type;
            }
          ++it->glyph_row->used[area];
@@ -25549,6 +26027,24 @@ append_stretch_glyph (struct it *it, Lisp_Object object,
          for (g = glyph - 1; g >= it->glyph_row->glyphs[area]; g--)
            g[1] = *g;
          glyph = it->glyph_row->glyphs[area];
+
+         /* Decrease the width of the first glyph of the row that
+            begins before first_visible_x (e.g., due to hscroll).
+            This is so the overall width of the row becomes smaller
+            by the scroll amount, and the stretch glyph appended by
+            extend_face_to_end_of_line will be wider, to shift the
+            row glyphs to the right.  (In L2R rows, the corresponding
+            left-shift effect is accomplished by setting row->x to a
+            negative value, which won't work with R2L rows.)
+
+            This must leave us with a positive value of WIDTH, since
+            otherwise the call to move_it_in_display_line_to at the
+            beginning of display_line would have got past the entire
+            first glyph, and then it->current_x would have been
+            greater or equal to it->first_visible_x.  */
+         if (it->current_x < it->first_visible_x)
+           width -= it->first_visible_x - it->current_x;
+         eassert (width > 0);
        }
       glyph->charpos = CHARPOS (it->position);
       glyph->object = object;
@@ -25582,8 +26078,7 @@ append_stretch_glyph (struct it *it, Lisp_Object object,
       if (it->bidi_p)
        {
          glyph->resolved_level = it->bidi_it.resolved_level;
-         if ((it->bidi_it.type & 7) != it->bidi_it.type)
-           emacs_abort ();
+         eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
          glyph->bidi_type = it->bidi_it.type;
        }
       else
@@ -25878,14 +26373,13 @@ produce_special_glyphs (struct it *it, enum display_element_type what)
 
   temp_it.dp = NULL;
   temp_it.what = IT_CHARACTER;
-  temp_it.len = 1;
   temp_it.c = temp_it.char_to_display = GLYPH_CHAR (glyph);
   temp_it.face_id = GLYPH_FACE (glyph);
   temp_it.len = CHAR_BYTES (temp_it.c);
 
   PRODUCE_GLYPHS (&temp_it);
   it->pixel_width = temp_it.pixel_width;
-  it->nglyphs = temp_it.pixel_width;
+  it->nglyphs = temp_it.nglyphs;
 }
 
 #ifdef HAVE_WINDOW_SYSTEM
@@ -26043,8 +26537,7 @@ append_glyphless_glyph (struct it *it, int face_id, int for_no_font, int len,
       if (it->bidi_p)
        {
          glyph->resolved_level = it->bidi_it.resolved_level;
-         if ((it->bidi_it.type & 7) != it->bidi_it.type)
-           emacs_abort ();
+         eassert ((it->bidi_it.type & 7) == it->bidi_it.type);
          glyph->bidi_type = it->bidi_it.type;
        }
       ++it->glyph_row->used[area];
@@ -27542,7 +28035,7 @@ erase_phys_cursor (struct window *w)
   /* Maybe clear the display under the cursor.  */
   if (w->phys_cursor_type == HOLLOW_BOX_CURSOR)
     {
-      int x, y, left_x;
+      int x, y;
       int header_line_height = WINDOW_HEADER_LINE_HEIGHT (w);
       int width;
 
@@ -27551,13 +28044,15 @@ erase_phys_cursor (struct window *w)
        goto mark_cursor_off;
 
       width = cursor_glyph->pixel_width;
-      left_x = window_box_left_offset (w, TEXT_AREA);
       x = w->phys_cursor.x;
-      if (x < left_x)
-       width -= left_x - x;
+      if (x < 0)
+       {
+         width += x;
+         x = 0;
+       }
       width = min (width, window_box_width (w, TEXT_AREA) - x);
       y = WINDOW_TO_FRAME_PIXEL_Y (w, max (header_line_height, cursor_row->y));
-      x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, max (x, left_x));
+      x = WINDOW_TEXT_TO_FRAME_PIXEL_X (w, x);
 
       if (width > 0)
        FRAME_RIF (f)->clear_frame_area (f, x, y, width, cursor_row->visible_height);
@@ -27633,6 +28128,10 @@ display_and_set_cursor (struct window *w, bool on,
       && (!on
          || w->phys_cursor.x != x
          || w->phys_cursor.y != y
+         /* HPOS can be negative in R2L rows whose
+            exact_window_width_line_p flag is set (i.e. their newline
+            would "overflow into the fringe").  */
+         || hpos < 0
          || new_cursor_type != w->phys_cursor_type
          || ((new_cursor_type == BAR_CURSOR || new_cursor_type == HBAR_CURSOR)
              && new_cursor_width != w->phys_cursor_width)))
@@ -27879,7 +28378,7 @@ show_mouse_face (Mouse_HLInfo *hlinfo, enum draw_glyphs_face draw)
 
 #ifdef HAVE_WINDOW_SYSTEM
   /* Change the mouse cursor.  */
-  if (FRAME_WINDOW_P (f))
+  if (FRAME_WINDOW_P (f) && NILP (do_mouse_tracking))
     {
 #if ! defined (USE_GTK) && ! defined (HAVE_NS)
       if (draw == DRAW_NORMAL_TEXT
@@ -29407,6 +29906,8 @@ note_mouse_highlight (struct frame *f, int x, int y)
       /* Is this char mouse-active or does it have help-echo?  */
       position = make_number (pos);
 
+      USE_SAFE_ALLOCA;
+
       if (BUFFERP (object))
        {
          /* Put all the overlays we want in a vector in overlay_vec.  */
@@ -29688,6 +30189,7 @@ note_mouse_highlight (struct frame *f, int x, int y)
       BEGV = obegv;
       ZV = ozv;
       current_buffer = obuf;
+      SAFE_FREE ();
     }
 
  set_cursor:
@@ -30207,8 +30709,6 @@ expose_frame (struct frame *f, int x, int y, int w, int h)
       r.x = r.y = 0;
       r.width = FRAME_TEXT_WIDTH (f);
       r.height = FRAME_TEXT_HEIGHT (f);
-/**       r.width = FRAME_COLUMN_WIDTH (f) * FRAME_COLS (f); **/
-/**       r.height = FRAME_LINE_HEIGHT (f) * FRAME_LINES (f); **/
     }
   else
     {
@@ -30361,6 +30861,7 @@ syms_of_xdisp (void)
   defsubr (&Scurrent_bidi_paragraph_direction);
   defsubr (&Swindow_text_pixel_size);
   defsubr (&Smove_point_visually);
+  defsubr (&Sbidi_find_overridden_directionality);
 
   DEFSYM (Qmenu_bar_update_hook, "menu-bar-update-hook");
   DEFSYM (Qoverriding_terminal_local_map, "overriding-terminal-local-map");
@@ -30463,6 +30964,7 @@ syms_of_xdisp (void)
 
   DEFSYM (Qright_to_left, "right-to-left");
   DEFSYM (Qleft_to_right, "left-to-right");
+  defsubr (&Sbidi_resolved_levels);
 
 #ifdef HAVE_WINDOW_SYSTEM
   DEFVAR_BOOL ("x-stretch-cursor", x_stretch_cursor_p,
@@ -30642,9 +31144,12 @@ all the functions in the list are called, with the frame as argument.  */);
   DEFVAR_LISP ("window-scroll-functions", Vwindow_scroll_functions,
     doc: /* List of functions to call before redisplaying a window with scrolling.
 Each function is called with two arguments, the window and its new
-display-start position.  Note that these functions are also called by
-`set-window-buffer'.  Also note that the value of `window-end' is not
-valid when these functions are called.
+display-start position.
+These functions are called whenever the `window-start' marker is modified,
+either to point into another buffer (e.g. via `set-window-buffer') or another
+place in the same buffer.
+Note that the value of `window-end' is not valid when these functions are
+called.
 
 Warning: Do not use this feature to alter the way the window
 is scrolled.  It is not designed for that, and such use probably won't
@@ -30869,6 +31374,12 @@ To add a prefix to continuation lines, use `wrap-prefix'.  */);
     doc: /* Non-nil means don't free realized faces.  Internal use only.  */);
   inhibit_free_realized_faces = 0;
 
+  DEFVAR_BOOL ("inhibit-bidi-mirroring", inhibit_bidi_mirroring,
+    doc: /* Non-nil means don't mirror characters even when bidi context requires that.
+Intended for use during debugging and for testing bidi display;
+see biditest.el in the test suite.  */);
+  inhibit_bidi_mirroring = 0;
+
 #ifdef GLYPH_DEBUG
   DEFVAR_BOOL ("inhibit-try-window-id", inhibit_try_window_id,
               doc: /* Inhibit try_window_id display optimization.  */);