unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Re: with-current-buffer
  2005-06-27  6:28   ` Miles Bader
@ 2005-06-28  0:04     ` Juri Linkov
  2005-06-28 17:28       ` with-current-buffer Kevin Rodgers
  2005-06-28 18:47       ` with-current-buffer Richard M. Stallman
  0 siblings, 2 replies; 11+ messages in thread
From: Juri Linkov @ 2005-06-28  0:04 UTC (permalink / raw)
  Cc: emacs-devel, rms, miles

>>   (with-current-buffer "c"
>>     (pop-to-buffer "b"))
>>   (message "%s" (buffer-name (current-buffer))))
>> 
>> At the end, it print "a" as the current buffer name, but actually
>> this command makes the buffer "b" current.  Shouldn't it print "b"?
>
> I think this isn't a bug -- the `with-current-buffer' form of course
> restores the current buffer to be "a" after the wrapped code is
> finished; why would you expect differently?
>
> As I understand it, what `pop-to-buffer' does is set the window-buffer
> of the selected window, in _addition_ to setting the current buffer,
> and the main emacs interaction loop always restores the current buffer
> to be the window-buffer of the selected window when you look for the
> next command.
>
> So in your example, `pop-to-buffer' sets the current buffer and the
> window-buffer-of-selected-widnow to be "b", then the
> `with-current-buffer' form (correctly) restores the current buffer to
> be "a" again, then you print your message [which says "current buffer
> is a"], then the interaction loop goes to read a command, which sets
> the current buffer based on the window state -- which is "b", because
> of pop-to-buffer's effect.

What causes trouble here is the fact that Emacs changes the current
buffer behind the scenes (somewhere in the main loop), so at the end
of the function it is not known what the buffer will be current after
the function finishes.  But sometimes it is desirable to perform some
actions on that buffer (e.g. run hooks).

Maybe with-current-buffer should keep track of switching buffers
inside with-current-buffer, so it could anticipate what a buffer will
be set as current and don't restore the old buffer.

Or maybe `pop-to-buffer inside with-current-buffer' is not a suitable
programming construct (I mean mostly for the `next-error' command
where this problem occurred) and it's better to replace it with more
explicit buffer switching that will restore the old buffer only when
there were no `pop-to-buffer' calls.

-- 
Juri Linkov
http://www.jurta.org/emacs/

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

* Re: with-current-buffer
  2005-06-28  0:04     ` with-current-buffer Juri Linkov
@ 2005-06-28 17:28       ` Kevin Rodgers
  2005-06-29  3:45         ` with-current-buffer Stefan Monnier
  2005-06-28 18:47       ` with-current-buffer Richard M. Stallman
  1 sibling, 1 reply; 11+ messages in thread
From: Kevin Rodgers @ 2005-06-28 17:28 UTC (permalink / raw)


Juri Linkov wrote:
 > What causes trouble here is the fact that Emacs changes the current
 > buffer behind the scenes (somewhere in the main loop), so at the end
 > of the function it is not known what the buffer will be current after
 > the function finishes.  But sometimes it is desirable to perform some
 > actions on that buffer (e.g. run hooks).

But you also changed the selected window.  That is the cause of the
trouble.

 > Maybe with-current-buffer should keep track of switching buffers
 > inside with-current-buffer, so it could anticipate what a buffer will
 > be set as current and don't restore the old buffer.
 >
 > Or maybe `pop-to-buffer inside with-current-buffer' is not a suitable
 > programming construct (I mean mostly for the `next-error' command
 > where this problem occurred) and it's better to replace it with more
 > explicit buffer switching that will restore the old buffer only when
 > there were no `pop-to-buffer' calls.

In the old days we'd use save-window-excursion; now there is
save-selected-window.

Should there be a with-selected-window macro, analogous to
with-current-buffer?

-- 
Kevin Rodgers

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

* Re: with-current-buffer
  2005-06-28  0:04     ` with-current-buffer Juri Linkov
  2005-06-28 17:28       ` with-current-buffer Kevin Rodgers
@ 2005-06-28 18:47       ` Richard M. Stallman
  1 sibling, 0 replies; 11+ messages in thread
From: Richard M. Stallman @ 2005-06-28 18:47 UTC (permalink / raw)
  Cc: snogglethorpe, emacs-devel, miles

    What causes trouble here is the fact that Emacs changes the current
    buffer behind the scenes (somewhere in the main loop), so at the end
    of the function it is not known what the buffer will be current after
    the function finishes.  But sometimes it is desirable to perform some
    actions on that buffer (e.g. run hooks).

