Subject: Re: bug#23749: 24.3.50; can't make the cursor to the end of a buffer when that buffer is visible

From: John Mastro <[email-masked]>
Date: Sat, 11 Jun 2016 19:24:09 -0700

Samuel Lê <[email-masked]> wrote:
> - run "emacs -Q" from command line.
> - split emacs screen (C-x 2)
> - on one side open an elisp code (say test.el)
> - on the other side put the *scratch* buffer
> - in test.el, enter (with-current-buffer "*scratch* (insert "test\n")
> (goto-char (point-max)))
> - then press C-x e to evaluate that command
> - it inserts "test" in *scratch* buffer but doesnt go at the end of the
> - buffer.
>
> It works fine when buffer *scratch* is not visible.

I confirmed that the behavior is the same in Emacs 25.0.94, but I
suspect this is not a bug because there's also a concept of "window
point" (see `window-point').

Adding `with-selected-window' will cause point to move as you expected:

(with-current-buffer "*scratch*"
  (with-selected-window (get-buffer-window (current-buffer))
    (insert "test\n")
    (goto-char (point-max))))

However, if the buffer isn't shown in a window, `get-buffer-window' will
return nil and `with-selected-window' will thus signal an error. So the
right fix really depends on the context and ultimate goal.

        John