[Top][All Lists]

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

narrowing, indirect buffers and set-buffer

From: Stephen Berman
Subject: narrowing, indirect buffers and set-buffer
Date: Mon, 26 Oct 2009 23:32:42 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)

At first the following looked like a bug to me:

1. emacs -Q
2. C-x b bla RET
3. M-: (insert "This is a test.")
4. M-: (narrow-to-region 5 10) 
=> buffer "bla" now displays " is a"
5. M-: (make-indirect-buffer (current-buffer) "blip")
6. M-: (save-restriction
        (set-buffer (get-buffer-create "bloop")))
=> buffer "bla" now displays "This is a test."

Contrast the above with (after killing buffer "bla") the same steps 1-4
and then step 6, i.e., leaving out step 5: then narrowing in "bla"
remains in effect.  So calling make-indirect-buffer appeared to disable
save-restriction when it contains a set-buffer call.

But then I read the following in (elisp)Current Buffer:

   *Warning:* Lisp functions that change to a different current buffer
   should not depend on the command loop to set it back afterwards.
   Editing commands written in Emacs Lisp can be called from other programs
   as well as from the command loop; it is convenient for the caller if
   the subroutine does not change which buffer is current (unless, of
   course, that is the subroutine's purpose).  Therefore, you should
   normally use `set-buffer' within a `save-current-buffer' or
   `save-excursion' (*note Excursions::) form that will restore the
   current buffer when your function is done.

And indeed, if step 6 above (i.e., after make-indirect-buffer) is
replaced by this: 

6.' M-: (save-restriction
          (set-buffer (get-buffer-create "bloop"))))

or by this:

6.'' M-: (save-restriction
           (set-buffer (get-buffer-create "bloop"))))

then the narrowing in "bla" still remains in effect (also if the call to
widen is between save-restriction and save-current-buffer/save-excursion).

So is the failure of narrowing to remain in effect above not a bug but a
programming error, which should be correctly written as 6' or 6''?  The
reason I'm unsure is that the failure only happens after calling
make-indirect-buffer, which is not what I would expect from the passage
in the Elisp manual quoted above.  Can someone explain this?

Steve Berman

reply via email to

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