GNU bug report logs - #48294
Use 'with-current-buffer' byte-compiler warning seems wrong

Previous Next

Package: emacs;

Reported by: rswgnu <at> gmail.com

Date: Sat, 8 May 2021 20:15:01 UTC

Severity: normal

Tags: notabug

Done: Lars Ingebrigtsen <larsi <at> gnus.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 48294 in the body.
You can then email your comments to 48294 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#48294; Package emacs. (Sat, 08 May 2021 20:15:01 GMT) Full text and rfc822 format available.

Acknowledgement sent to rswgnu <at> gmail.com:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sat, 08 May 2021 20:15:01 GMT) Full text and rfc822 format available.

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

From: Robert Weiner <rsw <at> gnu.org>
To: bug-gnu-emacs <at> gnu.org
Subject: Use 'with-current-buffer' byte-compiler warning seems wrong
Date: Sat, 8 May 2021 16:14:09 -0400
[Message part 1 (text/plain, inline)]
Hi:

I get a lot of these byte-compile warnings in my Elisp code:

hbut.el:683:26:Warning: Use ‘with-current-buffer’ rather than
    save-excursion+set-buffer

but since with-current-buffer does not save the value of point, it is
not a valid substitute for save-excursion and should not be suggested.
Evaluate the two samples below and you will see that they are not
equivalent.  If I am correct, I'd like this suggestion disabled.  Thanks.
 -- rsw

(save-excursion
  (set-buffer (current-buffer))
  (forward-char 20))

(with-current-buffer (current-buffer)
  (forward-char 20))

------


In GNU Emacs 27.1.90 (build 1, x86_64-apple-darwin18.7.0, NS appkit-1671.60
Version 10.14.6 (Build 18G95))
 of 2020-12-18 built on builder10-14.porkrind.org
Windowing system distributor 'Apple', version 10.3.1894
System Description:  Mac OS X 10.15.7

Configured using:
 'configure --with-ns '--enable-locallisppath=/Library/Application
 Support/Emacs/${version}/site-lisp:/Library/Application
 Support/Emacs/site-lisp' --with-modules'

Configured features:
NOTIFY KQUEUE ACL GNUTLS LIBXML2 ZLIB TOOLKIT_SCROLL_BARS NS MODULES
THREADS JSON PDUMPER

