GNU bug report logs - #13122
transpose-subr defeats save-excursion

Previous Next

Package: emacs;

Reported by: Jyothis V <jyothisv <at> gmail.com>

Date: Sat, 8 Dec 2012 07:16:01 UTC

Severity: minor

Done: Chong Yidong <cyd <at> gnu.org>

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 13122 in the body.
You can then email your comments to 13122 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-gnu-emacs <at> gnu.org:
bug#13122; Package emacs. (Sat, 08 Dec 2012 07:16:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Jyothis V <jyothisv <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sat, 08 Dec 2012 07:16:02 GMT) Full text and rfc822 format available.

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

From: Jyothis V <jyothisv <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Subject: save-excursion not saving point
Date: Sat, 8 Dec 2012 11:50:13 +0530
[Message part 1 (text/plain, inline)]
Hi,

I wrote the following version of transpose-chars which is supposed to
always transform the last two chars without moving the point.
(defun gosmacs-tranpose-chars (n)
  (interactive "p")
  (save-excursion
    (forward-char (- n))
    (transpose-chars 1)))

The trouble is, even though I have used save-excursion, the point does
move 2 characters backwards when n=1! For other values of n, this seems to
be working fine.

Regards,
Jyothis V

In GNU Emacs 24.2.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.6.2)
 of 2012-11-18 on eric
Windowing system distributor `The X.Org Foundation', version 11.0.11300000
Configured using:
 `configure '--prefix=/usr' '--sysconfdir=/etc' '--libexecdir=/usr/lib'
 '--localstatedir=/var' '--with-x-toolkit=gtk3' '--with-xft'
 'CFLAGS=-march=x86-64 -mtune=generic -O2 -pipe -fstack-protector
 --param=ssp-buffer-size=4 -D_FORTIFY_SOURCE=2'
 'LDFLAGS=-Wl,-O1,--sort-common,--as-needed,-z,relro''

Important settings:
  value of $LC_ALL: nil
  value of $LC_COLLATE: nil
  value of $LC_CTYPE: nil
  value of $LC_MESSAGES: nil
  value of $LC_MONETARY: nil
  value of $LC_NUMERIC: nil
  value of $LC_TIME: nil
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: nil
  locale-coding-system: utf-8-unix
  default enable-multibyte-characters: t

Major mode: Lisp Interaction

Minor modes in effect:
  diff-auto-refine-mode: t
  global-auto-complete-mode: t
  auto-complete-mode: t
  icomplete-mode: t
  ido-everywhere: t
  shell-dirtrack-mode: t
  which-function-mode: t
  cua-mode: t
  display-time-mode: t
  electric-pair-mode: t
  show-paren-mode: t
  tooltip-mode: t
  mouse-wheel-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t
