Subject: Re: bug#15469: 24.3; emacsclient -c fails to grab focus when asking“Revert buffer from file?”

From: Lars Ingebrigtsen <[email-masked]>
Date: Sun, 06 Dec 2020 17:14:45 +0100

Eli Zaretskii <[email-masked]> writes:

>> > We ask the question before we switch to the new frame, so I guess the
>> > question appears in the previous frame?
>> 
>> No, it appears in the new frame.
>
> OK, but still: we raise the frame only when we are done, right?

Yes.  I instrumented raise-frame, and it was indeed called after I
answered "yes" to the "revert" question:

Debugger entered--entering a function:
* raise-frame(#<frame  *Minibuf-1* 0x5652caf5a498>)
  select-frame-set-input-focus(#<frame  *Minibuf-1* 0x5652caf5a498>)
  server-switch-buffer(#<buffer foo> nil nil #<frame  *Minibuf-1* 0x5652caf5a498>)
  server-execute(#<process server <2>> (("/tmp/foo")) nil nil t #<frame  *Minibuf-1* 0x5652caf5a498> window-system)
  #f(compiled-function () #<bytecode -0x1de923e1af168e73>)()
  server-execute-continuation(#<process server <2>>)
  server-process-filter(#<process server <2>> "-env SHELL=/bin/bash -env SESSION_MANAGER=local/xo...")

So just raising the frame before we process the buffers seems to fix the
problem for me.

Comments?

diff --git a/lisp/server.el b/lisp/server.el
index 2fd94552dd..aa7297ba48 100644
--- a/lisp/server.el
+++ b/lisp/server.el
@@ -1328,6 +1328,8 @@ server-execute
   ;; inhibit-quit flag, which is good since `commands' (as well as
   ;; find-file-noselect via the major-mode) can run arbitrary code,
   ;; including code that needs to wait.
+  (when (and frame server-raise-frame)
+    (select-frame-set-input-focus frame))
   (with-local-quit
     (condition-case err
         (let ((buffers (server-visit-files files proc nowait)))
@@ -1676,9 +1678,7 @@ server-switch-buffer
                   (switch-to-buffer next-buffer))
 	      ;; After all the above, we might still have ended up with
 	      ;; a minibuffer/dedicated-window (if there's no other).
-	      (error (pop-to-buffer next-buffer)))))))
-    (when server-raise-frame
-      (select-frame-set-input-focus (window-frame)))))
+	      (error (pop-to-buffer next-buffer)))))))))
 
 ;;;###autoload
 (defun server-save-buffers-kill-terminal (arg)

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no