Subject: bug#36894: Restore frameset focus and selectedness

From: Juri Linkov <[email-masked]>
Date: Wed, 14 Aug 2019 23:50:44 +0300

>>  > The window manager always selects the last created frame, i.e.
>>  > it seems it's impossible to create a new frame without selecting it.
>> 
>> The xfce window manager has an option for whether a new window should
>> be automatically given focus or not.
>
> I'm not sure I understand what you both are saying.  Are you saying
> that this problem is impossible to solve in Emacs?

The problem can be solved with this patch:

diff --git a/lisp/frameset.el b/lisp/frameset.el
index 60b6fe38ad..4f7800630c 100644
--- a/lisp/frameset.el
+++ b/lisp/frameset.el
@@ -1346,6 +1352,16 @@ frameset-restore
 	    (error
 	     (delay-warning 'frameset (error-message-string err) :warning))))))
 
+    ;; Make sure the last selected frame has focus.
+    (let ((last-focus-frame
+           (catch 'last-focus
+             (maphash (lambda (frame _)
+                        (when (frame-parameter frame 'last-focus-update)
+                          (throw 'last-focus frame)))
+                      frameset--action-map))))
+      (when last-focus-frame
+        (select-frame-set-input-focus last-focus-frame)))
+
     ;; Make sure there's at least one visible frame.
     (unless (or (daemonp)
 		(catch 'visible