The append-to-buffer function's definition ends with:

(dolist (window windows)
  (when (= (window-point window) point)
    (set-window-point window (point))))))))

What is the purpose of this? Experimenting, I commented out this code and re-ran, but could detect no difference in functionality?

Thanks for your responses, I'm responding to Drew's response. Perhaps I did something wrong in my experiment. However, in my experiment, I split the buffer into two windows, with identical window points, commented out the pertinent code, and executed the (altered) append-to-buffer. I observed that the other window's point "seemed" to have been adjusted correctly, despite the fact that I had commented out the code.

I specified point to emphasize that I was observing the 2nd window's point, not window-point, which I have no way of eyeballing. Can anyone repeat my experiment to see if (in emacs 24.4) I've interpreted the actions correctly. If so, that would suggest that emacs 24.4 was enhanced so that the code become redundant. If not, then I did something wrong in my experiment. This will help me, because I will be writing my own personal code, and I need to know what is necessary.... Thanks

  • My guess is that if there are many windows showing the same buffer, this code ensures that the point in them doesn't move. Commented Apr 24, 2015 at 21:22
  • What's the purpose of what? The code you posted? (no purpose) Or the question posed in the title?
    – Drew
    Commented Apr 24, 2015 at 21:28
  • @Drew; since @Steve didn't write the title (@rekado did), I assume he's questioning the purpose of the code. Commented Apr 24, 2015 at 22:08
  • OK, I've edited the title. @rekado's edit changed the question inappropriately, and should have been rejected as such.
    – Drew
    Commented Apr 25, 2015 at 6:12
  • 1
    I had to guess. The title was "What's your emacs question? Be specific"
    – user2005
    Commented Apr 25, 2015 at 6:50

2 Answers 2


If your question is what is the use of set-window-point then the answer is that it sets the window-point value for the given window's buffer.

See the Elisp manual, node Window Point. It starts by telling you this, which should already answer your question:

Each window has its own value of point (*note Point::), independent of the value of point in other windows displaying the same buffer. This makes it useful to have multiple windows showing one buffer.

set-window-point gives you a way to set the point value for a given window.

Wrt the question of why the code you posted is used in append-to-buffer -

Here is the full code:

(defun append-to-buffer (buffer start end)
  "Append to specified buffer the text of the region.
It is inserted into that buffer before its point.

When calling from a program, give three arguments:
BUFFER (or buffer name), START and END.
START and END specify the portion of the current buffer to be copied."
   (list (read-buffer "Append to buffer: " (other-buffer (current-buffer) t))
     (region-beginning) (region-end)))
  (let* ((oldbuf (current-buffer))
         (append-to (get-buffer-create buffer))
         (windows (get-buffer-window-list append-to t t))
      (with-current-buffer append-to
        (setq point (point))
        (insert-buffer-substring oldbuf start end)
        (dolist (window windows)
          (when (= (window-point window) point)
            (set-window-point window (point))))))))

The part you quoted comes right after (a) saving the current value of point to a variable named point and then (b) inserting a string before point.

That last part is significant: point moves forward by the length of the inserted string. The doc string of insert-buffer-substring does not say this explicitly - but it should. (The doc string of function insert does say this explicitly. I just filed Emacs bug #20421, to ask that this doc string be improved this way.)

This means that after the string is inserted the saved position (in variable point) typically is no longer equal to (point).

What the dolist loop does is to check existing windows for the same buffer, to see if any of them still have their window-point pointing to the saved position, point, and if so, updating those window points to the new position of (point), that is, to where the cursor is now.

In sum, it resyncs all windows showing the same buffer, with respect to their cursor positions (their window points).

(More typically, the variable used to save the original value of (point) would be called opoint or orig-point or some such, and not just point. That would make the code a bit clearer - it is too easy to misread the difference between point (a variable) and (point) (the current point value).


Emacs says the following about this function:

set-window-point is a built-in function in `C source code'.

(set-window-point WINDOW POS)

Make point value in WINDOW be at position POS in WINDOW's buffer. WINDOW must be a live window and defaults to the selected one. Return POS.

To ask Emacs type C-h f and then input the function name.

  • Thanks for your responses, I'm responding to Drew's response.
    – zugzwang
    Commented Apr 25, 2015 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.