GNU bug report logs - #12170
save-excursion fails boundary case with recenter

Previous Next

Package: emacs;

Reported by: "Bill Brodie" <wbrodie <at> panix.com>

Date: Fri, 10 Aug 2012 02:01:02 UTC

Severity: normal

Tags: notabug

Done: npostavs <at> users.sourceforge.net

Bug is archived. No further changes may be made.

Full log


Message #26 received at 12170 <at> debbugs.gnu.org (full text, mbox):

From: "Bill Brodie" <wbrodie <at> panix.com>
To: "'Stefan Monnier'" <monnier <at> IRO.UMontreal.CA>
Cc: 'martin rudalics' <rudalics <at> gmx.at>, 12170 <at> debbugs.gnu.org
Subject: RE: bug#12170: save-excursion fails boundary case with recenter
Date: Fri, 10 Aug 2012 11:46:25 -0400
Thanks, Stefan.

The purpose of the original code was to scroll the window so that a previous
section marker is at the top of the window only if it will fit without
moving point:

(defun f ()
   (interactive)
   (save-excursion
     (re-search-backward "^__")
     (beginning-of-line)
     (recenter 0)))

I was surprised to discover that this fails if the span of lines from the
previous section marker to the current position, inclusive, just barely
won't fit in the window.

Perhaps the moral is that `save-excursion' should not enclose any code,
including `recenter', that affects how buffers are displayed within windows.

Bill

-----Original Message-----

> (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)))))

As mentioned by Martin, this is a misunderstanding about what save-excursion
does and what `point' is.

Every buffer can have many different `point's (it basically has one per
window, accessible via `window-point' and changeable via `set-window-point',
plus one for itself, called `point').
`save-excursion' preserves only `point'.
`recenter' changes `window-point'.

But `point' and `window-point' are linked (point is set to window-point and
vice-versa in various occasions), so they're often confused.

It seems your real problem is not that `point' changes but that the cursor
ends up in a different position than the one you wanted (the cursor
position, is represented by `window-point' rather than by `point'), right?

If so, you want to preserve window-point.  And there's nothing quite like
save-excursion to preserve window-point.  You can try save-window-excursion,
tho it will do a lot more than you asked for.
Or otherwise manually read window-point at the beginning and
set-window-point at the end.


        Stefan





This bug report was last modified 8 years and 120 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.