I don't understand what you mean by "trouble"--the scenario is not
clearly specified.  It is a basic feature that the main loop sets the
current buffer.  It is a basic feature that with-current-buffer
preserves the current buffer.

    Maybe with-current-buffer should keep track of switching buffers
    inside with-current-buffer, so it could anticipate what a buffer will
    be set as current and don't restore the old buffer.

Certainly not.  with-current-buffer must do its job reliably.

    Or maybe `pop-to-buffer inside with-current-buffer' is not a suitable
    programming construct (I mean mostly for the `next-error' command
    where this problem occurred) 

There is nothing wrong with it in principle, no reason why it should
in general be avoided.  It might not be the right thing for the
specific place you have in mind.

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

* Re: with-current-buffer
  2005-06-28 17:28       ` with-current-buffer Kevin Rodgers
@ 2005-06-29  3:45         ` Stefan Monnier
  2005-06-29 17:45           ` with-current-buffer Kevin Rodgers
  0 siblings, 1 reply; 11+ messages in thread
From: Stefan Monnier @ 2005-06-29  3:45 UTC (permalink / raw)
  Cc: emacs-devel

> Should there be a with-selected-window macro, analogous to
> with-current-buffer?

Ever tried C-h f ?


        Stefan

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

* Re: with-current-buffer
  2005-06-29  3:45         ` with-current-buffer Stefan Monnier
@ 2005-06-29 17:45           ` Kevin Rodgers
  0 siblings, 0 replies; 11+ messages in thread
From: Kevin Rodgers @ 2005-06-29 17:45 UTC (permalink / raw)


Stefan Monnier wrote:
>>Should there be a with-selected-window macro, analogous to
>>with-current-buffer?
> 
> Ever tried C-h f ?

Not in Emacs 22.

-- 
Kevin

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

* with-current-buffer
@ 2005-09-11 15:00 Emilio Lopes
  2005-09-11 16:52 ` with-current-buffer Stefan Monnier
  2005-09-11 20:23 ` with-current-buffer Luc Teirlinck
  0 siblings, 2 replies; 11+ messages in thread
From: Emilio Lopes @ 2005-09-11 15:00 UTC (permalink / raw)


Start Emacs (latest CVS on GNU/Linux) with

   ./src/emacs --no-init-file --no-site-file

Display a buffer, let's say "FOO", with some content.  Move point to
its beginning.

Insert the following code in the "*scratch*" buffer:

   (with-current-buffer "FOO"
     (goto-char (point-max))
     (insert "bar"))

Now execute the code above in these two different situations:

   1- The buffer "FOO" is visible along with the "*scratch*" buffer.

   2- The buffer "FOO" is not visible.

In both cases the insertion is done at the right spot, but in case 1
point in buffer "FOO" is restored after the code is executed.

Is that intended behavior?

I expected the point to be moved permanently in both cases.

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

* Re: with-current-buffer
  2005-09-11 15:00 with-current-buffer Emilio Lopes
@ 2005-09-11 16:52 ` Stefan Monnier
  2005-09-11 19:23   ` with-current-buffer Emilio Lopes
  2005-09-11 20:23 ` with-current-buffer Luc Teirlinck
  1 sibling, 1 reply; 11+ messages in thread
From: Stefan Monnier @ 2005-09-11 16:52 UTC (permalink / raw)
  Cc: emacs-devel

> In both cases the insertion is done at the right spot, but in case 1
> point in buffer "FOO" is restored after the code is executed.

> Is that intended behavior?

> I expected point to be moved permanently in both cases.

Which point?


        Stefan

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

* Re: with-current-buffer
  2005-09-11 16:52 ` with-current-buffer Stefan Monnier
@ 2005-09-11 19:23   ` Emilio Lopes
  2005-09-11 21:15     ` with-current-buffer Stefan Monnier
  0 siblings, 1 reply; 11+ messages in thread
From: Emilio Lopes @ 2005-09-11 19:23 UTC (permalink / raw)


Stefan Monnier writes:

>> In both cases the insertion is done at the right spot, but in case
>> 1 point in buffer "FOO" is restored after the code is executed.

>> Is that intended behavior?

>> I expected point to be moved permanently in both cases.

> Which point?

The point in buffer "FOO", of course.  I expected the point to stay at
point max after `with-current-buffer' exits.  In the case 1 explained
in the original message the code behaves as if it were wrapped with
`save-excursion'.

Can you reproduce it?

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

* Re: with-current-buffer
  2005-09-11 15:00 with-current-buffer Emilio Lopes
  2005-09-11 16:52 ` with-current-buffer Stefan Monnier
