NB: The post How to move point to the end of another buffer, using `with-current-buffer` and `goto-char`? presumably asks the same question as this one, but the answers given to it do not readily apply to my case here. E.g. those answers involve inserting text in the other buffer, etc. The question I'm asking here is considerably simpler, and I expect the answer to it should be correspondingly simple as well.
Suppose that
- the Emacs frame is split into two windows, where one of the windows holds a buffer called
*whatever*
and the other one holds the standard*scratch*
buffer; - the
*whatever*
buffer contains several lines of text; - the cursor in the
*whatever*
buffer is at the beginning of that buffer (i.e. at its(point-min)
); - the
*scratch*
buffer is the current one;
If I now evaluate any of the following in the *scratch*
buffer, the position of the cursor in the *whatever*
buffer remains unchanged:
(with-current-buffer "*whatever*"
(goto-char (point-max)))
(with-current-buffer "*whatever*"
(end-of-buffer))
(save-excursion
(set-buffer "*whatever*")
(goto-char (point-max)))
(save-excursion
(set-buffer "*whatever*")
(end-of-buffer))
I figure that, whatever (goto-char (point-max))
or (end-of-buffer)
does to *whatever*
's cursor's position gets subsequently discarded.
How must I modify any of the snippets above so that the
*whatever*
buffer's cursor gets positioned (persistently!) at the end of that buffer.What should I have done to answer this question myself using the Emacs docs? I tried everything I could think of.
After reading Jules Tamagnan's illuminating answer I realized that I need make a further stipulation explicit:
I'm looking for a solution that will work irrespective of whether the buffer in question is visible in a window or not.
After all, if I view buffer B in a window W, move the cursor to a particular location L, then view some other buffer C in the same window W, and finally point W back to buffer B, I expect to see the cursor at the same location L where I left it. IOW, there must be a way for a buffer to remember where its point is, irrespective of whether it is associated with a window or not.
What about the case where the target buffer is currently visible in two or more windows (W1, W2, W3, ...)? There are many reasonable possibilities for this case. The proposed solution could, for example, change the position of the cursor
- in the most recently active window among W1, W2, W3, ...;
- in all the windows W1, W2, W3, ...; or
- etc., etc.
(I don't have a strong opinion on this corner case, since I expect it to be extremely rare in practice.)
window-point
.