unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* with-current-buffer mystery
@ 2004-11-25  7:57 Ryan Yeske
  2004-11-25 13:02 ` Johan Bockgård
  0 siblings, 1 reply; 3+ messages in thread
From: Ryan Yeske @ 2004-11-25  7:57 UTC (permalink / raw)


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

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2004-11-26  4:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2004-11-25  7:57 with-current-buffer mystery Ryan Yeske
2004-11-25 13:02 ` Johan Bockgård
2004-11-26  4:38   ` Ryan Yeske

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).