all messages for Emacs-related lists mirrored at yhetil.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; 18+ 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] 18+ 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; 18+ 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] 18+ 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; 18+ 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] 18+ 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; 18+ 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] 18+ 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; 18+ 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] 18+ 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; 18+ 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] 18+ 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; 18+ 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] 18+ 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; 18+ 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] 18+ 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; 18+ 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] 18+ 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; 18+ 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] 18+ 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; 18+ 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] 18+ messages in thread

* with-current-buffer
@ 2019-11-11  8:31 Andreas Röhler
  2019-11-11  9:04 ` with-current-buffer tomas
  2019-11-11 11:16 ` with-current-buffer Joost Kremers
  0 siblings, 2 replies; 18+ messages in thread
From: Andreas Röhler @ 2019-11-11  8:31 UTC (permalink / raw)
  To: help-gnu-emacs

Hi,

docstring of "with-current-buffer" says:

"Execute the forms in BODY with BUFFER-OR-NAME temporarily current."

However, seems the old buffer isn't stored, buffer-or-name specified 
here remain current.

Also don't see inside the macro which way the old buffer should be restored:

(defmacro with-current-buffer (buffer-or-name &rest body)
   "Execute the forms in BODY with BUFFER-OR-NAME temporarily current.
BUFFER-OR-NAME must be a buffer or the name of an existing buffer.
The value returned is the value of the last form in BODY.  See
also `with-temp-buffer'."
   (declare (indent 1) (debug t))
   `(save-current-buffer
      (set-buffer ,buffer-or-name)
      ,@body))


A bug?

Thanks,

Andreas,

GNU Emacs 26.3 (build 1, i686-pc-linux-gnu, GTK+ Version 3.14.5) of 
2019-09-04




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

* Re: with-current-buffer
  2019-11-11  8:31 with-current-buffer Andreas Röhler
@ 2019-11-11  9:04 ` tomas
  2019-11-11 10:58   ` with-current-buffer Andreas Röhler
  2019-11-11 11:16 ` with-current-buffer Joost Kremers
  1 sibling, 1 reply; 18+ messages in thread
From: tomas @ 2019-11-11  9:04 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 449 bytes --]

On Mon, Nov 11, 2019 at 09:31:08AM +0100, Andreas Röhler wrote:
> Hi,
> 
> docstring of "with-current-buffer" says:
> 
> "Execute the forms in BODY with BUFFER-OR-NAME temporarily current."
> 
> However, seems the old buffer isn't stored, buffer-or-name specified
> here remain current.
> 
> Also don't see inside the macro which way the old buffer should be restored:

This is the job of save-current-buffer, no?

Cheers
-- tomás

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

* Re: with-current-buffer
  2019-11-11  9:04 ` with-current-buffer tomas
@ 2019-11-11 10:58   ` Andreas Röhler
  2019-11-11 11:09     ` with-current-buffer tomas
  2019-11-11 11:17     ` with-current-buffer Joost Kremers
  0 siblings, 2 replies; 18+ messages in thread
From: Andreas Röhler @ 2019-11-11 10:58 UTC (permalink / raw)
  To: help-gnu-emacs


On 11.11.19 10:04, tomas@tuxteam.de wrote:
> On Mon, Nov 11, 2019 at 09:31:08AM +0100, Andreas Röhler wrote:
>> Hi,
>>
>> docstring of "with-current-buffer" says:
>>
>> "Execute the forms in BODY with BUFFER-OR-NAME 	 current."
>>
>> However, seems the old buffer isn't stored, buffer-or-name specified
>> here remain current.
>>
>> Also don't see inside the macro which way the old buffer should be restored:
> This is the job of save-current-buffer, no?
>
> Cheers
> -- tomás


Hi Tomás,

thanks. So I have to write something like

(save-current-buffer
         (with-current-buffer buffer


Expected a behavior like with-temp-buffer.

As the wording "temporarily" seems to indicate.

Cheers,

Andreas




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

* Re: with-current-buffer
  2019-11-11 10:58   ` with-current-buffer Andreas Röhler
@ 2019-11-11 11:09     ` tomas
  2019-11-11 11:17     ` with-current-buffer Joost Kremers
  1 sibling, 0 replies; 18+ messages in thread
