unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: Robert Weiner <rsw@gnu.org>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 48294@debbugs.gnu.org
Subject: bug#48294: Use 'with-current-buffer' byte-compiler warning seems wrong
Date: Sun, 9 May 2021 11:57:25 -0400	[thread overview]
Message-ID: <CA+OMD9h2nUocnLksc7zMnVjY_itGM9xOaz3RL5adUvnPca21dw@mail.gmail.com> (raw)
In-Reply-To: <83k0o88h7o.fsf@gnu.org>

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

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@gnu.org> wrote:

> tags 48294 notabug
> thanks
>
> > From: Robert Weiner <rsw@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.
>

[-- Attachment #2: Type: text/html, Size: 3961 bytes --]

  reply	other threads:[~2021-05-09 15:57 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-08 20:14 bug#48294: Use 'with-current-buffer' byte-compiler warning seems wrong Robert Weiner
2021-05-09  8:05 ` Eli Zaretskii
2021-05-09 15:57   ` Robert Weiner [this message]
2021-05-09 16:11     ` Eli Zaretskii
2021-05-09 18:09       ` Robert Weiner
2021-05-09 18:24         ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CA+OMD9h2nUocnLksc7zMnVjY_itGM9xOaz3RL5adUvnPca21dw@mail.gmail.com \
    --to=rsw@gnu.org \
    --cc=48294@debbugs.gnu.org \
    --cc=eliz@gnu.org \
    --cc=rswgnu@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).