unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Behaviour for save-excursion
@ 2009-10-06 13:44 Nathaniel Flath
  2009-10-06 14:44 ` martin rudalics
  2009-10-06 14:48 ` Chong Yidong
  0 siblings, 2 replies; 3+ messages in thread
From: Nathaniel Flath @ 2009-10-06 13:44 UTC (permalink / raw)
  To: emacs-devel

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

What is the proper behaviour for save-excursion?  The emacs-lisp intro and
the docstring seem to have slightly differing explanations - namely, the
docstring mentions that it saves current-buffer as well as point and mark,
and the emacs-lisp intro does not.  Based on just what it does, I'm not
entirely sure which is correct - the following code snippet, executed in
*scratch*, returns the "*scratch*" buffer but the buffer displayed is the
"bar" buffer, and evaluating current-bufer afterwards returns the "bar"
buffer.

(progn
  (save-excursion
    (switch-to-buffer "bar"))
  (current-buffer))

Either the manual or the docstring should be changed to reflect what it
does, but I'm not entirely sure which based on this.

Thanks,
Nathaniel Flath

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Behaviour for save-excursion
  2009-10-06 13:44 Behaviour for save-excursion Nathaniel Flath
@ 2009-10-06 14:44 ` martin rudalics
  2009-10-06 14:48 ` Chong Yidong
  1 sibling, 0 replies; 3+ messages in thread
From: martin rudalics @ 2009-10-06 14:44 UTC (permalink / raw)
  To: Nathaniel Flath; +Cc: emacs-devel

 > What is the proper behaviour for save-excursion?  The emacs-lisp intro and
 > the docstring seem to have slightly differing explanations - namely, the
 > docstring mentions that it saves current-buffer as well as point and mark,
 > and the emacs-lisp intro does not.  Based on just what it does, I'm not
 > entirely sure which is correct - the following code snippet, executed in
 > *scratch*, returns the "*scratch*" buffer but the buffer displayed is the
 > "bar" buffer, and evaluating current-bufer afterwards returns the "bar"
 > buffer.
 >
 > (progn
 >   (save-excursion
 >     (switch-to-buffer "bar"))
 >   (current-buffer))
 >
 > Either the manual or the docstring should be changed to reflect what it
 > does, but I'm not entirely sure which based on this.

Please read the Elisp manual entry on `save-excursion' which states:

      Likewise, `save-excursion' does not restore window-buffer
      correspondences altered by functions such as `switch-to-buffer'.
      One way to restore these correspondences, and the selected window,
      is to use `save-window-excursion' inside `save-excursion' (*note
      Window Configurations::).

Also the following entry from section 27.2 The Current Buffer of the
Elisp manual might provide further explanation:

      When an editing command returns to the editor command loop, the
   command loop designates the buffer displayed in the selected window as
   current, to prevent confusion: the buffer that the cursor is in when
   Emacs reads a command is the buffer that the command will apply to.
   (*Note Command Loop::.)  Therefore, `set-buffer' is not the way to
   switch visibly to a different buffer so that the user can edit it.  For
   that, you must use the functions described in *Note Displaying
   Buffers::.

martin




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Behaviour for save-excursion
  2009-10-06 13:44 Behaviour for save-excursion Nathaniel Flath
  2009-10-06 14:44 ` martin rudalics
@ 2009-10-06 14:48 ` Chong Yidong
  1 sibling, 0 replies; 3+ messages in thread
From: Chong Yidong @ 2009-10-06 14:48 UTC (permalink / raw)
  To: Nathaniel Flath; +Cc: emacs-devel

Nathaniel Flath <flat0103@gmail.com> writes:

> What is the proper behaviour for save-excursion?  The emacs-lisp intro
> and the docstring seem to have slightly differing explanations -
> namely, the docstring mentions that it saves current-buffer as well as
> point and mark, and the emacs-lisp intro does not.

The Lisp intro does say that it saves current-buffer:

     In addition to recording the values of point and mark,
  `save-excursion' keeps track of the current buffer, and restores it,
  too.


> Based on just what it does, I'm not entirely sure which is correct -
> the following code snippet, executed in *scratch*, returns the
> "*scratch*" buffer but the buffer displayed is the "bar" buffer, and
> evaluating current-bufer afterwards returns the "bar" buffer.

You shouldn't use switch-to-buffer in Lisp code unless you know what
you're doing.  That's why the docstring for switch-to-buffer has this:

  WARNING: This is NOT the way to work on another buffer temporarily
  within a Lisp program!  Use `set-buffer' instead.  That avoids
  messing with the window-buffer correspondences.




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2009-10-06 14:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-10-06 13:44 Behaviour for save-excursion Nathaniel Flath
2009-10-06 14:44 ` martin rudalics
2009-10-06 14:48 ` Chong Yidong

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