@ 2005-09-11 20:23 ` Luc Teirlinck
  1 sibling, 0 replies; 11+ messages in thread
From: Luc Teirlinck @ 2005-09-11 20:23 UTC (permalink / raw)
  Cc: emacs-devel

Emilio Lopes wrote:

   Display a buffer, let's say "FOO", with some content.  Move point to
   its beginning.

   Insert the following code in the "*scratch*" buffer:

      (with-current-buffer "FOO"
	(goto-char (point-max))
	(insert "bar"))

   Now execute the code above in these two different situations:

      1- The buffer "FOO" is visible along with the "*scratch*" buffer.

      2- The buffer "FOO" is not visible.

   In both cases the insertion is done at the right spot, but in case 1
   point in buffer "FOO" is restored after the code is executed.

Point in the buffer is not restored in either case.  Just execute:
(with-current-buffer "FOO" (point)) immediately afterwards.  You will
see that point _in the buffer_ really is where you expect it to be.

But each window that displays the buffer has its own value of point.
That value would not be restored either, if you would change it.  But
your code never changes it.  You should use set-window-point, if that
is what you want to do,

   Is that intended behavior?

I definitely believe that it is intended.

Sincerely,

Luc.

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

* Re: with-current-buffer
  2005-09-11 19:23   ` with-current-buffer Emilio Lopes
@ 2005-09-11 21:15     ` Stefan Monnier
  2005-09-12 17:15       ` with-current-buffer Emilio Lopes
  0 siblings, 1 reply; 11+ messages in thread
From: Stefan Monnier @ 2005-09-11 21:15 UTC (permalink / raw)
  Cc: emacs-devel

>>> In both cases the insertion is done at the right spot, but in case
>>> 1 point in buffer "FOO" is restored after the code is executed.

>>> Is that intended behavior?

>>> I expected point to be moved permanently in both cases.

>> Which point?

> The point in buffer "FOO", of course.

If foo is displayed in a window, it has 2 points: its own and the one of
the window.

> I expected the point to stay at point max after `with-current-buffer' exits.

It does stay there.  Try

   (progn
     (with-current-buffer "FOO"
       (goto-char (point-max))
       (insert "bar"))
     (with-current-buffer "FOO"
       (point)))

> In the case 1 explained in the original message the code behaves as if it
> were wrapped with `save-excursion'.

No, it's just that the window's point gets copied to the buffer's point,
probably during redisplay.

> Can you reproduce it?

Of course,


        Stefan

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

* Re: with-current-buffer
  2005-09-11 21:15     ` with-current-buffer Stefan Monnier
@ 2005-09-12 17:15       ` Emilio Lopes
  0 siblings, 0 replies; 11+ messages in thread
From: Emilio Lopes @ 2005-09-12 17:15 UTC (permalink / raw)


Stefan Monnier writes:

> If foo is displayed in a window, it has 2 points: its own and the
> one of the window.

My first thought was to ask if this is a new feature.  It isn't.

It's just plain embarrassing that one can work more than ten years
with an editor without ever noting something as fundamental as this.

Thanks to you and Luc for enlightening me.

-- 
Emílio C. Lopes
Munich, Germany

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

end of thread, other threads:[~2005-09-12 17:15 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-09-11 15:00 with-current-buffer Emilio Lopes
2005-09-11 16:52 ` with-current-buffer Stefan Monnier
2005-09-11 19:23   ` with-current-buffer Emilio Lopes
2005-09-11 21:15     ` with-current-buffer Stefan Monnier
2005-09-12 17:15       ` with-current-buffer Emilio Lopes
2005-09-11 20:23 ` with-current-buffer Luc Teirlinck
  -- strict thread matches above, loose matches on Subject: below --
2005-06-24  5:36 fit-window-to-buffer Richard M. Stallman
2005-06-26 23:33 ` with-current-buffer (was: fit-window-to-buffer) Juri Linkov
2005-06-27  6:28   ` Miles Bader
2005-06-28  0:04     ` with-current-buffer Juri Linkov
2005-06-28 17:28       ` with-current-buffer Kevin Rodgers
2005-06-29  3:45         ` with-current-buffer Stefan Monnier
2005-06-29 17:45           ` with-current-buffer Kevin Rodgers
2005-06-28 18:47       ` with-current-buffer Richard M. Stallman

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

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).