[Top][All Lists]

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

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

From: Stefan Monnier
Subject: Re: `save-excursion' defeated by `set-buffer'
Date: Tue, 22 Dec 2009 19:45:25 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.90 (gnu/linux)

>> 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 ..) ...).

>> 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".


reply via email to

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