[Message part 2 (text/html, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13122; Package emacs. (Sat, 08 Dec 2012 11:22:02 GMT) Full text and rfc822 format available.

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

From: martin rudalics <rudalics <at> gmx.at>
To: Jyothis V <jyothisv <at> gmail.com>
Cc: 13122 <at> debbugs.gnu.org
Subject: Re: bug#13122: save-excursion not saving point
Date: Sat, 08 Dec 2012 12:21:08 +0100
> I wrote the following version of transpose-chars which is supposed to
> always transform the last two chars without moving the point.
> (defun gosmacs-tranpose-chars (n)
>   (interactive "p")
>   (save-excursion
>     (forward-char (- n))
>     (transpose-chars 1)))
>
> The trouble is, even though I have used save-excursion, the point does
> move 2 characters backwards when n=1! For other values of n, this seems to
> be working fine.

This is a limitation (or bug) in the code of `transpose-subr' and
`transpose-subr-1' due to the use of `delete-and-extract-region' (IIRC
transposing mangles `undo' as well.)

martin




Changed bug title to 'transpose-subr defeats save-excursion' from 'save-excursion not saving point' Request was from Glenn Morris <rgm <at> gnu.org> to control <at> debbugs.gnu.org. (Tue, 11 Dec 2012 07:52:03 GMT) Full text and rfc822 format available.

Severity set to 'minor' from 'normal' Request was from Glenn Morris <rgm <at> gnu.org> to control <at> debbugs.gnu.org. (Tue, 11 Dec 2012 07:52:03 GMT) Full text and rfc822 format available.

Reply sent to Chong Yidong <cyd <at> gnu.org>:
You have taken responsibility. (Fri, 21 Dec 2012 05:45:02 GMT) Full text and rfc822 format available.

Notification sent to Jyothis V <jyothisv <at> gmail.com>:
bug acknowledged by developer. (Fri, 21 Dec 2012 05:45:02 GMT) Full text and rfc822 format available.

Message #17 received at 13122-done <at> debbugs.gnu.org (full text, mbox):

From: Chong Yidong <cyd <at> gnu.org>
To: martin rudalics <rudalics <at> gmx.at>
Cc: Jyothis V <jyothisv <at> gmail.com>, 13122-done <at> debbugs.gnu.org
Subject: Re: bug#13122: save-excursion not saving point
Date: Fri, 21 Dec 2012 13:43:55 +0800
martin rudalics <rudalics <at> gmx.at> writes:

> This is a limitation (or bug) in the code of `transpose-subr' and
> `transpose-subr-1' due to the use of `delete-and-extract-region' (IIRC
> transposing mangles `undo' as well.)

I've committed a fix to trunk.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13122; Package emacs. (Mon, 07 Jan 2013 16:53:03 GMT) Full text and rfc822 format available.

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

From: Alexander Kjeldaas <alexander.kjeldaas <at> gmail.com>
To: 13122 <at> debbugs.gnu.org
Subject: sort-regexp-fields also seems to not save point
Date: Mon, 7 Jan 2013 13:32:18 +0100
[Message part 1 (text/plain, inline)]
I think this might be related.  In the following function, point is also
not restored:

(defun my-java-organize-imports2 ()
  "Sorts import statements"
  (interactive)
  (message "Point %d" (point))
  (sit-for 3)
  (save-excursion
    (let (deactivate-mark)
      ;; Remove empty lines between imports
      (replace-regexp "^import \\(.*\\)\n\\(\n\\)*import \\(.*\\)$"
                      "import \\1\nimport \\3"
                      nil (point-min) (point-max))
      ;; Sort imports in file
      (sort-regexp-fields nil "^.*$" "^import .*$" (point-min)
(point-max))))
  (message "Point %d" (point))
  (sit-for 3))

Thanks,
Alexander
[Message part 2 (text/html, inline)]

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Tue, 05 Feb 2013 12:24:03 GMT) Full text and rfc822 format available.

bug unarchived. Request was from Tino Calancha <tino.calancha <at> gmail.com> to control <at> debbugs.gnu.org. (Fri, 26 Apr 2019 19:46:02 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13122; Package emacs. (Fri, 26 Apr 2019 19:55:01 GMT) Full text and rfc822 format available.

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

From: Tino Calancha <tino.calancha <at> gmail.com>
To: 13122 <at> debbugs.gnu.org
Subject: Re: bug#13122: save-excursion not saving point
Date: Sat, 27 Apr 2019 04:53:56 +0900

;; Following expression should eval as non-nil
(let ((pos 3))
  (with-temp-buffer
    (insert "abcdef")
    (goto-char pos)
    (save-excursion (transpose-chars 2))
    (= pos (point))))
=> nil

In GNU Emacs 27.0.50 (build 3, x86_64-pc-linux-gnu, GTK+ Version 3.22.11)
 of 2019-04-23 built on calancha-pc.dy.bbexcite.jp
Repository revision: 981470e3590534a4d2947dfe5626cae832c6502d
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.11902000
System Description: Debian GNU/Linux 9 (stretch)


Jyothis V <jyothisv <at> gmail.com> writes:


> I wrote the following version of transpose-chars which is supposed to
> always transform the last two chars without moving the point.
> (defun gosmacs-tranpose-chars (n)
>   (interactive "p")
>   (save-excursion
>     (forward-char (- n))
>     (transpose-chars 1)))
>
> The trouble is, even though I have used save-excursion, the point does
> move 2 characters backwards when n=1! For other values of n, this seems to be working fine.
>
> Regards,
> Jyothis V
>
> In GNU Emacs 24.2.1 (x86_64-unknown-linux-gnu, GTK+ Version 3.6.2)
>  of 2012-11-18 on eric
> Windowing system distributor `The X.Org Foundation', version 11.0.11300000




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13122; Package emacs. (Sat, 27 Apr 2019 06:58:02 GMT) Full text and rfc822 format available.

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

From: Eli Zaretskii <eliz <at> gnu.org>
To: Tino Calancha <tino.calancha <at> gmail.com>
Cc: 13122 <at> debbugs.gnu.org
Subject: Re: bug#13122: save-excursion not saving point
Date: Sat, 27 Apr 2019 09:57:24 +0300
> From: Tino Calancha <tino.calancha <at> gmail.com>
> Date: Sat, 27 Apr 2019 04:53:56 +0900
> 
> ;; Following expression should eval as non-nil
> (let ((pos 3))
>   (with-temp-buffer
>     (insert "abcdef")
>     (goto-char pos)
>     (save-excursion (transpose-chars 2))
>     (= pos (point))))
> => nil

Why did you think it should evaluate to non-nil?  The ELisp manual has
a warning near the end of description of save-excursion; I think it's
relevant to this use case.





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13122; Package emacs. (Sat, 27 Apr 2019 07:46:02 GMT) Full text and rfc822 format available.

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

From: Tino Calancha <tino.calancha <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 13122 <at> debbugs.gnu.org, Tino Calancha <tino.calancha <at> gmail.com>
Subject: Re: bug#13122: save-excursion not saving point
Date: Sat, 27 Apr 2019 16:44:52 +0900 (JST)

On Sat, 27 Apr 2019, Eli Zaretskii wrote:

>> From: Tino Calancha <tino.calancha <at> gmail.com>
>> Date: Sat, 27 Apr 2019 04:53:56 +0900
>>
>> ;; Following expression should eval as non-nil
>> (let ((pos 3))
>>   (with-temp-buffer
>>     (insert "abcdef")
>>     (goto-char pos)
>>     (save-excursion (transpose-chars 2))
>>     (= pos (point))))
>> => nil

I read that warning; maybe getting `nil' is above is expected; if that's 
the case then we can close the report again.

The reason why I shared the above snippet is because Chong applied a patch
(commit: aa26f345096166bd8c135876dbab9b671ae232e3)
to fix the original recipe:
a call to `traspose-chars' inside a `save-excursion' moving the 
point if the argument is n != 1.

> Why did you think it should evaluate to non-nil?  The ELisp manual has
> a warning near the end of description of save-excursion; I think it's
> relevant to this use case.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13122; Package emacs. (Sat, 27 Apr 2019 10:00:02 GMT) Full text and rfc822 format available.

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

From: Andreas Schwab <schwab <at> linux-m68k.org>
To: Tino Calancha <tino.calancha <at> gmail.com>
Cc: Eli Zaretskii <eliz <at> gnu.org>, 13122 <at> debbugs.gnu.org
Subject: Re: bug#13122: save-excursion not saving point
Date: Sat, 27 Apr 2019 11:59:48 +0200
On Apr 27 2019, Tino Calancha <tino.calancha <at> gmail.com> wrote:

> On Sat, 27 Apr 2019, Eli Zaretskii wrote:
>
>>> From: Tino Calancha <tino.calancha <at> gmail.com>
>>> Date: Sat, 27 Apr 2019 04:53:56 +0900
>>>
>>> ;; Following expression should eval as non-nil
>>> (let ((pos 3))
>>>   (with-temp-buffer
>>>     (insert "abcdef")
>>>     (goto-char pos)
>>>     (save-excursion (transpose-chars 2))
>>>     (= pos (point))))
>>> => nil
>
> I read that warning; maybe getting `nil' is above is expected; if that's
> the case then we can close the report again.

Since the form inside save-excursion can modify the text before point,
there is never a guarantee that the numeric value of point stays the
same.  And if point points inside the modified text, it is generally
unspecified how point moves during the modification.

Andreas.

-- 
Andreas Schwab, schwab <at> linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#13122; Package emacs. (Sat, 04 May 2019 08:40:02 GMT) Full text and rfc822 format available.

Message #39 received at 13122-done <at> debbugs.gnu.org (full text, mbox):

From: Tino Calancha <tino.calancha <at> gmail.com>
To: 13122-done <at> debbugs.gnu.org
Subject: Re: bug#13122: save-excursion not saving point
Date: Sat, 4 May 2019 17:39:11 +0900 (JST)
Saving the point, in the general case, would rely on unspecified 
behaviour; so I close this bug.
>>>> ;; Following expression should eval as non-nil
>>>> (let ((pos 3))
>>>>   (with-temp-buffer
>>>>     (insert "abcdef")
>>>>     (goto-char pos)
>>>>     (save-excursion (transpose-chars 2))
>>>>     (= pos (point))))
>>>> => nil
> Since the form inside save-excursion can modify the text before point,
> there is never a guarantee that the numeric value of point stays the
> same.  And if point points inside the modified text, it is generally
> unspecified how point moves during the modification.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sat, 01 Jun 2019 11:24:06 GMT) Full text and rfc822 format available.

This bug report was last modified 4 years and 323 days ago.

Previous Next


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