Subject: Re: bug#10955: 24.0.94; Closing Speedbar frame raises error

From: martin rudalics <[email-masked]>
Date: Tue, 06 Mar 2012 16:36:17 +0100

> 0. emacs -Q
 > 1. Check "Enter Debugger on Error" in the Options menu.
 > 2. M-x speedbar
 > 3. With the Speedbar frame selected, type `q'.
 > =>The Speedbar frame vanishes, and in the remaining frame, the following
 > backtrace appears:
 >
 > Debugger entered--Lisp error: (error "Attempt to delete the sole visible or iconified frame")
 >   delete-frame(#<frame Speedbar 1.0 0x89f3f80>)
 >   window--delete(#<window 8 on  SPEEDBAR> t t)
 >   replace-buffer-in-windows(#<buffer  SPEEDBAR>)
 >   kill-buffer(#<buffer  SPEEDBAR>)
 >   dframe-close-frame()
 >   call-interactively(dframe-close-frame nil nil)
 >
 > No error is raised if steps 0-2 are repeated and at step 3 `Q'
 > (delete-frame) is typed.
 >
 > Note that the frame consists of a single window containing the
 > *Backtrace* buffer (normally, a backtrace splits the window).  Moreover,
 > typing `q' (top-level) in this buffer deletes the backtrace, but an
 > empty *Backtrace* buffer remains as the current buffer (normally, `q'
 > kills the *Backtrace* buffer.)

At the time `window--delete' is called the speedbar frame is invisible
(you should be able to verify this by calling

   (frame-visible-p (window-frame window))

right at the beginning of `window--delete').  `dframe-frame-mode' has

	      (make-frame-invisible (symbol-value frame-var))))

and so we have found someone responsible for this behavior.  Now there's
nothing wrong with making a frame invisible and then trying to delete
it.  What happens, however, is that `delete-frame' is determined to
delete a frame if and only if there are "other visible frames" which
sounds like a reasonable condition.  Unfortunately, it's implementation
seems wrong.  Look at the code below called for the invisible frame f:

other_visible_frames (FRAME_PTR f)
{
   /* We know the selected frame is visible,
      so if F is some other frame, it can't be the sole visible one.  */
   if (f == SELECTED_FRAME ()) <---- f is selected, so we take this
     {
       Lisp_Object frames;
       int count = 0; <---- we set this to zero

       for (frames = Vframe_list; <---- there are two frames, a visible and an invisible one
	   CONSP (frames);
	   frames = XCDR (frames))
	{
	  Lisp_Object this;

	  this = XCAR (frames);
	  /* Verify that the frame's window still exists
	     and we can still talk to it.  And note any recent change
	     in visibility.  */
#ifdef HAVE_WINDOW_SYSTEM
	  if (FRAME_WINDOW_P (XFRAME (this)))
	    {
	      x_sync (XFRAME (this));
	      FRAME_SAMPLE_VISIBILITY (XFRAME (this));
	    }
#endif

	  if (FRAME_VISIBLE_P (XFRAME (this)) <---- Neither of these three disjuncts holds for f
	      || FRAME_ICONIFIED_P (XFRAME (this))
	      /* Allow deleting the terminal frame when at least
		 one X frame exists!  */
	      || (FRAME_WINDOW_P (XFRAME (this)) && !FRAME_WINDOW_P (f)))
	    count++; <---- incremented once only, for the sole visible frame
	}
       return count > 1; <---- fails since count == 1
     }
   return 1;
}

So this code makes it impossible to delete an invisible (or iconified)
frame if it's selected and there is only one other visible frame.  The
attached patch should fix this but I'm not very sure whether it's
correct.

martin
=== modified file 'src/frame.c'
--- src/frame.c	2012-01-19 07:21:25 +0000
+++ src/frame.c	2012-03-06 15:18:55 +0000
@@ -1143,14 +1143,15 @@
 	    }
 #endif
 
-	  if (FRAME_VISIBLE_P (XFRAME (this))
-	      || FRAME_ICONIFIED_P (XFRAME (this))
-	      /* Allow deleting the terminal frame when at least
-		 one X frame exists!  */
-	      || (FRAME_WINDOW_P (XFRAME (this)) && !FRAME_WINDOW_P (f)))
+	  if (f != XFRAME (this)
+	      && (FRAME_VISIBLE_P (XFRAME (this))
+		  || FRAME_ICONIFIED_P (XFRAME (this))
+		  /* Allow deleting the terminal frame when at least
+		     one X frame exists!  */
+		  || (FRAME_WINDOW_P (XFRAME (this)) && !FRAME_WINDOW_P (f))))
 	    count++;
 	}
-      return count > 1;
+      return count;
     }
   return 1;
 }