Important settings:
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction
[Message part 2 (text/html, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#48294; Package emacs. (Sun, 09 May 2021 08:06:02 GMT) Full text and rfc822 format available.

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

From: Eli Zaretskii <eliz <at> gnu.org>
To: rswgnu <at> gmail.com
Cc: 48294 <at> debbugs.gnu.org
Subject: Re: bug#48294: Use 'with-current-buffer' byte-compiler warning seems
 wrong
Date: Sun, 09 May 2021 11:05:47 +0300
tags 48294 notabug
thanks

> From: Robert Weiner <rsw <at> gnu.org>
> Date: Sat, 8 May 2021 16:14:09 -0400
> 
> I get a lot of these byte-compile warnings in my Elisp code:
> 
> hbut.el:683:26:Warning: Use ‘with-current-buffer’ rather than
>     save-excursion+set-buffer
> 
> but since with-current-buffer does not save the value of point, it is
> not a valid substitute for save-excursion and should not be suggested.
> Evaluate the two samples below and you will see that they are not
> equivalent.  If I am correct, I'd like this suggestion disabled.  Thanks.  -- rsw
> 
> (save-excursion
>   (set-buffer (current-buffer))
>   (forward-char 20))
> 
> (with-current-buffer (current-buffer)
>   (forward-char 20))

The ELisp manual says about this:

     Because ‘save-excursion’ only saves point for the buffer that was
  current at the start of the excursion, any changes made to point in
  other buffers, during the excursion, will remain in effect afterward.
  This frequently leads to unintended consequences, so the byte compiler
  warns if you call ‘set-buffer’ during an excursion:

       Warning: Use ‘with-current-buffer’ rather than
		save-excursion+set-buffer

  To avoid such problems, you should call ‘save-excursion’ only after
  setting the desired current buffer, as in the following example:

       (defun append-string-to-buffer (string buffer)
	 "Append STRING to the end of BUFFER."
	 (with-current-buffer buffer
	   (save-excursion
	     (goto-char (point-max))
	     (insert string))))

I believe this example shows how to solve your problem.




Added tag(s) notabug. Request was from Eli Zaretskii <eliz <at> gnu.org> to control <at> debbugs.gnu.org. (Sun, 09 May 2021 08:06:02 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to 48294 <at> debbugs.gnu.org and rswgnu <at> gmail.com Request was from Lars Ingebrigtsen <larsi <at> gnus.org> to control <at> debbugs.gnu.org. (Sun, 09 May 2021 10:12:01 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#48294; Package emacs. (Sun, 09 May 2021 15:58:02 GMT) Full text and rfc822 format available.

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

From: Robert Weiner <rsw <at> gnu.org>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 48294 <at> debbugs.gnu.org
Subject: Re: bug#48294: Use 'with-current-buffer' byte-compiler warning seems
 wrong
Date: Sun, 9 May 2021 11:57:25 -0400
[Message part 1 (text/plain, inline)]
Hi Eli:

Thanks for the feedback.  Everything you wrote is very clear.  The issue,
however, is that I want to save point in the current buffer prior to
switching context to the 'with-current-buffer' buffer, just as my
(save-excursion (set-buffer ...)) code does.  If I move the save-excursion
into the with-current-buffer body, then it applies to the new buffer not
the original one and if in that same body we change buffers again to the
original and move point, then that movement will stay in place.  So the
question is, what is the appropriate code that the byte-compiler will
accept when you want to save your original place before switching buffers.
Do I have to just do a (let ((opoint (point))) ...) and then restore it?
The issue is that within the with-current-buffer body, there could be a
hard to trace sequence of calls any of which could switch back to the
original buffer and move point.  So how would you protect against that?

Thanks,

Bob


On Sun, May 9, 2021 at 4:05 AM Eli Zaretskii <eliz <at> gnu.org> wrote:

> tags 48294 notabug
> thanks
>
> > From: Robert Weiner <rsw <at> gnu.org>
> > Date: Sat, 8 May 2021 16:14:09 -0400
> >
> > I get a lot of these byte-compile warnings in my Elisp code:
> >
> > hbut.el:683:26:Warning: Use ‘with-current-buffer’ rather than
> >     save-excursion+set-buffer
> >
> > but since with-current-buffer does not save the value of point, it is
> > not a valid substitute for save-excursion and should not be suggested.
> > Evaluate the two samples below and you will see that they are not
> > equivalent.  If I am correct, I'd like this suggestion disabled.
> Thanks.  -- rsw
> >
> > (save-excursion
> >   (set-buffer (current-buffer))
> >   (forward-char 20))
> >
> > (with-current-buffer (current-buffer)
> >   (forward-char 20))
>
> The ELisp manual says about this:
>
>      Because ‘save-excursion’ only saves point for the buffer that was
>   current at the start of the excursion, any changes made to point in
>   other buffers, during the excursion, will remain in effect afterward.
>   This frequently leads to unintended consequences, so the byte compiler
>   warns if you call ‘set-buffer’ during an excursion:
>
>        Warning: Use ‘with-current-buffer’ rather than
>                 save-excursion+set-buffer
>
>   To avoid such problems, you should call ‘save-excursion’ only after
>   setting the desired current buffer, as in the following example:
>
>        (defun append-string-to-buffer (string buffer)
>          "Append STRING to the end of BUFFER."
>          (with-current-buffer buffer
>            (save-excursion
>              (goto-char (point-max))
>              (insert string))))
>
> I believe this example shows how to solve your problem.
>
[Message part 2 (text/html, inline)]

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#48294; Package emacs. (Sun, 09 May 2021 16:12:01 GMT) Full text and rfc822 format available.

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

From: Eli Zaretskii <eliz <at> gnu.org>
To: rswgnu <at> gmail.com
Cc: 48294 <at> debbugs.gnu.org
Subject: Re: bug#48294: Use 'with-current-buffer' byte-compiler warning seems
 wrong
Date: Sun, 09 May 2021 19:11:13 +0300
> From: Robert Weiner <rsw <at> gnu.org>
> Date: Sun, 9 May 2021 11:57:25 -0400
> Cc: 48294 <at> debbugs.gnu.org
> 
> Thanks for the feedback.  Everything you wrote is very clear.  The issue, however, is that I want to save
> point in the current buffer prior to switching context to the 'with-current-buffer' buffer, just as my
> (save-excursion (set-buffer ...)) code does.  If I move the save-excursion into the with-current-buffer body,
> then it applies to the new buffer not the original one and if in that same body we change buffers again to the
> original and move point, then that movement will stay in place.  So the question is, what is the appropriate
> code that the byte-compiler will accept when you want to save your original place before switching buffers. 

I don't understand: with-current-buffer doesn't change point of the
original buffer, so why do you need to save it?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#48294; Package emacs. (Sun, 09 May 2021 18:10:05 GMT) Full text and rfc822 format available.

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

From: Robert Weiner <rswgnu <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 48294 <at> debbugs.gnu.org
Subject: Re: bug#48294: Use 'with-current-buffer' byte-compiler warning seems
 wrong
Date: Sun, 9 May 2021 14:09:23 -0400
Somtimes a nested function within the body of the with-current-buffer will change the buffer and move point in the original buffer and this may largely only be known at run-time.

-- Bob

> On May 9, 2021, at 12:11 PM, Eli Zaretskii <eliz <at> gnu.org> wrote:
> 
> 
>> 
>> From: Robert Weiner <rsw <at> gnu.org>
>> Date: Sun, 9 May 2021 11:57:25 -0400
>> Cc: 48294 <at> debbugs.gnu.org
>> 
>> Thanks for the feedback.  Everything you wrote is very clear.  The issue, however, is that I want to save
>> point in the current buffer prior to switching context to the 'with-current-buffer' buffer, just as my
>> (save-excursion (set-buffer ...)) code does.  If I move the save-excursion into the with-current-buffer body,
>> then it applies to the new buffer not the original one and if in that same body we change buffers again to the
>> original and move point, then that movement will stay in place.  So the question is, what is the appropriate
>> code that the byte-compiler will accept when you want to save your original place before switching buffers. 
> 
> I don't understand: with-current-buffer doesn't change point of the
> original buffer, so why do you need to save it?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#48294; Package emacs. (Sun, 09 May 2021 18:25:01 GMT) Full text and rfc822 format available.

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

From: Eli Zaretskii <eliz <at> gnu.org>
To: Robert Weiner <rswgnu <at> gmail.com>
Cc: 48294 <at> debbugs.gnu.org
Subject: Re: bug#48294: Use 'with-current-buffer' byte-compiler warning seems
 wrong
Date: Sun, 09 May 2021 21:24:41 +0300
> From: Robert Weiner <rswgnu <at> gmail.com>
> Date: Sun, 9 May 2021 14:09:23 -0400
> Cc: 48294 <at> debbugs.gnu.org
> 
> Somtimes a nested function within the body of the with-current-buffer will change the buffer and move point in the original buffer and this may largely only be known at run-time.

Then I'd use save-excursion outside of with-current-buffer.




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

This bug report was last modified 2 years and 317 days ago.

Previous Next


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