emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: `save-excursion' defeated by `set-buffer'


From: David Kastrup
Subject: Re: `save-excursion' defeated by `set-buffer'
Date: Wed, 23 Dec 2009 10:07:03 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.90 (gnu/linux)

Stefan Monnier <address@hidden> writes:

>>> So either you want to use
>>> 
>>> (save-current-buffer (set-buffer foo) (goto-char (point-min)))
>>> aka
>>> (with-current-buffer foo (goto-char (point-min)))
>
>> Says who?
>
> I, based on my experience.
>
>> You mean "so either you _could_ use".  But save-excursion
>> does save the mark in the current buffer, and it does save point and
>> mark of the current buffer.
>
>> Please check the difference of
>> (with-temp-buffer
>>   (let ((buf1 (current-buffer)))
>>      (insert "xxxx")
>>      (prin1 (point))
>>      (save-excursion
>>        (with-temp-buffer
>>           (with-current-buffer buf1
>>             (goto-char (point-min)))))
>>      (prin1 (point))))
>> and
>> (with-temp-buffer
>>   (let ((buf1 (current-buffer)))
>>      (insert "xxxx")
>>      (prin1 (point))
>>      (with-temp-buffer
>>         (with-current-buffer buf1
>>           (goto-char (point-min))))
>>      (prin1 (point))))
>
>> You'll see that save-excursion restores point (and mark) even when
>> temporarily moving into some other buffer.
>
> Irrelevant: neither example uses (save-excursion (set-buffer ..) ...).

Don't be disingenuous.  with-temp-buffer uses with-current-buffer, which
is basically (save-current-buffer (set-buffer ...

>>> if moving point in foo is what you wanted, or
>>> (with-current-buffer foo (save-excursion (goto-char (point-min))))
>>> if you didn't want to move point in foo.
>
>> And what if I wanted to have mark, point, and buffer restored, as the
>
> Obviously "if you didn't want to move point in foo" implies that you did
> not want "to have mark, point, and buffer restored".

I don't see the obviousness.  For example, there are recursive editing
operations (like in the debugger) which change the buffer, where the
user can switch around buffers and change point and stuff at will, but
where it is important that upon return from the save-excursion,
current-buffer _and_ point are where they were before.  And the DOC
string of save-excursion says that it will do that, and it does not say
that if you use it for that purpose, the compiler will get into
hysterics.

And even _if_ using save-excursion for saving _both_ buffer and its
point was deprecated (for which I see no good reason whatsoever): as
long as the DOC string of save-excursion does not even _mention_
save-current-buffer, it is not appropriate to throw warnings at the
user.

-- 
David Kastrup





reply via email to

[Prev in Thread] Current Thread [Next in Thread]