From: "Drew Adams" <drew.adams@oracle.com>
To: "'Uday S Reddy'" <u.s.reddy@cs.bham.ac.uk>,
"'Tim X'" <timx@nospam.dev.null>
Cc: help-gnu-emacs@gnu.org
Subject: RE: `save-excursion' defeated by `set-buffer'
Date: Mon, 14 Mar 2011 01:47:45 -0700 [thread overview]
Message-ID: <14D78D0C9BD34D95A0C192BCAB75E7ED@us.oracle.com> (raw)
In-Reply-To: <19836.48264.656000.373465@gargle.gargle.HOWL>
> It takes quite a bit of insight into the code to understand that it is
> actually *harmful* redundancy.
What redundancy? What harm?
> The unintentional uses of save-excursion cover up potential
> bugs inside the code which don't get noticed because of this
> redundant saving.
Redundant saving? What redundant saving? Each use of `save-excursion' saves
and restores the buffer that was originally current along with its point and
mark. Where's the redundancy? Where's the harm?
> So, my test is pure and simple. Convert all the save-excursion's that
> the compiler complains about to save-current-buffer.
OMG.
> If your code continues to run perfectly, you are my hero.
> If your code falls over, then you should accept that your code is
> buggy
If it wasn't before, it probably is after your near-global search-and-replace.
And what if your code doesn't seem to fall over immediately, and it takes you 2
years to discover that it was misguided after all not to save and restore point
in that particular case?
> and it is only able to stand up on the crutches of what you thought
> was "harmless redundancy".
Please tell us more about the harmless redundancy and the harmful redundancy.
Or even just about the redundancy.
> Then it is up to you whether you want to fix the code or
> continue to live with it. But there are many of us who won't touch
> such code with a tadpole because we regard it as buggy.
>
> Unfortunately, it is more than a question of clarity. It is a
> question of correctness. Let me restate my example from Friday a bit
> more explicitly
>
> (defun my-beautiful-function ()
> (save-excursion
> (set-buffer B)
> (goto-char x)
> (setq a (find-something))
> ...))
>
> (defun find-something ()
> (....
> (set-buffer A)
> (goto-char y)
> ...))
>
> find-something is *buggy* here. It is moving point in buffer A though
> it wasn't supposed to.
Why wasn't it supposed to? How can you know from this skeleton whether it was
supposed to move point in A or not?
Or are you just telling us that it is _hypothetically_ buggy, that
_hypothetically_ it is not supposed to move point in A, but it does?
Tell us more. What buffer is `m-b-f' called in? Presumably, if it is intended
to be called in buffer A, then it wants to restore point in A when it is done
(assuming the `save-excursion' is the last thing it does).
Presumably, if it is intended instead to be called in buffer C then it wants to
restore point in C when it is done. In this case, presumably (if we don't just
assume that it is buggy) `find-something' wants to leave point in A at Y
(assuming it doesn't move it in the last `...').
There is no way to know from your skeleton what the purpose or the behavior is.
The devil is in the details...which are missing.
There is nothing intrinsically wrong with a function that moves point to some
position in some buffer and leaves it there. I probably wouldn't name such a
function just `find-something', however, and I would document that part of its
purpose is to move point in buffer A or whatever. Assuming that is the case.
> However, my-beautiful-function works fine as
> long as I call it in buffer A because the "harmlessly redundant"
> save-excursion at its top-level restores A's point.
Oh, so you were presuming that `m-b-f' wants to restore A's point. OK. Where's
the problem? (So far nothing is _redundant_, however.)
If `m-b-f' intends to always do things in buffer A and afterward restore point
in A to what it was initially, and if it doesn't know which buffer it will be
called from, then it should wrap (with-current-buffer A ...) around its
`save-excursion'.
> Tomorrow, you decide to call my-beautiful-function from some other
> buffer C, and all hell breaks loose. You suddenly find that the point
> is moving in the unrelated buffer A and you have no idea why.
Well you should know. You should know from the name of `find-something' (which
needs a better name) and from its doc that it moves point in A.
Or if by hypothesis `f-s' is buggy and should not in fact move point in A, then
you should be able to see by debugging that it in fact does move point in A,
even when `m-b-f' is called in A. And you should be able to tell just by
looking at the code that it moves point in A.
I don't deny that if the overall code is complex such a bug might not be
obvious, but I submit that this is a general problem that has nothing special to
do with `save-excursion' or `set-buffer'. It is simply about keeping track of
the side effects performed by your code.
`find-something' as written has the side effect of moving point in buffer A -
always. If that is not the intention, then yes, its bug will not surface as
long as it is called inside a `save-excursion' for buffer A.
The same problem would exist for a function that increments a variable X by 10
and is called by a function within a `let' that binds it to 42. You might never
notice the incrementing as long as that `let' binding is there. Call the
incrementing function from another context and you might suddenly discover the
incrementing.
There are a zillion cases of such things in a language like Lisp. Given both
its side effects and dynamic binding there are all kinds of possible pitfalls in
Lisp.
It should be very clear that a construct that restores point for the current
buffer will restore point for the current buffer (!). Restoring point for a
given buffer hides point movements that took place between saving and restoring
- of course. The same would be true if you instead used your proposed
`save-point-and-mark' to do the saving and restoring. Nothing remarkable here
at all.
Nothing mysterious. Except for the various `...' and the context of use
(invocation). Without knowing those things, without knowing the intended
behavior, the behavior could be nearly anything.
> So, rewrite the code as follows:
>
> (defun my-beautiful-function ()
> (save-current-buffer
> (set-buffer B)
> (goto-char x)
> ...
> (setq a (find-something))
> ...))
>
> (defun find-something ()
> (....
> (save-current-buffer
> (set-buffer A)
> (save-excursion
> (goto-char y)
> ...)))
>
> Not only would you have mollified the compiler, but you will have much
> more robust code that will continue to function when used in
> unforeseen ways.
Oh, so you meant that `m-b-f' doesn't need to do anything specifically in A, and
it need not be called from A. Such details were missing. And `f-s' should not
move point in A (the buggy hypothesis). In that case, sure, that's a perfectly
fine way to do what you apparently want.
It all depends on what the intended behavior is. You might well want `f-s' to
move point in A. You might, as part of its contract, know that `m-b-f' will
always be called from A, or from C, or whatever.
next prev parent reply other threads:[~2011-03-14 8:47 UTC|newest]
Thread overview: 78+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <mailman.0.1299085819.4487.help-gnu-emacs@gnu.org>
2011-03-02 20:54 ` `save-excursion' defeated by `set-buffer' Uday Reddy
2011-03-05 4:28 ` Stefan Monnier
2011-03-10 19:57 ` Andreas Röhler
2011-03-11 1:07 ` Leo
2011-03-11 1:28 ` Stefan Monnier
2011-03-11 8:52 ` Andreas Röhler
[not found] ` <mailman.25.1299833256.26376.help-gnu-emacs@gnu.org>
2011-03-11 9:54 ` David Kastrup
2011-03-11 11:09 ` Andreas Röhler
[not found] ` <mailman.10.1299841456.13496.help-gnu-emacs@gnu.org>
2011-03-11 11:38 ` David Kastrup
2011-03-11 15:52 ` Stefan Monnier
2011-03-12 8:59 ` Eli Zaretskii
2011-03-12 19:00 ` Andreas Röhler
2011-03-12 19:56 ` Drew Adams
2011-03-12 20:27 ` Eli Zaretskii
2011-03-12 22:20 ` Drew Adams
2011-03-14 18:59 ` Juanma Barranquero
2011-03-14 21:17 ` Drew Adams
[not found] ` <mailman.6.1299959800.9013.help-gnu-emacs@gnu.org>
2011-03-13 3:00 ` Uday Reddy
2011-03-13 18:22 ` Drew Adams
[not found] ` <mailman.0.1300040583.10860.help-gnu-emacs@gnu.org>
2011-03-14 1:04 ` Uday Reddy
2011-03-14 8:43 ` Drew Adams
[not found] ` <mailman.4.1299956141.9013.help-gnu-emacs@gnu.org>
2011-03-12 22:29 ` Tim X
2011-03-13 7:15 ` Andreas Röhler
2011-03-13 18:23 ` Drew Adams
2011-03-13 12:46 ` Uday S Reddy
2011-03-14 8:47 ` Drew Adams [this message]
[not found] ` <mailman.7.1300092490.2602.help-gnu-emacs@gnu.org>
2011-03-14 12:22 ` Uday Reddy
2011-03-14 14:20 ` Uday S Reddy
2011-03-14 17:36 ` Drew Adams
2011-03-14 23:20 ` Uday S Reddy
2011-03-15 3:55 ` Drew Adams
[not found] ` <mailman.14.1300124226.2531.help-gnu-emacs@gnu.org>
2011-03-15 14:39 ` Stefan Monnier
2011-03-15 15:59 ` Drew Adams
[not found] ` <mailman.2.1300204800.1264.help-gnu-emacs@gnu.org>
2011-03-15 17:46 ` Stefan Monnier
2011-03-15 18:55 ` Drew Adams
2011-03-14 14:18 ` Andreas Röhler
[not found] ` <mailman.5.1300111985.27831.help-gnu-emacs@gnu.org>
2011-03-14 14:54 ` Uday Reddy
[not found] ` <mailman.5.1300000253.31664.help-gnu-emacs@gnu.org>
2011-03-13 14:16 ` Uday Reddy
2011-03-13 18:25 ` Drew Adams
[not found] ` <mailman.2.1300040743.10860.help-gnu-emacs@gnu.org>
2011-03-13 20:48 ` Uday Reddy
2011-03-14 0:31 ` Drew Adams
[not found] ` <mailman.4.1300066631.25374.help-gnu-emacs@gnu.org>
2011-03-14 14:34 ` Stefan Monnier
2011-03-13 2:11 ` Uday Reddy
2011-03-13 18:26 ` Drew Adams
[not found] ` <mailman.5.1299920357.7270.help-gnu-emacs@gnu.org>
2011-03-12 9:34 ` David Kastrup
2011-03-12 10:12 ` Eli Zaretskii
[not found] ` <mailman.10.1299924724.7270.help-gnu-emacs@gnu.org>
2011-03-12 10:42 ` David Kastrup
2011-03-12 12:28 ` Eli Zaretskii
2011-03-12 15:17 ` Uday Reddy
2011-03-12 15:25 ` David Kastrup
2011-03-13 2:40 ` Uday Reddy
2011-03-14 14:25 ` Stefan Monnier
2011-03-14 17:26 ` Andreas Röhler
[not found] ` <mailman.11.1300123292.2531.help-gnu-emacs@gnu.org>
2011-03-15 14:35 ` Stefan Monnier
2011-03-15 14:47 ` David Kastrup
2011-03-15 15:19 ` PJ Weisberg
2011-03-15 16:00 ` Drew Adams
[not found] ` <mailman.6.1300202904.14512.help-gnu-emacs@gnu.org>
2011-03-15 17:42 ` Stefan Monnier
[not found] ` <mailman.3.1300204850.1264.help-gnu-emacs@gnu.org>
2011-03-15 17:49 ` Stefan Monnier
2011-03-15 18:56 ` Drew Adams
2011-03-15 21:30 ` Stefan Monnier
2011-03-15 19:02 ` Jason Earl
2011-03-15 20:55 ` Drew Adams
2011-03-16 4:31 ` rusi
2011-03-16 8:11 ` David Kastrup
2011-03-17 3:46 ` rusi
2011-03-17 7:10 ` rusi
2011-03-17 8:29 ` Antoine Levitt
[not found] ` <mailman.1.1300215374.6982.help-gnu-emacs@gnu.org>
2011-03-16 12:05 ` Uday S Reddy
2011-03-12 22:18 ` Tim X
2011-03-11 23:06 ` Uday Reddy
2011-03-10 22:40 ` David Kastrup
2011-03-11 2:46 ` Stefan Monnier
2011-04-01 3:20 ` rusi
2011-04-01 12:39 ` Uday Reddy
2011-03-02 17:12 Andreas Röhler
2011-03-03 4:58 ` Le Wang
[not found] ` <mailman.7.1299128292.20537.help-gnu-emacs@gnu.org>
2011-03-13 15:05 ` Uday Reddy
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=14D78D0C9BD34D95A0C192BCAB75E7ED@us.oracle.com \
--to=drew.adams@oracle.com \
--cc=help-gnu-emacs@gnu.org \
--cc=timx@nospam.dev.null \
--cc=u.s.reddy@cs.bham.ac.uk \
/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.
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).