From: tomas @ 2019-11-11 11:09 UTC (permalink / raw)
  To: Andreas Röhler; +Cc: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 513 bytes --]

On Mon, Nov 11, 2019 at 11:58:43AM +0100, Andreas Röhler wrote:

[...]

> thanks. So I have to write something like
> 
> (save-current-buffer
>         (with-current-buffer buffer

Hm. I'm far from an expert, but I think

  (save-current-buffer
    (with-current-buffer buffer
      ...))

should be equivalent to

  (with-current-buffer buffer
    ...)

i.e. you can just drop the "outer" save-current-buffer. Still, if
I were you I'd wait for more expert advise :-)

Cheers
-- t

[-- Attachment #2: Digital signature --]
[-- Type: application/pgp-signature, Size: 198 bytes --]

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

* Re: with-current-buffer
  2019-11-11  8:31 with-current-buffer Andreas Röhler
  2019-11-11  9:04 ` with-current-buffer tomas
@ 2019-11-11 11:16 ` Joost Kremers
  2019-11-11 11:31   ` with-current-buffer Andreas Röhler
  1 sibling, 1 reply; 18+ messages in thread
From: Joost Kremers @ 2019-11-11 11:16 UTC (permalink / raw)
  To: help-gnu-emacs


On Mon, Nov 11 2019, Andreas Röhler wrote:
> docstring of "with-current-buffer" says:
>
> "Execute the forms in BODY with BUFFER-OR-NAME temporarily 
> current."
>
> However, seems the old buffer isn't stored, buffer-or-name 
> specified here remain
> current.

Could you be more specific? `save-current-buffer` works fine for 
me.

> Also don't see inside the macro which way the old buffer should 
> be restored:

That's done by `save-current-buffer`.


-- 
Joost Kremers
Life has its moments



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

* Re: with-current-buffer
  2019-11-11 10:58   ` with-current-buffer Andreas Röhler
  2019-11-11 11:09     ` with-current-buffer tomas
@ 2019-11-11 11:17     ` Joost Kremers
  1 sibling, 0 replies; 18+ messages in thread
From: Joost Kremers @ 2019-11-11 11:17 UTC (permalink / raw)
  To: help-gnu-emacs


On Mon, Nov 11 2019, Andreas Röhler wrote:
> thanks. So I have to write something like
>
> (save-current-buffer
>         (with-current-buffer buffer

Nope, there's a `save-current-buffer` in the definition of 
`with-current-buffer`, so this would be superfluous.

-- 
Joost Kremers
Life has its moments



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

* Re: with-current-buffer
  2019-11-11 11:16 ` with-current-buffer Joost Kremers
@ 2019-11-11 11:31   ` Andreas Röhler
  0 siblings, 0 replies; 18+ messages in thread
From: Andreas Röhler @ 2019-11-11 11:31 UTC (permalink / raw)
  To: help-gnu-emacs


On 11.11.19 12:16, Joost Kremers wrote:
>
> On Mon, Nov 11 2019, Andreas Röhler wrote:
>> docstring of "with-current-buffer" says:
>>
>> "Execute the forms in BODY with BUFFER-OR-NAME temporarily current."
>>
>> However, seems the old buffer isn't stored, buffer-or-name specified 
>> here remain
>> current.
>
> Could you be more specific? `save-current-buffer` works fine for me.
>
>> Also don't see inside the macro which way the old buffer should be 
>> restored:
>
> That's done by `save-current-buffer`.
>
>

Turns out there an error in code, which set the wrong buffer   - 
"with-current-buffer" works fine and temporarily as expected.

Thanks all,

Andreas




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

end of thread, other threads:[~2019-11-11 11:31 UTC | newest]

Thread overview: 18+ 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 --
2019-11-11  8:31 with-current-buffer Andreas Röhler
2019-11-11  9:04 ` with-current-buffer tomas
2019-11-11 10:58   ` with-current-buffer Andreas Röhler
2019-11-11 11:09     ` with-current-buffer tomas
2019-11-11 11:17     ` with-current-buffer Joost Kremers
2019-11-11 11:16 ` with-current-buffer Joost Kremers
2019-11-11 11:31   ` with-current-buffer Andreas Röhler
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 external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.