From 17ee955294f501c5da91d74baa8a8e38210d77eb Mon Sep 17 00:00:00 2001
From: Eli Zaretskii <eliz@gnu.org>
Date: Mon, 28 Jul 2014 16:09:02 +0300
Subject: [PATCH] Fix another part of bug #18035 with redisplay of line-prefix
 and linum-mode.

 src/xdisp.c (display_line): If called with iterator set up to write
 to a marginal area, delay the call to handle_line_prefix until we
 switch back to the text area.
---
 src/ChangeLog |  4 ++++
 src/xdisp.c   | 27 +++++++++++++++++++++++----
 2 files changed, 27 insertions(+), 4 deletions(-)

diff --git a/src/ChangeLog b/src/ChangeLog
index 619efb479e6..090ed55bf92 100644
--- a/src/ChangeLog
+++ b/src/ChangeLog
@@ -1,5 +1,9 @@
 2014-07-28  Eli Zaretskii  <eliz@gnu.org>
 
+	* xdisp.c (display_line): If called with iterator set up to write
+	to a marginal area, delay the call to handle_line_prefix until we
+	switch back to the text area.  (Bug#18035)
+
 	* .gdbinit (xwindow): The members total_cols, total_lines,
 	left_col, and top_line are C integers (and has been so for the
 	last 1.5 years).
diff --git a/src/xdisp.c b/src/xdisp.c
index d1016b6b8d7..5cd90136ee6 100644
--- a/src/xdisp.c
+++ b/src/xdisp.c
@@ -19942,6 +19942,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);
@@ -20002,13 +20003,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.  */
@@ -20140,6 +20151,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;
 	}
 
-- 
2.39.5