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

From: Eli Zaretskii <[email-masked]>
Date: Fri, 10 Aug 2012 22:01:51 +0300

> Date: Fri, 10 Aug 2012 16:47:09 +0200
> From: martin rudalics <[email-masked]>
> Cc: [email-masked]
> 
>    (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

By saying "culprit" you mean you think this is a bug?  I think Stefan
explained why it isn't.

>    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'

Only if point will be visible when window is displayed starting at
startp.

> 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.

Not sure there's a lesson here to learn, but set-window-start sets the
w->force_start flag unconditionally, so it will hold even if point is
not in the displayed portion of the window (i.e., point will move).
Does that explain things?