GNU bug report logs - #4655
23.1.50; buffer-swap-text oddity

Previous Next

Package: emacs;

Reported by: Markus Rost <rost <at> math.uni-bielefeld.de>

Date: Tue, 6 Oct 2009 18:55:07 UTC

Severity: minor

Tags: patch

Done: npostavs <at> users.sourceforge.net

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 4655 in the body.
You can then email your comments to 4655 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-submit-list <at> lists.donarmstrong.com, Emacs Bugs <bug-gnu-emacs <at> gnu.org>:
bug#4655; Package emacs. (Tue, 06 Oct 2009 18:55:08 GMT) Full text and rfc822 format available.

Acknowledgement sent to Markus Rost <rost <at> math.uni-bielefeld.de>:
New bug report received and forwarded. Copy sent to Emacs Bugs <bug-gnu-emacs <at> gnu.org>. (Tue, 06 Oct 2009 18:55:08 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> emacsbugs.donarmstrong.com (full text, mbox):

From: Markus Rost <rost <at> math.uni-bielefeld.de>
To: emacs-pretest-bug <at> gnu.org
Subject: 23.1.50; buffer-swap-text oddity
Date: Tue,  6 Oct 2009 20:46:04 +0200 (CEST)
If you evaluate the form

(let ((bufA (get-buffer-create "A"))
      (bufB (get-buffer-create "B")))
  (set-buffer bufA)
  (message "buffer-msg-1 %s" (current-buffer))
  (save-excursion
    (buffer-swap-text bufB))
  (message "buffer-msg-2 %s" (current-buffer)))

the *Messages* buffer contains

buffer-msg-1 A
buffer-msg-2 B

With the save-excursion I would have expected the messages

buffer-msg-1 A
buffer-msg-2 A




Information forwarded to bug-submit-list <at> lists.donarmstrong.com, Emacs Bugs <bug-gnu-emacs <at> gnu.org>:
bug#4655; Package emacs. (Wed, 07 Oct 2009 04:10:11 GMT) Full text and rfc822 format available.

Acknowledgement sent to Juanma Barranquero <lekktu <at> gmail.com>:
Extra info received and forwarded to list. Copy sent to Emacs Bugs <bug-gnu-emacs <at> gnu.org>. (Wed, 07 Oct 2009 04:10:11 GMT) Full text and rfc822 format available.

Message #10 received at 4655 <at> emacsbugs.donarmstrong.com (full text, mbox):

From: Juanma Barranquero <lekktu <at> gmail.com>
To: Markus Rost <rost <at> math.uni-bielefeld.de>
Cc: 4655 <at> debbugs.gnu.org
Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity
Date: Wed, 7 Oct 2009 06:01:22 +0200
> (let ((bufA (get-buffer-create "A"))
>      (bufB (get-buffer-create "B")))
>  (set-buffer bufA)
>  (message "buffer-msg-1 %s" (current-buffer))
>  (save-excursion
>    (buffer-swap-text bufB))
>  (message "buffer-msg-2 %s" (current-buffer)))
>
> the *Messages* buffer contains
>
> buffer-msg-1 A
> buffer-msg-2 B
>
> With the save-excursion I would have expected the messages
>
> buffer-msg-1 A
> buffer-msg-2 A

Why "with the save-excursion"? I would expect A/A both with and
without it (and I'm quite surprised to find that it is not so).

Why do you expect buffer-swap-text to switch the current buffer? And,
why it is doing it?

    Juanma



Information forwarded to bug-submit-list <at> lists.donarmstrong.com, Emacs Bugs <bug-gnu-emacs <at> gnu.org>:
bug#4655; Package emacs. (Wed, 07 Oct 2009 05:45:07 GMT) Full text and rfc822 format available.

Acknowledgement sent to Stefan Monnier <monnier <at> iro.umontreal.ca>:
Extra info received and forwarded to list. Copy sent to Emacs Bugs <bug-gnu-emacs <at> gnu.org>. (Wed, 07 Oct 2009 05:45:07 GMT) Full text and rfc822 format available.

Message #15 received at 4655 <at> emacsbugs.donarmstrong.com (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Juanma Barranquero <lekktu <at> gmail.com>
Cc: 4655 <at> debbugs.gnu.org, Markus Rost <rost <at> math.uni-bielefeld.de>
Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity
Date: Wed, 07 Oct 2009 01:41:40 -0400
> Why do you expect buffer-swap-text to switch the current buffer?

I'm not sure he expects it.  And if he does, I can't tell you why, so
I'll pass.

> And, why it is doing it?

I don't think it does when not called within save-excursion.
But when called within the save-excursion, the reason is simple:

1- markers are swapped along with the buffer texts (with a some
   exceptions, mostly window-markers).
2- save-excursion saves the "position and current buffer" as a marker.

Combine 1 and 2 together, and you'll see that save-excursion ends up
changing the current buffer.


        Stefan



Information forwarded to bug-submit-list <at> lists.donarmstrong.com, Emacs Bugs <bug-gnu-emacs <at> gnu.org>:
bug#4655; Package emacs. (Wed, 07 Oct 2009 09:00:04 GMT) Full text and rfc822 format available.

Acknowledgement sent to Markus Rost <rost <at> math.uni-bielefeld.de>:
Extra info received and forwarded to list. Copy sent to Emacs Bugs <bug-gnu-emacs <at> gnu.org>. (Wed, 07 Oct 2009 09:00:04 GMT) Full text and rfc822 format available.

Message #20 received at 4655 <at> emacsbugs.donarmstrong.com (full text, mbox):

From: Markus Rost <rost <at> math.uni-bielefeld.de>
To: Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: lekktu <at> gmail.com, 4655 <at> debbugs.gnu.org
Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity
Date: Wed,  7 Oct 2009 10:53:51 +0200 (CEST)
Juanma wrote:

> I would expect A/A both with and
> without it (and I'm quite surprised to find that it is not so).

Exactly.

> Why do you expect buffer-swap-text to switch the current buffer?

I don't.  See below for the concrete situation which led to my
example.

Stefan wrote:

> 1- markers are swapped along with the buffer texts (with a some
>    exceptions, mostly window-markers).
> 2- save-excursion saves the "position and current buffer" as a marker.

> Combine 1 and 2 together, and you'll see that save-excursion ends up
> changing the current buffer.

That explains what happened, thanks.  But 2- is not obvious from the
documentation of save-excursion.  And the conclusion "save-excursion
ends up changing the current buffer" is very, very surprising.

Since I am not the only one to be surprised, it seems to me that if
save-excursion/buffer-swap-text keeps having this behavior, it should
be documented, perhaps with a warning for lisp programmers.

==========

Here is the concrete scenario which induced my buffer-swap-text
example:  I have set

(defadvice rmail-cease-edit (after update-rmail-summary preactivate)
  "Recompute Summary-line immediately."
  (rmail-summary))

I expected this to behave like hitting 'h' after editing an rmail
buffer.  But I get an error:

rmail-new-summary: No RMAIL buffer found

The reason is that rmail-cease-edit contains the piece

    (save-excursion
      (rmail-show-message)
      (rmail-toggle-header (if pruned 1 0))))

and rmail-show-message has some buffer/text swapping in it.  Moreover,
the swap partner rmail-view-buffer (an odd name by the way) doesn't
know its rmail-buffer (maybe it should?), so calling rmail-summary
from it triggers the error.

==========

On a general note:  Thank you for all the great developments in Emacs
22/23.

Markus



Information forwarded to bug-submit-list <at> lists.donarmstrong.com, Emacs Bugs <bug-gnu-emacs <at> gnu.org>:
bug#4655; Package emacs. (Wed, 07 Oct 2009 10:15:05 GMT) Full text and rfc822 format available.

Acknowledgement sent to Juanma Barranquero <lekktu <at> gmail.com>:
Extra info received and forwarded to list. Copy sent to Emacs Bugs <bug-gnu-emacs <at> gnu.org>. (Wed, 07 Oct 2009 10:15:05 GMT) Full text and rfc822 format available.

Message #25 received at 4655 <at> emacsbugs.donarmstrong.com (full text, mbox):

From: Juanma Barranquero <lekktu <at> gmail.com>
To: Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: 4655 <at> debbugs.gnu.org, Markus Rost <rost <at> math.uni-bielefeld.de>
Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity
Date: Wed, 7 Oct 2009 12:07:47 +0200
On Wed, Oct 7, 2009 at 07:41, Stefan Monnier <monnier <at> iro.umontreal.ca> wrote:

> 2- save-excursion saves the "position and current buffer" as a marker.

> Combine 1 and 2 together, and you'll see that save-excursion ends up
> changing the current buffer.

Oops. Yes, I see.

Well, we could document it, but from my POV it looks more like a bug
than a "feature".

    Juanma



Information forwarded to bug-submit-list <at> lists.donarmstrong.com, Emacs Bugs <bug-gnu-emacs <at> gnu.org>:
bug#4655; Package emacs. (Wed, 07 Oct 2009 14:35:05 GMT) Full text and rfc822 format available.

Acknowledgement sent to Stefan Monnier <monnier <at> iro.umontreal.ca>:
Extra info received and forwarded to list. Copy sent to Emacs Bugs <bug-gnu-emacs <at> gnu.org>. (Wed, 07 Oct 2009 14:35:05 GMT) Full text and rfc822 format available.

Message #30 received at 4655 <at> emacsbugs.donarmstrong.com (full text, mbox):

From: Stefan Monnier <monnier <at> iro.umontreal.ca>
To: Juanma Barranquero <lekktu <at> gmail.com>
Cc: 4655 <at> debbugs.gnu.org, Markus Rost <rost <at> math.uni-bielefeld.de>
Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity
Date: Wed, 07 Oct 2009 10:29:11 -0400
>> 2- save-excursion saves the "position and current buffer" as a marker.
>> Combine 1 and 2 together, and you'll see that save-excursion ends up
>> changing the current buffer.

> Oops. Yes, I see.
> Well, we could document it, but from my POV it looks more like a bug
> than a "feature".

Agreed.  This said, we have a problem.

If we consider the buffer-swap-text as a kind of "two-way copy&paste",
just with a more efficient implementation, then it would be OK for
save-excursion to not preserve the position, i.e. handle save-excursion
markers similarly to window-markers.

I see some problems with it, tho:
1- how to find all the save-excursion markers (we'd probably have to
   walk the specpdl list and compare the restore function with
   save_excursion_restore, which is very ugly).
2- if we reset those marker's position in buffer-swap-text, then
   (save-excursion (buffer-swap-text BUF) (dosomething) (buffer-swap-text BUF))
   would end up moving point to (point-min) for no good reason, which is
   rather inconvenient and might be worse than the OP.
3- we could change save-excursion itself to check the the markers still
   points to the same buffer.  That would solve the OP, but the cost of
   making all save-excursions more expensive (for the sake of a rare
   corner case).
4- should save-excursion care more about preserving the position inside
   the current text, or preserving the current buffer?
5- most uses of save-excursion are wrong, and the OP is among them.

I.e. I'd tend to prefer documenting it (in buffer-swap-text), and remind
people that they should usually use with-current-buffer rather than
save-excursion.


        Stefan "who thinks save-excursion is an ugly beast"



Information forwarded to bug-submit-list <at> lists.donarmstrong.com, Emacs Bugs <bug-gnu-emacs <at> gnu.org>:
bug#4655; Package emacs. (Thu, 08 Oct 2009 16:55:06 GMT) Full text and rfc822 format available.

Acknowledgement sent to Markus Rost <rost <at> math.uni-bielefeld.de>:
Extra info received and forwarded to list. Copy sent to Emacs Bugs <bug-gnu-emacs <at> gnu.org>. (Thu, 08 Oct 2009 16:55:06 GMT) Full text and rfc822 format available.

Message #35 received at 4655 <at> emacsbugs.donarmstrong.com (full text, mbox):

From: Markus Rost <rost <at> math.uni-bielefeld.de>
To: Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: lekktu <at> gmail.com, 4655 <at> debbugs.gnu.org
Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity
Date: Thu,  8 Oct 2009 18:48:39 +0200 (CEST)
A follow up to my previous message:

If you look at the end of rmail-cease-edit:

    (save-excursion
      (rmail-show-message)
      (rmail-toggle-header (if pruned 1 0))))
  (run-hooks 'rmail-mode-hook))

the question arises in which buffer rmail-mode-hook should be run?
One may check with

(add-hook 'rmail-mode-hook
	  (lambda () (message "foo: %s" (current-buffer))))

that currently it runs in rmail-view-buffer, which is perhaps not what
one expects.  Changing to

    (rmail-show-message)
    (save-excursion
      (rmail-toggle-header (if pruned 1 0))))
  (run-hooks 'rmail-mode-hook))

is probably OK.  That would be in line with Stefan's point:

>  5- most uses of save-excursion are wrong,

=======

Here are more issues related to the usage of buffer-swap-text:

I tried to fix

<URL:http://lists.gnu.org/archive/html/bug-gnu-emacs/2009-10/msg00052.html>
<URL:http://lists.gnu.org/archive/html/bug-gnu-emacs/2009-10/msg00054.html>

(I think this bug should be fixed before the next release:  The mbox
files should not be corrupted.)

with the patch below.  See also the thread of

<URL:http://lists.gnu.org/archive/html/emacs-devel/2008-10/msg00468.html>

It seems to work, except that after a save ('s' in Rmail) the
rmail-buffer gets a wrong value of buffer-file-coding-system (to be
seen in the mode line).  Only temporarily, switching back and forth to
another message in Rmail restores the correct value of
buffer-file-coding-system.

After further debugging, it turned out that the problem sits in

;; Used in `write-region-annotate-functions' to write rmail files.
(defun rmail-write-region-annotate (start end)
  (when (and (null start) (rmail-buffers-swapped-p))
    (set-buffer rmail-view-buffer)
    (widen)
    nil))

Namely, when rmail-expunge-and-save calls save-buffer, then, with
rmail-write-region-annotate in write-region-annotate-functions,
save-buffer and basic-save-buffer* write the rmail-view-buffer to the
file (which is correct) but then set the buffer-file-coding-system to
last-coding-system-used in the rmail-buffer (which is not correct).

It seems that generally write-region-annotate-functions should not
change the buffer to be written.  (What if arg START of write-region
is a buffer position?)


===File ~/.emacs.d/rmail/rmail23/bug/rmail-swap.diff========
*** rmail.el	06 Oct 2009 13:20:10 +0200	1.554
--- rmail.el	08 Oct 2009 14:07:01 +0200	
***************
*** 1306,1320 ****
  
  (defun rmail-swap-buffers ()
    "Swap text between current buffer and `rmail-view-buffer'.
! This function preserves the current buffer's modified flag, and also
! sets the current buffer's `buffer-file-coding-system' to that of
! `rmail-view-buffer'."
    (let ((modp (buffer-modified-p))
  	(coding
  	 (with-current-buffer rmail-view-buffer
  	   buffer-file-coding-system)))
      (buffer-swap-text rmail-view-buffer)
      (setq buffer-file-coding-system coding)
      (restore-buffer-modified-p modp)))
  
  (defun rmail-buffers-swapped-p ()
--- 1306,1322 ----
  
  (defun rmail-swap-buffers ()
    "Swap text between current buffer and `rmail-view-buffer'.
! This function preserves the current buffer's modified flag and
! also swaps the value of `buffer-file-coding-system'."
    (let ((modp (buffer-modified-p))
+ 	(coding0 buffer-file-coding-system)
  	(coding
  	 (with-current-buffer rmail-view-buffer
  	   buffer-file-coding-system)))
      (buffer-swap-text rmail-view-buffer)
      (setq buffer-file-coding-system coding)
+     (with-current-buffer rmail-view-buffer
+       (setq buffer-file-coding-system coding0))
      (restore-buffer-modified-p modp)))
  
  (defun rmail-buffers-swapped-p ()
============================================================



Severity set to 'minor' from 'normal' Request was from Glenn Morris <rgm <at> gnu.org> to control <at> debbugs.gnu.org. (Wed, 27 Jan 2010 22:29:02 GMT) Full text and rfc822 format available.

Information forwarded to owner <at> debbugs.gnu.org, bug-gnu-emacs <at> gnu.org:
bug#4655; Package emacs. (Wed, 13 Jul 2011 14:14:04 GMT) Full text and rfc822 format available.

Message #40 received at 4655 <at> debbugs.gnu.org (full text, mbox):

From: Lars Magne Ingebrigtsen <larsi <at> gnus.org>
To: Stefan Monnier <monnier <at> iro.umontreal.ca>
Cc: Markus Rost <rost <at> math.uni-bielefeld.de>,
	Juanma Barranquero <lekktu <at> gmail.com>, 4655 <at> debbugs.gnu.org
Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity
Date: Wed, 13 Jul 2011 16:08:17 +0200
Stefan Monnier <monnier <at> iro.umontreal.ca> writes:

>>> 2- save-excursion saves the "position and current buffer" as a marker.
>>> Combine 1 and 2 together, and you'll see that save-excursion ends up
>>> changing the current buffer.
>
>> Oops. Yes, I see.
>> Well, we could document it, but from my POV it looks more like a bug
>> than a "feature".
>
> Agreed.  This said, we have a problem.

Has this been fixed?  I seem to be unable to reproduce this bug in Emacs
24...

-- 
(domestic pets only, the antidote for overdose, milk.)
  bloggy blog http://lars.ingebrigtsen.no/




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#4655; Package emacs. (Mon, 11 Jul 2016 01:59:01 GMT) Full text and rfc822 format available.

Message #43 received at 4655 <at> debbugs.gnu.org (full text, mbox):

From: npostavs <at> users.sourceforge.net
To: Lars Magne Ingebrigtsen <larsi <at> gnus.org>
Cc: Markus Rost <rost <at> math.uni-bielefeld.de>,
 Juanma Barranquero <lekktu <at> gmail.com>, 4655 <at> debbugs.gnu.org,
 Stefan Monnier <monnier <at> iro.umontreal.ca>
Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity
Date: Sun, 10 Jul 2016 21:58:36 -0400
[Message part 1 (text/plain, inline)]
tags 4655 patch
quit

Lars Magne Ingebrigtsen <larsi <at> gnus.org> writes:

> Stefan Monnier <monnier <at> iro.umontreal.ca> writes:
>
>>>> 2- save-excursion saves the "position and current buffer" as a marker.
>>>> Combine 1 and 2 together, and you'll see that save-excursion ends up
>>>> changing the current buffer.
>>
>>> Oops. Yes, I see.
>>> Well, we could document it, but from my POV it looks more like a bug
>>> than a "feature".
>>
>> Agreed.  This said, we have a problem.
>
> Has this been fixed?  I seem to be unable to reproduce this bug in Emacs
> 24...

Reproduces for me with 23.4 up through 25.0.95.  So here's a patch to
mention it in the manual:

[v1-0001-Document-buffer-swap-text-save-excursion-interact.patch (text/x-diff, inline)]
From 7c79e2f1aef7e59d80d7b46533a93f2f23ad6759 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <npostavs <at> gmail.com>
Date: Sun, 10 Jul 2016 21:52:23 -0400
Subject: [PATCH v1] Document buffer-swap-text+save-excursion interaction

* doc/lispref/buffers.texi (Swapping Text): Add warning about
interaction of `buffer-swap-text' and `save-excursion' (Bug #4655).
---
 doc/lispref/buffers.texi | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/doc/lispref/buffers.texi b/doc/lispref/buffers.texi
index 1f7f263..22c6726 100644
--- a/doc/lispref/buffers.texi
+++ b/doc/lispref/buffers.texi
@@ -1211,6 +1211,11 @@ Swapping Text
 overlays, the text properties, the undo list, the value of the
 @code{enable-multibyte-characters} flag (@pxref{Text Representations,
 enable-multibyte-characters}), etc.
+
+@strong{Warning:} The current buffer will be set to @var{buffer} if
+this function is called from within @code{save-excursion}, since the
+marker used by @code{save-excursion} to save the position and buffer
+will be swapped as well.
 @end defun
 
   If you use @code{buffer-swap-text} on a file-visiting buffer, you
-- 
2.8.0


Added tag(s) patch. Request was from npostavs <at> users.sourceforge.net to control <at> debbugs.gnu.org. (Mon, 11 Jul 2016 01:59:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#4655; Package emacs. (Mon, 11 Jul 2016 14:38:02 GMT) Full text and rfc822 format available.

Message #48 received at 4655 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: npostavs <at> users.sourceforge.net
Cc: rost <at> math.uni-bielefeld.de, lekktu <at> gmail.com, larsi <at> gnus.org,
 4655 <at> debbugs.gnu.org, monnier <at> iro.umontreal.ca
Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity
Date: Mon, 11 Jul 2016 17:37:18 +0300
> From: npostavs <at> users.sourceforge.net
> Date: Sun, 10 Jul 2016 21:58:36 -0400
> Cc: Markus Rost <rost <at> math.uni-bielefeld.de>,
> 	Juanma Barranquero <lekktu <at> gmail.com>, 4655 <at> debbugs.gnu.org,
> 	Stefan Monnier <monnier <at> iro.umontreal.ca>
> 
> --- a/doc/lispref/buffers.texi
> +++ b/doc/lispref/buffers.texi
> @@ -1211,6 +1211,11 @@ Swapping Text
>  overlays, the text properties, the undo list, the value of the
>  @code{enable-multibyte-characters} flag (@pxref{Text Representations,
>  enable-multibyte-characters}), etc.
> +
> +@strong{Warning:} The current buffer will be set to @var{buffer} if
> +this function is called from within @code{save-excursion}, since the
> +marker used by @code{save-excursion} to save the position and buffer
> +will be swapped as well.
>  @end defun

Fine with me, but I think we should also add a single sentence to the
doc string, something like

  Using this function from `save-excursion' might produce surprising
  results, see Info node `(elisp)Swapping Text'.

With that taken care of, please push to emacs-25.

Thanks.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#4655; Package emacs. (Mon, 11 Jul 2016 19:03:01 GMT) Full text and rfc822 format available.

Message #51 received at 4655 <at> debbugs.gnu.org (full text, mbox):

From: Markus Rost <rost <at> math.uni-bielefeld.de>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: lekktu <at> gmail.com, larsi <at> gnus.org, 4655 <at> debbugs.gnu.org,
 monnier <at> iro.umontreal.ca, npostavs <at> users.sourceforge.net
Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity
Date: Mon, 11 Jul 2016 20:50:35 +0200 (CEST)
> please push to emacs-25.

Oh, I am still using

GNU Emacs 22.3.5 (x86_64-unknown-linux-gnu, GTK+ Version 2.24.23) of 2015-11-14 on euclid




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#4655; Package emacs. (Mon, 11 Jul 2016 19:08:01 GMT) Full text and rfc822 format available.

Message #54 received at 4655 <at> debbugs.gnu.org (full text, mbox):

From: Dmitry Gutov <dgutov <at> yandex.ru>
To: Markus Rost <rost <at> math.uni-bielefeld.de>, Eli Zaretskii <eliz <at> gnu.org>
Cc: lekktu <at> gmail.com, larsi <at> gnus.org, 4655 <at> debbugs.gnu.org,
 monnier <at> iro.umontreal.ca, npostavs <at> users.sourceforge.net
Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity
Date: Mon, 11 Jul 2016 22:07:42 +0300
On 07/11/2016 09:50 PM, Markus Rost wrote:
>> please push to emacs-25.
>
> Oh, I am still using
>
> GNU Emacs 22.3.5 (x86_64-unknown-linux-gnu, GTK+ Version 2.24.23) of 2015-11-14 on euclid

Sorry, you'll have to upgrade to Emacs 25 when it comes out, to see this 
fixed. Emacs 24 and earlier are not supported anymore.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#4655; Package emacs. (Tue, 12 Jul 2016 02:00:02 GMT) Full text and rfc822 format available.

Message #57 received at 4655 <at> debbugs.gnu.org (full text, mbox):

From: npostavs <at> users.sourceforge.net
To: Dmitry Gutov <dgutov <at> yandex.ru>
Cc: lekktu <at> gmail.com, 4655 <at> debbugs.gnu.org, monnier <at> iro.umontreal.ca,
 Markus Rost <rost <at> math.uni-bielefeld.de>, Eli Zaretskii <eliz <at> gnu.org>,
 larsi <at> gnus.org
Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity
Date: Mon, 11 Jul 2016 21:58:59 -0400
[Message part 1 (text/plain, inline)]
Dmitry Gutov <dgutov <at> yandex.ru> writes:

> On 07/11/2016 09:50 PM, Markus Rost wrote:
>>> please push to emacs-25.

I'm thinking also to tweak the wording a bit to make it clear that the
buffer change happens at the end of the save-excursion, not when
buffer-swap-text is called.

[v2-0001-Document-buffer-swap-text-save-excursion-interact.patch (text/x-diff, inline)]
From 3c66c6f163e72da7b8f5d40dab7cb2655faddf47 Mon Sep 17 00:00:00 2001
From: Noam Postavsky <npostavs <at> gmail.com>
Date: Sun, 10 Jul 2016 21:52:23 -0400
Subject: [PATCH v2] Document buffer-swap-text+save-excursion interaction

* doc/lispref/buffers.texi (Swapping Text):
* src/buffer.c (Fbuffer_swap_text): Add warning about interaction of
`buffer-swap-text' and `save-excursion' (Bug #4655).
---
 doc/lispref/buffers.texi | 5 +++++
 src/buffer.c             | 4 +++-
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/doc/lispref/buffers.texi b/doc/lispref/buffers.texi
index 1f7f263..e10c2b3 100644
--- a/doc/lispref/buffers.texi
+++ b/doc/lispref/buffers.texi
@@ -1211,6 +1211,11 @@ Swapping Text
 overlays, the text properties, the undo list, the value of the
 @code{enable-multibyte-characters} flag (@pxref{Text Representations,
 enable-multibyte-characters}), etc.
+
+@strong{Warning:} If called from within @code{save-excursion}, this
+function will set the saved buffer to @var{buffer}, since the
+marker used by @code{save-excursion} to save the position and buffer
+will be swapped as well.
 @end defun
 
   If you use @code{buffer-swap-text} on a file-visiting buffer, you
diff --git a/src/buffer.c b/src/buffer.c
index e4269c0..89f4479 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -2231,7 +2231,9 @@ advance_to_char_boundary (ptrdiff_t byte_pos)
 
 DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
        1, 1, 0,
-       doc: /* Swap the text between current buffer and BUFFER.  */)
+       doc: /* Swap the text between current buffer and BUFFER.
+Using this function from `save-excursion' might produce surprising
+results, see Info node `(elisp)Swapping Text'.  */)
   (Lisp_Object buffer)
 {
   struct buffer *other_buffer;
-- 
2.8.0

[Message part 3 (text/plain, inline)]

>>
>> Oh, I am still using
>>
>> GNU Emacs 22.3.5 (x86_64-unknown-linux-gnu, GTK+ Version 2.24.23) of 2015-11-14 on euclid
>
> Sorry, you'll have to upgrade to Emacs 25 when it comes out, to see
> this fixed. Emacs 24 and earlier are not supported anymore.

To be clear, the particular behaviour that prompted this bug report will
not be fixed/changed, I'm just documenting it (obviously there are a lot
of other changes and improvements between 22.3 and 25).

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#4655; Package emacs. (Tue, 12 Jul 2016 05:18:02 GMT) Full text and rfc822 format available.

Message #60 received at 4655 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: npostavs <at> users.sourceforge.net
Cc: lekktu <at> gmail.com, monnier <at> iro.umontreal.ca, dgutov <at> yandex.ru,
 rost <at> math.uni-bielefeld.de, 4655 <at> debbugs.gnu.org, larsi <at> gnus.org
Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity
Date: Tue, 12 Jul 2016 08:16:56 +0300
> From: npostavs <at> users.sourceforge.net
> Cc: Markus Rost <rost <at> math.uni-bielefeld.de>,  Eli Zaretskii <eliz <at> gnu.org>,  4655 <at> debbugs.gnu.org,  lekktu <at> gmail.com,  larsi <at> gnus.org,  monnier <at> iro.umontreal.ca
> Date: Mon, 11 Jul 2016 21:58:59 -0400
> 
> I'm thinking also to tweak the wording a bit to make it clear that the
> buffer change happens at the end of the save-excursion, not when
> buffer-swap-text is called.

Yes, something like "upon return from this form ...".  I think it's a
good idea.

Otherwise the text is good, IMO.

Thanks.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#4655; Package emacs. (Wed, 13 Jul 2016 23:51:01 GMT) Full text and rfc822 format available.

Message #63 received at 4655 <at> debbugs.gnu.org (full text, mbox):

From: npostavs <at> users.sourceforge.net
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: lekktu <at> gmail.com, monnier <at> iro.umontreal.ca, dgutov <at> yandex.ru,
 rost <at> math.uni-bielefeld.de, 4655 <at> debbugs.gnu.org, larsi <at> gnus.org
Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity
Date: Wed, 13 Jul 2016 19:50:49 -0400
[Message part 1 (text/plain, inline)]
Eli Zaretskii <eliz <at> gnu.org> writes:

> Yes, something like "upon return from this form ...".  I think it's a
> good idea.
>
> Otherwise the text is good, IMO.

Okay, here is my final version, I will push tomorrow.

[v3-0001-Document-buffer-swap-text-save-excursion-interact.patch (text/x-diff, inline)]
From f0295dbfceb3264f93d583537ff184b63779adea Mon Sep 17 00:00:00 2001
From: Noam Postavsky <npostavs <at> gmail.com>
Date: Sun, 10 Jul 2016 21:52:23 -0400
Subject: [PATCH v3] Document buffer-swap-text+save-excursion interaction

* doc/lispref/buffers.texi (Swapping Text):
* src/buffer.c (Fbuffer_swap_text): Add warning about interaction of
`buffer-swap-text' and `save-excursion' (Bug #4655).
---
 doc/lispref/buffers.texi | 6 ++++++
 src/buffer.c             | 4 +++-
 2 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/doc/lispref/buffers.texi b/doc/lispref/buffers.texi
index 1f7f263..740d7cf 100644
--- a/doc/lispref/buffers.texi
+++ b/doc/lispref/buffers.texi
@@ -1211,6 +1211,12 @@ Swapping Text
 overlays, the text properties, the undo list, the value of the
 @code{enable-multibyte-characters} flag (@pxref{Text Representations,
 enable-multibyte-characters}), etc.
+
+@strong{Warning:} If this function is called from within a
+@code{save-excursion} form, the current buffer will be set to
+@var{buffer} upon leaving the form, since the marker used by
+@code{save-excursion} to save the position and buffer will be swapped
+as well.
 @end defun
 
   If you use @code{buffer-swap-text} on a file-visiting buffer, you
diff --git a/src/buffer.c b/src/buffer.c
index e4269c0..89f4479 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -2231,7 +2231,9 @@ advance_to_char_boundary (ptrdiff_t byte_pos)
 
 DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
        1, 1, 0,
-       doc: /* Swap the text between current buffer and BUFFER.  */)
+       doc: /* Swap the text between current buffer and BUFFER.
+Using this function from `save-excursion' might produce surprising
+results, see Info node `(elisp)Swapping Text'.  */)
   (Lisp_Object buffer)
 {
   struct buffer *other_buffer;
-- 
2.8.0


Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#4655; Package emacs. (Thu, 14 Jul 2016 15:05:01 GMT) Full text and rfc822 format available.

Message #66 received at 4655 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: npostavs <at> users.sourceforge.net
Cc: lekktu <at> gmail.com, monnier <at> iro.umontreal.ca, dgutov <at> yandex.ru,
 rost <at> math.uni-bielefeld.de, 4655 <at> debbugs.gnu.org, larsi <at> gnus.org
Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity
Date: Thu, 14 Jul 2016 18:03:27 +0300
> From: npostavs <at> users.sourceforge.net
> Cc: 4655 <at> debbugs.gnu.org,  lekktu <at> gmail.com,  monnier <at> iro.umontreal.ca,  dgutov <at> yandex.ru,  rost <at> math.uni-bielefeld.de,  larsi <at> gnus.org
> Date: Wed, 13 Jul 2016 19:50:49 -0400
> 
> Okay, here is my final version, I will push tomorrow.
> 
> >From f0295dbfceb3264f93d583537ff184b63779adea Mon Sep 17 00:00:00 2001
> From: Noam Postavsky <npostavs <at> gmail.com>
> Date: Sun, 10 Jul 2016 21:52:23 -0400
> Subject: [PATCH v3] Document buffer-swap-text+save-excursion interaction
> 
> * doc/lispref/buffers.texi (Swapping Text):
> * src/buffer.c (Fbuffer_swap_text): Add warning about interaction of
> `buffer-swap-text' and `save-excursion' (Bug #4655).
> ---
>  doc/lispref/buffers.texi | 6 ++++++
>  src/buffer.c             | 4 +++-
>  2 files changed, 9 insertions(+), 1 deletion(-)
> 
> diff --git a/doc/lispref/buffers.texi b/doc/lispref/buffers.texi
> index 1f7f263..740d7cf 100644
> --- a/doc/lispref/buffers.texi
> +++ b/doc/lispref/buffers.texi
> @@ -1211,6 +1211,12 @@ Swapping Text
>  overlays, the text properties, the undo list, the value of the
>  @code{enable-multibyte-characters} flag (@pxref{Text Representations,
>  enable-multibyte-characters}), etc.
> +
> +@strong{Warning:} If this function is called from within a
> +@code{save-excursion} form, the current buffer will be set to
> +@var{buffer} upon leaving the form, since the marker used by
> +@code{save-excursion} to save the position and buffer will be swapped
> +as well.
>  @end defun
>  
>    If you use @code{buffer-swap-text} on a file-visiting buffer, you
> diff --git a/src/buffer.c b/src/buffer.c
> index e4269c0..89f4479 100644
> --- a/src/buffer.c
> +++ b/src/buffer.c
> @@ -2231,7 +2231,9 @@ advance_to_char_boundary (ptrdiff_t byte_pos)
>  
>  DEFUN ("buffer-swap-text", Fbuffer_swap_text, Sbuffer_swap_text,
>         1, 1, 0,
> -       doc: /* Swap the text between current buffer and BUFFER.  */)
> +       doc: /* Swap the text between current buffer and BUFFER.
> +Using this function from `save-excursion' might produce surprising
> +results, see Info node `(elisp)Swapping Text'.  */)
>    (Lisp_Object buffer)
>  {
>    struct buffer *other_buffer;
> -- 
> 2.8.0

Thanks, looks good.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#4655; Package emacs. (Fri, 15 Jul 2016 00:06:01 GMT) Full text and rfc822 format available.

Message #69 received at 4655 <at> debbugs.gnu.org (full text, mbox):

From: npostavs <at> users.sourceforge.net
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: lekktu <at> gmail.com, monnier <at> iro.umontreal.ca, dgutov <at> yandex.ru,
 rost <at> math.uni-bielefeld.de, 4655 <at> debbugs.gnu.org, larsi <at> gnus.org
Subject: Re: bug#4655: 23.1.50; buffer-swap-text oddity
Date: Thu, 14 Jul 2016 20:05:28 -0400
close 4655
quit

>> From: npostavs <at> users.sourceforge.net
>> Cc: 4655 <at> debbugs.gnu.org,  lekktu <at> gmail.com,  monnier <at> iro.umontreal.ca,  dgutov <at> yandex.ru,  rost <at> math.uni-bielefeld.de,  larsi <at> gnus.org
>> Date: Wed, 13 Jul 2016 19:50:49 -0400
>> 
>> Okay, here is my final version, I will push tomorrow.

pushed as 9ba51edf





bug closed, send any further explanations to 4655 <at> debbugs.gnu.org and Markus Rost <rost <at> math.uni-bielefeld.de> Request was from npostavs <at> users.sourceforge.net to control <at> debbugs.gnu.org. (Fri, 15 Jul 2016 00:06:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Fri, 12 Aug 2016 11:24:04 GMT) Full text and rfc822 format available.

This bug report was last modified 9 years and 28 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.