Subject: Re: bug#12170: save-excursion fails boundary case with recenter

From: martin rudalics <[email-masked]>
Date: Fri, 10 Aug 2012 16:47:09 +0200

> (1) By "window height", I meant the number of lines displayed for the actual
 > buffer, not counting the mode line or minibuffer.  It turns out this is one
 > less than the value returned by `window-height'.
 >
 > (2) The value of `point' changes (the cursor "hops", in other words) when
 > the window is redisplayed, or when control returns to the user.
 >
 > Here is code that will produce the bug (with emacs -Q):
 >
 > (progn
 >    (defun f (n)
 >      (save-excursion (forward-line (- n)) (recenter 0)))
 >    (let ((buffer (switch-to-buffer "foo"))
 >          (height (1- (window-height (get-buffer-window "foo")))))
 >      (insert-char 10 (* height 2))
 >      (let ((pt (point)))
 >        (f height)
 >        (redisplay)
 >        (message "height %s old %s new %s" height pt (point)))))
 >
 > If you leave out the `redisplay' call, on the other hand, old = new in the
 > message -- but after control returns to the user, point has still been moved
 > in the buffer.

I see it now, thanks.  Surprisingly it doesn't show up with code like

(progn
   (defmacro save-this-window-excursion (&rest body)
     "..."
     (let ((start (make-symbol "start"))
	  (point (make-symbol "point")))
       `(let ((,start (copy-marker (window-start)))
	     (,point (copy-marker (window-point))))
	 (save-selected-window
	   (progn ,@body))
	 (set-window-start (selected-window) ,start t)
	 (set-window-point (selected-window) ,point))))

   (defun f (n)
      (save-this-window-excursion (forward-line (- n)) (recenter 0)))

    (let ((buffer (switch-to-buffer "foo"))
          (height (1- (window-height (get-buffer-window "foo")))))
      (insert-char 10 (* height 2))
      (let ((pt (point)))
        (f height)
        (redisplay)
        (message "height %s old %s new %s" height pt (point)))))

so I'd suspect the culprit somewhere in redisplay_window's code

   if (w->optional_new_start

w->optional_new_start is 1 from `recenter'

       && CHARPOS (startp) >= BEGV
       && CHARPOS (startp) <= ZV)
     {
       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;

w->force_start 1 will cause redisplay to honor the start position set up
by `recenter' despite of save_excursion_restore's Fgoto_char.

     }

But I don't have the slightest idea how calling

	 (set-window-start (selected-window) ,start t)

would remedy this.  Eli will soon teach us a lesson here.

martin