2

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.

Drew
  • 29,895
  • 7
  • 74
  • 104
progman
  • 71
  • 3
  • The above code works fine for me in a quick test with three dummy buffers (`AAA, BBB, CCC`). After writing some text to `AAA`, I place point at `1`, and then I switch to `*scratch*`. There, I run the following code: `(bury-buffer "AAA") (with-current-buffer "AAA" (goto-char (point-max)))`. Returning to `AAA` after that, point is now located at the end of the buffer (`point-max`). – Thomas Apr 04 '21 at 21:21

2 Answers2

2

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.

progman
  • 71
  • 3
1

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.

Drew
  • 29,895
  • 7
  • 74
  • 104
  • but the 'point' IS that the buffer is not selected nor visible. window-point is taken from point, not the other way round so when unburying the buffer, my expectation is for the window point to follow that of the buffer I set programmatically. – progman Apr 04 '21 at 08:22
  • *"my expectation is for the window point to follow that of the buffer"*. Your expectation is wrong. That's the point of `window-point` being something other than `point`. Please read the manual, as referenced. – Drew Apr 04 '21 at 15:13
  • the manual discusses the setting of window point in the context of the selection/de-selection of a window. the page you mention, which I had already seen, does not address how the window point is set when an existing window has the buffer it is displaying changed to something else ie. switch-to-buffer. – progman Apr 04 '21 at 20:45
  • my point is that there is no window to run set-window-point on - the situation I have given is that of a buried buffer that is brought to visibility via switch-to-buffer (C-x b). While running set-window-point afterwards achieves a result, it is hardly in the spirit of answering the question of where Emacs is getting the window point from (given it is not taken from the buffer if goto-char is being honoured). – progman Apr 04 '21 at 20:45