Subject: Re: bug#11939: 24.1; `save-buffers-kill-emacs' loses minibuffer focus when it calls `list-processes'

From: martin rudalics <[email-masked]>
Date: Mon, 16 Jul 2012 11:12:18 +0200

> I'm afraid I still do not understand (but maybe I don't need to).  I thought the
 > problem has to do with input focus for minibuffer reading.  But maybe you are
 > saying that the minibuffer need not be involved, and that `read-event' will also
 > depend on which frame has the focus.
 >
 > In any case, please give me the specific recipe that you would like me to try.
 > I'm afraid I've lost track at this point.

`y-or-n-p' has the following two lines

                     (when minibuffer-auto-raise
                       (raise-frame (window-frame (minibuffer-window))))

So when you want to give focus to the minibuffer window you might want
to replace `raise-frame' by the stronger `select-frame-set-input-focus'

                     (when minibuffer-auto-raise
                       (select-frame-set-input-focus (window-frame (minibuffer-window))))

For this to work you have to defalias `yes-or-no-p' to `y-or-n-p'.

 > And is this still pertinent, given what we have discovered wrt a reproducible
 > recipe from emacs -Q?

As long as we have not found a solution, yes.

 >> I suppose that as long as you don't type either yes or no in the
 >> original frame nothing happens at all.
 >
 > Presumably _some_ buffer receives the input.  If it is a read-only buffer then I
 > would expect a read-only error message.  If it is not then I would expect to see
 > what I type in some buffer.  I see neither effect.  And presumably (it seems
 > that way anyway) it is the *Process List* buffer, which is read-only, that has
 > the focus.
 >
 > This behavior does not seem normal.

You would have to try with `y-or-n-p'.  read_minibuf has only

   Fmake_frame_visible (mini_frame);

   if (minibuffer_auto_raise)
     Fraise_frame (mini_frame);

which might not be sufficient for your case.  Once `y-or-n-p' with focus
selection works we can try to make it work in read_minibuf too.

 > The frame for posing and answering all questions should be, as usual, the
 > minibuffer frame.  Aside from `read-event' etc., reading user input should use
 > the minibuffer, and that means it should use the minibuffer frame.
 >
 > Looking at the C code for `yes-or-no-p' (Emacs 23.3), it clearly calls
 > `read-from-minibuffer' (`Fread_from_minibuffer'), so it should use the
 > minibuffer.  The frame that should have the input focus for that prompt and
 > reading should be the standalone minibuffer frame - that is the _only_ frame
 > that has a minibuffer.
 >
 > It makes no sense for any other frame to have the input focus when reading from
 > the minibuffer, since no other frame _has_ a minibuffer.

Most users don't care about standalone minibuffer frames.  They want to
see one frame where they find all information.

 >>  > More precisely, I don't have much of an opinion in the
 >>  > case of a non standalone minibuffer.
 >>  >
 >>  > But in the case of a standalone minibuffer, it definitely
 >>  > should continue to have the input focus.  That's the point, for me.
 >>
 >> Why "continue"?  You could invoke C-x C-c in any frame.
 >
 > See above.  It should have the input focus for any _reading from the
 > minibuffer_.  Obviously, other frames can have the input focus when not reading
 > from the minibuffer.  `yes-or-no-p' reads from the minibuffer.

So if another frame has input focus and emacs asks a `yes-or-no-p'
question, focus _should_ switch.

 > But the behavior I saw with that test indicated/suggested that *Help* was not
 > entirely special-display (with `pop-up-frames' t and the *Help* frame defined as
 > indicated with `special-display-buffer-names'.
 >
 > The frame was created with the correct colors etc., but buffer *Help* ended up
 > being replaced in the frame, which is not what "special display" means.  So I
 > think there might be a second bug here.

If this is independent from the behavior we discuss currently, we'll
have to investigate it.

 >>  > If I use Dired+ (which does what my patch does) then there
 >>  > is no problem, so in my daily use this is solved for the
 >>  > Dired but reported, but not in general (viz.
 >>  > this bug wrt quitting with active processes).
 >>
 >> But your patch doesn't address the input focus issue or am I missing
 >> something?
 >
 > Correct.  But the problem does not exist with my patch.  I don't have an
 > explanation.

Me neither.  You would have to go through it and find what it does
differently.

 > But please see the #15566 thread - in particular, the part about the use of code
 > that is nearly identical but that does not manifest the input problem.  See the
 > message I sent that has the attachment `throw-chmod-vs-chmod-rec.el'.  I do not
 > understand why what I report happens, but it happens.

 >> I have attached another version of `with-temp-buffer-window' which now
 >> explicitly shifts input focus to the frame selected at the time the
 >> macro is called.  I hope this will fix the `pop-up-frames' t scenario.
 >> I'm afraid it will not fix the problem when you invoke C-x C-c in any
 >> window but the minibuffer-only window so we probably have to fix that
 >> issue separately.  Please try it.
 >
 > Just what is the recipe to try (e.g. from emacs -Q)?

With your usual setting load the file and test the shell and the dired
scenarios.

 > Wrt "C-x C-c in any window but the minibuffer-only window": It's about reading
 > from the minibuffer (e.g. `read-from-minibuffer').  It never matters (apart from
 > this bug about new frames that get created during the minibuffer reading) which
 > buffer/frame is selected when you call `read-from-minibuffer'.  Focus is always
 > switched to the minibuffer (and its frame).

Because it does a simple `raise-frame' of the minibuffer frame which
seems to work when you don't pop up another frame before.

 > The problem here seems to be that just after focus is correctly switched to the
 > minibuffer frame, for minibuffer interaction (reading), the new frame is popped
 > up and MS Windows gives it the focus.  At least that's the (limited) conceptual
 > model I'm adopting so far.
 >
 > IOW, I don't think the problem is to give the minibuffer frame the focus.  I'm
 > assuming that that is happening correctly, as usual.  The problem, I think, is
 > that the new frame creation happens at the same time or just afterward, and that
 > steals the focus from the minibuffer frame (thanks to MS Windows).

It doesn't help to speculate.  I know of three functions to experiment
with (`select-frame' and `raise-frame' being apparently too weak):

- `select-frame-set-input-focus' which is probably too strong because it
   can change the Z-order thus obfuscating the frame where important
   information (like the list of directories to delete) is displayed,

- `x-focus-frame' which should redirect any input to the frame argument, and

- `redirect-frame-focus' which could be used to redirect input only from
   the frame where, for example, the marked directories are displayed to
   the minibuffer-only frame.

You can try to experiment with these, either in `y-or-n-p' or in
`with-temp-buffer-window'.  If neither of these helps to direct focus
away from the new frame, I see no solution.

martin