with-current-buffer does not move point
Asked Answered
T

4

9

In Elisp this seemingly easy peace of code does not actually move the point.

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

AFAIK with-current-buffer should not restore the point in the target buffer. If not, then how do I manipulate point in a buffer?

Tog answered 6/2, 2013 at 3:26 Comment(2)
This is the code that worked - (mapc (lambda (win) (unless (eq (selected-window) win) (with-selected-window win (goto-char (point-min)) (forward-line (1- line))))) (get-buffer-window-list emms-lyrics-buffer nil t))Tog
I recommend dolist instead of mapc (marginally faster, and often indents a bit better).Wesla
W
10

I think you're confusing the buffer's point with the window's point. If you want to move the cursor in some window showing "foo", you need to select that window while you do the goto-char, or else you need to use set-window-point.

In general a buffer has N+1 points (one is its own, and the N are for the N windows that display the buffer).

Wesla answered 6/2, 2013 at 4:10 Comment(0)
P
1

You might have to use switch-to-buffer and then switch back.

Patrimony answered 6/2, 2013 at 4:5 Comment(0)
D
1
(with-selected-window (get-buffer-window (get-buffer-create "foo"))
        (goto-char (point-max))))
Distributary answered 24/11, 2022 at 10:45 Comment(0)
S
0

Try doing:

(set-buffer "foo")
(goto-char (point-max))
Standley answered 6/2, 2013 at 3:30 Comment(1)
That doesn't work either. Btw, foo is a freshly created buffer.Tog

© 2022 - 2024 — McMap. All rights reserved.