I have a buffer which is updated periodically using run-at-time
, and in order to maintain the point in each window I use set-window-point
, but I find that in sometimes when something is interrupted and an update happens in between, the window point is not reset. For example, take:
(defun example ()
(interactive)
(let* ((buf (get-buffer-create "*Example*"))
(w-points (mapcar (lambda (win)
`(,win . ,(window-point win)))
(get-buffer-window-list buf))))
(with-current-buffer buf
(let ((buf-point (point)))
(erase-buffer)
(insert "line1\nline2\nline3\nline4\nline5")
(goto-char buf-point)
(mapc (lambda (wp) (set-window-point (car wp) (cdr wp))) w-points)))
(message "Ran update")))
(setq example-timer (run-at-time t 5 #'example))
Now with a configuration of two windows, switch to the example buffer in one of them, and move down a few lines. Switch to the other window, you should see the window point stay the same after every update.
If you then invoke M-x
(though any kind of minibuffer interaction will suffice), wait for an update to occur, and then cancel the invocation using C-g
, then you should see the point in the window displaying the example buffer go back to the start of the window.
Edit: I've created a video here:
https://user-images.githubusercontent.com/17688577/210167335-f7a4d50f-dbaf-4ffc-b1e0-38c5612ed2e3.mp4
What can I do to make this second case not happen so that the window point stays the same regardless?