unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Using `save-window-excursion' instead of `save-excursion' for `comment-region'?
@ 2013-12-05 10:18 Bastien
  2013-12-05 11:46 ` martin rudalics
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Bastien @ 2013-12-05 10:18 UTC (permalink / raw)
  To: emacs-devel

In Org buffer, you can comment code within source blocks.  This opens
a new buffer, insert the code there, comment it, and insert the buffer
contents back into Org's buffer.

With the current `comment-region' function, point is lost when Org
goes back to the org buffer.  Using `save-window-excursion' instead
of `save-excursion' fixes the problem.

Are you aware of problems that this change may trigger?

I cannot think of any, but I'm always a bit cautious when using
`save-window-excursion'.

Thanks for your feedback,

-- 
 Bastien




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

* Re: Using `save-window-excursion' instead of `save-excursion' for `comment-region'?
  2013-12-05 10:18 Using `save-window-excursion' instead of `save-excursion' for `comment-region'? Bastien
@ 2013-12-05 11:46 ` martin rudalics
  2013-12-05 12:49 ` Andreas Röhler
  2013-12-05 18:29 ` Stefan Monnier
  2 siblings, 0 replies; 9+ messages in thread
From: martin rudalics @ 2013-12-05 11:46 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-devel

 > In Org buffer, you can comment code within source blocks.  This opens
 > a new buffer, insert the code there, comment it, and insert the buffer
 > contents back into Org's buffer.
 >
 > With the current `comment-region' function, point is lost when Org
 > goes back to the org buffer.  Using `save-window-excursion' instead
 > of `save-excursion' fixes the problem.
 >
 > Are you aware of problems that this change may trigger?
 >
 > I cannot think of any, but I'm always a bit cautious when using
 > `save-window-excursion'.

Please be.  `save-window-excursion' should be used iff you really change
the window configuration as, for example, by resizing, deleting or
creating windows, or showing a different buffer in some window.  WOW
everything covered by `window-configuration-change-hook'.  In the case
you describe above you do not show the buffer used for inserting the
code, commenting it, ... in a window IIUC so that requirement is not
satisfied.

Can you explain why `save-excursion' fails?

martin



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

* Re: Using `save-window-excursion' instead of `save-excursion' for `comment-region'?
  2013-12-05 10:18 Using `save-window-excursion' instead of `save-excursion' for `comment-region'? Bastien
  2013-12-05 11:46 ` martin rudalics
@ 2013-12-05 12:49 ` Andreas Röhler
  2013-12-05 18:29 ` Stefan Monnier
  2 siblings, 0 replies; 9+ messages in thread
From: Andreas Röhler @ 2013-12-05 12:49 UTC (permalink / raw)
  To: emacs-devel; +Cc: Bastien

Am 05.12.2013 11:18, schrieb Bastien:
> In Org buffer, you can comment code within source blocks.  This opens
> a new buffer, insert the code there, comment it, and insert the buffer
> contents back into Org's buffer.
>
> With the current `comment-region' function, point is lost when Org
> goes back to the org buffer.  Using `save-window-excursion' instead
> of `save-excursion' fixes the problem.
>
> Are you aware of problems that this change may trigger?
>
> I cannot think of any, but I'm always a bit cautious when using
> `save-window-excursion'.
>
> Thanks for your feedback,
>

Hi Bastien,

last time I've seen in, indirect buffers were used, which would be worth mentioning.

Andreas



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

* Re: Using `save-window-excursion' instead of `save-excursion' for `comment-region'?
  2013-12-05 10:18 Using `save-window-excursion' instead of `save-excursion' for `comment-region'? Bastien
  2013-12-05 11:46 ` martin rudalics
  2013-12-05 12:49 ` Andreas Röhler
@ 2013-12-05 18:29 ` Stefan Monnier
  2013-12-05 18:43   ` Bastien
  2 siblings, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2013-12-05 18:29 UTC (permalink / raw)
  To: Bastien; +Cc: emacs-devel

> In Org buffer, you can comment code within source blocks.  This opens
> a new buffer, insert the code there, comment it, and insert the buffer
> contents back into Org's buffer.

> With the current `comment-region' function, point is lost when Org
> goes back to the org buffer.  Using `save-window-excursion' instead
> of `save-excursion' fixes the problem.

I'm not sure exactly what means "point is lost" in this case, but if
using save-window-excursion solves the problem, it's only by accident.


        Stefan



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

* Re: Using `save-window-excursion' instead of `save-excursion' for `comment-region'?
  2013-12-05 18:29 ` Stefan Monnier
@ 2013-12-05 18:43   ` Bastien
  2013-12-05 19:05     ` Aaron Ecay
  0 siblings, 1 reply; 9+ messages in thread
From: Bastien @ 2013-12-05 18:43 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> In Org buffer, you can comment code within source blocks.  This opens
>> a new buffer, insert the code there, comment it, and insert the buffer
>> contents back into Org's buffer.
>
>> With the current `comment-region' function, point is lost when Org
>> goes back to the org buffer.  Using `save-window-excursion' instead
>> of `save-excursion' fixes the problem.
>
> I'm not sure exactly what means "point is lost" in this case, but if
> using save-window-excursion solves the problem, it's only by
> accident.

This is how it works:

comment-region calls comment-region-function within save-excursion
(assuming there is no window change.)

comment-region-function calls org-babel-do-in-edit-buffer which
inserts the source code in another buffer, then calls back again
comment-region with comment-region-function bound to the correct
mode-dependent function.

The "outward" comment-region does not restore the point position
correctly.

So I'm not sure why save-window-excursion would only works "by
accident" here.  I cannot think of a better fix right now, I'll
continue to travel through the Babel maze.

-- 
 Bastien



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

* Re: Using `save-window-excursion' instead of `save-excursion' for `comment-region'?
  2013-12-05 18:43   ` Bastien
