Subject: bug#11133: 24.0.94; Cursor gets "stuck" on an indented line with wrapped overlay

From: Eli Zaretskii <[email-masked]>
Date: Fri, 06 Apr 2012 18:48:15 +0300

> Date: Fri, 30 Mar 2012 19:12:10 +0300
> From: Eli Zaretskii <[email-masked]>
> Cc: [email-masked]
> 
> > From: Ivan Andrus <[email-masked]>
> > Date: Fri, 30 Mar 2012 16:47:05 +0200
> > 
> > Put the following (including comments) in a buffer and evaluate.
> > 
> > #+begin_src emacs-lisp
> > ;; Note that the next line is indented
> >  ;; This buffer is for notes you don't want to save, and for Lisp evaluation.;; If you want to create a file, visit that file with C-x C-f,;; then enter the text in that file's own buffer.
> > 
> > ;; evaluate this
> > (let ((ov (make-overlay
> > 	   (progn (goto-char (point-min))
> > 		  (forward-line)
> > 		  (back-to-indentation)
> > 		  (point))
> > 	   (progn (end-of-line)
> > 		  (point))
> > 	   nil t nil)))
> >  (overlay-put ov 'display
> > 	       (make-string 500 ?b)))
> > 
> > #+end_src
> > 
> > Now run `previous-line' several times and it will get stuck at the
> > indentation of the bbbbb line.  If you have an extremely wide screen you
> > may need to shrink the emacs window so that the bbbb line wraps.  Also
> > note that running `previous-line' with a large prefix-argument will
> > move past the line
> 
> More accurately, "large enough" is the number of screen lines taken by
> the overlay string.  In my case, it was 7.
> 
> > This does not seem to be a problem in Emacs 22.1.1 in a terminal which
> > is the only other emacs I have easy access to.
> 
> This is a bug in visual line movement.  Visual line movement was
> introduced in Emacs 23, that's why you don't see this in Emacs 22.  In
> Emacs 23, the cursor gets stuck similarly, so this is not a regression
> wrt Emacs 23.  Disable line-move-visual with the current 24 trunk, and
> the problem will go away in Emacs 24 as well.

The problem here is that vertical-motion does not consider the case of
a display string that starts at point and takes more than one screen
line (because it's long).  The patch below plumbs that hole.

Stefan and Chong, please tell me whether to install now or wait until
after the Emacs 24.1 branch is cut.

=== modified file 'src/indent.c'
--- src/indent.c	2012-01-19 07:21:25 +0000
+++ src/indent.c	2012-04-06 15:40:43 +0000
@@ -2021,6 +2021,7 @@ whether or not it is currently displayed
       EMACS_INT it_start;
       int first_x, it_overshoot_count = 0;
       int overshoot_handled = 0;
+      int disp_string_at_start_p = 0;
 
       itdata = bidi_shelve_cache ();
       SET_TEXT_POS (pt, PT, PT_BYTE);
@@ -2035,6 +2036,8 @@ whether or not it is currently displayed
 	{
 	  const char *s = SSDATA (it.string);
 	  const char *e = s + SBYTES (it.string);
+
+	  disp_string_at_start_p = it.string_from_display_prop_p;
 	  while (s < e)
 	    {
 	      if (*s++ == '\n')
@@ -2062,19 +2065,29 @@ whether or not it is currently displayed
       /* IT may move too far if truncate-lines is on and PT lies
 	 beyond the right margin.  IT may also move too far if the
 	 starting point is on a Lisp string that has embedded
-	 newlines.  In these cases, backtrack.  */
+	 newlines, or spans several screen lines.  In these cases,
+	 backtrack.  */
       if (IT_CHARPOS (it) > it_start)
 	{
-	  /* We need to backtrack also if the Lisp string contains no
-	     newlines, but there is a newline right after it.  In this
-	     case, IT overshoots if there is an after-string just
-	     before the newline.  */
-	  if (it_overshoot_count < 0
-	      && it.method == GET_FROM_BUFFER
-	      && it.c == '\n')
-	    it_overshoot_count = 1;
-	  if (it_overshoot_count > 0)
-	    move_it_by_lines (&it, -it_overshoot_count);
+	  if (it_overshoot_count < 0)
+	    {
+	      /* We need to backtrack also if the Lisp string contains
+		 no newlines, but there is a newline right after it.
+		 In this case, IT overshoots if there is an
+		 after-string just before the newline.  */
+	      if (it.method == GET_FROM_BUFFER && it.c == '\n')
+		it_overshoot_count = 1;
+	      else if (disp_string_at_start_p && it.vpos > 0)
+		{
+		  /* This is the case of a display string that spans
+		     several screen lines.  In that case, we end up at
+		     the end of the string, and it.vpos tells us how
+		     many screen lines we need to backtrack.  */
+		  it_overshoot_count = it.vpos;
+		}
+	      if (it_overshoot_count > 0)
+		move_it_by_lines (&it, -it_overshoot_count);
+	    }
 
 	  overshoot_handled = 1;
 	}