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 #23 received at 12170 <at> debbugs.gnu.org (full text, mbox):

From: Stefan Monnier <monnier <at> IRO.UMontreal.CA>
To: "Bill Brodie" <wbrodie <at> panix.com>
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:04:47 -0400
> (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 138 days ago.

Previous Next


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