@ 2013-12-05 19:05     ` Aaron Ecay
  2013-12-05 19:45       ` Stefan Monnier
  0 siblings, 1 reply; 9+ messages in thread
From: Aaron Ecay @ 2013-12-05 19:05 UTC (permalink / raw)
  To: Bastien, Stefan Monnier; +Cc: emacs-devel

2013ko abenudak 5an, Bastien-ek idatzi zuen:
> 
> This is how it works:
> 
> comment-region calls comment-region-function within save-excursion
> (assuming there is no window change.)
> 
> comment-region-function calls org-babel-do-in-edit-buffer which
> inserts the source code in another buffer, then calls back again
> comment-region with comment-region-function bound to the correct
> mode-dependent function.
> 
> The "outward" comment-region does not restore the point position
> correctly.
> 
> So I'm not sure why save-window-excursion would only works "by
> accident" here.  I cannot think of a better fix right now, I'll
> continue to travel through the Babel maze.

I think the problem is that org-babel-do-in-edit-buffer deletes the
whole contents of the source code block, and re-inserts it.  This means
that the marker that save-excursion uses (pointing to somewhere in the
deleted and re-inserted span) no longer points to the desired position,
but rather to just before the span.

Org babel has its own point-restoration functionality (which works by
counting lines and columns), but this is executed inside of the scope of
comment-region’s save-excursion, so exiting the latter restores the
point to a bogus position (from the POV of the user).

-- 
Aaron Ecay



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

* Re: Using `save-window-excursion' instead of `save-excursion' for `comment-region'?
  2013-12-05 19:05     ` Aaron Ecay
@ 2013-12-05 19:45       ` Stefan Monnier
  2013-12-06  9:49         ` Bastien
  0 siblings, 1 reply; 9+ messages in thread
From: Stefan Monnier @ 2013-12-05 19:45 UTC (permalink / raw)
  To: Aaron Ecay; +Cc: Bastien, emacs-devel

> I think the problem is that org-babel-do-in-edit-buffer deletes the
> whole contents of the source code block, and re-inserts it.  This means
> that the marker that save-excursion uses (pointing to somewhere in the
> deleted and re-inserted span) no longer points to the desired position,
> but rather to just before the span.

> Org babel has its own point-restoration functionality (which works by
> counting lines and columns), but this is executed inside of the scope of
> comment-region’s save-excursion, so exiting the latter restores the
> point to a bogus position (from the POV of the user).

I think your analysis is exactly right, and the reason why
save-window-excursion doesn't exhibit the problem is because
save-window-excursion does not save-excursion in the current buffer.
I.e. paradoxically it's because it "preserves less" rather than because
it "preserves more".  Replacing the save-excursion with
save-current-buffer would probably work as well.


        Stefan



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

* Re: Using `save-window-excursion' instead of `save-excursion' for `comment-region'?
  2013-12-05 19:45       ` Stefan Monnier
@ 2013-12-06  9:49         ` Bastien
  2013-12-06  9:58           ` Bastien
  0 siblings, 1 reply; 9+ messages in thread
From: Bastien @ 2013-12-06  9:49 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Aaron Ecay, emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

> I think your analysis is exactly right, and the reason why
> save-window-excursion doesn't exhibit the problem is because
> save-window-excursion does not save-excursion in the current buffer.
> I.e. paradoxically it's because it "preserves less" rather than because
> it "preserves more".  Replacing the save-excursion with
> save-current-buffer would probably work as well.

I see, thanks.

I've fixed the problem in Org by creating `org-comment-dwim',
which use comment-dwim when outside a source block, and
`org-babel-do-in-edit-buffer' (directly, not wrapped into
comment-region) when the point is within a buffer.

Aaron, thanks for your input and for testing this heavily.

-- 
 Bastien



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

* Re: Using `save-window-excursion' instead of `save-excursion' for `comment-region'?
  2013-12-06  9:49         ` Bastien
@ 2013-12-06  9:58           ` Bastien
  0 siblings, 0 replies; 9+ messages in thread
From: Bastien @ 2013-12-06  9:58 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Aaron Ecay, emacs-devel

Bastien <bzg@gnu.org> writes:

> I've fixed the problem in Org by creating `org-comment-dwim',
> which use comment-dwim when outside a source block, and
> `org-babel-do-in-edit-buffer' (directly, not wrapped into
> comment-region) when the point is within a buffer.
                                           ^^^^^^^^

"a source block"

of course,

-- 
 Bastien



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

end of thread, other threads:[~2013-12-06  9:58 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-12-05 10:18 Using `save-window-excursion' instead of `save-excursion' for `comment-region'? Bastien
2013-12-05 11:46 ` martin rudalics
2013-12-05 12:49 ` Andreas Röhler
2013-12-05 18:29 ` Stefan Monnier
2013-12-05 18:43   ` Bastien
2013-12-05 19:05     ` Aaron Ecay
2013-12-05 19:45       ` Stefan Monnier
2013-12-06  9:49         ` Bastien
2013-12-06  9:58           ` Bastien

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