help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

with-current-buffer mystery


From: Ryan Yeske
Subject: with-current-buffer mystery
Date: Thu, 25 Nov 2004 07:57:28 GMT
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50

Hello,

I'm writing some code that appends text from a process into a buffer.
I copied the filter code from the info node (elisp)Filter Functions:

     (defun ordinary-insertion-filter (proc string)
       (with-current-buffer (process-buffer proc)
         (let ((moving (= (point) (process-mark proc))))
           (save-excursion
             ;; Insert the text, advancing the process marker.
             (goto-char (process-mark proc))
             (insert string)
             (set-marker (process-mark proc) (point)))
           (if moving (goto-char (process-mark proc))))))

The text below this code fragment says:

        The reason to use `with-current-buffer', rather than using
        `save-excursion' to save and restore the current buffer, is so
        as to preserve the change in point made by the second call to
        `goto-char'.

Right.  So `with-current-buffer' should preserve the current buffer,
but allow changes to the point?

When I eval the following expressions, I found that it seems to depend
on whether or not the buffer is visible.

;; create a dummy buffer with text
(progn
  (pop-to-buffer (get-buffer-create "*dummy*"))
  (erase-buffer)
  (insert "1\n2\n3\n")
  (goto-char (point-min)))

Notice point is at the beginning of buffer.  Eval the next
expression with *dummy* still visible in other-window.

;; goto end of buffer and insert a 4, note where point is
(with-current-buffer (get-buffer-create "*dummy*")
  (goto-char (point-max))
  (insert "4")
  (newline))

The 4 is inserted at the correct position, but point did not
permanently change.  Switch to that buffer and check.

(pop-to-buffer "*dummy*")

So, maybe I'm misunderstanding `with-current-buffer'.

If you repeat the second step, but with *dummy* buried:

(delete-other-windows)

(with-current-buffer "*dummy*"
  (goto-char (point-max))
  (insert "5")
  (newline))

(pop-to-buffer "*dummy*")

The 5 is in the right place, and the point is at the end.

What is going on?

Any help or explanations would be greatly appreciated.

Ryan


reply via email to

[Prev in Thread] Current Thread [Next in Thread]