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: Mon, 21 Dec 2009 17:23:18 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.90 (gnu/linux)

Stefan Monnier <address@hidden> writes:

>> Me too I've seen these messages with some code I've been using. Yet
>> I wasn't sure how to interpret them / what to do with them. I looked
>> into the elisp manual, but I didn't find anything. It would be great
>> if someone could add a remark to the elisp manual and / or
>> docstrings about the relation between save-excursion, set-buffer and
>> with-current-buffer.
>
> save-excursion only saves point in the current buffer, so
>
>    (save-excursion (set-buffer foo) (goto-char (point-min)))
>
> will move point in foo and the point-saving done by save-excursion is
> useless.

The buffer saving isn't.  The DOC string of save-excursion states:

    save-excursion is a special form in `C source code'.

    (save-excursion &rest BODY)

    Save point, mark, and current buffer; execute BODY; restore those things.
    Executes BODY just like `progn'.
    The values of point, mark and the current buffer are restored
    even in case of abnormal exit (throw or error).
    The state of activation of the mark is also restored.

    This construct does not save `deactivate-mark', and therefore
    functions that change the buffer will still cause deactivation
    of the mark at the end of the command.  To prevent that, bind
    `deactivate-mark' with `let'.

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

> 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
DOC string of save-excursion states as the purpose of save-excursion
without telling you that the compiler will start nagging you if you use
it for that purpose?

I think the warning is a mistake.

-- 
David Kastrup





reply via email to

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