Subject: bug#4851: 23.1.50; narrowing, indirect buffers and set-buffer

From: Stephen Berman <[email-masked]>
Date: Mon, 02 Nov 2009 15:49:37 +0100

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

There is an apparently relevant passage 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
         (save-current-buffer
          (widen)
          (set-buffer (get-buffer-create "bloop"))))

or by this:

6.'' M-: (save-restriction
          (save-excursion
           (widen)
           (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 perhaps the failure of narrowing to remain in effect above is not a
bug but a programming error, which should be correctly written as 6' or
6''.  But I'm not sure, since 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.  If this is not an Emacs bug can someone
explain why the failure of narrowing to remain in effect is only
triggered by make-indirect-buffer?

In GNU Emacs 23.1.50.1 (i686-pc-linux-gnu, GTK+ Version 2.14.4)
 of 2009-10-27 on escher
Windowing system distributor `The X.Org Foundation', version 11.0.10502000
Important settings:
  value of $LC_ALL: nil
  value of $LC_COLLATE: nil
  value of $LC_CTYPE: nil
  value of $LC_MESSAGES: nil
  value of $LC_MONETARY: nil
  value of $LC_NUMERIC: nil
  value of $LC_TIME: nil
  value of $LANG: en_US.UTF-8
  value of $XMODIFIERS: @im=local
  locale-coding-system: utf-8-unix
  default enable-multibyte-characters: t