Subject: bug#36894: Restore frameset focus and selectedness

From: Juri Linkov <[email-masked]>
Date: Wed, 14 Aug 2019 00:29:15 +0300

>> Maybe then the desktop should save the selectedness as well.
>
> You mean, restore last-focus-update?  If you add it to
> desktop-globals-to-save, is it restored?

'last-focus-update' is a frame parameter saved in 'desktop-saved-frameset'.

>> > But I asked for a reproducible recipe, and I don't think you have
>> > shown one.  Could you please do that?  I'd like to study it.
>> 
>> In your case above, if you save the frames is such order that
>> the selected frame is not saved last, can you reproduce the problem?
>> I see this problem often when the last frame is not selected.
>
> How can this happen?  In my use, desktop is saved when I kill Emacs,
> and in that case the selected frame is always the last one saved.
>
> That's hwy I asked for a reproducible recipe: I think there are some
> factors at work on your system which I don't understand.

Indeed something strange happens, here is a complete recipe:

0. emacs -Q
1. C-h C-t   (view-emacs-todo)
2. C-x 5 2   (make-frame-command)
3. C-h C-n   (view-emacs-news)
4. C-x 5 o   (other-frame)
5. M-x desktop-save RET RET
6. C-x C-c

7. emacs -Q -f desktop-read --eval '(message "selected-frame: %S" (selected-frame))'
8. M-: (selected-frame) RET

9. In the *Messages* buffer the printed selected-frame is not the same.
   See more explanations of messages in the *Messages* buffer below:

The next message comes from 'message' added to frameset.el:
frameset-restore frame-list: (#<frame emacs@localhost 0x5572e78000c0> #<frame emacs@localhost 0x5572e71a9eb0>)

The next message comes from 'message' added to frameset.el:
frameset-restore selected-frame: #<frame emacs@localhost 0x5572e71a9eb0>

The next message comes from desktop.el:
Desktop: 2 frames, 2 buffers restored.

The next message comes from 'message' added to desktop.el:
desktop-read selected-frame: #<frame TODO 0x5572e71a9eb0>

The next message comes from 'message' on the command line:
selected-frame: #<frame TODO 0x5572e71a9eb0>

The next message comes from 'message' added to startup.el:
normal-top-level selected-frame: #<frame TODO 0x5572e71a9eb0>

The next message comes from M-: (selected-frame)
#<frame  *Minibuf-1* 0x5572e78000c0>

Here 0x5572e71a9eb0 is the frame with TODO, and 0x5572e78000c0 with NEWS.
(0x5572e71a9eb0 was reused, but 0x5572e78000c0 is a new frame created by frameset-restore)
After loading the former should be selected, but actually the latter is selected.

The 'message' added at the end of 'normal-top-level' shows one value
of selected-frame, but evaluating M-: (selected-frame) immediately after loading
shows another value of selected-frame.

Conclusion:
The window manager always selects the last created frame, i.e.
it seems it's impossible to create a new frame without selecting it.

Here is complete information:

In GNU Emacs 27.0.50 (build 84, x86_64-pc-linux-gnu, GTK+ Version 3.22.30)
 of 2019-08-13 built on localhost
Repository revision: 2b329ed420eb15f6738edd402697ac2876b2aa61
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.11906000
System Description: Linux Mint 19.1

Configured using:
 'configure --with-imagemagick'

Configured features:
XPM JPEG TIFF GIF PNG RSVG IMAGEMAGICK SOUND GPM DBUS GSETTINGS GLIB
NOTIFY INOTIFY ACL LIBSELINUX GNUTLS LIBXML2 FREETYPE HARFBUZZ M17N_FLT
LIBOTF XFT ZLIB TOOLKIT_SCROLL_BARS GTK3 X11 XDBE XIM THREADS PDUMPER
LCMS2 GMP

Important settings:
  value of $LC_MONETARY: fi_FI.UTF-8
  value of $LC_NUMERIC: fi_FI.UTF-8
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Outline

Minor modes in effect:
  bug-reference-mode: t
  tooltip-mode: t
  global-eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  buffer-read-only: t
  line-number-mode: t
  transient-mark-mode: t
  view-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message rmc puny dired dired-loaddefs
format-spec rfc822 mml mml-sec password-cache epa derived epg epg-config
gnus-util rmail rmail-loaddefs text-property-search mm-decode mm-bodies
mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader sendmail
rfc2047 rfc2045 ietf-drums mm-util mail-prsvr mail-utils cl-seq cl-extra
help-mode time-date subr-x goto-addr thingatpt seq byte-opt gv bytecomp
byte-compile cconv view vc-git diff-mode easymenu bug-reference noutline
outline easy-mmode desktop frameset cl-loaddefs cl-lib tooltip eldoc
electric uniquify ediff-hook vc-hooks lisp-float-type mwheel term/x-win
x-win term/common-win x-dnd tool-bar dnd fontset image regexp-opt fringe
tabulated-list replace newcomment text-mode elisp-mode lisp-mode
prog-mode register page menu-bar rfn-eshadow isearch timer select
scroll-bar mouse jit-lock font-lock syntax facemenu font-core
term/tty-colors frame cl-generic cham georgian utf-8-lang misc-lang
vietnamese tibetan thai tai-viet lao korean japanese eucjp-ms cp51932
hebrew greek romanian slovak czech european ethiopic indian cyrillic
chinese composite charscript charprop case-table epa-hook jka-cmpr-hook
help simple abbrev obarray minibuffer cl-preloaded nadvice loaddefs
button faces cus-face macroexp files text-properties overlay sha1 md5
base64 format env code-pages mule custom widget hashtable-print-readable
backquote threads dbusbind inotify lcms2 dynamic-setting
system-font-setting font-render-setting move-toolbar gtk x-toolkit x
multi-tty make-network-process emacs)

Memory information:
((conses 16 55782 4941)
 (symbols 48 7047 1)
 (strings 32 19182 1991)
 (string-bytes 1 619171)
 (vectors 16 11862)
 (vector-slots 8 157169 9776)
 (floats 8 36 26)
 (intervals 56 229 0)
 (buffers 992 13))