Emacs not moving point after goto-char on buried buffer

Viewed 137

if I eval

(with-current-buffer "xx" 
 (goto-char(point-max)))

when the buffer xx is buried, point is NOT moved after I switch to it. it's driving me crazy after sifting my code for a bug for 5 days only to find it isn't one but a maddening behaviour I can find no documentation for nor search results relating to.

2 Answers

You moved point to where you wanted in that buffer, but not in any window. The buffer in question need never be displayed. with-current-buffer lets Lisp code do stuff in a buffer. You're confusing window-point with point.

Try this, to see the difference, assuming that your buffer xx is displayed in some visible window on some frame (or use t instead of visible if it's in an invisible window):

(with-current-buffer "xx"
  (goto-char 5000)
  (message "PT: %S, WINDOW PT: %S"
           (point)
           (window-point (get-buffer-window "xx" 'visible))))

You can use function set-window-point to set the window-point. See the Elisp manual, node Window Point.

from a reading of the source it appears emacs keeps a list of previously seen buffers per window (window-prev-buffers) recording the cursor position that prevailed at the time of last viewing. switch-to-buffer, the standard way of raising a buffer, consults switch-to-buffer-preserve-window-point in deciding whether to restore this saved cursor position when a previously seen buffer is revisited by a window. by setting the variable to nil the buffer's actual point is used as the window point. this gets the behaviour I want although the variable is not consulted on a buffer local basis so setting has to be global (or local to the buffer one happens to be switching from!) which is undesirable since I can't say there are no alternate universes out there where it's useful for a window and buffer to not agree on where the cursor is.

Related