unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Bill Brodie" <wbrodie@panix.com>
To: "'Stefan Monnier'" <monnier@IRO.UMontreal.CA>
Cc: 12170@debbugs.gnu.org
Subject: bug#12170: save-excursion fails boundary case with recenter
Date: Fri, 10 Aug 2012 11:46:25 -0400	[thread overview]
Message-ID: <006101cd770f$4cd7d380$e6877a80$@com> (raw)
In-Reply-To: <jwv4noag5if.fsf-monnier+emacs@gnu.org>

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






  reply	other threads:[~2012-08-10 15:46 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-08-10  1:23 bug#12170: save-excursion fails boundary case with recenter Bill Brodie
2012-08-10  9:34 ` martin rudalics
2012-08-10  9:50   ` Bastien
2012-08-10 13:03     ` martin rudalics
2012-08-10 13:40   ` Bill Brodie
2012-08-10 14:47     ` martin rudalics
2012-08-10 16:18       ` Stefan Monnier
2012-08-10 16:47         ` martin rudalics
2012-08-10 17:07           ` Stefan Monnier
2012-08-10 19:01       ` Eli Zaretskii
2012-08-11  9:32         ` martin rudalics
2012-08-11 11:11           ` Eli Zaretskii
2012-08-11 14:22             ` martin rudalics
2012-08-11 15:10               ` Eli Zaretskii
2012-08-11 16:05                 ` martin rudalics
2012-08-11 16:31                   ` Eli Zaretskii
2012-08-11 16:49                     ` Eli Zaretskii
2012-08-12 10:31                       ` martin rudalics
2012-08-11 16:27                 ` Bill Brodie
2012-08-10 15:04     ` Stefan Monnier
2012-08-10 15:46       ` Bill Brodie [this message]
2012-08-10 16:46         ` martin rudalics
2012-08-10 16:46       ` martin rudalics
2012-08-10 18:58     ` Eli Zaretskii
2012-08-11  9:31       ` martin rudalics

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='006101cd770f$4cd7d380$e6877a80$@com' \
    --to=wbrodie@panix.com \
    --cc=12170@debbugs.gnu.org \
    --cc=monnier@IRO.UMontreal.CA \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).